| 1 | /* |
|---|
| 2 | * sys_kill.c - Kernel function implementing the "kill" system call. |
|---|
| 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> |
|---|
| 26 | #include <hal_irqmask.h> |
|---|
| 27 | #include <errno.h> |
|---|
| 28 | #include <thread.h> |
|---|
| 29 | #include <printk.h> |
|---|
| 30 | #include <process.h> |
|---|
| 31 | #include <shared_syscalls.h> |
|---|
| 32 | #include <cluster.h> |
|---|
| 33 | #include <rpc.h> |
|---|
| 34 | |
|---|
| 35 | /////////////////////////// |
|---|
| 36 | int sys_kill( pid_t pid, |
|---|
| 37 | uint32_t sig_id ) |
|---|
| 38 | { |
|---|
| 39 | uint32_t save_sr; // required to enable IRQs |
|---|
| 40 | xptr_t process_xp; // extended pointer on target reference process |
|---|
| 41 | cxy_t process_cxy; // target process cluster |
|---|
| 42 | process_t * process_ptr; // local pointer on target process |
|---|
| 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 |
|---|
| 47 | |
|---|
| 48 | thread_t * this = CURRENT_THREAD; |
|---|
| 49 | |
|---|
| 50 | #if CONFIG_SYSCALL_DEBUG |
|---|
| 51 | uint64_t tm_start; |
|---|
| 52 | uint64_t tm_end; |
|---|
| 53 | tm_start = hal_get_cycles(); |
|---|
| 54 | printk("\n[DBG] %s : core[%x,%d] enter / process %x / sig %d / cycle %d\n", |
|---|
| 55 | __FUNCTION__ , local_cxy , this->core->lid , pid, sig_id, (uint32_t)tm_start ); |
|---|
| 56 | #endif |
|---|
| 57 | |
|---|
| 58 | // get cluster and pointers on reference process |
|---|
| 59 | process_xp = cluster_get_reference_process_from_pid( pid ); |
|---|
| 60 | process_cxy = GET_CXY( process_xp ); |
|---|
| 61 | process_ptr = (process_t *)GET_PTR( process_xp ); |
|---|
| 62 | |
|---|
| 63 | // check process existence |
|---|
| 64 | if( process_xp == XPTR_NULL ) |
|---|
| 65 | { |
|---|
| 66 | syscall_dmsg("\n[ERROR] in %s : process %x not found\n", |
|---|
| 67 | __FUNCTION__ , pid ); |
|---|
| 68 | this->errno = EINVAL; |
|---|
| 69 | return -1; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | // get parent process PID |
|---|
| 73 | parent_xp = hal_remote_lwd( XPTR( process_cxy , &process_ptr->parent_xp ) ); |
|---|
| 74 | parent_cxy = GET_CXY( parent_xp ); |
|---|
| 75 | parent_ptr = GET_PTR( parent_xp ); |
|---|
| 76 | ppid = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) ); |
|---|
| 77 | |
|---|
| 78 | // processes INIT and processes KSH cannot be stoped or killed |
|---|
| 79 | if( ppid < 2 ) |
|---|
| 80 | { |
|---|
| 81 | syscall_dmsg("\n[ERROR] in %s : process %x cannot be killed\n", |
|---|
| 82 | __FUNCTION__ , pid ); |
|---|
| 83 | this->errno = EINVAL; |
|---|
| 84 | return -1; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | // does nothing if sig_id == 0 |
|---|
| 88 | if( sig_id == 0 ) return 0; |
|---|
| 89 | |
|---|
| 90 | // check sig_id |
|---|
| 91 | if( (sig_id != SIGSTOP) && (sig_id != SIGCONT) && (sig_id != SIGKILL) ) |
|---|
| 92 | { |
|---|
| 93 | syscall_dmsg("\n[ERROR] in %s : illegal signal type for process %x\n", |
|---|
| 94 | __FUNCTION__ , sig_id , pid ); |
|---|
| 95 | this->errno = EINVAL; |
|---|
| 96 | return -1; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | // enable IRQs |
|---|
| 100 | hal_enable_irq( &save_sr ); |
|---|
| 101 | |
|---|
| 102 | // execute process_make_kill() function in owner cluster |
|---|
| 103 | if( local_cxy == process_cxy ) // owner cluster is local |
|---|
| 104 | { |
|---|
| 105 | process_make_kill( pid , sig_id ); |
|---|
| 106 | } |
|---|
| 107 | else // owner cluster is remote |
|---|
| 108 | { |
|---|
| 109 | rpc_process_make_kill_client( process_cxy , pid , sig_id ); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | // restore IRQs |
|---|
| 113 | hal_restore_irq( save_sr ); |
|---|
| 114 | |
|---|
| 115 | hal_fence(); |
|---|
| 116 | |
|---|
| 117 | #if CONFIG_SYSCALL_DEBUG |
|---|
| 118 | tm_end = hal_get_cycles(); |
|---|
| 119 | printk("\n[DBG] %s : core[%x,%d] exit / process %x / sig %d / cost = %d\n", |
|---|
| 120 | __FUNCTION__ , local_cxy , this->core->lid , pid, sig_id, (uint32_t)(tm_end - tm_start) ); |
|---|
| 121 | #endif |
|---|
| 122 | |
|---|
| 123 | return 0; |
|---|
| 124 | |
|---|
| 125 | } // end sys_kill() |
|---|
| 126 | |
|---|