Index: trunk/kernel/mm/vmm.c
===================================================================
--- trunk/kernel/mm/vmm.c	(revision 179)
+++ trunk/kernel/mm/vmm.c	(revision 204)
@@ -62,32 +62,18 @@
     intptr_t  size;
 
+    vmm_dmsg("\n[INFO] %s : enter for process %x\n", __FUNCTION__ , process->pid );
+
     // get pointer on VMM
     vmm_t   * vmm = &process->vmm;
 
-    // check UTILS zone size
-    if( (CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE ) >
-        CONFIG_VMM_ELF_BASE )
-    {
-        printk("\n[PANIC] in %s : UTILS zone too small for process %x\n",
-               __FUNCTION__ , process->pid );
-        hal_core_sleep();
-    }
-
-    // check max number of stacks slots
-    if( CONFIG_THREAD_MAX_PER_CLUSTER > 32 )
-    {
-        printk("\n[PANIC] in %s : max number of threads per cluster for a single process"
-               " cannot be larger than 32\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    // check STACK zone size
-    if( (CONFIG_VMM_STACK_SIZE * CONFIG_THREAD_MAX_PER_CLUSTER) >
-        (CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE) )
-    {
-        printk("\n[PANIC] in %s : STACK zone too small for process %x\n",
-               __FUNCTION__ , process->pid );
-        hal_core_sleep();
-    }
+    assert( ((CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE) 
+            <= CONFIG_VMM_ELF_BASE) , __FUNCTION__ , "UTILS zone too small\n" );
+
+    assert( (CONFIG_THREAD_MAX_PER_CLUSTER <= 32) , __FUNCTION__ ,
+            "no more than 32 threads per cluster for a single process\n");
+
+    assert( ((CONFIG_VMM_STACK_SIZE * CONFIG_THREAD_MAX_PER_CLUSTER) <=
+             (CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE)) , __FUNCTION__ ,
+             "STACK zone too small\n");
 
     // initialize the rwlock protecting the vsegs list
@@ -101,10 +87,6 @@
                         CONFIG_VMM_GRDXT_W2,
                         CONFIG_VMM_GRDXT_W3 );
-    if( error )
-    {
-        printk("\n[PANIC] in %s : cannot initialize radix tree for process %x\n",
-               __FUNCTION__ , process->pid );
-        hal_core_sleep();
-    }
+ 
+    assert( (error == 0) , __FUNCTION__ , "cannot initialize radix tree\n" );
 
     // register kentry vseg in VMM
@@ -112,10 +94,7 @@
     size = CONFIG_VMM_KENTRY_SIZE << CONFIG_PPM_PAGE_SHIFT;
     vseg_kentry = vmm_create_vseg( process , base , size , VSEG_TYPE_CODE );
-    if( vseg_kentry == NULL )
-    {
-        printk("\n[PANIC] in %s : cannot register kent vseg for process %x\n",
-               __FUNCTION__ , process->pid );
-        hal_core_sleep();
-    }
+
+    assert( (vseg_kentry != NULL) , __FUNCTION__ , "cannot register kentry vseg\n" );
+
     vmm->kent_vpn_base = 1;
 
@@ -124,10 +103,7 @@
     size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT;
     vseg_args = vmm_create_vseg( process , base , size , VSEG_TYPE_DATA );
-    if( vseg_args == NULL )
-    {
-        printk("\n[PANIC] in %s : cannot register args vseg for process %x\n",
-               __FUNCTION__ , process->pid );
-        hal_core_sleep();
-    }
+
+    assert( (vseg_args != NULL) , __FUNCTION__ , "cannot register args vseg\n" );
+
     vmm->args_vpn_base = CONFIG_VMM_KENTRY_SIZE + 1;
 
@@ -136,10 +112,7 @@
     size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT;
     vseg_envs = vmm_create_vseg( process , base , size , VSEG_TYPE_DATA );
-    if( vseg_envs == NULL )
-    {
-        printk("\n[PANIC] in %s : cannot register envs vseg for process %x\n",
-               __FUNCTION__ , process->pid );
-        hal_core_sleep();
-    }
+
+    assert( (vseg_envs != NULL) , __FUNCTION__ , "cannot register envs vseg\n" );
+
     vmm->envs_vpn_base = CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + 1;
 
@@ -148,19 +121,13 @@
     size = (CONFIG_VMM_MMAP_BASE-CONFIG_VMM_HEAP_BASE) << CONFIG_PPM_PAGE_SHIFT;
     vseg_heap = vmm_create_vseg( process , base , size , VSEG_TYPE_HEAP );
-    if( vseg_heap == NULL )
-    {
-        printk("\n[PANIC] in %s : cannot register heap vseg in for process %x\n",
-               __FUNCTION__ , process->pid );
-        hal_core_sleep();
-    }
+
+    assert( (vseg_heap != NULL) , __FUNCTION__ , "cannot register heap vseg\n" );
+
     vmm->heap_vpn_base = CONFIG_VMM_HEAP_BASE;
 
     // initialize generic page table
     error = hal_gpt_create( &vmm->gpt );
-    if( error )
-    {
-        printk("PANIC in %s : cannot initialize page table\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
+
+    assert( (error == 0) , __FUNCTION__ , "cannot initialize page table\n");
 
     // initialize STACK allocator
@@ -181,5 +148,8 @@
 
     hal_fence();
-}
+
+    vmm_dmsg("\n[INFO] %s : exit for process %x\n", __FUNCTION__ , process->pid );
+
+}  // end vmm_init()
 
 //////////////////////////////////////////
@@ -301,5 +271,6 @@
 
     return 0;
-}
+
+}  // vmm_copy()
 
 ///////////////////////////////////////
@@ -342,5 +313,6 @@
     // release memory allocated to the local page table
     hal_gpt_destroy( &vmm->gpt );
-}
+
+}  // end vmm_destroy()
 
 /////////////////////////////////////////////////
@@ -357,9 +329,11 @@
 	{
 		vseg = LIST_ELEMENT( iter , vseg_t , list );
+
 		if( ((vpn_base + vpn_size) > vseg->vpn_base) &&
              (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg;
 	}
     return NULL;
-}
+
+}  // end vmm_check_conflict()
 
 ////////////////////////////////////////////////////////////////////////////////////////////
@@ -399,5 +373,6 @@
     *vpn_size = CONFIG_VMM_STACK_SIZE - 1;
     return 0;
-}
+
+} // end vmm_stack_alloc()
 
 ////////////////////////////////////////////////////////////////////////////////////////////
@@ -464,5 +439,6 @@
     *vpn_size = size;
     return 0;
-}
+
+}  // end vmm_mmap_alloc()
 
 //////////////////////////////////////////////
@@ -473,5 +449,5 @@
 {
     vseg_t     * vseg;          // created vseg pointer
-    vpn_t        vpn_base;      // vseg first page
+    vpn_t        vpn_base;      // first page index
     vpn_t        vpn_size;      // number of pages
 	error_t      error;
@@ -480,8 +456,8 @@
 	vmm_t * vmm = &process->vmm;
 
-	vmm_dmsg("\n[INFO] %s enter for process %x / base = %x / size = %x / type = %s\n",
+	vmm_dmsg("\n[INFO] %s : enter for process %x / base = %x / size = %x / type = %s\n",
 		     __FUNCTION__ , process->pid , base , size , vseg_type_str(type) );
 
-    // compute base, size, vpn_base, vpn_size, depending on type
+    // compute base, size, vpn_base, vpn_size, depending on vseg type
     // we use the VMM specific allocators for STACK and MMAP vsegs
     if( type == VSEG_TYPE_STACK )
@@ -520,6 +496,9 @@
     else
     {
-        vpn_base = ARROUND_DOWN( base , CONFIG_PPM_PAGE_SIZE ) >> CONFIG_PPM_PAGE_SHIFT;
-	    vpn_size = ARROUND_UP( base + size , CONFIG_PPM_PAGE_SIZE ) >> CONFIG_PPM_PAGE_SHIFT;
+        uint32_t vpn_min = base >> CONFIG_PPM_PAGE_SHIFT;
+        uint32_t vpn_max = (base + size - 1) >> CONFIG_PPM_PAGE_SHIFT;
+
+        vpn_base = vpn_min;
+	    vpn_size = vpn_max - vpn_min + 1;
     }
 
@@ -555,5 +534,5 @@
 	rwlock_wr_unlock( &vmm->vsegs_lock );
 
-	vmm_dmsg("\n[INFO] : %s exit for process %x, vseg [%x, %x] has been mapped\n",
+	vmm_dmsg("\n[INFO] %s : exit for process %x / vseg [%x, %x] has been mapped\n",
 		     __FUNCTION__ , process->pid , vseg->min , vseg->max );
 
