1 /*
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
5  * routine.
6  *
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.
9  *
10  * Copyright (C) 2014 Anton Blanchard <anton@au.ibm.com>, IBM
11  *
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.
16  */
17 
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdbool.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 
26 #define FIXUP_SECTION ".ex_fixup"
27 
28 static inline unsigned long __fls(unsigned long x);
29 
30 #include "word-at-a-time.h"
31 
32 #include "utils.h"
33 
34 static inline unsigned long __fls(unsigned long x)
35 {
36 	int lz;
37 
38 	asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (x));
39 	return sizeof(unsigned long) - 1 - lz;
40 }
41 
42 static int page_size;
43 static char *mem_region;
44 
45 static int protect_region(void)
46 {
47 	if (mprotect(mem_region + page_size, page_size, PROT_NONE)) {
48 		perror("mprotect");
49 		return 1;
50 	}
51 
52 	return 0;
53 }
54 
55 static int unprotect_region(void)
56 {
57 	if (mprotect(mem_region + page_size, page_size, PROT_READ|PROT_WRITE)) {
58 		perror("mprotect");
59 		return 1;
60 	}
61 
62 	return 0;
63 }
64 
65 extern char __start___ex_table[];
66 extern char __stop___ex_table[];
67 
68 struct extbl_entry {
69 	int insn;
70 	int fixup;
71 };
72 
73 static void segv_handler(int signr, siginfo_t *info, void *ptr)
74 {
75 	ucontext_t *uc = (ucontext_t *)ptr;
76 	unsigned long addr = (unsigned long)info->si_addr;
77 	unsigned long *ip = &UCONTEXT_NIA(uc);
78 	struct extbl_entry *entry = (struct extbl_entry *)__start___ex_table;
79 
80 	while (entry < (struct extbl_entry *)__stop___ex_table) {
81 		unsigned long insn, fixup;
82 
83 		insn  = (unsigned long)&entry->insn + entry->insn;
84 		fixup = (unsigned long)&entry->fixup + entry->fixup;
85 
86 		if (insn == *ip) {
87 			*ip = fixup;
88 			return;
89 		}
90 	}
91 
92 	printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
93 	abort();
94 }
95 
96 static void setup_segv_handler(void)
97 {
98 	struct sigaction action;
99 
100 	memset(&action, 0, sizeof(action));
101 	action.sa_sigaction = segv_handler;
102 	action.sa_flags = SA_SIGINFO;
103 	sigaction(SIGSEGV, &action, NULL);
104 }
105 
106 static int do_one_test(char *p, int page_offset)
107 {
108 	unsigned long should;
109 	unsigned long got;
110 
111 	FAIL_IF(unprotect_region());
112 	should = *(unsigned long *)p;
113 	FAIL_IF(protect_region());
114 
115 	got = load_unaligned_zeropad(p);
116 
117 	if (should != got) {
118 		printf("offset %u load_unaligned_zeropad returned 0x%lx, should be 0x%lx\n", page_offset, got, should);
119 		return 1;
120 	}
121 
122 	return 0;
123 }
124 
125 static int test_body(void)
126 {
127 	unsigned long i;
128 
129 	page_size = getpagesize();
130 	mem_region = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
131 		MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
132 
133 	FAIL_IF(mem_region == MAP_FAILED);
134 
135 	for (i = 0; i < page_size; i++)
136 		mem_region[i] = i;
137 
138 	memset(mem_region+page_size, 0, page_size);
139 
140 	setup_segv_handler();
141 
142 	for (i = 0; i < page_size; i++)
143 		FAIL_IF(do_one_test(mem_region+i, i));
144 
145 	return 0;
146 }
147 
148 int main(void)
149 {
150 	return test_harness(test_body, "load_unaligned_zeropad");
151 }
152