forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiber.c
559 lines (475 loc) · 16.4 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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/**************************************************************************/
/* */
/* OCaml */
/* */
/* KC Sivaramakrishnan, Indian Institute of Technology, Madras */
/* Tom Kelly, OCaml Labs Consultancy */
/* Stephen Dolan, University of Cambridge */
/* */
/* Copyright 2021 Indian Institute of Technology, Madras */
/* Copyright 2021 OCaml Labs Consultancy */
/* Copyright 2019 University of Cambridge */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
#include <string.h>
#include <unistd.h>
#include "caml/alloc.h"
#include "caml/codefrag.h"
#include "caml/fail.h"
#include "caml/fiber.h"
#include "caml/gc_ctrl.h"
#include "caml/platform.h"
#include "caml/minor_gc.h"
#include "caml/misc.h"
#include "caml/major_gc.h"
#include "caml/memory.h"
#include "caml/startup_aux.h"
#ifdef NATIVE_CODE
#include "caml/stack.h"
#include "caml/frame_descriptors.h"
#endif
#ifdef USE_MMAP_MAP_STACK
#include <sys/mman.h>
#endif
#ifdef DEBUG
#define fiber_debug_log(...) caml_gc_log(__VA_ARGS__)
#else
#define fiber_debug_log(...)
#endif
void caml_change_max_stack_size (uintnat new_max_size)
{
struct stack_info *current_stack = Caml_state->current_stack;
asize_t size = Stack_high(current_stack) - (value*)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 %"
ARCH_INTNAT_PRINTF_FORMAT "uk bytes",
new_max_size * sizeof (value) / 1024);
}
caml_max_stack_size = new_max_size;
}
#define NUM_STACK_SIZE_CLASSES 5
struct stack_info** caml_alloc_stack_cache (void)
{
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;
}
Caml_inline struct stack_info* alloc_for_stack (mlsize_t wosize)
{
size_t len = sizeof(struct stack_info) +
sizeof(value) * wosize +
8 /* for alignment to 16-bytes, needed for arm64 */ +
sizeof(struct stack_handler);
#ifdef USE_MMAP_MAP_STACK
struct stack_info* si;
si = mmap(NULL, len, PROT_WRITE | PROT_READ,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_STACK, -1, 0);
if (si == MAP_FAILED)
return NULL;
si->size = len;
return si;
#else
return caml_stat_alloc_noexc(len);
#endif /* USE_MMAP_MAP_STACK */
}
Caml_inline struct stack_info** stack_cache_bucket (mlsize_t wosize) {
mlsize_t size_bucket_wsz = caml_fiber_wsz;
struct stack_info** size_bucket = Caml_state->stack_cache;
struct stack_info** end = size_bucket + NUM_STACK_SIZE_CLASSES;
/* wosize is in stack cache bucket n iff wosize == caml_fiber_wsz * 2**n */
while (size_bucket < end) {
if (wosize == size_bucket_wsz)
return size_bucket;
++size_bucket;
size_bucket_wsz += size_bucket_wsz;
}
return NULL;
}
static struct stack_info*
alloc_size_class_stack_noexc(mlsize_t wosize, struct stack_info** size_bucket,
value hval, value hexn, value heff)
{
struct stack_info* stack;
struct stack_handler* hand;
CAML_STATIC_ASSERT(sizeof(struct stack_info) % sizeof(value) == 0);
CAML_STATIC_ASSERT(sizeof(struct stack_handler) % sizeof(value) == 0);
if (size_bucket != NULL && *size_bucket != NULL) {
stack = *size_bucket;
*size_bucket = (struct stack_info*)stack->exception_ptr;
CAMLassert(stack->size_bucket == stack_cache_bucket(wosize));
hand = stack->handler;
} else {
/* couldn't get a cached stack, so have to create one */
stack = alloc_for_stack(wosize);
if (stack == NULL) {
return NULL;
}
stack->size_bucket = size_bucket;
/* 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));
stack->handler = hand;
}
hand->handle_value = hval;
hand->handle_exn = hexn;
hand->handle_effect = heff;
hand->parent = NULL;
stack->sp = (value*)hand;
stack->exception_ptr = NULL;
#ifdef DEBUG
stack->magic = 42;
#endif
CAMLassert(Stack_high(stack) - Stack_base(stack) == wosize ||
Stack_high(stack) - Stack_base(stack) == wosize + 1);
return stack;
}
/* 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** size_bucket = stack_cache_bucket (wosize);
return alloc_size_class_stack_noexc(wosize, size_bucket, hval, hexn, heff);
}
#ifdef NATIVE_CODE
value caml_alloc_stack (value hval, value hexn, value heff) {
struct stack_info* stack =
alloc_size_class_stack_noexc(caml_fiber_wsz, Caml_state->stack_cache,
hval, hexn, heff);
if (!stack) caml_raise_out_of_memory();
fiber_debug_log ("Allocate stack=%p of %" ARCH_INTNAT_PRINTF_FORMAT
"u 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;
Pop_frame_pointer(p);
*pc = *(uintnat*)p; /* ret addr */
*sp = p + sizeof(value);
}
Caml_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;
regs = gc_regs;
next_chunk:
if (sp == (char*)Stack_high(stack)) return;
Pop_frame_pointer(sp);
retaddr = *(uintnat*)sp;
sp += sizeof(value);
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. */
sp += 3 * sizeof(value); /* trap frame & DWARF pointer */
regs = *(value**)sp; /* update gc_regs */
sp += 1 * sizeof(value); /* 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 (void)
{
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 */
value caml_global_data;
CAMLprim value caml_alloc_stack(value hval, value hexn, value heff)
{
value* sp;
struct stack_info* stack =
alloc_size_class_stack_noexc(caml_fiber_wsz, Caml_state->stack_cache,
hval, hexn, heff);
if (!stack) caml_raise_out_of_memory();
sp = Stack_high(stack);
sp -= 1;
sp[0] = Val_long(1);
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;
}
/*
Root scanning.
Used by the GC to find roots on the stacks of running or runnable fibers.
*/
Caml_inline int is_block_and_not_code_frag(value v) {
return Is_block(v) && caml_find_code_fragment_by_pc((char *) v) == NULL;
}
void caml_scan_stack(scanning_action f, void* fdata,
struct stack_info* stack, value* v_gc_regs)
{
value *low, *high, *sp;
while (stack != NULL) {
CAMLassert(stack->magic == 42);
high = Stack_high(stack);
low = stack->sp;
for (sp = low; sp < high; sp++) {
/* Code pointers inside the stack are naked pointers.
We must avoid passing them to function [f]. */
value v = *sp;
if (is_block_and_not_code_frag(v)) {
f(fdata, v, sp);
}
}
if (is_block_and_not_code_frag(Stack_handle_value(stack)))
f(fdata, Stack_handle_value(stack), &Stack_handle_value(stack));
if (is_block_and_not_code_frag(Stack_handle_exception(stack)))
f(fdata, Stack_handle_exception(stack), &Stack_handle_exception(stack));
if (is_block_and_not_code_frag(Stack_handle_effect(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;
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);
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. It is possible to have
* multiple c_stack_links to point to the same stack since callbacks are run
* on existing stacks. */
{
struct c_stack_link* link;
for (link = Caml_state->c_stack; link; link = link->prev) {
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);
if (stack->size_bucket != NULL) {
stack->exception_ptr = (void*)(*stack->size_bucket);
*stack->size_bucket = stack;
#ifdef DEBUG
memset(Stack_base(stack), 0x42,
(Stack_high(stack)-Stack_base(stack))*sizeof(value));
#endif
} else {
#ifdef DEBUG
memset(stack, 0x42, (char*)stack->handler - (char*)stack);
#endif
#ifdef USE_MMAP_MAP_STACK
munmap(stack, stack->size);
#else
caml_stat_free(stack);
#endif
}
}
CAMLprim value caml_continuation_use_noexc (value cont)
{
value v;
value null_stk = Val_ptr(NULL);
CAMLnoalloc;
fiber_debug_log("cont: is_block(%d) tag_val(%ul) is_young(%d)",
Is_block(cont), Tag_val(cont), Is_young(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_young(cont) ) caml_darken_cont(cont);
/* at this stage the stack is assured to be marked */
v = Field(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;
}
CAMLprim value caml_continuation_use_and_update_handler_noexc
(value cont, value hval, value hexn, value heff)
{
value stack;
struct stack_info* stk;
stack = caml_continuation_use_noexc (cont);
stk = Ptr_val(stack);
if (stk == NULL) {
/* The continuation has already been taken */
return stack;
}
while (Stack_parent(stk) != NULL) stk = Stack_parent(stk);
Stack_handle_value(stk) = hval;
Stack_handle_exception(stk) = hexn;
Stack_handle_effect(stk) = heff;
return stack;
}
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 */
}
CAMLprim value caml_drop_continuation (value cont)
{
struct stack_info* stk = Ptr_val(caml_continuation_use(cont));
caml_free_stack(stk);
return Val_unit;
}