Index: trunk/kernel/mm/vseg.c
===================================================================
--- trunk/kernel/mm/vseg.c	(revision 690)
+++ trunk/kernel/mm/vseg.c	(revision 1)
@@ -2,5 +2,7 @@
  * vseg.c - virtual segment (vseg) related operations
  *
- * Authors   Alain Greiner (2016,2017,2018,2019,2020)
+ * Authors   Ghassan Almaless (2008,2009,2010,2011, 2012)
+ *           Mohamed Lamine Karaoui (2015)
+ *           Alain Greiner (2016)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -8,9 +10,9 @@
  * This file is part of ALMOS-MKH.
  *
- * ALMOS-MKH is free software; you can redistribute it and/or modify it
+ * ALMOS-MKH.is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
  * the Free Software Foundation; version 2.0 of the License.
  *
- * ALMOS-MKH is distributed in the hope that it will be useful, but
+ * ALMOS-MKH.is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
@@ -18,9 +20,9 @@
  *
  * You should have received a copy of the GNU General Public License
- * along with ALMOS-MKH; if not, write to the Free Software Foundation,
+ * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <hal_kernel_types.h>
+#include <hal_types.h>
 #include <hal_special.h>
 #include <hal_remote.h>
@@ -33,4 +35,5 @@
 #include <ppm.h>
 #include <mapper.h>
+#include <spinlock.h>
 #include <vfs.h>
 #include <page.h>
@@ -40,27 +43,32 @@
 
 ////////////////////////////////////////////////////////////////////////////////////////
-//   global variables for display / must be consistent with enum in "vseg.h"
+//   global variables for display / must be consistant with enum in "vseg.h"
 ////////////////////////////////////////////////////////////////////////////////////////
 
-
-//////////////////////////////////////////
-char * vseg_type_str( uint32_t vseg_type )
-{
-  	if     ( vseg_type == VSEG_TYPE_CODE   ) return "CODE";
-  	else if( vseg_type == VSEG_TYPE_DATA   ) return "DATA";
-  	else if( vseg_type == VSEG_TYPE_STACK  ) return "STAK";
-  	else if( vseg_type == VSEG_TYPE_ANON   ) return "ANON";
-  	else if( vseg_type == VSEG_TYPE_FILE   ) return "FILE";
-  	else if( vseg_type == VSEG_TYPE_REMOTE ) return "REMO";
-  	else if( vseg_type == VSEG_TYPE_KCODE  ) return "KCOD";
-  	else if( vseg_type == VSEG_TYPE_KDATA  ) return "KDAT";
-  	else if( vseg_type == VSEG_TYPE_KDEV   ) return "KDEV";
-    else                                     return "undefined";
-}
-
-///////////////////////////
-vseg_t * vseg_alloc( void )
-{
-    return (vseg_t*)kmem_alloc( bits_log2( sizeof(vseg_t)) , AF_ZERO );
+const char* vseg_type_name[VSEG_TYPES_NR] =
+{
+	"CODE",
+	"DATA",
+    "HEAP",
+    "STACK",
+    "ANON",
+    "FILE",
+    "REMOTE",
+    "KCODE",
+    "KDATA",
+    "KDEV",
+};
+
+
+/////////////////////
+vseg_t * vseg_alloc()
+{
+    kmem_req_t   req;
+
+    req.type  = KMEM_VSEG;
+	req.size  = sizeof(vseg_t);
+	req.flags = AF_KERNEL;
+
+    return (vseg_t *)kmem_alloc( &req );
 }
 
@@ -68,16 +76,37 @@
 void vseg_free( vseg_t * vseg )
 {
-	kmem_free( vseg , bits_log2( sizeof(vseg_t)) );
-}
-
-/////////////////////////////////////////
-void vseg_init_flags( vseg_t      * vseg,
-                      vseg_type_t   type )
-{
+    kmem_req_t  req;
+
+	req.type = KMEM_VSEG;
+	req.ptr  = vseg;
+	kmem_free( &req );
+}
+
+///////////////////////////////////
+void vseg_init( vseg_t      * vseg,
+	            intptr_t      base, 
+                intptr_t      size,
+                vpn_t         vpn_base,
+                vpn_t         vpn_size,
+		        uint32_t      type,
+                cxy_t         cxy,
+                fdid_t        fdid,
+		        uint32_t      offset )
+{
+    vseg->type      = type;
+	vseg->min       = base;
+	vseg->max       = base + size;
+    vseg->vpn_base  = vpn_base;
+	vseg->vpn_size  = vpn_size;
+	vseg->mapper    = XPTR_NULL;
+	vseg->fdid      = fdid;
+	vseg->offset    = offset;
+    vseg->cxy       = cxy;
+
     // set vseg flags depending on type
-	if( type == VSEG_TYPE_CODE )
-    {
-        vseg->flags = VSEG_USER    |
-                      VSEG_EXEC    |
+	if     ( type == VSEG_TYPE_CODE ) 
+    {
+        vseg->flags = VSEG_USER    |
+                      VSEG_EXEC    | 
                       VSEG_CACHE   |
                       VSEG_PRIVATE ;
@@ -88,102 +117,118 @@
                       VSEG_WRITE   |
                       VSEG_CACHE   |
+                      VSEG_PRIVATE ; 
+    }
+    else if( type == VSEG_TYPE_DATA ) 
+    {
+        vseg->flags = VSEG_USER    |
+                      VSEG_WRITE   |
+                      VSEG_CACHE   | 
+                      VSEG_DISTRIB ;
+    }
+    else if( type == VSEG_TYPE_HEAP ) 
+    {
+        vseg->flags = VSEG_USER    |
+                      VSEG_WRITE   |
+                      VSEG_CACHE   |
+                      VSEG_DISTRIB ;
+    }
+    else if( type == VSEG_TYPE_REMOTE ) 
+    {
+        vseg->flags = VSEG_USER    |
+                      VSEG_WRITE   | 
+                      VSEG_CACHE   ; 
+    }
+    else if( type == VSEG_TYPE_ANON ) 
+    {
+        vseg->flags = VSEG_USER    |
+                      VSEG_WRITE   |
+                      VSEG_CACHE   |
+                      VSEG_DISTRIB ;
+    }
+    else if( type == VSEG_TYPE_FILE ) 
+    {
+        vseg->flags = VSEG_USER    |
+                      VSEG_WRITE   |
+                      VSEG_CACHE   ;
+    }
+    else if( type == VSEG_TYPE_KCODE ) 
+    {
+        vseg->flags = VSEG_EXEC    |
+                      VSEG_CACHE   |
                       VSEG_PRIVATE ;
     }
-    else if( type == VSEG_TYPE_DATA )
-    {
-        vseg->flags = VSEG_USER    |
-                      VSEG_WRITE   |
-                      VSEG_CACHE   |
-                      VSEG_DISTRIB ;
-    }
-    else if( type == VSEG_TYPE_REMOTE )
-    {
-        vseg->flags = VSEG_USER    |
-                      VSEG_WRITE   |
-                      VSEG_CACHE   ;
-    }
-    else if( type == VSEG_TYPE_ANON )
-    {
-        vseg->flags = VSEG_USER    |
-                      VSEG_WRITE   |
-                      VSEG_CACHE; 
-    }
-    else if( type == VSEG_TYPE_FILE )
-    {
-        vseg->flags = VSEG_USER    |
-                      VSEG_WRITE   |
-                      VSEG_CACHE   ;
-    }
-    else if( type == VSEG_TYPE_KCODE )
-    {
-        vseg->flags = VSEG_EXEC    |
+    else if( type == VSEG_TYPE_KDATA ) 
+    {
+        vseg->flags = VSEG_WRITE   |
                       VSEG_CACHE   |
                       VSEG_PRIVATE ;
     }
-    else if( type == VSEG_TYPE_KDATA )
-    {
-        vseg->flags = VSEG_CACHE   |
-                      VSEG_WRITE   ;
-    }
-    else if( type == VSEG_TYPE_KDEV )
-    {
-        vseg->flags = VSEG_WRITE   ;
-    }
-    else
-    {
-	    assert( __FUNCTION__, false , "illegal vseg type\n" );
-    }
-
+    else 
+    {
+	    printk("\n[PANIC] in %s : illegal vseg type\n", __FUNCTION__);
+        hal_core_sleep();
+    }  
 }  // end vseg_init()
 
 //////////////////////////////////////////
 void vseg_init_from_ref( vseg_t    * vseg,
-                         xptr_t      ref_xp )
+                         xptr_t      ref )
 {
     // get remote vseg cluster and pointer
-    cxy_t    cxy = (cxy_t   )GET_CXY( ref_xp );
-    vseg_t * ptr = (vseg_t *)GET_PTR( ref_xp );
+    cxy_t    cxy = (cxy_t   )GET_CXY( ref );
+    vseg_t * ptr = (vseg_t *)GET_PTR( ref );
 
     // initialize vseg with remote_read access
-    vseg->type        =           hal_remote_l32( XPTR( cxy , &ptr->type        ) );
-    vseg->min         = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->min         ) );
-    vseg->max         = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->max         ) );
-    vseg->vpn_base    =           hal_remote_l32( XPTR( cxy , &ptr->vpn_base    ) );
-    vseg->vpn_size    =           hal_remote_l32( XPTR( cxy , &ptr->vpn_size    ) );
-    vseg->flags       =           hal_remote_l32( XPTR( cxy , &ptr->flags       ) );
-    vseg->file_offset =           hal_remote_l32( XPTR( cxy , &ptr->file_offset ) );
-    vseg->file_size   =           hal_remote_l32( XPTR( cxy , &ptr->file_size   ) );
-	vseg->mapper_xp   = (xptr_t)  hal_remote_l64( XPTR( cxy , &ptr->mapper_xp   ) );
-
-    switch (vseg->type)
-    {
-        case VSEG_TYPE_DATA:      // unused 
-        {
-            vseg->cxy = 0xffff;
-            break;
-        }
-        case VSEG_TYPE_CODE:      // always local
-        case VSEG_TYPE_STACK: 
-        case VSEG_TYPE_KCODE:
-        {
-            vseg->cxy = local_cxy;
-            break;
-        }
-        case VSEG_TYPE_ANON:      // intrinsic
-        case VSEG_TYPE_FILE:
-        case VSEG_TYPE_REMOTE: 
-        case VSEG_TYPE_KDEV:
-        case VSEG_TYPE_KDATA:
-        {
-            vseg->cxy = (cxy_t) hal_remote_l32( XPTR(cxy, &ptr->cxy) );
-            break;
-        }
-        default: 
-        {
-            assert( __FUNCTION__, false, "Illegal vseg type" );
-            break;
-        }
-    }
-}
-
-
+    vseg->type     =           hal_remote_lw ( XPTR( cxy , &ptr->type     ) );
+    vseg->min      = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->min      ) );
+    vseg->max      = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->max      ) );
+    vseg->vpn_base =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_base ) );
+    vseg->vpn_size =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_size ) );
+    vseg->flags    =           hal_remote_lw ( XPTR( cxy , &ptr->flags    ) );
+	vseg->mapper   = (xptr_t)  hal_remote_lwd( XPTR( cxy , &ptr->mapper   ) );
+
+    if( vseg->type == VSEG_TYPE_FILE )
+    {
+        vseg->fdid   = hal_remote_lw( XPTR( cxy , &ptr->fdid   ) );
+        vseg->offset = hal_remote_lw( XPTR( cxy , &ptr->offset ) );
+    }
+    else
+    {
+        vseg->fdid   = 0;
+        vseg->offset = 0;
+    }
+} // end vseg_init_from_ref()
+
+
+///////////////////////////////
+error_t vseg_attach( vmm_t  * vmm,
+                     vseg_t * vseg )
+{
+    // add vseg in radix-tree
+    error_t error = grdxt_insert( &vmm->grdxt , vseg->vpn_base , vseg );
+    if ( error ) return ENOMEM;
+
+    // update vseg descriptor
+    vseg->vmm = vmm;
+
+    // add vseg in vmm list
+    list_add_last( &vmm->vsegs_root , &vseg->list );
+
+    return 0;
+}
+
+///////////////////////////////
+void vseg_detach( vmm_t  * vmm,
+                  vseg_t * vseg )
+{
+    // remove vseg from radix-tree
+    grdxt_remove( &vmm->grdxt , vseg->vpn_base );
+
+    // update vseg descriptor
+    vseg->vmm = NULL;
+
+    // remove vseg from vmm list
+    list_unlink( &vseg->list );
+}
+
+
