• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
__fls(unsigned long x)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 
protect_region(void)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 
unprotect_region(void)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 #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]
72 #else
73 #error implement UCONTEXT_NIA
74 #endif
75 
76 struct extbl_entry {
77 	int insn;
78 	int fixup;
79 };
80 
segv_handler(int signr,siginfo_t * info,void * ptr)81 static void segv_handler(int signr, siginfo_t *info, void *ptr)
82 {
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;
87 
88 	while (entry < (struct extbl_entry *)__stop___ex_table) {
89 		unsigned long insn, fixup;
90 
91 		insn  = (unsigned long)&entry->insn + entry->insn;
92 		fixup = (unsigned long)&entry->fixup + entry->fixup;
93 
94 		if (insn == *ip) {
95 			*ip = fixup;
96 			return;
97 		}
98 	}
99 
100 	printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
101 	abort();
102 }
103 
setup_segv_handler(void)104 static void setup_segv_handler(void)
105 {
106 	struct sigaction action;
107 
108 	memset(&action, 0, sizeof(action));
109 	action.sa_sigaction = segv_handler;
110 	action.sa_flags = SA_SIGINFO;
111 	sigaction(SIGSEGV, &action, NULL);
112 }
113 
do_one_test(char * p,int page_offset)114 static int do_one_test(char *p, int page_offset)
115 {
116 	unsigned long should;
117 	unsigned long got;
118 
119 	FAIL_IF(unprotect_region());
120 	should = *(unsigned long *)p;
121 	FAIL_IF(protect_region());
122 
123 	got = load_unaligned_zeropad(p);
124 
125 	if (should != got) {
126 		printf("offset %u load_unaligned_zeropad returned 0x%lx, should be 0x%lx\n", page_offset, got, should);
127 		return 1;
128 	}
129 
130 	return 0;
131 }
132 
test_body(void)133 static int test_body(void)
134 {
135 	unsigned long i;
136 
137 	page_size = getpagesize();
138 	mem_region = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
139 		MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
140 
141 	FAIL_IF(mem_region == MAP_FAILED);
142 
143 	for (i = 0; i < page_size; i++)
144 		mem_region[i] = i;
145 
146 	memset(mem_region+page_size, 0, page_size);
147 
148 	setup_segv_handler();
149 
150 	for (i = 0; i < page_size; i++)
151 		FAIL_IF(do_one_test(mem_region+i, i));
152 
153 	return 0;
154 }
155 
main(void)156 int main(void)
157 {
158 	return test_harness(test_body, "load_unaligned_zeropad");
159 }
160