05bba0
From 0c9168510f4ddbdec3b0a88095db2e8f3f0aeef7 Mon Sep 17 00:00:00 2001
05bba0
From: Eduardo Habkost <ehabkost@redhat.com>
05bba0
Date: Thu, 25 Jun 2015 19:31:23 +0200
05bba0
Subject: [PATCH 03/10] target-i386: Intel MPX
05bba0
05bba0
Message-id: <1435260689-9556-3-git-send-email-ehabkost@redhat.com>
05bba0
Patchwork-id: 66500
05bba0
O-Subject: [RHEL-7.2 qemu-kvm PATCH 2/8] target-i386: Intel MPX
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
From: Liu Jinsong <jinsong.liu@intel.com>
05bba0
05bba0
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1233350
05bba0
05bba0
Add some MPX related definiation, and hardcode sizes and offsets
05bba0
of xsave features 3 and 4. It also add corresponding part to
05bba0
kvm_get/put_xsave, and vmstate.
05bba0
05bba0
Signed-off-by: Liu Jinsong <jinsong.liu@intel.com>
05bba0
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
05bba0
(cherry picked from commit 79e9ebebbf2a00c46fcedb6dc7dd5e12bbd30216)
05bba0
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
05bba0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
05bba0
05bba0
Conflicts:
05bba0
	target-i386/kvm.c
05bba0
	target-i386/machine.c
05bba0
---
05bba0
 target-i386/cpu.c     |  4 ++++
05bba0
 target-i386/cpu.h     | 25 ++++++++++++++++++++++---
05bba0
 target-i386/kvm.c     | 24 ++++++++++++++++++++++++
05bba0
 target-i386/machine.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
05bba0
 4 files changed, 101 insertions(+), 3 deletions(-)
05bba0
05bba0
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
05bba0
index 0fd100f..a9e80d9 100644
05bba0
--- a/target-i386/cpu.c
05bba0
+++ b/target-i386/cpu.c
05bba0
@@ -224,6 +224,10 @@ typedef struct ExtSaveArea {
05bba0
 static const ExtSaveArea ext_save_areas[] = {
05bba0
     [2] = { .feature = FEAT_1_ECX, .bits = CPUID_EXT_AVX,
05bba0
             .offset = 0x240, .size = 0x100 },
05bba0
+    [3] = { .feature = FEAT_7_0_EBX, .bits = CPUID_7_0_EBX_MPX,
05bba0
+            .offset = 0x3c0, .size = 0x40  },
05bba0
+    [4] = { .feature = FEAT_7_0_EBX, .bits = CPUID_7_0_EBX_MPX,
05bba0
+            .offset = 0x400, .size = 0x10  },
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 e9edd3d..7ebdbb3 100644
05bba0
--- a/target-i386/cpu.h
05bba0
+++ b/target-i386/cpu.h
05bba0
@@ -383,9 +383,14 @@
05bba0
 
05bba0
 #define MSR_VM_HSAVE_PA                 0xc0010117
05bba0
 
05bba0
-#define XSTATE_FP                       1
05bba0
-#define XSTATE_SSE                      2
05bba0
-#define XSTATE_YMM                      4
05bba0
+#define MSR_IA32_BNDCFGS                0x00000d90
05bba0
+
05bba0
+#define XSTATE_FP                       (1ULL << 0)
05bba0
+#define XSTATE_SSE                      (1ULL << 1)
05bba0
+#define XSTATE_YMM                      (1ULL << 2)
05bba0
+#define XSTATE_BNDREGS                  (1ULL << 3)
05bba0
+#define XSTATE_BNDCSR                   (1ULL << 4)
05bba0
+
05bba0
 
05bba0
 /* CPUID feature words */
05bba0
 typedef enum FeatureWord {
05bba0
@@ -548,6 +553,7 @@ typedef uint32_t FeatureWordArray[FEATURE_WORDS];
05bba0
 #define CPUID_7_0_EBX_ERMS     (1 << 9)
05bba0
 #define CPUID_7_0_EBX_INVPCID  (1 << 10)
05bba0
 #define CPUID_7_0_EBX_RTM      (1 << 11)
05bba0
+#define CPUID_7_0_EBX_MPX      (1 << 14)
05bba0
 #define CPUID_7_0_EBX_RDSEED   (1 << 18)
05bba0
 #define CPUID_7_0_EBX_ADX      (1 << 19)
05bba0
 #define CPUID_7_0_EBX_SMAP     (1 << 20)
05bba0
@@ -698,6 +704,16 @@ typedef union {
05bba0
     uint64_t q;
05bba0
 } MMXReg;
05bba0
 
05bba0
+typedef struct BNDReg {
05bba0
+    uint64_t lb;
05bba0
+    uint64_t ub;
05bba0
+} BNDReg;
05bba0
+
05bba0
+typedef struct BNDCSReg {
05bba0
+    uint64_t cfgu;
05bba0
+    uint64_t sts;
05bba0
+} BNDCSReg;
05bba0
+
05bba0
 #ifdef HOST_WORDS_BIGENDIAN
05bba0
 #define XMM_B(n) _b[15 - (n)]
05bba0
 #define XMM_W(n) _w[7 - (n)]
05bba0
@@ -918,6 +934,9 @@ typedef struct CPUX86State {
05bba0
 
05bba0
     uint64_t xstate_bv;
05bba0
     XMMReg ymmh_regs[CPU_NB_REGS];
05bba0
+    BNDReg bnd_regs[4];
05bba0
+    BNDCSReg bndcs_regs;
05bba0
+    uint64_t msr_bndcfgs;
05bba0
 
05bba0
     uint64_t xcr0;
05bba0
 
05bba0
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
05bba0
index 97ae345..6d9aa63 100644
05bba0
--- a/target-i386/kvm.c
05bba0
+++ b/target-i386/kvm.c
05bba0
@@ -68,6 +68,7 @@ static bool has_msr_tsc_deadline;
05bba0
 static bool has_msr_async_pf_en;
05bba0
 static bool has_msr_pv_eoi_en;
05bba0
 static bool has_msr_misc_enable;
05bba0
+static bool has_msr_bndcfgs;
05bba0
 static bool has_msr_kvm_steal_time;
05bba0
 static int lm_capable_kernel;
05bba0
 static bool has_msr_hv_hypercall;
05bba0
@@ -785,6 +786,10 @@ static int kvm_get_supported_msrs(KVMState *s)
05bba0
                     has_msr_misc_enable = true;
05bba0
                     continue;
05bba0
                 }
05bba0
+                if (kvm_msr_list->indices[i] == MSR_IA32_BNDCFGS) {
05bba0
+                    has_msr_bndcfgs = true;
05bba0
+                    continue;
05bba0
+                }
05bba0
             }
05bba0
         }
05bba0
 
05bba0
@@ -988,6 +993,8 @@ static int kvm_put_fpu(X86CPU *cpu)
05bba0
 #define XSAVE_XMM_SPACE   40
05bba0
 #define XSAVE_XSTATE_BV   128
05bba0
 #define XSAVE_YMMH_SPACE  144
05bba0
+#define XSAVE_BNDREGS     240
05bba0
+#define XSAVE_BNDCSR      256
05bba0
 
05bba0
 static int kvm_put_xsave(X86CPU *cpu)
05bba0
 {
05bba0
@@ -1020,6 +1027,10 @@ static int kvm_put_xsave(X86CPU *cpu)
05bba0
     *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV] = env->xstate_bv;
05bba0
     memcpy(&xsave->region[XSAVE_YMMH_SPACE], env->ymmh_regs,
05bba0
             sizeof env->ymmh_regs);
05bba0
+    memcpy(&xsave->region[XSAVE_BNDREGS], env->bnd_regs,
05bba0
+            sizeof env->bnd_regs);
05bba0
+    memcpy(&xsave->region[XSAVE_BNDCSR], &env->bndcs_regs,
05bba0
+            sizeof(env->bndcs_regs));
05bba0
     r = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XSAVE, xsave);
05bba0
     return r;
05bba0
 }
05bba0
@@ -1255,6 +1266,9 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
05bba0
                                   MSR_MTRRphysMask(i), env->mtrr_var[i].mask);
05bba0
             }
05bba0
         }
05bba0
+        if (has_msr_bndcfgs) {
05bba0
+            kvm_msr_entry_set(&msrs[n++], MSR_IA32_BNDCFGS, env->msr_bndcfgs);
05bba0
+        }
05bba0
     }
05bba0
     if (env->mcg_cap) {
05bba0
         int i;
05bba0
@@ -1336,6 +1350,10 @@ static int kvm_get_xsave(X86CPU *cpu)
05bba0
     env->xstate_bv = *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV];
05bba0
     memcpy(env->ymmh_regs, &xsave->region[XSAVE_YMMH_SPACE],
05bba0
             sizeof env->ymmh_regs);
05bba0
+    memcpy(env->bnd_regs, &xsave->region[XSAVE_BNDREGS],
05bba0
+            sizeof env->bnd_regs);
05bba0
+    memcpy(&env->bndcs_regs, &xsave->region[XSAVE_BNDCSR],
05bba0
+            sizeof(env->bndcs_regs));
05bba0
     return 0;
05bba0
 }
05bba0
 
05bba0
@@ -1479,6 +1497,9 @@ static int kvm_get_msrs(X86CPU *cpu)
05bba0
     if (has_msr_misc_enable) {
05bba0
         msrs[n++].index = MSR_IA32_MISC_ENABLE;
05bba0
     }
05bba0
+    if (has_msr_bndcfgs) {
05bba0
+        msrs[n++].index = MSR_IA32_BNDCFGS;
05bba0
+    }
05bba0
 
05bba0
     if (!env->tsc_valid) {
05bba0
         msrs[n++].index = MSR_IA32_TSC;
05bba0
@@ -1620,6 +1641,9 @@ static int kvm_get_msrs(X86CPU *cpu)
05bba0
         case MSR_IA32_MISC_ENABLE:
05bba0
             env->msr_ia32_misc_enable = msrs[i].data;
05bba0
             break;
05bba0
+        case MSR_IA32_BNDCFGS:
05bba0
+            env->msr_bndcfgs = msrs[i].data;
05bba0
+            break;
05bba0
         default:
05bba0
             if (msrs[i].index >= MSR_MC0_CTL &&
05bba0
                 msrs[i].index < MSR_MC0_CTL + (env->mcg_cap & 0xff) * 4) {
05bba0
diff --git a/target-i386/machine.c b/target-i386/machine.c
05bba0
index 4c1ead4..b6be606 100644
05bba0
--- a/target-i386/machine.c
05bba0
+++ b/target-i386/machine.c
05bba0
@@ -63,6 +63,21 @@ 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_bnd_regs = {
05bba0
+    .name = "bnd_regs",
05bba0
+    .version_id = 1,
05bba0
+    .minimum_version_id = 1,
05bba0
+    .minimum_version_id_old = 1,
05bba0
+    .fields      = (VMStateField[]) {
05bba0
+        VMSTATE_UINT64(lb, BNDReg),
05bba0
+        VMSTATE_UINT64(ub, BNDReg),
05bba0
+        VMSTATE_END_OF_LIST()
05bba0
+    }
05bba0
+};
05bba0
+
05bba0
+#define VMSTATE_BND_REGS(_field, _state, _n)          \
05bba0
+    VMSTATE_STRUCT_ARRAY(_field, _state, _n, 0, vmstate_bnd_regs, BNDReg)
05bba0
+
05bba0
 static const VMStateDescription vmstate_mtrr_var = {
05bba0
     .name = "mtrr_var",
05bba0
     .version_id = 1,
05bba0
@@ -572,6 +587,39 @@ static const VMStateDescription vmstate_msr_hyperv_time = {
05bba0
     }
05bba0
 };
05bba0
 
05bba0
+static bool mpx_needed(void *opaque)
05bba0
+{
05bba0
+    X86CPU *cpu = opaque;
05bba0
+    CPUX86State *env = &cpu->env;
05bba0
+    unsigned int i;
05bba0
+
05bba0
+    for (i = 0; i < 4; i++) {
05bba0
+        if (env->bnd_regs[i].lb || env->bnd_regs[i].ub) {
05bba0
+            return true;
05bba0
+        }
05bba0
+    }
05bba0
+
05bba0
+    if (env->bndcs_regs.cfgu || env->bndcs_regs.sts) {
05bba0
+        return true;
05bba0
+    }
05bba0
+
05bba0
+    return !!env->msr_bndcfgs;
05bba0
+}
05bba0
+
05bba0
+static const VMStateDescription vmstate_mpx = {
05bba0
+    .name = "cpu/mpx",
05bba0
+    .version_id = 1,
05bba0
+    .minimum_version_id = 1,
05bba0
+    .minimum_version_id_old = 1,
05bba0
+    .fields      = (VMStateField[]) {
05bba0
+        VMSTATE_BND_REGS(env.bnd_regs, X86CPU, 4),
05bba0
+        VMSTATE_UINT64(env.bndcs_regs.cfgu, X86CPU),
05bba0
+        VMSTATE_UINT64(env.bndcs_regs.sts, X86CPU),
05bba0
+        VMSTATE_UINT64(env.msr_bndcfgs, X86CPU),
05bba0
+        VMSTATE_END_OF_LIST()
05bba0
+    }
05bba0
+};
05bba0
+
05bba0
 const VMStateDescription vmstate_x86_cpu = {
05bba0
     .name = "cpu",
05bba0
     .version_id = 12,
05bba0
@@ -713,6 +761,9 @@ const VMStateDescription vmstate_x86_cpu = {
05bba0
             .vmsd = &vmstate_msr_hyperv_time,
05bba0
             .needed = hyperv_time_enable_needed,
05bba0
         } , {
05bba0
+            .vmsd = &vmstate_mpx,
05bba0
+            .needed = mpx_needed,
05bba0
+        } , {
05bba0
             /* empty */
05bba0
         }
05bba0
     }
05bba0
-- 
05bba0
1.8.3.1
05bba0