Skip to content

Commit

Permalink
Move x86 trampoline to naked function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Jul 16, 2016
1 parent c3ba332 commit 4c6df41
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
39 changes: 15 additions & 24 deletions src/arch/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
//! `swap` for the initial call, and so we use a trampoline.
use stack::Stack;

#[naked]
unsafe extern "C" fn trampoline() -> ! {
asm!(
r#"
# Pop function.
popl %ebx
# Push argument.
pushl %eax
# Call it.
call *%ebx
"#);
::core::intrinsics::unreachable()
}

#[derive(Debug)]
pub struct StackPointer(*mut usize);

Expand All @@ -29,35 +43,12 @@ impl StackPointer {
}

pub unsafe fn init(stack: &Stack, f: unsafe extern "C" fn(usize) -> !) -> StackPointer {
let g: usize;
asm!(
r#"
# Push address of the trampoline.
call 1f
# Pop function.
popl %ebx
# Push argument.
pushl %eax
# Call it.
call *%ebx
1:
# Pop address of the trampoline.
popl %eax
"#
: "={eax}" (g)
:
: "memory"
: "volatile"
);

let mut sp = StackPointer::new(stack);
sp.push(0); // alignment
sp.push(0); // alignment
sp.push(0); // alignment
sp.push(f as usize); // function
sp.push(g as usize); // trampoline
sp.push(trampoline as usize);
sp
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Nathan Zadoks <nathan@nathan7.eu>
// See the LICENSE file included in this distribution.
#![feature(asm)]
#![cfg_attr(target_arch = "x86", feature(naked_functions, core_intrinsics))]
#![no_std]

//! libfringe is a low-level green threading library.
Expand Down

0 comments on commit 4c6df41

Please sign in to comment.