.githooks/
riscv/
test/
.clang-format
570 B
.gitignore
5 B
.gitsigners
112 B
AGENTS.md
227 B
LICENSE
1.1 KiB
Makefile
3.5 KiB
README
12.5 KiB
STYLE
781 B
abi_host.c
12.2 KiB
abi_linux.c
3.7 KiB
bitmanip.h
5.8 KiB
color.h
567 B
debug_map.c
5.1 KiB
debug_map.h
1.3 KiB
emulator.c
87.3 KiB
host.h
5.5 KiB
host_posix.c
11.2 KiB
hosted.h
5.7 KiB
image_native.c
4.6 KiB
image_rad0.c
2.7 KiB
image_raw.c
2.6 KiB
io.c
1.1 KiB
io.h
444 B
jit.c
99.2 KiB
jit.h
7.8 KiB
machine.c
4.4 KiB
machine.h
2.1 KiB
machine_cpu.c
13.6 KiB
machine_csr.c
4.6 KiB
machine_devices.c
10.8 KiB
machine_fdt.c
8.7 KiB
machine_internal.h
7.4 KiB
muldiv.h
1.0 KiB
profile.c
17.5 KiB
profile.h
1.4 KiB
riscv.c
755 B
riscv.h
4.3 KiB
types.h
1019 B
vector.c
54.0 KiB
vector.h
1.3 KiB
hosted.h
raw
| 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 "machine.h" |
| 13 | #include "riscv.h" |
| 14 | #include "types.h" |
| 15 | #include "vector.h" |
| 16 | |
| 17 | /* Maximum physical memory size (384MB). The runtime can choose any size up to |
| 18 | * this value with `-memory-size=...`. */ |
| 19 | #define MEMORY_SIZE (384 * 1024 * 1024) |
| 20 | /* Read-only data offset. */ |
| 21 | #define DATA_RO_OFFSET 0x10000 |
| 22 | /* Writable data region base, where data memory starts. */ |
| 23 | #define DATA_MEMORY_START 0xFFFFF0 |
| 24 | /* Maximum open files in guest runtime. */ |
| 25 | #define MAX_OPEN_FILES 32 |
| 26 | /* First call number of the host ABI. Linux call numbers lie below it. */ |
| 27 | #define ABI_HOST_BASE 0x400 |
| 28 | |
| 29 | /* CPU state. */ |
| 30 | struct cpu { |
| 31 | u64 regs[REGISTERS]; |
| 32 | u32 pc; /* Program counter. */ |
| 33 | u32 programsize; /* Size of loaded program. */ |
| 34 | instr_t *program; /* Program instructions. */ |
| 35 | bool running; /* Execution status. */ |
| 36 | bool faulted; /* There was a fault in execution. */ |
| 37 | bool ebreak; /* Program terminated via EBREAK. */ |
| 38 | reg_t modified; /* Index of the last modified register. */ |
| 39 | bool reserved; /* An LR reservation is held. */ |
| 40 | u32 reservation; /* Address of the reservation. */ |
| 41 | u32 reservation_size; /* Width of the reserved access in bytes. */ |
| 42 | |
| 43 | /* Integer vector registers and CSRs. */ |
| 44 | struct vector_state vector; |
| 45 | }; |
| 46 | |
| 47 | /* Guest memory sizes chosen on the command line, in bytes. */ |
| 48 | struct guest_sizes { |
| 49 | u32 memory; /* Physical memory. */ |
| 50 | u32 data; /* Data memory, from DATA_MEMORY_START. */ |
| 51 | u32 stack; /* Stack, at the end of memory. */ |
| 52 | }; |
| 53 | |
| 54 | /* Where a hosted image was placed in guest memory. */ |
| 55 | struct hosted_image { |
| 56 | u32 code_base; /* Address of the first instruction; execution starts. */ |
| 57 | u32 code_bytes; /* Bytes of code. */ |
| 58 | u32 data_bytes; /* Bytes of read-write data from DATA_MEMORY_START, |
| 59 | * including any zero-filled tail. */ |
| 60 | }; |
| 61 | |
| 62 | /* CPU memory. */ |
| 63 | extern u8 memory[MEMORY_SIZE]; |
| 64 | |
| 65 | /* Decode a little-endian word without relying on host alignment. */ |
| 66 | static inline u32 decode_u32_le(const u8 *bytes) { |
| 67 | return (u32)bytes[0] | ((u32)bytes[1] << 8) | ((u32)bytes[2] << 16) | |
| 68 | ((u32)bytes[3] << 24); |
| 69 | } |
| 70 | |
| 71 | /* Decode a little-endian doubleword without relying on host alignment. */ |
| 72 | static inline u64 decode_u64_le(const u8 *bytes) { |
| 73 | return (u64)decode_u32_le(bytes) | ((u64)decode_u32_le(bytes + 4) << 32); |
| 74 | } |
| 75 | |
| 76 | /* Store a little-endian integer of `size` bytes. */ |
| 77 | static inline void encode_le(u8 *bytes, u64 value, u32 size) { |
| 78 | for (u32 i = 0; i < size; i++) |
| 79 | bytes[i] = (u8)(value >> (8 * i)); |
| 80 | } |
| 81 | |
| 82 | /* Return the guest memory sizes. */ |
| 83 | struct guest_sizes guest_sizes(void); |
| 84 | |
| 85 | /* Return true if [addr, addr + len) lies in guest memory. Otherwise report the |
| 86 | * named call as out of bounds, fault the guest at `pc` and return false. */ |
| 87 | bool guest_check(struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len); |
| 88 | |
| 89 | /* Add a host file descriptor to the guest table. Returns the guest descriptor, |
| 90 | * or -1 when the table is full. */ |
| 91 | int guest_fd_table_add(int host_fd); |
| 92 | |
| 93 | /* Get the host descriptor of a guest descriptor, or -1 if it is not open. The |
| 94 | * standard streams map to themselves. */ |
| 95 | int guest_fd_table_get(int guest_fd); |
| 96 | |
| 97 | /* Remove a guest descriptor from the table. */ |
| 98 | void guest_fd_table_remove(int guest_fd); |
| 99 | |
| 100 | /* Read exactly `size` bytes from `file` into guest memory at `offset`. */ |
| 101 | void image_read(FILE *file, u32 offset, u32 size, const char *label); |
| 102 | |
| 103 | /* Image loaders, one per image format. Each takes the open image of |
| 104 | * `file_size` bytes, positioned anywhere, and places code and read-only data |
| 105 | * in [DATA_RO_OFFSET, DATA_MEMORY_START) and read-write data in |
| 106 | * [DATA_MEMORY_START, data_limit). They exit through `bail` on a malformed |
| 107 | * image. */ |
| 108 | |
| 109 | /* Load a native image, which starts with the magic "RADIANCE". See |
| 110 | * image_native.c. */ |
| 111 | struct hosted_image image_native_load( |
| 112 | FILE *file, long file_size, u32 data_limit |
| 113 | ); |
| 114 | |
| 115 | /* Load a native image in machine mode, where every segment must lie in the |
| 116 | * `memory_size` bytes of RAM at MACHINE_RAM_BASE, and record its entry and |
| 117 | * sections in `config`. See image_native.c. */ |
| 118 | void image_native_load_machine( |
| 119 | FILE *file, long file_size, u32 memory_size, struct machine_config *config |
| 120 | ); |
| 121 | |
| 122 | /* Load a "RAD0" version 1 image. See image_rad0.c. */ |
| 123 | struct hosted_image image_rad0_load(FILE *file, long file_size, u32 data_limit); |
| 124 | |
| 125 | /* Load a headerless image at `path`, with its data in sidecar files. See |
| 126 | * image_raw.c. */ |
| 127 | struct hosted_image image_raw_load( |
| 128 | FILE *file, long file_size, const char *path, u32 data_limit |
| 129 | ); |
| 130 | |
| 131 | /* Return true if `header`, the first `len` bytes of an image, starts with the |
| 132 | * native magic. */ |
| 133 | bool image_native_match(const u8 *header, size_t len); |
| 134 | |
| 135 | /* Return true if `header`, the first `len` bytes of an image, starts with the |
| 136 | * "RAD0" magic. */ |
| 137 | bool image_rad0_match(const u8 *header, size_t len); |
| 138 | |
| 139 | /* System calls, one entry point per call interface. Each reads the call |
| 140 | * number from a7 and its arguments from a0 and up, and stores the result in |
| 141 | * a0 unless the call faulted or stopped the guest. */ |
| 142 | |
| 143 | /* Run a host ABI call, numbered from ABI_HOST_BASE. See abi_host.c. */ |
| 144 | void abi_host_call(struct cpu *cpu, u32 pc); |
| 145 | |
| 146 | /* Run a Linux-numbered call of older programs. See abi_linux.c. */ |
| 147 | void abi_linux_call(struct cpu *cpu, u32 pc); |
| 148 | |
| 149 | #endif |