aboutsummaryrefslogtreecommitdiff
path: root/src/rt/rust_upcall.cpp
blob: e4604049c4dfc1e91d43f09ca1d85c2ee2a966f2 (plain) (blame)
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
560
561
562
563
#include "rust_internal.h"

// Upcalls.

#ifdef __GNUC__
#define LOG_UPCALL_ENTRY(task)                              \
    (task)->dom->get_log().reset_indent(0);                 \
    (task)->log(rust_log::UPCALL,                           \
                "> UPCALL %s - task: 0x%" PRIxPTR           \
                " retpc: x%" PRIxPTR,                       \
                __FUNCTION__,                               \
                (task), __builtin_return_address(0));       \
    (task)->dom->get_log().indent();
#else
#define LOG_UPCALL_ENTRY(task)                              \
    (task)->dom->get_log().reset_indent(0);                 \
    (task)->log(rust_log::UPCALL,                           \
                "> UPCALL task: x%" PRIxPTR (task));        \
    (task)->dom->get_log().indent();
#endif

extern "C" CDECL char const *str_buf(rust_task *task, rust_str *s);

inline bool
requires_message_passing(rust_task *sender, rust_task *receiver) {
    return sender->dom != receiver->dom;
}

extern "C" void upcall_grow_task(rust_task *task, size_t n_frame_bytes) {
    LOG_UPCALL_ENTRY(task);
    task->grow(n_frame_bytes);
}

extern "C" CDECL void upcall_log_int(rust_task *task, int32_t i) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::ULOG,
              "upcall log_int(0x%" PRIx32 " = %" PRId32 " = '%c')", i, i,
              (char) i);
}

extern "C" CDECL void upcall_log_str(rust_task *task, rust_str *str) {
    LOG_UPCALL_ENTRY(task);
    const char *c = str_buf(task, str);
    task->log(rust_log::UPCALL | rust_log::ULOG, "upcall log_str(\"%s\")", c);
}

extern "C" CDECL void upcall_trace_word(rust_task *task, uintptr_t i) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::TRACE, "trace: 0x%" PRIxPTR "", i,
              i, (char) i);
}

extern "C" CDECL void upcall_trace_str(rust_task *task, char const *c) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::TRACE, "trace: %s", c);
}

extern "C" CDECL rust_port*
upcall_new_port(rust_task *task, size_t unit_sz) {
    LOG_UPCALL_ENTRY(task);
    rust_dom *dom = task->dom;
    task->log(rust_log::UPCALL | rust_log::MEM | rust_log::COMM,
              "upcall_new_port(task=0x%" PRIxPTR ", unit_sz=%d)",
              (uintptr_t) task, unit_sz);
    return new (dom) rust_port(task, unit_sz);
}

extern "C" CDECL void upcall_del_port(rust_task *task, rust_port *port) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::MEM | rust_log::COMM,
              "upcall del_port(0x%" PRIxPTR ")", (uintptr_t) port);
    I(task->dom, !port->ref_count);
    delete port;
}

/**
 * Creates a new channel pointing to a given port.
 */
extern "C" CDECL rust_chan*
upcall_new_chan(rust_task *task, rust_port *port) {
    LOG_UPCALL_ENTRY(task);
    rust_dom *dom = task->dom;
    task->log(rust_log::UPCALL | rust_log::MEM | rust_log::COMM,
              "upcall_new_chan(task=0x%" PRIxPTR ", port=0x%" PRIxPTR ")",
              (uintptr_t) task, port);
    I(dom, port);
    return new (dom) rust_chan(task, port);
}

/**
 * Called whenever the channel's ref count drops to zero.
 */
extern "C" CDECL void upcall_del_chan(rust_task *task, rust_chan *chan) {
    LOG_UPCALL_ENTRY(task);
    rust_dom *dom = task->dom;
    task->log(rust_log::UPCALL | rust_log::MEM | rust_log::COMM,
              "upcall del_chan(0x%" PRIxPTR ")", (uintptr_t) chan);
    I(dom, !chan->ref_count);
    delete chan;
}

/**
 * Clones a channel and stores it in the spawnee's domain. Each spawned task
 * has it's own copy of the channel.
 */
extern "C" CDECL rust_chan *
upcall_clone_chan(rust_task *task,
                  maybe_proxy<rust_task> *spawnee_proxy,
                  rust_chan *chan) {
    LOG_UPCALL_ENTRY(task);
    rust_task *spawnee = spawnee_proxy->delegate();
    task->log(rust_log::UPCALL | rust_log::MEM | rust_log::COMM,
              "spawnee: 0x%" PRIxPTR ", chan: 0x%" PRIxPTR,
              (uintptr_t) spawnee, (uintptr_t) chan);
    return new (spawnee->dom) rust_chan(spawnee, chan->port);
}

extern "C" CDECL void upcall_yield(rust_task *task) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::COMM, "upcall yield()");
    task->yield(1);
}

extern "C" CDECL void
upcall_join(rust_task *task, maybe_proxy<rust_task> *proxy) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::COMM,
                  "join proxy 0x%" PRIxPTR " -> task = 0x%" PRIxPTR,
                  proxy, proxy->delegate());

    rust_task *other = proxy->delegate();

    // If the other task is already dying, we don't have to wait for it.
    if (!other->dead()) {
        other->waiting_tasks.push(&task->alarm);
        task->block(other);
        task->yield(2);
    }
}

/**
 * Sends an chunk of data along the specified channel.
 *
 * sptr: pointer to a chunk of data to send
 */
extern "C" CDECL void
upcall_send(rust_task *task, rust_chan *chan, void *sptr) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::COMM,
              "chan: 0x%" PRIxPTR ", sptr: 0x%" PRIxPTR ", size: %d",
              (uintptr_t) chan, (uintptr_t) sptr, chan->port->unit_sz);

    chan->buffer.enqueue(sptr);
    chan->transmit();
    task->log(rust_log::COMM, "=== WROTE DATA ===>");
}

extern "C" CDECL void
upcall_recv(rust_task *task, uintptr_t *dptr, rust_port *port) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::COMM,
              "port: 0x%" PRIxPTR ", dptr: 0x%" PRIxPTR
              ", size: 0x%" PRIxPTR ", chan_no: %d",
              (uintptr_t) port, (uintptr_t) dptr, port->unit_sz,
              port->chans.length());

    for (uint32_t i = 0; i < port->chans.length(); i++) {
        rust_chan *chan = port->chans[i];
        if (chan->buffer.is_empty() == false) {
            chan->buffer.dequeue(dptr);
            task->log(rust_log::COMM, "<=== READ DATA ===");
            return;
        }
    }

    // No data was buffered on any incoming channel, so block this task
    // on the port. Remember the rendezvous location so that any sender
    // task can write to it before waking up this task.

    task->rendezvous_ptr = dptr;
    task->block(port);
    task->yield(3);
}

extern "C" CDECL void upcall_fail(rust_task *task, char const *expr,
    char const *file, size_t line) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::ERR,
              "upcall fail '%s', %s:%" PRIdPTR, expr, file, line);
    task->fail(4);
}

/**
 * Called whenever a task's ref count drops to zero.
 */
extern "C" CDECL void
upcall_kill(rust_task *task, maybe_proxy<rust_task> *target_proxy) {
    LOG_UPCALL_ENTRY(task);
    rust_task *target_task = target_proxy->delegate();
    if (target_proxy != target_task) {
        task->dom->free(target_proxy);
    }
    task->log(rust_log::UPCALL | rust_log::TASK,
              "kill task 0x%" PRIxPTR ", ref count %d",
              target_task,
              target_task->ref_count);

    if (requires_message_passing(task, target_task)) {
        rust_dom *target_domain = target_task->dom;
        target_domain->send_message(
                new (target_domain)
                kill_task_message(target_domain, target_task));
    } else {
        target_task->kill();
    }
}

/**
 * Called by the exit glue when the task terminates.
 */
extern "C" CDECL void
upcall_exit(rust_task *task) {
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::TASK,
              "task ref_count: %d", task->ref_count);
    task->die();
    task->notify_waiting_tasks();
    task->yield(1);
}

extern "C" CDECL uintptr_t
upcall_malloc(rust_task *task, size_t nbytes, type_desc *td)
{
    LOG_UPCALL_ENTRY(task);

    task->dom->log(rust_log::UPCALL|rust_log::MEM,
                   "upcall malloc(%" PRIdPTR ", 0x%" PRIxPTR ")"
                   " with gc-chain head = 0x%" PRIxPTR,
                   nbytes, td, task->gc_alloc_chain);
    void *p = task->malloc(nbytes, td);
    task->dom->log(rust_log::UPCALL|rust_log::MEM,
                   "upcall malloc(%" PRIdPTR ", 0x%" PRIxPTR
                   ") = 0x%" PRIxPTR
                   " with gc-chain head = 0x%" PRIxPTR,
                   nbytes, td, (uintptr_t)p, task->gc_alloc_chain);
    return (uintptr_t) p;
}

/**
 * Called whenever an object's ref count drops to zero.
 */
extern "C" CDECL void
upcall_free(rust_task *task, void* ptr, uintptr_t is_gc)
{
    LOG_UPCALL_ENTRY(task);
    rust_dom *dom = task->dom;
    dom->log(rust_log::UPCALL|rust_log::MEM,
             "upcall free(0x%" PRIxPTR ")",
             (uintptr_t)ptr);
    task->free(ptr, (bool) is_gc);
}

extern "C" CDECL uintptr_t
upcall_mark(rust_task *task, void* ptr)
{
    LOG_UPCALL_ENTRY(task);

    rust_dom *dom = task->dom;
    if (ptr) {
        gc_alloc *gcm = (gc_alloc*) (((char*)ptr) - sizeof(gc_alloc));
        uintptr_t marked = (uintptr_t) gcm->mark();
        dom->log(rust_log::UPCALL|rust_log::MEM|rust_log::GC,
                 "upcall mark(0x%" PRIxPTR ") = %" PRIdPTR,
                 (uintptr_t)gcm, marked);
        return marked;
    }
    return 0;
}

extern "C" CDECL rust_str *
upcall_new_str(rust_task *task, char const *s, size_t fill) {
    LOG_UPCALL_ENTRY(task);
    rust_dom *dom = task->dom;
    size_t alloc = next_power_of_two(sizeof(rust_str) + fill);
    void *mem = dom->malloc(alloc);
    if (!mem) {
        task->fail(3);
        return NULL;
    }
    rust_str *st = new (mem) rust_str(dom, alloc, fill, (uint8_t const *) s);
    task->log(rust_log::UPCALL | rust_log::MEM,
              "upcall new_str('%s', %" PRIdPTR ") = 0x%" PRIxPTR,
              s, fill, st);
    return st;
}

extern "C" CDECL rust_vec *
upcall_new_vec(rust_task *task, size_t fill, type_desc *td)
{
    LOG_UPCALL_ENTRY(task);
    rust_dom *dom = task->dom;
    dom->log(rust_log::UPCALL|rust_log::MEM,
             "upcall new_vec(%" PRIdPTR ")",
             fill);
    size_t alloc = next_power_of_two(sizeof(rust_vec) + fill);
    void *mem = task->malloc(alloc, td);
    if (!mem) {
        task->fail(3);
        return NULL;
    }
    rust_vec *v = new (mem) rust_vec(dom, alloc, 0, NULL);
    task->log(rust_log::UPCALL | rust_log::MEM,
              "upcall new_vec(%" PRIdPTR ") = 0x%" PRIxPTR, fill, v);
    return v;
}

extern "C" CDECL rust_str *
upcall_vec_grow(rust_task *task, rust_vec *v, size_t n_bytes, uintptr_t is_gc)
{
    LOG_UPCALL_ENTRY(task);
    rust_dom *dom = task->dom;
    task->log(rust_log::UPCALL|rust_log::MEM,
             "upcall vec_grow(%" PRIxPTR ", %" PRIdPTR
             "), alloc=%" PRIdPTR ", fill=%" PRIdPTR,
             v, n_bytes, v->alloc, v->fill);
    size_t alloc = next_power_of_two(sizeof(rust_vec) + v->fill + n_bytes);
    if (v->ref_count == 1) {

        // Fastest path: already large enough.
        if (v->alloc >= alloc) {
            task->log(rust_log::UPCALL | rust_log::MEM, "no-growth path");
            return v;
        }

        // Second-fastest path: can at least realloc.
        task->log(rust_log::UPCALL | rust_log::MEM, "realloc path");
        v = (rust_vec*) dom->realloc(v, alloc);
        if (!v) {
            task->fail(4);
            return NULL;
        }
        v->alloc = alloc;

    } else {
        // Slowest path: make a new vec.
        task->log(rust_log::UPCALL | rust_log::MEM, "new vec path");
        void *mem = dom->malloc(alloc);
        if (!mem) {
            task->fail(4);
            return NULL;
        }
        v->deref();
        v = new (mem) rust_vec(dom, alloc, v->fill, &v->data[0]);
    }
    I(dom, sizeof(rust_vec) + v->fill <= v->alloc);
    return v;
}

static rust_crate_cache::c_sym *
fetch_c_sym(rust_task *task, rust_crate const *curr_crate, size_t lib_num,
    size_t c_sym_num, char const *library, char const *symbol) {
    rust_crate_cache *cache = task->get_crate_cache(curr_crate);
    rust_crate_cache::lib *l = cache->get_lib(lib_num, library);
    return cache->get_c_sym(c_sym_num, l, symbol);
}

extern "C" CDECL uintptr_t upcall_require_rust_sym(rust_task *task,
    rust_crate const *curr_crate, size_t lib_num, // # of lib
    size_t c_sym_num, // # of C sym "rust_crate" in lib
    size_t rust_sym_num, // # of rust sym
    char const *library, char const **path) {
    LOG_UPCALL_ENTRY(task);
    rust_dom *dom = task->dom;

    task->log(rust_log::UPCALL | rust_log::CACHE,
              "upcall require rust sym: lib #%" PRIdPTR
              " = %s, c_sym #%" PRIdPTR
              ", rust_sym #%" PRIdPTR
              ", curr_crate = 0x%" PRIxPTR, lib_num, library, c_sym_num,
              rust_sym_num, curr_crate);
    for (char const **c = crate_rel(curr_crate, path); *c; ++c) {
        task->log(rust_log::UPCALL, " + %s", crate_rel(curr_crate, *c));
    }

    task->log(rust_log::UPCALL | rust_log::CACHE,
              "require C symbol 'rust_crate' from lib #%" PRIdPTR, lib_num);
    rust_crate_cache::c_sym *c =
            fetch_c_sym(task, curr_crate, lib_num, c_sym_num, library,
                        "rust_crate");

    task->log(rust_log::UPCALL | rust_log::CACHE,
              "require rust symbol inside crate");
    rust_crate_cache::rust_sym *s = task->cache->get_rust_sym(rust_sym_num,
                                                              dom,
                                                              curr_crate, c,
                                                              path);

    uintptr_t addr = s->get_val();
    if (addr) {
        task->log(rust_log::UPCALL | rust_log::CACHE,
                  "found-or-cached addr: 0x%" PRIxPTR, addr);
    } else {
        task->log(rust_log::UPCALL | rust_log::CACHE,
                  "failed to resolve symbol");
        task->fail(7);
    }
    return addr;
}

extern "C" CDECL uintptr_t upcall_require_c_sym(rust_task *task,
    rust_crate const *curr_crate, size_t lib_num, // # of lib
    size_t c_sym_num, // # of C sym
    char const *library, char const *symbol) {
    LOG_UPCALL_ENTRY(task);

    task->log(rust_log::UPCALL | rust_log::CACHE,
              "upcall require c sym: lib #%" PRIdPTR
              " = %s, c_sym #%" PRIdPTR
              " = %s"
              ", curr_crate = 0x%" PRIxPTR, lib_num, library, c_sym_num,
              symbol, curr_crate);

    rust_crate_cache::c_sym *c = fetch_c_sym(task, curr_crate, lib_num,
                                             c_sym_num, library, symbol);

    uintptr_t addr = c->get_val();
    if (addr) {
        task->log(rust_log::UPCALL | rust_log::CACHE,
                  "found-or-cached addr: 0x%" PRIxPTR, addr);
    } else {
        task->log(rust_log::UPCALL | rust_log::CACHE,
                  "failed to resolve symbol");
        task->fail(6);
    }
    return addr;
}

extern "C" CDECL type_desc *
upcall_get_type_desc(rust_task *task,
                     rust_crate const *curr_crate,
                     size_t size,
                     size_t align,
                     size_t n_descs,
                     type_desc const **descs)
{
    LOG_UPCALL_ENTRY(task);
    task->log(rust_log::UPCALL | rust_log::CACHE,
              "upcall get_type_desc with size=%" PRIdPTR
              ", align=%" PRIdPTR ", %" PRIdPTR " descs", size, align,
              n_descs);
    rust_crate_cache *cache = task->get_crate_cache(curr_crate);
    type_desc *td = cache->get_type_desc(size, align, n_descs, descs);
    task->log(rust_log::UPCALL | rust_log::CACHE,
              "returning tydesc 0x%" PRIxPTR, td);
    return td;
}

#if defined(__WIN32__)
static DWORD WINAPI rust_thread_start(void *ptr)
#elif defined(__GNUC__)
static void *rust_thread_start(void *ptr)
#else
#error "Platform not supported"
#endif
{
    // We were handed the domain we are supposed to run.
    rust_dom *dom = (rust_dom *) ptr;

    // Start a new rust main loop for this thread.
    dom->start_main_loop();

    rust_srv *srv = dom->srv;
    delete dom;
    delete srv;

    return 0;
}

extern "C" CDECL rust_task *
upcall_new_task(rust_task *spawner) {
    LOG_UPCALL_ENTRY(spawner);

    rust_dom *dom = spawner->dom;
    rust_task *task = new (dom) rust_task(dom, spawner);
    dom->log(rust_log::UPCALL | rust_log::MEM | rust_log::TASK,
             "upcall new_task(spawner 0x%" PRIxPTR ") = 0x%" PRIxPTR,
             spawner, task);
    return task;
}

extern "C" CDECL rust_task *
upcall_start_task(rust_task *spawner, rust_task *task,
    uintptr_t exit_task_glue, uintptr_t spawnee_fn, size_t callsz) {
    LOG_UPCALL_ENTRY(spawner);

    rust_dom *dom = spawner->dom;
    dom->log(rust_log::UPCALL | rust_log::MEM | rust_log::TASK,
             "upcall start_task(task 0x%" PRIxPTR
             " exit_task_glue 0x%" PRIxPTR
             ", spawnee 0x%" PRIxPTR
             ", callsz %" PRIdPTR ")", task, exit_task_glue, spawnee_fn,
             callsz);
    task->start(exit_task_glue, spawnee_fn, spawner->rust_sp, callsz);
    return task;
}

extern "C" CDECL maybe_proxy<rust_task> *
upcall_new_thread(rust_task *task) {
    LOG_UPCALL_ENTRY(task);

    rust_dom *old_dom = task->dom;
    rust_dom *new_dom = new rust_dom(old_dom->srv->clone(),
                                     old_dom->root_crate);

    task->log(rust_log::UPCALL | rust_log::MEM,
              "upcall new_thread() = dom 0x%" PRIxPTR " task 0x%" PRIxPTR,
              new_dom, new_dom->root_task);
    rust_proxy<rust_task> *proxy =
        new (old_dom) rust_proxy<rust_task>(old_dom,
                                            new_dom->root_task, true);
    task->log(rust_log::UPCALL | rust_log::MEM,
              "new proxy = 0x%" PRIxPTR " -> task = 0x%" PRIxPTR,
              proxy, proxy->delegate());
    return proxy;
}

extern "C" CDECL maybe_proxy<rust_task> *
upcall_start_thread(rust_task *spawner,
                    maybe_proxy<rust_task> *root_task_proxy,
    uintptr_t exit_task_glue, uintptr_t spawnee_fn, size_t callsz) {
    LOG_UPCALL_ENTRY(spawner);

    rust_dom *dom = spawner->dom;
    rust_task *root_task = root_task_proxy->delegate();
    dom->log(rust_log::UPCALL | rust_log::MEM | rust_log::TASK,
             "upcall start_thread(exit_task_glue 0x%" PRIxPTR
             ", spawnee 0x%" PRIxPTR
             ", callsz %" PRIdPTR ")", exit_task_glue, spawnee_fn, callsz);
    root_task->start(exit_task_glue, spawnee_fn, spawner->rust_sp, callsz);

#if defined(__WIN32__)
    HANDLE thread;
    thread = CreateThread(NULL, 0, rust_thread_start, root_task->dom,
            0, NULL);
    dom->win32_require("CreateThread", thread != NULL);
#else
    pthread_t thread;
    pthread_create(&thread, &dom->attr, rust_thread_start,
                   (void *) root_task->dom);
#endif
    return root_task_proxy;
}

//
// Local Variables:
// mode: C++
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
//