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;
cfg_if::cfg_if! {
if #[cfg(target_os = "uefi")] {
macro_rules! seh {
($asm:expr) => { $asm }
}
macro_rules! cfi {
($asm:expr) => { "" }
}
} else {
macro_rules! seh {
($asm:expr) => { "" }
}
macro_rules! cfi {
($asm:expr) => { $asm }
}
}
}
pub const STACK_ALIGNMENT: usize = 16;
pub const PARENT_STACK_OFFSET: usize = 0;
pub const PARENT_LINK_OFFSET: usize = 16;
pub type StackWord = u64;
global_asm!(
".balign 4",
asm_function_begin!("stack_init_trampoline"),
cfi!(".cfi_startproc"),
seh!(".seh_proc stack_init_trampoline"),
cfi!(cfi_signal_frame!()),
"stp x29, lr, [sp, #-32]!",
"str x19, [sp, #16]",
"mov x3, sp",
"str x3, [x1, #-16]!",
"add sp, x2, #32",
"mov x29, x1",
seh!(".seh_save_fplr_x 32"),
seh!(".seh_save_reg x19, 16"),
seh!(".seh_nop"),
seh!(".seh_set_fp"),
seh!(".seh_save_fplr 0"),
seh!(".seh_set_fp"),
seh!(".seh_endprologue"),
cfi!(".cfi_escape 0x0f, 5, 0x8d, 0x00, 0x06, 0x23, 0x20"),
cfi!(".cfi_offset x19, -16"),
cfi!(".cfi_offset lr, -24"),
cfi!(".cfi_offset x29, -32"),
"mov x2, sp",
"adr lr, 0f",
"ldr x3, [x1, #8]",
"br x3",
"0:",
asm_function_alt_entry!("stack_init_trampoline_return"),
"brk #0",
seh!(".seh_endproc"),
cfi!(".cfi_endproc"),
asm_function_end!("stack_init_trampoline"),
);
global_asm!(
".balign 4",
asm_function_begin!("stack_call_trampoline"),
cfi!(".cfi_startproc"),
seh!(".seh_proc stack_call_trampoline"),
cfi!(cfi_signal_frame!()),
"stp x29, x30, [sp, #-16]!",
"mov x29, sp",
seh!(".seh_save_fplr_x 16"),
seh!(".seh_set_fp"),
seh!(".seh_endprologue"),
cfi!(".cfi_def_cfa x29, 16"),
cfi!(".cfi_offset x30, -8"),
cfi!(".cfi_offset x29, -16"),
"mov sp, x1",
"blr x2",
"mov sp, x29",
"ldp x29, x30, [sp], #16",
"ret",
seh!(".seh_endproc"),
cfi!(".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, 16, obj);
debug_assert_eq!(sp % STACK_ALIGNMENT, 0);
push(&mut sp, None);
push(
&mut sp,
Some(stack_init_trampoline as *const () as StackWord),
);
push(&mut sp, None);
push(&mut sp, None);
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!(
cfi_reset_args_size_root!(),
"ldr x3, [x2, #16]",
"blr x3",
"add sp, x2, #32",
inlateout("x0") arg => ret_val,
lateout("x1") ret_sp,
in("x1") stack_base.get() as u64,
in("x2") sp.get() as u64,
lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _,
lateout("x24") _, lateout("x25") _, lateout("x26") _, lateout("x27") _,
lateout("x28") _,
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!(
"stp x19, x29, [sp, #-32]!",
"adr lr, 0f",
"str lr, [sp, #16]",
"ldr x2, [x2]",
"mov x1, sp",
"ldr x19, [x2, #16]",
"ldp x29, lr, [x2]",
cfi_reset_args_size_yield!(),
"ret",
"0:",
"stp x29, lr, [sp, #-32]!",
"str x19, [sp, #16]",
"mov x3, sp",
"str x3, [x1, #-16]",
"ldp x19, x29, [x2]",
"add sp, x2, #32",
inlateout("x0") arg => ret_val,
in("x2") parent_link as u64,
lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _,
lateout("x24") _, lateout("x25") _, lateout("x26") _, lateout("x27") _,
lateout("x28") _,
clobber_abi("C"),
);
ret_val
}
#[inline(always)]
pub unsafe fn switch_and_reset(arg: EncodedValue, parent_link: *mut StackPointer) -> ! {
asm!(
"ldr x2, [{parent_link}]",
"ldr x19, [x2, #16]",
"ldp x29, lr, [x2]",
"ret",
parent_link = in(reg) parent_link as u64,
in("x0") arg,
in("x1") 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 "C-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!(
"adr lr, 0f",
"stp x29, lr, [sp, #-32]!",
"str x19, [sp, #16]",
"mov x3, sp",
"str x3, [x1, #-16]",
"ldr lr, [x2, #16]",
"ldp x19, x29, [x2]",
"add sp, x2, #32",
cfi_reset_args_size_root!(),
"b {throw}",
"0:",
"add sp, x2, #32",
throw = sym throw,
in("x0") forced_unwind.0.get(),
lateout("x0") ret_val,
lateout("x1") ret_sp,
in("x1") stack_base.get() as u64,
in("x2") sp.get() as u64,
lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _,
lateout("x24") _, lateout("x25") _, lateout("x26") _, lateout("x27") _,
lateout("x28") _,
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(32);
drop_fn(ptr);
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub struct TrapHandlerRegs {
pub pc: u64,
pub sp: u64,
pub x0: u64,
pub x1: u64,
pub x29: u64,
pub lr: u64,
}
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, 16, val);
let val_ptr = sp;
TrapHandlerRegs {
pc: handler as u64,
sp: sp as u64,
x0: val_ptr as u64,
x1: parent_link as u64,
x29: parent_link as u64,
lr: stack_init_trampoline_return.as_ptr() as u64,
}
}
#[inline]
pub unsafe fn on_stack(arg: *mut u8, stack: impl Stack, f: StackCallFunc) {
asm_may_unwind_root!(
cfi_reset_args_size_root!(),
concat!("bl ", asm_mangle!("stack_call_trampoline")),
"nop",
in("x0") arg,
in("x1") adjusted_stack_base(&stack).get(),
in("x2") f,
clobber_abi("C"),
);
}