Changeset 446 for trunk/kernel/syscalls


Ignore:
Timestamp:
Jun 19, 2018, 5:12:57 PM (6 years ago)
Author:
alain
Message:

miscelaneous...

Location:
trunk/kernel/syscalls
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_exit.c

    r445 r446  
    3636int sys_exit( uint32_t status )
    3737{
    38     reg_t       save_sr;       // required to enable IRQs
     38    reg_t       save_sr;           // required to enable IRQs
     39
     40    xptr_t      owner_xp;          // extended pointer on owner process
     41    cxy_t       owner_cxy;         // owner process cluster
     42    process_t * owner_ptr;         // local pointer on owner process
     43    thread_t  * main_ptr;          // local pointer on process main thread
     44    xptr_t      parent_xp;         // extended pointer on parent process
     45    cxy_t       parent_cxy;        // parent process cluster
     46    process_t * parent_ptr;        // local pointer on parent process
     47    xptr_t      children_lock_xp;  // extended pointer on children locki
     48    thread_t  * parent_main_ptr;   // local pointer on parent main thread
     49    xptr_t      parent_main_xp;    // extended pointer on parent main thread
     50    uint32_t    term_state;        // termination status for owner process
    3951
    4052    thread_t  * this    = CURRENT_THREAD;
     
    5264
    5365    // get owner process descriptor pointers
    54     xptr_t      owner_xp  = process->owner_xp;
    55     cxy_t       owner_cxy = GET_CXY( owner_xp );
    56     process_t * owner_ptr = GET_PTR( owner_xp );
     66    owner_xp  = process->owner_xp;
     67    owner_cxy = GET_CXY( owner_xp );
     68    owner_ptr = GET_PTR( owner_xp );
    5769
    58     // get pointers on the process main thread
    59     thread_t * main    = hal_remote_lpt( XPTR( owner_cxy , &owner_ptr->th_tbl[0] ) );
    60 
    61     // enable IRQs
    62     hal_enable_irq( &save_sr );
    63 
    64     // mark for delete all process threads in all clusters,
    65     // but the main thread and this calling thread
    66     process_sigaction( pid , DELETE_ALL_THREADS );
    67 
    68     // disable IRQs
    69     hal_restore_irq( save_sr );
    70 
    71 #if( DEBUG_SYS_EXIT & 1)
    72 if( tm_start > DEBUG_SYS_EXIT )
    73 printk("\n[DBG] %s : thread %x deleted threads / process %x\n",
    74 __FUNCTION__ , this, pid );
     70#if (DEBUG_SYS_EXIT & 1)
     71if( DEBUG_SYS_EXIT < tm_start )
     72printk("\n[DBG] %s : thread %x get owner process %x in cluster %x\n",
     73__FUNCTION__ , this, owner_ptr, owner_cxy );
    7574#endif
    7675
    77     // mark for delete this calling thread when it is not the main
    78     if( (owner_cxy != local_cxy) || (main != this) )
    79     {
     76    // get pointer on the process main thread
     77    main_ptr  = hal_remote_lpt( XPTR( owner_cxy , &owner_ptr->th_tbl[0] ) );
    8078
    81 #if( DEBUG_SYS_EXIT & 1)
    82 if( tm_start > DEBUG_SYS_EXIT )
    83 printk("\n[DBG] %s : calling thread %x marked iself for delete / process %x\n",
    84 __FUNCTION__ , this, pid );
     79    // get parent process descriptor pointers
     80    parent_xp  = process->parent_xp;
     81    parent_cxy = GET_CXY( parent_xp );
     82    parent_ptr = GET_PTR( parent_xp );
     83
     84#if (DEBUG_SYS_EXIT & 1)
     85if( DEBUG_SYS_EXIT < tm_start )
     86printk("\n[DBG] %s : thread %x get parent process %x in cluster %x\n",
     87__FUNCTION__ , this, parent_ptr, parent_cxy );
    8588#endif
    86         thread_delete( XPTR( local_cxy , this ) , pid , true );
    87     }
     89
     90    // get extended pointer on lock protecting children list in parent process
     91    children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock );
     92
     93    // get pointers on the parent process main thread
     94    parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->th_tbl[0] ) );
     95    parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
    8896
    8997    // remove process from TXT list
     
    96104#endif
    97105
    98     // block the main thread
    99     thread_block( XPTR( owner_cxy , main ) , THREAD_BLOCKED_GLOBAL );
     106    // mark for delete all process threads in all clusters,
     107    // but the main thread and this calling thread
     108    hal_enable_irq( &save_sr );
     109    process_sigaction( pid , DELETE_ALL_THREADS );
     110    hal_restore_irq( save_sr );
     111
     112#if( DEBUG_SYS_EXIT & 1)
     113if( tm_start > DEBUG_SYS_EXIT )
     114printk("\n[DBG] %s : thread %x deleted threads for process %x\n",
     115__FUNCTION__ , this, pid );
     116#endif
     117
     118    // mark for delete this calling thread when it is not the main
     119    if( (owner_cxy != local_cxy) || (main_ptr != this) )
     120    {
     121
     122#if( DEBUG_SYS_EXIT & 1)
     123if( tm_start > DEBUG_SYS_EXIT )
     124printk("\n[DBG] %s : calling thread %x marked iself for delete in process %x\n",
     125__FUNCTION__ , this, pid );
     126#endif
     127        thread_delete( XPTR( local_cxy , this ) , pid , true );
     128    }
     129
     130    // block this main thread
     131    thread_block( XPTR( owner_cxy , main_ptr ) , THREAD_BLOCKED_GLOBAL );
    100132
    101133#if( DEBUG_SYS_EXIT & 1)
     
    107139    // atomically update owner process descriptor term_state to ask
    108140    // the parent process sys_wait() function to delete the main thread
    109     hal_remote_atomic_or( XPTR( owner_cxy , &process->term_state ) ,
    110                           PROCESS_TERM_EXIT | (status & 0xFF) );
     141    term_state = (status & 0xFF) | PROCESS_TERM_EXIT;
     142    hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , term_state );
    111143
    112144#if( DEBUG_SYS_EXIT & 1)
    113145if( tm_start > DEBUG_SYS_EXIT )
    114 printk("\n[DBG] %s : thread %x set exit status in process %x term_state\n",
    115 __FUNCTION__ , this, pid );
     146printk("\n[DBG] %s : thread %x set exit status in process %x / term_state %x\n",
     147__FUNCTION__ , this, pid, term_state );
     148#endif
     149
     150    // take the children lock and unblock the parent process main thread
     151    remote_spinlock_lock( children_lock_xp );
     152    thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
     153    remote_spinlock_unlock( children_lock_xp );
     154
     155#if( DEBUG_SYS_EXIT & 1)
     156if( tm_start > DEBUG_SYS_EXIT )
     157printk("\n[DBG] %s : thread %x in cluster %x unblock parent main thread %x in cluster %x\n",
     158__FUNCTION__ , this, local_cxy, parent_main_ptr, parent_cxy );
    116159#endif
    117160
  • trunk/kernel/syscalls/sys_fg.c

    r443 r446  
    3333#include <rpc.h>
    3434
    35 //////////////////////////
    36 int sys_fg( pid_t    pid )
     35///////////////////////
     36int sys_fg( pid_t pid )
    3737{
    3838    xptr_t      process_xp;     // extended pointer on reference process descriptor
     
    8585    hal_remote_swd( XPTR( chdev_cxy , &chdev_ptr->ext.txt.owner_xp ) , process_xp );
    8686
     87    // reset PROCESS_TERM_WAIT and PROCESS_TERM_STOP flags in process term_state
     88    hal_remote_atomic_and( XPTR( process_cxy , &process_ptr->term_state ),
     89                                 ~(PROCESS_TERM_WAIT | PROCESS_TERM_STOP) );
     90 
    8791    hal_fence();
    8892
  • trunk/kernel/syscalls/sys_kill.c

    r440 r446  
    3737              uint32_t sig_id )
    3838{
    39     xptr_t      owner_xp;      // extended pointer on target process in owner cluster
    40     cxy_t       owner_cxy;     // target process in owner cluster
    41     process_t * owner_ptr;     // local pointer on target process in owner cluster
    42     uint32_t    retval;        // return value for the switch
     39    reg_t       save_sr;           // required to enable IRQs
     40    xptr_t      owner_xp;          // extended pointer on process in owner cluster
     41    cxy_t       owner_cxy;         // process owner cluster
     42    process_t * owner_ptr;         // local pointer on process in owner cluster
     43    uint32_t    retval;            // return value for the switch
     44    xptr_t      parent_xp;         // extended pointer on parent process
     45    cxy_t       parent_cxy;        // parent process cluster
     46    process_t * parent_ptr;        // local pointer on parent process
     47    xptr_t      children_lock_xp;  // extended pointer on children lock in parent
     48    thread_t  * parent_main_ptr;   // local pointer on parent main thread
     49    xptr_t      parent_main_xp;    // extended pointer on parent main thread
    4350
    4451    thread_t  * this    = CURRENT_THREAD;
     
    5966    owner_ptr = GET_PTR( owner_xp );
    6067   
     68#if (DEBUG_SYS_KILL & 1)
     69if( DEBUG_SYS_KILL < tm_start )
     70printk("\n[DBG] %s : thread %x found process %x in cluster %x\n",
     71__FUNCTION__ , this, owner_ptr, owner_cxy );
     72#endif
     73
    6174    // check process found
    6275    if( owner_xp == XPTR_NULL)
     
    92105    }
    93106
     107    // get parent process descriptor pointers
     108    parent_xp  = (xptr_t)hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) );
     109    parent_cxy = GET_CXY( parent_xp );
     110    parent_ptr = GET_PTR( parent_xp );
     111
     112#if (DEBUG_SYS_KILL & 1)
     113if( DEBUG_SYS_KILL < tm_start )
     114printk("\n[DBG] %s : thread %x get parent process %x in cluster %x\n",
     115__FUNCTION__ , this, parent_ptr, parent_cxy );
     116#endif
     117
     118    // get extended pointer on lock protecting children list in parent process
     119    children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock );
     120
     121    // get pointers on the parent process main thread
     122    parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->th_tbl[0] ) );
     123    parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
     124
    94125    // analyse signal type / supported values are : 0, SIGSTOP, SIGCONT, SIGKILL
    95126    switch( sig_id )
     
    108139            process_sigaction( pid , BLOCK_ALL_THREADS );
    109140
    110             // get pointer on target process main thread
     141            // block the main thread
    111142            xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
    112 
    113             // block main thread
    114143            thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
    115144
     
    117146            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
    118147                                  PROCESS_TERM_STOP );
     148
     149            // take the children lock and unblock the parent process main thread
     150            remote_spinlock_lock( children_lock_xp );
     151            thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
     152            remote_spinlock_unlock( children_lock_xp );
     153
    119154            retval = 0;
    120155            break;
     
    137172            process_txt_detach( owner_xp );
    138173
    139             // mark for delete all process threads in all clusters, but the main
     174            // mark for delete all threads in all clusters, but the main
     175            hal_enable_irq( &save_sr );
    140176            process_sigaction( pid , DELETE_ALL_THREADS );
    141 
    142             // get pointer on target process main thread
     177            hal_restore_irq( save_sr );
     178
     179            // block main thread
    143180            xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
    144 
    145             // block main thread
    146181            thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
    147182
     
    150185            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
    151186                                  PROCESS_TERM_KILL );
     187
     188            // take the children lock and unblock the parent process main thread
     189            remote_spinlock_lock( children_lock_xp );
     190            thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
     191            remote_spinlock_unlock( children_lock_xp );
     192
    152193            retval = 0;
    153194            break;
  • trunk/kernel/syscalls/sys_read.c

    r443 r446  
    6363    reg_t        save_sr;     // required to enable IRQs during syscall
    6464
    65         thread_t  *  this    = CURRENT_THREAD;
    66         process_t *  process = this->process;
     65        thread_t  *  this             = CURRENT_THREAD;
     66        process_t *  process          = this->process;
     67    xptr_t       process_owner_xp = process->owner_xp;
    6768 
    6869#if DEBUG_SYS_READ
     
    165166                                count );
    166167    }
    167     else if( type == INODE_TYPE_DEV )  // check ownership & read from from device
     168    else if( type == INODE_TYPE_DEV )  // check ownership & read from device
    168169    {
    169170        // get cluster and pointers on TXT_RX chdev
     
    172173        chdev_t * chdev_ptr = GET_PTR( chdev_xp );
    173174
    174         volatile xptr_t    owner_xp;   
     175        volatile xptr_t    txt_owner_xp;   
     176        uint32_t           iter = 0;
    175177
    176178        while( 1 )
    177179        {
    178             // extended pointer on owner process
    179             owner_xp  = hal_remote_lwd( XPTR( chdev_cxy , &chdev_ptr->ext.txt.owner_xp ) );
     180            // extended pointer on TXT owner process
     181            txt_owner_xp  = hal_remote_lwd( XPTR( chdev_cxy , &chdev_ptr->ext.txt.owner_xp ) );
    180182
    181183            // check TXT_RX ownership
    182             if ( XPTR( local_cxy , process ) != owner_xp )
     184            if ( process_owner_xp != txt_owner_xp )
    183185            {
     186                if( (iter & 0xFFF) == 0 )
     187                printk("\n[WARNING] in %s : thread %x in process %x wait TXT_RX / cycle %d\n",
     188                __FUNCTION__, this->trdid, process->pid, (uint32_t)hal_get_cycles() );
     189
    184190                // deschedule without blocking
    185                 sched_yield( "wait TXT ownership" );
     191                sched_yield( "wait TXT_RX ownership" );
     192
     193                iter++;
    186194            }
    187195            else
     
    191199        }
    192200
     201printk("\n###### in %s : thread %x in process %x got TXT_RX ownership\n",
     202__FUNCTION__, this->trdid, process->pid );
     203               
    193204        // move count bytes from device
    194205        nbytes = devfs_user_move( true,             // from device to buffer
  • trunk/kernel/syscalls/sys_wait.c

    r443 r446  
    4141    cxy_t       child_cxy;
    4242    pid_t       child_pid;
    43     int         child_state;
     43    uint32_t    child_state;
    4444    thread_t  * child_thread;
    4545    reg_t       save_sr;
     
    5050
    5151#if DEBUG_SYS_WAIT
    52 uint64_t    tm_start;
    53 uint64_t    tm_end;
    54 tm_start = hal_get_cycles();
    55 if( DEBUG_SYS_WAIT < tm_start )
    56 printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
    57 __FUNCTION__, this, process->pid, (uint32_t)tm_start );
     52uint64_t    cycle = hal_get_cycles();
     53if( DEBUG_SYS_WAIT < cycle )
     54printk("\n[DBG] %s : thread %x in process %x enter / cycle %d\n",
     55__FUNCTION__, this, process->pid, (uint32_t)cycle );
    5856#endif
    5957
     
    8381    {
    8482
    85 #if DEBUG_SYSCALL_ERROR
     83#if DEBUG_SYSCALLS_ERROR
    8684printk("\n[ERROR] in %s : calling thread %x is not thread 0 in owner cluster %x\n",
    8785__FUNCTION__ , trdid , owner_cxy );
     
    112110            child_cxy = GET_CXY( child_xp );
    113111
    114             // get term_state from child owner process
    115             child_state = (int)hal_remote_lw ( XPTR(child_cxy,&child_ptr->term_state));
     112            // get PID, term_state, and main thread from child process
     113            child_pid    = hal_remote_lw (XPTR( child_cxy , &child_ptr->pid ));
     114            child_state  = hal_remote_lw ( XPTR(child_cxy , &child_ptr->term_state ) );
     115            child_thread = hal_remote_lpt(XPTR( child_cxy , &child_ptr->th_tbl[0] ));
    116116
     117#if (DEBUG_SYS_WAIT &1)
     118cycle = hal_get_cycles();
     119if( DEBUG_SYS_WAIT < cycle )
     120printk("\n[DBG] %s : thread %x in process %x check child %x / state %x\n",
     121__FUNCTION__, this, process->pid, child_pid, child_state );
     122#endif
    117123            // test if this child process is terminated,
    118124            // but termination not yet reported to parent process
     
    122128                 ((child_state & PROCESS_TERM_WAIT) == 0) )
    123129            {
    124                 // get pointer on child main thread and PID from child owner process
    125                 child_pid    = (pid_t)     hal_remote_lw (XPTR( child_cxy , &child_ptr->pid ));
    126                 child_thread = (thread_t *)hal_remote_lpt(XPTR( child_cxy ,
    127                                                                 &child_ptr->th_tbl[0] ));
    128 
    129                 // set the PROCESS_FLAG_WAIT in owner child process descriptor
     130                // set the PROCESS_FLAG_WAIT in child process descriptor
    130131                hal_remote_atomic_or( XPTR( child_cxy , &child_ptr->term_state ),
    131132                                      PROCESS_TERM_WAIT );
    132133
    133                 // set the THREAD_FLAG_REQ_DELETE in child main thread
     134                // set the THREAD_FLAG_REQ_DELETE in main thread if kill or exit
     135                if((child_state & PROCESS_TERM_EXIT) || (child_state & PROCESS_TERM_KILL))
    134136                hal_remote_atomic_or( XPTR( child_cxy , &child_thread->flags ) ,
    135137                                            THREAD_FLAG_REQ_DELETE );
     
    139141
    140142#if DEBUG_SYS_WAIT
    141 tm_end = hal_get_cycles();
    142 if( DEBUG_SYS_WAIT < tm_end )
    143 printk("\n[DBG] %s : thread %x exit / parent %x / child %x / cycle %d\n",
    144 __FUNCTION__, this, process->pid, child_pid, (uint32_t)tm_end );
     143cycle = hal_get_cycles();
     144if( DEBUG_SYS_WAIT < cycle )
     145{
     146    if     ( child_state & PROCESS_TERM_EXIT )
     147        printk("\n[DBG] %s : thread %x in process %x exit / child %x exit / cycle %d\n",
     148        __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle );
     149    if( child_state & PROCESS_TERM_KILL )
     150        printk("\n[DBG] %s : thread %x in process %x exit / child %x killed / cycle %d\n",
     151        __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle );
     152    if( child_state & PROCESS_TERM_STOP )
     153        printk("\n[DBG] %s : thread %x in process %x exit / child %x stopped / cycle %d\n",
     154        __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle );
     155}
    145156#endif
    146157                 // return child termination state  to parent process
     
    150161        }  // end loop on children
    151162       
     163        // block on WAIT condition
     164        thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_WAIT );
     165
    152166        // release lock protecting children list
    153167        remote_spinlock_unlock( children_lock_xp );
    154168
    155         // deschedule without blocking
    156         sched_yield( "parent wait children termination" );
     169#if (DEBUG_SYS_WAIT & 1)
     170cycle = hal_get_cycles();
     171if( DEBUG_SYS_WAIT < cycle )
     172printk("\n[DBG] %s : thread %x in process %x block & deschedule / cycle %d\n",
     173__FUNCTION__, this, process->pid, (uint32_t)cycle );
     174#endif
     175
     176        // deschedule
     177        sched_yield( "parent process wait children processes termination" );
     178
     179#if (DEBUG_SYS_WAIT & 1)
     180cycle = hal_get_cycles();
     181if( DEBUG_SYS_WAIT < cycle )
     182printk("\n[DBG] %s : thread %x in process %x unblock & resume / cycle %d\n",
     183__FUNCTION__, this, process->pid, (uint32_t)cycle );
     184#endif
    157185
    158186    }  // end while
Note: See TracChangeset for help on using the changeset viewer.