crates

extensible_host_controller_interface

check proofs license minimum Rust version

Everything a Universal Serial Bus (USB) host controller (xHCI) driver computes, with nothing it touches. That covers register offsets and field decode, Transfer Request Block (TRB) construction and decode, the producer and event ring cycle discipline, slot and endpoint contexts, port status and the port debounce. The caller performs every volatile access and owns the direct memory access (DMA) memory. This crate supplies the arithmetic, so it runs in no_std, tests on the host and suits a model checker. It does not touch hardware, allocate, or drive a controller on its own.

EXAMPLES

use extensible_host_controller_interface::{Capabilities, ProducerRing, TransferRequestBlock};

// QEMU's qemu-xhci: 0x40 bytes of capability registers, 64 slots, 16 interrupters and 8 ports
// (4 USB 2 and 4 USB 3, the defaults), runtime at 0x1000, doorbells at 0x2000. Port 1's status
// register and the command ring's doorbell:
let caps = Capabilities::decode(0x0100_0040, 0x0800_1040, 0, 0x0000_7000, 0x2000, 0x1000);
assert_eq!((caps.port_status_and_control(1), caps.doorbell(0)), (Some(0x440), 0x2000));

// A four-entry command ring: three usable slots, then a link TRB back to the first.
let mut ring = ProducerRing::new(0x1000, 4);
let a = ring.enqueue(TransferRequestBlock::enable_slot());
assert_eq!((a.index, a.transfer_request_block.is_cycle(), a.link), (0, true, None));

The caller writes each placed TRB (and the link, when link is Some) to DMA memory at ring.entry_physical_address(index), then rings the doorbell.

examples/fake_controller.rs runs a whole control transfer against a controller simulated in memory: the driver places it with try_enqueue, and the fake consumes the transfer ring and writes a Transfer Event. The driver then decodes the event and reports it with complete. Run it with cargo run --example fake_controller.

What is proven

Thirteen Kani harnesses, in src/verification.rs:

Install Kani and run them as "Building and testing" below describes.

What the caller does

The crate computes; the caller performs every access, in this order. On x86_64 a compiler fence is enough for each barrier below. On aarch64 and riscv64, which order memory weakly, each one is a barrier instruction (Linux spells them wmb() and rmb()).

To produce a TRB on a command or transfer ring:

  1. Keep at most ring.capacity() TRBs outstanding: placed, with no completion event yet. ring.try_enqueue counts them, and refuses at capacity, for a caller that reports each completion event's TRB pointer to ring.complete. ring.enqueue never refuses.
  2. Call ring.enqueue, then write dwords 0 to 2 of the placed TRB at ring.entry_physical_address(index) with volatile writes.
  3. Issue a write barrier, then write dword 3, which holds the cycle bit. Until that bit matches, the controller treats the TRB as not yet produced (xHCI 1.2 section 4.9.2).
  4. When link is Some, write the link TRB at its index the same way.
  5. Issue a write barrier, then write the doorbell register.

To consume an event:

  1. Read dword 3 of the entry at event_ring.index() with a volatile read. The controller writes that dword last (xHCI 1.2 section 4.11.3).
  2. When event_ring.is_ready says no, stop. Otherwise issue a read barrier, then read dwords 0 to 2, and decode the four with Event::decode.
  3. Call event_ring.advance(), and after the events of this pass, write event_ring.dequeue_pointer() | registers::EVENT_RING_DEQUEUE_POINTER_BUSY to the interrupter's ERDP register (xHCI 1.2 section 4.9.4). The busy bit clears Event Handler Busy, and without it the interrupter raises no further interrupt (section 4.17.2).

Building and testing

cargo build
cargo test
rustup target add x86_64-unknown-none aarch64-unknown-none-softfloat riscv64imac-unknown-none-elf
cargo build --lib --target x86_64-unknown-none   # also aarch64-unknown-none-softfloat, riscv64imac-unknown-none-elf

Run rustup target add once, before the first bare-metal build. Without it, that build stops with can't find crate for core. The tests run on the host in about a second.

Running the proofs

The proofs need Kani, which rustup does not install. Install it once, with network access, since the setup step downloads a model checker:

cargo install --locked kani-verifier --version 0.68.0
cargo kani setup

CI uses version 0.68.0, and another version may verify differently.

Then cargo kani, from the crate's directory, runs all thirteen harnesses. On an x86_64 Linux desktop that takes about 2 minutes and peaks near 0.8 gigabytes of memory. the_capability_walk_ends_and_stays_inside_the_limit takes about half of that time. CI runs plain cargo kani, with no extra flags. To run one harness, give its full path and --exact:

cargo kani --harness verification::a_producer_ring_stays_in_bounds --exact

A passing run prints several lines that look alarming. All of them are expected:

Trust the last line of the run: Complete - 13 successfully verified harnesses, 0 failures, 13 total.

Running one falsification patch

patch -p1 --no-backup-if-mismatch < falsifications/verification.a_producer_ring_stays_in_bounds.patch
cargo kani --harness verification::a_producer_ring_stays_in_bounds --exact   # ends VERIFICATION:- FAILED
git checkout -- src

The harness name is the part of the patch's file name after verification.. The run ends with Complete - 0 successfully verified harnesses, 1 failures, 1 total., and the failed check names the assertion that the patch breaks. git checkout -- src reverts the patch.

BUGS

The reference is the xHCI 1.2 specification, chapters 4 to 7.

Minimum Rust version

1.85. To check it, install that toolchain and its bare-metal target, then test and build with it:

rustup toolchain install 1.85 --profile minimal --target x86_64-unknown-none
cargo +1.85 test
cargo +1.85 build --lib --target x86_64-unknown-none

License

Licensed under either of the MIT license (LICENSE-MIT) or the Apache License, Version 2.0 (LICENSE-APACHE), at your option.