[421] | 1 | /* |
---|
| 2 | * sys_wait.c - wait termination or blocking of a child process. |
---|
| 3 | * |
---|
[440] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[421] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[421] | 25 | #include <hal_uspace.h> |
---|
[435] | 26 | #include <hal_irqmask.h> |
---|
[421] | 27 | #include <core.h> |
---|
| 28 | #include <thread.h> |
---|
| 29 | #include <process.h> |
---|
| 30 | #include <vmm.h> |
---|
| 31 | #include <printk.h> |
---|
| 32 | |
---|
| 33 | ///////////////////////////////// |
---|
| 34 | int sys_wait( uint32_t * status ) |
---|
| 35 | { |
---|
| 36 | error_t error; |
---|
[440] | 37 | vseg_t * vseg; |
---|
[421] | 38 | xptr_t iter_xp; |
---|
| 39 | xptr_t child_xp; |
---|
| 40 | process_t * child_ptr; |
---|
| 41 | cxy_t child_cxy; |
---|
| 42 | pid_t child_pid; |
---|
[446] | 43 | uint32_t child_state; |
---|
[433] | 44 | thread_t * child_thread; |
---|
[435] | 45 | reg_t save_sr; |
---|
[421] | 46 | |
---|
| 47 | thread_t * this = CURRENT_THREAD; |
---|
| 48 | process_t * process = this->process; |
---|
[433] | 49 | pid_t pid = process->pid; |
---|
[421] | 50 | |
---|
[438] | 51 | #if DEBUG_SYS_WAIT |
---|
[446] | 52 | uint64_t cycle = hal_get_cycles(); |
---|
| 53 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 54 | printk("\n[DBG] %s : thread %x in process %x enter / cycle %d\n", |
---|
| 55 | __FUNCTION__, this, process->pid, (uint32_t)cycle ); |
---|
[421] | 56 | #endif |
---|
| 57 | |
---|
| 58 | // check status in user space |
---|
[440] | 59 | error = vmm_get_vseg( process, (intptr_t)status , &vseg ); |
---|
[421] | 60 | |
---|
| 61 | if( error ) |
---|
| 62 | { |
---|
[435] | 63 | |
---|
[438] | 64 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 65 | printk("\n[ERROR] in %s : status buffer %x unmapped for thread %x in process %x\n", |
---|
| 66 | __FUNCTION__ , (intptr_t)status, this->trdid , process->pid ); |
---|
| 67 | vmm_display( process , false ); |
---|
[435] | 68 | #endif |
---|
[436] | 69 | this->errno = EINVAL; |
---|
[421] | 70 | return -1; |
---|
| 71 | } |
---|
| 72 | |
---|
[443] | 73 | // get calling process owner cluster |
---|
[436] | 74 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
[421] | 75 | |
---|
[443] | 76 | // get calling thread trdid |
---|
| 77 | trdid_t trdid = this->trdid; |
---|
| 78 | |
---|
| 79 | // this function must be executed by the process main thread |
---|
[436] | 80 | if( (owner_cxy != local_cxy) || (LTID_FROM_TRDID(trdid) != 0) ) |
---|
| 81 | { |
---|
[421] | 82 | |
---|
[446] | 83 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 84 | printk("\n[ERROR] in %s : calling thread %x is not thread 0 in owner cluster %x\n", |
---|
| 85 | __FUNCTION__ , trdid , owner_cxy ); |
---|
| 86 | #endif |
---|
| 87 | this->errno = EINVAL; |
---|
| 88 | return -1; |
---|
| 89 | } |
---|
| 90 | |
---|
[433] | 91 | // get extended pointer on children list root and lock |
---|
| 92 | xptr_t children_root_xp = XPTR( owner_cxy , &process->children_root ); |
---|
| 93 | xptr_t children_lock_xp = XPTR( owner_cxy , &process->children_lock ); |
---|
[421] | 94 | |
---|
| 95 | // exit this blocking loop only when a child processes change state |
---|
| 96 | while( 1 ) |
---|
| 97 | { |
---|
[435] | 98 | // enable IRQS |
---|
| 99 | hal_enable_irq( &save_sr ); |
---|
| 100 | |
---|
[433] | 101 | // get lock protecting children list |
---|
| 102 | remote_spinlock_lock( children_lock_xp ); |
---|
[421] | 103 | |
---|
[436] | 104 | // scan the list of child process |
---|
[433] | 105 | XLIST_FOREACH( children_root_xp , iter_xp ) |
---|
[421] | 106 | { |
---|
[433] | 107 | // get child process owner cluster and local pointer |
---|
[421] | 108 | child_xp = XLIST_ELEMENT( iter_xp , process_t , children_list ); |
---|
| 109 | child_ptr = GET_PTR( child_xp ); |
---|
| 110 | child_cxy = GET_CXY( child_xp ); |
---|
| 111 | |
---|
[446] | 112 | // get PID, term_state, and main thread from child process |
---|
| 113 | child_pid = hal_remote_lw (XPTR( child_cxy , &child_ptr->pid )); |
---|
| 114 | child_state = hal_remote_lw ( XPTR(child_cxy , &child_ptr->term_state ) ); |
---|
| 115 | child_thread = hal_remote_lpt(XPTR( child_cxy , &child_ptr->th_tbl[0] )); |
---|
[421] | 116 | |
---|
[446] | 117 | #if (DEBUG_SYS_WAIT &1) |
---|
| 118 | cycle = hal_get_cycles(); |
---|
| 119 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 120 | printk("\n[DBG] %s : thread %x in process %x check child %x / state %x\n", |
---|
| 121 | __FUNCTION__, this, process->pid, child_pid, child_state ); |
---|
| 122 | #endif |
---|
[443] | 123 | // test if this child process is terminated, |
---|
[433] | 124 | // but termination not yet reported to parent process |
---|
[435] | 125 | if( ((child_state & PROCESS_TERM_EXIT) || |
---|
| 126 | (child_state & PROCESS_TERM_KILL) || |
---|
| 127 | (child_state & PROCESS_TERM_STOP)) && |
---|
| 128 | ((child_state & PROCESS_TERM_WAIT) == 0) ) |
---|
[421] | 129 | { |
---|
[446] | 130 | // set the PROCESS_FLAG_WAIT in child process descriptor |
---|
[433] | 131 | hal_remote_atomic_or( XPTR( child_cxy , &child_ptr->term_state ), |
---|
[435] | 132 | PROCESS_TERM_WAIT ); |
---|
[433] | 133 | |
---|
[446] | 134 | // set the THREAD_FLAG_REQ_DELETE in main thread if kill or exit |
---|
| 135 | if((child_state & PROCESS_TERM_EXIT) || (child_state & PROCESS_TERM_KILL)) |
---|
[433] | 136 | hal_remote_atomic_or( XPTR( child_cxy , &child_thread->flags ) , |
---|
| 137 | THREAD_FLAG_REQ_DELETE ); |
---|
| 138 | |
---|
[435] | 139 | // release lock protecting children list |
---|
| 140 | remote_spinlock_unlock( children_lock_xp ); |
---|
| 141 | |
---|
[438] | 142 | #if DEBUG_SYS_WAIT |
---|
[446] | 143 | cycle = hal_get_cycles(); |
---|
| 144 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 145 | { |
---|
| 146 | if ( child_state & PROCESS_TERM_EXIT ) |
---|
| 147 | printk("\n[DBG] %s : thread %x in process %x exit / child %x exit / cycle %d\n", |
---|
| 148 | __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle ); |
---|
| 149 | if( child_state & PROCESS_TERM_KILL ) |
---|
| 150 | printk("\n[DBG] %s : thread %x in process %x exit / child %x killed / cycle %d\n", |
---|
| 151 | __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle ); |
---|
| 152 | if( child_state & PROCESS_TERM_STOP ) |
---|
| 153 | printk("\n[DBG] %s : thread %x in process %x exit / child %x stopped / cycle %d\n", |
---|
| 154 | __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle ); |
---|
| 155 | } |
---|
[421] | 156 | #endif |
---|
[436] | 157 | // return child termination state to parent process |
---|
[433] | 158 | hal_copy_to_uspace( status , &child_state , sizeof(int) ); |
---|
| 159 | return child_pid; |
---|
[421] | 160 | } |
---|
[436] | 161 | } // end loop on children |
---|
[421] | 162 | |
---|
[446] | 163 | // block on WAIT condition |
---|
| 164 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_WAIT ); |
---|
| 165 | |
---|
[435] | 166 | // release lock protecting children list |
---|
[433] | 167 | remote_spinlock_unlock( children_lock_xp ); |
---|
[421] | 168 | |
---|
[446] | 169 | #if (DEBUG_SYS_WAIT & 1) |
---|
| 170 | cycle = hal_get_cycles(); |
---|
| 171 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 172 | printk("\n[DBG] %s : thread %x in process %x block & deschedule / cycle %d\n", |
---|
| 173 | __FUNCTION__, this, process->pid, (uint32_t)cycle ); |
---|
| 174 | #endif |
---|
[421] | 175 | |
---|
[446] | 176 | // deschedule |
---|
| 177 | sched_yield( "parent process wait children processes termination" ); |
---|
| 178 | |
---|
| 179 | #if (DEBUG_SYS_WAIT & 1) |
---|
| 180 | cycle = hal_get_cycles(); |
---|
| 181 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 182 | printk("\n[DBG] %s : thread %x in process %x unblock & resume / cycle %d\n", |
---|
| 183 | __FUNCTION__, this, process->pid, (uint32_t)cycle ); |
---|
| 184 | #endif |
---|
| 185 | |
---|
[435] | 186 | } // end while |
---|
| 187 | |
---|
[421] | 188 | // never executed |
---|
| 189 | return -1; |
---|
| 190 | |
---|
| 191 | } // end sys_wait() |
---|