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

Last change on this file since 609 was 594, checked in by alain, 6 years ago

Fix various bugs in sys_stat() and sys_mmap() functions.
Improve debug in other functions.

File size: 7.3 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
38#if DEBUG_SYS_KILL
39////////////////////////////////////////////
40static 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///////////////////////////
54int 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)
70uint64_t     tm_start = hal_get_cycles();
71#endif
72
73#if DEBUG_SYS_KILL
74tm_start = hal_get_cycles();
75if( DEBUG_SYS_KILL < tm_start )
76printk("\n[%s] thread[%x,%x] enter / process %x / %s / cycle %d\n",
77__FUNCTION__, this->process->pid, this->trdid, pid,
78sig_type_str(sig_id), (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)
87if( DEBUG_SYS_KILL < tm_start )
88printk("\n[%s] thread[%x,%x] get owner process %x in 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
97printk("\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)
109if( DEBUG_SYS_KILL < tm_start )
110printk("\n[%x] thread[%x,%x] get parent process %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            // atomically update owner process termination state
131            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
132                                  PROCESS_TERM_STOP );
133
134            // unblock the parent process main thread
135            thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
136
137            // calling thread deschedules when it is itself a target thread
138            if( this->process->pid == pid ) sched_yield("block itself");
139
140            break;
141        }
142        case SIGCONT:     // unblock all target process threads
143        {
144            // unblock all threads in all clusters
145            process_sigaction( pid , UNBLOCK_ALL_THREADS );
146
147            // atomically update owner process termination state
148            hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) ,
149                                   ~PROCESS_TERM_STOP );
150
151            // unblock the parent process main thread
152            thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
153
154            break;
155        }
156        case SIGKILL:
157        {
158            // a process cannot kill itself
159            if( (pid == process->pid) ) 
160            {
161
162#if DEBUG_SYSCALLS_ERROR
163printk("\n[ERROR] in %s : process %x cannot kill itself\n", __FUNCTION__, pid );
164#endif
165                this->errno = EINVAL;
166                return -1;
167            }
168
169            // processe INIT cannot be killed
170            if( pid == 1 )
171            {
172
173#if DEBUG_SYSCALLS_ERROR
174printk("\n[ERROR] in %s : process_init cannot be killed\n", __FUNCTION__ );
175#endif
176                        this->errno = EINVAL;
177                return -1;
178            }
179
180            // remove process from TXT list
181            process_txt_detach( owner_xp );
182
183            // mark for delete all threads in all clusters, but the main
184            process_sigaction( pid , DELETE_ALL_THREADS );
185
186            // block main thread
187            xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
188            thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
189
190            // atomically update owner process descriptor term_state to ask
191            // the parent process sys_wait() function to delete this main thread
192            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
193                                  PROCESS_TERM_KILL );
194
195            // unblock the parent process main thread
196            thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
197
198            break;
199        }
200        default:
201        {
202
203#if DEBUG_SYSCALLS_ERROR
204printk("\n[ERROR] in %s : illegal signal %d / process %x\n", __FUNCTION__, sig_id, pid );
205#endif
206            this->errno = EINVAL;
207            return -1;
208        }
209    }
210   
211    hal_fence();
212
213#if (DEBUG_SYS_KILL || CONFIG_INSTRUMENTATION_SYSCALLS)
214uint64_t     tm_end = hal_get_cycles();
215#endif
216
217#if DEBUG_SYS_KILL
218if( DEBUG_SYS_KILL < tm_end )
219printk("\n[%s] thread[%x,%x] exit / process %x / %s / cost = %d / cycle %d\n",
220__FUNCTION__ , this->process->pid, this->trdid, pid,
221sig_type_str(sig_id), (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
222#endif
223
224#if CONFIG_INSTRUMENTATION_SYSCALLS
225hal_atomic_add( &syscalls_cumul_cost[SYS_KILL] , tm_end - tm_start );
226hal_atomic_add( &syscalls_occurences[SYS_KILL] , 1 );
227#endif
228
229        return 0;
230
231}  // end sys_kill()
232
Note: See TracBrowser for help on using the repository browser.