[1] | 1 | /* |
---|
[409] | 2 | * sys_fork.c - Kernel function implementing the "fork" system call. |
---|
[1] | 3 | * |
---|
[23] | 4 | * Authors Alain Greiner (2016,2017) |
---|
[1] | 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 | |
---|
[23] | 24 | #include <kernel_config.h> |
---|
| 25 | #include <hal_types.h> |
---|
[407] | 26 | #include <hal_context.h> |
---|
| 27 | #include <hal_switch.h> |
---|
[23] | 28 | #include <hal_atomic.h> |
---|
[1] | 29 | #include <errno.h> |
---|
[23] | 30 | #include <printk.h> |
---|
| 31 | #include <core.h> |
---|
[1] | 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 | |
---|
[23] | 40 | ////////////// |
---|
| 41 | int sys_fork() |
---|
[1] | 42 | { |
---|
[408] | 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 |
---|
[1] | 47 | |
---|
[408] | 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 |
---|
[407] | 55 | |
---|
[408] | 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; |
---|
[1] | 64 | |
---|
[433] | 65 | #if CONFIG_DEBUG_SYS_FORK |
---|
[416] | 66 | uint64_t tm_start; |
---|
| 67 | uint64_t tm_end; |
---|
| 68 | tm_start = hal_get_cycles(); |
---|
[433] | 69 | if( CONFIG_DEBUG_SYS_FORK < tm_start ) |
---|
| 70 | printk("\n[DBG] %s : thread %x enter / parent %x / cycle = %d\n", |
---|
| 71 | __FUNCTION__, parent_thread_ptr, parent_pid, (uint32_t)tm_start ); |
---|
[416] | 72 | #endif |
---|
[407] | 73 | |
---|
[408] | 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 ) |
---|
[1] | 82 | { |
---|
[435] | 83 | |
---|
| 84 | #if CONFIG_DEBUG_SYSCALLS_ERROR |
---|
| 85 | printk("\n[ERROR] in %s : too much children processes\n", __FUNCTION__); |
---|
| 86 | #endif |
---|
[408] | 87 | hal_remote_atomic_add ( children_xp , -1 ); |
---|
| 88 | parent_thread_ptr->errno = EAGAIN; |
---|
| 89 | return -1; |
---|
[1] | 90 | } |
---|
| 91 | |
---|
[408] | 92 | // Select target cluster for child process and main thread. |
---|
[23] | 93 | // If placement is not user-defined, the placement is defined by the DQDT. |
---|
[408] | 94 | if( parent_thread_ptr->fork_user ) // user defined placement |
---|
[1] | 95 | { |
---|
[408] | 96 | target_cxy = parent_thread_ptr->fork_cxy; |
---|
| 97 | parent_thread_ptr->fork_user = false; |
---|
[1] | 98 | } |
---|
[408] | 99 | else // DQDT placement |
---|
[1] | 100 | { |
---|
| 101 | target_cxy = dqdt_get_cluster_for_process(); |
---|
| 102 | } |
---|
| 103 | |
---|
[408] | 104 | // call process_make_fork in target cluster |
---|
| 105 | if( target_cxy == local_cxy ) |
---|
[1] | 106 | { |
---|
[408] | 107 | error = process_make_fork( ref_process_xp, |
---|
| 108 | parent_thread_xp, |
---|
| 109 | &child_pid, |
---|
| 110 | &child_thread_ptr ); |
---|
[1] | 111 | } |
---|
[408] | 112 | else |
---|
[1] | 113 | { |
---|
[408] | 114 | rpc_process_make_fork_client( target_cxy, |
---|
| 115 | ref_process_xp, |
---|
| 116 | parent_thread_xp, |
---|
| 117 | &child_pid, |
---|
| 118 | &child_thread_ptr, |
---|
| 119 | &error ); |
---|
[1] | 120 | } |
---|
[23] | 121 | |
---|
[1] | 122 | if( error ) |
---|
| 123 | { |
---|
[435] | 124 | |
---|
| 125 | #if CONFIG_DEBUG_SYSCALLS_ERROR |
---|
| 126 | printk("\n[ERROR] in %s : cannot fork process %x in cluster %x\n", |
---|
| 127 | __FUNCTION__, parent_pid, local_cxy ); |
---|
| 128 | #endif |
---|
[408] | 129 | parent_thread_ptr->errno = EAGAIN; |
---|
| 130 | return -1; |
---|
[1] | 131 | } |
---|
| 132 | |
---|
[408] | 133 | // set remote child FPU_context from parent_thread register values |
---|
| 134 | // only when the parent thread is the FPU owner |
---|
| 135 | if( CURRENT_THREAD->core->fpu_owner == parent_thread_ptr ) |
---|
[407] | 136 | { |
---|
[408] | 137 | hal_fpu_context_save( XPTR( target_cxy , child_thread_ptr ) ); |
---|
[407] | 138 | } |
---|
[1] | 139 | |
---|
[408] | 140 | // set remote child CPU context from parent_thread register values |
---|
| 141 | hal_cpu_context_fork( XPTR( target_cxy , child_thread_ptr ) ); |
---|
[1] | 142 | |
---|
[408] | 143 | // From this point, both parent and child threads execute the following code. |
---|
| 144 | // They can be distinguished by the CURRENT_THREAD value, and child will only |
---|
| 145 | // execute it when it is unblocked by parent. |
---|
| 146 | // - parent unblock child, and return child PID to user application. |
---|
| 147 | // - child thread does nothing, and return 0 to user pplication |
---|
[1] | 148 | |
---|
[407] | 149 | thread_t * current = CURRENT_THREAD; |
---|
[1] | 150 | |
---|
[408] | 151 | if( current == parent_thread_ptr ) // current == parent thread |
---|
[407] | 152 | { |
---|
| 153 | // parent_thread unblock child_thread |
---|
[408] | 154 | thread_unblock( XPTR( target_cxy , child_thread_ptr ) , THREAD_BLOCKED_GLOBAL ); |
---|
[1] | 155 | |
---|
[433] | 156 | #if CONFIG_DEBUG_SYS_FORK |
---|
[416] | 157 | tm_end = hal_get_cycles(); |
---|
[433] | 158 | if( CONFIG_DEBUG_SYS_FORK < tm_end ) |
---|
| 159 | printk("\n[DBG] %s : parent_thread %x exit / cost = %d / cycle %d\n", |
---|
| 160 | __FUNCTION__ , parent_thread_ptr, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
[416] | 161 | #endif |
---|
[1] | 162 | |
---|
[407] | 163 | return child_pid; |
---|
| 164 | } |
---|
[408] | 165 | else // current == child_thread |
---|
[407] | 166 | { |
---|
| 167 | |
---|
[433] | 168 | #if CONFIG_DEBUG_SYS_FORK |
---|
[416] | 169 | tm_end = hal_get_cycles(); |
---|
[433] | 170 | if( CONFIG_DEBUG_SYS_FORK < tm_end ) |
---|
| 171 | printk("\n[DBG] %s : child_thread %x exit / cost = %d / cycle %d\n", |
---|
| 172 | __FUNCTION__ , child_thread_ptr, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
[416] | 173 | #endif |
---|
[408] | 174 | |
---|
[407] | 175 | return 0; |
---|
| 176 | } |
---|
| 177 | |
---|
[1] | 178 | } // end sys_fork() |
---|
| 179 | |
---|