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

Last change on this file since 653 was 651, checked in by alain, 5 years ago

1) Improve the VMM MMAP allocator: implement the "buddy" algorithm
to allocate only aligned blocks.
2) fix a bug in the pthread_join() / pthread_exit() mmechanism.

File size: 5.5 KB
RevLine 
[410]1/*
[416]2 * sys_exit.c - Kernel function implementing the "exit" system call.
[410]3 *
[625]4 * Author    Alain Greiner (2016,2017,2018,2019)
[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    xptr_t      owner_xp;          // extended pointer on owner process
41    cxy_t       owner_cxy;         // owner process cluster
42    process_t * owner_ptr;         // local pointer on owner process
43    thread_t  * main_ptr;          // local pointer on process main thread
44    xptr_t      parent_xp;         // extended pointer on parent process
45    cxy_t       parent_cxy;        // parent process cluster
46    process_t * parent_ptr;        // local pointer on parent process
47    thread_t  * parent_main_ptr;   // local pointer on parent main thread
48    xptr_t      parent_main_xp;    // extended pointer on parent main thread
49    uint32_t    term_state;        // termination status for owner process
50
[433]51    thread_t  * this    = CURRENT_THREAD;
52    process_t * process = this->process;
53    pid_t       pid     = process->pid;
[416]54
[625]55#if (DEBUG_SYS_EXIT || CONFIG_INSTRUMENTATION_SYSCALLS)
56uint64_t     tm_start = hal_get_cycles();
57#endif
58
[438]59#if DEBUG_SYS_EXIT
60if( DEBUG_SYS_EXIT < tm_start )
[619]61printk("\n[%s] thread[%x,%x] enter / status %x / cycle %d\n",
[625]62__FUNCTION__, pid, this->trdid , status , (uint32_t)tm_start );
[410]63#endif
64
[443]65    // get owner process descriptor pointers
[446]66    owner_xp  = process->owner_xp;
67    owner_cxy = GET_CXY( owner_xp );
68    owner_ptr = GET_PTR( owner_xp );
[410]69
[566]70    // get local pointer on the main thread
[446]71    main_ptr  = hal_remote_lpt( XPTR( owner_cxy , &owner_ptr->th_tbl[0] ) );
[410]72
[446]73    // get parent process descriptor pointers
74    parent_xp  = process->parent_xp;
75    parent_cxy = GET_CXY( parent_xp );
76    parent_ptr = GET_PTR( parent_xp );
77
78    // get pointers on the parent process main thread
79    parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->th_tbl[0] ) ); 
80    parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
81
82    // remove process from TXT list
83    process_txt_detach( owner_xp );
84
85#if( DEBUG_SYS_EXIT & 1)
[457]86if( DEBUG_SYS_EXIT < tm_start )
[625]87printk("\n[%s] thread[%x,%x] detached process %x from TXT\n",
88__FUNCTION__, pid, this->trdid, pid );
[446]89#endif
90
[443]91    // mark for delete all process threads in all clusters,
92    // but the main thread and this calling thread
[625]93    process_sigaction( pid , DELETE_ALL_THREADS );
[440]94
[438]95#if( DEBUG_SYS_EXIT & 1)
[457]96if( DEBUG_SYS_EXIT < tm_start )
[625]97printk("\n[%s] thread[%x,%x] deleted all threads in process %x (but itself)\n",
98__FUNCTION__, pid, this->trdid, pid );
[436]99#endif
[435]100
[566]101    // mark for delete the calling thread when it is not the main
[446]102    if( (owner_cxy != local_cxy) || (main_ptr != this) )
[440]103    {
[435]104
[438]105#if( DEBUG_SYS_EXIT & 1)
[440]106if( tm_start > DEBUG_SYS_EXIT )
[625]107printk("\n[%s] thread[%x,%x] marked iself for delete\n",
108__FUNCTION__, pid, this->trdid );
[436]109#endif
[651]110        thread_delete( XPTR( local_cxy , this ) , true );    // forced
[440]111    }
[445]112
[625]113    // block the main thread
[446]114    thread_block( XPTR( owner_cxy , main_ptr ) , THREAD_BLOCKED_GLOBAL );
[436]115
[438]116#if( DEBUG_SYS_EXIT & 1)
[625]117trdid_t main_trdid = hal_remote_l32( XPTR( owner_cxy , &main_ptr->trdid ) );
[440]118if( tm_start > DEBUG_SYS_EXIT )
[625]119printk("\n[%s] thread[%x,%x] blocked main thread[%x,%x]\n",
120__FUNCTION__, pid, this->trdid, pid, main_trdid );
[436]121#endif
122
[625]123    // update term_state in owner process descriptor to ask
124    // the parent process sys_wait() function to delete the process
[446]125    term_state = (status & 0xFF) | PROCESS_TERM_EXIT;
126    hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , term_state );
[410]127
[438]128#if( DEBUG_SYS_EXIT & 1)
[440]129if( tm_start > DEBUG_SYS_EXIT )
[619]130printk("\n[%s] thread[%x,%x] set exit status %x in owner process\n",
[625]131__FUNCTION__, pid, this->trdid, term_state );
[436]132#endif
133
[457]134    // unblock the parent process main thread
[446]135    thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
[436]136
[438]137#if( DEBUG_SYS_EXIT & 1)
[440]138if( tm_start > DEBUG_SYS_EXIT )
[619]139printk("\n[%s] thread[%x,%x] unblocked parent main thread in process %x\n",
[625]140__FUNCTION__ , pid, this->trdid,
[566]141hal_remote_l32( XPTR( parent_cxy , &parent_ptr->pid) ) );
[436]142#endif
143
[410]144    hal_fence();
145
[625]146#if (DEBUG_SYS_EXIT || CONFIG_INSTRUMENTATION_SYSCALLS)
147uint64_t     tm_end = hal_get_cycles();
148#endif
149
[438]150#if DEBUG_SYS_EXIT
151if( DEBUG_SYS_EXIT < tm_end )
[625]152printk("\n[%s] thread[%x,%x] exit / term_state %x / cycle %d\n",
153__FUNCTION__, pid, this->trdid, term_state,  (uint32_t)tm_end );
[410]154#endif
155
[625]156#if CONFIG_INSTRUMENTATION_SYSCALLS
157hal_atomic_add( &syscalls_cumul_cost[SYS_EXIT] , tm_end - tm_start );
158hal_atomic_add( &syscalls_occurences[SYS_EXIT] , 1 );
159#endif
160
[440]161    // this thread deschedule
[436]162    sched_yield( "process exit" );
163
[410]164        return 0;
165
166}  // end sys_exit()
167
Note: See TracBrowser for help on using the repository browser.