baremetal: add baremetal platform
This commit is contained in:
192
core/shared/platform/include/baremetal/platform_api_vmcore.h
Normal file
192
core/shared/platform/include/baremetal/platform_api_vmcore.h
Normal file
@ -0,0 +1,192 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _PLATFORM_API_VMCORE_H
|
||||
#define _PLATFORM_API_VMCORE_H
|
||||
|
||||
#include "platform_common.h"
|
||||
#include "platform_internal.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/****************************************************
|
||||
* Section 1 *
|
||||
* Interfaces required by the runtime *
|
||||
****************************************************/
|
||||
|
||||
/**
|
||||
* Initialize the platform internal resources if needed,
|
||||
* this function is called by wasm_runtime_init() and
|
||||
* wasm_runtime_full_init()
|
||||
*
|
||||
* @return 0 if success
|
||||
*/
|
||||
int
|
||||
bh_platform_init(void);
|
||||
|
||||
/**
|
||||
* Destroy the platform internal resources if needed,
|
||||
* this function is called by wasm_runtime_destroy()
|
||||
*/
|
||||
void
|
||||
bh_platform_destroy(void);
|
||||
|
||||
/**
|
||||
******** memory allocator APIs **********
|
||||
*/
|
||||
|
||||
void *
|
||||
os_malloc(unsigned size);
|
||||
|
||||
void *
|
||||
os_realloc(void *ptr, unsigned size);
|
||||
|
||||
void
|
||||
os_free(void *ptr);
|
||||
|
||||
/**
|
||||
* Note: the above APIs can simply return NULL if wasm runtime
|
||||
* isn't initialized with Alloc_With_System_Allocator.
|
||||
* Refer to wasm_runtime_full_init().
|
||||
*/
|
||||
|
||||
int
|
||||
os_printf(const char *format, ...);
|
||||
|
||||
int
|
||||
os_vprintf(const char *format, va_list ap);
|
||||
|
||||
/**
|
||||
* Get microseconds after boot.
|
||||
*/
|
||||
uint64
|
||||
os_time_get_boot_us(void);
|
||||
|
||||
/**
|
||||
* Get thread-specific CPU-time clock in microseconds
|
||||
*/
|
||||
uint64
|
||||
os_time_thread_cputime_us(void);
|
||||
|
||||
/**
|
||||
* Get current thread id.
|
||||
* Implementation optional: Used by runtime for logging only.
|
||||
*/
|
||||
korp_tid
|
||||
os_self_thread(void);
|
||||
|
||||
/**
|
||||
* Get current thread's stack boundary address, used for runtime
|
||||
* to check the native stack overflow. Return NULL if it is not
|
||||
* easy to implement, but may have potential issue.
|
||||
*/
|
||||
uint8 *
|
||||
os_thread_get_stack_boundary(void);
|
||||
|
||||
/**
|
||||
* Set whether the MAP_JIT region write protection is enabled for this thread.
|
||||
* Pass true to make the region executable, false to make it writable.
|
||||
*/
|
||||
void
|
||||
os_thread_jit_write_protect_np(bool enabled);
|
||||
|
||||
/**
|
||||
************** mutext APIs ***********
|
||||
* vmcore: Not required until pthread is supported by runtime
|
||||
* app-mgr: Must be implemented
|
||||
*/
|
||||
|
||||
int
|
||||
os_mutex_init(korp_mutex *mutex);
|
||||
|
||||
int
|
||||
os_mutex_destroy(korp_mutex *mutex);
|
||||
|
||||
int
|
||||
os_mutex_lock(korp_mutex *mutex);
|
||||
|
||||
int
|
||||
os_mutex_unlock(korp_mutex *mutex);
|
||||
|
||||
/**************************************************
|
||||
* Section 2 *
|
||||
* APIs required by WAMR AOT *
|
||||
**************************************************/
|
||||
|
||||
/* Memory map modes */
|
||||
enum {
|
||||
MMAP_PROT_NONE = 0,
|
||||
MMAP_PROT_READ = 1,
|
||||
MMAP_PROT_WRITE = 2,
|
||||
MMAP_PROT_EXEC = 4
|
||||
};
|
||||
|
||||
/* Memory map 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 *
|
||||
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file);
|
||||
void
|
||||
os_munmap(void *addr, size_t size);
|
||||
int
|
||||
os_mprotect(void *addr, size_t size, int prot);
|
||||
|
||||
static inline void *
|
||||
os_mremap_slow(void *old_addr, size_t old_size, size_t new_size)
|
||||
{
|
||||
void *new_memory = os_mmap(NULL, new_size, MMAP_PROT_WRITE | MMAP_PROT_READ,
|
||||
0, os_get_invalid_handle());
|
||||
if (!new_memory) {
|
||||
return NULL;
|
||||
}
|
||||
/*
|
||||
* bh_memcpy_s can't be used as it doesn't support values bigger than
|
||||
* UINT32_MAX
|
||||
*/
|
||||
memcpy(new_memory, old_addr, new_size < old_size ? new_size : old_size);
|
||||
os_munmap(old_addr, old_size);
|
||||
|
||||
return new_memory;
|
||||
}
|
||||
|
||||
/* Doesn't guarantee that protection flags will be preserved.
|
||||
os_mprotect() must be called after remapping. */
|
||||
void *
|
||||
os_mremap(void *old_addr, size_t old_size, size_t new_size);
|
||||
|
||||
#if (WASM_MEM_DUAL_BUS_MIRROR != 0)
|
||||
void *
|
||||
os_get_dbus_mirror(void *ibus);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Flush cpu data cache, in some CPUs, after applying relocation to the
|
||||
* AOT code, the code may haven't been written back to the cpu data cache,
|
||||
* which may cause unexpected behaviour when executing the AOT code.
|
||||
* Implement this function if required, or just leave it empty.
|
||||
*/
|
||||
void
|
||||
os_dcache_flush(void);
|
||||
|
||||
/**
|
||||
* Flush instruction cache.
|
||||
*/
|
||||
void
|
||||
os_icache_flush(void *start, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* #ifndef _PLATFORM_API_VMCORE_H */
|
||||
209
core/shared/platform/include/baremetal/platform_common.h
Normal file
209
core/shared/platform/include/baremetal/platform_common.h
Normal file
@ -0,0 +1,209 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _PLATFORM_COMMON_H
|
||||
#define _PLATFORM_COMMON_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "platform_internal.h"
|
||||
#include "../../../../config.h"
|
||||
|
||||
#define BH_MAX_THREAD 32
|
||||
|
||||
#define BHT_ERROR (-1)
|
||||
#define BHT_TIMED_OUT (1)
|
||||
#define BHT_OK (0)
|
||||
|
||||
#define BHT_WAIT_FOREVER ((uint64) - 1LL)
|
||||
|
||||
#define BH_KB (1024)
|
||||
#define BH_MB ((BH_KB) * 1024)
|
||||
#define BH_GB ((BH_MB) * 1024)
|
||||
|
||||
#ifndef BH_MALLOC
|
||||
#define BH_MALLOC os_malloc
|
||||
#endif
|
||||
|
||||
#ifndef BH_FREE
|
||||
#define BH_FREE os_free
|
||||
#endif
|
||||
|
||||
#ifndef BH_TIME_T_MAX
|
||||
#define BH_TIME_T_MAX LONG_MAX
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_BUILD)
|
||||
#if defined(COMPILING_WASM_RUNTIME_API)
|
||||
__declspec(dllexport) void *
|
||||
BH_MALLOC(unsigned int size);
|
||||
__declspec(dllexport) void
|
||||
BH_FREE(void *ptr);
|
||||
#else
|
||||
__declspec(dllimport) void *
|
||||
BH_MALLOC(unsigned int size);
|
||||
__declspec(dllimport) void
|
||||
BH_FREE(void *ptr);
|
||||
#endif
|
||||
#else
|
||||
void *
|
||||
BH_MALLOC(unsigned int size);
|
||||
void
|
||||
BH_FREE(void *ptr);
|
||||
#endif
|
||||
|
||||
#if defined(BH_VPRINTF)
|
||||
#if defined(MSVC)
|
||||
__declspec(dllimport) int
|
||||
BH_VPRINTF(const char *format, va_list ap);
|
||||
#else
|
||||
int
|
||||
BH_VPRINTF(const char *format, va_list ap);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL (void *)0
|
||||
#endif
|
||||
|
||||
#if !defined(BH_HAS_DLFCN)
|
||||
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
|
||||
#define BH_HAS_DLFCN 1
|
||||
#else
|
||||
#define BH_HAS_DLFCN 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef __cplusplus
|
||||
|
||||
#ifndef true
|
||||
#define true 1
|
||||
#endif
|
||||
|
||||
#ifndef false
|
||||
#define false 0
|
||||
#endif
|
||||
|
||||
#ifndef inline
|
||||
#define inline __inline
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/* Return the offset of the given field in the given type */
|
||||
#ifndef offsetof
|
||||
/* GCC 4.0 and later has the builtin. */
|
||||
#if defined(__GNUC__) && __GNUC__ >= 4
|
||||
#define offsetof(Type, field) __builtin_offsetof(Type, field)
|
||||
#else
|
||||
#define offsetof(Type, field) ((size_t)(&((Type *)0)->field))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef uint8_t uint8;
|
||||
typedef int8_t int8;
|
||||
typedef uint16_t uint16;
|
||||
typedef int16_t int16;
|
||||
typedef uint32_t uint32;
|
||||
typedef int32_t int32;
|
||||
typedef float float32;
|
||||
typedef double float64;
|
||||
typedef uint64_t uint64;
|
||||
typedef int64_t int64;
|
||||
|
||||
typedef void *(*thread_start_routine_t)(void *);
|
||||
|
||||
#ifndef bh_socket_t
|
||||
/* If no socket defined on current platform,
|
||||
give a fake definition to make the compiler happy */
|
||||
#define bh_socket_t int
|
||||
#endif
|
||||
|
||||
/* Format specifiers macros in case
|
||||
they are not provided by compiler */
|
||||
#ifndef __PRI64_PREFIX
|
||||
#if UINTPTR_MAX == UINT64_MAX
|
||||
#define __PRI64_PREFIX "l"
|
||||
#define __PRIPTR_PREFIX "l"
|
||||
#else
|
||||
#define __PRI64_PREFIX "ll"
|
||||
#define __PRIPTR_PREFIX
|
||||
#endif
|
||||
#endif /* #ifndef __PRI64_PREFIX */
|
||||
|
||||
/* Macros for printing format specifiers */
|
||||
#ifndef PRId32
|
||||
#define PRId32 "d"
|
||||
#endif
|
||||
#ifndef PRIi32
|
||||
#define PRIi32 "i"
|
||||
#endif
|
||||
#ifndef PRIu32
|
||||
#define PRIu32 "u"
|
||||
#endif
|
||||
#ifndef PRIx32
|
||||
#define PRIx32 "x"
|
||||
#endif
|
||||
#ifndef PRIX32
|
||||
#define PRIX32 "X"
|
||||
#endif
|
||||
|
||||
#ifndef PRId64
|
||||
#define PRId64 __PRI64_PREFIX "d"
|
||||
#endif
|
||||
#ifndef PRIu64
|
||||
#define PRIu64 __PRI64_PREFIX "u"
|
||||
#endif
|
||||
#ifndef PRIx64
|
||||
#define PRIx64 __PRI64_PREFIX "x"
|
||||
#endif
|
||||
#ifndef PRIX64
|
||||
#define PRIX64 __PRI64_PREFIX "X"
|
||||
#endif
|
||||
#ifndef PRIxPTR
|
||||
#define PRIxPTR __PRIPTR_PREFIX "x"
|
||||
#endif
|
||||
#ifndef PRIXPTR
|
||||
#define PRIXPTR __PRIPTR_PREFIX "X"
|
||||
#endif
|
||||
|
||||
/* Macros for scanning format specifiers */
|
||||
#ifndef SCNd32
|
||||
#define SCNd32 "d"
|
||||
#endif
|
||||
#ifndef SCNi32
|
||||
#define SCNi32 "i"
|
||||
#endif
|
||||
#ifndef SCNu32
|
||||
#define SCNu32 "u"
|
||||
#endif
|
||||
#ifndef SCNx32
|
||||
#define SCNx32 "x"
|
||||
#endif
|
||||
|
||||
#ifndef SCNd64
|
||||
#define SCNd64 __PRI64_PREFIX "d"
|
||||
#endif
|
||||
#ifndef SCNu64
|
||||
#define SCNu64 __PRI64_PREFIX "u"
|
||||
#endif
|
||||
#ifndef SCNx64
|
||||
#define SCNx64 __PRI64_PREFIX "x"
|
||||
#endif
|
||||
#ifndef SCNxPTR
|
||||
#define SCNxPTR __PRIPTR_PREFIX "x"
|
||||
#endif
|
||||
|
||||
#ifndef NAN
|
||||
#define NAN (0.0 / 0.0)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* #ifndef _PLATFORM_COMMON_H */
|
||||
Reference in New Issue
Block a user