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

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

Complete restructuration of kernel locks.

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