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