You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PIT evaluate drops sub-100ns time, so the PIT runs fast and same-instant reads differ #1065
PitDevice::evaluate counts whole 838ns ticks since last and advances last by them. VmTime has 100ns resolution, so wrapping_add truncates the advance, and the next evaluation counts the dropped 0–99ns again. Every PIO access to the PIT or port 0x61 is an evaluation, so the PIT runs fast, and faster the more often the guest touches it. Two reads at the same VM time can also return different counts.
Add the attached pit_sub_100ns_leak.rs to vm/devices/chipset/tests/ and run:
cargo test -p chipset --test pit_sub_100ns_leak
Both tests program counter 0 in mode 2 with count 0x50, then advance VM time by 2.5µs (2 ticks + 824ns):
reads_at_the_same_instant_agree latches the count twice at the same VM time.
restored_device_reads_the_same_count saves the device, restores it into a new device, and latches at the same VM time.
Expected
Both reads return the same count, and the restored device reads the same count as the original.
Actual
thread 'reads_at_the_same_instant_agree' panicked at vm/devices/chipset/tests/pit_sub_100ns_leak.rs:62:9:
assertion `left == right` failed: counter moved between two reads at the same VM time
left: 79
right: 78
The first evaluation counts 2 ticks (1676ns) but advances last by only 1600ns. The second evaluation then sees 900ns and counts a third tick. The restore test fails the same way (78 vs. 79).
Root cause
pit/mod.rs:541-545:
let delta = now.checked_sub(self.last).unwrap_or(Duration::ZERO);let ticks = delta.as_nanos()asu64 / NANOS_PER_TICK;self.last = self.last.wrapping_add(Duration::from_nanos(ticks *NANOS_PER_TICK));// truncated to 100ns
The comment above this code explains why last must not be set to now: the timers would run late. Truncating to 100ns makes them run fast instead.
Impact
The PIT runs fast by up to 99ns per evaluation, so the drift depends on how often the guest accesses the PIT. For example, a guest that busy-polls the counter or port 0x61 to calibrate a clock sees a fast PIT.
PIT reads are not deterministic for a given VM time, which affects save/restore and replay.
Suggested fix
Carry the remainder below 100ns in a new field, last_frac_ns, and include it in evaluate and arm_wakeup. The field is saved as a new optional #[mesh(3)] field; without it, a restored device would count the remainder again. Old saved states restore with 0, and restore rejects values >= 100.
Activity
openvmm: pin the PIT's sub-100 ns tick time
chipset: keep the sub-100 ns part of the PIT's last tick time