source: trunk/kernel/syscalls/sys_fork.c @ 558

Last change on this file since 558 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.4 KB
Line 
1/*
2 * sys_fork.c - Kernel function implementing the "fork" system call.
3 *
4 * Authors  Alain Greiner  (2016,2017)
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_context.h>
27#include <hal_switch.h>
28#include <hal_atomic.h>
29#include <errno.h>
30#include <printk.h>
31#include <core.h>
32#include <cluster.h>
33#include <list.h>
34#include <thread.h>
35#include <scheduler.h>
36#include <kmem.h>
37#include <dqdt.h>
38#include <process.h>
39
40#include <syscalls.h>
41
42//////////////
43int sys_fork( void )
44{
45        process_t       * parent_process_ptr;   // pointer on local parent process descriptor
46    xptr_t            parent_thread_xp;     // extended pointer on parent thread descriptor
47    pid_t             parent_pid;           // parent process identifier
48    thread_t        * parent_thread_ptr;    // local pointer on local parent thread descriptor
49    cxy_t             parent_cxy;           // parent thread cluster
50
51    pid_t             child_pid;            // child process identifier
52    thread_t        * child_thread_ptr;     // local pointer on remote child thread descriptor
53    cxy_t             child_cxy;            // target cluster for forked child process
54 
55    xptr_t            ref_process_xp;       // extended pointer on reference parent process
56    cxy_t             ref_process_cxy;      // cluster of reference parent process
57    process_t       * ref_process_ptr;      // local pointer on reference parent process
58
59        error_t           error;
60   
61
62    // get pointers on local parent process and thread
63        parent_thread_ptr  = CURRENT_THREAD;
64    parent_thread_xp   = XPTR( local_cxy , parent_thread_ptr );
65        parent_process_ptr = parent_thread_ptr->process;
66    parent_pid         = parent_process_ptr->pid;
67    parent_cxy         = local_cxy;
68
69#if DEBUG_SYS_FORK
70uint64_t          tm_start;
71uint64_t          tm_end;
72tm_start = hal_get_cycles();
73if( DEBUG_SYS_FORK < tm_start )
74printk("\n[DBG] %s : thread %x in process %x enter / cycle =  %d\n",
75__FUNCTION__, parent_thread_ptr->trdid, parent_pid, (uint32_t)tm_start );
76#endif
77
78    // get infos on reference parent process
79    ref_process_xp  = parent_process_ptr->ref_xp;
80    ref_process_cxy = GET_CXY( ref_process_xp );
81    ref_process_ptr = GET_PTR( ref_process_xp );
82
83    // check parent process children number from reference
84    xptr_t   children_xp = XPTR( ref_process_cxy , &ref_process_ptr->children_nr );
85    if( hal_remote_atomic_add( children_xp , 1 ) >= CONFIG_PROCESS_MAX_CHILDREN )
86        {
87
88#if DEBUG_SYSCALLS_ERROR
89printk("\n[ERROR] in %s : too much children processes\n", __FUNCTION__);
90#endif
91            hal_remote_atomic_add ( children_xp , -1 );
92        parent_thread_ptr->errno = EAGAIN;
93        return -1;
94        }
95
96    // Select target cluster for child process and main thread.
97    // If placement is not user-defined, it is defined by the DQDT.
98        if( parent_thread_ptr->fork_user )
99        {
100        child_cxy = parent_thread_ptr->fork_cxy;
101        parent_thread_ptr->fork_user = false;
102        }
103        else                                  // DQDT placement
104        {
105                child_cxy = dqdt_get_cluster_for_process();
106        }
107
108#if (DEBUG_SYS_FORK & 1 )
109if( DEBUG_SYS_FORK < tm_start )
110printk("\n[DBG] %s : thread %x in process %x selected cluster %x\n",
111__FUNCTION__, parent_thread_ptr->trdid, parent_pid, child_cxy );
112#endif
113
114    // call process_make_fork in target cluster
115    if( child_cxy == local_cxy )
116    {
117        error = process_make_fork( ref_process_xp,
118                                   parent_thread_xp,
119                                   &child_pid,
120                                   &child_thread_ptr );
121    }
122    else
123    {
124        rpc_process_make_fork_client( child_cxy,
125                                      ref_process_xp,
126                                      parent_thread_xp,
127                                      &child_pid,
128                                      &child_thread_ptr,
129                                      &error );
130    }
131
132    if( error )
133    {
134
135#if DEBUG_SYSCALLS_ERROR
136printk("\n[ERROR] in %s : cannot fork process %x in cluster %x\n",
137__FUNCTION__, parent_pid, local_cxy );
138#endif
139        parent_thread_ptr->errno = EAGAIN;
140        return -1;
141    }
142
143    // set remote child FPU_context from parent_thread register values
144    // only when the parent thread is the FPU owner
145        if( CURRENT_THREAD->core->fpu_owner == parent_thread_ptr )
146        {
147                hal_fpu_context_save( XPTR( child_cxy , child_thread_ptr ) );
148        }
149
150    // set remote child CPU context from parent_thread register values
151    hal_cpu_context_fork( XPTR( child_cxy , child_thread_ptr ) );
152
153    // From this point, both parent and child threads execute the following code,
154    // but they can be distinguished by the (CURRENT_THREAD,local_cxy) values.
155    // - parent unblock child, and return child PID to user application.
156    // - child thread does nothing, and return 0 to user pplication
157    // The child thread will only execute it when it is unblocked by parent thread.
158
159    thread_t * current = CURRENT_THREAD;
160
161    if( (current == parent_thread_ptr) && (local_cxy == parent_cxy) )   // parent thread
162    {
163        // parent_thread unblock child_thread
164        thread_unblock( XPTR( child_cxy , child_thread_ptr ) , THREAD_BLOCKED_GLOBAL );
165
166#if DEBUG_SYS_FORK
167tm_end = hal_get_cycles();
168if( DEBUG_SYS_FORK < tm_end )
169printk("\n[DBG] %s : process %x exit / cost = %d / cycle %d\n",
170__FUNCTION__, parent_pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
171#endif
172        return child_pid;
173    }
174        else                                                               // child_thread
175    {
176
177#if DEBUG_SYS_FORK
178tm_end = hal_get_cycles();
179if( DEBUG_SYS_FORK < tm_end )
180printk("\n[DBG] %s : process %x exit / cost = %d / cycle %d\n",
181__FUNCTION__, child_pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
182#endif
183        return 0;
184    }
185
186}  // end sys_fork()
187
Note: See TracBrowser for help on using the repository browser.