Skip to content

Commit 20d9137

Browse files
calmisibonzini
authored andcommitted
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>
1 parent 488808e commit 20d9137

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

arch/x86/kvm/cpuid.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,20 @@ int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
463463
return 36;
464464
}
465465

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+
466480
/*
467481
* This "raw" version returns the reserved GPA bits without any adjustments for
468482
* encryption technologies that usurp bits. The raw mask should be used if and

arch/x86/kvm/cpuid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ void __init kvm_init_xstate_sizes(void);
5959
u32 xstate_required_size(u64 xstate_bv, bool compacted);
6060

6161
int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu);
62+
int cpuid_query_maxguestphyaddr(struct kvm_vcpu *vcpu);
6263
u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu);
6364

6465
static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)

arch/x86/kvm/mmu/mmu.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5416,12 +5416,19 @@ void __kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,
54165416

54175417
static inline int kvm_mmu_get_tdp_level(struct kvm_vcpu *vcpu)
54185418
{
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+
54195426
/* tdp_root_level is architecture forced level, use it if nonzero */
54205427
if (tdp_root_level)
54215428
return tdp_root_level;
54225429

54235430
/* 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)
54255432
return 4;
54265433

54275434
return max_tdp_level;

0 commit comments

Comments
 (0)