Re: [PATCH v4 06/21] x86/fpu: Ignore APX when copying from/to guest FPU
From: Paolo Bonzini
Date: Thu May 14 2026 - 12:07:38 EST
On Wed, May 13, 2026 at 9:11 PM Dave Hansen <dave.hansen@xxxxxxxxx> wrote:
>
> On 5/11/26 18:14, Chang S. Bae wrote:
> > From: Paolo Bonzini <pbonzini@xxxxxxxxxx>
> >
> > KVM will store APX extended GPRs directly in the regs[] field of struct
> > kvm_vcpu. This is done to make accessors more uniform between GPRs and
> > EGPRs, and partly because x86 maintainers expressed the desire to compile
> > Linux with APX enabled sooner or later; accessing guest EGPRs from KVM's
> > C code would get in the way.
> >
> > Therefore, let KVM handle the APX feature on its own when executing
> > ioctls to get and set the virtual machine's XSAVE state.
>
> I'm sure this works.
>
> But it does look a bit hacky to me. I just despise adding new special
> cases to the XSAVE code when we don't _have_ to.
>
> Couldn't we, for instance, just let the APX registers use the "fpu"
> ABIs? PKRU is weird too, but it still gets to use those ABIs.
For PKRU it makes sense because it is not kept in the FPU even for the
non-guest case. This is not the case for EGPRs, but I guess it would
be the same if the kernel was compiled with APX? If the kernel entry
code would have to stash r16-r31 from userspace before entering C
code, then we can add similar "unsigned long *egprs" arguments to
copy_uabi_from_kernel_to_xstate, copy_uabi_to_xstate, and likewise in
the other direction.
The problem in doing so is that these are just plans; if the idea of
compiling the kernel with APX doesn't materialize, all this copying
code would just sit there in arch/x86/fpu, adding extra complexity
solely for KVM. Maybe we can add a comment like
/*
* Since the sigframe case can copy the EGPRs from the FPU,
* copying the registers in and out of struct kvm_vcpu is left
* to KVM code. If EGPRs are ever moved to struct thread_struct,
* they should be handled in the same way as PKRU is.
*/
to clarify the reason for this choice?
Separately, it's possible to avoid hardcoding APX by passing a mask
argument to fpu_copy_uabi_to_guest_fpstate and
fpu_copy_guest_fpstate_to_uabi. KVM can then pass
~(u64)XFEATURE_MASK_APX.
Paolo