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

Last change on this file since 461 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
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
35///////////////////////////
36int sys_kill( pid_t    pid,
37              uint32_t sig_id )
38{
[446]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
[409]50
[416]51    thread_t  * this    = CURRENT_THREAD;
[435]52    process_t * process = this->process;
[416]53
[438]54#if DEBUG_SYS_KILL
[409]55uint64_t    tm_start;
56uint64_t    tm_end;
57tm_start = hal_get_cycles();
[438]58if( DEBUG_SYS_KILL < tm_start )
[433]59printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cycle %d\n",
60__FUNCTION__ , this, pid, sig_id, (uint32_t)tm_start );
[409]61#endif
62
[436]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   
[446]68#if (DEBUG_SYS_KILL & 1)
69if( DEBUG_SYS_KILL < tm_start )
[448]70printk("\n[DBG] %s : thread %x get owner process %x in cluster %x\n",
[446]71__FUNCTION__ , this, owner_ptr, owner_cxy );
72#endif
73
[436]74    // check process found
75    if( owner_xp == XPTR_NULL)
[435]76    {
77
[438]78#if DEBUG_SYSCALLS_ERROR
[436]79printk("\n[ERROR] in %s : process %x not found\n", __FUNCTION__, pid );
[435]80#endif
81        this->errno = EINVAL;
82        return -1;
83    }
84
[440]85    // process cannot kill itself
86    if( (pid == process->pid) ) 
[23]87    {
[433]88
[438]89#if DEBUG_SYSCALLS_ERROR
[440]90printk("\n[ERROR] in %s : process %x cannot kill itself\n", __FUNCTION__, pid );
[435]91#endif
[421]92        this->errno = EINVAL;
93        return -1;
94    }
[436]95
[440]96    // processe INIT cannot be killed
[435]97    if( pid == 1 )
[421]98    {
[23]99
[438]100#if DEBUG_SYSCALLS_ERROR
[435]101printk("\n[ERROR] in %s : process_init cannot be killed\n", __FUNCTION__ );
102#endif
[409]103                this->errno = EINVAL;
104        return -1;
105    }
[23]106
[446]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
[435]125    // analyse signal type / supported values are : 0, SIGSTOP, SIGCONT, SIGKILL
[433]126    switch( sig_id )
[23]127    {
[436]128        case 0 :          // does nothing
[433]129        {
130            retval = 0;
131            break;
132        }
[436]133        case SIGSTOP:     // block all target process threads
[433]134        {
[436]135            // transfer TXT ownership
136            process_txt_transfer_ownership( owner_xp );
[433]137
[436]138            // block all threads in all clusters, but the main thread
[435]139            process_sigaction( pid , BLOCK_ALL_THREADS );
[433]140
[446]141            // block the main thread
[436]142            xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
143            thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
144
[435]145            // atomically update owner process termination state
[433]146            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
[435]147                                  PROCESS_TERM_STOP );
[446]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
[433]154            retval = 0;
155            break;
156        }
[436]157        case SIGCONT:     // unblock all target process threads
[433]158        {
159            // unblock all threads in all clusters
[435]160            process_sigaction( pid , UNBLOCK_ALL_THREADS );
[433]161
[436]162            // atomically update owner process termination state
[433]163            hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) ,
[435]164                                   ~PROCESS_TERM_STOP );
[433]165            retval = 0;
166            break;
167        }
168        break;
169        case SIGKILL:
170        {
[436]171            // remove process from TXT list
172            process_txt_detach( owner_xp );
[433]173
[446]174            // mark for delete all threads in all clusters, but the main
175            hal_enable_irq( &save_sr );
[435]176            process_sigaction( pid , DELETE_ALL_THREADS );
[446]177            hal_restore_irq( save_sr );
[435]178
[446]179            // block main thread
[436]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
[435]185            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
186                                  PROCESS_TERM_KILL );
[446]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
[433]193            retval = 0;
194            break;
195        }
196        default:
197        {
198
[438]199#if DEBUG_SYSCALLS_ERROR
[435]200printk("\n[ERROR] in %s : illegal signal %d / process %x\n", __FUNCTION__, sig_id, pid );
201#endif
[433]202            this->errno = EINVAL;
203            retval = -1;
204            break;
205        }
[23]206    }
[433]207   
[124]208    hal_fence();
[23]209
[438]210#if DEBUG_SYS_KILL
[409]211tm_end = hal_get_cycles();
[438]212if( DEBUG_SYS_KILL < tm_end )
[436]213printk("\n[DBG] %s : thread %x exit / process %x / sig %d / cost = %d / cycle %d\n",
[433]214__FUNCTION__ , this, pid, sig_id, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
[409]215#endif
[23]216
[433]217        return retval;
218
[23]219}  // end sys_kill()
220
Note: See TracBrowser for help on using the repository browser.