[421] | 1 | /* |
---|
| 2 | * sys_wait.c - wait termination or blocking of a child process. |
---|
| 3 | * |
---|
| 4 | * Author Alain Greiner (2016,2017) |
---|
| 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 | |
---|
| 24 | #include <hal_types.h> |
---|
| 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; |
---|
| 37 | paddr_t paddr; |
---|
| 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; |
---|
| 43 | int 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 | |
---|
[433] | 51 | #if CONFIG_DEBUG_SYS_WAIT |
---|
[421] | 52 | uint64_t tm_start; |
---|
| 53 | uint64_t tm_end; |
---|
| 54 | tm_start = hal_get_cycles(); |
---|
[433] | 55 | if( CONFIG_DEBUG_SYS_WAIT < tm_start ) |
---|
| 56 | printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n", |
---|
| 57 | __FUNCTION__, this, process->pid, (uint32_t)tm_start ); |
---|
[421] | 58 | #endif |
---|
| 59 | |
---|
| 60 | // check status in user space |
---|
| 61 | error = vmm_v2p_translate( false , status , &paddr ); |
---|
| 62 | |
---|
| 63 | if( error ) |
---|
| 64 | { |
---|
[435] | 65 | |
---|
| 66 | #if CONFIG_DEBUG_SYSCALLS_ERROR |
---|
| 67 | printk("\n[ERROR] in %s : status buffer unmapped for thread %x in process %x\n", |
---|
| 68 | __FUNCTION__ , this->trdid , process->pid ); |
---|
| 69 | #endif |
---|
[421] | 70 | this->errno = EFAULT; |
---|
| 71 | return -1; |
---|
| 72 | } |
---|
| 73 | |
---|
[433] | 74 | // get process owner cluster |
---|
| 75 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
[421] | 76 | |
---|
[433] | 77 | // This function must be executed in owner cluster |
---|
| 78 | assert( (owner_cxy == local_cxy) , __FUNCTION__ , |
---|
| 79 | "calling thread must execute in owner cluster" ); |
---|
[421] | 80 | |
---|
[433] | 81 | // This function must be executed by the main thread |
---|
| 82 | assert( (process->th_tbl[0] == this) , __FUNCTION__ , |
---|
| 83 | "this function must be executed by the main thread" ); |
---|
| 84 | |
---|
| 85 | // get extended pointer on children list root and lock |
---|
| 86 | xptr_t children_root_xp = XPTR( owner_cxy , &process->children_root ); |
---|
| 87 | xptr_t children_lock_xp = XPTR( owner_cxy , &process->children_lock ); |
---|
[421] | 88 | |
---|
| 89 | // exit this blocking loop only when a child processes change state |
---|
| 90 | while( 1 ) |
---|
| 91 | { |
---|
[435] | 92 | // enable IRQS |
---|
| 93 | hal_enable_irq( &save_sr ); |
---|
| 94 | |
---|
[433] | 95 | // get lock protecting children list |
---|
| 96 | remote_spinlock_lock( children_lock_xp ); |
---|
[421] | 97 | |
---|
[433] | 98 | // scan the list of owner child process |
---|
| 99 | XLIST_FOREACH( children_root_xp , iter_xp ) |
---|
[421] | 100 | { |
---|
[433] | 101 | // get child process owner cluster and local pointer |
---|
[421] | 102 | child_xp = XLIST_ELEMENT( iter_xp , process_t , children_list ); |
---|
| 103 | child_ptr = GET_PTR( child_xp ); |
---|
| 104 | child_cxy = GET_CXY( child_xp ); |
---|
| 105 | |
---|
[433] | 106 | // get term_state from child owner process |
---|
| 107 | child_state = (int)hal_remote_lw ( XPTR(child_cxy,&child_ptr->term_state)); |
---|
[421] | 108 | |
---|
[433] | 109 | // test if child process is terminated, |
---|
| 110 | // but termination not yet reported to parent process |
---|
[435] | 111 | if( ((child_state & PROCESS_TERM_EXIT) || |
---|
| 112 | (child_state & PROCESS_TERM_KILL) || |
---|
| 113 | (child_state & PROCESS_TERM_STOP)) && |
---|
| 114 | ((child_state & PROCESS_TERM_WAIT) == 0) ) |
---|
[421] | 115 | { |
---|
[433] | 116 | // get pointer on main thread and PID from child owner process |
---|
| 117 | child_pid = (pid_t) hal_remote_lw ( XPTR(child_cxy,&child_ptr->pid)); |
---|
| 118 | child_thread = (thread_t *)hal_remote_lpt( XPTR(child_cxy,&child_ptr->th_tbl[0])); |
---|
[421] | 119 | |
---|
[433] | 120 | // set the PROCESS_FLAG_WAIT in owner child descriptor |
---|
| 121 | hal_remote_atomic_or( XPTR( child_cxy , &child_ptr->term_state ), |
---|
[435] | 122 | PROCESS_TERM_WAIT ); |
---|
[433] | 123 | |
---|
| 124 | // set the THREAD_FLAG_REQ_DELETE in child main thread |
---|
| 125 | hal_remote_atomic_or( XPTR( child_cxy , &child_thread->flags ) , |
---|
| 126 | THREAD_FLAG_REQ_DELETE ); |
---|
| 127 | |
---|
[435] | 128 | // release lock protecting children list |
---|
| 129 | remote_spinlock_unlock( children_lock_xp ); |
---|
| 130 | |
---|
[433] | 131 | #if CONFIG_DEBUG_SYS_WAIT |
---|
[421] | 132 | tm_end = hal_get_cycles(); |
---|
[433] | 133 | if( CONFIG_DEBUG_SYS_WAIT < tm_end ) |
---|
[435] | 134 | printk("\n[DBG] %s : thread %x exit / parent %x / child %x / cycle %d\n", |
---|
| 135 | __FUNCTION__, this, process->pid, child_pid, (uint32_t)tm_end ); |
---|
[421] | 136 | #endif |
---|
[433] | 137 | // return relevant info to calling parent process |
---|
| 138 | hal_copy_to_uspace( status , &child_state , sizeof(int) ); |
---|
| 139 | return child_pid; |
---|
[421] | 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
[435] | 143 | // release lock protecting children list |
---|
[433] | 144 | remote_spinlock_unlock( children_lock_xp ); |
---|
[421] | 145 | |
---|
[433] | 146 | // deschedule without blocking |
---|
| 147 | sched_yield( "parent wait children termination" ); |
---|
[421] | 148 | |
---|
[435] | 149 | } // end while |
---|
| 150 | |
---|
[421] | 151 | // never executed |
---|
| 152 | return -1; |
---|
| 153 | |
---|
| 154 | } // end sys_wait() |
---|