| 1 | /* |
|---|
| 2 | * process.c - process related functions definition. |
|---|
| 3 | * |
|---|
| 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
|---|
| 5 | * Mohamed Lamine Karaoui (2015) |
|---|
| 6 | * Alain Greiner (2016,2017,2018) |
|---|
| 7 | * |
|---|
| 8 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| 9 | * |
|---|
| 10 | * This file is part of ALMOS-MKH. |
|---|
| 11 | * |
|---|
| 12 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
|---|
| 13 | * under the terms of the GNU General Public License as published by |
|---|
| 14 | * the Free Software Foundation; version 2.0 of the License. |
|---|
| 15 | * |
|---|
| 16 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
|---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 19 | * General Public License for more details. |
|---|
| 20 | * |
|---|
| 21 | * You should have received a copy of the GNU General Public License |
|---|
| 22 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
|---|
| 23 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #include <kernel_config.h> |
|---|
| 27 | #include <hal_kernel_types.h> |
|---|
| 28 | #include <hal_remote.h> |
|---|
| 29 | #include <hal_uspace.h> |
|---|
| 30 | #include <hal_irqmask.h> |
|---|
| 31 | #include <errno.h> |
|---|
| 32 | #include <printk.h> |
|---|
| 33 | #include <memcpy.h> |
|---|
| 34 | #include <bits.h> |
|---|
| 35 | #include <kmem.h> |
|---|
| 36 | #include <page.h> |
|---|
| 37 | #include <vmm.h> |
|---|
| 38 | #include <vfs.h> |
|---|
| 39 | #include <core.h> |
|---|
| 40 | #include <thread.h> |
|---|
| 41 | #include <chdev.h> |
|---|
| 42 | #include <list.h> |
|---|
| 43 | #include <string.h> |
|---|
| 44 | #include <scheduler.h> |
|---|
| 45 | #include <busylock.h> |
|---|
| 46 | #include <queuelock.h> |
|---|
| 47 | #include <remote_queuelock.h> |
|---|
| 48 | #include <rwlock.h> |
|---|
| 49 | #include <remote_rwlock.h> |
|---|
| 50 | #include <dqdt.h> |
|---|
| 51 | #include <cluster.h> |
|---|
| 52 | #include <ppm.h> |
|---|
| 53 | #include <boot_info.h> |
|---|
| 54 | #include <process.h> |
|---|
| 55 | #include <elf.h> |
|---|
| 56 | #include <syscalls.h> |
|---|
| 57 | #include <shared_syscalls.h> |
|---|
| 58 | |
|---|
| 59 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 60 | // Extern global variables |
|---|
| 61 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 62 | |
|---|
| 63 | extern process_t process_zero; // allocated in kernel_init.c |
|---|
| 64 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
|---|
| 65 | |
|---|
| 66 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 67 | // Process initialisation related functions |
|---|
| 68 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 69 | |
|---|
| 70 | ///////////////////////////////// |
|---|
| 71 | process_t * process_alloc( void ) |
|---|
| 72 | { |
|---|
| 73 | kmem_req_t req; |
|---|
| 74 | |
|---|
| 75 | req.type = KMEM_PROCESS; |
|---|
| 76 | req.size = sizeof(process_t); |
|---|
| 77 | req.flags = AF_KERNEL; |
|---|
| 78 | |
|---|
| 79 | return (process_t *)kmem_alloc( &req ); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | //////////////////////////////////////// |
|---|
| 83 | void process_free( process_t * process ) |
|---|
| 84 | { |
|---|
| 85 | kmem_req_t req; |
|---|
| 86 | |
|---|
| 87 | req.type = KMEM_PROCESS; |
|---|
| 88 | req.ptr = process; |
|---|
| 89 | kmem_free( &req ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | ///////////////////////////////////////////////// |
|---|
| 93 | void process_reference_init( process_t * process, |
|---|
| 94 | pid_t pid, |
|---|
| 95 | xptr_t parent_xp ) |
|---|
| 96 | { |
|---|
| 97 | cxy_t parent_cxy; |
|---|
| 98 | process_t * parent_ptr; |
|---|
| 99 | xptr_t stdin_xp; |
|---|
| 100 | xptr_t stdout_xp; |
|---|
| 101 | xptr_t stderr_xp; |
|---|
| 102 | uint32_t stdin_id; |
|---|
| 103 | uint32_t stdout_id; |
|---|
| 104 | uint32_t stderr_id; |
|---|
| 105 | error_t error; |
|---|
| 106 | uint32_t txt_id; |
|---|
| 107 | char rx_path[40]; |
|---|
| 108 | char tx_path[40]; |
|---|
| 109 | xptr_t file_xp; |
|---|
| 110 | xptr_t chdev_xp; |
|---|
| 111 | chdev_t * chdev_ptr; |
|---|
| 112 | cxy_t chdev_cxy; |
|---|
| 113 | pid_t parent_pid; |
|---|
| 114 | |
|---|
| 115 | // get parent process cluster and local pointer |
|---|
| 116 | parent_cxy = GET_CXY( parent_xp ); |
|---|
| 117 | parent_ptr = GET_PTR( parent_xp ); |
|---|
| 118 | |
|---|
| 119 | // get parent_pid |
|---|
| 120 | parent_pid = hal_remote_l32( XPTR( parent_cxy , &parent_ptr->pid ) ); |
|---|
| 121 | |
|---|
| 122 | #if DEBUG_PROCESS_REFERENCE_INIT |
|---|
| 123 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 124 | if( DEBUG_PROCESS_REFERENCE_INIT ) |
|---|
| 125 | printk("\n[%s] thread %x in process %x enter to initalialize process %x / cycle %d\n", |
|---|
| 126 | __FUNCTION__, CURRENT_THREAD->trdid, parent_pid , pid , cycle ); |
|---|
| 127 | #endif |
|---|
| 128 | |
|---|
| 129 | // initialize PID, REF_XP, PARENT_XP, and STATE |
|---|
| 130 | process->pid = pid; |
|---|
| 131 | process->ref_xp = XPTR( local_cxy , process ); |
|---|
| 132 | process->owner_xp = XPTR( local_cxy , process ); |
|---|
| 133 | process->parent_xp = parent_xp; |
|---|
| 134 | process->term_state = 0; |
|---|
| 135 | |
|---|
| 136 | // initialize vmm as empty |
|---|
| 137 | error = vmm_init( process ); |
|---|
| 138 | |
|---|
| 139 | assert( (error == 0) , "cannot initialize VMM\n" ); |
|---|
| 140 | |
|---|
| 141 | #if (DEBUG_PROCESS_REFERENCE_INIT & 1) |
|---|
| 142 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 143 | if( DEBUG_PROCESS_REFERENCE_INIT ) |
|---|
| 144 | printk("\n[%s] thread %x in process %x / vmm empty for process %x / cycle %d\n", |
|---|
| 145 | __FUNCTION__, CURRENT_THREAD->trdid, parent_pid , pid, cycle ); |
|---|
| 146 | #endif |
|---|
| 147 | |
|---|
| 148 | // initialize fd_array as empty |
|---|
| 149 | process_fd_init( process ); |
|---|
| 150 | |
|---|
| 151 | // define the stdin/stdout/stderr pseudo files <=> select a TXT terminal. |
|---|
| 152 | if( (pid == 1) || (parent_pid == 1) ) // INIT or KSH process |
|---|
| 153 | { |
|---|
| 154 | // select a TXT channel |
|---|
| 155 | if( pid == 1 ) txt_id = 0; // INIT |
|---|
| 156 | else txt_id = process_txt_alloc(); // KSH |
|---|
| 157 | |
|---|
| 158 | // attach process to TXT |
|---|
| 159 | process_txt_attach( process , txt_id ); |
|---|
| 160 | |
|---|
| 161 | #if (DEBUG_PROCESS_REFERENCE_INIT & 1) |
|---|
| 162 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 163 | if( DEBUG_PROCESS_REFERENCE_INIT ) |
|---|
| 164 | printk("\n[%s] thread %x in process %x / process %x attached to TXT%d / cycle %d\n", |
|---|
| 165 | __FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, txt_id, cycle ); |
|---|
| 166 | #endif |
|---|
| 167 | // build path to TXT_RX[i] and TXT_TX[i] chdevs |
|---|
| 168 | snprintf( rx_path , 40 , "/dev/external/txt%d_rx", txt_id ); |
|---|
| 169 | snprintf( tx_path , 40 , "/dev/external/txt%d_tx", txt_id ); |
|---|
| 170 | |
|---|
| 171 | // create stdin pseudo file |
|---|
| 172 | error = vfs_open( process, |
|---|
| 173 | rx_path, |
|---|
| 174 | O_RDONLY, |
|---|
| 175 | 0, // FIXME chmod |
|---|
| 176 | &stdin_xp, |
|---|
| 177 | &stdin_id ); |
|---|
| 178 | |
|---|
| 179 | assert( (error == 0) , "cannot open stdin pseudo file" ); |
|---|
| 180 | assert( (stdin_id == 0) , "stdin index must be 0" ); |
|---|
| 181 | |
|---|
| 182 | #if (DEBUG_PROCESS_REFERENCE_INIT & 1) |
|---|
| 183 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 184 | if( DEBUG_PROCESS_REFERENCE_INIT ) |
|---|
| 185 | printk("\n[%s] thread %x in process %x / stdin open for process %x / cycle %d\n", |
|---|
| 186 | __FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, cycle ); |
|---|
| 187 | #endif |
|---|
| 188 | |
|---|
| 189 | // create stdout pseudo file |
|---|
| 190 | error = vfs_open( process, |
|---|
| 191 | tx_path, |
|---|
| 192 | O_WRONLY, |
|---|
| 193 | 0, // FIXME chmod |
|---|
| 194 | &stdout_xp, |
|---|
| 195 | &stdout_id ); |
|---|
| 196 | |
|---|
| 197 | assert( (error == 0) , "cannot open stdout pseudo file" ); |
|---|
| 198 | assert( (stdout_id == 1) , "stdout index must be 1" ); |
|---|
| 199 | |
|---|
| 200 | #if (DEBUG_PROCESS_REFERENCE_INIT & 1) |
|---|
| 201 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 202 | if( DEBUG_PROCESS_REFERENCE_INIT ) |
|---|
| 203 | printk("\n[%s] thread %x in process %x / stdout open for process %x / cycle %d\n", |
|---|
| 204 | __FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, cycle ); |
|---|
| 205 | #endif |
|---|
| 206 | |
|---|
| 207 | // create stderr pseudo file |
|---|
| 208 | error = vfs_open( process, |
|---|
| 209 | tx_path, |
|---|
| 210 | O_WRONLY, |
|---|
| 211 | 0, // FIXME chmod |
|---|
| 212 | &stderr_xp, |
|---|
| 213 | &stderr_id ); |
|---|
| 214 | |
|---|
| 215 | assert( (error == 0) , "cannot open stderr pseudo file" ); |
|---|
| 216 | assert( (stderr_id == 2) , "stderr index must be 2" ); |
|---|
| 217 | |
|---|
| 218 | #if (DEBUG_PROCESS_REFERENCE_INIT & 1) |
|---|
| 219 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 220 | if( DEBUG_PROCESS_REFERENCE_INIT ) |
|---|
| 221 | printk("\n[%s] thread %x in process %x / stderr open for process %x / cycle %d\n", |
|---|
| 222 | __FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, cycle ); |
|---|
| 223 | #endif |
|---|
| 224 | |
|---|
| 225 | } |
|---|
| 226 | else // normal user process |
|---|
| 227 | { |
|---|
| 228 | // get extended pointer on stdin pseudo file in parent process |
|---|
| 229 | file_xp = (xptr_t)hal_remote_l64( XPTR( parent_cxy , &parent_ptr->fd_array.array[0] ) ); |
|---|
| 230 | |
|---|
| 231 | // get extended pointer on parent process TXT chdev |
|---|
| 232 | chdev_xp = chdev_from_file( file_xp ); |
|---|
| 233 | |
|---|
| 234 | // get cluster and local pointer on chdev |
|---|
| 235 | chdev_cxy = GET_CXY( chdev_xp ); |
|---|
| 236 | chdev_ptr = GET_PTR( chdev_xp ); |
|---|
| 237 | |
|---|
| 238 | // get parent process TXT terminal index |
|---|
| 239 | txt_id = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) ); |
|---|
| 240 | |
|---|
| 241 | // attach child process to parent process TXT terminal |
|---|
| 242 | process_txt_attach( process , txt_id ); |
|---|
| 243 | |
|---|
| 244 | // copy all open files from parent process fd_array to this process |
|---|
| 245 | process_fd_remote_copy( XPTR( local_cxy , &process->fd_array ), |
|---|
| 246 | XPTR( parent_cxy , &parent_ptr->fd_array ) ); |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | // initialize specific inodes root and cwd |
|---|
| 250 | process->vfs_root_xp = (xptr_t)hal_remote_l64( XPTR( parent_cxy, |
|---|
| 251 | &parent_ptr->vfs_root_xp ) ); |
|---|
| 252 | process->vfs_cwd_xp = (xptr_t)hal_remote_l64( XPTR( parent_cxy, |
|---|
| 253 | &parent_ptr->vfs_cwd_xp ) ); |
|---|
| 254 | vfs_inode_remote_up( process->vfs_root_xp ); |
|---|
| 255 | vfs_inode_remote_up( process->vfs_cwd_xp ); |
|---|
| 256 | |
|---|
| 257 | remote_rwlock_init( XPTR( local_cxy , &process->cwd_lock ), LOCK_PROCESS_CWD ); |
|---|
| 258 | |
|---|
| 259 | #if (DEBUG_PROCESS_REFERENCE_INIT & 1) |
|---|
| 260 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 261 | if( DEBUG_PROCESS_REFERENCE_INIT ) |
|---|
| 262 | printk("\n[%s] thread %x in process %x / set fd_array for process %x / cycle %d\n", |
|---|
| 263 | __FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid , cycle ); |
|---|
| 264 | #endif |
|---|
| 265 | |
|---|
| 266 | // reset children list root |
|---|
| 267 | xlist_root_init( XPTR( local_cxy , &process->children_root ) ); |
|---|
| 268 | process->children_nr = 0; |
|---|
| 269 | remote_queuelock_init( XPTR( local_cxy , &process->children_lock ), LOCK_PROCESS_CHILDREN ); |
|---|
| 270 | |
|---|
| 271 | // reset semaphore / mutex / barrier / condvar list roots |
|---|
| 272 | xlist_root_init( XPTR( local_cxy , &process->sem_root ) ); |
|---|
| 273 | xlist_root_init( XPTR( local_cxy , &process->mutex_root ) ); |
|---|
| 274 | xlist_root_init( XPTR( local_cxy , &process->barrier_root ) ); |
|---|
| 275 | xlist_root_init( XPTR( local_cxy , &process->condvar_root ) ); |
|---|
| 276 | remote_queuelock_init( XPTR( local_cxy , &process->sync_lock ), LOCK_PROCESS_USERSYNC ); |
|---|
| 277 | |
|---|
| 278 | // register new process in the local cluster manager pref_tbl[] |
|---|
| 279 | lpid_t lpid = LPID_FROM_PID( pid ); |
|---|
| 280 | LOCAL_CLUSTER->pmgr.pref_tbl[lpid] = XPTR( local_cxy , process ); |
|---|
| 281 | |
|---|
| 282 | // register new process descriptor in local cluster manager local_list |
|---|
| 283 | cluster_process_local_link( process ); |
|---|
| 284 | |
|---|
| 285 | // register new process descriptor in local cluster manager copies_list |
|---|
| 286 | cluster_process_copies_link( process ); |
|---|
| 287 | |
|---|
| 288 | // initialize th_tbl[] array and associated threads |
|---|
| 289 | uint32_t i; |
|---|
| 290 | |
|---|
| 291 | for( i = 0 ; i < CONFIG_THREADS_MAX_PER_CLUSTER ; i++ ) |
|---|
| 292 | { |
|---|
| 293 | process->th_tbl[i] = NULL; |
|---|
| 294 | } |
|---|
| 295 | process->th_nr = 0; |
|---|
| 296 | rwlock_init( &process->th_lock , LOCK_PROCESS_THTBL ); |
|---|
| 297 | |
|---|
| 298 | hal_fence(); |
|---|
| 299 | |
|---|
| 300 | #if (DEBUG_PROCESS_REFERENCE_INIT & 1) |
|---|
| 301 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 302 | if( DEBUG_PROCESS_REFERENCE_INIT ) |
|---|
| 303 | printk("\n[%s] thread %x in process %x exit for process %x / cycle %d\n", |
|---|
| 304 | __FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, cycle ); |
|---|
| 305 | #endif |
|---|
| 306 | |
|---|
| 307 | } // process_reference_init() |
|---|
| 308 | |
|---|
| 309 | ///////////////////////////////////////////////////// |
|---|
| 310 | error_t process_copy_init( process_t * local_process, |
|---|
| 311 | xptr_t reference_process_xp ) |
|---|
| 312 | { |
|---|
| 313 | error_t error; |
|---|
| 314 | |
|---|
| 315 | // get reference process cluster and local pointer |
|---|
| 316 | cxy_t ref_cxy = GET_CXY( reference_process_xp ); |
|---|
| 317 | process_t * ref_ptr = GET_PTR( reference_process_xp ); |
|---|
| 318 | |
|---|
| 319 | // initialize PID, REF_XP, PARENT_XP, and STATE |
|---|
| 320 | local_process->pid = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->pid ) ); |
|---|
| 321 | local_process->parent_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->parent_xp ) ); |
|---|
| 322 | local_process->ref_xp = reference_process_xp; |
|---|
| 323 | local_process->owner_xp = reference_process_xp; |
|---|
| 324 | local_process->term_state = 0; |
|---|
| 325 | |
|---|
| 326 | #if DEBUG_PROCESS_COPY_INIT |
|---|
| 327 | thread_t * this = CURRET_THREAD; |
|---|
| 328 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 329 | if( DEBUG_PROCESS_COPY_INIT ) |
|---|
| 330 | printk("\n[%s] thread %x in process %x enter for process %x / cycle %d\n", |
|---|
| 331 | __FUNCTION__, this->trdid, this->process->pid, local_process->pid, cycle ); |
|---|
| 332 | #endif |
|---|
| 333 | |
|---|
| 334 | // check user process |
|---|
| 335 | assert( (local_process->pid != 0), "PID cannot be 0" ); |
|---|
| 336 | |
|---|
| 337 | // reset local process vmm |
|---|
| 338 | error = vmm_init( local_process ); |
|---|
| 339 | assert( (error == 0) , "cannot initialize VMM\n"); |
|---|
| 340 | |
|---|
| 341 | // reset process file descriptors array |
|---|
| 342 | process_fd_init( local_process ); |
|---|
| 343 | |
|---|
| 344 | // reset vfs_root_xp / vfs_bin_xp / vfs_cwd_xp fields |
|---|
| 345 | local_process->vfs_root_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->vfs_root_xp ) ); |
|---|
| 346 | local_process->vfs_bin_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->vfs_bin_xp ) ); |
|---|
| 347 | local_process->vfs_cwd_xp = XPTR_NULL; |
|---|
| 348 | |
|---|
| 349 | // reset children list root (not used in a process descriptor copy) |
|---|
| 350 | xlist_root_init( XPTR( local_cxy , &local_process->children_root ) ); |
|---|
| 351 | local_process->children_nr = 0; |
|---|
| 352 | remote_queuelock_init( XPTR( local_cxy , &local_process->children_lock ), |
|---|
| 353 | LOCK_PROCESS_CHILDREN ); |
|---|
| 354 | |
|---|
| 355 | // reset children_list (not used in a process descriptor copy) |
|---|
| 356 | xlist_entry_init( XPTR( local_cxy , &local_process->children_list ) ); |
|---|
| 357 | |
|---|
| 358 | // reset semaphores list root (not used in a process descriptor copy) |
|---|
| 359 | xlist_root_init( XPTR( local_cxy , &local_process->sem_root ) ); |
|---|
| 360 | xlist_root_init( XPTR( local_cxy , &local_process->mutex_root ) ); |
|---|
| 361 | xlist_root_init( XPTR( local_cxy , &local_process->barrier_root ) ); |
|---|
| 362 | xlist_root_init( XPTR( local_cxy , &local_process->condvar_root ) ); |
|---|
| 363 | |
|---|
| 364 | // initialize th_tbl[] array and associated fields |
|---|
| 365 | uint32_t i; |
|---|
| 366 | for( i = 0 ; i < CONFIG_THREADS_MAX_PER_CLUSTER ; i++ ) |
|---|
| 367 | { |
|---|
| 368 | local_process->th_tbl[i] = NULL; |
|---|
| 369 | } |
|---|
| 370 | local_process->th_nr = 0; |
|---|
| 371 | rwlock_init( &local_process->th_lock , LOCK_PROCESS_THTBL ); |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | // register new process descriptor in local cluster manager local_list |
|---|
| 375 | cluster_process_local_link( local_process ); |
|---|
| 376 | |
|---|
| 377 | // register new process descriptor in owner cluster manager copies_list |
|---|
| 378 | cluster_process_copies_link( local_process ); |
|---|
| 379 | |
|---|
| 380 | hal_fence(); |
|---|
| 381 | |
|---|
| 382 | #if DEBUG_PROCESS_COPY_INIT |
|---|
| 383 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 384 | if( DEBUG_PROCESS_COPY_INIT ) |
|---|
| 385 | printk("\n[%s] thread %x in process %x exit for process %x / cycle %d\n", |
|---|
| 386 | __FUNCTION__, this->trdid, this->process->pid, local_process->pid, cycle ); |
|---|
| 387 | #endif |
|---|
| 388 | |
|---|
| 389 | return 0; |
|---|
| 390 | |
|---|
| 391 | } // end process_copy_init() |
|---|
| 392 | |
|---|
| 393 | /////////////////////////////////////////// |
|---|
| 394 | void process_destroy( process_t * process ) |
|---|
| 395 | { |
|---|
| 396 | xptr_t parent_xp; |
|---|
| 397 | process_t * parent_ptr; |
|---|
| 398 | cxy_t parent_cxy; |
|---|
| 399 | xptr_t children_lock_xp; |
|---|
| 400 | xptr_t children_nr_xp; |
|---|
| 401 | |
|---|
| 402 | pid_t pid = process->pid; |
|---|
| 403 | |
|---|
| 404 | // check no more threads |
|---|
| 405 | assert( (process->th_nr == 0) , "process %x in cluster %x contains threads", pid , local_cxy ); |
|---|
| 406 | |
|---|
| 407 | #if DEBUG_PROCESS_DESTROY |
|---|
| 408 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 409 | if( DEBUG_PROCESS_DESTROY ) |
|---|
| 410 | printk("\n[%s] thread %x in process %x enter for process %x in cluster %x / cycle %d\n", |
|---|
| 411 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, pid, local_cxy, cycle ); |
|---|
| 412 | #endif |
|---|
| 413 | |
|---|
| 414 | // remove process from local_list in local cluster manager |
|---|
| 415 | cluster_process_local_unlink( process ); |
|---|
| 416 | |
|---|
| 417 | // remove process from copies_list in owner cluster manager |
|---|
| 418 | cluster_process_copies_unlink( process ); |
|---|
| 419 | |
|---|
| 420 | // remove process from children_list |
|---|
| 421 | // and release PID if owner cluster |
|---|
| 422 | if( CXY_FROM_PID( pid ) == local_cxy ) |
|---|
| 423 | { |
|---|
| 424 | // get pointers on parent process |
|---|
| 425 | parent_xp = process->parent_xp; |
|---|
| 426 | parent_cxy = GET_CXY( parent_xp ); |
|---|
| 427 | parent_ptr = GET_PTR( parent_xp ); |
|---|
| 428 | |
|---|
| 429 | // get extended pointer on children_lock in parent process |
|---|
| 430 | children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock ); |
|---|
| 431 | children_nr_xp = XPTR( parent_cxy , &parent_ptr->children_nr ); |
|---|
| 432 | |
|---|
| 433 | // remove process from children_list |
|---|
| 434 | remote_queuelock_acquire( children_lock_xp ); |
|---|
| 435 | xlist_unlink( XPTR( local_cxy , &process->children_list ) ); |
|---|
| 436 | hal_remote_atomic_add( children_nr_xp , -1 ); |
|---|
| 437 | remote_queuelock_release( children_lock_xp ); |
|---|
| 438 | |
|---|
| 439 | // release the process PID to cluster manager |
|---|
| 440 | cluster_pid_release( pid ); |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | // FIXME close all open files and synchronize dirty [AG] |
|---|
| 444 | |
|---|
| 445 | // decrease refcount for bin file, root file and cwd file |
|---|
| 446 | if( process->vfs_bin_xp != XPTR_NULL ) vfs_file_count_down( process->vfs_bin_xp ); |
|---|
| 447 | if( process->vfs_root_xp != XPTR_NULL ) vfs_file_count_down( process->vfs_root_xp ); |
|---|
| 448 | if( process->vfs_cwd_xp != XPTR_NULL ) vfs_file_count_down( process->vfs_cwd_xp ); |
|---|
| 449 | |
|---|
| 450 | // Destroy VMM |
|---|
| 451 | vmm_destroy( process ); |
|---|
| 452 | |
|---|
| 453 | // release memory allocated to process descriptor |
|---|
| 454 | process_free( process ); |
|---|
| 455 | |
|---|
| 456 | #if DEBUG_PROCESS_DESTROY |
|---|
| 457 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 458 | if( DEBUG_PROCESS_DESTROY ) |
|---|
| 459 | printk("\n[%s] thread %x in process %x exit / process %x in cluster %x / cycle %d\n", |
|---|
| 460 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, pid, local_cxy, cycle ); |
|---|
| 461 | #endif |
|---|
| 462 | |
|---|
| 463 | } // end process_destroy() |
|---|
| 464 | |
|---|
| 465 | /////////////////////////////////////////////////////////////////// |
|---|
| 466 | const char * process_action_str( process_sigactions_t action_type ) |
|---|
| 467 | { |
|---|
| 468 | switch ( action_type ) |
|---|
| 469 | { |
|---|
| 470 | case BLOCK_ALL_THREADS: return "BLOCK"; |
|---|
| 471 | case UNBLOCK_ALL_THREADS: return "UNBLOCK"; |
|---|
| 472 | case DELETE_ALL_THREADS: return "DELETE"; |
|---|
| 473 | default: return "undefined"; |
|---|
| 474 | } |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | //////////////////////////////////////// |
|---|
| 478 | void process_sigaction( pid_t pid, |
|---|
| 479 | uint32_t type ) |
|---|
| 480 | { |
|---|
| 481 | cxy_t owner_cxy; // owner cluster identifier |
|---|
| 482 | lpid_t lpid; // process index in owner cluster |
|---|
| 483 | cluster_t * cluster; // pointer on cluster manager |
|---|
| 484 | xptr_t root_xp; // extended pointer on root of copies |
|---|
| 485 | xptr_t lock_xp; // extended pointer on lock protecting copies |
|---|
| 486 | xptr_t iter_xp; // iterator on copies list |
|---|
| 487 | xptr_t process_xp; // extended pointer on process copy |
|---|
| 488 | cxy_t process_cxy; // process copy cluster identifier |
|---|
| 489 | process_t * process_ptr; // local pointer on process copy |
|---|
| 490 | reg_t save_sr; // for critical section |
|---|
| 491 | rpc_desc_t rpc; // shared RPC descriptor |
|---|
| 492 | thread_t * client; // pointer on client thread |
|---|
| 493 | xptr_t client_xp; // extended pointer on client thread |
|---|
| 494 | process_t * local; // pointer on process copy in local cluster |
|---|
| 495 | uint32_t remote_nr; // number of remote process copies |
|---|
| 496 | |
|---|
| 497 | client = CURRENT_THREAD; |
|---|
| 498 | client_xp = XPTR( local_cxy , client ); |
|---|
| 499 | local = NULL; |
|---|
| 500 | remote_nr = 0; |
|---|
| 501 | |
|---|
| 502 | // check calling thread can yield |
|---|
| 503 | thread_assert_can_yield( client , __FUNCTION__ ); |
|---|
| 504 | |
|---|
| 505 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 506 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 507 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 508 | printk("\n[%s] thread[%x,%x] enter to %s process %x / cycle %d\n", |
|---|
| 509 | __FUNCTION__ , client->process->pid, client->trdid, |
|---|
| 510 | process_action_str( type ) , pid , cycle ); |
|---|
| 511 | #endif |
|---|
| 512 | |
|---|
| 513 | // get pointer on local cluster manager |
|---|
| 514 | cluster = LOCAL_CLUSTER; |
|---|
| 515 | |
|---|
| 516 | // get owner cluster identifier and process lpid |
|---|
| 517 | owner_cxy = CXY_FROM_PID( pid ); |
|---|
| 518 | lpid = LPID_FROM_PID( pid ); |
|---|
| 519 | |
|---|
| 520 | // get root of list of copies and lock from owner cluster |
|---|
| 521 | root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] ); |
|---|
| 522 | lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] ); |
|---|
| 523 | |
|---|
| 524 | // check action type |
|---|
| 525 | assert( ((type == DELETE_ALL_THREADS ) || |
|---|
| 526 | (type == BLOCK_ALL_THREADS ) || |
|---|
| 527 | (type == UNBLOCK_ALL_THREADS )), "illegal action type" ); |
|---|
| 528 | |
|---|
| 529 | // This client thread send parallel RPCs to all remote clusters containing |
|---|
| 530 | // target process copies, wait all responses, and then handles directly |
|---|
| 531 | // the threads in local cluster, when required. |
|---|
| 532 | // The client thread allocates a - shared - RPC descriptor in the stack, |
|---|
| 533 | // because all parallel, non-blocking, server threads use the same input |
|---|
| 534 | // arguments, and use the shared RPC response field |
|---|
| 535 | |
|---|
| 536 | // mask IRQs |
|---|
| 537 | hal_disable_irq( &save_sr); |
|---|
| 538 | |
|---|
| 539 | // client thread blocks itself |
|---|
| 540 | thread_block( client_xp , THREAD_BLOCKED_RPC ); |
|---|
| 541 | |
|---|
| 542 | // take the lock protecting process copies |
|---|
| 543 | remote_queuelock_acquire( lock_xp ); |
|---|
| 544 | |
|---|
| 545 | // initialize shared RPC descriptor |
|---|
| 546 | rpc.responses = 0; |
|---|
| 547 | rpc.blocking = false; |
|---|
| 548 | rpc.index = RPC_PROCESS_SIGACTION; |
|---|
| 549 | rpc.thread = client; |
|---|
| 550 | rpc.lid = client->core->lid; |
|---|
| 551 | rpc.args[0] = type; |
|---|
| 552 | rpc.args[1] = pid; |
|---|
| 553 | |
|---|
| 554 | // scan list of process copies |
|---|
| 555 | // to send RPCs to remote copies |
|---|
| 556 | XLIST_FOREACH( root_xp , iter_xp ) |
|---|
| 557 | { |
|---|
| 558 | // get extended pointers and cluster on process |
|---|
| 559 | process_xp = XLIST_ELEMENT( iter_xp , process_t , copies_list ); |
|---|
| 560 | process_cxy = GET_CXY( process_xp ); |
|---|
| 561 | process_ptr = GET_PTR( process_xp ); |
|---|
| 562 | |
|---|
| 563 | // printk("\n@@@ in %s : process_cxy %x / process_ptr %x / pid %x\n", |
|---|
| 564 | // __FUNCTION__, process_cxy, process_ptr, hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) ) ); |
|---|
| 565 | |
|---|
| 566 | if( process_cxy == local_cxy ) // process copy is local |
|---|
| 567 | { |
|---|
| 568 | local = process_ptr; |
|---|
| 569 | } |
|---|
| 570 | else // process copy is remote |
|---|
| 571 | { |
|---|
| 572 | // update number of remote process copies |
|---|
| 573 | remote_nr++; |
|---|
| 574 | |
|---|
| 575 | // atomically increment responses counter |
|---|
| 576 | hal_atomic_add( (void *)&rpc.responses , 1 ); |
|---|
| 577 | |
|---|
| 578 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 579 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 580 | printk("\n[%s] thread[%x,%x] send RPC to cluster %x for process %x\n", |
|---|
| 581 | __FUNCTION__, client->process->pid, client->trdid, process_cxy, pid ); |
|---|
| 582 | #endif |
|---|
| 583 | // call RPC in target cluster |
|---|
| 584 | rpc_process_sigaction_client( process_cxy , &rpc ); |
|---|
| 585 | } |
|---|
| 586 | } // end list of copies |
|---|
| 587 | |
|---|
| 588 | // release the lock protecting process copies |
|---|
| 589 | remote_queuelock_release( lock_xp ); |
|---|
| 590 | |
|---|
| 591 | // restore IRQs |
|---|
| 592 | hal_restore_irq( save_sr); |
|---|
| 593 | |
|---|
| 594 | // - if there is remote process copies, the client thread deschedules, |
|---|
| 595 | // (it will be unblocked by the last RPC server thread). |
|---|
| 596 | // - if there is no remote copies, the client thread unblock itself. |
|---|
| 597 | if( remote_nr ) |
|---|
| 598 | { |
|---|
| 599 | sched_yield("blocked on rpc_process_sigaction"); |
|---|
| 600 | } |
|---|
| 601 | else |
|---|
| 602 | { |
|---|
| 603 | thread_unblock( client_xp , THREAD_BLOCKED_RPC ); |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | // handle the local process copy if required |
|---|
| 607 | if( local != NULL ) |
|---|
| 608 | { |
|---|
| 609 | |
|---|
| 610 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 611 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 612 | printk("\n[%s] thread[%x,%x] handles local process %x in cluster %x\n", |
|---|
| 613 | __FUNCTION__, client->process->pid, client->trdid, pid , local_cxy ); |
|---|
| 614 | #endif |
|---|
| 615 | if (type == DELETE_ALL_THREADS ) process_delete_threads ( local , client_xp ); |
|---|
| 616 | else if(type == BLOCK_ALL_THREADS ) process_block_threads ( local ); |
|---|
| 617 | else if(type == UNBLOCK_ALL_THREADS ) process_unblock_threads( local ); |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 621 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 622 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 623 | printk("\n[%s] thread[%x,%x] exit after %s process %x / cycle %d\n", |
|---|
| 624 | __FUNCTION__, client->process->pid, client->trdid, |
|---|
| 625 | process_action_str( type ), pid, cycle ); |
|---|
| 626 | #endif |
|---|
| 627 | |
|---|
| 628 | } // end process_sigaction() |
|---|
| 629 | |
|---|
| 630 | ///////////////////////////////////////////////// |
|---|
| 631 | void process_block_threads( process_t * process ) |
|---|
| 632 | { |
|---|
| 633 | thread_t * target; // pointer on target thread |
|---|
| 634 | thread_t * this; // pointer on calling thread |
|---|
| 635 | uint32_t ltid; // index in process th_tbl[] |
|---|
| 636 | cxy_t owner_cxy; // target process owner cluster |
|---|
| 637 | uint32_t count; // requests counter |
|---|
| 638 | volatile uint32_t ack_count; // acknowledges counter |
|---|
| 639 | |
|---|
| 640 | // get calling thread pointer |
|---|
| 641 | this = CURRENT_THREAD; |
|---|
| 642 | |
|---|
| 643 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 644 | pid_t pid = process->pid; |
|---|
| 645 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 646 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 647 | printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n", |
|---|
| 648 | __FUNCTION__, this->process->pid, this->trdid, pid, local_cxy , cycle ); |
|---|
| 649 | #endif |
|---|
| 650 | |
|---|
| 651 | // check target process is an user process |
|---|
| 652 | assert( (LPID_FROM_PID( process->pid ) != 0 ), "target process must be an user process" ); |
|---|
| 653 | |
|---|
| 654 | // get target process cluster |
|---|
| 655 | owner_cxy = CXY_FROM_PID( process->pid ); |
|---|
| 656 | |
|---|
| 657 | // get lock protecting process th_tbl[] |
|---|
| 658 | rwlock_rd_acquire( &process->th_lock ); |
|---|
| 659 | |
|---|
| 660 | // loop on target process local threads |
|---|
| 661 | // we use both "ltid" and "count" because it can exist "holes" in th_tbl |
|---|
| 662 | // - if the calling thread and the target thread are not running on the same |
|---|
| 663 | // core, we ask the target scheduler to acknowlege the blocking |
|---|
| 664 | // to be sure that the target thread is not running. |
|---|
| 665 | // - if the calling thread and the target thread are running on the same core, |
|---|
| 666 | // we don't need confirmation from scheduler. |
|---|
| 667 | |
|---|
| 668 | for( ltid = 0 , count = 0 , ack_count = 0 ; count < process->th_nr ; ltid++ ) |
|---|
| 669 | { |
|---|
| 670 | target = process->th_tbl[ltid]; |
|---|
| 671 | |
|---|
| 672 | if( target != NULL ) // thread exist |
|---|
| 673 | { |
|---|
| 674 | count++; |
|---|
| 675 | |
|---|
| 676 | // set the global blocked bit in target thread descriptor. |
|---|
| 677 | thread_block( XPTR( local_cxy , target ) , THREAD_BLOCKED_GLOBAL ); |
|---|
| 678 | |
|---|
| 679 | if( this->core->lid != target->core->lid ) |
|---|
| 680 | { |
|---|
| 681 | // increment responses counter |
|---|
| 682 | hal_atomic_add( (void*)&ack_count , 1 ); |
|---|
| 683 | |
|---|
| 684 | // set FLAG_REQ_ACK and &ack_rsp_count in target descriptor |
|---|
| 685 | thread_set_req_ack( target , (uint32_t *)&ack_count ); |
|---|
| 686 | |
|---|
| 687 | // force scheduling on target thread |
|---|
| 688 | dev_pic_send_ipi( local_cxy , target->core->lid ); |
|---|
| 689 | } |
|---|
| 690 | } |
|---|
| 691 | } |
|---|
| 692 | |
|---|
| 693 | // release lock protecting process th_tbl[] |
|---|
| 694 | rwlock_rd_release( &process->th_lock ); |
|---|
| 695 | |
|---|
| 696 | // wait other threads acknowledges TODO this could be improved... |
|---|
| 697 | while( 1 ) |
|---|
| 698 | { |
|---|
| 699 | // exit when all scheduler acknoledges received |
|---|
| 700 | if ( ack_count == 0 ) break; |
|---|
| 701 | |
|---|
| 702 | // wait 1000 cycles before retry |
|---|
| 703 | hal_fixed_delay( 1000 ); |
|---|
| 704 | } |
|---|
| 705 | |
|---|
| 706 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 707 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 708 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 709 | printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n", |
|---|
| 710 | __FUNCTION__, this->process->pid, this->trdid, pid, local_cxy , cycle ); |
|---|
| 711 | #endif |
|---|
| 712 | |
|---|
| 713 | } // end process_block_threads() |
|---|
| 714 | |
|---|
| 715 | ///////////////////////////////////////////////// |
|---|
| 716 | void process_delete_threads( process_t * process, |
|---|
| 717 | xptr_t client_xp ) |
|---|
| 718 | { |
|---|
| 719 | thread_t * this; // pointer on calling thread |
|---|
| 720 | thread_t * target; // local pointer on target thread |
|---|
| 721 | xptr_t target_xp; // extended pointer on target thread |
|---|
| 722 | cxy_t owner_cxy; // owner process cluster |
|---|
| 723 | uint32_t ltid; // index in process th_tbl |
|---|
| 724 | uint32_t count; // threads counter |
|---|
| 725 | |
|---|
| 726 | // get calling thread pointer |
|---|
| 727 | this = CURRENT_THREAD; |
|---|
| 728 | |
|---|
| 729 | // get target process owner cluster |
|---|
| 730 | owner_cxy = CXY_FROM_PID( process->pid ); |
|---|
| 731 | |
|---|
| 732 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 733 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 734 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 735 | printk("\n[%s] thread[%x,%x] enter in cluster %x for process %x / cycle %d\n", |
|---|
| 736 | __FUNCTION__, this->process->pid, this->trdid, local_cxy, process->pid, cycle ); |
|---|
| 737 | #endif |
|---|
| 738 | |
|---|
| 739 | // check target process is an user process |
|---|
| 740 | assert( (LPID_FROM_PID( process->pid ) != 0), "process %x not an user process", process->pid ); |
|---|
| 741 | |
|---|
| 742 | // get lock protecting process th_tbl[] |
|---|
| 743 | rwlock_wr_acquire( &process->th_lock ); |
|---|
| 744 | |
|---|
| 745 | // loop on target process local threads |
|---|
| 746 | // we use both "ltid" and "count" because it can exist "holes" in th_tbl |
|---|
| 747 | for( ltid = 0 , count = 0 ; count < process->th_nr ; ltid++ ) |
|---|
| 748 | { |
|---|
| 749 | target = process->th_tbl[ltid]; |
|---|
| 750 | |
|---|
| 751 | if( target != NULL ) // valid thread |
|---|
| 752 | { |
|---|
| 753 | count++; |
|---|
| 754 | target_xp = XPTR( local_cxy , target ); |
|---|
| 755 | |
|---|
| 756 | // main thread and client thread should not be deleted |
|---|
| 757 | if( ((ltid != 0) || (owner_cxy != local_cxy)) && // not main thread |
|---|
| 758 | (client_xp) != target_xp ) // not client thread |
|---|
| 759 | { |
|---|
| 760 | // mark target thread for delete and block it |
|---|
| 761 | thread_delete( target_xp , process->pid , false ); // not forced |
|---|
| 762 | } |
|---|
| 763 | } |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | // release lock protecting process th_tbl[] |
|---|
| 767 | rwlock_wr_release( &process->th_lock ); |
|---|
| 768 | |
|---|
| 769 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 770 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 771 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 772 | printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n", |
|---|
| 773 | __FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy , cycle ); |
|---|
| 774 | #endif |
|---|
| 775 | |
|---|
| 776 | } // end process_delete_threads() |
|---|
| 777 | |
|---|
| 778 | /////////////////////////////////////////////////// |
|---|
| 779 | void process_unblock_threads( process_t * process ) |
|---|
| 780 | { |
|---|
| 781 | thread_t * target; // pointer on target thead |
|---|
| 782 | thread_t * this; // pointer on calling thread |
|---|
| 783 | uint32_t ltid; // index in process th_tbl |
|---|
| 784 | uint32_t count; // requests counter |
|---|
| 785 | |
|---|
| 786 | // get calling thread pointer |
|---|
| 787 | this = CURRENT_THREAD; |
|---|
| 788 | |
|---|
| 789 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 790 | pid_t pid = process->pid; |
|---|
| 791 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 792 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 793 | printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n", |
|---|
| 794 | __FUNCTION__, this->process->pid, this->trdid, pid, local_cxy , cycle ); |
|---|
| 795 | #endif |
|---|
| 796 | |
|---|
| 797 | // check target process is an user process |
|---|
| 798 | assert( ( process->pid != 0 ), |
|---|
| 799 | "target process must be an user process" ); |
|---|
| 800 | |
|---|
| 801 | // get lock protecting process th_tbl[] |
|---|
| 802 | rwlock_rd_acquire( &process->th_lock ); |
|---|
| 803 | |
|---|
| 804 | // loop on process threads to unblock all threads |
|---|
| 805 | // we use both "ltid" and "count" because it can exist "holes" in th_tbl |
|---|
| 806 | for( ltid = 0 , count = 0 ; count < process->th_nr ; ltid++ ) |
|---|
| 807 | { |
|---|
| 808 | target = process->th_tbl[ltid]; |
|---|
| 809 | |
|---|
| 810 | if( target != NULL ) // thread found |
|---|
| 811 | { |
|---|
| 812 | count++; |
|---|
| 813 | |
|---|
| 814 | // reset the global blocked bit in target thread descriptor. |
|---|
| 815 | thread_unblock( XPTR( local_cxy , target ) , THREAD_BLOCKED_GLOBAL ); |
|---|
| 816 | } |
|---|
| 817 | } |
|---|
| 818 | |
|---|
| 819 | // release lock protecting process th_tbl[] |
|---|
| 820 | rwlock_rd_release( &process->th_lock ); |
|---|
| 821 | |
|---|
| 822 | #if DEBUG_PROCESS_SIGACTION |
|---|
| 823 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 824 | if( DEBUG_PROCESS_SIGACTION < cycle ) |
|---|
| 825 | printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n", |
|---|
| 826 | __FUNCTION__, this->process->pid, this->trdid, pid, local_cxy, cycle ); |
|---|
| 827 | #endif |
|---|
| 828 | |
|---|
| 829 | } // end process_unblock_threads() |
|---|
| 830 | |
|---|
| 831 | /////////////////////////////////////////////// |
|---|
| 832 | process_t * process_get_local_copy( pid_t pid ) |
|---|
| 833 | { |
|---|
| 834 | error_t error; |
|---|
| 835 | process_t * process_ptr; // local pointer on process |
|---|
| 836 | xptr_t process_xp; // extended pointer on process |
|---|
| 837 | |
|---|
| 838 | cluster_t * cluster = LOCAL_CLUSTER; |
|---|
| 839 | |
|---|
| 840 | #if DEBUG_PROCESS_GET_LOCAL_COPY |
|---|
| 841 | thread_t * this = CURRENT_THREAD; |
|---|
| 842 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 843 | if( DEBUG_PROCESS_GET_LOCAL_COPY < cycle ) |
|---|
| 844 | printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n", |
|---|
| 845 | __FUNCTION__, this->process->pid, this->trdid, pid, local_cxy, cycle ); |
|---|
| 846 | #endif |
|---|
| 847 | |
|---|
| 848 | // get lock protecting local list of processes |
|---|
| 849 | remote_queuelock_acquire( XPTR( local_cxy , &cluster->pmgr.local_lock ) ); |
|---|
| 850 | |
|---|
| 851 | // scan the local list of process descriptors to find the process |
|---|
| 852 | xptr_t iter; |
|---|
| 853 | bool_t found = false; |
|---|
| 854 | XLIST_FOREACH( XPTR( local_cxy , &cluster->pmgr.local_root ) , iter ) |
|---|
| 855 | { |
|---|
| 856 | process_xp = XLIST_ELEMENT( iter , process_t , local_list ); |
|---|
| 857 | process_ptr = GET_PTR( process_xp ); |
|---|
| 858 | if( process_ptr->pid == pid ) |
|---|
| 859 | { |
|---|
| 860 | found = true; |
|---|
| 861 | break; |
|---|
| 862 | } |
|---|
| 863 | } |
|---|
| 864 | |
|---|
| 865 | // release lock protecting local list of processes |
|---|
| 866 | remote_queuelock_release( XPTR( local_cxy , &cluster->pmgr.local_lock ) ); |
|---|
| 867 | |
|---|
| 868 | // allocate memory for a new local process descriptor |
|---|
| 869 | // and initialise it from reference cluster if not found |
|---|
| 870 | if( !found ) |
|---|
| 871 | { |
|---|
| 872 | // get extended pointer on reference process descriptor |
|---|
| 873 | xptr_t ref_xp = cluster_get_reference_process_from_pid( pid ); |
|---|
| 874 | |
|---|
| 875 | assert( (ref_xp != XPTR_NULL) , "illegal pid\n" ); |
|---|
| 876 | |
|---|
| 877 | // allocate memory for local process descriptor |
|---|
| 878 | process_ptr = process_alloc(); |
|---|
| 879 | |
|---|
| 880 | if( process_ptr == NULL ) return NULL; |
|---|
| 881 | |
|---|
| 882 | // initialize local process descriptor copy |
|---|
| 883 | error = process_copy_init( process_ptr , ref_xp ); |
|---|
| 884 | |
|---|
| 885 | if( error ) return NULL; |
|---|
| 886 | } |
|---|
| 887 | |
|---|
| 888 | #if DEBUG_PROCESS_GET_LOCAL_COPY |
|---|
| 889 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 890 | if( DEBUG_PROCESS_GET_LOCAL_COPY < cycle ) |
|---|
| 891 | printk("\n[%s] thread[%x,%x] exit in cluster %x / process %x / cycle %d\n", |
|---|
| 892 | __FUNCTION__, this->process->pid, this->trdid, local_cxy, process_ptr, cycle ); |
|---|
| 893 | #endif |
|---|
| 894 | |
|---|
| 895 | return process_ptr; |
|---|
| 896 | |
|---|
| 897 | } // end process_get_local_copy() |
|---|
| 898 | |
|---|
| 899 | //////////////////////////////////////////// |
|---|
| 900 | pid_t process_get_ppid( xptr_t process_xp ) |
|---|
| 901 | { |
|---|
| 902 | cxy_t process_cxy; |
|---|
| 903 | process_t * process_ptr; |
|---|
| 904 | xptr_t parent_xp; |
|---|
| 905 | cxy_t parent_cxy; |
|---|
| 906 | process_t * parent_ptr; |
|---|
| 907 | |
|---|
| 908 | // get process cluster and local pointer |
|---|
| 909 | process_cxy = GET_CXY( process_xp ); |
|---|
| 910 | process_ptr = GET_PTR( process_xp ); |
|---|
| 911 | |
|---|
| 912 | // get pointers on parent process |
|---|
| 913 | parent_xp = (xptr_t)hal_remote_l64( XPTR( process_cxy , &process_ptr->parent_xp ) ); |
|---|
| 914 | parent_cxy = GET_CXY( parent_xp ); |
|---|
| 915 | parent_ptr = GET_PTR( parent_xp ); |
|---|
| 916 | |
|---|
| 917 | return hal_remote_l32( XPTR( parent_cxy , &parent_ptr->pid ) ); |
|---|
| 918 | } |
|---|
| 919 | |
|---|
| 920 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 921 | // File descriptor array related functions |
|---|
| 922 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 923 | |
|---|
| 924 | /////////////////////////////////////////// |
|---|
| 925 | void process_fd_init( process_t * process ) |
|---|
| 926 | { |
|---|
| 927 | uint32_t fd; |
|---|
| 928 | |
|---|
| 929 | remote_queuelock_init( XPTR( local_cxy , &process->fd_array.lock ), LOCK_PROCESS_FDARRAY ); |
|---|
| 930 | |
|---|
| 931 | process->fd_array.current = 0; |
|---|
| 932 | |
|---|
| 933 | // initialize array |
|---|
| 934 | for ( fd = 0 ; fd < CONFIG_PROCESS_FILE_MAX_NR ; fd++ ) |
|---|
| 935 | { |
|---|
| 936 | process->fd_array.array[fd] = XPTR_NULL; |
|---|
| 937 | } |
|---|
| 938 | } |
|---|
| 939 | ///////////////////////////////////////////////// |
|---|
| 940 | error_t process_fd_register( process_t * process, |
|---|
| 941 | xptr_t file_xp, |
|---|
| 942 | uint32_t * fdid ) |
|---|
| 943 | { |
|---|
| 944 | bool_t found; |
|---|
| 945 | uint32_t id; |
|---|
| 946 | uint32_t count; |
|---|
| 947 | xptr_t xp; |
|---|
| 948 | |
|---|
| 949 | // get reference process cluster and local pointer |
|---|
| 950 | xptr_t ref_xp = process->ref_xp; |
|---|
| 951 | process_t * ref_ptr = GET_PTR( ref_xp ); |
|---|
| 952 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
|---|
| 953 | |
|---|
| 954 | // take lock protecting reference fd_array |
|---|
| 955 | remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->fd_array.lock ) ); |
|---|
| 956 | |
|---|
| 957 | found = false; |
|---|
| 958 | |
|---|
| 959 | for ( id = 0; id < CONFIG_PROCESS_FILE_MAX_NR ; id++ ) |
|---|
| 960 | { |
|---|
| 961 | xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->fd_array.array[id] ) ); |
|---|
| 962 | if ( xp == XPTR_NULL ) |
|---|
| 963 | { |
|---|
| 964 | // update reference fd_array |
|---|
| 965 | hal_remote_s64( XPTR( ref_cxy , &ref_ptr->fd_array.array[id] ) , file_xp ); |
|---|
| 966 | count = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->fd_array.current ) ) + 1; |
|---|
| 967 | hal_remote_s32( XPTR( ref_cxy , &ref_ptr->fd_array.current ) , count ); |
|---|
| 968 | |
|---|
| 969 | // update local fd_array copy if required |
|---|
| 970 | if( ref_cxy != local_cxy ) |
|---|
| 971 | { |
|---|
| 972 | process->fd_array.array[id] = file_xp; |
|---|
| 973 | process->fd_array.current = count; |
|---|
| 974 | } |
|---|
| 975 | |
|---|
| 976 | // exit |
|---|
| 977 | *fdid = id; |
|---|
| 978 | found = true; |
|---|
| 979 | break; |
|---|
| 980 | } |
|---|
| 981 | } |
|---|
| 982 | |
|---|
| 983 | // release lock protecting reference fd_array |
|---|
| 984 | remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->fd_array.lock ) ); |
|---|
| 985 | |
|---|
| 986 | if ( !found ) return -1; |
|---|
| 987 | else return 0; |
|---|
| 988 | } |
|---|
| 989 | |
|---|
| 990 | //////////////////////////////////////////////// |
|---|
| 991 | xptr_t process_fd_get_xptr( process_t * process, |
|---|
| 992 | uint32_t fdid ) |
|---|
| 993 | { |
|---|
| 994 | xptr_t file_xp; |
|---|
| 995 | xptr_t lock_xp; |
|---|
| 996 | |
|---|
| 997 | // access local copy of process descriptor |
|---|
| 998 | file_xp = process->fd_array.array[fdid]; |
|---|
| 999 | |
|---|
| 1000 | if( file_xp == XPTR_NULL ) |
|---|
| 1001 | { |
|---|
| 1002 | // get reference process cluster and local pointer |
|---|
| 1003 | xptr_t ref_xp = process->ref_xp; |
|---|
| 1004 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
|---|
| 1005 | process_t * ref_ptr = GET_PTR( ref_xp ); |
|---|
| 1006 | |
|---|
| 1007 | // build extended pointer on lock protecting reference fd_array |
|---|
| 1008 | lock_xp = XPTR( ref_cxy , &ref_ptr->fd_array.lock ); |
|---|
| 1009 | |
|---|
| 1010 | // take lock protecting reference fd_array |
|---|
| 1011 | remote_queuelock_acquire( lock_xp ); |
|---|
| 1012 | |
|---|
| 1013 | // access reference process descriptor |
|---|
| 1014 | file_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->fd_array.array[fdid] ) ); |
|---|
| 1015 | |
|---|
| 1016 | // update local fd_array if found |
|---|
| 1017 | if( file_xp != XPTR_NULL ) process->fd_array.array[fdid] = file_xp; |
|---|
| 1018 | |
|---|
| 1019 | // release lock protecting reference fd_array |
|---|
| 1020 | remote_queuelock_release( lock_xp ); |
|---|
| 1021 | } |
|---|
| 1022 | |
|---|
| 1023 | return file_xp; |
|---|
| 1024 | |
|---|
| 1025 | } // end process_fd_get_xptr() |
|---|
| 1026 | |
|---|
| 1027 | /////////////////////////////////////////// |
|---|
| 1028 | void process_fd_remote_copy( xptr_t dst_xp, |
|---|
| 1029 | xptr_t src_xp ) |
|---|
| 1030 | { |
|---|
| 1031 | uint32_t fd; |
|---|
| 1032 | xptr_t entry; |
|---|
| 1033 | |
|---|
| 1034 | // get cluster and local pointer for src fd_array |
|---|
| 1035 | cxy_t src_cxy = GET_CXY( src_xp ); |
|---|
| 1036 | fd_array_t * src_ptr = GET_PTR( src_xp ); |
|---|
| 1037 | |
|---|
| 1038 | // get cluster and local pointer for dst fd_array |
|---|
| 1039 | cxy_t dst_cxy = GET_CXY( dst_xp ); |
|---|
| 1040 | fd_array_t * dst_ptr = GET_PTR( dst_xp ); |
|---|
| 1041 | |
|---|
| 1042 | // get the remote lock protecting the src fd_array |
|---|
| 1043 | remote_queuelock_acquire( XPTR( src_cxy , &src_ptr->lock ) ); |
|---|
| 1044 | |
|---|
| 1045 | // loop on all fd_array entries |
|---|
| 1046 | for( fd = 0 ; fd < CONFIG_PROCESS_FILE_MAX_NR ; fd++ ) |
|---|
| 1047 | { |
|---|
| 1048 | entry = (xptr_t)hal_remote_l64( XPTR( src_cxy , &src_ptr->array[fd] ) ); |
|---|
| 1049 | |
|---|
| 1050 | if( entry != XPTR_NULL ) |
|---|
| 1051 | { |
|---|
| 1052 | // increment file descriptor refcount |
|---|
| 1053 | vfs_file_count_up( entry ); |
|---|
| 1054 | |
|---|
| 1055 | // copy entry in destination process fd_array |
|---|
| 1056 | hal_remote_s64( XPTR( dst_cxy , &dst_ptr->array[fd] ) , entry ); |
|---|
| 1057 | } |
|---|
| 1058 | } |
|---|
| 1059 | |
|---|
| 1060 | // release lock on source process fd_array |
|---|
| 1061 | remote_queuelock_release( XPTR( src_cxy , &src_ptr->lock ) ); |
|---|
| 1062 | |
|---|
| 1063 | } // end process_fd_remote_copy() |
|---|
| 1064 | |
|---|
| 1065 | |
|---|
| 1066 | //////////////////////////////////// |
|---|
| 1067 | bool_t process_fd_array_full( void ) |
|---|
| 1068 | { |
|---|
| 1069 | // get extended pointer on reference process |
|---|
| 1070 | xptr_t ref_xp = CURRENT_THREAD->process->ref_xp; |
|---|
| 1071 | |
|---|
| 1072 | // get reference process cluster and local pointer |
|---|
| 1073 | process_t * ref_ptr = GET_PTR( ref_xp ); |
|---|
| 1074 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
|---|
| 1075 | |
|---|
| 1076 | // get number of open file descriptors from reference fd_array |
|---|
| 1077 | uint32_t current = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->fd_array.current ) ); |
|---|
| 1078 | |
|---|
| 1079 | return ( current >= CONFIG_PROCESS_FILE_MAX_NR ); |
|---|
| 1080 | } |
|---|
| 1081 | |
|---|
| 1082 | |
|---|
| 1083 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1084 | // Thread related functions |
|---|
| 1085 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1086 | |
|---|
| 1087 | ///////////////////////////////////////////////////// |
|---|
| 1088 | error_t process_register_thread( process_t * process, |
|---|
| 1089 | thread_t * thread, |
|---|
| 1090 | trdid_t * trdid ) |
|---|
| 1091 | { |
|---|
| 1092 | ltid_t ltid; |
|---|
| 1093 | bool_t found = false; |
|---|
| 1094 | |
|---|
| 1095 | // check arguments |
|---|
| 1096 | assert( (process != NULL) , "process argument is NULL" ); |
|---|
| 1097 | assert( (thread != NULL) , "thread argument is NULL" ); |
|---|
| 1098 | |
|---|
| 1099 | // get the lock protecting th_tbl for all threads |
|---|
| 1100 | // but the idle thread executing kernel_init (cannot yield) |
|---|
| 1101 | if( thread->type != THREAD_IDLE ) rwlock_wr_acquire( &process->th_lock ); |
|---|
| 1102 | |
|---|
| 1103 | // scan th_tbl |
|---|
| 1104 | for( ltid = 0 ; ltid < CONFIG_THREADS_MAX_PER_CLUSTER ; ltid++ ) |
|---|
| 1105 | { |
|---|
| 1106 | if( process->th_tbl[ltid] == NULL ) |
|---|
| 1107 | { |
|---|
| 1108 | found = true; |
|---|
| 1109 | break; |
|---|
| 1110 | } |
|---|
| 1111 | } |
|---|
| 1112 | |
|---|
| 1113 | if( found ) |
|---|
| 1114 | { |
|---|
| 1115 | // register thread in th_tbl[] |
|---|
| 1116 | process->th_tbl[ltid] = thread; |
|---|
| 1117 | process->th_nr++; |
|---|
| 1118 | |
|---|
| 1119 | // returns trdid |
|---|
| 1120 | *trdid = TRDID( local_cxy , ltid ); |
|---|
| 1121 | |
|---|
| 1122 | // if( LPID_FROM_PID( process->pid ) == 0 ) |
|---|
| 1123 | // printk("\n@@@ %s : allocate ltid %d for a thread %s in cluster %x\n", |
|---|
| 1124 | // __FUNCTION__, ltid, thread_type_str( thread->type), local_cxy ); |
|---|
| 1125 | |
|---|
| 1126 | } |
|---|
| 1127 | |
|---|
| 1128 | // release the lock protecting th_tbl |
|---|
| 1129 | if( thread->type != THREAD_IDLE ) rwlock_wr_release( &process->th_lock ); |
|---|
| 1130 | |
|---|
| 1131 | return (found) ? 0 : 0xFFFFFFFF; |
|---|
| 1132 | |
|---|
| 1133 | } // end process_register_thread() |
|---|
| 1134 | |
|---|
| 1135 | ///////////////////////////////////////////////// |
|---|
| 1136 | bool_t process_remove_thread( thread_t * thread ) |
|---|
| 1137 | { |
|---|
| 1138 | uint32_t count; // number of threads in local process descriptor |
|---|
| 1139 | |
|---|
| 1140 | process_t * process = thread->process; |
|---|
| 1141 | |
|---|
| 1142 | // get thread local index |
|---|
| 1143 | ltid_t ltid = LTID_FROM_TRDID( thread->trdid ); |
|---|
| 1144 | |
|---|
| 1145 | // get the lock protecting th_tbl[] |
|---|
| 1146 | rwlock_wr_acquire( &process->th_lock ); |
|---|
| 1147 | |
|---|
| 1148 | // get number of threads |
|---|
| 1149 | count = process->th_nr; |
|---|
| 1150 | |
|---|
| 1151 | // check thread |
|---|
| 1152 | assert( (thread != NULL) , "thread argument is NULL" ); |
|---|
| 1153 | |
|---|
| 1154 | // check th_nr value |
|---|
| 1155 | assert( (count > 0) , "process th_nr cannot be 0\n" ); |
|---|
| 1156 | |
|---|
| 1157 | // remove thread from th_tbl[] |
|---|
| 1158 | process->th_tbl[ltid] = NULL; |
|---|
| 1159 | process->th_nr = count-1; |
|---|
| 1160 | |
|---|
| 1161 | // if( LPID_FROM_PID( process->pid ) == 0 ) |
|---|
| 1162 | // printk("\n@@@ %s : release ltid %d for a thread %s in cluster %x\n", |
|---|
| 1163 | // __FUNCTION__, ltid, thread_type_str( thread->type), local_cxy ); |
|---|
| 1164 | |
|---|
| 1165 | // release lock protecting th_tbl |
|---|
| 1166 | rwlock_wr_release( &process->th_lock ); |
|---|
| 1167 | |
|---|
| 1168 | return (count == 1); |
|---|
| 1169 | |
|---|
| 1170 | } // end process_remove_thread() |
|---|
| 1171 | |
|---|
| 1172 | ///////////////////////////////////////////////////////// |
|---|
| 1173 | error_t process_make_fork( xptr_t parent_process_xp, |
|---|
| 1174 | xptr_t parent_thread_xp, |
|---|
| 1175 | pid_t * child_pid, |
|---|
| 1176 | thread_t ** child_thread ) |
|---|
| 1177 | { |
|---|
| 1178 | process_t * process; // local pointer on child process descriptor |
|---|
| 1179 | thread_t * thread; // local pointer on child thread descriptor |
|---|
| 1180 | pid_t new_pid; // process identifier for child process |
|---|
| 1181 | pid_t parent_pid; // process identifier for parent process |
|---|
| 1182 | xptr_t ref_xp; // extended pointer on reference process |
|---|
| 1183 | xptr_t vfs_bin_xp; // extended pointer on .elf file |
|---|
| 1184 | error_t error; |
|---|
| 1185 | |
|---|
| 1186 | // get cluster and local pointer for parent process |
|---|
| 1187 | cxy_t parent_process_cxy = GET_CXY( parent_process_xp ); |
|---|
| 1188 | process_t * parent_process_ptr = GET_PTR( parent_process_xp ); |
|---|
| 1189 | |
|---|
| 1190 | // get parent process PID and extended pointer on .elf file |
|---|
| 1191 | parent_pid = hal_remote_l32 (XPTR( parent_process_cxy , &parent_process_ptr->pid)); |
|---|
| 1192 | vfs_bin_xp = hal_remote_l64(XPTR( parent_process_cxy , &parent_process_ptr->vfs_bin_xp)); |
|---|
| 1193 | |
|---|
| 1194 | // get extended pointer on reference process |
|---|
| 1195 | ref_xp = hal_remote_l64( XPTR( parent_process_cxy , &parent_process_ptr->ref_xp ) ); |
|---|
| 1196 | |
|---|
| 1197 | // check parent process is the reference process |
|---|
| 1198 | assert( (parent_process_xp == ref_xp ) , |
|---|
| 1199 | "parent process must be the reference process\n" ); |
|---|
| 1200 | |
|---|
| 1201 | #if DEBUG_PROCESS_MAKE_FORK |
|---|
| 1202 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1203 | thread_t * this = CURRENT_THREAD; |
|---|
| 1204 | trdid_t trdid = this->trdid; |
|---|
| 1205 | pid_t pid = this->process->pid; |
|---|
| 1206 | if( DEBUG_PROCESS_MAKE_FORK < cycle ) |
|---|
| 1207 | printk("\n[%s] thread[%x,%x] enter / cluster %x / cycle %d\n", |
|---|
| 1208 | __FUNCTION__, pid, trdid, local_cxy, cycle ); |
|---|
| 1209 | #endif |
|---|
| 1210 | |
|---|
| 1211 | // allocate a process descriptor |
|---|
| 1212 | process = process_alloc(); |
|---|
| 1213 | if( process == NULL ) |
|---|
| 1214 | { |
|---|
| 1215 | printk("\n[ERROR] in %s : cannot get process in cluster %x\n", |
|---|
| 1216 | __FUNCTION__, local_cxy ); |
|---|
| 1217 | return -1; |
|---|
| 1218 | } |
|---|
| 1219 | |
|---|
| 1220 | // allocate a child PID from local cluster |
|---|
| 1221 | error = cluster_pid_alloc( process , &new_pid ); |
|---|
| 1222 | if( error ) |
|---|
| 1223 | { |
|---|
| 1224 | printk("\n[ERROR] in %s : cannot get PID in cluster %x\n", |
|---|
| 1225 | __FUNCTION__, local_cxy ); |
|---|
| 1226 | process_free( process ); |
|---|
| 1227 | return -1; |
|---|
| 1228 | } |
|---|
| 1229 | |
|---|
| 1230 | #if( DEBUG_PROCESS_MAKE_FORK & 1 ) |
|---|
| 1231 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1232 | if( DEBUG_PROCESS_MAKE_FORK < cycle ) |
|---|
| 1233 | printk("\n[%s] thread[%x,%x] allocated process %x / cycle %d\n", |
|---|
| 1234 | __FUNCTION__, pid, trdid, new_pid, cycle ); |
|---|
| 1235 | #endif |
|---|
| 1236 | |
|---|
| 1237 | // initializes child process descriptor from parent process descriptor |
|---|
| 1238 | process_reference_init( process, |
|---|
| 1239 | new_pid, |
|---|
| 1240 | parent_process_xp ); |
|---|
| 1241 | |
|---|
| 1242 | #if( DEBUG_PROCESS_MAKE_FORK & 1 ) |
|---|
| 1243 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1244 | if( DEBUG_PROCESS_MAKE_FORK < cycle ) |
|---|
| 1245 | printk("\n[%s] thread[%x,%x] initialized child_process %x / cycle %d\n", |
|---|
| 1246 | __FUNCTION__, pid, trdid, new_pid, cycle ); |
|---|
| 1247 | #endif |
|---|
| 1248 | |
|---|
| 1249 | |
|---|
| 1250 | // copy VMM from parent descriptor to child descriptor |
|---|
| 1251 | error = vmm_fork_copy( process, |
|---|
| 1252 | parent_process_xp ); |
|---|
| 1253 | if( error ) |
|---|
| 1254 | { |
|---|
| 1255 | printk("\n[ERROR] in %s : cannot copy VMM in cluster %x\n", |
|---|
| 1256 | __FUNCTION__, local_cxy ); |
|---|
| 1257 | process_free( process ); |
|---|
| 1258 | cluster_pid_release( new_pid ); |
|---|
| 1259 | return -1; |
|---|
| 1260 | } |
|---|
| 1261 | |
|---|
| 1262 | #if( DEBUG_PROCESS_MAKE_FORK & 1 ) |
|---|
| 1263 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1264 | if( DEBUG_PROCESS_MAKE_FORK < cycle ) |
|---|
| 1265 | printk("\n[%s] thread[%x,%x] copied VMM from parent to child / cycle %d\n", |
|---|
| 1266 | __FUNCTION__, pid, trdid, cycle ); |
|---|
| 1267 | #endif |
|---|
| 1268 | |
|---|
| 1269 | // if parent_process is INIT, or if parent_process is the TXT owner, |
|---|
| 1270 | // the child_process becomes the owner of its TXT terminal |
|---|
| 1271 | if( (parent_pid == 1) || process_txt_is_owner( parent_process_xp ) ) |
|---|
| 1272 | { |
|---|
| 1273 | process_txt_set_ownership( XPTR( local_cxy , process ) ); |
|---|
| 1274 | |
|---|
| 1275 | #if( DEBUG_PROCESS_MAKE_FORK & 1 ) |
|---|
| 1276 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1277 | if( DEBUG_PROCESS_MAKE_EXEC < cycle ) |
|---|
| 1278 | printk("\n[%s] thread[%x,%x] / child takes TXT ownership / cycle %d\n", |
|---|
| 1279 | __FUNCTION__ , pid, trdid, cycle ); |
|---|
| 1280 | #endif |
|---|
| 1281 | |
|---|
| 1282 | } |
|---|
| 1283 | |
|---|
| 1284 | // update extended pointer on .elf file |
|---|
| 1285 | process->vfs_bin_xp = vfs_bin_xp; |
|---|
| 1286 | |
|---|
| 1287 | // create child thread descriptor from parent thread descriptor |
|---|
| 1288 | error = thread_user_fork( parent_thread_xp, |
|---|
| 1289 | process, |
|---|
| 1290 | &thread ); |
|---|
| 1291 | if( error ) |
|---|
| 1292 | { |
|---|
| 1293 | printk("\n[ERROR] in %s : cannot create thread in cluster %x\n", |
|---|
| 1294 | __FUNCTION__, local_cxy ); |
|---|
| 1295 | process_free( process ); |
|---|
| 1296 | cluster_pid_release( new_pid ); |
|---|
| 1297 | return -1; |
|---|
| 1298 | } |
|---|
| 1299 | |
|---|
| 1300 | // check main thread LTID |
|---|
| 1301 | assert( (LTID_FROM_TRDID(thread->trdid) == 0) , |
|---|
| 1302 | "main thread must have LTID == 0\n" ); |
|---|
| 1303 | |
|---|
| 1304 | #if( DEBUG_PROCESS_MAKE_FORK & 1 ) |
|---|
| 1305 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1306 | if( DEBUG_PROCESS_MAKE_FORK < cycle ) |
|---|
| 1307 | printk("\n[%s] thread[%x,%x] created main thread %x / cycle %d\n", |
|---|
| 1308 | __FUNCTION__, pid, trdid, thread, cycle ); |
|---|
| 1309 | #endif |
|---|
| 1310 | |
|---|
| 1311 | // set Copy_On_Write flag in parent process GPT |
|---|
| 1312 | // this includes all replicated GPT copies |
|---|
| 1313 | if( parent_process_cxy == local_cxy ) // reference is local |
|---|
| 1314 | { |
|---|
| 1315 | vmm_set_cow( parent_process_ptr ); |
|---|
| 1316 | } |
|---|
| 1317 | else // reference is remote |
|---|
| 1318 | { |
|---|
| 1319 | rpc_vmm_set_cow_client( parent_process_cxy, |
|---|
| 1320 | parent_process_ptr ); |
|---|
| 1321 | } |
|---|
| 1322 | |
|---|
| 1323 | // set Copy_On_Write flag in child process GPT |
|---|
| 1324 | vmm_set_cow( process ); |
|---|
| 1325 | |
|---|
| 1326 | #if( DEBUG_PROCESS_MAKE_FORK & 1 ) |
|---|
| 1327 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1328 | if( DEBUG_PROCESS_MAKE_FORK < cycle ) |
|---|
| 1329 | printk("\n[%s] thread[%x,%x] set COW in parent and child / cycle %d\n", |
|---|
| 1330 | __FUNCTION__, pid, trdid, cycle ); |
|---|
| 1331 | #endif |
|---|
| 1332 | |
|---|
| 1333 | // get extended pointers on parent children_root, children_lock and children_nr |
|---|
| 1334 | xptr_t children_root_xp = XPTR( parent_process_cxy , &parent_process_ptr->children_root ); |
|---|
| 1335 | xptr_t children_lock_xp = XPTR( parent_process_cxy , &parent_process_ptr->children_lock ); |
|---|
| 1336 | xptr_t children_nr_xp = XPTR( parent_process_cxy , &parent_process_ptr->children_nr ); |
|---|
| 1337 | |
|---|
| 1338 | // register process in parent children list |
|---|
| 1339 | remote_queuelock_acquire( children_lock_xp ); |
|---|
| 1340 | xlist_add_last( children_root_xp , XPTR( local_cxy , &process->children_list ) ); |
|---|
| 1341 | hal_remote_atomic_add( children_nr_xp , 1 ); |
|---|
| 1342 | remote_queuelock_release( children_lock_xp ); |
|---|
| 1343 | |
|---|
| 1344 | // return success |
|---|
| 1345 | *child_thread = thread; |
|---|
| 1346 | *child_pid = new_pid; |
|---|
| 1347 | |
|---|
| 1348 | #if DEBUG_PROCESS_MAKE_FORK |
|---|
| 1349 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1350 | if( DEBUG_PROCESS_MAKE_FORK < cycle ) |
|---|
| 1351 | printk("\n[%s] thread[%x,%x] exit / created process %x / cycle %d\n", |
|---|
| 1352 | __FUNCTION__, pid, trdid, new_pid, cycle ); |
|---|
| 1353 | #endif |
|---|
| 1354 | |
|---|
| 1355 | return 0; |
|---|
| 1356 | |
|---|
| 1357 | } // end process_make_fork() |
|---|
| 1358 | |
|---|
| 1359 | ///////////////////////////////////////////////////// |
|---|
| 1360 | error_t process_make_exec( exec_info_t * exec_info ) |
|---|
| 1361 | { |
|---|
| 1362 | thread_t * thread; // local pointer on this thread |
|---|
| 1363 | process_t * process; // local pointer on this process |
|---|
| 1364 | pid_t pid; // this process identifier |
|---|
| 1365 | error_t error; // value returned by called functions |
|---|
| 1366 | char * path; // path to .elf file |
|---|
| 1367 | xptr_t file_xp; // extended pointer on .elf file descriptor |
|---|
| 1368 | uint32_t file_id; // file index in fd_array |
|---|
| 1369 | uint32_t args_nr; // number of main thread arguments |
|---|
| 1370 | char ** args_pointers; // array of pointers on main thread arguments |
|---|
| 1371 | |
|---|
| 1372 | // get thread, process & PID |
|---|
| 1373 | thread = CURRENT_THREAD; |
|---|
| 1374 | process = thread->process; |
|---|
| 1375 | pid = process->pid; |
|---|
| 1376 | |
|---|
| 1377 | // get relevant infos from exec_info |
|---|
| 1378 | path = exec_info->path; |
|---|
| 1379 | args_nr = exec_info->args_nr; |
|---|
| 1380 | args_pointers = exec_info->args_pointers; |
|---|
| 1381 | |
|---|
| 1382 | #if DEBUG_PROCESS_MAKE_EXEC |
|---|
| 1383 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1384 | if( DEBUG_PROCESS_MAKE_EXEC < cycle ) |
|---|
| 1385 | printk("\n[%s] thread[%x,%x] enters for %s / cycle %d\n", |
|---|
| 1386 | __FUNCTION__, pid, thread->trdid, path, cycle ); |
|---|
| 1387 | #endif |
|---|
| 1388 | |
|---|
| 1389 | // open the file identified by <path> |
|---|
| 1390 | file_xp = XPTR_NULL; |
|---|
| 1391 | file_id = 0xFFFFFFFF; |
|---|
| 1392 | error = vfs_open( process, |
|---|
| 1393 | path, |
|---|
| 1394 | O_RDONLY, |
|---|
| 1395 | 0, |
|---|
| 1396 | &file_xp, |
|---|
| 1397 | &file_id ); |
|---|
| 1398 | if( error ) |
|---|
| 1399 | { |
|---|
| 1400 | printk("\n[ERROR] in %s : failed to open file <%s>\n", __FUNCTION__ , path ); |
|---|
| 1401 | return -1; |
|---|
| 1402 | } |
|---|
| 1403 | |
|---|
| 1404 | #if (DEBUG_PROCESS_MAKE_EXEC & 1) |
|---|
| 1405 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1406 | if( DEBUG_PROCESS_MAKE_EXEC < cycle ) |
|---|
| 1407 | printk("\n[%s] thread[%x,%x] opened file <%s> / cycle %d\n", |
|---|
| 1408 | __FUNCTION__, pid, thread->trdid, path, cycle ); |
|---|
| 1409 | #endif |
|---|
| 1410 | |
|---|
| 1411 | // delete all threads other than this main thread in all clusters |
|---|
| 1412 | process_sigaction( pid , DELETE_ALL_THREADS ); |
|---|
| 1413 | |
|---|
| 1414 | #if (DEBUG_PROCESS_MAKE_EXEC & 1) |
|---|
| 1415 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1416 | if( DEBUG_PROCESS_MAKE_EXEC < cycle ) |
|---|
| 1417 | printk("\n[%s] thread[%x,%x] deleted all threads / cycle %d\n", |
|---|
| 1418 | __FUNCTION__, pid, thread->trdid, cycle ); |
|---|
| 1419 | #endif |
|---|
| 1420 | |
|---|
| 1421 | // reset local process VMM |
|---|
| 1422 | vmm_destroy( process ); |
|---|
| 1423 | |
|---|
| 1424 | #if( DEBUG_PROCESS_MAKE_EXEC & 1 ) |
|---|
| 1425 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1426 | if( DEBUG_PROCESS_MAKE_EXEC < cycle ) |
|---|
| 1427 | printk("\n[%s] thread[%x,%x] reset VMM / cycle %d\n", |
|---|
| 1428 | __FUNCTION__, pid, thread->trdid, cycle ); |
|---|
| 1429 | #endif |
|---|
| 1430 | |
|---|
| 1431 | // re-initialize the VMM (kentry/args/envs vsegs registration) |
|---|
| 1432 | error = vmm_init( process ); |
|---|
| 1433 | if( error ) |
|---|
| 1434 | { |
|---|
| 1435 | printk("\n[ERROR] in %s : cannot initialise VMM for %s\n", __FUNCTION__ , path ); |
|---|
| 1436 | vfs_close( file_xp , file_id ); |
|---|
| 1437 | // FIXME restore old process VMM |
|---|
| 1438 | return -1; |
|---|
| 1439 | } |
|---|
| 1440 | |
|---|
| 1441 | #if( DEBUG_PROCESS_MAKE_EXEC & 1 ) |
|---|
| 1442 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1443 | if( DEBUG_PROCESS_MAKE_EXEC < cycle ) |
|---|
| 1444 | printk("\n[%s] thread[%x,%x] / kentry/args/envs vsegs registered / cycle %d\n", |
|---|
| 1445 | __FUNCTION__, pid, thread->trdid, cycle ); |
|---|
| 1446 | #endif |
|---|
| 1447 | |
|---|
| 1448 | // register code & data vsegs as well as entry-point in process VMM, |
|---|
| 1449 | // and register extended pointer on .elf file in process descriptor |
|---|
| 1450 | error = elf_load_process( file_xp , process ); |
|---|
| 1451 | if( error ) |
|---|
| 1452 | { |
|---|
| 1453 | printk("\n[ERROR] in %s : failed to access <%s>\n", __FUNCTION__ , path ); |
|---|
| 1454 | vfs_close( file_xp , file_id ); |
|---|
| 1455 | // FIXME restore old process VMM |
|---|
| 1456 | return -1; |
|---|
| 1457 | } |
|---|
| 1458 | |
|---|
| 1459 | #if( DEBUG_PROCESS_MAKE_EXEC & 1 ) |
|---|
| 1460 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1461 | if( DEBUG_PROCESS_MAKE_EXEC < cycle ) |
|---|
| 1462 | printk("\n[%s] thread[%x,%x] / code/data vsegs registered / cycle %d\n", |
|---|
| 1463 | __FUNCTION__, pid, thread->trdid, cycle ); |
|---|
| 1464 | #endif |
|---|
| 1465 | |
|---|
| 1466 | // update the existing main thread descriptor... and jump to user code |
|---|
| 1467 | error = thread_user_exec( (void *)process->vmm.entry_point, |
|---|
| 1468 | args_nr, |
|---|
| 1469 | args_pointers ); |
|---|
| 1470 | if( error ) |
|---|
| 1471 | { |
|---|
| 1472 | printk("\n[ERROR] in %s : cannot update main thread for %s\n", __FUNCTION__ , path ); |
|---|
| 1473 | vfs_close( file_xp , file_id ); |
|---|
| 1474 | // FIXME restore old process VMM |
|---|
| 1475 | return -1; |
|---|
| 1476 | } |
|---|
| 1477 | |
|---|
| 1478 | assert( false, "we should not execute this code"); |
|---|
| 1479 | |
|---|
| 1480 | return 0; |
|---|
| 1481 | |
|---|
| 1482 | } // end process_make_exec() |
|---|
| 1483 | |
|---|
| 1484 | |
|---|
| 1485 | /////////////////////////////////////////////// |
|---|
| 1486 | void process_zero_create( process_t * process ) |
|---|
| 1487 | { |
|---|
| 1488 | error_t error; |
|---|
| 1489 | pid_t pid; |
|---|
| 1490 | |
|---|
| 1491 | #if DEBUG_PROCESS_ZERO_CREATE |
|---|
| 1492 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1493 | if( DEBUG_PROCESS_ZERO_CREATE < cycle ) |
|---|
| 1494 | printk("\n[%s] enter / cluster %x / cycle %d\n", |
|---|
| 1495 | __FUNCTION__, local_cxy, cycle ); |
|---|
| 1496 | #endif |
|---|
| 1497 | |
|---|
| 1498 | // get PID from local cluster manager for this kernel process |
|---|
| 1499 | error = cluster_pid_alloc( process , &pid ); |
|---|
| 1500 | |
|---|
| 1501 | if( error || (LPID_FROM_PID( pid ) != 0) ) |
|---|
| 1502 | { |
|---|
| 1503 | printk("\n[PANIC] in %s : cannot get valid PID in cluster %x / PID = %x\n", |
|---|
| 1504 | __FUNCTION__ , local_cxy, pid ); |
|---|
| 1505 | hal_core_sleep(); |
|---|
| 1506 | } |
|---|
| 1507 | |
|---|
| 1508 | // initialize PID, REF_XP, PARENT_XP, and STATE |
|---|
| 1509 | // the kernel process_zero is its own parent_process, |
|---|
| 1510 | // reference_process, and owner_process, and cannot be killed... |
|---|
| 1511 | process->pid = pid; |
|---|
| 1512 | process->ref_xp = XPTR( local_cxy , process ); |
|---|
| 1513 | process->owner_xp = XPTR( local_cxy , process ); |
|---|
| 1514 | process->parent_xp = XPTR( local_cxy , process ); |
|---|
| 1515 | process->term_state = 0; |
|---|
| 1516 | |
|---|
| 1517 | // reset th_tbl[] array and associated fields |
|---|
| 1518 | uint32_t i; |
|---|
| 1519 | for( i = 0 ; i < CONFIG_THREADS_MAX_PER_CLUSTER ; i++ ) |
|---|
| 1520 | { |
|---|
| 1521 | process->th_tbl[i] = NULL; |
|---|
| 1522 | } |
|---|
| 1523 | process->th_nr = 0; |
|---|
| 1524 | rwlock_init( &process->th_lock , LOCK_PROCESS_THTBL ); |
|---|
| 1525 | |
|---|
| 1526 | |
|---|
| 1527 | // reset children list as empty |
|---|
| 1528 | xlist_root_init( XPTR( local_cxy , &process->children_root ) ); |
|---|
| 1529 | process->children_nr = 0; |
|---|
| 1530 | remote_queuelock_init( XPTR( local_cxy , &process->children_lock ), |
|---|
| 1531 | LOCK_PROCESS_CHILDREN ); |
|---|
| 1532 | |
|---|
| 1533 | // register kernel process in cluster manager local_list |
|---|
| 1534 | cluster_process_local_link( process ); |
|---|
| 1535 | |
|---|
| 1536 | hal_fence(); |
|---|
| 1537 | |
|---|
| 1538 | #if DEBUG_PROCESS_ZERO_CREATE |
|---|
| 1539 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1540 | if( DEBUG_PROCESS_ZERO_CREATE < cycle ) |
|---|
| 1541 | printk("\n[%s] exit / cluster %x / cycle %d\n", |
|---|
| 1542 | __FUNCTION__, local_cxy, cycle ); |
|---|
| 1543 | #endif |
|---|
| 1544 | |
|---|
| 1545 | } // end process_zero_init() |
|---|
| 1546 | |
|---|
| 1547 | //////////////////////////////// |
|---|
| 1548 | void process_init_create( void ) |
|---|
| 1549 | { |
|---|
| 1550 | process_t * process; // local pointer on process descriptor |
|---|
| 1551 | pid_t pid; // process_init identifier |
|---|
| 1552 | thread_t * thread; // local pointer on main thread |
|---|
| 1553 | pthread_attr_t attr; // main thread attributes |
|---|
| 1554 | lid_t lid; // selected core local index for main thread |
|---|
| 1555 | xptr_t file_xp; // extended pointer on .elf file descriptor |
|---|
| 1556 | uint32_t file_id; // file index in fd_array |
|---|
| 1557 | error_t error; |
|---|
| 1558 | |
|---|
| 1559 | #if DEBUG_PROCESS_INIT_CREATE |
|---|
| 1560 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1561 | if( DEBUG_PROCESS_INIT_CREATE < cycle ) |
|---|
| 1562 | printk("\n[%s] thread %x in process %x enter / cycle %d\n", |
|---|
| 1563 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle ); |
|---|
| 1564 | #endif |
|---|
| 1565 | |
|---|
| 1566 | // allocates memory for process descriptor from local cluster |
|---|
| 1567 | process = process_alloc(); |
|---|
| 1568 | |
|---|
| 1569 | // check memory allocator |
|---|
| 1570 | assert( (process != NULL), |
|---|
| 1571 | "no memory for process descriptor in cluster %x\n", local_cxy ); |
|---|
| 1572 | |
|---|
| 1573 | // get PID from local cluster |
|---|
| 1574 | error = cluster_pid_alloc( process , &pid ); |
|---|
| 1575 | |
|---|
| 1576 | // check PID allocator |
|---|
| 1577 | assert( (error == 0), |
|---|
| 1578 | "cannot allocate PID in cluster %x\n", local_cxy ); |
|---|
| 1579 | |
|---|
| 1580 | // check PID value |
|---|
| 1581 | assert( (pid == 1) , |
|---|
| 1582 | "process INIT must be first process in cluster 0\n" ); |
|---|
| 1583 | |
|---|
| 1584 | // initialize process descriptor / parent is local process_zero |
|---|
| 1585 | process_reference_init( process, |
|---|
| 1586 | pid, |
|---|
| 1587 | XPTR( local_cxy , &process_zero ) ); |
|---|
| 1588 | |
|---|
| 1589 | #if(DEBUG_PROCESS_INIT_CREATE & 1) |
|---|
| 1590 | if( DEBUG_PROCESS_INIT_CREATE < cycle ) |
|---|
| 1591 | printk("\n[%s] thread %x in process %x initialized process descriptor\n", |
|---|
| 1592 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid ); |
|---|
| 1593 | #endif |
|---|
| 1594 | |
|---|
| 1595 | // open the file identified by CONFIG_PROCESS_INIT_PATH |
|---|
| 1596 | file_xp = XPTR_NULL; |
|---|
| 1597 | file_id = -1; |
|---|
| 1598 | error = vfs_open( process, |
|---|
| 1599 | CONFIG_PROCESS_INIT_PATH, |
|---|
| 1600 | O_RDONLY, |
|---|
| 1601 | 0, |
|---|
| 1602 | &file_xp, |
|---|
| 1603 | &file_id ); |
|---|
| 1604 | |
|---|
| 1605 | assert( (error == 0), |
|---|
| 1606 | "failed to open file <%s>\n", CONFIG_PROCESS_INIT_PATH ); |
|---|
| 1607 | |
|---|
| 1608 | #if(DEBUG_PROCESS_INIT_CREATE & 1) |
|---|
| 1609 | if( DEBUG_PROCESS_INIT_CREATE < cycle ) |
|---|
| 1610 | printk("\n[%s] thread %x in process %x open .elf file decriptor\n", |
|---|
| 1611 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid ); |
|---|
| 1612 | #endif |
|---|
| 1613 | |
|---|
| 1614 | // register "code" and "data" vsegs as well as entry-point |
|---|
| 1615 | // in process VMM, using information contained in the elf file. |
|---|
| 1616 | error = elf_load_process( file_xp , process ); |
|---|
| 1617 | |
|---|
| 1618 | assert( (error == 0), |
|---|
| 1619 | "cannot access .elf file <%s>\n", CONFIG_PROCESS_INIT_PATH ); |
|---|
| 1620 | |
|---|
| 1621 | #if(DEBUG_PROCESS_INIT_CREATE & 1) |
|---|
| 1622 | if( DEBUG_PROCESS_INIT_CREATE < cycle ) |
|---|
| 1623 | printk("\n[%s] thread %x in process %x registered code/data vsegs in VMM\n", |
|---|
| 1624 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid ); |
|---|
| 1625 | #endif |
|---|
| 1626 | |
|---|
| 1627 | // get extended pointers on process_zero children_root, children_lock |
|---|
| 1628 | xptr_t children_root_xp = XPTR( local_cxy , &process_zero.children_root ); |
|---|
| 1629 | xptr_t children_lock_xp = XPTR( local_cxy , &process_zero.children_lock ); |
|---|
| 1630 | |
|---|
| 1631 | // take lock protecting kernel process children list |
|---|
| 1632 | remote_queuelock_acquire( children_lock_xp ); |
|---|
| 1633 | |
|---|
| 1634 | // register process INIT in parent local process_zero |
|---|
| 1635 | xlist_add_last( children_root_xp , XPTR( local_cxy , &process->children_list ) ); |
|---|
| 1636 | hal_atomic_add( &process_zero.children_nr , 1 ); |
|---|
| 1637 | |
|---|
| 1638 | // release lock protecting kernel process children list |
|---|
| 1639 | remote_queuelock_release( children_lock_xp ); |
|---|
| 1640 | |
|---|
| 1641 | #if(DEBUG_PROCESS_INIT_CREATE & 1) |
|---|
| 1642 | if( DEBUG_PROCESS_INIT_CREATE < cycle ) |
|---|
| 1643 | printk("\n[%s] thread %x in process %x registered init process in parent\n", |
|---|
| 1644 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid ); |
|---|
| 1645 | #endif |
|---|
| 1646 | |
|---|
| 1647 | // select a core in local cluster to execute the main thread |
|---|
| 1648 | lid = cluster_select_local_core(); |
|---|
| 1649 | |
|---|
| 1650 | // initialize pthread attributes for main thread |
|---|
| 1651 | attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED | PT_ATTR_CORE_DEFINED; |
|---|
| 1652 | attr.cxy = local_cxy; |
|---|
| 1653 | attr.lid = lid; |
|---|
| 1654 | |
|---|
| 1655 | // create and initialize thread descriptor |
|---|
| 1656 | error = thread_user_create( pid, |
|---|
| 1657 | (void *)process->vmm.entry_point, |
|---|
| 1658 | NULL, |
|---|
| 1659 | &attr, |
|---|
| 1660 | &thread ); |
|---|
| 1661 | |
|---|
| 1662 | assert( (error == 0), |
|---|
| 1663 | "cannot create main thread for <%s>\n", CONFIG_PROCESS_INIT_PATH ); |
|---|
| 1664 | |
|---|
| 1665 | assert( (thread->trdid == 0), |
|---|
| 1666 | "main thread must have index 0 for <%s>\n", CONFIG_PROCESS_INIT_PATH ); |
|---|
| 1667 | |
|---|
| 1668 | #if(DEBUG_PROCESS_INIT_CREATE & 1) |
|---|
| 1669 | if( DEBUG_PROCESS_INIT_CREATE < cycle ) |
|---|
| 1670 | printk("\n[%s] thread %x in process %x created main thread\n", |
|---|
| 1671 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid ); |
|---|
| 1672 | #endif |
|---|
| 1673 | |
|---|
| 1674 | // activate thread |
|---|
| 1675 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL ); |
|---|
| 1676 | |
|---|
| 1677 | hal_fence(); |
|---|
| 1678 | |
|---|
| 1679 | #if DEBUG_PROCESS_INIT_CREATE |
|---|
| 1680 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1681 | if( DEBUG_PROCESS_INIT_CREATE < cycle ) |
|---|
| 1682 | printk("\n[%s] thread %x in process %x exit / cycle %d\n", |
|---|
| 1683 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle ); |
|---|
| 1684 | #endif |
|---|
| 1685 | |
|---|
| 1686 | } // end process_init_create() |
|---|
| 1687 | |
|---|
| 1688 | ///////////////////////////////////////// |
|---|
| 1689 | void process_display( xptr_t process_xp ) |
|---|
| 1690 | { |
|---|
| 1691 | process_t * process_ptr; |
|---|
| 1692 | cxy_t process_cxy; |
|---|
| 1693 | |
|---|
| 1694 | xptr_t parent_xp; // extended pointer on parent process |
|---|
| 1695 | process_t * parent_ptr; |
|---|
| 1696 | cxy_t parent_cxy; |
|---|
| 1697 | |
|---|
| 1698 | xptr_t owner_xp; // extended pointer on owner process |
|---|
| 1699 | process_t * owner_ptr; |
|---|
| 1700 | cxy_t owner_cxy; |
|---|
| 1701 | |
|---|
| 1702 | pid_t pid; |
|---|
| 1703 | pid_t ppid; |
|---|
| 1704 | lpid_t lpid; |
|---|
| 1705 | uint32_t state; |
|---|
| 1706 | uint32_t th_nr; |
|---|
| 1707 | |
|---|
| 1708 | xptr_t txt_file_xp; // extended pointer on TXT_RX file descriptor |
|---|
| 1709 | xptr_t txt_chdev_xp; // extended pointer on TXT_RX chdev |
|---|
| 1710 | chdev_t * txt_chdev_ptr; |
|---|
| 1711 | cxy_t txt_chdev_cxy; |
|---|
| 1712 | xptr_t txt_owner_xp; // extended pointer on TXT owner process |
|---|
| 1713 | |
|---|
| 1714 | xptr_t elf_file_xp; // extended pointer on .elf file |
|---|
| 1715 | cxy_t elf_file_cxy; |
|---|
| 1716 | vfs_file_t * elf_file_ptr; |
|---|
| 1717 | vfs_inode_t * elf_inode_ptr; // local pointer on .elf inode |
|---|
| 1718 | |
|---|
| 1719 | char txt_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
|---|
| 1720 | char elf_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
|---|
| 1721 | |
|---|
| 1722 | // get cluster and local pointer on process |
|---|
| 1723 | process_ptr = GET_PTR( process_xp ); |
|---|
| 1724 | process_cxy = GET_CXY( process_xp ); |
|---|
| 1725 | |
|---|
| 1726 | // get process PID, LPID, and state |
|---|
| 1727 | pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) ); |
|---|
| 1728 | lpid = LPID_FROM_PID( pid ); |
|---|
| 1729 | state = hal_remote_l32( XPTR( process_cxy , &process_ptr->term_state ) ); |
|---|
| 1730 | |
|---|
| 1731 | // get process PPID |
|---|
| 1732 | parent_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->parent_xp ) ); |
|---|
| 1733 | parent_cxy = GET_CXY( parent_xp ); |
|---|
| 1734 | parent_ptr = GET_PTR( parent_xp ); |
|---|
| 1735 | ppid = hal_remote_l32( XPTR( parent_cxy , &parent_ptr->pid ) ); |
|---|
| 1736 | |
|---|
| 1737 | // get number of threads |
|---|
| 1738 | th_nr = hal_remote_l32( XPTR( process_cxy , &process_ptr->th_nr ) ); |
|---|
| 1739 | |
|---|
| 1740 | // get pointers on owner process descriptor |
|---|
| 1741 | owner_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ) ); |
|---|
| 1742 | owner_cxy = GET_CXY( owner_xp ); |
|---|
| 1743 | owner_ptr = GET_PTR( owner_xp ); |
|---|
| 1744 | |
|---|
| 1745 | // get process TXT name and .elf name |
|---|
| 1746 | if( lpid ) // user process |
|---|
| 1747 | { |
|---|
| 1748 | |
|---|
| 1749 | // get extended pointer on file descriptor associated to TXT_RX |
|---|
| 1750 | txt_file_xp = hal_remote_l64( XPTR( owner_cxy , &owner_ptr->fd_array.array[0] ) ); |
|---|
| 1751 | |
|---|
| 1752 | assert( (txt_file_xp != XPTR_NULL) , |
|---|
| 1753 | "process must be attached to one TXT terminal\n" ); |
|---|
| 1754 | |
|---|
| 1755 | // get TXT_RX chdev pointers |
|---|
| 1756 | txt_chdev_xp = chdev_from_file( txt_file_xp ); |
|---|
| 1757 | txt_chdev_cxy = GET_CXY( txt_chdev_xp ); |
|---|
| 1758 | txt_chdev_ptr = GET_PTR( txt_chdev_xp ); |
|---|
| 1759 | |
|---|
| 1760 | // get TXT_RX name and ownership |
|---|
| 1761 | hal_remote_strcpy( XPTR( local_cxy , txt_name ) , |
|---|
| 1762 | XPTR( txt_chdev_cxy , txt_chdev_ptr->name ) ); |
|---|
| 1763 | |
|---|
| 1764 | txt_owner_xp = (xptr_t)hal_remote_l64( XPTR( txt_chdev_cxy, |
|---|
| 1765 | &txt_chdev_ptr->ext.txt.owner_xp ) ); |
|---|
| 1766 | |
|---|
| 1767 | // get process .elf name |
|---|
| 1768 | elf_file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->vfs_bin_xp ) ); |
|---|
| 1769 | elf_file_cxy = GET_CXY( elf_file_xp ); |
|---|
| 1770 | elf_file_ptr = GET_PTR( elf_file_xp ); |
|---|
| 1771 | elf_inode_ptr = hal_remote_lpt( XPTR( elf_file_cxy , &elf_file_ptr->inode ) ); |
|---|
| 1772 | vfs_inode_get_name( XPTR( elf_file_cxy , elf_inode_ptr ) , elf_name ); |
|---|
| 1773 | } |
|---|
| 1774 | else // kernel process_zero |
|---|
| 1775 | { |
|---|
| 1776 | // TXT name and .elf name are not registered in kernel process_zero |
|---|
| 1777 | strcpy( txt_name , "txt0_rx" ); |
|---|
| 1778 | txt_owner_xp = process_xp; |
|---|
| 1779 | strcpy( elf_name , "kernel.elf" ); |
|---|
| 1780 | } |
|---|
| 1781 | |
|---|
| 1782 | // display process info |
|---|
| 1783 | if( txt_owner_xp == process_xp ) |
|---|
| 1784 | { |
|---|
| 1785 | nolock_printk("PID %X | %s (FG) | %X | PPID %X | TS %X | %d | %s\n", |
|---|
| 1786 | pid, txt_name, process_ptr, ppid, state, th_nr, elf_name ); |
|---|
| 1787 | } |
|---|
| 1788 | else |
|---|
| 1789 | { |
|---|
| 1790 | nolock_printk("PID %X | %s (BG) | %X | PPID %X | TS %X | %d | %s\n", |
|---|
| 1791 | pid, txt_name, process_ptr, ppid, state, th_nr, elf_name ); |
|---|
| 1792 | } |
|---|
| 1793 | } // end process_display() |
|---|
| 1794 | |
|---|
| 1795 | |
|---|
| 1796 | //////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1797 | // Terminals related functions |
|---|
| 1798 | //////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1799 | |
|---|
| 1800 | ////////////////////////////////// |
|---|
| 1801 | uint32_t process_txt_alloc( void ) |
|---|
| 1802 | { |
|---|
| 1803 | uint32_t index; // TXT terminal index |
|---|
| 1804 | xptr_t chdev_xp; // extended pointer on TXT_RX chdev |
|---|
| 1805 | chdev_t * chdev_ptr; // local pointer on TXT_RX chdev |
|---|
| 1806 | cxy_t chdev_cxy; // TXT_RX chdev cluster |
|---|
| 1807 | xptr_t root_xp; // extended pointer on owner field in chdev |
|---|
| 1808 | |
|---|
| 1809 | // scan the user TXT_RX chdevs (TXT0 is reserved for kernel) |
|---|
| 1810 | for( index = 1 ; index < LOCAL_CLUSTER->nb_txt_channels ; index ++ ) |
|---|
| 1811 | { |
|---|
| 1812 | // get pointers on TXT_RX[index] |
|---|
| 1813 | chdev_xp = chdev_dir.txt_rx[index]; |
|---|
| 1814 | chdev_cxy = GET_CXY( chdev_xp ); |
|---|
| 1815 | chdev_ptr = GET_PTR( chdev_xp ); |
|---|
| 1816 | |
|---|
| 1817 | // get extended pointer on root of attached process |
|---|
| 1818 | root_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.root ); |
|---|
| 1819 | |
|---|
| 1820 | // return free TXT index if found |
|---|
| 1821 | if( xlist_is_empty( root_xp ) ) return index; |
|---|
| 1822 | } |
|---|
| 1823 | |
|---|
| 1824 | assert( false , "no free TXT terminal found" ); |
|---|
| 1825 | |
|---|
| 1826 | return -1; |
|---|
| 1827 | |
|---|
| 1828 | } // end process_txt_alloc() |
|---|
| 1829 | |
|---|
| 1830 | ///////////////////////////////////////////// |
|---|
| 1831 | void process_txt_attach( process_t * process, |
|---|
| 1832 | uint32_t txt_id ) |
|---|
| 1833 | { |
|---|
| 1834 | xptr_t chdev_xp; // extended pointer on TXT_RX chdev |
|---|
| 1835 | cxy_t chdev_cxy; // TXT_RX chdev cluster |
|---|
| 1836 | chdev_t * chdev_ptr; // local pointer on TXT_RX chdev |
|---|
| 1837 | xptr_t root_xp; // extended pointer on list root in chdev |
|---|
| 1838 | xptr_t lock_xp; // extended pointer on list lock in chdev |
|---|
| 1839 | |
|---|
| 1840 | // check process is in owner cluster |
|---|
| 1841 | assert( (CXY_FROM_PID( process->pid ) == local_cxy) , |
|---|
| 1842 | "process descriptor not in owner cluster" ); |
|---|
| 1843 | |
|---|
| 1844 | // check terminal index |
|---|
| 1845 | assert( (txt_id < LOCAL_CLUSTER->nb_txt_channels) , |
|---|
| 1846 | "illegal TXT terminal index" ); |
|---|
| 1847 | |
|---|
| 1848 | // get pointers on TXT_RX[txt_id] chdev |
|---|
| 1849 | chdev_xp = chdev_dir.txt_rx[txt_id]; |
|---|
| 1850 | chdev_cxy = GET_CXY( chdev_xp ); |
|---|
| 1851 | chdev_ptr = GET_PTR( chdev_xp ); |
|---|
| 1852 | |
|---|
| 1853 | // get extended pointer on root & lock of attached process list |
|---|
| 1854 | root_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.root ); |
|---|
| 1855 | lock_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.lock ); |
|---|
| 1856 | |
|---|
| 1857 | // get lock protecting list of processes attached to TXT |
|---|
| 1858 | remote_busylock_acquire( lock_xp ); |
|---|
| 1859 | |
|---|
| 1860 | // insert process in attached process list |
|---|
| 1861 | xlist_add_last( root_xp , XPTR( local_cxy , &process->txt_list ) ); |
|---|
| 1862 | |
|---|
| 1863 | // release lock protecting list of processes attached to TXT |
|---|
| 1864 | remote_busylock_release( lock_xp ); |
|---|
| 1865 | |
|---|
| 1866 | #if DEBUG_PROCESS_TXT |
|---|
| 1867 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1868 | if( DEBUG_PROCESS_TXT < cycle ) |
|---|
| 1869 | printk("\n[%s] thread %x in process %x attached process %x to TXT %d / cycle %d\n", |
|---|
| 1870 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, |
|---|
| 1871 | process->pid, txt_id , cycle ); |
|---|
| 1872 | #endif |
|---|
| 1873 | |
|---|
| 1874 | } // end process_txt_attach() |
|---|
| 1875 | |
|---|
| 1876 | ///////////////////////////////////////////// |
|---|
| 1877 | void process_txt_detach( xptr_t process_xp ) |
|---|
| 1878 | { |
|---|
| 1879 | process_t * process_ptr; // local pointer on process in owner cluster |
|---|
| 1880 | cxy_t process_cxy; // process owner cluster |
|---|
| 1881 | pid_t process_pid; // process identifier |
|---|
| 1882 | xptr_t file_xp; // extended pointer on stdin file |
|---|
| 1883 | xptr_t chdev_xp; // extended pointer on TXT_RX chdev |
|---|
| 1884 | cxy_t chdev_cxy; // TXT_RX chdev cluster |
|---|
| 1885 | chdev_t * chdev_ptr; // local pointer on TXT_RX chdev |
|---|
| 1886 | xptr_t lock_xp; // extended pointer on list lock in chdev |
|---|
| 1887 | |
|---|
| 1888 | // get process cluster, local pointer, and PID |
|---|
| 1889 | process_cxy = GET_CXY( process_xp ); |
|---|
| 1890 | process_ptr = GET_PTR( process_xp ); |
|---|
| 1891 | process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) ); |
|---|
| 1892 | |
|---|
| 1893 | // check process descriptor in owner cluster |
|---|
| 1894 | assert( (CXY_FROM_PID( process_pid ) == process_cxy ) , |
|---|
| 1895 | "process descriptor not in owner cluster" ); |
|---|
| 1896 | |
|---|
| 1897 | // release TXT ownership (does nothing if not TXT owner) |
|---|
| 1898 | process_txt_transfer_ownership( process_xp ); |
|---|
| 1899 | |
|---|
| 1900 | // get extended pointer on process stdin file |
|---|
| 1901 | file_xp = (xptr_t)hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) ); |
|---|
| 1902 | |
|---|
| 1903 | // get pointers on TXT_RX chdev |
|---|
| 1904 | chdev_xp = chdev_from_file( file_xp ); |
|---|
| 1905 | chdev_cxy = GET_CXY( chdev_xp ); |
|---|
| 1906 | chdev_ptr = (chdev_t *)GET_PTR( chdev_xp ); |
|---|
| 1907 | |
|---|
| 1908 | // get extended pointer on lock protecting attached process list |
|---|
| 1909 | lock_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.lock ); |
|---|
| 1910 | |
|---|
| 1911 | // get lock protecting list of processes attached to TXT |
|---|
| 1912 | remote_busylock_acquire( lock_xp ); |
|---|
| 1913 | |
|---|
| 1914 | // unlink process from attached process list |
|---|
| 1915 | xlist_unlink( XPTR( process_cxy , &process_ptr->txt_list ) ); |
|---|
| 1916 | |
|---|
| 1917 | // release lock protecting list of processes attached to TXT |
|---|
| 1918 | remote_busylock_release( lock_xp ); |
|---|
| 1919 | |
|---|
| 1920 | #if DEBUG_PROCESS_TXT |
|---|
| 1921 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1922 | uint32_t txt_id = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) ); |
|---|
| 1923 | if( DEBUG_PROCESS_TXT < cycle ) |
|---|
| 1924 | printk("\n[%s] thread %x in process %x detached process %x from TXT %d / cycle %d\n", |
|---|
| 1925 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, |
|---|
| 1926 | process_pid, txt_id, cycle ); |
|---|
| 1927 | #endif |
|---|
| 1928 | |
|---|
| 1929 | } // end process_txt_detach() |
|---|
| 1930 | |
|---|
| 1931 | /////////////////////////////////////////////////// |
|---|
| 1932 | void process_txt_set_ownership( xptr_t process_xp ) |
|---|
| 1933 | { |
|---|
| 1934 | process_t * process_ptr; |
|---|
| 1935 | cxy_t process_cxy; |
|---|
| 1936 | pid_t process_pid; |
|---|
| 1937 | xptr_t file_xp; |
|---|
| 1938 | xptr_t txt_xp; |
|---|
| 1939 | chdev_t * txt_ptr; |
|---|
| 1940 | cxy_t txt_cxy; |
|---|
| 1941 | |
|---|
| 1942 | // get pointers on process in owner cluster |
|---|
| 1943 | process_cxy = GET_CXY( process_xp ); |
|---|
| 1944 | process_ptr = GET_PTR( process_xp ); |
|---|
| 1945 | process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) ); |
|---|
| 1946 | |
|---|
| 1947 | // check owner cluster |
|---|
| 1948 | assert( (process_cxy == CXY_FROM_PID( process_pid )) , |
|---|
| 1949 | "process descriptor not in owner cluster\n" ); |
|---|
| 1950 | |
|---|
| 1951 | // get extended pointer on stdin pseudo file |
|---|
| 1952 | file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) ); |
|---|
| 1953 | |
|---|
| 1954 | // get pointers on TXT chdev |
|---|
| 1955 | txt_xp = chdev_from_file( file_xp ); |
|---|
| 1956 | txt_cxy = GET_CXY( txt_xp ); |
|---|
| 1957 | txt_ptr = GET_PTR( txt_xp ); |
|---|
| 1958 | |
|---|
| 1959 | // set owner field in TXT chdev |
|---|
| 1960 | hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , process_xp ); |
|---|
| 1961 | |
|---|
| 1962 | #if DEBUG_PROCESS_TXT |
|---|
| 1963 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1964 | uint32_t txt_id = hal_remote_l32( XPTR( txt_cxy , &txt_ptr->channel ) ); |
|---|
| 1965 | if( DEBUG_PROCESS_TXT < cycle ) |
|---|
| 1966 | printk("\n[%s] thread %x in process %x give TXT %d to process %x / cycle %d\n", |
|---|
| 1967 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, process_pid, cycle ); |
|---|
| 1968 | #endif |
|---|
| 1969 | |
|---|
| 1970 | } // end process_txt_set ownership() |
|---|
| 1971 | |
|---|
| 1972 | //////////////////////////////////////////////////////// |
|---|
| 1973 | void process_txt_transfer_ownership( xptr_t process_xp ) |
|---|
| 1974 | { |
|---|
| 1975 | process_t * process_ptr; // local pointer on process releasing ownership |
|---|
| 1976 | cxy_t process_cxy; // process cluster |
|---|
| 1977 | pid_t process_pid; // process identifier |
|---|
| 1978 | xptr_t file_xp; // extended pointer on TXT_RX pseudo file |
|---|
| 1979 | xptr_t txt_xp; // extended pointer on TXT_RX chdev |
|---|
| 1980 | chdev_t * txt_ptr; // local pointer on TXT_RX chdev |
|---|
| 1981 | cxy_t txt_cxy; // cluster of TXT_RX chdev |
|---|
| 1982 | uint32_t txt_id; // TXT_RX channel |
|---|
| 1983 | xptr_t owner_xp; // extended pointer on current TXT_RX owner |
|---|
| 1984 | xptr_t root_xp; // extended pointer on root of attached process list |
|---|
| 1985 | xptr_t lock_xp; // extended pointer on lock protecting attached process list |
|---|
| 1986 | xptr_t iter_xp; // iterator for xlist |
|---|
| 1987 | xptr_t current_xp; // extended pointer on current process |
|---|
| 1988 | process_t * current_ptr; // local pointer on current process |
|---|
| 1989 | cxy_t current_cxy; // cluster for current process |
|---|
| 1990 | |
|---|
| 1991 | #if DEBUG_PROCESS_TXT |
|---|
| 1992 | uint32_t cycle; |
|---|
| 1993 | #endif |
|---|
| 1994 | |
|---|
| 1995 | // get pointers on process in owner cluster |
|---|
| 1996 | process_cxy = GET_CXY( process_xp ); |
|---|
| 1997 | process_ptr = GET_PTR( process_xp ); |
|---|
| 1998 | process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) ); |
|---|
| 1999 | |
|---|
| 2000 | // check owner cluster |
|---|
| 2001 | assert( (process_cxy == CXY_FROM_PID( process_pid )) , |
|---|
| 2002 | "process descriptor not in owner cluster\n" ); |
|---|
| 2003 | |
|---|
| 2004 | // get extended pointer on stdin pseudo file |
|---|
| 2005 | file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) ); |
|---|
| 2006 | |
|---|
| 2007 | // get pointers on TXT chdev |
|---|
| 2008 | txt_xp = chdev_from_file( file_xp ); |
|---|
| 2009 | txt_cxy = GET_CXY( txt_xp ); |
|---|
| 2010 | txt_ptr = GET_PTR( txt_xp ); |
|---|
| 2011 | |
|---|
| 2012 | // get extended pointer on TXT_RX owner and TXT channel |
|---|
| 2013 | owner_xp = hal_remote_l64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) ); |
|---|
| 2014 | txt_id = hal_remote_l32 ( XPTR( txt_cxy , &txt_ptr->channel ) ); |
|---|
| 2015 | |
|---|
| 2016 | // transfer ownership only if process is the TXT owner |
|---|
| 2017 | if( (owner_xp == process_xp) && (txt_id > 0) ) |
|---|
| 2018 | { |
|---|
| 2019 | // get extended pointers on root and lock of attached processes list |
|---|
| 2020 | root_xp = XPTR( txt_cxy , &txt_ptr->ext.txt.root ); |
|---|
| 2021 | lock_xp = XPTR( txt_cxy , &txt_ptr->ext.txt.lock ); |
|---|
| 2022 | |
|---|
| 2023 | // get lock |
|---|
| 2024 | remote_busylock_acquire( lock_xp ); |
|---|
| 2025 | |
|---|
| 2026 | if( process_get_ppid( process_xp ) != 1 ) // process is not KSH |
|---|
| 2027 | { |
|---|
| 2028 | // scan attached process list to find KSH process |
|---|
| 2029 | XLIST_FOREACH( root_xp , iter_xp ) |
|---|
| 2030 | { |
|---|
| 2031 | current_xp = XLIST_ELEMENT( iter_xp , process_t , txt_list ); |
|---|
| 2032 | current_cxy = GET_CXY( current_xp ); |
|---|
| 2033 | current_ptr = GET_PTR( current_xp ); |
|---|
| 2034 | |
|---|
| 2035 | if( process_get_ppid( current_xp ) == 1 ) // current is KSH |
|---|
| 2036 | { |
|---|
| 2037 | // release lock |
|---|
| 2038 | remote_busylock_release( lock_xp ); |
|---|
| 2039 | |
|---|
| 2040 | // set owner field in TXT chdev |
|---|
| 2041 | hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , current_xp ); |
|---|
| 2042 | |
|---|
| 2043 | #if DEBUG_PROCESS_TXT |
|---|
| 2044 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 2045 | uint32_t ksh_pid = hal_remote_l32( XPTR( current_cxy , ¤t_ptr->pid ) ); |
|---|
| 2046 | if( DEBUG_PROCESS_TXT < cycle ) |
|---|
| 2047 | printk("\n[%s] thread %x in process %x release TXT %d to KSH %x / cycle %d\n", |
|---|
| 2048 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, ksh_pid, cycle ); |
|---|
| 2049 | process_txt_display( txt_id ); |
|---|
| 2050 | #endif |
|---|
| 2051 | return; |
|---|
| 2052 | } |
|---|
| 2053 | } |
|---|
| 2054 | |
|---|
| 2055 | // release lock |
|---|
| 2056 | remote_busylock_release( lock_xp ); |
|---|
| 2057 | |
|---|
| 2058 | // PANIC if KSH not found |
|---|
| 2059 | assert( false , "KSH process not found for TXT %d" ); |
|---|
| 2060 | |
|---|
| 2061 | return; |
|---|
| 2062 | } |
|---|
| 2063 | else // process is KSH |
|---|
| 2064 | { |
|---|
| 2065 | // scan attached process list to find another process |
|---|
| 2066 | XLIST_FOREACH( root_xp , iter_xp ) |
|---|
| 2067 | { |
|---|
| 2068 | current_xp = XLIST_ELEMENT( iter_xp , process_t , txt_list ); |
|---|
| 2069 | current_cxy = GET_CXY( current_xp ); |
|---|
| 2070 | current_ptr = GET_PTR( current_xp ); |
|---|
| 2071 | |
|---|
| 2072 | if( current_xp != process_xp ) // current is not KSH |
|---|
| 2073 | { |
|---|
| 2074 | // release lock |
|---|
| 2075 | remote_busylock_release( lock_xp ); |
|---|
| 2076 | |
|---|
| 2077 | // set owner field in TXT chdev |
|---|
| 2078 | hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , current_xp ); |
|---|
| 2079 | |
|---|
| 2080 | #if DEBUG_PROCESS_TXT |
|---|
| 2081 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 2082 | uint32_t new_pid = hal_remote_l32( XPTR( current_cxy , ¤t_ptr->pid ) ); |
|---|
| 2083 | if( DEBUG_PROCESS_TXT < cycle ) |
|---|
| 2084 | printk("\n[%s] thread %x in process %x release TXT %d to process %x / cycle %d\n", |
|---|
| 2085 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, new_pid, cycle ); |
|---|
| 2086 | process_txt_display( txt_id ); |
|---|
| 2087 | #endif |
|---|
| 2088 | return; |
|---|
| 2089 | } |
|---|
| 2090 | } |
|---|
| 2091 | |
|---|
| 2092 | // release lock |
|---|
| 2093 | remote_busylock_release( lock_xp ); |
|---|
| 2094 | |
|---|
| 2095 | // no more owner for TXT if no other process found |
|---|
| 2096 | hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , XPTR_NULL ); |
|---|
| 2097 | |
|---|
| 2098 | #if DEBUG_PROCESS_TXT |
|---|
| 2099 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 2100 | if( DEBUG_PROCESS_TXT < cycle ) |
|---|
| 2101 | printk("\n[%s] thread %x in process %x release TXT %d to nobody / cycle %d\n", |
|---|
| 2102 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, cycle ); |
|---|
| 2103 | process_txt_display( txt_id ); |
|---|
| 2104 | #endif |
|---|
| 2105 | return; |
|---|
| 2106 | } |
|---|
| 2107 | } |
|---|
| 2108 | else |
|---|
| 2109 | { |
|---|
| 2110 | |
|---|
| 2111 | #if DEBUG_PROCESS_TXT |
|---|
| 2112 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 2113 | if( DEBUG_PROCESS_TXT < cycle ) |
|---|
| 2114 | printk("\n[%s] thread %x in process %d does nothing (not TXT owner) / cycle %d\n", |
|---|
| 2115 | __FUNCTION__, CURRENT_THREAD->trdid, process_pid, cycle ); |
|---|
| 2116 | process_txt_display( txt_id ); |
|---|
| 2117 | #endif |
|---|
| 2118 | |
|---|
| 2119 | } |
|---|
| 2120 | } // end process_txt_transfer_ownership() |
|---|
| 2121 | |
|---|
| 2122 | |
|---|
| 2123 | //////////////////////////////////////////////// |
|---|
| 2124 | bool_t process_txt_is_owner( xptr_t process_xp ) |
|---|
| 2125 | { |
|---|
| 2126 | // get local pointer and cluster of process in owner cluster |
|---|
| 2127 | cxy_t process_cxy = GET_CXY( process_xp ); |
|---|
| 2128 | process_t * process_ptr = GET_PTR( process_xp ); |
|---|
| 2129 | |
|---|
| 2130 | // check calling thread execute in target process owner cluster |
|---|
| 2131 | pid_t process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) ); |
|---|
| 2132 | assert( (process_cxy == CXY_FROM_PID( process_pid )) , |
|---|
| 2133 | "process descriptor not in owner cluster\n" ); |
|---|
| 2134 | |
|---|
| 2135 | // get extended pointer on stdin pseudo file |
|---|
| 2136 | xptr_t file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) ); |
|---|
| 2137 | |
|---|
| 2138 | // get pointers on TXT chdev |
|---|
| 2139 | xptr_t txt_xp = chdev_from_file( file_xp ); |
|---|
| 2140 | cxy_t txt_cxy = GET_CXY( txt_xp ); |
|---|
| 2141 | chdev_t * txt_ptr = GET_PTR( txt_xp ); |
|---|
| 2142 | |
|---|
| 2143 | // get extended pointer on TXT_RX owner process |
|---|
| 2144 | xptr_t owner_xp = hal_remote_l64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) ); |
|---|
| 2145 | |
|---|
| 2146 | return (process_xp == owner_xp); |
|---|
| 2147 | |
|---|
| 2148 | } // end process_txt_is_owner() |
|---|
| 2149 | |
|---|
| 2150 | //////////////////////////////////////////////// |
|---|
| 2151 | xptr_t process_txt_get_owner( uint32_t channel ) |
|---|
| 2152 | { |
|---|
| 2153 | xptr_t txt_rx_xp = chdev_dir.txt_rx[channel]; |
|---|
| 2154 | cxy_t txt_rx_cxy = GET_CXY( txt_rx_xp ); |
|---|
| 2155 | chdev_t * txt_rx_ptr = GET_PTR( txt_rx_xp ); |
|---|
| 2156 | |
|---|
| 2157 | return (xptr_t)hal_remote_l64( XPTR( txt_rx_cxy , &txt_rx_ptr->ext.txt.owner_xp ) ); |
|---|
| 2158 | |
|---|
| 2159 | } // end process_txt_get_owner() |
|---|
| 2160 | |
|---|
| 2161 | /////////////////////////////////////////// |
|---|
| 2162 | void process_txt_display( uint32_t txt_id ) |
|---|
| 2163 | { |
|---|
| 2164 | xptr_t chdev_xp; |
|---|
| 2165 | cxy_t chdev_cxy; |
|---|
| 2166 | chdev_t * chdev_ptr; |
|---|
| 2167 | xptr_t root_xp; |
|---|
| 2168 | xptr_t lock_xp; |
|---|
| 2169 | xptr_t current_xp; |
|---|
| 2170 | xptr_t iter_xp; |
|---|
| 2171 | cxy_t txt0_cxy; |
|---|
| 2172 | chdev_t * txt0_ptr; |
|---|
| 2173 | xptr_t txt0_xp; |
|---|
| 2174 | xptr_t txt0_lock_xp; |
|---|
| 2175 | |
|---|
| 2176 | assert( (txt_id < LOCAL_CLUSTER->nb_txt_channels) , |
|---|
| 2177 | "illegal TXT terminal index" ); |
|---|
| 2178 | |
|---|
| 2179 | // get pointers on TXT0 chdev |
|---|
| 2180 | txt0_xp = chdev_dir.txt_tx[0]; |
|---|
| 2181 | txt0_cxy = GET_CXY( txt0_xp ); |
|---|
| 2182 | txt0_ptr = GET_PTR( txt0_xp ); |
|---|
| 2183 | |
|---|
| 2184 | // get extended pointer on TXT0 lock |
|---|
| 2185 | txt0_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
|---|
| 2186 | |
|---|
| 2187 | // get pointers on TXT_RX[txt_id] chdev |
|---|
| 2188 | chdev_xp = chdev_dir.txt_rx[txt_id]; |
|---|
| 2189 | chdev_cxy = GET_CXY( chdev_xp ); |
|---|
| 2190 | chdev_ptr = GET_PTR( chdev_xp ); |
|---|
| 2191 | |
|---|
| 2192 | // get extended pointer on root & lock of attached process list |
|---|
| 2193 | root_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.root ); |
|---|
| 2194 | lock_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.lock ); |
|---|
| 2195 | |
|---|
| 2196 | // get lock on attached process list |
|---|
| 2197 | remote_busylock_acquire( lock_xp ); |
|---|
| 2198 | |
|---|
| 2199 | // get TXT0 lock in busy waiting mode |
|---|
| 2200 | remote_busylock_acquire( txt0_lock_xp ); |
|---|
| 2201 | |
|---|
| 2202 | // display header |
|---|
| 2203 | nolock_printk("\n***** processes attached to TXT_%d / cycle %d\n", |
|---|
| 2204 | txt_id , (uint32_t)hal_get_cycles() ); |
|---|
| 2205 | |
|---|
| 2206 | // scan attached process list |
|---|
| 2207 | XLIST_FOREACH( root_xp , iter_xp ) |
|---|
| 2208 | { |
|---|
| 2209 | current_xp = XLIST_ELEMENT( iter_xp , process_t , txt_list ); |
|---|
| 2210 | process_display( current_xp ); |
|---|
| 2211 | } |
|---|
| 2212 | |
|---|
| 2213 | // release TXT0 lock in busy waiting mode |
|---|
| 2214 | remote_busylock_release( txt0_lock_xp ); |
|---|
| 2215 | |
|---|
| 2216 | // release lock on attached process list |
|---|
| 2217 | remote_busylock_release( lock_xp ); |
|---|
| 2218 | |
|---|
| 2219 | } // end process_txt_display |
|---|