1 // A WORD OF CAUTION
2 //
3 // This entire file basically needs to be kept in sync with itself. It's not
4 // really possible to modify just one bit of this file without understanding
5 // all the other bits. Documentation tries to reference various bits here and
6 // there but try to make sure to read over everything before tweaking things!
7 //
8 // Also at this time this file is heavily based off the x86_64 file, so you'll
9 // probably want to read that one as well.
10 
11 use core::arch::naked_asm;
12 
13 #[unsafe(naked)]
14 pub(crate) unsafe extern "C" fn wasmtime_fiber_switch(top_of_stack: *mut u8 /* r0 */) {
15     naked_asm!(
16         "
17         // Save callee-saved registers
18         push {{r4-r11,lr}}
19 
20         // Swap stacks, recording our current stack pointer
21         ldr r4, [r0, #-0x08]
22         str sp, [r0, #-0x08]
23         mov sp, r4
24 
25         // Restore and return
26         pop {{r4-r11,lr}}
27         bx lr
28         ",
29     );
30 }
31 
32 #[unsafe(naked)]
33 pub(crate) unsafe extern "C" fn wasmtime_fiber_init(
34     top_of_stack: *mut u8,                        // r0
35     entry_point: extern "C" fn(*mut u8, *mut u8), // r1
36     entry_arg0: *mut u8,                          // r2
37 ) {
38     naked_asm!(
39         "
40         adr r3, {start}
41         str r3, [r0, #-0x0c] // => lr
42         str r0, [r0, #-0x10] // => r11
43         str r1, [r0, #-0x14] // => r10
44         str r2, [r0, #-0x18] // => r9
45 
46         add r3, r0, #-0x2c
47         str r3, [r0, #-0x08]
48         bx lr
49         ",
50         start = sym wasmtime_fiber_start,
51     );
52 }
53 
54 #[unsafe(naked)]
55 unsafe extern "C" fn wasmtime_fiber_start() -> ! {
56     naked_asm!(
57         "
58         .cfi_startproc simple
59         .cfi_def_cfa_offset 0
60         // See the x86_64 file for more commentary on what these CFI directives
61         // are doing. Like over there note that the relative offsets to
62         // registers here match the frame layout in `wasmtime_fiber_switch`.
63         //
64         // TODO: this is only lightly tested. This gets backtraces in gdb but
65         // not at runtime. Perhaps the libgcc at runtime was too old? Doesn't
66         // support something here? Unclear. Will need investigation if someone
67         // ends up needing this and it still doesn't work.
68         .cfi_escape 0x0f,    /* DW_CFA_def_cfa_expression */ \
69             5,               /* the byte length of this expression */ \
70             0x7d, 0x00,      /* DW_OP_breg14(%sp) + 0 */ \
71             0x06,            /* DW_OP_deref */ \
72             0x23, 0x24	 /* DW_OP_plus_uconst 0x24 */
73 
74         .cfi_rel_offset lr, -0x04
75         .cfi_rel_offset r11, -0x08
76         .cfi_rel_offset r10, -0x0c
77         .cfi_rel_offset r9, -0x10
78         .cfi_rel_offset r8, -0x14
79         .cfi_rel_offset r7, -0x18
80         .cfi_rel_offset r6, -0x1c
81         .cfi_rel_offset r5, -0x20
82         .cfi_rel_offset r4, -0x24
83 
84         mov r1, r11
85         mov r0, r9
86         blx r10
87         .cfi_endproc
88         ",
89     );
90 }
91