From 93b29b886e8665e368598c711279d45b7e5d066c Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Mon, 4 Nov 2019 13:42:39 +0000 Subject: [PATCH] Utility for classifying the current RTM behaviour Signed-off-by: Andrew Cooper --- arch/x86/include/arch/cpuid.h | 2 ++ arch/x86/include/arch/tsx.h | 16 +++++++++++++ docs/all-tests.dox | 2 ++ tests/rtm-check/Makefile | 9 +++++++ tests/rtm-check/main.c | 45 +++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+) create mode 100644 tests/rtm-check/Makefile create mode 100644 tests/rtm-check/main.c diff --git a/arch/x86/include/arch/cpuid.h b/arch/x86/include/arch/cpuid.h index f91117a..4a117a9 100644 --- a/arch/x86/include/arch/cpuid.h +++ b/arch/x86/include/arch/cpuid.h @@ -89,7 +89,9 @@ static inline bool cpu_has(unsigned int feature) #define cpu_has_svm cpu_has(X86_FEATURE_SVM) #define cpu_has_fsgsbase cpu_has(X86_FEATURE_FSGSBASE) +#define cpu_has_hle cpu_has(X86_FEATURE_HLE) #define cpu_has_smep cpu_has(X86_FEATURE_SMEP) +#define cpu_has_rtm cpu_has(X86_FEATURE_RTM) #define cpu_has_smap cpu_has(X86_FEATURE_SMAP) #define cpu_has_umip cpu_has(X86_FEATURE_UMIP) diff --git a/arch/x86/include/arch/tsx.h b/arch/x86/include/arch/tsx.h index 503f4a3..c54ff1b 100644 --- a/arch/x86/include/arch/tsx.h +++ b/arch/x86/include/arch/tsx.h @@ -21,8 +21,10 @@ #define XTF_X86_TSX_H #include +#include #define _XBEGIN_STARTED (~0u) +#define _XBEGIN_UD EXINFO_SYM(UD, 0) #define _XABORT_EXPLICIT (1u << 0) #define _XABORT_RETRY (1u << 1) #define _XABORT_CONFLICT (1u << 2) @@ -41,6 +43,20 @@ static inline unsigned int _xbegin(void) return ret; } +/* Like _xbegin(), but will catch #UD as well. */ +static inline unsigned int _xbegin_safe(void) +{ + unsigned int ret = _XBEGIN_STARTED; + + asm volatile ("1: .byte 0xc7, 0xf8, 0, 0, 0, 0; 2:" /* xbegin 2f; 2: */ + _ASM_EXTABLE_HANDLER(1b, 2b, %P[rec]) + : "+a" (ret) + : [rec] "p" (ex_record_fault_eax) + : "memory"); + + return ret; +} + static inline int _xtest(void) { int rc; diff --git a/docs/all-tests.dox b/docs/all-tests.dox index 902fc44..322f078 100644 --- a/docs/all-tests.dox +++ b/docs/all-tests.dox @@ -170,6 +170,8 @@ states. @subpage test-msr - Print MSR information. +@subpage test-rtm-check - Probe for the RTM behaviour. + @section index-in-development In Development diff --git a/tests/rtm-check/Makefile b/tests/rtm-check/Makefile new file mode 100644 index 0000000..010cda2 --- /dev/null +++ b/tests/rtm-check/Makefile @@ -0,0 +1,9 @@ +include $(ROOT)/build/common.mk + +NAME := rtm-check +CATEGORY := utility +TEST-ENVS := pv64 hvm64 + +obj-perenv += main.o + +include $(ROOT)/build/gen.mk diff --git a/tests/rtm-check/main.c b/tests/rtm-check/main.c new file mode 100644 index 0000000..eadee26 --- /dev/null +++ b/tests/rtm-check/main.c @@ -0,0 +1,45 @@ +/** + * @file tests/rtm-check/main.c + * @ref test-rtm-check + * + * @page test-rtm-check rtm-check + * + * Probe the RTM behaviour on the system, independently of CPUID settings. + * Classifies as usable, unavailable (@#UD) or always aborting. + * + * @see tests/rtm-check/main.c + */ +#include + +const char test_title[] = "Test RTM behaviour"; + +void test_main(void) +{ + printk("CPUID: HLE %u, RTM %u\n", + cpu_has_hle, cpu_has_rtm); + + for ( int i = 0; i < 1000; ++i ) + { + unsigned int xstatus = _xbegin_safe(); + + if ( xstatus == _XBEGIN_STARTED ) + { + _xend(); + return xtf_success("RTM usable\n"); + } + else if ( xstatus == _XBEGIN_UD ) + return xtf_success("RTM unavailable\n"); + } + + xtf_success("RTM always aborting\n"); +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ -- 2.39.5