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 26b54e11ada..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 } @@ -1684,6 +1703,203 @@ 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(), + ]) + ); + } + + /// 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 {