Skip to content

x86: pass through cpuid leaves 15h and 16h #5186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ and this project adheres to
so users need to regenerate snapshots.
- [#4731](https://github.com/firecracker-microvm/firecracker/pull/4731): Added
support for modifying the host TAP device name during snapshot restore.
- [#5186](https://github.com/firecracker-microvm/firecracker/pull/5186): Pass
through CPUID leaves 15h and 16h on Intel CPUs (Crystal clock, processor, and
and bus frequencies).

### Changed

Expand Down
59 changes: 49 additions & 10 deletions src/vmm/src/cpu_config/x86_64/cpuid/intel/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
CheckedAssignError, get_range, set_bit, set_range,
};
use crate::cpu_config::x86_64::cpuid::{
BRAND_STRING_LENGTH, CpuidKey, CpuidRegisters, CpuidTrait, MissingBrandStringLeaves,
BRAND_STRING_LENGTH, CpuidKey, CpuidRegisters, CpuidTrait, MissingBrandStringLeaves, cpuid,
host_brand_string,
};

Expand All @@ -14,12 +14,8 @@
pub enum NormalizeCpuidError {
/// Failed to set deterministic cache leaf: {0}
DeterministicCache(#[from] DeterministicCacheError),
/// Leaf 0x6 is missing from CPUID.
MissingLeaf6,
/// Leaf 0x7 / subleaf 0 is missing from CPUID.
MissingLeaf7,
/// Leaf 0xA is missing from CPUID.
MissingLeafA,
/// Leaf {0} is missing from CPUID.
MissingLeaf(usize),
/// Failed to get brand string: {0}
GetBrandString(DefaultBrandStringError),
/// Failed to set brand string: {0}
Expand Down Expand Up @@ -75,10 +71,53 @@
self.update_performance_monitoring_entry()?;
self.update_extended_topology_v2_entry();
self.update_brand_string_entry()?;
self.update_frequency_information();

Ok(())
}

/// Passes through the host value of cpuid leaves 15h and 16h if they
/// are not already configured via cpu template.
fn update_frequency_information(&mut self) {
let Some(leaf_15h) = self.get_mut(&CpuidKey::leaf(0x15)) else {
return;

Check warning on line 83 in src/vmm/src/cpu_config/x86_64/cpuid/intel/normalize.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/cpu_config/x86_64/cpuid/intel/normalize.rs#L83

Added line #L83 was not covered by tests
};

if leaf_15h.result == CpuidRegisters::default() {
let host_leaf_15 = cpuid(0x15);

// CPUID.15H:EAX[31:0]
// Ratio of TSC frequency to Core Crystal Clock frequency, denominator
leaf_15h.result.eax = host_leaf_15.eax;
// CPUID.15H:EBX[31:0]
// Ratio of TSC frequency to Core Crystal Clock frequency, numerator
leaf_15h.result.ebx = host_leaf_15.ebx;
// CPUID.15H:ECX[31:0]
// Core Crystal Clock frequency, in units of Hz
leaf_15h.result.ecx = host_leaf_15.ecx;
// edx is reserved
}

let Some(leaf_16h) = self.get_mut(&CpuidKey::leaf(0x16)) else {
return;

Check warning on line 102 in src/vmm/src/cpu_config/x86_64/cpuid/intel/normalize.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/cpu_config/x86_64/cpuid/intel/normalize.rs#L102

Added line #L102 was not covered by tests
};

if leaf_16h.result == CpuidRegisters::default() {
let host_leaf_16 = cpuid(0x16);

// CPUID.16H:EAX[15:0]
// Processor Base Frequency (in MHz)
leaf_16h.result.eax = host_leaf_16.eax;
// CPUID.16H:EBX[15:0]
// Processor Maximum Frequency (in MHz)
leaf_16h.result.ebx = host_leaf_16.ebx;
// CPUID.16H:ECX[15:0]
// Bus/Reference frequency (in MHz)
leaf_16h.result.ecx = host_leaf_16.ecx;
// edx is reserved
}
}

/// Update deterministic cache entry
#[allow(clippy::unwrap_in_result)]
fn update_deterministic_cache_entry(
Expand Down Expand Up @@ -164,7 +203,7 @@
fn update_power_management_entry(&mut self) -> Result<(), NormalizeCpuidError> {
let leaf_6 = self
.get_mut(&CpuidKey::leaf(0x6))
.ok_or(NormalizeCpuidError::MissingLeaf6)?;
.ok_or(NormalizeCpuidError::MissingLeaf(6))?;

// CPUID.06H:EAX[1]
// Intel Turbo Boost Technology available (see description of IA32_MISC_ENABLE[38]).
Expand All @@ -184,7 +223,7 @@
fn update_extended_feature_flags_entry(&mut self) -> Result<(), NormalizeCpuidError> {
let leaf_7_0 = self
.get_mut(&CpuidKey::subleaf(0x7, 0))
.ok_or(NormalizeCpuidError::MissingLeaf7)?;
.ok_or(NormalizeCpuidError::MissingLeaf(7))?;

// Set the following bits as recommended in kernel doc. These bits are reserved in AMD.
// - CPUID.07H:EBX[6] (FDP_EXCPTN_ONLY)
Expand Down Expand Up @@ -243,7 +282,7 @@
fn update_performance_monitoring_entry(&mut self) -> Result<(), NormalizeCpuidError> {
let leaf_a = self
.get_mut(&CpuidKey::leaf(0xA))
.ok_or(NormalizeCpuidError::MissingLeafA)?;
.ok_or(NormalizeCpuidError::MissingLeaf(0xA))?;
leaf_a.result = CpuidRegisters {
eax: 0,
ebx: 0,
Expand Down