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