|
|
4841a6 |
From 50840e01d05a466a1dfbc219e49233834e5d7ed0 Mon Sep 17 00:00:00 2001
|
|
|
4841a6 |
From: Yang Zhong <yang.zhong@intel.com>
|
|
|
4841a6 |
Date: Wed, 16 Feb 2022 22:04:29 -0800
|
|
|
4841a6 |
Subject: [PATCH 07/24] x86: Grant AMX permission for guest
|
|
|
4841a6 |
|
|
|
4841a6 |
RH-Author: Paul Lai <plai@redhat.com>
|
|
|
4841a6 |
RH-MergeRequest: 176: Enable KVM AMX support
|
|
|
4841a6 |
RH-Commit: [7/13] 437578191f61139ca710cc7045ab38eb0d05eae2
|
|
|
4841a6 |
RH-Bugzilla: 1916415
|
|
|
4841a6 |
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
|
|
|
4841a6 |
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
|
|
|
4841a6 |
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
4841a6 |
|
|
|
4841a6 |
Kernel allocates 4K xstate buffer by default. For XSAVE features
|
|
|
4841a6 |
which require large state component (e.g. AMX), Linux kernel
|
|
|
4841a6 |
dynamically expands the xstate buffer only after the process has
|
|
|
4841a6 |
acquired the necessary permissions. Those are called dynamically-
|
|
|
4841a6 |
enabled XSAVE features (or dynamic xfeatures).
|
|
|
4841a6 |
|
|
|
4841a6 |
There are separate permissions for native tasks and guests.
|
|
|
4841a6 |
|
|
|
4841a6 |
Qemu should request the guest permissions for dynamic xfeatures
|
|
|
4841a6 |
which will be exposed to the guest. This only needs to be done
|
|
|
4841a6 |
once before the first vcpu is created.
|
|
|
4841a6 |
|
|
|
4841a6 |
KVM implemented one new ARCH_GET_XCOMP_SUPP system attribute API to
|
|
|
4841a6 |
get host side supported_xcr0 and Qemu can decide if it can request
|
|
|
4841a6 |
dynamically enabled XSAVE features permission.
|
|
|
4841a6 |
https://lore.kernel.org/all/20220126152210.3044876-1-pbonzini@redhat.com/
|
|
|
4841a6 |
|
|
|
4841a6 |
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
4841a6 |
Signed-off-by: Yang Zhong <yang.zhong@intel.com>
|
|
|
4841a6 |
Signed-off-by: Jing Liu <jing2.liu@intel.com>
|
|
|
4841a6 |
Message-Id: <20220217060434.52460-4-yang.zhong@intel.com>
|
|
|
4841a6 |
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
4841a6 |
(cherry picked from commit 19db68ca68a78fa033a21d419036b6e416554564)
|
|
|
4841a6 |
Signed-off-by: Paul Lai <plai@redhat.com>
|
|
|
4841a6 |
---
|
|
|
4841a6 |
target/i386/cpu.c | 7 +++++
|
|
|
4841a6 |
target/i386/cpu.h | 4 +++
|
|
|
4841a6 |
target/i386/kvm/kvm-cpu.c | 12 ++++----
|
|
|
4841a6 |
target/i386/kvm/kvm.c | 57 ++++++++++++++++++++++++++++++++++++++
|
|
|
4841a6 |
target/i386/kvm/kvm_i386.h | 1 +
|
|
|
4841a6 |
5 files changed, 75 insertions(+), 6 deletions(-)
|
|
|
4841a6 |
|
|
|
4841a6 |
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
|
|
|
4841a6 |
index 0453c27c9d..c19b51ea32 100644
|
|
|
4841a6 |
--- a/target/i386/cpu.c
|
|
|
4841a6 |
+++ b/target/i386/cpu.c
|
|
|
4841a6 |
@@ -6027,6 +6027,7 @@ static void x86_cpu_enable_xsave_components(X86CPU *cpu)
|
|
|
4841a6 |
CPUX86State *env = &cpu->env;
|
|
|
4841a6 |
int i;
|
|
|
4841a6 |
uint64_t mask;
|
|
|
4841a6 |
+ static bool request_perm;
|
|
|
4841a6 |
|
|
|
4841a6 |
if (!(env->features[FEAT_1_ECX] & CPUID_EXT_XSAVE)) {
|
|
|
4841a6 |
env->features[FEAT_XSAVE_COMP_LO] = 0;
|
|
|
4841a6 |
@@ -6042,6 +6043,12 @@ static void x86_cpu_enable_xsave_components(X86CPU *cpu)
|
|
|
4841a6 |
}
|
|
|
4841a6 |
}
|
|
|
4841a6 |
|
|
|
4841a6 |
+ /* Only request permission for first vcpu */
|
|
|
4841a6 |
+ if (kvm_enabled() && !request_perm) {
|
|
|
4841a6 |
+ kvm_request_xsave_components(cpu, mask);
|
|
|
4841a6 |
+ request_perm = true;
|
|
|
4841a6 |
+ }
|
|
|
4841a6 |
+
|
|
|
4841a6 |
env->features[FEAT_XSAVE_COMP_LO] = mask;
|
|
|
4841a6 |
env->features[FEAT_XSAVE_COMP_HI] = mask >> 32;
|
|
|
4841a6 |
}
|
|
|
4841a6 |
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
|
|
|
4841a6 |
index e1dd8b9555..58676390e6 100644
|
|
|
4841a6 |
--- a/target/i386/cpu.h
|
|
|
4841a6 |
+++ b/target/i386/cpu.h
|
|
|
4841a6 |
@@ -549,6 +549,10 @@ typedef enum X86Seg {
|
|
|
4841a6 |
#define XSTATE_ZMM_Hi256_MASK (1ULL << XSTATE_ZMM_Hi256_BIT)
|
|
|
4841a6 |
#define XSTATE_Hi16_ZMM_MASK (1ULL << XSTATE_Hi16_ZMM_BIT)
|
|
|
4841a6 |
#define XSTATE_PKRU_MASK (1ULL << XSTATE_PKRU_BIT)
|
|
|
4841a6 |
+#define XSTATE_XTILE_CFG_MASK (1ULL << XSTATE_XTILE_CFG_BIT)
|
|
|
4841a6 |
+#define XSTATE_XTILE_DATA_MASK (1ULL << XSTATE_XTILE_DATA_BIT)
|
|
|
4841a6 |
+
|
|
|
4841a6 |
+#define XSTATE_DYNAMIC_MASK (XSTATE_XTILE_DATA_MASK)
|
|
|
4841a6 |
|
|
|
4841a6 |
#define ESA_FEATURE_ALIGN64_BIT 1
|
|
|
4841a6 |
|
|
|
4841a6 |
diff --git a/target/i386/kvm/kvm-cpu.c b/target/i386/kvm/kvm-cpu.c
|
|
|
4841a6 |
index 86ef7b2712..bdc967c484 100644
|
|
|
4841a6 |
--- a/target/i386/kvm/kvm-cpu.c
|
|
|
4841a6 |
+++ b/target/i386/kvm/kvm-cpu.c
|
|
|
4841a6 |
@@ -84,7 +84,7 @@ static void kvm_cpu_max_instance_init(X86CPU *cpu)
|
|
|
4841a6 |
static void kvm_cpu_xsave_init(void)
|
|
|
4841a6 |
{
|
|
|
4841a6 |
static bool first = true;
|
|
|
4841a6 |
- KVMState *s = kvm_state;
|
|
|
4841a6 |
+ uint32_t eax, ebx, ecx, edx;
|
|
|
4841a6 |
int i;
|
|
|
4841a6 |
|
|
|
4841a6 |
if (!first) {
|
|
|
4841a6 |
@@ -100,11 +100,11 @@ static void kvm_cpu_xsave_init(void)
|
|
|
4841a6 |
ExtSaveArea *esa = &x86_ext_save_areas[i];
|
|
|
4841a6 |
|
|
|
4841a6 |
if (esa->size) {
|
|
|
4841a6 |
- int sz = kvm_arch_get_supported_cpuid(s, 0xd, i, R_EAX);
|
|
|
4841a6 |
- if (sz != 0) {
|
|
|
4841a6 |
- assert(esa->size == sz);
|
|
|
4841a6 |
- esa->offset = kvm_arch_get_supported_cpuid(s, 0xd, i, R_EBX);
|
|
|
4841a6 |
- esa->ecx = kvm_arch_get_supported_cpuid(s, 0xd, i, R_ECX);
|
|
|
4841a6 |
+ host_cpuid(0xd, i, &eax, &ebx, &ecx, &edx;;
|
|
|
4841a6 |
+ if (eax != 0) {
|
|
|
4841a6 |
+ assert(esa->size == eax);
|
|
|
4841a6 |
+ esa->offset = ebx;
|
|
|
4841a6 |
+ esa->ecx = ecx;
|
|
|
4841a6 |
}
|
|
|
4841a6 |
}
|
|
|
4841a6 |
}
|
|
|
4841a6 |
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
|
|
|
4841a6 |
index a668f521ac..b5d98c4361 100644
|
|
|
4841a6 |
--- a/target/i386/kvm/kvm.c
|
|
|
4841a6 |
+++ b/target/i386/kvm/kvm.c
|
|
|
4841a6 |
@@ -17,6 +17,7 @@
|
|
|
4841a6 |
#include "qapi/error.h"
|
|
|
4841a6 |
#include <sys/ioctl.h>
|
|
|
4841a6 |
#include <sys/utsname.h>
|
|
|
4841a6 |
+#include <sys/syscall.h>
|
|
|
4841a6 |
|
|
|
4841a6 |
#include <linux/kvm.h>
|
|
|
4841a6 |
#include "standard-headers/asm-x86/kvm_para.h"
|
|
|
4841a6 |
@@ -347,6 +348,7 @@ uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
|
|
|
4841a6 |
struct kvm_cpuid2 *cpuid;
|
|
|
4841a6 |
uint32_t ret = 0;
|
|
|
4841a6 |
uint32_t cpuid_1_edx;
|
|
|
4841a6 |
+ uint64_t bitmask;
|
|
|
4841a6 |
|
|
|
4841a6 |
cpuid = get_supported_cpuid(s);
|
|
|
4841a6 |
|
|
|
4841a6 |
@@ -404,6 +406,25 @@ uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
|
|
|
4841a6 |
if (!has_msr_arch_capabs) {
|
|
|
4841a6 |
ret &= ~CPUID_7_0_EDX_ARCH_CAPABILITIES;
|
|
|
4841a6 |
}
|
|
|
4841a6 |
+ } else if (function == 0xd && index == 0 &&
|
|
|
4841a6 |
+ (reg == R_EAX || reg == R_EDX)) {
|
|
|
4841a6 |
+ struct kvm_device_attr attr = {
|
|
|
4841a6 |
+ .group = 0,
|
|
|
4841a6 |
+ .attr = KVM_X86_XCOMP_GUEST_SUPP,
|
|
|
4841a6 |
+ .addr = (unsigned long) &bitmask
|
|
|
4841a6 |
+ };
|
|
|
4841a6 |
+
|
|
|
4841a6 |
+ bool sys_attr = kvm_check_extension(s, KVM_CAP_SYS_ATTRIBUTES);
|
|
|
4841a6 |
+ if (!sys_attr) {
|
|
|
4841a6 |
+ warn_report("cannot get sys attribute capabilities %d", sys_attr);
|
|
|
4841a6 |
+ }
|
|
|
4841a6 |
+
|
|
|
4841a6 |
+ int rc = kvm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
|
|
|
4841a6 |
+ if (rc == -1 && (errno == ENXIO || errno == EINVAL)) {
|
|
|
4841a6 |
+ warn_report("KVM_GET_DEVICE_ATTR(0, KVM_X86_XCOMP_GUEST_SUPP) "
|
|
|
4841a6 |
+ "error: %d", rc);
|
|
|
4841a6 |
+ }
|
|
|
4841a6 |
+ ret = (reg == R_EAX) ? bitmask : bitmask >> 32;
|
|
|
4841a6 |
} else if (function == 0x80000001 && reg == R_ECX) {
|
|
|
4841a6 |
/*
|
|
|
4841a6 |
* It's safe to enable TOPOEXT even if it's not returned by
|
|
|
4841a6 |
@@ -5054,3 +5075,39 @@ bool kvm_arch_cpu_check_are_resettable(void)
|
|
|
4841a6 |
{
|
|
|
4841a6 |
return !sev_es_enabled();
|
|
|
4841a6 |
}
|
|
|
4841a6 |
+
|
|
|
4841a6 |
+#define ARCH_REQ_XCOMP_GUEST_PERM 0x1025
|
|
|
4841a6 |
+
|
|
|
4841a6 |
+void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask)
|
|
|
4841a6 |
+{
|
|
|
4841a6 |
+ KVMState *s = kvm_state;
|
|
|
4841a6 |
+ uint64_t supported;
|
|
|
4841a6 |
+
|
|
|
4841a6 |
+ mask &= XSTATE_DYNAMIC_MASK;
|
|
|
4841a6 |
+ if (!mask) {
|
|
|
4841a6 |
+ return;
|
|
|
4841a6 |
+ }
|
|
|
4841a6 |
+ /*
|
|
|
4841a6 |
+ * Just ignore bits that are not in CPUID[EAX=0xD,ECX=0].
|
|
|
4841a6 |
+ * ARCH_REQ_XCOMP_GUEST_PERM would fail, and QEMU has warned
|
|
|
4841a6 |
+ * about them already because they are not supported features.
|
|
|
4841a6 |
+ */
|
|
|
4841a6 |
+ supported = kvm_arch_get_supported_cpuid(s, 0xd, 0, R_EAX);
|
|
|
4841a6 |
+ supported |= (uint64_t)kvm_arch_get_supported_cpuid(s, 0xd, 0, R_EDX) << 32;
|
|
|
4841a6 |
+ mask &= supported;
|
|
|
4841a6 |
+
|
|
|
4841a6 |
+ while (mask) {
|
|
|
4841a6 |
+ int bit = ctz64(mask);
|
|
|
4841a6 |
+ int rc = syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_GUEST_PERM, bit);
|
|
|
4841a6 |
+ if (rc) {
|
|
|
4841a6 |
+ /*
|
|
|
4841a6 |
+ * Older kernel version (<5.17) do not support
|
|
|
4841a6 |
+ * ARCH_REQ_XCOMP_GUEST_PERM, but also do not return
|
|
|
4841a6 |
+ * any dynamic feature from kvm_arch_get_supported_cpuid.
|
|
|
4841a6 |
+ */
|
|
|
4841a6 |
+ warn_report("prctl(ARCH_REQ_XCOMP_GUEST_PERM) failure "
|
|
|
4841a6 |
+ "for feature bit %d", bit);
|
|
|
4841a6 |
+ }
|
|
|
4841a6 |
+ mask &= ~BIT_ULL(bit);
|
|
|
4841a6 |
+ }
|
|
|
4841a6 |
+}
|
|
|
4841a6 |
diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h
|
|
|
4841a6 |
index a978509d50..4124912c20 100644
|
|
|
4841a6 |
--- a/target/i386/kvm/kvm_i386.h
|
|
|
4841a6 |
+++ b/target/i386/kvm/kvm_i386.h
|
|
|
4841a6 |
@@ -52,5 +52,6 @@ bool kvm_hyperv_expand_features(X86CPU *cpu, Error **errp);
|
|
|
4841a6 |
uint64_t kvm_swizzle_msi_ext_dest_id(uint64_t address);
|
|
|
4841a6 |
|
|
|
4841a6 |
bool kvm_enable_sgx_provisioning(KVMState *s);
|
|
|
4841a6 |
+void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask);
|
|
|
4841a6 |
|
|
|
4841a6 |
#endif
|
|
|
4841a6 |
--
|
|
|
4841a6 |
2.35.3
|
|
|
4841a6 |
|