Index: soft/giet_vm/boot/boot_handler.c
===================================================================
--- soft/giet_vm/boot/boot_handler.c	(revision 165)
+++ soft/giet_vm/boot/boot_handler.c	(revision 167)
@@ -31,19 +31,20 @@
 // As most applications use only a limited number of segments, the number of PT2s 
 // actually used by a given virtual space is generally smaller than 2048, and is
-// defined in the MAPPING_INFO_BINARY data structure (using the length field). 
-// The value is calculated and put in _max_pt2 indexed by the vspace_id.
-// The physical alignment constraints, is ensured by the align flag in the MAPPING_INFO 
-// structure.
+// defined by the (GIET_NB_PT2_MAX) configuration parameter.
 // The max number of virtual spaces (GIET_NB_VSPACE_MAX) is a configuration parameter.
 //
-// Each page table (one page table per virtual space) is monolithic:
-// - a first 8K aligned PT1[2148] array, indexed by the (ix1) field of VPN. 
+// Each page table (one page table per virtual space) is monolithic, and
+// contains one PT1 and (GIET_NB_PT2_MAX) PT2s. The PT1 is addressed using the ix1 field
+// (11 bits) of the VPN, and the selected PT2 is addressed using the ix2 field (9 bits).
+// - PT1[2048] : a first 8K aligned array of unsigned int, indexed by the (ix1) field of VPN. 
+//   Each entry in the PT1 contains a 32 bits PTD. The MSB bit PTD[31] is 
+//   the PTD valid bit, and LSB bits PTD[19:0] are the 20 MSB bits of the physical base
+//   address of the selected PT2.
 //   The PT1 contains 2048 PTD of 4 bytes => 8K bytes.
-// - an aray of array PT2[1024][_max_pt2[vspace_id]], indexed by
-//   the (ix2) field of the VPN, and by the PT2 index (pt2_id).
-//   Each PT2 contains 512 PTE2 of 8bytes => 4Kbytes * _max_pt2[vspace_id]
-// The size of each page table is 8K + (_max_pt2[vspace_id])*4K bytes.
-// All page tables must be stored in the seg_kernel_pt segment (at address
-// seg_kernel_pt_base) 
+// - PT2[1024][GIET_NB_PT2_MAX] : an array of array of unsigned int. 
+//   Each PT2[1024] must be 4K aligned,  and each entry in a PT2 contains two unsigned int: 
+//   the first word contains the protection flags, and the second word contains the PPN.
+//   Each PT2 contains 512 PTE2 of 8bytes => 4K bytes.
+// The total size of a page table is finally = 8K + (GIET_NB_PT2_MAX)*4K bytes.
 ////////////////////////////////////////////////////////////////////////////////////
 
@@ -55,9 +56,12 @@
 #include <stdarg.h>
 
-
 #if !defined(GIET_NB_VSPACE_MAX) 
 # error The GIET_NB_VSPACE_MAX value must be defined in the 'giet_config.h' file !
 #endif
 
+#if !defined(GIET_NB_PT2_MAX) 
+# error The GIET_NB_PT2_MAX value must be defined in the 'giet_config.h' file !
+#endif
+
 ////////////////////////////////////////////////////////////////////////////
 //  Page Tables global variables
@@ -66,8 +70,4 @@
 // Next free PT2 index array
 unsigned int  _next_free_pt2[GIET_NB_VSPACE_MAX] =
-                         { [0 ... GIET_NB_VSPACE_MAX-1] = 0 };
-
-// Max number of PT2 array
-unsigned int  _max_pt2[GIET_NB_VSPACE_MAX] =
                          { [0 ... GIET_NB_VSPACE_MAX-1] = 0 };
 
@@ -439,5 +439,5 @@
 // allocation), this function checks a possible overflow of the PT2 array.
 //
-// The parametre global is a boolean taht indicate wether a global vseg is
+// The global parameter is a boolean indicating wether a global vseg is
 // being mapped.
 //////////////////////////////////////////////////////////////////////////////
@@ -445,6 +445,5 @@
                    unsigned int    vpn,           
                    unsigned int    flags,
-                   unsigned int    ppn,
-                   unsigned char   global )
+                   unsigned int    ppn )
 {
     unsigned int    ix1;
@@ -458,19 +457,9 @@
     ix2 = vpn  & 0x1FF;         //  9 bits
 
-        
-    unsigned int max_pt2   = _max_pt2[vspace_id];
-    if(max_pt2 == 0)
-    {
-        boot_puts("Unfound page table for vspace ");
-        boot_putw(vspace_id);
-        boot_puts("\n");
-        boot_exit();
-    }
-
-    page_table_t* pt    = (page_table_t *)_ptabs[vspace_id];
+    page_table_t* pt = (page_table_t *)_ptabs[vspace_id];
     if ( (pt->pt1[ix1] & PTE_V) == 0 )   // set a new PTD in PT1 
     {
         pt2_id = _next_free_pt2[vspace_id];
-        if ( pt2_id == max_pt2 )
+        if ( pt2_id == GIET_NB_PT2_MAX )
         {
             boot_puts("\n[BOOT ERROR] in boot_add_pte() function\n");
@@ -557,6 +546,5 @@
                           vpn,
                           flags,
-                          ppn,
-                          0 );
+                          ppn );
             vpn++;
             ppn++;
@@ -593,6 +581,5 @@
                           vpn,
                           flags,
-                          ppn,
-                          1 );
+                          ppn );
             vpn++;
             ppn++;
@@ -616,7 +603,8 @@
 // This function compute the physical base address for a vseg
 // as specified in the mapping info data structure.
-// It updates the pbase field of the vseg.
-// It updates the page allocator (nextfreepage field of the pseg),
-// and checks a possible pseg overflow.
+// It updates the pbase and the length fields of the vseg.
+// It updates the pbase and vbase fields of all vobjs in the vseg.
+// It updates the next_base field of the pseg.
+// It checks a possible pseg overflow.
 // It is a global vseg if vspace_id = (-1) 
 ///////////////////////////////////////////////////////////////////////////
@@ -641,10 +629,17 @@
     else                                // unconstrained mapping
     {
-        vseg->pbase = pseg->base + (pseg->next_free_page<<12);
+        vseg->pbase = pseg->next_base;
+
+        // test alignment constraint
+        if ( vobj[vseg->vobj_offset].align )
+        {
+            vseg->pbase = align_to( vseg->pbase, vobj[vseg->vobj_offset].align );
+        }
     }
     
-    // loop on vobjs to computes the length of the vseg,
-    // initialise the vaddr and paddr fields of all vobjs,
-    // and initialise the page table pointers array 
+    // loop on vobjs to (1) computes the length of the vseg,
+    // (2) initialise the vaddr and paddr fields of all vobjs,
+    // (3) initialise the page table pointers array 
+
     cur_vaddr = vseg->vbase;
     cur_paddr = vseg->pbase;
@@ -659,5 +654,5 @@
         }
 
-        // set vaddr/paddr
+        // set vaddr/paddr for current vobj
         vobj[vobj_id].vaddr = cur_vaddr;        
         vobj[vobj_id].paddr = cur_paddr;  
@@ -667,8 +662,8 @@
         cur_paddr += vobj[vobj_id].length; 
 
-        // initialise _ptabs[] and _max_pt2[]
+        // initialise _ptabs[] if current vobj is a PTAB
         if ( vobj[vobj_id].type == VOBJ_TYPE_PTAB )
         {
-            if(vobj[vobj_id].length < (PT1_SIZE + PT2_SIZE) ) // max_pt2 >= 1
+            if(vobj[vobj_id].length < (PT1_SIZE + PT2_SIZE*GIET_NB_PT2_MAX) ) 
             {
                 boot_puts( "\n[BOOT ERROR] in boot_vseg_map() function: " );
@@ -684,20 +679,17 @@
                 boot_exit();
             }
-            _ptabs[vspace_id]   = (page_table_t*)vobj[vobj_id].paddr;
-            _max_pt2[vspace_id] = (vobj[vobj_id].length - PT1_SIZE) / PT2_SIZE;
-        }
-    } 
+            _ptabs[vspace_id] = (page_table_t*)vobj[vobj_id].paddr;
+        }
+    } // end for vobjs
     
     //set the vseg length
-    unsigned int plength = pseg->length;
-    unsigned int vlength = cur_paddr - vseg->pbase;
-    vseg->length = align_to(vlength, 12);
+    vseg->length = align_to( (cur_paddr - vseg->pbase), 12);
 
     // checking pseg overflow
     if ( (vseg->pbase < pseg->base) || 
-         ((vseg->pbase + vlength) > (pseg->base + plength)) )
+         ((vseg->pbase + vseg->length) > (pseg->base + pseg->length)) )
     {
         boot_puts("\n[BOOT ERROR] in boot_vseg_map() function\n");
-        boot_puts("impossible identity mapping for virtual segment: ");
+        boot_puts("impossible mapping for virtual segment: ");
         boot_puts( vseg->name ); 
         boot_puts("\n"); 
@@ -717,18 +709,13 @@
     }
 
-    // computes number of pages
-    pages = vseg->length >> 12;
-    if ( (vseg->length & 0xFFF) != 0 ) pages++;
-
-    // set the next free physical address
-    if ( vseg->ident != 0 )
-            ;            // nothing to do
-    else                                
-        pseg->next_free_page = pseg->next_free_page + pages;
+    // set the next_base field in vseg
+    if ( vseg->ident == 0 )
+        pseg->next_base = vseg->pbase + vseg->length;
 
 #if BOOT_DEBUG_PT
-boot_puts("- vseg ");
 boot_puts( vseg->name );
-boot_puts(" : vbase = ");
+boot_puts(" : len = ");
+boot_putw( vseg->length );
+boot_puts(" / vbase = ");
 boot_putw( vseg->vbase );
 boot_puts(" / pbase = ");
@@ -816,5 +803,5 @@
     for ( pseg_id = 0 ; pseg_id < header->psegs ; pseg_id++ )
     {
-        pseg[pseg_id].next_free_page = 0;
+        pseg[pseg_id].next_base = pseg[pseg_id].base;
     }
 
@@ -860,6 +847,6 @@
 
 #if BOOT_DEBUG_PT
-boot_puts(">>> page table physical address = ");
-boot_putw((unsigned)_ptabs[vspace_id]);
+boot_puts("\n>>> page table physical address = ");
+boot_putw((unsigned int)_ptabs[vspace_id]);
 boot_puts("\n");
 #endif
