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

Last change on this file since 443 was 443, checked in by alain, 4 years ago

Fix few bugs whike debugging the sort multi-thread application.

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