9ae3a8
From 6d78a7906b69054a4798b1b68548f59fec9e5da8 Mon Sep 17 00:00:00 2001
9ae3a8
From: Vadim Rozenfeld <vrozenfe@redhat.com>
9ae3a8
Date: Mon, 3 Mar 2014 12:09:13 +0100
9ae3a8
Subject: [PATCH 01/12] target-i386: Move hyperv_* static globals to X86CPU
9ae3a8
MIME-Version: 1.0
9ae3a8
Content-Type: text/plain; charset=UTF-8
9ae3a8
Content-Transfer-Encoding: 8bit
9ae3a8
9ae3a8
RH-Author: Vadim Rozenfeld <vrozenfe@redhat.com>
9ae3a8
Message-id: <1393848564-10511-2-git-send-email-vrozenfe@redhat.com>
9ae3a8
Patchwork-id: 57957
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm v4 PATCH 01/12] target-i386: Move hyperv_* static globals to X86CPU
9ae3a8
Bugzilla: 1004773
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
9ae3a8
RH-Acked-by: Juan Quintela <quintela@redhat.com>
9ae3a8
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
9ae3a8
From: Igor Mammedov <imammedo@redhat.com>
9ae3a8
9ae3a8
  since hyperv_* helper functions are used only in target-i386/kvm.c
9ae3a8
  move them there as static helpers
9ae3a8
9ae3a8
Requested-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
9ae3a8
Signed-off-by: Andreas Färber <afaerber@suse.de>
9ae3a8
(cherry picked from commit 92067bf4bfa144ea3967a9951808f5e587bdab18)
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	target-i386/cpu.h
9ae3a8
	target-i386/kvm.c
9ae3a8
---
9ae3a8
 target-i386/Makefile.objs |  2 +-
9ae3a8
 target-i386/cpu-qom.h     |  4 +++
9ae3a8
 target-i386/cpu.c         | 16 ++++++++----
9ae3a8
 target-i386/cpu.h         |  4 +++
9ae3a8
 target-i386/hyperv.c      | 64 -----------------------------------------------
9ae3a8
 target-i386/hyperv.h      | 45 ---------------------------------
9ae3a8
 target-i386/kvm.c         | 36 ++++++++++++++++++--------
9ae3a8
 7 files changed, 46 insertions(+), 125 deletions(-)
9ae3a8
 delete mode 100644 target-i386/hyperv.c
9ae3a8
 delete mode 100644 target-i386/hyperv.h
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 target-i386/Makefile.objs |    2 +-
9ae3a8
 target-i386/cpu-qom.h     |    4 +++
9ae3a8
 target-i386/cpu.c         |   16 +++++++---
9ae3a8
 target-i386/cpu.h         |    4 +++
9ae3a8
 target-i386/hyperv.c      |   64 ---------------------------------------------
9ae3a8
 target-i386/hyperv.h      |   45 -------------------------------
9ae3a8
 target-i386/kvm.c         |   36 ++++++++++++++++++-------
9ae3a8
 7 files changed, 46 insertions(+), 125 deletions(-)
9ae3a8
 delete mode 100644 target-i386/hyperv.c
9ae3a8
 delete mode 100644 target-i386/hyperv.h
9ae3a8
9ae3a8
diff --git a/target-i386/Makefile.objs b/target-i386/Makefile.objs
9ae3a8
index c1d4f05..887dca7 100644
9ae3a8
--- a/target-i386/Makefile.objs
9ae3a8
+++ b/target-i386/Makefile.objs
9ae3a8
@@ -2,7 +2,7 @@ obj-y += translate.o helper.o cpu.o
9ae3a8
 obj-y += excp_helper.o fpu_helper.o cc_helper.o int_helper.o svm_helper.o
9ae3a8
 obj-y += smm_helper.o misc_helper.o mem_helper.o seg_helper.o
9ae3a8
 obj-$(CONFIG_SOFTMMU) += machine.o arch_memory_mapping.o arch_dump.o
9ae3a8
-obj-$(CONFIG_KVM) += kvm.o hyperv.o
9ae3a8
+obj-$(CONFIG_KVM) += kvm.o
9ae3a8
 obj-$(CONFIG_NO_KVM) += kvm-stub.o
9ae3a8
 obj-$(CONFIG_LINUX_USER) += ioport-user.o
9ae3a8
 obj-$(CONFIG_BSD_USER) += ioport-user.o
9ae3a8
diff --git a/target-i386/cpu-qom.h b/target-i386/cpu-qom.h
9ae3a8
index 12c7bea..ac56fec 100644
9ae3a8
--- a/target-i386/cpu-qom.h
9ae3a8
+++ b/target-i386/cpu-qom.h
9ae3a8
@@ -66,6 +66,10 @@ typedef struct X86CPU {
9ae3a8
 
9ae3a8
     CPUX86State env;
9ae3a8
 
9ae3a8
+    bool hyperv_vapic;
9ae3a8
+    bool hyperv_relaxed_timing;
9ae3a8
+    int hyperv_spinlock_attempts;
9ae3a8
+
9ae3a8
     /* Features that were filtered out because of missing host capabilities */
9ae3a8
     uint32_t filtered_features[FEATURE_WORDS];
9ae3a8
 
9ae3a8
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
9ae3a8
index 31ff568..826d8d6 100644
9ae3a8
--- a/target-i386/cpu.c
9ae3a8
+++ b/target-i386/cpu.c
9ae3a8
@@ -35,8 +35,6 @@
9ae3a8
 #include "qapi/visitor.h"
9ae3a8
 #include "sysemu/arch_init.h"
9ae3a8
 
9ae3a8
-#include "hyperv.h"
9ae3a8
-
9ae3a8
 #include "hw/hw.h"
9ae3a8
 #if defined(CONFIG_KVM)
9ae3a8
 #include <linux/kvm_para.h>
9ae3a8
@@ -1629,12 +1627,19 @@ static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
9ae3a8
                 object_property_parse(OBJECT(cpu), num, "tsc-frequency", errp);
9ae3a8
             } else if (!strcmp(featurestr, "hv-spinlocks")) {
9ae3a8
                 char *err;
9ae3a8
+                const int min = 0xFFF;
9ae3a8
                 numvalue = strtoul(val, &err, 0);
9ae3a8
                 if (!*val || *err) {
9ae3a8
                     error_setg(errp, "bad numerical value %s", val);
9ae3a8
                     goto out;
9ae3a8
                 }
9ae3a8
-                hyperv_set_spinlock_retries(numvalue);
9ae3a8
+                if (numvalue < min) {
9ae3a8
+                    fprintf(stderr, "hv-spinlocks value shall always be >= 0x%x"
9ae3a8
+                            ", fixup will be removed in future versions\n",
9ae3a8
+                            min);
9ae3a8
+                    numvalue = min;
9ae3a8
+                }
9ae3a8
+                cpu->hyperv_spinlock_attempts = numvalue;
9ae3a8
             } else {
9ae3a8
                 error_setg(errp, "unrecognized feature %s", featurestr);
9ae3a8
                 goto out;
9ae3a8
@@ -1644,9 +1649,9 @@ static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
9ae3a8
         } else if (!strcmp(featurestr, "enforce")) {
9ae3a8
             check_cpuid = enforce_cpuid = 1;
9ae3a8
         } else if (!strcmp(featurestr, "hv_relaxed")) {
9ae3a8
-            hyperv_enable_relaxed_timing(true);
9ae3a8
+            cpu->hyperv_relaxed_timing = true;
9ae3a8
         } else if (!strcmp(featurestr, "hv_vapic")) {
9ae3a8
-            hyperv_enable_vapic_recommended(true);
9ae3a8
+            cpu->hyperv_vapic = true;
9ae3a8
         } else {
9ae3a8
             error_setg(errp, "feature string `%s' not in format (+feature|"
9ae3a8
                        "-feature|feature=xyz)", featurestr);
9ae3a8
@@ -2559,6 +2564,7 @@ static void x86_cpu_initfn(Object *obj)
9ae3a8
                         x86_cpu_get_feature_words,
9ae3a8
                         NULL, NULL, (void *)cpu->filtered_features, NULL);
9ae3a8
 
9ae3a8
+    cpu->hyperv_spinlock_attempts = HYPERV_SPINLOCK_NEVER_RETRY;
9ae3a8
     env->cpuid_apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);
9ae3a8
 
9ae3a8
     /* init various static tables used in TCG mode */
9ae3a8
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
9ae3a8
index 37ff264..4c4c9ff 100644
9ae3a8
--- a/target-i386/cpu.h
9ae3a8
+++ b/target-i386/cpu.h
9ae3a8
@@ -565,6 +565,10 @@ typedef uint32_t FeatureWordArray[FEATURE_WORDS];
9ae3a8
 #define CPUID_MWAIT_IBE     (1 << 1) /* Interrupts can exit capability */
9ae3a8
 #define CPUID_MWAIT_EMX     (1 << 0) /* enumeration supported */
9ae3a8
 
9ae3a8
+#ifndef HYPERV_SPINLOCK_NEVER_RETRY
9ae3a8
+#define HYPERV_SPINLOCK_NEVER_RETRY             0xFFFFFFFF
9ae3a8
+#endif
9ae3a8
+
9ae3a8
 #define EXCP00_DIVZ     0
9ae3a8
 #define EXCP01_DB       1
9ae3a8
 #define EXCP02_NMI      2
9ae3a8
diff --git a/target-i386/hyperv.c b/target-i386/hyperv.c
9ae3a8
deleted file mode 100644
9ae3a8
index f284e99..0000000
9ae3a8
--- a/target-i386/hyperv.c
9ae3a8
+++ /dev/null
9ae3a8
@@ -1,64 +0,0 @@
9ae3a8
-/*
9ae3a8
- * QEMU Hyper-V support
9ae3a8
- *
9ae3a8
- * Copyright Red Hat, Inc. 2011
9ae3a8
- *
9ae3a8
- * Author: Vadim Rozenfeld     <vrozenfe@redhat.com>
9ae3a8
- *
9ae3a8
- * This work is licensed under the terms of the GNU GPL, version 2 or later.
9ae3a8
- * See the COPYING file in the top-level directory.
9ae3a8
- *
9ae3a8
- */
9ae3a8
-
9ae3a8
-#include "hyperv.h"
9ae3a8
-
9ae3a8
-static bool hyperv_vapic;
9ae3a8
-static bool hyperv_relaxed_timing;
9ae3a8
-static int hyperv_spinlock_attempts = HYPERV_SPINLOCK_NEVER_RETRY;
9ae3a8
-
9ae3a8
-void hyperv_enable_vapic_recommended(bool val)
9ae3a8
-{
9ae3a8
-    hyperv_vapic = val;
9ae3a8
-}
9ae3a8
-
9ae3a8
-void hyperv_enable_relaxed_timing(bool val)
9ae3a8
-{
9ae3a8
-    hyperv_relaxed_timing = val;
9ae3a8
-}
9ae3a8
-
9ae3a8
-void hyperv_set_spinlock_retries(int val)
9ae3a8
-{
9ae3a8
-    hyperv_spinlock_attempts = val;
9ae3a8
-    if (hyperv_spinlock_attempts < 0xFFF) {
9ae3a8
-        hyperv_spinlock_attempts = 0xFFF;
9ae3a8
-    }
9ae3a8
-}
9ae3a8
-
9ae3a8
-bool hyperv_enabled(void)
9ae3a8
-{
9ae3a8
-    return hyperv_hypercall_available() || hyperv_relaxed_timing_enabled();
9ae3a8
-}
9ae3a8
-
9ae3a8
-bool hyperv_hypercall_available(void)
9ae3a8
-{
9ae3a8
-    if (hyperv_vapic ||
9ae3a8
-        (hyperv_spinlock_attempts != HYPERV_SPINLOCK_NEVER_RETRY)) {
9ae3a8
-      return true;
9ae3a8
-    }
9ae3a8
-    return false;
9ae3a8
-}
9ae3a8
-
9ae3a8
-bool hyperv_vapic_recommended(void)
9ae3a8
-{
9ae3a8
-    return hyperv_vapic;
9ae3a8
-}
9ae3a8
-
9ae3a8
-bool hyperv_relaxed_timing_enabled(void)
9ae3a8
-{
9ae3a8
-    return hyperv_relaxed_timing;
9ae3a8
-}
9ae3a8
-
9ae3a8
-int hyperv_get_spinlock_retries(void)
9ae3a8
-{
9ae3a8
-    return hyperv_spinlock_attempts;
9ae3a8
-}
9ae3a8
diff --git a/target-i386/hyperv.h b/target-i386/hyperv.h
9ae3a8
deleted file mode 100644
9ae3a8
index bacb1d4..0000000
9ae3a8
--- a/target-i386/hyperv.h
9ae3a8
+++ /dev/null
9ae3a8
@@ -1,45 +0,0 @@
9ae3a8
-/*
9ae3a8
- * QEMU Hyper-V support
9ae3a8
- *
9ae3a8
- * Copyright Red Hat, Inc. 2011
9ae3a8
- *
9ae3a8
- * Author: Vadim Rozenfeld     <vrozenfe@redhat.com>
9ae3a8
- *
9ae3a8
- * This work is licensed under the terms of the GNU GPL, version 2 or later.
9ae3a8
- * See the COPYING file in the top-level directory.
9ae3a8
- *
9ae3a8
- */
9ae3a8
-
9ae3a8
-#ifndef QEMU_HW_HYPERV_H
9ae3a8
-#define QEMU_HW_HYPERV_H 1
9ae3a8
-
9ae3a8
-#include "qemu-common.h"
9ae3a8
-#ifdef CONFIG_KVM
9ae3a8
-#include <asm/hyperv.h>
9ae3a8
-#endif
9ae3a8
-
9ae3a8
-#ifndef HYPERV_SPINLOCK_NEVER_RETRY
9ae3a8
-#define HYPERV_SPINLOCK_NEVER_RETRY             0xFFFFFFFF
9ae3a8
-#endif
9ae3a8
-
9ae3a8
-#ifndef KVM_CPUID_SIGNATURE_NEXT
9ae3a8
-#define KVM_CPUID_SIGNATURE_NEXT                0x40000100
9ae3a8
-#endif
9ae3a8
-
9ae3a8
-#if !defined(CONFIG_USER_ONLY) && defined(CONFIG_KVM)
9ae3a8
-void hyperv_enable_vapic_recommended(bool val);
9ae3a8
-void hyperv_enable_relaxed_timing(bool val);
9ae3a8
-void hyperv_set_spinlock_retries(int val);
9ae3a8
-#else
9ae3a8
-static inline void hyperv_enable_vapic_recommended(bool val) { }
9ae3a8
-static inline void hyperv_enable_relaxed_timing(bool val) { }
9ae3a8
-static inline void hyperv_set_spinlock_retries(int val) { }
9ae3a8
-#endif
9ae3a8
-
9ae3a8
-bool hyperv_enabled(void);
9ae3a8
-bool hyperv_hypercall_available(void);
9ae3a8
-bool hyperv_vapic_recommended(void);
9ae3a8
-bool hyperv_relaxed_timing_enabled(void);
9ae3a8
-int hyperv_get_spinlock_retries(void);
9ae3a8
-
9ae3a8
-#endif /* QEMU_HW_HYPERV_H */
9ae3a8
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
9ae3a8
index 4b470e4..c7fabd7 100644
9ae3a8
--- a/target-i386/kvm.c
9ae3a8
+++ b/target-i386/kvm.c
9ae3a8
@@ -31,7 +31,7 @@
9ae3a8
 #include "hw/i386/pc.h"
9ae3a8
 #include "hw/i386/apic.h"
9ae3a8
 #include "exec/ioport.h"
9ae3a8
-#include "hyperv.h"
9ae3a8
+#include <asm/hyperv.h>
9ae3a8
 #include "hw/pci/pci.h"
9ae3a8
 
9ae3a8
 //#define DEBUG_KVM
9ae3a8
@@ -421,6 +421,22 @@ unsigned long kvm_arch_vcpu_id(CPUState *cs)
9ae3a8
     return cpu->env.cpuid_apic_id;
9ae3a8
 }
9ae3a8
 
9ae3a8
+#ifndef KVM_CPUID_SIGNATURE_NEXT
9ae3a8
+#define KVM_CPUID_SIGNATURE_NEXT                0x40000100
9ae3a8
+#endif
9ae3a8
+
9ae3a8
+static bool hyperv_hypercall_available(X86CPU *cpu)
9ae3a8
+{
9ae3a8
+    return cpu->hyperv_vapic ||
9ae3a8
+           (cpu->hyperv_spinlock_attempts != HYPERV_SPINLOCK_NEVER_RETRY);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static bool hyperv_enabled(X86CPU *cpu)
9ae3a8
+{
9ae3a8
+    return hyperv_hypercall_available(cpu) ||
9ae3a8
+           cpu->hyperv_relaxed_timing;
9ae3a8
+}
9ae3a8
+
9ae3a8
 #define KVM_MAX_CPUID_ENTRIES  100
9ae3a8
 
9ae3a8
 int kvm_arch_init_vcpu(CPUState *cs)
9ae3a8
@@ -443,7 +459,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
9ae3a8
     c = &cpuid_data.entries[cpuid_i++];
9ae3a8
     memset(c, 0, sizeof(*c));
9ae3a8
     c->function = KVM_CPUID_SIGNATURE;
9ae3a8
-    if (!hyperv_enabled()) {
9ae3a8
+    if (!hyperv_enabled(cpu)) {
9ae3a8
         memcpy(signature, "KVMKVMKVM\0\0\0", 12);
9ae3a8
         c->eax = 0;
9ae3a8
     } else {
9ae3a8
@@ -459,7 +475,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
9ae3a8
     c->function = KVM_CPUID_FEATURES;
9ae3a8
     c->eax = env->features[FEAT_KVM];
9ae3a8
 
9ae3a8
-    if (hyperv_enabled()) {
9ae3a8
+    if (hyperv_enabled(cpu)) {
9ae3a8
         memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
9ae3a8
         c->eax = signature[0];
9ae3a8
 
9ae3a8
@@ -472,10 +488,10 @@ int kvm_arch_init_vcpu(CPUState *cs)
9ae3a8
         c = &cpuid_data.entries[cpuid_i++];
9ae3a8
         memset(c, 0, sizeof(*c));
9ae3a8
         c->function = HYPERV_CPUID_FEATURES;
9ae3a8
-        if (hyperv_relaxed_timing_enabled()) {
9ae3a8
+        if (cpu->hyperv_relaxed_timing) {
9ae3a8
             c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
9ae3a8
         }
9ae3a8
-        if (hyperv_vapic_recommended()) {
9ae3a8
+        if (cpu->hyperv_vapic) {
9ae3a8
             c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
9ae3a8
             c->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
9ae3a8
         }
9ae3a8
@@ -483,13 +499,13 @@ int kvm_arch_init_vcpu(CPUState *cs)
9ae3a8
         c = &cpuid_data.entries[cpuid_i++];
9ae3a8
         memset(c, 0, sizeof(*c));
9ae3a8
         c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
9ae3a8
-        if (hyperv_relaxed_timing_enabled()) {
9ae3a8
+        if (cpu->hyperv_relaxed_timing) {
9ae3a8
             c->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
9ae3a8
         }
9ae3a8
-        if (hyperv_vapic_recommended()) {
9ae3a8
+        if (cpu->hyperv_vapic) {
9ae3a8
             c->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
9ae3a8
         }
9ae3a8
-        c->ebx = hyperv_get_spinlock_retries();
9ae3a8
+        c->ebx = cpu->hyperv_spinlock_attempts;
9ae3a8
 
9ae3a8
         c = &cpuid_data.entries[cpuid_i++];
9ae3a8
         memset(c, 0, sizeof(*c));
9ae3a8
@@ -1177,11 +1193,11 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
9ae3a8
             kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL,
9ae3a8
                               env->msr_global_ctrl);
9ae3a8
         }
9ae3a8
-        if (hyperv_hypercall_available()) {
9ae3a8
+        if (hyperv_hypercall_available(cpu)) {
9ae3a8
             kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_GUEST_OS_ID, 0);
9ae3a8
             kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_HYPERCALL, 0);
9ae3a8
         }
9ae3a8
-        if (hyperv_vapic_recommended()) {
9ae3a8
+        if (cpu->hyperv_vapic) {
9ae3a8
             kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE, 0);
9ae3a8
         }
9ae3a8
     }
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8