source: trunk/kernel/kern/process.c @ 564

Last change on this file since 564 was 564, checked in by alain, 3 years ago

Complete restructuration of kernel locks.

File size: 76.8 KB
Line 
1/*
2 * process.c - 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)
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#include <kernel_config.h>
27#include <hal_kernel_types.h>
28#include <hal_remote.h>
29#include <hal_uspace.h>
30#include <hal_irqmask.h>
31#include <errno.h>
32#include <printk.h>
33#include <memcpy.h>
34#include <bits.h>
35#include <kmem.h>
36#include <page.h>
37#include <vmm.h>
38#include <vfs.h>
39#include <core.h>
40#include <thread.h>
41#include <chdev.h>
42#include <list.h>
43#include <string.h>
44#include <scheduler.h>
45#include <busylock.h>
46#include <queuelock.h>
47#include <remote_queuelock.h>
48#include <rwlock.h>
49#include <remote_rwlock.h>
50#include <dqdt.h>
51#include <cluster.h>
52#include <ppm.h>
53#include <boot_info.h>
54#include <process.h>
55#include <elf.h>
56#include <syscalls.h>
57#include <shared_syscalls.h>
58
59//////////////////////////////////////////////////////////////////////////////////////////
60// Extern global variables
61//////////////////////////////////////////////////////////////////////////////////////////
62
63extern process_t           process_zero;     // allocated in kernel_init.c
64extern chdev_directory_t   chdev_dir;        // allocated in kernel_init.c
65
66//////////////////////////////////////////////////////////////////////////////////////////
67// Process initialisation related functions
68//////////////////////////////////////////////////////////////////////////////////////////
69
70///////////////////////////
71process_t * process_alloc( void )
72{
73        kmem_req_t   req;
74
75    req.type  = KMEM_PROCESS;
76        req.size  = sizeof(process_t);
77        req.flags = AF_KERNEL;
78
79    return (process_t *)kmem_alloc( &req );
80}
81
82////////////////////////////////////////
83void process_free( process_t * process )
84{
85    kmem_req_t  req;
86
87        req.type = KMEM_PROCESS;
88        req.ptr  = process;
89        kmem_free( &req );
90}
91
92/////////////////////////////////////////////////
93void process_reference_init( process_t * process,
94                             pid_t       pid,
95                             xptr_t      parent_xp )
96{
97    cxy_t       parent_cxy;
98    process_t * parent_ptr;
99    xptr_t      stdin_xp;
100    xptr_t      stdout_xp;
101    xptr_t      stderr_xp;
102    uint32_t    stdin_id;
103    uint32_t    stdout_id;
104    uint32_t    stderr_id;
105    error_t     error;
106    uint32_t    txt_id;
107    char        rx_path[40];
108    char        tx_path[40];
109    xptr_t      file_xp;
110    xptr_t      chdev_xp;
111    chdev_t *   chdev_ptr;
112    cxy_t       chdev_cxy;
113    pid_t       parent_pid;
114
115    // get parent process cluster and local pointer
116    parent_cxy = GET_CXY( parent_xp );
117    parent_ptr = GET_PTR( parent_xp );
118
119    // get parent_pid
120    parent_pid = hal_remote_l32( XPTR( parent_cxy , &parent_ptr->pid ) );
121
122#if DEBUG_PROCESS_REFERENCE_INIT
123uint32_t cycle = (uint32_t)hal_get_cycles();
124if( DEBUG_PROCESS_REFERENCE_INIT )
125printk("\n[DBG] %s : thread %x in process %x enter to initalialize process %x / cycle %d\n",
126__FUNCTION__, CURRENT_THREAD->trdid, parent_pid , pid , cycle );
127#endif
128
129    // initialize PID, REF_XP, PARENT_XP, and STATE
130        process->pid        = pid;
131    process->ref_xp     = XPTR( local_cxy , process );
132    process->owner_xp   = XPTR( local_cxy , process );
133    process->parent_xp  = parent_xp;
134    process->term_state = 0;
135
136    // initialize vmm as empty
137    error = vmm_init( process );
138
139assert( (error == 0) , "cannot initialize VMM\n" );
140 
141#if (DEBUG_PROCESS_REFERENCE_INIT & 1)
142cycle = (uint32_t)hal_get_cycles();
143if( DEBUG_PROCESS_REFERENCE_INIT )
144printk("\n[DBG] %s : thread %x in process %x / vmm empty for process %x / cycle %d\n", 
145__FUNCTION__, CURRENT_THREAD->trdid, parent_pid , pid, cycle );
146#endif
147
148    // initialize fd_array as empty
149    process_fd_init( process );
150
151    // define the stdin/stdout/stderr pseudo files <=> select a TXT terminal.
152    if( (pid == 1) || (parent_pid == 1)) // INIT or KSH process
153    {
154        // allocate a TXT channel
155        if( pid == 1 )  txt_id = 0;                     // INIT
156        else            txt_id = process_txt_alloc();   // KSH
157
158        // attach process to TXT
159        process_txt_attach( process , txt_id ); 
160
161#if (DEBUG_PROCESS_REFERENCE_INIT & 1)
162cycle = (uint32_t)hal_get_cycles();
163if( DEBUG_PROCESS_REFERENCE_INIT )
164printk("\n[DBG] %s : thread %x in process %x / process %x attached to TXT%d / cycle %d\n", 
165__FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, txt_id, cycle );
166#endif
167        // build path to TXT_RX[i] and TXT_TX[i] chdevs
168        snprintf( rx_path , 40 , "/dev/external/txt%d_rx", txt_id );
169        snprintf( tx_path , 40 , "/dev/external/txt%d_tx", txt_id );
170
171        // create stdin pseudo file         
172        error = vfs_open( process,
173                           rx_path,
174                           O_RDONLY, 
175                           0,                // FIXME chmod
176                           &stdin_xp, 
177                           &stdin_id );
178
179assert( (error == 0) , "cannot open stdin pseudo file" );
180assert( (stdin_id == 0) , "stdin index must be 0" );
181
182#if (DEBUG_PROCESS_REFERENCE_INIT & 1)
183cycle = (uint32_t)hal_get_cycles();
184if( DEBUG_PROCESS_REFERENCE_INIT )
185printk("\n[DBG] %s : thread %x in process %x / stdin open for process %x / cycle %d\n", 
186__FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, cycle );
187#endif
188
189        // create stdout pseudo file         
190        error = vfs_open( process,
191                           tx_path,
192                           O_WRONLY, 
193                           0,                // FIXME chmod
194                           &stdout_xp, 
195                           &stdout_id );
196
197        assert( (error == 0) , "cannot open stdout pseudo file" );
198        assert( (stdout_id == 1) , "stdout index must be 1" );
199
200#if (DEBUG_PROCESS_REFERENCE_INIT & 1)
201cycle = (uint32_t)hal_get_cycles();
202if( DEBUG_PROCESS_REFERENCE_INIT )
203printk("\n[DBG] %s : thread %x in process %x / stdout open for process %x / cycle %d\n", 
204__FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, cycle );
205#endif
206
207        // create stderr pseudo file         
208        error = vfs_open( process,
209                           tx_path,
210                           O_WRONLY, 
211                           0,                // FIXME chmod
212                           &stderr_xp, 
213                           &stderr_id );
214
215        assert( (error == 0) , "cannot open stderr pseudo file" );
216        assert( (stderr_id == 2) , "stderr index must be 2" );
217
218#if (DEBUG_PROCESS_REFERENCE_INIT & 1)
219cycle = (uint32_t)hal_get_cycles();
220if( DEBUG_PROCESS_REFERENCE_INIT )
221printk("\n[DBG] %s : thread %x in process %x / stderr open for process %x / cycle %d\n", 
222__FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, cycle );
223#endif
224
225    }
226    else                                            // normal user process
227    {
228        // get extended pointer on stdin pseudo file in parent process
229        file_xp = (xptr_t)hal_remote_l64( XPTR( parent_cxy , &parent_ptr->fd_array.array[0] ) );
230
231        // get extended pointer on parent process TXT chdev
232        chdev_xp = chdev_from_file( file_xp );
233 
234        // get cluster and local pointer on chdev
235        chdev_cxy = GET_CXY( chdev_xp );
236        chdev_ptr = GET_PTR( chdev_xp );
237 
238        // get parent process TXT terminal index
239        txt_id = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) );
240
241        // attach child process to parent process TXT terminal
242        process_txt_attach( process , txt_id ); 
243
244        // copy all open files from parent process fd_array to this process
245        process_fd_remote_copy( XPTR( local_cxy , &process->fd_array ),
246                                XPTR( parent_cxy , &parent_ptr->fd_array ) );
247    }
248
249    // initialize specific inodes root and cwd
250    process->vfs_root_xp = (xptr_t)hal_remote_l64( XPTR( parent_cxy,
251                                                         &parent_ptr->vfs_root_xp ) );
252    process->vfs_cwd_xp  = (xptr_t)hal_remote_l64( XPTR( parent_cxy,
253                                                         &parent_ptr->vfs_cwd_xp ) );
254    vfs_inode_remote_up( process->vfs_root_xp );
255    vfs_inode_remote_up( process->vfs_cwd_xp );
256
257    remote_rwlock_init( XPTR( local_cxy , &process->cwd_lock ), LOCK_PROCESS_CWD );
258
259#if (DEBUG_PROCESS_REFERENCE_INIT & 1)
260cycle = (uint32_t)hal_get_cycles();
261if( DEBUG_PROCESS_REFERENCE_INIT )
262printk("\n[DBG] %s : thread %x in process %x / set fd_array for process %x / cycle %d\n", 
263__FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid , cycle );
264#endif
265
266    // reset children list root
267    xlist_root_init( XPTR( local_cxy , &process->children_root ) );
268    process->children_nr     = 0;
269    remote_queuelock_init( XPTR( local_cxy , &process->children_lock ), LOCK_PROCESS_CHILDREN );
270
271    // reset semaphore / mutex / barrier / condvar list roots
272    xlist_root_init( XPTR( local_cxy , &process->sem_root ) );
273    xlist_root_init( XPTR( local_cxy , &process->mutex_root ) );
274    xlist_root_init( XPTR( local_cxy , &process->barrier_root ) );
275    xlist_root_init( XPTR( local_cxy , &process->condvar_root ) );
276    remote_queuelock_init( XPTR( local_cxy , &process->sync_lock ), LOCK_PROCESS_USERSYNC );
277
278    // register new process in the local cluster manager pref_tbl[]
279    lpid_t lpid = LPID_FROM_PID( pid );
280    LOCAL_CLUSTER->pmgr.pref_tbl[lpid] = XPTR( local_cxy , process );
281
282    // register new process descriptor in local cluster manager local_list
283    cluster_process_local_link( process );
284
285    // register new process descriptor in local cluster manager copies_list
286    cluster_process_copies_link( process );
287
288    // initialize th_tbl[] array and associated threads
289    uint32_t i;
290
291    for( i = 0 ; i < CONFIG_THREADS_MAX_PER_CLUSTER ; i++ )
292        {
293        process->th_tbl[i] = NULL;
294    }
295    process->th_nr  = 0;
296    rwlock_init( &process->th_lock , LOCK_PROCESS_THTBL );
297
298        hal_fence();
299
300#if (DEBUG_PROCESS_REFERENCE_INIT & 1)
301cycle = (uint32_t)hal_get_cycles();
302if( DEBUG_PROCESS_REFERENCE_INIT )
303printk("\n[DBG] %s : thread %x in process %x exit for process %x / cycle %d\n", 
304__FUNCTION__, CURRENT_THREAD->trdid, parent_pid, pid, cycle );
305#endif
306
307}  // process_reference_init()
308
309/////////////////////////////////////////////////////
310error_t process_copy_init( process_t * local_process,
311                           xptr_t      reference_process_xp )
312{
313    error_t error;
314
315    // get reference process cluster and local pointer
316    cxy_t       ref_cxy = GET_CXY( reference_process_xp );
317    process_t * ref_ptr = GET_PTR( reference_process_xp );
318
319    // initialize PID, REF_XP, PARENT_XP, and STATE
320    local_process->pid        = hal_remote_l32(  XPTR( ref_cxy , &ref_ptr->pid ) );
321    local_process->parent_xp  = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->parent_xp ) );
322    local_process->ref_xp     = reference_process_xp;
323    local_process->owner_xp   = reference_process_xp;
324    local_process->term_state = 0;
325
326#if DEBUG_PROCESS_COPY_INIT
327thread_t * this = CURRET_THREAD; 
328uint32_t cycle = (uint32_t)hal_get_cycles();
329if( DEBUG_PROCESS_COPY_INIT )
330printk("\n[DBG] %s : thread %x in process %x enter for process %x / cycle %d\n",
331__FUNCTION__, this->trdid, this->process->pid, local_process->pid, cycle );
332#endif
333
334// check user process
335assert( (local_process->pid != 0), "PID cannot be 0" );
336
337    // reset local process vmm
338    error = vmm_init( local_process );
339    assert( (error == 0) , "cannot initialize VMM\n");
340
341    // reset process file descriptors array
342        process_fd_init( local_process );
343
344    // reset vfs_root_xp / vfs_bin_xp / vfs_cwd_xp fields
345    local_process->vfs_root_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->vfs_root_xp ) );
346    local_process->vfs_bin_xp  = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->vfs_bin_xp ) );
347    local_process->vfs_cwd_xp  = XPTR_NULL;
348
349    // reset children list root (not used in a process descriptor copy)
350    xlist_root_init( XPTR( local_cxy , &local_process->children_root ) );
351    local_process->children_nr   = 0;
352    remote_queuelock_init( XPTR( local_cxy , &local_process->children_lock ),
353                           LOCK_PROCESS_CHILDREN );
354
355    // reset children_list (not used in a process descriptor copy)
356    xlist_entry_init( XPTR( local_cxy , &local_process->children_list ) );
357
358    // reset semaphores list root (not used in a process descriptor copy)
359    xlist_root_init( XPTR( local_cxy , &local_process->sem_root ) );
360    xlist_root_init( XPTR( local_cxy , &local_process->mutex_root ) );
361    xlist_root_init( XPTR( local_cxy , &local_process->barrier_root ) );
362    xlist_root_init( XPTR( local_cxy , &local_process->condvar_root ) );
363
364    // initialize th_tbl[] array and associated fields
365    uint32_t i;
366    for( i = 0 ; i < CONFIG_THREADS_MAX_PER_CLUSTER ; i++ )
367        {
368        local_process->th_tbl[i] = NULL;
369    }
370    local_process->th_nr  = 0;
371    rwlock_init( &local_process->th_lock , LOCK_PROCESS_THTBL );
372
373
374    // register new process descriptor in local cluster manager local_list
375    cluster_process_local_link( local_process );
376
377    // register new process descriptor in owner cluster manager copies_list
378    cluster_process_copies_link( local_process );
379
380        hal_fence();
381
382#if DEBUG_PROCESS_COPY_INIT
383cycle = (uint32_t)hal_get_cycles();
384if( DEBUG_PROCESS_COPY_INIT )
385printk("\n[DBG] %s : thread %x in process %x exit for process %x / cycle %d\n",
386__FUNCTION__, this->trdid, this->process->pid, local_process->pid, cycle );
387#endif
388
389    return 0;
390
391} // end process_copy_init()
392
393///////////////////////////////////////////
394void process_destroy( process_t * process )
395{
396    xptr_t      parent_xp;
397    process_t * parent_ptr;
398    cxy_t       parent_cxy;
399    xptr_t      children_lock_xp;
400    xptr_t      children_nr_xp;
401
402    pid_t       pid = process->pid;
403
404        assert( (process->th_nr == 0) ,
405    "process %x in cluster %x has still active threads", pid , local_cxy );
406
407#if DEBUG_PROCESS_DESTROY
408uint32_t cycle = (uint32_t)hal_get_cycles();
409if( DEBUG_PROCESS_DESTROY )
410printk("\n[DBG] %s : thread %x in process %x enter for process %x in cluster %x / cycle %d\n",
411__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, pid, local_cxy, cycle );
412#endif
413
414    // remove process from local_list in local cluster manager
415    cluster_process_local_unlink( process );
416
417    // remove process from copies_list in owner cluster manager
418    cluster_process_copies_unlink( process );
419
420    // remove process from children_list
421    // and release PID if owner cluster
422    if( CXY_FROM_PID( pid ) == local_cxy )
423    {
424        // get pointers on parent process
425        parent_xp  = process->parent_xp;
426        parent_cxy = GET_CXY( parent_xp );
427        parent_ptr = GET_PTR( parent_xp );
428
429        // get extended pointer on children_lock in parent process
430        children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock );
431        children_nr_xp   = XPTR( parent_cxy , &parent_ptr->children_nr );
432
433        // remove process from children_list
434        remote_queuelock_acquire( children_lock_xp );
435        xlist_unlink( XPTR( local_cxy , &process->children_list ) );
436            hal_remote_atomic_add( children_nr_xp , -1 );
437        remote_queuelock_release( children_lock_xp );
438
439        // release the process PID to cluster manager
440        cluster_pid_release( pid );
441    }
442
443    // FIXME close all open files and synchronize dirty [AG]
444
445    // decrease refcount for bin file, root file and cwd file
446        if( process->vfs_bin_xp  != XPTR_NULL ) vfs_file_count_down( process->vfs_bin_xp );
447        if( process->vfs_root_xp != XPTR_NULL ) vfs_file_count_down( process->vfs_root_xp );
448        if( process->vfs_cwd_xp  != XPTR_NULL ) vfs_file_count_down( process->vfs_cwd_xp );
449
450    // Destroy VMM
451    vmm_destroy( process );
452
453    // release memory allocated to process descriptor
454    process_free( process );
455
456#if DEBUG_PROCESS_DESTROY
457cycle = (uint32_t)hal_get_cycles();
458if( DEBUG_PROCESS_DESTROY )
459printk("\n[DBG] %s : thread %x in process %x exit / process %x in cluster %x / cycle %d\n",
460__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, pid, local_cxy, cycle );
461#endif
462
463}  // end process_destroy()
464
465/////////////////////////////////////////////////
466const char * process_action_str( process_sigactions_t action_type )
467{
468  switch ( action_type ) {
469  case BLOCK_ALL_THREADS:   return "BLOCK";
470  case UNBLOCK_ALL_THREADS: return "UNBLOCK";
471  case DELETE_ALL_THREADS:  return "DELETE";
472  default:                  return "undefined";
473  }
474}
475
476////////////////////////////////////////
477void process_sigaction( pid_t       pid,
478                        uint32_t    type )
479{
480    cxy_t              owner_cxy;         // owner cluster identifier
481    lpid_t             lpid;              // process index in owner cluster
482    cluster_t        * cluster;           // pointer on cluster manager
483    xptr_t             root_xp;           // extended pointer on root of copies
484    xptr_t             lock_xp;           // extended pointer on lock protecting copies
485    xptr_t             iter_xp;           // iterator on copies list
486    xptr_t             process_xp;        // extended pointer on process copy
487    cxy_t              process_cxy;       // process copy cluster identifier
488    process_t        * process_ptr;       // local pointer on process copy
489    reg_t              save_sr;           // for critical section
490    rpc_desc_t         rpc;               // shared RPC descriptor
491    thread_t         * client;            // pointer on client thread
492    xptr_t             client_xp;         // extended pointer on client thread
493    process_t        * local;             // pointer on process copy in local cluster
494    uint32_t           remote_nr;         // number of remote process copies
495
496    client    = CURRENT_THREAD;
497    client_xp = XPTR( local_cxy , client );
498    local     = NULL;
499    remote_nr = 0;
500
501// check calling thread can yield
502assert( (client->busylocks == 0),
503"cannot yield : busylocks = %d\n", client->busylocks );
504
505#if DEBUG_PROCESS_SIGACTION
506uint32_t cycle = (uint32_t)hal_get_cycles();
507if( DEBUG_PROCESS_SIGACTION < cycle )
508printk("\n[DBG] %s : thread %x in process %x enter to %s process %x / cycle %d\n",
509__FUNCTION__ , client->trdid, client->process->pid,
510process_action_str( type ) , pid , cycle );
511#endif
512
513    // get pointer on local cluster manager
514    cluster = LOCAL_CLUSTER;
515
516    // get owner cluster identifier and process lpid
517    owner_cxy = CXY_FROM_PID( pid );
518    lpid      = LPID_FROM_PID( pid );
519
520    // get root of list of copies, lock, and number of copies from owner cluster
521    root_xp   = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] );
522    lock_xp   = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
523
524    // check action type
525    assert( ((type == DELETE_ALL_THREADS ) ||
526             (type == BLOCK_ALL_THREADS )  ||
527             (type == UNBLOCK_ALL_THREADS )), "illegal action type" );
528             
529
530    // The client thread send parallel RPCs to all remote clusters containing
531    // target process copies, wait all responses, and then handles directly
532    // the threads in local cluster, when required.
533    // The client thread allocates a - shared - RPC descriptor in the stack,
534    // because all parallel, non-blocking, server threads use the same input
535    // arguments, and use the shared RPC response field
536
537    // mask IRQs
538    hal_disable_irq( &save_sr);
539
540    // client thread blocks itself
541    thread_block( client_xp , THREAD_BLOCKED_RPC );
542
543    // take the lock protecting process copies
544    remote_queuelock_acquire( lock_xp );
545
546    // initialize shared RPC descriptor
547    rpc.responses = 0;
548    rpc.blocking  = false;
549    rpc.index     = RPC_PROCESS_SIGACTION;
550    rpc.thread    = client;
551    rpc.lid       = client->core->lid;
552    rpc.args[0]   = type;
553    rpc.args[1]   = pid;
554
555    // scan list of process copies
556    // to send RPCs to remote copies
557    XLIST_FOREACH( root_xp , iter_xp )
558    {
559        // get extended pointers and cluster on process
560        process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list );
561        process_cxy = GET_CXY( process_xp );
562        process_ptr = GET_PTR( process_xp );
563
564        if( process_cxy == local_cxy )    // process is local
565        { 
566            local = process_ptr;
567        }
568        else                              // process is remote
569        {
570            // update number of remote process copies
571            remote_nr++;
572
573            // atomically increment responses counter
574            hal_atomic_add( (void *)&rpc.responses , 1 );
575
576#if DEBUG_PROCESS_SIGACTION
577if( DEBUG_PROCESS_SIGACTION < cycle )
578printk("\n[DBG] %s : thread %x in process %x handles remote process %x in cluster %x\n",
579__FUNCTION__, client->trdid, client->process->pid, pid , process_cxy );
580#endif
581            // call RPC in target cluster
582            rpc_process_sigaction_client( process_cxy , &rpc );
583        }
584    }  // end list of copies
585
586    // release the lock protecting process copies
587    remote_queuelock_release( lock_xp );
588
589    // restore IRQs
590    hal_restore_irq( save_sr);
591
592    // - if there is remote process copies, the client thread deschedules,
593    //   (it will be unblocked by the last RPC server thread).
594    // - if there is no remote copies, the client thread unblock itself.
595    if( remote_nr )
596    {
597        sched_yield("blocked on rpc_process_sigaction");
598    } 
599    else
600    {
601        thread_unblock( client_xp , THREAD_BLOCKED_RPC );
602    }
603
604    // handle the local process copy if required
605    if( local != NULL )
606    {
607
608#if DEBUG_PROCESS_SIGACTION
609if( DEBUG_PROCESS_SIGACTION < cycle )
610printk("\n[DBG] %s : thread %x in process %x handles local process %x in cluster %x\n",
611__FUNCTION__, client->trdid, client->process->pid, pid , local_cxy );
612#endif
613        if     (type == DELETE_ALL_THREADS  ) process_delete_threads ( local , client_xp ); 
614        else if(type == BLOCK_ALL_THREADS   ) process_block_threads  ( local , client_xp ); 
615        else if(type == UNBLOCK_ALL_THREADS ) process_unblock_threads( local );
616    }
617
618#if DEBUG_PROCESS_SIGACTION
619cycle = (uint32_t)hal_get_cycles();
620if( DEBUG_PROCESS_SIGACTION < cycle )
621printk("\n[DBG] %s : thread %x in process %x exit after %s process %x / cycle %d\n",
622__FUNCTION__, client->trdid, client->process->pid,
623process_action_str( type ), pid, cycle );
624#endif
625
626}  // end process_sigaction()
627
628/////////////////////////////////////////////////
629void process_block_threads( process_t * process,
630                            xptr_t      client_xp )
631{
632    thread_t          * target;         // pointer on target thread
633    thread_t          * this;           // pointer on calling thread
634    uint32_t            ltid;           // index in process th_tbl[]
635    cxy_t               owner_cxy;      // target process owner cluster
636    uint32_t            count;          // requests counter
637    volatile uint32_t   ack_count;      // scheduler acknowledge counter
638
639    // get calling thread pointer
640    this = CURRENT_THREAD;
641
642#if DEBUG_PROCESS_SIGACTION
643pid_t pid = process->pid;
644uint32_t cycle = (uint32_t)hal_get_cycles();
645if( DEBUG_PROCESS_SIGACTION < cycle )
646printk("\n[DBG] %s : thread %x in process %x enter for process %x in cluster %x / cycle %d\n",
647__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy , cycle );
648#endif
649
650// check target process is an user process
651assert( ( process->pid != 0 ),
652"target process must be an user process" );
653
654    // get target process owner cluster
655    owner_cxy = CXY_FROM_PID( process->pid );
656
657    // get lock protecting process th_tbl[]
658    rwlock_rd_acquire( &process->th_lock );
659
660    // loop on target process local threads
661    // we use both "ltid" and "count" because it can exist "holes" in th_tbl
662    for( ltid = 0 , count = 0 , ack_count = 0 ; count < process->th_nr ; ltid++ )
663    {
664        target = process->th_tbl[ltid];
665
666        if( target != NULL )                                 // thread exist
667        {
668            count++;
669
670            // main thread and client thread should not be blocked
671            if( ((ltid != 0) || (owner_cxy != local_cxy)) &&         // not main thread
672                (client_xp) != XPTR( local_cxy , target ) )          // not client thread
673            {
674                // set the global blocked bit in target thread descriptor.
675                thread_block( XPTR( local_cxy , target ) , THREAD_BLOCKED_GLOBAL );
676 
677                // - if the calling thread and the target thread are on the same core,
678                //   we don't need confirmation from scheduler,
679                // - if the calling thread and the target thread are not running on the same
680                //   core, we ask the target scheduler to acknowlege the blocking
681                //   to be sure that the target thread is not running.
682           
683                if( this->core->lid != target->core->lid )
684                {
685                    // increment responses counter
686                    hal_atomic_add( (void*)&ack_count , 1 );
687
688                    // set FLAG_REQ_ACK and &ack_rsp_count in target descriptor
689                    thread_set_req_ack( target , (uint32_t *)&ack_count );
690
691                    // force scheduling on target thread
692                    dev_pic_send_ipi( local_cxy , target->core->lid );
693                }
694            }
695        }
696    }
697
698    // release lock protecting process th_tbl[]
699    rwlock_rd_release( &process->th_lock );
700
701    // busy waiting acknowledges
702    // TODO this could be improved...
703    while( 1 )
704    {
705        // exit when all scheduler acknoledges received
706        if ( ack_count == 0 ) break;
707   
708        // wait 1000 cycles before retry
709        hal_fixed_delay( 1000 );
710    }
711
712#if DEBUG_PROCESS_SIGACTION
713cycle = (uint32_t)hal_get_cycles();
714if( DEBUG_PROCESS_SIGACTION < cycle )
715printk("\n[DBG] %s : thread %x in process %x exit for process %x in cluster %x / cycle %d\n",
716__FUNCTION__, this, this->process->pid, pid, local_cxy , cycle );
717#endif
718
719}  // end process_block_threads()
720
721/////////////////////////////////////////////////
722void process_delete_threads( process_t * process,
723                             xptr_t      client_xp )
724{
725    thread_t          * this;          // pointer on calling thread
726    thread_t          * target;        // local pointer on target thread
727    xptr_t              target_xp;     // extended pointer on target thread
728    cxy_t               owner_cxy;     // owner process cluster
729    uint32_t            ltid;          // index in process th_tbl
730    uint32_t            count;         // threads counter
731
732    // get calling thread pointer
733    this = CURRENT_THREAD;
734
735    // get target process owner cluster
736    owner_cxy = CXY_FROM_PID( process->pid );
737
738#if DEBUG_PROCESS_SIGACTION
739pid_t pid = process->pid;
740uint32_t cycle = (uint32_t)hal_get_cycles();
741if( DEBUG_PROCESS_SIGACTION < cycle )
742printk("\n[DBG] %s : thread %x n process %x enter for process %x in cluster %x / cycle %d\n",
743__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy , cycle );
744#endif
745
746// check target process is an user process
747assert( ( process->pid != 0 ),
748"target process must be an user process" );
749
750    // get lock protecting process th_tbl[]
751    rwlock_rd_acquire( &process->th_lock );
752
753    // loop on target process local threads                       
754    // we use both "ltid" and "count" because it can exist "holes" in th_tbl
755    for( ltid = 0 , count = 0  ; count < process->th_nr ; ltid++ )
756    {
757        target = process->th_tbl[ltid];
758
759        if( target != NULL )    // valid thread 
760        {
761            count++;
762            target_xp = XPTR( local_cxy , target );
763
764            // main thread and client thread should not be deleted
765            if( ((ltid != 0) || (owner_cxy != local_cxy)) &&         // not main thread
766                (client_xp) != target_xp )                           // not client thread
767            {
768                // mark target thread for delete and block it
769                thread_delete( target_xp , process->pid , false );   // not forced
770            }
771        }
772    }
773
774    // release lock protecting process th_tbl[]
775    rwlock_rd_release( &process->th_lock );
776
777#if DEBUG_PROCESS_SIGACTION
778cycle = (uint32_t)hal_get_cycles();
779if( DEBUG_PROCESS_SIGACTION < cycle )
780printk("\n[DBG] %s : thread %x in process %x exit for process %x in cluster %x / cycle %d\n",
781__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy , cycle );
782#endif
783
784}  // end process_delete_threads()
785
786///////////////////////////////////////////////////
787void process_unblock_threads( process_t * process )
788{
789    thread_t          * target;        // pointer on target thead
790    thread_t          * this;          // pointer on calling thread
791    uint32_t            ltid;          // index in process th_tbl
792    uint32_t            count;         // requests counter
793
794    // get calling thread pointer
795    this = CURRENT_THREAD;
796
797#if DEBUG_PROCESS_SIGACTION
798pid_t pid = process->pid;
799uint32_t cycle = (uint32_t)hal_get_cycles();
800if( DEBUG_PROCESS_SIGACTION < cycle )
801printk("\n[DBG] %s : thread %x in process %x enter for process %x in cluster %x / cycle %d\n",
802__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy , cycle );
803#endif
804
805// check target process is an user process
806assert( ( process->pid != 0 ),
807"target process must be an user process" );
808
809    // get lock protecting process th_tbl[]
810    rwlock_rd_acquire( &process->th_lock );
811
812    // loop on process threads to unblock all threads
813    // we use both "ltid" and "count" because it can exist "holes" in th_tbl
814    for( ltid = 0 , count = 0 ; count < process->th_nr ; ltid++ )
815    {
816        target = process->th_tbl[ltid];
817
818        if( target != NULL )             // thread found
819        {
820            count++;
821
822            // reset the global blocked bit in target thread descriptor.
823            thread_unblock( XPTR( local_cxy , target ) , THREAD_BLOCKED_GLOBAL );
824        }
825    }
826
827    // release lock protecting process th_tbl[]
828    rwlock_rd_release( &process->th_lock );
829
830#if DEBUG_PROCESS_SIGACTION
831cycle = (uint32_t)hal_get_cycles();
832if( DEBUG_PROCESS_SIGACTION < cycle )
833printk("\n[DBG] %s : thread %x in process %x exit for process %x in cluster %x / cycle %d\n",
834__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy, cycle );
835#endif
836
837}  // end process_unblock_threads()
838
839///////////////////////////////////////////////
840process_t * process_get_local_copy( pid_t pid )
841{
842    error_t        error;
843    process_t    * process_ptr;   // local pointer on process
844    xptr_t         process_xp;    // extended pointer on process
845
846    cluster_t * cluster = LOCAL_CLUSTER;
847
848#if DEBUG_PROCESS_GET_LOCAL_COPY
849thread_t * this = CURRENT_THREAD;
850uint32_t cycle = (uint32_t)hal_get_cycles();
851if( DEBUG_PROCESS_GET_LOCAL_COPY < cycle )
852printk("\n[DBG] %s : thread %x in cluster %x enter for process %x in cluster %x / cycle %d\n",
853__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy, cycle );
854#endif
855
856    // get lock protecting local list of processes
857    remote_queuelock_acquire( XPTR( local_cxy , &cluster->pmgr.local_lock ) );
858
859    // scan the local list of process descriptors to find the process
860    xptr_t  iter;
861    bool_t  found = false;
862    XLIST_FOREACH( XPTR( local_cxy , &cluster->pmgr.local_root ) , iter )
863    {
864        process_xp  = XLIST_ELEMENT( iter , process_t , local_list );
865        process_ptr = GET_PTR( process_xp );
866        if( process_ptr->pid == pid )
867        {
868            found = true;
869            break;
870        }
871    }
872
873    // release lock protecting local list of processes
874    remote_queuelock_release( XPTR( local_cxy , &cluster->pmgr.local_lock ) );
875
876    // allocate memory for a new local process descriptor
877    // and initialise it from reference cluster if not found
878    if( !found )
879    {
880        // get extended pointer on reference process descriptor
881        xptr_t ref_xp = cluster_get_reference_process_from_pid( pid );
882
883        assert( (ref_xp != XPTR_NULL) , "illegal pid\n" );
884
885        // allocate memory for local process descriptor
886        process_ptr = process_alloc();
887
888        if( process_ptr == NULL )  return NULL;
889
890        // initialize local process descriptor copy
891        error = process_copy_init( process_ptr , ref_xp );
892
893        if( error ) return NULL;
894    }
895
896#if DEBUG_PROCESS_GET_LOCAL_COPY
897cycle = (uint32_t)hal_get_cycles();
898if( DEBUG_PROCESS_GET_LOCAL_COPY < cycle )
899printk("\n[DBG] %s : thread %x in cluster %x exit in cluster %x / process %x / cycle %d\n",
900__FUNCTION__, this->trdid, this->process->pid, local_cxy, process_ptr, cycle );
901#endif
902
903    return process_ptr;
904
905}  // end process_get_local_copy()
906
907////////////////////////////////////////////
908pid_t process_get_ppid( xptr_t  process_xp )
909{
910    cxy_t       process_cxy;
911    process_t * process_ptr;
912    xptr_t      parent_xp;
913    cxy_t       parent_cxy;
914    process_t * parent_ptr;
915
916    // get process cluster and local pointer
917    process_cxy = GET_CXY( process_xp );
918    process_ptr = GET_PTR( process_xp );
919
920    // get pointers on parent process
921    parent_xp  = (xptr_t)hal_remote_l64( XPTR( process_cxy , &process_ptr->parent_xp ) );
922    parent_cxy = GET_CXY( parent_xp );
923    parent_ptr = GET_PTR( parent_xp );
924
925    return hal_remote_l32( XPTR( parent_cxy , &parent_ptr->pid ) );
926}
927
928//////////////////////////////////////////////////////////////////////////////////////////
929// File descriptor array related functions
930//////////////////////////////////////////////////////////////////////////////////////////
931
932///////////////////////////////////////////
933void process_fd_init( process_t * process )
934{
935    uint32_t fd;
936
937    remote_queuelock_init( XPTR( local_cxy , &process->fd_array.lock ), LOCK_PROCESS_FDARRAY );
938
939    process->fd_array.current = 0;
940
941    // initialize array
942    for ( fd = 0 ; fd < CONFIG_PROCESS_FILE_MAX_NR ; fd++ )
943    {
944        process->fd_array.array[fd] = XPTR_NULL;
945    }
946}
947/////////////////////////////////////////////////
948error_t process_fd_register( process_t * process,
949                             xptr_t      file_xp,
950                             uint32_t  * fdid )
951{
952    bool_t    found;
953    uint32_t  id;
954    uint32_t  count;
955    xptr_t    xp;
956
957    // get reference process cluster and local pointer
958    xptr_t ref_xp = process->ref_xp;
959    process_t * ref_ptr = GET_PTR( ref_xp );
960    cxy_t       ref_cxy = GET_CXY( ref_xp );
961
962    // take lock protecting reference fd_array
963        remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->fd_array.lock ) );
964
965    found   = false;
966
967    for ( id = 0; id < CONFIG_PROCESS_FILE_MAX_NR ; id++ )
968    {
969        xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->fd_array.array[id] ) );
970        if ( xp == XPTR_NULL )
971        {
972            // update reference fd_array
973            hal_remote_s64( XPTR( ref_cxy , &ref_ptr->fd_array.array[id] ) , file_xp );
974                count = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->fd_array.current ) ) + 1;
975            hal_remote_s32( XPTR( ref_cxy , &ref_ptr->fd_array.current ) , count );
976
977            // update local fd_array copy if required
978            if( ref_cxy != local_cxy )
979            {
980                process->fd_array.array[id] = file_xp;
981                process->fd_array.current   = count;
982            }
983
984            // exit
985                        *fdid = id;
986            found = true;
987            break;
988        }
989    }
990
991    // release lock protecting reference fd_array
992        remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->fd_array.lock ) );
993
994    if ( !found ) return -1;
995    else          return 0;
996}
997
998////////////////////////////////////////////////
999xptr_t process_fd_get_xptr( process_t * process,
1000                            uint32_t    fdid )
1001{
1002    xptr_t  file_xp;
1003    xptr_t  lock_xp;
1004
1005    // access local copy of process descriptor
1006    file_xp = process->fd_array.array[fdid];
1007
1008    if( file_xp == XPTR_NULL )
1009    {
1010        // get reference process cluster and local pointer
1011        xptr_t      ref_xp  = process->ref_xp;
1012        cxy_t       ref_cxy = GET_CXY( ref_xp );
1013        process_t * ref_ptr = GET_PTR( ref_xp );
1014
1015        // build extended pointer on lock protecting reference fd_array
1016        lock_xp = XPTR( ref_cxy , &ref_ptr->fd_array.lock );
1017
1018        // take lock protecting reference fd_array
1019            remote_queuelock_acquire( lock_xp );
1020
1021        // access reference process descriptor
1022        file_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->fd_array.array[fdid] ) );
1023
1024        // update local fd_array if found
1025        if( file_xp != XPTR_NULL )  process->fd_array.array[fdid] = file_xp;
1026       
1027        // release lock protecting reference fd_array
1028            remote_queuelock_release( lock_xp );
1029    }
1030
1031    return file_xp;
1032
1033}  // end process_fd_get_xptr()
1034
1035///////////////////////////////////////////
1036void process_fd_remote_copy( xptr_t dst_xp,
1037                             xptr_t src_xp )
1038{
1039    uint32_t fd;
1040    xptr_t   entry;
1041
1042    // get cluster and local pointer for src fd_array
1043    cxy_t        src_cxy = GET_CXY( src_xp );
1044    fd_array_t * src_ptr = GET_PTR( src_xp );
1045
1046    // get cluster and local pointer for dst fd_array
1047    cxy_t        dst_cxy = GET_CXY( dst_xp );
1048    fd_array_t * dst_ptr = GET_PTR( dst_xp );
1049
1050    // get the remote lock protecting the src fd_array
1051        remote_queuelock_acquire( XPTR( src_cxy , &src_ptr->lock ) );
1052
1053    // loop on all fd_array entries
1054    for( fd = 0 ; fd < CONFIG_PROCESS_FILE_MAX_NR ; fd++ )
1055        {
1056                entry = (xptr_t)hal_remote_l64( XPTR( src_cxy , &src_ptr->array[fd] ) );
1057
1058                if( entry != XPTR_NULL )
1059                {
1060            // increment file descriptor refcount
1061            vfs_file_count_up( entry );
1062
1063                        // copy entry in destination process fd_array
1064                        hal_remote_s64( XPTR( dst_cxy , &dst_ptr->array[fd] ) , entry );
1065                }
1066        }
1067
1068    // release lock on source process fd_array
1069        remote_queuelock_release( XPTR( src_cxy , &src_ptr->lock ) );
1070
1071}  // end process_fd_remote_copy()
1072
1073
1074////////////////////////////////////
1075bool_t process_fd_array_full( void )
1076{
1077    // get extended pointer on reference process
1078    xptr_t ref_xp = CURRENT_THREAD->process->ref_xp;
1079
1080    // get reference process cluster and local pointer
1081    process_t * ref_ptr = GET_PTR( ref_xp );
1082    cxy_t       ref_cxy = GET_CXY( ref_xp );
1083
1084    // get number of open file descriptors from reference fd_array
1085    uint32_t current = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->fd_array.current ) );
1086
1087        return ( current >= CONFIG_PROCESS_FILE_MAX_NR );
1088}
1089
1090
1091////////////////////////////////////////////////////////////////////////////////////
1092//  Thread related functions
1093////////////////////////////////////////////////////////////////////////////////////
1094
1095/////////////////////////////////////////////////////
1096error_t process_register_thread( process_t * process,
1097                                 thread_t  * thread,
1098                                 trdid_t   * trdid )
1099{
1100    ltid_t         ltid;
1101    bool_t         found = false;
1102 
1103// check arguments
1104assert( (process != NULL) , "process argument is NULL" );
1105assert( (thread != NULL) , "thread argument is NULL" );
1106
1107    // get the lock protecting th_tbl for all threads
1108    // but the idle thread executing kernel_init (cannot yield)
1109    if( thread->type != THREAD_IDLE ) rwlock_wr_acquire( &process->th_lock );
1110
1111    // scan kth_tbl
1112    for( ltid = 0 ; ltid < CONFIG_THREADS_MAX_PER_CLUSTER ; ltid++ )
1113    {
1114        if( process->th_tbl[ltid] == NULL )
1115        {
1116            found = true;
1117            break;
1118        }
1119    }
1120
1121    if( found )
1122    {
1123        // register thread in th_tbl[]
1124        process->th_tbl[ltid] = thread;
1125        process->th_nr++;
1126
1127        // returns trdid
1128        *trdid = TRDID( local_cxy , ltid );
1129    }
1130
1131    // get the lock protecting th_tbl for all threads
1132    // but the idle thread executing kernel_init (cannot yield)
1133    if( thread->type != THREAD_IDLE ) rwlock_wr_release( &process->th_lock );
1134
1135    return (found) ? 0 : 0xFFFFFFFF;
1136
1137}  // end process_register_thread()
1138
1139/////////////////////////////////////////////////
1140bool_t process_remove_thread( thread_t * thread )
1141{
1142    uint32_t count;  // number of threads in local process descriptor
1143
1144// check argument
1145assert( (thread != NULL) , "thread argument is NULL" );
1146
1147    process_t * process = thread->process;
1148
1149    // get thread local index
1150    ltid_t  ltid = LTID_FROM_TRDID( thread->trdid );
1151   
1152    // the lock depends on thread user/kernel type, because we cannot
1153    // use a descheduling policy for the lock protecting the kth_tbl
1154
1155    // get the lock protecting th_tbl[]
1156    rwlock_wr_acquire( &process->th_lock );
1157
1158    // get number of kernel threads
1159    count = process->th_nr;
1160
1161// check th_nr value
1162assert( (count > 0) , "process kth_nr cannot be 0\n" );
1163
1164    // remove thread from th_tbl[]
1165    process->th_tbl[ltid] = NULL;
1166    process->th_nr = count-1;
1167
1168    // release lock protecting kth_tbl
1169    rwlock_wr_release( &process->th_lock );
1170
1171    return (count == 1);
1172
1173}  // end process_remove_thread()
1174
1175/////////////////////////////////////////////////////////
1176error_t process_make_fork( xptr_t      parent_process_xp,
1177                           xptr_t      parent_thread_xp,
1178                           pid_t     * child_pid,
1179                           thread_t ** child_thread )
1180{
1181    process_t * process;         // local pointer on child process descriptor
1182    thread_t  * thread;          // local pointer on child thread descriptor
1183    pid_t       new_pid;         // process identifier for child process
1184    pid_t       parent_pid;      // process identifier for parent process
1185    xptr_t      ref_xp;          // extended pointer on reference process
1186    xptr_t      vfs_bin_xp;      // extended pointer on .elf file
1187    error_t     error;
1188
1189    // get cluster and local pointer for parent process
1190    cxy_t       parent_process_cxy = GET_CXY( parent_process_xp );
1191    process_t * parent_process_ptr = GET_PTR( parent_process_xp );
1192
1193    // get parent process PID and extended pointer on .elf file
1194    parent_pid = hal_remote_l32 (XPTR( parent_process_cxy , &parent_process_ptr->pid));
1195    vfs_bin_xp = hal_remote_l64(XPTR( parent_process_cxy , &parent_process_ptr->vfs_bin_xp));
1196
1197    // get extended pointer on reference process
1198    ref_xp = hal_remote_l64( XPTR( parent_process_cxy , &parent_process_ptr->ref_xp ) );
1199
1200// check parent process is the reference process
1201assert( (parent_process_xp == ref_xp ) ,
1202"parent process must be the reference process\n" );
1203
1204#if DEBUG_PROCESS_MAKE_FORK
1205uint32_t cycle = (uint32_t)hal_get_cycles();
1206if( DEBUG_PROCESS_MAKE_FORK < cycle )
1207printk("\n[DBG] %s : thread %x in process %x enter / cluster %x / cycle %d\n",
1208__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, local_cxy, cycle );
1209#endif
1210
1211    // allocate a process descriptor
1212    process = process_alloc();
1213    if( process == NULL )
1214    {
1215        printk("\n[ERROR] in %s : cannot get process in cluster %x\n", 
1216        __FUNCTION__, local_cxy ); 
1217        return -1;
1218    }
1219
1220    // allocate a child PID from local cluster
1221    error = cluster_pid_alloc( process , &new_pid );
1222    if( error ) 
1223    {
1224        printk("\n[ERROR] in %s : cannot get PID in cluster %x\n", 
1225        __FUNCTION__, local_cxy ); 
1226        process_free( process );
1227        return -1;
1228    }
1229
1230#if( DEBUG_PROCESS_MAKE_FORK & 1 )
1231cycle = (uint32_t)hal_get_cycles();
1232if( DEBUG_PROCESS_MAKE_FORK < cycle )
1233printk("\n[DBG] %s : thread %x in process %x allocated process %x / cycle %d\n",
1234__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, new_pid, cycle );
1235#endif
1236
1237    // initializes child process descriptor from parent process descriptor
1238    process_reference_init( process,
1239                            new_pid,
1240                            parent_process_xp );
1241
1242#if( DEBUG_PROCESS_MAKE_FORK & 1 )
1243cycle = (uint32_t)hal_get_cycles();
1244if( DEBUG_PROCESS_MAKE_FORK < cycle )
1245printk("\n[DBG] %s : thread %x in process %x initialized child_process %x / cycle %d\n",
1246__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, new_pid, cycle );
1247#endif
1248
1249
1250    // copy VMM from parent descriptor to child descriptor
1251    error = vmm_fork_copy( process,
1252                           parent_process_xp );
1253    if( error )
1254    {
1255        printk("\n[ERROR] in %s : cannot copy VMM in cluster %x\n", 
1256        __FUNCTION__, local_cxy ); 
1257        process_free( process );
1258        cluster_pid_release( new_pid );
1259        return -1;
1260    }
1261
1262#if( DEBUG_PROCESS_MAKE_FORK & 1 )
1263cycle = (uint32_t)hal_get_cycles();
1264if( DEBUG_PROCESS_MAKE_FORK < cycle )
1265printk("\n[DBG] %s : thread %x in process %x copied VMM from parent %x to child %x / cycle %d\n",
1266__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, 
1267parent_pid, new_pid, cycle );
1268#endif
1269
1270    // if parent_process is INIT, or if parent_process is the TXT owner,
1271    // the child_process becomes the owner of its TXT terminal
1272    if( (parent_pid == 1) || process_txt_is_owner( parent_process_xp ) )
1273    {
1274        process_txt_set_ownership( XPTR( local_cxy , process ) );
1275
1276#if( DEBUG_PROCESS_MAKE_FORK & 1 )
1277cycle = (uint32_t)hal_get_cycles();
1278if( DEBUG_PROCESS_MAKE_EXEC < cycle )
1279printk("\n[DBG] %s : thread %x in process %x / child takes TXT ownership / cycle %d\n",
1280__FUNCTION__ , CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle );
1281#endif
1282
1283    }
1284
1285    // update extended pointer on .elf file
1286    process->vfs_bin_xp = vfs_bin_xp;
1287
1288    // create child thread descriptor from parent thread descriptor
1289    error = thread_user_fork( parent_thread_xp,
1290                              process,
1291                              &thread );
1292    if( error )
1293    {
1294        printk("\n[ERROR] in %s : cannot create thread in cluster %x\n",
1295        __FUNCTION__, local_cxy ); 
1296        process_free( process );
1297        cluster_pid_release( new_pid );
1298        return -1;
1299    }
1300
1301// check main thread LTID
1302assert( (LTID_FROM_TRDID(thread->trdid) == 0) ,
1303"main thread must have LTID == 0\n" );
1304
1305#if( DEBUG_PROCESS_MAKE_FORK & 1 )
1306cycle = (uint32_t)hal_get_cycles();
1307if( DEBUG_PROCESS_MAKE_FORK < cycle )
1308printk("\n[DBG] %s : thread %x in process %x created main thread %x / cycle %d\n", 
1309__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, thread, cycle );
1310#endif
1311
1312    // set Copy_On_Write flag in parent process GPT
1313    // this includes all replicated GPT copies
1314    if( parent_process_cxy == local_cxy )   // reference is local
1315    {
1316        vmm_set_cow( parent_process_ptr );
1317    }
1318    else                                    // reference is remote
1319    {
1320        rpc_vmm_set_cow_client( parent_process_cxy,
1321                                parent_process_ptr );
1322    }
1323
1324    // set Copy_On_Write flag in child process GPT
1325    vmm_set_cow( process );
1326 
1327#if( DEBUG_PROCESS_MAKE_FORK & 1 )
1328cycle = (uint32_t)hal_get_cycles();
1329if( DEBUG_PROCESS_MAKE_FORK < cycle )
1330printk("\n[DBG] %s : thread %x in process %x set COW in parent and child / cycle %d\n",
1331__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle );
1332#endif
1333
1334    // get extended pointers on parent children_root, children_lock and children_nr
1335    xptr_t children_root_xp = XPTR( parent_process_cxy , &parent_process_ptr->children_root );
1336    xptr_t children_lock_xp = XPTR( parent_process_cxy , &parent_process_ptr->children_lock );
1337    xptr_t children_nr_xp   = XPTR( parent_process_cxy , &parent_process_ptr->children_nr   );
1338
1339    // register process in parent children list
1340    remote_queuelock_acquire( children_lock_xp );
1341        xlist_add_last( children_root_xp , XPTR( local_cxy , &process->children_list ) );
1342        hal_remote_atomic_add( children_nr_xp , 1 );
1343    remote_queuelock_release( children_lock_xp );
1344
1345    // return success
1346    *child_thread = thread;
1347    *child_pid    = new_pid;
1348
1349#if DEBUG_PROCESS_MAKE_FORK
1350cycle = (uint32_t)hal_get_cycles();
1351if( DEBUG_PROCESS_MAKE_FORK < cycle )
1352printk("\n[DBG] %s : thread %x in process %x exit / created process %x / cycle %d\n",
1353__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, new_pid, cycle );
1354#endif
1355
1356    return 0;
1357
1358}   // end process_make_fork()
1359
1360/////////////////////////////////////////////////////
1361error_t process_make_exec( exec_info_t  * exec_info )
1362{
1363    thread_t       * thread;                  // local pointer on this thread
1364    process_t      * process;                 // local pointer on this process
1365    pid_t            pid;                     // this process identifier
1366        error_t          error;                   // value returned by called functions
1367    char           * path;                    // path to .elf file
1368    xptr_t           file_xp;                 // extended pointer on .elf file descriptor
1369    uint32_t         file_id;                 // file index in fd_array
1370    uint32_t         args_nr;                 // number of main thread arguments
1371    char          ** args_pointers;           // array of pointers on main thread arguments
1372
1373    // get thread, process & PID
1374    thread  = CURRENT_THREAD;
1375    process = thread->process;
1376    pid     = process->pid;
1377
1378        // get relevant infos from exec_info
1379        path          = exec_info->path;
1380    args_nr       = exec_info->args_nr;
1381    args_pointers = exec_info->args_pointers;
1382
1383#if DEBUG_PROCESS_MAKE_EXEC
1384uint32_t cycle = (uint32_t)hal_get_cycles();
1385if( DEBUG_PROCESS_MAKE_EXEC < cycle )
1386printk("\n[DBG] %s : thread %x in process %x enters / path %s / cycle %d\n",
1387__FUNCTION__, thread->trdid, pid, path, cycle );
1388#endif
1389
1390    // open the file identified by <path>
1391    file_xp = XPTR_NULL;
1392    file_id = 0xFFFFFFFF;
1393        error   = vfs_open( process,
1394                            path,
1395                            O_RDONLY,
1396                            0,
1397                            &file_xp,
1398                            &file_id );
1399        if( error )
1400        {
1401                printk("\n[ERROR] in %s : failed to open file <%s>\n", __FUNCTION__ , path );
1402                return -1;
1403        }
1404
1405#if (DEBUG_PROCESS_MAKE_EXEC & 1)
1406cycle = (uint32_t)hal_get_cycles();
1407if( DEBUG_PROCESS_MAKE_EXEC < cycle )
1408printk("\n[DBG] %s : thread %x in process %x opened file <%s> / cycle %d\n",
1409__FUNCTION__, thread->trdid, pid, path, cycle );
1410#endif
1411
1412    // delete all threads other than this main thread in all clusters
1413    process_sigaction( pid , DELETE_ALL_THREADS );
1414
1415#if (DEBUG_PROCESS_MAKE_EXEC & 1)
1416cycle = (uint32_t)hal_get_cycles();
1417if( DEBUG_PROCESS_MAKE_EXEC < cycle )
1418printk("\n[DBG] %s : thread %x in process %x deleted all threads / cycle %d\n",
1419__FUNCTION__, thread->trdid, pid, cycle );
1420#endif
1421
1422    // reset local process VMM
1423    vmm_destroy( process );
1424
1425#if( DEBUG_PROCESS_MAKE_EXEC & 1 )
1426cycle = (uint32_t)hal_get_cycles();
1427if( DEBUG_PROCESS_MAKE_EXEC < cycle )
1428printk("\n[DBG] %s : thread %x in process %x reset VMM / cycle %d\n",
1429__FUNCTION__, thread->trdid, pid, cycle );
1430#endif
1431
1432    // re-initialize the VMM (kentry/args/envs vsegs registration)
1433    error = vmm_init( process );
1434    if( error )
1435    {
1436        printk("\n[ERROR] in %s : cannot initialise VMM for %s\n", __FUNCTION__ , path );
1437        vfs_close( file_xp , file_id );
1438        // FIXME restore old process VMM
1439        return -1;
1440    }
1441   
1442#if( DEBUG_PROCESS_MAKE_EXEC & 1 )
1443cycle = (uint32_t)hal_get_cycles();
1444if( DEBUG_PROCESS_MAKE_EXEC < cycle )
1445printk("\n[DBG] %s : thread %x in process %x / kentry/args/envs vsegs registered / cycle %d\n",
1446__FUNCTION__, thread->trdid, pid, cycle );
1447#endif
1448
1449    // register code & data vsegs as well as entry-point in process VMM,
1450    // and register extended pointer on .elf file in process descriptor
1451        error = elf_load_process( file_xp , process );
1452    if( error )
1453        {
1454                printk("\n[ERROR] in %s : failed to access <%s>\n", __FUNCTION__ , path );
1455        vfs_close( file_xp , file_id );
1456        // FIXME restore old process VMM
1457        return -1;
1458        }
1459
1460#if( DEBUG_PROCESS_MAKE_EXEC & 1 )
1461cycle = (uint32_t)hal_get_cycles();
1462if( DEBUG_PROCESS_MAKE_EXEC < cycle )
1463printk("\n[DBG] %s : thread %x in process %x / code/data vsegs registered / cycle %d\n",
1464__FUNCTION__, thread->trdid, pid, cycle );
1465#endif
1466
1467    // update the existing main thread descriptor... and jump to user code
1468    error = thread_user_exec( (void *)process->vmm.entry_point,
1469                              args_nr,
1470                              args_pointers );
1471    if( error )
1472    {
1473        printk("\n[ERROR] in %s : cannot update main thread for %s\n", __FUNCTION__ , path );
1474        vfs_close( file_xp , file_id );
1475        // FIXME restore old process VMM
1476        return -1;
1477    }
1478
1479    assert( false, "we should not execute this code");
1480 
1481        return 0;
1482
1483}  // end process_make_exec()
1484
1485
1486///////////////////////////////////////////////
1487void process_zero_create( process_t * process )
1488{
1489
1490#if DEBUG_PROCESS_ZERO_CREATE
1491uint32_t cycle = (uint32_t)hal_get_cycles();
1492if( DEBUG_PROCESS_ZERO_CREATE < cycle )
1493printk("\n[DBG] %s : enter / cluster %x / cycle %d\n",
1494__FUNCTION__, local_cxy, cycle );
1495#endif
1496
1497    // initialize PID, REF_XP, PARENT_XP, and STATE
1498    process->pid        = 0;
1499    process->ref_xp     = XPTR( local_cxy , process );
1500    process->owner_xp   = XPTR( local_cxy , process );
1501    process->parent_xp  = XPTR_NULL;
1502    process->term_state = 0;
1503
1504    // reset th_tbl[] array and associated fields
1505    uint32_t i;
1506    for( i = 0 ; i < CONFIG_THREADS_MAX_PER_CLUSTER ; i++ )
1507        {
1508        process->th_tbl[i] = NULL;
1509    }
1510    process->th_nr  = 0;
1511    rwlock_init( &process->th_lock , LOCK_PROCESS_THTBL );
1512
1513
1514    // reset children list as empty
1515    xlist_root_init( XPTR( local_cxy , &process->children_root ) );
1516    process->children_nr = 0;
1517    remote_queuelock_init( XPTR( local_cxy , &process->children_lock ),
1518                           LOCK_PROCESS_CHILDREN );
1519
1520        hal_fence();
1521
1522#if DEBUG_PROCESS_ZERO_CREATE
1523cycle = (uint32_t)hal_get_cycles();
1524if( DEBUG_PROCESS_ZERO_CREATE < cycle )
1525printk("\n[DBG] %s : exit / cluster %x / cycle %d\n",
1526__FUNCTION__, local_cxy, cycle );
1527#endif
1528
1529}  // end process_zero_init()
1530
1531////////////////////////////////
1532void process_init_create( void )
1533{
1534    process_t      * process;       // local pointer on process descriptor
1535    pid_t            pid;           // process_init identifier
1536    thread_t       * thread;        // local pointer on main thread
1537    pthread_attr_t   attr;          // main thread attributes
1538    lid_t            lid;           // selected core local index for main thread
1539    xptr_t           file_xp;       // extended pointer on .elf file descriptor
1540    uint32_t         file_id;       // file index in fd_array
1541    error_t          error;
1542
1543#if DEBUG_PROCESS_INIT_CREATE
1544uint32_t cycle = (uint32_t)hal_get_cycles();
1545if( DEBUG_PROCESS_INIT_CREATE < cycle )
1546printk("\n[DBG] %s : thread %x in process %x enter / cycle %d\n",
1547__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle );
1548#endif
1549
1550    // allocates memory for process descriptor from local cluster
1551        process = process_alloc(); 
1552       
1553// check memory allocator
1554assert( (process != NULL),
1555"no memory for process descriptor in cluster %x\n", local_cxy  );
1556
1557    // get PID from local cluster
1558    error = cluster_pid_alloc( process , &pid );
1559
1560// check PID allocator
1561assert( (error == 0),
1562"cannot allocate PID in cluster %x\n", local_cxy );
1563
1564// check PID value
1565assert( (pid == 1) ,
1566"process INIT must be first process in cluster 0\n" );
1567
1568    // initialize process descriptor / parent is local process_zero
1569    process_reference_init( process,
1570                            pid,
1571                            XPTR( local_cxy , &process_zero ) ); 
1572
1573#if(DEBUG_PROCESS_INIT_CREATE & 1)
1574if( DEBUG_PROCESS_INIT_CREATE < cycle )
1575printk("\n[DBG] %s : thread %x in process %x initialized process descriptor\n",
1576__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1577#endif
1578
1579    // open the file identified by CONFIG_PROCESS_INIT_PATH
1580    file_xp = XPTR_NULL;
1581    file_id = -1;
1582        error   = vfs_open( process,
1583                            CONFIG_PROCESS_INIT_PATH,
1584                            O_RDONLY,
1585                            0,
1586                            &file_xp,
1587                            &file_id );
1588
1589assert( (error == 0),
1590"failed to open file <%s>\n", CONFIG_PROCESS_INIT_PATH );
1591
1592#if(DEBUG_PROCESS_INIT_CREATE & 1)
1593if( DEBUG_PROCESS_INIT_CREATE < cycle )
1594printk("\n[DBG] %s : thread %x in process %x open .elf file decriptor\n",
1595__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1596#endif
1597
1598   // register "code" and "data" vsegs as well as entry-point
1599    // in process VMM, using information contained in the elf file.
1600        error = elf_load_process( file_xp , process );
1601
1602assert( (error == 0),
1603"cannot access .elf file <%s>\n", CONFIG_PROCESS_INIT_PATH );
1604
1605#if(DEBUG_PROCESS_INIT_CREATE & 1)
1606if( DEBUG_PROCESS_INIT_CREATE < cycle )
1607printk("\n[DBG] %s : thread %x in process %x registered code/data vsegs in VMM\n",
1608__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1609#endif
1610
1611    // get extended pointers on process_zero children_root, children_lock
1612    xptr_t children_root_xp = XPTR( local_cxy , &process_zero.children_root );
1613    xptr_t children_lock_xp = XPTR( local_cxy , &process_zero.children_lock );
1614
1615    // take lock protecting kernel process children list
1616    remote_queuelock_acquire( children_lock_xp );
1617
1618    // register process INIT in parent local process_zero
1619        xlist_add_last( children_root_xp , XPTR( local_cxy , &process->children_list ) );
1620        hal_atomic_add( &process_zero.children_nr , 1 );
1621
1622    // release lock protecting kernel process children list
1623    remote_queuelock_release( children_lock_xp );
1624
1625#if(DEBUG_PROCESS_INIT_CREATE & 1)
1626if( DEBUG_PROCESS_INIT_CREATE < cycle )
1627printk("\n[DBG] %s : thread %x in process %x registered init process in parent\n",
1628__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1629#endif
1630
1631    // select a core in local cluster to execute the main thread
1632    lid  = cluster_select_local_core();
1633
1634    // initialize pthread attributes for main thread
1635    attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED | PT_ATTR_CORE_DEFINED;
1636    attr.cxy        = local_cxy;
1637    attr.lid        = lid;
1638
1639    // create and initialize thread descriptor
1640        error = thread_user_create( pid,
1641                                (void *)process->vmm.entry_point,
1642                                NULL,
1643                                &attr,
1644                                &thread );
1645
1646assert( (error == 0),
1647"cannot create main thread for <%s>\n", CONFIG_PROCESS_INIT_PATH );
1648
1649assert( (thread->trdid == 0),
1650"main thread must have index 0 for <%s>\n", CONFIG_PROCESS_INIT_PATH );
1651
1652#if(DEBUG_PROCESS_INIT_CREATE & 1)
1653if( DEBUG_PROCESS_INIT_CREATE < cycle )
1654printk("\n[DBG] %s : thread %x in process %x created main thread\n",
1655__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1656#endif
1657
1658    // activate thread
1659        thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
1660
1661    hal_fence();
1662
1663#if DEBUG_PROCESS_INIT_CREATE
1664cycle = (uint32_t)hal_get_cycles();
1665if( DEBUG_PROCESS_INIT_CREATE < cycle )
1666printk("\n[DBG] %s : thread %x in process %x exit / cycle %d\n",
1667__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle );
1668#endif
1669
1670}  // end process_init_create()
1671
1672/////////////////////////////////////////
1673void process_display( xptr_t process_xp )
1674{
1675    process_t   * process_ptr;
1676    cxy_t         process_cxy;
1677
1678    xptr_t        parent_xp;       // extended pointer on parent process
1679    process_t   * parent_ptr;
1680    cxy_t         parent_cxy;
1681
1682    xptr_t        owner_xp;        // extended pointer on owner process
1683    process_t   * owner_ptr;
1684    cxy_t         owner_cxy;
1685
1686    pid_t         pid;
1687    pid_t         ppid;
1688    uint32_t      state;
1689    uint32_t      th_nr;
1690
1691    xptr_t        txt_file_xp;     // extended pointer on TXT_RX file descriptor
1692    xptr_t        txt_chdev_xp;    // extended pointer on TXT_RX chdev
1693    chdev_t     * txt_chdev_ptr;
1694    cxy_t         txt_chdev_cxy;
1695    xptr_t        txt_owner_xp;    // extended pointer on TXT owner process
1696
1697    xptr_t        elf_file_xp;     // extended pointer on .elf file
1698    cxy_t         elf_file_cxy;
1699    vfs_file_t  * elf_file_ptr;
1700    vfs_inode_t * elf_inode_ptr;   // local pointer on .elf inode
1701
1702    char          txt_name[CONFIG_VFS_MAX_NAME_LENGTH];
1703    char          elf_name[CONFIG_VFS_MAX_NAME_LENGTH];
1704
1705    // get cluster and local pointer on process
1706    process_ptr = GET_PTR( process_xp );
1707    process_cxy = GET_CXY( process_xp );
1708
1709    // get PID and state
1710    pid   = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
1711    state = hal_remote_l32( XPTR( process_cxy , &process_ptr->term_state ) );
1712
1713    // get PPID
1714    parent_xp  = hal_remote_l64( XPTR( process_cxy , &process_ptr->parent_xp ) );
1715    parent_cxy = GET_CXY( parent_xp );
1716    parent_ptr = GET_PTR( parent_xp );
1717    ppid       = hal_remote_l32( XPTR( parent_cxy , &parent_ptr->pid ) );
1718
1719    // get number of threads
1720    th_nr      = hal_remote_l32( XPTR( process_cxy , &process_ptr->th_nr ) );
1721
1722    // get pointers on owner process descriptor
1723    owner_xp  = hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ) );
1724    owner_cxy = GET_CXY( owner_xp );
1725    owner_ptr = GET_PTR( owner_xp );
1726
1727    // get extended pointer on TXT_RX file descriptor attached to process
1728    txt_file_xp = hal_remote_l64( XPTR( owner_cxy , &owner_ptr->fd_array.array[0] ) );
1729
1730    assert( (txt_file_xp != XPTR_NULL) ,
1731    "process must be attached to one TXT terminal\n" ); 
1732
1733    // get TXT_RX chdev pointers
1734    txt_chdev_xp  = chdev_from_file( txt_file_xp );
1735    txt_chdev_cxy = GET_CXY( txt_chdev_xp );
1736    txt_chdev_ptr = GET_PTR( txt_chdev_xp );
1737
1738    // get TXT_RX name and ownership
1739    hal_remote_strcpy( XPTR( local_cxy , txt_name ) ,
1740                       XPTR( txt_chdev_cxy , txt_chdev_ptr->name ) );
1741   
1742    txt_owner_xp = (xptr_t)hal_remote_l64( XPTR( txt_chdev_cxy, 
1743                                                 &txt_chdev_ptr->ext.txt.owner_xp ) );
1744   
1745    // get process .elf name
1746    elf_file_xp   = hal_remote_l64( XPTR( process_cxy , &process_ptr->vfs_bin_xp ) );
1747    elf_file_cxy  = GET_CXY( elf_file_xp );
1748    elf_file_ptr  = (vfs_file_t *)GET_PTR( elf_file_xp );
1749    elf_inode_ptr = (vfs_inode_t *)hal_remote_lpt( XPTR( elf_file_cxy , &elf_file_ptr->inode ) );
1750    vfs_inode_get_name( XPTR( elf_file_cxy , elf_inode_ptr ) , elf_name );
1751
1752    // display process info
1753    if( txt_owner_xp == process_xp )
1754    {
1755        nolock_printk("PID %X | PPID %X | TS %X | %s (FG) | %X | %d | %s\n", 
1756        pid, ppid, state, txt_name, process_ptr, th_nr, elf_name );
1757    }
1758    else
1759    {
1760        nolock_printk("PID %X | PPID %X | TS %X | %s (BG) | %X | %d | %s\n", 
1761        pid, ppid, state, txt_name, process_ptr, th_nr, elf_name );
1762    }
1763}  // end process_display()
1764
1765
1766////////////////////////////////////////////////////////////////////////////////////////
1767//     Terminals related functions
1768////////////////////////////////////////////////////////////////////////////////////////
1769
1770////////////////////////////
1771uint32_t process_txt_alloc( void )
1772{
1773    uint32_t  index;       // TXT terminal index
1774    xptr_t    chdev_xp;    // extended pointer on TXT_RX chdev
1775    chdev_t * chdev_ptr;   // local pointer on TXT_RX chdev
1776    cxy_t     chdev_cxy;   // TXT_RX chdev cluster
1777    xptr_t    root_xp;     // extended pointer on owner field in chdev
1778
1779    // scan the user TXT_RX chdevs (TXT0 is reserved for kernel)
1780    for( index = 1 ; index < LOCAL_CLUSTER->nb_txt_channels ; index ++ )
1781    {
1782        // get pointers on TXT_RX[index]
1783        chdev_xp  = chdev_dir.txt_rx[index];
1784        chdev_cxy = GET_CXY( chdev_xp );
1785        chdev_ptr = GET_PTR( chdev_xp );
1786
1787        // get extended pointer on root of attached process
1788        root_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.root );
1789
1790        // return free TXT index if found
1791        if( xlist_is_empty( root_xp ) ) return index; 
1792    }
1793
1794    assert( false , "no free TXT terminal found" );
1795
1796    return -1;
1797
1798} // end process_txt_alloc()
1799
1800/////////////////////////////////////////////
1801void process_txt_attach( process_t * process,
1802                         uint32_t    txt_id )
1803{
1804    xptr_t      chdev_xp;     // extended pointer on TXT_RX chdev
1805    cxy_t       chdev_cxy;    // TXT_RX chdev cluster
1806    chdev_t *   chdev_ptr;    // local pointer on TXT_RX chdev
1807    xptr_t      root_xp;      // extended pointer on list root in chdev
1808    xptr_t      lock_xp;      // extended pointer on list lock in chdev
1809
1810// check process is in owner cluster
1811assert( (CXY_FROM_PID( process->pid ) == local_cxy) ,
1812"process descriptor not in owner cluster" );
1813
1814// check terminal index
1815assert( (txt_id < LOCAL_CLUSTER->nb_txt_channels) ,
1816"illegal TXT terminal index" );
1817
1818    // get pointers on TXT_RX[txt_id] chdev
1819    chdev_xp  = chdev_dir.txt_rx[txt_id];
1820    chdev_cxy = GET_CXY( chdev_xp );
1821    chdev_ptr = GET_PTR( chdev_xp );
1822
1823    // get extended pointer on root & lock of attached process list
1824    root_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.root );
1825    lock_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.lock );
1826
1827    // get lock protecting list of processes attached to TXT
1828    remote_busylock_acquire( lock_xp );
1829
1830    // insert process in attached process list
1831    xlist_add_last( root_xp , XPTR( local_cxy , &process->txt_list ) );
1832
1833    // release lock protecting list of processes attached to TXT
1834    remote_busylock_release( lock_xp );
1835
1836#if DEBUG_PROCESS_TXT
1837uint32_t cycle = (uint32_t)hal_get_cycles();
1838if( DEBUG_PROCESS_TXT < cycle )
1839printk("\n[DBG] %s : thread %x in process %x attached process %x to TXT %d / cycle %d\n",
1840__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid,
1841process->pid, txt_id , cycle );
1842#endif
1843
1844} // end process_txt_attach()
1845
1846/////////////////////////////////////////////
1847void process_txt_detach( xptr_t  process_xp )
1848{
1849    process_t * process_ptr;  // local pointer on process in owner cluster
1850    cxy_t       process_cxy;  // process owner cluster
1851    pid_t       process_pid;  // process identifier
1852    xptr_t      file_xp;      // extended pointer on stdin file
1853    xptr_t      chdev_xp;     // extended pointer on TXT_RX chdev
1854    cxy_t       chdev_cxy;    // TXT_RX chdev cluster
1855    chdev_t *   chdev_ptr;    // local pointer on TXT_RX chdev
1856    xptr_t      lock_xp;      // extended pointer on list lock in chdev
1857
1858    // get process cluster, local pointer, and PID
1859    process_cxy = GET_CXY( process_xp );
1860    process_ptr = GET_PTR( process_xp );
1861    process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
1862
1863// check process descriptor in owner cluster
1864assert( (CXY_FROM_PID( process_pid ) == process_cxy ) ,
1865"process descriptor not in owner cluster" );
1866
1867    // release TXT ownership (does nothing if not TXT owner)
1868    process_txt_transfer_ownership( process_xp );
1869
1870    // get extended pointer on process stdin file
1871    file_xp = (xptr_t)hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) );
1872
1873    // get pointers on TXT_RX chdev
1874    chdev_xp  = chdev_from_file( file_xp );
1875    chdev_cxy = GET_CXY( chdev_xp );
1876    chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
1877
1878    // get extended pointer on lock protecting attached process list
1879    lock_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.lock );
1880
1881    // get lock protecting list of processes attached to TXT
1882    remote_busylock_acquire( lock_xp );
1883
1884    // unlink process from attached process list
1885    xlist_unlink( XPTR( process_cxy , &process_ptr->txt_list ) );
1886
1887    // release lock protecting list of processes attached to TXT
1888    remote_busylock_release( lock_xp );
1889
1890#if DEBUG_PROCESS_TXT
1891uint32_t cycle  = (uint32_t)hal_get_cycles();
1892uint32_t txt_id = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) );
1893if( DEBUG_PROCESS_TXT < cycle )
1894printk("\n[DBG] %s : thread %x in process %x detached process %x from TXT %d / cycle %d\n",
1895__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid,
1896process_pid, txt_id, cycle );
1897#endif
1898
1899} // end process_txt_detach()
1900
1901///////////////////////////////////////////////////
1902void process_txt_set_ownership( xptr_t process_xp )
1903{
1904    process_t * process_ptr;
1905    cxy_t       process_cxy;
1906    pid_t       process_pid;
1907    xptr_t      file_xp;
1908    xptr_t      txt_xp;     
1909    chdev_t   * txt_ptr;
1910    cxy_t       txt_cxy;
1911
1912    // get pointers on process in owner cluster
1913    process_cxy = GET_CXY( process_xp );
1914    process_ptr = GET_PTR( process_xp );
1915    process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
1916
1917    // check owner cluster
1918    assert( (process_cxy == CXY_FROM_PID( process_pid )) ,
1919    "process descriptor not in owner cluster\n" );
1920
1921    // get extended pointer on stdin pseudo file
1922    file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) );
1923
1924    // get pointers on TXT chdev
1925    txt_xp  = chdev_from_file( file_xp );
1926    txt_cxy = GET_CXY( txt_xp );
1927    txt_ptr = GET_PTR( txt_xp );
1928
1929    // set owner field in TXT chdev
1930    hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , process_xp );
1931
1932#if DEBUG_PROCESS_TXT
1933uint32_t cycle  = (uint32_t)hal_get_cycles();
1934uint32_t txt_id = hal_remote_l32( XPTR( txt_cxy , &txt_ptr->channel ) );
1935if( DEBUG_PROCESS_TXT < cycle )
1936printk("\n[DBG] %s : thread %x in process %x give TXT %d to process %x / cycle %d\n",
1937__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, process_pid, cycle );
1938#endif
1939
1940}  // end process_txt_set ownership()
1941
1942////////////////////////////////////////////////////////
1943void process_txt_transfer_ownership( xptr_t process_xp )
1944{
1945    process_t * process_ptr;     // local pointer on process releasing ownership
1946    cxy_t       process_cxy;     // process cluster
1947    pid_t       process_pid;     // process identifier
1948    xptr_t      file_xp;         // extended pointer on TXT_RX pseudo file
1949    xptr_t      txt_xp;          // extended pointer on TXT_RX chdev
1950    chdev_t   * txt_ptr;         // local pointer on TXT_RX chdev
1951    cxy_t       txt_cxy;         // cluster of TXT_RX chdev
1952    uint32_t    txt_id;          // TXT_RX channel
1953    xptr_t      owner_xp;        // extended pointer on current TXT_RX owner
1954    xptr_t      root_xp;         // extended pointer on root of attached process list
1955    xptr_t      lock_xp;         // extended pointer on lock protecting attached process list
1956    xptr_t      iter_xp;         // iterator for xlist
1957    xptr_t      current_xp;      // extended pointer on current process
1958    process_t * current_ptr;     // local pointer on current process
1959    cxy_t       current_cxy;     // cluster for current process
1960
1961#if DEBUG_PROCESS_TXT
1962uint32_t cycle;
1963#endif
1964
1965    // get pointers on process in owner cluster
1966    process_cxy = GET_CXY( process_xp );
1967    process_ptr = GET_PTR( process_xp );
1968    process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
1969
1970    // check owner cluster
1971    assert( (process_cxy == CXY_FROM_PID( process_pid )) ,
1972    "process descriptor not in owner cluster\n" );
1973
1974    // get extended pointer on stdin pseudo file
1975    file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) );
1976
1977    // get pointers on TXT chdev
1978    txt_xp  = chdev_from_file( file_xp );
1979    txt_cxy = GET_CXY( txt_xp );
1980    txt_ptr = GET_PTR( txt_xp );
1981
1982    // get extended pointer on TXT_RX owner and TXT channel
1983    owner_xp = hal_remote_l64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) );
1984    txt_id   = hal_remote_l32 ( XPTR( txt_cxy , &txt_ptr->channel ) );
1985
1986    // transfer ownership only if process is the TXT owner
1987    if( (owner_xp == process_xp) && (txt_id > 0) ) 
1988    {
1989        // get extended pointers on root and lock of attached processes list
1990        root_xp = XPTR( txt_cxy , &txt_ptr->ext.txt.root );
1991        lock_xp = XPTR( txt_cxy , &txt_ptr->ext.txt.lock );
1992
1993        // get lock
1994        remote_busylock_acquire( lock_xp );
1995
1996        if( process_get_ppid( process_xp ) != 1 )           // process is not KSH
1997        {
1998            // scan attached process list to find KSH process
1999            XLIST_FOREACH( root_xp , iter_xp )
2000            {
2001                current_xp  = XLIST_ELEMENT( iter_xp , process_t , txt_list );
2002                current_cxy = GET_CXY( current_xp );
2003                current_ptr = GET_PTR( current_xp );
2004
2005                if( process_get_ppid( current_xp ) == 1 )  // current is KSH
2006                {
2007                    // release lock
2008                    remote_busylock_release( lock_xp );
2009
2010                    // set owner field in TXT chdev
2011                    hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , current_xp );
2012
2013#if DEBUG_PROCESS_TXT
2014cycle   = (uint32_t)hal_get_cycles();
2015uint32_t ksh_pid = hal_remote_l32( XPTR( current_cxy , &current_ptr->pid ) );
2016if( DEBUG_PROCESS_TXT < cycle )
2017printk("\n[DBG] %s : thread %x in process %x release TXT %d to KSH %x / cycle %d\n",
2018__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, ksh_pid, cycle );
2019process_txt_display( txt_id );
2020#endif
2021                     return;
2022                }
2023            }
2024 
2025            // release lock
2026            remote_busylock_release( lock_xp );
2027
2028            // PANIC if KSH not found
2029            assert( false , "KSH process not found for TXT %d" );
2030
2031            return;
2032        }
2033        else                                               // process is KSH
2034        {
2035            // scan attached process list to find another process
2036            XLIST_FOREACH( root_xp , iter_xp )
2037            {
2038                current_xp  = XLIST_ELEMENT( iter_xp , process_t , txt_list );
2039                current_cxy = GET_CXY( current_xp );
2040                current_ptr = GET_PTR( current_xp );
2041
2042                if( current_xp != process_xp )            // current is not KSH
2043                {
2044                    // release lock
2045                    remote_busylock_release( lock_xp );
2046
2047                    // set owner field in TXT chdev
2048                    hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , current_xp );
2049
2050#if DEBUG_PROCESS_TXT
2051cycle   = (uint32_t)hal_get_cycles();
2052uint32_t new_pid = hal_remote_l32( XPTR( current_cxy , &current_ptr->pid ) );
2053if( DEBUG_PROCESS_TXT < cycle )
2054printk("\n[DBG] %s : thread %x in process %x release TXT %d to process %x / cycle %d\n",
2055__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, new_pid, cycle );
2056process_txt_display( txt_id );
2057#endif
2058                     return;
2059                }
2060            }
2061
2062            // release lock
2063            remote_busylock_release( lock_xp );
2064
2065            // no more owner for TXT if no other process found
2066            hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , XPTR_NULL );
2067
2068#if DEBUG_PROCESS_TXT
2069cycle = (uint32_t)hal_get_cycles();
2070if( DEBUG_PROCESS_TXT < cycle )
2071printk("\n[DBG] %s : thread %x in process %x release TXT %d to nobody / cycle %d\n",
2072__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, cycle );
2073process_txt_display( txt_id );
2074#endif
2075            return;
2076        }
2077    }
2078    else
2079    {
2080
2081#if DEBUG_PROCESS_TXT
2082cycle = (uint32_t)hal_get_cycles();
2083if( DEBUG_PROCESS_TXT < cycle )
2084printk("\n[DBG] %s : thread %x in process %d does nothing (not TXT owner) / cycle %d\n",
2085__FUNCTION__, CURRENT_THREAD->trdid, process_pid, cycle );
2086process_txt_display( txt_id );
2087#endif
2088
2089    }
2090}  // end process_txt_transfer_ownership()
2091
2092
2093////////////////////////////////////////////////
2094bool_t process_txt_is_owner( xptr_t process_xp )
2095{
2096    // get local pointer and cluster of process in owner cluster
2097    cxy_t       process_cxy = GET_CXY( process_xp );
2098    process_t * process_ptr = GET_PTR( process_xp );
2099
2100// check calling thread execute in target process owner cluster
2101pid_t process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
2102assert( (process_cxy == CXY_FROM_PID( process_pid )) ,
2103"process descriptor not in owner cluster\n" );
2104
2105    // get extended pointer on stdin pseudo file
2106    xptr_t file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) );
2107
2108    // get pointers on TXT chdev
2109    xptr_t    txt_xp  = chdev_from_file( file_xp );
2110    cxy_t     txt_cxy = GET_CXY( txt_xp );
2111    chdev_t * txt_ptr = GET_PTR( txt_xp );
2112
2113    // get extended pointer on TXT_RX owner process
2114    xptr_t owner_xp = hal_remote_l64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) );
2115
2116    return (process_xp == owner_xp);
2117
2118}   // end process_txt_is_owner()
2119
2120////////////////////////////////////////////////     
2121xptr_t process_txt_get_owner( uint32_t channel )
2122{
2123    xptr_t      txt_rx_xp  = chdev_dir.txt_rx[channel];
2124    cxy_t       txt_rx_cxy = GET_CXY( txt_rx_xp );
2125    chdev_t *   txt_rx_ptr = GET_PTR( txt_rx_xp );
2126
2127    return (xptr_t)hal_remote_l64( XPTR( txt_rx_cxy , &txt_rx_ptr->ext.txt.owner_xp ) );
2128
2129}  // end process_txt_get_owner()
2130
2131///////////////////////////////////////////
2132void process_txt_display( uint32_t txt_id )
2133{
2134    xptr_t      chdev_xp;
2135    cxy_t       chdev_cxy;
2136    chdev_t   * chdev_ptr;
2137    xptr_t      root_xp;
2138    xptr_t      lock_xp;
2139    xptr_t      current_xp;
2140    xptr_t      iter_xp;
2141    cxy_t       txt0_cxy;
2142    chdev_t   * txt0_ptr;
2143    xptr_t      txt0_xp;
2144    xptr_t      txt0_lock_xp;
2145   
2146    assert( (txt_id < LOCAL_CLUSTER->nb_txt_channels) ,
2147    "illegal TXT terminal index" );
2148
2149    // get pointers on TXT0 chdev
2150    txt0_xp  = chdev_dir.txt_tx[0];
2151    txt0_cxy = GET_CXY( txt0_xp );
2152    txt0_ptr = GET_PTR( txt0_xp );
2153
2154    // get extended pointer on TXT0 lock
2155    txt0_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
2156
2157    // get pointers on TXT_RX[txt_id] chdev
2158    chdev_xp  = chdev_dir.txt_rx[txt_id];
2159    chdev_cxy = GET_CXY( chdev_xp );
2160    chdev_ptr = GET_PTR( chdev_xp );
2161
2162    // get extended pointer on root & lock of attached process list
2163    root_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.root );
2164    lock_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.lock );
2165
2166    // get lock on attached process list
2167    remote_busylock_acquire( lock_xp );
2168
2169    // get TXT0 lock in busy waiting mode
2170    remote_busylock_acquire( txt0_lock_xp );
2171
2172    // display header
2173    nolock_printk("\n***** processes attached to TXT_%d / cycle %d\n",
2174    txt_id , (uint32_t)hal_get_cycles() );
2175
2176    // scan attached process list
2177    XLIST_FOREACH( root_xp , iter_xp )
2178    {
2179        current_xp  = XLIST_ELEMENT( iter_xp , process_t , txt_list );
2180        process_display( current_xp );
2181    }
2182
2183    // release TXT0 lock in busy waiting mode
2184    remote_busylock_release( txt0_lock_xp );
2185
2186    // release lock on attached process list
2187    remote_busylock_release( lock_xp );
2188
2189}  // end process_txt_display
Note: See TracBrowser for help on using the repository browser.