Blame SOURCES/kvm-target-i386-add-support-for-MSR_IA32_TSX_CTRL.patch

c461a1
From 73fac9c9beb00cc462eaae8589b4b2261142a8b2 Mon Sep 17 00:00:00 2001
2ec96d
From: Eduardo Habkost <ehabkost@redhat.com>
c461a1
Date: Wed, 4 Dec 2019 01:48:29 +0100
2ec96d
Subject: [PATCH 2/2] target/i386: add support for MSR_IA32_TSX_CTRL
2ec96d
2ec96d
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
c461a1
Message-id: <20191204014829.608318-3-ehabkost@redhat.com>
c461a1
Patchwork-id: 92854
c461a1
O-Subject: [RHEL-7.8 qemu-kvm PATCH 2/2] target/i386: add support for MSR_IA32_TSX_CTRL
c461a1
Bugzilla: 1771961
2ec96d
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
2ec96d
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
2ec96d
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
2ec96d
2ec96d
From: Paolo Bonzini <pbonzini@redhat.com>
2ec96d
2ec96d
The MSR_IA32_TSX_CTRL MSR can be used to hide TSX (also known as the
2ec96d
Trusty Side-channel Extension).  By virtualizing the MSR, KVM guests
2ec96d
can disable TSX and avoid paying the price of mitigating TSX-based
2ec96d
attacks on microarchitectural side channels.
2ec96d
2ec96d
Backport notes:
2ec96d
* MSR code had to be rewritten
2ec96d
* .needed is inside VMStateSubsection
2ec96d
2ec96d
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
2ec96d
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2ec96d
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
2ec96d
(cherry picked from commit 2a9758c51e2c2d13fc3845c3d603c11df98b8823)
2ec96d
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
2ec96d
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
2ec96d
---
2ec96d
 target-i386/cpu.c     |  2 +-
2ec96d
 target-i386/cpu.h     |  5 +++++
2ec96d
 target-i386/kvm.c     | 14 ++++++++++++++
2ec96d
 target-i386/machine.c | 21 +++++++++++++++++++++
2ec96d
 4 files changed, 41 insertions(+), 1 deletion(-)
2ec96d
2ec96d
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
c461a1
index 120df73..57f5364 100644
2ec96d
--- a/target-i386/cpu.c
2ec96d
+++ b/target-i386/cpu.c
c461a1
@@ -211,7 +211,7 @@ static const char *cpuid_apm_edx_feature_name[] = {
2ec96d
 
2ec96d
 static const char *cpuid_arch_capabilities_feature_name[] = {
2ec96d
     "rdctl-no", "ibrs-all", "rsba", "skip-l1dfl-vmentry",
c461a1
-    "ssb-no", "mds-no", NULL, NULL,
c461a1
+    "ssb-no", "mds-no", NULL, "tsx-ctrl",
2ec96d
     "taa-no", NULL, NULL, NULL,
2ec96d
     NULL, NULL, NULL, NULL,
2ec96d
     NULL, NULL, NULL, NULL,
2ec96d
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
c461a1
index 8f73af7..c9bcdd5 100644
2ec96d
--- a/target-i386/cpu.h
2ec96d
+++ b/target-i386/cpu.h
2ec96d
@@ -307,7 +307,11 @@
2ec96d
 #define MSR_IA32_SPEC_CTRL              0x48
2ec96d
 #define MSR_VIRT_SSBD                   0xc001011f
2ec96d
 #define MSR_IA32_PRED_CMD               0x49
2ec96d
+
2ec96d
 #define MSR_IA32_ARCH_CAPABILITIES      0x10a
2ec96d
+#define ARCH_CAP_TSX_CTRL_MSR		(1<<7)
2ec96d
+
2ec96d
+#define MSR_IA32_TSX_CTRL		0x122
2ec96d
 #define MSR_IA32_TSCDEADLINE            0x6e0
2ec96d
 
2ec96d
 #define MSR_P6_PERFCTR0                 0xc1
c461a1
@@ -1067,6 +1071,7 @@ typedef struct CPUX86State {
2ec96d
     uint64_t xss;
2ec96d
 
2ec96d
     uint32_t pkru;
2ec96d
+    uint32_t tsx_ctrl;
2ec96d
 
2ec96d
     uint64_t spec_ctrl;
2ec96d
     uint64_t virt_ssbd;
2ec96d
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
c461a1
index c79b0ea..7df2b28 100644
2ec96d
--- a/target-i386/kvm.c
2ec96d
+++ b/target-i386/kvm.c
c461a1
@@ -80,6 +80,7 @@ static bool has_msr_hv_tsc;
2ec96d
 static bool has_msr_mtrr;
2ec96d
 static bool has_msr_xss;
2ec96d
 static bool has_msr_spec_ctrl;
2ec96d
+static bool has_msr_tsx_ctrl;
2ec96d
 static bool has_msr_virt_ssbd;
2ec96d
 static bool has_msr_arch_capabs;
2ec96d
 
c461a1
@@ -908,6 +909,10 @@ static int kvm_get_supported_msrs(KVMState *s)
2ec96d
                     has_msr_spec_ctrl = true;
2ec96d
                     continue;
2ec96d
                 }
2ec96d
+                if (kvm_msr_list->indices[i] == MSR_IA32_TSX_CTRL) {
2ec96d
+                    has_msr_tsx_ctrl = true;
2ec96d
+                    continue;
2ec96d
+                }
2ec96d
                 if (kvm_msr_list->indices[i] == MSR_VIRT_SSBD) {
2ec96d
                     has_msr_virt_ssbd = true;
2ec96d
                     continue;
c461a1
@@ -1330,6 +1335,9 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
2ec96d
     if (has_msr_spec_ctrl) {
2ec96d
         kvm_msr_entry_set(&msrs[n++], MSR_IA32_SPEC_CTRL, env->spec_ctrl);
2ec96d
     }
2ec96d
+    if (has_msr_tsx_ctrl) {
2ec96d
+        kvm_msr_entry_set(&msrs[n++], MSR_IA32_TSX_CTRL, env->tsx_ctrl);
2ec96d
+    }
2ec96d
     if (has_msr_virt_ssbd) {
2ec96d
         kvm_msr_entry_set(&msrs[n++], MSR_VIRT_SSBD, env->virt_ssbd);
2ec96d
     }
c461a1
@@ -1699,6 +1707,9 @@ static int kvm_get_msrs(X86CPU *cpu)
2ec96d
     if (has_msr_spec_ctrl) {
2ec96d
         msrs[n++].index = MSR_IA32_SPEC_CTRL;
2ec96d
     }
2ec96d
+    if (has_msr_tsx_ctrl) {
2ec96d
+        msrs[n++].index = MSR_IA32_TSX_CTRL;
2ec96d
+    }
2ec96d
     if (has_msr_virt_ssbd) {
2ec96d
         msrs[n++].index = MSR_VIRT_SSBD;
2ec96d
     }
c461a1
@@ -1945,6 +1956,9 @@ static int kvm_get_msrs(X86CPU *cpu)
2ec96d
         case MSR_IA32_SPEC_CTRL:
2ec96d
             env->spec_ctrl = msrs[i].data;
2ec96d
             break;
2ec96d
+        case MSR_IA32_TSX_CTRL:
2ec96d
+            env->tsx_ctrl = msrs[i].data;
2ec96d
+            break;
2ec96d
         case MSR_VIRT_SSBD:
2ec96d
             env->virt_ssbd = msrs[i].data;
2ec96d
             break;
2ec96d
diff --git a/target-i386/machine.c b/target-i386/machine.c
c461a1
index cd2cf6f..892c8f4 100644
2ec96d
--- a/target-i386/machine.c
2ec96d
+++ b/target-i386/machine.c
2ec96d
@@ -778,6 +778,24 @@ static const VMStateDescription vmstate_msr_virt_ssbd = {
2ec96d
     }
2ec96d
 };
2ec96d
 
2ec96d
+static bool msr_tsx_ctrl_needed(void *opaque)
2ec96d
+{
2ec96d
+    X86CPU *cpu = opaque;
2ec96d
+    CPUX86State *env = &cpu->env;
2ec96d
+
2ec96d
+    return env->features[FEAT_ARCH_CAPABILITIES] & ARCH_CAP_TSX_CTRL_MSR;
2ec96d
+}
2ec96d
+
2ec96d
+static const VMStateDescription vmstate_msr_tsx_ctrl = {
2ec96d
+    .name = "cpu/msr_tsx_ctrl",
2ec96d
+    .version_id = 1,
2ec96d
+    .minimum_version_id = 1,
2ec96d
+    .fields = (VMStateField[]) {
2ec96d
+        VMSTATE_UINT32(env.tsx_ctrl, X86CPU),
2ec96d
+        VMSTATE_END_OF_LIST()
2ec96d
+    }
2ec96d
+};
2ec96d
+
c461a1
 VMStateDescription vmstate_x86_cpu = {
2ec96d
     .name = "cpu",
2ec96d
     .version_id = 12,
c461a1
@@ -938,6 +956,9 @@ VMStateDescription vmstate_x86_cpu = {
2ec96d
         }, {
2ec96d
             .vmsd = &vmstate_msr_virt_ssbd,
2ec96d
             .needed = virt_ssbd_needed,
2ec96d
+        }, {
2ec96d
+            .vmsd = &vmstate_msr_tsx_ctrl,
2ec96d
+            .needed = msr_tsx_ctrl_needed,
2ec96d
         } , {
2ec96d
             /* empty */
2ec96d
         }
2ec96d
-- 
2ec96d
1.8.3.1
2ec96d