@@ -39,20 +39,17 @@ native "rust" mod rustrt {
39
39
fn new_task ( ) -> task_id ;
40
40
fn drop_task ( task : * rust_task ) ;
41
41
fn get_task_pointer ( id : task_id ) -> * rust_task ;
42
- fn start_task ( id : task_id ) ;
43
42
fn get_task_trampoline ( ) -> u32 ;
44
43
45
44
fn migrate_alloc ( alloc : * u8 , target : task_id ) ;
45
+ fn start_task ( id : task_id , closure : * u8 ) ;
46
46
}
47
47
48
48
type rust_task =
49
49
{ id : task ,
50
50
mutable notify_enabled: u32 ,
51
51
mutable notify_chan: comm:: chan < task_notification > ,
52
- ctx : task_context ,
53
- stack_ptr : * u8 } ;
54
-
55
- type task_context = { regs : x86_registers , next : * u8 } ;
52
+ mutable stack_ptr: * u8 } ;
56
53
57
54
resource rust_task_ptr ( task: * rust_task) { rustrt:: drop_task ( task) ; }
58
55
@@ -115,20 +112,22 @@ fn spawn_inner(thunk: -fn(), notify: option<comm::chan<task_notification>>) ->
115
112
task_id {
116
113
let id = rustrt:: new_task ( ) ;
117
114
118
- // the order of arguments are outptr, taskptr, envptr.
119
- // LLVM fastcall puts the first two in ecx, edx, and the rest on the
120
- // stack.
115
+ let raw_thunk: { code : u32 , env : u32 } = cast ( thunk) ;
121
116
122
117
// set up the task pointer
123
118
let task_ptr = rust_task_ptr ( rustrt:: get_task_pointer ( id) ) ;
124
- let regs = ptr:: addr_of ( ( * * task_ptr) . ctx . regs ) ;
125
- ( * regs) . edx = cast ( * task_ptr) ; ;
126
- ( * regs) . esp = cast ( ( * * task_ptr) . stack_ptr ) ;
127
119
128
120
assert ( ptr:: null ( ) != ( * * task_ptr) . stack_ptr ) ;
129
121
130
- let raw_thunk: { code : u32 , env : u32 } = cast ( thunk) ;
131
- ( * regs) . eip = raw_thunk. code ;
122
+ // copy the thunk from our stack to the new stack
123
+ let sp: uint = cast ( ( * * task_ptr) . stack_ptr ) ;
124
+ let ptrsize = sys:: size_of :: < * u8 > ( ) ;
125
+ let thunkfn: * mutable uint = cast ( sp - ptrsize * 2 u) ;
126
+ let thunkenv: * mutable uint = cast ( sp - ptrsize) ;
127
+ * thunkfn = cast ( raw_thunk. code ) ;
128
+ * thunkenv = cast ( raw_thunk. env ) ;
129
+ // align the stack to 16 bytes
130
+ ( * * task_ptr) . stack_ptr = cast ( sp - ptrsize * 4 u) ;
132
131
133
132
// set up notifications if they are enabled.
134
133
alt notify {
@@ -139,60 +138,14 @@ fn spawn_inner(thunk: -fn(), notify: option<comm::chan<task_notification>>) ->
139
138
none { }
140
139
} ;
141
140
142
- // okay, now we align the stack and add the environment pointer and a fake
143
- // return address.
144
-
145
- // -12 for the taskm output location, the env pointer
146
- // -4 for the return address.
147
- ( * regs) . esp = align_down ( ( * regs) . esp - 12u32 ) - 4u32 ;
148
-
149
- let ra: * mutable u32 = cast ( ( * regs) . esp ) ;
150
- let env: * mutable u32 = cast ( ( * regs) . esp + 4u32 ) ;
151
- let tptr: * mutable u32 = cast ( ( * regs) . esp + 12u32 ) ;
152
-
153
- // put the return pointer in ecx.
154
- ( * regs) . ecx = ( * regs) . esp + 8u32 ; ;
155
-
156
- * tptr = cast ( * task_ptr) ; ;
157
- * env = raw_thunk. env ; ;
158
- * ra = rustrt:: get_task_trampoline ( ) ;
159
-
141
+ // give the thunk environment's allocation to the new task
160
142
rustrt:: migrate_alloc ( cast ( raw_thunk. env ) , id) ;
161
- rustrt:: start_task ( id) ;
162
-
143
+ rustrt:: start_task ( id, cast ( thunkfn ) ) ;
144
+ // don't cleanup the thunk in this task
163
145
unsafe :: leak ( thunk) ;
164
-
165
146
ret id;
166
147
}
167
148
168
- // Who says we can't write an operating system in Rust?
169
- type x86_registers =
170
- // This needs to match the structure in context.h
171
-
172
-
173
- { mutable eax: u32 ,
174
- mutable ebx: u32 ,
175
- mutable ecx: u32 ,
176
- mutable edx: u32 ,
177
- mutable ebp: u32 ,
178
- mutable esi: u32 ,
179
- mutable edi: u32 ,
180
- mutable esp: u32 ,
181
- mutable cs: u16 ,
182
- mutable ds: u16 ,
183
- mutable ss: u16 ,
184
- mutable es: u16 ,
185
- mutable fs: u16 ,
186
- mutable gs: u16 ,
187
- mutable eflags: u32 ,
188
- mutable eip: u32 } ;
189
-
190
- fn align_down ( x : u32 ) -> u32 {
191
-
192
- // Aligns x down to 16 bytes
193
- x & !15u32
194
- }
195
-
196
149
// Local Variables:
197
150
// mode: rust;
198
151
// fill-column: 78;
0 commit comments