use core::mem;
use stack::Stack;
pub const STACK_ALIGNMENT: usize = 4;
#[derive(Debug, Clone, Copy)]
pub struct StackPointer(*mut usize);
pub unsafe fn init(stack: &Stack, f: unsafe extern "C" fn(usize, StackPointer) -> !) -> StackPointer {
#[naked]
unsafe extern "C" fn trampoline_1() {
asm!(
r#"
# gdb has a hardcoded check that rejects backtraces where frame addresses
# do not monotonically decrease. It is turned off if the function is called
# "__morestack" and that is hardcoded. So, to make gdb backtraces match
# the actual unwinder behavior, we call ourselves "__morestack" and mark
# the symbol as local; it shouldn't interfere with anything.
__morestack:
.local __morestack
# Set up the first part of our DWARF CFI linking stacks together. When
# we reach this function from unwinding, r2 will be pointing at the bottom
# of the parent linked stack. This link is set each time swap() is called.
# When unwinding the frame corresponding to this function, a DWARF unwinder
# will use r2+8 as the next call frame address, restore r2 from CFA-4 and
# restore return address (r9) from CFA-8. This mirrors what the second half
# of `swap_trampoline` does.
.cfi_def_cfa r2, 8
.cfi_offset r2, -4
.cfi_offset r9, -8
# This nop is here so that the initial swap doesn't return to the start
# of the trampoline, which confuses the unwinder since it will look for
# frame information in the previous symbol rather than this one. It is
# never actually executed.
l.nop
.Lend:
.size __morestack, .Lend-__morestack
"#
: : : : "volatile")
}
#[naked]
unsafe extern "C" fn trampoline_2() {
asm!(
r#"
# Set up the second part of our DWARF CFI.
# When unwinding the frame corresponding to this function, a DWARF unwinder
# will restore r2 (and thus CFA of the first trampoline) from the stack slot.
# This stack slot is updated every time swap() is called to point to the bottom
# of the stack of the context switch just switched from.
.cfi_def_cfa r2, 8
.cfi_offset r2, -4
.cfi_offset r9, -8
# This nop is here so that the return address of the swap trampoline
# doesn't point to the start of the symbol. This confuses gdb's backtraces,
# causing them to think the parent function is trampoline_1 instead of
# trampoline_2.
l.nop
# Call the provided function.
l.lwz r5, 8(r1)
l.jalr r5
l.nop
"#
: : : : "volatile")
}
unsafe fn push(sp: &mut StackPointer, val: usize) {
sp.0 = sp.0.offset(-1);
*sp.0 = val
}
let mut sp = StackPointer(stack.base() as *mut usize);
push(&mut sp, f as usize);
push(&mut sp, 0xdead0cfa); push(&mut sp, trampoline_1 as usize + 4);
let frame = sp;
push(&mut sp, frame.0 as usize); push(&mut sp, trampoline_2 as usize + 4);
frame
}
#[inline(always)]
pub unsafe fn swap(arg: usize, new_sp: StackPointer,
new_stack: Option<&Stack>) -> (usize, StackPointer) {
let mut dummy: usize = mem::uninitialized();
let new_cfa = if let Some(new_stack) = new_stack {
(new_stack.base() as *mut usize).offset(-2)
} else {
&mut dummy
};
#[naked]
unsafe extern "C" fn trampoline() {
asm!(
r#"
# Save the frame pointer and link register; the unwinder uses them to find
# the CFA of the caller, and so they have to have the correct value immediately
# after the call instruction that invoked the trampoline.
l.sw -4(r1), r2
l.sw -8(r1), r9
.cfi_offset r2, -4
.cfi_offset r9, -8
# Link the call stacks together by writing the current stack bottom
# address to the CFA slot in the new stack.
l.addi r7, r1, -8
l.sw 0(r6), r7
# Pass the stack pointer of the old context to the new one.
l.or r4, r0, r1
# Load stack pointer of the new context.
l.or r1, r0, r5
# Restore frame pointer and link register of the new context.
# Load frame and instruction pointers of the new context.
l.lwz r2, -4(r1)
l.lwz r9, -8(r1)
# Return into the new context.
l.jr r9
l.nop
"#
: : : : "volatile")
}
let ret: usize;
let ret_sp: *mut usize;
asm!(
r#"
# Call the trampoline to switch to the new context.
l.jal ${2}
l.nop
"#
: "={r3}" (ret)
"={r4}" (ret_sp)
: "s" (trampoline as usize)
"{r3}" (arg)
"{r5}" (new_sp.0)
"{r6}" (new_cfa)
:"r5", "r6", "r7",
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
"r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
"r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
"cc", "memory"
: "volatile");
(ret, StackPointer(ret_sp))
}