Skip to content

Commit 3c5c324

Browse files
kaihuangbonzini
authored andcommitted
KVM: VMX: Include MKTME KeyID bits in shadow_zero_check
Intel MKTME KeyID bits (including Intel TDX private KeyID bits) should never be set to SPTE. Set shadow_me_value to 0 and shadow_me_mask to include all MKTME KeyID bits to include them to shadow_zero_check. Signed-off-by: Kai Huang <kai.huang@intel.com> Message-Id: <27bc10e97a3c0b58a4105ff9107448c190328239.1650363789.git.kai.huang@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent e54f1ff commit 3c5c324

3 files changed

Lines changed: 50 additions & 19 deletions

File tree

arch/x86/kvm/mmu.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ static inline gfn_t kvm_mmu_max_gfn(void)
8989
return (1ULL << (max_gpa_bits - PAGE_SHIFT)) - 1;
9090
}
9191

92+
static inline u8 kvm_get_shadow_phys_bits(void)
93+
{
94+
/*
95+
* boot_cpu_data.x86_phys_bits is reduced when MKTME or SME are detected
96+
* in CPU detection code, but the processor treats those reduced bits as
97+
* 'keyID' thus they are not reserved bits. Therefore KVM needs to look at
98+
* the physical address bits reported by CPUID.
99+
*/
100+
if (likely(boot_cpu_data.extended_cpuid_level >= 0x80000008))
101+
return cpuid_eax(0x80000008) & 0xff;
102+
103+
/*
104+
* Quite weird to have VMX or SVM but not MAXPHYADDR; probably a VM with
105+
* custom CPUID. Proceed with whatever the kernel found since these features
106+
* aren't virtualizable (SME/SEV also require CPUIDs higher than 0x80000008).
107+
*/
108+
return boot_cpu_data.x86_phys_bits;
109+
}
110+
92111
void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask);
93112
void kvm_mmu_set_me_spte_mask(u64 me_value, u64 me_mask);
94113
void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only);

arch/x86/kvm/mmu/spte.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -311,25 +311,6 @@ u64 kvm_mmu_changed_pte_notifier_make_spte(u64 old_spte, kvm_pfn_t new_pfn)
311311
return new_spte;
312312
}
313313

314-
static u8 kvm_get_shadow_phys_bits(void)
315-
{
316-
/*
317-
* boot_cpu_data.x86_phys_bits is reduced when MKTME or SME are detected
318-
* in CPU detection code, but the processor treats those reduced bits as
319-
* 'keyID' thus they are not reserved bits. Therefore KVM needs to look at
320-
* the physical address bits reported by CPUID.
321-
*/
322-
if (likely(boot_cpu_data.extended_cpuid_level >= 0x80000008))
323-
return cpuid_eax(0x80000008) & 0xff;
324-
325-
/*
326-
* Quite weird to have VMX or SVM but not MAXPHYADDR; probably a VM with
327-
* custom CPUID. Proceed with whatever the kernel found since these features
328-
* aren't virtualizable (SME/SEV also require CPUIDs higher than 0x80000008).
329-
*/
330-
return boot_cpu_data.x86_phys_bits;
331-
}
332-
333314
u64 mark_spte_for_access_track(u64 spte)
334315
{
335316
if (spte_ad_enabled(spte))

arch/x86/kvm/vmx/vmx.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7883,6 +7883,31 @@ static __init void vmx_setup_user_return_msrs(void)
78837883
kvm_add_user_return_msr(vmx_uret_msrs_list[i]);
78847884
}
78857885

7886+
static void __init vmx_setup_me_spte_mask(void)
7887+
{
7888+
u64 me_mask = 0;
7889+
7890+
/*
7891+
* kvm_get_shadow_phys_bits() returns shadow_phys_bits. Use
7892+
* the former to avoid exposing shadow_phys_bits.
7893+
*
7894+
* On pre-MKTME system, boot_cpu_data.x86_phys_bits equals to
7895+
* shadow_phys_bits. On MKTME and/or TDX capable systems,
7896+
* boot_cpu_data.x86_phys_bits holds the actual physical address
7897+
* w/o the KeyID bits, and shadow_phys_bits equals to MAXPHYADDR
7898+
* reported by CPUID. Those bits between are KeyID bits.
7899+
*/
7900+
if (boot_cpu_data.x86_phys_bits != kvm_get_shadow_phys_bits())
7901+
me_mask = rsvd_bits(boot_cpu_data.x86_phys_bits,
7902+
kvm_get_shadow_phys_bits() - 1);
7903+
/*
7904+
* Unlike SME, host kernel doesn't support setting up any
7905+
* MKTME KeyID on Intel platforms. No memory encryption
7906+
* bits should be included into the SPTE.
7907+
*/
7908+
kvm_mmu_set_me_spte_mask(0, me_mask);
7909+
}
7910+
78867911
static struct kvm_x86_init_ops vmx_init_ops __initdata;
78877912

78887913
static __init int hardware_setup(void)
@@ -7985,6 +8010,12 @@ static __init int hardware_setup(void)
79858010
kvm_mmu_set_ept_masks(enable_ept_ad_bits,
79868011
cpu_has_vmx_ept_execute_only());
79878012

8013+
/*
8014+
* Setup shadow_me_value/shadow_me_mask to include MKTME KeyID
8015+
* bits to shadow_zero_check.
8016+
*/
8017+
vmx_setup_me_spte_mask();
8018+
79888019
kvm_configure_mmu(enable_ept, 0, vmx_get_max_tdp_level(),
79898020
ept_caps_to_lpage_level(vmx_capability.ept));
79908021

0 commit comments

Comments
 (0)