Re: [PATCH 7/7] KVM: VMX: replace vmx_spec_ctrl_restore_host with RESTORE_HOST_SPEC_CTRL_BODY

From: Uros Bizjak

Date: Thu Apr 09 2026 - 13:36:54 EST




On 4/8/26 20:22, Paolo Bonzini wrote:
Reuse the same assembly as SVM, just with alternatives instead
of cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS).

Due to the dearth of registers in 32-bit x86, save/restore %eax
across the restoring sequence, keeping it simple; the main alternative
would be to leave vmx and flags on the stack, avoiding the need to read
flags from %ebx. It's not really measurable difference.

You don't need to save/restore %eax. Considering that
RESTORE_HOST_SPEC_CTRL_BODY doesn't touch %ebx, and that

testl $KVM_ENTER_SAVE_SPEC_CTRL, \enter_flags

is placed at the beginning of the macro, you can put @flags into %eax:

+ pop %_ASM_AX /* @flags */
+ pop %_ASM_DI /* @vmx */
+
+ /* Clobbers RAX, RCX, RDX, RSI. */
+ ALTERNATIVE "jmp .Lspec_ctrl_host_done", "", X86_FEATURE_MSR_SPEC_CTRL
+ RESTORE_HOST_SPEC_CTRL_BODY VMX_spec_ctrl(%_ASM_DI), %eax,
.Lspec_ctrl_host_done
+.Lspec_ctrl_host_done:
+
+ CLEAR_BRANCH_HISTORY_VMEXIT

The above macro will clobber %eax via clear_bhb_loop(), so you should leave:

/* Put return value in AX */
mov %_ASM_BX, %_ASM_AX

after it, like in the attached (partial) patch.

BR,
Uros.diff --git a/arch/x86/kvm/vmx/vmenter.S b/arch/x86/kvm/vmx/vmenter.S
index 4426d34811fc..b707ebf6e7a5 100644
--- a/arch/x86/kvm/vmx/vmenter.S
+++ b/arch/x86/kvm/vmx/vmenter.S
@@ -286,10 +286,13 @@ SYM_INNER_LABEL_ALIGN(vmx_vmexit, SYM_L_GLOBAL)
FILL_RETURN_BUFFER %_ASM_CX, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_VMEXIT,\
X86_FEATURE_RSB_VMEXIT_LITE

- pop %_ASM_ARG2 /* @flags */
- pop %_ASM_ARG1 /* @vmx */
+ pop %_ASM_AX /* @flags */
+ pop %_ASM_DI /* @vmx */

- call vmx_spec_ctrl_restore_host
+ /* Clobbers RAX, RCX, RDX, RSI. */
+ ALTERNATIVE "jmp .Lspec_ctrl_host_done", "", X86_FEATURE_MSR_SPEC_CTRL
+ RESTORE_HOST_SPEC_CTRL_BODY VMX_spec_ctrl(%_ASM_DI), %eax, .Lspec_ctrl_host_done
+.Lspec_ctrl_host_done:

CLEAR_BRANCH_HISTORY_VMEXIT