aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/x86/kvm/vmx.c150
1 files changed, 106 insertions, 44 deletions
diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 7c3522a989d0..bfee7f4ac1dd 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -202,6 +202,10 @@ struct loaded_vmcs {
202 bool nmi_known_unmasked; 202 bool nmi_known_unmasked;
203 unsigned long vmcs_host_cr3; /* May not match real cr3 */ 203 unsigned long vmcs_host_cr3; /* May not match real cr3 */
204 unsigned long vmcs_host_cr4; /* May not match real cr4 */ 204 unsigned long vmcs_host_cr4; /* May not match real cr4 */
205 /* Support for vnmi-less CPUs */
206 int soft_vnmi_blocked;
207 ktime_t entry_time;
208 s64 vnmi_blocked_time;
205 struct list_head loaded_vmcss_on_cpu_link; 209 struct list_head loaded_vmcss_on_cpu_link;
206}; 210};
207 211
@@ -1291,6 +1295,11 @@ static inline bool cpu_has_vmx_invpcid(void)
1291 SECONDARY_EXEC_ENABLE_INVPCID; 1295 SECONDARY_EXEC_ENABLE_INVPCID;
1292} 1296}
1293 1297
1298static inline bool cpu_has_virtual_nmis(void)
1299{
1300 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1301}
1302
1294static inline bool cpu_has_vmx_wbinvd_exit(void) 1303static inline bool cpu_has_vmx_wbinvd_exit(void)
1295{ 1304{
1296 return vmcs_config.cpu_based_2nd_exec_ctrl & 1305 return vmcs_config.cpu_based_2nd_exec_ctrl &
@@ -1348,11 +1357,6 @@ static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1348 (vmcs12->secondary_vm_exec_control & bit); 1357 (vmcs12->secondary_vm_exec_control & bit);
1349} 1358}
1350 1359
1351static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1352{
1353 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1354}
1355
1356static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) 1360static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1357{ 1361{
1358 return vmcs12->pin_based_vm_exec_control & 1362 return vmcs12->pin_based_vm_exec_control &
@@ -3712,9 +3716,9 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
3712 &_vmexit_control) < 0) 3716 &_vmexit_control) < 0)
3713 return -EIO; 3717 return -EIO;
3714 3718
3715 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING | 3719 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3716 PIN_BASED_VIRTUAL_NMIS; 3720 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3717 opt = PIN_BASED_POSTED_INTR | PIN_BASED_VMX_PREEMPTION_TIMER; 3721 PIN_BASED_VMX_PREEMPTION_TIMER;
3718 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, 3722 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3719 &_pin_based_exec_control) < 0) 3723 &_pin_based_exec_control) < 0)
3720 return -EIO; 3724 return -EIO;
@@ -5666,7 +5670,8 @@ static void enable_irq_window(struct kvm_vcpu *vcpu)
5666 5670
5667static void enable_nmi_window(struct kvm_vcpu *vcpu) 5671static void enable_nmi_window(struct kvm_vcpu *vcpu)
5668{ 5672{
5669 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { 5673 if (!cpu_has_virtual_nmis() ||
5674 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5670 enable_irq_window(vcpu); 5675 enable_irq_window(vcpu);
5671 return; 5676 return;
5672 } 5677 }
@@ -5706,6 +5711,19 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5706{ 5711{
5707 struct vcpu_vmx *vmx = to_vmx(vcpu); 5712 struct vcpu_vmx *vmx = to_vmx(vcpu);
5708 5713
5714 if (!cpu_has_virtual_nmis()) {
5715 /*
5716 * Tracking the NMI-blocked state in software is built upon
5717 * finding the next open IRQ window. This, in turn, depends on
5718 * well-behaving guests: They have to keep IRQs disabled at
5719 * least as long as the NMI handler runs. Otherwise we may
5720 * cause NMI nesting, maybe breaking the guest. But as this is
5721 * highly unlikely, we can live with the residual risk.
5722 */
5723 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5724 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5725 }
5726
5709 ++vcpu->stat.nmi_injections; 5727 ++vcpu->stat.nmi_injections;
5710 vmx->loaded_vmcs->nmi_known_unmasked = false; 5728 vmx->loaded_vmcs->nmi_known_unmasked = false;
5711 5729
@@ -5724,6 +5742,8 @@ static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5724 struct vcpu_vmx *vmx = to_vmx(vcpu); 5742 struct vcpu_vmx *vmx = to_vmx(vcpu);
5725 bool masked; 5743 bool masked;
5726 5744
5745 if (!cpu_has_virtual_nmis())
5746 return vmx->loaded_vmcs->soft_vnmi_blocked;
5727 if (vmx->loaded_vmcs->nmi_known_unmasked) 5747 if (vmx->loaded_vmcs->nmi_known_unmasked)
5728 return false; 5748 return false;
5729 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; 5749 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
@@ -5735,13 +5755,20 @@ static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5735{ 5755{
5736 struct vcpu_vmx *vmx = to_vmx(vcpu); 5756 struct vcpu_vmx *vmx = to_vmx(vcpu);
5737 5757
5738 vmx->loaded_vmcs->nmi_known_unmasked = !masked; 5758 if (!cpu_has_virtual_nmis()) {
5739 if (masked) 5759 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5740 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 5760 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5741 GUEST_INTR_STATE_NMI); 5761 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5742 else 5762 }
5743 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, 5763 } else {
5744 GUEST_INTR_STATE_NMI); 5764 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5765 if (masked)
5766 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5767 GUEST_INTR_STATE_NMI);
5768 else
5769 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5770 GUEST_INTR_STATE_NMI);
5771 }
5745} 5772}
5746 5773
5747static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) 5774static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
@@ -5749,6 +5776,10 @@ static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5749 if (to_vmx(vcpu)->nested.nested_run_pending) 5776 if (to_vmx(vcpu)->nested.nested_run_pending)
5750 return 0; 5777 return 0;
5751 5778
5779 if (!cpu_has_virtual_nmis() &&
5780 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5781 return 0;
5782
5752 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 5783 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5753 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI 5784 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5754 | GUEST_INTR_STATE_NMI)); 5785 | GUEST_INTR_STATE_NMI));
@@ -6476,6 +6507,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
6476 * AAK134, BY25. 6507 * AAK134, BY25.
6477 */ 6508 */
6478 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && 6509 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6510 cpu_has_virtual_nmis() &&
6479 (exit_qualification & INTR_INFO_UNBLOCK_NMI)) 6511 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
6480 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); 6512 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6481 6513
@@ -6962,7 +6994,7 @@ static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6962 } 6994 }
6963 6995
6964 /* Create a new VMCS */ 6996 /* Create a new VMCS */
6965 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); 6997 item = kzalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
6966 if (!item) 6998 if (!item)
6967 return NULL; 6999 return NULL;
6968 item->vmcs02.vmcs = alloc_vmcs(); 7000 item->vmcs02.vmcs = alloc_vmcs();
@@ -7979,6 +8011,7 @@ static int handle_pml_full(struct kvm_vcpu *vcpu)
7979 * "blocked by NMI" bit has to be set before next VM entry. 8011 * "blocked by NMI" bit has to be set before next VM entry.
7980 */ 8012 */
7981 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && 8013 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
8014 cpu_has_virtual_nmis() &&
7982 (exit_qualification & INTR_INFO_UNBLOCK_NMI)) 8015 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7983 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 8016 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7984 GUEST_INTR_STATE_NMI); 8017 GUEST_INTR_STATE_NMI);
@@ -8823,6 +8856,25 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
8823 return 0; 8856 return 0;
8824 } 8857 }
8825 8858
8859 if (unlikely(!cpu_has_virtual_nmis() &&
8860 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8861 if (vmx_interrupt_allowed(vcpu)) {
8862 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8863 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8864 vcpu->arch.nmi_pending) {
8865 /*
8866 * This CPU don't support us in finding the end of an
8867 * NMI-blocked window if the guest runs with IRQs
8868 * disabled. So we pull the trigger after 1 s of
8869 * futile waiting, but inform the user about this.
8870 */
8871 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8872 "state on VCPU %d after 1 s timeout\n",
8873 __func__, vcpu->vcpu_id);
8874 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8875 }
8876 }
8877
8826 if (exit_reason < kvm_vmx_max_exit_handlers 8878 if (exit_reason < kvm_vmx_max_exit_handlers
8827 && kvm_vmx_exit_handlers[exit_reason]) 8879 && kvm_vmx_exit_handlers[exit_reason])
8828 return kvm_vmx_exit_handlers[exit_reason](vcpu); 8880 return kvm_vmx_exit_handlers[exit_reason](vcpu);
@@ -9105,33 +9157,38 @@ static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9105 9157
9106 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; 9158 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9107 9159
9108 if (vmx->loaded_vmcs->nmi_known_unmasked) 9160 if (cpu_has_virtual_nmis()) {
9109 return; 9161 if (vmx->loaded_vmcs->nmi_known_unmasked)
9110 /* 9162 return;
9111 * Can't use vmx->exit_intr_info since we're not sure what 9163 /*
9112 * the exit reason is. 9164 * Can't use vmx->exit_intr_info since we're not sure what
9113 */ 9165 * the exit reason is.
9114 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); 9166 */
9115 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; 9167 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9116 vector = exit_intr_info & INTR_INFO_VECTOR_MASK; 9168 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9117 /* 9169 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9118 * SDM 3: 27.7.1.2 (September 2008) 9170 /*
9119 * Re-set bit "block by NMI" before VM entry if vmexit caused by 9171 * SDM 3: 27.7.1.2 (September 2008)
9120 * a guest IRET fault. 9172 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9121 * SDM 3: 23.2.2 (September 2008) 9173 * a guest IRET fault.
9122 * Bit 12 is undefined in any of the following cases: 9174 * SDM 3: 23.2.2 (September 2008)
9123 * If the VM exit sets the valid bit in the IDT-vectoring 9175 * Bit 12 is undefined in any of the following cases:
9124 * information field. 9176 * If the VM exit sets the valid bit in the IDT-vectoring
9125 * If the VM exit is due to a double fault. 9177 * information field.
9126 */ 9178 * If the VM exit is due to a double fault.
9127 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && 9179 */
9128 vector != DF_VECTOR && !idtv_info_valid) 9180 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9129 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 9181 vector != DF_VECTOR && !idtv_info_valid)
9130 GUEST_INTR_STATE_NMI); 9182 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9131 else 9183 GUEST_INTR_STATE_NMI);
9132 vmx->loaded_vmcs->nmi_known_unmasked = 9184 else
9133 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) 9185 vmx->loaded_vmcs->nmi_known_unmasked =
9134 & GUEST_INTR_STATE_NMI); 9186 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9187 & GUEST_INTR_STATE_NMI);
9188 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9189 vmx->loaded_vmcs->vnmi_blocked_time +=
9190 ktime_to_ns(ktime_sub(ktime_get(),
9191 vmx->loaded_vmcs->entry_time));
9135} 9192}
9136 9193
9137static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, 9194static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
@@ -9248,6 +9305,11 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
9248 struct vcpu_vmx *vmx = to_vmx(vcpu); 9305 struct vcpu_vmx *vmx = to_vmx(vcpu);
9249 unsigned long debugctlmsr, cr3, cr4; 9306 unsigned long debugctlmsr, cr3, cr4;
9250 9307
9308 /* Record the guest's net vcpu time for enforced NMI injections. */
9309 if (unlikely(!cpu_has_virtual_nmis() &&
9310 vmx->loaded_vmcs->soft_vnmi_blocked))
9311 vmx->loaded_vmcs->entry_time = ktime_get();
9312
9251 /* Don't enter VMX if guest state is invalid, let the exit handler 9313 /* Don't enter VMX if guest state is invalid, let the exit handler
9252 start emulation until we arrive back to a valid state */ 9314 start emulation until we arrive back to a valid state */
9253 if (vmx->emulation_required) 9315 if (vmx->emulation_required)