Skip to content

Commit 844a5fe

Browse files
committed
KVM: MMU: fix ept=0/pte.u=1/pte.w=0/CR0.WP=0/CR4.SMEP=1/EFER.NX=0 combo
Yes, all of these are needed. :) This is admittedly a bit odd, but kvm-unit-tests access.flat tests this if you run it with "-cpu host" and of course ept=0. KVM runs the guest with CR0.WP=1, so it must handle supervisor writes specially when pte.u=1/pte.w=0/CR0.WP=0. Such writes cause a fault when U=1 and W=0 in the SPTE, but they must succeed because CR0.WP=0. When KVM gets the fault, it sets U=0 and W=1 in the shadow PTE and restarts execution. This will still cause a user write to fault, while supervisor writes will succeed. User reads will fault spuriously now, and KVM will then flip U and W again in the SPTE (U=1, W=0). User reads will be enabled and supervisor writes disabled, going back to the originary situation where supervisor writes fault spuriously. When SMEP is in effect, however, U=0 will enable kernel execution of this page. To avoid this, KVM also sets NX=1 in the shadow PTE together with U=0. If the guest has not enabled NX, the result is a continuous stream of page faults due to the NX bit being reserved. The fix is to force EFER.NX=1 even if the CPU is taking care of the EFER switch. (All machines with SMEP have the CPU_LOAD_IA32_EFER vm-entry control, so they do not use user-return notifiers for EFER---if they did, EFER.NX would be forced to the same value as the host). There is another bug in the reserved bit check, which I've split to a separate patch for easier application to stable kernels. Cc: [email protected] Cc: Andy Lutomirski <[email protected]> Reviewed-by: Xiao Guangrong <[email protected]> Fixes: f6577a5 Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 313f636 commit 844a5fe

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

Documentation/virtual/kvm/mmu.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ In the first case there are two additional complications:
358358
- if CR4.SMEP is enabled: since we've turned the page into a kernel page,
359359
the kernel may now execute it. We handle this by also setting spte.nx.
360360
If we get a user fetch or read fault, we'll change spte.u=1 and
361-
spte.nx=gpte.nx back.
361+
spte.nx=gpte.nx back. For this to work, KVM forces EFER.NX to 1 when
362+
shadow paging is in use.
362363
- if CR4.SMAP is disabled: since the page has been changed to a kernel
363364
page, it can not be reused when CR4.SMAP is enabled. We set
364365
CR4.SMAP && !CR0.WP into shadow page's role to avoid this case. Note,

arch/x86/kvm/vmx.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,26 +1857,31 @@ static void reload_tss(void)
18571857

18581858
static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
18591859
{
1860-
u64 guest_efer;
1861-
u64 ignore_bits;
1860+
u64 guest_efer = vmx->vcpu.arch.efer;
1861+
u64 ignore_bits = 0;
18621862

1863-
guest_efer = vmx->vcpu.arch.efer;
1863+
if (!enable_ept) {
1864+
/*
1865+
* NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
1866+
* host CPUID is more efficient than testing guest CPUID
1867+
* or CR4. Host SMEP is anyway a requirement for guest SMEP.
1868+
*/
1869+
if (boot_cpu_has(X86_FEATURE_SMEP))
1870+
guest_efer |= EFER_NX;
1871+
else if (!(guest_efer & EFER_NX))
1872+
ignore_bits |= EFER_NX;
1873+
}
18641874

18651875
/*
1866-
* NX is emulated; LMA and LME handled by hardware; SCE meaningless
1867-
* outside long mode
1876+
* LMA and LME handled by hardware; SCE meaningless outside long mode.
18681877
*/
1869-
ignore_bits = EFER_NX | EFER_SCE;
1878+
ignore_bits |= EFER_SCE;
18701879
#ifdef CONFIG_X86_64
18711880
ignore_bits |= EFER_LMA | EFER_LME;
18721881
/* SCE is meaningful only in long mode on Intel */
18731882
if (guest_efer & EFER_LMA)
18741883
ignore_bits &= ~(u64)EFER_SCE;
18751884
#endif
1876-
guest_efer &= ~ignore_bits;
1877-
guest_efer |= host_efer & ignore_bits;
1878-
vmx->guest_msrs[efer_offset].data = guest_efer;
1879-
vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
18801885

18811886
clear_atomic_switch_msr(vmx, MSR_EFER);
18821887

@@ -1887,16 +1892,21 @@ static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
18871892
*/
18881893
if (cpu_has_load_ia32_efer ||
18891894
(enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
1890-
guest_efer = vmx->vcpu.arch.efer;
18911895
if (!(guest_efer & EFER_LMA))
18921896
guest_efer &= ~EFER_LME;
18931897
if (guest_efer != host_efer)
18941898
add_atomic_switch_msr(vmx, MSR_EFER,
18951899
guest_efer, host_efer);
18961900
return false;
1897-
}
1901+
} else {
1902+
guest_efer &= ~ignore_bits;
1903+
guest_efer |= host_efer & ignore_bits;
18981904

1899-
return true;
1905+
vmx->guest_msrs[efer_offset].data = guest_efer;
1906+
vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
1907+
1908+
return true;
1909+
}
19001910
}
19011911

19021912
static unsigned long segment_base(u16 selector)

0 commit comments

Comments
 (0)