From f1fe5d7872a2aaae98ede5d0c3aab0bbde8fcefa Mon Sep 17 00:00:00 2001 From: Karl Fessel Date: Fri, 16 Oct 2020 05:21:53 +0200 Subject: [PATCH] add RIOT-OS support to WAMR (#425) * add RIOT platform see riot-os.org * add simple RIOT example --- core/shared/platform/riot/platform_internal.h | 75 +++ core/shared/platform/riot/riot_platform.c | 73 +++ core/shared/platform/riot/riot_thread.c | 429 ++++++++++++++++++ core/shared/platform/riot/riot_time.c | 15 + .../platform/riot/shared_platform.cmake | 17 + product-mini/platforms/riot/CMakeLists.txt | 59 +++ product-mini/platforms/riot/Makefile | 96 ++++ product-mini/platforms/riot/iwasmt.c | 148 ++++++ product-mini/platforms/riot/test_wasm.h | 46 ++ 9 files changed, 958 insertions(+) create mode 100644 core/shared/platform/riot/platform_internal.h create mode 100644 core/shared/platform/riot/riot_platform.c create mode 100644 core/shared/platform/riot/riot_thread.c create mode 100644 core/shared/platform/riot/riot_time.c create mode 100644 core/shared/platform/riot/shared_platform.cmake create mode 100644 product-mini/platforms/riot/CMakeLists.txt create mode 100644 product-mini/platforms/riot/Makefile create mode 100644 product-mini/platforms/riot/iwasmt.c create mode 100644 product-mini/platforms/riot/test_wasm.h diff --git a/core/shared/platform/riot/platform_internal.h b/core/shared/platform/riot/platform_internal.h new file mode 100644 index 00000000..0f79c6f7 --- /dev/null +++ b/core/shared/platform/riot/platform_internal.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * Copyright (C) 2020 TU Bergakademie Freiberg Karl Fessel + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _PLATFORM_INTERNAL_H +#define _PLATFORM_INTERNAL_H + +//Riot includes core +#include +#include +#include + +//Riot includes sys +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef BH_PLATFORM_RIOT +#define BH_PLATFORM_RIOT +#endif + +#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB) + +/* Default thread priority */ +#define BH_THREAD_DEFAULT_PRIORITY 7 + +typedef thread_t korp_thread; +typedef kernel_pid_t korp_tid; +typedef mutex_t korp_mutex; + +// typedef sema_t korp_sem; + +struct os_thread_wait_node; +typedef struct os_thread_wait_node *os_thread_wait_list; +typedef struct korp_cond { + mutex_t wait_list_lock; + os_thread_wait_list thread_wait_list; +} korp_cond; + +#define os_printf printf +#define os_vprintf vprintf + +#if WA_MATH +/* math functions which are not provided by os*/ +double sqrt(double x); +double floor(double x); +double ceil(double x); +double fmin(double x, double y); +double fmax(double x, double y); +double rint(double x); +double fabs(double x); +double trunc(double x); +float floorf(float x); +float ceilf(float x); +float fminf(float x, float y); +float fmaxf(float x, float y); +float rintf(float x); +float truncf(float x); +int signbit(double x); +int isnan(double x); +#endif + +#endif /* end of _BH_PLATFORM_H */ + diff --git a/core/shared/platform/riot/riot_platform.c b/core/shared/platform/riot/riot_platform.c new file mode 100644 index 00000000..bad69076 --- /dev/null +++ b/core/shared/platform/riot/riot_platform.c @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * Copyright (C) 2020 TU Bergakademie Freiberg Karl Fessel + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "platform_api_vmcore.h" +#include "platform_api_extension.h" + +int +os_thread_sys_init(void); + +void +os_thread_sys_destroy(void); + +int +bh_platform_init(void) +{ + return os_thread_sys_init(); +} + +void +bh_platform_destroy(void) +{ + os_thread_sys_destroy(); +} + +void * +os_malloc(unsigned size) +{ + return malloc(size); +} + +void * +os_realloc(void *ptr, unsigned size) +{ + return realloc(ptr, size); +} + +void +os_free(void *ptr) +{ + free(ptr); +} + +void * +os_mmap(void *hint, unsigned int size, int prot, int flags) +{ + return BH_MALLOC(size); +} + +void +os_munmap(void *addr, size_t size) +{ + return BH_FREE(addr); +} + +int +os_mprotect(void *addr, size_t size, int prot) +{ + return 0; +} + +void +os_dcache_flush(void) +{ +#if defined(CONFIG_CPU_CORTEX_M7) && defined(CONFIG_ARM_MPU) + uint32 key; + key = irq_lock(); + SCB_CleanDCache(); + irq_unlock(key); +#endif +} diff --git a/core/shared/platform/riot/riot_thread.c b/core/shared/platform/riot/riot_thread.c new file mode 100644 index 00000000..75576114 --- /dev/null +++ b/core/shared/platform/riot/riot_thread.c @@ -0,0 +1,429 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * Copyright (C) 2020 TU Bergakademie Freiberg Karl Fessel + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "platform_api_vmcore.h" +#include "platform_api_extension.h" + +#include + +#define bh_assert(v) do { \ + if (!(v)) { \ + printf("\nASSERTION FAILED: %s, at %s, line %d\n", \ + #v, __FILE__, __LINE__); \ + core_panic(0,0/*expr_string*/); \ + while (1); \ + } \ + } while (0) + +struct os_thread_data; +typedef struct os_thread_wait_node { + sema_t sem; + void * ret; + os_thread_wait_list next; +} os_thread_wait_node; + +// all information for thread to cleanup it self +typedef struct os_thread_data { + /* Next thread data */ + struct os_thread_data *next; + /* thread handle */ + kernel_pid_t tid; + /* Thread start routine */ + thread_start_routine_t start_routine; + /* Thread start routine argument */ + void *arg; + /* thread local root */ + void *tlr; + /* Lock for waiting list */ + mutex_t wait_list_lock; + /* Waiting list of other threads who are joining this thread */ + os_thread_wait_list thread_wait_list; + /* Thread stack size */ + unsigned stack_size; + /* Thread stack */ + char stack[1]; +} os_thread_data; + +typedef struct os_thread_obj { + korp_tid thread; + /* Whether the thread is terminated and this thread object is to + be freed in the future. */ + bool to_be_freed; + struct os_thread_obj *next; +} os_thread_obj; + +static bool is_thread_sys_inited = false; + +/* Lock for thread data list */ +static mutex_t thread_data_lock; + +/* Thread data list */ +static os_thread_data *thread_data_list = NULL; + +static void thread_data_list_add(os_thread_data *thread_data) +{ + mutex_lock(&thread_data_lock); + if (!thread_data_list) + thread_data_list = thread_data; + else { + /* If already in list, just return */ + os_thread_data *p = thread_data_list; + while (p) { + if (p == thread_data) { + mutex_unlock(&thread_data_lock); + return; + } + p = p->next; + } + + /* Set as head of list */ + thread_data->next = thread_data_list; + thread_data_list = thread_data; + } + mutex_unlock(&thread_data_lock); +} + +static void thread_data_list_remove(os_thread_data *thread_data) +{ + mutex_lock(&thread_data_lock); + if (thread_data_list) { + if (thread_data_list == thread_data) + thread_data_list = thread_data_list->next; + else { + /* Search and remove it from list */ + os_thread_data *p = thread_data_list; + while (p && p->next != thread_data) + p = p->next; + if (p && p->next == thread_data) + p->next = p->next->next; + } + } + mutex_unlock(&thread_data_lock); +} + +static os_thread_data * +thread_data_list_lookup(korp_tid tid) +{ + mutex_lock(&thread_data_lock); + if (thread_data_list) { + os_thread_data *p = thread_data_list; + while (p) { + if (p->tid == tid) { + /* Found */ + mutex_unlock(&thread_data_lock); + return p; + } + p = p->next; + } + } + mutex_unlock(&thread_data_lock); + return NULL; +} + +int +os_thread_sys_init() +{ + if (is_thread_sys_inited) + return BHT_OK; + + mutex_init(&thread_data_lock); + + is_thread_sys_inited = true; + return BHT_OK; +} + +void +os_thread_sys_destroy() +{ + if (is_thread_sys_inited) { + is_thread_sys_inited = false; + } +} + +static os_thread_data * +thread_data_current() +{ + kernel_pid_t tid = thread_getpid(); + return thread_data_list_lookup(tid); +} + +static void +os_thread_cleanup(void) +{ + //TODO Check this (Join sema trigger, cleanup of thread_data) + os_thread_data *thread_data = thread_data_current(); + bh_assert(thread_data != NULL); + mutex_lock(&thread_data->wait_list_lock); + if (thread_data->thread_wait_list) { + /* Signal each joining thread */ + os_thread_wait_list head = thread_data->thread_wait_list; + while (head) { + os_thread_wait_list next = head->next; + head->ret = thread_data->arg; + sema_post(&head->sem); + head = next; + } + thread_data->thread_wait_list = NULL; + } + mutex_unlock(&thread_data->wait_list_lock); + + thread_data_list_remove(thread_data); +} + +static void * +os_thread_wrapper(void *thread_data) +{ + /* Set thread custom data */ + os_thread_data * t = (os_thread_data*) thread_data; + t->tid = thread_getpid(); + thread_data_list_add(t); + + // save the return value to arg since it is not need after the call + t->arg = (t->start_routine)(t->arg); + + os_thread_cleanup(); // internal structures and joiners + + BH_FREE(thread_data); + sched_task_exit();// stop thread //clean + return NULL; //never reached +} + +int +os_thread_create(korp_tid *p_tid, thread_start_routine_t start, + void *arg, unsigned int stack_size) +{ + return os_thread_create_with_prio(p_tid, start, arg, stack_size, + BH_THREAD_DEFAULT_PRIORITY); +} + +int +os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, + void *arg, unsigned int stack_size, int prio) +{ + kernel_pid_t tid; + os_thread_data *thread_data; + unsigned thread_data_size; + + if (!p_tid || !stack_size) + return BHT_ERROR; + + /* Create and initialize thread data */ + thread_data_size = offsetof(os_thread_data, stack) + stack_size; + if (!(thread_data = BH_MALLOC(thread_data_size))) { + return BHT_ERROR; + } + + memset(thread_data, 0, thread_data_size); + mutex_init(&thread_data->wait_list_lock); + thread_data->stack_size = stack_size; + thread_data->start_routine = start; + thread_data->arg = arg; + + /* Create the thread &*/ + if (!((tid = thread_create( thread_data->stack, + stack_size,prio,0, os_thread_wrapper, thread_data,"WASM")))) { + BH_FREE(thread_data); + return BHT_ERROR; + } + + thread_data->tid = tid; + + /* Set thread custom data */ + thread_data_list_add(thread_data); + *p_tid = tid; + return BHT_OK; +} + +korp_tid +os_self_thread() +{ + return (korp_tid) thread_getpid(); +} + +int +os_thread_join (korp_tid thread, void **value_ptr) +{ + // will test if thread is still working, + // wait if it is +// (void) value_ptr; + os_thread_data *thread_data; + os_thread_wait_node node; + + sema_create(&node.sem, 0); + node.next = NULL; + + /* Get thread data */ + thread_data = thread_data_list_lookup(thread); + if(thread_data == NULL){ + //thread not found + sema_destroy(&node.sem); + return BHT_ERROR; + } + bh_assert(thread_data != NULL); + + mutex_lock(&thread_data->wait_list_lock); + if (!thread_data->thread_wait_list) + thread_data->thread_wait_list = &node; + else { + /* Add to end of waiting list */ + os_thread_wait_node *p = thread_data->thread_wait_list; + while (p->next) + p = p->next; + p->next = &node; + } + mutex_unlock(&thread_data->wait_list_lock); + + sema_wait(&node.sem); + //get the return value pointer conted may not be availible after return + if(value_ptr) (* value_ptr) = node.ret; + /* Wait some time for the thread to be actually terminated */ +// TODO: k_sleep(100); + + //TODO: bump target prio to make it finish and free its resources + thread_yield(); + + //node has done its job + sema_destroy(&node.sem); + + return BHT_OK; +} + + + +// int vm_mutex_trylock(korp_mutex *mutex) +// { +// return mutex_trylock(mutex); +// } + +int +os_mutex_init(korp_mutex *mutex) +{ + mutex_init(mutex); + return BHT_OK; +} + +int +os_mutex_destroy(korp_mutex *mutex) +{ + (void) mutex; + return BHT_OK; +} + +int +os_mutex_lock(korp_mutex *mutex) +{ + mutex_lock(mutex); + return 0; //Riot mutexes do not return until success +} + +int +os_mutex_unlock(korp_mutex *mutex) +{ + mutex_unlock(mutex); + return 0; //Riot mutexes do not return until success + +} + +int +os_cond_init(korp_cond *cond) +{ + mutex_init(&cond->wait_list_lock); + cond->thread_wait_list = NULL; + return BHT_OK; +} + +int +os_cond_destroy(korp_cond *cond) +{ + (void) cond; + return BHT_OK; +} + +static int +os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, + bool timed, uint64 useconds) +{ + os_thread_wait_node *node; + + /* Create wait node and append it to wait list */ + if (!(node = BH_MALLOC(sizeof(os_thread_wait_node)))) + return BHT_ERROR; + + sema_create(&node->sem, 0); + node->next = NULL; + + mutex_lock(&cond->wait_list_lock); + if (!cond->thread_wait_list) + cond->thread_wait_list = node; + else { + /* Add to end of wait list */ + os_thread_wait_node *p = cond->thread_wait_list; + while (p->next) + p = p->next; + p->next = node; + } + mutex_unlock(&cond->wait_list_lock); + + /* Unlock mutex, wait sem and lock mutex again */ + mutex_unlock(mutex); + if(timed) + sema_wait(&node->sem); + else + sema_wait_timed(&node->sem,useconds); + mutex_lock(mutex); + + /* Remove wait node from wait list */ + mutex_lock(&cond->wait_list_lock); + if (cond->thread_wait_list == node) + cond->thread_wait_list = node->next; + else { + /* Remove from the wait list */ + os_thread_wait_node *p = cond->thread_wait_list; + while (p->next != node) + p = p->next; + p->next = node->next; + } + BH_FREE(node); + mutex_unlock(&cond->wait_list_lock); + + return BHT_OK; +} + +int +os_cond_wait(korp_cond *cond, korp_mutex *mutex) +{ + return os_cond_wait_internal(cond, mutex, false, 0); +} + +int +os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int useconds) +{ + uint64 useconds64 = (uint64) useconds; + return os_cond_wait_internal(cond, mutex, + (useconds64 != BHT_WAIT_FOREVER), useconds64); +} + +int +os_cond_signal(korp_cond *cond) +{ + /* Signal the head wait node of wait list */ + mutex_lock(&cond->wait_list_lock); + if (cond->thread_wait_list) + sema_post(&cond->thread_wait_list->sem); + mutex_unlock(&cond->wait_list_lock); + + return BHT_OK; +} + +uint8 *os_thread_get_stack_boundary() +{ +#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \ + || defined(MODULE_MPU_STACK_GUARD) + return (uint8*)thread_get_active()->stack_start; +#else + return NULL; +#endif +} diff --git a/core/shared/platform/riot/riot_time.c b/core/shared/platform/riot/riot_time.c new file mode 100644 index 00000000..8b480f69 --- /dev/null +++ b/core/shared/platform/riot/riot_time.c @@ -0,0 +1,15 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * Copyright (C) 2020 TU Bergakademie Freiberg Karl Fessel + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "platform_api_vmcore.h" +#include + +uint64 +os_time_get_boot_microsecond() +{ + return xtimer_now_usec64(); +} + diff --git a/core/shared/platform/riot/shared_platform.cmake b/core/shared/platform/riot/shared_platform.cmake new file mode 100644 index 00000000..52cf9046 --- /dev/null +++ b/core/shared/platform/riot/shared_platform.cmake @@ -0,0 +1,17 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# Copyright (C) 2020 TU Bergakademie Freiberg Karl Fessel +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR}) + +add_definitions(-DBH_PLATFORM_RIOT) + +include_directories(${PLATFORM_SHARED_DIR}) +include_directories(${PLATFORM_SHARED_DIR}/../include) + +# include (${CMAKE_CURRENT_LIST_DIR}/../common/math/platform_api_math.cmake) + +file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c) + +set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_MATH_SOURCE}) + diff --git a/product-mini/platforms/riot/CMakeLists.txt b/product-mini/platforms/riot/CMakeLists.txt new file mode 100644 index 00000000..9b8b2229 --- /dev/null +++ b/product-mini/platforms/riot/CMakeLists.txt @@ -0,0 +1,59 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# Copyright (C) 2020 TU Bergakademie Freiberg Karl Fessel +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +cmake_minimum_required(VERSION 3.8.2) + +set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY") + +project(NONE) + +enable_language (ASM) + +set (WAMR_BUILD_PLATFORM "riot") + +# Build as X86_32 by default, change to "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", "MIPS" or "XTENSA" +# if we want to support arm, thumb, mips or xtensa + + +if (NOT DEFINED WAMR_BUILD_TARGET) + set (WAMR_BUILD_TARGET "X86_32") +endif () + +if (NOT DEFINED WAMR_BUILD_INTERP) + # Enable Interpreter by default + set (WAMR_BUILD_INTERP 1) +endif () + +if (NOT DEFINED WAMR_BUILD_AOT) + # Disable AOT by default. + set (WAMR_BUILD_AOT 0) +endif () + +if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN) + # Enable libc builtin support by default + set (WAMR_BUILD_LIBC_BUILTIN 1) +endif () + +if (NOT DEFINED WAMR_BUILD_LIBC_WASI) + # Disable libc wasi support by default + set (WAMR_BUILD_LIBC_WASI 0) +endif () + +if (NOT DEFINED WAMR_ROOT_DIR) + # this assumption is true if this file is copied to WAMR_ROOT + set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +endif () + + +include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) + +# need includes from RIOT prepare them as a cmake list +string(REGEX MATCHALL "([^\ ]+\ |[^\ ]+$)" RIOT_INCLUDES_LIST "${RIOT_INCLUDES}") + +include_directories(SYSTEM ${RIOT_INCLUDES_LIST}) + +# target_sources( ${WAMR_RUNTIME_LIB_SOURCE} ) +# executable linking is done by RIOT build system + +add_library( wamr ${WAMR_RUNTIME_LIB_SOURCE}) diff --git a/product-mini/platforms/riot/Makefile b/product-mini/platforms/riot/Makefile new file mode 100644 index 00000000..a6df9549 --- /dev/null +++ b/product-mini/platforms/riot/Makefile @@ -0,0 +1,96 @@ +APPLICATION = wamr-mini +# If no BOARD is defined in the environment, use this default: +BOARD ?= native + +# This has to be the absolute path to the RIOT base directory: +RIOTBASE ?= $(CURDIR)/../../../../RIOT + +USEMODULE += xtimer +USEMODULE += sema + +WPEDANTIC := 0 +WERROR := 0 + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +DEVELHELP ?= 1 + +# Change this to 0 show compiler invocation lines by default: +QUIET ?= 1 + +ARCHIVES += $(BINDIR)/libwamr.a + +#Load the usual RIOT make infastructure + +include $(RIOTBASE)/Makefile.include + + +WAMR_SOURCE = $(CURDIR)/../../.. +WAMR_BUILD_DIR = $(BINDIR)/wamr + +#less Wall TODO: get things fixed +CFLAGS := $(filter-out -pedantic, $(CFLAGS)) +CFLAGS += -Wno-format +CFLAGS += -Wno-strict-prototypes +CFLAGS += -Wno-old-style-definition +CFLAGS += -Wno-cast-function-type + +WAMR_CORE = $(WAMR_SOURCE)/core +IWASM_ROOT = $(WAMR_CORE)/iwasm +SHARED_LIB_ROOT = $(WAMR_CORE)/shared + +IWASM_INCLUDES += ${IWASM_ROOT}/include \ + ${SHARED_LIB_ROOT}/platform/include \ + ${SHARED_LIB_ROOT}/platform/riot \ + + +INCLUDES += $(addprefix -I,${IWASM_INCLUDES}) + + + +RIOT_INCLUDES = $(filter-out -%,$(subst -I,,$(INCLUDES))) + +#WAMR_BUILD_TARGET is "X86_32" "AARCH64[sub]", "ARM[sub]", +# "THUMB[sub]", "MIPS" or "XTENSA" +#no msp430, no AVR support for now + +#translate (CPU_ARCH) to Build Target +ifeq ($(CPU),native) +#$(CPU) is defined for every CPU +#Riot native is x86_32 + WAMR_BUILD_TARGET = X86_32 +else ifeq ($(findstring arm,$(CPU_ARCH)),arm) + WAMR_BUILD_TARGET = THUMB +else ifeq ($(CPU_ARCH),mips32r2) + WAMR_BUILD_TARGET = MIPS +else ifeq ($(CPU_ARCH),xtensa) + WAMR_BUILD_TARGET = XTENSA +endif + +ifeq ($(QUIET), 0) + CMAKEMAKEFLAGS += VERBOSE=1 +endif + + +$(BINDIR)/libwamr.a: $(WAMR_BUILD_DIR)/libwamr.a + cp $< $@ + +$(WAMR_BUILD_DIR)/libwamr.a: $(WAMR_BUILD_DIR)/Makefile + $(MAKE) -C $(WAMR_BUILD_DIR) $(CMAKEMAKEFLAGS) + +$(WAMR_BUILD_DIR)/Makefile: CMakeLists.txt + cmake -B$(WAMR_BUILD_DIR) \ + "-DRIOT_INCLUDES=$(RIOT_INCLUDES)"\ + -DWAMR_ROOT_DIR=$(WAMR_SOURCE)\ + -DWAMR_BUILD_TARGET=$(WAMR_BUILD_TARGET)\ + -DCMAKE_SYSTEM_NAME=Generic \ + -DCMAKE_SYSTEM_PROCESSOR="$(MCPU)" \ + -DCMAKE_C_COMPILER=$(CC) \ + -DCMAKE_C_COMPILER_WORKS=TRUE \ + +print_build_target: + @echo CPU_ARCH: $(CPU_ARCH) + @echo CPU: $(CPU) + @echo CFLAGS: $(CFLAGS) + @echo WAMR_BUILD_TARGET: $(WAMR_BUILD_TARGET) diff --git a/product-mini/platforms/riot/iwasmt.c b/product-mini/platforms/riot/iwasmt.c new file mode 100644 index 00000000..7f6b75fb --- /dev/null +++ b/product-mini/platforms/riot/iwasmt.c @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * Copyright (C) 2020 TU Bergakademie Freiberg Karl Fessel + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +// +#include +#include +#include + +#include + +// #include "platform_api_extension.h" +#include "wasm_export.h" + +#include + +/*provide some test program*/ +#include "test_wasm.h" + +#define DEFAULT_THREAD_STACKSIZE (6 * 1024) +#define DEFAULT_THREAD_PRIORITY 50 + +static int app_argc; +static char **app_argv; + +static void* +app_instance_main(wasm_module_inst_t module_inst) +{ + const char *exception; + + wasm_application_execute_main(module_inst, app_argc, app_argv); + if ((exception = wasm_runtime_get_exception(module_inst))){ + puts(exception); + } + return NULL; +} + + +void * +iwasm_t(void *arg1) +{ + wasm_module_t wasm_module = (wasm_module_t) arg1; + wasm_module_inst_t wasm_module_inst = NULL; + char error_buf[128]; + + /* instantiate the module */ + if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, 8 * 1024, + 8 * 1024, error_buf, sizeof(error_buf)))) { + puts(error_buf); + }else{ + app_instance_main(wasm_module_inst); + /* destroy the module instance */ + wasm_runtime_deinstantiate(wasm_module_inst); + } + return NULL; +} + +void * +iwasm_main(void *arg1) +{ + (void) arg1; /*unused*/ + uint8_t *wasm_file_buf = NULL; + unsigned wasm_file_buf_size = 0; + wasm_module_t wasm_module = NULL; + char error_buf[128]; + + RuntimeInitArgs init_args; + +//chose allocator +#define FUNC_ALLOC +//#define POOL_ALLOC + + memset(&init_args, 0, sizeof(RuntimeInitArgs)); +#ifdef POOL_ALLOC + static char global_heap_buf[256 * 1024] = { 0 };//(256 kB) + + init_args.mem_alloc_type = Alloc_With_Pool; + init_args.mem_alloc_option.pool.heap_buf = global_heap_buf; + init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf); +#elif defined(FUNC_ALLOC) + init_args.mem_alloc_type = Alloc_With_Allocator; + init_args.mem_alloc_option.allocator.malloc_func = malloc; + init_args.mem_alloc_option.allocator.realloc_func = realloc; + init_args.mem_alloc_option.allocator.free_func = free; +#else + init_args.mem_alloc_type = Alloc_With_System_Allocator; +#endif + + /* initialize runtime environment */ + if (!wasm_runtime_full_init(&init_args)) { + puts("Init runtime environment failed."); + return NULL; + } + + + /* load WASM byte buffer from byte buffer of include file */ + wasm_file_buf = (uint8_t *) wasm_test_file; + wasm_file_buf_size = sizeof(wasm_test_file); + + /* load WASM module */ + if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_buf_size, + error_buf, sizeof(error_buf)))) { + puts(error_buf); + + }else{ + iwasm_t(wasm_module); + wasm_runtime_unload(wasm_module); + } + + wasm_runtime_destroy(); + return NULL; +} + +bool +iwasm_init(void) +{ + struct{ + char * stack; + int stacksize; + uint8_t priority; + int flags; + thread_task_func_t task_func; + void * arg; + const char * name; + } b = { + .stacksize = DEFAULT_THREAD_STACKSIZE, + .priority = 8, + .flags = 0, + .task_func = iwasm_main, + .arg = NULL, + .name = "simple_wamr" + }; + + b.stack=malloc(b.stacksize); + kernel_pid_t tpid = thread_create (b.stack, b.stacksize, b.priority, b.flags, b.task_func, b.arg, b.name); + + return tpid != 0 ? true : false;; +} +#define telltruth(X) ((X) ? "true" : "false") + + +int +main(void) +{ + printf("iwasm_initilised: %s\n",telltruth(iwasm_init())); +} diff --git a/product-mini/platforms/riot/test_wasm.h b/product-mini/platforms/riot/test_wasm.h new file mode 100644 index 00000000..0c343024 --- /dev/null +++ b/product-mini/platforms/riot/test_wasm.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +/** + * The byte array buffer is the file content of a test wasm binary file, + * which is compiled by wasi-sdk toolchain from C source file of: + * product-mini/app-samples/hello-world/main.c. + */ +unsigned char wasm_test_file[] = { + 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x03, 0x60, + 0x01, 0x7F, 0x01, 0x7F, 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F, 0x60, 0x01, + 0x7F, 0x00, 0x02, 0x31, 0x04, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x70, 0x75, + 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x6D, 0x61, 0x6C, + 0x6C, 0x6F, 0x63, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x70, 0x72, + 0x69, 0x6E, 0x74, 0x66, 0x00, 0x01, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x66, + 0x72, 0x65, 0x65, 0x00, 0x02, 0x03, 0x02, 0x01, 0x01, 0x04, 0x05, 0x01, + 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x13, 0x03, + 0x7F, 0x01, 0x41, 0xC0, 0x28, 0x0B, 0x7F, 0x00, 0x41, 0xBA, 0x08, 0x0B, + 0x7F, 0x00, 0x41, 0xC0, 0x28, 0x0B, 0x07, 0x2C, 0x04, 0x06, 0x6D, 0x65, + 0x6D, 0x6F, 0x72, 0x79, 0x02, 0x00, 0x0A, 0x5F, 0x5F, 0x64, 0x61, 0x74, + 0x61, 0x5F, 0x65, 0x6E, 0x64, 0x03, 0x01, 0x0B, 0x5F, 0x5F, 0x68, 0x65, + 0x61, 0x70, 0x5F, 0x62, 0x61, 0x73, 0x65, 0x03, 0x02, 0x04, 0x6D, 0x61, + 0x69, 0x6E, 0x00, 0x04, 0x0A, 0xB2, 0x01, 0x01, 0xAF, 0x01, 0x01, 0x03, + 0x7F, 0x23, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x20, 0x6B, 0x22, 0x02, + 0x24, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x9B, 0x88, 0x80, 0x80, 0x00, + 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x02, 0x40, 0x02, 0x40, 0x41, + 0x80, 0x08, 0x10, 0x81, 0x80, 0x80, 0x80, 0x00, 0x22, 0x03, 0x0D, 0x00, + 0x41, 0xA8, 0x88, 0x80, 0x80, 0x00, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, + 0x1A, 0x41, 0x7F, 0x21, 0x04, 0x0C, 0x01, 0x0B, 0x20, 0x02, 0x20, 0x03, + 0x36, 0x02, 0x10, 0x41, 0x80, 0x88, 0x80, 0x80, 0x00, 0x20, 0x02, 0x41, + 0x10, 0x6A, 0x10, 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, 0x00, 0x21, + 0x04, 0x20, 0x03, 0x41, 0x04, 0x6A, 0x41, 0x00, 0x2F, 0x00, 0x91, 0x88, + 0x80, 0x80, 0x00, 0x3B, 0x00, 0x00, 0x20, 0x03, 0x41, 0x00, 0x28, 0x00, + 0x8D, 0x88, 0x80, 0x80, 0x00, 0x36, 0x00, 0x00, 0x20, 0x02, 0x20, 0x03, + 0x36, 0x02, 0x00, 0x41, 0x93, 0x88, 0x80, 0x80, 0x00, 0x20, 0x02, 0x10, + 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x20, 0x03, 0x10, 0x83, 0x80, 0x80, + 0x80, 0x00, 0x0B, 0x20, 0x02, 0x41, 0x20, 0x6A, 0x24, 0x80, 0x80, 0x80, + 0x80, 0x00, 0x20, 0x04, 0x0B, 0x0B, 0x41, 0x01, 0x00, 0x41, 0x80, 0x08, + 0x0B, 0x3A, 0x62, 0x75, 0x66, 0x20, 0x70, 0x74, 0x72, 0x3A, 0x20, 0x25, + 0x70, 0x0A, 0x00, 0x31, 0x32, 0x33, 0x34, 0x0A, 0x00, 0x62, 0x75, 0x66, + 0x3A, 0x20, 0x25, 0x73, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, + 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x00, 0x6D, 0x61, 0x6C, 0x6C, 0x6F, 0x63, + 0x20, 0x62, 0x75, 0x66, 0x20, 0x66, 0x61, 0x69, 0x6C, 0x65, 0x64, 0x00 +};