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