Index: trunk/kernel/kern/thread.c
===================================================================
--- trunk/kernel/kern/thread.c	(revision 450)
+++ trunk/kernel/kern/thread.c	(revision 457)
@@ -24,5 +24,5 @@
 
 #include <kernel_config.h>
-#include <hal_types.h>
+#include <hal_kernel_types.h>
 #include <hal_context.h>
 #include <hal_irqmask.h>
@@ -167,5 +167,5 @@
     thread->quantum         = 0;            // TODO
     thread->ticks_nr        = 0;            // TODO
-    thread->time_last_check = 0;
+    thread->time_last_check = 0;            // TODO
 	thread->core            = &local_cluster->core_tbl[core_lid];
 	thread->process         = process;
@@ -243,6 +243,6 @@
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_THREAD_USER_CREATE < cycle )
-printk("\n[DBG] %s : thread %x enter for process %x in cluster %x / cycle %d\n",
-__FUNCTION__, CURRENT_THREAD, pid , local_cxy , cycle );
+printk("\n[DBG] %s : thread %x in process %x enter in cluster %x / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD->trdid, pid , local_cxy , cycle );
 #endif
 
@@ -301,4 +301,10 @@
     }
 
+#if( DEBUG_THREAD_USER_CREATE & 1)
+if( DEBUG_THREAD_USER_CREATE < cycle )
+printk("\n[DBG] %s : stack vseg created / vpn_base %x / %d pages\n",
+__FUNCTION__, vseg->vpn_base, vseg->vpn_size );
+#endif
+
     // allocate memory for thread descriptor
     thread = thread_alloc();
@@ -313,5 +319,5 @@
 #if( DEBUG_THREAD_USER_CREATE & 1)
 if( DEBUG_THREAD_USER_CREATE < cycle )
-printk("\n[DBG] %s : thread descriptor %x allocated\n",
+printk("\n[DBG] %s : new thread descriptor %x allocated\n",
 __FUNCTION__, thread );
 #endif
@@ -336,6 +342,6 @@
 #if( DEBUG_THREAD_USER_CREATE & 1)
 if( DEBUG_THREAD_USER_CREATE < cycle )
-printk("\n[DBG] %s : thread descriptor %x initialised / trdid = %x\n",
-__FUNCTION__, thread , thread->trdid );
+printk("\n[DBG] %s : new thread descriptor initialised / trdid %x\n",
+__FUNCTION__, thread->trdid );
 #endif
 
@@ -347,5 +353,5 @@
 
     // allocate & initialize CPU context
-	if( hal_cpu_context_create( thread ) )
+	if( hal_cpu_context_alloc( thread ) )
     {
 	    printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ );
@@ -354,6 +360,7 @@
         return ENOMEM;
     }
-
-    // allocate  FPU context
+    hal_cpu_context_init( thread );
+
+    // allocate & initialize FPU context
     if( hal_fpu_context_alloc( thread ) )
     {
@@ -363,10 +370,18 @@
         return ENOMEM;
     }
+    hal_fpu_context_init( thread );
+
+#if( DEBUG_THREAD_USER_CREATE & 1)
+if( DEBUG_THREAD_USER_CREATE < cycle )
+printk("\n[DBG] %s : CPU & FPU contexts created\n",
+__FUNCTION__, thread->trdid );
+vmm_display( process , true );
+#endif
 
 #if DEBUG_THREAD_USER_CREATE
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_THREAD_USER_CREATE < cycle )
-printk("\n[DBG] %s : thread %x exit / new_thread %x in process %x / core %d / cycle %d\n",
-__FUNCTION__, CURRENT_THREAD, thread->trdid , pid , core_lid, cycle );
+printk("\n[DBG] %s : thread %x in process %x exit / new_thread %x / core %d / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD->trdid , pid, thread->trdid, core_lid, cycle );
 #endif
 
@@ -554,4 +569,93 @@
 }  // end thread_user_fork()
 
+////////////////////////////////////////////////
+error_t thread_user_exec( void     * entry_func,
+                          uint32_t   argc,
+                          char    ** argv )
+{
+    thread_t  * thread  = CURRENT_THREAD;
+    process_t * process = thread->process;
+
+#if DEBUG_THREAD_USER_EXEC
+uint32_t cycle = (uint32_t)hal_get_cycles();
+if( DEBUG_THREAD_USER_EXEC < cycle )
+printk("\n[DBG] %s : thread %x in process %x enter / cycle %d\n",
+__FUNCTION__, thread->trdid, process->pid, cycle );
+#endif
+
+	assert( (thread->type == THREAD_USER )          , __FUNCTION__, "bad type" );
+	assert( (thread->signature == THREAD_SIGNATURE) , __FUNCTION__, "bad signature" );
+	assert( (thread->local_locks == 0)              , __FUNCTION__, "bad local locks" );
+	assert( (thread->remote_locks == 0)             , __FUNCTION__, "bad remote locks" );
+
+	// re-initialize various thread descriptor fields
+    thread->quantum         = 0;            // TODO
+    thread->ticks_nr        = 0;            // TODO
+    thread->time_last_check = 0;            // TODO
+
+#if CONFIG_LOCKS_DEBUG 
+    list_root_init( &thread->locks_root );  
+    xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
+#endif
+
+    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->blocked         = 0;
+    thread->errno           = 0;
+    thread->fork_user       = 0;    // not inherited
+    thread->fork_cxy        = 0;    // not inherited
+
+    // reset thread info
+    memset( &thread->info , 0 , sizeof(thread_info_t) );
+
+    // initialize join_lock
+    remote_spinlock_init( XPTR( local_cxy , &thread->join_lock ) );
+
+    // allocate an user stack vseg for main thread
+    vseg_t * vseg = vmm_create_vseg( process,
+                                     VSEG_TYPE_STACK,
+                                     0,                 // size unused
+                                     0,                 // length unused
+                                     0,                 // file_offset unused
+                                     0,                 // file_size unused
+                                     XPTR_NULL,         // mapper_xp unused
+                                     local_cxy );
+    if( vseg == NULL )
+    {
+	    printk("\n[ERROR] in %s : cannot create stack vseg for main thread\n", __FUNCTION__ );
+		return -1;
+    }
+
+    // update user stack in stack descriptor
+    thread->u_stack_base = vseg->min;
+    thread->u_stack_size = vseg->max - vseg->min;
+    
+    // release FPU ownership if required
+    if( thread->core->fpu_owner == thread ) thread->core->fpu_owner = NULL;
+
+    // re-initialize  FPU context
+    hal_fpu_context_init( thread );
+
+#if DEBUG_THREAD_USER_EXEC
+cycle = (uint32_t)hal_get_cycles();
+if( DEBUG_THREAD_USER_EXEC < cycle )
+printk("\n[DBG] %s : thread %x in process %x set CPU context & jump to user code / cycle %d\n",
+__FUNCTION__, thread->trdid, process->pid, cycle );
+vmm_display( process , true );
+#endif
+
+    // re-initialize CPU context... and jump to user code
+	hal_cpu_context_exec( thread );
+
+    assert( false, __FUNCTION__, "we should execute this code");
+ 
+    return 0;
+
+}  // end thread_user_exec()
+
 /////////////////////////////////////////////////////////
 error_t thread_kernel_create( thread_t     ** new_thread,
@@ -594,9 +698,16 @@
     {
         thread_release( thread );
+        return ENOMEM;
+    }
+
+    // allocate & initialize CPU context
+	error = hal_cpu_context_alloc( thread );
+    if( error )
+    {
+        thread_release( thread );
         return EINVAL;
     }
-
-    // allocate & initialize CPU context
-	hal_cpu_context_create( thread );
+    hal_cpu_context_init( thread );
+
 
 #if DEBUG_THREAD_KERNEL_CREATE
@@ -612,15 +723,15 @@
 } // end thread_kernel_create()
 
-/////////////////////////////////////////////////
-error_t thread_idle_init( thread_t      * thread,
-                          thread_type_t   type,
-                          void          * func,
-                          void          * args,
-				          lid_t           core_lid )
+//////////////////////////////////////////////
+void thread_idle_init( thread_t      * thread,
+                       thread_type_t   type,
+                       void          * func,
+                       void          * args,
+	                   lid_t           core_lid )
 {
     assert( (type == THREAD_IDLE) , __FUNCTION__ , "illegal thread type" );
-
     assert( (core_lid < LOCAL_CLUSTER->cores_nr) , __FUNCTION__ , "illegal core index" );
 
+    // initialize thread descriptor
     error_t  error = thread_init( thread,
                                   &process_zero,
@@ -631,8 +742,12 @@
                                   0 , 0 );   // no user stack for a kernel thread
 
+    assert( (error == 0), __FUNCTION__, "cannot create thread idle" );
+
     // allocate & initialize CPU context if success
-    if( error == 0 ) hal_cpu_context_create( thread );
-
-    return error;
+    error = hal_cpu_context_alloc( thread );
+
+    assert( (error == 0), __FUNCTION__, "cannot allocate CPU context" );
+
+    hal_cpu_context_init( thread );
 
 }  // end thread_idle_init()
@@ -798,8 +913,10 @@
 
 #if DEBUG_THREAD_BLOCK
-uint32_t cycle = (uint32_t)hal_get_cycles();
+uint32_t    cycle   = (uint32_t)hal_get_cycles();
+process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) );
 if( DEBUG_THREAD_BLOCK < cycle )
-printk("\n[DBG] %s : thread %x  in cxy %x blocked thread %x in cxy %x / cause %x / cycle %d\n",
-__FUNCTION__ , CURRENT_THREAD , local_cxy , ptr , cxy , cause , cycle );
+printk("\n[DBG] %s : thread %x in process %x blocked thread %x in process %x / cause %x\n",
+__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid,
+ptr->trdid, hal_remote_lw(XPTR( cxy , &process->pid )), cause );
 #endif
 
@@ -819,8 +936,10 @@
 
 #if DEBUG_THREAD_BLOCK
-uint32_t cycle = (uint32_t)hal_get_cycles();
+uint32_t    cycle   = (uint32_t)hal_get_cycles();
+process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) );
 if( DEBUG_THREAD_BLOCK < cycle )
-printk("\n[DBG] %s : thread %x  in cxy %x unblocked thread %x in cxy %x / cause %x / cycle %d\n",
-__FUNCTION__ , CURRENT_THREAD , local_cxy , ptr , cxy , cause , cycle );
+printk("\n[DBG] %s : thread %x in process %x unblocked thread %x in process %x / cause %x\n",
+__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid,
+ptr->trdid, hal_remote_lw(XPTR( cxy , &process->pid )), cause );
 #endif
 
