| 1 | /*
|
|---|
| 2 | * sys_thread_cancel.c - terminate execution of an user thread.
|
|---|
| 3 | *
|
|---|
| 4 | * Authors Alain Greiner (2016,2017, 2018)
|
|---|
| 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_irqmask.h>
|
|---|
| 26 | #include <hal_remote.h>
|
|---|
| 27 | #include <hal_special.h>
|
|---|
| 28 | #include <thread.h>
|
|---|
| 29 | #include <errno.h>
|
|---|
| 30 | #include <printk.h>
|
|---|
| 31 |
|
|---|
| 32 | //////////////////////////////////////
|
|---|
| 33 | int sys_thread_cancel( trdid_t trdid )
|
|---|
| 34 | {
|
|---|
| 35 | reg_t save_sr; // required to enable IRQs
|
|---|
| 36 | xptr_t target_xp; // target thread extended pointer
|
|---|
| 37 | cxy_t target_cxy; // target thread cluster identifier
|
|---|
| 38 | ltid_t target_ltid; // target thread local index
|
|---|
| 39 | cxy_t owner_cxy; // process owner cluster identifier
|
|---|
| 40 | xptr_t owner_xp; // extended pointer on owner process
|
|---|
| 41 |
|
|---|
| 42 | // get killer thread pointers
|
|---|
| 43 | thread_t * this = CURRENT_THREAD;
|
|---|
| 44 | process_t * process = this->process;
|
|---|
| 45 | pid_t pid = process->pid;
|
|---|
| 46 |
|
|---|
| 47 | // get extended pointer on target thread
|
|---|
| 48 | target_xp = thread_get_xptr( pid , trdid );
|
|---|
| 49 |
|
|---|
| 50 | // check target_xp
|
|---|
| 51 | if( target_xp == XPTR_NULL )
|
|---|
| 52 | {
|
|---|
| 53 |
|
|---|
| 54 | #if DEBUG_SYSCALLS_ERROR
|
|---|
| 55 | printk("\n[ERROR] in %s : target thread %x not found\n", __FUNCTION__, trdid );
|
|---|
| 56 | #endif
|
|---|
| 57 | this->errno = EINVAL;
|
|---|
| 58 | return -1;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | #if DEBUG_SYS_THREAD_CANCEL
|
|---|
| 62 | uint64_t tm_start;
|
|---|
| 63 | uint64_t tm_end;
|
|---|
| 64 | tm_start = hal_get_cycles();
|
|---|
| 65 | if( DEBUG_SYS_THREAD_CANCEL < tm_start )
|
|---|
| 66 | printk("\n[DBG] %s : thread %x enter to kill thread %x / cycle %d\n",
|
|---|
| 67 | __FUCTION__, this, GET_PTR( target_xp ), (uint32_t)tm_start );
|
|---|
| 68 | #endif
|
|---|
| 69 |
|
|---|
| 70 | // get process owner cluster identifier
|
|---|
| 71 | owner_cxy = CXY_FROM_PID( pid );
|
|---|
| 72 |
|
|---|
| 73 | // get target thread ltid and cluster
|
|---|
| 74 | target_cxy = CXY_FROM_TRDID( trdid );
|
|---|
| 75 | target_ltid = LTID_FROM_TRDID( trdid );
|
|---|
| 76 |
|
|---|
| 77 | // If target thread is the main thread, the process must be deleted,
|
|---|
| 78 | // This require synchronisation with parent process
|
|---|
| 79 | if( (target_cxy == owner_cxy) && (target_ltid == 0) )
|
|---|
| 80 | {
|
|---|
| 81 | // get extended pointer on owner cluster
|
|---|
| 82 | owner_xp = cluster_get_owner_process_from_pid( pid );
|
|---|
| 83 |
|
|---|
| 84 | // mark for delete all threads but the main
|
|---|
| 85 | hal_enable_irq( &save_sr );
|
|---|
| 86 | process_sigaction( pid , DELETE_ALL_THREADS );
|
|---|
| 87 | hal_restore_irq( save_sr );
|
|---|
| 88 |
|
|---|
| 89 | // remove process from TXT list
|
|---|
| 90 | process_txt_detach( owner_xp );
|
|---|
| 91 |
|
|---|
| 92 | // block the main thread
|
|---|
| 93 | thread_block( XPTR( local_cxy ,this ) , THREAD_BLOCKED_GLOBAL );
|
|---|
| 94 |
|
|---|
| 95 | // atomically update owner process descriptor term_state to ask
|
|---|
| 96 | // the parent process sys_wait() function to delete the main thread
|
|---|
| 97 | hal_remote_atomic_or( XPTR( local_cxy , &process->term_state ) ,
|
|---|
| 98 | PROCESS_TERM_EXIT );
|
|---|
| 99 | }
|
|---|
| 100 | else
|
|---|
| 101 | {
|
|---|
| 102 | // block target thread and mark it for delete
|
|---|
| 103 | thread_delete( target_xp , pid , false );
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | #if DEBUG_SYS_THREAD_CANCEL
|
|---|
| 107 | tm_end = hal_get_cycles();
|
|---|
| 108 | if( DEBUG_SYS_THREAD_CANCEL < tm_end )
|
|---|
| 109 | printk("\n[DBG] %s : thread %x exit after kill thread %x / cycle %d\n",
|
|---|
| 110 | __FUCTION__, this, GET_PTR( target_xp ), (uint32_t)tm_end );
|
|---|
| 111 | #endif
|
|---|
| 112 |
|
|---|
| 113 | return 0;
|
|---|
| 114 |
|
|---|
| 115 | } // end sys_thread_cancel()
|
|---|