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