@@ -70,6 +70,9 @@ MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
7070static bool __read_mostly enable_vpid = 1 ;
7171module_param_named (vpid , enable_vpid , bool , 0444 );
7272
73+ static bool __read_mostly enable_vnmi = 1 ;
74+ module_param_named (vnmi , enable_vnmi , bool , S_IRUGO );
75+
7376static bool __read_mostly flexpriority_enabled = 1 ;
7477module_param_named (flexpriority , flexpriority_enabled , bool , S_IRUGO );
7578
@@ -202,6 +205,10 @@ struct loaded_vmcs {
202205 bool nmi_known_unmasked ;
203206 unsigned long vmcs_host_cr3 ; /* May not match real cr3 */
204207 unsigned long vmcs_host_cr4 ; /* May not match real cr4 */
208+ /* Support for vnmi-less CPUs */
209+ int soft_vnmi_blocked ;
210+ ktime_t entry_time ;
211+ s64 vnmi_blocked_time ;
205212 struct list_head loaded_vmcss_on_cpu_link ;
206213};
207214
@@ -1291,6 +1298,11 @@ static inline bool cpu_has_vmx_invpcid(void)
12911298 SECONDARY_EXEC_ENABLE_INVPCID ;
12921299}
12931300
1301+ static inline bool cpu_has_virtual_nmis (void )
1302+ {
1303+ return vmcs_config .pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS ;
1304+ }
1305+
12941306static inline bool cpu_has_vmx_wbinvd_exit (void )
12951307{
12961308 return vmcs_config .cpu_based_2nd_exec_ctrl &
@@ -1348,11 +1360,6 @@ static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
13481360 (vmcs12 -> secondary_vm_exec_control & bit );
13491361}
13501362
1351- static 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-
13561363static inline bool nested_cpu_has_preemption_timer (struct vmcs12 * vmcs12 )
13571364{
13581365 return vmcs12 -> pin_based_vm_exec_control &
@@ -3712,9 +3719,9 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
37123719 & _vmexit_control ) < 0 )
37133720 return - EIO ;
37143721
3715- min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
3716- PIN_BASED_VIRTUAL_NMIS ;
3717- opt = PIN_BASED_POSTED_INTR | PIN_BASED_VMX_PREEMPTION_TIMER ;
3722+ min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING ;
3723+ opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3724+ PIN_BASED_VMX_PREEMPTION_TIMER ;
37183725 if (adjust_vmx_controls (min , opt , MSR_IA32_VMX_PINBASED_CTLS ,
37193726 & _pin_based_exec_control ) < 0 )
37203727 return - EIO ;
@@ -5232,6 +5239,10 @@ static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
52325239
52335240 if (!kvm_vcpu_apicv_active (& vmx -> vcpu ))
52345241 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR ;
5242+
5243+ if (!enable_vnmi )
5244+ pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS ;
5245+
52355246 /* Enable the preemption timer dynamically */
52365247 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER ;
52375248 return pin_based_exec_ctrl ;
@@ -5666,7 +5677,8 @@ static void enable_irq_window(struct kvm_vcpu *vcpu)
56665677
56675678static void enable_nmi_window (struct kvm_vcpu * vcpu )
56685679{
5669- if (vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO ) & GUEST_INTR_STATE_STI ) {
5680+ if (!enable_vnmi ||
5681+ vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO ) & GUEST_INTR_STATE_STI ) {
56705682 enable_irq_window (vcpu );
56715683 return ;
56725684 }
@@ -5706,6 +5718,19 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
57065718{
57075719 struct vcpu_vmx * vmx = to_vmx (vcpu );
57085720
5721+ if (!enable_vnmi ) {
5722+ /*
5723+ * Tracking the NMI-blocked state in software is built upon
5724+ * finding the next open IRQ window. This, in turn, depends on
5725+ * well-behaving guests: They have to keep IRQs disabled at
5726+ * least as long as the NMI handler runs. Otherwise we may
5727+ * cause NMI nesting, maybe breaking the guest. But as this is
5728+ * highly unlikely, we can live with the residual risk.
5729+ */
5730+ vmx -> loaded_vmcs -> soft_vnmi_blocked = 1 ;
5731+ vmx -> loaded_vmcs -> vnmi_blocked_time = 0 ;
5732+ }
5733+
57095734 ++ vcpu -> stat .nmi_injections ;
57105735 vmx -> loaded_vmcs -> nmi_known_unmasked = false;
57115736
@@ -5724,6 +5749,8 @@ static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
57245749 struct vcpu_vmx * vmx = to_vmx (vcpu );
57255750 bool masked ;
57265751
5752+ if (!enable_vnmi )
5753+ return vmx -> loaded_vmcs -> soft_vnmi_blocked ;
57275754 if (vmx -> loaded_vmcs -> nmi_known_unmasked )
57285755 return false;
57295756 masked = vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO ) & GUEST_INTR_STATE_NMI ;
@@ -5735,20 +5762,31 @@ static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
57355762{
57365763 struct vcpu_vmx * vmx = to_vmx (vcpu );
57375764
5738- vmx -> loaded_vmcs -> nmi_known_unmasked = !masked ;
5739- if (masked )
5740- vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,
5741- GUEST_INTR_STATE_NMI );
5742- else
5743- vmcs_clear_bits (GUEST_INTERRUPTIBILITY_INFO ,
5744- GUEST_INTR_STATE_NMI );
5765+ if (!enable_vnmi ) {
5766+ if (vmx -> loaded_vmcs -> soft_vnmi_blocked != masked ) {
5767+ vmx -> loaded_vmcs -> soft_vnmi_blocked = masked ;
5768+ vmx -> loaded_vmcs -> vnmi_blocked_time = 0 ;
5769+ }
5770+ } else {
5771+ vmx -> loaded_vmcs -> nmi_known_unmasked = !masked ;
5772+ if (masked )
5773+ vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,
5774+ GUEST_INTR_STATE_NMI );
5775+ else
5776+ vmcs_clear_bits (GUEST_INTERRUPTIBILITY_INFO ,
5777+ GUEST_INTR_STATE_NMI );
5778+ }
57455779}
57465780
57475781static int vmx_nmi_allowed (struct kvm_vcpu * vcpu )
57485782{
57495783 if (to_vmx (vcpu )-> nested .nested_run_pending )
57505784 return 0 ;
57515785
5786+ if (!enable_vnmi &&
5787+ to_vmx (vcpu )-> loaded_vmcs -> soft_vnmi_blocked )
5788+ return 0 ;
5789+
57525790 return !(vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO ) &
57535791 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
57545792 | GUEST_INTR_STATE_NMI ));
@@ -6476,6 +6514,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
64766514 * AAK134, BY25.
64776515 */
64786516 if (!(to_vmx (vcpu )-> idt_vectoring_info & VECTORING_INFO_VALID_MASK ) &&
6517+ enable_vnmi &&
64796518 (exit_qualification & INTR_INFO_UNBLOCK_NMI ))
64806519 vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO , GUEST_INTR_STATE_NMI );
64816520
@@ -6535,6 +6574,7 @@ static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
65356574
65366575static int handle_nmi_window (struct kvm_vcpu * vcpu )
65376576{
6577+ WARN_ON_ONCE (!enable_vnmi );
65386578 vmcs_clear_bits (CPU_BASED_VM_EXEC_CONTROL ,
65396579 CPU_BASED_VIRTUAL_NMI_PENDING );
65406580 ++ vcpu -> stat .nmi_window_exits ;
@@ -6758,6 +6798,9 @@ static __init int hardware_setup(void)
67586798 if (!cpu_has_vmx_flexpriority ())
67596799 flexpriority_enabled = 0 ;
67606800
6801+ if (!cpu_has_virtual_nmis ())
6802+ enable_vnmi = 0 ;
6803+
67616804 /*
67626805 * set_apic_access_page_addr() is used to reload apic access
67636806 * page upon invalidation. No need to do anything if not
@@ -6962,7 +7005,7 @@ static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
69627005 }
69637006
69647007 /* Create a new VMCS */
6965- item = kmalloc (sizeof (struct vmcs02_list ), GFP_KERNEL );
7008+ item = kzalloc (sizeof (struct vmcs02_list ), GFP_KERNEL );
69667009 if (!item )
69677010 return NULL ;
69687011 item -> vmcs02 .vmcs = alloc_vmcs ();
@@ -7979,6 +8022,7 @@ static int handle_pml_full(struct kvm_vcpu *vcpu)
79798022 * "blocked by NMI" bit has to be set before next VM entry.
79808023 */
79818024 if (!(to_vmx (vcpu )-> idt_vectoring_info & VECTORING_INFO_VALID_MASK ) &&
8025+ enable_vnmi &&
79828026 (exit_qualification & INTR_INFO_UNBLOCK_NMI ))
79838027 vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,
79848028 GUEST_INTR_STATE_NMI );
@@ -8823,6 +8867,25 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
88238867 return 0 ;
88248868 }
88258869
8870+ if (unlikely (!enable_vnmi &&
8871+ vmx -> loaded_vmcs -> soft_vnmi_blocked )) {
8872+ if (vmx_interrupt_allowed (vcpu )) {
8873+ vmx -> loaded_vmcs -> soft_vnmi_blocked = 0 ;
8874+ } else if (vmx -> loaded_vmcs -> vnmi_blocked_time > 1000000000LL &&
8875+ vcpu -> arch .nmi_pending ) {
8876+ /*
8877+ * This CPU don't support us in finding the end of an
8878+ * NMI-blocked window if the guest runs with IRQs
8879+ * disabled. So we pull the trigger after 1 s of
8880+ * futile waiting, but inform the user about this.
8881+ */
8882+ printk (KERN_WARNING "%s: Breaking out of NMI-blocked "
8883+ "state on VCPU %d after 1 s timeout\n" ,
8884+ __func__ , vcpu -> vcpu_id );
8885+ vmx -> loaded_vmcs -> soft_vnmi_blocked = 0 ;
8886+ }
8887+ }
8888+
88268889 if (exit_reason < kvm_vmx_max_exit_handlers
88278890 && kvm_vmx_exit_handlers [exit_reason ])
88288891 return kvm_vmx_exit_handlers [exit_reason ](vcpu );
@@ -9105,33 +9168,38 @@ static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
91059168
91069169 idtv_info_valid = vmx -> idt_vectoring_info & VECTORING_INFO_VALID_MASK ;
91079170
9108- if (vmx -> loaded_vmcs -> nmi_known_unmasked )
9109- return ;
9110- /*
9111- * Can't use vmx->exit_intr_info since we're not sure what
9112- * the exit reason is.
9113- */
9114- exit_intr_info = vmcs_read32 (VM_EXIT_INTR_INFO );
9115- unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI ) != 0 ;
9116- vector = exit_intr_info & INTR_INFO_VECTOR_MASK ;
9117- /*
9118- * SDM 3: 27.7.1.2 (September 2008)
9119- * Re-set bit "block by NMI" before VM entry if vmexit caused by
9120- * a guest IRET fault.
9121- * SDM 3: 23.2.2 (September 2008)
9122- * Bit 12 is undefined in any of the following cases:
9123- * If the VM exit sets the valid bit in the IDT-vectoring
9124- * information field.
9125- * If the VM exit is due to a double fault.
9126- */
9127- if ((exit_intr_info & INTR_INFO_VALID_MASK ) && unblock_nmi &&
9128- vector != DF_VECTOR && !idtv_info_valid )
9129- vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,
9130- GUEST_INTR_STATE_NMI );
9131- else
9132- vmx -> loaded_vmcs -> nmi_known_unmasked =
9133- !(vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO )
9134- & GUEST_INTR_STATE_NMI );
9171+ if (enable_vnmi ) {
9172+ if (vmx -> loaded_vmcs -> nmi_known_unmasked )
9173+ return ;
9174+ /*
9175+ * Can't use vmx->exit_intr_info since we're not sure what
9176+ * the exit reason is.
9177+ */
9178+ exit_intr_info = vmcs_read32 (VM_EXIT_INTR_INFO );
9179+ unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI ) != 0 ;
9180+ vector = exit_intr_info & INTR_INFO_VECTOR_MASK ;
9181+ /*
9182+ * SDM 3: 27.7.1.2 (September 2008)
9183+ * Re-set bit "block by NMI" before VM entry if vmexit caused by
9184+ * a guest IRET fault.
9185+ * SDM 3: 23.2.2 (September 2008)
9186+ * Bit 12 is undefined in any of the following cases:
9187+ * If the VM exit sets the valid bit in the IDT-vectoring
9188+ * information field.
9189+ * If the VM exit is due to a double fault.
9190+ */
9191+ if ((exit_intr_info & INTR_INFO_VALID_MASK ) && unblock_nmi &&
9192+ vector != DF_VECTOR && !idtv_info_valid )
9193+ vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,
9194+ GUEST_INTR_STATE_NMI );
9195+ else
9196+ vmx -> loaded_vmcs -> nmi_known_unmasked =
9197+ !(vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO )
9198+ & GUEST_INTR_STATE_NMI );
9199+ } else if (unlikely (vmx -> loaded_vmcs -> soft_vnmi_blocked ))
9200+ vmx -> loaded_vmcs -> vnmi_blocked_time +=
9201+ ktime_to_ns (ktime_sub (ktime_get (),
9202+ vmx -> loaded_vmcs -> entry_time ));
91359203}
91369204
91379205static void __vmx_complete_interrupts (struct kvm_vcpu * vcpu ,
@@ -9248,6 +9316,11 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
92489316 struct vcpu_vmx * vmx = to_vmx (vcpu );
92499317 unsigned long debugctlmsr , cr3 , cr4 ;
92509318
9319+ /* Record the guest's net vcpu time for enforced NMI injections. */
9320+ if (unlikely (!enable_vnmi &&
9321+ vmx -> loaded_vmcs -> soft_vnmi_blocked ))
9322+ vmx -> loaded_vmcs -> entry_time = ktime_get ();
9323+
92519324 /* Don't enter VMX if guest state is invalid, let the exit handler
92529325 start emulation until we arrive back to a valid state */
92539326 if (vmx -> emulation_required )
0 commit comments