Commit 4ff41732 authored by Paolo Bonzini's avatar Paolo Bonzini

KVM: x86: introduce kvm_supported_xcr0()

XSAVE support for KVM is already using host_xcr0 & KVM_SUPPORTED_XCR0 as
a "dynamic" version of KVM_SUPPORTED_XCR0.

However, this is not enough because the MPX bits should not be presented
to the guest unless kvm_x86_ops confirms the support.  So, replace all
instances of host_xcr0 & KVM_SUPPORTED_XCR0 with a new function
kvm_supported_xcr0() that also has this check.

Note that here:

		if (xstate_bv & ~KVM_SUPPORTED_XCR0)
			return -EINVAL;
		if (xstate_bv & ~host_cr0)
			return -EINVAL;

the code is equivalent to

		if ((xstate_bv & ~KVM_SUPPORTED_XCR0) ||
		    (xstate_bv & ~host_cr0)
			return -EINVAL;

i.e. "xstate_bv & (~KVM_SUPPORTED_XCR0 | ~host_cr0)" which is in turn
equal to "xstate_bv & ~(KVM_SUPPORTED_XCR0 & host_cr0)".  So we should
also use the new function there.
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 94b3ffcd
...@@ -43,6 +43,16 @@ static u32 xstate_required_size(u64 xstate_bv) ...@@ -43,6 +43,16 @@ static u32 xstate_required_size(u64 xstate_bv)
return ret; return ret;
} }
u64 kvm_supported_xcr0(void)
{
u64 xcr0 = KVM_SUPPORTED_XCR0 & host_xcr0;
if (!kvm_x86_ops->mpx_supported || !kvm_x86_ops->mpx_supported())
xcr0 &= ~(XSTATE_BNDREGS | XSTATE_BNDCSR);
return xcr0;
}
void kvm_update_cpuid(struct kvm_vcpu *vcpu) void kvm_update_cpuid(struct kvm_vcpu *vcpu)
{ {
struct kvm_cpuid_entry2 *best; struct kvm_cpuid_entry2 *best;
...@@ -73,7 +83,7 @@ void kvm_update_cpuid(struct kvm_vcpu *vcpu) ...@@ -73,7 +83,7 @@ void kvm_update_cpuid(struct kvm_vcpu *vcpu)
} else { } else {
vcpu->arch.guest_supported_xcr0 = vcpu->arch.guest_supported_xcr0 =
(best->eax | ((u64)best->edx << 32)) & (best->eax | ((u64)best->edx << 32)) &
host_xcr0 & KVM_SUPPORTED_XCR0; kvm_supported_xcr0();
vcpu->arch.guest_xstate_size = best->ebx = vcpu->arch.guest_xstate_size = best->ebx =
xstate_required_size(vcpu->arch.xcr0); xstate_required_size(vcpu->arch.xcr0);
} }
...@@ -210,13 +220,6 @@ static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -210,13 +220,6 @@ static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
entry->flags = 0; entry->flags = 0;
} }
static bool supported_xcr0_bit(unsigned bit)
{
u64 mask = ((u64)1 << bit);
return mask & KVM_SUPPORTED_XCR0 & host_xcr0;
}
#define F(x) bit(X86_FEATURE_##x) #define F(x) bit(X86_FEATURE_##x)
static int __do_cpuid_ent_emulated(struct kvm_cpuid_entry2 *entry, static int __do_cpuid_ent_emulated(struct kvm_cpuid_entry2 *entry,
...@@ -439,16 +442,18 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -439,16 +442,18 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
} }
case 0xd: { case 0xd: {
int idx, i; int idx, i;
u64 supported = kvm_supported_xcr0();
entry->eax &= host_xcr0 & KVM_SUPPORTED_XCR0; entry->eax &= supported;
entry->edx &= (host_xcr0 & KVM_SUPPORTED_XCR0) >> 32; entry->edx &= supported >> 32;
entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
for (idx = 1, i = 1; idx < 64; ++idx) { for (idx = 1, i = 1; idx < 64; ++idx) {
u64 mask = ((u64)1 << idx);
if (*nent >= maxnent) if (*nent >= maxnent)
goto out; goto out;
do_cpuid_1_ent(&entry[i], function, idx); do_cpuid_1_ent(&entry[i], function, idx);
if (entry[i].eax == 0 || !supported_xcr0_bit(idx)) if (entry[i].eax == 0 || !(supported & mask))
continue; continue;
entry[i].flags |= entry[i].flags |=
KVM_CPUID_FLAG_SIGNIFCANT_INDEX; KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
......
...@@ -3084,9 +3084,7 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, ...@@ -3084,9 +3084,7 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
* CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
* with old userspace. * with old userspace.
*/ */
if (xstate_bv & ~KVM_SUPPORTED_XCR0) if (xstate_bv & ~kvm_supported_xcr0())
return -EINVAL;
if (xstate_bv & ~host_xcr0)
return -EINVAL; return -EINVAL;
memcpy(&vcpu->arch.guest_fpu.state->xsave, memcpy(&vcpu->arch.guest_fpu.state->xsave,
guest_xsave->region, vcpu->arch.guest_xstate_size); guest_xsave->region, vcpu->arch.guest_xstate_size);
......
...@@ -126,6 +126,8 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt, ...@@ -126,6 +126,8 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
| XSTATE_BNDREGS | XSTATE_BNDCSR) | XSTATE_BNDREGS | XSTATE_BNDCSR)
extern u64 host_xcr0; extern u64 host_xcr0;
extern u64 kvm_supported_xcr0(void);
extern unsigned int min_timer_period_us; extern unsigned int min_timer_period_us;
extern struct static_key kvm_no_apic_vcpu; extern struct static_key kvm_no_apic_vcpu;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment