1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019,2021 Arm Limited
4 * Original author: Dave Martin <Dave.Martin@arm.com>
10 #include <linux/errno.h>
11 #include <linux/auxvec.h>
12 #include <linux/signal.h>
13 #include <asm/sigcontext.h>
14 #include <asm/ucontext.h>
16 typedef struct ucontext ucontext_t;
22 #define EXPECTED_TESTS 18
24 static volatile unsigned int test_num = 1;
25 static unsigned int test_passed;
26 static unsigned int test_failed;
27 static unsigned int test_skipped;
29 static void fdputs(int fd, const char *str)
40 static void putstr(const char *str)
45 static void putnum(unsigned int num)
56 #define puttestname(test_name, trampoline_name) do { \
59 putstr(trampoline_name); \
62 void print_summary(void)
64 putstr("# Totals: pass:");
68 putstr(" xfail:0 xpass:0 skip:");
73 static const char *volatile current_test_name;
74 static const char *volatile current_trampoline_name;
75 static volatile int sigill_expected, sigill_received;
77 static void handler(int n, siginfo_t *si __always_unused,
78 void *uc_ __always_unused)
82 putstr("# \t[SIGILL in ");
83 puttestname(current_test_name, current_trampoline_name);
85 write(1, &"00011011"[((uc->uc_mcontext.pstate & PSR_BTYPE_MASK)
86 >> PSR_BTYPE_SHIFT) * 2], 2);
87 if (!sigill_expected) {
92 puttestname(current_test_name, current_trampoline_name);
93 putstr("(unexpected SIGILL)\n");
98 putstr(" (expected)]\n");
100 /* zap BTYPE so that resuming the faulting code will work */
101 uc->uc_mcontext.pstate &= ~PSR_BTYPE_MASK;
106 static void __do_test(void (*trampoline)(void (*)(void)),
108 const char *trampoline_name,
117 puttestname(name, trampoline_name);
123 /* Branch Target exceptions should only happen in BTI binaries: */
127 sigill_expected = expect_sigill;
129 current_test_name = name;
130 current_trampoline_name = trampoline_name;
134 if (expect_sigill && !sigill_received) {
143 puttestname(name, trampoline_name);
147 #define do_test(expect_sigill_br_x0, \
148 expect_sigill_br_x16, \
152 __do_test(call_using_br_x0, name, "call_using_br_x0", #name, \
153 expect_sigill_br_x0); \
154 __do_test(call_using_br_x16, name, "call_using_br_x16", #name, \
155 expect_sigill_br_x16); \
156 __do_test(call_using_blr, name, "call_using_blr", #name, \
157 expect_sigill_blr); \
160 void start(int *argcp)
164 const struct auxv_entry {
168 unsigned long hwcap = 0, hwcap2 = 0;
170 putstr("TAP version 13\n");
172 putnum(EXPECTED_TESTS);
175 /* Gross hack for finding AT_HWCAP2 from the initial process stack: */
176 p = (void *const *)argcp + 1 + *argcp + 1; /* start of environment */
177 /* step over environment */
180 for (auxv = (const struct auxv_entry *)p; auxv->type != AT_NULL; ++auxv) {
181 switch (auxv->type) {
193 if (hwcap & HWCAP_PACA)
194 putstr("# HWCAP_PACA present\n");
196 putstr("# HWCAP_PACA not present\n");
198 if (hwcap2 & HWCAP2_BTI) {
199 putstr("# HWCAP2_BTI present\n");
200 if (!(hwcap & HWCAP_PACA))
201 putstr("# Bad hardware? Expect problems.\n");
203 putstr("# HWCAP2_BTI not present\n");
207 putstr("# Test binary");
210 putstr(" built for BTI\n");
212 sa.sa_handler = (sighandler_t)(void *)handler;
213 sa.sa_flags = SA_SIGINFO;
214 sigemptyset(&sa.sa_mask);
215 sigaction(SIGILL, &sa, NULL);
216 sigaddset(&sa.sa_mask, SIGILL);
217 sigprocmask(SIG_UNBLOCK, &sa.sa_mask, NULL);
219 do_test(1, 1, 1, nohint_func);
220 do_test(1, 1, 1, bti_none_func);
221 do_test(1, 0, 0, bti_c_func);
222 do_test(0, 0, 1, bti_j_func);
223 do_test(0, 0, 0, bti_jc_func);
224 do_test(1, 0, 0, paciasp_func);
228 if (test_num - 1 != EXPECTED_TESTS)
229 putstr("# WARNING - EXPECTED TEST COUNT WRONG\n");