소스 검색

[Pal] Remove redundant spinlock implementation

borysp 4 년 전
부모
커밋
e049518369

+ 1 - 1
Pal/src/host/Linux-SGX/Makefile

@@ -14,7 +14,7 @@ CFLAGS += $(defs)
 ASFLAGS += $(defs)
 enclave-objs = $(addprefix db_,files devices pipes eventfd sockets streams memory \
 		 threading mutex events process object main rtld \
-		 exception misc spinlock) \
+		 exception misc) \
 	       $(addprefix enclave_,ocalls ecalls framework platform pages untrusted)
 enclave-asm-objs = enclave_entry
 urts-objs = $(addprefix sgx_,enclave framework platform main rtld thread process exception graphene) \

+ 7 - 6
Pal/src/host/Linux-SGX/db_memory.c

@@ -28,6 +28,7 @@
 #include "pal_security.h"
 #include "pal_error.h"
 #include "pal_debug.h"
+#include "spinlock.h"
 #include "api.h"
 
 #include <asm/mman.h>
@@ -41,7 +42,7 @@ static struct pal_vma {
 } pal_vmas[PAL_VMA_MAX];
 
 static uint32_t pal_nvmas = 0;
-static struct spinlock pal_vma_lock;
+static spinlock_t pal_vma_lock = INIT_SPINLOCK_UNLOCKED;
 
 bool _DkCheckMemoryMappable (const void * addr, size_t size)
 {
@@ -50,16 +51,16 @@ bool _DkCheckMemoryMappable (const void * addr, size_t size)
         return true;
     }
 
-    _DkSpinLock(&pal_vma_lock);
+    spinlock_lock(&pal_vma_lock);
 
     for (uint32_t i = 0 ; i < pal_nvmas ; i++)
         if (addr < pal_vmas[i].top && addr + size > pal_vmas[i].bottom) {
             printf("address %p-%p is not mappable\n", addr, addr + size);
-            _DkSpinUnlock(&pal_vma_lock);
+            spinlock_unlock(&pal_vma_lock);
             return true;
         }
 
-    _DkSpinUnlock(&pal_vma_lock);
+    spinlock_unlock(&pal_vma_lock);
     return false;
 }
 
@@ -90,12 +91,12 @@ int _DkVirtualMemoryAlloc (void ** paddr, uint64_t size, int alloc_type, int pro
 
     if (alloc_type & PAL_ALLOC_INTERNAL) {
         SGX_DBG(DBG_M, "pal allocates %p-%p for internal use\n", mem, mem + size);
-        _DkSpinLock(&pal_vma_lock);
+        spinlock_lock(&pal_vma_lock);
         assert(pal_nvmas < PAL_VMA_MAX);
         pal_vmas[pal_nvmas].bottom = mem;
         pal_vmas[pal_nvmas].top = mem + size;
         pal_nvmas++;
-        _DkSpinUnlock(&pal_vma_lock);
+        spinlock_unlock(&pal_vma_lock);
     }
 
     *paddr = mem;

+ 11 - 10
Pal/src/host/Linux-SGX/db_process.c

@@ -35,6 +35,7 @@
 #include "pal_error.h"
 #include "pal_security.h"
 #include "pal_crypto.h"
+#include "spinlock.h"
 #include "api.h"
 
 #include <linux/sched.h>
@@ -52,22 +53,22 @@ struct trusted_child {
 
 DEFINE_LISTP(trusted_child);
 static LISTP_TYPE(trusted_child) trusted_children = LISTP_INIT;
-static struct spinlock trusted_children_lock = LOCK_INIT;
+static spinlock_t trusted_children_lock = INIT_SPINLOCK_UNLOCKED;
 
 int register_trusted_child(const char * uri, const char * mr_enclave_str)
 {
     struct trusted_child * tc = NULL, * new;
     int uri_len = strlen(uri);
 
-    _DkSpinLock(&trusted_children_lock);
+    spinlock_lock(&trusted_children_lock);
 
     LISTP_FOR_EACH_ENTRY(tc, &trusted_children, list) {
         if (!memcmp(tc->uri, uri, uri_len + 1)) {
-            _DkSpinUnlock(&trusted_children_lock);
+            spinlock_unlock(&trusted_children_lock);
             return 0;
         }
     }
-    _DkSpinUnlock(&trusted_children_lock);
+    spinlock_unlock(&trusted_children_lock);
 
     new = malloc(sizeof(struct trusted_child) + uri_len);
     if (!new)
@@ -116,18 +117,18 @@ int register_trusted_child(const char * uri, const char * mr_enclave_str)
 
     SGX_DBG(DBG_S, "trusted: %s %s\n", mr_enclave_text, new->uri);
 
-    _DkSpinLock(&trusted_children_lock);
+    spinlock_lock(&trusted_children_lock);
 
     LISTP_FOR_EACH_ENTRY(tc, &trusted_children, list) {
         if (!memcmp(tc->uri, uri, uri_len + 1)) {
-            _DkSpinUnlock(&trusted_children_lock);
+            spinlock_unlock(&trusted_children_lock);
             free(new);
             return 0;
         }
     }
 
     LISTP_ADD_TAIL(new, &trusted_children, list);
-    _DkSpinUnlock(&trusted_children_lock);
+    spinlock_unlock(&trusted_children_lock);
     return 0;
 }
 
@@ -230,18 +231,18 @@ static int check_child_mr_enclave(PAL_HANDLE child, sgx_measurement_t* mr_enclav
     }
 
     struct trusted_child * tc;
-    _DkSpinLock(&trusted_children_lock);
+    spinlock_lock(&trusted_children_lock);
 
     /* Try to find a matching mr_enclave from the manifest */
     LISTP_FOR_EACH_ENTRY(tc, &trusted_children, list) {
         if (!memcmp(mr_enclave, &tc->mr_enclave, sizeof(sgx_measurement_t))) {
-            _DkSpinUnlock(&trusted_children_lock);
+            spinlock_unlock(&trusted_children_lock);
             SGX_DBG(DBG_S, "trusted child: %s\n", tc->uri);
             return 0;
         }
     }
 
-    _DkSpinUnlock(&trusted_children_lock);
+    spinlock_unlock(&trusted_children_lock);
     return 1;
 }
 

+ 0 - 52
Pal/src/host/Linux-SGX/db_spinlock.c

@@ -1,52 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS 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 Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_spinlock.c
- *
- * This file contains APIs that provide operations of (futex based) mutexes.
- * Based on "Mutexes and Condition Variables using Futexes"
- * (http://locklessinc.com/articles/mutex_cv_futex)
- */
-
-#include <atomic.h>
-#include <limits.h>
-
-#include "api.h"
-#include "pal.h"
-#include "pal_debug.h"
-#include "pal_defs.h"
-#include "pal_error.h"
-#include "pal_internal.h"
-#include "pal_linux.h"
-#include "pal_linux_defs.h"
-
-int _DkSpinLock(struct spinlock* lock) {
-    struct atomic_int* m = &lock->value;
-    while (1) {
-        int c = atomic_read(m);
-        if (!c && atomic_cmpxchg(m, 0, 1) == 0)
-            break;
-        CPU_RELAX();
-    }
-    return 0;
-}
-
-int _DkSpinUnlock(struct spinlock* lock) {
-    struct atomic_int* m = &lock->value;
-    atomic_set(m, 0);
-    return 0;
-}

+ 20 - 19
Pal/src/host/Linux-SGX/enclave_framework.c

@@ -1,12 +1,13 @@
-#include <pal_linux.h>
-#include <pal_linux_error.h>
-#include <pal_internal.h>
+#include <api.h>
+#include <list.h>
+#include <pal_crypto.h>
 #include <pal_debug.h>
 #include <pal_error.h>
+#include <pal_internal.h>
+#include <pal_linux.h>
+#include <pal_linux_error.h>
 #include <pal_security.h>
-#include <pal_crypto.h>
-#include <api.h>
-#include <list.h>
+#include <spinlock.h>
 #include <stdbool.h>
 
 #include "enclave_pages.h"
@@ -218,7 +219,7 @@ struct trusted_file {
 
 DEFINE_LISTP(trusted_file);
 static LISTP_TYPE(trusted_file) trusted_file_list = LISTP_INIT;
-static struct spinlock trusted_file_lock = LOCK_INIT;
+static spinlock_t trusted_file_lock = INIT_SPINLOCK_UNLOCKED;
 static int trusted_file_indexes = 0;
 static bool allow_file_creation = 0;
 static int file_check_policy = FILE_CHECK_POLICY_STRICT;
@@ -300,7 +301,7 @@ int load_trusted_file (PAL_HANDLE file, sgx_stub_t ** stubptr,
     }
     len += URI_PREFIX_FILE_LEN;
 
-    _DkSpinLock(&trusted_file_lock);
+    spinlock_lock(&trusted_file_lock);
 
     LISTP_FOR_EACH_ENTRY(tmp, &trusted_file_list, list) {
         if (tmp->stubs) {
@@ -318,7 +319,7 @@ int load_trusted_file (PAL_HANDLE file, sgx_stub_t ** stubptr,
         }
     }
 
-    _DkSpinUnlock(&trusted_file_lock);
+    spinlock_unlock(&trusted_file_lock);
 
     if (!tf || !tf->index) {
         if (!tf) {
@@ -434,19 +435,19 @@ unmap:
         goto failed;
     }
 
-    _DkSpinLock(&trusted_file_lock);
+    spinlock_lock(&trusted_file_lock);
     if (tf->stubs || tf->index == -PAL_ERROR_DENIED)
         free(tf->stubs);
     *stubptr = tf->stubs = stubs;
     *sizeptr = tf->size;
     ret = tf->index;
-    _DkSpinUnlock(&trusted_file_lock);
+    spinlock_unlock(&trusted_file_lock);
     return ret;
 
 failed:
     free(stubs);
 
-    _DkSpinLock(&trusted_file_lock);
+    spinlock_lock(&trusted_file_lock);
     if (tf->stubs) {
         *stubptr = tf->stubs;
         *sizeptr = tf->size;
@@ -454,7 +455,7 @@ failed:
     } else {
         tf->index = -PAL_ERROR_DENIED;
     }
-    _DkSpinUnlock(&trusted_file_lock);
+    spinlock_unlock(&trusted_file_lock);
 
 #if PRINT_ENCLAVE_STAT
     if (!ret) {
@@ -624,15 +625,15 @@ static int register_trusted_file (const char * uri, const char * checksum_str)
     size_t uri_len = strlen(uri);
     int ret;
 
-    _DkSpinLock(&trusted_file_lock);
+    spinlock_lock(&trusted_file_lock);
 
     LISTP_FOR_EACH_ENTRY(tf, &trusted_file_list, list) {
         if (tf->uri_len == uri_len && !memcmp(tf->uri, uri, uri_len)) {
-            _DkSpinUnlock(&trusted_file_lock);
+            spinlock_unlock(&trusted_file_lock);
             return 0;
         }
     }
-    _DkSpinUnlock(&trusted_file_lock);
+    spinlock_unlock(&trusted_file_lock);
 
     new = malloc(sizeof(struct trusted_file));
     if (!new)
@@ -697,18 +698,18 @@ static int register_trusted_file (const char * uri, const char * checksum_str)
         SGX_DBG(DBG_S, "allowed: %s\n", new->uri);
     }
 
-    _DkSpinLock(&trusted_file_lock);
+    spinlock_lock(&trusted_file_lock);
 
     LISTP_FOR_EACH_ENTRY(tf, &trusted_file_list, list) {
         if (tf->uri_len == uri_len && !memcmp(tf->uri, uri, uri_len)) {
-            _DkSpinUnlock(&trusted_file_lock);
+            spinlock_unlock(&trusted_file_lock);
             free(new);
             return 0;
         }
     }
 
     LISTP_ADD_TAIL(new, &trusted_file_list, list);
-    _DkSpinUnlock(&trusted_file_lock);
+    spinlock_unlock(&trusted_file_lock);
     return 0;
 }
 

+ 4 - 3
Pal/src/host/Linux-SGX/enclave_untrusted.c

@@ -18,14 +18,15 @@
 #include <pal_error.h>
 #include <pal_internal.h>
 #include <pal_security.h>
+#include <spinlock.h>
 
 #include "enclave_ocalls.h"
 
-static PAL_LOCK malloc_lock = LOCK_INIT;
+static spinlock_t malloc_lock = INIT_SPINLOCK_UNLOCKED;
 static size_t g_page_size   = PRESET_PAGESIZE;
 
-#define SYSTEM_LOCK()   _DkSpinLock(&malloc_lock)
-#define SYSTEM_UNLOCK() _DkSpinUnlock(&malloc_lock)
+#define SYSTEM_LOCK()   spinlock_lock(&malloc_lock)
+#define SYSTEM_UNLOCK() spinlock_unlock(&malloc_lock)
 
 #define ALLOC_ALIGNMENT g_page_size
 

+ 5 - 10
Pal/src/host/Linux-SGX/pal_host.h

@@ -28,18 +28,13 @@
 #endif
 
 #include <atomic.h>
+#include <spinlock.h>
 
-/* Spinlocking */
-typedef struct spinlock {
-    struct atomic_int value;
-} PAL_LOCK;
+typedef spinlock_t PAL_LOCK;
 
-int _DkSpinLock (struct spinlock * lock);
-int _DkSpinUnlock (struct spinlock * lock);
-
-#define LOCK_INIT   { .value =  { 0 } }
-#define _DkInternalLock _DkSpinLock
-#define _DkInternalUnlock _DkSpinUnlock
+#define LOCK_INIT INIT_SPINLOCK_UNLOCKED
+#define _DkInternalLock spinlock_lock
+#define _DkInternalUnlock spinlock_unlock
 
 void * malloc_untrusted (int size);
 void free_untrusted (void * mem);

+ 2 - 4
Pal/src/host/Linux/db_mutex.c

@@ -187,16 +187,14 @@ void _DkMutexRelease(PAL_HANDLE handle) {
     return;
 }
 
-int _DkInternalLock(PAL_LOCK* lock) {
+void _DkInternalLock(PAL_LOCK* lock) {
     // Retry the lock if being interrupted by signals
     while (_DkMutexLock(lock) < 0)
         ;
-    return 0;
 }
 
-int _DkInternalUnlock(PAL_LOCK* lock) {
+void _DkInternalUnlock(PAL_LOCK* lock) {
     _DkMutexUnlock(lock);
-    return 0;
 }
 
 static int mutex_wait(PAL_HANDLE handle, int64_t timeout_us) {

+ 5 - 4
Pal/src/host/Skeleton/db_misc.c

@@ -21,17 +21,18 @@
  */
 
 #include "api.h"
+#include "assert.h"
 #include "pal.h"
 #include "pal_defs.h"
 #include "pal_error.h"
 #include "pal_internal.h"
 
-int _DkInternalLock(PAL_LOCK* lock) {
-    return -PAL_ERROR_NOTIMPLEMENTED;
+void _DkInternalLock(PAL_LOCK* lock) {
+    __abort();
 }
 
-int _DkInternalUnlock(PAL_LOCK* lock) {
-    return -PAL_ERROR_NOTIMPLEMENTED;
+void _DkInternalUnlock(PAL_LOCK* lock) {
+    __abort();
 }
 
 unsigned long _DkSystemTimeQuery(void) {

+ 2 - 2
Pal/src/pal_internal.h

@@ -335,8 +335,8 @@ void _DkRaiseFailure (int error);
 void _DkExceptionReturn (void * event);
 
 /* other DK calls */
-int _DkInternalLock (PAL_LOCK * mut);
-int _DkInternalUnlock (PAL_LOCK * mut);
+void _DkInternalLock(PAL_LOCK* mut);
+void _DkInternalUnlock(PAL_LOCK* mut);
 unsigned long _DkSystemTimeQuery (void);
 size_t _DkFastRandomBitsRead (void * buffer, size_t size);