1From 7eacb2ab839e74cb07038398def5e3cc198448d4 Mon Sep 17 00:00:00 2001
2From: Wenyong Huang <wenyong.huang@intel.com>
3Date: Tue, 23 Jan 2024 12:21:20 +0800
4Subject: [PATCH] Enhance setting write gs base with cmake variable (#3066)
5
6In linux x86-64, developer can use cmake variable to configure whether
7to enable writing linear memory base address to x86 GS register or not:
8- `cmake -DWAMR_DISABLE_WRITE_GS_BASE=1`: disabled it
9- `cmake -DWAMR_DISABLE_WRITE_GS_BASE=0`: enabled it
10- `cmake` without `-DWAMR_DISABLE_WRITE_GS_BASE=1/0`:
11        auto-detected by the compiler
12
13Upstream: https://github.com/bytecodealliance/wasm-micro-runtime/pull/3066
14Signed-off-by: Thomas Devoogdt <thomas@devoogdt.com>
15---
16 .../build-scripts/config_common.cmake         | 65 ++++++++++++-------
17 1 file changed, 41 insertions(+), 24 deletions(-)
18
19diff --git a/lib/wasm-micro-runtime-WAMR-1.3.0/build-scripts/config_common.cmake b/lib/wasm-micro-runtime-WAMR-1.3.0/build-scripts/config_common.cmake
20index e73ebc85f..a61a522f3 100644
21--- a/lib/wasm-micro-runtime-WAMR-1.3.0/build-scripts/config_common.cmake
22+++ b/lib/wasm-micro-runtime-WAMR-1.3.0/build-scripts/config_common.cmake
23@@ -408,32 +408,49 @@ if (WAMR_BUILD_STATIC_PGO EQUAL 1)
24   add_definitions (-DWASM_ENABLE_STATIC_PGO=1)
25   message ("     AOT static PGO enabled")
26 endif ()
27-if (WAMR_DISABLE_WRITE_GS_BASE EQUAL 1)
28-  add_definitions (-DWASM_DISABLE_WRITE_GS_BASE=1)
29-  message ("     Write linear memory base addr to x86 GS register disabled")
30-elseif (WAMR_BUILD_TARGET STREQUAL "X86_64"
31-        AND WAMR_BUILD_PLATFORM STREQUAL "linux")
32-  set (TEST_WRGSBASE_SOURCE "${CMAKE_BINARY_DIR}/test_wrgsbase.c")
33-  file (WRITE "${TEST_WRGSBASE_SOURCE}" "
34-  #include <stdio.h>
35-  #include <stdint.h>
36-  int main() {
37-      uint64_t value;
38-      asm volatile (\"wrgsbase %0\" : : \"r\"(value));
39-      printf(\"WRGSBASE instruction is available.\\n\");
40-      return 0;
41-  }")
42-  # Try to compile and run the test program
43-  try_run (TEST_WRGSBASE_RESULT
44-    TEST_WRGSBASE_COMPILED
45-    ${CMAKE_BINARY_DIR}/test_wrgsbase
46-    SOURCES ${TEST_WRGSBASE_SOURCE}
47-    CMAKE_FLAGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
48-  )
49-  #message("${TEST_WRGSBASE_COMPILED}, ${TEST_WRGSBASE_RESULT}")
50-  if (NOT TEST_WRGSBASE_RESULT EQUAL 0)
51+if (WAMR_BUILD_TARGET STREQUAL "X86_64"
52+    AND WAMR_BUILD_PLATFORM STREQUAL "linux")
53+  if (WAMR_DISABLE_WRITE_GS_BASE EQUAL 1)
54+    # disabled by user
55+    set (DISABLE_WRITE_GS_BASE 1)
56+  elseif (WAMR_DISABLE_WRITE_GS_BASE EQUAL 0)
57+    # enabled by user
58+    set (DISABLE_WRITE_GS_BASE 0)
59+  elseif (CMAKE_CROSSCOMPILING)
60+    # disabled in cross compilation environment
61+    set (DISABLE_WRITE_GS_BASE 1)
62+  else ()
63+    # auto-detected by the compiler
64+    set (TEST_WRGSBASE_SOURCE "${CMAKE_BINARY_DIR}/test_wrgsbase.c")
65+    file (WRITE "${TEST_WRGSBASE_SOURCE}" "
66+    #include <stdio.h>
67+    #include <stdint.h>
68+    int main() {
69+        uint64_t value;
70+        asm volatile (\"wrgsbase %0\" : : \"r\"(value));
71+        printf(\"WRGSBASE instruction is available.\\n\");
72+        return 0;
73+    }")
74+    # Try to compile and run the test program
75+    try_run (TEST_WRGSBASE_RESULT
76+      TEST_WRGSBASE_COMPILED
77+      ${CMAKE_BINARY_DIR}/test_wrgsbase
78+      SOURCES ${TEST_WRGSBASE_SOURCE}
79+      CMAKE_FLAGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
80+    )
81+    #message("${TEST_WRGSBASE_COMPILED}, ${TEST_WRGSBASE_RESULT}")
82+    if (TEST_WRGSBASE_RESULT EQUAL 0)
83+      set (DISABLE_WRITE_GS_BASE 0)
84+    else ()
85+      set (DISABLE_WRITE_GS_BASE 1)
86+    endif ()
87+  endif ()
88+  if (DISABLE_WRITE_GS_BASE EQUAL 1)
89     add_definitions (-DWASM_DISABLE_WRITE_GS_BASE=1)
90     message ("     Write linear memory base addr to x86 GS register disabled")
91+  else ()
92+    add_definitions (-DWASM_DISABLE_WRITE_GS_BASE=0)
93+    message ("     Write linear memory base addr to x86 GS register enabled")
94   endif ()
95 endif ()
96 if (WAMR_CONFIGUABLE_BOUNDS_CHECKS EQUAL 1)
97--
982.34.1
99
100