From 5d656d854a8daca0f23833666b0b71229fe2ab73 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Thu, 12 Nov 2026 09:10:54 -0800 Subject: [PATCH] chipset: start each PIT low/high count with its low byte In the low-then-high access mode (RW = 3), an 8254 counter takes a count as two bytes, the low byte first, through a write flip-flop that a control word for the counter resets to the low byte and that each byte then toggles, so that a guest can write one count after another without a new control word. Timer::write kept the low byte that it had buffered in cr_low once the high byte completed the count, and only set_control cleared it, so after the first count every byte was taken as a high byte with that stale low byte. Control word 0x30, then the counts 0 and 16, left counter 0 counting 65536 rather than 16 (microsoft/nvx#1063), and control word 0x34, then the counts 0x1234 and 0x5678, left the count register at 0x5634 (microsoft/nvx#1146). A guest that reloads a count without a control word, as Linux's PIT one-shot clock event device does for each event after its control word 0x38, so programmed a wrong period or one-shot length, and IRQ0 at the wrong rate. The high byte now takes the buffered low byte, which returns the flip-flop to the low byte of the next count. The low-byte-only and high-byte-only access modes, the reset by a control word, and the saved state, which already holds cr_low, are unchanged. A state that was saved without this change after such a count holds the stale low byte, which a restore keeps, as it cannot tell it from a low byte that the guest wrote: the guest's next counts then go wrong until it writes a control word for the counter, as they did before. Tests: in every mode, two counts written after a first one without a control word each take their low byte first. Through the I/O ports, as the issues reproduce it: counter 0 in mode 0, given the count 0 and then 16, raises IRQ0 on the 17th tick and not on the 16th, and counter 0 in mode 2, given 0x5678 while it counts 0x1234, holds 0x5678 at the end of the current period. Without the fix, the three tests fail with a count register of 0x5634, with IRQ0 still low after 17 ticks of a count of 65536, and with a latched count of 0x5634. The test attached to microsoft/nvx#1063 fails at the parent commit with the latched count 0xff9d of a count of 65536, and passes with the fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vm/devices/chipset/src/pit/mod.rs | 129 +++++++++++++++++++++++++++++- 1 file changed, 128 insertions(+), 1 deletion(-) diff --git a/vm/devices/chipset/src/pit/mod.rs b/vm/devices/chipset/src/pit/mod.rs index 4f1e8bf904d..6f66e9f519e 100644 --- a/vm/devices/chipset/src/pit/mod.rs +++ b/vm/devices/chipset/src/pit/mod.rs @@ -366,7 +366,9 @@ impl Timer { RwMode::LOW => self.state.cr = n, RwMode::HIGH => self.state.cr = n << 8, RwMode::LOW_HIGH => { - if let Some(low) = self.state.cr_low { + // The high byte completes the count and returns the write + // flip-flop to the low byte of the next count. + if let Some(low) = self.state.cr_low.take() { self.state.cr = (n << 8) | (low as u16); } else { self.state.cr_low = Some(n as u8); @@ -949,15 +951,20 @@ mod save_restore { mod tests { use super::ControlWord; use super::Mode; + use super::NANOS_PER_TICK; + use super::PIT_CONTROL_REGISTER; + use super::PIT_TIMER_RANGE_START; use super::RwMode; use super::Timer; use super::to_bcd; use crate::pit::from_bcd; + use chipset_device::pio::PortIoIntercept; use parking_lot::Mutex; use std::sync::Arc; use test_with_tracing::test; use vmcore::line_interrupt::LineInterrupt; use vmcore::line_interrupt::LineSetTarget; + use vmcore::line_interrupt::test_helpers::TestLineInterruptTarget; #[derive(Default)] struct RecordingTarget(Mutex>); @@ -1079,6 +1086,126 @@ mod tests { test_output(true); } + #[test] + fn low_high_access_takes_each_count_low_byte_first() { + // The high byte of a count returns the write flip-flop to the low + // byte, so each count that follows without a control word starts with + // its low byte, whatever the mode. + for mode in [ + Mode::TerminalCount, + Mode::HardwareOneShot, + Mode::RateGenerator, + Mode::SquareWave, + Mode::SoftwareStrobe, + Mode::HardwareStrobe, + ] { + let mut timer = Timer::new(true, None); + set_timer(&mut timer, mode, 0x1234, false); + for count in [0x5678_u16, 0x9abc] { + let [low, high] = count.to_le_bytes(); + timer.write(low); + timer.write(high); + assert_eq!(timer.state.cr, count, "{mode:?}"); + } + } + } + + /// A PIT that a test programs through its I/O ports, as a guest does, in + /// a VM time that stands still until `run_to` moves it. + struct GuestPit { + pit: super::PitDevice, + irq0: Arc, + keeper: vmcore::vmtime::VmTimeKeeper, + pool: pal_async::DefaultPool, + } + + impl GuestPit { + fn new() -> Self { + use vmcore::vmtime::VmTime; + + let mut pool = pal_async::DefaultPool::new(); + let driver = pool.driver(); + let keeper = vmcore::vmtime::VmTimeKeeper::new(&driver, VmTime::from_100ns(0)); + let source = pool.run_until(keeper.builder().build(&driver)).unwrap(); + let irq0 = TestLineInterruptTarget::new_arc(); + let pit = super::PitDevice::new( + LineInterrupt::new_with_target("pit", irq0.clone(), 0), + source.access("pit"), + ); + Self { + pit, + irq0, + keeper, + pool, + } + } + + fn write(&mut self, port: u16, data: u8) { + self.pit.io_write(port, &[data]).unwrap(); + } + + /// Moves VM time to `ticks` PIT ticks after the start, rounded up to + /// VM time's 100 ns. + fn run_to(&mut self, ticks: u64) { + use vmcore::vmtime::SavedState; + use vmcore::vmtime::VmTime; + + let time = VmTime::from_100ns((ticks * NANOS_PER_TICK).div_ceil(100)); + self.pool + .run_until(self.keeper.restore(SavedState::from_vmtime(time))); + } + + /// Latches counter 0, which brings the PIT up to VM time as every + /// port access does, and reads its count, low byte first. + fn latched_count(&mut self) -> u16 { + self.write(PIT_CONTROL_REGISTER, 0x00); + let mut count = [0; 2]; + for byte in &mut count { + self.pit + .io_read(PIT_TIMER_RANGE_START, std::slice::from_mut(byte)) + .unwrap(); + } + u16::from_le_bytes(count) + } + } + + #[test] + fn low_high_access_reloads_a_count_without_a_control_word() { + // Counter 0, low then high byte, mode 0: the count 0 (65536), then, + // without a control word, the count 16, which raises IRQ0 on the 17th + // tick, after the tick that loads it and its 16 counts. + let mut guest = GuestPit::new(); + guest.write(PIT_CONTROL_REGISTER, 0x30); + for byte in [0x00, 0x00, 0x10, 0x00] { + guest.write(PIT_TIMER_RANGE_START, byte); + } + + guest.run_to(16); + let count = guest.latched_count(); + assert!(!guest.irq0.is_high(0), "count {count:#06x}"); + guest.run_to(17); + let count = guest.latched_count(); + assert!(guest.irq0.is_high(0), "count {count:#06x}"); + } + + #[test] + fn low_high_access_reprograms_a_running_rate_generator() { + // Counter 0, low then high byte, mode 2: the count 0x1234, then, while + // it counts and without a control word, the count 0x5678, which the + // counter loads at the end of the current period. + let mut guest = GuestPit::new(); + guest.write(PIT_CONTROL_REGISTER, 0x34); + guest.write(PIT_TIMER_RANGE_START, 0x34); + guest.write(PIT_TIMER_RANGE_START, 0x12); + guest.run_to(1); + assert_eq!(guest.latched_count(), 0x1234); + + guest.write(PIT_TIMER_RANGE_START, 0x78); + guest.write(PIT_TIMER_RANGE_START, 0x56); + guest.run_to(1 + 0x1234); + assert_eq!(guest.latched_count(), 0x5678); + } + #[test] fn elapsed_interval_matches_incremental_pit_evaluation() { for mode in [