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

Last change on this file since 526 was 506, checked in by viala@…, 6 years ago

[syscalls] add interface in implementation.

Add const where possible, fix protoypes to follow interface.

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