2014-01-25 01:37:51 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-02-03 20:15:43 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.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.
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
use std::uint;
|
|
|
|
use std::cast::{transmute, transmute_mut_unsafe,
|
|
|
|
transmute_region, transmute_mut_region};
|
2013-11-13 04:21:38 -06:00
|
|
|
use stack::Stack;
|
2013-12-12 20:01:59 -06:00
|
|
|
use std::unstable::stack;
|
2014-02-16 02:04:33 -06:00
|
|
|
use std::raw;
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2013-07-12 16:43:57 -05:00
|
|
|
// FIXME #7761: Registers is boxed so that it is 16-byte aligned, for storing
|
2013-02-03 20:15:43 -06:00
|
|
|
// SSE regs. It would be marginally better not to do this. In C++ we
|
|
|
|
// use an attribute on a struct.
|
2013-07-12 16:43:57 -05:00
|
|
|
// FIXME #7761: It would be nice to define regs as `~Option<Registers>` since
|
2013-03-12 02:48:41 -05:00
|
|
|
// the registers are sometimes empty, but the discriminant would
|
|
|
|
// then misalign the regs again.
|
|
|
|
pub struct Context {
|
|
|
|
/// Hold the registers while the task or scheduler is suspended
|
2013-10-19 19:33:09 -05:00
|
|
|
priv regs: ~Registers,
|
2013-10-17 03:40:33 -05:00
|
|
|
/// Lower bound and upper bound for the stack
|
2013-10-19 19:33:09 -05:00
|
|
|
priv stack_bounds: Option<(uint, uint)>,
|
2013-03-12 02:48:41 -05:00
|
|
|
}
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2014-02-10 18:13:50 -06:00
|
|
|
pub type InitFn = extern "C" fn(uint, *(), *()) -> !;
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Context {
|
|
|
|
pub fn empty() -> Context {
|
2013-03-12 02:48:41 -05:00
|
|
|
Context {
|
2013-10-17 03:40:33 -05:00
|
|
|
regs: new_regs(),
|
|
|
|
stack_bounds: None,
|
2013-03-12 02:48:41 -05:00
|
|
|
}
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
|
2013-11-18 15:25:09 -06:00
|
|
|
/// Create a new context that will resume execution by running proc()
|
2014-02-10 18:13:50 -06:00
|
|
|
///
|
|
|
|
/// The `init` function will be run with `arg` and the `start` procedure
|
|
|
|
/// split up into code and env pointers. It is required that the `init`
|
|
|
|
/// function never return.
|
|
|
|
///
|
|
|
|
/// FIXME: this is basically an awful the interface. The main reason for
|
|
|
|
/// this is to reduce the number of allocations made when a green
|
|
|
|
/// task is spawned as much as possible
|
|
|
|
pub fn new(init: InitFn, arg: uint, start: proc(),
|
|
|
|
stack: &mut Stack) -> Context {
|
2013-02-03 20:15:43 -06:00
|
|
|
|
|
|
|
let sp: *uint = stack.end();
|
|
|
|
let sp: *mut uint = unsafe { transmute_mut_unsafe(sp) };
|
|
|
|
// Save and then immediately load the current context,
|
|
|
|
// which we will then modify to call the given function when restored
|
|
|
|
let mut regs = new_regs();
|
|
|
|
unsafe {
|
2013-12-12 20:01:59 -06:00
|
|
|
rust_swap_registers(transmute_mut_region(&mut *regs),
|
|
|
|
transmute_region(&*regs));
|
2013-02-03 20:15:43 -06:00
|
|
|
};
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
initialize_call_frame(&mut *regs,
|
2014-02-10 18:13:50 -06:00
|
|
|
init,
|
|
|
|
arg,
|
|
|
|
unsafe { transmute(start) },
|
2013-12-12 20:01:59 -06:00
|
|
|
sp);
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2013-10-17 03:40:33 -05:00
|
|
|
// Scheduler tasks don't have a stack in the "we allocated it" sense,
|
|
|
|
// but rather they run on pthreads stacks. We have complete control over
|
|
|
|
// them in terms of the code running on them (and hopefully they don't
|
|
|
|
// overflow). Additionally, their coroutine stacks are listed as being
|
|
|
|
// zero-length, so that's how we detect what's what here.
|
|
|
|
let stack_base: *uint = stack.start();
|
|
|
|
let bounds = if sp as uint == stack_base as uint {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((stack_base as uint, sp as uint))
|
|
|
|
};
|
2013-03-12 02:48:41 -05:00
|
|
|
return Context {
|
2013-10-17 03:40:33 -05:00
|
|
|
regs: regs,
|
|
|
|
stack_bounds: bounds,
|
2013-03-12 02:48:41 -05:00
|
|
|
}
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
|
2013-03-12 02:48:41 -05:00
|
|
|
/* Switch contexts
|
|
|
|
|
|
|
|
Suspend the current execution context and resume another by
|
|
|
|
saving the registers values of the executing thread to a Context
|
|
|
|
then loading the registers from a previously saved Context.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn swap(out_context: &mut Context, in_context: &Context) {
|
2013-07-19 16:25:05 -05:00
|
|
|
rtdebug!("swapping contexts");
|
2013-02-03 20:15:43 -06:00
|
|
|
let out_regs: &mut Registers = match out_context {
|
2013-11-28 14:22:53 -06:00
|
|
|
&Context { regs: ~ref mut r, .. } => r
|
2013-02-03 20:15:43 -06:00
|
|
|
};
|
|
|
|
let in_regs: &Registers = match in_context {
|
2013-11-28 14:22:53 -06:00
|
|
|
&Context { regs: ~ref r, .. } => r
|
2013-02-03 20:15:43 -06:00
|
|
|
};
|
2013-10-17 03:40:33 -05:00
|
|
|
|
|
|
|
rtdebug!("noting the stack limit and doing raw swap");
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// Right before we switch to the new context, set the new context's
|
|
|
|
// stack limit in the OS-specified TLS slot. This also means that
|
|
|
|
// we cannot call any more rust functions after record_stack_bounds
|
|
|
|
// returns because they would all likely fail due to the limit being
|
2013-11-12 08:21:17 -06:00
|
|
|
// invalid for the current task. Lucky for us `rust_swap_registers`
|
|
|
|
// is a C function so we don't have to worry about that!
|
2013-10-17 03:40:33 -05:00
|
|
|
match in_context.stack_bounds {
|
2013-12-12 20:01:59 -06:00
|
|
|
Some((lo, hi)) => stack::record_stack_bounds(lo, hi),
|
2013-10-17 03:40:33 -05:00
|
|
|
// If we're going back to one of the original contexts or
|
|
|
|
// something that's possibly not a "normal task", then reset
|
|
|
|
// the stack limit to 0 to make morestack never fail
|
2014-01-25 01:37:51 -06:00
|
|
|
None => stack::record_stack_bounds(0, uint::MAX),
|
2013-10-17 03:40:33 -05:00
|
|
|
}
|
2013-11-12 08:21:17 -06:00
|
|
|
rust_swap_registers(out_regs, in_regs)
|
2013-10-17 03:40:33 -05:00
|
|
|
}
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 18:13:50 -06:00
|
|
|
#[link(name = "context_switch", kind = "static")]
|
2013-02-03 20:15:43 -06:00
|
|
|
extern {
|
2013-11-12 08:21:17 -06:00
|
|
|
fn rust_swap_registers(out_regs: *mut Registers, in_regs: *Registers);
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
|
2013-10-17 03:40:33 -05:00
|
|
|
// Register contexts used in various architectures
|
|
|
|
//
|
|
|
|
// These structures all represent a context of one task throughout its
|
|
|
|
// execution. Each struct is a representation of the architecture's register
|
|
|
|
// set. When swapping between tasks, these register sets are used to save off
|
|
|
|
// the current registers into one struct, and load them all from another.
|
|
|
|
//
|
|
|
|
// Note that this is only used for context switching, which means that some of
|
|
|
|
// the registers may go unused. For example, for architectures with
|
|
|
|
// callee/caller saved registers, the context will only reflect the callee-saved
|
|
|
|
// registers. This is because the caller saved registers are already stored
|
|
|
|
// elsewhere on the stack (if it was necessary anyway).
|
|
|
|
//
|
|
|
|
// Additionally, there may be fields on various architectures which are unused
|
|
|
|
// entirely because they only reflect what is theoretically possible for a
|
|
|
|
// "complete register set" to show, but user-space cannot alter these registers.
|
|
|
|
// An example of this would be the segment selectors for x86.
|
|
|
|
//
|
|
|
|
// These structures/functions are roughly in-sync with the source files inside
|
|
|
|
// of src/rt/arch/$arch. The only currently used function from those folders is
|
2013-11-12 08:21:17 -06:00
|
|
|
// the `rust_swap_registers` function, but that's only because for now segmented
|
2013-10-17 03:40:33 -05:00
|
|
|
// stacks are disabled.
|
|
|
|
|
2013-03-12 01:17:40 -05:00
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
struct Registers {
|
|
|
|
eax: u32, ebx: u32, ecx: u32, edx: u32,
|
|
|
|
ebp: u32, esi: u32, edi: u32, esp: u32,
|
|
|
|
cs: u16, ds: u16, ss: u16, es: u16, fs: u16, gs: u16,
|
|
|
|
eflags: u32, eip: u32
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
fn new_regs() -> ~Registers {
|
|
|
|
~Registers {
|
|
|
|
eax: 0, ebx: 0, ecx: 0, edx: 0,
|
|
|
|
ebp: 0, esi: 0, edi: 0, esp: 0,
|
|
|
|
cs: 0, ds: 0, ss: 0, es: 0, fs: 0, gs: 0,
|
|
|
|
eflags: 0, eip: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86")]
|
2014-02-10 18:13:50 -06:00
|
|
|
fn initialize_call_frame(regs: &mut Registers, fptr: InitFn, arg: uint,
|
|
|
|
procedure: raw::Procedure, sp: *mut uint) {
|
2013-03-12 01:17:40 -05:00
|
|
|
|
2014-02-10 18:13:50 -06:00
|
|
|
// x86 has interesting stack alignment requirements, so do some alignment
|
|
|
|
// plus some offsetting to figure out what the actual stack should be.
|
2013-03-12 01:17:40 -05:00
|
|
|
let sp = align_down(sp);
|
2013-03-12 02:48:41 -05:00
|
|
|
let sp = mut_offset(sp, -4);
|
2013-03-12 01:17:40 -05:00
|
|
|
|
2014-02-10 18:13:50 -06:00
|
|
|
unsafe { *mut_offset(sp, 2) = procedure.env as uint };
|
|
|
|
unsafe { *mut_offset(sp, 1) = procedure.code as uint };
|
|
|
|
unsafe { *mut_offset(sp, 0) = arg as uint };
|
2013-03-12 01:17:40 -05:00
|
|
|
let sp = mut_offset(sp, -1);
|
2013-04-27 04:07:32 -05:00
|
|
|
unsafe { *sp = 0 }; // The final return address
|
2013-03-12 01:17:40 -05:00
|
|
|
|
|
|
|
regs.esp = sp as u32;
|
|
|
|
regs.eip = fptr as u32;
|
|
|
|
|
|
|
|
// Last base pointer on the stack is 0
|
|
|
|
regs.ebp = 0;
|
|
|
|
}
|
|
|
|
|
2013-10-17 03:40:33 -05:00
|
|
|
// windows requires saving more registers (both general and XMM), so the windows
|
|
|
|
// register context must be larger.
|
2013-08-26 08:01:55 -05:00
|
|
|
#[cfg(windows, target_arch = "x86_64")]
|
|
|
|
type Registers = [uint, ..34];
|
|
|
|
#[cfg(not(windows), target_arch = "x86_64")]
|
2013-03-22 20:52:04 -05:00
|
|
|
type Registers = [uint, ..22];
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2013-08-26 08:01:55 -05:00
|
|
|
#[cfg(windows, target_arch = "x86_64")]
|
|
|
|
fn new_regs() -> ~Registers { ~([0, .. 34]) }
|
|
|
|
#[cfg(not(windows), target_arch = "x86_64")]
|
2013-03-27 15:53:03 -05:00
|
|
|
fn new_regs() -> ~Registers { ~([0, .. 22]) }
|
2013-02-03 20:15:43 -06:00
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2014-02-10 18:13:50 -06:00
|
|
|
fn initialize_call_frame(regs: &mut Registers, fptr: InitFn, arg: uint,
|
|
|
|
procedure: raw::Procedure, sp: *mut uint) {
|
|
|
|
extern { fn rust_bootstrap_green_task(); }
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2013-10-17 03:40:33 -05:00
|
|
|
// Redefinitions from rt/arch/x86_64/regs.h
|
2013-03-22 16:00:15 -05:00
|
|
|
static RUSTRT_RSP: uint = 1;
|
|
|
|
static RUSTRT_IP: uint = 8;
|
|
|
|
static RUSTRT_RBP: uint = 2;
|
2014-02-10 18:13:50 -06:00
|
|
|
static RUSTRT_R12: uint = 4;
|
|
|
|
static RUSTRT_R13: uint = 5;
|
|
|
|
static RUSTRT_R14: uint = 6;
|
|
|
|
static RUSTRT_R15: uint = 7;
|
2013-02-03 20:15:43 -06:00
|
|
|
|
|
|
|
let sp = align_down(sp);
|
|
|
|
let sp = mut_offset(sp, -1);
|
|
|
|
|
|
|
|
// The final return address. 0 indicates the bottom of the stack
|
|
|
|
unsafe { *sp = 0; }
|
|
|
|
|
|
|
|
rtdebug!("creating call frame");
|
2014-02-10 18:13:50 -06:00
|
|
|
rtdebug!("fptr {:#x}", fptr as uint);
|
|
|
|
rtdebug!("arg {:#x}", arg);
|
2013-10-17 03:40:33 -05:00
|
|
|
rtdebug!("sp {}", sp);
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2014-02-10 18:13:50 -06:00
|
|
|
// These registers are frobbed by rust_bootstrap_green_task into the right
|
|
|
|
// location so we can invoke the "real init function", `fptr`.
|
|
|
|
regs[RUSTRT_R12] = arg as uint;
|
|
|
|
regs[RUSTRT_R13] = procedure.code as uint;
|
|
|
|
regs[RUSTRT_R14] = procedure.env as uint;
|
|
|
|
regs[RUSTRT_R15] = fptr as uint;
|
|
|
|
|
|
|
|
// These registers are picked up by the regulard context switch paths. These
|
|
|
|
// will put us in "mostly the right context" except for frobbing all the
|
|
|
|
// arguments to the right place. We have the small trampoline code inside of
|
|
|
|
// rust_bootstrap_green_task to do that.
|
2013-02-03 20:15:43 -06:00
|
|
|
regs[RUSTRT_RSP] = sp as uint;
|
2014-02-10 18:13:50 -06:00
|
|
|
regs[RUSTRT_IP] = rust_bootstrap_green_task as uint;
|
2013-02-03 20:15:43 -06:00
|
|
|
|
|
|
|
// Last base pointer on the stack should be 0
|
|
|
|
regs[RUSTRT_RBP] = 0;
|
|
|
|
}
|
|
|
|
|
2013-03-12 01:17:40 -05:00
|
|
|
#[cfg(target_arch = "arm")]
|
2013-03-22 20:52:04 -05:00
|
|
|
type Registers = [uint, ..32];
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2013-03-12 01:17:40 -05:00
|
|
|
#[cfg(target_arch = "arm")]
|
2013-03-29 01:46:13 -05:00
|
|
|
fn new_regs() -> ~Registers { ~([0, .. 32]) }
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2013-03-12 01:17:40 -05:00
|
|
|
#[cfg(target_arch = "arm")]
|
2014-02-10 18:13:50 -06:00
|
|
|
fn initialize_call_frame(regs: &mut Registers, fptr: InitFn, arg: uint,
|
|
|
|
procedure: raw::Procedure, sp: *mut uint) {
|
|
|
|
extern { fn rust_bootstrap_green_task(); }
|
|
|
|
|
2013-05-21 13:14:22 -05:00
|
|
|
let sp = align_down(sp);
|
|
|
|
// sp of arm eabi is 8-byte aligned
|
|
|
|
let sp = mut_offset(sp, -2);
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2013-03-12 01:17:40 -05:00
|
|
|
// The final return address. 0 indicates the bottom of the stack
|
|
|
|
unsafe { *sp = 0; }
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2014-02-10 18:13:50 -06:00
|
|
|
// ARM uses the same technique as x86_64 to have a landing pad for the start
|
|
|
|
// of all new green tasks. Neither r1/r2 are saved on a context switch, so
|
|
|
|
// the shim will copy r3/r4 into r1/r2 and then execute the function in r5
|
|
|
|
regs[0] = arg as uint; // r0
|
|
|
|
regs[3] = procedure.code as uint; // r3
|
|
|
|
regs[4] = procedure.env as uint; // r4
|
|
|
|
regs[5] = fptr as uint; // r5
|
|
|
|
regs[13] = sp as uint; // #52 sp, r13
|
|
|
|
regs[14] = rust_bootstrap_green_task as uint; // #56 pc, r14 --> lr
|
2013-03-12 01:17:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "mips")]
|
2013-03-22 20:52:04 -05:00
|
|
|
type Registers = [uint, ..32];
|
2013-03-12 01:17:40 -05:00
|
|
|
|
|
|
|
#[cfg(target_arch = "mips")]
|
2013-03-29 01:46:13 -05:00
|
|
|
fn new_regs() -> ~Registers { ~([0, .. 32]) }
|
2013-03-12 01:17:40 -05:00
|
|
|
|
|
|
|
#[cfg(target_arch = "mips")]
|
2014-02-10 18:13:50 -06:00
|
|
|
fn initialize_call_frame(regs: &mut Registers, fptr: InitFn, arg: uint,
|
|
|
|
procedure: raw::Procedure, sp: *mut uint) {
|
2013-05-20 22:58:08 -05:00
|
|
|
let sp = align_down(sp);
|
|
|
|
// sp of mips o32 is 8-byte aligned
|
|
|
|
let sp = mut_offset(sp, -2);
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2013-03-12 01:17:40 -05:00
|
|
|
// The final return address. 0 indicates the bottom of the stack
|
|
|
|
unsafe { *sp = 0; }
|
2013-02-03 20:15:43 -06:00
|
|
|
|
2013-03-12 01:17:40 -05:00
|
|
|
regs[4] = arg as uint;
|
|
|
|
regs[29] = sp as uint;
|
2013-03-19 00:34:12 -05:00
|
|
|
regs[25] = fptr as uint;
|
2013-03-12 01:17:40 -05:00
|
|
|
regs[31] = fptr as uint;
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn align_down(sp: *mut uint) -> *mut uint {
|
|
|
|
unsafe {
|
2013-04-27 04:07:32 -05:00
|
|
|
let sp: uint = transmute(sp);
|
2013-02-03 20:15:43 -06:00
|
|
|
let sp = sp & !(16 - 1);
|
|
|
|
transmute::<uint, *mut uint>(sp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 16:43:57 -05:00
|
|
|
// ptr::mut_offset is positive ints only
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
|
2013-12-12 20:01:59 -06:00
|
|
|
use std::mem::size_of;
|
2013-04-09 00:31:23 -05:00
|
|
|
(ptr as int + count * (size_of::<T>() as int)) as *mut T
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|