Index: trunk/kernel/syscalls/sys_condvar.c
===================================================================
--- trunk/kernel/syscalls/sys_cond_var.c	(revision 1)
+++ trunk/kernel/syscalls/sys_condvar.c	(revision 23)
@@ -1,15 +1,16 @@
 /*
- * sys_cond_var: interface to access condition vraibles service
+ * sys_condvar.c - Access a POSIX condvar.
  * 
- * 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,127 +18,152 @@
  *
  * 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 <kmem.h>
-#include <task.h>
+#include <printk.h>
 #include <vmm.h>
-#include <kmagics.h>
-#include <semaphore.h>
-#include <cond_var.h>
+#include <syscalls.h>
+#include <remote_condvar.h>
 
-static inline bool_t isBadSem(struct semaphore_s *sem)
-{ 
-	return vmm_check_object(sem, struct semaphore_s, SEMAPHORE_ID);
-}
+////////////////////////////////////////
+int sys_condvar( void         * condvar,
+                 uint32_t       operation,
+                 void         * mutex )
+{
+	error_t    error;
+    paddr_t    paddr;
+ 
+    thread_t * this = CURRENT_THREAD;
 
-static inline bool_t isBadCV(struct cv_s *cv)
-{ 
-	return vmm_check_object(cv, struct cv_s, COND_VAR_ID);
-}
+    // check condvar in user vspace
+	error = vmm_v2p_translate( false , condvar , &paddr );
+	if( error )
+    {
+        printk("\n[ERROR] in %s : illegal condvar virtual address = %x\n",
+               __FUNCTION__ , (intptr_t)condvar );
+        this->errno = error;
+        return -1;
+    }
 
-int sys_cond_var(struct cv_s **cv, uint_t operation, struct semaphore_s **sem)
-{
-	kmem_req_t req;
-	struct cv_s *icv;
-	struct semaphore_s *isem;
-	error_t err = EINVAL;
- 
-	if((err = vmm_check_address("usr cv ptr", current_task,cv,sizeof(struct cv_s*))))
-		goto SYS_COND_END;
+    // execute requested operation
+	switch( operation )
+	{
+        //////////////////
+        case CONDVAR_INIT:
+        {
+            error = remote_condvar_create( (intptr_t)condvar );
+    
+		    if( error )
+            {
+                printk("\n[ERROR] in %s : cannot create condvar = %x\n",
+                       __FUNCTION__ , (intptr_t)condvar );
+                this->errno = error;
+                return -1;
+            }
+		    break;
+        }
+        //////////////////
+	    case CONDVAR_WAIT:
+        {
+            // check mutex in user vspace
+	        error = vmm_v2p_translate( false , mutex , &paddr );
 
-	if((err = cpu_copy_from_uspace(&icv, cv, sizeof(struct cv_s *))))
-		goto SYS_COND_END;
+	        if( error )
+            {
+                printk("\n[ERROR] in %s : illegal condvar virtual address = %x\n",
+                       __FUNCTION__ , (intptr_t)condvar );
+                this->errno = error;
+                return -1;
+            }
 
-	switch(operation)
-	{
-	case CV_INIT:
-		req.type  = KMEM_CV;
-		req.size  = sizeof(*icv);
-		req.flags = AF_USER;
+            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
+
+            if( condvar_xp == XPTR_NULL )     // user error
+            {
+                printk("\n[ERROR] in %s : condvar %x not registered\n",
+                       __FUNCTION__ , (intptr_t)condvar );
+                this->errno = EINVAL;
+                return -1;
+            }
     
-		if((icv = kmem_alloc(&req)) == NULL)
-		{
-			err = ENOMEM;
-			break;
+            xptr_t mutex_xp = remote_condvar_from_ident( (intptr_t)condvar );
+            if( mutex_xp == XPTR_NULL )     // user error
+            {
+                printk("\n[ERROR] in %s : mutex %x not registered\n",
+                       __FUNCTION__ , (intptr_t)condvar );
+                this->errno = EINVAL;
+                return -1;
+            }
+
+            remote_condvar_wait( condvar_xp , mutex_xp );
+
+            break;
+        }
+        ////////////////////
+	    case CONDVAR_SIGNAL:
+        {
+            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
+
+            if( condvar_xp == XPTR_NULL )     // user error
+            {
+                printk("\n[ERROR] in %s : condvar %x not registered\n",
+                       __FUNCTION__ , (intptr_t)condvar );
+                this->errno = EINVAL;
+                return -1;
+            }
+
+            remote_condvar_signal( condvar_xp ); 
+            
+            break;
+        }
+        ///////////////////////
+	    case CONDVAR_BROADCAST:
+        {
+            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
+
+            if( condvar_xp == XPTR_NULL )     // user error
+            {
+                printk("\n[ERROR] in %s : condvar %x not registered\n",
+                       __FUNCTION__ , (intptr_t)condvar );
+                this->errno = EINVAL;
+                return -1;
+            }
+
+            remote_condvar_broadcast( condvar_xp );
+
+            break;
+        }
+        /////////////////////
+	    case CONDVAR_DESTROY:
+        {
+            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
+
+            if( condvar_xp == XPTR_NULL )     // user error
+            {
+                printk("\n[ERROR] in %s : condvar %x not registered\n",
+                       __FUNCTION__ , (intptr_t)condvar );
+                this->errno = EINVAL;
+                return -1;
+            }
+
+            remote_condvar_destroy( condvar_xp ); 
+
+            break;
 		}
-    
-		if((err = cv_init(icv)))
-			break;
-    
-		if((err = cpu_copy_to_uspace(cv, &icv, sizeof(struct cv_s *))))
-		{
-			req.ptr = icv;
-			kmem_free(&req);
-		}
+        /////////
+	    default:
+        {
+            printk("\n[PANIC] in %s : illegal operation type\n", __FUNCTION__ );
+            hal_core_sleep();
+        }
+	}   // end switch
 
-		break;
+	return 0;
 
-	case CV_WAIT:
-		err = vmm_check_address("usr sem ptr", 
-					current_task, 
-					sem, sizeof(struct semaphore_s*));
+}  // enc sys_condvar()
 
-		if(err) break;
-
-		if((err = cpu_copy_from_uspace(&isem, sem, sizeof(struct semaphore_s *))))
-			break;
-
-		if(isBadSem(isem))
-			break;
-
-		if(isBadCV(icv))
-			break;
-    
-		return cv_wait(icv, isem);
-
-	case CV_SIGNAL:
-		if(isBadCV(icv))
-			break;
-    
-		return cv_signal(icv);
-
-	case CV_BROADCAST:
-		if(isBadCV(icv))
-			break;
-    
-		return cv_broadcast(icv);
-
-	case CV_DESTROY:
-		if(isBadCV(icv))
-			break;
-    
-		if((err = cv_destroy(icv)))
-			break;
-    
-		req.type = KMEM_CV;
-		req.ptr  = icv;
-		kmem_free(&req);
-		return 0;
-
-	default:
-		err = EINVAL;
-	}
-
-SYS_COND_END:
-	current_thread->info.errno = err;
-	return err;
-}
-
-KMEM_OBJATTR_INIT(cv_kmem_init)
-{
-	attr->type   = KMEM_CV;
-	attr->name   = "KCM Condition Variable";
-	attr->size   = sizeof(struct cv_s);
-	attr->aligne = 0;
-	attr->min    = CONFIG_CONDTION_VAR_MIN;
-	attr->max    = CONFIG_CONDTION_VAR_MAX;
-	attr->ctor   = NULL;
-	attr->dtor   = NULL;
-  
-	return 0;
-}
