Prototypes Prototype docsP11 · Docs and assistant
RISC-V computer (P34)
DocsPage 34 of 39

RISC-V computer (P34)

Boot is a RISC-V computer written in plain JavaScript that starts Linux in the page and gives you its shell.

The processor

The processor runs RV32IMA: the base integer instructions, multiply and divide, and the atomic instructions. It has the CSR instructions and fence.i, machine mode and user mode, and the traps and interrupts of the RISC-V privileged specification.

A trap stores the cause and the address in mcause, mepc and mtval, and jumps to the handler in mtvec. MRET returns from it. WFI stops the processor until an interrupt is due. The counters cycle, time and instret are 64 bits.

The processor reads each instruction from RAM and decodes it with shifts and masks. It executes the instruction in one loop over typed arrays and allocates nothing per instruction. It runs in a Web Worker, so the page stays responsive while Linux starts.

The machine

Around the processor is a small machine:

  • 64 MiB of RAM at 0x80000000,
  • a 16550 serial port at 0x10000000, which is the terminal,
  • a CLINT timer at 0x11000000,
  • a syscon register at 0x11100000 that powers off and restarts.

Boot writes a device tree that describes this machine, in the binary format Linux reads. Five instructions of firmware put the tree's address in a register and jump to the kernel.

Time is counted in instructions. The timer moves one tick for every 100 instructions, so Boot is a 100 MHz processor with a 1 MHz timer. When the kernel waits for its next tick, the page sleeps until the tick is due.

Linux

The kernel is Linux 6.8 for 32-bit RISC-V without memory protection, with BusyBox for the shell and its programs. It is the same file anyone can download. In node, Linux reaches its shell prompt after 38,486,010 instructions.

Power on starts the kernel from its first instruction, and the page shows the phase: firmware, kernel, init and shell. Resume at the shell loads a snapshot of the whole machine at the moment the shell printed its prompt. The snapshot holds the registers, the RAM, the timer and the serial port.

The tests

The processor is held against riscv-tests, the official test suite of the RISC-V instruction set. The tests are compiled with clang from the suite's own source and run in the page, one instruction at a time. A test passes when it writes 1 to its tohost address.

75 tests run and pass: 42 for the integer instructions, 8 for multiply and divide, 10 for the atomics and 15 for machine mode. 3 tests are excluded because they test hardware Boot does not have:

  • amocas.w and amocas.d need the compare-and-swap extension, Zacas,
  • pmpaddr needs physical memory protection.

Pressing a test shows its code, with the instruction where it ended marked.

The inspector

The inspector runs a small C program or the kernel one instruction at a time. It shows the registers and the control registers, and disassembles the next instructions the way llvm-objdump does. It counts every instruction it runs by kind. A breakpoint stops the run at an address or a function name.

The disassembler writes the same text as llvm-objdump for all 2,741 different instruction words in the compiled tests.

Previous: Spreadsheet engine (P33). Next: Git in the page (P35).

How Prototype docs is built

The pages are written in Markdown and built into these pages and an index of 221 sections. The assistant answers from the index, and every answer names the sections it was taken from.