1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
#include "rust_cc.h"
#include "rust_gc.h"
#include "rust_internal.h"
#include "rust_scheduler.h"
#include "rust_unwind.h"
#include "rust_upcall.h"
#include <stdint.h>
extern "C" void record_sp(void *limit);
/**
* Switches to the C-stack and invokes |fn_ptr|, passing |args| as argument.
*/
extern "C" CDECL void
upcall_call_shim_on_c_stack(void *args, void *fn_ptr) {
rust_task *task = rust_scheduler::get_task();
// FIXME (1226) - The shim functions generated by rustc contain the
// morestack prologue, so we need to let them know they have enough
// stack.
record_sp(0);
rust_scheduler *sched = task->sched;
try {
sched->c_context.call_shim_on_c_stack(args, fn_ptr);
} catch (...) {
task = rust_scheduler::get_task();
task->record_stack_limit();
throw;
}
task = rust_scheduler::get_task();
task->record_stack_limit();
}
#if defined(__i386__) || defined(__x86_64__) || defined(_M_X64)
void
check_stack(rust_task *task) {
void *esp;
# ifdef __i386__
asm volatile("movl %%esp,%0" : "=r" (esp));
# else
asm volatile("mov %%rsp,%0" : "=r" (esp));
# endif
if (esp < task->stk->data)
task->kernel->fatal("Out of stack space, sorry");
}
#else
#warning "Stack checks are not supported on this architecture"
void
check_stack(rust_task *task) {
// TODO
}
#endif
// Copy elements from one vector to another,
// dealing with reference counts
static inline void
copy_elements(rust_task *task, type_desc *elem_t,
void *pdst, void *psrc, size_t n) {
char *dst = (char *)pdst, *src = (char *)psrc;
memmove(dst, src, n);
// increment the refcount of each element of the vector
if (elem_t->take_glue) {
glue_fn *take_glue = elem_t->take_glue;
size_t elem_size = elem_t->size;
const type_desc **tydescs = elem_t->first_param;
for (char *p = dst; p < dst+n; p += elem_size) {
take_glue(NULL, NULL, tydescs, p);
}
}
}
struct s_fail_args {
char const *expr;
char const *file;
size_t line;
};
extern "C" CDECL void
upcall_s_fail(s_fail_args *args) {
rust_task *task = rust_scheduler::get_task();
LOG_UPCALL_ENTRY(task);
LOG_ERR(task, upcall, "upcall fail '%s', %s:%" PRIdPTR,
args->expr, args->file, args->line);
task->fail();
}
struct s_malloc_args {
size_t nbytes;
type_desc *td;
};
extern "C" CDECL uintptr_t
upcall_s_malloc(s_malloc_args *args) {
rust_task *task = rust_scheduler::get_task();
LOG_UPCALL_ENTRY(task);
LOG(task, mem,
"upcall malloc(%" PRIdPTR ", 0x%" PRIxPTR ")",
args->nbytes, args->td);
gc::maybe_gc(task);
cc::maybe_cc(task);
// TODO: Maybe use dladdr here to find a more useful name for the
// type_desc.
void *p = task->malloc(args->nbytes, "tdesc", args->td);
memset(p, '\0', args->nbytes);
task->local_allocs[p] = args->td;
debug::maybe_track_origin(task, p);
LOG(task, mem,
"upcall malloc(%" PRIdPTR ", 0x%" PRIxPTR ") = 0x%" PRIxPTR,
args->nbytes, args->td, (uintptr_t)p);
return (uintptr_t) p;
}
struct s_free_args {
void *ptr;
uintptr_t is_gc;
};
/**
* Called whenever an object's ref count drops to zero.
*/
extern "C" CDECL void
upcall_s_free(s_free_args *args) {
rust_task *task = rust_scheduler::get_task();
LOG_UPCALL_ENTRY(task);
rust_scheduler *sched = task->sched;
DLOG(sched, mem,
"upcall free(0x%" PRIxPTR ", is_gc=%" PRIdPTR ")",
(uintptr_t)args->ptr, args->is_gc);
task->local_allocs.erase(args->ptr);
debug::maybe_untrack_origin(task, args->ptr);
task->free(args->ptr, (bool) args->is_gc);
}
struct s_shared_malloc_args {
size_t nbytes;
type_desc *td;
};
extern "C" CDECL uintptr_t
upcall_s_shared_malloc(s_shared_malloc_args *args) {
rust_task *task = rust_scheduler::get_task();
LOG_UPCALL_ENTRY(task);
LOG(task, mem,
"upcall shared_malloc(%" PRIdPTR ", 0x%" PRIxPTR ")",
args->nbytes, args->td);
void *p = task->kernel->malloc(args->nbytes, "shared malloc");
memset(p, '\0', args->nbytes);
LOG(task, mem,
"upcall shared_malloc(%" PRIdPTR ", 0x%" PRIxPTR
") = 0x%" PRIxPTR,
args->nbytes, args->td, (uintptr_t)p);
return (uintptr_t) p;
}
struct s_shared_free_args {
void *ptr;
};
/**
* Called whenever an object's ref count drops to zero.
*/
extern "C" CDECL void
upcall_s_shared_free(s_shared_free_args *args) {
rust_task *task = rust_scheduler::get_task();
LOG_UPCALL_ENTRY(task);
rust_scheduler *sched = task->sched;
DLOG(sched, mem,
"upcall shared_free(0x%" PRIxPTR")",
(uintptr_t)args->ptr);
task->kernel->free(args->ptr);
}
struct s_get_type_desc_args {
size_t size;
size_t align;
size_t n_descs;
type_desc const **descs;
uintptr_t n_obj_params;
};
extern "C" CDECL type_desc *
upcall_s_get_type_desc(s_get_type_desc_args *args) {
rust_task *task = rust_scheduler::get_task();
check_stack(task);
LOG_UPCALL_ENTRY(task);
LOG(task, cache, "upcall get_type_desc with size=%" PRIdPTR
", align=%" PRIdPTR ", %" PRIdPTR " descs", args->size, args->align,
args->n_descs);
rust_crate_cache *cache = task->get_crate_cache();
type_desc *td = cache->get_type_desc(args->size, args->align, args->n_descs,
args->descs, args->n_obj_params);
LOG(task, cache, "returning tydesc 0x%" PRIxPTR, td);
return td;
}
struct s_vec_grow_args {
rust_vec** vp;
size_t new_sz;
};
extern "C" CDECL void
upcall_s_vec_grow(s_vec_grow_args *args) {
rust_task *task = rust_scheduler::get_task();
LOG_UPCALL_ENTRY(task);
reserve_vec(task, args->vp, args->new_sz);
(*args->vp)->fill = args->new_sz;
}
struct s_vec_push_args {
rust_vec** vp;
type_desc* elt_ty;
void* elt;
};
extern "C" CDECL void
upcall_s_vec_push(s_vec_push_args *args) {
rust_task *task = rust_scheduler::get_task();
LOG_UPCALL_ENTRY(task);
size_t new_sz = (*args->vp)->fill + args->elt_ty->size;
reserve_vec(task, args->vp, new_sz);
rust_vec* v = *args->vp;
copy_elements(task, args->elt_ty, &v->data[0] + v->fill,
args->elt, args->elt_ty->size);
v->fill += args->elt_ty->size;
}
/**
* Returns a token that can be used to deallocate all of the allocated space
* space in the dynamic stack.
*/
extern "C" CDECL void *
upcall_s_dynastack_mark() {
return rust_scheduler::get_task()->dynastack.mark();
}
struct s_dynastack_alloc_args {
size_t sz;
};
/**
* Allocates space in the dynamic stack and returns it.
*
* FIXME: Deprecated since dynamic stacks need to be self-describing for GC.
*/
extern "C" CDECL void *
upcall_s_dynastack_alloc(s_dynastack_alloc_args *args) {
size_t sz = args->sz;
return sz ? rust_scheduler::get_task()->dynastack.alloc(sz, NULL) : NULL;
}
struct s_dynastack_alloc_2_args {
size_t sz;
type_desc *ty;
};
/**
* Allocates space associated with a type descriptor in the dynamic stack and
* returns it.
*/
extern "C" CDECL void *
upcall_s_dynastack_alloc_2(s_dynastack_alloc_2_args *args) {
size_t sz = args->sz;
type_desc *ty = args->ty;
return sz ? rust_scheduler::get_task()->dynastack.alloc(sz, ty) : NULL;
}
struct s_dynastack_free_args {
void *ptr;
};
/** Frees space in the dynamic stack. */
extern "C" CDECL void
upcall_s_dynastack_free(s_dynastack_free_args *args) {
return rust_scheduler::get_task()->dynastack.free(args->ptr);
}
extern "C" _Unwind_Reason_Code
__gxx_personality_v0(int version,
_Unwind_Action actions,
uint64_t exception_class,
_Unwind_Exception *ue_header,
_Unwind_Context *context);
struct s_rust_personality_args {
int version;
_Unwind_Action actions;
uint64_t exception_class;
_Unwind_Exception *ue_header;
_Unwind_Context *context;
};
extern "C" _Unwind_Reason_Code
upcall_s_rust_personality(s_rust_personality_args *args) {
return __gxx_personality_v0(args->version,
args->actions,
args->exception_class,
args->ue_header,
args->context);
}
extern "C" void
shape_cmp_type(int8_t *result, const type_desc *tydesc,
const type_desc **subtydescs, uint8_t *data_0,
uint8_t *data_1, uint8_t cmp_type);
struct s_cmp_type_args {
int8_t *result;
const type_desc *tydesc;
const type_desc **subtydescs;
uint8_t *data_0;
uint8_t *data_1;
uint8_t cmp_type;
};
extern "C" void
upcall_s_cmp_type(s_cmp_type_args *args) {
shape_cmp_type(args->result, args->tydesc, args->subtydescs,
args->data_0, args->data_1, args->cmp_type);
}
extern "C" void
shape_log_type(const type_desc *tydesc, uint8_t *data, uint32_t level);
struct s_log_type_args {
const type_desc *tydesc;
uint8_t *data;
uint32_t level;
};
extern "C" void
upcall_s_log_type(s_log_type_args *args) {
shape_log_type(args->tydesc, args->data, args->level);
}
// ______________________________________________________________________________
// Upcalls in original format: deprecated and should be removed once snapshot
// transitions them away.
extern "C" CDECL void
upcall_fail(char const *expr,
char const *file,
size_t line) {
s_fail_args args = {expr,file,line};
upcall_s_fail(&args);
}
extern "C" CDECL uintptr_t
upcall_malloc(size_t nbytes, type_desc *td) {
s_malloc_args args = {nbytes, td};
return upcall_s_malloc(&args);
}
/**
* Called whenever an object's ref count drops to zero.
*/
extern "C" CDECL void
upcall_free(void* ptr, uintptr_t is_gc) {
s_free_args args = {ptr, is_gc};
upcall_s_free(&args);
}
extern "C" CDECL uintptr_t
upcall_shared_malloc(size_t nbytes, type_desc *td) {
s_shared_malloc_args args = {nbytes, td};
return upcall_s_shared_malloc(&args);
}
/**
* Called whenever an object's ref count drops to zero.
*/
extern "C" CDECL void
upcall_shared_free(void* ptr) {
s_shared_free_args args = {ptr};
upcall_s_shared_free(&args);
}
extern "C" CDECL type_desc *
upcall_get_type_desc(void *curr_crate, // ignored, legacy compat.
size_t size,
size_t align,
size_t n_descs,
type_desc const **descs,
uintptr_t n_obj_params) {
s_get_type_desc_args args = {size,align,n_descs,descs,n_obj_params};
return upcall_s_get_type_desc(&args);
}
extern "C" CDECL void
upcall_vec_grow(rust_vec** vp, size_t new_sz) {
s_vec_grow_args args = {vp, new_sz};
upcall_s_vec_grow(&args);
}
extern "C" CDECL void
upcall_vec_push(rust_vec** vp, type_desc* elt_ty, void* elt) {
s_vec_push_args args = {vp, elt_ty, elt};
upcall_s_vec_push(&args);
}
/**
* Returns a token that can be used to deallocate all of the allocated space
* space in the dynamic stack.
*/
extern "C" CDECL void *
upcall_dynastack_mark() {
return upcall_s_dynastack_mark();
}
/**
* Allocates space in the dynamic stack and returns it.
*
* FIXME: Deprecated since dynamic stacks need to be self-describing for GC.
*/
extern "C" CDECL void *
upcall_dynastack_alloc(size_t sz) {
s_dynastack_alloc_args args = {sz};
return upcall_s_dynastack_alloc(&args);
}
/**
* Allocates space associated with a type descriptor in the dynamic stack and
* returns it.
*/
extern "C" CDECL void *
upcall_dynastack_alloc_2(size_t sz, type_desc *ty) {
s_dynastack_alloc_2_args args = {sz, ty};
return upcall_s_dynastack_alloc_2(&args);
}
/** Frees space in the dynamic stack. */
extern "C" CDECL void
upcall_dynastack_free(void *ptr) {
s_dynastack_free_args args = {ptr};
return upcall_s_dynastack_free(&args);
}
extern "C" _Unwind_Reason_Code
upcall_rust_personality(int version,
_Unwind_Action actions,
uint64_t exception_class,
_Unwind_Exception *ue_header,
_Unwind_Context *context) {
s_rust_personality_args args = {version, actions, exception_class, ue_header,
context};
return upcall_s_rust_personality(&args);
}
extern "C" void
upcall_cmp_type(int8_t *result, const type_desc *tydesc,
const type_desc **subtydescs, uint8_t *data_0,
uint8_t *data_1, uint8_t cmp_type) {
s_cmp_type_args args = {result, tydesc, subtydescs, data_0, data_1, cmp_type};
upcall_s_cmp_type(&args);
}
extern "C" void
upcall_log_type(const type_desc *tydesc, uint8_t *data, uint32_t level) {
s_log_type_args args = {tydesc, data, level};
upcall_s_log_type(&args);
}
struct rust_new_stack2_args {
void *new_stack;
size_t stk_sz;
void *args_addr;
size_t args_sz;
};
// A new stack function suitable for calling through
// upcall_call_shim_on_c_stack
// FIXME: Convert this to the same arrangement as
// the other upcalls, simplify __morestack
extern "C" CDECL void
upcall_new_stack(struct rust_new_stack2_args *args) {
rust_task *task = rust_scheduler::get_task();
args->new_stack = task->new_stack(args->stk_sz,
args->args_addr,
args->args_sz);
}
// FIXME: As above
extern "C" CDECL void
upcall_del_stack() {
rust_task *task = rust_scheduler::get_task();
task->del_stack();
}
// Landing pads need to call this to insert the
// correct limit into TLS.
// NB: This must run on the Rust stack because it
// needs to acquire the value of the stack pointer
extern "C" CDECL void
upcall_reset_stack_limit() {
rust_task *task = rust_scheduler::get_task();
task->reset_stack_limit();
}
//
// Local Variables:
// mode: C++
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//
|