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

Last change on this file since 603 was 594, checked in by alain, 6 years ago

Fix various bugs in sys_stat() and sys_mmap() functions.
Improve debug in other functions.

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