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

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

Fix bugs in exec

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