skip to content
==> src/hello.c <==
1/* hello for the wasm executor (alpha-rust-mesh programs/hello/wasm): op 0 = arg * 3 + 7.
2 * View: 0x0 magic "HELO" (set by init), 0x4 last arg, 0x8 last result. Counters: 0 tasks, 1 inits.
3 *
4 * wasm-only test hooks (kcheck W*, lifecheck; the GPU images answer R_BAD_OP): op 8 `arg` u32 loads
5 * of the view, one mesh::ld32 call each (the per-access cost); op 9 `arg` bulk reads of the whole
6 * 4 KiB view into linear memory, one mesh::read call each; op 10 traps (unreachable); op 11 spins
7 * until meshd interrupts it (task_ms); op 12 grows linear memory by `arg` MiB (the old size in
8 * pages, or u32::MAX past mem_mb). */
9#include "mesh/rt_wasm.h"
10
11#define MAGIC 0x4f4c4548u /* "HELO" */
12
13static void init(const mesh_ctx *ctx) {
14    mesh_ctx_set_u32(ctx, 0x0, MAGIC);
15    mesh_ctx_count(ctx, 1, 1);
16}
17
18static uint8_t page[4096];
19
20static uint32_t task(const mesh_ctx *ctx, uint32_t op, uint32_t arg) {
21    switch (op) {
22    case MESH_OP_TASK: {
23        uint32_t r = arg * 3u + 7u;
24        mesh_ctx_set_u32(ctx, 0x4, arg);
25        mesh_ctx_set_u32(ctx, 0x8, r);
26        mesh_ctx_count(ctx, 0, 1);
27        return r;
28    }
29    case 8: {
30        mesh_view v = mesh_ctx_view(ctx);
31        uint32_t x = 0;
32        for (uint32_t i = 0; i < arg; i++) x += mesh_view_u32(v, (i % 1024) * 4);
33        return x;
34    }
35    case 9: {
36        mesh_view v = mesh_ctx_view(ctx);
37        uint32_t x = 0;
38        for (uint32_t i = 0; i < arg; i++) {
39            mesh_view_read(v, 0, page, sizeof page);
40            x ^= page[0];
41        }
42        return x;
43    }
44    case 10: MESH_TRAP();
45    case 11:
46        for (;;) {
47            /* spin until interrupted */
48            __asm__ volatile("" ::: "memory");
49        }
50    case 12: return (uint32_t)__builtin_wasm_memory_grow(0, (unsigned long)arg * 16);
51    default: return MESH_R_BAD_OP;
52    }
53}
54
55MESH_PROGRAM(init, task)