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

Last change on this file since 21 was 16, checked in by alain, 8 years ago

mprove the HAL for interrupt, exception, syscall handling.

File size: 26.5 KB
RevLine 
[1]1/*
2 * thread.h -  Thread and related operations definition.
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Mohamed Lamine Karaoui (2015)
6 *         Alain Greiner (2016)
7 *
8 * Copyright (c) UPMC Sorbonne Universites
9 *
10 * This file is part of ALMOS-MKH.
11 *
12 * ALMOS-MKH is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2.0 of the License.
15 *
16 * ALMOS-MKH is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef _THREAD_H_
27#define _THREAD_H_
28
29#include <hal_types.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>
[5]42#include <dev_dma.h>
[1]43
44/***************************************************************************************
45 * This defines the various pthread_attr_t flags.
46 **************************************************************************************/
47
48#define PT_FLAG_DETACH                0x001    // user defined not joinable
49#define PT_FLAG_CLUSTER_DEFINED       0x002    // user defined target cluster
50#define PT_FLAG_CORE_DEFINED          0x004    // user defined core cluster
51
52/***************************************************************************************
53 * This structure defines the input argument of the pthread_create() system call.
54 * It contains all informations required to initialize an user thread, and is passed
55 * as input argument to the thread_user_create() function.
56 * It is partly set by the kernel, partly set by the user application itself,
57 * using the pthread_attr_***() functions.
58 **************************************************************************************/
59 
60typedef struct pthread_attr_s
61{
62    pid_t       pid;             /*! owner process identifier                         */
63        void      * entry_func;      /*! pointer on entry function                        */
64        void      * entry_args;      /*! pointer on entry function arguments              */
65        uint32_t    flags;           /*! pthread flags        (entirely user specified)   */
66        cxy_t       cxy;             /*! target cluster       (can be user specified)     */
67        lid_t       lid;             /*! target core          (can be user specified)     */
68} 
69pthread_attr_t;
70
71
72/***************************************************************************************
73 * This enum defines the thread types.
74 **************************************************************************************/
75
76typedef enum
77{   
78        THREAD_USER    = 0,          /*! user thread (pthread)                            */ 
79        THREAD_RPC     = 1,          /*! kernel thread executing pending RPCs             */
80        THREAD_DEV     = 2,          /*! kernel thread executing I/O device commands      */
81        THREAD_KERNEL  = 3,          /*! other kernel thread                              */
82        THREAD_IDLE    = 4,          /*! kernel idle thread                               */
83        THREAD_TYPES_NR
84} 
85thread_type_t;
86
87/***************************************************************************************
88 * This defines the masks associated to the thread flags.
89 **************************************************************************************/
90
91#define THREAD_FLAG_LOADABLE     0x0001  /*! This thread has not been executed yet    */
92#define THREAD_FLAG_DETACHED     0x0002  /*! This user thread is detached from parent */
93
94/***************************************************************************************
95 * This defines the masks associated to the thread signals.
96 **************************************************************************************/
97
98#define THREAD_SIG_KILL          0x0001  /*! This thread must be destroyed ASAP       */
99
100/***************************************************************************************
101 * This defines the masks associated to the blocking causes.
102 **************************************************************************************/
103
104#define THREAD_BLOCKED_GLOBAL    0x0001  /*! thread global blocking                   */ 
105#define THREAD_BLOCKED_IO        0x0002  /*! thread wait IO operation completion      */
106#define THREAD_BLOCKED_MAPPER    0x0004  /*! thread wait mapper                       */
107#define THREAD_BLOCKED_EXIT      0x0010  /*! thread requested exit                    */
108#define THREAD_BLOCKED_KILL      0x0020  /*! thread received kill signal              */
109#define THREAD_BLOCKED_SEM       0x0040  /*! thread wait semaphore                    */
110#define THREAD_BLOCKED_PAGE      0x0080  /*! thread wait page access                  */
111#define THREAD_BLOCKED_IDLE      0x1000  /*! thread RPC wait activation               */
112#define THREAD_BLOCKED_DEV_QUEUE 0x2000  /*! thread DEV wait queue                    */
113#define THREAD_BLOCKED_DEV_ISR   0x4000  /*! thread DEV wait ISR                      */
114
115/***************************************************************************************
116 * This structure defines thread instrumentation informations.
117 **************************************************************************************/
118
119typedef struct thread_info_s
120{
121        uint32_t              pgfault_nr;    /*! cumulated number of page fault           */
122        uint32_t              sched_nr;      /*! TODO ???  [AG]                           */
123        uint32_t              u_err_nr;      /*! TODO ???  [AG]                           */
124        uint32_t              m_err_nr;      /*! TODO ???  [AG]                           */
125        uint32_t              tm_tmp;        /*! temp date to compute execution duration  */
126        uint32_t              tm_exec;       /*! TODO ???  [AG]                           */
127        uint32_t              tm_create;     /*! date of the creation                     */
128        uint32_t              tm_born;       /*! date of the thread loading               */
129        uint32_t              tm_dead;       /*! date of the death                        */
[16]130        cycle_t               tm_sleep;      /*! TODO ???  [AG]                           */
131        cycle_t               tm_wait;       /*! TODO ???  [AG]                           */
132        cycle_t               tm_usr;        /*! user execution duration                  */
133        cycle_t               tm_sys;        /*! system execution duration                */
[1]134}
135thread_info_t;
136
137/***************************************************************************************
138 * This structure defines a thread descriptor.
139 * It is used for both the user threads and the kernel threads.
140 * In a process, an user thread is identified by a unique TRDID (thread identifier),
141 * that is returned by the kernel to the user:
142 * - The TRDID 16 LSB bits contain the LTID (Local Thread Index).
143 * - The TRDID 16 MSB bits contain the owner cluster CXY.
144 * - The LTID is used to index the th_tbl[] array in the local process descriptor.
145 * This TRDID is computed by the process_register_thread() function, when the user
146 * thread is registerd in the local copy of the process descriptor.
147 **************************************************************************************/
148
149#define THREAD_SIGNATURE    0xDEADBEEF
150
151typedef struct thread_s
152{
[16]153    void              * cpu_uzone;       /*! used for exception/interrupt/syscall     */
154        void              * cpu_context;     /*! used for context switch                  */
155        void              * fpu_context;     /*! used for dynamic FPU allocation          */
156
[1]157        uint32_t            trdid;           /*! thread index (in THTBL)                  */
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
165        uint32_t            local_locks;         /*! number of local locks owned by thread    */
166    list_entry_t        locks_root;      /*! root of local locks list                 */
167
168        uint32_t            remote_locks;        /*! number of local locks owned by thread    */
169    xlist_entry_t       xlocks_root;     /*! root of remote locks list                */
170
171        intptr_t            u_stack_base;    /*! user stack base address                  */
172        uint32_t            u_stack_size;    /*! user stack size (bytes)                  */
173        intptr_t            k_stack_base;    /*! kernel stack base address                */
174        uint32_t            k_stack_size;    /*! kernel stack size (bytes)                */
175
176    void              * entry_func;      /*! pointer on entry function                */
177    void              * entry_args;      /*! pointer on entry function arguments      */
178
179    uint32_t            flags;           /*! bit vector of flags                      */
180    uint32_t            blocked;         /*! bit vector of blocking causes            */
181    uint32_t            signals;         /*! bit vector of signals                    */
182
[16]183        error_t             errno;           /*! errno value set by last system call      */
[1]184
185    bool_t              fork_user;       /*! user defined placement for next fork()   */
186    cxy_t               fork_cxy;        /*! target cluster  for next fork()          */
187
188        xlist_entry_t       children_root;   /*! root of list of attached children        */
189    uint32_t            children_nr;     /*! number of attached children threads      */
190    remote_spinlock_t * children_lock;   /*! lock protecting the children list        */
191
192    xlist_entry_t       brothers_list;   /*! member of threads with same parent       */
193
194        list_entry_t        sched_list;      /*! member of threads attached to same core  */
195
196    uint32_t            dev_channel;     /*! device channel for a DEV thread          */
197    union                                /*! embedded command for a DEV thread        */
198    {
199        ioc_command_t   ioc;             /*! IOC device generic command               */
200        txt_command_t   txt;             /*! TXT device generic command               */
201        nic_command_t   nic;             /*! NIC device generic command               */
[5]202        mmc_command_t   mmc;             /*! MMC device generic command               */
203        dma_command_t   dma;             /*! DMA device generic command               */
[1]204    } 
[5]205    command;
[1]206   
207        cxy_t               rpc_client_cxy;  /*! client cluster index (for a RPC thread)  */
208
209    xlist_entry_t       wait_list;       /*! member of threads blocked on same cond   */
210
211        thread_info_t       info;            /*! embedded thread_info_t                   */
212
213        uint32_t            signature;       /*! for kernel stack overflow detection      */
214} 
215thread_t;
216
217/***************************************************************************************
218 * This macro returns a pointer on the calling thread from the core hardware register.
219 **************************************************************************************/
220
221#define CURRENT_THREAD  (hal_get_current_thread())
222
223/***************************************************************************************
[16]224 * This function returns a printable string for a thread type.
225 ***************************************************************************************
226 * @ type    : thread type.
227 * returns pointer on string.
228 **************************************************************************************/
229char * thread_type_str( uint32_t type );
230
231/***************************************************************************************
[1]232 * This function allocates memory for an user thread descriptor in the local cluster,
233 * and initializes it from information contained in the "attr" argument.
234 * It is used by the pthread_create system call, the CPU context is initialised from
235 * scratch, and the "loadable" field is set.
236 * The new thread is attached to the core specified in the "attr" argument.
237 * It is registered in the local process descriptor for the process specified in "attr".
238 * The thread descriptor pointer is returned to allow the parent thread to register it
239 * in its children list.
240 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
241 ***************************************************************************************
242 * @ new_thread   : address of buffer for new thread descriptor pointer. 
243 * @ attr         : pointer on pthread attributes descriptor.
244 * @ u_stack_base : actual user stack size.
245 * @ u_stack_size : actual user stack base.
246 * @ returns 0 if success / returns ENOMEM if error.
247 **************************************************************************************/
248error_t thread_user_create( thread_t       ** new_thread,
249                            pthread_attr_t  * attr,
250                            intptr_t          u_stack_base,
251                            uint32_t          u_stack_size );
252
253/***************************************************************************************
254 * This function allocates memory for an user thread descriptor in the local cluster,
255 * and initialises it from informations contained in the calling thread descriptor.
256 * It is used by the fork() system call to create the child process main thread.
257 * The new thread is attached to the core with the lowest load.
258 * It is registered in the process descriptor defined by the "process" argument.
259 * This new thread inherits its execution context from the calling thread,
260 * and the "loadable" field is NOT set.
261 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
262 ***************************************************************************************
263 * @ new_thread   : address of buffer for new thread descriptor pointer. 
264 * @ process      : local pointer on owner process descriptor.
265 * @ u_stack_base : actual user stack size.
266 * @ u_stack_size : actual user stack base.
267 * @ returns 0 if success / returns ENOMEM if error.
268 **************************************************************************************/
269error_t thread_user_fork( thread_t ** new_thread,
270                          process_t * process,
271                          intptr_t    u_stack_base,
272                          uint32_t    u_stack_size );
273
274/***************************************************************************************
275 * This function allocates memory for a kernel thread descriptor in the local cluster,
[14]276 * and initialise it from arguments values, calling the thread_kernel_init() function,
277 * that also allocates and initializes the CPU context.
[1]278 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
279 ***************************************************************************************
280 * @ new_thread   : address of buffer for new thread pointer.
281 * @ type         : kernel thread type.
282 * @ func         : pointer on function.
283 * @ args         : function arguments.
284 * @ core_lid     : local core index.
285 * @ returns 0 if success / returns ENOMEM if error
286 **************************************************************************************/
287error_t thread_kernel_create( thread_t     ** new_thread,
288                              thread_type_t   type,
289                              void          * func, 
290                              void          * args,
291                              lid_t           core_lid );
292
293/***************************************************************************************
[14]294 * This function initialises an existing kernel thread descriptor from arguments values.
295 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
296 ***************************************************************************************
297 * @ thread   : pointer on existing thread descriptor.
298 * @ type     : kernel thread type.
299 * @ func     : pointer on function.
300 * @ args     : function arguments.
301 * @ core_lid : local core index.
302 * @ returns 0 if success / returns EINVAL if error
303 **************************************************************************************/
304error_t thread_kernel_init( thread_t      * thread,
305                            thread_type_t   type,
306                            void          * func, 
307                            void          * args,
308                            lid_t           core_lid );
309
310/***************************************************************************************
[1]311 * This function releases the physical memory allocated for a thread descriptor
312 * in the local cluster. It can be used for both an user and a kernel thread.
313 * The physical memory dynamically allocated in the HEAP or MMAP zones by an user
314 * thread will be released when the process is killed, and the page table flushed.
315 ***************************************************************************************
316 * @ thread  : pointer on the thread descriptor to release. 
317 **************************************************************************************/
318void thread_destroy( thread_t * thread );
319
320/***************************************************************************************
[14]321 * This function defines the code of the thread executed by all cores after kernel_init,
322 * or when no other thread is runnable for a given core.
323 *
324 * TODO: In the TSAR architecture, it enters an infinite loop, in wich it forces
325 * the core in sleep (low-power) mode. Any IRQ will force the core to exit this sleep
326 * mode, but no ISR is executed.
327 * TODO: We must analyse if we have the same behaviour for I86 architectures...
[1]328 **************************************************************************************/
[14]329void thread_idle_func();
[1]330
331/***************************************************************************************
332 * This function registers a child thread in the global list of attached
333 * children threads of a parent thread.
334 * It does NOT take a lock, as this function is allways called by the parent thread.
335 ***************************************************************************************
336 * @ xp_parent : extended pointer on the parent thread descriptor.
337 * @ xp_child  : extended pointer on the child thread descriptor.
338 **************************************************************************************/
339void thread_child_parent_link( xptr_t  xp_parent,
340                               xptr_t  xp_child );
341
342/***************************************************************************************
343 * This function removes an user thread from the parent thread global list
344 * of attached children threads.
345 * It does take the lock, as this function can be called by the child thread.
346 * In this case, it uses the extended pointer on the parent thread contained
347 * in the pthread_attr_t embedded in the child thread descriptor.
348 ***************************************************************************************
349 * @ xp_parent : extended pointer on the parent thread descriptor.
350 * @ xp_child  : extended pointer on the child thread descriptor.
351 **************************************************************************************/
352void thread_child_parent_unlink( xptr_t xp_parent,
353                                 xptr_t xp_child );
354
355
356/***************************************************************************************
357 * This function atomically set a signal in a thread descriptor.
358 ***************************************************************************************
359 * @ thread    : local pointer on target thread.
360 * @ mask      : mask on selected signal.
361 **************************************************************************************/
362inline void thread_set_signal( thread_t * thread,
363                               uint32_t   mask );
364
365/***************************************************************************************
366 * This function reset a signal in a thread descriptor.
367 ***************************************************************************************
368 * @ thread    : local pointer on target thread.
369 * @ mask      : mask on selected signal.
370 **************************************************************************************/
371inline void thread_reset_signal( thread_t * thread,
372                                 uint32_t   mask );
373
374/***************************************************************************************
375 * This function returns true if the calling thread is attached to its parent thread.
376 **************************************************************************************/
377inline bool_t thread_is_joinable();
378
379/***************************************************************************************
380 * This function returns true if the calling thread is not blocked.
381 **************************************************************************************/
382inline bool_t thread_is_runnable();
383
384/***************************************************************************************
385 * This function cheks if the calling thread can deschedule.
386 ***************************************************************************************
387 * @ returns true if no locks taken.
388 **************************************************************************************/
389inline bool_t thread_can_yield();
390
391/***************************************************************************************
392 * This function checks if the calling thread must be descheduled.
393 ***************************************************************************************
394 * @ returns true if no locks taken, and elapsed time.
395 **************************************************************************************/
396bool_t thread_check_sched();
397
398/***************************************************************************************
399 * This function can be used by the calling thread to suicide.
400 * All locks must be previouly released.
401 * The scenario depends on the attached/detached flag :
402 * - if detached, it set the SIG_KILL signal in the "signals" bit_vector, registers
403 *   the BLOCKED_EXIT bit in the "blocked" bit_vector, and deschedule.
404 * - if attached, it simply set the BLOCKED_EXIT bit in the "blocked" bit vector
405 *   and deschedule. The SIG_KILL signal will be set by the parent thread when
406 *   executing the pthread_join().
407 ***************************************************************************************
408 * @ returns 0 if success / returns EINVAL if locks_count is not zero.
409 **************************************************************************************/
410error_t thread_exit();
411
412/*************************************************************************************** 
413 * This function registers a blocking cause in the target thread "blocked" bit vector.
414 * Warning : this function does not deschedule the calling thread.
415 * The descheduling can be forced by a sched_yield().
416 ***************************************************************************************
417 * @ thread   : local pointer on target thread.
418 * @ cause    : mask defining the cause (one hot).
419 **************************************************************************************/
420void thread_block( thread_t * thread,
421                   uint32_t   cause );
422
423/*************************************************************************************** 
424 * This function reset the bit identified by the cause argument in the "blocked"
425 * bit vector of a remote thread descriptor.
426 * We need an extended pointer, because the client thread of an I/O operation on a
427 * given device is not in the same cluster as the associated device descriptor.
428 * Warning : this function does not reschedule the remote thread.
429 * The scheduling can be forced by sending an IPI to the core running the remote thread.
430 ***************************************************************************************
431 * @ thread   : extended pointer on the remote thread.
432 * @ cause    : mask defining the cause (one hot).
433 **************************************************************************************/
434void thread_unblock( xptr_t   thread,
435                     uint32_t cause );
436
437/*************************************************************************************** 
438 * This function kills a target thread, identified by its local pointer.
439 * It is generally called by the local process_destroy() function.
440 * - it forces the global blocked bit in target thread descriptor.
441 * - it set the SIG_KILL signal in target thread.
442 * - it send an IPI_SCHED_REQUEST to the target thread core.
443 ***************************************************************************************
444 * @ thread   : local pointer on the target thread.
445 * @ returns 0 if success / returns EINVAL if locks_count is not zero.
446 **************************************************************************************/
447void thread_kill( thread_t * thread );
448
[16]449/***************************************************************************************
450 * This function updates the calling thread user_time counter, and reset 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 reset 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 handle all pending signals for the thread identified by the <thread>
469 * argument. It is called each time the core exit 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
[1]479#endif  /* _THREAD_H_ */
Note: See TracBrowser for help on using the repository browser.