kvm: x86: Return LA57 feature based on hardware capability [Linux 5.0]

This Linux kernel change "kvm: x86: Return LA57 feature based on hardware capability" is included in the Linux 5.0 release. This change is authored by Yu Zhang <yu.c.zhang [at] linux.intel.com> on Fri Feb 1 00:09:43 2019 +0800. The commit for this change in Linux stable tree is 511da98 (patch).

kvm: x86: Return LA57 feature based on hardware capability

Previously, 'commit 372fddf70904 ("x86/mm: Introduce the 'no5lvl' kernel
parameter")' cleared X86_FEATURE_LA57 in boot_cpu_data, if Linux chooses
to not run in 5-level paging mode. Yet boot_cpu_data is queried by
do_cpuid_ent() as the host capability later when creating vcpus, and Qemu
will not be able to detect this feature and create VMs with LA57 feature.

As discussed earlier, VMs can still benefit from extended linear address
width, e.g. to enhance features like ASLR. So we would like to fix this,
by return the true hardware capability when Qemu queries.

Signed-off-by: Yu Zhang <[email protected]>
Cc: [email protected]
Signed-off-by: Paolo Bonzini <[email protected]>

There are 4 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 arch/x86/kvm/cpuid.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index bbffa6c..c07958b 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -335,6 +335,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
    unsigned f_xsaves = kvm_x86_ops->xsaves_supported() ? F(XSAVES) : 0;
    unsigned f_umip = kvm_x86_ops->umip_emulated() ? F(UMIP) : 0;
    unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0;
+   unsigned f_la57 = 0;

    /* cpuid 1.edx */
    const u32 kvm_cpuid_1_edx_x86_features =
@@ -489,7 +490,10 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
            // TSC_ADJUST is emulated
            entry->ebx |= F(TSC_ADJUST);
            entry->ecx &= kvm_cpuid_7_0_ecx_x86_features;
+           f_la57 = entry->ecx & F(LA57);
            cpuid_mask(&entry->ecx, CPUID_7_ECX);
+           /* Set LA57 based on hardware capability. */
+           entry->ecx |= f_la57;
            entry->ecx |= f_umip;
            /* PKU is not yet implemented for shadow paging. */
            if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))

Leave a Reply

Your email address will not be published. Required fields are marked *