A USB keyboard: the xHCI driver and what it is allowed to touch
Milestone 242 (USB host and HID, because on commodity hardware the keyboard is not a UART). How a key pressed on a USB keyboard reaches the shell, what each piece holds, and how to prove it works.
The path a key takes
USB keyboard ──► xHCI controller ──DMA, confined by the IOMMU──► usb_keyboard_driver (EL0)
│ OPERATION_BYTES, CALL
▼
line_editor ──► swish
The driver diffs each eight-byte boot report against the last and turns the difference into Linux
evdev key codes with partsfoundry/universal_serial_bus's boot_keyboard. That module's documentation gives the table's
sources and nife's own choices. The driver feeds those through video_terminal::keymap,
the same layout table the virtio keyboard uses. The bytes go to the line discipline's endpoint in
the framing input (the UART) already uses, so the shell cannot tell the two apart and both work at
once.
Who does what
| Piece | Where | Holds |
|---|---|---|
| Find the controller, place its BAR, route its interrupt (MSI-X, MSI, or INTx) | kernel/src/pci.rs, find_xhci_device |
config space |
| Take it from the PC firmware (USB Legacy Support), draw the register window, confine its DMA | kernel/src/extensible_host_controller_interface.rs |
EL1 |
| What the driver is handed and refused | kernel/src/user/usb_keyboard_service.rs |
the Spawn literal |
| Reset, rings, enumeration, the keyboard | components/src/usb_keyboard_driver.rs |
EL0 |
| Descriptors, setup packets, boot reports | partsfoundry/universal_serial_bus |
host-tested, Kani |
Register window, handoff, TRBs, rings, contexts, PORTSC |
partsfoundry/extensible_host_controller_interface |
host-tested, Kani |
| Where the line discipline's endpoint comes from | crates/system_initializer, step 3b |
the progenitor |
The kernel starts the driver before the progenitor and waits for its one bring-up report, which it
prints as the boot's usb : line. The progenitor is granted the driver's attach endpoint at
slot 27 and sends WRITE on the terminal endpoint through it once the line discipline exists.
What the driver does, in order
The driver halts and resets the controller, lays out the slot array, command ring and event ring in
its region, and sets it running. Then it takes each connected root port in turn. It resets the port,
enables a slot, addresses the device, and reads its device and configuration descriptors.
partsfoundry/universal_serial_bus walks those, because they are what the device chose to send. A boot keyboard is then
configured, asked for the boot protocol, and given reads queued on its interrupt endpoint.
Next the driver sends its report to the kernel, takes the terminal endpoint, and serves. Each report
is diffed against the last, the differences go through video_terminal::keymap, and the bytes go to
the line discipline. A keyboard that is unplugged gives its slot back, and one plugged in later is
found.
Until bring-up ends, every wait is a bounded poll of the event ring, and the interrupt is used only
afterwards. Each wait takes the bound extensible_host_controller_interface names for it: a halt
32 ms, a controller reset 10 s, a command 5 s, a port reset 800 ms, and 20 ms after powering a port.
The crate names none for two of them, so the driver names its own: the run handshake takes the halt
bound, as Linux's xhci_start does, and a control transfer 5 s, USB 2.0's limit for a standard
request. Those come from the specification and Linux rather than from a measurement on xenon. A
wait that reaches its bound fails the step it was in. While a command is waited for, port change
events are remembered and every other event is dropped, so a key pressed during a stall recovery is
lost.
The driver places every TRB with ProducerRing::try_enqueue and reports each completion through
complete, so when a controller stops consuming, its ring fills and the driver refuses the next TRB
rather than overwriting an entry the controller has not reached. The driver reports a full command
or control ring as the command or transfer timeout it follows from.
The confinement, in one sentence
The driver holds every register page of its controller except the page its interrupt message is
programmed through, and its controller can reach nothing but its own DMA region. VT-d also maps any
region the firmware reserved for it (an RMRR). The first half is
register_window (a Kani harness proves no withheld page is ever mapped). The second is the IOMMU.
The kernel refuses to start the driver at all on a machine whose IOMMU does not confine the
controller, because a process holding an xHCI register file names every physical address the
controller reads.
The kernel keeps less of an xHCI than of an NVMe controller, on purpose. NVMe kept its admin plane at EL1 (§86 (whether an NVMe driver can leave the kernel), option 2a), because creating a queue is where a ring's physical address is named. An xHCI names physical addresses everywhere: the slot array, the command ring, the event ring, every transfer ring and every input context. Its enumeration means reading what a device chose to send, the last thing to do at EL1. So the kernel keeps only what is not the driver's: the USB Legacy Support handshake that stops a PC's BIOS answering SMIs for the keyboard, the register window, and the DMA confinement. NVMe runs unconfined on a machine with no IOMMU, and this driver does not run at all, because it names every ring.
What the driver holds
kernel/src/user/usb_keyboard_service.rs is what the driver is handed, and its Spawn literal is
the whole of the confinement claim.
- Slot 0, the controller's interrupt (
Interrupt, WAIT and ACKNOWLEDGE). - Slot 1, an attach endpoint (READ). The one message it receives there carries the endpoint its
keystrokes go to, delegated by the progenitor once the line discipline exists. So a driver the
kernel starts before the progenitor holds
WRITEon an endpoint the progenitor creates, and the kernel never names that endpoint. - Slot 2, a report endpoint (WRITE), for the one bring-up report the kernel waits for.
- Mapped, device-typed: the register pages
register_windowchose, every page of BAR0 but the MSI-X table's and the pending-bit array's. - Mapped, normal memory: the work pages of its DMA region.
What it is denied is each a decision. The MSI-X pages are withheld, because whoever writes the table aims the interrupt message, and with interrupt remapping off that is any vector on any CPU. The scratchpad pages are the controller's private memory; the driver writes their addresses and never their contents. It cannot reach PCI configuration space, so it cannot change bus mastering, move a BAR or touch MSI. It holds no budget, filesystem, console or clock.
The kernel enables the interrupt through the arch layer, as the Interrupt capability's ACKNOWLEDGE does.
On
riscv64 that is the source's stable round-robin hart (target_context). A source enabled on the
boot hart's context instead is moved by its first ACKNOWLEDGE, and no keystroke after the first arrives.
Proving it
script/swish-check boots each architecture a fourth time with qemu-xhci and usb-kbd attached
and nothing else that could type, presses echo hello through the QEMU monitor, and requires
hello back. The keyboard is full speed (usb_version=1), as real keyboards are. On x86_64 the
controller has MSI and no MSI-X (msix=off,msi=on), as xenon's Intel PCH controller does.
To boot one by hand:
NIFE_USB_KEYBOARD=1 NIFE_SCREEN_MON=/tmp/mon.sock helpers/qemu-bounded.sh 120 \
helpers/qemu-runner-aarch64.sh target/aarch64-unknown-none-softfloat/debug/kernel
# in another terminal, once the prompt is up:
echo 'sendkey e' | nc -U /tmp/mon.sock
NIFE_USB_KEYBOARD_OPTS and NIFE_USB_CONTROLLER_OPTS are appended to the two -device options.
The keystroke stall: the PLIC enable is written first
plic::enable, which is the driver's ACKNOWLEDGE when no claim is outstanding, writes the enable bit before
the priority. QEMU's PLIC re-evaluates delivery on a priority, threshold or completion write and on
a rising line, never on an enable write. Written the other way round, an xHCI event raised while the
driver is draining is stranded until another device's line rises, a UART byte for instance. On
riscv64 that stalls the echo of echo hello mid-line about one boot in thirty, until a typed byte
releases the held keys.
It looks like a lost wakeup of line_editor across harts, and it is not one. A thread dump at the
stall shows nobody sending to line_editor; the driver sits in Interrupt::WAIT, and its PLIC
source is
pending, enabled, above threshold and undelivered. kernel/src/drivers/plic.rs has the reasoning,
and sched::tests::an_interrupt_raised_while_its_line_is_masked_is_delivered_at_the_ack holds it on
all three architectures.
Measured on patagonia with the swish-check keystrokes alone (riscv64, TCG, four harts): 10 stalls in 300 boots with the priority written first, 0 in 300 with the enable first. aarch64: 0 in 150. x86_64 was not counted.
The ports settle before the scan
The driver's controller reset disconnects every device, and a root port reports its device again
only once it has re-detected it. QEMU does that the instant the controller runs; xenon's Intel
controller takes about ten milliseconds. So the boot-time scan waits until no port's connection has
changed for 100 ms (USB 2.0's TATTDB), reading every 25 ms and giving up after 2 s. Those are
Linux's hub debounce numbers, and no machine's timing is in them
(extensible_host_controller_interface::port::PortDebounce). A scan that gave up says so in the boot
line instead of printing a count. The scan only names the keyboard early: a device that shows up
later arrives as a connect change, and its port is debounced the same way before the driver
resets it.
To see what the ports read on a machine, build with --features xhci_port_probe
(cargo xtask uefi-image --features xhci_port_probe on x86_64). The kernel prints every root
port's PORTSC as the firmware left it and over the five seconds after the driver's scan, as
xhci-probe: lines.
Per architecture
| aarch64 | riscv64 | x86_64 | |
|---|---|---|---|
| Under QEMU | INTx, SMMUv3; green | INTx, riscv-iommu; green | MSI (and MSI-X), VT-d; green |
| Real silicon | argon: not tried | radon: refused, no IOMMU | xenon: a low-speed keyboard found at boot and typing; MSI, VT-d |
The rings and the cycle bit
partsfoundry/extensible_host_controller_interface is the xHCI arithmetic with nothing it touches, the
same split the NVMe driver has. This is the protocol it models (xHCI 1.2, chapters 4 and 6).
The xHCI is the first controller in the tree whose data structures the device walks on its own. The driver lays them out in memory the controller reads by DMA: a device context base address array (one pointer per device slot), a command ring, an event ring with its segment table, and one transfer ring per endpoint. Every ring is an array of 16-byte TRBs (transfer request blocks), each with a cycle bit. The producer writes TRBs carrying its current cycle. The consumer accepts a TRB only while the bit matches its own, and both flip at the end of the ring. A producer ring ends in a link TRB back to its start. The driver rings a doorbell to say a ring has new work, and the controller writes events to the event ring and raises an interrupt.
The register window
register_window makes the confinement checkable. Every page of the BAR, up to 16, is mapped
except a page that overlaps the MSI-X table or pending-bit array. So the rule a reader checks is
one sentence: the driver holds every register page of its controller except the page its
interrupt message is programmed through.
The interrupt page is withheld because no IOMMU guards it. Interrupt remapping is off on every x86 boot here (§86 (whether an NVMe driver can leave the kernel), its interrupt finding). A process that could write an MSI-X entry could make the device deliver any vector it liked. An interrupt message is not a DMA the IOMMU translates, so nothing would stop it.
The pages the driver needs are then checked against the mask: the capability and operational registers through the last port's status register, interrupter 0's registers, and the doorbells for every slot. A needed page that is withheld or out of reach is a refusal, because a driver started without it would fault on its first access.
Untrusted descriptors
partsfoundry/universal_serial_bus is the part of the driver that reads untrusted input. Every descriptor and every
report is whatever the device chose to send, and a device is whatever a stranger plugged in. So its
parsers are total: they bound every walk, check every length before reading through it, and answer
a Refusal that names what was wrong rather than panicking or guessing. Its Kani harnesses prove
that for every input up to a bound, not only for the inputs a test happened to try.
BUGS
- Root ports only: a keyboard behind a hub is not found, and neither is a keyboard with a hub
built into it.
partsfoundry/extensible_host_controller_interface's BUGS. - One controller, one keyboard. The first xHCI on the bus, the first boot keyboard on it.
- No key repeat, no Caps Lock, no LEDs.
partsfoundry/universal_serial_bus's BUGS. - No IOMMU, no USB keyboard. radon has none; a machine whose VT-d tables give the controller to a unit this kernel did not bring up gets the refusal line. This is the design, and it is a cost.
- A device refused after boot is refused silently: the report endpoint carries one message.
- The attach handshake is a blocking send. A driver that died between its report and its first
receive on the attach endpoint would block the progenitor's
SEND_CAPABILITY, and the boot with it. The driver receives there right after reporting, so the window is one system call wide. - The driver is mapped the USB Legacy Support capability (
USBLEGSUP,USBLEGCTLSTS) whenever it sits on a page nothing else withholds. A compromised driver can then hand the controller back to the firmware's SMI handler. The kernel never needs the driver to touch it. Filed as milestone 960 (the xHCI driver is not mapped the USB Legacy Support capability),design/roadmap/0960-the-xhci-driver-is-not-mapped-the-legacy-support-capability.md. - Silicon is one machine, xenon. Every timeout but the port settle is the specification's limit, not a measurement.