Skip to content

virt_support_apic: add local APIC behavior unit tests - #5246

Merged
Sunil Muthuswamy (sunilmut) merged 2 commits into
microsoft:mainfrom
sunilmut:user/sunilmut/virt-support-apic-unit-tests
Nov 26, 2026
Merged

Sunil Muthuswamy (sunilmut) merged 2 commits into
microsoft:mainfrom
sunilmut:user/sunilmut/virt-support-apic-unit-tests

Conversation

@sunilmut

Copy link
Copy Markdown
Member

virt_support_apic emulates the local APIC for OpenHCL on SNP/TDX and for
OpenVMM's WHP software APIC, but most of its guest-facing behavior had no unit
tests. This PR adds 20 tests in lib.rs that drive the APIC the way a guest and
VMM do: register access over MMIO and MSRs, MSIs, LINT pulses, and the
delivery, offload, save, and restore APIs. The test client now records wakes,
IO-APIC EOIs, and APIC base updates, and has a settable clock for timer tests.

Coverage areas

  • Interrupt priority, EOI, and level-triggered EOI forwarding to the IO-APIC.
  • Software disable, LVT masking, and reserved vectors.
  • xAPIC and x2APIC IPI routing: physical, flat and cluster logical, broadcast,
    shorthands, lowest priority.
  • INIT, SIPI, NMI, EXTINT, and LINT delivery.
  • APIC base MSR transitions and MMIO/x2APIC MSR access rules.
  • Timer one-shot, periodic, divide, and masking.
  • Save/restore round trip.
  • Hyper-V APIC MSRs.
  • APIC offload, lazy EOI, the local fast path, and scan().

Two tests are marked %23[ignore] for bugs found while writing them:

  • dfr_write_with_unknown_model_does_not_panic: a reserved DFR model panics.
    Fixed separately in "virt_support_apic: ignore DFR writes with a reserved
    model"; this test can be dropped once that merges.
  • tpr_write_ignores_reserved_bits: TPR writes pass bits above 7 to CR8.

Adds pal_async as a dev-dependency for the async scan() test.

Coverage of production lines goes from 40% to 98%. Tests only; no production
code changes.

Add behavior tests for interrupt delivery and EOI, IPI routing, APIC base
and register access, the timer, save/restore, Hyper-V MSRs, offload, lazy
EOI, and scan. Two tests are ignored for known bugs (reserved DFR model
panic, unmasked TPR writes).

These unit tests raise virt_support_apic production-code coverage from
40.3% (467 of 1,160 lines) to 98.0% (1,137 of 1,160).
@sunilmut
Sunil Muthuswamy (sunilmut) requested a review from a team as a code owner November 23, 2026 17:16
Copilot AI balanced review requested due to automatic review settings November 23, 2026 17:16
@github-actions github-actions Bot added the unsafe Related to unsafe code label Nov 23, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Unsafe Code Detected

This PR modifies files containing unsafe Rust code. Extra scrutiny is required during review.

For more on why we check whole files, instead of just diffs, check out the Rustonomicon

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The x2APIC TPR test incorrectly requires writes with nonzero reserved bits to succeed instead of raising #GP and leaving CR8 unchanged.

1 open issue
What changed in this PR

Adds behavior-focused unit tests for the local APIC emulator used by OpenHCL and OpenVMM, without changing production code.

Changes:

  • Adds 20 tests covering interrupt delivery, register access, timers, save/restore, and offload behavior; two remain ignored for known bugs.
  • Extends the test client to record callbacks, control time, and simulate offloaded interrupt state.
  • Adds pal_async as a development dependency for the asynchronous scan() test.
File Description
vmm_core/​virt_support_apic/​src/​lib.rs Extends test helpers and adds local APIC behavior tests.
vmm_core/​virt_support_apic/​Cargo.toml Adds the pal_async development dependency.
Cargo.lock Records the new dependency for virt_support_apic.

Review effort: Balanced

Comment thread vmm_core/virt_support_apic/src/lib.rs Resolved
Keep the xAPIC MMIO masking expectation, but require an InvalidAccess
error and unchanged CR8 for an x2APIC TPR write with reserved bits set.
Rename the ignored test and document both existing product bugs.
Copilot AI balanced review requested due to automatic review settings November 25, 2026 01:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The save–restore–save test can miss serialization regressions because it does not independently verify restored LDR, DFR, or timer values against their programmed values.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Low severity Verify restored LDR, DFR, and timer register values

vmm_core/​virt_support_apic/​src/​lib.rs:3110

The save–restore–save equality can miss serialization regressions: if save() starts writing zero for LDR instead of its programmed value, both saves still match after restore() loads that zero. Only LVT_LINT0 is independently checked here, so losing the programmed LDR, DFR, or timer configuration would pass. Please also read those restored registers through the guest interface and compare them with the values programmed above.

1 resolved since last review

Review effort: Balanced

@sunilmut
Sunil Muthuswamy (sunilmut) merged commit 4661306 into microsoft:main Nov 26, 2026
108 of 109 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

unsafe Related to unsafe code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants