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

From: Li, Xin3
Date: Thu Jun 13 2024 - 14:03:15 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.
> */

Will do!

There is a reason for this comment because it won't be architectural:
https://lore.kernel.org/lkml/c0c7c605-d487-483e-a034-983b76ee1dfa@xxxxxxxxx/

FRED is designed to atomically save and restore _full_ supervisor/user
context upon event delivery and return. But unfortunately, KVM still has
to save/restore guest CR2 explicitly due to the issue mentioned above.

Thanks!
Xin