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

Last change on this file since 8 was 1, checked in by alain, 8 years ago

First import

File size: 22.0 KB
RevLine 
[1]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)
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 <almos_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 * These macros are used to compose or decompose global thread identifier (TRDID)
57 * to or from cluster identifier / local thread index (CXY , LTID)
58 ********************************************************************************************/
59
60#define LTID_FROM_TRDID( trdid )   (ltid_t)(trdid & 0x0000FFFF)
61#define CXY_FROM_TRDID( trdid )    (cxy_t)(trdid >> 16)
62#define TRDID( cxy , ltid )        (trdid_t)((cxy << 16) | ltid )
63
64/*********************************************************************************************
65 * This structure defines an array of extended pointers on the open file descriptors
66 * for a given process. We use an extended pointer because the open file descriptor
67 * is always stored in the same cluster as the inode associated to the file.
68 * A free entry in this array contains the XPTR_NULL value.
69 * The array size is defined by a the CONFIG_PROCESS_FILE_MAX_NR parameter.
70 * All modifications (open/close) must be done by the reference cluster, and reported
71 ********************************************************************************************/
72
73typedef struct fd_array_s
74{
75        remote_spinlock_t lock;             /*! lock protecting fd_array[] change.              */
76    uint32_t          current;          /*! current number of open files                    */
77    uint32_t          max;              /*! max number of open files (can be increased)     */
78        xptr_t            array[CONFIG_PROCESS_FILE_MAX_NR]; 
79}
80fd_array_t;
81
82/*********************************************************************************************
83 * This structure defines a process descriptor.
84 * A process is identified by a unique PID (process identifier):
85 * - The PID 16 LSB bits contain the LPID (Local Process Index)
86 * - The PID 16 MSB bits contain the owner cluster CXY.
87 * In each cluster, the process manager allocates LPID values for the process that are
88 * allocated to this cluster.
89 * The process descriptor for a PID process is replicated in all clusters containing
90 * at least one thread of the PID process, with some restrictions:
91 * - The list of registered vsegs, the page table (contained in vmm), and the fd_array[]
92 *   are only complete in the reference process descriptor, other copies are read-only caches.
93 * - The following fields are NOT defined in copies other than the reference:
94 *   children_root , children_list , children_nr , sig_mgr
95 ********************************************************************************************/
96
97typedef struct process_s
98{
99        vmm_t            vmm;               /*! embedded virtual memory manager                 */
100
101        fd_array_t       fd_array;          /*! embedded file descriptors array                 */
102
103        xptr_t           vfs_root_xp;       /*! extende pointer on current file system root     */
104        xptr_t           vfs_cwd_xp;        /*! extended pointer on current working directory   */
105        xptr_t           vfs_bin_xp;        /*! extended pointer on associate .elf file         */
106
107        spinlock_t       cd_lock;           /*! lock protecting working directory changes       */
108 
109        pid_t            pid;               /*! process identifier                              */
110        pid_t            ppid;              /*! parent process identifier                       */
111        bool_t           is_ref;            /*! this is the reference process if true           */
112    xptr_t           ref_xp;            /*! extended pointer on reference process           */
113
114        xlist_entry_t    children_root;     /*! root of the children process xlist              */
115    uint32_t         children_nr;       /*! number of children processes                    */
116
117        xlist_entry_t    brothers_list;     /*! member of list of children of same parent       */
118
119    list_entry_t     local_list;        /*! member of list of process in same cluster       */
120
121    xlist_entry_t    copies_list;       /*! member of list of copies of same process        */
122
123        spinlock_t       th_lock;           /*! lock protecting th_tbl[] concurrent access      */
124        uint32_t         th_nr;             /*! number of threads in this cluster               */
125
126        struct thread_s * th_tbl[CONFIG_THREAD_MAX_PER_CLUSTER]; /*! pointers on local threads  */
127
128    xlist_entry_t    sem_root;          /*! root of the process semaphores list             */
129    uint32_t         sem_nr;            /*! number of semaphores                            */
130
131        sig_mgr_t        sig_mgr;           /*! embedded signal manager TODO [AG]               */
132}
133process_t;
134
135/*********************************************************************************************
136 * This structure defines the minimal information required by the process_make_exec()
137 * function to build a new reference process descriptor, and the associated main thread.
138 ********************************************************************************************/
139
140typedef struct exec_info_s
141{
142    pid_t              pid;            /*! process identifier                               */
143    pid_t              ppid;           /*! parent process identifier                        */
144
145    xptr_t             fd_array_xp;    /*! extended pointer on parent process fd_array      */
146
147    xptr_t             vfs_root_xp;    /*! extended pointer on file system root             */
148    xptr_t             vfs_cwd_xp;     /*! extended pointer on current working directory    */
149    xptr_t             vfs_bin_xp;     /*! extended pointer on process .elf file            */
150
151    char               path[256];      /*! pathname for  .elf file                          */
152
153    char            ** args_pointers;  /*! physical base address of array of pointers       */
154    char             * args_buf_base;  /*! physical base address of kernel args buffer      */
155    uint32_t           args_nr;        /*! actual number of arguments                       */
156
157    char            ** envs_pointers;  /*! physical base address of array of pointers       */
158    char             * envs_buf_base;  /*! physical base address of kernel args buffer      */
159    char             * envs_buf_free;  /*! physical address of first free slot in envs_buf  */
160    uint32_t           envs_nr;        /*! actual number of environment variables           */
161}
162exec_info_t;
163
164/*********************************************************************************************
165 * This macro returns a pointer on the process descriptor for the calling thread.
166 ********************************************************************************************/
167
168#define CURRENT_PROCESS (hal_get_current_thread()->process)
169
170/***************   Process Descriptor Operations    *****************************************/
171
172/*********************************************************************************************
173 * This function allocates memory in local cluster for a process descriptor.
174 *********************************************************************************************
175 * @ returns pointer on process descriptor if success / return NULL if failure
176 ********************************************************************************************/
177process_t * process_alloc();
178
179/*********************************************************************************************
180 * This function releases memory in local cluster for a process descriptor.
181 *********************************************************************************************
182 * @ process      : pointer on process descriptor to release.
183 ********************************************************************************************/
184void process_free( process_t * process );
185
186/*********************************************************************************************
187 * This function initializes the kernel process_zero descriptor (global variable) from
188 * informations found in the boot_info.
189 *********************************************************************************************
190 * @ info      : pointer on local boot_info_t structure.
191 ********************************************************************************************/
192void process_zero_init( boot_info_t * info );
193
194/*********************************************************************************************
195 * This function allocates memory and initialises the "process_init" descriptor and the
196 * associated thread descriptor from information found in the "process_zero" descriptor.
197 *********************************************************************************************
198 * Any error gives a kernel panic.
199 ********************************************************************************************/
200void process_init_create();
201
202/*********************************************************************************************
203 * This function intializes a new process descriptor, in the reference cluster.
204 * The PID value must have been defined previously by the owner cluster manager.
205 * The reference cluster can be different from the owner cluster.
206 * It set the pid / ppid / is_ref / pref / fields.
207 * It registers this process descriptor in three lists:
208 * - the children_list in the parent process descriptor.
209 * - the local_list, rooted in the reference cluster manager.
210 * - the copies_list, rooted in the owner cluster manager.
211 * It reset the embedded structures such as the VMM or the file descriptor array.
212 *********************************************************************************************
213 * @ process      : [in] pointer on process descriptor to initialize.
214 * @ pid          : [in] process identifier defined by owner cluster.
215 * @ ppid         : [in] parent process identifier.
216 ********************************************************************************************/
217void process_reference_init( process_t * process,
218                             pid_t       pid,
219                             pid_t       ppid );
220
221/*********************************************************************************************
222 * This function intializes a copy process descriptor, in the local cluster,
223 * from informations defined in the reference remote process descriptor.
224 *********************************************************************************************
225 * @ process              : [in] local pointer on process descriptor to initialize.
226 * @ reference_process_xp : [in] extended pointer on reference process descriptor.
227 * @ return 0 if success / return ENOMEM if failure
228 ********************************************************************************************/
229error_t process_copy_init( process_t * local_process,
230                           xptr_t      reference_process_xp );
231
232/*********************************************************************************************
233 * This function releases all memory allocated for a process descriptor in the local cluster,
234 * after releasing memory allocated for embedded substructures (fd_array, vmm, etc).
235 * The local th_tbl[] array must be empty.
236 *********************************************************************************************
237 * @ process     : pointer on the process descriptor.
238 ********************************************************************************************/
239void process_destroy( process_t * process );
240
241/*********************************************************************************************
242 * This function kills an user process in a given cluster.
243 * It can be directly called in the reference cluster, or it can be called through the
244 * PROCESS_KILL RPC.
245 * - In a first loop, it set the THREAD_SIG_KILL signal to all threads of process.
246 * - In a second loop, it wait, for each thread the reset of the THREAD_SIG_KILL signal
247 *   by the scheduler, and completes the thread descriptor destruction.
248 *********************************************************************************************
249 * @ process     : pointer on the process descriptor.
250 ********************************************************************************************/
251void process_kill( process_t * process );
252
253/*********************************************************************************************
254 * This function returns a pointer on the local copy of a process identified by its PID.
255 * If this local copy does not exist yet, it is dynamically created, from the reference
256 * process descriptor, registered in the global copies_list, and registered in the local_list.
257 * This function is used by both thread_user_create() and thread_user_copy() functions.
258 *********************************************************************************************
259 * @ pid     : searched process identifier.
260 * @ returns pointer on the local process descriptor if success / returns NULL if failure.
261 ********************************************************************************************/
262process_t * process_get_local_copy( pid_t pid );
263
264/*********************************************************************************************
265 * This function builds a new reference process descriptor and associated main thread.
266 * It is executed in the local cluster, that becomes both "owner" and "reference".
267 * The new process descriptor and the main_thread are initialised using only informations
268 * found in the local exec_info structure, that must be build by the caller.
269 * - It can be called by the process_init_create() function to build the "init" process.
270 * - It can be called directly by the sys_exec() function in case of local exec.
271 * - It can be called through the rpc_process_exec_server() function in case of remote exec.
272 *********************************************************************************************
273 * @ exec_info   : [in]  pointer on the exec_info structure.
274 * @ return 0 if success / return non-zero if error.
275 ********************************************************************************************/
276error_t process_make_exec( exec_info_t * exec_info );
277
278
279/********************   Signal Management Operations   **************************************/ 
280
281/*********************************************************************************************
282 * This function TODO [AG]
283 ********************************************************************************************/
284void process_signal_handler( process_t * process );
285
286
287/********************   File Management Operations   ****************************************/ 
288
289/*********************************************************************************************
290 * This function reset the file descriptor array for a given process: no open file.
291 *********************************************************************************************
292 * @ process  : pointer on the local process descriptor.
293 ********************************************************************************************/
294void process_fd_init( process_t * process );
295
296/*********************************************************************************************
297 * This function closes all open files and releases all file descriptors.
298 *********************************************************************************************
299 * @ process  : pointer on the local process descriptor.
300 ********************************************************************************************/
301void process_fd_destroy( process_t * process );
302
303/*********************************************************************************************
304 * This function checks the number of open files for a given process.
305 *********************************************************************************************
306 * @ process  : pointer on the local process descriptor.
307 * @ returns true if file descriptor array full.
308 ********************************************************************************************/
309bool_t process_fd_array_full( process_t * process );
310
311/*********************************************************************************************
312 * These functions allocates a slot in the fd_array for a given process,
313 * and returns the file descriptor index.
314 *********************************************************************************************
315 * @ process  : pointer on the local process descriptor.
316 * @ file     : extended pointer on the file descriptor.
317 * @ fd       : [out] file descriptor index
318 * @ return 0 if success / return EMFILE if array full.
319 ********************************************************************************************/
320error_t process_fd_allocate( process_t * process, 
321                             xptr_t      file_xp, 
322                             uint32_t  * fd );
323
324/*********************************************************************************************
325 * This function releases an existing file descriptor from the process fd_array.
326 *********************************************************************************************
327 * @ process  : pointer on the local process descriptor.
328 * @ return 0 if success / return EBADF if illegal file descriptor.
329 ********************************************************************************************/
330error_t process_fd_release( process_t * process,
331                            uint32_t    fd );
332
333/*********************************************************************************************
334 * This function copies all non-zero entries from a local <src> fd_array,
335 * embedded in a process descriptor, to another local <dst_xp> fd_array, embedded
336 * in another process descriptor.
337 * It takes the remote lock protecting the <src> fd_array during the copy.
338 * For each involved file descriptor, the refcount is incremented.
339 *********************************************************************************************
340 * @ dst   : pointer on the destination fd_array_t.
341 * @ src   : pointer on the source fd_array_t.
342 ********************************************************************************************/
343void process_fd_local_copy( fd_array_t * dst,
344                            fd_array_t * src );
345
346/*********************************************************************************************
347 * This function copies all non-zero entries from a remote <src_xp> fd_array,
348 * embedded in a process descriptor, to another remote <dst_xp> fd_array, embedded
349 * in another process descriptor. The calling thread can be running in any cluster.
350 * It takes the remote lock protecting the <src_xp> fd_array during the copy.
351 * For each involved file descriptor, the refcount is incremented.
352 *********************************************************************************************
353 * @ dst_xp   : extended pointer on the destination fd_array_t.
354 * @ src_xp   : extended pointer on the source fd_array_t.
355 ********************************************************************************************/
356void process_fd_remote_copy( xptr_t dst_xp,
357                             xptr_t src_xp );
358
359
360/********************   Thread Related Operations   *****************************************/ 
361
362/*********************************************************************************************
363 * This function registers a new thread in the local process descriptor.
364 * It checks that there is an available slot in the local th_tbl[] array,
365 * allocates a new LTID, and registers the new thread in the th_tbl[].
366 * WARNING : the lock protecting the th_tbl[] must be taken by the caller.
367 *********************************************************************************************
368 * @ process  : pointer on the local process descriptor.
369 * @ thread   : pointer on new thread to be registered.
370 * @ trdid    : [out] address of buffer for allocated trdid.
371 * @ returns 0 if success / returns non zero if no slot available.
372 ********************************************************************************************/
373error_t process_register_thread( process_t       * process,
374                                 struct thread_s * thread,
375                                 trdid_t         * trdid );
376
377/*********************************************************************************************
378 * This function removes a thread registration from the local process descriptor.
379 * WARNING : the lock protecting the th_tbl[] must be taken by the caller.
380 *********************************************************************************************
381 * @ thread   : local pointer on thread to be removed.
382 ********************************************************************************************/
383void process_remove_thread( struct thread_s * thread );
384
385
386
387#endif  /* _PROCESS_H_ */
Note: See TracBrowser for help on using the repository browser.