Make emulator compatible with all image versions

ea44945194729394c41fa42dc596d035068a4cb43fbc5d2e919fb1a3ca7ae072
This allows us to use the same emulator binary to emulate all versions
of the radiance image file.
Alexis Sellier committed ago 1 parent f36eb5cf
Makefile +3 -2
12 12
           -Wmissing-field-initializers \
13 13
           -fno-common -fstack-protector-all \
14 14
           -mcmodel=medium \
15 15
           -march=native -flto
16 16
LDFLAGS := -fuse-ld=lld -Wl,-z,stack-size=33554432
17 -
EMULATOR_SRCS  := emulator.c host_posix.c jit.c riscv/debug.c io.c riscv.c \
17 +
EMULATOR_SRCS  := emulator.c abi_host.c abi_linux.c image_native.c image_rad0.c \
18 +
                 image_raw.c host_posix.c jit.c riscv/debug.c io.c riscv.c \
18 19
                 machine.c machine_cpu.c machine_csr.c machine_devices.c machine_fdt.c
19 20
20 21
default: bin/emulator
21 22
22 -
bin/emulator: $(EMULATOR_SRCS) jit.h muldiv.h color.h types.h io.h host.h riscv.h riscv/debug.h machine.h machine_internal.h
23 +
bin/emulator: $(EMULATOR_SRCS) jit.h muldiv.h color.h types.h io.h host.h hosted.h riscv.h riscv/debug.h machine.h machine_internal.h
23 24
	@echo "cc    emulator => $@"
24 25
	@mkdir -p bin
25 26
	@$(CC) $(CFLAGS) $(LDFLAGS) $(EMULATOR_SRCS) -o $@
26 27
27 28
install: bin/emulator
README +29 -5
43 43
  Execution starts at the code address. Code and read-only data occupy
44 44
  exactly their size. Read-write data is followed by its zero-filled tail.
45 45
  Each segment must lie inside the memory of the mode, and the file must be
46 46
  exactly 48 plus the three sizes. Nothing else is checked.
47 47
48 -
  The legacy format of older compilers, "RAD0" version 1, has section sizes
49 -
  and fixed load addresses (text, then read-only data at 0x00010000, then
50 -
  read-write data at 0x00FFFFF0).
48 +
  Images of older compilers also load in hosted mode, so that programs from
49 +
  every release run unchanged. The format is recognized by its magic bytes:
51 50
52 -
  Machine mode loads only the native format.
51 +
    "RAD0" version 1   Section sizes and fixed load addresses: read-only data
52 +
                       at 0x00010000, then text, then read-write data at
53 +
                       0x00FFFFF0. See image_rad0.c.
54 +
    no magic           The file is the text. Optional sidecar files
55 +
                       `<program>.ro.data` and `<program>.rw.data` hold the
56 +
                       data, at the same addresses. See image_raw.c.
57 +
58 +
  Machine mode loads only the native format. Each format is loaded by its own
59 +
  file, image_native.c, image_rad0.c and image_raw.c.
53 60
54 61
  A `<program>.debug` sidecar may be used for source locations.
55 62
56 63
57 64
OPTIONS
109 116
                                              0 exited, 1 step limit, 2 fault;
110 117
                                              code i32).
111 118
    0x40C  waitAny(out)                       Returns the handle that ended.
112 119
113 120
  Errors: -1 NotFound, -2 Exists, -3 Denied, -4 NotEmpty, -5 Invalid (also
114 -
  any other call number), -6 TooLarge, -7 Limit, -8 Io.
121 +
  any other call number from 0x400), -6 TooLarge, -7 Limit, -8 Io.
115 122
116 123
  `spawn` takes a 128-byte record with the image path, the arguments, the
117 124
  destinations of standard output and error, the mode (hosted or machine),
118 125
  the machine and memory sizes, and the step budget. The child runs this
119 126
  executable again with the matching options; its standard input is empty.
123 130
124 131
  The full specification is `.ai/decisions/2026-10-28-host-abi` in the
125 132
  Radiance repository. Everything that touches the host operating system is
126 133
  behind `host.h`; `host_posix.c` implements it for Linux and macOS.
127 134
135 +
LINUX SYSTEM CALLS
136 +
137 +
  Programs built before the host ABI, up to October 2026, call the emulator
138 +
  with Linux RV64 numbers. They run unchanged: numbers below 0x400 are Linux
139 +
  calls and numbers from 0x400 are host calls, in the same run. See
140 +
  abi_linux.c.
141 +
142 +
    56  openat(dirfd, path, flags)   Zero-terminated path; `dirfd` is ignored.
143 +
                                     Flags O_RDONLY, or O_WRONLY | O_CREAT |
144 +
                                     O_TRUNC. Returns a descriptor or -1.
145 +
    57  close(fd)                    Zero, or -1.
146 +
    63  read(fd, ptr, len)           Bytes read; zero on failure.
147 +
    64  write(fd, ptr, len)          Bytes written; zero on failure.
148 +
    93  exit(status)                 Does not return.
149 +
150 +
  Any other number returns -38 (ENOSYS).
151 +
128 152
MEMORY LAYOUT
129 153
130 154
    0x00010000                 Read-only data
131 155
    Program base               Program text (instructions)
132 156
    0x00FFFFF0                 Read-write data
abi_host.c added +379 -0
1 +
/* Host ABI of current programs, numbered from ABI_HOST_BASE (0x400).
2 +
 *
3 +
 * The call number is in a7, arguments in a0..a3 and the result in a0; a
4 +
 * negative result is a host-neutral `HOST_E_*` error code. Paths are a pointer
5 +
 * and a length. See README. */
6 +
#include <stdint.h>
7 +
#include <stdio.h>
8 +
#include <stdlib.h>
9 +
#include <string.h>
10 +
11 +
#include "host.h"
12 +
#include "hosted.h"
13 +
#include "types.h"
14 +
15 +
#define ECALL_EXIT     0x400
16 +
#define ECALL_READ     0x401
17 +
#define ECALL_WRITE    0x402
18 +
#define ECALL_OPEN     0x403
19 +
#define ECALL_CLOSE    0x404
20 +
#define ECALL_STAT     0x405
21 +
#define ECALL_MAKE_DIR 0x406
22 +
#define ECALL_REMOVE   0x407
23 +
#define ECALL_RENAME   0x408
24 +
#define ECALL_LIST_DIR 0x409
25 +
#define ECALL_SPAWN    0x40A
26 +
#define ECALL_WAIT     0x40B
27 +
#define ECALL_WAIT_ANY 0x40C
28 +
29 +
/* Result of a call that faulted the guest: a0 is left unchanged. */
30 +
#define ECALL_FAULTED INT64_MIN
31 +
32 +
/* Size of the guest records exchanged with the host. */
33 +
#define SLICE_SIZE   16
34 +
#define STAT_SIZE    16
35 +
#define OUTCOME_SIZE 8
36 +
#define SPAWN_SIZE   128
37 +
38 +
/* Spawn record values. */
39 +
#define SPAWN_MODE_MACHINE 1
40 +
#define SPAWN_OUT_FILE     2
41 +
#define SPAWN_FLAG_INHERIT 1
42 +
43 +
/* Copy the guest string of `len` bytes at `addr` into `out`, which holds
44 +
 * HOST_PATH_MAX bytes, and terminate it. Returns 0, `HOST_E_INVALID` for an
45 +
 * embedded zero byte or a path that is too long, or `ECALL_FAULTED` after
46 +
 * faulting the guest. */
47 +
static i64 guest_path(
48 +
    struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len, char *out
49 +
) {
50 +
    if (!guest_check(cpu, pc, call, addr, len))
51 +
        return ECALL_FAULTED;
52 +
    if (len >= HOST_PATH_MAX || memchr(&memory[addr], 0, len))
53 +
        return HOST_E_INVALID;
54 +
    memcpy(out, &memory[addr], len);
55 +
    out[len] = '\0';
56 +
    return 0;
57 +
}
58 +
59 +
/* Open a file and give it a guest descriptor. */
60 +
static i64 ecall_open(const char *path, u64 mode) {
61 +
    if (mode > 1)
62 +
        return HOST_E_INVALID;
63 +
    i64 host_fd = mode ? host_open_write(path) : host_open_read(path);
64 +
    if (host_fd < 0)
65 +
        return host_fd;
66 +
    int guest_fd = guest_fd_table_add((int)host_fd);
67 +
    if (guest_fd < 0) {
68 +
        host_close((int)host_fd);
69 +
        return HOST_E_LIMIT;
70 +
    }
71 +
    return guest_fd;
72 +
}
73 +
74 +
/* Close a guest descriptor. The standard streams cannot be closed. */
75 +
static i64 ecall_close(u64 guest_fd) {
76 +
    if (guest_fd < 3 || guest_fd >= MAX_OPEN_FILES)
77 +
        return HOST_E_INVALID;
78 +
    int host_fd = guest_fd_table_get((int)guest_fd);
79 +
    if (host_fd < 0)
80 +
        return HOST_E_INVALID;
81 +
    guest_fd_table_remove((int)guest_fd);
82 +
    return host_close(host_fd) < 0 ? HOST_E_IO : 0;
83 +
}
84 +
85 +
/* Read or write guest memory through a guest descriptor. */
86 +
static i64 ecall_transfer(
87 +
    struct cpu *cpu, u32 pc, bool write, u64 guest_fd, u64 addr, u64 len
88 +
) {
89 +
    if (!guest_check(cpu, pc, write ? "write" : "read", addr, len))
90 +
        return ECALL_FAULTED;
91 +
    int host_fd =
92 +
        guest_fd < MAX_OPEN_FILES ? guest_fd_table_get((int)guest_fd) : -1;
93 +
    if (host_fd < 0)
94 +
        return HOST_E_INVALID;
95 +
    if (len == 0)
96 +
        return 0;
97 +
    return write ? host_write(host_fd, &memory[addr], (u32)len)
98 +
                 : host_read(host_fd, &memory[addr], (u32)len);
99 +
}
100 +
101 +
/* Describe a path into a guest Stat record. */
102 +
static i64 ecall_stat(
103 +
    struct cpu *cpu, u32 pc, u64 path_addr, u64 path_len, u64 out
104 +
) {
105 +
    char path[HOST_PATH_MAX];
106 +
    i64  rc = guest_path(cpu, pc, "stat", path_addr, path_len, path);
107 +
    if (rc)
108 +
        return rc;
109 +
    if (!guest_check(cpu, pc, "stat", out, STAT_SIZE))
110 +
        return ECALL_FAULTED;
111 +
    struct host_stat st;
112 +
    rc = host_stat(path, &st);
113 +
    if (rc < 0)
114 +
        return rc;
115 +
    encode_le(&memory[out], st.size, 8);
116 +
    encode_le(&memory[out + 8], st.kind, 4);
117 +
    encode_le(&memory[out + 12], 0, 4);
118 +
    return 0;
119 +
}
120 +
121 +
/* List a directory into a guest buffer. */
122 +
static i64 ecall_list_dir(
123 +
    struct cpu *cpu, u32 pc, u64 path_addr, u64 path_len, u64 buf, u64 buf_len
124 +
) {
125 +
    char path[HOST_PATH_MAX];
126 +
    i64  rc = guest_path(cpu, pc, "listDir", path_addr, path_len, path);
127 +
    if (rc)
128 +
        return rc;
129 +
    if (!guest_check(cpu, pc, "listDir", buf, buf_len))
130 +
        return ECALL_FAULTED;
131 +
    return host_list_dir(path, &memory[buf], (u32)buf_len);
132 +
}
133 +
134 +
/* Wait for one child, or for any child when `any` is set, and store its
135 +
 * outcome record in guest memory. */
136 +
static i64 ecall_wait(struct cpu *cpu, u32 pc, bool any, u64 child, u64 out) {
137 +
    if (!guest_check(cpu, pc, any ? "waitAny" : "wait", out, OUTCOME_SIZE))
138 +
        return ECALL_FAULTED;
139 +
    struct host_outcome outcome = { 0, 0 };
140 +
    /* Handles beyond the table are unknown whatever their width. */
141 +
    i64 rc =
142 +
        any ? host_wait_any(&outcome)
143 +
            : host_wait(child >= HOST_MAX_CHILDREN ? -1 : (i64)child, &outcome);
144 +
    if (rc < 0)
145 +
        return rc;
146 +
    encode_le(&memory[out], outcome.kind, 4);
147 +
    encode_le(&memory[out + 4], (u32)outcome.code, 4);
148 +
    return any ? rc : 0;
149 +
}
150 +
151 +
/* Format a numeric emulator option into `buf` and return it. */
152 +
static char *format_option(char *buf, const char *name, u64 value) {
153 +
    snprintf(buf, 48, "%s%llu", name, (unsigned long long)value);
154 +
    return buf;
155 +
}
156 +
157 +
/* Decode a spawn record at `spec` and start the child emulator. */
158 +
static i64 ecall_spawn(struct cpu *cpu, u32 pc, u64 spec) {
159 +
    if (!guest_check(cpu, pc, "spawn", spec, SPAWN_SIZE))
160 +
        return ECALL_FAULTED;
161 +
162 +
    const u8 *rec       = &memory[spec];
163 +
    u64       image_ptr = decode_u64_le(rec);
164 +
    u32       image_len = decode_u32_le(rec + 8);
165 +
    u64       args_ptr  = decode_u64_le(rec + 16);
166 +
    u32       args_len  = decode_u32_le(rec + 24);
167 +
    u32       out_kind  = decode_u32_le(rec + 32);
168 +
    u32       err_kind  = decode_u32_le(rec + 36);
169 +
    u32       mode      = decode_u32_le(rec + 72);
170 +
    u32       flags     = decode_u32_le(rec + 112);
171 +
172 +
    if (decode_u64_le(rec + 120) != 0 || mode > SPAWN_MODE_MACHINE ||
173 +
        out_kind > SPAWN_OUT_FILE || err_kind > SPAWN_OUT_FILE ||
174 +
        (flags & ~(u32)SPAWN_FLAG_INHERIT) != 0)
175 +
        return HOST_E_INVALID;
176 +
177 +
    char image[HOST_PATH_MAX];
178 +
    char out_path[HOST_PATH_MAX];
179 +
    char err_path[HOST_PATH_MAX];
180 +
    i64  rc = guest_path(cpu, pc, "spawn", image_ptr, image_len, image);
181 +
    if (rc)
182 +
        return rc;
183 +
    if (out_kind == SPAWN_OUT_FILE) {
184 +
        rc = guest_path(
185 +
            cpu,
186 +
            pc,
187 +
            "spawn",
188 +
            decode_u64_le(rec + 40),
189 +
            decode_u32_le(rec + 48),
190 +
            out_path
191 +
        );
192 +
        if (rc)
193 +
            return rc;
194 +
    }
195 +
    if (err_kind == SPAWN_OUT_FILE) {
196 +
        rc = guest_path(
197 +
            cpu,
198 +
            pc,
199 +
            "spawn",
200 +
            decode_u64_le(rec + 56),
201 +
            decode_u32_le(rec + 64),
202 +
            err_path
203 +
        );
204 +
        if (rc)
205 +
            return rc;
206 +
    }
207 +
208 +
    /* Validate the argument slices and measure their bytes. */
209 +
    if (!guest_check(cpu, pc, "spawn", args_ptr, (u64)args_len * SLICE_SIZE))
210 +
        return ECALL_FAULTED;
211 +
    u64 pool_size = 0;
212 +
    for (u32 i = 0; i < args_len; i++) {
213 +
        const u8 *slice = &memory[args_ptr + (u64)i * SLICE_SIZE];
214 +
        u64       ptr   = decode_u64_le(slice);
215 +
        u32       len   = decode_u32_le(slice + 8);
216 +
        if (!guest_check(cpu, pc, "spawn", ptr, len))
217 +
            return ECALL_FAULTED;
218 +
        if (memchr(&memory[ptr], 0, len))
219 +
            return HOST_E_INVALID;
220 +
        pool_size += (u64)len + 1;
221 +
    }
222 +
223 +
    /* Memory sizes are in KB. With `inherit`, zero sizes take the parent's. */
224 +
    u64 memory_kb = decode_u32_le(rec + 80);
225 +
    u64 data_kb   = decode_u32_le(rec + 84);
226 +
    u64 stack_kb  = decode_u32_le(rec + 88);
227 +
    if (flags & SPAWN_FLAG_INHERIT) {
228 +
        struct guest_sizes sizes = guest_sizes();
229 +
        if (!memory_kb)
230 +
            memory_kb = sizes.memory / 1024;
231 +
        if (!data_kb)
232 +
            data_kb = sizes.data / 1024;
233 +
        if (!stack_kb)
234 +
            stack_kb = sizes.stack / 1024;
235 +
    }
236 +
    const struct {
237 +
        const char *name;
238 +
        u64         value;
239 +
    } options[] = {
240 +
        { "-harts=", decode_u32_le(rec + 76) },
241 +
        { "-memory-size=", memory_kb },
242 +
        { "-data-size=", data_kb },
243 +
        { "-stack-size=", stack_kb },
244 +
        { "-irq=", decode_u32_le(rec + 92) },
245 +
        { "-irq-at=", decode_u64_le(rec + 96) },
246 +
        { "-max-steps=", decode_u64_le(rec + 104) },
247 +
    };
248 +
249 +
    /* The program name, emulator options, "--", the image and the guest
250 +
     * arguments, which follow the image unchanged. */
251 +
    enum { OPTION_COUNT = 7, FIXED_MAX = OPTION_COUNT + 5 };
252 +
    char  numbers[OPTION_COUNT + 1][48];
253 +
    char *fixed[FIXED_MAX];
254 +
    int   nfixed   = 0;
255 +
    int   nnumbers = 0;
256 +
257 +
    fixed[nfixed++] = "emulator";
258 +
    fixed[nfixed++] = "-run";
259 +
    if (mode == SPAWN_MODE_MACHINE)
260 +
        fixed[nfixed++] = "-machine";
261 +
    for (usize i = 0; i < sizeof options / sizeof options[0]; i++) {
262 +
        if (options[i].value)
263 +
            fixed[nfixed++] = format_option(
264 +
                numbers[nnumbers++], options[i].name, options[i].value
265 +
            );
266 +
    }
267 +
    fixed[nfixed++] =
268 +
        format_option(numbers[nnumbers++], "-outcome-fd=", HOST_OUTCOME_FD);
269 +
    fixed[nfixed++] = "--";
270 +
271 +
    char **argv = malloc(((usize)nfixed + 2 + args_len) * sizeof *argv);
272 +
    char  *pool = malloc(pool_size ? pool_size : 1);
273 +
    if (!argv || !pool) {
274 +
        free(argv);
275 +
        free(pool);
276 +
        return HOST_E_IO;
277 +
    }
278 +
    int n = 0;
279 +
    for (int i = 0; i < nfixed; i++)
280 +
        argv[n++] = fixed[i];
281 +
    argv[n++] = image;
282 +
283 +
    char *next = pool;
284 +
    for (u32 i = 0; i < args_len; i++) {
285 +
        const u8 *slice = &memory[args_ptr + (u64)i * SLICE_SIZE];
286 +
        u64       ptr   = decode_u64_le(slice);
287 +
        u32       len   = decode_u32_le(slice + 8);
288 +
        memcpy(next, &memory[ptr], len);
289 +
        next[len]  = '\0';
290 +
        argv[n++]  = next;
291 +
        next      += len + 1;
292 +
    }
293 +
    argv[n] = NULL;
294 +
295 +
    struct host_spawn req = {
296 +
        .argv     = argv,
297 +
        .out_kind = out_kind,
298 +
        .out_path = out_path,
299 +
        .err_kind = err_kind,
300 +
        .err_path = err_path,
301 +
    };
302 +
    /* One path for both streams is opened once and shared. */
303 +
    if (out_kind == SPAWN_OUT_FILE && err_kind == SPAWN_OUT_FILE &&
304 +
        strcmp(out_path, err_path) == 0)
305 +
        req.err_kind = HOST_OUT_MERGE;
306 +
307 +
    fflush(NULL);
308 +
    rc = host_spawn(&req);
309 +
    free(argv);
310 +
    free(pool);
311 +
    return rc;
312 +
}
313 +
314 +
/* Run the host call selected by a7 and store its result in a0. */
315 +
void abi_host_call(struct cpu *cpu, u32 pc) {
316 +
    u64  call = cpu->regs[A7];
317 +
    u64  a0 = cpu->regs[A0], a1 = cpu->regs[A1];
318 +
    u64  a2 = cpu->regs[A2], a3 = cpu->regs[A3];
319 +
    i64  rc;
320 +
    char path[HOST_PATH_MAX];
321 +
    char other[HOST_PATH_MAX];
322 +
323 +
    switch (call) {
324 +
    case ECALL_EXIT:
325 +
        /* The status is already in a0. */
326 +
        cpu->running = false;
327 +
        return;
328 +
    case ECALL_READ:
329 +
    case ECALL_WRITE:
330 +
        rc = ecall_transfer(cpu, pc, call == ECALL_WRITE, a0, a1, a2);
331 +
        break;
332 +
    case ECALL_OPEN:
333 +
        rc = guest_path(cpu, pc, "open", a0, a1, path);
334 +
        if (!rc)
335 +
            rc = ecall_open(path, a2);
336 +
        break;
337 +
    case ECALL_CLOSE:
338 +
        rc = ecall_close(a0);
339 +
        break;
340 +
    case ECALL_STAT:
341 +
        rc = ecall_stat(cpu, pc, a0, a1, a2);
342 +
        break;
343 +
    case ECALL_MAKE_DIR:
344 +
        rc = guest_path(cpu, pc, "makeDir", a0, a1, path);
345 +
        if (!rc)
346 +
            rc = host_make_dir(path);
347 +
        break;
348 +
    case ECALL_REMOVE:
349 +
        rc = guest_path(cpu, pc, "remove", a0, a1, path);
350 +
        if (!rc)
351 +
            rc = host_remove(path);
352 +
        break;
353 +
    case ECALL_RENAME:
354 +
        rc = guest_path(cpu, pc, "rename", a0, a1, path);
355 +
        if (!rc)
356 +
            rc = guest_path(cpu, pc, "rename", a2, a3, other);
357 +
        if (!rc)
358 +
            rc = host_rename(path, other);
359 +
        break;
360 +
    case ECALL_LIST_DIR:
361 +
        rc = ecall_list_dir(cpu, pc, a0, a1, a2, a3);
362 +
        break;
363 +
    case ECALL_SPAWN:
364 +
        rc = ecall_spawn(cpu, pc, a0);
365 +
        break;
366 +
    case ECALL_WAIT:
367 +
        rc = ecall_wait(cpu, pc, false, a0, a1);
368 +
        break;
369 +
    case ECALL_WAIT_ANY:
370 +
        rc = ecall_wait(cpu, pc, true, 0, a0);
371 +
        break;
372 +
    default:
373 +
        rc = HOST_E_INVALID;
374 +
        break;
375 +
    }
376 +
    /* A fault has already stopped the guest; leave its registers alone. */
377 +
    if (rc != ECALL_FAULTED)
378 +
        cpu->regs[A0] = (u64)rc;
379 +
}
abi_linux.c added +121 -0
1 +
/* Linux-numbered calls of programs built before the host ABI, up to October
2 +
 * 2026.
3 +
 *
4 +
 * The call number is in a7 and arguments in a0..a3, with the numbers and
5 +
 * argument order of Linux on RV64. Only the calls these programs make exist:
6 +
 *
7 +
 *   56  openat(dirfd, path, flags, mode)  descriptor, or -1
8 +
 *   57  close(fd)                         0, or -1
9 +
 *   63  read(fd, buf, len)                bytes read, 0 on failure
10 +
 *   64  write(fd, buf, len)               bytes written, 0 on failure
11 +
 *   93  exit(status)
12 +
 *
13 +
 * `openat` ignores `dirfd` and `mode`, and takes a zero-terminated path. Its
14 +
 * flags use Linux values; the programs only open a file for reading, or for
15 +
 * writing with O_CREAT and O_TRUNC, and other flags fail. Other call numbers
16 +
 * return -ENOSYS. */
17 +
#include <string.h>
18 +
19 +
#include "host.h"
20 +
#include "hosted.h"
21 +
#include "types.h"
22 +
23 +
#define LINUX_OPENAT 56
24 +
#define LINUX_CLOSE  57
25 +
#define LINUX_READ   63
26 +
#define LINUX_WRITE  64
27 +
#define LINUX_EXIT   93
28 +
29 +
/* Linux `open` flag values. */
30 +
#define LINUX_O_ACCMODE 03
31 +
#define LINUX_O_RDONLY  00
32 +
#define LINUX_O_WRONLY  01
33 +
#define LINUX_O_CREAT   0100
34 +
#define LINUX_O_TRUNC   01000
35 +
36 +
/* Linux error number for an unknown call. */
37 +
#define LINUX_ENOSYS 38
38 +
39 +
/* Open the zero-terminated guest path at `addr` and give it a guest
40 +
 * descriptor. Returns -1 on any failure. */
41 +
static i64 linux_openat(u64 addr, u64 flags) {
42 +
    u32 size = guest_sizes().memory;
43 +
    if (addr >= size)
44 +
        return -1;
45 +
    const u8 *end = memchr(&memory[addr], 0, size - addr);
46 +
    if (!end || (usize)(end - &memory[addr]) >= HOST_PATH_MAX)
47 +
        return -1;
48 +
49 +
    const char *path = (const char *)&memory[addr];
50 +
    i64         host_fd;
51 +
    if (flags == LINUX_O_RDONLY)
52 +
        host_fd = host_open_read(path);
53 +
    else if (flags == (LINUX_O_WRONLY | LINUX_O_CREAT | LINUX_O_TRUNC))
54 +
        host_fd = host_open_write(path);
55 +
    else
56 +
        return -1;
57 +
    if (host_fd < 0)
58 +
        return -1;
59 +
60 +
    int guest_fd = guest_fd_table_add((int)host_fd);
61 +
    if (guest_fd < 0) {
62 +
        host_close((int)host_fd);
63 +
        return -1;
64 +
    }
65 +
    return guest_fd;
66 +
}
67 +
68 +
/* Close a guest descriptor. Closing a standard stream does nothing. */
69 +
static i64 linux_close(i64 guest_fd) {
70 +
    if (guest_fd >= 0 && guest_fd < 3)
71 +
        return 0;
72 +
    int host_fd = guest_fd_table_get((int)guest_fd);
73 +
    if (host_fd < 0)
74 +
        return -1;
75 +
    guest_fd_table_remove((int)guest_fd);
76 +
    return host_close(host_fd) < 0 ? -1 : 0;
77 +
}
78 +
79 +
/* Read or write guest memory through a guest descriptor. Returns the bytes
80 +
 * transferred, 0 on failure, or -1 after faulting the guest. */
81 +
static i64 linux_transfer(
82 +
    struct cpu *cpu, u32 pc, bool write, i64 guest_fd, u64 addr, u64 len
83 +
) {
84 +
    if (!guest_check(cpu, pc, write ? "write" : "read", addr, len))
85 +
        return -1;
86 +
    int host_fd = guest_fd_table_get((int)guest_fd);
87 +
    if (host_fd < 0 || len == 0)
88 +
        return 0;
89 +
    i64 n = write ? host_write(host_fd, &memory[addr], (u32)len)
90 +
                  : host_read(host_fd, &memory[addr], (u32)len);
91 +
    return n < 0 ? 0 : n;
92 +
}
93 +
94 +
void abi_linux_call(struct cpu *cpu, u32 pc) {
95 +
    u64 call = cpu->regs[A7];
96 +
    u64 a0 = cpu->regs[A0], a1 = cpu->regs[A1], a2 = cpu->regs[A2];
97 +
    i64 rc;
98 +
99 +
    switch (call) {
100 +
    case LINUX_EXIT:
101 +
        /* The status is already in a0. */
102 +
        cpu->running = false;
103 +
        return;
104 +
    case LINUX_READ:
105 +
    case LINUX_WRITE:
106 +
        rc = linux_transfer(cpu, pc, call == LINUX_WRITE, (i64)a0, a1, a2);
107 +
        if (rc < 0)
108 +
            return;
109 +
        break;
110 +
    case LINUX_OPENAT:
111 +
        rc = linux_openat(a1, a2);
112 +
        break;
113 +
    case LINUX_CLOSE:
114 +
        rc = linux_close((i64)a0);
115 +
        break;
116 +
    default:
117 +
        rc = -LINUX_ENOSYS;
118 +
        break;
119 +
    }
120 +
    cpu->regs[A0] = (u64)rc;
121 +
}
emulator.c +43 -596
9 9
#include <termios.h>
10 10
#include <unistd.h>
11 11
12 12
#include "color.h"
13 13
#include "host.h"
14 +
#include "hosted.h"
14 15
#include "io.h"
15 16
#include "jit.h"
16 17
#include "machine.h"
17 18
#include "muldiv.h"
18 19
#include "riscv.h"
36 37
) {
37 38
    fprintf(stderr, "%s:%d: assertion `%s` failed\n", file, line, condition);
38 39
    abort();
39 40
}
40 41
41 -
/* Maximum physical memory size (384MB). The runtime can choose any size up to
42 -
 * this value with `-memory-size=...`. */
43 -
#define MEMORY_SIZE              (384 * 1024 * 1024)
44 42
/* Default physical memory size (128MB). */
45 43
#define DEFAULT_MEMORY_SIZE      (128 * 1024 * 1024)
46 -
/* Writable data region base. */
47 -
#define DATA_RW_OFFSET           0xFFFFF0
48 -
/* Data memory starts at writable data region. */
49 -
#define DATA_MEMORY_START        DATA_RW_OFFSET
50 44
/* Default data memory size. */
51 45
#define DEFAULT_DATA_MEMORY_SIZE (DEFAULT_MEMORY_SIZE - DATA_MEMORY_START)
52 46
/* Default stack size (256KB), allocated at the end of memory. */
53 47
#define DEFAULT_STACK_SIZE       (256 * 1024)
54 48
/* Maximum instructions to show in the TUI. */
55 49
#define MAX_INSTR_DISPLAY        40
56 50
/* Stack words to display in the TUI. */
57 51
#define STACK_DISPLAY_WORDS      32
58 52
/* Maximum number of CPU state snapshots to store for undo. */
59 53
#define MAX_SNAPSHOTS            64
60 -
/* Maximum open files in guest runtime. */
61 -
#define MAX_OPEN_FILES           32
62 54
/* Number of history entries to print when reporting faults. */
63 55
#define FAULT_TRACE_DEPTH        8
64 56
/* Instruction trace depth for headless tracing. */
65 57
#define TRACE_HISTORY            64
66 58
/* Maximum number of steps executed in headless mode before timing out. */
67 59
#define HEADLESS_MAX_STEPS       ((u64)1000000000000)
68 60
/* Height of header and footer in rows. */
69 61
#define HEADER_HEIGHT            2
70 62
#define FOOTER_HEIGHT            3
71 -
/* Read-only data offset. */
72 -
#define DATA_RO_OFFSET           0x10000
73 63
/* TTY escape codes. */
74 64
#define TTY_CLEAR                "\033[2J\033[H"
75 65
#define TTY_GOTO_RC              "\033[%d;%dH"
76 66
/* Exit code returned on EBREAK. */
77 67
#define EBREAK_EXIT_CODE         133
78 -
/* Native image magic: the ASCII bytes "RADIANCE" as a little-endian u64. */
79 -
#define NATIVE_IMAGE_MAGIC       0x45434E4149444152ULL
80 -
/* Legacy image magic of older compilers: the ASCII bytes "RAD0". A u32
81 -
 * version follows; only version 1 is supported. */
82 -
#define LEGACY_IMAGE_MAGIC       0x30444152U
83 -
#define LEGACY_IMAGE_VERSION     1U
84 -
/* Size of the legacy image header. */
85 -
#define LEGACY_HEADER_SIZE       20U
86 68
87 69
/* Registers displayed in the TUI, in order. */
88 70
static const reg_t registers_displayed[] = {
89 71
    SP, FP, RA, A0, A1, A2, A3, A4, A5, A6, A7, T0, T1, T2, T3, T4, T5, T6
90 72
};
107 89
};
108 90
109 91
/* Global debug info. */
110 92
static struct debug_info g_debug = { 0 };
111 93
112 -
/* CPU state. */
113 -
struct cpu {
114 -
    u64      regs[REGISTERS];
115 -
    u32      pc;          /* Program counter. */
116 -
    u32      programsize; /* Size of loaded program. */
117 -
    instr_t *program;     /* Program instructions. */
118 -
    bool     running;     /* Execution status. */
119 -
    bool     faulted;     /* There was a fault in execution. */
120 -
    bool     ebreak;      /* Program terminated via EBREAK. */
121 -
    reg_t    modified;    /* Index of the last modified register. */
122 -
};
123 -
124 94
/* Snapshot of CPU and memory state for reversing execution. */
125 95
struct snapshot {
126 96
    struct cpu cpu;                 /* Copy of CPU state. */
127 97
    u8         memory[MEMORY_SIZE]; /* Copy of memory. */
128 98
};
133 103
    int             head; /* Index of most recent snapshot. */
134 104
    int             count;
135 105
};
136 106
137 107
/* CPU memory. */
138 -
static u8 memory[MEMORY_SIZE];
108 +
u8 memory[MEMORY_SIZE];
139 109
140 110
/* Loaded section sizes, used for bounds checking and diagnostics. */
141 111
static u32 program_base  = 0;
142 112
static u32 program_bytes = 0;
143 113
static u32 data_bytes    = 0;
154 124
        guest_fds[i] = -1;
155 125
    }
156 126
}
157 127
158 128
/* Add a host file descriptor to the guest table. */
159 -
static int guest_fd_table_add(int host_fd) {
129 +
int guest_fd_table_add(int host_fd) {
160 130
    /* Start at 3 to skip stdin/stdout/stderr. */
161 131
    for (int i = 3; i < MAX_OPEN_FILES; i++) {
162 132
        if (guest_fds[i] == -1) {
163 133
            guest_fds[i] = host_fd;
164 134
            return i;
166 136
    }
167 137
    return -1;
168 138
}
169 139
170 140
/* Get the host fd for a guest file descriptor. */
171 -
static int guest_fd_table_get(int guest_fd) {
141 +
int guest_fd_table_get(int guest_fd) {
172 142
    if (guest_fd < 0 || guest_fd >= MAX_OPEN_FILES) {
173 143
        return -1;
174 144
    }
175 145
    /* Standard streams map directly. */
176 146
    if (guest_fd < 3) {
178 148
    }
179 149
    return guest_fds[guest_fd];
180 150
}
181 151
182 152
/* Remove a file descriptor from the guest table. */
183 -
static void guest_fd_table_remove(int guest_fd) {
153 +
void guest_fd_table_remove(int guest_fd) {
184 154
    if (guest_fd >= 3 && guest_fd < MAX_OPEN_FILES) {
185 155
        guest_fds[guest_fd] = -1;
186 156
    }
187 157
}
188 158
1286 1256
    trace_reset();
1287 1257
    guest_fd_table_init();
1288 1258
    cpu_reset(cpu);
1289 1259
}
1290 1260
1291 -
/* Decode a little-endian word without relying on host alignment. */
1292 -
static u32 decode_u32_le(const u8 *bytes) {
1293 -
    return (u32)bytes[0] | ((u32)bytes[1] << 8) | ((u32)bytes[2] << 16) |
1294 -
           ((u32)bytes[3] << 24);
1295 -
}
1296 -
1297 -
/* Decode a little-endian doubleword without relying on host alignment. */
1298 -
static u64 decode_u64_le(const u8 *bytes) {
1299 -
    return (u64)decode_u32_le(bytes) | ((u64)decode_u32_le(bytes + 4) << 32);
1300 -
}
1301 -
1302 -
/* Read exactly size bytes from file into guest memory at offset. */
1303 -
static void load_image_section(
1304 -
    FILE *file, u32 offset, u32 size, const char *label
1305 -
) {
1261 +
void image_read(FILE *file, u32 offset, u32 size, const char *label) {
1306 1262
    if (size == 0)
1307 1263
        return;
1308 1264
1309 1265
    size_t read = fread(&memory[offset], 1, size, file);
1310 1266
    if (read != size)
1459 1415
    fclose(file);
1460 1416
1461 1417
    return line;
1462 1418
}
1463 1419
1464 -
/* A native image segment: its address, the bytes stored in the file, and the
1465 -
 * zero-filled bytes that follow them in memory. */
1466 -
struct image_segment {
1467 -
    u64 base;
1468 -
    u32 size;
1469 -
    u32 tail;
1470 -
};
1471 -
1472 -
/* A decoded native image: the code, read-only data and read-write data
1473 -
 * segments, in that order. Execution starts at the code address. */
1474 -
struct image {
1475 -
    struct image_segment segments[3];
1476 -
};
1477 -
1478 -
/* The address range [low, high) that each segment must lie in. */
1479 -
struct image_range {
1480 -
    u64 low[3];
1481 -
    u64 high[3];
1482 -
};
1483 -
1484 -
/* Decode and check a native image of file_size bytes, leaving the file
1485 -
 * positioned at the payload. The caller has already checked the magic. */
1486 -
static struct image parse_image(
1487 -
    FILE *file, long file_size, const struct image_range *range
1488 -
) {
1489 -
    enum { HEADER_BYTES = 48 };
1490 -
    u8 header[HEADER_BYTES];
1491 -
    rewind(file);
1492 -
    if (fread(header, 1, sizeof(header), file) != sizeof(header))
1493 -
        bail("truncated image header");
1494 -
1495 -
    struct image image;
1496 -
    u64          expected = HEADER_BYTES;
1497 -
    for (u32 i = 0; i < 3; i++) {
1498 -
        const u8             *record  = header + 8 + i * 12;
1499 -
        struct image_segment *seg     = &image.segments[i];
1500 -
        seg->base                     = decode_u64_le(record);
1501 -
        seg->size                     = decode_u32_le(record + 8);
1502 -
        seg->tail                     = i == 2 ? decode_u32_le(record + 12) : 0;
1503 -
        expected                     += seg->size;
1504 -
        u64 extent                    = (u64)seg->size + seg->tail;
1505 -
        if (extent &&
1506 -
            (seg->base < range->low[i] || seg->base >= range->high[i] ||
1507 -
             extent > range->high[i] - seg->base))
1508 -
            bail("image segment outside memory");
1509 -
    }
1510 -
    if (expected != (u64)file_size)
1511 -
        bail("image size does not match header");
1512 -
    return image;
1513 -
}
1514 -
1515 -
/* Read the stored bytes of each segment into memory, with ram_base the address
1516 -
 * that memory offset 0 stands for. The zero-filled tails need no clearing:
1517 -
 * guest memory is static and the image loads once. */
1518 -
static void load_image_segments(
1519 -
    FILE *file, const struct image *image, u64 ram_base
1520 -
) {
1521 -
    for (u32 i = 0; i < 3; i++) {
1522 -
        const struct image_segment *seg = &image->segments[i];
1523 -
        load_image_section(
1524 -
            file, (u32)(seg->base - ram_base), seg->size, "native"
1525 -
        );
1526 -
    }
1527 -
}
1528 -
1529 -
/* Load a native image in machine mode, where every segment must lie in the
1530 -
 * RAM bank at MACHINE_RAM_BASE, and record it for machine_run. */
1531 -
static void load_machine_image(FILE *file, long file_size) {
1532 -
    u64                end   = MACHINE_RAM_BASE + g_opts.memory_size;
1533 -
    struct image_range range = {
1534 -
        .low  = { MACHINE_RAM_BASE, MACHINE_RAM_BASE, MACHINE_RAM_BASE },
1535 -
        .high = { end, end, end },
1536 -
    };
1537 -
    struct image image = parse_image(file, file_size, &range);
1538 -
    load_image_segments(file, &image, MACHINE_RAM_BASE);
1539 -
1540 -
    machine_options.entry              = image.segments[0].base;
1541 -
    struct machine_section *sections[] = { &machine_options.text,
1542 -
                                           &machine_options.rodata,
1543 -
                                           &machine_options.rwdata };
1544 -
    for (u32 i = 0; i < 3; i++) {
1545 -
        sections[i]->base = image.segments[i].base;
1546 -
        sections[i]->size = image.segments[i].size + image.segments[i].tail;
1547 -
    }
1548 -
}
1549 -
1550 -
/* Load a native image in hosted mode and point the CPU at the code. Code and
1551 -
 * read-only data must lie in [DATA_RO_OFFSET, DATA_MEMORY_START) and
1552 -
 * read-write data in [DATA_MEMORY_START, data_limit). */
1553 -
static void load_hosted_image(
1554 -
    struct cpu *cpu, FILE *file, long file_size, u32 data_limit
1555 -
) {
1556 -
    struct image_range range = {
1557 -
        .low  = { DATA_RO_OFFSET, DATA_RO_OFFSET, DATA_MEMORY_START },
1558 -
        .high = { DATA_MEMORY_START, DATA_MEMORY_START, data_limit },
1559 -
    };
1560 -
    struct image image = parse_image(file, file_size, &range);
1561 -
    load_image_segments(file, &image, 0);
1562 -
1563 -
    const struct image_segment *code   = &image.segments[0];
1564 -
    const struct image_segment *rwdata = &image.segments[2];
1565 -
    u32                         extent = rwdata->size + rwdata->tail;
1566 -
    program_base                       = (u32)code->base;
1567 -
    program_bytes                      = code->size;
1568 -
    /* The environment block must lie beyond the end of read-write data. */
1569 -
    data_bytes = extent ? (u32)(rwdata->base - DATA_MEMORY_START) + extent : 0;
1570 -
    cpu->programsize = program_bytes / sizeof(instr_t);
1571 -
    cpu->pc          = (u32)code->base;
1572 -
}
1573 -
1574 1420
/* Load the program image into memory. */
1575 1421
static void program_init(struct cpu *cpu, const char *filepath) {
1576 1422
    program_bytes = 0;
1577 1423
    data_bytes    = 0;
1578 1424
    if (g_opts.debug_enabled)
1597 1443
1598 1444
    u32 data_limit = DATA_MEMORY_START + g_opts.data_memory_size;
1599 1445
    if (data_limit > g_opts.memory_size)
1600 1446
        data_limit = g_opts.memory_size;
1601 1447
1602 -
    u8     header[LEGACY_HEADER_SIZE];
1448 +
    /* The format is recognized by its magic; see hosted.h. */
1449 +
    u8     header[sizeof(u64)];
1603 1450
    size_t header_read = fread(header, 1, sizeof(header), file);
1604 -
    if (header_read >= sizeof(u64) &&
1605 -
        decode_u64_le(header) == NATIVE_IMAGE_MAGIC) {
1606 -
        if (g_opts.machine_mode)
1607 -
            load_machine_image(file, size);
1608 -
        else
1609 -
            load_hosted_image(cpu, file, size, data_limit);
1610 -
        fclose(file);
1611 -
        return;
1612 -
    }
1613 -
    if (g_opts.machine_mode || header_read < sizeof(u32) ||
1614 -
        decode_u32_le(header) != LEGACY_IMAGE_MAGIC) {
1615 -
        fclose(file);
1616 -
        bail("unrecognized Radiance image: '%s'", filepath);
1617 -
    }
1618 -
    if (header_read != sizeof(header)) {
1619 -
        fclose(file);
1620 -
        bail("truncated Radiance image header");
1621 -
    }
1622 -
    u32 version = decode_u32_le(&header[4]);
1623 -
    if (version != LEGACY_IMAGE_VERSION) {
1624 -
        fclose(file);
1625 -
        bail("unsupported Radiance image version: %u", version);
1626 -
    }
1627 -
    program_bytes    = decode_u32_le(&header[8]);
1628 -
    u32 rodata_bytes = decode_u32_le(&header[12]);
1629 -
    data_bytes       = decode_u32_le(&header[16]);
1630 -
    if (program_bytes == 0 || program_bytes % sizeof(instr_t) != 0) {
1631 -
        fclose(file);
1632 -
        bail("invalid text section size in Radiance image: %u", program_bytes);
1633 -
    }
1634 -
    u64 image_size =
1635 -
        (u64)LEGACY_HEADER_SIZE + program_bytes + rodata_bytes + data_bytes;
1636 -
    if (image_size != (u64)size) {
1637 -
        fclose(file);
1638 -
        bail(
1639 -
            "Radiance image size does not match header: file has %ld "
1640 -
            "bytes, header requires %llu",
1641 -
            size,
1642 -
            (unsigned long long)image_size
1451 +
    if (g_opts.machine_mode) {
1452 +
        if (!image_native_match(header, header_read)) {
1453 +
            fclose(file);
1454 +
            bail("unrecognized Radiance image: '%s'", filepath);
1455 +
        }
1456 +
        image_native_load_machine(
1457 +
            file, size, g_opts.memory_size, &machine_options
1643 1458
        );
1644 -
    }
1645 -
    if ((u64)DATA_RO_OFFSET + rodata_bytes > DATA_MEMORY_START) {
1646 1459
        fclose(file);
1647 -
        bail("read-only data section exceeds available program memory");
1648 -
    }
1649 -
    program_base = align(DATA_RO_OFFSET + rodata_bytes, WORD_SIZE);
1650 -
    if ((u64)program_base + program_bytes > DATA_MEMORY_START) {
1651 -
        fclose(file);
1652 -
        bail("text section exceeds available program memory");
1653 -
    }
1654 -
    if ((u64)DATA_MEMORY_START + data_bytes > data_limit) {
1655 -
        fclose(file);
1656 -
        bail("read-write data section exceeds available data memory");
1460 +
        return;
1657 1461
    }
1658 -
1659 -
    load_image_section(file, program_base, program_bytes, "text");
1660 -
    load_image_section(file, DATA_RO_OFFSET, rodata_bytes, "read-only data");
1661 -
    load_image_section(file, DATA_MEMORY_START, data_bytes, "read-write data");
1462 +
    struct hosted_image image;
1463 +
    if (image_native_match(header, header_read))
1464 +
        image = image_native_load(file, size, data_limit);
1465 +
    else if (image_rad0_match(header, header_read))
1466 +
        image = image_rad0_load(file, size, data_limit);
1467 +
    else
1468 +
        image = image_raw_load(file, size, filepath, data_limit);
1662 1469
    fclose(file);
1663 1470
1471 +
    program_base     = image.code_base;
1472 +
    program_bytes    = image.code_bytes;
1473 +
    data_bytes       = image.data_bytes;
1664 1474
    cpu->programsize = program_bytes / sizeof(instr_t);
1665 1475
    cpu->pc          = program_base;
1666 1476
}
1667 1477
1668 -
/* Hosted system calls. The call number is in a7, arguments in a0..a3 and the
1669 -
 * result in a0; a negative result is an error code. See README. */
1670 -
#define ECALL_EXIT     0x400
1671 -
#define ECALL_READ     0x401
1672 -
#define ECALL_WRITE    0x402
1673 -
#define ECALL_OPEN     0x403
1674 -
#define ECALL_CLOSE    0x404
1675 -
#define ECALL_STAT     0x405
1676 -
#define ECALL_MAKE_DIR 0x406
1677 -
#define ECALL_REMOVE   0x407
1678 -
#define ECALL_RENAME   0x408
1679 -
#define ECALL_LIST_DIR 0x409
1680 -
#define ECALL_SPAWN    0x40A
1681 -
#define ECALL_WAIT     0x40B
1682 -
#define ECALL_WAIT_ANY 0x40C
1683 -
1684 -
/* Result of a call that faulted the guest: a0 is left unchanged. */
1685 -
#define ECALL_FAULTED INT64_MIN
1686 -
1687 -
/* Size of the guest records exchanged with the host. */
1688 -
#define SLICE_SIZE   16
1689 -
#define STAT_SIZE    16
1690 -
#define OUTCOME_SIZE 8
1691 -
#define SPAWN_SIZE   128
1692 -
1693 -
/* Spawn record values. */
1694 -
#define SPAWN_MODE_MACHINE 1
1695 -
#define SPAWN_OUT_FILE     2
1696 -
#define SPAWN_FLAG_INHERIT 1
1697 -
1698 -
/* Return true if the guest range [addr, addr + len) lies in guest memory. */
1699 -
static bool guest_range(u64 addr, u64 len) {
1700 -
    return addr <= g_opts.memory_size && len <= g_opts.memory_size - addr;
1701 -
}
1702 -
1703 -
/* Check a guest range, faulting the guest if it is out of bounds. */
1704 -
static bool ecall_range(
1705 -
    struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len
1706 -
) {
1707 -
    if (guest_range(addr, len))
1478 +
struct guest_sizes guest_sizes(void) {
1479 +
    return (struct guest_sizes){
1480 +
        .memory = g_opts.memory_size,
1481 +
        .data   = g_opts.data_memory_size,
1482 +
        .stack  = g_opts.stack_size,
1483 +
    };
1484 +
}
1485 +
1486 +
bool guest_check(struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len) {
1487 +
    if (addr <= g_opts.memory_size && len <= g_opts.memory_size - addr)
1708 1488
        return true;
1709 1489
    printf(
1710 1490
        "sys_%s out of bounds: addr=%016llx len=%llu\n",
1711 1491
        call,
1712 1492
        (unsigned long long)addr,
1715 1495
    cpu->running = false;
1716 1496
    emit_fault_diagnostics(cpu, pc);
1717 1497
    return false;
1718 1498
}
1719 1499
1720 -
/* Store a little-endian integer of `size` bytes into guest memory. */
1721 -
static void encode_le(u8 *bytes, u64 value, u32 size) {
1722 -
    for (u32 i = 0; i < size; i++)
1723 -
        bytes[i] = (u8)(value >> (8 * i));
1724 -
}
1725 -
1726 -
/* Copy the guest string of `len` bytes at `addr` into `out`, which holds
1727 -
 * HOST_PATH_MAX bytes, and terminate it. Returns 0, `HOST_E_INVALID` for an
1728 -
 * embedded zero byte or a path that is too long, or `ECALL_FAULTED` after
1729 -
 * faulting the guest. */
1730 -
static i64 guest_path(
1731 -
    struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len, char *out
1732 -
) {
1733 -
    if (!ecall_range(cpu, pc, call, addr, len))
1734 -
        return ECALL_FAULTED;
1735 -
    if (len >= HOST_PATH_MAX || memchr(&memory[addr], 0, len))
1736 -
        return HOST_E_INVALID;
1737 -
    memcpy(out, &memory[addr], len);
1738 -
    out[len] = '\0';
1739 -
    return 0;
1740 -
}
1741 -
1742 -
/* Open a file and give it a guest descriptor. */
1743 -
static i64 ecall_open(const char *path, u64 mode) {
1744 -
    if (mode > 1)
1745 -
        return HOST_E_INVALID;
1746 -
    i64 host_fd = mode ? host_open_write(path) : host_open_read(path);
1747 -
    if (host_fd < 0)
1748 -
        return host_fd;
1749 -
    int guest_fd = guest_fd_table_add((int)host_fd);
1750 -
    if (guest_fd < 0) {
1751 -
        host_close((int)host_fd);
1752 -
        return HOST_E_LIMIT;
1753 -
    }
1754 -
    return guest_fd;
1755 -
}
1756 -
1757 -
/* Close a guest descriptor. The standard streams cannot be closed. */
1758 -
static i64 ecall_close(u64 guest_fd) {
1759 -
    if (guest_fd < 3 || guest_fd >= MAX_OPEN_FILES)
1760 -
        return HOST_E_INVALID;
1761 -
    int host_fd = guest_fd_table_get((int)guest_fd);
1762 -
    if (host_fd < 0)
1763 -
        return HOST_E_INVALID;
1764 -
    guest_fd_table_remove((int)guest_fd);
1765 -
    return host_close(host_fd) < 0 ? HOST_E_IO : 0;
1766 -
}
1767 -
1768 -
/* Read or write guest memory through a guest descriptor. */
1769 -
static i64 ecall_transfer(
1770 -
    struct cpu *cpu, u32 pc, bool write, u64 guest_fd, u64 addr, u64 len
1771 -
) {
1772 -
    if (!ecall_range(cpu, pc, write ? "write" : "read", addr, len))
1773 -
        return ECALL_FAULTED;
1774 -
    int host_fd =
1775 -
        guest_fd < MAX_OPEN_FILES ? guest_fd_table_get((int)guest_fd) : -1;
1776 -
    if (host_fd < 0)
1777 -
        return HOST_E_INVALID;
1778 -
    if (len == 0)
1779 -
        return 0;
1780 -
    return write ? host_write(host_fd, &memory[addr], (u32)len)
1781 -
                 : host_read(host_fd, &memory[addr], (u32)len);
1782 -
}
1783 -
1784 -
/* Describe a path into a guest Stat record. */
1785 -
static i64 ecall_stat(
1786 -
    struct cpu *cpu, u32 pc, u64 path_addr, u64 path_len, u64 out
1787 -
) {
1788 -
    char path[HOST_PATH_MAX];
1789 -
    i64  rc = guest_path(cpu, pc, "stat", path_addr, path_len, path);
1790 -
    if (rc)
1791 -
        return rc;
1792 -
    if (!ecall_range(cpu, pc, "stat", out, STAT_SIZE))
1793 -
        return ECALL_FAULTED;
1794 -
    struct host_stat st;
1795 -
    rc = host_stat(path, &st);
1796 -
    if (rc < 0)
1797 -
        return rc;
1798 -
    encode_le(&memory[out], st.size, 8);
1799 -
    encode_le(&memory[out + 8], st.kind, 4);
1800 -
    encode_le(&memory[out + 12], 0, 4);
1801 -
    return 0;
1802 -
}
1803 -
1804 -
/* List a directory into a guest buffer. */
1805 -
static i64 ecall_list_dir(
1806 -
    struct cpu *cpu, u32 pc, u64 path_addr, u64 path_len, u64 buf, u64 buf_len
1807 -
) {
1808 -
    char path[HOST_PATH_MAX];
1809 -
    i64  rc = guest_path(cpu, pc, "listDir", path_addr, path_len, path);
1810 -
    if (rc)
1811 -
        return rc;
1812 -
    if (!ecall_range(cpu, pc, "listDir", buf, buf_len))
1813 -
        return ECALL_FAULTED;
1814 -
    return host_list_dir(path, &memory[buf], (u32)buf_len);
1815 -
}
1816 -
1817 -
/* Wait for one child, or for any child when `any` is set, and store its
1818 -
 * outcome record in guest memory. */
1819 -
static i64 ecall_wait(struct cpu *cpu, u32 pc, bool any, u64 child, u64 out) {
1820 -
    if (!ecall_range(cpu, pc, any ? "waitAny" : "wait", out, OUTCOME_SIZE))
1821 -
        return ECALL_FAULTED;
1822 -
    struct host_outcome outcome = { 0, 0 };
1823 -
    /* Handles beyond the table are unknown whatever their width. */
1824 -
    i64 rc =
1825 -
        any ? host_wait_any(&outcome)
1826 -
            : host_wait(child >= HOST_MAX_CHILDREN ? -1 : (i64)child, &outcome);
1827 -
    if (rc < 0)
1828 -
        return rc;
1829 -
    encode_le(&memory[out], outcome.kind, 4);
1830 -
    encode_le(&memory[out + 4], (u32)outcome.code, 4);
1831 -
    return any ? rc : 0;
1832 -
}
1833 -
1834 -
/* Format a numeric emulator option into `buf` and return it. */
1835 -
static char *format_option(char *buf, const char *name, u64 value) {
1836 -
    snprintf(buf, 48, "%s%llu", name, (unsigned long long)value);
1837 -
    return buf;
1838 -
}
1839 -
1840 -
/* Decode a spawn record at `spec` and start the child emulator. */
1841 -
static i64 ecall_spawn(struct cpu *cpu, u32 pc, u64 spec) {
1842 -
    if (!ecall_range(cpu, pc, "spawn", spec, SPAWN_SIZE))
1843 -
        return ECALL_FAULTED;
1844 -
1845 -
    const u8 *rec       = &memory[spec];
1846 -
    u64       image_ptr = decode_u64_le(rec);
1847 -
    u32       image_len = decode_u32_le(rec + 8);
1848 -
    u64       args_ptr  = decode_u64_le(rec + 16);
1849 -
    u32       args_len  = decode_u32_le(rec + 24);
1850 -
    u32       out_kind  = decode_u32_le(rec + 32);
1851 -
    u32       err_kind  = decode_u32_le(rec + 36);
1852 -
    u32       mode      = decode_u32_le(rec + 72);
1853 -
    u32       flags     = decode_u32_le(rec + 112);
1854 -
1855 -
    if (decode_u64_le(rec + 120) != 0 || mode > SPAWN_MODE_MACHINE ||
1856 -
        out_kind > SPAWN_OUT_FILE || err_kind > SPAWN_OUT_FILE ||
1857 -
        (flags & ~(u32)SPAWN_FLAG_INHERIT) != 0)
1858 -
        return HOST_E_INVALID;
1859 -
1860 -
    char image[HOST_PATH_MAX];
1861 -
    char out_path[HOST_PATH_MAX];
1862 -
    char err_path[HOST_PATH_MAX];
1863 -
    i64  rc = guest_path(cpu, pc, "spawn", image_ptr, image_len, image);
1864 -
    if (rc)
1865 -
        return rc;
1866 -
    if (out_kind == SPAWN_OUT_FILE) {
1867 -
        rc = guest_path(
1868 -
            cpu,
1869 -
            pc,
1870 -
            "spawn",
1871 -
            decode_u64_le(rec + 40),
1872 -
            decode_u32_le(rec + 48),
1873 -
            out_path
1874 -
        );
1875 -
        if (rc)
1876 -
            return rc;
1877 -
    }
1878 -
    if (err_kind == SPAWN_OUT_FILE) {
1879 -
        rc = guest_path(
1880 -
            cpu,
1881 -
            pc,
1882 -
            "spawn",
1883 -
            decode_u64_le(rec + 56),
1884 -
            decode_u32_le(rec + 64),
1885 -
            err_path
1886 -
        );
1887 -
        if (rc)
1888 -
            return rc;
1889 -
    }
1890 -
1891 -
    /* Validate the argument slices and measure their bytes. */
1892 -
    if (!ecall_range(cpu, pc, "spawn", args_ptr, (u64)args_len * SLICE_SIZE))
1893 -
        return ECALL_FAULTED;
1894 -
    u64 pool_size = 0;
1895 -
    for (u32 i = 0; i < args_len; i++) {
1896 -
        const u8 *slice = &memory[args_ptr + (u64)i * SLICE_SIZE];
1897 -
        u64       ptr   = decode_u64_le(slice);
1898 -
        u32       len   = decode_u32_le(slice + 8);
1899 -
        if (!ecall_range(cpu, pc, "spawn", ptr, len))
1900 -
            return ECALL_FAULTED;
1901 -
        if (memchr(&memory[ptr], 0, len))
1902 -
            return HOST_E_INVALID;
1903 -
        pool_size += (u64)len + 1;
1904 -
    }
1905 -
1906 -
    /* Memory sizes are in KB. With `inherit`, zero sizes take the parent's. */
1907 -
    u64 memory_kb = decode_u32_le(rec + 80);
1908 -
    u64 data_kb   = decode_u32_le(rec + 84);
1909 -
    u64 stack_kb  = decode_u32_le(rec + 88);
1910 -
    if (flags & SPAWN_FLAG_INHERIT) {
1911 -
        if (!memory_kb)
1912 -
            memory_kb = g_opts.memory_size / 1024;
1913 -
        if (!data_kb)
1914 -
            data_kb = g_opts.data_memory_size / 1024;
1915 -
        if (!stack_kb)
1916 -
            stack_kb = g_opts.stack_size / 1024;
1917 -
    }
1918 -
    const struct {
1919 -
        const char *name;
1920 -
        u64         value;
1921 -
    } options[] = {
1922 -
        { "-harts=", decode_u32_le(rec + 76) },
1923 -
        { "-memory-size=", memory_kb },
1924 -
        { "-data-size=", data_kb },
1925 -
        { "-stack-size=", stack_kb },
1926 -
        { "-irq=", decode_u32_le(rec + 92) },
1927 -
        { "-irq-at=", decode_u64_le(rec + 96) },
1928 -
        { "-max-steps=", decode_u64_le(rec + 104) },
1929 -
    };
1930 -
1931 -
    /* The program name, emulator options, "--", the image and the guest
1932 -
     * arguments, which follow the image unchanged. */
1933 -
    enum { OPTION_COUNT = 7, FIXED_MAX = OPTION_COUNT + 5 };
1934 -
    char  numbers[OPTION_COUNT + 1][48];
1935 -
    char *fixed[FIXED_MAX];
1936 -
    int   nfixed   = 0;
1937 -
    int   nnumbers = 0;
1938 -
1939 -
    fixed[nfixed++] = "emulator";
1940 -
    fixed[nfixed++] = "-run";
1941 -
    if (mode == SPAWN_MODE_MACHINE)
1942 -
        fixed[nfixed++] = "-machine";
1943 -
    for (usize i = 0; i < sizeof options / sizeof options[0]; i++) {
1944 -
        if (options[i].value)
1945 -
            fixed[nfixed++] = format_option(
1946 -
                numbers[nnumbers++], options[i].name, options[i].value
1947 -
            );
1948 -
    }
1949 -
    fixed[nfixed++] =
1950 -
        format_option(numbers[nnumbers++], "-outcome-fd=", HOST_OUTCOME_FD);
1951 -
    fixed[nfixed++] = "--";
1952 -
1953 -
    char **argv = malloc(((usize)nfixed + 2 + args_len) * sizeof *argv);
1954 -
    char  *pool = malloc(pool_size ? pool_size : 1);
1955 -
    if (!argv || !pool) {
1956 -
        free(argv);
1957 -
        free(pool);
1958 -
        return HOST_E_IO;
1959 -
    }
1960 -
    int n = 0;
1961 -
    for (int i = 0; i < nfixed; i++)
1962 -
        argv[n++] = fixed[i];
1963 -
    argv[n++] = image;
1964 -
1965 -
    char *next = pool;
1966 -
    for (u32 i = 0; i < args_len; i++) {
1967 -
        const u8 *slice = &memory[args_ptr + (u64)i * SLICE_SIZE];
1968 -
        u64       ptr   = decode_u64_le(slice);
1969 -
        u32       len   = decode_u32_le(slice + 8);
1970 -
        memcpy(next, &memory[ptr], len);
1971 -
        next[len]  = '\0';
1972 -
        argv[n++]  = next;
1973 -
        next      += len + 1;
1974 -
    }
1975 -
    argv[n] = NULL;
1976 -
1977 -
    struct host_spawn req = {
1978 -
        .argv     = argv,
1979 -
        .out_kind = out_kind,
1980 -
        .out_path = out_path,
1981 -
        .err_kind = err_kind,
1982 -
        .err_path = err_path,
1983 -
    };
1984 -
    /* One path for both streams is opened once and shared. */
1985 -
    if (out_kind == SPAWN_OUT_FILE && err_kind == SPAWN_OUT_FILE &&
1986 -
        strcmp(out_path, err_path) == 0)
1987 -
        req.err_kind = HOST_OUT_MERGE;
1988 -
1989 -
    fflush(NULL);
1990 -
    rc = host_spawn(&req);
1991 -
    free(argv);
1992 -
    free(pool);
1993 -
    return rc;
1994 -
}
1995 -
1996 -
/* Run the host call selected by a7 and store its result in a0. */
1500 +
/* Run a system call. The host ABI and the Linux numbering of older programs
1501 +
 * use disjoint call numbers, so every program gets the calls it was built
1502 +
 * for. */
1997 1503
static void ecall_dispatch(struct cpu *cpu, u32 pc) {
1998 -
    u64  call = cpu->regs[A7];
1999 -
    u64  a0 = cpu->regs[A0], a1 = cpu->regs[A1];
2000 -
    u64  a2 = cpu->regs[A2], a3 = cpu->regs[A3];
2001 -
    i64  rc;
2002 -
    char path[HOST_PATH_MAX];
2003 -
    char other[HOST_PATH_MAX];
2004 -
2005 -
    switch (call) {
2006 -
    case ECALL_EXIT:
2007 -
        /* The status is already in a0. */
2008 -
        cpu->running = false;
2009 -
        return;
2010 -
    case ECALL_READ:
2011 -
    case ECALL_WRITE:
2012 -
        rc = ecall_transfer(cpu, pc, call == ECALL_WRITE, a0, a1, a2);
2013 -
        break;
2014 -
    case ECALL_OPEN:
2015 -
        rc = guest_path(cpu, pc, "open", a0, a1, path);
2016 -
        if (!rc)
2017 -
            rc = ecall_open(path, a2);
2018 -
        break;
2019 -
    case ECALL_CLOSE:
2020 -
        rc = ecall_close(a0);
2021 -
        break;
2022 -
    case ECALL_STAT:
2023 -
        rc = ecall_stat(cpu, pc, a0, a1, a2);
2024 -
        break;
2025 -
    case ECALL_MAKE_DIR:
2026 -
        rc = guest_path(cpu, pc, "makeDir", a0, a1, path);
2027 -
        if (!rc)
2028 -
            rc = host_make_dir(path);
2029 -
        break;
2030 -
    case ECALL_REMOVE:
2031 -
        rc = guest_path(cpu, pc, "remove", a0, a1, path);
2032 -
        if (!rc)
2033 -
            rc = host_remove(path);
2034 -
        break;
2035 -
    case ECALL_RENAME:
2036 -
        rc = guest_path(cpu, pc, "rename", a0, a1, path);
2037 -
        if (!rc)
2038 -
            rc = guest_path(cpu, pc, "rename", a2, a3, other);
2039 -
        if (!rc)
2040 -
            rc = host_rename(path, other);
2041 -
        break;
2042 -
    case ECALL_LIST_DIR:
2043 -
        rc = ecall_list_dir(cpu, pc, a0, a1, a2, a3);
2044 -
        break;
2045 -
    case ECALL_SPAWN:
2046 -
        rc = ecall_spawn(cpu, pc, a0);
2047 -
        break;
2048 -
    case ECALL_WAIT:
2049 -
        rc = ecall_wait(cpu, pc, false, a0, a1);
2050 -
        break;
2051 -
    case ECALL_WAIT_ANY:
2052 -
        rc = ecall_wait(cpu, pc, true, 0, a0);
2053 -
        break;
2054 -
    default:
2055 -
        rc = HOST_E_INVALID;
2056 -
        break;
2057 -
    }
2058 -
    /* A fault has already stopped the guest; leave its registers alone. */
2059 -
    if (rc != ECALL_FAULTED)
2060 -
        cpu->regs[A0] = (u64)rc;
1504 +
    if (cpu->regs[A7] >= ABI_HOST_BASE)
1505 +
        abi_host_call(cpu, pc);
1506 +
    else
1507 +
        abi_linux_call(cpu, pc);
2061 1508
}
2062 1509
2063 1510
/* Execute a single instruction. */
2064 1511
static void cpu_execute(struct cpu *cpu, enum display display, bool headless) {
2065 1512
    if (cpu_out_of_bounds(cpu)) {
hosted.h added +146 -0
1 +
/* Hosted mode: state shared by the emulator core, the image loaders of each
2 +
 * image format and the system calls of each call interface.
3 +
 *
4 +
 * Programs from every Radiance release run unchanged. An image format is
5 +
 * recognized by its magic bytes, and the two call interfaces use disjoint
6 +
 * call numbers, so neither needs a version field in the program. */
7 +
#ifndef HOSTED_H
8 +
#define HOSTED_H
9 +
10 +
#include <stdio.h>
11 +
12 +
#include "types.h"
13 +
/* After types.h, so that `bool` is the same type as in emulator.c, which
14 +
 * includes it through jit.h. */
15 +
#include <stdbool.h>
16 +
17 +
#include "machine.h"
18 +
#include "riscv.h"
19 +
20 +
/* Maximum physical memory size (384MB). The runtime can choose any size up to
21 +
 * this value with `-memory-size=...`. */
22 +
#define MEMORY_SIZE       (384 * 1024 * 1024)
23 +
/* Read-only data offset. */
24 +
#define DATA_RO_OFFSET    0x10000
25 +
/* Writable data region base, where data memory starts. */
26 +
#define DATA_MEMORY_START 0xFFFFF0
27 +
/* Maximum open files in guest runtime. */
28 +
#define MAX_OPEN_FILES    32
29 +
/* First call number of the host ABI. Linux call numbers lie below it. */
30 +
#define ABI_HOST_BASE     0x400
31 +
32 +
/* CPU state. */
33 +
struct cpu {
34 +
    u64      regs[REGISTERS];
35 +
    u32      pc;          /* Program counter. */
36 +
    u32      programsize; /* Size of loaded program. */
37 +
    instr_t *program;     /* Program instructions. */
38 +
    bool     running;     /* Execution status. */
39 +
    bool     faulted;     /* There was a fault in execution. */
40 +
    bool     ebreak;      /* Program terminated via EBREAK. */
41 +
    reg_t    modified;    /* Index of the last modified register. */
42 +
};
43 +
44 +
/* Guest memory sizes chosen on the command line, in bytes. */
45 +
struct guest_sizes {
46 +
    u32 memory; /* Physical memory. */
47 +
    u32 data;   /* Data memory, from DATA_MEMORY_START. */
48 +
    u32 stack;  /* Stack, at the end of memory. */
49 +
};
50 +
51 +
/* Where a hosted image was placed in guest memory. */
52 +
struct hosted_image {
53 +
    u32 code_base;  /* Address of the first instruction; execution starts. */
54 +
    u32 code_bytes; /* Bytes of code. */
55 +
    u32 data_bytes; /* Bytes of read-write data from DATA_MEMORY_START,
56 +
                     * including any zero-filled tail. */
57 +
};
58 +
59 +
/* CPU memory. */
60 +
extern u8 memory[MEMORY_SIZE];
61 +
62 +
/* Decode a little-endian word without relying on host alignment. */
63 +
static inline u32 decode_u32_le(const u8 *bytes) {
64 +
    return (u32)bytes[0] | ((u32)bytes[1] << 8) | ((u32)bytes[2] << 16) |
65 +
           ((u32)bytes[3] << 24);
66 +
}
67 +
68 +
/* Decode a little-endian doubleword without relying on host alignment. */
69 +
static inline u64 decode_u64_le(const u8 *bytes) {
70 +
    return (u64)decode_u32_le(bytes) | ((u64)decode_u32_le(bytes + 4) << 32);
71 +
}
72 +
73 +
/* Store a little-endian integer of `size` bytes. */
74 +
static inline void encode_le(u8 *bytes, u64 value, u32 size) {
75 +
    for (u32 i = 0; i < size; i++)
76 +
        bytes[i] = (u8)(value >> (8 * i));
77 +
}
78 +
79 +
/* Return the guest memory sizes. */
80 +
struct guest_sizes guest_sizes(void);
81 +
82 +
/* Return true if [addr, addr + len) lies in guest memory. Otherwise report the
83 +
 * named call as out of bounds, fault the guest at `pc` and return false. */
84 +
bool guest_check(struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len);
85 +
86 +
/* Add a host file descriptor to the guest table. Returns the guest descriptor,
87 +
 * or -1 when the table is full. */
88 +
int guest_fd_table_add(int host_fd);
89 +
90 +
/* Get the host descriptor of a guest descriptor, or -1 if it is not open. The
91 +
 * standard streams map to themselves. */
92 +
int guest_fd_table_get(int guest_fd);
93 +
94 +
/* Remove a guest descriptor from the table. */
95 +
void guest_fd_table_remove(int guest_fd);
96 +
97 +
/* Read exactly `size` bytes from `file` into guest memory at `offset`. */
98 +
void image_read(FILE *file, u32 offset, u32 size, const char *label);
99 +
100 +
/* Image loaders, one per image format. Each takes the open image of
101 +
 * `file_size` bytes, positioned anywhere, and places code and read-only data
102 +
 * in [DATA_RO_OFFSET, DATA_MEMORY_START) and read-write data in
103 +
 * [DATA_MEMORY_START, data_limit). They exit through `bail` on a malformed
104 +
 * image. */
105 +
106 +
/* Load a native image, which starts with the magic "RADIANCE". See
107 +
 * image_native.c. */
108 +
struct hosted_image image_native_load(
109 +
    FILE *file, long file_size, u32 data_limit
110 +
);
111 +
112 +
/* Load a native image in machine mode, where every segment must lie in the
113 +
 * `memory_size` bytes of RAM at MACHINE_RAM_BASE, and record its entry and
114 +
 * sections in `config`. See image_native.c. */
115 +
void image_native_load_machine(
116 +
    FILE *file, long file_size, u32 memory_size, struct machine_config *config
117 +
);
118 +
119 +
/* Load a "RAD0" version 1 image. See image_rad0.c. */
120 +
struct hosted_image image_rad0_load(FILE *file, long file_size, u32 data_limit);
121 +
122 +
/* Load a headerless image at `path`, with its data in sidecar files. See
123 +
 * image_raw.c. */
124 +
struct hosted_image image_raw_load(
125 +
    FILE *file, long file_size, const char *path, u32 data_limit
126 +
);
127 +
128 +
/* Return true if `header`, the first `len` bytes of an image, starts with the
129 +
 * native magic. */
130 +
bool image_native_match(const u8 *header, size_t len);
131 +
132 +
/* Return true if `header`, the first `len` bytes of an image, starts with the
133 +
 * "RAD0" magic. */
134 +
bool image_rad0_match(const u8 *header, size_t len);
135 +
136 +
/* System calls, one entry point per call interface. Each reads the call
137 +
 * number from a7 and its arguments from a0 and up, and stores the result in
138 +
 * a0 unless the call faulted or stopped the guest. */
139 +
140 +
/* Run a host ABI call, numbered from ABI_HOST_BASE. See abi_host.c. */
141 +
void abi_host_call(struct cpu *cpu, u32 pc);
142 +
143 +
/* Run a Linux-numbered call of older programs. See abi_linux.c. */
144 +
void abi_linux_call(struct cpu *cpu, u32 pc);
145 +
146 +
#endif
image_native.c added +125 -0
1 +
/* Native image format of current compilers.
2 +
 *
3 +
 * A 48-byte little-endian header, the magic "RADIANCE" followed by an address
4 +
 * and size for the code and read-only data and an address, size and
5 +
 * zero-filled tail for read-write data, then the bytes of the three segments.
6 +
 * Execution starts at the code address. */
7 +
#include <stdio.h>
8 +
9 +
#include "hosted.h"
10 +
#include "io.h"
11 +
#include "machine.h"
12 +
#include "types.h"
13 +
14 +
/* Native image magic: the ASCII bytes "RADIANCE" as a little-endian u64. */
15 +
#define NATIVE_IMAGE_MAGIC 0x45434E4149444152ULL
16 +
17 +
/* A native image segment: its address, the bytes stored in the file, and the
18 +
 * zero-filled bytes that follow them in memory. */
19 +
struct image_segment {
20 +
    u64 base;
21 +
    u32 size;
22 +
    u32 tail;
23 +
};
24 +
25 +
/* A decoded native image: the code, read-only data and read-write data
26 +
 * segments, in that order. Execution starts at the code address. */
27 +
struct image {
28 +
    struct image_segment segments[3];
29 +
};
30 +
31 +
/* The address range [low, high) that each segment must lie in. */
32 +
struct image_range {
33 +
    u64 low[3];
34 +
    u64 high[3];
35 +
};
36 +
37 +
bool image_native_match(const u8 *header, size_t len) {
38 +
    return len >= sizeof(u64) && decode_u64_le(header) == NATIVE_IMAGE_MAGIC;
39 +
}
40 +
41 +
/* Decode and check a native image of file_size bytes, leaving the file
42 +
 * positioned at the payload. The caller has already checked the magic. */
43 +
static struct image parse_image(
44 +
    FILE *file, long file_size, const struct image_range *range
45 +
) {
46 +
    enum { HEADER_BYTES = 48 };
47 +
    u8 header[HEADER_BYTES];
48 +
    rewind(file);
49 +
    if (fread(header, 1, sizeof(header), file) != sizeof(header))
50 +
        bail("truncated image header");
51 +
52 +
    struct image image;
53 +
    u64          expected = HEADER_BYTES;
54 +
    for (u32 i = 0; i < 3; i++) {
55 +
        const u8             *record  = header + 8 + i * 12;
56 +
        struct image_segment *seg     = &image.segments[i];
57 +
        seg->base                     = decode_u64_le(record);
58 +
        seg->size                     = decode_u32_le(record + 8);
59 +
        seg->tail                     = i == 2 ? decode_u32_le(record + 12) : 0;
60 +
        expected                     += seg->size;
61 +
        u64 extent                    = (u64)seg->size + seg->tail;
62 +
        if (extent &&
63 +
            (seg->base < range->low[i] || seg->base >= range->high[i] ||
64 +
             extent > range->high[i] - seg->base))
65 +
            bail("image segment outside memory");
66 +
    }
67 +
    if (expected != (u64)file_size)
68 +
        bail("image size does not match header");
69 +
    return image;
70 +
}
71 +
72 +
/* Read the stored bytes of each segment into memory, with ram_base the address
73 +
 * that memory offset 0 stands for. The zero-filled tails need no clearing:
74 +
 * guest memory is static and the image loads once. */
75 +
static void load_image_segments(
76 +
    FILE *file, const struct image *image, u64 ram_base
77 +
) {
78 +
    for (u32 i = 0; i < 3; i++) {
79 +
        const struct image_segment *seg = &image->segments[i];
80 +
        image_read(file, (u32)(seg->base - ram_base), seg->size, "native");
81 +
    }
82 +
}
83 +
84 +
void image_native_load_machine(
85 +
    FILE *file, long file_size, u32 memory_size, struct machine_config *config
86 +
) {
87 +
    u64                end   = MACHINE_RAM_BASE + memory_size;
88 +
    struct image_range range = {
89 +
        .low  = { MACHINE_RAM_BASE, MACHINE_RAM_BASE, MACHINE_RAM_BASE },
90 +
        .high = { end, end, end },
91 +
    };
92 +
    struct image image = parse_image(file, file_size, &range);
93 +
    load_image_segments(file, &image, MACHINE_RAM_BASE);
94 +
95 +
    config->entry                      = image.segments[0].base;
96 +
    struct machine_section *sections[] = { &config->text,
97 +
                                           &config->rodata,
98 +
                                           &config->rwdata };
99 +
    for (u32 i = 0; i < 3; i++) {
100 +
        sections[i]->base = image.segments[i].base;
101 +
        sections[i]->size = image.segments[i].size + image.segments[i].tail;
102 +
    }
103 +
}
104 +
105 +
struct hosted_image image_native_load(
106 +
    FILE *file, long file_size, u32 data_limit
107 +
) {
108 +
    struct image_range range = {
109 +
        .low  = { DATA_RO_OFFSET, DATA_RO_OFFSET, DATA_MEMORY_START },
110 +
        .high = { DATA_MEMORY_START, DATA_MEMORY_START, data_limit },
111 +
    };
112 +
    struct image image = parse_image(file, file_size, &range);
113 +
    load_image_segments(file, &image, 0);
114 +
115 +
    const struct image_segment *code   = &image.segments[0];
116 +
    const struct image_segment *rwdata = &image.segments[2];
117 +
    u32                         extent = rwdata->size + rwdata->tail;
118 +
    /* The environment block must lie beyond the end of read-write data. */
119 +
    return (struct hosted_image){
120 +
        .code_base  = (u32)code->base,
121 +
        .code_bytes = code->size,
122 +
        .data_bytes =
123 +
            extent ? (u32)(rwdata->base - DATA_MEMORY_START) + extent : 0,
124 +
    };
125 +
}
image_rad0.c added +72 -0
1 +
/* "RAD0" version 1 image format, written by compilers from April to October
2 +
 * 2026.
3 +
 *
4 +
 * A 20-byte little-endian header, the magic "RAD0", the version 1 and the
5 +
 * sizes of the text, read-only data and read-write data sections, then the
6 +
 * bytes of the three sections in that order. Read-only data loads at
7 +
 * DATA_RO_OFFSET, text at the next word after it, and read-write data at
8 +
 * DATA_MEMORY_START. Execution starts at the first instruction. */
9 +
#include <stdio.h>
10 +
11 +
#include "hosted.h"
12 +
#include "io.h"
13 +
#include "riscv.h"
14 +
#include "types.h"
15 +
16 +
/* The ASCII bytes "RAD0" as a little-endian u32. */
17 +
#define RAD0_MAGIC       0x30444152U
18 +
/* The only version of the format. */
19 +
#define RAD0_VERSION     1U
20 +
/* Size of the header. */
21 +
#define RAD0_HEADER_SIZE 20U
22 +
23 +
bool image_rad0_match(const u8 *header, size_t len) {
24 +
    return len >= sizeof(u32) && decode_u32_le(header) == RAD0_MAGIC;
25 +
}
26 +
27 +
struct hosted_image image_rad0_load(
28 +
    FILE *file, long file_size, u32 data_limit
29 +
) {
30 +
    u8 header[RAD0_HEADER_SIZE];
31 +
    rewind(file);
32 +
    if (fread(header, 1, sizeof(header), file) != sizeof(header))
33 +
        bail("truncated Radiance image header");
34 +
35 +
    u32 version = decode_u32_le(&header[4]);
36 +
    if (version != RAD0_VERSION)
37 +
        bail("unsupported Radiance image version: %u", version);
38 +
39 +
    u32 program_bytes = decode_u32_le(&header[8]);
40 +
    u32 rodata_bytes  = decode_u32_le(&header[12]);
41 +
    u32 data_bytes    = decode_u32_le(&header[16]);
42 +
    if (program_bytes == 0 || program_bytes % sizeof(instr_t) != 0)
43 +
        bail("invalid text section size in Radiance image: %u", program_bytes);
44 +
45 +
    u64 image_size =
46 +
        (u64)RAD0_HEADER_SIZE + program_bytes + rodata_bytes + data_bytes;
47 +
    if (image_size != (u64)file_size)
48 +
        bail(
49 +
            "Radiance image size does not match header: file has %ld "
50 +
            "bytes, header requires %llu",
51 +
            file_size,
52 +
            (unsigned long long)image_size
53 +
        );
54 +
    if ((u64)DATA_RO_OFFSET + rodata_bytes > DATA_MEMORY_START)
55 +
        bail("read-only data section exceeds available program memory");
56 +
57 +
    u32 program_base = align(DATA_RO_OFFSET + rodata_bytes, WORD_SIZE);
58 +
    if ((u64)program_base + program_bytes > DATA_MEMORY_START)
59 +
        bail("text section exceeds available program memory");
60 +
    if ((u64)DATA_MEMORY_START + data_bytes > data_limit)
61 +
        bail("read-write data section exceeds available data memory");
62 +
63 +
    image_read(file, program_base, program_bytes, "text");
64 +
    image_read(file, DATA_RO_OFFSET, rodata_bytes, "read-only data");
65 +
    image_read(file, DATA_MEMORY_START, data_bytes, "read-write data");
66 +
67 +
    return (struct hosted_image){
68 +
        .code_base  = program_base,
69 +
        .code_bytes = program_bytes,
70 +
        .data_bytes = data_bytes,
71 +
    };
72 +
}
image_raw.c added +80 -0
1 +
/* Headerless image format, written by compilers from February to April 2026.
2 +
 *
3 +
 * The image file holds only the code. Its read-only and read-write data are in
4 +
 * the optional sidecar files `<image>.ro.data` and `<image>.rw.data`, which the
5 +
 * compiler leaves out when a section is empty. Read-only data loads at
6 +
 * DATA_RO_OFFSET, code at the next word after it, and read-write data at
7 +
 * DATA_MEMORY_START. Execution starts at the first instruction.
8 +
 *
9 +
 * An image without the magic of another format is taken to be headerless. */
10 +
#include <limits.h>
11 +
#include <stdio.h>
12 +
13 +
#include "hosted.h"
14 +
#include "io.h"
15 +
#include "riscv.h"
16 +
#include "types.h"
17 +
18 +
#ifndef PATH_MAX
19 +
#define PATH_MAX 4096
20 +
#endif
21 +
22 +
/* Load the sidecar file `<path>.<suffix>` at `offset`, which must end at or
23 +
 * before `limit`, and return its size. A missing sidecar is empty. */
24 +
static u32 load_sidecar(
25 +
    const char *path, const char *suffix, u32 offset, u32 limit
26 +
) {
27 +
    char sidecar[PATH_MAX];
28 +
    if (snprintf(sidecar, sizeof(sidecar), "%s.%s", path, suffix) >=
29 +
        (int)sizeof(sidecar))
30 +
        bail("image path too long: '%s'", path);
31 +
32 +
    FILE *file = fopen(sidecar, "rb");
33 +
    if (!file)
34 +
        return 0;
35 +
    if (fseek(file, 0, SEEK_END) != 0) {
36 +
        fclose(file);
37 +
        bail("failed to seek '%s'", sidecar);
38 +
    }
39 +
    long size = ftell(file);
40 +
    if (size < 0 || fseek(file, 0, SEEK_SET) != 0) {
41 +
        fclose(file);
42 +
        bail("failed to determine the size of '%s'", sidecar);
43 +
    }
44 +
    if ((u64)offset + (u64)size > limit) {
45 +
        fclose(file);
46 +
        bail(
47 +
            "%s section too large: %ld bytes, at most %u bytes",
48 +
            suffix,
49 +
            size,
50 +
            limit - offset
51 +
        );
52 +
    }
53 +
    image_read(file, offset, (u32)size, suffix);
54 +
    fclose(file);
55 +
    return (u32)size;
56 +
}
57 +
58 +
struct hosted_image image_raw_load(
59 +
    FILE *file, long file_size, const char *path, u32 data_limit
60 +
) {
61 +
    if (file_size % sizeof(instr_t) != 0)
62 +
        bail("unrecognized Radiance image: '%s'", path);
63 +
64 +
    u32 rodata_bytes =
65 +
        load_sidecar(path, "ro.data", DATA_RO_OFFSET, DATA_MEMORY_START);
66 +
    u32 program_base = align(DATA_RO_OFFSET + rodata_bytes, WORD_SIZE);
67 +
    if ((u64)program_base + (u64)file_size > DATA_MEMORY_START)
68 +
        bail("text section exceeds available program memory");
69 +
70 +
    rewind(file);
71 +
    image_read(file, program_base, (u32)file_size, "text");
72 +
    u32 data_bytes =
73 +
        load_sidecar(path, "rw.data", DATA_MEMORY_START, data_limit);
74 +
75 +
    return (struct hosted_image){
76 +
        .code_base  = program_base,
77 +
        .code_bytes = (u32)file_size,
78 +
        .data_bytes = data_bytes,
79 +
    };
80 +
}