[410] | 1 | /* |
---|
[416] | 2 | * sys_exit.c - Kernel function implementing the "exit" system call. |
---|
[410] | 3 | * |
---|
[440] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[410] | 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 <kernel_config.h> |
---|
| 25 | #include <hal_types.h> |
---|
| 26 | #include <hal_irqmask.h> |
---|
| 27 | #include <errno.h> |
---|
| 28 | #include <thread.h> |
---|
| 29 | #include <printk.h> |
---|
| 30 | #include <process.h> |
---|
[435] | 31 | #include <shared_syscalls.h> |
---|
[410] | 32 | #include <cluster.h> |
---|
| 33 | #include <rpc.h> |
---|
| 34 | |
---|
| 35 | /////////////////////////////// |
---|
| 36 | int sys_exit( uint32_t status ) |
---|
| 37 | { |
---|
[446] | 38 | reg_t save_sr; // required to enable IRQs |
---|
[410] | 39 | |
---|
[446] | 40 | xptr_t owner_xp; // extended pointer on owner process |
---|
| 41 | cxy_t owner_cxy; // owner process cluster |
---|
| 42 | process_t * owner_ptr; // local pointer on owner process |
---|
| 43 | thread_t * main_ptr; // local pointer on process main thread |
---|
| 44 | xptr_t parent_xp; // extended pointer on parent process |
---|
| 45 | cxy_t parent_cxy; // parent process cluster |
---|
| 46 | process_t * parent_ptr; // local pointer on parent process |
---|
| 47 | xptr_t children_lock_xp; // extended pointer on children locki |
---|
| 48 | thread_t * parent_main_ptr; // local pointer on parent main thread |
---|
| 49 | xptr_t parent_main_xp; // extended pointer on parent main thread |
---|
| 50 | uint32_t term_state; // termination status for owner process |
---|
| 51 | |
---|
[433] | 52 | thread_t * this = CURRENT_THREAD; |
---|
| 53 | process_t * process = this->process; |
---|
| 54 | pid_t pid = process->pid; |
---|
[416] | 55 | |
---|
[438] | 56 | #if DEBUG_SYS_EXIT |
---|
[410] | 57 | uint64_t tm_start; |
---|
| 58 | uint64_t tm_end; |
---|
| 59 | tm_start = hal_get_cycles(); |
---|
[438] | 60 | if( DEBUG_SYS_EXIT < tm_start ) |
---|
[433] | 61 | printk("\n[DBG] %s : thread %x enter / process %x / status %x / cycle %d\n", |
---|
| 62 | __FUNCTION__ , this, pid , status , (uint32_t)tm_start ); |
---|
[410] | 63 | #endif |
---|
| 64 | |
---|
[443] | 65 | // get owner process descriptor pointers |
---|
[446] | 66 | owner_xp = process->owner_xp; |
---|
| 67 | owner_cxy = GET_CXY( owner_xp ); |
---|
| 68 | owner_ptr = GET_PTR( owner_xp ); |
---|
[410] | 69 | |
---|
[446] | 70 | #if (DEBUG_SYS_EXIT & 1) |
---|
| 71 | if( DEBUG_SYS_EXIT < tm_start ) |
---|
| 72 | printk("\n[DBG] %s : thread %x get owner process %x in cluster %x\n", |
---|
| 73 | __FUNCTION__ , this, owner_ptr, owner_cxy ); |
---|
| 74 | #endif |
---|
[433] | 75 | |
---|
[446] | 76 | // get pointer on the process main thread |
---|
| 77 | main_ptr = hal_remote_lpt( XPTR( owner_cxy , &owner_ptr->th_tbl[0] ) ); |
---|
[410] | 78 | |
---|
[446] | 79 | // get parent process descriptor pointers |
---|
| 80 | parent_xp = process->parent_xp; |
---|
| 81 | parent_cxy = GET_CXY( parent_xp ); |
---|
| 82 | parent_ptr = GET_PTR( parent_xp ); |
---|
| 83 | |
---|
| 84 | #if (DEBUG_SYS_EXIT & 1) |
---|
| 85 | if( DEBUG_SYS_EXIT < tm_start ) |
---|
| 86 | printk("\n[DBG] %s : thread %x get parent process %x in cluster %x\n", |
---|
| 87 | __FUNCTION__ , this, parent_ptr, parent_cxy ); |
---|
| 88 | #endif |
---|
| 89 | |
---|
| 90 | // get extended pointer on lock protecting children list in parent process |
---|
| 91 | children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock ); |
---|
| 92 | |
---|
| 93 | // get pointers on the parent process main thread |
---|
| 94 | parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->th_tbl[0] ) ); |
---|
| 95 | parent_main_xp = XPTR( parent_cxy , parent_main_ptr ); |
---|
| 96 | |
---|
| 97 | // remove process from TXT list |
---|
| 98 | process_txt_detach( owner_xp ); |
---|
| 99 | |
---|
| 100 | #if( DEBUG_SYS_EXIT & 1) |
---|
| 101 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
| 102 | printk("\n[DBG] %s : thread %x removed process %x from TXT list\n", |
---|
| 103 | __FUNCTION__ , this, pid ); |
---|
| 104 | #endif |
---|
| 105 | |
---|
[443] | 106 | // mark for delete all process threads in all clusters, |
---|
| 107 | // but the main thread and this calling thread |
---|
[446] | 108 | hal_enable_irq( &save_sr ); |
---|
[440] | 109 | process_sigaction( pid , DELETE_ALL_THREADS ); |
---|
| 110 | hal_restore_irq( save_sr ); |
---|
| 111 | |
---|
[438] | 112 | #if( DEBUG_SYS_EXIT & 1) |
---|
[440] | 113 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
[446] | 114 | printk("\n[DBG] %s : thread %x deleted threads for process %x\n", |
---|
[440] | 115 | __FUNCTION__ , this, pid ); |
---|
[436] | 116 | #endif |
---|
[435] | 117 | |
---|
[440] | 118 | // mark for delete this calling thread when it is not the main |
---|
[446] | 119 | if( (owner_cxy != local_cxy) || (main_ptr != this) ) |
---|
[440] | 120 | { |
---|
[435] | 121 | |
---|
[438] | 122 | #if( DEBUG_SYS_EXIT & 1) |
---|
[440] | 123 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
[446] | 124 | printk("\n[DBG] %s : calling thread %x marked iself for delete in process %x\n", |
---|
[440] | 125 | __FUNCTION__ , this, pid ); |
---|
[436] | 126 | #endif |
---|
[440] | 127 | thread_delete( XPTR( local_cxy , this ) , pid , true ); |
---|
| 128 | } |
---|
[445] | 129 | |
---|
[446] | 130 | // block this main thread |
---|
| 131 | thread_block( XPTR( owner_cxy , main_ptr ) , THREAD_BLOCKED_GLOBAL ); |
---|
[436] | 132 | |
---|
[438] | 133 | #if( DEBUG_SYS_EXIT & 1) |
---|
[440] | 134 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
[446] | 135 | printk("\n[DBG] %s : thread %x blocked main thread for process %x\n", |
---|
| 136 | __FUNCTION__, this , pid ); |
---|
[436] | 137 | #endif |
---|
| 138 | |
---|
[446] | 139 | // atomically update owner process descriptor term_state to ask |
---|
| 140 | // the parent process sys_wait() function to delete the main thread |
---|
| 141 | term_state = (status & 0xFF) | PROCESS_TERM_EXIT; |
---|
| 142 | hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , term_state ); |
---|
[410] | 143 | |
---|
[438] | 144 | #if( DEBUG_SYS_EXIT & 1) |
---|
[440] | 145 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
[446] | 146 | printk("\n[DBG] %s : thread %x set exit status in process %x / term_state %x\n", |
---|
| 147 | __FUNCTION__ , this, pid, term_state ); |
---|
[436] | 148 | #endif |
---|
| 149 | |
---|
[446] | 150 | // take the children lock and unblock the parent process main thread |
---|
| 151 | remote_spinlock_lock( children_lock_xp ); |
---|
| 152 | thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT ); |
---|
| 153 | remote_spinlock_unlock( children_lock_xp ); |
---|
[436] | 154 | |
---|
[438] | 155 | #if( DEBUG_SYS_EXIT & 1) |
---|
[440] | 156 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
[446] | 157 | printk("\n[DBG] %s : thread %x in cluster %x unblock parent main thread %x in cluster %x\n", |
---|
| 158 | __FUNCTION__ , this, local_cxy, parent_main_ptr, parent_cxy ); |
---|
[436] | 159 | #endif |
---|
| 160 | |
---|
[410] | 161 | hal_fence(); |
---|
| 162 | |
---|
[438] | 163 | #if DEBUG_SYS_EXIT |
---|
[410] | 164 | tm_end = hal_get_cycles(); |
---|
[438] | 165 | if( DEBUG_SYS_EXIT < tm_end ) |
---|
[433] | 166 | printk("\n[DBG] %s : thread %x exit / process %x / status %x / cost = %d / cycle %d\n", |
---|
| 167 | __FUNCTION__, this, pid, status, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
[410] | 168 | #endif |
---|
| 169 | |
---|
[440] | 170 | // this thread deschedule |
---|
[436] | 171 | sched_yield( "process exit" ); |
---|
| 172 | |
---|
[410] | 173 | return 0; |
---|
| 174 | |
---|
| 175 | } // end sys_exit() |
---|
| 176 | |
---|