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