Index: trunk/kernel/syscalls/sys_mmap.c
===================================================================
--- trunk/kernel/syscalls/sys_mmap.c	(revision 566)
+++ trunk/kernel/syscalls/sys_mmap.c	(revision 594)
@@ -51,22 +51,24 @@
 	process_t   * process = this->process;
 
+#if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS)
+uint64_t     tm_start = hal_get_cycles();
+#endif
+
 #if DEBUG_SYS_MMAP
-uint64_t      tm_start;
-uint64_t      tm_end;
 tm_start = hal_get_cycles();
 if ( DEBUG_SYS_MMAP < tm_start )
-printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
-__FUNCTION__, this, process->pid, (uint32_t)tm_start );
-#endif
-
-    // check arguments in user space
+printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
+__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_start );
+#endif
+
+    // check user buffer (containing attributes) is mapped 
     error = vmm_get_vseg( process , (intptr_t)attr , &vseg );
 
-    if ( error )
-    {
-
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : user buffer unmapped %x / thread %x / process %x\n",
-__FUNCTION__ , (intptr_t)attr , this->trdid , process->pid );
+    if( error )
+    {
+
+#if DEBUG_SYSCALLS_ERROR
+printk("\n[ERROR] in %s : thread[%x,%x] / mmap attributes unmapped %x\n",
+__FUNCTION__ , process->pid, this->trdid, (intptr_t)attr );
 vmm_display( process , false );
 #endif
@@ -75,11 +77,11 @@
     }
 
-    // copy arguments from uspace
+    // copy attributes from user space to kernel space
     hal_copy_from_uspace( &k_attr , attr , sizeof(mmap_attr_t) );
 
-    // get fdid, offset, and length arguments
-    uint32_t fdid   = k_attr.fdid;
-    uint32_t offset = k_attr.offset;
-    uint32_t length = k_attr.length;
+    // get addr, fdid, offset, and length attributes
+    uint32_t  fdid   = k_attr.fdid;
+    uint32_t  offset = k_attr.offset;
+    uint32_t  length = k_attr.length;
 
     // get flags
@@ -95,6 +97,6 @@
 
 #if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : MAP_FIXED not supported / thread %x / process %x\n",
-__FUNCTION__ , this->trdid , process->pid );
+printk("\n[ERROR] in %s : thread[%x,%x] / MAP_FIXED not supported\n",
+__FUNCTION__ , process->pid, this->trdid );
 #endif
         this->errno = EINVAL;
@@ -106,6 +108,6 @@
 
 #if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : MAP_SHARED == MAP_PRIVATE / thread %x / process %x\n",
-__FUNCTION__ , this->trdid , process->pid );
+printk("\n[ERROR] in %s : thread[%x,%x] / MAP_SHARED == MAP_PRIVATE\n",
+__FUNCTION__ , process->pid, this->trdid );
 #endif
         this->errno = EINVAL;
@@ -115,12 +117,15 @@
     // FIXME handle Copy_On_Write for MAP_PRIVATE...
 
-    // get access rigths
-    bool_t     prot_read   = ( (k_attr.prot & PROT_READ )   != 0 );
-    bool_t     prot_write  = ( (k_attr.prot & PROT_WRITE)   != 0 );
-
     // test mmap type : can be FILE / ANON / REMOTE
 
     if( (map_anon == false) && (map_remote == false) )   // FILE
     {
+
+#if (DEBUG_SYS_MMAP & 1)
+if ( DEBUG_SYS_MMAP < tm_start )
+printk("\n[%s] thread[%x,%x] map file : fdid %d / offset %d / %d bytes\n",
+__FUNCTION__, process->pid, this->trdid, fdid, offset, length );
+#endif
+
 	    // FIXME: handle concurent delete of file by another thread closing it 
 
@@ -129,6 +134,6 @@
 
 #if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s: bad file descriptor %d / thread %x / process %x\n",
-__FUNCTION__ , fdid , this->trdid , process->pid );
+printk("\n[ERROR] in %s : thread[%x,%x] / bad file descriptor %d\n",
+__FUNCTION__ , process->pid , this->trdid , fdid );
 #endif
             this->errno = EBADFD;
@@ -143,6 +148,6 @@
 
 #if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s: file %d not found / thread %x / process %x\n",
-__FUNCTION__ , fdid , this->trdid , process->pid );
+printk("\n[ERROR] in %s : thread[%x,%x] / file descriptor %d not found\n",
+__FUNCTION__  , this->trdid , process->pid , fdid );
 #endif
             this->errno = EBADFD;
@@ -154,11 +159,22 @@
         vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
 
-        // get inode pointer, mapper pointer and file attributes
+#if (DEBUG_SYS_MMAP & 1)
+if ( DEBUG_SYS_MMAP < tm_start )
+printk("\n[%s] thread[%x,%x] get file pointer %x in cluster %x\n",
+__FUNCTION__, process->pid, this->trdid, file_ptr, file_cxy );
+#endif
+
+        // get inode pointer & mapper pointer 
         vfs_inode_t * inode_ptr  = hal_remote_lpt(XPTR(file_cxy , &file_ptr->inode ));
-        uint32_t      file_attr  = hal_remote_l32 (XPTR(file_cxy , &file_ptr->attr  ));
         mapper_t    * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper));
 
         // get file size
 		uint32_t size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) );
+
+#if (DEBUG_SYS_MMAP & 1)
+if ( DEBUG_SYS_MMAP < tm_start )
+printk("\n[%s] thread[%x,%x] get file size : %d bytes\n",
+__FUNCTION__, process->pid, this->trdid, size );
+#endif
 
         // chek offset and length arguments
@@ -167,10 +183,16 @@
 
 #if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s: offset(%d) + len(%d) >= file's size(%d) / thread %x / process %x\n", 
-__FUNCTION__, k_attr.offset, k_attr.length, size, this->trdid, process->pid );
+printk("\n[ERROR] in %s: thread[%x,%x] / offset(%d) + len(%d) >= file's size(%d)\n", 
+__FUNCTION__, process->pid, this->trdid, k_attr.offset, k_attr.length, size );
 #endif
             this->errno = ERANGE;
             return -1;
 		}
+
+/* TODO
+        // chek access rigths
+        uint32_t   file_attr  = hal_remote_l32(XPTR(file_cxy , &file_ptr->attr  ));
+        bool_t     prot_read  = ( (k_attr.prot & PROT_READ )   != 0 );
+        bool_t     prot_write = ( (k_attr.prot & PROT_WRITE)   != 0 );
 
         // check access rights
@@ -186,4 +208,5 @@
 			return -1;
 		}
+*/
 
 		// increment file refcount
@@ -194,29 +217,38 @@
         vseg_cxy  = file_cxy;
     }
-    else                                                // ANON or REMOTE
-    {
-        // no mapper for ANON or REMOTE
+    else if ( map_anon )                                 // MAP_ANON
+    {
         mapper_xp = XPTR_NULL;
-
-        if( map_anon )
+        vseg_type = VSEG_TYPE_ANON;
+        vseg_cxy  = local_cxy;
+
+#if (DEBUG_SYS_MMAP & 1)
+if ( DEBUG_SYS_MMAP < tm_start )
+printk("\n[%s] thread[%x,%x] map anon / %d bytes / cluster %x\n",
+__FUNCTION__, process->pid, this->trdid, length, vseg_cxy );
+#endif
+
+    } 
+    else                                                 // MAP_REMOTE
+    {
+        mapper_xp = XPTR_NULL;
+        vseg_type = VSEG_TYPE_REMOTE;
+        vseg_cxy  = k_attr.fdid;
+
+#if (DEBUG_SYS_MMAP & 1)
+if ( DEBUG_SYS_MMAP < tm_start )
+printk("\n[%s] thread[%x,%x] map remote / %d bytes / cluster %x\n",
+__FUNCTION__, process->pid, this->trdid, length, vseg_cxy );
+#endif
+ 
+        if( cluster_is_undefined( vseg_cxy ) )
         {
-            vseg_type = VSEG_TYPE_ANON;
-            vseg_cxy  = local_cxy;
-        }
-        else
-        {
-            vseg_type = VSEG_TYPE_REMOTE;
-            vseg_cxy  = k_attr.fdid;
- 
-            if( cluster_is_undefined( vseg_cxy ) )
-            {
-
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : illegal cxy for MAP_REMOTE / thread %x / process %x\n",
-__FUNCTION__, this->trdid , process->pid );
-#endif
-                this->errno = EINVAL;
-                return -1;
-            }
+
+#if DEBUG_SYSCALLS_ERROR
+printk("\n[ERROR] in %s : thread[%x,%x] / illegal cxy %x for REMOTE\n",
+__FUNCTION__, this->trdid , process->pid, vseg_cxy );
+#endif
+            this->errno = EINVAL;
+            return -1;
         }
     }
@@ -228,5 +260,5 @@
     xptr_t      ref_xp  = process->ref_xp;
     cxy_t       ref_cxy = GET_CXY( ref_xp );
-    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
+    process_t * ref_ptr = GET_PTR( ref_xp );
 
     // create the vseg in reference cluster
@@ -235,8 +267,8 @@
         vseg = vmm_create_vseg( process,
                                 vseg_type,
-                                0,               // base address unused for mmap()
-                                length,
-                                offset,
-                                0,               // file_size unused for mmap()
+                                0,               // vseg base (unused for mmap)
+                                length,          // vseg size
+                                offset,          // file offset
+                                0,               // file_size (unused for mmap)
                                 mapper_xp,
                                 vseg_cxy );
@@ -247,8 +279,8 @@
                                     ref_ptr,
                                     vseg_type,
-                                    0,            // base address unused for mmap()
-                                    length,
-                                    offset,
-                                    0,            // file size unused for mmap()
+                                    0,            // vseg base (unused for mmap)
+                                    length,       // vseg size
+                                    offset,       // file offset
+                                    0,            // file size (unused for mmap)
                                     mapper_xp,
                                     vseg_cxy,
@@ -263,6 +295,6 @@
 
 #if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : cannot create vseg / thread %x / process %x\n",
-__FUNCTION__, this->trdid , process->pid );
+printk("\n[ERROR] in %s : thread[%x,%x] / cannot create vseg\n",
+__FUNCTION__, process->pid, this->trdid );
 #endif
         this->errno = ENOMEM;
@@ -275,11 +307,13 @@
     hal_fence();
 
+#if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS)
+uint64_t     tm_end = hal_get_cycles();
+#endif
+
 #if DEBUG_SYS_MMAP
-tm_end = hal_get_cycles();
-if ( DEBUG_SYS_MMAP < tm_start )
-printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n"
-"vseg %s / cluster %x / base %x / size %x / cost %d\n", 
-__FUNCTION__, this, process->pid, (uint32_t)tm_end,
-vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)(tm_end - tm_start) );
+if ( DEBUG_SYS_MMAP < tm_start )
+printk("\n[%s] thread[%x,%x] exit / %s / cxy %x / base %x / size %d / cycle %d\n",
+__FUNCTION__, process->pid, this->trdid,
+vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)tm_end );
 #endif
 
