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

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

Fix a bug in function sched_handle_signal():
When the deleted user thread is the last executed thread,
the sched->u_last field must be updated to point on another user thread.

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