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

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

Complete restructuration of kernel locks.

File size: 6.8 KB
RevLine 
[23]1/*
[416]2 * sys_kill.c - Kernel function implementing the "kill" system call.
[23]3 *
[440]4 * Author    Alain Greiner (2016,2017,2018)
[23]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>
[457]25#include <hal_kernel_types.h>
[409]26#include <hal_irqmask.h>
[23]27#include <errno.h>
28#include <thread.h>
29#include <printk.h>
30#include <process.h>
[416]31#include <shared_syscalls.h>
[23]32#include <cluster.h>
33#include <rpc.h>
34
[506]35#include <syscalls.h>
36
[23]37///////////////////////////
38int sys_kill( pid_t    pid,
39              uint32_t sig_id )
40{
[446]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
[409]51
[416]52    thread_t  * this    = CURRENT_THREAD;
[435]53    process_t * process = this->process;
[416]54
[438]55#if DEBUG_SYS_KILL
[409]56uint64_t    tm_start;
57uint64_t    tm_end;
58tm_start = hal_get_cycles();
[438]59if( DEBUG_SYS_KILL < tm_start )
[433]60printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cycle %d\n",
61__FUNCTION__ , this, pid, sig_id, (uint32_t)tm_start );
[409]62#endif
63
[436]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   
[446]69#if (DEBUG_SYS_KILL & 1)
70if( DEBUG_SYS_KILL < tm_start )
[448]71printk("\n[DBG] %s : thread %x get owner process %x in cluster %x\n",
[446]72__FUNCTION__ , this, owner_ptr, owner_cxy );
73#endif
74
[436]75    // check process found
76    if( owner_xp == XPTR_NULL)
[435]77    {
78
[438]79#if DEBUG_SYSCALLS_ERROR
[436]80printk("\n[ERROR] in %s : process %x not found\n", __FUNCTION__, pid );
[435]81#endif
82        this->errno = EINVAL;
83        return -1;
84    }
85
[440]86    // process cannot kill itself
87    if( (pid == process->pid) ) 
[23]88    {
[433]89
[438]90#if DEBUG_SYSCALLS_ERROR
[440]91printk("\n[ERROR] in %s : process %x cannot kill itself\n", __FUNCTION__, pid );
[435]92#endif
[421]93        this->errno = EINVAL;
94        return -1;
95    }
[436]96
[440]97    // processe INIT cannot be killed
[435]98    if( pid == 1 )
[421]99    {
[23]100
[438]101#if DEBUG_SYSCALLS_ERROR
[435]102printk("\n[ERROR] in %s : process_init cannot be killed\n", __FUNCTION__ );
103#endif
[409]104                this->errno = EINVAL;
105        return -1;
106    }
[23]107
[446]108    // get parent process descriptor pointers
[566]109    parent_xp  = (xptr_t)hal_remote_l64( XPTR( owner_cxy , &owner_ptr->parent_xp ) );
[446]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
[435]123    // analyse signal type / supported values are : 0, SIGSTOP, SIGCONT, SIGKILL
[433]124    switch( sig_id )
[23]125    {
[436]126        case 0 :          // does nothing
[433]127        {
128            retval = 0;
129            break;
130        }
[436]131        case SIGSTOP:     // block all target process threads
[433]132        {
[436]133            // transfer TXT ownership
134            process_txt_transfer_ownership( owner_xp );
[433]135
[436]136            // block all threads in all clusters, but the main thread
[435]137            process_sigaction( pid , BLOCK_ALL_THREADS );
[433]138
[446]139            // block the main thread
[436]140            xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
141            thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
142
[435]143            // atomically update owner process termination state
[433]144            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
[435]145                                  PROCESS_TERM_STOP );
[446]146
[566]147            // unblock the parent process main thread
[446]148            thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
149
[433]150            retval = 0;
151            break;
152        }
[436]153        case SIGCONT:     // unblock all target process threads
[433]154        {
155            // unblock all threads in all clusters
[435]156            process_sigaction( pid , UNBLOCK_ALL_THREADS );
[433]157
[436]158            // atomically update owner process termination state
[433]159            hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) ,
[435]160                                   ~PROCESS_TERM_STOP );
[433]161            retval = 0;
162            break;
163        }
164        break;
165        case SIGKILL:
166        {
[436]167            // remove process from TXT list
168            process_txt_detach( owner_xp );
[433]169
[446]170            // mark for delete all threads in all clusters, but the main
171            hal_enable_irq( &save_sr );
[435]172            process_sigaction( pid , DELETE_ALL_THREADS );
[446]173            hal_restore_irq( save_sr );
[435]174
[446]175            // block main thread
[436]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
[435]181            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
182                                  PROCESS_TERM_KILL );
[446]183
[566]184            // unblock the parent process main thread
[446]185            thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
186
[433]187            retval = 0;
188            break;
189        }
190        default:
191        {
192
[438]193#if DEBUG_SYSCALLS_ERROR
[435]194printk("\n[ERROR] in %s : illegal signal %d / process %x\n", __FUNCTION__, sig_id, pid );
195#endif
[433]196            this->errno = EINVAL;
197            retval = -1;
198            break;
199        }
[23]200    }
[433]201   
[124]202    hal_fence();
[23]203
[438]204#if DEBUG_SYS_KILL
[409]205tm_end = hal_get_cycles();
[438]206if( DEBUG_SYS_KILL < tm_end )
[436]207printk("\n[DBG] %s : thread %x exit / process %x / sig %d / cost = %d / cycle %d\n",
[433]208__FUNCTION__ , this, pid, sig_id, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
[409]209#endif
[23]210
[433]211        return retval;
212
[23]213}  // end sys_kill()
214
Note: See TracBrowser for help on using the repository browser.