x86/mm/pkeys: Add functions to fetch PKRU
authorDave Hansen <[email protected]>
Fri, 12 Feb 2016 21:02:15 +0000 (13:02 -0800)
committerIngo Molnar <[email protected]>
Thu, 18 Feb 2016 08:32:43 +0000 (09:32 +0100)
This adds the raw instruction to access PKRU as well as some
accessor functions that correctly handle when the CPU does not
support the instruction.  We don't use it here, but we will use
read_pkru() in the next patch.

Signed-off-by: Dave Hansen <[email protected]>
Reviewed-by: Thomas Gleixner <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Brian Gerst <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Denys Vlasenko <[email protected]>
Cc: H. Peter Anvin <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Rik van Riel <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
arch/x86/include/asm/pgtable.h
arch/x86/include/asm/special_insns.h

index 0687c4748b8f87690471c94bade518d2b768242d..e997dcc6ee2bec19d7918deec02542db68335a4b 100644 (file)
@@ -99,6 +99,14 @@ static inline int pte_dirty(pte_t pte)
        return pte_flags(pte) & _PAGE_DIRTY;
 }
 
+
+static inline u32 read_pkru(void)
+{
+       if (boot_cpu_has(X86_FEATURE_OSPKE))
+               return __read_pkru();
+       return 0;
+}
+
 static inline int pte_young(pte_t pte)
 {
        return pte_flags(pte) & _PAGE_ACCESSED;
index 2270e41b32fd856e226840d4f76dddaa7852d13d..aee6e76e561ec0cd24cfe1f92f4dc9cf6e24ece4 100644 (file)
@@ -98,6 +98,28 @@ static inline void native_write_cr8(unsigned long val)
 }
 #endif
 
+#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
+static inline u32 __read_pkru(void)
+{
+       u32 ecx = 0;
+       u32 edx, pkru;
+
+       /*
+        * "rdpkru" instruction.  Places PKRU contents in to EAX,
+        * clears EDX and requires that ecx=0.
+        */
+       asm volatile(".byte 0x0f,0x01,0xee\n\t"
+                    : "=a" (pkru), "=d" (edx)
+                    : "c" (ecx));
+       return pkru;
+}
+#else
+static inline u32 __read_pkru(void)
+{
+       return 0;
+}
+#endif
+
 static inline void native_wbinvd(void)
 {
        asm volatile("wbinvd": : :"memory");