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 // This file is modeled after x86_64.rs and comments are not copied over. For 9 // reference be sure to review the other file. Note that the pointer size is 10 // different so the reserved space at the top of the stack is 8 bytes, not 16 11 // bytes. Still two pointers though. 12 13 use core::arch::naked_asm; 14 15 #[unsafe(naked)] 16 pub(crate) unsafe extern "C" fn wasmtime_fiber_switch(top_of_stack: *mut u8) { 17 naked_asm!( 18 " 19 // Load our stack-to-use 20 mov eax, 0x4[esp] 21 mov ecx, -0x8[eax] 22 23 // Save callee-saved registers 24 push ebp 25 push ebx 26 push esi 27 push edi 28 29 // Save our current stack and jump to the stack-to-use 30 mov -0x8[eax], esp 31 mov esp, ecx 32 33 // Restore callee-saved registers 34 pop edi 35 pop esi 36 pop ebx 37 pop ebp 38 ret 39 ", 40 ) 41 } 42 43 #[unsafe(naked)] 44 pub(crate) unsafe extern "C" fn wasmtime_fiber_init( 45 top_of_stack: *mut u8, 46 entry_point: extern "C" fn(*mut u8, *mut u8), 47 entry_arg0: *mut u8, 48 ) { 49 naked_asm!( 50 " 51 mov eax, 4[esp] 52 53 // move top_of_stack to the 2nd argument 54 mov -0x0c[eax], eax 55 56 // move entry_arg0 to the 1st argument 57 mov ecx, 12[esp] 58 mov -0x10[eax], ecx 59 60 // Move our start function to the return address which the `ret` in 61 // `wasmtime_fiber_start` will return to. 62 lea ecx, {start} 63 mov -0x14[eax], ecx 64 65 // And move `entry_point` to get loaded into `%ebp` through the context 66 // switch. This'll get jumped to in `wasmtime_fiber_start`. 67 mov ecx, 8[esp] 68 mov -0x18[eax], ecx 69 70 // Our stack from top-to-bottom looks like: 71 // 72 // * 8 bytes of reserved space per unix.rs (two-pointers space) 73 // * 8 bytes of arguments (two arguments wasmtime_fiber_start forwards) 74 // * 4 bytes of return address 75 // * 16 bytes of saved registers 76 // 77 // Note that after the return address the stack is conveniently 16-byte 78 // aligned as required, so we just leave the arguments on the stack in 79 // `wasmtime_fiber_start` and immediately do the call. 80 lea ecx, -0x24[eax] 81 mov -0x08[eax], ecx 82 ret 83 ", 84 start = sym wasmtime_fiber_start, 85 ); 86 } 87 88 #[unsafe(naked)] 89 unsafe extern "C" fn wasmtime_fiber_start() -> ! { 90 naked_asm!( 91 " 92 .cfi_startproc simple 93 .cfi_def_cfa_offset 0 94 .cfi_escape 0x0f, /* DW_CFA_def_cfa_expression */ \ 95 5, /* the byte length of this expression */ \ 96 0x74, 0x08, /* DW_OP_breg4 (%esp) + 8 */ \ 97 0x06, /* DW_OP_deref */ \ 98 0x23, 0x14 /* DW_OP_plus_uconst 0x14 */ 99 100 .cfi_rel_offset eip, -4 101 .cfi_rel_offset ebp, -8 102 .cfi_rel_offset ebx, -12 103 .cfi_rel_offset esi, -16 104 .cfi_rel_offset edi, -20 105 106 // Our arguments and stack alignment are all prepped by 107 // `wasmtime_fiber_init`. 108 call ebp 109 ud2 110 .cfi_endproc 111 ", 112 ); 113 } 114