Skip to content

Commit e95dce8

Browse files
ppennaCopilot
andcommitted
virt: refuse an interrupt shadow kind that a backend cannot hold
Activity::interrupt_shadow_kind names the STI kind, which only KVM on an Intel CPU holds. MSHV, WHP, and OpenHCL's mshv backend keep the interrupt shadow in the one bit that Hyper-V's interrupt state register has for both kinds. OpenHCL's TDX backend reads both kinds of its VMCS as one Boolean and restores a MOV SS shadow, and its SNP backend does not hold the shadow yet. None of them restores a kind, and a restore that ignored it would give the VP a shadow that differs from the saved one without a word, as a build that predates the field does. The set_activity of MSHV, WHP, and OpenHCL's mshv, SNP, and TDX backends now refuses a kind, before it changes any state, as KVM refuses a kind without a shadow. MSHV and WHP return the UnsupportedState error that they already have for the elements that they cannot hold, and name interrupt_shadow_kind beside nmi_pending_count; OpenHCL's backends return their Unimplemented error. Only a damaged or crafted saved state reaches it: a restore requires the backend that captured the snapshot, and only KVM on an Intel CPU saves a kind. KVM on an AMD CPU sets its one shadow for a kind, which reads back with none, as set_activity's comment says. The Guide's two pages say so. That the VMCS of TDX holds the kind stays a TODO. Test: the backend contract test restores_or_refuses_an_interrupt_shadow_kind restores an activity with an STI shadow and a requested halt into the VP of each backend, and requires that the restore either gives the VP that activity, as KVM does on an Intel CPU, or fails and leaves the activity as it was, the halt included. On an AMD CPU the restore may read the kind back as none. It also requires that a restore of a kind without a shadow fails and leaves the activity as it was. It runs on KVM, MSHV, and WHP, in the new shadow module of virt_backend_tests, which builds on Windows too. OpenHCL's backends are checked by compiling only. On nvx-mshv-test (Linux 6.18.34, MSHV on an Intel Xeon Platinum 8573C) and nvx-whp-test (Windows 10.0.26100, WHP on an Intel Xeon Platinum 8370C), at the previous commit, the test fails on both with "restoring an STI interrupt shadow succeeded, but read back as" an activity without the kind: the restore dropped it. With this commit it passes, as do the 13 contract tests of virt_backend_tests that run on each, the 234 tests of virt and virt_mshv on MSHV, and the 181 of virt and virt_whp on WHP. On nvx-kvm-test it passes with the 29 contract tests that run there, the 150 tests of virt_kvm and kvm, those that need /dev/kvm included, and the 137 of virt. cargo clippy --all-targets for virt, virt_kvm, kvm, virt_mshv, virt_whp, and vmm_tests, which also passes for virt_kvm and vmm_tests on x86_64-unknown-linux-musl and for virt on aarch64-apple-darwin, cargo check and cargo clippy --all-targets for virt_mshv_vtl on OpenHCL's target, x86_64-unknown-linux-musl, cargo doc, and cargo xtask fmt have no warnings. Refs microsoft/nvx#1369 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8c1bc64 commit e95dce8

9 files changed

Lines changed: 118 additions & 7 deletions

File tree

‎Guide/src/dev_guide/snapshot_format.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,13 @@ safe side, so the activity records the kind in its optional field 8,
384384
or a debug exception that the `STI` shadow would let through waits for one
385385
more instruction.
386386

387-
KVM refuses a kind without a shadow, before it changes the activity. Only a
388-
damaged or crafted snapshot holds one.
387+
A restore never drops a kind that it reads, but into AMD's one shadow. A backend
388+
that cannot hold the kind, MSHV, WHP, or OpenHCL's, refuses the activity, and
389+
KVM refuses a kind without a shadow. None changes any state before it refuses.
390+
KVM on an AMD CPU sets its one shadow for the kind, which reads back with none.
391+
Only a damaged or crafted snapshot reaches any of them: a snapshot restores only
392+
on the backend and the CPU generation that captured it, and an AMD capture
393+
records no kind.
389394

390395
## Extending the format
391396

‎Guide/src/user_guide/openvmm/snapshots.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,10 @@ holds an NMI or a debug exception off for one more instruction. The snapshot has
283283
no version for this; see [Fields of an
284284
element](../../dev_guide/snapshot_format.md#fields-of-an-element). KVM on an AMD
285285
CPU, whose one kind of shadow has no kind to record, and MSHV and WHP, whose
286-
interrupt state has one bit for both, record none.
286+
interrupt state has one bit for both, record none. A backend that cannot hold
287+
the kind refuses a snapshot that holds one, which only a damaged or crafted
288+
snapshot of another backend does, since a restore requires the backend that
289+
captured the snapshot.
287290

288291
For a tiered microVM restore, and for a restore that replaces the network
289292
policy of a snapshot without a tier, OpenVMM starts device workers with

‎openhcl/virt_mshv_vtl/src/processor/mshv/x64.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,12 @@ impl AccessVpState for UhVpStateAccess<'_, '_, HypervisorBackedX86> {
15421542
if value.nmi_pending_count.is_some() {
15431543
return Err(vp_state::Error::Unimplemented("nmi_pending_count"));
15441544
}
1545+
// This backend's interrupt state has one bit for both kinds of
1546+
// interrupt shadow, so it cannot restore a kind. Refuse it before
1547+
// changing any state.
1548+
if value.interrupt_shadow_kind.is_some() {
1549+
return Err(vp_state::Error::Unimplemented("interrupt_shadow_kind"));
1550+
}
15451551
self.set_register_state(value)?;
15461552

15471553
// TODO: Set the NMI pending bit via the APIC.

‎openhcl/virt_mshv_vtl/src/processor/snp/mod.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,14 +2779,18 @@ impl AccessVpState for UhVpStateAccess<'_, '_, SnpBacked> {
27792779
pending_event: _, // TODO SNP
27802780
pending_interruption: _, // TODO SNP
27812781
nmi_pending_count,
2782-
// TODO SNP
2783-
interrupt_shadow_kind: _,
2782+
interrupt_shadow_kind,
27842783
} = value;
27852784
// This backend holds at most one pending NMI, so it cannot restore a
27862785
// count of them. Refuse it before changing any state.
27872786
if nmi_pending_count.is_some() {
27882787
return Err(vp_state::Error::Unimplemented("nmi_pending_count"));
27892788
}
2789+
// This backend does not restore an interrupt shadow yet (TODO SNP), so
2790+
// it cannot restore a kind. Refuse it before changing any state.
2791+
if interrupt_shadow_kind.is_some() {
2792+
return Err(vp_state::Error::Unimplemented("interrupt_shadow_kind"));
2793+
}
27902794
let lapic = &mut self.vp.backing.cvm.lapics[self.vtl];
27912795
lapic.activity = mp_state;
27922796
lapic.nmi_pending = nmi_pending;

‎openhcl/virt_mshv_vtl/src/processor/tdx/mod.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3959,14 +3959,18 @@ impl AccessVpState for UhVpStateAccess<'_, '_, TdxBacked> {
39593959
pending_event: _, // TODO TDX
39603960
pending_interruption: _, // TODO TDX
39613961
nmi_pending_count,
3962-
// TODO TDX: the VMCS holds the kind
3963-
interrupt_shadow_kind: _,
3962+
interrupt_shadow_kind,
39643963
} = value;
39653964
// This backend holds at most one pending NMI, so it cannot restore a
39663965
// count of them. Refuse it before changing any state.
39673966
if nmi_pending_count.is_some() {
39683967
return Err(vp_state::Error::Unimplemented("nmi_pending_count"));
39693968
}
3969+
// TODO TDX: the VMCS holds the kind, which this backend does not
3970+
// restore yet. Refuse it before changing any state.
3971+
if interrupt_shadow_kind.is_some() {
3972+
return Err(vp_state::Error::Unimplemented("interrupt_shadow_kind"));
3973+
}
39703974
self.vp.backing.cvm.lapics[self.vtl].activity = mp_state;
39713975
self.vp.backing.cvm.lapics[self.vtl].nmi_pending = nmi_pending;
39723976
let interruptibility = Interruptibility::new()

‎vmm_core/virt_mshv/src/x86_64/vp_state.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ impl AccessVpState for &'_ mut MshvProcessor<'_> {
266266
if value.nmi_pending_count.is_some() {
267267
return Err(ErrorInner::UnsupportedState("nmi_pending_count").into());
268268
}
269+
// MSHV's interrupt state has one bit for both kinds of interrupt
270+
// shadow, so it cannot restore a kind. Refuse it before changing any
271+
// state.
272+
if value.interrupt_shadow_kind.is_some() {
273+
return Err(ErrorInner::UnsupportedState("interrupt_shadow_kind").into());
274+
}
269275
self.set_register_state(value)?;
270276
// The NMI pending bit is not part of the register state; it must
271277
// be set via the APIC page.

‎vmm_core/virt_whp/src/apic.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ impl WhpProcessor<'_> {
217217
if value.nmi_pending_count.is_some() {
218218
return Err(Error::UnsupportedState("nmi_pending_count"));
219219
}
220+
// WHP's interrupt state has one bit for both kinds of interrupt
221+
// shadow, so it cannot restore a kind. Refuse it before changing any
222+
// state.
223+
if value.interrupt_shadow_kind.is_some() {
224+
return Err(Error::UnsupportedState("interrupt_shadow_kind"));
225+
}
220226
match self.vp.partition.vtlp(vtl).lapic {
221227
LocalApicKind::Emulated(_) => {
222228
let lapic = self.state.vtls[vtl].lapic.as_mut().unwrap();

‎vmm_tests/vmm_tests/tests/virt_backend_tests/main.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ mod save;
5050
guest_is_native,
5151
any(target_os = "linux", windows)
5252
))]
53+
mod shadow;
54+
#[cfg(all(
55+
guest_arch = "x86_64",
56+
guest_is_native,
57+
any(target_os = "linux", windows)
58+
))]
5359
mod speculation;
5460
#[cfg(all(
5561
guest_arch = "x86_64",
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
//! Guest-free contract tests of the interrupt shadow that a restore sets.
5+
6+
use super::native::backend_test;
7+
use hvdef::Vtl;
8+
use virt::Partition;
9+
use virt::Processor;
10+
use virt::vp;
11+
use virt::vp::AccessVpState as _;
12+
13+
// A VP that stops right after an STI that ran with IF clear holds an interrupt
14+
// shadow that, unlike the one that a MOV SS leaves, does not block NMIs at the
15+
// next instruction boundary. The activity that a save reads reports it as the
16+
// STI kind, which KVM on an Intel CPU does. A backend whose interrupt state has
17+
// one bit for both kinds, as MSHV's and WHP's have, cannot restore the kind,
18+
// and must refuse it rather than drop it. KVM on an AMD CPU, whose one kind of
19+
// shadow KVM reports as both, sets it and reads it back without a kind. So a
20+
// restore either gives the VP the activity that it is given, or fails and
21+
// changes none of it, but for the kind on such a CPU.
22+
backend_test!(restores_or_refuses_an_interrupt_shadow_kind, requires: []);
23+
fn restores_or_refuses_an_interrupt_shadow_kind(
24+
partition: &impl Partition,
25+
processor: &mut impl Processor,
26+
) -> anyhow::Result<()> {
27+
let one_kind = partition.caps().vendor.is_amd_compatible();
28+
let mut state = processor.access_state(Vtl::Vtl0);
29+
let before = state.activity()?;
30+
31+
// The halt shows a restore that changed the activity before it refused.
32+
let sti = vp::Activity {
33+
mp_state: vp::MpState::Halted,
34+
interrupt_shadow: true,
35+
interrupt_shadow_kind: Some(vp::InterruptShadowKind::Sti),
36+
..Default::default()
37+
};
38+
if state.set_activity(&sti).is_ok() {
39+
let restored = state.activity()?;
40+
let expected = vp::Activity {
41+
interrupt_shadow_kind: (!one_kind).then_some(vp::InterruptShadowKind::Sti),
42+
..sti
43+
};
44+
anyhow::ensure!(
45+
restored == expected,
46+
"restoring an STI interrupt shadow succeeded, but read back as {restored:?}"
47+
);
48+
} else {
49+
anyhow::ensure!(
50+
state.activity()? == before,
51+
"the refused restore of an STI interrupt shadow changed the activity"
52+
);
53+
}
54+
55+
// A kind describes a shadow, so no backend restores one without it, and
56+
// the refusal changes none of the activity either.
57+
let before = state.activity()?;
58+
let kind_only = vp::Activity {
59+
interrupt_shadow: false,
60+
..sti
61+
};
62+
anyhow::ensure!(
63+
state.set_activity(&kind_only).is_err(),
64+
"restoring a kind without an interrupt shadow succeeded"
65+
);
66+
anyhow::ensure!(
67+
state.activity()? == before,
68+
"the refused restore of a kind without an interrupt shadow changed the activity"
69+
);
70+
Ok(())
71+
}

0 commit comments

Comments
 (0)