Ignore:
Timestamp:
Aug 2, 2018, 11:47:13 AM (6 years ago)
Author:
alain
Message:

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File:
1 edited

Legend:

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

    r450 r457  
    2424
    2525#include <kernel_config.h>
    26 #include <hal_types.h>
     26#include <hal_kernel_types.h>
    2727#include <hal_context.h>
    2828#include <hal_irqmask.h>
     
    167167    thread->quantum         = 0;            // TODO
    168168    thread->ticks_nr        = 0;            // TODO
    169     thread->time_last_check = 0;
     169    thread->time_last_check = 0;            // TODO
    170170        thread->core            = &local_cluster->core_tbl[core_lid];
    171171        thread->process         = process;
     
    243243uint32_t cycle = (uint32_t)hal_get_cycles();
    244244if( DEBUG_THREAD_USER_CREATE < cycle )
    245 printk("\n[DBG] %s : thread %x enter for process %x in cluster %x / cycle %d\n",
    246 __FUNCTION__, CURRENT_THREAD, pid , local_cxy , cycle );
     245printk("\n[DBG] %s : thread %x in process %x enter in cluster %x / cycle %d\n",
     246__FUNCTION__, CURRENT_THREAD->trdid, pid , local_cxy , cycle );
    247247#endif
    248248
     
    301301    }
    302302
     303#if( DEBUG_THREAD_USER_CREATE & 1)
     304if( DEBUG_THREAD_USER_CREATE < cycle )
     305printk("\n[DBG] %s : stack vseg created / vpn_base %x / %d pages\n",
     306__FUNCTION__, vseg->vpn_base, vseg->vpn_size );
     307#endif
     308
    303309    // allocate memory for thread descriptor
    304310    thread = thread_alloc();
     
    313319#if( DEBUG_THREAD_USER_CREATE & 1)
    314320if( DEBUG_THREAD_USER_CREATE < cycle )
    315 printk("\n[DBG] %s : thread descriptor %x allocated\n",
     321printk("\n[DBG] %s : new thread descriptor %x allocated\n",
    316322__FUNCTION__, thread );
    317323#endif
     
    336342#if( DEBUG_THREAD_USER_CREATE & 1)
    337343if( DEBUG_THREAD_USER_CREATE < cycle )
    338 printk("\n[DBG] %s : thread descriptor %x initialised / trdid = %x\n",
    339 __FUNCTION__, thread , thread->trdid );
     344printk("\n[DBG] %s : new thread descriptor initialised / trdid %x\n",
     345__FUNCTION__, thread->trdid );
    340346#endif
    341347
     
    347353
    348354    // allocate & initialize CPU context
    349         if( hal_cpu_context_create( thread ) )
     355        if( hal_cpu_context_alloc( thread ) )
    350356    {
    351357            printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ );
     
    354360        return ENOMEM;
    355361    }
    356 
    357     // allocate  FPU context
     362    hal_cpu_context_init( thread );
     363
     364    // allocate & initialize FPU context
    358365    if( hal_fpu_context_alloc( thread ) )
    359366    {
     
    363370        return ENOMEM;
    364371    }
     372    hal_fpu_context_init( thread );
     373
     374#if( DEBUG_THREAD_USER_CREATE & 1)
     375if( DEBUG_THREAD_USER_CREATE < cycle )
     376printk("\n[DBG] %s : CPU & FPU contexts created\n",
     377__FUNCTION__, thread->trdid );
     378vmm_display( process , true );
     379#endif
    365380
    366381#if DEBUG_THREAD_USER_CREATE
    367382cycle = (uint32_t)hal_get_cycles();
    368383if( DEBUG_THREAD_USER_CREATE < cycle )
    369 printk("\n[DBG] %s : thread %x exit / new_thread %x in process %x / core %d / cycle %d\n",
    370 __FUNCTION__, CURRENT_THREAD, thread->trdid , pid , core_lid, cycle );
     384printk("\n[DBG] %s : thread %x in process %x exit / new_thread %x / core %d / cycle %d\n",
     385__FUNCTION__, CURRENT_THREAD->trdid , pid, thread->trdid, core_lid, cycle );
    371386#endif
    372387
     
    554569}  // end thread_user_fork()
    555570
     571////////////////////////////////////////////////
     572error_t thread_user_exec( void     * entry_func,
     573                          uint32_t   argc,
     574                          char    ** argv )
     575{
     576    thread_t  * thread  = CURRENT_THREAD;
     577    process_t * process = thread->process;
     578
     579#if DEBUG_THREAD_USER_EXEC
     580uint32_t cycle = (uint32_t)hal_get_cycles();
     581if( DEBUG_THREAD_USER_EXEC < cycle )
     582printk("\n[DBG] %s : thread %x in process %x enter / cycle %d\n",
     583__FUNCTION__, thread->trdid, process->pid, cycle );
     584#endif
     585
     586        assert( (thread->type == THREAD_USER )          , __FUNCTION__, "bad type" );
     587        assert( (thread->signature == THREAD_SIGNATURE) , __FUNCTION__, "bad signature" );
     588        assert( (thread->local_locks == 0)              , __FUNCTION__, "bad local locks" );
     589        assert( (thread->remote_locks == 0)             , __FUNCTION__, "bad remote locks" );
     590
     591        // re-initialize various thread descriptor fields
     592    thread->quantum         = 0;            // TODO
     593    thread->ticks_nr        = 0;            // TODO
     594    thread->time_last_check = 0;            // TODO
     595
     596#if CONFIG_LOCKS_DEBUG
     597    list_root_init( &thread->locks_root ); 
     598    xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
     599#endif
     600
     601    thread->entry_func      = entry_func;
     602    thread->main_argc       = argc;
     603    thread->main_argv       = argv;
     604
     605    // the main thread is always detached
     606    thread->flags           = THREAD_FLAG_DETACHED;
     607    thread->blocked         = 0;
     608    thread->errno           = 0;
     609    thread->fork_user       = 0;    // not inherited
     610    thread->fork_cxy        = 0;    // not inherited
     611
     612    // reset thread info
     613    memset( &thread->info , 0 , sizeof(thread_info_t) );
     614
     615    // initialize join_lock
     616    remote_spinlock_init( XPTR( local_cxy , &thread->join_lock ) );
     617
     618    // allocate an user stack vseg for main thread
     619    vseg_t * vseg = vmm_create_vseg( process,
     620                                     VSEG_TYPE_STACK,
     621                                     0,                 // size unused
     622                                     0,                 // length unused
     623                                     0,                 // file_offset unused
     624                                     0,                 // file_size unused
     625                                     XPTR_NULL,         // mapper_xp unused
     626                                     local_cxy );
     627    if( vseg == NULL )
     628    {
     629            printk("\n[ERROR] in %s : cannot create stack vseg for main thread\n", __FUNCTION__ );
     630                return -1;
     631    }
     632
     633    // update user stack in stack descriptor
     634    thread->u_stack_base = vseg->min;
     635    thread->u_stack_size = vseg->max - vseg->min;
     636   
     637    // release FPU ownership if required
     638    if( thread->core->fpu_owner == thread ) thread->core->fpu_owner = NULL;
     639
     640    // re-initialize  FPU context
     641    hal_fpu_context_init( thread );
     642
     643#if DEBUG_THREAD_USER_EXEC
     644cycle = (uint32_t)hal_get_cycles();
     645if( DEBUG_THREAD_USER_EXEC < cycle )
     646printk("\n[DBG] %s : thread %x in process %x set CPU context & jump to user code / cycle %d\n",
     647__FUNCTION__, thread->trdid, process->pid, cycle );
     648vmm_display( process , true );
     649#endif
     650
     651    // re-initialize CPU context... and jump to user code
     652        hal_cpu_context_exec( thread );
     653
     654    assert( false, __FUNCTION__, "we should execute this code");
     655 
     656    return 0;
     657
     658}  // end thread_user_exec()
     659
    556660/////////////////////////////////////////////////////////
    557661error_t thread_kernel_create( thread_t     ** new_thread,
     
    594698    {
    595699        thread_release( thread );
     700        return ENOMEM;
     701    }
     702
     703    // allocate & initialize CPU context
     704        error = hal_cpu_context_alloc( thread );
     705    if( error )
     706    {
     707        thread_release( thread );
    596708        return EINVAL;
    597709    }
    598 
    599     // allocate & initialize CPU context
    600         hal_cpu_context_create( thread );
     710    hal_cpu_context_init( thread );
     711
    601712
    602713#if DEBUG_THREAD_KERNEL_CREATE
     
    612723} // end thread_kernel_create()
    613724
    614 /////////////////////////////////////////////////
    615 error_t thread_idle_init( thread_t      * thread,
    616                           thread_type_t   type,
    617                           void          * func,
    618                           void          * args,
    619                                           lid_t           core_lid )
     725//////////////////////////////////////////////
     726void thread_idle_init( thread_t      * thread,
     727                       thread_type_t   type,
     728                       void          * func,
     729                       void          * args,
     730                           lid_t           core_lid )
    620731{
    621732    assert( (type == THREAD_IDLE) , __FUNCTION__ , "illegal thread type" );
    622 
    623733    assert( (core_lid < LOCAL_CLUSTER->cores_nr) , __FUNCTION__ , "illegal core index" );
    624734
     735    // initialize thread descriptor
    625736    error_t  error = thread_init( thread,
    626737                                  &process_zero,
     
    631742                                  0 , 0 );   // no user stack for a kernel thread
    632743
     744    assert( (error == 0), __FUNCTION__, "cannot create thread idle" );
     745
    633746    // allocate & initialize CPU context if success
    634     if( error == 0 ) hal_cpu_context_create( thread );
    635 
    636     return error;
     747    error = hal_cpu_context_alloc( thread );
     748
     749    assert( (error == 0), __FUNCTION__, "cannot allocate CPU context" );
     750
     751    hal_cpu_context_init( thread );
    637752
    638753}  // end thread_idle_init()
     
    798913
    799914#if DEBUG_THREAD_BLOCK
    800 uint32_t cycle = (uint32_t)hal_get_cycles();
     915uint32_t    cycle   = (uint32_t)hal_get_cycles();
     916process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) );
    801917if( DEBUG_THREAD_BLOCK < cycle )
    802 printk("\n[DBG] %s : thread %x  in cxy %x blocked thread %x in cxy %x / cause %x / cycle %d\n",
    803 __FUNCTION__ , CURRENT_THREAD , local_cxy , ptr , cxy , cause , cycle );
     918printk("\n[DBG] %s : thread %x in process %x blocked thread %x in process %x / cause %x\n",
     919__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid,
     920ptr->trdid, hal_remote_lw(XPTR( cxy , &process->pid )), cause );
    804921#endif
    805922
     
    819936
    820937#if DEBUG_THREAD_BLOCK
    821 uint32_t cycle = (uint32_t)hal_get_cycles();
     938uint32_t    cycle   = (uint32_t)hal_get_cycles();
     939process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) );
    822940if( DEBUG_THREAD_BLOCK < cycle )
    823 printk("\n[DBG] %s : thread %x  in cxy %x unblocked thread %x in cxy %x / cause %x / cycle %d\n",
    824 __FUNCTION__ , CURRENT_THREAD , local_cxy , ptr , cxy , cause , cycle );
     941printk("\n[DBG] %s : thread %x in process %x unblocked thread %x in process %x / cause %x\n",
     942__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid,
     943ptr->trdid, hal_remote_lw(XPTR( cxy , &process->pid )), cause );
    825944#endif
    826945
Note: See TracChangeset for help on using the changeset viewer.