Ignore:
Timestamp:
Jun 29, 2017, 4:44:52 PM (7 years ago)
Author:
alain
Message:

euh...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/kern/process.c

    r23 r101  
    8181}
    8282
    83 ////////////////////////////////////////////
    84 void process_zero_init( boot_info_t * info )
    85 {
    86     // reset process descriptor
    87         memset( &process_zero , 0 , sizeof(process_t) );
    88 
    89     // initialize kernel code & data vsegs base addresses
    90         process_zero.vmm.code_vpn_base = (vpn_t)(info->kernel_code_start >> CONFIG_PPM_PAGE_SHIFT);
    91         process_zero.vmm.data_vpn_base = (vpn_t)(info->kernel_data_start >> CONFIG_PPM_PAGE_SHIFT);
    92 
    93     // reset threads and childs number
    94         process_zero.th_nr       = 0;
    95         process_zero.children_nr = 0;
    96 
    97     // set PID
    98         process_zero.pid            = 0;
    99 }
    100 
    10183/////////////////////////////////////////////////
    10284void process_reference_init( process_t * process,
    10385                             pid_t       pid,
    104                              pid_t       ppid )
    105 {
    106     // reset reference process vmm
    107     vmm_init( process );
     86                             xptr_t      parent_xp )
     87{
     88    cxy_t       parent_cxy;
     89    process_t * parent_ptr;
     90    pid_t       parent_pid;
     91
     92    process_dmsg("\n[INFO] %s : enters for process %x in cluster %x / parent_xp = %l\n",
     93                 __FUNCTION__ , pid , parent_xp );
     94
     95    // get parent process cluster, local pointer, and pid
     96    // for all processes other than process_zero
     97    if( process == &process_zero )
     98    {
     99        assert( (pid == 0) , __FUNCTION__ , "process_zero must have PID = 0\n");
     100
     101        parent_pid = 0;  // process_zero is its own parent...
     102    }
     103    else
     104    {
     105        assert( (parent_xp != XPTR_NULL) , __FUNCTION__ , "parent_xp cannot be NULL\n");
     106
     107        parent_cxy = GET_CXY( parent_xp );
     108        parent_ptr = (process_t *)GET_PTR( parent_xp );
     109        parent_pid = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) );
     110    }
     111
     112    // reset reference process vmm (not for kernel process)
     113    if( pid ) vmm_init( process );
    108114
    109115    // reset reference process file descriptors array
    110116        process_fd_init( process );
    111117
    112     // reset reference process files structures and cd_lock
     118    // reset reference process files structures and cwd_lock
    113119        process->vfs_root_xp     = XPTR_NULL;
    114120        process->vfs_bin_xp      = XPTR_NULL;
     
    127133    remote_spinlock_init( XPTR( local_cxy , &process->sync_lock ) );
    128134
    129     // register new process in the parent children list
    130     xptr_t entry = XPTR( local_cxy , &process->brothers_list );
    131     xptr_t root  = XPTR( local_cxy , &process->children_root );
    132     xlist_add_first( root , entry );
     135    // register new process in the parent children list (not for kernel process)
     136    if( pid )
     137    {
     138        xptr_t entry = XPTR( local_cxy  , &process->brothers_list );
     139        xptr_t root  = XPTR( parent_cxy , &parent_ptr->children_root );
     140        xlist_add_first( root , entry );
     141    }
    133142   
    134143    // reset th_tbl[] array as empty
     
    143152    // initialize PID and PPID
    144153        process->pid   = pid;
    145     process->ppid  = ppid;
     154    process->ppid  = parent_pid;
    146155
    147156    // set ref_xp field
     
    157166
    158167        hal_wbflush();
     168
     169    process_dmsg("\n[INFO] %s : exit for process %x in cluster %x\n",
     170                 __FUNCTION__ , pid );
    159171
    160172} // end process_reference_init()
     
    576588
    577589    return (found) ? 0 : ENOMEM;
    578 }
     590
     591}  // process_register_thread()
    579592   
    580593///////////////////////////////////////////////
     
    595608    process->th_tbl[ltid] = NULL;
    596609    process->th_nr--;
    597 }
     610
     611}  // process_remove_thread()
     612
    598613
    599614/////////////////////////////////////////////////////
     
    601616{
    602617    char           * path;                            // pathname to .elf file
    603     process_t      * process;                         // pointer on new process
     618    process_t      * process;                         // local pointer on new process
    604619    pid_t            pid;                             // new process pid
     620    xptr_t           parent_xp;                       // extended pointer on parent process
     621    cxy_t            parent_cxy;
     622    process_t      * parent_ptr;
     623    uint32_t         parent_pid;
    605624    thread_t       * thread;                          // pointer on new thread
    606625    pthread_attr_t   attr;                            // main thread attributes
     
    609628        error_t          error;
    610629
    611         // get pid and pathname to .elf file 
    612         path  = exec_info->path;
    613     pid   = exec_info->pid;
     630        // get parent and .elf pathname from exec_info
     631        path      = exec_info->path;
     632    parent_xp = exec_info->parent_xp;
     633
     634    // get parent process cluster and local pointer
     635    parent_cxy = GET_CXY( parent_xp );
     636    parent_ptr = (process_t *)GET_PTR( parent_xp );
     637    parent_pid = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) );
    614638   
    615     assert( (CXY_FROM_PID( pid ) == local_cxy) , __FUNCTION__ , "illegal PID\n" );
    616 
    617     exec_dmsg("\n[INFO] %s enters in cluster %x for process %x / path = %s\n",
    618                 __FUNCTION__ , local_cxy , pid , path );
     639    exec_dmsg("\n[INFO] %s enters in cluster %x for path = %s\n",
     640                __FUNCTION__ , local_cxy , path );
    619641
    620642    // create new process descriptor
     
    623645    if( process == NULL )
    624646    {
    625         printk("\n[ERROR] in %s : no memory in cluster %x for process %x / path = %s\n",
    626                __FUNCTION__ , local_cxy , pid , path );
     647        printk("\n[ERROR] in %s : no memory / cluster = %x / ppid = %x / path = %s\n",
     648               __FUNCTION__ , local_cxy , parent_pid , path );
    627649        return ENOMEM;
    628650    }
    629651
     652    // get a pid from the local cluster
     653    error = cluster_pid_alloc( XPTR( local_cxy , process ) , &pid );
     654
     655    if( error )
     656    {
     657        printk("\n[ERROR] in %s : cannot get PID / cluster = %x / ppid = %x / path = %s\n",
     658               __FUNCTION__ , local_cxy , parent_pid , path );
     659                return ENOMEM;
     660    }
     661 
    630662    // initialize the process descriptor as the reference
    631     process_reference_init( process , pid , exec_info->ppid );
     663    process_reference_init( process , pid , parent_xp );
    632664                               
    633     // restore from exec_info the extended pointer on vfs root, cwd, and bin
    634     process->vfs_root_xp = exec_info->vfs_root_xp;
    635     process->vfs_cwd_xp  = exec_info->vfs_cwd_xp;
    636     process->vfs_bin_xp  = exec_info->vfs_bin_xp;
    637 
    638     // restore from exec_info the embedded fd_array
    639     process_fd_remote_copy( XPTR( local_cxy , &process->fd_array ), exec_info->fd_array_xp );
    640                            
    641     exec_dmsg("\n[INFO] %s restaured fd-array in cluster %x for process %x / path = %s\n",
    642                 __FUNCTION__, local_cxy , pid , path );
     665    exec_dmsg("\n[INFO] %s created process %x cluster %x / path = %s\n",
     666                __FUNCTION__, parent_pid , local_cxy , path );
     667
     668    // initializes vfs_root and vfs_cwd from parent process
     669    xptr_t  vfs_root_xp = hal_remote_lwd( XPTR( parent_cxy , &parent_ptr->vfs_root_xp ) );
     670        vfs_file_count_up( vfs_root_xp );
     671        process->vfs_root_xp = vfs_root_xp;
     672
     673    xptr_t  vfs_cwd_xp = hal_remote_lwd( XPTR( parent_cxy , &parent_ptr->vfs_cwd_xp ) );
     674        vfs_file_count_up( vfs_cwd_xp );
     675        process->vfs_cwd_xp = vfs_cwd_xp;
     676
     677    // initialize embedded fd_array from parent process
     678    process_fd_remote_copy( XPTR( local_cxy , &process->fd_array ),
     679                            XPTR( parent_cxy , &parent_ptr->fd_array) );
    643680
    644681        // initialize signal manager TODO ??? [AG]
     
    663700        }
    664701
    665     // create "heap" vseg descriptor
     702    // register "heap" vseg descriptor in VMM
    666703    vseg_t * heap_vseg = vmm_create_vseg( process,
    667704                                          CONFIG_VMM_HEAP_BASE,
     
    699736        }
    700737
     738    // update children list in parent process
     739        xlist_add_last( XPTR( parent_cxy , &parent_ptr->children_root ),
     740                    XPTR( local_cxy  , &process->brothers_list ) );
     741        hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr) , 1 );
     742
    701743        // Register thread in scheduler
    702744        sched_register_thread( core , thread );
    703745
    704746        exec_dmsg("\n[INFO] %s created thread for process %x on core %d in cluster %x\n",
    705                __FUNCTION__ , process->pid , core->lid , local_cxy );
     747               __FUNCTION__ , pid , core->lid , local_cxy );
    706748
    707749    // activate new thread
     
    715757void process_init_create()
    716758{
    717         process_t   * process_init;  // process_init descriptor
    718     pid_t         init_pid;      // process_init pid
    719759    exec_info_t   exec_info;     // structure to be passed to process_make_exec()
    720760
    721         error_t error1 = 0;
    722         error_t error2 = 0;
    723         error_t error3 = 0;
    724 
    725         process_dmsg("\n[INFO] %s enters in cluster %x\n", __FUNCTION__ , local_cxy );
    726 
    727     // allocate memory for process descriptor
    728     process_init = process_alloc();
    729     if( process_init == NULL )
    730     {
    731         printk("\n[PANIC] in %s : no memory for process descriptor in cluster %x\n",
    732                __FUNCTION__ , local_cxy );
    733         hal_core_sleep();
    734     }
    735 
    736     // get a pid from the local cluster
    737     xptr_t xp_process = XPTR( local_cxy , process_init );
    738     error1 = cluster_pid_alloc( xp_process , &init_pid );
    739     if( error1 )
    740     {
    741         printk("\n[PANIC] in %s : cannot get PID in cluster %x\n",
    742                __FUNCTION__ , local_cxy );
    743                 hal_core_sleep();
    744     }
    745  
    746     // initializes process_init descriptor as the reference
    747         process_reference_init( process_init , init_pid , process_zero.pid );
    748 
    749     // initialize process_init VMM
    750         vmm_init( process_init );
    751 
    752     // initializes vfs_root and vfs_cwd from process_zero
    753         vfs_file_count_up( process_zero.vfs_root_xp );
    754         process_init->vfs_root_xp = process_zero.vfs_root_xp;
    755 
    756         vfs_file_count_up( process_zero.vfs_cwd_xp );
    757         process_init->vfs_cwd_xp  = process_zero.vfs_cwd_xp;
    758 
    759     // update children list in process_zero
    760         xlist_add_last( XPTR( local_cxy , &process_zero.children_root ),
    761                     XPTR( local_cxy , &process_init->brothers_list ) );
    762         process_zero.children_nr = 1;
    763 
    764     // TODO create inodes for stdin / stdout / stderr pseudo-files
    765     // these inodes should be created in the cluster containing the relevant TXT chdev
    766 
    767     // open stdin / stdout / stderr pseudo-files
     761        error_t   error1;
     762        error_t   error2;
     763        error_t   error3;
    768764    xptr_t    stdin_xp;
    769765    xptr_t    stdout_xp;
     
    773769    uint32_t  stderr_id;
    774770
     771        process_dmsg("\n[INFO] %s enters in cluster %x\n", __FUNCTION__ , local_cxy );
     772
     773    // open stdin / stdout / stderr pseudo-files
    775774        error1 = vfs_open( XPTR_NULL, CONFIG_DEV_STDIN , O_RDONLY, 0, &stdin_xp , &stdin_id  );
    776775        error2 = vfs_open( XPTR_NULL, CONFIG_DEV_STDOUT, O_WRONLY, 0, &stdout_xp, &stdout_id );
     
    782781                if( !error2 ) vfs_close( stdout_xp , stdout_id );
    783782                if( !error3 ) vfs_close( stderr_xp , stderr_id );
    784    
    785783                printk("\n[PANIC] in %s : cannot open stdin/stdout/stderr in cluster %x\n",
    786784               __FUNCTION__ , local_cxy );
     
    791789    if( (stdin_id != 0) || (stdout_id != 1) || (stderr_id != 2) )
    792790    {
     791                vfs_close( stdin_xp  , stdin_id );
     792                vfs_close( stdout_xp , stdout_id );
     793                vfs_close( stderr_xp , stderr_id );
    793794                printk("\n[PANIC] in %s : bad indexes for stdin/stdout/stderr in cluster %x\n",
    794795            __FUNCTION__ , local_cxy );
     
    797798
    798799    // initialize the exec_info structure
    799     exec_info.pid          = process_init->pid;
    800     exec_info.ppid         = process_init->ppid;
    801     exec_info.fd_array_xp  = XPTR( local_cxy , &process_init->fd_array );
    802     exec_info.vfs_root_xp  = process_init->vfs_root_xp;
    803     exec_info.vfs_cwd_xp   = process_init->vfs_cwd_xp;
    804     exec_info.vfs_bin_xp   = process_init->vfs_bin_xp;
    805 
    806     // TODO thread_init ???
    807     // exec_info.args_nr      = 1;
    808     // exec_info.envs_nr      = 1;
    809     // strcpy( exec_info.args[0] , "init" );
    810     // strcpy( exec_info.envs[0] , "ALMOS-MKH.CONFIG = "CONFIG_ALMOS_VERSION );
    811     // strcpy( exec_info.path    , INIT_PATHNAME );
     800    exec_info.parent_xp    = XPTR( local_cxy , &process_zero );
     801    strcpy( exec_info.path , CONFIG_PROCESS_INIT_PATH );
     802    exec_info.args_nr      = 0;
     803    exec_info.envs_nr      = 0;
    812804
    813805    // create process_init and thread_init
    814806        error1 = process_make_exec( &exec_info );
     807
    815808        if( error1 )
    816809        {
Note: See TracChangeset for help on using the changeset viewer.