Ignore:
Timestamp:
Nov 7, 2017, 3:08:12 PM (7 years ago)
Author:
alain
Message:

First implementation of fork/exec.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/kern/thread.h

    r406 r407  
    2727
    2828#include <hal_types.h>
     29#include <shared_syscalls.h>
    2930#include <hal_special.h>
    3031#include <xlist.h>
     
    5152
    5253/***************************************************************************************
    53  * This defines the various pthread_attr_t attributes bit-vector.
    54  **************************************************************************************/
    55 
    56 /***************************************************************************************
    57  * This opaque structure contains the user defined attributes for a user thread.
    58  * It is passed as input argument to the thread_user_create() function.
    59  * It is set by the user application itself, using the pthread_attr_***() functions.
    60  * The currently supported attributes are defined below.
    61  **************************************************************************************/
    62 
    63 typedef struct pthread_attr_s
    64 {
    65         uint32_t    attributes;      /*! user defined attributes bit vector               */
    66         cxy_t       cxy;             /*! target cluster identifier                        */
    67         lid_t       lid;             /*! target core index                                */
    68 }
    69 pthread_attr_t;
    70 
    71 typedef enum
    72 {
    73     PT_ATTR_DETACH          = 0x0001,  /*! user defined not joinable                  */
    74     PT_ATTR_CLUSTER_DEFINED = 0x0002,  /*! user defined target cluster                */
    75     PT_ATTR_CORE_DEFINED    = 0x0004,  /*! user defined core index in cluster         */
    76 }
    77 pt_attributes_t;
    78 
    79 /***************************************************************************************
    8054 * This enum defines the thread types.
    8155 **************************************************************************************/
     
    8660        THREAD_RPC     = 1,          /*! kernel thread executing pending RPCs             */
    8761        THREAD_DEV     = 2,          /*! kernel thread executing I/O device commands      */
    88         THREAD_KERNEL  = 3,          /*! other kernel thread                              */
    89         THREAD_IDLE    = 4,          /*! kernel idle thread                               */
    90         THREAD_TYPES_NR
     62        THREAD_IDLE    = 3,          /*! kernel idle thread                               */
    9163}
    9264thread_type_t;
     
    10072#define THREAD_FLAG_JOIN         0x0004  /*! Parent thread made a join                */
    10173#define THREAD_FLAG_EXIT         0x0008  /*! This thread made an exit                 */
    102 #define THREAD_FLAG_SCHED        0x0010  /*! Descheduling required for this thread    */
     74#define THREAD_FLAG_SCHED        0x0010  /*! Scheduling required for this thread      */
    10375
    10476/***************************************************************************************
     
    10678 **************************************************************************************/
    10779
    108 #define THREAD_SIG_KILL          0x0001  /*! This thread must be destroyed ASAP       */
     80#define THREAD_SIG_KILL          0x0001  /*! This thread killed by another thread     */
     81#define THREAD_SIG_SUICIDE       0x0002  /*! This thread required exit                */
    10982
    11083/***************************************************************************************
     
    11689#define THREAD_BLOCKED_MAPPER    0x0004  /*! thread wait mapper                       */
    11790#define THREAD_BLOCKED_JOIN      0x0008  /*! thread blocked in join / wait exit       */
    118 #define THREAD_BLOCKED_EXIT      0x0010  /*! thread blocked in exit / wait join i     */
     91#define THREAD_BLOCKED_EXIT      0x0010  /*! thread blocked in exit / wait join       */
    11992#define THREAD_BLOCKED_KILL      0x0020  /*! thread received kill signal              */
    12093#define THREAD_BLOCKED_SEM       0x0040  /*! thread wait semaphore                    */
    12194#define THREAD_BLOCKED_PAGE      0x0080  /*! thread wait page access                  */
    12295#define THREAD_BLOCKED_USERSYNC  0x0100  /*! thread wait POSIX (cond/mutex/barrier)   */
    123 
    124 #define THREAD_BLOCKED_IDLE      0x1000  /*! thread RPC wait activation               */
     96#define THREAD_BLOCKED_RPC       0x0200  /*! thread wait RPC completion               */
     97
    12598#define THREAD_BLOCKED_DEV_QUEUE 0x2000  /*! thread DEV wait queue                    */
    12699#define THREAD_BLOCKED_DEV_ISR   0x4000  /*! thread DEV wait ISR                      */
     
    199172
    200173    uint32_t            flags;           /*! bit vector of flags                      */
    201     uint32_t            blocked;         /*! bit vector of blocking causes            */
    202     uint32_t            signals;         /*! bit vector of signals                    */
     174    volatile uint32_t   blocked;         /*! bit vector of blocking causes            */
     175    volatile uint32_t   signals;         /*! bit vector of (KILL / SUICIDE) signals   */
    203176
    204177        error_t             errno;           /*! errno value set by last system call      */
     
    212185    remote_spinlock_t * children_lock;   /*! lock protecting the children list        */
    213186
    214     xlist_entry_t       brothers_list;   /*! member of threads with same parent       */
     187    xlist_entry_t       brothers_list;   /*! list of attached threads to same parent  */
    215188
    216189        list_entry_t        sched_list;      /*! member of threads attached to same core  */
     
    273246
    274247/***************************************************************************************
    275  * This function allocates memory for an user thread descriptor in the local cluster,
     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,
    276250 * and initializes it from information contained in the calling thread descriptor.
    277  * It is used by the fork() system call to create the child process main thread.
    278  * The new thread is attached to the core with the lowest load.
    279  * It is registered in the process descriptor defined by the "process" argument.
    280  * This new thread inherits its execution context from the calling thread,
    281  * and the "loadable" field is NOT set.
    282  * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
     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.
    283261 ***************************************************************************************
    284262 * @ process      : local pointer on owner process descriptor.
     263 * @ stack_base   : user stack base address (from parent).
     264 * @ stack_size   : user stack size (from parent).
    285265 * @ new_thread   : [out] address of buffer for new thread descriptor pointer.
    286266 * @ returns 0 if success / returns ENOMEM if error.
    287267 **************************************************************************************/
    288268error_t thread_user_fork( process_t * process,
     269                          intptr_t    stack_base,
     270                          uint32_t    stack_size,
    289271                          thread_t ** new_thread );
    290272
     
    351333 * It does NOT take a lock, as this function is always called by the parent thread.
    352334 ***************************************************************************************
    353  * @ xp_parent : extended pointer on the parent thread descriptor.
    354  * @ xp_child  : extended pointer on the child thread descriptor.
    355  **************************************************************************************/
    356 void thread_child_parent_link( xptr_t  xp_parent,
    357                                xptr_t  xp_child );
     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 );
    358340
    359341/***************************************************************************************
     
    361343 * of attached children threads.
    362344 ***************************************************************************************
    363  * @ xp_parent : extended pointer on the parent thread descriptor.
    364  * @ xp_child  : extended pointer on the child thread descriptor.
    365  **************************************************************************************/
    366 void thread_child_parent_unlink( xptr_t xp_parent,
    367                                  xptr_t xp_child );
     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 );
    368350
    369351/***************************************************************************************
     
    386368
    387369/***************************************************************************************
    388  * This function returns true if the calling thread is attached to its parent thread.
    389  **************************************************************************************/
    390 inline bool_t thread_is_joinable();
    391 
    392 /***************************************************************************************
    393  * This function returns true if the calling thread is not blocked.
    394  **************************************************************************************/
    395 inline bool_t thread_is_runnable();
    396 
    397 /***************************************************************************************
    398370 * This function checks if the calling thread can deschedule.
    399371 ***************************************************************************************
     
    403375
    404376/***************************************************************************************
    405  * This function implements the delayed descheduling machanism : It is called  by
     377 * This function implements the delayed descheduling mechanism : It is called  by
    406378 * all lock release functions, and calls the sched_yield() function when all locks
    407  * have beeen released and the THREAD_FLAG_SCHED flag is set.
     379 * have beeen released and the calling thread THREAD_FLAG_SCHED flag is set.
    408380 **************************************************************************************/
    409381void thread_check_sched();
    410382
    411383/***************************************************************************************
    412  * This function can be used by the calling thread to suicide.
    413  * All locks must be previously released.
    414  * The scenario depends on the attached/detached flag :
    415  * - if detached, it sets the SIG_KILL signal in the "signals" bit_vector, registers
    416  *   the BLOCKED_EXIT bit in the "blocked" bit_vector, and deschedule.
    417  * - if attached, it simply sets the BLOCKED_EXIT bit in the "blocked" bit vector
    418  *   and deschedule. The SIG_KILL signal will be set by the parent thread when
    419  *   executing the pthread_join().
     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.
    420397 ***************************************************************************************
    421398 * @ returns 0 if success / returns EINVAL if locks_count is not zero.
     
    424401
    425402/***************************************************************************************
     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/***************************************************************************************
    426421 * This function registers a blocking cause in the target thread "blocked" bit vector.
    427  * Warning : this function does not deschedule the calling thread.
    428  * The descheduling can be forced by a sched_yield().
    429  ***************************************************************************************
    430  * @ thread   : local pointer on target thread.
     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.
    431426 * @ cause    : mask defining the cause (one hot).
    432427 **************************************************************************************/
     
    436431/***************************************************************************************
    437432 * This function resets the bit identified by the cause argument in the "blocked"
    438  * bit vector of a remote thread descriptor.
     433 * bit vector of a remote thread descriptor, using an atomic access.
    439434 * We need an extended pointer, because the client thread of an I/O operation on a
    440435 * given device is not in the same cluster as the associated device descriptor.
     
    444439 * @ thread   : extended pointer on the remote thread.
    445440 * @ cause    : mask defining the cause (one hot).
    446  **************************************************************************************/
    447 void thread_unblock( xptr_t   thread,
    448                      uint32_t cause );
    449 
    450 /***************************************************************************************
    451  * This function kills a target thread, identified by its local pointer.
    452  * It is generally called by the local process_destroy() function.
    453  * - it forces the global blocked bit in target thread descriptor.
    454  * - it set the SIG_KILL signal in target thread.
    455  * - it send an IPI_SCHED_REQUEST to the target thread core.
    456  ***************************************************************************************
    457  * @ thread   : local pointer on the target thread.
    458  * @ returns 0 if success / returns EINVAL if locks_count is not zero.
    459  **************************************************************************************/
    460 void thread_kill( thread_t * thread );
     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 );
    461445
    462446/***************************************************************************************
Note: See TracChangeset for help on using the changeset viewer.