From e51ab72d376fabfbd84ec8c8689c40e48f9c3cdd Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Thu, 29 Oct 2026 20:31:29 -0700 Subject: [PATCH 1/2] openvmm_core: test each register of a recorded CPUID difference The comparison of a restore's effective CPUID with the one that the snapshot recorded names, for each entry that both have, every register in which they differ, with its value and mask on each side, then the entries that only one side has. Its tests checked a flipped bit of EAX alone, and matched the differences of the topology leaves by prefix. Check the whole message for a change of the value, and of the mask, of each register alone; for several registers of one entry, which are named in order on one line; and for an entry that differs among entries that the snapshot lacks, followed by entries that only the snapshot has, which are named and counted in that order. Follow-up to microsoft/nvx#568, from the review of microsoft/openvmm#4804. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/worker/dispatch/time_abi.rs | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs b/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs index 26b54e11ada..660f9bd16e2 100644 --- a/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs +++ b/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs @@ -1684,6 +1684,127 @@ mod tests { ); } + /// A difference of an entry that both records hold names each register + /// in which the entry differs, in order, with its value and mask in + /// each, and no other register; the entries that only one record holds + /// are named with the partition's, in order, then the snapshot's. + #[test] + fn recorded_cpuid_names_each_register_in_which_an_entry_differs() { + let (_, _, effective) = test_config(2); + let record = test_record(&effective); + let entries: Vec<_> = record_entries(&effective).collect(); + let message = |recorded: &[EffectiveCpuidEntry]| { + let record = SnapshotCpuProfile { + effective_cpuid: encode_effective_cpuid(recorded.iter().copied()), + ..record.clone() + }; + format!( + "{:#}", + check_recorded_cpuid(&effective, &record).unwrap_err() + ) + }; + let expected = |differences: &[String]| { + format!( + "[E_CPU_SURFACE] the effective CPUID of CPU profile {TEST_PROFILE} differs from \ + the snapshot's in {} entries: {}", + differences.len(), + differences.join("; ") + ) + }; + let register = + |register: usize, ours: &EffectiveCpuidEntry, theirs: &EffectiveCpuidEntry| { + format!( + "{} is {:#010x} under mask {:#010x}, and {:#010x} under mask {:#010x} in \ + the snapshot", + REGISTERS[register], + ours.result[register], + ours.mask[register], + theirs.result[register], + theirs.mask[register] + ) + }; + + // The value or the mask of each register alone, of an entry of a + // subleaf. + let (index, entry) = entries + .iter() + .enumerate() + .find(|(_, entry)| entry.index.is_some()) + .unwrap(); + let name = leaf_name(entry.function, entry.index); + for changed_register in 0..4 { + for change_mask in [false, true] { + let mut changed = *entry; + if change_mask { + changed.mask[changed_register] ^= 1 << changed_register; + } else { + changed.result[changed_register] ^= 1 << changed_register; + } + let mut recorded = entries.clone(); + recorded[index] = changed; + assert_eq!( + message(&recorded), + expected(&[format!( + "{name} {}", + register(changed_register, entry, &changed) + )]) + ); + } + } + + // Several registers, in order, on one line. + let mut changed = *entry; + changed.result[3] ^= 1; + changed.mask[1] ^= 1; + changed.result[0] ^= 1; + let mut recorded = entries.clone(); + recorded[index] = changed; + assert_eq!( + message(&recorded), + expected(&[format!( + "{name} {}, {}, {}", + register(0, entry, &changed), + register(1, entry, &changed), + register(3, entry, &changed) + )]) + ); + + // An entry that differs among entries that the snapshot lacks, then + // entries that only the snapshot has. + let mut recorded = entries.clone(); + recorded[0].result[2] ^= 1; + let differing = format!( + "{} {}", + leaf_name(entries[0].function, entries[0].index), + register(2, &entries[0], &recorded[0]) + ); + let missing = [recorded.remove(1), recorded.remove(1)]; + recorded.extend( + [0x8000_00fe, 0x8000_00ff].map(|function| EffectiveCpuidEntry { + function, + index: None, + result: [function, 0, 0, 0], + mask: [!0; 4], + }), + ); + assert_eq!( + message(&recorded), + expected(&[ + differing, + format!( + "{} is not in the snapshot", + leaf_name(missing[0].function, missing[0].index) + ), + format!( + "{} is not in the snapshot", + leaf_name(missing[1].function, missing[1].index) + ), + "CPUID 0x800000fe is only in the snapshot".to_owned(), + "CPUID 0x800000ff is only in the snapshot".to_owned(), + ]) + ); + } + /// Returns the snapshot record of `effective`, an effective CPUID of the /// test profile. fn test_record(effective: &EffectiveCpuid) -> SnapshotCpuProfile { From bcfc47cff6606afadffef97551e538962895b598 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Thu, 29 Oct 2026 20:34:33 -0700 Subject: [PATCH 2/2] openvmm_core: name repeated and unordered entries of a CPUID record When the effective CPUID that a restore recomputes differs from the one that the snapshot recorded, the E_CPU_SURFACE error names every entry that differs. It mapped the snapshot's entries by leaf and subleaf, so an entry listed twice kept only its last listing, and it named a snapshot whose entries were out of order only when nothing else differed. The record's decoder admits both, although no OpenVMM writes them, so the error did not name every difference: a changed listing followed by the entry's own was reported as the same entries in another order, and a record both out of order and with a changed entry named the changed entry alone, which a second restore then followed with the order. Compare every listing of an entry with the partition's, and name each that differs, with the registers in which it differs; name each entry that the snapshot lists more than once, with the number of its listings; and name a snapshot that does not list its entries in order of leaf and subleaf, as the partition's effective CPUID does, whatever else differs. The error keeps its code, and names the differences of a record that OpenVMM writes as before. Tests check the whole error for an entry listed twice, changed before or after its own listing, or unchanged; for entries out of order, alone and with an entry that differs; and for an entry listed again out of order. The snapshot guide says what the error names. Follow-up to microsoft/nvx#568, from the review of microsoft/openvmm#4804. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Guide/src/user_guide/openvmm/snapshots.md | 6 +- .../src/worker/dispatch/time_abi.rs | 153 ++++++++++++++---- 2 files changed, 129 insertions(+), 30 deletions(-) diff --git a/Guide/src/user_guide/openvmm/snapshots.md b/Guide/src/user_guide/openvmm/snapshots.md index 81cb5d0c490..e701a1bc238 100644 --- a/Guide/src/user_guide/openvmm/snapshots.md +++ b/Guide/src/user_guide/openvmm/snapshots.md @@ -361,7 +361,11 @@ must be a valid profile, its CPU time bits included, with the recorded digest; a host of the profile's generation; and an identical effective CPUID. OpenVMM computes the effective CPUID, including its own leaves and the bits it owns, such as the cache topology, so an OpenVMM build that computes it -differently rejects earlier snapshots with `E_CPU_SURFACE`. +differently rejects earlier snapshots with `E_CPU_SURFACE`. That error names +each entry in which the two effective CPUIDs differ, with every register in +which it differs, each entry that only one of them has, each entry that the +snapshot lists more than once, and a snapshot that does not list its entries +in order of leaf and subleaf. These checks all run, even after one of them fails: on restore, the comparison of the effective CPUID with the snapshot's, then, on every boot, the check that VP 0 observes it and the check that the backend supports the diff --git a/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs b/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs index 660f9bd16e2..74b5c6524d5 100644 --- a/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs +++ b/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs @@ -738,10 +738,14 @@ fn record_entries(effective: &EffectiveCpuid) -> impl Iterator = record_entries(effective) .map(|entry| (key(&entry), entry)) .collect(); - let theirs: BTreeMap<_, _> = recorded.iter().map(|entry| (key(entry), *entry)).collect(); + // Every entry that the snapshot lists for a leaf and subleaf, in order. + let mut theirs: BTreeMap<_, Vec<_>> = BTreeMap::new(); + for entry in recorded { + theirs.entry(key(entry)).or_default().push(entry); + } let mut differences = Vec::new(); for (&entry_key, entry) in &ours { - match theirs.get(&entry_key) { - None => differences.push(format!("{} is not in the snapshot", name(entry_key))), - Some(theirs) if theirs != entry => { - let registers: Vec = (0..4) - .filter(|®ister| { - (entry.result[register], entry.mask[register]) - != (theirs.result[register], theirs.mask[register]) - }) - .map(|register| { - format!( - "{} is {:#010x} under mask {:#010x}, and {:#010x} under mask {:#010x} in the snapshot", - REGISTERS[register], - entry.result[register], - entry.mask[register], - theirs.result[register], - theirs.mask[register] - ) - }) - .collect(); - differences.push(format!("{} {}", name(entry_key), registers.join(", "))); - } - Some(_) => {} + let Some(listed) = theirs.get(&entry_key) else { + differences.push(format!("{} is not in the snapshot", name(entry_key))); + continue; + }; + for &theirs in listed.iter().filter(|&&theirs| theirs != entry) { + let registers: Vec = (0..4) + .filter(|®ister| { + (entry.result[register], entry.mask[register]) + != (theirs.result[register], theirs.mask[register]) + }) + .map(|register| { + format!( + "{} is {:#010x} under mask {:#010x}, and {:#010x} under mask {:#010x} in the snapshot", + REGISTERS[register], + entry.result[register], + entry.mask[register], + theirs.result[register], + theirs.mask[register] + ) + }) + .collect(); + differences.push(format!("{} {}", name(entry_key), registers.join(", "))); } } for &entry_key in theirs @@ -787,8 +795,19 @@ fn recorded_cpuid_differences( { differences.push(format!("{} is only in the snapshot", name(entry_key))); } - if differences.is_empty() { - differences.push("the snapshot lists the same entries in another order".to_owned()); + for (&entry_key, listed) in theirs.iter().filter(|(_, listed)| listed.len() > 1) { + differences.push(format!( + "{} is in the snapshot {} times", + name(entry_key), + listed.len() + )); + } + if recorded + .windows(2) + .any(|pair| key(&pair[0]) > key(&pair[1])) + { + differences + .push("the snapshot does not list its entries in order of leaf and subleaf".to_owned()); } differences } @@ -1805,6 +1824,82 @@ mod tests { ); } + /// A failure names every way in which the snapshot's record differs, at + /// once, including those that only a record that no OpenVMM writes has: + /// an entry that the snapshot lists more than once, with each of its + /// listings that differs, and entries out of order, with an entry that + /// differs. + #[test] + fn recorded_cpuid_names_repeated_and_unordered_entries() { + let (_, _, effective) = test_config(2); + let record = test_record(&effective); + let entries: Vec<_> = record_entries(&effective).collect(); + let message = |recorded: &[EffectiveCpuidEntry]| { + let record = SnapshotCpuProfile { + effective_cpuid: encode_effective_cpuid(recorded.iter().copied()), + ..record.clone() + }; + format!( + "{:#}", + check_recorded_cpuid(&effective, &record).unwrap_err() + ) + }; + let expected = |differences: &[&str]| { + format!( + "[E_CPU_SURFACE] the effective CPUID of CPU profile {TEST_PROFILE} differs from \ + the snapshot's in {} entries: {}", + differences.len(), + differences.join("; ") + ) + }; + let unordered = "the snapshot does not list its entries in order of leaf and subleaf"; + + // An entry listed twice, once changed, before or after itself, or + // unchanged. + let first = entries[0]; + let mut changed = first; + changed.result[0] ^= 1; + let name = leaf_name(first.function, first.index); + let difference = format!( + "{name} EAX is {:#010x} under mask {:#010x}, and {:#010x} under mask {:#010x} in the \ + snapshot", + first.result[0], first.mask[0], changed.result[0], changed.mask[0] + ); + let twice = format!("{name} is in the snapshot 2 times"); + for listed in [[changed, first], [first, changed]] { + let recorded: Vec<_> = listed.into_iter().chain(entries[1..].to_vec()).collect(); + assert_eq!(message(&recorded), expected(&[&difference, &twice])); + } + let recorded: Vec<_> = [first, first] + .into_iter() + .chain(entries[1..].to_vec()) + .collect(); + assert_eq!(message(&recorded), expected(&[&twice])); + + // Entries out of order, alone, and with an entry that differs. + let mut recorded = entries.clone(); + recorded.swap(0, 1); + assert_eq!(message(&recorded), expected(&[unordered])); + let last = recorded.len() - 1; + recorded[last].result[1] ^= 1; + let entry = entries[last]; + let difference = format!( + "{} EBX is {:#010x} under mask {:#010x}, and {:#010x} under mask {:#010x} in the \ + snapshot", + leaf_name(entry.function, entry.index), + entry.result[1], + entry.mask[1], + entry.result[1] ^ 1, + entry.mask[1] + ); + assert_eq!(message(&recorded), expected(&[&difference, unordered])); + + // An entry listed again out of order. + let mut recorded = entries.clone(); + recorded.push(first); + assert_eq!(message(&recorded), expected(&[&twice, unordered])); + } + /// Returns the snapshot record of `effective`, an effective CPUID of the /// test profile. fn test_record(effective: &EffectiveCpuid) -> SnapshotCpuProfile {