KVM: nVMX: Enable unrestricted guest mode and fix some nEPT issues
cover KVM: nVMX: Enable unrestricted guest mode and fix some nEPT issues
These patches apply on top of nEPT v6 and basically enable unrestricted guest mode to be used by L1.
Jan Kiszka (7):
KVM: nEPT: Advertise WB type EPTP
KVM: nVMX: Replace kvm_set_cr0 with vmx_set_cr0 in
load_vmcs12_host_state
KVM: nVMX: Enable unrestricted guest mode support
KVM: nVMX: Fix guest CR3 read-back on VM-exit
KVM: nVMX: Load nEPT state after EFER
KVM: nVMX: Implement support for EFER saving on VM-exit
KVM: nVMX: Update mmu.base_role.nxe after EFER loading on
VM-entry/exit
arch/x86/kvm/vmx.c | 44 ++++++++++++++++++++++++++++++-------------- 1 files changed, 30 insertions(+), 14 deletions(-)
Signature · 1 line
1.7.3.4
PATCH 1/7 KVM: nEPT: Advertise WB type EPTP
From: Jan Kiszka <[email protected]>
At least WB must be possible.
--- arch/x86/kvm/vmx.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +2 -2
@@ -2250,8 +2250,8 @@ static __init void nested_vmx_setup_ctls_msrs(void)if (enable_ept) {/* nested EPT: emulate EPT also to L1 */ nested_vmx_secondary_ctls_high |=SECONDARY_EXEC_ENABLE_EPT ; - nested_vmx_ept_caps =VMX_EPT_PAGE_WALK_4_BIT ; - nested_vmx_ept_caps |=VMX_EPT_INVEPT_BIT ; + nested_vmx_ept_caps =VMX_EPT_PAGE_WALK_4_BIT | +VMX_EPTP_WB_BIT |VMX_EPT_INVEPT_BIT ; nested_vmx_ept_caps &= vmx_capability.ept;/* * Since invept is completely emulated we support both global
Signature · 1 line
1.7.3.4
PATCH 2/7 KVM: nVMX: Replace kvm_set_cr0 with vmx_set_cr0 in load_vmcs12_host_state
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Likely a typo, but a fatal one as kvm_set_cr0 performs checks on the state transition that may prevent loading L1's cr0.
--- arch/x86/kvm/vmx.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/vmx.c +1 -1
@@ -8174,7 +8174,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,* fpu_active (which may have changed). * Note that vmx_set_cr0 refers to efer set above. */ -kvm_set_cr0 (vcpu, vmcs12->host_cr0); +vmx_set_cr0 (vcpu, vmcs12->host_cr0);/* * If we did fpu_activate()/fpu_deactivate() during L2's run, we need * to apply the same changes to L1's vmcs. We just set cr0 correctly,
Signature · 1 line
1.7.3.4
PATCH 3/7 KVM: nVMX: Enable unrestricted guest mode support
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Now that we provide EPT support, there is no reason to torture our guests by hiding the relieving unrestricted guest mode feature. We just need to relax CR0 checks for always-on bits as PE and PG can now be switched off.
--- arch/x86/kvm/vmx.c | 18 ++++++++++++++---- 1 files changed, 14 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx.c +14 -4
@@ -2245,6 +2245,7 @@ static __init void nested_vmx_setup_ctls_msrs(void) nested_vmx_secondary_ctls_low =0 ; nested_vmx_secondary_ctls_high &=SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | +SECONDARY_EXEC_UNRESTRICTED_GUEST |SECONDARY_EXEC_WBINVD_EXITING ;if (enable_ept) {
@@ -4868,6 +4869,17 @@ vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) hypercall[2 ] =0xc1 ; } +static bool nested_cr0_valid (struct vmcs12 *vmcs12,unsigned long val) +{ +unsigned long always_on =VMXON_CR0_ALWAYSON ; + +if (nested_vmx_secondary_ctls_high & +SECONDARY_EXEC_UNRESTRICTED_GUEST && +nested_cpu_has2 (vmcs12,SECONDARY_EXEC_UNRESTRICTED_GUEST )) + always_on &= ~(X86_CR0_PE |X86_CR0_PG ); +return (val & always_on) == always_on; +} +/* called to set cr0 as appropriate for a mov-to-cr0 exit. */ static int handle_set_cr0 (struct kvm_vcpu *vcpu,unsigned long val) {
@@ -4886,9 +4898,7 @@ static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) val = (val & ~vmcs12->cr0_guest_host_mask) | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); -/* TODO: will have to take unrestricted guest mode into -* account */ -if ((val &VMXON_CR0_ALWAYSON ) !=VMXON_CR0_ALWAYSON ) +if (!nested_cr0_valid (vmcs12, val))return 1 ;if (kvm_set_cr0 (vcpu, val))
@@ -7849,7 +7859,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)return 1 ; } -if (((vmcs12->guest_cr0 &VMXON_CR0_ALWAYSON ) !=VMXON_CR0_ALWAYSON ) || +if (!nested_cr0_valid (vmcs12, vmcs12->guest_cr0) || ((vmcs12->guest_cr4 &VMXON_CR4_ALWAYSON ) !=VMXON_CR4_ALWAYSON )) {nested_vmx_entry_failure (vcpu, vmcs12,EXIT_REASON_INVALID_STATE ,ENTRY_FAIL_DEFAULT );
Signature · 1 line
1.7.3.4
PATCH 4/7 KVM: nVMX: Fix guest CR3 read-back on VM-exit
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
If nested EPT is enabled, the L2 guest may change CR3 without any exits. We therefore have to read the current value from the VMCS when switching to L1. However, if paging wasn't enabled, L0 tracks L2's CR3, and GUEST_CR3 rather contains the real-mode identity map. So we need to retrieve CR3 from the architectural state after conditionally updating it - and this is what kvm_read_cr3 does.
--- arch/x86/kvm/vmx.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/vmx.c +1 -1
@@ -8097,7 +8097,7 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)* Additionally, restore L2's PDPTR to vmcs12. */ if (enable_ept) { - vmcs12->guest_cr3 =vmcs_read64 (GUEST_CR3 ); + vmcs12->guest_cr3 =kvm_read_cr3 (vcpu); vmcs12->guest_pdptr0 =vmcs_read64 (GUEST_PDPTR0 ); vmcs12->guest_pdptr1 =vmcs_read64 (GUEST_PDPTR1 ); vmcs12->guest_pdptr2 =vmcs_read64 (GUEST_PDPTR2 );
Signature · 1 line
1.7.3.4
PATCH 5/7 KVM: nVMX: Load nEPT state after EFER
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
We need to update EFER.NX before building the nEPT state via nested_ept_init_mmu_context. Otherwise, we risk to create an MMU context that claims to have NX disabled while the guest EPT used NX. This will cause spurious faults for L2.
--- arch/x86/kvm/vmx.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +5 -5
@@ -7724,11 +7724,6 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)vmx_flush_tlb (vcpu); } -if (nested_cpu_has_ept (vmcs12)) { -kvm_mmu_unload (vcpu); -nested_ept_init_mmu_context (vcpu); - } -if (vmcs12->vm_entry_controls &VM_ENTRY_LOAD_IA32_EFER ) vcpu->arch.efer = vmcs12->guest_ia32_efer;else if (vmcs12->vm_entry_controls &VM_ENTRY_IA32E_MODE )
@@ -7738,6 +7733,11 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)/* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ vmx_set_efer (vcpu, vcpu->arch.efer); +if (nested_cpu_has_ept (vmcs12)) { +kvm_mmu_unload (vcpu); +nested_ept_init_mmu_context (vcpu); + } +/* * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
Signature · 1 line
1.7.3.4
PATCH 6/7 KVM: nVMX: Implement support for EFER saving on VM-exit
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Implement and advertise VM_EXIT_SAVE_IA32_EFER. L0 traps EFER writes unconditionally, so we always find the current L2 value in the architectural state.
--- arch/x86/kvm/vmx.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
arch/x86/kvm/vmx.c +3 -1
@@ -2203,7 +2203,7 @@ static __init void nested_vmx_setup_ctls_msrs(void) nested_vmx_exit_ctls_high =0 ;#endif nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | -VM_EXIT_LOAD_IA32_EFER ); +VM_EXIT_LOAD_IA32_EFER |VM_EXIT_SAVE_IA32_EFER );/* entry controls */ rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
@@ -8113,6 +8113,8 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) vmcs12->guest_ia32_debugctl =vmcs_read64 (GUEST_IA32_DEBUGCTL );if (vmcs12->vm_exit_controls &VM_EXIT_SAVE_IA32_PAT ) vmcs12->guest_ia32_pat =vmcs_read64 (GUEST_IA32_PAT ); +if (vmcs12->vm_exit_controls &VM_EXIT_SAVE_IA32_EFER ) + vmcs12->guest_ia32_efer = vcpu->arch.efer; vmcs12->guest_sysenter_cs =vmcs_read32 (GUEST_SYSENTER_CS ); vmcs12->guest_sysenter_esp =vmcs_readl (GUEST_SYSENTER_ESP ); vmcs12->guest_sysenter_eip =vmcs_readl (GUEST_SYSENTER_EIP );
Signature · 1 line
1.7.3.4
PATCH 7/7 KVM: nVMX: Update mmu.base_role.nxe after EFER loading on VM-entry/exit
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
This job is normally performed by the architectural EFER set service which we cannot use as it prevents transitions that are valid when switching between L1 and L2. So open-code the update of base_role.nxe after changing EFER on VM-entry and exit.
--- arch/x86/kvm/vmx.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
arch/x86/kvm/vmx.c +4 -0
@@ -7732,6 +7732,8 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) vcpu->arch.efer &= ~(EFER_LMA |EFER_LME );/* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ vmx_set_efer (vcpu, vcpu->arch.efer); + vcpu->arch.mmu.base_role.nxe = + (vcpu->arch.efer &EFER_NX ) && !enable_ept;if (nested_cpu_has_ept (vmcs12)) {kvm_mmu_unload (vcpu);
@@ -8176,6 +8178,8 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,else vcpu->arch.efer &= ~(EFER_LMA |EFER_LME );vmx_set_efer (vcpu, vcpu->arch.efer); + vcpu->arch.mmu.base_role.nxe = + (vcpu->arch.efer &EFER_NX ) && !enable_ept;kvm_register_write (vcpu,VCPU_REGS_RSP , vmcs12->host_rsp);kvm_register_write (vcpu,VCPU_REGS_RIP , vmcs12->host_rip);
Signature · 1 line
1.7.3.4
Re: [PATCH 6/7] KVM: nVMX: Implement support for EFER saving on VM-exit
In reply to Jan KiszkaQuoted 16 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
Implement and advertise
VM_EXIT_SAVE_IA32_EFER. L0 traps EFER writes unconditionally, so we always find the current L2 value in the architectural state.Signed-off-by Jan Kiszka <[email protected]>--- arch/x86/kvm/vmx.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)arch/x86/kvm/vmx.c +0
-0
arch/x86/kvm/vmx.c static __init void nested_vmx_setup_ctls_msrs(void) · line 2206
Quoted 5 lines
arch/x86/kvm/vmx.c +1
-1@@ -2203,7 +2203,7 @@ static __init void nested_vmx_setup_ctls_msrs(void) nested_vmx_exit_ctls_high =0 ;#endif nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | -VM_EXIT_LOAD_IA32_EFER ); +VM_EXIT_LOAD_IA32_EFER |VM_EXIT_SAVE_IA32_EFER );
Here you may need to access MSR_IA32_VMX_EXIT_CTLS first to check if host support these two features. The codes for "entry controls" following it can achieve goals like this.
Arthur
Quoted 19 lines
arch/x86/kvm/vmx.c +2
-1/* entry controls */ rdmsr(MSR_IA32_VMX_ENTRY_CTLS,@@ -8113,6 +8113,8 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) vmcs12->guest_ia32_debugctl =vmcs_read64 (GUEST_IA32_DEBUGCTL );if (vmcs12->vm_exit_controls &VM_EXIT_SAVE_IA32_PAT ) vmcs12->guest_ia32_pat =vmcs_read64 (GUEST_IA32_PAT ); +if (vmcs12->vm_exit_controls &VM_EXIT_SAVE_IA32_EFER ) + vmcs12->guest_ia32_efer = vcpu->arch.efer; vmcs12->guest_sysenter_cs =vmcs_read32 (GUEST_SYSENTER_CS ); vmcs12->guest_sysenter_esp =vmcs_readl (GUEST_SYSENTER_ESP ); vmcs12->guest_sysenter_eip =vmcs_readl (GUEST_SYSENTER_EIP ); --1.7.3.4
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 6/7] KVM: nVMX: Implement support for EFER saving on VM-exit
Quoted 12 lines · Arthur Chunqi Li
Quoted 11 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
Implement and advertise
VM_EXIT_SAVE_IA32_EFER. L0 traps EFER writes unconditionally, so we always find the current L2 value in the architectural state.Signed-off-by Jan Kiszka <[email protected]>--- arch/x86/kvm/vmx.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
Quoted 13 lines
arch/x86/kvm/vmx.c static __init void nested_vmx_setup_ctls_msrs(void) · line 2206
Quoted 10 lines
arch/x86/kvm/vmx.c +1
-1@@ -2203,7 +2203,7 @@ static __init void nested_vmx_setup_ctls_msrs(void) nested_vmx_exit_ctls_high =0 ;#endif nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | -VM_EXIT_LOAD_IA32_EFER ); +VM_EXIT_LOAD_IA32_EFER |VM_EXIT_SAVE_IA32_EFER );Here you may need to access
MSR_IA32_VMX_EXIT_CTLSfirst to check if host support these two features. The codes for "entry controls" following it can achieve goals like this.
We are providing this feature independently of the host's support. Every write to EFER is trapped by L0, so we can emulate saving by reading what L0 recorded.
Jan
Re: [PATCH 1/7] KVM: nEPT: Advertise WB type EPTP
In reply to Jan KiszkaQuoted 5 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
At least WB must be possible.
Signed-off-by Jan Kiszka <[email protected]>
Quoted 21 lines
--- arch/x86/kvm/vmx.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)arch/x86/kvm/vmx.c +2
-2@@ -2250,8 +2250,8 @@ static __init void nested_vmx_setup_ctls_msrs(void)if (enable_ept) {/* nested EPT: emulate EPT also to L1 */ nested_vmx_secondary_ctls_high |=SECONDARY_EXEC_ENABLE_EPT ; - nested_vmx_ept_caps =VMX_EPT_PAGE_WALK_4_BIT ; - nested_vmx_ept_caps |=VMX_EPT_INVEPT_BIT ; + nested_vmx_ept_caps =VMX_EPT_PAGE_WALK_4_BIT | +VMX_EPTP_WB_BIT |VMX_EPT_INVEPT_BIT ; nested_vmx_ept_caps &= vmx_capability.ept;/* * Since invept is completely emulated we support both global Signature · 1 line
1.7.3.4
--
Gleb.