baremetal: add baremetal platform

This commit is contained in:
2026-01-28 18:54:18 +01:00
parent 888655330c
commit 3d39486c25
4 changed files with 409 additions and 1 deletions

View File

@ -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)

View File

@ -0,0 +1,206 @@
#include "platform_api_vmcore.h"
// int
// os_rwlock_init(korp_rwlock *rwlock)
// {
// if (!rwlock)
// return -1;
// rwlock->readers = 0;
// rwlock->writer = false;
// return 0;
// }
//
// int
// os_rwlock_destroy(korp_rwlock *rwlock)
// {
// if (!rwlock)
// return -1;
// return 0;
// }
//
// int
// os_rwlock_rdlock(korp_rwlock *rwlock)
// {
// if (!rwlock)
// return -1;
//
// // Wait until no writer
// while (__atomic_load_n(&rwlock->writer, __ATOMIC_ACQUIRE))
// ;
//
// // Increment readers count
// __atomic_fetch_add(&rwlock->readers, 1, __ATOMIC_ACQUIRE);
// return 0;
// }
//
// int
// os_rwlock_wrlock(korp_rwlock *rwlock)
// {
// if (!rwlock)
// return -1;
//
// // Wait until no readers and no writer
// while (__atomic_load_n(&rwlock->readers, __ATOMIC_ACQUIRE) > 0
// || __atomic_test_and_set(&rwlock->writer, __ATOMIC_ACQUIRE))
// ;
//
// return 0;
// }
//
// int
// os_rwlock_unlock(korp_rwlock *rwlock)
// {
// if (!rwlock)
// return -1;
//
// if (__atomic_load_n(&rwlock->writer, __ATOMIC_ACQUIRE)) {
// // Writer releasing
// __atomic_clear(&rwlock->writer, __ATOMIC_RELEASE);
// }
// else {
// // Reader releasing
// __atomic_fetch_sub(&rwlock->readers, 1, __ATOMIC_RELEASE);
// }
// return 0;
// }
/****************************************************
* 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)
{
return NULL;
}
void *
os_realloc(void *ptr, unsigned size)
{
return NULL;
}
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)
{
return 0;
}
/**
* Get thread-specific CPU-time clock in microseconds
*/
uint64
os_time_thread_cputime_us(void)
{
return 0;
}
/**
* Get current thread id.
* Implementation optional: Used by runtime for logging only.
*/
korp_tid
os_self_thread(void)
{
return 0;
}
/**
* 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)
{
return NULL;
}
/**
* 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;
}

View File

@ -0,0 +1,190 @@
#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 <stdarg.h>
#include <ctype.h>
#include <pthread.h>
// #include <signal.h>
#include <semaphore.h>
// #include <limits.h>
#include <dirent.h>
// #include <fcntl.h>
// #include <unistd.h>
// #include <poll.h>
// #include <sched.h>
// #include <errno.h>
// #include <sys/types.h>
// #include <sys/stat.h>
// #include <sys/mman.h>
// #include <sys/time.h>
// #include <sys/timeb.h>
// #include <sys/uio.h>
// #include <sys/ioctl.h>
// #include <sys/socket.h>
// #include <sys/resource.h>
#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 struct {
volatile uint32_t readers;
volatile bool writer;
} korp_rwlock;
// int
// os_rwlock_init(korp_rwlock *rwlock);
// int
// os_rwlock_destroy(korp_rwlock *rwlock);
// int
// os_rwlock_rdlock(korp_rwlock *rwlock);
// int
// os_rwlock_wrlock(korp_rwlock *rwlock);
// int
// os_rwlock_unlock(korp_rwlock *rwlock);
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()
{
// TODO: What pagesize?
return 4096;
}
// https://git.musl-libc.org/cgit/musl/tree/src/string/memcpy.c
__attribute__((weak)) void *
memcpy(void *restrict dest, const void *restrict src, size_t n)
{
unsigned char *d = dest;
const unsigned char *s = src;
for (; n; n--)
*d++ = *s++;
return dest;
}
// https://git.musl-libc.org/cgit/musl/tree/src/string/memmove.c
__attribute__((weak)) void *
memmove(void *dest, const void *src, size_t n)
{
char *d = dest;
const char *s = src;
if (d == s)
return d;
if ((uintptr_t)s - (uintptr_t)d - n <= -2 * n)
return memcpy(d, s, n);
if (d < s) {
for (; n; n--)
*d++ = *s++;
}
else {
while (n)
n--, d[n] = s[n];
}
return dest;
}
// https://git.musl-libc.org/cgit/musl/tree/src/string/memset.c
__attribute__((weak)) void *
memset(void *dest, int c, size_t n)
{
unsigned char *s = dest;
size_t k;
/* Fill head and tail with minimal branching. Each
* conditional ensures that all the subsequently used
* offsets are well-defined and in the dest region. */
if (!n)
return dest;
s[0] = c;
s[n - 1] = c;
if (n <= 2)
return dest;
s[1] = c;
s[2] = c;
s[n - 2] = c;
s[n - 3] = c;
if (n <= 6)
return dest;
s[3] = c;
s[n - 4] = c;
if (n <= 8)
return dest;
/* Advance pointer to align it at a 4-byte boundary,
* and truncate n to a multiple of 4. The previous code
* already took care of any head/tail that get cut off
* by the alignment. */
k = -(uintptr_t)s & 3;
s += k;
n -= k;
n &= -4;
for (; n; n--, s++)
*s = c;
return dest;
}
// https://github.com/zerovm/glibc/blob/master/string/strcmp.c
__attribute__((weak)) int
strcmp(const char *s1, const char *s2)
{
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}
uint32_t
__stack_chk_fail_local()
{
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* end of _PLATFORM_INTERNAL_H */

View File

@ -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})