Re: [PATCH v4 4/8] KVM: VMX: Handle MMIO Stale Data in VM-Enter assembly via ALTERNATIVES_2

From: Pawan Gupta

Date: Wed Nov 12 2025 - 18:01:44 EST


On Wed, Nov 12, 2025 at 12:30:36PM -0800, Sean Christopherson wrote:
> VMX "needs" to abuse RFLAGS no matter what, because RFLAGS is the only register
> that's available at the time of VMLAUNCH/VMRESUME. On Intel, only RSP and
> RFLAGS are context switched via the VMCS, all other GPRs need to be context
> switch by software. Which is why I didn't balk at Pawan's idea to use RFLAGS.ZF
> to track whether or not a VERW for MMIO is needed.
>
> Hmm, actually, @flags is already on the stack because it's needed at VM-Exit.
> Using EBX was a holdover from the conversion from inline asm to "proper" asm,
> e.g. from commit 77df549559db ("KVM: VMX: Pass @launched to the vCPU-run asm via
> standard ABI regs").
>
> Oooh, and if we stop using bt+RFLAGS.CF, then we drop the annoying SHIFT definitions
> in arch/x86/kvm/vmx/run_flags.h.
>
> Very lightly tested at this point, but I think this can all be simplified to
>
> /*
> * Note, ALTERNATIVE_2 works in reverse order. If CLEAR_CPU_BUF_VM is
> * enabled, do VERW unconditionally. If CPU_BUF_VM_MMIO is enabled,
> * check @flags to see if the vCPU has access to host MMIO, and do VERW
> * if so. Else, do nothing (no mitigations needed/enabled).
> */
> ALTERNATIVE_2 "", \
> __stringify(testl $VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO, WORD_SIZE(%_ASM_SP); \

WORD_SIZE(%_ASM_SP) is still a bit fragile, but this is definitely an
improvement.

> jz .Lskip_clear_cpu_buffers; \
> VERW; \
> .Lskip_clear_cpu_buffers:), \
> X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO, \
> __stringify(VERW), X86_FEATURE_CLEAR_CPU_BUF_VM
>
> /* Check if vmlaunch or vmresume is needed */
> testl $VMX_RUN_VMRESUME, WORD_SIZE(%_ASM_SP)
> jz .Lvmlaunch