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