Implement XIP feature and enable ARC target support (#694)
Implement XIP (Execution In Place) feature for AOT mode to enable running the AOT code inside AOT file directly, without memory mapping the executable memory for AOT code and applying relocations for text section. Developer can use wamrc with "--enable-indirect-mode --disable-llvm-intrinsics" flags to generate the AOT file and run iwasm with "--xip" flag. Known issues: there might still be some relocations in the text section which access the ".rodata" like sections. And also enable ARC target support for both interpreter mode and AOT mode. Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
@ -35,11 +35,11 @@ endif
|
||||
|
||||
WAMR_BUILD_PLATFORM := nuttx
|
||||
|
||||
ifeq (${WAMR_BUILD_TARGET}, X86_32)
|
||||
ifeq ($(WAMR_BUILD_TARGET), X86_32)
|
||||
CFLAGS += -DBUILD_TARGET_X86_32
|
||||
INVOKE_NATIVE := invokeNative_ia32.s
|
||||
AOT_RELOC := aot_reloc_x86_32.c
|
||||
else ifeq (${WAMR_BUILD_TARGET}, X86_64)
|
||||
else ifeq ($(WAMR_BUILD_TARGET), X86_64)
|
||||
CFLAGS += -DBUILD_TARGET_X86_64
|
||||
INVOKE_NATIVE := invokeNative_em64.s
|
||||
AOT_RELOC := aot_reloc_x86_64.c
|
||||
@ -96,17 +96,18 @@ else
|
||||
$(error Build target is unsupported)
|
||||
endif
|
||||
|
||||
ifeq (${CONFIG_INTERPRETERS_WAMR_LOG},y)
|
||||
ifeq ($(CONFIG_INTERPRETERS_WAMR_LOG),y)
|
||||
CFLAGS += -DWASM_ENABLE_LOG=1
|
||||
else
|
||||
CFLAGS += -DWASM_ENABLE_LOG=0
|
||||
endif
|
||||
|
||||
ifeq (${CONFIG_INTERPRETERS_WAMR_AOT},y)
|
||||
CFLAGS += -I${IWASM_ROOT}/aot
|
||||
ifeq ($(CONFIG_INTERPRETERS_WAMR_AOT),y)
|
||||
CFLAGS += -I$(IWASM_ROOT)/aot
|
||||
CFLAGS += -DWASM_ENABLE_AOT=1
|
||||
CSRCS += aot_loader.c \
|
||||
${AOT_RELOC} \
|
||||
$(AOT_RELOC) \
|
||||
aot_intrinsic.c \
|
||||
aot_runtime.c
|
||||
else
|
||||
CFLAGS += -DWASM_ENABLE_AOT=0
|
||||
@ -115,7 +116,7 @@ endif
|
||||
CFLAGS += -DWASM_ENABLE_INTERP=1
|
||||
CSRCS += wasm_runtime.c
|
||||
|
||||
ifeq (${CONFIG_INTERPRETERS_WAMR_FAST},y)
|
||||
ifeq ($(CONFIG_INTERPRETERS_WAMR_FAST),y)
|
||||
CFLAGS += -DWASM_ENABLE_FAST_INTERP=1
|
||||
CSRCS += wasm_interp_fast.c
|
||||
else
|
||||
@ -137,7 +138,7 @@ endif
|
||||
ifeq ($(CONFIG_INTERPRETERS_WAMR_THREAD_MGR),y)
|
||||
CFLAGS += -DWASM_ENABLE_THREAD_MGR=1
|
||||
CSRCS += thread_manager.c
|
||||
VPATH += ${IWASM_ROOT}/libraries/thread-mgr
|
||||
VPATH += $(IWASM_ROOT)/libraries/thread-mgr
|
||||
else
|
||||
CFLAGS += -DWASM_ENABLE_THREAD_MGR=0
|
||||
endif
|
||||
@ -191,9 +192,8 @@ CFLAGS += -I${CORE_ROOT} \
|
||||
-I${SHARED_ROOT}/mem-alloc \
|
||||
-I${SHARED_ROOT}/platform/nuttx
|
||||
|
||||
|
||||
ifeq (${WAMR_BUILD_INTERP}, 1)
|
||||
CFLAGS += -I${IWASM_ROOT}/interpreter
|
||||
ifeq ($(WAMR_BUILD_INTERP), 1)
|
||||
CFLAGS += -I$(IWASM_ROOT)/interpreter
|
||||
endif
|
||||
|
||||
CSRCS += nuttx_platform.c \
|
||||
@ -220,19 +220,19 @@ CSRCS += nuttx_platform.c \
|
||||
wasm_memory.c \
|
||||
wasm_c_api.c
|
||||
|
||||
ASRCS += ${INVOKE_NATIVE}
|
||||
ASRCS += $(INVOKE_NATIVE)
|
||||
|
||||
VPATH += ${SHARED_ROOT}/platform/nuttx
|
||||
VPATH += ${SHARED_ROOT}/platform/common/posix
|
||||
VPATH += ${SHARED_ROOT}/mem-alloc
|
||||
VPATH += ${SHARED_ROOT}/mem-alloc/ems
|
||||
VPATH += ${SHARED_ROOT}/utils
|
||||
VPATH += ${SHARED_ROOT}/utils/uncommon
|
||||
VPATH += ${IWASM_ROOT}/common
|
||||
VPATH += ${IWASM_ROOT}/interpreter
|
||||
VPATH += ${IWASM_ROOT}/libraries
|
||||
VPATH += ${IWASM_ROOT}/libraries/libc-builtin
|
||||
VPATH += ${IWASM_ROOT}/libraries/lib-pthread
|
||||
VPATH += ${IWASM_ROOT}/common/arch
|
||||
VPATH += ${IWASM_ROOT}/aot
|
||||
VPATH += ${IWASM_ROOT}/aot/arch
|
||||
VPATH += $(SHARED_ROOT)/platform/nuttx
|
||||
VPATH += $(SHARED_ROOT)/platform/common/posix
|
||||
VPATH += $(SHARED_ROOT)/mem-alloc
|
||||
VPATH += $(SHARED_ROOT)/mem-alloc/ems
|
||||
VPATH += $(SHARED_ROOT)/utils
|
||||
VPATH += $(SHARED_ROOT)/utils/uncommon
|
||||
VPATH += $(IWASM_ROOT)/common
|
||||
VPATH += $(IWASM_ROOT)/interpreter
|
||||
VPATH += $(IWASM_ROOT)/libraries
|
||||
VPATH += $(IWASM_ROOT)/libraries/libc-builtin
|
||||
VPATH += $(IWASM_ROOT)/libraries/lib-pthread
|
||||
VPATH += $(IWASM_ROOT)/common/arch
|
||||
VPATH += $(IWASM_ROOT)/aot
|
||||
VPATH += $(IWASM_ROOT)/aot/arch
|
||||
|
||||
@ -32,7 +32,9 @@ print_help()
|
||||
printf(" --stack-size=n Set maximum stack size in bytes, default is 16 KB\n");
|
||||
printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n");
|
||||
printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
|
||||
" that runs commands in the form of `FUNC ARG...`\n");
|
||||
" that runs commands in the form of \"FUNC ARG...\"\n");
|
||||
printf(" --xip Enable XIP (Execution In Place) mode to run AOT file\n"
|
||||
" generated with \"--enable-indirect-mode\" flag\n");
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
printf(" --env=<env> Pass wasi environment variables with \"key=value\"\n");
|
||||
printf(" to the program, for example:\n");
|
||||
@ -232,6 +234,7 @@ main(int argc, char *argv[])
|
||||
int log_verbose_level = 2;
|
||||
#endif
|
||||
bool is_repl_mode = false;
|
||||
bool is_xip_mode = false;
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
const char *dir_list[8] = { NULL };
|
||||
uint32 dir_list_size = 0;
|
||||
@ -259,6 +262,9 @@ main(int argc, char *argv[])
|
||||
else if (!strcmp(argv[0], "--repl")) {
|
||||
is_repl_mode = true;
|
||||
}
|
||||
else if (!strcmp(argv[0], "--xip")) {
|
||||
is_xip_mode = true;
|
||||
}
|
||||
else if (!strncmp(argv[0], "--stack-size=", 13)) {
|
||||
if (argv[0][13] == '\0')
|
||||
return print_help();
|
||||
@ -355,6 +361,24 @@ main(int argc, char *argv[])
|
||||
(uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
|
||||
goto fail1;
|
||||
|
||||
if (is_xip_mode) {
|
||||
uint8 *wasm_file_mapped;
|
||||
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
|
||||
int map_flags = MMAP_MAP_NONE;
|
||||
|
||||
if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size,
|
||||
map_prot, map_flags))) {
|
||||
printf("mmap memory failed\n");
|
||||
wasm_runtime_free(wasm_file_buf);
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
bh_memcpy_s(wasm_file_mapped, wasm_file_size,
|
||||
wasm_file_buf, wasm_file_size);
|
||||
wasm_runtime_free(wasm_file_buf);
|
||||
wasm_file_buf = wasm_file_mapped;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
wasm_runtime_set_module_reader(module_reader_callback, moudle_destroyer);
|
||||
#endif
|
||||
@ -395,7 +419,10 @@ fail3:
|
||||
|
||||
fail2:
|
||||
/* free the file buffer */
|
||||
wasm_runtime_free(wasm_file_buf);
|
||||
if (!is_xip_mode)
|
||||
wasm_runtime_free(wasm_file_buf);
|
||||
else
|
||||
os_munmap(wasm_file_buf, wasm_file_size);
|
||||
|
||||
fail1:
|
||||
/* destroy runtime environment */
|
||||
|
||||
@ -10,11 +10,12 @@ QEMU_CORTEX_A53="qemu_cortex_a53"
|
||||
QEMU_XTENSA_TARGET="qemu_xtensa"
|
||||
QEMU_RISCV64_TARGET="qemu_riscv64"
|
||||
QEMU_RISCV32_TARGET="qemu_riscv32"
|
||||
QEMU_ARC_TARGET="qemu_arc"
|
||||
|
||||
usage ()
|
||||
{
|
||||
echo "USAGE:"
|
||||
echo "$0 $X86_TARGET|$STM32_TARGET|$ESP32_TARGET|$QEMU_CORTEX_A53|$QEMU_XTENSA_TARGET|$QEMU_RISCV64_TARGET|$QEMU_RISCV32_TARGET"
|
||||
echo "$0 $X86_TARGET|$STM32_TARGET|$ESP32_TARGET|$QEMU_CORTEX_A53|$QEMU_XTENSA_TARGET|$QEMU_RISCV64_TARGET|$QEMU_RISCV32_TARGET|$QEMU_ARC_TARGET"
|
||||
echo "Example:"
|
||||
echo " $0 $X86_TARGET"
|
||||
echo " $0 $STM32_TARGET"
|
||||
@ -88,6 +89,14 @@ case $TARGET in
|
||||
-DWAMR_BUILD_AOT=0
|
||||
west build -t run
|
||||
;;
|
||||
$QEMU_ARC_TARGET)
|
||||
west build -b qemu_arc_em \
|
||||
. -p always -- \
|
||||
-DCONF_FILE=prj_qemu_arc.conf \
|
||||
-DWAMR_BUILD_TARGET=ARC \
|
||||
-DWAMR_BUILD_AOT=0
|
||||
west build -t run
|
||||
;;
|
||||
*)
|
||||
echo "unsupported target: $TARGET"
|
||||
usage
|
||||
|
||||
6
product-mini/platforms/zephyr/simple/prj_qemu_arc.conf
Normal file
6
product-mini/platforms/zephyr/simple/prj_qemu_arc.conf
Normal file
@ -0,0 +1,6 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
CONFIG_STACK_SENTINEL=y
|
||||
CONFIG_PRINTK=y
|
||||
CONFIG_LOG=y
|
||||
@ -64,10 +64,42 @@ static void*
|
||||
app_instance_main(wasm_module_inst_t module_inst)
|
||||
{
|
||||
const char *exception;
|
||||
wasm_function_inst_t func;
|
||||
wasm_exec_env_t exec_env;
|
||||
unsigned argv[2] = { 0 };
|
||||
|
||||
if (wasm_runtime_lookup_function(module_inst, "main", NULL)
|
||||
|| wasm_runtime_lookup_function(module_inst,
|
||||
"__main_argc_argv", NULL)) {
|
||||
LOG_VERBOSE("Calling main funciton\n");
|
||||
wasm_application_execute_main(module_inst, app_argc, app_argv);
|
||||
}
|
||||
else if ((func = wasm_runtime_lookup_function(module_inst,
|
||||
"app_main", NULL))) {
|
||||
exec_env = wasm_runtime_create_exec_env(module_inst,
|
||||
CONFIG_APP_HEAP_SIZE);
|
||||
if (!exec_env) {
|
||||
os_printf("Create exec env failed\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LOG_VERBOSE("Calling app_main funciton\n");
|
||||
wasm_runtime_call_wasm(exec_env, func, 0, argv);
|
||||
|
||||
if (!wasm_runtime_get_exception(module_inst)) {
|
||||
os_printf("result: 0x%x\n", argv[0]);
|
||||
}
|
||||
|
||||
wasm_runtime_destroy_exec_env(exec_env);
|
||||
}
|
||||
else {
|
||||
os_printf("Failed to lookup function main or app_main to call\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wasm_application_execute_main(module_inst, app_argc, app_argv);
|
||||
if ((exception = wasm_runtime_get_exception(module_inst)))
|
||||
printf("%s\n", exception);
|
||||
os_printf("%s\n", exception);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user