0013-kvm-vmx-Reinstate-support-for-CPUs-without-virtual-N.patch 11 KB

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