From 12d0be482eac9a4447c1a448fb7de0feef55d933 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Wed, 28 Jan 2026 18:54:18 +0100 Subject: [PATCH] baremetal: add baremetal platform --- CMakeLists.txt | 2 +- .../shared/platform/baremetal/platform_init.c | 120 ++++++++++++++++++ .../platform/baremetal/platform_internal.h | 72 +++++++++++ .../platform/baremetal/shared_platform.cmake | 12 ++ 4 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 core/shared/platform/baremetal/platform_init.c create mode 100644 core/shared/platform/baremetal/platform_internal.h create mode 100644 core/shared/platform/baremetal/shared_platform.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b28fa89..6031b7c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,7 +150,7 @@ endif () include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) set (THREADS_PREFER_PTHREAD_FLAG ON) -find_package(Threads REQUIRED) +# find_package(Threads REQUIRED) add_library (vmlib ${WAMR_RUNTIME_LIB_SOURCE}) set_target_properties (vmlib PROPERTIES OUTPUT_NAME iwasm) diff --git a/core/shared/platform/baremetal/platform_init.c b/core/shared/platform/baremetal/platform_init.c new file mode 100644 index 00000000..709e34b3 --- /dev/null +++ b/core/shared/platform/baremetal/platform_init.c @@ -0,0 +1,120 @@ +#include "platform_api_vmcore.h" + +/**************************************************** + * 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() +{ + return 0; +} + +void +bh_platform_destroy() +{ +} + +/** + ******** 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, ...) +{ + return 0; +} + +int +os_vprintf(const char *format, va_list ap) +{ + return 0; +} + +/** + * 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) +{ + return 0; +} + +int +os_mutex_destroy(korp_mutex *mutex) +{ + return 0; +} + +int +os_mutex_lock(korp_mutex *mutex) +{ + return 0; +} + +int +os_mutex_unlock(korp_mutex *mutex) +{ + return 0; +} diff --git a/core/shared/platform/baremetal/platform_internal.h b/core/shared/platform/baremetal/platform_internal.h new file mode 100644 index 00000000..c44b0bfb --- /dev/null +++ b/core/shared/platform/baremetal/platform_internal.h @@ -0,0 +1,72 @@ +#ifndef _PLATFORM_INTERNAL_H +#define _PLATFORM_INTERNAL_H + +#include +#include +// #include +// #include +#include +#include +#include +#include +// #include +#include +// #include +// #include +#include +// #include +#include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef BH_PLATFORM_BAREMETAL +#define BH_PLATFORM_BAREMETAL +#endif + +#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024) +#define BH_THREAD_DEFAULT_PRIORITY 0 + +typedef pthread_t korp_tid; +typedef pthread_mutex_t korp_mutex; +typedef pthread_cond_t korp_cond; +typedef pthread_t korp_thread; +typedef pthread_rwlock_t korp_rwlock; +typedef sem_t korp_sem; + +typedef int os_file_handle; +typedef DIR *os_dir_stream; +typedef int os_raw_file_handle; + +static inline os_file_handle +os_get_invalid_handle(void) +{ + return -1; +} + +static inline int +os_getpagesize() +{ + return 4096; +} + +#ifdef __cplusplus +} +#endif + +#endif /* end of _PLATFORM_INTERNAL_H */ diff --git a/core/shared/platform/baremetal/shared_platform.cmake b/core/shared/platform/baremetal/shared_platform.cmake new file mode 100644 index 00000000..a123d693 --- /dev/null +++ b/core/shared/platform/baremetal/shared_platform.cmake @@ -0,0 +1,12 @@ +set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR}) + +add_definitions(-DBH_PLATFORM_BAREMETAL) + +include_directories(${PLATFORM_SHARED_DIR}) +include_directories(${PLATFORM_SHARED_DIR}/../include) + +file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c) +set (PLATFORM_SHARED_SOURCE ${source_all}) + +file (GLOB header ${PLATFORM_SHARED_DIR}/../include/baremetal/*.h) +LIST (APPEND RUNTIME_LIB_HEADER_LIST ${header})