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
, ...)
30 if (!dprint_in_signal
) {
34 int len
= vsnprintf(dprint_in_signal_buffer
,
35 DPRINT_IN_SIGNAL_BUF_SIZE
,
38 * len is amount that would have been printed,
39 * but actual write is truncated at BUF_SIZE.
41 if (len
> DPRINT_IN_SIGNAL_BUF_SIZE
)
42 len
= DPRINT_IN_SIGNAL_BUF_SIZE
;
43 ret
= write(1, dprint_in_signal_buffer
, len
);
49 #define dprintf_level(level, args...) do { \
50 if (level <= DEBUG_LEVEL) \
51 sigsafe_printf(args); \
54 #define dprintf0(args...) dprintf_level(0, args)
55 #define dprintf1(args...) dprintf_level(1, args)
56 #define dprintf2(args...) dprintf_level(2, args)
57 #define dprintf3(args...) dprintf_level(3, args)
58 #define dprintf4(args...) dprintf_level(4, args)
60 extern unsigned int shadow_pkru
;
61 static inline unsigned int __rdpkru(void)
63 unsigned int eax
, edx
;
67 asm volatile(".byte 0x0f,0x01,0xee\n\t"
68 : "=a" (eax
), "=d" (edx
)
74 static inline unsigned int _rdpkru(int line
)
76 unsigned int pkru
= __rdpkru();
78 dprintf4("rdpkru(line=%d) pkru: %x shadow: %x\n",
79 line
, pkru
, shadow_pkru
);
80 assert(pkru
== shadow_pkru
);
85 #define rdpkru() _rdpkru(__LINE__)
87 static inline void __wrpkru(unsigned int pkru
)
89 unsigned int eax
= pkru
;
93 dprintf4("%s() changing %08x to %08x\n", __func__
, __rdpkru(), pkru
);
94 asm volatile(".byte 0x0f,0x01,0xef\n\t"
95 : : "a" (eax
), "c" (ecx
), "d" (edx
));
96 assert(pkru
== __rdpkru());
99 static inline void wrpkru(unsigned int pkru
)
101 dprintf4("%s() changing %08x to %08x\n", __func__
, __rdpkru(), pkru
);
102 /* will do the shadow check for us: */
106 dprintf4("%s(%08x) pkru: %08x\n", __func__
, pkru
, __rdpkru());
110 * These are technically racy. since something could
111 * change PKRU between the read and the write.
113 static inline void __pkey_access_allow(int pkey
, int do_allow
)
115 unsigned int pkru
= rdpkru();
123 dprintf4("pkru now: %08x\n", rdpkru());
127 static inline void __pkey_write_allow(int pkey
, int do_allow_write
)
129 long pkru
= rdpkru();
130 int bit
= pkey
* 2 + 1;
138 dprintf4("pkru now: %08x\n", rdpkru());
141 #define PROT_PKEY0 0x10 /* protection key value (bit 0) */
142 #define PROT_PKEY1 0x20 /* protection key value (bit 1) */
143 #define PROT_PKEY2 0x40 /* protection key value (bit 2) */
144 #define PROT_PKEY3 0x80 /* protection key value (bit 3) */
146 #define PAGE_SIZE 4096
149 static inline void __cpuid(unsigned int *eax
, unsigned int *ebx
,
150 unsigned int *ecx
, unsigned int *edx
)
152 /* ecx is often an input as well as an output. */
159 : "0" (*eax
), "2" (*ecx
));
162 /* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx) */
163 #define X86_FEATURE_PKU (1<<3) /* Protection Keys for Userspace */
164 #define X86_FEATURE_OSPKE (1<<4) /* OS Protection Keys Enable */
166 static inline int cpu_has_pku(void)
175 __cpuid(&eax
, &ebx
, &ecx
, &edx
);
177 if (!(ecx
& X86_FEATURE_PKU
)) {
178 dprintf2("cpu does not have PKU\n");
181 if (!(ecx
& X86_FEATURE_OSPKE
)) {
182 dprintf2("cpu does not have OSPKE\n");
188 #define XSTATE_PKRU_BIT (9)
189 #define XSTATE_PKRU 0x200
191 int pkru_xstate_offset(void)
199 unsigned long XSTATE_CPUID
= 0xd;
202 /* assume that XSTATE_PKRU is set in XCR0 */
203 leaf
= XSTATE_PKRU_BIT
;
207 __cpuid(&eax
, &ebx
, &ecx
, &edx
);
209 if (leaf
== XSTATE_PKRU_BIT
) {
215 if (xstate_size
== 0) {
216 printf("could not find size/offset of PKRU in xsave state\n");
220 return xstate_offset
;
223 #endif /* _PKEYS_HELPER_H */