Re: [PATCH v14 06/22] KVM: x86: Load guest FPU state when access XSAVE-managed MSRs
From: Chao Gao
Date: Wed Sep 10 2025 - 07:18:31 EST
On Wed, Sep 10, 2025 at 05:37:50PM +0800, Xiaoyao Li wrote:
>On 9/9/2025 5:39 PM, Chao Gao wrote:
>> From: Sean Christopherson <seanjc@xxxxxxxxxx>
>>
>> Load the guest's FPU state if userspace is accessing MSRs whose values
>> are managed by XSAVES. Introduce two helpers, kvm_{get,set}_xstate_msr(),
>> to facilitate access to such kind of MSRs.
>>
>> If MSRs supported in kvm_caps.supported_xss are passed through to guest,
>> the guest MSRs are swapped with host's before vCPU exits to userspace and
>> after it reenters kernel before next VM-entry.
>>
>> Because the modified code is also used for the KVM_GET_MSRS device ioctl(),
>> explicitly check @vcpu is non-null before attempting to load guest state.
>> The XSAVE-managed MSRs cannot be retrieved via the device ioctl() without
>> loading guest FPU state (which doesn't exist).
>>
>> Note that guest_cpuid_has() is not queried as host userspace is allowed to
>> access MSRs that have not been exposed to the guest, e.g. it might do
>> KVM_SET_MSRS prior to KVM_SET_CPUID2.
...
>> + bool fpu_loaded = false;
>> int i;
>> - for (i = 0; i < msrs->nmsrs; ++i)
>> + for (i = 0; i < msrs->nmsrs; ++i) {
>> + /*
>> + * If userspace is accessing one or more XSTATE-managed MSRs,
>> + * temporarily load the guest's FPU state so that the guest's
>> + * MSR value(s) is resident in hardware, i.e. so that KVM can
>> + * get/set the MSR via RDMSR/WRMSR.
>> + */
>> + if (vcpu && !fpu_loaded && kvm_caps.supported_xss &&
>
>why not check vcpu->arch.guest_supported_xss?
Looks like Sean anticipated someone would ask this question.