forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiber.c
500 lines (425 loc) · 14.3 KB
/
fiber.c
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
#define CAML_INTERNALS
#include <string.h>
#include <unistd.h>
#include "caml/misc.h"
#include "caml/fiber.h"
#include "caml/gc_ctrl.h"
#include "caml/instruct.h"
#include "caml/fail.h"
#include "caml/alloc.h"
#include "caml/platform.h"
#include "caml/fix_code.h"
#include "caml/minor_gc.h"
#include "caml/major_gc.h"
#include "caml/shared_heap.h"
#include "caml/memory.h"
#include "caml/startup_aux.h"
#ifdef NATIVE_CODE
#include "caml/stack.h"
#include "frame_descriptors.h"
#endif
#ifdef DEBUG
#define fiber_debug_log(...) caml_gc_log(__VA_ARGS__)
#else
#define fiber_debug_log(...)
#endif
struct stack_info** caml_alloc_stack_cache () {
int i;
struct stack_info** stack_cache =
(struct stack_info**)caml_stat_alloc_noexc(sizeof(struct stack_info*)*NUM_STACK_SIZE_CLASSES);
if (stack_cache == NULL)
return NULL;
for(i = 0; i < NUM_STACK_SIZE_CLASSES; i++)
stack_cache[i] = NULL;
return stack_cache;
}
static inline struct stack_info* alloc_for_stack (mlsize_t wosize)
{
return caml_stat_alloc_noexc(sizeof(struct stack_info) +
sizeof(value) * wosize +
8 /* for alignment */ +
sizeof(struct stack_handler));
}
static inline intnat stack_size_class (mlsize_t wosize)
{
intnat size_class = 0;
if (wosize % caml_fiber_wsz != 0)
return -1;
wosize = wosize / caml_fiber_wsz;
do {
if ((wosize = wosize >> 1) == 0)
return size_class;
} while(size_class++ < NUM_STACK_SIZE_CLASSES);
return -1;
}
/* allocate a stack with at least "wosize" usable words of stack */
static struct stack_info* alloc_stack_noexc(mlsize_t wosize, value hval, value hexn, value heff)
{
struct stack_info* stack;
struct stack_handler* hand;
intnat size_class;
CAML_STATIC_ASSERT(sizeof(struct stack_info) % sizeof(value) == 0);
CAML_STATIC_ASSERT(sizeof(struct stack_handler) % sizeof(value) == 0);
size_class = stack_size_class (wosize);
if (size_class >= 0 && Caml_state->stack_cache[size_class] != NULL) {
stack = Caml_state->stack_cache[size_class];
CAMLassert(stack->size_class == size_class);
Caml_state->stack_cache[size_class] = stack->handler->parent;
} else {
stack = alloc_for_stack(wosize);
stack->size_class = size_class;
}
if (stack == NULL) {
return NULL;
}
/* Ensure 16-byte alignment because some architectures require it */
hand = (struct stack_handler*)
(((uintnat)stack + sizeof(struct stack_info) + sizeof(value) * wosize + 8)
& ((uintnat)-1 << 4));
hand->handle_value = hval;
hand->handle_exn = hexn;
hand->handle_effect = heff;
hand->parent = NULL;
stack->handler = hand;
stack->sp = (value*)hand;
stack->exception_ptr = NULL;
stack->magic = 42;
CAMLassert(Stack_high(stack) - Stack_base(stack) == wosize ||
Stack_high(stack) - Stack_base(stack) == wosize + 1);
return stack;
}
#ifdef NATIVE_CODE
value caml_alloc_stack (value hval, value hexn, value heff) {
struct stack_info* stack = alloc_stack_noexc(caml_fiber_wsz, hval, hexn, heff);
if (!stack) caml_raise_out_of_memory();
fiber_debug_log ("Allocate stack=%p of %lu words", stack, caml_fiber_wsz);
return Val_ptr(stack);
}
void caml_get_stack_sp_pc (struct stack_info* stack, char** sp /* out */, uintnat* pc /* out */)
{
char* p = (char*)stack->sp;
p += sizeof(value);
*sp = p;
*pc = Saved_return_address(*sp);
}
static inline void scan_stack_frames(scanning_action f, void* fdata, struct stack_info* stack, value* gc_regs)
{
char * sp;
uintnat retaddr;
value * regs;
frame_descr * d;
uintnat h;
int n, ofs;
unsigned short * p;
value *root;
caml_frame_descrs fds = caml_get_frame_descrs();
sp = (char*)stack->sp;
next_chunk:
if (sp == (char*)Stack_high(stack)) return;
retaddr = *(uintnat*)sp;
sp += sizeof(value);
regs = gc_regs;
while(1) {
/* Find the descriptor corresponding to the return address */
h = Hash_retaddr(retaddr, fds.mask);
while(1) {
d = fds.descriptors[h];
if (d->retaddr == retaddr) break;
h = (h+1) & fds.mask;
}
if (d->frame_size != 0xFFFF) {
/* Scan the roots in this frame */
for (p = d->live_ofs, n = d->num_live; n > 0; n--, p++) {
ofs = *p;
if (ofs & 1) {
root = regs + (ofs >> 1);
} else {
root = (value *)(sp + ofs);
}
f (fdata, *root, root);
}
/* Move to next frame */
sp += (d->frame_size & 0xFFFC);
retaddr = Saved_return_address(sp);
/* XXX KC: disabled already scanned optimization. */
} else {
/* This marks the top of an ML stack chunk. Move sp to the previous stack
* chunk. This includes skipping over the trap frame (2 words). */
sp += 2 * sizeof(value); /* trap frame */
regs = *(value**)sp;
sp += 2 * sizeof(value); /* DWARF and gc_regs */
goto next_chunk;
}
}
}
void caml_scan_stack(scanning_action f, void* fdata, struct stack_info* stack, value* gc_regs)
{
while (stack != NULL) {
scan_stack_frames(f, fdata, stack, gc_regs);
f(fdata, Stack_handle_value(stack), &Stack_handle_value(stack));
f(fdata, Stack_handle_exception(stack), &Stack_handle_exception(stack));
f(fdata, Stack_handle_effect(stack), &Stack_handle_effect(stack));
stack = Stack_parent(stack);
}
}
void caml_maybe_expand_stack ()
{
struct stack_info* stk = Caml_state->current_stack;
uintnat stack_available =
(value*)stk->sp - Stack_base(stk);
uintnat stack_needed =
Stack_threshold / sizeof(value)
+ 8 /* for words pushed by caml_start_program */;
if (stack_available < stack_needed)
if (!caml_try_realloc_stack (stack_needed))
caml_raise_stack_overflow();
if (Caml_state->gc_regs_buckets == NULL) {
/* ensure there is at least one gc_regs bucket available before
running any OCaml code */
value* bucket = caml_stat_alloc(sizeof(value) * Wosize_gc_regs);
bucket[0] = 0; /* no next bucket */
Caml_state->gc_regs_buckets = bucket;
}
}
#else /* End NATIVE_CODE, begin BYTE_CODE */
caml_root caml_global_data;
CAMLprim value caml_alloc_stack(value hval, value hexn, value heff)
{
struct stack_info* stack = alloc_stack_noexc(caml_fiber_wsz, hval, hexn, heff);
value* sp;
if (!stack) caml_raise_out_of_memory();
sp = Stack_high(stack);
// ?
sp -= 1;
sp[0] = Val_long(1); /* trapsp ?? */
stack->sp = sp;
return Val_ptr(stack);
}
CAMLprim value caml_ensure_stack_capacity(value required_space)
{
asize_t req = Long_val(required_space);
if (Caml_state->current_stack->sp - req < Stack_base(Caml_state->current_stack))
if (!caml_try_realloc_stack(req))
caml_raise_stack_overflow();
return Val_unit;
}
void caml_change_max_stack_size (uintnat new_max_size)
{
asize_t size = Stack_high(Caml_state->current_stack) - Caml_state->current_stack->sp
+ Stack_threshold / sizeof (value);
if (new_max_size < size) new_max_size = size;
if (new_max_size != caml_max_stack_size){
caml_gc_log ("Changing stack limit to %luk bytes",
new_max_size * sizeof (value) / 1024);
}
caml_max_stack_size = new_max_size;
}
/*
Root scanning.
Used by the GC to find roots on the stacks of running or runnable fibers.
*/
void caml_scan_stack(scanning_action f, void* fdata, struct stack_info* stack, value* v_gc_regs)
{
value *low, *high, *sp;
while (stack != NULL) {
Assert(stack->magic == 42);
high = Stack_high(stack);
low = stack->sp;
for (sp = low; sp < high; sp++) {
f(fdata, *sp, sp);
}
f(fdata, Stack_handle_value(stack), &Stack_handle_value(stack));
f(fdata, Stack_handle_exception(stack), &Stack_handle_exception(stack));
f(fdata, Stack_handle_effect(stack), &Stack_handle_effect(stack));
stack = Stack_parent(stack);
}
}
#endif /* end BYTE_CODE */
/*
Stack management.
Used by the interpreter to allocate stack space.
*/
#ifdef NATIVE_CODE
/* Update absolute exception pointers for new stack*/
static void rewrite_exception_stack(struct stack_info *old_stack, value** exn_ptr, struct stack_info *new_stack)
{
fiber_debug_log ("Old [%p, %p]", Stack_base(old_stack), Stack_high(old_stack));
fiber_debug_log ("New [%p, %p]", Stack_base(new_stack), Stack_high(new_stack));
if(exn_ptr) {
fiber_debug_log ("*exn_ptr=%p", *exn_ptr);
while (Stack_base(old_stack) < *exn_ptr && *exn_ptr <= Stack_high(old_stack)) {
#ifdef DEBUG
value* old_val = *exn_ptr;
#endif
*exn_ptr = Stack_high(new_stack) - (Stack_high(old_stack) - *exn_ptr);
fiber_debug_log ("Rewriting %p to %p", old_val, *exn_ptr);
CAMLassert(Stack_base(new_stack) < *exn_ptr);
CAMLassert((value*)*exn_ptr <= Stack_high(new_stack));
exn_ptr = (value**)*exn_ptr;
}
fiber_debug_log ("finished with *exn_ptr=%p", *exn_ptr);
} else {
fiber_debug_log ("exn_ptr is null");
}
}
#endif
int caml_try_realloc_stack(asize_t required_space)
{
struct stack_info *old_stack, *new_stack;
asize_t size;
int stack_used;
CAMLnoalloc;
old_stack = Caml_state->current_stack;
stack_used = Stack_high(old_stack) - (value*)old_stack->sp;
#ifdef DEBUG
size = stack_used + stack_used / 16 + required_space;
if (size >= caml_max_stack_size) return 0;
#else
size = Stack_high(old_stack) - Stack_base(old_stack);
do {
if (size >= caml_max_stack_size) return 0;
size *= 2;
} while (size < stack_used + required_space);
#endif
if (size > 4096 / sizeof(value)) {
caml_gc_log ("Growing stack to %"
ARCH_INTNAT_PRINTF_FORMAT "uk bytes",
(uintnat) size * sizeof(value) / 1024);
} else {
caml_gc_log ("Growing stack to %"
ARCH_INTNAT_PRINTF_FORMAT "u bytes",
(uintnat) size * sizeof(value));
}
new_stack = alloc_stack_noexc(size,
Stack_handle_value(old_stack),
Stack_handle_exception(old_stack),
Stack_handle_effect(old_stack));
if (!new_stack) return 0;
memcpy(Stack_high(new_stack) - stack_used,
Stack_high(old_stack) - stack_used,
stack_used * sizeof(value));
new_stack->sp = Stack_high(new_stack) - stack_used;
Stack_parent(new_stack) = Stack_parent(old_stack);
#ifdef NATIVE_CODE
rewrite_exception_stack(old_stack, (value**)&Caml_state->exn_handler, new_stack);
#endif
/* Update stack pointers in Caml_state->c_stack */
{
struct c_stack_link* link;
for (link = Caml_state->c_stack; link; link = link->prev) {
#ifdef DEBUG
struct stack_info* cb = NULL;
/* Verify that all callback frames on this C stack belong to the
same fiber */
if (link->stack == NULL) {
/* only possible for the first link, if it has not done any C calls yet */
/* FIXME: at the moment, this is possible due to a couple of odd ways of entering C code
(via GC, etc.) */
//CAMLassert(link == Caml_state->c_stack);
} else if (cb == NULL) {
/* first non-NULL stack */
cb = link->stack;
} else {
CAMLassert(link->stack == cb);
}
#endif
if (link->stack == old_stack) {
link->stack = new_stack;
link->sp = (void*)((char*)Stack_high(new_stack) - ((char*)Stack_high(old_stack) - (char*)link->sp));
}
}
}
caml_free_stack(old_stack);
Caml_state->current_stack = new_stack;
return 1;
}
struct stack_info* caml_alloc_main_stack (uintnat init_size)
{
struct stack_info* stk =
alloc_stack_noexc(init_size, Val_unit, Val_unit, Val_unit);
return stk;
}
void caml_free_stack (struct stack_info* stack)
{
CAMLnoalloc;
CAMLassert(stack->magic == 42);
#ifdef DEBUG
memset(stack, 0x42, (char*)stack->handler - (char*)stack);
#endif
if (stack->size_class != -1) {
stack->handler->parent = Caml_state->stack_cache[stack->size_class];
Caml_state->stack_cache[stack->size_class] = stack;
} else {
caml_stat_free(stack);
}
}
CAMLprim value caml_clone_continuation (value cont)
{
CAMLparam1(cont);
CAMLlocal1(new_cont);
intnat stack_used;
struct stack_info *source, *orig_source, *target, *ret_stack;
struct stack_info **link = &ret_stack;
new_cont = caml_alloc_1(Cont_tag, Val_ptr(NULL));
orig_source = source = Ptr_val(caml_continuation_use(cont));
do {
CAMLnoalloc;
stack_used = Stack_high(source) - (value*)source->sp;
target = alloc_stack_noexc(Stack_high(source) - Stack_base(source),
Stack_handle_value(source),
Stack_handle_exception(source),
Stack_handle_effect(source));
if (!target) caml_raise_out_of_memory();
memcpy(Stack_high(target) - stack_used, Stack_high(source) - stack_used,
stack_used * sizeof(value));
#ifdef NATIVE_CODE
{
/* rewrite exception pointer in the caml context on the new stack */
target->exception_ptr = source->exception_ptr;
rewrite_exception_stack(source, (value**)&target->exception_ptr, target);
}
#endif
target->sp = Stack_high(target) - stack_used;
*link = target;
link = &Stack_parent(target);
source = Stack_parent(source);
} while (source != NULL);
caml_continuation_replace(cont, orig_source);
caml_continuation_replace(new_cont, ret_stack);
CAMLreturn (new_cont);
}
CAMLprim value caml_continuation_use_noexc (value cont)
{
value v;
value null_stk = Val_ptr(NULL);
fiber_debug_log("cont: is_block(%d) tag_val(%ul) is_minor(%d)", Is_block(cont), Tag_val(cont), Is_minor(cont));
CAMLassert(Is_block(cont) && Tag_val(cont) == Cont_tag);
/* this forms a barrier between execution and any other domains
that might be marking this continuation */
if (!Is_minor(cont) ) caml_darken_cont(cont);
/* at this stage the stack is assured to be marked */
v = Op_val(cont)[0];
if (caml_domain_alone()) {
Field(cont, 0) = null_stk;
return v;
}
if (atomic_compare_exchange_strong(Op_atomic_val(cont), &v, null_stk)) {
return v;
} else {
return null_stk;
}
}
CAMLprim value caml_continuation_use (value cont)
{
value v = caml_continuation_use_noexc(cont);
if (v == Val_ptr(NULL))
caml_raise_continuation_already_taken();
return v;
}
void caml_continuation_replace(value cont, struct stack_info* stk)
{
value n = Val_ptr(NULL);
int b = atomic_compare_exchange_strong(Op_atomic_val(cont), &n, Val_ptr(stk));
CAMLassert(b);
(void)b; /* squash unused warning */
}