Pablo Greco e6a3ae
From 7e7c5dab29ed99bda0fb5d810db6f12ae4aa2608 Mon Sep 17 00:00:00 2001
Pablo Greco e6a3ae
From: "plai@redhat.com" <plai@redhat.com>
Pablo Greco e6a3ae
Date: Tue, 26 Nov 2019 18:36:54 +0000
Pablo Greco e6a3ae
Subject: [PATCH 10/11] x86/cpu: Add support for UMONITOR/UMWAIT/TPAUSE
Pablo Greco e6a3ae
MIME-Version: 1.0
Pablo Greco e6a3ae
Content-Type: text/plain; charset=UTF-8
Pablo Greco e6a3ae
Content-Transfer-Encoding: 8bit
Pablo Greco e6a3ae
Pablo Greco e6a3ae
x86/cpu: Add support for UMONITOR/UMWAIT/TPAUSE
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: plai@redhat.com
Pablo Greco e6a3ae
Message-id: <1574797015-32564-7-git-send-email-plai@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 92695
Pablo Greco e6a3ae
O-Subject: [RHEL8.2 qemu-kvm PATCH 6/7] x86/cpu: Add support for UMONITOR/UMWAIT/TPAUSE
Pablo Greco e6a3ae
Bugzilla: 1634827
Pablo Greco e6a3ae
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
From: Tao Xu <tao3.xu@intel.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
UMONITOR, UMWAIT and TPAUSE are a set of user wait instructions.
Pablo Greco e6a3ae
This patch adds support for user wait instructions in KVM. Availability
Pablo Greco e6a3ae
of the user wait instructions is indicated by the presence of the CPUID
Pablo Greco e6a3ae
feature flag WAITPKG CPUID.0x07.0x0:ECX[5]. User wait instructions may
Pablo Greco e6a3ae
be executed at any privilege level, and use IA32_UMWAIT_CONTROL MSR to
Pablo Greco e6a3ae
set the maximum time.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
The patch enable the umonitor, umwait and tpause features in KVM.
Pablo Greco e6a3ae
Because umwait and tpause can put a (psysical) CPU into a power saving
Pablo Greco e6a3ae
state, by default we dont't expose it to kvm and enable it only when
Pablo Greco e6a3ae
guest CPUID has it. And use QEMU command-line "-overcommit cpu-pm=on"
Pablo Greco e6a3ae
(enable_cpu_pm is enabled), a VM can use UMONITOR, UMWAIT and TPAUSE
Pablo Greco e6a3ae
instructions. If the instruction causes a delay, the amount of time
Pablo Greco e6a3ae
delayed is called here the physical delay. The physical delay is first
Pablo Greco e6a3ae
computed by determining the virtual delay (the time to delay relative to
Pablo Greco e6a3ae
the VM’s timestamp counter). Otherwise, UMONITOR, UMWAIT and TPAUSE cause
Pablo Greco e6a3ae
an invalid-opcode exception(#UD).
Pablo Greco e6a3ae
Pablo Greco e6a3ae
The release document ref below link:
Pablo Greco e6a3ae
https://software.intel.com/sites/default/files/\
Pablo Greco e6a3ae
managed/39/c5/325462-sdm-vol-1-2abcd-3abcd.pdf
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Co-developed-by: Jingqi Liu <jingqi.liu@intel.com>
Pablo Greco e6a3ae
Signed-off-by: Jingqi Liu <jingqi.liu@intel.com>
Pablo Greco e6a3ae
Signed-off-by: Tao Xu <tao3.xu@intel.com>
Pablo Greco e6a3ae
Message-Id: <20191011074103.30393-2-tao3.xu@intel.com>
Pablo Greco e6a3ae
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Pablo Greco e6a3ae
(cherry picked from commit 67192a298f5bf98f96e5516c3b6474c49e4853cd)
Pablo Greco e6a3ae
Signed-off-by: Paul Lai <plai@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Resolved Conflicts:
Pablo Greco e6a3ae
	target/i386/cpu.c
Pablo Greco e6a3ae
	target/i386/cpu.h
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 target/i386/cpu.c | 2 +-
Pablo Greco e6a3ae
 target/i386/cpu.h | 2 ++
Pablo Greco e6a3ae
 target/i386/kvm.c | 6 ++++++
Pablo Greco e6a3ae
 3 files changed, 9 insertions(+), 1 deletion(-)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
Pablo Greco e6a3ae
index 87b0502..7d2afc7 100644
Pablo Greco e6a3ae
--- a/target/i386/cpu.c
Pablo Greco e6a3ae
+++ b/target/i386/cpu.c
Pablo Greco e6a3ae
@@ -1016,7 +1016,7 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = {
Pablo Greco e6a3ae
         .type = CPUID_FEATURE_WORD,
Pablo Greco e6a3ae
         .feat_names = {
Pablo Greco e6a3ae
             NULL, "avx512vbmi", "umip", "pku",
Pablo Greco e6a3ae
-            "ospke", NULL, "avx512vbmi2", NULL,
Pablo Greco e6a3ae
+            "ospke", "waitpkg", "avx512vbmi2", NULL,
Pablo Greco e6a3ae
             "gfni", "vaes", "vpclmulqdq", "avx512vnni",
Pablo Greco e6a3ae
             "avx512bitalg", NULL, "avx512-vpopcntdq", NULL,
Pablo Greco e6a3ae
             "la57", NULL, NULL, NULL,
Pablo Greco e6a3ae
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
Pablo Greco e6a3ae
index 7ab8ee9..fac98aa 100644
Pablo Greco e6a3ae
--- a/target/i386/cpu.h
Pablo Greco e6a3ae
+++ b/target/i386/cpu.h
Pablo Greco e6a3ae
@@ -708,6 +708,8 @@ typedef uint64_t FeatureWordArray[FEATURE_WORDS];
Pablo Greco e6a3ae
 #define CPUID_7_0_ECX_UMIP     (1U << 2)
Pablo Greco e6a3ae
 #define CPUID_7_0_ECX_PKU      (1U << 3)
Pablo Greco e6a3ae
 #define CPUID_7_0_ECX_OSPKE    (1U << 4)
Pablo Greco e6a3ae
+/* UMONITOR/UMWAIT/TPAUSE Instructions */
Pablo Greco e6a3ae
+#define CPUID_7_0_ECX_WAITPKG  (1U << 5)
Pablo Greco e6a3ae
 #define CPUID_7_0_ECX_VBMI2    (1U << 6) /* Additional VBMI Instrs */
Pablo Greco e6a3ae
 #define CPUID_7_0_ECX_GFNI     (1U << 8)
Pablo Greco e6a3ae
 #define CPUID_7_0_ECX_VAES     (1U << 9)
Pablo Greco e6a3ae
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
Pablo Greco e6a3ae
index ffd01f0..0fd5650 100644
Pablo Greco e6a3ae
--- a/target/i386/kvm.c
Pablo Greco e6a3ae
+++ b/target/i386/kvm.c
Pablo Greco e6a3ae
@@ -392,6 +392,12 @@ uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
Pablo Greco e6a3ae
         if (host_tsx_blacklisted()) {
Pablo Greco e6a3ae
             ret &= ~(CPUID_7_0_EBX_RTM | CPUID_7_0_EBX_HLE);
Pablo Greco e6a3ae
         }
Pablo Greco e6a3ae
+    } else if (function == 7 && index == 0 && reg == R_ECX) {
Pablo Greco e6a3ae
+        if (enable_cpu_pm) {
Pablo Greco e6a3ae
+            ret |= CPUID_7_0_ECX_WAITPKG;
Pablo Greco e6a3ae
+        } else {
Pablo Greco e6a3ae
+            ret &= ~CPUID_7_0_ECX_WAITPKG;
Pablo Greco e6a3ae
+        }
Pablo Greco e6a3ae
     } else if (function == 7 && index == 0 && reg == R_EDX) {
Pablo Greco e6a3ae
         /*
Pablo Greco e6a3ae
          * Linux v4.17-v4.20 incorrectly return ARCH_CAPABILITIES on SVM hosts.
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae