source: trunk/kernel/syscalls/sys_kill.c @ 576

Last change on this file since 576 was 566, checked in by alain, 6 years ago

Complete restructuration of kernel locks.

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