| 1 | /* | 
|---|
| 2 |  * sys_thread_exit.c - terminates the execution of calling thread | 
|---|
| 3 |  *  | 
|---|
| 4 |  * Authors   Alain Greiner (2016,2017,2018,2019) | 
|---|
| 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_kernel_types.h> | 
|---|
| 25 | #include <hal_irqmask.h> | 
|---|
| 26 | #include <thread.h> | 
|---|
| 27 | #include <process.h> | 
|---|
| 28 | #include <core.h> | 
|---|
| 29 | #include <vmm.h> | 
|---|
| 30 | #include <scheduler.h> | 
|---|
| 31 | #include <printk.h> | 
|---|
| 32 |  | 
|---|
| 33 | #include <syscalls.h> | 
|---|
| 34 |  | 
|---|
| 35 | ///////////////////////////////////////// | 
|---|
| 36 | int sys_thread_exit( void * exit_status ) | 
|---|
| 37 | { | 
|---|
| 38 |     error_t     error; | 
|---|
| 39 |     vseg_t    * vseg; | 
|---|
| 40 |  | 
|---|
| 41 |         thread_t  * this      = CURRENT_THREAD; | 
|---|
| 42 |     trdid_t     trdid     = this->trdid; | 
|---|
| 43 |     process_t * process   = this->process; | 
|---|
| 44 |     pid_t       pid       = process->pid; | 
|---|
| 45 |      | 
|---|
| 46 |     // check exit_value pointer in user space if required | 
|---|
| 47 |     if( exit_status != NULL ) | 
|---|
| 48 |     { | 
|---|
| 49 |         error = vmm_get_vseg( process , (intptr_t)exit_status  , &vseg ); | 
|---|
| 50 |  | 
|---|
| 51 |         if( error ) | 
|---|
| 52 |             { | 
|---|
| 53 |  | 
|---|
| 54 | #if DEBUG_SYSCALLS_ERROR | 
|---|
| 55 | printk("\n[ERROR] in %s : exit_status buffer %x unmapped / thread[%x,%x]\n", | 
|---|
| 56 | __FUNCTION__, (intptr_t)exit_status, process->pid, this->trdid ); | 
|---|
| 57 | #endif | 
|---|
| 58 |             this->errno = EINVAL; | 
|---|
| 59 |                 return -1; | 
|---|
| 60 |         } | 
|---|
| 61 |     } | 
|---|
| 62 |  | 
|---|
| 63 |         // check busylocks | 
|---|
| 64 |     uint32_t count = this->busylocks; | 
|---|
| 65 |         if( count ) | 
|---|
| 66 |         { | 
|---|
| 67 |  | 
|---|
| 68 | #if DEBUG_SYSCALLS_ERROR | 
|---|
| 69 | printk("\n[ERROR] in %s : busylocks count = %d  / thread[%x,%x]\n", | 
|---|
| 70 | __FUNCTION__ , count, process->pid, this->trdid ); | 
|---|
| 71 | #endif | 
|---|
| 72 |             this->errno = EINVAL; | 
|---|
| 73 |                 return -1; | 
|---|
| 74 |     } | 
|---|
| 75 |  | 
|---|
| 76 |     // If calling thread is the main thread, the process must be deleted.  | 
|---|
| 77 |     // => delete all process threads and synchronize with parent process. | 
|---|
| 78 |     // If calling thread is not the main thread, only this thread must be deleted. | 
|---|
| 79 |     // => register exit_status in thread descriptor, block the thread,  | 
|---|
| 80 |     //    mark it for delete, and synchronize with joining thread.  | 
|---|
| 81 |  | 
|---|
| 82 |     if( (CXY_FROM_PID( pid ) == local_cxy) && (LTID_FROM_TRDID(trdid) == 0) )  // main | 
|---|
| 83 |     { | 
|---|
| 84 |  | 
|---|
| 85 | #if DEBUG_SYS_THREAD_EXIT | 
|---|
| 86 | uint64_t     tm_start = hal_get_cycles(); | 
|---|
| 87 | if( DEBUG_SYS_THREAD_EXIT < tm_start ) | 
|---|
| 88 | printk("\n[%s] thread[%x,%x] is main => delete process / cycle %d\n", | 
|---|
| 89 | __FUNCTION__ , pid , trdid , (uint32_t)tm_start ); | 
|---|
| 90 | #endif | 
|---|
| 91 |         // delete process | 
|---|
| 92 |         sys_exit( 0 ); | 
|---|
| 93 |     } | 
|---|
| 94 |     else                                                             // not the main | 
|---|
| 95 |     { | 
|---|
| 96 |  | 
|---|
| 97 | #if DEBUG_SYS_THREAD_EXIT | 
|---|
| 98 | uint64_t     tm_start = hal_get_cycles(); | 
|---|
| 99 | if( DEBUG_SYS_THREAD_EXIT < tm_start ) | 
|---|
| 100 | printk("\n[%s] thread[%x,%x] is not main => delete thread / cycle %d\n", | 
|---|
| 101 | __FUNCTION__ , pid , trdid , (uint32_t)tm_start ); | 
|---|
| 102 | #endif | 
|---|
| 103 |         // register exit_status in thread descriptor | 
|---|
| 104 |         this->exit_status = exit_status; | 
|---|
| 105 |  | 
|---|
| 106 |         // block calling thread and mark it for delete, | 
|---|
| 107 |         thread_delete( XPTR( local_cxy , this ) , false );   // not forced | 
|---|
| 108 |  | 
|---|
| 109 |         // deschedule | 
|---|
| 110 |         sched_yield( "suicide after thread_exit" ); | 
|---|
| 111 |     } | 
|---|
| 112 |  | 
|---|
| 113 |     return 0;   // never executed but required by compiler | 
|---|
| 114 |  | 
|---|
| 115 | }  // end sys_thread exit | 
|---|