source: trunk/kernel/kern/thread.h@ 662

Last change on this file since 662 was 662, checked in by alain, 6 years ago

Introduce the ksocket.h & ksocket.c files in kernel/kern.

File size: 27.3 KB
Line 
1/*
2 * thread.h - Thread and related operations definition.
3 *
4 * Author Ghassan Almaless (2008,2009,2010,2011,2012)
5 * Alain Greiner (2016,2017,2018,2019,2020)
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#ifndef _THREAD_H_
26#define _THREAD_H_
27
28#include <hal_kernel_types.h>
29#include <shared_syscalls.h>
30#include <shared_almos.h>
31#include <hal_special.h>
32#include <hal_kentry.h>
33#include <xlist.h>
34#include <list.h>
35#include <hal_context.h>
36#include <remote_busylock.h>
37#include <core.h>
38#include <chdev.h>
39#include <cluster.h>
40#include <process.h>
41#include <dev_ioc.h>
42#include <dev_nic.h>
43#include <dev_txt.h>
44#include <dev_mmc.h>
45#include <dev_dma.h>
46
47/***************************************************************************************
48 * These macros are used to compose or decompose the global thread identifier (TRDID)
49 * to or from cluster identifier / local thread index (CXY , LTID)
50 **************************************************************************************/
51
52#define LTID_FROM_TRDID( trdid ) (ltid_t)(trdid & 0x0000FFFF)
53#define CXY_FROM_TRDID( trdid ) (cxy_t)(trdid >> 16)
54#define TRDID( cxy , ltid ) (trdid_t)((cxy << 16) | ltid )
55
56/***************************************************************************************
57 * This enum defines the thread types.
58 **************************************************************************************/
59
60typedef enum
61{
62 THREAD_USER = 0, /*! user thread (pthread) */
63 THREAD_RPC = 1, /*! kernel thread executing pending RPCs */
64 THREAD_DEV = 2, /*! kernel thread executing I/O device commands */
65 THREAD_IDLE = 3, /*! kernel idle thread */
66}
67thread_type_t;
68
69/***************************************************************************************
70 * This defines the thread flags bit-vector.
71 **************************************************************************************/
72
73#define THREAD_FLAG_DETACHED 0x0001 /*! This thread is detached from parent */
74#define THREAD_FLAG_JOIN_DONE 0x0002 /*! Parent thread made a join request */
75#define THREAD_FLAG_KILL_DONE 0x0004 /*! This thread received a kill request */
76#define THREAD_FLAG_REQ_ACK 0x0010 /*! Acknowledge required from scheduler */
77#define THREAD_FLAG_REQ_DELETE 0x0020 /*! Destruction required from scheduler */
78
79/***************************************************************************************
80 * This defines the thread blocking causes bit-vector.
81 **************************************************************************************/
82
83#define THREAD_BLOCKED_GLOBAL 0x0001 /*! ANY : deactivated / wait activation */
84#define THREAD_BLOCKED_IO 0x0002 /*! USR : wait IO operation completion */
85#define THREAD_BLOCKED_MAPPER 0x0004 /*! ??? : wait mapper */
86#define THREAD_BLOCKED_EXIT 0x0008 /*! USR : blocked in join / wait exit */
87#define THREAD_BLOCKED_JOIN 0x0010 /*! USR : blocked in exit / wait join */
88#define THREAD_BLOCKED_SEM 0x0020 /*! USR : wait semaphore */
89#define THREAD_BLOCKED_PAGE 0x0040 /*! ??? : wait page access */
90#define THREAD_BLOCKED_IDLE 0x0080 /*! RPC : wait RPC_FIFO non empty */
91#define THREAD_BLOCKED_USERSYNC 0x0100 /*! USR : wait cond / mutex / barrier */
92#define THREAD_BLOCKED_RPC 0x0200 /*! ANY : wait RPC completion */
93#define THREAD_BLOCKED_ISR 0x0400 /*! DEV : wait hardware IRQ */
94#define THREAD_BLOCKED_WAIT 0x0800 /*! USR : wait child process termination */
95#define THREAD_BLOCKED_LOCK 0x1000 /*! ANY : wait queuelock or rwlock */
96#define THREAD_BLOCKED_CLIENT 0x2000 /*! DEV : wait clients queue non empty */
97
98/***************************************************************************************
99 * This structure defines a thread descriptor.
100 * It is used for both the user threads and the kernel threads.
101 * In a process, a user thread is identified by a unique TRDID (thread identifier):
102 * - The TRDID 16 LSB bits contain the LTID (Local Thread Index).
103 * - The TRDID 16 MSB bits contain the CXY of cluster containing the thread.
104 * For the main thread the LTID value is always 0, in the owner cluster.
105 * The LTID is used to index the th_tbl[] array in the local process descriptor.
106 * This TRDID is computed by the process_register_thread() function, when the user
107 * thread is registered in the local copy of the process descriptor.
108 *
109 * Implementation notes:
110 *
111 * (1) Don't modify the first 4 fields order, as this order is used by the
112 * hal_kentry assembly code for the TSAR architectures.
113 *
114 * (2) Most of the thread state is private and accessed only by this thread,
115 * but some fields are shared, and can be modified by other threads.
116 * - the "blocked" bit_vector can be modified by another thread
117 * running in another cluster (using atomic instructions),
118 * to change this thread scheduling status.
119 * - the "flags" bit_vector can be modified by another thread
120 * running in another cluster (using atomic instructions),
121 * to register requests such as ACK or DELETE.
122 * - the "join_xp" field can be modified by the joining thread,
123 * and this rendez-vous is protected by the dedicated "join_lock".
124 *
125 * (3) When this thread is blocked on a shared resource (queuelock, condvar,
126 * or chdev), it registers in the associated waiting queue, using the
127 * "wait_list" (local list) or "wait_xlist" (trans-cluster list) fields.
128 *
129 * (4) The thread_info_t structure is defined in the shared_almos.h file in the
130 * /kernel/syscall/shared_include directory.
131 **************************************************************************************/
132
133#define THREAD_SIGNATURE 0xDEADBEEF
134
135typedef struct thread_s
136{
137 void * cpu_context; /*! CPU context used by sched_yield */
138 void * fpu_context; /*! FPU context used by sched_yield */
139 void * uzone_current; /*! used by hal_do_syscall & hal_do_except */
140 void * uzone_previous; /*! used by hal_do_syscall & hal_do_except */
141
142 intptr_t k_stack_base; /*! kernel stack base address */
143 uint32_t k_stack_size; /*! kernel stack size (bytes) */
144
145 uint32_t trdid; /*! thread index (cxy.ltid) */
146 thread_type_t type; /*! thread type */
147 uint32_t quantum; /*! number of clock ticks given to thread */
148 uint32_t ticks_nr; /*! number of ticks used */
149 uint32_t time_last_check; /*! last cpu_time_stamp */
150 core_t * core; /*! pointer to the owner core */
151 process_t * process; /*! pointer on local process descriptor */
152 xptr_t parent; /*! extended pointer on parent thread */
153
154 remote_busylock_t join_lock; /*! lock protecting the join/exit */
155 xptr_t join_xp; /*! joining/killer thread extended pointer */
156 void * exit_status; /*! status returned to joiniy thread */
157
158 uint32_t * ack_rsp_count; /*! pointer on acknowledge response counter */
159
160 vseg_t * user_stack_vseg; /*! local pointer on user stack vseg */
161
162 void * entry_func; /*! pointer on entry function */
163 void * entry_args; /*! pointer on entry function arguments */
164 uint32_t main_argc; /*! main thread number of arguments */
165 char ** main_argv; /*! main thread array of strings arguments */
166
167 uint32_t flags; /*! bit vector of flags */
168 uint32_t blocked; /*! bit vector of blocking causes */
169
170 error_t errno; /*! errno value set by last system call */
171 uint32_t utls; /*! user thread local storage */
172
173 bool_t fork_user; /*! user defined placement for next fork() */
174 cxy_t fork_cxy; /*! target cluster for next fork() */
175
176 list_entry_t sched_list; /*! member of threads attached to same core */
177
178 chdev_t * chdev; /*! chdev pointer (for a DEV thread only) */
179
180 reg_t save_sr; /*! used by sched_yield() function */
181
182 ioc_command_t ioc_cmd; /*! IOC device generic command */
183 txt_command_t txt_cmd; /*! TXT device generic command */
184 nic_command_t nic_cmd; /*! NIC device generic command */
185 mmc_command_t mmc_cmd; /*! MMC device generic command */
186 dma_command_t dma_cmd; /*! DMA device generic command */
187 fbf_command_t fbf_cmd; /*! FBF device generic command */
188
189 xptr_t rpc_client_xp; /*! client thread (for a RPC thread only) */
190
191 list_entry_t wait_list; /*! member of a local waiting queue */
192 xlist_entry_t wait_xlist; /*! member of a trans-cluster waiting queue */
193 xlist_entry_t tmp_xlist; /*! member of a trans-cluster kleenex queue */
194
195 uint32_t busylocks; /*! number of taken busylocks */
196
197#if DEBUG_BUSYLOCK_TYPE
198 xlist_entry_t busylocks_root; /*! root of xlist of taken busylocks */
199#endif
200
201 thread_info_t info; /*! embedded thread_info_t */
202
203 uint32_t signature; /*! for kernel stack overflow detection */
204}
205thread_t;
206
207/***************************************************************************************
208 * This macro returns a pointer on the calling thread from the core hardware register.
209 **************************************************************************************/
210
211#define CURRENT_THREAD (hal_get_current_thread())
212
213/***************************************************************************************
214 * This function returns a printable string for a thread type.
215 ***************************************************************************************
216 * @ type : thread type.
217 * returns pointer on string.
218 **************************************************************************************/
219const char * thread_type_str( thread_type_t type );
220
221/***************************************************************************************
222 * This function is used by the pthread_create() system call to create a "new" thread
223 * in an existing process. It allocates memory for an user thread descriptor in the
224 * local cluster, and initializes it from information contained in the arguments.
225 * The CPU context is initialized from scratch.
226 * It is registered in the local process descriptor specified by the <pid> argument.
227 * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated by the caller
228 * to start at the next scheduling point.
229 ***************************************************************************************
230 * @ pid : process identifier.
231 * @ start_func : pointer on entry function.
232 * @ start_args : pointer on function argument (can be NULL).
233 * @ attr : pointer on pthread attributes descriptor.
234 * @ new_thread : [out] address of buffer for new thread descriptor pointer.
235 * @ returns 0 if success / returns ENOMEM if error.
236 **************************************************************************************/
237error_t thread_user_create( pid_t pid,
238 void * start_func,
239 void * start_arg,
240 pthread_attr_t * attr,
241 thread_t ** new_thread );
242
243/***************************************************************************************
244 * This function is used by the sys_fork() syscall to create the "child" main thread
245 * in the local cluster. It is called, generally through the RPC_PROCESS_MAKE_FORK,
246 * by the process_make_fork() function. It allocates memory from the local cluster
247 * for a "child" thread descriptor, and initializes it from the "parent" thread
248 * descriptor defined by the <parent_thread_xp> argument.
249 * The new thread is attached to the core that has the lowest load in local cluster.
250 * It is registered in the "child" process defined by the <child_process> argument.
251 * This new thread inherits its user stack from the parent thread, as it uses the
252 * Copy-On-Write mechanism to get a private stack when required.
253 * The content of the parent kernel stack is copied into the child kernel stack, as
254 * the Copy-On-Write mechanism cannot be used for kernel segments (because kernel
255 * uses physical addressing on some architectures).
256 * The CPU and FPU execution contexts are created and linked to the new thread.
257 * but the actual context copy is NOT done, and is done by the sys_fork() function.
258 * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated to start.
259 ***************************************************************************************
260 * @ parent_thread_xp : extended pointer on parent thread descriptor.
261 * @ child_process : local pointer on child process descriptor.
262 * @ child_thread : [out] address of buffer for child thread descriptor pointer.
263 * @ returns 0 if success / returns -1 if error.
264 **************************************************************************************/
265error_t thread_user_fork( xptr_t parent_thread_xp,
266 process_t * child_process,
267 thread_t ** child_thread );
268
269/***************************************************************************************
270 * This function is called by the process_make_exec() function to re-initialise the
271 * calling thread descriptor, that will become the new process main thread.
272 * It must be called by the main thread of the calling process.
273 * - The calling thread TRDID is not modified.
274 * - The kernel stack (currently in use) is not modified.
275 * - A new user stack vseg is created and initialised.
276 * - The function calls the hal_cpu_context_exec() to re-initialize the CPU context
277 * and the uzone registered in kernel stack, an jump to user code.
278 ***************************************************************************************
279 * @ entry_func : main thread entry point.
280 * @ argc : number of main thread arguments.
281 * @ argv : array of pointers on stringarguments.
282 * @ returns 0 if success / returns ENOMEM if error.
283 **************************************************************************************/
284error_t thread_user_exec( void * entry_func,
285 uint32_t argc,
286 char ** argv);
287
288/***************************************************************************************
289 * This function allocates memory for a kernel thread descriptor in the local cluster,
290 * and initializes it from arguments values.
291 * It is called by kernel_init() to statically create all DEV server threads
292 * It is also called to dynamically create RPC threads when required.
293 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
294 ***************************************************************************************
295 * @ new_thread : address of buffer for new thread pointer.
296 * @ type : kernel thread type.
297 * @ func : pointer on function.
298 * @ args : function arguments.
299 * @ core_lid : local core index.
300 * @ returns 0 if success / returns ENOMEM if error
301 **************************************************************************************/
302error_t thread_kernel_create( thread_t ** new_thread,
303 thread_type_t type,
304 void * func,
305 void * args,
306 lid_t core_lid );
307
308/***************************************************************************************
309 * This function is called by the kernel_init() function to initialize the IDLE thread
310 * descriptor from arguments values.
311 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
312 * It returns a kernel panic if failure.
313 ***************************************************************************************
314 * @ thread : pointer on existing thread descriptor.
315 * @ type : kernel thread type.
316 * @ func : pointer on function.
317 * @ args : function arguments.
318 * @ core_lid : local core index.
319 **************************************************************************************/
320void thread_idle_init( thread_t * thread,
321 thread_type_t type,
322 void * func,
323 void * args,
324 lid_t core_lid );
325
326/***************************************************************************************
327 * This low-level function is called by the sched_handle_signals() function when a
328 * thread is marked for delete. It removes the thread identified by the <thread>
329 * argument from the process th_tbl[], and releases all physical memory allocated for
330 * this. This includes the thread descriptor itself, the associated CPU and FPU context,
331 * and the physical memory allocated for an user thread stack.
332 * This function does not remove the thread from the scheduler, as this is done by
333 * the scheduler itself.
334 ***************************************************************************************
335 * @ thread : pointer on the thread descriptor to release.
336 * @ return the number of threads registered in the process th_tbl[] before deletion.
337 **************************************************************************************/
338uint32_t thread_destroy( thread_t * thread );
339
340/***************************************************************************************
341 * This function defines the code of the thread executed by all cores after kernel_init,
342 * or when no other thread is runnable for a given core.
343 * It enter and infinite loop in wich:
344 * - it unmask the IRQs
345 * - it optionally calls the hal_core_sleep() function to reduce the power consumption
346 * (this behavior is controlled by the CONFIG_THREAD_IDLE_MODE_SLEEP flag).
347 * - it call the sched_yield() function to find another runnable thread.
348 *
349 * TODO: In the TSAR architecture the hal_core_sleep() function forces the core to
350 * low-power mode. Any IRQ will force the core to exit this low-power mode, but no ISR
351 * is executed. We must analyse if we have the same behaviour for I86 architectures...
352 **************************************************************************************/
353void thread_idle_func( void );
354
355/***************************************************************************************
356 * This function is used by a "blocker" thread running in the same cluster as a "target"
357 * thread to request the scheduler of the target thread to acknowledge that the target
358 * thread is blocked and not running, at the next context switch.
359 * This function executes atomically the following actions :
360 * - it set the request_pending boolean in the target scheduler descriptor.
361 * - it set the REQ_ACK flag in the "flags" field of the target thread descriptor.
362 * - It registers the responses counter pointer in the target thread descriptor.
363 * The request_pending flag is handled as a set/reset flip-flop by the "blocker" thread
364 * and by the "target" scheduler.
365 ***************************************************************************************
366 * @ target : local pointer on target thread.
367 * @ ack_rsp_count : local pointer on responses counter.
368 **************************************************************************************/
369void thread_set_req_ack( thread_t * target,
370 uint32_t * ack_rsp_count );
371
372/***************************************************************************************
373 * This function is used by the sched_handle_signal() function executed by the
374 * scheduler of a "target" thread to reset a "blocked not running" acknowledge request
375 * in both the target thread descriptor, and in the target thread scheduler.
376 ***************************************************************************************
377 * @ target : local pointer on target thread.
378 **************************************************************************************/
379void thread_reset_req_ack( thread_t * target );
380
381/***************************************************************************************
382 * This function is used by the four sys_thread_cancel(), sys_thread_exit(),
383 * sys_kill() and sys_exit() system calls to mark for delete a given thread.
384 * It set the THREAD_BLOCKED_GLOBAL bit and set the THREAD_FLAG_REQ_DELETE bit in the
385 * thread descriptor identified by the <thread_xp> argument, to ask the scheduler
386 * to asynchronously delete the target thread, at the next scheduling point.
387 * The calling thread can run in any cluster, as it uses remote accesses.
388 * This function makes a kernel panic if the target thread is the main thread,
389 * because the main thread deletion will cause the process deletion, and a process
390 * must be deleted by the parent process, running the wait function.
391 * If the target thread is running in "attached" mode, and the <is_forced> argument
392 * is false, this function implements the required sychronisation with the joining
393 * thread, blocking the killer thread until the pthread_join() syscall is executed
394 * by the joining thread.
395 ***************************************************************************************
396 * @ thread_xp : extended pointer on the target thread.
397 * @ is_forced : the deletion does not depends on the attached mode.
398 **************************************************************************************/
399void thread_delete( xptr_t thread_xp,
400 bool_t is_forced );
401
402/***************************************************************************************
403 * This function registers a blocking cause defined by the <cause> argument
404 * in a remote thread descriptor identified by the <thread_xp> argument.
405 * We need an extended pointer, because this function can be called by another thread
406 * than the target thread, executing the sys_kill() function.
407 * WARNING : this function does not deschedule the target thread, and the descheduling
408 * must be explicitely forced by a sched_yield().
409 ***************************************************************************************
410 * @ thread_xp : extended pointer on remote thread descriptor.
411 * @ cause : mask defining the cause (one hot).
412 **************************************************************************************/
413void thread_block( xptr_t thread_xp,
414 uint32_t cause );
415
416/***************************************************************************************
417 * This function resets the bit identified by the <cause> argument in a remote
418 * thread descriptor identified by the <thread_xp> argument.
419 * We need an extended pointer, because the client thread of an I/O operation on a
420 * given device is generally not in the same cluster as the associated server thread.
421 * WARNING : this function does not reschedule the remote thread.
422 * The scheduling can be forced by sending an IPI to the core running the remote thread.
423 ***************************************************************************************
424 * @ thread_xp : extended pointer the remote thread.
425 * @ cause : mask defining the cause (one hot).
426 * @ return non zero if the bit-vector was actually modified / return 0 otherwise
427 **************************************************************************************/
428uint32_t thread_unblock( xptr_t thread_xp,
429 uint32_t cause );
430
431/***************************************************************************************
432 * This function updates the calling thread user_time or kernel_time counters.
433 ***************************************************************************************
434 * @ thread : local pointer on target thread.
435 * @ is_user : update user time if true / update kernel time if false
436 **************************************************************************************/
437void thread_time_update( thread_t * thread,
438 bool_t is_user );
439
440/***************************************************************************************
441 * This function returns the extended pointer on a thread descriptor identified
442 * by its thread identifier, and process identifier.
443 * It can be called by any thread running in any cluster.
444 ***************************************************************************************
445 * @ pid : process identifier.
446 * @ trdid : thread identifier.
447 * @ return the extended pointer if thread found / return XPTR_NULL if not found.
448 **************************************************************************************/
449xptr_t thread_get_xptr( pid_t pid,
450 trdid_t trdid );
451
452/***************************************************************************************
453 * This function checks that the thread identified by the <thread> argument does hold
454 * any busylock (local or remote).
455 * If the xlist of taken busylocks is not empty, it displays the set of taken locks,
456 * and makes a kernel panic.
457 ***************************************************************************************
458 * @ thread : local pointer on target thread.
459 * @ func_str : faulty function name.
460 **************************************************************************************/
461void thread_assert_can_yield( thread_t * thread,
462 const char * func_str );
463
464/***************************************************************************************
465 * This debug function display the list of busylocks (local or remote)
466 * currently owned by a the thread identified by the <thead_xp> argument.
467 * The <string> argument is printed in header (can be the calling function name).
468 * WARNING : the DEBUG_BUSYLOCK parameter must be set in the kernel_config.h file.
469 ***************************************************************************************
470 * @ thread_xp : extended pointer on target thread.
471 * @ string : defines the calling context.
472 **************************************************************************************/
473void thread_display_busylocks( xptr_t thread_xp,
474 const char * string );
475
476
477
478#endif /* _THREAD_H_ */
Note: See TracBrowser for help on using the repository browser.