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

Last change on this file since 615 was 584, checked in by alain, 6 years ago

Introduce sys_place_fork() function.

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