1*2679f63fSMichael Ellerman #include <stdlib.h>
2*2679f63fSMichael Ellerman #include <string.h>
3*2679f63fSMichael Ellerman #include <stdio.h>
4*2679f63fSMichael Ellerman #include <signal.h>
5*2679f63fSMichael Ellerman #include <unistd.h>
6*2679f63fSMichael Ellerman #include <sys/mman.h>
7*2679f63fSMichael Ellerman
8*2679f63fSMichael Ellerman #include "utils.h"
9*2679f63fSMichael Ellerman
10*2679f63fSMichael Ellerman extern char __start___ex_table[];
11*2679f63fSMichael Ellerman extern char __stop___ex_table[];
12*2679f63fSMichael Ellerman
13*2679f63fSMichael Ellerman #if defined(__powerpc64__)
14*2679f63fSMichael Ellerman #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP]
15*2679f63fSMichael Ellerman #elif defined(__powerpc__)
16*2679f63fSMichael Ellerman #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
17*2679f63fSMichael Ellerman #else
18*2679f63fSMichael Ellerman #error implement UCONTEXT_NIA
19*2679f63fSMichael Ellerman #endif
20*2679f63fSMichael Ellerman
segv_handler(int signr,siginfo_t * info,void * ptr)21*2679f63fSMichael Ellerman static void segv_handler(int signr, siginfo_t *info, void *ptr)
22*2679f63fSMichael Ellerman {
23*2679f63fSMichael Ellerman ucontext_t *uc = (ucontext_t *)ptr;
24*2679f63fSMichael Ellerman unsigned long addr = (unsigned long)info->si_addr;
25*2679f63fSMichael Ellerman unsigned long *ip = &UCONTEXT_NIA(uc);
26*2679f63fSMichael Ellerman unsigned long *ex_p = (unsigned long *)__start___ex_table;
27*2679f63fSMichael Ellerman
28*2679f63fSMichael Ellerman while (ex_p < (unsigned long *)__stop___ex_table) {
29*2679f63fSMichael Ellerman unsigned long insn, fixup;
30*2679f63fSMichael Ellerman
31*2679f63fSMichael Ellerman insn = *ex_p++;
32*2679f63fSMichael Ellerman fixup = *ex_p++;
33*2679f63fSMichael Ellerman
34*2679f63fSMichael Ellerman if (insn == *ip) {
35*2679f63fSMichael Ellerman *ip = fixup;
36*2679f63fSMichael Ellerman return;
37*2679f63fSMichael Ellerman }
38*2679f63fSMichael Ellerman }
39*2679f63fSMichael Ellerman
40*2679f63fSMichael Ellerman printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
41*2679f63fSMichael Ellerman abort();
42*2679f63fSMichael Ellerman }
43*2679f63fSMichael Ellerman
setup_segv_handler(void)44*2679f63fSMichael Ellerman static void setup_segv_handler(void)
45*2679f63fSMichael Ellerman {
46*2679f63fSMichael Ellerman struct sigaction action;
47*2679f63fSMichael Ellerman
48*2679f63fSMichael Ellerman memset(&action, 0, sizeof(action));
49*2679f63fSMichael Ellerman action.sa_sigaction = segv_handler;
50*2679f63fSMichael Ellerman action.sa_flags = SA_SIGINFO;
51*2679f63fSMichael Ellerman sigaction(SIGSEGV, &action, NULL);
52*2679f63fSMichael Ellerman }
53*2679f63fSMichael Ellerman
54*2679f63fSMichael Ellerman unsigned long COPY_LOOP(void *to, const void *from, unsigned long size);
55*2679f63fSMichael Ellerman unsigned long test_copy_tofrom_user_reference(void *to, const void *from, unsigned long size);
56*2679f63fSMichael Ellerman
57*2679f63fSMichael Ellerman static int total_passed;
58*2679f63fSMichael Ellerman static int total_failed;
59*2679f63fSMichael Ellerman
do_one_test(char * dstp,char * srcp,unsigned long len)60*2679f63fSMichael Ellerman static void do_one_test(char *dstp, char *srcp, unsigned long len)
61*2679f63fSMichael Ellerman {
62*2679f63fSMichael Ellerman unsigned long got, expected;
63*2679f63fSMichael Ellerman
64*2679f63fSMichael Ellerman got = COPY_LOOP(dstp, srcp, len);
65*2679f63fSMichael Ellerman expected = test_copy_tofrom_user_reference(dstp, srcp, len);
66*2679f63fSMichael Ellerman
67*2679f63fSMichael Ellerman if (got != expected) {
68*2679f63fSMichael Ellerman total_failed++;
69*2679f63fSMichael Ellerman printf("FAIL from=%p to=%p len=%ld returned %ld, expected %ld\n",
70*2679f63fSMichael Ellerman srcp, dstp, len, got, expected);
71*2679f63fSMichael Ellerman //abort();
72*2679f63fSMichael Ellerman } else
73*2679f63fSMichael Ellerman total_passed++;
74*2679f63fSMichael Ellerman }
75*2679f63fSMichael Ellerman
76*2679f63fSMichael Ellerman //#define MAX_LEN 512
77*2679f63fSMichael Ellerman #define MAX_LEN 16
78*2679f63fSMichael Ellerman
test_copy_exception(void)79*2679f63fSMichael Ellerman int test_copy_exception(void)
80*2679f63fSMichael Ellerman {
81*2679f63fSMichael Ellerman int page_size;
82*2679f63fSMichael Ellerman static char *p, *q;
83*2679f63fSMichael Ellerman unsigned long src, dst, len;
84*2679f63fSMichael Ellerman
85*2679f63fSMichael Ellerman page_size = getpagesize();
86*2679f63fSMichael Ellerman p = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
87*2679f63fSMichael Ellerman MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
88*2679f63fSMichael Ellerman
89*2679f63fSMichael Ellerman if (p == MAP_FAILED) {
90*2679f63fSMichael Ellerman perror("mmap");
91*2679f63fSMichael Ellerman exit(1);
92*2679f63fSMichael Ellerman }
93*2679f63fSMichael Ellerman
94*2679f63fSMichael Ellerman memset(p, 0, page_size);
95*2679f63fSMichael Ellerman
96*2679f63fSMichael Ellerman setup_segv_handler();
97*2679f63fSMichael Ellerman
98*2679f63fSMichael Ellerman if (mprotect(p + page_size, page_size, PROT_NONE)) {
99*2679f63fSMichael Ellerman perror("mprotect");
100*2679f63fSMichael Ellerman exit(1);
101*2679f63fSMichael Ellerman }
102*2679f63fSMichael Ellerman
103*2679f63fSMichael Ellerman q = p + page_size - MAX_LEN;
104*2679f63fSMichael Ellerman
105*2679f63fSMichael Ellerman for (src = 0; src < MAX_LEN; src++) {
106*2679f63fSMichael Ellerman for (dst = 0; dst < MAX_LEN; dst++) {
107*2679f63fSMichael Ellerman for (len = 0; len < MAX_LEN+1; len++) {
108*2679f63fSMichael Ellerman // printf("from=%p to=%p len=%ld\n", q+dst, q+src, len);
109*2679f63fSMichael Ellerman do_one_test(q+dst, q+src, len);
110*2679f63fSMichael Ellerman }
111*2679f63fSMichael Ellerman }
112*2679f63fSMichael Ellerman }
113*2679f63fSMichael Ellerman
114*2679f63fSMichael Ellerman printf("Totals:\n");
115*2679f63fSMichael Ellerman printf(" Pass: %d\n", total_passed);
116*2679f63fSMichael Ellerman printf(" Fail: %d\n", total_failed);
117*2679f63fSMichael Ellerman
118*2679f63fSMichael Ellerman return 0;
119*2679f63fSMichael Ellerman }
120*2679f63fSMichael Ellerman
main(void)121*2679f63fSMichael Ellerman int main(void)
122*2679f63fSMichael Ellerman {
123*2679f63fSMichael Ellerman return test_harness(test_copy_exception, str(COPY_LOOP));
124*2679f63fSMichael Ellerman }
125