Index: trunk/hal/tsar_mips32/core/hal_context.c
===================================================================
--- trunk/hal/tsar_mips32/core/hal_context.c	(revision 406)
+++ trunk/hal/tsar_mips32/core/hal_context.c	(revision 407)
@@ -44,6 +44,10 @@
 
 /////////////////////////////////////////////////////////////////////////////////////////
-// This structuree defines the cpu_context for TSAR MIPS32.
-// These registers are saved/restored at each context switch.
+// This structure defines the CPU context for TSAR MIPS32.
+// The following registers are saved/restored at each context switch:
+// - GPR : all, but (zero, k0, k1), plus (hi, lo)
+// - CP0 : c0_th , c0_sr , C0_epc
+// - CP2 : c2_ptpr , C2_mode
+//
 // WARNING : check the two CONFIG_CPU_CTX_SIZE & CONFIG_FPU_CTX_SIZE configuration 
 //           parameterss when modifying this structure.
@@ -85,5 +89,5 @@
     uint32_t gp_28;      // slot 28
 	uint32_t sp_29;      // slot 29
-	uint32_t fp_30;      // slot 30
+	uint32_t s8_30;      // slot 30
 	uint32_t ra_31;      // slot 31
 
@@ -108,69 +112,69 @@
 
 /////////////////////////////////////////////////////////////////////////////////////////
-//        CPU context access functions
-/////////////////////////////////////////////////////////////////////////////////////////
-
-/////////////////////////////////////////////////////////////////////////////////////////
-// This function allocates and initializes the cpu_context stucture in thread descriptor.
-// The following context slots are initialised by this function:
-// GPR : a0_04 / sp_29 / fp_30 / ra_31
+//        CPU context related functions
+/////////////////////////////////////////////////////////////////////////////////////////
+
+
+//////////////////////////////////////////////////
+error_t hal_cpu_context_alloc( thread_t * thread )
+{
+    assert( (sizeof(hal_cpu_context_t) <= CONFIG_CPU_CTX_SIZE) , __FUNCTION__ ,
+    "illegal CPU context size" );
+
+    // allocate memory for cpu_context
+    kmem_req_t  req;
+    req.type   = KMEM_CPU_CTX;
+    req.flags  = AF_KERNEL | AF_ZERO;
+
+    hal_cpu_context_t * context = (hal_cpu_context_t *)kmem_alloc( &req );
+    if( context == NULL ) return -1;
+
+    // link to thread
+    thread->cpu_context = (void *)context;
+    return 0;
+
+}   // end hal_cpu_context_alloc()
+
+///////////////////////////////////////////////////
+// The following context slots are initialised :
+// GPR : a0_04 / sp_29 / ra_31
 // CP0 : c0_sr / c0_th / c0_epc
 // CP2 : c2_ptpr / c2_mode
-/////////////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////
 error_t hal_cpu_context_create( thread_t * thread )
 {
-    kmem_req_t  req;
-
-    assert( (sizeof(hal_cpu_context_t) <= CONFIG_CPU_CTX_SIZE) , __FUNCTION__ ,
-    "inconsistent CPU context size" );
-
-    context_dmsg("\n[DMSG] %s : enters for thread %x in process %x\n",
-                 __FUNCTION__ , thread->trdid , thread->process->pid );
-
-    // allocate memory for cpu_context
-    req.type   = KMEM_CPU_CTX;
-    req.flags  = AF_KERNEL | AF_ZERO;
-
-    hal_cpu_context_t * context = (hal_cpu_context_t *)kmem_alloc( &req );
-    if( context == NULL ) return ENOMEM;
-
-    // set cpu context pointer in thread
-    thread->cpu_context = (void*)context;
-
-    // stack pointer, status register and mmu_mode depends on thread type
-	uint32_t sp_29;
-    uint32_t c0_sr;
-    uint32_t c2_mode;
+    // allocate memory for a CPU context
+    error_t error = hal_cpu_context_alloc( thread );
+
+    if( error ) return error;
+
+    hal_cpu_context_t * context = (hal_cpu_context_t *)thread->cpu_context;
+
+    // initialisation depends on thread type
     if( thread->type == THREAD_USER )
     {
-        sp_29   = ((uint32_t)thread->u_stack_base) + thread->u_stack_size;
-        c0_sr   = SR_USR_MODE;
-        c2_mode = 0xF;
+        context->a0_04   = (uint32_t)thread->entry_args;
+        context->sp_29   = (uint32_t)thread->u_stack_base + (uint32_t)thread->u_stack_size - 8;
+        context->ra_31   = (uint32_t)&hal_kentry_eret;
+        context->c0_epc  = (uint32_t)thread->entry_func;
+        context->c0_sr   = SR_USR_MODE;
+	    context->c0_th   = (uint32_t)thread; 
+	    context->c2_ptpr = (uint32_t)((thread->process->vmm.gpt.ppn) >> 1);
+        context->c2_mode = 0xF;
     }
-    else
+    else  // kernel thread
     {
-        sp_29   = ((uint32_t)thread->k_stack_base) + thread->k_stack_size;
-        c0_sr   = SR_SYS_MODE;
-        c2_mode = 0x3;
+        context->a0_04   = (uint32_t)thread->entry_args;
+        context->sp_29   = (uint32_t)thread->k_stack_base + (uint32_t)thread->k_stack_size - 8;
+        context->ra_31   = (uint32_t)thread->entry_func;
+        context->c0_sr   = SR_SYS_MODE;
+	    context->c0_th   = (uint32_t)thread; 
+	    context->c2_ptpr = (uint32_t)((thread->process->vmm.gpt.ppn) >> 1);
+        context->c2_mode = 0x3;
     }
 
-    // align stack pointer on a double word boundary 
-	sp_29 = (sp_29 - 8) & (~ 0x7);
-
-    // initialise context
-    context->a0_04      = (uint32_t)thread->entry_args;
-	context->sp_29      = sp_29; 
-	context->fp_30      = sp_29;                               // TODO check this [AG]
-    context->ra_31      = (uint32_t)&hal_kentry_eret;
-    context->c0_epc     = (uint32_t)thread->entry_func;
-	context->c0_sr      = c0_sr;
-	context->c0_th      = (uint32_t)thread; 
-	context->c2_ptpr    = (uint32_t)((thread->process->vmm.gpt.ppn) >> 1);
-	context->c2_mode    = c2_mode;
-
-    context_dmsg("\n[DMSG] %s : exit for thread %x in process %x\n"
+context_dmsg("\n[DBG] %s : thread %x in process %x\n"
                  " - a0   = %x\n"
                  " - sp   = %x\n"
-                 " - fp   = %x\n"
                  " - ra   = %x\n"
                  " - sr   = %x\n"
@@ -180,5 +184,5 @@
                  " - mode = %x\n",  
                  __FUNCTION__ , thread->trdid , thread->process->pid,
-                 context->a0_04, context->sp_29, context->fp_30, context->ra_31,
+                 context->a0_04, context->sp_29, context->ra_31,
                  context->c0_sr, context->c0_th, context->c0_epc,
                  context->c2_ptpr, context->c2_mode );
@@ -201,58 +205,5 @@
            ctx->c2_ptpr , ctx->c2_mode );
 
-}  // end hal_context_display()
-
-/////////////////////////////////////////////////////////////////////////////////////////
-// These registers are saved/restored to/from CPU context defined by <ctx> argument.
-// - GPR : all, but (zero, k0, k1), plus (hi, lo)
-// - CP0 : c0_th , c0_sr , C0_epc
-// - CP2 : c2_ptpr , C2_mode
-/////////////////////////////////////////////////////////////////////////////////////////
-// old_thread  : pointer on current thread descriptor
-// new_thread  : pointer on new thread descriptor
-/////////////////////////////////////////////////////////////////////////////////////////
-void hal_cpu_context_switch( thread_t * old_thread,
-                             thread_t * new_thread )
-{
-    hal_cpu_context_t * ctx_old = old_thread->cpu_context;
-    hal_cpu_context_t * ctx_new = new_thread->cpu_context;
-
-    #if CONFIG_CONTEXT_DEBUG
-    hal_cpu_context_display( old_thread );
-    hal_cpu_context_display( new_thread );
-    #endif
-
-    // reset loadable field in new thread descriptor 
-    new_thread->flags &= ~THREAD_FLAG_LOADABLE;
-
-    hal_do_switch( ctx_old , ctx_new );
-}
-
-/////////////////////////////////////////////
-error_t hal_cpu_context_copy( thread_t * dst,
-                              thread_t * src )
-{
-    kmem_req_t  req;
-
-    // allocate memory for dst cpu_context
-    req.type   = KMEM_CPU_CTX;
-    req.size   = sizeof(hal_cpu_context_t);
-    req.flags  = AF_KERNEL | AF_ZERO;
-
-    hal_cpu_context_t * dst_context = (hal_cpu_context_t *)kmem_alloc( &req );
-    if( dst_context == NULL ) return ENOMEM;
-
-    // set cpu context pointer in dst thread
-    dst->cpu_context = dst_context;
-
-    // get cpu context pointer from src thread
-    hal_cpu_context_t * src_context = src->cpu_context;
-
-    // copy CPU context from src to dst
-    memcpy( dst_context , src_context , sizeof(hal_cpu_context_t) );
-
-    return 0;
-
-}  // end hal_cpu_context_copy()
+}  // end hal_cpu_context_display()
 
 /////////////////////////////////////////////////
@@ -268,48 +219,41 @@
 
 
-///////////////////////////////////////////////////
-error_t hal_fpu_context_create( thread_t * thread )
-{
+
+
+
+//////////////////////////////////////////////////
+error_t hal_fpu_context_alloc( thread_t * thread )
+{
+    assert( (sizeof(hal_fpu_context_t) <= CONFIG_FPU_CTX_SIZE) , __FUNCTION__ ,
+    "illegal CPU context size" );
+
+    // allocate memory for fpu_context
     kmem_req_t  req;
-
-    assert( (sizeof(hal_fpu_context_t) <= CONFIG_FPU_CTX_SIZE) , __FUNCTION__ ,
-    "inconsistent FPU context size" );
-
-    // allocate memory for uzone
     req.type   = KMEM_FPU_CTX;
     req.flags  = AF_KERNEL | AF_ZERO;
 
     hal_fpu_context_t * context = (hal_fpu_context_t *)kmem_alloc( &req );
-    if( context == NULL ) return ENOMEM;
-
-    // set fpu context pointer in thread
-    thread->fpu_context = (void*)context;
-    
+    if( context == NULL ) return -1;
+
+    // link to thread
+    thread->fpu_context = (void *)context;
     return 0;
-}  // hal_fpu_context_create()
-
-/////////////////////////////////////////////
-error_t hal_fpu_context_copy( thread_t * dst,
-                              thread_t * src )
-{
-    kmem_req_t  req;
-
-    // allocate memory for dst fpu_context
-    req.type   = KMEM_FPU_CTX;
-    req.flags  = AF_KERNEL | AF_ZERO;
-
-    hal_fpu_context_t * dst_context = (hal_fpu_context_t *)kmem_alloc( &req );
-    if( dst_context == NULL ) return ENOMEM;
-
-    // set fpu context pointer in dst thread
-    dst->fpu_context = (void*)dst_context;
-
-    // get fpu context pointer from src thread
+
+}   // end hal_fpu_context_alloc()
+
+//////////////////////////////////////////
+void hal_fpu_context_copy( thread_t * dst,
+                           thread_t * src )
+{
+    assert( (src != NULL) , __FUNCTION__ , "src thread pointer is NULL\n"); 
+    assert( (dst != NULL) , __FUNCTION__ , "dst thread pointer is NULL\n"); 
+
+    // get fpu context pointers
     hal_fpu_context_t * src_context = src->fpu_context;
+    hal_fpu_context_t * dst_context = dst->fpu_context;
 
     // copy CPU context from src to dst
     memcpy( dst_context , src_context , sizeof(hal_fpu_context_t) );
 
-    return 0;
 }  // end hal_fpu_context_copy()
 
@@ -413,9 +357,3 @@
 } // end hal_cpu_context_restore()
 
-/////////////////////////////////////
-void hal_fpu_context_dup( xptr_t dst,
-                          xptr_t src )
-{
-	hal_remote_memcpy( dst , src , sizeof(hal_fpu_context_t) );
-}
-
+
