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

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

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

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