]>
Commit | Line | Data |
---|---|---|
59d5af67 | 1 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
b42b4a1b FG |
2 | From: Paolo Bonzini <pbonzini@redhat.com> |
3 | Date: Mon, 6 Nov 2017 13:31:12 +0100 | |
59d5af67 | 4 | Subject: [PATCH] kvm: vmx: Reinstate support for CPUs without virtual NMI |
b42b4a1b FG |
5 | MIME-Version: 1.0 |
6 | Content-Type: text/plain; charset=UTF-8 | |
7 | Content-Transfer-Encoding: 8bit | |
8 | ||
9 | commit 8a1b43922d0d1279e7936ba85c4c2a870403c95f upstream. | |
10 | ||
11 | This is more or less a revert of commit 2c82878b0cb3 ("KVM: VMX: require | |
12 | virtual NMI support", 2017-03-27); it turns out that Core 2 Duo machines | |
13 | only had virtual NMIs in some SKUs. | |
14 | ||
15 | The revert is not trivial because in the meanwhile there have been several | |
16 | fixes to nested NMI injection. Therefore, the entire vNMI state is moved | |
17 | to struct loaded_vmcs. | |
18 | ||
19 | Another change compared to before the patch is a simplification here: | |
20 | ||
21 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked && | |
22 | !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis( | |
23 | get_vmcs12(vcpu))))) { | |
24 | ||
25 | The final condition here is always true (because nested_cpu_has_virtual_nmis | |
26 | is always false) and is removed. | |
27 | ||
28 | Fixes: 2c82878b0cb38fd516fd612c67852a6bbf282003 | |
29 | Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1490803 | |
30 | Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> | |
31 | Signed-off-by: Radim Krčmář <rkrcmar@redhat.com> | |
32 | Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> | |
33 | Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com> | |
34 | --- | |
35 | arch/x86/kvm/vmx.c | 150 +++++++++++++++++++++++++++++++++++++---------------- | |
36 | 1 file changed, 106 insertions(+), 44 deletions(-) | |
37 | ||
38 | diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c | |
39 | index 118709e7597d..a2c95522ac99 100644 | |
40 | --- a/arch/x86/kvm/vmx.c | |
41 | +++ b/arch/x86/kvm/vmx.c | |
42 | @@ -202,6 +202,10 @@ struct loaded_vmcs { | |
43 | bool nmi_known_unmasked; | |
44 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ | |
45 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ | |
46 | + /* Support for vnmi-less CPUs */ | |
47 | + int soft_vnmi_blocked; | |
48 | + ktime_t entry_time; | |
49 | + s64 vnmi_blocked_time; | |
50 | struct list_head loaded_vmcss_on_cpu_link; | |
51 | }; | |
52 | ||
53 | @@ -1288,6 +1292,11 @@ static inline bool cpu_has_vmx_invpcid(void) | |
54 | SECONDARY_EXEC_ENABLE_INVPCID; | |
55 | } | |
56 | ||
57 | +static inline bool cpu_has_virtual_nmis(void) | |
58 | +{ | |
59 | + return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; | |
60 | +} | |
61 | + | |
62 | static inline bool cpu_has_vmx_wbinvd_exit(void) | |
63 | { | |
64 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
65 | @@ -1339,11 +1348,6 @@ static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) | |
66 | (vmcs12->secondary_vm_exec_control & bit); | |
67 | } | |
68 | ||
69 | -static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) | |
70 | -{ | |
71 | - return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; | |
72 | -} | |
73 | - | |
74 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) | |
75 | { | |
76 | return vmcs12->pin_based_vm_exec_control & | |
77 | @@ -3676,9 +3680,9 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) | |
78 | &_vmexit_control) < 0) | |
79 | return -EIO; | |
80 | ||
81 | - min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING | | |
82 | - PIN_BASED_VIRTUAL_NMIS; | |
83 | - opt = PIN_BASED_POSTED_INTR | PIN_BASED_VMX_PREEMPTION_TIMER; | |
84 | + min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; | |
85 | + opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | | |
86 | + PIN_BASED_VMX_PREEMPTION_TIMER; | |
87 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, | |
88 | &_pin_based_exec_control) < 0) | |
89 | return -EIO; | |
90 | @@ -5538,7 +5542,8 @@ static void enable_irq_window(struct kvm_vcpu *vcpu) | |
91 | ||
92 | static void enable_nmi_window(struct kvm_vcpu *vcpu) | |
93 | { | |
94 | - if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { | |
95 | + if (!cpu_has_virtual_nmis() || | |
96 | + vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { | |
97 | enable_irq_window(vcpu); | |
98 | return; | |
99 | } | |
100 | @@ -5578,6 +5583,19 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu) | |
101 | { | |
102 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
103 | ||
104 | + if (!cpu_has_virtual_nmis()) { | |
105 | + /* | |
106 | + * Tracking the NMI-blocked state in software is built upon | |
107 | + * finding the next open IRQ window. This, in turn, depends on | |
108 | + * well-behaving guests: They have to keep IRQs disabled at | |
109 | + * least as long as the NMI handler runs. Otherwise we may | |
110 | + * cause NMI nesting, maybe breaking the guest. But as this is | |
111 | + * highly unlikely, we can live with the residual risk. | |
112 | + */ | |
113 | + vmx->loaded_vmcs->soft_vnmi_blocked = 1; | |
114 | + vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
115 | + } | |
116 | + | |
117 | ++vcpu->stat.nmi_injections; | |
118 | vmx->loaded_vmcs->nmi_known_unmasked = false; | |
119 | ||
120 | @@ -5596,6 +5614,8 @@ static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) | |
121 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
122 | bool masked; | |
123 | ||
124 | + if (!cpu_has_virtual_nmis()) | |
125 | + return vmx->loaded_vmcs->soft_vnmi_blocked; | |
126 | if (vmx->loaded_vmcs->nmi_known_unmasked) | |
127 | return false; | |
128 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; | |
129 | @@ -5607,13 +5627,20 @@ static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) | |
130 | { | |
131 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
132 | ||
133 | - vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
134 | - if (masked) | |
135 | - vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
136 | - GUEST_INTR_STATE_NMI); | |
137 | - else | |
138 | - vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, | |
139 | - GUEST_INTR_STATE_NMI); | |
140 | + if (!cpu_has_virtual_nmis()) { | |
141 | + if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { | |
142 | + vmx->loaded_vmcs->soft_vnmi_blocked = masked; | |
143 | + vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
144 | + } | |
145 | + } else { | |
146 | + vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
147 | + if (masked) | |
148 | + vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
149 | + GUEST_INTR_STATE_NMI); | |
150 | + else | |
151 | + vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, | |
152 | + GUEST_INTR_STATE_NMI); | |
153 | + } | |
154 | } | |
155 | ||
156 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) | |
157 | @@ -5621,6 +5648,10 @@ static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) | |
158 | if (to_vmx(vcpu)->nested.nested_run_pending) | |
159 | return 0; | |
160 | ||
161 | + if (!cpu_has_virtual_nmis() && | |
162 | + to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) | |
163 | + return 0; | |
164 | + | |
165 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & | |
166 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI | |
167 | | GUEST_INTR_STATE_NMI)); | |
168 | @@ -6348,6 +6379,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) | |
169 | * AAK134, BY25. | |
170 | */ | |
171 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && | |
172 | + cpu_has_virtual_nmis() && | |
173 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) | |
174 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); | |
175 | ||
176 | @@ -6820,7 +6852,7 @@ static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) | |
177 | } | |
178 | ||
179 | /* Create a new VMCS */ | |
180 | - item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); | |
181 | + item = kzalloc(sizeof(struct vmcs02_list), GFP_KERNEL); | |
182 | if (!item) | |
183 | return NULL; | |
184 | item->vmcs02.vmcs = alloc_vmcs(); | |
185 | @@ -7837,6 +7869,7 @@ static int handle_pml_full(struct kvm_vcpu *vcpu) | |
186 | * "blocked by NMI" bit has to be set before next VM entry. | |
187 | */ | |
188 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && | |
189 | + cpu_has_virtual_nmis() && | |
190 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) | |
191 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
192 | GUEST_INTR_STATE_NMI); | |
193 | @@ -8554,6 +8587,25 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu) | |
194 | return 0; | |
195 | } | |
196 | ||
197 | + if (unlikely(!cpu_has_virtual_nmis() && | |
198 | + vmx->loaded_vmcs->soft_vnmi_blocked)) { | |
199 | + if (vmx_interrupt_allowed(vcpu)) { | |
200 | + vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
201 | + } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && | |
202 | + vcpu->arch.nmi_pending) { | |
203 | + /* | |
204 | + * This CPU don't support us in finding the end of an | |
205 | + * NMI-blocked window if the guest runs with IRQs | |
206 | + * disabled. So we pull the trigger after 1 s of | |
207 | + * futile waiting, but inform the user about this. | |
208 | + */ | |
209 | + printk(KERN_WARNING "%s: Breaking out of NMI-blocked " | |
210 | + "state on VCPU %d after 1 s timeout\n", | |
211 | + __func__, vcpu->vcpu_id); | |
212 | + vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
213 | + } | |
214 | + } | |
215 | + | |
216 | if (exit_reason < kvm_vmx_max_exit_handlers | |
217 | && kvm_vmx_exit_handlers[exit_reason]) | |
218 | return kvm_vmx_exit_handlers[exit_reason](vcpu); | |
219 | @@ -8837,33 +8889,38 @@ static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) | |
220 | ||
221 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
222 | ||
223 | - if (vmx->loaded_vmcs->nmi_known_unmasked) | |
224 | - return; | |
225 | - /* | |
226 | - * Can't use vmx->exit_intr_info since we're not sure what | |
227 | - * the exit reason is. | |
228 | - */ | |
229 | - exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
230 | - unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; | |
231 | - vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
232 | - /* | |
233 | - * SDM 3: 27.7.1.2 (September 2008) | |
234 | - * Re-set bit "block by NMI" before VM entry if vmexit caused by | |
235 | - * a guest IRET fault. | |
236 | - * SDM 3: 23.2.2 (September 2008) | |
237 | - * Bit 12 is undefined in any of the following cases: | |
238 | - * If the VM exit sets the valid bit in the IDT-vectoring | |
239 | - * information field. | |
240 | - * If the VM exit is due to a double fault. | |
241 | - */ | |
242 | - if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && | |
243 | - vector != DF_VECTOR && !idtv_info_valid) | |
244 | - vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
245 | - GUEST_INTR_STATE_NMI); | |
246 | - else | |
247 | - vmx->loaded_vmcs->nmi_known_unmasked = | |
248 | - !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) | |
249 | - & GUEST_INTR_STATE_NMI); | |
250 | + if (cpu_has_virtual_nmis()) { | |
251 | + if (vmx->loaded_vmcs->nmi_known_unmasked) | |
252 | + return; | |
253 | + /* | |
254 | + * Can't use vmx->exit_intr_info since we're not sure what | |
255 | + * the exit reason is. | |
256 | + */ | |
257 | + exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
258 | + unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; | |
259 | + vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
260 | + /* | |
261 | + * SDM 3: 27.7.1.2 (September 2008) | |
262 | + * Re-set bit "block by NMI" before VM entry if vmexit caused by | |
263 | + * a guest IRET fault. | |
264 | + * SDM 3: 23.2.2 (September 2008) | |
265 | + * Bit 12 is undefined in any of the following cases: | |
266 | + * If the VM exit sets the valid bit in the IDT-vectoring | |
267 | + * information field. | |
268 | + * If the VM exit is due to a double fault. | |
269 | + */ | |
270 | + if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && | |
271 | + vector != DF_VECTOR && !idtv_info_valid) | |
272 | + vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
273 | + GUEST_INTR_STATE_NMI); | |
274 | + else | |
275 | + vmx->loaded_vmcs->nmi_known_unmasked = | |
276 | + !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) | |
277 | + & GUEST_INTR_STATE_NMI); | |
278 | + } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) | |
279 | + vmx->loaded_vmcs->vnmi_blocked_time += | |
280 | + ktime_to_ns(ktime_sub(ktime_get(), | |
281 | + vmx->loaded_vmcs->entry_time)); | |
282 | } | |
283 | ||
284 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, | |
285 | @@ -8980,6 +9037,11 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) | |
286 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
287 | unsigned long debugctlmsr, cr3, cr4; | |
288 | ||
289 | + /* Record the guest's net vcpu time for enforced NMI injections. */ | |
290 | + if (unlikely(!cpu_has_virtual_nmis() && | |
291 | + vmx->loaded_vmcs->soft_vnmi_blocked)) | |
292 | + vmx->loaded_vmcs->entry_time = ktime_get(); | |
293 | + | |
294 | /* Don't enter VMX if guest state is invalid, let the exit handler | |
295 | start emulation until we arrive back to a valid state */ | |
296 | if (vmx->emulation_required) | |
297 | -- | |
298 | 2.14.2 | |
299 |