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