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

Last change on this file since 574 was 566, checked in by alain, 6 years ago

Complete restructuration of kernel locks.

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