[23] | 1 | /* |
---|
[416] | 2 | * sys_kill.c - Kernel function implementing the "kill" system call. |
---|
[23] | 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 <kernel_config.h> |
---|
| 25 | #include <hal_types.h> |
---|
[409] | 26 | #include <hal_irqmask.h> |
---|
[23] | 27 | #include <errno.h> |
---|
| 28 | #include <thread.h> |
---|
| 29 | #include <printk.h> |
---|
| 30 | #include <process.h> |
---|
[416] | 31 | #include <shared_syscalls.h> |
---|
[23] | 32 | #include <cluster.h> |
---|
| 33 | #include <rpc.h> |
---|
| 34 | |
---|
| 35 | /////////////////////////// |
---|
| 36 | int sys_kill( pid_t pid, |
---|
| 37 | uint32_t sig_id ) |
---|
| 38 | { |
---|
[409] | 39 | uint32_t save_sr; // required to enable IRQs |
---|
[433] | 40 | xptr_t owner_xp; // extended pointer on target reference process |
---|
| 41 | cxy_t owner_cxy; // target process cluster |
---|
| 42 | process_t * owner_ptr; // local pointer on target process |
---|
[421] | 43 | xptr_t parent_xp; // extended pointer on parent process |
---|
| 44 | cxy_t parent_cxy; // parent process cluster |
---|
| 45 | process_t * parent_ptr; // local pointer on parent process |
---|
| 46 | pid_t ppid; // parent process PID |
---|
[433] | 47 | uint32_t retval; // return value for the switch |
---|
[409] | 48 | |
---|
[416] | 49 | thread_t * this = CURRENT_THREAD; |
---|
| 50 | |
---|
[433] | 51 | #if CONFIG_DEBUG_SYS_KILL |
---|
[409] | 52 | uint64_t tm_start; |
---|
| 53 | uint64_t tm_end; |
---|
| 54 | tm_start = hal_get_cycles(); |
---|
[433] | 55 | if( CONFIG_DEBUG_SYS_KILL < tm_start ) |
---|
| 56 | printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cycle %d\n", |
---|
| 57 | __FUNCTION__ , this, pid, sig_id, (uint32_t)tm_start ); |
---|
[409] | 58 | #endif |
---|
| 59 | |
---|
[433] | 60 | // get cluster and pointers on owner process |
---|
| 61 | owner_xp = cluster_get_owner_process_from_pid( pid ); |
---|
| 62 | owner_cxy = GET_CXY( owner_xp ); |
---|
| 63 | owner_ptr = GET_PTR( owner_xp ); |
---|
[23] | 64 | |
---|
[421] | 65 | // check process existence |
---|
[433] | 66 | if( owner_xp == XPTR_NULL ) |
---|
[23] | 67 | { |
---|
[433] | 68 | |
---|
| 69 | syscall_dmsg("\n[ERROR] in %s : process %x not found\n", __FUNCTION__ , pid ); |
---|
| 70 | |
---|
[421] | 71 | this->errno = EINVAL; |
---|
| 72 | return -1; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | // get parent process PID |
---|
[433] | 76 | parent_xp = hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) ); |
---|
[421] | 77 | parent_cxy = GET_CXY( parent_xp ); |
---|
| 78 | parent_ptr = GET_PTR( parent_xp ); |
---|
| 79 | ppid = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) ); |
---|
| 80 | |
---|
| 81 | // processes INIT and processes KSH cannot be stoped or killed |
---|
| 82 | if( ppid < 2 ) |
---|
| 83 | { |
---|
[23] | 84 | |
---|
[433] | 85 | syscall_dmsg("\n[ERROR] in %s : process %x cannot be killed\n", __FUNCTION__ , pid ); |
---|
| 86 | |
---|
[409] | 87 | this->errno = EINVAL; |
---|
| 88 | return -1; |
---|
| 89 | } |
---|
[23] | 90 | |
---|
[409] | 91 | // enable IRQs |
---|
| 92 | hal_enable_irq( &save_sr ); |
---|
[23] | 93 | |
---|
[433] | 94 | // analyse signal type |
---|
| 95 | // supported values are : 0, SIGSTOP, SIGCONT, SIGKILL |
---|
| 96 | switch( sig_id ) |
---|
[23] | 97 | { |
---|
[433] | 98 | case 0 : |
---|
| 99 | { |
---|
| 100 | // does nothing |
---|
| 101 | retval = 0; |
---|
| 102 | break; |
---|
| 103 | } |
---|
| 104 | case SIGSTOP: |
---|
| 105 | { |
---|
| 106 | // remove TXT ownership from target process |
---|
| 107 | process_txt_reset_ownership( owner_xp ); |
---|
| 108 | |
---|
| 109 | // block all threads in all clusters |
---|
| 110 | process_sigaction( owner_ptr , BLOCK_ALL_THREADS ); |
---|
| 111 | |
---|
| 112 | // atomically update reference process termination state |
---|
| 113 | hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
| 114 | PROCESS_FLAG_BLOCK ); |
---|
| 115 | |
---|
| 116 | retval = 0; |
---|
| 117 | break; |
---|
| 118 | } |
---|
| 119 | case SIGCONT: |
---|
| 120 | { |
---|
| 121 | // unblock all threads in all clusters |
---|
| 122 | process_sigaction( owner_ptr , UNBLOCK_ALL_THREADS ); |
---|
| 123 | |
---|
| 124 | // atomically update reference process termination state |
---|
| 125 | hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
| 126 | ~PROCESS_FLAG_BLOCK ); |
---|
| 127 | retval = 0; |
---|
| 128 | break; |
---|
| 129 | } |
---|
| 130 | break; |
---|
| 131 | case SIGKILL: |
---|
| 132 | { |
---|
| 133 | // the process_make_kill() function must be executed |
---|
| 134 | // by an RPC thread in process owner cluster |
---|
| 135 | // It deletes all target process threads in all clusters, |
---|
| 136 | // and updates the process termination state |
---|
| 137 | rpc_process_make_kill_client( owner_cxy , owner_ptr , false , 0 ); |
---|
| 138 | |
---|
| 139 | retval = 0; |
---|
| 140 | break; |
---|
| 141 | } |
---|
| 142 | default: |
---|
| 143 | { |
---|
| 144 | |
---|
| 145 | syscall_dmsg("\n[ERROR] in %s : illegal signal type %d for process %x\n", |
---|
| 146 | __FUNCTION__ , sig_id , pid ); |
---|
| 147 | |
---|
| 148 | this->errno = EINVAL; |
---|
| 149 | retval = -1; |
---|
| 150 | break; |
---|
| 151 | } |
---|
[23] | 152 | } |
---|
[433] | 153 | |
---|
[409] | 154 | // restore IRQs |
---|
| 155 | hal_restore_irq( save_sr ); |
---|
[23] | 156 | |
---|
[124] | 157 | hal_fence(); |
---|
[23] | 158 | |
---|
[433] | 159 | #if CONFIG_DEBUG_SYS_KILL |
---|
[409] | 160 | tm_end = hal_get_cycles(); |
---|
[433] | 161 | if( CONFIG_DEBUG_SYS_KILL < tm_end ) |
---|
| 162 | printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cost = %d / cycle %d\n", |
---|
| 163 | __FUNCTION__ , this, pid, sig_id, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
[409] | 164 | #endif |
---|
[23] | 165 | |
---|
[433] | 166 | return retval; |
---|
| 167 | |
---|
[23] | 168 | } // end sys_kill() |
---|
| 169 | |
---|