xref: /openbmc/qemu/tests/tcg/x86_64/noexec.c (revision ed75658a)
1 #include "../multiarch/noexec.c.inc"
2 
3 static void *arch_mcontext_pc(const mcontext_t *ctx)
4 {
5     return (void *)ctx->gregs[REG_RIP];
6 }
7 
8 int arch_mcontext_arg(const mcontext_t *ctx)
9 {
10     return ctx->gregs[REG_RDI];
11 }
12 
13 static void arch_flush(void *p, int len)
14 {
15 }
16 
17 extern char noexec_1[];
18 extern char noexec_2[];
19 extern char noexec_end[];
20 
21 asm("noexec_1:\n"
22     "    movq $1,%rdi\n"    /* %rdi is 0 on entry, set 1. */
23     "noexec_2:\n"
24     "    movq $2,%rdi\n"    /* %rdi is 0/1; set 2. */
25     "    ret\n"
26     "noexec_end:");
27 
28 int main(void)
29 {
30     struct noexec_test noexec_tests[] = {
31         {
32             .name = "fallthrough",
33             .test_code = noexec_1,
34             .test_len = noexec_end - noexec_1,
35             .page_ofs = noexec_1 - noexec_2,
36             .entry_ofs = noexec_1 - noexec_2,
37             .expected_si_ofs = 0,
38             .expected_pc_ofs = 0,
39             .expected_arg = 1,
40         },
41         {
42             .name = "jump",
43             .test_code = noexec_1,
44             .test_len = noexec_end - noexec_1,
45             .page_ofs = noexec_1 - noexec_2,
46             .entry_ofs = 0,
47             .expected_si_ofs = 0,
48             .expected_pc_ofs = 0,
49             .expected_arg = 0,
50         },
51         {
52             .name = "fallthrough [cross]",
53             .test_code = noexec_1,
54             .test_len = noexec_end - noexec_1,
55             .page_ofs = noexec_1 - noexec_2 - 2,
56             .entry_ofs = noexec_1 - noexec_2 - 2,
57             .expected_si_ofs = 0,
58             .expected_pc_ofs = -2,
59             .expected_arg = 1,
60         },
61         {
62             .name = "jump [cross]",
63             .test_code = noexec_1,
64             .test_len = noexec_end - noexec_1,
65             .page_ofs = noexec_1 - noexec_2 - 2,
66             .entry_ofs = -2,
67             .expected_si_ofs = 0,
68             .expected_pc_ofs = -2,
69             .expected_arg = 0,
70         },
71     };
72 
73     return test_noexec(noexec_tests,
74                        sizeof(noexec_tests) / sizeof(noexec_tests[0]));
75 }
76