abi_host.c 12.2 KiB raw
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
#define ECALL_EXCHANGE 0x40D
29
30
/* Result of a call that faulted the guest: a0 is left unchanged. */
31
#define ECALL_FAULTED INT64_MIN
32
33
/* Size of the guest records exchanged with the host. */
34
#define SLICE_SIZE   16
35
#define STAT_SIZE    16
36
#define OUTCOME_SIZE 8
37
#define SPAWN_SIZE   128
38
39
/* Spawn record values. */
40
#define SPAWN_MODE_MACHINE 1
41
#define SPAWN_OUT_FILE     2
42
#define SPAWN_FLAG_INHERIT 1
43
44
/* Copy the guest string of `len` bytes at `addr` into `out`, which holds
45
 * HOST_PATH_MAX bytes, and terminate it. Returns 0, `HOST_E_INVALID` for an
46
 * embedded zero byte or a path that is too long, or `ECALL_FAULTED` after
47
 * faulting the guest. */
48
static i64 guest_path(
49
    struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len, char *out
50
) {
51
    if (!guest_check(cpu, pc, call, addr, len))
52
        return ECALL_FAULTED;
53
    if (len >= HOST_PATH_MAX || memchr(&memory[addr], 0, len))
54
        return HOST_E_INVALID;
55
    memcpy(out, &memory[addr], len);
56
    out[len] = '\0';
57
    return 0;
58
}
59
60
/* Open a file and give it a guest descriptor. */
61
static i64 ecall_open(const char *path, u64 mode) {
62
    if (mode > 1)
63
        return HOST_E_INVALID;
64
    i64 host_fd = mode ? host_open_write(path) : host_open_read(path);
65
    if (host_fd < 0)
66
        return host_fd;
67
    int guest_fd = guest_fd_table_add((int)host_fd);
68
    if (guest_fd < 0) {
69
        host_close((int)host_fd);
70
        return HOST_E_LIMIT;
71
    }
72
    return guest_fd;
73
}
74
75
/* Close a guest descriptor. The standard streams cannot be closed. */
76
static i64 ecall_close(u64 guest_fd) {
77
    if (guest_fd < 3 || guest_fd >= MAX_OPEN_FILES)
78
        return HOST_E_INVALID;
79
    int host_fd = guest_fd_table_get((int)guest_fd);
80
    if (host_fd < 0)
81
        return HOST_E_INVALID;
82
    guest_fd_table_remove((int)guest_fd);
83
    return host_close(host_fd) < 0 ? HOST_E_IO : 0;
84
}
85
86
/* Read or write guest memory through a guest descriptor. */
87
static i64 ecall_transfer(
88
    struct cpu *cpu, u32 pc, bool write, u64 guest_fd, u64 addr, u64 len
89
) {
90
    if (!guest_check(cpu, pc, write ? "write" : "read", addr, len))
91
        return ECALL_FAULTED;
92
    int host_fd =
93
        guest_fd < MAX_OPEN_FILES ? guest_fd_table_get((int)guest_fd) : -1;
94
    if (host_fd < 0)
95
        return HOST_E_INVALID;
96
    if (len == 0)
97
        return 0;
98
    return write ? host_write(host_fd, &memory[addr], (u32)len)
99
                 : host_read(host_fd, &memory[addr], (u32)len);
100
}
101
102
/* Describe a path into a guest Stat record. */
103
static i64 ecall_stat(
104
    struct cpu *cpu, u32 pc, u64 path_addr, u64 path_len, u64 out
105
) {
106
    char path[HOST_PATH_MAX];
107
    i64  rc = guest_path(cpu, pc, "stat", path_addr, path_len, path);
108
    if (rc)
109
        return rc;
110
    if (!guest_check(cpu, pc, "stat", out, STAT_SIZE))
111
        return ECALL_FAULTED;
112
    struct host_stat st;
113
    rc = host_stat(path, &st);
114
    if (rc < 0)
115
        return rc;
116
    encode_le(&memory[out], st.size, 8);
117
    encode_le(&memory[out + 8], st.kind, 4);
118
    encode_le(&memory[out + 12], 0, 4);
119
    return 0;
120
}
121
122
/* List a directory into a guest buffer. */
123
static i64 ecall_list_dir(
124
    struct cpu *cpu, u32 pc, u64 path_addr, u64 path_len, u64 buf, u64 buf_len
125
) {
126
    char path[HOST_PATH_MAX];
127
    i64  rc = guest_path(cpu, pc, "listDir", path_addr, path_len, path);
128
    if (rc)
129
        return rc;
130
    if (!guest_check(cpu, pc, "listDir", buf, buf_len))
131
        return ECALL_FAULTED;
132
    return host_list_dir(path, &memory[buf], (u32)buf_len);
133
}
134
135
/* Wait for one child, or for any child when `any` is set, and store its
136
 * outcome record in guest memory. */
137
static i64 ecall_wait(struct cpu *cpu, u32 pc, bool any, u64 child, u64 out) {
138
    if (!guest_check(cpu, pc, any ? "waitAny" : "wait", out, OUTCOME_SIZE))
139
        return ECALL_FAULTED;
140
    struct host_outcome outcome = { 0, 0 };
141
    /* Handles beyond the table are unknown whatever their width. */
142
    i64 rc =
143
        any ? host_wait_any(&outcome)
144
            : host_wait(child >= HOST_MAX_CHILDREN ? -1 : (i64)child, &outcome);
145
    if (rc < 0)
146
        return rc;
147
    encode_le(&memory[out], outcome.kind, 4);
148
    encode_le(&memory[out + 4], (u32)outcome.code, 4);
149
    return any ? rc : 0;
150
}
151
152
/* Format a numeric emulator option into `buf` and return it. */
153
static char *format_option(char *buf, const char *name, u64 value) {
154
    snprintf(buf, 48, "%s%llu", name, (unsigned long long)value);
155
    return buf;
156
}
157
158
/* Decode a spawn record at `spec` and start the child emulator. */
159
static i64 ecall_spawn(struct cpu *cpu, u32 pc, u64 spec) {
160
    if (!guest_check(cpu, pc, "spawn", spec, SPAWN_SIZE))
161
        return ECALL_FAULTED;
162
163
    const u8 *rec       = &memory[spec];
164
    u64       image_ptr = decode_u64_le(rec);
165
    u32       image_len = decode_u32_le(rec + 8);
166
    u64       args_ptr  = decode_u64_le(rec + 16);
167
    u32       args_len  = decode_u32_le(rec + 24);
168
    u32       out_kind  = decode_u32_le(rec + 32);
169
    u32       err_kind  = decode_u32_le(rec + 36);
170
    u32       mode      = decode_u32_le(rec + 72);
171
    u32       flags     = decode_u32_le(rec + 112);
172
173
    if (decode_u64_le(rec + 120) != 0 || mode > SPAWN_MODE_MACHINE ||
174
        out_kind > SPAWN_OUT_FILE || err_kind > SPAWN_OUT_FILE ||
175
        (flags & ~(u32)SPAWN_FLAG_INHERIT) != 0)
176
        return HOST_E_INVALID;
177
178
    char image[HOST_PATH_MAX];
179
    char out_path[HOST_PATH_MAX];
180
    char err_path[HOST_PATH_MAX];
181
    i64  rc = guest_path(cpu, pc, "spawn", image_ptr, image_len, image);
182
    if (rc)
183
        return rc;
184
    if (out_kind == SPAWN_OUT_FILE) {
185
        rc = guest_path(
186
            cpu,
187
            pc,
188
            "spawn",
189
            decode_u64_le(rec + 40),
190
            decode_u32_le(rec + 48),
191
            out_path
192
        );
193
        if (rc)
194
            return rc;
195
    }
196
    if (err_kind == SPAWN_OUT_FILE) {
197
        rc = guest_path(
198
            cpu,
199
            pc,
200
            "spawn",
201
            decode_u64_le(rec + 56),
202
            decode_u32_le(rec + 64),
203
            err_path
204
        );
205
        if (rc)
206
            return rc;
207
    }
208
209
    /* Validate the argument slices and measure their bytes. */
210
    if (!guest_check(cpu, pc, "spawn", args_ptr, (u64)args_len * SLICE_SIZE))
211
        return ECALL_FAULTED;
212
    u64 pool_size = 0;
213
    for (u32 i = 0; i < args_len; i++) {
214
        const u8 *slice = &memory[args_ptr + (u64)i * SLICE_SIZE];
215
        u64       ptr   = decode_u64_le(slice);
216
        u32       len   = decode_u32_le(slice + 8);
217
        if (!guest_check(cpu, pc, "spawn", ptr, len))
218
            return ECALL_FAULTED;
219
        if (memchr(&memory[ptr], 0, len))
220
            return HOST_E_INVALID;
221
        pool_size += (u64)len + 1;
222
    }
223
224
    /* Memory sizes are in KB. With `inherit`, zero sizes take the parent's. */
225
    u64 memory_kb = decode_u32_le(rec + 80);
226
    u64 data_kb   = decode_u32_le(rec + 84);
227
    u64 stack_kb  = decode_u32_le(rec + 88);
228
    if (flags & SPAWN_FLAG_INHERIT) {
229
        struct guest_sizes sizes = guest_sizes();
230
        if (!memory_kb)
231
            memory_kb = sizes.memory / 1024;
232
        if (!data_kb)
233
            data_kb = sizes.data / 1024;
234
        if (!stack_kb)
235
            stack_kb = sizes.stack / 1024;
236
    }
237
    const struct {
238
        const char *name;
239
        u64         value;
240
    } options[] = {
241
        { "-harts=", decode_u32_le(rec + 76) },
242
        { "-memory-size=", memory_kb },
243
        { "-data-size=", data_kb },
244
        { "-stack-size=", stack_kb },
245
        { "-irq=", decode_u32_le(rec + 92) },
246
        { "-irq-at=", decode_u64_le(rec + 96) },
247
        { "-max-steps=", decode_u64_le(rec + 104) },
248
    };
249
250
    /* The program name, emulator options, "--", the image and the guest
251
     * arguments, which follow the image unchanged. */
252
    enum { OPTION_COUNT = 7, FIXED_MAX = OPTION_COUNT + 5 };
253
    char  numbers[OPTION_COUNT + 1][48];
254
    char *fixed[FIXED_MAX];
255
    int   nfixed   = 0;
256
    int   nnumbers = 0;
257
258
    fixed[nfixed++] = "emulator";
259
    fixed[nfixed++] = "-run";
260
    if (mode == SPAWN_MODE_MACHINE)
261
        fixed[nfixed++] = "-machine";
262
    for (usize i = 0; i < sizeof options / sizeof options[0]; i++) {
263
        if (options[i].value)
264
            fixed[nfixed++] = format_option(
265
                numbers[nnumbers++], options[i].name, options[i].value
266
            );
267
    }
268
    fixed[nfixed++] =
269
        format_option(numbers[nnumbers++], "-outcome-fd=", HOST_OUTCOME_FD);
270
    fixed[nfixed++] = "--";
271
272
    char **argv = malloc(((usize)nfixed + 2 + args_len) * sizeof *argv);
273
    char  *pool = malloc(pool_size ? pool_size : 1);
274
    if (!argv || !pool) {
275
        free(argv);
276
        free(pool);
277
        return HOST_E_IO;
278
    }
279
    int n = 0;
280
    for (int i = 0; i < nfixed; i++)
281
        argv[n++] = fixed[i];
282
    argv[n++] = image;
283
284
    char *next = pool;
285
    for (u32 i = 0; i < args_len; i++) {
286
        const u8 *slice = &memory[args_ptr + (u64)i * SLICE_SIZE];
287
        u64       ptr   = decode_u64_le(slice);
288
        u32       len   = decode_u32_le(slice + 8);
289
        memcpy(next, &memory[ptr], len);
290
        next[len]  = '\0';
291
        argv[n++]  = next;
292
        next      += len + 1;
293
    }
294
    argv[n] = NULL;
295
296
    struct host_spawn req = {
297
        .argv     = argv,
298
        .out_kind = out_kind,
299
        .out_path = out_path,
300
        .err_kind = err_kind,
301
        .err_path = err_path,
302
    };
303
    /* One path for both streams is opened once and shared. */
304
    if (out_kind == SPAWN_OUT_FILE && err_kind == SPAWN_OUT_FILE &&
305
        strcmp(out_path, err_path) == 0)
306
        req.err_kind = HOST_OUT_MERGE;
307
308
    fflush(NULL);
309
    rc = host_spawn(&req);
310
    free(argv);
311
    free(pool);
312
    return rc;
313
}
314
315
/* Run the host call selected by a7 and store its result in a0. */
316
void abi_host_call(struct cpu *cpu, u32 pc) {
317
    u64  call = cpu->regs[A7];
318
    u64  a0 = cpu->regs[A0], a1 = cpu->regs[A1];
319
    u64  a2 = cpu->regs[A2], a3 = cpu->regs[A3];
320
    i64  rc;
321
    char path[HOST_PATH_MAX];
322
    char other[HOST_PATH_MAX];
323
324
    switch (call) {
325
    case ECALL_EXIT:
326
        /* The status is already in a0. */
327
        cpu->running = false;
328
        return;
329
    case ECALL_READ:
330
    case ECALL_WRITE:
331
        rc = ecall_transfer(cpu, pc, call == ECALL_WRITE, a0, a1, a2);
332
        break;
333
    case ECALL_OPEN:
334
        rc = guest_path(cpu, pc, "open", a0, a1, path);
335
        if (!rc)
336
            rc = ecall_open(path, a2);
337
        break;
338
    case ECALL_CLOSE:
339
        rc = ecall_close(a0);
340
        break;
341
    case ECALL_STAT:
342
        rc = ecall_stat(cpu, pc, a0, a1, a2);
343
        break;
344
    case ECALL_MAKE_DIR:
345
        rc = guest_path(cpu, pc, "makeDir", a0, a1, path);
346
        if (!rc)
347
            rc = host_make_dir(path);
348
        break;
349
    case ECALL_REMOVE:
350
        rc = guest_path(cpu, pc, "remove", a0, a1, path);
351
        if (!rc)
352
            rc = host_remove(path);
353
        break;
354
    case ECALL_RENAME:
355
    case ECALL_EXCHANGE: {
356
        const char *name = call == ECALL_RENAME ? "rename" : "exchange";
357
        rc               = guest_path(cpu, pc, name, a0, a1, path);
358
        if (!rc)
359
            rc = guest_path(cpu, pc, name, a2, a3, other);
360
        if (!rc)
361
            rc = call == ECALL_RENAME ? host_rename(path, other)
362
                                      : host_exchange(path, other);
363
        break;
364
    }
365
    case ECALL_LIST_DIR:
366
        rc = ecall_list_dir(cpu, pc, a0, a1, a2, a3);
367
        break;
368
    case ECALL_SPAWN:
369
        rc = ecall_spawn(cpu, pc, a0);
370
        break;
371
    case ECALL_WAIT:
372
        rc = ecall_wait(cpu, pc, false, a0, a1);
373
        break;
374
    case ECALL_WAIT_ANY:
375
        rc = ecall_wait(cpu, pc, true, 0, a0);
376
        break;
377
    default:
378
        rc = HOST_E_INVALID;
379
        break;
380
    }
381
    /* A fault has already stopped the guest; leave its registers alone. */
382
    if (rc != ECALL_FAULTED)
383
        cpu->regs[A0] = (u64)rc;
384
}