Skip to content

Commit 25f4fdc

Browse files
kalyazinalsrdn
authored andcommitted
cpuid: add unit tests for msrs_to_save_by_cpuid
Signed-off-by: Nikita Kalyazin <[email protected]>
1 parent 1a16a6c commit 25f4fdc

File tree

1 file changed

+44
-0
lines changed
  • src/cpuid/src/template/intel

1 file changed

+44
-0
lines changed

src/cpuid/src/template/intel/mod.rs

+44
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,47 @@ pub(crate) fn msrs_to_save_by_cpuid(cpuid: &CpuId) -> HashSet<u32> {
5252
);
5353
msrs
5454
}
55+
56+
#[cfg(test)]
57+
pub mod tests {
58+
use super::*;
59+
60+
#[test]
61+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
62+
fn test_msrs_to_save_by_cpuid_empty() {
63+
// No CPUID entries are provided.
64+
let entrs = [];
65+
let cpuid = CpuId::from_entries(&entrs).unwrap();
66+
67+
let msrs = msrs_to_save_by_cpuid(&cpuid);
68+
69+
// No MSRs are expected to match the CPUID features.
70+
assert!(msrs.is_empty());
71+
}
72+
73+
#[test]
74+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
75+
fn test_msrs_to_save_by_cpuid_one() {
76+
use kvm_bindings::kvm_cpuid_entry2;
77+
78+
use crate::cpu_leaf::leaf_0x7;
79+
80+
// One CPUID entry with MPX feature flag is provided
81+
// that causes MSR_IA32_BNDCFGS to be pulled in.
82+
let entrs = [kvm_cpuid_entry2 {
83+
function: 0x7,
84+
index: 0x0,
85+
ebx: 1 << leaf_0x7::index0::ebx::MPX_BITINDEX,
86+
..Default::default()
87+
}];
88+
let cpuid = CpuId::from_entries(&entrs).unwrap();
89+
90+
let msrs = msrs_to_save_by_cpuid(&cpuid);
91+
92+
// One MSR is expected to be pulled in by the CPUID feature provided.
93+
assert_eq!(msrs.len(), 1);
94+
95+
// The expected MSR is MSR_IA32_BNDCFGS.
96+
assert!(msrs.contains(&MSR_IA32_BNDCFGS));
97+
}
98+
}

0 commit comments

Comments
 (0)