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

Last change on this file since 423 was 416, checked in by alain, 7 years ago

Improve sys_exec.

File size: 6.0 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_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//////////////
41int sys_fork()
42{
43        process_t       * parent_process_ptr;   // pointer on local parent process descriptor
44    xptr_t            parent_thread_xp;     // extended pointer on parent thread descriptor
45    pid_t             parent_pid;           // parent process identifier
46    thread_t        * parent_thread_ptr;    // local pointer on local parent thread descriptor
47
48    pid_t             child_pid;            // child process identifier
49    thread_t        * child_thread_ptr;     // local pointer on remote child thread descriptor
50    cxy_t             target_cxy;           // target cluster for forked child process
51 
52    xptr_t            ref_process_xp;       // extended pointer on reference parent process
53    cxy_t             ref_process_cxy;      // cluster of reference parent process
54    process_t       * ref_process_ptr;      // local pointer on reference parent process
55
56        error_t           error;
57   
58
59    // get pointers on local parent process and thread
60        parent_thread_ptr  = CURRENT_THREAD;
61    parent_thread_xp   = XPTR( local_cxy , parent_thread_ptr );
62        parent_process_ptr = parent_thread_ptr->process;
63    parent_pid         = parent_process_ptr->pid;
64
65#if CONFIG_SYSCALL_DEBUG
66uint64_t          tm_start;
67uint64_t          tm_end;
68tm_start = hal_get_cycles();
69printk("\n[DBG] %s : core[%x,%d] enter / process %x / cycle =  %d\n",
70__FUNCTION__, local_cxy, parent_thread_ptr->core->lid, parent_pid, 
71(uint32_t)tm_start );
72#endif
73
74    // get infos on reference process
75    ref_process_xp  = parent_process_ptr->ref_xp;
76    ref_process_cxy = GET_CXY( ref_process_xp );
77    ref_process_ptr = (process_t *)GET_PTR( ref_process_xp );
78
79    // check parent process children number from reference
80    xptr_t   children_xp = XPTR( ref_process_cxy , &ref_process_ptr->children_nr );
81    if( hal_remote_atomic_add( children_xp , 1 ) >= CONFIG_PROCESS_MAX_CHILDREN )
82        {
83            printk("\n[ERROR] in %s : too much children processes\n", __FUNCTION__);
84            hal_remote_atomic_add ( children_xp , -1 );
85        parent_thread_ptr->errno = EAGAIN;
86        return -1;
87        }
88
89    // Select target cluster for child process and main thread.
90    // If placement is not user-defined, the placement is defined by the DQDT.
91        if( parent_thread_ptr->fork_user )    // user defined placement
92        {
93        target_cxy = parent_thread_ptr->fork_cxy;
94        parent_thread_ptr->fork_user = false;
95        }
96        else                                  // DQDT placement
97        {
98                target_cxy = dqdt_get_cluster_for_process();
99        }
100
101    // call process_make_fork in target cluster
102    if( target_cxy == local_cxy )
103    {
104        error = process_make_fork( ref_process_xp,
105                                   parent_thread_xp,
106                                   &child_pid,
107                                   &child_thread_ptr );
108    }
109    else
110    {
111        rpc_process_make_fork_client( target_cxy,
112                                      ref_process_xp,
113                                      parent_thread_xp,
114                                      &child_pid,
115                                      &child_thread_ptr,
116                                      &error );
117    }
118
119    if( error )
120    {
121        printk("\n[ERROR] in %s : cannot fork process %x in cluster %x\n",
122        __FUNCTION__, parent_pid, local_cxy );
123        parent_thread_ptr->errno = EAGAIN;
124        return -1;
125    }
126
127    // set remote child FPU_context from parent_thread register values
128    // only when the parent thread is the FPU owner
129        if( CURRENT_THREAD->core->fpu_owner == parent_thread_ptr )
130        {
131                hal_fpu_context_save( XPTR( target_cxy , child_thread_ptr ) );
132        }
133
134    // set remote child CPU context from  parent_thread register values
135    hal_cpu_context_fork( XPTR( target_cxy , child_thread_ptr ) );
136
137    // From this point, both parent and child threads execute the following code.
138    // They can be distinguished by the CURRENT_THREAD value, and child will only
139    // execute it when it is unblocked by parent.
140    // - parent unblock child, and return child PID to user application.
141    // - child thread does nothing, and return 0 to user pplication
142
143    thread_t * current = CURRENT_THREAD;
144
145    if( current == parent_thread_ptr )    // current == parent thread
146    {
147        // parent_thread unblock child_thread
148        thread_unblock( XPTR( target_cxy , child_thread_ptr ) , THREAD_BLOCKED_GLOBAL );
149
150#if CONFIG_SYSCALL_DEBUG
151tm_end = hal_get_cycles();
152printk("\n[DBG] %s : core[%x,%d] parent_process %x exit / cost = %d\n",
153__FUNCTION__, local_cxy, parent_thread_ptr->core->lid,  parent_pid, 
154(uint32_t)(tm_end - tm_start) );
155#endif
156
157        return child_pid;
158    }
159        else                                   // current == child_thread
160    {
161
162#if CONFIG_SYSCALL_DEBUG
163tm_end = hal_get_cycles();
164printk("\n[DBG] %s : core[%x,%d] child process %x exit / cost =  %d\n",
165__FUNCTION__, local_cxy, parent_thread_ptr->core->lid, child_pid, 
166(uint32_t)(tm_end - tm_start) );
167#endif
168
169        return 0;
170    }
171
172}  // end sys_fork()
173
Note: See TracBrowser for help on using the repository browser.