From 8ec03a5165358265fff85217066a10d344a2a07d Mon Sep 17 00:00:00 2001 From: alvkeke Date: Thu, 14 Jan 2021 11:26:35 +0800 Subject: [PATCH] add porting codes of rt-thread (#494) --- README.md | 2 +- SConscript | 33 ++ build-scripts/SConscript | 88 +++++ build-scripts/SConscript_config | 123 +++++++ core/iwasm/aot/SConscript | 31 ++ core/iwasm/common/SConscript | 26 ++ core/iwasm/interpreter/SConscript | 30 ++ core/iwasm/libraries/lib-pthread/SConscript | 20 + core/iwasm/libraries/libc-builtin/SConscript | 24 ++ core/iwasm/libraries/libc-emcc/SConscript | 19 + core/iwasm/libraries/libc-wasi/SConscript | 34 ++ core/iwasm/libraries/thread-mgr/SConscript | 19 + core/shared/mem-alloc/SConscript | 33 ++ core/shared/platform/rt-thread/SConscript | 34 ++ .../platform/rt-thread/platform_internal.h | 32 ++ core/shared/platform/rt-thread/rtt_platform.c | 196 ++++++++++ .../platform/rt-thread/shared_platform.cmake | 19 + core/shared/utils/SConscript | 17 + core/shared/utils/uncommon/SConscript | 32 ++ doc/build_wamr.md | 54 +++ product-mini/platforms/rt-thread/SConscript | 18 + .../platforms/rt-thread/rtt_embed_entry.cmake | 66 ++++ .../platforms/rt-thread/rtt_wamr_entry.c | 345 ++++++++++++++++++ 23 files changed, 1294 insertions(+), 1 deletion(-) create mode 100644 SConscript create mode 100644 build-scripts/SConscript create mode 100644 build-scripts/SConscript_config create mode 100644 core/iwasm/aot/SConscript create mode 100644 core/iwasm/common/SConscript create mode 100644 core/iwasm/interpreter/SConscript create mode 100644 core/iwasm/libraries/lib-pthread/SConscript create mode 100644 core/iwasm/libraries/libc-builtin/SConscript create mode 100644 core/iwasm/libraries/libc-emcc/SConscript create mode 100644 core/iwasm/libraries/libc-wasi/SConscript create mode 100644 core/iwasm/libraries/thread-mgr/SConscript create mode 100644 core/shared/mem-alloc/SConscript create mode 100644 core/shared/platform/rt-thread/SConscript create mode 100644 core/shared/platform/rt-thread/platform_internal.h create mode 100644 core/shared/platform/rt-thread/rtt_platform.c create mode 100644 core/shared/platform/rt-thread/shared_platform.cmake create mode 100644 core/shared/utils/SConscript create mode 100644 core/shared/utils/uncommon/SConscript create mode 100644 product-mini/platforms/rt-thread/SConscript create mode 100644 product-mini/platforms/rt-thread/rtt_embed_entry.cmake create mode 100644 product-mini/platforms/rt-thread/rtt_wamr_entry.c diff --git a/README.md b/README.md index 2a4a5fbb..1b644dc2 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ The iwasm supports the following architectures: Following platforms are supported. Refer to [WAMR porting guide](./doc/port_wamr.md) for how to port WAMR to a new platform. - [Linux](./doc/build_wamr.md#linux), [Linux SGX (Intel Software Guard Extension)](./doc/linux_sgx.md), [MacOS](./doc/build_wamr.md#macos), [Android](./doc/build_wamr.md#android), [Windows](./doc/build_wamr.md#windows) -- [Zephyr](./doc/build_wamr.md#zephyr), [AliOS-Things](./doc/build_wamr.md#alios-things), [VxWorks](./doc/build_wamr.md#vxworks), [NuttX](./doc/build_wamr.md#nuttx) +- [Zephyr](./doc/build_wamr.md#zephyr), [AliOS-Things](./doc/build_wamr.md#alios-things), [VxWorks](./doc/build_wamr.md#vxworks), [NuttX](./doc/build_wamr.md#nuttx), [RT-Thread](./doc/build_wamr.md#RT-Thread) ### Build iwasm VM core (mini product) diff --git a/SConscript b/SConscript new file mode 100644 index 00000000..af4c3efe --- /dev/null +++ b/SConscript @@ -0,0 +1,33 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +# for module compiling +import os + +from building import * + +objs = [] +cwd = GetCurrentDir() +list = os.listdir(cwd) + +if GetDepend(['PKG_USING_WAMR']): + wamr_entry_sconscript = os.path.join(cwd, "product-mini", "platforms", "rt-thread", 'SConscript') + + if os.path.isfile(wamr_entry_sconscript): + objs = objs + SConscript(wamr_entry_sconscript) + else: + print("[WAMR] entry script wrong:", wamr_entry_sconscript) + Return('objs') + + wamr_runlib_sconsript = os.path.join(cwd, "build-scripts", 'SConscript') + + if os.path.isfile(wamr_runlib_sconsript): + objs = objs + SConscript(wamr_runlib_sconsript) + else: + print("[WAMR] runtime lib script wrong:", wamr_runlib_sconsript) + +Return('objs') + diff --git a/build-scripts/SConscript b/build-scripts/SConscript new file mode 100644 index 00000000..a1839642 --- /dev/null +++ b/build-scripts/SConscript @@ -0,0 +1,88 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +import os +from building import * + +cwd = GetCurrentDir() +objs = [] + +WAMR_ROOT_DIR = os.path.join(cwd, "..") + + +SHARED_DIR = os.path.join(WAMR_ROOT_DIR, 'core', 'shared') + +IWASM_DIR = os.path.join(WAMR_ROOT_DIR, 'core', 'iwasm') + +APP_MGR_DIR = os.path.join(WAMR_ROOT_DIR, 'core', 'app-mgr') + +APP_FRAMEWORK_DIR = os.path.join(WAMR_ROOT_DIR, 'core', 'app-framework') + +DEPS_DIR = os.path.join(WAMR_ROOT_DIR, 'core', 'deps') + +if GetDepend(['WAMR_BUILD_INTERP']): + script_path = os.path.join(IWASM_DIR, 'interpreter', 'SConscript') + objs += SConscript(script_path) + + +if GetDepend(['WAMR_BUILD_AOT']): + script_path = os.path.join(IWASM_DIR, 'aot', 'SConscript') + objs += SConscript(script_path) + if GetDepend(['WAMR_BUILD_JIT']): + script_path = os.path.join(IWASM_DIR, 'compilation', 'SConscript') + objs += SConscript(script_path) + + +if GetDepend(['WAMR_BUILD_APP_FRAMEWORK']): + objs += SConscript(os.path.join(APP_FRAMEWORK_DIR, 'SConscript')) + objs += SConscript(os.path.join(SHARED_DIR, 'coap', 'SConscript')) + objs += SConscript(os.path.join(APP_MGR_DIR, 'app-manager', 'SConscript')) + objs += SConscript(os.path.join(APP_MGR_DIR, 'app-mgr-shared', 'SConscript')) + + + +if GetDepend(['WAMR_BUILD_LIBC_BUILTIN']): + objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-builtin', 'SConscript')) + + + +if GetDepend(['WAMR_BUILD_LIBC_WASI']): + objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-wasi', 'SConscript')) + + +if GetDepend(['WAMR_BUILD_LIB_PTHREAD']): + objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-pthread', 'SConscript')) + # TODO: 这里加一下 + + + +# if (WAMR_BUILD_THREAD_MGR EQUAL 1) +# include (${IWASM_DIR}/libraries/thread-mgr/thread_mgr.cmake) +# endif () + +if GetDepend(['WAMR_BUILD_THREAD_MGR']): + objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'thread-mgr', 'SConscript')) + + + +# if (WAMR_BUILD_LIBC_EMCC EQUAL 1) +# include (${IWASM_DIR}/libraries/libc-emcc/libc_emcc.cmake) +# endif() + +if GetDepend(['WAMR_BUILD_LIBC_EMCC']): + objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-emmc', 'SConscript')) + +objs += SConscript(os.path.join(cwd, 'SConscript_config')); + + +objs += SConscript(os.path.join(SHARED_DIR, 'platform', 'rt-thread', 'SConscript')) +objs += SConscript(os.path.join(SHARED_DIR, 'mem-alloc', 'SConscript')) +objs += SConscript(os.path.join(IWASM_DIR, 'common', 'SConscript')) +objs += SConscript(os.path.join(SHARED_DIR, 'utils', 'SConscript')) + + + +Return('objs') diff --git a/build-scripts/SConscript_config b/build-scripts/SConscript_config new file mode 100644 index 00000000..1626086e --- /dev/null +++ b/build-scripts/SConscript_config @@ -0,0 +1,123 @@ + + +import os +import re + +from building import * +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +Import('rtconfig') + +src = Split(''' +''') +objs = [] +cwd = GetCurrentDir() + +IWASM_INC_DIR = os.path.join(cwd, '..', 'core', 'iwasm', 'include') + +# include_directories (${IWASM_DIR}/include) + +CPPPATH = [IWASM_INC_DIR] + +if rtconfig.BUILD == 'debug': + CPPDEFINES = ['BH_DEBUG=1'] +else: + CPPDEFINES = ['BH_DEBUG=0'] + +if rtconfig.ARCH == 'arm': + if re.match('^cortex-m.*', rtconfig.CPU): + print('[WAMR] using thumbv4t') + CPPDEFINES += ['BUILD_TARGET_THUMB'] + CPPDEFINES += [r'BUILD_TARGET=\"thumbv4t\"'] + elif re.match('^cortex-a.*', rtconfig.CPU): + print('[WAMR] using armv7') + CPPDEFINES += ['BUILD_TARGET_ARM'] + CPPDEFINES += [r'BUILD_TARGET=\"armv7\"'] + elif re.match('^cortex-r.*', rtconfig.CPU): + print('[WAMR] using armv7') + CPPDEFINES += ['BUILD_TARGET_ARM'] + CPPDEFINES += [r'BUILD_TARGET=\"armv7\"'] + elif rtconfig.CPU == 'armv6': + print('[WAMR] using armv6') + CPPDEFINES += ['BUILD_TARGET_ARM'] + CPPDEFINES += [r'BUILD_TARGET=\"armv6\"'] + elif re.match('^arm9*', rtconfig.CPU): + print('[WAMR] using armv4') + CPPDEFINES += ['BUILD_TARGET_ARM'] + CPPDEFINES += [r'BUILD_TARGET=\"armv4\"'] + +else: + print("[WAMR] unknown arch", rtconfig.ARCH) + + +LIBS = ['m'] + +if GetDepend(['WAMR_BUILD_INTERP']): + CPPDEFINES += ['WASM_ENABLE_INTERP=1'] + if GetDepend(['WAMR_BUILD_FAST_INTERP']): + CPPDEFINES += ['WASM_ENABLE_FAST_INTERP=1'] + print("[WAMR] fast interpreter was enabled") + else: + CPPDEFINES += ['WASM_ENABLE_FAST_INTERP=0'] + print("[WAMR] fast interpreter was disabled") +else: + CPPDEFINES += ['WASM_ENABLE_INTERP=0'] + +CPPDEFINES += ['WASM_ENABLE_JIT=0'] + +if GetDepend(['WAMR_BUILD_MULTI_MODULE']): + CPPDEFINES += ['WASM_ENABLE_MULTI_MODULE=1'] +else: + CPPDEFINES += ['WASM_ENABLE_MULTI_MODULE=0'] + +if GetDepend(['WAMR_BUILD_SPEC_TEST']): + CPPDEFINES += ['WASM_ENABLE_SPEC_TEST=1'] + print("[WAMR] spec test compatible mode was enabled") + +if GetDepend(['WAMR_BUILD_BULK_MEMORY']): + CPPDEFINES += ['WASM_ENABLE_BULK_MEMORY=1'] + print("[WAMR] Bulk memory feature was enabled") +else: + CPPDEFINES += ['WASM_ENABLE_BULK_MEMORY=0'] + +if GetDepend(['WAMR_BUILD_SHARED_MEMORY']): + CPPDEFINES += ['WASM_ENABLE_SHARED_MEMORY=1'] + print("[WAMR] Shared memory enabled") +else: + CPPDEFINES += ['WASM_ENABLE_SHARED_MEMORY=0'] + +if GetDepend(['WAMR_BUILD_MINI_LOADER']): + CPPDEFINES += ['WASM_ENABLE_MINI_LOADER=1'] + print("[WAMR] mini loader enabled") +else: + CPPDEFINES += ['WASM_ENABLE_MINI_LOADER=0'] + +if GetDepend(['WAMR_DISABLE_HW_BOUND_CHECK']): + CPPDEFINES += ['WASM_DISABLE_HW_BOUND_CHECK=1'] + print("[WAMR] Hardware boundary check disabled") + +if GetDepend(['WAMR_BUILD_SIMD']): + CPPDEFINES += ['WASM_ENABLE_SIMD=1'] + print('[WAMR] SIMD enabled') + +if GetDepend(['WAMR_BUILD_MEMORY_PROFILING']): + CPPDEFINES += ['WASM_ENABLE_MEMORY_PROFILING=1'] + print('[WAMR] Memory profiling enabled') + +if GetDepend(['WAMR_BUILD_CUSTOM_NAME_SECTION']): + CPPDEFINES += ['WASM_ENABLE_CUSTOM_NAME_SECTION=1'] + print('[WAMR] Custom name section enabled') + +if GetDepend(['WAMR_BUILD_TAIL_CALL']): + CPPDEFINES += ['WASM_ENABLE_TAIL_CALL=1'] + print('[WAMR] Tail call enabledd') + + +group = DefineGroup('wamr_config_common', src, depend = ['PKG_USING_WAMR'], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES, LIBS = LIBS) + +Return('group') + diff --git a/core/iwasm/aot/SConscript b/core/iwasm/aot/SConscript new file mode 100644 index 00000000..c1a3c96e --- /dev/null +++ b/core/iwasm/aot/SConscript @@ -0,0 +1,31 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * +import re +Import('rtconfig') + +cwd = GetCurrentDir() + +src = Split(''' +aot_loader.c +aot_runtime.c +''') + +if rtconfig.ARCH == 'arm': + if re.match('^cortex-m.*', rtconfig.CPU): + src += ['arch/aot_reloc_thumb.c'] + elif re.match('^cortex-a.*', rtconfig.CPU): + src += ['arch/aot_reloc_arm.c'] + + +CPPPATH = [cwd, cwd + '/../include'] + +CPPDEFINES = ['WASM_ENABLE_AOT=1'] + +group = DefineGroup('iwasm_aot', src, depend = [''], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) + +Return('group') diff --git a/core/iwasm/common/SConscript b/core/iwasm/common/SConscript new file mode 100644 index 00000000..235b0e88 --- /dev/null +++ b/core/iwasm/common/SConscript @@ -0,0 +1,26 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * +import re + +Import('rtconfig') + +cwd = GetCurrentDir() + +src = Glob('*.c') + +if rtconfig.ARCH == 'arm': + if re.match('^cortex-m.*', rtconfig.CPU): + src += ['arch/invokeNative_thumb.s'] + elif re.match('^cortex-a.*', rtconfig.CPU): + src += ['arch/invokeNative_arm.s'] + +CPPPATH = [cwd, cwd + '/../include'] + +group = DefineGroup('iwasm_common', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/core/iwasm/interpreter/SConscript b/core/iwasm/interpreter/SConscript new file mode 100644 index 00000000..7c0605ee --- /dev/null +++ b/core/iwasm/interpreter/SConscript @@ -0,0 +1,30 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * + +cwd = GetCurrentDir() + +src = Split(''' +wasm_runtime.c +''') + +if GetDepend(['WAMR_BUILD_FAST_INTERP']): + src += ["wasm_interp_fast.c"] +else: + src += ["wasm_interp_classic.c"] + +if GetDepend(['WAMR_BUILD_MINI_LOADER']): + src += ["wasm_mini_loader.c"] +else: + src += ["wasm_loader.c"] + + +CPPPATH = [cwd] + +group = DefineGroup('iwasm_interpreter', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/core/iwasm/libraries/lib-pthread/SConscript b/core/iwasm/libraries/lib-pthread/SConscript new file mode 100644 index 00000000..d03936c2 --- /dev/null +++ b/core/iwasm/libraries/lib-pthread/SConscript @@ -0,0 +1,20 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * + +cwd = GetCurrentDir() + +src = Split(''' +libc_pthread_wrapper.c +''') + +CPPPATH = [cwd] + + +group = DefineGroup('iwasm_libc_pthread', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/core/iwasm/libraries/libc-builtin/SConscript b/core/iwasm/libraries/libc-builtin/SConscript new file mode 100644 index 00000000..8dd00438 --- /dev/null +++ b/core/iwasm/libraries/libc-builtin/SConscript @@ -0,0 +1,24 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * + +cwd = GetCurrentDir() + +#src = Split(''' +#libc_builtin_wrapper.c +#''') + +src = Glob('*.c') + +CPPDEFINES = ['WASM_ENABLE_LIBC_BUILTIN=1'] + +CPPPATH = [cwd] + + +group = DefineGroup('iwasm_libc_builtin', src, depend = [''], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) + +Return('group') diff --git a/core/iwasm/libraries/libc-emcc/SConscript b/core/iwasm/libraries/libc-emcc/SConscript new file mode 100644 index 00000000..432ed4a0 --- /dev/null +++ b/core/iwasm/libraries/libc-emcc/SConscript @@ -0,0 +1,19 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * + +cwd = GetCurrentDir() + +src = Split(''' +libc_emcc_wrapper.c +''') + +CPPPATH = [cwd] + +group = DefineGroup('iwasm_libc_emcc', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/core/iwasm/libraries/libc-wasi/SConscript b/core/iwasm/libraries/libc-wasi/SConscript new file mode 100644 index 00000000..6ed3799e --- /dev/null +++ b/core/iwasm/libraries/libc-wasi/SConscript @@ -0,0 +1,34 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * +import os + +cwd = GetCurrentDir() + +src = Split(''' +''') + +CPPPATH = [cwd, + cwd+'/sandboxed-system-primitives/include', + cwd+'/sandboxed-system-primitives/src'] + +def addSrcFiles(arr, path): + for f in os.listdir(path): + fpath = os.path.join(path, f); + if os.path.isfile(fpath): + ext = os.path.splitext(fpath)[-1] + if ext == '.c' or ext == '.cpp': + arr += [fpath] + elif os.path.isdir(fpath): + addSrcFiles(arr, fpath) + + +addSrcFiles(src, cwd) + +group = DefineGroup('iwasm_libc_wasi', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/core/iwasm/libraries/thread-mgr/SConscript b/core/iwasm/libraries/thread-mgr/SConscript new file mode 100644 index 00000000..65f561ae --- /dev/null +++ b/core/iwasm/libraries/thread-mgr/SConscript @@ -0,0 +1,19 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * + +cwd = GetCurrentDir() + +src = Split(''' +thread_manager.c +''') + +CPPPATH = [cwd] + +group = DefineGroup('iwasm_lib_thread_mgr', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/core/shared/mem-alloc/SConscript b/core/shared/mem-alloc/SConscript new file mode 100644 index 00000000..602d8715 --- /dev/null +++ b/core/shared/mem-alloc/SConscript @@ -0,0 +1,33 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * +import os + +cwd = GetCurrentDir() + +src = Split(''' +''') + + +def addSrcFiles(arr, path): + for f in os.listdir(path): + fpath = os.path.join(path, f); + if os.path.isfile(fpath): + ext = os.path.splitext(fpath)[-1] + if ext == '.c' or ext == '.cpp': + arr += [fpath] + elif os.path.isdir(fpath): + addSrcFiles(arr, fpath) + + + +addSrcFiles(src, cwd); +CPPPATH = [cwd, cwd+'/../include'] + +group = DefineGroup('iwasm_platform_core', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/core/shared/platform/rt-thread/SConscript b/core/shared/platform/rt-thread/SConscript new file mode 100644 index 00000000..1e93f475 --- /dev/null +++ b/core/shared/platform/rt-thread/SConscript @@ -0,0 +1,34 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + + +from building import * +import os + +cwd = GetCurrentDir() + +src = Split(''' +''') + + +def addSrcFiles(arr, path): + for f in os.listdir(path): + fpath = os.path.join(path, f); + if os.path.isfile(fpath): + ext = os.path.splitext(fpath)[-1] + if ext == '.c' or ext == '.cpp': + arr += [fpath] + elif os.path.isdir(fpath): + addSrcFiles(arr, fpath) + + + +addSrcFiles(src, cwd); +CPPPATH = [cwd, cwd+'/../include'] + +group = DefineGroup('iwasm_platform_core', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/core/shared/platform/rt-thread/platform_internal.h b/core/shared/platform/rt-thread/platform_internal.h new file mode 100644 index 00000000..9ca34422 --- /dev/null +++ b/core/shared/platform/rt-thread/platform_internal.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef RTTHREAD_PLATFORM_INTERNAL_H +#define RTTHREAD_PLATFORM_INTERNAL_H + +#include +#include +#include +#include +#include +#include +#include +#include + +typedef rt_thread_t korp_tid; +typedef struct rt_mutex korp_mutex; +typedef struct rt_thread korp_cond; +typedef struct rt_thread korp_thread; + +typedef rt_uint8_t uint8_t; +typedef rt_int8_t int8_t; +typedef rt_uint16_t uint16_t; +typedef rt_int16_t int16_t; +typedef rt_uint64_t uint64_t; +typedef rt_int64_t int64_t; + + +#endif //RTTHREAD_PLATFORM_INTERNAL_H diff --git a/core/shared/platform/rt-thread/rtt_platform.c b/core/shared/platform/rt-thread/rtt_platform.c new file mode 100644 index 00000000..508d8953 --- /dev/null +++ b/core/shared/platform/rt-thread/rtt_platform.c @@ -0,0 +1,196 @@ +/* + * Copyright (c) 2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include + +typedef struct os_malloc_list { + void* real; + void* used; + rt_list_t node; +}os_malloc_list_t; + +int bh_platform_init(void) +{ + return 0; +} + +void bh_platform_destroy(void) +{ +} + +void *os_malloc(unsigned size) +{ + void *buf_origin; + void *buf_fixed; + rt_ubase_t *addr_field; + + if (size == 0) + { + return RT_NULL; + } + buf_origin = rt_malloc(size + 8 + sizeof(rt_ubase_t)); + buf_fixed = buf_origin + sizeof(void*); + if ((rt_ubase_t)buf_fixed & 0x7) + { + buf_fixed = (void*)((rt_ubase_t)(buf_fixed+8)&(~7)); + } + + addr_field = buf_fixed - sizeof(rt_ubase_t); + *addr_field = (rt_ubase_t )buf_origin; + + return buf_fixed; + +} + +void *os_realloc(void *ptr, unsigned size) +{ + + void* mem_origin; + void* mem_new; + void *mem_new_fixed; + rt_ubase_t *addr_field; + + if (!ptr) + { + return RT_NULL; + } + + addr_field = ptr - sizeof(rt_ubase_t); + mem_origin = (void*)(*addr_field); + mem_new = rt_realloc(mem_origin, size + 8 + sizeof(rt_ubase_t)); + + if (mem_origin != mem_new) + { + mem_new_fixed = mem_new + sizeof(rt_ubase_t); + if ((rt_ubase_t)mem_new_fixed & 0x7) + { + mem_new_fixed = (void*)((rt_ubase_t)(mem_new_fixed+8)&(~7)); + } + + addr_field = mem_new_fixed - sizeof(rt_ubase_t); + *addr_field = (rt_ubase_t )mem_new; + + return mem_new_fixed; + } + + return ptr; +} + +void os_free(void *ptr) +{ + void* mem_origin; + rt_ubase_t *addr_field; + + if (ptr) + { + addr_field = ptr - sizeof(rt_ubase_t); + mem_origin = (void*)(*addr_field); + + rt_free(mem_origin); + } + +} + + +static char wamr_vprint_buf[RT_CONSOLEBUF_SIZE * 2]; +int os_printf(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + rt_size_t len = vsnprintf(wamr_vprint_buf, sizeof(wamr_vprint_buf)-1, format, ap); + wamr_vprint_buf[len] = 0x00; + rt_kputs(wamr_vprint_buf); + va_end(ap); + return 0; +} + +int os_vprintf(const char *format, va_list ap) +{ + rt_size_t len = vsnprintf(wamr_vprint_buf, sizeof(wamr_vprint_buf)-1, format, ap); + wamr_vprint_buf[len] = 0; + rt_kputs(wamr_vprint_buf); + return 0; +} + +uint64 os_time_get_boot_microsecond(void) +{ + uint64 ret = rt_tick_get()*1000; + ret /= RT_TICK_PER_SECOND; + return ret; +} + +korp_tid os_self_thread(void) +{ + return rt_thread_self(); +} + +uint8 *os_thread_get_stack_boundary(void) +{ + rt_thread_t tid = rt_thread_self(); + return tid->stack_addr; +} + +int os_mutex_init(korp_mutex *mutex) +{ + return rt_mutex_init(mutex, "wamr0", RT_IPC_FLAG_FIFO); +} + +int os_mutex_destroy(korp_mutex *mutex) +{ + return rt_mutex_detach(mutex); +} + +int os_mutex_lock(korp_mutex *mutex) +{ + return rt_mutex_take(mutex, RT_WAITING_FOREVER); +} + +int os_mutex_unlock(korp_mutex *mutex) +{ + return rt_mutex_release(mutex); +} + + +/* + * functions below was not implement + */ + +int os_cond_init(korp_cond *cond) +{ + return 0; +} + +int os_cond_destroy(korp_cond *cond) +{ + return 0; +} + +int os_cond_wait(korp_cond *cond, korp_mutex *mutex) +{ + return 0; +} + +void *os_mmap(void *hint, size_t size, int prot, int flags) +{ + return rt_malloc(size); +} + +void os_munmap(void *addr, size_t size) +{ + rt_free(addr); +} + +#ifdef OS_ENABLE_HW_BOUND_CHECK +int os_mprotect(void *addr, size_t size, int prot) +{ + return 0; +} +#endif + +void os_dcache_flush(void) +{ +} diff --git a/core/shared/platform/rt-thread/shared_platform.cmake b/core/shared/platform/rt-thread/shared_platform.cmake new file mode 100644 index 00000000..fce9bff3 --- /dev/null +++ b/core/shared/platform/rt-thread/shared_platform.cmake @@ -0,0 +1,19 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR}) + +add_definitions(-DBH_PLATFORM_RTT) + +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/core/shared/utils/SConscript b/core/shared/utils/SConscript new file mode 100644 index 00000000..358f2ffc --- /dev/null +++ b/core/shared/utils/SConscript @@ -0,0 +1,17 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * +import os + +cwd = GetCurrentDir() + +src = Glob('*.c') +CPPPATH = [cwd] + +group = DefineGroup('iwasm_shared_utils', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/core/shared/utils/uncommon/SConscript b/core/shared/utils/uncommon/SConscript new file mode 100644 index 00000000..f608645f --- /dev/null +++ b/core/shared/utils/uncommon/SConscript @@ -0,0 +1,32 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * +import os + +cwd = GetCurrentDir() + +# src = Split(''' +# ''') + + +def addSrcFiles(arr, path): + for f in os.listdir(path): + fpath = os.path.join(path, f); + if os.path.isfile(fpath): + ext = os.path.splitext(fpath)[-1] + if ext == '.c' or ext == '.cpp': + arr += [fpath] + #elif os.path.isdir(fpath): + # addSrcFiles(arr, fpath) + +src = Glob('*.c') +src += Glob('*.cpp') +CPPPATH = [cwd] + +group = DefineGroup('iwasm_shared_utils_uncommon', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 57a65a73..f26f1a2b 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -305,6 +305,60 @@ AliOS-Things ``` download the binary to developerkit board, check the output from serial port +RT-Thread +------------------------- + +1. Get rt-thread [system codes](https://github.com/RT-Thread/rt-thread). + +2. Enable WAMR software package with menuconfig tool which provided by RT-Thread. + + * Environment in Linux, run command below: + + ```bash + scons --menuconfig + ``` + + * Environment in Windows ConEmu, run command below: + + ```bash + menuconfig + ``` + + Select and enable `WAMR` in: + + * RT-Thread online packages + * tools packages + * WebAssembly Micro Runtime (WAMR) + +3. Configure `WAMR` with menuconfig tool. + + you can choice features of iwasm below: + + * Enable testing parameters of iwasm + * Enable interpreter Mode / Fast interpreter Mode + * Use built-libc + * Enable AOT + +4. Exit menuconfig tool and save configure, update and download package. + + ```bash + pkgs --update + ``` + +5. build project and download the binary to boards. + + ```bash + scons + ``` + + or build project with 8-thread by using command below: + + ```bash + scons -j8 + ``` + + after project building, you can got an binary file named `rtthread.bin`, then you can download this file to the MCU board. + Android ------------------------- able to generate a shared library support Android platform. diff --git a/product-mini/platforms/rt-thread/SConscript b/product-mini/platforms/rt-thread/SConscript new file mode 100644 index 00000000..8f866caf --- /dev/null +++ b/product-mini/platforms/rt-thread/SConscript @@ -0,0 +1,18 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +from building import * + +cwd = GetCurrentDir() + + +src = Glob('*.c') + +CPPPATH = [cwd] + +group = DefineGroup('iwasm_entry', src, depend = ['PKG_USING_WAMR'], CPPPATH = CPPPATH) + +Return('group') diff --git a/product-mini/platforms/rt-thread/rtt_embed_entry.cmake b/product-mini/platforms/rt-thread/rtt_embed_entry.cmake new file mode 100644 index 00000000..0ec54ae6 --- /dev/null +++ b/product-mini/platforms/rt-thread/rtt_embed_entry.cmake @@ -0,0 +1,66 @@ +# +# Copyright (c) 2021, RT-Thread Development Team +# +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +set(CMAKE_ASM_COMPILER_WORKS 1) + +set(WAMR_BUILD_PLATFORM "rt-thread") +set(WAMR_BUILD_TARGET "ARM") + +#set(WAMR_BUILD_INTERP 1) +#set(WAMR_BUILD_FAST_INTERP 1) +#set(WAMR_BUILD_AOT 0) +#set(WAMR_BUILD_JIT 0) +#set(WAMR_BUILD_LIBC_BUILTIN 1) +#set(WAMR_BUILD_LIBC_WASI 0) + +if (NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif () + +if (NOT DEFINED WAMR_BUILD_INTERP) + # Enable Interpreter by default + set (WAMR_BUILD_INTERP 1) +endif () + +if (NOT DEFINED WAMR_BUILD_AOT) + # Enable AOT by default. + set (WAMR_BUILD_AOT 0) +endif () + +# Disable JIT by default. +set (WAMR_BUILD_JIT 0) + +if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN) + # Enable libc builtin support by default + set (WAMR_BUILD_LIBC_BUILTIN 1) +endif () + +set (WAMR_BUILD_LIBC_WASI 0) + +if (NOT DEFINED WAMR_BUILD_FAST_INTERP) + # Enable fast interpreter + set (WAMR_BUILD_FAST_INTERP 1) +endif () + +set (WAMR_BUILD_MULTI_MODULE 0) +set (WAMR_BUILD_LIB_PTHREAD 0) +set (WAMR_BUILD_MINI_LOADER 0) +set (WAMR_BUILD_SIMD 0) + + +set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..) + +set(CMAKE_ASM_COMPILER_WORKS 1) + +include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) + +file (GLOB wamr_entry_src + ${WAMR_ROOT_DIR}/product-mini/platforms/rt-thread/rtt_wamr_entry.c + ) + +set(WAMR_SOURCE ${wamr_entry_src} ${WAMR_RUNTIME_LIB_SOURCE}) + + diff --git a/product-mini/platforms/rt-thread/rtt_wamr_entry.c b/product-mini/platforms/rt-thread/rtt_wamr_entry.c new file mode 100644 index 00000000..0b9b25e2 --- /dev/null +++ b/product-mini/platforms/rt-thread/rtt_wamr_entry.c @@ -0,0 +1,345 @@ +/* + * Copyright (c) 2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include "wasm_export.h" +#include "platform_api_vmcore.h" +#include +#include +#include +#include + +static int wasm_vprintf(wasm_exec_env_t env, const char* fmt, va_list va) +{ + return vprintf(fmt, va); +} + +static int wasm_vsprintf(wasm_exec_env_t env, char* buf, const char* fmt, va_list va) +{ + return vsprintf(buf, fmt, va); +} + +static int wasm_vsnprintf(wasm_exec_env_t env, char *buf, int n, const char *fmt, va_list va) +{ + return vsnprintf(buf, n, fmt, va); +} + +static rt_device_t wasm_rt_device_find(wasm_exec_env_t env, const char *name) +{ + return rt_device_find(name); +} + +static rt_err_t wasm_rt_device_open(wasm_exec_env_t env, rt_device_t dev, rt_uint16_t o_flag) +{ + return rt_device_open(dev , o_flag); +} + +static rt_size_t wasm_rt_device_write(wasm_exec_env_t env, rt_device_t dev, rt_off_t offset, const void*buf, rt_size_t size) +{ + return rt_device_write(dev, offset, buf, size); +} + +static rt_size_t wasm_rt_device_read(wasm_exec_env_t env, rt_device_t dev, rt_off_t offset, void *buf, rt_size_t size) +{ + return rt_device_read(dev, offset, buf, size); +} + +static rt_err_t wasm_rt_device_close(wasm_exec_env_t env, rt_device_t dev) +{ + return rt_device_close(dev); +} + +static rt_err_t wasm_rt_device_control(wasm_exec_env_t env, rt_device_t dev, int cmd, void *arg) +{ + return rt_device_control(dev, cmd, arg); +} + +static NativeSymbol native_export_symbols[] = { + { + "vprintf", + wasm_vprintf, + "($*)i" + }, + { + "vsprintf", + wasm_vsprintf, + "($$*)i" + }, + { + "vsnprintf", + wasm_vsnprintf, + "($i$*)i" + }, + { + "rt_device_find", + wasm_rt_device_find, + "($)i" + }, + { + "rt_device_open", + wasm_rt_device_open, + "(ii)i" + }, + { + "rt_device_write", + wasm_rt_device_write, + "(ii*~)i" + }, + { + "rt_device_read", + wasm_rt_device_read, + "(ii*~)i" + }, + { + "rt_device_close", + wasm_rt_device_close, + "(i)i" + }, + { + "rt_device_control", + wasm_rt_device_control, + "(ii*)i" + } +}; + +/** + * run WASM module instance. + * @param module_inst instance of wasm module + * @param app_argc wasm argument count + * @param app_argv wasm arguments + * @return NULL + */ +static void * +app_instance_main(wasm_module_inst_t module_inst, int app_argc, char **app_argv) +{ + const char *exception; + + wasm_application_execute_main(module_inst, app_argc, app_argv); + if ((exception = wasm_runtime_get_exception(module_inst))) + rt_kprintf("%s\n", exception); + return NULL; +} + +rt_uint8_t *my_read_file_to_buffer(char* filename, rt_uint32_t *size) +{ + struct stat f_stat; + dfs_file_stat(filename, &f_stat); + struct dfs_fd fd; + + rt_uint8_t* buff = rt_malloc(f_stat.st_size); + *size = 0; + if (!buff) + { + rt_set_errno(-ENOMEM); + return RT_NULL; + } + + int ret = dfs_file_open(&fd, filename, O_RDONLY); + if (ret) + { + rt_free(buff); + rt_set_errno(ret); + return RT_NULL; + } + + *size = dfs_file_read(&fd, buff, f_stat.st_size); + + dfs_file_close(&fd); + + if (*size != f_stat.st_size) + { + rt_free(buff); + rt_set_errno(-EBADF); + return RT_NULL; + } + + return buff; +} + +void iwasm_help(void) +{ +#ifdef WAMR_ENABLE_IWASM_PARAMS + rt_kputs("wrong input: iwasm [-t] [-m] [-s] <*.wasm> \n iwasm [-h]\n"); + rt_kputs("\t -h: show this tips.\n"); + rt_kputs("\t -t: show time taking to run this app.\n"); + rt_kputs("\t -m: show memory taking to run this app\n"); + rt_kputs("\t wasm file name and exec params must behind of all vm-param\n"); +#else + rt_kputs("wrong input: iwasm <*.wasm> \n"); +#endif /* WAMR_ENABLE_PARAMS */ +} + +int iwasm(int argc, char **argv) +{ + rt_uint8_t *wasm_file_buf = NULL; + rt_uint32_t wasm_file_size; + rt_uint32_t stack_size = 4 * 1024, heap_size = 4 * 1024; + wasm_module_t wasm_module = NULL; + wasm_module_inst_t wasm_module_inst = NULL; + RuntimeInitArgs init_args; + static char error_buf[128] = { 0 }; + // avoid stack overflow + +#ifdef WAMR_ENABLE_IWASM_PARAMS + int i_arg_begin; + bool show_mem = false; + bool show_stack = false; + bool show_time_exec = false; + for(i_arg_begin=1; i_arg_begin < argc; i_arg_begin++) + { + if (argv[i_arg_begin][0] != '-') + { + break; + } + + if (argv[i_arg_begin][1] == 'm') + { + show_mem = true; + } + else if (argv[i_arg_begin][1] == 's') + { + show_stack = true; + } + else if (argv[i_arg_begin][1] == 't') + { + show_time_exec = true; + } + else if (argv[i_arg_begin][1] == 'h') + { + iwasm_help(); + return 0; + } + else if (argv[i_arg_begin][1] == 0x00) + { + continue; + } + else + { + rt_kprintf("[iwasm] unknown param: %s\n", argv[i_arg_begin]); + } + } +#else /* WAMR_ENABLE_PARAMS */ +#define i_arg_begin 1 +#endif /* WAMR_ENABLE_PARAMS */ + + if (argc - i_arg_begin < 1) + { + iwasm_help(); + return -1; + } + + rt_memset(&init_args, 0, sizeof(RuntimeInitArgs)); + init_args.mem_alloc_type = Alloc_With_Allocator; + init_args.mem_alloc_option.allocator.malloc_func = os_malloc; + init_args.mem_alloc_option.allocator.realloc_func = os_realloc; + init_args.mem_alloc_option.allocator.free_func = os_free; + init_args.native_symbols = native_export_symbols; + init_args.n_native_symbols = sizeof(native_export_symbols) / sizeof(NativeSymbol); + init_args.native_module_name = "env"; + +#ifdef WAMR_ENABLE_IWASM_PARAMS +#if defined(RT_USING_HEAP) && defined(RT_USING_MEMHEAP_AS_HEAP) + extern long list_memheap(void); + if (show_mem) + { + list_memheap(); + } +#else + rt_uint32_t total, max, used; + if (show_mem) + { + rt_memory_info(&total, &used, &max); + } +#endif + rt_thread_t tid; + if (show_stack) + { + tid = rt_thread_self(); + printf("thread stack addr: %p, size: %u, sp: %p\n", tid->stack_addr, tid->stack_size, tid->sp); + } +#endif /* WAMR_ENABLE_PARAMS */ + + if (wasm_runtime_full_init(&init_args) == false) + { + rt_kprintf("Init WASM runtime environment failed.\n"); + return -1; + } + + wasm_file_buf = my_read_file_to_buffer (argv[i_arg_begin], &wasm_file_size); + if (!wasm_file_buf) + { + rt_err_t err = rt_get_errno(); + rt_kprintf("WASM load file to RAM failed: %d\n", err); + goto fail1; + } + rt_memset(error_buf, 0x00, sizeof(error_buf)); + wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf, sizeof(error_buf)); + if (!wasm_module) + { + rt_kprintf("%s\n", error_buf); + goto fail2; + } + rt_memset(error_buf, 0x00, sizeof(error_buf)); + wasm_module_inst = wasm_runtime_instantiate(wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf)); + if (!wasm_module_inst) + { + rt_kprintf("%s\n", error_buf); + goto fail3; + } + +#ifdef WAMR_ENABLE_IWASM_PARAMS + rt_tick_t ticks_exec; + if (show_time_exec) { + ticks_exec = rt_tick_get(); + } +#endif /* WAMR_ENABLE_PARAMS */ + + app_instance_main(wasm_module_inst, argc - i_arg_begin, &argv[i_arg_begin]); + +#ifdef WAMR_ENABLE_IWASM_PARAMS + if (show_time_exec) + { + ticks_exec = rt_tick_get() - ticks_exec; + printf("[iwasm] execute ticks took: %u [ticks/s = %u]\n", ticks_exec, RT_TICK_PER_SECOND); + } +#if defined(RT_USING_HEAP) && defined(RT_USING_MEMHEAP_AS_HEAP) + if (show_mem) + { + list_memheap(); + } +#else + rt_uint32_t total_after, max_after, used_after; + if (show_mem) + { + rt_memory_info(&total_after, &used_after, &max_after); + rt_kprintf("[iwasm] memory took: %u\n", used_after - used); + } +#endif + if (show_stack) + { + printf("[iwasm] thread stack addr: %p, size: %u, sp: %p\n", tid->stack_addr, tid->stack_size, tid->sp); + } + +#endif /* WAMR_ENABLE_PARAMS */ + + /* destroy the module instance */ + wasm_runtime_deinstantiate(wasm_module_inst); + + fail3: + /* unload the module */ + wasm_runtime_unload(wasm_module); + + fail2: + /* free the file buffer */ + rt_free(wasm_file_buf); + + fail1: + /* destroy runtime environment */ + wasm_runtime_destroy(); + return 0; +} +MSH_CMD_EXPORT(iwasm, Embeded VM of WebAssembly); +