Index: trunk/hal/tsar_mips32/core/hal_exception.c
===================================================================
--- trunk/hal/tsar_mips32/core/hal_exception.c	(revision 407)
+++ trunk/hal/tsar_mips32/core/hal_exception.c	(revision 408)
@@ -99,4 +99,26 @@
 xcode_values_t;
 
+////////////////////////////////////////////////////
+static char * hal_mmu_exception_str( uint32_t code )
+{
+    if     ( code == MMU_WRITE_PT1_UNMAPPED        ) return "WRITE_PT1_UNMAPPED";
+    else if( code == MMU_WRITE_PT2_UNMAPPED        ) return "WRITE_PT2_UNMAPPED";
+    else if( code == MMU_WRITE_PRIVILEGE_VIOLATION ) return "WRITE_PRIVILEGE_VIOLATION";
+    else if( code == MMU_WRITE_ACCESS_VIOLATION    ) return "WRITE_ACCESS_VIOLATION";
+    else if( code == MMU_WRITE_UNDEFINED_XTN       ) return "WRITE_UNDEFINED_XTN";
+    else if( code == MMU_WRITE_PT1_ILLEGAL_ACCESS  ) return "WRITE_PT1_ILLEGAL_ACCESS";
+    else if( code == MMU_WRITE_PT2_ILLEGAL_ACCESS  ) return "WRITE_PT2_ILLEGAL_ACCESS";
+    else if( code == MMU_WRITE_DATA_ILLEGAL_ACCESS ) return "WRITE_DATA_ILLEGAL_ACCESS";
+    else if( code == MMU_READ_PT1_UNMAPPED         ) return "READ_PT1_UNMAPPED";
+    else if( code == MMU_READ_PT2_UNMAPPED         ) return "READ_PT2_UNMAPPED";
+    else if( code == MMU_READ_PRIVILEGE_VIOLATION  ) return "READ_PRIVILEGE_VIOLATION";
+    else if( code == MMU_READ_EXEC_VIOLATION       ) return "READ_EXEC_VIOLATION";
+    else if( code == MMU_READ_UNDEFINED_XTN        ) return "READ_UNDEFINED_XTN";
+    else if( code == MMU_READ_PT1_ILLEGAL_ACCESS   ) return "READ_PT1_ILLEGAL_ACCESS";
+    else if( code == MMU_READ_PT2_ILLEGAL_ACCESS   ) return "READ_PT2_ILLEGAL_ACCESS";
+    else if( code == MMU_READ_DATA_ILLEGAL_ACCESS  ) return "READ_DATA_ILLEGAL_ACCESS";
+    else                                             return "undefined";
+}
+
 //////////////////////////////////////////////////////////////////////////////////////////
 // This function is called when a FPU Coprocessor Unavailable exception has been 
@@ -120,5 +142,5 @@
         if( core->fpu_owner != this )
 	    {
-		    hal_fpu_context_save ( core->fpu_owner->fpu_context );
+		    hal_fpu_context_save( XPTR( local_cxy , core->fpu_owner ) );
         }
 	}
@@ -179,6 +201,13 @@
     }
 
- excp_dmsg("\n[DBG] %s : core[%x,%d] / is_ins %d / code %x / vaddr %x\n",
-__FUNCTION__ , local_cxy , this->core->lid , is_ins, excp_code, bad_vaddr );
+// @@@
+thread_t * parent = (thread_t *)0xa4000;
+uint32_t cond = (this == 0xe0000) && (hal_time_stamp() > 5380000);
+
+if( cond ) hal_gpt_display( this->process );
+if( cond ) hal_gpt_display( parent->process );
+if( cond ) printk("\n[DBG] %s : core[%x,%d] / is_ins %d / %s / vaddr %x\n",
+__FUNCTION__ , local_cxy , this->core->lid , is_ins,
+hal_mmu_exception_str(excp_code) , bad_vaddr );
 
    // analyse exception code
@@ -217,15 +246,15 @@
             return EXCP_USER_ERROR;
         }
-        case MMU_WRITE_ACCESS_VIOLATION:     // user error or Copy-on-Write
+        case MMU_WRITE_ACCESS_VIOLATION:     // user error, or Copy-on-Write
         {
             // access page table to get GPT_COW flag
             bool_t cow = hal_gpt_pte_is_cow( &(process->vmm.gpt),
-                                             bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );  // vpn
+                                             bad_vaddr >> CONFIG_PPM_PAGE_SHIFT ); 
 
             if( cow )                        // Copy-on-Write 
             {
                 // try to allocate and copy the page
-                error = vmm_copy_on_write( process,
-                                           bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );  // vpn
+                error = vmm_handle_cow( process,
+                                        bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );  
                 if( error )
                 {
@@ -238,5 +267,5 @@
                 {
 
-excp_dmsg("\n[DBG] %s : core[%x,%d] / copy-on-write handled for vaddr = %x\n",
+if( cond ) printk("\n[DBG] %s : core[%x,%d] / copy-on-write handled for vaddr = %x\n",
 __FUNCTION__ , local_cxy , this->core->lid , bad_vaddr );
 
@@ -274,13 +303,14 @@
 //////////////////////////////////////////////////////////////////////////////////////////
 // @ this     : pointer on faulty thread descriptor.
-// @ regs_tbl : pointer on register array.
+// @ uzone : pointer on register array.
 // @ error    : EXCP_USER_ERROR or EXCP_KERNEL_PANIC
 //////////////////////////////////////////////////////////////////////////////////////////
 static void hal_exception_dump( thread_t * this,
-                                reg_t    * regs_tbl,
+                                reg_t    * uzone,
                                 error_t    error )
 {
-    uint32_t  save_sr;
-    core_t  * core = this->core;
+    uint32_t    save_sr;
+    core_t    * core    = this->core;
+    process_t * process = this->process;
 
     // get pointers on TXT0 chdev
@@ -297,38 +327,39 @@
     if( error == EXCP_USER_ERROR )
     {
-        nolock_printk("\n========= USER ERROR / core[%x,%d] / cycle %d ==============\n",
-        local_cxy , core->lid , (uint32_t)hal_get_cycles() );
+        nolock_printk("\n=== USER ERROR / trdid %x / pid %x / core[%x,%d] / cycle %d ===\n",
+        this->trdid, process->pid, local_cxy, core->lid , (uint32_t)hal_get_cycles() );
     }
     else
     {
-        nolock_printk("\n======= KERNEL PANIC / core[%x,%d] / cycle %d ==============\n",
-        local_cxy , core->lid , (uint32_t)hal_get_cycles() );
+        nolock_printk("\n=== KERNEL PANIC / trdid %x / pid %x / core[%x,%d] / cycle %d ===\n",
+        this->trdid, process->pid, local_cxy, core->lid , (uint32_t)hal_get_cycles() );
     }
 
-	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("cp0_cr %X   cp0_epc %X   cp0_sr %X   cp2_mode %X\n",
-		   regs_tbl[UZ_CR], regs_tbl[UZ_EPC], regs_tbl[UZ_SR], regs_tbl[UZ_MODE]);
-
-    nolock_printk("at_01 %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("local locks = %d / remote locks = %d / blocked_vector = %X\n\n",
+    this->local_locks, this->remote_locks, this->blocked );
+
+    nolock_printk("c0_cr   %X  c0_epc  %X  c0_sr  %X  c0_th  %X\n",
+    uzone[UZ_CR], uzone[UZ_EPC], uzone[UZ_SR], uzone[UZ_TH] );
+
+    nolock_printk("c2_mode %X  c2_ptpr %X\n",
+    uzone[UZ_MODE], uzone[UZ_PTPR] );
+
+    nolock_printk("at_01   %X  v0_2    %X  v1_3   %X  a0_4   %X  a1_5   %X\n",
+   	uzone[UZ_AT], uzone[UZ_V0], uzone[UZ_V1], uzone[UZ_A0], uzone[UZ_A1] );
+
+    nolock_printk("a2_6    %X  a3_7    %X  t0_8   %X  t1_9   %X  t2_10  %X\n",
+	uzone[UZ_A2], uzone[UZ_A3], uzone[UZ_T0], uzone[UZ_T1], uzone[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("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("t3_11   %X  t4_12   %X  t5_13  %X  t6_14  %X  t7_15  %X\n",
+	uzone[UZ_T3], uzone[UZ_T4], uzone[UZ_T5], uzone[UZ_T6], uzone[UZ_T7] );
+
+    nolock_printk("s0_16   %X  s1_17   %X  s2_18  %X  s3_19  %X  s4_20  %X\n",
+	uzone[UZ_S0], uzone[UZ_S1], uzone[UZ_S2], uzone[UZ_S3], uzone[UZ_S4] );
   
-    nolock_printk("s5_21 %X  s6_22 %X  s7_23 %X  s8_24  %X  ra_25  %X\n",
-	       regs_tbl[UZ_S5],regs_tbl[UZ_S6],regs_tbl[UZ_S7],regs_tbl[UZ_T8],regs_tbl[UZ_T9]);
-
-    nolock_printk("gp_28 %X  sp_29 %X  S8_30 %X  ra_31  %X\n",
-		   regs_tbl[UZ_GP],regs_tbl[UZ_SP],regs_tbl[UZ_S8],regs_tbl[UZ_RA]);
+    nolock_printk("s5_21   %X  s6_22   %X  s7_23  %X  s8_24  %X  ra_25  %X\n",
+	uzone[UZ_S5], uzone[UZ_S6], uzone[UZ_S7], uzone[UZ_T8], uzone[UZ_T9] );
+
+    nolock_printk("gp_28   %X  sp_29   %X  S8_30  %X  ra_31  %X\n",
+	uzone[UZ_GP], uzone[UZ_SP], uzone[UZ_S8], uzone[UZ_RA] );
 
     // release the lock
@@ -337,20 +368,22 @@
 }  // 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 )
-{
-	error_t             error;
-	uint32_t            excCode;                  // 4 bits XCODE from CP0_CR
+///////////////////////
+void hal_do_exception()
+{
+    uint32_t   * uzone;
+    thread_t   * this;
+	error_t      error;
+	uint32_t     excCode;                  // 4 bits XCODE from CP0_CR
+
+    // get pointer on faulty thread uzone
+    this  = CURRENT_THREAD;
+    uzone = (uint32_t *)CURRENT_THREAD->uzone;
 
     // get 4 bits XCODE from CP0_CR register
-	excCode        = (regs_tbl[UZ_CR] >> 2) & 0xF;
+	excCode        = (uzone[UZ_CR] >> 2) & 0xF;
 
 excp_dmsg("\n[DBG] %s : core[%x,%d] / thread %x in process %x / xcode %x / cycle %d\n",
 __FUNCTION__, local_cxy, this->core->lid, this->trdid, 
-this->process->pid, excCode, (uint32_t)hal_get_cycle() );
+this->process->pid, excCode, (uint32_t)hal_get_cycles() );
 
 	switch(excCode)
@@ -368,5 +401,5 @@
 	    case XCODE_CPU:    // can be non fatal
         {
-            if( ((regs_tbl[UZ_CR] >> 28) & 0x3) == 1 )  // unavailable FPU
+            if( ((uzone[UZ_CR] >> 28) & 0x3) == 1 )     // unavailable FPU
             {
                 error = hal_fpu_exception( this );
@@ -395,5 +428,5 @@
 	if( error == EXCP_USER_ERROR )          //  user error => kill user process
 	{
-        hal_exception_dump( this , regs_tbl , error );
+        hal_exception_dump( this , uzone , error );
 
         // FIXME : replace this loop by sys_kill()
@@ -403,10 +436,12 @@
     else if( error == EXCP_KERNEL_PANIC )   // kernel error => kernel panic
     {
-        hal_exception_dump( this , regs_tbl , error );
-        hal_core_sleep();
+        hal_exception_dump( this , uzone , error );
+        panic( "KERNEL_PANIC for thread %x in process %x on core [%x,%d]/n",
+        this->trdid , this->process->pid , local_cxy , this->core->lid );
     }
 
 excp_dmsg("\n[DBG] %s : core[%x,%d] exit / thread %x in process %x / cycle %d\n",
-__FUNCTION__, local_cxy, this->core->lid, this->trdid, this->process->pid, hal_time_stamp() );
+__FUNCTION__, local_cxy, this->core->lid, this->trdid, this->process->pid,
+(uint32_t)hal_get_cycles() );
 
 }  // end hal_do_exception()
