Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, Sean Christopherson <seanjc@google.com>
Subject: [kvm-unit-tests PATCH] x86/xsave: Extend VMOVDQA testing to also cover VMOVNTDQA
Date: Thu, 14 May 2026 13:40:20 -0700	[thread overview]
Message-ID: <20260514204020.1614792-1-seanjc@google.com> (raw)

Extend the XSAVE test's VMOVDQA coverage to also validate KVM's recently
added (and initially broken) emulation of VMOVNTDQA instructions.  Despite
the seemingly generic and all-too-similar names, VMOVNTDQ and VMOVNTDQA
are unidirectional instructions with opposite semantics: VMOVNTDQ *always*
stores to memory, whereas VMOVNTDQA *always* loads from memory.

Rework the checking to use a buffer that is never written as part of the
testcase itself for the source of truth.  KVM's bug was that it mixed up
the direction of VMOVNTDQA, i.e. KVM was loading from registers and storing
to memory.  Comparing the src and dst buffers on a:

  src => reg => dst

sequence won't detect mismatches due to such bugs, i.e. if KVM wrongly
does:

  reg => src, reg => dst

ecause by the end, src == dst, but with the wrong values.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 x86/xsave.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/x86/xsave.c b/x86/xsave.c
index 254f9fde..0a40f466 100644
--- a/x86/xsave.c
+++ b/x86/xsave.c
@@ -2,6 +2,7 @@
 #include "desc.h"
 #include "processor.h"
 
+char __attribute__((aligned(32))) v32_0[32];
 char __attribute__((aligned(32))) v32_1[32];
 char __attribute__((aligned(32))) v32_2[32];
 char __attribute__((aligned(32))) v32_3[32];
@@ -11,14 +12,40 @@ static void initialize_avx_buffers(void)
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(v32_1); i++)
-		v32_1[i] = (char)rdtsc();
+		v32_0[i] = (char)rdtsc();
 
+	memcpy(v32_1, v32_0, sizeof(v32_2));
 	memset(v32_2, 0, sizeof(v32_2));
 	memset(v32_3, 0, sizeof(v32_3));
+
+	asm volatile("vzeroall" ::: "memory");
 }
 
+#define __TEST_VMOVNTDQA(reg1, reg2, FEP)				\
+do {									\
+	initialize_avx_buffers();					\
+									\
+	asm volatile(FEP "vmovntdqa v32_1(%%rip), %%" #reg1 "\n"	\
+		     FEP "vmovdqa %%" #reg1 ", %%" #reg2 "\n"		\
+		     FEP "vmovntdq %%" #reg2 ", v32_2(%%rip)\n"		\
+		     "vmovdqa %%" #reg2 ", v32_3(%%rip)\n"		\
+		     ::: "memory", #reg1, #reg2);			\
+									\
+	report(!memcmp(v32_0, v32_1, sizeof(v32_0)),			\
+	       "%s VMOVNTDQ{A} original memory, " #reg1 " and " #reg2,	\
+	       strlen(FEP) ? "Emulated" : "Native");			\
+	report(!memcmp(v32_0, v32_2, sizeof(v32_0)),			\
+	       "%s VMOVNTDQ{A} using " #reg1 " and " #reg2,		\
+	       strlen(FEP) ? "Emulated" : "Native");			\
+	report(!memcmp(v32_0, v32_3, sizeof(v32_0)),			\
+	       "%s VMOVNTDQ{A} using " #reg1 " and " #reg2,		\
+	       strlen(FEP) ? "Emulated+Native" : "Native");		\
+} while (0)
+
 #define __TEST_VMOVDQA(reg1, reg2, FEP)					\
 do {									\
+	initialize_avx_buffers();					\
+									\
 	asm volatile(FEP "vmovdqa v32_1(%%rip), %%" #reg1 "\n"		\
 		     FEP "vmovdqa %%" #reg1 ", %%" #reg2 "\n"		\
 		     FEP "vmovdqa %%" #reg2 ", v32_2(%%rip)\n"		\
@@ -35,12 +62,15 @@ do {									\
 
 #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);		\
+									\
+	__TEST_VMOVNTDQA(ymm##r1, ymm##r2, "");				\
+									\
+	if (is_fep_available)						\
+		__TEST_VMOVNTDQA(ymm##r1, ymm##r2, KVM_FEP);		\
 } while (0)
 
 static void test_write_xcr0(u64 val)

base-commit: 9eb6c57313060d34f7e5b2ac6f90bb5873bbe2ff
-- 
2.54.0.563.g4f69b47b94-goog


                 reply	other threads:[~2026-05-14 20:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260514204020.1614792-1-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox