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