Index: trunk/hal/tsar_mips32/core/hal_kentry.S
===================================================================
--- trunk/hal/tsar_mips32/core/hal_kentry.S	(revision 408)
+++ trunk/hal/tsar_mips32/core/hal_kentry.S	(revision 418)
@@ -99,9 +99,10 @@
 	
 #------------------------------------------------------------------------------------
-# This code is executed when the core is in user mode:
+# This code is executed when the core is in user mode,
+# to handle a syscall, an interrupt, or an user exception.
 # - save current c2_mode in $26.
 # - set MMU OFF.
-# - save user stack pointer in $27.
-# - set kernel stack pointer in $29.
+# - copy user stack pointer in $27 to be saved in uzone.
+# - set kernel stack pointer in $29 == top_kernel_stack(this).
 
 user_mode:
@@ -117,8 +118,9 @@
 
 #------------------------------------------------------------------------------------
-# This code is executed when the core is already in kernel mode:
+# This code is executed when the core is already in kernel mode,
+# after a syscall, to handle an interrupt.
 # - save current c2_mode in $26.
 # - set MMU OFF.
-# - save current kernel stack pointer in $27.
+# - copy current kernel stack pointer in $27.
 
 kernel_mode:
@@ -134,7 +136,7 @@
 # - $26 contains the previous c2_mode value.
 # - $27 contains the previous sp value (can be usp or ksp).
-# - $29 contains the kernel stack pointer.
+# - $29 contains the curren kernel stack pointer.
 # We execute the following actions:
-# - allocate an uzone in kernel stack, decrementing $29.
+# - decrement $29 to allocate an uzone in kernel stack
 # - save relevant GPR, CP0 and CP2 registers to uzone.
 # - set the SR in kernel mode: IRQ disabled, clear exl.
@@ -198,5 +200,6 @@
 	mtc0	$3,	    $12			        # set new c0_sr 
 
-#if CONFIG_KENTRY_DEBUG
+#-----------------------
+#if CONFIG_KENTRY_DEBUG 
 
     # display "enter" message
@@ -207,4 +210,12 @@
     jal     putx
     nop
+    la      $4,     msg_cycle
+    jal     puts
+    nop
+    jal     hal_time_stamp
+    nop
+    move    $4,     $2
+    jal     putx
+    nop
     la      $4,     msg_crlf
     jal     puts
@@ -270,19 +281,18 @@
     jal     puts
     nop    
-	
+
 #endif
+#-----
     
 #------------------------------------------------------------------------------------
-# This code update the uzone field in thread descriptor, 
-# and call the relevant Interrupt / Exception / Syscall handler,
+# This code calls the relevant Interrupt / Exception / Syscall handler,
 # depending on XCODE in CP0_CR.
-# assumption: $29 contains the kernel stack pointer, that is the uzone base.
+# In case of syscall or exception, it set the "uzone" pointer in thread descriptor,
+# that is used by the hal_do_syscall() & hal_do_exception() functions.
+# WARNING: This "uzone" pointer is NOT modified in case of interrupt,
+# because syscalls can be interrupted, and we want preserve this pointer.
 
 	mfc0    $17,    $13                 # $17 <= CR
 	andi    $17,    $17,   0x3F         # $17 <= XCODE
-
-	mfc0    $4,     $4,   2             # $4 <= pointer on thread desc
-    sw      $29,    8($4)               # update uzone pointer in thread desc
-
 	ori	    $8,     $0,   0x20
     beq	    $8,	    $17,  cause_sys     # go to syscall handler
@@ -292,4 +302,6 @@
 
 cause_excp:
+	mfc0    $4,     $4,   2             # get pointer on thread desc
+    sw      $29,    8($4)               # update uzone pointer in thread desc
 	jal     hal_do_exception            # call exception handler
 	nop
@@ -298,4 +310,6 @@
 
 cause_sys:
+	mfc0    $4,     $4,   2             # get pointer on thread desc
+    sw      $29,    8($4)               # update uzone pointer in thread desc
 	jal 	hal_do_syscall              # call syscall handler                 
     nop
@@ -309,8 +323,13 @@
 # -----------------------------------------------------------------------------------
 # Kernel exit
-# The pointer on uzone is supposed to be contained in $29
+# - All registers saved in the uzone are restored, using the pointer on uzone,
+#   that is contained in $29.
+# - The "uzone" field in thread descriptor, that has beeen modified at kernel entry
+#   is restored from value contained in the uzone[UZ_SP] slot. 
 # -----------------------------------------------------------------------------------
+
 kentry_exit:
 
+#----------------------
 #if CONFIG_KENTRY_DEBUG
 
@@ -322,4 +341,12 @@
     jal     putx
     nop
+    la      $4,     msg_cycle
+    jal     puts
+    nop
+    jal     hal_time_stamp
+    nop
+    move    $4,     $2
+    jal     putx
+    nop
     la      $4,     msg_crlf
     jal     puts
@@ -387,4 +414,5 @@
 	
 #endif
+#-----
     
 	# restore registers from uzone
@@ -475,3 +503,6 @@
     .align 2
     .asciiz "\nexit kernel : &uzone = " 
-
+msg_cycle:
+    .align 2
+    .asciiz " / cycle = "
+
