Skip to content

[DO NOT REVIEW] chipset: zero-extend wide reads of the RTC's ports - #5072

Merged
Pedro Henrique Penna (ppenna) merged 1 commit into
feature/microvm/devfrom
copilot/nvx-issue-1151-rtc-wide-reads
Nov 13, 2026
Merged

Pedro Henrique Penna (ppenna) merged 1 commit into
feature/microvm/devfrom
copilot/nvx-issue-1151-rtc-wide-reads

Conversation

@ppenna

@ppenna Pedro Henrique Penna (ppenna) commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Makes the emulated CMOS RTC zero-extend a read of its ports 0x70 and 0x71 that is wider than a byte, as the comment in its io_read says it does, for microsoft/nvx#1151. Rtc::io_read wrote only the first byte of the read buffer, so the other bytes kept what the buffer held.

One commit on 6f57a6d61, the head of feature/microvm/dev.

Zero-extend wide reads of the RTC's ports (60e160369)

  • Cause. Rtc::io_read stored the register's byte in data[0] and left data[1..] as the caller passed them, and what they held depends on the backend. On MSHV and WHP, virt_support_x86emu::emulate::emulate_io passes the guest's EAX as the buffer, so a 16- or 32-bit in from port 0x70 or 0x71 kept the guest's upper bytes of AX or EAX, as an 8-bit in does. KVM zeroes its PIO data before it exits to the VMM for an in (emulator_pio_in_out in arch/x86/kvm/x86.c, in Linux 5.15, 6.1, 6.8, and master), which hid the defect there, and the string ins path through x86emu passes a zeroed buffer on every backend.
  • Change. io_read reads the register first, so that a read of another port still fails with InvalidRegister and leaves the buffer as it was, then zeroes the whole buffer and stores the register's byte in its first byte, as the DMA controller's io_read does. A one-byte read returns the same byte as before, and a read of port 0x71 still reads the selected register once.
  • Alternatives. Zeroing the buffer in vmotherboard's dispatch would cover every device, but the chipset devices own the whole buffer: the PIC, the PIT, and the i8042 reject a wide access with InvalidAccessSize, and the DMA controller and the microVM devices fill the buffer. The patch attached to openvmm: wide RTC port reads leave stale bytes in the upper portion of the result nvx#1151 also adds a test of one-byte reads, which test_default, test_readonly, and test_writeable already cover through get_cmos_data.
  • Tests. wide_reads_are_zero_extended reads both ports with 2- and 4-byte buffers that hold 0xa5 bytes, and requires the register's byte followed by zeros. At 6f57a6d61, a 2-byte read of port 0x70 returns [0x40, 0xa5].

Not in this change, found while auditing the read path:

  • Piix4CmosRtc::io_read in chipset_legacy, which the microVM does not use, also writes only data[0] for its extended ports, 0x72 and 0x73, and their shadows, 0x76 and 0x77. Its other ports, 0x70, 0x71, 0x74, and 0x75, go to Rtc::io_read, which this change fixes.
  • emulate_io returns RAX with bits 63:32 cleared after an 8- or 16-bit in, where the processor keeps bits 63:8 or 63:16, as KVM's kvm_fast_pio_in does. This is read from the code, not reproduced.

Validation

With Rust 1.97.1, on Windows, for chipset, at 60e160369: cargo check, cargo clippy --all-targets (clean apart from the environment's LNK4199 linker notices), cargo doc --no-deps, and cargo nextest run --profile agent (113 passed), then cargo xtask fmt --fix (complete, no changes). At 6f57a6d61, with the new test alone, it fails: "port 0x70, 2 bytes", [64, 165] where [64, 0] is expected.

In NVX's Alpine microVM, with OpenVMM built from 6f57a6d61 (the release v0.1.0-dev.8e9e9eb74cf2), a static guest program selected status D, 0x80, and read each port with an 8-, a 16-, and a 32-bit in, each starting with EAX = 0xdeadbeef, three times per boot:

Backend Host inw 0x70 inl 0x70 inw 0x71 inl 0x71
Expected dead000d 0000000d dead0080 00000080
KVM Azure Linux 3 VM, Linux 6.6.159 dead000d 0000000d dead0080 00000080
MSHV Azure Linux 3 VM, Linux 6.18.34 deadbe0d deadbe0d deadbe80 deadbe80
WHP Windows VM deadbe0d deadbe0d deadbe80 deadbe80

The 8-bit reads returned deadbe0d and deadbe80 on every backend. With OpenVMM built from 60e160369, every 16- and 32-bit read returned the zero-extended value on all three backends: dead000d and 0000000d from port 0x70, and dead0080 and 00000080 from port 0x71.

On Linux, on the Azure Linux 3 KVM test VM, at 60e160369: cargo nextest run --profile agent -p chipset and cargo test -p chipset --lib, 113 passed each. Replayed on upstream main, where it applies without conflicts, chipset type-checks and its 64 tests pass on Windows.

The RTC is OpenVMM's own on every backend. NVX's microVM scenarios, among them one that reads the RTC's status B one byte at a time, pass with this head on KVM, MSHV, and WHP; microsoft/nvx#1563 pins it and records those runs.

OpenVMM CI of this head, run 43352068114, passed every job that NVX requires. Its run vmm-tests [x64-windows-amd-snp] failed in one of its 114 tests, multiarch::vmgs::hyperv_openhcl_uefi_x64_windows_datacenter_core_2025_x64_prepped_snp_vmgstool_copy_igvmfile, whose Hyper-V VM never started because the runner's Hyper-V failed to load the CVM firmware vmfirmwarecvm.dll (0x80070002). The same test failed the same way at 6f57a6d61, in #5067's run 43331611028, so it predates this change.

Rtc::io_read wrote only the first byte of the read buffer, although its
comment says that a read wider than a byte returns the register's byte
zero-extended to the size of the access. The other bytes kept what the
buffer held. On MSHV and WHP, emulate_io gives the device the guest's
EAX as the buffer, so a 16- or 32-bit IN from port 0x70 or 0x71 kept
the guest's upper bytes of AX or EAX, as an 8-bit IN does
(microsoft/nvx#1151). KVM zeroes its PIO data before it exits to the
VMM for an IN, which hid the defect there.

io_read now reads the register first, so that a read of another port
still leaves the buffer as it was, then zeroes the whole buffer and
stores the register's byte in its first byte, as the DMA controller's
io_read does. A one-byte read returns the same byte as before.

Tests: wide_reads_are_zero_extended reads both ports with 2- and 4-byte
buffers that hold 0xa5 bytes, and requires the register's byte followed
by zeros. At 6f57a6d, a 2-byte read of port 0x70 returns
[0x40, 0xa5].

Evidence: in NVX's Alpine microVM, with OpenVMM built from 6f57a6d, a
guest program selected status D, 0x80, and read port 0x70 and 0x71 with
16- and 32-bit INs, each starting with EAX = 0xdeadbeef. On MSHV and
WHP, every read returned 0xdeadbe0d from port 0x70 and 0xdeadbe80 from
port 0x71. On KVM they returned the zero-extended values, 0xdead000d
and 0x0000000d, and 0xdead0080 and 0x00000080, since a 16-bit IN keeps
EAX[31:16]. With cargo test -p chipset on Windows x64, the new test
fails at 6f57a6d and passes with this commit, with the crate's other
112 tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added the release-nvx Targets a branch under feature/microvm/. label Nov 13, 2026
Pedro Henrique Penna (ppenna) added a commit to microsoft/nvx that referenced this pull request Nov 13, 2026
OpenVMM's emulated CMOS RTC stored only the first byte of a read of its
ports, 0x70 and 0x71, although its comment says that a wider read
returns the register's byte zero-extended to the size of the access.
The other bytes kept what the backend's buffer held. On MSHV and WHP,
OpenVMM passes the guest's EAX as that buffer, so a 16- or 32-bit IN
from either port kept the guest's upper bytes of AX or EAX, as an 8-bit
IN does (#1151). KVM zeroes its PIO data before it exits to the VMM for
an IN, which hid the defect there.

Pin 60e160369b1f1f10543e0699d4ae1e95810a1e62, the head of
microsoft/openvmm#5072, one commit on 6f57a6d61, the head of
feature/microvm/dev:

- 60e160369 chipset: zero-extend wide reads of the RTC's ports

Rtc::io_read now reads the register, then zeroes the whole buffer and
stores the register's byte in its first byte. A one-byte read returns
the same byte as before, and a read of another port still fails
without touching the buffer.

Evidence: in NVX's Alpine microVM, a guest program selected status D,
0x80, and read each port with 16- and 32-bit INs that start with EAX =
0xdeadbeef, three times per boot. With OpenVMM 6f57a6d61, from the
release v0.1.0-dev.8e9e9eb74cf2, every read returned 0xdeadbe0d from
port 0x70 and 0xdeadbe80 from port 0x71 on nvx-mshv-test and
nvx-whp-test. On nvx-kvm-test, the reads returned the zero-extended
values, 0xdead000d and 0x0000000d, and 0xdead0080 and 0x00000080,
since a 16-bit IN keeps EAX[31:16]. With OpenVMM built from the pin,
all three hosts returned the zero-extended values. The revision's new
chipset test fails at 6f57a6d61, where a 2-byte read of port 0x70
returns [0x40, 0xa5], and passes at the pin with the crate's other 112
tests, on Windows and on Linux.

Tests: with OpenVMM built from the pin, the microVM scenarios
guest-boot, lifecycle, which reads the RTC's status B one byte at a
time, interrupt-map, snapshot-core, restore-processors, smp-snapshot,
restore-downtime, time-abi-conformance, and time-host-correction pass
with 1 and 2 processors on nvx-kvm-test, nvx-mshv-test, and
nvx-whp-test.

NVX needs no coordinated change: the control-session vectors, the
source excerpts, the contract versions, the activation bound, and the
CPU profiles that nvx-rs copies from OpenVMM match the revision's.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Pedro Henrique Penna (ppenna) added a commit to microsoft/nvx that referenced this pull request Nov 13, 2026
The NVX pin moves to 60e160369, position 267, 136 commits past the
mapped tip. Position 267, which microsoft/openvmm#5072 proposes for
feature/microvm/dev on its head, 6f57a6d61, is unassigned. The Scope
also says that microsoft/openvmm#5067 merged positions 265 and 266.

Position 267 makes the RTC's io_read zero-extend a read of port 0x70
or 0x71 that is wider than a byte, in code that upstream main has, and
adds a test, in a file that PR10's position 020, PR15's position 057,
and position 200 also change, none of them in the code that it
changes. It changes nothing that a mapped PR adds. Replaying it on
upstream main applies without conflicts, and chipset type-checks there
and its 64 tests pass, so it can go upstream on its own.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ppenna
Pedro Henrique Penna (ppenna) merged commit 60e1603 into feature/microvm/dev Nov 13, 2026
73 of 74 checks passed
@github-actions

Copy link
Copy Markdown

Pedro Henrique Penna (ppenna) added a commit to microsoft/nvx that referenced this pull request Nov 13, 2026
The NVX pin moves to 5ca20ee08, position 268, 137 commits past the
mapped tip. Position 268, which microsoft/openvmm#5073 proposes for
feature/microvm/dev on its head, 60e160369, is unassigned. The Scope
also says that microsoft/openvmm#5072 merged position 267.

Position 268 makes VmTimerPeriodic skip at once the deadlines that VM
time passed, and register its next deadline under the timer state's
lock, in code that upstream main has, and adds tests, in a file that
PR10's position 019 and positions 156, 157, and 255 also change, none
of them in the code that it changes. It changes nothing that a mapped
PR adds. Replaying it on upstream main, 7d90c4104, applies without
conflicts, and vmcore type-checks there with all its targets and its
35 tests pass, so it can go upstream on its own.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-nvx Targets a branch under feature/microvm/.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant