1be5c7
From 90a276ed72deab84f3fdd4b57e9ccfc6514934fb Mon Sep 17 00:00:00 2001
1be5c7
From: Zeng Guang <guang.zeng@intel.com>
1be5c7
Date: Wed, 16 Feb 2022 22:04:33 -0800
1be5c7
Subject: [PATCH 11/24] x86: Support XFD and AMX xsave data migration
1be5c7
1be5c7
RH-Author: Paul Lai <plai@redhat.com>
1be5c7
RH-MergeRequest: 176: Enable KVM AMX support
1be5c7
RH-Commit: [11/13] 4ff6e5544ffdac4e6d2f568f7f63b937502ca6c5
1be5c7
RH-Bugzilla: 1916415
1be5c7
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
1be5c7
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
1be5c7
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
1be5c7
1be5c7
XFD(eXtended Feature Disable) allows to enable a
1be5c7
feature on xsave state while preventing specific
1be5c7
user threads from using the feature.
1be5c7
1be5c7
Support save and restore XFD MSRs if CPUID.D.1.EAX[4]
1be5c7
enumerate to be valid. Likewise migrate the MSRs and
1be5c7
related xsave state necessarily.
1be5c7
1be5c7
Signed-off-by: Zeng Guang <guang.zeng@intel.com>
1be5c7
Signed-off-by: Wei Wang <wei.w.wang@intel.com>
1be5c7
Signed-off-by: Yang Zhong <yang.zhong@intel.com>
1be5c7
Message-Id: <20220217060434.52460-8-yang.zhong@intel.com>
1be5c7
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1be5c7
(cherry picked from commit cdec2b753b487d9e8aab028231c35d87789ea083)
1be5c7
Signed-off-by: Paul Lai <plai@redhat.com>
1be5c7
---
1be5c7
 target/i386/cpu.h     |  9 +++++++++
1be5c7
 target/i386/kvm/kvm.c | 18 +++++++++++++++++
1be5c7
 target/i386/machine.c | 46 +++++++++++++++++++++++++++++++++++++++++++
1be5c7
 3 files changed, 73 insertions(+)
1be5c7
1be5c7
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
1be5c7
index 14a3501b87..8ab2a4042a 100644
1be5c7
--- a/target/i386/cpu.h
1be5c7
+++ b/target/i386/cpu.h
1be5c7
@@ -505,6 +505,9 @@ typedef enum X86Seg {
1be5c7
 
1be5c7
 #define MSR_VM_HSAVE_PA                 0xc0010117
1be5c7
 
1be5c7
+#define MSR_IA32_XFD                    0x000001c4
1be5c7
+#define MSR_IA32_XFD_ERR                0x000001c5
1be5c7
+
1be5c7
 #define MSR_IA32_BNDCFGS                0x00000d90
1be5c7
 #define MSR_IA32_XSS                    0x00000da0
1be5c7
 #define MSR_IA32_UMWAIT_CONTROL         0xe1
1be5c7
@@ -870,6 +873,8 @@ typedef uint64_t FeatureWordArray[FEATURE_WORDS];
1be5c7
 #define CPUID_7_1_EAX_AVX_VNNI          (1U << 4)
1be5c7
 /* AVX512 BFloat16 Instruction */
1be5c7
 #define CPUID_7_1_EAX_AVX512_BF16       (1U << 5)
1be5c7
+/* XFD Extend Feature Disabled */
1be5c7
+#define CPUID_D_1_EAX_XFD               (1U << 4)
1be5c7
 
1be5c7
 /* Packets which contain IP payload have LIP values */
1be5c7
 #define CPUID_14_0_ECX_LIP              (1U << 31)
1be5c7
@@ -1610,6 +1615,10 @@ typedef struct CPUX86State {
1be5c7
     uint64_t msr_rtit_cr3_match;
1be5c7
     uint64_t msr_rtit_addrs[MAX_RTIT_ADDRS];
1be5c7
 
1be5c7
+    /* Per-VCPU XFD MSRs */
1be5c7
+    uint64_t msr_xfd;
1be5c7
+    uint64_t msr_xfd_err;
1be5c7
+
1be5c7
     /* exception/interrupt handling */
1be5c7
     int error_code;
1be5c7
     int exception_is_int;
1be5c7
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
1be5c7
index d3d476df27..b1128b0e07 100644
1be5c7
--- a/target/i386/kvm/kvm.c
1be5c7
+++ b/target/i386/kvm/kvm.c
1be5c7
@@ -3219,6 +3219,13 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
1be5c7
                               env->msr_ia32_sgxlepubkeyhash[3]);
1be5c7
         }
1be5c7
 
1be5c7
+        if (env->features[FEAT_XSAVE] & CPUID_D_1_EAX_XFD) {
1be5c7
+            kvm_msr_entry_add(cpu, MSR_IA32_XFD,
1be5c7
+                              env->msr_xfd);
1be5c7
+            kvm_msr_entry_add(cpu, MSR_IA32_XFD_ERR,
1be5c7
+                              env->msr_xfd_err);
1be5c7
+        }
1be5c7
+
1be5c7
         /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
1be5c7
          *       kvm_put_msr_feature_control. */
1be5c7
     }
1be5c7
@@ -3571,6 +3578,11 @@ static int kvm_get_msrs(X86CPU *cpu)
1be5c7
         kvm_msr_entry_add(cpu, MSR_IA32_SGXLEPUBKEYHASH3, 0);
1be5c7
     }
1be5c7
 
1be5c7
+    if (env->features[FEAT_XSAVE] & CPUID_D_1_EAX_XFD) {
1be5c7
+        kvm_msr_entry_add(cpu, MSR_IA32_XFD, 0);
1be5c7
+        kvm_msr_entry_add(cpu, MSR_IA32_XFD_ERR, 0);
1be5c7
+    }
1be5c7
+
1be5c7
     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, cpu->kvm_msr_buf);
1be5c7
     if (ret < 0) {
1be5c7
         return ret;
1be5c7
@@ -3870,6 +3882,12 @@ static int kvm_get_msrs(X86CPU *cpu)
1be5c7
             env->msr_ia32_sgxlepubkeyhash[index - MSR_IA32_SGXLEPUBKEYHASH0] =
1be5c7
                            msrs[i].data;
1be5c7
             break;
1be5c7
+        case MSR_IA32_XFD:
1be5c7
+            env->msr_xfd = msrs[i].data;
1be5c7
+            break;
1be5c7
+        case MSR_IA32_XFD_ERR:
1be5c7
+            env->msr_xfd_err = msrs[i].data;
1be5c7
+            break;
1be5c7
         }
1be5c7
     }
1be5c7
 
1be5c7
diff --git a/target/i386/machine.c b/target/i386/machine.c
1be5c7
index 83c2b91529..3977e9d8f8 100644
1be5c7
--- a/target/i386/machine.c
1be5c7
+++ b/target/i386/machine.c
1be5c7
@@ -1455,6 +1455,48 @@ static const VMStateDescription vmstate_msr_intel_sgx = {
1be5c7
     }
1be5c7
 };
1be5c7
 
1be5c7
+static bool xfd_msrs_needed(void *opaque)
1be5c7
+{
1be5c7
+    X86CPU *cpu = opaque;
1be5c7
+    CPUX86State *env = &cpu->env;
1be5c7
+
1be5c7
+    return !!(env->features[FEAT_XSAVE] & CPUID_D_1_EAX_XFD);
1be5c7
+}
1be5c7
+
1be5c7
+static const VMStateDescription vmstate_msr_xfd = {
1be5c7
+    .name = "cpu/msr_xfd",
1be5c7
+    .version_id = 1,
1be5c7
+    .minimum_version_id = 1,
1be5c7
+    .needed = xfd_msrs_needed,
1be5c7
+    .fields = (VMStateField[]) {
1be5c7
+        VMSTATE_UINT64(env.msr_xfd, X86CPU),
1be5c7
+        VMSTATE_UINT64(env.msr_xfd_err, X86CPU),
1be5c7
+        VMSTATE_END_OF_LIST()
1be5c7
+    }
1be5c7
+};
1be5c7
+
1be5c7
+#ifdef TARGET_X86_64
1be5c7
+static bool amx_xtile_needed(void *opaque)
1be5c7
+{
1be5c7
+    X86CPU *cpu = opaque;
1be5c7
+    CPUX86State *env = &cpu->env;
1be5c7
+
1be5c7
+    return !!(env->features[FEAT_7_0_EDX] & CPUID_7_0_EDX_AMX_TILE);
1be5c7
+}
1be5c7
+
1be5c7
+static const VMStateDescription vmstate_amx_xtile = {
1be5c7
+    .name = "cpu/intel_amx_xtile",
1be5c7
+    .version_id = 1,
1be5c7
+    .minimum_version_id = 1,
1be5c7
+    .needed = amx_xtile_needed,
1be5c7
+    .fields = (VMStateField[]) {
1be5c7
+        VMSTATE_UINT8_ARRAY(env.xtilecfg, X86CPU, 64),
1be5c7
+        VMSTATE_UINT8_ARRAY(env.xtiledata, X86CPU, 8192),
1be5c7
+        VMSTATE_END_OF_LIST()
1be5c7
+    }
1be5c7
+};
1be5c7
+#endif
1be5c7
+
1be5c7
 const VMStateDescription vmstate_x86_cpu = {
1be5c7
     .name = "cpu",
1be5c7
     .version_id = 12,
1be5c7
@@ -1593,6 +1635,10 @@ const VMStateDescription vmstate_x86_cpu = {
1be5c7
 #endif
1be5c7
         &vmstate_msr_tsx_ctrl,
1be5c7
         &vmstate_msr_intel_sgx,
1be5c7
+        &vmstate_msr_xfd,
1be5c7
+#ifdef TARGET_X86_64
1be5c7
+        &vmstate_amx_xtile,
1be5c7
+#endif
1be5c7
         NULL
1be5c7
     }
1be5c7
 };
1be5c7
-- 
1be5c7
2.35.3
1be5c7