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