Skip to content

Commit d02fcf5

Browse files
committed
kvm: vmx: Allow disabling virtual NMI support
To simplify testing of these rarely used code paths, add a module parameter that turns it on. One eventinj.flat test (NMI after iret) fails when loading kvm_intel with vnmi=0. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
1 parent 8a1b439 commit d02fcf5

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

‎arch/x86/kvm/vmx.c‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
7070
static bool __read_mostly enable_vpid = 1;
7171
module_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+
7376
static bool __read_mostly flexpriority_enabled = 1;
7477
module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
7578

@@ -5236,6 +5239,10 @@ static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
52365239

52375240
if (!kvm_vcpu_apicv_active(&vmx->vcpu))
52385241
pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
5242+
5243+
if (!enable_vnmi)
5244+
pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5245+
52395246
/* Enable the preemption timer dynamically */
52405247
pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
52415248
return pin_based_exec_ctrl;
@@ -5670,7 +5677,7 @@ static void enable_irq_window(struct kvm_vcpu *vcpu)
56705677

56715678
static void enable_nmi_window(struct kvm_vcpu *vcpu)
56725679
{
5673-
if (!cpu_has_virtual_nmis() ||
5680+
if (!enable_vnmi ||
56745681
vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
56755682
enable_irq_window(vcpu);
56765683
return;
@@ -5711,7 +5718,7 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
57115718
{
57125719
struct vcpu_vmx *vmx = to_vmx(vcpu);
57135720

5714-
if (!cpu_has_virtual_nmis()) {
5721+
if (!enable_vnmi) {
57155722
/*
57165723
* Tracking the NMI-blocked state in software is built upon
57175724
* finding the next open IRQ window. This, in turn, depends on
@@ -5742,7 +5749,7 @@ static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
57425749
struct vcpu_vmx *vmx = to_vmx(vcpu);
57435750
bool masked;
57445751

5745-
if (!cpu_has_virtual_nmis())
5752+
if (!enable_vnmi)
57465753
return vmx->loaded_vmcs->soft_vnmi_blocked;
57475754
if (vmx->loaded_vmcs->nmi_known_unmasked)
57485755
return false;
@@ -5755,7 +5762,7 @@ static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
57555762
{
57565763
struct vcpu_vmx *vmx = to_vmx(vcpu);
57575764

5758-
if (!cpu_has_virtual_nmis()) {
5765+
if (!enable_vnmi) {
57595766
if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
57605767
vmx->loaded_vmcs->soft_vnmi_blocked = masked;
57615768
vmx->loaded_vmcs->vnmi_blocked_time = 0;
@@ -5776,7 +5783,7 @@ static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
57765783
if (to_vmx(vcpu)->nested.nested_run_pending)
57775784
return 0;
57785785

5779-
if (!cpu_has_virtual_nmis() &&
5786+
if (!enable_vnmi &&
57805787
to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
57815788
return 0;
57825789

@@ -6507,7 +6514,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
65076514
* AAK134, BY25.
65086515
*/
65096516
if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6510-
cpu_has_virtual_nmis() &&
6517+
enable_vnmi &&
65116518
(exit_qualification & INTR_INFO_UNBLOCK_NMI))
65126519
vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
65136520

@@ -6567,6 +6574,7 @@ static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
65676574

65686575
static int handle_nmi_window(struct kvm_vcpu *vcpu)
65696576
{
6577+
WARN_ON_ONCE(!enable_vnmi);
65706578
vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
65716579
CPU_BASED_VIRTUAL_NMI_PENDING);
65726580
++vcpu->stat.nmi_window_exits;
@@ -6790,6 +6798,9 @@ static __init int hardware_setup(void)
67906798
if (!cpu_has_vmx_flexpriority())
67916799
flexpriority_enabled = 0;
67926800

6801+
if (!cpu_has_virtual_nmis())
6802+
enable_vnmi = 0;
6803+
67936804
/*
67946805
* set_apic_access_page_addr() is used to reload apic access
67956806
* page upon invalidation. No need to do anything if not
@@ -8011,7 +8022,7 @@ static int handle_pml_full(struct kvm_vcpu *vcpu)
80118022
* "blocked by NMI" bit has to be set before next VM entry.
80128023
*/
80138024
if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
8014-
cpu_has_virtual_nmis() &&
8025+
enable_vnmi &&
80158026
(exit_qualification & INTR_INFO_UNBLOCK_NMI))
80168027
vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
80178028
GUEST_INTR_STATE_NMI);
@@ -8856,7 +8867,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
88568867
return 0;
88578868
}
88588869

8859-
if (unlikely(!cpu_has_virtual_nmis() &&
8870+
if (unlikely(!enable_vnmi &&
88608871
vmx->loaded_vmcs->soft_vnmi_blocked)) {
88618872
if (vmx_interrupt_allowed(vcpu)) {
88628873
vmx->loaded_vmcs->soft_vnmi_blocked = 0;
@@ -9157,7 +9168,7 @@ static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
91579168

91589169
idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
91599170

9160-
if (cpu_has_virtual_nmis()) {
9171+
if (enable_vnmi) {
91619172
if (vmx->loaded_vmcs->nmi_known_unmasked)
91629173
return;
91639174
/*
@@ -9306,7 +9317,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
93069317
unsigned long debugctlmsr, cr3, cr4;
93079318

93089319
/* Record the guest's net vcpu time for enforced NMI injections. */
9309-
if (unlikely(!cpu_has_virtual_nmis() &&
9320+
if (unlikely(!enable_vnmi &&
93109321
vmx->loaded_vmcs->soft_vnmi_blocked))
93119322
vmx->loaded_vmcs->entry_time = ktime_get();
93129323

0 commit comments

Comments
 (0)