9ae3a8
From 50e0729e98e9938b338367e29afd6b7abce9bb04 Mon Sep 17 00:00:00 2001
9ae3a8
From: "plai@redhat.com" <plai@redhat.com>
9ae3a8
Date: Mon, 23 Sep 2019 20:40:18 +0200
9ae3a8
Subject: [PATCH 02/12] target-i386: Merge feature filtering/checking functions
9ae3a8
MIME-Version: 1.0
9ae3a8
Content-Type: text/plain; charset=UTF-8
9ae3a8
Content-Transfer-Encoding: 8bit
9ae3a8
9ae3a8
RH-Author: plai@redhat.com
9ae3a8
Message-id: <1569271227-28026-2-git-send-email-plai@redhat.com>
9ae3a8
Patchwork-id: 90853
9ae3a8
O-Subject: [RHEL7.8 qemu-kvm PATCH v6 01/10] target-i386: Merge feature filtering/checking functions
9ae3a8
Bugzilla: 1709971
9ae3a8
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
RH-Acked-by: Bandan Das <bsd@redhat.com>
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
From: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
9ae3a8
Merge filter_features_for_kvm() and kvm_check_features_against_host().
9ae3a8
9ae3a8
Both functions made exactly the same calculations, the only difference
9ae3a8
was that filter_features_for_kvm() changed the bits on cpu->features[],
9ae3a8
and kvm_check_features_against_host() did error reporting.
9ae3a8
9ae3a8
Reviewed-by: Richard Henderson <rth@twiddle.net>
9ae3a8
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
Signed-off-by: Andreas Färber <afaerber@suse.de>
9ae3a8
(cherry picked from commit 51f63aed32314479065207ff2fb28255de4dbda4)
9ae3a8
Signed-off-by: Paul Lai <plai@redhat.com>
9ae3a8
9ae3a8
  unavailable_host_feature() removed due to lack of references.
9ae3a8
  report_unavailable_features() from 51f63aed3 to make things compile.
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 target-i386/cpu.c | 91 ++++++++++++++-----------------------------------------
9ae3a8
 1 file changed, 22 insertions(+), 69 deletions(-)
9ae3a8
9ae3a8
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
9ae3a8
index ca43268..6fb805b 100644
9ae3a8
--- a/target-i386/cpu.c
9ae3a8
+++ b/target-i386/cpu.c
9ae3a8
@@ -1774,11 +1774,11 @@ static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
9ae3a8
 #endif /* CONFIG_KVM */
9ae3a8
 }
9ae3a8
 
9ae3a8
-static int unavailable_host_feature(FeatureWordInfo *f, uint32_t mask)
9ae3a8
+static void report_unavailable_features(FeatureWordInfo *f, uint32_t mask)
9ae3a8
 {
9ae3a8
     int i;
9ae3a8
 
9ae3a8
-    for (i = 0; i < 32; ++i)
9ae3a8
+    for (i = 0; i < 32; ++i) {
9ae3a8
         if (1 << i & mask) {
9ae3a8
             const char *reg = get_register_name_32(f->cpuid_reg);
9ae3a8
             assert(reg);
9ae3a8
@@ -1787,40 +1787,8 @@ static int unavailable_host_feature(FeatureWordInfo *f, uint32_t mask)
9ae3a8
                 f->cpuid_eax, reg,
9ae3a8
                 f->feat_names[i] ? "." : "",
9ae3a8
                 f->feat_names[i] ? f->feat_names[i] : "", i);
9ae3a8
-            break;
9ae3a8
-        }
9ae3a8
-    return 0;
9ae3a8
-}
9ae3a8
-
9ae3a8
-/* Check if all requested cpu flags are making their way to the guest
9ae3a8
- *
9ae3a8
- * Returns 0 if all flags are supported by the host, non-zero otherwise.
9ae3a8
- *
9ae3a8
- * This function may be called only if KVM is enabled.
9ae3a8
- */
9ae3a8
-static int kvm_check_features_against_host(KVMState *s, X86CPU *cpu)
9ae3a8
-{
9ae3a8
-    CPUX86State *env = &cpu->env;
9ae3a8
-    int rv = 0;
9ae3a8
-    FeatureWord w;
9ae3a8
-
9ae3a8
-    assert(kvm_enabled());
9ae3a8
-
9ae3a8
-    for (w = 0; w < FEATURE_WORDS; w++) {
9ae3a8
-        FeatureWordInfo *wi = &feature_word_info[w];
9ae3a8
-        uint32_t guest_feat = env->features[w];
9ae3a8
-        uint32_t host_feat = kvm_arch_get_supported_cpuid(s, wi->cpuid_eax,
9ae3a8
-                                                             wi->cpuid_ecx,
9ae3a8
-                                                             wi->cpuid_reg);
9ae3a8
-        uint32_t mask;
9ae3a8
-        for (mask = 1; mask; mask <<= 1) {
9ae3a8
-            if (guest_feat & mask && !(host_feat & mask)) {
9ae3a8
-                unavailable_host_feature(wi, mask);
9ae3a8
-                rv = 1;
9ae3a8
-            }
9ae3a8
         }
9ae3a8
     }
9ae3a8
-    return rv;
9ae3a8
 }
9ae3a8
 
9ae3a8
 static void x86_cpuid_version_get_family(Object *obj, Visitor *v, void *opaque,
9ae3a8
@@ -2419,12 +2387,21 @@ CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
9ae3a8
     return cpu_list;
9ae3a8
 }
9ae3a8
 
9ae3a8
-#ifdef CONFIG_KVM
9ae3a8
-static void filter_features_for_kvm(X86CPU *cpu)
9ae3a8
+/*
9ae3a8
+ * Filters CPU feature words based on host availability of each feature.
9ae3a8
+ *
9ae3a8
+ * This function may be called only if KVM is enabled.
9ae3a8
+ *
9ae3a8
+ * Returns: 0 if all flags are supported by the host, non-zero otherwise.
9ae3a8
+ */
9ae3a8
+static int filter_features_for_kvm(X86CPU *cpu)
9ae3a8
 {
9ae3a8
     CPUX86State *env = &cpu->env;
9ae3a8
     KVMState *s = kvm_state;
9ae3a8
     FeatureWord w;
9ae3a8
+    int rv = 0;
9ae3a8
+
9ae3a8
+    assert(kvm_enabled());
9ae3a8
 
9ae3a8
     for (w = 0; w < FEATURE_WORDS; w++) {
9ae3a8
         FeatureWordInfo *wi = &feature_word_info[w];
9ae3a8
@@ -2434,9 +2411,16 @@ static void filter_features_for_kvm(X86CPU *cpu)
9ae3a8
         uint32_t requested_features = env->features[w];
9ae3a8
         env->features[w] &= host_feat;
9ae3a8
         cpu->filtered_features[w] = requested_features & ~env->features[w];
9ae3a8
+        if (cpu->filtered_features[w]) {
9ae3a8
+            if (cpu->check_cpuid || cpu->enforce_cpuid) {
9ae3a8
+                report_unavailable_features(wi, cpu->filtered_features[w]);
9ae3a8
+            }
9ae3a8
+            rv = 1;
9ae3a8
+        }
9ae3a8
     }
9ae3a8
+
9ae3a8
+    return rv;
9ae3a8
 }
9ae3a8
-#endif
9ae3a8
 
9ae3a8
 static void cpu_x86_register(X86CPU *cpu, const char *name, Error **errp)
9ae3a8
 {
9ae3a8
@@ -3118,42 +3102,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
9ae3a8
             env->features[w] &= feature_word_info[w].tcg_features;
9ae3a8
         }
9ae3a8
     } else {
9ae3a8
-        KVMState *s = kvm_state;
9ae3a8
-        if ((cpu->check_cpuid || cpu->enforce_cpuid)
9ae3a8
-            && kvm_check_features_against_host(s, cpu) && cpu->enforce_cpuid) {
9ae3a8
+        if (filter_features_for_kvm(cpu) && cpu->enforce_cpuid) {
9ae3a8
             error_setg(&local_err,
9ae3a8
                        "Host's CPU doesn't support requested features");
9ae3a8
             goto out;
9ae3a8
         }
9ae3a8
-#ifdef CONFIG_KVM
9ae3a8
-        filter_features_for_kvm(cpu);
9ae3a8
-#endif
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    /*
9ae3a8
-     * RHEL-only:
9ae3a8
-     *
9ae3a8
-     * The arch-facilities feature flag is deprecated because it was never
9ae3a8
-     * supported upstream.  The upstream property is "arch-capabilities",
9ae3a8
-     * but it was not backported to this QEMU version.  Note that
9ae3a8
-     * arch-capabilities is not required for mitigation of CVE-2017-5715.
9ae3a8
-     *
9ae3a8
-     * In addition to being deprecated, arch-facilities blocks live migration
9ae3a8
-     * because the value of MSR_IA32_ARCH_CAPABILITIES is host-dependent and
9ae3a8
-     * not migration-safe.
9ae3a8
-     */
9ae3a8
-    if (cpu->env.features[FEAT_7_0_EDX] & CPUID_7_0_EDX_ARCH_CAPABILITIES) {
9ae3a8
-        static bool warned = false;
9ae3a8
-        static Error *arch_facilities_blocker;
9ae3a8
-        if (!warned) {
9ae3a8
-            error_setg(&arch_facilities_blocker,
9ae3a8
-                       "The arch-facilities CPU feature is deprecated and "
9ae3a8
-                       "does not support live migration");
9ae3a8
-            migrate_add_blocker(arch_facilities_blocker);
9ae3a8
-            error_report("WARNING: the arch-facilities CPU feature is "
9ae3a8
-                         "deprecated and does not support live migration");
9ae3a8
-            warned = true;
9ae3a8
-        }
9ae3a8
     }
9ae3a8
 
9ae3a8
 #ifndef CONFIG_USER_ONLY
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8