Index: trunk/kernel/syscalls/sys_barrier.c
===================================================================
--- trunk/kernel/syscalls/sys_barrier.c	(revision 690)
+++ trunk/kernel/syscalls/sys_barrier.c	(revision 1)
@@ -1,16 +1,15 @@
 /*
- * sys_barrier.c - Access a POSIX barrier.
+ * kern/sys_barrier.c - barrier service interface for userland
  * 
- * authors       Alain Greiner (2016,2017,2018,2019,2020)
+ * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
+ * Copyright (c) 2011,2012 UPMC Sorbonne Universites
  *
- * Copyright (c) UPMC Sorbonne Universites
+ * This file is part of ALMOS-kernel.
  *
- * This file is part of ALMOS-MKH.
- *
- * ALMOS-MKH is free software; you can redistribute it and/or modify it
+ * ALMOS-kernel 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-kernel 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,199 +17,96 @@
  *
  * 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-kernel; 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_special.h>
-#include <hal_uspace.h>
-#include <hal_vmm.h>
+#include <types.h>
 #include <errno.h>
 #include <thread.h>
-#include <printk.h>
+#include <task.h>
+#include <kmem.h>
 #include <vmm.h>
-#include <syscalls.h>
-#include <remote_barrier.h>
+#include <kmagics.h>
+#include <barrier.h>
 
-/////////////////////////////////////////////////////////////////////////////////
-// This function returns a printable string for the barrier related command type.
-/////////////////////////////////////////////////////////////////////////////////
+int sys_barrier(struct barrier_s **barrier, uint_t operation, uint_t count)
+{
+	kmem_req_t req;
+	struct barrier_s *ibarrier;
+	error_t err = EINVAL;
+  
+	if((err = vmm_check_address("usr barrier ptr", 
+				    current_task, 
+				    barrier,
+				    sizeof(struct barrier_s*))))
+		goto SYS_BARRIER_END;
 
-#if DEBUG_SYS_SOCKET || DEBUG_SYSCALLS_ERROR
-static char* barrier_cmd_str( uint32_t type )
+	if((err = cpu_copy_from_uspace(&ibarrier, barrier, sizeof(struct barrier_s *))))
+		goto SYS_BARRIER_END;
+
+	switch(operation)
+	{
+	case BARRIER_INIT_PRIVATE:
+	case BARRIER_INIT_SHARED:
+		req.type  = KMEM_BARRIER;
+		req.size  = sizeof(*ibarrier);
+		req.flags = AF_USER;
+
+		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;
+
+	case BARRIER_WAIT:
+
+		if((err = vmm_check_object(ibarrier, struct barrier_s, BARRIER_ID)))
+			break;
+
+		err = barrier_wait(ibarrier);
+		break;
+
+	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)
 {
-    if     ( type == BARRIER_INIT     ) return "INIT"; 
-    else if( type == BARRIER_WAIT     ) return "WAIT"; 
-    else if( type == BARRIER_DESTROY  ) return "DESTROY";
-    else                                return "undefined";
+	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;
 }
-#endif
-
-//////////////////////////////////
-int sys_barrier( intptr_t   vaddr,
-                 uint32_t   operation,
-                 uint32_t   count,
-                 intptr_t   attr )   
-{
-	error_t                 error;
-    vseg_t                * vseg;
-    pthread_barrierattr_t   k_attr;
-
-    thread_t  * this    = CURRENT_THREAD;
-    process_t * process = this->process;
-
-#if DEBUG_SYS_BARRIER || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
-uint64_t     tm_start = hal_get_cycles();
-#endif
-
-#if DEBUG_SYS_BARRIER
-if( DEBUG_SYS_BARRIER < tm_start )
-printk("\n[%s] thread[%x,%x] enters for %s / count %d / cycle %d\n",
-__FUNCTION__, process->pid, this->trdid, barrier_cmd_str(operation), count,
-(uint32_t)tm_start );
-#endif
-
-    // check vaddr in user vspace
-	if( vmm_get_vseg( process , vaddr , &vseg ) )
-    {
-
-#if DEBUG_SYSCALLS_ERROR
-if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
-printk("\n[ERROR] in %s for %s : unmapped barrier %x / thread[%x,%x]\n",
-__FUNCTION__, barrier_cmd_str(operation), vaddr, process->pid, this->trdid );
-#endif
-        this->errno = EINVAL;
-        return -1;
-    }
-
-    // execute requested operation
-	switch( operation )
-	{
-        //////////////////
-	    case BARRIER_INIT:
-        {
-            if( attr != 0 )   // QDT barrier required
-            {
-                if( vmm_get_vseg( process , attr , &vseg ) )
-                {
-
-#if DEBUG_SYSCALLS_ERROR
-if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
-printk("\n[ERROR] in %s for INIT : unmapped barrier attributes %x / thread[%x,%x]\n",
-__FUNCTION__ , attr , process->pid , this->trdid );
-#endif
-                    this->errno = EINVAL;
-                    return -1;
-                }
-  
-                // copy barrier attributes into kernel space
-                hal_copy_from_uspace( XPTR( local_cxy , &k_attr ),
-                                      (void *)attr,
-                                      sizeof(pthread_barrierattr_t) );
-
-                if ( count != k_attr.x_size * k_attr.y_size *k_attr.nthreads )  
-                {
-
-#if DEBUG_SYSCALLS_ERROR
-if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
-printk("\n[ERROR] in %s for INIT : count (%d) != x_size (%d) * y_size (%d) * nthreads (%x)\n",
-__FUNCTION__, count, k_attr.x_size, k_attr.y_size, k_attr.nthreads );
-#endif
-                    this->errno = EINVAL;
-                    return -1;
-                }
-  
-
-                // call relevant system function
-                error = generic_barrier_create( vaddr , count , &k_attr );
-            }
-            else               // simple barrier required
-            {
-                error = generic_barrier_create( vaddr , count , NULL );
-            }
-
-		    if( error )
-            {
-
-#if DEBUG_SYSCALLS_ERROR
-if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
-printk("\n[ERROR] in %s for INIT : cannot create barrier %x / thread[%x,%x]\n",
-__FUNCTION__ , vaddr , process->pid , this->trdid );
-#endif
-                this->errno = ENOMEM;
-                return -1;
-            }
-			break;
-        }
-        //////////////////
-	    case BARRIER_WAIT:
-        {
-            xptr_t barrier_xp = generic_barrier_from_ident( vaddr );
-
-            if( barrier_xp == XPTR_NULL )     // user error
-            {
-
-#if DEBUG_SYSCALLS_ERROR
-if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
-printk("\n[ERROR] in %s for WAIT : barrier %x not registered / thread[%x,%x]\n",
-__FUNCTION__ , (intptr_t)vaddr , process->pid, this->trdid );
-#endif
-                this->errno = EINVAL;
-                return -1;
-            }
-            else                          // success
-            {
-                generic_barrier_wait( barrier_xp ); 
-            }
-            break;
-        }
-        /////////////////////
-	    case BARRIER_DESTROY:
-        {
-            xptr_t barrier_xp = generic_barrier_from_ident( vaddr );
-
-            if( barrier_xp == XPTR_NULL )     // user error
-            {
-
-#if DEBUG_SYSCALLS_ERROR
-if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
-printk("\n[ERROR] in %s for DESTROY : barrier %x not registered / thread[%x,%x]\n",
-__FUNCTION__ , (intptr_t)vaddr , process->pid, this->trdid );
-#endif
-                this->errno = EINVAL;
-                return -1;
-            }
-            else                          // success
-            {
-                generic_barrier_destroy( barrier_xp ); 
-            }
-            break;
-        }
-        ////////
-        default:
-        {
-            assert( __FUNCTION__, false, "illegal operation type <%x>", operation );
-        }
-	}  // end switch
-
-    hal_fence();
-
-#if (DEBUG_SYS_BARRIER || CONFIG_INSTRUMENTATION_SYSCALLS)
-uint64_t     tm_end = hal_get_cycles();
-#endif
-
-#if DEBUG_SYS_BARRIER
-if( DEBUG_SYS_BARRIER < tm_end )
-printk("\n[%s] thread[%x,%x] exit for %s / cycle %d\n",
-__FUNCTION__, process->pid, this->trdid, sys_barrier_op_str(operation), (uint32_t)tm_end );
-#endif
-
-#if CONFIG_INSTRUMENTATION_SYSCALLS
-hal_atomic_add( &syscalls_cumul_cost[SYS_BARRIER] , tm_end - tm_start );
-hal_atomic_add( &syscalls_occurences[SYS_BARRIER] , 1 );
-#endif
-
-	return 0;
-
-}  // end sys_barrier()
-
