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