add RIOT-OS support to WAMR (#425)

* add RIOT platform

see riot-os.org

* add simple RIOT example
This commit is contained in:
Karl Fessel
2020-10-16 05:21:53 +02:00
committed by GitHub
parent 2dd3875fd9
commit f1fe5d7872
9 changed files with 958 additions and 0 deletions

View File

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

View File

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

View File

@ -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 <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
// #include "platform_api_extension.h"
#include "wasm_export.h"
#include <thread.h>
/*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()));
}

View File

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