2 * Userspace test harness for load_unaligned_zeropad. Creates two
3 * pages and uses mprotect to prevent access to the second page and
4 * a SEGV handler that walks the exception tables and runs the fixup
7 * The results are compared against a normal load that is that is
8 * performed while access to the second page is enabled via mprotect.
10 * Copyright (C) 2014 Anton Blanchard <anton@au.ibm.com>, IBM
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
26 #define FIXUP_SECTION ".ex_fixup"
28 static inline unsigned long __fls(unsigned long x);
30 #include "word-at-a-time.h"
34 static inline unsigned long __fls(unsigned long x)
38 asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (x));
39 return sizeof(unsigned long) - 1 - lz;
43 static char *mem_region;
45 static int protect_region(void)
47 if (mprotect(mem_region + page_size, page_size, PROT_NONE)) {
55 static int unprotect_region(void)
57 if (mprotect(mem_region + page_size, page_size, PROT_READ|PROT_WRITE)) {
65 extern char __start___ex_table[];
66 extern char __stop___ex_table[];
68 #if defined(__powerpc64__)
69 #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP]
70 #elif defined(__powerpc__)
71 #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
73 #error implement UCONTEXT_NIA
81 static void segv_handler(int signr, siginfo_t *info, void *ptr)
83 ucontext_t *uc = (ucontext_t *)ptr;
84 unsigned long addr = (unsigned long)info->si_addr;
85 unsigned long *ip = &UCONTEXT_NIA(uc);
86 struct extbl_entry *entry = (struct extbl_entry *)__start___ex_table;
88 while (entry < (struct extbl_entry *)__stop___ex_table) {
89 unsigned long insn, fixup;
91 insn = (unsigned long)&entry->insn + entry->insn;
92 fixup = (unsigned long)&entry->fixup + entry->fixup;
100 printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
104 static void setup_segv_handler(void)
106 struct sigaction action;
108 memset(&action, 0, sizeof(action));
109 action.sa_sigaction = segv_handler;
110 action.sa_flags = SA_SIGINFO;
111 sigaction(SIGSEGV, &action, NULL);
114 static int do_one_test(char *p, int page_offset)
116 unsigned long should;
119 FAIL_IF(unprotect_region());
120 should = *(unsigned long *)p;
121 FAIL_IF(protect_region());
123 got = load_unaligned_zeropad(p);
126 printf("offset %u load_unaligned_zeropad returned 0x%lx, should be 0x%lx\n", page_offset, got, should);
133 static int test_body(void)
137 page_size = getpagesize();
138 mem_region = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
139 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
141 FAIL_IF(mem_region == MAP_FAILED);
143 for (i = 0; i < page_size; i++)
146 memset(mem_region+page_size, 0, page_size);
148 setup_segv_handler();
150 for (i = 0; i < page_size; i++)
151 FAIL_IF(do_one_test(mem_region+i, i));
158 return test_harness(test_body, "load_unaligned_zeropad");