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

Last change on this file since 637 was 635, checked in by alain, 5 years ago

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

File size: 28.0 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     local_pgfault_nr;       /*! number of local page fault               */
104        uint32_t     global_pgfault_nr;      /*! number of global page fault              */
105    uint32_t     false_pgfault_cost;     /*! cumulated cost                           */
106    uint32_t     local_pgfault_cost;     /*! cumulated cost                           */
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 * This function does not remove the thread from the scheduler, as this is done by
342 * the scheduler itself.
343 ***************************************************************************************
344 * @ thread  : pointer on the thread descriptor to release.
345 * @ return the number of threads registered in the process th_tbl[] before deletion.
346 **************************************************************************************/
347uint32_t thread_destroy( thread_t * thread );
348
349/***************************************************************************************
350 * This function defines the code of the thread executed by all cores after kernel_init,
351 * or when no other thread is runnable for a given core.
352 * It enter and infinite loop in wich:
353 * - it unmask the IRQs
354 * - it optionally calls the hal_core_sleep() function to reduce the power consumption
355 *   (this behavior is controlled by the CONFIG_THREAD_IDLE_MODE_SLEEP flag).
356 * - it call the sched_yield() function to find another runnable thread.
357 *
358 * TODO: In the TSAR architecture the hal_core_sleep() function forces the core to
359 * low-power mode. Any IRQ will force the core to exit this low-power mode, but no ISR
360 * is executed. We must analyse if we have the same behaviour for I86 architectures...
361 **************************************************************************************/
362void thread_idle_func( void );
363
364/***************************************************************************************
365 * This function is used by a "blocker" thread running in the same cluster as a "target"
366 * thread to request the scheduler of the target thread to acknowledge that the target
367 * thread is blocked and not running, at the next context switch.
368 * This function executes atomically the following actions :
369 * - it set the request_pending boolean in the target scheduler descriptor.
370 * - it set the REQ_ACK flag in the "flags" field of the target thread descriptor.
371 * - It registers the responses counter pointer in the target thread descriptor.
372 * The request_pending flag is handled as a set/reset flip-flop by the "blocker" thread
373 * and by the "target" scheduler.
374 ***************************************************************************************
375 * @ target        : local pointer on target thread.
376 * @ ack_rsp_count : local pointer on responses counter.
377 **************************************************************************************/
378void thread_set_req_ack( thread_t * target,
379                         uint32_t * ack_rsp_count );
380
381/***************************************************************************************
382 * This function is used by the sched_handle_signal() function executed by the
383 * scheduler of a "target" thread to reset a "blocked not running" acknowledge request
384 * in both the target thread descriptor, and in the target  thread scheduler.
385 ***************************************************************************************
386 * @ target    : local pointer on target thread.
387 **************************************************************************************/
388void thread_reset_req_ack( thread_t * target );
389
390/***************************************************************************************
391 * This function is used by the four sys_thread_cancel(), sys_thread_exit(),
392 * sys_kill() and sys_exit() system calls to mark for delete a given thread.
393 * It set the THREAD_BLOCKED_GLOBAL bit and set the THREAD_FLAG_REQ_DELETE bit in the
394 * thread descriptor identified by the <thread_xp> argument, to ask the scheduler
395 * to asynchronously delete the target thread, at the next scheduling point.
396 * The calling thread can run in any cluster, as it uses remote accesses.
397 * This function makes a kernel panic if the target thread is the main thread,
398 * because the main thread deletion will cause the process deletion, and a process
399 * must be deleted by the parent process, running the wait function.
400 * If the target thread is running in "attached" mode, and the <is_forced> argument
401 * is false, this function implements the required sychronisation with the joining
402 * thread, blocking the killer thread until the pthread_join() syscall is executed
403 * by the joining thread.
404 ***************************************************************************************
405 * @ thread_xp   : extended pointer on the target thread.
406 * @ pid         : process identifier (to get the owner cluster identifier).
407 * @ is_forced   : the deletion does not depends on the attached mode.
408 **************************************************************************************/
409void thread_delete( xptr_t  thread_xp,
410                    pid_t   pid,
411                    bool_t  is_forced );
412
413/***************************************************************************************
414 * This function registers a blocking cause defined by the <cause> argument
415 * in a remote thread descriptor identified by the <thread_xp> argument.
416 * We need an extended pointer, because this function can be called by another thread
417 * than the target thread, executing the sys_kill() function.
418 * WARNING : this function does not deschedule the target thread, and the descheduling
419 * must be explicitely forced by a sched_yield().
420 ***************************************************************************************
421 * @ thread_xp   : extended pointer on remote thread descriptor.
422 * @ cause       : mask defining the cause (one hot).
423 **************************************************************************************/
424void thread_block( xptr_t   thread_xp,
425                   uint32_t cause );
426
427/***************************************************************************************
428 * This function resets the bit identified by the <cause> argument in a remote
429 * thread descriptor identified by the <thread_xp> argument.
430 * We need an extended pointer, because the client thread of an I/O operation on a
431 * given device is generally not in the same cluster as the associated server thread.
432 * WARNING : this function does not reschedule the remote thread.
433 * The scheduling can be forced by sending an IPI to the core running the remote thread.
434 ***************************************************************************************
435 * @ thread_xp   : extended pointer the remote thread.
436 * @ cause       : mask defining the cause (one hot).
437 * @ return non zero if the bit-vector was actually modified / return 0 otherwise
438 **************************************************************************************/
439uint32_t thread_unblock( xptr_t   thread_xp,
440                         uint32_t cause );
441
442/***************************************************************************************
443 * This function updates the calling thread user_time or kernel_time counters.
444 ***************************************************************************************
445 * @ thread   : local pointer on target thread.
446 * @ is_user  : update user time if true / update kernel time if false
447 **************************************************************************************/
448void thread_time_update( thread_t * thread,
449                         bool_t     is_user );
450
451/***************************************************************************************
452 * This function returns the extended pointer on a thread descriptor identified
453 * by its thread identifier, and process identifier.
454 * It can be called by any thread running in any cluster.
455 ***************************************************************************************
456 * @ pid     : process identifier.
457 * @ trdid   : thread identifier.
458 * @ return the extended pointer if thread found / return XPTR_NULL if not found.
459 **************************************************************************************/
460xptr_t thread_get_xptr( pid_t    pid,
461                        trdid_t  trdid );
462
463/***************************************************************************************
464 * This function checks that the thread identified by the <thread> argument does hold
465 * any busylock (local or remote).
466 * If the xlist of taken busylocks is not empty, it displays the set of taken locks,
467 * and makes a kernel panic. 
468 ***************************************************************************************
469 * @ thread    : local pointer on target thread.
470 * @ func_str  : faulty function name.
471 **************************************************************************************/
472void thread_assert_can_yield( thread_t    * thread,
473                              const char  * func_str );
474
475/***************************************************************************************
476 * This debug function display the list of busylocks (local or remote)
477 * currently owned by a the thread identified by the <thead_xp> argument.
478 * The <string> argument is printed in header (can be the calling function name).
479 * WARNING : the DEBUG_BUSYLOCK parameter must be set in the kernel_config.h file.
480 ***************************************************************************************
481 * @ thread_xp  : extended pointer on target thread.
482 * @ string     : defines the calling context.
483 **************************************************************************************/
484void thread_display_busylocks( xptr_t       thread_xp,
485                               const char * string );
486
487
488
489#endif  /* _THREAD_H_ */
Note: See TracBrowser for help on using the repository browser.