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 | int sys_kill( pid_t pid, |
---|
39 | uint32_t sig_id ) |
---|
40 | { |
---|
41 | reg_t save_sr; // required to enable IRQs |
---|
42 | xptr_t owner_xp; // extended pointer on process in owner cluster |
---|
43 | cxy_t owner_cxy; // process owner cluster |
---|
44 | process_t * owner_ptr; // local pointer on process in owner cluster |
---|
45 | uint32_t retval; // return value for the switch |
---|
46 | xptr_t parent_xp; // extended pointer on parent process |
---|
47 | cxy_t parent_cxy; // parent process cluster |
---|
48 | process_t * parent_ptr; // local pointer on parent process |
---|
49 | xptr_t children_lock_xp; // extended pointer on children lock in parent |
---|
50 | thread_t * parent_main_ptr; // local pointer on parent main thread |
---|
51 | xptr_t parent_main_xp; // extended pointer on parent main thread |
---|
52 | |
---|
53 | thread_t * this = CURRENT_THREAD; |
---|
54 | process_t * process = this->process; |
---|
55 | |
---|
56 | #if DEBUG_SYS_KILL |
---|
57 | uint64_t tm_start; |
---|
58 | uint64_t tm_end; |
---|
59 | tm_start = hal_get_cycles(); |
---|
60 | if( DEBUG_SYS_KILL < tm_start ) |
---|
61 | printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cycle %d\n", |
---|
62 | __FUNCTION__ , this, pid, sig_id, (uint32_t)tm_start ); |
---|
63 | #endif |
---|
64 | |
---|
65 | // get pointers on process descriptor in owner cluster |
---|
66 | owner_xp = cluster_get_owner_process_from_pid( pid ); |
---|
67 | owner_cxy = GET_CXY( owner_xp ); |
---|
68 | owner_ptr = GET_PTR( owner_xp ); |
---|
69 | |
---|
70 | #if (DEBUG_SYS_KILL & 1) |
---|
71 | if( DEBUG_SYS_KILL < tm_start ) |
---|
72 | printk("\n[DBG] %s : thread %x get owner process %x in cluster %x\n", |
---|
73 | __FUNCTION__ , this, owner_ptr, owner_cxy ); |
---|
74 | #endif |
---|
75 | |
---|
76 | // check process found |
---|
77 | if( owner_xp == XPTR_NULL) |
---|
78 | { |
---|
79 | |
---|
80 | #if DEBUG_SYSCALLS_ERROR |
---|
81 | printk("\n[ERROR] in %s : process %x not found\n", __FUNCTION__, pid ); |
---|
82 | #endif |
---|
83 | this->errno = EINVAL; |
---|
84 | return -1; |
---|
85 | } |
---|
86 | |
---|
87 | // process cannot kill itself |
---|
88 | if( (pid == process->pid) ) |
---|
89 | { |
---|
90 | |
---|
91 | #if DEBUG_SYSCALLS_ERROR |
---|
92 | printk("\n[ERROR] in %s : process %x cannot kill itself\n", __FUNCTION__, pid ); |
---|
93 | #endif |
---|
94 | this->errno = EINVAL; |
---|
95 | return -1; |
---|
96 | } |
---|
97 | |
---|
98 | // processe INIT cannot be killed |
---|
99 | if( pid == 1 ) |
---|
100 | { |
---|
101 | |
---|
102 | #if DEBUG_SYSCALLS_ERROR |
---|
103 | printk("\n[ERROR] in %s : process_init cannot be killed\n", __FUNCTION__ ); |
---|
104 | #endif |
---|
105 | this->errno = EINVAL; |
---|
106 | return -1; |
---|
107 | } |
---|
108 | |
---|
109 | // get parent process descriptor pointers |
---|
110 | parent_xp = (xptr_t)hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) ); |
---|
111 | parent_cxy = GET_CXY( parent_xp ); |
---|
112 | parent_ptr = GET_PTR( parent_xp ); |
---|
113 | |
---|
114 | #if (DEBUG_SYS_KILL & 1) |
---|
115 | if( DEBUG_SYS_KILL < tm_start ) |
---|
116 | printk("\n[DBG] %s : thread %x get parent process %x in cluster %x\n", |
---|
117 | __FUNCTION__ , this, parent_ptr, parent_cxy ); |
---|
118 | #endif |
---|
119 | |
---|
120 | // get extended pointer on lock protecting children list in parent process |
---|
121 | children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock ); |
---|
122 | |
---|
123 | // get pointers on the parent process main thread |
---|
124 | parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->th_tbl[0] ) ); |
---|
125 | parent_main_xp = XPTR( parent_cxy , parent_main_ptr ); |
---|
126 | |
---|
127 | // analyse signal type / supported values are : 0, SIGSTOP, SIGCONT, SIGKILL |
---|
128 | switch( sig_id ) |
---|
129 | { |
---|
130 | case 0 : // does nothing |
---|
131 | { |
---|
132 | retval = 0; |
---|
133 | break; |
---|
134 | } |
---|
135 | case SIGSTOP: // block all target process threads |
---|
136 | { |
---|
137 | // transfer TXT ownership |
---|
138 | process_txt_transfer_ownership( owner_xp ); |
---|
139 | |
---|
140 | // block all threads in all clusters, but the main thread |
---|
141 | process_sigaction( pid , BLOCK_ALL_THREADS ); |
---|
142 | |
---|
143 | // block the main thread |
---|
144 | xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] ); |
---|
145 | thread_block( main_xp , THREAD_BLOCKED_GLOBAL ); |
---|
146 | |
---|
147 | // atomically update owner process termination state |
---|
148 | hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
149 | PROCESS_TERM_STOP ); |
---|
150 | |
---|
151 | // take the children lock and unblock the parent process main thread |
---|
152 | remote_spinlock_lock( children_lock_xp ); |
---|
153 | thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT ); |
---|
154 | remote_spinlock_unlock( children_lock_xp ); |
---|
155 | |
---|
156 | retval = 0; |
---|
157 | break; |
---|
158 | } |
---|
159 | case SIGCONT: // unblock all target process threads |
---|
160 | { |
---|
161 | // unblock all threads in all clusters |
---|
162 | process_sigaction( pid , UNBLOCK_ALL_THREADS ); |
---|
163 | |
---|
164 | // atomically update owner process termination state |
---|
165 | hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
166 | ~PROCESS_TERM_STOP ); |
---|
167 | retval = 0; |
---|
168 | break; |
---|
169 | } |
---|
170 | break; |
---|
171 | case SIGKILL: |
---|
172 | { |
---|
173 | // remove process from TXT list |
---|
174 | process_txt_detach( owner_xp ); |
---|
175 | |
---|
176 | // mark for delete all threads in all clusters, but the main |
---|
177 | hal_enable_irq( &save_sr ); |
---|
178 | process_sigaction( pid , DELETE_ALL_THREADS ); |
---|
179 | hal_restore_irq( save_sr ); |
---|
180 | |
---|
181 | // block main thread |
---|
182 | xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] ); |
---|
183 | thread_block( main_xp , THREAD_BLOCKED_GLOBAL ); |
---|
184 | |
---|
185 | // atomically update owner process descriptor term_state to ask |
---|
186 | // the parent process sys_wait() function to delete this main thread |
---|
187 | hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
188 | PROCESS_TERM_KILL ); |
---|
189 | |
---|
190 | // take the children lock and unblock the parent process main thread |
---|
191 | remote_spinlock_lock( children_lock_xp ); |
---|
192 | thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT ); |
---|
193 | remote_spinlock_unlock( children_lock_xp ); |
---|
194 | |
---|
195 | retval = 0; |
---|
196 | break; |
---|
197 | } |
---|
198 | default: |
---|
199 | { |
---|
200 | |
---|
201 | #if DEBUG_SYSCALLS_ERROR |
---|
202 | printk("\n[ERROR] in %s : illegal signal %d / process %x\n", __FUNCTION__, sig_id, pid ); |
---|
203 | #endif |
---|
204 | this->errno = EINVAL; |
---|
205 | retval = -1; |
---|
206 | break; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | hal_fence(); |
---|
211 | |
---|
212 | #if DEBUG_SYS_KILL |
---|
213 | tm_end = hal_get_cycles(); |
---|
214 | if( DEBUG_SYS_KILL < tm_end ) |
---|
215 | printk("\n[DBG] %s : thread %x exit / process %x / sig %d / cost = %d / cycle %d\n", |
---|
216 | __FUNCTION__ , this, pid, sig_id, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
217 | #endif |
---|
218 | |
---|
219 | return retval; |
---|
220 | |
---|
221 | } // end sys_kill() |
---|
222 | |
---|