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