re-org platform APIs, simplify porting process (#201)

Co-authored-by: Xu Jun <jun1.xu@intel.com>
This commit is contained in:
Xu Jun
2020-03-16 16:43:57 +08:00
committed by GitHub
parent ef5ceffe71
commit f1a0e75ab7
177 changed files with 2954 additions and 7904 deletions

View File

@ -1,58 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_platform.h"
#include "bh_assert.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef BH_TEST
#include <setjmp.h>
#endif
#ifdef BH_TEST
/* for exception throwing */
jmp_buf bh_test_jb;
#endif
#define FIXED_BUFFER_SIZE (1<<9)
void bh_assert_internal(int v, const char *file_name, int line_number,
const char *expr_string)
{
if (v)
return;
if (!file_name)
file_name = "NULL FILENAME";
if (!expr_string)
expr_string = "NULL EXPR_STRING";
bh_printf_sgx("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
file_name, line_number);
#ifdef BH_TEST
longjmp(bh_test_jb, 1);
#endif
abort();
}
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
...)
{
#ifndef JEFF_TEST_VERIFIER
va_list args;
char msg[FIXED_BUFFER_SIZE] = { '\0' };
va_start(args, fmt);
vsnprintf(msg, FIXED_BUFFER_SIZE, fmt, args);
va_end(args);
bh_printf_sgx("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
bh_printf_sgx(msg);
bh_printf_sgx("\n");
#endif
}

View File

@ -1,126 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _BH_PLATFORM_H
#define _BH_PLATFORM_H
#include "bh_config.h"
#include "bh_types.h"
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <sgx_thread.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*bh_print_function_t)(const char* message);
void bh_set_print_function(bh_print_function_t pf);
extern int bh_printf_sgx(const char *message, ...);
extern int bh_vprintf_sgx(const char * format, va_list arg);
typedef uint64_t uint64;
typedef int64_t int64;
#ifndef BH_PLATFORM_LINUX_SGX
#define BH_PLATFORM_LINUX_SGX
#endif
/* NEED qsort */
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
/* Stack size of applet threads's native part. */
#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Default thread priority */
#define BH_THREAD_DEFAULT_PRIORITY 0
#define BH_ROUTINE_MODIFIER
#define BHT_TIMEDOUT ETIMEDOUT
#define INVALID_THREAD_ID 0xFFffFFff
typedef int korp_sem;
typedef void* (*thread_start_routine_t)(void*);
typedef sgx_thread_mutex_t korp_mutex;
typedef sgx_thread_t korp_tid;
typedef sgx_thread_t korp_thread;
typedef sgx_thread_cond_t korp_cond;
void *os_malloc(unsigned size);
void *os_realloc(void *ptr, unsigned size);
void os_free(void *ptr);
#define bh_printf bh_printf_sgx
int snprintf(char *buffer, size_t count, const char *format, ...);
int strncasecmp(const char *s1, const char *s2, size_t n);
double fmod(double x, double y);
float fmodf(float x, float y);
double sqrt(double x);
#define BH_WAIT_FOREVER 0xFFFFFFFF
#ifndef NULL
# define NULL ((void*) 0)
#endif
/**
* Return the offset of the given field in the given type.
*
* @param Type the type containing the filed
* @param field the field in the type
*
* @return the offset of field in Type
*/
#ifndef offsetof
#define offsetof(Type, field) ((size_t)(&((Type *)0)->field))
#endif
#define bh_assert assert
int bh_platform_init();
/* MMAP mode */
enum {
MMAP_PROT_NONE = 0,
MMAP_PROT_READ = 1,
MMAP_PROT_WRITE = 2,
MMAP_PROT_EXEC = 4
};
/* MMAP flags */
enum {
MMAP_MAP_NONE = 0,
/* Put the mapping into 0 to 2 G, supported only on x86_64 */
MMAP_MAP_32BIT = 1,
/* Don't interpret addr as a hint: place the mapping at exactly
that address. */
MMAP_MAP_FIXED = 2
};
void *bh_mmap(void *hint, unsigned int size, int prot, int flags);
void bh_munmap(void *addr, uint32 size);
int bh_mprotect(void *addr, uint32 size, int prot);
#define bh_dcache_flush() (void)0
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,17 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_platform.h"
#include <stdio.h>
void bh_log_emit(const char *fmt, va_list ap)
{
bh_vprintf_sgx(fmt, ap);
}
int bh_fflush(void *stream)
{
return 0;
}

View File

@ -1,181 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_thread.h"
#include "bh_assert.h"
#include <stdio.h>
#include <stdlib.h>
#include <sgx_thread.h>
int _vm_thread_sys_init()
{
return 0;
}
void vm_thread_sys_destroy(void)
{
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
{
return BHT_ERROR;
// return BHT_OK;
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
}
korp_tid _vm_self_thread()
{
return sgx_thread_self();
}
void vm_thread_exit(void * code)
{
}
// storage for one thread
static __thread void *_tls_store = NULL;
void *_vm_tls_get(unsigned idx)
{
return _tls_store;
}
int _vm_tls_put(unsigned idx, void * tls)
{
_tls_store = tls;
return BHT_OK;
//return BHT_ERROR;
}
int _vm_mutex_init(korp_mutex *mutex)
{
sgx_thread_mutex_t m = SGX_THREAD_MUTEX_INITIALIZER;
*mutex = m;
return BHT_OK;
}
int _vm_recursive_mutex_init(korp_mutex *mutex)
{
sgx_thread_mutex_t m = SGX_THREAD_RECURSIVE_MUTEX_INITIALIZER;
*mutex = m;
return BHT_OK;
}
int _vm_mutex_destroy(korp_mutex *mutex)
{
sgx_thread_mutex_destroy(mutex);
return BHT_OK;
}
/* Returned error (EINVAL, EAGAIN and EDEADLK) from
locking the mutex indicates some logic error present in
the program somewhere.
Don't try to recover error for an existing unknown error.*/
void vm_mutex_lock(korp_mutex *mutex)
{
sgx_thread_mutex_lock(mutex);
}
int vm_mutex_trylock(korp_mutex *mutex)
{
return (sgx_thread_mutex_trylock(mutex) == 0? BHT_OK: BHT_ERROR);
}
/* Returned error (EINVAL, EAGAIN and EPERM) from
unlocking the mutex indicates some logic error present
in the program somewhere.
Don't try to recover error for an existing unknown error.*/
void vm_mutex_unlock(korp_mutex *mutex)
{
sgx_thread_mutex_unlock(mutex);
}
int _vm_sem_init(korp_sem* sem, unsigned int c)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_sem_destroy(korp_sem *sem)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_sem_wait(korp_sem *sem)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_sem_reltimedwait(korp_sem *sem, int mills)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_sem_post(korp_sem *sem)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_cond_init(korp_cond *cond)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_cond_destroy(korp_cond *cond)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_cond_signal(korp_cond *cond)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_cond_broadcast(korp_cond *cond)
{
return BHT_OK;
//return BHT_ERROR;
}
int _vm_thread_cancel(korp_tid thread)
{
return 0;
}
int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
{
return 0;
}
int _vm_thread_detach(korp_tid thread)
{
return 0;
}

View File

@ -1,53 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_time.h"
#include <unistd.h>
#include <stdio.h>
#include <time.h>
/*
* This function returns milliseconds per tick.
* @return milliseconds per tick.
*/
uint64 _bh_time_get_tick_millisecond()
{
//TODO:
return 0;
}
/*
* This function returns milliseconds after boot.
* @return milliseconds after boot.
*/
uint64 _bh_time_get_boot_millisecond()
{
//TODO
return 0;
}
uint32 bh_get_tick_sec()
{
//TODO
return 0;
}
/*
* This function returns GMT time milliseconds since from 1970.1.1, AKA UNIX time.
* @return milliseconds since from 1970.1.1.
*/
uint64 _bh_time_get_millisecond_from_1970()
{
//TODO
return 0;
}
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
//TODO
return 0;
}

View File

@ -0,0 +1,53 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _PLATFORM_INTERNAL_H
#define _PLATFORM_INTERNAL_H
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <sgx_thread.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef BH_PLATFORM_LINUX_SGX
#define BH_PLATFORM_LINUX_SGX
#endif
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
/* Stack size of applet threads's native part. */
#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Default thread priority */
#define BH_THREAD_DEFAULT_PRIORITY 0
typedef sgx_thread_t korp_thread;
typedef sgx_thread_t korp_tid;
typedef sgx_thread_mutex_t korp_mutex;
typedef sgx_thread_cond_t korp_cond;
typedef void (*os_print_function_t)(const char* message);
void os_set_print_function(os_print_function_t pf);
#ifdef __cplusplus
}
#endif
#endif /* end of _PLATFORM_INTERNAL_H */

View File

@ -3,22 +3,27 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_common.h"
#include "bh_platform.h"
#include "platform_api_vmcore.h"
#include "platform_api_extension.h"
#include <unistd.h>
#if WASM_ENABLE_AOT != 0
#include "sgx_rsrv_mem_mngr.h"
#endif
#define FIXED_BUFFER_SIZE (1<<9)
static bh_print_function_t print_function = NULL;
static os_print_function_t print_function = NULL;
int bh_platform_init()
{
return 0;
}
void
bh_platform_destroy()
{
}
void *
os_malloc(unsigned size)
{
@ -47,12 +52,12 @@ int puts(const char *s)
return 0;
}
void bh_set_print_function(bh_print_function_t pf)
void os_set_print_function(os_print_function_t pf)
{
print_function = pf;
}
int bh_printf_sgx(const char *message, ...)
int os_printf(const char *message, ...)
{
if (print_function != NULL) {
char msg[FIXED_BUFFER_SIZE] = { '\0' };
@ -66,7 +71,7 @@ int bh_printf_sgx(const char *message, ...)
return 0;
}
int bh_vprintf_sgx(const char * format, va_list arg)
int os_vprintf(const char * format, va_list arg)
{
if (print_function != NULL) {
char msg[FIXED_BUFFER_SIZE] = { '\0' };
@ -77,7 +82,7 @@ int bh_vprintf_sgx(const char * format, va_list arg)
return 0;
}
void* bh_mmap(void *hint, unsigned int size, int prot, int flags)
void* os_mmap(void *hint, unsigned int size, int prot, int flags)
{
#if WASM_ENABLE_AOT != 0
int mprot = 0;
@ -87,7 +92,7 @@ void* bh_mmap(void *hint, unsigned int size, int prot, int flags)
ret = sgx_alloc_rsrv_mem(alignedSize);
if (ret == NULL) {
bh_printf_sgx("bh_mmap(size=%d, alignedSize=%d, prot=0x%x) failed.",size, alignedSize, prot);
os_printf_sgx("os_mmap(size=%d, alignedSize=%d, prot=0x%x) failed.",size, alignedSize, prot);
return NULL;
}
if (prot & MMAP_PROT_READ)
@ -98,7 +103,7 @@ void* bh_mmap(void *hint, unsigned int size, int prot, int flags)
mprot |= SGX_PROT_EXEC;
st = sgx_tprotect_rsrv_mem(ret, alignedSize, mprot);
if (st != SGX_SUCCESS){
bh_printf_sgx("bh_mmap(size=%d,prot=0x%x) failed to set protect.",size, prot);
os_printf_sgx("os_mmap(size=%d,prot=0x%x) failed to set protect.",size, prot);
sgx_free_rsrv_mem(ret, alignedSize);
return NULL;
}
@ -109,14 +114,14 @@ void* bh_mmap(void *hint, unsigned int size, int prot, int flags)
#endif
}
void bh_munmap(void *addr, uint32 size)
void os_munmap(void *addr, uint32 size)
{
#if WASM_ENABLE_AOT != 0
sgx_free_rsrv_mem(addr, size);
#endif
}
int bh_mprotect(void *addr, uint32 size, int prot)
int os_mprotect(void *addr, uint32 size, int prot)
{
#if WASM_ENABLE_AOT != 0
int mprot = 0;
@ -129,10 +134,16 @@ int bh_mprotect(void *addr, uint32 size, int prot)
if (prot & MMAP_PROT_EXEC)
mprot |= SGX_PROT_EXEC;
st = sgx_tprotect_rsrv_mem(addr, size, mprot);
if (st != SGX_SUCCESS) bh_printf_sgx("bh_mprotect(addr=0x%lx,size=%d,prot=0x%x) failed.", addr, size, prot);
if (st != SGX_SUCCESS) os_printf_sgx("os_mprotect(addr=0x%lx,size=%d,prot=0x%x) failed.", addr, size, prot);
return (st == SGX_SUCCESS? 0:-1);
#else
return -1;
#endif
}
void
os_dcache_flush(void)
{
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "platform_api_vmcore.h"
#include "platform_api_extension.h"
korp_tid os_self_thread()
{
return sgx_thread_self();
}
int os_mutex_init(korp_mutex *mutex)
{
sgx_thread_mutex_t m = SGX_THREAD_MUTEX_INITIALIZER;
*mutex = m;
return BHT_OK;
}
int os_mutex_destroy(korp_mutex *mutex)
{
sgx_thread_mutex_destroy(mutex);
return BHT_OK;
}
/* Returned error (EINVAL, EAGAIN and EDEADLK) from
locking the mutex indicates some logic error present in
the program somewhere.
Don't try to recover error for an existing unknown error.*/
void os_mutex_lock(korp_mutex *mutex)
{
sgx_thread_mutex_lock(mutex);
}
/* Returned error (EINVAL, EAGAIN and EPERM) from
unlocking the mutex indicates some logic error present
in the program somewhere.
Don't try to recover error for an existing unknown error.*/
void os_mutex_unlock(korp_mutex *mutex)
{
sgx_thread_mutex_unlock(mutex);
}

View File

@ -0,0 +1,14 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "platform_api_vmcore.h"
uint64
os_time_get_boot_microsecond()
{
/* TODO */
return 0;
}

View File

@ -3,6 +3,8 @@
set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
add_definitions(-DBH_PLATFORM_LINUX_SGX)
include_directories(${PLATFORM_SHARED_DIR})
include_directories(${PLATFORM_SHARED_DIR}/../include)