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

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

Fix a bug in rpc_vmm_get_pte_client() function (bad RPC index).

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