machine: Emulate RV64IMA harts and platform devices
4db56f55927810527a31ca73d68ffbd0f2799b8ccae7330b161c3fcb040bc36e
The emulator only ran hosted programs: user-level code with host system calls and no privilege boundary, devices or interrupts. A kernel could not run on it. Add a machine mode, selected with -machine, that runs RV64IMA with Zicsr and Zifencei images in M and U mode on up to eight harts (-harts=N). Each hart has its own registers, privilege, CSRs, traps, WFI and LR/SC reservation. Harts are scheduled round-robin, one instruction or trap boundary per virtual tick, and RAM and MMIO are strongly ordered with indivisible AMOs. -max-steps bounds the ticks, and -count-instructions reports retired instructions and ticks. The privilege boundary rejects U-mode access to M state and U-mode stores to the original text and rodata. User code may write the platform finish register to complete. The platform has RAM at 0x80000000, a CLINT, a PLIC, a transmit-only 16550 UART, one-shot interrupt injection (-irq, -irq-at) and a finish device. The emulator builds a flattened device tree that describes the harts, devices, loaded sections, stacks and reservations, and passes its address in a1. Native images load in machine mode at their physical addresses in the RAM bank, with the same header and segment validation as in hosted mode. Legacy images, hosted watchpoints and the source debugger are rejected in machine mode. The README documents the platform. Assisted-by: Claude:sonnet-5.5 Assisted-by: Claude:opus-5.5 Assisted-by: Codex:gpt-6
1 parent
87a22d95
Makefile
+3 -2
| 12 | 12 | -Wmissing-field-initializers \ |
|
| 13 | 13 | -fno-common -fstack-protector-all \ |
|
| 14 | 14 | -mcmodel=medium \ |
|
| 15 | 15 | -march=native -flto |
|
| 16 | 16 | LDFLAGS := -fuse-ld=lld -Wl,-z,stack-size=33554432 |
|
| 17 | - | EMULATOR_SRCS := emulator.c jit.c riscv/debug.c io.c riscv.c |
|
| 17 | + | EMULATOR_SRCS := emulator.c jit.c riscv/debug.c io.c riscv.c \ |
|
| 18 | + | machine.c machine_cpu.c machine_csr.c machine_devices.c machine_fdt.c |
|
| 18 | 19 | ||
| 19 | 20 | default: bin/emulator |
|
| 20 | 21 | ||
| 21 | - | bin/emulator: $(EMULATOR_SRCS) jit.h muldiv.h color.h types.h io.h riscv.h riscv/debug.h |
|
| 22 | + | bin/emulator: $(EMULATOR_SRCS) jit.h muldiv.h color.h types.h io.h riscv.h riscv/debug.h machine.h machine_internal.h |
|
| 22 | 23 | @echo "cc emulator => $@" |
|
| 23 | 24 | @mkdir -p bin |
|
| 24 | 25 | @$(CC) $(CFLAGS) $(LDFLAGS) $(EMULATOR_SRCS) -o $@ |
|
| 25 | 26 | ||
| 26 | 27 | install: bin/emulator |
README
+80 -1
| 3 | 3 | ||
| 4 | 4 | A RISC-V RV64I emulator with an x86-64 JIT compiler, interactive TUI |
|
| 5 | 5 | debugger, and headless execution mode. Part of the Radiant project. |
|
| 6 | 6 | ||
| 7 | 7 | Supports the RV64I base integer instruction set and the M (multiply/divide) |
|
| 8 | - | extension. |
|
| 8 | + | extension. Machine mode adds the A extension, Zicsr and Zifencei. |
|
| 9 | 9 | ||
| 10 | 10 | BUILDING |
|
| 11 | 11 | ||
| 12 | 12 | $ make |
|
| 13 | 13 |
| 47 | 47 | ||
| 48 | 48 | The legacy format of older compilers, "RAD0" version 1, has section sizes |
|
| 49 | 49 | and fixed load addresses (text, then read-only data at 0x00010000, then |
|
| 50 | 50 | read-write data at 0x00FFFFF0). |
|
| 51 | 51 | ||
| 52 | + | Machine mode loads only the native format. |
|
| 53 | + | ||
| 52 | 54 | A `<program>.debug` sidecar may be used for source locations. |
|
| 53 | 55 | ||
| 54 | 56 | ||
| 55 | 57 | OPTIONS |
|
| 56 | 58 | ||
| 57 | 59 | -run Run headless (no TUI). |
|
| 58 | 60 | -debug Load debug info for source-level diagnostics. |
|
| 61 | + | -machine Run RV64IMA M/U machine images, always headless. |
|
| 62 | + | Disables host ecalls, JIT, stack guards and TUI. |
|
| 63 | + | -harts=N Enable 1..8 independent machine harts (default 1). |
|
| 64 | + | -irq=N Inject one PLIC edge on source 1..31, excluding 10. |
|
| 65 | + | -irq-at=N Virtual tick of the one-shot edge (default 0). |
|
| 59 | 66 | -no-jit Disable the JIT compiler; interpret only. |
|
| 60 | 67 | -memory-size=KB Physical memory size in KB (default 128 MB). |
|
| 61 | 68 | -data-size=KB Data memory size in KB. |
|
| 62 | 69 | -stack-size=KB Stack size in KB (default 256 KB). |
|
| 63 | 70 | -no-guard-stack Disable stack guard zones (enabled by default, 16 bytes). |
| 97 | 104 | calls and faults. |
|
| 98 | 105 | ||
| 99 | 106 | The TUI debugger supports single-stepping, reverse execution (via |
|
| 100 | 107 | snapshots), register and stack inspection, and memory watchpoints. |
|
| 101 | 108 | ||
| 109 | + | MACHINE PLATFORM |
|
| 110 | + | ||
| 111 | + | Machine execution is a separate RV64IMA + Zicsr + Zifencei interpreter, without |
|
| 112 | + | floating point, compressed instructions, page translation or PMP. Each hart |
|
| 113 | + | has independent registers, privilege, CSRs, traps, WFI and LR/SC reservation. |
|
| 114 | + | Harts are selected round-robin; one instruction or trap boundary advances |
|
| 115 | + | shared mtime by one tick. RAM and MMIO are strongly ordered, AMOs indivisible, |
|
| 116 | + | and every overlapping store invalidates LR reservations. FENCE ordering is |
|
| 117 | + | inherent; FENCE.I observes uncached instruction fetches. |
|
| 118 | + | ||
| 119 | + | WFI sleeps until a locally enabled interrupt is pending, independently of the |
|
| 120 | + | global MIE bit. It never exits the emulator. When all harts sleep, virtual time |
|
| 121 | + | jumps to the next timer/input event or -max-steps deadline; idle ticks count |
|
| 122 | + | toward this bound. External, software, then timer is the M interrupt priority. |
|
| 123 | + | mtvec supports direct and vectored interrupts. Synchronous U faults trap to M. |
|
| 124 | + | ||
| 125 | + | Physical devices (all other aperture offsets/widths fault): |
|
| 126 | + | ||
| 127 | + | 0x02000000..0x02010000 CLINT: MSIP words at +4*hart; mtimecmp at |
|
| 128 | + | +0x4000+8*hart; mtime at +0xbff8. Timers accept |
|
| 129 | + | aligned 32-bit halves or 64-bit accesses. |
|
| 130 | + | 0x0c000000..0x10000000 PLIC: 31 sources, priorities 0..7 at +4*source, |
|
| 131 | + | pending at +0x1000, enable at +0x2000+0x80*hart, |
|
| 132 | + | threshold/claim/complete at +0x200000+0x1000*hart |
|
| 133 | + | and +4. Every register is 32-bit. M context=hart. |
|
| 134 | + | 0x10000000..0x10001000 Transmit-only 16550 UART: byte registers THR +0 |
|
| 135 | + | (stdout), IER +1 (only THRE, bit 1), IIR +2 and |
|
| 136 | + | LSR +5 (read only, always ready). Source 10 on the |
|
| 137 | + | PLIC is the level-sensitive THRE interrupt. |
|
| 138 | + | 0x10001000..0x10002000 Finish: 32-bit register at +0, writable in M and |
|
| 139 | + | U. Write 0x5555 for success, 0xNNNN3333 for |
|
| 140 | + | failure. Other writes fault. Reads return zero. |
|
| 141 | + | ||
| 142 | + | RAM is [0x80000000,0x80000000+memory-size), above every device aperture. |
|
| 143 | + | Naturally aligned fetches/loads/stores are checked even with -no-validate. |
|
| 144 | + | The hosted stack and frame-pointer checks do not apply. Instructions may |
|
| 145 | + | execute anywhere in RAM, including admitted images outside the original text. |
|
| 146 | + | U stores to the original text and rodata are rejected as an optional ROM |
|
| 147 | + | defense, not memory isolation. |
|
| 148 | + | ||
| 149 | + | Boot and FDT: |
|
| 150 | + | ||
| 151 | + | Every hart starts at the loaded text entry in M mode. a0 and mhartid are its |
|
| 152 | + | zero-based ID; a1 is the FDT address, 0x80800000. sp is the 16-byte aligned |
|
| 153 | + | top of RAM minus hart*stack-size. Each full stack is reserved; all other |
|
| 154 | + | registers start zero. -stack-size applies independently to every hart. |
|
| 155 | + | ||
| 156 | + | FDT v17 is stored in the fixed 64 KiB reservation [0x80800000,0x80810000). |
|
| 157 | + | Image or stack overlap with this reservation is rejected. The memory node |
|
| 158 | + | starts at 0x80000000. CPU interrupt-controller phandles are hart+1; |
|
| 159 | + | the PLIC phandle is 0x100. CLINT extended pairs are (hart+1,3),(hart+1,7); |
|
| 160 | + | PLIC contexts are ordered (hart+1,11). UART interrupt-parent is 0x100. |
|
| 161 | + | The timebase is 10 MHz. The finish compatible string is "radiant,finish". |
|
| 162 | + | ||
| 163 | + | /chosen has radiance,kernel-entry (u64), radiance,kernel-text, |
|
| 164 | + | radiance,kernel-rodata, radiance,kernel-rwdata, radiance,fdt-reservation |
|
| 165 | + | (each address-u64,size-u64), and radiance,kernel-stacks (one such pair per |
|
| 166 | + | hart). Exact loaded sections, every initial stack, FDT storage and full |
|
| 167 | + | MMIO apertures appear in both the reservation map and reserved-memory |
|
| 168 | + | no-map nodes. Empty loaded sections have size zero in /chosen and are |
|
| 169 | + | omitted from reservations. |
|
| 170 | + | ||
| 171 | + | -trace prints hart-tagged instructions and traps. -count-instructions reports |
|
| 172 | + | retired instructions separately from elapsed virtual ticks. Hosted source |
|
| 173 | + | debugging and watchpoints are rejected in machine mode. Process results are |
|
| 174 | + | 0 for explicit success, 1 for guest failure, and 2 for timeout/configuration or |
|
| 175 | + | host UART output failure. No host time, stdin polling or host threads affect |
|
| 176 | + | guest execution. This is not a cycle-accurate model of UART wire timing. |
|
| 177 | + | ||
| 178 | + | The platform is exercised by the Radiance test suite (`make test` in the |
|
| 179 | + | Radiance repository, with `RAD_EMULATOR` set to this binary). |
|
| 180 | + | ||
| 102 | 181 | LICENSE |
|
| 103 | 182 | ||
| 104 | 183 | Licensed under the MIT License, |
|
| 105 | 184 | Copyright (c) 2025-2026 Radiant Computer (https://radiant.computer) |
emulator.c
+103 -5
| 10 | 10 | #include <unistd.h> |
|
| 11 | 11 | ||
| 12 | 12 | #include "color.h" |
|
| 13 | 13 | #include "io.h" |
|
| 14 | 14 | #include "jit.h" |
|
| 15 | + | #include "machine.h" |
|
| 15 | 16 | #include "muldiv.h" |
|
| 16 | 17 | #include "riscv.h" |
|
| 17 | 18 | #include "riscv/debug.h" |
|
| 18 | 19 | #include "types.h" |
|
| 19 | 20 |
| 240 | 241 | bool watch_backtrace; |
|
| 241 | 242 | u32 watch_backtrace_depth; |
|
| 242 | 243 | bool validate_memory; |
|
| 243 | 244 | bool count_instructions; |
|
| 244 | 245 | bool jit_disabled; |
|
| 246 | + | bool machine_mode; |
|
| 245 | 247 | }; |
|
| 246 | 248 | ||
| 247 | 249 | /* Global emulator options. */ |
|
| 248 | 250 | static struct emulator_options g_opts = { |
|
| 249 | 251 | .stack_guard = true, |
| 265 | 267 | .watch_backtrace = false, |
|
| 266 | 268 | .watch_backtrace_depth = 8, |
|
| 267 | 269 | .validate_memory = true, |
|
| 268 | 270 | .count_instructions = false, |
|
| 269 | 271 | .jit_disabled = false, |
|
| 272 | + | .machine_mode = false, |
|
| 270 | 273 | }; |
|
| 271 | 274 | ||
| 275 | + | /* Machine-only options are kept separate from hosted CPU and debugger state. */ |
|
| 276 | + | static struct machine_config machine_options = { .hart_count = 1 }; |
|
| 277 | + | ||
| 272 | 278 | static void dump_watch_context(struct cpu *, u32 addr, u32 size, u32 value); |
|
| 273 | 279 | ||
| 274 | 280 | /* Return true if the given address range overlaps the watched region. */ |
|
| 275 | 281 | static inline bool watch_hit(u32 addr, u32 size) { |
|
| 276 | 282 | if (!g_opts.watch_enabled) |
| 501 | 507 | ||
| 502 | 508 | /* Print usage information and return 1. */ |
|
| 503 | 509 | static int usage(const char *prog) { |
|
| 504 | 510 | fprintf( |
|
| 505 | 511 | stderr, |
|
| 506 | - | "usage: %s [-run] [-no-guard-stack]" |
|
| 512 | + | "usage: %s [-run] [-machine] [-harts=1..8] [-no-guard-stack]" |
|
| 507 | 513 | " [-stack-size=KB] [-no-validate] [-debug]" |
|
| 508 | 514 | " [-trace] [-trace-depth=n] [-trace-instructions]" |
|
| 509 | 515 | " [-max-steps=n] [-memory-size=KB] [-data-size=KB]" |
|
| 510 | 516 | " [-watch=addr] [-watch-size=bytes] [-watch-arm-pc=addr]" |
|
| 511 | 517 | " [-watch-zero-only] [-watch-skip=n]" |
|
| 512 | 518 | " [-watch-backtrace] [-watch-bt-depth=n]" |
|
| 513 | 519 | " [-count-instructions] [-no-jit]" |
|
| 520 | + | " [-irq=1..31] [-irq-at=tick]" |
|
| 514 | 521 | " <file.bin> [program args...]\n", |
|
| 515 | 522 | prog |
|
| 516 | 523 | ); |
|
| 517 | 524 | return 1; |
|
| 518 | 525 | } |
| 665 | 672 | return true; |
|
| 666 | 673 | } |
|
| 667 | 674 | ||
| 668 | 675 | /* Parse emulator CLI arguments, returning the selected mode and file path. */ |
|
| 669 | 676 | static bool parse_cli_args(int argc, char *argv[], struct cli_config *cfg) { |
|
| 670 | - | bool headless = false; |
|
| 671 | - | int argi = 1; |
|
| 677 | + | bool headless = false; |
|
| 678 | + | int argi = 1; |
|
| 679 | + | bool machine_options_used = false; |
|
| 672 | 680 | ||
| 673 | 681 | while (argi < argc) { |
|
| 674 | 682 | const char *arg = argv[argi]; |
|
| 675 | 683 | ||
| 676 | 684 | if (strcmp(arg, "--") == 0) { |
| 683 | 691 | if (strcmp(arg, "-run") == 0) { |
|
| 684 | 692 | headless = true; |
|
| 685 | 693 | argi++; |
|
| 686 | 694 | continue; |
|
| 687 | 695 | } |
|
| 696 | + | if (strcmp(arg, "-machine") == 0) { |
|
| 697 | + | g_opts.machine_mode = true; |
|
| 698 | + | argi++; |
|
| 699 | + | continue; |
|
| 700 | + | } |
|
| 701 | + | if (strncmp(arg, "-harts=", 7) == 0) { |
|
| 702 | + | if (!parse_u32( |
|
| 703 | + | arg + 7, "hart count", 10, &machine_options.hart_count |
|
| 704 | + | ) || |
|
| 705 | + | machine_options.hart_count < 1 || |
|
| 706 | + | machine_options.hart_count > MACHINE_MAX_HARTS) { |
|
| 707 | + | fprintf(stderr, "hart count must be between 1 and 8\n"); |
|
| 708 | + | return false; |
|
| 709 | + | } |
|
| 710 | + | machine_options_used = true; |
|
| 711 | + | argi++; |
|
| 712 | + | continue; |
|
| 713 | + | } |
|
| 714 | + | if (strncmp(arg, "-irq=", 5) == 0) { |
|
| 715 | + | if (!parse_u32( |
|
| 716 | + | arg + 5, "IRQ source", 10, &machine_options.irq_source |
|
| 717 | + | ) || |
|
| 718 | + | machine_options.irq_source == 0 || |
|
| 719 | + | machine_options.irq_source > MACHINE_IRQ_COUNT || |
|
| 720 | + | machine_options.irq_source == MACHINE_UART_IRQ) { |
|
| 721 | + | fprintf( |
|
| 722 | + | stderr, |
|
| 723 | + | "IRQ source must be 1..31 excluding UART source 10\n" |
|
| 724 | + | ); |
|
| 725 | + | return false; |
|
| 726 | + | } |
|
| 727 | + | machine_options_used = true; |
|
| 728 | + | argi++; |
|
| 729 | + | continue; |
|
| 730 | + | } |
|
| 731 | + | if (strncmp(arg, "-irq-at=", 8) == 0) { |
|
| 732 | + | if (!parse_u64( |
|
| 733 | + | arg + 8, "IRQ injection tick", &machine_options.irq_at |
|
| 734 | + | )) |
|
| 735 | + | return false; |
|
| 736 | + | machine_options_used = true; |
|
| 737 | + | argi++; |
|
| 738 | + | continue; |
|
| 739 | + | } |
|
| 688 | 740 | ||
| 689 | 741 | if (strncmp(arg, "-stack-size=", 12) == 0) { |
|
| 690 | 742 | if (!parse_stack_size_value(arg + 12)) |
|
| 691 | 743 | return false; |
|
| 692 | 744 | argi++; |
| 814 | 866 | return false; |
|
| 815 | 867 | } |
|
| 816 | 868 | cfg->program_path = argv[argi++]; |
|
| 817 | 869 | cfg->arg_index = argi; |
|
| 818 | 870 | cfg->headless = headless; |
|
| 871 | + | if (machine_options_used && !g_opts.machine_mode) { |
|
| 872 | + | fprintf(stderr, "-harts and -irq options require -machine\n"); |
|
| 873 | + | return false; |
|
| 874 | + | } |
|
| 875 | + | if (g_opts.machine_mode && (g_opts.watch_enabled || g_opts.debug_enabled)) { |
|
| 876 | + | fprintf( |
|
| 877 | + | stderr, |
|
| 878 | + | "hosted watchpoints and source debugger are unavailable in machine " |
|
| 879 | + | "mode; use -trace\n" |
|
| 880 | + | ); |
|
| 881 | + | return false; |
|
| 882 | + | } |
|
| 819 | 883 | if (g_opts.watch_enabled && g_opts.watch_size == 0) |
|
| 820 | 884 | g_opts.watch_size = 4; |
|
| 821 | 885 | if (!validate_memory_layout()) |
|
| 822 | 886 | return false; |
|
| 823 | 887 |
| 1487 | 1551 | file, (u32)(seg->base - ram_base), seg->size, "native" |
|
| 1488 | 1552 | ); |
|
| 1489 | 1553 | } |
|
| 1490 | 1554 | } |
|
| 1491 | 1555 | ||
| 1556 | + | /* Load a native image in machine mode, where every segment must lie in the |
|
| 1557 | + | * RAM bank at MACHINE_RAM_BASE, and record it for machine_run. */ |
|
| 1558 | + | static void load_machine_image(FILE *file, long file_size) { |
|
| 1559 | + | u64 end = MACHINE_RAM_BASE + g_opts.memory_size; |
|
| 1560 | + | struct image_range range = { |
|
| 1561 | + | .low = { MACHINE_RAM_BASE, MACHINE_RAM_BASE, MACHINE_RAM_BASE }, |
|
| 1562 | + | .high = { end, end, end }, |
|
| 1563 | + | }; |
|
| 1564 | + | struct image image = parse_image(file, file_size, &range); |
|
| 1565 | + | load_image_segments(file, &image, MACHINE_RAM_BASE); |
|
| 1566 | + | ||
| 1567 | + | machine_options.entry = image.segments[0].base; |
|
| 1568 | + | struct machine_section *sections[] = { &machine_options.text, |
|
| 1569 | + | &machine_options.rodata, |
|
| 1570 | + | &machine_options.rwdata }; |
|
| 1571 | + | for (u32 i = 0; i < 3; i++) { |
|
| 1572 | + | sections[i]->base = image.segments[i].base; |
|
| 1573 | + | sections[i]->size = image.segments[i].size + image.segments[i].tail; |
|
| 1574 | + | } |
|
| 1575 | + | } |
|
| 1576 | + | ||
| 1492 | 1577 | /* Load a native image in hosted mode and point the CPU at the code. Code and |
|
| 1493 | 1578 | * read-only data must lie in [DATA_RO_OFFSET, DATA_MEMORY_START) and |
|
| 1494 | 1579 | * read-write data in [DATA_MEMORY_START, data_limit). */ |
|
| 1495 | 1580 | static void load_hosted_image( |
|
| 1496 | 1581 | struct cpu *cpu, FILE *file, long file_size, u32 data_limit |
| 1543 | 1628 | ||
| 1544 | 1629 | u8 header[LEGACY_HEADER_SIZE]; |
|
| 1545 | 1630 | size_t header_read = fread(header, 1, sizeof(header), file); |
|
| 1546 | 1631 | if (header_read >= sizeof(u64) && |
|
| 1547 | 1632 | decode_u64_le(header) == NATIVE_IMAGE_MAGIC) { |
|
| 1548 | - | load_hosted_image(cpu, file, size, data_limit); |
|
| 1633 | + | if (g_opts.machine_mode) |
|
| 1634 | + | load_machine_image(file, size); |
|
| 1635 | + | else |
|
| 1636 | + | load_hosted_image(cpu, file, size, data_limit); |
|
| 1549 | 1637 | fclose(file); |
|
| 1550 | 1638 | return; |
|
| 1551 | 1639 | } |
|
| 1552 | - | if (header_read < sizeof(u32) || |
|
| 1640 | + | if (g_opts.machine_mode || header_read < sizeof(u32) || |
|
| 1553 | 1641 | decode_u32_le(header) != LEGACY_IMAGE_MAGIC) { |
|
| 1554 | 1642 | fclose(file); |
|
| 1555 | 1643 | bail("unrecognized Radiance image: '%s'", filepath); |
|
| 1556 | 1644 | } |
|
| 1557 | 1645 | if (header_read != sizeof(header)) { |
| 2532 | 2620 | ||
| 2533 | 2621 | g_opts.trace_headless = headless; |
|
| 2534 | 2622 | ||
| 2535 | 2623 | cpu_init(&cpu); |
|
| 2536 | 2624 | program_init(&cpu, cli.program_path); |
|
| 2625 | + | if (g_opts.machine_mode) { |
|
| 2626 | + | machine_options.ram = memory; |
|
| 2627 | + | machine_options.memory_size = g_opts.memory_size; |
|
| 2628 | + | machine_options.stack_size = g_opts.stack_size; |
|
| 2629 | + | machine_options.max_steps = g_opts.headless_max_steps; |
|
| 2630 | + | machine_options.trace = |
|
| 2631 | + | g_opts.trace_enabled || g_opts.trace_print_instructions; |
|
| 2632 | + | machine_options.count_instructions = g_opts.count_instructions; |
|
| 2633 | + | return machine_run(&machine_options); |
|
| 2634 | + | } |
|
| 2537 | 2635 | int prog_argc = argc - cli.arg_index; |
|
| 2538 | 2636 | char **prog_argv = &argv[cli.arg_index]; |
|
| 2539 | 2637 | prepare_env(&cpu, prog_argc, prog_argv); |
|
| 2540 | 2638 | ||
| 2541 | 2639 | if (headless) { |
machine.c
added
+130 -0
| 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->regs[2] = m.stack_base[id] + config->stack_size; |
|
| 74 | + | h->regs[10] = id; |
|
| 75 | + | h->regs[11] = MACHINE_FDT_BASE; |
|
| 76 | + | m.clint.compare[id] = UINT64_MAX; |
|
| 77 | + | } |
|
| 78 | + | while (!m.finished && m.ticks < config->max_steps) { |
|
| 79 | + | machine_devices_update(&m); |
|
| 80 | + | bool runnable = false; |
|
| 81 | + | for (u32 checked = 0; checked < config->hart_count; checked++) { |
|
| 82 | + | u32 id = m.next_hart; |
|
| 83 | + | m.next_hart = id + 1 == config->hart_count ? 0 : id + 1; |
|
| 84 | + | struct machine_hart *h = &m.harts[id]; |
|
| 85 | + | if (h->waiting && !(machine_pending(&m, h) & h->mie)) |
|
| 86 | + | continue; |
|
| 87 | + | machine_step(&m, h); |
|
| 88 | + | advance(&m, 1); |
|
| 89 | + | runnable = true; |
|
| 90 | + | break; |
|
| 91 | + | } |
|
| 92 | + | if (!runnable) { |
|
| 93 | + | u64 next = machine_next_event(&m); |
|
| 94 | + | advance(&m, next - m.ticks); |
|
| 95 | + | } |
|
| 96 | + | } |
|
| 97 | + | if (config->count_instructions) |
|
| 98 | + | fprintf( |
|
| 99 | + | stderr, |
|
| 100 | + | "Processed %llu instructions across %u harts in %llu ticks\n", |
|
| 101 | + | m.retired, |
|
| 102 | + | config->hart_count, |
|
| 103 | + | m.ticks |
|
| 104 | + | ); |
|
| 105 | + | if (!m.finished) { |
|
| 106 | + | fprintf( |
|
| 107 | + | stderr, |
|
| 108 | + | "machine timed out after %llu ticks (%llu instructions)\n", |
|
| 109 | + | m.ticks, |
|
| 110 | + | m.retired |
|
| 111 | + | ); |
|
| 112 | + | for (u32 id = 0; id < config->hart_count; id++) { |
|
| 113 | + | const struct machine_hart *h = &m.harts[id]; |
|
| 114 | + | fprintf( |
|
| 115 | + | stderr, |
|
| 116 | + | "hart=%u pc=0x%llx mode=%c wfi=%u mcause=0x%llx mepc=0x%llx " |
|
| 117 | + | "mtval=0x%llx\n", |
|
| 118 | + | id, |
|
| 119 | + | h->pc, |
|
| 120 | + | h->privilege ? 'M' : 'U', |
|
| 121 | + | (unsigned)h->waiting, |
|
| 122 | + | h->mcause, |
|
| 123 | + | h->mepc, |
|
| 124 | + | h->mtval |
|
| 125 | + | ); |
|
| 126 | + | } |
|
| 127 | + | return 2; |
|
| 128 | + | } |
|
| 129 | + | return m.result; |
|
| 130 | + | } |
machine.h
added
+43 -0
| 1 | + | /* Deterministic RV64IMA machine with CLINT, PLIC, UART and finish devices. */ |
|
| 2 | + | #ifndef MACHINE_H |
|
| 3 | + | #define MACHINE_H |
|
| 4 | + | ||
| 5 | + | #include "types.h" |
|
| 6 | + | ||
| 7 | + | /* Platform limits and the physical base of RAM, also published in the FDT. */ |
|
| 8 | + | #define MACHINE_MAX_HARTS 8U |
|
| 9 | + | #define MACHINE_RAM_BASE 0x80000000ULL |
|
| 10 | + | #define MACHINE_UART_IRQ 10U |
|
| 11 | + | #define MACHINE_IRQ_COUNT 31U |
|
| 12 | + | ||
| 13 | + | /* A loaded physical section. Empty sections need not reserve any memory. */ |
|
| 14 | + | struct machine_section { |
|
| 15 | + | u64 base; /* Inclusive physical start address. */ |
|
| 16 | + | u32 size; /* Total reserved bytes, including the zero-filled tail. */ |
|
| 17 | + | }; |
|
| 18 | + | ||
| 19 | + | /* Immutable run configuration. The caller owns RAM. */ |
|
| 20 | + | struct machine_config { |
|
| 21 | + | u8 *ram; /* Loaded RAM at MACHINE_RAM_BASE. */ |
|
| 22 | + | u32 memory_size; /* RAM byte count. */ |
|
| 23 | + | u64 entry; /* Initial PC for every hart. */ |
|
| 24 | + | struct machine_section text; /* Loaded executable section. */ |
|
| 25 | + | struct machine_section rodata; /* Loaded immutable data section. */ |
|
| 26 | + | struct machine_section rwdata; /* Loaded initialized writable section. */ |
|
| 27 | + | u32 stack_size; /* Bytes reserved per hart; multiple of 16. */ |
|
| 28 | + | u32 hart_count; /* Number of enabled harts, 1 through 8. */ |
|
| 29 | + | u64 max_steps; /* Virtual ticks, including skipped idle time. */ |
|
| 30 | + | u32 irq_source; /* One-shot PLIC edge source, 0 disables. */ |
|
| 31 | + | u64 irq_at; /* Virtual tick of the one-shot edge. */ |
|
| 32 | + | bool trace; /* Print hart-tagged instructions and traps. */ |
|
| 33 | + | bool count_instructions; /* Report actual retired instructions. */ |
|
| 34 | + | }; |
|
| 35 | + | ||
| 36 | + | /* Run the machine until a finish write or the tick limit. Returns 0 when the |
|
| 37 | + | * guest writes the success value to the finish register, 1 when it writes a |
|
| 38 | + | * failure value, and 2 for an invalid layout, UART output failure or timeout. |
|
| 39 | + | * Machine execution never enters the hosted JIT, debugger, or syscall |
|
| 40 | + | * runtime. */ |
|
| 41 | + | int machine_run(const struct machine_config *config); |
|
| 42 | + | ||
| 43 | + | #endif /* MACHINE_H */ |
machine_cpu.c
added
+438 -0
| 1 | + | /* Instruction execution for one machine hart. */ |
|
| 2 | + | #include <stdint.h> |
|
| 3 | + | #include <stdio.h> |
|
| 4 | + | ||
| 5 | + | #include "machine_internal.h" |
|
| 6 | + | #include "muldiv.h" |
|
| 7 | + | #include "riscv.h" |
|
| 8 | + | ||
| 9 | + | /* RV64 wraparound arithmetic stays unsigned; extension never shifts by 64. */ |
|
| 10 | + | static u64 sext(u64 value, u32 bits) { |
|
| 11 | + | if (bits == 64) |
|
| 12 | + | return value; |
|
| 13 | + | u64 sign = 1ULL << (bits - 1); |
|
| 14 | + | value &= (1ULL << bits) - 1; |
|
| 15 | + | return (value ^ sign) - sign; |
|
| 16 | + | } |
|
| 17 | + | ||
| 18 | + | /* Perform an indivisible atomic operation in the sequential coherent bus model. |
|
| 19 | + | * aq/rl are satisfied by total instruction order; AMOs never target devices. */ |
|
| 20 | + | static bool atomic( |
|
| 21 | + | struct machine *m, |
|
| 22 | + | struct machine_hart *h, |
|
| 23 | + | u32 ins, |
|
| 24 | + | u64 address, |
|
| 25 | + | u64 source, |
|
| 26 | + | u64 *result, |
|
| 27 | + | u64 *cause |
|
| 28 | + | ) { |
|
| 29 | + | u32 width = ((ins >> 12) & 7) == 2 ? 4 : 8; |
|
| 30 | + | u32 operation = ins >> 27; |
|
| 31 | + | u64 old = 0; |
|
| 32 | + | if (operation != 2 && !machine_store_probe(m, h, address, width, cause)) { |
|
| 33 | + | h->reserved = false; |
|
| 34 | + | return false; |
|
| 35 | + | } |
|
| 36 | + | if (operation == 3) { |
|
| 37 | + | bool success = h->reserved && h->reservation == address && |
|
| 38 | + | h->reservation_size == width; |
|
| 39 | + | h->reserved = false; |
|
| 40 | + | if (success && !machine_bus( |
|
| 41 | + | m, |
|
| 42 | + | h, |
|
| 43 | + | address, |
|
| 44 | + | width, |
|
| 45 | + | ACCESS_STORE | ACCESS_ATOMIC, |
|
| 46 | + | &source, |
|
| 47 | + | cause |
|
| 48 | + | )) |
|
| 49 | + | return false; |
|
| 50 | + | *result = success ? 0 : 1; |
|
| 51 | + | return true; |
|
| 52 | + | } |
|
| 53 | + | /* Only LR can fail here: an AMO has passed the store probe. */ |
|
| 54 | + | if (!machine_bus(m, h, address, width, ACCESS_ATOMIC, &old, cause)) |
|
| 55 | + | return false; |
|
| 56 | + | *result = sext(old, width * 8); |
|
| 57 | + | if (operation == 2) { |
|
| 58 | + | h->reserved = true; |
|
| 59 | + | h->reservation = address; |
|
| 60 | + | h->reservation_size = width; |
|
| 61 | + | return true; |
|
| 62 | + | } |
|
| 63 | + | u64 value = 0; |
|
| 64 | + | if (width == 4) |
|
| 65 | + | source = (u32)source; |
|
| 66 | + | switch (operation) { |
|
| 67 | + | case 0: |
|
| 68 | + | value = old + source; |
|
| 69 | + | break; |
|
| 70 | + | case 1: |
|
| 71 | + | value = source; |
|
| 72 | + | break; |
|
| 73 | + | case 4: |
|
| 74 | + | value = old ^ source; |
|
| 75 | + | break; |
|
| 76 | + | case 8: |
|
| 77 | + | value = old | source; |
|
| 78 | + | break; |
|
| 79 | + | case 12: |
|
| 80 | + | value = old & source; |
|
| 81 | + | break; |
|
| 82 | + | case 16: |
|
| 83 | + | value = (i64)sext(old, width * 8) < (i64)sext(source, width * 8) |
|
| 84 | + | ? old |
|
| 85 | + | : source; |
|
| 86 | + | break; |
|
| 87 | + | case 20: |
|
| 88 | + | value = (i64)sext(old, width * 8) > (i64)sext(source, width * 8) |
|
| 89 | + | ? old |
|
| 90 | + | : source; |
|
| 91 | + | break; |
|
| 92 | + | case 24: |
|
| 93 | + | value = old < source ? old : source; |
|
| 94 | + | break; |
|
| 95 | + | case 28: |
|
| 96 | + | value = old > source ? old : source; |
|
| 97 | + | break; |
|
| 98 | + | } |
|
| 99 | + | return machine_bus( |
|
| 100 | + | m, h, address, width, ACCESS_STORE | ACCESS_ATOMIC, &value, cause |
|
| 101 | + | ); |
|
| 102 | + | } |
|
| 103 | + | ||
| 104 | + | /* Compute an OP or OP-32 instruction into `result`. Returns false if the |
|
| 105 | + | * instruction is illegal. */ |
|
| 106 | + | static bool register_op(u32 ins, u64 a, u64 b, u64 *result) { |
|
| 107 | + | bool word = (ins & 0x7f) == OP_OP_32; |
|
| 108 | + | u32 f3 = (ins >> 12) & 7, f7 = ins >> 25; |
|
| 109 | + | u64 value = 0; |
|
| 110 | + | if (f7 == 1) { |
|
| 111 | + | if (word) { |
|
| 112 | + | if (f3 == 0) |
|
| 113 | + | value = (u64)(u32)a * (u32)b; |
|
| 114 | + | else if (f3 == 4 || f3 == 6) |
|
| 115 | + | value = divs64(sext(a, 32), sext(b, 32), f3 == 6); |
|
| 116 | + | else if (f3 == 5) |
|
| 117 | + | value = (u32)b ? (u32)a / (u32)b : UINT32_MAX; |
|
| 118 | + | else if (f3 == 7) |
|
| 119 | + | value = (u32)b ? (u32)a % (u32)b : (u32)a; |
|
| 120 | + | else |
|
| 121 | + | return false; |
|
| 122 | + | } else { |
|
| 123 | + | switch (f3) { |
|
| 124 | + | case 0: |
|
| 125 | + | value = a * b; |
|
| 126 | + | break; |
|
| 127 | + | case 1: |
|
| 128 | + | value = mulh64(a, b); |
|
| 129 | + | break; |
|
| 130 | + | case 2: |
|
| 131 | + | value = mulhsu64(a, b); |
|
| 132 | + | break; |
|
| 133 | + | case 3: |
|
| 134 | + | value = mulhu64(a, b); |
|
| 135 | + | break; |
|
| 136 | + | case 4: |
|
| 137 | + | value = divs64(a, b, false); |
|
| 138 | + | break; |
|
| 139 | + | case 5: |
|
| 140 | + | value = b ? a / b : UINT64_MAX; |
|
| 141 | + | break; |
|
| 142 | + | case 6: |
|
| 143 | + | value = divs64(a, b, true); |
|
| 144 | + | break; |
|
| 145 | + | case 7: |
|
| 146 | + | value = b ? a % b : a; |
|
| 147 | + | break; |
|
| 148 | + | } |
|
| 149 | + | } |
|
| 150 | + | } else { |
|
| 151 | + | if (f7 != 0 && f7 != 0x20) |
|
| 152 | + | return false; |
|
| 153 | + | if (f7 == 0x20 && f3 != 0 && f3 != 5) |
|
| 154 | + | return false; |
|
| 155 | + | if (word && f3 != 0 && f3 != 1 && f3 != 5) |
|
| 156 | + | return false; |
|
| 157 | + | u32 shift = b & (word ? 31 : 63); |
|
| 158 | + | switch (f3) { |
|
| 159 | + | case 0: |
|
| 160 | + | value = f7 ? a - b : a + b; |
|
| 161 | + | break; |
|
| 162 | + | case 1: |
|
| 163 | + | value = a << shift; |
|
| 164 | + | break; |
|
| 165 | + | case 2: |
|
| 166 | + | value = (i64)a < (i64)b; |
|
| 167 | + | break; |
|
| 168 | + | case 3: |
|
| 169 | + | value = a < b; |
|
| 170 | + | break; |
|
| 171 | + | case 4: |
|
| 172 | + | value = a ^ b; |
|
| 173 | + | break; |
|
| 174 | + | case 5: |
|
| 175 | + | if (word) |
|
| 176 | + | value = f7 ? (u32)((i32)a >> shift) : (u32)a >> shift; |
|
| 177 | + | else |
|
| 178 | + | value = f7 ? (u64)((i64)a >> shift) : a >> shift; |
|
| 179 | + | break; |
|
| 180 | + | case 6: |
|
| 181 | + | value = a | b; |
|
| 182 | + | break; |
|
| 183 | + | case 7: |
|
| 184 | + | value = a & b; |
|
| 185 | + | break; |
|
| 186 | + | } |
|
| 187 | + | } |
|
| 188 | + | *result = word ? sext(value, 32) : value; |
|
| 189 | + | return true; |
|
| 190 | + | } |
|
| 191 | + | ||
| 192 | + | void machine_step(struct machine *m, struct machine_hart *h) { |
|
| 193 | + | h->wrote_instret = false; |
|
| 194 | + | u64 pending = machine_pending(m, h) & h->mie; |
|
| 195 | + | if (pending) |
|
| 196 | + | h->waiting = false; |
|
| 197 | + | if (pending && (h->privilege < 3 || (h->mstatus & MSTATUS_MIE))) { |
|
| 198 | + | u64 interrupt = (pending & MIP_MEIP) ? 11 |
|
| 199 | + | : (pending & MIP_MSIP) ? 3 |
|
| 200 | + | : 7; |
|
| 201 | + | machine_trap(m, h, MCAUSE_INTERRUPT | interrupt, 0); |
|
| 202 | + | return; |
|
| 203 | + | } |
|
| 204 | + | if (h->waiting) |
|
| 205 | + | return; |
|
| 206 | + | u64 raw = 0, cause = 0, address = h->pc; |
|
| 207 | + | if (!machine_bus(m, h, h->pc, 4, ACCESS_FETCH, &raw, &cause)) |
|
| 208 | + | goto fault; |
|
| 209 | + | u32 ins = (u32)raw; |
|
| 210 | + | if (m->config->trace) |
|
| 211 | + | fprintf( |
|
| 212 | + | stderr, |
|
| 213 | + | "hart=%u tick=%llu pc=0x%llx insn=%08x mode=%c\n", |
|
| 214 | + | h->id, |
|
| 215 | + | m->ticks, |
|
| 216 | + | h->pc, |
|
| 217 | + | ins, |
|
| 218 | + | h->privilege ? 'M' : 'U' |
|
| 219 | + | ); |
|
| 220 | + | u32 opcode = ins & 0x7f, rd = (ins >> 7) & 31; |
|
| 221 | + | u32 f3 = (ins >> 12) & 7, rs1 = (ins >> 15) & 31, rs2 = (ins >> 20) & 31; |
|
| 222 | + | u32 f7 = ins >> 25; |
|
| 223 | + | u64 a = h->regs[rs1], b = h->regs[rs2]; |
|
| 224 | + | u64 immediate = sext(ins >> 20, 12), result = 0, next = h->pc + 4; |
|
| 225 | + | bool write_rd = true; |
|
| 226 | + | switch (opcode) { |
|
| 227 | + | case OP_LUI: |
|
| 228 | + | result = sext(ins & 0xfffff000U, 32); |
|
| 229 | + | break; |
|
| 230 | + | case OP_AUIPC: |
|
| 231 | + | result = h->pc + sext(ins & 0xfffff000U, 32); |
|
| 232 | + | break; |
|
| 233 | + | case OP_JAL: |
|
| 234 | + | immediate = ((ins >> 31) << 20) | (((ins >> 12) & 255) << 12) | |
|
| 235 | + | (((ins >> 20) & 1) << 11) | (((ins >> 21) & 1023) << 1); |
|
| 236 | + | next = h->pc + sext(immediate, 21); |
|
| 237 | + | result = h->pc + 4; |
|
| 238 | + | goto target; |
|
| 239 | + | case OP_JALR: |
|
| 240 | + | if (f3) |
|
| 241 | + | goto illegal; |
|
| 242 | + | next = (a + immediate) & ~1ULL; |
|
| 243 | + | result = h->pc + 4; |
|
| 244 | + | goto target; |
|
| 245 | + | case OP_BRANCH: { |
|
| 246 | + | bool taken; |
|
| 247 | + | write_rd = false; |
|
| 248 | + | switch (f3) { |
|
| 249 | + | case 0: |
|
| 250 | + | taken = a == b; |
|
| 251 | + | break; |
|
| 252 | + | case 1: |
|
| 253 | + | taken = a != b; |
|
| 254 | + | break; |
|
| 255 | + | case 4: |
|
| 256 | + | taken = (i64)a < (i64)b; |
|
| 257 | + | break; |
|
| 258 | + | case 5: |
|
| 259 | + | taken = (i64)a >= (i64)b; |
|
| 260 | + | break; |
|
| 261 | + | case 6: |
|
| 262 | + | taken = a < b; |
|
| 263 | + | break; |
|
| 264 | + | case 7: |
|
| 265 | + | taken = a >= b; |
|
| 266 | + | break; |
|
| 267 | + | default: |
|
| 268 | + | goto illegal; |
|
| 269 | + | } |
|
| 270 | + | if (taken) { |
|
| 271 | + | immediate = ((ins >> 31) << 12) | (((ins >> 7) & 1) << 11) | |
|
| 272 | + | (((ins >> 25) & 63) << 5) | (((ins >> 8) & 15) << 1); |
|
| 273 | + | next = h->pc + sext(immediate, 13); |
|
| 274 | + | } |
|
| 275 | + | goto target; |
|
| 276 | + | } |
|
| 277 | + | case OP_LOAD: { |
|
| 278 | + | if (f3 == 7) |
|
| 279 | + | goto illegal; |
|
| 280 | + | u32 width = 1U << (f3 & 3); |
|
| 281 | + | address = a + immediate; |
|
| 282 | + | if (!machine_bus(m, h, address, width, 0, &result, &cause)) |
|
| 283 | + | goto fault; |
|
| 284 | + | if (!(f3 & 4)) |
|
| 285 | + | result = sext(result, width * 8); |
|
| 286 | + | break; |
|
| 287 | + | } |
|
| 288 | + | case OP_STORE: { |
|
| 289 | + | if (f3 > 3) |
|
| 290 | + | goto illegal; |
|
| 291 | + | u32 width = 1U << f3; |
|
| 292 | + | address = a + sext(((ins >> 25) << 5) | ((ins >> 7) & 31), 12); |
|
| 293 | + | if (!machine_bus(m, h, address, width, ACCESS_STORE, &b, &cause)) |
|
| 294 | + | goto fault; |
|
| 295 | + | write_rd = false; |
|
| 296 | + | break; |
|
| 297 | + | } |
|
| 298 | + | case OP_IMM: |
|
| 299 | + | switch (f3) { |
|
| 300 | + | case 0: |
|
| 301 | + | result = a + immediate; |
|
| 302 | + | break; |
|
| 303 | + | case 2: |
|
| 304 | + | result = (i64)a < (i64)immediate; |
|
| 305 | + | break; |
|
| 306 | + | case 3: |
|
| 307 | + | result = a < immediate; |
|
| 308 | + | break; |
|
| 309 | + | case 4: |
|
| 310 | + | result = a ^ immediate; |
|
| 311 | + | break; |
|
| 312 | + | case 6: |
|
| 313 | + | result = a | immediate; |
|
| 314 | + | break; |
|
| 315 | + | case 7: |
|
| 316 | + | result = a & immediate; |
|
| 317 | + | break; |
|
| 318 | + | case 1: |
|
| 319 | + | if (ins >> 26) |
|
| 320 | + | goto illegal; |
|
| 321 | + | result = a << ((ins >> 20) & 63); |
|
| 322 | + | break; |
|
| 323 | + | case 5: |
|
| 324 | + | if ((ins >> 26) != 0 && (ins >> 26) != 0x10) |
|
| 325 | + | goto illegal; |
|
| 326 | + | result = (ins >> 26) ? (u64)((i64)a >> ((ins >> 20) & 63)) |
|
| 327 | + | : a >> ((ins >> 20) & 63); |
|
| 328 | + | break; |
|
| 329 | + | } |
|
| 330 | + | break; |
|
| 331 | + | case OP_IMM_32: |
|
| 332 | + | if (f3 == 0) |
|
| 333 | + | result = a + immediate; |
|
| 334 | + | else if (f3 == 1 && f7 == 0) |
|
| 335 | + | result = (u32)a << rs2; |
|
| 336 | + | else if (f3 == 5 && f7 == 0) |
|
| 337 | + | result = (u32)a >> rs2; |
|
| 338 | + | else if (f3 == 5 && f7 == 0x20) |
|
| 339 | + | result = (u32)((i32)a >> rs2); |
|
| 340 | + | else |
|
| 341 | + | goto illegal; |
|
| 342 | + | result = sext(result, 32); |
|
| 343 | + | break; |
|
| 344 | + | case OP_OP: |
|
| 345 | + | case OP_OP_32: |
|
| 346 | + | if (!register_op(ins, a, b, &result)) |
|
| 347 | + | goto illegal; |
|
| 348 | + | break; |
|
| 349 | + | case OP_AMO: { |
|
| 350 | + | if (f3 != 2 && f3 != 3) |
|
| 351 | + | goto illegal; |
|
| 352 | + | u32 operation = ins >> 27; |
|
| 353 | + | switch (operation) { |
|
| 354 | + | case 2: |
|
| 355 | + | if (rs2) |
|
| 356 | + | goto illegal; |
|
| 357 | + | break; |
|
| 358 | + | case 0: |
|
| 359 | + | case 1: |
|
| 360 | + | case 3: |
|
| 361 | + | case 4: |
|
| 362 | + | case 8: |
|
| 363 | + | case 12: |
|
| 364 | + | case 16: |
|
| 365 | + | case 20: |
|
| 366 | + | case 24: |
|
| 367 | + | case 28: |
|
| 368 | + | break; |
|
| 369 | + | default: |
|
| 370 | + | goto illegal; |
|
| 371 | + | } |
|
| 372 | + | address = a; |
|
| 373 | + | if (!atomic(m, h, ins, address, b, &result, &cause)) |
|
| 374 | + | goto fault; |
|
| 375 | + | break; |
|
| 376 | + | } |
|
| 377 | + | case OP_FENCE: |
|
| 378 | + | if (rs1 || rd) |
|
| 379 | + | goto illegal; |
|
| 380 | + | if (f3 == 0) { |
|
| 381 | + | u32 fm = ins >> 28; |
|
| 382 | + | if (fm != 0 && !(fm == 8 && ((ins >> 20) & 255) == 0x33)) |
|
| 383 | + | goto illegal; |
|
| 384 | + | } else if (f3 != 1 || (ins >> 20)) |
|
| 385 | + | goto illegal; |
|
| 386 | + | /* The bus is strongly ordered; fetch is uncached, including FENCE.I. */ |
|
| 387 | + | write_rd = false; |
|
| 388 | + | break; |
|
| 389 | + | case OP_SYSTEM: |
|
| 390 | + | write_rd = false; |
|
| 391 | + | if (f3) { |
|
| 392 | + | if (!machine_csr(m, h, ins)) |
|
| 393 | + | goto illegal; |
|
| 394 | + | } else if (ins == 0x00000073) { /* ecall */ |
|
| 395 | + | machine_trap( |
|
| 396 | + | m, h, h->privilege == 0 ? CAUSE_ECALL_U : CAUSE_ECALL_M, 0 |
|
| 397 | + | ); |
|
| 398 | + | return; |
|
| 399 | + | } else if (ins == 0x00100073) { /* ebreak */ |
|
| 400 | + | machine_trap(m, h, CAUSE_BREAKPOINT, h->pc); |
|
| 401 | + | return; |
|
| 402 | + | } else if (ins == 0x10500073) { /* wfi */ |
|
| 403 | + | h->waiting = true; |
|
| 404 | + | } else if (ins == 0x30200073) { /* mret */ |
|
| 405 | + | if (h->privilege != 3) |
|
| 406 | + | goto illegal; |
|
| 407 | + | h->privilege = (h->mstatus >> 11) & 3; |
|
| 408 | + | h->mstatus = (h->mstatus & ~(MSTATUS_MIE | MSTATUS_MPP)) | |
|
| 409 | + | ((h->mstatus & MSTATUS_MPIE) >> 4) | MSTATUS_MPIE; |
|
| 410 | + | next = h->mepc; |
|
| 411 | + | } else |
|
| 412 | + | goto illegal; |
|
| 413 | + | break; |
|
| 414 | + | default: |
|
| 415 | + | goto illegal; |
|
| 416 | + | } |
|
| 417 | + | goto retire; |
|
| 418 | + | ||
| 419 | + | target: |
|
| 420 | + | if (next & 3) { |
|
| 421 | + | address = next; |
|
| 422 | + | cause = CAUSE_FETCH_MISALIGNED; |
|
| 423 | + | goto fault; |
|
| 424 | + | } |
|
| 425 | + | retire: |
|
| 426 | + | if (write_rd && rd) |
|
| 427 | + | h->regs[rd] = result; |
|
| 428 | + | h->pc = next; |
|
| 429 | + | if (!h->wrote_instret) |
|
| 430 | + | h->minstret++; |
|
| 431 | + | m->retired++; |
|
| 432 | + | return; |
|
| 433 | + | illegal: |
|
| 434 | + | machine_trap(m, h, CAUSE_ILLEGAL, ins); |
|
| 435 | + | return; |
|
| 436 | + | fault: |
|
| 437 | + | machine_trap(m, h, cause, address); |
|
| 438 | + | } |
machine_csr.c
added
+156 -0
| 1 | + | /* Traps and the machine CSR file. */ |
|
| 2 | + | #include <stdio.h> |
|
| 3 | + | ||
| 4 | + | #include "machine_internal.h" |
|
| 5 | + | ||
| 6 | + | /* Implemented CSR numbers. */ |
|
| 7 | + | enum { |
|
| 8 | + | CSR_MSTATUS = 0x300, |
|
| 9 | + | CSR_MIE = 0x304, |
|
| 10 | + | CSR_MTVEC = 0x305, |
|
| 11 | + | CSR_MSCRATCH = 0x340, |
|
| 12 | + | CSR_MEPC = 0x341, |
|
| 13 | + | CSR_MCAUSE = 0x342, |
|
| 14 | + | CSR_MTVAL = 0x343, |
|
| 15 | + | CSR_MIP = 0x344, |
|
| 16 | + | CSR_MINSTRET = 0xb02, |
|
| 17 | + | CSR_INSTRET = 0xc02, |
|
| 18 | + | CSR_MHARTID = 0xf14, |
|
| 19 | + | }; |
|
| 20 | + | ||
| 21 | + | void machine_trap( |
|
| 22 | + | struct machine *m, struct machine_hart *h, u64 cause, u64 value |
|
| 23 | + | ) { |
|
| 24 | + | u64 old = h->mstatus; |
|
| 25 | + | h->mstatus = (old & ~(MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_MIE)) | |
|
| 26 | + | ((u64)h->privilege << 11) | ((old & MSTATUS_MIE) << 4); |
|
| 27 | + | h->mepc = h->pc & ~3ULL; |
|
| 28 | + | h->mcause = cause; |
|
| 29 | + | h->mtval = value; |
|
| 30 | + | h->privilege = 3; |
|
| 31 | + | h->waiting = false; |
|
| 32 | + | h->reserved = false; |
|
| 33 | + | if (m->config->trace) |
|
| 34 | + | fprintf( |
|
| 35 | + | stderr, |
|
| 36 | + | "hart=%u trap pc=0x%llx cause=0x%llx value=0x%llx\n", |
|
| 37 | + | h->id, |
|
| 38 | + | h->pc, |
|
| 39 | + | cause, |
|
| 40 | + | value |
|
| 41 | + | ); |
|
| 42 | + | h->pc = h->mtvec & ~3ULL; |
|
| 43 | + | if ((cause & MCAUSE_INTERRUPT) && (h->mtvec & 3) == 1) |
|
| 44 | + | h->pc += 4 * (cause & ~MCAUSE_INTERRUPT); |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | /* Keep the implemented mstatus fields; MPP holds only M or U. */ |
|
| 48 | + | static u64 status_warl(u64 value) { |
|
| 49 | + | u64 result = value & (MSTATUS_MIE | MSTATUS_MPIE | MSTATUS_MPP); |
|
| 50 | + | if ((result & MSTATUS_MPP) != MSTATUS_MPP) |
|
| 51 | + | result &= ~MSTATUS_MPP; |
|
| 52 | + | return result; |
|
| 53 | + | } |
|
| 54 | + | ||
| 55 | + | static bool csr_read( |
|
| 56 | + | struct machine *m, struct machine_hart *h, u32 csr, u64 *value |
|
| 57 | + | ) { |
|
| 58 | + | if (h->privilege < ((csr >> 8) & 3)) |
|
| 59 | + | return false; |
|
| 60 | + | if (csr == CSR_INSTRET && h->privilege == 0) |
|
| 61 | + | return false; /* mcounteren is zero. */ |
|
| 62 | + | switch (csr) { |
|
| 63 | + | case CSR_MSTATUS: |
|
| 64 | + | *value = h->mstatus | (2ULL << 32); /* UXL: U mode is 64-bit. */ |
|
| 65 | + | break; |
|
| 66 | + | case CSR_MIE: |
|
| 67 | + | *value = h->mie; |
|
| 68 | + | break; |
|
| 69 | + | case CSR_MTVEC: |
|
| 70 | + | *value = h->mtvec; |
|
| 71 | + | break; |
|
| 72 | + | case CSR_MSCRATCH: |
|
| 73 | + | *value = h->mscratch; |
|
| 74 | + | break; |
|
| 75 | + | case CSR_MEPC: |
|
| 76 | + | *value = h->mepc; |
|
| 77 | + | break; |
|
| 78 | + | case CSR_MCAUSE: |
|
| 79 | + | *value = h->mcause; |
|
| 80 | + | break; |
|
| 81 | + | case CSR_MTVAL: |
|
| 82 | + | *value = h->mtval; |
|
| 83 | + | break; |
|
| 84 | + | case CSR_MIP: |
|
| 85 | + | *value = machine_pending(m, h); |
|
| 86 | + | break; |
|
| 87 | + | case CSR_MINSTRET: |
|
| 88 | + | case CSR_INSTRET: |
|
| 89 | + | *value = h->minstret; |
|
| 90 | + | break; |
|
| 91 | + | case CSR_MHARTID: |
|
| 92 | + | *value = h->id; |
|
| 93 | + | break; |
|
| 94 | + | default: |
|
| 95 | + | return false; |
|
| 96 | + | } |
|
| 97 | + | return true; |
|
| 98 | + | } |
|
| 99 | + | ||
| 100 | + | static void csr_write(struct machine_hart *h, u32 csr, u64 value) { |
|
| 101 | + | switch (csr) { |
|
| 102 | + | case CSR_MSTATUS: |
|
| 103 | + | h->mstatus = status_warl(value); |
|
| 104 | + | break; |
|
| 105 | + | case CSR_MIE: |
|
| 106 | + | h->mie = value & MIP_ALL; |
|
| 107 | + | break; |
|
| 108 | + | case CSR_MTVEC: |
|
| 109 | + | h->mtvec = (value & ~3ULL) | ((value & 3) == 1 ? 1 : 0); |
|
| 110 | + | break; |
|
| 111 | + | case CSR_MSCRATCH: |
|
| 112 | + | h->mscratch = value; |
|
| 113 | + | break; |
|
| 114 | + | case CSR_MEPC: |
|
| 115 | + | h->mepc = value & ~3ULL; |
|
| 116 | + | break; |
|
| 117 | + | case CSR_MCAUSE: |
|
| 118 | + | h->mcause = value; |
|
| 119 | + | break; |
|
| 120 | + | case CSR_MTVAL: |
|
| 121 | + | h->mtval = value; |
|
| 122 | + | break; |
|
| 123 | + | case CSR_MIP: |
|
| 124 | + | break; /* MSIP/MTIP/MEIP are device-owned read-only fields. */ |
|
| 125 | + | case CSR_MINSTRET: |
|
| 126 | + | /* An explicit write suppresses this instruction's increment. */ |
|
| 127 | + | h->minstret = value; |
|
| 128 | + | h->wrote_instret = true; |
|
| 129 | + | break; |
|
| 130 | + | } |
|
| 131 | + | } |
|
| 132 | + | ||
| 133 | + | bool machine_csr(struct machine *m, struct machine_hart *h, u32 instruction) { |
|
| 134 | + | u32 funct = (instruction >> 12) & 7; |
|
| 135 | + | u32 csr = instruction >> 20; |
|
| 136 | + | u32 rd = (instruction >> 7) & 31; |
|
| 137 | + | u32 rs = (instruction >> 15) & 31; |
|
| 138 | + | if (funct == 0 || funct == 4) |
|
| 139 | + | return false; |
|
| 140 | + | bool writes = (funct & 3) == 1 || rs != 0; |
|
| 141 | + | if (writes && (csr >> 10) == 3) |
|
| 142 | + | return false; |
|
| 143 | + | u64 old; |
|
| 144 | + | if (!csr_read(m, h, csr, &old)) |
|
| 145 | + | return false; |
|
| 146 | + | u64 source = (funct & 4) ? rs : h->regs[rs]; |
|
| 147 | + | if (writes) { |
|
| 148 | + | u64 value = (funct & 3) == 1 ? source |
|
| 149 | + | : (funct & 3) == 2 ? old | source |
|
| 150 | + | : old & ~source; |
|
| 151 | + | csr_write(h, csr, value); |
|
| 152 | + | } |
|
| 153 | + | if (rd) |
|
| 154 | + | h->regs[rd] = old; |
|
| 155 | + | return true; |
|
| 156 | + | } |
machine_devices.c
added
+348 -0
| 1 | + | /* Physical bus, CLINT, PLIC, UART and finish device. */ |
|
| 2 | + | #include <stdint.h> |
|
| 3 | + | #include <stdio.h> |
|
| 4 | + | ||
| 5 | + | #include "machine_internal.h" |
|
| 6 | + | ||
| 7 | + | bool machine_overlap(u64 a, u64 asize, u64 b, u64 bsize) { |
|
| 8 | + | if (!asize || !bsize) |
|
| 9 | + | return false; |
|
| 10 | + | return a <= b ? b - a < asize : a - b < bsize; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /* Return whether [address, address + width) lies in RAM, which lies above |
|
| 14 | + | * every device aperture. */ |
|
| 15 | + | static bool ram_range(const struct machine *m, u64 address, u32 width) { |
|
| 16 | + | return address >= MACHINE_RAM_BASE && |
|
| 17 | + | address - MACHINE_RAM_BASE < m->config->memory_size && |
|
| 18 | + | width <= m->config->memory_size - (address - MACHINE_RAM_BASE); |
|
| 19 | + | } |
|
| 20 | + | ||
| 21 | + | /* Invalidate all reservations intersected by a store to RAM. */ |
|
| 22 | + | static void invalidate(struct machine *m, u64 address, u32 width) { |
|
| 23 | + | for (u32 i = 0; i < m->config->hart_count; i++) { |
|
| 24 | + | struct machine_hart *h = &m->harts[i]; |
|
| 25 | + | if (h->reserved && |
|
| 26 | + | machine_overlap( |
|
| 27 | + | address, width, h->reservation, h->reservation_size |
|
| 28 | + | )) |
|
| 29 | + | h->reserved = false; |
|
| 30 | + | } |
|
| 31 | + | } |
|
| 32 | + | ||
| 33 | + | /* Choose highest priority, then lowest source ID as required by the PLIC. */ |
|
| 34 | + | static u32 plic_best(const struct machine *m, u32 context, bool notification) { |
|
| 35 | + | u32 eligible = m->plic.pending & m->plic.enable[context]; |
|
| 36 | + | if (!eligible) |
|
| 37 | + | return 0; |
|
| 38 | + | u32 best = 0, priority = notification ? m->plic.threshold[context] : 0; |
|
| 39 | + | for (u32 id = 1; id <= MACHINE_IRQ_COUNT; id++) { |
|
| 40 | + | if ((eligible & (1U << id)) && m->plic.priority[id] > priority) { |
|
| 41 | + | best = id; |
|
| 42 | + | priority = m->plic.priority[id]; |
|
| 43 | + | } |
|
| 44 | + | } |
|
| 45 | + | return best; |
|
| 46 | + | } |
|
| 47 | + | ||
| 48 | + | /* The UART interrupt line is asserted while an enabled THRE interrupt waits. */ |
|
| 49 | + | static bool uart_interrupt(const struct machine *m) { |
|
| 50 | + | return (m->uart.ier & 2) && m->uart.tx_irq; |
|
| 51 | + | } |
|
| 52 | + | ||
| 53 | + | void machine_devices_update(struct machine *m) { |
|
| 54 | + | if (!m->injected_irq && m->config->irq_source && |
|
| 55 | + | m->ticks >= m->config->irq_at) { |
|
| 56 | + | u32 bit = 1U << m->config->irq_source; |
|
| 57 | + | if (!(m->plic.active & bit)) |
|
| 58 | + | m->plic.pending |= bit; |
|
| 59 | + | m->injected_irq = true; |
|
| 60 | + | } |
|
| 61 | + | u32 bit = 1U << MACHINE_UART_IRQ; |
|
| 62 | + | if (uart_interrupt(m)) |
|
| 63 | + | m->plic.level |= bit; |
|
| 64 | + | else |
|
| 65 | + | m->plic.level &= ~bit; |
|
| 66 | + | m->plic.pending |= m->plic.level & ~m->plic.active; |
|
| 67 | + | } |
|
| 68 | + | ||
| 69 | + | bool machine_plic_notify(const struct machine *m, u32 context) { |
|
| 70 | + | return plic_best(m, context, true) != 0; |
|
| 71 | + | } |
|
| 72 | + | ||
| 73 | + | /* Lower *next to candidate when candidate lies after now. */ |
|
| 74 | + | static void earlier(u64 now, u64 candidate, u64 *next) { |
|
| 75 | + | if (candidate > now && candidate < *next) |
|
| 76 | + | *next = candidate; |
|
| 77 | + | } |
|
| 78 | + | ||
| 79 | + | u64 machine_next_event(const struct machine *m) { |
|
| 80 | + | u64 next = m->config->max_steps; |
|
| 81 | + | for (u32 id = 0; id < m->config->hart_count; id++) { |
|
| 82 | + | u64 compare = m->clint.compare[id]; |
|
| 83 | + | if ((m->harts[id].mie & MIP_MTIP) && compare > m->clint.time) { |
|
| 84 | + | u64 delta = compare - m->clint.time; |
|
| 85 | + | if (delta <= UINT64_MAX - m->ticks) |
|
| 86 | + | earlier(m->ticks, m->ticks + delta, &next); |
|
| 87 | + | } |
|
| 88 | + | } |
|
| 89 | + | if (!m->injected_irq && m->config->irq_source) |
|
| 90 | + | earlier(m->ticks, m->config->irq_at, &next); |
|
| 91 | + | return next; |
|
| 92 | + | } |
|
| 93 | + | ||
| 94 | + | /* CLINT supports naturally aligned RV32 halves and RV64 timer accesses. */ |
|
| 95 | + | static bool clint_access( |
|
| 96 | + | struct machine *m, u32 offset, u32 width, bool store, u64 *value |
|
| 97 | + | ) { |
|
| 98 | + | if (offset < 4 * m->config->hart_count && width == 4) { |
|
| 99 | + | u32 *word = &m->clint.software[offset / 4]; |
|
| 100 | + | if (store) |
|
| 101 | + | *word = (u32)*value & 1; |
|
| 102 | + | else |
|
| 103 | + | *value = *word; |
|
| 104 | + | return true; |
|
| 105 | + | } |
|
| 106 | + | u64 *reg; |
|
| 107 | + | u32 half; |
|
| 108 | + | if (offset >= 0x4000 && offset < 0x4000 + 8 * m->config->hart_count) { |
|
| 109 | + | reg = &m->clint.compare[(offset - 0x4000) / 8]; |
|
| 110 | + | half = offset & 7; |
|
| 111 | + | } else if (offset >= 0xbff8 && offset < 0xc000) { |
|
| 112 | + | reg = &m->clint.time; |
|
| 113 | + | half = offset - 0xbff8; |
|
| 114 | + | } else { |
|
| 115 | + | return false; |
|
| 116 | + | } |
|
| 117 | + | if (width == 8 && half == 0) { |
|
| 118 | + | if (store) |
|
| 119 | + | *reg = *value; |
|
| 120 | + | else |
|
| 121 | + | *value = *reg; |
|
| 122 | + | } else if (width == 4 && (half == 0 || half == 4)) { |
|
| 123 | + | u32 shift = half * 8; |
|
| 124 | + | if (store) |
|
| 125 | + | *reg = (*reg & ~(0xffffffffULL << shift)) | |
|
| 126 | + | ((u64)(u32)*value << shift); |
|
| 127 | + | else |
|
| 128 | + | *value = (u32)(*reg >> shift); |
|
| 129 | + | } else { |
|
| 130 | + | return false; |
|
| 131 | + | } |
|
| 132 | + | return true; |
|
| 133 | + | } |
|
| 134 | + | ||
| 135 | + | static bool plic_access( |
|
| 136 | + | struct machine *m, u32 offset, u32 width, bool store, u64 *value |
|
| 137 | + | ) { |
|
| 138 | + | struct machine_plic *p = &m->plic; |
|
| 139 | + | if (width != 4) |
|
| 140 | + | return false; |
|
| 141 | + | if (offset <= MACHINE_IRQ_COUNT * 4) { |
|
| 142 | + | u32 id = offset / 4; |
|
| 143 | + | if (store) { |
|
| 144 | + | if (id) |
|
| 145 | + | p->priority[id] = (u32)*value & 7; |
|
| 146 | + | } else |
|
| 147 | + | *value = p->priority[id]; |
|
| 148 | + | return true; |
|
| 149 | + | } |
|
| 150 | + | if (offset == 0x1000) { |
|
| 151 | + | if (store) |
|
| 152 | + | return false; |
|
| 153 | + | *value = p->pending; |
|
| 154 | + | return true; |
|
| 155 | + | } |
|
| 156 | + | if (offset >= 0x2000 && offset < 0x2000 + m->config->hart_count * 0x80 && |
|
| 157 | + | (offset & 0x7f) == 0) { |
|
| 158 | + | u32 context = (offset - 0x2000) / 0x80; |
|
| 159 | + | if (store) |
|
| 160 | + | p->enable[context] = (u32)*value & ~1U; |
|
| 161 | + | else |
|
| 162 | + | *value = p->enable[context]; |
|
| 163 | + | return true; |
|
| 164 | + | } |
|
| 165 | + | if (offset >= 0x200000 && |
|
| 166 | + | offset < 0x200000 + m->config->hart_count * 0x1000) { |
|
| 167 | + | u32 context = (offset - 0x200000) / 0x1000; |
|
| 168 | + | u32 reg = offset & 0xfff; |
|
| 169 | + | if (reg == 0) { |
|
| 170 | + | if (store) |
|
| 171 | + | p->threshold[context] = (u32)*value & 7; |
|
| 172 | + | else |
|
| 173 | + | *value = p->threshold[context]; |
|
| 174 | + | return true; |
|
| 175 | + | } |
|
| 176 | + | if (reg == 4) { |
|
| 177 | + | if (store) { |
|
| 178 | + | u32 id = (u32)*value; |
|
| 179 | + | if (id && id <= MACHINE_IRQ_COUNT && (p->active & (1U << id)) && |
|
| 180 | + | p->owner[id] == context && |
|
| 181 | + | (p->enable[context] & (1U << id))) { |
|
| 182 | + | p->active &= ~(1U << id); |
|
| 183 | + | p->pending |= p->level & (1U << id); |
|
| 184 | + | } |
|
| 185 | + | } else { |
|
| 186 | + | u32 id = plic_best(m, context, false); |
|
| 187 | + | *value = id; |
|
| 188 | + | if (id) { |
|
| 189 | + | p->pending &= ~(1U << id); |
|
| 190 | + | p->active |= 1U << id; |
|
| 191 | + | p->owner[id] = (u8)context; |
|
| 192 | + | } |
|
| 193 | + | } |
|
| 194 | + | return true; |
|
| 195 | + | } |
|
| 196 | + | } |
|
| 197 | + | return false; |
|
| 198 | + | } |
|
| 199 | + | ||
| 200 | + | /* Registers: THR at 0, IER at 1, IIR at 2 (read) and LSR at 5 (read). */ |
|
| 201 | + | static bool uart_access( |
|
| 202 | + | struct machine *m, u32 offset, u32 width, bool store, u64 *value |
|
| 203 | + | ) { |
|
| 204 | + | struct machine_uart *u = &m->uart; |
|
| 205 | + | if (width != 1) |
|
| 206 | + | return false; |
|
| 207 | + | if (store) { |
|
| 208 | + | switch (offset) { |
|
| 209 | + | case 0: |
|
| 210 | + | if (fputc((u8)*value, stdout) == EOF || fflush(stdout) == EOF) { |
|
| 211 | + | fprintf(stderr, "machine UART output failed\n"); |
|
| 212 | + | m->finished = true; |
|
| 213 | + | m->result = 2; |
|
| 214 | + | } |
|
| 215 | + | u->tx_irq = true; |
|
| 216 | + | break; |
|
| 217 | + | case 1: |
|
| 218 | + | if (!(u->ier & 2) && ((u8)*value & 2)) |
|
| 219 | + | u->tx_irq = true; |
|
| 220 | + | u->ier = (u8)*value & 2; |
|
| 221 | + | break; |
|
| 222 | + | default: |
|
| 223 | + | return false; |
|
| 224 | + | } |
|
| 225 | + | return true; |
|
| 226 | + | } |
|
| 227 | + | switch (offset) { |
|
| 228 | + | case 1: |
|
| 229 | + | *value = u->ier; |
|
| 230 | + | break; |
|
| 231 | + | case 2: |
|
| 232 | + | if (uart_interrupt(m)) { |
|
| 233 | + | *value = 2; /* THR empty. */ |
|
| 234 | + | u->tx_irq = false; |
|
| 235 | + | } else |
|
| 236 | + | *value = 1; /* No interrupt pending. */ |
|
| 237 | + | break; |
|
| 238 | + | case 5: |
|
| 239 | + | *value = 0x60; |
|
| 240 | + | break; |
|
| 241 | + | default: |
|
| 242 | + | return false; |
|
| 243 | + | } |
|
| 244 | + | return true; |
|
| 245 | + | } |
|
| 246 | + | ||
| 247 | + | /* The finish register reads as zero and accepts the success value 0x5555 or |
|
| 248 | + | * a failure value 0xNNNN3333, which stop the machine. */ |
|
| 249 | + | static bool finish_access( |
|
| 250 | + | struct machine *m, struct machine_hart *h, bool store, u64 *value |
|
| 251 | + | ) { |
|
| 252 | + | if (!store) { |
|
| 253 | + | *value = 0; |
|
| 254 | + | return true; |
|
| 255 | + | } |
|
| 256 | + | u32 code = (u32)*value; |
|
| 257 | + | if (code != 0x5555 && (code & 0xffff) != 0x3333) |
|
| 258 | + | return false; |
|
| 259 | + | m->finished = true; |
|
| 260 | + | m->result = code == 0x5555 ? 0 : 1; |
|
| 261 | + | if (m->result) |
|
| 262 | + | fprintf( |
|
| 263 | + | stderr, |
|
| 264 | + | "machine guest failure: code=%u hart=%u pc=0x%llx\n", |
|
| 265 | + | code >> 16, |
|
| 266 | + | h->id, |
|
| 267 | + | h->pc |
|
| 268 | + | ); |
|
| 269 | + | return true; |
|
| 270 | + | } |
|
| 271 | + | ||
| 272 | + | bool machine_store_probe( |
|
| 273 | + | const struct machine *m, |
|
| 274 | + | const struct machine_hart *h, |
|
| 275 | + | u64 address, |
|
| 276 | + | u32 width, |
|
| 277 | + | u64 *cause |
|
| 278 | + | ) { |
|
| 279 | + | bool misaligned = (address & (width - 1)) != 0; |
|
| 280 | + | *cause = misaligned ? CAUSE_STORE_MISALIGNED : CAUSE_STORE_FAULT; |
|
| 281 | + | if (misaligned || !ram_range(m, address, width)) |
|
| 282 | + | return false; |
|
| 283 | + | return h->privilege != 0 || |
|
| 284 | + | (!machine_overlap( |
|
| 285 | + | address, width, m->config->text.base, m->config->text.size |
|
| 286 | + | ) && |
|
| 287 | + | !machine_overlap( |
|
| 288 | + | address, width, m->config->rodata.base, m->config->rodata.size |
|
| 289 | + | )); |
|
| 290 | + | } |
|
| 291 | + | ||
| 292 | + | bool machine_bus( |
|
| 293 | + | struct machine *m, |
|
| 294 | + | struct machine_hart *h, |
|
| 295 | + | u64 address, |
|
| 296 | + | u32 width, |
|
| 297 | + | u32 access, |
|
| 298 | + | u64 *value, |
|
| 299 | + | u64 *cause |
|
| 300 | + | ) { |
|
| 301 | + | bool store = (access & ACCESS_STORE) != 0; |
|
| 302 | + | bool fetch = (access & ACCESS_FETCH) != 0; |
|
| 303 | + | if (address & (width - 1)) { |
|
| 304 | + | *cause = fetch ? CAUSE_FETCH_MISALIGNED |
|
| 305 | + | : store ? CAUSE_STORE_MISALIGNED |
|
| 306 | + | : CAUSE_LOAD_MISALIGNED; |
|
| 307 | + | return false; |
|
| 308 | + | } |
|
| 309 | + | *cause = fetch ? CAUSE_FETCH_FAULT |
|
| 310 | + | : store ? CAUSE_STORE_FAULT |
|
| 311 | + | : CAUSE_LOAD_FAULT; |
|
| 312 | + | if (ram_range(m, address, width)) { |
|
| 313 | + | u8 *ram = m->config->ram + (address - MACHINE_RAM_BASE); |
|
| 314 | + | if (store) { |
|
| 315 | + | if (!machine_store_probe(m, h, address, width, cause)) |
|
| 316 | + | return false; |
|
| 317 | + | for (u32 i = 0; i < width; i++) |
|
| 318 | + | ram[i] = (u8)(*value >> (i * 8)); |
|
| 319 | + | invalidate(m, address, width); |
|
| 320 | + | } else { |
|
| 321 | + | u64 v = 0; |
|
| 322 | + | for (u32 i = 0; i < width; i++) |
|
| 323 | + | v |= (u64)ram[i] << (i * 8); |
|
| 324 | + | *value = v; |
|
| 325 | + | } |
|
| 326 | + | return true; |
|
| 327 | + | } |
|
| 328 | + | if (access & (ACCESS_FETCH | ACCESS_ATOMIC)) |
|
| 329 | + | return false; |
|
| 330 | + | if (address >= MACHINE_CLINT_BASE && |
|
| 331 | + | address < MACHINE_CLINT_BASE + MACHINE_CLINT_SIZE) |
|
| 332 | + | return clint_access( |
|
| 333 | + | m, (u32)(address - MACHINE_CLINT_BASE), width, store, value |
|
| 334 | + | ); |
|
| 335 | + | if (address >= MACHINE_PLIC_BASE && |
|
| 336 | + | address < MACHINE_PLIC_BASE + MACHINE_PLIC_SIZE) |
|
| 337 | + | return plic_access( |
|
| 338 | + | m, (u32)(address - MACHINE_PLIC_BASE), width, store, value |
|
| 339 | + | ); |
|
| 340 | + | if (address >= MACHINE_UART_BASE && |
|
| 341 | + | address < MACHINE_UART_BASE + MACHINE_UART_SIZE) |
|
| 342 | + | return uart_access( |
|
| 343 | + | m, (u32)(address - MACHINE_UART_BASE), width, store, value |
|
| 344 | + | ); |
|
| 345 | + | if (address == MACHINE_FINISH_BASE && width == 4) |
|
| 346 | + | return finish_access(m, h, store, value); |
|
| 347 | + | return false; |
|
| 348 | + | } |
machine_fdt.c
added
+259 -0
| 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(&f, "riscv,isa", "rv64ima_zicsr_zifencei"); |
|
| 173 | + | begin(&f, "interrupt-controller"); |
|
| 174 | + | scalar(&f, "#interrupt-cells", 1); |
|
| 175 | + | property(&f, "interrupt-controller", "", 0); |
|
| 176 | + | string(&f, "compatible", "riscv,cpu-intc"); |
|
| 177 | + | scalar(&f, "phandle", id + 1); |
|
| 178 | + | end(&f); |
|
| 179 | + | end(&f); |
|
| 180 | + | } |
|
| 181 | + | end(&f); |
|
| 182 | + | ||
| 183 | + | begin(&f, "soc"); |
|
| 184 | + | scalar(&f, "#address-cells", 2); |
|
| 185 | + | scalar(&f, "#size-cells", 2); |
|
| 186 | + | string(&f, "compatible", "simple-bus"); |
|
| 187 | + | property(&f, "ranges", "", 0); |
|
| 188 | + | begin(&f, "clint@2000000"); |
|
| 189 | + | string(&f, "compatible", "riscv,clint0"); |
|
| 190 | + | region(&f, "reg", MACHINE_CLINT_BASE, MACHINE_CLINT_SIZE); |
|
| 191 | + | u8 local_interrupts[MACHINE_MAX_HARTS * 16]; |
|
| 192 | + | for (u32 id = 0; id < c->hart_count; id++) { |
|
| 193 | + | be32(local_interrupts + id * 16, id + 1); |
|
| 194 | + | be32(local_interrupts + id * 16 + 4, 3); |
|
| 195 | + | be32(local_interrupts + id * 16 + 8, id + 1); |
|
| 196 | + | be32(local_interrupts + id * 16 + 12, 7); |
|
| 197 | + | } |
|
| 198 | + | property(&f, "interrupts-extended", local_interrupts, c->hart_count * 16); |
|
| 199 | + | end(&f); |
|
| 200 | + | ||
| 201 | + | begin(&f, "plic@c000000"); |
|
| 202 | + | string(&f, "compatible", "riscv,plic0"); |
|
| 203 | + | region(&f, "reg", MACHINE_PLIC_BASE, MACHINE_PLIC_SIZE); |
|
| 204 | + | scalar(&f, "#interrupt-cells", 1); |
|
| 205 | + | scalar(&f, "#address-cells", 0); |
|
| 206 | + | property(&f, "interrupt-controller", "", 0); |
|
| 207 | + | scalar(&f, "phandle", 0x100); |
|
| 208 | + | scalar(&f, "riscv,ndev", MACHINE_IRQ_COUNT); |
|
| 209 | + | scalar(&f, "riscv,max-priority", 7); |
|
| 210 | + | u8 external_interrupts[MACHINE_MAX_HARTS * 8]; |
|
| 211 | + | for (u32 id = 0; id < c->hart_count; id++) { |
|
| 212 | + | be32(external_interrupts + id * 8, id + 1); |
|
| 213 | + | be32(external_interrupts + id * 8 + 4, 11); |
|
| 214 | + | } |
|
| 215 | + | property(&f, "interrupts-extended", external_interrupts, c->hart_count * 8); |
|
| 216 | + | end(&f); |
|
| 217 | + | ||
| 218 | + | begin(&f, "uart@10000000"); |
|
| 219 | + | string(&f, "compatible", "ns16550a"); |
|
| 220 | + | region(&f, "reg", MACHINE_UART_BASE, 0x100); |
|
| 221 | + | scalar(&f, "clock-frequency", 1843200); |
|
| 222 | + | scalar(&f, "current-speed", 115200); |
|
| 223 | + | scalar(&f, "reg-shift", 0); |
|
| 224 | + | scalar(&f, "reg-io-width", 1); |
|
| 225 | + | scalar(&f, "interrupt-parent", 0x100); |
|
| 226 | + | scalar(&f, "interrupts", MACHINE_UART_IRQ); |
|
| 227 | + | end(&f); |
|
| 228 | + | ||
| 229 | + | begin(&f, "finish@10001000"); |
|
| 230 | + | string(&f, "compatible", "radiant,finish"); |
|
| 231 | + | region(&f, "reg", MACHINE_FINISH_BASE, 4); |
|
| 232 | + | scalar(&f, "radiance,success-value", 0x5555); |
|
| 233 | + | scalar(&f, "radiance,failure-mask", 0xffff); |
|
| 234 | + | scalar(&f, "radiance,failure-value", 0x3333); |
|
| 235 | + | end(&f); |
|
| 236 | + | end(&f); |
|
| 237 | + | end(&f); |
|
| 238 | + | word(&f, 9); |
|
| 239 | + | ||
| 240 | + | u32 reserve_size = (f.reservation_count + 2) * 8; |
|
| 241 | + | u32 structure_offset = 40 + reserve_size; |
|
| 242 | + | u32 strings_offset = structure_offset + f.structure_size; |
|
| 243 | + | u32 total = strings_offset + f.strings_size; |
|
| 244 | + | if (!f.valid || total > MACHINE_FDT_SIZE) |
|
| 245 | + | return false; |
|
| 246 | + | u8 *out = c->ram + (MACHINE_FDT_BASE - MACHINE_RAM_BASE); |
|
| 247 | + | memset(out, 0, MACHINE_FDT_SIZE); |
|
| 248 | + | const u32 header[] = { |
|
| 249 | + | 0xd00dfeed, total, structure_offset, strings_offset, 40, 17, |
|
| 250 | + | 16, 0, f.strings_size, f.structure_size |
|
| 251 | + | }; |
|
| 252 | + | for (u32 i = 0; i < 10; i++) |
|
| 253 | + | be32(out + i * 4, header[i]); |
|
| 254 | + | for (u32 i = 0; i < f.reservation_count; i++) |
|
| 255 | + | be64(out + 40 + i * 8, f.reservations[i]); |
|
| 256 | + | memcpy(out + structure_offset, f.structure, f.structure_size); |
|
| 257 | + | memcpy(out + strings_offset, f.strings, f.strings_size); |
|
| 258 | + | return true; |
|
| 259 | + | } |
machine_internal.h
added
+170 -0
| 1 | + | /* State and interfaces shared by the machine implementation files. */ |
|
| 2 | + | #ifndef MACHINE_INTERNAL_H |
|
| 3 | + | #define MACHINE_INTERNAL_H |
|
| 4 | + | ||
| 5 | + | #include "machine.h" |
|
| 6 | + | ||
| 7 | + | /* Fixed physical addresses, also published in the FDT. */ |
|
| 8 | + | #define MACHINE_FDT_BASE (MACHINE_RAM_BASE + 0x00800000U) |
|
| 9 | + | #define MACHINE_FDT_SIZE 0x00010000U |
|
| 10 | + | #define MACHINE_CLINT_BASE 0x02000000U |
|
| 11 | + | #define MACHINE_CLINT_SIZE 0x00010000U |
|
| 12 | + | #define MACHINE_PLIC_BASE 0x0c000000U |
|
| 13 | + | #define MACHINE_PLIC_SIZE 0x04000000U |
|
| 14 | + | #define MACHINE_UART_BASE 0x10000000U |
|
| 15 | + | #define MACHINE_UART_SIZE 0x00001000U |
|
| 16 | + | #define MACHINE_FINISH_BASE 0x10001000U |
|
| 17 | + | #define MACHINE_TIMEBASE 10000000U |
|
| 18 | + | ||
| 19 | + | /* Implemented mstatus fields. */ |
|
| 20 | + | #define MSTATUS_MIE (1ULL << 3) |
|
| 21 | + | #define MSTATUS_MPIE (1ULL << 7) |
|
| 22 | + | #define MSTATUS_MPP (3ULL << 11) |
|
| 23 | + | ||
| 24 | + | /* Interrupt bits of mip and mie. The bit index is the interrupt cause. */ |
|
| 25 | + | #define MIP_MSIP (1ULL << 3) |
|
| 26 | + | #define MIP_MTIP (1ULL << 7) |
|
| 27 | + | #define MIP_MEIP (1ULL << 11) |
|
| 28 | + | #define MIP_ALL (MIP_MSIP | MIP_MTIP | MIP_MEIP) |
|
| 29 | + | ||
| 30 | + | /* The mcause bit that marks an interrupt. */ |
|
| 31 | + | #define MCAUSE_INTERRUPT (1ULL << 63) |
|
| 32 | + | ||
| 33 | + | /* Synchronous exception causes. */ |
|
| 34 | + | enum machine_cause { |
|
| 35 | + | CAUSE_FETCH_MISALIGNED = 0, |
|
| 36 | + | CAUSE_FETCH_FAULT = 1, |
|
| 37 | + | CAUSE_ILLEGAL = 2, |
|
| 38 | + | CAUSE_BREAKPOINT = 3, |
|
| 39 | + | CAUSE_LOAD_MISALIGNED = 4, |
|
| 40 | + | CAUSE_LOAD_FAULT = 5, |
|
| 41 | + | CAUSE_STORE_MISALIGNED = 6, |
|
| 42 | + | CAUSE_STORE_FAULT = 7, |
|
| 43 | + | CAUSE_ECALL_U = 8, |
|
| 44 | + | CAUSE_ECALL_M = 11, |
|
| 45 | + | }; |
|
| 46 | + | ||
| 47 | + | /* Bus access flags. A plain load has none. */ |
|
| 48 | + | enum machine_access { |
|
| 49 | + | ACCESS_STORE = 1, |
|
| 50 | + | ACCESS_FETCH = 2, |
|
| 51 | + | ACCESS_ATOMIC = 4, /* Part of an LR, SC or AMO, which only reach RAM. */ |
|
| 52 | + | }; |
|
| 53 | + | ||
| 54 | + | /* Independent architectural state of one RV64IMA hart. */ |
|
| 55 | + | struct machine_hart { |
|
| 56 | + | u64 regs[32]; /* Integer registers; x0 is never written. */ |
|
| 57 | + | u64 pc; /* Full-width physical instruction address. */ |
|
| 58 | + | u64 mstatus; /* Implemented M/U status fields. */ |
|
| 59 | + | u64 mie; /* MSIE, MTIE and MEIE enable bits. */ |
|
| 60 | + | u64 mtvec; /* Aligned trap base and direct/vectored mode. */ |
|
| 61 | + | u64 mscratch; /* Software-owned trap scratch register. */ |
|
| 62 | + | u64 mepc; /* Aligned exception return address. */ |
|
| 63 | + | u64 mcause; /* Last architectural trap cause. */ |
|
| 64 | + | u64 mtval; /* Fault address or illegal instruction. */ |
|
| 65 | + | u64 minstret; /* Retired instructions. */ |
|
| 66 | + | u32 id; /* Stable zero-based hardware hart ID. */ |
|
| 67 | + | u8 privilege; /* Architectural privilege: U=0, M=3. */ |
|
| 68 | + | bool waiting; /* WFI sleep until an enabled interrupt pends. */ |
|
| 69 | + | bool reserved; /* Whether the LR reservation is live. */ |
|
| 70 | + | u64 reservation; /* Exact physical address of the last LR. */ |
|
| 71 | + | u32 reservation_size; /* Width of the last LR, 4 or 8 bytes. */ |
|
| 72 | + | bool wrote_instret; /* The current instruction wrote minstret. */ |
|
| 73 | + | }; |
|
| 74 | + | ||
| 75 | + | /* Shared CLINT state. mtime is writable, so it is kept apart from the |
|
| 76 | + | * scheduler clock. */ |
|
| 77 | + | struct machine_clint { |
|
| 78 | + | u64 time; /* Writable mtime counter. */ |
|
| 79 | + | u64 compare[MACHINE_MAX_HARTS]; /* Per-hart mtimecmp, reset to all ones. */ |
|
| 80 | + | u32 software[MACHINE_MAX_HARTS]; /* Per-hart MSIP bit. */ |
|
| 81 | + | }; |
|
| 82 | + | ||
| 83 | + | /* PLIC gateway state with one M context per enabled hart. */ |
|
| 84 | + | struct machine_plic { |
|
| 85 | + | u32 priority[MACHINE_IRQ_COUNT + 1]; /* Source priority, WARL 0..7. */ |
|
| 86 | + | u32 pending; /* Latched requests not yet claimed. */ |
|
| 87 | + | u32 level; /* Asserted level-sensitive sources. */ |
|
| 88 | + | u32 active; /* Sources between claim and completion. */ |
|
| 89 | + | u8 owner[MACHINE_IRQ_COUNT + 1]; /* Context that owns an active source. */ |
|
| 90 | + | u32 enable[MACHINE_MAX_HARTS]; /* Per-context source enable bits. */ |
|
| 91 | + | u32 threshold[MACHINE_MAX_HARTS]; /* Per-context threshold, WARL 0..7. */ |
|
| 92 | + | }; |
|
| 93 | + | ||
| 94 | + | /* Transmit-only 16550 subset; transmission is synchronous. */ |
|
| 95 | + | struct machine_uart { |
|
| 96 | + | u8 ier; /* Interrupt enable; only THRE (value 2) is modeled. */ |
|
| 97 | + | bool tx_irq; /* THRE interrupt latch: set when the holding register |
|
| 98 | + | empties or THRE is enabled, cleared by IIR reads. */ |
|
| 99 | + | }; |
|
| 100 | + | ||
| 101 | + | /* Whole-machine state, never shared with the hosted interpreter or JIT. */ |
|
| 102 | + | struct machine { |
|
| 103 | + | const struct machine_config *config; /* Borrowed immutable options. */ |
|
| 104 | + | struct machine_hart harts[MACHINE_MAX_HARTS]; /* Independent live harts. */ |
|
| 105 | + | struct machine_clint clint; /* Shared local interrupts. */ |
|
| 106 | + | struct machine_plic plic; /* Shared external interrupts. */ |
|
| 107 | + | struct machine_uart uart; /* Shared console device. */ |
|
| 108 | + | bool injected_irq; /* One-shot edge has occurred. */ |
|
| 109 | + | u64 ticks; /* Monotonic scheduling time. */ |
|
| 110 | + | u64 retired; /* Total retired instructions. */ |
|
| 111 | + | u32 next_hart; /* Round-robin selection. */ |
|
| 112 | + | u64 stack_base[MACHINE_MAX_HARTS]; /* Reserved initial stack bases. */ |
|
| 113 | + | bool finished; /* Explicit platform termination. */ |
|
| 114 | + | int result; /* Host process exit result. */ |
|
| 115 | + | }; |
|
| 116 | + | ||
| 117 | + | /* Return whether two finite physical intervals intersect. */ |
|
| 118 | + | bool machine_overlap(u64 a, u64 asize, u64 b, u64 bsize); |
|
| 119 | + | /* Checked, naturally aligned bus access of 1, 2, 4 or 8 bytes. `access` |
|
| 120 | + | * holds machine_access flags; fetches and atomics reach RAM only. On failure, |
|
| 121 | + | * cause receives the exception code. No access uses a host pointer before |
|
| 122 | + | * range validation. */ |
|
| 123 | + | bool machine_bus( |
|
| 124 | + | struct machine *m, |
|
| 125 | + | struct machine_hart *h, |
|
| 126 | + | u64 address, |
|
| 127 | + | u32 width, |
|
| 128 | + | u32 access, |
|
| 129 | + | u64 *value, |
|
| 130 | + | u64 *cause |
|
| 131 | + | ); |
|
| 132 | + | /* Validate an atomic RAM store without performing it, including failed SC. */ |
|
| 133 | + | bool machine_store_probe( |
|
| 134 | + | const struct machine *m, |
|
| 135 | + | const struct machine_hart *h, |
|
| 136 | + | u64 address, |
|
| 137 | + | u32 width, |
|
| 138 | + | u64 *cause |
|
| 139 | + | ); |
|
| 140 | + | /* Refresh the one-shot edge and level-sensitive device interrupt state. */ |
|
| 141 | + | void machine_devices_update(struct machine *m); |
|
| 142 | + | /* Return the next future timer/edge wake tick, bounded by the run deadline. */ |
|
| 143 | + | u64 machine_next_event(const struct machine *m); |
|
| 144 | + | /* Whether the PLIC has a notification above threshold for a context. */ |
|
| 145 | + | bool machine_plic_notify(const struct machine *m, u32 context); |
|
| 146 | + | /* Derive a hart's hardware mip bits from CLINT and PLIC state. */ |
|
| 147 | + | static inline u64 machine_pending( |
|
| 148 | + | const struct machine *m, const struct machine_hart *h |
|
| 149 | + | ) { |
|
| 150 | + | u64 pending = 0; |
|
| 151 | + | if (m->clint.software[h->id] & 1) |
|
| 152 | + | pending |= MIP_MSIP; |
|
| 153 | + | if (m->clint.time >= m->clint.compare[h->id]) |
|
| 154 | + | pending |= MIP_MTIP; |
|
| 155 | + | if (machine_plic_notify(m, h->id)) |
|
| 156 | + | pending |= MIP_MEIP; |
|
| 157 | + | return pending; |
|
| 158 | + | } |
|
| 159 | + | /* Deliver an exception or interrupt; no instruction retires. */ |
|
| 160 | + | void machine_trap( |
|
| 161 | + | struct machine *m, struct machine_hart *h, u64 cause, u64 value |
|
| 162 | + | ); |
|
| 163 | + | /* Read/modify/write a CSR instruction; false means illegal instruction. */ |
|
| 164 | + | bool machine_csr(struct machine *m, struct machine_hart *h, u32 instruction); |
|
| 165 | + | /* Execute at most one instruction, or take an interrupt. */ |
|
| 166 | + | void machine_step(struct machine *m, struct machine_hart *h); |
|
| 167 | + | /* Write a bounded flattened device tree into the reserved RAM interval. */ |
|
| 168 | + | bool machine_fdt(struct machine *m); |
|
| 169 | + | ||
| 170 | + | #endif /* MACHINE_INTERNAL_H */ |
riscv.h
+2 -1
| 63 | 63 | OP_AUIPC = 0x17, |
|
| 64 | 64 | OP_IMM_32 = 0x1B, /* RV64I: ADDIW, SLLIW, SRLIW, SRAIW */ |
|
| 65 | 65 | OP_OP_32 = 0x3B, /* RV64I: ADDW, SUBW, SLLW, SRLW, SRAW, MULW, DIVW, REMW */ |
|
| 66 | 66 | OP_LUI = 0x37, |
|
| 67 | 67 | OP_SYSTEM = 0x73, |
|
| 68 | - | OP_FENCE = 0x0F |
|
| 68 | + | OP_FENCE = 0x0F, |
|
| 69 | + | OP_AMO = 0x2F /* A extension, machine mode only */ |
|
| 69 | 70 | } opcode_t; |
|
| 70 | 71 | ||
| 71 | 72 | /* Function3 values */ |
|
| 72 | 73 | typedef enum { |
|
| 73 | 74 | /* Memory operations */ |