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 pub(crate) unsafe fn wasmtime_fiber_init( 33 top_of_stack: *mut u8, 34 entry_point: extern "C" fn(*mut u8, *mut u8), 35 entry_arg0: *mut u8, 36 ) { 37 #[repr(C)] 38 #[derive(Default)] 39 struct InitialStack { 40 r4: *mut u8, 41 r5: *mut u8, 42 r6: *mut u8, 43 r7: *mut u8, 44 r8: *mut u8, 45 r9: *mut u8, 46 r10: *mut u8, 47 r11: *mut u8, 48 lr: *mut u8, 49 50 // unix.rs reserved space 51 last_sp: *mut u8, 52 run_result: *mut u8, 53 } 54 55 unsafe { 56 let initial_stack = top_of_stack.cast::<InitialStack>().sub(1); 57 initial_stack.write(InitialStack { 58 r9: entry_arg0, 59 r10: entry_point as *mut u8, 60 r11: top_of_stack, 61 lr: wasmtime_fiber_start as *mut u8, 62 last_sp: initial_stack.cast(), 63 ..InitialStack::default() 64 }); 65 } 66 } 67 68 #[unsafe(naked)] 69 unsafe extern "C" fn wasmtime_fiber_start() -> ! { 70 naked_asm!( 71 " 72 .cfi_startproc simple 73 .cfi_def_cfa_offset 0 74 // See the x86_64 file for more commentary on what these CFI directives 75 // are doing. Like over there note that the relative offsets to 76 // registers here match the frame layout in `wasmtime_fiber_switch`. 77 // 78 // TODO: this is only lightly tested. This gets backtraces in gdb but 79 // not at runtime. Perhaps the libgcc at runtime was too old? Doesn't 80 // support something here? Unclear. Will need investigation if someone 81 // ends up needing this and it still doesn't work. 82 .cfi_escape 0x0f, /* DW_CFA_def_cfa_expression */ \ 83 5, /* the byte length of this expression */ \ 84 0x7d, 0x00, /* DW_OP_breg14(%sp) + 0 */ \ 85 0x06, /* DW_OP_deref */ \ 86 0x23, 0x24 /* DW_OP_plus_uconst 0x24 */ 87 88 .cfi_rel_offset lr, -0x04 89 .cfi_rel_offset r11, -0x08 90 .cfi_rel_offset r10, -0x0c 91 .cfi_rel_offset r9, -0x10 92 .cfi_rel_offset r8, -0x14 93 .cfi_rel_offset r7, -0x18 94 .cfi_rel_offset r6, -0x1c 95 .cfi_rel_offset r5, -0x20 96 .cfi_rel_offset r4, -0x24 97 98 mov r1, r11 99 mov r0, r9 100 blx r10 101 .cfi_endproc 102 ", 103 ); 104 } 105