41k · Devices¶
Released Thu Nov 5, in class
This is the README OSlings gives every student for 41k_devices (oslings
lesson shows it). The starter code is not published here: it reaches you
through OSlings, in class on the class network. It is extra credit, +0.5: see
Extra Credit. Back to
All Exercises.
Extra credit. Not scheduled in class. Prerequisites: 31k_boot, 21r_unsafe_bridge. Nothing later depends on the code you write here: the next core exercise's skeleton already contains the finished version.
Learn → Understand → Implement. Turn the blind-write UART into a real device driver that talks to the hardware through its registers. You consolidate everything about MMIO, status flags, and
volatile.
Learn¶
Way back in exercise 31k we "printed" by storing a byte to the UART's transmit address and assuming the chip was ready. That works in QEMU at boot, but it isn't how you drive real hardware. A proper device driver communicates with a device through its registers and checks the device's status before each transfer. This exercise builds that driver — and the technique is the same for any memory-mapped device.
Device registers¶
A hardware device exposes a set of small control/status slots called
registers, mapped into the physical address space (MMIO — memory-mapped I/O,
from exercise 31k). The UART (a "Universal Asynchronous Receiver/Transmitter" —
the serial port) is an NS16550A, and several of its one-byte registers sit at
consecutive addresses starting at UART0 (0x1000_0000):
| Offset | Register | Meaning |
|---|---|---|
| 0 | RBR / THR | received byte (read) / byte to transmit (write) |
| 1 | IER | interrupt enable |
| 2 | FCR | FIFO control |
| 3 | LCR | line control (word length, etc.) |
| 4 | MCR | modem control (incl. loopback) |
| 5 | LSR | line status — the chip's current state |
Reading offset 0 gives you an incoming byte; writing offset 0 sends one. The same address means different things for read vs. write — normal for hardware.
The status register and "flags"¶
The key to a correct driver is the Line Status Register (LSR). Its individual bits ("flags") report what the chip is doing. Two matter here:
THRE(bit 5, "Transmit Holding Register Empty") — set when the transmitter has room for another byte. You must wait for this before writingTHR, or you'd overwrite a byte still being sent.DR(bit 0, "Data Ready") — set when a received byte is waiting inRBR. You check this before reading, so you don't read stale/garbage data.
Testing a flag is a bitwise AND with a mask: lsr & THRE != 0. This is the
universal pattern for reading device state — a status register full of
single-bit flags. (We used the same idea for the PTE flags in exercise 33k and
the lock bit in exercise 37k.)
So the driver becomes:
tx_ready()= isLSR & THREset?rx_ready()= isLSR & DRset?putc(c)= spin untiltx_ready(), then writectoTHR.getc()= ifrx_ready(), read and returnRBR, elseNone(returning anOptionbecause there may be nothing to read —Result/Optionerror habits from exercise 40k).
init() (given) sets 8-bit words, turns interrupts off (we poll the status
flags instead), and enables the FIFOs.
volatile, one more time¶
Every register access uses read_volatile/write_volatile. As in exercise 31k,
this forbids the compiler from caching, reordering, or eliminating the access —
essential when the "memory" is a device whose value changes on its own and whose
reads/writes have side effects.
Testing without a keyboard: loopback¶
There's no one typing into the automated test, so how do we test getc? The
UART has a loopback mode (a bit in MCR): while it's on, every byte the
transmitter sends is wired straight back into the receiver. So the test turns
loopback on, putcs a byte, and expects getc to hand the same byte back —
exercising tx_ready, putc, rx_ready, and getc together, end-to-end.
Heads-up on this harness: it reports its own results through a tiny blind-write console (
dbg_*inmain.rs), not through the driver you're writing — because a half-finished driver might hang or print nothing. Your driver is the thing under test, not the thing doing the reporting.
Understand¶
Read rv6/src/uart.rs: the register offsets and LSR/MCR bit constants, the
reg_read/reg_write MMIO helpers, and the given init/set_loopback/puts.
Then read rv6/src/main.rs: it inits the UART, checks tx_ready is set and
rx_ready/getc report "nothing yet", then uses loopback to confirm a byte sent
with putc comes back through getc.
Implement¶
In rv6/src/uart.rs:
tx_ready—LSR & THREis set.rx_ready—LSR & DRis set.putc— spin untiltx_ready(), then write the byte toTHR.getc— ifrx_ready(), returnSome(RBR), elseNone.
Check your work:
Passes when the status flags read correctly and a byte survives the loopback
round-trip — printing OSLINGS:PASS.
Stuck? oslings hint.