From 7bc764d4e0da03bdbeb0d0f734b368d618944ac3 Mon Sep 17 00:00:00 2001 From: Thomas Barrett Date: Sun, 14 Jan 2024 23:40:00 +0000 Subject: [PATCH] arch: x86_64: enable nested virtualization on amd if supported When using amd topology, the svm feature flag on cpuid leaf 0x8000_0001.ecx is overwritten. We update the amd cpu topology logic to use the flag values that originated in KVM_GET_SUPPORTED_CPUID ioctl and override as necessary. Signed-off-by: Thomas Barrett --- arch/src/x86_64/mod.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/arch/src/x86_64/mod.rs b/arch/src/x86_64/mod.rs index aa6945d99..b90095ef7 100644 --- a/arch/src/x86_64/mod.rs +++ b/arch/src/x86_64/mod.rs @@ -263,6 +263,26 @@ pub struct CpuidPatch { } impl CpuidPatch { + pub fn get_cpuid_reg( + cpuid: &[CpuIdEntry], + function: u32, + index: Option, + reg: CpuidReg, + ) -> Option { + for entry in cpuid.iter() { + if entry.function == function && (index.is_none() || index.unwrap() == entry.index) { + return match reg { + CpuidReg::EAX => Some(entry.eax), + CpuidReg::EBX => Some(entry.ebx), + CpuidReg::ECX => Some(entry.ecx), + CpuidReg::EDX => Some(entry.edx), + }; + } + } + + None + } + pub fn set_cpuid_reg( cpuid: &mut Vec, function: u32, @@ -1326,12 +1346,14 @@ fn update_cpuid_topology( ); CpuidPatch::set_cpuid_reg(cpuid, 0x8000_001e, Some(0), CpuidReg::EDX, 0); if cores_per_die * threads_per_core > 1 { + let ecx = + CpuidPatch::get_cpuid_reg(cpuid, 0x8000_0001, Some(0), CpuidReg::ECX).unwrap_or(0); CpuidPatch::set_cpuid_reg( cpuid, 0x8000_0001, Some(0), CpuidReg::ECX, - (1u32 << 1) | (1u32 << 22), + ecx | (1u32 << 1) | (1u32 << 22), ); CpuidPatch::set_cpuid_reg( cpuid,