power: supply: bq24190_charger: Add disable-reset device-property
[linux/fpc-iii.git] / tools / testing / selftests / x86 / protection_keys.c
blob2842a5fa22b33c0b1e94cfb9f8971c95de30d68f
1 /*
2 * Tests x86 Memory Protection Keys (see Documentation/x86/protection-keys.txt)
4 * There are examples in here of:
5 * * how to set protection keys on memory
6 * * how to set/clear bits in PKRU (the rights register)
7 * * how to handle SEGV_PKRU signals and extract pkey-relevant
8 * information from the siginfo
10 * Things to add:
11 * make sure KSM and KSM COW breaking works
12 * prefault pages in at malloc, or not
13 * protect MPX bounds tables with protection keys?
14 * make sure VMA splitting/merging is working correctly
15 * OOMs can destroy mm->mmap (see exit_mmap()), so make sure it is immune to pkeys
16 * look for pkey "leaks" where it is still set on a VMA but "freed" back to the kernel
17 * do a plain mprotect() to a mprotect_pkey() area and make sure the pkey sticks
19 * Compile like this:
20 * gcc -o protection_keys -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm
21 * gcc -m32 -o protection_keys_32 -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm
23 #define _GNU_SOURCE
24 #include <errno.h>
25 #include <linux/futex.h>
26 #include <sys/time.h>
27 #include <sys/syscall.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <stdbool.h>
32 #include <signal.h>
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <ucontext.h>
36 #include <sys/mman.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <sys/ptrace.h>
43 #include <setjmp.h>
45 #include "pkey-helpers.h"
47 int iteration_nr = 1;
48 int test_nr;
50 unsigned int shadow_pkru;
52 #define HPAGE_SIZE (1UL<<21)
53 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
54 #define ALIGN_UP(x, align_to) (((x) + ((align_to)-1)) & ~((align_to)-1))
55 #define ALIGN_DOWN(x, align_to) ((x) & ~((align_to)-1))
56 #define ALIGN_PTR_UP(p, ptr_align_to) ((typeof(p))ALIGN_UP((unsigned long)(p), ptr_align_to))
57 #define ALIGN_PTR_DOWN(p, ptr_align_to) ((typeof(p))ALIGN_DOWN((unsigned long)(p), ptr_align_to))
58 #define __stringify_1(x...) #x
59 #define __stringify(x...) __stringify_1(x)
61 #define PTR_ERR_ENOTSUP ((void *)-ENOTSUP)
63 int dprint_in_signal;
64 char dprint_in_signal_buffer[DPRINT_IN_SIGNAL_BUF_SIZE];
66 extern void abort_hooks(void);
67 #define pkey_assert(condition) do { \
68 if (!(condition)) { \
69 dprintf0("assert() at %s::%d test_nr: %d iteration: %d\n", \
70 __FILE__, __LINE__, \
71 test_nr, iteration_nr); \
72 dprintf0("errno at assert: %d", errno); \
73 abort_hooks(); \
74 assert(condition); \
75 } \
76 } while (0)
77 #define raw_assert(cond) assert(cond)
79 void cat_into_file(char *str, char *file)
81 int fd = open(file, O_RDWR);
82 int ret;
84 dprintf2("%s(): writing '%s' to '%s'\n", __func__, str, file);
86 * these need to be raw because they are called under
87 * pkey_assert()
89 raw_assert(fd >= 0);
90 ret = write(fd, str, strlen(str));
91 if (ret != strlen(str)) {
92 perror("write to file failed");
93 fprintf(stderr, "filename: '%s' str: '%s'\n", file, str);
94 raw_assert(0);
96 close(fd);
99 #if CONTROL_TRACING > 0
100 static int warned_tracing;
101 int tracing_root_ok(void)
103 if (geteuid() != 0) {
104 if (!warned_tracing)
105 fprintf(stderr, "WARNING: not run as root, "
106 "can not do tracing control\n");
107 warned_tracing = 1;
108 return 0;
110 return 1;
112 #endif
114 void tracing_on(void)
116 #if CONTROL_TRACING > 0
117 #define TRACEDIR "/sys/kernel/debug/tracing"
118 char pidstr[32];
120 if (!tracing_root_ok())
121 return;
123 sprintf(pidstr, "%d", getpid());
124 cat_into_file("0", TRACEDIR "/tracing_on");
125 cat_into_file("\n", TRACEDIR "/trace");
126 if (1) {
127 cat_into_file("function_graph", TRACEDIR "/current_tracer");
128 cat_into_file("1", TRACEDIR "/options/funcgraph-proc");
129 } else {
130 cat_into_file("nop", TRACEDIR "/current_tracer");
132 cat_into_file(pidstr, TRACEDIR "/set_ftrace_pid");
133 cat_into_file("1", TRACEDIR "/tracing_on");
134 dprintf1("enabled tracing\n");
135 #endif
138 void tracing_off(void)
140 #if CONTROL_TRACING > 0
141 if (!tracing_root_ok())
142 return;
143 cat_into_file("0", "/sys/kernel/debug/tracing/tracing_on");
144 #endif
147 void abort_hooks(void)
149 fprintf(stderr, "running %s()...\n", __func__);
150 tracing_off();
151 #ifdef SLEEP_ON_ABORT
152 sleep(SLEEP_ON_ABORT);
153 #endif
156 static inline void __page_o_noops(void)
158 /* 8-bytes of instruction * 512 bytes = 1 page */
159 asm(".rept 512 ; nopl 0x7eeeeeee(%eax) ; .endr");
163 * This attempts to have roughly a page of instructions followed by a few
164 * instructions that do a write, and another page of instructions. That
165 * way, we are pretty sure that the write is in the second page of
166 * instructions and has at least a page of padding behind it.
168 * *That* lets us be sure to madvise() away the write instruction, which
169 * will then fault, which makes sure that the fault code handles
170 * execute-only memory properly.
172 __attribute__((__aligned__(PAGE_SIZE)))
173 void lots_o_noops_around_write(int *write_to_me)
175 dprintf3("running %s()\n", __func__);
176 __page_o_noops();
177 /* Assume this happens in the second page of instructions: */
178 *write_to_me = __LINE__;
179 /* pad out by another page: */
180 __page_o_noops();
181 dprintf3("%s() done\n", __func__);
184 /* Define some kernel-like types */
185 #define u8 uint8_t
186 #define u16 uint16_t
187 #define u32 uint32_t
188 #define u64 uint64_t
190 #ifdef __i386__
191 #define SYS_mprotect_key 380
192 #define SYS_pkey_alloc 381
193 #define SYS_pkey_free 382
194 #define REG_IP_IDX REG_EIP
195 #define si_pkey_offset 0x18
196 #else
197 #define SYS_mprotect_key 329
198 #define SYS_pkey_alloc 330
199 #define SYS_pkey_free 331
200 #define REG_IP_IDX REG_RIP
201 #define si_pkey_offset 0x20
202 #endif
204 void dump_mem(void *dumpme, int len_bytes)
206 char *c = (void *)dumpme;
207 int i;
209 for (i = 0; i < len_bytes; i += sizeof(u64)) {
210 u64 *ptr = (u64 *)(c + i);
211 dprintf1("dump[%03d][@%p]: %016jx\n", i, ptr, *ptr);
215 #define __SI_FAULT (3 << 16)
216 #define SEGV_BNDERR (__SI_FAULT|3) /* failed address bound checks */
217 #define SEGV_PKUERR (__SI_FAULT|4)
219 static char *si_code_str(int si_code)
221 if (si_code & SEGV_MAPERR)
222 return "SEGV_MAPERR";
223 if (si_code & SEGV_ACCERR)
224 return "SEGV_ACCERR";
225 if (si_code & SEGV_BNDERR)
226 return "SEGV_BNDERR";
227 if (si_code & SEGV_PKUERR)
228 return "SEGV_PKUERR";
229 return "UNKNOWN";
232 int pkru_faults;
233 int last_si_pkey = -1;
234 void signal_handler(int signum, siginfo_t *si, void *vucontext)
236 ucontext_t *uctxt = vucontext;
237 int trapno;
238 unsigned long ip;
239 char *fpregs;
240 u32 *pkru_ptr;
241 u64 si_pkey;
242 u32 *si_pkey_ptr;
243 int pkru_offset;
244 fpregset_t fpregset;
246 dprint_in_signal = 1;
247 dprintf1(">>>>===============SIGSEGV============================\n");
248 dprintf1("%s()::%d, pkru: 0x%x shadow: %x\n", __func__, __LINE__,
249 __rdpkru(), shadow_pkru);
251 trapno = uctxt->uc_mcontext.gregs[REG_TRAPNO];
252 ip = uctxt->uc_mcontext.gregs[REG_IP_IDX];
253 fpregset = uctxt->uc_mcontext.fpregs;
254 fpregs = (void *)fpregset;
256 dprintf2("%s() trapno: %d ip: 0x%lx info->si_code: %s/%d\n", __func__,
257 trapno, ip, si_code_str(si->si_code), si->si_code);
258 #ifdef __i386__
260 * 32-bit has some extra padding so that userspace can tell whether
261 * the XSTATE header is present in addition to the "legacy" FPU
262 * state. We just assume that it is here.
264 fpregs += 0x70;
265 #endif
266 pkru_offset = pkru_xstate_offset();
267 pkru_ptr = (void *)(&fpregs[pkru_offset]);
269 dprintf1("siginfo: %p\n", si);
270 dprintf1(" fpregs: %p\n", fpregs);
272 * If we got a PKRU fault, we *HAVE* to have at least one bit set in
273 * here.
275 dprintf1("pkru_xstate_offset: %d\n", pkru_xstate_offset());
276 if (DEBUG_LEVEL > 4)
277 dump_mem(pkru_ptr - 128, 256);
278 pkey_assert(*pkru_ptr);
280 si_pkey_ptr = (u32 *)(((u8 *)si) + si_pkey_offset);
281 dprintf1("si_pkey_ptr: %p\n", si_pkey_ptr);
282 dump_mem(si_pkey_ptr - 8, 24);
283 si_pkey = *si_pkey_ptr;
284 pkey_assert(si_pkey < NR_PKEYS);
285 last_si_pkey = si_pkey;
287 if ((si->si_code == SEGV_MAPERR) ||
288 (si->si_code == SEGV_ACCERR) ||
289 (si->si_code == SEGV_BNDERR)) {
290 printf("non-PK si_code, exiting...\n");
291 exit(4);
294 dprintf1("signal pkru from xsave: %08x\n", *pkru_ptr);
295 /* need __rdpkru() version so we do not do shadow_pkru checking */
296 dprintf1("signal pkru from pkru: %08x\n", __rdpkru());
297 dprintf1("si_pkey from siginfo: %jx\n", si_pkey);
298 *(u64 *)pkru_ptr = 0x00000000;
299 dprintf1("WARNING: set PRKU=0 to allow faulting instruction to continue\n");
300 pkru_faults++;
301 dprintf1("<<<<==================================================\n");
302 return;
303 if (trapno == 14) {
304 fprintf(stderr,
305 "ERROR: In signal handler, page fault, trapno = %d, ip = %016lx\n",
306 trapno, ip);
307 fprintf(stderr, "si_addr %p\n", si->si_addr);
308 fprintf(stderr, "REG_ERR: %lx\n",
309 (unsigned long)uctxt->uc_mcontext.gregs[REG_ERR]);
310 exit(1);
311 } else {
312 fprintf(stderr, "unexpected trap %d! at 0x%lx\n", trapno, ip);
313 fprintf(stderr, "si_addr %p\n", si->si_addr);
314 fprintf(stderr, "REG_ERR: %lx\n",
315 (unsigned long)uctxt->uc_mcontext.gregs[REG_ERR]);
316 exit(2);
318 dprint_in_signal = 0;
321 int wait_all_children(void)
323 int status;
324 return waitpid(-1, &status, 0);
327 void sig_chld(int x)
329 dprint_in_signal = 1;
330 dprintf2("[%d] SIGCHLD: %d\n", getpid(), x);
331 dprint_in_signal = 0;
334 void setup_sigsegv_handler(void)
336 int r, rs;
337 struct sigaction newact;
338 struct sigaction oldact;
340 /* #PF is mapped to sigsegv */
341 int signum = SIGSEGV;
343 newact.sa_handler = 0;
344 newact.sa_sigaction = signal_handler;
346 /*sigset_t - signals to block while in the handler */
347 /* get the old signal mask. */
348 rs = sigprocmask(SIG_SETMASK, 0, &newact.sa_mask);
349 pkey_assert(rs == 0);
351 /* call sa_sigaction, not sa_handler*/
352 newact.sa_flags = SA_SIGINFO;
354 newact.sa_restorer = 0; /* void(*)(), obsolete */
355 r = sigaction(signum, &newact, &oldact);
356 r = sigaction(SIGALRM, &newact, &oldact);
357 pkey_assert(r == 0);
360 void setup_handlers(void)
362 signal(SIGCHLD, &sig_chld);
363 setup_sigsegv_handler();
366 pid_t fork_lazy_child(void)
368 pid_t forkret;
370 forkret = fork();
371 pkey_assert(forkret >= 0);
372 dprintf3("[%d] fork() ret: %d\n", getpid(), forkret);
374 if (!forkret) {
375 /* in the child */
376 while (1) {
377 dprintf1("child sleeping...\n");
378 sleep(30);
381 return forkret;
384 #define PKEY_DISABLE_ACCESS 0x1
385 #define PKEY_DISABLE_WRITE 0x2
387 u32 pkey_get(int pkey, unsigned long flags)
389 u32 mask = (PKEY_DISABLE_ACCESS|PKEY_DISABLE_WRITE);
390 u32 pkru = __rdpkru();
391 u32 shifted_pkru;
392 u32 masked_pkru;
394 dprintf1("%s(pkey=%d, flags=%lx) = %x / %d\n",
395 __func__, pkey, flags, 0, 0);
396 dprintf2("%s() raw pkru: %x\n", __func__, pkru);
398 shifted_pkru = (pkru >> (pkey * PKRU_BITS_PER_PKEY));
399 dprintf2("%s() shifted_pkru: %x\n", __func__, shifted_pkru);
400 masked_pkru = shifted_pkru & mask;
401 dprintf2("%s() masked pkru: %x\n", __func__, masked_pkru);
403 * shift down the relevant bits to the lowest two, then
404 * mask off all the other high bits.
406 return masked_pkru;
409 int pkey_set(int pkey, unsigned long rights, unsigned long flags)
411 u32 mask = (PKEY_DISABLE_ACCESS|PKEY_DISABLE_WRITE);
412 u32 old_pkru = __rdpkru();
413 u32 new_pkru;
415 /* make sure that 'rights' only contains the bits we expect: */
416 assert(!(rights & ~mask));
418 /* copy old pkru */
419 new_pkru = old_pkru;
420 /* mask out bits from pkey in old value: */
421 new_pkru &= ~(mask << (pkey * PKRU_BITS_PER_PKEY));
422 /* OR in new bits for pkey: */
423 new_pkru |= (rights << (pkey * PKRU_BITS_PER_PKEY));
425 __wrpkru(new_pkru);
427 dprintf3("%s(pkey=%d, rights=%lx, flags=%lx) = %x pkru now: %x old_pkru: %x\n",
428 __func__, pkey, rights, flags, 0, __rdpkru(), old_pkru);
429 return 0;
432 void pkey_disable_set(int pkey, int flags)
434 unsigned long syscall_flags = 0;
435 int ret;
436 int pkey_rights;
437 u32 orig_pkru;
439 dprintf1("START->%s(%d, 0x%x)\n", __func__,
440 pkey, flags);
441 pkey_assert(flags & (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
443 pkey_rights = pkey_get(pkey, syscall_flags);
445 dprintf1("%s(%d) pkey_get(%d): %x\n", __func__,
446 pkey, pkey, pkey_rights);
447 pkey_assert(pkey_rights >= 0);
449 pkey_rights |= flags;
451 ret = pkey_set(pkey, pkey_rights, syscall_flags);
452 assert(!ret);
453 /*pkru and flags have the same format */
454 shadow_pkru |= flags << (pkey * 2);
455 dprintf1("%s(%d) shadow: 0x%x\n", __func__, pkey, shadow_pkru);
457 pkey_assert(ret >= 0);
459 pkey_rights = pkey_get(pkey, syscall_flags);
460 dprintf1("%s(%d) pkey_get(%d): %x\n", __func__,
461 pkey, pkey, pkey_rights);
463 dprintf1("%s(%d) pkru: 0x%x\n", __func__, pkey, rdpkru());
464 if (flags)
465 pkey_assert(rdpkru() > orig_pkru);
466 dprintf1("END<---%s(%d, 0x%x)\n", __func__,
467 pkey, flags);
470 void pkey_disable_clear(int pkey, int flags)
472 unsigned long syscall_flags = 0;
473 int ret;
474 int pkey_rights = pkey_get(pkey, syscall_flags);
475 u32 orig_pkru = rdpkru();
477 pkey_assert(flags & (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
479 dprintf1("%s(%d) pkey_get(%d): %x\n", __func__,
480 pkey, pkey, pkey_rights);
481 pkey_assert(pkey_rights >= 0);
483 pkey_rights |= flags;
485 ret = pkey_set(pkey, pkey_rights, 0);
486 /* pkru and flags have the same format */
487 shadow_pkru &= ~(flags << (pkey * 2));
488 pkey_assert(ret >= 0);
490 pkey_rights = pkey_get(pkey, syscall_flags);
491 dprintf1("%s(%d) pkey_get(%d): %x\n", __func__,
492 pkey, pkey, pkey_rights);
494 dprintf1("%s(%d) pkru: 0x%x\n", __func__, pkey, rdpkru());
495 if (flags)
496 assert(rdpkru() > orig_pkru);
499 void pkey_write_allow(int pkey)
501 pkey_disable_clear(pkey, PKEY_DISABLE_WRITE);
503 void pkey_write_deny(int pkey)
505 pkey_disable_set(pkey, PKEY_DISABLE_WRITE);
507 void pkey_access_allow(int pkey)
509 pkey_disable_clear(pkey, PKEY_DISABLE_ACCESS);
511 void pkey_access_deny(int pkey)
513 pkey_disable_set(pkey, PKEY_DISABLE_ACCESS);
516 int sys_mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
517 unsigned long pkey)
519 int sret;
521 dprintf2("%s(0x%p, %zx, prot=%lx, pkey=%lx)\n", __func__,
522 ptr, size, orig_prot, pkey);
524 errno = 0;
525 sret = syscall(SYS_mprotect_key, ptr, size, orig_prot, pkey);
526 if (errno) {
527 dprintf2("SYS_mprotect_key sret: %d\n", sret);
528 dprintf2("SYS_mprotect_key prot: 0x%lx\n", orig_prot);
529 dprintf2("SYS_mprotect_key failed, errno: %d\n", errno);
530 if (DEBUG_LEVEL >= 2)
531 perror("SYS_mprotect_pkey");
533 return sret;
536 int sys_pkey_alloc(unsigned long flags, unsigned long init_val)
538 int ret = syscall(SYS_pkey_alloc, flags, init_val);
539 dprintf1("%s(flags=%lx, init_val=%lx) syscall ret: %d errno: %d\n",
540 __func__, flags, init_val, ret, errno);
541 return ret;
544 int alloc_pkey(void)
546 int ret;
547 unsigned long init_val = 0x0;
549 dprintf1("alloc_pkey()::%d, pkru: 0x%x shadow: %x\n",
550 __LINE__, __rdpkru(), shadow_pkru);
551 ret = sys_pkey_alloc(0, init_val);
553 * pkey_alloc() sets PKRU, so we need to reflect it in
554 * shadow_pkru:
556 dprintf4("alloc_pkey()::%d, ret: %d pkru: 0x%x shadow: 0x%x\n",
557 __LINE__, ret, __rdpkru(), shadow_pkru);
558 if (ret) {
559 /* clear both the bits: */
560 shadow_pkru &= ~(0x3 << (ret * 2));
561 dprintf4("alloc_pkey()::%d, ret: %d pkru: 0x%x shadow: 0x%x\n",
562 __LINE__, ret, __rdpkru(), shadow_pkru);
564 * move the new state in from init_val
565 * (remember, we cheated and init_val == pkru format)
567 shadow_pkru |= (init_val << (ret * 2));
569 dprintf4("alloc_pkey()::%d, ret: %d pkru: 0x%x shadow: 0x%x\n",
570 __LINE__, ret, __rdpkru(), shadow_pkru);
571 dprintf1("alloc_pkey()::%d errno: %d\n", __LINE__, errno);
572 /* for shadow checking: */
573 rdpkru();
574 dprintf4("alloc_pkey()::%d, ret: %d pkru: 0x%x shadow: 0x%x\n",
575 __LINE__, ret, __rdpkru(), shadow_pkru);
576 return ret;
579 int sys_pkey_free(unsigned long pkey)
581 int ret = syscall(SYS_pkey_free, pkey);
582 dprintf1("%s(pkey=%ld) syscall ret: %d\n", __func__, pkey, ret);
583 return ret;
587 * I had a bug where pkey bits could be set by mprotect() but
588 * not cleared. This ensures we get lots of random bit sets
589 * and clears on the vma and pte pkey bits.
591 int alloc_random_pkey(void)
593 int max_nr_pkey_allocs;
594 int ret;
595 int i;
596 int alloced_pkeys[NR_PKEYS];
597 int nr_alloced = 0;
598 int random_index;
599 memset(alloced_pkeys, 0, sizeof(alloced_pkeys));
601 /* allocate every possible key and make a note of which ones we got */
602 max_nr_pkey_allocs = NR_PKEYS;
603 max_nr_pkey_allocs = 1;
604 for (i = 0; i < max_nr_pkey_allocs; i++) {
605 int new_pkey = alloc_pkey();
606 if (new_pkey < 0)
607 break;
608 alloced_pkeys[nr_alloced++] = new_pkey;
611 pkey_assert(nr_alloced > 0);
612 /* select a random one out of the allocated ones */
613 random_index = rand() % nr_alloced;
614 ret = alloced_pkeys[random_index];
615 /* now zero it out so we don't free it next */
616 alloced_pkeys[random_index] = 0;
618 /* go through the allocated ones that we did not want and free them */
619 for (i = 0; i < nr_alloced; i++) {
620 int free_ret;
621 if (!alloced_pkeys[i])
622 continue;
623 free_ret = sys_pkey_free(alloced_pkeys[i]);
624 pkey_assert(!free_ret);
626 dprintf1("%s()::%d, ret: %d pkru: 0x%x shadow: 0x%x\n", __func__,
627 __LINE__, ret, __rdpkru(), shadow_pkru);
628 return ret;
631 int mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
632 unsigned long pkey)
634 int nr_iterations = random() % 100;
635 int ret;
637 while (0) {
638 int rpkey = alloc_random_pkey();
639 ret = sys_mprotect_pkey(ptr, size, orig_prot, pkey);
640 dprintf1("sys_mprotect_pkey(%p, %zx, prot=0x%lx, pkey=%ld) ret: %d\n",
641 ptr, size, orig_prot, pkey, ret);
642 if (nr_iterations-- < 0)
643 break;
645 dprintf1("%s()::%d, ret: %d pkru: 0x%x shadow: 0x%x\n", __func__,
646 __LINE__, ret, __rdpkru(), shadow_pkru);
647 sys_pkey_free(rpkey);
648 dprintf1("%s()::%d, ret: %d pkru: 0x%x shadow: 0x%x\n", __func__,
649 __LINE__, ret, __rdpkru(), shadow_pkru);
651 pkey_assert(pkey < NR_PKEYS);
653 ret = sys_mprotect_pkey(ptr, size, orig_prot, pkey);
654 dprintf1("mprotect_pkey(%p, %zx, prot=0x%lx, pkey=%ld) ret: %d\n",
655 ptr, size, orig_prot, pkey, ret);
656 pkey_assert(!ret);
657 dprintf1("%s()::%d, ret: %d pkru: 0x%x shadow: 0x%x\n", __func__,
658 __LINE__, ret, __rdpkru(), shadow_pkru);
659 return ret;
662 struct pkey_malloc_record {
663 void *ptr;
664 long size;
666 struct pkey_malloc_record *pkey_malloc_records;
667 long nr_pkey_malloc_records;
668 void record_pkey_malloc(void *ptr, long size)
670 long i;
671 struct pkey_malloc_record *rec = NULL;
673 for (i = 0; i < nr_pkey_malloc_records; i++) {
674 rec = &pkey_malloc_records[i];
675 /* find a free record */
676 if (rec)
677 break;
679 if (!rec) {
680 /* every record is full */
681 size_t old_nr_records = nr_pkey_malloc_records;
682 size_t new_nr_records = (nr_pkey_malloc_records * 2 + 1);
683 size_t new_size = new_nr_records * sizeof(struct pkey_malloc_record);
684 dprintf2("new_nr_records: %zd\n", new_nr_records);
685 dprintf2("new_size: %zd\n", new_size);
686 pkey_malloc_records = realloc(pkey_malloc_records, new_size);
687 pkey_assert(pkey_malloc_records != NULL);
688 rec = &pkey_malloc_records[nr_pkey_malloc_records];
690 * realloc() does not initialize memory, so zero it from
691 * the first new record all the way to the end.
693 for (i = 0; i < new_nr_records - old_nr_records; i++)
694 memset(rec + i, 0, sizeof(*rec));
696 dprintf3("filling malloc record[%d/%p]: {%p, %ld}\n",
697 (int)(rec - pkey_malloc_records), rec, ptr, size);
698 rec->ptr = ptr;
699 rec->size = size;
700 nr_pkey_malloc_records++;
703 void free_pkey_malloc(void *ptr)
705 long i;
706 int ret;
707 dprintf3("%s(%p)\n", __func__, ptr);
708 for (i = 0; i < nr_pkey_malloc_records; i++) {
709 struct pkey_malloc_record *rec = &pkey_malloc_records[i];
710 dprintf4("looking for ptr %p at record[%ld/%p]: {%p, %ld}\n",
711 ptr, i, rec, rec->ptr, rec->size);
712 if ((ptr < rec->ptr) ||
713 (ptr >= rec->ptr + rec->size))
714 continue;
716 dprintf3("found ptr %p at record[%ld/%p]: {%p, %ld}\n",
717 ptr, i, rec, rec->ptr, rec->size);
718 nr_pkey_malloc_records--;
719 ret = munmap(rec->ptr, rec->size);
720 dprintf3("munmap ret: %d\n", ret);
721 pkey_assert(!ret);
722 dprintf3("clearing rec->ptr, rec: %p\n", rec);
723 rec->ptr = NULL;
724 dprintf3("done clearing rec->ptr, rec: %p\n", rec);
725 return;
727 pkey_assert(false);
731 void *malloc_pkey_with_mprotect(long size, int prot, u16 pkey)
733 void *ptr;
734 int ret;
736 rdpkru();
737 dprintf1("doing %s(size=%ld, prot=0x%x, pkey=%d)\n", __func__,
738 size, prot, pkey);
739 pkey_assert(pkey < NR_PKEYS);
740 ptr = mmap(NULL, size, prot, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
741 pkey_assert(ptr != (void *)-1);
742 ret = mprotect_pkey((void *)ptr, PAGE_SIZE, prot, pkey);
743 pkey_assert(!ret);
744 record_pkey_malloc(ptr, size);
745 rdpkru();
747 dprintf1("%s() for pkey %d @ %p\n", __func__, pkey, ptr);
748 return ptr;
751 void *malloc_pkey_anon_huge(long size, int prot, u16 pkey)
753 int ret;
754 void *ptr;
756 dprintf1("doing %s(size=%ld, prot=0x%x, pkey=%d)\n", __func__,
757 size, prot, pkey);
759 * Guarantee we can fit at least one huge page in the resulting
760 * allocation by allocating space for 2:
762 size = ALIGN_UP(size, HPAGE_SIZE * 2);
763 ptr = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
764 pkey_assert(ptr != (void *)-1);
765 record_pkey_malloc(ptr, size);
766 mprotect_pkey(ptr, size, prot, pkey);
768 dprintf1("unaligned ptr: %p\n", ptr);
769 ptr = ALIGN_PTR_UP(ptr, HPAGE_SIZE);
770 dprintf1(" aligned ptr: %p\n", ptr);
771 ret = madvise(ptr, HPAGE_SIZE, MADV_HUGEPAGE);
772 dprintf1("MADV_HUGEPAGE ret: %d\n", ret);
773 ret = madvise(ptr, HPAGE_SIZE, MADV_WILLNEED);
774 dprintf1("MADV_WILLNEED ret: %d\n", ret);
775 memset(ptr, 0, HPAGE_SIZE);
777 dprintf1("mmap()'d thp for pkey %d @ %p\n", pkey, ptr);
778 return ptr;
781 int hugetlb_setup_ok;
782 #define GET_NR_HUGE_PAGES 10
783 void setup_hugetlbfs(void)
785 int err;
786 int fd;
787 int validated_nr_pages;
788 int i;
789 char buf[] = "123";
791 if (geteuid() != 0) {
792 fprintf(stderr, "WARNING: not run as root, can not do hugetlb test\n");
793 return;
796 cat_into_file(__stringify(GET_NR_HUGE_PAGES), "/proc/sys/vm/nr_hugepages");
799 * Now go make sure that we got the pages and that they
800 * are 2M pages. Someone might have made 1G the default.
802 fd = open("/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages", O_RDONLY);
803 if (fd < 0) {
804 perror("opening sysfs 2M hugetlb config");
805 return;
808 /* -1 to guarantee leaving the trailing \0 */
809 err = read(fd, buf, sizeof(buf)-1);
810 close(fd);
811 if (err <= 0) {
812 perror("reading sysfs 2M hugetlb config");
813 return;
816 if (atoi(buf) != GET_NR_HUGE_PAGES) {
817 fprintf(stderr, "could not confirm 2M pages, got: '%s' expected %d\n",
818 buf, GET_NR_HUGE_PAGES);
819 return;
822 hugetlb_setup_ok = 1;
825 void *malloc_pkey_hugetlb(long size, int prot, u16 pkey)
827 void *ptr;
828 int flags = MAP_ANONYMOUS|MAP_PRIVATE|MAP_HUGETLB;
830 if (!hugetlb_setup_ok)
831 return PTR_ERR_ENOTSUP;
833 dprintf1("doing %s(%ld, %x, %x)\n", __func__, size, prot, pkey);
834 size = ALIGN_UP(size, HPAGE_SIZE * 2);
835 pkey_assert(pkey < NR_PKEYS);
836 ptr = mmap(NULL, size, PROT_NONE, flags, -1, 0);
837 pkey_assert(ptr != (void *)-1);
838 mprotect_pkey(ptr, size, prot, pkey);
840 record_pkey_malloc(ptr, size);
842 dprintf1("mmap()'d hugetlbfs for pkey %d @ %p\n", pkey, ptr);
843 return ptr;
846 void *malloc_pkey_mmap_dax(long size, int prot, u16 pkey)
848 void *ptr;
849 int fd;
851 dprintf1("doing %s(size=%ld, prot=0x%x, pkey=%d)\n", __func__,
852 size, prot, pkey);
853 pkey_assert(pkey < NR_PKEYS);
854 fd = open("/dax/foo", O_RDWR);
855 pkey_assert(fd >= 0);
857 ptr = mmap(0, size, prot, MAP_SHARED, fd, 0);
858 pkey_assert(ptr != (void *)-1);
860 mprotect_pkey(ptr, size, prot, pkey);
862 record_pkey_malloc(ptr, size);
864 dprintf1("mmap()'d for pkey %d @ %p\n", pkey, ptr);
865 close(fd);
866 return ptr;
869 void *(*pkey_malloc[])(long size, int prot, u16 pkey) = {
871 malloc_pkey_with_mprotect,
872 malloc_pkey_anon_huge,
873 malloc_pkey_hugetlb
874 /* can not do direct with the pkey_mprotect() API:
875 malloc_pkey_mmap_direct,
876 malloc_pkey_mmap_dax,
880 void *malloc_pkey(long size, int prot, u16 pkey)
882 void *ret;
883 static int malloc_type;
884 int nr_malloc_types = ARRAY_SIZE(pkey_malloc);
886 pkey_assert(pkey < NR_PKEYS);
888 while (1) {
889 pkey_assert(malloc_type < nr_malloc_types);
891 ret = pkey_malloc[malloc_type](size, prot, pkey);
892 pkey_assert(ret != (void *)-1);
894 malloc_type++;
895 if (malloc_type >= nr_malloc_types)
896 malloc_type = (random()%nr_malloc_types);
898 /* try again if the malloc_type we tried is unsupported */
899 if (ret == PTR_ERR_ENOTSUP)
900 continue;
902 break;
905 dprintf3("%s(%ld, prot=%x, pkey=%x) returning: %p\n", __func__,
906 size, prot, pkey, ret);
907 return ret;
910 int last_pkru_faults;
911 void expected_pk_fault(int pkey)
913 dprintf2("%s(): last_pkru_faults: %d pkru_faults: %d\n",
914 __func__, last_pkru_faults, pkru_faults);
915 dprintf2("%s(%d): last_si_pkey: %d\n", __func__, pkey, last_si_pkey);
916 pkey_assert(last_pkru_faults + 1 == pkru_faults);
917 pkey_assert(last_si_pkey == pkey);
919 * The signal handler shold have cleared out PKRU to let the
920 * test program continue. We now have to restore it.
922 if (__rdpkru() != 0)
923 pkey_assert(0);
925 __wrpkru(shadow_pkru);
926 dprintf1("%s() set PKRU=%x to restore state after signal nuked it\n",
927 __func__, shadow_pkru);
928 last_pkru_faults = pkru_faults;
929 last_si_pkey = -1;
932 void do_not_expect_pk_fault(void)
934 pkey_assert(last_pkru_faults == pkru_faults);
937 int test_fds[10] = { -1 };
938 int nr_test_fds;
939 void __save_test_fd(int fd)
941 pkey_assert(fd >= 0);
942 pkey_assert(nr_test_fds < ARRAY_SIZE(test_fds));
943 test_fds[nr_test_fds] = fd;
944 nr_test_fds++;
947 int get_test_read_fd(void)
949 int test_fd = open("/etc/passwd", O_RDONLY);
950 __save_test_fd(test_fd);
951 return test_fd;
954 void close_test_fds(void)
956 int i;
958 for (i = 0; i < nr_test_fds; i++) {
959 if (test_fds[i] < 0)
960 continue;
961 close(test_fds[i]);
962 test_fds[i] = -1;
964 nr_test_fds = 0;
967 #define barrier() __asm__ __volatile__("": : :"memory")
968 __attribute__((noinline)) int read_ptr(int *ptr)
971 * Keep GCC from optimizing this away somehow
973 barrier();
974 return *ptr;
977 void test_read_of_write_disabled_region(int *ptr, u16 pkey)
979 int ptr_contents;
981 dprintf1("disabling write access to PKEY[1], doing read\n");
982 pkey_write_deny(pkey);
983 ptr_contents = read_ptr(ptr);
984 dprintf1("*ptr: %d\n", ptr_contents);
985 dprintf1("\n");
987 void test_read_of_access_disabled_region(int *ptr, u16 pkey)
989 int ptr_contents;
991 dprintf1("disabling access to PKEY[%02d], doing read @ %p\n", pkey, ptr);
992 rdpkru();
993 pkey_access_deny(pkey);
994 ptr_contents = read_ptr(ptr);
995 dprintf1("*ptr: %d\n", ptr_contents);
996 expected_pk_fault(pkey);
998 void test_write_of_write_disabled_region(int *ptr, u16 pkey)
1000 dprintf1("disabling write access to PKEY[%02d], doing write\n", pkey);
1001 pkey_write_deny(pkey);
1002 *ptr = __LINE__;
1003 expected_pk_fault(pkey);
1005 void test_write_of_access_disabled_region(int *ptr, u16 pkey)
1007 dprintf1("disabling access to PKEY[%02d], doing write\n", pkey);
1008 pkey_access_deny(pkey);
1009 *ptr = __LINE__;
1010 expected_pk_fault(pkey);
1012 void test_kernel_write_of_access_disabled_region(int *ptr, u16 pkey)
1014 int ret;
1015 int test_fd = get_test_read_fd();
1017 dprintf1("disabling access to PKEY[%02d], "
1018 "having kernel read() to buffer\n", pkey);
1019 pkey_access_deny(pkey);
1020 ret = read(test_fd, ptr, 1);
1021 dprintf1("read ret: %d\n", ret);
1022 pkey_assert(ret);
1024 void test_kernel_write_of_write_disabled_region(int *ptr, u16 pkey)
1026 int ret;
1027 int test_fd = get_test_read_fd();
1029 pkey_write_deny(pkey);
1030 ret = read(test_fd, ptr, 100);
1031 dprintf1("read ret: %d\n", ret);
1032 if (ret < 0 && (DEBUG_LEVEL > 0))
1033 perror("verbose read result (OK for this to be bad)");
1034 pkey_assert(ret);
1037 void test_kernel_gup_of_access_disabled_region(int *ptr, u16 pkey)
1039 int pipe_ret, vmsplice_ret;
1040 struct iovec iov;
1041 int pipe_fds[2];
1043 pipe_ret = pipe(pipe_fds);
1045 pkey_assert(pipe_ret == 0);
1046 dprintf1("disabling access to PKEY[%02d], "
1047 "having kernel vmsplice from buffer\n", pkey);
1048 pkey_access_deny(pkey);
1049 iov.iov_base = ptr;
1050 iov.iov_len = PAGE_SIZE;
1051 vmsplice_ret = vmsplice(pipe_fds[1], &iov, 1, SPLICE_F_GIFT);
1052 dprintf1("vmsplice() ret: %d\n", vmsplice_ret);
1053 pkey_assert(vmsplice_ret == -1);
1055 close(pipe_fds[0]);
1056 close(pipe_fds[1]);
1059 void test_kernel_gup_write_to_write_disabled_region(int *ptr, u16 pkey)
1061 int ignored = 0xdada;
1062 int futex_ret;
1063 int some_int = __LINE__;
1065 dprintf1("disabling write to PKEY[%02d], "
1066 "doing futex gunk in buffer\n", pkey);
1067 *ptr = some_int;
1068 pkey_write_deny(pkey);
1069 futex_ret = syscall(SYS_futex, ptr, FUTEX_WAIT, some_int-1, NULL,
1070 &ignored, ignored);
1071 if (DEBUG_LEVEL > 0)
1072 perror("futex");
1073 dprintf1("futex() ret: %d\n", futex_ret);
1076 /* Assumes that all pkeys other than 'pkey' are unallocated */
1077 void test_pkey_syscalls_on_non_allocated_pkey(int *ptr, u16 pkey)
1079 int err;
1080 int i;
1082 /* Note: 0 is the default pkey, so don't mess with it */
1083 for (i = 1; i < NR_PKEYS; i++) {
1084 if (pkey == i)
1085 continue;
1087 dprintf1("trying get/set/free to non-allocated pkey: %2d\n", i);
1088 err = sys_pkey_free(i);
1089 pkey_assert(err);
1091 /* not enforced when pkey_get() is not a syscall
1092 err = pkey_get(i, 0);
1093 pkey_assert(err < 0);
1096 err = sys_pkey_free(i);
1097 pkey_assert(err);
1099 err = sys_mprotect_pkey(ptr, PAGE_SIZE, PROT_READ, i);
1100 pkey_assert(err);
1104 /* Assumes that all pkeys other than 'pkey' are unallocated */
1105 void test_pkey_syscalls_bad_args(int *ptr, u16 pkey)
1107 int err;
1108 int bad_flag = (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE) + 1;
1109 int bad_pkey = NR_PKEYS+99;
1111 /* not enforced when pkey_get() is not a syscall
1112 err = pkey_get(bad_pkey, bad_flag);
1113 pkey_assert(err < 0);
1116 /* pass a known-invalid pkey in: */
1117 err = sys_mprotect_pkey(ptr, PAGE_SIZE, PROT_READ, bad_pkey);
1118 pkey_assert(err);
1121 /* Assumes that all pkeys other than 'pkey' are unallocated */
1122 void test_pkey_alloc_exhaust(int *ptr, u16 pkey)
1124 unsigned long flags;
1125 unsigned long init_val;
1126 int err;
1127 int allocated_pkeys[NR_PKEYS] = {0};
1128 int nr_allocated_pkeys = 0;
1129 int i;
1131 for (i = 0; i < NR_PKEYS*2; i++) {
1132 int new_pkey;
1133 dprintf1("%s() alloc loop: %d\n", __func__, i);
1134 new_pkey = alloc_pkey();
1135 dprintf4("%s()::%d, err: %d pkru: 0x%x shadow: 0x%x\n", __func__,
1136 __LINE__, err, __rdpkru(), shadow_pkru);
1137 rdpkru(); /* for shadow checking */
1138 dprintf2("%s() errno: %d ENOSPC: %d\n", __func__, errno, ENOSPC);
1139 if ((new_pkey == -1) && (errno == ENOSPC)) {
1140 dprintf2("%s() failed to allocate pkey after %d tries\n",
1141 __func__, nr_allocated_pkeys);
1142 break;
1144 pkey_assert(nr_allocated_pkeys < NR_PKEYS);
1145 allocated_pkeys[nr_allocated_pkeys++] = new_pkey;
1148 dprintf3("%s()::%d\n", __func__, __LINE__);
1151 * ensure it did not reach the end of the loop without
1152 * failure:
1154 pkey_assert(i < NR_PKEYS*2);
1157 * There are 16 pkeys supported in hardware. One is taken
1158 * up for the default (0) and another can be taken up by
1159 * an execute-only mapping. Ensure that we can allocate
1160 * at least 14 (16-2).
1162 pkey_assert(i >= NR_PKEYS-2);
1164 for (i = 0; i < nr_allocated_pkeys; i++) {
1165 err = sys_pkey_free(allocated_pkeys[i]);
1166 pkey_assert(!err);
1167 rdpkru(); /* for shadow checking */
1171 void test_ptrace_of_child(int *ptr, u16 pkey)
1173 __attribute__((__unused__)) int peek_result;
1174 pid_t child_pid;
1175 void *ignored = 0;
1176 long ret;
1177 int status;
1179 * This is the "control" for our little expermient. Make sure
1180 * we can always access it when ptracing.
1182 int *plain_ptr_unaligned = malloc(HPAGE_SIZE);
1183 int *plain_ptr = ALIGN_PTR_UP(plain_ptr_unaligned, PAGE_SIZE);
1186 * Fork a child which is an exact copy of this process, of course.
1187 * That means we can do all of our tests via ptrace() and then plain
1188 * memory access and ensure they work differently.
1190 child_pid = fork_lazy_child();
1191 dprintf1("[%d] child pid: %d\n", getpid(), child_pid);
1193 ret = ptrace(PTRACE_ATTACH, child_pid, ignored, ignored);
1194 if (ret)
1195 perror("attach");
1196 dprintf1("[%d] attach ret: %ld %d\n", getpid(), ret, __LINE__);
1197 pkey_assert(ret != -1);
1198 ret = waitpid(child_pid, &status, WUNTRACED);
1199 if ((ret != child_pid) || !(WIFSTOPPED(status))) {
1200 fprintf(stderr, "weird waitpid result %ld stat %x\n",
1201 ret, status);
1202 pkey_assert(0);
1204 dprintf2("waitpid ret: %ld\n", ret);
1205 dprintf2("waitpid status: %d\n", status);
1207 pkey_access_deny(pkey);
1208 pkey_write_deny(pkey);
1210 /* Write access, untested for now:
1211 ret = ptrace(PTRACE_POKEDATA, child_pid, peek_at, data);
1212 pkey_assert(ret != -1);
1213 dprintf1("poke at %p: %ld\n", peek_at, ret);
1217 * Try to access the pkey-protected "ptr" via ptrace:
1219 ret = ptrace(PTRACE_PEEKDATA, child_pid, ptr, ignored);
1220 /* expect it to work, without an error: */
1221 pkey_assert(ret != -1);
1222 /* Now access from the current task, and expect an exception: */
1223 peek_result = read_ptr(ptr);
1224 expected_pk_fault(pkey);
1227 * Try to access the NON-pkey-protected "plain_ptr" via ptrace:
1229 ret = ptrace(PTRACE_PEEKDATA, child_pid, plain_ptr, ignored);
1230 /* expect it to work, without an error: */
1231 pkey_assert(ret != -1);
1232 /* Now access from the current task, and expect NO exception: */
1233 peek_result = read_ptr(plain_ptr);
1234 do_not_expect_pk_fault();
1236 ret = ptrace(PTRACE_DETACH, child_pid, ignored, 0);
1237 pkey_assert(ret != -1);
1239 ret = kill(child_pid, SIGKILL);
1240 pkey_assert(ret != -1);
1242 wait(&status);
1244 free(plain_ptr_unaligned);
1247 void test_executing_on_unreadable_memory(int *ptr, u16 pkey)
1249 void *p1;
1250 int scratch;
1251 int ptr_contents;
1252 int ret;
1254 p1 = ALIGN_PTR_UP(&lots_o_noops_around_write, PAGE_SIZE);
1255 dprintf3("&lots_o_noops: %p\n", &lots_o_noops_around_write);
1256 /* lots_o_noops_around_write should be page-aligned already */
1257 assert(p1 == &lots_o_noops_around_write);
1259 /* Point 'p1' at the *second* page of the function: */
1260 p1 += PAGE_SIZE;
1262 madvise(p1, PAGE_SIZE, MADV_DONTNEED);
1263 lots_o_noops_around_write(&scratch);
1264 ptr_contents = read_ptr(p1);
1265 dprintf2("ptr (%p) contents@%d: %x\n", p1, __LINE__, ptr_contents);
1267 ret = mprotect_pkey(p1, PAGE_SIZE, PROT_EXEC, (u64)pkey);
1268 pkey_assert(!ret);
1269 pkey_access_deny(pkey);
1271 dprintf2("pkru: %x\n", rdpkru());
1274 * Make sure this is an *instruction* fault
1276 madvise(p1, PAGE_SIZE, MADV_DONTNEED);
1277 lots_o_noops_around_write(&scratch);
1278 do_not_expect_pk_fault();
1279 ptr_contents = read_ptr(p1);
1280 dprintf2("ptr (%p) contents@%d: %x\n", p1, __LINE__, ptr_contents);
1281 expected_pk_fault(pkey);
1284 void test_mprotect_pkey_on_unsupported_cpu(int *ptr, u16 pkey)
1286 int size = PAGE_SIZE;
1287 int sret;
1289 if (cpu_has_pku()) {
1290 dprintf1("SKIP: %s: no CPU support\n", __func__);
1291 return;
1294 sret = syscall(SYS_mprotect_key, ptr, size, PROT_READ, pkey);
1295 pkey_assert(sret < 0);
1298 void (*pkey_tests[])(int *ptr, u16 pkey) = {
1299 test_read_of_write_disabled_region,
1300 test_read_of_access_disabled_region,
1301 test_write_of_write_disabled_region,
1302 test_write_of_access_disabled_region,
1303 test_kernel_write_of_access_disabled_region,
1304 test_kernel_write_of_write_disabled_region,
1305 test_kernel_gup_of_access_disabled_region,
1306 test_kernel_gup_write_to_write_disabled_region,
1307 test_executing_on_unreadable_memory,
1308 test_ptrace_of_child,
1309 test_pkey_syscalls_on_non_allocated_pkey,
1310 test_pkey_syscalls_bad_args,
1311 test_pkey_alloc_exhaust,
1314 void run_tests_once(void)
1316 int *ptr;
1317 int prot = PROT_READ|PROT_WRITE;
1319 for (test_nr = 0; test_nr < ARRAY_SIZE(pkey_tests); test_nr++) {
1320 int pkey;
1321 int orig_pkru_faults = pkru_faults;
1323 dprintf1("======================\n");
1324 dprintf1("test %d preparing...\n", test_nr);
1326 tracing_on();
1327 pkey = alloc_random_pkey();
1328 dprintf1("test %d starting with pkey: %d\n", test_nr, pkey);
1329 ptr = malloc_pkey(PAGE_SIZE, prot, pkey);
1330 dprintf1("test %d starting...\n", test_nr);
1331 pkey_tests[test_nr](ptr, pkey);
1332 dprintf1("freeing test memory: %p\n", ptr);
1333 free_pkey_malloc(ptr);
1334 sys_pkey_free(pkey);
1336 dprintf1("pkru_faults: %d\n", pkru_faults);
1337 dprintf1("orig_pkru_faults: %d\n", orig_pkru_faults);
1339 tracing_off();
1340 close_test_fds();
1342 printf("test %2d PASSED (itertation %d)\n", test_nr, iteration_nr);
1343 dprintf1("======================\n\n");
1345 iteration_nr++;
1348 void pkey_setup_shadow(void)
1350 shadow_pkru = __rdpkru();
1353 int main(void)
1355 int nr_iterations = 22;
1357 setup_handlers();
1359 printf("has pku: %d\n", cpu_has_pku());
1361 if (!cpu_has_pku()) {
1362 int size = PAGE_SIZE;
1363 int *ptr;
1365 printf("running PKEY tests for unsupported CPU/OS\n");
1367 ptr = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1368 assert(ptr != (void *)-1);
1369 test_mprotect_pkey_on_unsupported_cpu(ptr, 1);
1370 exit(0);
1373 pkey_setup_shadow();
1374 printf("startup pkru: %x\n", rdpkru());
1375 setup_hugetlbfs();
1377 while (nr_iterations-- > 0)
1378 run_tests_once();
1380 printf("done (all tests OK)\n");
1381 return 0;