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

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

Fix a bad bug in scheduler...

File size: 5.6 KB
Line 
1/*
2 * sys_kill.c - Kernel function implementing the "kill" system call.
3 *
4 * Author    Alain Greiner (2016,2017)
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_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    uint32_t    save_sr;       // required to enable IRQs
40    xptr_t      owner_xp;      // extended pointer on target reference process
41    cxy_t       owner_cxy;     // target process cluster
42    process_t * owner_ptr;     // local pointer on target process
43    xptr_t      parent_xp;     // extended pointer on parent process
44    cxy_t       parent_cxy;    // parent process cluster
45    process_t * parent_ptr;    // local pointer on parent process
46    pid_t       ppid;          // parent process PID
47    uint32_t    retval;        // return value for the switch
48
49    thread_t  * this    = CURRENT_THREAD;
50    process_t * process = this->process;
51
52#if CONFIG_DEBUG_SYS_KILL
53uint64_t    tm_start;
54uint64_t    tm_end;
55tm_start = hal_get_cycles();
56if( CONFIG_DEBUG_SYS_KILL < tm_start )
57printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cycle %d\n",
58__FUNCTION__ , this, pid, sig_id, (uint32_t)tm_start );
59#endif
60
61    // process cannot kill itself
62    if( pid == process->pid )
63    {
64
65#if CONFIG_DEBUG_SYSCALLS_ERROR
66printk("\n[ERROR] in %s : process %d cannot kill itself\n", __FUNCTION__ , pid );
67#endif
68        this->errno = EINVAL;
69        return -1;
70    }
71
72    // get cluster and pointers on owner target process descriptor
73    owner_xp  = cluster_get_owner_process_from_pid( pid );
74    owner_cxy = GET_CXY( owner_xp );
75    owner_ptr = GET_PTR( owner_xp );
76
77    // check process existence
78    if( owner_xp == XPTR_NULL )
79    {
80
81#if CONFIG_DEBUG_SYSCALLS_ERROR
82printk("\n[ERROR] in %s : process %x not found\n", __FUNCTION__ , pid );
83#endif
84        this->errno = EINVAL;
85        return -1;
86    }
87   
88    // get parent process PID
89    parent_xp  = hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) );
90    parent_cxy = GET_CXY( parent_xp );
91    parent_ptr = GET_PTR( parent_xp );
92    ppid       = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) );
93
94    // processes INIT
95    if( pid == 1 )
96    {
97
98#if CONFIG_DEBUG_SYSCALLS_ERROR
99printk("\n[ERROR] in %s : process_init cannot be killed\n", __FUNCTION__ );
100#endif
101                this->errno = EINVAL;
102        return -1;
103    }
104
105    // enable IRQs
106    hal_enable_irq( &save_sr );
107
108    // analyse signal type / supported values are : 0, SIGSTOP, SIGCONT, SIGKILL
109    switch( sig_id )
110    {
111        case 0 :
112        {
113            // does nothing
114            retval = 0;
115            break;
116        }
117        case SIGSTOP:     
118        {
119            // remove TXT ownership from target process
120            process_txt_reset_ownership( owner_xp );
121
122            // block all threads in all clusters
123            process_sigaction( pid , BLOCK_ALL_THREADS );
124
125            // atomically update owner process termination state
126            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
127                                  PROCESS_TERM_STOP );
128 
129            retval = 0;
130            break;
131        }
132        case SIGCONT:
133        {
134            // unblock all threads in all clusters
135            process_sigaction( pid , UNBLOCK_ALL_THREADS );
136
137            // atomically update reference process termination state
138            hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) ,
139                                   ~PROCESS_TERM_STOP );
140            retval = 0;
141            break;
142        }
143        break;
144        case SIGKILL:
145        {
146            // remove TXT ownership from owner process descriptor
147            process_txt_reset_ownership( owner_xp );
148
149            // block all process threads in all clusters
150            process_sigaction( pid , BLOCK_ALL_THREADS );
151
152            // mark all process threads in all clusters for delete
153            process_sigaction( pid , DELETE_ALL_THREADS );
154
155            // atomically update owner process descriptor flags
156            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
157                                  PROCESS_TERM_KILL );
158
159            retval = 0;
160            break;
161        }
162        default:
163        {
164
165#if CONFIG_DEBUG_SYSCALLS_ERROR
166printk("\n[ERROR] in %s : illegal signal %d / process %x\n", __FUNCTION__, sig_id, pid );
167#endif
168            this->errno = EINVAL;
169            retval = -1;
170            break;
171        }
172    }
173   
174    // restore IRQs
175    hal_restore_irq( save_sr );
176
177    hal_fence();
178
179#if CONFIG_DEBUG_SYS_KILL
180tm_end = hal_get_cycles();
181if( CONFIG_DEBUG_SYS_KILL < tm_end )
182printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cost = %d / cycle %d\n",
183__FUNCTION__ , this, pid, sig_id, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
184#endif
185
186        return retval;
187
188}  // end sys_kill()
189
Note: See TracBrowser for help on using the repository browser.