.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
machine_fdt.c
raw
| 1 | /* Flattened device tree describing the machine to the guest. */ |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "machine_internal.h" |
| 6 | |
| 7 | /* Fixed buffers bound every FDT operation independently of guest input. */ |
| 8 | struct fdt_builder { |
| 9 | u8 structure[24576]; |
| 10 | u8 strings[8192]; |
| 11 | u64 reservations[64]; |
| 12 | u32 structure_size; |
| 13 | u32 strings_size; |
| 14 | u32 reservation_count; |
| 15 | bool valid; |
| 16 | }; |
| 17 | |
| 18 | static void be32(u8 *out, u32 value) { |
| 19 | out[0] = value >> 24; |
| 20 | out[1] = value >> 16; |
| 21 | out[2] = value >> 8; |
| 22 | out[3] = value; |
| 23 | } |
| 24 | |
| 25 | static void be64(u8 *out, u64 value) { |
| 26 | be32(out, (u32)(value >> 32)); |
| 27 | be32(out + 4, (u32)value); |
| 28 | } |
| 29 | |
| 30 | static void bytes(struct fdt_builder *f, const void *data, u32 size) { |
| 31 | u32 padded = (size + 3) & ~3U; |
| 32 | if (padded > sizeof(f->structure) - f->structure_size) { |
| 33 | f->valid = false; |
| 34 | return; |
| 35 | } |
| 36 | memcpy(f->structure + f->structure_size, data, size); |
| 37 | memset(f->structure + f->structure_size + size, 0, padded - size); |
| 38 | f->structure_size += padded; |
| 39 | } |
| 40 | |
| 41 | static void word(struct fdt_builder *f, u32 value) { |
| 42 | u8 buffer[4]; |
| 43 | be32(buffer, value); |
| 44 | bytes(f, buffer, 4); |
| 45 | } |
| 46 | |
| 47 | static void begin(struct fdt_builder *f, const char *name) { |
| 48 | word(f, 1); |
| 49 | bytes(f, name, (u32)strlen(name) + 1); |
| 50 | } |
| 51 | |
| 52 | static void end(struct fdt_builder *f) { |
| 53 | word(f, 2); |
| 54 | } |
| 55 | |
| 56 | static void property( |
| 57 | struct fdt_builder *f, const char *name, const void *value, u32 size |
| 58 | ) { |
| 59 | u32 length = (u32)strlen(name) + 1; |
| 60 | if (length > sizeof(f->strings) - f->strings_size) { |
| 61 | f->valid = false; |
| 62 | return; |
| 63 | } |
| 64 | word(f, 3); |
| 65 | word(f, size); |
| 66 | word(f, f->strings_size); |
| 67 | memcpy(f->strings + f->strings_size, name, length); |
| 68 | f->strings_size += length; |
| 69 | bytes(f, value, size); |
| 70 | } |
| 71 | |
| 72 | static void string(struct fdt_builder *f, const char *name, const char *value) { |
| 73 | property(f, name, value, (u32)strlen(value) + 1); |
| 74 | } |
| 75 | |
| 76 | static void scalar(struct fdt_builder *f, const char *name, u32 value) { |
| 77 | u8 buffer[4]; |
| 78 | be32(buffer, value); |
| 79 | property(f, name, buffer, sizeof(buffer)); |
| 80 | } |
| 81 | |
| 82 | static void region( |
| 83 | struct fdt_builder *f, const char *name, u64 base, u64 size |
| 84 | ) { |
| 85 | u8 buffer[16]; |
| 86 | be64(buffer, base); |
| 87 | be64(buffer + 8, size); |
| 88 | property(f, name, buffer, sizeof(buffer)); |
| 89 | } |
| 90 | |
| 91 | static void reserved( |
| 92 | struct fdt_builder *f, const char *kind, u64 base, u64 size |
| 93 | ) { |
| 94 | if (!size) |
| 95 | return; |
| 96 | if (f->reservation_count + 2 > |
| 97 | sizeof(f->reservations) / sizeof(f->reservations[0])) { |
| 98 | f->valid = false; |
| 99 | return; |
| 100 | } |
| 101 | f->reservations[f->reservation_count++] = base; |
| 102 | f->reservations[f->reservation_count++] = size; |
| 103 | char name[64]; |
| 104 | snprintf(name, sizeof(name), "%s@%llx", kind, base); |
| 105 | begin(f, name); |
| 106 | region(f, "reg", base, size); |
| 107 | property(f, "no-map", "", 0); |
| 108 | end(f); |
| 109 | } |
| 110 | |
| 111 | bool machine_fdt(struct machine *m) { |
| 112 | struct fdt_builder f = { .valid = true }; |
| 113 | const struct machine_config *c = m->config; |
| 114 | begin(&f, ""); |
| 115 | scalar(&f, "#address-cells", 2); |
| 116 | scalar(&f, "#size-cells", 2); |
| 117 | string(&f, "compatible", "radiance,rv64-machine"); |
| 118 | string(&f, "model", "Radiance deterministic RV64IMA machine"); |
| 119 | |
| 120 | begin(&f, "chosen"); |
| 121 | string(&f, "stdout-path", "/soc/uart@10000000"); |
| 122 | u8 entry[8]; |
| 123 | be64(entry, c->entry); |
| 124 | property(&f, "radiance,kernel-entry", entry, sizeof(entry)); |
| 125 | region(&f, "radiance,kernel-text", c->text.base, c->text.size); |
| 126 | region(&f, "radiance,kernel-rodata", c->rodata.base, c->rodata.size); |
| 127 | region(&f, "radiance,kernel-rwdata", c->rwdata.base, c->rwdata.size); |
| 128 | region(&f, "radiance,fdt-reservation", MACHINE_FDT_BASE, MACHINE_FDT_SIZE); |
| 129 | u8 stacks[MACHINE_MAX_HARTS * 16]; |
| 130 | for (u32 id = 0; id < c->hart_count; id++) { |
| 131 | be64(stacks + id * 16, m->stack_base[id]); |
| 132 | be64(stacks + id * 16 + 8, c->stack_size); |
| 133 | } |
| 134 | property(&f, "radiance,kernel-stacks", stacks, c->hart_count * 16); |
| 135 | end(&f); |
| 136 | |
| 137 | char memory_name[40]; |
| 138 | snprintf(memory_name, sizeof(memory_name), "memory@%llx", MACHINE_RAM_BASE); |
| 139 | begin(&f, memory_name); |
| 140 | string(&f, "device_type", "memory"); |
| 141 | region(&f, "reg", MACHINE_RAM_BASE, c->memory_size); |
| 142 | end(&f); |
| 143 | |
| 144 | begin(&f, "reserved-memory"); |
| 145 | scalar(&f, "#address-cells", 2); |
| 146 | scalar(&f, "#size-cells", 2); |
| 147 | property(&f, "ranges", "", 0); |
| 148 | reserved(&f, "kernel-text", c->text.base, c->text.size); |
| 149 | reserved(&f, "kernel-rodata", c->rodata.base, c->rodata.size); |
| 150 | reserved(&f, "kernel-rwdata", c->rwdata.base, c->rwdata.size); |
| 151 | reserved(&f, "fdt", MACHINE_FDT_BASE, MACHINE_FDT_SIZE); |
| 152 | for (u32 id = 0; id < c->hart_count; id++) |
| 153 | reserved(&f, "kernel-stack", m->stack_base[id], c->stack_size); |
| 154 | reserved(&f, "clint-aperture", MACHINE_CLINT_BASE, MACHINE_CLINT_SIZE); |
| 155 | reserved(&f, "plic-aperture", MACHINE_PLIC_BASE, MACHINE_PLIC_SIZE); |
| 156 | /* The UART and finish pages. */ |
| 157 | reserved(&f, "console-aperture", MACHINE_UART_BASE, 2 * MACHINE_UART_SIZE); |
| 158 | end(&f); |
| 159 | |
| 160 | begin(&f, "cpus"); |
| 161 | scalar(&f, "#address-cells", 1); |
| 162 | scalar(&f, "#size-cells", 0); |
| 163 | scalar(&f, "timebase-frequency", MACHINE_TIMEBASE); |
| 164 | for (u32 id = 0; id < c->hart_count; id++) { |
| 165 | char name[24]; |
| 166 | snprintf(name, sizeof(name), "cpu@%x", id); |
| 167 | begin(&f, name); |
| 168 | string(&f, "device_type", "cpu"); |
| 169 | string(&f, "compatible", "riscv"); |
| 170 | scalar(&f, "reg", id); |
| 171 | string(&f, "status", "okay"); |
| 172 | string( |
| 173 | &f, |
| 174 | "riscv,isa", |
| 175 | "rv64ima_zba_zbb_zbs_zicsr_zifencei_zvbb_zve64x_zvl256b" |
| 176 | ); |
| 177 | begin(&f, "interrupt-controller"); |
| 178 | scalar(&f, "#interrupt-cells", 1); |
| 179 | property(&f, "interrupt-controller", "", 0); |
| 180 | string(&f, "compatible", "riscv,cpu-intc"); |
| 181 | scalar(&f, "phandle", id + 1); |
| 182 | end(&f); |
| 183 | end(&f); |
| 184 | } |
| 185 | end(&f); |
| 186 | |
| 187 | begin(&f, "soc"); |
| 188 | scalar(&f, "#address-cells", 2); |
| 189 | scalar(&f, "#size-cells", 2); |
| 190 | string(&f, "compatible", "simple-bus"); |
| 191 | property(&f, "ranges", "", 0); |
| 192 | begin(&f, "clint@2000000"); |
| 193 | string(&f, "compatible", "riscv,clint0"); |
| 194 | region(&f, "reg", MACHINE_CLINT_BASE, MACHINE_CLINT_SIZE); |
| 195 | u8 local_interrupts[MACHINE_MAX_HARTS * 16]; |
| 196 | for (u32 id = 0; id < c->hart_count; id++) { |
| 197 | be32(local_interrupts + id * 16, id + 1); |
| 198 | be32(local_interrupts + id * 16 + 4, 3); |
| 199 | be32(local_interrupts + id * 16 + 8, id + 1); |
| 200 | be32(local_interrupts + id * 16 + 12, 7); |
| 201 | } |
| 202 | property(&f, "interrupts-extended", local_interrupts, c->hart_count * 16); |
| 203 | end(&f); |
| 204 | |
| 205 | begin(&f, "plic@c000000"); |
| 206 | string(&f, "compatible", "riscv,plic0"); |
| 207 | region(&f, "reg", MACHINE_PLIC_BASE, MACHINE_PLIC_SIZE); |
| 208 | scalar(&f, "#interrupt-cells", 1); |
| 209 | scalar(&f, "#address-cells", 0); |
| 210 | property(&f, "interrupt-controller", "", 0); |
| 211 | scalar(&f, "phandle", 0x100); |
| 212 | scalar(&f, "riscv,ndev", MACHINE_IRQ_COUNT); |
| 213 | scalar(&f, "riscv,max-priority", 7); |
| 214 | u8 external_interrupts[MACHINE_MAX_HARTS * 8]; |
| 215 | for (u32 id = 0; id < c->hart_count; id++) { |
| 216 | be32(external_interrupts + id * 8, id + 1); |
| 217 | be32(external_interrupts + id * 8 + 4, 11); |
| 218 | } |
| 219 | property(&f, "interrupts-extended", external_interrupts, c->hart_count * 8); |
| 220 | end(&f); |
| 221 | |
| 222 | begin(&f, "uart@10000000"); |
| 223 | string(&f, "compatible", "ns16550a"); |
| 224 | region(&f, "reg", MACHINE_UART_BASE, 0x100); |
| 225 | scalar(&f, "clock-frequency", 1843200); |
| 226 | scalar(&f, "current-speed", 115200); |
| 227 | scalar(&f, "reg-shift", 0); |
| 228 | scalar(&f, "reg-io-width", 1); |
| 229 | scalar(&f, "interrupt-parent", 0x100); |
| 230 | scalar(&f, "interrupts", MACHINE_UART_IRQ); |
| 231 | end(&f); |
| 232 | |
| 233 | begin(&f, "finish@10001000"); |
| 234 | string(&f, "compatible", "radiant,finish"); |
| 235 | region(&f, "reg", MACHINE_FINISH_BASE, 4); |
| 236 | scalar(&f, "radiance,success-value", 0x5555); |
| 237 | scalar(&f, "radiance,failure-mask", 0xffff); |
| 238 | scalar(&f, "radiance,failure-value", 0x3333); |
| 239 | end(&f); |
| 240 | end(&f); |
| 241 | end(&f); |
| 242 | word(&f, 9); |
| 243 | |
| 244 | u32 reserve_size = (f.reservation_count + 2) * 8; |
| 245 | u32 structure_offset = 40 + reserve_size; |
| 246 | u32 strings_offset = structure_offset + f.structure_size; |
| 247 | u32 total = strings_offset + f.strings_size; |
| 248 | if (!f.valid || total > MACHINE_FDT_SIZE) |
| 249 | return false; |
| 250 | u8 *out = c->ram + (MACHINE_FDT_BASE - MACHINE_RAM_BASE); |
| 251 | memset(out, 0, MACHINE_FDT_SIZE); |
| 252 | const u32 header[] = { |
| 253 | 0xd00dfeed, total, structure_offset, strings_offset, 40, 17, |
| 254 | 16, 0, f.strings_size, f.structure_size |
| 255 | }; |
| 256 | for (u32 i = 0; i < 10; i++) |
| 257 | be32(out + i * 4, header[i]); |
| 258 | for (u32 i = 0; i < f.reservation_count; i++) |
| 259 | be64(out + 40 + i * 8, f.reservations[i]); |
| 260 | memcpy(out + structure_offset, f.structure, f.structure_size); |
| 261 | memcpy(out + strings_offset, f.strings, f.strings_size); |
| 262 | return true; |
| 263 | } |