staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / x86 / pkey-helpers.h
blob254e5436bdd9926091dc7c53bdd37be8bdb19121
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _PKEYS_HELPER_H
3 #define _PKEYS_HELPER_H
4 #define _GNU_SOURCE
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <signal.h>
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <ucontext.h>
14 #include <sys/mman.h>
16 #define NR_PKEYS 16
17 #define PKRU_BITS_PER_PKEY 2
19 #ifndef DEBUG_LEVEL
20 #define DEBUG_LEVEL 0
21 #endif
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, ...)
27 va_list ap;
29 if (!dprint_in_signal) {
30 va_start(ap, format);
31 vprintf(format, ap);
32 va_end(ap);
33 } else {
34 int ret;
36 * No printf() functions are signal-safe.
37 * They deadlock easily. Write the format
38 * string to get some output, even if
39 * incomplete.
41 ret = write(1, format, strlen(format));
42 if (ret < 0)
43 exit(1);
46 #define dprintf_level(level, args...) do { \
47 if (level <= DEBUG_LEVEL) \
48 sigsafe_printf(args); \
49 } while (0)
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;
60 unsigned int ecx = 0;
61 unsigned int pkru;
63 asm volatile(".byte 0x0f,0x01,0xee\n\t"
64 : "=a" (eax), "=d" (edx)
65 : "c" (ecx));
66 pkru = eax;
67 return pkru;
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);
78 return pkru;
81 #define rdpkru() _rdpkru(__LINE__)
83 static inline void __wrpkru(unsigned int pkru)
85 unsigned int eax = pkru;
86 unsigned int ecx = 0;
87 unsigned int edx = 0;
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: */
99 rdpkru();
100 __wrpkru(pkru);
101 shadow_pkru = pkru;
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();
112 int bit = pkey * 2;
114 if (do_allow)
115 pkru &= (1<<bit);
116 else
117 pkru |= (1<<bit);
119 dprintf4("pkru now: %08x\n", rdpkru());
120 wrpkru(pkru);
123 static inline void __pkey_write_allow(int pkey, int do_allow_write)
125 long pkru = rdpkru();
126 int bit = pkey * 2 + 1;
128 if (do_allow_write)
129 pkru &= (1<<bit);
130 else
131 pkru |= (1<<bit);
133 wrpkru(pkru);
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
143 #define MB (1<<20)
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. */
149 asm volatile(
150 "cpuid;"
151 : "=a" (*eax),
152 "=b" (*ebx),
153 "=c" (*ecx),
154 "=d" (*edx)
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)
164 unsigned int eax;
165 unsigned int ebx;
166 unsigned int ecx;
167 unsigned int edx;
169 eax = 0x7;
170 ecx = 0x0;
171 __cpuid(&eax, &ebx, &ecx, &edx);
173 if (!(ecx & X86_FEATURE_PKU)) {
174 dprintf2("cpu does not have PKU\n");
175 return 0;
177 if (!(ecx & X86_FEATURE_OSPKE)) {
178 dprintf2("cpu does not have OSPKE\n");
179 return 0;
181 return 1;
184 #define XSTATE_PKRU_BIT (9)
185 #define XSTATE_PKRU 0x200
187 int pkru_xstate_offset(void)
189 unsigned int eax;
190 unsigned int ebx;
191 unsigned int ecx;
192 unsigned int edx;
193 int xstate_offset;
194 int xstate_size;
195 unsigned long XSTATE_CPUID = 0xd;
196 int leaf;
198 /* assume that XSTATE_PKRU is set in XCR0 */
199 leaf = XSTATE_PKRU_BIT;
201 eax = XSTATE_CPUID;
202 ecx = leaf;
203 __cpuid(&eax, &ebx, &ecx, &edx);
205 if (leaf == XSTATE_PKRU_BIT) {
206 xstate_offset = ebx;
207 xstate_size = eax;
211 if (xstate_size == 0) {
212 printf("could not find size/offset of PKRU in xsave state\n");
213 return 0;
216 return xstate_offset;
219 #endif /* _PKEYS_HELPER_H */