|
| 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