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

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

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

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