Re: [PATCH v2 12/25] KVM: VMX: Handle FRED event data

From: Sean Christopherson
Date: Thu Jun 13 2024 - 12:57:53 EST


On Wed, Feb 07, 2024, Xin Li wrote:
> @@ -7382,6 +7419,24 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
>
> vmx_disable_fb_clear(vmx);
>
> + /*
> + * %cr2 needs to be saved after a VM exit and restored before a VM
> + * entry in case a VM exit happens immediately after delivery of a
> + * guest #PF but before guest reads %cr2.
> + *
> + * A FRED guest should read its #PF faulting linear address from
> + * the event data field in its FRED stack frame instead of %cr2.
> + * But the FRED 5.0 spec still requires a FRED CPU to update %cr2
> + * in the normal way, thus %cr2 is still updated even for a FRED
> + * guest.
> + *
> + * Note, an NMI could interrupt KVM:
> + * 1) after VM exit but before CR2 is saved.
> + * 2) after CR2 is restored but before VM entry.
> + * And a #PF could happen durng NMI handlng, which overwrites %cr2.
> + * Thus exc_nmi() should save and restore %cr2 upon entering and
> + * before leaving to make sure %cr2 not corrupted.
> + */

This is 99.9% noise. What software does or does not do with respect to CR2 is
completely irrelevant. The *only* thing that matters is the architectural
behavior, and architecturally guest CR2 _must_ be up-to-date at all times because
CR2 accesses cannot be intercepted. So, just say:

/*
* Note, even though FRED delivers the faulting linear address via the
* event data field on the stack, CR2 is still updated.
*/

> if (vcpu->arch.cr2 != native_read_cr2())
> native_write_cr2(vcpu->arch.cr2);
>