[1] | 1 | /* |
---|
| 2 | * thread.c - implementation of thread operations (user & kernel) |
---|
[171] | 3 | * |
---|
[1] | 4 | * Author Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
[23] | 5 | * Alain Greiner (2016,2017) |
---|
[1] | 6 | * |
---|
| 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 8 | * |
---|
[5] | 9 | * This file is part of ALMOS-MKH. |
---|
[1] | 10 | * |
---|
[5] | 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[1] | 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
[5] | 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[1] | 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
[5] | 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[1] | 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
[14] | 25 | #include <kernel_config.h> |
---|
[1] | 26 | #include <hal_types.h> |
---|
| 27 | #include <hal_context.h> |
---|
| 28 | #include <hal_irqmask.h> |
---|
| 29 | #include <hal_special.h> |
---|
| 30 | #include <hal_remote.h> |
---|
| 31 | #include <memcpy.h> |
---|
| 32 | #include <printk.h> |
---|
| 33 | #include <cluster.h> |
---|
| 34 | #include <process.h> |
---|
| 35 | #include <scheduler.h> |
---|
[188] | 36 | #include <dev_pic.h> |
---|
[1] | 37 | #include <core.h> |
---|
| 38 | #include <list.h> |
---|
| 39 | #include <xlist.h> |
---|
| 40 | #include <page.h> |
---|
| 41 | #include <kmem.h> |
---|
| 42 | #include <ppm.h> |
---|
| 43 | #include <thread.h> |
---|
| 44 | |
---|
| 45 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 46 | // Extern global variables |
---|
| 47 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 48 | |
---|
| 49 | extern process_t process_zero; |
---|
| 50 | |
---|
| 51 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[16] | 52 | // This function returns a printable string for the thread type. |
---|
[1] | 53 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[5] | 54 | char * thread_type_str( uint32_t type ) |
---|
| 55 | { |
---|
[296] | 56 | if ( type == THREAD_USER ) return "USR"; |
---|
[16] | 57 | else if( type == THREAD_RPC ) return "RPC"; |
---|
| 58 | else if( type == THREAD_DEV ) return "DEV"; |
---|
[296] | 59 | else if( type == THREAD_IDLE ) return "IDL"; |
---|
[5] | 60 | else return "undefined"; |
---|
| 61 | } |
---|
| 62 | |
---|
[1] | 63 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 64 | // This static function allocates physical memory for a thread descriptor. |
---|
| 65 | // It can be called by the three functions: |
---|
[1] | 66 | // - thread_user_create() |
---|
[14] | 67 | // - thread_user_fork() |
---|
[1] | 68 | // - thread_kernel_create() |
---|
| 69 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 70 | // @ return pointer on thread descriptor if success / return NULL if failure. |
---|
[1] | 71 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 72 | static thread_t * thread_alloc() |
---|
[1] | 73 | { |
---|
[23] | 74 | page_t * page; // pointer on page descriptor containing thread descriptor |
---|
[171] | 75 | kmem_req_t req; // kmem request |
---|
[1] | 76 | |
---|
| 77 | // allocates memory for thread descriptor + kernel stack |
---|
| 78 | req.type = KMEM_PAGE; |
---|
[14] | 79 | req.size = CONFIG_THREAD_DESC_ORDER; |
---|
[1] | 80 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 81 | page = kmem_alloc( &req ); |
---|
| 82 | |
---|
[23] | 83 | if( page == NULL ) return NULL; |
---|
[1] | 84 | |
---|
[315] | 85 | // return pointer on new thread descriptor |
---|
| 86 | xptr_t base_xp = ppm_page2base( XPTR(local_cxy , page ) ); |
---|
| 87 | return (thread_t *)GET_PTR( base_xp ); |
---|
| 88 | |
---|
| 89 | } // end thread_alloc() |
---|
| 90 | |
---|
| 91 | |
---|
[14] | 92 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[23] | 93 | // This static function releases the physical memory for a thread descriptor. |
---|
[53] | 94 | // It is called by the three functions: |
---|
[23] | 95 | // - thread_user_create() |
---|
| 96 | // - thread_user_fork() |
---|
| 97 | // - thread_kernel_create() |
---|
| 98 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 99 | // @ thread : pointer on thread descriptor. |
---|
| 100 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 101 | static void thread_release( thread_t * thread ) |
---|
| 102 | { |
---|
| 103 | kmem_req_t req; |
---|
| 104 | |
---|
[315] | 105 | xptr_t base_xp = ppm_base2page( XPTR(local_cxy , thread ) ); |
---|
| 106 | |
---|
[23] | 107 | req.type = KMEM_PAGE; |
---|
[315] | 108 | req.ptr = GET_PTR( base_xp ); |
---|
[23] | 109 | kmem_free( &req ); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 113 | // This static function initializes a thread descriptor (kernel or user). |
---|
[409] | 114 | // It can be called by the three functions: |
---|
[14] | 115 | // - thread_user_create() |
---|
| 116 | // - thread_user_fork() |
---|
| 117 | // - thread_kernel_create() |
---|
| 118 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 119 | // @ thread : pointer on thread descriptor |
---|
| 120 | // @ process : pointer on process descriptor. |
---|
| 121 | // @ type : thread type. |
---|
| 122 | // @ func : pointer on thread entry function. |
---|
| 123 | // @ args : pointer on thread entry function arguments. |
---|
| 124 | // @ core_lid : target core local index. |
---|
| 125 | // @ u_stack_base : stack base (user thread only) |
---|
| 126 | // @ u_stack_size : stack base (user thread only) |
---|
| 127 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 128 | static error_t thread_init( thread_t * thread, |
---|
| 129 | process_t * process, |
---|
| 130 | thread_type_t type, |
---|
| 131 | void * func, |
---|
| 132 | void * args, |
---|
| 133 | lid_t core_lid, |
---|
| 134 | intptr_t u_stack_base, |
---|
| 135 | uint32_t u_stack_size ) |
---|
| 136 | { |
---|
| 137 | error_t error; |
---|
| 138 | trdid_t trdid; // allocated thread identifier |
---|
| 139 | |
---|
| 140 | cluster_t * local_cluster = LOCAL_CLUSTER; |
---|
| 141 | |
---|
| 142 | // register new thread in process descriptor, and get a TRDID |
---|
[1] | 143 | error = process_register_thread( process, thread , &trdid ); |
---|
| 144 | |
---|
[171] | 145 | if( error ) |
---|
[1] | 146 | { |
---|
[14] | 147 | printk("\n[ERROR] in %s : cannot get TRDID\n", __FUNCTION__ ); |
---|
| 148 | return EINVAL; |
---|
[1] | 149 | } |
---|
[14] | 150 | |
---|
[407] | 151 | // compute thread descriptor size without kernel stack |
---|
| 152 | uint32_t desc_size = (intptr_t)(&thread->signature) - (intptr_t)thread + 4; |
---|
| 153 | |
---|
[1] | 154 | // Initialize new thread descriptor |
---|
| 155 | thread->trdid = trdid; |
---|
[171] | 156 | thread->type = type; |
---|
[1] | 157 | thread->quantum = 0; // TODO |
---|
| 158 | thread->ticks_nr = 0; // TODO |
---|
| 159 | thread->time_last_check = 0; |
---|
| 160 | thread->core = &local_cluster->core_tbl[core_lid]; |
---|
| 161 | thread->process = process; |
---|
| 162 | |
---|
| 163 | thread->local_locks = 0; |
---|
[409] | 164 | thread->remote_locks = 0; |
---|
[1] | 165 | |
---|
[409] | 166 | #if CONFIG_LOCKS_DEBUG |
---|
| 167 | list_root_init( &thread->locks_root ); |
---|
[1] | 168 | xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) ); |
---|
[409] | 169 | #endif |
---|
[1] | 170 | |
---|
[171] | 171 | thread->u_stack_base = u_stack_base; |
---|
[1] | 172 | thread->u_stack_size = u_stack_size; |
---|
[407] | 173 | thread->k_stack_base = (intptr_t)thread + desc_size; |
---|
| 174 | thread->k_stack_size = CONFIG_THREAD_DESC_SIZE - desc_size; |
---|
[1] | 175 | |
---|
| 176 | thread->entry_func = func; // thread entry point |
---|
| 177 | thread->entry_args = args; // thread function arguments |
---|
[171] | 178 | thread->flags = 0; // all flags reset |
---|
[1] | 179 | thread->errno = 0; // no error detected |
---|
[407] | 180 | thread->fork_user = 0; // no user defined placement for fork |
---|
| 181 | thread->fork_cxy = 0; // user defined target cluster for fork |
---|
[409] | 182 | thread->blocked = THREAD_BLOCKED_GLOBAL; |
---|
[1] | 183 | |
---|
| 184 | // reset children list |
---|
| 185 | xlist_root_init( XPTR( local_cxy , &thread->children_root ) ); |
---|
| 186 | thread->children_nr = 0; |
---|
| 187 | |
---|
| 188 | // reset sched list and brothers list |
---|
| 189 | list_entry_init( &thread->sched_list ); |
---|
| 190 | xlist_entry_init( XPTR( local_cxy , &thread->brothers_list ) ); |
---|
| 191 | |
---|
| 192 | // reset thread info |
---|
| 193 | memset( &thread->info , 0 , sizeof(thread_info_t) ); |
---|
| 194 | |
---|
[409] | 195 | // initializes join_lock |
---|
| 196 | remote_spinlock_init( XPTR( local_cxy , &thread->join_lock ) ); |
---|
| 197 | |
---|
[1] | 198 | // initialise signature |
---|
| 199 | thread->signature = THREAD_SIGNATURE; |
---|
| 200 | |
---|
[408] | 201 | // FIXME call hal_thread_init() function to initialise the save_sr field |
---|
| 202 | thread->save_sr = 0xFF13; |
---|
| 203 | |
---|
[1] | 204 | // update local DQDT |
---|
| 205 | dqdt_local_update_threads( 1 ); |
---|
| 206 | |
---|
[171] | 207 | // register new thread in core scheduler |
---|
[1] | 208 | sched_register_thread( thread->core , thread ); |
---|
| 209 | |
---|
| 210 | return 0; |
---|
| 211 | |
---|
[296] | 212 | } // end thread_init() |
---|
| 213 | |
---|
[1] | 214 | ///////////////////////////////////////////////////////// |
---|
[23] | 215 | error_t thread_user_create( pid_t pid, |
---|
| 216 | void * start_func, |
---|
| 217 | void * start_arg, |
---|
[1] | 218 | pthread_attr_t * attr, |
---|
[23] | 219 | thread_t ** new_thread ) |
---|
[1] | 220 | { |
---|
| 221 | error_t error; |
---|
| 222 | thread_t * thread; // pointer on created thread descriptor |
---|
| 223 | process_t * process; // pointer to local process descriptor |
---|
| 224 | lid_t core_lid; // selected core local index |
---|
[23] | 225 | vseg_t * vseg; // stack vseg |
---|
[1] | 226 | |
---|
[407] | 227 | assert( (attr != NULL) , __FUNCTION__, "pthread attributes must be defined" ); |
---|
[5] | 228 | |
---|
[428] | 229 | thread_dmsg("\n[DBG] %s : core[%x,%d] enter for process %x\n", |
---|
| 230 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, process->pid ); |
---|
| 231 | |
---|
[23] | 232 | // get process descriptor local copy |
---|
| 233 | process = process_get_local_copy( pid ); |
---|
[1] | 234 | |
---|
[23] | 235 | if( process == NULL ) |
---|
| 236 | { |
---|
| 237 | printk("\n[ERROR] in %s : cannot get process descriptor %x\n", |
---|
| 238 | __FUNCTION__ , pid ); |
---|
| 239 | return ENOMEM; |
---|
| 240 | } |
---|
| 241 | |
---|
[171] | 242 | // select a target core in local cluster |
---|
[407] | 243 | if( attr->attributes & PT_ATTR_CORE_DEFINED ) |
---|
[23] | 244 | { |
---|
[407] | 245 | core_lid = attr->lid; |
---|
| 246 | if( core_lid >= LOCAL_CLUSTER->cores_nr ) |
---|
| 247 | { |
---|
| 248 | printk("\n[ERROR] in %s : illegal core index attribute = %d\n", |
---|
| 249 | __FUNCTION__ , core_lid ); |
---|
| 250 | return EINVAL; |
---|
| 251 | } |
---|
[23] | 252 | } |
---|
[407] | 253 | else |
---|
| 254 | { |
---|
| 255 | core_lid = cluster_select_local_core(); |
---|
| 256 | } |
---|
[1] | 257 | |
---|
[171] | 258 | // allocate a stack from local VMM |
---|
[407] | 259 | vseg = vmm_create_vseg( process, |
---|
| 260 | VSEG_TYPE_STACK, |
---|
| 261 | 0, // size unused |
---|
| 262 | 0, // length unused |
---|
| 263 | 0, // file_offset unused |
---|
| 264 | 0, // file_size unused |
---|
| 265 | XPTR_NULL, // mapper_xp unused |
---|
| 266 | local_cxy ); |
---|
[1] | 267 | |
---|
[170] | 268 | if( vseg == NULL ) |
---|
[23] | 269 | { |
---|
| 270 | printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ ); |
---|
| 271 | return ENOMEM; |
---|
[171] | 272 | } |
---|
[23] | 273 | |
---|
[171] | 274 | // allocate memory for thread descriptor |
---|
[14] | 275 | thread = thread_alloc(); |
---|
[1] | 276 | |
---|
[23] | 277 | if( thread == NULL ) |
---|
| 278 | { |
---|
| 279 | printk("\n[ERROR] in %s : cannot create new thread\n", __FUNCTION__ ); |
---|
| 280 | vmm_remove_vseg( vseg ); |
---|
| 281 | return ENOMEM; |
---|
| 282 | } |
---|
[14] | 283 | |
---|
[171] | 284 | // initialize thread descriptor |
---|
[14] | 285 | error = thread_init( thread, |
---|
| 286 | process, |
---|
| 287 | THREAD_USER, |
---|
[23] | 288 | start_func, |
---|
| 289 | start_arg, |
---|
[14] | 290 | core_lid, |
---|
[23] | 291 | vseg->min, |
---|
| 292 | vseg->max - vseg->min ); |
---|
[171] | 293 | if( error ) |
---|
[14] | 294 | { |
---|
[23] | 295 | printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ ); |
---|
| 296 | vmm_remove_vseg( vseg ); |
---|
| 297 | thread_release( thread ); |
---|
[14] | 298 | return EINVAL; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | // set DETACHED flag if required |
---|
[407] | 302 | if( attr->attributes & PT_ATTR_DETACH ) |
---|
| 303 | { |
---|
| 304 | thread->flags |= THREAD_FLAG_DETACHED; |
---|
| 305 | } |
---|
[1] | 306 | |
---|
[171] | 307 | // allocate & initialize CPU context |
---|
[407] | 308 | if( hal_cpu_context_create( thread ) ) |
---|
[23] | 309 | { |
---|
| 310 | printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ ); |
---|
| 311 | vmm_remove_vseg( vseg ); |
---|
| 312 | thread_release( thread ); |
---|
| 313 | return ENOMEM; |
---|
| 314 | } |
---|
| 315 | |
---|
[407] | 316 | // allocate FPU context |
---|
| 317 | if( hal_fpu_context_alloc( thread ) ) |
---|
[23] | 318 | { |
---|
| 319 | printk("\n[ERROR] in %s : cannot create FPU context\n", __FUNCTION__ ); |
---|
| 320 | vmm_remove_vseg( vseg ); |
---|
| 321 | thread_release( thread ); |
---|
| 322 | return ENOMEM; |
---|
| 323 | } |
---|
| 324 | |
---|
[408] | 325 | // update DQDT for new thread |
---|
| 326 | dqdt_local_update_threads( 1 ); |
---|
| 327 | |
---|
[428] | 328 | thread_dmsg("\n[DBG] %s : core[%x,%d] exit for process %x / trdid = %x / core = %d\n", |
---|
| 329 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, process->pid, thread->trdid, core_lid ); |
---|
[1] | 330 | |
---|
| 331 | *new_thread = thread; |
---|
| 332 | return 0; |
---|
[14] | 333 | |
---|
[296] | 334 | } // end thread_user_create() |
---|
| 335 | |
---|
[408] | 336 | /////////////////////////////////////////////////////// |
---|
| 337 | error_t thread_user_fork( xptr_t parent_thread_xp, |
---|
| 338 | process_t * child_process, |
---|
| 339 | thread_t ** child_thread ) |
---|
[1] | 340 | { |
---|
| 341 | error_t error; |
---|
[408] | 342 | thread_t * child_ptr; // local pointer on local child thread |
---|
| 343 | lid_t core_lid; // selected core local index |
---|
[1] | 344 | |
---|
[408] | 345 | thread_t * parent_ptr; // local pointer on remote parent thread |
---|
| 346 | cxy_t parent_cxy; // parent thread cluster |
---|
| 347 | process_t * parent_process; // local pointer on parent process |
---|
| 348 | xptr_t parent_gpt_xp; // extended pointer on parent thread GPT |
---|
[5] | 349 | |
---|
[408] | 350 | void * func; // parent thread entry_func |
---|
| 351 | void * args; // parent thread entry_args |
---|
| 352 | intptr_t base; // parent thread u_stack_base |
---|
| 353 | uint32_t size; // parent thread u_stack_size |
---|
| 354 | uint32_t flags; // parent_thread flags |
---|
| 355 | vpn_t vpn_base; // parent thread stack vpn_base |
---|
| 356 | vpn_t vpn_size; // parent thread stack vpn_size |
---|
| 357 | reg_t * uzone; // parent thread pointer on uzone |
---|
| 358 | |
---|
| 359 | vseg_t * vseg; // child thread STACK vseg |
---|
| 360 | |
---|
| 361 | thread_dmsg("\n[DBG] %s : core[%x,%d] enters at cycle %d\n", |
---|
| 362 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() ); |
---|
| 363 | |
---|
[1] | 364 | // select a target core in local cluster |
---|
| 365 | core_lid = cluster_select_local_core(); |
---|
| 366 | |
---|
[408] | 367 | // get cluster and local pointer on parent thread descriptor |
---|
| 368 | parent_cxy = GET_CXY( parent_thread_xp ); |
---|
| 369 | parent_ptr = (thread_t *)GET_PTR( parent_thread_xp ); |
---|
[1] | 370 | |
---|
[408] | 371 | // get relevant fields from parent thread |
---|
[428] | 372 | func = (void *) hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_func )); |
---|
| 373 | args = (void *) hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_args )); |
---|
| 374 | base = (intptr_t)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->u_stack_base )); |
---|
| 375 | size = (uint32_t)hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->u_stack_size )); |
---|
| 376 | flags = hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->flags )); |
---|
| 377 | uzone = (reg_t *) hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->uzone_current )); |
---|
[1] | 378 | |
---|
[408] | 379 | vpn_base = base >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 380 | vpn_size = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 381 | |
---|
| 382 | // get pointer on parent process in parent thread cluster |
---|
| 383 | parent_process = (process_t *)hal_remote_lpt( XPTR( parent_cxy, |
---|
| 384 | &parent_ptr->process ) ); |
---|
| 385 | |
---|
| 386 | // get extended pointer on parent GPT in parent thread cluster |
---|
| 387 | parent_gpt_xp = XPTR( parent_cxy , &parent_process->vmm.gpt ); |
---|
| 388 | |
---|
| 389 | // allocate memory for child thread descriptor |
---|
| 390 | child_ptr = thread_alloc(); |
---|
| 391 | if( child_ptr == NULL ) |
---|
[23] | 392 | { |
---|
| 393 | printk("\n[ERROR] in %s : cannot allocate new thread\n", __FUNCTION__ ); |
---|
[408] | 394 | return -1; |
---|
[23] | 395 | } |
---|
[14] | 396 | |
---|
[171] | 397 | // initialize thread descriptor |
---|
[408] | 398 | error = thread_init( child_ptr, |
---|
| 399 | child_process, |
---|
[14] | 400 | THREAD_USER, |
---|
[408] | 401 | func, |
---|
| 402 | args, |
---|
[14] | 403 | core_lid, |
---|
[408] | 404 | base, |
---|
| 405 | size ); |
---|
[23] | 406 | if( error ) |
---|
[14] | 407 | { |
---|
[408] | 408 | printk("\n[ERROR] in %s : cannot initialize child thread\n", __FUNCTION__ ); |
---|
| 409 | thread_release( child_ptr ); |
---|
[14] | 410 | return EINVAL; |
---|
| 411 | } |
---|
| 412 | |
---|
[407] | 413 | // return child pointer |
---|
[408] | 414 | *child_thread = child_ptr; |
---|
[1] | 415 | |
---|
[408] | 416 | // set detached flag if required |
---|
| 417 | if( flags & THREAD_FLAG_DETACHED ) child_ptr->flags = THREAD_FLAG_DETACHED; |
---|
[1] | 418 | |
---|
[408] | 419 | // update uzone pointer in child thread descriptor |
---|
[428] | 420 | child_ptr->uzone_current = (char *)((intptr_t)uzone + |
---|
| 421 | (intptr_t)child_ptr - |
---|
| 422 | (intptr_t)parent_ptr ); |
---|
[408] | 423 | |
---|
| 424 | |
---|
[407] | 425 | // allocate CPU context for child thread |
---|
[408] | 426 | if( hal_cpu_context_alloc( child_ptr ) ) |
---|
[23] | 427 | { |
---|
[407] | 428 | printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ ); |
---|
[408] | 429 | thread_release( child_ptr ); |
---|
| 430 | return -1; |
---|
[23] | 431 | } |
---|
| 432 | |
---|
[407] | 433 | // allocate FPU context for child thread |
---|
[408] | 434 | if( hal_fpu_context_alloc( child_ptr ) ) |
---|
[23] | 435 | { |
---|
[407] | 436 | printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ ); |
---|
[408] | 437 | thread_release( child_ptr ); |
---|
| 438 | return -1; |
---|
[23] | 439 | } |
---|
| 440 | |
---|
[408] | 441 | // create and initialize STACK vseg |
---|
| 442 | vseg = vseg_alloc(); |
---|
| 443 | vseg_init( vseg, |
---|
| 444 | VSEG_TYPE_STACK, |
---|
| 445 | base, |
---|
| 446 | size, |
---|
| 447 | vpn_base, |
---|
| 448 | vpn_size, |
---|
| 449 | 0, 0, XPTR_NULL, // not a file vseg |
---|
| 450 | local_cxy ); |
---|
[1] | 451 | |
---|
[408] | 452 | // register STACK vseg in local child VSL |
---|
| 453 | vseg_attach( &child_process->vmm , vseg ); |
---|
| 454 | |
---|
| 455 | // copy all valid STACK GPT entries |
---|
| 456 | vpn_t vpn; |
---|
| 457 | bool_t mapped; |
---|
| 458 | ppn_t ppn; |
---|
| 459 | for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ ) |
---|
| 460 | { |
---|
| 461 | error = hal_gpt_pte_copy( &child_process->vmm.gpt, |
---|
| 462 | parent_gpt_xp, |
---|
| 463 | vpn, |
---|
| 464 | true, // set cow |
---|
| 465 | &ppn, |
---|
| 466 | &mapped ); |
---|
| 467 | if( error ) |
---|
| 468 | { |
---|
| 469 | vseg_detach( &child_process->vmm , vseg ); |
---|
| 470 | vseg_free( vseg ); |
---|
| 471 | thread_release( child_ptr ); |
---|
| 472 | printk("\n[ERROR] in %s : cannot update child GPT\n", __FUNCTION__ ); |
---|
| 473 | return -1; |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | // increment page descriptor fork_nr for the referenced page if mapped |
---|
| 477 | if( mapped ) |
---|
| 478 | { |
---|
| 479 | xptr_t page_xp = ppm_ppn2page( ppn ); |
---|
| 480 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
| 481 | page_t * page_ptr = (page_t *)GET_PTR( page_xp ); |
---|
| 482 | hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->fork_nr ) , 1 ); |
---|
| 483 | |
---|
| 484 | thread_dmsg("\n[DBG] %s : core[%x,%d] copied PTE to child GPT : vpn %x\n", |
---|
| 485 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn ); |
---|
| 486 | |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | // set COW flag for STAK vseg in parent thread GPT |
---|
| 491 | hal_gpt_flip_cow( true, // set cow |
---|
| 492 | parent_gpt_xp, |
---|
| 493 | vpn_base, |
---|
| 494 | vpn_size ); |
---|
| 495 | |
---|
| 496 | // update DQDT for child thread |
---|
| 497 | dqdt_local_update_threads( 1 ); |
---|
| 498 | |
---|
[407] | 499 | thread_dmsg("\n[DBG] %s : core[%x,%d] exit / created main thread %x for process %x\n", |
---|
[408] | 500 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, child_ptr->trdid, child_process->pid ); |
---|
[407] | 501 | |
---|
[1] | 502 | return 0; |
---|
[5] | 503 | |
---|
[296] | 504 | } // end thread_user_fork() |
---|
| 505 | |
---|
[1] | 506 | ///////////////////////////////////////////////////////// |
---|
| 507 | error_t thread_kernel_create( thread_t ** new_thread, |
---|
| 508 | thread_type_t type, |
---|
[171] | 509 | void * func, |
---|
| 510 | void * args, |
---|
[1] | 511 | lid_t core_lid ) |
---|
| 512 | { |
---|
| 513 | error_t error; |
---|
[14] | 514 | thread_t * thread; // pointer on new thread descriptor |
---|
[1] | 515 | |
---|
[428] | 516 | thread_dmsg("\n[DBG] %s : core[%x,%d] enters / type %s / cycle %d\n", |
---|
[407] | 517 | __FUNCTION__ , local_cxy , core_lid , thread_type_str( type ) , hal_time_stamp() ); |
---|
[1] | 518 | |
---|
[407] | 519 | assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) , |
---|
| 520 | __FUNCTION__ , "illegal thread type" ); |
---|
[1] | 521 | |
---|
[171] | 522 | assert( (core_lid < LOCAL_CLUSTER->cores_nr) , |
---|
[5] | 523 | __FUNCTION__ , "illegal core_lid" ); |
---|
[1] | 524 | |
---|
[171] | 525 | // allocate memory for new thread descriptor |
---|
[14] | 526 | thread = thread_alloc(); |
---|
| 527 | |
---|
| 528 | if( thread == NULL ) return ENOMEM; |
---|
| 529 | |
---|
[171] | 530 | // initialize thread descriptor |
---|
[14] | 531 | error = thread_init( thread, |
---|
| 532 | &process_zero, |
---|
| 533 | type, |
---|
| 534 | func, |
---|
| 535 | args, |
---|
| 536 | core_lid, |
---|
| 537 | 0 , 0 ); // no user stack for a kernel thread |
---|
| 538 | |
---|
[171] | 539 | if( error ) // release allocated memory for thread descriptor |
---|
[1] | 540 | { |
---|
[185] | 541 | thread_release( thread ); |
---|
[14] | 542 | return EINVAL; |
---|
[1] | 543 | } |
---|
| 544 | |
---|
[171] | 545 | // allocate & initialize CPU context |
---|
| 546 | hal_cpu_context_create( thread ); |
---|
[14] | 547 | |
---|
[408] | 548 | // update DQDT for kernel thread |
---|
| 549 | dqdt_local_update_threads( 1 ); |
---|
| 550 | |
---|
[407] | 551 | thread_dmsg("\n[DBG] %s : core = [%x,%d] exit / trdid = %x / type %s / cycle %d\n", |
---|
| 552 | __FUNCTION__, local_cxy, core_lid, thread->trdid, thread_type_str(type), hal_time_stamp() ); |
---|
[1] | 553 | |
---|
[171] | 554 | *new_thread = thread; |
---|
[1] | 555 | return 0; |
---|
[5] | 556 | |
---|
[296] | 557 | } // end thread_kernel_create() |
---|
| 558 | |
---|
[14] | 559 | /////////////////////////////////////////////////// |
---|
| 560 | error_t thread_kernel_init( thread_t * thread, |
---|
| 561 | thread_type_t type, |
---|
[171] | 562 | void * func, |
---|
| 563 | void * args, |
---|
[14] | 564 | lid_t core_lid ) |
---|
| 565 | { |
---|
[407] | 566 | assert( (type == THREAD_IDLE) , __FUNCTION__ , "illegal thread type" ); |
---|
[1] | 567 | |
---|
[407] | 568 | assert( (core_lid < LOCAL_CLUSTER->cores_nr) , __FUNCTION__ , "illegal core index" ); |
---|
[14] | 569 | |
---|
| 570 | error_t error = thread_init( thread, |
---|
| 571 | &process_zero, |
---|
| 572 | type, |
---|
| 573 | func, |
---|
| 574 | args, |
---|
| 575 | core_lid, |
---|
| 576 | 0 , 0 ); // no user stack for a kernel thread |
---|
| 577 | |
---|
| 578 | // allocate & initialize CPU context if success |
---|
| 579 | if( error == 0 ) hal_cpu_context_create( thread ); |
---|
[171] | 580 | |
---|
[14] | 581 | return error; |
---|
| 582 | |
---|
[407] | 583 | } // end thread_kernel_init() |
---|
| 584 | |
---|
[1] | 585 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 586 | // TODO: check that all memory dynamically allocated during thread execution |
---|
| 587 | // has been released, using a cache of mmap and malloc requests. [AG] |
---|
| 588 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 589 | void thread_destroy( thread_t * thread ) |
---|
| 590 | { |
---|
| 591 | uint32_t tm_start; |
---|
| 592 | uint32_t tm_end; |
---|
[409] | 593 | reg_t save_sr; |
---|
[1] | 594 | |
---|
| 595 | process_t * process = thread->process; |
---|
| 596 | core_t * core = thread->core; |
---|
| 597 | |
---|
[407] | 598 | thread_dmsg("\n[DBG] %s : enters for thread %x in process %x / type = %s\n", |
---|
[5] | 599 | __FUNCTION__ , thread->trdid , process->pid , thread_type_str( thread->type ) ); |
---|
[1] | 600 | |
---|
[5] | 601 | assert( (thread->children_nr == 0) , __FUNCTION__ , "still attached children" ); |
---|
| 602 | |
---|
| 603 | assert( (thread->local_locks == 0) , __FUNCTION__ , "all local locks not released" ); |
---|
[171] | 604 | |
---|
[5] | 605 | assert( (thread->remote_locks == 0) , __FUNCTION__ , "all remote locks not released" ); |
---|
| 606 | |
---|
[101] | 607 | tm_start = hal_get_cycles(); |
---|
[1] | 608 | |
---|
| 609 | // update intrumentation values |
---|
[408] | 610 | process->vmm.pgfault_nr += thread->info.pgfault_nr; |
---|
[1] | 611 | |
---|
| 612 | // release memory allocated for CPU context and FPU context |
---|
| 613 | hal_cpu_context_destroy( thread ); |
---|
[409] | 614 | if ( thread->type == THREAD_USER ) hal_fpu_context_destroy( thread ); |
---|
[1] | 615 | |
---|
[428] | 616 | // release FPU ownership if required |
---|
[409] | 617 | hal_disable_irq( &save_sr ); |
---|
[1] | 618 | if( core->fpu_owner == thread ) |
---|
| 619 | { |
---|
| 620 | core->fpu_owner = NULL; |
---|
| 621 | hal_fpu_disable(); |
---|
| 622 | } |
---|
[409] | 623 | hal_restore_irq( save_sr ); |
---|
[1] | 624 | |
---|
[171] | 625 | // remove thread from process th_tbl[] |
---|
[428] | 626 | process_remove_thread( thread ); |
---|
[1] | 627 | |
---|
[23] | 628 | // update local DQDT |
---|
| 629 | dqdt_local_update_threads( -1 ); |
---|
| 630 | |
---|
[1] | 631 | // invalidate thread descriptor |
---|
| 632 | thread->signature = 0; |
---|
| 633 | |
---|
| 634 | // release memory for thread descriptor |
---|
[23] | 635 | thread_release( thread ); |
---|
[1] | 636 | |
---|
[101] | 637 | tm_end = hal_get_cycles(); |
---|
[1] | 638 | |
---|
[407] | 639 | thread_dmsg("\n[DBG] %s : exit for thread %x in process %x / duration = %d\n", |
---|
[5] | 640 | __FUNCTION__, thread->trdid , process->pid , tm_end - tm_start ); |
---|
[1] | 641 | |
---|
[407] | 642 | } // end thread_destroy() |
---|
| 643 | |
---|
[1] | 644 | ///////////////////////////////////////////////// |
---|
| 645 | void thread_child_parent_link( xptr_t xp_parent, |
---|
| 646 | xptr_t xp_child ) |
---|
| 647 | { |
---|
[171] | 648 | // get extended pointers on children list root |
---|
| 649 | cxy_t parent_cxy = GET_CXY( xp_parent ); |
---|
[1] | 650 | thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent ); |
---|
| 651 | xptr_t root = XPTR( parent_cxy , &parent_ptr->children_root ); |
---|
| 652 | |
---|
[171] | 653 | // get extended pointer on children list entry |
---|
| 654 | cxy_t child_cxy = GET_CXY( xp_child ); |
---|
[1] | 655 | thread_t * child_ptr = (thread_t *)GET_PTR( xp_child ); |
---|
| 656 | xptr_t entry = XPTR( child_cxy , &child_ptr->brothers_list ); |
---|
| 657 | |
---|
| 658 | // set the link |
---|
| 659 | xlist_add_first( root , entry ); |
---|
| 660 | hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , 1 ); |
---|
| 661 | |
---|
[409] | 662 | } // end thread_child_parent_link() |
---|
| 663 | |
---|
[1] | 664 | /////////////////////////////////////////////////// |
---|
| 665 | void thread_child_parent_unlink( xptr_t xp_parent, |
---|
| 666 | xptr_t xp_child ) |
---|
| 667 | { |
---|
| 668 | // get extended pointer on children list lock |
---|
[171] | 669 | cxy_t parent_cxy = GET_CXY( xp_parent ); |
---|
[1] | 670 | thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent ); |
---|
| 671 | xptr_t lock = XPTR( parent_cxy , &parent_ptr->children_lock ); |
---|
| 672 | |
---|
[171] | 673 | // get extended pointer on children list entry |
---|
| 674 | cxy_t child_cxy = GET_CXY( xp_child ); |
---|
[1] | 675 | thread_t * child_ptr = (thread_t *)GET_PTR( xp_child ); |
---|
| 676 | xptr_t entry = XPTR( child_cxy , &child_ptr->brothers_list ); |
---|
| 677 | |
---|
| 678 | // get the lock |
---|
| 679 | remote_spinlock_lock( lock ); |
---|
| 680 | |
---|
| 681 | // remove the link |
---|
| 682 | xlist_unlink( entry ); |
---|
| 683 | hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , -1 ); |
---|
[171] | 684 | |
---|
[1] | 685 | // release the lock |
---|
| 686 | remote_spinlock_unlock( lock ); |
---|
| 687 | |
---|
[409] | 688 | } // thread_child_parent_unlink() |
---|
| 689 | |
---|
[416] | 690 | ////////////////////////////////////////////////// |
---|
| 691 | inline void thread_set_req_ack( thread_t * target, |
---|
| 692 | uint32_t * rsp_count ) |
---|
[1] | 693 | { |
---|
[409] | 694 | reg_t save_sr; // for critical section |
---|
| 695 | |
---|
[416] | 696 | // get pointer on target thread scheduler |
---|
| 697 | scheduler_t * sched = &target->core->scheduler; |
---|
[409] | 698 | |
---|
[416] | 699 | // wait scheduler ready to handle a new request |
---|
| 700 | while( sched->req_ack_pending ) asm volatile( "nop" ); |
---|
[409] | 701 | |
---|
| 702 | // enter critical section |
---|
| 703 | hal_disable_irq( &save_sr ); |
---|
| 704 | |
---|
[416] | 705 | // set request in target thread scheduler |
---|
| 706 | sched->req_ack_pending = true; |
---|
[409] | 707 | |
---|
[416] | 708 | // set ack request in target thread "flags" |
---|
| 709 | hal_atomic_or( &target->flags , THREAD_FLAG_REQ_ACK ); |
---|
[409] | 710 | |
---|
[416] | 711 | // set pointer on responses counter in target thread |
---|
| 712 | target->ack_rsp_count = rsp_count; |
---|
[409] | 713 | |
---|
| 714 | // exit critical section |
---|
| 715 | hal_restore_irq( save_sr ); |
---|
| 716 | |
---|
[407] | 717 | hal_fence(); |
---|
[171] | 718 | |
---|
[416] | 719 | } // thread_set_req_ack() |
---|
[409] | 720 | |
---|
[416] | 721 | ///////////////////////////////////////////////////// |
---|
| 722 | inline void thread_reset_req_ack( thread_t * target ) |
---|
[1] | 723 | { |
---|
[409] | 724 | reg_t save_sr; // for critical section |
---|
| 725 | |
---|
| 726 | // get pointer on target thread scheduler |
---|
[416] | 727 | scheduler_t * sched = &target->core->scheduler; |
---|
[409] | 728 | |
---|
| 729 | // check signal pending in scheduler |
---|
[416] | 730 | assert( sched->req_ack_pending , __FUNCTION__ , "no pending signal" ); |
---|
[409] | 731 | |
---|
| 732 | // enter critical section |
---|
| 733 | hal_disable_irq( &save_sr ); |
---|
| 734 | |
---|
| 735 | // reset signal in scheduler |
---|
[416] | 736 | sched->req_ack_pending = false; |
---|
[409] | 737 | |
---|
| 738 | // reset signal in thread "flags" |
---|
[416] | 739 | hal_atomic_and( &target->flags , ~THREAD_FLAG_REQ_ACK ); |
---|
[409] | 740 | |
---|
| 741 | // reset pointer on responses counter |
---|
[416] | 742 | target->ack_rsp_count = NULL; |
---|
[409] | 743 | |
---|
| 744 | // exit critical section |
---|
| 745 | hal_restore_irq( save_sr ); |
---|
| 746 | |
---|
[407] | 747 | hal_fence(); |
---|
[171] | 748 | |
---|
[416] | 749 | } // thread_reset_req_ack() |
---|
[409] | 750 | |
---|
[1] | 751 | //////////////////////////////// |
---|
| 752 | inline bool_t thread_can_yield() |
---|
| 753 | { |
---|
| 754 | thread_t * this = CURRENT_THREAD; |
---|
[367] | 755 | return (this->local_locks == 0) && (this->remote_locks == 0); |
---|
[1] | 756 | } |
---|
| 757 | |
---|
[367] | 758 | ///////////////////////// |
---|
| 759 | void thread_check_sched() |
---|
[1] | 760 | { |
---|
[338] | 761 | thread_t * this = CURRENT_THREAD; |
---|
[1] | 762 | |
---|
[367] | 763 | if( (this->local_locks == 0) && |
---|
| 764 | (this->remote_locks == 0) && |
---|
| 765 | (this->flags & THREAD_FLAG_SCHED) ) |
---|
| 766 | { |
---|
| 767 | this->flags &= ~THREAD_FLAG_SCHED; |
---|
[408] | 768 | sched_yield( "delayed scheduling" ); |
---|
[367] | 769 | } |
---|
[1] | 770 | |
---|
[407] | 771 | } // end thread_check_sched() |
---|
| 772 | |
---|
| 773 | ///////////////////////////////////// |
---|
| 774 | void thread_block( thread_t * thread, |
---|
| 775 | uint32_t cause ) |
---|
| 776 | { |
---|
| 777 | // set blocking cause |
---|
| 778 | hal_atomic_or( &thread->blocked , cause ); |
---|
| 779 | hal_fence(); |
---|
| 780 | |
---|
| 781 | } // end thread_block() |
---|
| 782 | |
---|
| 783 | ///////////////////////////////////////// |
---|
| 784 | uint32_t thread_unblock( xptr_t thread, |
---|
| 785 | uint32_t cause ) |
---|
| 786 | { |
---|
| 787 | // get thread cluster and local pointer |
---|
| 788 | cxy_t cxy = GET_CXY( thread ); |
---|
| 789 | thread_t * ptr = (thread_t *)GET_PTR( thread ); |
---|
| 790 | |
---|
| 791 | // reset blocking cause |
---|
| 792 | uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause ); |
---|
| 793 | hal_fence(); |
---|
| 794 | |
---|
| 795 | // return a non zero value if the cause bit is modified |
---|
| 796 | return( previous & cause ); |
---|
| 797 | |
---|
| 798 | } // end thread_unblock() |
---|
| 799 | |
---|
[409] | 800 | ///////////////////////////////////// |
---|
| 801 | void thread_kill( thread_t * target ) |
---|
[1] | 802 | { |
---|
[416] | 803 | volatile uint32_t rsp_count = 1; // responses counter |
---|
[1] | 804 | |
---|
[409] | 805 | thread_t * killer = CURRENT_THREAD; |
---|
[1] | 806 | |
---|
[416] | 807 | thread_dmsg("\n[DBG] %s : killer thread %x enter for target thread %x\n", |
---|
[428] | 808 | __FUNCTION__, local_cxy, killer->trdid , target->trdid ); |
---|
[1] | 809 | |
---|
[409] | 810 | // set the global blocked bit in target thread descriptor. |
---|
| 811 | thread_block( target , THREAD_BLOCKED_GLOBAL ); |
---|
| 812 | |
---|
| 813 | // request target scheduler to deschedule the target thread |
---|
| 814 | // when killer thread is not running on same core as target thread |
---|
| 815 | if( killer->core->lid != target->core->lid ) |
---|
[1] | 816 | { |
---|
[409] | 817 | // set signal in target thread descriptor and in target scheduler |
---|
[416] | 818 | thread_set_req_ack( target , (void *)(&rsp_count) ); |
---|
[409] | 819 | |
---|
| 820 | // send an IPI to the target thread core. |
---|
| 821 | dev_pic_send_ipi( local_cxy , target->core->lid ); |
---|
| 822 | |
---|
| 823 | // poll the response |
---|
| 824 | while( 1 ) |
---|
| 825 | { |
---|
| 826 | // exit when response received from scheduler |
---|
[416] | 827 | if( rsp_count == 0 ) break; |
---|
[409] | 828 | |
---|
| 829 | // deschedule without blocking |
---|
| 830 | hal_fixed_delay( 1000 ); |
---|
| 831 | } |
---|
[1] | 832 | } |
---|
| 833 | |
---|
[416] | 834 | // set REQ_DELETE flag |
---|
| 835 | hal_atomic_or( &target->flags , THREAD_FLAG_REQ_DELETE ); |
---|
[1] | 836 | |
---|
[416] | 837 | thread_dmsg("\n[DBG] %s : killer thread %x exit for target thread %x\n", |
---|
[428] | 838 | __FUNCTION__, local_cxy, killer->trdid , target->trdid ); |
---|
[409] | 839 | |
---|
[407] | 840 | } // end thread_kill() |
---|
| 841 | |
---|
[14] | 842 | /////////////////////// |
---|
| 843 | void thread_idle_func() |
---|
[1] | 844 | { |
---|
| 845 | while( 1 ) |
---|
| 846 | { |
---|
[408] | 847 | // unmask IRQs |
---|
| 848 | hal_enable_irq( NULL ); |
---|
| 849 | |
---|
[407] | 850 | if( CONFIG_THREAD_IDLE_MODE_SLEEP ) // force core to low-power mode |
---|
| 851 | { |
---|
[1] | 852 | |
---|
[407] | 853 | idle_dmsg("\n[DBG] %s : core[%x][%d] goes to sleep at cycle %d\n", |
---|
| 854 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() ); |
---|
[1] | 855 | |
---|
[407] | 856 | hal_core_sleep(); |
---|
[1] | 857 | |
---|
[407] | 858 | idle_dmsg("\n[DBG] %s : core[%x][%d] wake up at cycle %d\n", |
---|
| 859 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() ); |
---|
| 860 | |
---|
| 861 | } |
---|
[418] | 862 | else // search a runable thread |
---|
[407] | 863 | { |
---|
[418] | 864 | sched_yield( "IDLE" ); |
---|
[407] | 865 | } |
---|
[418] | 866 | } |
---|
[407] | 867 | } // end thread_idle() |
---|
[1] | 868 | |
---|
[407] | 869 | |
---|
[16] | 870 | ///////////////////////////////////////////////// |
---|
| 871 | void thread_user_time_update( thread_t * thread ) |
---|
| 872 | { |
---|
| 873 | // TODO |
---|
[337] | 874 | // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ ); |
---|
[16] | 875 | } |
---|
[1] | 876 | |
---|
[16] | 877 | /////////////////////////////////////////////////// |
---|
| 878 | void thread_kernel_time_update( thread_t * thread ) |
---|
| 879 | { |
---|
| 880 | // TODO |
---|
[337] | 881 | // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ ); |
---|
[16] | 882 | } |
---|
| 883 | |
---|
[23] | 884 | ///////////////////////////////////// |
---|
| 885 | xptr_t thread_get_xptr( pid_t pid, |
---|
| 886 | trdid_t trdid ) |
---|
| 887 | { |
---|
| 888 | cxy_t target_cxy; // target thread cluster identifier |
---|
| 889 | ltid_t target_thread_ltid; // target thread local index |
---|
[171] | 890 | thread_t * target_thread_ptr; // target thread local pointer |
---|
[23] | 891 | xptr_t target_process_xp; // extended pointer on target process descriptor |
---|
[171] | 892 | process_t * target_process_ptr; // local pointer on target process descriptor |
---|
[23] | 893 | pid_t target_process_pid; // target process identifier |
---|
| 894 | xlist_entry_t root; // root of list of process in target cluster |
---|
| 895 | xptr_t lock_xp; // extended pointer on lock protecting this list |
---|
[16] | 896 | |
---|
[23] | 897 | // get target cluster identifier and local thread identifier |
---|
| 898 | target_cxy = CXY_FROM_TRDID( trdid ); |
---|
| 899 | target_thread_ltid = LTID_FROM_TRDID( trdid ); |
---|
| 900 | |
---|
| 901 | // get root of list of process descriptors in target cluster |
---|
| 902 | hal_remote_memcpy( XPTR( local_cxy , &root ), |
---|
| 903 | XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ), |
---|
| 904 | sizeof(xlist_entry_t) ); |
---|
| 905 | |
---|
[171] | 906 | // get extended pointer on lock protecting the list of processes |
---|
[23] | 907 | lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock ); |
---|
| 908 | |
---|
| 909 | // take the lock protecting the list of processes in target cluster |
---|
| 910 | remote_spinlock_lock( lock_xp ); |
---|
| 911 | |
---|
| 912 | // loop on list of process in target cluster to find the PID process |
---|
| 913 | xptr_t iter; |
---|
| 914 | bool_t found = false; |
---|
| 915 | XLIST_FOREACH( XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ) , iter ) |
---|
| 916 | { |
---|
| 917 | target_process_xp = XLIST_ELEMENT( iter , process_t , local_list ); |
---|
| 918 | target_process_ptr = (process_t *)GET_PTR( target_process_xp ); |
---|
| 919 | target_process_pid = hal_remote_lw( XPTR( target_cxy , &target_process_ptr->pid ) ); |
---|
| 920 | if( target_process_pid == pid ) |
---|
| 921 | { |
---|
| 922 | found = true; |
---|
| 923 | break; |
---|
| 924 | } |
---|
| 925 | } |
---|
| 926 | |
---|
| 927 | // release the lock protecting the list of processes in target cluster |
---|
| 928 | remote_spinlock_unlock( lock_xp ); |
---|
| 929 | |
---|
| 930 | // check target thread found |
---|
| 931 | if( found == false ) |
---|
| 932 | { |
---|
| 933 | return XPTR_NULL; |
---|
| 934 | } |
---|
| 935 | |
---|
| 936 | // get target thread local pointer |
---|
| 937 | xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] ); |
---|
[171] | 938 | target_thread_ptr = (thread_t *)hal_remote_lpt( xp ); |
---|
[23] | 939 | |
---|
| 940 | if( target_thread_ptr == NULL ) |
---|
| 941 | { |
---|
| 942 | return XPTR_NULL; |
---|
| 943 | } |
---|
| 944 | |
---|
| 945 | return XPTR( target_cxy , target_thread_ptr ); |
---|
[171] | 946 | } |
---|
[23] | 947 | |
---|