thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

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

22c213
From eb0fc0ae2750a0462698d6d21ebb56a4249539f9 Mon Sep 17 00:00:00 2001
22c213
From: Paolo Bonzini <pbonzini@redhat.com>
22c213
Date: Mon, 17 Feb 2020 16:23:11 +0000
22c213
Subject: [PATCH 1/9] target/i386: kvm: initialize feature MSRs very early
22c213
MIME-Version: 1.0
22c213
Content-Type: text/plain; charset=UTF-8
22c213
Content-Transfer-Encoding: 8bit
22c213
22c213
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
22c213
Message-id: <20200217162316.2464-2-pbonzini@redhat.com>
22c213
Patchwork-id: 93899
22c213
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH 1/6] target/i386: kvm: initialize feature MSRs very early
22c213
Bugzilla: 1791648
22c213
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
RH-Acked-by: Maxim Levitsky <mlevitsk@redhat.com>
22c213
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
22c213
Some read-only MSRs affect the behavior of ioctls such as
22c213
KVM_SET_NESTED_STATE.  We can initialize them once and for all
22c213
right after the CPU is realized, since they will never be modified
22c213
by the guest.
22c213
22c213
Reported-by: Qingua Cheng <qcheng@redhat.com>
22c213
Cc: qemu-stable@nongnu.org
22c213
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
22c213
Message-Id: <1579544504-3616-2-git-send-email-pbonzini@redhat.com>
22c213
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
22c213
(cherry picked from commit 420ae1fc51c99abfd03b1c590f55617edd2a2bed)
22c213
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
22c213
---
22c213
 target/i386/kvm.c      | 81 ++++++++++++++++++++++++++++++--------------------
22c213
 target/i386/kvm_i386.h |  1 +
22c213
 2 files changed, 49 insertions(+), 33 deletions(-)
22c213
22c213
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
22c213
index 86d9a1f..f41605b 100644
22c213
--- a/target/i386/kvm.c
22c213
+++ b/target/i386/kvm.c
22c213
@@ -67,6 +67,8 @@
22c213
  * 255 kvm_msr_entry structs */
22c213
 #define MSR_BUF_SIZE 4096
22c213
 
22c213
+static void kvm_init_msrs(X86CPU *cpu);
22c213
+
22c213
 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
22c213
     KVM_CAP_INFO(SET_TSS_ADDR),
22c213
     KVM_CAP_INFO(EXT_CPUID),
22c213
@@ -1842,6 +1844,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
22c213
         has_msr_tsc_aux = false;
22c213
     }
22c213
 
22c213
+    kvm_init_msrs(cpu);
22c213
+
22c213
     r = hyperv_init_vcpu(cpu);
22c213
     if (r) {
22c213
         goto fail;
22c213
@@ -2660,11 +2664,53 @@ static void kvm_msr_entry_add_vmx(X86CPU *cpu, FeatureWordArray f)
22c213
                       VMCS12_MAX_FIELD_INDEX << 1);
22c213
 }
22c213
 
22c213
+static int kvm_buf_set_msrs(X86CPU *cpu)
22c213
+{
22c213
+    int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, cpu->kvm_msr_buf);
22c213
+    if (ret < 0) {
22c213
+        return ret;
22c213
+    }
22c213
+
22c213
+    if (ret < cpu->kvm_msr_buf->nmsrs) {
22c213
+        struct kvm_msr_entry *e = &cpu->kvm_msr_buf->entries[ret];
22c213
+        error_report("error: failed to set MSR 0x%" PRIx32 " to 0x%" PRIx64,
22c213
+                     (uint32_t)e->index, (uint64_t)e->data);
22c213
+    }
22c213
+
22c213
+    assert(ret == cpu->kvm_msr_buf->nmsrs);
22c213
+    return 0;
22c213
+}
22c213
+
22c213
+static void kvm_init_msrs(X86CPU *cpu)
22c213
+{
22c213
+    CPUX86State *env = &cpu->env;
22c213
+
22c213
+    kvm_msr_buf_reset(cpu);
22c213
+    if (has_msr_arch_capabs) {
22c213
+        kvm_msr_entry_add(cpu, MSR_IA32_ARCH_CAPABILITIES,
22c213
+                          env->features[FEAT_ARCH_CAPABILITIES]);
22c213
+    }
22c213
+
22c213
+    if (has_msr_core_capabs) {
22c213
+        kvm_msr_entry_add(cpu, MSR_IA32_CORE_CAPABILITY,
22c213
+                          env->features[FEAT_CORE_CAPABILITY]);
22c213
+    }
22c213
+
22c213
+    /*
22c213
+     * Older kernels do not include VMX MSRs in KVM_GET_MSR_INDEX_LIST, but
22c213
+     * all kernels with MSR features should have them.
22c213
+     */
22c213
+    if (kvm_feature_msrs && cpu_has_vmx(env)) {
22c213
+        kvm_msr_entry_add_vmx(cpu, env->features);
22c213
+    }
22c213
+
22c213
+    assert(kvm_buf_set_msrs(cpu) == 0);
22c213
+}
22c213
+
22c213
 static int kvm_put_msrs(X86CPU *cpu, int level)
22c213
 {
22c213
     CPUX86State *env = &cpu->env;
22c213
     int i;
22c213
-    int ret;
22c213
 
22c213
     kvm_msr_buf_reset(cpu);
22c213
 
22c213
@@ -2722,17 +2768,6 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
22c213
     }
22c213
 #endif
22c213
 
22c213
-    /* If host supports feature MSR, write down. */
22c213
-    if (has_msr_arch_capabs) {
22c213
-        kvm_msr_entry_add(cpu, MSR_IA32_ARCH_CAPABILITIES,
22c213
-                          env->features[FEAT_ARCH_CAPABILITIES]);
22c213
-    }
22c213
-
22c213
-    if (has_msr_core_capabs) {
22c213
-        kvm_msr_entry_add(cpu, MSR_IA32_CORE_CAPABILITY,
22c213
-                          env->features[FEAT_CORE_CAPABILITY]);
22c213
-    }
22c213
-
22c213
     /*
22c213
      * The following MSRs have side effects on the guest or are too heavy
22c213
      * for normal writeback. Limit them to reset or full state updates.
22c213
@@ -2910,14 +2945,6 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
22c213
 
22c213
         /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
22c213
          *       kvm_put_msr_feature_control. */
22c213
-
22c213
-        /*
22c213
-         * Older kernels do not include VMX MSRs in KVM_GET_MSR_INDEX_LIST, but
22c213
-         * all kernels with MSR features should have them.
22c213
-         */
22c213
-        if (kvm_feature_msrs && cpu_has_vmx(env)) {
22c213
-            kvm_msr_entry_add_vmx(cpu, env->features);
22c213
-        }
22c213
     }
22c213
 
22c213
     if (env->mcg_cap) {
22c213
@@ -2933,19 +2960,7 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
22c213
         }
22c213
     }
22c213
 
22c213
-    ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, cpu->kvm_msr_buf);
22c213
-    if (ret < 0) {
22c213
-        return ret;
22c213
-    }
22c213
-
22c213
-    if (ret < cpu->kvm_msr_buf->nmsrs) {
22c213
-        struct kvm_msr_entry *e = &cpu->kvm_msr_buf->entries[ret];
22c213
-        error_report("error: failed to set MSR 0x%" PRIx32 " to 0x%" PRIx64,
22c213
-                     (uint32_t)e->index, (uint64_t)e->data);
22c213
-    }
22c213
-
22c213
-    assert(ret == cpu->kvm_msr_buf->nmsrs);
22c213
-    return 0;
22c213
+    return kvm_buf_set_msrs(cpu);
22c213
 }
22c213
 
22c213
 
22c213
diff --git a/target/i386/kvm_i386.h b/target/i386/kvm_i386.h
22c213
index 06fe06b..d98c6f6 100644
22c213
--- a/target/i386/kvm_i386.h
22c213
+++ b/target/i386/kvm_i386.h
22c213
@@ -66,4 +66,5 @@ bool kvm_enable_x2apic(void);
22c213
 bool kvm_has_x2apic_api(void);
22c213
 
22c213
 bool kvm_hv_vpindex_settable(void);
22c213
+
22c213
 #endif
22c213
-- 
22c213
1.8.3.1
22c213