xref: /qemu/tests/tcg/aarch64/bti-1.c (revision d84ed5d2)
1 /*
2  * Branch target identification, basic notskip cases.
3  */
4 
5 #include "bti-crt.c.inc"
6 
7 static void skip2_sigill(int sig, siginfo_t *info, ucontext_t *uc)
8 {
9     uc->uc_mcontext.pc += 8;
10     uc->uc_mcontext.pstate = 1;
11 }
12 
13 #define NOP       "nop"
14 #define BTI_N     "hint #32"
15 #define BTI_C     "hint #34"
16 #define BTI_J     "hint #36"
17 #define BTI_JC    "hint #38"
18 
19 #define BTYPE_1(DEST) \
20     asm("mov %w0,#1; adr x16, 1f; br x16; 1: " DEST "; mov %w0,#0" \
21         : "=r"(skipped) : : "x16")
22 
23 #define BTYPE_2(DEST) \
24     asm("mov %w0,#1; adr x16, 1f; blr x16; 1: " DEST "; mov %w0,#0" \
25         : "=r"(skipped) : : "x16", "x30")
26 
27 #define BTYPE_3(DEST) \
28     asm("mov %w0,#1; adr x15, 1f; br x15; 1: " DEST "; mov %w0,#0" \
29         : "=r"(skipped) : : "x15")
30 
31 #define TEST(WHICH, DEST, EXPECT) \
32     do { WHICH(DEST); fail += skipped ^ EXPECT; } while (0)
33 
34 
35 int main()
36 {
37     int fail = 0;
38     int skipped;
39 
40     /* Signal-like with SA_SIGINFO.  */
41     signal_info(SIGILL, skip2_sigill);
42 
43     TEST(BTYPE_1, NOP, 1);
44     TEST(BTYPE_1, BTI_N, 1);
45     TEST(BTYPE_1, BTI_C, 0);
46     TEST(BTYPE_1, BTI_J, 0);
47     TEST(BTYPE_1, BTI_JC, 0);
48 
49     TEST(BTYPE_2, NOP, 1);
50     TEST(BTYPE_2, BTI_N, 1);
51     TEST(BTYPE_2, BTI_C, 0);
52     TEST(BTYPE_2, BTI_J, 1);
53     TEST(BTYPE_2, BTI_JC, 0);
54 
55     TEST(BTYPE_3, NOP, 1);
56     TEST(BTYPE_3, BTI_N, 1);
57     TEST(BTYPE_3, BTI_C, 1);
58     TEST(BTYPE_3, BTI_J, 0);
59     TEST(BTYPE_3, BTI_JC, 0);
60 
61     return fail;
62 }
63