Skip to content

Commit de18333

Browse files
committed
---
yaml --- r: 6269 b: refs/heads/master c: ced0d4f h: refs/heads/master i: 6267: 1ec0d05 v: v3
1 parent 6a3a516 commit de18333

File tree

7 files changed

+145
-31
lines changed

7 files changed

+145
-31
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 5b9f76eb7c53a28ed99d4005645485e24568ce22
2+
refs/heads/master: ced0d4f15e11e2c74766d1055146946ded3fba51

trunk/mk/rt.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ RUNTIME_CS_$(1) := \
6767

6868
RUNTIME_S_$(1) := rt/arch/$$(HOST_$(1))/_context.S \
6969
rt/arch/$$(HOST_$(1))/ccall.S \
70-
rt/arch/$$(HOST_$(1))/morestack.S
70+
rt/arch/$$(HOST_$(1))/morestack.S \
71+
rt/arch/$$(HOST_$(1))/record_sp.S
7172

7273
RUNTIME_HDR_$(1) := rt/globals.h \
7374
rt/rust.h \

trunk/src/rt/arch/i386/morestack.S

+98-21
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,117 @@
22

33
// __morestack
44
//
5-
// LLVM generates a call to this to allocate more stack space in a functiono
5+
// LLVM generates a call to this to allocate more stack space in a function
66
// prolog when we run out.
77

88
#if defined(__APPLE__) || defined(_WIN32)
9-
#define RUST_NEW_STACK _rust_new_stack
10-
#define RUST_DEL_STACK _rust_del_stack
11-
#define MORESTACK ___morestack
9+
#define RUST_NEW_STACK _rust_new_stack
10+
#define RUST_DEL_STACK _rust_del_stack
11+
#define RUST_GET_PREV_STACK _rust_get_prev_stack
12+
#define RUST_GET_TASK _rust_get_task
13+
#define UPCALL_ALLOC_C_STACK _upcall_alloc_c_stack
14+
#define UPCALL_CALL_C_STACK _upcall_call_c_stack
15+
#define MORESTACK ___morestack
1216
#else
13-
#define RUST_NEW_STACK rust_new_stack
14-
#define RUST_DEL_STACK rust_del_stack
15-
#define MORESTACK __morestack
17+
#define RUST_NEW_STACK rust_new_stack
18+
#define RUST_DEL_STACK rust_del_stack
19+
#define RUST_GET_PREV_STACK rust_get_prev_stack
20+
#define RUST_GET_TASK rust_get_task
21+
#define UPCALL_ALLOC_C_STACK upcall_alloc_c_stack
22+
#define UPCALL_CALL_C_STACK upcall_call_c_stack
23+
#define MORESTACK __morestack
1624
#endif
1725

26+
#ifdef __APPLE__
27+
#define ALIGNMENT 4
28+
#else
29+
#define ALIGNMENT 8
30+
#endif
31+
32+
#define RETURN_OFFSET 7
33+
1834
.globl RUST_NEW_STACK
1935
.globl RUST_DEL_STACK
20-
36+
.globl RUST_GET_PREV_STACK
37+
.globl RUST_GET_TASK
38+
.globl UPCALL_ALLOC_C_STACK
39+
.globl UPCALL_CALL_C_STACK
2140
.globl MORESTACK
2241

2342
MORESTACK:
24-
pushl 8(%esp) // argsz > ra stksz argsz x x ra args
25-
leal 28(%esp),%eax // argsz ra stksz argsz x x ra args
26-
pushl %eax // argp > argsz ra stksz argsz x x ra args
27-
pushl 12(%esp) // stksz > argp argsz ra stksz argsz x x ra args
28-
calll RUST_NEW_STACK
29-
addl $12,%esp // ra stksz argsz x x ra args
30-
31-
movl (%esp),%edx // Grab the return pointer.
32-
incl %edx // Skip past the `ret`.
33-
movl %eax,%esp // Switch to the new stack.
43+
44+
// Sanity check to make sure that there is a currently-running task.
45+
subl $12,%esp
46+
calll RUST_GET_TASK
47+
testl %eax,%eax
48+
jz L$bail
49+
50+
subl $12,%esp
51+
pushl $12
52+
calll UPCALL_ALLOC_C_STACK
53+
movl %eax,%edx
54+
55+
// C stack | esp+12
56+
// ---------------------+-------------------------
57+
movl 20(%esp),%eax // | ra stksz argsz x ra args
58+
movl %eax,8(%edx) // argsz > | ra stksz argsz x ra args
59+
leal 32(%esp),%eax // argsz | ra stksz argsz x ra args
60+
movl %eax,4(%edx) // argp > argsz | ra stksz argsz x ra args
61+
movl 16(%esp),%eax // argp argsz | ra stksz argsz x ra args
62+
movl %eax,(%edx) // stksz > argp argsz | ra stksz argsz x ra args
63+
64+
calll L$pic_ref_pt_0
65+
L$pic_ref_pt_0:
66+
popl %eax
67+
68+
movl rust_new_stack_sym-L$pic_ref_pt_0(%eax),%eax
69+
movl %eax,(%esp)
70+
movl %edx,4(%esp)
71+
calll UPCALL_CALL_C_STACK
72+
73+
movl 16(%esp),%edx // Grab the return pointer.
74+
addl $RETURN_OFFSET,%edx // Skip past the `add esp,4` and the `ret`.
75+
76+
movl %eax,%esp // Switch stacks.
77+
subl $12,%esp // Align the stack.
3478
calll *%edx // Re-enter the function that called us.
3579

3680
// Now the function that called us has returned, so we need to delete the
3781
// old stack space.
38-
calll RUST_DEL_STACK
39-
movl %eax,%esp
40-
retl $8 // ra stksz argsz x x ra args
82+
83+
calll RUST_GET_PREV_STACK
84+
movl %eax,%esp // Switch back to the old stack.
85+
86+
movl $0,(%esp)
87+
calll UPCALL_ALLOC_C_STACK
88+
89+
calll L$pic_ref_pt_1
90+
L$pic_ref_pt_1:
91+
popl %edx
92+
93+
movl rust_del_stack_sym-L$pic_ref_pt_1(%edx),%edx
94+
movl %edx,(%esp)
95+
movl %eax,4(%esp)
96+
calll UPCALL_CALL_C_STACK
97+
98+
addl $16,%esp
99+
retl $16 + ALIGNMENT // ra stksz argsz x ra args
100+
101+
L$bail:
102+
movl 12(%esp),%edx
103+
addl $RETURN_OFFSET,%edx
104+
addl $12+16,%esp
105+
jmpl *%edx
106+
107+
#ifdef __APPLE__
108+
109+
.section __IMPORT,__pointers,non_lazy_symbol_pointers
110+
rust_new_stack_sym:
111+
.indirect_symbol RUST_NEW_STACK
112+
.long 0
113+
rust_del_stack_sym:
114+
.indirect_symbol RUST_DEL_STACK
115+
.long 0
116+
117+
#endif
41118

trunk/src/rt/rust.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "rust_internal.h"
2+
#include <cstdio>
23

34
struct
45
command_line_args : public kernel_owned<command_line_args>
@@ -75,6 +76,8 @@ int check_claims = 0;
7576

7677
extern "C" CDECL int
7778
rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {
79+
fprintf(stderr, "rust_start, argc=%d argv=%p\n", argc, argv);
80+
7881
rust_env *env = load_env();
7982

8083
update_log_settings(crate_map, env->logspec);

trunk/src/rt/rust_scheduler.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include <stdarg.h>
33
#include <cassert>
4+
#include <pthread.h>
45
#include "rust_internal.h"
56
#include "globals.h"
67

@@ -12,6 +13,9 @@ DWORD rust_scheduler::task_key;
1213

1314
bool rust_scheduler::tls_initialized = false;
1415

16+
// Defined in arch/*/record_sp.S.
17+
extern "C" void rust_record_sp(uintptr_t sp);
18+
1519
rust_scheduler::rust_scheduler(rust_kernel *kernel,
1620
rust_srv *srv,
1721
int id) :
@@ -286,6 +290,8 @@ rust_scheduler::start_main_loop() {
286290
scheduled_task->state->name);
287291

288292
place_task_in_tls(scheduled_task);
293+
rust_record_sp(scheduled_task->stk->limit);
294+
//pthread_setspecific(89, (void *)scheduled_task->stk->limit);
289295

290296
interrupt_flag = 0;
291297

@@ -375,6 +381,8 @@ rust_scheduler::place_task_in_tls(rust_task *task) {
375381

376382
rust_task *
377383
rust_scheduler::get_task() {
384+
if (!tls_initialized)
385+
return NULL;
378386
rust_task *task = reinterpret_cast<rust_task *>
379387
(pthread_getspecific(task_key));
380388
assert(task && "Couldn't get the task from TLS!");
@@ -396,6 +404,8 @@ rust_scheduler::place_task_in_tls(rust_task *task) {
396404

397405
rust_task *
398406
rust_scheduler::get_task() {
407+
if (!tls_initialized)
408+
return NULL;
399409
rust_task *task = reinterpret_cast<rust_task *>(TlsGetValue(task_key));
400410
assert(task && "Couldn't get the task from TLS!");
401411
return task;

trunk/src/rt/rust_task.cpp

+23-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
#ifndef __WIN32__
99
#include <execinfo.h>
1010
#endif
11+
#include <iostream>
1112
#include <cassert>
1213
#include <cstring>
1314

1415
#include "globals.h"
1516

17+
#define RED_ZONE_SIZE 128
18+
1619
// Stack size
1720
size_t g_custom_min_stack_size = 0;
1821

@@ -34,16 +37,16 @@ new_stk(rust_scheduler *sched, rust_task *task, size_t minsz)
3437
size_t min_stk_bytes = get_min_stk_size(sched->min_stack_size);
3538
if (minsz < min_stk_bytes)
3639
minsz = min_stk_bytes;
37-
size_t sz = sizeof(stk_seg) + minsz;
40+
size_t sz = sizeof(stk_seg) + minsz + RED_ZONE_SIZE;
3841
stk_seg *stk = (stk_seg *)task->malloc(sz, "stack");
3942
LOGPTR(task->sched, "new stk", (uintptr_t)stk);
4043
memset(stk, 0, sizeof(stk_seg));
4144
stk->next = task->stk;
42-
stk->limit = (uintptr_t) &stk->data[minsz];
45+
stk->limit = (uintptr_t) &stk->data[minsz + RED_ZONE_SIZE];
4346
LOGPTR(task->sched, "stk limit", stk->limit);
4447
stk->valgrind_id =
4548
VALGRIND_STACK_REGISTER(&stk->data[0],
46-
&stk->data[minsz]);
49+
&stk->data[minsz + RED_ZONE_SIZE]);
4750
task->stk = stk;
4851
return stk;
4952
}
@@ -63,18 +66,32 @@ del_stk(rust_task *task, stk_seg *stk)
6366
// Entry points for `__morestack` (see arch/*/morestack.S).
6467
extern "C" void *
6568
rust_new_stack(size_t stk_sz, void *args_addr, size_t args_sz) {
69+
std::cerr << "*** New stack!\n";
70+
6671
rust_task *task = rust_scheduler::get_task();
72+
if (!task)
73+
return NULL;
74+
6775
stk_seg *stk_seg = new_stk(task->sched, task, stk_sz);
6876
memcpy(stk_seg->data, args_addr, args_sz);
6977
return stk_seg->data;
7078
}
7179

72-
extern "C" void *
80+
extern "C" void
7381
rust_del_stack() {
7482
rust_task *task = rust_scheduler::get_task();
75-
stk_seg *next_seg = task->stk->next;
7683
del_stk(task, task->stk);
77-
return next_seg->data;
84+
}
85+
86+
extern "C" void *
87+
rust_get_prev_stack() {
88+
rust_task *task = rust_scheduler::get_task();
89+
return task->stk->next;
90+
}
91+
92+
extern "C" rust_task *
93+
rust_get_task() {
94+
return rust_scheduler::get_task();
7895
}
7996

8097

trunk/src/rt/rustrt.def.in

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
__morestack
12
align_of
23
chan_id_send
34
check_claims
@@ -28,19 +29,24 @@ rand_free
2829
rand_new
2930
rand_next
3031
refcount
32+
rust_del_stack
3133
rust_file_is_dir
34+
rust_getcwd
35+
rust_get_prev_stack
3236
rust_get_stdin
3337
rust_get_stdout
3438
rust_get_stderr
3539
rust_str_push
3640
rust_list_files
3741
rust_port_size
42+
rust_new_stack
3843
rust_process_wait
3944
rust_ptr_eq
4045
rust_run_program
4146
rust_start
4247
rust_getcwd
4348
rust_task_sleep
49+
rust_get_task
4450
set_min_stack
4551
sched_threads
4652
size_of
@@ -61,10 +67,10 @@ upcall_dynastack_mark
6167
upcall_fail
6268
upcall_free
6369
upcall_get_type_desc
64-
upcall_vec_grow
65-
upcall_vec_push
6670
upcall_log_type
6771
upcall_malloc
6872
upcall_rust_personality
6973
upcall_shared_malloc
7074
upcall_shared_free
75+
upcall_vec_grow
76+
upcall_vec_push

0 commit comments

Comments
 (0)