diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2014-02-24 06:15:16 -0500 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2014-03-17 07:21:38 -0400 |
commit | 4ff417320c2dfc984ec1939a7da888976441a881 (patch) | |
tree | 3448c4e65aecd71260c9bbe4e99c70533c6055e9 | |
parent | 94b3ffcd41a90d2cb0b32ca23aa58a01111d5dc0 (diff) |
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: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r-- | arch/x86/kvm/cpuid.c | 27 | ||||
-rw-r--r-- | arch/x86/kvm/x86.c | 4 | ||||
-rw-r--r-- | arch/x86/kvm/x86.h | 2 |
3 files changed, 19 insertions, 14 deletions
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index ddc8a7e165df..18aefb4d0927 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c | |||
@@ -43,6 +43,16 @@ static u32 xstate_required_size(u64 xstate_bv) | |||
43 | return ret; | 43 | return ret; |
44 | } | 44 | } |
45 | 45 | ||
46 | u64 kvm_supported_xcr0(void) | ||
47 | { | ||
48 | u64 xcr0 = KVM_SUPPORTED_XCR0 & host_xcr0; | ||
49 | |||
50 | if (!kvm_x86_ops->mpx_supported || !kvm_x86_ops->mpx_supported()) | ||
51 | xcr0 &= ~(XSTATE_BNDREGS | XSTATE_BNDCSR); | ||
52 | |||
53 | return xcr0; | ||
54 | } | ||
55 | |||
46 | void kvm_update_cpuid(struct kvm_vcpu *vcpu) | 56 | void kvm_update_cpuid(struct kvm_vcpu *vcpu) |
47 | { | 57 | { |
48 | struct kvm_cpuid_entry2 *best; | 58 | struct kvm_cpuid_entry2 *best; |
@@ -73,7 +83,7 @@ void kvm_update_cpuid(struct kvm_vcpu *vcpu) | |||
73 | } else { | 83 | } else { |
74 | vcpu->arch.guest_supported_xcr0 = | 84 | vcpu->arch.guest_supported_xcr0 = |
75 | (best->eax | ((u64)best->edx << 32)) & | 85 | (best->eax | ((u64)best->edx << 32)) & |
76 | host_xcr0 & KVM_SUPPORTED_XCR0; | 86 | kvm_supported_xcr0(); |
77 | vcpu->arch.guest_xstate_size = best->ebx = | 87 | vcpu->arch.guest_xstate_size = best->ebx = |
78 | xstate_required_size(vcpu->arch.xcr0); | 88 | xstate_required_size(vcpu->arch.xcr0); |
79 | } | 89 | } |
@@ -210,13 +220,6 @@ static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |||
210 | entry->flags = 0; | 220 | entry->flags = 0; |
211 | } | 221 | } |
212 | 222 | ||
213 | static bool supported_xcr0_bit(unsigned bit) | ||
214 | { | ||
215 | u64 mask = ((u64)1 << bit); | ||
216 | |||
217 | return mask & KVM_SUPPORTED_XCR0 & host_xcr0; | ||
218 | } | ||
219 | |||
220 | #define F(x) bit(X86_FEATURE_##x) | 223 | #define F(x) bit(X86_FEATURE_##x) |
221 | 224 | ||
222 | static int __do_cpuid_ent_emulated(struct kvm_cpuid_entry2 *entry, | 225 | 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 | } | 442 | } |
440 | case 0xd: { | 443 | case 0xd: { |
441 | int idx, i; | 444 | int idx, i; |
445 | u64 supported = kvm_supported_xcr0(); | ||
442 | 446 | ||
443 | entry->eax &= host_xcr0 & KVM_SUPPORTED_XCR0; | 447 | entry->eax &= supported; |
444 | entry->edx &= (host_xcr0 & KVM_SUPPORTED_XCR0) >> 32; | 448 | entry->edx &= supported >> 32; |
445 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | 449 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
446 | for (idx = 1, i = 1; idx < 64; ++idx) { | 450 | for (idx = 1, i = 1; idx < 64; ++idx) { |
451 | u64 mask = ((u64)1 << idx); | ||
447 | if (*nent >= maxnent) | 452 | if (*nent >= maxnent) |
448 | goto out; | 453 | goto out; |
449 | 454 | ||
450 | do_cpuid_1_ent(&entry[i], function, idx); | 455 | do_cpuid_1_ent(&entry[i], function, idx); |
451 | if (entry[i].eax == 0 || !supported_xcr0_bit(idx)) | 456 | if (entry[i].eax == 0 || !(supported & mask)) |
452 | continue; | 457 | continue; |
453 | entry[i].flags |= | 458 | entry[i].flags |= |
454 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | 459 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a37da6b0165a..3f5fb4535f9c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c | |||
@@ -3084,9 +3084,7 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, | |||
3084 | * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility | 3084 | * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility |
3085 | * with old userspace. | 3085 | * with old userspace. |
3086 | */ | 3086 | */ |
3087 | if (xstate_bv & ~KVM_SUPPORTED_XCR0) | 3087 | if (xstate_bv & ~kvm_supported_xcr0()) |
3088 | return -EINVAL; | ||
3089 | if (xstate_bv & ~host_xcr0) | ||
3090 | return -EINVAL; | 3088 | return -EINVAL; |
3091 | memcpy(&vcpu->arch.guest_fpu.state->xsave, | 3089 | memcpy(&vcpu->arch.guest_fpu.state->xsave, |
3092 | guest_xsave->region, vcpu->arch.guest_xstate_size); | 3090 | guest_xsave->region, vcpu->arch.guest_xstate_size); |
diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 392ecbff0030..8c97bac9a895 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h | |||
@@ -126,6 +126,8 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt, | |||
126 | | XSTATE_BNDREGS | XSTATE_BNDCSR) | 126 | | XSTATE_BNDREGS | XSTATE_BNDCSR) |
127 | extern u64 host_xcr0; | 127 | extern u64 host_xcr0; |
128 | 128 | ||
129 | extern u64 kvm_supported_xcr0(void); | ||
130 | |||
129 | extern unsigned int min_timer_period_us; | 131 | extern unsigned int min_timer_period_us; |
130 | 132 | ||
131 | extern struct static_key kvm_no_apic_vcpu; | 133 | extern struct static_key kvm_no_apic_vcpu; |