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