Blame SOURCES/kvm-target-i386-kvm-initialize-feature-MSRs-very-early.patch

016a62
From c4660f9a4e2ffde711294ee7c5959f17735fd863 Mon Sep 17 00:00:00 2001
016a62
From: Paolo Bonzini <pbonzini@redhat.com>
016a62
Date: Thu, 6 Feb 2020 23:51:16 +0000
016a62
Subject: [PATCH 2/2] target/i386: kvm: initialize feature MSRs very early
016a62
016a62
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
016a62
Message-id: <20200206235116.19421-2-pbonzini@redhat.com>
016a62
Patchwork-id: 93733
016a62
O-Subject: [PATCH 1/1] target/i386: kvm: initialize feature MSRs very early
016a62
Bugzilla: 1790308
016a62
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
016a62
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
016a62
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
016a62
016a62
Some read-only MSRs affect the behavior of ioctls such as
016a62
KVM_SET_NESTED_STATE.  We can initialize them once and for all
016a62
right after the CPU is realized, since they will never be modified
016a62
by the guest.
016a62
016a62
Reported-by: Qingua Cheng <qcheng@redhat.com>
016a62
Cc: qemu-stable@nongnu.org
016a62
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
016a62
Message-Id: <1579544504-3616-2-git-send-email-pbonzini@redhat.com>
016a62
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
016a62
(cherry picked from commit 420ae1fc51c99abfd03b1c590f55617edd2a2bed)
016a62
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
016a62
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
016a62
---
016a62
 target/i386/kvm.c      | 81 ++++++++++++++++++++++++++++++--------------------
016a62
 target/i386/kvm_i386.h |  1 +
016a62
 2 files changed, 49 insertions(+), 33 deletions(-)
016a62
016a62
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
016a62
index 92eda8d..e43bcd3 100644
016a62
--- a/target/i386/kvm.c
016a62
+++ b/target/i386/kvm.c
016a62
@@ -65,6 +65,8 @@
016a62
  * 255 kvm_msr_entry structs */
016a62
 #define MSR_BUF_SIZE 4096
016a62
 
016a62
+static void kvm_init_msrs(X86CPU *cpu);
016a62
+
016a62
 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
016a62
     KVM_CAP_INFO(SET_TSS_ADDR),
016a62
     KVM_CAP_INFO(EXT_CPUID),
016a62
@@ -1296,6 +1298,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
016a62
         has_msr_tsc_aux = false;
016a62
     }
016a62
 
016a62
+    kvm_init_msrs(cpu);
016a62
+
016a62
     return 0;
016a62
 
016a62
  fail:
016a62
@@ -2099,11 +2103,53 @@ static void kvm_msr_entry_add_vmx(X86CPU *cpu, FeatureWordArray f)
016a62
                       VMCS12_MAX_FIELD_INDEX << 1);
016a62
 }
016a62
 
016a62
+static int kvm_buf_set_msrs(X86CPU *cpu)
016a62
+{
016a62
+    int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, cpu->kvm_msr_buf);
016a62
+    if (ret < 0) {
016a62
+        return ret;
016a62
+    }
016a62
+
016a62
+    if (ret < cpu->kvm_msr_buf->nmsrs) {
016a62
+        struct kvm_msr_entry *e = &cpu->kvm_msr_buf->entries[ret];
016a62
+        error_report("error: failed to set MSR 0x%" PRIx32 " to 0x%" PRIx64,
016a62
+                     (uint32_t)e->index, (uint64_t)e->data);
016a62
+    }
016a62
+
016a62
+    assert(ret == cpu->kvm_msr_buf->nmsrs);
016a62
+    return 0;
016a62
+}
016a62
+
016a62
+static void kvm_init_msrs(X86CPU *cpu)
016a62
+{
016a62
+    CPUX86State *env = &cpu->env;
016a62
+
016a62
+    kvm_msr_buf_reset(cpu);
016a62
+    if (has_msr_arch_capabs) {
016a62
+        kvm_msr_entry_add(cpu, MSR_IA32_ARCH_CAPABILITIES,
016a62
+                          env->features[FEAT_ARCH_CAPABILITIES]);
016a62
+    }
016a62
+
016a62
+    if (has_msr_core_capabs) {
016a62
+        kvm_msr_entry_add(cpu, MSR_IA32_CORE_CAPABILITY,
016a62
+                          env->features[FEAT_CORE_CAPABILITY]);
016a62
+    }
016a62
+
016a62
+    /*
016a62
+     * Older kernels do not include VMX MSRs in KVM_GET_MSR_INDEX_LIST, but
016a62
+     * all kernels with MSR features should have them.
016a62
+     */
016a62
+    if (kvm_feature_msrs && cpu_has_vmx(env)) {
016a62
+        kvm_msr_entry_add_vmx(cpu, env->features);
016a62
+    }
016a62
+
016a62
+    assert(kvm_buf_set_msrs(cpu) == 0);
016a62
+}
016a62
+
016a62
 static int kvm_put_msrs(X86CPU *cpu, int level)
016a62
 {
016a62
     CPUX86State *env = &cpu->env;
016a62
     int i;
016a62
-    int ret;
016a62
 
016a62
     kvm_msr_buf_reset(cpu);
016a62
 
016a62
@@ -2161,17 +2207,6 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
016a62
     }
016a62
 #endif
016a62
 
016a62
-    /* If host supports feature MSR, write down. */
016a62
-    if (has_msr_arch_capabs) {
016a62
-        kvm_msr_entry_add(cpu, MSR_IA32_ARCH_CAPABILITIES,
016a62
-                          env->features[FEAT_ARCH_CAPABILITIES]);
016a62
-    }
016a62
-
016a62
-    if (has_msr_core_capabs) {
016a62
-        kvm_msr_entry_add(cpu, MSR_IA32_CORE_CAPABILITY,
016a62
-                          env->features[FEAT_CORE_CAPABILITY]);
016a62
-    }
016a62
-
016a62
     /*
016a62
      * The following MSRs have side effects on the guest or are too heavy
016a62
      * for normal writeback. Limit them to reset or full state updates.
016a62
@@ -2331,14 +2366,6 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
016a62
 
016a62
         /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
016a62
          *       kvm_put_msr_feature_control. */
016a62
-
016a62
-        /*
016a62
-         * Older kernels do not include VMX MSRs in KVM_GET_MSR_INDEX_LIST, but
016a62
-         * all kernels with MSR features should have them.
016a62
-         */
016a62
-        if (kvm_feature_msrs && cpu_has_vmx(env)) {
016a62
-            kvm_msr_entry_add_vmx(cpu, env->features);
016a62
-        }
016a62
     }
016a62
 
016a62
     if (env->mcg_cap) {
016a62
@@ -2354,19 +2381,7 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
016a62
         }
016a62
     }
016a62
 
016a62
-    ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, cpu->kvm_msr_buf);
016a62
-    if (ret < 0) {
016a62
-        return ret;
016a62
-    }
016a62
-
016a62
-    if (ret < cpu->kvm_msr_buf->nmsrs) {
016a62
-        struct kvm_msr_entry *e = &cpu->kvm_msr_buf->entries[ret];
016a62
-        error_report("error: failed to set MSR 0x%" PRIx32 " to 0x%" PRIx64,
016a62
-                     (uint32_t)e->index, (uint64_t)e->data);
016a62
-    }
016a62
-
016a62
-    assert(ret == cpu->kvm_msr_buf->nmsrs);
016a62
-    return 0;
016a62
+    return kvm_buf_set_msrs(cpu);
016a62
 }
016a62
 
016a62
 
016a62
diff --git a/target/i386/kvm_i386.h b/target/i386/kvm_i386.h
016a62
index df9bbf3..5748337 100644
016a62
--- a/target/i386/kvm_i386.h
016a62
+++ b/target/i386/kvm_i386.h
016a62
@@ -70,4 +70,5 @@ void kvm_put_apicbase(X86CPU *cpu, uint64_t value);
016a62
 
016a62
 bool kvm_enable_x2apic(void);
016a62
 bool kvm_has_x2apic_api(void);
016a62
+
016a62
 #endif
016a62
-- 
016a62
1.8.3.1
016a62