mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosry@kernel.org>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yosry Ahmed <yosryahmed@google.com>,
	Yosry Ahmed <yosry@kernel.org>
Subject: [PATCH 3/8] KVM: selftests: Reuse GPR switching logic for nVMX
Date: Mon, 18 May 2026 20:25:09 +0000	[thread overview]
Message-ID: <20260518202514.2037078-4-yosry@kernel.org> (raw)
In-Reply-To: <20260518202514.2037078-1-yosry@kernel.org>

From: Yosry Ahmed <yosryahmed@google.com>

Reuse the GPR switching logic for nVMX by adding a wrapper that also
switches RAX, replacing the push/pop of a subset of the registers.

Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 .../selftests/kvm/include/x86/processor.h     |  5 ++
 tools/testing/selftests/kvm/include/x86/vmx.h | 46 +++++--------------
 2 files changed, 17 insertions(+), 34 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index 8e4eab84b91bc..ca2ec92490f7c 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -402,6 +402,7 @@ extern struct gpr64_regs guest_regs;
 #define DEFINE_ASM_GPR64_OFFSET(reg) \
 	asm(".equ GPR64_OFF_" #reg ", %c0" : : "i"(offsetof(struct gpr64_regs, reg)))
 
+DEFINE_ASM_GPR64_OFFSET(rax);
 DEFINE_ASM_GPR64_OFFSET(rbx);
 DEFINE_ASM_GPR64_OFFSET(rcx);
 DEFINE_ASM_GPR64_OFFSET(rdx);
@@ -439,6 +440,10 @@ DEFINE_ASM_GPR64_OFFSET(r15);
 	GUEST_SWITCH_GPR_ASM(r14) \
 	GUEST_SWITCH_GPR_ASM(r15)
 
+#define GUEST_SWITCH_GPRS_ASM \
+	GUEST_SWITCH_GPR_ASM(rax) \
+	GUEST_SWITCH_GPRS_NORAX_ASM
+
 struct desc64 {
 	u16 limit0;
 	u16 base0;
diff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h
index 90fffaf915958..a7f6f0c9b6b9d 100644
--- a/tools/testing/selftests/kvm/include/x86/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86/vmx.h
@@ -363,9 +363,6 @@ static inline u64 vmptrstz(void)
 	return value;
 }
 
-/*
- * No guest state (e.g. GPRs) is established by this vmlaunch.
- */
 static inline int vmlaunch(void)
 {
 	int ret;
@@ -373,34 +370,23 @@ static inline int vmlaunch(void)
 	if (enable_evmcs)
 		return evmcs_vmlaunch();
 
-	__asm__ __volatile__("push %%rbp;"
-			     "push %%rcx;"
-			     "push %%rdx;"
-			     "push %%rsi;"
-			     "push %%rdi;"
-			     "push $0;"
+	__asm__ __volatile__("push $0;"
 			     "vmwrite %%rsp, %[host_rsp];"
 			     "lea 1f(%%rip), %%rax;"
 			     "vmwrite %%rax, %[host_rip];"
+			     GUEST_SWITCH_GPRS_ASM
 			     "vmlaunch;"
 			     "incq (%%rsp);"
-			     "1: pop %%rax;"
-			     "pop %%rdi;"
-			     "pop %%rsi;"
-			     "pop %%rdx;"
-			     "pop %%rcx;"
-			     "pop %%rbp;"
+			     "1: ;"
+			     GUEST_SWITCH_GPRS_ASM
+			     "pop %%rax;"
 			     : [ret]"=&a"(ret)
 			     : [host_rsp]"r"((u64)HOST_RSP),
 			       [host_rip]"r"((u64)HOST_RIP)
-			     : "memory", "cc", "rbx", "r8", "r9", "r10",
-			       "r11", "r12", "r13", "r14", "r15");
+			     : "memory", "cc");
 	return ret;
 }
 
-/*
- * No guest state (e.g. GPRs) is established by this vmresume.
- */
 static inline int vmresume(void)
 {
 	int ret;
@@ -408,28 +394,20 @@ static inline int vmresume(void)
 	if (enable_evmcs)
 		return evmcs_vmresume();
 
-	__asm__ __volatile__("push %%rbp;"
-			     "push %%rcx;"
-			     "push %%rdx;"
-			     "push %%rsi;"
-			     "push %%rdi;"
-			     "push $0;"
+	__asm__ __volatile__("push $0;"
 			     "vmwrite %%rsp, %[host_rsp];"
 			     "lea 1f(%%rip), %%rax;"
 			     "vmwrite %%rax, %[host_rip];"
+			     GUEST_SWITCH_GPRS_ASM
 			     "vmresume;"
 			     "incq (%%rsp);"
-			     "1: pop %%rax;"
-			     "pop %%rdi;"
-			     "pop %%rsi;"
-			     "pop %%rdx;"
-			     "pop %%rcx;"
-			     "pop %%rbp;"
+			     "1: ;"
+			     GUEST_SWITCH_GPRS_ASM
+			     "pop %%rax;"
 			     : [ret]"=&a"(ret)
 			     : [host_rsp]"r"((u64)HOST_RSP),
 			       [host_rip]"r"((u64)HOST_RIP)
-			     : "memory", "cc", "rbx", "r8", "r9", "r10",
-			       "r11", "r12", "r13", "r14", "r15");
+			     : "memory", "cc");
 	return ret;
 }
 
-- 
2.54.0.563.g4f69b47b94-goog


  parent reply	other threads:[~2026-05-18 20:25 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-18 20:25 [PATCH 0/8] KVM: selftests: Stress save+restore and #PF (ft. nested) Yosry Ahmed
2026-05-18 20:25 ` [PATCH 1/8] KVM: selftests: Fix offsets in GPR switching for nSVM Yosry Ahmed
2026-05-18 20:25 ` [PATCH 2/8] KVM: selftests: Move GPR load/save definitions outside of nSVM code Yosry Ahmed
2026-05-18 20:25 ` Yosry Ahmed [this message]
2026-05-18 20:25 ` [PATCH 4/8] KVM: selftests: Drop HORRIFIC_L2_UCALL_CLOBBER_HACK Yosry Ahmed
2026-05-18 20:25 ` [PATCH 5/8] KVM: selftests: Add basic stress test for save+restore and #PF handling Yosry Ahmed
2026-05-28 22:12   ` Yosry Ahmed
2026-05-18 20:25 ` [PATCH 6/8] KVM: selftests: Trigger save+restore randomly in the #PF stress test Yosry Ahmed
2026-05-18 20:25 ` [PATCH 7/8] KVM: selftests: Support running stress save+restore and #PF test in L2 Yosry Ahmed
2026-05-18 20:25 ` [PATCH 8/8] KVM: selftests: Trigger L2->L1 exits stress save+restore and #PF test Yosry Ahmed
2026-05-18 20:40 ` [PATCH 0/8] KVM: selftests: Stress save+restore and #PF (ft. nested) Yosry Ahmed
2026-05-28 19:26 ` Yosry Ahmed

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=20260518202514.2037078-4-yosry@kernel.org \
    --to=yosry@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=yosryahmed@google.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

all inboxes | Powered by JetHome®