|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use super::stack::StackSegment; |
| 12 | +use libc::c_void; |
| 13 | +use cast::{transmute, transmute_mut_unsafe, |
| 14 | + transmute_region, transmute_mut_region}; |
| 15 | + |
| 16 | +// XXX: Registers is boxed so that it is 16-byte aligned, for storing |
| 17 | +// SSE regs. It would be marginally better not to do this. In C++ we |
| 18 | +// use an attribute on a struct. |
| 19 | +pub struct Context(~Registers); |
| 20 | + |
| 21 | +pub impl Context { |
| 22 | + static fn empty() -> Context { |
| 23 | + Context(new_regs()) |
| 24 | + } |
| 25 | + |
| 26 | + /// Create a new context that will resume execution by running ~fn() |
| 27 | + /// # Safety Note |
| 28 | + /// The `start` closure must remain valid for the life of the Task |
| 29 | + static fn new(start: &~fn(), stack: &mut StackSegment) -> Context { |
| 30 | + |
| 31 | + // The C-ABI function that is the task entry point |
| 32 | + extern fn task_start_wrapper(f: &~fn()) { (*f)() } |
| 33 | + |
| 34 | + let fp: *c_void = task_start_wrapper as *c_void; |
| 35 | + let argp: *c_void = unsafe { transmute::<&~fn(), *c_void>(&*start) }; |
| 36 | + let sp: *uint = stack.end(); |
| 37 | + let sp: *mut uint = unsafe { transmute_mut_unsafe(sp) }; |
| 38 | + |
| 39 | + // Save and then immediately load the current context, |
| 40 | + // which we will then modify to call the given function when restored |
| 41 | + let mut regs = new_regs(); |
| 42 | + unsafe { |
| 43 | + swap_registers(transmute_mut_region(&mut *regs), |
| 44 | + transmute_region(&*regs)) |
| 45 | + }; |
| 46 | + |
| 47 | + initialize_call_frame(&mut *regs, fp, argp, sp); |
| 48 | + |
| 49 | + return Context(regs); |
| 50 | + } |
| 51 | + |
| 52 | + static fn swap(out_context: &mut Context, in_context: &Context) { |
| 53 | + let out_regs: &mut Registers = match out_context { |
| 54 | + &Context(~ref mut r) => r |
| 55 | + }; |
| 56 | + let in_regs: &Registers = match in_context { |
| 57 | + &Context(~ref r) => r |
| 58 | + }; |
| 59 | + |
| 60 | + unsafe { swap_registers(out_regs, in_regs) }; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +extern { |
| 65 | + fn swap_registers(out_regs: *mut Registers, in_regs: *Registers); |
| 66 | +} |
| 67 | + |
| 68 | +// Definitions of these registers are in rt/arch/x86_64/regs.h |
| 69 | +#[cfg(target_arch = "x86_64")] |
| 70 | +type Registers = [uint * 22]; |
| 71 | + |
| 72 | +#[cfg(target_arch = "x86_64")] |
| 73 | +fn new_regs() -> ~Registers { ~[0, .. 22] } |
| 74 | + |
| 75 | +#[cfg(target_arch = "x86_64")] |
| 76 | +fn initialize_call_frame(regs: &mut Registers, |
| 77 | + fptr: *c_void, arg: *c_void, sp: *mut uint) { |
| 78 | + |
| 79 | + // Redefinitions from regs.h |
| 80 | + const RUSTRT_ARG0: uint = 3; |
| 81 | + const RUSTRT_RSP: uint = 1; |
| 82 | + const RUSTRT_IP: uint = 8; |
| 83 | + const RUSTRT_RBP: uint = 2; |
| 84 | + |
| 85 | + let sp = align_down(sp); |
| 86 | + let sp = mut_offset(sp, -1); |
| 87 | + |
| 88 | + // The final return address. 0 indicates the bottom of the stack |
| 89 | + unsafe { *sp = 0; } |
| 90 | + |
| 91 | + rtdebug!("creating call frame"); |
| 92 | + rtdebug!("fptr %x", fptr as uint); |
| 93 | + rtdebug!("arg %x", arg as uint); |
| 94 | + rtdebug!("sp %x", sp as uint); |
| 95 | + |
| 96 | + regs[RUSTRT_ARG0] = arg as uint; |
| 97 | + regs[RUSTRT_RSP] = sp as uint; |
| 98 | + regs[RUSTRT_IP] = fptr as uint; |
| 99 | + |
| 100 | + // Last base pointer on the stack should be 0 |
| 101 | + regs[RUSTRT_RBP] = 0; |
| 102 | +} |
| 103 | + |
| 104 | +#[cfg(target_arch = "x86")] |
| 105 | +struct Registers { |
| 106 | + eax: u32, ebx: u32, ecx: u32, edx: u32, |
| 107 | + ebp: u32, esi: u32, edi: u32, esp: u32, |
| 108 | + cs: u16, ds: u16, ss: u16, es: u16, fs: u16, gs: u16, |
| 109 | + eflags: u32, eip: u32 |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(target_arch = "x86")] |
| 113 | +fn new_regs() -> ~Registers { |
| 114 | + ~Registers { |
| 115 | + eax: 0, ebx: 0, ecx: 0, edx: 0, |
| 116 | + ebp: 0, esi: 0, edi: 0, esp: 0, |
| 117 | + cs: 0, ds: 0, ss: 0, es: 0, fs: 0, gs: 0, |
| 118 | + eflags: 0, eip: 0 |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +#[cfg(target_arch = "x86")] |
| 123 | +fn initialize_call_frame(regs: &mut Registers, |
| 124 | + fptr: *c_void, arg: *c_void, sp: *mut uint) { |
| 125 | + |
| 126 | + let sp = align_down(sp); |
| 127 | + let sp = mut_offset(sp, -4); // XXX: -4 words? Needs this be done at all? |
| 128 | + |
| 129 | + unsafe { *sp = arg as uint; } |
| 130 | + let sp = mut_offset(sp, -1); |
| 131 | + unsafe { *sp = 0; } // The final return address |
| 132 | + |
| 133 | + regs.esp = sp as u32; |
| 134 | + regs.eip = fptr as u32; |
| 135 | + |
| 136 | + // Last base pointer on the stack is 0 |
| 137 | + regs.ebp = 0; |
| 138 | +} |
| 139 | + |
| 140 | +fn align_down(sp: *mut uint) -> *mut uint { |
| 141 | + unsafe { |
| 142 | + let sp = transmute::<*mut uint, uint>(sp); |
| 143 | + let sp = sp & !(16 - 1); |
| 144 | + transmute::<uint, *mut uint>(sp) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// XXX: ptr::offset is positive ints only |
| 149 | +#[inline(always)] |
| 150 | +pub pure fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T { |
| 151 | + use core::sys::size_of; |
| 152 | + unsafe { |
| 153 | + (ptr as int + count * (size_of::<T>() as int)) as *mut T |
| 154 | + } |
| 155 | +} |
| 156 | + |
0 commit comments