Skip to content

Commit cc0c726

Browse files
wey-gualxiord
authored andcommitted
Some more Unit Tests for set_cpuid2
To introduce more UTs for set_cpuid2 * Setting Manufacturer ID * Disabling Intel SHA extensions Issue: #28 Signed-off-by: Wey Gu <[email protected]>
1 parent c346b17 commit cc0c726

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

src/ioctls/vcpu.rs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ mod tests {
12821282

12831283
#[cfg(target_arch = "x86_64")]
12841284
#[test]
1285-
fn cpuid2_test() {
1285+
fn test_get_cpuid() {
12861286
let kvm = Kvm::new().unwrap();
12871287
if kvm.check_extension(Cap::ExtCpuid) {
12881288
let vm = kvm.create_vm().unwrap();
@@ -1300,6 +1300,77 @@ mod tests {
13001300
}
13011301
}
13021302

1303+
#[cfg(target_arch = "x86_64")]
1304+
#[test]
1305+
fn test_set_cpuid() {
1306+
let kvm = Kvm::new().unwrap();
1307+
if kvm.check_extension(Cap::ExtCpuid) {
1308+
let vm = kvm.create_vm().unwrap();
1309+
let mut cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
1310+
let ncpuids = cpuid.as_slice().len();
1311+
assert!(ncpuids <= KVM_MAX_CPUID_ENTRIES);
1312+
let vcpu = vm.create_vcpu(0).unwrap();
1313+
1314+
// Setting Manufacturer ID
1315+
{
1316+
let entries = cpuid.as_mut_slice();
1317+
for entry in entries.iter_mut() {
1318+
match entry.function {
1319+
0 => {
1320+
// " KVMKVMKVM "
1321+
entry.ebx = 0x4b4d564b;
1322+
entry.ecx = 0x564b4d56;
1323+
entry.edx = 0x4d;
1324+
}
1325+
_ => (),
1326+
}
1327+
}
1328+
}
1329+
vcpu.set_cpuid2(&cpuid).unwrap();
1330+
let cpuid_0 = vcpu.get_cpuid2(ncpuids).unwrap();
1331+
for entry in cpuid_0.as_slice() {
1332+
match entry.function {
1333+
0 => {
1334+
assert_eq!(entry.ebx, 0x4b4d564b);
1335+
assert_eq!(entry.ecx, 0x564b4d56);
1336+
assert_eq!(entry.edx, 0x4d);
1337+
}
1338+
_ => (),
1339+
}
1340+
}
1341+
1342+
// Disabling Intel SHA extensions.
1343+
const EBX_SHA_SHIFT: u32 = 29;
1344+
let mut ebx_sha_off = 0u32;
1345+
{
1346+
let entries = cpuid.as_mut_slice();
1347+
for entry in entries.iter_mut() {
1348+
match entry.function {
1349+
7 => {
1350+
if entry.ecx == 0 {
1351+
entry.ebx &= !(1 << EBX_SHA_SHIFT);
1352+
ebx_sha_off = entry.ebx;
1353+
}
1354+
}
1355+
_ => (),
1356+
}
1357+
}
1358+
}
1359+
vcpu.set_cpuid2(&cpuid).unwrap();
1360+
let cpuid_1 = vcpu.get_cpuid2(ncpuids).unwrap();
1361+
for entry in cpuid_1.as_slice() {
1362+
match entry.function {
1363+
7 => {
1364+
if entry.ecx == 0 {
1365+
assert_eq!(entry.ebx, ebx_sha_off);
1366+
}
1367+
}
1368+
_ => (),
1369+
}
1370+
}
1371+
}
1372+
}
1373+
13031374
#[cfg(target_arch = "x86_64")]
13041375
#[allow(non_snake_case)]
13051376
#[test]

0 commit comments

Comments
 (0)