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

Last change on this file since 581 was 581, checked in by alain, 6 years ago

1) Improve the busylock debug infrastructure.
2) introduce a non-distributed, but portable implementation for the pthread_barrier.

File size: 77.9 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        // select 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    error_t error;
1490    pid_t   pid;
1491
1492#if DEBUG_PROCESS_ZERO_CREATE
1493uint32_t cycle = (uint32_t)hal_get_cycles();
1494if( DEBUG_PROCESS_ZERO_CREATE < cycle )
1495printk("\n[DBG] %s : enter / cluster %x / cycle %d\n",
1496__FUNCTION__, local_cxy, cycle );
1497#endif
1498
1499    // get PID from local cluster manager for this kernel process
1500    error = cluster_pid_alloc( process , &pid );
1501
1502    if( error || (LPID_FROM_PID( pid ) != 0) )
1503    {
1504        printk("\n[PANIC] in %s : cannot get valid PID in cluster %x / PID = %x\n",
1505        __FUNCTION__ , local_cxy, pid );
1506        hal_core_sleep();
1507    }
1508
1509    // initialize PID, REF_XP, PARENT_XP, and STATE
1510    // the kernel process_zero is its own parent_process,
1511    // reference_process, and owner_process, and cannot be killed...
1512    process->pid        = pid;
1513    process->ref_xp     = XPTR( local_cxy , process );
1514    process->owner_xp   = XPTR( local_cxy , process );
1515    process->parent_xp  = XPTR( local_cxy , process );
1516    process->term_state = 0;
1517
1518    // reset th_tbl[] array and associated fields
1519    uint32_t i;
1520    for( i = 0 ; i < CONFIG_THREADS_MAX_PER_CLUSTER ; i++ )
1521        {
1522        process->th_tbl[i] = NULL;
1523    }
1524    process->th_nr  = 0;
1525    rwlock_init( &process->th_lock , LOCK_PROCESS_THTBL );
1526
1527
1528    // reset children list as empty
1529    xlist_root_init( XPTR( local_cxy , &process->children_root ) );
1530    process->children_nr = 0;
1531    remote_queuelock_init( XPTR( local_cxy , &process->children_lock ),
1532                           LOCK_PROCESS_CHILDREN );
1533
1534    // register kernel process in cluster manager local_list
1535    cluster_process_local_link( process );
1536   
1537        hal_fence();
1538
1539#if DEBUG_PROCESS_ZERO_CREATE
1540cycle = (uint32_t)hal_get_cycles();
1541if( DEBUG_PROCESS_ZERO_CREATE < cycle )
1542printk("\n[DBG] %s : exit / cluster %x / cycle %d\n",
1543__FUNCTION__, local_cxy, cycle );
1544#endif
1545
1546}  // end process_zero_init()
1547
1548////////////////////////////////
1549void process_init_create( void )
1550{
1551    process_t      * process;       // local pointer on process descriptor
1552    pid_t            pid;           // process_init identifier
1553    thread_t       * thread;        // local pointer on main thread
1554    pthread_attr_t   attr;          // main thread attributes
1555    lid_t            lid;           // selected core local index for main thread
1556    xptr_t           file_xp;       // extended pointer on .elf file descriptor
1557    uint32_t         file_id;       // file index in fd_array
1558    error_t          error;
1559
1560#if DEBUG_PROCESS_INIT_CREATE
1561uint32_t cycle = (uint32_t)hal_get_cycles();
1562if( DEBUG_PROCESS_INIT_CREATE < cycle )
1563printk("\n[DBG] %s : thread %x in process %x enter / cycle %d\n",
1564__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle );
1565#endif
1566
1567    // allocates memory for process descriptor from local cluster
1568        process = process_alloc(); 
1569       
1570// check memory allocator
1571assert( (process != NULL),
1572"no memory for process descriptor in cluster %x\n", local_cxy  );
1573
1574    // get PID from local cluster
1575    error = cluster_pid_alloc( process , &pid );
1576
1577// check PID allocator
1578assert( (error == 0),
1579"cannot allocate PID in cluster %x\n", local_cxy );
1580
1581// check PID value
1582assert( (pid == 1) ,
1583"process INIT must be first process in cluster 0\n" );
1584
1585    // initialize process descriptor / parent is local process_zero
1586    process_reference_init( process,
1587                            pid,
1588                            XPTR( local_cxy , &process_zero ) ); 
1589
1590#if(DEBUG_PROCESS_INIT_CREATE & 1)
1591if( DEBUG_PROCESS_INIT_CREATE < cycle )
1592printk("\n[DBG] %s : thread %x in process %x initialized process descriptor\n",
1593__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1594#endif
1595
1596    // open the file identified by CONFIG_PROCESS_INIT_PATH
1597    file_xp = XPTR_NULL;
1598    file_id = -1;
1599        error   = vfs_open( process,
1600                            CONFIG_PROCESS_INIT_PATH,
1601                            O_RDONLY,
1602                            0,
1603                            &file_xp,
1604                            &file_id );
1605
1606assert( (error == 0),
1607"failed to open file <%s>\n", CONFIG_PROCESS_INIT_PATH );
1608
1609#if(DEBUG_PROCESS_INIT_CREATE & 1)
1610if( DEBUG_PROCESS_INIT_CREATE < cycle )
1611printk("\n[DBG] %s : thread %x in process %x open .elf file decriptor\n",
1612__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1613#endif
1614
1615   // register "code" and "data" vsegs as well as entry-point
1616    // in process VMM, using information contained in the elf file.
1617        error = elf_load_process( file_xp , process );
1618
1619assert( (error == 0),
1620"cannot access .elf file <%s>\n", CONFIG_PROCESS_INIT_PATH );
1621
1622#if(DEBUG_PROCESS_INIT_CREATE & 1)
1623if( DEBUG_PROCESS_INIT_CREATE < cycle )
1624printk("\n[DBG] %s : thread %x in process %x registered code/data vsegs in VMM\n",
1625__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1626#endif
1627
1628    // get extended pointers on process_zero children_root, children_lock
1629    xptr_t children_root_xp = XPTR( local_cxy , &process_zero.children_root );
1630    xptr_t children_lock_xp = XPTR( local_cxy , &process_zero.children_lock );
1631
1632    // take lock protecting kernel process children list
1633    remote_queuelock_acquire( children_lock_xp );
1634
1635    // register process INIT in parent local process_zero
1636        xlist_add_last( children_root_xp , XPTR( local_cxy , &process->children_list ) );
1637        hal_atomic_add( &process_zero.children_nr , 1 );
1638
1639    // release lock protecting kernel process children list
1640    remote_queuelock_release( children_lock_xp );
1641
1642#if(DEBUG_PROCESS_INIT_CREATE & 1)
1643if( DEBUG_PROCESS_INIT_CREATE < cycle )
1644printk("\n[DBG] %s : thread %x in process %x registered init process in parent\n",
1645__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1646#endif
1647
1648    // select a core in local cluster to execute the main thread
1649    lid  = cluster_select_local_core();
1650
1651    // initialize pthread attributes for main thread
1652    attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED | PT_ATTR_CORE_DEFINED;
1653    attr.cxy        = local_cxy;
1654    attr.lid        = lid;
1655
1656    // create and initialize thread descriptor
1657        error = thread_user_create( pid,
1658                                (void *)process->vmm.entry_point,
1659                                NULL,
1660                                &attr,
1661                                &thread );
1662
1663assert( (error == 0),
1664"cannot create main thread for <%s>\n", CONFIG_PROCESS_INIT_PATH );
1665
1666assert( (thread->trdid == 0),
1667"main thread must have index 0 for <%s>\n", CONFIG_PROCESS_INIT_PATH );
1668
1669#if(DEBUG_PROCESS_INIT_CREATE & 1)
1670if( DEBUG_PROCESS_INIT_CREATE < cycle )
1671printk("\n[DBG] %s : thread %x in process %x created main thread\n",
1672__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid );
1673#endif
1674
1675    // activate thread
1676        thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
1677
1678    hal_fence();
1679
1680#if DEBUG_PROCESS_INIT_CREATE
1681cycle = (uint32_t)hal_get_cycles();
1682if( DEBUG_PROCESS_INIT_CREATE < cycle )
1683printk("\n[DBG] %s : thread %x in process %x exit / cycle %d\n",
1684__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle );
1685#endif
1686
1687}  // end process_init_create()
1688
1689/////////////////////////////////////////
1690void process_display( xptr_t process_xp )
1691{
1692    process_t   * process_ptr;
1693    cxy_t         process_cxy;
1694
1695    xptr_t        parent_xp;       // extended pointer on parent process
1696    process_t   * parent_ptr;
1697    cxy_t         parent_cxy;
1698
1699    xptr_t        owner_xp;        // extended pointer on owner process
1700    process_t   * owner_ptr;
1701    cxy_t         owner_cxy;
1702
1703    pid_t         pid;
1704    pid_t         ppid;
1705    lpid_t        lpid;
1706    uint32_t      state;
1707    uint32_t      th_nr;
1708
1709    xptr_t        txt_file_xp;     // extended pointer on TXT_RX file descriptor
1710    xptr_t        txt_chdev_xp;    // extended pointer on TXT_RX chdev
1711    chdev_t     * txt_chdev_ptr;
1712    cxy_t         txt_chdev_cxy;
1713    xptr_t        txt_owner_xp;    // extended pointer on TXT owner process
1714
1715    xptr_t        elf_file_xp;     // extended pointer on .elf file
1716    cxy_t         elf_file_cxy;
1717    vfs_file_t  * elf_file_ptr;
1718    vfs_inode_t * elf_inode_ptr;   // local pointer on .elf inode
1719
1720    char          txt_name[CONFIG_VFS_MAX_NAME_LENGTH];
1721    char          elf_name[CONFIG_VFS_MAX_NAME_LENGTH];
1722
1723    // get cluster and local pointer on process
1724    process_ptr = GET_PTR( process_xp );
1725    process_cxy = GET_CXY( process_xp );
1726
1727    // get process PID, LPID, and state
1728    pid   = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
1729    lpid  = LPID_FROM_PID( pid );
1730    state = hal_remote_l32( XPTR( process_cxy , &process_ptr->term_state ) );
1731
1732    // get process PPID
1733    parent_xp  = hal_remote_l64( XPTR( process_cxy , &process_ptr->parent_xp ) );
1734    parent_cxy = GET_CXY( parent_xp );
1735    parent_ptr = GET_PTR( parent_xp );
1736    ppid       = hal_remote_l32( XPTR( parent_cxy , &parent_ptr->pid ) );
1737
1738    // get number of threads
1739    th_nr      = hal_remote_l32( XPTR( process_cxy , &process_ptr->th_nr ) );
1740
1741    // get pointers on owner process descriptor
1742    owner_xp  = hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ) );
1743    owner_cxy = GET_CXY( owner_xp );
1744    owner_ptr = GET_PTR( owner_xp );
1745
1746    // get process TXT name and .elf name
1747    if( lpid )                                   // user process
1748    {
1749
1750        // get extended pointer on file descriptor associated to TXT_RX
1751        txt_file_xp = hal_remote_l64( XPTR( owner_cxy , &owner_ptr->fd_array.array[0] ) );
1752
1753        assert( (txt_file_xp != XPTR_NULL) ,
1754        "process must be attached to one TXT terminal\n" ); 
1755
1756        // get TXT_RX chdev pointers
1757        txt_chdev_xp  = chdev_from_file( txt_file_xp );
1758        txt_chdev_cxy = GET_CXY( txt_chdev_xp );
1759        txt_chdev_ptr = GET_PTR( txt_chdev_xp );
1760
1761        // get TXT_RX name and ownership
1762        hal_remote_strcpy( XPTR( local_cxy , txt_name ) ,
1763                           XPTR( txt_chdev_cxy , txt_chdev_ptr->name ) );
1764   
1765        txt_owner_xp = (xptr_t)hal_remote_l64( XPTR( txt_chdev_cxy, 
1766                                                     &txt_chdev_ptr->ext.txt.owner_xp ) );
1767
1768        // get process .elf name
1769        elf_file_xp   = hal_remote_l64( XPTR( process_cxy , &process_ptr->vfs_bin_xp ) );
1770        elf_file_cxy  = GET_CXY( elf_file_xp );
1771        elf_file_ptr  = GET_PTR( elf_file_xp );
1772        elf_inode_ptr = hal_remote_lpt( XPTR( elf_file_cxy , &elf_file_ptr->inode ) );
1773        vfs_inode_get_name( XPTR( elf_file_cxy , elf_inode_ptr ) , elf_name );
1774    }
1775    else                                         // kernel process_zero
1776    {
1777        // TXT name and .elf name are not registered in kernel process_zero
1778        strcpy( txt_name , "txt0_rx" );
1779        txt_owner_xp = process_xp; 
1780        strcpy( elf_name , "kernel.elf" );
1781    }
1782
1783    // display process info
1784    if( txt_owner_xp == process_xp )
1785    {
1786        nolock_printk("PID %X | %s (FG) | %X | PPID %X | TS %X | %d | %s\n", 
1787        pid, txt_name, process_ptr, ppid, state, th_nr, elf_name );
1788    }
1789    else
1790    {
1791        nolock_printk("PID %X | %s (BG) | %X | PPID %X | TS %X | %d | %s\n", 
1792        pid, txt_name, process_ptr, ppid, state, th_nr, elf_name );
1793    }
1794}  // end process_display()
1795
1796
1797////////////////////////////////////////////////////////////////////////////////////////
1798//     Terminals related functions
1799////////////////////////////////////////////////////////////////////////////////////////
1800
1801//////////////////////////////////
1802uint32_t process_txt_alloc( void )
1803{
1804    uint32_t  index;       // TXT terminal index
1805    xptr_t    chdev_xp;    // extended pointer on TXT_RX chdev
1806    chdev_t * chdev_ptr;   // local pointer on TXT_RX chdev
1807    cxy_t     chdev_cxy;   // TXT_RX chdev cluster
1808    xptr_t    root_xp;     // extended pointer on owner field in chdev
1809
1810    // scan the user TXT_RX chdevs (TXT0 is reserved for kernel)
1811    for( index = 1 ; index < LOCAL_CLUSTER->nb_txt_channels ; index ++ )
1812    {
1813        // get pointers on TXT_RX[index]
1814        chdev_xp  = chdev_dir.txt_rx[index];
1815        chdev_cxy = GET_CXY( chdev_xp );
1816        chdev_ptr = GET_PTR( chdev_xp );
1817
1818        // get extended pointer on root of attached process
1819        root_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.root );
1820
1821        // return free TXT index if found
1822        if( xlist_is_empty( root_xp ) ) return index; 
1823    }
1824
1825    assert( false , "no free TXT terminal found" );
1826
1827    return -1;
1828
1829} // end process_txt_alloc()
1830
1831/////////////////////////////////////////////
1832void process_txt_attach( process_t * process,
1833                         uint32_t    txt_id )
1834{
1835    xptr_t      chdev_xp;     // extended pointer on TXT_RX chdev
1836    cxy_t       chdev_cxy;    // TXT_RX chdev cluster
1837    chdev_t *   chdev_ptr;    // local pointer on TXT_RX chdev
1838    xptr_t      root_xp;      // extended pointer on list root in chdev
1839    xptr_t      lock_xp;      // extended pointer on list lock in chdev
1840
1841// check process is in owner cluster
1842assert( (CXY_FROM_PID( process->pid ) == local_cxy) ,
1843"process descriptor not in owner cluster" );
1844
1845// check terminal index
1846assert( (txt_id < LOCAL_CLUSTER->nb_txt_channels) ,
1847"illegal TXT terminal index" );
1848
1849    // get pointers on TXT_RX[txt_id] chdev
1850    chdev_xp  = chdev_dir.txt_rx[txt_id];
1851    chdev_cxy = GET_CXY( chdev_xp );
1852    chdev_ptr = GET_PTR( chdev_xp );
1853
1854    // get extended pointer on root & lock of attached process list
1855    root_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.root );
1856    lock_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.lock );
1857
1858    // get lock protecting list of processes attached to TXT
1859    remote_busylock_acquire( lock_xp );
1860
1861    // insert process in attached process list
1862    xlist_add_last( root_xp , XPTR( local_cxy , &process->txt_list ) );
1863
1864    // release lock protecting list of processes attached to TXT
1865    remote_busylock_release( lock_xp );
1866
1867#if DEBUG_PROCESS_TXT
1868uint32_t cycle = (uint32_t)hal_get_cycles();
1869if( DEBUG_PROCESS_TXT < cycle )
1870printk("\n[DBG] %s : thread %x in process %x attached process %x to TXT %d / cycle %d\n",
1871__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid,
1872process->pid, txt_id , cycle );
1873#endif
1874
1875} // end process_txt_attach()
1876
1877/////////////////////////////////////////////
1878void process_txt_detach( xptr_t  process_xp )
1879{
1880    process_t * process_ptr;  // local pointer on process in owner cluster
1881    cxy_t       process_cxy;  // process owner cluster
1882    pid_t       process_pid;  // process identifier
1883    xptr_t      file_xp;      // extended pointer on stdin file
1884    xptr_t      chdev_xp;     // extended pointer on TXT_RX chdev
1885    cxy_t       chdev_cxy;    // TXT_RX chdev cluster
1886    chdev_t *   chdev_ptr;    // local pointer on TXT_RX chdev
1887    xptr_t      lock_xp;      // extended pointer on list lock in chdev
1888
1889    // get process cluster, local pointer, and PID
1890    process_cxy = GET_CXY( process_xp );
1891    process_ptr = GET_PTR( process_xp );
1892    process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
1893
1894// check process descriptor in owner cluster
1895assert( (CXY_FROM_PID( process_pid ) == process_cxy ) ,
1896"process descriptor not in owner cluster" );
1897
1898    // release TXT ownership (does nothing if not TXT owner)
1899    process_txt_transfer_ownership( process_xp );
1900
1901    // get extended pointer on process stdin file
1902    file_xp = (xptr_t)hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) );
1903
1904    // get pointers on TXT_RX chdev
1905    chdev_xp  = chdev_from_file( file_xp );
1906    chdev_cxy = GET_CXY( chdev_xp );
1907    chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
1908
1909    // get extended pointer on lock protecting attached process list
1910    lock_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.lock );
1911
1912    // get lock protecting list of processes attached to TXT
1913    remote_busylock_acquire( lock_xp );
1914
1915    // unlink process from attached process list
1916    xlist_unlink( XPTR( process_cxy , &process_ptr->txt_list ) );
1917
1918    // release lock protecting list of processes attached to TXT
1919    remote_busylock_release( lock_xp );
1920
1921#if DEBUG_PROCESS_TXT
1922uint32_t cycle  = (uint32_t)hal_get_cycles();
1923uint32_t txt_id = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) );
1924if( DEBUG_PROCESS_TXT < cycle )
1925printk("\n[DBG] %s : thread %x in process %x detached process %x from TXT %d / cycle %d\n",
1926__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid,
1927process_pid, txt_id, cycle );
1928#endif
1929
1930} // end process_txt_detach()
1931
1932///////////////////////////////////////////////////
1933void process_txt_set_ownership( xptr_t process_xp )
1934{
1935    process_t * process_ptr;
1936    cxy_t       process_cxy;
1937    pid_t       process_pid;
1938    xptr_t      file_xp;
1939    xptr_t      txt_xp;     
1940    chdev_t   * txt_ptr;
1941    cxy_t       txt_cxy;
1942
1943    // get pointers on process in owner cluster
1944    process_cxy = GET_CXY( process_xp );
1945    process_ptr = GET_PTR( process_xp );
1946    process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
1947
1948    // check owner cluster
1949    assert( (process_cxy == CXY_FROM_PID( process_pid )) ,
1950    "process descriptor not in owner cluster\n" );
1951
1952    // get extended pointer on stdin pseudo file
1953    file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) );
1954
1955    // get pointers on TXT chdev
1956    txt_xp  = chdev_from_file( file_xp );
1957    txt_cxy = GET_CXY( txt_xp );
1958    txt_ptr = GET_PTR( txt_xp );
1959
1960    // set owner field in TXT chdev
1961    hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , process_xp );
1962
1963#if DEBUG_PROCESS_TXT
1964uint32_t cycle  = (uint32_t)hal_get_cycles();
1965uint32_t txt_id = hal_remote_l32( XPTR( txt_cxy , &txt_ptr->channel ) );
1966if( DEBUG_PROCESS_TXT < cycle )
1967printk("\n[DBG] %s : thread %x in process %x give TXT %d to process %x / cycle %d\n",
1968__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, process_pid, cycle );
1969#endif
1970
1971}  // end process_txt_set ownership()
1972
1973////////////////////////////////////////////////////////
1974void process_txt_transfer_ownership( xptr_t process_xp )
1975{
1976    process_t * process_ptr;     // local pointer on process releasing ownership
1977    cxy_t       process_cxy;     // process cluster
1978    pid_t       process_pid;     // process identifier
1979    xptr_t      file_xp;         // extended pointer on TXT_RX pseudo file
1980    xptr_t      txt_xp;          // extended pointer on TXT_RX chdev
1981    chdev_t   * txt_ptr;         // local pointer on TXT_RX chdev
1982    cxy_t       txt_cxy;         // cluster of TXT_RX chdev
1983    uint32_t    txt_id;          // TXT_RX channel
1984    xptr_t      owner_xp;        // extended pointer on current TXT_RX owner
1985    xptr_t      root_xp;         // extended pointer on root of attached process list
1986    xptr_t      lock_xp;         // extended pointer on lock protecting attached process list
1987    xptr_t      iter_xp;         // iterator for xlist
1988    xptr_t      current_xp;      // extended pointer on current process
1989    process_t * current_ptr;     // local pointer on current process
1990    cxy_t       current_cxy;     // cluster for current process
1991
1992#if DEBUG_PROCESS_TXT
1993uint32_t cycle;
1994#endif
1995
1996    // get pointers on process in owner cluster
1997    process_cxy = GET_CXY( process_xp );
1998    process_ptr = GET_PTR( process_xp );
1999    process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
2000
2001    // check owner cluster
2002    assert( (process_cxy == CXY_FROM_PID( process_pid )) ,
2003    "process descriptor not in owner cluster\n" );
2004
2005    // get extended pointer on stdin pseudo file
2006    file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) );
2007
2008    // get pointers on TXT chdev
2009    txt_xp  = chdev_from_file( file_xp );
2010    txt_cxy = GET_CXY( txt_xp );
2011    txt_ptr = GET_PTR( txt_xp );
2012
2013    // get extended pointer on TXT_RX owner and TXT channel
2014    owner_xp = hal_remote_l64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) );
2015    txt_id   = hal_remote_l32 ( XPTR( txt_cxy , &txt_ptr->channel ) );
2016
2017    // transfer ownership only if process is the TXT owner
2018    if( (owner_xp == process_xp) && (txt_id > 0) ) 
2019    {
2020        // get extended pointers on root and lock of attached processes list
2021        root_xp = XPTR( txt_cxy , &txt_ptr->ext.txt.root );
2022        lock_xp = XPTR( txt_cxy , &txt_ptr->ext.txt.lock );
2023
2024        // get lock
2025        remote_busylock_acquire( lock_xp );
2026
2027        if( process_get_ppid( process_xp ) != 1 )           // process is not KSH
2028        {
2029            // scan attached process list to find KSH process
2030            XLIST_FOREACH( root_xp , iter_xp )
2031            {
2032                current_xp  = XLIST_ELEMENT( iter_xp , process_t , txt_list );
2033                current_cxy = GET_CXY( current_xp );
2034                current_ptr = GET_PTR( current_xp );
2035
2036                if( process_get_ppid( current_xp ) == 1 )  // current is KSH
2037                {
2038                    // release lock
2039                    remote_busylock_release( lock_xp );
2040
2041                    // set owner field in TXT chdev
2042                    hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , current_xp );
2043
2044#if DEBUG_PROCESS_TXT
2045cycle   = (uint32_t)hal_get_cycles();
2046uint32_t ksh_pid = hal_remote_l32( XPTR( current_cxy , &current_ptr->pid ) );
2047if( DEBUG_PROCESS_TXT < cycle )
2048printk("\n[DBG] %s : thread %x in process %x release TXT %d to KSH %x / cycle %d\n",
2049__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, ksh_pid, cycle );
2050process_txt_display( txt_id );
2051#endif
2052                     return;
2053                }
2054            }
2055 
2056            // release lock
2057            remote_busylock_release( lock_xp );
2058
2059            // PANIC if KSH not found
2060            assert( false , "KSH process not found for TXT %d" );
2061
2062            return;
2063        }
2064        else                                               // process is KSH
2065        {
2066            // scan attached process list to find another process
2067            XLIST_FOREACH( root_xp , iter_xp )
2068            {
2069                current_xp  = XLIST_ELEMENT( iter_xp , process_t , txt_list );
2070                current_cxy = GET_CXY( current_xp );
2071                current_ptr = GET_PTR( current_xp );
2072
2073                if( current_xp != process_xp )            // current is not KSH
2074                {
2075                    // release lock
2076                    remote_busylock_release( lock_xp );
2077
2078                    // set owner field in TXT chdev
2079                    hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , current_xp );
2080
2081#if DEBUG_PROCESS_TXT
2082cycle   = (uint32_t)hal_get_cycles();
2083uint32_t new_pid = hal_remote_l32( XPTR( current_cxy , &current_ptr->pid ) );
2084if( DEBUG_PROCESS_TXT < cycle )
2085printk("\n[DBG] %s : thread %x in process %x release TXT %d to process %x / cycle %d\n",
2086__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, new_pid, cycle );
2087process_txt_display( txt_id );
2088#endif
2089                     return;
2090                }
2091            }
2092
2093            // release lock
2094            remote_busylock_release( lock_xp );
2095
2096            // no more owner for TXT if no other process found
2097            hal_remote_s64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) , XPTR_NULL );
2098
2099#if DEBUG_PROCESS_TXT
2100cycle = (uint32_t)hal_get_cycles();
2101if( DEBUG_PROCESS_TXT < cycle )
2102printk("\n[DBG] %s : thread %x in process %x release TXT %d to nobody / cycle %d\n",
2103__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, txt_id, cycle );
2104process_txt_display( txt_id );
2105#endif
2106            return;
2107        }
2108    }
2109    else
2110    {
2111
2112#if DEBUG_PROCESS_TXT
2113cycle = (uint32_t)hal_get_cycles();
2114if( DEBUG_PROCESS_TXT < cycle )
2115printk("\n[DBG] %s : thread %x in process %d does nothing (not TXT owner) / cycle %d\n",
2116__FUNCTION__, CURRENT_THREAD->trdid, process_pid, cycle );
2117process_txt_display( txt_id );
2118#endif
2119
2120    }
2121}  // end process_txt_transfer_ownership()
2122
2123
2124////////////////////////////////////////////////
2125bool_t process_txt_is_owner( xptr_t process_xp )
2126{
2127    // get local pointer and cluster of process in owner cluster
2128    cxy_t       process_cxy = GET_CXY( process_xp );
2129    process_t * process_ptr = GET_PTR( process_xp );
2130
2131// check calling thread execute in target process owner cluster
2132pid_t process_pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) );
2133assert( (process_cxy == CXY_FROM_PID( process_pid )) ,
2134"process descriptor not in owner cluster\n" );
2135
2136    // get extended pointer on stdin pseudo file
2137    xptr_t file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[0] ) );
2138
2139    // get pointers on TXT chdev
2140    xptr_t    txt_xp  = chdev_from_file( file_xp );
2141    cxy_t     txt_cxy = GET_CXY( txt_xp );
2142    chdev_t * txt_ptr = GET_PTR( txt_xp );
2143
2144    // get extended pointer on TXT_RX owner process
2145    xptr_t owner_xp = hal_remote_l64( XPTR( txt_cxy , &txt_ptr->ext.txt.owner_xp ) );
2146
2147    return (process_xp == owner_xp);
2148
2149}   // end process_txt_is_owner()
2150
2151////////////////////////////////////////////////     
2152xptr_t process_txt_get_owner( uint32_t channel )
2153{
2154    xptr_t      txt_rx_xp  = chdev_dir.txt_rx[channel];
2155    cxy_t       txt_rx_cxy = GET_CXY( txt_rx_xp );
2156    chdev_t *   txt_rx_ptr = GET_PTR( txt_rx_xp );
2157
2158    return (xptr_t)hal_remote_l64( XPTR( txt_rx_cxy , &txt_rx_ptr->ext.txt.owner_xp ) );
2159
2160}  // end process_txt_get_owner()
2161
2162///////////////////////////////////////////
2163void process_txt_display( uint32_t txt_id )
2164{
2165    xptr_t      chdev_xp;
2166    cxy_t       chdev_cxy;
2167    chdev_t   * chdev_ptr;
2168    xptr_t      root_xp;
2169    xptr_t      lock_xp;
2170    xptr_t      current_xp;
2171    xptr_t      iter_xp;
2172    cxy_t       txt0_cxy;
2173    chdev_t   * txt0_ptr;
2174    xptr_t      txt0_xp;
2175    xptr_t      txt0_lock_xp;
2176   
2177    assert( (txt_id < LOCAL_CLUSTER->nb_txt_channels) ,
2178    "illegal TXT terminal index" );
2179
2180    // get pointers on TXT0 chdev
2181    txt0_xp  = chdev_dir.txt_tx[0];
2182    txt0_cxy = GET_CXY( txt0_xp );
2183    txt0_ptr = GET_PTR( txt0_xp );
2184
2185    // get extended pointer on TXT0 lock
2186    txt0_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
2187
2188    // get pointers on TXT_RX[txt_id] chdev
2189    chdev_xp  = chdev_dir.txt_rx[txt_id];
2190    chdev_cxy = GET_CXY( chdev_xp );
2191    chdev_ptr = GET_PTR( chdev_xp );
2192
2193    // get extended pointer on root & lock of attached process list
2194    root_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.root );
2195    lock_xp = XPTR( chdev_cxy , &chdev_ptr->ext.txt.lock );
2196
2197    // get lock on attached process list
2198    remote_busylock_acquire( lock_xp );
2199
2200    // get TXT0 lock in busy waiting mode
2201    remote_busylock_acquire( txt0_lock_xp );
2202
2203    // display header
2204    nolock_printk("\n***** processes attached to TXT_%d / cycle %d\n",
2205    txt_id , (uint32_t)hal_get_cycles() );
2206
2207    // scan attached process list
2208    XLIST_FOREACH( root_xp , iter_xp )
2209    {
2210        current_xp  = XLIST_ELEMENT( iter_xp , process_t , txt_list );
2211        process_display( current_xp );
2212    }
2213
2214    // release TXT0 lock in busy waiting mode
2215    remote_busylock_release( txt0_lock_xp );
2216
2217    // release lock on attached process list
2218    remote_busylock_release( lock_xp );
2219
2220}  // end process_txt_display
Note: See TracBrowser for help on using the repository browser.