use core::arch::{asm, global_asm};
use super::{allocate_obj_on_stack, push};
use crate::coroutine::adjusted_stack_base;
use crate::stack::{Stack, StackPointer};
use crate::unwind::{
asm_may_unwind_root, asm_may_unwind_yield, cfi_reset_args_size_root, cfi_reset_args_size_yield,
InitialFunc, StackCallFunc, TrapHandler,
};
use crate::util::EncodedValue;
pub const STACK_ALIGNMENT: usize = 16;
pub const PARENT_STACK_OFFSET: usize = 0;
pub const PARENT_LINK_OFFSET: usize = 8;
pub type StackWord = u32;
global_asm!(
".balign 16",
asm_function_begin!("stack_init_trampoline"),
".cfi_startproc",
cfi_signal_frame!(),
"push ebp",
"mov [edx - 8], esp",
"sub edx, 8",
"lea esp, [eax + 4]",
"mov ebp, edx",
".cfi_escape 0x0f, 5, 0x75, 0x00, 0x06, 0x23, 0x0c",
".cfi_offset esi, -4",
".cfi_offset eip, -8",
".cfi_offset ebp, -12",
"call 2f",
"2:",
"jmp [edx + 4]",
asm_function_alt_entry!("stack_init_trampoline_return"),
"int3",
".cfi_endproc",
asm_function_end!("stack_init_trampoline"),
);
global_asm!(
".balign 16",
asm_function_begin!("stack_call_trampoline"),
".cfi_startproc",
cfi_signal_frame!(),
"push ebp",
"mov ebp, esp",
".cfi_def_cfa ebp, 8",
".cfi_offset ebp, -8",
"mov esp, edx",
"call eax",
"mov esp, ebp",
"pop ebp",
"ret",
".cfi_endproc",
asm_function_end!("stack_call_trampoline"),
);
extern "C" {
fn stack_init_trampoline(arg: EncodedValue, stack_base: StackPointer, stack_ptr: StackPointer);
static stack_init_trampoline_return: [u8; 0];
#[allow(dead_code)]
fn stack_call_trampoline(arg: *mut u8, sp: StackPointer, f: StackCallFunc);
}
#[inline]
pub unsafe fn init_stack<T>(stack: &impl Stack, func: InitialFunc<T>, obj: T) -> StackPointer {
let mut sp = adjusted_stack_base(stack).get();
push(&mut sp, Some(func as StackWord));
push(&mut sp, None);
allocate_obj_on_stack(&mut sp, 8, obj);
let initial_obj = sp;
push(&mut sp, None);
push(&mut sp, None);
push(&mut sp, None);
push(&mut sp, Some(initial_obj as StackWord));
push(
&mut sp,
Some(stack_init_trampoline as *const () as StackWord),
);
StackPointer::new_unchecked(sp)
}
#[inline]
pub unsafe fn switch_and_link(
arg: EncodedValue,
sp: StackPointer,
stack_base: StackPointer,
) -> (EncodedValue, Option<StackPointer>) {
let (ret_val, ret_sp);
asm_may_unwind_root!(
"push esi",
cfi_reset_args_size_root!(),
"call [eax]",
"pop esi",
inlateout("ecx") arg => ret_val,
lateout("edx") ret_sp,
in("edx") stack_base.get(),
in("eax") sp.get(),
lateout("ebx") _, lateout("edi") _,
clobber_abi("C"),
);
(ret_val, StackPointer::new(ret_sp))
}
#[inline(always)]
pub unsafe fn switch_yield(arg: EncodedValue, parent_link: *mut StackPointer) -> EncodedValue {
let ret_val;
asm_may_unwind_yield!(
"push ebp",
"push esi",
"call 2f",
"2:",
".equ .Loffset_yield, 3f - 2b",
"add dword ptr [esp], offset .Loffset_yield",
"mov edx, esp",
"mov esp, [eax]",
"pop ebp",
cfi_reset_args_size_yield!(),
"ret",
"3:",
"push ebp",
"mov [edx - 8], esp",
"lea esp, [eax + 4]",
"pop esi",
"pop ebp",
inlateout("ecx") arg => ret_val,
in("eax") parent_link,
lateout("ebx") _, lateout("edi") _,
clobber_abi("C"),
);
ret_val
}
#[inline(always)]
pub unsafe fn switch_and_reset(arg: EncodedValue, parent_link: *mut StackPointer) -> ! {
asm!(
"mov esp, [{parent_link}]",
"pop ebp",
"ret",
parent_link = in(reg) parent_link,
in("ecx") arg,
in("edx") 0,
options(noreturn),
);
}
#[inline]
#[cfg(feature = "asm-unwind")]
pub unsafe fn switch_and_throw(
forced_unwind: crate::unwind::ForcedUnwind,
sp: StackPointer,
stack_base: StackPointer,
) -> (EncodedValue, Option<StackPointer>) {
extern "fastcall-unwind" fn throw(forced_unwind: crate::unwind::ForcedUnwind) -> ! {
extern crate std;
use std::boxed::Box;
std::panic::resume_unwind(Box::new(forced_unwind));
}
let (ret_val, ret_sp);
asm_may_unwind_root!(
"push esi",
"call 2f",
"2:",
".equ .Loffset_throw, 3f - 2b",
"add dword ptr [esp], offset .Loffset_throw",
"push ebp",
"mov [edx - 8], esp",
"mov esp, eax",
"pop eax",
"pop esi",
"pop ebp",
cfi_reset_args_size_root!(),
"push eax",
"jmp {throw}",
"3:",
"pop esi",
throw = sym throw,
in("ecx") forced_unwind.0.get(),
lateout("ecx") ret_val,
lateout("edx") ret_sp,
in("edx") stack_base.get(),
in("eax") sp.get(),
lateout("ebx") _, lateout("edi") _,
clobber_abi("C"),
);
(ret_val, StackPointer::new(ret_sp))
}
#[inline]
pub unsafe fn drop_initial_obj(
_stack_base: StackPointer,
stack_ptr: StackPointer,
drop_fn: unsafe fn(ptr: *mut u8),
) {
let ptr = (stack_ptr.get() as *mut u8).add(20);
drop_fn(ptr);
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub struct TrapHandlerRegs {
pub eip: u32,
pub esp: u32,
pub ebp: u32,
pub ecx: u32,
pub edx: u32,
}
pub unsafe fn setup_trap_trampoline<T>(
stack_base: StackPointer,
val: T,
handler: TrapHandler<T>,
) -> TrapHandlerRegs {
let parent_link = stack_base.get() - PARENT_LINK_OFFSET;
let mut sp = parent_link;
allocate_obj_on_stack(&mut sp, 8, val);
let val_ptr = sp;
debug_assert_eq!(sp % STACK_ALIGNMENT, 0);
push(
&mut sp,
Some(stack_init_trampoline_return.as_ptr() as StackWord),
);
TrapHandlerRegs {
eip: handler as u32,
esp: sp as u32,
ecx: val_ptr as u32,
edx: parent_link as u32,
ebp: parent_link as u32,
}
}
#[inline]
pub unsafe fn on_stack(arg: *mut u8, stack: impl Stack, f: StackCallFunc) {
asm_may_unwind_root!(
cfi_reset_args_size_root!(),
concat!("call ", asm_mangle!("stack_call_trampoline")),
"nop",
in("ecx") arg,
in("edx") adjusted_stack_base(&stack).get(),
in("eax") f,
clobber_abi("fastcall"),
)
}