1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018, Michael Ellerman, IBM Corp.
5 * Test that an out-of-bounds branch to counter behaves as expected.
13 #include <sys/types.h>
21 #define BAD_NIP 0x788c545a18000000ull
23 static struct pt_regs signal_regs
;
24 static jmp_buf setjmp_env
;
26 static void save_regs(ucontext_t
*ctxt
)
28 struct pt_regs
*regs
= ctxt
->uc_mcontext
.regs
;
30 memcpy(&signal_regs
, regs
, sizeof(signal_regs
));
33 static void segv_handler(int signum
, siginfo_t
*info
, void *ctxt_v
)
36 longjmp(setjmp_env
, 1);
39 static void usr2_handler(int signum
, siginfo_t
*info
, void *ctxt_v
)
46 printf("Everything is OK in here.\n");
50 #define REG_POISON 0x5a5a
51 #define POISONED_REG(n) ((((unsigned long)REG_POISON) << 48) | ((n) << 32) | \
52 (((unsigned long)REG_POISON) << 16) | (n))
54 static inline void poison_regs(void)
56 #define POISON_REG(n) \
57 "lis " __stringify(n) "," __stringify(REG_POISON) ";" \
58 "addi " __stringify(n) "," __stringify(n) "," __stringify(n) ";" \
59 "sldi " __stringify(n) "," __stringify(n) ", 32 ;" \
60 "oris " __stringify(n) "," __stringify(n) "," __stringify(REG_POISON) ";" \
61 "addi " __stringify(n) "," __stringify(n) "," __stringify(n) ";"
80 : "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
81 "26", "27", "28", "29"
86 static int check_regs(void)
90 for (i
= 15; i
<= 29; i
++)
91 FAIL_IF(signal_regs
.gpr
[i
] != POISONED_REG(i
));
97 static void dump_regs(void)
99 for (int i
= 0; i
< 32; i
+= 4) {
100 printf("r%02d 0x%016lx r%02d 0x%016lx " \
101 "r%02d 0x%016lx r%02d 0x%016lx\n",
102 i
, signal_regs
.gpr
[i
],
103 i
+1, signal_regs
.gpr
[i
+1],
104 i
+2, signal_regs
.gpr
[i
+2],
105 i
+3, signal_regs
.gpr
[i
+3]);
115 static struct opd bad_opd
= {
118 #define BAD_FUNC (&bad_opd)
120 #define BAD_FUNC BAD_NIP
123 int test_wild_bctr(void)
125 int (*func_ptr
)(void);
126 struct sigaction segv
= {
127 .sa_sigaction
= segv_handler
,
128 .sa_flags
= SA_SIGINFO
130 struct sigaction usr2
= {
131 .sa_sigaction
= usr2_handler
,
132 .sa_flags
= SA_SIGINFO
135 FAIL_IF(sigaction(SIGSEGV
, &segv
, NULL
));
136 FAIL_IF(sigaction(SIGUSR2
, &usr2
, NULL
));
138 bzero(&signal_regs
, sizeof(signal_regs
));
140 if (setjmp(setjmp_env
) == 0) {
144 kill(getpid(), SIGUSR2
);
145 printf("Regs before:\n");
147 bzero(&signal_regs
, sizeof(signal_regs
));
151 func_ptr
= (int (*)(void))BAD_FUNC
;
154 FAIL_IF(1); /* we didn't segv? */
157 FAIL_IF(signal_regs
.nip
!= BAD_NIP
);
159 printf("All good - took SEGV as expected branching to 0x%llx\n", BAD_NIP
);
162 FAIL_IF(check_regs());
169 return test_harness(test_wild_bctr
, "wild_bctr");