[410] | 1 | /* |
---|
[416] | 2 | * sys_exit.c - Kernel function implementing the "exit" system call. |
---|
[410] | 3 | * |
---|
[625] | 4 | * Author Alain Greiner (2016,2017,2018,2019) |
---|
[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> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[410] | 26 | #include <hal_irqmask.h> |
---|
| 27 | #include <errno.h> |
---|
| 28 | #include <thread.h> |
---|
| 29 | #include <printk.h> |
---|
| 30 | #include <process.h> |
---|
[670] | 31 | #include <dev_fbf.h> |
---|
[435] | 32 | #include <shared_syscalls.h> |
---|
[410] | 33 | #include <cluster.h> |
---|
| 34 | #include <rpc.h> |
---|
| 35 | |
---|
[506] | 36 | #include <syscalls.h> |
---|
| 37 | |
---|
[410] | 38 | /////////////////////////////// |
---|
| 39 | int sys_exit( uint32_t status ) |
---|
| 40 | { |
---|
[446] | 41 | xptr_t owner_xp; // extended pointer on owner process |
---|
| 42 | cxy_t owner_cxy; // owner process cluster |
---|
| 43 | process_t * owner_ptr; // local pointer on owner process |
---|
| 44 | thread_t * main_ptr; // local pointer on process main thread |
---|
| 45 | xptr_t parent_xp; // extended pointer on parent process |
---|
| 46 | cxy_t parent_cxy; // parent process cluster |
---|
| 47 | process_t * parent_ptr; // local pointer on parent process |
---|
| 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 | |
---|
[625] | 56 | #if (DEBUG_SYS_EXIT || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 57 | uint64_t tm_start = hal_get_cycles(); |
---|
| 58 | #endif |
---|
| 59 | |
---|
[438] | 60 | #if DEBUG_SYS_EXIT |
---|
| 61 | if( DEBUG_SYS_EXIT < tm_start ) |
---|
[619] | 62 | printk("\n[%s] thread[%x,%x] enter / status %x / cycle %d\n", |
---|
[625] | 63 | __FUNCTION__, pid, this->trdid , status , (uint32_t)tm_start ); |
---|
[410] | 64 | #endif |
---|
| 65 | |
---|
[443] | 66 | // get owner process descriptor pointers |
---|
[446] | 67 | owner_xp = process->owner_xp; |
---|
| 68 | owner_cxy = GET_CXY( owner_xp ); |
---|
| 69 | owner_ptr = GET_PTR( owner_xp ); |
---|
[410] | 70 | |
---|
[566] | 71 | // get local pointer on the main thread |
---|
[446] | 72 | main_ptr = hal_remote_lpt( XPTR( owner_cxy , &owner_ptr->th_tbl[0] ) ); |
---|
[410] | 73 | |
---|
[446] | 74 | // get parent process descriptor pointers |
---|
| 75 | parent_xp = process->parent_xp; |
---|
| 76 | parent_cxy = GET_CXY( parent_xp ); |
---|
| 77 | parent_ptr = GET_PTR( parent_xp ); |
---|
| 78 | |
---|
| 79 | // get pointers on the parent process main thread |
---|
| 80 | parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->th_tbl[0] ) ); |
---|
| 81 | parent_main_xp = XPTR( parent_cxy , parent_main_ptr ); |
---|
| 82 | |
---|
| 83 | // remove process from TXT list |
---|
| 84 | process_txt_detach( owner_xp ); |
---|
| 85 | |
---|
| 86 | #if( DEBUG_SYS_EXIT & 1) |
---|
[457] | 87 | if( DEBUG_SYS_EXIT < tm_start ) |
---|
[625] | 88 | printk("\n[%s] thread[%x,%x] detached process %x from TXT\n", |
---|
| 89 | __FUNCTION__, pid, this->trdid, pid ); |
---|
[446] | 90 | #endif |
---|
| 91 | |
---|
[664] | 92 | // close all open files |
---|
| 93 | process_fd_clean_all( owner_xp ); |
---|
| 94 | |
---|
| 95 | #if( DEBUG_SYS_EXIT & 1) |
---|
| 96 | if( DEBUG_SYS_EXIT < tm_start ) |
---|
| 97 | printk("\n[%s] thread[%x,%x] closed all files for process %x\n", |
---|
| 98 | __FUNCTION__, pid, this->trdid, pid ); |
---|
| 99 | #endif |
---|
| 100 | |
---|
[670] | 101 | // delete all process registered FBF windows |
---|
| 102 | dev_fbf_cleanup( pid ); |
---|
| 103 | |
---|
| 104 | #if( DEBUG_SYS_EXIT & 1) |
---|
| 105 | if( DEBUG_SYS_EXIT < tm_start ) |
---|
| 106 | printk("\n[%s] thread[%x,%x] deleted all FBF windows for process %x\n", |
---|
| 107 | __FUNCTION__, pid, this->trdid, pid ); |
---|
| 108 | #endif |
---|
| 109 | |
---|
[443] | 110 | // mark for delete all process threads in all clusters, |
---|
| 111 | // but the main thread and this calling thread |
---|
[625] | 112 | process_sigaction( pid , DELETE_ALL_THREADS ); |
---|
[440] | 113 | |
---|
[438] | 114 | #if( DEBUG_SYS_EXIT & 1) |
---|
[457] | 115 | if( DEBUG_SYS_EXIT < tm_start ) |
---|
[625] | 116 | printk("\n[%s] thread[%x,%x] deleted all threads in process %x (but itself)\n", |
---|
| 117 | __FUNCTION__, pid, this->trdid, pid ); |
---|
[436] | 118 | #endif |
---|
[435] | 119 | |
---|
[566] | 120 | // mark for delete the calling thread when it is not the main |
---|
[446] | 121 | if( (owner_cxy != local_cxy) || (main_ptr != this) ) |
---|
[440] | 122 | { |
---|
[435] | 123 | |
---|
[438] | 124 | #if( DEBUG_SYS_EXIT & 1) |
---|
[440] | 125 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
[625] | 126 | printk("\n[%s] thread[%x,%x] marked iself for delete\n", |
---|
| 127 | __FUNCTION__, pid, this->trdid ); |
---|
[436] | 128 | #endif |
---|
[670] | 129 | thread_delete_request( XPTR( local_cxy , this ) , true ); // forced |
---|
[440] | 130 | } |
---|
[445] | 131 | |
---|
[625] | 132 | // block the main thread |
---|
[446] | 133 | thread_block( XPTR( owner_cxy , main_ptr ) , THREAD_BLOCKED_GLOBAL ); |
---|
[436] | 134 | |
---|
[438] | 135 | #if( DEBUG_SYS_EXIT & 1) |
---|
[625] | 136 | trdid_t main_trdid = hal_remote_l32( XPTR( owner_cxy , &main_ptr->trdid ) ); |
---|
[440] | 137 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
[625] | 138 | printk("\n[%s] thread[%x,%x] blocked main thread[%x,%x]\n", |
---|
| 139 | __FUNCTION__, pid, this->trdid, pid, main_trdid ); |
---|
[436] | 140 | #endif |
---|
| 141 | |
---|
[625] | 142 | // update term_state in owner process descriptor to ask |
---|
| 143 | // the parent process sys_wait() function to delete the process |
---|
[446] | 144 | term_state = (status & 0xFF) | PROCESS_TERM_EXIT; |
---|
| 145 | hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , term_state ); |
---|
[410] | 146 | |
---|
[438] | 147 | #if( DEBUG_SYS_EXIT & 1) |
---|
[440] | 148 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
[619] | 149 | printk("\n[%s] thread[%x,%x] set exit status %x in owner process\n", |
---|
[625] | 150 | __FUNCTION__, pid, this->trdid, term_state ); |
---|
[436] | 151 | #endif |
---|
| 152 | |
---|
[457] | 153 | // unblock the parent process main thread |
---|
[446] | 154 | thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT ); |
---|
[436] | 155 | |
---|
[438] | 156 | #if( DEBUG_SYS_EXIT & 1) |
---|
[440] | 157 | if( tm_start > DEBUG_SYS_EXIT ) |
---|
[619] | 158 | printk("\n[%s] thread[%x,%x] unblocked parent main thread in process %x\n", |
---|
[625] | 159 | __FUNCTION__ , pid, this->trdid, |
---|
[566] | 160 | hal_remote_l32( XPTR( parent_cxy , &parent_ptr->pid) ) ); |
---|
[436] | 161 | #endif |
---|
| 162 | |
---|
[410] | 163 | hal_fence(); |
---|
| 164 | |
---|
[625] | 165 | #if (DEBUG_SYS_EXIT || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 166 | uint64_t tm_end = hal_get_cycles(); |
---|
| 167 | #endif |
---|
| 168 | |
---|
[438] | 169 | #if DEBUG_SYS_EXIT |
---|
| 170 | if( DEBUG_SYS_EXIT < tm_end ) |
---|
[625] | 171 | printk("\n[%s] thread[%x,%x] exit / term_state %x / cycle %d\n", |
---|
| 172 | __FUNCTION__, pid, this->trdid, term_state, (uint32_t)tm_end ); |
---|
[410] | 173 | #endif |
---|
| 174 | |
---|
[625] | 175 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 176 | hal_atomic_add( &syscalls_cumul_cost[SYS_EXIT] , tm_end - tm_start ); |
---|
| 177 | hal_atomic_add( &syscalls_occurences[SYS_EXIT] , 1 ); |
---|
| 178 | #endif |
---|
| 179 | |
---|
[440] | 180 | // this thread deschedule |
---|
[436] | 181 | sched_yield( "process exit" ); |
---|
| 182 | |
---|
[410] | 183 | return 0; |
---|
| 184 | |
---|
| 185 | } // end sys_exit() |
---|
| 186 | |
---|