From e661139e1e22b80ce0a78a489f13689269b55193 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Wed, 11 Jan 2023 11:37:19 +0000 Subject: [PATCH] arch: Print details of host hypervisor status & address space size e.g. on QEMU on KVM: cloud-hypervisor: 17.079406ms: INFO:arch/src/x86_64/mod.rs:565 -- Running under nested virtualisation. Hypervisor string: KVMKVMKVM Or under Azure: cloud-hypervisor: 3.881263ms: INFO:arch/src/x86_64/mod.rs:565 -- Running under nested virtualisation. Hypervisor string: Microsoft Hv Fixes: #5067 Signed-off-by: Rob Bradford --- arch/src/x86_64/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/arch/src/x86_64/mod.rs b/arch/src/x86_64/mod.rs index 5e8d663eb..151f23f96 100644 --- a/arch/src/x86_64/mod.rs +++ b/arch/src/x86_64/mod.rs @@ -554,6 +554,23 @@ pub fn generate_common_cpuid( kvm_hyperv: bool, #[cfg(feature = "tdx")] tdx_enabled: bool, ) -> super::Result> { + // SAFETY: cpuid called with valid leaves + if unsafe { x86_64::__cpuid(1) }.ecx & 1 << HYPERVISOR_ECX_BIT == 1 << HYPERVISOR_ECX_BIT { + // SAFETY: cpuid called with valid leaves + let hypervisor_cpuid = unsafe { x86_64::__cpuid(0x4000_0000) }; + + let mut identifier: [u8; 12] = [0; 12]; + identifier[0..4].copy_from_slice(&hypervisor_cpuid.ebx.to_le_bytes()[..]); + identifier[4..8].copy_from_slice(&hypervisor_cpuid.ecx.to_le_bytes()[..]); + identifier[8..12].copy_from_slice(&hypervisor_cpuid.edx.to_le_bytes()[..]); + + info!( + "Running under nested virtualisation. Hypervisor string: {}", + String::from_utf8_lossy(&identifier) + ); + } + + info!("Generating guest CPUID for with physical address size: {phys_bits}"); let cpuid_patches = vec![ // Patch tsc deadline timer bit CpuidPatch {