• Sean Christopherson's avatar
    KVM: x86: Allow guest to set EFER.NX=1 on non-PAE 32-bit kernels · 1383279c
    Sean Christopherson authored
    Remove an ancient restriction that disallowed exposing EFER.NX to the
    guest if EFER.NX=0 on the host, even if NX is fully supported by the CPU.
    The motivation of the check, added by commit 2cc51560 ("KVM: VMX:
    Avoid saving and restoring msr_efer on lightweight vmexit"), was to rule
    out the case of host.EFER.NX=0 and guest.EFER.NX=1 so that KVM could run
    the guest with the host's EFER.NX and thus avoid context switching EFER
    if the only divergence was the NX bit.
    
    Fast forward to today, and KVM has long since stopped running the guest
    with the host's EFER.NX.  Not only does KVM context switch EFER if
    host.EFER.NX=1 && guest.EFER.NX=0, KVM also forces host.EFER.NX=0 &&
    guest.EFER.NX=1 when using shadow paging (to emulate SMEP).  Furthermore,
    the entire motivation for the restriction was made obsolete over a decade
    ago when Intel added dedicated host and guest EFER fields in the VMCS
    (Nehalem timeframe), which reduced the overhead of context switching EFER
    from 400+ cycles (2 * WRMSR + 1 * RDMSR) to a mere ~2 cycles.
    
    In practice, the removed restriction only affects non-PAE 32-bit kernels,
    as EFER.NX is set during boot if NX is supported and the kernel will use
    PAE paging (32-bit or 64-bit), regardless of whether or not the kernel
    will actually use NX itself (mark PTEs non-executable).
    
    Alternatively and/or complementarily, startup_32_smp() in head_32.S could
    be modified to set EFER.NX=1 regardless of paging mode, thus eliminating
    the scenario where NX is supported but not enabled.  However, that runs
    the risk of breaking non-KVM non-PAE kernels (though the risk is very,
    very low as there are no known EFER.NX errata), and also eliminates an
    easy-to-use mechanism for stressing KVM's handling of guest vs. host EFER
    across nested virtualization transitions.
    Suggested-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
    Message-Id: <20210805183804.1221554-1-seanjc@google.com>
    Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    1383279c
cpuid.c 33.3 KB