source: trunk/kernel/kern/process.h

Last change on this file was 683, checked in by alain, 11 months ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

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