source: trunk/kernel/syscalls/sys_wait.c @ 635

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

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

File size: 6.6 KB
Line 
1/*
2 * sys_wait.c - wait termination or blocking of a child process.
3 *
4 * Author    Alain Greiner (2016,2017,2018,2019)
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 <hal_kernel_types.h>
25#include <hal_uspace.h>
26#include <hal_irqmask.h>
27#include <hal_vmm.h>
28#include <remote_queuelock.h>
29#include <core.h>
30#include <thread.h>
31#include <process.h>
32#include <vmm.h>
33#include <printk.h>
34
35#include <syscalls.h>
36
37/////////////////////////////////
38int sys_wait( uint32_t * status )
39{
40        error_t     error;
41    vseg_t    * vseg;
42    xptr_t      iter_xp;
43    xptr_t      child_xp;
44    process_t * child_ptr;
45    cxy_t       child_cxy;
46    pid_t       child_pid;
47    uint32_t    child_state;
48    thread_t  * child_thread;
49    reg_t       save_sr;
50
51    thread_t  * this    = CURRENT_THREAD;
52    process_t * process = this->process;
53    pid_t       pid     = process->pid;
54
55#if DEBUG_SYS_WAIT
56uint64_t    cycle = hal_get_cycles();
57if( DEBUG_SYS_WAIT < cycle )
58printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
59__FUNCTION__, pid, this->trdid, (uint32_t)cycle );
60#endif
61
62    // check status in user space
63    error = vmm_get_vseg( process, (intptr_t)status , &vseg );
64
65        if( error )
66        {
67
68#if DEBUG_SYSCALLS_ERROR
69printk("\n[ERROR] in %s : status buffer %x unmapped for thread[%x,%x]\n",
70__FUNCTION__ , (intptr_t)status, pid, this->trdid );
71#endif
72        this->errno = EINVAL;
73                return -1;
74        }
75
76    // get calling process owner cluster
77    cxy_t   owner_cxy = CXY_FROM_PID( pid );
78
79    // get calling thread trdid
80    trdid_t trdid = this->trdid;
81
82    // this function must be executed by the process main thread
83    if( (owner_cxy != local_cxy) || (LTID_FROM_TRDID(trdid) != 0) )
84    {
85
86#if DEBUG_SYSCALLS_ERROR
87printk("\n[ERROR] in %s : calling thread[%x,%x] is not thread 0 in owner cluster %x\n",
88__FUNCTION__ , pid, this->trdid , owner_cxy );
89#endif
90        this->errno = EINVAL;
91                return -1;
92        }
93
94    // get extended pointer on children list root and lock
95    xptr_t children_root_xp = XPTR( owner_cxy , &process->children_root );
96    xptr_t children_lock_xp = XPTR( owner_cxy , &process->children_lock );
97
98    // exit this loop only when a child processes change state
99    while( 1 )
100    {
101        // enable IRQS
102        hal_enable_irq( &save_sr );
103 
104        // get lock protecting children list
105        remote_queuelock_acquire( children_lock_xp );
106
107        // scan the list of child process
108        XLIST_FOREACH( children_root_xp , iter_xp )
109        {
110            // get child process owner cluster and local pointer
111            child_xp  = XLIST_ELEMENT( iter_xp , process_t , children_list );
112            child_ptr = GET_PTR( child_xp );
113            child_cxy = GET_CXY( child_xp );
114
115            // get PID, term_state, and main thread from child process
116            child_pid    = hal_remote_l32 (XPTR( child_cxy , &child_ptr->pid ));
117            child_state  = hal_remote_l32 ( XPTR(child_cxy , &child_ptr->term_state ) );
118            child_thread = hal_remote_lpt(XPTR( child_cxy , &child_ptr->th_tbl[0] ));
119
120            // test if this child process is terminated,
121            // but termination not yet reported to parent process
122            if( ((child_state & PROCESS_TERM_EXIT)  || 
123                 (child_state & PROCESS_TERM_KILL)  ||
124                 (child_state & PROCESS_TERM_STOP)) &&
125                 ((child_state & PROCESS_TERM_WAIT) == 0) )
126            {
127                // set the PROCESS_TERM_WAIT in child process descriptor
128                hal_remote_atomic_or( XPTR( child_cxy , &child_ptr->term_state ),
129                                      PROCESS_TERM_WAIT );
130
131                // set the THREAD_FLAG_REQ_DELETE in main thread if kill or exit
132                if((child_state & PROCESS_TERM_EXIT) || (child_state & PROCESS_TERM_KILL))
133                hal_remote_atomic_or( XPTR( child_cxy , &child_thread->flags ) ,
134                                            THREAD_FLAG_REQ_DELETE );
135
136                // release lock protecting children list
137                remote_queuelock_release( children_lock_xp );
138
139#if DEBUG_SYS_WAIT
140cycle = hal_get_cycles();
141if( DEBUG_SYS_WAIT < cycle )
142{
143    if( child_state & PROCESS_TERM_EXIT )
144        printk("\n[%s] thread[%x,%x] exit : child process %x terminated / cycle %d\n",
145        __FUNCTION__, pid, this->trdid, child_pid, (uint32_t)cycle );
146    if( child_state & PROCESS_TERM_KILL )
147        printk("\n[%s] thread[%x,%x] exit : child process %x killed / cycle %d\n",
148        __FUNCTION__, pid, this->trdid, child_pid, (uint32_t)cycle );
149    if( child_state & PROCESS_TERM_STOP )
150        printk("\n[%s] thread[%x,%x] exit : child process %x stopped / cycle %d\n",
151        __FUNCTION__, pid, this->trdid, child_pid, (uint32_t)cycle );
152}
153#endif
154                 // return child termination state  to parent process
155                 hal_copy_to_uspace( local_cxy,
156                                     &child_state,
157                                     status,
158                                     sizeof(int) );
159                 return child_pid;
160            }
161        }  // end loop on children
162       
163        // we execute this code when no child change detected
164        // - release the lock protecting children list,
165        // - block on the WAIT condition
166        // - deschedule to keep waiting in the while loop
167
168        // release lock protecting children list
169        remote_queuelock_release( children_lock_xp );
170
171        // block on WAIT condition
172        thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_WAIT );
173
174#if (DEBUG_SYS_WAIT & 1)
175cycle = hal_get_cycles();
176if( DEBUG_SYS_WAIT < cycle )
177printk("\n[%s] thread[%x,%x] block & deschedule / cycle %d\n",
178__FUNCTION__, pid, this->trdid, (uint32_t)cycle );
179#endif
180
181        // deschedule
182        sched_yield( "parent process wait children processes termination" );
183
184#if (DEBUG_SYS_WAIT & 1)
185cycle = hal_get_cycles();
186if( DEBUG_SYS_WAIT < cycle )
187printk("\n[%s] thread[%x,%x] resume / cycle %d\n",
188__FUNCTION__, pid, this->trdid, (uint32_t)cycle );
189#endif
190
191    }  // end while
192
193    // never executed
194        return -1; 
195
196}  // end sys_wait()
Note: See TracBrowser for help on using the repository browser.