Skip to contentSkip to conversation

xsave: add testcase for emulation of AVX instructions

PATCH In review Paolo Bonzini 2 participants 2 messages Nov 14, 2025 – Nov 21, 2025 KVM

lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 xsave: add testcase for emulation of AVX instructions

Companion patch to the emulator changes in KVM. Funnily enough, no valid combination involving AVX was tried.

---
 x86/xsave.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
x86/xsave.c +44 -1
index cc8e3a0a..e6d15938 100644
@@ -15,6 +15,34 @@
 #define XSTATE_SSE      0x2
 #define XSTATE_YMM      0x4
 
+char __attribute__((aligned(32))) v32_1[32] = {
+    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+    128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
+};
+char __attribute__((aligned(32))) v32_2[32] = { 0 };
+
+static __attribute__((target("avx"))) void
+test_avx_fep(void)
+{
+	asm volatile("vzeroall\n"
+	    KVM_FEP "vmovdqa v32_1, %%ymm0\n"
+	    KVM_FEP "vmovdqa %%ymm0, v32_2\n" : : :
+	    "memory",
+	    "%ymm0", "%ymm1", "%ymm2", "%ymm3", "%ymm4", "%ymm5", "%ymm6", "%ymm7",
+	    "%ymm8", "%ymm9", "%ymm10", "%ymm11", "%ymm12", "%ymm13", "%ymm14", "%ymm15");
+}
+
+static __attribute__((target("avx"))) void
+test_avx(void)
+{
+	asm volatile("vzeroall\n"
+	    "vmovdqa v32_1, %%ymm0\n"
+	    "vmovdqa %%ymm0, v32_2\n" : : :
+	    "memory",
+	    "%ymm0", "%ymm1", "%ymm2", "%ymm3", "%ymm4", "%ymm5", "%ymm6", "%ymm7",
+	    "%ymm8", "%ymm9", "%ymm10", "%ymm11", "%ymm12", "%ymm13", "%ymm14", "%ymm15");
+}
+
 static void test_xsave(void)
 {
     unsigned long cr4;
@@ -45,7 +73,22 @@ static void test_xsave(void)
     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)");
+           "\t\txgetbv(XCR_XFEATURE_ENABLED_MASK)");
+
+    if (supported_xcr0 & XSTATE_YMM) {
+        test_bits = XSTATE_FP | XSTATE_SSE | XSTATE_YMM;
+        report(xsetbv_safe(XCR_XFEATURE_ENABLED_MASK, test_bits) == 0,
+               "\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FS | XSTATE_SSE | XSTATE_YMM)");
+        xgetbv_safe(XCR_XFEATURE_ENABLED_MASK, &xcr0);
+        if (xcr0 == test_bits) {
+            if (is_fep_available)
+                test_avx_fep();
+            else
+                test_avx();
+            report(memcmp(v32_1, v32_2, 32) == 0,
+                   "vmovdqa emulation");
+        }
+    }
 
     printf("\tIllegal tests\n");
     test_bits = 0;
Signature · 1 line
2.43.5
#1 lore raw
Quoted 14 lines · Paolo Bonzini

Companion patch to the emulator changes in KVM. Funnily enough, no valid combination involving AVX was tried.

---
 x86/xsave.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
x86/xsave.c +0 -0
index cc8e3a0a..e6d15938 100644
@@ -15,6 +15,34 @@
 #define XSTATE_SSE      0x2

x86/xsave.c line 25

Quoted 10 lines
x86/xsave.c +8 -0
 #define XSTATE_YMM      0x4
 
+char __attribute__((aligned(32))) v32_1[32] = {
+    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+    128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
+};
+char __attribute__((aligned(32))) v32_2[32] = { 0 };
+
+static __attribute__((target("avx"))) void
+test_avx_fep(void)

Bizarre wrap.

x86/xsave.c line 27

Quoted 2 lines
x86/xsave.c +2 -0
+{
+	asm volatile("vzeroall\n"

Why zero all registers? I can't think of any reason why VZEROALL is needed, and clobbering everything for no apparent reason is weird/confusing.

x86/xsave.c line 28

Quoted 1 line
x86/xsave.c +1 -0
+	    KVM_FEP "vmovdqa v32_1, %%ymm0\n"

These need to use RIP-relative addressing, otherwise EFI builds fail.

x86/xsave.c line 40

Quoted 12 lines
x86/xsave.c +12 -0
+	    KVM_FEP "vmovdqa %%ymm0, v32_2\n" : : :
+	    "memory",
+	    "%ymm0", "%ymm1", "%ymm2", "%ymm3", "%ymm4", "%ymm5", "%ymm6", "%ymm7",
+	    "%ymm8", "%ymm9", "%ymm10", "%ymm11", "%ymm12", "%ymm13", "%ymm14", "%ymm15");
+}
+
+static __attribute__((target("avx"))) void
+test_avx(void)
+{
+	asm volatile("vzeroall\n"
+	    "vmovdqa v32_1, %%ymm0\n"
+	    "vmovdqa %%ymm0, v32_2\n" : : :

We should also test reg=>reg, registers other than ymm0, and that emulated accesses are correctly propagated to hardware. E.g. with macro shenanigans, it's not too hard:

	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);			\
Quoted 9 lines
x86/xsave.c +5 -0
+	    "memory",
+	    "%ymm0", "%ymm1", "%ymm2", "%ymm3", "%ymm4", "%ymm5", "%ymm6", "%ymm7",
+	    "%ymm8", "%ymm9", "%ymm10", "%ymm11", "%ymm12", "%ymm13", "%ymm14", "%ymm15");
+}
+
 static void test_xsave(void)
 {
     unsigned long cr4;

x86/xsave.c static void test_xsave(void) · line 76

Quoted 5 lines
x86/xsave.c +1 -1
@@ -45,7 +73,22 @@ static void test_xsave(void)
     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)");
+           "\t\txgetbv(XCR_XFEATURE_ENABLED_MASK)");

Ugh, this test is ancient and crusty. We really should have dedicated helpers for accessing XCR0, it uses spaces instead of tabs, there's unnecessary code duplication, and a handful of other minor issues.

I'll send a series with a pile of cleanups, and better validation of VMOVDQA emulation.