9ae3a8
From a824033fdc6956ee449f49f6d1a74ebfb21d0700 Mon Sep 17 00:00:00 2001
9ae3a8
From: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
Date: Fri, 31 Mar 2017 11:37:37 +0200
9ae3a8
Subject: [PATCH 1/4] target-i386: get/set/migrate XSAVES state
9ae3a8
9ae3a8
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
Message-id: <20170331113737.9930-1-ehabkost@redhat.com>
9ae3a8
Patchwork-id: 74596
9ae3a8
O-Subject: [RHEL-7.4 qemu-kvm PATCH] target-i386: get/set/migrate XSAVES state
9ae3a8
Bugzilla: 1327593
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Bandan Das <bsd@redhat.com>
9ae3a8
RH-Acked-by: David Hildenbrand <david@redhat.com>
9ae3a8
9ae3a8
From: Wanpeng Li <wanpeng.li@linux.intel.com>
9ae3a8
9ae3a8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1327593
9ae3a8
Brew: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=12913269
9ae3a8
9ae3a8
Add xsaves related definition, it also adds corresponding part
9ae3a8
to kvm_get/put, and vmstate.
9ae3a8
9ae3a8
Backport notes:
9ae3a8
* As we didn't have unmigratable_flags yet, our backport
9ae3a8
  of upstream commit 0bb0b2d2fe7f645ddaf1f0ff40ac669c9feb4aa1
9ae3a8
  (commit 5fcaf5176d7545518c76f3aa8ea7ce6fb063c62d) didn't
9ae3a8
  include "xsaves" cpuid_xsave_feature_name[]. This patch now
9ae3a8
  adds "xsave" to cpuid_xsave_feature_name[].
9ae3a8
9ae3a8
Signed-off-by: Wanpeng Li <wanpeng.li@linux.intel.com>
9ae3a8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
(cherry picked from commit 18cd2c17b5370369a886155c001da0a7f54bbcca)
9ae3a8
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 target-i386/cpu.c     |  2 +-
9ae3a8
 target-i386/cpu.h     |  2 ++
9ae3a8
 target-i386/kvm.c     | 15 +++++++++++++++
9ae3a8
 target-i386/machine.c | 21 +++++++++++++++++++++
9ae3a8
 4 files changed, 39 insertions(+), 1 deletion(-)
9ae3a8
9ae3a8
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
9ae3a8
index 33f0997..ae56995 100644
9ae3a8
--- a/target-i386/cpu.c
9ae3a8
+++ b/target-i386/cpu.c
9ae3a8
@@ -177,7 +177,7 @@ static const char *cpuid_7_0_edx_feature_name[] = {
9ae3a8
 };
9ae3a8
 
9ae3a8
 static const char *cpuid_xsave_feature_name[] = {
9ae3a8
-    "xsaveopt", "xsavec", "xgetbv1", NULL,
9ae3a8
+    "xsaveopt", "xsavec", "xgetbv1", "xsaves",
9ae3a8
     NULL, NULL, NULL, NULL,
9ae3a8
     NULL, NULL, NULL, NULL,
9ae3a8
     NULL, NULL, NULL, NULL,
9ae3a8
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
9ae3a8
index f04deb4..ac60309 100644
9ae3a8
--- a/target-i386/cpu.h
9ae3a8
+++ b/target-i386/cpu.h
9ae3a8
@@ -384,6 +384,7 @@
9ae3a8
 #define MSR_VM_HSAVE_PA                 0xc0010117
9ae3a8
 
9ae3a8
 #define MSR_IA32_BNDCFGS                0x00000d90
9ae3a8
+#define MSR_IA32_XSS                    0x00000da0
9ae3a8
 
9ae3a8
 #define XSTATE_FP                       (1ULL << 0)
9ae3a8
 #define XSTATE_SSE                      (1ULL << 1)
9ae3a8
@@ -1026,6 +1027,7 @@ typedef struct CPUX86State {
9ae3a8
     uint64_t xstate_bv;
9ae3a8
 
9ae3a8
     uint64_t xcr0;
9ae3a8
+    uint64_t xss;
9ae3a8
 
9ae3a8
     TPRAccess tpr_access_type;
9ae3a8
 } CPUX86State;
9ae3a8
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
9ae3a8
index e1b0ca2..6a479f4 100644
9ae3a8
--- a/target-i386/kvm.c
9ae3a8
+++ b/target-i386/kvm.c
9ae3a8
@@ -76,6 +76,7 @@ static bool has_msr_hv_hypercall;
9ae3a8
 static bool has_msr_hv_vapic;
9ae3a8
 static bool has_msr_hv_tsc;
9ae3a8
 static bool has_msr_mtrr;
9ae3a8
+static bool has_msr_xss;
9ae3a8
 
9ae3a8
 static bool has_msr_architectural_pmu;
9ae3a8
 static uint32_t num_architectural_pmu_counters;
9ae3a8
@@ -795,6 +796,10 @@ static int kvm_get_supported_msrs(KVMState *s)
9ae3a8
                     has_msr_bndcfgs = true;
9ae3a8
                     continue;
9ae3a8
                 }
9ae3a8
+                if (kvm_msr_list->indices[i] == MSR_IA32_XSS) {
9ae3a8
+                    has_msr_xss = true;
9ae3a8
+                    continue;
9ae3a8
+                }
9ae3a8
             }
9ae3a8
         }
9ae3a8
 
9ae3a8
@@ -1177,6 +1182,9 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
9ae3a8
     if (has_msr_bndcfgs) {
9ae3a8
         kvm_msr_entry_set(&msrs[n++], MSR_IA32_BNDCFGS, env->msr_bndcfgs);
9ae3a8
     }
9ae3a8
+    if (has_msr_xss) {
9ae3a8
+        kvm_msr_entry_set(&msrs[n++], MSR_IA32_XSS, env->xss);
9ae3a8
+    }
9ae3a8
 #ifdef TARGET_X86_64
9ae3a8
     if (lm_capable_kernel) {
9ae3a8
         kvm_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar);
9ae3a8
@@ -1530,6 +1538,10 @@ static int kvm_get_msrs(X86CPU *cpu)
9ae3a8
     if (has_msr_bndcfgs) {
9ae3a8
         msrs[n++].index = MSR_IA32_BNDCFGS;
9ae3a8
     }
9ae3a8
+    if (has_msr_xss) {
9ae3a8
+        msrs[n++].index = MSR_IA32_XSS;
9ae3a8
+    }
9ae3a8
+
9ae3a8
 
9ae3a8
     if (!env->tsc_valid) {
9ae3a8
         msrs[n++].index = MSR_IA32_TSC;
9ae3a8
@@ -1677,6 +1689,9 @@ static int kvm_get_msrs(X86CPU *cpu)
9ae3a8
         case MSR_IA32_BNDCFGS:
9ae3a8
             env->msr_bndcfgs = msrs[i].data;
9ae3a8
             break;
9ae3a8
+        case MSR_IA32_XSS:
9ae3a8
+            env->xss = msrs[i].data;
9ae3a8
+            break;
9ae3a8
         default:
9ae3a8
             if (msrs[i].index >= MSR_MC0_CTL &&
9ae3a8
                 msrs[i].index < MSR_MC0_CTL + (env->mcg_cap & 0xff) * 4) {
9ae3a8
diff --git a/target-i386/machine.c b/target-i386/machine.c
9ae3a8
index 2c97002..ce7fcd3 100644
9ae3a8
--- a/target-i386/machine.c
9ae3a8
+++ b/target-i386/machine.c
9ae3a8
@@ -704,6 +704,24 @@ static const VMStateDescription vmstate_avx512 = {
9ae3a8
     }
9ae3a8
 };
9ae3a8
 
9ae3a8
+static bool xss_needed(void *opaque)
9ae3a8
+{
9ae3a8
+    X86CPU *cpu = opaque;
9ae3a8
+    CPUX86State *env = &cpu->env;
9ae3a8
+
9ae3a8
+    return env->xss != 0;
9ae3a8
+}
9ae3a8
+
9ae3a8
+static const VMStateDescription vmstate_xss = {
9ae3a8
+    .name = "cpu/xss",
9ae3a8
+    .version_id = 1,
9ae3a8
+    .minimum_version_id = 1,
9ae3a8
+    .fields = (VMStateField[]) {
9ae3a8
+        VMSTATE_UINT64(env.xss, X86CPU),
9ae3a8
+        VMSTATE_END_OF_LIST()
9ae3a8
+    }
9ae3a8
+};
9ae3a8
+
9ae3a8
 const VMStateDescription vmstate_x86_cpu = {
9ae3a8
     .name = "cpu",
9ae3a8
     .version_id = 12,
9ae3a8
@@ -850,6 +868,9 @@ const VMStateDescription vmstate_x86_cpu = {
9ae3a8
         }, {
9ae3a8
             .vmsd = &vmstate_avx512,
9ae3a8
             .needed = avx512_needed,
9ae3a8
+         }, {
9ae3a8
+            .vmsd = &vmstate_xss,
9ae3a8
+            .needed = xss_needed,
9ae3a8
         } , {
9ae3a8
             /* empty */
9ae3a8
         }
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8