.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.c
raw
| 1 | /* Machine layout, boot and the round-robin hart scheduler. */ |
| 2 | #include <stdint.h> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include "machine_internal.h" |
| 6 | |
| 7 | /* Return whether [base, base + size) overlaps any of the first n sections. */ |
| 8 | static bool overlaps_any( |
| 9 | u64 base, u32 size, const struct machine_section *sections, u32 n |
| 10 | ) { |
| 11 | for (u32 i = 0; i < n; i++) { |
| 12 | if (machine_overlap(base, size, sections[i].base, sections[i].size)) |
| 13 | return true; |
| 14 | } |
| 15 | return false; |
| 16 | } |
| 17 | |
| 18 | /* Place each hart's stack below the top of RAM. Reject a layout in which the |
| 19 | * FDT reservation, a stack and a loaded section collide, before the FDT or a |
| 20 | * stack can overwrite the image. The loader has already placed the sections |
| 21 | * in RAM without overlapping each other. */ |
| 22 | static bool layout_init(struct machine *m) { |
| 23 | const struct machine_config *c = m->config; |
| 24 | struct machine_section used[] = { |
| 25 | { MACHINE_FDT_BASE, MACHINE_FDT_SIZE }, c->text, c->rodata, c->rwdata |
| 26 | }; |
| 27 | if ((c->stack_size & 15) || |
| 28 | c->memory_size < MACHINE_FDT_BASE + MACHINE_FDT_SIZE - MACHINE_RAM_BASE) |
| 29 | return false; |
| 30 | for (u32 i = 1; i < 4; i++) { |
| 31 | if (overlaps_any(used[i].base, used[i].size, used, 1)) |
| 32 | return false; |
| 33 | } |
| 34 | u64 top = (MACHINE_RAM_BASE + c->memory_size) & ~15ULL; |
| 35 | for (u32 id = 0; id < c->hart_count; id++) { |
| 36 | if (top - MACHINE_RAM_BASE < c->stack_size) |
| 37 | return false; |
| 38 | u64 base = top - c->stack_size; |
| 39 | if (overlaps_any(base, c->stack_size, used, 4)) |
| 40 | return false; |
| 41 | m->stack_base[id] = base; |
| 42 | top = base; |
| 43 | } |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | /* Advance virtual time. */ |
| 48 | static void advance(struct machine *m, u64 delta) { |
| 49 | m->ticks += delta; |
| 50 | m->clint.time += delta; |
| 51 | } |
| 52 | |
| 53 | int machine_run(const struct machine_config *config) { |
| 54 | struct machine m = { .config = config }; |
| 55 | if (!layout_init(&m)) { |
| 56 | fprintf( |
| 57 | stderr, |
| 58 | "invalid machine layout: sections, aligned stacks and the FDT " |
| 59 | "reservation must fit in RAM without overlap\n" |
| 60 | ); |
| 61 | return 2; |
| 62 | } |
| 63 | if (!machine_fdt(&m)) { |
| 64 | fprintf(stderr, "machine FDT exceeds its bounded reservation\n"); |
| 65 | return 2; |
| 66 | } |
| 67 | m.uart.tx_irq = true; |
| 68 | for (u32 id = 0; id < config->hart_count; id++) { |
| 69 | struct machine_hart *h = &m.harts[id]; |
| 70 | h->id = id; |
| 71 | h->pc = config->entry; |
| 72 | h->privilege = 3; |
| 73 | h->vector.vtype = 1ULL << 63; |
| 74 | h->regs[2] = m.stack_base[id] + config->stack_size; |
| 75 | h->regs[10] = id; |
| 76 | h->regs[11] = MACHINE_FDT_BASE; |
| 77 | m.clint.compare[id] = UINT64_MAX; |
| 78 | } |
| 79 | while (!m.finished && m.ticks < config->max_steps) { |
| 80 | machine_devices_update(&m); |
| 81 | bool runnable = false; |
| 82 | for (u32 checked = 0; checked < config->hart_count; checked++) { |
| 83 | u32 id = m.next_hart; |
| 84 | m.next_hart = id + 1 == config->hart_count ? 0 : id + 1; |
| 85 | struct machine_hart *h = &m.harts[id]; |
| 86 | if (h->waiting && !(machine_pending(&m, h) & h->mie)) |
| 87 | continue; |
| 88 | machine_step(&m, h); |
| 89 | advance(&m, 1); |
| 90 | runnable = true; |
| 91 | break; |
| 92 | } |
| 93 | if (!runnable) { |
| 94 | u64 next = machine_next_event(&m); |
| 95 | advance(&m, next - m.ticks); |
| 96 | } |
| 97 | } |
| 98 | if (config->count_instructions) |
| 99 | fprintf( |
| 100 | stderr, |
| 101 | "Processed %llu instructions across %u harts in %llu ticks\n", |
| 102 | m.retired, |
| 103 | config->hart_count, |
| 104 | m.ticks |
| 105 | ); |
| 106 | if (!m.finished) { |
| 107 | if (config->timed_out) |
| 108 | *config->timed_out = true; |
| 109 | fprintf( |
| 110 | stderr, |
| 111 | "machine timed out after %llu ticks (%llu instructions)\n", |
| 112 | m.ticks, |
| 113 | m.retired |
| 114 | ); |
| 115 | for (u32 id = 0; id < config->hart_count; id++) { |
| 116 | const struct machine_hart *h = &m.harts[id]; |
| 117 | fprintf( |
| 118 | stderr, |
| 119 | "hart=%u pc=0x%llx mode=%c wfi=%u mcause=0x%llx mepc=0x%llx " |
| 120 | "mtval=0x%llx\n", |
| 121 | id, |
| 122 | h->pc, |
| 123 | h->privilege ? 'M' : 'U', |
| 124 | (unsigned)h->waiting, |
| 125 | h->mcause, |
| 126 | h->mepc, |
| 127 | h->mtval |
| 128 | ); |
| 129 | } |
| 130 | return 2; |
| 131 | } |
| 132 | return m.result; |
| 133 | } |