History

Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm

Pull KVM updates from Paolo Bonzini: "s390: - ioctl hardening - selftests ARM: - ITS translation cache - support for 512 vCPUs - various cleanups and bugfixes PPC: - various minor fixes and preparation x86: - bugfixes all over the place (posted interrupts, SVM, emulation corner cases, blocked INIT) - some IPI optimizations" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: (75 commits) KVM: X86: Use IPI shorthands in kvm guest when support KVM: x86: Fix INIT signal handling in various CPU states KVM: VMX: Introduce exit reason for receiving INIT signal on guest-mode KVM: VMX: Stop the preemption timer during vCPU reset KVM: LAPIC: Micro optimize IPI latency kvm: Nested KVM MMUs need PAE root too KVM: x86: set ctxt->have_exception in x86_decode_insn() KVM: x86: always stop emulation on page fault KVM: nVMX: trace nested VM-Enter failures detected by H/W KVM: nVMX: add tracepoint for failed nested VM-Enter x86: KVM: svm: Fix a check in nested_svm_vmrun() KVM: x86: Return to userspace with internal error on unexpected exit reason KVM: x86: Add kvm_emulate_{rd,wr}msr() to consolidate VXM/SVM code KVM: x86: Refactor up kvm_{g,s}et_msr() to simplify callers doc: kvm: Fix return description of KVM_SET_MSRS KVM: X86: Tune PLE Window tracepoint KVM: VMX: Change ple_window type to unsigned int KVM: X86: Remove tailing newline for tracepoints KVM: X86: Trace vcpu_id for vmexit KVM: x86: Manually calculate reserved bits when loading PDPTRS ...

Linus Torvalds committed 7 years ago

Commit fe38bd62 parents 404e634 fb3925d63 files · +1694 −797Browse files

Unified · Split

63 files
  1. Documentation/virt/kvm/api.txt +24 −9
  2. Documentation/virt/kvm/mmu.txt +2 −2
  3. arch/arm/include/uapi/asm/kvm.h +3 −1
  4. arch/arm64/include/asm/pgtable-prot.h +1 −1
  5. arch/arm64/include/uapi/asm/kvm.h +3 −1
  6. arch/arm64/kvm/hyp/tlb.c +13 −1
  7. arch/powerpc/include/asm/kvm_host.h +18 −4
  8. arch/powerpc/include/asm/kvm_ppc.h +1 −0
  9. arch/powerpc/include/asm/xive.h +9 −0
  10. arch/powerpc/kvm/book3s.c +5 −3
  11. arch/powerpc/kvm/book3s_hv.c +18 −6
  12. arch/powerpc/kvm/book3s_hv_rm_mmu.c +1 −1
  13. arch/powerpc/kvm/book3s_hv_rmhandlers.S +25 −13
  14. arch/powerpc/kvm/book3s_xive.c +51 −9
  15. arch/powerpc/kvm/book3s_xive.h +2 −0
  16. arch/powerpc/kvm/book3s_xive_native.c +18 −5
  17. arch/powerpc/kvm/e500.c +3 −0
  18. arch/powerpc/kvm/emulate.c +1 −0
  19. arch/powerpc/kvm/emulate_loadstore.c +0 −6
  20. arch/powerpc/kvm/powerpc.c +2 −1
  21. arch/powerpc/sysdev/xive/common.c +64 −23
  22. arch/powerpc/sysdev/xive/native.c +7 −0
  23. arch/s390/include/uapi/asm/kvm.h +6 −0
  24. arch/s390/kvm/kvm-s390.c +5 −1
  25. arch/x86/include/asm/kvm_emulate.h +2 −1
  26. arch/x86/include/asm/kvm_host.h +15 −4
  27. arch/x86/include/asm/vmx.h +14 −0
  28. arch/x86/include/uapi/asm/vmx.h +2 −0
  29. arch/x86/kernel/kvm.c +0 −12
  30. arch/x86/kvm/cpuid.c +19 −8
  31. arch/x86/kvm/emulate.c +25 −2
  32. arch/x86/kvm/lapic.c +11 −9
  33. arch/x86/kvm/mmu.c +39 −22
  34. arch/x86/kvm/mmu.h +1 −1
  35. arch/x86/kvm/svm.c +93 −105
  36. arch/x86/kvm/trace.h +54 −20
  37. arch/x86/kvm/vmx/nested.c +162 −143
  38. arch/x86/kvm/vmx/vmenter.S +2 −2
  39. arch/x86/kvm/vmx/vmx.c +48 −46
  40. arch/x86/kvm/vmx/vmx.h +1 −1
  41. arch/x86/kvm/x86.c +128 −69
  42. arch/x86/kvm/x86.h +1 −1
  43. include/kvm/arm_vgic.h +3 −1
  44. include/uapi/linux/kvm.h +3 −0
  45. tools/testing/selftests/kvm/Makefile +6 −4
  46. tools/testing/selftests/kvm/dirty_log_test.c +53 −8
  47. tools/testing/selftests/kvm/include/kvm_util.h +1 −7
  48. tools/testing/selftests/kvm/lib/aarch64/ucall.c +112 −0
  49. tools/testing/selftests/kvm/lib/s390x/ucall.c +56 −0
  50. tools/testing/selftests/kvm/lib/ucall.c +0 −157
  51. tools/testing/selftests/kvm/lib/x86_64/ucall.c +56 −0
  52. tools/testing/selftests/kvm/s390x/memop.c +166 −0
  53. tools/testing/selftests/kvm/s390x/sync_regs_test.c +34 −2
  54. virt/kvm/arm/arm.c +2 −0
  55. virt/kvm/arm/vgic/vgic-init.c +6 −2
  56. virt/kvm/arm/vgic/vgic-irqfd.c +30 −6
  57. virt/kvm/arm/vgic/vgic-its.c +207 −0
  58. virt/kvm/arm/vgic/vgic-mmio-v3.c +27 −58
  59. virt/kvm/arm/vgic/vgic-v2.c +4 −3
  60. virt/kvm/arm/vgic/vgic-v3.c +4 −3
  61. virt/kvm/arm/vgic/vgic.c +17 −9
  62. virt/kvm/arm/vgic/vgic.h +5 −0
  63. virt/kvm/kvm_main.c +3 −4
Documentation/virt/kvm/api.txt+24−9
@@ -586,7 +586,7 @@ Capability: basic · 585 unmodified lines
586586 Architectures: x86
587587 Type: vcpu ioctl
588588 Parameters: struct kvm_msrs (in)
589−Returns: 0 on success, -1 on error
589+Returns: number of msrs successfully set (see below), -1 on error
590590
591591 Writes model-specific registers to the vcpu. See KVM_GET_MSRS for the
592592 data structures.
@@ -595,6 +595,11 @@ Application code should set the 'nmsrs' member (which indicates the · 2 unmodified lines
595595 size of the entries array), and the 'index' and 'data' members of each
596596 array entry.
597597
598+It tries to set the MSRs in array entries[] one by one. If setting an MSR
599+fails, e.g., due to setting reserved bits, the MSR isn't supported/emulated
600+by KVM, etc..., it stops processing the MSR list and returns the number of
601+MSRs that have been set successfully.
602+
598603
599604 4.20 KVM_SET_CPUID
600605
@@ -753,8 +758,8 @@ in-kernel irqchip (GIC), and for in-kernel irqchip can tell the GIC to · 152 unmodified lines
753758 use PPIs designated for specific cpus. The irq field is interpreted
754759 like this:
755760
756−  bits: | 31 ... 24 | 23 ... 16 | 15 ... 0 |
757− field: | irq_type | vcpu_index | irq_id |
761+  bits: | 31 ... 28 | 27 ... 24 | 23 ... 16 | 15 ... 0 |
762+ field: | vcpu2_index | irq_type | vcpu_index | irq_id |
758763
759764 The irq_type field has the following values:
760765 - irq_type[0]: out-of-kernel GIC: irq_id 0 is IRQ, irq_id 1 is FIQ
@@ -766,6 +771,14 @@ The irq_type field has the following values: · 5 unmodified lines
766771
767772 In both cases, level is used to assert/deassert the line.
768773
774+When KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 is supported, the target vcpu is
775+identified as (256 * vcpu2_index + vcpu_index). Otherwise, vcpu2_index
776+must be zero.
777+
778+Note that on arm/arm64, the KVM_CAP_IRQCHIP capability only conditions
779+injection of interrupts for the in-kernel irqchip. KVM_IRQ_LINE can always
780+be used for a userspace interrupt controller.
781+
769782 struct kvm_irq_level {
770783 union {
771784 __u32 irq; /* GSI */
@@ -3079,12 +3092,14 @@ This exception is also raised directly at the corresponding VCPU if the · 2307 unmodified lines
30793092 flag KVM_S390_MEMOP_F_INJECT_EXCEPTION is set in the "flags" field.
30803093
30813094 The start address of the memory region has to be specified in the "gaddr"
3082−field, and the length of the region in the "size" field. "buf" is the buffer
3083−supplied by the userspace application where the read data should be written
3084−to for KVM_S390_MEMOP_LOGICAL_READ, or where the data that should be written
3085−is stored for a KVM_S390_MEMOP_LOGICAL_WRITE. "buf" is unused and can be NULL
3086−when KVM_S390_MEMOP_F_CHECK_ONLY is specified. "ar" designates the access
3087−register number to be used.
3095+field, and the length of the region in the "size" field (which must not
3096+be 0). The maximum value for "size" can be obtained by checking the
3097+KVM_CAP_S390_MEM_OP capability. "buf" is the buffer supplied by the
3098+userspace application where the read data should be written to for
3099+KVM_S390_MEMOP_LOGICAL_READ, or where the data that should be written is
3100+stored for a KVM_S390_MEMOP_LOGICAL_WRITE. When KVM_S390_MEMOP_F_CHECK_ONLY
3101+is specified, "buf" is unused and can be NULL. "ar" designates the access
3102+register number to be used; the valid range is 0..15.
30883103
30893104 The "reserved" field is meant for future extensions. It is not used by
30903105 KVM with the currently defined set of flags.
Documentation/virt/kvm/mmu.txt+2−2
@@ -294,7 +294,7 @@ Handling a page fault is performed as follows: · 293 unmodified lines
294294 - walk shadow page table
295295 - check for valid generation number in the spte (see "Fast invalidation of
296296 MMIO sptes" below)
297− - cache the information to vcpu->arch.mmio_gva, vcpu->arch.access and
297+ - cache the information to vcpu->arch.mmio_gva, vcpu->arch.mmio_access and
298298 vcpu->arch.mmio_gfn, and call the emulator
299299 - If both P bit and R/W bit of error code are set, this could possibly
300300 be handled as a "fast page fault" (fixed without taking the MMU lock). See
@@ -304,7 +304,7 @@ Handling a page fault is performed as follows: · 3 unmodified lines
304304 - if permissions are insufficient, reflect the fault back to the guest
305305 - determine the host page
306306 - if this is an mmio request, there is no host page; cache the info to
307− vcpu->arch.mmio_gva, vcpu->arch.access and vcpu->arch.mmio_gfn
307+ vcpu->arch.mmio_gva, vcpu->arch.mmio_access and vcpu->arch.mmio_gfn
308308 - walk the shadow page table to find the spte for the translation,
309309 instantiating missing intermediate page tables as necessary
310310 - If this is an mmio request, cache the mmio info to the spte and set some
arch/arm/include/uapi/asm/kvm.h+3−1
@@ -266,8 +266,10 @@ struct kvm_vcpu_events { · 265 unmodified lines
266266 #define KVM_DEV_ARM_ITS_CTRL_RESET 4
267267
268268 /* KVM_IRQ_LINE irq field index values */
269+#define KVM_ARM_IRQ_VCPU2_SHIFT 28
270+#define KVM_ARM_IRQ_VCPU2_MASK 0xf
269271 #define KVM_ARM_IRQ_TYPE_SHIFT 24
270−#define KVM_ARM_IRQ_TYPE_MASK 0xff
272+#define KVM_ARM_IRQ_TYPE_MASK 0xf
271273 #define KVM_ARM_IRQ_VCPU_SHIFT 16
272274 #define KVM_ARM_IRQ_VCPU_MASK 0xff
273275 #define KVM_ARM_IRQ_NUM_SHIFT 0
arch/arm64/include/asm/pgtable-prot.h+1−1
@@ -77,7 +77,7 @@ · 76 unmodified lines
7777 })
7878
7979 #define PAGE_S2 __pgprot(_PROT_DEFAULT | PAGE_S2_MEMATTR(NORMAL) | PTE_S2_RDONLY | PAGE_S2_XN)
80−#define PAGE_S2_DEVICE __pgprot(_PROT_DEFAULT | PAGE_S2_MEMATTR(DEVICE_nGnRE) | PTE_S2_RDONLY | PAGE_S2_XN)
80+#define PAGE_S2_DEVICE __pgprot(_PROT_DEFAULT | PAGE_S2_MEMATTR(DEVICE_nGnRE) | PTE_S2_RDONLY | PTE_S2_XN)
8181
8282 #define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_PROT_NONE | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN)
8383 #define PAGE_SHARED __pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN | PTE_UXN | PTE_WRITE)
arch/arm64/include/uapi/asm/kvm.h+3−1
@@ -325,8 +325,10 @@ struct kvm_vcpu_events { · 324 unmodified lines
325325 #define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1
326326
327327 /* KVM_IRQ_LINE irq field index values */
328+#define KVM_ARM_IRQ_VCPU2_SHIFT 28
329+#define KVM_ARM_IRQ_VCPU2_MASK 0xf
328330 #define KVM_ARM_IRQ_TYPE_SHIFT 24
329−#define KVM_ARM_IRQ_TYPE_MASK 0xff
331+#define KVM_ARM_IRQ_TYPE_MASK 0xf
330332 #define KVM_ARM_IRQ_VCPU_SHIFT 16
331333 #define KVM_ARM_IRQ_VCPU_MASK 0xff
332334 #define KVM_ARM_IRQ_NUM_SHIFT 0
arch/arm64/kvm/hyp/tlb.c+13−1
@@ -193,6 +193,18 @@ void __hyp_text __kvm_flush_vm_context(void) · 192 unmodified lines
193193 {
194194 dsb(ishst);
195195 __tlbi(alle1is);
196− asm volatile("ic ialluis" : : );
196+
197+ /*
198+ * VIPT and PIPT caches are not affected by VMID, so no maintenance
199+ * is necessary across a VMID rollover.
200+ *
201+ * VPIPT caches constrain lookup and maintenance to the active VMID,
202+ * so we need to invalidate lines with a stale VMID to avoid an ABA
203+ * race after multiple rollovers.
204+ *
205+ */
206+ if (icache_is_vpipt())
207+ asm volatile("ic ialluis");
208+
197209 dsb(ish);
198210 }
arch/powerpc/include/asm/kvm_host.h+18−4
@@ -232,11 +232,25 @@ struct revmap_entry { · 231 unmodified lines
232232 };
233233
234234 /*
235− * We use the top bit of each memslot->arch.rmap entry as a lock bit,
236− * and bit 32 as a present flag. The bottom 32 bits are the
237− * index in the guest HPT of a HPTE that points to the page.
235+ * The rmap array of size number of guest pages is allocated for each memslot.
236+ * This array is used to store usage specific information about the guest page.
237+ * Below are the encodings of the various possible usage types.
238238 */
239−#define KVMPPC_RMAP_LOCK_BIT 63
239+/* Free bits which can be used to define a new usage */
240+#define KVMPPC_RMAP_TYPE_MASK 0xff00000000000000
241+#define KVMPPC_RMAP_NESTED 0xc000000000000000 /* Nested rmap array */
242+#define KVMPPC_RMAP_HPT 0x0100000000000000 /* HPT guest */
243+
244+/*
245+ * rmap usage definition for a hash page table (hpt) guest:
246+ * 0x0000080000000000 Lock bit
247+ * 0x0000018000000000 RC bits
248+ * 0x0000000100000000 Present bit
249+ * 0x00000000ffffffff HPT index bits
250+ * The bottom 32 bits are the index in the guest HPT of a HPTE that points to
251+ * the page.
252+ */
253+#define KVMPPC_RMAP_LOCK_BIT 43
240254 #define KVMPPC_RMAP_RC_SHIFT 32
241255 #define KVMPPC_RMAP_REFERENCED (HPTE_R_R << KVMPPC_RMAP_RC_SHIFT)
242256 #define KVMPPC_RMAP_PRESENT 0x100000000ul
arch/powerpc/include/asm/kvm_ppc.h+1−0
@@ -598,6 +598,7 @@ extern int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu, · 597 unmodified lines
598598 union kvmppc_one_reg *val);
599599 extern int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu,
600600 union kvmppc_one_reg *val);
601+extern bool kvmppc_xive_native_supported(void);
601602
602603 #else
603604 static inline int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
arch/powerpc/include/asm/xive.h+9−0
@@ -46,7 +46,15 @@ struct xive_irq_data { · 45 unmodified lines
4646
4747 /* Setup/used by frontend */
4848 int target;
49+ /*
50+ * saved_p means that there is a queue entry for this interrupt
51+ * in some CPU's queue (not including guest vcpu queues), even
52+ * if P is not set in the source ESB.
53+ * stale_p means that there is no queue entry for this interrupt
54+ * in some CPU's queue, even if P is set in the source ESB.
55+ */
4956 bool saved_p;
57+ bool stale_p;
5058 };
5159 #define XIVE_IRQ_FLAG_STORE_EOI 0x01
5260 #define XIVE_IRQ_FLAG_LSI 0x02
@@ -127,6 +135,7 @@ extern int xive_native_get_queue_state(u32 vp_id, uint32_t prio, u32 *qtoggle, · 74 unmodified lines
127135 extern int xive_native_set_queue_state(u32 vp_id, uint32_t prio, u32 qtoggle,
128136 u32 qindex);
129137 extern int xive_native_get_vp_state(u32 vp_id, u64 *out_state);
138+extern bool xive_native_has_queue_state_support(void);
130139
131140 #else
132141
arch/powerpc/kvm/book3s.c+5−3
@@ -1083,9 +1083,11 @@ static int kvmppc_book3s_init(void) · 1082 unmodified lines
10831083 if (xics_on_xive()) {
10841084 kvmppc_xive_init_module();
10851085 kvm_register_device_ops(&kvm_xive_ops, KVM_DEV_TYPE_XICS);
1086− kvmppc_xive_native_init_module();
1087− kvm_register_device_ops(&kvm_xive_native_ops,
1088− KVM_DEV_TYPE_XIVE);
1086+ if (kvmppc_xive_native_supported()) {
1087+ kvmppc_xive_native_init_module();
1088+ kvm_register_device_ops(&kvm_xive_native_ops,
1089+ KVM_DEV_TYPE_XIVE);
1090+ }
10891091 } else
10901092 #endif
10911093 kvm_register_device_ops(&kvm_xics_ops, KVM_DEV_TYPE_XICS);
arch/powerpc/kvm/book3s_hv.c+18−6
@@ -1678,7 +1678,14 @@ static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, · 1677 unmodified lines
16781678 *val = get_reg_val(id, vcpu->arch.pspb);
16791679 break;
16801680 case KVM_REG_PPC_DPDES:
1681− *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1681+ /*
1682+ * On POWER9, where we are emulating msgsndp etc.,
1683+ * we return 1 bit for each vcpu, which can come from
1684+ * either vcore->dpdes or doorbell_request.
1685+ * On POWER8, doorbell_request is 0.
1686+ */
1687+ *val = get_reg_val(id, vcpu->arch.vcore->dpdes |
1688+ vcpu->arch.doorbell_request);
16821689 break;
16831690 case KVM_REG_PPC_VTB:
16841691 *val = get_reg_val(id, vcpu->arch.vcore->vtb);
@@ -2860,7 +2867,7 @@ static void collect_piggybacks(struct core_info *cip, int target_threads) · 1175 unmodified lines
28602867 if (!spin_trylock(&pvc->lock))
28612868 continue;
28622869 prepare_threads(pvc);
2863− if (!pvc->n_runnable) {
2870+ if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) {
28642871 list_del_init(&pvc->preempt_list);
28652872 if (pvc->runner == NULL) {
28662873 pvc->vcore_state = VCORE_INACTIVE;
@@ -2881,15 +2888,20 @@ static void collect_piggybacks(struct core_info *cip, int target_threads) · 14 unmodified lines
28812888 spin_unlock(&lp->lock);
28822889 }
28832890
2884−static bool recheck_signals(struct core_info *cip)
2891+static bool recheck_signals_and_mmu(struct core_info *cip)
28852892 {
28862893 int sub, i;
28872894 struct kvm_vcpu *vcpu;
2895+ struct kvmppc_vcore *vc;
28882896
2889− for (sub = 0; sub < cip->n_subcores; ++sub)
2890− for_each_runnable_thread(i, vcpu, cip->vc[sub])
2897+ for (sub = 0; sub < cip->n_subcores; ++sub) {
2898+ vc = cip->vc[sub];
2899+ if (!vc->kvm->arch.mmu_ready)
2900+ return true;
2901+ for_each_runnable_thread(i, vcpu, vc)
28912902 if (signal_pending(vcpu->arch.run_task))
28922903 return true;
2904+ }
28932905 return false;
28942906 }
28952907
@@ -3119,7 +3131,7 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) · 223 unmodified lines
31193131 local_irq_disable();
31203132 hard_irq_disable();
31213133 if (lazy_irq_pending() || need_resched() ||
3122− recheck_signals(&core_info) || !vc->kvm->arch.mmu_ready) {
3134+ recheck_signals_and_mmu(&core_info)) {
31233135 local_irq_enable();
31243136 vc->vcore_state = VCORE_INACTIVE;
31253137 /* Unlock all except the primary vcore */
arch/powerpc/kvm/book3s_hv_rm_mmu.c+1−1
@@ -99,7 +99,7 @@ void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev, · 98 unmodified lines
9999 } else {
100100 rev->forw = rev->back = pte_index;
101101 *rmap = (*rmap & ~KVMPPC_RMAP_INDEX) |
102− pte_index | KVMPPC_RMAP_PRESENT;
102+ pte_index | KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_HPT;
103103 }
104104 unlock_rmap(rmap);
105105 }
arch/powerpc/kvm/book3s_hv_rmhandlers.S+25−13
@@ -942,6 +942,8 @@ ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300) · 941 unmodified lines
942942 ld r11, VCPU_XIVE_SAVED_STATE(r4)
943943 li r9, TM_QW1_OS
944944 lwz r8, VCPU_XIVE_CAM_WORD(r4)
945+ cmpwi r8, 0
946+ beq no_xive
945947 li r7, TM_QW1_OS + TM_WORD2
946948 mfmsr r0
947949 andi. r0, r0, MSR_DR /* in real mode? */
@@ -2831,29 +2833,39 @@ kvm_cede_prodded: · 1883 unmodified lines
28312833 kvm_cede_exit:
28322834 ld r9, HSTATE_KVM_VCPU(r13)
28332835 #ifdef CONFIG_KVM_XICS
2834− /* Abort if we still have a pending escalation */
2836+ /* are we using XIVE with single escalation? */
2837+ ld r10, VCPU_XIVE_ESC_VADDR(r9)
2838+ cmpdi r10, 0
2839+ beq 3f
2840+ li r6, XIVE_ESB_SET_PQ_00
2841+ /*
2842+ * If we still have a pending escalation, abort the cede,
2843+ * and we must set PQ to 10 rather than 00 so that we don't
2844+ * potentially end up with two entries for the escalation
2845+ * interrupt in the XIVE interrupt queue. In that case
2846+ * we also don't want to set xive_esc_on to 1 here in
2847+ * case we race with xive_esc_irq().
2848+ */
28352849 lbz r5, VCPU_XIVE_ESC_ON(r9)
28362850 cmpwi r5, 0
2837− beq 1f
2851+ beq 4f
28382852 li r0, 0
28392853 stb r0, VCPU_CEDED(r9)
2840−1: /* Enable XIVE escalation */
2841− li r5, XIVE_ESB_SET_PQ_00
2854+ li r6, XIVE_ESB_SET_PQ_10
2855+ b 5f
2856+4: li r0, 1
2857+ stb r0, VCPU_XIVE_ESC_ON(r9)
2858+ /* make sure store to xive_esc_on is seen before xive_esc_irq runs */
2859+ sync
2860+5: /* Enable XIVE escalation */
28422861 mfmsr r0
28432862 andi. r0, r0, MSR_DR /* in real mode? */
28442863 beq 1f
2845− ld r10, VCPU_XIVE_ESC_VADDR(r9)
2846− cmpdi r10, 0
2847− beq 3f
2848− ldx r0, r10, r5
2864+ ldx r0, r10, r6
28492865 b 2f
28502866 1: ld r10, VCPU_XIVE_ESC_RADDR(r9)
2851− cmpdi r10, 0
2852− beq 3f
2853− ldcix r0, r10, r5
2867+ ldcix r0, r10, r6
28542868 2: sync
2855− li r0, 1
2856− stb r0, VCPU_XIVE_ESC_ON(r9)
28572869 #endif /* CONFIG_KVM_XICS */
28582870 3: b guest_exit_cont
28592871
arch/powerpc/kvm/book3s_xive.c+51−9
@@ -67,8 +67,14 @@ void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu) · 66 unmodified lines
6767 void __iomem *tima = local_paca->kvm_hstate.xive_tima_virt;
6868 u64 pq;
6969
70− if (!tima)
70+ /*
71+ * Nothing to do if the platform doesn't have a XIVE
72+ * or this vCPU doesn't have its own XIVE context
73+ * (e.g. because it's not using an in-kernel interrupt controller).
74+ */
75+ if (!tima || !vcpu->arch.xive_cam_word)
7176 return;
77+
7278 eieio();
7379 __raw_writeq(vcpu->arch.xive_saved_state.w01, tima + TM_QW1_OS);
7480 __raw_writel(vcpu->arch.xive_cam_word, tima + TM_QW1_OS + TM_WORD2);
@@ -160,6 +166,9 @@ static irqreturn_t xive_esc_irq(int irq, void *data) · 85 unmodified lines
160166 */
161167 vcpu->arch.xive_esc_on = false;
162168
169+ /* This orders xive_esc_on = false vs. subsequent stale_p = true */
170+ smp_wmb(); /* goes with smp_mb() in cleanup_single_escalation */
171+
163172 return IRQ_HANDLED;
164173 }
165174
@@ -1113,6 +1122,31 @@ void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu) · 947 unmodified lines
11131122 vcpu->arch.xive_esc_raddr = 0;
11141123 }
11151124
1125+/*
1126+ * In single escalation mode, the escalation interrupt is marked so
1127+ * that EOI doesn't re-enable it, but just sets the stale_p flag to
1128+ * indicate that the P bit has already been dealt with. However, the
1129+ * assembly code that enters the guest sets PQ to 00 without clearing
1130+ * stale_p (because it has no easy way to address it). Hence we have
1131+ * to adjust stale_p before shutting down the interrupt.
1132+ */
1133+void xive_cleanup_single_escalation(struct kvm_vcpu *vcpu,
1134+ struct kvmppc_xive_vcpu *xc, int irq)
1135+{
1136+ struct irq_data *d = irq_get_irq_data(irq);
1137+ struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
1138+
1139+ /*
1140+ * This slightly odd sequence gives the right result
1141+ * (i.e. stale_p set if xive_esc_on is false) even if
1142+ * we race with xive_esc_irq() and xive_irq_eoi().
1143+ */
1144+ xd->stale_p = false;
1145+ smp_mb(); /* paired with smb_wmb in xive_esc_irq */
1146+ if (!vcpu->arch.xive_esc_on)
1147+ xd->stale_p = true;
1148+}
1149+
11161150 void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu)
11171151 {
11181152 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
@@ -1134,20 +1168,28 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) · 15 unmodified lines
11341168 /* Mask the VP IPI */
11351169 xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_01);
11361170
1137− /* Disable the VP */
1138− xive_native_disable_vp(xc->vp_id);
1139−
1140− /* Free the queues & associated interrupts */
1171+ /* Free escalations */
11411172 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
1142− struct xive_q *q = &xc->queues[i];
1143−
1144− /* Free the escalation irq */
11451173 if (xc->esc_virq[i]) {
1174+ if (xc->xive->single_escalation)
1175+ xive_cleanup_single_escalation(vcpu, xc,
1176+ xc->esc_virq[i]);
11461177 free_irq(xc->esc_virq[i], vcpu);
11471178 irq_dispose_mapping(xc->esc_virq[i]);
11481179 kfree(xc->esc_virq_names[i]);
11491180 }
1150− /* Free the queue */
1181+ }
1182+
1183+ /* Disable the VP */
1184+ xive_native_disable_vp(xc->vp_id);
1185+
1186+ /* Clear the cam word so guest entry won't try to push context */
1187+ vcpu->arch.xive_cam_word = 0;
1188+
1189+ /* Free the queues */
1190+ for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
1191+ struct xive_q *q = &xc->queues[i];
1192+
11511193 xive_native_disable_queue(xc->vp_id, q, i);
11521194 if (q->qpage) {
11531195 free_pages((unsigned long)q->qpage,
arch/powerpc/kvm/book3s_xive.h+2−0
@@ -282,6 +282,8 @@ int kvmppc_xive_select_target(struct kvm *kvm, u32 *server, u8 prio); · 281 unmodified lines
282282 int kvmppc_xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio,
283283 bool single_escalation);
284284 struct kvmppc_xive *kvmppc_xive_get_device(struct kvm *kvm, u32 type);
285+void xive_cleanup_single_escalation(struct kvm_vcpu *vcpu,
286+ struct kvmppc_xive_vcpu *xc, int irq);
285287
286288 #endif /* CONFIG_KVM_XICS */
287289 #endif /* _KVM_PPC_BOOK3S_XICS_H */
arch/powerpc/kvm/book3s_xive_native.c+18−5
@@ -67,20 +67,28 @@ void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu) · 66 unmodified lines
6767 xc->valid = false;
6868 kvmppc_xive_disable_vcpu_interrupts(vcpu);
6969
70− /* Disable the VP */
71− xive_native_disable_vp(xc->vp_id);
72−
73− /* Free the queues & associated interrupts */
70+ /* Free escalations */
7471 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
7572 /* Free the escalation irq */
7673 if (xc->esc_virq[i]) {
74+ if (xc->xive->single_escalation)
75+ xive_cleanup_single_escalation(vcpu, xc,
76+ xc->esc_virq[i]);
7777 free_irq(xc->esc_virq[i], vcpu);
7878 irq_dispose_mapping(xc->esc_virq[i]);
7979 kfree(xc->esc_virq_names[i]);
8080 xc->esc_virq[i] = 0;
8181 }
82+ }
83+
84+ /* Disable the VP */
85+ xive_native_disable_vp(xc->vp_id);
86+
87+ /* Clear the cam word so guest entry won't try to push context */
88+ vcpu->arch.xive_cam_word = 0;
8289
83− /* Free the queue */
90+ /* Free the queues */
91+ for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
8492 kvmppc_xive_native_cleanup_queue(vcpu, i);
8593 }
8694
@@ -1171,6 +1179,11 @@ int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu, union kvmppc_one_reg *val) · 1084 unmodified lines
11711179 return 0;
11721180 }
11731181
1182+bool kvmppc_xive_native_supported(void)
1183+{
1184+ return xive_native_has_queue_state_support();
1185+}
1186+
11741187 static int xive_native_debug_show(struct seq_file *m, void *private)
11751188 {
11761189 struct kvmppc_xive *xive = m->private;
arch/powerpc/kvm/e500.c+3−0
@@ -440,6 +440,9 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_e500(struct kvm *kvm, · 439 unmodified lines
440440 struct kvm_vcpu *vcpu;
441441 int err;
442442
443+ BUILD_BUG_ON_MSG(offsetof(struct kvmppc_vcpu_e500, vcpu) != 0,
444+ "struct kvm_vcpu must be at offset 0 for arch usercopy region");
445+
443446 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
444447 if (!vcpu_e500) {
445448 err = -ENOMEM;
arch/powerpc/kvm/emulate.c+1−0
@@ -271,6 +271,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) · 270 unmodified lines
271271 */
272272 if (inst == KVMPPC_INST_SW_BREAKPOINT) {
273273 run->exit_reason = KVM_EXIT_DEBUG;
274+ run->debug.arch.status = 0;
274275 run->debug.arch.address = kvmppc_get_pc(vcpu);
275276 emulated = EMULATE_EXIT_USER;
276277 advance = 0;
arch/powerpc/kvm/emulate_loadstore.c+0−6
@@ -89,12 +89,6 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu) · 88 unmodified lines
8989 rs = get_rs(inst);
9090 rt = get_rt(inst);
9191
92− /*
93− * if mmio_vsx_tx_sx_enabled == 0, copy data between
94− * VSR[0..31] and memory
95− * if mmio_vsx_tx_sx_enabled == 1, copy data between
96− * VSR[32..63] and memory
97− */
9892 vcpu->arch.mmio_vsx_copy_nums = 0;
9993 vcpu->arch.mmio_vsx_offset = 0;
10094 vcpu->arch.mmio_copy_type = KVMPPC_VSX_COPY_NONE;
arch/powerpc/kvm/powerpc.c+2−1
@@ -561,7 +561,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · 560 unmodified lines
561561 * a POWER9 processor) and the PowerNV platform, as
562562 * nested is not yet supported.
563563 */
564− r = xive_enabled() && !!cpu_has_feature(CPU_FTR_HVMODE);
564+ r = xive_enabled() && !!cpu_has_feature(CPU_FTR_HVMODE) &&
565+ kvmppc_xive_native_supported();
565566 break;
566567 #endif
567568
arch/powerpc/sysdev/xive/common.c+64−23
@@ -135,7 +135,7 @@ static u32 xive_read_eq(struct xive_q *q, bool just_peek) · 134 unmodified lines
135135 static u32 xive_scan_interrupts(struct xive_cpu *xc, bool just_peek)
136136 {
137137 u32 irq = 0;
138− u8 prio;
138+ u8 prio = 0;
139139
140140 /* Find highest pending priority */
141141 while (xc->pending_prio != 0) {
@@ -148,8 +148,19 @@ static u32 xive_scan_interrupts(struct xive_cpu *xc, bool just_peek) · 6 unmodified lines
148148 irq = xive_read_eq(&xc->queue[prio], just_peek);
149149
150150 /* Found something ? That's it */
151− if (irq)
152− break;
151+ if (irq) {
152+ if (just_peek || irq_to_desc(irq))
153+ break;
154+ /*
155+ * We should never get here; if we do then we must
156+ * have failed to synchronize the interrupt properly
157+ * when shutting it down.
158+ */
159+ pr_crit("xive: got interrupt %d without descriptor, dropping\n",
160+ irq);
161+ WARN_ON(1);
162+ continue;
163+ }
153164
154165 /* Clear pending bits */
155166 xc->pending_prio &= ~(1 << prio);
@@ -307,6 +318,7 @@ static void xive_do_queue_eoi(struct xive_cpu *xc) · 151 unmodified lines
307318 */
308319 static void xive_do_source_eoi(u32 hw_irq, struct xive_irq_data *xd)
309320 {
321+ xd->stale_p = false;
310322 /* If the XIVE supports the new "store EOI facility, use it */
311323 if (xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
312324 xive_esb_write(xd, XIVE_ESB_STORE_EOI, 0);
@@ -350,7 +362,7 @@ static void xive_do_source_eoi(u32 hw_irq, struct xive_irq_data *xd) · 37 unmodified lines
350362 }
351363 }
352364
353−/* irq_chip eoi callback */
365+/* irq_chip eoi callback, called with irq descriptor lock held */
354366 static void xive_irq_eoi(struct irq_data *d)
355367 {
356368 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
@@ -366,6 +378,8 @@ static void xive_irq_eoi(struct irq_data *d) · 9 unmodified lines
366378 if (!irqd_irq_disabled(d) && !irqd_is_forwarded_to_vcpu(d) &&
367379 !(xd->flags & XIVE_IRQ_NO_EOI))
368380 xive_do_source_eoi(irqd_to_hwirq(d), xd);
381+ else
382+ xd->stale_p = true;
369383
370384 /*
371385 * Clear saved_p to indicate that it's no longer occupying
@@ -397,11 +411,16 @@ static void xive_do_source_set_mask(struct xive_irq_data *xd, · 25 unmodified lines
397411 */
398412 if (mask) {
399413 val = xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
400− xd->saved_p = !!(val & XIVE_ESB_VAL_P);
401− } else if (xd->saved_p)
414+ if (!xd->stale_p && !!(val & XIVE_ESB_VAL_P))
415+ xd->saved_p = true;
416+ xd->stale_p = false;
417+ } else if (xd->saved_p) {
402418 xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
403− else
419+ xd->saved_p = false;
420+ } else {
404421 xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
422+ xd->stale_p = false;
423+ }
405424 }
406425
407426 /*
@@ -541,6 +560,8 @@ static unsigned int xive_irq_startup(struct irq_data *d) · 133 unmodified lines
541560 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
542561 int target, rc;
543562
563+ xd->saved_p = false;
564+ xd->stale_p = false;
544565 pr_devel("xive_irq_startup: irq %d [0x%x] data @%p\n",
545566 d->irq, hw_irq, d);
546567
@@ -587,6 +608,7 @@ static unsigned int xive_irq_startup(struct irq_data *d) · 40 unmodified lines
587608 return 0;
588609 }
589610
611+/* called with irq descriptor lock held */
590612 static void xive_irq_shutdown(struct irq_data *d)
591613 {
592614 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
@@ -601,16 +623,6 @@ static void xive_irq_shutdown(struct irq_data *d) · 8 unmodified lines
601623 /* Mask the interrupt at the source */
602624 xive_do_source_set_mask(xd, true);
603625
604− /*
605− * The above may have set saved_p. We clear it otherwise it
606− * will prevent re-enabling later on. It is ok to forget the
607− * fact that the interrupt might be in a queue because we are
608− * accounting that already in xive_dec_target_count() and will
609− * be re-routing it to a new queue with proper accounting when
610− * it's started up again
611− */
612− xd->saved_p = false;
613−
614626 /*
615627 * Mask the interrupt in HW in the IVT/EAS and set the number
616628 * to be the "bad" IRQ number
@@ -797,6 +809,10 @@ static int xive_irq_retrigger(struct irq_data *d) · 180 unmodified lines
797809 return 1;
798810 }
799811
812+/*
813+ * Caller holds the irq descriptor lock, so this won't be called
814+ * concurrently with xive_get_irqchip_state on the same interrupt.
815+ */
800816 static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state)
801817 {
802818 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
@@ -820,6 +836,10 @@ static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state) · 17 unmodified lines
820836
821837 /* Set it to PQ=10 state to prevent further sends */
822838 pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
839+ if (!xd->stale_p) {
840+ xd->saved_p = !!(pq & XIVE_ESB_VAL_P);
841+ xd->stale_p = !xd->saved_p;
842+ }
823843
824844 /* No target ? nothing to do */
825845 if (xd->target == XIVE_INVALID_TARGET) {
@@ -827,7 +847,7 @@ static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state) · 1 unmodified line
827847 * An untargetted interrupt should have been
828848 * also masked at the source
829849 */
830− WARN_ON(pq & 2);
850+ WARN_ON(xd->saved_p);
831851
832852 return 0;
833853 }
@@ -847,9 +867,8 @@ static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state) · 13 unmodified lines
847867 * This saved_p is cleared by the host EOI, when we know
848868 * for sure the queue slot is no longer in use.
849869 */
850− if (pq & 2) {
851− pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
852− xd->saved_p = true;
870+ if (xd->saved_p) {
871+ xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
853872
854873 /*
855874 * Sync the XIVE source HW to ensure the interrupt
@@ -862,8 +881,7 @@ static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state) · 6 unmodified lines
862881 */
863882 if (xive_ops->sync_source)
864883 xive_ops->sync_source(hw_irq);
865− } else
866− xd->saved_p = false;
884+ }
867885 } else {
868886 irqd_clr_forwarded_to_vcpu(d);
869887
@@ -914,6 +932,23 @@ static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state) · 44 unmodified lines
914932 return 0;
915933 }
916934
935+/* Called with irq descriptor lock held. */
936+static int xive_get_irqchip_state(struct irq_data *data,
937+ enum irqchip_irq_state which, bool *state)
938+{
939+ struct xive_irq_data *xd = irq_data_get_irq_handler_data(data);
940+
941+ switch (which) {
942+ case IRQCHIP_STATE_ACTIVE:
943+ *state = !xd->stale_p &&
944+ (xd->saved_p ||
945+ !!(xive_esb_read(xd, XIVE_ESB_GET) & XIVE_ESB_VAL_P));
946+ return 0;
947+ default:
948+ return -EINVAL;
949+ }
950+}
951+
917952 static struct irq_chip xive_irq_chip = {
918953 .name = "XIVE-IRQ",
919954 .irq_startup = xive_irq_startup,
@@ -925,6 +960,7 @@ static struct irq_chip xive_irq_chip = { · 5 unmodified lines
925960 .irq_set_type = xive_irq_set_type,
926961 .irq_retrigger = xive_irq_retrigger,
927962 .irq_set_vcpu_affinity = xive_irq_set_vcpu_affinity,
963+ .irq_get_irqchip_state = xive_get_irqchip_state,
928964 };
929965
930966 bool is_xive_irq(struct irq_chip *chip)
@@ -1337,6 +1373,11 @@ static void xive_flush_cpu_queue(unsigned int cpu, struct xive_cpu *xc) · 406 unmodified lines
13371373 raw_spin_lock(&desc->lock);
13381374 xd = irq_desc_get_handler_data(desc);
13391375
1376+ /*
1377+ * Clear saved_p to indicate that it's no longer pending
1378+ */
1379+ xd->saved_p = false;
1380+
13401381 /*
13411382 * For LSIs, we EOI, this will cause a resend if it's
13421383 * still asserted. Otherwise do an MSI retrigger.
arch/powerpc/sysdev/xive/native.c+7−0
@@ -800,6 +800,13 @@ int xive_native_set_queue_state(u32 vp_id, u32 prio, u32 qtoggle, u32 qindex) · 799 unmodified lines
800800 }
801801 EXPORT_SYMBOL_GPL(xive_native_set_queue_state);
802802
803+bool xive_native_has_queue_state_support(void)
804+{
805+ return opal_check_token(OPAL_XIVE_GET_QUEUE_STATE) &&
806+ opal_check_token(OPAL_XIVE_SET_QUEUE_STATE);
807+}
808+EXPORT_SYMBOL_GPL(xive_native_has_queue_state_support);
809+
803810 int xive_native_get_vp_state(u32 vp_id, u64 *out_state)
804811 {
805812 __be64 state;
arch/s390/include/uapi/asm/kvm.h+6−0
@@ -231,6 +231,12 @@ struct kvm_guest_debug_arch { · 230 unmodified lines
231231 #define KVM_SYNC_GSCB (1UL << 9)
232232 #define KVM_SYNC_BPBC (1UL << 10)
233233 #define KVM_SYNC_ETOKEN (1UL << 11)
234+
235+#define KVM_SYNC_S390_VALID_FIELDS \
236+ (KVM_SYNC_PREFIX | KVM_SYNC_GPRS | KVM_SYNC_ACRS | KVM_SYNC_CRS | \
237+ KVM_SYNC_ARCH0 | KVM_SYNC_PFAULT | KVM_SYNC_VRS | KVM_SYNC_RICCB | \
238+ KVM_SYNC_FPRS | KVM_SYNC_GSCB | KVM_SYNC_BPBC | KVM_SYNC_ETOKEN)
239+
234240 /* length and alignment of the sdnx as a power of two */
235241 #define SDNXC 8
236242 #define SDNXL (1UL << SDNXC)
arch/s390/kvm/kvm-s390.c+5−1
@@ -4000,6 +4000,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) · 3999 unmodified lines
40004000 if (kvm_run->immediate_exit)
40014001 return -EINTR;
40024002
4003+ if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
4004+ kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
4005+ return -EINVAL;
4006+
40034007 vcpu_load(vcpu);
40044008
40054009 if (guestdbg_exit_pending(vcpu)) {
@@ -4257,7 +4261,7 @@ static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, · 251 unmodified lines
42574261 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
42584262 | KVM_S390_MEMOP_F_CHECK_ONLY;
42594263
4260− if (mop->flags & ~supported_flags)
4264+ if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size)
42614265 return -EINVAL;
42624266
42634267 if (mop->size > MEM_OP_MAX_SIZE)
arch/x86/include/asm/kvm_emulate.h+2−1
@@ -229,7 +229,7 @@ struct x86_emulate_ops { · 228 unmodified lines
229229 int (*pre_leave_smm)(struct x86_emulate_ctxt *ctxt,
230230 const char *smstate);
231231 void (*post_leave_smm)(struct x86_emulate_ctxt *ctxt);
232−
232+ int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
233233 };
234234
235235 typedef u32 __attribute__((vector_size(16))) sse128_t;
@@ -429,6 +429,7 @@ enum x86_intercept { · 193 unmodified lines
429429 x86_intercept_ins,
430430 x86_intercept_out,
431431 x86_intercept_outs,
432+ x86_intercept_xsetbv,
432433
433434 nr_x86_intercepts
434435 };
arch/x86/include/asm/kvm_host.h+15−4
@@ -718,7 +718,7 @@ struct kvm_vcpu_arch { · 717 unmodified lines
718718
719719 /* Cache MMIO info */
720720 u64 mmio_gva;
721− unsigned access;
721+ unsigned mmio_access;
722722 gfn_t mmio_gfn;
723723 u64 mmio_gen;
724724
@@ -1072,7 +1072,7 @@ struct kvm_x86_ops { · 347 unmodified lines
10721072
10731073 void (*run)(struct kvm_vcpu *vcpu);
10741074 int (*handle_exit)(struct kvm_vcpu *vcpu);
1075− void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
1075+ int (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
10761076 void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
10771077 u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu);
10781078 void (*patch_hypercall)(struct kvm_vcpu *vcpu,
@@ -1211,6 +1211,8 @@ struct kvm_x86_ops { · 132 unmodified lines
12111211 uint16_t (*nested_get_evmcs_version)(struct kvm_vcpu *vcpu);
12121212
12131213 bool (*need_emulation_on_page_fault)(struct kvm_vcpu *vcpu);
1214+
1215+ bool (*apic_init_signal_blocked)(struct kvm_vcpu *vcpu);
12141216 };
12151217
12161218 struct kvm_arch_async_pf {
@@ -1328,8 +1330,10 @@ int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, · 111 unmodified lines
13281330
13291331 void kvm_enable_efer_bits(u64);
13301332 bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
1331−int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
1332−int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
1333+int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data);
1334+int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data);
1335+int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu);
1336+int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu);
13331337
13341338 struct x86_emulate_ctxt;
13351339
@@ -1583,6 +1587,13 @@ bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq, · 247 unmodified lines
15831587 void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
15841588 struct kvm_lapic_irq *irq);
15851589
1590+static inline bool kvm_irq_is_postable(struct kvm_lapic_irq *irq)
1591+{
1592+ /* We can only post Fixed and LowPrio IRQs */
1593+ return (irq->delivery_mode == dest_Fixed ||
1594+ irq->delivery_mode == dest_LowestPrio);
1595+}
1596+
15861597 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
15871598 {
15881599 if (kvm_x86_ops->vcpu_blocking)
arch/x86/include/asm/vmx.h+14−0
@@ -562,6 +562,20 @@ enum vm_instruction_error_number { · 561 unmodified lines
562562 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID = 28,
563563 };
564564
565+/*
566+ * VM-instruction errors that can be encountered on VM-Enter, used to trace
567+ * nested VM-Enter failures reported by hardware. Errors unique to VM-Enter
568+ * from a SMI Transfer Monitor are not included as things have gone seriously
569+ * sideways if we get one of those...
570+ */
571+#define VMX_VMENTER_INSTRUCTION_ERRORS \
572+ { VMXERR_VMLAUNCH_NONCLEAR_VMCS, "VMLAUNCH_NONCLEAR_VMCS" }, \
573+ { VMXERR_VMRESUME_NONLAUNCHED_VMCS, "VMRESUME_NONLAUNCHED_VMCS" }, \
574+ { VMXERR_VMRESUME_AFTER_VMXOFF, "VMRESUME_AFTER_VMXOFF" }, \
575+ { VMXERR_ENTRY_INVALID_CONTROL_FIELD, "VMENTRY_INVALID_CONTROL_FIELD" }, \
576+ { VMXERR_ENTRY_INVALID_HOST_STATE_FIELD, "VMENTRY_INVALID_HOST_STATE_FIELD" }, \
577+ { VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS, "VMENTRY_EVENTS_BLOCKED_BY_MOV_SS" }
578+
565579 enum vmx_l1d_flush_state {
566580 VMENTER_L1D_FLUSH_AUTO,
567581 VMENTER_L1D_FLUSH_NEVER,
arch/x86/include/uapi/asm/vmx.h+2−0
@@ -31,6 +31,7 @@ · 30 unmodified lines
3131 #define EXIT_REASON_EXCEPTION_NMI 0
3232 #define EXIT_REASON_EXTERNAL_INTERRUPT 1
3333 #define EXIT_REASON_TRIPLE_FAULT 2
34+#define EXIT_REASON_INIT_SIGNAL 3
3435
3536 #define EXIT_REASON_PENDING_INTERRUPT 7
3637 #define EXIT_REASON_NMI_WINDOW 8
@@ -90,6 +91,7 @@ · 53 unmodified lines
9091 { EXIT_REASON_EXCEPTION_NMI, "EXCEPTION_NMI" }, \
9192 { EXIT_REASON_EXTERNAL_INTERRUPT, "EXTERNAL_INTERRUPT" }, \
9293 { EXIT_REASON_TRIPLE_FAULT, "TRIPLE_FAULT" }, \
94+ { EXIT_REASON_INIT_SIGNAL, "INIT_SIGNAL" }, \
9395 { EXIT_REASON_PENDING_INTERRUPT, "PENDING_INTERRUPT" }, \
9496 { EXIT_REASON_NMI_WINDOW, "NMI_WINDOW" }, \
9597 { EXIT_REASON_TASK_SWITCH, "TASK_SWITCH" }, \
arch/x86/kernel/kvm.c+0−12
@@ -502,16 +502,6 @@ static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector) · 501 unmodified lines
502502 __send_ipi_mask(local_mask, vector);
503503 }
504504
505−static void kvm_send_ipi_allbutself(int vector)
506−{
507− kvm_send_ipi_mask_allbutself(cpu_online_mask, vector);
508−}
509−
510−static void kvm_send_ipi_all(int vector)
511−{
512− __send_ipi_mask(cpu_online_mask, vector);
513−}
514−
515505 /*
516506 * Set the IPI entry points
517507 */
@@ -519,8 +509,6 @@ static void kvm_setup_pv_ipi(void) · 1 unmodified line
519509 {
520510 apic->send_IPI_mask = kvm_send_ipi_mask;
521511 apic->send_IPI_mask_allbutself = kvm_send_ipi_mask_allbutself;
522− apic->send_IPI_allbutself = kvm_send_ipi_allbutself;
523− apic->send_IPI_all = kvm_send_ipi_all;
524512 pr_info("KVM setup pv IPIs\n");
525513 }
526514
arch/x86/kvm/cpuid.c+19−8
@@ -392,6 +392,12 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index) · 391 unmodified lines
392392
393393 entry->edx &= kvm_cpuid_7_0_edx_x86_features;
394394 cpuid_mask(&entry->edx, CPUID_7_EDX);
395+ if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
396+ entry->edx |= F(SPEC_CTRL);
397+ if (boot_cpu_has(X86_FEATURE_STIBP))
398+ entry->edx |= F(INTEL_STIBP);
399+ if (boot_cpu_has(X86_FEATURE_SSBD))
400+ entry->edx |= F(SPEC_CTRL_SSBD);
395401 /*
396402 * We emulate ARCH_CAPABILITIES in software even
397403 * if the host doesn't support it.
@@ -729,18 +735,23 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, · 331 unmodified lines
729735 g_phys_as = phys_as;
730736 entry->eax = g_phys_as | (virt_as << 8);
731737 entry->edx = 0;
738+ entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features;
739+ cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
732740 /*
733− * IBRS, IBPB and VIRT_SSBD aren't necessarily present in
734− * hardware cpuid
741+ * AMD has separate bits for each SPEC_CTRL bit.
742+ * arch/x86/kernel/cpu/bugs.c is kind enough to
743+ * record that in cpufeatures so use them.
735744 */
736− if (boot_cpu_has(X86_FEATURE_AMD_IBPB))
745+ if (boot_cpu_has(X86_FEATURE_IBPB))
737746 entry->ebx |= F(AMD_IBPB);
738− if (boot_cpu_has(X86_FEATURE_AMD_IBRS))
747+ if (boot_cpu_has(X86_FEATURE_IBRS))
739748 entry->ebx |= F(AMD_IBRS);
740− if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
741− entry->ebx |= F(VIRT_SSBD);
742− entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features;
743− cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
749+ if (boot_cpu_has(X86_FEATURE_STIBP))
750+ entry->ebx |= F(AMD_STIBP);
751+ if (boot_cpu_has(X86_FEATURE_SSBD))
752+ entry->ebx |= F(AMD_SSBD);
753+ if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
754+ entry->ebx |= F(AMD_SSB_NO);
744755 /*
745756 * The preference is to use SPEC CTRL MSR instead of the
746757 * VIRT_SPEC MSR.
arch/x86/kvm/emulate.c+25−2
@@ -4156,6 +4156,20 @@ out: · 4155 unmodified lines
41564156 return rc;
41574157 }
41584158
4159+static int em_xsetbv(struct x86_emulate_ctxt *ctxt)
4160+{
4161+ u32 eax, ecx, edx;
4162+
4163+ eax = reg_read(ctxt, VCPU_REGS_RAX);
4164+ edx = reg_read(ctxt, VCPU_REGS_RDX);
4165+ ecx = reg_read(ctxt, VCPU_REGS_RCX);
4166+
4167+ if (ctxt->ops->set_xcr(ctxt, ecx, ((u64)edx << 32) | eax))
4168+ return emulate_gp(ctxt, 0);
4169+
4170+ return X86EMUL_CONTINUE;
4171+}
4172+
41594173 static bool valid_cr(int nr)
41604174 {
41614175 switch (nr) {
@@ -4409,6 +4423,12 @@ static const struct opcode group7_rm1[] = { · 247 unmodified lines
44094423 N, N, N, N, N, N,
44104424 };
44114425
4426+static const struct opcode group7_rm2[] = {
4427+ N,
4428+ II(ImplicitOps | Priv, em_xsetbv, xsetbv),
4429+ N, N, N, N, N, N,
4430+};
4431+
44124432 static const struct opcode group7_rm3[] = {
44134433 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
44144434 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
@@ -4498,7 +4518,8 @@ static const struct group_dual group7 = { { · 83 unmodified lines
44984518 }, {
44994519 EXT(0, group7_rm0),
45004520 EXT(0, group7_rm1),
4501− N, EXT(0, group7_rm3),
4521+ EXT(0, group7_rm2),
4522+ EXT(0, group7_rm3),
45024523 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
45034524 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
45044525 EXT(0, group7_rm7),
@@ -5144,7 +5165,7 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · 639 unmodified lines
51445165 else {
51455166 rc = __do_insn_fetch_bytes(ctxt, 1);
51465167 if (rc != X86EMUL_CONTINUE)
5147− return rc;
5168+ goto done;
51485169 }
51495170
51505171 switch (mode) {
@@ -5395,6 +5416,8 @@ done_prefixes: · 244 unmodified lines
53955416 ctxt->memopp->addr.mem.ea + ctxt->_eip);
53965417
53975418 done:
5419+ if (rc == X86EMUL_PROPAGATE_FAULT)
5420+ ctxt->have_exception = true;
53985421 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
53995422 }
54005423
arch/x86/kvm/lapic.c+11−9
@@ -1198,10 +1198,8 @@ void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector) · 1197 unmodified lines
11981198 }
11991199 EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
12001200
1201−static void apic_send_ipi(struct kvm_lapic *apic)
1201+static void apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high)
12021202 {
1203− u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1204− u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
12051203 struct kvm_lapic_irq irq;
12061204
12071205 irq.vector = icr_low & APIC_VECTOR_MASK;
@@ -1914,8 +1912,9 @@ int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val) · 706 unmodified lines
19141912 }
19151913 case APIC_ICR:
19161914 /* No delay here, so we always clear the pending bit */
1917− kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
1918− apic_send_ipi(apic);
1915+ val &= ~(1 << 12);
1916+ apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2));
1917+ kvm_lapic_set_reg(apic, APIC_ICR, val);
19191918 break;
19201919
19211920 case APIC_ICR2:
@@ -2707,11 +2706,14 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu) · 785 unmodified lines
27072706 return;
27082707
27092708 /*
2710− * INITs are latched while in SMM. Because an SMM CPU cannot
2711− * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2712− * and delay processing of INIT until the next RSM.
2709+ * INITs are latched while CPU is in specific states
2710+ * (SMM, VMX non-root mode, SVM with GIF=0).
2711+ * Because a CPU cannot be in these states immediately
2712+ * after it has processed an INIT signal (and thus in
2713+ * KVM_MP_STATE_INIT_RECEIVED state), just eat SIPIs
2714+ * and leave the INIT pending.
27132715 */
2714− if (is_smm(vcpu)) {
2716+ if (is_smm(vcpu) || kvm_x86_ops->apic_init_signal_blocked(vcpu)) {
27152717 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
27162718 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
27172719 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
arch/x86/kvm/mmu.c+39−22
@@ -214,6 +214,7 @@ static u64 __read_mostly shadow_accessed_mask; · 213 unmodified lines
214214 static u64 __read_mostly shadow_dirty_mask;
215215 static u64 __read_mostly shadow_mmio_mask;
216216 static u64 __read_mostly shadow_mmio_value;
217+static u64 __read_mostly shadow_mmio_access_mask;
217218 static u64 __read_mostly shadow_present_mask;
218219 static u64 __read_mostly shadow_me_mask;
219220
@@ -299,14 +300,21 @@ static void kvm_flush_remote_tlbs_with_address(struct kvm *kvm, · 79 unmodified lines
299300 kvm_flush_remote_tlbs_with_range(kvm, &range);
300301 }
301302
302−void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask, u64 mmio_value)
303+void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask, u64 mmio_value, u64 access_mask)
303304 {
305+ BUG_ON((u64)(unsigned)access_mask != access_mask);
304306 BUG_ON((mmio_mask & mmio_value) != mmio_value);
305307 shadow_mmio_value = mmio_value | SPTE_SPECIAL_MASK;
306308 shadow_mmio_mask = mmio_mask | SPTE_SPECIAL_MASK;
309+ shadow_mmio_access_mask = access_mask;
307310 }
308311 EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask);
309312
313+static bool is_mmio_spte(u64 spte)
314+{
315+ return (spte & shadow_mmio_mask) == shadow_mmio_value;
316+}
317+
310318 static inline bool sp_ad_disabled(struct kvm_mmu_page *sp)
311319 {
312320 return sp->role.ad_disabled;
@@ -314,19 +322,19 @@ static inline bool sp_ad_disabled(struct kvm_mmu_page *sp) · 1 unmodified line
314322
315323 static inline bool spte_ad_enabled(u64 spte)
316324 {
317− MMU_WARN_ON((spte & shadow_mmio_mask) == shadow_mmio_value);
325+ MMU_WARN_ON(is_mmio_spte(spte));
318326 return !(spte & shadow_acc_track_value);
319327 }
320328
321329 static inline u64 spte_shadow_accessed_mask(u64 spte)
322330 {
323− MMU_WARN_ON((spte & shadow_mmio_mask) == shadow_mmio_value);
331+ MMU_WARN_ON(is_mmio_spte(spte));
324332 return spte_ad_enabled(spte) ? shadow_accessed_mask : 0;
325333 }
326334
327335 static inline u64 spte_shadow_dirty_mask(u64 spte)
328336 {
329− MMU_WARN_ON((spte & shadow_mmio_mask) == shadow_mmio_value);
337+ MMU_WARN_ON(is_mmio_spte(spte));
330338 return spte_ad_enabled(spte) ? shadow_dirty_mask : 0;
331339 }
332340
@@ -389,7 +397,7 @@ static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn, · 56 unmodified lines
389397 u64 mask = generation_mmio_spte_mask(gen);
390398 u64 gpa = gfn << PAGE_SHIFT;
391399
392− access &= ACC_WRITE_MASK | ACC_USER_MASK;
400+ access &= shadow_mmio_access_mask;
393401 mask |= shadow_mmio_value | access;
394402 mask |= gpa | shadow_nonpresent_or_rsvd_mask;
395403 mask |= (gpa & shadow_nonpresent_or_rsvd_mask)
@@ -401,11 +409,6 @@ static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn, · 5 unmodified lines
401409 mmu_spte_set(sptep, mask);
402410 }
403411
404−static bool is_mmio_spte(u64 spte)
405−{
406− return (spte & shadow_mmio_mask) == shadow_mmio_value;
407−}
408−
409412 static gfn_t get_mmio_spte_gfn(u64 spte)
410413 {
411414 u64 gpa = spte & shadow_nonpresent_or_rsvd_lower_gfn_mask;
@@ -418,8 +421,7 @@ static gfn_t get_mmio_spte_gfn(u64 spte) · 6 unmodified lines
418421
419422 static unsigned get_mmio_spte_access(u64 spte)
420423 {
421− u64 mask = generation_mmio_spte_mask(MMIO_SPTE_GEN_MASK) | shadow_mmio_mask;
422− return (spte & ~mask) & ~PAGE_MASK;
424+ return spte & shadow_mmio_access_mask;
423425 }
424426
425427 static bool set_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
@@ -3302,7 +3304,8 @@ static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn, · 2876 unmodified lines
33023304 }
33033305
33043306 if (unlikely(is_noslot_pfn(pfn)))
3305− vcpu_cache_mmio_info(vcpu, gva, gfn, access);
3307+ vcpu_cache_mmio_info(vcpu, gva, gfn,
3308+ access & shadow_mmio_access_mask);
33063309
33073310 return false;
33083311 }
@@ -5611,13 +5614,13 @@ slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot, · 2302 unmodified lines
56115614 PT_PAGE_TABLE_LEVEL, lock_flush_tlb);
56125615 }
56135616
5614−static void free_mmu_pages(struct kvm_vcpu *vcpu)
5617+static void free_mmu_pages(struct kvm_mmu *mmu)
56155618 {
5616− free_page((unsigned long)vcpu->arch.mmu->pae_root);
5617− free_page((unsigned long)vcpu->arch.mmu->lm_root);
5619+ free_page((unsigned long)mmu->pae_root);
5620+ free_page((unsigned long)mmu->lm_root);
56185621 }
56195622
5620−static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
5623+static int alloc_mmu_pages(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
56215624 {
56225625 struct page *page;
56235626 int i;
@@ -5638,9 +5641,9 @@ static int alloc_mmu_pages(struct kvm_vcpu *vcpu) · 14 unmodified lines
56385641 if (!page)
56395642 return -ENOMEM;
56405643
5641− vcpu->arch.mmu->pae_root = page_address(page);
5644+ mmu->pae_root = page_address(page);
56425645 for (i = 0; i < 4; ++i)
5643− vcpu->arch.mmu->pae_root[i] = INVALID_PAGE;
5646+ mmu->pae_root[i] = INVALID_PAGE;
56445647
56455648 return 0;
56465649 }
@@ -5648,6 +5651,7 @@ static int alloc_mmu_pages(struct kvm_vcpu *vcpu) · 1 unmodified line
56485651 int kvm_mmu_create(struct kvm_vcpu *vcpu)
56495652 {
56505653 uint i;
5654+ int ret;
56515655
56525656 vcpu->arch.mmu = &vcpu->arch.root_mmu;
56535657 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
@@ -5665,7 +5669,19 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) · 11 unmodified lines
56655669 vcpu->arch.guest_mmu.prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID;
56665670
56675671 vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
5668− return alloc_mmu_pages(vcpu);
5672+
5673+ ret = alloc_mmu_pages(vcpu, &vcpu->arch.guest_mmu);
5674+ if (ret)
5675+ return ret;
5676+
5677+ ret = alloc_mmu_pages(vcpu, &vcpu->arch.root_mmu);
5678+ if (ret)
5679+ goto fail_allocate_root;
5680+
5681+ return ret;
5682+ fail_allocate_root:
5683+ free_mmu_pages(&vcpu->arch.guest_mmu);
5684+ return ret;
56695685 }
56705686
56715687
@@ -6094,7 +6110,7 @@ static void kvm_set_mmio_spte_mask(void) · 422 unmodified lines
60946110 if (IS_ENABLED(CONFIG_X86_64) && shadow_phys_bits == 52)
60956111 mask &= ~1ull;
60966112
6097− kvm_mmu_set_mmio_spte_mask(mask, mask);
6113+ kvm_mmu_set_mmio_spte_mask(mask, mask, ACC_WRITE_MASK | ACC_USER_MASK);
60986114 }
60996115
61006116 int kvm_mmu_module_init(void)
@@ -6168,7 +6184,8 @@ unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm) · 67 unmodified lines
61686184 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
61696185 {
61706186 kvm_mmu_unload(vcpu);
6171− free_mmu_pages(vcpu);
6187+ free_mmu_pages(&vcpu->arch.root_mmu);
6188+ free_mmu_pages(&vcpu->arch.guest_mmu);
61726189 mmu_free_memory_caches(vcpu);
61736190 }
61746191
arch/x86/kvm/mmu.h+1−1
@@ -51,7 +51,7 @@ static inline u64 rsvd_bits(int s, int e) · 50 unmodified lines
5151 return ((1ULL << (e - s + 1)) - 1) << s;
5252 }
5353
54−void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask, u64 mmio_value);
54+void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask, u64 mmio_value, u64 access_mask);
5555
5656 void
5757 reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context);
arch/x86/kvm/svm.c+93−105
@@ -68,10 +68,8 @@ MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); · 67 unmodified lines
6868 #define SEG_TYPE_LDT 2
6969 #define SEG_TYPE_BUSY_TSS16 3
7070
71−#define SVM_FEATURE_NPT (1 << 0)
7271 #define SVM_FEATURE_LBRV (1 << 1)
7372 #define SVM_FEATURE_SVML (1 << 2)
74−#define SVM_FEATURE_NRIP (1 << 3)
7573 #define SVM_FEATURE_TSC_RATE (1 << 4)
7674 #define SVM_FEATURE_VMCB_CLEAN (1 << 5)
7775 #define SVM_FEATURE_FLUSH_ASID (1 << 6)
@@ -770,7 +768,7 @@ static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) · 692 unmodified lines
770768
771769 }
772770
773−static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
771+static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
774772 {
775773 struct vcpu_svm *svm = to_svm(vcpu);
776774
@@ -779,18 +777,17 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu) · 2 unmodified lines
779777 svm->next_rip = svm->vmcb->control.next_rip;
780778 }
781779
782− if (!svm->next_rip) {
783− if (kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) !=
784− EMULATE_DONE)
785− printk(KERN_DEBUG "%s: NOP\n", __func__);
786− return;
787− }
780+ if (!svm->next_rip)
781+ return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP);
782+
788783 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
789784 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
790785 __func__, kvm_rip_read(vcpu), svm->next_rip);
791786
792787 kvm_rip_write(vcpu, svm->next_rip);
793788 svm_set_interrupt_shadow(vcpu, 0);
789+
790+ return EMULATE_DONE;
794791 }
795792
796793 static void svm_queue_exception(struct kvm_vcpu *vcpu)
@@ -821,7 +818,7 @@ static void svm_queue_exception(struct kvm_vcpu *vcpu) · 24 unmodified lines
821818 * raises a fault that is not intercepted. Still better than
822819 * failing in all cases.
823820 */
824− skip_emulated_instruction(&svm->vcpu);
821+ (void)skip_emulated_instruction(&svm->vcpu);
825822 rip = kvm_rip_read(&svm->vcpu);
826823 svm->int3_rip = rip + svm->vmcb->save.cs.base;
827824 svm->int3_injected = rip - old_rip;
@@ -1269,11 +1266,11 @@ static void grow_ple_window(struct kvm_vcpu *vcpu) · 441 unmodified lines
12691266 pause_filter_count_grow,
12701267 pause_filter_count_max);
12711268
1272− if (control->pause_filter_count != old)
1269+ if (control->pause_filter_count != old) {
12731270 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1274−
1275− trace_kvm_ple_window_grow(vcpu->vcpu_id,
1276− control->pause_filter_count, old);
1271+ trace_kvm_ple_window_update(vcpu->vcpu_id,
1272+ control->pause_filter_count, old);
1273+ }
12771274 }
12781275
12791276 static void shrink_ple_window(struct kvm_vcpu *vcpu)
@@ -1287,11 +1284,11 @@ static void shrink_ple_window(struct kvm_vcpu *vcpu) · 7 unmodified lines
12871284 pause_filter_count,
12881285 pause_filter_count_shrink,
12891286 pause_filter_count);
1290− if (control->pause_filter_count != old)
1287+ if (control->pause_filter_count != old) {
12911288 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1292−
1293− trace_kvm_ple_window_shrink(vcpu->vcpu_id,
1294− control->pause_filter_count, old);
1289+ trace_kvm_ple_window_update(vcpu->vcpu_id,
1290+ control->pause_filter_count, old);
1291+ }
12951292 }
12961293
12971294 static __init int svm_hardware_setup(void)
@@ -2136,6 +2133,9 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) · 838 unmodified lines
21362133 struct page *nested_msrpm_pages;
21372134 int err;
21382135
2136+ BUILD_BUG_ON_MSG(offsetof(struct vcpu_svm, vcpu) != 0,
2137+ "struct kvm_vcpu must be at offset 0 for arch usercopy region");
2138+
21392139 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
21402140 if (!svm) {
21412141 err = -ENOMEM;
@@ -2903,13 +2903,11 @@ static int nop_on_interception(struct vcpu_svm *svm) · 761 unmodified lines
29032903
29042904 static int halt_interception(struct vcpu_svm *svm)
29052905 {
2906− svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
29072906 return kvm_emulate_halt(&svm->vcpu);
29082907 }
29092908
29102909 static int vmmcall_interception(struct vcpu_svm *svm)
29112910 {
2912− svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
29132911 return kvm_emulate_hypercall(&svm->vcpu);
29142912 }
29152913
@@ -3588,9 +3586,9 @@ static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, · 672 unmodified lines
35883586 mark_all_dirty(svm->vmcb);
35893587 }
35903588
3591−static bool nested_svm_vmrun(struct vcpu_svm *svm)
3589+static int nested_svm_vmrun(struct vcpu_svm *svm)
35923590 {
3593− int rc;
3591+ int ret;
35943592 struct vmcb *nested_vmcb;
35953593 struct vmcb *hsave = svm->nested.hsave;
35963594 struct vmcb *vmcb = svm->vmcb;
@@ -3599,13 +3597,16 @@ static bool nested_svm_vmrun(struct vcpu_svm *svm) · 2 unmodified lines
35993597
36003598 vmcb_gpa = svm->vmcb->save.rax;
36013599
3602− rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map);
3603− if (rc) {
3604− if (rc == -EINVAL)
3605− kvm_inject_gp(&svm->vcpu, 0);
3606− return false;
3600+ ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map);
3601+ if (ret == -EINVAL) {
3602+ kvm_inject_gp(&svm->vcpu, 0);
3603+ return 1;
3604+ } else if (ret) {
3605+ return kvm_skip_emulated_instruction(&svm->vcpu);
36073606 }
36083607
3608+ ret = kvm_skip_emulated_instruction(&svm->vcpu);
3609+
36093610 nested_vmcb = map.hva;
36103611
36113612 if (!nested_vmcb_checks(nested_vmcb)) {
@@ -3616,7 +3617,7 @@ static bool nested_svm_vmrun(struct vcpu_svm *svm) · 4 unmodified lines
36163617
36173618 kvm_vcpu_unmap(&svm->vcpu, &map, true);
36183619
3619− return false;
3620+ return ret;
36203621 }
36213622
36223623 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
@@ -3660,7 +3661,16 @@ static bool nested_svm_vmrun(struct vcpu_svm *svm) · 37 unmodified lines
36603661
36613662 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, &map);
36623663
3663− return true;
3664+ if (!nested_svm_vmrun_msrpm(svm)) {
3665+ svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3666+ svm->vmcb->control.exit_code_hi = 0;
3667+ svm->vmcb->control.exit_info_1 = 0;
3668+ svm->vmcb->control.exit_info_2 = 0;
3669+
3670+ nested_svm_vmexit(svm);
3671+ }
3672+
3673+ return ret;
36643674 }
36653675
36663676 static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
@@ -3697,7 +3707,6 @@ static int vmload_interception(struct vcpu_svm *svm) · 30 unmodified lines
36973707
36983708 nested_vmcb = map.hva;
36993709
3700− svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
37013710 ret = kvm_skip_emulated_instruction(&svm->vcpu);
37023711
37033712 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
@@ -3724,7 +3733,6 @@ static int vmsave_interception(struct vcpu_svm *svm) · 20 unmodified lines
37243733
37253734 nested_vmcb = map.hva;
37263735
3727− svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
37283736 ret = kvm_skip_emulated_instruction(&svm->vcpu);
37293737
37303738 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
@@ -3738,27 +3746,7 @@ static int vmrun_interception(struct vcpu_svm *svm) · 7 unmodified lines
37383746 if (nested_svm_check_permissions(svm))
37393747 return 1;
37403748
3741− /* Save rip after vmrun instruction */
3742− kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
3743−
3744− if (!nested_svm_vmrun(svm))
3745− return 1;
3746−
3747− if (!nested_svm_vmrun_msrpm(svm))
3748− goto failed;
3749−
3750− return 1;
3751−
3752−failed:
3753−
3754− svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3755− svm->vmcb->control.exit_code_hi = 0;
3756− svm->vmcb->control.exit_info_1 = 0;
3757− svm->vmcb->control.exit_info_2 = 0;
3758−
3759− nested_svm_vmexit(svm);
3760−
3761− return 1;
3749+ return nested_svm_vmrun(svm);
37623750 }
37633751
37643752 static int stgi_interception(struct vcpu_svm *svm)
@@ -3775,7 +3763,6 @@ static int stgi_interception(struct vcpu_svm *svm) · 10 unmodified lines
37753763 if (vgif_enabled(svm))
37763764 clr_intercept(svm, INTERCEPT_STGI);
37773765
3778− svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
37793766 ret = kvm_skip_emulated_instruction(&svm->vcpu);
37803767 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
37813768
@@ -3791,7 +3778,6 @@ static int clgi_interception(struct vcpu_svm *svm) · 9 unmodified lines
37913778 if (nested_svm_check_permissions(svm))
37923779 return 1;
37933780
3794− svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
37953781 ret = kvm_skip_emulated_instruction(&svm->vcpu);
37963782
37973783 disable_gif(svm);
@@ -3816,7 +3802,6 @@ static int invlpga_interception(struct vcpu_svm *svm) · 18 unmodified lines
38163802 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
38173803 kvm_mmu_invlpg(vcpu, kvm_rax_read(&svm->vcpu));
38183804
3819− svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
38203805 return kvm_skip_emulated_instruction(&svm->vcpu);
38213806 }
38223807
@@ -3839,7 +3824,6 @@ static int xsetbv_interception(struct vcpu_svm *svm) · 16 unmodified lines
38393824 u32 index = kvm_rcx_read(&svm->vcpu);
38403825
38413826 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3842− svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
38433827 return kvm_skip_emulated_instruction(&svm->vcpu);
38443828 }
38453829
@@ -3898,25 +3882,29 @@ static int task_switch_interception(struct vcpu_svm *svm) · 52 unmodified lines
38983882 if (reason != TASK_SWITCH_GATE ||
38993883 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
39003884 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
3901− (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3902− skip_emulated_instruction(&svm->vcpu);
3885+ (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
3886+ if (skip_emulated_instruction(&svm->vcpu) != EMULATE_DONE)
3887+ goto fail;
3888+ }
39033889
39043890 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
39053891 int_vec = -1;
39063892
39073893 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
3908− has_error_code, error_code) == EMULATE_FAIL) {
3909− svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3910− svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3911− svm->vcpu.run->internal.ndata = 0;
3912− return 0;
3913− }
3894+ has_error_code, error_code) == EMULATE_FAIL)
3895+ goto fail;
3896+
39143897 return 1;
3898+
3899+fail:
3900+ svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3901+ svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3902+ svm->vcpu.run->internal.ndata = 0;
3903+ return 0;
39153904 }
39163905
39173906 static int cpuid_interception(struct vcpu_svm *svm)
39183907 {
3919− svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
39203908 return kvm_emulate_cpuid(&svm->vcpu);
39213909 }
39223910
@@ -4232,23 +4220,7 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · 309 unmodified lines
42324220
42334221 static int rdmsr_interception(struct vcpu_svm *svm)
42344222 {
4235− u32 ecx = kvm_rcx_read(&svm->vcpu);
4236− struct msr_data msr_info;
4237−
4238− msr_info.index = ecx;
4239− msr_info.host_initiated = false;
4240− if (svm_get_msr(&svm->vcpu, &msr_info)) {
4241− trace_kvm_msr_read_ex(ecx);
4242− kvm_inject_gp(&svm->vcpu, 0);
4243− return 1;
4244− } else {
4245− trace_kvm_msr_read(ecx, msr_info.data);
4246−
4247− kvm_rax_write(&svm->vcpu, msr_info.data & 0xffffffff);
4248− kvm_rdx_write(&svm->vcpu, msr_info.data >> 32);
4249− svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
4250− return kvm_skip_emulated_instruction(&svm->vcpu);
4251− }
4223+ return kvm_emulate_rdmsr(&svm->vcpu);
42524224 }
42534225
42544226 static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
@@ -4438,23 +4410,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) · 183 unmodified lines
44384410
44394411 static int wrmsr_interception(struct vcpu_svm *svm)
44404412 {
4441− struct msr_data msr;
4442− u32 ecx = kvm_rcx_read(&svm->vcpu);
4443− u64 data = kvm_read_edx_eax(&svm->vcpu);
4444−
4445− msr.data = data;
4446− msr.index = ecx;
4447− msr.host_initiated = false;
4448−
4449− svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
4450− if (kvm_set_msr(&svm->vcpu, &msr)) {
4451− trace_kvm_msr_write_ex(ecx, data);
4452− kvm_inject_gp(&svm->vcpu, 0);
4453− return 1;
4454− } else {
4455− trace_kvm_msr_write(ecx, data);
4456− return kvm_skip_emulated_instruction(&svm->vcpu);
4457− }
4413+ return kvm_emulate_wrmsr(&svm->vcpu);
44584414 }
44594415
44604416 static int msr_interception(struct vcpu_svm *svm)
@@ -5025,9 +4981,14 @@ static int handle_exit(struct kvm_vcpu *vcpu) · 564 unmodified lines
50254981
50264982 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
50274983 || !svm_exit_handlers[exit_code]) {
5028− WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
5029− kvm_queue_exception(vcpu, UD_VECTOR);
5030− return 1;
4984+ vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%x\n", exit_code);
4985+ dump_vmcb(vcpu);
4986+ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4987+ vcpu->run->internal.suberror =
4988+ KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
4989+ vcpu->run->internal.ndata = 1;
4990+ vcpu->run->internal.data[0] = exit_code;
4991+ return 0;
50314992 }
50324993
50334994 return svm_exit_handlers[exit_code](svm);
@@ -5274,7 +5235,8 @@ get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, · 240 unmodified lines
52745235
52755236 kvm_set_msi_irq(kvm, e, &irq);
52765237
5277− if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
5238+ if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
5239+ !kvm_irq_is_postable(&irq)) {
52785240 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
52795241 __func__, irq.vector);
52805242 return -1;
@@ -5328,6 +5290,7 @@ static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq, · 47 unmodified lines
53285290 * 1. When cannot target interrupt to a specific vcpu.
53295291 * 2. Unsetting posted interrupt.
53305292 * 3. APIC virtialization is disabled for the vcpu.
5293+ * 4. IRQ has incompatible delivery mode (SMI, INIT, etc)
53315294 */
53325295 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
53335296 kvm_vcpu_apicv_active(&svm->vcpu)) {
@@ -5933,6 +5896,8 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) · 599 unmodified lines
59335896 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
59345897 }
59355898
5899+#define F(x) bit(X86_FEATURE_##x)
5900+
59365901 static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
59375902 {
59385903 switch (func) {
@@ -5944,6 +5909,11 @@ static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) · 5 unmodified lines
59445909 if (nested)
59455910 entry->ecx |= (1 << 2); /* Set SVM bit */
59465911 break;
5912+ case 0x80000008:
5913+ if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
5914+ boot_cpu_has(X86_FEATURE_AMD_SSBD))
5915+ entry->ebx |= F(VIRT_SSBD);
5916+ break;
59475917 case 0x8000000A:
59485918 entry->eax = 1; /* SVM revision 1 */
59495919 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
@@ -5954,11 +5924,11 @@ static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) · 4 unmodified lines
59545924
59555925 /* Support next_rip if host supports it */
59565926 if (boot_cpu_has(X86_FEATURE_NRIPS))
5957− entry->edx |= SVM_FEATURE_NRIP;
5927+ entry->edx |= F(NRIPS);
59585928
59595929 /* Support NPT for the guest if enabled */
59605930 if (npt_enabled)
5961− entry->edx |= SVM_FEATURE_NPT;
5931+ entry->edx |= F(NPT);
59625932
59635933 break;
59645934 case 0x8000001F:
@@ -6067,6 +6037,7 @@ static const struct __x86_intercept { · 102 unmodified lines
60676037 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
60686038 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
60696039 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
6040+ [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
60706041 };
60716042
60726043 #undef PRE_EX
@@ -7193,6 +7164,21 @@ static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu) · 1120 unmodified lines
71937164 return false;
71947165 }
71957166
7167+static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7168+{
7169+ struct vcpu_svm *svm = to_svm(vcpu);
7170+
7171+ /*
7172+ * TODO: Last condition latch INIT signals on vCPU when
7173+ * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
7174+ * To properly emulate the INIT intercept, SVM should implement
7175+ * kvm_x86_ops->check_nested_events() and call nested_svm_vmexit()
7176+ * there if an INIT signal is pending.
7177+ */
7178+ return !gif_set(svm) ||
7179+ (svm->vmcb->control.intercept & (1ULL << INTERCEPT_INIT));
7180+}
7181+
71967182 static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
71977183 .cpu_has_kvm_support = has_svm,
71987184 .disabled_by_bios = is_disabled,
@@ -7329,6 +7315,8 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { · 130 unmodified lines
73297315 .nested_get_evmcs_version = NULL,
73307316
73317317 .need_emulation_on_page_fault = svm_need_emulation_on_page_fault,
7318+
7319+ .apic_init_signal_blocked = svm_apic_init_signal_blocked,
73327320 };
73337321
73347322 static int __init svm_init(void)
arch/x86/kvm/trace.h+54−20
@@ -232,17 +232,20 @@ TRACE_EVENT(kvm_exit, · 231 unmodified lines
232232 __field( u32, isa )
233233 __field( u64, info1 )
234234 __field( u64, info2 )
235+ __field( unsigned int, vcpu_id )
235236 ),
236237
237238 TP_fast_assign(
238239 __entry->exit_reason = exit_reason;
239240 __entry->guest_rip = kvm_rip_read(vcpu);
240241 __entry->isa = isa;
242+ __entry->vcpu_id = vcpu->vcpu_id;
241243 kvm_x86_ops->get_exit_info(vcpu, &__entry->info1,
242244 &__entry->info2);
243245 ),
244246
245− TP_printk("reason %s rip 0x%lx info %llx %llx",
247+ TP_printk("vcpu %u reason %s rip 0x%lx info %llx %llx",
248+ __entry->vcpu_id,
246249 (__entry->isa == KVM_ISA_VMX) ?
247250 __print_symbolic(__entry->exit_reason, VMX_EXIT_REASONS) :
248251 __print_symbolic(__entry->exit_reason, SVM_EXIT_REASONS),
@@ -887,36 +890,27 @@ TRACE_EVENT(kvm_pml_full, · 638 unmodified lines
887890 TP_printk("vcpu %d: PML full", __entry->vcpu_id)
888891 );
889892
890−TRACE_EVENT(kvm_ple_window,
891− TP_PROTO(bool grow, unsigned int vcpu_id, int new, int old),
892− TP_ARGS(grow, vcpu_id, new, old),
893+TRACE_EVENT(kvm_ple_window_update,
894+ TP_PROTO(unsigned int vcpu_id, unsigned int new, unsigned int old),
895+ TP_ARGS(vcpu_id, new, old),
893896
894897 TP_STRUCT__entry(
895− __field( bool, grow )
896898 __field( unsigned int, vcpu_id )
897− __field( int, new )
898− __field( int, old )
899+ __field( unsigned int, new )
900+ __field( unsigned int, old )
899901 ),
900902
901903 TP_fast_assign(
902− __entry->grow = grow;
903904 __entry->vcpu_id = vcpu_id;
904905 __entry->new = new;
905906 __entry->old = old;
906907 ),
907908
908− TP_printk("vcpu %u: ple_window %d (%s %d)",
909− __entry->vcpu_id,
910− __entry->new,
911− __entry->grow ? "grow" : "shrink",
912− __entry->old)
909+ TP_printk("vcpu %u old %u new %u (%s)",
910+ __entry->vcpu_id, __entry->old, __entry->new,
911+ __entry->old < __entry->new ? "growed" : "shrinked")
913912 );
914913
915−#define trace_kvm_ple_window_grow(vcpu_id, new, old) \
916− trace_kvm_ple_window(true, vcpu_id, new, old)
917−#define trace_kvm_ple_window_shrink(vcpu_id, new, old) \
918− trace_kvm_ple_window(false, vcpu_id, new, old)
919−
920914 TRACE_EVENT(kvm_pvclock_update,
921915 TP_PROTO(unsigned int vcpu_id, struct pvclock_vcpu_time_info *pvclock),
922916 TP_ARGS(vcpu_id, pvclock),
@@ -1320,7 +1314,7 @@ TRACE_EVENT(kvm_avic_incomplete_ipi, · 397 unmodified lines
13201314 __entry->index = index;
13211315 ),
13221316
1323− TP_printk("vcpu=%u, icrh:icrl=%#010x:%08x, id=%u, index=%u\n",
1317+ TP_printk("vcpu=%u, icrh:icrl=%#010x:%08x, id=%u, index=%u",
13241318 __entry->vcpu, __entry->icrh, __entry->icrl,
13251319 __entry->id, __entry->index)
13261320 );
@@ -1345,7 +1339,7 @@ TRACE_EVENT(kvm_avic_unaccelerated_access, · 18 unmodified lines
13451339 __entry->vec = vec;
13461340 ),
13471341
1348− TP_printk("vcpu=%u, offset=%#x(%s), %s, %s, vec=%#x\n",
1342+ TP_printk("vcpu=%u, offset=%#x(%s), %s, %s, vec=%#x",
13491343 __entry->vcpu,
13501344 __entry->offset,
13511345 __print_symbolic(__entry->offset, kvm_trace_symbol_apic),
@@ -1462,6 +1456,46 @@ TRACE_EVENT(kvm_hv_send_ipi_ex, · 110 unmodified lines
14621456 __entry->vector, __entry->format,
14631457 __entry->valid_bank_mask)
14641458 );
1459+
1460+TRACE_EVENT(kvm_pv_tlb_flush,
1461+ TP_PROTO(unsigned int vcpu_id, bool need_flush_tlb),
1462+ TP_ARGS(vcpu_id, need_flush_tlb),
1463+
1464+ TP_STRUCT__entry(
1465+ __field( unsigned int, vcpu_id )
1466+ __field( bool, need_flush_tlb )
1467+ ),
1468+
1469+ TP_fast_assign(
1470+ __entry->vcpu_id = vcpu_id;
1471+ __entry->need_flush_tlb = need_flush_tlb;
1472+ ),
1473+
1474+ TP_printk("vcpu %u need_flush_tlb %s", __entry->vcpu_id,
1475+ __entry->need_flush_tlb ? "true" : "false")
1476+);
1477+
1478+/*
1479+ * Tracepoint for failed nested VMX VM-Enter.
1480+ */
1481+TRACE_EVENT(kvm_nested_vmenter_failed,
1482+ TP_PROTO(const char *msg, u32 err),
1483+ TP_ARGS(msg, err),
1484+
1485+ TP_STRUCT__entry(
1486+ __field(const char *, msg)
1487+ __field(u32, err)
1488+ ),
1489+
1490+ TP_fast_assign(
1491+ __entry->msg = msg;
1492+ __entry->err = err;
1493+ ),
1494+
1495+ TP_printk("%s%s", __entry->msg, !__entry->err ? "" :
1496+ __print_symbolic(__entry->err, VMX_VMENTER_INSTRUCTION_ERRORS))
1497+);
1498+
14651499 #endif /* _TRACE_KVM_H */
14661500
14671501 #undef TRACE_INCLUDE_PATH
arch/x86/kvm/vmx/nested.c+162−143
@@ -19,6 +19,14 @@ module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); · 18 unmodified lines
1919 static bool __read_mostly nested_early_check = 0;
2020 module_param(nested_early_check, bool, S_IRUGO);
2121
22+#define CC(consistency_check) \
23+({ \
24+ bool failed = (consistency_check); \
25+ if (failed) \
26+ trace_kvm_nested_vmenter_failed(#consistency_check, 0); \
27+ failed; \
28+})
29+
2230 /*
2331 * Hyper-V requires all of these, so mark them as supported even though
2432 * they are just treated the same as all-context.
@@ -430,8 +438,8 @@ static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, · 405 unmodified lines
430438 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
431439 return 0;
432440
433− if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
434− !page_address_valid(vcpu, vmcs12->io_bitmap_b))
441+ if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
442+ CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
435443 return -EINVAL;
436444
437445 return 0;
@@ -443,7 +451,7 @@ static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, · 5 unmodified lines
443451 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
444452 return 0;
445453
446− if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
454+ if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
447455 return -EINVAL;
448456
449457 return 0;
@@ -455,7 +463,7 @@ static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, · 5 unmodified lines
455463 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
456464 return 0;
457465
458− if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
466+ if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
459467 return -EINVAL;
460468
461469 return 0;
@@ -688,7 +696,7 @@ static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, · 226 unmodified lines
688696 struct vmcs12 *vmcs12)
689697 {
690698 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
691− !page_address_valid(vcpu, vmcs12->apic_access_addr))
699+ CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
692700 return -EINVAL;
693701 else
694702 return 0;
@@ -707,16 +715,15 @@ static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, · 12 unmodified lines
707715 * If virtualize x2apic mode is enabled,
708716 * virtualize apic access must be disabled.
709717 */
710− if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
711− nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
718+ if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
719+ nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
712720 return -EINVAL;
713721
714722 /*
715723 * If virtual interrupt delivery is enabled,
716724 * we must exit on external interrupts.
717725 */
718− if (nested_cpu_has_vid(vmcs12) &&
719− !nested_exit_on_intr(vcpu))
726+ if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
720727 return -EINVAL;
721728
722729 /*
@@ -727,15 +734,15 @@ static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, · 4 unmodified lines
727734 * bits 5:0 of posted_intr_desc_addr should be zero.
728735 */
729736 if (nested_cpu_has_posted_intr(vmcs12) &&
730− (!nested_cpu_has_vid(vmcs12) ||
731− !nested_exit_intr_ack_set(vcpu) ||
732− (vmcs12->posted_intr_nv & 0xff00) ||
733− (vmcs12->posted_intr_desc_addr & 0x3f) ||
734− (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
737+ (CC(!nested_cpu_has_vid(vmcs12)) ||
738+ CC(!nested_exit_intr_ack_set(vcpu)) ||
739+ CC((vmcs12->posted_intr_nv & 0xff00)) ||
740+ CC((vmcs12->posted_intr_desc_addr & 0x3f)) ||
741+ CC((vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu)))))
735742 return -EINVAL;
736743
737744 /* tpr shadow is needed by all apicv features. */
738− if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
745+ if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
739746 return -EINVAL;
740747
741748 return 0;
@@ -759,10 +766,12 @@ static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, · 17 unmodified lines
759766 static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
760767 struct vmcs12 *vmcs12)
761768 {
762− if (nested_vmx_check_msr_switch(vcpu, vmcs12->vm_exit_msr_load_count,
763− vmcs12->vm_exit_msr_load_addr) ||
764− nested_vmx_check_msr_switch(vcpu, vmcs12->vm_exit_msr_store_count,
765− vmcs12->vm_exit_msr_store_addr))
769+ if (CC(nested_vmx_check_msr_switch(vcpu,
770+ vmcs12->vm_exit_msr_load_count,
771+ vmcs12->vm_exit_msr_load_addr)) ||
772+ CC(nested_vmx_check_msr_switch(vcpu,
773+ vmcs12->vm_exit_msr_store_count,
774+ vmcs12->vm_exit_msr_store_addr)))
766775 return -EINVAL;
767776
768777 return 0;
@@ -771,8 +780,9 @@ static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, · 2 unmodified lines
771780 static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
772781 struct vmcs12 *vmcs12)
773782 {
774− if (nested_vmx_check_msr_switch(vcpu, vmcs12->vm_entry_msr_load_count,
775− vmcs12->vm_entry_msr_load_addr))
783+ if (CC(nested_vmx_check_msr_switch(vcpu,
784+ vmcs12->vm_entry_msr_load_count,
785+ vmcs12->vm_entry_msr_load_addr)))
776786 return -EINVAL;
777787
778788 return 0;
@@ -784,8 +794,8 @@ static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, · 5 unmodified lines
784794 if (!nested_cpu_has_pml(vmcs12))
785795 return 0;
786796
787− if (!nested_cpu_has_ept(vmcs12) ||
788− !page_address_valid(vcpu, vmcs12->pml_address))
797+ if (CC(!nested_cpu_has_ept(vmcs12)) ||
798+ CC(!page_address_valid(vcpu, vmcs12->pml_address)))
789799 return -EINVAL;
790800
791801 return 0;
@@ -794,8 +804,8 @@ static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, · 2 unmodified lines
794804 static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
795805 struct vmcs12 *vmcs12)
796806 {
797− if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
798− !nested_cpu_has_ept(vmcs12))
807+ if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
808+ !nested_cpu_has_ept(vmcs12)))
799809 return -EINVAL;
800810 return 0;
801811 }
@@ -803,8 +813,8 @@ static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, · 1 unmodified line
803813 static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
804814 struct vmcs12 *vmcs12)
805815 {
806− if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
807− !nested_cpu_has_ept(vmcs12))
816+ if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
817+ !nested_cpu_has_ept(vmcs12)))
808818 return -EINVAL;
809819 return 0;
810820 }
@@ -815,8 +825,8 @@ static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, · 4 unmodified lines
815825 if (!nested_cpu_has_shadow_vmcs(vmcs12))
816826 return 0;
817827
818− if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
819− !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
828+ if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
829+ CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
820830 return -EINVAL;
821831
822832 return 0;
@@ -826,12 +836,12 @@ static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, · 3 unmodified lines
826836 struct vmx_msr_entry *e)
827837 {
828838 /* x2APIC MSR accesses are not allowed */
829− if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
839+ if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
830840 return -EINVAL;
831− if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
832− e->index == MSR_IA32_UCODE_REV)
841+ if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
842+ CC(e->index == MSR_IA32_UCODE_REV))
833843 return -EINVAL;
834− if (e->reserved != 0)
844+ if (CC(e->reserved != 0))
835845 return -EINVAL;
836846 return 0;
837847 }
@@ -839,9 +849,9 @@ static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, · 1 unmodified line
839849 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
840850 struct vmx_msr_entry *e)
841851 {
842− if (e->index == MSR_FS_BASE ||
843− e->index == MSR_GS_BASE ||
844− e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
852+ if (CC(e->index == MSR_FS_BASE) ||
853+ CC(e->index == MSR_GS_BASE) ||
854+ CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
845855 nested_vmx_msr_check_common(vcpu, e))
846856 return -EINVAL;
847857 return 0;
@@ -850,7 +860,7 @@ static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, · 2 unmodified lines
850860 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
851861 struct vmx_msr_entry *e)
852862 {
853− if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
863+ if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
854864 nested_vmx_msr_check_common(vcpu, e))
855865 return -EINVAL;
856866 return 0;
@@ -864,9 +874,7 @@ static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) · 7 unmodified lines
864874 {
865875 u32 i;
866876 struct vmx_msr_entry e;
867− struct msr_data msr;
868877
869− msr.host_initiated = false;
870878 for (i = 0; i < count; i++) {
871879 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
872880 &e, sizeof(e))) {
@@ -881,9 +889,7 @@ static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) · 8 unmodified lines
881889 __func__, i, e.index, e.reserved);
882890 goto fail;
883891 }
884− msr.index = e.index;
885− msr.data = e.value;
886− if (kvm_set_msr(vcpu, &msr)) {
892+ if (kvm_set_msr(vcpu, e.index, e.value)) {
887893 pr_debug_ratelimited(
888894 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
889895 __func__, i, e.index, e.value);
@@ -897,11 +903,11 @@ fail: · 7 unmodified lines
897903
898904 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
899905 {
906+ u64 data;
900907 u32 i;
901908 struct vmx_msr_entry e;
902909
903910 for (i = 0; i < count; i++) {
904− struct msr_data msr_info;
905911 if (kvm_vcpu_read_guest(vcpu,
906912 gpa + i * sizeof(e),
907913 &e, 2 * sizeof(u32))) {
@@ -916,9 +922,7 @@ static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) · 8 unmodified lines
916922 __func__, i, e.index, e.reserved);
917923 return -EINVAL;
918924 }
919− msr_info.host_initiated = false;
920− msr_info.index = e.index;
921− if (kvm_get_msr(vcpu, &msr_info)) {
925+ if (kvm_get_msr(vcpu, e.index, &data)) {
922926 pr_debug_ratelimited(
923927 "%s cannot read MSR (%u, 0x%x)\n",
924928 __func__, i, e.index);
@@ -927,10 +931,10 @@ static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) · 2 unmodified lines
927931 if (kvm_vcpu_write_guest(vcpu,
928932 gpa + i * sizeof(e) +
929933 offsetof(struct vmx_msr_entry, value),
930− &msr_info.data, sizeof(msr_info.data))) {
934+ &data, sizeof(data))) {
931935 pr_debug_ratelimited(
932936 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
933− __func__, i, e.index, msr_info.data);
937+ __func__, i, e.index, data);
934938 return -EINVAL;
935939 }
936940 }
@@ -955,7 +959,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne · 18 unmodified lines
955959 u32 *entry_failure_code)
956960 {
957961 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
958− if (!nested_cr3_valid(vcpu, cr3)) {
962+ if (CC(!nested_cr3_valid(vcpu, cr3))) {
959963 *entry_failure_code = ENTRY_FAIL_DEFAULT;
960964 return -EINVAL;
961965 }
@@ -965,7 +969,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne · 3 unmodified lines
965969 * must not be dereferenced.
966970 */
967971 if (is_pae_paging(vcpu) && !nested_ept) {
968− if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
972+ if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
969973 *entry_failure_code = ENTRY_FAIL_PDPTE;
970974 return -EINVAL;
971975 }
@@ -2411,12 +2415,12 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, · 1439 unmodified lines
24112415
24122416 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
24132417 {
2414− if (!nested_cpu_has_nmi_exiting(vmcs12) &&
2415− nested_cpu_has_virtual_nmis(vmcs12))
2418+ if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2419+ nested_cpu_has_virtual_nmis(vmcs12)))
24162420 return -EINVAL;
24172421
2418− if (!nested_cpu_has_virtual_nmis(vmcs12) &&
2419− nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
2422+ if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
2423+ nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)))
24202424 return -EINVAL;
24212425
24222426 return 0;
@@ -2430,11 +2434,11 @@ static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) · 7 unmodified lines
24302434 /* Check for memory type validity */
24312435 switch (address & VMX_EPTP_MT_MASK) {
24322436 case VMX_EPTP_MT_UC:
2433− if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
2437+ if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
24342438 return false;
24352439 break;
24362440 case VMX_EPTP_MT_WB:
2437− if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
2441+ if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
24382442 return false;
24392443 break;
24402444 default:
@@ -2442,16 +2446,16 @@ static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) · 1 unmodified line
24422446 }
24432447
24442448 /* only 4 levels page-walk length are valid */
2445− if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
2449+ if (CC((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4))
24462450 return false;
24472451
24482452 /* Reserved bits should not be set */
2449− if (address >> maxphyaddr || ((address >> 7) & 0x1f))
2453+ if (CC(address >> maxphyaddr || ((address >> 7) & 0x1f)))
24502454 return false;
24512455
24522456 /* AD, if set, should be supported */
24532457 if (address & VMX_EPTP_AD_ENABLE_BIT) {
2454− if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
2458+ if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
24552459 return false;
24562460 }
24572461
@@ -2466,21 +2470,21 @@ static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, · 8 unmodified lines
24662470 {
24672471 struct vcpu_vmx *vmx = to_vmx(vcpu);
24682472
2469− if (!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2470− vmx->nested.msrs.pinbased_ctls_low,
2471− vmx->nested.msrs.pinbased_ctls_high) ||
2472− !vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2473− vmx->nested.msrs.procbased_ctls_low,
2474− vmx->nested.msrs.procbased_ctls_high))
2473+ if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2474+ vmx->nested.msrs.pinbased_ctls_low,
2475+ vmx->nested.msrs.pinbased_ctls_high)) ||
2476+ CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2477+ vmx->nested.msrs.procbased_ctls_low,
2478+ vmx->nested.msrs.procbased_ctls_high)))
24752479 return -EINVAL;
24762480
24772481 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
2478− !vmx_control_verify(vmcs12->secondary_vm_exec_control,
2479− vmx->nested.msrs.secondary_ctls_low,
2480− vmx->nested.msrs.secondary_ctls_high))
2482+ CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2483+ vmx->nested.msrs.secondary_ctls_low,
2484+ vmx->nested.msrs.secondary_ctls_high)))
24812485 return -EINVAL;
24822486
2483− if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu) ||
2487+ if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
24842488 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
24852489 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
24862490 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
@@ -2491,7 +2495,7 @@ static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, · 4 unmodified lines
24912495 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
24922496 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
24932497 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
2494− (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
2498+ CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
24952499 return -EINVAL;
24962500
24972501 if (!nested_cpu_has_preemption_timer(vmcs12) &&
@@ -2499,17 +2503,17 @@ static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, · 1 unmodified line
24992503 return -EINVAL;
25002504
25012505 if (nested_cpu_has_ept(vmcs12) &&
2502− !valid_ept_address(vcpu, vmcs12->ept_pointer))
2506+ CC(!valid_ept_address(vcpu, vmcs12->ept_pointer)))
25032507 return -EINVAL;
25042508
25052509 if (nested_cpu_has_vmfunc(vmcs12)) {
2506− if (vmcs12->vm_function_control &
2507− ~vmx->nested.msrs.vmfunc_controls)
2510+ if (CC(vmcs12->vm_function_control &
2511+ ~vmx->nested.msrs.vmfunc_controls))
25082512 return -EINVAL;
25092513
25102514 if (nested_cpu_has_eptp_switching(vmcs12)) {
2511− if (!nested_cpu_has_ept(vmcs12) ||
2512− !page_address_valid(vcpu, vmcs12->eptp_list_address))
2515+ if (CC(!nested_cpu_has_ept(vmcs12)) ||
2516+ CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
25132517 return -EINVAL;
25142518 }
25152519 }
@@ -2525,10 +2529,10 @@ static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, · 9 unmodified lines
25252529 {
25262530 struct vcpu_vmx *vmx = to_vmx(vcpu);
25272531
2528− if (!vmx_control_verify(vmcs12->vm_exit_controls,
2529− vmx->nested.msrs.exit_ctls_low,
2530− vmx->nested.msrs.exit_ctls_high) ||
2531− nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))
2532+ if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2533+ vmx->nested.msrs.exit_ctls_low,
2534+ vmx->nested.msrs.exit_ctls_high)) ||
2535+ CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
25322536 return -EINVAL;
25332537
25342538 return 0;
@@ -2542,9 +2546,9 @@ static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, · 7 unmodified lines
25422546 {
25432547 struct vcpu_vmx *vmx = to_vmx(vcpu);
25442548
2545− if (!vmx_control_verify(vmcs12->vm_entry_controls,
2546− vmx->nested.msrs.entry_ctls_low,
2547− vmx->nested.msrs.entry_ctls_high))
2549+ if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2550+ vmx->nested.msrs.entry_ctls_low,
2551+ vmx->nested.msrs.entry_ctls_high)))
25482552 return -EINVAL;
25492553
25502554 /*
@@ -2564,31 +2568,31 @@ static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, · 13 unmodified lines
25642568 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
25652569
25662570 /* VM-entry interruption-info field: interruption type */
2567− if (intr_type == INTR_TYPE_RESERVED ||
2568− (intr_type == INTR_TYPE_OTHER_EVENT &&
2569− !nested_cpu_supports_monitor_trap_flag(vcpu)))
2571+ if (CC(intr_type == INTR_TYPE_RESERVED) ||
2572+ CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2573+ !nested_cpu_supports_monitor_trap_flag(vcpu)))
25702574 return -EINVAL;
25712575
25722576 /* VM-entry interruption-info field: vector */
2573− if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2574− (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2575− (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
2577+ if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2578+ CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2579+ CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
25762580 return -EINVAL;
25772581
25782582 /* VM-entry interruption-info field: deliver error code */
25792583 should_have_error_code =
25802584 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
25812585 x86_exception_has_error_code(vector);
2582− if (has_error_code != should_have_error_code)
2586+ if (CC(has_error_code != should_have_error_code))
25832587 return -EINVAL;
25842588
25852589 /* VM-entry exception error code */
2586− if (has_error_code &&
2587− vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
2590+ if (CC(has_error_code &&
2591+ vmcs12->vm_entry_exception_error_code & GENMASK(31, 15)))
25882592 return -EINVAL;
25892593
25902594 /* VM-entry interruption-info field: reserved bits */
2591− if (intr_info & INTR_INFO_RESVD_BITS_MASK)
2595+ if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
25922596 return -EINVAL;
25932597
25942598 /* VM-entry instruction length */
@@ -2596,9 +2600,9 @@ static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, · 1 unmodified line
25962600 case INTR_TYPE_SOFT_EXCEPTION:
25972601 case INTR_TYPE_SOFT_INTR:
25982602 case INTR_TYPE_PRIV_SW_EXCEPTION:
2599− if ((vmcs12->vm_entry_instruction_len > 15) ||
2600− (vmcs12->vm_entry_instruction_len == 0 &&
2601− !nested_cpu_has_zero_length_injection(vcpu)))
2603+ if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2604+ CC(vmcs12->vm_entry_instruction_len == 0 &&
2605+ CC(!nested_cpu_has_zero_length_injection(vcpu))))
26022606 return -EINVAL;
26032607 }
26042608 }
@@ -2625,40 +2629,40 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, · 20 unmodified lines
26252629 {
26262630 bool ia32e;
26272631
2628− if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
2629− !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
2630− !nested_cr3_valid(vcpu, vmcs12->host_cr3))
2632+ if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2633+ CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
2634+ CC(!nested_cr3_valid(vcpu, vmcs12->host_cr3)))
26312635 return -EINVAL;
26322636
2633− if (is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu) ||
2634− is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))
2637+ if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2638+ CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
26352639 return -EINVAL;
26362640
26372641 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
2638− !kvm_pat_valid(vmcs12->host_ia32_pat))
2642+ CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
26392643 return -EINVAL;
26402644
26412645 ia32e = (vmcs12->vm_exit_controls &
26422646 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
26432647
2644− if (vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
2645− vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
2646− vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
2647− vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
2648− vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
2649− vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
2650− vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
2651− vmcs12->host_cs_selector == 0 ||
2652− vmcs12->host_tr_selector == 0 ||
2653− (vmcs12->host_ss_selector == 0 && !ia32e))
2648+ if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2649+ CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2650+ CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2651+ CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2652+ CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2653+ CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2654+ CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2655+ CC(vmcs12->host_cs_selector == 0) ||
2656+ CC(vmcs12->host_tr_selector == 0) ||
2657+ CC(vmcs12->host_ss_selector == 0 && !ia32e))
26542658 return -EINVAL;
26552659
26562660 #ifdef CONFIG_X86_64
2657− if (is_noncanonical_address(vmcs12->host_fs_base, vcpu) ||
2658− is_noncanonical_address(vmcs12->host_gs_base, vcpu) ||
2659− is_noncanonical_address(vmcs12->host_gdtr_base, vcpu) ||
2660− is_noncanonical_address(vmcs12->host_idtr_base, vcpu) ||
2661− is_noncanonical_address(vmcs12->host_tr_base, vcpu))
2661+ if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2662+ CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2663+ CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2664+ CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
2665+ CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)))
26622666 return -EINVAL;
26632667 #endif
26642668
@@ -2669,9 +2673,9 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, · 4 unmodified lines
26692673 * the host address-space size VM-exit control.
26702674 */
26712675 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
2672− if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
2673− ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
2674− ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
2676+ if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2677+ CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2678+ CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
26752679 return -EINVAL;
26762680 }
26772681
@@ -2688,16 +2692,16 @@ static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, · 10 unmodified lines
26882692 if (vmcs12->vmcs_link_pointer == -1ull)
26892693 return 0;
26902694
2691− if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
2695+ if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
26922696 return -EINVAL;
26932697
2694− if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
2698+ if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
26952699 return -EINVAL;
26962700
26972701 shadow = map.hva;
26982702
2699− if (shadow->hdr.revision_id != VMCS12_REVISION ||
2700− shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
2703+ if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2704+ CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
27012705 r = -EINVAL;
27022706
27032707 kvm_vcpu_unmap(vcpu, &map, false);
@@ -2709,8 +2713,8 @@ static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, · 5 unmodified lines
27092713 */
27102714 static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
27112715 {
2712− if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
2713− vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
2716+ if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
2717+ vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT))
27142718 return -EINVAL;
27152719
27162720 return 0;
@@ -2724,12 +2728,12 @@ static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, · 7 unmodified lines
27242728
27252729 *exit_qual = ENTRY_FAIL_DEFAULT;
27262730
2727− if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
2728− !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
2731+ if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2732+ CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
27292733 return -EINVAL;
27302734
27312735 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
2732− !kvm_pat_valid(vmcs12->guest_ia32_pat))
2736+ CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
27332737 return -EINVAL;
27342738
27352739 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
@@ -2749,16 +2753,16 @@ static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, · 13 unmodified lines
27492753 if (to_vmx(vcpu)->nested.nested_run_pending &&
27502754 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
27512755 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
2752− if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
2753− ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
2754− ((vmcs12->guest_cr0 & X86_CR0_PG) &&
2755− ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
2756+ if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
2757+ CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
2758+ CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
2759+ ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
27562760 return -EINVAL;
27572761 }
27582762
27592763 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
2760− (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
2761− (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
2764+ (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
2765+ CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
27622766 return -EINVAL;
27632767
27642768 if (nested_check_guest_non_reg_state(vmcs12))
@@ -2841,9 +2845,13 @@ static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) · 76 unmodified lines
28412845 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
28422846
28432847 if (vm_fail) {
2848+ u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
2849+
28442850 preempt_enable();
2845− WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
2846− VMXERR_ENTRY_INVALID_CONTROL_FIELD);
2851+
2852+ trace_kvm_nested_vmenter_failed(
2853+ "early hardware check VM-instruction error: ", error);
2854+ WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
28472855 return 1;
28482856 }
28492857
@@ -3401,6 +3409,15 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) · 551 unmodified lines
34013409 unsigned long exit_qual;
34023410 bool block_nested_events =
34033411 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
3412+ struct kvm_lapic *apic = vcpu->arch.apic;
3413+
3414+ if (lapic_in_kernel(vcpu) &&
3415+ test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3416+ if (block_nested_events)
3417+ return -EBUSY;
3418+ nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3419+ return 0;
3420+ }
34043421
34053422 if (vcpu->arch.exception.pending &&
34063423 nested_vmx_check_exception(vcpu, &exit_qual)) {
@@ -3889,7 +3906,6 @@ static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) · 482 unmodified lines
38893906 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
38903907 struct vcpu_vmx *vmx = to_vmx(vcpu);
38913908 struct vmx_msr_entry g, h;
3892− struct msr_data msr;
38933909 gpa_t gpa;
38943910 u32 i, j;
38953911
@@ -3949,7 +3965,6 @@ static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) · 53 unmodified lines
39493965 * from the guest value. The intent is to stuff host state as
39503966 * silently as possible, not to fully process the exit load list.
39513967 */
3952− msr.host_initiated = false;
39533968 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
39543969 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
39553970 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
@@ -3979,9 +3994,7 @@ static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) · 23 unmodified lines
39793994 goto vmabort;
39803995 }
39813996
3982− msr.index = h.index;
3983− msr.data = h.value;
3984− if (kvm_set_msr(vcpu, &msr)) {
3997+ if (kvm_set_msr(vcpu, h.index, h.value)) {
39853998 pr_debug_ratelimited(
39863999 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
39874000 __func__, j, h.index, h.value);
@@ -4466,7 +4479,12 @@ static int handle_vmoff(struct kvm_vcpu *vcpu) · 478 unmodified lines
44664479 {
44674480 if (!nested_vmx_check_permission(vcpu))
44684481 return 1;
4482+
44694483 free_nested(vcpu);
4484+
4485+ /* Process a latched INIT during time CPU was in VMX operation */
4486+ kvm_make_request(KVM_REQ_EVENT, vcpu);
4487+
44704488 return nested_vmx_succeed(vcpu);
44714489 }
44724490
@@ -5261,8 +5279,9 @@ bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason) · 788 unmodified lines
52615279 return false;
52625280
52635281 if (unlikely(vmx->fail)) {
5264− pr_info_ratelimited("%s failed vm entry %x\n", __func__,
5265− vmcs_read32(VM_INSTRUCTION_ERROR));
5282+ trace_kvm_nested_vmenter_failed(
5283+ "hardware VM-instruction error: ",
5284+ vmcs_read32(VM_INSTRUCTION_ERROR));
52665285 return true;
52675286 }
52685287
arch/x86/kvm/vmx/vmenter.S+2−2
@@ -94,7 +94,7 @@ ENDPROC(vmx_vmexit) · 93 unmodified lines
9494
9595 /**
9696 * __vmx_vcpu_run - Run a vCPU via a transition to VMX guest mode
97− * @vmx: struct vcpu_vmx *
97+ * @vmx: struct vcpu_vmx * (forwarded to vmx_update_host_rsp)
9898 * @regs: unsigned long * (to guest registers)
9999 * @launched: %true if the VMCS has been launched
100100 *
@@ -151,7 +151,7 @@ ENTRY(__vmx_vcpu_run) · 50 unmodified lines
151151 mov VCPU_R14(%_ASM_AX), %r14
152152 mov VCPU_R15(%_ASM_AX), %r15
153153 #endif
154− /* Load guest RAX. This kills the vmx_vcpu pointer! */
154+ /* Load guest RAX. This kills the @regs pointer! */
155155 mov VCPU_RAX(%_ASM_AX), %_ASM_AX
156156
157157 /* Enter guest mode */
arch/x86/kvm/vmx/vmx.c+48−46
@@ -1472,8 +1472,11 @@ static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data) · 1471 unmodified lines
14721472 return 0;
14731473 }
14741474
1475−
1476−static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1475+/*
1476+ * Returns an int to be compatible with SVM implementation (which can fail).
1477+ * Do not use directly, use skip_emulated_instruction() instead.
1478+ */
1479+static int __skip_emulated_instruction(struct kvm_vcpu *vcpu)
14771480 {
14781481 unsigned long rip;
14791482
@@ -1483,6 +1486,13 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu) · 3 unmodified lines
14831486
14841487 /* skipping an emulated instruction also counts */
14851488 vmx_set_interrupt_shadow(vcpu, 0);
1489+
1490+ return EMULATE_DONE;
1491+}
1492+
1493+static inline void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1494+{
1495+ (void)__skip_emulated_instruction(vcpu);
14861496 }
14871497
14881498 static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
@@ -4026,7 +4036,7 @@ static void ept_set_mmio_spte_mask(void) · 2537 unmodified lines
40264036 * of an EPT paging-structure entry is 110b (write/execute).
40274037 */
40284038 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
4029− VMX_EPT_MISCONFIG_WX_VALUE);
4039+ VMX_EPT_MISCONFIG_WX_VALUE, 0);
40304040 }
40314041
40324042 #define VMX_XSS_EXIT_BITMAP 0
@@ -4152,6 +4162,7 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · 119 unmodified lines
41524162
41534163 vcpu->arch.microcode_version = 0x100000000ULL;
41544164 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
4165+ vmx->hv_deadline_tsc = -1;
41554166 kvm_set_cr8(vcpu, 0);
41564167
41574168 if (!init_event) {
@@ -4856,41 +4867,12 @@ static int handle_cpuid(struct kvm_vcpu *vcpu) · 698 unmodified lines
48564867
48574868 static int handle_rdmsr(struct kvm_vcpu *vcpu)
48584869 {
4859− u32 ecx = kvm_rcx_read(vcpu);
4860− struct msr_data msr_info;
4861−
4862− msr_info.index = ecx;
4863− msr_info.host_initiated = false;
4864− if (vmx_get_msr(vcpu, &msr_info)) {
4865− trace_kvm_msr_read_ex(ecx);
4866− kvm_inject_gp(vcpu, 0);
4867− return 1;
4868− }
4869−
4870− trace_kvm_msr_read(ecx, msr_info.data);
4871−
4872− kvm_rax_write(vcpu, msr_info.data & -1u);
4873− kvm_rdx_write(vcpu, (msr_info.data >> 32) & -1u);
4874− return kvm_skip_emulated_instruction(vcpu);
4870+ return kvm_emulate_rdmsr(vcpu);
48754871 }
48764872
48774873 static int handle_wrmsr(struct kvm_vcpu *vcpu)
48784874 {
4879− struct msr_data msr;
4880− u32 ecx = kvm_rcx_read(vcpu);
4881− u64 data = kvm_read_edx_eax(vcpu);
4882−
4883− msr.data = data;
4884− msr.index = ecx;
4885− msr.host_initiated = false;
4886− if (kvm_set_msr(vcpu, &msr) != 0) {
4887− trace_kvm_msr_write_ex(ecx, data);
4888− kvm_inject_gp(vcpu, 0);
4889− return 1;
4890− }
4891−
4892− trace_kvm_msr_write(ecx, data);
4893− return kvm_skip_emulated_instruction(vcpu);
4875+ return kvm_emulate_wrmsr(vcpu);
48944876 }
48954877
48964878 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
@@ -5227,31 +5209,33 @@ emulation_error: · 330 unmodified lines
52275209 static void grow_ple_window(struct kvm_vcpu *vcpu)
52285210 {
52295211 struct vcpu_vmx *vmx = to_vmx(vcpu);
5230− int old = vmx->ple_window;
5212+ unsigned int old = vmx->ple_window;
52315213
52325214 vmx->ple_window = __grow_ple_window(old, ple_window,
52335215 ple_window_grow,
52345216 ple_window_max);
52355217
5236− if (vmx->ple_window != old)
5218+ if (vmx->ple_window != old) {
52375219 vmx->ple_window_dirty = true;
5238−
5239− trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
5220+ trace_kvm_ple_window_update(vcpu->vcpu_id,
5221+ vmx->ple_window, old);
5222+ }
52405223 }
52415224
52425225 static void shrink_ple_window(struct kvm_vcpu *vcpu)
52435226 {
52445227 struct vcpu_vmx *vmx = to_vmx(vcpu);
5245− int old = vmx->ple_window;
5228+ unsigned int old = vmx->ple_window;
52465229
52475230 vmx->ple_window = __shrink_ple_window(old, ple_window,
52485231 ple_window_shrink,
52495232 ple_window);
52505233
5251− if (vmx->ple_window != old)
5234+ if (vmx->ple_window != old) {
52525235 vmx->ple_window_dirty = true;
5253−
5254− trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
5236+ trace_kvm_ple_window_update(vcpu->vcpu_id,
5237+ vmx->ple_window, old);
5238+ }
52555239 }
52565240
52575241 /*
@@ -5887,8 +5871,13 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu) · 629 unmodified lines
58875871 else {
58885872 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
58895873 exit_reason);
5890− kvm_queue_exception(vcpu, UD_VECTOR);
5891− return 1;
5874+ dump_vmcs();
5875+ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5876+ vcpu->run->internal.suberror =
5877+ KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
5878+ vcpu->run->internal.ndata = 1;
5879+ vcpu->run->internal.data[0] = exit_reason;
5880+ return 0;
58925881 }
58935882 }
58945883
@@ -6615,6 +6604,9 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) · 720 unmodified lines
66156604 unsigned long *msr_bitmap;
66166605 int cpu;
66176606
6607+ BUILD_BUG_ON_MSG(offsetof(struct vcpu_vmx, vcpu) != 0,
6608+ "struct kvm_vcpu must be at offset 0 for arch usercopy region");
6609+
66186610 vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
66196611 if (!vmx)
66206612 return ERR_PTR(-ENOMEM);
@@ -7369,10 +7361,14 @@ static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, · 748 unmodified lines
73697361 * irqbalance to make the interrupts single-CPU.
73707362 *
73717363 * We will support full lowest-priority interrupt later.
7364+ *
7365+ * In addition, we can only inject generic interrupts using
7366+ * the PI mechanism, refuse to route others through it.
73727367 */
73737368
73747369 kvm_set_msi_irq(kvm, e, &irq);
7375− if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
7370+ if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
7371+ !kvm_irq_is_postable(&irq)) {
73767372 /*
73777373 * Make sure the IRTE is in remapped mode if
73787374 * we don't handle it in posted mode.
@@ -7474,6 +7470,11 @@ static bool vmx_need_emulation_on_page_fault(struct kvm_vcpu *vcpu) · 95 unmodified lines
74747470 return false;
74757471 }
74767472
7473+static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7474+{
7475+ return to_vmx(vcpu)->nested.vmxon;
7476+}
7477+
74777478 static __init int hardware_setup(void)
74787479 {
74797480 unsigned long host_bndcfgs;
@@ -7705,7 +7706,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { · 225 unmodified lines
77057706
77067707 .run = vmx_vcpu_run,
77077708 .handle_exit = vmx_handle_exit,
7708− .skip_emulated_instruction = skip_emulated_instruction,
7709+ .skip_emulated_instruction = __skip_emulated_instruction,
77097710 .set_interrupt_shadow = vmx_set_interrupt_shadow,
77107711 .get_interrupt_shadow = vmx_get_interrupt_shadow,
77117712 .patch_hypercall = vmx_patch_hypercall,
@@ -7799,6 +7800,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { · 87 unmodified lines
77997800 .nested_enable_evmcs = NULL,
78007801 .nested_get_evmcs_version = NULL,
78017802 .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
7803+ .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
78027804 };
78037805
78047806 static void vmx_cleanup_l1d_flush(void)
arch/x86/kvm/vmx/vmx.h+1−1
@@ -253,7 +253,7 @@ struct vcpu_vmx { · 252 unmodified lines
253253 struct nested_vmx nested;
254254
255255 /* Dynamic PLE window. */
256− int ple_window;
256+ unsigned int ple_window;
257257 bool ple_window_dirty;
258258
259259 bool req_immediate_exit;
arch/x86/kvm/x86.c+128−69
@@ -674,8 +674,14 @@ static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, · 673 unmodified lines
674674 data, offset, len, access);
675675 }
676676
677+static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
678+{
679+ return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
680+ rsvd_bits(1, 2);
681+}
682+
677683 /*
678− * Load the pae pdptrs. Return true is they are all valid.
684+ * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
679685 */
680686 int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
681687 {
@@ -694,8 +700,7 @@ int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3) · 12 unmodified lines
694700 }
695701 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
696702 if ((pdpte[i] & PT_PRESENT_MASK) &&
697− (pdpte[i] &
698− vcpu->arch.mmu->guest_rsvd_check.rsvd_bits_mask[0][2])) {
703+ (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
699704 ret = 0;
700705 goto out;
701706 }
@@ -1254,6 +1259,13 @@ static u64 kvm_get_arch_capabilities(void) · 552 unmodified lines
12541259 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
12551260 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
12561261
1262+ if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1263+ data |= ARCH_CAP_RDCL_NO;
1264+ if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1265+ data |= ARCH_CAP_SSB_NO;
1266+ if (!boot_cpu_has_bug(X86_BUG_MDS))
1267+ data |= ARCH_CAP_MDS_NO;
1268+
12571269 return data;
12581270 }
12591271
@@ -1351,19 +1363,23 @@ void kvm_enable_efer_bits(u64 mask) · 91 unmodified lines
13511363 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
13521364
13531365 /*
1354− * Writes msr value into into the appropriate "register".
1366+ * Write @data into the MSR specified by @index. Select MSR specific fault
1367+ * checks are bypassed if @host_initiated is %true.
13551368 * Returns 0 on success, non-0 otherwise.
13561369 * Assumes vcpu_load() was already called.
13571370 */
1358−int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
1371+static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1372+ bool host_initiated)
13591373 {
1360− switch (msr->index) {
1374+ struct msr_data msr;
1375+
1376+ switch (index) {
13611377 case MSR_FS_BASE:
13621378 case MSR_GS_BASE:
13631379 case MSR_KERNEL_GS_BASE:
13641380 case MSR_CSTAR:
13651381 case MSR_LSTAR:
1366− if (is_noncanonical_address(msr->data, vcpu))
1382+ if (is_noncanonical_address(data, vcpu))
13671383 return 1;
13681384 break;
13691385 case MSR_IA32_SYSENTER_EIP:
@@ -1380,38 +1396,95 @@ int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) · 10 unmodified lines
13801396 * value, and that something deterministic happens if the guest
13811397 * invokes 64-bit SYSENTER.
13821398 */
1383− msr->data = get_canonical(msr->data, vcpu_virt_addr_bits(vcpu));
1399+ data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
13841400 }
1385− return kvm_x86_ops->set_msr(vcpu, msr);
1401+
1402+ msr.data = data;
1403+ msr.index = index;
1404+ msr.host_initiated = host_initiated;
1405+
1406+ return kvm_x86_ops->set_msr(vcpu, &msr);
13861407 }
1387−EXPORT_SYMBOL_GPL(kvm_set_msr);
13881408
13891409 /*
1390− * Adapt set_msr() to msr_io()'s calling convention
1410+ * Read the MSR specified by @index into @data. Select MSR specific fault
1411+ * checks are bypassed if @host_initiated is %true.
1412+ * Returns 0 on success, non-0 otherwise.
1413+ * Assumes vcpu_load() was already called.
13911414 */
1392−static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1415+static int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1416+ bool host_initiated)
13931417 {
13941418 struct msr_data msr;
1395− int r;
1419+ int ret;
13961420
13971421 msr.index = index;
1398− msr.host_initiated = true;
1399− r = kvm_get_msr(vcpu, &msr);
1400− if (r)
1401− return r;
1422+ msr.host_initiated = host_initiated;
14021423
1403− *data = msr.data;
1404− return 0;
1424+ ret = kvm_x86_ops->get_msr(vcpu, &msr);
1425+ if (!ret)
1426+ *data = msr.data;
1427+ return ret;
14051428 }
14061429
1407−static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1430+int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
14081431 {
1409− struct msr_data msr;
1432+ return __kvm_get_msr(vcpu, index, data, false);
1433+}
1434+EXPORT_SYMBOL_GPL(kvm_get_msr);
14101435
1411− msr.data = *data;
1412− msr.index = index;
1413− msr.host_initiated = true;
1414− return kvm_set_msr(vcpu, &msr);
1436+int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1437+{
1438+ return __kvm_set_msr(vcpu, index, data, false);
1439+}
1440+EXPORT_SYMBOL_GPL(kvm_set_msr);
1441+
1442+int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1443+{
1444+ u32 ecx = kvm_rcx_read(vcpu);
1445+ u64 data;
1446+
1447+ if (kvm_get_msr(vcpu, ecx, &data)) {
1448+ trace_kvm_msr_read_ex(ecx);
1449+ kvm_inject_gp(vcpu, 0);
1450+ return 1;
1451+ }
1452+
1453+ trace_kvm_msr_read(ecx, data);
1454+
1455+ kvm_rax_write(vcpu, data & -1u);
1456+ kvm_rdx_write(vcpu, (data >> 32) & -1u);
1457+ return kvm_skip_emulated_instruction(vcpu);
1458+}
1459+EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1460+
1461+int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1462+{
1463+ u32 ecx = kvm_rcx_read(vcpu);
1464+ u64 data = kvm_read_edx_eax(vcpu);
1465+
1466+ if (kvm_set_msr(vcpu, ecx, data)) {
1467+ trace_kvm_msr_write_ex(ecx, data);
1468+ kvm_inject_gp(vcpu, 0);
1469+ return 1;
1470+ }
1471+
1472+ trace_kvm_msr_write(ecx, data);
1473+ return kvm_skip_emulated_instruction(vcpu);
1474+}
1475+EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1476+
1477+/*
1478+ * Adapt set_msr() to msr_io()'s calling convention
1479+ */
1480+static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1481+{
1482+ return __kvm_get_msr(vcpu, index, data, true);
1483+}
1484+
1485+static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1486+{
1487+ return __kvm_set_msr(vcpu, index, *data, true);
14151488 }
14161489
14171490 #ifdef CONFIG_X86_64
@@ -2452,6 +2525,8 @@ static void record_steal_time(struct kvm_vcpu *vcpu) · 1034 unmodified lines
24522525 * Doing a TLB flush here, on the guest's behalf, can avoid
24532526 * expensive IPIs.
24542527 */
2528+ trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
2529+ vcpu->arch.st.steal.preempted & KVM_VCPU_FLUSH_TLB);
24552530 if (xchg(&vcpu->arch.st.steal.preempted, 0) & KVM_VCPU_FLUSH_TLB)
24562531 kvm_vcpu_flush_tlb(vcpu, false);
24572532
@@ -2748,18 +2823,6 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · 290 unmodified lines
27482823 }
27492824 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
27502825
2751−
2752−/*
2753− * Reads an msr value (of 'msr_index') into 'pdata'.
2754− * Returns 0 on success, non-0 otherwise.
2755− * Assumes vcpu_load() was already called.
2756− */
2757−int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
2758−{
2759− return kvm_x86_ops->get_msr(vcpu, msr);
2760−}
2761−EXPORT_SYMBOL_GPL(kvm_get_msr);
2762−
27632826 static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
27642827 {
27652828 u64 data;
@@ -3506,8 +3569,7 @@ static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, · 740 unmodified lines
35063569 for (bank = 0; bank < bank_num; bank++)
35073570 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
35083571
3509− if (kvm_x86_ops->setup_mce)
3510− kvm_x86_ops->setup_mce(vcpu);
3572+ kvm_x86_ops->setup_mce(vcpu);
35113573 out:
35123574 return r;
35133575 }
@@ -5377,7 +5439,7 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, · 1863 unmodified lines
53775439 */
53785440 if (vcpu_match_mmio_gva(vcpu, gva)
53795441 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
5380− vcpu->arch.access, 0, access)) {
5442+ vcpu->arch.mmio_access, 0, access)) {
53815443 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
53825444 (gva & (PAGE_SIZE - 1));
53835445 trace_vcpu_match_mmio(gva, *gpa, write, false);
@@ -5971,28 +6033,13 @@ static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector, · 587 unmodified lines
59716033 static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
59726034 u32 msr_index, u64 *pdata)
59736035 {
5974− struct msr_data msr;
5975− int r;
5976−
5977− msr.index = msr_index;
5978− msr.host_initiated = false;
5979− r = kvm_get_msr(emul_to_vcpu(ctxt), &msr);
5980− if (r)
5981− return r;
5982−
5983− *pdata = msr.data;
5984− return 0;
6036+ return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
59856037 }
59866038
59876039 static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
59886040 u32 msr_index, u64 data)
59896041 {
5990− struct msr_data msr;
5991−
5992− msr.data = data;
5993− msr.index = msr_index;
5994− msr.host_initiated = false;
5995− return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
6042+ return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
59966043 }
59976044
59986045 static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
@@ -6075,6 +6122,11 @@ static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt) · 76 unmodified lines
60756122 kvm_smm_changed(emul_to_vcpu(ctxt));
60766123 }
60776124
6125+static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6126+{
6127+ return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6128+}
6129+
60786130 static const struct x86_emulate_ops emulate_ops = {
60796131 .read_gpr = emulator_read_gpr,
60806132 .write_gpr = emulator_write_gpr,
@@ -6116,6 +6168,7 @@ static const struct x86_emulate_ops emulate_ops = { · 35 unmodified lines
61166168 .set_hflags = emulator_set_hflags,
61176169 .pre_leave_smm = emulator_pre_leave_smm,
61186170 .post_leave_smm = emulator_post_leave_smm,
6171+ .set_xcr = emulator_set_xcr,
61196172 };
61206173
61216174 static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
@@ -6390,9 +6443,11 @@ static void kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu, int *r) · 268 unmodified lines
63906443 int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
63916444 {
63926445 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
6393− int r = EMULATE_DONE;
6446+ int r;
63946447
6395− kvm_x86_ops->skip_emulated_instruction(vcpu);
6448+ r = kvm_x86_ops->skip_emulated_instruction(vcpu);
6449+ if (unlikely(r != EMULATE_DONE))
6450+ return 0;
63966451
63976452 /*
63986453 * rflags is the old, "raw" value of the flags. The new value has
@@ -6528,8 +6583,16 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, · 129 unmodified lines
65286583 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
65296584 emulation_type))
65306585 return EMULATE_DONE;
6531− if (ctxt->have_exception && inject_emulated_exception(vcpu))
6586+ if (ctxt->have_exception) {
6587+ /*
6588+ * #UD should result in just EMULATION_FAILED, and trap-like
6589+ * exception should not be encountered during decode.
6590+ */
6591+ WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6592+ exception_type(ctxt->exception.vector) == EXCPT_TRAP);
6593+ inject_emulated_exception(vcpu);
65326594 return EMULATE_DONE;
6595+ }
65336596 if (emulation_type & EMULTYPE_SKIP)
65346597 return EMULATE_FAIL;
65356598 return handle_emulation_failure(vcpu, emulation_type);
@@ -6544,6 +6607,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, · 8 unmodified lines
65446607 kvm_rip_write(vcpu, ctxt->_eip);
65456608 if (ctxt->eflags & X86_EFLAGS_RF)
65466609 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
6610+ kvm_x86_ops->set_interrupt_shadow(vcpu, 0);
65476611 return EMULATE_DONE;
65486612 }
65496613
@@ -9322,10 +9386,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · 2772 unmodified lines
93229386 kvm_page_track_init(kvm);
93239387 kvm_mmu_init_vm(kvm);
93249388
9325− if (kvm_x86_ops->vm_init)
9326− return kvm_x86_ops->vm_init(kvm);
9327−
9328− return 0;
9389+ return kvm_x86_ops->vm_init(kvm);
93299390 }
93309391
93319392 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
@@ -10017,7 +10078,7 @@ EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma); · 685 unmodified lines
1001710078
1001810079 bool kvm_arch_has_irq_bypass(void)
1001910080 {
10020− return kvm_x86_ops->update_pi_irte != NULL;
10081+ return true;
1002110082 }
1002210083
1002310084 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
@@ -10057,9 +10118,6 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, · 33 unmodified lines
1005710118 int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
1005810119 uint32_t guest_irq, bool set)
1005910120 {
10060− if (!kvm_x86_ops->update_pi_irte)
10061− return -EINVAL;
10062−
1006310121 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
1006410122 }
1006510123
@@ -10086,11 +10144,12 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); · 20 unmodified lines
1008610144 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
1008710145 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
1008810146 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
10147+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
1008910148 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
1009010149 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
1009110150 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
1009210151 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
10093−EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
10152+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
1009410153 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
1009510154 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
1009610155 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
arch/x86/kvm/x86.h+1−1
@@ -196,7 +196,7 @@ static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu, · 195 unmodified lines
196196 * actually a nGPA.
197197 */
198198 vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
199− vcpu->arch.access = access;
199+ vcpu->arch.mmio_access = access;
200200 vcpu->arch.mmio_gfn = gfn;
201201 vcpu->arch.mmio_gen = gen;
202202 }
include/kvm/arm_vgic.h+3−1
@@ -249,6 +249,9 @@ struct vgic_dist { · 248 unmodified lines
249249 struct list_head lpi_list_head;
250250 int lpi_list_count;
251251
252+ /* LPI translation cache */
253+ struct list_head lpi_translation_cache;
254+
252255 /* used by vgic-debug */
253256 struct vgic_state_iter *iter;
254257
@@ -311,7 +314,6 @@ struct vgic_cpu { · 56 unmodified lines
311314 * parts of the redistributor.
312315 */
313316 struct vgic_io_device rd_iodev;
314− struct vgic_io_device sgi_iodev;
315317 struct vgic_redist_region *rdreg;
316318
317319 /* Contains the attributes and gpa of the LPI pending tables. */
include/uapi/linux/kvm.h+3−0
@@ -243,6 +243,8 @@ struct kvm_hyperv_exit { · 242 unmodified lines
243243 #define KVM_INTERNAL_ERROR_SIMUL_EX 2
244244 /* Encounter unexpected vm-exit due to delivery event. */
245245 #define KVM_INTERNAL_ERROR_DELIVERY_EV 3
246+/* Encounter unexpected vm-exit reason */
247+#define KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON 4
246248
247249 /* for KVM_RUN, returned by mmap(vcpu_fd, offset=0) */
248250 struct kvm_run {
@@ -996,6 +998,7 @@ struct kvm_ppc_resize_hpt { · 747 unmodified lines
996998 #define KVM_CAP_ARM_PTRAUTH_ADDRESS 171
997999 #define KVM_CAP_ARM_PTRAUTH_GENERIC 172
9981000 #define KVM_CAP_PMU_EVENT_FILTER 173
1001+#define KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 174
9991002
10001003 #ifdef KVM_CAP_IRQ_ROUTING
10011004
tools/testing/selftests/kvm/Makefile+6−4
@@ -7,10 +7,10 @@ top_srcdir = ../../../.. · 6 unmodified lines
77 KSFT_KHDR_INSTALL := 1
88 UNAME_M := $(shell uname -m)
99
10−LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/ucall.c lib/sparsebit.c
11−LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c
12−LIBKVM_aarch64 = lib/aarch64/processor.c
13−LIBKVM_s390x = lib/s390x/processor.c
10+LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c
11+LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/ucall.c
12+LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c
13+LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c
1414
1515 TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test
1616 TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test
@@ -32,7 +32,9 @@ TEST_GEN_PROGS_aarch64 += clear_dirty_log_test · 15 unmodified lines
3232 TEST_GEN_PROGS_aarch64 += dirty_log_test
3333 TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
3434
35+TEST_GEN_PROGS_s390x = s390x/memop
3536 TEST_GEN_PROGS_s390x += s390x/sync_regs_test
37+TEST_GEN_PROGS_s390x += dirty_log_test
3638 TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
3739
3840 TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
tools/testing/selftests/kvm/dirty_log_test.c+53−8
@@ -26,8 +26,8 @@ · 25 unmodified lines
2626 /* The memory slot index to track dirty pages */
2727 #define TEST_MEM_SLOT_INDEX 1
2828
29−/* Default guest test memory offset, 1G */
30−#define DEFAULT_GUEST_TEST_MEM 0x40000000
29+/* Default guest test virtual memory offset */
30+#define DEFAULT_GUEST_TEST_MEM 0xc0000000
3131
3232 /* How many pages to dirty for each guest loop */
3333 #define TEST_PAGES_PER_LOOP 1024
@@ -38,6 +38,27 @@ · 4 unmodified lines
3838 /* Interval for each host loop (ms) */
3939 #define TEST_HOST_LOOP_INTERVAL 10UL
4040
41+/* Dirty bitmaps are always little endian, so we need to swap on big endian */
42+#if defined(__s390x__)
43+# define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7)
44+# define test_bit_le(nr, addr) \
45+ test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
46+# define set_bit_le(nr, addr) \
47+ set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
48+# define clear_bit_le(nr, addr) \
49+ clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
50+# define test_and_set_bit_le(nr, addr) \
51+ test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
52+# define test_and_clear_bit_le(nr, addr) \
53+ test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
54+#else
55+# define test_bit_le test_bit
56+# define set_bit_le set_bit
57+# define clear_bit_le clear_bit
58+# define test_and_set_bit_le test_and_set_bit
59+# define test_and_clear_bit_le test_and_clear_bit
60+#endif
61+
4162 /*
4263 * Guest/Host shared variables. Ensure addr_gva2hva() and/or
4364 * sync_global_to/from_guest() are used when accessing from
@@ -69,11 +90,23 @@ static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM; · 25 unmodified lines
6990 */
7091 static void guest_code(void)
7192 {
93+ uint64_t addr;
7294 int i;
7395
96+ /*
97+ * On s390x, all pages of a 1M segment are initially marked as dirty
98+ * when a page of the segment is written to for the very first time.
99+ * To compensate this specialty in this test, we need to touch all
100+ * pages during the first iteration.
101+ */
102+ for (i = 0; i < guest_num_pages; i++) {
103+ addr = guest_test_virt_mem + i * guest_page_size;
104+ *(uint64_t *)addr = READ_ONCE(iteration);
105+ }
106+
74107 while (true) {
75108 for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
76− uint64_t addr = guest_test_virt_mem;
109+ addr = guest_test_virt_mem;
77110 addr += (READ_ONCE(random_array[i]) % guest_num_pages)
78111 * guest_page_size;
79112 addr &= ~(host_page_size - 1);
@@ -158,15 +191,15 @@ static void vm_dirty_log_verify(unsigned long *bmap) · 78 unmodified lines
158191 value_ptr = host_test_mem + page * host_page_size;
159192
160193 /* If this is a special page that we were tracking... */
161− if (test_and_clear_bit(page, host_bmap_track)) {
194+ if (test_and_clear_bit_le(page, host_bmap_track)) {
162195 host_track_next_count++;
163− TEST_ASSERT(test_bit(page, bmap),
196+ TEST_ASSERT(test_bit_le(page, bmap),
164197 "Page %"PRIu64" should have its dirty bit "
165198 "set in this iteration but it is missing",
166199 page);
167200 }
168201
169− if (test_bit(page, bmap)) {
202+ if (test_bit_le(page, bmap)) {
170203 host_dirty_count++;
171204 /*
172205 * If the bit is set, the value written onto
@@ -209,7 +242,7 @@ static void vm_dirty_log_verify(unsigned long *bmap) · 36 unmodified lines
209242 * should report its dirtyness in the
210243 * next run
211244 */
212− set_bit(page, host_bmap_track);
245+ set_bit_le(page, host_bmap_track);
213246 }
214247 }
215248 }
@@ -293,6 +326,10 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, · 77 unmodified lines
293326 * case where the size is not aligned to 64 pages.
294327 */
295328 guest_num_pages = (1ul << (30 - guest_page_shift)) + 16;
329+#ifdef __s390x__
330+ /* Round up to multiple of 1M (segment size) */
331+ guest_num_pages = (guest_num_pages + 0xff) & ~0xffUL;
332+#endif
296333 host_page_size = getpagesize();
297334 host_num_pages = (guest_num_pages * guest_page_size) / host_page_size +
298335 !!((guest_num_pages * guest_page_size) % host_page_size);
@@ -304,6 +341,11 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, · 5 unmodified lines
304341 guest_test_phys_mem = phys_offset;
305342 }
306343
344+#ifdef __s390x__
345+ /* Align to 1M (segment size) */
346+ guest_test_phys_mem &= ~((1 << 20) - 1);
347+#endif
348+
307349 DEBUG("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
308350
309351 bmap = bitmap_alloc(host_num_pages);
@@ -337,7 +379,7 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, · 27 unmodified lines
337379 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
338380 #endif
339381 #ifdef __aarch64__
340− ucall_init(vm, UCALL_MMIO, NULL);
382+ ucall_init(vm, NULL);
341383 #endif
342384
343385 /* Export the shared variables to the guest */
@@ -454,6 +496,9 @@ int main(int argc, char *argv[]) · 110 unmodified lines
454496 vm_guest_mode_params_init(VM_MODE_P48V48_64K, true, true);
455497 }
456498 #endif
499+#ifdef __s390x__
500+ vm_guest_mode_params_init(VM_MODE_P40V48_4K, true, true);
501+#endif
457502
458503 while ((opt = getopt(argc, argv, "hi:I:p:m:")) != -1) {
459504 switch (opt) {
tools/testing/selftests/kvm/include/kvm_util.h+1−7
@@ -165,12 +165,6 @@ int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd); · 164 unmodified lines
165165 memcpy(&(g), _p, sizeof(g)); \
166166 })
167167
168−/* ucall implementation types */
169−typedef enum {
170− UCALL_PIO,
171− UCALL_MMIO,
172−} ucall_type_t;
173−
174168 /* Common ucalls */
175169 enum {
176170 UCALL_NONE,
@@ -186,7 +180,7 @@ struct ucall { · 9 unmodified lines
186180 uint64_t args[UCALL_MAX_ARGS];
187181 };
188182
189−void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg);
183+void ucall_init(struct kvm_vm *vm, void *arg);
190184 void ucall_uninit(struct kvm_vm *vm);
191185 void ucall(uint64_t cmd, int nargs, ...);
192186 uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc);
tools/testing/selftests/kvm/lib/aarch64/ucall.c+112−0
@@ -0,0 +1,112 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * ucall support. A ucall is a "hypercall to userspace".
4+ *
5+ * Copyright (C) 2018, Red Hat, Inc.
6+ */
7+#include "kvm_util.h"
8+#include "../kvm_util_internal.h"
9+
10+static vm_vaddr_t *ucall_exit_mmio_addr;
11+
12+static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
13+{
14+ if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1))
15+ return false;
16+
17+ virt_pg_map(vm, gpa, gpa, 0);
18+
19+ ucall_exit_mmio_addr = (vm_vaddr_t *)gpa;
20+ sync_global_to_guest(vm, ucall_exit_mmio_addr);
21+
22+ return true;
23+}
24+
25+void ucall_init(struct kvm_vm *vm, void *arg)
26+{
27+ vm_paddr_t gpa, start, end, step, offset;
28+ unsigned int bits;
29+ bool ret;
30+
31+ if (arg) {
32+ gpa = (vm_paddr_t)arg;
33+ ret = ucall_mmio_init(vm, gpa);
34+ TEST_ASSERT(ret, "Can't set ucall mmio address to %lx", gpa);
35+ return;
36+ }
37+
38+ /*
39+ * Find an address within the allowed physical and virtual address
40+ * spaces, that does _not_ have a KVM memory region associated with
41+ * it. Identity mapping an address like this allows the guest to
42+ * access it, but as KVM doesn't know what to do with it, it
43+ * will assume it's something userspace handles and exit with
44+ * KVM_EXIT_MMIO. Well, at least that's how it works for AArch64.
45+ * Here we start with a guess that the addresses around 5/8th
46+ * of the allowed space are unmapped and then work both down and
47+ * up from there in 1/16th allowed space sized steps.
48+ *
49+ * Note, we need to use VA-bits - 1 when calculating the allowed
50+ * virtual address space for an identity mapping because the upper
51+ * half of the virtual address space is the two's complement of the
52+ * lower and won't match physical addresses.
53+ */
54+ bits = vm->va_bits - 1;
55+ bits = vm->pa_bits < bits ? vm->pa_bits : bits;
56+ end = 1ul << bits;
57+ start = end * 5 / 8;
58+ step = end / 16;
59+ for (offset = 0; offset < end - start; offset += step) {
60+ if (ucall_mmio_init(vm, start - offset))
61+ return;
62+ if (ucall_mmio_init(vm, start + offset))
63+ return;
64+ }
65+ TEST_ASSERT(false, "Can't find a ucall mmio address");
66+}
67+
68+void ucall_uninit(struct kvm_vm *vm)
69+{
70+ ucall_exit_mmio_addr = 0;
71+ sync_global_to_guest(vm, ucall_exit_mmio_addr);
72+}
73+
74+void ucall(uint64_t cmd, int nargs, ...)
75+{
76+ struct ucall uc = {
77+ .cmd = cmd,
78+ };
79+ va_list va;
80+ int i;
81+
82+ nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
83+
84+ va_start(va, nargs);
85+ for (i = 0; i < nargs; ++i)
86+ uc.args[i] = va_arg(va, uint64_t);
87+ va_end(va);
88+
89+ *ucall_exit_mmio_addr = (vm_vaddr_t)&uc;
90+}
91+
92+uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
93+{
94+ struct kvm_run *run = vcpu_state(vm, vcpu_id);
95+ struct ucall ucall = {};
96+
97+ if (run->exit_reason == KVM_EXIT_MMIO &&
98+ run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
99+ vm_vaddr_t gva;
100+
101+ TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
102+ "Unexpected ucall exit mmio address access");
103+ memcpy(&gva, run->mmio.data, sizeof(gva));
104+ memcpy(&ucall, addr_gva2hva(vm, gva), sizeof(ucall));
105+
106+ vcpu_run_complete_io(vm, vcpu_id);
107+ if (uc)
108+ memcpy(uc, &ucall, sizeof(ucall));
109+ }
110+
111+ return ucall.cmd;
112+}
tools/testing/selftests/kvm/lib/s390x/ucall.c+56−0
@@ -0,0 +1,56 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * ucall support. A ucall is a "hypercall to userspace".
4+ *
5+ * Copyright (C) 2019 Red Hat, Inc.
6+ */
7+#include "kvm_util.h"
8+
9+void ucall_init(struct kvm_vm *vm, void *arg)
10+{
11+}
12+
13+void ucall_uninit(struct kvm_vm *vm)
14+{
15+}
16+
17+void ucall(uint64_t cmd, int nargs, ...)
18+{
19+ struct ucall uc = {
20+ .cmd = cmd,
21+ };
22+ va_list va;
23+ int i;
24+
25+ nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
26+
27+ va_start(va, nargs);
28+ for (i = 0; i < nargs; ++i)
29+ uc.args[i] = va_arg(va, uint64_t);
30+ va_end(va);
31+
32+ /* Exit via DIAGNOSE 0x501 (normally used for breakpoints) */
33+ asm volatile ("diag 0,%0,0x501" : : "a"(&uc) : "memory");
34+}
35+
36+uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
37+{
38+ struct kvm_run *run = vcpu_state(vm, vcpu_id);
39+ struct ucall ucall = {};
40+
41+ if (run->exit_reason == KVM_EXIT_S390_SIEIC &&
42+ run->s390_sieic.icptcode == 4 &&
43+ (run->s390_sieic.ipa >> 8) == 0x83 && /* 0x83 means DIAGNOSE */
44+ (run->s390_sieic.ipb >> 16) == 0x501) {
45+ int reg = run->s390_sieic.ipa & 0xf;
46+
47+ memcpy(&ucall, addr_gva2hva(vm, run->s.regs.gprs[reg]),
48+ sizeof(ucall));
49+
50+ vcpu_run_complete_io(vm, vcpu_id);
51+ if (uc)
52+ memcpy(uc, &ucall, sizeof(ucall));
53+ }
54+
55+ return ucall.cmd;
56+}
tools/testing/selftests/kvm/lib/ucall.c+0−157
@@ -1,157 +0,0 @@
1−// SPDX-License-Identifier: GPL-2.0
2−/*
3− * ucall support. A ucall is a "hypercall to userspace".
4− *
5− * Copyright (C) 2018, Red Hat, Inc.
6− */
7−#include "kvm_util.h"
8−#include "kvm_util_internal.h"
9−
10−#define UCALL_PIO_PORT ((uint16_t)0x1000)
11−
12−static ucall_type_t ucall_type;
13−static vm_vaddr_t *ucall_exit_mmio_addr;
14−
15−static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
16−{
17− if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1))
18− return false;
19−
20− virt_pg_map(vm, gpa, gpa, 0);
21−
22− ucall_exit_mmio_addr = (vm_vaddr_t *)gpa;
23− sync_global_to_guest(vm, ucall_exit_mmio_addr);
24−
25− return true;
26−}
27−
28−void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg)
29−{
30− ucall_type = type;
31− sync_global_to_guest(vm, ucall_type);
32−
33− if (type == UCALL_PIO)
34− return;
35−
36− if (type == UCALL_MMIO) {
37− vm_paddr_t gpa, start, end, step, offset;
38− unsigned bits;
39− bool ret;
40−
41− if (arg) {
42− gpa = (vm_paddr_t)arg;
43− ret = ucall_mmio_init(vm, gpa);
44− TEST_ASSERT(ret, "Can't set ucall mmio address to %lx", gpa);
45− return;
46− }
47−
48− /*
49− * Find an address within the allowed physical and virtual address
50− * spaces, that does _not_ have a KVM memory region associated with
51− * it. Identity mapping an address like this allows the guest to
52− * access it, but as KVM doesn't know what to do with it, it
53− * will assume it's something userspace handles and exit with
54− * KVM_EXIT_MMIO. Well, at least that's how it works for AArch64.
55− * Here we start with a guess that the addresses around 5/8th
56− * of the allowed space are unmapped and then work both down and
57− * up from there in 1/16th allowed space sized steps.
58− *
59− * Note, we need to use VA-bits - 1 when calculating the allowed
60− * virtual address space for an identity mapping because the upper
61− * half of the virtual address space is the two's complement of the
62− * lower and won't match physical addresses.
63− */
64− bits = vm->va_bits - 1;
65− bits = vm->pa_bits < bits ? vm->pa_bits : bits;
66− end = 1ul << bits;
67− start = end * 5 / 8;
68− step = end / 16;
69− for (offset = 0; offset < end - start; offset += step) {
70− if (ucall_mmio_init(vm, start - offset))
71− return;
72− if (ucall_mmio_init(vm, start + offset))
73− return;
74− }
75− TEST_ASSERT(false, "Can't find a ucall mmio address");
76− }
77−}
78−
79−void ucall_uninit(struct kvm_vm *vm)
80−{
81− ucall_type = 0;
82− sync_global_to_guest(vm, ucall_type);
83− ucall_exit_mmio_addr = 0;
84− sync_global_to_guest(vm, ucall_exit_mmio_addr);
85−}
86−
87−static void ucall_pio_exit(struct ucall *uc)
88−{
89−#ifdef __x86_64__
90− asm volatile("in %[port], %%al"
91− : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax");
92−#endif
93−}
94−
95−static void ucall_mmio_exit(struct ucall *uc)
96−{
97− *ucall_exit_mmio_addr = (vm_vaddr_t)uc;
98−}
99−
100−void ucall(uint64_t cmd, int nargs, ...)
101−{
102− struct ucall uc = {
103− .cmd = cmd,
104− };
105− va_list va;
106− int i;
107−
108− nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
109−
110− va_start(va, nargs);
111− for (i = 0; i < nargs; ++i)
112− uc.args[i] = va_arg(va, uint64_t);
113− va_end(va);
114−
115− switch (ucall_type) {
116− case UCALL_PIO:
117− ucall_pio_exit(&uc);
118− break;
119− case UCALL_MMIO:
120− ucall_mmio_exit(&uc);
121− break;
122− };
123−}
124−
125−uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
126−{
127− struct kvm_run *run = vcpu_state(vm, vcpu_id);
128− struct ucall ucall = {};
129− bool got_ucall = false;
130−
131−#ifdef __x86_64__
132− if (ucall_type == UCALL_PIO && run->exit_reason == KVM_EXIT_IO &&
133− run->io.port == UCALL_PIO_PORT) {
134− struct kvm_regs regs;
135− vcpu_regs_get(vm, vcpu_id, &regs);
136− memcpy(&ucall, addr_gva2hva(vm, (vm_vaddr_t)regs.rdi), sizeof(ucall));
137− got_ucall = true;
138− }
139−#endif
140− if (ucall_type == UCALL_MMIO && run->exit_reason == KVM_EXIT_MMIO &&
141− run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
142− vm_vaddr_t gva;
143− TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
144− "Unexpected ucall exit mmio address access");
145− memcpy(&gva, run->mmio.data, sizeof(gva));
146− memcpy(&ucall, addr_gva2hva(vm, gva), sizeof(ucall));
147− got_ucall = true;
148− }
149−
150− if (got_ucall) {
151− vcpu_run_complete_io(vm, vcpu_id);
152− if (uc)
153− memcpy(uc, &ucall, sizeof(ucall));
154− }
155−
156− return ucall.cmd;
157−}
tools/testing/selftests/kvm/lib/x86_64/ucall.c+56−0
@@ -0,0 +1,56 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * ucall support. A ucall is a "hypercall to userspace".
4+ *
5+ * Copyright (C) 2018, Red Hat, Inc.
6+ */
7+#include "kvm_util.h"
8+
9+#define UCALL_PIO_PORT ((uint16_t)0x1000)
10+
11+void ucall_init(struct kvm_vm *vm, void *arg)
12+{
13+}
14+
15+void ucall_uninit(struct kvm_vm *vm)
16+{
17+}
18+
19+void ucall(uint64_t cmd, int nargs, ...)
20+{
21+ struct ucall uc = {
22+ .cmd = cmd,
23+ };
24+ va_list va;
25+ int i;
26+
27+ nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
28+
29+ va_start(va, nargs);
30+ for (i = 0; i < nargs; ++i)
31+ uc.args[i] = va_arg(va, uint64_t);
32+ va_end(va);
33+
34+ asm volatile("in %[port], %%al"
35+ : : [port] "d" (UCALL_PIO_PORT), "D" (&uc) : "rax");
36+}
37+
38+uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
39+{
40+ struct kvm_run *run = vcpu_state(vm, vcpu_id);
41+ struct ucall ucall = {};
42+
43+ if (run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) {
44+ struct kvm_regs regs;
45+
46+ vcpu_regs_get(vm, vcpu_id, &regs);
47+ memcpy(&ucall, addr_gva2hva(vm, (vm_vaddr_t)regs.rdi),
48+ sizeof(ucall));
49+
50+ vcpu_run_complete_io(vm, vcpu_id);
51+ if (uc)
52+ memcpy(uc, &ucall, sizeof(ucall));
53+ }
54+
55+ return ucall.cmd;
56+}
tools/testing/selftests/kvm/s390x/memop.c+166−0
@@ -0,0 +1,166 @@
1+// SPDX-License-Identifier: GPL-2.0-or-later
2+/*
3+ * Test for s390x KVM_S390_MEM_OP
4+ *
5+ * Copyright (C) 2019, Red Hat, Inc.
6+ */
7+
8+#include <stdio.h>
9+#include <stdlib.h>
10+#include <string.h>
11+#include <sys/ioctl.h>
12+
13+#include "test_util.h"
14+#include "kvm_util.h"
15+
16+#define VCPU_ID 1
17+
18+static uint8_t mem1[65536];
19+static uint8_t mem2[65536];
20+
21+static void guest_code(void)
22+{
23+ int i;
24+
25+ for (;;) {
26+ for (i = 0; i < sizeof(mem2); i++)
27+ mem2[i] = mem1[i];
28+ GUEST_SYNC(0);
29+ }
30+}
31+
32+int main(int argc, char *argv[])
33+{
34+ struct kvm_vm *vm;
35+ struct kvm_run *run;
36+ struct kvm_s390_mem_op ksmo;
37+ int rv, i, maxsize;
38+
39+ setbuf(stdout, NULL); /* Tell stdout not to buffer its content */
40+
41+ maxsize = kvm_check_cap(KVM_CAP_S390_MEM_OP);
42+ if (!maxsize) {
43+ fprintf(stderr, "CAP_S390_MEM_OP not supported -> skip test\n");
44+ exit(KSFT_SKIP);
45+ }
46+ if (maxsize > sizeof(mem1))
47+ maxsize = sizeof(mem1);
48+
49+ /* Create VM */
50+ vm = vm_create_default(VCPU_ID, 0, guest_code);
51+ run = vcpu_state(vm, VCPU_ID);
52+
53+ for (i = 0; i < sizeof(mem1); i++)
54+ mem1[i] = i * i + i;
55+
56+ /* Set the first array */
57+ ksmo.gaddr = addr_gva2gpa(vm, (uintptr_t)mem1);
58+ ksmo.flags = 0;
59+ ksmo.size = maxsize;
60+ ksmo.op = KVM_S390_MEMOP_LOGICAL_WRITE;
61+ ksmo.buf = (uintptr_t)mem1;
62+ ksmo.ar = 0;
63+ vcpu_ioctl(vm, VCPU_ID, KVM_S390_MEM_OP, &ksmo);
64+
65+ /* Let the guest code copy the first array to the second */
66+ vcpu_run(vm, VCPU_ID);
67+ TEST_ASSERT(run->exit_reason == KVM_EXIT_S390_SIEIC,
68+ "Unexpected exit reason: %u (%s)\n",
69+ run->exit_reason,
70+ exit_reason_str(run->exit_reason));
71+
72+ memset(mem2, 0xaa, sizeof(mem2));
73+
74+ /* Get the second array */
75+ ksmo.gaddr = (uintptr_t)mem2;
76+ ksmo.flags = 0;
77+ ksmo.size = maxsize;
78+ ksmo.op = KVM_S390_MEMOP_LOGICAL_READ;
79+ ksmo.buf = (uintptr_t)mem2;
80+ ksmo.ar = 0;
81+ vcpu_ioctl(vm, VCPU_ID, KVM_S390_MEM_OP, &ksmo);
82+
83+ TEST_ASSERT(!memcmp(mem1, mem2, maxsize),
84+ "Memory contents do not match!");
85+
86+ /* Check error conditions - first bad size: */
87+ ksmo.gaddr = (uintptr_t)mem1;
88+ ksmo.flags = 0;
89+ ksmo.size = -1;
90+ ksmo.op = KVM_S390_MEMOP_LOGICAL_WRITE;
91+ ksmo.buf = (uintptr_t)mem1;
92+ ksmo.ar = 0;
93+ rv = _vcpu_ioctl(vm, VCPU_ID, KVM_S390_MEM_OP, &ksmo);
94+ TEST_ASSERT(rv == -1 && errno == E2BIG, "ioctl allows insane sizes");
95+
96+ /* Zero size: */
97+ ksmo.gaddr = (uintptr_t)mem1;
98+ ksmo.flags = 0;
99+ ksmo.size = 0;
100+ ksmo.op = KVM_S390_MEMOP_LOGICAL_WRITE;
101+ ksmo.buf = (uintptr_t)mem1;
102+ ksmo.ar = 0;
103+ rv = _vcpu_ioctl(vm, VCPU_ID, KVM_S390_MEM_OP, &ksmo);
104+ TEST_ASSERT(rv == -1 && (errno == EINVAL || errno == ENOMEM),
105+ "ioctl allows 0 as size");
106+
107+ /* Bad flags: */
108+ ksmo.gaddr = (uintptr_t)mem1;
109+ ksmo.flags = -1;
110+ ksmo.size = maxsize;
111+ ksmo.op = KVM_S390_MEMOP_LOGICAL_WRITE;
112+ ksmo.buf = (uintptr_t)mem1;
113+ ksmo.ar = 0;
114+ rv = _vcpu_ioctl(vm, VCPU_ID, KVM_S390_MEM_OP, &ksmo);
115+ TEST_ASSERT(rv == -1 && errno == EINVAL, "ioctl allows all flags");
116+
117+ /* Bad operation: */
118+ ksmo.gaddr = (uintptr_t)mem1;
119+ ksmo.flags = 0;
120+ ksmo.size = maxsize;
121+ ksmo.op = -1;
122+ ksmo.buf = (uintptr_t)mem1;
123+ ksmo.ar = 0;
124+ rv = _vcpu_ioctl(vm, VCPU_ID, KVM_S390_MEM_OP, &ksmo);
125+ TEST_ASSERT(rv == -1 && errno == EINVAL, "ioctl allows bad operations");
126+
127+ /* Bad guest address: */
128+ ksmo.gaddr = ~0xfffUL;
129+ ksmo.flags = KVM_S390_MEMOP_F_CHECK_ONLY;
130+ ksmo.size = maxsize;
131+ ksmo.op = KVM_S390_MEMOP_LOGICAL_WRITE;
132+ ksmo.buf = (uintptr_t)mem1;
133+ ksmo.ar = 0;
134+ rv = _vcpu_ioctl(vm, VCPU_ID, KVM_S390_MEM_OP, &ksmo);
135+ TEST_ASSERT(rv > 0, "ioctl does not report bad guest memory access");
136+
137+ /* Bad host address: */
138+ ksmo.gaddr = (uintptr_t)mem1;
139+ ksmo.flags = 0;
140+ ksmo.size = maxsize;
141+ ksmo.op = KVM_S390_MEMOP_LOGICAL_WRITE;
142+ ksmo.buf = 0;
143+ ksmo.ar = 0;
144+ rv = _vcpu_ioctl(vm, VCPU_ID, KVM_S390_MEM_OP, &ksmo);
145+ TEST_ASSERT(rv == -1 && errno == EFAULT,
146+ "ioctl does not report bad host memory address");
147+
148+ /* Bad access register: */
149+ run->psw_mask &= ~(3UL << (63 - 17));
150+ run->psw_mask |= 1UL << (63 - 17); /* Enable AR mode */
151+ vcpu_run(vm, VCPU_ID); /* To sync new state to SIE block */
152+ ksmo.gaddr = (uintptr_t)mem1;
153+ ksmo.flags = 0;
154+ ksmo.size = maxsize;
155+ ksmo.op = KVM_S390_MEMOP_LOGICAL_WRITE;
156+ ksmo.buf = (uintptr_t)mem1;
157+ ksmo.ar = 17;
158+ rv = _vcpu_ioctl(vm, VCPU_ID, KVM_S390_MEM_OP, &ksmo);
159+ TEST_ASSERT(rv == -1 && errno == EINVAL, "ioctl allows ARs > 15");
160+ run->psw_mask &= ~(3UL << (63 - 17)); /* Disable AR mode */
161+ vcpu_run(vm, VCPU_ID); /* Run to sync new state */
162+
163+ kvm_vm_free(vm);
164+
165+ return 0;
166+}
tools/testing/selftests/kvm/s390x/sync_regs_test.c+34−2
@@ -25,9 +25,11 @@ · 24 unmodified lines
2525
2626 static void guest_code(void)
2727 {
28+ register u64 stage asm("11") = 0;
29+
2830 for (;;) {
29− asm volatile ("diag 0,0,0x501");
30− asm volatile ("ahi 11,1");
31+ GUEST_SYNC(0);
32+ asm volatile ("ahi %0,1" : : "r"(stage));
3133 }
3234 }
3335
@@ -83,6 +85,36 @@ int main(int argc, char *argv[]) · 49 unmodified lines
8385
8486 run = vcpu_state(vm, VCPU_ID);
8587
88+ /* Request reading invalid register set from VCPU. */
89+ run->kvm_valid_regs = INVALID_SYNC_FIELD;
90+ rv = _vcpu_run(vm, VCPU_ID);
91+ TEST_ASSERT(rv < 0 && errno == EINVAL,
92+ "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
93+ rv);
94+ vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
95+
96+ run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
97+ rv = _vcpu_run(vm, VCPU_ID);
98+ TEST_ASSERT(rv < 0 && errno == EINVAL,
99+ "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
100+ rv);
101+ vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
102+
103+ /* Request setting invalid register set into VCPU. */
104+ run->kvm_dirty_regs = INVALID_SYNC_FIELD;
105+ rv = _vcpu_run(vm, VCPU_ID);
106+ TEST_ASSERT(rv < 0 && errno == EINVAL,
107+ "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
108+ rv);
109+ vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0;
110+
111+ run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
112+ rv = _vcpu_run(vm, VCPU_ID);
113+ TEST_ASSERT(rv < 0 && errno == EINVAL,
114+ "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
115+ rv);
116+ vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0;
117+
86118 /* Request and verify all valid register sets. */
87119 run->kvm_valid_regs = TEST_SYNC_FIELDS;
88120 rv = _vcpu_run(vm, VCPU_ID);
virt/kvm/arm/arm.c+2−0
@@ -196,6 +196,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · 195 unmodified lines
196196 case KVM_CAP_MP_STATE:
197197 case KVM_CAP_IMMEDIATE_EXIT:
198198 case KVM_CAP_VCPU_EVENTS:
199+ case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
199200 r = 1;
200201 break;
201202 case KVM_CAP_ARM_SET_DEVICE_ADDR:
@@ -888,6 +889,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, · 686 unmodified lines
888889
889890 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
890891 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
892+ vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
891893 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
892894
893895 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
virt/kvm/arm/vgic/vgic-init.c+6−2
@@ -54,6 +54,7 @@ void kvm_vgic_early_init(struct kvm *kvm) · 53 unmodified lines
5454 struct vgic_dist *dist = &kvm->arch.vgic;
5555
5656 INIT_LIST_HEAD(&dist->lpi_list_head);
57+ INIT_LIST_HEAD(&dist->lpi_translation_cache);
5758 raw_spin_lock_init(&dist->lpi_list_lock);
5859 }
5960
@@ -199,7 +200,6 @@ int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) · 139 unmodified lines
199200 int i;
200201
201202 vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF;
202− vgic_cpu->sgi_iodev.base_addr = VGIC_ADDR_UNDEF;
203203
204204 INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
205205 raw_spin_lock_init(&vgic_cpu->ap_list_lock);
@@ -304,6 +304,7 @@ int vgic_init(struct kvm *kvm) · 98 unmodified lines
304304 }
305305
306306 if (vgic_has_its(kvm)) {
307+ vgic_lpi_translation_cache_init(kvm);
307308 ret = vgic_v4_init(kvm);
308309 if (ret)
309310 goto out;
@@ -345,6 +346,9 @@ static void kvm_vgic_dist_destroy(struct kvm *kvm) · 35 unmodified lines
345346 INIT_LIST_HEAD(&dist->rd_regions);
346347 }
347348
349+ if (vgic_has_its(kvm))
350+ vgic_lpi_translation_cache_destroy(kvm);
351+
348352 if (vgic_supports_direct_msis(kvm))
349353 vgic_v4_teardown(kvm);
350354 }
@@ -515,7 +519,7 @@ int kvm_vgic_hyp_init(void) · 164 unmodified lines
515519 break;
516520 default:
517521 ret = -ENODEV;
518− };
522+ }
519523
520524 if (ret)
521525 return ret;
virt/kvm/arm/vgic/vgic-irqfd.c+30−6
@@ -66,6 +66,15 @@ out: · 65 unmodified lines
6666 return r;
6767 }
6868
69+static void kvm_populate_msi(struct kvm_kernel_irq_routing_entry *e,
70+ struct kvm_msi *msi)
71+{
72+ msi->address_lo = e->msi.address_lo;
73+ msi->address_hi = e->msi.address_hi;
74+ msi->data = e->msi.data;
75+ msi->flags = e->msi.flags;
76+ msi->devid = e->msi.devid;
77+}
6978 /**
7079 * kvm_set_msi: inject the MSI corresponding to the
7180 * MSI routing entry
@@ -79,21 +88,36 @@ int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, · 7 unmodified lines
7988 {
8089 struct kvm_msi msi;
8190
82− msi.address_lo = e->msi.address_lo;
83− msi.address_hi = e->msi.address_hi;
84− msi.data = e->msi.data;
85− msi.flags = e->msi.flags;
86− msi.devid = e->msi.devid;
87−
8891 if (!vgic_has_its(kvm))
8992 return -ENODEV;
9093
9194 if (!level)
9295 return -1;
9396
97+ kvm_populate_msi(e, &msi);
9498 return vgic_its_inject_msi(kvm, &msi);
9599 }
96100
101+/**
102+ * kvm_arch_set_irq_inatomic: fast-path for irqfd injection
103+ *
104+ * Currently only direct MSI injection is supported.
105+ */
106+int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
107+ struct kvm *kvm, int irq_source_id, int level,
108+ bool line_status)
109+{
110+ if (e->type == KVM_IRQ_ROUTING_MSI && vgic_has_its(kvm) && level) {
111+ struct kvm_msi msi;
112+
113+ kvm_populate_msi(e, &msi);
114+ if (!vgic_its_inject_cached_translation(kvm, &msi))
115+ return 0;
116+ }
117+
118+ return -EWOULDBLOCK;
119+}
120+
97121 int kvm_vgic_setup_default_irq_routing(struct kvm *kvm)
98122 {
99123 struct kvm_irq_routing_entry *entries;
virt/kvm/arm/vgic/vgic-its.c+207−0
@@ -138,6 +138,14 @@ struct its_ite { · 137 unmodified lines
138138 u32 event_id;
139139 };
140140
141+struct vgic_translation_cache_entry {
142+ struct list_head entry;
143+ phys_addr_t db;
144+ u32 devid;
145+ u32 eventid;
146+ struct vgic_irq *irq;
147+};
148+
141149 /**
142150 * struct vgic_its_abi - ITS abi ops and settings
143151 * @cte_esz: collection table entry size
@@ -527,6 +535,127 @@ static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm, · 383 unmodified lines
527535 return 0;
528536 }
529537
538+static struct vgic_irq *__vgic_its_check_cache(struct vgic_dist *dist,
539+ phys_addr_t db,
540+ u32 devid, u32 eventid)
541+{
542+ struct vgic_translation_cache_entry *cte;
543+
544+ list_for_each_entry(cte, &dist->lpi_translation_cache, entry) {
545+ /*
546+ * If we hit a NULL entry, there is nothing after this
547+ * point.
548+ */
549+ if (!cte->irq)
550+ break;
551+
552+ if (cte->db != db || cte->devid != devid ||
553+ cte->eventid != eventid)
554+ continue;
555+
556+ /*
557+ * Move this entry to the head, as it is the most
558+ * recently used.
559+ */
560+ if (!list_is_first(&cte->entry, &dist->lpi_translation_cache))
561+ list_move(&cte->entry, &dist->lpi_translation_cache);
562+
563+ return cte->irq;
564+ }
565+
566+ return NULL;
567+}
568+
569+static struct vgic_irq *vgic_its_check_cache(struct kvm *kvm, phys_addr_t db,
570+ u32 devid, u32 eventid)
571+{
572+ struct vgic_dist *dist = &kvm->arch.vgic;
573+ struct vgic_irq *irq;
574+ unsigned long flags;
575+
576+ raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
577+ irq = __vgic_its_check_cache(dist, db, devid, eventid);
578+ raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
579+
580+ return irq;
581+}
582+
583+static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its,
584+ u32 devid, u32 eventid,
585+ struct vgic_irq *irq)
586+{
587+ struct vgic_dist *dist = &kvm->arch.vgic;
588+ struct vgic_translation_cache_entry *cte;
589+ unsigned long flags;
590+ phys_addr_t db;
591+
592+ /* Do not cache a directly injected interrupt */
593+ if (irq->hw)
594+ return;
595+
596+ raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
597+
598+ if (unlikely(list_empty(&dist->lpi_translation_cache)))
599+ goto out;
600+
601+ /*
602+ * We could have raced with another CPU caching the same
603+ * translation behind our back, so let's check it is not in
604+ * already
605+ */
606+ db = its->vgic_its_base + GITS_TRANSLATER;
607+ if (__vgic_its_check_cache(dist, db, devid, eventid))
608+ goto out;
609+
610+ /* Always reuse the last entry (LRU policy) */
611+ cte = list_last_entry(&dist->lpi_translation_cache,
612+ typeof(*cte), entry);
613+
614+ /*
615+ * Caching the translation implies having an extra reference
616+ * to the interrupt, so drop the potential reference on what
617+ * was in the cache, and increment it on the new interrupt.
618+ */
619+ if (cte->irq)
620+ __vgic_put_lpi_locked(kvm, cte->irq);
621+
622+ vgic_get_irq_kref(irq);
623+
624+ cte->db = db;
625+ cte->devid = devid;
626+ cte->eventid = eventid;
627+ cte->irq = irq;
628+
629+ /* Move the new translation to the head of the list */
630+ list_move(&cte->entry, &dist->lpi_translation_cache);
631+
632+out:
633+ raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
634+}
635+
636+void vgic_its_invalidate_cache(struct kvm *kvm)
637+{
638+ struct vgic_dist *dist = &kvm->arch.vgic;
639+ struct vgic_translation_cache_entry *cte;
640+ unsigned long flags;
641+
642+ raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
643+
644+ list_for_each_entry(cte, &dist->lpi_translation_cache, entry) {
645+ /*
646+ * If we hit a NULL entry, there is nothing after this
647+ * point.
648+ */
649+ if (!cte->irq)
650+ break;
651+
652+ __vgic_put_lpi_locked(kvm, cte->irq);
653+ cte->irq = NULL;
654+ }
655+
656+ raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
657+}
658+
530659 int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
531660 u32 devid, u32 eventid, struct vgic_irq **irq)
532661 {
@@ -547,6 +676,8 @@ int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its, · 14 unmodified lines
547676 if (!vcpu->arch.vgic_cpu.lpis_enabled)
548677 return -EBUSY;
549678
679+ vgic_its_cache_translation(kvm, its, devid, eventid, ite->irq);
680+
550681 *irq = ite->irq;
551682 return 0;
552683 }
@@ -608,6 +739,25 @@ static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its, · 55 unmodified lines
608739 return 0;
609740 }
610741
742+int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi)
743+{
744+ struct vgic_irq *irq;
745+ unsigned long flags;
746+ phys_addr_t db;
747+
748+ db = (u64)msi->address_hi << 32 | msi->address_lo;
749+ irq = vgic_its_check_cache(kvm, db, msi->devid, msi->data);
750+
751+ if (!irq)
752+ return -1;
753+
754+ raw_spin_lock_irqsave(&irq->irq_lock, flags);
755+ irq->pending_latch = true;
756+ vgic_queue_irq_unlock(kvm, irq, flags);
757+
758+ return 0;
759+}
760+
611761 /*
612762 * Queries the KVM IO bus framework to get the ITS pointer from the given
613763 * doorbell address.
@@ -619,6 +769,9 @@ int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi) · 5 unmodified lines
619769 struct vgic_its *its;
620770 int ret;
621771
772+ if (!vgic_its_inject_cached_translation(kvm, msi))
773+ return 1;
774+
622775 its = vgic_msi_to_its(kvm, msi);
623776 if (IS_ERR(its))
624777 return PTR_ERR(its);
@@ -691,6 +844,8 @@ static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its, · 66 unmodified lines
691844 * don't bother here since we clear the ITTE anyway and the
692845 * pending state is a property of the ITTE struct.
693846 */
847+ vgic_its_invalidate_cache(kvm);
848+
694849 its_free_ite(kvm, ite);
695850 return 0;
696851 }
@@ -726,6 +881,8 @@ static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its, · 29 unmodified lines
726881 ite->collection = collection;
727882 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
728883
884+ vgic_its_invalidate_cache(kvm);
885+
729886 return update_affinity(ite->irq, vcpu);
730887 }
731888
@@ -954,6 +1111,8 @@ static void vgic_its_free_device(struct kvm *kvm, struct its_device *device) · 222 unmodified lines
9541111 list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list)
9551112 its_free_ite(kvm, ite);
9561113
1114+ vgic_its_invalidate_cache(kvm);
1115+
9571116 list_del(&device->dev_list);
9581117 kfree(device);
9591118 }
@@ -1059,6 +1218,7 @@ static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its, · 99 unmodified lines
10591218
10601219 if (!valid) {
10611220 vgic_its_free_collection(its, coll_id);
1221+ vgic_its_invalidate_cache(kvm);
10621222 } else {
10631223 collection = find_collection(its, coll_id);
10641224
@@ -1207,6 +1367,8 @@ static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its, · 142 unmodified lines
12071367 vgic_put_irq(kvm, irq);
12081368 }
12091369
1370+ vgic_its_invalidate_cache(kvm);
1371+
12101372 kfree(intids);
12111373 return 0;
12121374 }
@@ -1557,6 +1719,8 @@ static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, · 344 unmodified lines
15571719 goto out;
15581720
15591721 its->enabled = !!(val & GITS_CTLR_ENABLE);
1722+ if (!its->enabled)
1723+ vgic_its_invalidate_cache(kvm);
15601724
15611725 /*
15621726 * Try to process any pending commands. This function bails out early
@@ -1657,6 +1821,47 @@ out: · 94 unmodified lines
16571821 return ret;
16581822 }
16591823
1824+/* Default is 16 cached LPIs per vcpu */
1825+#define LPI_DEFAULT_PCPU_CACHE_SIZE 16
1826+
1827+void vgic_lpi_translation_cache_init(struct kvm *kvm)
1828+{
1829+ struct vgic_dist *dist = &kvm->arch.vgic;
1830+ unsigned int sz;
1831+ int i;
1832+
1833+ if (!list_empty(&dist->lpi_translation_cache))
1834+ return;
1835+
1836+ sz = atomic_read(&kvm->online_vcpus) * LPI_DEFAULT_PCPU_CACHE_SIZE;
1837+
1838+ for (i = 0; i < sz; i++) {
1839+ struct vgic_translation_cache_entry *cte;
1840+
1841+ /* An allocation failure is not fatal */
1842+ cte = kzalloc(sizeof(*cte), GFP_KERNEL);
1843+ if (WARN_ON(!cte))
1844+ break;
1845+
1846+ INIT_LIST_HEAD(&cte->entry);
1847+ list_add(&cte->entry, &dist->lpi_translation_cache);
1848+ }
1849+}
1850+
1851+void vgic_lpi_translation_cache_destroy(struct kvm *kvm)
1852+{
1853+ struct vgic_dist *dist = &kvm->arch.vgic;
1854+ struct vgic_translation_cache_entry *cte, *tmp;
1855+
1856+ vgic_its_invalidate_cache(kvm);
1857+
1858+ list_for_each_entry_safe(cte, tmp,
1859+ &dist->lpi_translation_cache, entry) {
1860+ list_del(&cte->entry);
1861+ kfree(cte);
1862+ }
1863+}
1864+
16601865 #define INITIAL_BASER_VALUE \
16611866 (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \
16621867 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \
@@ -1685,6 +1890,8 @@ static int vgic_its_create(struct kvm_device *dev, u32 type) · 22 unmodified lines
16851890 kfree(its);
16861891 return ret;
16871892 }
1893+
1894+ vgic_lpi_translation_cache_init(dev->kvm);
16881895 }
16891896
16901897 mutex_init(&its->its_lock);
virt/kvm/arm/vgic/vgic-mmio-v3.c+27−58
@@ -192,8 +192,10 @@ static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu, · 191 unmodified lines
192192
193193 vgic_cpu->lpis_enabled = val & GICR_CTLR_ENABLE_LPIS;
194194
195− if (was_enabled && !vgic_cpu->lpis_enabled)
195+ if (was_enabled && !vgic_cpu->lpis_enabled) {
196196 vgic_flush_pending_lpis(vcpu);
197+ vgic_its_invalidate_cache(vcpu->kvm);
198+ }
197199
198200 if (!was_enabled && vgic_cpu->lpis_enabled)
199201 vgic_enable_lpis(vcpu);
@@ -515,7 +517,8 @@ static const struct vgic_register_region vgic_v3_dist_registers[] = { · 315 unmodified lines
515517 VGIC_ACCESS_32bit),
516518 };
517519
518−static const struct vgic_register_region vgic_v3_rdbase_registers[] = {
520+static const struct vgic_register_region vgic_v3_rd_registers[] = {
521+ /* RD_base registers */
519522 REGISTER_DESC_WITH_LENGTH(GICR_CTLR,
520523 vgic_mmio_read_v3r_ctlr, vgic_mmio_write_v3r_ctlr, 4,
521524 VGIC_ACCESS_32bit),
@@ -540,44 +543,42 @@ static const struct vgic_register_region vgic_v3_rdbase_registers[] = { · 18 unmodified lines
540543 REGISTER_DESC_WITH_LENGTH(GICR_IDREGS,
541544 vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
542545 VGIC_ACCESS_32bit),
543−};
544−
545−static const struct vgic_register_region vgic_v3_sgibase_registers[] = {
546− REGISTER_DESC_WITH_LENGTH(GICR_IGROUPR0,
546+ /* SGI_base registers */
547+ REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_IGROUPR0,
547548 vgic_mmio_read_group, vgic_mmio_write_group, 4,
548549 VGIC_ACCESS_32bit),
549− REGISTER_DESC_WITH_LENGTH(GICR_ISENABLER0,
550+ REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_ISENABLER0,
550551 vgic_mmio_read_enable, vgic_mmio_write_senable, 4,
551552 VGIC_ACCESS_32bit),
552− REGISTER_DESC_WITH_LENGTH(GICR_ICENABLER0,
553+ REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_ICENABLER0,
553554 vgic_mmio_read_enable, vgic_mmio_write_cenable, 4,
554555 VGIC_ACCESS_32bit),
555− REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ISPENDR0,
556+ REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ISPENDR0,
556557 vgic_mmio_read_pending, vgic_mmio_write_spending,
557558 vgic_v3_uaccess_read_pending, vgic_v3_uaccess_write_pending, 4,
558559 VGIC_ACCESS_32bit),
559− REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ICPENDR0,
560+ REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ICPENDR0,
560561 vgic_mmio_read_pending, vgic_mmio_write_cpending,
561562 vgic_mmio_read_raz, vgic_mmio_uaccess_write_wi, 4,
562563 VGIC_ACCESS_32bit),
563− REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ISACTIVER0,
564+ REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ISACTIVER0,
564565 vgic_mmio_read_active, vgic_mmio_write_sactive,
565566 NULL, vgic_mmio_uaccess_write_sactive,
566567 4, VGIC_ACCESS_32bit),
567− REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ICACTIVER0,
568+ REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ICACTIVER0,
568569 vgic_mmio_read_active, vgic_mmio_write_cactive,
569570 NULL, vgic_mmio_uaccess_write_cactive,
570571 4, VGIC_ACCESS_32bit),
571− REGISTER_DESC_WITH_LENGTH(GICR_IPRIORITYR0,
572+ REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_IPRIORITYR0,
572573 vgic_mmio_read_priority, vgic_mmio_write_priority, 32,
573574 VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
574− REGISTER_DESC_WITH_LENGTH(GICR_ICFGR0,
575+ REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_ICFGR0,
575576 vgic_mmio_read_config, vgic_mmio_write_config, 8,
576577 VGIC_ACCESS_32bit),
577− REGISTER_DESC_WITH_LENGTH(GICR_IGRPMODR0,
578+ REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_IGRPMODR0,
578579 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
579580 VGIC_ACCESS_32bit),
580− REGISTER_DESC_WITH_LENGTH(GICR_NSACR,
581+ REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_NSACR,
581582 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
582583 VGIC_ACCESS_32bit),
583584 };
@@ -607,9 +608,8 @@ int vgic_register_redist_iodev(struct kvm_vcpu *vcpu) · 23 unmodified lines
607608 struct vgic_dist *vgic = &kvm->arch.vgic;
608609 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
609610 struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev;
610− struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev;
611611 struct vgic_redist_region *rdreg;
612− gpa_t rd_base, sgi_base;
612+ gpa_t rd_base;
613613 int ret;
614614
615615 if (!IS_VGIC_ADDR_UNDEF(vgic_cpu->rd_iodev.base_addr))
@@ -631,52 +631,31 @@ int vgic_register_redist_iodev(struct kvm_vcpu *vcpu) · 15 unmodified lines
631631 vgic_cpu->rdreg = rdreg;
632632
633633 rd_base = rdreg->base + rdreg->free_index * KVM_VGIC_V3_REDIST_SIZE;
634− sgi_base = rd_base + SZ_64K;
635634
636635 kvm_iodevice_init(&rd_dev->dev, &kvm_io_gic_ops);
637636 rd_dev->base_addr = rd_base;
638637 rd_dev->iodev_type = IODEV_REDIST;
639− rd_dev->regions = vgic_v3_rdbase_registers;
640− rd_dev->nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers);
638+ rd_dev->regions = vgic_v3_rd_registers;
639+ rd_dev->nr_regions = ARRAY_SIZE(vgic_v3_rd_registers);
641640 rd_dev->redist_vcpu = vcpu;
642641
643642 mutex_lock(&kvm->slots_lock);
644643 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, rd_base,
645− SZ_64K, &rd_dev->dev);
644+ 2 * SZ_64K, &rd_dev->dev);
646645 mutex_unlock(&kvm->slots_lock);
647646
648647 if (ret)
649648 return ret;
650649
651− kvm_iodevice_init(&sgi_dev->dev, &kvm_io_gic_ops);
652− sgi_dev->base_addr = sgi_base;
653− sgi_dev->iodev_type = IODEV_REDIST;
654− sgi_dev->regions = vgic_v3_sgibase_registers;
655− sgi_dev->nr_regions = ARRAY_SIZE(vgic_v3_sgibase_registers);
656− sgi_dev->redist_vcpu = vcpu;
657−
658− mutex_lock(&kvm->slots_lock);
659− ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, sgi_base,
660− SZ_64K, &sgi_dev->dev);
661− if (ret) {
662− kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS,
663− &rd_dev->dev);
664− goto out;
665− }
666−
667650 rdreg->free_index++;
668−out:
669− mutex_unlock(&kvm->slots_lock);
670− return ret;
651+ return 0;
671652 }
672653
673654 static void vgic_unregister_redist_iodev(struct kvm_vcpu *vcpu)
674655 {
675656 struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev;
676− struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev;
677657
678658 kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &rd_dev->dev);
679− kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &sgi_dev->dev);
680659 }
681660
682661 static int vgic_register_all_redist_iodevs(struct kvm *kvm)
@@ -826,8 +805,8 @@ int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr) · 143 unmodified lines
826805 iodev.base_addr = 0;
827806 break;
828807 case KVM_DEV_ARM_VGIC_GRP_REDIST_REGS:{
829− iodev.regions = vgic_v3_rdbase_registers;
830− iodev.nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers);
808+ iodev.regions = vgic_v3_rd_registers;
809+ iodev.nr_regions = ARRAY_SIZE(vgic_v3_rd_registers);
831810 iodev.base_addr = 0;
832811 break;
833812 }
@@ -985,21 +964,11 @@ int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write, · 151 unmodified lines
985964 int offset, u32 *val)
986965 {
987966 struct vgic_io_device rd_dev = {
988− .regions = vgic_v3_rdbase_registers,
989− .nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers),
967+ .regions = vgic_v3_rd_registers,
968+ .nr_regions = ARRAY_SIZE(vgic_v3_rd_registers),
990969 };
991970
992− struct vgic_io_device sgi_dev = {
993− .regions = vgic_v3_sgibase_registers,
994− .nr_regions = ARRAY_SIZE(vgic_v3_sgibase_registers),
995− };
996−
997− /* SGI_base is the next 64K frame after RD_base */
998− if (offset >= SZ_64K)
999− return vgic_uaccess(vcpu, &sgi_dev, is_write, offset - SZ_64K,
1000− val);
1001− else
1002− return vgic_uaccess(vcpu, &rd_dev, is_write, offset, val);
971+ return vgic_uaccess(vcpu, &rd_dev, is_write, offset, val);
1003972 }
1004973
1005974 int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
virt/kvm/arm/vgic/vgic-v2.c+4−3
@@ -357,10 +357,11 @@ out: · 356 unmodified lines
357357 DEFINE_STATIC_KEY_FALSE(vgic_v2_cpuif_trap);
358358
359359 /**
360− * vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT
361− * @node: pointer to the DT node
360+ * vgic_v2_probe - probe for a VGICv2 compatible interrupt controller
361+ * @info: pointer to the GIC description
362362 *
363− * Returns 0 if a GICv2 has been found, returns an error code otherwise
363+ * Returns 0 if the VGICv2 has been probed successfully, returns an error code
364+ * otherwise
364365 */
365366 int vgic_v2_probe(const struct gic_kvm_info *info)
366367 {
virt/kvm/arm/vgic/vgic-v3.c+4−3
@@ -573,10 +573,11 @@ static int __init early_gicv4_enable(char *buf) · 572 unmodified lines
573573 early_param("kvm-arm.vgic_v4_enable", early_gicv4_enable);
574574
575575 /**
576− * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
577− * @node: pointer to the DT node
576+ * vgic_v3_probe - probe for a VGICv3 compatible interrupt controller
577+ * @info: pointer to the GIC description
578578 *
579− * Returns 0 if a GICv3 has been found, returns an error code otherwise
579+ * Returns 0 if the VGICv3 has been probed successfully, returns an error code
580+ * otherwise
580581 */
581582 int vgic_v3_probe(const struct gic_kvm_info *info)
582583 {
virt/kvm/arm/vgic/vgic.c+17−9
@@ -119,6 +119,22 @@ static void vgic_irq_release(struct kref *ref) · 118 unmodified lines
119119 {
120120 }
121121
122+/*
123+ * Drop the refcount on the LPI. Must be called with lpi_list_lock held.
124+ */
125+void __vgic_put_lpi_locked(struct kvm *kvm, struct vgic_irq *irq)
126+{
127+ struct vgic_dist *dist = &kvm->arch.vgic;
128+
129+ if (!kref_put(&irq->refcount, vgic_irq_release))
130+ return;
131+
132+ list_del(&irq->lpi_list);
133+ dist->lpi_list_count--;
134+
135+ kfree(irq);
136+}
137+
122138 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
123139 {
124140 struct vgic_dist *dist = &kvm->arch.vgic;
@@ -128,16 +144,8 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) · 3 unmodified lines
128144 return;
129145
130146 raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
131− if (!kref_put(&irq->refcount, vgic_irq_release)) {
132− raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
133− return;
134− };
135−
136− list_del(&irq->lpi_list);
137− dist->lpi_list_count--;
147+ __vgic_put_lpi_locked(kvm, irq);
138148 raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
139−
140− kfree(irq);
141149 }
142150
143151 void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu)
virt/kvm/arm/vgic/vgic.h+5−0
@@ -161,6 +161,7 @@ vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev, · 160 unmodified lines
161161 gpa_t addr, int len);
162162 struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
163163 u32 intid);
164+void __vgic_put_lpi_locked(struct kvm *kvm, struct vgic_irq *irq);
164165 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq);
165166 bool vgic_get_phys_line_level(struct vgic_irq *irq);
166167 void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending);
@@ -307,6 +308,10 @@ int vgic_copy_lpi_list(struct kvm *kvm, struct kvm_vcpu *vcpu, u32 **intid_ptr); · 140 unmodified lines
307308 int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
308309 u32 devid, u32 eventid, struct vgic_irq **irq);
309310 struct vgic_its *vgic_msi_to_its(struct kvm *kvm, struct kvm_msi *msi);
311+int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi);
312+void vgic_lpi_translation_cache_init(struct kvm *kvm);
313+void vgic_lpi_translation_cache_destroy(struct kvm *kvm);
314+void vgic_its_invalidate_cache(struct kvm *kvm);
310315
311316 bool vgic_supports_direct_msis(struct kvm *kvm);
312317 int vgic_v4_init(struct kvm *kvm);
virt/kvm/kvm_main.c+3−4
@@ -2321,6 +2321,8 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) · 2320 unmodified lines
23212321 bool waited = false;
23222322 u64 block_ns;
23232323
2324+ kvm_arch_vcpu_blocking(vcpu);
2325+
23242326 start = cur = ktime_get();
23252327 if (vcpu->halt_poll_ns && !kvm_arch_no_poll(vcpu)) {
23262328 ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns);
@@ -2341,8 +2343,6 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) · 14 unmodified lines
23412343 } while (single_task_running() && ktime_before(cur, stop));
23422344 }
23432345
2344− kvm_arch_vcpu_blocking(vcpu);
2345−
23462346 for (;;) {
23472347 prepare_to_swait_exclusive(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
23482348
@@ -2355,9 +2355,8 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) · 6 unmodified lines
23552355
23562356 finish_swait(&vcpu->wq, &wait);
23572357 cur = ktime_get();
2358−
2359− kvm_arch_vcpu_unblocking(vcpu);
23602358 out:
2359+ kvm_arch_vcpu_unblocking(vcpu);
23612360 block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
23622361
23632362 if (!vcpu_valid_wakeup(vcpu))

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t