/* Host ABI of current programs, numbered from ABI_HOST_BASE (0x400). * * The call number is in a7, arguments in a0..a3 and the result in a0; a * negative result is a host-neutral `HOST_E_*` error code. Paths are a pointer * and a length. See README. */ #include #include #include #include #include "host.h" #include "hosted.h" #include "types.h" #define ECALL_EXIT 0x400 #define ECALL_READ 0x401 #define ECALL_WRITE 0x402 #define ECALL_OPEN 0x403 #define ECALL_CLOSE 0x404 #define ECALL_STAT 0x405 #define ECALL_MAKE_DIR 0x406 #define ECALL_REMOVE 0x407 #define ECALL_RENAME 0x408 #define ECALL_LIST_DIR 0x409 #define ECALL_SPAWN 0x40A #define ECALL_WAIT 0x40B #define ECALL_WAIT_ANY 0x40C #define ECALL_EXCHANGE 0x40D /* Result of a call that faulted the guest: a0 is left unchanged. */ #define ECALL_FAULTED INT64_MIN /* Size of the guest records exchanged with the host. */ #define SLICE_SIZE 16 #define STAT_SIZE 16 #define OUTCOME_SIZE 8 #define SPAWN_SIZE 128 /* Spawn record values. */ #define SPAWN_MODE_MACHINE 1 #define SPAWN_OUT_FILE 2 #define SPAWN_FLAG_INHERIT 1 /* Copy the guest string of `len` bytes at `addr` into `out`, which holds * HOST_PATH_MAX bytes, and terminate it. Returns 0, `HOST_E_INVALID` for an * embedded zero byte or a path that is too long, or `ECALL_FAULTED` after * faulting the guest. */ static i64 guest_path( struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len, char *out ) { if (!guest_check(cpu, pc, call, addr, len)) return ECALL_FAULTED; if (len >= HOST_PATH_MAX || memchr(&memory[addr], 0, len)) return HOST_E_INVALID; memcpy(out, &memory[addr], len); out[len] = '\0'; return 0; } /* Open a file and give it a guest descriptor. */ static i64 ecall_open(const char *path, u64 mode) { if (mode > 1) return HOST_E_INVALID; i64 host_fd = mode ? host_open_write(path) : host_open_read(path); if (host_fd < 0) return host_fd; int guest_fd = guest_fd_table_add((int)host_fd); if (guest_fd < 0) { host_close((int)host_fd); return HOST_E_LIMIT; } return guest_fd; } /* Close a guest descriptor. The standard streams cannot be closed. */ static i64 ecall_close(u64 guest_fd) { if (guest_fd < 3 || guest_fd >= MAX_OPEN_FILES) return HOST_E_INVALID; int host_fd = guest_fd_table_get((int)guest_fd); if (host_fd < 0) return HOST_E_INVALID; guest_fd_table_remove((int)guest_fd); return host_close(host_fd) < 0 ? HOST_E_IO : 0; } /* Read or write guest memory through a guest descriptor. */ static i64 ecall_transfer( struct cpu *cpu, u32 pc, bool write, u64 guest_fd, u64 addr, u64 len ) { if (!guest_check(cpu, pc, write ? "write" : "read", addr, len)) return ECALL_FAULTED; int host_fd = guest_fd < MAX_OPEN_FILES ? guest_fd_table_get((int)guest_fd) : -1; if (host_fd < 0) return HOST_E_INVALID; if (len == 0) return 0; return write ? host_write(host_fd, &memory[addr], (u32)len) : host_read(host_fd, &memory[addr], (u32)len); } /* Describe a path into a guest Stat record. */ static i64 ecall_stat( struct cpu *cpu, u32 pc, u64 path_addr, u64 path_len, u64 out ) { char path[HOST_PATH_MAX]; i64 rc = guest_path(cpu, pc, "stat", path_addr, path_len, path); if (rc) return rc; if (!guest_check(cpu, pc, "stat", out, STAT_SIZE)) return ECALL_FAULTED; struct host_stat st; rc = host_stat(path, &st); if (rc < 0) return rc; encode_le(&memory[out], st.size, 8); encode_le(&memory[out + 8], st.kind, 4); encode_le(&memory[out + 12], 0, 4); return 0; } /* List a directory into a guest buffer. */ static i64 ecall_list_dir( struct cpu *cpu, u32 pc, u64 path_addr, u64 path_len, u64 buf, u64 buf_len ) { char path[HOST_PATH_MAX]; i64 rc = guest_path(cpu, pc, "listDir", path_addr, path_len, path); if (rc) return rc; if (!guest_check(cpu, pc, "listDir", buf, buf_len)) return ECALL_FAULTED; return host_list_dir(path, &memory[buf], (u32)buf_len); } /* Wait for one child, or for any child when `any` is set, and store its * outcome record in guest memory. */ static i64 ecall_wait(struct cpu *cpu, u32 pc, bool any, u64 child, u64 out) { if (!guest_check(cpu, pc, any ? "waitAny" : "wait", out, OUTCOME_SIZE)) return ECALL_FAULTED; struct host_outcome outcome = { 0, 0 }; /* Handles beyond the table are unknown whatever their width. */ i64 rc = any ? host_wait_any(&outcome) : host_wait(child >= HOST_MAX_CHILDREN ? -1 : (i64)child, &outcome); if (rc < 0) return rc; encode_le(&memory[out], outcome.kind, 4); encode_le(&memory[out + 4], (u32)outcome.code, 4); return any ? rc : 0; } /* Format a numeric emulator option into `buf` and return it. */ static char *format_option(char *buf, const char *name, u64 value) { snprintf(buf, 48, "%s%llu", name, (unsigned long long)value); return buf; } /* Decode a spawn record at `spec` and start the child emulator. */ static i64 ecall_spawn(struct cpu *cpu, u32 pc, u64 spec) { if (!guest_check(cpu, pc, "spawn", spec, SPAWN_SIZE)) return ECALL_FAULTED; const u8 *rec = &memory[spec]; u64 image_ptr = decode_u64_le(rec); u32 image_len = decode_u32_le(rec + 8); u64 args_ptr = decode_u64_le(rec + 16); u32 args_len = decode_u32_le(rec + 24); u32 out_kind = decode_u32_le(rec + 32); u32 err_kind = decode_u32_le(rec + 36); u32 mode = decode_u32_le(rec + 72); u32 flags = decode_u32_le(rec + 112); if (decode_u64_le(rec + 120) != 0 || mode > SPAWN_MODE_MACHINE || out_kind > SPAWN_OUT_FILE || err_kind > SPAWN_OUT_FILE || (flags & ~(u32)SPAWN_FLAG_INHERIT) != 0) return HOST_E_INVALID; char image[HOST_PATH_MAX]; char out_path[HOST_PATH_MAX]; char err_path[HOST_PATH_MAX]; i64 rc = guest_path(cpu, pc, "spawn", image_ptr, image_len, image); if (rc) return rc; if (out_kind == SPAWN_OUT_FILE) { rc = guest_path( cpu, pc, "spawn", decode_u64_le(rec + 40), decode_u32_le(rec + 48), out_path ); if (rc) return rc; } if (err_kind == SPAWN_OUT_FILE) { rc = guest_path( cpu, pc, "spawn", decode_u64_le(rec + 56), decode_u32_le(rec + 64), err_path ); if (rc) return rc; } /* Validate the argument slices and measure their bytes. */ if (!guest_check(cpu, pc, "spawn", args_ptr, (u64)args_len * SLICE_SIZE)) return ECALL_FAULTED; u64 pool_size = 0; for (u32 i = 0; i < args_len; i++) { const u8 *slice = &memory[args_ptr + (u64)i * SLICE_SIZE]; u64 ptr = decode_u64_le(slice); u32 len = decode_u32_le(slice + 8); if (!guest_check(cpu, pc, "spawn", ptr, len)) return ECALL_FAULTED; if (memchr(&memory[ptr], 0, len)) return HOST_E_INVALID; pool_size += (u64)len + 1; } /* Memory sizes are in KB. With `inherit`, zero sizes take the parent's. */ u64 memory_kb = decode_u32_le(rec + 80); u64 data_kb = decode_u32_le(rec + 84); u64 stack_kb = decode_u32_le(rec + 88); if (flags & SPAWN_FLAG_INHERIT) { struct guest_sizes sizes = guest_sizes(); if (!memory_kb) memory_kb = sizes.memory / 1024; if (!data_kb) data_kb = sizes.data / 1024; if (!stack_kb) stack_kb = sizes.stack / 1024; } const struct { const char *name; u64 value; } options[] = { { "-harts=", decode_u32_le(rec + 76) }, { "-memory-size=", memory_kb }, { "-data-size=", data_kb }, { "-stack-size=", stack_kb }, { "-irq=", decode_u32_le(rec + 92) }, { "-irq-at=", decode_u64_le(rec + 96) }, { "-max-steps=", decode_u64_le(rec + 104) }, }; /* The program name, emulator options, "--", the image and the guest * arguments, which follow the image unchanged. */ enum { OPTION_COUNT = 7, FIXED_MAX = OPTION_COUNT + 5 }; char numbers[OPTION_COUNT + 1][48]; char *fixed[FIXED_MAX]; int nfixed = 0; int nnumbers = 0; fixed[nfixed++] = "emulator"; fixed[nfixed++] = "-run"; if (mode == SPAWN_MODE_MACHINE) fixed[nfixed++] = "-machine"; for (usize i = 0; i < sizeof options / sizeof options[0]; i++) { if (options[i].value) fixed[nfixed++] = format_option( numbers[nnumbers++], options[i].name, options[i].value ); } fixed[nfixed++] = format_option(numbers[nnumbers++], "-outcome-fd=", HOST_OUTCOME_FD); fixed[nfixed++] = "--"; char **argv = malloc(((usize)nfixed + 2 + args_len) * sizeof *argv); char *pool = malloc(pool_size ? pool_size : 1); if (!argv || !pool) { free(argv); free(pool); return HOST_E_IO; } int n = 0; for (int i = 0; i < nfixed; i++) argv[n++] = fixed[i]; argv[n++] = image; char *next = pool; for (u32 i = 0; i < args_len; i++) { const u8 *slice = &memory[args_ptr + (u64)i * SLICE_SIZE]; u64 ptr = decode_u64_le(slice); u32 len = decode_u32_le(slice + 8); memcpy(next, &memory[ptr], len); next[len] = '\0'; argv[n++] = next; next += len + 1; } argv[n] = NULL; struct host_spawn req = { .argv = argv, .out_kind = out_kind, .out_path = out_path, .err_kind = err_kind, .err_path = err_path, }; /* One path for both streams is opened once and shared. */ if (out_kind == SPAWN_OUT_FILE && err_kind == SPAWN_OUT_FILE && strcmp(out_path, err_path) == 0) req.err_kind = HOST_OUT_MERGE; fflush(NULL); rc = host_spawn(&req); free(argv); free(pool); return rc; } /* Run the host call selected by a7 and store its result in a0. */ void abi_host_call(struct cpu *cpu, u32 pc) { u64 call = cpu->regs[A7]; u64 a0 = cpu->regs[A0], a1 = cpu->regs[A1]; u64 a2 = cpu->regs[A2], a3 = cpu->regs[A3]; i64 rc; char path[HOST_PATH_MAX]; char other[HOST_PATH_MAX]; switch (call) { case ECALL_EXIT: /* The status is already in a0. */ cpu->running = false; return; case ECALL_READ: case ECALL_WRITE: rc = ecall_transfer(cpu, pc, call == ECALL_WRITE, a0, a1, a2); break; case ECALL_OPEN: rc = guest_path(cpu, pc, "open", a0, a1, path); if (!rc) rc = ecall_open(path, a2); break; case ECALL_CLOSE: rc = ecall_close(a0); break; case ECALL_STAT: rc = ecall_stat(cpu, pc, a0, a1, a2); break; case ECALL_MAKE_DIR: rc = guest_path(cpu, pc, "makeDir", a0, a1, path); if (!rc) rc = host_make_dir(path); break; case ECALL_REMOVE: rc = guest_path(cpu, pc, "remove", a0, a1, path); if (!rc) rc = host_remove(path); break; case ECALL_RENAME: case ECALL_EXCHANGE: { const char *name = call == ECALL_RENAME ? "rename" : "exchange"; rc = guest_path(cpu, pc, name, a0, a1, path); if (!rc) rc = guest_path(cpu, pc, name, a2, a3, other); if (!rc) rc = call == ECALL_RENAME ? host_rename(path, other) : host_exchange(path, other); break; } case ECALL_LIST_DIR: rc = ecall_list_dir(cpu, pc, a0, a1, a2, a3); break; case ECALL_SPAWN: rc = ecall_spawn(cpu, pc, a0); break; case ECALL_WAIT: rc = ecall_wait(cpu, pc, false, a0, a1); break; case ECALL_WAIT_ANY: rc = ecall_wait(cpu, pc, true, 0, a0); break; default: rc = HOST_E_INVALID; break; } /* A fault has already stopped the guest; leave its registers alone. */ if (rc != ECALL_FAULTED) cpu->regs[A0] = (u64)rc; }