1 | /* |
---|
2 | * sys_kill.c - Kernel function implementing the "kill" system call. |
---|
3 | * |
---|
4 | * Author 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 <kernel_config.h> |
---|
25 | #include <hal_kernel_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 | #include <syscalls.h> |
---|
36 | |
---|
37 | |
---|
38 | #if DEBUG_SYS_KILL |
---|
39 | //////////////////////////////////////////// |
---|
40 | static char* sig_type_str( uint32_t sig_id ) |
---|
41 | { |
---|
42 | switch( sig_id ) |
---|
43 | { |
---|
44 | case SIGKILL: return "SIGKILL"; |
---|
45 | case SIGSTOP: return "SIGSTOP"; |
---|
46 | case SIGCONT: return "SIGCONT"; |
---|
47 | default: return "undefined"; |
---|
48 | } |
---|
49 | } |
---|
50 | #endif |
---|
51 | |
---|
52 | |
---|
53 | /////////////////////////// |
---|
54 | int sys_kill( pid_t pid, |
---|
55 | uint32_t sig_id ) |
---|
56 | { |
---|
57 | xptr_t owner_xp; // extended pointer on process in owner cluster |
---|
58 | cxy_t owner_cxy; // process owner cluster |
---|
59 | process_t * owner_ptr; // local pointer on process in owner cluster |
---|
60 | xptr_t parent_xp; // extended pointer on parent process |
---|
61 | cxy_t parent_cxy; // parent process cluster |
---|
62 | process_t * parent_ptr; // local pointer on parent process |
---|
63 | thread_t * parent_main_ptr; // local pointer on parent main thread |
---|
64 | xptr_t parent_main_xp; // extended pointer on parent main thread |
---|
65 | |
---|
66 | thread_t * this = CURRENT_THREAD; |
---|
67 | process_t * process = this->process; |
---|
68 | |
---|
69 | #if (DEBUG_SYS_KILL || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
70 | uint64_t tm_start = hal_get_cycles(); |
---|
71 | #endif |
---|
72 | |
---|
73 | #if DEBUG_SYS_KILL |
---|
74 | tm_start = hal_get_cycles(); |
---|
75 | if( DEBUG_SYS_KILL < tm_start ) |
---|
76 | printk("\n[%s] thread[%x,%x] enter : %s to process %x / cycle %d\n", |
---|
77 | __FUNCTION__, this->process->pid, this->trdid, |
---|
78 | sig_type_str(sig_id), pid, (uint32_t)tm_start ); |
---|
79 | #endif |
---|
80 | |
---|
81 | // get pointers on process descriptor in owner cluster |
---|
82 | owner_xp = cluster_get_owner_process_from_pid( pid ); |
---|
83 | owner_cxy = GET_CXY( owner_xp ); |
---|
84 | owner_ptr = GET_PTR( owner_xp ); |
---|
85 | |
---|
86 | #if (DEBUG_SYS_KILL & 1) |
---|
87 | if( DEBUG_SYS_KILL < tm_start ) |
---|
88 | printk("\n[%s] thread[%x,%x] get target process descriptor %x in owner cluster %x\n", |
---|
89 | __FUNCTION__ , this->process->pid, this->trdid, owner_ptr, owner_cxy ); |
---|
90 | #endif |
---|
91 | |
---|
92 | // check process found |
---|
93 | if( owner_xp == XPTR_NULL) |
---|
94 | { |
---|
95 | |
---|
96 | #if DEBUG_SYSCALLS_ERROR |
---|
97 | printk("\n[ERROR] in %s : process %x not found\n", __FUNCTION__, pid ); |
---|
98 | #endif |
---|
99 | this->errno = EINVAL; |
---|
100 | return -1; |
---|
101 | } |
---|
102 | |
---|
103 | // get parent process descriptor pointers |
---|
104 | parent_xp = (xptr_t)hal_remote_l64( XPTR( owner_cxy , &owner_ptr->parent_xp ) ); |
---|
105 | parent_cxy = GET_CXY( parent_xp ); |
---|
106 | parent_ptr = GET_PTR( parent_xp ); |
---|
107 | |
---|
108 | #if (DEBUG_SYS_KILL & 1) |
---|
109 | if( DEBUG_SYS_KILL < tm_start ) |
---|
110 | printk("\n[%s] thread[%x,%x] get parent process descriptor %x in cluster %x\n", |
---|
111 | __FUNCTION__ , this->process->pid, this->trdid, parent_ptr, parent_cxy ); |
---|
112 | #endif |
---|
113 | |
---|
114 | // get pointers on the parent process main thread |
---|
115 | parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->th_tbl[0] ) ); |
---|
116 | parent_main_xp = XPTR( parent_cxy , parent_main_ptr ); |
---|
117 | |
---|
118 | // analyse signal type / supported values are : 0, SIGSTOP, SIGCONT, SIGKILL |
---|
119 | switch( sig_id ) |
---|
120 | { |
---|
121 | case 0 : // does nothing |
---|
122 | { |
---|
123 | break; |
---|
124 | } |
---|
125 | case SIGSTOP: // block all target process threads |
---|
126 | { |
---|
127 | // block all threads in all clusters |
---|
128 | process_sigaction( pid , BLOCK_ALL_THREADS ); |
---|
129 | |
---|
130 | #if (DEBUG_SYS_KILL & 1) |
---|
131 | if( DEBUG_SYS_KILL < tm_start ) |
---|
132 | printk("\n[%s] thread[%x,%x] blocked all threads of process %x\n", |
---|
133 | __FUNCTION__ , this->process->pid, this->trdid, pid ); |
---|
134 | #endif |
---|
135 | // atomically update owner process termination state |
---|
136 | hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
137 | PROCESS_TERM_STOP ); |
---|
138 | |
---|
139 | // unblock the parent process main thread |
---|
140 | thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT ); |
---|
141 | |
---|
142 | // calling thread deschedules when it is itself a target thread |
---|
143 | if( this->process->pid == pid ) |
---|
144 | { |
---|
145 | |
---|
146 | #if (DEBUG_SYS_KILL & 1) |
---|
147 | if( DEBUG_SYS_KILL < tm_start ) |
---|
148 | printk("\n[%s] thread[%x,%x] is a target thread => deschedule\n", |
---|
149 | __FUNCTION__ , this->process->pid, this->trdid ); |
---|
150 | #endif |
---|
151 | sched_yield("block itself"); |
---|
152 | } |
---|
153 | |
---|
154 | break; |
---|
155 | } |
---|
156 | case SIGCONT: // unblock all target process threads |
---|
157 | { |
---|
158 | // unblock all threads in all clusters |
---|
159 | process_sigaction( pid , UNBLOCK_ALL_THREADS ); |
---|
160 | |
---|
161 | // atomically update owner process termination state |
---|
162 | hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
163 | ~PROCESS_TERM_STOP ); |
---|
164 | |
---|
165 | // unblock the parent process main thread |
---|
166 | thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT ); |
---|
167 | |
---|
168 | break; |
---|
169 | } |
---|
170 | case SIGKILL: |
---|
171 | { |
---|
172 | // a process cannot kill itself |
---|
173 | if( (pid == process->pid) ) |
---|
174 | { |
---|
175 | |
---|
176 | #if DEBUG_SYSCALLS_ERROR |
---|
177 | printk("\n[ERROR] in %s : process %x cannot kill itself\n", __FUNCTION__, pid ); |
---|
178 | #endif |
---|
179 | this->errno = EINVAL; |
---|
180 | return -1; |
---|
181 | } |
---|
182 | |
---|
183 | // processe INIT cannot be killed |
---|
184 | if( pid == 1 ) |
---|
185 | { |
---|
186 | |
---|
187 | #if DEBUG_SYSCALLS_ERROR |
---|
188 | printk("\n[ERROR] in %s : process_init cannot be killed\n", __FUNCTION__ ); |
---|
189 | #endif |
---|
190 | this->errno = EINVAL; |
---|
191 | return -1; |
---|
192 | } |
---|
193 | |
---|
194 | // remove process from TXT list |
---|
195 | process_txt_detach( owner_xp ); |
---|
196 | |
---|
197 | // close all open files |
---|
198 | process_fd_clean_all( owner_xp ); |
---|
199 | |
---|
200 | // mark for delete all threads in all clusters, but the main |
---|
201 | process_sigaction( pid , DELETE_ALL_THREADS ); |
---|
202 | |
---|
203 | // block main thread |
---|
204 | xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] ); |
---|
205 | thread_block( main_xp , THREAD_BLOCKED_GLOBAL ); |
---|
206 | |
---|
207 | // atomically update owner process descriptor term_state to ask |
---|
208 | // the parent process sys_wait() function to delete this main thread |
---|
209 | hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
210 | PROCESS_TERM_KILL ); |
---|
211 | |
---|
212 | // unblock the parent process main thread |
---|
213 | thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT ); |
---|
214 | |
---|
215 | break; |
---|
216 | } |
---|
217 | default: |
---|
218 | { |
---|
219 | |
---|
220 | #if DEBUG_SYSCALLS_ERROR |
---|
221 | printk("\n[ERROR] in %s : illegal signal %d / process %x\n", __FUNCTION__, sig_id, pid ); |
---|
222 | #endif |
---|
223 | this->errno = EINVAL; |
---|
224 | return -1; |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | hal_fence(); |
---|
229 | |
---|
230 | #if (DEBUG_SYS_KILL || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
231 | uint64_t tm_end = hal_get_cycles(); |
---|
232 | #endif |
---|
233 | |
---|
234 | #if DEBUG_SYS_KILL |
---|
235 | if( DEBUG_SYS_KILL < tm_end ) |
---|
236 | printk("\n[%s] thread[%x,%x] exit / process %x / %s / cost = %d / cycle %d\n", |
---|
237 | __FUNCTION__ , this->process->pid, this->trdid, pid, |
---|
238 | sig_type_str(sig_id), (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
239 | #endif |
---|
240 | |
---|
241 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
242 | hal_atomic_add( &syscalls_cumul_cost[SYS_KILL] , tm_end - tm_start ); |
---|
243 | hal_atomic_add( &syscalls_occurences[SYS_KILL] , 1 ); |
---|
244 | #endif |
---|
245 | |
---|
246 | return 0; |
---|
247 | |
---|
248 | } // end sys_kill() |
---|
249 | |
---|