blob: e65cd2541daa52e73f54e768fb86229843b50bd9 [file] [edit]
From: rive
Subject: [PATCH 0061] build for windows on arm
WAMR did not build for Windows on Arm: its arm64 call shim is ELF and
Mach-O assembly, whose .type directive COFF rejects. This adds a COFF form
of the simd shim with unwind info, as patch 0045 gave the x64 one, so a
crash in a native still walks past it to the host's crash reporter. It
keeps its scratch in caller saved registers and restores sp from the frame
pointer, which tells the unwinder where the stack args moved sp.
Natives then ran with every argument zero, as the call laid out its args
for Windows x64 on any Windows: four registers shared by ints and floats.
Windows on Arm passes them as AAPCS64 does, eight of each, and returns a
v128 in a vector register, so only x64 keeps that layout, and arm64 takes
the quick native dispatch too.
simde includes clang's arm_acle.h, whose crc32 declarations conflict with
msvc's intrin.h, so the fast interpreter hides __ARM_ACLE around it; the
wasm simd ops use no crc32. And os_icache_flush did nothing on Windows,
which x64 never notices, but arm64 can run stale instructions from a module
it just loaded.
diff --git a/core/iwasm/common/arch/invokeNative_mingw_aarch64_simd.s b/core/iwasm/common/arch/invokeNative_mingw_aarch64_simd.s
new file mode 100644
--- /dev/null
+++ b/core/iwasm/common/arch/invokeNative_mingw_aarch64_simd.s
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2020 Intel Corporation Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+/*
+ * rive patch: invokeNative_aarch64_simd.s for Windows, in COFF with unwind
+ * info, without which a crash in a native stops the SEH walk here, short of
+ * the host's crash reporter. Scratch registers replace the callee saved ones,
+ * and the frame pointer gives the unwinder the stack the args moved.
+ */
+ .text
+ .globl invokeNative
+ .def invokeNative
+ .scl 2
+ .type 32
+ .endef
+ .p2align 2
+invokeNative:
+ .seh_proc invokeNative
+
+/*
+ * Arguments passed in:
+ *
+ * x0 function ptr
+ * x1 argv
+ * x2 nstacks
+ */
+
+ stp x29, x30, [sp, #-16]!
+ .seh_save_fplr_x 16
+ mov x29, sp
+ .seh_set_fp
+ .seh_endprologue
+
+ mov x9, x0 /* x9 = function ptr */
+ mov x10, x1 /* x10 = argv */
+ mov x11, x2 /* x11 = nstacks */
+
+ /* Fill in float-point registers */
+ ld1 {v0.2D, v1.2D, v2.2D, v3.2D}, [x10], #64
+ ld1 {v4.2D, v5.2D, v6.2D, v7.2D}, [x10], #64
+
+ /* Fill integer registers */
+ ldp x0, x1, [x10], #16 /* x0 = argv[8] = exec_env, x1 = argv[9] */
+ ldp x2, x3, [x10], #16
+ ldp x4, x5, [x10], #16
+ ldp x6, x7, [x10], #16
+
+ /* Now x10 points to stack args */
+ cbz x11, call_func
+
+ /* Reserve 16 byte aligned stack space and copy the stack args */
+ lsl x12, x11, #3
+ add x12, x12, #15
+ and x12, x12, #-16
+ sub sp, sp, x12
+ mov x12, sp
+
+loop_stack_args:
+ ldr x13, [x10], #8
+ str x13, [x12], #8
+ subs x11, x11, #1
+ b.ne loop_stack_args
+
+call_func:
+ blr x9
+
+ .seh_startepilogue
+ mov sp, x29
+ .seh_set_fp
+ ldp x29, x30, [sp], #16
+ .seh_save_fplr_x 16
+ .seh_endepilogue
+ ret
+ .seh_endproc
diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c
--- a/core/iwasm/common/wasm_runtime_common.c
+++ b/core/iwasm/common/wasm_runtime_common.c
@@ -5868,12 +5868,17 @@ fail:
|| defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64)
+/* rive patch: Windows on Arm passes args as AAPCS64 does, only x64 differs */
+#if (defined(_WIN32) || defined(_WIN32_)) && !defined(BUILD_TARGET_AARCH64)
+#define WIN64_X64_INVOKE 1
+#endif
+
#if WASM_ENABLE_SIMD != 0
#ifdef v128
#undef v128
#endif
-#if defined(_WIN32) || defined(_WIN32_)
+#if defined(WIN64_X64_INVOKE)
typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
__int8 m128i_i8[16];
__int16 m128i_i16[8];
@@ -5925,10 +5930,10 @@ static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
#endif
/* NOLINTEND */
-#if defined(_WIN32) || defined(_WIN32_)
+#if defined(WIN64_X64_INVOKE)
#define MAX_REG_FLOATS 4
#define MAX_REG_INTS 4
-#else /* else of defined(_WIN32) || defined(_WIN32_) */
+#else /* else of defined(WIN64_X64_INVOKE) */
#define MAX_REG_FLOATS 8
#if defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64)
@@ -5938,7 +5943,7 @@ static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
#endif /* end of defined(BUILD_TARGET_AARCH64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64) */
-#endif /* end of defined(_WIN32) || defined(_WIN32_) */
+#endif /* end of defined(WIN64_X64_INVOKE) */
/*
* ASAN is not designed to work with custom stack unwind or other low-level
@@ -5980,7 +5985,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
#define fps ints
#endif /* end of BUILD_TARGET_RISCV64_LP64 */
-#if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
+#if defined(WIN64_X64_INVOKE) || defined(BUILD_TARGET_RISCV64_LP64)
/* important difference in calling conventions */
#define n_fps n_ints
#else
diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h
--- a/core/iwasm/common/wasm_runtime_common.h
+++ b/core/iwasm/common/wasm_runtime_common.h
@@ -1264,9 +1264,9 @@ wasm_runtime_rive_notify_start(WASMExecEnv *exec_env, bool begin);
arg classifier; relies on int and float args living in disjoint register
files, which Windows x64 does not honor */
#ifndef WASM_ENABLE_QUICK_NATIVE
-#if !defined(_WIN32) && !defined(_WIN32_) \
- && (defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_X86_64) \
- || defined(BUILD_TARGET_AMD_64))
+#if defined(BUILD_TARGET_AARCH64) \
+ || (!defined(_WIN32) && !defined(_WIN32_) \
+ && (defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)))
#define WASM_ENABLE_QUICK_NATIVE 1
#else
#define WASM_ENABLE_QUICK_NATIVE 0
diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c
--- a/core/iwasm/interpreter/wasm_interp_fast.c
+++ b/core/iwasm/interpreter/wasm_interp_fast.c
@@ -22,7 +22,16 @@
#endif
#if WASM_ENABLE_SIMDE != 0
+#if defined(_MSC_VER) && defined(__aarch64__)
+/* rive patch: simde includes clang's arm_acle.h for crc32 helpers the wasm
+ ops never use, whose declarations conflict with msvc's intrin.h */
+#pragma push_macro("__ARM_ACLE")
+#undef __ARM_ACLE
#include "simde/wasm/simd128.h"
+#pragma pop_macro("__ARM_ACLE")
+#else
+#include "simde/wasm/simd128.h"
+#endif
#endif
#if defined(__aarch64__) && defined(__ARM_NEON)
#include <arm_neon.h>
diff --git a/core/shared/platform/windows/platform_init.c b/core/shared/platform/windows/platform_init.c
--- a/core/shared/platform/windows/platform_init.c
+++ b/core/shared/platform/windows/platform_init.c
@@ -76,4 +76,7 @@ os_dcache_flush(void)
void
os_icache_flush(void *start, size_t len)
-{}
\ No newline at end of file
+{
+ /* rive patch: arm64 fetches stale code from loaded modules without it */
+ FlushInstructionCache(GetCurrentProcess(), start, len);
+}
\ No newline at end of file