From 490fa2ddaccd73834a69e60ce248620e1e0b0c40 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 9 Aug 2023 19:43:08 +0800 Subject: [PATCH] Auto-check wrgsbase in cmake script (#2437) Auto-check whether `WRGSBASE` instruction is supported in linux x86-64 in the cmake script. And if not, disable writing x86 GS register. --- build-scripts/config_common.cmake | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 6b4247e0..73ed83c9 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -395,6 +395,30 @@ endif () if (WAMR_DISABLE_WRITE_GS_BASE EQUAL 1) add_definitions (-DWASM_DISABLE_WRITE_GS_BASE=1) message (" Write linear memory base addr to x86 GS register disabled") +elseif (WAMR_BUILD_TARGET STREQUAL "X86_64" + AND WAMR_BUILD_PLATFORM STREQUAL "linux") + set (TEST_WRGSBASE_SOURCE "${CMAKE_BINARY_DIR}/test_wrgsbase.c") + file (WRITE "${TEST_WRGSBASE_SOURCE}" " + #include + #include + int main() { + uint64_t value; + asm volatile (\"wrgsbase %0\" : : \"r\"(value)); + printf(\"WRGSBASE instruction is available.\\n\"); + return 0; + }") + # Try to compile and run the test program + try_run (TEST_WRGSBASE_RESULT + TEST_WRGSBASE_COMPILED + ${CMAKE_BINARY_DIR}/test_wrgsbase + SOURCES ${TEST_WRGSBASE_SOURCE} + CMAKE_FLAGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + ) + #message("${TEST_WRGSBASE_COMPILED}, ${TEST_WRGSBASE_RESULT}") + if (NOT TEST_WRGSBASE_RESULT EQUAL 0) + add_definitions (-DWASM_DISABLE_WRITE_GS_BASE=1) + message (" Write linear memory base addr to x86 GS register disabled") + endif () endif () if (WAMR_CONFIGUABLE_BOUNDS_CHECKS EQUAL 1) add_definitions (-DWASM_CONFIGURABLE_BOUNDS_CHECKS=1)