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

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

miscelaneous...

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