From 75ca6f66e1b4f66f68a5558aa8c4d6a68291c8a2 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sat, 16 Jul 2016 13:17:22 -0700 Subject: [PATCH] Move x86 trampoline to naked function Close #19 --- src/arch/x86.rs | 37 ++++++++++++++----------------------- src/lib.rs | 1 + 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/arch/x86.rs b/src/arch/x86.rs index 686751d..6fe27cc 100644 --- a/src/arch/x86.rs +++ b/src/arch/x86.rs @@ -29,35 +29,26 @@ 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" - ); + #[naked] + unsafe extern "C" fn trampoline() -> ! { + asm!( + r#" + # Pop function. + popl %ebx + # Push argument. + pushl %eax + # Call it. + call *%ebx + "# ::: "memory" : "volatile"); + ::core::intrinsics::unreachable() + } 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 } diff --git a/src/lib.rs b/src/lib.rs index e986317..0631ae0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ // Copyright (c) edef // 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.