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