Skip to content

fix(serial): honest device classification, single-instance binding, bounded TX wait - #33

Open
emilf wants to merge 1 commit into
mainfrom
fix/serial-device-classification
Open

emilf wants to merge 1 commit into
mainfrom
fix/serial-device-classification

Conversation

@emilf

@emilf emilf commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Summary

Three real defects found while chasing "no output on COM1". The output loss itself was a measurement error, not a bug — see Root cause was capture timing below. These are the genuine problems that session uncovered, and none of them required the port to be broken.

Root cause was capture timing (not a bug)

The original symptom was 0 bytes on COM1 with debugcon working. That was wrong: I attached socat to the serial socket after QEMU had booted, so the monitor's one-shot banner had already been written and discarded with no reader attached.

Attaching to the persistent relay before launching QEMU proves the port works:

BdsDxe: loading Boot0002 "UEFI QEMU NVMe Ctrl deadbeef 1" ...
[WARN  drivers::usb::xhci] xHCI 0000:0...

→ 1429 bytes on COM1, 0 faults.

LSR reads 0x60 (THRE set) throughout, so the transmitter was healthy the whole time. This PR deliberately does not claim to have fixed silent COM1 output, because there was nothing to fix there.

Defects actually fixed

1. Serial device paths got a fabricated I/O address

classify_device_path set found_serial if any node in a composite path was a serial node, then stamped the device with a hardcoded 0x3F8 when no address had been found. In practice 26 unrelated handles were classified as serial ports, all claiming the same address.

The UEFI UART node type carries baud/framing only (baud_rate, data_bits, parity, stop_bits) — no I/O base. Correct behaviour is to set the address only when the terminal node is serial, and leave serial-in-the-middle paths address-less instead of inventing one.

The 0x3F8 fallback is retained for the terminal-node case behind an explicit TODO(serial-discovery): it is an assumption (conventional COM1), not a discovery result, and would program the wrong port on hardware with a non-standard UART base.

2. The driver manager could bind the same class repeatedly

SerialDriver keeps process-wide state in SERIAL_STATE, so binding a second DeviceClass::Serial device overwrote the first while leaving it unowned. The manager now binds single-instance classes (currently Serial) at most once, snapshotted before the mutable device loop.

Marked TODO(single-instance-binding): dedupe by physical resource (I/O base, MMIO range, PCI BDF) so genuinely distinct ports can each bind, and define the ownership rule for multi-port hardware.

3. Transmit polling was unbounded inside a lock

SerialPort::write_buffer spun while line_status() & 0x20 == 0 with SERIAL_STATE held. On a port that never reports THRE that wedges the console permanently, silently, and inside a mutex — turning one dead device into a dead subsystem with no error.

The wait is now bounded (wait_transmit_ready), and write_bytes_direct returns Err instead of an unconditional Ok, so callers fall back rather than believing a dropped write succeeded.

Docs

  • AGENTS.md — replaced the one-line relay note with a channel table (serial/monitor/debugcon/QMP, both ends), the attach-before-boot rule, the headless debugcon-only default, per-channel proof guidance, and the "don't poll hardware unbounded under a lock" rule. This is the file read at the start of a dev session, so the debugging model is now stated where it will be seen.
  • docs/hardware-and-drivers.md — documented both open assumptions, the bounded-wait rationale, and a new safety rule.

Verification

  • Build clean on the pinned toolchain (nightly-2026-02-18).
  • Boot with a reader attached to the persistent relay before QEMU starts: 1429 bytes on COM1, 0 page/GP faults, monitor reaches idle loop.
  • No regression in the debugcon path.

Open assumptions

Tracked as a repo issue; the TODO(serial-discovery) and TODO(single-instance-binding) comments point at the same work:

  1. UART base defaults to 0x3F8 rather than being read from ACPI (SPCR / _CRS) or EFI_SERIAL_IO_MODE.
  2. One console UART per system rather than per-resource identity.

…ounded TX wait

Found during a debug session chasing "no output on COM1". The output loss turned
out to be a capture-timing error (attaching to the serial socket after the
monitor had already printed), NOT a broken port: with a reader attached before
boot, COM1 emits the full boot stream. These are the real defects that session
uncovered, none of which required the port to be broken.

1. Serial device paths were given a fabricated I/O address.

classify_device_path set `found_serial` if ANY node in a composite path was a
serial node, then stamped the device with a hardcoded 0x3F8 when no address had
been found. In practice 26 unrelated handles were classified as serial ports,
all claiming the same address. The UEFI UART node type carries baud/framing
only (baud_rate, data_bits, parity, stop_bits) and no I/O base, so the correct
behaviour is to set the address only when the TERMINAL node is serial, and to
leave serial-in-the-middle paths address-less instead of inventing one.

The 0x3F8 fallback is retained for the terminal-node case with an explicit
TODO(serial-discovery): it is an assumption (conventional COM1), not a
discovery result, and would program the wrong port on hardware with a
non-standard UART base. Proper resolution: ACPI SPCR or the serial device's
_CRS I/O resource, or EFI_SERIAL_IO_MODE/MMIO configuration.

2. The driver manager could bind the same class repeatedly.

SerialDriver keeps process-wide state in SERIAL_STATE, so binding a second
DeviceClass::Serial device overwrote the first while leaving it unowned. The
manager now binds single-instance classes (currently Serial) at most once,
snapshotted before the mutable device loop. Marked
TODO(single-instance-binding): dedupe by physical resource (I/O base, MMIO
range, PCI BDF) so genuinely distinct ports can each bind, and define the
ownership rule for multi-port hardware.

3. Transmit polling was unbounded inside a lock.

SerialPort::write_buffer spun `while line_status() & 0x20 == 0` with
SERIAL_STATE held. On a port that never reports THRE that wedges the console
permanently, silently, and inside a mutex. The wait is now bounded
(wait_transmit_ready) and write_bytes_direct returns Err instead of an
unconditional Ok, so callers fall back rather than believing a dropped write
succeeded.

Verification:
- Build clean (pinned nightly-2026-02-18).
- Boot with a reader attached to the persistent relay before QEMU starts:
  1429 bytes on COM1, 0 page/GP faults, monitor reaches idle loop.
- LSR reads 0x60 (THRE set) throughout, confirming the transmitter itself is
  healthy and that early captures were measuring a quiet period.

Docs:
- AGENTS.md: replaced the one-line relay note with a channel table
  (serial/monitor/debugcon/QMP, both ends), the attach-before-boot rule, the
  headless debugcon-only default, and per-channel proof guidance, so a dev
  session starts with the right debugging model.
- hardware-and-drivers.md: documented both open assumptions, the bounded-wait
  rationale, and a "never poll hardware unbounded while holding a lock" rule.

No root-cause fiction: nothing here claims to have fixed silent COM1 output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant