The hardware cheatsheet
ortferoThe machine as a program sees it: the processor, the memory map, and the registers of every device. It is enough to bring the machine up and to drive each device from nothing. Addresses, offsets, ids, codes and register values are hexadecimal; counts, sizes and dimensions are decimal, but for the sizes in the memory map.
| V | What there is |
|---|---|
| vtype | SEW 8, 16, 32, 64; LMUL 1, 2, 4, 8. Fractional LMUL sets vill. vta and vma are ignored: tails and masked elements are left undisturbed |
| Config | vsetvli, vsetivli, vsetvl |
| Memory | Unit-stride, strided and indexed, EEW equal to SEW; vl1r.v and vs1r.v only. No vlm, no fault-only-first, no segments |
| Integer | vadd, vsub, vrsub, vand, vor, vxor, vsll, vsrl, vsra, vmin(u), vmax(u), vmerge, vmv.v, vmseq..vmsgt, vsadd(u), vssub(u), vsmul, vssra, vaadd, vmul, vmulh(u, su), vmacc, vnmsac, vwmul(u, su), vwadd(u), vslideup, vslidedown, vslide1up, vslide1down |
| Reduce, mask | vredsum, vredmax(u), vredmin(u), vcpop.m, vfirst.m, the eight mask logicals, vmv.x.s, vmv.s.x |
| Float | SEW 32 and 64: vfadd, vfsub, vfmul, vfdiv, vfmin, vfmax, vfsgnj(n), vfmacc, vfsqrt, vfcvt, vfmv, vfredusum |
| Missing | Illegal instruction: vdiv(u), vrem(u), vrgather, vcompress, vid, viota, vzext, vsext, vmv<n>r.v; narrowing, and widening but vwmul and vwadd; vmadd, vnmsub, vaaddu, vasub(u), vadc, vsbc, vmadc, vmsbc; vredand, vredor, vredxor, vmsbf, vmsif, vmsof; vfrsub, vfrdiv, vfsgnjx, vmfeq and the other float compares, vfclass, vfmsac and the other fused forms but vfmacc, vfredosum, vfredmax, vfredmin |
The CSRs that work:
| CSR | Number | What it does |
|---|---|---|
| mstatus | 300 | MIE (bit 3), MPIE (7), MPP (12:11); nothing else acts |
| mie | 304 | MSIE (3), MTIE (7), MEIE (11) |
| mip | 344 | Read only, live |
| mtvec | 305 | Mode 0 direct, 1 vectored (interrupts at base + 4 * cause) |
| mscratch | 340 | Scratch |
| mepc | 341 | Taken raw by mret |
| mcause | 342 | The cause |
| mtval | 343 | The faulting word or pc |
| mhartid | F14 | 0 |
| marchid | F12 | 616C64616E34, "aldan4" |
| time | C01 | The clint's mtime, 10 MHz |
| instret | C02 | Instructions retired |
| fflags, frm, fcsr | 1, 2, 3 | The float state |
| vstart, vxsat, vxrm, vcsr | 8, 9, A, F | The vector state |
| vl, vtype, vlenb | C20, C21, C22 | Read only |
| tselect, tdata1-3, tinfo, tcontrol | 7A0 to 7A5 | Sdtrig: 8 triggers, mcontrol6 (execute, exact address) and icount |
Any other CSR number is accepted without a trap and lands in a 128-entry array indexed by its low seven bits, so unrelated CSRs alias each other: cycle (C00) reads mstatus, and a write to satp (180), sstatus (100) or mcycle (B00) overwrites mstatus. Keep to the table.
| Address | Size | What is there |
|---|---|---|
| 00100000 | 1000 | Power switch (syscon) |
| 00101000 | 1000 | Real-time clock |
| 02000000 | 10000 | Timer (clint) |
| 0C000000 | 400000 | Interrupt controller (plic) |
| 10000000 | 100 | Serial line (16550) |
| 10001000 | 5 * 1000 | Virtio transports 0 to 4 |
| 30000000 | 100000 | PCI configuration (ECAM) |
| 40000000 | 40000000 | PCI memory window |
| 80000000 | 10000 | Boot ROM |
| 80010000 | FF0000 | RAM |
| 80800000 | 1000 | The device tree, in RAM |
The plic gathers the device lines into mip.MEIP. All sources are level-triggered: a source is pending while its line is high and it is not claimed.
| Source | Device |
|---|---|
| 1 | Virtio 0: host directory |
| 2 | Virtio 1: network |
| 3 | Virtio 2: keyboard |
| 4 | Virtio 3: sound |
| 5 | Virtio 4: display |
| A | Serial line, receive |
| 21 | NVMe (PCI slot 1, INTA) |
| Offset | Register |
|---|---|
| 0 + 4 * id | Priority of source id, 0 to 7; 0 is never |
| 1000 | Pending, one bit per source, three words |
| 2000 | Enable for machine mode, three words |
| 200000 | Threshold, 0 to 7 |
| 200004 | Read: claim, the best source or 0. Write: complete that source |
The clint at 02000000 counts wall-clock time at 10 MHz.
| Offset | Width | Register |
|---|---|---|
| 0 | 32 | msip: bit 0 is mip.MSIP |
| 4000 | 64 | mtimecmp, all ones at power-on |
| BFF8 | 64 | mtime, writable |
A goldfish real-time clock at 00101000: nanoseconds since 1970, from the host's wall clock.
| Offset | Register |
|---|---|
| 0 | TIME_LOW, 32 bits. Read it first: it latches the high half. A write sets the time |
| 4 | TIME_HIGH. Read it second; write it first |
The clock has no alarm and no interrupt.
One register at 00100000, written with sh: 5555 powers the machine off, 7777 reboots it. The host acts at the end of the current slice, so follow the store with a wfi loop.
A 16550 subset at 10000000, byte registers one apart. It needs no set-up: the baud rate is ignored, and a byte written is out at once.
| Offset | Register |
|---|---|
| 0 | Read: RBR, the next byte in. Write: THR, a byte out |
| 1 | IER: bit 0 raises source A while a byte waits |
| 2 | IIR: 4 while a byte waits, else 1 |
| 5 | LSR: 60 always (THR empty), and bit 0 while a byte waits |
LCR, MCR, FCR and SCR are stored and do nothing. Read RBR until LSR bit 0 clears to drop the line.
Five transports, virtio-mmio version 2 only, at 10001000 + 1000 * i with plic source 1 + i. A transport with device id 0 is empty: the host directory and the network are absent on some hosts, so read the id before using one.
| Slot | Device | Id | Queues |
|---|---|---|---|
| 0 | Host directory | 9 | 0 requests |
| 1 | Network | 1 | 0 receive, 1 transmit |
| 2 | Keyboard | 12 | 0 events, 1 status |
| 3 | Sound | 19 | 0 control, 1 events, 2 transmit, 3 receive |
| 4 | Display | 10 | 0 control, 1 cursor |
| Offset | Register |
|---|---|
| 0 | Magic 74726976 |
| 4 | Version 2 |
| 8 | Device id |
| 10, 14 | Device features, and its select (0 or 1) |
| 20, 24 | Driver features, and its select |
| 30 | Queue select |
| 34 | Queue size max: 128 entries |
| 38 | Queue size, a power of two |
| 44 | Queue ready |
| 50 | Notify: write the queue's index |
| 60 | Interrupt status: 1 used ring, 2 config |
| 64 | Interrupt ack: write the bits to clear |
| 70 | Status |
| 80, 84 | Descriptor table, low and high |
| 90, 94 | Available ring, low and high |
| A0, A4 | Used ring, low and high |
| 100 | The device's configuration |
virtio-gpu, 2D only. The screen is 780 x 520 times a scale of 1 or 2, or 0 x 0 with no window. Pixels are 32-bit B8G8R8X8, a little-endian xrgb word.
Every request starts with a 24-byte header: le32 type, le32 flags, le64 fence, le32 context, u8 ring, padding. The reply is a header of its own, 1100 when all is well.
| Type | Request and its fields after the header |
|---|---|
| 100 | Display info: reply 1101 then 16 modes of {x, y, w, h, enabled, flags}; mode 0 is the screen |
| 101 | Create 2D: id, format 2, width, height |
| 106 | Attach backing: id, count (1 to 16), then {le64 address, le32 length, pad} each |
| 103 | Set scanout: x, y, w, h, scanout 0, id |
| 105 | Transfer to host: x, y, w, h, le64 offset, id, pad |
| 104 | Flush: x, y, w, h, id, pad |
| 102 | Unref: id, pad |
| 107 | Detach backing: id, pad |
virtio-sound with one output stream, id 0: signed 16-bit mono at 48 kHz, nothing else. Control requests go on queue 0 and start with le32 code, replies with le32 status: 8000 ok, 8001 bad message, 8002 not supported, 8003 I/O error.
| Code | Request |
|---|---|
| 101 | Set params: stream, buffer bytes, period bytes, features, then u8 channels 1, format 5, rate 7, pad |
| 102 | Prepare: stream |
| 104 | Start: stream |
| 105 | Stop: stream |
| 103 | Release: stream |
| 100 | Info: start, count, size; one 32-byte record |
virtio-net, feature MAC (bit 5) only. The configuration holds the MAC 52:54:00:12:34:56. Every frame has a 12-byte header in front of it, all zero but for a 1 at byte 10 on receive. Frames are Ethernet without the checksum, up to 1514 bytes.
The host answers as a small router:
virtio-9p over the directory aldan in the host user's home. The configuration is le16 length then the tag "aldan". One request to a chain: the message, then room for the reply.
An NVMe controller at PCI 00:01.0, reached through ECAM at 30000000 + device * 8000, so its configuration is at 30008000. Device 0 is a host bridge; nothing else is on the bus.
| Offset | Register |
|---|---|
| 0 | Id 00101B36 |
| 4 | Command: 2 memory, 4 bus master, 400 INTx off |
| 8 | Class 010802 |
| 10, 14 | BAR0, 64-bit, 4000 bytes; nothing assigned at reset |
Bring-up: write BAR0 = 40000000 and BAR1 = 0, set command to 6, then use the controller's registers at 40000000. Configuration stores are whole words.
| Offset | Register |
|---|---|
| 0 | CAP: queues of up to 64 entries, doorbell stride 4, 4 KB pages |
| 14 | CC: write 00460001 to enable |
| 1C | CSTS: bit 0 ready, bit 1 fatal |
| 24 | AQA: admin completion size - 1 at 16, admin submission size - 1 at 0 |
| 28, 30 | ASQ, ACQ: 64-bit page addresses |
| 1000 | Doorbells: admin submission tail, admin completion head, then the same for queue 1 |