Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
054c2af
arch: End generated CPU profile files with a newline
phip1611 Nov 30, 2026
9b94a7a
arch: Snapshot all host MSRs when a CPU profile is used
phip1611 Nov 30, 2026
f2cd7bb
arch: Accept FB_CLEAR profiles on hosts with TSX disabled
phip1611 Nov 30, 2026
c595dfb
arch: Treat IA32_ARCH_CAPABILITIES bit 63 as reserved
phip1611 Nov 30, 2026
7ddf490
arch: Check the AVX10 version for CPUID compatibility
phip1611 Nov 30, 2026
43b0ba9
arch: Ignore x87 defeature bits in CPUID compatibility
phip1611 Nov 30, 2026
92b79a5
arch: Fix the AVX10 CPUID definition of EBX[18:16]
phip1611 Nov 30, 2026
423d30f
arch: Fix overlapping CPUID definitions
phip1611 Nov 30, 2026
38da806
arch: Reject overlapping CPU profile definitions at compile time
phip1611 Nov 30, 2026
75faf10
arch: Align CPUID definition ranges with the SDM
phip1611 Nov 30, 2026
47493b8
arch: Stop exposing unused RDT and XSAVE CPUID fields
phip1611 Nov 30, 2026
6294b53
arch: Fix CPUID definition names and descriptions
phip1611 Nov 30, 2026
056b078
arch: Hide the PKRU XSAVE component in CPU profiles
phip1611 Nov 30, 2026
64c70f9
arch: Pass the VMX CPUID bit through in CPU profiles
phip1611 Nov 30, 2026
8917d06
arch: Always set the x87 defeature bits in CPU profiles
phip1611 Nov 30, 2026
a984c9b
arch: Hide VMX controls of hidden features in CPU profiles
phip1611 Nov 30, 2026
1bddea3
arch: Keep KVM version details out of VMX MSR profiles
phip1611 Nov 30, 2026
9e4c0f2
arch: Fix errors in the MSR definitions
phip1611 Nov 30, 2026
baf412c
arch: Add v2 Sapphire Rapids and Cascade Lake CPU profiles
phip1611 Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions arch/src/x86_64/arch_capabilities_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ const IA32_ARCH_CAPABILITIES: u32 = 0x10a;

/// Check that the MSR updates required by the CPU profile are compatible with the
/// host's feature MSRs.
///
/// `host_has_rtm` tells whether the hypervisor reports RTM (TSX) support.
pub(crate) fn valid_required_arch_capabilities_update(
required_updates: &VcpuMsrConfigUpdate,
host_feature_msrs: &[MsrEntry],
host_has_rtm: bool,
) -> Result<(), Error> {
let find_arch_capabilities = |msrs: &[MsrEntry]| {
msrs.iter()
Expand All @@ -37,6 +40,7 @@ pub(crate) fn valid_required_arch_capabilities_update(
if arch_capabilities_compatible(
required_arch_capabilities_msr,
host_arch_capabilities,
host_has_rtm,
"CPU Profile",
"Host",
) {
Expand All @@ -52,9 +56,17 @@ pub(crate) fn valid_required_arch_capabilities_update(
/// If this check fails then programs that work when the value is `src_val`, may possibly
/// no longer work if the value is `dest_val`.
///
/// `dest_has_rtm` tells whether RTM (TSX) is available where `dest_val` comes from.
///
/// The `src_id` and `dest_id` parameters are used to identify where `src_val` and `dest_val`
/// originate from (e.g. CPU profile, Host) when logging the detected incompatibility.
fn arch_capabilities_compatible(src_val: u64, dest_val: u64, src_id: &str, dest_id: &str) -> bool {
fn arch_capabilities_compatible(
src_val: u64,
dest_val: u64,
dest_has_rtm: bool,
src_id: &str,
dest_id: &str,
) -> bool {
const RSBA_MASK: u64 = 1 << 2;
const RRSBA_MASK: u64 = 1 << 19;
// We consider it unsafe to migrate from a machine without RSBA or RRSBA to one that advertises this behavior.
Expand All @@ -63,11 +75,11 @@ fn arch_capabilities_compatible(src_val: u64, dest_val: u64, src_id: &str, dest_
// said mitigations, but that should be ensured by other CPUID and/or MSR value checks.
const SUPERSET_MASK: u64 = RSBA_MASK | RRSBA_MASK;

// Bits 31 and 33..=61 are (currently) reserved
// Bits 31, 33..=61 and 63 are (currently) reserved
const RESERVED_MASK: u64 = {
let bits_0_to_61 = (1_u64 << 62) - 1;
let bits_0_to_32 = (1_u64 << 33) - 1;
(bits_0_to_61 ^ bits_0_to_32) | (1 << 31)
(bits_0_to_61 ^ bits_0_to_32) | (1 << 31) | (1 << 63)
};

const SUBSET_MASK: u64 = !(SUPERSET_MASK | RESERVED_MASK);
Expand All @@ -78,8 +90,16 @@ fn arch_capabilities_compatible(src_val: u64, dest_val: u64, src_id: &str, dest_
const FBSDP_NO_MASK: u64 = 1 << 14;
const PSDP_NO_MASK: u64 = 1 << 15;
const FB_CLEAR_MASK: u64 = 1 << 17;
const TOLERATE_MISSING_FB_CLEAR_MASK: u64 =
MDS_NO_MASK | TAA_NO_MASK | SBDR_SSDP_NO_MASK | FBSDP_NO_MASK | PSDP_NO_MASK;
// KVM clears TAA_NO when the host has no RTM, e.g. with tsx=off, because
// TAA needs TSX. Such a host is not affected by TAA either way.
let tolerate_missing_fb_clear_mask = {
let mask = MDS_NO_MASK | SBDR_SSDP_NO_MASK | FBSDP_NO_MASK | PSDP_NO_MASK;
if dest_has_rtm {
mask | TAA_NO_MASK
} else {
mask
}
};

// For safety reasons we will require equality on the reserved bits for now: If/when they become unreserved then we can adjust the checks
// accordingly.
Expand All @@ -106,7 +126,7 @@ fn arch_capabilities_compatible(src_val: u64, dest_val: u64, src_id: &str, dest_
//
// TODO: Perhaps we should also rather make Hypervisor::get_msr_based_features() adjust bit
// 17? With CPU profiles this doesn't seem necessary though.
if !(((dest_val & TOLERATE_MISSING_FB_CLEAR_MASK) == TOLERATE_MISSING_FB_CLEAR_MASK)
if !(((dest_val & tolerate_missing_fb_clear_mask) == tolerate_missing_fb_clear_mask)
&& (only_in_src == FB_CLEAR_MASK))
{
subset_check = false;
Expand Down Expand Up @@ -180,11 +200,38 @@ mod tests {
assert!(arch_capabilities_compatible(
cascade_lake_msr_value,
sapphire_rapids_msr_value,
true,
"Cascade Lake",
"Sapphire Rapids",
));
}

// KVM clears TAA_NO on hosts without RTM, e.g. with tsx=off. The Cascade
// Lake profile sets FB_CLEAR, so it must still be usable on such a host.
#[test]
fn check_arch_capabilities_cascade_lake_profile_tsx_off_host() {
const TAA_NO: u64 = 1 << 8;
let cascade_lake_profile_value: u64 = 0xc0aa06b;
let sapphire_rapids_tsx_off_value: u64 = 0x400000000c08e1eb & !TAA_NO;

assert!(arch_capabilities_compatible(
cascade_lake_profile_value,
sapphire_rapids_tsx_off_value,
false,
"Cascade Lake profile",
"Sapphire Rapids host (tsx=off)",
));

// With RTM, a missing TAA_NO means that the host is affected by TAA
assert!(!arch_capabilities_compatible(
cascade_lake_profile_value,
sapphire_rapids_tsx_off_value,
true,
"Cascade Lake profile",
"Sapphire Rapids host",
));
}

#[test]
fn check_arch_capabilities_sapphire_rapids_granite_rapids() {
// Value of IA32_ARCH_CAPABILITIES on Sapphire Rapids (obtained from KVM with kernel version 6.18.33)
Expand All @@ -198,6 +245,7 @@ mod tests {
assert!(arch_capabilities_compatible(
sapphire_rapids_msr_value,
granite_rapids_msr_value,
true,
"Sapphire Rapids",
"Granite Rapids",
));
Expand All @@ -209,6 +257,7 @@ mod tests {
assert!(!arch_capabilities_compatible(
granite_rapids_msr_value,
sapphire_rapids_msr_value,
true,
"Granite Rapids",
"Sapphire Rapids",
));
Expand All @@ -225,6 +274,7 @@ mod tests {
assert!(arch_capabilities_compatible(
restricted_sapphire_rapids_msr_value,
sapphire_rapids_msr_value,
true,
"Sapphire Rapids profile",
"Sapphire Rapids host",
));
Expand All @@ -234,6 +284,7 @@ mod tests {
assert!(arch_capabilities_compatible(
restricted_sapphire_rapids_msr_value,
granite_rapids_msr_value,
true,
"Sapphire Rapids profile",
"Granite Rapids host",
));
Expand All @@ -251,22 +302,33 @@ mod tests {
const RESERVED_ONE: u64 = 1 << 31;
const RESERVED_TWO: u64 = 1 << 42;
const RESERVED_THREE: u64 = 1 << 61;
const RESERVED_FOUR: u64 = 1 << 63;
let sapphire_rapids_msr_value: u64 = 0x400000000c08e1eb;
let with_reserved_bits = sapphire_rapids_msr_value | RESERVED_ONE | RESERVED_THREE;
let with_other_reserved_bits = sapphire_rapids_msr_value | RESERVED_TWO;

assert!(!arch_capabilities_compatible(
with_reserved_bits,
with_other_reserved_bits,
true,
"Reserved 1",
"Reserved 2",
));

assert!(!arch_capabilities_compatible(
with_other_reserved_bits,
with_reserved_bits,
true,
"Reserved 2",
"Reserved 1",
));

assert!(!arch_capabilities_compatible(
sapphire_rapids_msr_value,
sapphire_rapids_msr_value | RESERVED_FOUR,
true,
"Without bit 63",
"With bit 63",
));
}
}
29 changes: 24 additions & 5 deletions arch/src/x86_64/cpu_profile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,15 @@ fn required_msr_updates(
}
}

// Our MSR snapshottable MSRs should consist of the MSR indices that are contained in the set of required MSRs and the feature MSRs to be set.
// The MSR indices/addresses in this vector will be a subset of `required`, but not necessarily an equality as required may contain MSRs
// that the hypervisor does not permit setting (e.g. various VMX CTLS, IA32_VMX_CR0_FIXED, IA32_VMX_CR4_FIXED).
// Snapshot every MSR the hypervisor lists, as the Host profile does.
// `required` is frozen when the profile is generated, so limiting the
// snapshot to it would silently drop MSRs that newer hypervisors save

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a desirable policy?

// (e.g. IA32_DEBUGCTL since KVM 7.1). VMX MSRs only matter with nested
// virtualization.
let mut snapshottable_msr_indices: Vec<u32> = msr_index_list
.iter()
.copied()
.filter(|msr| required.contains(msr))
.filter(|msr| nested || !VMX_FEATURE_MSR_RANGE.contains(msr))
.chain(adjusted_feature_msrs.iter().map(|msr| msr.index))
.collect();
// Remove any duplicate entries
Expand All @@ -274,7 +276,9 @@ fn required_msr_updates(
#[cfg(test)]
mod tests {
use hypervisor::arch::x86::MsrEntry;
use hypervisor::arch::x86::msr_index::{MSR_IA32_UCODE_REV, MSR_IA32_VMX_MISC};
use hypervisor::arch::x86::msr_index::{
MSR_IA32_DEBUGCTLMSR, MSR_IA32_TSC, MSR_IA32_UCODE_REV, MSR_IA32_VMX_MISC,
};
use proptest::prelude::*;

use super::{CpuIdEntry, CpuVendor, CpuidProfileData, CpuidReg, adjust_cpuid};
Expand Down Expand Up @@ -1247,6 +1251,21 @@ mod tests {
);
}

// MSRs that the host saves but the profile does not list, for example
// ones a newer KVM started to save, must still be part of snapshots.
#[test]
fn snapshots_include_msrs_unknown_to_profile() {
let profile_data = MsrProfileData {
adjustments: Vec::new(),
required_msrs: vec![RegisterAddress(MSR_IA32_TSC)],
};
let msr_index_list = [MSR_IA32_TSC, MSR_IA32_DEBUGCTLMSR];

let updates =
required_msr_updates(profile_data, &[], &msr_index_list, false, false).unwrap();
assert_eq!(updates.snapshottable_msr_indices, msr_index_list);
}

proptest! {
#[test]
fn nested_off_does_not_return_vmx_msrs(
Expand Down
6 changes: 6 additions & 0 deletions arch/src/x86_64/cpu_profile_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ fn generate_cpuid_profile_data_with<const N: usize, const M: usize>(

serde_json::to_writer_pretty(&mut cpuid_data_file, &cpuid_profile_data)
.context("Cpu profile generation failed: Could not serialize the generated cpuid profile data to the given writer")?;
cpuid_data_file
.write_all(b"\n")
.context("CPU profile generation failed: Unable to write cpuid profile data")?;
cpuid_data_file
.flush()
.context("CPU profile generation failed: Unable to flush cpuid profile data")?;
Expand Down Expand Up @@ -439,6 +442,9 @@ fn generate_msr_profile_data_with<'a, const N: usize>(

serde_json::to_writer_pretty(&mut msr_data_file, &msr_profile_data)
.context("Cpu profile generation failed: Could not serialize the generated MSR profile data to the given writer")?;
msr_data_file
.write_all(b"\n")
.context("CPU profile generation failed: Unable to write MSR profile data")?;
msr_data_file
.flush()
.context("CPU profile generation failed: Unable to flush MSR profile data")?;
Expand Down
Loading
Loading