From 12bcc20710464ea75ed99bb5a04929c0a240209e Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Mon, 28 Nov 2022 17:48:06 +0800 Subject: [PATCH] Implement invokeNative asm code for MinGW (#1753) And update the document of building iwasm for MinGW. --- .../common/arch/invokeNative_mingw_x64.s | 57 +++++++++++++++++++ .../common/arch/invokeNative_mingw_x64_simd.s | 57 +++++++++++++++++++ core/iwasm/common/iwasm_common.cmake | 12 +++- doc/build_wamr.md | 11 ++-- product-mini/platforms/windows/main.c | 4 +- 5 files changed, 132 insertions(+), 9 deletions(-) create mode 100644 core/iwasm/common/arch/invokeNative_mingw_x64.s create mode 100644 core/iwasm/common/arch/invokeNative_mingw_x64_simd.s diff --git a/core/iwasm/common/arch/invokeNative_mingw_x64.s b/core/iwasm/common/arch/invokeNative_mingw_x64.s new file mode 100644 index 000000000..cefaa28c1 --- /dev/null +++ b/core/iwasm/common/arch/invokeNative_mingw_x64.s @@ -0,0 +1,57 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +.text +.align 2 +.globl invokeNative +invokeNative: + + # %rcx func_ptr + # %rdx argv + # %r8 n_stacks + + push %rbp + mov %rsp, %rbp + + mov %rcx, %r10 # func_ptr + mov %rdx, %rax # argv + mov %r8, %rcx # n_stacks + + # fill all fp args + movsd 0(%rax), %xmm0 + movsd 8(%rax), %xmm1 + movsd 16(%rax), %xmm2 + movsd 24(%rax), %xmm3 + + # check for stack args + cmp $0, %rcx + jz cycle_end + + mov %rsp, %rdx + and $15, %rdx + jz no_abort + int $3 +no_abort: + mov %rcx, %rdx + and $1, %rdx + shl $3, %rdx + sub %rdx, %rsp + + # store stack args + lea 56(%rax, %rcx, 8), %r9 + sub %rsp, %r9 # offset +cycle: + push (%rsp, %r9) + loop cycle + +cycle_end: + mov 32(%rax), %rcx + mov 40(%rax), %rdx + mov 48(%rax), %r8 + mov 56(%rax), %r9 + + sub $32, %rsp # shadow space + + call *%r10 + leave + ret diff --git a/core/iwasm/common/arch/invokeNative_mingw_x64_simd.s b/core/iwasm/common/arch/invokeNative_mingw_x64_simd.s new file mode 100644 index 000000000..48ae52480 --- /dev/null +++ b/core/iwasm/common/arch/invokeNative_mingw_x64_simd.s @@ -0,0 +1,57 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +.text +.align 2 +.globl invokeNative +invokeNative: + + # %rcx func_ptr + # %rdx argv + # %r8 n_stacks + + push %rbp + mov %rsp, %rbp + + mov %rcx, %r10 # func_ptr + mov %rdx, %rax # argv + mov %r8, %rcx # n_stacks + + # fill all fp args + movdqu 0(%rax), %xmm0 + movdqu 16(%rax), %xmm1 + movdqu 32(%rax), %xmm2 + movdqu 48(%rax), %xmm3 + + # check for stack args + cmp $0, %rcx + jz cycle_end + + mov %rsp, %rdx + and $15, %rdx + jz no_abort + int $3 +no_abort: + mov %rcx, %rdx + and $1, %rdx + shl $3, %rdx + sub %rdx, %rsp + + # store stack args + lea 88(%rax, %rcx, 8), %r9 + sub %rsp, %r9 # offset +cycle: + push (%rsp, %r9) + loop cycle + +cycle_end: + mov 64(%rax), %rcx + mov 72(%rax), %rdx + mov 80(%rax), %r8 + mov 88(%rax), %r9 + + sub $32, %rsp # shadow space + + call *%r10 + leave + ret diff --git a/core/iwasm/common/iwasm_common.cmake b/core/iwasm/common/iwasm_common.cmake index de0826e2c..11d6e0eaa 100644 --- a/core/iwasm/common/iwasm_common.cmake +++ b/core/iwasm/common/iwasm_common.cmake @@ -27,13 +27,21 @@ if (WAMR_BUILD_INVOKE_NATIVE_GENERAL EQUAL 1) elseif (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64") if (NOT WAMR_BUILD_SIMD EQUAL 1) if (WAMR_BUILD_PLATFORM STREQUAL "windows") - set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_em64.asm) + if (NOT MINGW) + set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_em64.asm) + else () + set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_mingw_x64.s) + endif () else () set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_em64.s) endif () else () if (WAMR_BUILD_PLATFORM STREQUAL "windows") - set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_em64_simd.asm) + if (NOT MINGW) + set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_em64_simd.asm) + else () + set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_mingw_x64_simd.s) + endif () else() set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_em64_simd.s) endif() diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 18404e778..d3c7a8265 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -382,6 +382,8 @@ are valid for the MSYS2 build environment: ```Bash pacman -R cmake pacman -S mingw-w64-x86_64-cmake +pacman -S mingw-w64-x86_64-gcc +pacman -S make git ``` Then follow the build instructions for Windows above, and add the following @@ -389,16 +391,15 @@ arguments for cmake: ```Bash cmake .. -G"Unix Makefiles" \ - -DWAMR_BUILD_LIBC_UVWASI=0 \ - -DWAMR_BUILD_INVOKE_NATIVE_GENERAL=1 \ -DWAMR_DISABLE_HW_BOUND_CHECK=1 ```` Note that WASI will be disabled until further work is done towards full MinGW support. -- uvwasi not building out of the box, though it reportedly supports MinGW. -- Failing compilation of assembler files, the C version of `invokeNative()` will -be used instead. +- Since memory access boundary check with hardware trap feature is disabled, when generating the AOT file with `wamrc`, the `--bounds-checks=1` flag should be added to generate the memory access boundary check instructions to ensure the sandbox security: +```bash +wamrc --bounds-checks=1 -o +``` - Compiler complaining about missing `UnwindInfoAddress` field in `RUNTIME_FUNCTION` struct (winnt.h). diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index 8b1d6057e..399da4fb2 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -55,7 +55,7 @@ print_help() } /* clang-format on */ -static void * +static const void * app_instance_main(wasm_module_inst_t module_inst) { const char *exception; @@ -66,7 +66,7 @@ app_instance_main(wasm_module_inst_t module_inst) return exception; } -static void * +static const void * app_instance_func(wasm_module_inst_t module_inst, const char *func_name) { wasm_application_execute_func(module_inst, func_name, app_argc - 1,