[1] | 1 | /* |
---|
[564] | 2 | * thread.c - thread operations implementation (user & kernel) |
---|
[171] | 3 | * |
---|
[1] | 4 | * Author Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
[625] | 5 | * Alain Greiner (2016,2017,2018,2019) |
---|
[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> |
---|
[457] | 26 | #include <hal_kernel_types.h> |
---|
[1] | 27 | #include <hal_context.h> |
---|
| 28 | #include <hal_irqmask.h> |
---|
| 29 | #include <hal_special.h> |
---|
| 30 | #include <hal_remote.h> |
---|
[625] | 31 | #include <hal_vmm.h> |
---|
[1] | 32 | #include <memcpy.h> |
---|
| 33 | #include <printk.h> |
---|
| 34 | #include <cluster.h> |
---|
| 35 | #include <process.h> |
---|
| 36 | #include <scheduler.h> |
---|
[188] | 37 | #include <dev_pic.h> |
---|
[1] | 38 | #include <core.h> |
---|
| 39 | #include <list.h> |
---|
| 40 | #include <xlist.h> |
---|
| 41 | #include <page.h> |
---|
| 42 | #include <kmem.h> |
---|
| 43 | #include <ppm.h> |
---|
| 44 | #include <thread.h> |
---|
[446] | 45 | #include <rpc.h> |
---|
[1] | 46 | |
---|
| 47 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 48 | // Extern global variables |
---|
| 49 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 50 | |
---|
[564] | 51 | extern process_t process_zero; // allocated in kernel_init.c |
---|
| 52 | extern char * lock_type_str[]; // allocated in kernel_init.c |
---|
| 53 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
[1] | 54 | |
---|
| 55 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[16] | 56 | // This function returns a printable string for the thread type. |
---|
[1] | 57 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[527] | 58 | const char * thread_type_str( thread_type_t type ) |
---|
[5] | 59 | { |
---|
[527] | 60 | switch ( type ) { |
---|
| 61 | case THREAD_USER: return "USR"; |
---|
| 62 | case THREAD_RPC: return "RPC"; |
---|
| 63 | case THREAD_DEV: return "DEV"; |
---|
| 64 | case THREAD_IDLE: return "IDL"; |
---|
| 65 | default: return "undefined"; |
---|
| 66 | } |
---|
[5] | 67 | } |
---|
| 68 | |
---|
[1] | 69 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 70 | // This static function allocates physical memory for a thread descriptor. |
---|
| 71 | // It can be called by the three functions: |
---|
[1] | 72 | // - thread_user_create() |
---|
[14] | 73 | // - thread_user_fork() |
---|
[1] | 74 | // - thread_kernel_create() |
---|
| 75 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[14] | 76 | // @ return pointer on thread descriptor if success / return NULL if failure. |
---|
[1] | 77 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[485] | 78 | static thread_t * thread_alloc( void ) |
---|
[1] | 79 | { |
---|
[171] | 80 | kmem_req_t req; // kmem request |
---|
[1] | 81 | |
---|
| 82 | // allocates memory for thread descriptor + kernel stack |
---|
[635] | 83 | req.type = KMEM_PPM; |
---|
| 84 | req.order = CONFIG_THREAD_DESC_ORDER; |
---|
[1] | 85 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 86 | |
---|
[635] | 87 | return kmem_alloc( &req ); |
---|
[1] | 88 | |
---|
[315] | 89 | } // end thread_alloc() |
---|
| 90 | |
---|
| 91 | |
---|
[14] | 92 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 93 | // This static function initializes a thread descriptor (kernel or user). |
---|
[438] | 94 | // It can be called by the four functions: |
---|
[14] | 95 | // - thread_user_create() |
---|
| 96 | // - thread_user_fork() |
---|
| 97 | // - thread_kernel_create() |
---|
[438] | 98 | // - thread_idle_init() |
---|
[625] | 99 | // The "type" and "trdid" fields must have been previously set. |
---|
[438] | 100 | // It updates the local DQDT. |
---|
[14] | 101 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[625] | 102 | // @ thread : pointer on local thread descriptor |
---|
| 103 | // @ process : pointer on local process descriptor. |
---|
| 104 | // @ type : thread type. |
---|
| 105 | // @ trdid : thread identifier |
---|
| 106 | // @ func : pointer on thread entry function. |
---|
| 107 | // @ args : pointer on thread entry function arguments. |
---|
| 108 | // @ core_lid : target core local index. |
---|
| 109 | // @ user_stack_vseg : local pointer on user stack vseg (user thread only) |
---|
[14] | 110 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 111 | static error_t thread_init( thread_t * thread, |
---|
| 112 | process_t * process, |
---|
| 113 | thread_type_t type, |
---|
[625] | 114 | trdid_t trdid, |
---|
[14] | 115 | void * func, |
---|
| 116 | void * args, |
---|
| 117 | lid_t core_lid, |
---|
[625] | 118 | vseg_t * user_stack_vseg ) |
---|
[14] | 119 | { |
---|
| 120 | |
---|
[635] | 121 | // check type and trdid fields are initialized |
---|
[625] | 122 | assert( (thread->type == type) , "bad type argument" ); |
---|
| 123 | assert( (thread->trdid == trdid) , "bad trdid argument" ); |
---|
[14] | 124 | |
---|
[564] | 125 | #if DEBUG_THREAD_INIT |
---|
[593] | 126 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 127 | thread_t * this = CURRENT_THREAD; |
---|
[564] | 128 | if( DEBUG_THREAD_INIT < cycle ) |
---|
[635] | 129 | printk("\n[%s] thread[%x,%x] enter for thread[%x,%x] / cycle %d\n", |
---|
| 130 | __FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle ); |
---|
[443] | 131 | #endif |
---|
| 132 | |
---|
[407] | 133 | // compute thread descriptor size without kernel stack |
---|
| 134 | uint32_t desc_size = (intptr_t)(&thread->signature) - (intptr_t)thread + 4; |
---|
| 135 | |
---|
[1] | 136 | // Initialize new thread descriptor |
---|
| 137 | thread->quantum = 0; // TODO |
---|
| 138 | thread->ticks_nr = 0; // TODO |
---|
[457] | 139 | thread->time_last_check = 0; // TODO |
---|
[625] | 140 | thread->core = &LOCAL_CLUSTER->core_tbl[core_lid]; |
---|
[1] | 141 | thread->process = process; |
---|
[564] | 142 | thread->busylocks = 0; |
---|
[1] | 143 | |
---|
[564] | 144 | #if DEBUG_BUSYLOCK |
---|
| 145 | xlist_root_init( XPTR( local_cxy , &thread->busylocks_root ) ); |
---|
[409] | 146 | #endif |
---|
[1] | 147 | |
---|
[625] | 148 | thread->user_stack_vseg = user_stack_vseg; |
---|
[407] | 149 | thread->k_stack_base = (intptr_t)thread + desc_size; |
---|
| 150 | thread->k_stack_size = CONFIG_THREAD_DESC_SIZE - desc_size; |
---|
[1] | 151 | thread->entry_func = func; // thread entry point |
---|
| 152 | thread->entry_args = args; // thread function arguments |
---|
[171] | 153 | thread->flags = 0; // all flags reset |
---|
[1] | 154 | thread->errno = 0; // no error detected |
---|
[407] | 155 | thread->fork_user = 0; // no user defined placement for fork |
---|
| 156 | thread->fork_cxy = 0; // user defined target cluster for fork |
---|
[409] | 157 | thread->blocked = THREAD_BLOCKED_GLOBAL; |
---|
[1] | 158 | |
---|
[564] | 159 | // initialize sched list |
---|
[1] | 160 | list_entry_init( &thread->sched_list ); |
---|
| 161 | |
---|
[564] | 162 | // initialize waiting queue entries |
---|
| 163 | list_entry_init( &thread->wait_list ); |
---|
| 164 | xlist_entry_init( XPTR( local_cxy , &thread->wait_xlist ) ); |
---|
| 165 | |
---|
| 166 | // initialize thread info |
---|
[1] | 167 | memset( &thread->info , 0 , sizeof(thread_info_t) ); |
---|
| 168 | |
---|
[564] | 169 | // initialize join_lock |
---|
| 170 | remote_busylock_init( XPTR( local_cxy , &thread->join_lock ), LOCK_THREAD_JOIN ); |
---|
[409] | 171 | |
---|
[1] | 172 | // initialise signature |
---|
| 173 | thread->signature = THREAD_SIGNATURE; |
---|
| 174 | |
---|
[443] | 175 | // FIXME define and call an architecture specific hal_thread_init() |
---|
| 176 | // function to initialise the save_sr field |
---|
[408] | 177 | thread->save_sr = 0xFF13; |
---|
| 178 | |
---|
[171] | 179 | // register new thread in core scheduler |
---|
[1] | 180 | sched_register_thread( thread->core , thread ); |
---|
| 181 | |
---|
[438] | 182 | // update DQDT |
---|
[583] | 183 | dqdt_increment_threads(); |
---|
[438] | 184 | |
---|
[641] | 185 | #if CONFIG_INSTRUMENTATION_PGFAULTS |
---|
| 186 | thread->info.false_pgfault_nr = 0; |
---|
| 187 | thread->info.false_pgfault_cost = 0; |
---|
| 188 | thread->info.false_pgfault_max = 0; |
---|
| 189 | thread->info.local_pgfault_nr = 0; |
---|
| 190 | thread->info.local_pgfault_cost = 0; |
---|
| 191 | thread->info.local_pgfault_max = 0; |
---|
| 192 | thread->info.global_pgfault_nr = 0; |
---|
| 193 | thread->info.global_pgfault_cost = 0; |
---|
| 194 | thread->info.global_pgfault_max = 0; |
---|
| 195 | #endif |
---|
| 196 | |
---|
[564] | 197 | #if DEBUG_THREAD_INIT |
---|
[443] | 198 | cycle = (uint32_t)hal_get_cycles(); |
---|
[564] | 199 | if( DEBUG_THREAD_INIT < cycle ) |
---|
[635] | 200 | printk("\n[%s] thread[%x,%x] exit for thread[%x,%x] / cycle %d\n", |
---|
| 201 | __FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle ); |
---|
[443] | 202 | #endif |
---|
| 203 | |
---|
[1] | 204 | return 0; |
---|
| 205 | |
---|
[296] | 206 | } // end thread_init() |
---|
| 207 | |
---|
[625] | 208 | ////////////////////////////////////////////////// |
---|
[23] | 209 | error_t thread_user_create( pid_t pid, |
---|
| 210 | void * start_func, |
---|
| 211 | void * start_arg, |
---|
[1] | 212 | pthread_attr_t * attr, |
---|
[23] | 213 | thread_t ** new_thread ) |
---|
[1] | 214 | { |
---|
| 215 | error_t error; |
---|
| 216 | thread_t * thread; // pointer on created thread descriptor |
---|
[625] | 217 | trdid_t trdid; // created thred identifier |
---|
[1] | 218 | process_t * process; // pointer to local process descriptor |
---|
| 219 | lid_t core_lid; // selected core local index |
---|
[625] | 220 | vseg_t * us_vseg; // user stack vseg |
---|
[1] | 221 | |
---|
[593] | 222 | assert( (attr != NULL) , "pthread attributes must be defined" ); |
---|
[5] | 223 | |
---|
[438] | 224 | #if DEBUG_THREAD_USER_CREATE |
---|
[593] | 225 | thread_t * this = CURRENT_THREAD; |
---|
| 226 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 227 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[593] | 228 | printk("\n[%s] thread[%x,%x] enter in cluster %x for process %x / cycle %d\n", |
---|
| 229 | __FUNCTION__, this->process->pid , this->trdid , local_cxy , pid , cycle ); |
---|
[433] | 230 | #endif |
---|
[428] | 231 | |
---|
[23] | 232 | // get process descriptor local copy |
---|
| 233 | process = process_get_local_copy( pid ); |
---|
[440] | 234 | |
---|
[23] | 235 | if( process == NULL ) |
---|
| 236 | { |
---|
| 237 | printk("\n[ERROR] in %s : cannot get process descriptor %x\n", |
---|
[625] | 238 | __FUNCTION__ , pid ); |
---|
| 239 | return -1; |
---|
[23] | 240 | } |
---|
| 241 | |
---|
[443] | 242 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 243 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[593] | 244 | printk("\n[%s] process descriptor = %x for process %x in cluster %x\n", |
---|
[443] | 245 | __FUNCTION__, process , pid , local_cxy ); |
---|
| 246 | #endif |
---|
| 247 | |
---|
[171] | 248 | // select a target core in local cluster |
---|
[407] | 249 | if( attr->attributes & PT_ATTR_CORE_DEFINED ) |
---|
[23] | 250 | { |
---|
[407] | 251 | core_lid = attr->lid; |
---|
| 252 | if( core_lid >= LOCAL_CLUSTER->cores_nr ) |
---|
| 253 | { |
---|
| 254 | printk("\n[ERROR] in %s : illegal core index attribute = %d\n", |
---|
| 255 | __FUNCTION__ , core_lid ); |
---|
[625] | 256 | return -1; |
---|
[407] | 257 | } |
---|
[23] | 258 | } |
---|
[407] | 259 | else |
---|
| 260 | { |
---|
[637] | 261 | core_lid = cluster_select_local_core( local_cxy ); |
---|
[407] | 262 | } |
---|
[1] | 263 | |
---|
[443] | 264 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 265 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[593] | 266 | printk("\n[%s] core[%x,%d] selected\n", |
---|
[443] | 267 | __FUNCTION__, local_cxy , core_lid ); |
---|
| 268 | #endif |
---|
| 269 | |
---|
[625] | 270 | // allocate memory for thread descriptor |
---|
| 271 | thread = thread_alloc(); |
---|
[1] | 272 | |
---|
[625] | 273 | if( thread == NULL ) |
---|
[23] | 274 | { |
---|
[625] | 275 | printk("\n[ERROR] in %s : cannot create new thread in cluster %x\n", |
---|
| 276 | __FUNCTION__, local_cxy ); |
---|
| 277 | return -1; |
---|
[171] | 278 | } |
---|
[23] | 279 | |
---|
[457] | 280 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 281 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[625] | 282 | printk("\n[%s] new thread descriptor %x allocated\n", |
---|
| 283 | __FUNCTION__, thread ); |
---|
[457] | 284 | #endif |
---|
| 285 | |
---|
[625] | 286 | // set type in thread descriptor |
---|
| 287 | thread->type = THREAD_USER; |
---|
[1] | 288 | |
---|
[625] | 289 | // register new thread in process descriptor, and get a TRDID |
---|
| 290 | error = process_register_thread( process, thread , &trdid ); |
---|
| 291 | |
---|
| 292 | if( error ) |
---|
[23] | 293 | { |
---|
[625] | 294 | printk("\n[ERROR] in %s : cannot register new thread in process %x\n", |
---|
| 295 | __FUNCTION__, pid ); |
---|
| 296 | thread_destroy( thread ); |
---|
| 297 | return -1; |
---|
[23] | 298 | } |
---|
[14] | 299 | |
---|
[625] | 300 | // set trdid in thread descriptor |
---|
| 301 | thread->trdid = trdid; |
---|
| 302 | |
---|
[443] | 303 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 304 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[625] | 305 | printk("\n[%s] new thread %x registered in process %x\n", |
---|
| 306 | __FUNCTION__, trdid, pid ); |
---|
[443] | 307 | #endif |
---|
| 308 | |
---|
[625] | 309 | // allocate a stack from local VMM |
---|
| 310 | us_vseg = vmm_create_vseg( process, |
---|
| 311 | VSEG_TYPE_STACK, |
---|
| 312 | LTID_FROM_TRDID( trdid ), |
---|
| 313 | 0, // size unused |
---|
| 314 | 0, // file_offset unused |
---|
| 315 | 0, // file_size unused |
---|
| 316 | XPTR_NULL, // mapper_xp unused |
---|
| 317 | local_cxy ); |
---|
| 318 | |
---|
| 319 | if( us_vseg == NULL ) |
---|
| 320 | { |
---|
| 321 | printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ ); |
---|
| 322 | process_remove_thread( thread ); |
---|
| 323 | thread_destroy( thread ); |
---|
| 324 | return -1; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 328 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
| 329 | printk("\n[%s] stack vseg created / vpn_base %x / %d pages\n", |
---|
| 330 | __FUNCTION__, us_vseg->vpn_base, us_vseg->vpn_size ); |
---|
| 331 | #endif |
---|
| 332 | |
---|
[171] | 333 | // initialize thread descriptor |
---|
[14] | 334 | error = thread_init( thread, |
---|
| 335 | process, |
---|
| 336 | THREAD_USER, |
---|
[625] | 337 | trdid, |
---|
[23] | 338 | start_func, |
---|
| 339 | start_arg, |
---|
[14] | 340 | core_lid, |
---|
[625] | 341 | us_vseg ); |
---|
[171] | 342 | if( error ) |
---|
[14] | 343 | { |
---|
[23] | 344 | printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ ); |
---|
[625] | 345 | vmm_remove_vseg( process , us_vseg ); |
---|
| 346 | process_remove_thread( thread ); |
---|
| 347 | thread_destroy( thread ); |
---|
| 348 | return -1; |
---|
[14] | 349 | } |
---|
| 350 | |
---|
[443] | 351 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 352 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[625] | 353 | printk("\n[%s] new thread %x in process %x initialised\n", |
---|
| 354 | __FUNCTION__, thread->trdid, process->pid ); |
---|
[443] | 355 | #endif |
---|
| 356 | |
---|
[14] | 357 | // set DETACHED flag if required |
---|
[407] | 358 | if( attr->attributes & PT_ATTR_DETACH ) |
---|
| 359 | { |
---|
| 360 | thread->flags |= THREAD_FLAG_DETACHED; |
---|
| 361 | } |
---|
[1] | 362 | |
---|
[171] | 363 | // allocate & initialize CPU context |
---|
[457] | 364 | if( hal_cpu_context_alloc( thread ) ) |
---|
[23] | 365 | { |
---|
| 366 | printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ ); |
---|
[625] | 367 | vmm_remove_vseg( process , us_vseg ); |
---|
| 368 | process_remove_thread( thread ); |
---|
| 369 | thread_destroy( thread ); |
---|
| 370 | return -1; |
---|
[23] | 371 | } |
---|
[457] | 372 | hal_cpu_context_init( thread ); |
---|
[23] | 373 | |
---|
[457] | 374 | // allocate & initialize FPU context |
---|
[407] | 375 | if( hal_fpu_context_alloc( thread ) ) |
---|
[23] | 376 | { |
---|
| 377 | printk("\n[ERROR] in %s : cannot create FPU context\n", __FUNCTION__ ); |
---|
[625] | 378 | vmm_remove_vseg( process , us_vseg ); |
---|
| 379 | process_remove_thread( thread ); |
---|
| 380 | thread_destroy( thread ); |
---|
| 381 | return -1; |
---|
[23] | 382 | } |
---|
[457] | 383 | hal_fpu_context_init( thread ); |
---|
[23] | 384 | |
---|
[457] | 385 | #if( DEBUG_THREAD_USER_CREATE & 1) |
---|
| 386 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[593] | 387 | printk("\n[%s] CPU & FPU contexts created\n", |
---|
[457] | 388 | __FUNCTION__, thread->trdid ); |
---|
[637] | 389 | hal_vmm_display( XPTR( local_cxy , process ) , true ); |
---|
[457] | 390 | #endif |
---|
| 391 | |
---|
[438] | 392 | #if DEBUG_THREAD_USER_CREATE |
---|
[433] | 393 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 394 | if( DEBUG_THREAD_USER_CREATE < cycle ) |
---|
[593] | 395 | printk("\n[%s] thread[%x,%x] exit / new_thread %x / core %d / cycle %d\n", |
---|
| 396 | __FUNCTION__, this->process->pid , this->trdid , thread->trdid, core_lid, cycle ); |
---|
[433] | 397 | #endif |
---|
[1] | 398 | |
---|
| 399 | *new_thread = thread; |
---|
| 400 | return 0; |
---|
[14] | 401 | |
---|
[296] | 402 | } // end thread_user_create() |
---|
| 403 | |
---|
[408] | 404 | /////////////////////////////////////////////////////// |
---|
| 405 | error_t thread_user_fork( xptr_t parent_thread_xp, |
---|
| 406 | process_t * child_process, |
---|
| 407 | thread_t ** child_thread ) |
---|
[1] | 408 | { |
---|
| 409 | error_t error; |
---|
[625] | 410 | thread_t * child_ptr; // local pointer on child thread |
---|
| 411 | trdid_t child_trdid; // child thread identifier |
---|
[408] | 412 | lid_t core_lid; // selected core local index |
---|
| 413 | thread_t * parent_ptr; // local pointer on remote parent thread |
---|
| 414 | cxy_t parent_cxy; // parent thread cluster |
---|
| 415 | process_t * parent_process; // local pointer on parent process |
---|
| 416 | xptr_t parent_gpt_xp; // extended pointer on parent thread GPT |
---|
[625] | 417 | void * parent_func; // parent thread entry_func |
---|
| 418 | void * parent_args; // parent thread entry_args |
---|
| 419 | uint32_t parent_flags; // parent_thread flags |
---|
| 420 | vseg_t * parent_us_vseg; // parent thread user stack vseg |
---|
| 421 | vseg_t * child_us_vseg; // child thread user stack vseg |
---|
[5] | 422 | |
---|
[438] | 423 | #if DEBUG_THREAD_USER_FORK |
---|
[593] | 424 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 425 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 426 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
[625] | 427 | printk("\n[%s] thread[%x,%x] enter for child_process %x / cycle %d\n", |
---|
[593] | 428 | __FUNCTION__, this->process->pid, this->trdid, child_process->pid, cycle ); |
---|
[433] | 429 | #endif |
---|
[408] | 430 | |
---|
[1] | 431 | // select a target core in local cluster |
---|
[637] | 432 | core_lid = cluster_select_local_core( local_cxy ); |
---|
[1] | 433 | |
---|
[625] | 434 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
| 435 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
| 436 | printk("\n[%s] thread[%x,%x] selected core [%x,%d]\n", |
---|
| 437 | __FUNCTION__, this->process->pid, this->trdid, local_cxy, core_lid ); |
---|
| 438 | #endif |
---|
| 439 | |
---|
[408] | 440 | // get cluster and local pointer on parent thread descriptor |
---|
| 441 | parent_cxy = GET_CXY( parent_thread_xp ); |
---|
[469] | 442 | parent_ptr = GET_PTR( parent_thread_xp ); |
---|
[1] | 443 | |
---|
[625] | 444 | // get relevant infos from parent thread |
---|
| 445 | parent_func = (void *) hal_remote_lpt( XPTR(parent_cxy,&parent_ptr->entry_func )); |
---|
| 446 | parent_args = (void *) hal_remote_lpt( XPTR(parent_cxy,&parent_ptr->entry_args )); |
---|
| 447 | parent_flags = (uint32_t)hal_remote_l32( XPTR(parent_cxy,&parent_ptr->flags )); |
---|
| 448 | parent_us_vseg = (vseg_t *)hal_remote_lpt( XPTR(parent_cxy,&parent_ptr->user_stack_vseg )); |
---|
[1] | 449 | |
---|
[408] | 450 | // get pointer on parent process in parent thread cluster |
---|
| 451 | parent_process = (process_t *)hal_remote_lpt( XPTR( parent_cxy, |
---|
| 452 | &parent_ptr->process ) ); |
---|
| 453 | |
---|
[625] | 454 | // build extended pointer on parent GPT in parent thread cluster |
---|
[408] | 455 | parent_gpt_xp = XPTR( parent_cxy , &parent_process->vmm.gpt ); |
---|
| 456 | |
---|
[625] | 457 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
| 458 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
| 459 | printk("\n[%s] thread[%x,%x] get parent GPT\n", |
---|
| 460 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
| 461 | #endif |
---|
| 462 | |
---|
[408] | 463 | // allocate memory for child thread descriptor |
---|
| 464 | child_ptr = thread_alloc(); |
---|
[625] | 465 | |
---|
[408] | 466 | if( child_ptr == NULL ) |
---|
[23] | 467 | { |
---|
[625] | 468 | printk("\n[ERROR] in %s : cannot allocate new thread\n", |
---|
| 469 | __FUNCTION__ ); |
---|
[408] | 470 | return -1; |
---|
[23] | 471 | } |
---|
[14] | 472 | |
---|
[625] | 473 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
| 474 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
| 475 | printk("\n[%s] thread[%x,%x] allocated new thread descriptor %x\n", |
---|
| 476 | __FUNCTION__, this->process->pid, this->trdid, child_ptr ); |
---|
| 477 | #endif |
---|
| 478 | |
---|
| 479 | // set type in thread descriptor |
---|
| 480 | child_ptr->type = THREAD_USER; |
---|
| 481 | |
---|
| 482 | // register new thread in process descriptor, and get a TRDID |
---|
| 483 | error = process_register_thread( child_process, child_ptr , &child_trdid ); |
---|
| 484 | |
---|
| 485 | if( error ) |
---|
| 486 | { |
---|
| 487 | printk("\n[ERROR] in %s : cannot register new thread in process %x\n", |
---|
| 488 | __FUNCTION__, child_process->pid ); |
---|
| 489 | thread_destroy( child_ptr ); |
---|
| 490 | return -1; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | // set trdid in thread descriptor |
---|
| 494 | child_ptr->trdid = child_trdid; |
---|
| 495 | |
---|
| 496 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
| 497 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
| 498 | printk("\n[%s] thread[%x,%x] registered child thread %x in child process %x\n", |
---|
| 499 | __FUNCTION__, this->process->pid, this->trdid, child_trdid, child_process->pid ); |
---|
| 500 | #endif |
---|
| 501 | |
---|
| 502 | // get an user stack vseg from local VMM allocator |
---|
| 503 | child_us_vseg = vmm_create_vseg( child_process, |
---|
| 504 | VSEG_TYPE_STACK, |
---|
| 505 | LTID_FROM_TRDID( child_trdid ), |
---|
| 506 | 0, // size unused |
---|
| 507 | 0, // file_offset unused |
---|
| 508 | 0, // file_size unused |
---|
| 509 | XPTR_NULL, // mapper_xp unused |
---|
| 510 | local_cxy ); |
---|
| 511 | if( child_us_vseg == NULL ) |
---|
| 512 | { |
---|
| 513 | printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ ); |
---|
| 514 | process_remove_thread( child_ptr ); |
---|
| 515 | thread_destroy( child_ptr ); |
---|
| 516 | return -1; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
| 520 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
| 521 | printk("\n[%s] thread[%x,%x] created an user stack vseg / vpn_base %x / %d pages\n", |
---|
| 522 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 523 | child_us_vseg->vpn_base, child_us_vseg->vpn_size ); |
---|
| 524 | #endif |
---|
| 525 | |
---|
[171] | 526 | // initialize thread descriptor |
---|
[408] | 527 | error = thread_init( child_ptr, |
---|
| 528 | child_process, |
---|
[14] | 529 | THREAD_USER, |
---|
[625] | 530 | child_trdid, |
---|
| 531 | parent_func, |
---|
| 532 | parent_args, |
---|
[14] | 533 | core_lid, |
---|
[625] | 534 | child_us_vseg ); |
---|
[23] | 535 | if( error ) |
---|
[14] | 536 | { |
---|
[408] | 537 | printk("\n[ERROR] in %s : cannot initialize child thread\n", __FUNCTION__ ); |
---|
[625] | 538 | vmm_remove_vseg( child_process , child_us_vseg ); |
---|
| 539 | process_remove_thread( child_ptr ); |
---|
| 540 | thread_destroy( child_ptr ); |
---|
| 541 | return -1; |
---|
[14] | 542 | } |
---|
| 543 | |
---|
[564] | 544 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
| 545 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
[593] | 546 | printk("\n[%s] thread[%x,%x] initialised thread %x in process %x\n", |
---|
| 547 | __FUNCTION__, this->process->pid, this->trdid, child_ptr->trdid, child_process->pid ); |
---|
[564] | 548 | #endif |
---|
| 549 | |
---|
[408] | 550 | // set detached flag if required |
---|
[625] | 551 | if( parent_flags & THREAD_FLAG_DETACHED ) child_ptr->flags = THREAD_FLAG_DETACHED; |
---|
[1] | 552 | |
---|
[625] | 553 | // allocate a CPU context for child thread |
---|
[408] | 554 | if( hal_cpu_context_alloc( child_ptr ) ) |
---|
[23] | 555 | { |
---|
[407] | 556 | printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ ); |
---|
[625] | 557 | vmm_remove_vseg( child_process , child_us_vseg ); |
---|
| 558 | process_remove_thread( child_ptr ); |
---|
| 559 | thread_destroy( child_ptr ); |
---|
[408] | 560 | return -1; |
---|
[23] | 561 | } |
---|
| 562 | |
---|
[625] | 563 | // allocate a FPU context for child thread |
---|
[408] | 564 | if( hal_fpu_context_alloc( child_ptr ) ) |
---|
[23] | 565 | { |
---|
[407] | 566 | printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ ); |
---|
[625] | 567 | vmm_remove_vseg( child_process , child_us_vseg ); |
---|
| 568 | process_remove_thread( child_ptr ); |
---|
| 569 | thread_destroy( child_ptr ); |
---|
[408] | 570 | return -1; |
---|
[23] | 571 | } |
---|
| 572 | |
---|
[564] | 573 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
| 574 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
[593] | 575 | printk("\n[%s] thread[%x,%x] created CPU & FPU contexts for thread %x in process %x\n", |
---|
| 576 | __FUNCTION__, this->process->pid, this->trdid, child_ptr->trdid, child_process->pid ); |
---|
[564] | 577 | #endif |
---|
| 578 | |
---|
[625] | 579 | // scan parent GPT, and copy all valid entries |
---|
| 580 | // associated to user stack vseg into child GPT |
---|
| 581 | vpn_t parent_vpn; |
---|
| 582 | vpn_t child_vpn; |
---|
| 583 | bool_t mapped; |
---|
| 584 | ppn_t ppn; |
---|
| 585 | vpn_t parent_vpn_base = hal_remote_l32( XPTR( parent_cxy, &parent_us_vseg->vpn_base ) ); |
---|
| 586 | vpn_t parent_vpn_size = hal_remote_l32( XPTR( parent_cxy, &parent_us_vseg->vpn_size ) ); |
---|
| 587 | vpn_t child_vpn_base = child_us_vseg->vpn_base; |
---|
[635] | 588 | |
---|
[625] | 589 | for( parent_vpn = parent_vpn_base , child_vpn = child_vpn_base ; |
---|
| 590 | parent_vpn < (parent_vpn_base + parent_vpn_size) ; |
---|
| 591 | parent_vpn++ , child_vpn++ ) |
---|
[408] | 592 | { |
---|
| 593 | error = hal_gpt_pte_copy( &child_process->vmm.gpt, |
---|
[625] | 594 | child_vpn, |
---|
[408] | 595 | parent_gpt_xp, |
---|
[625] | 596 | parent_vpn, |
---|
[408] | 597 | true, // set cow |
---|
| 598 | &ppn, |
---|
| 599 | &mapped ); |
---|
| 600 | if( error ) |
---|
| 601 | { |
---|
| 602 | printk("\n[ERROR] in %s : cannot update child GPT\n", __FUNCTION__ ); |
---|
[625] | 603 | vmm_remove_vseg( child_process , child_us_vseg ); |
---|
| 604 | process_remove_thread( child_ptr ); |
---|
| 605 | thread_destroy( child_ptr ); |
---|
[408] | 606 | return -1; |
---|
| 607 | } |
---|
| 608 | |
---|
[625] | 609 | // increment pending forks counter for a mapped page |
---|
[408] | 610 | if( mapped ) |
---|
| 611 | { |
---|
[469] | 612 | // get pointers on the page descriptor |
---|
[408] | 613 | xptr_t page_xp = ppm_ppn2page( ppn ); |
---|
| 614 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
[469] | 615 | page_t * page_ptr = GET_PTR( page_xp ); |
---|
| 616 | |
---|
[625] | 617 | // build extended pointers on forks and lock fields |
---|
[469] | 618 | xptr_t forks_xp = XPTR( page_cxy , &page_ptr->forks ); |
---|
| 619 | xptr_t lock_xp = XPTR( page_cxy , &page_ptr->lock ); |
---|
| 620 | |
---|
[564] | 621 | // get lock protecting page |
---|
| 622 | remote_busylock_acquire( lock_xp ); |
---|
| 623 | |
---|
| 624 | // increment the forks counter in page descriptor |
---|
[473] | 625 | hal_remote_atomic_add( forks_xp , 1 ); |
---|
[408] | 626 | |
---|
[564] | 627 | // release lock protecting page |
---|
| 628 | remote_busylock_release( lock_xp ); |
---|
[625] | 629 | } |
---|
| 630 | } |
---|
[564] | 631 | |
---|
[438] | 632 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
| 633 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
[635] | 634 | printk("\n[%s] thread[%x,%x] copied STACK vseg PTEs & set COW in child GPT\n", |
---|
[625] | 635 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
[433] | 636 | #endif |
---|
[408] | 637 | |
---|
[625] | 638 | // set COW flag for all mapped entries of user stack vseg in parent GPT |
---|
| 639 | hal_gpt_set_cow( parent_gpt_xp, |
---|
| 640 | parent_vpn_base, |
---|
| 641 | parent_vpn_size ); |
---|
[408] | 642 | |
---|
[625] | 643 | #if (DEBUG_THREAD_USER_FORK & 1) |
---|
| 644 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
[635] | 645 | printk("\n[%s] thread[%x,%x] set COW for STACK vseg in parent GPT\n", |
---|
[625] | 646 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
| 647 | #endif |
---|
| 648 | |
---|
| 649 | // return child pointer |
---|
| 650 | *child_thread = child_ptr; |
---|
| 651 | |
---|
[438] | 652 | #if DEBUG_THREAD_USER_FORK |
---|
[433] | 653 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 654 | if( DEBUG_THREAD_USER_FORK < cycle ) |
---|
[625] | 655 | printk("\n[%s] thread[%x,%x] exit / created thread[%x,%x] / cycle %d\n", |
---|
| 656 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 657 | child_ptr->process->pid, child_ptr->trdid, cycle ); |
---|
[433] | 658 | #endif |
---|
[407] | 659 | |
---|
[1] | 660 | return 0; |
---|
[5] | 661 | |
---|
[296] | 662 | } // end thread_user_fork() |
---|
| 663 | |
---|
[457] | 664 | //////////////////////////////////////////////// |
---|
| 665 | error_t thread_user_exec( void * entry_func, |
---|
| 666 | uint32_t argc, |
---|
| 667 | char ** argv ) |
---|
| 668 | { |
---|
| 669 | thread_t * thread = CURRENT_THREAD; |
---|
| 670 | process_t * process = thread->process; |
---|
| 671 | |
---|
| 672 | #if DEBUG_THREAD_USER_EXEC |
---|
| 673 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 674 | if( DEBUG_THREAD_USER_EXEC < cycle ) |
---|
[651] | 675 | printk("\n[%s] thread[%x,%x] enter / entry %x / cycle %d\n", |
---|
| 676 | __FUNCTION__, process->pid, thread->trdid, entry_func , cycle ); |
---|
[457] | 677 | #endif |
---|
| 678 | |
---|
[564] | 679 | // check parent thread attributes |
---|
| 680 | assert( (thread->type == THREAD_USER ) , "bad type" ); |
---|
| 681 | assert( (thread->signature == THREAD_SIGNATURE) , "bad signature" ); |
---|
| 682 | assert( (thread->busylocks == 0) , "bad busylocks" ); |
---|
[457] | 683 | |
---|
| 684 | // re-initialize various thread descriptor fields |
---|
| 685 | thread->quantum = 0; // TODO |
---|
| 686 | thread->ticks_nr = 0; // TODO |
---|
| 687 | thread->time_last_check = 0; // TODO |
---|
| 688 | |
---|
| 689 | thread->entry_func = entry_func; |
---|
| 690 | thread->main_argc = argc; |
---|
| 691 | thread->main_argv = argv; |
---|
| 692 | |
---|
| 693 | // the main thread is always detached |
---|
| 694 | thread->flags = THREAD_FLAG_DETACHED; |
---|
| 695 | thread->blocked = 0; |
---|
| 696 | thread->errno = 0; |
---|
| 697 | thread->fork_user = 0; // not inherited |
---|
| 698 | thread->fork_cxy = 0; // not inherited |
---|
| 699 | |
---|
[564] | 700 | // re-initialize busylocks counters |
---|
| 701 | thread->busylocks = 0; |
---|
| 702 | |
---|
[457] | 703 | // reset thread info |
---|
| 704 | memset( &thread->info , 0 , sizeof(thread_info_t) ); |
---|
| 705 | |
---|
[564] | 706 | // re-initialize join_lock |
---|
| 707 | remote_busylock_init( XPTR( local_cxy , &thread->join_lock ), LOCK_THREAD_JOIN ); |
---|
[457] | 708 | |
---|
| 709 | // allocate an user stack vseg for main thread |
---|
[625] | 710 | vseg_t * us_vseg = vmm_create_vseg( process, |
---|
| 711 | VSEG_TYPE_STACK, |
---|
| 712 | LTID_FROM_TRDID( thread->trdid ), |
---|
| 713 | 0, // length unused |
---|
| 714 | 0, // file_offset unused |
---|
| 715 | 0, // file_size unused |
---|
| 716 | XPTR_NULL, // mapper_xp unused |
---|
| 717 | local_cxy ); |
---|
| 718 | if( us_vseg == NULL ) |
---|
[457] | 719 | { |
---|
| 720 | printk("\n[ERROR] in %s : cannot create stack vseg for main thread\n", __FUNCTION__ ); |
---|
| 721 | return -1; |
---|
| 722 | } |
---|
| 723 | |
---|
[469] | 724 | // update user stack in thread descriptor |
---|
[625] | 725 | thread->user_stack_vseg = us_vseg; |
---|
[457] | 726 | |
---|
| 727 | // release FPU ownership if required |
---|
| 728 | if( thread->core->fpu_owner == thread ) thread->core->fpu_owner = NULL; |
---|
| 729 | |
---|
| 730 | // re-initialize FPU context |
---|
| 731 | hal_fpu_context_init( thread ); |
---|
| 732 | |
---|
| 733 | #if DEBUG_THREAD_USER_EXEC |
---|
| 734 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 735 | if( DEBUG_THREAD_USER_EXEC < cycle ) |
---|
[593] | 736 | printk("\n[%s] thread[%x,%x] set CPU context & jump to user code / cycle %d\n", |
---|
| 737 | __FUNCTION__, process->pid, thread->trdid, cycle ); |
---|
[637] | 738 | hal_vmm_display( XPTR( local_cxy , process ) , true ); |
---|
[457] | 739 | #endif |
---|
| 740 | |
---|
| 741 | // re-initialize CPU context... and jump to user code |
---|
| 742 | hal_cpu_context_exec( thread ); |
---|
| 743 | |
---|
[564] | 744 | assert( false, "we should not execute this code"); |
---|
[457] | 745 | |
---|
| 746 | return 0; |
---|
| 747 | |
---|
| 748 | } // end thread_user_exec() |
---|
| 749 | |
---|
[1] | 750 | ///////////////////////////////////////////////////////// |
---|
| 751 | error_t thread_kernel_create( thread_t ** new_thread, |
---|
| 752 | thread_type_t type, |
---|
[171] | 753 | void * func, |
---|
| 754 | void * args, |
---|
[1] | 755 | lid_t core_lid ) |
---|
| 756 | { |
---|
| 757 | error_t error; |
---|
[14] | 758 | thread_t * thread; // pointer on new thread descriptor |
---|
[625] | 759 | trdid_t trdid; // new thread identifier |
---|
[1] | 760 | |
---|
[593] | 761 | thread_t * this = CURRENT_THREAD; |
---|
[1] | 762 | |
---|
[593] | 763 | assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) , |
---|
| 764 | "illegal thread type" ); |
---|
[1] | 765 | |
---|
[593] | 766 | assert( (core_lid < LOCAL_CLUSTER->cores_nr) , |
---|
| 767 | "illegal core_lid" ); |
---|
| 768 | |
---|
[438] | 769 | #if DEBUG_THREAD_KERNEL_CREATE |
---|
[593] | 770 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 771 | if( DEBUG_THREAD_KERNEL_CREATE < cycle ) |
---|
[593] | 772 | printk("\n[%s] thread[%x,%x] enter / requested_type %s / cycle %d\n", |
---|
| 773 | __FUNCTION__, this->process->pid, this->trdid, thread_type_str(type), cycle ); |
---|
[433] | 774 | #endif |
---|
| 775 | |
---|
[171] | 776 | // allocate memory for new thread descriptor |
---|
[14] | 777 | thread = thread_alloc(); |
---|
| 778 | |
---|
[581] | 779 | if( thread == NULL ) |
---|
| 780 | { |
---|
| 781 | printk("\n[ERROR] in %s : thread %x in process %x\n" |
---|
| 782 | " no memory for thread descriptor\n", |
---|
[593] | 783 | __FUNCTION__, this->trdid, this->process->pid ); |
---|
[581] | 784 | return ENOMEM; |
---|
| 785 | } |
---|
[14] | 786 | |
---|
[625] | 787 | // set type in thread descriptor |
---|
| 788 | thread->type = type; |
---|
| 789 | |
---|
| 790 | // register new thread in local kernel process descriptor, and get a TRDID |
---|
| 791 | error = process_register_thread( &process_zero , thread , &trdid ); |
---|
| 792 | |
---|
| 793 | if( error ) |
---|
| 794 | { |
---|
| 795 | printk("\n[ERROR] in %s : cannot register thread in kernel process\n", __FUNCTION__ ); |
---|
| 796 | return -1; |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | // set trdid in thread descriptor |
---|
| 800 | thread->trdid = trdid; |
---|
| 801 | |
---|
[171] | 802 | // initialize thread descriptor |
---|
[14] | 803 | error = thread_init( thread, |
---|
| 804 | &process_zero, |
---|
| 805 | type, |
---|
[625] | 806 | trdid, |
---|
[14] | 807 | func, |
---|
| 808 | args, |
---|
| 809 | core_lid, |
---|
[625] | 810 | NULL ); // no user stack for a kernel thread |
---|
[14] | 811 | |
---|
[171] | 812 | if( error ) // release allocated memory for thread descriptor |
---|
[1] | 813 | { |
---|
[625] | 814 | printk("\n[ERROR] in %s : cannot initialize thread descriptor\n", __FUNCTION__ ); |
---|
| 815 | thread_destroy( thread ); |
---|
[457] | 816 | return ENOMEM; |
---|
[1] | 817 | } |
---|
| 818 | |
---|
[171] | 819 | // allocate & initialize CPU context |
---|
[457] | 820 | error = hal_cpu_context_alloc( thread ); |
---|
[581] | 821 | |
---|
[457] | 822 | if( error ) |
---|
| 823 | { |
---|
[581] | 824 | printk("\n[ERROR] in %s : thread %x in process %x\n" |
---|
[593] | 825 | " cannot create CPU context\n", |
---|
| 826 | __FUNCTION__, this->trdid, this->process->pid ); |
---|
[625] | 827 | thread_destroy( thread ); |
---|
[457] | 828 | return EINVAL; |
---|
| 829 | } |
---|
[581] | 830 | |
---|
[457] | 831 | hal_cpu_context_init( thread ); |
---|
[14] | 832 | |
---|
[583] | 833 | // set THREAD_BLOCKED_IDLE for DEV threads |
---|
| 834 | if( type == THREAD_DEV ) thread->blocked |= THREAD_BLOCKED_IDLE; |
---|
[457] | 835 | |
---|
[438] | 836 | #if DEBUG_THREAD_KERNEL_CREATE |
---|
[433] | 837 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 838 | if( DEBUG_THREAD_KERNEL_CREATE < cycle ) |
---|
[593] | 839 | printk("\n[%s] thread[%x,%x] exit / new_thread %x / type %s / cycle %d\n", |
---|
| 840 | __FUNCTION__, this->process->pid, this->trdid, thread, thread_type_str(type), cycle ); |
---|
[433] | 841 | #endif |
---|
[1] | 842 | |
---|
[171] | 843 | *new_thread = thread; |
---|
[1] | 844 | return 0; |
---|
[5] | 845 | |
---|
[296] | 846 | } // end thread_kernel_create() |
---|
| 847 | |
---|
[457] | 848 | ////////////////////////////////////////////// |
---|
| 849 | void thread_idle_init( thread_t * thread, |
---|
| 850 | thread_type_t type, |
---|
| 851 | void * func, |
---|
| 852 | void * args, |
---|
| 853 | lid_t core_lid ) |
---|
[14] | 854 | { |
---|
[625] | 855 | trdid_t trdid; |
---|
| 856 | error_t error; |
---|
[14] | 857 | |
---|
[564] | 858 | // check arguments |
---|
| 859 | assert( (type == THREAD_IDLE) , "illegal thread type" ); |
---|
| 860 | assert( (core_lid < LOCAL_CLUSTER->cores_nr) , "illegal core index" ); |
---|
| 861 | |
---|
[625] | 862 | // set type in thread descriptor |
---|
| 863 | thread->type = THREAD_IDLE; |
---|
| 864 | |
---|
| 865 | // register idle thread in local kernel process descriptor, and get a TRDID |
---|
| 866 | error = process_register_thread( &process_zero , thread , &trdid ); |
---|
| 867 | |
---|
| 868 | assert( (error == 0), "cannot register idle_thread in kernel process" ); |
---|
| 869 | |
---|
| 870 | // set trdid in thread descriptor |
---|
| 871 | thread->trdid = trdid; |
---|
| 872 | |
---|
[457] | 873 | // initialize thread descriptor |
---|
[625] | 874 | error = thread_init( thread, |
---|
| 875 | &process_zero, |
---|
| 876 | THREAD_IDLE, |
---|
| 877 | trdid, |
---|
| 878 | func, |
---|
| 879 | args, |
---|
| 880 | core_lid, |
---|
| 881 | NULL ); // no user stack for a kernel thread |
---|
[14] | 882 | |
---|
[625] | 883 | assert( (error == 0), "cannot initialize idle_thread" ); |
---|
[457] | 884 | |
---|
[14] | 885 | // allocate & initialize CPU context if success |
---|
[457] | 886 | error = hal_cpu_context_alloc( thread ); |
---|
[171] | 887 | |
---|
[625] | 888 | assert( (error == 0), "cannot allocate CPU context" ); |
---|
[14] | 889 | |
---|
[457] | 890 | hal_cpu_context_init( thread ); |
---|
| 891 | |
---|
[438] | 892 | } // end thread_idle_init() |
---|
[407] | 893 | |
---|
[625] | 894 | //////////////////////////////////////////// |
---|
| 895 | uint32_t thread_destroy( thread_t * thread ) |
---|
[1] | 896 | { |
---|
[625] | 897 | reg_t save_sr; |
---|
| 898 | uint32_t count; |
---|
[1] | 899 | |
---|
[625] | 900 | thread_type_t type = thread->type; |
---|
| 901 | process_t * process = thread->process; |
---|
| 902 | core_t * core = thread->core; |
---|
[1] | 903 | |
---|
[641] | 904 | #if DEBUG_THREAD_DESTROY |
---|
[640] | 905 | uint32_t cycle; |
---|
[583] | 906 | thread_t * this = CURRENT_THREAD; |
---|
[640] | 907 | #endif |
---|
| 908 | |
---|
| 909 | #if (DEBUG_THREAD_DESTROY & 1) |
---|
| 910 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 911 | if( DEBUG_THREAD_DESTROY < cycle ) |
---|
[593] | 912 | printk("\n[%s] thread[%x,%x] enter to destroy thread[%x,%x] / cycle %d\n", |
---|
[583] | 913 | __FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle ); |
---|
[433] | 914 | #endif |
---|
[1] | 915 | |
---|
[625] | 916 | // check calling thread busylocks counter |
---|
[583] | 917 | thread_assert_can_yield( thread , __FUNCTION__ ); |
---|
[171] | 918 | |
---|
[635] | 919 | #if CONFIG_INSTRUMENTATION_PGFAULTS |
---|
[640] | 920 | process->vmm.false_pgfault_nr += thread->info.false_pgfault_nr; |
---|
[641] | 921 | process->vmm.false_pgfault_cost += thread->info.false_pgfault_cost; |
---|
[640] | 922 | process->vmm.local_pgfault_nr += thread->info.local_pgfault_nr; |
---|
[641] | 923 | process->vmm.local_pgfault_cost += thread->info.local_pgfault_cost; |
---|
[640] | 924 | process->vmm.global_pgfault_nr += thread->info.global_pgfault_nr; |
---|
| 925 | process->vmm.global_pgfault_cost += thread->info.global_pgfault_cost; |
---|
[635] | 926 | #endif |
---|
[1] | 927 | |
---|
[640] | 928 | #if (CONFIG_INSTRUMENTATION_PGFAULTS & 1) |
---|
| 929 | uint32_t false_nr = thread->info.false_pgfault_nr; |
---|
[641] | 930 | uint32_t false_cost = thread->info.false_pgfault_cost; |
---|
| 931 | uint32_t false_max = thread->info.false_pgfault_max; |
---|
| 932 | uint32_t false_one = false_nr ? (false_cost / false_nr ) : 0; |
---|
| 933 | |
---|
[640] | 934 | uint32_t local_nr = thread->info.local_pgfault_nr; |
---|
[641] | 935 | uint32_t local_cost = thread->info.local_pgfault_cost; |
---|
| 936 | uint32_t local_max = thread->info.local_pgfault_max; |
---|
| 937 | uint32_t local_one = local_nr ? (local_cost / local_nr ) : 0; |
---|
| 938 | |
---|
[640] | 939 | uint32_t global_nr = thread->info.global_pgfault_nr; |
---|
| 940 | uint32_t global_cost = thread->info.global_pgfault_cost; |
---|
[641] | 941 | uint32_t global_max = thread->info.global_pgfault_max; |
---|
| 942 | uint32_t global_one = global_nr ? (global_cost / global_nr) : 0; |
---|
| 943 | |
---|
[647] | 944 | printk("\n***** thread[%x,%x] page faults\n" |
---|
[641] | 945 | " - false : %d events / cost %d cycles / max %d cycles\n" |
---|
| 946 | " - local : %d events / cost %d cycles / max %d cycles\n" |
---|
| 947 | " - global : %d events / cost %d cycles / max %d cycles\n", |
---|
| 948 | thread->process->pid, thread->trdid, |
---|
| 949 | false_nr , false_one , false_max, |
---|
| 950 | local_nr , local_one , local_max, |
---|
| 951 | global_nr, global_one, global_max ); |
---|
[640] | 952 | #endif |
---|
| 953 | |
---|
[625] | 954 | // remove thread from process th_tbl[] |
---|
| 955 | count = process_remove_thread( thread ); |
---|
| 956 | |
---|
[635] | 957 | // release memory allocated for CPU context and FPU context |
---|
[1] | 958 | hal_cpu_context_destroy( thread ); |
---|
[625] | 959 | hal_fpu_context_destroy( thread ); |
---|
[1] | 960 | |
---|
[625] | 961 | // release user stack vseg (for an user thread only) |
---|
| 962 | if( type == THREAD_USER ) vmm_remove_vseg( process , thread->user_stack_vseg ); |
---|
| 963 | |
---|
[428] | 964 | // release FPU ownership if required |
---|
[409] | 965 | hal_disable_irq( &save_sr ); |
---|
[1] | 966 | if( core->fpu_owner == thread ) |
---|
| 967 | { |
---|
| 968 | core->fpu_owner = NULL; |
---|
| 969 | hal_fpu_disable(); |
---|
| 970 | } |
---|
[409] | 971 | hal_restore_irq( save_sr ); |
---|
[1] | 972 | |
---|
| 973 | // invalidate thread descriptor |
---|
| 974 | thread->signature = 0; |
---|
| 975 | |
---|
[625] | 976 | // release memory for thread descriptor (including kernel stack) |
---|
| 977 | kmem_req_t req; |
---|
[635] | 978 | req.type = KMEM_PPM; |
---|
| 979 | req.ptr = thread; |
---|
[625] | 980 | kmem_free( &req ); |
---|
| 981 | |
---|
[438] | 982 | #if DEBUG_THREAD_DESTROY |
---|
[433] | 983 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 984 | if( DEBUG_THREAD_DESTROY < cycle ) |
---|
[593] | 985 | printk("\n[%s] thread[%x,%x] exit / destroyed thread[%x,%x] / cycle %d\n", |
---|
[583] | 986 | __FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle ); |
---|
[433] | 987 | #endif |
---|
[1] | 988 | |
---|
[625] | 989 | return count; |
---|
| 990 | |
---|
[407] | 991 | } // end thread_destroy() |
---|
| 992 | |
---|
[416] | 993 | ////////////////////////////////////////////////// |
---|
| 994 | inline void thread_set_req_ack( thread_t * target, |
---|
| 995 | uint32_t * rsp_count ) |
---|
[1] | 996 | { |
---|
[409] | 997 | reg_t save_sr; // for critical section |
---|
| 998 | |
---|
[416] | 999 | // get pointer on target thread scheduler |
---|
| 1000 | scheduler_t * sched = &target->core->scheduler; |
---|
[409] | 1001 | |
---|
[416] | 1002 | // wait scheduler ready to handle a new request |
---|
| 1003 | while( sched->req_ack_pending ) asm volatile( "nop" ); |
---|
[409] | 1004 | |
---|
| 1005 | // enter critical section |
---|
| 1006 | hal_disable_irq( &save_sr ); |
---|
| 1007 | |
---|
[416] | 1008 | // set request in target thread scheduler |
---|
| 1009 | sched->req_ack_pending = true; |
---|
[409] | 1010 | |
---|
[416] | 1011 | // set ack request in target thread "flags" |
---|
| 1012 | hal_atomic_or( &target->flags , THREAD_FLAG_REQ_ACK ); |
---|
[409] | 1013 | |
---|
[416] | 1014 | // set pointer on responses counter in target thread |
---|
| 1015 | target->ack_rsp_count = rsp_count; |
---|
[409] | 1016 | |
---|
| 1017 | // exit critical section |
---|
| 1018 | hal_restore_irq( save_sr ); |
---|
| 1019 | |
---|
[407] | 1020 | hal_fence(); |
---|
[171] | 1021 | |
---|
[416] | 1022 | } // thread_set_req_ack() |
---|
[409] | 1023 | |
---|
[416] | 1024 | ///////////////////////////////////////////////////// |
---|
| 1025 | inline void thread_reset_req_ack( thread_t * target ) |
---|
[1] | 1026 | { |
---|
[409] | 1027 | reg_t save_sr; // for critical section |
---|
| 1028 | |
---|
| 1029 | // get pointer on target thread scheduler |
---|
[416] | 1030 | scheduler_t * sched = &target->core->scheduler; |
---|
[409] | 1031 | |
---|
| 1032 | // check signal pending in scheduler |
---|
[492] | 1033 | assert( sched->req_ack_pending , "no pending signal" ); |
---|
[409] | 1034 | |
---|
| 1035 | // enter critical section |
---|
| 1036 | hal_disable_irq( &save_sr ); |
---|
| 1037 | |
---|
| 1038 | // reset signal in scheduler |
---|
[416] | 1039 | sched->req_ack_pending = false; |
---|
[409] | 1040 | |
---|
| 1041 | // reset signal in thread "flags" |
---|
[416] | 1042 | hal_atomic_and( &target->flags , ~THREAD_FLAG_REQ_ACK ); |
---|
[409] | 1043 | |
---|
| 1044 | // reset pointer on responses counter |
---|
[416] | 1045 | target->ack_rsp_count = NULL; |
---|
[409] | 1046 | |
---|
| 1047 | // exit critical section |
---|
| 1048 | hal_restore_irq( save_sr ); |
---|
| 1049 | |
---|
[407] | 1050 | hal_fence(); |
---|
[171] | 1051 | |
---|
[416] | 1052 | } // thread_reset_req_ack() |
---|
[409] | 1053 | |
---|
[436] | 1054 | ////////////////////////////////////// |
---|
| 1055 | void thread_block( xptr_t thread_xp, |
---|
| 1056 | uint32_t cause ) |
---|
[407] | 1057 | { |
---|
[436] | 1058 | // get thread cluster and local pointer |
---|
| 1059 | cxy_t cxy = GET_CXY( thread_xp ); |
---|
| 1060 | thread_t * ptr = GET_PTR( thread_xp ); |
---|
| 1061 | |
---|
[407] | 1062 | // set blocking cause |
---|
[436] | 1063 | hal_remote_atomic_or( XPTR( cxy , &ptr->blocked ) , cause ); |
---|
[407] | 1064 | hal_fence(); |
---|
| 1065 | |
---|
[438] | 1066 | #if DEBUG_THREAD_BLOCK |
---|
[457] | 1067 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1068 | process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) ); |
---|
[593] | 1069 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 1070 | if( DEBUG_THREAD_BLOCK < cycle ) |
---|
[593] | 1071 | printk("\n[%s] thread[%x,%x] blocked thread %x in process %x / cause %x\n", |
---|
| 1072 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[564] | 1073 | ptr->trdid, hal_remote_l32(XPTR( cxy , &process->pid )), cause ); |
---|
[433] | 1074 | #endif |
---|
| 1075 | |
---|
[407] | 1076 | } // end thread_block() |
---|
| 1077 | |
---|
[433] | 1078 | //////////////////////////////////////////// |
---|
| 1079 | uint32_t thread_unblock( xptr_t thread_xp, |
---|
[407] | 1080 | uint32_t cause ) |
---|
| 1081 | { |
---|
| 1082 | // get thread cluster and local pointer |
---|
[433] | 1083 | cxy_t cxy = GET_CXY( thread_xp ); |
---|
| 1084 | thread_t * ptr = GET_PTR( thread_xp ); |
---|
[407] | 1085 | |
---|
| 1086 | // reset blocking cause |
---|
| 1087 | uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause ); |
---|
| 1088 | hal_fence(); |
---|
| 1089 | |
---|
[438] | 1090 | #if DEBUG_THREAD_BLOCK |
---|
[457] | 1091 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1092 | process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) ); |
---|
[593] | 1093 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 1094 | if( DEBUG_THREAD_BLOCK < cycle ) |
---|
[593] | 1095 | printk("\n[%s] thread[%x,%x] unblocked thread %x in process %x / cause %x\n", |
---|
| 1096 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[564] | 1097 | ptr->trdid, hal_remote_l32(XPTR( cxy , &process->pid )), cause ); |
---|
[433] | 1098 | #endif |
---|
| 1099 | |
---|
[446] | 1100 | // return a non zero value if the cause bit is modified |
---|
| 1101 | return( previous & cause ); |
---|
[436] | 1102 | |
---|
[446] | 1103 | } // end thread_unblock() |
---|
[407] | 1104 | |
---|
[440] | 1105 | ////////////////////////////////////// |
---|
| 1106 | void thread_delete( xptr_t target_xp, |
---|
| 1107 | bool_t is_forced ) |
---|
| 1108 | { |
---|
| 1109 | reg_t save_sr; // for critical section |
---|
| 1110 | bool_t target_join_done; // joining thread arrived first |
---|
| 1111 | bool_t target_attached; // target thread attached |
---|
| 1112 | xptr_t killer_xp; // extended pointer on killer thread (this) |
---|
| 1113 | thread_t * killer_ptr; // pointer on killer thread (this) |
---|
| 1114 | cxy_t target_cxy; // target thread cluster |
---|
| 1115 | thread_t * target_ptr; // pointer on target thread |
---|
[651] | 1116 | process_t * target_process; // pointer on target process |
---|
[625] | 1117 | pid_t target_pid; // target process identifier |
---|
[440] | 1118 | xptr_t target_flags_xp; // extended pointer on target thread <flags> |
---|
| 1119 | xptr_t target_join_lock_xp; // extended pointer on target thread <join_lock> |
---|
| 1120 | xptr_t target_join_xp_xp; // extended pointer on target thread <join_xp> |
---|
| 1121 | trdid_t target_trdid; // target thread identifier |
---|
| 1122 | ltid_t target_ltid; // target thread local index |
---|
[651] | 1123 | uint32_t target_flags; // target thread flags |
---|
[440] | 1124 | xptr_t joining_xp; // extended pointer on joining thread |
---|
[651] | 1125 | thread_t * joining_ptr; // local pointer on joining thread |
---|
| 1126 | cxy_t joining_cxy; // joining thread cluster |
---|
[440] | 1127 | |
---|
[564] | 1128 | // get target thread cluster and local pointer |
---|
[440] | 1129 | target_cxy = GET_CXY( target_xp ); |
---|
| 1130 | target_ptr = GET_PTR( target_xp ); |
---|
[564] | 1131 | |
---|
[651] | 1132 | // get target thread trdid, ltid, flags, and process PID |
---|
[564] | 1133 | target_trdid = hal_remote_l32( XPTR( target_cxy , &target_ptr->trdid ) ); |
---|
[440] | 1134 | target_ltid = LTID_FROM_TRDID( target_trdid ); |
---|
[651] | 1135 | target_flags_xp = XPTR( target_cxy , &target_ptr->flags ); |
---|
| 1136 | target_flags = hal_remote_l32( target_flags_xp ); |
---|
[625] | 1137 | target_process = hal_remote_lpt( XPTR( target_cxy , &target_ptr->process ) ); |
---|
| 1138 | target_pid = hal_remote_l32( XPTR( target_cxy , &target_process->pid ) ); |
---|
[651] | 1139 | target_attached = ((target_flags & THREAD_FLAG_DETACHED) == 0); |
---|
[440] | 1140 | |
---|
| 1141 | // get killer thread pointers |
---|
| 1142 | killer_ptr = CURRENT_THREAD; |
---|
| 1143 | killer_xp = XPTR( local_cxy , killer_ptr ); |
---|
| 1144 | |
---|
| 1145 | #if DEBUG_THREAD_DELETE |
---|
[564] | 1146 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[440] | 1147 | if( DEBUG_THREAD_DELETE < cycle ) |
---|
[651] | 1148 | printk("\n[%s] killer[%x,%x] enters / target[%x,%x] / forced %d / flags %x / cycle %d\n", |
---|
[583] | 1149 | __FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, |
---|
[651] | 1150 | target_pid, target_trdid, is_forced, target_flags, cycle ); |
---|
[440] | 1151 | #endif |
---|
| 1152 | |
---|
[564] | 1153 | // check target thread is not the main thread, because the main thread |
---|
| 1154 | // must be deleted by the parent process sys_wait() function |
---|
[651] | 1155 | assert( ((CXY_FROM_PID( target_pid ) != target_cxy) || (target_ltid != 0)), |
---|
[625] | 1156 | "target thread cannot be the main thread" ); |
---|
[564] | 1157 | |
---|
[583] | 1158 | // check killer thread can yield |
---|
| 1159 | thread_assert_can_yield( killer_ptr , __FUNCTION__ ); |
---|
[440] | 1160 | |
---|
[583] | 1161 | // if the target thread is attached, we must synchonize with the joining thread |
---|
| 1162 | // before blocking and marking the target thead for delete. |
---|
| 1163 | |
---|
| 1164 | if( target_attached && (is_forced == false) ) // synchronize with joining thread |
---|
[564] | 1165 | { |
---|
[440] | 1166 | // build extended pointers on target thread join fields |
---|
| 1167 | target_join_lock_xp = XPTR( target_cxy , &target_ptr->join_lock ); |
---|
| 1168 | target_join_xp_xp = XPTR( target_cxy , &target_ptr->join_xp ); |
---|
| 1169 | |
---|
| 1170 | // enter critical section |
---|
| 1171 | hal_disable_irq( &save_sr ); |
---|
| 1172 | |
---|
| 1173 | // take the join_lock in target thread descriptor |
---|
[564] | 1174 | remote_busylock_acquire( target_join_lock_xp ); |
---|
[440] | 1175 | |
---|
| 1176 | // get join_done from target thread descriptor |
---|
[564] | 1177 | target_join_done = ((hal_remote_l32( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0); |
---|
[440] | 1178 | |
---|
[583] | 1179 | if( target_join_done ) // joining thread arrived first |
---|
[440] | 1180 | { |
---|
| 1181 | // get extended pointer on joining thread |
---|
[564] | 1182 | joining_xp = (xptr_t)hal_remote_l64( target_join_xp_xp ); |
---|
[651] | 1183 | |
---|
| 1184 | // get cluster and local pointer on joining thread |
---|
| 1185 | joining_ptr = GET_PTR( joining_xp ); |
---|
| 1186 | joining_cxy = GET_CXY( joining_xp ); |
---|
| 1187 | |
---|
| 1188 | // copy exit_status from target thread to joining thread, because |
---|
| 1189 | // target thread may be deleted before joining thread resume |
---|
| 1190 | void * status = hal_remote_lpt( XPTR( target_cxy , &target_ptr->exit_status ) ); |
---|
| 1191 | hal_remote_spt( XPTR( joining_cxy , &joining_ptr->exit_status ) , status ); |
---|
[440] | 1192 | |
---|
| 1193 | // reset the join_done flag in target thread |
---|
| 1194 | hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_JOIN_DONE ); |
---|
| 1195 | |
---|
| 1196 | // unblock the joining thread |
---|
| 1197 | thread_unblock( joining_xp , THREAD_BLOCKED_JOIN ); |
---|
| 1198 | |
---|
| 1199 | // release the join_lock in target thread descriptor |
---|
[564] | 1200 | remote_busylock_release( target_join_lock_xp ); |
---|
[440] | 1201 | |
---|
[583] | 1202 | // block the target thread |
---|
| 1203 | thread_block( target_xp , THREAD_BLOCKED_GLOBAL ); |
---|
| 1204 | |
---|
[564] | 1205 | // set the REQ_DELETE flag in target thread descriptor |
---|
| 1206 | hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE ); |
---|
| 1207 | |
---|
[583] | 1208 | // exit critical section |
---|
[440] | 1209 | hal_restore_irq( save_sr ); |
---|
[564] | 1210 | |
---|
[583] | 1211 | #if DEBUG_THREAD_DELETE |
---|
[651] | 1212 | cycle = (uint32_t)hal_get_cycles(); |
---|
[564] | 1213 | if( DEBUG_THREAD_DELETE < cycle ) |
---|
[593] | 1214 | printk("\n[%s] killer[%x,%x] exit / target[%x,%x] marked after join / cycle %d\n", |
---|
[583] | 1215 | __FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, |
---|
[651] | 1216 | target_pid, target_trdid, cycle ); |
---|
[564] | 1217 | #endif |
---|
[583] | 1218 | |
---|
| 1219 | } |
---|
| 1220 | else // killer thread arrived first |
---|
| 1221 | { |
---|
[440] | 1222 | // set the kill_done flag in target thread |
---|
| 1223 | hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE ); |
---|
| 1224 | |
---|
[651] | 1225 | // block target thread on BLOCKED_JOIN |
---|
[440] | 1226 | thread_block( killer_xp , THREAD_BLOCKED_JOIN ); |
---|
| 1227 | |
---|
| 1228 | // set extended pointer on killer thread in target thread |
---|
[564] | 1229 | hal_remote_s64( target_join_xp_xp , killer_xp ); |
---|
[440] | 1230 | |
---|
| 1231 | // release the join_lock in target thread descriptor |
---|
[564] | 1232 | remote_busylock_release( target_join_lock_xp ); |
---|
[440] | 1233 | |
---|
[583] | 1234 | #if DEBUG_THREAD_DELETE |
---|
[651] | 1235 | cycle = (uint32_t)hal_get_cycles(); |
---|
[564] | 1236 | if( DEBUG_THREAD_DELETE < cycle ) |
---|
[593] | 1237 | printk("\n[%s] killer[%x,%x] deschedules / target[%x,%x] not completed / cycle %d\n", |
---|
[583] | 1238 | __FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, |
---|
[651] | 1239 | target_pid, target_trdid, cycle ); |
---|
[564] | 1240 | #endif |
---|
[440] | 1241 | // deschedule |
---|
| 1242 | sched_yield( "killer thread wait joining thread" ); |
---|
| 1243 | |
---|
[583] | 1244 | // block the target thread |
---|
| 1245 | thread_block( target_xp , THREAD_BLOCKED_GLOBAL ); |
---|
| 1246 | |
---|
[564] | 1247 | // set the REQ_DELETE flag in target thread descriptor |
---|
| 1248 | hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE ); |
---|
| 1249 | |
---|
[583] | 1250 | // exit critical section |
---|
[440] | 1251 | hal_restore_irq( save_sr ); |
---|
[583] | 1252 | |
---|
| 1253 | #if DEBUG_THREAD_DELETE |
---|
[651] | 1254 | cycle = (uint32_t)hal_get_cycles(); |
---|
[583] | 1255 | if( DEBUG_THREAD_DELETE < cycle ) |
---|
[593] | 1256 | printk("\n[%s] killer[%x,%x] exit / target[%x,%x] marked after join / cycle %d\n", |
---|
[583] | 1257 | __FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, |
---|
[651] | 1258 | target_pid, target_trdid, cycle ); |
---|
[583] | 1259 | #endif |
---|
| 1260 | |
---|
[440] | 1261 | } |
---|
[564] | 1262 | } |
---|
[583] | 1263 | else // no synchronization with joining thread required |
---|
[564] | 1264 | { |
---|
[583] | 1265 | // block the target thread |
---|
| 1266 | thread_block( target_xp , THREAD_BLOCKED_GLOBAL ); |
---|
| 1267 | |
---|
[564] | 1268 | // set the REQ_DELETE flag in target thread descriptor |
---|
| 1269 | hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE ); |
---|
[440] | 1270 | |
---|
| 1271 | #if DEBUG_THREAD_DELETE |
---|
[651] | 1272 | cycle = (uint32_t)hal_get_cycles(); |
---|
[440] | 1273 | if( DEBUG_THREAD_DELETE < cycle ) |
---|
[593] | 1274 | printk("\n[%s] killer[%x,%x] exit / target [%x,%x] marked / no join / cycle %d\n", |
---|
[583] | 1275 | __FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, |
---|
[651] | 1276 | target_pid, target_trdid, cycle ); |
---|
[440] | 1277 | #endif |
---|
| 1278 | |
---|
[583] | 1279 | } |
---|
[440] | 1280 | } // end thread_delete() |
---|
| 1281 | |
---|
| 1282 | |
---|
| 1283 | |
---|
[564] | 1284 | ///////////////////////////// |
---|
[485] | 1285 | void thread_idle_func( void ) |
---|
[1] | 1286 | { |
---|
[625] | 1287 | |
---|
| 1288 | #if DEBUG_THREAD_IDLE |
---|
| 1289 | uint32_t cycle; |
---|
| 1290 | #endif |
---|
| 1291 | |
---|
[1] | 1292 | while( 1 ) |
---|
| 1293 | { |
---|
[408] | 1294 | // unmask IRQs |
---|
| 1295 | hal_enable_irq( NULL ); |
---|
| 1296 | |
---|
[443] | 1297 | // force core to low-power mode (optional) |
---|
[583] | 1298 | if( CONFIG_SCHED_IDLE_MODE_SLEEP ) |
---|
[407] | 1299 | { |
---|
[1] | 1300 | |
---|
[564] | 1301 | #if DEBUG_THREAD_IDLE |
---|
[625] | 1302 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1303 | if( DEBUG_THREAD_IDLE < cycle ) |
---|
[593] | 1304 | printk("\n[%s] idle thread on core[%x,%d] goes to sleep / cycle %d\n", |
---|
[446] | 1305 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, cycle ); |
---|
[433] | 1306 | #endif |
---|
[1] | 1307 | |
---|
[407] | 1308 | hal_core_sleep(); |
---|
[1] | 1309 | |
---|
[564] | 1310 | #if DEBUG_THREAD_IDLE |
---|
[625] | 1311 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1312 | if( DEBUG_THREAD_IDLE < cycle ) |
---|
[593] | 1313 | printk("\n[%s] idle thread on core[%x,%d] wake up / cycle %d\n", |
---|
[531] | 1314 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, cycle ); |
---|
[433] | 1315 | #endif |
---|
[407] | 1316 | |
---|
| 1317 | } |
---|
[443] | 1318 | |
---|
[446] | 1319 | #if DEBUG_THREAD_IDLE |
---|
[625] | 1320 | cycle = (uint32_t)hal_get_cycles(); |
---|
[564] | 1321 | if( DEBUG_THREAD_IDLE < cycle ) |
---|
[640] | 1322 | sched_remote_display( local_cxy , CURRENT_THREAD->core->lid ); |
---|
[446] | 1323 | #endif |
---|
[564] | 1324 | // search a runable thread |
---|
| 1325 | sched_yield( "running idle thread" ); |
---|
[446] | 1326 | |
---|
[564] | 1327 | } // end while |
---|
| 1328 | |
---|
[407] | 1329 | } // end thread_idle() |
---|
[1] | 1330 | |
---|
[407] | 1331 | |
---|
[473] | 1332 | /////////////////////////////////////////// |
---|
| 1333 | void thread_time_update( thread_t * thread, |
---|
[564] | 1334 | bool_t is_user ) |
---|
[16] | 1335 | { |
---|
[473] | 1336 | cycle_t current_cycle; // current cycle counter value |
---|
| 1337 | cycle_t last_cycle; // last cycle counter value |
---|
[1] | 1338 | |
---|
[473] | 1339 | // get pointer on thread_info structure |
---|
| 1340 | thread_info_t * info = &thread->info; |
---|
| 1341 | |
---|
| 1342 | // get last cycle counter value |
---|
| 1343 | last_cycle = info->last_cycle; |
---|
| 1344 | |
---|
| 1345 | // get current cycle counter value |
---|
| 1346 | current_cycle = hal_get_cycles(); |
---|
| 1347 | |
---|
| 1348 | // update thread_info structure |
---|
| 1349 | info->last_cycle = current_cycle; |
---|
| 1350 | |
---|
| 1351 | // update time in thread_info |
---|
| 1352 | if( is_user ) info->usr_cycles += (current_cycle - last_cycle); |
---|
| 1353 | else info->sys_cycles += (current_cycle - last_cycle); |
---|
[16] | 1354 | |
---|
[564] | 1355 | } // end thread_time_update() |
---|
| 1356 | |
---|
[23] | 1357 | ///////////////////////////////////// |
---|
| 1358 | xptr_t thread_get_xptr( pid_t pid, |
---|
| 1359 | trdid_t trdid ) |
---|
| 1360 | { |
---|
| 1361 | cxy_t target_cxy; // target thread cluster identifier |
---|
| 1362 | ltid_t target_thread_ltid; // target thread local index |
---|
[171] | 1363 | thread_t * target_thread_ptr; // target thread local pointer |
---|
[23] | 1364 | xptr_t target_process_xp; // extended pointer on target process descriptor |
---|
[171] | 1365 | process_t * target_process_ptr; // local pointer on target process descriptor |
---|
[23] | 1366 | pid_t target_process_pid; // target process identifier |
---|
| 1367 | xlist_entry_t root; // root of list of process in target cluster |
---|
| 1368 | xptr_t lock_xp; // extended pointer on lock protecting this list |
---|
[16] | 1369 | |
---|
[580] | 1370 | #if DEBUG_THREAD_GET_XPTR |
---|
| 1371 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1372 | thread_t * this = CURRENT_THREAD; |
---|
| 1373 | if( DEBUG_THREAD_GET_XPTR < cycle ) |
---|
[593] | 1374 | printk("\n[%s] thread %x in process %x enters / pid %x / trdid %x / cycle %d\n", |
---|
[580] | 1375 | __FUNCTION__, this->trdid, this->process->pid, pid, trdid, cycle ); |
---|
| 1376 | #endif |
---|
| 1377 | |
---|
[23] | 1378 | // get target cluster identifier and local thread identifier |
---|
| 1379 | target_cxy = CXY_FROM_TRDID( trdid ); |
---|
| 1380 | target_thread_ltid = LTID_FROM_TRDID( trdid ); |
---|
| 1381 | |
---|
[436] | 1382 | // check trdid argument |
---|
[564] | 1383 | if( (target_thread_ltid >= CONFIG_THREADS_MAX_PER_CLUSTER) || |
---|
[637] | 1384 | cluster_is_active( target_cxy ) == false ) return XPTR_NULL; |
---|
[436] | 1385 | |
---|
[23] | 1386 | // get root of list of process descriptors in target cluster |
---|
| 1387 | hal_remote_memcpy( XPTR( local_cxy , &root ), |
---|
| 1388 | XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ), |
---|
| 1389 | sizeof(xlist_entry_t) ); |
---|
| 1390 | |
---|
[564] | 1391 | // get extended pointer on lock protecting the list of local processes |
---|
[23] | 1392 | lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock ); |
---|
| 1393 | |
---|
| 1394 | // take the lock protecting the list of processes in target cluster |
---|
[564] | 1395 | remote_queuelock_acquire( lock_xp ); |
---|
[23] | 1396 | |
---|
[580] | 1397 | #if( DEBUG_THREAD_GET_XPTR & 1 ) |
---|
| 1398 | if( DEBUG_THREAD_GET_XPTR < cycle ) |
---|
[593] | 1399 | printk("\n[%s] scan processes in cluster %x :\n", __FUNCTION__, target_cxy ); |
---|
[580] | 1400 | #endif |
---|
| 1401 | |
---|
[564] | 1402 | // scan the list of local processes in target cluster |
---|
[23] | 1403 | xptr_t iter; |
---|
| 1404 | bool_t found = false; |
---|
| 1405 | XLIST_FOREACH( XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ) , iter ) |
---|
| 1406 | { |
---|
| 1407 | target_process_xp = XLIST_ELEMENT( iter , process_t , local_list ); |
---|
[469] | 1408 | target_process_ptr = GET_PTR( target_process_xp ); |
---|
[564] | 1409 | target_process_pid = hal_remote_l32( XPTR( target_cxy , &target_process_ptr->pid ) ); |
---|
[580] | 1410 | |
---|
| 1411 | #if( DEBUG_THREAD_GET_XPTR & 1 ) |
---|
| 1412 | if( DEBUG_THREAD_GET_XPTR < cycle ) |
---|
| 1413 | printk(" - process %x\n", target_process_pid ); |
---|
| 1414 | #endif |
---|
| 1415 | |
---|
[23] | 1416 | if( target_process_pid == pid ) |
---|
| 1417 | { |
---|
| 1418 | found = true; |
---|
| 1419 | break; |
---|
| 1420 | } |
---|
| 1421 | } |
---|
| 1422 | |
---|
| 1423 | // release the lock protecting the list of processes in target cluster |
---|
[564] | 1424 | remote_queuelock_release( lock_xp ); |
---|
[23] | 1425 | |
---|
[436] | 1426 | // check PID found |
---|
[580] | 1427 | if( found == false ) |
---|
| 1428 | { |
---|
[23] | 1429 | |
---|
[580] | 1430 | #if( DEBUG_THREAD_GET_XPTR & 1 ) |
---|
| 1431 | if( DEBUG_THREAD_GET_XPTR < cycle ) |
---|
[593] | 1432 | printk("\n[%s] pid %x not found in cluster %x\n", |
---|
[580] | 1433 | __FUNCTION__, pid, target_cxy ); |
---|
| 1434 | #endif |
---|
| 1435 | return XPTR_NULL; |
---|
| 1436 | } |
---|
| 1437 | |
---|
[23] | 1438 | // get target thread local pointer |
---|
| 1439 | xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] ); |
---|
[171] | 1440 | target_thread_ptr = (thread_t *)hal_remote_lpt( xp ); |
---|
[23] | 1441 | |
---|
[580] | 1442 | if( target_thread_ptr == NULL ) |
---|
| 1443 | { |
---|
[23] | 1444 | |
---|
[580] | 1445 | #if( DEBUG_THREAD_GET_XPTR & 1 ) |
---|
| 1446 | if( DEBUG_THREAD_GET_XPTR < cycle ) |
---|
[593] | 1447 | printk("\n[%s] thread %x not registered in process %x in cluster %x\n", |
---|
[580] | 1448 | __FUNCTION__, trdid, pid, target_cxy ); |
---|
| 1449 | #endif |
---|
| 1450 | return XPTR_NULL; |
---|
| 1451 | } |
---|
| 1452 | |
---|
| 1453 | #if DEBUG_THREAD_GET_XPTR |
---|
| 1454 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 1455 | if( DEBUG_THREAD_GET_XPTR < cycle ) |
---|
[593] | 1456 | printk("\n[%s] thread %x in process %x exit / pid %x / trdid %x / cycle %d\n", |
---|
[580] | 1457 | __FUNCTION__, this->trdid, this->process->pid, pid, trdid, cycle ); |
---|
| 1458 | #endif |
---|
| 1459 | |
---|
[23] | 1460 | return XPTR( target_cxy , target_thread_ptr ); |
---|
[564] | 1461 | |
---|
| 1462 | } // end thread_get_xptr() |
---|
| 1463 | |
---|
| 1464 | /////////////////////////////////////////////////// |
---|
| 1465 | void thread_assert_can_yield( thread_t * thread, |
---|
| 1466 | const char * func_str ) |
---|
| 1467 | { |
---|
| 1468 | // does nothing if thread does not hold any busylock |
---|
| 1469 | |
---|
| 1470 | if( thread->busylocks ) |
---|
| 1471 | { |
---|
| 1472 | // get pointers on TXT0 chdev |
---|
| 1473 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
| 1474 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
| 1475 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
| 1476 | |
---|
| 1477 | // get extended pointer on TXT0 lock |
---|
| 1478 | xptr_t txt0_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
| 1479 | |
---|
| 1480 | // get TXT0 lock |
---|
| 1481 | remote_busylock_acquire( txt0_lock_xp ); |
---|
| 1482 | |
---|
| 1483 | // display error message on TXT0 |
---|
[593] | 1484 | nolock_printk("\n[PANIC] in %s / thread[%x,%x] cannot yield : " |
---|
[580] | 1485 | "hold %d busylock(s) / cycle %d\n", |
---|
[593] | 1486 | func_str, thread->process->pid, thread->trdid, |
---|
[624] | 1487 | thread->busylocks - 1, (uint32_t)hal_get_cycles() ); |
---|
[564] | 1488 | |
---|
| 1489 | #if DEBUG_BUSYLOCK |
---|
[580] | 1490 | |
---|
[583] | 1491 | // scan list of busylocks |
---|
| 1492 | xptr_t iter_xp; |
---|
[580] | 1493 | xptr_t root_xp = XPTR( local_cxy , &thread->busylocks_root ); |
---|
| 1494 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
[564] | 1495 | { |
---|
[580] | 1496 | xptr_t lock_xp = XLIST_ELEMENT( iter_xp , busylock_t , xlist ); |
---|
| 1497 | cxy_t lock_cxy = GET_CXY( lock_xp ); |
---|
| 1498 | busylock_t * lock_ptr = GET_PTR( lock_xp ); |
---|
| 1499 | uint32_t lock_type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->type ) ); |
---|
| 1500 | nolock_printk(" - %s in cluster %x\n", lock_type_str[lock_type] , lock_cxy ); |
---|
| 1501 | } |
---|
[564] | 1502 | |
---|
| 1503 | #endif |
---|
[23] | 1504 | |
---|
[564] | 1505 | // release TXT0 lock |
---|
| 1506 | remote_busylock_release( txt0_lock_xp ); |
---|
| 1507 | |
---|
| 1508 | // suicide |
---|
| 1509 | hal_core_sleep(); |
---|
| 1510 | } |
---|
| 1511 | } // end thread_assert_can yield() |
---|
| 1512 | |
---|
[619] | 1513 | ////////////////////////////////////////////////////// |
---|
| 1514 | void thread_display_busylocks( xptr_t thread_xp, |
---|
| 1515 | const char * string ) |
---|
[564] | 1516 | { |
---|
[623] | 1517 | |
---|
[581] | 1518 | cxy_t thread_cxy = GET_CXY( thread_xp ); |
---|
| 1519 | thread_t * thread_ptr = GET_PTR( thread_xp ); |
---|
[564] | 1520 | |
---|
[623] | 1521 | #if DEBUG_BUSYLOCK |
---|
[564] | 1522 | |
---|
[623] | 1523 | xptr_t iter_xp; |
---|
[564] | 1524 | |
---|
[623] | 1525 | // get relevant info from target thread descriptor |
---|
[619] | 1526 | uint32_t locks = hal_remote_l32( XPTR( thread_cxy , &thread_ptr->busylocks ) ); |
---|
| 1527 | trdid_t trdid = hal_remote_l32( XPTR( thread_cxy , &thread_ptr->trdid ) ); |
---|
| 1528 | process_t * process = hal_remote_lpt( XPTR( thread_cxy , &thread_ptr->process ) ); |
---|
| 1529 | pid_t pid = hal_remote_l32( XPTR( thread_cxy , &process->pid ) ); |
---|
[564] | 1530 | |
---|
[581] | 1531 | // get extended pointer on root of busylocks |
---|
[619] | 1532 | xptr_t root_xp = XPTR( thread_cxy , &thread_ptr->busylocks_root ); |
---|
[564] | 1533 | |
---|
[581] | 1534 | // get pointers on TXT0 chdev |
---|
| 1535 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
| 1536 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
| 1537 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
[580] | 1538 | |
---|
[581] | 1539 | // get extended pointer on remote TXT0 lock |
---|
| 1540 | xptr_t txt0_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
[580] | 1541 | |
---|
[581] | 1542 | // get TXT0 lock |
---|
| 1543 | remote_busylock_acquire( txt0_lock_xp ); |
---|
[580] | 1544 | |
---|
[581] | 1545 | // display header |
---|
[619] | 1546 | nolock_printk("\n***** thread[%x,%x] in <%s> : %d busylocks *****\n", |
---|
| 1547 | pid, trdid, string, locks ); |
---|
[581] | 1548 | |
---|
| 1549 | // scan the xlist of busylocks when required |
---|
| 1550 | if( locks ) |
---|
| 1551 | { |
---|
| 1552 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
[580] | 1553 | { |
---|
[581] | 1554 | xptr_t lock_xp = XLIST_ELEMENT( iter_xp , busylock_t , xlist ); |
---|
| 1555 | cxy_t lock_cxy = GET_CXY( lock_xp ); |
---|
| 1556 | busylock_t * lock_ptr = GET_PTR( lock_xp ); |
---|
| 1557 | uint32_t lock_type = hal_remote_l32(XPTR( lock_cxy , &lock_ptr->type )); |
---|
| 1558 | nolock_printk(" - %s in cluster %x\n", lock_type_str[lock_type] , lock_cxy ); |
---|
[580] | 1559 | } |
---|
[581] | 1560 | } |
---|
[580] | 1561 | |
---|
[581] | 1562 | // release TXT0 lock |
---|
| 1563 | remote_busylock_release( txt0_lock_xp ); |
---|
| 1564 | |
---|
[623] | 1565 | #else |
---|
[581] | 1566 | |
---|
[623] | 1567 | printk("\n[ERROR] in %s : set DEBUG_BUSYLOCK in kernel_config.h for %s / thread(%x,%x)\n", |
---|
| 1568 | __FUNCTION__, string, thread_cxy, thread_ptr ); |
---|
| 1569 | |
---|
[581] | 1570 | #endif |
---|
| 1571 | |
---|
[623] | 1572 | return; |
---|
[581] | 1573 | |
---|
[580] | 1574 | } // end thread_display_busylock() |
---|
[581] | 1575 | |
---|