Index: trunk/kernel/syscalls/sys_barrier.c
===================================================================
--- trunk/kernel/syscalls/sys_barrier.c	(revision 1)
+++ trunk/kernel/syscalls/sys_barrier.c	(revision 23)
@@ -1,15 +1,16 @@
 /*
- * kern/sys_barrier.c - barrier service interface for userland
+ * sys_barrier.c - Access a POSIX barrier.
  * 
- * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
- * Copyright (c) 2011,2012 UPMC Sorbonne Universites
+ * authors       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,96 +18,100 @@
  *
  * 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 <types.h>
+#include <hal_types.h>
+#include <hal_special.h>
 #include <errno.h>
 #include <thread.h>
-#include <task.h>
-#include <kmem.h>
+#include <printk.h>
 #include <vmm.h>
-#include <kmagics.h>
-#include <barrier.h>
+#include <syscalls.h>
+#include <remote_barrier.h>
 
-int sys_barrier(struct barrier_s **barrier, uint_t operation, uint_t count)
+//////////////////////////////////
+int sys_barrier( void     * vaddr,
+                 uint32_t   operation,
+                 uint32_t   count )
 {
-	kmem_req_t req;
-	struct barrier_s *ibarrier;
-	error_t err = EINVAL;
+	error_t      error;
+    paddr_t      paddr;
   
-	if((err = vmm_check_address("usr barrier ptr", 
-				    current_task, 
-				    barrier,
-				    sizeof(struct barrier_s*))))
-		goto SYS_BARRIER_END;
+    thread_t   * this = CURRENT_THREAD;
 
-	if((err = cpu_copy_from_uspace(&ibarrier, barrier, sizeof(struct barrier_s *))))
-		goto SYS_BARRIER_END;
+    // check vaddr in user vspace
+	error = vmm_v2p_translate( false , vaddr , &paddr );
+	if( error )
+    {
+        printk("\n[ERROR] in %s : illegal barrier virtual address = %x\n",
+               __FUNCTION__ , (intptr_t)vaddr );
+        this->errno = error;
+        return -1;
+    }
 
-	switch(operation)
+    // execute requested operation
+	switch( operation )
 	{
-	case BARRIER_INIT_PRIVATE:
-	case BARRIER_INIT_SHARED:
-		req.type  = KMEM_BARRIER;
-		req.size  = sizeof(*ibarrier);
-		req.flags = AF_USER;
+        //////////////////
+	    case BARRIER_INIT:
+        {
+            error = remote_barrier_create( (intptr_t)vaddr , count );
+    
+		    if( error )
+            {
+                printk("\n[ERROR] in %s : cannot create barrier = %x\n",
+                       __FUNCTION__ , (intptr_t)vaddr );
+                this->errno = error;
+                return -1;
+            }
+			break;
+        }
+        //////////////////
+	    case BARRIER_WAIT:
+        {
+            xptr_t barrier_xp = remote_barrier_from_ident( (intptr_t)vaddr );
 
-		if((ibarrier = kmem_alloc(&req)) == NULL)
-		{
-			err = ENOMEM;
-			break;
-		}
-    
-		if((err = barrier_init(ibarrier, count, operation)))
-			break;
-    
-		if((err = cpu_copy_to_uspace(barrier, &ibarrier, sizeof(struct barrier_s *))))
-		{
-			req.ptr = ibarrier;
-			kmem_free(&req);
-		}
-		break;
+            if( barrier_xp == XPTR_NULL )     // user error
+            {
+                printk("\n[ERROR] in %s : barrier %x not registered\n",
+                       __FUNCTION__ , (intptr_t)vaddr );
+                this->errno = EINVAL;
+                return -1;
+            }
+            else                          // success
+            {
+                remote_barrier_wait( barrier_xp ); 
+            }
+            break;
+        }
+        /////////////////////
+	    case BARRIER_DESTROY:
+        {
+            xptr_t barrier_xp = remote_barrier_from_ident( (intptr_t)vaddr );
 
-	case BARRIER_WAIT:
+            if( barrier_xp == XPTR_NULL )     // user error
+            {
+                printk("\n[ERROR] in %s : barrier %x not registered\n",
+                       __FUNCTION__ , (intptr_t)vaddr );
+                this->errno = EINVAL;
+                return -1;
+            }
+            else                          // success
+            {
+                remote_barrier_destroy( barrier_xp ); 
+            }
+            break;
+        }
+        ////////
+	    default:
+        {
+            printk("\n[PANIC] in %s : illegal operation type\n", __FUNCTION__ );
+            hal_core_sleep();
+        }
+	}  // end switch
 
-		if((err = vmm_check_object(ibarrier, struct barrier_s, BARRIER_ID)))
-			break;
+	return 0;
 
-		err = barrier_wait(ibarrier);
-		break;
+}  // end sys_barrier()
 
-	case BARRIER_DESTROY:
-    
-		if((err = vmm_check_object(ibarrier, struct barrier_s, BARRIER_ID)))
-			break;
-    
-		if((err = barrier_destroy(ibarrier)))
-			break;
-   
-		req.type = KMEM_BARRIER;
-		req.ptr  = ibarrier;
-		kmem_free(&req);
-		return 0;
-
-	default:
-		err = EINVAL;
-	}
-
-SYS_BARRIER_END:
-	current_thread->info.errno = err;
-	return err;
-}
-
-KMEM_OBJATTR_INIT(barrier_kmem_init)
-{
-	attr->type   = KMEM_BARRIER;
-	attr->name   = "KCM Barrier";
-	attr->size   = sizeof(struct barrier_s);
-	attr->aligne = 0;
-	attr->min    = CONFIG_BARRIER_MIN;
-	attr->max    = CONFIG_BARRIER_MAX;
-	attr->ctor   = NULL;
-	attr->dtor   = NULL;
-	return 0;
-}
