Просмотр исходного кода

Implement invokeNative asm code for MinGW (#1753)

And update the document of building iwasm for MinGW.
Wenyong Huang 3 лет назад
Родитель
Сommit
12bcc20710

+ 57 - 0
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

+ 57 - 0
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

+ 10 - 2
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()

+ 6 - 5
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 <aot_file> <wasm_file>
+```
 - Compiler complaining about missing `UnwindInfoAddress` field in `RUNTIME_FUNCTION`
 struct (winnt.h).
 

+ 2 - 2
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,