2012-06-03 01:14:25 -05:00
|
|
|
/**
|
|
|
|
The rust task is a cooperatively-scheduled green thread that executes
|
|
|
|
Rust code on a segmented stack.
|
|
|
|
|
|
|
|
This class has too many responsibilities:
|
|
|
|
|
|
|
|
* Working with the scheduler loop to signal and respond to state changes,
|
|
|
|
and dealing with all the thread synchronization issues involved
|
|
|
|
|
|
|
|
* Managing the dynamically resizing list of Rust stack segments
|
|
|
|
|
|
|
|
* Switching between running Rust code on the Rust segmented stack and
|
2012-06-27 15:14:04 -05:00
|
|
|
foreign C code on large stacks owned by the scheduler
|
2012-06-03 01:14:25 -05:00
|
|
|
|
|
|
|
The lifetime of a rust_task object closely mirrors that of a running Rust
|
|
|
|
task object, but they are not identical. In particular, the rust_task is an
|
|
|
|
atomically reference counted object that might be accessed from arbitrary
|
|
|
|
threads at any time. This may keep the task from being destroyed even after
|
|
|
|
the task is dead from a Rust task lifecycle perspective.
|
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
FIXME (#2696): The task and the scheduler have an over-complicated,
|
|
|
|
undocumented protocol for shutting down the task, hopefully without
|
|
|
|
races. It would be easier to reason about if other runtime objects could
|
|
|
|
not access the task from arbitrary threads, and didn't need to be
|
|
|
|
atomically refcounted.
|
2012-06-03 01:14:25 -05:00
|
|
|
*/
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
#ifndef RUST_TASK_H
|
|
|
|
#define RUST_TASK_H
|
2010-07-28 18:24:50 -05:00
|
|
|
|
2011-09-20 17:35:14 -05:00
|
|
|
#include <map>
|
|
|
|
|
2012-04-03 17:33:25 -05:00
|
|
|
#include "rust_globals.h"
|
2010-07-28 18:24:50 -05:00
|
|
|
#include "util/array_list.h"
|
2011-05-31 19:44:54 -05:00
|
|
|
#include "context.h"
|
2011-09-23 13:42:20 -05:00
|
|
|
#include "rust_debug.h"
|
|
|
|
#include "rust_kernel.h"
|
2012-02-01 20:52:08 -06:00
|
|
|
#include "boxed_region.h"
|
2012-02-08 17:28:25 -06:00
|
|
|
#include "rust_stack.h"
|
2012-02-15 00:23:16 -06:00
|
|
|
#include "rust_port_selector.h"
|
2012-04-02 22:18:01 -05:00
|
|
|
#include "rust_type.h"
|
|
|
|
#include "rust_sched_loop.h"
|
2011-05-31 19:44:54 -05:00
|
|
|
|
2012-03-21 16:00:37 -05:00
|
|
|
// The amount of extra space at the end of each stack segment, available
|
|
|
|
// to the rt, compiler and dynamic linker for running small functions
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#1509): We want this to be 128 but need to slim the red zone calls
|
|
|
|
// down, disable lazy symbol relocation, and other things we haven't
|
|
|
|
// discovered yet
|
2012-03-21 16:00:37 -05:00
|
|
|
#define RZ_LINUX_32 (1024*2)
|
|
|
|
#define RZ_LINUX_64 (1024*2)
|
|
|
|
#define RZ_MAC_32 (1024*20)
|
|
|
|
#define RZ_MAC_64 (1024*20)
|
|
|
|
#define RZ_WIN_32 (1024*20)
|
|
|
|
#define RZ_BSD_32 (1024*20)
|
|
|
|
#define RZ_BSD_64 (1024*20)
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
#ifdef __i386__
|
|
|
|
#define RED_ZONE_SIZE RZ_LINUX_32
|
|
|
|
#endif
|
|
|
|
#ifdef __x86_64__
|
|
|
|
#define RED_ZONE_SIZE RZ_LINUX_64
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#ifdef __i386__
|
|
|
|
#define RED_ZONE_SIZE RZ_MAC_32
|
|
|
|
#endif
|
|
|
|
#ifdef __x86_64__
|
|
|
|
#define RED_ZONE_SIZE RZ_MAC_64
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef __WIN32__
|
|
|
|
#ifdef __i386__
|
|
|
|
#define RED_ZONE_SIZE RZ_WIN_32
|
|
|
|
#endif
|
|
|
|
#ifdef __x86_64__
|
|
|
|
#define RED_ZONE_SIZE RZ_WIN_64
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#ifdef __i386__
|
|
|
|
#define RED_ZONE_SIZE RZ_BSD_32
|
|
|
|
#endif
|
|
|
|
#ifdef __x86_64__
|
|
|
|
#define RED_ZONE_SIZE RZ_BSD_64
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-09-19 12:34:27 -05:00
|
|
|
struct rust_box;
|
|
|
|
|
2011-06-27 21:15:03 -05:00
|
|
|
struct frame_glue_fns {
|
|
|
|
uintptr_t mark_glue_off;
|
|
|
|
uintptr_t drop_glue_off;
|
|
|
|
uintptr_t reloc_glue_off;
|
|
|
|
};
|
|
|
|
|
2011-08-16 18:39:47 -05:00
|
|
|
// std::lib::task::task_result
|
2011-11-11 17:27:18 -06:00
|
|
|
typedef unsigned long task_result;
|
|
|
|
#define tr_success 0
|
|
|
|
#define tr_failure 1
|
2011-08-16 18:39:47 -05:00
|
|
|
|
2012-02-09 03:13:32 -06:00
|
|
|
struct spawn_args;
|
2012-02-14 02:43:45 -06:00
|
|
|
struct cleanup_args;
|
2012-02-23 01:50:27 -06:00
|
|
|
struct reset_args;
|
2012-03-21 15:17:24 -05:00
|
|
|
struct new_stack_args;
|
2012-02-09 03:13:32 -06:00
|
|
|
|
2011-08-16 18:39:47 -05:00
|
|
|
// std::lib::task::task_notification
|
|
|
|
//
|
|
|
|
// since it's currently a unary tag, we only add the fields.
|
|
|
|
struct task_notification {
|
|
|
|
rust_task_id id;
|
|
|
|
task_result result; // task_result
|
|
|
|
};
|
|
|
|
|
2012-06-01 01:25:13 -05:00
|
|
|
extern "C" void
|
|
|
|
rust_task_fail(rust_task *task,
|
|
|
|
char const *expr,
|
|
|
|
char const *file,
|
|
|
|
size_t line);
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
struct
|
2012-07-10 15:56:00 -05:00
|
|
|
rust_task : public kernel_owned<rust_task>
|
2010-07-19 16:05:18 -05:00
|
|
|
{
|
2011-08-05 17:16:48 -05:00
|
|
|
RUST_ATOMIC_REFCOUNT();
|
2011-07-27 16:51:25 -05:00
|
|
|
|
2012-02-08 19:46:12 -06:00
|
|
|
rust_task_id id;
|
|
|
|
bool notify_enabled;
|
2012-03-15 01:08:47 -05:00
|
|
|
rust_port_id notify_port;
|
2012-02-08 19:46:12 -06:00
|
|
|
|
2011-09-06 16:03:20 -05:00
|
|
|
context ctx;
|
2010-07-19 16:05:18 -05:00
|
|
|
stk_seg *stk;
|
|
|
|
uintptr_t runtime_sp; // Runtime sp while task running.
|
2012-02-03 17:18:58 -06:00
|
|
|
rust_scheduler *sched;
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop *sched_loop;
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
// Fields known only to the runtime.
|
2011-06-24 18:50:06 -05:00
|
|
|
rust_kernel *kernel;
|
2010-08-08 21:24:35 -05:00
|
|
|
const char *const name;
|
2010-09-10 03:21:29 -05:00
|
|
|
int32_t list_index;
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
// Rendezvous pointer for receiving data when blocked on a port. If we're
|
|
|
|
// trying to read data and no data is available on any incoming channel,
|
|
|
|
// we block on the port, and yield control to the scheduler. Since, we
|
2010-07-28 14:36:59 -05:00
|
|
|
// were not able to read anything, we remember the location where the
|
2010-07-19 16:05:18 -05:00
|
|
|
// result should go in the rendezvous_ptr, and let the sender write to
|
|
|
|
// that location before waking us up.
|
|
|
|
uintptr_t* rendezvous_ptr;
|
|
|
|
|
2011-06-27 21:15:03 -05:00
|
|
|
memory_region local_region;
|
2012-02-01 20:52:08 -06:00
|
|
|
boxed_region boxed;
|
2011-06-27 21:15:03 -05:00
|
|
|
|
2012-01-09 15:47:37 -06:00
|
|
|
// Indicates that fail() has been called and we are cleaning up.
|
|
|
|
// We use this to suppress the "killed" flag during calls to yield.
|
|
|
|
bool unwinding;
|
|
|
|
|
2011-07-23 21:03:02 -05:00
|
|
|
bool propagate_failure;
|
2011-07-14 21:39:53 -05:00
|
|
|
|
2011-09-23 20:30:22 -05:00
|
|
|
uint32_t cc_counter;
|
2011-09-20 17:35:14 -05:00
|
|
|
|
2011-09-23 13:42:20 -05:00
|
|
|
debug::task_debug_info debug;
|
|
|
|
|
2012-01-11 14:37:09 -06:00
|
|
|
// The amount of stack we're using, excluding red zones
|
|
|
|
size_t total_stack_sz;
|
|
|
|
|
2012-06-27 12:07:00 -05:00
|
|
|
// Used by rust task management routines in libcore/task.rs.
|
|
|
|
void *task_local_data;
|
|
|
|
void (*task_local_data_cleanup)(void *data);
|
|
|
|
|
2012-02-07 01:38:22 -06:00
|
|
|
private:
|
2012-02-09 03:13:32 -06:00
|
|
|
|
2012-03-03 01:40:27 -06:00
|
|
|
// Protects state, cond, cond_name
|
2012-07-12 18:52:13 -05:00
|
|
|
// Protects the killed flag, disallow_kill flag, reentered_rust_stack
|
|
|
|
lock_and_signal lifecycle_lock;
|
2012-03-17 20:12:15 -05:00
|
|
|
rust_task_state state;
|
2012-03-03 01:40:27 -06:00
|
|
|
rust_cond *cond;
|
|
|
|
const char *cond_name;
|
2012-03-02 23:22:42 -06:00
|
|
|
|
2012-07-02 19:42:58 -05:00
|
|
|
bool event_reject;
|
|
|
|
rust_cond event_cond;
|
|
|
|
void *event;
|
|
|
|
|
2012-02-14 02:43:45 -06:00
|
|
|
// Indicates that the task was killed and needs to unwind
|
|
|
|
bool killed;
|
|
|
|
// Indicates that we've called back into Rust from C
|
|
|
|
bool reentered_rust_stack;
|
2012-07-10 15:56:00 -05:00
|
|
|
unsigned long disallow_kill;
|
2012-02-14 02:43:45 -06:00
|
|
|
|
2012-02-09 03:13:32 -06:00
|
|
|
// The stack used for running C code, borrowed from the scheduler thread
|
|
|
|
stk_seg *c_stack;
|
|
|
|
uintptr_t next_c_sp;
|
2012-02-09 13:51:34 -06:00
|
|
|
uintptr_t next_rust_sp;
|
2012-02-09 03:13:32 -06:00
|
|
|
|
2012-02-15 00:23:16 -06:00
|
|
|
rust_port_selector port_selector;
|
|
|
|
|
2012-03-02 18:33:24 -06:00
|
|
|
lock_and_signal supervisor_lock;
|
|
|
|
rust_task *supervisor; // Parent-link for failure propagation.
|
|
|
|
|
2012-02-07 01:38:22 -06:00
|
|
|
// Called when the atomic refcount reaches zero
|
|
|
|
void delete_this();
|
2012-02-08 20:29:15 -06:00
|
|
|
|
2012-03-21 15:17:24 -05:00
|
|
|
void new_stack_fast(size_t requested_sz);
|
|
|
|
void new_stack(size_t requested_sz);
|
2012-02-08 20:29:15 -06:00
|
|
|
void free_stack(stk_seg *stk);
|
|
|
|
size_t get_next_stack_size(size_t min, size_t current, size_t requested);
|
|
|
|
|
2012-02-09 03:13:32 -06:00
|
|
|
void return_c_stack();
|
|
|
|
|
2012-03-17 20:12:15 -05:00
|
|
|
void transition(rust_task_state src, rust_task_state dst,
|
2012-03-03 04:36:53 -06:00
|
|
|
rust_cond *cond, const char* cond_name);
|
2012-07-12 18:52:21 -05:00
|
|
|
void transition_inner(rust_task_state src, rust_task_state dst,
|
2012-07-02 19:42:58 -05:00
|
|
|
rust_cond *cond, const char* cond_name);
|
2012-03-03 04:36:53 -06:00
|
|
|
|
2012-07-12 18:52:21 -05:00
|
|
|
bool must_fail_from_being_killed_inner();
|
2012-06-01 01:25:13 -05:00
|
|
|
// Called by rust_task_fail to unwind on failure
|
|
|
|
void begin_failure(char const *expr,
|
|
|
|
char const *file,
|
|
|
|
size_t line);
|
2012-03-03 22:50:11 -06:00
|
|
|
|
2012-02-09 03:13:32 -06:00
|
|
|
friend void task_start_wrapper(spawn_args *a);
|
2012-02-14 02:43:45 -06:00
|
|
|
friend void cleanup_task(cleanup_args *a);
|
2012-02-23 01:50:27 -06:00
|
|
|
friend void reset_stack_limit_on_c_stack(reset_args *a);
|
2012-03-21 15:17:24 -05:00
|
|
|
friend void new_stack_slow(new_stack_args *a);
|
2012-06-01 01:25:13 -05:00
|
|
|
friend void rust_task_fail(rust_task *task,
|
|
|
|
char const *expr,
|
|
|
|
char const *file,
|
|
|
|
size_t line);
|
2012-02-09 03:13:32 -06:00
|
|
|
|
2012-07-12 18:52:27 -05:00
|
|
|
friend class rust_port;
|
|
|
|
friend class rust_port_selector;
|
2012-07-12 18:52:21 -05:00
|
|
|
bool block_inner(rust_cond *on, const char* name);
|
|
|
|
void wakeup_inner(rust_cond *from);
|
2012-07-12 18:52:27 -05:00
|
|
|
bool blocked_on(rust_cond *cond);
|
2012-07-02 19:42:58 -05:00
|
|
|
|
2012-02-07 01:38:22 -06:00
|
|
|
public:
|
|
|
|
|
2010-08-08 21:24:35 -05:00
|
|
|
// Only a pointer to 'name' is kept, so it must live as long as this task.
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_task(rust_sched_loop *sched_loop,
|
2012-03-17 20:12:15 -05:00
|
|
|
rust_task_state state,
|
2010-08-08 21:24:35 -05:00
|
|
|
rust_task *spawner,
|
2012-01-29 23:20:36 -06:00
|
|
|
const char *name,
|
|
|
|
size_t init_stack_sz);
|
2010-09-07 20:39:07 -05:00
|
|
|
|
2011-12-30 22:46:08 -06:00
|
|
|
void start(spawn_fn spawnee_fn,
|
2012-02-01 20:52:08 -06:00
|
|
|
rust_opaque_box *env,
|
2012-01-04 22:11:39 -06:00
|
|
|
void *args);
|
2011-08-10 20:48:57 -05:00
|
|
|
void start();
|
2012-07-12 18:52:13 -05:00
|
|
|
void assert_is_running();
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-07-18 14:02:26 -05:00
|
|
|
void *malloc(size_t sz, const char *tag, type_desc *td=0);
|
2012-02-03 02:34:42 -06:00
|
|
|
void *realloc(void *data, size_t sz);
|
|
|
|
void free(void *p);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2012-03-17 20:12:15 -05:00
|
|
|
void set_state(rust_task_state state,
|
2012-03-03 04:36:53 -06:00
|
|
|
rust_cond *cond, const char* cond_name);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2012-03-03 22:50:11 -06:00
|
|
|
bool block(rust_cond *on, const char* name);
|
2010-07-19 16:05:18 -05:00
|
|
|
void wakeup(rust_cond *from);
|
|
|
|
void die();
|
|
|
|
|
2010-10-11 18:40:18 -05:00
|
|
|
// Print a backtrace, if the "bt" logging option is on.
|
|
|
|
void backtrace();
|
|
|
|
|
2012-02-02 17:48:08 -06:00
|
|
|
// Yields control to the scheduler. Called from the Rust stack
|
|
|
|
void yield(bool *killed);
|
2010-08-11 23:23:34 -05:00
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
// Fail this task (assuming caller-on-stack is different task).
|
|
|
|
void kill();
|
|
|
|
|
2012-02-14 02:43:45 -06:00
|
|
|
// Indicates that we've been killed and now is an apropriate
|
|
|
|
// time to fail as a result
|
|
|
|
bool must_fail_from_being_killed();
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
// Fail self, assuming caller-on-stack is this task.
|
2011-07-13 15:43:35 -05:00
|
|
|
void fail();
|
2012-06-01 01:25:13 -05:00
|
|
|
void fail(char const *expr, char const *file, size_t line);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
// Disconnect from our supervisor.
|
|
|
|
void unsupervise();
|
|
|
|
|
|
|
|
frame_glue_fns *get_frame_glue_fns(uintptr_t fp);
|
2011-06-20 19:19:50 -05:00
|
|
|
|
2011-07-18 14:02:26 -05:00
|
|
|
void *calloc(size_t size, const char *tag);
|
2011-06-29 20:47:47 -05:00
|
|
|
|
2011-08-15 13:34:12 -05:00
|
|
|
// Use this function sparingly. Depending on the ref count is generally
|
|
|
|
// not at all safe.
|
|
|
|
intptr_t get_ref_count() const { return ref_count; }
|
2011-08-15 18:54:02 -05:00
|
|
|
|
2011-11-13 18:36:47 -06:00
|
|
|
void notify(bool success);
|
2011-11-30 19:54:11 -06:00
|
|
|
|
2012-02-08 20:04:14 -06:00
|
|
|
void *next_stack(size_t stk_sz, void *args_addr, size_t args_sz);
|
|
|
|
void prev_stack();
|
2011-12-01 17:26:42 -06:00
|
|
|
void record_stack_limit();
|
2011-12-06 18:26:47 -06:00
|
|
|
void reset_stack_limit();
|
2012-04-01 01:12:06 -05:00
|
|
|
|
2011-12-18 18:59:49 -06:00
|
|
|
bool on_rust_stack();
|
2011-12-20 13:20:54 -06:00
|
|
|
void check_stack_canary();
|
2012-03-02 17:14:52 -06:00
|
|
|
void delete_all_stacks();
|
2012-02-08 18:22:38 -06:00
|
|
|
|
2012-03-15 01:08:47 -05:00
|
|
|
void config_notify(rust_port_id port);
|
2012-02-08 23:42:04 -06:00
|
|
|
|
|
|
|
void call_on_c_stack(void *args, void *fn_ptr);
|
2012-02-09 13:51:34 -06:00
|
|
|
void call_on_rust_stack(void *args, void *fn_ptr);
|
2012-02-14 13:58:27 -06:00
|
|
|
bool have_c_stack() { return c_stack != NULL; }
|
2012-02-15 00:23:16 -06:00
|
|
|
|
|
|
|
rust_port_selector *get_port_selector() { return &port_selector; }
|
2012-03-02 23:22:42 -06:00
|
|
|
|
2012-03-17 20:12:15 -05:00
|
|
|
rust_task_state get_state() { return state; }
|
2012-03-03 01:40:27 -06:00
|
|
|
rust_cond *get_cond() { return cond; }
|
|
|
|
const char *get_cond_name() { return cond_name; }
|
2012-03-21 02:31:40 -05:00
|
|
|
|
2012-07-02 19:42:58 -05:00
|
|
|
void clear_event_reject() {
|
|
|
|
this->event_reject = false;
|
|
|
|
}
|
|
|
|
|
2012-07-10 12:58:44 -05:00
|
|
|
void *wait_event(bool *killed);
|
2012-07-02 19:42:58 -05:00
|
|
|
void signal_event(void *event);
|
|
|
|
|
2012-03-21 02:31:40 -05:00
|
|
|
void cleanup_after_turn();
|
2012-05-15 13:34:52 -05:00
|
|
|
|
|
|
|
void inhibit_kill();
|
|
|
|
void allow_kill();
|
2010-07-19 16:05:18 -05:00
|
|
|
};
|
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2697): It would be really nice to be able to get rid of this.
|
2012-04-02 22:18:01 -05:00
|
|
|
inline void *operator new[](size_t size, rust_task *task, const char *tag) {
|
|
|
|
return task->malloc(size, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> struct task_owned {
|
|
|
|
inline void *operator new(size_t size, rust_task *task,
|
|
|
|
const char *tag) {
|
|
|
|
return task->malloc(size, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void *operator new[](size_t size, rust_task *task,
|
|
|
|
const char *tag) {
|
|
|
|
return task->malloc(size, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void *operator new(size_t size, rust_task &task,
|
|
|
|
const char *tag) {
|
|
|
|
return task.malloc(size, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void *operator new[](size_t size, rust_task &task,
|
|
|
|
const char *tag) {
|
|
|
|
return task.malloc(size, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void *ptr) {
|
|
|
|
((T *)ptr)->task->free(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-10 00:28:52 -06:00
|
|
|
// This stuff is on the stack-switching fast path
|
|
|
|
|
2012-06-03 01:14:25 -05:00
|
|
|
// Records the pointer to the end of the Rust stack in a platform-
|
|
|
|
// specific location in the thread control block
|
|
|
|
extern "C" CDECL void record_sp_limit(void *limit);
|
|
|
|
extern "C" CDECL uintptr_t get_sp_limit();
|
|
|
|
// Gets a pointer to the vicinity of the current stack pointer
|
|
|
|
extern "C" uintptr_t get_sp();
|
|
|
|
|
|
|
|
// This is the function that switches between the C and the Rust stack by
|
|
|
|
// calling another function with a single void* argument while changing the
|
|
|
|
// stack pointer. It has a funny name because gdb doesn't normally like to
|
|
|
|
// backtrace through split stacks (thinks it indicates a bug), but has a
|
|
|
|
// special case to allow functions named __morestack to move the stack pointer
|
|
|
|
// around.
|
2012-02-10 00:15:15 -06:00
|
|
|
extern "C" void __morestack(void *args, void *fn_ptr, uintptr_t stack_ptr);
|
|
|
|
|
|
|
|
inline static uintptr_t
|
|
|
|
sanitize_next_sp(uintptr_t next_sp) {
|
|
|
|
|
|
|
|
// Since I'm not precisely sure where the next stack pointer sits in
|
|
|
|
// relation to where the context switch actually happened, nor in relation
|
|
|
|
// to the amount of stack needed for calling __morestack I've added some
|
|
|
|
// extra bytes here.
|
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2698): On the rust stack this potentially puts is quite far
|
|
|
|
// into the red zone. Might want to just allocate a new rust stack every
|
|
|
|
// time we switch back to rust.
|
2012-02-10 00:15:15 -06:00
|
|
|
const uintptr_t padding = 16;
|
|
|
|
|
|
|
|
return align_down(next_sp - padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
rust_task::call_on_c_stack(void *args, void *fn_ptr) {
|
2012-02-10 00:28:52 -06:00
|
|
|
// Too expensive to check
|
2012-04-01 21:14:16 -05:00
|
|
|
// assert(on_rust_stack());
|
2012-02-10 00:15:15 -06:00
|
|
|
|
2012-06-28 00:03:12 -05:00
|
|
|
// The shim functions generated by rustc contain the morestack prologue,
|
|
|
|
// so we need to let them know they have enough stack.
|
2012-06-27 12:07:00 -05:00
|
|
|
record_sp_limit(0);
|
|
|
|
|
2012-02-14 01:18:21 -06:00
|
|
|
uintptr_t prev_rust_sp = next_rust_sp;
|
2012-02-10 00:15:15 -06:00
|
|
|
next_rust_sp = get_sp();
|
|
|
|
|
|
|
|
bool borrowed_a_c_stack = false;
|
|
|
|
uintptr_t sp;
|
|
|
|
if (c_stack == NULL) {
|
2012-03-29 18:31:30 -05:00
|
|
|
c_stack = sched_loop->borrow_c_stack();
|
2012-02-10 00:15:15 -06:00
|
|
|
next_c_sp = align_down(c_stack->end);
|
|
|
|
sp = next_c_sp;
|
|
|
|
borrowed_a_c_stack = true;
|
|
|
|
} else {
|
|
|
|
sp = sanitize_next_sp(next_c_sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
__morestack(args, fn_ptr, sp);
|
|
|
|
|
|
|
|
// Note that we may not actually get here if we threw an exception,
|
|
|
|
// in which case we will return the c stack when the exception is caught.
|
|
|
|
if (borrowed_a_c_stack) {
|
|
|
|
return_c_stack();
|
|
|
|
}
|
2012-02-14 01:18:21 -06:00
|
|
|
|
|
|
|
next_rust_sp = prev_rust_sp;
|
2012-06-27 12:07:00 -05:00
|
|
|
|
|
|
|
record_stack_limit();
|
2012-02-10 00:15:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
rust_task::call_on_rust_stack(void *args, void *fn_ptr) {
|
2012-02-10 00:28:52 -06:00
|
|
|
// Too expensive to check
|
2012-04-01 21:14:16 -05:00
|
|
|
// assert(!on_rust_stack());
|
2012-06-27 12:07:00 -05:00
|
|
|
|
|
|
|
// Because of the hack in the other function that disables the stack limit
|
|
|
|
// when entering the C stack, here we restore the stack limit again.
|
|
|
|
record_stack_limit();
|
|
|
|
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(get_sp_limit() != 0 && "Stack must be configured");
|
|
|
|
assert(next_rust_sp);
|
2012-02-10 00:15:15 -06:00
|
|
|
|
2012-02-14 02:43:45 -06:00
|
|
|
bool had_reentered_rust_stack = reentered_rust_stack;
|
2012-05-15 16:03:59 -05:00
|
|
|
{
|
2012-07-12 18:52:27 -05:00
|
|
|
// FIXME (#2875) This must be racy. Figure it out.
|
2012-07-12 18:52:13 -05:00
|
|
|
scoped_lock with(lifecycle_lock);
|
2012-05-15 16:03:59 -05:00
|
|
|
reentered_rust_stack = true;
|
|
|
|
}
|
2012-02-14 02:43:45 -06:00
|
|
|
|
2012-02-14 01:18:21 -06:00
|
|
|
uintptr_t prev_c_sp = next_c_sp;
|
2012-02-10 00:15:15 -06:00
|
|
|
next_c_sp = get_sp();
|
|
|
|
|
|
|
|
uintptr_t sp = sanitize_next_sp(next_rust_sp);
|
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2047): There are times when this is called and needs
|
2012-03-22 19:19:01 -05:00
|
|
|
// to be able to throw, and we don't account for that.
|
2012-02-10 00:15:15 -06:00
|
|
|
__morestack(args, fn_ptr, sp);
|
2012-02-14 01:18:21 -06:00
|
|
|
|
|
|
|
next_c_sp = prev_c_sp;
|
2012-05-15 16:03:59 -05:00
|
|
|
{
|
2012-07-12 18:52:13 -05:00
|
|
|
scoped_lock with(lifecycle_lock);
|
2012-05-15 16:03:59 -05:00
|
|
|
reentered_rust_stack = had_reentered_rust_stack;
|
|
|
|
}
|
2012-06-27 12:07:00 -05:00
|
|
|
|
|
|
|
record_sp_limit(0);
|
2012-02-10 00:15:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
rust_task::return_c_stack() {
|
2012-02-10 00:28:52 -06:00
|
|
|
// Too expensive to check
|
2012-04-01 21:14:16 -05:00
|
|
|
// assert(on_rust_stack());
|
|
|
|
assert(c_stack != NULL);
|
2012-03-29 18:31:30 -05:00
|
|
|
sched_loop->return_c_stack(c_stack);
|
2012-02-10 00:15:15 -06:00
|
|
|
c_stack = NULL;
|
|
|
|
next_c_sp = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 16:00:37 -05:00
|
|
|
// NB: This runs on the Rust stack
|
|
|
|
inline void *
|
|
|
|
rust_task::next_stack(size_t stk_sz, void *args_addr, size_t args_sz) {
|
|
|
|
new_stack_fast(stk_sz + args_sz);
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(stk->end - (uintptr_t)stk->data >= stk_sz + args_sz
|
|
|
|
&& "Did not receive enough stack");
|
2012-03-21 16:00:37 -05:00
|
|
|
uint8_t *new_sp = (uint8_t*)stk->end;
|
|
|
|
// Push the function arguments to the new stack
|
|
|
|
new_sp = align_down(new_sp - args_sz);
|
|
|
|
|
2012-03-21 18:11:46 -05:00
|
|
|
// I don't know exactly where the region ends that valgrind needs us
|
|
|
|
// to mark accessible. On x86_64 these extra bytes aren't needed, but
|
|
|
|
// on i386 we get errors without.
|
|
|
|
const int fudge_bytes = 16;
|
|
|
|
reuse_valgrind_stack(stk, new_sp - fudge_bytes);
|
2012-03-21 16:00:37 -05:00
|
|
|
|
|
|
|
memcpy(new_sp, args_addr, args_sz);
|
|
|
|
record_stack_limit();
|
|
|
|
return new_sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The amount of stack in a segment available to Rust code
|
|
|
|
inline size_t
|
|
|
|
user_stack_size(stk_seg *stk) {
|
|
|
|
return (size_t)(stk->end
|
|
|
|
- (uintptr_t)&stk->data[0]
|
|
|
|
- RED_ZONE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct new_stack_args {
|
|
|
|
rust_task *task;
|
|
|
|
size_t requested_sz;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
new_stack_slow(new_stack_args *args);
|
|
|
|
|
|
|
|
// NB: This runs on the Rust stack
|
|
|
|
// This is the new stack fast path, in which we
|
|
|
|
// reuse the next cached stack segment
|
|
|
|
inline void
|
|
|
|
rust_task::new_stack_fast(size_t requested_sz) {
|
|
|
|
// The minimum stack size, in bytes, of a Rust stack, excluding red zone
|
2012-03-29 18:31:30 -05:00
|
|
|
size_t min_sz = sched_loop->min_stack_size;
|
2012-03-21 16:00:37 -05:00
|
|
|
|
|
|
|
// Try to reuse an existing stack segment
|
|
|
|
if (stk != NULL && stk->next != NULL) {
|
|
|
|
size_t next_sz = user_stack_size(stk->next);
|
|
|
|
if (min_sz <= next_sz && requested_sz <= next_sz) {
|
|
|
|
stk = stk->next;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_stack_args args = {this, requested_sz};
|
|
|
|
call_on_c_stack(&args, (void*)new_stack_slow);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: This runs on the Rust stack
|
|
|
|
inline void
|
|
|
|
rust_task::prev_stack() {
|
|
|
|
// We're not going to actually delete anything now because that would
|
|
|
|
// require switching to the C stack and be costly. Instead we'll just move
|
|
|
|
// up the link list and clean up later, either in new_stack or after our
|
|
|
|
// turn ends on the scheduler.
|
|
|
|
stk = stk->prev;
|
|
|
|
record_stack_limit();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2012-03-21 16:12:12 -05:00
|
|
|
record_sp_limit(void *limit);
|
2012-03-21 16:00:37 -05:00
|
|
|
|
2012-06-03 01:14:25 -05:00
|
|
|
// The LLVM-generated segmented-stack function prolog compares the amount of
|
|
|
|
// stack needed for each frame to the end-of-stack pointer stored in the
|
|
|
|
// TCB. As an optimization, when the frame size is less than 256 bytes, it
|
|
|
|
// will simply compare %esp to to the stack limit instead of subtracting the
|
|
|
|
// frame size. As a result we need our stack limit to account for those 256
|
|
|
|
// bytes.
|
|
|
|
const unsigned LIMIT_OFFSET = 256;
|
|
|
|
|
2012-03-21 16:00:37 -05:00
|
|
|
inline void
|
|
|
|
rust_task::record_stack_limit() {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(stk);
|
|
|
|
assert((uintptr_t)stk->end - RED_ZONE_SIZE
|
|
|
|
- (uintptr_t)stk->data >= LIMIT_OFFSET
|
|
|
|
&& "Stack size must be greater than LIMIT_OFFSET");
|
2012-03-21 16:12:12 -05:00
|
|
|
record_sp_limit(stk->data + LIMIT_OFFSET + RED_ZONE_SIZE);
|
2012-03-21 16:00:37 -05:00
|
|
|
}
|
|
|
|
|
2012-04-02 00:13:39 -05:00
|
|
|
inline rust_task* rust_get_current_task() {
|
2012-03-21 16:15:30 -05:00
|
|
|
uintptr_t sp_limit = get_sp_limit();
|
2012-04-02 00:13:39 -05:00
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#1226) - Because of a hack in upcall_call_shim_on_c_stack this
|
2012-03-21 16:15:30 -05:00
|
|
|
// value is sometimes inconveniently set to 0, so we can't use this
|
|
|
|
// method of retreiving the task pointer and need to fall back to TLS.
|
2012-04-02 00:13:39 -05:00
|
|
|
if (sp_limit == 0)
|
2012-04-02 16:13:40 -05:00
|
|
|
return rust_sched_loop::get_task_tls();
|
2012-03-21 16:15:30 -05:00
|
|
|
|
2012-04-02 00:13:39 -05:00
|
|
|
// The stack pointer boundary is stored in a quickly-accessible location
|
|
|
|
// in the TCB. From that we can calculate the address of the stack segment
|
|
|
|
// structure it belongs to, and in that structure is a pointer to the task
|
|
|
|
// that owns it.
|
2012-03-21 16:15:30 -05:00
|
|
|
uintptr_t seg_addr =
|
|
|
|
sp_limit - RED_ZONE_SIZE - LIMIT_OFFSET - sizeof(stk_seg);
|
|
|
|
stk_seg *stk = (stk_seg*) seg_addr;
|
2012-04-02 16:13:40 -05:00
|
|
|
|
2012-03-21 16:15:30 -05:00
|
|
|
// Make sure we've calculated the right address
|
|
|
|
::check_stack_canary(stk);
|
2012-04-02 16:23:24 -05:00
|
|
|
assert(stk->task != NULL && "task pointer not in stack structure");
|
2012-03-21 16:15:30 -05:00
|
|
|
return stk->task;
|
|
|
|
}
|
2012-03-21 16:00:37 -05:00
|
|
|
|
2010-07-28 02:36:35 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
#endif /* RUST_TASK_H */
|