9ae3a8
From eba382cf6a9a9b0003f10ac3da3e638d6f70d492 Mon Sep 17 00:00:00 2001
9ae3a8
From: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
Date: Wed, 13 Jun 2018 18:50:55 +0200
9ae3a8
Subject: [PATCH 03/17] i386: Define the Virt SSBD MSR and handling of it
9ae3a8
 (CVE-2018-3639)
9ae3a8
MIME-Version: 1.0
9ae3a8
Content-Type: text/plain; charset=UTF-8
9ae3a8
Content-Transfer-Encoding: 8bit
9ae3a8
9ae3a8
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
Message-id: <20180613185056.18066-2-ehabkost@redhat.com>
9ae3a8
Patchwork-id: 80679
9ae3a8
O-Subject: [RHEL-7.5 qemu-kvm PATCH 1/2] i386: Define the Virt SSBD MSR and handling of it (CVE-2018-3639)
9ae3a8
Bugzilla: 1584583
9ae3a8
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
9ae3a8
9ae3a8
"Some AMD processors only support a non-architectural means of enabling
9ae3a8
speculative store bypass disable (SSBD).  To allow a simplified view of
9ae3a8
this to a guest, an architectural definition has been created through a new
9ae3a8
CPUID bit, 0x80000008_EBX[25], and a new MSR, 0xc001011f.  With this, a
9ae3a8
hypervisor can virtualize the existence of this definition and provide an
9ae3a8
architectural method for using SSBD to a guest.
9ae3a8
9ae3a8
Add the new CPUID feature, the new MSR and update the existing SSBD
9ae3a8
support to use this MSR when present." (from x86/speculation: Add virtualized
9ae3a8
speculative store bypass disable support in Linux).
9ae3a8
9ae3a8
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
9ae3a8
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
9ae3a8
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
9ae3a8
Message-Id: <20180521215424.13520-4-berrange@redhat.com>
9ae3a8
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
(cherry picked from commit cfeea0c021db6234c154dbc723730e81553924ff)
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
    target-i386/kvm.c (MSR code)
9ae3a8
    target-i386/machine.c (trivial change from
9ae3a8
        VMStateDescription.needed to VMStateSubsection.needed)
9ae3a8
9ae3a8
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
---
9ae3a8
 target-i386/cpu.h     |  2 ++
9ae3a8
 target-i386/kvm.c     | 17 +++++++++++++++--
9ae3a8
 target-i386/machine.c | 21 +++++++++++++++++++++
9ae3a8
 3 files changed, 38 insertions(+), 2 deletions(-)
9ae3a8
9ae3a8
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
9ae3a8
index c72b545..debb0e5 100644
9ae3a8
--- a/target-i386/cpu.h
9ae3a8
+++ b/target-i386/cpu.h
9ae3a8
@@ -305,6 +305,7 @@
9ae3a8
 #define MSR_IA32_APICBASE_BASE          (0xfffff<<12)
9ae3a8
 #define MSR_TSC_ADJUST                  0x0000003b
9ae3a8
 #define MSR_IA32_SPEC_CTRL              0x48
9ae3a8
+#define MSR_VIRT_SSBD                   0xc001011f
9ae3a8
 #define MSR_IA32_TSCDEADLINE            0x6e0
9ae3a8
 
9ae3a8
 #define MSR_P6_PERFCTR0                 0xc1
9ae3a8
@@ -1052,6 +1053,7 @@ typedef struct CPUX86State {
9ae3a8
     uint32_t pkru;
9ae3a8
 
9ae3a8
     uint64_t spec_ctrl;
9ae3a8
+    uint64_t virt_ssbd;
9ae3a8
 
9ae3a8
     TPRAccess tpr_access_type;
9ae3a8
 } CPUX86State;
9ae3a8
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
9ae3a8
index a1a49d8..35a9cf4 100644
9ae3a8
--- a/target-i386/kvm.c
9ae3a8
+++ b/target-i386/kvm.c
9ae3a8
@@ -78,6 +78,7 @@ static bool has_msr_hv_tsc;
9ae3a8
 static bool has_msr_mtrr;
9ae3a8
 static bool has_msr_xss;
9ae3a8
 static bool has_msr_spec_ctrl;
9ae3a8
+static bool has_msr_virt_ssbd;
9ae3a8
 
9ae3a8
 static bool has_msr_architectural_pmu;
9ae3a8
 static uint32_t num_architectural_pmu_counters;
9ae3a8
@@ -805,6 +806,10 @@ static int kvm_get_supported_msrs(KVMState *s)
9ae3a8
                     has_msr_spec_ctrl = true;
9ae3a8
                     continue;
9ae3a8
                 }
9ae3a8
+                if (kvm_msr_list->indices[i] == MSR_VIRT_SSBD) {
9ae3a8
+                    has_msr_virt_ssbd = true;
9ae3a8
+                    continue;
9ae3a8
+                }
9ae3a8
             }
9ae3a8
         }
9ae3a8
 
9ae3a8
@@ -1217,6 +1222,10 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
9ae3a8
     if (has_msr_spec_ctrl) {
9ae3a8
         kvm_msr_entry_set(&msrs[n++], MSR_IA32_SPEC_CTRL, env->spec_ctrl);
9ae3a8
     }
9ae3a8
+    if (has_msr_virt_ssbd) {
9ae3a8
+        kvm_msr_entry_set(&msrs[n++], MSR_VIRT_SSBD, env->virt_ssbd);
9ae3a8
+    }
9ae3a8
+
9ae3a8
 #ifdef TARGET_X86_64
9ae3a8
     if (lm_capable_kernel) {
9ae3a8
         kvm_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar);
9ae3a8
@@ -1577,8 +1586,9 @@ static int kvm_get_msrs(X86CPU *cpu)
9ae3a8
     if (has_msr_spec_ctrl) {
9ae3a8
         msrs[n++].index = MSR_IA32_SPEC_CTRL;
9ae3a8
     }
9ae3a8
-
9ae3a8
-
9ae3a8
+    if (has_msr_virt_ssbd) {
9ae3a8
+        msrs[n++].index = MSR_VIRT_SSBD;
9ae3a8
+    }
9ae3a8
     if (!env->tsc_valid) {
9ae3a8
         msrs[n++].index = MSR_IA32_TSC;
9ae3a8
         env->tsc_valid = !runstate_is_running();
9ae3a8
@@ -1822,6 +1832,9 @@ static int kvm_get_msrs(X86CPU *cpu)
9ae3a8
         case MSR_IA32_SPEC_CTRL:
9ae3a8
             env->spec_ctrl = msrs[i].data;
9ae3a8
             break;
9ae3a8
+        case MSR_VIRT_SSBD:
9ae3a8
+            env->virt_ssbd = msrs[i].data;
9ae3a8
+            break;
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
diff --git a/target-i386/machine.c b/target-i386/machine.c
9ae3a8
index d883c86..507ab1a 100644
9ae3a8
--- a/target-i386/machine.c
9ae3a8
+++ b/target-i386/machine.c
9ae3a8
@@ -760,6 +760,24 @@ static const VMStateDescription vmstate_spec_ctrl = {
9ae3a8
     }
9ae3a8
 };
9ae3a8
 
9ae3a8
+static bool virt_ssbd_needed(void *opaque)
9ae3a8
+{
9ae3a8
+    X86CPU *cpu = opaque;
9ae3a8
+    CPUX86State *env = &cpu->env;
9ae3a8
+
9ae3a8
+    return env->virt_ssbd != 0;
9ae3a8
+}
9ae3a8
+
9ae3a8
+static const VMStateDescription vmstate_msr_virt_ssbd = {
9ae3a8
+    .name = "cpu/virt_ssbd",
9ae3a8
+    .version_id = 1,
9ae3a8
+    .minimum_version_id = 1,
9ae3a8
+    .fields = (VMStateField[]){
9ae3a8
+        VMSTATE_UINT64(env.virt_ssbd, X86CPU),
9ae3a8
+        VMSTATE_END_OF_LIST()
9ae3a8
+    }
9ae3a8
+};
9ae3a8
+
9ae3a8
 const VMStateDescription vmstate_x86_cpu = {
9ae3a8
     .name = "cpu",
9ae3a8
     .version_id = 12,
9ae3a8
@@ -917,6 +935,9 @@ const VMStateDescription vmstate_x86_cpu = {
9ae3a8
         }, {
9ae3a8
             .vmsd = &vmstate_spec_ctrl,
9ae3a8
             .needed = spec_ctrl_needed,
9ae3a8
+        }, {
9ae3a8
+            .vmsd = &vmstate_msr_virt_ssbd,
9ae3a8
+            .needed = virt_ssbd_needed,
9ae3a8
         } , {
9ae3a8
             /* empty */
9ae3a8
         }
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8