x86: pkey: introduce write_pkru() for KVM
authorXiao Guangrong <guangrong.xiao@linux.intel.com>
Tue, 22 Mar 2016 08:51:17 +0000 (16:51 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 22 Mar 2016 15:21:05 +0000 (16:21 +0100)
KVM will use it to switch pkru between guest and host.

CC: Ingo Molnar <mingo@redhat.com>
CC: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Huaitong Han <huaitong.han@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/include/asm/pgtable.h
arch/x86/include/asm/special_insns.h

index 1ff49ec29ecede42b78c49132e431ea4ed193026..97f3242e133ccc9c2866baade8ca4f82ade65f04 100644 (file)
@@ -107,6 +107,12 @@ static inline u32 read_pkru(void)
        return 0;
 }
 
+static inline void write_pkru(u32 pkru)
+{
+       if (boot_cpu_has(X86_FEATURE_OSPKE))
+               __write_pkru(pkru);
+}
+
 static inline int pte_young(pte_t pte)
 {
        return pte_flags(pte) & _PAGE_ACCESSED;
index aee6e76e561ec0cd24cfe1f92f4dc9cf6e24ece4..d96d0437776569f5c9c0e6f28d125dbc5671d037 100644 (file)
@@ -113,11 +113,27 @@ static inline u32 __read_pkru(void)
                     : "c" (ecx));
        return pkru;
 }
+
+static inline void __write_pkru(u32 pkru)
+{
+       u32 ecx = 0, edx = 0;
+
+       /*
+        * "wrpkru" instruction.  Loads contents in EAX to PKRU,
+        * requires that ecx = edx = 0.
+        */
+       asm volatile(".byte 0x0f,0x01,0xef\n\t"
+                    : : "a" (pkru), "c"(ecx), "d"(edx));
+}
 #else
 static inline u32 __read_pkru(void)
 {
        return 0;
 }
+
+static inline void __write_pkru(u32 pkru)
+{
+}
 #endif
 
 static inline void native_wbinvd(void)