2016-07-16 09:22:41 +08:00
|
|
|
// This file is part of libfringe, a low-level green threading library.
|
|
|
|
// Copyright (c) edef <edef@edef.eu>,
|
|
|
|
// whitequark <whitequark@whitequark.org>
|
2016-08-21 05:45:01 +08:00
|
|
|
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
|
|
|
|
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
|
|
|
|
// http://opensource.org/licenses/MIT>, at your option. This file may not be
|
|
|
|
// copied, modified, or distributed except according to those terms.
|
2016-07-16 09:22:41 +08:00
|
|
|
|
2016-07-18 05:42:45 +08:00
|
|
|
// To understand the machine code in this file, keep in mind these facts:
|
|
|
|
// * i686 SysV C ABI requires the stack to be aligned at function entry,
|
|
|
|
// so that `%esp+4` is a multiple of 16. Aligned operands are a requirement
|
|
|
|
// of SIMD instructions, and making this the responsibility of the caller
|
|
|
|
// avoids having to maintain a frame pointer, which is necessary when
|
|
|
|
// a function has to realign the stack from an unknown state.
|
|
|
|
// * i686 SysV C ABI passes the first argument on the stack. This is
|
|
|
|
// unfortunate, because unlike every other architecture we can't reuse
|
|
|
|
// `swap` for the initial call, and so we use a trampoline.
|
2016-07-16 09:22:41 +08:00
|
|
|
use stack::Stack;
|
|
|
|
|
2016-09-03 06:14:07 +08:00
|
|
|
pub const STACK_ALIGNMENT: usize = 16;
|
|
|
|
|
2016-09-03 19:03:30 +08:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2016-07-16 09:22:41 +08:00
|
|
|
pub struct StackPointer(*mut usize);
|
|
|
|
|
|
|
|
pub unsafe fn init(stack: &Stack, f: unsafe extern "C" fn(usize) -> !) -> StackPointer {
|
2016-09-03 20:38:24 +08:00
|
|
|
#[cfg(not(target_vendor = "apple"))]
|
2016-07-17 04:17:22 +08:00
|
|
|
#[naked]
|
2016-09-03 20:25:36 +08:00
|
|
|
unsafe extern "C" fn trampoline() {
|
2016-07-17 04:17:22 +08:00
|
|
|
asm!(
|
|
|
|
r#"
|
2016-07-18 05:42:45 +08:00
|
|
|
# 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
|
|
|
|
|
2016-09-03 20:25:36 +08:00
|
|
|
# When a normal function is entered, the return address is pushed onto the stack,
|
|
|
|
# and the first thing it does is pushing the frame pointer. The init trampoline
|
|
|
|
# is not a normal function; on entry the stack pointer is one word above the place
|
|
|
|
# where the return address should be, and right under it the return address as
|
|
|
|
# well as the stack pointer are already pre-filled. So, simply move the stack
|
|
|
|
# pointer where it belongs; and add CFI just like in any other function prologue.
|
|
|
|
subl $$8, %esp
|
|
|
|
.cfi_def_cfa_offset 8
|
2016-07-18 05:42:45 +08:00
|
|
|
.cfi_offset %ebp, -8
|
2016-09-03 20:25:36 +08:00
|
|
|
movl %esp, %ebp
|
|
|
|
.cfi_def_cfa_register %ebp
|
|
|
|
# Call f.
|
|
|
|
pushl %eax
|
|
|
|
calll *12(%esp)
|
2016-07-18 05:42:45 +08:00
|
|
|
|
|
|
|
.Lend:
|
|
|
|
.size __morestack, .Lend-__morestack
|
|
|
|
"#
|
2016-09-01 23:39:34 +08:00
|
|
|
: : : : "volatile")
|
2016-07-17 04:17:22 +08:00
|
|
|
}
|
2016-07-16 09:22:41 +08:00
|
|
|
|
2016-09-03 20:38:24 +08:00
|
|
|
#[cfg(target_vendor = "apple")]
|
|
|
|
#[naked]
|
|
|
|
unsafe extern "C" fn trampoline() {
|
|
|
|
asm!(
|
|
|
|
r#"
|
|
|
|
# Identical to the above, except avoids .local/.size that aren't available on Mach-O.
|
|
|
|
__morestack:
|
|
|
|
.private_extern __morestack
|
|
|
|
|
|
|
|
subl $$8, %esp
|
|
|
|
.cfi_def_cfa_offset 8
|
|
|
|
.cfi_offset %ebp, -8
|
|
|
|
movl %esp, %ebp
|
|
|
|
.cfi_def_cfa_register %ebp
|
|
|
|
# Call f.
|
|
|
|
pushl %eax
|
|
|
|
calll *12(%esp)
|
|
|
|
"#
|
|
|
|
: : : : "volatile")
|
|
|
|
}
|
|
|
|
|
2016-07-17 13:55:03 +08:00
|
|
|
unsafe fn push(sp: &mut StackPointer, val: usize) {
|
|
|
|
sp.0 = sp.0.offset(-1);
|
|
|
|
*sp.0 = val
|
|
|
|
}
|
|
|
|
|
2016-08-12 07:18:36 +08:00
|
|
|
let mut sp = StackPointer(stack.base() as *mut usize);
|
2016-09-03 20:25:36 +08:00
|
|
|
push(&mut sp, f as usize); // function
|
|
|
|
push(&mut sp, trampoline as usize); // trampoline / linked return address
|
|
|
|
push(&mut sp, 0xdead0bbb); // initial %ebp / linked %ebp
|
2016-07-16 09:22:41 +08:00
|
|
|
sp
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2016-09-03 19:03:30 +08:00
|
|
|
pub unsafe fn swap(arg: usize, old_sp: *mut StackPointer, new_sp: StackPointer,
|
2016-07-18 05:42:45 +08:00
|
|
|
new_stack: &Stack) -> usize {
|
|
|
|
// Address of the topmost CFA stack slot.
|
2016-09-03 20:25:36 +08:00
|
|
|
let new_cfa = (new_stack.base() as *mut usize).offset(-3);
|
2016-07-18 05:42:45 +08:00
|
|
|
|
|
|
|
#[naked]
|
2016-08-12 06:49:30 +08:00
|
|
|
unsafe extern "C" fn trampoline() {
|
2016-07-18 05:42:45 +08:00
|
|
|
asm!(
|
|
|
|
r#"
|
2016-09-03 20:25:36 +08:00
|
|
|
# Remember the frame and instruction pointers in the callee, to link
|
|
|
|
# the stacks together later. We put them on stack because x86 doesn't
|
|
|
|
# have enough registers.
|
|
|
|
movl %ebp, -8(%edx)
|
|
|
|
movl (%esp), %ebx
|
|
|
|
movl %ebx, -12(%edx)
|
|
|
|
|
2016-07-18 05:42:45 +08:00
|
|
|
# Save frame pointer explicitly; the unwinder uses it to find CFA of
|
|
|
|
# the caller, and so it has to have the correct value immediately after
|
|
|
|
# the call instruction that invoked the trampoline.
|
|
|
|
pushl %ebp
|
|
|
|
|
|
|
|
# Save stack pointer of the old context.
|
2016-09-03 19:03:30 +08:00
|
|
|
movl %esp, (%esi)
|
|
|
|
# Load stack pointer of the new context.
|
|
|
|
movl %edx, %esp
|
2016-07-18 05:42:45 +08:00
|
|
|
|
2016-09-03 20:25:36 +08:00
|
|
|
# Load frame and instruction pointers of the new context.
|
2016-07-18 05:42:45 +08:00
|
|
|
popl %ebp
|
|
|
|
popl %ebx
|
2016-09-03 20:25:36 +08:00
|
|
|
|
|
|
|
# Put the frame and instruction pointers into the trampoline stack frame,
|
|
|
|
# making it appear to return right after the call instruction that invoked
|
|
|
|
# this trampoline. This is done after the loads above, since on the very first
|
|
|
|
# swap, the saved %ebp/%ebx intentionally alias 0(%edi)/4(%edi).
|
|
|
|
movl -8(%edx), %esi
|
|
|
|
movl %esi, 0(%edi)
|
|
|
|
movl -12(%edx), %esi
|
|
|
|
movl %esi, 4(%edi)
|
|
|
|
|
|
|
|
# Return into new context.
|
2016-07-18 05:42:45 +08:00
|
|
|
jmpl *%ebx
|
|
|
|
"#
|
2016-09-01 23:39:34 +08:00
|
|
|
: : : : "volatile")
|
2016-07-18 05:42:45 +08:00
|
|
|
}
|
|
|
|
|
2016-07-16 09:22:41 +08:00
|
|
|
let ret: usize;
|
|
|
|
asm!(
|
|
|
|
r#"
|
|
|
|
# Push instruction pointer of the old context and switch to
|
|
|
|
# the new context.
|
2016-07-18 05:42:45 +08:00
|
|
|
call ${1:c}
|
2016-07-16 09:22:41 +08:00
|
|
|
"#
|
|
|
|
: "={eax}" (ret)
|
2016-08-12 06:49:30 +08:00
|
|
|
: "s" (trampoline as usize)
|
2016-07-18 05:42:45 +08:00
|
|
|
"{eax}" (arg)
|
2016-07-16 09:22:41 +08:00
|
|
|
"{esi}" (old_sp)
|
2016-09-03 19:03:30 +08:00
|
|
|
"{edx}" (new_sp.0)
|
2016-07-18 05:42:45 +08:00
|
|
|
"{edi}" (new_cfa)
|
2016-09-01 00:47:07 +08:00
|
|
|
:/*"eax",*/"ebx", "ecx", "edx", "esi", "edi",/*"ebp", "esp",*/
|
2016-09-02 00:36:40 +08:00
|
|
|
"mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
|
2016-07-16 09:22:41 +08:00
|
|
|
"xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
|
2016-09-02 00:36:40 +08:00
|
|
|
"cc", "dirflag", "fpsr", "flags", "memory"
|
2016-07-16 09:22:41 +08:00
|
|
|
: "volatile");
|
|
|
|
ret
|
|
|
|
}
|