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

Last change on this file since 408 was 408, checked in by alain, 4 years ago

Fix several bugs in the fork() syscall.

File size: 27.8 KB
Line 
1/*
2 * thread.h -  Thread and related operations definition.
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner (2016)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#ifndef _THREAD_H_
26#define _THREAD_H_
27
28#include <hal_types.h>
29#include <shared_syscalls.h>
30#include <hal_special.h>
31#include <xlist.h>
32#include <list.h>
33#include <hal_context.h>
34#include <spinlock.h>
35#include <core.h>
36#include <chdev.h>
37#include <cluster.h>
38#include <process.h>
39#include <dev_ioc.h>
40#include <dev_nic.h>
41#include <dev_txt.h>
42#include <dev_mmc.h>
43#include <dev_dma.h>
44
45/***************************************************************************************
46 * These macros are used to compose or decompose global thread identifier (TRDID)
47 * to or from cluster identifier / local thread index (CXY , LTID)
48 **************************************************************************************/
49
50#define LTID_FROM_TRDID( trdid )   (ltid_t)(trdid & 0x0000FFFF)
51#define CXY_FROM_TRDID( trdid )    (cxy_t)(trdid >> 16)
52#define TRDID( cxy , ltid )        (trdid_t)((cxy << 16) | ltid )
53
54/***************************************************************************************
55 * This enum defines the thread types.
56 **************************************************************************************/
57
58typedef enum
59{
60        THREAD_USER    = 0,          /*! user thread (pthread)                            */
61        THREAD_RPC     = 1,          /*! kernel thread executing pending RPCs             */
62        THREAD_DEV     = 2,          /*! kernel thread executing I/O device commands      */
63        THREAD_IDLE    = 3,          /*! kernel idle thread                               */
64}
65thread_type_t;
66
67/***************************************************************************************
68 * This defines the thread flags bit-vector.
69 **************************************************************************************/
70
71#define THREAD_FLAG_LOADABLE     0x0001  /*! This thread has not been executed yet    */
72#define THREAD_FLAG_DETACHED     0x0002  /*! This thread is detached from parent      */
73#define THREAD_FLAG_JOIN         0x0004  /*! Parent thread made a join                */
74#define THREAD_FLAG_EXIT         0x0008  /*! This thread made an exit                 */
75#define THREAD_FLAG_SCHED        0x0010  /*! Scheduling required for this thread      */
76
77/***************************************************************************************
78 * This defines the masks associated to the thread signals.
79 **************************************************************************************/
80
81#define THREAD_SIG_KILL          0x0001  /*! This thread killed by another thread     */
82#define THREAD_SIG_SUICIDE       0x0002  /*! This thread required exit                */
83
84/***************************************************************************************
85 * This defines the masks associated to the blocking causes.
86 **************************************************************************************/
87
88#define THREAD_BLOCKED_GLOBAL    0x0001  /*! thread deactivated / wait activation     */
89#define THREAD_BLOCKED_IO        0x0002  /*! thread wait IO operation completion      */
90#define THREAD_BLOCKED_MAPPER    0x0004  /*! thread wait mapper                       */
91#define THREAD_BLOCKED_JOIN      0x0008  /*! thread blocked in join / wait exit       */
92#define THREAD_BLOCKED_EXIT      0x0010  /*! thread blocked in exit / wait join       */
93#define THREAD_BLOCKED_KILL      0x0020  /*! thread received kill signal              */
94#define THREAD_BLOCKED_SEM       0x0040  /*! thread wait semaphore                    */
95#define THREAD_BLOCKED_PAGE      0x0080  /*! thread wait page access                  */
96#define THREAD_BLOCKED_USERSYNC  0x0100  /*! thread wait POSIX (cond/mutex/barrier)   */
97#define THREAD_BLOCKED_RPC       0x0200  /*! thread wait RPC completion               */
98
99#define THREAD_BLOCKED_DEV_ISR   0x4000  /*! thread DEV wait ISR                      */
100
101/***************************************************************************************
102 * This structure defines thread instrumentation informations.
103 **************************************************************************************/
104
105typedef struct thread_info_s
106{
107        uint32_t              pgfault_nr;    /*! cumulated number of page fault           */
108        uint32_t              sched_nr;      /*! TODO ???  [AG]                           */
109        uint32_t              u_err_nr;      /*! TODO ???  [AG]                           */
110        uint32_t              m_err_nr;      /*! TODO ???  [AG]                           */
111        uint32_t              tm_tmp;        /*! temp date to compute execution duration  */
112        uint32_t              tm_exec;       /*! TODO ???  [AG]                           */
113        uint32_t              tm_create;     /*! date of the creation                     */
114        uint32_t              tm_born;       /*! date of the thread loading               */
115        uint32_t              tm_dead;       /*! date of the death                        */
116        cycle_t               tm_sleep;      /*! TODO ???  [AG]                           */
117        cycle_t               tm_wait;       /*! TODO ???  [AG]                           */
118        cycle_t               tm_usr;        /*! user execution duration                  */
119        cycle_t               tm_sys;        /*! system execution duration                */
120}
121thread_info_t;
122
123/***************************************************************************************
124 * This structure defines a thread descriptor.
125 * It is used for both the user threads and the kernel threads.
126 * In a process, a user thread is identified by a unique TRDID (thread identifier),
127 * that is returned by the kernel to the user:
128 * - The TRDID 16 LSB bits contain the LTID (Local Thread Index).
129 * - The TRDID 16 MSB bits contain the CXY of cluster containing the thread.
130 * - The LTID is used to index the th_tbl[] array in the local process descriptor.
131 * This TRDID is computed by the process_register_thread() function, when the user
132 * thread is registered in the local copy of the process descriptor.
133 *
134 * WARNING : Don't modify the first 3 fields order, as this order is used by the
135 * hal_kentry assembly code for the TSAR architecture.
136 **************************************************************************************/
137
138#define THREAD_SIGNATURE    0xDEADBEEF
139
140typedef struct thread_s
141{
142        void              * cpu_context;     /*! pointer on CPU context switch            */
143        void              * fpu_context;     /*! pointer on FPU context switch            */
144    void              * uzone;           /*! pointer on uzone for hal_kentry          */
145
146        intptr_t            k_stack_base;    /*! kernel stack base address                */
147        uint32_t            k_stack_size;    /*! kernel stack size (bytes)                */
148
149        uint32_t            trdid;           /*! thread index (cxy.ltid)                  */
150        thread_type_t       type;            /*! thread type                              */
151        uint32_t            quantum;         /*! number of clock ticks given to thread    */
152        uint32_t            ticks_nr;        /*! number of ticks used                     */
153        uint32_t            time_last_check; /*! last cpu_time_stamp                      */
154        core_t            * core;            /*! pointer to the owner core                */
155        process_t         * process;         /*! pointer on local process descriptor      */
156    xptr_t              parent;          /*! extended pointer on parent thread        */
157
158    void              * exit_value;      /*! exit_value used in case of join          */
159
160        uint32_t            local_locks;         /*! number of local locks owned by thread    */
161    list_entry_t        locks_root;      /*! root of local locks list                 */
162
163    remote_spinlock_t * flags_lock;      /*! lock protecting the flags                */
164
165        uint32_t            remote_locks;        /*! number of local locks owned by thread    */
166    xlist_entry_t       xlocks_root;     /*! root of remote locks list                */
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      */
173
174    uint32_t            flags;           /*! bit vector of flags                      */
175    uint32_t            signals;         /*! bit vector of (KILL / SUICIDE) signals   */
176    uint32_t            blocked;         /*! bit vector of blocking causes            */
177
178        error_t             errno;           /*! errno value set by last system call      */
179    uint32_t            utls;            /*! user thread local storage                */
180
181    bool_t              fork_user;       /*! user defined placement for next fork()   */
182    cxy_t               fork_cxy;        /*! target cluster  for next fork()          */
183
184        xlist_entry_t       children_root;   /*! root of list of attached children        */
185    uint32_t            children_nr;     /*! number of attached children threads      */
186    remote_spinlock_t * children_lock;   /*! lock protecting the children list        */
187
188    xlist_entry_t       brothers_list;   /*! list of attached threads to same parent  */
189
190        list_entry_t        sched_list;      /*! member of threads attached to same core  */
191
192    chdev_t           * chdev;           /*! chdev pointer (for a DEV thread only)    */
193
194    reg_t               save_sr;         /*! used by sched_yield() function           */
195
196    ioc_command_t       ioc_cmd;         /*! IOC device generic command               */
197    txt_command_t       txt_cmd;         /*! TXT device generic command               */
198    nic_command_t       nic_cmd;         /*! NIC device generic command               */
199    mmc_command_t       mmc_cmd;         /*! MMC device generic command               */
200    dma_command_t       dma_cmd;         /*! DMA device generic command               */
201
202        cxy_t               rpc_client_cxy;  /*! client cluster index (for a RPC thread)  */
203
204    xlist_entry_t       wait_list;       /*! member of threads blocked on same cond   */
205
206        thread_info_t       info;            /*! embedded thread_info_t                   */
207
208        uint32_t            signature;       /*! for kernel stack overflow detection      */
209}
210thread_t;
211
212/***************************************************************************************
213 * This macro returns a pointer on the calling thread from the core hardware register.
214 **************************************************************************************/
215
216#define CURRENT_THREAD  (hal_get_current_thread())
217
218/***************************************************************************************
219 * This function returns a printable string for a thread type.
220 ***************************************************************************************
221 * @ type    : thread type.
222 * returns pointer on string.
223 **************************************************************************************/
224char * thread_type_str( uint32_t type );
225
226/***************************************************************************************
227 * This function is used by the pthread_create() system call to create a "new" thread
228 * in an existing process. It allocates memory for an user thread descriptor in the
229 * local cluster, and initializes it from information contained in the arguments.
230 * The CPU context is initialized from scratch. If required by the <attr> argument,
231 * the new thread is attached to the core specified in <attr>.
232 * It is registered in the local process descriptor specified by the <pid> argument.
233 * The thread descriptor pointer is returned to allow the parent thread to register it
234 * in its children list.
235 * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated to start.
236 ***************************************************************************************
237 * @ pid          : process identifier.
238 * @ start_func   : pointer on entry function.
239 * @ start_args   : pointer on function argument (can be NULL).
240 * @ attr         : pointer on pthread attributes descriptor.
241 * @ new_thread   : [out] address of buffer for new thread descriptor pointer.
242 * @ returns 0 if success / returns ENOMEM if error.
243 **************************************************************************************/
244error_t thread_user_create( pid_t             pid,
245                            void            * start_func,
246                            void            * start_arg,
247                            pthread_attr_t  * attr,
248                            thread_t       ** new_thread );
249
250/***************************************************************************************
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.
254 * The new thread is attached to the core that has the lowest load in local cluster.
255 * It is registered in the "child" process defined by the <child_process> argument.
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).
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.
264 ***************************************************************************************
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.
269 **************************************************************************************/
270error_t thread_user_fork( xptr_t      parent_thread_xp,
271                          process_t * child_process,
272                          thread_t ** child_thread );
273
274/***************************************************************************************
275 * This function allocates memory for a kernel thread descriptor in the local cluster,
276 * and initializes it from arguments values.
277 * It is called by kernel_init() to statically create all DEV server threads
278 * It is also called to dynamically create RPC threads when required.
279 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
280 ***************************************************************************************
281 * @ new_thread   : address of buffer for new thread pointer.
282 * @ type         : kernel thread type.
283 * @ func         : pointer on function.
284 * @ args         : function arguments.
285 * @ core_lid     : local core index.
286 * @ returns 0 if success / returns ENOMEM if error
287 **************************************************************************************/
288error_t thread_kernel_create( thread_t     ** new_thread,
289                              thread_type_t   type,
290                              void          * func,
291                              void          * args,
292                              lid_t           core_lid );
293
294/***************************************************************************************
295 * This function initializes an existing thread descriptor from arguments values.
296 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
297 * It is called by the kernel_init() function to initialize the IDLE thread.
298 ***************************************************************************************
299 * @ thread   : pointer on existing thread descriptor.
300 * @ type     : kernel thread type.
301 * @ func     : pointer on function.
302 * @ args     : function arguments.
303 * @ core_lid : local core index.
304 * @ returns 0 if success / returns EINVAL if error
305 **************************************************************************************/
306error_t thread_kernel_init( thread_t      * thread,
307                            thread_type_t   type,
308                            void          * func,
309                            void          * args,
310                            lid_t           core_lid );
311
312/***************************************************************************************
313 * This function releases the physical memory allocated for a thread descriptor
314 * in the local cluster. It can be used for both an user and a kernel thread.
315 * The physical memory dynamically allocated in the HEAP or MMAP zones by an user
316 * thread will be released when the process is killed, and the page table flushed.
317 ***************************************************************************************
318 * @ thread  : pointer on the thread descriptor to release.
319 **************************************************************************************/
320void thread_destroy( thread_t * thread );
321
322/***************************************************************************************
323 * This function defines the code of the thread executed by all cores after kernel_init,
324 * or when no other thread is runnable for a given core.
325 *
326 * TODO: In the TSAR architecture, it enters an infinite loop, in wich it forces
327 * the core in sleep (low-power) mode. Any IRQ will force the core to exit this sleep
328 * mode, but no ISR is executed.
329 * TODO: We must analyse if we have the same behaviour for I86 architectures...
330 **************************************************************************************/
331void thread_idle_func();
332
333/***************************************************************************************
334 * This function registers a child thread in the global list of attached
335 * children threads of a parent thread.
336 * It does NOT take a lock, as this function is always called by the parent thread.
337 ***************************************************************************************
338 * @ parent_xp : extended pointer on the parent thread descriptor.
339 * @ child_xp  : extended pointer on the child thread descriptor.
340 **************************************************************************************/
341void thread_child_parent_link( xptr_t  parent_xp,
342                               xptr_t  child_xp );
343
344/***************************************************************************************
345 * This function removes an user thread from the parent thread global list
346 * of attached children threads.
347 ***************************************************************************************
348 * @ parent_xp : extended pointer on the parent thread descriptor.
349 * @ child_xp  : extended pointer on the child thread descriptor.
350 **************************************************************************************/
351void thread_child_parent_unlink( xptr_t parent_xp,
352                                 xptr_t child_xp );
353
354/***************************************************************************************
355 * This function atomically sets a signal in a thread descriptor.
356 ***************************************************************************************
357 * @ thread    : local pointer on target thread.
358 *s released all locks @ mask      : mask on selected signal.
359 **************************************************************************************/
360inline void thread_set_signal( thread_t * thread,
361                               uint32_t   mask );
362
363/***************************************************************************************
364 * This function resets a signal in a thread descriptor.
365 ***************************************************************************************
366 * @ thread    : local pointer on target thread.
367 * @ mask      : mask on selected signal.
368 **************************************************************************************/
369inline void thread_reset_signal( thread_t * thread,
370                                 uint32_t   mask );
371
372/***************************************************************************************
373 * This function checks if the calling thread can deschedule.
374 ***************************************************************************************
375 * @ returns true if no locks taken.
376 **************************************************************************************/
377inline bool_t thread_can_yield();
378
379/***************************************************************************************
380 * This function implements the delayed descheduling mechanism : It is called  by
381 * all lock release functions, and calls the sched_yield() function when all locks
382 * have beeen released and the calling thread THREAD_FLAG_SCHED flag is set.
383 **************************************************************************************/
384void thread_check_sched();
385
386/***************************************************************************************
387 * This function is used by the calling thread to suicide.
388 * All locks must be previously released. The scenario depends on the DETACHED flag.
389 * if detached :
390 * 1) the calling thread sets the SIG_SUICIDE bit in the "signals" bit_vector,
391 *    registers the BLOCKED_GLOBAL bit in the "blocked" bit_vector, and deschedule.
392 * 2) the scheduler, detecting the SIG_SUICIDE bit, remove the thread from the
393 *    scheduler list, remove the thread from its process, and destroys the thread.
394 * if attached :
395 * 1) the calling thread simply sets the BLOCKED_EXIT bit in the "blocked" bit vector
396 *    and deschedule.
397 * 2) The SIG_KILL bit and BLOCKED_SIGNAL bits are set by the parent thread when
398 *    executing the pthread_join(), and detecting the BLOCKED_EXIT bit.
399 *    The scenario is a standard kill as described below.
400 ***************************************************************************************
401 * @ returns 0 if success / returns EINVAL if locks_count is not zero.
402 **************************************************************************************/
403error_t thread_exit();
404
405/***************************************************************************************
406 * This function request to kill a local target thread, with the following scenario:
407 * 1. This function set the BLOCKED_GLOBAL bit in target thread "blocked" bit_vector,
408 *    set the SIG_KILL bit in target thread "signals" bit_vector, and send an IPI
409 *    to the target thread core to force scheduling.
410 * 2. The scheduler, detecting the SIG_KILL set, removes the thread from the scheduler
411 *    list, and reset the SIG_KILL bit to acknowledge the killer.
412 * 3. The caller of this function, (such as the process_kill() function), must poll
413 *    SIG_KILL bit until reset, detach the thread from its parent if the thread is
414 *    attached, remove the thread from its process, and destroys the thread.
415 *
416 * NOTE: The third step must be done by the caller to allows the process_kill()
417 *       function to parallelize the work on all schedulers in a given cluster.
418 ***************************************************************************************
419 * @ thread   : local pointer on the target thread.
420 **************************************************************************************/
421void thread_kill( thread_t * thread );
422
423/***************************************************************************************
424 * This function registers a blocking cause in the target thread "blocked" bit vector.
425 * Warning : this function does not deschedule the calling thread, and the descheduling
426 * must be explicitely forced by a sched_yield().
427 ***************************************************************************************
428 * @ thread   : local pointer on target thread descriptor.
429 * @ cause    : mask defining the cause (one hot).
430 **************************************************************************************/
431void thread_block( thread_t * thread,
432                   uint32_t   cause );
433
434/***************************************************************************************
435 * This function resets the bit identified by the cause argument in the "blocked"
436 * bit vector of a remote thread descriptor, using an atomic access.
437 * We need an extended pointer, because the client thread of an I/O operation on a
438 * given device is not in the same cluster as the associated device descriptor.
439 * Warning : this function does not reschedule the remote thread.
440 * The scheduling can be forced by sending an IPI to the core running the remote thread.
441 ***************************************************************************************
442 * @ thread   : extended pointer on the remote thread.
443 * @ cause    : mask defining the cause (one hot).
444 * @ return non zero if the bit-vector was actually modified / return 0 otherwise
445 **************************************************************************************/
446uint32_t thread_unblock( xptr_t   thread,
447                         uint32_t cause );
448
449/***************************************************************************************
450 * This function updates the calling thread user_time counter, and resets the thread
451 * cycles counter.
452 * TODO This function is not implemented.
453 ***************************************************************************************
454 * @ thread   : local pointer on target thread.
455 **************************************************************************************/
456void thread_user_time_update( thread_t * thread );
457
458/**************************************************************************************n
459 * This function updates the calling thread kernel_time counter, and resets the thread
460 * cycles counter.
461 * TODO This function is not implemented.
462 ***************************************************************************************
463 * @ thread   : local pointer on target thread.
464 **************************************************************************************/
465void thread_kernel_time_update( thread_t * thread );
466
467/***************************************************************************************
468 * This function handles all pending signals for the thread identified by the <thread>
469 * argument. It is called each time the core exits the kernel, after handling an
470 * interrupt, exception or syscall.
471 * TODO This function is not implemented.
472 ***************************************************************************************
473 * @ thread   : local pointer on target thread.
474 **************************************************************************************/
475void thread_signals_handle( thread_t * thread );
476
477/***************************************************************************************
478 * This function returns the extended pointer on a thread descriptor identified
479 * by its thread identifier, and process identifier.
480 * It can be called by any thread running in any cluster.
481 ***************************************************************************************
482 * @ pid     : process identifier.
483 * @ trdid   : thread identifier.
484 * @ return the extended pointer if thread found / return XPTR_NULL if not found.
485 **************************************************************************************/
486xptr_t thread_get_xptr( pid_t    pid,
487                        trdid_t  trdid );
488
489
490#endif  /* _THREAD_H_ */
Note: See TracBrowser for help on using the repository browser.