05bba0
From 5567938d29bf8a0ce451e59a8db009c12ab3444c Mon Sep 17 00:00:00 2001
05bba0
From: Eduardo Habkost <ehabkost@redhat.com>
05bba0
Date: Thu, 25 Jun 2015 19:31:29 +0200
05bba0
Subject: [PATCH 09/10] target-i386: add Intel AVX-512 support
05bba0
05bba0
Message-id: <1435260689-9556-9-git-send-email-ehabkost@redhat.com>
05bba0
Patchwork-id: 66506
05bba0
O-Subject: [RHEL-7.2 qemu-kvm PATCH 8/8] target-i386: add Intel AVX-512 support
05bba0
Bugzilla: 1233350
05bba0
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
05bba0
RH-Acked-by: Bandan Das <bsd@redhat.com>
05bba0
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
05bba0
05bba0
Add AVX512 feature bits, register definition and corresponding
05bba0
xsave/vmstate support.
05bba0
05bba0
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
05bba0
Signed-off-by: Chao Peng <chao.p.peng@linux.intel.com>
05bba0
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
05bba0
(cherry picked from commit 9aecd6f8aef653cea58932f06a2740299dbe5fd3)
05bba0
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
05bba0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
05bba0
05bba0
Conflicts:
05bba0
	target-i386/cpu.h
05bba0
	target-i386/machine.c
05bba0
---
05bba0
 target-i386/cpu.c     | 10 ++++--
05bba0
 target-i386/cpu.h     | 61 ++++++++++++++++++++++++++++++++++++
05bba0
 target-i386/kvm.c     | 19 +++++++++++
05bba0
 target-i386/machine.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++
05bba0
 4 files changed, 175 insertions(+), 2 deletions(-)
05bba0
05bba0
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
05bba0
index fe60c33..06efe17 100644
05bba0
--- a/target-i386/cpu.c
05bba0
+++ b/target-i386/cpu.c
05bba0
@@ -146,8 +146,8 @@ static const char *svm_feature_name[] = {
05bba0
 static const char *cpuid_7_0_ebx_feature_name[] = {
05bba0
     "fsgsbase", NULL, NULL, "bmi1", "hle", "avx2", NULL, "smep",
05bba0
     "bmi2", "erms", "invpcid", "rtm", NULL, NULL, "mpx", NULL,
05bba0
-    NULL, NULL, "rdseed", "adx", "smap", NULL, NULL, NULL,
05bba0
-    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
05bba0
+    "avx512f", NULL, "rdseed", "adx", "smap", NULL, NULL, NULL,
05bba0
+    NULL, NULL, "avx512pf", "avx512er", "avx512cd", NULL, NULL, NULL,
05bba0
 };
05bba0
 
05bba0
 typedef struct FeatureWordInfo {
05bba0
@@ -228,6 +228,12 @@ static const ExtSaveArea ext_save_areas[] = {
05bba0
             .offset = 0x3c0, .size = 0x40  },
05bba0
     [4] = { .feature = FEAT_7_0_EBX, .bits = CPUID_7_0_EBX_MPX,
05bba0
             .offset = 0x400, .size = 0x40  },
05bba0
+    [5] = { .feature = FEAT_7_0_EBX, .bits = CPUID_7_0_EBX_AVX512F,
05bba0
+            .offset = 0x440, .size = 0x40 },
05bba0
+    [6] = { .feature = FEAT_7_0_EBX, .bits = CPUID_7_0_EBX_AVX512F,
05bba0
+            .offset = 0x480, .size = 0x200 },
05bba0
+    [7] = { .feature = FEAT_7_0_EBX, .bits = CPUID_7_0_EBX_AVX512F,
05bba0
+            .offset = 0x680, .size = 0x400 },
05bba0
 };
05bba0
 
05bba0
 const char *get_register_name_32(unsigned int reg)
05bba0
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
05bba0
index fc1e42e..61e9b86 100644
05bba0
--- a/target-i386/cpu.h
05bba0
+++ b/target-i386/cpu.h
05bba0
@@ -390,6 +390,9 @@
05bba0
 #define XSTATE_YMM                      (1ULL << 2)
05bba0
 #define XSTATE_BNDREGS                  (1ULL << 3)
05bba0
 #define XSTATE_BNDCSR                   (1ULL << 4)
05bba0
+#define XSTATE_OPMASK                   (1ULL << 5)
05bba0
+#define XSTATE_ZMM_Hi256                (1ULL << 6)
05bba0
+#define XSTATE_Hi16_ZMM                 (1ULL << 7)
05bba0
 
05bba0
 
05bba0
 /* CPUID feature words */
05bba0
@@ -554,9 +557,13 @@ typedef uint32_t FeatureWordArray[FEATURE_WORDS];
05bba0
 #define CPUID_7_0_EBX_INVPCID  (1U << 10)
05bba0
 #define CPUID_7_0_EBX_RTM      (1U << 11)
05bba0
 #define CPUID_7_0_EBX_MPX      (1U << 14)
05bba0
+#define CPUID_7_0_EBX_AVX512F  (1U << 16) /* AVX-512 Foundation */
05bba0
 #define CPUID_7_0_EBX_RDSEED   (1U << 18)
05bba0
 #define CPUID_7_0_EBX_ADX      (1U << 19)
05bba0
 #define CPUID_7_0_EBX_SMAP     (1U << 20)
05bba0
+#define CPUID_7_0_EBX_AVX512PF (1U << 26) /* AVX-512 Prefetch */
05bba0
+#define CPUID_7_0_EBX_AVX512ER (1U << 27) /* AVX-512 Exponential and Reciprocal */
05bba0
+#define CPUID_7_0_EBX_AVX512CD (1U << 28) /* AVX-512 Conflict Detection */
05bba0
 
05bba0
 #define CPUID_VENDOR_SZ      12
05bba0
 
05bba0
@@ -697,6 +704,24 @@ typedef union {
05bba0
 } XMMReg;
05bba0
 
05bba0
 typedef union {
05bba0
+    uint8_t _b[32];
05bba0
+    uint16_t _w[16];
05bba0
+    uint32_t _l[8];
05bba0
+    uint64_t _q[4];
05bba0
+    float32 _s[8];
05bba0
+    float64 _d[4];
05bba0
+} YMMReg;
05bba0
+
05bba0
+typedef union {
05bba0
+    uint8_t _b[64];
05bba0
+    uint16_t _w[32];
05bba0
+    uint32_t _l[16];
05bba0
+    uint64_t _q[8];
05bba0
+    float32 _s[16];
05bba0
+    float64 _d[8];
05bba0
+} ZMMReg;
05bba0
+
05bba0
+typedef union {
05bba0
     uint8_t _b[8];
05bba0
     uint16_t _w[4];
05bba0
     uint32_t _l[2];
05bba0
@@ -715,6 +740,20 @@ typedef struct BNDCSReg {
05bba0
 } BNDCSReg;
05bba0
 
05bba0
 #ifdef HOST_WORDS_BIGENDIAN
05bba0
+#define ZMM_B(n) _b[63 - (n)]
05bba0
+#define ZMM_W(n) _w[31 - (n)]
05bba0
+#define ZMM_L(n) _l[15 - (n)]
05bba0
+#define ZMM_S(n) _s[15 - (n)]
05bba0
+#define ZMM_Q(n) _q[7 - (n)]
05bba0
+#define ZMM_D(n) _d[7 - (n)]
05bba0
+
05bba0
+#define YMM_B(n) _b[31 - (n)]
05bba0
+#define YMM_W(n) _w[15 - (n)]
05bba0
+#define YMM_L(n) _l[7 - (n)]
05bba0
+#define YMM_S(n) _s[7 - (n)]
05bba0
+#define YMM_Q(n) _q[3 - (n)]
05bba0
+#define YMM_D(n) _d[3 - (n)]
05bba0
+
05bba0
 #define XMM_B(n) _b[15 - (n)]
05bba0
 #define XMM_W(n) _w[7 - (n)]
05bba0
 #define XMM_L(n) _l[3 - (n)]
05bba0
@@ -727,6 +766,20 @@ typedef struct BNDCSReg {
05bba0
 #define MMX_L(n) _l[1 - (n)]
05bba0
 #define MMX_S(n) _s[1 - (n)]
05bba0
 #else
05bba0
+#define ZMM_B(n) _b[n]
05bba0
+#define ZMM_W(n) _w[n]
05bba0
+#define ZMM_L(n) _l[n]
05bba0
+#define ZMM_S(n) _s[n]
05bba0
+#define ZMM_Q(n) _q[n]
05bba0
+#define ZMM_D(n) _d[n]
05bba0
+
05bba0
+#define YMM_B(n) _b[n]
05bba0
+#define YMM_W(n) _w[n]
05bba0
+#define YMM_L(n) _l[n]
05bba0
+#define YMM_S(n) _s[n]
05bba0
+#define YMM_Q(n) _q[n]
05bba0
+#define YMM_D(n) _d[n]
05bba0
+
05bba0
 #define XMM_B(n) _b[n]
05bba0
 #define XMM_W(n) _w[n]
05bba0
 #define XMM_L(n) _l[n]
05bba0
@@ -765,6 +818,8 @@ typedef struct {
05bba0
 
05bba0
 #define NB_MMU_MODES 3
05bba0
 
05bba0
+#define NB_OPMASK_REGS 8
05bba0
+
05bba0
 typedef enum TPRAccess {
05bba0
     TPR_ACCESS_READ,
05bba0
     TPR_ACCESS_WRITE,
05bba0
@@ -826,6 +881,12 @@ typedef struct CPUX86State {
05bba0
 
05bba0
     XMMReg ymmh_regs[CPU_NB_REGS];
05bba0
 
05bba0
+    uint64_t opmask_regs[NB_OPMASK_REGS];
05bba0
+    YMMReg zmmh_regs[CPU_NB_REGS];
05bba0
+#ifdef TARGET_X86_64
05bba0
+    ZMMReg hi16_zmm_regs[CPU_NB_REGS];
05bba0
+#endif
05bba0
+
05bba0
     /* sysenter registers */
05bba0
     uint32_t sysenter_cs;
05bba0
     target_ulong sysenter_esp;
05bba0
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
05bba0
index bbbbac0..c91bfb8 100644
05bba0
--- a/target-i386/kvm.c
05bba0
+++ b/target-i386/kvm.c
05bba0
@@ -995,6 +995,9 @@ static int kvm_put_fpu(X86CPU *cpu)
05bba0
 #define XSAVE_YMMH_SPACE  144
05bba0
 #define XSAVE_BNDREGS     240
05bba0
 #define XSAVE_BNDCSR      256
05bba0
+#define XSAVE_OPMASK      272
05bba0
+#define XSAVE_ZMM_Hi256   288
05bba0
+#define XSAVE_Hi16_ZMM    416
05bba0
 
05bba0
 static int kvm_put_xsave(X86CPU *cpu)
05bba0
 {
05bba0
@@ -1031,6 +1034,14 @@ static int kvm_put_xsave(X86CPU *cpu)
05bba0
             sizeof env->bnd_regs);
05bba0
     memcpy(&xsave->region[XSAVE_BNDCSR], &env->bndcs_regs,
05bba0
             sizeof(env->bndcs_regs));
05bba0
+    memcpy(&xsave->region[XSAVE_OPMASK], env->opmask_regs,
05bba0
+            sizeof env->opmask_regs);
05bba0
+    memcpy(&xsave->region[XSAVE_ZMM_Hi256], env->zmmh_regs,
05bba0
+            sizeof env->zmmh_regs);
05bba0
+#ifdef TARGET_X86_64
05bba0
+    memcpy(&xsave->region[XSAVE_Hi16_ZMM], env->hi16_zmm_regs,
05bba0
+            sizeof env->hi16_zmm_regs);
05bba0
+#endif
05bba0
     r = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XSAVE, xsave);
05bba0
     return r;
05bba0
 }
05bba0
@@ -1354,6 +1365,14 @@ static int kvm_get_xsave(X86CPU *cpu)
05bba0
             sizeof env->bnd_regs);
05bba0
     memcpy(&env->bndcs_regs, &xsave->region[XSAVE_BNDCSR],
05bba0
             sizeof(env->bndcs_regs));
05bba0
+    memcpy(env->opmask_regs, &xsave->region[XSAVE_OPMASK],
05bba0
+            sizeof env->opmask_regs);
05bba0
+    memcpy(env->zmmh_regs, &xsave->region[XSAVE_ZMM_Hi256],
05bba0
+            sizeof env->zmmh_regs);
05bba0
+#ifdef TARGET_X86_64
05bba0
+    memcpy(env->hi16_zmm_regs, &xsave->region[XSAVE_Hi16_ZMM],
05bba0
+            sizeof env->hi16_zmm_regs);
05bba0
+#endif
05bba0
     return 0;
05bba0
 }
05bba0
 
05bba0
diff --git a/target-i386/machine.c b/target-i386/machine.c
05bba0
index b6be606..2c97002 100644
05bba0
--- a/target-i386/machine.c
05bba0
+++ b/target-i386/machine.c
05bba0
@@ -63,6 +63,44 @@ static const VMStateDescription vmstate_ymmh_reg = {
05bba0
 #define VMSTATE_YMMH_REGS_VARS(_field, _state, _n, _v)                         \
05bba0
     VMSTATE_STRUCT_ARRAY(_field, _state, _n, _v, vmstate_ymmh_reg, XMMReg)
05bba0
 
05bba0
+static const VMStateDescription vmstate_zmmh_reg = {
05bba0
+    .name = "zmmh_reg",
05bba0
+    .version_id = 1,
05bba0
+    .minimum_version_id = 1,
05bba0
+    .fields = (VMStateField[]) {
05bba0
+        VMSTATE_UINT64(YMM_Q(0), YMMReg),
05bba0
+        VMSTATE_UINT64(YMM_Q(1), YMMReg),
05bba0
+        VMSTATE_UINT64(YMM_Q(2), YMMReg),
05bba0
+        VMSTATE_UINT64(YMM_Q(3), YMMReg),
05bba0
+        VMSTATE_END_OF_LIST()
05bba0
+    }
05bba0
+};
05bba0
+
05bba0
+#define VMSTATE_ZMMH_REGS_VARS(_field, _state, _n)                             \
05bba0
+    VMSTATE_STRUCT_ARRAY(_field, _state, _n, 0, vmstate_zmmh_reg, YMMReg)
05bba0
+
05bba0
+#ifdef TARGET_X86_64
05bba0
+static const VMStateDescription vmstate_hi16_zmm_reg = {
05bba0
+    .name = "hi16_zmm_reg",
05bba0
+    .version_id = 1,
05bba0
+    .minimum_version_id = 1,
05bba0
+    .fields = (VMStateField[]) {
05bba0
+        VMSTATE_UINT64(ZMM_Q(0), ZMMReg),
05bba0
+        VMSTATE_UINT64(ZMM_Q(1), ZMMReg),
05bba0
+        VMSTATE_UINT64(ZMM_Q(2), ZMMReg),
05bba0
+        VMSTATE_UINT64(ZMM_Q(3), ZMMReg),
05bba0
+        VMSTATE_UINT64(ZMM_Q(4), ZMMReg),
05bba0
+        VMSTATE_UINT64(ZMM_Q(5), ZMMReg),
05bba0
+        VMSTATE_UINT64(ZMM_Q(6), ZMMReg),
05bba0
+        VMSTATE_UINT64(ZMM_Q(7), ZMMReg),
05bba0
+        VMSTATE_END_OF_LIST()
05bba0
+    }
05bba0
+};
05bba0
+
05bba0
+#define VMSTATE_Hi16_ZMM_REGS_VARS(_field, _state, _n)                         \
05bba0
+    VMSTATE_STRUCT_ARRAY(_field, _state, _n, 0, vmstate_hi16_zmm_reg, ZMMReg)
05bba0
+#endif
05bba0
+
05bba0
 static const VMStateDescription vmstate_bnd_regs = {
05bba0
     .name = "bnd_regs",
05bba0
     .version_id = 1,
05bba0
@@ -620,6 +658,52 @@ static const VMStateDescription vmstate_mpx = {
05bba0
     }
05bba0
 };
05bba0
 
05bba0
+static bool avx512_needed(void *opaque)
05bba0
+{
05bba0
+    X86CPU *cpu = opaque;
05bba0
+    CPUX86State *env = &cpu->env;
05bba0
+    unsigned int i;
05bba0
+
05bba0
+    for (i = 0; i < NB_OPMASK_REGS; i++) {
05bba0
+        if (env->opmask_regs[i]) {
05bba0
+            return true;
05bba0
+        }
05bba0
+    }
05bba0
+
05bba0
+    for (i = 0; i < CPU_NB_REGS; i++) {
05bba0
+#define ENV_ZMMH(reg, field) (env->zmmh_regs[reg].YMM_Q(field))
05bba0
+        if (ENV_ZMMH(i, 0) || ENV_ZMMH(i, 1) ||
05bba0
+            ENV_ZMMH(i, 2) || ENV_ZMMH(i, 3)) {
05bba0
+            return true;
05bba0
+        }
05bba0
+#ifdef TARGET_X86_64
05bba0
+#define ENV_Hi16_ZMM(reg, field) (env->hi16_zmm_regs[reg].ZMM_Q(field))
05bba0
+        if (ENV_Hi16_ZMM(i, 0) || ENV_Hi16_ZMM(i, 1) ||
05bba0
+            ENV_Hi16_ZMM(i, 2) || ENV_Hi16_ZMM(i, 3) ||
05bba0
+            ENV_Hi16_ZMM(i, 4) || ENV_Hi16_ZMM(i, 5) ||
05bba0
+            ENV_Hi16_ZMM(i, 6) || ENV_Hi16_ZMM(i, 7)) {
05bba0
+            return true;
05bba0
+        }
05bba0
+#endif
05bba0
+    }
05bba0
+
05bba0
+    return false;
05bba0
+}
05bba0
+
05bba0
+static const VMStateDescription vmstate_avx512 = {
05bba0
+    .name = "cpu/avx512",
05bba0
+    .version_id = 1,
05bba0
+    .minimum_version_id = 1,
05bba0
+    .fields = (VMStateField[]) {
05bba0
+        VMSTATE_UINT64_ARRAY(env.opmask_regs, X86CPU, NB_OPMASK_REGS),
05bba0
+        VMSTATE_ZMMH_REGS_VARS(env.zmmh_regs, X86CPU, CPU_NB_REGS),
05bba0
+#ifdef TARGET_X86_64
05bba0
+        VMSTATE_Hi16_ZMM_REGS_VARS(env.hi16_zmm_regs, X86CPU, CPU_NB_REGS),
05bba0
+#endif
05bba0
+        VMSTATE_END_OF_LIST()
05bba0
+    }
05bba0
+};
05bba0
+
05bba0
 const VMStateDescription vmstate_x86_cpu = {
05bba0
     .name = "cpu",
05bba0
     .version_id = 12,
05bba0
@@ -763,6 +847,9 @@ const VMStateDescription vmstate_x86_cpu = {
05bba0
         } , {
05bba0
             .vmsd = &vmstate_mpx,
05bba0
             .needed = mpx_needed,
05bba0
+        }, {
05bba0
+            .vmsd = &vmstate_avx512,
05bba0
+            .needed = avx512_needed,
05bba0
         } , {
05bba0
             /* empty */
05bba0
         }
05bba0
-- 
05bba0
1.8.3.1
05bba0