Skip to content

Commit 115959f

Browse files
committed
1 parent aa59b37 commit 115959f

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/ioctls/vcpu.rs

+45-3
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,8 @@ mod tests {
14171417

14181418
use super::*;
14191419
use ioctls::system::Kvm;
1420+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1421+
use vmm_sys_util::fam::FamStruct;
14201422
#[cfg(any(
14211423
target_arch = "x86",
14221424
target_arch = "x86_64",
@@ -1512,16 +1514,18 @@ mod tests {
15121514
let kvm = Kvm::new().unwrap();
15131515
if kvm.check_extension(Cap::ExtCpuid) {
15141516
let vm = kvm.create_vm().unwrap();
1515-
let mut cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
1517+
//let mut cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
1518+
let mut cpuid = kvm.get_supported_cpuid(48).unwrap();
15161519
let ncpuids = cpuid.as_slice().len();
1520+
assert_eq!(cpuid.as_fam_struct_ref().len(), ncpuids);
15171521
assert!(ncpuids <= KVM_MAX_CPUID_ENTRIES);
15181522
let vcpu = vm.create_vcpu(0).unwrap();
15191523

15201524
// Setting Manufacturer ID
15211525
{
15221526
let entries = cpuid.as_mut_slice();
15231527
for entry in entries.iter_mut() {
1524-
if entry.function == 0 {
1528+
if entry.function == 0 && entry.index == 0 && entry.flags == 0 {
15251529
// " KVMKVMKVM "
15261530
entry.ebx = 0x4b4d564b;
15271531
entry.ecx = 0x564b4d56;
@@ -1531,8 +1535,12 @@ mod tests {
15311535
}
15321536
vcpu.set_cpuid2(&cpuid).unwrap();
15331537
let cpuid_0 = vcpu.get_cpuid2(ncpuids).unwrap();
1538+
assert_eq!(
1539+
cpuid.as_fam_struct_ref().len(),
1540+
cpuid_0.as_fam_struct_ref().len()
1541+
);
15341542
for entry in cpuid_0.as_slice() {
1535-
if entry.function == 0 {
1543+
if entry.function == 0 && entry.index == 0 && entry.flags == 0 {
15361544
assert_eq!(entry.ebx, 0x4b4d564b);
15371545
assert_eq!(entry.ecx, 0x564b4d56);
15381546
assert_eq!(entry.edx, 0x4d);
@@ -1561,6 +1569,40 @@ mod tests {
15611569
}
15621570
}
15631571

1572+
#[cfg(target_arch = "x86_64")]
1573+
#[test]
1574+
fn test_set_cpuid_one_entry() {
1575+
let kvm = Kvm::new().unwrap();
1576+
if kvm.check_extension(Cap::ExtCpuid) {
1577+
let vm = kvm.create_vm().unwrap();
1578+
let mut cpuid = CpuId::new(1).unwrap();
1579+
let ncpuids = cpuid.as_slice().len();
1580+
assert_eq!(ncpuids, 1);
1581+
assert_eq!(cpuid.as_fam_struct_ref().len(), ncpuids);
1582+
let vcpu = vm.create_vcpu(0).unwrap();
1583+
1584+
// Setting Manufacturer ID
1585+
{
1586+
let entries = cpuid.as_mut_slice();
1587+
// " KVMKVMKVM "
1588+
entries[0].ebx = 0x4b4d564b;
1589+
entries[0].ecx = 0x564b4d56;
1590+
entries[0].edx = 0x4d;
1591+
}
1592+
vcpu.set_cpuid2(&cpuid).unwrap();
1593+
let cpuid_0 = vcpu.get_cpuid2(ncpuids).unwrap();
1594+
assert_eq!(
1595+
cpuid.as_fam_struct_ref().len(),
1596+
cpuid_0.as_fam_struct_ref().len()
1597+
);
1598+
for entry in cpuid_0.as_slice() {
1599+
assert_eq!(entry.ebx, 0x4b4d564b);
1600+
assert_eq!(entry.ecx, 0x564b4d56);
1601+
assert_eq!(entry.edx, 0x4d);
1602+
}
1603+
}
1604+
}
1605+
15641606
#[cfg(target_arch = "x86_64")]
15651607
#[allow(non_snake_case)]
15661608
#[test]

0 commit comments

Comments
 (0)