xref: /openbmc/qemu/tests/tcg/arm/pcalign-a32.c (revision 2df1eb27)
1 /* Test PC misalignment exception */
2 
3 #ifdef __thumb__
4 #error "This test must be compiled for ARM"
5 #endif
6 
7 #include <assert.h>
8 #include <signal.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 
12 static void *expected;
13 
14 static void sigbus(int sig, siginfo_t *info, void *vuc)
15 {
16     assert(info->si_code == BUS_ADRALN);
17     assert(info->si_addr == expected);
18     exit(EXIT_SUCCESS);
19 }
20 
21 int main()
22 {
23     void *tmp;
24 
25     struct sigaction sa = {
26         .sa_sigaction = sigbus,
27         .sa_flags = SA_SIGINFO
28     };
29 
30     if (sigaction(SIGBUS, &sa, NULL) < 0) {
31         perror("sigaction");
32         return EXIT_FAILURE;
33     }
34 
35     asm volatile("adr %0, 1f + 2\n\t"
36                  "str %0, %1\n\t"
37                  "bx  %0\n"
38                  "1:"
39                  : "=&r"(tmp), "=m"(expected));
40 
41     /*
42      * From v8, it is CONSTRAINED UNPREDICTABLE whether BXWritePC aligns
43      * the address or not.  If so, we can legitimately fall through.
44      */
45     return EXIT_SUCCESS;
46 }
47