Index: trunk/hal/tsar_mips32/core/hal_exception.c
===================================================================
--- trunk/hal/tsar_mips32/core/hal_exception.c	(revision 367)
+++ trunk/hal/tsar_mips32/core/hal_exception.c	(revision 380)
@@ -27,4 +27,5 @@
 #include <thread.h>
 #include <printk.h>
+#include <chdev.h>
 #include <vmm.h>
 #include <errno.h>
@@ -37,9 +38,10 @@
 #include <mips32_uzone.h>
 
+
 //////////////////////////////////////////////////////////////////////////////////////////
 //  Extern global variables
 //////////////////////////////////////////////////////////////////////////////////////////
 
-extern remote_spinlock_t  exception_lock;  // allocated in the do_exception.c file.
+extern   chdev_directory_t    chdev_dir;  // allocated in the kernel_init.c file.
 
 //////////////////////////////////////////////////////////////////////////////////////////
@@ -59,5 +61,290 @@
 xcode_values_t;
 
-///////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////
+// This static function is called when a FPU Coprocessor Unavailable exception has been 
+// detected for the calling thread. 
+// It enables the FPU, It saves the current FPU context in the current owner thread 
+// descriptor if required, and restore the FPU context from the calling thread descriptor.
+//////////////////////////////////////////////////////////////////////////////////////////
+// @ this     : pointer on faulty thread descriptor.
+// @ return always EXCP_NON_FATAL
+//////////////////////////////////////////////////////////////////////////////////////////
+static error_t hal_fpu_exception( thread_t * this )
+{
+	core_t   * core = this->core; 
+
+    // enable FPU  
+	hal_fpu_enable();
+
+    // save FPU context in current owner thread if required 
+	if( core->fpu_owner != NULL )
+    {
+        if( core->fpu_owner != this )
+	    {
+		    hal_fpu_context_save ( core->fpu_owner->fpu_context );
+        }
+	}
+
+    // attach the FPU to the requesting thread
+	hal_fpu_context_restore( this->fpu_context );
+	core->fpu_owner = this;
+
+	return EXCP_NON_FATAL;
+
+}  // end hal_fpu_exception()
+
+//////////////////////////////////////////////////////////////////////////////////////////
+// This static function is called when an MMU exception has been detected. 
+// It get the relevant exception arguments from the MMU.
+// It signal a fatal error in case of illegal access. In case of page unmapped
+// it checks that the faulty address belongs to a registered vseg. It update the local
+// vseg list from the reference cluster if required, and signal a fatal user error
+// in case of illegal virtual address. Finally, it updates the local page table from the
+// reference cluster.
+//////////////////////////////////////////////////////////////////////////////////////////
+// @ this     : pointer on faulty thread descriptor.
+// @ return EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC
+//////////////////////////////////////////////////////////////////////////////////////////
+static error_t hal_mmu_exception( thread_t * this ) 
+{
+	vseg_t         * vseg;        // vseg containing the bad_vaddr
+	process_t      * process;     // local process descriptor
+    error_t          error;       // return value
+
+    reg_t            mmu_ins_excp_code;
+    reg_t            mmu_ins_bad_vaddr;
+    reg_t            mmu_dat_excp_code;
+    reg_t            mmu_dat_bad_vaddr;
+
+    intptr_t         bad_vaddr;
+    uint32_t         excp_code;
+	
+    process     = this->process;
+
+    // get relevant values from MMU
+	hal_get_mmu_excp( &mmu_ins_excp_code,
+	                  &mmu_ins_bad_vaddr,
+	                  &mmu_dat_excp_code, 
+	                  &mmu_dat_bad_vaddr );
+
+    // get exception code and faulty vaddr
+    if( mmu_ins_excp_code )
+    {
+        excp_code = mmu_ins_excp_code;
+        bad_vaddr = mmu_ins_bad_vaddr;
+    }
+    else if( mmu_dat_excp_code )
+    {
+        excp_code = mmu_dat_excp_code;
+        bad_vaddr = mmu_dat_bad_vaddr;
+    }
+    else
+    {
+         return EXCP_NON_FATAL;
+    }
+
+	vmm_dmsg("\n[INFO] %s : enters for thread %x / process %x"
+             " / bad_vaddr = %x / excep_code = %x\n", 
+		     __FUNCTION__, this->trdid , process->pid , bad_vaddr , excp_code );
+
+    // a kernel thread should not rise an MMU exception
+	if( this->type != THREAD_USER )
+	{
+		printk("\n[PANIC] in %s : thread %x is a kernel thread / vaddr = %x\n",
+		       __FUNCTION__ , this->trdid , bad_vaddr );
+		return EXCP_KERNEL_PANIC;
+	}
+  
+    // enable IRQs 
+	hal_enable_irq( NULL );
+
+    // vaddr must be contained in a registered vseg
+    vseg = vmm_get_vseg( process , bad_vaddr );
+
+    if( vseg == NULL )   // vseg not found in local cluster
+	{
+        // get extended pointer on reference process
+        xptr_t ref_xp = process->ref_xp;
+
+        // get cluster and local pointer on reference process 
+        cxy_t       ref_cxy = GET_CXY( ref_xp );
+        process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
+
+        if( local_cxy != ref_cxy )   // reference process is remote
+        {
+            // get extended pointer on reference vseg
+            xptr_t vseg_xp;
+            rpc_vmm_get_ref_vseg_client( ref_cxy , ref_ptr , bad_vaddr , &vseg_xp );
+            
+        
+            if( vseg == NULL )          // vseg not found => illegal user vaddr 
+            {
+                printk("\n[ERROR] in %s for thread %x : illegal vaddr = %x\n",
+                       __FUNCTION__ , this->trdid , bad_vaddr );
+
+                hal_disable_irq( NULL );
+		        return EXCP_USER_ERROR;
+            }
+            else                        // vseg found => make a local copy
+            {
+                // allocate a vseg in local cluster
+                vseg = vseg_alloc();
+
+                if( vseg == NULL )
+                {
+		            printk("\n[PANIC] in %s : no memory for vseg / thread = %x\n",
+		                   __FUNCTION__ , this->trdid );
+                    hal_disable_irq( NULL );
+		            return EXCP_KERNEL_PANIC;
+                }
+
+                // initialise local vseg from reference
+                vseg_init_from_ref( vseg , ref_xp );
+
+                // register local vseg in local VMM
+                error = vseg_attach( &process->vmm , vseg );
+            }
+        }
+        else                   // reference is local => illegal user vaddr
+        {
+            printk("\n[ERROR] in %s for thread %x : illegal vaddr = %x\n",
+                   __FUNCTION__ , this->trdid , bad_vaddr );
+
+            hal_disable_irq( NULL );
+		    return EXCP_USER_ERROR;
+        }
+    }
+
+	vmm_dmsg("\n[INFO] %s : found vseg for thread %x / vseg_min = %x / vseg_max = %x\n", 
+			 __FUNCTION__ , this->trdid , vseg->min , vseg->max );
+
+    // analyse exception code
+    if( excp_code & MMU_EXCP_PAGE_UNMAPPED )
+    {
+        // try to map the unmapped PTE
+        error = vmm_handle_page_fault( process, 
+                                       vseg,
+                                       bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );  // vpn
+
+        if( error )
+        {
+            printk("\n[PANIC] in %s for thread %x : cannot map legal vaddr = %x\n",
+               __FUNCTION__ , this->trdid , bad_vaddr );
+
+            hal_disable_irq( NULL );
+		    return EXCP_KERNEL_PANIC;
+        }
+        else
+        {
+            vmm_dmsg("\n[INFO] %s : page fault handled for vaddr = %x in thread %x\n",
+		             __FUNCTION__ , bad_vaddr , this->trdid );
+  
+            // page fault successfully handled
+            hal_disable_irq( NULL );
+            return EXCP_NON_FATAL;
+        }
+    }
+    else if( excp_code & MMU_EXCP_USER_PRIVILEGE )
+    {
+        printk("\n[ERROR] in %s for thread %x : user access to kernel vseg at vaddr = %x\n",
+               __FUNCTION__ , this->trdid , bad_vaddr );
+
+        hal_disable_irq( NULL );
+        return EXCP_USER_ERROR;
+    }
+    else if( excp_code & MMU_EXCP_USER_EXEC )
+    {
+        printk("\n[ERROR] in %s for thread %x : access to non-exec vseg at vaddr = %x\n",
+               __FUNCTION__ , this->trdid , bad_vaddr );
+
+        hal_disable_irq( NULL );
+        return EXCP_USER_ERROR;
+    }
+    else if( excp_code & MMU_EXCP_USER_WRITE )
+    {
+        printk("\n[ERROR] in %s for thread %x : write to non-writable vseg at vaddr = %x\n",
+               __FUNCTION__ , this->trdid , bad_vaddr );
+
+        hal_disable_irq( NULL );
+        return EXCP_USER_ERROR;
+    }
+
+    else  // this is a kernel error => panic    
+    {
+        printk("\n[PANIC] in %s for thread %x : kernel exception = %x / vaddr = %x\n",
+               __FUNCTION__ , this->trdid , excp_code , bad_vaddr );
+
+        hal_disable_irq( NULL );
+        return EXCP_KERNEL_PANIC;
+    }
+  
+} // end hal_mmu_exception()
+
+//////////////////////////////////////////////////////////////////////////////////////////
+// This static function prints on the kernel terminal the saved context (core registers)
+// and the thread state of a faulty thread.
+//////////////////////////////////////////////////////////////////////////////////////////
+// @ this     : pointer on faulty thread descriptor.
+// @ regs_tbl : pointer on register array.
+// @ return always EXCP_NON_FATAL
+//////////////////////////////////////////////////////////////////////////////////////////
+static void hal_exception_dump( thread_t * this,
+                                reg_t    * regs_tbl )
+{
+    uint32_t  save_sr;
+
+    // get pointers on TXT0 chdev
+    xptr_t    txt0_xp  = chdev_dir.txt[0];
+    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
+    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
+
+    // get extended pointer on remote TXT0 chdev lock
+    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
+
+    // get TXT0 lock in busy waiting mode
+    remote_spinlock_lock_busy( lock_xp , &save_sr );
+
+    if( this->type == THREAD_USER )
+    nolock_printk("\n================= USER ERROR / cycle %d ====================\n",
+           hal_time_stamp() );
+    else
+    nolock_printk("\n================= KERNEL PANIC / cycle %d ==================\n",
+           hal_time_stamp() );
+
+	nolock_printk("  thread type = %s / trdid = %x / pid %x / core[%x,%d]\n"
+           "  local locks = %d / remote locks = %d / blocked_vector = %X\n\n",
+           thread_type_str(this->type), this->trdid, this->process->pid, local_cxy,
+           this->core->lid, this->local_locks, this->remote_locks, this->blocked );
+
+	nolock_printk("CR    %X  EPC   %X  SR    %X  SP     %X\n",
+		   regs_tbl[UZ_CR], regs_tbl[UZ_EPC], regs_tbl[UZ_SR], regs_tbl[UZ_SP]);
+
+    nolock_printk("at_1  %X  v0_2  %X  v1_3  %X  a0_4   %X  a1_5   %X\n",
+   	       regs_tbl[UZ_AT], regs_tbl[UZ_V0], regs_tbl[UZ_V1], regs_tbl[UZ_A0], regs_tbl[UZ_A1]);
+
+    nolock_printk("a2_6  %X  a3_7  %X  t0_8  %X  t1_9   %X  t2_10  %X\n",
+		   regs_tbl[UZ_A2],regs_tbl[UZ_A3],regs_tbl[UZ_T0],regs_tbl[UZ_T1],regs_tbl[UZ_T2]);
+  
+    nolock_printk("t3_11 %X  t4_12 %X  t5_13 %X  t6_14  %X  t7_15  %X\n",
+		   regs_tbl[UZ_T3],regs_tbl[UZ_T4],regs_tbl[UZ_T5],regs_tbl[UZ_T6],regs_tbl[UZ_T7]);
+
+    nolock_printk("t8_24 %X  t9_25 %X  gp_28 %X  c0_hi  %X  c0_lo  %X\n",
+		   regs_tbl[UZ_T8],regs_tbl[UZ_T9],regs_tbl[UZ_GP],regs_tbl[UZ_HI],regs_tbl[UZ_LO]);
+
+    nolock_printk("s0_16 %X  s1_17 %X  s2_18 %X  s3_19  %X  s4_20  %X\n",
+		   regs_tbl[UZ_S0],regs_tbl[UZ_S1],regs_tbl[UZ_S2],regs_tbl[UZ_S3],regs_tbl[UZ_S4]);
+  
+    nolock_printk("s5_21 %X  s6_22 %X  s7_23 %X  s8_30  %X  ra_31  %X\n",
+	       regs_tbl[UZ_S5],regs_tbl[UZ_S6],regs_tbl[UZ_S7],regs_tbl[UZ_S8],regs_tbl[UZ_RA]);
+
+    // release the lock
+    remote_spinlock_unlock_busy( lock_xp , save_sr );
+
+}  // end hal_exception_dump()
+
+
+///////////////////////////////////////////////////////////////////////////////
+// TODO replace the hal_core_sleep() by the generic panic() function.
+///////////////////////////////////////////////////////////////////////////////
 void hal_do_exception( thread_t * this, 
                        reg_t    * regs_tbl )
@@ -72,16 +359,15 @@
 	{
         case XCODE_DBE:     // can be non fatal
-	    case XCODE_IBE:     // call generic excepton handler for a MMU exception
-        {
-		    error = do_exception( this , true );
+	    case XCODE_IBE:     // can be non fatal
+        {
+		    error = hal_mmu_exception( this );
         }
 		break;
 
 	    case XCODE_CPU:    // can be non fatal
-                           // call generic excepton handler for a FPU exception
         {
             if( ((regs_tbl[UZ_CR] >> 28) & 0x3) == 1 )  // unavailable FPU
             {
-                error = do_exception( this , false );
+                error = hal_fpu_exception( this );
             }
             else
@@ -92,8 +378,8 @@
 		break;
 
-        case XCODE_OVR:   // user fatal error
-        case XCODE_RI:    // user fatal error
-        case XCODE_ADEL:  // user fatal error
-        case XCODE_ADES:  // kill process 
+        case XCODE_OVR:    // user fatal error
+        case XCODE_RI:     // user fatal error
+        case XCODE_ADEL:   // user fatal error
+        case XCODE_ADES:   // user fatal error
         {
 		    error = EXCP_USER_ERROR;
@@ -120,47 +406,3 @@
 }  // end hal_do_exception()
 
-/////////////////////////////////////////
-void hal_exception_dump( thread_t * this,
-                         reg_t    * regs_tbl )
-{
-    // take the exception_lock located in io_cluster 
-    remote_spinlock_lock( XPTR( LOCAL_CLUSTER->io_cxy , &exception_lock ) );
-
-    if( this->type == THREAD_USER )
-    printk("\n================= USER ERROR / cycle %d ====================\n",
-           hal_time_stamp() );
-    else
-    printk("\n================= KERNEL PANIC / cycle %d ==================\n",
-           hal_time_stamp() );
-
-	printk("  thread type = %s / trdid = %x / pid %x / core[%x,%d]\n"
-           "  local locks = %d / remote locks = %d / blocked_vector = %X\n\n",
-           thread_type_str(this->type), this->trdid, this->process->pid, local_cxy,
-           this->core->lid, this->local_locks, this->remote_locks, this->blocked );
-
-	printk("CR    %X  EPC   %X  SR    %X  SP     %X\n",
-		   regs_tbl[UZ_CR], regs_tbl[UZ_EPC], regs_tbl[UZ_SR], regs_tbl[UZ_SP]);
-
-    printk("at_1  %X  v0_2  %X  v1_3  %X  a0_4   %X  a1_5   %X\n",
-   	       regs_tbl[UZ_AT], regs_tbl[UZ_V0], regs_tbl[UZ_V1], regs_tbl[UZ_A0], regs_tbl[UZ_A1]);
-
-    printk("a2_6  %X  a3_7  %X  t0_8  %X  t1_9   %X  t2_10  %X\n",
-		   regs_tbl[UZ_A2],regs_tbl[UZ_A3],regs_tbl[UZ_T0],regs_tbl[UZ_T1],regs_tbl[UZ_T2]);
-  
-    printk("t3_11 %X  t4_12 %X  t5_13 %X  t6_14  %X  t7_15  %X\n",
-		   regs_tbl[UZ_T3],regs_tbl[UZ_T4],regs_tbl[UZ_T5],regs_tbl[UZ_T6],regs_tbl[UZ_T7]);
-
-    printk("t8_24 %X  t9_25 %X  gp_28 %X  c0_hi  %X  c0_lo  %X\n",
-		   regs_tbl[UZ_T8],regs_tbl[UZ_T9],regs_tbl[UZ_GP],regs_tbl[UZ_HI],regs_tbl[UZ_LO]);
-
-    printk("s0_16 %X  s1_17 %X  s2_18 %X  s3_19  %X  s4_20  %X\n",
-		   regs_tbl[UZ_S0],regs_tbl[UZ_S1],regs_tbl[UZ_S2],regs_tbl[UZ_S3],regs_tbl[UZ_S4]);
-  
-    printk("s5_21 %X  s6_22 %X  s7_23 %X  s8_30  %X  ra_31  %X\n",
-	       regs_tbl[UZ_S5],regs_tbl[UZ_S6],regs_tbl[UZ_S7],regs_tbl[UZ_S8],regs_tbl[UZ_RA]);
-
-    // release the exception_lock
-    remote_spinlock_unlock( XPTR( LOCAL_CLUSTER->io_cxy , &exception_lock ) );
-
-}  // end hal_exception_dump()
-
+
