Index: trunk/kernel/libk/user_dir.c
===================================================================
--- trunk/kernel/libk/user_dir.c	(revision 671)
+++ trunk/kernel/libk/user_dir.c	(revision 683)
@@ -2,5 +2,5 @@
  * user_dir.c - kernel DIR related operations implementation.
  * 
- * Authors   Alain   Greiner (2016,2017,2018,2019)
+ * Authors   Alain   Greiner (2016,2017,2018,2019,2020)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -105,8 +105,12 @@
     list_entry_t    root;              // root of temporary list of allocated pages
     uint32_t        page_id;           // page index in list of physical pages
-    kmem_req_t      req;               // kmem request descriptor
     ppn_t           fake_ppn;          // unused, but required by hal_gptlock_pte()
     uint32_t        fake_attr;         // unused, but required by hal_gptlock_pte()
     error_t         error;
+
+#if DEBUG_USER_DIR_CREATE || DEBUG_USER_DIR_ERROR
+uint32_t   cycle = (uint32_t)hal_get_cycles();
+thread_t * this  = CURRENT_THREAD;
+#endif
 
     // get cluster, local pointer, and pid of reference process
@@ -115,8 +119,6 @@
     ref_pid = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->pid ) );
 
-#if DEBUG_USER_DIR
-uint32_t cycle = (uint32_t)hal_get_cycles();
-thread_t * this = CURRENT_THREAD;
-if( cycle > DEBUG_USER_DIR )
+#if DEBUG_USER_DIR_CREATE
+if( DEBUG_USER_DIR_CREATE < cycle )
 printk("\n[%s] thread[%x,%x] enter for inode (%x,%x) and process %x / cycle %d\n",
 __FUNCTION__, this->process->pid, this->trdid, local_cxy, inode, ref_pid, cycle );
@@ -133,18 +135,18 @@
 
     // allocate memory for a local user_dir descriptor
-    req.type  = KMEM_KCM;
-    req.order = bits_log2( sizeof(user_dir_t) );
-    req.flags = AF_ZERO | AF_KERNEL;
-    dir       = kmem_alloc( &req );
+    dir = kmem_alloc( bits_log2(sizeof(user_dir_t)) , AF_ZERO );
 
     if( dir == NULL )
     {
-        printk("\n[ERROR] in %s : cannot allocate user_dir_t in cluster %x\n",
-        __FUNCTION__, local_cxy );
+
+#if DEBUG_USER_DIR_ERROR
+printk("\n[ERROR] in %s : thread[%x,%x] cannot allocate user_dir_t in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
+#endif
         return NULL;
     }
 
-    // Build and initialize the dirent array as a list of pages.
-    // For each iteration in this while loop:
+    // First loop to build and initialize the dirent array
+    // as a temporary list of pages. For each iteration :
     // - allocate one physical 4 Kbytes (64 dirent slots)
     // - call the relevant FS specific function to scan the directory mapper,
@@ -162,13 +164,13 @@
     {
         // allocate one physical page
-        req.type  = KMEM_PPM;
-        req.order = 0;
-        req.flags = AF_ZERO;
-        base      = kmem_alloc( &req );
+        base = kmem_alloc( CONFIG_PPM_PAGE_ORDER , AF_ZERO );
 
         if( base == NULL )
         {
-            printk("\n[ERROR] in %s : cannot allocate page in cluster %x\n",
-            __FUNCTION__, ref_cxy );
+
+#if DEBUG_USER_DIR_ERROR
+printk("\n[ERROR] in %s : thread[%x,%x] cannot allocate page in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
+#endif
             goto user_dir_create_failure;
         }
@@ -184,6 +186,9 @@
         if( error )
         {
-            printk("\n[ERROR] in %s : cannot initialise dirent array in cluster %x\n",
-            __FUNCTION__, ref_cxy );
+
+#if DEBUG_USER_DIR_ERROR
+printk("\n[ERROR] in %s : thread[%x,%x] cannot initialize dirent array in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
+#endif
             goto user_dir_create_failure;
         }
@@ -204,6 +209,6 @@
     } // end while
         
-#if DEBUG_USER_DIR
-if( cycle > DEBUG_USER_DIR )
+#if DEBUG_USER_DIR_CREATE
+if( DEBUG_USER_DIR_CREATE < cycle )
 printk("\n[%s] thread[%x,%x] initialised dirent array / %d entries\n",
 __FUNCTION__, this->process->pid, this->trdid, total_dirents, cycle );
@@ -241,11 +246,14 @@
     if( vseg == NULL )
     {
-        printk("\n[ERROR] in %s : cannot create vseg for user_dir in cluster %x\n",
-        __FUNCTION__, ref_cxy);
+
+#if DEBUG_USER_DIR_ERROR
+printk("\n[ERROR] in %s : thread[%x,%x] cannot create vseg in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
+#endif
         goto user_dir_create_failure;
     }
 
-#if DEBUG_USER_DIR
-if( cycle > DEBUG_USER_DIR )
+#if DEBUG_USER_DIR_CREATE
+if( DEBUG_USER_DIR_CREATE < cycle )
 printk("\n[%s] thread[%x,%x] allocated vseg ANON / base %x / size %x\n",
 __FUNCTION__, this->process->pid, this->trdid, vseg->min, vseg->max - vseg->min );
@@ -269,7 +277,11 @@
     vpn_base = hal_remote_l32( XPTR( ref_cxy , &vseg->vpn_base ) );
 
-    // scan the list of allocated physical pages to map
+    // Second loop on the allocated physical pages to map
     // all physical pages in the reference process GPT
+    // The pages are mapped in the user process GPT, but
+    // are removed from the temporary list
+
     page_id = 0;
+
     while( list_is_empty( &root ) == false )
     {
@@ -290,7 +302,9 @@
         if( error )
         {
-            printk("\n[ERROR] in %s : cannot map vpn %x in GPT\n",
-            __FUNCTION__, vpn );
-
+
+#if DEBUG_USER_DIR_ERROR
+printk("\n[ERROR] in %s : thread[%x,%x] cannot map vpn %x in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy, cycle );
+#endif
             // delete the vseg
             intptr_t base = (intptr_t)hal_remote_lpt( XPTR( ref_cxy , &vseg->min ) );
@@ -298,7 +312,5 @@
          
             // release the user_dir descriptor
-            req.type = KMEM_KCM;
-            req.ptr  = dir;
-            kmem_free( &req );
+            kmem_free( dir , bits_log2(sizeof(user_dir_t)) );
             return NULL;
         }
@@ -310,6 +322,6 @@
                          ppn );
 
-#if DEBUG_USER_DIR 
-if( cycle > DEBUG_USER_DIR )
+#if DEBUG_USER_DIR_CREATE
+if( DEBUG_USER_DIR_CREATE < cycle )
 printk("\n[%s] thread[%x,%x] mapped vpn %x to ppn %x\n",
 __FUNCTION__, this->process->pid, this->trdid, vpn + page_id, ppn );
@@ -329,5 +341,5 @@
     dir->current = 0;
     dir->entries = total_dirents;
-    dir->ident   = (intptr_t)(vpn_base << CONFIG_PPM_PAGE_SHIFT);
+    dir->ident   = (intptr_t)(vpn_base << CONFIG_PPM_PAGE_ORDER);
 
     // build extended pointers on root and lock of user_dir xlist in ref process
@@ -347,7 +359,6 @@
     remote_queuelock_release( lock_xp );
 
-#if DEBUG_USER_DIR
-cycle = (uint32_t)hal_get_cycles();
-if( cycle > DEBUG_USER_DIR )
+#if DEBUG_USER_DIR_CREATE
+if( DEBUG_USER_DIR_CREATE < cycle )
 printk("\n[%s] thread[%x,%x] created user_dir (%x,%x) / %d entries / cycle %d\n",
 __FUNCTION__, this->process->pid, this->trdid, local_cxy, dir, total_dirents, cycle );
@@ -358,12 +369,11 @@
 user_dir_create_failure:
 
-    // release local user_dir_t structure
-    req.type = KMEM_KCM;
-    req.ptr  = dir;
-    kmem_free( &req );
-
-    // release local physical pages
+    // release user_dir_t structure
+    kmem_free( dir , bits_log2(sizeof(user_dir_t)) );
+
+    // release physical pages
     while( list_is_empty( &root ) == false )
     {
+        // get page descriptor
         page = LIST_FIRST( &root , page_t , list );
 
@@ -371,7 +381,6 @@
         base = GET_PTR( ppm_page2base( XPTR( local_cxy , page ) ) );
  
-        req.type  = KMEM_PPM;
-        req.ptr   = base;
-        kmem_free( &req );
+        // release the page
+        kmem_free( base , CONFIG_PPM_PAGE_ORDER );
     }
 
@@ -402,4 +411,8 @@
     cluster = LOCAL_CLUSTER;
 
+#if DEBUG_USER_DIR_DESTROY 
+uint32_t cycle = (uint32_t)hal_get_cycles();
+#endif
+
     // get cluster, local pointer, and PID of reference user process
     ref_cxy = GET_CXY( ref_xp );
@@ -407,7 +420,6 @@
     ref_pid = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->pid ) );
 
-#if DEBUG_USER_DIR
-uint32_t cycle = (uint32_t)hal_get_cycles();
-if( cycle > DEBUG_USER_DIR )
+#if DEBUG_USER_DIR_DESTROY
+if( DEBUG_USER_DIR_DESTROY < cycle )
 printk("\n[%s] thread[%x,%x] enter for user_dir (%x,%x) and process %x / cycle %d\n",
 __FUNCTION__, this->process->pid, this->trdid, local_cxy, dir, ref_pid, cycle );
@@ -475,7 +487,6 @@
         hal_atomic_add( &responses , 1 );
 
-#if (DEBUG_USER_DIR & 1)
-uint32_t cycle = (uint32_t)hal_get_cycles();
-if( cycle > DEBUG_USER_DIR )
+#if (DEBUG_USER_DIR_DESTROY & 1)
+if(  DEBUG_USER_DIR_DESTROY < cycle )
 printk("\n[%s] thread[%x,%x] register RPC request in cluster %x\n",
 __FUNCTION__, this->process->pid, this->trdid, process_cxy );
@@ -496,12 +507,9 @@
 
     // release local user_dir_t structure
-    kmem_req_t  req;
-    req.type = KMEM_KCM;
-    req.ptr  = dir;
-    kmem_free( &req );
-
-#if DEBUG_USER_DIR
+    kmem_free( dir , bits_log2(sizeof(user_dir_t)) );
+
+#if DEBUG_USER_DIR_DESTROY
 cycle = (uint32_t)hal_get_cycles();
-if( cycle > DEBUG_USER_DIR )
+if( DEBUG_USER_DIR_DESTROY < cycle )
 printk("\n[%s] thread[%x,%x] deleted user_dir (%x,%x) / cycle %d\n",
 __FUNCTION__, this->process->pid, this->trdid, local_cxy, dir, cycle );
