Re: [PATCH v3 12/13] KVM: selftests: Dedup assembly code for VMLAUNCH and VMRESUME
From: Yosry Ahmed
Date: Thu Aug 27 2026 - 02:48:14 EST
On Wed, Aug 26, 2026 at 4:39 PM Sean Christopherson <seanjc@xxxxxxxxxx> wrote:
>
> Dedup the assembly code from VMLAUNCH vs. VMRESUME, the difference is
> literally only the actual VM-Enter instruction.
You couldn't resist macrofiying this too.
>
> 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) \
s/HELPERS/HELPER?
or maybe better: DEFINE_VMX_VM_ENTRY_HELPER()?
With the rename:
Reviewed-by: Yosry Ahmed <yosry@xxxxxxxxxx>
(Who's f734222792@xxxxxxxxx?)
> +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
>