Index: trunk/kernel/syscalls/sys_read.c
===================================================================
--- trunk/kernel/syscalls/sys_read.c	(revision 1)
+++ trunk/kernel/syscalls/sys_read.c	(revision 23)
@@ -1,15 +1,16 @@
 /*
- * kern/sys_read.c - read bytes from an opened file
+ * sys_read.c - read bytes from a file
  * 
- * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
- * Copyright (c) 2011,2012 UPMC Sorbonne Universites
+ * Author     Alain Greiner (2016,2017)
  *
- * This file is part of ALMOS-kernel.
+ * Copyright (c) UPMC Sorbonne Universites
  *
- * ALMOS-kernel is free software; you can redistribute it and/or modify it
+ * This file is part of ALMOS-MKH.
+ *
+ * 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-kernel 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
@@ -17,40 +18,116 @@
  *
  * You should have received a copy of the GNU General Public License
- * along with ALMOS-kernel; 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 <kernel_config.h>
+#include <hal_types.h>
+#include <hal_uspace.h>
+#include <hal_special.h>
 #include <errno.h>
+#include <vfs.h>
+#include <vmm.h>
 #include <thread.h>
-#include <vfs.h>
-#include <sys-vfs.h>
-#include <task.h>
+#include <printk.h>
+#include <process.h>
 
-int sys_read (uint_t fd, void *buf, size_t count)
+/* TODO: user page(s) need to be locked  [AG] */
+
+/////////////////////////////////
+int sys_read( uint32_t   file_id,
+              void     * buf,
+              uint32_t   count )
 {
-	struct ku_obj kub;
-	ssize_t err;
-	struct thread_s *this;
-	struct task_s *task;
-	struct vfs_file_s *file;
+    error_t      error;
+    paddr_t      paddr;
+    char         kbuf[CONFIG_VFS_KBUF_SIZE];
 
-	file = NULL;
-	this = current_thread;
-	task = current_task;
+	xptr_t       file_xp;     // remote file extended pointer
+    uint32_t     nbytes;      // number of bytes in one iteration
 
-	if((fd >= CONFIG_TASK_FILE_MAX_NR) || (task_fd_lookup(task, fd, &file)))
+	thread_t  *  this    = CURRENT_THREAD;
+	process_t *  process = this->process;
+ 
+    // check file_id argument
+	if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
 	{
-		this->info.errno = EBADFD;
+        printk("\n[ERROR] in %s : illegal file descriptor index = %d\n",
+               __FUNCTION__ , file_id );
+		this->errno = EBADFD;
 		return -1;
 	}
 
-	KU_SLICE_BUFF(kub, buf, count);
-	if((err = vfs_read(file, &kub)) < 0)
+    // check user buffer in user space
+    error = vmm_v2p_translate( false , buf , &paddr );
+
+    if ( error )
+    {
+        printk("\n[ERROR] in %s : user buffer unmapped = %x\n",
+               __FUNCTION__ , (intptr_t)buf );
+		this->errno = EINVAL;
+		return -1;
+    }
+
+    // get extended pointer on remote file descriptor
+    file_xp = process_fd_get_xptr( process , file_id );
+
+    if( file_xp == XPTR_NULL )
+    {
+        printk("\n[ERROR] in %s : undefined file descriptor index = %d\n",
+               __FUNCTION__ , file_id );
+        this->errno = EBADFD;
+        return -1;
+    }
+
+    // get file descriptor cluster and local pointer
+    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
+    cxy_t        file_cxy = GET_CXY( file_xp );
+
+    // check file readable
+    uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
+    if( (attr & FD_ATTR_READ_ENABLE) == 0 )
 	{
-		this->info.errno = -err;
-		printk(INFO, "INFO: sys_read: Error %d\n", this->info.errno);
+        printk("\n[ERROR] in %s : file %d not readable\n",
+               __FUNCTION__ , file_id );
+		this->errno = EBADFD;
 		return -1;
 	}
    
-	return err;
-}
+    // transfer at most CONFIG_VFS_KBUF_SIZE bytes per iteration
+    while( count )
+    {
+        if( count <= CONFIG_VFS_KBUF_SIZE ) 
+        {
+            nbytes = count;
+            count  = 0;
+        }
+        else
+        {
+            nbytes = CONFIG_VFS_KBUF_SIZE;
+            count  = count - CONFIG_VFS_KBUF_SIZE;
+        }
+
+        // transfer nbytes to kernel buffer
+        error = vfs_move( true,               // read => (to_buffer = true)
+                          file_xp , 
+                          kbuf , 
+                          nbytes );
+
+        if( error )
+        {
+            printk("\n[ERROR] in %s cannot read data from file %d\n",
+                   __FUNCTION__ , file_id );
+            this->errno = error;
+            return -1;
+        }
+
+        // copy kernel buffer to user space
+        hal_copy_to_uspace( buf , kbuf , nbytes );
+    }
+
+    hal_wbflush();
+
+	return 0;
+
+}  // end sys_read()
