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

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

First implementation of fork/exec.

File size: 27.6 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 <cluster.h>
37#include <process.h>
38#include <dev_ioc.h>
39#include <dev_nic.h>
40#include <dev_txt.h>
41#include <dev_mmc.h>
42#include <dev_dma.h>
43
44/***************************************************************************************
45 * These macros are used to compose or decompose global thread identifier (TRDID)
46 * to or from cluster identifier / local thread index (CXY , LTID)
47 **************************************************************************************/
48
49#define LTID_FROM_TRDID( trdid )   (ltid_t)(trdid & 0x0000FFFF)
50#define CXY_FROM_TRDID( trdid )    (cxy_t)(trdid >> 16)
51#define TRDID( cxy , ltid )        (trdid_t)((cxy << 16) | ltid )
52
53/***************************************************************************************
54 * This enum defines the thread types.
55 **************************************************************************************/
56
57typedef enum
58{
59        THREAD_USER    = 0,          /*! user thread (pthread)                            */
60        THREAD_RPC     = 1,          /*! kernel thread executing pending RPCs             */
61        THREAD_DEV     = 2,          /*! kernel thread executing I/O device commands      */
62        THREAD_IDLE    = 3,          /*! kernel idle thread                               */
63}
64thread_type_t;
65
66/***************************************************************************************
67 * This defines the thread flags bit-vector.
68 **************************************************************************************/
69
70#define THREAD_FLAG_LOADABLE     0x0001  /*! This thread has not been executed yet    */
71#define THREAD_FLAG_DETACHED     0x0002  /*! This thread is detached from parent      */
72#define THREAD_FLAG_JOIN         0x0004  /*! Parent thread made a join                */
73#define THREAD_FLAG_EXIT         0x0008  /*! This thread made an exit                 */
74#define THREAD_FLAG_SCHED        0x0010  /*! Scheduling required for this thread      */
75
76/***************************************************************************************
77 * This defines the masks associated to the thread signals.
78 **************************************************************************************/
79
80#define THREAD_SIG_KILL          0x0001  /*! This thread killed by another thread     */
81#define THREAD_SIG_SUICIDE       0x0002  /*! This thread required exit                */
82
83/***************************************************************************************
84 * This defines the masks associated to the blocking causes.
85 **************************************************************************************/
86
87#define THREAD_BLOCKED_GLOBAL    0x0001  /*! thread deactivated / wait activation     */
88#define THREAD_BLOCKED_IO        0x0002  /*! thread wait IO operation completion      */
89#define THREAD_BLOCKED_MAPPER    0x0004  /*! thread wait mapper                       */
90#define THREAD_BLOCKED_JOIN      0x0008  /*! thread blocked in join / wait exit       */
91#define THREAD_BLOCKED_EXIT      0x0010  /*! thread blocked in exit / wait join       */
92#define THREAD_BLOCKED_KILL      0x0020  /*! thread received kill signal              */
93#define THREAD_BLOCKED_SEM       0x0040  /*! thread wait semaphore                    */
94#define THREAD_BLOCKED_PAGE      0x0080  /*! thread wait page access                  */
95#define THREAD_BLOCKED_USERSYNC  0x0100  /*! thread wait POSIX (cond/mutex/barrier)   */
96#define THREAD_BLOCKED_RPC       0x0200  /*! thread wait RPC completion               */
97
98#define THREAD_BLOCKED_DEV_QUEUE 0x2000  /*! thread DEV wait queue                    */
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 4 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;     /*! used for context switch                  */
143        void              * fpu_context;     /*! used for dynamic FPU allocation          */
144
145        intptr_t            k_stack_base;    /*! kernel stack base address                */
146        uint32_t            k_stack_size;    /*! kernel stack size (bytes)                */
147
148        uint32_t            trdid;           /*! thread index (cxy.ltid)                  */
149        thread_type_t       type;            /*! thread type                              */
150        uint32_t            quantum;         /*! number of clock ticks given to thread    */
151        uint32_t            ticks_nr;        /*! number of ticks used                     */
152        uint32_t            time_last_check; /*! last cpu_time_stamp                      */
153        core_t            * core;            /*! pointer to the owner core                */
154        process_t         * process;         /*! pointer on local process descriptor      */
155    xptr_t              parent;          /*! extended pointer on parent thread        */
156
157    void              * exit_value;      /*! exit_value used in case of join          */
158
159        uint32_t            local_locks;         /*! number of local locks owned by thread    */
160    list_entry_t        locks_root;      /*! root of local locks list                 */
161
162    remote_spinlock_t * flags_lock;      /*! lock protecting the flags                */
163
164        uint32_t            remote_locks;        /*! number of local locks owned by thread    */
165    xlist_entry_t       xlocks_root;     /*! root of remote locks list                */
166
167        intptr_t            u_stack_base;    /*! user stack base address                  */
168        uint32_t            u_stack_size;    /*! user stack size (bytes)                  */
169
170    void              * entry_func;      /*! pointer on entry function                */
171    void              * entry_args;      /*! pointer on entry function arguments      */
172
173    uint32_t            flags;           /*! bit vector of flags                      */
174    volatile uint32_t   blocked;         /*! bit vector of blocking causes            */
175    volatile uint32_t   signals;         /*! bit vector of (KILL / SUICIDE) signals   */
176
177        error_t             errno;           /*! errno value set by last system call      */
178    uint32_t            utls;            /*! user thread local storage                */
179
180    bool_t              fork_user;       /*! user defined placement for next fork()   */
181    cxy_t               fork_cxy;        /*! target cluster  for next fork()          */
182
183        xlist_entry_t       children_root;   /*! root of list of attached children        */
184    uint32_t            children_nr;     /*! number of attached children threads      */
185    remote_spinlock_t * children_lock;   /*! lock protecting the children list        */
186
187    xlist_entry_t       brothers_list;   /*! list of attached threads to same parent  */
188
189        list_entry_t        sched_list;      /*! member of threads attached to same core  */
190
191    uint32_t            dev_channel;     /*! device channel for a DEV thread          */
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        cxy_t               rpc_client_cxy;  /*! client cluster index (for a RPC thread)  */
200
201    xlist_entry_t       wait_list;       /*! member of threads blocked on same cond   */
202
203        thread_info_t       info;            /*! embedded thread_info_t                   */
204
205        uint32_t            signature;       /*! for kernel stack overflow detection      */
206}
207thread_t;
208
209/***************************************************************************************
210 * This macro returns a pointer on the calling thread from the core hardware register.
211 **************************************************************************************/
212
213#define CURRENT_THREAD  (hal_get_current_thread())
214
215/***************************************************************************************
216 * This function returns a printable string for a thread type.
217 ***************************************************************************************
218 * @ type    : thread type.
219 * returns pointer on string.
220 **************************************************************************************/
221char * thread_type_str( uint32_t type );
222
223/***************************************************************************************
224 * This function allocates memory for a user thread descriptor in the local cluster,
225 * and initializes it from information contained in the arguments.
226 * It is used by the "pthread_create" system call.
227 * The CPU context is initialized from scratch, and the "loadable" field is set.
228 * The new thread is attached to the core specified in the <attr> argument.
229 * It is registered in the local process descriptor specified by the <pid> argument.
230 * The thread descriptor pointer is returned to allow the parent thread to register it
231 * in its children list.
232 * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated to start.
233 ***************************************************************************************
234 * @ pid          : process identifier.
235 * @ start_func   : pointer on entry function.
236 * @ start_args   : pointer on function argument (can be NULL).
237 * @ attr         : pointer on pthread attributes descriptor.
238 * @ new_thread   : [out] address of buffer for new thread descriptor pointer.
239 * @ returns 0 if success / returns ENOMEM if error.
240 **************************************************************************************/
241error_t thread_user_create( pid_t             pid,
242                            void            * start_func,
243                            void            * start_arg,
244                            pthread_attr_t  * attr,
245                            thread_t       ** new_thread );
246
247/***************************************************************************************
248 * This function is used by the fork() system call to create the child process main
249 * thread. It allocates memory for an user thread descriptor in the local cluster,
250 * and initializes it from information contained in the calling thread descriptor.
251 * The new thread is attached to the core that has the lowest load in local cluster.
252 * It is registered in the child process descriptor defined by the <process> argument.
253 * This new thread inherits its user stack from the parent thread, as it uses the
254 * Copy-On-Write mechanism to get a private stack when required.
255 * The content of the parent kernel stack is copied into the child kernel stack, as
256 * the Copy-On-Write mechanism cannot be used for kernel segments (because kernel
257 * uses physical addressing on some architectures).
258 * The CPU and FPU execution contexts are created and linked to the new thread,
259 * but the actual context copy is NOT done. The THREAD_BLOCKED_GLOBAL bit is set,
260 * and the thread must be explicitely unblocked later to make the new thread runable.
261 ***************************************************************************************
262 * @ process      : local pointer on owner process descriptor.
263 * @ stack_base   : user stack base address (from parent).
264 * @ stack_size   : user stack size (from parent).
265 * @ new_thread   : [out] address of buffer for new thread descriptor pointer.
266 * @ returns 0 if success / returns ENOMEM if error.
267 **************************************************************************************/
268error_t thread_user_fork( process_t * process,
269                          intptr_t    stack_base,
270                          uint32_t    stack_size,
271                          thread_t ** new_thread );
272
273/***************************************************************************************
274 * This function allocates memory for a kernel thread descriptor in the local cluster,
275 * and initializes it from arguments values, calling the thread_kernel_init() function,
276 * that also allocates and initializes the CPU context.
277 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
278 ***************************************************************************************
279 * @ new_thread   : address of buffer for new thread pointer.
280 * @ type         : kernel thread type.
281 * @ func         : pointer on function.
282 * @ args         : function arguments.
283 * @ core_lid     : local core index.
284 * @ returns 0 if success / returns ENOMEM if error
285 **************************************************************************************/
286error_t thread_kernel_create( thread_t     ** new_thread,
287                              thread_type_t   type,
288                              void          * func,
289                              void          * args,
290                              lid_t           core_lid );
291
292/***************************************************************************************
293 * This function initializes an existing kernel thread descriptor from arguments values.
294 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
295 ***************************************************************************************
296 * @ thread   : pointer on existing thread descriptor.
297 * @ type     : kernel thread type.
298 * @ func     : pointer on function.
299 * @ args     : function arguments.
300 * @ core_lid : local core index.
301 * @ returns 0 if success / returns EINVAL if error
302 **************************************************************************************/
303error_t thread_kernel_init( thread_t      * thread,
304                            thread_type_t   type,
305                            void          * func,
306                            void          * args,
307                            lid_t           core_lid );
308
309/***************************************************************************************
310 * This function releases the physical memory allocated for a thread descriptor
311 * in the local cluster. It can be used for both an user and a kernel thread.
312 * The physical memory dynamically allocated in the HEAP or MMAP zones by an user
313 * thread will be released when the process is killed, and the page table flushed.
314 ***************************************************************************************
315 * @ thread  : pointer on the thread descriptor to release.
316 **************************************************************************************/
317void thread_destroy( thread_t * thread );
318
319/***************************************************************************************
320 * This function defines the code of the thread executed by all cores after kernel_init,
321 * or when no other thread is runnable for a given core.
322 *
323 * TODO: In the TSAR architecture, it enters an infinite loop, in wich it forces
324 * the core in sleep (low-power) mode. Any IRQ will force the core to exit this sleep
325 * mode, but no ISR is executed.
326 * TODO: We must analyse if we have the same behaviour for I86 architectures...
327 **************************************************************************************/
328void thread_idle_func();
329
330/***************************************************************************************
331 * This function registers a child thread in the global list of attached
332 * children threads of a parent thread.
333 * It does NOT take a lock, as this function is always called by the parent thread.
334 ***************************************************************************************
335 * @ parent_xp : extended pointer on the parent thread descriptor.
336 * @ child_xp  : extended pointer on the child thread descriptor.
337 **************************************************************************************/
338void thread_child_parent_link( xptr_t  parent_xp,
339                               xptr_t  child_xp );
340
341/***************************************************************************************
342 * This function removes an user thread from the parent thread global list
343 * of attached children threads.
344 ***************************************************************************************
345 * @ parent_xp : extended pointer on the parent thread descriptor.
346 * @ child_xp  : extended pointer on the child thread descriptor.
347 **************************************************************************************/
348void thread_child_parent_unlink( xptr_t parent_xp,
349                                 xptr_t child_xp );
350
351/***************************************************************************************
352 * This function atomically sets a signal in a thread descriptor.
353 ***************************************************************************************
354 * @ thread    : local pointer on target thread.
355 *s released all locks @ mask      : mask on selected signal.
356 **************************************************************************************/
357inline void thread_set_signal( thread_t * thread,
358                               uint32_t   mask );
359
360/***************************************************************************************
361 * This function resets a signal in a thread descriptor.
362 ***************************************************************************************
363 * @ thread    : local pointer on target thread.
364 * @ mask      : mask on selected signal.
365 **************************************************************************************/
366inline void thread_reset_signal( thread_t * thread,
367                                 uint32_t   mask );
368
369/***************************************************************************************
370 * This function checks if the calling thread can deschedule.
371 ***************************************************************************************
372 * @ returns true if no locks taken.
373 **************************************************************************************/
374inline bool_t thread_can_yield();
375
376/***************************************************************************************
377 * This function implements the delayed descheduling mechanism : It is called  by
378 * all lock release functions, and calls the sched_yield() function when all locks
379 * have beeen released and the calling thread THREAD_FLAG_SCHED flag is set.
380 **************************************************************************************/
381void thread_check_sched();
382
383/***************************************************************************************
384 * This function is used by the calling thread to suicide.
385 * All locks must be previously released. The scenario depends on the DETACHED flag.
386 * if detached :
387 * 1) the calling thread sets the SIG_SUICIDE bit in the "signals" bit_vector,
388 *    registers the BLOCKED_GLOBAL bit in the "blocked" bit_vector, and deschedule.
389 * 2) the scheduler, detecting the SIG_SUICIDE bit, remove the thread from the
390 *    scheduler list, remove the thread from its process, and destroys the thread.
391 * if attached :
392 * 1) the calling thread simply sets the BLOCKED_EXIT bit in the "blocked" bit vector
393 *    and deschedule.
394 * 2) The SIG_KILL bit and BLOCKED_SIGNAL bits are set by the parent thread when
395 *    executing the pthread_join(), and detecting the BLOCKED_EXIT bit.
396 *    The scenario is a standard kill as described below.
397 ***************************************************************************************
398 * @ returns 0 if success / returns EINVAL if locks_count is not zero.
399 **************************************************************************************/
400error_t thread_exit();
401
402/***************************************************************************************
403 * This function request to kill a local target thread, with the following scenario:
404 * 1. This function set the BLOCKED_GLOBAL bit in target thread "blocked" bit_vector,
405 *    set the SIG_KILL bit in target thread "signals" bit_vector, and send an IPI
406 *    to the target thread core to force scheduling.
407 * 2. The scheduler, detecting the SIG_KILL set, removes the thread from the scheduler
408 *    list, and reset the SIG_KILL bit to acknowledge the killer.
409 * 3. The caller of this function, (such as the process_kill() function), must poll
410 *    SIG_KILL bit until reset, detach the thread from its parent if the thread is
411 *    attached, remove the thread from its process, and destroys the thread.
412 *
413 * NOTE: The third step must be done by the caller to allows the process_kill()
414 *       function to parallelize the work on all schedulers in a given cluster.
415 ***************************************************************************************
416 * @ thread   : local pointer on the target thread.
417 **************************************************************************************/
418void thread_kill( thread_t * thread );
419
420/***************************************************************************************
421 * This function registers a blocking cause in the target thread "blocked" bit vector.
422 * Warning : this function does not deschedule the calling thread, and the descheduling
423 * must be explicitely forced by a sched_yield().
424 ***************************************************************************************
425 * @ thread   : local pointer on target thread descriptor.
426 * @ cause    : mask defining the cause (one hot).
427 **************************************************************************************/
428void thread_block( thread_t * thread,
429                   uint32_t   cause );
430
431/***************************************************************************************
432 * This function resets the bit identified by the cause argument in the "blocked"
433 * bit vector of a remote thread descriptor, using an atomic access.
434 * We need an extended pointer, because the client thread of an I/O operation on a
435 * given device is not in the same cluster as the associated device descriptor.
436 * Warning : this function does not reschedule the remote thread.
437 * The scheduling can be forced by sending an IPI to the core running the remote thread.
438 ***************************************************************************************
439 * @ thread   : extended pointer on the remote thread.
440 * @ cause    : mask defining the cause (one hot).
441 * @ return non zero if the bit-vector was actually modified / return 0 otherwise
442 **************************************************************************************/
443uint32_t thread_unblock( xptr_t   thread,
444                         uint32_t cause );
445
446/***************************************************************************************
447 * This function updates the calling thread user_time counter, and resets the thread
448 * cycles counter.
449 * TODO This function is not implemented.
450 ***************************************************************************************
451 * @ thread   : local pointer on target thread.
452 **************************************************************************************/
453void thread_user_time_update( thread_t * thread );
454
455/**************************************************************************************n
456 * This function updates the calling thread kernel_time counter, and resets the thread
457 * cycles counter.
458 * TODO This function is not implemented.
459 ***************************************************************************************
460 * @ thread   : local pointer on target thread.
461 **************************************************************************************/
462void thread_kernel_time_update( thread_t * thread );
463
464/***************************************************************************************
465 * This function handles all pending signals for the thread identified by the <thread>
466 * argument. It is called each time the core exits the kernel, after handling an
467 * interrupt, exception or syscall.
468 * TODO This function is not implemented.
469 ***************************************************************************************
470 * @ thread   : local pointer on target thread.
471 **************************************************************************************/
472void thread_signals_handle( thread_t * thread );
473
474/***************************************************************************************
475 * This function returns the extended pointer on a thread descriptor identified
476 * by its thread identifier, and process identifier.
477 * It can be called by any thread running in any cluster.
478 ***************************************************************************************
479 * @ pid     : process identifier.
480 * @ trdid   : thread identifier.
481 * @ return the extended pointer if thread found / return XPTR_NULL if not found.
482 **************************************************************************************/
483xptr_t thread_get_xptr( pid_t    pid,
484                        trdid_t  trdid );
485
486
487#endif  /* _THREAD_H_ */
Note: See TracBrowser for help on using the repository browser.