[1] | 1 | /* |
---|
| 2 | * process.h - process related management functions |
---|
[173] | 3 | * |
---|
[1] | 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
| 5 | * Mohamed Lamine Karaoui (2015) |
---|
[23] | 6 | * Alain Greiner (2016,2017) |
---|
[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> |
---|
| 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 structure defines an array of extended pointers on the open file descriptors |
---|
| 57 | * for a given process. We use an extended pointer because the open file descriptor |
---|
| 58 | * is always stored in the same cluster as the inode associated to the file. |
---|
| 59 | * A free entry in this array contains the XPTR_NULL value. |
---|
| 60 | * The array size is defined by a the CONFIG_PROCESS_FILE_MAX_NR parameter. |
---|
[23] | 61 | * All modifications (open/close) in this structure must be done by the reference cluster, |
---|
| 62 | * and reported in process copies. |
---|
[1] | 63 | ********************************************************************************************/ |
---|
| 64 | |
---|
| 65 | typedef struct fd_array_s |
---|
| 66 | { |
---|
[101] | 67 | remote_spinlock_t lock; /*! lock protecting fd_array */ |
---|
| 68 | uint32_t current; /*! current number of open files */ |
---|
| 69 | xptr_t array[CONFIG_PROCESS_FILE_MAX_NR]; /*! xptr on open file descriptors */ |
---|
[1] | 70 | } |
---|
| 71 | fd_array_t; |
---|
| 72 | |
---|
| 73 | /********************************************************************************************* |
---|
| 74 | * This structure defines a process descriptor. |
---|
| 75 | * A process is identified by a unique PID (process identifier): |
---|
| 76 | * - The PID 16 LSB bits contain the LPID (Local Process Index) |
---|
| 77 | * - The PID 16 MSB bits contain the owner cluster CXY. |
---|
| 78 | * In each cluster, the process manager allocates LPID values for the process that are |
---|
| 79 | * allocated to this cluster. |
---|
| 80 | * The process descriptor for a PID process is replicated in all clusters containing |
---|
[23] | 81 | * at least one thread of the PID process, with the following rules : |
---|
| 82 | * |
---|
| 83 | * 1) The <pid>, <ppid>, <ref_xp>, <vfs_root_xp>, <vfs_bin_xp> fields are defined |
---|
| 84 | * in all process descriptor copies. |
---|
| 85 | * 2) The <vfs_cwd_xp> and associated <cwd_lock>, that can be dynamically modified, |
---|
| 86 | * are only defined in the reference process descriptor. |
---|
| 87 | * 2) The <vmm>, containing the list of registered vsegs, and the page table, are only |
---|
| 88 | * complete in the reference process cluster, other copies are read-only caches. |
---|
| 89 | * 3) the <fd_array>, containing extended pointers on the open file descriptors, is only |
---|
| 90 | * complete in the reference process cluster, other copies are read-only caches. |
---|
[173] | 91 | * 4) The <sem_root>, <mutex_root>, <barrier_root>, <condvar_root>, and the associated |
---|
[23] | 92 | * <sync_lock>, that are dynamically allocated, are only defined in the reference cluster. |
---|
| 93 | * 5) The <children_root>, and <children_nr> fields are only defined in the reference |
---|
| 94 | * cluster, and are undefined in other clusters. |
---|
| 95 | * 6) The <brothers_list>, <local_list>, <copies_list>, <th_tbl>, <th_nr>, <th_lock> fields |
---|
| 96 | * are defined in all process descriptors copies. |
---|
[173] | 97 | * 7) The <sig_mgr> field is only defined in the reference cluster. TODO |
---|
[1] | 98 | ********************************************************************************************/ |
---|
| 99 | |
---|
| 100 | typedef struct process_s |
---|
| 101 | { |
---|
[23] | 102 | vmm_t vmm; /*! embedded virtual memory manager */ |
---|
[1] | 103 | |
---|
[23] | 104 | fd_array_t fd_array; /*! embedded open file descriptors array */ |
---|
[1] | 105 | |
---|
[173] | 106 | xptr_t vfs_root_xp; /*! extended pointer on current VFS root inode */ |
---|
[23] | 107 | xptr_t vfs_bin_xp; /*! extended pointer on .elf file inode */ |
---|
| 108 | pid_t pid; /*! process identifier */ |
---|
| 109 | pid_t ppid; /*! parent process identifier */ |
---|
| 110 | xptr_t ref_xp; /*! extended pointer on reference process */ |
---|
[1] | 111 | |
---|
[23] | 112 | xptr_t vfs_cwd_xp; /*! extended pointer on current working dir inode */ |
---|
| 113 | remote_rwlock_t cwd_lock; /*! lock protecting working directory changes */ |
---|
[173] | 114 | |
---|
[23] | 115 | xlist_entry_t children_root; /*! root of the children process xlist */ |
---|
| 116 | uint32_t children_nr; /*! number of children processes */ |
---|
[1] | 117 | |
---|
[23] | 118 | xlist_entry_t brothers_list; /*! member of list of children of same parent */ |
---|
| 119 | xlist_entry_t local_list; /*! member of list of process in same cluster */ |
---|
| 120 | xlist_entry_t copies_list; /*! member of list of copies of same process */ |
---|
[1] | 121 | |
---|
[23] | 122 | spinlock_t th_lock; /*! lock protecting th_tbl[] concurrent access */ |
---|
| 123 | uint32_t th_nr; /*! number of threads in this cluster */ |
---|
| 124 | struct thread_s * th_tbl[CONFIG_THREAD_MAX_PER_CLUSTER]; /*! pointers on local threads */ |
---|
[1] | 125 | |
---|
[23] | 126 | xlist_entry_t sem_root; /*! root of the process semaphore list */ |
---|
| 127 | xlist_entry_t mutex_root; /*! root of the process mutex list */ |
---|
| 128 | xlist_entry_t barrier_root; /*! root of the process barrier list */ |
---|
| 129 | xlist_entry_t condvar_root; /*! root of the process condvar list */ |
---|
[1] | 130 | |
---|
[23] | 131 | remote_spinlock_t sync_lock; /*! lock protecting sem,mutex,barrier,condvar lists */ |
---|
[1] | 132 | |
---|
[23] | 133 | sig_mgr_t sig_mgr; /*! embedded signal manager TODO [AG] */ |
---|
[1] | 134 | } |
---|
| 135 | process_t; |
---|
| 136 | |
---|
| 137 | /********************************************************************************************* |
---|
[101] | 138 | * This structure defines the information required by the process_make_exec() function |
---|
| 139 | * to create a new reference process descriptor, and the associated main thread. |
---|
[1] | 140 | ********************************************************************************************/ |
---|
| 141 | |
---|
| 142 | typedef struct exec_info_s |
---|
| 143 | { |
---|
[101] | 144 | xptr_t parent_xp; /*! extended pointer on parent process descriptor */ |
---|
[1] | 145 | |
---|
[23] | 146 | char path[CONFIG_VFS_MAX_PATH_LENGTH]; /*! .elf file path */ |
---|
[1] | 147 | |
---|
| 148 | char ** args_pointers; /*! physical base address of array of pointers */ |
---|
| 149 | char * args_buf_base; /*! physical base address of kernel args buffer */ |
---|
| 150 | uint32_t args_nr; /*! actual number of arguments */ |
---|
| 151 | |
---|
| 152 | char ** envs_pointers; /*! physical base address of array of pointers */ |
---|
| 153 | char * envs_buf_base; /*! physical base address of kernel args buffer */ |
---|
| 154 | char * envs_buf_free; /*! physical address of first free slot in envs_buf */ |
---|
| 155 | uint32_t envs_nr; /*! actual number of environment variables */ |
---|
| 156 | } |
---|
| 157 | exec_info_t; |
---|
| 158 | |
---|
| 159 | /*************** Process Descriptor Operations *****************************************/ |
---|
| 160 | |
---|
[173] | 161 | /********************************************************************************************* |
---|
| 162 | * This function allocates memory in local cluster for a process descriptor. |
---|
| 163 | ********************************************************************************************* |
---|
[1] | 164 | * @ returns pointer on process descriptor if success / return NULL if failure |
---|
| 165 | ********************************************************************************************/ |
---|
| 166 | process_t * process_alloc(); |
---|
| 167 | |
---|
[173] | 168 | /********************************************************************************************* |
---|
| 169 | * This function releases memory in local cluster for a process descriptor. |
---|
| 170 | ********************************************************************************************* |
---|
[1] | 171 | * @ process : pointer on process descriptor to release. |
---|
| 172 | ********************************************************************************************/ |
---|
| 173 | void process_free( process_t * process ); |
---|
| 174 | |
---|
[173] | 175 | /********************************************************************************************* |
---|
| 176 | * This function allocates memory and initializes the "process_init" descriptor and the |
---|
[101] | 177 | * associated "thread_init" descriptor. It should be called once at the end of the kernel |
---|
| 178 | * initialisation procedure, by the kernel "process_zero". |
---|
[173] | 179 | * The "process_init" is the first user process, and all other user processes will be forked |
---|
[101] | 180 | * from this process. The code executed by "process_init" is stored in a .elf file, whose |
---|
[173] | 181 | * pathname is defined by the CONFIG_PROCESS_INIT_PATH argument. It uses fork/exec syscalls |
---|
[101] | 182 | * to create the "shell" user process, and various other user daemon processes. |
---|
[173] | 183 | * Practically, it builds the exec_info structure, registers the stdin / stdout / stderr |
---|
| 184 | * pseudo-file descriptors and the vfs_root and vfs_cwd in parent process_zero, and calls |
---|
[101] | 185 | * the generic process_make_exec() function, that makes the real job. |
---|
[173] | 186 | ********************************************************************************************/ |
---|
[1] | 187 | void process_init_create(); |
---|
| 188 | |
---|
[173] | 189 | /********************************************************************************************* |
---|
| 190 | * This function initializes a new process descriptor, in the reference cluster. |
---|
[1] | 191 | * The PID value must have been defined previously by the owner cluster manager. |
---|
[173] | 192 | * The reference cluster can be different from the owner cluster. |
---|
[23] | 193 | * It set the pid / ppid / ref_xp fields. |
---|
[1] | 194 | * It registers this process descriptor in three lists: |
---|
[101] | 195 | * - the children_list in the parent reference process descriptor. |
---|
[1] | 196 | * - the local_list, rooted in the reference cluster manager. |
---|
| 197 | * - the copies_list, rooted in the owner cluster manager. |
---|
[173] | 198 | * It resets the embedded structures such as the VMM or the file descriptor array. |
---|
| 199 | ********************************************************************************************* |
---|
[1] | 200 | * @ process : [in] pointer on process descriptor to initialize. |
---|
| 201 | * @ pid : [in] process identifier defined by owner cluster. |
---|
[101] | 202 | * @ parent_xp : [in] extended pointer on parent process. |
---|
[1] | 203 | ********************************************************************************************/ |
---|
| 204 | void process_reference_init( process_t * process, |
---|
| 205 | pid_t pid, |
---|
[101] | 206 | xptr_t parent_xp ); |
---|
[1] | 207 | |
---|
[173] | 208 | /********************************************************************************************* |
---|
| 209 | * This function initializes a copy process descriptor, in the local cluster, |
---|
| 210 | * from information defined in the reference remote process descriptor. |
---|
| 211 | ********************************************************************************************* |
---|
[1] | 212 | * @ process : [in] local pointer on process descriptor to initialize. |
---|
| 213 | * @ reference_process_xp : [in] extended pointer on reference process descriptor. |
---|
| 214 | * @ return 0 if success / return ENOMEM if failure |
---|
| 215 | ********************************************************************************************/ |
---|
| 216 | error_t process_copy_init( process_t * local_process, |
---|
| 217 | xptr_t reference_process_xp ); |
---|
| 218 | |
---|
[173] | 219 | /********************************************************************************************* |
---|
[1] | 220 | * This function releases all memory allocated for a process descriptor in the local cluster, |
---|
[23] | 221 | * including memory allocated for embedded substructures (fd_array, vmm, etc). |
---|
[1] | 222 | * The local th_tbl[] array must be empty. |
---|
[173] | 223 | ********************************************************************************************* |
---|
[1] | 224 | * @ process : pointer on the process descriptor. |
---|
| 225 | ********************************************************************************************/ |
---|
| 226 | void process_destroy( process_t * process ); |
---|
| 227 | |
---|
[173] | 228 | /********************************************************************************************* |
---|
| 229 | * This function kills a user process in a given cluster. |
---|
[1] | 230 | * It can be directly called in the reference cluster, or it can be called through the |
---|
| 231 | * PROCESS_KILL RPC. |
---|
| 232 | * - In a first loop, it set the THREAD_SIG_KILL signal to all threads of process. |
---|
| 233 | * - In a second loop, it wait, for each thread the reset of the THREAD_SIG_KILL signal |
---|
[173] | 234 | * by the scheduler, and completes the thread descriptor destruction. |
---|
| 235 | ********************************************************************************************* |
---|
[1] | 236 | * @ process : pointer on the process descriptor. |
---|
| 237 | ********************************************************************************************/ |
---|
| 238 | void process_kill( process_t * process ); |
---|
| 239 | |
---|
[173] | 240 | /********************************************************************************************* |
---|
[1] | 241 | * This function returns a pointer on the local copy of a process identified by its PID. |
---|
[173] | 242 | * If this local copy does not exist yet, it is dynamically created, from the reference |
---|
[1] | 243 | * process descriptor, registered in the global copies_list, and registered in the local_list. |
---|
[23] | 244 | * This function is used by the thread_user_create() function. |
---|
[173] | 245 | ********************************************************************************************* |
---|
[1] | 246 | * @ pid : searched process identifier. |
---|
| 247 | * @ returns pointer on the local process descriptor if success / returns NULL if failure. |
---|
| 248 | ********************************************************************************************/ |
---|
| 249 | process_t * process_get_local_copy( pid_t pid ); |
---|
| 250 | |
---|
[173] | 251 | /********************************************************************************************* |
---|
[101] | 252 | * This function allocates memory and initializes a new user process descriptor, |
---|
[173] | 253 | * and the associated main thread, from information found in the <exec_info> structure |
---|
| 254 | * (defined in the process.h file), that must be built by the caller. |
---|
| 255 | * The new process inherits from the parent process (i) the open file descriptors, (ii) the |
---|
[101] | 256 | * vfs_root and the vfs_cwd inodes. |
---|
[173] | 257 | * It accesses the .elf file to get the size of the code and data segments, and initializes |
---|
[101] | 258 | * the vsegs list in the VMM. |
---|
[1] | 259 | * It is executed in the local cluster, that becomes both "owner" and "reference". |
---|
| 260 | * - It can be called by the process_init_create() function to build the "init" process. |
---|
| 261 | * - It can be called directly by the sys_exec() function in case of local exec. |
---|
| 262 | * - It can be called through the rpc_process_exec_server() function in case of remote exec. |
---|
[173] | 263 | ********************************************************************************************* |
---|
[1] | 264 | * @ exec_info : [in] pointer on the exec_info structure. |
---|
| 265 | * @ return 0 if success / return non-zero if error. |
---|
| 266 | ********************************************************************************************/ |
---|
| 267 | error_t process_make_exec( exec_info_t * exec_info ); |
---|
| 268 | |
---|
| 269 | |
---|
[173] | 270 | /******************** Signal Management Operations **************************************/ |
---|
[1] | 271 | |
---|
[173] | 272 | /********************************************************************************************* |
---|
[1] | 273 | * This function TODO [AG] |
---|
| 274 | ********************************************************************************************/ |
---|
| 275 | void process_signal_handler( process_t * process ); |
---|
| 276 | |
---|
| 277 | |
---|
[173] | 278 | /******************** File Management Operations ****************************************/ |
---|
[1] | 279 | |
---|
| 280 | /********************************************************************************************* |
---|
[173] | 281 | * This function initializes all entries of the local fd_array as empty. |
---|
| 282 | ********************************************************************************************* |
---|
[1] | 283 | * @ process : pointer on the local process descriptor. |
---|
| 284 | ********************************************************************************************/ |
---|
| 285 | void process_fd_init( process_t * process ); |
---|
| 286 | |
---|
| 287 | /********************************************************************************************* |
---|
[23] | 288 | * This function uses as many remote accesses as required, to reset an entry in fd_array[], |
---|
| 289 | * in all clusters containing a copy. The entry is identified by the <file_id> argument. |
---|
[173] | 290 | * This function must be executed by a thread running reference cluster, that contains |
---|
[23] | 291 | * the complete list of process descriptors copies. |
---|
[173] | 292 | ********************************************************************************************* |
---|
[1] | 293 | * @ process : pointer on the local process descriptor. |
---|
[23] | 294 | * @ file_id : file descriptor index in the fd_array. |
---|
[1] | 295 | ********************************************************************************************/ |
---|
[23] | 296 | void process_fd_remove( process_t * process, |
---|
| 297 | uint32_t file_id ); |
---|
[1] | 298 | |
---|
| 299 | /********************************************************************************************* |
---|
[173] | 300 | * This function returns an extended pointer on a file descriptor identified by its index |
---|
[23] | 301 | * in fd_array. It can be called by any thread running in any cluster. |
---|
[173] | 302 | * It accesses first the local process descriptor. In case of local miss, it uses remote |
---|
| 303 | * access to access the reference process descriptor. |
---|
| 304 | * It updates the local fd_array when the file descriptor exists in reference cluster. |
---|
[23] | 305 | * The file descriptor refcount is not incremented. |
---|
[173] | 306 | ********************************************************************************************* |
---|
[1] | 307 | * @ process : pointer on the local process descriptor. |
---|
[23] | 308 | * @ file_id : file descriptor index in the fd_array. |
---|
| 309 | * @ return extended pointer on file descriptor if success / return XPTR_NULL if not found. |
---|
[1] | 310 | ********************************************************************************************/ |
---|
[23] | 311 | xptr_t process_fd_get_xptr( process_t * process, |
---|
| 312 | uint32_t file_id ); |
---|
[1] | 313 | |
---|
| 314 | /********************************************************************************************* |
---|
[173] | 315 | * This function checks the number of open files for a given process. |
---|
[23] | 316 | * It can be called by any thread in any cluster, because it uses portable remote access |
---|
| 317 | * primitives to access the reference process descriptor. |
---|
[173] | 318 | ********************************************************************************************* |
---|
[23] | 319 | * @ returns true if file descriptor array full. |
---|
[1] | 320 | ********************************************************************************************/ |
---|
[23] | 321 | bool_t process_fd_array_full(); |
---|
[1] | 322 | |
---|
| 323 | /********************************************************************************************* |
---|
[23] | 324 | * This function allocates a free slot in the fd_array of the reference process, |
---|
| 325 | * register the <file_xp> argument in the allocated slot, and return the slot index. |
---|
| 326 | * It can be called by any thread in any cluster, because it uses portable remote access |
---|
| 327 | * primitives to access the reference process descriptor. |
---|
[173] | 328 | ********************************************************************************************* |
---|
[23] | 329 | * @ file_xp : extended pointer on the file descriptor to be registered. |
---|
| 330 | * @ file_id : [out] buffer for fd_array slot index. |
---|
| 331 | * @ return 0 if success / return EMFILE if array full. |
---|
[1] | 332 | ********************************************************************************************/ |
---|
[173] | 333 | error_t process_fd_register( xptr_t file_xp, |
---|
[23] | 334 | uint32_t * file_id ); |
---|
[1] | 335 | |
---|
| 336 | /********************************************************************************************* |
---|
| 337 | * This function copies all non-zero entries from a remote <src_xp> fd_array, |
---|
| 338 | * embedded in a process descriptor, to another remote <dst_xp> fd_array, embedded |
---|
| 339 | * in another process descriptor. The calling thread can be running in any cluster. |
---|
| 340 | * It takes the remote lock protecting the <src_xp> fd_array during the copy. |
---|
[173] | 341 | * For each involved file descriptor, the refcount is incremented. |
---|
| 342 | ********************************************************************************************* |
---|
[1] | 343 | * @ dst_xp : extended pointer on the destination fd_array_t. |
---|
| 344 | * @ src_xp : extended pointer on the source fd_array_t. |
---|
| 345 | ********************************************************************************************/ |
---|
| 346 | void process_fd_remote_copy( xptr_t dst_xp, |
---|
| 347 | xptr_t src_xp ); |
---|
| 348 | |
---|
| 349 | |
---|
[23] | 350 | |
---|
[173] | 351 | /******************** Thread Related Operations *****************************************/ |
---|
[1] | 352 | |
---|
| 353 | /********************************************************************************************* |
---|
| 354 | * This function registers a new thread in the local process descriptor. |
---|
| 355 | * It checks that there is an available slot in the local th_tbl[] array, |
---|
| 356 | * allocates a new LTID, and registers the new thread in the th_tbl[]. |
---|
| 357 | * WARNING : the lock protecting the th_tbl[] must be taken by the caller. |
---|
[173] | 358 | ********************************************************************************************* |
---|
[1] | 359 | * @ process : pointer on the local process descriptor. |
---|
| 360 | * @ thread : pointer on new thread to be registered. |
---|
| 361 | * @ trdid : [out] address of buffer for allocated trdid. |
---|
| 362 | * @ returns 0 if success / returns non zero if no slot available. |
---|
| 363 | ********************************************************************************************/ |
---|
| 364 | error_t process_register_thread( process_t * process, |
---|
| 365 | struct thread_s * thread, |
---|
| 366 | trdid_t * trdid ); |
---|
| 367 | |
---|
| 368 | /********************************************************************************************* |
---|
| 369 | * This function removes a thread registration from the local process descriptor. |
---|
| 370 | * WARNING : the lock protecting the th_tbl[] must be taken by the caller. |
---|
[173] | 371 | ********************************************************************************************* |
---|
[1] | 372 | * @ thread : local pointer on thread to be removed. |
---|
| 373 | ********************************************************************************************/ |
---|
| 374 | void process_remove_thread( struct thread_s * thread ); |
---|
| 375 | |
---|
| 376 | |
---|
| 377 | |
---|
| 378 | #endif /* _PROCESS_H_ */ |
---|