016a62
From 91ac1f511b0414292d07688c3cb3012bed6e3649 Mon Sep 17 00:00:00 2001
016a62
From: "Michael S. Tsirkin" <mst@redhat.com>
016a62
Date: Fri, 22 Jun 2018 22:22:05 +0300
016a62
Subject: [PATCH 09/11] i386/cpu: make -cpu host support monitor/mwait
016a62
016a62
When guest CPU PM is enabled, and with -cpu host, expose the host CPU
016a62
MWAIT leaf in the CPUID so guest can make good PM decisions.
016a62
016a62
Note: the result is 100% CPU utilization reported by host as host
016a62
no longer knows that the CPU is halted.
016a62
016a62
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
016a62
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
016a62
Message-Id: <20180622192148.178309-3-mst@redhat.com>
016a62
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
016a62
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
016a62
---
016a62
 accel/tcg/user-exec-stub.c |  3 +++
016a62
 target/i386/cpu.c          | 32 ++++++++++++++++++++++----------
016a62
 target/i386/cpu.h          |  9 +++++++++
016a62
 target/i386/kvm.c          |  9 +++++++++
016a62
 4 files changed, 43 insertions(+), 10 deletions(-)
016a62
016a62
diff --git a/accel/tcg/user-exec-stub.c b/accel/tcg/user-exec-stub.c
016a62
index dbcf1ad..a32b449 100644
016a62
--- a/accel/tcg/user-exec-stub.c
016a62
+++ b/accel/tcg/user-exec-stub.c
016a62
@@ -2,6 +2,9 @@
016a62
 #include "qemu-common.h"
016a62
 #include "qom/cpu.h"
016a62
 #include "sysemu/replay.h"
016a62
+#include "sysemu/sysemu.h"
016a62
+
016a62
+bool enable_cpu_pm = false;
016a62
 
016a62
 void cpu_resume(CPUState *cpu)
016a62
 {
016a62
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
016a62
index 307b629..87b0502 100644
016a62
--- a/target/i386/cpu.c
016a62
+++ b/target/i386/cpu.c
016a62
@@ -5662,11 +5662,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
016a62
         }
016a62
         break;
016a62
     case 5:
016a62
-        /* mwait info: needed for Core compatibility */
016a62
-        *eax = 0; /* Smallest monitor-line size in bytes */
016a62
-        *ebx = 0; /* Largest monitor-line size in bytes */
016a62
-        *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
016a62
-        *edx = 0;
016a62
+        /* MONITOR/MWAIT Leaf */
016a62
+        *eax = cpu->mwait.eax; /* Smallest monitor-line size in bytes */
016a62
+        *ebx = cpu->mwait.ebx; /* Largest monitor-line size in bytes */
016a62
+        *ecx = cpu->mwait.ecx; /* flags */
016a62
+        *edx = cpu->mwait.edx; /* mwait substates */
016a62
         break;
016a62
     case 6:
016a62
         /* Thermal and Power Leaf */
016a62
@@ -6521,13 +6521,25 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
016a62
     Error *local_err = NULL;
016a62
     static bool ht_warned;
016a62
 
016a62
-    if (xcc->host_cpuid_required && !accel_uses_host_cpuid()) {
016a62
-        char *name = x86_cpu_class_get_model_name(xcc);
016a62
-        error_setg(&local_err, "CPU model '%s' requires KVM", name);
016a62
-        g_free(name);
016a62
-        goto out;
016a62
+    if (xcc->host_cpuid_required) {
016a62
+        if (!accel_uses_host_cpuid()) {
016a62
+            char *name = x86_cpu_class_get_model_name(xcc);
016a62
+            error_setg(&local_err, "CPU model '%s' requires KVM", name);
016a62
+            g_free(name);
016a62
+            goto out;
016a62
+        }
016a62
+
016a62
+        if (enable_cpu_pm) {
016a62
+            host_cpuid(5, 0, &cpu->mwait.eax, &cpu->mwait.ebx,
016a62
+                       &cpu->mwait.ecx, &cpu->mwait.edx);
016a62
+            env->features[FEAT_1_ECX] |= CPUID_EXT_MONITOR;
016a62
+        }
016a62
     }
016a62
 
016a62
+    /* mwait extended info: needed for Core compatibility */
016a62
+    /* We always wake on interrupt even if host does not have the capability */
016a62
+    cpu->mwait.ecx |= CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
016a62
+
016a62
     if (cpu->apic_id == UNASSIGNED_APIC_ID) {
016a62
         error_setg(errp, "apic-id property was not initialized properly");
016a62
         return;
016a62
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
016a62
index d33fa8d..7ab8ee9 100644
016a62
--- a/target/i386/cpu.h
016a62
+++ b/target/i386/cpu.h
016a62
@@ -1564,6 +1564,15 @@ struct X86CPU {
016a62
     /* if true the CPUID code directly forward host cache leaves to the guest */
016a62
     bool cache_info_passthrough;
016a62
 
016a62
+    /* if true the CPUID code directly forwards
016a62
+     * host monitor/mwait leaves to the guest */
016a62
+    struct {
016a62
+        uint32_t eax;
016a62
+        uint32_t ebx;
016a62
+        uint32_t ecx;
016a62
+        uint32_t edx;
016a62
+    } mwait;
016a62
+
016a62
     /* Features that were filtered out because of missing host capabilities */
016a62
     FeatureWordArray filtered_features;
016a62
 
016a62
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
016a62
index 879c3e0..ffd01f0 100644
016a62
--- a/target/i386/kvm.c
016a62
+++ b/target/i386/kvm.c
016a62
@@ -377,6 +377,15 @@ uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
016a62
         if (!kvm_irqchip_in_kernel()) {
016a62
             ret &= ~CPUID_EXT_X2APIC;
016a62
         }
016a62
+
016a62
+        if (enable_cpu_pm) {
016a62
+            int disable_exits = kvm_check_extension(s,
016a62
+                                                    KVM_CAP_X86_DISABLE_EXITS);
016a62
+
016a62
+            if (disable_exits & KVM_X86_DISABLE_EXITS_MWAIT) {
016a62
+                ret |= CPUID_EXT_MONITOR;
016a62
+            }
016a62
+        }
016a62
     } else if (function == 6 && reg == R_EAX) {
016a62
         ret |= CPUID_6_EAX_ARAT; /* safe to allow because of emulated APIC */
016a62
     } else if (function == 7 && index == 0 && reg == R_EBX) {
016a62
-- 
016a62
1.8.3.1
016a62