5d360b
From 27f52a18ed8503f5a0333106c38e44f911052d4d Mon Sep 17 00:00:00 2001
5d360b
From: "plai@redhat.com" <plai@redhat.com>
5d360b
Date: Mon, 11 Sep 2017 21:57:36 +0200
5d360b
Subject: [PATCH 4/4] target-i386: Add PKU and and OSPKE support
5d360b
5d360b
RH-Author: plai@redhat.com
5d360b
Message-id: <1505167056-5861-1-git-send-email-plai@redhat.com>
5d360b
Patchwork-id: 76308
5d360b
O-Subject: [RHEL7.5 PATCH BZ 1387648 v2] target-i386: Add PKU and and OSPKE support
5d360b
Bugzilla: 1387648
5d360b
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
5d360b
RH-Acked-by: Bandan Das <bsd@redhat.com>
5d360b
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
5d360b
5d360b
From: Huaitong Han <huaitong.han@intel.com>
5d360b
5d360b
------ v2 comment
5d360b
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1387648
5d360b
BREW: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=14031692
5d360b
5d360b
In v1, Eduardo noticed "avx512-vpopcntdq" moved in
5d360b
target-i386/cpu.c:cpuid_7_0_ecx_feature_name[].
5d360b
Corrected the patch typo here in v2.
5d360b
5d360b
Tested on intel-purley-fpgabmp-01.khw.lab.eng.bos.redhat.com.
5d360b
VM guest was fedora-26 image (linux 4.11.0-300).
5d360b
Tests Run (of tools/testing/selftests/x86/protection_keys.c):
5d360b
 1. Successful test on host,
5d360b
 2. Successful negative test w/ current qemu-kvm in VM guest.
5d360b
 3. Successful test w/ test build qemu-kvm in VM guest.
5d360b
------
5d360b
5d360b
Add PKU and OSPKE CPUID features, including xsave state and
5d360b
migration support.
5d360b
5d360b
Signed-off-by: Huaitong Han <huaitong.han@intel.com>
5d360b
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
5d360b
[ehabkost: squashed 3 patches together, edited patch description]
5d360b
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
5d360b
5d360b
(cherry picked from commit f74eefe0b98cd7e13825de8e8d9f32e22aed102c)
5d360b
Signed-off-by: Paul Lai <plai@redhat.com>
5d360b
5d360b
Resolved Conflicts:
5d360b
	target-i386/cpu.c
5d360b
	target-i386/cpu.h
5d360b
	target-i386/kvm.c
5d360b
	target-i386/machine.c
5d360b
5d360b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
5d360b
---
5d360b
 target-i386/cpu.c     |  6 ++++--
5d360b
 target-i386/cpu.h     |  6 ++++++
5d360b
 target-i386/kvm.c     |  3 +++
5d360b
 target-i386/machine.c | 25 +++++++++++++++++++++++++
5d360b
 4 files changed, 38 insertions(+), 2 deletions(-)
5d360b
5d360b
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
5d360b
index ae56995..fbd3117 100644
5d360b
--- a/target-i386/cpu.c
5d360b
+++ b/target-i386/cpu.c
5d360b
@@ -155,8 +155,8 @@ static const char *cpuid_7_0_ebx_feature_name[] = {
5d360b
 };
5d360b
 
5d360b
 static const char *cpuid_7_0_ecx_feature_name[] = {
5d360b
-    NULL, "avx512vbmi", NULL, NULL,
5d360b
-    NULL, NULL, NULL, NULL,
5d360b
+    NULL, "avx512vbmi", NULL, "pku",
5d360b
+    "ospke", NULL, NULL, NULL,
5d360b
     NULL, NULL, NULL, NULL,
5d360b
     NULL, NULL, "avx512-vpopcntdq", NULL,
5d360b
     NULL, NULL, NULL, NULL,
5d360b
@@ -361,6 +361,8 @@ static const ExtSaveArea ext_save_areas[] = {
5d360b
             .offset = 0x480, .size = 0x200 },
5d360b
     [7] = { .feature = FEAT_7_0_EBX, .bits = CPUID_7_0_EBX_AVX512F,
5d360b
             .offset = 0x680, .size = 0x400 },
5d360b
+    [9] = { .feature = FEAT_7_0_ECX, .bits = CPUID_7_0_ECX_PKU,
5d360b
+            .offset = 0xA80, .size = 0x8 },
5d360b
 };
5d360b
 
5d360b
 const char *get_register_name_32(unsigned int reg)
5d360b
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
5d360b
index ac60309..7a12c0d 100644
5d360b
--- a/target-i386/cpu.h
5d360b
+++ b/target-i386/cpu.h
5d360b
@@ -394,6 +394,7 @@
5d360b
 #define XSTATE_OPMASK                   (1ULL << 5)
5d360b
 #define XSTATE_ZMM_Hi256                (1ULL << 6)
5d360b
 #define XSTATE_Hi16_ZMM                 (1ULL << 7)
5d360b
+#define XSTATE_PKRU                     (1ULL << 9)
5d360b
 
5d360b
 
5d360b
 /* CPUID feature words */
5d360b
@@ -586,6 +587,9 @@ typedef uint32_t FeatureWordArray[FEATURE_WORDS];
5d360b
 #define CPUID_7_0_EDX_AVX512_4VNNIW (1U << 2) /* AVX512 Neural Network Instructions */
5d360b
 #define CPUID_7_0_EDX_AVX512_4FMAPS (1U << 3) /* AVX512 Multiply Accumulation Single Precision */
5d360b
 
5d360b
+#define CPUID_7_0_ECX_PKU      (1U << 3)
5d360b
+#define CPUID_7_0_ECX_OSPKE    (1U << 4)
5d360b
+
5d360b
 #define CPUID_XSAVE_XSAVEOPT   (1U << 0)
5d360b
 #define CPUID_XSAVE_XSAVEC     (1U << 1)
5d360b
 #define CPUID_XSAVE_XGETBV1    (1U << 2)
5d360b
@@ -1029,6 +1033,8 @@ typedef struct CPUX86State {
5d360b
     uint64_t xcr0;
5d360b
     uint64_t xss;
5d360b
 
5d360b
+    uint32_t pkru;
5d360b
+
5d360b
     TPRAccess tpr_access_type;
5d360b
 } CPUX86State;
5d360b
 
5d360b
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
5d360b
index 6a479f4..0dc0e79 100644
5d360b
--- a/target-i386/kvm.c
5d360b
+++ b/target-i386/kvm.c
5d360b
@@ -1008,6 +1008,7 @@ static int kvm_put_fpu(X86CPU *cpu)
5d360b
 #define XSAVE_OPMASK      272
5d360b
 #define XSAVE_ZMM_Hi256   288
5d360b
 #define XSAVE_Hi16_ZMM    416
5d360b
+#define XSAVE_PKRU        672
5d360b
 
5d360b
 static int kvm_put_xsave(X86CPU *cpu)
5d360b
 {
5d360b
@@ -1051,6 +1052,7 @@ static int kvm_put_xsave(X86CPU *cpu)
5d360b
 #ifdef TARGET_X86_64
5d360b
     memcpy(&xsave->region[XSAVE_Hi16_ZMM], env->hi16_zmm_regs,
5d360b
             sizeof env->hi16_zmm_regs);
5d360b
+    memcpy(&xsave->region[XSAVE_PKRU], &env->pkru, sizeof env->pkru);
5d360b
 #endif
5d360b
     r = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XSAVE, xsave);
5d360b
     return r;
5d360b
@@ -1388,6 +1390,7 @@ static int kvm_get_xsave(X86CPU *cpu)
5d360b
 #ifdef TARGET_X86_64
5d360b
     memcpy(env->hi16_zmm_regs, &xsave->region[XSAVE_Hi16_ZMM],
5d360b
             sizeof env->hi16_zmm_regs);
5d360b
+    memcpy(&env->pkru, &xsave->region[XSAVE_PKRU], sizeof env->pkru);
5d360b
 #endif
5d360b
     return 0;
5d360b
 }
5d360b
diff --git a/target-i386/machine.c b/target-i386/machine.c
5d360b
index ce7fcd3..ba34088 100644
5d360b
--- a/target-i386/machine.c
5d360b
+++ b/target-i386/machine.c
5d360b
@@ -722,6 +722,26 @@ static const VMStateDescription vmstate_xss = {
5d360b
     }
5d360b
 };
5d360b
 
5d360b
+#ifdef TARGET_X86_64
5d360b
+static bool pkru_needed(void *opaque)
5d360b
+{
5d360b
+    X86CPU *cpu = opaque;
5d360b
+    CPUX86State *env = &cpu->env;
5d360b
+
5d360b
+    return env->pkru != 0;
5d360b
+}
5d360b
+
5d360b
+static const VMStateDescription vmstate_pkru = {
5d360b
+    .name = "cpu/pkru",
5d360b
+    .version_id = 1,
5d360b
+    .minimum_version_id = 1,
5d360b
+    .fields = (VMStateField[]){
5d360b
+        VMSTATE_UINT32(env.pkru, X86CPU),
5d360b
+        VMSTATE_END_OF_LIST()
5d360b
+    }
5d360b
+};
5d360b
+#endif
5d360b
+
5d360b
 const VMStateDescription vmstate_x86_cpu = {
5d360b
     .name = "cpu",
5d360b
     .version_id = 12,
5d360b
@@ -871,6 +891,11 @@ const VMStateDescription vmstate_x86_cpu = {
5d360b
          }, {
5d360b
             .vmsd = &vmstate_xss,
5d360b
             .needed = xss_needed,
5d360b
+#ifdef TARGET_X86_64
5d360b
+         }, {
5d360b
+            .vmsd = &vmstate_pkru,
5d360b
+            .needed = pkru_needed,
5d360b
+#endif
5d360b
         } , {
5d360b
             /* empty */
5d360b
         }
5d360b
-- 
5d360b
1.8.3.1
5d360b