source: trunk/kernel/kern/process.h @ 413

Last change on this file since 413 was 409, checked in by alain, 7 years ago

Fix bugs in exec

File size: 31.5 KB
Line 
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
44struct 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_signal() function.
57 ********************************************************************************************/
58
59enum process_sigactions
60{
61    BLOCK_ALL_THREADS,
62    UNBLOCK_ALL_THREADS,
63    DELETE_ALL_THREADS,
64};
65
66/*********************************************************************************************
67 * This structure defines an array of extended pointers on the open file descriptors
68 * for a given process. We use an extended pointer because the open file descriptor
69 * is always stored in the same cluster as the inode associated to the file.
70 * A free entry in this array contains the XPTR_NULL value.
71 * The array size is defined by a the CONFIG_PROCESS_FILE_MAX_NR parameter.
72 * All modifications (open/close) in this structure must be done by the reference cluster,
73 * and reported in process copies.
74 ********************************************************************************************/
75
76typedef struct fd_array_s
77{
78        remote_spinlock_t lock;                               /*! lock protecting fd_array      */
79    uint32_t          current;                            /*! current number of open files  */
80        xptr_t            array[CONFIG_PROCESS_FILE_MAX_NR];  /*! xptr on open file descriptors */
81}
82fd_array_t;
83
84/*********************************************************************************************
85 * This structure defines a process descriptor.
86 * A process is identified by a unique PID (process identifier):
87 * - The PID 16 LSB bits contain the LPID (Local Process Index)
88 * - The PID 16 MSB bits contain the owner cluster CXY.
89 * In each cluster, the process manager allocates  the LPID values for the process that
90 * are owned by this cluster.
91 * The process descriptor is replicated in all clusters containing at least one thread
92 * of the PID process, with the following rules :
93 * 1) The <pid>, <ppid>, <ref_xp>, <vfs_root_xp>, <vfs_bin_xp>  fields are defined
94 *    in all process descriptor copies.
95 * 2) The <vfs_cwd_xp> and associated <cwd_lock>, that can be dynamically modified,
96 *    are only defined in the reference process descriptor.
97 * 2) The <vmm>, containing the VSL (list of registered vsegs), and the GPT (generic
98 *    page table), are only complete in the reference process cluster, other copies
99 *    are actually use as read-only caches.
100 * 3) the <fd_array>, containing extended pointers on the open file descriptors, is only
101 *    complete in the reference process cluster, other copies are read-only caches.
102 * 4) The <sem_root>, <mutex_root>, <barrier_root>, <condvar_root>, and the associated
103 *    <sync_lock>, that are dynamically allocated, are only defined in the reference cluster.
104 * 5) The <children_root>, and <children_nr> fields are only defined in the reference
105 *    cluster, and are undefined in other clusters.
106 * 6) The <brothers_list>, <local_list>, <copies_list>, <th_tbl>, <th_nr>, <th_lock> fields
107 *    are defined in all process descriptors copies.
108 ********************************************************************************************/
109
110typedef struct process_s
111{
112        vmm_t             vmm;              /*! embedded virtual memory manager                 */
113
114        fd_array_t        fd_array;         /*! embedded open file descriptors array            */
115
116        xptr_t            vfs_root_xp;      /*! extended pointer on current VFS root inode      */
117        xptr_t            vfs_bin_xp;       /*! extended pointer on .elf file inode             */
118        pid_t             pid;              /*! process identifier                              */
119        pid_t             ppid;             /*! parent process identifier                       */
120    xptr_t            ref_xp;           /*! extended pointer on reference process           */
121
122        xptr_t            vfs_cwd_xp;       /*! extended pointer on current working dir inode   */
123        remote_rwlock_t   cwd_lock;         /*! lock protecting working directory changes       */
124
125        xlist_entry_t     children_root;    /*! root of the children process xlist              */
126    uint32_t          children_nr;      /*! number of children processes                    */
127
128        xlist_entry_t     brothers_list;    /*! member of list of children of same parent       */
129    xlist_entry_t     local_list;       /*! member of list of process in same cluster       */
130    xlist_entry_t     copies_list;      /*! member of list of copies of same process        */
131
132        spinlock_t        th_lock;          /*! lock protecting th_tbl[] concurrent access      */
133        uint32_t          th_nr;            /*! number of threads in this cluster               */
134        struct thread_s * th_tbl[CONFIG_THREAD_MAX_PER_CLUSTER]; /*! pointers on local threads  */
135
136    xlist_entry_t     sem_root;         /*! root of the process semaphore list              */
137    xlist_entry_t     mutex_root;       /*! root of the process mutex list                  */
138    xlist_entry_t     barrier_root;     /*! root of the process barrier list                */
139    xlist_entry_t     condvar_root;     /*! root of the process condvar list                */
140
141    remote_spinlock_t sync_lock;        /*! lock protecting sem,mutex,barrier,condvar lists */
142}
143process_t;
144
145/*********************************************************************************************
146 * This structure defines the information required by the process_make_exec() function
147 * to create a new reference process descriptor, and the associated main thread.
148 ********************************************************************************************/
149
150typedef struct exec_info_s
151{
152    pid_t              pid;            /*! process identifier (both parent and child)       */
153
154    char               path[CONFIG_VFS_MAX_PATH_LENGTH];   /*!  .elf file path              */
155
156    char            ** args_pointers;  /*! physical base address of array of pointers       */
157    char             * args_buf_base;  /*! physical base address of kernel args buffer      */
158    uint32_t           args_nr;        /*! actual number of arguments                       */
159
160    char            ** envs_pointers;  /*! physical base address of array of pointers       */
161    char             * envs_buf_base;  /*! physical base address of kernel args buffer      */
162    char             * envs_buf_free;  /*! physical address of first free slot in envs_buf  */
163    uint32_t           envs_nr;        /*! actual number of environment variables           */
164}
165exec_info_t;
166
167/***************   Process Descriptor Operations    *****************************************/
168
169/*********************************************************************************************
170 * This function allocates memory in local cluster for a process descriptor.
171 *********************************************************************************************
172 * @ returns pointer on process descriptor if success / return NULL if failure
173 ********************************************************************************************/
174process_t * process_alloc();
175
176/*********************************************************************************************
177 * This function releases memory in local cluster for a process descriptor.
178 *********************************************************************************************
179 * @ process      : pointer on process descriptor to release.
180 ********************************************************************************************/
181void process_free( process_t * process );
182
183/*********************************************************************************************
184 * This function allocates memory and initializes the "process_init" descriptor and the
185 * associated "thread_init" descriptor. It is called once at the end of the kernel
186 * initialisation procedure, by the kernel process in cluster_IO.
187 * The "process_init" is the first user process, and all other user processes will be forked
188 * from this process. The code executed by "process_init" is stored in a .elf file, whose
189 * pathname is defined by the CONFIG_PROCESS_INIT_PATH configuration variable.
190 * The process_init streams are defined  by the CONFIG_INIT_[STDIN/STDOUT/STDERR] variables.
191 * Its local process identifier is 1, and parent process is the local kernel process_zero.
192 ********************************************************************************************/
193void process_init_create();
194
195/*********************************************************************************************
196 * This function initialize, in each cluster, the kernel "process_zero", that is the owner
197 * of all kernel threads in a given cluster. It is called by the kernel_init() function.
198 * Both the PID and PPID fields are set to zero, and the ref_xp is the local process_zero.
199 * The th_tbl[] is initialized as empty.
200 *********************************************************************************************
201 * @ process      : [in] pointer on local process descriptor to initialize.
202 ********************************************************************************************/
203void process_zero_init( process_t * process );
204
205/*********************************************************************************************
206 * This function initializes a local, reference user process descriptor from another process
207 * descriptor, defined by the <model_xp> argument. The <process> descriptor, the <pid>, and
208 * the <ppid> arguments must be previously defined by the caller.
209 * It can be called by two functions, depending on the process type:
210 * 1) if "process" is the "process_init", the parent is the kernel process. It is
211 *    called once, by the process_init_create() function in cluster[xmax-1][ymax-1].
212 * 2) if the caller is the process_make_fork() function, the model is generally a remote
213 *    process, that is also the parent process.
214
215 * 3) if the caller is the process_make_exec() function, the model is always a local process,
216 *    and the parent is the parent of the model process. DEPRECATED [AG]
217
218 * The following fields are initialised (for all process but process_zero).
219 * - It set the pid / ppid / ref_xp fields.
220 * - It initializes the VMM (register the kentry, args, envs vsegs in VSL)
221 * - It initializes the FDT, defining the three pseudo files STDIN / STDOUT / STDERR.
222 * - It set the root_xp, bin_xp, cwd_xp fields.
223 * - It reset the children list as empty, but does NOT register it in parent children list.
224 * - It reset the TH_TBL list of threads as empty.
225 * - It reset the semaphore / mutex / barrier / condvar lists as empty.
226 * - It registers the process in the local_list, rooted in the local cluster manager.
227 * - It registers the process in the copies_list, rooted in the owner cluster manager.
228 * - It registers the process extended pointer in the local pref_tbl[] array.
229 *********************************************************************************************
230 * @ process      : [in] pointer on local process descriptor to initialize.
231 * @ pid          : [in] process identifier.
232 * @ ppid         : [in] parent process identifier.
233 * @ model_xp     : [in] extended pointer on model process descriptor (local or remote).
234 ********************************************************************************************/
235void process_reference_init( process_t * process,
236                             pid_t       pid,
237                             pid_t       ppid,
238                             xptr_t      model_xp );
239
240/*********************************************************************************************
241 * This function initializes a copy process descriptor, in the local cluster,
242 * from information defined in the reference remote process descriptor.
243 *********************************************************************************************
244 * @ process              : [in] local pointer on process descriptor to initialize.
245 * @ reference_process_xp : [in] extended pointer on reference process descriptor.
246 * @ return 0 if success / return ENOMEM if failure
247 ********************************************************************************************/
248error_t process_copy_init( process_t * local_process,
249                           xptr_t      reference_process_xp );
250
251/*********************************************************************************************
252 * This function releases all memory allocated for a process descriptor in the local cluster,
253 * including memory allocated for embedded substructures (fd_array, vmm, etc).
254 * The local th_tbl[] array must be empty.
255 *********************************************************************************************
256 * @ process     : pointer on the process descriptor.
257 ********************************************************************************************/
258void process_destroy( process_t * process );
259
260/*********************************************************************************************
261 * This function returns a printable string defining the action for process_signa().
262 *********************************************************************************************
263 * @ action_type   : BLOCK_ALL_THREADS / UNBLOCK_ALL_THREADS / DELETE_ALL_THREADS
264 * @ return a string pointer.
265 ********************************************************************************************/
266char * process_action_str( uint32_t action_type );
267
268/*********************************************************************************************
269 * This function allows any thread running in any cluster to block, unblock  or delete
270 * all threads of a given process identified by the <process> argument, dependig on the
271 * <acion_type> argument.
272 * It can be called by the sys_kill() or sys_exit() functions to handle the "kill" & "exit"
273 * system calls, or by the process_make_exec() function to handle the "exec" system call.
274 * It must be executed in the owner cluster for the target process (using the relevant RPC
275 * (RPC_PROCESS_SIGNAL or RPC_PROCESS_EXEC) if the client thread in not running in the
276 * owner cluster.
277 * It uses the multicast, non blocking, RPC_PROCESS_KILL to send the signal to all process
278 * copies in parallel, block & deschedule when all signals have been sent, and finally
279 * returns only when all responses have been received and the operation is completed.
280 *********************************************************************************************
281 * @ process     : pointer on the process descriptor.
282 * @ action_type   : BLOCK_ALL_THREADS / UNBLOCK_ALL_THREADS / DELETE_ALL_THREADS
283 ********************************************************************************************/
284void process_sigaction( process_t * process,
285                        uint32_t    action_type );
286
287/*********************************************************************************************
288 * This function blocks all threads of a given user process in a given cluster.
289 * It is always called by a local RPC thread, through the multicast RPC_PROCESS_KILL.
290 * It loop on all local threads of the process, requesting the relevant schedulers to
291 * block and deschedule these threads, using IPI if required. The threads are not detached
292 * from the scheduler, and not detached from the local process.
293 * It acknowledges the client thread in the owner cluster only when all process threads
294 * are descheduled and blocked on the BLOCKED_GLOBAL condition, using the <rsp_xp> argument.
295 *********************************************************************************************
296 * @ process     : pointer on the target process descriptor.
297 * @ rsp_xp      : extended pointer on the response counter.
298 * # client_xp   : extended pointer on client thread descriptor.
299 ********************************************************************************************/
300void process_block( process_t * process,
301                    xptr_t      rsp_xp,
302                    xptr_t      client_xp );
303
304/*********************************************************************************************
305 * This function unblocks all threads of a given user process in a given cluster.
306 * It is always called by a local RPC thread, through the multicast RPC_PROCESS_KILL.
307 * It loops on local threads of the process, to reset the BLOCKED_GLOBAL bit in all threads.
308 * It acknowledges directly the client thread in the owner cluster when this is done,
309 * using the <rsp_xp> argument.
310 *********************************************************************************************
311 * @ process     : pointer on the process descriptor.
312 * @ rsp_xp      : extended pointer on the response counter.
313 * # client_xp   : extended pointer on client thread descriptor.
314 ********************************************************************************************/
315void process_unblock( process_t * process,
316                      xptr_t      rsp_xp,
317                      xptr_t      client_xp );
318
319/*********************************************************************************************
320 * This function delete all threads descriptors, of given user process in a given cluster.
321 * It is always called by a local RPC thread, through the multicast RPC_PROCESS_KILL.
322 * It detach all process threads from the scheduler, detach the threads from the local
323 * process, and release the local memory allocated to threads descriptors (including the
324 * associated structures such as CPU and FPU context). Finally, it release the memory
325 * allocated to the local process descriptor itself, but only when the local cluster
326 * is NOT the process owner, but only a copy.  It acknowledges directly the client thread
327 * in the owner cluster, using ithe <rsp_xp> argument.
328 *********************************************************************************************
329 * @ process     : pointer on the process descriptor.
330 * @ rsp_xp      : extended pointer on the response counter.
331 * # client_xp   : extended pointer on client thread descriptor.
332 ********************************************************************************************/
333void process_delete( process_t * process,
334                     xptr_t      rsp_xp,
335                     xptr_t      client_xp );
336
337/*********************************************************************************************
338 * This function returns a pointer on the local copy of a process identified by its PID.
339 * If this local copy does not exist yet, it is dynamically created, from the reference
340 * process descriptor, registered in the global copies_list, and registered in the local_list.
341 * This function is used by the thread_user_create() function.
342 *********************************************************************************************
343 * @ pid     : searched process identifier.
344 * @ returns pointer on the local process descriptor if success / returns NULL if failure.
345 ********************************************************************************************/
346process_t * process_get_local_copy( pid_t pid );
347
348/*********************************************************************************************
349 * This function implements the "exec" system call, and is called by the sys_exec() function.
350 * The "new" process keep the "old" process PID and PPID, all open files, and env variables,
351 * the vfs_root and vfs_cwd, but build a brand new memory image (new VMM from the new .elf).
352 * It actually creates a "new" reference process descriptor, and copies all relevant
353 * information from the "old" process descriptor to the "new" process descriptor.
354 * It completes the "new" process descriptor, from information found in the <exec_info>
355 * structure (defined in the process.h file), that must be built by the caller.
356 * It creates and initializes the associated main thread. It finally destroys all copies
357 * of the "old" process in all clusters, and destroys all old associated threads.
358 * It is executed in the local cluster, that becomes both the "owner" and the "reference"
359 * cluster for the "new" process.
360 *********************************************************************************************
361 * @ exec_info   : [in]  pointer on the exec_info structure.
362 * @ return 0 if success / return non-zero if error.
363 ********************************************************************************************/
364error_t process_make_exec( exec_info_t * exec_info );
365
366/*********************************************************************************************
367 * This function implements the "fork" system call, and is called by the sys_fork() function.
368 * It allocates memory and initializes a new "child" process descriptor, and the
369 * associated "child" thread descriptor in the local cluster. This function can involve
370 * up to three different clusters :
371 * - the local (child) cluster can be any cluster defined by the sys_fork function.
372 * - the parent cluster must be the reference cluster for the parent process.
373 * - the client cluster containing the thread requesting the fork can be any cluster.
374 * The new "child" process descriptor is initialised from informations found in the "parent"
375 * reference process descriptor, containing the complete process description.
376 * The new "child" thread descriptor is initialised from informations found in the "parent"
377 * thread descriptor.
378 *********************************************************************************************
379 * @ parent_process_xp  : extended pointer on the reference parent process.
380 * @ parent_thread_xp   : extended pointer on the parent thread requesting the fork.
381 * @ child_pid          : [out] child process identifier.
382 * @ child_thread_ptr   : [out] local pointer on child thread in target cluster.
383 * @ return 0 if success / return non-zero if error.
384 ********************************************************************************************/
385error_t process_make_fork(  xptr_t             parent_process_xp,
386                            xptr_t             parent_thread_xp,
387                            pid_t            * child_pid, 
388                            struct thread_s ** child_thread_ptr );
389
390/*********************************************************************************************
391 * This function implement the "exit" system call, and is called by the sys_exit() function.
392 * It must be executed by a thread running in the calling process owner cluster.
393 * It uses twice the multicast RPC_PROCESS_SIGNAL to first block all process threads
394 * in all clusters, and then delete all thread  and process descriptors.
395 *********************************************************************************************
396 * @ process  : pointer on process descriptor in owner cluster.
397 * @ status   : exit return value.
398 ********************************************************************************************/
399void process_make_exit( process_t * process,
400                        uint32_t    status );
401
402/*********************************************************************************************
403 * This function implement the "kill" system call, and is called by the sys_kill() function.
404 * It must be executed by a thread running in the target process owner cluster.
405 * Only the SIGKILL, SIGSTOP, and SIGCONT signals are supported.
406 * User defined handlers are not supported.
407 * It uses once or twice the multicast RPC_PROCESS_SIGNAL to block, unblock or delete
408 * all process threads in all clusters, and then delete process descriptors.
409 *********************************************************************************************
410 * @ process  : pointer on process descriptor in owner cluster.
411 * @ sig_id   : signal type.
412 ********************************************************************************************/
413void process_make_kill( process_t * process,
414                        uint32_t    sig_id );
415
416
417/********************   File Management Operations   ****************************************/
418
419/*********************************************************************************************
420 * This function initializes all entries of the local fd_array as empty.
421 *********************************************************************************************
422 * @ process  : pointer on the local process descriptor.
423 ********************************************************************************************/
424void process_fd_init( process_t * process );
425
426/*********************************************************************************************
427 * This function uses as many remote accesses as required, to reset an entry in fd_array[],
428 * in all clusters containing a copy. The entry is identified by the <fdid> argument.
429 * This function must be executed by a thread running reference cluster, that contains
430 * the complete list of process descriptors copies.
431 *********************************************************************************************
432 * @ process  : pointer on the local process descriptor.
433 * @ fdid     : file descriptor index in the fd_array.
434 ********************************************************************************************/
435void process_fd_remove( process_t * process,
436                        uint32_t    fdid );
437
438/*********************************************************************************************
439 * This function returns an extended pointer on a file descriptor identified by its index
440 * in fd_array. It can be called by any thread running in any cluster.
441 * It accesses first the local process descriptor. In case of local miss, it uses remote
442 * access to access the reference process descriptor.
443 * It updates the local fd_array when the file descriptor exists in reference cluster.
444 * The file descriptor refcount is not incremented.
445 *********************************************************************************************
446 * @ process  : pointer on the local process descriptor.
447 * @ fdid     : file descriptor index in the fd_array.
448 * @ return extended pointer on file descriptor if success / return XPTR_NULL if not found.
449 ********************************************************************************************/
450xptr_t process_fd_get_xptr( process_t * process,
451                            uint32_t    fdid );
452
453/*********************************************************************************************
454 * This function checks the number of open files for a given process.
455 * It can be called by any thread in any cluster, because it uses portable remote access
456 * primitives to access the reference process descriptor.
457 *********************************************************************************************
458 * @ returns true if file descriptor array full.
459 ********************************************************************************************/
460bool_t process_fd_array_full();
461
462/*********************************************************************************************
463 * This function allocates a free slot in the fd_array of the reference process,
464 * register the <file_xp> argument in the allocated slot, and return the slot index.
465 * It can be called by any thread in any cluster, because it uses portable remote access
466 * primitives to access the reference process descriptor.
467 *********************************************************************************************
468 * @ file_xp  : extended pointer on the file descriptor to be registered.
469 * @ fdid     : [out] buffer for fd_array slot index.
470 * @ return 0 if success / return EMFILE if array full.
471 ********************************************************************************************/
472error_t process_fd_register( process_t * process,
473                             xptr_t      file_xp,
474                             uint32_t  * fdid );
475
476/*********************************************************************************************
477 * This function copies all non-zero entries (other than the three first stdin/stdout/stderr)
478 * from a remote <src_xp> fd_array, embedded in a process descriptor, to another remote
479 * <dst_xp> fd_array, embedded in another process descriptor.
480 * The calling thread can be running in any cluster.
481 * It takes the remote lock protecting the <src_xp> fd_array during the copy.
482 * For each involved file descriptor, the refcount is incremented.
483 *********************************************************************************************
484 * @ dst_xp   : extended pointer on the destination fd_array_t.
485 * @ src_xp   : extended pointer on the source fd_array_t.
486 ********************************************************************************************/
487void process_fd_remote_copy( xptr_t dst_xp,
488                             xptr_t src_xp );
489
490
491
492/********************   Thread Related Operations   *****************************************/
493
494/*********************************************************************************************
495 * This function registers a new thread in the local process descriptor.
496 * It checks that there is an available slot in the local th_tbl[] array,
497 * allocates a new LTID, and registers the new thread in the th_tbl[].
498 * WARNING : the lock protecting the th_tbl[] must be taken by the caller.
499 *********************************************************************************************
500 * @ process  : pointer on the local process descriptor.
501 * @ thread   : pointer on new thread to be registered.
502 * @ trdid    : [out] address of buffer for allocated trdid.
503 * @ returns 0 if success / returns non zero if no slot available.
504 ********************************************************************************************/
505error_t process_register_thread( process_t       * process,
506                                 struct thread_s * thread,
507                                 trdid_t         * trdid );
508
509/*********************************************************************************************
510 * This function removes a thread registration from the local process descriptor.
511 * WARNING : the lock protecting the th_tbl[] must be taken by the caller.
512 *********************************************************************************************
513 * @ thread   : local pointer on thread to be removed.
514 ********************************************************************************************/
515void process_remove_thread( struct thread_s * thread );
516
517
518
519#endif  /* _PROCESS_H_ */
Note: See TracBrowser for help on using the repository browser.