1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _PKEYS_HELPER_H
3 #define _PKEYS_HELPER_H
17 #define PKRU_BITS_PER_PKEY 2
22 #define DPRINT_IN_SIGNAL_BUF_SIZE 4096
23 extern int dprint_in_signal
;
24 extern char dprint_in_signal_buffer
[DPRINT_IN_SIGNAL_BUF_SIZE
];
25 static inline void sigsafe_printf(const char *format
, ...)
29 if (!dprint_in_signal
) {
36 * No printf() functions are signal-safe.
37 * They deadlock easily. Write the format
38 * string to get some output, even if
41 ret
= write(1, format
, strlen(format
));
46 #define dprintf_level(level, args...) do { \
47 if (level <= DEBUG_LEVEL) \
48 sigsafe_printf(args); \
50 #define dprintf0(args...) dprintf_level(0, args)
51 #define dprintf1(args...) dprintf_level(1, args)
52 #define dprintf2(args...) dprintf_level(2, args)
53 #define dprintf3(args...) dprintf_level(3, args)
54 #define dprintf4(args...) dprintf_level(4, args)
56 extern unsigned int shadow_pkru
;
57 static inline unsigned int __rdpkru(void)
59 unsigned int eax
, edx
;
63 asm volatile(".byte 0x0f,0x01,0xee\n\t"
64 : "=a" (eax
), "=d" (edx
)
70 static inline unsigned int _rdpkru(int line
)
72 unsigned int pkru
= __rdpkru();
74 dprintf4("rdpkru(line=%d) pkru: %x shadow: %x\n",
75 line
, pkru
, shadow_pkru
);
76 assert(pkru
== shadow_pkru
);
81 #define rdpkru() _rdpkru(__LINE__)
83 static inline void __wrpkru(unsigned int pkru
)
85 unsigned int eax
= pkru
;
89 dprintf4("%s() changing %08x to %08x\n", __func__
, __rdpkru(), pkru
);
90 asm volatile(".byte 0x0f,0x01,0xef\n\t"
91 : : "a" (eax
), "c" (ecx
), "d" (edx
));
92 assert(pkru
== __rdpkru());
95 static inline void wrpkru(unsigned int pkru
)
97 dprintf4("%s() changing %08x to %08x\n", __func__
, __rdpkru(), pkru
);
98 /* will do the shadow check for us: */
102 dprintf4("%s(%08x) pkru: %08x\n", __func__
, pkru
, __rdpkru());
106 * These are technically racy. since something could
107 * change PKRU between the read and the write.
109 static inline void __pkey_access_allow(int pkey
, int do_allow
)
111 unsigned int pkru
= rdpkru();
119 dprintf4("pkru now: %08x\n", rdpkru());
123 static inline void __pkey_write_allow(int pkey
, int do_allow_write
)
125 long pkru
= rdpkru();
126 int bit
= pkey
* 2 + 1;
134 dprintf4("pkru now: %08x\n", rdpkru());
137 #define PROT_PKEY0 0x10 /* protection key value (bit 0) */
138 #define PROT_PKEY1 0x20 /* protection key value (bit 1) */
139 #define PROT_PKEY2 0x40 /* protection key value (bit 2) */
140 #define PROT_PKEY3 0x80 /* protection key value (bit 3) */
142 #define PAGE_SIZE 4096
145 static inline void __cpuid(unsigned int *eax
, unsigned int *ebx
,
146 unsigned int *ecx
, unsigned int *edx
)
148 /* ecx is often an input as well as an output. */
155 : "0" (*eax
), "2" (*ecx
));
158 /* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx) */
159 #define X86_FEATURE_PKU (1<<3) /* Protection Keys for Userspace */
160 #define X86_FEATURE_OSPKE (1<<4) /* OS Protection Keys Enable */
162 static inline int cpu_has_pku(void)
171 __cpuid(&eax
, &ebx
, &ecx
, &edx
);
173 if (!(ecx
& X86_FEATURE_PKU
)) {
174 dprintf2("cpu does not have PKU\n");
177 if (!(ecx
& X86_FEATURE_OSPKE
)) {
178 dprintf2("cpu does not have OSPKE\n");
184 #define XSTATE_PKRU_BIT (9)
185 #define XSTATE_PKRU 0x200
187 int pkru_xstate_offset(void)
195 unsigned long XSTATE_CPUID
= 0xd;
198 /* assume that XSTATE_PKRU is set in XCR0 */
199 leaf
= XSTATE_PKRU_BIT
;
203 __cpuid(&eax
, &ebx
, &ecx
, &edx
);
205 if (leaf
== XSTATE_PKRU_BIT
) {
211 if (xstate_size
== 0) {
212 printf("could not find size/offset of PKRU in xsave state\n");
216 return xstate_offset
;
219 #endif /* _PKEYS_HELPER_H */