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

Last change on this file since 501 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

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