Skip to content

Commit 359169f

Browse files
ppennaCopilot
andcommitted
virt: say what an STI shadow restored as a MOV SS shadow loses
The comments and the Guide of the previous commits said that an STI shadow that restores as a MOV SS shadow only delays an NMI or a debug exception by an instruction, and that a restore that is given no kind sets a MOV SS shadow (microsoft/nvx#1369). Both overstate it. A MOV SS shadow makes an Intel CPU skip an instruction breakpoint at the next instruction, as KVM models in kvm_is_code_breakpoint_inhibited, so a build that predates the kind, which restores the STI shadow of a later snapshot as a MOV SS shadow, holds an NMI off for one more instruction and loses that breakpoint, where it would not delay it. And a restore sets a MOV SS shadow only for a state that has a shadow: a state that has none gets none. The doc comments of InterruptShadowKind, of Activity::interrupt_shadow_kind, and of shadow::to_kvm, the comments of the two contract tests of KVM, and the Guide's snapshot format and snapshots pages now say so. No code, saved field, or version changes, and the choice stands: where a state holds a shadow and no kind, the MOV SS shadow is still the one to set, since the reverse would let an NMI or a debug exception split the MOV SS from the instruction that follows it, which sets the stack pointer. cargo xtask fmt passes, as does cargo doc for virt and virt_kvm with warnings denied; the change touches comments and Markdown only. Refs microsoft/nvx#1369 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e95dce8 commit 359169f

6 files changed

Lines changed: 56 additions & 46 deletions

File tree

‎Guide/src/dev_guide/snapshot_format.md‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,20 +369,23 @@ KVM on an Intel CPU can hold an interrupt shadow of either kind, which the
369369
Boolean `interrupt_shadow` cannot say: the shadow that a `MOV SS` or `POP SS`
370370
leaves also blocks NMIs and suppresses certain debug exceptions at the next
371371
instruction boundary, and the one that an `STI` leaves, with `IF` clear, does
372-
not. A restore that is given no kind sets a `MOV SS` shadow, which errs on the
373-
safe side, so the activity records the kind in its optional field 8,
374-
`interrupt_shadow_kind`, when the shadow is an `STI` shadow:
372+
not. A restore of a shadow that is given no kind sets a `MOV SS` shadow, which
373+
errs on the safe side, and a restore of no shadow sets none, so the activity
374+
records the kind in its optional field 8, `interrupt_shadow_kind`, when the
375+
shadow is an `STI` shadow:
375376

376377
- A saved state from a build that predates the field has none, and nor has a
377378
state whose shadow is a `MOV SS` shadow, or that has no shadow, so restoring
378-
it sets a `MOV SS` shadow, as before, and a save encodes every such state byte
379-
for byte as an earlier build did. Only KVM on an Intel CPU saves the field,
380-
for an `STI` shadow. An AMD CPU has one kind of shadow, and the interrupt
381-
state register of MSHV and WHP has one bit for both, so they save none.
379+
it sets a `MOV SS` shadow if it has a shadow, and none if it has none, as
380+
before, and a save encodes every such state byte for byte as an earlier build
381+
did. Only KVM on an Intel CPU saves the field, for an `STI` shadow. An AMD CPU
382+
has one kind of shadow, and the interrupt state register of MSHV and WHP has
383+
one bit for both, so they save none.
382384
- A build that predates the field ignores it in a saved state from a later
383385
build, so it restores an `STI` shadow as a `MOV SS` shadow, as before: an NMI
384-
or a debug exception that the `STI` shadow would let through waits for one
385-
more instruction.
386+
that the `STI` shadow would let through waits for one more instruction, and an
387+
instruction breakpoint at that instruction, which a `MOV SS` shadow suppresses
388+
on an Intel CPU and an `STI` shadow does not, is lost.
386389

387390
A restore never drops a kind that it reads, but into AMD's one shadow. A backend
388391
that cannot hold the kind, MSHV, WHP, or OpenHCL's, refuses the activity, and

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,12 @@ A KVM VP on an Intel CPU can hold an interrupt shadow of either kind. A `MOV SS`
276276
or `POP SS` leaves a shadow that also blocks NMIs and suppresses certain debug
277277
exceptions at the next instruction boundary, and an `STI` that finds `IF` clear
278278
leaves one that does not. A capture records the kind of an `STI` shadow only,
279-
and a restore that is given no kind sets a `MOV SS` shadow, as it always did, so
280-
a snapshot that an earlier OpenVMM captured restores as it did. An earlier
281-
OpenVMM ignores the kind in a later snapshot and sets a `MOV SS` shadow, which
282-
holds an NMI or a debug exception off for one more instruction. The snapshot has
283-
no version for this; see [Fields of an
279+
and a restore of a shadow that is given no kind sets a `MOV SS` shadow, as it
280+
always did, so a snapshot that an earlier OpenVMM captured restores as it did.
281+
An earlier OpenVMM ignores the kind in a later snapshot and sets a `MOV SS`
282+
shadow, which holds an NMI off for one more instruction and loses an
283+
instruction breakpoint at that instruction. The snapshot has no version for
284+
this; see [Fields of an
284285
element](../../dev_guide/snapshot_format.md#fields-of-an-element). KVM on an AMD
285286
CPU, whose one kind of shadow has no kind to record, and MSHV and WHP, whose
286287
interrupt state has one bit for both, record none. A backend that cannot hold

‎vmm_core/virt/src/x86/vp.rs‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -476,29 +476,31 @@ pub struct Activity {
476476
/// change or reuse the field's number.
477477
#[mesh(7)]
478478
pub nmi_pending_count: Option<u8>,
479-
/// The kind of the interrupt shadow, when it is not the kind that restoring
480-
/// `None` sets.
479+
/// The kind of the interrupt shadow, when it is not the kind that a restore
480+
/// sets for a shadow with no kind.
481481
///
482482
/// An interrupt shadow lasts until the next instruction ends. The one that
483483
/// a MOV SS or POP SS leaves also blocks NMIs and suppresses certain debug
484484
/// exceptions at that boundary, and the one that an STI leaves does not,
485485
/// as [`InterruptShadowKind`] says. Where a backend has a shadow of each
486-
/// kind, restoring `None` sets a MOV SS shadow, so its save reports `None`
487-
/// for that one and `Some(InterruptShadowKind::Sti)` for the other, and a
488-
/// state reads back from its restore as it was saved. KVM on an Intel CPU
489-
/// reports the kind. A backend whose shadow has one kind reports `None` for
490-
/// every shadow: AMD hardware, and Hyper-V's interrupt state register,
491-
/// which MSHV and WHP use, which has one bit for both. MSHV, WHP, and
492-
/// OpenHCL's backends refuse to restore a kind, rather than drop it, and
493-
/// KVM on an AMD CPU sets its one shadow, which reads back with no kind. A
494-
/// kind without `interrupt_shadow` describes nothing, and KVM refuses it.
486+
/// kind, a restore sets a MOV SS shadow for a shadow with no kind, so its
487+
/// save reports `None` for that one and `Some(InterruptShadowKind::Sti)`
488+
/// for the other, and a state reads back from its restore as it was saved.
489+
/// KVM on an Intel CPU reports the kind. A backend whose shadow has one
490+
/// kind reports `None` for every shadow: AMD hardware, and Hyper-V's
491+
/// interrupt state register, which MSHV and WHP use, which has one bit for
492+
/// both. MSHV, WHP, and OpenHCL's backends refuse to restore a kind, rather
493+
/// than drop it, and KVM on an AMD CPU sets its one shadow, which reads
494+
/// back with no kind. A kind without `interrupt_shadow` describes nothing,
495+
/// and KVM refuses it.
495496
///
496497
/// The field needs no version. A saved state from a build that predates
497498
/// it, or whose shadow is not an STI shadow, has no such field, which is
498-
/// byte for byte what earlier builds saved, so restoring it sets the
499-
/// shadow that `None` gives, as before. A build that predates the field
500-
/// ignores it in a later state, so it restores an STI shadow as a MOV SS
501-
/// shadow, as before. Never change or reuse the field's number.
499+
/// byte for byte what earlier builds saved, so restoring it sets a MOV SS
500+
/// shadow if it has a shadow, and none if it has none, as before. A build
501+
/// that predates the field ignores it in a later state, so it restores an
502+
/// STI shadow as a MOV SS shadow, as before. Never change or reuse the
503+
/// field's number.
502504
#[mesh(8)]
503505
pub interrupt_shadow_kind: Option<InterruptShadowKind>,
504506
}
@@ -510,11 +512,13 @@ pub struct Activity {
510512
/// certain debug exceptions there, and the one that an STI leaves does not. A
511513
/// restored guest differs from the saved one at that boundary if the restore
512514
/// sets one kind as the other. The MOV SS shadow is the safe one to set: an STI
513-
/// shadow that restores as a MOV SS shadow only delays an NMI or a debug
514-
/// exception by an instruction, while the reverse lets one split a MOV SS from
515-
/// the instruction that follows it, which sets the stack pointer. So
516-
/// [`Activity::interrupt_shadow_kind`] names only the kinds that restoring
517-
/// `None` does not set, and no variant stands for the MOV SS shadow.
515+
/// shadow that restores as a MOV SS shadow holds an NMI off for one more
516+
/// instruction, and an Intel CPU skips an instruction breakpoint at that
517+
/// instruction, which an STI shadow does not, while the reverse lets an NMI or
518+
/// a debug exception split a MOV SS from the instruction that follows it, which
519+
/// sets the stack pointer. So [`Activity::interrupt_shadow_kind`] names only the
520+
/// kinds that restoring a shadow with no kind does not set, and no variant
521+
/// stands for the MOV SS shadow.
518522
#[derive(Copy, Clone, Debug, PartialEq, Eq, Protobuf, Inspect)]
519523
#[mesh(package = "virt.x86")]
520524
pub enum InterruptShadowKind {

‎vmm_core/virt_kvm/src/arch/x86_64/shadow.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ pub(super) fn from_kvm(shadow: u8) -> (bool, Option<InterruptShadowKind>) {
4444
/// before the saved state had a kind. The other way would be unsafe: a MOV SS
4545
/// shadow that restored as an STI shadow would let an NMI or a debug exception
4646
/// split the `MOV SS` from the instruction that follows it, which sets the
47-
/// stack pointer, while an STI shadow that restores as a MOV SS shadow only
48-
/// delays them by an instruction.
47+
/// stack pointer, while an STI shadow that restores as a MOV SS shadow holds an
48+
/// NMI off for one more instruction, and makes an Intel guest CPU skip an
49+
/// instruction breakpoint at that instruction, which KVM models in
50+
/// `kvm_is_code_breakpoint_inhibited`.
4951
///
5052
/// SVM sets its one shadow for any value but 0, and KVM reports it as both
5153
/// kinds, so a restore of the STI kind there reads back with no kind. An SVM

‎vmm_tests/vmm_tests/tests/virt_backend_tests/activity.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,12 @@ fn restores_a_queued_exception_as_injected(
466466
// holds an interrupt shadow, which blocks maskable interrupts for one
467467
// instruction. A MOV SS shadow also blocks NMIs and suppresses certain debug
468468
// exceptions there, as an STI shadow does not. A save names a kind only for an
469-
// STI shadow, because a restore that is given none sets a MOV SS shadow, as
470-
// every restore did. So a state with a shadow and no kind, such as every state
471-
// that an earlier save holds, reads back as saved on every host, and an STI
472-
// shadow does where KVM tells it. KVM on VMX reports each kind. SVM has one
473-
// kind of shadow, which KVM reports as both, so a save holds no kind for it,
474-
// and it restores whatever shadow it is given.
469+
// STI shadow, because a restore of a shadow that is given none sets a MOV SS
470+
// shadow, as every restore did. So a state with a shadow and no kind, such as
471+
// every state that an earlier save holds, reads back as saved on every host,
472+
// and an STI shadow does where KVM tells it. KVM on VMX reports each kind. SVM
473+
// has one kind of shadow, which KVM reports as both, so a save holds no kind
474+
// for it, and it restores whatever shadow it is given.
475475
backend_test!(restores_the_kind_of_an_interrupt_shadow, kvm_only, requires: []);
476476
fn restores_the_kind_of_an_interrupt_shadow(
477477
partition: &impl Partition,

‎vmm_tests/vmm_tests/tests/virt_backend_tests/restore.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ fn restore_accepts_the_state_that_kvm_normalizes(
154154
}
155155

156156
// A save names the kind of an interrupt shadow only for an STI shadow, and a
157-
// restore that is given no kind sets a MOV SS shadow, so the shadow of an
158-
// earlier save, which has no kind, and an STI shadow read back as they were
159-
// saved, and the restore's check accepts them as they are. An SVM CPU has one
160-
// kind of shadow, which a save holds no kind for.
157+
// restore of a shadow that is given no kind sets a MOV SS shadow, so the shadow
158+
// of an earlier save, which has no kind, and an STI shadow read back as they
159+
// were saved, and the restore's check accepts them as they are. An SVM CPU has
160+
// one kind of shadow, which a save holds no kind for.
161161
backend_test!(restore_reads_back_an_interrupt_shadow_as_saved, kvm_only, requires: []);
162162
fn restore_reads_back_an_interrupt_shadow_as_saved(
163163
partition: &impl Partition,

0 commit comments

Comments
 (0)