abi_linux.c 3.7 KiB raw
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
}