x86: xsave: Cleanups and AVX testing
Sean Christopherson
reviewerv2 cover x86: xsave: Cleanups and AVX testing
A "slightly" beefed up version of Paolo's patch to validate KVM's recently added AVX VMOVDQA emulation.
v1: https://lore.kernel.org/all/[email protected]
Paolo Bonzini (1):
x86: xsave: Add testcase for emulation of AVX instructions
Sean Christopherson (10):
x86: xsave: Replace spaces with tabs
x86: xsave: Drop unnecessary and confusing uint64_t overrides
x64: xsave: Use non-safe write_cr4() when toggling OSXSAVE
x86: xsave: Add and use dedicated XCR0 read/write helpers
x86: xsave: Dedup XGETBV and XSETBV #UD tests
x86: xsave: Programmatically test more unsupported XCR accesses
x86: xsave: Define XFEATURE_MASK_<feature> bits in processor.h
x86: xsave: Always verify XCR0 is actually written
x86: xsave: Drop remaining indentation quirks and printf markers
x86: xsave: Verify XSETBV and XGETBV ignore RCX[63:32]
lib/x86/processor.h | 62 ++++++++++ x86/xsave.c | 270 +++++++++++++++++++++++++++++--------------- 2 files changed, 239 insertions(+), 93 deletions(-)
base-commit: f561b31d3dee01f8be58978be23bb0903543153d
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 1/11 x86: xsave: Replace spaces with tabs
Replace spaces with tabs in the XSAVE test so that upcoming changes don't have to carry forward the non-standard formatting.
No functional change intended.
--- x86/xsave.c | 198 ++++++++++++++++++++++++++-------------------------- 1 file changed, 99 insertions(+), 99 deletions(-)
x86/xsave.c +99 -99
@@ -8,120 +8,120 @@#define uint64_t unsigned long long#endif -#define XCR_XFEATURE_ENABLED_MASK 0x00000000 -#define XCR_XFEATURE_ILLEGAL_MASK 0x00000010 +#define XCR_XFEATURE_ENABLED_MASK 0x00000000 +#define XCR_XFEATURE_ILLEGAL_MASK 0x00000010 -#define XSTATE_FP 0x1 -#define XSTATE_SSE 0x2 -#define XSTATE_YMM 0x4 +#define XSTATE_FP 0x1 +#define XSTATE_SSE 0x2 +#define XSTATE_YMM 0x4static void test_xsave (void ) { -unsigned long cr4; -uint64_t supported_xcr0; -uint64_t test_bits; -u64 xcr0; - -printf ("Legal instruction testing:\n" ); - - supported_xcr0 =this_cpu_supported_xcr0 (); -printf ("Supported XCR0 bits: %#lx\n" , supported_xcr0); - - test_bits =XSTATE_FP |XSTATE_SSE ; -report ((supported_xcr0 & test_bits) == test_bits, -"Check minimal XSAVE required bits" ); - - cr4 =read_cr4 (); -report (write_cr4_safe (cr4 |X86_CR4_OSXSAVE ) ==0 ,"Set CR4 OSXSAVE" ); -report (this_cpu_has (X86_FEATURE_OSXSAVE ), -"Check CPUID.1.ECX.OSXSAVE - expect 1" ); - -printf ("\tLegal tests\n" ); - test_bits =XSTATE_FP ; -report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==0 , -"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP)" ); - - test_bits =XSTATE_FP |XSTATE_SSE ; -report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==0 , -"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP | XSTATE_SSE)" ); -report (xgetbv_safe (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==0 , -" xgetbv(XCR_XFEATURE_ENABLED_MASK)" ); - -printf ("\tIllegal tests\n" ); - test_bits =0 ; -report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==GP_VECTOR , -"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, 0) - expect #GP" ); - - test_bits =XSTATE_SSE ; -report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==GP_VECTOR , -"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_SSE) - expect #GP" ); - -if (supported_xcr0 &XSTATE_YMM ) { - test_bits =XSTATE_YMM ; -report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==GP_VECTOR , -"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_YMM) - expect #GP" ); - - test_bits =XSTATE_FP |XSTATE_YMM ; -report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==GP_VECTOR , -"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP | XSTATE_YMM) - expect #GP" ); - } - - test_bits =XSTATE_SSE ; -report (xsetbv_safe (XCR_XFEATURE_ILLEGAL_MASK , test_bits) ==GP_VECTOR , -"\t\txsetbv(XCR_XFEATURE_ILLEGAL_MASK, XSTATE_FP) - expect #GP" ); - - test_bits =XSTATE_SSE ; -report (xsetbv_safe (XCR_XFEATURE_ILLEGAL_MASK , test_bits) ==GP_VECTOR , -"\t\txgetbv(XCR_XFEATURE_ILLEGAL_MASK, XSTATE_FP) - expect #GP" ); - - cr4 &= ~X86_CR4_OSXSAVE ; -report (write_cr4_safe (cr4) ==0 ,"Unset CR4 OSXSAVE" ); -report (this_cpu_has (X86_FEATURE_OSXSAVE ) ==0 , -"Check CPUID.1.ECX.OSXSAVE - expect 0" ); - -printf ("\tIllegal tests:\n" ); - test_bits =XSTATE_FP ; -report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==UD_VECTOR , -"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP) - expect #UD" ); - - test_bits =XSTATE_FP |XSTATE_SSE ; -report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==UD_VECTOR , -"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP | XSTATE_SSE) - expect #UD" ); - -printf ("\tIllegal tests:\n" ); -report (xgetbv_safe (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==UD_VECTOR , -"\txgetbv(XCR_XFEATURE_ENABLED_MASK) - expect #UD" ); +unsigned long cr4; +uint64_t supported_xcr0; +uint64_t test_bits; +u64 xcr0; + +printf ("Legal instruction testing:\n" ); + + supported_xcr0 =this_cpu_supported_xcr0 (); +printf ("Supported XCR0 bits: %#lx\n" , supported_xcr0); + + test_bits =XSTATE_FP |XSTATE_SSE ; +report ((supported_xcr0 & test_bits) == test_bits, +"Check minimal XSAVE required bits" ); + + cr4 =read_cr4 (); +report (write_cr4_safe (cr4 |X86_CR4_OSXSAVE ) ==0 ,"Set CR4 OSXSAVE" ); +report (this_cpu_has (X86_FEATURE_OSXSAVE ), +"Check CPUID.1.ECX.OSXSAVE - expect 1" ); + +printf ("\tLegal tests\n" ); + test_bits =XSTATE_FP ; +report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==0 , +"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP)" ); + + test_bits =XSTATE_FP |XSTATE_SSE ; +report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==0 , +"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP | XSTATE_SSE)" ); +report (xgetbv_safe (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==0 , +"\t\txgetbv(XCR_XFEATURE_ENABLED_MASK)" ); + +printf ("\tIllegal tests\n" ); + test_bits =0 ; +report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==GP_VECTOR , +"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, 0) - expect #GP" ); + + test_bits =XSTATE_SSE ; +report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==GP_VECTOR , +"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_SSE) - expect #GP" ); + +if (supported_xcr0 &XSTATE_YMM ) { + test_bits =XSTATE_YMM ; +report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==GP_VECTOR , +"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_YMM) - expect #GP" ); + + test_bits =XSTATE_FP |XSTATE_YMM ; +report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==GP_VECTOR , +"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP | XSTATE_YMM) - expect #GP" ); + } + + test_bits =XSTATE_SSE ; +report (xsetbv_safe (XCR_XFEATURE_ILLEGAL_MASK , test_bits) ==GP_VECTOR , +"\t\txsetbv(XCR_XFEATURE_ILLEGAL_MASK, XSTATE_FP) - expect #GP" ); + + test_bits =XSTATE_SSE ; +report (xsetbv_safe (XCR_XFEATURE_ILLEGAL_MASK , test_bits) ==GP_VECTOR , +"\t\txgetbv(XCR_XFEATURE_ILLEGAL_MASK, XSTATE_FP) - expect #GP" ); + + cr4 &= ~X86_CR4_OSXSAVE ; +report (write_cr4_safe (cr4) ==0 ,"Unset CR4 OSXSAVE" ); +report (this_cpu_has (X86_FEATURE_OSXSAVE ) ==0 , +"Check CPUID.1.ECX.OSXSAVE - expect 0" ); + +printf ("\tIllegal tests:\n" ); + test_bits =XSTATE_FP ; +report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==UD_VECTOR , +"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP) - expect #UD" ); + + test_bits =XSTATE_FP |XSTATE_SSE ; +report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==UD_VECTOR , +"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP | XSTATE_SSE) - expect #UD" ); + +printf ("\tIllegal tests:\n" ); +report (xgetbv_safe (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==UD_VECTOR , +"\txgetbv(XCR_XFEATURE_ENABLED_MASK) - expect #UD" ); }static void test_no_xsave (void ) { -unsigned long cr4; -u64 xcr0; +unsigned long cr4; +u64 xcr0; -report (this_cpu_has (X86_FEATURE_OSXSAVE ) ==0 , -"Check CPUID.1.ECX.OSXSAVE - expect 0" ); +report (this_cpu_has (X86_FEATURE_OSXSAVE ) ==0 , +"Check CPUID.1.ECX.OSXSAVE - expect 0" ); -printf ("Illegal instruction testing:\n" ); +printf ("Illegal instruction testing:\n" ); - cr4 =read_cr4 (); -report (write_cr4_safe (cr4 |X86_CR4_OSXSAVE ) ==GP_VECTOR , -"Set OSXSAVE in CR4 - expect #GP" ); + cr4 =read_cr4 (); +report (write_cr4_safe (cr4 |X86_CR4_OSXSAVE ) ==GP_VECTOR , +"Set OSXSAVE in CR4 - expect #GP" ); -report (xgetbv_safe (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==UD_VECTOR , -"Execute xgetbv - expect #UD" ); +report (xgetbv_safe (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==UD_VECTOR , +"Execute xgetbv - expect #UD" ); -report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK ,0x3 ) ==UD_VECTOR , -"Execute xsetbv - expect #UD" ); +report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK ,0x3 ) ==UD_VECTOR , +"Execute xsetbv - expect #UD" ); }int main (void ) { -if (this_cpu_has (X86_FEATURE_XSAVE )) { -printf ("CPU has XSAVE feature\n" ); -test_xsave (); - }else { -printf ("CPU don't has XSAVE feature\n" ); -test_no_xsave (); - } -return report_summary (); +if (this_cpu_has (X86_FEATURE_XSAVE )) { +printf ("CPU has XSAVE feature\n" ); +test_xsave (); + }else { +printf ("CPU don't has XSAVE feature\n" ); +test_no_xsave (); + } +return report_summary (); }
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 2/11 x86: xsave: Drop unnecessary and confusing uint64_t overrides
In reply to Sean ChristophersonDrop the uint64_t #defines from the XSAVE test, as they are unnecessary (typedef'd by stdint-uintn.h) and confusing (the test is 64-bit only).
Opportunistically use "u64" throughout the test, for brevity and for consistency (tracking xcr0 as two different types is bizarre).
No functional change intended.
--- x86/xsave.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
x86/xsave.c +1 -9
@@ -2,12 +2,6 @@#include "desc.h" #include "processor.h" -#ifdef __x86_64__ -#define uint64_t unsigned long -#else -#define uint64_t unsigned long long -#endif -#define XCR_XFEATURE_ENABLED_MASK 0x00000000#define XCR_XFEATURE_ILLEGAL_MASK 0x00000010
@@ -17,10 +11,8 @@static void test_xsave (void ) { +u64 supported_xcr0, xcr0, test_bits;unsigned long cr4; -uint64_t supported_xcr0; -uint64_t test_bits; -u64 xcr0;printf ("Legal instruction testing:\n" );
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 3/11 x64: xsave: Use non-safe write_cr4() when toggling OSXSAVE
In reply to Sean ChristophersonUse the non-safe variant of write_cr4() when toggling OSXSAVE as continuing on if XSAVE support can't be enabled is pointless.
--- x86/xsave.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
x86/xsave.c +3 -3
@@ -24,7 +24,8 @@ static void test_xsave(void) "Check minimal XSAVE required bits"); cr4 =read_cr4 (); -report (write_cr4_safe (cr4 |X86_CR4_OSXSAVE ) ==0 ,"Set CR4 OSXSAVE" ); +write_cr4 (cr4 |X86_CR4_OSXSAVE ); +report (this_cpu_has (X86_FEATURE_OSXSAVE ),"Check CPUID.1.ECX.OSXSAVE - expect 1" );
@@ -66,8 +67,7 @@ static void test_xsave(void)report (xsetbv_safe (XCR_XFEATURE_ILLEGAL_MASK , test_bits) ==GP_VECTOR ,"\t\txgetbv(XCR_XFEATURE_ILLEGAL_MASK, XSTATE_FP) - expect #GP" ); - cr4 &= ~X86_CR4_OSXSAVE ; -report (write_cr4_safe (cr4) ==0 ,"Unset CR4 OSXSAVE" ); +write_cr4 (cr4 & ~X86_CR4_OSXSAVE );report (this_cpu_has (X86_FEATURE_OSXSAVE ) ==0 ,"Check CPUID.1.ECX.OSXSAVE - expect 0" );
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 4/11 x86: xsave: Add and use dedicated XCR0 read/write helpers
In reply to Sean ChristophersonAdd dedicated {read,write}_xcr0{,_safe}() helpers so that tests don't need to manually pass '0' for the common case of accessing XCR0.
Use the helpers in the XSTATE tests, and intentionally use the non-safe variants for the initial writes to set XCR0=FP and XCR0=FP|SSE. There is no point continuing on if the most basic setup fails.
Opportunistically clean up the report messages to make them human friendly, and fix typos in the xsetbv_safe(XCR_XFEATURE_ILLEGAL_MASK, SSE) report message.
--- lib/x86/processor.h | 38 +++++++++++++++++++++++++++++++ x86/xsave.c | 55 +++++++++++++++++---------------------------- 2 files changed, 59 insertions(+), 34 deletions(-)
lib/x86/processor.h +38 -0
@@ -701,6 +701,44 @@ static inline int xsetbv_safe(u32 index, u64 value)return wrreg64_safe (".byte 0x0f,0x01,0xd1" , index, value); } +static inline u64 xgetbv (u32 index) +{ +u64 value; +int vector =xgetbv_safe (index, &value); + +assert_msg (!vector,"Unexpected exception '%s' reading XCR%u" , +exception_mnemonic (vector), index); +return value; +} + +static inline void xsetbv (u32 index,u64 value) +{ +int vector =xsetbv_safe (index, value); + +assert_msg (!vector,"Unexpected exception '%s' writing XCR%u = 0x%" PRIx64, +exception_mnemonic (vector), index, value); +} + +static inline int read_xcr0_safe (u64 *value) +{ +return xgetbv_safe (0 , value); +} + +static inline int write_xcr0_safe (u64 value) +{ +return xsetbv_safe (0 , value); +} + +static inline u64 read_xcr0 (void ) +{ +return xgetbv (0 ); +} + +static inline void write_xcr0 (u64 value) +{ +xsetbv (0 , value); +} +static inline int write_cr0_safe (ulong val) {return asm_safe ("mov %0,%%cr0" ,"r" (val));
x86/xsave.c +21 -34
89 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 5/11 x86: xsave: Dedup XGETBV and XSETBV #UD tests
In reply to Sean ChristophersonDeduplicate the "no XSAVE support" and "CR4.OSXSAVE=0" #UD tests as the two scenarios are identical for all intents and purposes.
--- x86/xsave.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-)
x86/xsave.c +19 -25
@@ -10,7 +10,7 @@static void test_xsave (void ) { -u64 supported_xcr0, xcr0, test_bits; +u64 supported_xcr0, test_bits;unsigned long cr4;printf ("Legal instruction testing:\n" );
@@ -56,51 +56,45 @@ static void test_xsave(void)report (xsetbv_safe (XCR_XFEATURE_ILLEGAL_MASK , test_bits) ==GP_VECTOR ,"\t\txgetbv(XCR_XFEATURE_ILLEGAL_MASK, XSTATE_FP) - expect #GP" ); -write_cr4 (cr4 & ~X86_CR4_OSXSAVE ); -report (this_cpu_has (X86_FEATURE_OSXSAVE ) ==0 , -"Check CPUID.1.ECX.OSXSAVE - expect 0" ); +write_cr4 (cr4); -printf ("\tIllegal tests:\n" ); -report (write_xcr0_safe (XSTATE_FP ) ==UD_VECTOR , -"\t\tWrite XCR0=FP with CR4.OSXSAVE=0 - expect #UD" ); - -report (write_xcr0_safe (XSTATE_FP |XSTATE_SSE ) ==UD_VECTOR , -"\t\tWrite XCR0=(FP|SSE) with CR4.OSXSAVE=0 - expect #UD" ); - -printf ("\tIllegal tests:\n" ); -report (read_xcr0_safe (&xcr0) ==UD_VECTOR , -"\tRead XCR0 with CR4.OSXSAVE=0 - expect #UD" ); +report (this_cpu_has (X86_FEATURE_OSXSAVE ) == !!(cr4 &X86_CR4_OSXSAVE ), +"CPUID.1.ECX.OSXSAVE == CR4.OSXSAVE" ); }static void test_no_xsave (void ) { -unsigned long cr4; +unsigned long cr4 =read_cr4 ();u64 xcr0; +if (cr4 &X86_CR4_OSXSAVE ) +write_cr4 (cr4 & ~X86_CR4_OSXSAVE ); +report (this_cpu_has (X86_FEATURE_OSXSAVE ) ==0 ,"Check CPUID.1.ECX.OSXSAVE - expect 0" ); -printf ("Illegal instruction testing:\n" ); - - cr4 =read_cr4 (); -report (write_cr4_safe (cr4 |X86_CR4_OSXSAVE ) ==GP_VECTOR , -"Set OSXSAVE in CR4 - expect #GP" ); -report (read_xcr0_safe (&xcr0) ==UD_VECTOR , -"Read XCR0 without XSAVE support - expect #UD" ); +"Read XCR0 without OSXSAVE enabled - expect #UD" );report (write_xcr0_safe (XSTATE_FP |XSTATE_SSE ) ==UD_VECTOR ,"Write XCR0=(FP|SSE) without XSAVE support - expect #UD" ); + +if (cr4 &X86_CR4_OSXSAVE ) +write_cr4 (cr4); }int main (void ) { +test_no_xsave (); +if (this_cpu_has (X86_FEATURE_XSAVE )) { -printf ("CPU has XSAVE feature\n" );test_xsave (); }else { -printf ("CPU don't has XSAVE feature\n" ); -test_no_xsave (); +report_skip ("XSAVE unsupported, skipping positive tests" ); + +report (write_cr4_safe (read_cr4 () |X86_CR4_OSXSAVE ) ==GP_VECTOR , +"Set CR4.OSXSAVE without XSAVE- expect #GP" ); } +return report_summary (); }
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 6/11 x86: xsave: Programmatically test more unsupported XCR accesses
In reply to Sean ChristophersonExpand the XSAVE test's coverage for unsupported XCRs to more than just XCR16. Somewhat arbitrarily test XCRs 1-63, and XCRs [bit1:bit31] as a middle ground between hardcoding a few XCRs and exhaustively testing all four billion possibilities.
--- x86/xsave.c | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-)
x86/xsave.c +36 -9
@@ -2,12 +2,45 @@#include "desc.h" #include "processor.h" -#define XCR_XFEATURE_ILLEGAL_MASK 0x00000010 -#define XSTATE_FP 0x1#define XSTATE_SSE 0x2#define XSTATE_YMM 0x4 +static void test_unsupported_xcrs (void ) +{ +u64 ign; +int i; + +for (i =1 ; i <64 ; i++) { +/* XGETBV(1) returns "XCR0 & XINUSE" on some CPUs. */ +if (i !=1 ) +report (xgetbv_safe (i, &ign) ==GP_VECTOR , +"XGETBV(%u) - expect #GP" , i); + +report (xsetbv_safe (i,XSTATE_FP ) ==GP_VECTOR , +"XSETBV(%u, FP) - expect #GP" , i); + +report (xsetbv_safe (i,XSTATE_FP |XSTATE_SSE ) ==GP_VECTOR , +"XSETBV(%u, FP|SSE) - expect #GP" , i); + +/* +* RCX[63:32] are ignored by XGETBV and XSETBV, i.e. testing +* bits set above 31 will access XCR0. +*/ +if (i >31 ) +continue ; + +report (xgetbv_safe (BIT (i), &ign) ==GP_VECTOR , +"XGETBV(0x%lx) - expect #GP" ,BIT (i)); + +report (xsetbv_safe (BIT (i),XSTATE_FP ) ==GP_VECTOR , +"XSETBV(0x%lx, FP) - expect #GP" ,BIT (i)); + +report (xsetbv_safe (BIT (i),XSTATE_FP |XSTATE_SSE ) ==GP_VECTOR , +"XSETBV(0x%lx, FP|SSE) - expect #GP" ,BIT (i)); + } +} +static void test_xsave (void ) {u64 supported_xcr0, test_bits;
@@ -48,13 +81,7 @@ static void test_xsave(void) "\t\tWrite XCR0 = (FP | YMM) - expect #GP"); } - test_bits =XSTATE_SSE ; -report (xsetbv_safe (XCR_XFEATURE_ILLEGAL_MASK , test_bits) ==GP_VECTOR , -"\t\txsetbv(XCR_XFEATURE_ILLEGAL_MASK, XSTATE_FP) - expect #GP" ); - - test_bits =XSTATE_SSE ; -report (xsetbv_safe (XCR_XFEATURE_ILLEGAL_MASK , test_bits) ==GP_VECTOR , -"\t\txgetbv(XCR_XFEATURE_ILLEGAL_MASK, XSTATE_FP) - expect #GP" ); +test_unsupported_xcrs ();write_cr4 (cr4);
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 7/11 x86: xsave: Define XFEATURE_MASK_<feature> bits in processor.h
In reply to Sean ChristophersonDefine all known XFEATURE_MASK_<feature> bits in processor.h so that the macros are available to all tests, and to match the nomenclature used by the Linux kernel.
No functional change intended.
--- lib/x86/processor.h | 24 ++++++++++++++++++++++++ x86/xsave.c | 33 ++++++++++++++------------------- 2 files changed, 38 insertions(+), 19 deletions(-)
lib/x86/processor.h +24 -0
@@ -182,6 +182,30 @@ static inline u64 get_non_canonical(u64 addr, u64 mask)#define X86_EFLAGS_ALU (X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | \ X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF) +#define XFEATURE_MASK_FP BIT_ULL(0) +#define XFEATURE_MASK_SSE BIT_ULL(1) +#define XFEATURE_MASK_YMM BIT_ULL(2) +#define XFEATURE_MASK_BNDREGS BIT_ULL(3) +#define XFEATURE_MASK_BNDCSR BIT_ULL(4) +#define XFEATURE_MASK_OPMASK BIT_ULL(5) +#define XFEATURE_MASK_ZMM_Hi256 BIT_ULL(6) +#define XFEATURE_MASK_Hi16_ZMM BIT_ULL(7) +#define XFEATURE_MASK_PT BIT_ULL(8) +#define XFEATURE_MASK_PKRU BIT_ULL(9) +#define XFEATURE_MASK_PASID BIT_ULL(10) +#define XFEATURE_MASK_CET_USER BIT_ULL(11) +#define XFEATURE_MASK_CET_KERNEL BIT_ULL(12) +#define XFEATURE_MASK_LBR BIT_ULL(15) +#define XFEATURE_MASK_XTILE_CFG BIT_ULL(17) +#define XFEATURE_MASK_XTILE_DATA BIT_ULL(18) + +#define XFEATURE_MASK_FP_SSE (XFEATURE_MASK_FP | XFEATURE_MASK_SSE) + +#define XFEATURE_MASK_AVX512 (XFEATURE_MASK_OPMASK | \ + XFEATURE_MASK_ZMM_Hi256 | \ + XFEATURE_MASK_Hi16_ZMM) +#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILE_DATA | \ + XFEATURE_MASK_XTILE_CFG)/* * CPU features
x86/xsave.c +14 -19
@@ -2,10 +2,6 @@#include "desc.h" #include "processor.h" -#define XSTATE_FP 0x1 -#define XSTATE_SSE 0x2 -#define XSTATE_YMM 0x4 -static void test_unsupported_xcrs (void ) {u64 ign;
@@ -17,10 +13,10 @@ static void test_unsupported_xcrs(void)report (xgetbv_safe (i, &ign) ==GP_VECTOR ,"XGETBV(%u) - expect #GP" , i); -report (xsetbv_safe (i,XSTATE_FP ) ==GP_VECTOR , +report (xsetbv_safe (i,XFEATURE_MASK_FP ) ==GP_VECTOR ,"XSETBV(%u, FP) - expect #GP" , i); -report (xsetbv_safe (i,XSTATE_FP |XSTATE_SSE ) ==GP_VECTOR , +report (xsetbv_safe (i,XFEATURE_MASK_FP_SSE ) ==GP_VECTOR ,"XSETBV(%u, FP|SSE) - expect #GP" , i);/*
@@ -33,17 +29,17 @@ static void test_unsupported_xcrs(void)report (xgetbv_safe (BIT (i), &ign) ==GP_VECTOR ,"XGETBV(0x%lx) - expect #GP" ,BIT (i)); -report (xsetbv_safe (BIT (i),XSTATE_FP ) ==GP_VECTOR , +report (xsetbv_safe (BIT (i),XFEATURE_MASK_FP ) ==GP_VECTOR ,"XSETBV(0x%lx, FP) - expect #GP" ,BIT (i)); -report (xsetbv_safe (BIT (i),XSTATE_FP |XSTATE_SSE ) ==GP_VECTOR , +report (xsetbv_safe (BIT (i),XFEATURE_MASK_FP_SSE ) ==GP_VECTOR ,"XSETBV(0x%lx, FP|SSE) - expect #GP" ,BIT (i)); } }static void test_xsave (void ) { -u64 supported_xcr0, test_bits; +u64 supported_xcr0;unsigned long cr4;printf ("Legal instruction testing:\n" );
@@ -51,9 +47,8 @@ static void test_xsave(void) supported_xcr0 =this_cpu_supported_xcr0 ();printf ("Supported XCR0 bits: %#lx\n" , supported_xcr0); - test_bits =XSTATE_FP |XSTATE_SSE ; -report ((supported_xcr0 & test_bits) == test_bits, -"Check minimal XSAVE required bits" ); +report ((supported_xcr0 &XFEATURE_MASK_FP_SSE ) ==XFEATURE_MASK_FP_SSE , +"FP and SSE should always be supported in XCR0" ); cr4 =read_cr4 ();write_cr4 (cr4 |X86_CR4_OSXSAVE );
@@ -62,22 +57,22 @@ static void test_xsave(void) "Check CPUID.1.ECX.OSXSAVE - expect 1");printf ("\tLegal tests\n" ); -write_xcr0 (XSTATE_FP ); -write_xcr0 (XSTATE_FP |XSTATE_SSE ); +write_xcr0 (XFEATURE_MASK_FP ); +write_xcr0 (XFEATURE_MASK_FP_SSE ); (void )read_xcr0 ();printf ("\tIllegal tests\n" );report (write_xcr0_safe (0 ) ==GP_VECTOR ,"\t\tWrite XCR0 = 0 - expect #GP" ); -report (write_xcr0_safe (XSTATE_SSE ) ==GP_VECTOR , +report (write_xcr0_safe (XFEATURE_MASK_SSE ) ==GP_VECTOR ,"\t\tWrite XCR0 = SSE - expect #GP" ); -if (supported_xcr0 &XSTATE_YMM ) { -report (write_xcr0_safe (XSTATE_YMM ) ==GP_VECTOR , +if (supported_xcr0 &XFEATURE_MASK_YMM ) { +report (write_xcr0_safe (XFEATURE_MASK_YMM ) ==GP_VECTOR ,"\t\tWrite XCR0 = YMM - expect #GP" ); -report (write_xcr0_safe (XSTATE_FP |XSTATE_YMM ) ==GP_VECTOR , +report (write_xcr0_safe (XFEATURE_MASK_FP |XFEATURE_MASK_YMM ) ==GP_VECTOR ,"\t\tWrite XCR0 = (FP | YMM) - expect #GP" ); }
@@ -103,7 +98,7 @@ static void test_no_xsave(void)report (read_xcr0_safe (&xcr0) ==UD_VECTOR ,"Read XCR0 without OSXSAVE enabled - expect #UD" ); -report (write_xcr0_safe (XSTATE_FP |XSTATE_SSE ) ==UD_VECTOR , +report (write_xcr0_safe (XFEATURE_MASK_FP_SSE ) ==UD_VECTOR ,"Write XCR0=(FP|SSE) without XSAVE support - expect #UD" );if (cr4 &X86_CR4_OSXSAVE )
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 8/11 x86: xsave: Add testcase for emulation of AVX instructions
In reply to Sean ChristophersonFrom: Paolo Bonzini <[email protected]>
Extend the XSAVE test to validate KVM's recently added emulation of AVX VMOVDQA instructions. Test mem=>reg, reg=>reg, and reg=>mem moves, and when forced emulation is supported, verify that mixing forced emulation and native accesses works as expected, e.g. that KVM writes internal state into hardware.
--- x86/xsave.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
x86/xsave.c +66 -0
@@ -2,6 +2,69 @@#include "desc.h" #include "processor.h" +char __attribute__((aligned (32 ))) v32_1[32 ]; +char __attribute__((aligned (32 ))) v32_2[32 ]; +char __attribute__((aligned (32 ))) v32_3[32 ]; + +static void initialize_avx_buffers (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (v32_1); i++) + v32_1[i] = (char )rdtsc (); + +memset (v32_2,0 ,sizeof (v32_2)); +memset (v32_3,0 ,sizeof (v32_3)); +} + +#define __TEST_VMOVDQA (reg1, reg2,FEP ) \ +do { \ + asm volatile(FEP "vmovdqa v32_1(%%rip), %%" #reg1 "\n" \ + FEP "vmovdqa %%" #reg1 ", %%" #reg2 "\n" \ + FEP "vmovdqa %%" #reg2 ", v32_2(%%rip)\n" \ + "vmovdqa %%" #reg2 ", v32_3(%%rip)\n" \ + ::: "memory", #reg1, #reg2); \ + \ + report(!memcmp(v32_1, v32_2, sizeof(v32_1)), \ + "%s VMOVDQA using " #reg1 " and " #reg2, \ + strlen(FEP) ? "Emulated" : "Native"); \ + report(!memcmp(v32_1, v32_3, sizeof(v32_1)), \ + "%s VMOVDQA using " #reg1 " and " #reg2, \ + strlen(FEP) ? "Emulated+Native" : "Native"); \ +} while (0) + +#define TEST_VMOVDQA (r1, r2) \ +do { \ + initialize_avx_buffers(); \ + \ + __TEST_VMOVDQA(ymm##r1, ymm##r2, ""); \ + \ + if (is_fep_available) \ + __TEST_VMOVDQA(ymm##r1, ymm##r2, KVM_FEP); \ +} while (0) + +static __attribute__((target ("avx" )))void test_avx_vmovdqa (void ) +{ +write_xcr0 (XFEATURE_MASK_FP_SSE |XFEATURE_MASK_YMM ); + +TEST_VMOVDQA (0 ,15 ); +TEST_VMOVDQA (1 ,14 ); +TEST_VMOVDQA (2 ,13 ); +TEST_VMOVDQA (3 ,12 ); +TEST_VMOVDQA (4 ,11 ); +TEST_VMOVDQA (5 ,10 ); +TEST_VMOVDQA (6 ,9 ); +TEST_VMOVDQA (7 ,8 ); +TEST_VMOVDQA (8 ,7 ); +TEST_VMOVDQA (9 ,6 ); +TEST_VMOVDQA (10 ,5 ); +TEST_VMOVDQA (11 ,4 ); +TEST_VMOVDQA (12 ,3 ); +TEST_VMOVDQA (13 ,2 ); +TEST_VMOVDQA (14 ,2 ); +TEST_VMOVDQA (15 ,1 ); +} +static void test_unsupported_xcrs (void ) {u64 ign;
@@ -61,6 +124,9 @@ static void test_xsave(void)write_xcr0 (XFEATURE_MASK_FP_SSE ); (void )read_xcr0 (); +if (supported_xcr0 &XFEATURE_MASK_YMM ) +test_avx_vmovdqa (); +printf ("\tIllegal tests\n" );report (write_xcr0_safe (0 ) ==GP_VECTOR ,"\t\tWrite XCR0 = 0 - expect #GP" );
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 9/11 x86: xsave: Always verify XCR0 is actually written
In reply to Sean ChristophersonWhen testing writes to XCR0, always verify that XCR0 reads back what was written.
--- x86/xsave.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
x86/xsave.c +11 -4
@@ -43,9 +43,17 @@ do { \ __TEST_VMOVDQA(ymm##r1, ymm##r2, KVM_FEP); \ } while (0) +static void test_write_xcr0 (u64 val) +{ +write_xcr0 (val); + +report (read_xcr0 () == val, +"Wanted XCR0 == 0x%lx, got XCR0 == 0x%lx" , val,read_xcr0 ()); +} +static __attribute__((target ("avx" )))void test_avx_vmovdqa (void ) { -write_xcr0 (XFEATURE_MASK_FP_SSE |XFEATURE_MASK_YMM ); +test_write_xcr0 (XFEATURE_MASK_FP_SSE |XFEATURE_MASK_YMM );TEST_VMOVDQA (0 ,15 );TEST_VMOVDQA (1 ,14 );
@@ -120,9 +128,8 @@ static void test_xsave(void) "Check CPUID.1.ECX.OSXSAVE - expect 1");printf ("\tLegal tests\n" ); -write_xcr0 (XFEATURE_MASK_FP ); -write_xcr0 (XFEATURE_MASK_FP_SSE ); - (void )read_xcr0 (); +test_write_xcr0 (XFEATURE_MASK_FP ); +test_write_xcr0 (XFEATURE_MASK_FP_SSE );if (supported_xcr0 &XFEATURE_MASK_YMM )test_avx_vmovdqa ();
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 10/11 x86: xsave: Drop remaining indentation quirks and printf markers
In reply to Sean ChristophersonDrop the remaining tab indentationa and "legal" vs. "illegal" markers in the XSAVE test. Now that there are more than a handful of testscases, the annotations just get in the way.
--- x86/xsave.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
x86/xsave.c +4 -8
@@ -113,8 +113,6 @@ static void test_xsave(void)u64 supported_xcr0;unsigned long cr4; -printf ("Legal instruction testing:\n" ); - supported_xcr0 =this_cpu_supported_xcr0 ();printf ("Supported XCR0 bits: %#lx\n" , supported_xcr0);
@@ -127,26 +125,24 @@ static void test_xsave(void)report (this_cpu_has (X86_FEATURE_OSXSAVE ),"Check CPUID.1.ECX.OSXSAVE - expect 1" ); -printf ("\tLegal tests\n" );test_write_xcr0 (XFEATURE_MASK_FP );test_write_xcr0 (XFEATURE_MASK_FP_SSE );if (supported_xcr0 &XFEATURE_MASK_YMM )test_avx_vmovdqa (); -printf ("\tIllegal tests\n" );report (write_xcr0_safe (0 ) ==GP_VECTOR , -"\t\tWrite XCR0 = 0 - expect #GP" ); +"Write XCR0 = 0 - expect #GP" );report (write_xcr0_safe (XFEATURE_MASK_SSE ) ==GP_VECTOR , -"\t\tWrite XCR0 = SSE - expect #GP" ); +"Write XCR0 = SSE - expect #GP" );if (supported_xcr0 &XFEATURE_MASK_YMM ) {report (write_xcr0_safe (XFEATURE_MASK_YMM ) ==GP_VECTOR , -"\t\tWrite XCR0 = YMM - expect #GP" ); +"Write XCR0 = YMM - expect #GP" );report (write_xcr0_safe (XFEATURE_MASK_FP |XFEATURE_MASK_YMM ) ==GP_VECTOR , -"\t\tWrite XCR0 = (FP | YMM) - expect #GP" ); +"Write XCR0 = (FP | YMM) - expect #GP" ); }test_unsupported_xcrs ();
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
reviewerPATCH v2 11/11 x86: xsave: Verify XSETBV and XGETBV ignore RCX[63:32]
In reply to Sean ChristophersonWhen testing XCR0 accesses, verify that RCX[63:32] is ignored (X{G,S}ETBV have the same register semantics as {RD,WR}MSR);
--- x86/xsave.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-)
x86/xsave.c +22 -2
@@ -45,10 +45,30 @@ do { \static void test_write_xcr0 (u64 val) { +u64 xcr0_alias =rdtsc () <<32 , cur; +int vector; + +/* +* Verify that RCX[63:32] are ignored by XSETBV and XGETBV. Use the +* safe variants as XCR0 will be written "normally" below. +*/ + vector =xsetbv_safe (xcr0_alias, val); +report (!vector,"XGETBV(0x%lx) (i.e. XCR0) should succeed (exception = %s)" , + xcr0_alias, vector ?exception_mnemonic (vector) :"none" ); + + vector =xgetbv_safe (xcr0_alias, &cur); +report (!vector,"XGETBV(0x%lx) (i.e. XCR0) should succeed (exception = %s)" , + xcr0_alias, vector ?exception_mnemonic (vector) :"none" ); +report (cur == val, +"Wanted aliased XCR0 == 0x%lx, got XCR0 == 0x%lx" , val, cur); + + cur =read_xcr0 (); +report (cur == val,"Wanted XCR0 == 0x%lx, got XCR0 == 0x%lx" , val, cur); +write_xcr0 (val); -report (read_xcr0 () == val, -"Wanted XCR0 == 0x%lx, got XCR0 == 0x%lx" , val,read_xcr0 ()); + cur =read_xcr0 (); +report (cur == val,"Wanted XCR0 == 0x%lx, got XCR0 == 0x%lx" , val, cur); }static __attribute__((target ("avx" )))void test_avx_vmovdqa (void )
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Quoted 20 lines · Sean Christopherson
A "slightly" beefed up version of Paolo's patch to validate KVM's recently added AVX VMOVDQA emulation.
v1: https://lore.kernel.org/all/[email protected]
Paolo Bonzini (1):
x86: xsave: Add testcase for emulation of AVX instructionsSean Christopherson (10):
x86: xsave: Replace spaces with tabs
x86: xsave: Drop unnecessary and confusinguint64_toverrides
x64: xsave: Use non-safewrite_cr4()when toggling OSXSAVE
x86: xsave: Add and use dedicated XCR0 read/write helpers
x86: xsave: Dedup XGETBV and XSETBV #UD tests
x86: xsave: Programmatically test more unsupported XCR accesses
x86: xsave: Define XFEATURE_MASK_<feature> bits inprocessor.h
x86: xsave: Always verify XCR0 is actually written
x86: xsave: Drop remaining indentation quirks and printf markers
x86: xsave: Verify XSETBV and XGETBV ignore RCX[63:32]
Quoted 6 lines
lib/x86/processor.h | 62 ++++++++++ x86/xsave.c | 270 +++++++++++++++++++++++++++++--------------- 2 files changed, 239 insertions(+), 93 deletions(-)base-commit:
f561b31d3dee01f8be58978be23bb0903543153d
Applied, thanks.
Paolo