Description
Summary
Timer::evaluate steps a periodic counter through elapsed time one period at a time, about 2 loop iterations per period in modes 2 and 3. Only timer 0 arms a VM-time wakeup, so nothing evaluates timers 1 and 2 while the guest leaves the PIT alone. The first PIO access after an idle stretch (any access to ports 0x40–0x43 or 0x61) pays for the whole stretch on the vCPU thread.
Repro
Add the attached pit_idle_evaluate_cost.rs (.txt) to vm/devices/chipset/tests/ and run:
cargo test -p chipset --test pit_idle_evaluate_costThe test enables timer 2's gate (port 0x61) and programs counter 2 with count 2, first in mode 2 and then in mode 3. It advances frozen VM time by 64s and times one read of port 0x61.
Expected
One port access takes microseconds, however long the idle time.
Actual
Debug build:
thread 'access_after_idle_is_cheap' panicked at vm/devices/chipset/tests/pit_idle_evaluate_cost.rs:60:9:
mode 2: one PIO access after 64s idle took 844.280903ms
Release build (counter 2, count 2, mode 2; mode 3 is ~25% slower):
| idle VM time | one PIO access |
|---|---|
| 2s | 5.9ms |
| 4s | 11.7ms |
| 8s | 23.8ms |
| 64s | 204ms |
The cost grows linearly, about 3.2ms per idle second; at that rate an hour of idle time would cost ~11s for a single access. With count 65536 the access takes microseconds. Restore can trigger the same thing, because it only checks that the saved last is not after now: a saved state with an old last makes the next access evaluate the whole gap.
Impact
A guest can stall its own vCPU with ordinary programming: counter 2 in a periodic mode with a small count, then idle. A stale or crafted saved state can stall the VMM that restores it.
Suggested fix
In modes 2 and 3 the counter state repeats every period, and only OUT differs between reloads. Once the counter reloads with the same OUT as at its first reload in this call, a full period has passed, so the remaining ticks can be reduced with ticks %= period. The skipped periods only repeat OUT transitions that already happened in this call, and nothing runs between them. The interrupt line therefore ends in the same state, and the interrupt controller latches the same request.
The new unit test test_periodic_fast_forward checks that the result matches one-tick-at-a-time evaluation. It covers modes 2 and 3, binary and BCD, and 11 counts (including 0, 1, 2 and odd counts), at every tick count up to ~4 periods.
Activity
ppenna commentedon Nov 13, 2026
Already fixed on
dev. microsoft/openvmm#4586, merged upstream on 2026-10-28 as023329997, makes the PIT skip whole periods, anddevhas pinned it since #1075, merged on 2026-11-01 as4f9a3214f, which pinned microVM v2.4 at998b9f240. Before that,dev's pins carried the microVM chain's earlier versions of the same change, so this issue reproduces only on upstream's PIT before #4586, which itsfix.patchapplies to. The fix still holds atdev8e9e9eb74, which pins OpenVMM6f57a6d61, with the PIT fixes for #1063, #1065, and #1066 on top.What fixed it.
Timer::evaluateskip whole periods in modes 2 and 3. When an active rate generator or square wave has just reloaded (ce == crwith its count loaded, sonullis false), the remaining ticks are reduced modulo the period: the count, 0x10000 or 10000 for a count of 0, and 0x10001 or 10001 for a mode 3 count of 1. Instead of replaying every skipped transition, OUT toggles once and back, so the line ends at the same level and an edge-triggered controller latches the same request. A mode 2 count of 1, which reloads every tick without lowering OUT, stops at once.evaluatecall therefore takes a bounded number of loop iterations, whatever the elapsed time, on every counter. Neither the first PIO access after idle time nor a restoredlastfar in the past costs more than microseconds, and neither doesChangeDeviceState::start, which evaluates the counters after a restore.long_elapsed_interval_fast_forwards_periodic_pit,periodic_fast_forward_matches_incremental_from_each_phase,count_one_rate_generator_does_not_emit_irq0_edges,reprogrammed_periodic_count_loads_before_fast_forward,low_gate_pauses_periodic_fast_forward, andlong_elapsed_interval_coalesces_periodic_irq0_edges.devpin since NVX's first commit has carried a version of this fast-forward. The chain's last one,938b14343(v2.3 position 023), was dropped by the v2.4 rebase of openvmm: pin microVM v2.4, rebased onto upstream main #1075 for upstream's merged version, which also handles counts of 1 and a reprogrammed count that has not loaded yet.Evidence. Each check ran on Windows x86-64 with Rust 1.97.1, in a checkout of the named revision with this issue's
pit_idle_evaluate_costtest, extended with timing tables, added tovm/devices/chipset/tests/:cargo test -p chipset --test pit_idle_evaluate_cost -- --nocapture --test-threads=1, in debug and with--release, andcargo test -p chipset pitfor the unit tests. The PIT does not depend on the host or the hypervisor. Times are for one read of port 0x61 after programming counter 2 with its gate enabled, unless noted, and are medians of 3 to 5 runs; the machine was shared, so the slow times vary by up to 2x between runs.43eb9f848, upstreammainbefore #4586 (PIT blob7df675e98)access_after_idle_is_cheapmode 2: one PIO access after 64s idle took 1.1323916sin debug and114.4013msin release43eb9f848, release43eb9f848, releaselastis 64 s or 1 h old, count 23d7512af6, NVX's pin when this issue was filed, release6f57a6d61, the pin ofdev8e9e9eb746f57a6d616f57a6d61last1 h, 1 day, 365 days, and 100 years old, and with a craftedlastof 0 andlast_frac_nsof 99 at VM time 2^63 - 1 (100 ns units), thenstart()and the first access6f57a6d61cargo test -p chipset pitwith the scratch tests below, debug and releaseScratch tests at
6f57a6d61, beside the existing ones, with the release run's counts:test_periodic_fast_forward, unchanged, passes.TimerStatematches one-tick evaluation and upstream's evaluation before #4586 in one call, and OUT rises and falls the same way, with at most 5 loop iterations per call.u64::MAX - 1, andu64::MAXticks takes at most 7 loop iterations.