source: trunk/kernel/syscalls/sys_exit.c @ 525

Last change on this file since 525 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: 6.2 KB
RevLine 
[410]1/*
[416]2 * sys_exit.c - Kernel function implementing the "exit" system call.
[410]3 *
[440]4 * Author    Alain Greiner (2016,2017,2018)
[410]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>
[410]26#include <hal_irqmask.h>
27#include <errno.h>
28#include <thread.h>
29#include <printk.h>
30#include <process.h>
[435]31#include <shared_syscalls.h>
[410]32#include <cluster.h>
33#include <rpc.h>
34
[506]35#include <syscalls.h>
36
[410]37///////////////////////////////
38int sys_exit( uint32_t status )
39{
[446]40    reg_t       save_sr;           // required to enable IRQs
[410]41
[446]42    xptr_t      owner_xp;          // extended pointer on owner process
43    cxy_t       owner_cxy;         // owner process cluster
44    process_t * owner_ptr;         // local pointer on owner process
45    thread_t  * main_ptr;          // local pointer on process main thread
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 locki
50    thread_t  * parent_main_ptr;   // local pointer on parent main thread
51    xptr_t      parent_main_xp;    // extended pointer on parent main thread
52    uint32_t    term_state;        // termination status for owner process
53
[433]54    thread_t  * this    = CURRENT_THREAD;
55    process_t * process = this->process;
56    pid_t       pid     = process->pid;
[416]57
[438]58#if DEBUG_SYS_EXIT
[410]59uint64_t    tm_start;
60uint64_t    tm_end;
61tm_start = hal_get_cycles();
[438]62if( DEBUG_SYS_EXIT < tm_start )
[457]63printk("\n[DBG] %s : thread %x in process %x enter / status %x / cycle %d\n",
64__FUNCTION__, this->trdid, process->pid , status , (uint32_t)tm_start );
[410]65#endif
66
[443]67    // get owner process descriptor pointers
[446]68    owner_xp  = process->owner_xp;
69    owner_cxy = GET_CXY( owner_xp );
70    owner_ptr = GET_PTR( owner_xp );
[410]71
[446]72#if (DEBUG_SYS_EXIT & 1)
73if( DEBUG_SYS_EXIT < tm_start )
[457]74printk("\n[DBG] %s : thread %x in process %x get owner process %x in cluster %x\n",
75__FUNCTION__, this->trdid, process->pid, owner_ptr, owner_cxy );
[446]76#endif
[433]77
[457]78    // get local pointer on the process main thread
[446]79    main_ptr  = hal_remote_lpt( XPTR( owner_cxy , &owner_ptr->th_tbl[0] ) );
[410]80
[446]81    // get parent process descriptor pointers
82    parent_xp  = process->parent_xp;
83    parent_cxy = GET_CXY( parent_xp );
84    parent_ptr = GET_PTR( parent_xp );
85
86#if (DEBUG_SYS_EXIT & 1)
87if( DEBUG_SYS_EXIT < tm_start )
[457]88printk("\n[DBG] %s : thread %x in process %x get parent process %x in cluster %x\n",
89__FUNCTION__, this->trdid, process->pid, parent_ptr, parent_cxy );
[446]90#endif
91
92    // get extended pointer on lock protecting children list in parent process
93    children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock ); 
94
95    // get pointers on the parent process main thread
96    parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->th_tbl[0] ) ); 
97    parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
98
99    // remove process from TXT list
100    process_txt_detach( owner_xp );
101
102#if( DEBUG_SYS_EXIT & 1)
[457]103if( DEBUG_SYS_EXIT < tm_start )
104printk("\n[DBG] %s : thread %x in process %x detached process from TXT\n",
105__FUNCTION__, this->trdid, process->pid );
[446]106#endif
107
[443]108    // mark for delete all process threads in all clusters,
109    // but the main thread and this calling thread
[446]110    hal_enable_irq( &save_sr );
[457]111    process_sigaction( process->pid , DELETE_ALL_THREADS );
[440]112    hal_restore_irq( save_sr );
113
[438]114#if( DEBUG_SYS_EXIT & 1)
[457]115if( DEBUG_SYS_EXIT < tm_start )
116printk("\n[DBG] %s : thread %x in process %x deleted all threads but itself\n",
117__FUNCTION__, this->trdid, process->pid );
[436]118#endif
[435]119
[440]120    // mark for delete this calling thread when it is not the main
[446]121    if( (owner_cxy != local_cxy) || (main_ptr != this) )
[440]122    {
[435]123
[438]124#if( DEBUG_SYS_EXIT & 1)
[440]125if( tm_start > DEBUG_SYS_EXIT )
[457]126printk("\n[DBG] %s : thread %x in process %x marked iself for delete\n",
127__FUNCTION__, this->trdid, process->pid );
[436]128#endif
[440]129        thread_delete( XPTR( local_cxy , this ) , pid , true );
130    }
[445]131
[446]132    // block this main thread
133    thread_block( XPTR( owner_cxy , main_ptr ) , THREAD_BLOCKED_GLOBAL );
[436]134
[438]135#if( DEBUG_SYS_EXIT & 1)
[440]136if( tm_start > DEBUG_SYS_EXIT )
[457]137printk("\n[DBG] %s : thread %x in process %x blocked main thread\n",
138__FUNCTION__, this->trdid, process->pid );
[436]139#endif
140
[446]141    // atomically update owner process descriptor term_state to ask
142    // the parent process sys_wait() function to delete the main thread
143    term_state = (status & 0xFF) | PROCESS_TERM_EXIT;
144    hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , term_state );
[410]145
[438]146#if( DEBUG_SYS_EXIT & 1)
[440]147if( tm_start > DEBUG_SYS_EXIT )
[457]148printk("\n[DBG] %s : thread %x in process %x set exit status %x in owner process\n",
149__FUNCTION__, this->trdid, process->pid, term_state );
[436]150#endif
151
[457]152    // unblock the parent process main thread
[446]153    remote_spinlock_lock( children_lock_xp );
154    thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
155    remote_spinlock_unlock( children_lock_xp );
[436]156
[438]157#if( DEBUG_SYS_EXIT & 1)
[440]158if( tm_start > DEBUG_SYS_EXIT )
[457]159printk("\n[DBG] %s : thread %x in process %x unblock parent main thread in process %x\n",
160__FUNCTION__ , this->trdid, process->pid,
161hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid) ) );
[436]162#endif
163
[410]164    hal_fence();
165
[438]166#if DEBUG_SYS_EXIT
[410]167tm_end = hal_get_cycles();
[438]168if( DEBUG_SYS_EXIT < tm_end )
[457]169printk("\n[DBG] %s : thread %x in process %x exit / status %x / cost = %d / cycle %d\n",
170__FUNCTION__, this->trdid, process->pid, status,
171(uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
[410]172#endif
173
[440]174    // this thread deschedule
[436]175    sched_yield( "process exit" );
176
[410]177        return 0;
178
179}  // end sys_exit()
180
Note: See TracBrowser for help on using the repository browser.