baremetal: second try (with i386-elf-gcc)

This commit is contained in:
2026-02-23 17:59:16 +01:00
parent e426ea39e3
commit 0e8e3d07c3
5 changed files with 415 additions and 11 deletions

View File

@ -2,6 +2,7 @@
// Many implementations taken from musl: https://git.musl-libc.org
#ifdef NODEF
typedef int (*cmpfun)(const void *, const void *);
void
@ -183,11 +184,13 @@ os_cond_destroy(korp_cond *cond)
{
return 0;
}
#endif
/****************************************************
* string
****************************************************/
#ifdef NODEF
static char *
twobyte_strstr(const unsigned char *h, const unsigned char *n)
{
@ -381,6 +384,7 @@ strstr(const char *h, const char *n)
return twoway_strstr((void *)h, (void *)n);
}
#endif
__attribute__((weak)) void *
memcpy(void *restrict dest, const void *restrict src, size_t n)
@ -459,6 +463,7 @@ memset(void *dest, int c, size_t n)
return dest;
}
#ifdef NODEF
__attribute__((weak)) int
memcmp(const void *vl, const void *vr, size_t n)
{
@ -611,11 +616,13 @@ strncasecmp(const char *_l, const char *_r, size_t n)
;
return my_tolower(*l) - my_tolower(*r);
}
#endif
/****************************************************
* stdio
****************************************************/
#ifdef NODEF
__attribute__((weak)) int
snprintf(char *restrict s, size_t n, const char *restrict fmt, ...)
{
@ -644,6 +651,7 @@ __vsnprintf_chk(char *s, size_t n, int m, size_t o, const char *fmt,
{
return 0;
}
#endif
/****************************************************
* mman
@ -676,6 +684,7 @@ os_mprotect(void *addr, size_t size, int prot)
* lgcc/math
****************************************************/
#ifdef NODEF
double
sqrt(double x)
{
@ -688,7 +697,6 @@ sqrtf(float x)
return 0.0;
}
/*
#define arith64_u64 unsigned long long int
#define arith64_s64 signed long long int
#define arith64_u32 unsigned int
@ -1002,17 +1010,17 @@ __attribute__((weak)) arith64_u64
__udivmoddi4(arith64_u64 a, arith64_u64 b, arith64_u64 *c)
{
//
https://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html#index-_005f_005fudivmoddi4
https: // gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html#index-_005f_005fudivmoddi4
*c = __umoddi3(a, b);
return __udivdi3(a, b);
}
*/
#endif
/****************************************************
* rwlock
****************************************************/
/*
#ifdef NODEF
int
os_rwlock_init(korp_rwlock *rwlock)
{
@ -1076,7 +1084,7 @@ os_rwlock_unlock(korp_rwlock *rwlock)
}
return 0;
}
*/
#endif
/****************************************************
* Section 1 *

View File

@ -13,7 +13,7 @@
#include <ctype.h>
#include <pthread.h>
// #include <signal.h>
#include <semaphore.h>
// #include <semaphore.h>
// #include <limits.h>
#include <dirent.h>
// #include <fcntl.h>
@ -46,7 +46,7 @@ typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef sem_t korp_sem;
// typedef sem_t korp_sem;
// typedef pthread_rwlock_t korp_rwlock;
typedef struct {

View File

@ -3,10 +3,10 @@ set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
add_definitions(-DBH_PLATFORM_BAREMETAL)
include_directories(${PLATFORM_SHARED_DIR})
include_directories(${PLATFORM_SHARED_DIR}/../include)
# include_directories(${PLATFORM_SHARED_DIR}/../include)
file (GLOB header ${PLATFORM_SHARED_DIR}/../include/baremetal/*.h)
LIST (APPEND RUNTIME_LIB_HEADER_LIST ${header})
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})

View 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 */

View File

@ -0,0 +1,204 @@
/*
* 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 */