| 1 | /* | 
|---|
| 2 | * process.h - process related management functions | 
|---|
| 3 | * | 
|---|
| 4 | * Authors  Ghassan Almaless (2008,2009,2010,2011,2012) | 
|---|
| 5 | *          Mohamed Lamine Karaoui (2015) | 
|---|
| 6 | *          Alain Greiner (2016,2017) | 
|---|
| 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 _PROCESS_H_ | 
|---|
| 27 | #define _PROCESS_H_ | 
|---|
| 28 |  | 
|---|
| 29 | #include <kernel_config.h> | 
|---|
| 30 | #include <errno.h> | 
|---|
| 31 | #include <hal_types.h> | 
|---|
| 32 | #include <list.h> | 
|---|
| 33 | #include <xlist.h> | 
|---|
| 34 | #include <bits.h> | 
|---|
| 35 | #include <spinlock.h> | 
|---|
| 36 | #include <hal_atomic.h> | 
|---|
| 37 | #include <vmm.h> | 
|---|
| 38 | #include <signal.h> | 
|---|
| 39 | #include <cluster.h> | 
|---|
| 40 | #include <vfs.h> | 
|---|
| 41 |  | 
|---|
| 42 | /****  Forward declarations  ****/ | 
|---|
| 43 |  | 
|---|
| 44 | struct thread_s; | 
|---|
| 45 |  | 
|---|
| 46 | /********************************************************************************************* | 
|---|
| 47 | * These macros are used to compose or decompose global process identifier (PID) | 
|---|
| 48 | * to or from cluster identifier / local process index (CXY , LPID) | 
|---|
| 49 | ********************************************************************************************/ | 
|---|
| 50 |  | 
|---|
| 51 | #define LPID_FROM_PID( pid )       (lpid_t)(pid & 0x0000FFFF) | 
|---|
| 52 | #define CXY_FROM_PID( pid )        (cxy_t)(pid >> 16) | 
|---|
| 53 | #define PID( cxy , lpid )          (pid_t)((cxy << 16) | lpid ) | 
|---|
| 54 |  | 
|---|
| 55 | /********************************************************************************************* | 
|---|
| 56 | * This enum defines the actions that can be executed by the process_sigaction() function. | 
|---|
| 57 | ********************************************************************************************/ | 
|---|
| 58 |  | 
|---|
| 59 | enum process_sigactions | 
|---|
| 60 | { | 
|---|
| 61 | BLOCK_ALL_THREADS    = 11, | 
|---|
| 62 | UNBLOCK_ALL_THREADS  = 22, | 
|---|
| 63 | DELETE_ALL_THREADS   = 33, | 
|---|
| 64 | }; | 
|---|
| 65 |  | 
|---|
| 66 | /********************************************************************************************* | 
|---|
| 67 | * This enum defines the process states for ALMOS_MKH. | 
|---|
| 68 | ********************************************************************************************/ | 
|---|
| 69 |  | 
|---|
| 70 | enum process_states | 
|---|
| 71 | { | 
|---|
| 72 | PROCESS_STATE_RUNNING = 0,           /*! process is executing                           */ | 
|---|
| 73 | PROCESS_STATE_STOPPED = 1,           /*! process has been stopped by a signal           */ | 
|---|
| 74 | PROCESS_STATE_KILLED  = 2,           /*! process has been killed by a signal            */ | 
|---|
| 75 | PROCESS_STATE_EXITED  = 3,           /*! process terminated with an exit                */ | 
|---|
| 76 | }; | 
|---|
| 77 |  | 
|---|
| 78 | /********************************************************************************************* | 
|---|
| 79 | * This structure defines an array of extended pointers on the open file descriptors | 
|---|
| 80 | * for a given process. We use an extended pointer because the open file descriptor | 
|---|
| 81 | * is always stored in the same cluster as the inode associated to the file. | 
|---|
| 82 | * A free entry in this array contains the XPTR_NULL value. | 
|---|
| 83 | * The array size is defined by a the CONFIG_PROCESS_FILE_MAX_NR parameter. | 
|---|
| 84 | * All modifications (open/close) in this structure must be done by the reference cluster, | 
|---|
| 85 | * and reported in process copies. | 
|---|
| 86 | ********************************************************************************************/ | 
|---|
| 87 |  | 
|---|
| 88 | typedef struct fd_array_s | 
|---|
| 89 | { | 
|---|
| 90 | remote_spinlock_t lock;                               /*! lock protecting fd_array      */ | 
|---|
| 91 | uint32_t          current;                            /*! current number of open files  */ | 
|---|
| 92 | xptr_t            array[CONFIG_PROCESS_FILE_MAX_NR];  /*! xptr on open file descriptors */ | 
|---|
| 93 | } | 
|---|
| 94 | fd_array_t; | 
|---|
| 95 |  | 
|---|
| 96 | /********************************************************************************************* | 
|---|
| 97 | * This structure defines a process descriptor. | 
|---|
| 98 | * A process is identified by a unique PID (process identifier): | 
|---|
| 99 | * - The PID 16 LSB bits contain the LPID (Local Process Index) | 
|---|
| 100 | * - The PID 16 MSB bits contain the owner cluster CXY. | 
|---|
| 101 | * In each cluster, the process manager allocates  the LPID values for the process that | 
|---|
| 102 | * are owned by this cluster. | 
|---|
| 103 | * The process descriptor is replicated in all clusters containing at least one thread | 
|---|
| 104 | * of the PID process, with the following rules : | 
|---|
| 105 | * 1) The <pid>, <ppid>, <ref_xp>, <vfs_root_xp>, <vfs_bin_xp>  fields are defined | 
|---|
| 106 | *    in all process descriptor copies. | 
|---|
| 107 | * 2) The <vfs_cwd_xp> and associated <cwd_lock>, that can be dynamically modified, | 
|---|
| 108 | *    are only defined in the reference process descriptor. | 
|---|
| 109 | * 2) The <vmm>, containing the VSL (list of registered vsegs), and the GPT (generic | 
|---|
| 110 | *    page table), are only complete in the reference process cluster, other copies | 
|---|
| 111 | *    are actually use as read-only caches. | 
|---|
| 112 | * 3) the <fd_array>, containing extended pointers on the open file descriptors, is only | 
|---|
| 113 | *    complete in the reference process cluster, other copies are read-only caches. | 
|---|
| 114 | * 4) The <sem_root>, <mutex_root>, <barrier_root>, <condvar_root>, and the associated | 
|---|
| 115 | *    <sync_lock>, that are dynamically allocated, are only defined in the reference cluster. | 
|---|
| 116 | * 5) The <children_root>, <children_nr>, <brothers_list>, and <txt_list> fields are only | 
|---|
| 117 | *    defined in the reference cluster, and are undefined in other clusters. | 
|---|
| 118 | * 6) The <local_list>, <copies_list>, <th_tbl>, <th_nr>, <th_lock> fields | 
|---|
| 119 | *    are defined in all process descriptors copies. | 
|---|
| 120 | ********************************************************************************************/ | 
|---|
| 121 |  | 
|---|
| 122 | typedef struct process_s | 
|---|
| 123 | { | 
|---|
| 124 | vmm_t             vmm;              /*! embedded virtual memory manager                 */ | 
|---|
| 125 |  | 
|---|
| 126 | fd_array_t        fd_array;         /*! embedded open file descriptors array            */ | 
|---|
| 127 |  | 
|---|
| 128 | xptr_t            vfs_root_xp;      /*! extended pointer on current VFS root inode      */ | 
|---|
| 129 | xptr_t            vfs_bin_xp;       /*! extended pointer on .elf file descriptor        */ | 
|---|
| 130 | pid_t             pid;              /*! process identifier                              */ | 
|---|
| 131 | xptr_t            ref_xp;           /*! extended pointer on reference process           */ | 
|---|
| 132 | xptr_t            parent_xp;        /*! extended pointer on parent process              */ | 
|---|
| 133 |  | 
|---|
| 134 | xptr_t            vfs_cwd_xp;       /*! extended pointer on current working dir inode   */ | 
|---|
| 135 | remote_rwlock_t   cwd_lock;         /*! lock protecting working directory changes       */ | 
|---|
| 136 |  | 
|---|
| 137 | xlist_entry_t     children_root;    /*! root of the children process xlist              */ | 
|---|
| 138 | remote_spinlock_t children_lock;    /*! lock protecting children process xlist          */ | 
|---|
| 139 | uint32_t          children_nr;      /*! number of children processes                    */ | 
|---|
| 140 |  | 
|---|
| 141 | xlist_entry_t     children_list;    /*! member of list of children of same parent       */ | 
|---|
| 142 | xlist_entry_t     local_list;       /*! member of list of process in same cluster       */ | 
|---|
| 143 | xlist_entry_t     copies_list;      /*! member of list of copies of same process        */ | 
|---|
| 144 | xlist_entry_t     txt_list;         /*! member of list of processes sharing same TXT    */ | 
|---|
| 145 |  | 
|---|
| 146 | spinlock_t        th_lock;          /*! lock protecting th_tbl[] concurrent access      */ | 
|---|
| 147 | uint32_t          th_nr;            /*! number of threads in this cluster               */ | 
|---|
| 148 |  | 
|---|
| 149 | struct thread_s * th_tbl[CONFIG_THREAD_MAX_PER_CLUSTER]; /*! pointers on local threads  */ | 
|---|
| 150 |  | 
|---|
| 151 | xlist_entry_t     sem_root;         /*! root of the process semaphore list              */ | 
|---|
| 152 | xlist_entry_t     mutex_root;       /*! root of the process mutex list                  */ | 
|---|
| 153 | xlist_entry_t     barrier_root;     /*! root of the process barrier list                */ | 
|---|
| 154 | xlist_entry_t     condvar_root;     /*! root of the process condvar list                */ | 
|---|
| 155 | remote_spinlock_t sync_lock;        /*! lock protecting sem,mutex,barrier,condvar lists */ | 
|---|
| 156 |  | 
|---|
| 157 | uint32_t          state;            /*! RUNNING / STOPPED / KILLED / EXITED             */ | 
|---|
| 158 |  | 
|---|
| 159 | bool_t            txt_owner;        /*! current TXT owner                               */ | 
|---|
| 160 | } | 
|---|
| 161 | process_t; | 
|---|
| 162 |  | 
|---|
| 163 | /********************************************************************************************* | 
|---|
| 164 | * This structure defines the information required by the process_make_exec() function | 
|---|
| 165 | * to create a new reference process descriptor, and the associated main thread. | 
|---|
| 166 | ********************************************************************************************/ | 
|---|
| 167 |  | 
|---|
| 168 | typedef struct exec_info_s | 
|---|
| 169 | { | 
|---|
| 170 | pid_t              pid;            /*! process identifier (both parent and child)       */ | 
|---|
| 171 |  | 
|---|
| 172 | char               path[CONFIG_VFS_MAX_PATH_LENGTH];   /*!  .elf file path              */ | 
|---|
| 173 |  | 
|---|
| 174 | char            ** args_pointers;  /*! physical base address of array of pointers       */ | 
|---|
| 175 | char             * args_buf_base;  /*! physical base address of kernel args buffer      */ | 
|---|
| 176 | uint32_t           args_nr;        /*! actual number of arguments                       */ | 
|---|
| 177 |  | 
|---|
| 178 | char            ** envs_pointers;  /*! physical base address of array of pointers       */ | 
|---|
| 179 | char             * envs_buf_base;  /*! physical base address of kernel args buffer      */ | 
|---|
| 180 | char             * envs_buf_free;  /*! physical address of first free slot in envs_buf  */ | 
|---|
| 181 | uint32_t           envs_nr;        /*! actual number of environment variables           */ | 
|---|
| 182 | } | 
|---|
| 183 | exec_info_t; | 
|---|
| 184 |  | 
|---|
| 185 | /***************   Process Descriptor Operations    *****************************************/ | 
|---|
| 186 |  | 
|---|
| 187 | /********************************************************************************************* | 
|---|
| 188 | * This function allocates memory in local cluster for a process descriptor. | 
|---|
| 189 | ********************************************************************************************* | 
|---|
| 190 | * @ returns pointer on process descriptor if success / return NULL if failure | 
|---|
| 191 | ********************************************************************************************/ | 
|---|
| 192 | process_t * process_alloc(); | 
|---|
| 193 |  | 
|---|
| 194 | /********************************************************************************************* | 
|---|
| 195 | * This function releases memory in local cluster for a process descriptor. | 
|---|
| 196 | ********************************************************************************************* | 
|---|
| 197 | * @ process      : pointer on process descriptor to release. | 
|---|
| 198 | ********************************************************************************************/ | 
|---|
| 199 | void process_free( process_t * process ); | 
|---|
| 200 |  | 
|---|
| 201 | /********************************************************************************************* | 
|---|
| 202 | * This function initialize, in each cluster, the kernel "process_zero", that is the owner | 
|---|
| 203 | * of all kernel threads in a given cluster. It is called by the kernel_init() function. | 
|---|
| 204 | * The process_zero descriptor is allocated as a global variable in file kernel_init.c | 
|---|
| 205 | * Both the PID and PPID fields are set to zero, the ref_xp is the local process_zero, | 
|---|
| 206 | * and the parent process is set to XPTR_NULL. The th_tbl[] is initialized as empty. | 
|---|
| 207 | ********************************************************************************************* | 
|---|
| 208 | * @ process      : [in] pointer on local process descriptor to initialize. | 
|---|
| 209 | ********************************************************************************************/ | 
|---|
| 210 | void process_zero_create( process_t * process ); | 
|---|
| 211 |  | 
|---|
| 212 | /********************************************************************************************* | 
|---|
| 213 | * This function allocates memory and initializes the "process_init" descriptor and the | 
|---|
| 214 | * associated "thread_init" descriptor. It is called once at the end of the kernel | 
|---|
| 215 | * initialisation procedure. Its local process identifier is 1, and parent process | 
|---|
| 216 | * is the kernel process in cluster 0. | 
|---|
| 217 | * The "process_init" is the first user process, and all other user processes will be forked | 
|---|
| 218 | * from this process. The code executed by "process_init" is stored in a .elf file, whose | 
|---|
| 219 | * pathname is defined by the CONFIG_PROCESS_INIT_PATH configuration variable. | 
|---|
| 220 | * The process_init does not use the [STDIN/STDOUT/STDERR] streams, but it is linked | 
|---|
| 221 | * to kernel TXT0, because these streams must be defined for all user processes. | 
|---|
| 222 | ********************************************************************************************/ | 
|---|
| 223 | void process_init_create(); | 
|---|
| 224 |  | 
|---|
| 225 | /********************************************************************************************* | 
|---|
| 226 | * This function initializes a local, reference, user process descriptor from another process | 
|---|
| 227 | * descriptor, defined by the <model_xp> argument. The <process> and <pid> arguments must | 
|---|
| 228 | * be previously allocated by he caller. This function can be called by three functions: | 
|---|
| 229 | * 1) process_init_create() : process is the reference INIT process / pid = 1 / | 
|---|
| 230 | *    the parent and model process descriptors are both the kernel process_zero. | 
|---|
| 231 | * 2) process_make_fork() : the model process descriptor is the (generally remote) | 
|---|
| 232 | *    parent process. | 
|---|
| 233 | * 3) process_make exec() : the model process is the local old_process, the new_process | 
|---|
| 234 | *    parent is the same as the old_process parent. | 
|---|
| 235 | * The following fields are initialised : | 
|---|
| 236 | * - It set the pid / ppid / ref_xp / parent_xp / state fields. | 
|---|
| 237 | * - It initializes the VMM (register the kentry, args, envs vsegs in VSL) | 
|---|
| 238 | * - It initializes the FDT, defining the three pseudo files STDIN / STDOUT / STDERR. | 
|---|
| 239 | * - It set the root_xp, bin_xp, cwd_xp fields. | 
|---|
| 240 | * - It reset the children list as empty, but does NOT register it in parent children list. | 
|---|
| 241 | * - It reset the TH_TBL list of threads as empty. | 
|---|
| 242 | * - It reset the semaphore / mutex / barrier / condvar lists as empty. | 
|---|
| 243 | * - It registers the process in the local_list, rooted in the local cluster manager. | 
|---|
| 244 | * - It registers the process in the copies_list, rooted in the owner cluster manager. | 
|---|
| 245 | * - It registers the process extended pointer in the local pref_tbl[] array. | 
|---|
| 246 | ********************************************************************************************* | 
|---|
| 247 | * @ process      : [in] pointer on local process descriptor to initialize. | 
|---|
| 248 | * @ pid          : [in] process identifier. | 
|---|
| 249 | * @ parent_xp    : [in] extended pointer on parent process descriptor. | 
|---|
| 250 | * @ model_xp     : [in] extended pointer on model process descriptor. | 
|---|
| 251 | ********************************************************************************************/ | 
|---|
| 252 | void process_reference_init( process_t * process, | 
|---|
| 253 | pid_t       pid, | 
|---|
| 254 | xptr_t      parent_xp, | 
|---|
| 255 | xptr_t      model_xp ); | 
|---|
| 256 |  | 
|---|
| 257 | /********************************************************************************************* | 
|---|
| 258 | * This function initializes a copy process descriptor, in the local cluster, | 
|---|
| 259 | * from information defined in the reference remote process descriptor. | 
|---|
| 260 | ********************************************************************************************* | 
|---|
| 261 | * @ process              : [in] local pointer on process descriptor to initialize. | 
|---|
| 262 | * @ reference_process_xp : [in] extended pointer on reference process descriptor. | 
|---|
| 263 | * @ return 0 if success / return ENOMEM if failure | 
|---|
| 264 | ********************************************************************************************/ | 
|---|
| 265 | error_t process_copy_init( process_t * local_process, | 
|---|
| 266 | xptr_t      reference_process_xp ); | 
|---|
| 267 |  | 
|---|
| 268 | /********************************************************************************************* | 
|---|
| 269 | * This function releases all memory allocated for a process descriptor in the local cluster, | 
|---|
| 270 | * including memory allocated for embedded substructures (fd_array, vmm, etc). | 
|---|
| 271 | * The local th_tbl[] array must be empty. | 
|---|
| 272 | ********************************************************************************************* | 
|---|
| 273 | * @ process     : pointer on the process descriptor. | 
|---|
| 274 | ********************************************************************************************/ | 
|---|
| 275 | void process_destroy( process_t * process ); | 
|---|
| 276 |  | 
|---|
| 277 | /********************************************************************************************* | 
|---|
| 278 | * This function returns a printable string defining the process state. | 
|---|
| 279 | ********************************************************************************************* | 
|---|
| 280 | * @ state   : RUNNING / BLOCKED / EXITED / KILLED | 
|---|
| 281 | * @ return a string pointer. | 
|---|
| 282 | ********************************************************************************************/ | 
|---|
| 283 | char * process_state_str( uint32_t state ); | 
|---|
| 284 |  | 
|---|
| 285 | /********************************************************************************************* | 
|---|
| 286 | * This debug function diplays on the kernel terminal TXT0 detailed informations on a | 
|---|
| 287 | * reference process identified by the <process_xp> argument. | 
|---|
| 288 | * It can be called by a thread running in any cluster. | 
|---|
| 289 | ********************************************************************************************* | 
|---|
| 290 | * @ process_xp : extended pointer on reference process. | 
|---|
| 291 | ********************************************************************************************/ | 
|---|
| 292 | void process_display( xptr_t process_xp ); | 
|---|
| 293 |  | 
|---|
| 294 | /********************************************************************************************* | 
|---|
| 295 | * This function returns a printable string defining the sigaction type. | 
|---|
| 296 | ********************************************************************************************* | 
|---|
| 297 | * @ sigaction_type   : BLOCK_ALL_THREADS / UNBLOCK_ALL_THREADS / DELETE_ALL_THREADS | 
|---|
| 298 | * @ return a string pointer. | 
|---|
| 299 | ********************************************************************************************/ | 
|---|
| 300 | char * process_action_str( uint32_t sigaction_type ); | 
|---|
| 301 |  | 
|---|
| 302 | /********************************************************************************************* | 
|---|
| 303 | * This function allows a client thread running in the owner cluster of a process identified | 
|---|
| 304 | * by the <process> argument to block, unblock or delete all threads of the target process, | 
|---|
| 305 | * depending on the <action_type> argument.  The scenario is the following: | 
|---|
| 306 | * - It uses the multicast, non blocking rpc_process_sigaction_client() function to send | 
|---|
| 307 | *   parallel requests to all remote clusters containing a process copy. Then it blocks | 
|---|
| 308 | $   and deschedule to wait completion of these parrallel requests. | 
|---|
| 309 | * - In each remote cluster, the rpc_process_sigaction_server() function, calls directly | 
|---|
| 310 | *   the relevant process_block(), process_unblock(), or process_delete() function, and | 
|---|
| 311 | *   decrement the responses counter to signal completion. The last server unblock | 
|---|
| 312 | *   the client thread. | 
|---|
| 313 | * - Finally, the client thread calls directly the process_block(), process_unblock(), or | 
|---|
| 314 | *   process_delete() function in the owner cluster. | 
|---|
| 315 | * It is used by the sys_kill() & sys_exit() functions to handle the "kill" & "exit" syscalls. | 
|---|
| 316 | * It is also used by the process_make_exec() function to handle the "exec" syscall. | 
|---|
| 317 | * WARNING : the DELETE and the BLOCK actions are NOT executed on the client thread. | 
|---|
| 318 | ********************************************************************************************* | 
|---|
| 319 | * @ process     : pointer on the process descriptor in owner cluster. | 
|---|
| 320 | * @ action_type : BLOCK_ALL_THREADS / UNBLOCK_ALL_THREADS / DELETE_ALL_THREADS | 
|---|
| 321 | ********************************************************************************************/ | 
|---|
| 322 | void process_sigaction( process_t * process, | 
|---|
| 323 | uint32_t    action_type ); | 
|---|
| 324 |  | 
|---|
| 325 | /********************************************************************************************* | 
|---|
| 326 | * This function blocks all threads (but the client thread defined by the <client_xp> | 
|---|
| 327 | * argument) for a given <process> in a given cluster. | 
|---|
| 328 | * It loops on all local threads of the process, set the THREAD_BLOCKED_GLOBAL bit, | 
|---|
| 329 | * and request the relevant schedulers to acknowledge the blocking, using IPI if required. | 
|---|
| 330 | * The threads are not detached from the scheduler, and not detached from the local process. | 
|---|
| 331 | * This function returns only when all blockable threads in cluster are actually blocked. | 
|---|
| 332 | ********************************************************************************************* | 
|---|
| 333 | * @ process     : pointer on the target process descriptor. | 
|---|
| 334 | * @ client_xp   : extended pointer on the client thread, that should not be blocked. | 
|---|
| 335 | ********************************************************************************************/ | 
|---|
| 336 | void process_block_threads( process_t * process, | 
|---|
| 337 | xptr_t      client_xp ); | 
|---|
| 338 |  | 
|---|
| 339 | /********************************************************************************************* | 
|---|
| 340 | * This function unblocks all threads of a given user process in a given cluster. | 
|---|
| 341 | ********************************************************************************************* | 
|---|
| 342 | * @ process     : pointer on the process descriptor. | 
|---|
| 343 | ********************************************************************************************/ | 
|---|
| 344 | void process_unblock_threads( process_t * process ); | 
|---|
| 345 |  | 
|---|
| 346 | /********************************************************************************************* | 
|---|
| 347 | * This function delete all threads, (but the client thread defined by the <client_xp> | 
|---|
| 348 | * argument) for a given <process> in a given cluster. | 
|---|
| 349 | * It loops on all local threads of the process, and set the THREAD_FLAG_REQ_DELETE bit. | 
|---|
| 350 | * For each marked thread, the following actions will be done by the scheduler at the next | 
|---|
| 351 | * scheduling point: | 
|---|
| 352 | * - the thread will be detached from the scheduler. | 
|---|
| 353 | * - the thread will be detached from the local process descriptor. | 
|---|
| 354 | * - the thread will be detached from parent if required. | 
|---|
| 355 | * - the memory allocated to the thread descriptor is released. | 
|---|
| 356 | * - the memory allocated to the process descriptor is released, if it is the last thread. | 
|---|
| 357 | ********************************************************************************************* | 
|---|
| 358 | * @ process     : pointer on the process descriptor. | 
|---|
| 359 | * @ client_xp   : extended pointer on the client thread, that should not be deleted. | 
|---|
| 360 | ********************************************************************************************/ | 
|---|
| 361 | void process_delete_threads( process_t * process, | 
|---|
| 362 | xptr_t      client_xp ); | 
|---|
| 363 |  | 
|---|
| 364 | /********************************************************************************************* | 
|---|
| 365 | * This function returns a pointer on the local copy of a process identified by its PID. | 
|---|
| 366 | * If this local copy does not exist yet, it is dynamically created, from the reference | 
|---|
| 367 | * process descriptor, registered in the global copies_list, and registered in the local_list. | 
|---|
| 368 | * This function is used by the thread_user_create() function. | 
|---|
| 369 | ********************************************************************************************* | 
|---|
| 370 | * @ pid     : searched process identifier. | 
|---|
| 371 | * @ returns pointer on the local process descriptor if success / returns NULL if failure. | 
|---|
| 372 | ********************************************************************************************/ | 
|---|
| 373 | process_t * process_get_local_copy( pid_t pid ); | 
|---|
| 374 |  | 
|---|
| 375 | /********************************************************************************************* | 
|---|
| 376 | * This function implements the "exec" system call, and is called by the sys_exec() function. | 
|---|
| 377 | * The "new" process keep the "old" process PID and PPID, all open files, and env variables, | 
|---|
| 378 | * the vfs_root and vfs_cwd, but build a brand new memory image (new VMM from the new .elf). | 
|---|
| 379 | * It actually creates a "new" reference process descriptor, and copies all relevant | 
|---|
| 380 | * information from the "old" process descriptor to the "new" process descriptor. | 
|---|
| 381 | * It completes the "new" process descriptor, from information found in the <exec_info> | 
|---|
| 382 | * structure (defined in the process.h file), that must be built by the caller. | 
|---|
| 383 | * It creates and initializes the associated main thread. It finally destroys all copies | 
|---|
| 384 | * of the "old" process in all clusters, and destroys all old associated threads. | 
|---|
| 385 | * It is executed in the local cluster, that becomes both the "owner" and the "reference" | 
|---|
| 386 | * cluster for the "new" process. | 
|---|
| 387 | ********************************************************************************************* | 
|---|
| 388 | * @ exec_info   : [in]  pointer on the exec_info structure. | 
|---|
| 389 | * @ return 0 if success / return non-zero if error. | 
|---|
| 390 | ********************************************************************************************/ | 
|---|
| 391 | error_t process_make_exec( exec_info_t * exec_info ); | 
|---|
| 392 |  | 
|---|
| 393 | /********************************************************************************************* | 
|---|
| 394 | * This function implements the "fork" system call, and is called by the sys_fork() function. | 
|---|
| 395 | * It allocates memory and initializes a new "child" process descriptor, and the | 
|---|
| 396 | * associated "child" thread descriptor in the local cluster. This function can involve | 
|---|
| 397 | * up to three different clusters : | 
|---|
| 398 | * - the local (child) cluster can be any cluster defined by the sys_fork function. | 
|---|
| 399 | * - the parent cluster must be the reference cluster for the parent process. | 
|---|
| 400 | * - the client cluster containing the thread requesting the fork can be any cluster. | 
|---|
| 401 | * The new "child" process descriptor is initialised from informations found in the "parent" | 
|---|
| 402 | * reference process descriptor, containing the complete process description. | 
|---|
| 403 | * The new "child" thread descriptor is initialised from informations found in the "parent" | 
|---|
| 404 | * thread descriptor. | 
|---|
| 405 | ********************************************************************************************* | 
|---|
| 406 | * @ parent_process_xp  : extended pointer on the reference parent process. | 
|---|
| 407 | * @ parent_thread_xp   : extended pointer on the parent thread requesting the fork. | 
|---|
| 408 | * @ child_pid          : [out] child process identifier. | 
|---|
| 409 | * @ child_thread_ptr   : [out] local pointer on child thread in target cluster. | 
|---|
| 410 | * @ return 0 if success / return non-zero if error. | 
|---|
| 411 | ********************************************************************************************/ | 
|---|
| 412 | error_t process_make_fork(  xptr_t             parent_process_xp, | 
|---|
| 413 | xptr_t             parent_thread_xp, | 
|---|
| 414 | pid_t            * child_pid, | 
|---|
| 415 | struct thread_s ** child_thread_ptr ); | 
|---|
| 416 |  | 
|---|
| 417 | /********************************************************************************************* | 
|---|
| 418 | * This function implement the "exit" system call, and is called by the sys_exit() function. | 
|---|
| 419 | * It must be executed by a thread running in the calling process owner cluster. | 
|---|
| 420 | * It uses twice the multicast RPC_PROCESS_SIGNAL to first block all process threads | 
|---|
| 421 | * in all clusters, and then delete all threads and process descriptors. | 
|---|
| 422 | ********************************************************************************************* | 
|---|
| 423 | * @ pid      : process identifier. | 
|---|
| 424 | * @ status   : exit return value. | 
|---|
| 425 | ********************************************************************************************/ | 
|---|
| 426 | void process_make_exit( pid_t       pid, | 
|---|
| 427 | uint32_t    status ); | 
|---|
| 428 |  | 
|---|
| 429 | /********************************************************************************************* | 
|---|
| 430 | * This function implement the "kill" system call, and is called by the sys_kill() function. | 
|---|
| 431 | * It must be executed by a thread running in the target process owner cluster. | 
|---|
| 432 | * Only the SIGKILL, SIGSTOP, and SIGCONT signals are supported. | 
|---|
| 433 | * User defined handlers are not supported. | 
|---|
| 434 | * It uses once or twice the multicast RPC_PROCESS_SIGNAL to block, unblock or delete | 
|---|
| 435 | * all process threads in all clusters, and then delete process descriptors. | 
|---|
| 436 | ********************************************************************************************* | 
|---|
| 437 | * @ pid     : process identifier. | 
|---|
| 438 | * @ sig_id  : signal type. | 
|---|
| 439 | ********************************************************************************************/ | 
|---|
| 440 | void process_make_kill( pid_t     pid, | 
|---|
| 441 | uint32_t  sig_id ); | 
|---|
| 442 |  | 
|---|
| 443 |  | 
|---|
| 444 | /********************   File Management Operations   ****************************************/ | 
|---|
| 445 |  | 
|---|
| 446 | /********************************************************************************************* | 
|---|
| 447 | * This function initializes all entries of the local fd_array as empty. | 
|---|
| 448 | ********************************************************************************************* | 
|---|
| 449 | * @ process  : pointer on the local process descriptor. | 
|---|
| 450 | ********************************************************************************************/ | 
|---|
| 451 | void process_fd_init( process_t * process ); | 
|---|
| 452 |  | 
|---|
| 453 | /********************************************************************************************* | 
|---|
| 454 | * This function uses as many remote accesses as required, to reset an entry in fd_array[], | 
|---|
| 455 | * in all clusters containing a copy. The entry is identified by the <fdid> argument. | 
|---|
| 456 | * This function must be executed by a thread running reference cluster, that contains | 
|---|
| 457 | * the complete list of process descriptors copies. | 
|---|
| 458 | ********************************************************************************************* | 
|---|
| 459 | * @ process  : pointer on the local process descriptor. | 
|---|
| 460 | * @ fdid     : file descriptor index in the fd_array. | 
|---|
| 461 | ********************************************************************************************/ | 
|---|
| 462 | void process_fd_remove( process_t * process, | 
|---|
| 463 | uint32_t    fdid ); | 
|---|
| 464 |  | 
|---|
| 465 | /********************************************************************************************* | 
|---|
| 466 | * This function returns an extended pointer on a file descriptor identified by its index | 
|---|
| 467 | * in fd_array. It can be called by any thread running in any cluster. | 
|---|
| 468 | * It accesses first the local process descriptor. In case of local miss, it uses remote | 
|---|
| 469 | * access to access the reference process descriptor. | 
|---|
| 470 | * It updates the local fd_array when the file descriptor exists in reference cluster. | 
|---|
| 471 | * The file descriptor refcount is not incremented. | 
|---|
| 472 | ********************************************************************************************* | 
|---|
| 473 | * @ process  : pointer on the local process descriptor. | 
|---|
| 474 | * @ fdid     : file descriptor index in the fd_array. | 
|---|
| 475 | * @ return extended pointer on file descriptor if success / return XPTR_NULL if not found. | 
|---|
| 476 | ********************************************************************************************/ | 
|---|
| 477 | xptr_t process_fd_get_xptr( process_t * process, | 
|---|
| 478 | uint32_t    fdid ); | 
|---|
| 479 |  | 
|---|
| 480 | /********************************************************************************************* | 
|---|
| 481 | * This function checks the number of open files for a given process. | 
|---|
| 482 | * It can be called by any thread in any cluster, because it uses portable remote access | 
|---|
| 483 | * primitives to access the reference process descriptor. | 
|---|
| 484 | ********************************************************************************************* | 
|---|
| 485 | * @ returns true if file descriptor array full. | 
|---|
| 486 | ********************************************************************************************/ | 
|---|
| 487 | bool_t process_fd_array_full(); | 
|---|
| 488 |  | 
|---|
| 489 | /********************************************************************************************* | 
|---|
| 490 | * This function allocates a free slot in the fd_array of the reference process, | 
|---|
| 491 | * register the <file_xp> argument in the allocated slot, and return the slot index. | 
|---|
| 492 | * It can be called by any thread in any cluster, because it uses portable remote access | 
|---|
| 493 | * primitives to access the reference process descriptor. | 
|---|
| 494 | ********************************************************************************************* | 
|---|
| 495 | * @ file_xp  : extended pointer on the file descriptor to be registered. | 
|---|
| 496 | * @ fdid     : [out] buffer for fd_array slot index. | 
|---|
| 497 | * @ return 0 if success / return EMFILE if array full. | 
|---|
| 498 | ********************************************************************************************/ | 
|---|
| 499 | error_t process_fd_register( process_t * process, | 
|---|
| 500 | xptr_t      file_xp, | 
|---|
| 501 | uint32_t  * fdid ); | 
|---|
| 502 |  | 
|---|
| 503 | /********************************************************************************************* | 
|---|
| 504 | * This function copies all non-zero entries (other than the three first stdin/stdout/stderr) | 
|---|
| 505 | * from a remote <src_xp> fd_array, embedded in a process descriptor, to another remote | 
|---|
| 506 | * <dst_xp> fd_array, embedded in another process descriptor. | 
|---|
| 507 | * The calling thread can be running in any cluster. | 
|---|
| 508 | * It takes the remote lock protecting the <src_xp> fd_array during the copy. | 
|---|
| 509 | * For each involved file descriptor, the refcount is incremented. | 
|---|
| 510 | ********************************************************************************************* | 
|---|
| 511 | * @ dst_xp   : extended pointer on the destination fd_array_t. | 
|---|
| 512 | * @ src_xp   : extended pointer on the source fd_array_t. | 
|---|
| 513 | ********************************************************************************************/ | 
|---|
| 514 | void process_fd_remote_copy( xptr_t dst_xp, | 
|---|
| 515 | xptr_t src_xp ); | 
|---|
| 516 |  | 
|---|
| 517 |  | 
|---|
| 518 |  | 
|---|
| 519 | /********************   Thread Related Operations   *****************************************/ | 
|---|
| 520 |  | 
|---|
| 521 | /********************************************************************************************* | 
|---|
| 522 | * This function registers a new thread in the local process descriptor. | 
|---|
| 523 | * It checks that there is an available slot in the local th_tbl[] array, | 
|---|
| 524 | * allocates a new LTID, and registers the new thread in the th_tbl[]. | 
|---|
| 525 | * WARNING : the lock protecting the th_tbl[] must be taken by the caller. | 
|---|
| 526 | ********************************************************************************************* | 
|---|
| 527 | * @ process  : pointer on the local process descriptor. | 
|---|
| 528 | * @ thread   : pointer on new thread to be registered. | 
|---|
| 529 | * @ trdid    : [out] address of buffer for allocated trdid. | 
|---|
| 530 | * @ returns 0 if success / returns non zero if no slot available. | 
|---|
| 531 | ********************************************************************************************/ | 
|---|
| 532 | error_t process_register_thread( process_t       * process, | 
|---|
| 533 | struct thread_s * thread, | 
|---|
| 534 | trdid_t         * trdid ); | 
|---|
| 535 |  | 
|---|
| 536 | /********************************************************************************************* | 
|---|
| 537 | * This function removes a thread registration from the local process descriptor. | 
|---|
| 538 | * WARNING : the lock protecting the th_tbl[] must be taken by the caller. | 
|---|
| 539 | ********************************************************************************************* | 
|---|
| 540 | * @ thread   : local pointer on thread to be removed. | 
|---|
| 541 | ********************************************************************************************/ | 
|---|
| 542 | void process_remove_thread( struct thread_s * thread ); | 
|---|
| 543 |  | 
|---|
| 544 |  | 
|---|
| 545 | /***************   Terminals related operations  ********************************************/ | 
|---|
| 546 |  | 
|---|
| 547 | /********************************************************************************************* | 
|---|
| 548 | * This function scan the set of user TXT terminals to find a free terminal | 
|---|
| 549 | * (the list of attached processes is empty for a free TXT terminal). | 
|---|
| 550 | * It is called only by the process_reference_init() function when creating a KSH process. | 
|---|
| 551 | * It makes a kernel panic if no free TXT terminal is found. | 
|---|
| 552 | * As a KSH process is never deleted, the allocated TXT terminal is never released. | 
|---|
| 553 | ********************************************************************************************* | 
|---|
| 554 | * @ return TXT terminal index if succes / kernel panic if no terminal found. | 
|---|
| 555 | ********************************************************************************************/ | 
|---|
| 556 | uint32_t process_txt_alloc(); | 
|---|
| 557 |  | 
|---|
| 558 | /********************************************************************************************* | 
|---|
| 559 | * This function attach a reference process descriptor, identified by the <process> | 
|---|
| 560 | * argument to a TXT terminal, identified by its <txt_id> channel index argument. | 
|---|
| 561 | * It insert the process descriptor in the xlist rooted in the TXT_RX device. | 
|---|
| 562 | * It is called by the process_reference_init() function. | 
|---|
| 563 | ********************************************************************************************* | 
|---|
| 564 | * @ process  : local pointer on process descriptor. | 
|---|
| 565 | * @ txt_id   : TXT channel index. | 
|---|
| 566 | ********************************************************************************************/ | 
|---|
| 567 | void process_txt_attach( process_t * process, | 
|---|
| 568 | uint32_t    txt_id ); | 
|---|
| 569 |  | 
|---|
| 570 | /********************************************************************************************* | 
|---|
| 571 | * This function detach a reference process descriptor, identified by the <process_xp> | 
|---|
| 572 | * argument, from the list of process attached to a given TXT terminal. | 
|---|
| 573 | * It is called when the process is killed. | 
|---|
| 574 | ********************************************************************************************* | 
|---|
| 575 | * @ process  : local pointer on process descriptor. | 
|---|
| 576 | ********************************************************************************************/ | 
|---|
| 577 | void process_txt_detach( process_t * process ); | 
|---|
| 578 |  | 
|---|
| 579 | /********************************************************************************************* | 
|---|
| 580 | * This function gives to a process identified by the <process_xp> argument, and attached | 
|---|
| 581 | * to terminal TXT[i] the exclusive ownership of the TXT_RX[i] terminal. | 
|---|
| 582 | ********************************************************************************************* | 
|---|
| 583 | * @ process_xp  : extended pointer on reference process descriptor. | 
|---|
| 584 | ********************************************************************************************/ | 
|---|
| 585 | void process_txt_set_ownership( xptr_t process_xp ); | 
|---|
| 586 |  | 
|---|
| 587 | /********************************************************************************************* | 
|---|
| 588 | * When the process identified by the <process_xp> argument has the exclusive ownership | 
|---|
| 589 | * of the TXT_RX[i] terminal, this function gives this ownership to the KSH[i] process. | 
|---|
| 590 | * It does nothing if the process is not the owner. | 
|---|
| 591 | ********************************************************************************************* | 
|---|
| 592 | * @ process_xp  : extended pointer on reference process descriptor. | 
|---|
| 593 | ********************************************************************************************/ | 
|---|
| 594 | void process_txt_reset_ownership( xptr_t process_xp ); | 
|---|
| 595 |  | 
|---|
| 596 | #endif  /* _PROCESS_H_ */ | 
|---|