From 7c3110e6d59340489a28b38cdecd43427a222b1b Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Mon, 21 Nov 2022 13:07:11 +0000 Subject: [PATCH] arch: x86_64: Use host cpuid information for L2 cache for older KVM If the KVM version is too old (pre Linux 5.7) then fetch the CPUID information from the host and use that in the guest. We prefer the KVM version over the host version as that would use the CPUID for the running CPU vs the CPU that runs this code which might be different due to a hybrid topology. Fixes: #4918 Signed-off-by: Rob Bradford --- arch/src/x86_64/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/src/x86_64/mod.rs b/arch/src/x86_64/mod.rs index 9d839cbd5..8dceb2664 100644 --- a/arch/src/x86_64/mod.rs +++ b/arch/src/x86_64/mod.rs @@ -633,6 +633,20 @@ pub fn generate_common_cpuid( } } } + // Copy host L2 cache details if not populated by KVM + 0x8000_0006 => { + if entry.eax == 0 && entry.ebx == 0 && entry.ecx == 0 && entry.edx == 0 { + // SAFETY: cpuid called with valid leaves + if unsafe { std::arch::x86_64::__cpuid(0x8000_0000).eax } >= 0x8000_0006 { + // SAFETY: cpuid called with valid leaves + let leaf = unsafe { std::arch::x86_64::__cpuid(0x8000_0006) }; + entry.eax = leaf.eax; + entry.ebx = leaf.ebx; + entry.ecx = leaf.ecx; + entry.edx = leaf.edx; + } + } + } // Set CPU physical bits 0x8000_0008 => { entry.eax = (entry.eax & 0xffff_ff00) | (phys_bits as u32 & 0xff);