Path: blob/main/crates/fiber/src/stackswitch/riscv32imac.rs
3055 views
// A WORD OF CAUTION1//2// This entire file basically needs to be kept in sync with itself. It's not3// really possible to modify just one bit of this file without understanding4// all the other bits. Documentation tries to reference various bits here and5// there but try to make sure to read over everything before tweaking things!6//7// This file is modelled after riscv64.rs. For reference be sure to review the8// other file.910use core::arch::naked_asm;1112#[inline(never)] // FIXME(rust-lang/rust#148307)13pub(crate) unsafe extern "C" fn wasmtime_fiber_switch(top_of_stack: *mut u8) {14unsafe { wasmtime_fiber_switch_(top_of_stack) }15}1617#[unsafe(naked)]18unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8 /* a0 */) {19naked_asm!(20"21// See https://github.com/rust-lang/rust/issues/80608.22.attribute arch, \"rv32i\" // This implementation should work for any23// architecture with the same registers as riscv32i, e.g. riscv32imac,24// but not riscv32gc.2526// We're switching to arbitrary code somewhere else, so pessimistically27// assume that all callee-save register are clobbered. This means we need28// to save/restore all of them.29//30// Note that this order for saving is important since we use CFI directives31// below to point to where all the saved registers are.32sw ra, -0x4(sp)33sw fp, -0x8(sp) // fp is s034sw s1, -0xc(sp)35sw s2, -0x10(sp)36sw s3, -0x14(sp)37sw s4, -0x18(sp)38sw s5, -0x1c(sp)39sw s6, -0x20(sp)40sw s7, -0x24(sp)41sw s8, -0x28(sp)42sw s9, -0x2c(sp)43sw s10, -0x30(sp)44sw s11, -0x34(sp)45addi sp, sp, -0x40 // Choose 0x40 to be 16-byte aligned4647lw t0, -0x8(a0)48sw sp, -0x8(a0)4950// Swap stacks and restore all our callee-saved registers51mv sp, t05253lw s11, 0xc(sp)54lw s10, 0x10(sp)55lw s9, 0x14(sp)56lw s8, 0x18(sp)57lw s7, 0x1c(sp)58lw s6, 0x20(sp)59lw s5, 0x24(sp)60lw s4, 0x28(sp)61lw s3, 0x2c(sp)62lw s2, 0x30(sp)63lw s1, 0x34(sp)64lw fp, 0x38(sp)65lw ra, 0x3c(sp)66addi sp, sp, 0x4067jr ra68",69);70}7172pub(crate) unsafe fn wasmtime_fiber_init(73top_of_stack: *mut u8,74entry_point: extern "C" fn(*mut u8, *mut u8),75entry_arg0: *mut u8,76) {77#[repr(C)]78#[derive(Default)]79struct InitialStack {80padding: [u8; 12], // 12 bytes of padding for 16-byte alignment8182s11: *mut u8,83s10: *mut u8,84s9: *mut u8,85s8: *mut u8,86s7: *mut u8,87s6: *mut u8,88s5: *mut u8,89s4: *mut u8,90s3: *mut u8,91s2: *mut u8,92s1: *mut u8,93fp: *mut u8,9495ra: *mut u8,9697// unix.rs reserved space98padding_2: [u8; 8], // 8 bytes of padding for 16-byte alignment99last_sp: *mut u8,100run_result: *mut u8,101}102103unsafe {104let initial_stack = top_of_stack.cast::<InitialStack>().sub(1);105initial_stack.write(InitialStack {106s1: entry_point as *mut u8,107s2: entry_arg0,108fp: top_of_stack,109ra: wasmtime_fiber_start as *mut u8,110last_sp: initial_stack.cast(),111..InitialStack::default()112});113}114}115116#[unsafe(naked)]117unsafe extern "C" fn wasmtime_fiber_start() -> ! {118naked_asm!(119"120.cfi_startproc simple121.cfi_def_cfa_offset 0122123124.cfi_escape 0x0f, /* DW_CFA_def_cfa_expression */ \1255, /* the byte length of this expression */ \1260x52, /* DW_OP_reg2 (sp) */ \1270x06, /* DW_OP_deref */ \1280x08, 0x40, /* DW_OP_const1u 0x40 */ \1290x22 /* DW_OP_plus */130131132.cfi_rel_offset ra, -0x4133.cfi_rel_offset fp, -0x8134.cfi_rel_offset s1, -0xc135.cfi_rel_offset s2, -0x10136.cfi_rel_offset s3, -0x14137.cfi_rel_offset s4, -0x18138.cfi_rel_offset s5, -0x1c139.cfi_rel_offset s6, -0x20140.cfi_rel_offset s7, -0x24141.cfi_rel_offset s8, -0x28142.cfi_rel_offset s9, -0x2c143.cfi_rel_offset s10, -0x30144.cfi_rel_offset s11, -0x34145146mv a0, s2147mv a1, fp148jalr s1149// .4byte 0 will cause panic.150// for safety just like x86_64.rs and riscv64.rs.151.4byte 0152.cfi_endproc153",154);155}156157158