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