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