From 7df709e7fec767c2a9ba1af38e9c101b90578f3b Mon Sep 17 00:00:00 2001 From: Derek Bruening Date: Fri, 10 Nov 2023 01:03:47 -0500 Subject: [PATCH] i#2267: Expect floatpc failure on AMD processor Adds CMake detection of the processor vendor. Relaxes the floatpc_xl8all test to expect failure when running on AMD for inter-block cases, where DR won't be able to translate as the processor does not supply the PC if there wasn't an exception. Issue: #2267 --- CMakeLists.txt | 7 +++++++ core/arch/x86/mangle.c | 7 +++++++ make/utils.cmake | 25 +++++++++++++++++++++++++ suite/tests/CMakeLists.txt | 5 +++++ suite/tests/common/floatpc.c | 2 +- suite/tests/common/floatpc.template | 4 ++-- 6 files changed, 47 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c1bb8ea09bc..45139a26e85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -320,6 +320,13 @@ if (NOT "${TARGET_ARCH}" STREQUAL "${CMAKE_SYSTEM_PROCESSOR}") endif () endif () +get_processor_vendor(CPU_VENDOR) +if ("${CPU_VENDOR}" STREQUAL "GenuineIntel") + set(CPU_INTEL ON) +elseif ("${CPU_VENDOR}" STREQUAL "AuthenticAMD") + set(CPU_AMD ON) +endif () + # Support for running cross-compiled tests under emulation. if (CMAKE_CROSSCOMPILING AND DEFINED CMAKE_FIND_ROOT_PATH) find_program(QEMU_BINARY qemu-${CMAKE_SYSTEM_PROCESSOR} DOC "QEMU emulation tool") diff --git a/core/arch/x86/mangle.c b/core/arch/x86/mangle.c index d91e808fc31..85eb7a00ad0 100644 --- a/core/arch/x86/mangle.c +++ b/core/arch/x86/mangle.c @@ -2322,6 +2322,13 @@ void mangle_float_pc(dcontext_t *dcontext, instrlist_t *ilist, instr_t *instr, instr_t *next_instr, uint *flags INOUT) { + /* XXX i#2267: AMD processors don't provide the last PC when there was no + * exception. Recent processors seem to zero it out. Should we do the same, + * and not supply it for intra-block cases? For now we do supply it, and + * we try (but fail) for inter-block for -translate_fpu_pc (we expect failure + * in our floatpc_xl8all test template for AMD). + */ + /* If there is a prior non-control float instr, we can inline the pc update. * Otherwise, we go back to d_r_dispatch. In the latter case we do not support * building traces across the float pc save: we assume it's rare. diff --git a/make/utils.cmake b/make/utils.cmake index f8f2edc8e32..1b346febf48 100644 --- a/make/utils.cmake +++ b/make/utils.cmake @@ -342,3 +342,28 @@ function (check_sve_processor_and_compiler_support out) endif () set(${out} ${proc_found_sve} PARENT_SCOPE) endfunction (check_sve_processor_and_compiler_support) + +function (get_processor_vendor out) + set(vendor "") + if (APPLE) + find_program(SYSCTL NAMES sysctl PATHS /usr/sbin) + if (SYSCTL) + execute_process(COMMAND ${SYSCTL} -n machdep.cpu.vendor + OUTPUT_VARIABLE vendor OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_VARIABLE err) + if ("${vendor}" STREQUAL "") + execute_process(COMMAND ${SYSCTL} -n machdep.cpu.brand_string + OUTPUT_VARIABLE vendor OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_VARIABLE err) + endif () + endif () + elseif (WIN32) + get_filename_component(vendor + "[HKEY_LOCAL_MACHINE\\Hardware\\Description\\System\\CentralProcessor\\0;VendorIdentifier]" + NAME CACHE) + elseif (EXISTS "/proc/cpuinfo") + file(READ "/proc/cpuinfo" contents) + string(REGEX REPLACE ".*vendor_id[ \t]*:[ \t]+([a-zA-Z0-9_-]+).*" "\\1" + vendor "${contents}") + endif () + message(STATUS "Processor vendor is ${vendor}") + set(${out} ${vendor} PARENT_SCOPE) +endfunction () diff --git a/suite/tests/CMakeLists.txt b/suite/tests/CMakeLists.txt index 1627c9f1287..476279fb21f 100644 --- a/suite/tests/CMakeLists.txt +++ b/suite/tests/CMakeLists.txt @@ -1050,6 +1050,11 @@ function(template2expect outexpect template runops key) elseif (DEFINED ${key}_runsve) set(rundefs "${rundefs} -D__ARM_FEATURE_SVE") endif () + if (CPU_AMD) + set(rundefs "${rundefs} -DCPU_AMD") + elseif (CPU_INTEL) + set(rundefs "${rundefs} -DCPU_INTEL") + endif () if (DEFINED ${key}_test_sample_client) set(rundefs "${rundefs} -DTEST_SAMPLE_CLIENT") diff --git a/suite/tests/common/floatpc.c b/suite/tests/common/floatpc.c index 0df27620c73..be273776fa4 100644 --- a/suite/tests/common/floatpc.c +++ b/suite/tests/common/floatpc.c @@ -1,5 +1,5 @@ /* ********************************************************** - * Copyright (c) 2013 Google, Inc. All rights reserved. + * Copyright (c) 2013-2023 Google, Inc. All rights reserved. * **********************************************************/ /* diff --git a/suite/tests/common/floatpc.template b/suite/tests/common/floatpc.template index 363d6b56e98..91770dbc022 100644 --- a/suite/tests/common/floatpc.template +++ b/suite/tests/common/floatpc.template @@ -1,6 +1,6 @@ #ifdef X64 FXSAVE64 intra is correctly handled -# if defined(translate_fpu_pc) || defined(hotp_only) +# if (defined(translate_fpu_pc) || defined(hotp_only)) && !defined(CPU_AMD) FXSAVE64 inter is correctly handled # else FXSAVE64 inter is **incorrectly** handled @@ -14,7 +14,7 @@ FNSTENV inter is **incorrectly** handled # endif #endif FXSAVE intra is correctly handled -#if defined(translate_fpu_pc) || defined(hotp_only) +# if (defined(translate_fpu_pc) || defined(hotp_only)) && !defined(CPU_AMD) FXSAVE inter is correctly handled #else FXSAVE inter is **incorrectly** handled