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

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

Remove the "giant" rwlock protecting the GPT, and
use the GPT_LOCKED attribute in each PTE to prevent
concurrent modifications of one GPT entry.
The version number has been incremented to 2.1.

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