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
RevLine 
[23]1/*
[416]2 * sys_kill.c - Kernel function implementing the "kill" system call.
[23]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>
[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{
[409]39    uint32_t    save_sr;       // required to enable IRQs
[433]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
[421]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
[433]47    uint32_t    retval;        // return value for the switch
[409]48
[416]49    thread_t  * this    = CURRENT_THREAD;
[435]50    process_t * process = this->process;
[416]51
[433]52#if CONFIG_DEBUG_SYS_KILL
[409]53uint64_t    tm_start;
54uint64_t    tm_end;
55tm_start = hal_get_cycles();
[433]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 );
[409]59#endif
60
[435]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
[433]73    owner_xp  = cluster_get_owner_process_from_pid( pid );
74    owner_cxy = GET_CXY( owner_xp );
75    owner_ptr = GET_PTR( owner_xp );
[23]76
[421]77    // check process existence
[433]78    if( owner_xp == XPTR_NULL )
[23]79    {
[433]80
[435]81#if CONFIG_DEBUG_SYSCALLS_ERROR
82printk("\n[ERROR] in %s : process %x not found\n", __FUNCTION__ , pid );
83#endif
[421]84        this->errno = EINVAL;
85        return -1;
86    }
87   
88    // get parent process PID
[433]89    parent_xp  = hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) );
[421]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
[435]94    // processes INIT
95    if( pid == 1 )
[421]96    {
[23]97
[435]98#if CONFIG_DEBUG_SYSCALLS_ERROR
99printk("\n[ERROR] in %s : process_init cannot be killed\n", __FUNCTION__ );
100#endif
[409]101                this->errno = EINVAL;
102        return -1;
103    }
[23]104
[409]105    // enable IRQs
106    hal_enable_irq( &save_sr );
[23]107
[435]108    // analyse signal type / supported values are : 0, SIGSTOP, SIGCONT, SIGKILL
[433]109    switch( sig_id )
[23]110    {
[433]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
[435]123            process_sigaction( pid , BLOCK_ALL_THREADS );
[433]124
[435]125            // atomically update owner process termination state
[433]126            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
[435]127                                  PROCESS_TERM_STOP );
[433]128 
129            retval = 0;
130            break;
131        }
132        case SIGCONT:
133        {
134            // unblock all threads in all clusters
[435]135            process_sigaction( pid , UNBLOCK_ALL_THREADS );
[433]136
137            // atomically update reference process termination state
138            hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) ,
[435]139                                   ~PROCESS_TERM_STOP );
[433]140            retval = 0;
141            break;
142        }
143        break;
144        case SIGKILL:
145        {
[435]146            // remove TXT ownership from owner process descriptor
147            process_txt_reset_ownership( owner_xp );
[433]148
[435]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
[433]159            retval = 0;
160            break;
161        }
162        default:
163        {
164
[435]165#if CONFIG_DEBUG_SYSCALLS_ERROR
166printk("\n[ERROR] in %s : illegal signal %d / process %x\n", __FUNCTION__, sig_id, pid );
167#endif
[433]168            this->errno = EINVAL;
169            retval = -1;
170            break;
171        }
[23]172    }
[433]173   
[409]174    // restore IRQs
175    hal_restore_irq( save_sr );
[23]176
[124]177    hal_fence();
[23]178
[433]179#if CONFIG_DEBUG_SYS_KILL
[409]180tm_end = hal_get_cycles();
[433]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 );
[409]184#endif
[23]185
[433]186        return retval;
187
[23]188}  // end sys_kill()
189
Note: See TracBrowser for help on using the repository browser.