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
) {
33 int len
= vsnprintf(dprint_in_signal_buffer
,
34 DPRINT_IN_SIGNAL_BUF_SIZE
,
37 * len is amount that would have been printed,
38 * but actual write is truncated at BUF_SIZE.
40 if (len
> DPRINT_IN_SIGNAL_BUF_SIZE
)
41 len
= DPRINT_IN_SIGNAL_BUF_SIZE
;
42 write(1, dprint_in_signal_buffer
, len
);
46 #define dprintf_level(level, args...) do { \
47 if (level <= DEBUG_LEVEL) \
48 sigsafe_printf(args); \
51 #define dprintf0(args...) dprintf_level(0, args)
52 #define dprintf1(args...) dprintf_level(1, args)
53 #define dprintf2(args...) dprintf_level(2, args)
54 #define dprintf3(args...) dprintf_level(3, args)
55 #define dprintf4(args...) dprintf_level(4, args)
57 extern unsigned int shadow_pkru
;
58 static inline unsigned int __rdpkru(void)
60 unsigned int eax
, edx
;
64 asm volatile(".byte 0x0f,0x01,0xee\n\t"
65 : "=a" (eax
), "=d" (edx
)
71 static inline unsigned int _rdpkru(int line
)
73 unsigned int pkru
= __rdpkru();
75 dprintf4("rdpkru(line=%d) pkru: %x shadow: %x\n",
76 line
, pkru
, shadow_pkru
);
77 assert(pkru
== shadow_pkru
);
82 #define rdpkru() _rdpkru(__LINE__)
84 static inline void __wrpkru(unsigned int pkru
)
86 unsigned int eax
= pkru
;
90 dprintf4("%s() changing %08x to %08x\n", __func__
, __rdpkru(), pkru
);
91 asm volatile(".byte 0x0f,0x01,0xef\n\t"
92 : : "a" (eax
), "c" (ecx
), "d" (edx
));
93 assert(pkru
== __rdpkru());
96 static inline void wrpkru(unsigned int pkru
)
98 dprintf4("%s() changing %08x to %08x\n", __func__
, __rdpkru(), pkru
);
99 /* will do the shadow check for us: */
103 dprintf4("%s(%08x) pkru: %08x\n", __func__
, pkru
, __rdpkru());
107 * These are technically racy. since something could
108 * change PKRU between the read and the write.
110 static inline void __pkey_access_allow(int pkey
, int do_allow
)
112 unsigned int pkru
= rdpkru();
120 dprintf4("pkru now: %08x\n", rdpkru());
124 static inline void __pkey_write_allow(int pkey
, int do_allow_write
)
126 long pkru
= rdpkru();
127 int bit
= pkey
* 2 + 1;
135 dprintf4("pkru now: %08x\n", rdpkru());
138 #define PROT_PKEY0 0x10 /* protection key value (bit 0) */
139 #define PROT_PKEY1 0x20 /* protection key value (bit 1) */
140 #define PROT_PKEY2 0x40 /* protection key value (bit 2) */
141 #define PROT_PKEY3 0x80 /* protection key value (bit 3) */
143 #define PAGE_SIZE 4096
146 static inline void __cpuid(unsigned int *eax
, unsigned int *ebx
,
147 unsigned int *ecx
, unsigned int *edx
)
149 /* ecx is often an input as well as an output. */
156 : "0" (*eax
), "2" (*ecx
));
159 /* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx) */
160 #define X86_FEATURE_PKU (1<<3) /* Protection Keys for Userspace */
161 #define X86_FEATURE_OSPKE (1<<4) /* OS Protection Keys Enable */
163 static inline int cpu_has_pku(void)
172 __cpuid(&eax
, &ebx
, &ecx
, &edx
);
174 if (!(ecx
& X86_FEATURE_PKU
)) {
175 dprintf2("cpu does not have PKU\n");
178 if (!(ecx
& X86_FEATURE_OSPKE
)) {
179 dprintf2("cpu does not have OSPKE\n");
185 #define XSTATE_PKRU_BIT (9)
186 #define XSTATE_PKRU 0x200
188 int pkru_xstate_offset(void)
196 unsigned long XSTATE_CPUID
= 0xd;
199 /* assume that XSTATE_PKRU is set in XCR0 */
200 leaf
= XSTATE_PKRU_BIT
;
204 __cpuid(&eax
, &ebx
, &ecx
, &edx
);
206 if (leaf
== XSTATE_PKRU_BIT
) {
212 if (xstate_size
== 0) {
213 printf("could not find size/offset of PKRU in xsave state\n");
217 return xstate_offset
;
220 #endif /* _PKEYS_HELPER_H */