Ignore:
Timestamp:
Nov 19, 2020, 11:44:34 PM (4 years ago)
Author:
alain
Message:

1) Introduce up to 4 command lines arguments in the KSH "load" command.
These arguments are transfered to the user process through the
argc/argv mechanism, using the user space "args" vseg.

2) Introduce the named and anonymous "pipes", for inter-process communication
through the pipe() and mkfifo() syscalls.

3) Introduce the "chat" application to validate the two above mechanisms.

4) Improve printk() and assert() fonctions in printk.c.

File:
1 edited

Legend:

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

    r662 r669  
    3030#include <hal_remote.h>
    3131#include <hal_vmm.h>
     32#include <hal_switch.h>
    3233#include <memcpy.h>
    3334#include <printk.h>
     
    120121
    121122// check type and trdid fields are initialized
    122 assert( (thread->type == type)   , "bad type argument" );
    123 assert( (thread->trdid == trdid) , "bad trdid argument" );
     123assert( __FUNCTION__, (thread->type == type)   , "bad type argument" );
     124assert( __FUNCTION__, (thread->trdid == trdid) , "bad trdid argument" );
    124125
    125126#if DEBUG_THREAD_INIT
     
    160161    list_entry_init( &thread->sched_list );
    161162
     163    // initialize the embedded alarm to unlink
     164    list_entry_init( &thread->alarm.list );
     165
    162166    // initialize waiting queue entries
    163167    list_entry_init( &thread->wait_list );
     
    220224    vseg_t       * us_vseg;      // user stack vseg
    221225
    222 assert( (attr != NULL) , "pthread attributes must be defined" );
     226assert( __FUNCTION__, (attr != NULL) , "pthread attributes must be defined" );
    223227
    224228#if DEBUG_THREAD_USER_CREATE
     
    370374        return -1;
    371375    }
    372     hal_cpu_context_init( thread );
     376    hal_cpu_context_init( thread,
     377                          false , 0 , 0 );   // not a main thread
    373378
    374379    // allocate & initialize FPU context
     
    662667}  // end thread_user_fork()
    663668
    664 ////////////////////////////////////////////////
    665 error_t thread_user_exec( void     * entry_func,
    666                           uint32_t   argc,
    667                           char    ** argv )
     669/////////////////////////////////////
     670void thread_user_exec( uint32_t argc,
     671                       intptr_t argv )
    668672{
    669673    thread_t  * thread  = CURRENT_THREAD;
     
    673677uint32_t cycle = (uint32_t)hal_get_cycles();
    674678if( DEBUG_THREAD_USER_EXEC < cycle )
    675 printk("\n[%s] thread[%x,%x] enter / entry %x / cycle %d\n",
    676 __FUNCTION__, process->pid, thread->trdid, entry_func , cycle );
     679printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
     680__FUNCTION__, process->pid, thread->trdid, cycle );
    677681#endif
    678682
    679683// check parent thread attributes
    680 assert( (thread->type == THREAD_USER )          , "bad type" );
    681 assert( (thread->signature == THREAD_SIGNATURE) , "bad signature" );
    682 assert( (thread->busylocks == 0)                , "bad busylocks" );
     684assert( __FUNCTION__, (thread->type      == THREAD_USER )     , "bad type" );
     685assert( __FUNCTION__, (thread->signature == THREAD_SIGNATURE) , "bad signature" );
     686assert( __FUNCTION__, (thread->busylocks == 0)                , "bad busylocks" );
    683687
    684688        // re-initialize various thread descriptor fields
    685     thread->quantum         = 0;            // TODO
    686     thread->ticks_nr        = 0;            // TODO
    687     thread->time_last_check = 0;            // TODO
    688 
    689     thread->entry_func      = entry_func;
    690     thread->main_argc       = argc;
    691     thread->main_argv       = argv;
    692 
    693     // the main thread is always detached
    694     thread->flags           = THREAD_FLAG_DETACHED;
     689    thread->quantum         = 0;                               // TODO
     690    thread->ticks_nr        = 0;                               // TODO
     691    thread->time_last_check = 0;                               // TODO
     692    thread->entry_func      = (void*)process->vmm.entry_point;
     693    thread->flags           = THREAD_FLAG_DETACHED;            // main always detached
    695694    thread->blocked         = 0;
    696695    thread->errno           = 0;
    697     thread->fork_user       = 0;    // not inherited
    698     thread->fork_cxy        = 0;    // not inherited
    699 
    700     // re-initialize busylocks counters
    701     thread->busylocks       = 0;
     696    thread->fork_user       = 0;
     697    thread->fork_cxy        = 0;
    702698
    703699    // reset thread info
     
    707703    remote_busylock_init( XPTR( local_cxy , &thread->join_lock ), LOCK_THREAD_JOIN );
    708704
    709     // allocate an user stack vseg for main thread
    710     vseg_t * us_vseg = vmm_create_vseg( process,
    711                                         VSEG_TYPE_STACK,
    712                                         LTID_FROM_TRDID( thread->trdid ),
    713                                         0,                 // length unused
    714                                         0,                 // file_offset unused
    715                                         0,                 // file_size unused
    716                                         XPTR_NULL,         // mapper_xp unused
    717                                         local_cxy );
    718     if( us_vseg == NULL )
    719     {
    720             printk("\n[ERROR] in %s : cannot create stack vseg for main thread\n", __FUNCTION__ );
    721                 return -1;
    722     }
    723 
    724     // update user stack in thread descriptor
    725     thread->user_stack_vseg = us_vseg;
    726    
    727705    // release FPU ownership if required
    728706    if( thread->core->fpu_owner == thread ) thread->core->fpu_owner = NULL;
    729707
    730     // re-initialize FPU context
     708    // initialize thread FPU context
    731709    hal_fpu_context_init( thread );
     710
     711    // initialize thread CPU context
     712    hal_cpu_context_init( thread,
     713                          true,          // main thread
     714                          argc,
     715                          argv ); 
    732716
    733717#if DEBUG_THREAD_USER_EXEC
    734718cycle = (uint32_t)hal_get_cycles();
    735719if( DEBUG_THREAD_USER_EXEC < cycle )
    736 printk("\n[%s] thread[%x,%x] set CPU context & jump to user code / cycle %d\n",
    737 __FUNCTION__, process->pid, thread->trdid, cycle );
    738 hal_vmm_display( XPTR( local_cxy , process ) , true );
    739 #endif
    740 
    741     // re-initialize CPU context... and jump to user code
    742         hal_cpu_context_exec( thread );
    743 
    744     assert( false, "we should not execute this code");
    745  
    746     return 0;
     720{
     721    printk("\n[%s] thread[%x,%x] set CPU context & jump to user code / cycle %d\n",
     722    __FUNCTION__, process->pid, thread->trdid, cycle );
     723
     724    hal_cpu_context_display( XPTR( local_cxy , thread ) );
     725    hal_vmm_display( XPTR( local_cxy , process ) , true );
     726}
     727#endif
     728
     729    // restore CPU registers ... and jump to user code
     730    hal_do_cpu_restore( thread->cpu_context );
    747731
    748732}  // end thread_user_exec()
     
    761745    thread_t * this = CURRENT_THREAD;
    762746
    763 assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
     747assert( __FUNCTION__, ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
    764748"illegal thread type" );
    765749
    766 assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
     750assert( __FUNCTION__, (core_lid < LOCAL_CLUSTER->cores_nr) ,
    767751"illegal core_lid" );
    768752
     
    829813    }
    830814
    831     hal_cpu_context_init( thread );
     815    hal_cpu_context_init( thread,
     816                          false , 0 , 0 );  // not a main thread
    832817
    833818#if DEBUG_THREAD_KERNEL_CREATE
     
    854839
    855840// check arguments
    856 assert( (type == THREAD_IDLE) , "illegal thread type" );
    857 assert( (core_lid < LOCAL_CLUSTER->cores_nr) , "illegal core index" );
     841assert( __FUNCTION__, (type == THREAD_IDLE) , "illegal thread type" );
     842assert( __FUNCTION__, (core_lid < LOCAL_CLUSTER->cores_nr) , "illegal core index" );
    858843
    859844    // set type in thread descriptor
     
    863848    error = process_register_thread( &process_zero , thread , &trdid );
    864849
    865 assert( (error == 0), "cannot register idle_thread in kernel process" );
     850assert( __FUNCTION__, (error == 0), "cannot register idle_thread in kernel process" );
    866851
    867852    // set trdid in thread descriptor
     
    878863                         NULL );   // no user stack for a kernel thread
    879864
    880 assert( (error == 0), "cannot initialize idle_thread" );
    881 
    882     // allocate & initialize CPU context if success
     865assert( __FUNCTION__, (error == 0), "cannot initialize idle_thread" );
     866
     867    // allocate CPU context
    883868    error = hal_cpu_context_alloc( thread );
    884869
    885 assert( (error == 0), "cannot allocate CPU context" );
    886 
    887     hal_cpu_context_init( thread );
     870assert( __FUNCTION__, (error == 0), "cannot allocate CPU context" );
     871
     872    // initialize CPU context
     873    hal_cpu_context_init( thread,
     874                          false , 0 , 0 );   // not a main thread
    888875
    889876}  // end thread_idle_init()
     
    949936#endif
    950937
     938    // unlink embedded alarm from the list rooted in core when required
     939    list_entry_t * entry = &thread->alarm.list;
     940    if( (entry->next != NULL) || (entry->pred != NULL) )  list_unlink( entry );
     941
    951942    // remove thread from process th_tbl[]
    952943    count = process_remove_thread( thread );
     
    10281019
    10291020    // check signal pending in scheduler
    1030     assert( sched->req_ack_pending , "no pending signal" );
     1021    assert( __FUNCTION__, sched->req_ack_pending , "no pending signal" );
    10311022   
    10321023    // enter critical section
     
    11011092
    11021093//////////////////////////////////////
    1103 void thread_delete( xptr_t  target_xp,
     1094void thread_delete_request( xptr_t  target_xp,
    11041095                    bool_t  is_forced )
    11051096{
     
    11501141// check target thread is not the main thread, because the main thread
    11511142// must be deleted by the parent process sys_wait() function
    1152 assert( ((CXY_FROM_PID( target_pid ) != target_cxy) || (target_ltid != 0)),
     1143assert( __FUNCTION__, ((CXY_FROM_PID( target_pid ) != target_cxy) || (target_ltid != 0)),
    11531144"target thread cannot be the main thread" );
    11541145
     
    12751266
    12761267    }
    1277 }  // end thread_delete()
     1268}  // end thread_delete_request()
    12781269
    12791270
Note: See TracChangeset for help on using the changeset viewer.