aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2013-10-02 10:06:16 -0400
committerGleb Natapov <gleb@redhat.com>2013-10-03 05:29:09 -0400
commit4344ee981e21990f8ea14d3c9e3890b9b7b06279 (patch)
tree745e9fc25021336e11188d010589ce0a56d7b3d9
parentd7876f1be40a16223a44355740de625849504eb5 (diff)
KVM: x86: only copy XSAVE state for the supported features
This makes the interface more deterministic for userspace, which can expect (after configuring only the features it supports) to get exactly the same state from the kernel, independent of the host CPU and kernel version. Suggested-by: Gleb Natapov <gleb@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Gleb Natapov <gleb@redhat.com>
-rw-r--r--arch/x86/include/asm/kvm_host.h1
-rw-r--r--arch/x86/kvm/cpuid.c28
-rw-r--r--arch/x86/kvm/x86.c11
3 files changed, 34 insertions, 6 deletions
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 35d10d1a6b58..52110d0ceb13 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -390,6 +390,7 @@ struct kvm_vcpu_arch {
390 struct fpu guest_fpu; 390 struct fpu guest_fpu;
391 u64 xcr0; 391 u64 xcr0;
392 u64 guest_supported_xcr0; 392 u64 guest_supported_xcr0;
393 u32 guest_xstate_size;
393 394
394 struct kvm_pio_request pio; 395 struct kvm_pio_request pio;
395 void *pio_data; 396 void *pio_data;
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 5a5ff94fef65..0a1e3b8b964d 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -23,6 +23,26 @@
23#include "mmu.h" 23#include "mmu.h"
24#include "trace.h" 24#include "trace.h"
25 25
26static u32 xstate_required_size(u64 xstate_bv)
27{
28 int feature_bit = 0;
29 u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
30
31 xstate_bv &= ~XSTATE_FPSSE;
32 while (xstate_bv) {
33 if (xstate_bv & 0x1) {
34 u32 eax, ebx, ecx, edx;
35 cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
36 ret = max(ret, eax + ebx);
37 }
38
39 xstate_bv >>= 1;
40 feature_bit++;
41 }
42
43 return ret;
44}
45
26void kvm_update_cpuid(struct kvm_vcpu *vcpu) 46void kvm_update_cpuid(struct kvm_vcpu *vcpu)
27{ 47{
28 struct kvm_cpuid_entry2 *best; 48 struct kvm_cpuid_entry2 *best;
@@ -47,12 +67,16 @@ void kvm_update_cpuid(struct kvm_vcpu *vcpu)
47 } 67 }
48 68
49 best = kvm_find_cpuid_entry(vcpu, 0xD, 0); 69 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
50 if (!best) 70 if (!best) {
51 vcpu->arch.guest_supported_xcr0 = 0; 71 vcpu->arch.guest_supported_xcr0 = 0;
52 else 72 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
73 } else {
53 vcpu->arch.guest_supported_xcr0 = 74 vcpu->arch.guest_supported_xcr0 =
54 (best->eax | ((u64)best->edx << 32)) & 75 (best->eax | ((u64)best->edx << 32)) &
55 host_xcr0 & KVM_SUPPORTED_XCR0; 76 host_xcr0 & KVM_SUPPORTED_XCR0;
77 vcpu->arch.guest_xstate_size =
78 xstate_required_size(vcpu->arch.guest_supported_xcr0);
79 }
56 80
57 kvm_pmu_cpuid_update(vcpu); 81 kvm_pmu_cpuid_update(vcpu);
58} 82}
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 7d5c5207a414..e8e2d09dfe7d 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2984,11 +2984,13 @@ static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
2984static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, 2984static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
2985 struct kvm_xsave *guest_xsave) 2985 struct kvm_xsave *guest_xsave)
2986{ 2986{
2987 if (cpu_has_xsave) 2987 if (cpu_has_xsave) {
2988 memcpy(guest_xsave->region, 2988 memcpy(guest_xsave->region,
2989 &vcpu->arch.guest_fpu.state->xsave, 2989 &vcpu->arch.guest_fpu.state->xsave,
2990 xstate_size); 2990 vcpu->arch.guest_xstate_size);
2991 else { 2991 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] &=
2992 vcpu->arch.guest_supported_xcr0 | XSTATE_FPSSE;
2993 } else {
2992 memcpy(guest_xsave->region, 2994 memcpy(guest_xsave->region,
2993 &vcpu->arch.guest_fpu.state->fxsave, 2995 &vcpu->arch.guest_fpu.state->fxsave,
2994 sizeof(struct i387_fxsave_struct)); 2996 sizeof(struct i387_fxsave_struct));
@@ -3014,7 +3016,7 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
3014 if (xstate_bv & ~host_xcr0) 3016 if (xstate_bv & ~host_xcr0)
3015 return -EINVAL; 3017 return -EINVAL;
3016 memcpy(&vcpu->arch.guest_fpu.state->xsave, 3018 memcpy(&vcpu->arch.guest_fpu.state->xsave,
3017 guest_xsave->region, xstate_size); 3019 guest_xsave->region, vcpu->arch.guest_xstate_size);
3018 } else { 3020 } else {
3019 if (xstate_bv & ~XSTATE_FPSSE) 3021 if (xstate_bv & ~XSTATE_FPSSE)
3020 return -EINVAL; 3022 return -EINVAL;
@@ -6951,6 +6953,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
6951 vcpu->arch.pv_time_enabled = false; 6953 vcpu->arch.pv_time_enabled = false;
6952 6954
6953 vcpu->arch.guest_supported_xcr0 = 0; 6955 vcpu->arch.guest_supported_xcr0 = 0;
6956 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
6954 6957
6955 kvm_async_pf_hash_reset(vcpu); 6958 kvm_async_pf_hash_reset(vcpu);
6956 kvm_pmu_init(vcpu); 6959 kvm_pmu_init(vcpu);