[PATCH v3 12/13] KVM: selftests: Dedup assembly code for VMLAUNCH and VMRESUME

From: Sean Christopherson

Date: Wed Aug 26 2026 - 19:40:45 EST


Dedup the assembly code from VMLAUNCH vs. VMRESUME, the difference is
literally only the actual VM-Enter instruction.

No functional change intended.

Cc: Yosry Ahmed <yosry@xxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
tools/testing/selftests/kvm/include/x86/vmx.h | 74 +++++++------------
1 file changed, 26 insertions(+), 48 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h
index 3def8df902a2..fa01341e22bc 100644
--- a/tools/testing/selftests/kvm/include/x86/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86/vmx.h
@@ -359,55 +359,33 @@ static inline u64 vmptrst(void)
return value;
}

-static inline int vmlaunch(void)
-{
- int ret;
-
- if (enable_evmcs)
- return evmcs_vmlaunch();
-
- __asm__ __volatile__("push $0;"
- "vmwrite %%rsp, %[host_rsp];"
- "lea 1f(%%rip), %%rax;"
- "vmwrite %%rax, %[host_rip];"
- VMX_SWITCH_GPRS_ASM
- "vmlaunch;"
- "incq (%%rsp);"
- "1: ;"
- VMX_SWITCH_GPRS_ASM
- "pop %%rax;"
- : [ret]"=&a"(ret)
- : [host_rsp]"r"((u64)HOST_RSP),
- [host_rip]"r"((u64)HOST_RIP),
- GUEST_REGS_OFFSETS
- : "memory", "cc");
- return ret;
-}
-
-static inline int vmresume(void)
-{
- int ret;
-
- if (enable_evmcs)
- return evmcs_vmresume();
-
- __asm__ __volatile__("push $0;"
- "vmwrite %%rsp, %[host_rsp];"
- "lea 1f(%%rip), %%rax;"
- "vmwrite %%rax, %[host_rip];"
- VMX_SWITCH_GPRS_ASM
- "vmresume;"
- "incq (%%rsp);"
- "1: ;"
- VMX_SWITCH_GPRS_ASM
- "pop %%rax;"
- : [ret]"=&a"(ret)
- : [host_rsp]"r"((u64)HOST_RSP),
- [host_rip]"r"((u64)HOST_RIP),
- GUEST_REGS_OFFSETS
- : "memory", "cc");
- return ret;
+#define BUILD_VMX_VM_ENTRY_HELPERS(insn) \
+static inline int insn(void) \
+{ \
+ int ret; \
+ \
+ if (enable_evmcs) \
+ return evmcs_##insn(); \
+ \
+ __asm__ __volatile__("push $0;" \
+ "vmwrite %%rsp, %[host_rsp];" \
+ "lea 1f(%%rip), %%rax;" \
+ "vmwrite %%rax, %[host_rip];" \
+ VMX_SWITCH_GPRS_ASM \
+ __stringify(insn)";" \
+ "incq (%%rsp);" \
+ "1: ;" \
+ VMX_SWITCH_GPRS_ASM \
+ "pop %%rax;" \
+ : [ret]"=&a"(ret) \
+ : [host_rsp]"r"((u64)HOST_RSP), \
+ [host_rip]"r"((u64)HOST_RIP), \
+ GUEST_REGS_OFFSETS \
+ : "memory", "cc"); \
+ return ret; \
}
+BUILD_VMX_VM_ENTRY_HELPERS(vmlaunch)
+BUILD_VMX_VM_ENTRY_HELPERS(vmresume)

static inline void vmcall(void)
{
--
2.55.0.887.g758fc8c411-goog