/* Linux-numbered calls of programs built before the host ABI, up to October * 2026. * * The call number is in a7 and arguments in a0..a3, with the numbers and * argument order of Linux on RV64. Only the calls these programs make exist: * * 56 openat(dirfd, path, flags, mode) descriptor, or -1 * 57 close(fd) 0, or -1 * 63 read(fd, buf, len) bytes read, 0 on failure * 64 write(fd, buf, len) bytes written, 0 on failure * 93 exit(status) * * `openat` ignores `dirfd` and `mode`, and takes a zero-terminated path. Its * flags use Linux values; the programs only open a file for reading, or for * writing with O_CREAT and O_TRUNC, and other flags fail. Other call numbers * return -ENOSYS. */ #include #include "host.h" #include "hosted.h" #include "types.h" #define LINUX_OPENAT 56 #define LINUX_CLOSE 57 #define LINUX_READ 63 #define LINUX_WRITE 64 #define LINUX_EXIT 93 /* Linux `open` flag values. */ #define LINUX_O_ACCMODE 03 #define LINUX_O_RDONLY 00 #define LINUX_O_WRONLY 01 #define LINUX_O_CREAT 0100 #define LINUX_O_TRUNC 01000 /* Linux error number for an unknown call. */ #define LINUX_ENOSYS 38 /* Open the zero-terminated guest path at `addr` and give it a guest * descriptor. Returns -1 on any failure. */ static i64 linux_openat(u64 addr, u64 flags) { u32 size = guest_sizes().memory; if (addr >= size) return -1; const u8 *end = memchr(&memory[addr], 0, size - addr); if (!end || (usize)(end - &memory[addr]) >= HOST_PATH_MAX) return -1; const char *path = (const char *)&memory[addr]; i64 host_fd; if (flags == LINUX_O_RDONLY) host_fd = host_open_read(path); else if (flags == (LINUX_O_WRONLY | LINUX_O_CREAT | LINUX_O_TRUNC)) host_fd = host_open_write(path); else return -1; if (host_fd < 0) return -1; int guest_fd = guest_fd_table_add((int)host_fd); if (guest_fd < 0) { host_close((int)host_fd); return -1; } return guest_fd; } /* Close a guest descriptor. Closing a standard stream does nothing. */ static i64 linux_close(i64 guest_fd) { if (guest_fd >= 0 && guest_fd < 3) return 0; int host_fd = guest_fd_table_get((int)guest_fd); if (host_fd < 0) return -1; guest_fd_table_remove((int)guest_fd); return host_close(host_fd) < 0 ? -1 : 0; } /* Read or write guest memory through a guest descriptor. Returns the bytes * transferred, 0 on failure, or -1 after faulting the guest. */ static i64 linux_transfer( struct cpu *cpu, u32 pc, bool write, i64 guest_fd, u64 addr, u64 len ) { if (!guest_check(cpu, pc, write ? "write" : "read", addr, len)) return -1; int host_fd = guest_fd_table_get((int)guest_fd); if (host_fd < 0 || len == 0) return 0; i64 n = write ? host_write(host_fd, &memory[addr], (u32)len) : host_read(host_fd, &memory[addr], (u32)len); return n < 0 ? 0 : n; } void abi_linux_call(struct cpu *cpu, u32 pc) { u64 call = cpu->regs[A7]; u64 a0 = cpu->regs[A0], a1 = cpu->regs[A1], a2 = cpu->regs[A2]; i64 rc; switch (call) { case LINUX_EXIT: /* The status is already in a0. */ cpu->running = false; return; case LINUX_READ: case LINUX_WRITE: rc = linux_transfer(cpu, pc, call == LINUX_WRITE, (i64)a0, a1, a2); if (rc < 0) return; break; case LINUX_OPENAT: rc = linux_openat(a1, a2); break; case LINUX_CLOSE: rc = linux_close((i64)a0); break; default: rc = -LINUX_ENOSYS; break; } cpu->regs[A0] = (u64)rc; }