Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: x86/mmu: Taking guest pa into consideration when calculate tdp level

For TDX, the maxpa (CPUID.0x80000008.EAX[7:0]) is fixed as native and
the max_gpa (CPUID.0x80000008.EAX[23:16]) is configurable and used
to configure the EPT level and GPAW.

Use max_gpa to determine the TDP level.

Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Xiaoyao Li and committed by
Paolo Bonzini
20d91372 488808e6

+23 -1
+14
arch/x86/kvm/cpuid.c
··· 463 463 return 36; 464 464 } 465 465 466 + int cpuid_query_maxguestphyaddr(struct kvm_vcpu *vcpu) 467 + { 468 + struct kvm_cpuid_entry2 *best; 469 + 470 + best = kvm_find_cpuid_entry(vcpu, 0x80000000); 471 + if (!best || best->eax < 0x80000008) 472 + goto not_found; 473 + best = kvm_find_cpuid_entry(vcpu, 0x80000008); 474 + if (best) 475 + return (best->eax >> 16) & 0xff; 476 + not_found: 477 + return 0; 478 + } 479 + 466 480 /* 467 481 * This "raw" version returns the reserved GPA bits without any adjustments for 468 482 * encryption technologies that usurp bits. The raw mask should be used if and
+1
arch/x86/kvm/cpuid.h
··· 59 59 u32 xstate_required_size(u64 xstate_bv, bool compacted); 60 60 61 61 int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu); 62 + int cpuid_query_maxguestphyaddr(struct kvm_vcpu *vcpu); 62 63 u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu); 63 64 64 65 static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
+8 -1
arch/x86/kvm/mmu/mmu.c
··· 5416 5416 5417 5417 static inline int kvm_mmu_get_tdp_level(struct kvm_vcpu *vcpu) 5418 5418 { 5419 + int maxpa; 5420 + 5421 + if (vcpu->kvm->arch.vm_type == KVM_X86_TDX_VM) 5422 + maxpa = cpuid_query_maxguestphyaddr(vcpu); 5423 + else 5424 + maxpa = cpuid_maxphyaddr(vcpu); 5425 + 5419 5426 /* tdp_root_level is architecture forced level, use it if nonzero */ 5420 5427 if (tdp_root_level) 5421 5428 return tdp_root_level; 5422 5429 5423 5430 /* Use 5-level TDP if and only if it's useful/necessary. */ 5424 - if (max_tdp_level == 5 && cpuid_maxphyaddr(vcpu) <= 48) 5431 + if (max_tdp_level == 5 && maxpa <= 48) 5425 5432 return 4; 5426 5433 5427 5434 return max_tdp_level;