[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 | |
---|
[506] | 33 | #include <syscalls.h> |
---|
| 34 | |
---|
[421] | 35 | ///////////////////////////////// |
---|
| 36 | int sys_wait( uint32_t * status ) |
---|
| 37 | { |
---|
| 38 | error_t error; |
---|
[440] | 39 | vseg_t * vseg; |
---|
[421] | 40 | xptr_t iter_xp; |
---|
| 41 | xptr_t child_xp; |
---|
| 42 | process_t * child_ptr; |
---|
| 43 | cxy_t child_cxy; |
---|
| 44 | pid_t child_pid; |
---|
[446] | 45 | uint32_t child_state; |
---|
[433] | 46 | thread_t * child_thread; |
---|
[435] | 47 | reg_t save_sr; |
---|
[421] | 48 | |
---|
| 49 | thread_t * this = CURRENT_THREAD; |
---|
| 50 | process_t * process = this->process; |
---|
[433] | 51 | pid_t pid = process->pid; |
---|
[421] | 52 | |
---|
[438] | 53 | #if DEBUG_SYS_WAIT |
---|
[446] | 54 | uint64_t cycle = hal_get_cycles(); |
---|
| 55 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 56 | printk("\n[DBG] %s : thread %x in process %x enter / cycle %d\n", |
---|
| 57 | __FUNCTION__, this, process->pid, (uint32_t)cycle ); |
---|
[421] | 58 | #endif |
---|
| 59 | |
---|
| 60 | // check status in user space |
---|
[440] | 61 | error = vmm_get_vseg( process, (intptr_t)status , &vseg ); |
---|
[421] | 62 | |
---|
| 63 | if( error ) |
---|
| 64 | { |
---|
[435] | 65 | |
---|
[438] | 66 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 67 | printk("\n[ERROR] in %s : status buffer %x unmapped for thread %x in process %x\n", |
---|
| 68 | __FUNCTION__ , (intptr_t)status, this->trdid , process->pid ); |
---|
| 69 | vmm_display( process , false ); |
---|
[435] | 70 | #endif |
---|
[436] | 71 | this->errno = EINVAL; |
---|
[421] | 72 | return -1; |
---|
| 73 | } |
---|
| 74 | |
---|
[443] | 75 | // get calling process owner cluster |
---|
[436] | 76 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
[421] | 77 | |
---|
[443] | 78 | // get calling thread trdid |
---|
| 79 | trdid_t trdid = this->trdid; |
---|
| 80 | |
---|
| 81 | // this function must be executed by the process main thread |
---|
[436] | 82 | if( (owner_cxy != local_cxy) || (LTID_FROM_TRDID(trdid) != 0) ) |
---|
| 83 | { |
---|
[421] | 84 | |
---|
[446] | 85 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 86 | printk("\n[ERROR] in %s : calling thread %x is not thread 0 in owner cluster %x\n", |
---|
| 87 | __FUNCTION__ , trdid , owner_cxy ); |
---|
| 88 | #endif |
---|
| 89 | this->errno = EINVAL; |
---|
| 90 | return -1; |
---|
| 91 | } |
---|
| 92 | |
---|
[433] | 93 | // get extended pointer on children list root and lock |
---|
| 94 | xptr_t children_root_xp = XPTR( owner_cxy , &process->children_root ); |
---|
| 95 | xptr_t children_lock_xp = XPTR( owner_cxy , &process->children_lock ); |
---|
[421] | 96 | |
---|
| 97 | // exit this blocking loop only when a child processes change state |
---|
| 98 | while( 1 ) |
---|
| 99 | { |
---|
[435] | 100 | // enable IRQS |
---|
| 101 | hal_enable_irq( &save_sr ); |
---|
| 102 | |
---|
[433] | 103 | // get lock protecting children list |
---|
| 104 | remote_spinlock_lock( children_lock_xp ); |
---|
[421] | 105 | |
---|
[436] | 106 | // scan the list of child process |
---|
[433] | 107 | XLIST_FOREACH( children_root_xp , iter_xp ) |
---|
[421] | 108 | { |
---|
[433] | 109 | // get child process owner cluster and local pointer |
---|
[421] | 110 | child_xp = XLIST_ELEMENT( iter_xp , process_t , children_list ); |
---|
| 111 | child_ptr = GET_PTR( child_xp ); |
---|
| 112 | child_cxy = GET_CXY( child_xp ); |
---|
| 113 | |
---|
[446] | 114 | // get PID, term_state, and main thread from child process |
---|
| 115 | child_pid = hal_remote_lw (XPTR( child_cxy , &child_ptr->pid )); |
---|
| 116 | child_state = hal_remote_lw ( XPTR(child_cxy , &child_ptr->term_state ) ); |
---|
| 117 | child_thread = hal_remote_lpt(XPTR( child_cxy , &child_ptr->th_tbl[0] )); |
---|
[421] | 118 | |
---|
[446] | 119 | #if (DEBUG_SYS_WAIT &1) |
---|
| 120 | cycle = hal_get_cycles(); |
---|
| 121 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 122 | printk("\n[DBG] %s : thread %x in process %x check child %x / state %x\n", |
---|
| 123 | __FUNCTION__, this, process->pid, child_pid, child_state ); |
---|
| 124 | #endif |
---|
[443] | 125 | // test if this child process is terminated, |
---|
[433] | 126 | // but termination not yet reported to parent process |
---|
[435] | 127 | if( ((child_state & PROCESS_TERM_EXIT) || |
---|
| 128 | (child_state & PROCESS_TERM_KILL) || |
---|
| 129 | (child_state & PROCESS_TERM_STOP)) && |
---|
| 130 | ((child_state & PROCESS_TERM_WAIT) == 0) ) |
---|
[421] | 131 | { |
---|
[446] | 132 | // set the PROCESS_FLAG_WAIT in child process descriptor |
---|
[433] | 133 | hal_remote_atomic_or( XPTR( child_cxy , &child_ptr->term_state ), |
---|
[435] | 134 | PROCESS_TERM_WAIT ); |
---|
[433] | 135 | |
---|
[446] | 136 | // set the THREAD_FLAG_REQ_DELETE in main thread if kill or exit |
---|
| 137 | if((child_state & PROCESS_TERM_EXIT) || (child_state & PROCESS_TERM_KILL)) |
---|
[433] | 138 | hal_remote_atomic_or( XPTR( child_cxy , &child_thread->flags ) , |
---|
| 139 | THREAD_FLAG_REQ_DELETE ); |
---|
| 140 | |
---|
[435] | 141 | // release lock protecting children list |
---|
| 142 | remote_spinlock_unlock( children_lock_xp ); |
---|
| 143 | |
---|
[438] | 144 | #if DEBUG_SYS_WAIT |
---|
[446] | 145 | cycle = hal_get_cycles(); |
---|
| 146 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 147 | { |
---|
| 148 | if ( child_state & PROCESS_TERM_EXIT ) |
---|
| 149 | printk("\n[DBG] %s : thread %x in process %x exit / child %x exit / cycle %d\n", |
---|
| 150 | __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle ); |
---|
| 151 | if( child_state & PROCESS_TERM_KILL ) |
---|
| 152 | printk("\n[DBG] %s : thread %x in process %x exit / child %x killed / cycle %d\n", |
---|
| 153 | __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle ); |
---|
| 154 | if( child_state & PROCESS_TERM_STOP ) |
---|
| 155 | printk("\n[DBG] %s : thread %x in process %x exit / child %x stopped / cycle %d\n", |
---|
| 156 | __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle ); |
---|
| 157 | } |
---|
[421] | 158 | #endif |
---|
[436] | 159 | // return child termination state to parent process |
---|
[433] | 160 | hal_copy_to_uspace( status , &child_state , sizeof(int) ); |
---|
| 161 | return child_pid; |
---|
[421] | 162 | } |
---|
[436] | 163 | } // end loop on children |
---|
[421] | 164 | |
---|
[446] | 165 | // block on WAIT condition |
---|
| 166 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_WAIT ); |
---|
| 167 | |
---|
[435] | 168 | // release lock protecting children list |
---|
[433] | 169 | remote_spinlock_unlock( children_lock_xp ); |
---|
[421] | 170 | |
---|
[446] | 171 | #if (DEBUG_SYS_WAIT & 1) |
---|
| 172 | cycle = hal_get_cycles(); |
---|
| 173 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 174 | printk("\n[DBG] %s : thread %x in process %x block & deschedule / cycle %d\n", |
---|
| 175 | __FUNCTION__, this, process->pid, (uint32_t)cycle ); |
---|
| 176 | #endif |
---|
[421] | 177 | |
---|
[446] | 178 | // deschedule |
---|
| 179 | sched_yield( "parent process wait children processes termination" ); |
---|
| 180 | |
---|
| 181 | #if (DEBUG_SYS_WAIT & 1) |
---|
| 182 | cycle = hal_get_cycles(); |
---|
| 183 | if( DEBUG_SYS_WAIT < cycle ) |
---|
| 184 | printk("\n[DBG] %s : thread %x in process %x unblock & resume / cycle %d\n", |
---|
| 185 | __FUNCTION__, this, process->pid, (uint32_t)cycle ); |
---|
| 186 | #endif |
---|
| 187 | |
---|
[435] | 188 | } // end while |
---|
| 189 | |
---|
[421] | 190 | // never executed |
---|
| 191 | return -1; |
---|
| 192 | |
---|
| 193 | } // end sys_wait() |
---|