Index: trunk/kernel/kern/thread.c
===================================================================
--- trunk/kernel/kern/thread.c	(revision 662)
+++ trunk/kernel/kern/thread.c	(revision 669)
@@ -30,4 +30,5 @@
 #include <hal_remote.h>
 #include <hal_vmm.h>
+#include <hal_switch.h>
 #include <memcpy.h>
 #include <printk.h>
@@ -120,6 +121,6 @@
 
 // check type and trdid fields are initialized
-assert( (thread->type == type)   , "bad type argument" );
-assert( (thread->trdid == trdid) , "bad trdid argument" );
+assert( __FUNCTION__, (thread->type == type)   , "bad type argument" );
+assert( __FUNCTION__, (thread->trdid == trdid) , "bad trdid argument" );
 
 #if DEBUG_THREAD_INIT
@@ -160,4 +161,7 @@
     list_entry_init( &thread->sched_list );
 
+    // initialize the embedded alarm to unlink 
+    list_entry_init( &thread->alarm.list );
+
     // initialize waiting queue entries
     list_entry_init( &thread->wait_list );
@@ -220,5 +224,5 @@
     vseg_t       * us_vseg;      // user stack vseg
 
-assert( (attr != NULL) , "pthread attributes must be defined" );
+assert( __FUNCTION__, (attr != NULL) , "pthread attributes must be defined" );
 
 #if DEBUG_THREAD_USER_CREATE
@@ -370,5 +374,6 @@
         return -1;
     }
-    hal_cpu_context_init( thread );
+    hal_cpu_context_init( thread,
+                          false , 0 , 0 );   // not a main thread
 
     // allocate & initialize FPU context
@@ -662,8 +667,7 @@
 }  // end thread_user_fork()
 
-////////////////////////////////////////////////
-error_t thread_user_exec( void     * entry_func,
-                          uint32_t   argc,
-                          char    ** argv )
+/////////////////////////////////////
+void thread_user_exec( uint32_t argc,
+                       intptr_t argv )
 {
     thread_t  * thread  = CURRENT_THREAD;
@@ -673,31 +677,23 @@
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_THREAD_USER_EXEC < cycle )
-printk("\n[%s] thread[%x,%x] enter / entry %x / cycle %d\n",
-__FUNCTION__, process->pid, thread->trdid, entry_func , cycle );
+printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
+__FUNCTION__, process->pid, thread->trdid, cycle );
 #endif
 
 // check parent thread attributes
-assert( (thread->type == THREAD_USER )          , "bad type" );
-assert( (thread->signature == THREAD_SIGNATURE) , "bad signature" );
-assert( (thread->busylocks == 0)                , "bad busylocks" );
+assert( __FUNCTION__, (thread->type      == THREAD_USER )     , "bad type" );
+assert( __FUNCTION__, (thread->signature == THREAD_SIGNATURE) , "bad signature" );
+assert( __FUNCTION__, (thread->busylocks == 0)                , "bad busylocks" );
 
 	// re-initialize various thread descriptor fields
-    thread->quantum         = 0;            // TODO
-    thread->ticks_nr        = 0;            // TODO
-    thread->time_last_check = 0;            // TODO
-
-    thread->entry_func      = entry_func;
-    thread->main_argc       = argc; 
-    thread->main_argv       = argv;
-
-    // the main thread is always detached
-    thread->flags           = THREAD_FLAG_DETACHED;
+    thread->quantum         = 0;                               // TODO
+    thread->ticks_nr        = 0;                               // TODO
+    thread->time_last_check = 0;                               // TODO
+    thread->entry_func      = (void*)process->vmm.entry_point;
+    thread->flags           = THREAD_FLAG_DETACHED;            // main always detached
     thread->blocked         = 0;
     thread->errno           = 0;
-    thread->fork_user       = 0;    // not inherited
-    thread->fork_cxy        = 0;    // not inherited
-
-    // re-initialize busylocks counters
-    thread->busylocks       = 0;
+    thread->fork_user       = 0;
+    thread->fork_cxy        = 0;
 
     // reset thread info
@@ -707,42 +703,30 @@
     remote_busylock_init( XPTR( local_cxy , &thread->join_lock ), LOCK_THREAD_JOIN );
 
-    // allocate an user stack vseg for main thread
-    vseg_t * us_vseg = vmm_create_vseg( process,
-                                        VSEG_TYPE_STACK,
-                                        LTID_FROM_TRDID( thread->trdid ),
-                                        0,                 // length unused
-                                        0,                 // file_offset unused
-                                        0,                 // file_size unused
-                                        XPTR_NULL,         // mapper_xp unused
-                                        local_cxy );
-    if( us_vseg == NULL )
-    {
-	    printk("\n[ERROR] in %s : cannot create stack vseg for main thread\n", __FUNCTION__ );
-		return -1;
-    }
-
-    // update user stack in thread descriptor
-    thread->user_stack_vseg = us_vseg;
-    
     // release FPU ownership if required
     if( thread->core->fpu_owner == thread ) thread->core->fpu_owner = NULL;
 
-    // re-initialize  FPU context
+    // initialize thread FPU context
     hal_fpu_context_init( thread );
+
+    // initialize thread CPU context 
+    hal_cpu_context_init( thread,
+                          true,          // main thread
+                          argc,
+                          argv );  
 
 #if DEBUG_THREAD_USER_EXEC
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_THREAD_USER_EXEC < cycle )
-printk("\n[%s] thread[%x,%x] set CPU context & jump to user code / cycle %d\n",
-__FUNCTION__, process->pid, thread->trdid, cycle );
-hal_vmm_display( XPTR( local_cxy , process ) , true );
-#endif
-
-    // re-initialize CPU context... and jump to user code
-	hal_cpu_context_exec( thread );
-
-    assert( false, "we should not execute this code");
- 
-    return 0;
+{
+    printk("\n[%s] thread[%x,%x] set CPU context & jump to user code / cycle %d\n",
+    __FUNCTION__, process->pid, thread->trdid, cycle );
+
+    hal_cpu_context_display( XPTR( local_cxy , thread ) );
+    hal_vmm_display( XPTR( local_cxy , process ) , true );
+}
+#endif
+
+    // restore CPU registers ... and jump to user code
+    hal_do_cpu_restore( thread->cpu_context );
 
 }  // end thread_user_exec()
@@ -761,8 +745,8 @@
     thread_t * this = CURRENT_THREAD; 
 
-assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
+assert( __FUNCTION__, ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
 "illegal thread type" );
 
-assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
+assert( __FUNCTION__, (core_lid < LOCAL_CLUSTER->cores_nr) ,
 "illegal core_lid" );
 
@@ -829,5 +813,6 @@
     }
 
-    hal_cpu_context_init( thread );
+    hal_cpu_context_init( thread,
+                          false , 0 , 0 );  // not a main thread
 
 #if DEBUG_THREAD_KERNEL_CREATE
@@ -854,6 +839,6 @@
 
 // check arguments
-assert( (type == THREAD_IDLE) , "illegal thread type" );
-assert( (core_lid < LOCAL_CLUSTER->cores_nr) , "illegal core index" );
+assert( __FUNCTION__, (type == THREAD_IDLE) , "illegal thread type" );
+assert( __FUNCTION__, (core_lid < LOCAL_CLUSTER->cores_nr) , "illegal core index" );
 
     // set type in thread descriptor
@@ -863,5 +848,5 @@
     error = process_register_thread( &process_zero , thread , &trdid );
 
-assert( (error == 0), "cannot register idle_thread in kernel process" );
+assert( __FUNCTION__, (error == 0), "cannot register idle_thread in kernel process" );
 
     // set trdid in thread descriptor
@@ -878,12 +863,14 @@
                          NULL );   // no user stack for a kernel thread
 
-assert( (error == 0), "cannot initialize idle_thread" );
-
-    // allocate & initialize CPU context if success
+assert( __FUNCTION__, (error == 0), "cannot initialize idle_thread" );
+
+    // allocate CPU context 
     error = hal_cpu_context_alloc( thread );
 
-assert( (error == 0), "cannot allocate CPU context" );
-
-    hal_cpu_context_init( thread );
+assert( __FUNCTION__, (error == 0), "cannot allocate CPU context" );
+
+    // initialize CPU context
+    hal_cpu_context_init( thread,
+                          false , 0 , 0 );   // not a main thread
 
 }  // end thread_idle_init()
@@ -949,4 +936,8 @@
 #endif
 
+    // unlink embedded alarm from the list rooted in core when required
+    list_entry_t * entry = &thread->alarm.list;
+    if( (entry->next != NULL) || (entry->pred != NULL) )  list_unlink( entry );
+
     // remove thread from process th_tbl[]
     count = process_remove_thread( thread );
@@ -1028,5 +1019,5 @@
 
     // check signal pending in scheduler
-    assert( sched->req_ack_pending , "no pending signal" );
+    assert( __FUNCTION__, sched->req_ack_pending , "no pending signal" );
     
     // enter critical section
@@ -1101,5 +1092,5 @@
 
 //////////////////////////////////////
-void thread_delete( xptr_t  target_xp,
+void thread_delete_request( xptr_t  target_xp,
                     bool_t  is_forced )
 {
@@ -1150,5 +1141,5 @@
 // check target thread is not the main thread, because the main thread
 // must be deleted by the parent process sys_wait() function
-assert( ((CXY_FROM_PID( target_pid ) != target_cxy) || (target_ltid != 0)),
+assert( __FUNCTION__, ((CXY_FROM_PID( target_pid ) != target_cxy) || (target_ltid != 0)),
 "target thread cannot be the main thread" );
 
@@ -1275,5 +1266,5 @@
 
     }
-}  // end thread_delete()
+}  // end thread_delete_request()
 
 
