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

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

First import

File size: 23.1 KB
Line 
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>
42
43/***************************************************************************************
44 * This defines the various pthread_attr_t flags.
45 **************************************************************************************/
46
47#define PT_FLAG_DETACH                0x001    // user defined not joinable
48#define PT_FLAG_CLUSTER_DEFINED       0x002    // user defined target cluster
49#define PT_FLAG_CORE_DEFINED          0x004    // user defined core cluster
50
51/***************************************************************************************
52 * This structure defines the input argument of the pthread_create() system call.
53 * It contains all informations required to initialize an user thread, and is passed
54 * as input argument to the thread_user_create() function.
55 * It is partly set by the kernel, partly set by the user application itself,
56 * using the pthread_attr_***() functions.
57 **************************************************************************************/
58 
59typedef struct pthread_attr_s
60{
61    pid_t       pid;             /*! owner process identifier                         */
62        void      * entry_func;      /*! pointer on entry function                        */
63        void      * entry_args;      /*! pointer on entry function arguments              */
64        uint32_t    flags;           /*! pthread flags        (entirely user specified)   */
65        cxy_t       cxy;             /*! target cluster       (can be user specified)     */
66        lid_t       lid;             /*! target core          (can be user specified)     */
67} 
68pthread_attr_t;
69
70
71/***************************************************************************************
72 * This enum defines the thread types.
73 **************************************************************************************/
74
75typedef enum
76{   
77        THREAD_USER    = 0,          /*! user thread (pthread)                            */ 
78        THREAD_RPC     = 1,          /*! kernel thread executing pending RPCs             */
79        THREAD_DEV     = 2,          /*! kernel thread executing I/O device commands      */
80        THREAD_KERNEL  = 3,          /*! other kernel thread                              */
81        THREAD_IDLE    = 4,          /*! kernel idle thread                               */
82        THREAD_TYPES_NR
83} 
84thread_type_t;
85
86/***************************************************************************************
87 * This defines the masks associated to the thread flags.
88 **************************************************************************************/
89
90#define THREAD_FLAG_LOADABLE     0x0001  /*! This thread has not been executed yet    */
91#define THREAD_FLAG_DETACHED     0x0002  /*! This user thread is detached from parent */
92
93/***************************************************************************************
94 * This defines the masks associated to the thread signals.
95 **************************************************************************************/
96
97#define THREAD_SIG_KILL          0x0001  /*! This thread must be destroyed ASAP       */
98
99/***************************************************************************************
100 * This defines the masks associated to the blocking causes.
101 **************************************************************************************/
102
103#define THREAD_BLOCKED_GLOBAL    0x0001  /*! thread global blocking                   */ 
104#define THREAD_BLOCKED_IO        0x0002  /*! thread wait IO operation completion      */
105#define THREAD_BLOCKED_MAPPER    0x0004  /*! thread wait mapper                       */
106#define THREAD_BLOCKED_EXIT      0x0010  /*! thread requested exit                    */
107#define THREAD_BLOCKED_KILL      0x0020  /*! thread received kill signal              */
108#define THREAD_BLOCKED_SEM       0x0040  /*! thread wait semaphore                    */
109#define THREAD_BLOCKED_PAGE      0x0080  /*! thread wait page access                  */
110#define THREAD_BLOCKED_IDLE      0x1000  /*! thread RPC wait activation               */
111#define THREAD_BLOCKED_DEV_QUEUE 0x2000  /*! thread DEV wait queue                    */
112#define THREAD_BLOCKED_DEV_ISR   0x4000  /*! thread DEV wait ISR                      */
113
114/***************************************************************************************
115 * This structure defines thread instrumentation informations.
116 **************************************************************************************/
117
118typedef struct thread_info_s
119{
120        uint32_t              pgfault_nr;    /*! cumulated number of page fault           */
121        uint32_t              sched_nr;      /*! TODO ???  [AG]                           */
122        uint32_t              u_err_nr;      /*! TODO ???  [AG]                           */
123        uint32_t              m_err_nr;      /*! TODO ???  [AG]                           */
124        uint32_t              tm_tmp;        /*! temp date to compute execution duration  */
125        uint32_t              tm_exec;       /*! TODO ???  [AG]                           */
126        uint32_t              tm_create;     /*! date of the creation                     */
127        uint32_t              tm_born;       /*! date of the thread loading               */
128        uint32_t              tm_dead;       /*! date of the death                        */
129        clock_t               tm_sleep;      /*! TODO ???  [AG]                           */
130        clock_t               tm_wait;       /*! TODO ???  [AG]                           */
131        clock_t               tm_usr;        /*! user execution duration                  */
132        clock_t               tm_sys;        /*! system execution duration                */
133}
134thread_info_t;
135
136/***************************************************************************************
137 * This structure defines a thread descriptor.
138 * It is used for both the user threads and the kernel threads.
139 * In a process, an user thread is identified by a unique TRDID (thread identifier),
140 * that is returned by the kernel to the user:
141 * - The TRDID 16 LSB bits contain the LTID (Local Thread Index).
142 * - The TRDID 16 MSB bits contain the owner cluster CXY.
143 * - The LTID is used to index the th_tbl[] array in the local process descriptor.
144 * This TRDID is computed by the process_register_thread() function, when the user
145 * thread is registerd in the local copy of the process descriptor.
146 **************************************************************************************/
147
148#define THREAD_SIGNATURE    0xDEADBEEF
149
150typedef struct thread_s
151{
152        uint32_t            trdid;           /*! thread index (in THTBL)                  */
153        thread_type_t       type;            /*! thread type                              */
154        uint32_t            quantum;         /*! number of clock ticks given to thread    */
155        uint32_t            ticks_nr;        /*! number of ticks used                     */
156        uint32_t            time_last_check; /*! last cpu_time_stamp                      */
157        core_t            * core;            /*! pointer to the owner core                */
158        process_t         * process;         /*! pointer on local process descriptor      */
159        page_t            * page;            /*! pointer on page desc. containing thread  */ 
160
161        uint32_t            local_locks;         /*! number of local locks owned by thread    */
162    list_entry_t        locks_root;      /*! root of local locks list                 */
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        intptr_t            k_stack_base;    /*! kernel stack base address                */
170        uint32_t            k_stack_size;    /*! kernel stack size (bytes)                */
171
172    void              * entry_func;      /*! pointer on entry function                */
173    void              * entry_args;      /*! pointer on entry function arguments      */
174
175    uint32_t            flags;           /*! bit vector of flags                      */
176    uint32_t            blocked;         /*! bit vector of blocking causes            */
177    uint32_t            signals;         /*! bit vector of signals                    */
178
179        error_t             errno;           /*! errno value of last system call          */
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;   /*! member of threads with same parent       */
189
190        list_entry_t        sched_list;      /*! member of threads attached to same core  */
191
192    uint32_t            dev_channel;     /*! device channel for a DEV thread          */
193    union                                /*! embedded command for a DEV thread        */
194    {
195        ioc_command_t   ioc;             /*! IOC device generic command               */
196        txt_command_t   txt;             /*! TXT device generic command               */
197        nic_command_t   nic;             /*! NIC device generic command               */
198        mmc_command_t   mmc;             /*! NIC device generic command               */
199    } 
200    dev;
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        void              * fpu_context;     /*! pointer on FPU context                   */
207        void              * cpu_context;     /*! pointer on CPU context                   */
208
209        thread_info_t       info;            /*! embedded thread_info_t                   */
210
211        uint32_t            signature;       /*! for kernel stack overflow detection      */
212} 
213thread_t;
214
215/***************************************************************************************
216 * This macro returns a pointer on the calling thread from the core hardware register.
217 **************************************************************************************/
218
219#define CURRENT_THREAD  (hal_get_current_thread())
220
221/***************************************************************************************
222 * This function allocates memory for an user thread descriptor in the local cluster,
223 * and initializes it from information contained in the "attr" argument.
224 * It is used by the pthread_create system call, the CPU context is initialised from
225 * scratch, and the "loadable" field is set.
226 * The new thread is attached to the core specified in the "attr" argument.
227 * It is registered in the local process descriptor for the process specified in "attr".
228 * The thread descriptor pointer is returned to allow the parent thread to register it
229 * in its children list.
230 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
231 ***************************************************************************************
232 * @ new_thread   : address of buffer for new thread descriptor pointer. 
233 * @ attr         : pointer on pthread attributes descriptor.
234 * @ u_stack_base : actual user stack size.
235 * @ u_stack_size : actual user stack base.
236 * @ returns 0 if success / returns ENOMEM if error.
237 **************************************************************************************/
238error_t thread_user_create( thread_t       ** new_thread,
239                            pthread_attr_t  * attr,
240                            intptr_t          u_stack_base,
241                            uint32_t          u_stack_size );
242
243/***************************************************************************************
244 * This function allocates memory for an user thread descriptor in the local cluster,
245 * and initialises it from informations contained in the calling thread descriptor.
246 * It is used by the fork() system call to create the child process main thread.
247 * The new thread is attached to the core with the lowest load.
248 * It is registered in the process descriptor defined by the "process" argument.
249 * This new thread inherits its execution context from the calling thread,
250 * and the "loadable" field is NOT set.
251 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
252 ***************************************************************************************
253 * @ new_thread   : address of buffer for new thread descriptor pointer. 
254 * @ process      : local pointer on owner process descriptor.
255 * @ u_stack_base : actual user stack size.
256 * @ u_stack_size : actual user stack base.
257 * @ returns 0 if success / returns ENOMEM if error.
258 **************************************************************************************/
259error_t thread_user_fork( thread_t ** new_thread,
260                          process_t * process,
261                          intptr_t    u_stack_base,
262                          uint32_t    u_stack_size );
263
264/***************************************************************************************
265 * This function allocates memory for a kernel thread descriptor in the local cluster,
266 * and initialise it from arguments values.
267 * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
268 ***************************************************************************************
269 * @ new_thread   : address of buffer for new thread pointer.
270 * @ type         : kernel thread type.
271 * @ func         : pointer on function.
272 * @ args         : function arguments.
273 * @ core_lid     : local core index.
274 * @ returns 0 if success / returns ENOMEM if error
275 **************************************************************************************/
276error_t thread_kernel_create( thread_t     ** new_thread,
277                              thread_type_t   type,
278                              void          * func, 
279                              void          * args,
280                              lid_t           core_lid );
281
282/***************************************************************************************
283 * This function releases the physical memory allocated for a thread descriptor
284 * in the local cluster. It can be used for both an user and a kernel thread.
285 * The physical memory dynamically allocated in the HEAP or MMAP zones by an user
286 * thread will be released when the process is killed, and the page table flushed.
287 ***************************************************************************************
288 * @ thread  : pointer on the thread descriptor to release. 
289 **************************************************************************************/
290void thread_destroy( thread_t * thread );
291
292/***************************************************************************************
293 * This function defines the code of the thread executed when no other thread is
294 * runnable for a given core. It try to force the core to the low-power state.
295 **************************************************************************************/
296void * thread_idle_func();
297
298/***************************************************************************************
299 * This function registers a child thread in the global list of attached
300 * children threads of a parent thread.
301 * It does NOT take a lock, as this function is allways called by the parent thread.
302 ***************************************************************************************
303 * @ xp_parent : extended pointer on the parent thread descriptor.
304 * @ xp_child  : extended pointer on the child thread descriptor.
305 **************************************************************************************/
306void thread_child_parent_link( xptr_t  xp_parent,
307                               xptr_t  xp_child );
308
309/***************************************************************************************
310 * This function removes an user thread from the parent thread global list
311 * of attached children threads.
312 * It does take the lock, as this function can be called by the child thread.
313 * In this case, it uses the extended pointer on the parent thread contained
314 * in the pthread_attr_t embedded in the child thread descriptor.
315 ***************************************************************************************
316 * @ xp_parent : extended pointer on the parent thread descriptor.
317 * @ xp_child  : extended pointer on the child thread descriptor.
318 **************************************************************************************/
319void thread_child_parent_unlink( xptr_t xp_parent,
320                                 xptr_t xp_child );
321
322
323/***************************************************************************************
324 * This function atomically set a signal in a thread descriptor.
325 ***************************************************************************************
326 * @ thread    : local pointer on target thread.
327 * @ mask      : mask on selected signal.
328 **************************************************************************************/
329inline void thread_set_signal( thread_t * thread,
330                               uint32_t   mask );
331
332/***************************************************************************************
333 * This function reset a signal in a thread descriptor.
334 ***************************************************************************************
335 * @ thread    : local pointer on target thread.
336 * @ mask      : mask on selected signal.
337 **************************************************************************************/
338inline void thread_reset_signal( thread_t * thread,
339                                 uint32_t   mask );
340
341/***************************************************************************************
342 * This function returns true if the calling thread is attached to its parent thread.
343 **************************************************************************************/
344inline bool_t thread_is_joinable();
345
346/***************************************************************************************
347 * This function returns true if the calling thread is not blocked.
348 **************************************************************************************/
349inline bool_t thread_is_runnable();
350
351/***************************************************************************************
352 * This function cheks if the calling thread can deschedule.
353 ***************************************************************************************
354 * @ returns true if no locks taken.
355 **************************************************************************************/
356inline bool_t thread_can_yield();
357
358/***************************************************************************************
359 * This function checks if the calling thread must be descheduled.
360 ***************************************************************************************
361 * @ returns true if no locks taken, and elapsed time.
362 **************************************************************************************/
363bool_t thread_check_sched();
364
365/***************************************************************************************
366 * This function can be used by the calling thread to suicide.
367 * All locks must be previouly released.
368 * The scenario depends on the attached/detached flag :
369 * - if detached, it set the SIG_KILL signal in the "signals" bit_vector, registers
370 *   the BLOCKED_EXIT bit in the "blocked" bit_vector, and deschedule.
371 * - if attached, it simply set the BLOCKED_EXIT bit in the "blocked" bit vector
372 *   and deschedule. The SIG_KILL signal will be set by the parent thread when
373 *   executing the pthread_join().
374 ***************************************************************************************
375 * @ returns 0 if success / returns EINVAL if locks_count is not zero.
376 **************************************************************************************/
377error_t thread_exit();
378
379/*************************************************************************************** 
380 * This function registers a blocking cause in the target thread "blocked" bit vector.
381 * Warning : this function does not deschedule the calling thread.
382 * The descheduling can be forced by a sched_yield().
383 ***************************************************************************************
384 * @ thread   : local pointer on target thread.
385 * @ cause    : mask defining the cause (one hot).
386 **************************************************************************************/
387void thread_block( thread_t * thread,
388                   uint32_t   cause );
389
390/*************************************************************************************** 
391 * This function reset the bit identified by the cause argument in the "blocked"
392 * bit vector of a remote thread descriptor.
393 * We need an extended pointer, because the client thread of an I/O operation on a
394 * given device is not in the same cluster as the associated device descriptor.
395 * Warning : this function does not reschedule the remote thread.
396 * The scheduling can be forced by sending an IPI to the core running the remote thread.
397 ***************************************************************************************
398 * @ thread   : extended pointer on the remote thread.
399 * @ cause    : mask defining the cause (one hot).
400 **************************************************************************************/
401void thread_unblock( xptr_t   thread,
402                     uint32_t cause );
403
404/*************************************************************************************** 
405 * This function kills a target thread, identified by its local pointer.
406 * It is generally called by the local process_destroy() function.
407 * - it forces the global blocked bit in target thread descriptor.
408 * - it set the SIG_KILL signal in target thread.
409 * - it send an IPI_SCHED_REQUEST to the target thread core.
410 ***************************************************************************************
411 * @ thread   : local pointer on the target thread.
412 * @ returns 0 if success / returns EINVAL if locks_count is not zero.
413 **************************************************************************************/
414void thread_kill( thread_t * thread );
415
416#endif  /* _THREAD_H_ */
Note: See TracBrowser for help on using the repository browser.