1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PKRU_H
3 #define _ASM_X86_PKRU_H
5 #include <asm/fpu/xstate.h>
7 #define PKRU_AD_BIT 0x1
8 #define PKRU_WD_BIT 0x2
9 #define PKRU_BITS_PER_PKEY 2
11 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
12 extern u32 init_pkru_value;
14 #define init_pkru_value 0
17 static inline bool __pkru_allows_read(u32 pkru, u16 pkey)
19 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
20 return !(pkru & (PKRU_AD_BIT << pkru_pkey_bits));
23 static inline bool __pkru_allows_write(u32 pkru, u16 pkey)
25 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
27 * Access-disable disables writes too so we need to check
30 return !(pkru & ((PKRU_AD_BIT|PKRU_WD_BIT) << pkru_pkey_bits));
33 static inline u32 read_pkru(void)
35 if (boot_cpu_has(X86_FEATURE_OSPKE))
40 static inline void write_pkru(u32 pkru)
42 struct pkru_state *pk;
44 if (!boot_cpu_has(X86_FEATURE_OSPKE))
47 pk = get_xsave_addr(¤t->thread.fpu.state.xsave, XFEATURE_PKRU);
50 * The PKRU value in xstate needs to be in sync with the value that is
51 * written to the CPU. The FPU restore on return to userland would
52 * otherwise load the previous value again.