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

Last change on this file since 421 was 418, checked in by alain, 7 years ago

Fix a bug in hal_kentry.S : the "uzone" pointer in the thread descriptor
must not be modified in case of interrupt.

File size: 26.9 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)
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_types.h>
29#include <shared_syscalls.h>
30#include <hal_special.h>
31#include <xlist.h>
32#include <list.h>
33#include <hal_context.h>
34#include <spinlock.h>
35#include <core.h>
36#include <chdev.h>
37#include <cluster.h>
38#include <process.h>
39#include <dev_ioc.h>
40#include <dev_nic.h>
41#include <dev_txt.h>
42#include <dev_mmc.h>
43#include <dev_dma.h>
44
45/***************************************************************************************
46 * These macros are used to compose or decompose the global thread identifier (TRDID)
47 * to or from cluster identifier / local thread index (CXY , LTID)
48 **************************************************************************************/
49
50#define LTID_FROM_TRDID( trdid )   (ltid_t)(trdid & 0x0000FFFF)
51#define CXY_FROM_TRDID( trdid )    (cxy_t)(trdid >> 16)
52#define TRDID( cxy , ltid )        (trdid_t)((cxy << 16) | ltid )
53
54/***************************************************************************************
55 * This enum defines the thread types.
56 **************************************************************************************/
57
58typedef enum
59{
60        THREAD_USER    = 0,          /*! user thread (pthread)                            */
61        THREAD_RPC     = 1,          /*! kernel thread executing pending RPCs             */
62        THREAD_DEV     = 2,          /*! kernel thread executing I/O device commands      */
63        THREAD_IDLE    = 3,          /*! kernel idle thread                               */
64}
65thread_type_t;
66
67/***************************************************************************************
68 * This defines the thread flags bit-vector.
69 **************************************************************************************/
70
71#define THREAD_FLAG_DETACHED     0x0001  /*! This thread is detached from parent      */
72#define THREAD_FLAG_JOIN_DONE    0x0002  /*! Parent thread made a join                */
73#define THREAD_FLAG_SCHED        0x0004  /*! Scheduling required for this thread      */
74#define THREAD_FLAG_REQ_ACK      0x0008  /*! Acknowledge required from scheduler      */
75#define THREAD_FLAG_REQ_DELETE   0x0010  /*! Destruction required by scheduler        */
76
77/***************************************************************************************
78 * This defines the masks associated to the blocking causes.
79 **************************************************************************************/
80
81#define THREAD_BLOCKED_GLOBAL    0x0001  /*! thread deactivated / wait activation     */
82#define THREAD_BLOCKED_IO        0x0002  /*! thread wait IO operation completion      */
83#define THREAD_BLOCKED_MAPPER    0x0004  /*! thread wait mapper                       */
84#define THREAD_BLOCKED_EXIT      0x0008  /*! thread blocked in join / wait exit       */
85#define THREAD_BLOCKED_JOIN      0x0010  /*! thread blocked in exit / wait join       */
86#define THREAD_BLOCKED_SEM       0x0020  /*! thread wait semaphore                    */
87#define THREAD_BLOCKED_PAGE      0x0040  /*! thread wait page access                  */
88#define THREAD_BLOCKED_USERSYNC  0x0100  /*! thread wait (cond/mutex/barrier)         */
89#define THREAD_BLOCKED_RPC       0x0200  /*! thread wait RPC completion               */
90#define THREAD_BLOCKED_DEV_ISR   0x0400  /*! thread DEV wait ISR                      */
91
92/***************************************************************************************
93 * This structure defines thread instrumentation informations.
94 **************************************************************************************/
95
96typedef struct thread_info_s
97{
98        uint32_t              pgfault_nr;    /*! cumulated number of page fault           */
99        uint32_t              sched_nr;      /*! TODO ???  [AG]                           */
100        uint32_t              u_err_nr;      /*! TODO ???  [AG]                           */
101        uint32_t              m_err_nr;      /*! TODO ???  [AG]                           */
102        uint32_t              tm_tmp;        /*! temp date to compute execution duration  */
103        uint32_t              tm_exec;       /*! TODO ???  [AG]                           */
104        uint32_t              tm_create;     /*! date of the creation                     */
105        uint32_t              tm_born;       /*! date of the thread loading               */
106        uint32_t              tm_dead;       /*! date of the death                        */
107        cycle_t               tm_sleep;      /*! TODO ???  [AG]                           */
108        cycle_t               tm_wait;       /*! TODO ???  [AG]                           */
109        cycle_t               tm_usr;        /*! user execution duration                  */
110        cycle_t               tm_sys;        /*! system execution duration                */
111}
112thread_info_t;
113
114/***************************************************************************************
115 * This structure defines a thread descriptor.
116 * It is used for both the user threads and the kernel threads.
117 * In a process, a user thread is identified by a unique TRDID (thread identifier),
118 * that is returned by the kernel to the user:
119 * - The TRDID 16 LSB bits contain the LTID (Local Thread Index).
120 * - The TRDID 16 MSB bits contain the CXY of cluster containing the thread.
121 * - The LTID is used to index the th_tbl[] array in the local process descriptor.
122 _* This TRDID is computed by the process_register_thread() function, when the user
123 * thread is registered in the local copy of the process descriptor.
124 *
125 * WARNING : Don't modify the first 3 fields order, as this order is used by the
126 * hal_kentry assembly code for the TSAR architecture.
127 **************************************************************************************/
128
129#define THREAD_SIGNATURE    0xDEADBEEF
130
131typedef struct thread_s
132{
133        void              * cpu_context;     /*! pointer on CPU context switch            */
134        void              * fpu_context;     /*! pointer on FPU context switch            */
135    void              * uzone;           /*! used by hal_do_syscakl & hal_do_except   */
136
137        intptr_t            k_stack_base;    /*! kernel stack base address                */
138        uint32_t            k_stack_size;    /*! kernel stack size (bytes)                */
139
140        uint32_t            trdid;           /*! thread index (cxy.ltid)                  */
141        thread_type_t       type;            /*! thread type                              */
142        uint32_t            quantum;         /*! number of clock ticks given to thread    */
143        uint32_t            ticks_nr;        /*! number of ticks used                     */
144        uint32_t            time_last_check; /*! last cpu_time_stamp                      */
145        core_t            * core;            /*! pointer to the owner core                */
146        process_t         * process;         /*! pointer on local process descriptor      */
147    xptr_t              parent;          /*! extended pointer on parent thread        */
148
149        uint32_t            local_locks;         /*! number of local locks owned by thread    */
150        uint32_t            remote_locks;        /*! number of remote locks owned by thread   */
151
152    remote_spinlock_t * join_lock;       /*! lock protecting the join/exit            */
153    void              * join_value;      /*! exit_value used in case of join          */
154    xptr_t              join_xp;         /*! extended pointer on joining thread       */
155
156    uint32_t          * ack_rsp_count;   /*! pointer on acknowledge response counter  */
157
158        intptr_t            u_stack_base;    /*! user stack base address                  */
159        uint32_t            u_stack_size;    /*! user stack size (bytes)                  */
160
161    void              * entry_func;      /*! pointer on entry function                */
162    void              * entry_args;      /*! pointer on entry function arguments      */
163
164    uint32_t            flags;           /*! bit vector of flags                      */
165    uint32_t            blocked;         /*! bit vector of blocking causes            */
166
167        error_t             errno;           /*! errno value set by last system call      */
168    uint32_t            utls;            /*! user thread local storage                */
169
170    bool_t              fork_user;       /*! user defined placement for next fork()   */
171    cxy_t               fork_cxy;        /*! target cluster  for next fork()          */
172
173        xlist_entry_t       children_root;   /*! root of list of attached children        */
174    uint32_t            children_nr;     /*! number of attached children threads      */
175    remote_spinlock_t * children_lock;   /*! lock protecting the children list        */
176
177    xlist_entry_t       brothers_list;   /*! list of attached threads to same parent  */
178
179        list_entry_t        sched_list;      /*! member of threads attached to same core  */
180
181    chdev_t           * chdev;           /*! chdev pointer (for a DEV thread only)    */
182
183    reg_t               save_sr;         /*! used by sched_yield() function           */
184
185    ioc_command_t       ioc_cmd;         /*! IOC device generic command               */
186    txt_command_t       txt_cmd;         /*! TXT device generic command               */
187    nic_command_t       nic_cmd;         /*! NIC device generic command               */
188    mmc_command_t       mmc_cmd;         /*! MMC device generic command               */
189    dma_command_t       dma_cmd;         /*! DMA device generic command               */
190
191        cxy_t               rpc_client_cxy;  /*! client cluster index (for a RPC thread)  */
192
193    xlist_entry_t       wait_list;       /*! member of threads blocked on same cond   */
194
195#if CONFIG_LOCKS_DEBUG
196    list_entry_t        locks_root;      /*! root of list of locks taken              */
197    xlist_entry_t       xlocks_root;     /*! root of xlist of remote locks taken      */
198#endif
199
200        thread_info_t       info;            /*! embedded thread_info_t                   */
201
202        uint32_t            signature;       /*! for kernel stack overflow detection      */
203}
204thread_t;
205
206/***************************************************************************************
207 * This macro returns a pointer on the calling thread from the core hardware register.
208 **************************************************************************************/
209
210#define CURRENT_THREAD  (hal_get_current_thread())
211
212/***************************************************************************************
213 * This function returns a printable string for a thread type.
214 ***************************************************************************************
215 * @ type    : thread type.
216 * returns pointer on string.
217 **************************************************************************************/
218char * thread_type_str( uint32_t type );
219
220/***************************************************************************************
221 * This function is used by the pthread_create() system call to create a "new" thread
222 * in an existing process. It allocates memory for an user thread descriptor in the
223 * local cluster, and initializes it from information contained in the arguments.
224 * The CPU context is initialized from scratch. If required by the <attr> argument,
225 * the new thread is attached to the core specified in <attr>.
226 * It is registered in the local process descriptor specified by the <pid> argument.
227 * The thread descriptor pointer is returned to allow the parent thread to register it
228 * in its children list.
229 * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated to start.
230 ***************************************************************************************
231 * @ pid          : process identifier.
232 * @ start_func   : pointer on entry function.
233 * @ start_args   : pointer on function argument (can be NULL).
234 * @ attr         : pointer on pthread attributes descriptor.
235 * @ new_thread   : [out] address of buffer for new thread descriptor pointer.
236 * @ returns 0 if success / returns ENOMEM if error.
237 **************************************************************************************/
238error_t thread_user_create( pid_t             pid,
239                            void            * start_func,
240                            void            * start_arg,
241                            pthread_attr_t  * attr,
242                            thread_t       ** new_thread );
243
244/***************************************************************************************
245 * This function is used by the sys_fork() system call to create the "child" thread
246 * in the local cluster. It allocates memory for a thread descriptor, and initializes
247 * it from the "parent" thread descriptor defined by the <parent_thread_xp> argument.
248 * The new thread is attached to the core that has the lowest load in local cluster.
249 * It is registered in the "child" process defined by the <child_process> argument.
250 * This new thread inherits its user stack from the parent thread, as it uses the
251 * Copy-On-Write mechanism to get a private stack when required.
252 * The content of the parent kernel stack is copied into the child kernel stack, as
253 * the Copy-On-Write mechanism cannot be used for kernel segments (because kernel
254 * uses physical addressing on some architectures).
255 * The CPU and FPU execution contexts are created and linked to the new thread.
256 * but the actual context copy is NOT done, and must be done by by the sys_fork().
257 * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated to start.
258 ***************************************************************************************
259 * @ parent_thread_xp  : extended pointer on parent thread descriptor.
260 * @ child_process     : local pointer on child process descriptor.
261 * @ child_thread      : [out] address of buffer for child thread descriptor pointer.
262 * @ returns 0 if success / returns -1 if error.
263 **************************************************************************************/
264error_t thread_user_fork( xptr_t      parent_thread_xp,
265                          process_t * child_process,
266                          thread_t ** child_thread );
267
268/***************************************************************************************
269 * This function allocates memory for a kernel thread descriptor in the local cluster,
270 * and initializes it from arguments values.
271 * It is called by kernel_init() to statically create all DEV server threads
272 * It is also called to dynamically create RPC threads when required.
273 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
274 ***************************************************************************************
275 * @ new_thread   : address of buffer for new thread pointer.
276 * @ type         : kernel thread type.
277 * @ func         : pointer on function.
278 * @ args         : function arguments.
279 * @ core_lid     : local core index.
280 * @ returns 0 if success / returns ENOMEM if error
281 **************************************************************************************/
282error_t thread_kernel_create( thread_t     ** new_thread,
283                              thread_type_t   type,
284                              void          * func,
285                              void          * args,
286                              lid_t           core_lid );
287
288/***************************************************************************************
289 * This function initializes an existing thread descriptor from arguments values.
290 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
291 * It is called by the kernel_init() function to initialize the IDLE thread.
292 ***************************************************************************************
293 * @ thread   : pointer on existing thread descriptor.
294 * @ type     : kernel thread type.
295 * @ func     : pointer on function.
296 * @ args     : function arguments.
297 * @ core_lid : local core index.
298 * @ returns 0 if success / returns EINVAL if error
299 **************************************************************************************/
300error_t thread_kernel_init( thread_t      * thread,
301                            thread_type_t   type,
302                            void          * func,
303                            void          * args,
304                            lid_t           core_lid );
305
306/***************************************************************************************
307 * This function releases the physical memory allocated for a thread in a given cluster.
308 * This include the thread descriptor itself, the associated CPU and FPU context, and
309 * the physical memory allocated for an user thread local stack.
310 ***************************************************************************************
311 * @ thread  : pointer on the thread descriptor to release.
312 **************************************************************************************/
313void thread_destroy( thread_t * thread );
314
315/***************************************************************************************
316 * This function defines the code of the thread executed by all cores after kernel_init,
317 * or when no other thread is runnable for a given core.
318 *
319 * TODO: In the TSAR architecture, it enters an infinite loop, in wich it forces
320 * the core in sleep (low-power) mode. Any IRQ will force the core to exit this sleep
321 * mode, but no ISR is executed.
322 * TODO: We must analyse if we have the same behaviour for I86 architectures...
323 **************************************************************************************/
324void thread_idle_func();
325
326/***************************************************************************************
327 * This function registers a child thread in the global list of attached
328 * children threads of a parent thread.
329 * It does NOT take a lock, as this function is always called by the parent thread.
330 ***************************************************************************************
331 * @ parent_xp : extended pointer on the parent thread descriptor.
332 * @ child_xp  : extended pointer on the child thread descriptor.
333 **************************************************************************************/
334void thread_child_parent_link( xptr_t  parent_xp,
335                               xptr_t  child_xp );
336
337/***************************************************************************************
338 * This function removes an user thread from the parent thread global list
339 * of attached children threads.
340 ***************************************************************************************
341 * @ parent_xp : extended pointer on the parent thread descriptor.
342 * @ child_xp  : extended pointer on the child thread descriptor.
343 **************************************************************************************/
344void thread_child_parent_unlink( xptr_t parent_xp,
345                                 xptr_t child_xp );
346
347/***************************************************************************************
348 * This function is used by a "blocker" thread running in the same cluster as a "target"
349 * thread to request the scheduler of the target thread to acknowledge that the target
350 * thread is blocked and not running, at the next context switch.
351 * This function executes atomically the following actions :
352 * - it set the request_pending boolean in the target scheduler descriptor.
353 * - it set the REQ_ACK flag in the "flags" field of the target thread descriptor.
354 * - It registers the responses counter pointer in the target thread descriptor.
355 * The request_pending flag is handled as a set/reset flip-flop by the "blocker" thread
356 * and by the "target" scheduler.
357 ***************************************************************************************
358 * @ target        : local pointer on target thread.
359 * @ ack_rsp_count : local pointer on responses counter.
360 **************************************************************************************/
361void thread_set_req_ack( thread_t * target,
362                         uint32_t * ack_rsp_count );
363
364/***************************************************************************************
365 * This function is used by the sched_handle_signal() function executed by the
366 * scheduler of a "target" thread to reset a "blocked not running" acknowledge request
367 * in both the target thread descriptor, and in the target  thread scheduler.
368 ***************************************************************************************
369 * @ target    : local pointer on target thread.
370 **************************************************************************************/
371void thread_reset_req_ack( thread_t * target );
372
373/***************************************************************************************
374 * This function checks if the calling thread can deschedule.
375 ***************************************************************************************
376 * @ returns true if no locks taken.
377 **************************************************************************************/
378inline bool_t thread_can_yield();
379
380/***************************************************************************************
381 * This function implements the delayed descheduling mechanism : It is called  by
382 * all lock release functions, and calls the sched_yield() function when all locks
383 * have beeen released and the calling thread THREAD_FLAG_SCHED flag is set.
384 **************************************************************************************/
385void thread_check_sched();
386
387/***************************************************************************************
388 * This function is called to handle the "pthread_cancel" system call.
389 * It allows a killer thread to kill one single target thread.
390 * The killer thread must be running in the same cluster as the target thread.
391 * If not, the client thread must use the RPC_THREAD_KILL.
392 * - When the killer thread is running on the same core as the target thread,
393 *   this function simply set the BLOCKED_ GLOBAL bit and the REQ_DELETE flag
394 *   in the target thread descriptor and return.
395 * - When the killer thread is running on a different core than the target thread,
396 *   the killer set the BLOCKED_GLOBAL bit and the REQ_ACK flag in target  thread,
397 *   to ask the scheduler to confirm that the target is blocked and not running.
398 *   Then, it set the REQ_DELETE flag in the target thread and return.
399 * In both cases, the actual target thread destruction is done by the scheduler
400 * at the next scheduling point.
401 ***************************************************************************************
402 * @ thread   : local pointer on the target thread.
403 **************************************************************************************/
404void thread_kill( thread_t * thread );
405
406/***************************************************************************************
407 * This function registers a blocking cause in the target thread "blocked" bit vector.
408 * Warning : this function does not deschedule the calling thread, and the descheduling
409 * must be explicitely forced by a sched_yield().
410 ***************************************************************************************
411 * @ thread   : local pointer on target thread descriptor.
412 * @ cause    : mask defining the cause (one hot).
413 **************************************************************************************/
414void thread_block( thread_t * thread,
415                   uint32_t   cause );
416
417/***************************************************************************************
418 * This function resets the bit identified by the cause argument in the "blocked"
419 * bit vector of a remote thread descriptor, using an atomic access.
420 * We need an extended pointer, because the client thread of an I/O operation on a
421 * given device is not in the same cluster as the associated device descriptor.
422 * Warning : this function does not reschedule the remote thread.
423 * The scheduling can be forced by sending an IPI to the core running the remote thread.
424 ***************************************************************************************
425 * @ thread   : extended pointer on the remote thread.
426 * @ cause    : mask defining the cause (one hot).
427 * @ return non zero if the bit-vector was actually modified / return 0 otherwise
428 **************************************************************************************/
429uint32_t thread_unblock( xptr_t   thread,
430                         uint32_t cause );
431
432/***************************************************************************************
433 * This function updates the calling thread user_time counter, and resets the thread
434 * cycles counter.
435 * TODO This function is not implemented.
436 ***************************************************************************************
437 * @ thread   : local pointer on target thread.
438 **************************************************************************************/
439void thread_user_time_update( thread_t * thread );
440
441/**************************************************************************************n
442 * This function updates the calling thread kernel_time counter, and resets the thread
443 * cycles counter.
444 * TODO This function is not implemented.
445 ***************************************************************************************
446 * @ thread   : local pointer on target thread.
447 **************************************************************************************/
448void thread_kernel_time_update( thread_t * thread );
449
450/***************************************************************************************
451 * This function handles all pending signals for the thread identified by the <thread>
452 * argument. It is called each time the core exits the kernel, after handling an
453 * interrupt, exception or syscall.
454 * TODO This function is not implemented.
455 ***************************************************************************************
456 * @ thread   : local pointer on target thread.
457 **************************************************************************************/
458void thread_signals_handle( thread_t * thread );
459
460/***************************************************************************************
461 * This function returns the extended pointer on a thread descriptor identified
462 * by its thread identifier, and process identifier.
463 * It can be called by any thread running in any cluster.
464 ***************************************************************************************
465 * @ pid     : process identifier.
466 * @ trdid   : thread identifier.
467 * @ return the extended pointer if thread found / return XPTR_NULL if not found.
468 **************************************************************************************/
469xptr_t thread_get_xptr( pid_t    pid,
470                        trdid_t  trdid );
471
472
473#endif  /* _THREAD_H_ */
Note: See TracBrowser for help on using the repository browser.