Index: trunk/hal/tsar_mips32/core/hal_vmm.c
===================================================================
--- trunk/hal/tsar_mips32/core/hal_vmm.c	(revision 623)
+++ trunk/hal/tsar_mips32/core/hal_vmm.c	(revision 624)
@@ -27,4 +27,5 @@
 #include <hal_gpt.h>
 #include <process.h>
+#include <thread.h>
 #include <vseg.h>
 #include <xlist.h>
@@ -41,10 +42,12 @@
 
 // extern global variables
-extern process_t process_zero;
+extern process_t            process_zero;
+extern chdev_directory_t    chdev_dir;
 
 //////////////////////////////////////////////////////////////////////////////////////////
 // This function is called by the process_zero_init() function during kernel_init.
 // It initializes the VMM of the kernel proces_zero (containing all kernel threads)
-// in the local cluster.
+// in the local cluster: it registers one "kcode" vseg in kernel VSL, and registers 
+// one big page in slot[0] of kernel GPT.
 //////////////////////////////////////////////////////////////////////////////////////////
 error_t  hal_vmm_kernel_init( boot_info_t * info )
@@ -68,9 +71,15 @@
     }
 
+#if DEBUG_HAL_VMM
+thread_t * this = CURRENT_THREAD;
+printk("\n[%s] thread[%x,%x] enter in cluster %x / gpt %x\n", 
+__FUNCTION__, this->process->pid, this->trdid, local_cxy, gpt );
+#endif
+
     // compute attr and ppn for one PTE1
-    uint32_t attr  = 0x8A800000;           // bits : V,C,X,G
-    uint32_t ppn   = (cxy << 20) >> 9;     // physical page index is 0
-
-    // set PTE1  in slot[0]
+    uint32_t attr = GPT_MAPPED | GPT_READABLE | GPT_CACHABLE | GPT_EXECUTABLE | GPT_GLOBAL;
+    uint32_t ppn  = cxy << 20;    
+
+    // register PTE1  in slot[0] of kernel GPT
     error = hal_gpt_set_pte( XPTR( cxy , gpt ) , 0 , attr , ppn );
 
@@ -81,8 +90,13 @@
         hal_core_sleep();
     }
+
+#if DEBUG_HAL_VMM
+printk("\n[%s] thread[%x,%x] created PT1[0] : ppn %x / attr %x\n", 
+__FUNCTION__, this->process->pid, this->trdid, ppn, attr );
+#endif
 
     // create kcode vseg and register it in kernel VSL
     vseg_t * vseg = vmm_create_vseg( &process_zero,
-                                     VSEG_TYPE_CODE,
+                                     VSEG_TYPE_KCODE,
                                      info->kcode_base,
                                      info->kcode_size,
@@ -97,5 +111,13 @@
     }
 
-}  // end hal_vmm_init()
+#if DEBUG_HAL_VMM
+printk("\n[%s] thread[%x,%x] registered kcode vseg[%x,%x]\n",
+__FUNCTION__, this->process->pid, this->trdid, info->kcode_base, info->kcode_size );
+hal_vmm_display( &process_zero , true );
+#endif
+
+    return 0;
+
+}  // end hal_kernel_vmm_init()
 
 //////////////////////////////////////////////////////////////////////////////////////////
@@ -111,36 +133,52 @@
     uint32_t ppn;
 
-// TODO check ppn value in kernel GPT (must be 0)
+#if DEBUG_HAL_VMM
+thread_t * this = CURRENT_THREAD;
+printk("\n[%s] thread[%x,%x] enter in cluster %x \n", 
+__FUNCTION__, this->process->pid, this->trdid, local_cxy );
+hal_vmm_display( process , true );
+hal_vmm_display( &process_zero , true );
+#endif
 
     // get cluster identifier
     cxy_t cxy = local_cxy;
 
+    // get extended pointer on kernel GPT
+    xptr_t k_gpt_xp = XPTR( cxy , &process_zero.vmm.gpt );
+
+    // get ppn and attributes from slot[0] of kernel GPT
+    hal_gpt_get_pte( k_gpt_xp , 0 , &attr , &ppn );
+
+#if DEBUG_HAL_VMM
+printk("\n[%s] thread[%x,%x] get PT1[0] ( ppn %x / attr %x ) from kernel  GPT\n", 
+__FUNCTION__, this->process->pid, this->trdid, ppn, attr );
+#endif
+
     // get extended pointer on user GPT
-    xptr_t gpt_xp = XPTR( cxy , &process->vmm.gpt );
-
-    // get ppn and attributes from slot[0] in kernel GPT
-    hal_gpt_get_pte( gpt_xp , 0 , &attr , &ppn );
-
-// check ppn and attributes 
-assert( (attr == 0x8A800000) && (ppn == ((cxy << 20) >> 9)),  __FUNCTION__,
-"bad ppn = %x or attr = %x in slot[0] of kernel GPT\n", ppn , attr );
- 
+    xptr_t u_gpt_xp = XPTR( cxy , &process->vmm.gpt );
+
     // update user GPT : set PTE1 in slot[0]
-    error = hal_gpt_set_pte( gpt_xp , 0 , attr , ppn );
+    error = hal_gpt_set_pte( u_gpt_xp , 0 , attr , ppn );
 
     if( error )
     {
-        printk("\n[ERROR] in %s : cannot update GPT in cluster %x\n",
+        printk("\n[ERROR] in %s : cannot update user GPT in cluster %x\n",
         __FUNCTION__ , cxy );
         return -1;
     }
 
+#if DEBUG_HAL_VMM
+printk("\n[%s] thread[%x,%x] registered PT1[0] ( ppn %x / attr %x ) to user GPT\n", 
+__FUNCTION__, this->process->pid, this->trdid, ppn, attr );
+#endif
+
     // get pointer on the unique vseg registered in kernel VSL
-    xptr_t root_xp = XPTR( cxy , &process_zero.vmm.vsegs_root );
-    vseg_t * vseg = XLIST_FIRST( root_xp , vseg_t , xlist );
+    xptr_t   root_xp = XPTR( cxy , &process_zero.vmm.vsegs_root );
+    xptr_t   vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist );
+    vseg_t * vseg    = GET_PTR( vseg_xp );
 
 // check vsegs_nr 
-assert( (process_zero.vmm.vsegs_nr == 1 ) , __FUNCTION__,
-"bad vsegs number in kernel VSL\n" );
+assert( (process_zero.vmm.vsegs_nr == 1 ) ,
+"bad vsegs number in kernel VSL = %d\n", process_zero.vmm.vsegs_nr );
 
     // update user VSL : register one new vseg for kcode
@@ -154,9 +192,99 @@
     if( new == NULL )
     {
-        printk("\n[ERROR] in %s : cannot update VSL in cluster %x\n",
+        printk("\n[ERROR] in %s : cannot update user VSL in cluster %x\n",
         __FUNCTION__ , cxy );
         return -1;
     }
-}
-
-
+
+#if DEBUG_HAL_VMM
+printk("\n[%s] thread[%x,%x] created vseg %s ( base %x / size %x ) to user VSL\n", 
+__FUNCTION__, this->process->pid, this->trdid,
+vseg_type_str(vseg->type) , vseg->min, (vseg->max - vseg->min) );
+hal_vmm_display( process , true );
+#endif
+
+    return 0;
+
+}  // end hal_vmm_kernel_update()
+
+//////////////////////////////////////////
+void hal_vmm_display( process_t * process,
+                      bool_t      mapping )
+{
+    vmm_t * vmm = &process->vmm;
+    gpt_t * gpt = &vmm->gpt;
+
+    // get pointers on TXT0 chdev
+    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
+    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
+    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
+
+    // get extended pointer on remote TXT0 lock
+    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
+
+    // get locks protecting the VSL and the GPT
+    remote_rwlock_rd_acquire( XPTR( local_cxy , &vmm->vsegs_lock ) );
+    remote_rwlock_rd_acquire( XPTR( local_cxy , &vmm->gpt_lock ) );
+
+    // get TXT0 lock
+    remote_busylock_acquire( lock_xp );
+
+    nolock_printk("\n***** VSL and GPT for process %x in cluster %x\n",
+    process->pid , local_cxy );
+
+    // scan the list of vsegs
+    xptr_t         root_xp = XPTR( local_cxy , &vmm->vsegs_root );
+    xptr_t         iter_xp;
+    xptr_t         vseg_xp;
+    vseg_t       * vseg;
+    XLIST_FOREACH( root_xp , iter_xp )
+    {
+        vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
+        vseg    = GET_PTR( vseg_xp );
+
+        nolock_printk(" - %s : base = %X / size = %X / npages = %d\n",
+        vseg_type_str( vseg->type ) , vseg->min , vseg->max - vseg->min , vseg->vpn_size );
+
+        if( mapping )
+        {
+            vpn_t    vpn     = vseg->vpn_base;
+            vpn_t    vpn_max = vpn + vseg->vpn_size;
+            ppn_t    ppn;
+            uint32_t attr;
+
+            while( vpn < vpn_max )
+            {
+                hal_gpt_get_pte( XPTR( local_cxy , gpt ) , vpn , &attr , &ppn );
+
+                if( attr & GPT_MAPPED )
+                {
+                    if( attr & GPT_SMALL )
+                    {
+                        nolock_printk("    . SMALL : vpn = %X / attr = %X / ppn = %X\n",
+                        vpn , attr , ppn );
+                        vpn++;
+                    }
+                    else
+                    {
+                        nolock_printk("    . BIG   : vpn = %X / attr = %X / ppn = %X\n",
+                        vpn , attr , ppn );
+                        vpn += 512;
+                    }
+                }
+                else
+                {
+                    vpn++;
+                }
+            }
+        }
+    }
+
+    // release TXT0 lock
+    remote_busylock_release( lock_xp );
+
+    // release the VSK and GPT locks
+    remote_rwlock_rd_release( XPTR( local_cxy , &vmm->vsegs_lock ) );
+    remote_rwlock_rd_release( XPTR( local_cxy , &vmm->gpt_lock ) );
+
+}  // hal_vmm_display()
+
