Re: [PATCH v3 1/1] x86/kvm/fpu: Mask guest fpstate->xfeatures with guest_supported_xcr0

From: Paolo Bonzini
Date: Mon Feb 14 2022 - 05:25:03 EST


On 2/14/22 10:43, David Edmondson wrote:
Sorry if this is a daft question:

In what situations will there be bits set in
vcpu->arch.guest_supported_xcr0 that are not set in
vcpu->arch.guest_fpu.fpstate->xfeatures ?

guest_supported_xcr0 is filtered based on supported_xcr0, which I would
expect to weed out all bits that are not set in ->xfeatures.

Good point, so we can do just

vcpu->arch.guest_fpu.fpstate->user_xfeatures =
vcpu->arch.guest_supported_xcr0;

On top of this patch, we can even replace vcpu->arch.guest_supported_xcr0
with vcpu->arch.guest_fpu.fpstate->user_xfeatures. Probably with local
variables or wrapper functions though, so as to keep the code readable.
For example:

static inline u64 kvm_guest_supported_xfd()
{
u64 guest_supported_xcr0 = vcpu->arch.guest_fpu.fpstate->user_xfeatures;

return guest_supported_xcr0 & XFEATURE_MASK_USER_DYNAMIC;
}

Also, already in this patch fpstate_realloc should do

newfps->user_xfeatures = curfps->user_xfeatures | xfeatures;

only if !guest_fpu. In other words, the user_xfeatures of the guest FPU
should be controlled exclusively by KVM_SET_CPUID2.

Thanks,

Paolo