[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> |
---|
[446] | 44 | #include <rpc.h> |
---|
[1] | 45 | |
---|
| 46 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 47 | // Extern global variables |
---|
| 48 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 49 | |
---|
| 50 | extern process_t process_zero; |
---|
| 51 | |
---|
| 52 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[16] | 53 | // This function returns a printable string for the thread type. |
---|
[1] | 54 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[5] | 55 | char * thread_type_str( uint32_t type ) |
---|
| 56 | { |
---|
[296] | 57 | if ( type == THREAD_USER ) return "USR"; |
---|
[16] | 58 | else if( type == THREAD_RPC ) return "RPC"; |
---|
| 59 | else if( type == THREAD_DEV ) return "DEV"; |
---|
[296] | 60 | else if( type == THREAD_IDLE ) return "IDL"; |
---|
[5] | 61 | else return "undefined"; |
---|
| 62 | } |
---|
| 63 | |
---|
[1] | 64 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 65 | // This static function allocates physical memory for a thread descriptor. |
---|
| 66 | // It can be called by the three functions: |
---|
[1] | 67 | // - thread_user_create() |
---|
[14] | 68 | // - thread_user_fork() |
---|
[1] | 69 | // - thread_kernel_create() |
---|
| 70 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 71 | // @ return pointer on thread descriptor if success / return NULL if failure. |
---|
[1] | 72 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 73 | static thread_t * thread_alloc() |
---|
[1] | 74 | { |
---|
[23] | 75 | page_t * page; // pointer on page descriptor containing thread descriptor |
---|
[171] | 76 | kmem_req_t req; // kmem request |
---|
[1] | 77 | |
---|
| 78 | // allocates memory for thread descriptor + kernel stack |
---|
| 79 | req.type = KMEM_PAGE; |
---|
[14] | 80 | req.size = CONFIG_THREAD_DESC_ORDER; |
---|
[1] | 81 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 82 | page = kmem_alloc( &req ); |
---|
| 83 | |
---|
[23] | 84 | if( page == NULL ) return NULL; |
---|
[1] | 85 | |
---|
[315] | 86 | // return pointer on new thread descriptor |
---|
| 87 | xptr_t base_xp = ppm_page2base( XPTR(local_cxy , page ) ); |
---|
| 88 | return (thread_t *)GET_PTR( base_xp ); |
---|
| 89 | |
---|
| 90 | } // end thread_alloc() |
---|
| 91 | |
---|
| 92 | |
---|
[14] | 93 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[23] | 94 | // This static function releases the physical memory for a thread descriptor. |
---|
[53] | 95 | // It is called by the three functions: |
---|
[23] | 96 | // - thread_user_create() |
---|
| 97 | // - thread_user_fork() |
---|
| 98 | // - thread_kernel_create() |
---|
| 99 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 100 | // @ thread : pointer on thread descriptor. |
---|
| 101 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 102 | static void thread_release( thread_t * thread ) |
---|
| 103 | { |
---|
| 104 | kmem_req_t req; |
---|
| 105 | |
---|
[315] | 106 | xptr_t base_xp = ppm_base2page( XPTR(local_cxy , thread ) ); |
---|
| 107 | |
---|
[23] | 108 | req.type = KMEM_PAGE; |
---|
[315] | 109 | req.ptr = GET_PTR( base_xp ); |
---|
[23] | 110 | kmem_free( &req ); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 114 | // This static function initializes a thread descriptor (kernel or user). |
---|
[438] | 115 | // It can be called by the four functions: |
---|
[14] | 116 | // - thread_user_create() |
---|
| 117 | // - thread_user_fork() |
---|
| 118 | // - thread_kernel_create() |
---|
[438] | 119 | // - thread_idle_init() |
---|
| 120 | // It updates the local DQDT. |
---|
[14] | 121 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 122 | // @ thread : pointer on thread descriptor |
---|
| 123 | // @ process : pointer on process descriptor. |
---|
| 124 | // @ type : thread type. |
---|
| 125 | // @ func : pointer on thread entry function. |
---|
| 126 | // @ args : pointer on thread entry function arguments. |
---|
| 127 | // @ core_lid : target core local index. |
---|
| 128 | // @ u_stack_base : stack base (user thread only) |
---|
| 129 | // @ u_stack_size : stack base (user thread only) |
---|
| 130 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 131 | static error_t thread_init( thread_t * thread, |
---|
| 132 | process_t * process, |
---|
| 133 | thread_type_t type, |
---|
| 134 | void * func, |
---|
| 135 | void * args, |
---|
| 136 | lid_t core_lid, |
---|
| 137 | intptr_t u_stack_base, |
---|
| 138 | uint32_t u_stack_size ) |
---|
| 139 | { |
---|
| 140 | error_t error; |
---|
| 141 | trdid_t trdid; // allocated thread identifier |
---|
| 142 | |
---|
| 143 | cluster_t * local_cluster = LOCAL_CLUSTER; |
---|
| 144 | |
---|
[443] | 145 | #if DEBUG_THREAD_USER_INIT |
---|
| 146 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 147 | if( DEBUG_THREAD_USER_INIT < cycle ) |
---|
| 148 | printk("\n[DBG] %s : thread %x enter to init thread %x in process %x / cycle %d\n", |
---|
| 149 | __FUNCTION__, CURRENT_THREAD, thread, process->pid , cycle ); |
---|
| 150 | #endif |
---|
| 151 | |
---|
[14] | 152 | // register new thread in process descriptor, and get a TRDID |
---|
[1] | 153 | error = process_register_thread( process, thread , &trdid ); |
---|
| 154 | |
---|
[171] | 155 | if( error ) |
---|
[1] | 156 | { |
---|
[14] | 157 | printk("\n[ERROR] in %s : cannot get TRDID\n", __FUNCTION__ ); |
---|
| 158 | return EINVAL; |
---|
[1] | 159 | } |
---|
[14] | 160 | |
---|
[407] | 161 | // compute thread descriptor size without kernel stack |
---|
| 162 | uint32_t desc_size = (intptr_t)(&thread->signature) - (intptr_t)thread + 4; |
---|
| 163 | |
---|
[1] | 164 | // Initialize new thread descriptor |
---|
| 165 | thread->trdid = trdid; |
---|
[171] | 166 | thread->type = type; |
---|
[1] | 167 | thread->quantum = 0; // TODO |
---|
| 168 | thread->ticks_nr = 0; // TODO |
---|
| 169 | thread->time_last_check = 0; |
---|
| 170 | thread->core = &local_cluster->core_tbl[core_lid]; |
---|
| 171 | thread->process = process; |
---|
| 172 | |
---|
| 173 | thread->local_locks = 0; |
---|
[409] | 174 | thread->remote_locks = 0; |
---|
[1] | 175 | |
---|
[409] | 176 | #if CONFIG_LOCKS_DEBUG |
---|
| 177 | list_root_init( &thread->locks_root ); |
---|
[1] | 178 | xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) ); |
---|
[409] | 179 | #endif |
---|
[1] | 180 | |
---|
[171] | 181 | thread->u_stack_base = u_stack_base; |
---|
[1] | 182 | thread->u_stack_size = u_stack_size; |
---|
[407] | 183 | thread->k_stack_base = (intptr_t)thread + desc_size; |
---|
| 184 | thread->k_stack_size = CONFIG_THREAD_DESC_SIZE - desc_size; |
---|
[1] | 185 | |
---|
| 186 | thread->entry_func = func; // thread entry point |
---|
| 187 | thread->entry_args = args; // thread function arguments |
---|
[171] | 188 | thread->flags = 0; // all flags reset |
---|
[1] | 189 | thread->errno = 0; // no error detected |
---|
[407] | 190 | thread->fork_user = 0; // no user defined placement for fork |
---|
| 191 | thread->fork_cxy = 0; // user defined target cluster for fork |
---|
[409] | 192 | thread->blocked = THREAD_BLOCKED_GLOBAL; |
---|
[1] | 193 | |
---|
[440] | 194 | // reset sched list |
---|
[1] | 195 | list_entry_init( &thread->sched_list ); |
---|
| 196 | |
---|
| 197 | // reset thread info |
---|
| 198 | memset( &thread->info , 0 , sizeof(thread_info_t) ); |
---|
| 199 | |
---|
[409] | 200 | // initializes join_lock |
---|
| 201 | remote_spinlock_init( XPTR( local_cxy , &thread->join_lock ) ); |
---|
| 202 | |
---|
[1] | 203 | // initialise signature |
---|
| 204 | thread->signature = THREAD_SIGNATURE; |
---|
| 205 | |
---|
[443] | 206 | // FIXME define and call an architecture specific hal_thread_init() |
---|
| 207 | // function to initialise the save_sr field |
---|
[408] | 208 | thread->save_sr = 0xFF13; |
---|
| 209 | |
---|
[171] | 210 | // register new thread in core scheduler |
---|
[1] | 211 | sched_register_thread( thread->core , thread ); |
---|
| 212 | |
---|
[438] | 213 | // update DQDT |
---|
| 214 | dqdt_update_threads( 1 ); |
---|
| 215 | |
---|
[443] | 216 | #if DEBUG_THREAD_USER_INIT |
---|
| 217 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 218 | if( DEBUG_THREAD_USER_INIT < cycle ) |
---|
| 219 | printk("\n[DBG] %s : thread %x exit after init of thread %x in process %x / cycle %d\n", |
---|
| 220 | __FUNCTION__, CURRENT_THREAD, thread, process->pid , cycle ); |
---|
| 221 | #endif |
---|
| 222 | |
---|
[1] | 223 | return 0; |
---|
| 224 | |
---|
[296] | 225 | } // end thread_init() |
---|
| 226 | |
---|
[1] | 227 | ///////////////////////////////////////////////////////// |
---|
[23] | 228 | error_t thread_user_create( pid_t pid, |
---|
| 229 | void * start_func, |
---|
| 230 | void * start_arg, |
---|
[1] | 231 | pthread_attr_t * attr, |
---|
[23] | 232 | thread_t ** new_thread ) |
---|
[1] | 233 | { |
---|
| 234 | error_t error; |
---|
| 235 | thread_t * thread; // pointer on created thread descriptor |
---|
| 236 | process_t * process; // pointer to local process descriptor |
---|
| 237 | lid_t core_lid; // selected core local index |
---|
[23] | 238 | vseg_t * vseg; // stack vseg |
---|
[1] | 239 | |
---|
[407] | 240 | assert( (attr != NULL) , __FUNCTION__, "pthread attributes must be defined" ); |
---|
[5] | 241 | |
---|
[438] | 242 | #if DEBUG_THREAD_USER_CREATE |
---|
[433] | 243 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 244 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[443] | 245 | printk("\n[DBG] %s : thread %x enter for process %x in cluster %x / cycle %d\n", |
---|
| 246 | __FUNCTION__, CURRENT_THREAD, pid , local_cxy , cycle ); |
---|
[433] | 247 | #endif |
---|
[428] | 248 | |
---|
[23] | 249 | // get process descriptor local copy |
---|
| 250 | process = process_get_local_copy( pid ); |
---|
[440] | 251 | |
---|
[23] | 252 | if( process == NULL ) |
---|
| 253 | { |
---|
| 254 | printk("\n[ERROR] in %s : cannot get process descriptor %x\n", |
---|
| 255 | __FUNCTION__ , pid ); |
---|
| 256 | return ENOMEM; |
---|
| 257 | } |
---|
| 258 | |
---|
[443] | 259 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 260 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
| 261 | printk("\n[DBG] %s : process descriptor = %x for process %x in cluster %x\n", |
---|
| 262 | __FUNCTION__, process , pid , local_cxy ); |
---|
| 263 | #endif |
---|
| 264 | |
---|
[171] | 265 | // select a target core in local cluster |
---|
[407] | 266 | if( attr->attributes & PT_ATTR_CORE_DEFINED ) |
---|
[23] | 267 | { |
---|
[407] | 268 | core_lid = attr->lid; |
---|
| 269 | if( core_lid >= LOCAL_CLUSTER->cores_nr ) |
---|
| 270 | { |
---|
| 271 | printk("\n[ERROR] in %s : illegal core index attribute = %d\n", |
---|
| 272 | __FUNCTION__ , core_lid ); |
---|
| 273 | return EINVAL; |
---|
| 274 | } |
---|
[23] | 275 | } |
---|
[407] | 276 | else |
---|
| 277 | { |
---|
| 278 | core_lid = cluster_select_local_core(); |
---|
| 279 | } |
---|
[1] | 280 | |
---|
[443] | 281 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 282 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
| 283 | printk("\n[DBG] %s : core[%x,%d] selected\n", |
---|
| 284 | __FUNCTION__, local_cxy , core_lid ); |
---|
| 285 | #endif |
---|
| 286 | |
---|
[171] | 287 | // allocate a stack from local VMM |
---|
[407] | 288 | vseg = vmm_create_vseg( process, |
---|
| 289 | VSEG_TYPE_STACK, |
---|
| 290 | 0, // size unused |
---|
| 291 | 0, // length unused |
---|
| 292 | 0, // file_offset unused |
---|
| 293 | 0, // file_size unused |
---|
| 294 | XPTR_NULL, // mapper_xp unused |
---|
| 295 | local_cxy ); |
---|
[1] | 296 | |
---|
[170] | 297 | if( vseg == NULL ) |
---|
[23] | 298 | { |
---|
| 299 | printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ ); |
---|
| 300 | return ENOMEM; |
---|
[171] | 301 | } |
---|
[23] | 302 | |
---|
[171] | 303 | // allocate memory for thread descriptor |
---|
[14] | 304 | thread = thread_alloc(); |
---|
[1] | 305 | |
---|
[23] | 306 | if( thread == NULL ) |
---|
| 307 | { |
---|
| 308 | printk("\n[ERROR] in %s : cannot create new thread\n", __FUNCTION__ ); |
---|
| 309 | vmm_remove_vseg( vseg ); |
---|
| 310 | return ENOMEM; |
---|
| 311 | } |
---|
[14] | 312 | |
---|
[443] | 313 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 314 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
| 315 | printk("\n[DBG] %s : thread descriptor %x allocated\n", |
---|
| 316 | __FUNCTION__, thread ); |
---|
| 317 | #endif |
---|
| 318 | |
---|
[171] | 319 | // initialize thread descriptor |
---|
[14] | 320 | error = thread_init( thread, |
---|
| 321 | process, |
---|
| 322 | THREAD_USER, |
---|
[23] | 323 | start_func, |
---|
| 324 | start_arg, |
---|
[14] | 325 | core_lid, |
---|
[23] | 326 | vseg->min, |
---|
| 327 | vseg->max - vseg->min ); |
---|
[171] | 328 | if( error ) |
---|
[14] | 329 | { |
---|
[23] | 330 | printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ ); |
---|
| 331 | vmm_remove_vseg( vseg ); |
---|
| 332 | thread_release( thread ); |
---|
[14] | 333 | return EINVAL; |
---|
| 334 | } |
---|
| 335 | |
---|
[443] | 336 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 337 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
| 338 | printk("\n[DBG] %s : thread descriptor %x initialised / trdid = %x\n", |
---|
| 339 | __FUNCTION__, thread , thread->trdid ); |
---|
| 340 | #endif |
---|
| 341 | |
---|
[14] | 342 | // set DETACHED flag if required |
---|
[407] | 343 | if( attr->attributes & PT_ATTR_DETACH ) |
---|
| 344 | { |
---|
| 345 | thread->flags |= THREAD_FLAG_DETACHED; |
---|
| 346 | } |
---|
[1] | 347 | |
---|
[171] | 348 | // allocate & initialize CPU context |
---|
[407] | 349 | if( hal_cpu_context_create( thread ) ) |
---|
[23] | 350 | { |
---|
| 351 | printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ ); |
---|
| 352 | vmm_remove_vseg( vseg ); |
---|
| 353 | thread_release( thread ); |
---|
| 354 | return ENOMEM; |
---|
| 355 | } |
---|
| 356 | |
---|
[407] | 357 | // allocate FPU context |
---|
| 358 | if( hal_fpu_context_alloc( thread ) ) |
---|
[23] | 359 | { |
---|
| 360 | printk("\n[ERROR] in %s : cannot create FPU context\n", __FUNCTION__ ); |
---|
| 361 | vmm_remove_vseg( vseg ); |
---|
| 362 | thread_release( thread ); |
---|
| 363 | return ENOMEM; |
---|
| 364 | } |
---|
| 365 | |
---|
[438] | 366 | #if DEBUG_THREAD_USER_CREATE |
---|
[433] | 367 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 368 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[443] | 369 | printk("\n[DBG] %s : thread %x exit / new_thread %x in process %x / core %d / cycle %d\n", |
---|
| 370 | __FUNCTION__, CURRENT_THREAD, thread->trdid , pid , core_lid, cycle ); |
---|
[433] | 371 | #endif |
---|
[1] | 372 | |
---|
| 373 | *new_thread = thread; |
---|
| 374 | return 0; |
---|
[14] | 375 | |
---|
[296] | 376 | } // end thread_user_create() |
---|
| 377 | |
---|
[408] | 378 | /////////////////////////////////////////////////////// |
---|
| 379 | error_t thread_user_fork( xptr_t parent_thread_xp, |
---|
| 380 | process_t * child_process, |
---|
| 381 | thread_t ** child_thread ) |
---|
[1] | 382 | { |
---|
| 383 | error_t error; |
---|
[408] | 384 | thread_t * child_ptr; // local pointer on local child thread |
---|
| 385 | lid_t core_lid; // selected core local index |
---|
[1] | 386 | |
---|
[408] | 387 | thread_t * parent_ptr; // local pointer on remote parent thread |
---|
| 388 | cxy_t parent_cxy; // parent thread cluster |
---|
| 389 | process_t * parent_process; // local pointer on parent process |
---|
| 390 | xptr_t parent_gpt_xp; // extended pointer on parent thread GPT |
---|
[5] | 391 | |
---|
[408] | 392 | void * func; // parent thread entry_func |
---|
| 393 | void * args; // parent thread entry_args |
---|
| 394 | intptr_t base; // parent thread u_stack_base |
---|
| 395 | uint32_t size; // parent thread u_stack_size |
---|
| 396 | uint32_t flags; // parent_thread flags |
---|
| 397 | vpn_t vpn_base; // parent thread stack vpn_base |
---|
| 398 | vpn_t vpn_size; // parent thread stack vpn_size |
---|
| 399 | reg_t * uzone; // parent thread pointer on uzone |
---|
| 400 | |
---|
| 401 | vseg_t * vseg; // child thread STACK vseg |
---|
| 402 | |
---|
[438] | 403 | #if DEBUG_THREAD_USER_FORK |
---|
[433] | 404 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 405 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
[433] | 406 | printk("\n[DBG] %s : thread %x enter / child_process %x / cycle %d\n", |
---|
| 407 | __FUNCTION__, CURRENT_THREAD, child_process->pid, cycle ); |
---|
| 408 | #endif |
---|
[408] | 409 | |
---|
[1] | 410 | // select a target core in local cluster |
---|
| 411 | core_lid = cluster_select_local_core(); |
---|
| 412 | |
---|
[408] | 413 | // get cluster and local pointer on parent thread descriptor |
---|
| 414 | parent_cxy = GET_CXY( parent_thread_xp ); |
---|
| 415 | parent_ptr = (thread_t *)GET_PTR( parent_thread_xp ); |
---|
[1] | 416 | |
---|
[408] | 417 | // get relevant fields from parent thread |
---|
[428] | 418 | func = (void *) hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_func )); |
---|
| 419 | args = (void *) hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_args )); |
---|
| 420 | base = (intptr_t)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->u_stack_base )); |
---|
| 421 | size = (uint32_t)hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->u_stack_size )); |
---|
| 422 | flags = hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->flags )); |
---|
| 423 | uzone = (reg_t *) hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->uzone_current )); |
---|
[1] | 424 | |
---|
[408] | 425 | vpn_base = base >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 426 | vpn_size = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 427 | |
---|
| 428 | // get pointer on parent process in parent thread cluster |
---|
| 429 | parent_process = (process_t *)hal_remote_lpt( XPTR( parent_cxy, |
---|
| 430 | &parent_ptr->process ) ); |
---|
| 431 | |
---|
| 432 | // get extended pointer on parent GPT in parent thread cluster |
---|
| 433 | parent_gpt_xp = XPTR( parent_cxy , &parent_process->vmm.gpt ); |
---|
| 434 | |
---|
| 435 | // allocate memory for child thread descriptor |
---|
| 436 | child_ptr = thread_alloc(); |
---|
| 437 | if( child_ptr == NULL ) |
---|
[23] | 438 | { |
---|
| 439 | printk("\n[ERROR] in %s : cannot allocate new thread\n", __FUNCTION__ ); |
---|
[408] | 440 | return -1; |
---|
[23] | 441 | } |
---|
[14] | 442 | |
---|
[171] | 443 | // initialize thread descriptor |
---|
[408] | 444 | error = thread_init( child_ptr, |
---|
| 445 | child_process, |
---|
[14] | 446 | THREAD_USER, |
---|
[408] | 447 | func, |
---|
| 448 | args, |
---|
[14] | 449 | core_lid, |
---|
[408] | 450 | base, |
---|
| 451 | size ); |
---|
[23] | 452 | if( error ) |
---|
[14] | 453 | { |
---|
[408] | 454 | printk("\n[ERROR] in %s : cannot initialize child thread\n", __FUNCTION__ ); |
---|
| 455 | thread_release( child_ptr ); |
---|
[14] | 456 | return EINVAL; |
---|
| 457 | } |
---|
| 458 | |
---|
[407] | 459 | // return child pointer |
---|
[408] | 460 | *child_thread = child_ptr; |
---|
[1] | 461 | |
---|
[408] | 462 | // set detached flag if required |
---|
| 463 | if( flags & THREAD_FLAG_DETACHED ) child_ptr->flags = THREAD_FLAG_DETACHED; |
---|
[1] | 464 | |
---|
[408] | 465 | // update uzone pointer in child thread descriptor |
---|
[428] | 466 | child_ptr->uzone_current = (char *)((intptr_t)uzone + |
---|
| 467 | (intptr_t)child_ptr - |
---|
| 468 | (intptr_t)parent_ptr ); |
---|
[408] | 469 | |
---|
| 470 | |
---|
[407] | 471 | // allocate CPU context for child thread |
---|
[408] | 472 | if( hal_cpu_context_alloc( child_ptr ) ) |
---|
[23] | 473 | { |
---|
[407] | 474 | printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ ); |
---|
[408] | 475 | thread_release( child_ptr ); |
---|
| 476 | return -1; |
---|
[23] | 477 | } |
---|
| 478 | |
---|
[407] | 479 | // allocate FPU context for child thread |
---|
[408] | 480 | if( hal_fpu_context_alloc( child_ptr ) ) |
---|
[23] | 481 | { |
---|
[407] | 482 | printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ ); |
---|
[408] | 483 | thread_release( child_ptr ); |
---|
| 484 | return -1; |
---|
[23] | 485 | } |
---|
| 486 | |
---|
[408] | 487 | // create and initialize STACK vseg |
---|
| 488 | vseg = vseg_alloc(); |
---|
| 489 | vseg_init( vseg, |
---|
| 490 | VSEG_TYPE_STACK, |
---|
| 491 | base, |
---|
| 492 | size, |
---|
| 493 | vpn_base, |
---|
| 494 | vpn_size, |
---|
| 495 | 0, 0, XPTR_NULL, // not a file vseg |
---|
| 496 | local_cxy ); |
---|
[1] | 497 | |
---|
[408] | 498 | // register STACK vseg in local child VSL |
---|
| 499 | vseg_attach( &child_process->vmm , vseg ); |
---|
| 500 | |
---|
| 501 | // copy all valid STACK GPT entries |
---|
| 502 | vpn_t vpn; |
---|
| 503 | bool_t mapped; |
---|
| 504 | ppn_t ppn; |
---|
| 505 | for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ ) |
---|
| 506 | { |
---|
| 507 | error = hal_gpt_pte_copy( &child_process->vmm.gpt, |
---|
| 508 | parent_gpt_xp, |
---|
| 509 | vpn, |
---|
| 510 | true, // set cow |
---|
| 511 | &ppn, |
---|
| 512 | &mapped ); |
---|
| 513 | if( error ) |
---|
| 514 | { |
---|
| 515 | vseg_detach( &child_process->vmm , vseg ); |
---|
| 516 | vseg_free( vseg ); |
---|
| 517 | thread_release( child_ptr ); |
---|
| 518 | printk("\n[ERROR] in %s : cannot update child GPT\n", __FUNCTION__ ); |
---|
| 519 | return -1; |
---|
| 520 | } |
---|
| 521 | |
---|
[433] | 522 | // increment pending forks counter for the page if mapped |
---|
[408] | 523 | if( mapped ) |
---|
| 524 | { |
---|
| 525 | xptr_t page_xp = ppm_ppn2page( ppn ); |
---|
| 526 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
| 527 | page_t * page_ptr = (page_t *)GET_PTR( page_xp ); |
---|
[433] | 528 | hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->forks ) , 1 ); |
---|
[408] | 529 | |
---|
[438] | 530 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
[433] | 531 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 532 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
[433] | 533 | printk("\n[DBG] %s : thread %x copied stack PTE to child GPT : vpn %x\n", |
---|
| 534 | __FUNCTION__, CURRENT_THREAD, vpn ); |
---|
| 535 | #endif |
---|
[408] | 536 | |
---|
| 537 | } |
---|
| 538 | } |
---|
| 539 | |
---|
[433] | 540 | // set COW flag for all mapped entries of STAK vseg in parent thread GPT |
---|
| 541 | hal_gpt_set_cow( parent_gpt_xp, |
---|
| 542 | vpn_base, |
---|
| 543 | vpn_size ); |
---|
[408] | 544 | |
---|
[438] | 545 | #if DEBUG_THREAD_USER_FORK |
---|
[433] | 546 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 547 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
[433] | 548 | printk("\n[DBG] %s : thread %x exit / child_process %x / child_thread %x / cycle %d\n", |
---|
| 549 | __FUNCTION__, CURRENT_THREAD, child_process->pid, child_ptr, cycle ); |
---|
| 550 | #endif |
---|
[407] | 551 | |
---|
[1] | 552 | return 0; |
---|
[5] | 553 | |
---|
[296] | 554 | } // end thread_user_fork() |
---|
| 555 | |
---|
[1] | 556 | ///////////////////////////////////////////////////////// |
---|
| 557 | error_t thread_kernel_create( thread_t ** new_thread, |
---|
| 558 | thread_type_t type, |
---|
[171] | 559 | void * func, |
---|
| 560 | void * args, |
---|
[1] | 561 | lid_t core_lid ) |
---|
| 562 | { |
---|
| 563 | error_t error; |
---|
[14] | 564 | thread_t * thread; // pointer on new thread descriptor |
---|
[1] | 565 | |
---|
[407] | 566 | assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) , |
---|
| 567 | __FUNCTION__ , "illegal thread type" ); |
---|
[1] | 568 | |
---|
[171] | 569 | assert( (core_lid < LOCAL_CLUSTER->cores_nr) , |
---|
[5] | 570 | __FUNCTION__ , "illegal core_lid" ); |
---|
[1] | 571 | |
---|
[438] | 572 | #if DEBUG_THREAD_KERNEL_CREATE |
---|
[433] | 573 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 574 | if( DEBUG_THREAD_KERNEL_CREATE < cycle ) |
---|
[433] | 575 | printk("\n[DBG] %s : thread %x enter / requested_type %s / cycle %d\n", |
---|
| 576 | __FUNCTION__, CURRENT_THREAD, thread, thread_type_str(type), cycle ); |
---|
| 577 | #endif |
---|
| 578 | |
---|
[171] | 579 | // allocate memory for new thread descriptor |
---|
[14] | 580 | thread = thread_alloc(); |
---|
| 581 | |
---|
| 582 | if( thread == NULL ) return ENOMEM; |
---|
| 583 | |
---|
[171] | 584 | // initialize thread descriptor |
---|
[14] | 585 | error = thread_init( thread, |
---|
| 586 | &process_zero, |
---|
| 587 | type, |
---|
| 588 | func, |
---|
| 589 | args, |
---|
| 590 | core_lid, |
---|
| 591 | 0 , 0 ); // no user stack for a kernel thread |
---|
| 592 | |
---|
[171] | 593 | if( error ) // release allocated memory for thread descriptor |
---|
[1] | 594 | { |
---|
[185] | 595 | thread_release( thread ); |
---|
[14] | 596 | return EINVAL; |
---|
[1] | 597 | } |
---|
| 598 | |
---|
[171] | 599 | // allocate & initialize CPU context |
---|
| 600 | hal_cpu_context_create( thread ); |
---|
[14] | 601 | |
---|
[438] | 602 | #if DEBUG_THREAD_KERNEL_CREATE |
---|
[433] | 603 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 604 | if( DEBUG_THREAD_KERNEL_CREATE < cycle ) |
---|
[433] | 605 | printk("\n[DBG] %s : thread %x exit / new_thread %x / type %s / cycle %d\n", |
---|
| 606 | __FUNCTION__, CURRENT_THREAD, thread, thread_type_str(type), cycle ); |
---|
| 607 | #endif |
---|
[1] | 608 | |
---|
[171] | 609 | *new_thread = thread; |
---|
[1] | 610 | return 0; |
---|
[5] | 611 | |
---|
[296] | 612 | } // end thread_kernel_create() |
---|
| 613 | |
---|
[438] | 614 | ///////////////////////////////////////////////// |
---|
| 615 | error_t thread_idle_init( thread_t * thread, |
---|
| 616 | thread_type_t type, |
---|
| 617 | void * func, |
---|
| 618 | void * args, |
---|
| 619 | lid_t core_lid ) |
---|
[14] | 620 | { |
---|
[407] | 621 | assert( (type == THREAD_IDLE) , __FUNCTION__ , "illegal thread type" ); |
---|
[1] | 622 | |
---|
[407] | 623 | assert( (core_lid < LOCAL_CLUSTER->cores_nr) , __FUNCTION__ , "illegal core index" ); |
---|
[14] | 624 | |
---|
| 625 | error_t error = thread_init( thread, |
---|
| 626 | &process_zero, |
---|
| 627 | type, |
---|
| 628 | func, |
---|
| 629 | args, |
---|
| 630 | core_lid, |
---|
| 631 | 0 , 0 ); // no user stack for a kernel thread |
---|
| 632 | |
---|
| 633 | // allocate & initialize CPU context if success |
---|
| 634 | if( error == 0 ) hal_cpu_context_create( thread ); |
---|
[171] | 635 | |
---|
[14] | 636 | return error; |
---|
| 637 | |
---|
[438] | 638 | } // end thread_idle_init() |
---|
[407] | 639 | |
---|
[1] | 640 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 641 | // TODO: check that all memory dynamically allocated during thread execution |
---|
[440] | 642 | // has been released, using a cache of mmap requests. [AG] |
---|
[1] | 643 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[443] | 644 | bool_t thread_destroy( thread_t * thread ) |
---|
[1] | 645 | { |
---|
[409] | 646 | reg_t save_sr; |
---|
[443] | 647 | bool_t last_thread; |
---|
[1] | 648 | |
---|
| 649 | process_t * process = thread->process; |
---|
| 650 | core_t * core = thread->core; |
---|
| 651 | |
---|
[438] | 652 | #if DEBUG_THREAD_DESTROY |
---|
[433] | 653 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 654 | if( DEBUG_THREAD_DESTROY < cycle ) |
---|
[433] | 655 | printk("\n[DBG] %s : thread %x enter to destroy thread %x in process %x / cycle %d\n", |
---|
[450] | 656 | __FUNCTION__, CURRENT_THREAD, thread->trdid, process->pid, cycle ); |
---|
[433] | 657 | #endif |
---|
[1] | 658 | |
---|
[443] | 659 | assert( (thread->local_locks == 0) , __FUNCTION__ , |
---|
| 660 | "local lock not released for thread %x in process %x", thread->trdid, process->pid ); |
---|
[171] | 661 | |
---|
[443] | 662 | assert( (thread->remote_locks == 0) , __FUNCTION__ , |
---|
| 663 | "remote lock not released for thread %x in process %x", thread->trdid, process->pid ); |
---|
[5] | 664 | |
---|
[1] | 665 | // update intrumentation values |
---|
[408] | 666 | process->vmm.pgfault_nr += thread->info.pgfault_nr; |
---|
[1] | 667 | |
---|
| 668 | // release memory allocated for CPU context and FPU context |
---|
| 669 | hal_cpu_context_destroy( thread ); |
---|
[409] | 670 | if ( thread->type == THREAD_USER ) hal_fpu_context_destroy( thread ); |
---|
[1] | 671 | |
---|
[428] | 672 | // release FPU ownership if required |
---|
[409] | 673 | hal_disable_irq( &save_sr ); |
---|
[1] | 674 | if( core->fpu_owner == thread ) |
---|
| 675 | { |
---|
| 676 | core->fpu_owner = NULL; |
---|
| 677 | hal_fpu_disable(); |
---|
| 678 | } |
---|
[409] | 679 | hal_restore_irq( save_sr ); |
---|
[1] | 680 | |
---|
[171] | 681 | // remove thread from process th_tbl[] |
---|
[443] | 682 | last_thread = process_remove_thread( thread ); |
---|
[1] | 683 | |
---|
[438] | 684 | // update DQDT |
---|
| 685 | dqdt_update_threads( -1 ); |
---|
[23] | 686 | |
---|
[1] | 687 | // invalidate thread descriptor |
---|
| 688 | thread->signature = 0; |
---|
| 689 | |
---|
| 690 | // release memory for thread descriptor |
---|
[23] | 691 | thread_release( thread ); |
---|
[1] | 692 | |
---|
[438] | 693 | #if DEBUG_THREAD_DESTROY |
---|
[433] | 694 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 695 | if( DEBUG_THREAD_DESTROY < cycle ) |
---|
[450] | 696 | printk("\n[DBG] %s : thread %x exit / destroyed thread %x in process %x / last %d / cycle %d\n", |
---|
| 697 | __FUNCTION__, CURRENT_THREAD, thread->trdid, process->pid, last_thread / cycle ); |
---|
[433] | 698 | #endif |
---|
[1] | 699 | |
---|
[443] | 700 | return last_thread; |
---|
| 701 | |
---|
[407] | 702 | } // end thread_destroy() |
---|
| 703 | |
---|
[416] | 704 | ////////////////////////////////////////////////// |
---|
| 705 | inline void thread_set_req_ack( thread_t * target, |
---|
| 706 | uint32_t * rsp_count ) |
---|
[1] | 707 | { |
---|
[409] | 708 | reg_t save_sr; // for critical section |
---|
| 709 | |
---|
[416] | 710 | // get pointer on target thread scheduler |
---|
| 711 | scheduler_t * sched = &target->core->scheduler; |
---|
[409] | 712 | |
---|
[416] | 713 | // wait scheduler ready to handle a new request |
---|
| 714 | while( sched->req_ack_pending ) asm volatile( "nop" ); |
---|
[409] | 715 | |
---|
| 716 | // enter critical section |
---|
| 717 | hal_disable_irq( &save_sr ); |
---|
| 718 | |
---|
[416] | 719 | // set request in target thread scheduler |
---|
| 720 | sched->req_ack_pending = true; |
---|
[409] | 721 | |
---|
[416] | 722 | // set ack request in target thread "flags" |
---|
| 723 | hal_atomic_or( &target->flags , THREAD_FLAG_REQ_ACK ); |
---|
[409] | 724 | |
---|
[416] | 725 | // set pointer on responses counter in target thread |
---|
| 726 | target->ack_rsp_count = rsp_count; |
---|
[409] | 727 | |
---|
| 728 | // exit critical section |
---|
| 729 | hal_restore_irq( save_sr ); |
---|
| 730 | |
---|
[407] | 731 | hal_fence(); |
---|
[171] | 732 | |
---|
[416] | 733 | } // thread_set_req_ack() |
---|
[409] | 734 | |
---|
[416] | 735 | ///////////////////////////////////////////////////// |
---|
| 736 | inline void thread_reset_req_ack( thread_t * target ) |
---|
[1] | 737 | { |
---|
[409] | 738 | reg_t save_sr; // for critical section |
---|
| 739 | |
---|
| 740 | // get pointer on target thread scheduler |
---|
[416] | 741 | scheduler_t * sched = &target->core->scheduler; |
---|
[409] | 742 | |
---|
| 743 | // check signal pending in scheduler |
---|
[416] | 744 | assert( sched->req_ack_pending , __FUNCTION__ , "no pending signal" ); |
---|
[409] | 745 | |
---|
| 746 | // enter critical section |
---|
| 747 | hal_disable_irq( &save_sr ); |
---|
| 748 | |
---|
| 749 | // reset signal in scheduler |
---|
[416] | 750 | sched->req_ack_pending = false; |
---|
[409] | 751 | |
---|
| 752 | // reset signal in thread "flags" |
---|
[416] | 753 | hal_atomic_and( &target->flags , ~THREAD_FLAG_REQ_ACK ); |
---|
[409] | 754 | |
---|
| 755 | // reset pointer on responses counter |
---|
[416] | 756 | target->ack_rsp_count = NULL; |
---|
[409] | 757 | |
---|
| 758 | // exit critical section |
---|
| 759 | hal_restore_irq( save_sr ); |
---|
| 760 | |
---|
[407] | 761 | hal_fence(); |
---|
[171] | 762 | |
---|
[416] | 763 | } // thread_reset_req_ack() |
---|
[409] | 764 | |
---|
[1] | 765 | //////////////////////////////// |
---|
| 766 | inline bool_t thread_can_yield() |
---|
| 767 | { |
---|
| 768 | thread_t * this = CURRENT_THREAD; |
---|
[367] | 769 | return (this->local_locks == 0) && (this->remote_locks == 0); |
---|
[1] | 770 | } |
---|
| 771 | |
---|
[367] | 772 | ///////////////////////// |
---|
| 773 | void thread_check_sched() |
---|
[1] | 774 | { |
---|
[338] | 775 | thread_t * this = CURRENT_THREAD; |
---|
[1] | 776 | |
---|
[367] | 777 | if( (this->local_locks == 0) && |
---|
| 778 | (this->remote_locks == 0) && |
---|
| 779 | (this->flags & THREAD_FLAG_SCHED) ) |
---|
| 780 | { |
---|
| 781 | this->flags &= ~THREAD_FLAG_SCHED; |
---|
[408] | 782 | sched_yield( "delayed scheduling" ); |
---|
[367] | 783 | } |
---|
[1] | 784 | |
---|
[407] | 785 | } // end thread_check_sched() |
---|
| 786 | |
---|
[436] | 787 | ////////////////////////////////////// |
---|
| 788 | void thread_block( xptr_t thread_xp, |
---|
| 789 | uint32_t cause ) |
---|
[407] | 790 | { |
---|
[436] | 791 | // get thread cluster and local pointer |
---|
| 792 | cxy_t cxy = GET_CXY( thread_xp ); |
---|
| 793 | thread_t * ptr = GET_PTR( thread_xp ); |
---|
| 794 | |
---|
[407] | 795 | // set blocking cause |
---|
[436] | 796 | hal_remote_atomic_or( XPTR( cxy , &ptr->blocked ) , cause ); |
---|
[407] | 797 | hal_fence(); |
---|
| 798 | |
---|
[438] | 799 | #if DEBUG_THREAD_BLOCK |
---|
[433] | 800 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 801 | if( DEBUG_THREAD_BLOCK < cycle ) |
---|
[450] | 802 | printk("\n[DBG] %s : thread %x in cxy %x blocked thread %x in cxy %x / cause %x / cycle %d\n", |
---|
[446] | 803 | __FUNCTION__ , CURRENT_THREAD , local_cxy , ptr , cxy , cause , cycle ); |
---|
[433] | 804 | #endif |
---|
| 805 | |
---|
[407] | 806 | } // end thread_block() |
---|
| 807 | |
---|
[433] | 808 | //////////////////////////////////////////// |
---|
| 809 | uint32_t thread_unblock( xptr_t thread_xp, |
---|
[407] | 810 | uint32_t cause ) |
---|
| 811 | { |
---|
| 812 | // get thread cluster and local pointer |
---|
[433] | 813 | cxy_t cxy = GET_CXY( thread_xp ); |
---|
| 814 | thread_t * ptr = GET_PTR( thread_xp ); |
---|
[407] | 815 | |
---|
| 816 | // reset blocking cause |
---|
| 817 | uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause ); |
---|
| 818 | hal_fence(); |
---|
| 819 | |
---|
[438] | 820 | #if DEBUG_THREAD_BLOCK |
---|
[433] | 821 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 822 | if( DEBUG_THREAD_BLOCK < cycle ) |
---|
[450] | 823 | printk("\n[DBG] %s : thread %x in cxy %x unblocked thread %x in cxy %x / cause %x / cycle %d\n", |
---|
[446] | 824 | __FUNCTION__ , CURRENT_THREAD , local_cxy , ptr , cxy , cause , cycle ); |
---|
[433] | 825 | #endif |
---|
| 826 | |
---|
[446] | 827 | // return a non zero value if the cause bit is modified |
---|
| 828 | return( previous & cause ); |
---|
[436] | 829 | |
---|
[446] | 830 | } // end thread_unblock() |
---|
[407] | 831 | |
---|
[440] | 832 | ////////////////////////////////////// |
---|
| 833 | void thread_delete( xptr_t target_xp, |
---|
| 834 | pid_t pid, |
---|
| 835 | bool_t is_forced ) |
---|
| 836 | { |
---|
| 837 | reg_t save_sr; // for critical section |
---|
| 838 | bool_t target_join_done; // joining thread arrived first |
---|
| 839 | bool_t target_attached; // target thread attached |
---|
| 840 | xptr_t killer_xp; // extended pointer on killer thread (this) |
---|
| 841 | thread_t * killer_ptr; // pointer on killer thread (this) |
---|
| 842 | cxy_t target_cxy; // target thread cluster |
---|
| 843 | thread_t * target_ptr; // pointer on target thread |
---|
| 844 | xptr_t target_flags_xp; // extended pointer on target thread <flags> |
---|
| 845 | uint32_t target_flags; // target thread <flags> value |
---|
| 846 | xptr_t target_join_lock_xp; // extended pointer on target thread <join_lock> |
---|
| 847 | xptr_t target_join_xp_xp; // extended pointer on target thread <join_xp> |
---|
| 848 | trdid_t target_trdid; // target thread identifier |
---|
| 849 | ltid_t target_ltid; // target thread local index |
---|
| 850 | xptr_t joining_xp; // extended pointer on joining thread |
---|
| 851 | thread_t * joining_ptr; // pointer on joining thread |
---|
| 852 | cxy_t joining_cxy; // joining thread cluster |
---|
| 853 | cxy_t owner_cxy; // process owner cluster |
---|
| 854 | |
---|
| 855 | |
---|
| 856 | // get target thread pointers, identifiers, and flags |
---|
| 857 | target_cxy = GET_CXY( target_xp ); |
---|
| 858 | target_ptr = GET_PTR( target_xp ); |
---|
| 859 | target_trdid = hal_remote_lw( XPTR( target_cxy , &target_ptr->trdid ) ); |
---|
| 860 | target_ltid = LTID_FROM_TRDID( target_trdid ); |
---|
| 861 | target_flags_xp = XPTR( target_cxy , &target_ptr->flags ); |
---|
| 862 | target_flags = hal_remote_lw( target_flags_xp ); |
---|
| 863 | |
---|
| 864 | // get killer thread pointers |
---|
| 865 | killer_ptr = CURRENT_THREAD; |
---|
| 866 | killer_xp = XPTR( local_cxy , killer_ptr ); |
---|
| 867 | |
---|
| 868 | #if DEBUG_THREAD_DELETE |
---|
| 869 | uint32_t cycle = (uint32_t)hal_get_cycles; |
---|
| 870 | if( DEBUG_THREAD_DELETE < cycle ) |
---|
| 871 | printk("\n[DBG] %s : killer thread %x enter for target thread %x / cycle %d\n", |
---|
| 872 | __FUNCTION__, killer_ptr, target_ptr, cycle ); |
---|
| 873 | #endif |
---|
| 874 | |
---|
| 875 | // target thread cannot be the main thread, because the main thread |
---|
| 876 | // must be deleted by the parent process sys_wait() function |
---|
| 877 | owner_cxy = CXY_FROM_PID( pid ); |
---|
| 878 | assert( ((owner_cxy != target_cxy) || (target_ltid != 0)), __FUNCTION__, |
---|
| 879 | "tharget thread cannot be the main thread\n" ); |
---|
| 880 | |
---|
| 881 | // block the target thread |
---|
| 882 | thread_block( target_xp , THREAD_BLOCKED_GLOBAL ); |
---|
| 883 | |
---|
| 884 | // get attached from target flag descriptor |
---|
| 885 | target_attached = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_DETACHED) != 0); |
---|
| 886 | |
---|
| 887 | // synchronize with the joining thread if the target thread is attached |
---|
| 888 | if( target_attached && (is_forced == false) ) |
---|
| 889 | { |
---|
| 890 | // build extended pointers on target thread join fields |
---|
| 891 | target_join_lock_xp = XPTR( target_cxy , &target_ptr->join_lock ); |
---|
| 892 | target_join_xp_xp = XPTR( target_cxy , &target_ptr->join_xp ); |
---|
| 893 | |
---|
| 894 | // enter critical section |
---|
| 895 | hal_disable_irq( &save_sr ); |
---|
| 896 | |
---|
| 897 | // take the join_lock in target thread descriptor |
---|
| 898 | remote_spinlock_lock( target_join_lock_xp ); |
---|
| 899 | |
---|
| 900 | // get join_done from target thread descriptor |
---|
| 901 | target_join_done = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0); |
---|
| 902 | |
---|
| 903 | if( target_join_done ) // joining thread arrived first => unblock the joining thread |
---|
| 904 | { |
---|
| 905 | // get extended pointer on joining thread |
---|
| 906 | joining_xp = (xptr_t)hal_remote_lwd( target_join_xp_xp ); |
---|
| 907 | joining_ptr = GET_PTR( joining_xp ); |
---|
| 908 | joining_cxy = GET_CXY( joining_xp ); |
---|
| 909 | |
---|
| 910 | // reset the join_done flag in target thread |
---|
| 911 | hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_JOIN_DONE ); |
---|
| 912 | |
---|
| 913 | // unblock the joining thread |
---|
| 914 | thread_unblock( joining_xp , THREAD_BLOCKED_JOIN ); |
---|
| 915 | |
---|
| 916 | // release the join_lock in target thread descriptor |
---|
| 917 | remote_spinlock_unlock( target_join_lock_xp ); |
---|
| 918 | |
---|
| 919 | // restore IRQs |
---|
| 920 | hal_restore_irq( save_sr ); |
---|
| 921 | } |
---|
| 922 | else // this thread arrived first => register flags and deschedule |
---|
| 923 | { |
---|
| 924 | // set the kill_done flag in target thread |
---|
| 925 | hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE ); |
---|
| 926 | |
---|
| 927 | // block this thread on BLOCKED_JOIN |
---|
| 928 | thread_block( killer_xp , THREAD_BLOCKED_JOIN ); |
---|
| 929 | |
---|
| 930 | // set extended pointer on killer thread in target thread |
---|
| 931 | hal_remote_swd( target_join_xp_xp , killer_xp ); |
---|
| 932 | |
---|
| 933 | // release the join_lock in target thread descriptor |
---|
| 934 | remote_spinlock_unlock( target_join_lock_xp ); |
---|
| 935 | |
---|
| 936 | // deschedule |
---|
| 937 | sched_yield( "killer thread wait joining thread" ); |
---|
| 938 | |
---|
| 939 | // restore IRQs |
---|
| 940 | hal_restore_irq( save_sr ); |
---|
| 941 | } |
---|
| 942 | } // end if attached |
---|
| 943 | |
---|
| 944 | // set the REQ_DELETE flag in target thread descriptor |
---|
| 945 | hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE ); |
---|
| 946 | |
---|
| 947 | #if DEBUG_THREAD_DELETE |
---|
| 948 | cycle = (uint32_t)hal_get_cycles; |
---|
| 949 | if( DEBUG_THREAD_DELETE < cycle ) |
---|
| 950 | printk("\n[DBG] %s : killer thread %x exit for target thread %x / cycle %d\n", |
---|
| 951 | __FUNCTION__, killer_ptr, target_ptr, cycle ); |
---|
| 952 | #endif |
---|
| 953 | |
---|
| 954 | } // end thread_delete() |
---|
| 955 | |
---|
| 956 | |
---|
| 957 | |
---|
[14] | 958 | /////////////////////// |
---|
| 959 | void thread_idle_func() |
---|
[1] | 960 | { |
---|
[446] | 961 | |
---|
| 962 | #if DEBUG_THREAD_IDLE |
---|
| 963 | uint32_t cycle; |
---|
| 964 | #endif |
---|
| 965 | |
---|
[1] | 966 | while( 1 ) |
---|
| 967 | { |
---|
[408] | 968 | // unmask IRQs |
---|
| 969 | hal_enable_irq( NULL ); |
---|
| 970 | |
---|
[443] | 971 | // force core to low-power mode (optional) |
---|
| 972 | if( CONFIG_THREAD_IDLE_MODE_SLEEP ) |
---|
[407] | 973 | { |
---|
[1] | 974 | |
---|
[446] | 975 | #if (DEBUG_THREAD_IDLE & 1) |
---|
| 976 | cycle = (uint32_t)hal_get_cycles; |
---|
[438] | 977 | if( DEBUG_THREAD_IDLE < cycle ) |
---|
[446] | 978 | printk("\n[DBG] %s : idle thread on core[%x,%d] goes to sleep / cycle %d\n", |
---|
| 979 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, cycle ); |
---|
[433] | 980 | #endif |
---|
[1] | 981 | |
---|
[407] | 982 | hal_core_sleep(); |
---|
[1] | 983 | |
---|
[446] | 984 | #if (DEBUG_THREAD_IDLE & 1) |
---|
[433] | 985 | cycle = (uint32_t)hal_get_cycles; |
---|
[438] | 986 | if( DEBUG_THREAD_IDLE < cycle ) |
---|
[446] | 987 | printk("\n[DBG] %s : idle thread on core[%x,%d] wake up / cycle %d\n", |
---|
[433] | 988 | __FUNCTION__, this, local_cxy, this->core->lid, cycle ); |
---|
| 989 | #endif |
---|
[407] | 990 | |
---|
| 991 | } |
---|
[443] | 992 | |
---|
[446] | 993 | #if DEBUG_THREAD_IDLE |
---|
| 994 | sched_display( CURRENT_THREAD->core->lid ); |
---|
| 995 | #endif |
---|
| 996 | |
---|
[443] | 997 | // search a runable thread |
---|
| 998 | sched_yield( "IDLE" ); |
---|
[418] | 999 | } |
---|
[407] | 1000 | } // end thread_idle() |
---|
[1] | 1001 | |
---|
[407] | 1002 | |
---|
[16] | 1003 | ///////////////////////////////////////////////// |
---|
| 1004 | void thread_user_time_update( thread_t * thread ) |
---|
| 1005 | { |
---|
| 1006 | // TODO |
---|
[337] | 1007 | // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ ); |
---|
[16] | 1008 | } |
---|
[1] | 1009 | |
---|
[16] | 1010 | /////////////////////////////////////////////////// |
---|
| 1011 | void thread_kernel_time_update( thread_t * thread ) |
---|
| 1012 | { |
---|
| 1013 | // TODO |
---|
[337] | 1014 | // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ ); |
---|
[16] | 1015 | } |
---|
| 1016 | |
---|
[23] | 1017 | ///////////////////////////////////// |
---|
| 1018 | xptr_t thread_get_xptr( pid_t pid, |
---|
| 1019 | trdid_t trdid ) |
---|
| 1020 | { |
---|
| 1021 | cxy_t target_cxy; // target thread cluster identifier |
---|
| 1022 | ltid_t target_thread_ltid; // target thread local index |
---|
[171] | 1023 | thread_t * target_thread_ptr; // target thread local pointer |
---|
[23] | 1024 | xptr_t target_process_xp; // extended pointer on target process descriptor |
---|
[171] | 1025 | process_t * target_process_ptr; // local pointer on target process descriptor |
---|
[23] | 1026 | pid_t target_process_pid; // target process identifier |
---|
| 1027 | xlist_entry_t root; // root of list of process in target cluster |
---|
| 1028 | xptr_t lock_xp; // extended pointer on lock protecting this list |
---|
[16] | 1029 | |
---|
[23] | 1030 | // get target cluster identifier and local thread identifier |
---|
| 1031 | target_cxy = CXY_FROM_TRDID( trdid ); |
---|
| 1032 | target_thread_ltid = LTID_FROM_TRDID( trdid ); |
---|
| 1033 | |
---|
[436] | 1034 | // check trdid argument |
---|
| 1035 | if( (target_thread_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || |
---|
| 1036 | cluster_is_undefined( target_cxy ) ) return XPTR_NULL; |
---|
| 1037 | |
---|
[23] | 1038 | // get root of list of process descriptors in target cluster |
---|
| 1039 | hal_remote_memcpy( XPTR( local_cxy , &root ), |
---|
| 1040 | XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ), |
---|
| 1041 | sizeof(xlist_entry_t) ); |
---|
| 1042 | |
---|
[171] | 1043 | // get extended pointer on lock protecting the list of processes |
---|
[23] | 1044 | lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock ); |
---|
| 1045 | |
---|
| 1046 | // take the lock protecting the list of processes in target cluster |
---|
| 1047 | remote_spinlock_lock( lock_xp ); |
---|
| 1048 | |
---|
| 1049 | // loop on list of process in target cluster to find the PID process |
---|
| 1050 | xptr_t iter; |
---|
| 1051 | bool_t found = false; |
---|
| 1052 | XLIST_FOREACH( XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ) , iter ) |
---|
| 1053 | { |
---|
| 1054 | target_process_xp = XLIST_ELEMENT( iter , process_t , local_list ); |
---|
| 1055 | target_process_ptr = (process_t *)GET_PTR( target_process_xp ); |
---|
| 1056 | target_process_pid = hal_remote_lw( XPTR( target_cxy , &target_process_ptr->pid ) ); |
---|
| 1057 | if( target_process_pid == pid ) |
---|
| 1058 | { |
---|
| 1059 | found = true; |
---|
| 1060 | break; |
---|
| 1061 | } |
---|
| 1062 | } |
---|
| 1063 | |
---|
| 1064 | // release the lock protecting the list of processes in target cluster |
---|
| 1065 | remote_spinlock_unlock( lock_xp ); |
---|
| 1066 | |
---|
[436] | 1067 | // check PID found |
---|
| 1068 | if( found == false ) return XPTR_NULL; |
---|
[23] | 1069 | |
---|
| 1070 | // get target thread local pointer |
---|
| 1071 | xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] ); |
---|
[171] | 1072 | target_thread_ptr = (thread_t *)hal_remote_lpt( xp ); |
---|
[23] | 1073 | |
---|
[436] | 1074 | if( target_thread_ptr == NULL ) return XPTR_NULL; |
---|
[23] | 1075 | |
---|
| 1076 | return XPTR( target_cxy , target_thread_ptr ); |
---|
[171] | 1077 | } |
---|
[23] | 1078 | |
---|