History

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

Pull KVM changes from Paolo Bonzini: "Here are the 3.13 KVM changes. There was a lot of work on the PPC side: the HV and emulation flavors can now coexist in a single kernel is probably the most interesting change from a user point of view. On the x86 side there are nested virtualization improvements and a few bugfixes. ARM got transparent huge page support, improved overcommit, and support for big endian guests. Finally, there is a new interface to connect KVM with VFIO. This helps with devices that use NoSnoop PCI transactions, letting the driver in the guest execute WBINVD instructions. This includes some nVidia cards on Windows, that fail to start without these patches and the corresponding userspace changes" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: (146 commits) kvm, vmx: Fix lazy FPU on nested guest arm/arm64: KVM: PSCI: propagate caller endianness to the incoming vcpu arm/arm64: KVM: MMIO support for BE guest kvm, cpuid: Fix sparse warning kvm: Delete prototype for non-existent function kvm_check_iopl kvm: Delete prototype for non-existent function complete_pio hung_task: add method to reset detector pvclock: detect watchdog reset at pvclock read kvm: optimize out smp_mb after srcu_read_unlock srcu: API for barrier after srcu read unlock KVM: remove vm mmap method KVM: IOMMU: hva align mapping page size KVM: x86: trace cpuid emulation when called from emulator KVM: emulator: cleanup decode_register_operand() a bit KVM: emulator: check rex prefix inside decode_register() KVM: x86: fix emulation of "movzbl %bpl, %eax" kvm_host: typo fix KVM: x86: emulate SAHF instruction MAINTAINERS: add tree for kvm.git Documentation/kvm: add a 00-INDEX file ...

Linus Torvalds committed 13 years ago

Commit f0804802 parents eda670c e504c90133 files · +5170 −2239Browse files

Unified · Split

133 files
  1. Documentation/virtual/kvm/00-INDEX +24 −0
  2. Documentation/virtual/kvm/api.txt +145 −7
  3. Documentation/virtual/kvm/cpuid.txt +7 −0
  4. Documentation/virtual/kvm/devices/vfio.txt +22 −0
  5. Documentation/virtual/kvm/locking.txt +17 −2
  6. MAINTAINERS +2 −1
  7. arch/arm/include/asm/kvm_arm.h +6 −3
  8. arch/arm/include/asm/kvm_asm.h +1 −1
  9. arch/arm/include/asm/kvm_emulate.h +51 −0
  10. arch/arm/include/asm/kvm_host.h +1 −5
  11. arch/arm/include/asm/kvm_mmu.h +14 −3
  12. arch/arm/include/asm/pgtable-3level.h +2 −0
  13. arch/arm/include/uapi/asm/kvm.h +2 −1
  14. arch/arm/kvm/Kconfig +1 −0
  15. arch/arm/kvm/Makefile +1 −1
  16. arch/arm/kvm/arm.c +16 −2
  17. arch/arm/kvm/coproc.c +120 −0
  18. arch/arm/kvm/coproc_a15.c +1 −116
  19. arch/arm/kvm/coproc_a7.c +54 −0
  20. arch/arm/kvm/emulate.c +1 −1
  21. arch/arm/kvm/guest.c +23 −1
  22. arch/arm/kvm/handle_exit.c +13 −7
  23. arch/arm/kvm/mmio.c +75 −11
  24. arch/arm/kvm/mmu.c +185 −38
  25. arch/arm/kvm/psci.c +17 −4
  26. arch/arm/kvm/reset.c +6 −9
  27. arch/arm64/include/asm/kvm_arm.h +6 −2
  28. arch/arm64/include/asm/kvm_emulate.h +61 −0
  29. arch/arm64/include/asm/kvm_host.h +1 −5
  30. arch/arm64/include/asm/kvm_mmu.h +9 −3
  31. arch/arm64/include/asm/pgtable-hwdef.h +2 −0
  32. arch/arm64/kvm/Kconfig +1 −0
  33. arch/arm64/kvm/guest.c +20 −0
  34. arch/arm64/kvm/handle_exit.c +13 −5
  35. arch/ia64/include/asm/kvm_host.h +1 −5
  36. arch/ia64/kvm/kvm-ia64.c +3 −2
  37. arch/mips/include/asm/kvm_host.h +0 −7
  38. arch/mips/kvm/kvm_mips.c +3 −2
  39. arch/powerpc/include/asm/disassemble.h +4 −0
  40. arch/powerpc/include/asm/exception-64s.h +20 −1
  41. arch/powerpc/include/asm/kvm_asm.h +4 −0
  42. arch/powerpc/include/asm/kvm_book3s.h +19 −213
  43. arch/powerpc/include/asm/kvm_book3s_32.h +1 −1
  44. arch/powerpc/include/asm/kvm_book3s_64.h +4 −4
  45. arch/powerpc/include/asm/kvm_book3s_asm.h +5 −4
  46. arch/powerpc/include/asm/kvm_booke.h +6 −1
  47. arch/powerpc/include/asm/kvm_host.h +34 −23
  48. arch/powerpc/include/asm/kvm_ppc.h +79 −28
  49. arch/powerpc/include/asm/paca.h +1 −1
  50. arch/powerpc/include/asm/processor.h +2 −0
  51. arch/powerpc/include/asm/pte-book3e.h +1 −1
  52. arch/powerpc/include/asm/reg.h +15 −0
  53. arch/powerpc/include/uapi/asm/kvm.h +82 −4
  54. arch/powerpc/kernel/asm-offsets.c +14 −7
  55. arch/powerpc/kernel/exceptions-64s.S +28 −2
  56. arch/powerpc/kernel/idle_power7.S +1 −1
  57. arch/powerpc/kernel/traps.c +1 −1
  58. arch/powerpc/kvm/44x.c +43 −15
  59. arch/powerpc/kvm/44x_emulate.c +4 −4
  60. arch/powerpc/kvm/44x_tlb.c +1 −1
  61. arch/powerpc/kvm/Kconfig +22 −6
  62. arch/powerpc/kvm/Makefile +21 −8
  63. arch/powerpc/kvm/book3s.c +246 −11
  64. arch/powerpc/kvm/book3s.h +34 −0
  65. arch/powerpc/kvm/book3s_32_mmu.c +42 −31
  66. arch/powerpc/kvm/book3s_32_mmu_host.c +12 −4
  67. arch/powerpc/kvm/book3s_64_mmu.c +148 −33
  68. arch/powerpc/kvm/book3s_64_mmu_host.c +79 −27
  69. arch/powerpc/kvm/book3s_64_mmu_hv.c +10 −14
  70. arch/powerpc/kvm/book3s_64_vio_hv.c +1 −0
  71. arch/powerpc/kvm/book3s_emulate.c +7 −11
  72. arch/powerpc/kvm/book3s_exports.c +3 −2
  73. arch/powerpc/kvm/book3s_hv.c +309 −80
  74. arch/powerpc/kvm/book3s_hv_interrupts.S +0 −3
  75. arch/powerpc/kvm/book3s_hv_rmhandlers.S +356 −262
  76. arch/powerpc/kvm/book3s_interrupts.S +26 −6
  77. arch/powerpc/kvm/book3s_mmu_hpte.c +61 −5
  78. arch/powerpc/kvm/book3s_pr.c +361 −137
  79. arch/powerpc/kvm/book3s_pr_papr.c +37 −15
  80. arch/powerpc/kvm/book3s_rmhandlers.S +5 −27
  81. arch/powerpc/kvm/book3s_rtas.c +1 −0
  82. arch/powerpc/kvm/book3s_segment.S +2 −2
  83. arch/powerpc/kvm/book3s_xics.c +4 −3
  84. arch/powerpc/kvm/booke.c +299 −38
  85. arch/powerpc/kvm/booke.h +29 −0
  86. arch/powerpc/kvm/e500.c +44 −15
  87. arch/powerpc/kvm/e500.h +1 −1
  88. arch/powerpc/kvm/e500_emulate.c +30 −4
  89. arch/powerpc/kvm/e500_mmu.c +2 −2
  90. arch/powerpc/kvm/e500_mmu_host.c +5 −1
  91. arch/powerpc/kvm/e500mc.c +43 −15
  92. arch/powerpc/kvm/emulate.c +7 −5
  93. arch/powerpc/kvm/powerpc.c +95 −76
  94. arch/powerpc/kvm/trace.h +0 −429
  95. arch/powerpc/kvm/trace_booke.h +177 −0
  96. arch/powerpc/kvm/trace_pr.h +297 −0
  97. arch/s390/include/asm/kvm_host.h +0 −8
  98. arch/s390/kvm/diag.c +1 −3
  99. arch/s390/kvm/gaccess.h +14 −7
  100. arch/s390/kvm/intercept.c +0 −6
  101. arch/s390/kvm/interrupt.c +3 −0
  102. arch/s390/kvm/kvm-s390.c +63 −33
  103. arch/s390/kvm/kvm-s390.h +5 −4
  104. arch/s390/kvm/priv.c +61 −0
  105. arch/x86/include/asm/kvm_emulate.h +7 −3
  106. arch/x86/include/asm/kvm_host.h +15 −8
  107. arch/x86/include/asm/pvclock.h +2 −0
  108. arch/x86/include/uapi/asm/kvm.h +3 −3
  109. arch/x86/include/uapi/asm/msr-index.h +1 −0
  110. arch/x86/kernel/kvmclock.c +1 −0
  111. arch/x86/kernel/pvclock.c +13 −0
  112. arch/x86/kvm/Kconfig +1 −0
  113. arch/x86/kvm/Makefile +1 −1
  114. arch/x86/kvm/cpuid.c +106 −9
  115. arch/x86/kvm/cpuid.h +3 −2
  116. arch/x86/kvm/emulate.c +107 −23
  117. arch/x86/kvm/mmu.c +34 −81
  118. arch/x86/kvm/mmu.h +2 −2
  119. arch/x86/kvm/svm.c +2 −6
  120. arch/x86/kvm/vmx.c +113 −45
  121. arch/x86/kvm/x86.c +77 −31
  122. arch/x86/kvm/x86.h +1 −0
  123. include/linux/kvm_host.h +26 −16
  124. include/linux/sched.h +8 −0
  125. include/linux/srcu.h +14 −0
  126. include/trace/events/kvm.h +4 −6
  127. include/uapi/linux/kvm.h +11 −0
  128. kernel/hung_task.c +11 −0
  129. virt/kvm/Kconfig +3 −0
  130. virt/kvm/async_pf.c +6 −16
  131. virt/kvm/iommu.c +20 −18
  132. virt/kvm/kvm_main.c +55 −79
  133. virt/kvm/vfio.c +264 −0
Documentation/virtual/kvm/00-INDEX+24−0
@@ -0,0 +1,24 @@
1+00-INDEX
2+ - this file.
3+api.txt
4+ - KVM userspace API.
5+cpuid.txt
6+ - KVM-specific cpuid leaves (x86).
7+devices/
8+ - KVM_CAP_DEVICE_CTRL userspace API.
9+hypercalls.txt
10+ - KVM hypercalls.
11+locking.txt
12+ - notes on KVM locks.
13+mmu.txt
14+ - the x86 kvm shadow mmu.
15+msr.txt
16+ - KVM-specific MSRs (x86).
17+nested-vmx.txt
18+ - notes on nested virtualization for Intel x86 processors.
19+ppc-pv.txt
20+ - the paravirtualization interface on PowerPC.
21+review-checklist.txt
22+ - review checklist for KVM patches.
23+timekeeping.txt
24+ - timekeeping virtualization for x86-based architectures.
Documentation/virtual/kvm/api.txt+145−7
@@ -1122,9 +1122,9 @@ struct kvm_cpuid2 { · 1121 unmodified lines
11221122 struct kvm_cpuid_entry2 entries[0];
11231123 };
11241124
1125−#define KVM_CPUID_FLAG_SIGNIFCANT_INDEX 1
1126−#define KVM_CPUID_FLAG_STATEFUL_FUNC 2
1127−#define KVM_CPUID_FLAG_STATE_READ_NEXT 4
1125+#define KVM_CPUID_FLAG_SIGNIFCANT_INDEX BIT(0)
1126+#define KVM_CPUID_FLAG_STATEFUL_FUNC BIT(1)
1127+#define KVM_CPUID_FLAG_STATE_READ_NEXT BIT(2)
11281128
11291129 struct kvm_cpuid_entry2 {
11301130 __u32 function;
@@ -1810,6 +1810,50 @@ registers, find a list below: · 679 unmodified lines
18101810 PPC | KVM_REG_PPC_TLB3PS | 32
18111811 PPC | KVM_REG_PPC_EPTCFG | 32
18121812 PPC | KVM_REG_PPC_ICP_STATE | 64
1813+ PPC | KVM_REG_PPC_TB_OFFSET | 64
1814+ PPC | KVM_REG_PPC_SPMC1 | 32
1815+ PPC | KVM_REG_PPC_SPMC2 | 32
1816+ PPC | KVM_REG_PPC_IAMR | 64
1817+ PPC | KVM_REG_PPC_TFHAR | 64
1818+ PPC | KVM_REG_PPC_TFIAR | 64
1819+ PPC | KVM_REG_PPC_TEXASR | 64
1820+ PPC | KVM_REG_PPC_FSCR | 64
1821+ PPC | KVM_REG_PPC_PSPB | 32
1822+ PPC | KVM_REG_PPC_EBBHR | 64
1823+ PPC | KVM_REG_PPC_EBBRR | 64
1824+ PPC | KVM_REG_PPC_BESCR | 64
1825+ PPC | KVM_REG_PPC_TAR | 64
1826+ PPC | KVM_REG_PPC_DPDES | 64
1827+ PPC | KVM_REG_PPC_DAWR | 64
1828+ PPC | KVM_REG_PPC_DAWRX | 64
1829+ PPC | KVM_REG_PPC_CIABR | 64
1830+ PPC | KVM_REG_PPC_IC | 64
1831+ PPC | KVM_REG_PPC_VTB | 64
1832+ PPC | KVM_REG_PPC_CSIGR | 64
1833+ PPC | KVM_REG_PPC_TACR | 64
1834+ PPC | KVM_REG_PPC_TCSCR | 64
1835+ PPC | KVM_REG_PPC_PID | 64
1836+ PPC | KVM_REG_PPC_ACOP | 64
1837+ PPC | KVM_REG_PPC_VRSAVE | 32
1838+ PPC | KVM_REG_PPC_LPCR | 64
1839+ PPC | KVM_REG_PPC_PPR | 64
1840+ PPC | KVM_REG_PPC_ARCH_COMPAT 32
1841+ PPC | KVM_REG_PPC_TM_GPR0 | 64
1842+ ...
1843+ PPC | KVM_REG_PPC_TM_GPR31 | 64
1844+ PPC | KVM_REG_PPC_TM_VSR0 | 128
1845+ ...
1846+ PPC | KVM_REG_PPC_TM_VSR63 | 128
1847+ PPC | KVM_REG_PPC_TM_CR | 64
1848+ PPC | KVM_REG_PPC_TM_LR | 64
1849+ PPC | KVM_REG_PPC_TM_CTR | 64
1850+ PPC | KVM_REG_PPC_TM_FPSCR | 64
1851+ PPC | KVM_REG_PPC_TM_AMR | 64
1852+ PPC | KVM_REG_PPC_TM_PPR | 64
1853+ PPC | KVM_REG_PPC_TM_VRSAVE | 64
1854+ PPC | KVM_REG_PPC_TM_VSCR | 32
1855+ PPC | KVM_REG_PPC_TM_DSCR | 64
1856+ PPC | KVM_REG_PPC_TM_TAR | 64
18131857
18141858 ARM registers are mapped using the lower 32 bits. The upper 16 of that
18151859 is the register group type, or coprocessor number:
@@ -2304,7 +2348,31 @@ Possible features: · 488 unmodified lines
23042348 Depends on KVM_CAP_ARM_EL1_32BIT (arm64 only).
23052349
23062350
2307−4.83 KVM_GET_REG_LIST
2351+4.83 KVM_ARM_PREFERRED_TARGET
2352+
2353+Capability: basic
2354+Architectures: arm, arm64
2355+Type: vm ioctl
2356+Parameters: struct struct kvm_vcpu_init (out)
2357+Returns: 0 on success; -1 on error
2358+Errors:
2359+ ENODEV: no preferred target available for the host
2360+
2361+This queries KVM for preferred CPU target type which can be emulated
2362+by KVM on underlying host.
2363+
2364+The ioctl returns struct kvm_vcpu_init instance containing information
2365+about preferred CPU target type and recommended features for it. The
2366+kvm_vcpu_init->features bitmap returned will have feature bits set if
2367+the preferred target recommends setting these features, but this is
2368+not mandatory.
2369+
2370+The information returned by this ioctl can be used to prepare an instance
2371+of struct kvm_vcpu_init for KVM_ARM_VCPU_INIT ioctl which will result in
2372+in VCPU matching underlying host.
2373+
2374+
2375+4.84 KVM_GET_REG_LIST
23082376
23092377 Capability: basic
23102378 Architectures: arm, arm64
@@ -2323,8 +2391,7 @@ struct kvm_reg_list { · 12 unmodified lines
23232391 This ioctl returns the guest registers that are supported for the
23242392 KVM_GET_ONE_REG/KVM_SET_ONE_REG calls.
23252393
2326−
2327−4.84 KVM_ARM_SET_DEVICE_ADDR
2394+4.85 KVM_ARM_SET_DEVICE_ADDR
23282395
23292396 Capability: KVM_CAP_ARM_SET_DEVICE_ADDR
23302397 Architectures: arm, arm64
@@ -2362,7 +2429,7 @@ must be called after calling KVM_CREATE_IRQCHIP, but before calling · 31 unmodified lines
23622429 KVM_RUN on any of the VCPUs. Calling this ioctl twice for any of the
23632430 base addresses will return -EEXIST.
23642431
2365−4.85 KVM_PPC_RTAS_DEFINE_TOKEN
2432+4.86 KVM_PPC_RTAS_DEFINE_TOKEN
23662433
23672434 Capability: KVM_CAP_PPC_RTAS
23682435 Architectures: ppc
@@ -2661,6 +2728,77 @@ and usually define the validity of a groups of registers. (e.g. one bit · 292 unmodified lines
26612728 };
26622729
26632730
2731+4.81 KVM_GET_EMULATED_CPUID
2732+
2733+Capability: KVM_CAP_EXT_EMUL_CPUID
2734+Architectures: x86
2735+Type: system ioctl
2736+Parameters: struct kvm_cpuid2 (in/out)
2737+Returns: 0 on success, -1 on error
2738+
2739+struct kvm_cpuid2 {
2740+ __u32 nent;
2741+ __u32 flags;
2742+ struct kvm_cpuid_entry2 entries[0];
2743+};
2744+
2745+The member 'flags' is used for passing flags from userspace.
2746+
2747+#define KVM_CPUID_FLAG_SIGNIFCANT_INDEX BIT(0)
2748+#define KVM_CPUID_FLAG_STATEFUL_FUNC BIT(1)
2749+#define KVM_CPUID_FLAG_STATE_READ_NEXT BIT(2)
2750+
2751+struct kvm_cpuid_entry2 {
2752+ __u32 function;
2753+ __u32 index;
2754+ __u32 flags;
2755+ __u32 eax;
2756+ __u32 ebx;
2757+ __u32 ecx;
2758+ __u32 edx;
2759+ __u32 padding[3];
2760+};
2761+
2762+This ioctl returns x86 cpuid features which are emulated by
2763+kvm.Userspace can use the information returned by this ioctl to query
2764+which features are emulated by kvm instead of being present natively.
2765+
2766+Userspace invokes KVM_GET_EMULATED_CPUID by passing a kvm_cpuid2
2767+structure with the 'nent' field indicating the number of entries in
2768+the variable-size array 'entries'. If the number of entries is too low
2769+to describe the cpu capabilities, an error (E2BIG) is returned. If the
2770+number is too high, the 'nent' field is adjusted and an error (ENOMEM)
2771+is returned. If the number is just right, the 'nent' field is adjusted
2772+to the number of valid entries in the 'entries' array, which is then
2773+filled.
2774+
2775+The entries returned are the set CPUID bits of the respective features
2776+which kvm emulates, as returned by the CPUID instruction, with unknown
2777+or unsupported feature bits cleared.
2778+
2779+Features like x2apic, for example, may not be present in the host cpu
2780+but are exposed by kvm in KVM_GET_SUPPORTED_CPUID because they can be
2781+emulated efficiently and thus not included here.
2782+
2783+The fields in each entry are defined as follows:
2784+
2785+ function: the eax value used to obtain the entry
2786+ index: the ecx value used to obtain the entry (for entries that are
2787+ affected by ecx)
2788+ flags: an OR of zero or more of the following:
2789+ KVM_CPUID_FLAG_SIGNIFCANT_INDEX:
2790+ if the index field is valid
2791+ KVM_CPUID_FLAG_STATEFUL_FUNC:
2792+ if cpuid for this function returns different values for successive
2793+ invocations; there will be several entries with the same function,
2794+ all with this flag set
2795+ KVM_CPUID_FLAG_STATE_READ_NEXT:
2796+ for KVM_CPUID_FLAG_STATEFUL_FUNC entries, set if this entry is
2797+ the first entry to be read by a cpu
2798+ eax, ebx, ecx, edx: the values returned by the cpuid instruction for
2799+ this function/index combination
2800+
2801+
26642802 6. Capabilities that can be enabled
26652803 -----------------------------------
26662804
Documentation/virtual/kvm/cpuid.txt+7−0
@@ -43,6 +43,13 @@ KVM_FEATURE_CLOCKSOURCE2 || 3 || kvmclock available at msrs · 42 unmodified lines
4343 KVM_FEATURE_ASYNC_PF || 4 || async pf can be enabled by
4444 || || writing to msr 0x4b564d02
4545 ------------------------------------------------------------------------------
46+KVM_FEATURE_STEAL_TIME || 5 || steal time can be enabled by
47+ || || writing to msr 0x4b564d03.
48+------------------------------------------------------------------------------
49+KVM_FEATURE_PV_EOI || 6 || paravirtualized end of interrupt
50+ || || handler can be enabled by writing
51+ || || to msr 0x4b564d04.
52+------------------------------------------------------------------------------
4653 KVM_FEATURE_PV_UNHALT || 7 || guest checks this feature bit
4754 || || before enabling paravirtualized
4855 || || spinlock support.
Documentation/virtual/kvm/devices/vfio.txt+22−0
@@ -0,0 +1,22 @@
1+VFIO virtual device
2+===================
3+
4+Device types supported:
5+ KVM_DEV_TYPE_VFIO
6+
7+Only one VFIO instance may be created per VM. The created device
8+tracks VFIO groups in use by the VM and features of those groups
9+important to the correctness and acceleration of the VM. As groups
10+are enabled and disabled for use by the VM, KVM should be updated
11+about their presence. When registered with KVM, a reference to the
12+VFIO-group is held by KVM.
13+
14+Groups:
15+ KVM_DEV_VFIO_GROUP
16+
17+KVM_DEV_VFIO_GROUP attributes:
18+ KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking
19+ KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking
20+
21+For each, kvm_device_attr.addr points to an int32_t file descriptor
22+for the VFIO group.
Documentation/virtual/kvm/locking.txt+17−2
@@ -132,10 +132,14 @@ See the comments in spte_has_volatile_bits() and mmu_spte_update(). · 131 unmodified lines
132132 ------------
133133
134134 Name: kvm_lock
135−Type: raw_spinlock
135+Type: spinlock_t
136136 Arch: any
137137 Protects: - vm_list
138− - hardware virtualization enable/disable
138+
139+Name: kvm_count_lock
140+Type: raw_spinlock_t
141+Arch: any
142+Protects: - hardware virtualization enable/disable
139143 Comment: 'raw' because hardware enabling/disabling must be atomic /wrt
140144 migration.
141145
@@ -151,3 +155,14 @@ Type: spinlock_t · 9 unmodified lines
151155 Arch: any
152156 Protects: -shadow page/shadow tlb entry
153157 Comment: it is a spinlock since it is used in mmu notifier.
158+
159+Name: kvm->srcu
160+Type: srcu lock
161+Arch: any
162+Protects: - kvm->memslots
163+ - kvm->buses
164+Comment: The srcu read lock must be held while accessing memslots (e.g.
165+ when using gfn_to_* functions) and while accessing in-kernel
166+ MMIO/PIO address->device structure mapping (kvm->buses).
167+ The srcu index can be stored in kvm_vcpu->srcu_idx per vcpu
168+ if it is needed by multiple functions.
MAINTAINERS+2−1
@@ -4871,7 +4871,8 @@ KERNEL VIRTUAL MACHINE (KVM) · 4870 unmodified lines
48714871 M: Gleb Natapov <gleb@redhat.com>
48724872 M: Paolo Bonzini <pbonzini@redhat.com>
48734873 L: kvm@vger.kernel.org
4874−W: http://linux-kvm.org
4874+W: http://www.linux-kvm.org
4875+T: git git://git.kernel.org/pub/scm/virt/kvm/kvm.git
48754876 S: Supported
48764877 F: Documentation/*/kvm*.txt
48774878 F: Documentation/virtual/kvm/
arch/arm/include/asm/kvm_arm.h+6−3
@@ -57,6 +57,7 @@ · 56 unmodified lines
5757 * TSC: Trap SMC
5858 * TSW: Trap cache operations by set/way
5959 * TWI: Trap WFI
60+ * TWE: Trap WFE
6061 * TIDCP: Trap L2CTLR/L2ECTLR
6162 * BSU_IS: Upgrade barriers to the inner shareable domain
6263 * FB: Force broadcast of all maintainance operations
@@ -67,7 +68,7 @@ · 4 unmodified lines
6768 */
6869 #define HCR_GUEST_MASK (HCR_TSC | HCR_TSW | HCR_TWI | HCR_VM | HCR_BSU_IS | \
6970 HCR_FB | HCR_TAC | HCR_AMO | HCR_IMO | HCR_FMO | \
70− HCR_SWIO | HCR_TIDCP)
71+ HCR_TWE | HCR_SWIO | HCR_TIDCP)
7172 #define HCR_VIRT_EXCP_MASK (HCR_VA | HCR_VI | HCR_VF)
7273
7374 /* System Control Register (SCTLR) bits */
@@ -95,12 +96,12 @@ · 21 unmodified lines
9596 #define TTBCR_IRGN1 (3 << 24)
9697 #define TTBCR_EPD1 (1 << 23)
9798 #define TTBCR_A1 (1 << 22)
98−#define TTBCR_T1SZ (3 << 16)
99+#define TTBCR_T1SZ (7 << 16)
99100 #define TTBCR_SH0 (3 << 12)
100101 #define TTBCR_ORGN0 (3 << 10)
101102 #define TTBCR_IRGN0 (3 << 8)
102103 #define TTBCR_EPD0 (1 << 7)
103−#define TTBCR_T0SZ 3
104+#define TTBCR_T0SZ (7 << 0)
104105 #define HTCR_MASK (TTBCR_T0SZ | TTBCR_IRGN0 | TTBCR_ORGN0 | TTBCR_SH0)
105106
106107 /* Hyp System Trap Register */
@@ -208,6 +209,8 @@ · 101 unmodified lines
208209 #define HSR_EC_DABT (0x24)
209210 #define HSR_EC_DABT_HYP (0x25)
210211
212+#define HSR_WFI_IS_WFE (1U << 0)
213+
211214 #define HSR_HVC_IMM_MASK ((1UL << 16) - 1)
212215
213216 #define HSR_DABT_S1PTW (1U << 7)
arch/arm/include/asm/kvm_asm.h+1−1
@@ -39,7 +39,7 @@ · 38 unmodified lines
3939 #define c6_IFAR 17 /* Instruction Fault Address Register */
4040 #define c7_PAR 18 /* Physical Address Register */
4141 #define c7_PAR_high 19 /* PAR top 32 bits */
42−#define c9_L2CTLR 20 /* Cortex A15 L2 Control Register */
42+#define c9_L2CTLR 20 /* Cortex A15/A7 L2 Control Register */
4343 #define c10_PRRR 21 /* Primary Region Remap Register */
4444 #define c10_NMRR 22 /* Normal Memory Remap Register */
4545 #define c12_VBAR 23 /* Vector Base Address Register */
arch/arm/include/asm/kvm_emulate.h+51−0
@@ -157,4 +157,55 @@ static inline u32 kvm_vcpu_hvc_get_imm(struct kvm_vcpu *vcpu) · 156 unmodified lines
157157 return kvm_vcpu_get_hsr(vcpu) & HSR_HVC_IMM_MASK;
158158 }
159159
160+static inline unsigned long kvm_vcpu_get_mpidr(struct kvm_vcpu *vcpu)
161+{
162+ return vcpu->arch.cp15[c0_MPIDR];
163+}
164+
165+static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
166+{
167+ *vcpu_cpsr(vcpu) |= PSR_E_BIT;
168+}
169+
170+static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
171+{
172+ return !!(*vcpu_cpsr(vcpu) & PSR_E_BIT);
173+}
174+
175+static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
176+ unsigned long data,
177+ unsigned int len)
178+{
179+ if (kvm_vcpu_is_be(vcpu)) {
180+ switch (len) {
181+ case 1:
182+ return data & 0xff;
183+ case 2:
184+ return be16_to_cpu(data & 0xffff);
185+ default:
186+ return be32_to_cpu(data);
187+ }
188+ }
189+
190+ return data; /* Leave LE untouched */
191+}
192+
193+static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
194+ unsigned long data,
195+ unsigned int len)
196+{
197+ if (kvm_vcpu_is_be(vcpu)) {
198+ switch (len) {
199+ case 1:
200+ return data & 0xff;
201+ case 2:
202+ return cpu_to_be16(data & 0xffff);
203+ default:
204+ return cpu_to_be32(data);
205+ }
206+ }
207+
208+ return data; /* Leave LE untouched */
209+}
210+
160211 #endif /* __ARM_KVM_EMULATE_H__ */
arch/arm/include/asm/kvm_host.h+1−5
@@ -38,11 +38,6 @@ · 37 unmodified lines
3838
3939 #define KVM_VCPU_MAX_FEATURES 1
4040
41−/* We don't currently support large pages. */
42−#define KVM_HPAGE_GFN_SHIFT(x) 0
43−#define KVM_NR_PAGE_SIZES 1
44−#define KVM_PAGES_PER_HPAGE(x) (1UL<<31)
45−
4641 #include <kvm/arm_vgic.h>
4742
4843 struct kvm_vcpu;
@@ -154,6 +149,7 @@ struct kvm_vcpu_stat { · 105 unmodified lines
154149 struct kvm_vcpu_init;
155150 int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
156151 const struct kvm_vcpu_init *init);
152+int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
157153 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
158154 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
159155 struct kvm_one_reg;
arch/arm/include/asm/kvm_mmu.h+14−3
@@ -62,6 +62,12 @@ phys_addr_t kvm_get_idmap_vector(void); · 61 unmodified lines
6262 int kvm_mmu_init(void);
6363 void kvm_clear_hyp_idmap(void);
6464
65+static inline void kvm_set_pmd(pmd_t *pmd, pmd_t new_pmd)
66+{
67+ *pmd = new_pmd;
68+ flush_pmd_entry(pmd);
69+}
70+
6571 static inline void kvm_set_pte(pte_t *pte, pte_t new_pte)
6672 {
6773 *pte = new_pte;
@@ -103,9 +109,15 @@ static inline void kvm_set_s2pte_writable(pte_t *pte) · 35 unmodified lines
103109 pte_val(*pte) |= L_PTE_S2_RDWR;
104110 }
105111
112+static inline void kvm_set_s2pmd_writable(pmd_t *pmd)
113+{
114+ pmd_val(*pmd) |= L_PMD_S2_RDWR;
115+}
116+
106117 struct kvm;
107118
108−static inline void coherent_icache_guest_page(struct kvm *kvm, gfn_t gfn)
119+static inline void coherent_icache_guest_page(struct kvm *kvm, hva_t hva,
120+ unsigned long size)
109121 {
110122 /*
111123 * If we are going to insert an instruction page and the icache is
@@ -120,8 +132,7 @@ static inline void coherent_icache_guest_page(struct kvm *kvm, gfn_t gfn) · 8 unmodified lines
120132 * need any kind of flushing (DDI 0406C.b - Page B3-1392).
121133 */
122134 if (icache_is_pipt()) {
123− unsigned long hva = gfn_to_hva(kvm, gfn);
124− __cpuc_coherent_user_range(hva, hva + PAGE_SIZE);
135+ __cpuc_coherent_user_range(hva, hva + size);
125136 } else if (!icache_is_vivt_asid_tagged()) {
126137 /* any kind of VIPT cache */
127138 __flush_icache_all();
arch/arm/include/asm/pgtable-3level.h+2−0
@@ -126,6 +126,8 @@ · 125 unmodified lines
126126 #define L_PTE_S2_RDONLY (_AT(pteval_t, 1) << 6) /* HAP[1] */
127127 #define L_PTE_S2_RDWR (_AT(pteval_t, 3) << 6) /* HAP[2:1] */
128128
129+#define L_PMD_S2_RDWR (_AT(pmdval_t, 3) << 6) /* HAP[2:1] */
130+
129131 /*
130132 * Hyp-mode PL2 PTE definitions for LPAE.
131133 */
arch/arm/include/uapi/asm/kvm.h+2−1
@@ -63,7 +63,8 @@ struct kvm_regs { · 62 unmodified lines
6363
6464 /* Supported Processor Types */
6565 #define KVM_ARM_TARGET_CORTEX_A15 0
66−#define KVM_ARM_NUM_TARGETS 1
66+#define KVM_ARM_TARGET_CORTEX_A7 1
67+#define KVM_ARM_NUM_TARGETS 2
6768
6869 /* KVM_ARM_SET_DEVICE_ADDR ioctl id encoding */
6970 #define KVM_ARM_DEVICE_TYPE_SHIFT 0
arch/arm/kvm/Kconfig+1−0
@@ -20,6 +20,7 @@ config KVM · 19 unmodified lines
2020 bool "Kernel-based Virtual Machine (KVM) support"
2121 select PREEMPT_NOTIFIERS
2222 select ANON_INODES
23+ select HAVE_KVM_CPU_RELAX_INTERCEPT
2324 select KVM_MMIO
2425 select KVM_ARM_HOST
2526 depends on ARM_VIRT_EXT && ARM_LPAE
arch/arm/kvm/Makefile+1−1
@@ -19,6 +19,6 @@ kvm-arm-y = $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o · 18 unmodified lines
1919
2020 obj-y += kvm-arm.o init.o interrupts.o
2121 obj-y += arm.o handle_exit.o guest.o mmu.o emulate.o reset.o
22−obj-y += coproc.o coproc_a15.o mmio.o psci.o perf.o
22+obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o
2323 obj-$(CONFIG_KVM_ARM_VGIC) += $(KVM)/arm/vgic.o
2424 obj-$(CONFIG_KVM_ARM_TIMER) += $(KVM)/arm/arch_timer.o
arch/arm/kvm/arm.c+16−2
@@ -152,12 +152,13 @@ int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) · 151 unmodified lines
152152 return VM_FAULT_SIGBUS;
153153 }
154154
155−void kvm_arch_free_memslot(struct kvm_memory_slot *free,
155+void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
156156 struct kvm_memory_slot *dont)
157157 {
158158 }
159159
160−int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
160+int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
161+ unsigned long npages)
161162 {
162163 return 0;
163164 }
@@ -797,6 +798,19 @@ long kvm_arch_vm_ioctl(struct file *filp, · 633 unmodified lines
797798 return -EFAULT;
798799 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
799800 }
801+ case KVM_ARM_PREFERRED_TARGET: {
802+ int err;
803+ struct kvm_vcpu_init init;
804+
805+ err = kvm_vcpu_preferred_target(&init);
806+ if (err)
807+ return err;
808+
809+ if (copy_to_user(argp, &init, sizeof(init)))
810+ return -EFAULT;
811+
812+ return 0;
813+ }
800814 default:
801815 return -EINVAL;
802816 }
arch/arm/kvm/coproc.c+120−0
@@ -71,6 +71,98 @@ int kvm_handle_cp14_access(struct kvm_vcpu *vcpu, struct kvm_run *run) · 70 unmodified lines
7171 return 1;
7272 }
7373
74+static void reset_mpidr(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
75+{
76+ /*
77+ * Compute guest MPIDR. We build a virtual cluster out of the
78+ * vcpu_id, but we read the 'U' bit from the underlying
79+ * hardware directly.
80+ */
81+ vcpu->arch.cp15[c0_MPIDR] = ((read_cpuid_mpidr() & MPIDR_SMP_BITMASK) |
82+ ((vcpu->vcpu_id >> 2) << MPIDR_LEVEL_BITS) |
83+ (vcpu->vcpu_id & 3));
84+}
85+
86+/* TRM entries A7:4.3.31 A15:4.3.28 - RO WI */
87+static bool access_actlr(struct kvm_vcpu *vcpu,
88+ const struct coproc_params *p,
89+ const struct coproc_reg *r)
90+{
91+ if (p->is_write)
92+ return ignore_write(vcpu, p);
93+
94+ *vcpu_reg(vcpu, p->Rt1) = vcpu->arch.cp15[c1_ACTLR];
95+ return true;
96+}
97+
98+/* TRM entries A7:4.3.56, A15:4.3.60 - R/O. */
99+static bool access_cbar(struct kvm_vcpu *vcpu,
100+ const struct coproc_params *p,
101+ const struct coproc_reg *r)
102+{
103+ if (p->is_write)
104+ return write_to_read_only(vcpu, p);
105+ return read_zero(vcpu, p);
106+}
107+
108+/* TRM entries A7:4.3.49, A15:4.3.48 - R/O WI */
109+static bool access_l2ctlr(struct kvm_vcpu *vcpu,
110+ const struct coproc_params *p,
111+ const struct coproc_reg *r)
112+{
113+ if (p->is_write)
114+ return ignore_write(vcpu, p);
115+
116+ *vcpu_reg(vcpu, p->Rt1) = vcpu->arch.cp15[c9_L2CTLR];
117+ return true;
118+}
119+
120+static void reset_l2ctlr(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
121+{
122+ u32 l2ctlr, ncores;
123+
124+ asm volatile("mrc p15, 1, %0, c9, c0, 2\n" : "=r" (l2ctlr));
125+ l2ctlr &= ~(3 << 24);
126+ ncores = atomic_read(&vcpu->kvm->online_vcpus) - 1;
127+ /* How many cores in the current cluster and the next ones */
128+ ncores -= (vcpu->vcpu_id & ~3);
129+ /* Cap it to the maximum number of cores in a single cluster */
130+ ncores = min(ncores, 3U);
131+ l2ctlr |= (ncores & 3) << 24;
132+
133+ vcpu->arch.cp15[c9_L2CTLR] = l2ctlr;
134+}
135+
136+static void reset_actlr(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
137+{
138+ u32 actlr;
139+
140+ /* ACTLR contains SMP bit: make sure you create all cpus first! */
141+ asm volatile("mrc p15, 0, %0, c1, c0, 1\n" : "=r" (actlr));
142+ /* Make the SMP bit consistent with the guest configuration */
143+ if (atomic_read(&vcpu->kvm->online_vcpus) > 1)
144+ actlr |= 1U << 6;
145+ else
146+ actlr &= ~(1U << 6);
147+
148+ vcpu->arch.cp15[c1_ACTLR] = actlr;
149+}
150+
151+/*
152+ * TRM entries: A7:4.3.50, A15:4.3.49
153+ * R/O WI (even if NSACR.NS_L2ERR, a write of 1 is ignored).
154+ */
155+static bool access_l2ectlr(struct kvm_vcpu *vcpu,
156+ const struct coproc_params *p,
157+ const struct coproc_reg *r)
158+{
159+ if (p->is_write)
160+ return ignore_write(vcpu, p);
161+
162+ *vcpu_reg(vcpu, p->Rt1) = 0;
163+ return true;
164+}
165+
74166 /* See note at ARM ARM B1.14.4 */
75167 static bool access_dcsw(struct kvm_vcpu *vcpu,
76168 const struct coproc_params *p,
@@ -153,10 +245,22 @@ static bool pm_fake(struct kvm_vcpu *vcpu, · 76 unmodified lines
153245 * registers preceding 32-bit ones.
154246 */
155247 static const struct coproc_reg cp15_regs[] = {
248+ /* MPIDR: we use VMPIDR for guest access. */
249+ { CRn( 0), CRm( 0), Op1( 0), Op2( 5), is32,
250+ NULL, reset_mpidr, c0_MPIDR },
251+
156252 /* CSSELR: swapped by interrupt.S. */
157253 { CRn( 0), CRm( 0), Op1( 2), Op2( 0), is32,
158254 NULL, reset_unknown, c0_CSSELR },
159255
256+ /* ACTLR: trapped by HCR.TAC bit. */
257+ { CRn( 1), CRm( 0), Op1( 0), Op2( 1), is32,
258+ access_actlr, reset_actlr, c1_ACTLR },
259+
260+ /* CPACR: swapped by interrupt.S. */
261+ { CRn( 1), CRm( 0), Op1( 0), Op2( 2), is32,
262+ NULL, reset_val, c1_CPACR, 0x00000000 },
263+
160264 /* TTBR0/TTBR1: swapped by interrupt.S. */
161265 { CRm64( 2), Op1( 0), is64, NULL, reset_unknown64, c2_TTBR0 },
162266 { CRm64( 2), Op1( 1), is64, NULL, reset_unknown64, c2_TTBR1 },
@@ -194,6 +298,13 @@ static const struct coproc_reg cp15_regs[] = { · 31 unmodified lines
194298 { CRn( 7), CRm( 6), Op1( 0), Op2( 2), is32, access_dcsw},
195299 { CRn( 7), CRm(10), Op1( 0), Op2( 2), is32, access_dcsw},
196300 { CRn( 7), CRm(14), Op1( 0), Op2( 2), is32, access_dcsw},
301+ /*
302+ * L2CTLR access (guest wants to know #CPUs).
303+ */
304+ { CRn( 9), CRm( 0), Op1( 1), Op2( 2), is32,
305+ access_l2ctlr, reset_l2ctlr, c9_L2CTLR },
306+ { CRn( 9), CRm( 0), Op1( 1), Op2( 3), is32, access_l2ectlr},
307+
197308 /*
198309 * Dummy performance monitor implementation.
199310 */
@@ -234,6 +345,9 @@ static const struct coproc_reg cp15_regs[] = { · 34 unmodified lines
234345 /* CNTKCTL: swapped by interrupt.S. */
235346 { CRn(14), CRm( 1), Op1( 0), Op2( 0), is32,
236347 NULL, reset_val, c14_CNTKCTL, 0x00000000 },
348+
349+ /* The Configuration Base Address Register. */
350+ { CRn(15), CRm( 0), Op1( 4), Op2( 0), is32, access_cbar},
237351 };
238352
239353 /* Target specific emulation tables */
@@ -241,6 +355,12 @@ static struct kvm_coproc_target_table *target_tables[KVM_ARM_NUM_TARGETS]; · 1 unmodified line
241355
242356 void kvm_register_target_coproc_table(struct kvm_coproc_target_table *table)
243357 {
358+ unsigned int i;
359+
360+ for (i = 1; i < table->num; i++)
361+ BUG_ON(cmp_reg(&table->table[i-1],
362+ &table->table[i]) >= 0);
363+
244364 target_tables[table->target] = table;
245365 }
246366
arch/arm/kvm/coproc_a15.c+1−116
@@ -17,101 +17,12 @@ · 16 unmodified lines
1717 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1818 */
1919 #include <linux/kvm_host.h>
20−#include <asm/cputype.h>
21−#include <asm/kvm_arm.h>
22−#include <asm/kvm_host.h>
23−#include <asm/kvm_emulate.h>
2420 #include <asm/kvm_coproc.h>
21+#include <asm/kvm_emulate.h>
2522 #include <linux/init.h>
2623
27−static void reset_mpidr(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
28−{
29− /*
30− * Compute guest MPIDR:
31− * (Even if we present only one VCPU to the guest on an SMP
32− * host we don't set the U bit in the MPIDR, or vice versa, as
33− * revealing the underlying hardware properties is likely to
34− * be the best choice).
35− */
36− vcpu->arch.cp15[c0_MPIDR] = (read_cpuid_mpidr() & ~MPIDR_LEVEL_MASK)
37− | (vcpu->vcpu_id & MPIDR_LEVEL_MASK);
38−}
39−
4024 #include "coproc.h"
4125
42−/* A15 TRM 4.3.28: RO WI */
43−static bool access_actlr(struct kvm_vcpu *vcpu,
44− const struct coproc_params *p,
45− const struct coproc_reg *r)
46−{
47− if (p->is_write)
48− return ignore_write(vcpu, p);
49−
50− *vcpu_reg(vcpu, p->Rt1) = vcpu->arch.cp15[c1_ACTLR];
51− return true;
52−}
53−
54−/* A15 TRM 4.3.60: R/O. */
55−static bool access_cbar(struct kvm_vcpu *vcpu,
56− const struct coproc_params *p,
57− const struct coproc_reg *r)
58−{
59− if (p->is_write)
60− return write_to_read_only(vcpu, p);
61− return read_zero(vcpu, p);
62−}
63−
64−/* A15 TRM 4.3.48: R/O WI. */
65−static bool access_l2ctlr(struct kvm_vcpu *vcpu,
66− const struct coproc_params *p,
67− const struct coproc_reg *r)
68−{
69− if (p->is_write)
70− return ignore_write(vcpu, p);
71−
72− *vcpu_reg(vcpu, p->Rt1) = vcpu->arch.cp15[c9_L2CTLR];
73− return true;
74−}
75−
76−static void reset_l2ctlr(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
77−{
78− u32 l2ctlr, ncores;
79−
80− asm volatile("mrc p15, 1, %0, c9, c0, 2\n" : "=r" (l2ctlr));
81− l2ctlr &= ~(3 << 24);
82− ncores = atomic_read(&vcpu->kvm->online_vcpus) - 1;
83− l2ctlr |= (ncores & 3) << 24;
84−
85− vcpu->arch.cp15[c9_L2CTLR] = l2ctlr;
86−}
87−
88−static void reset_actlr(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
89−{
90− u32 actlr;
91−
92− /* ACTLR contains SMP bit: make sure you create all cpus first! */
93− asm volatile("mrc p15, 0, %0, c1, c0, 1\n" : "=r" (actlr));
94− /* Make the SMP bit consistent with the guest configuration */
95− if (atomic_read(&vcpu->kvm->online_vcpus) > 1)
96− actlr |= 1U << 6;
97− else
98− actlr &= ~(1U << 6);
99−
100− vcpu->arch.cp15[c1_ACTLR] = actlr;
101−}
102−
103−/* A15 TRM 4.3.49: R/O WI (even if NSACR.NS_L2ERR, a write of 1 is ignored). */
104−static bool access_l2ectlr(struct kvm_vcpu *vcpu,
105− const struct coproc_params *p,
106− const struct coproc_reg *r)
107−{
108− if (p->is_write)
109− return ignore_write(vcpu, p);
110−
111− *vcpu_reg(vcpu, p->Rt1) = 0;
112− return true;
113−}
114−
11526 /*
11627 * A15-specific CP15 registers.
11728 * CRn denotes the primary register number, but is copied to the CRm in the
@@ -121,29 +32,9 @@ static bool access_l2ectlr(struct kvm_vcpu *vcpu, · 3 unmodified lines
12132 * registers preceding 32-bit ones.
12233 */
12334 static const struct coproc_reg a15_regs[] = {
124− /* MPIDR: we use VMPIDR for guest access. */
125− { CRn( 0), CRm( 0), Op1( 0), Op2( 5), is32,
126− NULL, reset_mpidr, c0_MPIDR },
127−
12835 /* SCTLR: swapped by interrupt.S. */
12936 { CRn( 1), CRm( 0), Op1( 0), Op2( 0), is32,
13037 NULL, reset_val, c1_SCTLR, 0x00C50078 },
131− /* ACTLR: trapped by HCR.TAC bit. */
132− { CRn( 1), CRm( 0), Op1( 0), Op2( 1), is32,
133− access_actlr, reset_actlr, c1_ACTLR },
134− /* CPACR: swapped by interrupt.S. */
135− { CRn( 1), CRm( 0), Op1( 0), Op2( 2), is32,
136− NULL, reset_val, c1_CPACR, 0x00000000 },
137−
138− /*
139− * L2CTLR access (guest wants to know #CPUs).
140− */
141− { CRn( 9), CRm( 0), Op1( 1), Op2( 2), is32,
142− access_l2ctlr, reset_l2ctlr, c9_L2CTLR },
143− { CRn( 9), CRm( 0), Op1( 1), Op2( 3), is32, access_l2ectlr},
144−
145− /* The Configuration Base Address Register. */
146− { CRn(15), CRm( 0), Op1( 4), Op2( 0), is32, access_cbar},
14738 };
14839
14940 static struct kvm_coproc_target_table a15_target_table = {
@@ -154,12 +45,6 @@ static struct kvm_coproc_target_table a15_target_table = { · 4 unmodified lines
15445
15546 static int __init coproc_a15_init(void)
15647 {
157− unsigned int i;
158−
159− for (i = 1; i < ARRAY_SIZE(a15_regs); i++)
160− BUG_ON(cmp_reg(&a15_regs[i-1],
161− &a15_regs[i]) >= 0);
162−
16348 kvm_register_target_coproc_table(&a15_target_table);
16449 return 0;
16550 }
arch/arm/kvm/coproc_a7.c+54−0
@@ -0,0 +1,54 @@
1+/*
2+ * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3+ * Copyright (C) 2013 - ARM Ltd
4+ *
5+ * Authors: Rusty Russell <rusty@rustcorp.au>
6+ * Christoffer Dall <c.dall@virtualopensystems.com>
7+ * Jonathan Austin <jonathan.austin@arm.com>
8+ *
9+ * This program is free software; you can redistribute it and/or modify
10+ * it under the terms of the GNU General Public License, version 2, as
11+ * published by the Free Software Foundation.
12+ *
13+ * This program is distributed in the hope that it will be useful,
14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+ * GNU General Public License for more details.
17+ *
18+ * You should have received a copy of the GNU General Public License
19+ * along with this program; if not, write to the Free Software
20+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21+ */
22+#include <linux/kvm_host.h>
23+#include <asm/kvm_coproc.h>
24+#include <asm/kvm_emulate.h>
25+#include <linux/init.h>
26+
27+#include "coproc.h"
28+
29+/*
30+ * Cortex-A7 specific CP15 registers.
31+ * CRn denotes the primary register number, but is copied to the CRm in the
32+ * user space API for 64-bit register access in line with the terminology used
33+ * in the ARM ARM.
34+ * Important: Must be sorted ascending by CRn, CRM, Op1, Op2 and with 64-bit
35+ * registers preceding 32-bit ones.
36+ */
37+static const struct coproc_reg a7_regs[] = {
38+ /* SCTLR: swapped by interrupt.S. */
39+ { CRn( 1), CRm( 0), Op1( 0), Op2( 0), is32,
40+ NULL, reset_val, c1_SCTLR, 0x00C50878 },
41+};
42+
43+static struct kvm_coproc_target_table a7_target_table = {
44+ .target = KVM_ARM_TARGET_CORTEX_A7,
45+ .table = a7_regs,
46+ .num = ARRAY_SIZE(a7_regs),
47+};
48+
49+static int __init coproc_a7_init(void)
50+{
51+ kvm_register_target_coproc_table(&a7_target_table);
52+ return 0;
53+}
54+late_initcall(coproc_a7_init);
arch/arm/kvm/emulate.c+1−1
@@ -354,7 +354,7 @@ static void inject_abt(struct kvm_vcpu *vcpu, bool is_pabt, unsigned long addr) · 353 unmodified lines
354354 *vcpu_pc(vcpu) = exc_vector_base(vcpu) + vect_offset;
355355
356356 if (is_pabt) {
357− /* Set DFAR and DFSR */
357+ /* Set IFAR and IFSR */
358358 vcpu->arch.cp15[c6_IFAR] = addr;
359359 is_lpae = (vcpu->arch.cp15[c2_TTBCR] >> 31);
360360 /* Always give debug fault for now - should give guest a clue */
arch/arm/kvm/guest.c+23−1
@@ -190,6 +190,8 @@ int __attribute_const__ kvm_target_cpu(void) · 189 unmodified lines
190190 return -EINVAL;
191191
192192 switch (part_number) {
193+ case ARM_CPU_PART_CORTEX_A7:
194+ return KVM_ARM_TARGET_CORTEX_A7;
193195 case ARM_CPU_PART_CORTEX_A15:
194196 return KVM_ARM_TARGET_CORTEX_A15;
195197 default:
@@ -202,7 +204,7 @@ int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, · 6 unmodified lines
202204 {
203205 unsigned int i;
204206
205− /* We can only do a cortex A15 for now. */
207+ /* We can only cope with guest==host and only on A15/A7 (for now). */
206208 if (init->target != kvm_target_cpu())
207209 return -EINVAL;
208210
@@ -222,6 +224,26 @@ int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, · 13 unmodified lines
222224 return kvm_reset_vcpu(vcpu);
223225 }
224226
227+int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
228+{
229+ int target = kvm_target_cpu();
230+
231+ if (target < 0)
232+ return -ENODEV;
233+
234+ memset(init, 0, sizeof(*init));
235+
236+ /*
237+ * For now, we don't return any features.
238+ * In future, we might use features to return target
239+ * specific features available for the preferred
240+ * target type.
241+ */
242+ init->target = (__u32)target;
243+
244+ return 0;
245+}
246+
225247 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
226248 {
227249 return -EINVAL;
arch/arm/kvm/handle_exit.c+13−7
@@ -73,23 +73,29 @@ static int handle_dabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run) · 72 unmodified lines
7373 }
7474
7575 /**
76− * kvm_handle_wfi - handle a wait-for-interrupts instruction executed by a guest
76+ * kvm_handle_wfx - handle a WFI or WFE instructions trapped in guests
7777 * @vcpu: the vcpu pointer
7878 * @run: the kvm_run structure pointer
7979 *
80− * Simply sets the wait_for_interrupts flag on the vcpu structure, which will
81− * halt execution of world-switches and schedule other host processes until
82− * there is an incoming IRQ or FIQ to the VM.
80+ * WFE: Yield the CPU and come back to this vcpu when the scheduler
81+ * decides to.
82+ * WFI: Simply call kvm_vcpu_block(), which will halt execution of
83+ * world-switches and schedule other host processes until there is an
84+ * incoming IRQ or FIQ to the VM.
8385 */
84−static int kvm_handle_wfi(struct kvm_vcpu *vcpu, struct kvm_run *run)
86+static int kvm_handle_wfx(struct kvm_vcpu *vcpu, struct kvm_run *run)
8587 {
8688 trace_kvm_wfi(*vcpu_pc(vcpu));
87− kvm_vcpu_block(vcpu);
89+ if (kvm_vcpu_get_hsr(vcpu) & HSR_WFI_IS_WFE)
90+ kvm_vcpu_on_spin(vcpu);
91+ else
92+ kvm_vcpu_block(vcpu);
93+
8894 return 1;
8995 }
9096
9197 static exit_handle_fn arm_exit_handlers[] = {
92− [HSR_EC_WFI] = kvm_handle_wfi,
98+ [HSR_EC_WFI] = kvm_handle_wfx,
9399 [HSR_EC_CP15_32] = kvm_handle_cp15_32,
94100 [HSR_EC_CP15_64] = kvm_handle_cp15_64,
95101 [HSR_EC_CP14_MR] = kvm_handle_cp14_access,
arch/arm/kvm/mmio.c+75−11
@@ -23,6 +23,68 @@ · 22 unmodified lines
2323
2424 #include "trace.h"
2525
26+static void mmio_write_buf(char *buf, unsigned int len, unsigned long data)
27+{
28+ void *datap = NULL;
29+ union {
30+ u8 byte;
31+ u16 hword;
32+ u32 word;
33+ u64 dword;
34+ } tmp;
35+
36+ switch (len) {
37+ case 1:
38+ tmp.byte = data;
39+ datap = &tmp.byte;
40+ break;
41+ case 2:
42+ tmp.hword = data;
43+ datap = &tmp.hword;
44+ break;
45+ case 4:
46+ tmp.word = data;
47+ datap = &tmp.word;
48+ break;
49+ case 8:
50+ tmp.dword = data;
51+ datap = &tmp.dword;
52+ break;
53+ }
54+
55+ memcpy(buf, datap, len);
56+}
57+
58+static unsigned long mmio_read_buf(char *buf, unsigned int len)
59+{
60+ unsigned long data = 0;
61+ union {
62+ u16 hword;
63+ u32 word;
64+ u64 dword;
65+ } tmp;
66+
67+ switch (len) {
68+ case 1:
69+ data = buf[0];
70+ break;
71+ case 2:
72+ memcpy(&tmp.hword, buf, len);
73+ data = tmp.hword;
74+ break;
75+ case 4:
76+ memcpy(&tmp.word, buf, len);
77+ data = tmp.word;
78+ break;
79+ case 8:
80+ memcpy(&tmp.dword, buf, len);
81+ data = tmp.dword;
82+ break;
83+ }
84+
85+ return data;
86+}
87+
2688 /**
2789 * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation
2890 * @vcpu: The VCPU pointer
@@ -33,28 +95,27 @@ · 4 unmodified lines
3395 */
3496 int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
3597 {
36− unsigned long *dest;
98+ unsigned long data;
3799 unsigned int len;
38100 int mask;
39101
40102 if (!run->mmio.is_write) {
41− dest = vcpu_reg(vcpu, vcpu->arch.mmio_decode.rt);
42− *dest = 0;
43−
44103 len = run->mmio.len;
45104 if (len > sizeof(unsigned long))
46105 return -EINVAL;
47106
48− memcpy(dest, run->mmio.data, len);
49−
50− trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
51− *((u64 *)run->mmio.data));
107+ data = mmio_read_buf(run->mmio.data, len);
52108
53109 if (vcpu->arch.mmio_decode.sign_extend &&
54110 len < sizeof(unsigned long)) {
55111 mask = 1U << ((len * 8) - 1);
56− *dest = (*dest ^ mask) - mask;
112+ data = (data ^ mask) - mask;
57113 }
114+
115+ trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
116+ data);
117+ data = vcpu_data_host_to_guest(vcpu, data, len);
118+ *vcpu_reg(vcpu, vcpu->arch.mmio_decode.rt) = data;
58119 }
59120
60121 return 0;
@@ -105,6 +166,7 @@ int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run, · 44 unmodified lines
105166 phys_addr_t fault_ipa)
106167 {
107168 struct kvm_exit_mmio mmio;
169+ unsigned long data;
108170 unsigned long rt;
109171 int ret;
110172
@@ -125,13 +187,15 @@ int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run, · 14 unmodified lines
125187 }
126188
127189 rt = vcpu->arch.mmio_decode.rt;
190+ data = vcpu_data_guest_to_host(vcpu, *vcpu_reg(vcpu, rt), mmio.len);
191+
128192 trace_kvm_mmio((mmio.is_write) ? KVM_TRACE_MMIO_WRITE :
129193 KVM_TRACE_MMIO_READ_UNSATISFIED,
130194 mmio.len, fault_ipa,
131− (mmio.is_write) ? *vcpu_reg(vcpu, rt) : 0);
195+ (mmio.is_write) ? data : 0);
132196
133197 if (mmio.is_write)
134− memcpy(mmio.data, vcpu_reg(vcpu, rt), mmio.len);
198+ mmio_write_buf(mmio.data, mmio.len, data);
135199
136200 if (vgic_handle_mmio(vcpu, run, &mmio))
137201 return 1;
arch/arm/kvm/mmu.c+185−38
@@ -19,6 +19,7 @@ · 18 unmodified lines
1919 #include <linux/mman.h>
2020 #include <linux/kvm_host.h>
2121 #include <linux/io.h>
22+#include <linux/hugetlb.h>
2223 #include <trace/events/kvm.h>
2324 #include <asm/pgalloc.h>
2425 #include <asm/cacheflush.h>
@@ -41,6 +42,8 @@ static unsigned long hyp_idmap_start; · 16 unmodified lines
4142 static unsigned long hyp_idmap_end;
4243 static phys_addr_t hyp_idmap_vector;
4344
45+#define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
46+
4447 static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
4548 {
4649 /*
@@ -93,19 +96,29 @@ static bool page_empty(void *ptr) · 46 unmodified lines
9396
9497 static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
9598 {
96− pmd_t *pmd_table = pmd_offset(pud, 0);
97− pud_clear(pud);
98− kvm_tlb_flush_vmid_ipa(kvm, addr);
99− pmd_free(NULL, pmd_table);
99+ if (pud_huge(*pud)) {
100+ pud_clear(pud);
101+ kvm_tlb_flush_vmid_ipa(kvm, addr);
102+ } else {
103+ pmd_t *pmd_table = pmd_offset(pud, 0);
104+ pud_clear(pud);
105+ kvm_tlb_flush_vmid_ipa(kvm, addr);
106+ pmd_free(NULL, pmd_table);
107+ }
100108 put_page(virt_to_page(pud));
101109 }
102110
103111 static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
104112 {
105− pte_t *pte_table = pte_offset_kernel(pmd, 0);
106− pmd_clear(pmd);
107− kvm_tlb_flush_vmid_ipa(kvm, addr);
108− pte_free_kernel(NULL, pte_table);
113+ if (kvm_pmd_huge(*pmd)) {
114+ pmd_clear(pmd);
115+ kvm_tlb_flush_vmid_ipa(kvm, addr);
116+ } else {
117+ pte_t *pte_table = pte_offset_kernel(pmd, 0);
118+ pmd_clear(pmd);
119+ kvm_tlb_flush_vmid_ipa(kvm, addr);
120+ pte_free_kernel(NULL, pte_table);
121+ }
109122 put_page(virt_to_page(pmd));
110123 }
111124
@@ -136,18 +149,32 @@ static void unmap_range(struct kvm *kvm, pgd_t *pgdp, · 24 unmodified lines
136149 continue;
137150 }
138151
152+ if (pud_huge(*pud)) {
153+ /*
154+ * If we are dealing with a huge pud, just clear it and
155+ * move on.
156+ */
157+ clear_pud_entry(kvm, pud, addr);
158+ addr = pud_addr_end(addr, end);
159+ continue;
160+ }
161+
139162 pmd = pmd_offset(pud, addr);
140163 if (pmd_none(*pmd)) {
141164 addr = pmd_addr_end(addr, end);
142165 continue;
143166 }
144167
145− pte = pte_offset_kernel(pmd, addr);
146− clear_pte_entry(kvm, pte, addr);
147− next = addr + PAGE_SIZE;
168+ if (!kvm_pmd_huge(*pmd)) {
169+ pte = pte_offset_kernel(pmd, addr);
170+ clear_pte_entry(kvm, pte, addr);
171+ next = addr + PAGE_SIZE;
172+ }
148173
149− /* If we emptied the pte, walk back up the ladder */
150− if (page_empty(pte)) {
174+ /*
175+ * If the pmd entry is to be cleared, walk back up the ladder
176+ */
177+ if (kvm_pmd_huge(*pmd) || page_empty(pte)) {
151178 clear_pmd_entry(kvm, pmd, addr);
152179 next = pmd_addr_end(addr, end);
153180 if (page_empty(pmd) && !page_empty(pud)) {
@@ -420,29 +447,71 @@ void kvm_free_stage2_pgd(struct kvm *kvm) · 266 unmodified lines
420447 kvm->arch.pgd = NULL;
421448 }
422449
423−
424−static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
425− phys_addr_t addr, const pte_t *new_pte, bool iomap)
450+static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
451+ phys_addr_t addr)
426452 {
427453 pgd_t *pgd;
428454 pud_t *pud;
429455 pmd_t *pmd;
430− pte_t *pte, old_pte;
431456
432− /* Create 2nd stage page table mapping - Level 1 */
433457 pgd = kvm->arch.pgd + pgd_index(addr);
434458 pud = pud_offset(pgd, addr);
435459 if (pud_none(*pud)) {
436460 if (!cache)
437− return 0; /* ignore calls from kvm_set_spte_hva */
461+ return NULL;
438462 pmd = mmu_memory_cache_alloc(cache);
439463 pud_populate(NULL, pud, pmd);
440464 get_page(virt_to_page(pud));
441465 }
442466
443− pmd = pmd_offset(pud, addr);
467+ return pmd_offset(pud, addr);
468+}
469+
470+static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
471+ *cache, phys_addr_t addr, const pmd_t *new_pmd)
472+{
473+ pmd_t *pmd, old_pmd;
474+
475+ pmd = stage2_get_pmd(kvm, cache, addr);
476+ VM_BUG_ON(!pmd);
477+
478+ /*
479+ * Mapping in huge pages should only happen through a fault. If a
480+ * page is merged into a transparent huge page, the individual
481+ * subpages of that huge page should be unmapped through MMU
482+ * notifiers before we get here.
483+ *
484+ * Merging of CompoundPages is not supported; they should become
485+ * splitting first, unmapped, merged, and mapped back in on-demand.
486+ */
487+ VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
488+
489+ old_pmd = *pmd;
490+ kvm_set_pmd(pmd, *new_pmd);
491+ if (pmd_present(old_pmd))
492+ kvm_tlb_flush_vmid_ipa(kvm, addr);
493+ else
494+ get_page(virt_to_page(pmd));
495+ return 0;
496+}
497+
498+static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
499+ phys_addr_t addr, const pte_t *new_pte, bool iomap)
500+{
501+ pmd_t *pmd;
502+ pte_t *pte, old_pte;
444503
445− /* Create 2nd stage page table mapping - Level 2 */
504+ /* Create stage-2 page table mapping - Level 1 */
505+ pmd = stage2_get_pmd(kvm, cache, addr);
506+ if (!pmd) {
507+ /*
508+ * Ignore calls from kvm_set_spte_hva for unallocated
509+ * address ranges.
510+ */
511+ return 0;
512+ }
513+
514+ /* Create stage-2 page mappings - Level 2 */
446515 if (pmd_none(*pmd)) {
447516 if (!cache)
448517 return 0; /* ignore calls from kvm_set_spte_hva */
@@ -507,16 +576,60 @@ out: · 58 unmodified lines
507576 return ret;
508577 }
509578
579+static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
580+{
581+ pfn_t pfn = *pfnp;
582+ gfn_t gfn = *ipap >> PAGE_SHIFT;
583+
584+ if (PageTransCompound(pfn_to_page(pfn))) {
585+ unsigned long mask;
586+ /*
587+ * The address we faulted on is backed by a transparent huge
588+ * page. However, because we map the compound huge page and
589+ * not the individual tail page, we need to transfer the
590+ * refcount to the head page. We have to be careful that the
591+ * THP doesn't start to split while we are adjusting the
592+ * refcounts.
593+ *
594+ * We are sure this doesn't happen, because mmu_notifier_retry
595+ * was successful and we are holding the mmu_lock, so if this
596+ * THP is trying to split, it will be blocked in the mmu
597+ * notifier before touching any of the pages, specifically
598+ * before being able to call __split_huge_page_refcount().
599+ *
600+ * We can therefore safely transfer the refcount from PG_tail
601+ * to PG_head and switch the pfn from a tail page to the head
602+ * page accordingly.
603+ */
604+ mask = PTRS_PER_PMD - 1;
605+ VM_BUG_ON((gfn & mask) != (pfn & mask));
606+ if (pfn & mask) {
607+ *ipap &= PMD_MASK;
608+ kvm_release_pfn_clean(pfn);
609+ pfn &= ~mask;
610+ kvm_get_pfn(pfn);
611+ *pfnp = pfn;
612+ }
613+
614+ return true;
615+ }
616+
617+ return false;
618+}
619+
510620 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
511− gfn_t gfn, struct kvm_memory_slot *memslot,
621+ struct kvm_memory_slot *memslot,
512622 unsigned long fault_status)
513623 {
514− pte_t new_pte;
515− pfn_t pfn;
516624 int ret;
517− bool write_fault, writable;
625+ bool write_fault, writable, hugetlb = false, force_pte = false;
518626 unsigned long mmu_seq;
627+ gfn_t gfn = fault_ipa >> PAGE_SHIFT;
628+ unsigned long hva = gfn_to_hva(vcpu->kvm, gfn);
629+ struct kvm *kvm = vcpu->kvm;
519630 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
631+ struct vm_area_struct *vma;
632+ pfn_t pfn;
520633
521634 write_fault = kvm_is_write_fault(kvm_vcpu_get_hsr(vcpu));
522635 if (fault_status == FSC_PERM && !write_fault) {
@@ -524,6 +637,26 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · 1 unmodified line
524637 return -EFAULT;
525638 }
526639
640+ /* Let's check if we will get back a huge page backed by hugetlbfs */
641+ down_read(&current->mm->mmap_sem);
642+ vma = find_vma_intersection(current->mm, hva, hva + 1);
643+ if (is_vm_hugetlb_page(vma)) {
644+ hugetlb = true;
645+ gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
646+ } else {
647+ /*
648+ * Pages belonging to VMAs not aligned to the PMD mapping
649+ * granularity cannot be mapped using block descriptors even
650+ * if the pages belong to a THP for the process, because the
651+ * stage-2 block descriptor will cover more than a single THP
652+ * and we loose atomicity for unmapping, updates, and splits
653+ * of the THP or other pages in the stage-2 block range.
654+ */
655+ if (vma->vm_start & ~PMD_MASK)
656+ force_pte = true;
657+ }
658+ up_read(&current->mm->mmap_sem);
659+
527660 /* We need minimum second+third level pages */
528661 ret = mmu_topup_memory_cache(memcache, 2, KVM_NR_MEM_OBJS);
529662 if (ret)
@@ -541,26 +674,40 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · 11 unmodified lines
541674 */
542675 smp_rmb();
543676
544− pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write_fault, &writable);
677+ pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
545678 if (is_error_pfn(pfn))
546679 return -EFAULT;
547680
548− new_pte = pfn_pte(pfn, PAGE_S2);
549− coherent_icache_guest_page(vcpu->kvm, gfn);
550−
551− spin_lock(&vcpu->kvm->mmu_lock);
552− if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
681+ spin_lock(&kvm->mmu_lock);
682+ if (mmu_notifier_retry(kvm, mmu_seq))
553683 goto out_unlock;
554− if (writable) {
555− kvm_set_s2pte_writable(&new_pte);
556− kvm_set_pfn_dirty(pfn);
684+ if (!hugetlb && !force_pte)
685+ hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
686+
687+ if (hugetlb) {
688+ pmd_t new_pmd = pfn_pmd(pfn, PAGE_S2);
689+ new_pmd = pmd_mkhuge(new_pmd);
690+ if (writable) {
691+ kvm_set_s2pmd_writable(&new_pmd);
692+ kvm_set_pfn_dirty(pfn);
693+ }
694+ coherent_icache_guest_page(kvm, hva & PMD_MASK, PMD_SIZE);
695+ ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
696+ } else {
697+ pte_t new_pte = pfn_pte(pfn, PAGE_S2);
698+ if (writable) {
699+ kvm_set_s2pte_writable(&new_pte);
700+ kvm_set_pfn_dirty(pfn);
701+ }
702+ coherent_icache_guest_page(kvm, hva, PAGE_SIZE);
703+ ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, false);
557704 }
558− stage2_set_pte(vcpu->kvm, memcache, fault_ipa, &new_pte, false);
705+
559706
560707 out_unlock:
561− spin_unlock(&vcpu->kvm->mmu_lock);
708+ spin_unlock(&kvm->mmu_lock);
562709 kvm_release_pfn_clean(pfn);
563− return 0;
710+ return ret;
564711 }
565712
566713 /**
@@ -629,7 +776,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run) · 62 unmodified lines
629776
630777 memslot = gfn_to_memslot(vcpu->kvm, gfn);
631778
632− ret = user_mem_abort(vcpu, fault_ipa, gfn, memslot, fault_status);
779+ ret = user_mem_abort(vcpu, fault_ipa, memslot, fault_status);
633780 if (ret == 0)
634781 ret = 1;
635782 out_unlock:
arch/arm/kvm/psci.c+17−4
@@ -18,6 +18,7 @@ · 17 unmodified lines
1818 #include <linux/kvm_host.h>
1919 #include <linux/wait.h>
2020
21+#include <asm/cputype.h>
2122 #include <asm/kvm_emulate.h>
2223 #include <asm/kvm_psci.h>
2324
@@ -34,22 +35,30 @@ static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu) · 10 unmodified lines
3435 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
3536 {
3637 struct kvm *kvm = source_vcpu->kvm;
37− struct kvm_vcpu *vcpu;
38+ struct kvm_vcpu *vcpu = NULL, *tmp;
3839 wait_queue_head_t *wq;
3940 unsigned long cpu_id;
41+ unsigned long mpidr;
4042 phys_addr_t target_pc;
43+ int i;
4144
4245 cpu_id = *vcpu_reg(source_vcpu, 1);
4346 if (vcpu_mode_is_32bit(source_vcpu))
4447 cpu_id &= ~((u32) 0);
4548
46− if (cpu_id >= atomic_read(&kvm->online_vcpus))
49+ kvm_for_each_vcpu(i, tmp, kvm) {
50+ mpidr = kvm_vcpu_get_mpidr(tmp);
51+ if ((mpidr & MPIDR_HWID_BITMASK) == (cpu_id & MPIDR_HWID_BITMASK)) {
52+ vcpu = tmp;
53+ break;
54+ }
55+ }
56+
57+ if (!vcpu)
4758 return KVM_PSCI_RET_INVAL;
4859
4960 target_pc = *vcpu_reg(source_vcpu, 2);
5061
51− vcpu = kvm_get_vcpu(kvm, cpu_id);
52−
5362 wq = kvm_arch_vcpu_wq(vcpu);
5463 if (!waitqueue_active(wq))
5564 return KVM_PSCI_RET_INVAL;
@@ -62,6 +71,10 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) · 6 unmodified lines
6271 vcpu_set_thumb(vcpu);
6372 }
6473
74+ /* Propagate caller endianness */
75+ if (kvm_vcpu_is_be(source_vcpu))
76+ kvm_vcpu_set_be(vcpu);
77+
6578 *vcpu_pc(vcpu) = target_pc;
6679 vcpu->arch.pause = false;
6780 smp_mb(); /* Make sure the above is visible */
arch/arm/kvm/reset.c+6−9
@@ -30,16 +30,14 @@ · 29 unmodified lines
3030 #include <kvm/arm_arch_timer.h>
3131
3232 /******************************************************************************
33− * Cortex-A15 Reset Values
33+ * Cortex-A15 and Cortex-A7 Reset Values
3434 */
3535
36−static const int a15_max_cpu_idx = 3;
37−
38−static struct kvm_regs a15_regs_reset = {
36+static struct kvm_regs cortexa_regs_reset = {
3937 .usr_regs.ARM_cpsr = SVC_MODE | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT,
4038 };
4139
42−static const struct kvm_irq_level a15_vtimer_irq = {
40+static const struct kvm_irq_level cortexa_vtimer_irq = {
4341 { .irq = 27 },
4442 .level = 1,
4543 };
@@ -62,12 +60,11 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) · 16 unmodified lines
6260 const struct kvm_irq_level *cpu_vtimer_irq;
6361
6462 switch (vcpu->arch.target) {
63+ case KVM_ARM_TARGET_CORTEX_A7:
6564 case KVM_ARM_TARGET_CORTEX_A15:
66− if (vcpu->vcpu_id > a15_max_cpu_idx)
67− return -EINVAL;
68− reset_regs = &a15_regs_reset;
65+ reset_regs = &cortexa_regs_reset;
6966 vcpu->arch.midr = read_cpuid_id();
70− cpu_vtimer_irq = &a15_vtimer_irq;
67+ cpu_vtimer_irq = &cortexa_vtimer_irq;
7168 break;
7269 default:
7370 return -ENODEV;
arch/arm64/include/asm/kvm_arm.h+6−2
@@ -63,6 +63,7 @@ · 62 unmodified lines
6363 * TAC: Trap ACTLR
6464 * TSC: Trap SMC
6565 * TSW: Trap cache operations by set/way
66+ * TWE: Trap WFE
6667 * TWI: Trap WFI
6768 * TIDCP: Trap L2CTLR/L2ECTLR
6869 * BSU_IS: Upgrade barriers to the inner shareable domain
@@ -72,8 +73,9 @@ · 3 unmodified lines
7273 * FMO: Override CPSR.F and enable signaling with VF
7374 * SWIO: Turn set/way invalidates into set/way clean+invalidate
7475 */
75−#define HCR_GUEST_FLAGS (HCR_TSC | HCR_TSW | HCR_TWI | HCR_VM | HCR_BSU_IS | \
76− HCR_FB | HCR_TAC | HCR_AMO | HCR_IMO | HCR_FMO | \
76+#define HCR_GUEST_FLAGS (HCR_TSC | HCR_TSW | HCR_TWE | HCR_TWI | HCR_VM | \
77+ HCR_BSU_IS | HCR_FB | HCR_TAC | \
78+ HCR_AMO | HCR_IMO | HCR_FMO | \
7779 HCR_SWIO | HCR_TIDCP | HCR_RW)
7880 #define HCR_VIRT_EXCP_MASK (HCR_VA | HCR_VI | HCR_VF)
7981
@@ -242,4 +244,6 @@ · 162 unmodified lines
242244
243245 #define ESR_EL2_EC_xABT_xFSR_EXTABT 0x10
244246
247+#define ESR_EL2_EC_WFI_ISS_WFE (1 << 0)
248+
245249 #endif /* __ARM64_KVM_ARM_H__ */
arch/arm64/include/asm/kvm_emulate.h+61−0
@@ -177,4 +177,65 @@ static inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu) · 176 unmodified lines
177177 return kvm_vcpu_get_hsr(vcpu) & ESR_EL2_FSC_TYPE;
178178 }
179179
180+static inline unsigned long kvm_vcpu_get_mpidr(struct kvm_vcpu *vcpu)
181+{
182+ return vcpu_sys_reg(vcpu, MPIDR_EL1);
183+}
184+
185+static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
186+{
187+ if (vcpu_mode_is_32bit(vcpu))
188+ *vcpu_cpsr(vcpu) |= COMPAT_PSR_E_BIT;
189+ else
190+ vcpu_sys_reg(vcpu, SCTLR_EL1) |= (1 << 25);
191+}
192+
193+static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
194+{
195+ if (vcpu_mode_is_32bit(vcpu))
196+ return !!(*vcpu_cpsr(vcpu) & COMPAT_PSR_E_BIT);
197+
198+ return !!(vcpu_sys_reg(vcpu, SCTLR_EL1) & (1 << 25));
199+}
200+
201+static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
202+ unsigned long data,
203+ unsigned int len)
204+{
205+ if (kvm_vcpu_is_be(vcpu)) {
206+ switch (len) {
207+ case 1:
208+ return data & 0xff;
209+ case 2:
210+ return be16_to_cpu(data & 0xffff);
211+ case 4:
212+ return be32_to_cpu(data & 0xffffffff);
213+ default:
214+ return be64_to_cpu(data);
215+ }
216+ }
217+
218+ return data; /* Leave LE untouched */
219+}
220+
221+static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
222+ unsigned long data,
223+ unsigned int len)
224+{
225+ if (kvm_vcpu_is_be(vcpu)) {
226+ switch (len) {
227+ case 1:
228+ return data & 0xff;
229+ case 2:
230+ return cpu_to_be16(data & 0xffff);
231+ case 4:
232+ return cpu_to_be32(data & 0xffffffff);
233+ default:
234+ return cpu_to_be64(data);
235+ }
236+ }
237+
238+ return data; /* Leave LE untouched */
239+}
240+
180241 #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h+1−5
@@ -36,11 +36,6 @@ · 35 unmodified lines
3636
3737 #define KVM_VCPU_MAX_FEATURES 2
3838
39−/* We don't currently support large pages. */
40−#define KVM_HPAGE_GFN_SHIFT(x) 0
41−#define KVM_NR_PAGE_SIZES 1
42−#define KVM_PAGES_PER_HPAGE(x) (1UL<<31)
43−
4439 struct kvm_vcpu;
4540 int kvm_target_cpu(void);
4641 int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
@@ -151,6 +146,7 @@ struct kvm_vcpu_stat { · 104 unmodified lines
151146 struct kvm_vcpu_init;
152147 int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
153148 const struct kvm_vcpu_init *init);
149+int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
154150 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
155151 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
156152 struct kvm_one_reg;
arch/arm64/include/asm/kvm_mmu.h+9−3
@@ -91,6 +91,7 @@ int kvm_mmu_init(void); · 90 unmodified lines
9191 void kvm_clear_hyp_idmap(void);
9292
9393 #define kvm_set_pte(ptep, pte) set_pte(ptep, pte)
94+#define kvm_set_pmd(pmdp, pmd) set_pmd(pmdp, pmd)
9495
9596 static inline bool kvm_is_write_fault(unsigned long esr)
9697 {
@@ -116,13 +117,18 @@ static inline void kvm_set_s2pte_writable(pte_t *pte) · 19 unmodified lines
116117 pte_val(*pte) |= PTE_S2_RDWR;
117118 }
118119
120+static inline void kvm_set_s2pmd_writable(pmd_t *pmd)
121+{
122+ pmd_val(*pmd) |= PMD_S2_RDWR;
123+}
124+
119125 struct kvm;
120126
121−static inline void coherent_icache_guest_page(struct kvm *kvm, gfn_t gfn)
127+static inline void coherent_icache_guest_page(struct kvm *kvm, hva_t hva,
128+ unsigned long size)
122129 {
123130 if (!icache_is_aliasing()) { /* PIPT */
124− unsigned long hva = gfn_to_hva(kvm, gfn);
125− flush_icache_range(hva, hva + PAGE_SIZE);
131+ flush_icache_range(hva, hva + size);
126132 } else if (!icache_is_aivivt()) { /* non ASID-tagged VIVT */
127133 /* any kind of VIPT cache */
128134 __flush_icache_all();
arch/arm64/include/asm/pgtable-hwdef.h+2−0
@@ -85,6 +85,8 @@ · 84 unmodified lines
8585 #define PTE_S2_RDONLY (_AT(pteval_t, 1) << 6) /* HAP[2:1] */
8686 #define PTE_S2_RDWR (_AT(pteval_t, 3) << 6) /* HAP[2:1] */
8787
88+#define PMD_S2_RDWR (_AT(pmdval_t, 3) << 6) /* HAP[2:1] */
89+
8890 /*
8991 * Memory Attribute override for Stage-2 (MemAttr[3:0])
9092 */
arch/arm64/kvm/Kconfig+1−0
@@ -21,6 +21,7 @@ config KVM · 20 unmodified lines
2121 select MMU_NOTIFIER
2222 select PREEMPT_NOTIFIERS
2323 select ANON_INODES
24+ select HAVE_KVM_CPU_RELAX_INTERCEPT
2425 select KVM_MMIO
2526 select KVM_ARM_HOST
2627 select KVM_ARM_VGIC
arch/arm64/kvm/guest.c+20−0
@@ -248,6 +248,26 @@ int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, · 247 unmodified lines
248248 return kvm_reset_vcpu(vcpu);
249249 }
250250
251+int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
252+{
253+ int target = kvm_target_cpu();
254+
255+ if (target < 0)
256+ return -ENODEV;
257+
258+ memset(init, 0, sizeof(*init));
259+
260+ /*
261+ * For now, we don't return any features.
262+ * In future, we might use features to return target
263+ * specific features available for the preferred
264+ * target type.
265+ */
266+ init->target = (__u32)target;
267+
268+ return 0;
269+}
270+
251271 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
252272 {
253273 return -EINVAL;
arch/arm64/kvm/handle_exit.c+13−5
@@ -47,21 +47,29 @@ static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run) · 46 unmodified lines
4747 }
4848
4949 /**
50− * kvm_handle_wfi - handle a wait-for-interrupts instruction executed by a guest
50+ * kvm_handle_wfx - handle a wait-for-interrupts or wait-for-event
51+ * instruction executed by a guest
52+ *
5153 * @vcpu: the vcpu pointer
5254 *
53− * Simply call kvm_vcpu_block(), which will halt execution of
55+ * WFE: Yield the CPU and come back to this vcpu when the scheduler
56+ * decides to.
57+ * WFI: Simply call kvm_vcpu_block(), which will halt execution of
5458 * world-switches and schedule other host processes until there is an
5559 * incoming IRQ or FIQ to the VM.
5660 */
57−static int kvm_handle_wfi(struct kvm_vcpu *vcpu, struct kvm_run *run)
61+static int kvm_handle_wfx(struct kvm_vcpu *vcpu, struct kvm_run *run)
5862 {
59− kvm_vcpu_block(vcpu);
63+ if (kvm_vcpu_get_hsr(vcpu) & ESR_EL2_EC_WFI_ISS_WFE)
64+ kvm_vcpu_on_spin(vcpu);
65+ else
66+ kvm_vcpu_block(vcpu);
67+
6068 return 1;
6169 }
6270
6371 static exit_handle_fn arm_exit_handlers[] = {
64− [ESR_EL2_EC_WFI] = kvm_handle_wfi,
72+ [ESR_EL2_EC_WFI] = kvm_handle_wfx,
6573 [ESR_EL2_EC_CP15_32] = kvm_handle_cp15_32,
6674 [ESR_EL2_EC_CP15_64] = kvm_handle_cp15_64,
6775 [ESR_EL2_EC_CP14_MR] = kvm_handle_cp14_access,
arch/ia64/include/asm/kvm_host.h+1−5
@@ -234,10 +234,6 @@ struct kvm_vm_data { · 233 unmodified lines
234234 #define KVM_REQ_PTC_G 32
235235 #define KVM_REQ_RESUME 33
236236
237−#define KVM_HPAGE_GFN_SHIFT(x) 0
238−#define KVM_NR_PAGE_SIZES 1
239−#define KVM_PAGES_PER_HPAGE(x) 1
240−
241237 struct kvm;
242238 struct kvm_vcpu;
243239
@@ -480,7 +476,7 @@ struct kvm_arch { · 236 unmodified lines
480476
481477 struct list_head assigned_dev_head;
482478 struct iommu_domain *iommu_domain;
483− int iommu_flags;
479+ bool iommu_noncoherent;
484480
485481 unsigned long irq_sources_bitmap;
486482 unsigned long irq_states[KVM_IOAPIC_NUM_PINS];
arch/ia64/kvm/kvm-ia64.c+3−2
@@ -1550,12 +1550,13 @@ int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) · 1549 unmodified lines
15501550 return VM_FAULT_SIGBUS;
15511551 }
15521552
1553−void kvm_arch_free_memslot(struct kvm_memory_slot *free,
1553+void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
15541554 struct kvm_memory_slot *dont)
15551555 {
15561556 }
15571557
1558−int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
1558+int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1559+ unsigned long npages)
15591560 {
15601561 return 0;
15611562 }
arch/mips/include/asm/kvm_host.h+0−7
@@ -27,13 +27,6 @@ · 26 unmodified lines
2727
2828 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
2929
30−/* Don't support huge pages */
31−#define KVM_HPAGE_GFN_SHIFT(x) 0
32−
33−/* We don't currently support large pages. */
34−#define KVM_NR_PAGE_SIZES 1
35−#define KVM_PAGES_PER_HPAGE(x) 1
36−
3730
3831
3932 /* Special address that contains the comm page, used for reducing # of traps */
arch/mips/kvm/kvm_mips.c+3−2
@@ -198,12 +198,13 @@ kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · 197 unmodified lines
198198 return -ENOIOCTLCMD;
199199 }
200200
201−void kvm_arch_free_memslot(struct kvm_memory_slot *free,
201+void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
202202 struct kvm_memory_slot *dont)
203203 {
204204 }
205205
206−int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
206+int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
207+ unsigned long npages)
207208 {
208209 return 0;
209210 }
arch/powerpc/include/asm/disassemble.h+4−0
@@ -77,4 +77,8 @@ static inline unsigned int get_d(u32 inst) · 76 unmodified lines
7777 return inst & 0xffff;
7878 }
7979
80+static inline unsigned int get_oc(u32 inst)
81+{
82+ return (inst >> 11) & 0x7fff;
83+}
8084 #endif /* __ASM_PPC_DISASSEMBLE_H__ */
arch/powerpc/include/asm/exception-64s.h+20−1
@@ -198,12 +198,27 @@ END_FTR_SECTION_NESTED(ftr,ftr,943) · 197 unmodified lines
198198 cmpwi r10,0; \
199199 bne do_kvm_##n
200200
201+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
202+/*
203+ * If hv is possible, interrupts come into to the hv version
204+ * of the kvmppc_interrupt code, which then jumps to the PR handler,
205+ * kvmppc_interrupt_pr, if the guest is a PR guest.
206+ */
207+#define kvmppc_interrupt kvmppc_interrupt_hv
208+#else
209+#define kvmppc_interrupt kvmppc_interrupt_pr
210+#endif
211+
201212 #define __KVM_HANDLER(area, h, n) \
202213 do_kvm_##n: \
203214 BEGIN_FTR_SECTION_NESTED(947) \
204215 ld r10,area+EX_CFAR(r13); \
205216 std r10,HSTATE_CFAR(r13); \
206217 END_FTR_SECTION_NESTED(CPU_FTR_CFAR,CPU_FTR_CFAR,947); \
218+ BEGIN_FTR_SECTION_NESTED(948) \
219+ ld r10,area+EX_PPR(r13); \
220+ std r10,HSTATE_PPR(r13); \
221+ END_FTR_SECTION_NESTED(CPU_FTR_HAS_PPR,CPU_FTR_HAS_PPR,948); \
207222 ld r10,area+EX_R10(r13); \
208223 stw r9,HSTATE_SCRATCH1(r13); \
209224 ld r9,area+EX_R9(r13); \
@@ -217,6 +232,10 @@ do_kvm_##n: \ · 7 unmodified lines
217232 ld r10,area+EX_R10(r13); \
218233 beq 89f; \
219234 stw r9,HSTATE_SCRATCH1(r13); \
235+ BEGIN_FTR_SECTION_NESTED(948) \
236+ ld r9,area+EX_PPR(r13); \
237+ std r9,HSTATE_PPR(r13); \
238+ END_FTR_SECTION_NESTED(CPU_FTR_HAS_PPR,CPU_FTR_HAS_PPR,948); \
220239 ld r9,area+EX_R9(r13); \
221240 std r12,HSTATE_SCRATCH0(r13); \
222241 li r12,n; \
@@ -236,7 +255,7 @@ do_kvm_##n: \ · 13 unmodified lines
236255 #define KVM_HANDLER_SKIP(area, h, n)
237256 #endif
238257
239−#ifdef CONFIG_KVM_BOOK3S_PR
258+#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
240259 #define KVMTEST_PR(n) __KVMTEST(n)
241260 #define KVM_HANDLER_PR(area, h, n) __KVM_HANDLER(area, h, n)
242261 #define KVM_HANDLER_PR_SKIP(area, h, n) __KVM_HANDLER_SKIP(area, h, n)
arch/powerpc/include/asm/kvm_asm.h+4−0
@@ -123,6 +123,8 @@ · 122 unmodified lines
123123 #define BOOK3S_HFLAG_SLB 0x2
124124 #define BOOK3S_HFLAG_PAIRED_SINGLE 0x4
125125 #define BOOK3S_HFLAG_NATIVE_PS 0x8
126+#define BOOK3S_HFLAG_MULTI_PGSIZE 0x10
127+#define BOOK3S_HFLAG_NEW_TLBIE 0x20
126128
127129 #define RESUME_FLAG_NV (1<<0) /* Reload guest nonvolatile state? */
128130 #define RESUME_FLAG_HOST (1<<1) /* Resume host? */
@@ -136,6 +138,8 @@ · 7 unmodified lines
136138 #define KVM_GUEST_MODE_NONE 0
137139 #define KVM_GUEST_MODE_GUEST 1
138140 #define KVM_GUEST_MODE_SKIP 2
141+#define KVM_GUEST_MODE_GUEST_HV 3
142+#define KVM_GUEST_MODE_HOST_HV 4
139143
140144 #define KVM_INST_FETCH_FAILED -1
141145
arch/powerpc/include/asm/kvm_book3s.h+19−213
@@ -58,16 +58,18 @@ struct hpte_cache { · 57 unmodified lines
5858 struct hlist_node list_pte_long;
5959 struct hlist_node list_vpte;
6060 struct hlist_node list_vpte_long;
61+#ifdef CONFIG_PPC_BOOK3S_64
62+ struct hlist_node list_vpte_64k;
63+#endif
6164 struct rcu_head rcu_head;
6265 u64 host_vpn;
6366 u64 pfn;
6467 ulong slot;
6568 struct kvmppc_pte pte;
69+ int pagesize;
6670 };
6771
6872 struct kvmppc_vcpu_book3s {
69− struct kvm_vcpu vcpu;
70− struct kvmppc_book3s_shadow_vcpu *shadow_vcpu;
7173 struct kvmppc_sid_map sid_map[SID_MAP_NUM];
7274 struct {
7375 u64 esid;
@@ -99,6 +101,9 @@ struct kvmppc_vcpu_book3s { · 25 unmodified lines
99101 struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
100102 struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
101103 struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
104+#ifdef CONFIG_PPC_BOOK3S_64
105+ struct hlist_head hpte_hash_vpte_64k[HPTEG_HASH_NUM_VPTE_64K];
106+#endif
102107 int hpte_cache_count;
103108 spinlock_t mmu_lock;
104109 };
@@ -107,8 +112,9 @@ struct kvmppc_vcpu_book3s { · 2 unmodified lines
107112 #define CONTEXT_GUEST 1
108113 #define CONTEXT_GUEST_END 2
109114
110−#define VSID_REAL 0x0fffffffffc00000ULL
111−#define VSID_BAT 0x0fffffffffb00000ULL
115+#define VSID_REAL 0x07ffffffffc00000ULL
116+#define VSID_BAT 0x07ffffffffb00000ULL
117+#define VSID_64K 0x0800000000000000ULL
112118 #define VSID_1T 0x1000000000000000ULL
113119 #define VSID_REAL_DR 0x2000000000000000ULL
114120 #define VSID_REAL_IR 0x4000000000000000ULL
@@ -118,11 +124,12 @@ extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask) · 3 unmodified lines
118124 extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
119125 extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
120126 extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
121−extern void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr);
122127 extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
123128 extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
124129 extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
125−extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
130+extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
131+ bool iswrite);
132+extern void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
126133 extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
127134 extern void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong eaddr, ulong seg_size);
128135 extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
@@ -134,6 +141,7 @@ extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, · 5 unmodified lines
134141
135142 extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
136143 extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
144+extern void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte);
137145 extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
138146 extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
139147 extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
@@ -151,7 +159,8 @@ extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, · 11 unmodified lines
151159 bool upper, u32 val);
152160 extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
153161 extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
154−extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
162+extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, bool writing,
163+ bool *writable);
155164 extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
156165 unsigned long *rmap, long pte_index, int realmode);
157166 extern void kvmppc_invalidate_hpte(struct kvm *kvm, unsigned long *hptep,
@@ -172,6 +181,8 @@ extern long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags, · 14 unmodified lines
172181 unsigned long *hpret);
173182 extern long kvmppc_hv_get_dirty_log(struct kvm *kvm,
174183 struct kvm_memory_slot *memslot, unsigned long *map);
184+extern void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr,
185+ unsigned long mask);
175186
176187 extern void kvmppc_entry_trampoline(void);
177188 extern void kvmppc_hv_entry_trampoline(void);
@@ -184,11 +195,9 @@ extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd); · 6 unmodified lines
184195
185196 static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
186197 {
187− return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
198+ return vcpu->arch.book3s;
188199 }
189200
190−extern void kvm_return_point(void);
191−
192201 /* Also add subarch specific defines */
193202
194203 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
@@ -198,203 +207,6 @@ extern void kvm_return_point(void); · 3 unmodified lines
198207 #include <asm/kvm_book3s_64.h>
199208 #endif
200209
201−#ifdef CONFIG_KVM_BOOK3S_PR
202−
203−static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
204−{
205− return to_book3s(vcpu)->hior;
206−}
207−
208−static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
209− unsigned long pending_now, unsigned long old_pending)
210−{
211− if (pending_now)
212− vcpu->arch.shared->int_pending = 1;
213− else if (old_pending)
214− vcpu->arch.shared->int_pending = 0;
215−}
216−
217−static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
218−{
219− if ( num < 14 ) {
220− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
221− svcpu->gpr[num] = val;
222− svcpu_put(svcpu);
223− to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
224− } else
225− vcpu->arch.gpr[num] = val;
226−}
227−
228−static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
229−{
230− if ( num < 14 ) {
231− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
232− ulong r = svcpu->gpr[num];
233− svcpu_put(svcpu);
234− return r;
235− } else
236− return vcpu->arch.gpr[num];
237−}
238−
239−static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
240−{
241− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
242− svcpu->cr = val;
243− svcpu_put(svcpu);
244− to_book3s(vcpu)->shadow_vcpu->cr = val;
245−}
246−
247−static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
248−{
249− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
250− u32 r;
251− r = svcpu->cr;
252− svcpu_put(svcpu);
253− return r;
254−}
255−
256−static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
257−{
258− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
259− svcpu->xer = val;
260− to_book3s(vcpu)->shadow_vcpu->xer = val;
261− svcpu_put(svcpu);
262−}
263−
264−static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
265−{
266− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
267− u32 r;
268− r = svcpu->xer;
269− svcpu_put(svcpu);
270− return r;
271−}
272−
273−static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
274−{
275− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
276− svcpu->ctr = val;
277− svcpu_put(svcpu);
278−}
279−
280−static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
281−{
282− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
283− ulong r;
284− r = svcpu->ctr;
285− svcpu_put(svcpu);
286− return r;
287−}
288−
289−static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
290−{
291− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
292− svcpu->lr = val;
293− svcpu_put(svcpu);
294−}
295−
296−static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
297−{
298− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
299− ulong r;
300− r = svcpu->lr;
301− svcpu_put(svcpu);
302− return r;
303−}
304−
305−static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
306−{
307− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
308− svcpu->pc = val;
309− svcpu_put(svcpu);
310−}
311−
312−static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
313−{
314− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
315− ulong r;
316− r = svcpu->pc;
317− svcpu_put(svcpu);
318− return r;
319−}
320−
321−static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
322−{
323− ulong pc = kvmppc_get_pc(vcpu);
324− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
325− u32 r;
326−
327− /* Load the instruction manually if it failed to do so in the
328− * exit path */
329− if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
330− kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
331−
332− r = svcpu->last_inst;
333− svcpu_put(svcpu);
334− return r;
335−}
336−
337−/*
338− * Like kvmppc_get_last_inst(), but for fetching a sc instruction.
339− * Because the sc instruction sets SRR0 to point to the following
340− * instruction, we have to fetch from pc - 4.
341− */
342−static inline u32 kvmppc_get_last_sc(struct kvm_vcpu *vcpu)
343−{
344− ulong pc = kvmppc_get_pc(vcpu) - 4;
345− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
346− u32 r;
347−
348− /* Load the instruction manually if it failed to do so in the
349− * exit path */
350− if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
351− kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
352−
353− r = svcpu->last_inst;
354− svcpu_put(svcpu);
355− return r;
356−}
357−
358−static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
359−{
360− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
361− ulong r;
362− r = svcpu->fault_dar;
363− svcpu_put(svcpu);
364− return r;
365−}
366−
367−static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
368−{
369− ulong crit_raw = vcpu->arch.shared->critical;
370− ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
371− bool crit;
372−
373− /* Truncate crit indicators in 32 bit mode */
374− if (!(vcpu->arch.shared->msr & MSR_SF)) {
375− crit_raw &= 0xffffffff;
376− crit_r1 &= 0xffffffff;
377− }
378−
379− /* Critical section when crit == r1 */
380− crit = (crit_raw == crit_r1);
381− /* ... and we're in supervisor mode */
382− crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
383−
384− return crit;
385−}
386−#else /* CONFIG_KVM_BOOK3S_PR */
387−
388−static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
389−{
390− return 0;
391−}
392−
393−static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
394− unsigned long pending_now, unsigned long old_pending)
395−{
396−}
397−
398210 static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
399211 {
400212 vcpu->arch.gpr[num] = val;
@@ -489,12 +301,6 @@ static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu) · 88 unmodified lines
489301 return vcpu->arch.fault_dar;
490302 }
491303
492−static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
493−{
494− return false;
495−}
496−#endif
497−
498304 /* Magic register values loaded into r3 and r4 before the 'sc' assembly
499305 * instruction for the OSI hypercalls */
500306 #define OSI_SC_MAGIC_R3 0x113724FA
arch/powerpc/include/asm/kvm_book3s_32.h+1−1
@@ -22,7 +22,7 @@ · 21 unmodified lines
2222
2323 static inline struct kvmppc_book3s_shadow_vcpu *svcpu_get(struct kvm_vcpu *vcpu)
2424 {
25− return to_book3s(vcpu)->shadow_vcpu;
25+ return vcpu->arch.shadow_vcpu;
2626 }
2727
2828 static inline void svcpu_put(struct kvmppc_book3s_shadow_vcpu *svcpu)
arch/powerpc/include/asm/kvm_book3s_64.h+4−4
@@ -20,7 +20,7 @@ · 19 unmodified lines
2020 #ifndef __ASM_KVM_BOOK3S_64_H__
2121 #define __ASM_KVM_BOOK3S_64_H__
2222
23−#ifdef CONFIG_KVM_BOOK3S_PR
23+#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
2424 static inline struct kvmppc_book3s_shadow_vcpu *svcpu_get(struct kvm_vcpu *vcpu)
2525 {
2626 preempt_disable();
@@ -35,7 +35,7 @@ static inline void svcpu_put(struct kvmppc_book3s_shadow_vcpu *svcpu) · 8 unmodified lines
3535
3636 #define SPAPR_TCE_SHIFT 12
3737
38−#ifdef CONFIG_KVM_BOOK3S_64_HV
38+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
3939 #define KVM_DEFAULT_HPT_ORDER 24 /* 16MB HPT by default */
4040 extern unsigned long kvm_rma_pages;
4141 #endif
@@ -278,7 +278,7 @@ static inline int is_vrma_hpte(unsigned long hpte_v) · 236 unmodified lines
278278 (HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)));
279279 }
280280
281−#ifdef CONFIG_KVM_BOOK3S_64_HV
281+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
282282 /*
283283 * Note modification of an HPTE; set the HPTE modified bit
284284 * if anyone is interested.
@@ -289,6 +289,6 @@ static inline void note_hpte_modification(struct kvm *kvm, · 4 unmodified lines
289289 if (atomic_read(&kvm->arch.hpte_mod_interest))
290290 rev->guest_rpte |= HPTE_GR_MODIFIED;
291291 }
292−#endif /* CONFIG_KVM_BOOK3S_64_HV */
292+#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
293293
294294 #endif /* __ASM_KVM_BOOK3S_64_H__ */
arch/powerpc/include/asm/kvm_book3s_asm.h+5−4
@@ -83,7 +83,7 @@ struct kvmppc_host_state { · 82 unmodified lines
8383 u8 restore_hid5;
8484 u8 napping;
8585
86−#ifdef CONFIG_KVM_BOOK3S_64_HV
86+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
8787 u8 hwthread_req;
8888 u8 hwthread_state;
8989 u8 host_ipi;
@@ -101,6 +101,7 @@ struct kvmppc_host_state { · 11 unmodified lines
101101 #endif
102102 #ifdef CONFIG_PPC_BOOK3S_64
103103 u64 cfar;
104+ u64 ppr;
104105 #endif
105106 };
106107
@@ -108,14 +109,14 @@ struct kvmppc_book3s_shadow_vcpu { · 1 unmodified line
108109 ulong gpr[14];
109110 u32 cr;
110111 u32 xer;
111−
112− u32 fault_dsisr;
113− u32 last_inst;
114112 ulong ctr;
115113 ulong lr;
116114 ulong pc;
115+
117116 ulong shadow_srr1;
118117 ulong fault_dar;
118+ u32 fault_dsisr;
119+ u32 last_inst;
119120
120121 #ifdef CONFIG_PPC_BOOK3S_32
121122 u32 sr[16]; /* Guest SRs */
arch/powerpc/include/asm/kvm_booke.h+6−1
@@ -26,7 +26,12 @@ · 25 unmodified lines
2626 /* LPIDs we support with this build -- runtime limit may be lower */
2727 #define KVMPPC_NR_LPIDS 64
2828
29−#define KVMPPC_INST_EHPRIV 0x7c00021c
29+#define KVMPPC_INST_EHPRIV 0x7c00021c
30+#define EHPRIV_OC_SHIFT 11
31+/* "ehpriv 1" : ehpriv with OC = 1 is used for debug emulation */
32+#define EHPRIV_OC_DEBUG 1
33+#define KVMPPC_INST_EHPRIV_DEBUG (KVMPPC_INST_EHPRIV | \
34+ (EHPRIV_OC_DEBUG << EHPRIV_OC_SHIFT))
3035
3136 static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
3237 {
arch/powerpc/include/asm/kvm_host.h+34−23
@@ -63,20 +63,17 @@ extern void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte); · 62 unmodified lines
6363
6464 #endif
6565
66−/* We don't currently support large pages. */
67−#define KVM_HPAGE_GFN_SHIFT(x) 0
68−#define KVM_NR_PAGE_SIZES 1
69−#define KVM_PAGES_PER_HPAGE(x) (1UL<<31)
70−
7166 #define HPTEG_CACHE_NUM (1 << 15)
7267 #define HPTEG_HASH_BITS_PTE 13
7368 #define HPTEG_HASH_BITS_PTE_LONG 12
7469 #define HPTEG_HASH_BITS_VPTE 13
7570 #define HPTEG_HASH_BITS_VPTE_LONG 5
71+#define HPTEG_HASH_BITS_VPTE_64K 11
7672 #define HPTEG_HASH_NUM_PTE (1 << HPTEG_HASH_BITS_PTE)
7773 #define HPTEG_HASH_NUM_PTE_LONG (1 << HPTEG_HASH_BITS_PTE_LONG)
7874 #define HPTEG_HASH_NUM_VPTE (1 << HPTEG_HASH_BITS_VPTE)
7975 #define HPTEG_HASH_NUM_VPTE_LONG (1 << HPTEG_HASH_BITS_VPTE_LONG)
76+#define HPTEG_HASH_NUM_VPTE_64K (1 << HPTEG_HASH_BITS_VPTE_64K)
8077
8178 /* Physical Address Mask - allowed range of real mode RAM access */
8279 #define KVM_PAM 0x0fffffffffffffffULL
@@ -89,6 +86,9 @@ struct lppaca; · 6 unmodified lines
8986 struct slb_shadow;
9087 struct dtl_entry;
9188
89+struct kvmppc_vcpu_book3s;
90+struct kvmppc_book3s_shadow_vcpu;
91+
9292 struct kvm_vm_stat {
9393 u32 remote_tlb_flush;
9494 };
@@ -224,15 +224,15 @@ struct revmap_entry { · 129 unmodified lines
224224 #define KVMPPC_GOT_PAGE 0x80
225225
226226 struct kvm_arch_memory_slot {
227−#ifdef CONFIG_KVM_BOOK3S_64_HV
227+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
228228 unsigned long *rmap;
229229 unsigned long *slot_phys;
230−#endif /* CONFIG_KVM_BOOK3S_64_HV */
230+#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
231231 };
232232
233233 struct kvm_arch {
234234 unsigned int lpid;
235−#ifdef CONFIG_KVM_BOOK3S_64_HV
235+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
236236 unsigned long hpt_virt;
237237 struct revmap_entry *revmap;
238238 unsigned int host_lpid;
@@ -256,7 +256,10 @@ struct kvm_arch { · 17 unmodified lines
256256 cpumask_t need_tlb_flush;
257257 struct kvmppc_vcore *vcores[KVM_MAX_VCORES];
258258 int hpt_cma_alloc;
259−#endif /* CONFIG_KVM_BOOK3S_64_HV */
259+#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
260+#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
261+ struct mutex hpt_mutex;
262+#endif
260263 #ifdef CONFIG_PPC_BOOK3S_64
261264 struct list_head spapr_tce_tables;
262265 struct list_head rtas_tokens;
@@ -267,6 +270,7 @@ struct kvm_arch { · 4 unmodified lines
267270 #ifdef CONFIG_KVM_XICS
268271 struct kvmppc_xics *xics;
269272 #endif
273+ struct kvmppc_ops *kvm_ops;
270274 };
271275
272276 /*
@@ -294,6 +298,10 @@ struct kvmppc_vcore { · 21 unmodified lines
294298 u64 stolen_tb;
295299 u64 preempt_tb;
296300 struct kvm_vcpu *runner;
301+ u64 tb_offset; /* guest timebase - host timebase */
302+ ulong lpcr;
303+ u32 arch_compat;
304+ ulong pcr;
297305 };
298306
299307 #define VCORE_ENTRY_COUNT(vc) ((vc)->entry_exit_count & 0xff)
@@ -328,6 +336,7 @@ struct kvmppc_pte { · 28 unmodified lines
328336 bool may_read : 1;
329337 bool may_write : 1;
330338 bool may_execute : 1;
339+ u8 page_size; /* MMU_PAGE_xxx */
331340 };
332341
333342 struct kvmppc_mmu {
@@ -340,7 +349,8 @@ struct kvmppc_mmu { · 6 unmodified lines
340349 /* book3s */
341350 void (*mtsrin)(struct kvm_vcpu *vcpu, u32 srnum, ulong value);
342351 u32 (*mfsrin)(struct kvm_vcpu *vcpu, u32 srnum);
343− int (*xlate)(struct kvm_vcpu *vcpu, gva_t eaddr, struct kvmppc_pte *pte, bool data);
352+ int (*xlate)(struct kvm_vcpu *vcpu, gva_t eaddr,
353+ struct kvmppc_pte *pte, bool data, bool iswrite);
344354 void (*reset_msr)(struct kvm_vcpu *vcpu);
345355 void (*tlbie)(struct kvm_vcpu *vcpu, ulong addr, bool large);
346356 int (*esid_to_vsid)(struct kvm_vcpu *vcpu, ulong esid, u64 *vsid);
@@ -360,6 +370,7 @@ struct kvmppc_slb { · 13 unmodified lines
360370 bool large : 1; /* PTEs are 16MB */
361371 bool tb : 1; /* 1TB segment */
362372 bool class : 1;
373+ u8 base_page_size; /* MMU_PAGE_xxx */
363374 };
364375
365376 # ifdef CONFIG_PPC_FSL_BOOK3E
@@ -377,17 +388,6 @@ struct kvmppc_slb { · 11 unmodified lines
377388 #define KVMPPC_EPR_USER 1 /* exit to userspace to fill EPR */
378389 #define KVMPPC_EPR_KERNEL 2 /* in-kernel irqchip */
379390
380−struct kvmppc_booke_debug_reg {
381− u32 dbcr0;
382− u32 dbcr1;
383− u32 dbcr2;
384−#ifdef CONFIG_KVM_E500MC
385− u32 dbcr4;
386−#endif
387− u64 iac[KVMPPC_BOOKE_MAX_IAC];
388− u64 dac[KVMPPC_BOOKE_MAX_DAC];
389−};
390−
391391 #define KVMPPC_IRQ_DEFAULT 0
392392 #define KVMPPC_IRQ_MPIC 1
393393 #define KVMPPC_IRQ_XICS 2
@@ -402,6 +402,10 @@ struct kvm_vcpu_arch { · 8 unmodified lines
402402 int slb_max; /* 1 + index of last valid entry in slb[] */
403403 int slb_nr; /* total number of entries in SLB */
404404 struct kvmppc_mmu mmu;
405+ struct kvmppc_vcpu_book3s *book3s;
406+#endif
407+#ifdef CONFIG_PPC_BOOK3S_32
408+ struct kvmppc_book3s_shadow_vcpu *shadow_vcpu;
405409 #endif
406410
407411 ulong gpr[32];
@@ -463,6 +467,8 @@ struct kvm_vcpu_arch { · 55 unmodified lines
463467 u32 ctrl;
464468 ulong dabr;
465469 ulong cfar;
470+ ulong ppr;
471+ ulong shadow_srr1;
466472 #endif
467473 u32 vrsave; /* also USPRG0 */
468474 u32 mmucr;
@@ -498,6 +504,8 @@ struct kvm_vcpu_arch { · 29 unmodified lines
498504
499505 u64 mmcr[3];
500506 u32 pmc[8];
507+ u64 siar;
508+ u64 sdar;
501509
502510 #ifdef CONFIG_KVM_EXIT_TIMING
503511 struct mutex exit_timing_lock;
@@ -531,7 +539,10 @@ struct kvm_vcpu_arch { · 27 unmodified lines
531539 u32 eptcfg;
532540 u32 epr;
533541 u32 crit_save;
534− struct kvmppc_booke_debug_reg dbg_reg;
542+ /* guest debug registers*/
543+ struct debug_reg dbg_reg;
544+ /* hardware visible debug registers when in guest state */
545+ struct debug_reg shadow_dbg_reg;
535546 #endif
536547 gpa_t paddr_accessed;
537548 gva_t vaddr_accessed;
@@ -582,7 +593,7 @@ struct kvm_vcpu_arch { · 44 unmodified lines
582593 struct kvmppc_icp *icp; /* XICS presentation controller */
583594 #endif
584595
585−#ifdef CONFIG_KVM_BOOK3S_64_HV
596+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
586597 struct kvm_vcpu_arch_shared shregs;
587598
588599 unsigned long pgfault_addr;
arch/powerpc/include/asm/kvm_ppc.h+79−28
@@ -106,13 +106,6 @@ extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu, · 105 unmodified lines
106106 struct kvm_interrupt *irq);
107107 extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
108108 extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
109−
110−extern int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
111− unsigned int op, int *advance);
112−extern int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn,
113− ulong val);
114−extern int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn,
115− ulong *val);
116109 extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
117110
118111 extern int kvmppc_booke_init(void);
@@ -135,17 +128,17 @@ extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · 16 unmodified lines
135128 struct kvm_create_spapr_tce *args);
136129 extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
137130 unsigned long ioba, unsigned long tce);
138−extern long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
139− struct kvm_allocate_rma *rma);
140131 extern struct kvm_rma_info *kvm_alloc_rma(void);
141132 extern void kvm_release_rma(struct kvm_rma_info *ri);
142133 extern struct page *kvm_alloc_hpt(unsigned long nr_pages);
143134 extern void kvm_release_hpt(struct page *page, unsigned long nr_pages);
144135 extern int kvmppc_core_init_vm(struct kvm *kvm);
145136 extern void kvmppc_core_destroy_vm(struct kvm *kvm);
146−extern void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
137+extern void kvmppc_core_free_memslot(struct kvm *kvm,
138+ struct kvm_memory_slot *free,
147139 struct kvm_memory_slot *dont);
148−extern int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
140+extern int kvmppc_core_create_memslot(struct kvm *kvm,
141+ struct kvm_memory_slot *slot,
149142 unsigned long npages);
150143 extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
151144 struct kvm_memory_slot *memslot,
@@ -177,6 +170,72 @@ extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, · 25 unmodified lines
177170 extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
178171 extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
179172
173+union kvmppc_one_reg {
174+ u32 wval;
175+ u64 dval;
176+ vector128 vval;
177+ u64 vsxval[2];
178+ struct {
179+ u64 addr;
180+ u64 length;
181+ } vpaval;
182+};
183+
184+struct kvmppc_ops {
185+ struct module *owner;
186+ int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
187+ int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
188+ int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
189+ union kvmppc_one_reg *val);
190+ int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
191+ union kvmppc_one_reg *val);
192+ void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
193+ void (*vcpu_put)(struct kvm_vcpu *vcpu);
194+ void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
195+ int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
196+ struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id);
197+ void (*vcpu_free)(struct kvm_vcpu *vcpu);
198+ int (*check_requests)(struct kvm_vcpu *vcpu);
199+ int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
200+ void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
201+ int (*prepare_memory_region)(struct kvm *kvm,
202+ struct kvm_memory_slot *memslot,
203+ struct kvm_userspace_memory_region *mem);
204+ void (*commit_memory_region)(struct kvm *kvm,
205+ struct kvm_userspace_memory_region *mem,
206+ const struct kvm_memory_slot *old);
207+ int (*unmap_hva)(struct kvm *kvm, unsigned long hva);
208+ int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
209+ unsigned long end);
210+ int (*age_hva)(struct kvm *kvm, unsigned long hva);
211+ int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
212+ void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
213+ void (*mmu_destroy)(struct kvm_vcpu *vcpu);
214+ void (*free_memslot)(struct kvm_memory_slot *free,
215+ struct kvm_memory_slot *dont);
216+ int (*create_memslot)(struct kvm_memory_slot *slot,
217+ unsigned long npages);
218+ int (*init_vm)(struct kvm *kvm);
219+ void (*destroy_vm)(struct kvm *kvm);
220+ int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
221+ int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu,
222+ unsigned int inst, int *advance);
223+ int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
224+ int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
225+ void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
226+ long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
227+ unsigned long arg);
228+
229+};
230+
231+extern struct kvmppc_ops *kvmppc_hv_ops;
232+extern struct kvmppc_ops *kvmppc_pr_ops;
233+
234+static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
235+{
236+ return kvm->arch.kvm_ops == kvmppc_hv_ops;
237+}
238+
180239 /*
181240 * Cuts out inst bits with ordering according to spec.
182241 * That means the leftmost bit is zero. All given bits are included.
@@ -210,17 +269,6 @@ static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value) · 27 unmodified lines
210269 return r;
211270 }
212271
213−union kvmppc_one_reg {
214− u32 wval;
215− u64 dval;
216− vector128 vval;
217− u64 vsxval[2];
218− struct {
219− u64 addr;
220− u64 length;
221− } vpaval;
222−};
223−
224272 #define one_reg_size(id) \
225273 (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
226274
@@ -245,10 +293,10 @@ union kvmppc_one_reg { · 18 unmodified lines
245293 __v; \
246294 })
247295
248−void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
296+int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
249297 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
250298
251−void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
299+int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
252300 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
253301
254302 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
@@ -260,7 +308,7 @@ void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid); · 5 unmodified lines
260308
261309 struct openpic;
262310
263−#ifdef CONFIG_KVM_BOOK3S_64_HV
311+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
264312 extern void kvm_cma_reserve(void) __init;
265313 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
266314 {
@@ -269,10 +317,10 @@ static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr) · 2 unmodified lines
269317
270318 static inline u32 kvmppc_get_xics_latch(void)
271319 {
272− u32 xirr = get_paca()->kvm_hstate.saved_xirr;
320+ u32 xirr;
273321
322+ xirr = get_paca()->kvm_hstate.saved_xirr;
274323 get_paca()->kvm_hstate.saved_xirr = 0;
275−
276324 return xirr;
277325 }
278326
@@ -281,7 +329,10 @@ static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi) · 2 unmodified lines
281329 paca[cpu].kvm_hstate.host_ipi = host_ipi;
282330 }
283331
284−extern void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu);
332+static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
333+{
334+ vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
335+}
285336
286337 #else
287338 static inline void __init kvm_cma_reserve(void)
arch/powerpc/include/asm/paca.h+1−1
@@ -166,7 +166,7 @@ struct paca_struct { · 165 unmodified lines
166166 struct dtl_entry *dtl_curr; /* pointer corresponding to dtl_ridx */
167167
168168 #ifdef CONFIG_KVM_BOOK3S_HANDLER
169−#ifdef CONFIG_KVM_BOOK3S_PR
169+#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
170170 /* We use this to store guest state in */
171171 struct kvmppc_book3s_shadow_vcpu shadow_vcpu;
172172 #endif
arch/powerpc/include/asm/processor.h+2−0
@@ -208,6 +208,7 @@ struct debug_reg { · 207 unmodified lines
208208
209209 struct thread_struct {
210210 unsigned long ksp; /* Kernel stack pointer */
211+
211212 #ifdef CONFIG_PPC64
212213 unsigned long ksp_vsid;
213214 #endif
@@ -221,6 +222,7 @@ struct thread_struct { · 7 unmodified lines
221222 void *pgdir; /* root of page-table tree */
222223 unsigned long ksp_limit; /* if ksp <= ksp_limit stack overflow */
223224 #endif
225+ /* Debug Registers */
224226 struct debug_reg debug;
225227 struct thread_fp_state fp_state;
226228 struct thread_fp_state *fp_save_area;
arch/powerpc/include/asm/pte-book3e.h+1−1
@@ -40,7 +40,7 @@ · 39 unmodified lines
4040 #define _PAGE_U1 0x010000
4141 #define _PAGE_U0 0x020000
4242 #define _PAGE_ACCESSED 0x040000
43−#define _PAGE_LENDIAN 0x080000
43+#define _PAGE_ENDIAN 0x080000
4444 #define _PAGE_GUARDED 0x100000
4545 #define _PAGE_COHERENT 0x200000 /* M: enforce memory coherence */
4646 #define _PAGE_NO_CACHE 0x400000 /* I: cache inhibit */
arch/powerpc/include/asm/reg.h+15−0
@@ -248,6 +248,7 @@ · 247 unmodified lines
248248 #define SPRN_TBRU 0x10D /* Time Base Read Upper Register (user, R/O) */
249249 #define SPRN_TBWL 0x11C /* Time Base Lower Register (super, R/W) */
250250 #define SPRN_TBWU 0x11D /* Time Base Upper Register (super, R/W) */
251+#define SPRN_TBU40 0x11E /* Timebase upper 40 bits (hyper, R/W) */
251252 #define SPRN_SPURR 0x134 /* Scaled PURR */
252253 #define SPRN_HSPRG0 0x130 /* Hypervisor Scratch 0 */
253254 #define SPRN_HSPRG1 0x131 /* Hypervisor Scratch 1 */
@@ -288,6 +289,7 @@ · 34 unmodified lines
288289 #define LPCR_ISL (1ul << (63-2))
289290 #define LPCR_VC_SH (63-2)
290291 #define LPCR_DPFD_SH (63-11)
292+#define LPCR_DPFD (7ul << LPCR_DPFD_SH)
291293 #define LPCR_VRMASD (0x1ful << (63-16))
292294 #define LPCR_VRMA_L (1ul << (63-12))
293295 #define LPCR_VRMA_LP0 (1ul << (63-15))
@@ -304,6 +306,7 @@ · 10 unmodified lines
304306 #define LPCR_PECE2 0x00001000 /* machine check etc can cause exit */
305307 #define LPCR_MER 0x00000800 /* Mediated External Exception */
306308 #define LPCR_MER_SH 11
309+#define LPCR_TC 0x00000200 /* Translation control */
307310 #define LPCR_LPES 0x0000000c
308311 #define LPCR_LPES0 0x00000008 /* LPAR Env selector 0 */
309312 #define LPCR_LPES1 0x00000004 /* LPAR Env selector 1 */
@@ -316,6 +319,10 @@ · 6 unmodified lines
316319 #define LPID_RSVD 0x3ff /* Reserved LPID for partn switching */
317320 #define SPRN_HMER 0x150 /* Hardware m? error recovery */
318321 #define SPRN_HMEER 0x151 /* Hardware m? enable error recovery */
322+#define SPRN_PCR 0x152 /* Processor compatibility register */
323+#define PCR_VEC_DIS (1ul << (63-0)) /* Vec. disable (bit NA since POWER8) */
324+#define PCR_VSX_DIS (1ul << (63-1)) /* VSX disable (bit NA since POWER8) */
325+#define PCR_ARCH_205 0x2 /* Architecture 2.05 */
319326 #define SPRN_HEIR 0x153 /* Hypervisor Emulated Instruction Register */
320327 #define SPRN_TLBINDEXR 0x154 /* P7 TLB control register */
321328 #define SPRN_TLBVPNR 0x155 /* P7 TLB control register */
@@ -425,6 +432,7 @@ · 103 unmodified lines
425432 #define HID4_RMLS2_SH (63 - 2) /* Real mode limit bottom 2 bits */
426433 #define HID4_LPID5_SH (63 - 6) /* partition ID bottom 4 bits */
427434 #define HID4_RMOR_SH (63 - 22) /* real mode offset (16 bits) */
435+#define HID4_RMOR (0xFFFFul << HID4_RMOR_SH)
428436 #define HID4_LPES1 (1 << (63-57)) /* LPAR env. sel. bit 1 */
429437 #define HID4_RMLS0_SH (63 - 58) /* Real mode limit top bit */
430438 #define HID4_LPID1_SH 0 /* partition ID top 2 bits */
@@ -1107,6 +1115,13 @@ · 676 unmodified lines
11071115 #define PVR_BE 0x0070
11081116 #define PVR_PA6T 0x0090
11091117
1118+/* "Logical" PVR values defined in PAPR, representing architecture levels */
1119+#define PVR_ARCH_204 0x0f000001
1120+#define PVR_ARCH_205 0x0f000002
1121+#define PVR_ARCH_206 0x0f000003
1122+#define PVR_ARCH_206p 0x0f100003
1123+#define PVR_ARCH_207 0x0f000004
1124+
11101125 /* Macros for setting and retrieving special purpose registers */
11111126 #ifndef __ASSEMBLY__
11121127 #define mfmsr() ({unsigned long rval; \
arch/powerpc/include/uapi/asm/kvm.h+82−4
@@ -27,6 +27,7 @@ · 26 unmodified lines
2727 #define __KVM_HAVE_PPC_SMT
2828 #define __KVM_HAVE_IRQCHIP
2929 #define __KVM_HAVE_IRQ_LINE
30+#define __KVM_HAVE_GUEST_DEBUG
3031
3132 struct kvm_regs {
3233 __u64 pc;
@@ -269,7 +270,24 @@ struct kvm_fpu { · 236 unmodified lines
269270 __u64 fpr[32];
270271 };
271272
273+/*
274+ * Defines for h/w breakpoint, watchpoint (read, write or both) and
275+ * software breakpoint.
276+ * These are used as "type" in KVM_SET_GUEST_DEBUG ioctl and "status"
277+ * for KVM_DEBUG_EXIT.
278+ */
279+#define KVMPPC_DEBUG_NONE 0x0
280+#define KVMPPC_DEBUG_BREAKPOINT (1UL << 1)
281+#define KVMPPC_DEBUG_WATCH_WRITE (1UL << 2)
282+#define KVMPPC_DEBUG_WATCH_READ (1UL << 3)
272283 struct kvm_debug_exit_arch {
284+ __u64 address;
285+ /*
286+ * exiting to userspace because of h/w breakpoint, watchpoint
287+ * (read, write or both) and software breakpoint.
288+ */
289+ __u32 status;
290+ __u32 reserved;
273291 };
274292
275293 /* for KVM_SET_GUEST_DEBUG */
@@ -281,10 +299,6 @@ struct kvm_guest_debug_arch { · 5 unmodified lines
281299 * Type denotes h/w breakpoint, read watchpoint, write
282300 * watchpoint or watchpoint (both read and write).
283301 */
284−#define KVMPPC_DEBUG_NONE 0x0
285−#define KVMPPC_DEBUG_BREAKPOINT (1UL << 1)
286−#define KVMPPC_DEBUG_WATCH_WRITE (1UL << 2)
287−#define KVMPPC_DEBUG_WATCH_READ (1UL << 3)
288302 __u32 type;
289303 __u32 reserved;
290304 } bp[16];
@@ -429,6 +443,11 @@ struct kvm_get_htab_header { · 138 unmodified lines
429443 #define KVM_REG_PPC_MMCR0 (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x10)
430444 #define KVM_REG_PPC_MMCR1 (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x11)
431445 #define KVM_REG_PPC_MMCRA (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x12)
446+#define KVM_REG_PPC_MMCR2 (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x13)
447+#define KVM_REG_PPC_MMCRS (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x14)
448+#define KVM_REG_PPC_SIAR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x15)
449+#define KVM_REG_PPC_SDAR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x16)
450+#define KVM_REG_PPC_SIER (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x17)
432451
433452 #define KVM_REG_PPC_PMC1 (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x18)
434453 #define KVM_REG_PPC_PMC2 (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x19)
@@ -499,6 +518,65 @@ struct kvm_get_htab_header { · 64 unmodified lines
499518 #define KVM_REG_PPC_TLB3PS (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x9a)
500519 #define KVM_REG_PPC_EPTCFG (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x9b)
501520
521+/* Timebase offset */
522+#define KVM_REG_PPC_TB_OFFSET (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x9c)
523+
524+/* POWER8 registers */
525+#define KVM_REG_PPC_SPMC1 (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x9d)
526+#define KVM_REG_PPC_SPMC2 (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x9e)
527+#define KVM_REG_PPC_IAMR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x9f)
528+#define KVM_REG_PPC_TFHAR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xa0)
529+#define KVM_REG_PPC_TFIAR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xa1)
530+#define KVM_REG_PPC_TEXASR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xa2)
531+#define KVM_REG_PPC_FSCR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xa3)
532+#define KVM_REG_PPC_PSPB (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xa4)
533+#define KVM_REG_PPC_EBBHR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xa5)
534+#define KVM_REG_PPC_EBBRR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xa6)
535+#define KVM_REG_PPC_BESCR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xa7)
536+#define KVM_REG_PPC_TAR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xa8)
537+#define KVM_REG_PPC_DPDES (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xa9)
538+#define KVM_REG_PPC_DAWR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xaa)
539+#define KVM_REG_PPC_DAWRX (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xab)
540+#define KVM_REG_PPC_CIABR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xac)
541+#define KVM_REG_PPC_IC (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xad)
542+#define KVM_REG_PPC_VTB (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xae)
543+#define KVM_REG_PPC_CSIGR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xaf)
544+#define KVM_REG_PPC_TACR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb0)
545+#define KVM_REG_PPC_TCSCR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb1)
546+#define KVM_REG_PPC_PID (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb2)
547+#define KVM_REG_PPC_ACOP (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb3)
548+
549+#define KVM_REG_PPC_VRSAVE (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb4)
550+#define KVM_REG_PPC_LPCR (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb5)
551+#define KVM_REG_PPC_PPR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb6)
552+
553+/* Architecture compatibility level */
554+#define KVM_REG_PPC_ARCH_COMPAT (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb7)
555+
556+/* Transactional Memory checkpointed state:
557+ * This is all GPRs, all VSX regs and a subset of SPRs
558+ */
559+#define KVM_REG_PPC_TM (KVM_REG_PPC | 0x80000000)
560+/* TM GPRs */
561+#define KVM_REG_PPC_TM_GPR0 (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0)
562+#define KVM_REG_PPC_TM_GPR(n) (KVM_REG_PPC_TM_GPR0 + (n))
563+#define KVM_REG_PPC_TM_GPR31 (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x1f)
564+/* TM VSX */
565+#define KVM_REG_PPC_TM_VSR0 (KVM_REG_PPC_TM | KVM_REG_SIZE_U128 | 0x20)
566+#define KVM_REG_PPC_TM_VSR(n) (KVM_REG_PPC_TM_VSR0 + (n))
567+#define KVM_REG_PPC_TM_VSR63 (KVM_REG_PPC_TM | KVM_REG_SIZE_U128 | 0x5f)
568+/* TM SPRS */
569+#define KVM_REG_PPC_TM_CR (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x60)
570+#define KVM_REG_PPC_TM_LR (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x61)
571+#define KVM_REG_PPC_TM_CTR (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x62)
572+#define KVM_REG_PPC_TM_FPSCR (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x63)
573+#define KVM_REG_PPC_TM_AMR (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x64)
574+#define KVM_REG_PPC_TM_PPR (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x65)
575+#define KVM_REG_PPC_TM_VRSAVE (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x66)
576+#define KVM_REG_PPC_TM_VSCR (KVM_REG_PPC_TM | KVM_REG_SIZE_U32 | 0x67)
577+#define KVM_REG_PPC_TM_DSCR (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x68)
578+#define KVM_REG_PPC_TM_TAR (KVM_REG_PPC_TM | KVM_REG_SIZE_U64 | 0x69)
579+
502580 /* PPC64 eXternal Interrupt Controller Specification */
503581 #define KVM_DEV_XICS_GRP_SOURCES 1 /* 64-bit source attributes */
504582
arch/powerpc/kernel/asm-offsets.c+14−7
@@ -439,7 +439,7 @@ int main(void) · 438 unmodified lines
439439 DEFINE(VCPU_LR, offsetof(struct kvm_vcpu, arch.lr));
440440 DEFINE(VCPU_CR, offsetof(struct kvm_vcpu, arch.cr));
441441 DEFINE(VCPU_PC, offsetof(struct kvm_vcpu, arch.pc));
442−#ifdef CONFIG_KVM_BOOK3S_64_HV
442+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
443443 DEFINE(VCPU_MSR, offsetof(struct kvm_vcpu, arch.shregs.msr));
444444 DEFINE(VCPU_SRR0, offsetof(struct kvm_vcpu, arch.shregs.srr0));
445445 DEFINE(VCPU_SRR1, offsetof(struct kvm_vcpu, arch.shregs.srr1));
@@ -470,7 +470,7 @@ int main(void) · 24 unmodified lines
470470 DEFINE(KVM_LPID, offsetof(struct kvm, arch.lpid));
471471
472472 /* book3s */
473−#ifdef CONFIG_KVM_BOOK3S_64_HV
473+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
474474 DEFINE(KVM_SDR1, offsetof(struct kvm, arch.sdr1));
475475 DEFINE(KVM_HOST_LPID, offsetof(struct kvm, arch.host_lpid));
476476 DEFINE(KVM_HOST_LPCR, offsetof(struct kvm, arch.host_lpcr));
@@ -502,6 +502,8 @@ int main(void) · 25 unmodified lines
502502 DEFINE(VCPU_PRODDED, offsetof(struct kvm_vcpu, arch.prodded));
503503 DEFINE(VCPU_MMCR, offsetof(struct kvm_vcpu, arch.mmcr));
504504 DEFINE(VCPU_PMC, offsetof(struct kvm_vcpu, arch.pmc));
505+ DEFINE(VCPU_SIAR, offsetof(struct kvm_vcpu, arch.siar));
506+ DEFINE(VCPU_SDAR, offsetof(struct kvm_vcpu, arch.sdar));
505507 DEFINE(VCPU_SLB, offsetof(struct kvm_vcpu, arch.slb));
506508 DEFINE(VCPU_SLB_MAX, offsetof(struct kvm_vcpu, arch.slb_max));
507509 DEFINE(VCPU_SLB_NR, offsetof(struct kvm_vcpu, arch.slb_nr));
@@ -511,18 +513,22 @@ int main(void) · 3 unmodified lines
511513 DEFINE(VCPU_TRAP, offsetof(struct kvm_vcpu, arch.trap));
512514 DEFINE(VCPU_PTID, offsetof(struct kvm_vcpu, arch.ptid));
513515 DEFINE(VCPU_CFAR, offsetof(struct kvm_vcpu, arch.cfar));
516+ DEFINE(VCPU_PPR, offsetof(struct kvm_vcpu, arch.ppr));
517+ DEFINE(VCPU_SHADOW_SRR1, offsetof(struct kvm_vcpu, arch.shadow_srr1));
514518 DEFINE(VCORE_ENTRY_EXIT, offsetof(struct kvmppc_vcore, entry_exit_count));
515519 DEFINE(VCORE_NAP_COUNT, offsetof(struct kvmppc_vcore, nap_count));
516520 DEFINE(VCORE_IN_GUEST, offsetof(struct kvmppc_vcore, in_guest));
517521 DEFINE(VCORE_NAPPING_THREADS, offsetof(struct kvmppc_vcore, napping_threads));
518− DEFINE(VCPU_SVCPU, offsetof(struct kvmppc_vcpu_book3s, shadow_vcpu) -
519− offsetof(struct kvmppc_vcpu_book3s, vcpu));
522+ DEFINE(VCORE_TB_OFFSET, offsetof(struct kvmppc_vcore, tb_offset));
523+ DEFINE(VCORE_LPCR, offsetof(struct kvmppc_vcore, lpcr));
524+ DEFINE(VCORE_PCR, offsetof(struct kvmppc_vcore, pcr));
520525 DEFINE(VCPU_SLB_E, offsetof(struct kvmppc_slb, orige));
521526 DEFINE(VCPU_SLB_V, offsetof(struct kvmppc_slb, origv));
522527 DEFINE(VCPU_SLB_SIZE, sizeof(struct kvmppc_slb));
523528
524529 #ifdef CONFIG_PPC_BOOK3S_64
525−#ifdef CONFIG_KVM_BOOK3S_PR
530+#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
531+ DEFINE(PACA_SVCPU, offsetof(struct paca_struct, shadow_vcpu));
526532 # define SVCPU_FIELD(x, f) DEFINE(x, offsetof(struct paca_struct, shadow_vcpu.f))
527533 #else
528534 # define SVCPU_FIELD(x, f)
@@ -574,7 +580,7 @@ int main(void) · 45 unmodified lines
574580 HSTATE_FIELD(HSTATE_RESTORE_HID5, restore_hid5);
575581 HSTATE_FIELD(HSTATE_NAPPING, napping);
576582
577−#ifdef CONFIG_KVM_BOOK3S_64_HV
583+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
578584 HSTATE_FIELD(HSTATE_HWTHREAD_REQ, hwthread_req);
579585 HSTATE_FIELD(HSTATE_HWTHREAD_STATE, hwthread_state);
580586 HSTATE_FIELD(HSTATE_KVM_VCPU, kvm_vcpu);
@@ -590,10 +596,11 @@ int main(void) · 9 unmodified lines
590596 HSTATE_FIELD(HSTATE_DABR, dabr);
591597 HSTATE_FIELD(HSTATE_DECEXP, dec_expires);
592598 DEFINE(IPI_PRIORITY, IPI_PRIORITY);
593−#endif /* CONFIG_KVM_BOOK3S_64_HV */
599+#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
594600
595601 #ifdef CONFIG_PPC_BOOK3S_64
596602 HSTATE_FIELD(HSTATE_CFAR, cfar);
603+ HSTATE_FIELD(HSTATE_PPR, ppr);
597604 #endif /* CONFIG_PPC_BOOK3S_64 */
598605
599606 #else /* CONFIG_PPC_BOOK3S */
arch/powerpc/kernel/exceptions-64s.S+28−2
@@ -126,7 +126,7 @@ BEGIN_FTR_SECTION · 125 unmodified lines
126126 bgt cr1,.
127127 GET_PACA(r13)
128128
129−#ifdef CONFIG_KVM_BOOK3S_64_HV
129+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
130130 li r0,KVM_HWTHREAD_IN_KERNEL
131131 stb r0,HSTATE_HWTHREAD_STATE(r13)
132132 /* Order setting hwthread_state vs. testing hwthread_req */
@@ -425,7 +425,7 @@ data_access_check_stab: · 292 unmodified lines
425425 mfspr r9,SPRN_DSISR
426426 srdi r10,r10,60
427427 rlwimi r10,r9,16,0x20
428−#ifdef CONFIG_KVM_BOOK3S_PR
428+#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
429429 lbz r9,HSTATE_IN_GUEST(r13)
430430 rlwimi r10,r9,8,0x300
431431 #endif
@@ -650,6 +650,32 @@ slb_miss_user_pseries: · 218 unmodified lines
650650 b . /* prevent spec. execution */
651651 #endif /* __DISABLED__ */
652652
653+#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
654+kvmppc_skip_interrupt:
655+ /*
656+ * Here all GPRs are unchanged from when the interrupt happened
657+ * except for r13, which is saved in SPRG_SCRATCH0.
658+ */
659+ mfspr r13, SPRN_SRR0
660+ addi r13, r13, 4
661+ mtspr SPRN_SRR0, r13
662+ GET_SCRATCH0(r13)
663+ rfid
664+ b .
665+
666+kvmppc_skip_Hinterrupt:
667+ /*
668+ * Here all GPRs are unchanged from when the interrupt happened
669+ * except for r13, which is saved in SPRG_SCRATCH0.
670+ */
671+ mfspr r13, SPRN_HSRR0
672+ addi r13, r13, 4
673+ mtspr SPRN_HSRR0, r13
674+ GET_SCRATCH0(r13)
675+ hrfid
676+ b .
677+#endif
678+
653679 /*
654680 * Code from here down to __end_handlers is invoked from the
655681 * exception prologs above. Because the prologs assemble the
arch/powerpc/kernel/idle_power7.S+1−1
@@ -84,7 +84,7 @@ _GLOBAL(power7_nap) · 83 unmodified lines
8484 std r9,_MSR(r1)
8585 std r1,PACAR1(r13)
8686
87−#ifdef CONFIG_KVM_BOOK3S_64_HV
87+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
8888 /* Tell KVM we're napping */
8989 li r4,KVM_HWTHREAD_IN_NAP
9090 stb r4,HSTATE_HWTHREAD_STATE(r13)
arch/powerpc/kernel/traps.c+1−1
@@ -1529,7 +1529,7 @@ static void handle_debug(struct pt_regs *regs, unsigned long debug_status) · 1528 unmodified lines
15291529 * back on or not.
15301530 */
15311531 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1532− current->thread.debug.dbcr1))
1532+ current->thread.debug.dbcr1))
15331533 regs->msr |= MSR_DE;
15341534 else
15351535 /* Make sure the IDM flag is off */
arch/powerpc/kvm/44x.c+43−15
@@ -31,13 +31,13 @@ · 30 unmodified lines
3131 #include "44x_tlb.h"
3232 #include "booke.h"
3333
34−void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
34+static void kvmppc_core_vcpu_load_44x(struct kvm_vcpu *vcpu, int cpu)
3535 {
3636 kvmppc_booke_vcpu_load(vcpu, cpu);
3737 kvmppc_44x_tlb_load(vcpu);
3838 }
3939
40−void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
40+static void kvmppc_core_vcpu_put_44x(struct kvm_vcpu *vcpu)
4141 {
4242 kvmppc_44x_tlb_put(vcpu);
4343 kvmppc_booke_vcpu_put(vcpu);
@@ -114,29 +114,32 @@ int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu, · 70 unmodified lines
114114 return 0;
115115 }
116116
117−void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
117+static int kvmppc_core_get_sregs_44x(struct kvm_vcpu *vcpu,
118+ struct kvm_sregs *sregs)
118119 {
119− kvmppc_get_sregs_ivor(vcpu, sregs);
120+ return kvmppc_get_sregs_ivor(vcpu, sregs);
120121 }
121122
122−int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
123+static int kvmppc_core_set_sregs_44x(struct kvm_vcpu *vcpu,
124+ struct kvm_sregs *sregs)
123125 {
124126 return kvmppc_set_sregs_ivor(vcpu, sregs);
125127 }
126128
127−int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
128− union kvmppc_one_reg *val)
129+static int kvmppc_get_one_reg_44x(struct kvm_vcpu *vcpu, u64 id,
130+ union kvmppc_one_reg *val)
129131 {
130132 return -EINVAL;
131133 }
132134
133−int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
134− union kvmppc_one_reg *val)
135+static int kvmppc_set_one_reg_44x(struct kvm_vcpu *vcpu, u64 id,
136+ union kvmppc_one_reg *val)
135137 {
136138 return -EINVAL;
137139 }
138140
139−struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
141+static struct kvm_vcpu *kvmppc_core_vcpu_create_44x(struct kvm *kvm,
142+ unsigned int id)
140143 {
141144 struct kvmppc_vcpu_44x *vcpu_44x;
142145 struct kvm_vcpu *vcpu;
@@ -167,7 +170,7 @@ out: · 24 unmodified lines
167170 return ERR_PTR(err);
168171 }
169172
170−void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
173+static void kvmppc_core_vcpu_free_44x(struct kvm_vcpu *vcpu)
171174 {
172175 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
173176
@@ -176,28 +179,53 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) · 2 unmodified lines
176179 kmem_cache_free(kvm_vcpu_cache, vcpu_44x);
177180 }
178181
179−int kvmppc_core_init_vm(struct kvm *kvm)
182+static int kvmppc_core_init_vm_44x(struct kvm *kvm)
180183 {
181184 return 0;
182185 }
183186
184−void kvmppc_core_destroy_vm(struct kvm *kvm)
187+static void kvmppc_core_destroy_vm_44x(struct kvm *kvm)
185188 {
186189 }
187190
191+static struct kvmppc_ops kvm_ops_44x = {
192+ .get_sregs = kvmppc_core_get_sregs_44x,
193+ .set_sregs = kvmppc_core_set_sregs_44x,
194+ .get_one_reg = kvmppc_get_one_reg_44x,
195+ .set_one_reg = kvmppc_set_one_reg_44x,
196+ .vcpu_load = kvmppc_core_vcpu_load_44x,
197+ .vcpu_put = kvmppc_core_vcpu_put_44x,
198+ .vcpu_create = kvmppc_core_vcpu_create_44x,
199+ .vcpu_free = kvmppc_core_vcpu_free_44x,
200+ .mmu_destroy = kvmppc_mmu_destroy_44x,
201+ .init_vm = kvmppc_core_init_vm_44x,
202+ .destroy_vm = kvmppc_core_destroy_vm_44x,
203+ .emulate_op = kvmppc_core_emulate_op_44x,
204+ .emulate_mtspr = kvmppc_core_emulate_mtspr_44x,
205+ .emulate_mfspr = kvmppc_core_emulate_mfspr_44x,
206+};
207+
188208 static int __init kvmppc_44x_init(void)
189209 {
190210 int r;
191211
192212 r = kvmppc_booke_init();
193213 if (r)
194− return r;
214+ goto err_out;
215+
216+ r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_44x), 0, THIS_MODULE);
217+ if (r)
218+ goto err_out;
219+ kvm_ops_44x.owner = THIS_MODULE;
220+ kvmppc_pr_ops = &kvm_ops_44x;
195221
196− return kvm_init(NULL, sizeof(struct kvmppc_vcpu_44x), 0, THIS_MODULE);
222+err_out:
223+ return r;
197224 }
198225
199226 static void __exit kvmppc_44x_exit(void)
200227 {
228+ kvmppc_pr_ops = NULL;
201229 kvmppc_booke_exit();
202230 }
203231
arch/powerpc/kvm/44x_emulate.c+4−4
@@ -91,8 +91,8 @@ static int emulate_mfdcr(struct kvm_vcpu *vcpu, int rt, int dcrn) · 90 unmodified lines
9191 return EMULATE_DONE;
9292 }
9393
94−int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
95− unsigned int inst, int *advance)
94+int kvmppc_core_emulate_op_44x(struct kvm_run *run, struct kvm_vcpu *vcpu,
95+ unsigned int inst, int *advance)
9696 {
9797 int emulated = EMULATE_DONE;
9898 int dcrn = get_dcrn(inst);
@@ -152,7 +152,7 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, · 53 unmodified lines
152152 return emulated;
153153 }
154154
155−int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
155+int kvmppc_core_emulate_mtspr_44x(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
156156 {
157157 int emulated = EMULATE_DONE;
158158
@@ -172,7 +172,7 @@ int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val) · 13 unmodified lines
172172 return emulated;
173173 }
174174
175−int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
175+int kvmppc_core_emulate_mfspr_44x(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
176176 {
177177 int emulated = EMULATE_DONE;
178178
arch/powerpc/kvm/44x_tlb.c+1−1
@@ -268,7 +268,7 @@ static void kvmppc_44x_shadow_release(struct kvmppc_vcpu_44x *vcpu_44x, · 267 unmodified lines
268268 trace_kvm_stlb_inval(stlb_index);
269269 }
270270
271−void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
271+void kvmppc_mmu_destroy_44x(struct kvm_vcpu *vcpu)
272272 {
273273 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
274274 int i;
arch/powerpc/kvm/Kconfig+22−6
@@ -35,17 +35,20 @@ config KVM_BOOK3S_64_HANDLER · 34 unmodified lines
3535 bool
3636 select KVM_BOOK3S_HANDLER
3737
38−config KVM_BOOK3S_PR
38+config KVM_BOOK3S_PR_POSSIBLE
3939 bool
4040 select KVM_MMIO
4141 select MMU_NOTIFIER
4242
43+config KVM_BOOK3S_HV_POSSIBLE
44+ bool
45+
4346 config KVM_BOOK3S_32
4447 tristate "KVM support for PowerPC book3s_32 processors"
4548 depends on PPC_BOOK3S_32 && !SMP && !PTE_64BIT
4649 select KVM
4750 select KVM_BOOK3S_32_HANDLER
48− select KVM_BOOK3S_PR
51+ select KVM_BOOK3S_PR_POSSIBLE
4952 ---help---
5053 Support running unmodified book3s_32 guest kernels
5154 in virtual machines on book3s_32 host processors.
@@ -60,6 +63,7 @@ config KVM_BOOK3S_64 · 8 unmodified lines
6063 depends on PPC_BOOK3S_64
6164 select KVM_BOOK3S_64_HANDLER
6265 select KVM
66+ select KVM_BOOK3S_PR_POSSIBLE if !KVM_BOOK3S_HV_POSSIBLE
6367 ---help---
6468 Support running unmodified book3s_64 and book3s_32 guest kernels
6569 in virtual machines on book3s_64 host processors.
@@ -70,8 +74,9 @@ config KVM_BOOK3S_64 · 4 unmodified lines
7074 If unsure, say N.
7175
7276 config KVM_BOOK3S_64_HV
73− bool "KVM support for POWER7 and PPC970 using hypervisor mode in host"
77+ tristate "KVM support for POWER7 and PPC970 using hypervisor mode in host"
7478 depends on KVM_BOOK3S_64
79+ select KVM_BOOK3S_HV_POSSIBLE
7580 select MMU_NOTIFIER
7681 select CMA
7782 ---help---
@@ -90,9 +95,20 @@ config KVM_BOOK3S_64_HV · 12 unmodified lines
9095 If unsure, say N.
9196
9297 config KVM_BOOK3S_64_PR
93− def_bool y
94− depends on KVM_BOOK3S_64 && !KVM_BOOK3S_64_HV
95− select KVM_BOOK3S_PR
98+ tristate "KVM support without using hypervisor mode in host"
99+ depends on KVM_BOOK3S_64
100+ select KVM_BOOK3S_PR_POSSIBLE
101+ ---help---
102+ Support running guest kernels in virtual machines on processors
103+ without using hypervisor mode in the host, by running the
104+ guest in user mode (problem state) and emulating all
105+ privileged instructions and registers.
106+
107+ This is not as fast as using hypervisor mode, but works on
108+ machines where hypervisor mode is not available or not usable,
109+ and can emulate processors that are different from the host
110+ processor, including emulating 32-bit processors on a 64-bit
111+ host.
96112
97113 config KVM_BOOKE_HV
98114 bool
arch/powerpc/kvm/Makefile+21−8
@@ -53,41 +53,51 @@ kvm-e500mc-objs := \ · 52 unmodified lines
5353 e500_emulate.o
5454 kvm-objs-$(CONFIG_KVM_E500MC) := $(kvm-e500mc-objs)
5555
56−kvm-book3s_64-objs-$(CONFIG_KVM_BOOK3S_64_PR) := \
57− $(KVM)/coalesced_mmio.o \
56+kvm-book3s_64-builtin-objs-$(CONFIG_KVM_BOOK3S_64_HANDLER) := \
57+ book3s_64_vio_hv.o
58+
59+kvm-pr-y := \
5860 fpu.o \
5961 book3s_paired_singles.o \
6062 book3s_pr.o \
6163 book3s_pr_papr.o \
62− book3s_64_vio_hv.o \
6364 book3s_emulate.o \
6465 book3s_interrupts.o \
6566 book3s_mmu_hpte.o \
6667 book3s_64_mmu_host.o \
6768 book3s_64_mmu.o \
6869 book3s_32_mmu.o
69−kvm-book3s_64-builtin-objs-$(CONFIG_KVM_BOOK3S_64_PR) := \
70+
71+ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
72+kvm-book3s_64-module-objs := \
73+ $(KVM)/coalesced_mmio.o
74+
75+kvm-book3s_64-builtin-objs-$(CONFIG_KVM_BOOK3S_64_HANDLER) += \
7076 book3s_rmhandlers.o
77+endif
7178
72−kvm-book3s_64-objs-$(CONFIG_KVM_BOOK3S_64_HV) := \
79+kvm-hv-y += \
7380 book3s_hv.o \
7481 book3s_hv_interrupts.o \
7582 book3s_64_mmu_hv.o
83+
7684 kvm-book3s_64-builtin-xics-objs-$(CONFIG_KVM_XICS) := \
7785 book3s_hv_rm_xics.o
78−kvm-book3s_64-builtin-objs-$(CONFIG_KVM_BOOK3S_64_HV) := \
86+
87+ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
88+kvm-book3s_64-builtin-objs-$(CONFIG_KVM_BOOK3S_64_HANDLER) += \
7989 book3s_hv_rmhandlers.o \
8090 book3s_hv_rm_mmu.o \
81− book3s_64_vio_hv.o \
8291 book3s_hv_ras.o \
8392 book3s_hv_builtin.o \
8493 book3s_hv_cma.o \
8594 $(kvm-book3s_64-builtin-xics-objs-y)
95+endif
8696
8797 kvm-book3s_64-objs-$(CONFIG_KVM_XICS) += \
8898 book3s_xics.o
8999
90−kvm-book3s_64-module-objs := \
100+kvm-book3s_64-module-objs += \
91101 $(KVM)/kvm_main.o \
92102 $(KVM)/eventfd.o \
93103 powerpc.o \
@@ -123,4 +133,7 @@ obj-$(CONFIG_KVM_E500MC) += kvm.o · 29 unmodified lines
123133 obj-$(CONFIG_KVM_BOOK3S_64) += kvm.o
124134 obj-$(CONFIG_KVM_BOOK3S_32) += kvm.o
125135
136+obj-$(CONFIG_KVM_BOOK3S_64_PR) += kvm-pr.o
137+obj-$(CONFIG_KVM_BOOK3S_64_HV) += kvm-hv.o
138+
126139 obj-y += $(kvm-book3s_64-builtin-objs-y)
arch/powerpc/kvm/book3s.c+246−11
@@ -34,6 +34,7 @@ · 33 unmodified lines
3434 #include <linux/vmalloc.h>
3535 #include <linux/highmem.h>
3636
37+#include "book3s.h"
3738 #include "trace.h"
3839
3940 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
@@ -69,6 +70,50 @@ void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu) · 29 unmodified lines
6970 {
7071 }
7172
73+static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
74+{
75+ if (!is_kvmppc_hv_enabled(vcpu->kvm))
76+ return to_book3s(vcpu)->hior;
77+ return 0;
78+}
79+
80+static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
81+ unsigned long pending_now, unsigned long old_pending)
82+{
83+ if (is_kvmppc_hv_enabled(vcpu->kvm))
84+ return;
85+ if (pending_now)
86+ vcpu->arch.shared->int_pending = 1;
87+ else if (old_pending)
88+ vcpu->arch.shared->int_pending = 0;
89+}
90+
91+static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
92+{
93+ ulong crit_raw;
94+ ulong crit_r1;
95+ bool crit;
96+
97+ if (is_kvmppc_hv_enabled(vcpu->kvm))
98+ return false;
99+
100+ crit_raw = vcpu->arch.shared->critical;
101+ crit_r1 = kvmppc_get_gpr(vcpu, 1);
102+
103+ /* Truncate crit indicators in 32 bit mode */
104+ if (!(vcpu->arch.shared->msr & MSR_SF)) {
105+ crit_raw &= 0xffffffff;
106+ crit_r1 &= 0xffffffff;
107+ }
108+
109+ /* Critical section when crit == r1 */
110+ crit = (crit_raw == crit_r1);
111+ /* ... and we're in supervisor mode */
112+ crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
113+
114+ return crit;
115+}
116+
72117 void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
73118 {
74119 vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu);
@@ -126,28 +171,32 @@ void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec) · 51 unmodified lines
126171 printk(KERN_INFO "Queueing interrupt %x\n", vec);
127172 #endif
128173 }
129−
174+EXPORT_SYMBOL_GPL(kvmppc_book3s_queue_irqprio);
130175
131176 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
132177 {
133178 /* might as well deliver this straight away */
134179 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_PROGRAM, flags);
135180 }
181+EXPORT_SYMBOL_GPL(kvmppc_core_queue_program);
136182
137183 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
138184 {
139185 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
140186 }
187+EXPORT_SYMBOL_GPL(kvmppc_core_queue_dec);
141188
142189 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
143190 {
144191 return test_bit(BOOK3S_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
145192 }
193+EXPORT_SYMBOL_GPL(kvmppc_core_pending_dec);
146194
147195 void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
148196 {
149197 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
150198 }
199+EXPORT_SYMBOL_GPL(kvmppc_core_dequeue_dec);
151200
152201 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
153202 struct kvm_interrupt *irq)
@@ -285,8 +334,10 @@ int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu) · 131 unmodified lines
285334
286335 return 0;
287336 }
337+EXPORT_SYMBOL_GPL(kvmppc_core_prepare_to_enter);
288338
289−pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
339+pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, bool writing,
340+ bool *writable)
290341 {
291342 ulong mp_pa = vcpu->arch.magic_page_pa;
292343
@@ -302,20 +353,23 @@ pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) · 9 unmodified lines
302353
303354 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
304355 get_page(pfn_to_page(pfn));
356+ if (writable)
357+ *writable = true;
305358 return pfn;
306359 }
307360
308− return gfn_to_pfn(vcpu->kvm, gfn);
361+ return gfn_to_pfn_prot(vcpu->kvm, gfn, writing, writable);
309362 }
363+EXPORT_SYMBOL_GPL(kvmppc_gfn_to_pfn);
310364
311365 static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
312− struct kvmppc_pte *pte)
366+ bool iswrite, struct kvmppc_pte *pte)
313367 {
314368 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
315369 int r;
316370
317371 if (relocated) {
318− r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
372+ r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data, iswrite);
319373 } else {
320374 pte->eaddr = eaddr;
321375 pte->raddr = eaddr & KVM_PAM;
@@ -361,7 +415,7 @@ int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, · 39 unmodified lines
361415
362416 vcpu->stat.st++;
363417
364− if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
418+ if (kvmppc_xlate(vcpu, *eaddr, data, true, &pte))
365419 return -ENOENT;
366420
367421 *eaddr = pte.raddr;
@@ -374,6 +428,7 @@ int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, · 6 unmodified lines
374428
375429 return EMULATE_DONE;
376430 }
431+EXPORT_SYMBOL_GPL(kvmppc_st);
377432
378433 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
379434 bool data)
@@ -383,7 +438,7 @@ int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, · 3 unmodified lines
383438
384439 vcpu->stat.ld++;
385440
386− if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
441+ if (kvmppc_xlate(vcpu, *eaddr, data, false, &pte))
387442 goto nopte;
388443
389444 *eaddr = pte.raddr;
@@ -404,6 +459,7 @@ nopte: · 14 unmodified lines
404459 mmio:
405460 return EMULATE_DO_MMIO;
406461 }
462+EXPORT_SYMBOL_GPL(kvmppc_ld);
407463
408464 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
409465 {
@@ -419,6 +475,18 @@ void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu) · 9 unmodified lines
419475 {
420476 }
421477
478+int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
479+ struct kvm_sregs *sregs)
480+{
481+ return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
482+}
483+
484+int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
485+ struct kvm_sregs *sregs)
486+{
487+ return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
488+}
489+
422490 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
423491 {
424492 int i;
@@ -495,8 +563,7 @@ int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 70 unmodified lines
495563 if (size > sizeof(val))
496564 return -EINVAL;
497565
498− r = kvmppc_get_one_reg(vcpu, reg->id, &val);
499−
566+ r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, reg->id, &val);
500567 if (r == -EINVAL) {
501568 r = 0;
502569 switch (reg->id) {
@@ -528,6 +595,9 @@ int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 25 unmodified lines
528595 }
529596 val = get_reg_val(reg->id, vcpu->arch.vscr.u[3]);
530597 break;
598+ case KVM_REG_PPC_VRSAVE:
599+ val = get_reg_val(reg->id, vcpu->arch.vrsave);
600+ break;
531601 #endif /* CONFIG_ALTIVEC */
532602 case KVM_REG_PPC_DEBUG_INST: {
533603 u32 opcode = INS_TW;
@@ -572,8 +642,7 @@ int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 38 unmodified lines
572642 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
573643 return -EFAULT;
574644
575− r = kvmppc_set_one_reg(vcpu, reg->id, &val);
576−
645+ r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, reg->id, &val);
577646 if (r == -EINVAL) {
578647 r = 0;
579648 switch (reg->id) {
@@ -605,6 +674,13 @@ int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 25 unmodified lines
605674 }
606675 vcpu->arch.vscr.u[3] = set_reg_val(reg->id, val);
607676 break;
677+ case KVM_REG_PPC_VRSAVE:
678+ if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
679+ r = -ENXIO;
680+ break;
681+ }
682+ vcpu->arch.vrsave = set_reg_val(reg->id, val);
683+ break;
608684 #endif /* CONFIG_ALTIVEC */
609685 #ifdef CONFIG_KVM_XICS
610686 case KVM_REG_PPC_ICP_STATE:
@@ -625,6 +701,27 @@ int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 14 unmodified lines
625701 return r;
626702 }
627703
704+void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
705+{
706+ vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
707+}
708+
709+void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
710+{
711+ vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
712+}
713+
714+void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
715+{
716+ vcpu->kvm->arch.kvm_ops->set_msr(vcpu, msr);
717+}
718+EXPORT_SYMBOL_GPL(kvmppc_set_msr);
719+
720+int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
721+{
722+ return vcpu->kvm->arch.kvm_ops->vcpu_run(kvm_run, vcpu);
723+}
724+
628725 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
629726 struct kvm_translation *tr)
630727 {
@@ -644,3 +741,141 @@ void kvmppc_decrementer_func(unsigned long data) · 13 unmodified lines
644741 kvmppc_core_queue_dec(vcpu);
645742 kvm_vcpu_kick(vcpu);
646743 }
744+
745+struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
746+{
747+ return kvm->arch.kvm_ops->vcpu_create(kvm, id);
748+}
749+
750+void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
751+{
752+ vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
753+}
754+
755+int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
756+{
757+ return vcpu->kvm->arch.kvm_ops->check_requests(vcpu);
758+}
759+
760+int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
761+{
762+ return kvm->arch.kvm_ops->get_dirty_log(kvm, log);
763+}
764+
765+void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
766+ struct kvm_memory_slot *dont)
767+{
768+ kvm->arch.kvm_ops->free_memslot(free, dont);
769+}
770+
771+int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
772+ unsigned long npages)
773+{
774+ return kvm->arch.kvm_ops->create_memslot(slot, npages);
775+}
776+
777+void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
778+{
779+ kvm->arch.kvm_ops->flush_memslot(kvm, memslot);
780+}
781+
782+int kvmppc_core_prepare_memory_region(struct kvm *kvm,
783+ struct kvm_memory_slot *memslot,
784+ struct kvm_userspace_memory_region *mem)
785+{
786+ return kvm->arch.kvm_ops->prepare_memory_region(kvm, memslot, mem);
787+}
788+
789+void kvmppc_core_commit_memory_region(struct kvm *kvm,
790+ struct kvm_userspace_memory_region *mem,
791+ const struct kvm_memory_slot *old)
792+{
793+ kvm->arch.kvm_ops->commit_memory_region(kvm, mem, old);
794+}
795+
796+int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
797+{
798+ return kvm->arch.kvm_ops->unmap_hva(kvm, hva);
799+}
800+EXPORT_SYMBOL_GPL(kvm_unmap_hva);
801+
802+int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
803+{
804+ return kvm->arch.kvm_ops->unmap_hva_range(kvm, start, end);
805+}
806+
807+int kvm_age_hva(struct kvm *kvm, unsigned long hva)
808+{
809+ return kvm->arch.kvm_ops->age_hva(kvm, hva);
810+}
811+
812+int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
813+{
814+ return kvm->arch.kvm_ops->test_age_hva(kvm, hva);
815+}
816+
817+void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
818+{
819+ kvm->arch.kvm_ops->set_spte_hva(kvm, hva, pte);
820+}
821+
822+void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
823+{
824+ vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
825+}
826+
827+int kvmppc_core_init_vm(struct kvm *kvm)
828+{
829+
830+#ifdef CONFIG_PPC64
831+ INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
832+ INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
833+#endif
834+
835+ return kvm->arch.kvm_ops->init_vm(kvm);
836+}
837+
838+void kvmppc_core_destroy_vm(struct kvm *kvm)
839+{
840+ kvm->arch.kvm_ops->destroy_vm(kvm);
841+
842+#ifdef CONFIG_PPC64
843+ kvmppc_rtas_tokens_free(kvm);
844+ WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
845+#endif
846+}
847+
848+int kvmppc_core_check_processor_compat(void)
849+{
850+ /*
851+ * We always return 0 for book3s. We check
852+ * for compatability while loading the HV
853+ * or PR module
854+ */
855+ return 0;
856+}
857+
858+static int kvmppc_book3s_init(void)
859+{
860+ int r;
861+
862+ r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
863+ if (r)
864+ return r;
865+#ifdef CONFIG_KVM_BOOK3S_32
866+ r = kvmppc_book3s_init_pr();
867+#endif
868+ return r;
869+
870+}
871+
872+static void kvmppc_book3s_exit(void)
873+{
874+#ifdef CONFIG_KVM_BOOK3S_32
875+ kvmppc_book3s_exit_pr();
876+#endif
877+ kvm_exit();
878+}
879+
880+module_init(kvmppc_book3s_init);
881+module_exit(kvmppc_book3s_exit);
arch/powerpc/kvm/book3s.h+34−0
@@ -0,0 +1,34 @@
1+/*
2+ * Copyright IBM Corporation, 2013
3+ * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
4+ *
5+ * This program is free software; you can redistribute it and/or
6+ * modify it under the terms of the GNU General Public License as
7+ * published by the Free Software Foundation; either version 2 of the
8+ * License or (at your optional) any later version of the license.
9+ *
10+ */
11+
12+#ifndef __POWERPC_KVM_BOOK3S_H__
13+#define __POWERPC_KVM_BOOK3S_H__
14+
15+extern void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
16+ struct kvm_memory_slot *memslot);
17+extern int kvm_unmap_hva_hv(struct kvm *kvm, unsigned long hva);
18+extern int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start,
19+ unsigned long end);
20+extern int kvm_age_hva_hv(struct kvm *kvm, unsigned long hva);
21+extern int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva);
22+extern void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte);
23+
24+extern void kvmppc_mmu_destroy_pr(struct kvm_vcpu *vcpu);
25+extern int kvmppc_core_emulate_op_pr(struct kvm_run *run, struct kvm_vcpu *vcpu,
26+ unsigned int inst, int *advance);
27+extern int kvmppc_core_emulate_mtspr_pr(struct kvm_vcpu *vcpu,
28+ int sprn, ulong spr_val);
29+extern int kvmppc_core_emulate_mfspr_pr(struct kvm_vcpu *vcpu,
30+ int sprn, ulong *spr_val);
31+extern int kvmppc_book3s_init_pr(void);
32+extern void kvmppc_book3s_exit_pr(void);
33+
34+#endif
arch/powerpc/kvm/book3s_32_mmu.c+42−31
@@ -84,7 +84,8 @@ static inline bool sr_nx(u32 sr_raw) · 83 unmodified lines
8484 }
8585
8686 static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
87− struct kvmppc_pte *pte, bool data);
87+ struct kvmppc_pte *pte, bool data,
88+ bool iswrite);
8889 static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
8990 u64 *vsid);
9091
@@ -99,7 +100,7 @@ static u64 kvmppc_mmu_book3s_32_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr, · 8 unmodified lines
99100 u64 vsid;
100101 struct kvmppc_pte pte;
101102
102− if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, &pte, data))
103+ if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, &pte, data, false))
103104 return pte.vpage;
104105
105106 kvmppc_mmu_book3s_32_esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
@@ -111,10 +112,11 @@ static void kvmppc_mmu_book3s_32_reset_msr(struct kvm_vcpu *vcpu) · 5 unmodified lines
111112 kvmppc_set_msr(vcpu, 0);
112113 }
113114
114−static hva_t kvmppc_mmu_book3s_32_get_pteg(struct kvmppc_vcpu_book3s *vcpu_book3s,
115+static hva_t kvmppc_mmu_book3s_32_get_pteg(struct kvm_vcpu *vcpu,
115116 u32 sre, gva_t eaddr,
116117 bool primary)
117118 {
119+ struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
118120 u32 page, hash, pteg, htabmask;
119121 hva_t r;
120122
@@ -132,7 +134,7 @@ static hva_t kvmppc_mmu_book3s_32_get_pteg(struct kvmppc_vcpu_book3s *vcpu_book3 · 11 unmodified lines
132134 kvmppc_get_pc(&vcpu_book3s->vcpu), eaddr, vcpu_book3s->sdr1, pteg,
133135 sr_vsid(sre));
134136
135− r = gfn_to_hva(vcpu_book3s->vcpu.kvm, pteg >> PAGE_SHIFT);
137+ r = gfn_to_hva(vcpu->kvm, pteg >> PAGE_SHIFT);
136138 if (kvm_is_error_hva(r))
137139 return r;
138140 return r | (pteg & ~PAGE_MASK);
@@ -145,7 +147,8 @@ static u32 kvmppc_mmu_book3s_32_get_ptem(u32 sre, gva_t eaddr, bool primary) · 6 unmodified lines
145147 }
146148
147149 static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
148− struct kvmppc_pte *pte, bool data)
150+ struct kvmppc_pte *pte, bool data,
151+ bool iswrite)
149152 {
150153 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
151154 struct kvmppc_bat *bat;
@@ -186,8 +189,7 @@ static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr, · 34 unmodified lines
186189 printk(KERN_INFO "BAT is not readable!\n");
187190 continue;
188191 }
189− if (!pte->may_write) {
190− /* let's treat r/o BATs as not-readable for now */
192+ if (iswrite && !pte->may_write) {
191193 dprintk_pte("BAT is read-only!\n");
192194 continue;
193195 }
@@ -201,9 +203,8 @@ static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr, · 7 unmodified lines
201203
202204 static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr,
203205 struct kvmppc_pte *pte, bool data,
204− bool primary)
206+ bool iswrite, bool primary)
205207 {
206− struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
207208 u32 sre;
208209 hva_t ptegp;
209210 u32 pteg[16];
@@ -218,7 +219,7 @@ static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr, · 8 unmodified lines
218219
219220 pte->vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
220221
221− ptegp = kvmppc_mmu_book3s_32_get_pteg(vcpu_book3s, sre, eaddr, primary);
222+ ptegp = kvmppc_mmu_book3s_32_get_pteg(vcpu, sre, eaddr, primary);
222223 if (kvm_is_error_hva(ptegp)) {
223224 printk(KERN_INFO "KVM: Invalid PTEG!\n");
224225 goto no_page_found;
@@ -258,9 +259,6 @@ static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr, · 33 unmodified lines
258259 break;
259260 }
260261
261− if ( !pte->may_read )
262− continue;
263−
264262 dprintk_pte("MMU: Found PTE -> %x %x - %x\n",
265263 pteg[i], pteg[i+1], pp);
266264 found = 1;
@@ -271,19 +269,23 @@ static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr, · 4 unmodified lines
271269 /* Update PTE C and A bits, so the guest's swapper knows we used the
272270 page */
273271 if (found) {
274− u32 oldpte = pteg[i+1];
275−
276− if (pte->may_read)
277− pteg[i+1] |= PTEG_FLAG_ACCESSED;
278− if (pte->may_write)
279− pteg[i+1] |= PTEG_FLAG_DIRTY;
280− else
281− dprintk_pte("KVM: Mapping read-only page!\n");
282−
283− /* Write back into the PTEG */
284− if (pteg[i+1] != oldpte)
285− copy_to_user((void __user *)ptegp, pteg, sizeof(pteg));
286−
272+ u32 pte_r = pteg[i+1];
273+ char __user *addr = (char __user *) &pteg[i+1];
274+
275+ /*
276+ * Use single-byte writes to update the HPTE, to
277+ * conform to what real hardware does.
278+ */
279+ if (pte->may_read && !(pte_r & PTEG_FLAG_ACCESSED)) {
280+ pte_r |= PTEG_FLAG_ACCESSED;
281+ put_user(pte_r >> 8, addr + 2);
282+ }
283+ if (iswrite && pte->may_write && !(pte_r & PTEG_FLAG_DIRTY)) {
284+ pte_r |= PTEG_FLAG_DIRTY;
285+ put_user(pte_r, addr + 3);
286+ }
287+ if (!pte->may_read || (iswrite && !pte->may_write))
288+ return -EPERM;
287289 return 0;
288290 }
289291
@@ -302,12 +304,14 @@ no_page_found: · 12 unmodified lines
302304 }
303305
304306 static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
305− struct kvmppc_pte *pte, bool data)
307+ struct kvmppc_pte *pte, bool data,
308+ bool iswrite)
306309 {
307310 int r;
308311 ulong mp_ea = vcpu->arch.magic_page_ea;
309312
310313 pte->eaddr = eaddr;
314+ pte->page_size = MMU_PAGE_4K;
311315
312316 /* Magic page override */
313317 if (unlikely(mp_ea) &&
@@ -323,11 +327,13 @@ static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, · 9 unmodified lines
323327 return 0;
324328 }
325329
326− r = kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, pte, data);
330+ r = kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, pte, data, iswrite);
327331 if (r < 0)
328− r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, true);
332+ r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte,
333+ data, iswrite, true);
329334 if (r < 0)
330− r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, false);
335+ r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte,
336+ data, iswrite, false);
331337
332338 return r;
333339 }
@@ -347,7 +353,12 @@ static void kvmppc_mmu_book3s_32_mtsrin(struct kvm_vcpu *vcpu, u32 srnum, · 13 unmodified lines
347353
348354 static void kvmppc_mmu_book3s_32_tlbie(struct kvm_vcpu *vcpu, ulong ea, bool large)
349355 {
350− kvmppc_mmu_pte_flush(vcpu, ea, 0x0FFFF000);
356+ int i;
357+ struct kvm_vcpu *v;
358+
359+ /* flush this VA on all cpus */
360+ kvm_for_each_vcpu(i, v, vcpu->kvm)
361+ kvmppc_mmu_pte_flush(v, ea, 0x0FFFF000);
351362 }
352363
353364 static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
arch/powerpc/kvm/book3s_32_mmu_host.c+12−4
@@ -138,7 +138,8 @@ static u32 *kvmppc_mmu_get_pteg(struct kvm_vcpu *vcpu, u32 vsid, u32 eaddr, · 137 unmodified lines
138138
139139 extern char etext[];
140140
141−int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
141+int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte,
142+ bool iswrite)
142143 {
143144 pfn_t hpaddr;
144145 u64 vpn;
@@ -152,9 +153,11 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte) · 7 unmodified lines
152153 bool evict = false;
153154 struct hpte_cache *pte;
154155 int r = 0;
156+ bool writable;
155157
156158 /* Get host physical address for gpa */
157− hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);
159+ hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT,
160+ iswrite, &writable);
158161 if (is_error_noslot_pfn(hpaddr)) {
159162 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n",
160163 orig_pte->eaddr);
@@ -204,7 +207,7 @@ next_pteg: · 43 unmodified lines
204207 (primary ? 0 : PTE_SEC);
205208 pteg1 = hpaddr | PTE_M | PTE_R | PTE_C;
206209
207− if (orig_pte->may_write) {
210+ if (orig_pte->may_write && writable) {
208211 pteg1 |= PP_RWRW;
209212 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
210213 } else {
@@ -259,6 +262,11 @@ out: · 48 unmodified lines
259262 return r;
260263 }
261264
265+void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
266+{
267+ kvmppc_mmu_pte_vflush(vcpu, pte->vpage, 0xfffffffffULL);
268+}
269+
262270 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
263271 {
264272 struct kvmppc_sid_map *map;
@@ -341,7 +349,7 @@ void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu) · 76 unmodified lines
341349 svcpu_put(svcpu);
342350 }
343351
344−void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
352+void kvmppc_mmu_destroy_pr(struct kvm_vcpu *vcpu)
345353 {
346354 int i;
347355
arch/powerpc/kvm/book3s_64_mmu.c+148−33
@@ -107,9 +107,20 @@ static u64 kvmppc_mmu_book3s_64_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr, · 106 unmodified lines
107107 return kvmppc_slb_calc_vpn(slb, eaddr);
108108 }
109109
110+static int mmu_pagesize(int mmu_pg)
111+{
112+ switch (mmu_pg) {
113+ case MMU_PAGE_64K:
114+ return 16;
115+ case MMU_PAGE_16M:
116+ return 24;
117+ }
118+ return 12;
119+}
120+
110121 static int kvmppc_mmu_book3s_64_get_pagesize(struct kvmppc_slb *slbe)
111122 {
112− return slbe->large ? 24 : 12;
123+ return mmu_pagesize(slbe->base_page_size);
113124 }
114125
115126 static u32 kvmppc_mmu_book3s_64_get_page(struct kvmppc_slb *slbe, gva_t eaddr)
@@ -119,11 +130,11 @@ static u32 kvmppc_mmu_book3s_64_get_page(struct kvmppc_slb *slbe, gva_t eaddr) · 3 unmodified lines
119130 return ((eaddr & kvmppc_slb_offset_mask(slbe)) >> p);
120131 }
121132
122−static hva_t kvmppc_mmu_book3s_64_get_pteg(
123− struct kvmppc_vcpu_book3s *vcpu_book3s,
133+static hva_t kvmppc_mmu_book3s_64_get_pteg(struct kvm_vcpu *vcpu,
124134 struct kvmppc_slb *slbe, gva_t eaddr,
125135 bool second)
126136 {
137+ struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
127138 u64 hash, pteg, htabsize;
128139 u32 ssize;
129140 hva_t r;
@@ -148,10 +159,10 @@ static hva_t kvmppc_mmu_book3s_64_get_pteg( · 18 unmodified lines
148159
149160 /* When running a PAPR guest, SDR1 contains a HVA address instead
150161 of a GPA */
151− if (vcpu_book3s->vcpu.arch.papr_enabled)
162+ if (vcpu->arch.papr_enabled)
152163 r = pteg;
153164 else
154− r = gfn_to_hva(vcpu_book3s->vcpu.kvm, pteg >> PAGE_SHIFT);
165+ r = gfn_to_hva(vcpu->kvm, pteg >> PAGE_SHIFT);
155166
156167 if (kvm_is_error_hva(r))
157168 return r;
@@ -166,18 +177,38 @@ static u64 kvmppc_mmu_book3s_64_get_avpn(struct kvmppc_slb *slbe, gva_t eaddr) · 8 unmodified lines
166177 avpn = kvmppc_mmu_book3s_64_get_page(slbe, eaddr);
167178 avpn |= slbe->vsid << (kvmppc_slb_sid_shift(slbe) - p);
168179
169− if (p < 24)
170− avpn >>= ((80 - p) - 56) - 8;
180+ if (p < 16)
181+ avpn >>= ((80 - p) - 56) - 8; /* 16 - p */
171182 else
172− avpn <<= 8;
183+ avpn <<= p - 16;
173184
174185 return avpn;
175186 }
176187
188+/*
189+ * Return page size encoded in the second word of a HPTE, or
190+ * -1 for an invalid encoding for the base page size indicated by
191+ * the SLB entry. This doesn't handle mixed pagesize segments yet.
192+ */
193+static int decode_pagesize(struct kvmppc_slb *slbe, u64 r)
194+{
195+ switch (slbe->base_page_size) {
196+ case MMU_PAGE_64K:
197+ if ((r & 0xf000) == 0x1000)
198+ return MMU_PAGE_64K;
199+ break;
200+ case MMU_PAGE_16M:
201+ if ((r & 0xff000) == 0)
202+ return MMU_PAGE_16M;
203+ break;
204+ }
205+ return -1;
206+}
207+
177208 static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
178− struct kvmppc_pte *gpte, bool data)
209+ struct kvmppc_pte *gpte, bool data,
210+ bool iswrite)
179211 {
180− struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
181212 struct kvmppc_slb *slbe;
182213 hva_t ptegp;
183214 u64 pteg[16];
@@ -189,6 +220,7 @@ static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, · 5 unmodified lines
189220 u8 pp, key = 0;
190221 bool found = false;
191222 bool second = false;
223+ int pgsize;
192224 ulong mp_ea = vcpu->arch.magic_page_ea;
193225
194226 /* Magic page override */
@@ -202,6 +234,7 @@ static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, · 7 unmodified lines
202234 gpte->may_execute = true;
203235 gpte->may_read = true;
204236 gpte->may_write = true;
237+ gpte->page_size = MMU_PAGE_4K;
205238
206239 return 0;
207240 }
@@ -222,8 +255,12 @@ static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, · 14 unmodified lines
222255 v_mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_LARGE | HPTE_V_VALID |
223256 HPTE_V_SECONDARY;
224257
258+ pgsize = slbe->large ? MMU_PAGE_16M : MMU_PAGE_4K;
259+
260+ mutex_lock(&vcpu->kvm->arch.hpt_mutex);
261+
225262 do_second:
226− ptegp = kvmppc_mmu_book3s_64_get_pteg(vcpu_book3s, slbe, eaddr, second);
263+ ptegp = kvmppc_mmu_book3s_64_get_pteg(vcpu, slbe, eaddr, second);
227264 if (kvm_is_error_hva(ptegp))
228265 goto no_page_found;
229266
@@ -240,6 +277,13 @@ do_second: · 10 unmodified lines
240277 for (i=0; i<16; i+=2) {
241278 /* Check all relevant fields of 1st dword */
242279 if ((pteg[i] & v_mask) == v_val) {
280+ /* If large page bit is set, check pgsize encoding */
281+ if (slbe->large &&
282+ (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE)) {
283+ pgsize = decode_pagesize(slbe, pteg[i+1]);
284+ if (pgsize < 0)
285+ continue;
286+ }
243287 found = true;
244288 break;
245289 }
@@ -256,13 +300,15 @@ do_second: · 10 unmodified lines
256300 v = pteg[i];
257301 r = pteg[i+1];
258302 pp = (r & HPTE_R_PP) | key;
259− eaddr_mask = 0xFFF;
303+ if (r & HPTE_R_PP0)
304+ pp |= 8;
260305
261306 gpte->eaddr = eaddr;
262307 gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu, eaddr, data);
263− if (slbe->large)
264− eaddr_mask = 0xFFFFFF;
308+
309+ eaddr_mask = (1ull << mmu_pagesize(pgsize)) - 1;
265310 gpte->raddr = (r & HPTE_R_RPN & ~eaddr_mask) | (eaddr & eaddr_mask);
311+ gpte->page_size = pgsize;
266312 gpte->may_execute = ((r & HPTE_R_N) ? false : true);
267313 gpte->may_read = false;
268314 gpte->may_write = false;
@@ -277,6 +323,7 @@ do_second: · 8 unmodified lines
277323 case 3:
278324 case 5:
279325 case 7:
326+ case 10:
280327 gpte->may_read = true;
281328 break;
282329 }
@@ -287,30 +334,37 @@ do_second: · 4 unmodified lines
287334
288335 /* Update PTE R and C bits, so the guest's swapper knows we used the
289336 * page */
290− if (gpte->may_read) {
291− /* Set the accessed flag */
337+ if (gpte->may_read && !(r & HPTE_R_R)) {
338+ /*
339+ * Set the accessed flag.
340+ * We have to write this back with a single byte write
341+ * because another vcpu may be accessing this on
342+ * non-PAPR platforms such as mac99, and this is
343+ * what real hardware does.
344+ */
345+ char __user *addr = (char __user *) &pteg[i+1];
292346 r |= HPTE_R_R;
347+ put_user(r >> 8, addr + 6);
293348 }
294− if (data && gpte->may_write) {
295− /* Set the dirty flag -- XXX even if not writing */
349+ if (iswrite && gpte->may_write && !(r & HPTE_R_C)) {
350+ /* Set the dirty flag */
351+ /* Use a single byte write */
352+ char __user *addr = (char __user *) &pteg[i+1];
296353 r |= HPTE_R_C;
354+ put_user(r, addr + 7);
297355 }
298356
299− /* Write back into the PTEG */
300− if (pteg[i+1] != r) {
301− pteg[i+1] = r;
302− copy_to_user((void __user *)ptegp, pteg, sizeof(pteg));
303− }
357+ mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
304358
305− if (!gpte->may_read)
359+ if (!gpte->may_read || (iswrite && !gpte->may_write))
306360 return -EPERM;
307361 return 0;
308362
309363 no_page_found:
364+ mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
310365 return -ENOENT;
311366
312367 no_seg_found:
313−
314368 dprintk("KVM MMU: Trigger segment fault\n");
315369 return -EINVAL;
316370 }
@@ -345,6 +399,21 @@ static void kvmppc_mmu_book3s_64_slbmte(struct kvm_vcpu *vcpu, u64 rs, u64 rb) · 28 unmodified lines
345399 slbe->nx = (rs & SLB_VSID_N) ? 1 : 0;
346400 slbe->class = (rs & SLB_VSID_C) ? 1 : 0;
347401
402+ slbe->base_page_size = MMU_PAGE_4K;
403+ if (slbe->large) {
404+ if (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE) {
405+ switch (rs & SLB_VSID_LP) {
406+ case SLB_VSID_LP_00:
407+ slbe->base_page_size = MMU_PAGE_16M;
408+ break;
409+ case SLB_VSID_LP_01:
410+ slbe->base_page_size = MMU_PAGE_64K;
411+ break;
412+ }
413+ } else
414+ slbe->base_page_size = MMU_PAGE_16M;
415+ }
416+
348417 slbe->orige = rb & (ESID_MASK | SLB_ESID_V);
349418 slbe->origv = rs;
350419
@@ -460,14 +529,45 @@ static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu *vcpu, ulong va, · 109 unmodified lines
460529 bool large)
461530 {
462531 u64 mask = 0xFFFFFFFFFULL;
532+ long i;
533+ struct kvm_vcpu *v;
463534
464535 dprintk("KVM MMU: tlbie(0x%lx)\n", va);
465536
466− if (large)
467− mask = 0xFFFFFF000ULL;
468− kvmppc_mmu_pte_vflush(vcpu, va >> 12, mask);
537+ /*
538+ * The tlbie instruction changed behaviour starting with
539+ * POWER6. POWER6 and later don't have the large page flag
540+ * in the instruction but in the RB value, along with bits
541+ * indicating page and segment sizes.
542+ */
543+ if (vcpu->arch.hflags & BOOK3S_HFLAG_NEW_TLBIE) {
544+ /* POWER6 or later */
545+ if (va & 1) { /* L bit */
546+ if ((va & 0xf000) == 0x1000)
547+ mask = 0xFFFFFFFF0ULL; /* 64k page */
548+ else
549+ mask = 0xFFFFFF000ULL; /* 16M page */
550+ }
551+ } else {
552+ /* older processors, e.g. PPC970 */
553+ if (large)
554+ mask = 0xFFFFFF000ULL;
555+ }
556+ /* flush this VA on all vcpus */
557+ kvm_for_each_vcpu(i, v, vcpu->kvm)
558+ kvmppc_mmu_pte_vflush(v, va >> 12, mask);
469559 }
470560
561+#ifdef CONFIG_PPC_64K_PAGES
562+static int segment_contains_magic_page(struct kvm_vcpu *vcpu, ulong esid)
563+{
564+ ulong mp_ea = vcpu->arch.magic_page_ea;
565+
566+ return mp_ea && !(vcpu->arch.shared->msr & MSR_PR) &&
567+ (mp_ea >> SID_SHIFT) == esid;
568+}
569+#endif
570+
471571 static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
472572 u64 *vsid)
473573 {
@@ -475,11 +575,13 @@ static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid, · 1 unmodified line
475575 struct kvmppc_slb *slb;
476576 u64 gvsid = esid;
477577 ulong mp_ea = vcpu->arch.magic_page_ea;
578+ int pagesize = MMU_PAGE_64K;
478579
479580 if (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
480581 slb = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
481582 if (slb) {
482583 gvsid = slb->vsid;
584+ pagesize = slb->base_page_size;
483585 if (slb->tb) {
484586 gvsid <<= SID_SHIFT_1T - SID_SHIFT;
485587 gvsid |= esid & ((1ul << (SID_SHIFT_1T - SID_SHIFT)) - 1);
@@ -490,28 +592,41 @@ static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid, · 4 unmodified lines
490592
491593 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
492594 case 0:
493− *vsid = VSID_REAL | esid;
595+ gvsid = VSID_REAL | esid;
494596 break;
495597 case MSR_IR:
496− *vsid = VSID_REAL_IR | gvsid;
598+ gvsid |= VSID_REAL_IR;
497599 break;
498600 case MSR_DR:
499− *vsid = VSID_REAL_DR | gvsid;
601+ gvsid |= VSID_REAL_DR;
500602 break;
501603 case MSR_DR|MSR_IR:
502604 if (!slb)
503605 goto no_slb;
504606
505− *vsid = gvsid;
506607 break;
507608 default:
508609 BUG();
509610 break;
510611 }
511612
613+#ifdef CONFIG_PPC_64K_PAGES
614+ /*
615+ * Mark this as a 64k segment if the host is using
616+ * 64k pages, the host MMU supports 64k pages and
617+ * the guest segment page size is >= 64k,
618+ * but not if this segment contains the magic page.
619+ */
620+ if (pagesize >= MMU_PAGE_64K &&
621+ mmu_psize_defs[MMU_PAGE_64K].shift &&
622+ !segment_contains_magic_page(vcpu, esid))
623+ gvsid |= VSID_64K;
624+#endif
625+
512626 if (vcpu->arch.shared->msr & MSR_PR)
513− *vsid |= VSID_PR;
627+ gvsid |= VSID_PR;
514628
629+ *vsid = gvsid;
515630 return 0;
516631
517632 no_slb:
arch/powerpc/kvm/book3s_64_mmu_host.c+79−27
@@ -27,14 +27,14 @@ · 26 unmodified lines
2727 #include <asm/machdep.h>
2828 #include <asm/mmu_context.h>
2929 #include <asm/hw_irq.h>
30−#include "trace.h"
30+#include "trace_pr.h"
3131
3232 #define PTE_SIZE 12
3333
3434 void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
3535 {
3636 ppc_md.hpte_invalidate(pte->slot, pte->host_vpn,
37− MMU_PAGE_4K, MMU_PAGE_4K, MMU_SEGSIZE_256M,
37+ pte->pagesize, pte->pagesize, MMU_SEGSIZE_256M,
3838 false);
3939 }
4040
@@ -78,7 +78,8 @@ static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid) · 37 unmodified lines
7878 return NULL;
7979 }
8080
81−int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
81+int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte,
82+ bool iswrite)
8283 {
8384 unsigned long vpn;
8485 pfn_t hpaddr;
@@ -90,16 +91,26 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte) · 5 unmodified lines
9091 int attempt = 0;
9192 struct kvmppc_sid_map *map;
9293 int r = 0;
94+ int hpsize = MMU_PAGE_4K;
95+ bool writable;
96+ unsigned long mmu_seq;
97+ struct kvm *kvm = vcpu->kvm;
98+ struct hpte_cache *cpte;
99+ unsigned long gfn = orig_pte->raddr >> PAGE_SHIFT;
100+ unsigned long pfn;
101+
102+ /* used to check for invalidations in progress */
103+ mmu_seq = kvm->mmu_notifier_seq;
104+ smp_rmb();
93105
94106 /* Get host physical address for gpa */
95− hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);
96− if (is_error_noslot_pfn(hpaddr)) {
97− printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
107+ pfn = kvmppc_gfn_to_pfn(vcpu, gfn, iswrite, &writable);
108+ if (is_error_noslot_pfn(pfn)) {
109+ printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", gfn);
98110 r = -EINVAL;
99111 goto out;
100112 }
101− hpaddr <<= PAGE_SHIFT;
102− hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
113+ hpaddr = pfn << PAGE_SHIFT;
103114
104115 /* and write the mapping ea -> hpa into the pt */
105116 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
@@ -117,20 +128,39 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte) · 11 unmodified lines
117128 goto out;
118129 }
119130
120− vsid = map->host_vsid;
121− vpn = hpt_vpn(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
131+ vpn = hpt_vpn(orig_pte->eaddr, map->host_vsid, MMU_SEGSIZE_256M);
122132
123− if (!orig_pte->may_write)
124− rflags |= HPTE_R_PP;
125− else
126− mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
133+ kvm_set_pfn_accessed(pfn);
134+ if (!orig_pte->may_write || !writable)
135+ rflags |= PP_RXRX;
136+ else {
137+ mark_page_dirty(vcpu->kvm, gfn);
138+ kvm_set_pfn_dirty(pfn);
139+ }
127140
128141 if (!orig_pte->may_execute)
129142 rflags |= HPTE_R_N;
130143 else
131− kvmppc_mmu_flush_icache(hpaddr >> PAGE_SHIFT);
144+ kvmppc_mmu_flush_icache(pfn);
145+
146+ /*
147+ * Use 64K pages if possible; otherwise, on 64K page kernels,
148+ * we need to transfer 4 more bits from guest real to host real addr.
149+ */
150+ if (vsid & VSID_64K)
151+ hpsize = MMU_PAGE_64K;
152+ else
153+ hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
154+
155+ hash = hpt_hash(vpn, mmu_psize_defs[hpsize].shift, MMU_SEGSIZE_256M);
132156
133− hash = hpt_hash(vpn, PTE_SIZE, MMU_SEGSIZE_256M);
157+ cpte = kvmppc_mmu_hpte_cache_next(vcpu);
158+
159+ spin_lock(&kvm->mmu_lock);
160+ if (!cpte || mmu_notifier_retry(kvm, mmu_seq)) {
161+ r = -EAGAIN;
162+ goto out_unlock;
163+ }
134164
135165 map_again:
136166 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
@@ -139,11 +169,11 @@ map_again: · 2 unmodified lines
139169 if (attempt > 1)
140170 if (ppc_md.hpte_remove(hpteg) < 0) {
141171 r = -1;
142− goto out;
172+ goto out_unlock;
143173 }
144174
145175 ret = ppc_md.hpte_insert(hpteg, vpn, hpaddr, rflags, vflags,
146− MMU_PAGE_4K, MMU_PAGE_4K, MMU_SEGSIZE_256M);
176+ hpsize, hpsize, MMU_SEGSIZE_256M);
147177
148178 if (ret < 0) {
149179 /* If we couldn't map a primary PTE, try a secondary */
@@ -152,8 +182,6 @@ map_again: · 2 unmodified lines
152182 attempt++;
153183 goto map_again;
154184 } else {
155− struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu);
156−
157185 trace_kvm_book3s_64_mmu_map(rflags, hpteg,
158186 vpn, hpaddr, orig_pte);
159187
@@ -164,19 +192,37 @@ map_again: · 4 unmodified lines
164192 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
165193 }
166194
167− pte->slot = hpteg + (ret & 7);
168− pte->host_vpn = vpn;
169− pte->pte = *orig_pte;
170− pte->pfn = hpaddr >> PAGE_SHIFT;
195+ cpte->slot = hpteg + (ret & 7);
196+ cpte->host_vpn = vpn;
197+ cpte->pte = *orig_pte;
198+ cpte->pfn = pfn;
199+ cpte->pagesize = hpsize;
171200
172− kvmppc_mmu_hpte_cache_map(vcpu, pte);
201+ kvmppc_mmu_hpte_cache_map(vcpu, cpte);
202+ cpte = NULL;
173203 }
174− kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT);
204+
205+out_unlock:
206+ spin_unlock(&kvm->mmu_lock);
207+ kvm_release_pfn_clean(pfn);
208+ if (cpte)
209+ kvmppc_mmu_hpte_cache_free(cpte);
175210
176211 out:
177212 return r;
178213 }
179214
215+void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
216+{
217+ u64 mask = 0xfffffffffULL;
218+ u64 vsid;
219+
220+ vcpu->arch.mmu.esid_to_vsid(vcpu, pte->eaddr >> SID_SHIFT, &vsid);
221+ if (vsid & VSID_64K)
222+ mask = 0xffffffff0ULL;
223+ kvmppc_mmu_pte_vflush(vcpu, pte->vpage, mask);
224+}
225+
180226 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
181227 {
182228 struct kvmppc_sid_map *map;
@@ -291,6 +337,12 @@ int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr) · 108 unmodified lines
291337 slb_vsid &= ~SLB_VSID_KP;
292338 slb_esid |= slb_index;
293339
340+#ifdef CONFIG_PPC_64K_PAGES
341+ /* Set host segment base page size to 64K if possible */
342+ if (gvsid & VSID_64K)
343+ slb_vsid |= mmu_psize_defs[MMU_PAGE_64K].sllp;
344+#endif
345+
294346 svcpu->slb[slb_index].esid = slb_esid;
295347 svcpu->slb[slb_index].vsid = slb_vsid;
296348
@@ -326,7 +378,7 @@ void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu) · 29 unmodified lines
326378 svcpu_put(svcpu);
327379 }
328380
329−void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
381+void kvmppc_mmu_destroy_pr(struct kvm_vcpu *vcpu)
330382 {
331383 kvmppc_mmu_hpte_destroy(vcpu);
332384 __destroy_context(to_book3s(vcpu)->context_id[0]);
arch/powerpc/kvm/book3s_64_mmu_hv.c+10−14
@@ -260,10 +260,6 @@ int kvmppc_mmu_hv_init(void) · 259 unmodified lines
260260 return 0;
261261 }
262262
263−void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
264−{
265−}
266−
267263 static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
268264 {
269265 kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
@@ -451,7 +447,7 @@ static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r, · 181 unmodified lines
451447 }
452448
453449 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
454− struct kvmppc_pte *gpte, bool data)
450+ struct kvmppc_pte *gpte, bool data, bool iswrite)
455451 {
456452 struct kvm *kvm = vcpu->kvm;
457453 struct kvmppc_slb *slbe;
@@ -906,21 +902,22 @@ static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp, · 448 unmodified lines
906902 return 0;
907903 }
908904
909−int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
905+int kvm_unmap_hva_hv(struct kvm *kvm, unsigned long hva)
910906 {
911907 if (kvm->arch.using_mmu_notifiers)
912908 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
913909 return 0;
914910 }
915911
916−int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
912+int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start, unsigned long end)
917913 {
918914 if (kvm->arch.using_mmu_notifiers)
919915 kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp);
920916 return 0;
921917 }
922918
923−void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
919+void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
920+ struct kvm_memory_slot *memslot)
924921 {
925922 unsigned long *rmapp;
926923 unsigned long gfn;
@@ -994,7 +991,7 @@ static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, · 67 unmodified lines
994991 return ret;
995992 }
996993
997−int kvm_age_hva(struct kvm *kvm, unsigned long hva)
994+int kvm_age_hva_hv(struct kvm *kvm, unsigned long hva)
998995 {
999996 if (!kvm->arch.using_mmu_notifiers)
1000997 return 0;
@@ -1032,14 +1029,14 @@ static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp, · 31 unmodified lines
10321029 return ret;
10331030 }
10341031
1035−int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1032+int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva)
10361033 {
10371034 if (!kvm->arch.using_mmu_notifiers)
10381035 return 0;
10391036 return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
10401037 }
10411038
1042−void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1039+void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte)
10431040 {
10441041 if (!kvm->arch.using_mmu_notifiers)
10451042 return;
@@ -1512,9 +1509,8 @@ static ssize_t kvm_htab_write(struct file *file, const char __user *buf, · 466 unmodified lines
15121509
15131510 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
15141511 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1515− lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1516− lpcr |= senc << (LPCR_VRMASD_SH - 4);
1517− kvm->arch.lpcr = lpcr;
1512+ lpcr = senc << (LPCR_VRMASD_SH - 4);
1513+ kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
15181514 rma_setup = 1;
15191515 }
15201516 ++i;
arch/powerpc/kvm/book3s_64_vio_hv.c+1−0
@@ -74,3 +74,4 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn, · 73 unmodified lines
7474 /* Didn't find the liobn, punt it to userspace */
7575 return H_TOO_HARD;
7676 }
77+EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
arch/powerpc/kvm/book3s_emulate.c+7−11
@@ -86,8 +86,8 @@ static bool spr_allowed(struct kvm_vcpu *vcpu, enum priv_level level) · 85 unmodified lines
8686 return true;
8787 }
8888
89−int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
90− unsigned int inst, int *advance)
89+int kvmppc_core_emulate_op_pr(struct kvm_run *run, struct kvm_vcpu *vcpu,
90+ unsigned int inst, int *advance)
9191 {
9292 int emulated = EMULATE_DONE;
9393 int rt = get_rt(inst);
@@ -172,7 +172,7 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, · 78 unmodified lines
172172 vcpu->arch.mmu.tlbie(vcpu, addr, large);
173173 break;
174174 }
175−#ifdef CONFIG_KVM_BOOK3S_64_PR
175+#ifdef CONFIG_PPC_BOOK3S_64
176176 case OP_31_XOP_FAKE_SC1:
177177 {
178178 /* SC 1 papr hypercalls */
@@ -267,12 +267,9 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, · 88 unmodified lines
267267
268268 r = kvmppc_st(vcpu, &addr, 32, zeros, true);
269269 if ((r == -ENOENT) || (r == -EPERM)) {
270− struct kvmppc_book3s_shadow_vcpu *svcpu;
271−
272− svcpu = svcpu_get(vcpu);
273270 *advance = 0;
274271 vcpu->arch.shared->dar = vaddr;
275− svcpu->fault_dar = vaddr;
272+ vcpu->arch.fault_dar = vaddr;
276273
277274 dsisr = DSISR_ISSTORE;
278275 if (r == -ENOENT)
@@ -281,8 +278,7 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, · 2 unmodified lines
281278 dsisr |= DSISR_PROTFAULT;
282279
283280 vcpu->arch.shared->dsisr = dsisr;
284− svcpu->fault_dsisr = dsisr;
285− svcpu_put(svcpu);
281+ vcpu->arch.fault_dsisr = dsisr;
286282
287283 kvmppc_book3s_queue_irqprio(vcpu,
288284 BOOK3S_INTERRUPT_DATA_STORAGE);
@@ -349,7 +345,7 @@ static struct kvmppc_bat *kvmppc_find_bat(struct kvm_vcpu *vcpu, int sprn) · 60 unmodified lines
349345 return bat;
350346 }
351347
352−int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
348+int kvmppc_core_emulate_mtspr_pr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
353349 {
354350 int emulated = EMULATE_DONE;
355351
@@ -472,7 +468,7 @@ unprivileged: · 116 unmodified lines
472468 return emulated;
473469 }
474470
475−int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
471+int kvmppc_core_emulate_mfspr_pr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
476472 {
477473 int emulated = EMULATE_DONE;
478474
arch/powerpc/kvm/book3s_exports.c+3−2
@@ -20,9 +20,10 @@ · 19 unmodified lines
2020 #include <linux/export.h>
2121 #include <asm/kvm_book3s.h>
2222
23−#ifdef CONFIG_KVM_BOOK3S_64_HV
23+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
2424 EXPORT_SYMBOL_GPL(kvmppc_hv_entry_trampoline);
25−#else
25+#endif
26+#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
2627 EXPORT_SYMBOL_GPL(kvmppc_entry_trampoline);
2728 EXPORT_SYMBOL_GPL(kvmppc_load_up_fpu);
2829 #ifdef CONFIG_ALTIVEC
arch/powerpc/kvm/book3s_hv.c+309−80
@@ -52,6 +52,9 @@ · 51 unmodified lines
5252 #include <linux/vmalloc.h>
5353 #include <linux/highmem.h>
5454 #include <linux/hugetlb.h>
55+#include <linux/module.h>
56+
57+#include "book3s.h"
5558
5659 /* #define EXIT_DEBUG */
5760 /* #define EXIT_DEBUG_SIMPLE */
@@ -66,7 +69,7 @@ · 8 unmodified lines
6669 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
6770 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
6871
69−void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
72+static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
7073 {
7174 int me;
7275 int cpu = vcpu->cpu;
@@ -125,7 +128,7 @@ void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu) · 52 unmodified lines
125128 * purely defensive; they should never fail.)
126129 */
127130
128−void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
131+static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
129132 {
130133 struct kvmppc_vcore *vc = vcpu->arch.vcore;
131134
@@ -143,7 +146,7 @@ void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · 11 unmodified lines
143146 spin_unlock(&vcpu->arch.tbacct_lock);
144147 }
145148
146−void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
149+static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
147150 {
148151 struct kvmppc_vcore *vc = vcpu->arch.vcore;
149152
@@ -155,17 +158,46 @@ void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu) · 5 unmodified lines
155158 spin_unlock(&vcpu->arch.tbacct_lock);
156159 }
157160
158−void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
161+static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
159162 {
160163 vcpu->arch.shregs.msr = msr;
161164 kvmppc_end_cede(vcpu);
162165 }
163166
164−void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
167+void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
165168 {
166169 vcpu->arch.pvr = pvr;
167170 }
168171
172+int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
173+{
174+ unsigned long pcr = 0;
175+ struct kvmppc_vcore *vc = vcpu->arch.vcore;
176+
177+ if (arch_compat) {
178+ if (!cpu_has_feature(CPU_FTR_ARCH_206))
179+ return -EINVAL; /* 970 has no compat mode support */
180+
181+ switch (arch_compat) {
182+ case PVR_ARCH_205:
183+ pcr = PCR_ARCH_205;
184+ break;
185+ case PVR_ARCH_206:
186+ case PVR_ARCH_206p:
187+ break;
188+ default:
189+ return -EINVAL;
190+ }
191+ }
192+
193+ spin_lock(&vc->lock);
194+ vc->arch_compat = arch_compat;
195+ vc->pcr = pcr;
196+ spin_unlock(&vc->lock);
197+
198+ return 0;
199+}
200+
169201 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
170202 {
171203 int r;
@@ -195,7 +227,7 @@ void kvmppc_dump_regs(struct kvm_vcpu *vcpu) · 23 unmodified lines
195227 pr_err(" ESID = %.16llx VSID = %.16llx\n",
196228 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
197229 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
198− vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
230+ vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
199231 vcpu->arch.last_inst);
200232 }
201233
@@ -489,7 +521,7 @@ static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, · 287 unmodified lines
489521 memset(dt, 0, sizeof(struct dtl_entry));
490522 dt->dispatch_reason = 7;
491523 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
492− dt->timebase = now;
524+ dt->timebase = now + vc->tb_offset;
493525 dt->enqueue_to_dispatch_time = stolen;
494526 dt->srr0 = kvmppc_get_pc(vcpu);
495527 dt->srr1 = vcpu->arch.shregs.msr;
@@ -538,6 +570,15 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) · 42 unmodified lines
538570 }
539571 break;
540572 case H_CONFER:
573+ target = kvmppc_get_gpr(vcpu, 4);
574+ if (target == -1)
575+ break;
576+ tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
577+ if (!tvcpu) {
578+ ret = H_PARAMETER;
579+ break;
580+ }
581+ kvm_vcpu_yield_to(tvcpu);
541582 break;
542583 case H_REGISTER_VPA:
543584 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
@@ -576,8 +617,8 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) · 32 unmodified lines
576617 return RESUME_GUEST;
577618 }
578619
579−static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
580− struct task_struct *tsk)
620+static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
621+ struct task_struct *tsk)
581622 {
582623 int r = RESUME_HOST;
583624
@@ -671,16 +712,16 @@ static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, · 87 unmodified lines
671712 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
672713 vcpu->arch.trap, kvmppc_get_pc(vcpu),
673714 vcpu->arch.shregs.msr);
715+ run->hw.hardware_exit_reason = vcpu->arch.trap;
674716 r = RESUME_HOST;
675− BUG();
676717 break;
677718 }
678719
679720 return r;
680721 }
681722
682−int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
683− struct kvm_sregs *sregs)
723+static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
724+ struct kvm_sregs *sregs)
684725 {
685726 int i;
686727
@@ -694,12 +735,12 @@ int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, · 7 unmodified lines
694735 return 0;
695736 }
696737
697−int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
698− struct kvm_sregs *sregs)
738+static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
739+ struct kvm_sregs *sregs)
699740 {
700741 int i, j;
701742
702− kvmppc_set_pvr(vcpu, sregs->pvr);
743+ kvmppc_set_pvr_hv(vcpu, sregs->pvr);
703744
704745 j = 0;
705746 for (i = 0; i < vcpu->arch.slb_nr; i++) {
@@ -714,7 +755,23 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · 8 unmodified lines
714755 return 0;
715756 }
716757
717−int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
758+static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
759+{
760+ struct kvmppc_vcore *vc = vcpu->arch.vcore;
761+ u64 mask;
762+
763+ spin_lock(&vc->lock);
764+ /*
765+ * Userspace can only modify DPFD (default prefetch depth),
766+ * ILE (interrupt little-endian) and TC (translation control).
767+ */
768+ mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
769+ vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
770+ spin_unlock(&vc->lock);
771+}
772+
773+static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
774+ union kvmppc_one_reg *val)
718775 {
719776 int r = 0;
720777 long int i;
@@ -749,6 +806,12 @@ int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val) · 28 unmodified lines
749806 i = id - KVM_REG_PPC_PMC1;
750807 *val = get_reg_val(id, vcpu->arch.pmc[i]);
751808 break;
809+ case KVM_REG_PPC_SIAR:
810+ *val = get_reg_val(id, vcpu->arch.siar);
811+ break;
812+ case KVM_REG_PPC_SDAR:
813+ *val = get_reg_val(id, vcpu->arch.sdar);
814+ break;
752815 #ifdef CONFIG_VSX
753816 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
754817 if (cpu_has_feature(CPU_FTR_VSX)) {
@@ -787,6 +850,18 @@ int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val) · 32 unmodified lines
787850 val->vpaval.length = vcpu->arch.dtl.len;
788851 spin_unlock(&vcpu->arch.vpa_update_lock);
789852 break;
853+ case KVM_REG_PPC_TB_OFFSET:
854+ *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
855+ break;
856+ case KVM_REG_PPC_LPCR:
857+ *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
858+ break;
859+ case KVM_REG_PPC_PPR:
860+ *val = get_reg_val(id, vcpu->arch.ppr);
861+ break;
862+ case KVM_REG_PPC_ARCH_COMPAT:
863+ *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
864+ break;
790865 default:
791866 r = -EINVAL;
792867 break;
@@ -795,7 +870,8 @@ int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val) · 2 unmodified lines
795870 return r;
796871 }
797872
798−int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
873+static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
874+ union kvmppc_one_reg *val)
799875 {
800876 int r = 0;
801877 long int i;
@@ -833,6 +909,12 @@ int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val) · 31 unmodified lines
833909 i = id - KVM_REG_PPC_PMC1;
834910 vcpu->arch.pmc[i] = set_reg_val(id, *val);
835911 break;
912+ case KVM_REG_PPC_SIAR:
913+ vcpu->arch.siar = set_reg_val(id, *val);
914+ break;
915+ case KVM_REG_PPC_SDAR:
916+ vcpu->arch.sdar = set_reg_val(id, *val);
917+ break;
836918 #ifdef CONFIG_VSX
837919 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
838920 if (cpu_has_feature(CPU_FTR_VSX)) {
@@ -880,6 +962,20 @@ int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val) · 41 unmodified lines
880962 len -= len % sizeof(struct dtl_entry);
881963 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
882964 break;
965+ case KVM_REG_PPC_TB_OFFSET:
966+ /* round up to multiple of 2^24 */
967+ vcpu->arch.vcore->tb_offset =
968+ ALIGN(set_reg_val(id, *val), 1UL << 24);
969+ break;
970+ case KVM_REG_PPC_LPCR:
971+ kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
972+ break;
973+ case KVM_REG_PPC_PPR:
974+ vcpu->arch.ppr = set_reg_val(id, *val);
975+ break;
976+ case KVM_REG_PPC_ARCH_COMPAT:
977+ r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
978+ break;
883979 default:
884980 r = -EINVAL;
885981 break;
@@ -888,14 +984,8 @@ int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val) · 2 unmodified lines
888984 return r;
889985 }
890986
891−int kvmppc_core_check_processor_compat(void)
892−{
893− if (cpu_has_feature(CPU_FTR_HVMODE))
894− return 0;
895− return -EIO;
896−}
897−
898−struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
987+static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
988+ unsigned int id)
899989 {
900990 struct kvm_vcpu *vcpu;
901991 int err = -EINVAL;
@@ -919,8 +1009,7 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) · 17 unmodified lines
9191009 vcpu->arch.mmcr[0] = MMCR0_FC;
9201010 vcpu->arch.ctrl = CTRL_RUNLATCH;
9211011 /* default to host PVR, since we can't spoof it */
922− vcpu->arch.pvr = mfspr(SPRN_PVR);
923− kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1012+ kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
9241013 spin_lock_init(&vcpu->arch.vpa_update_lock);
9251014 spin_lock_init(&vcpu->arch.tbacct_lock);
9261015 vcpu->arch.busy_preempt = TB_NIL;
@@ -940,6 +1029,7 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) · 13 unmodified lines
9401029 spin_lock_init(&vcore->lock);
9411030 init_waitqueue_head(&vcore->wq);
9421031 vcore->preempt_tb = TB_NIL;
1032+ vcore->lpcr = kvm->arch.lpcr;
9431033 }
9441034 kvm->arch.vcores[core] = vcore;
9451035 kvm->arch.online_vcores++;
@@ -972,7 +1062,7 @@ static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa) · 26 unmodified lines
9721062 vpa->dirty);
9731063 }
9741064
975−void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1065+static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
9761066 {
9771067 spin_lock(&vcpu->arch.vpa_update_lock);
9781068 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
@@ -983,6 +1073,12 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) · 4 unmodified lines
9831073 kmem_cache_free(kvm_vcpu_cache, vcpu);
9841074 }
9851075
1076+static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1077+{
1078+ /* Indicate we want to get back into the guest */
1079+ return 1;
1080+}
1081+
9861082 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
9871083 {
9881084 unsigned long dec_nsec, now;
@@ -1264,8 +1360,8 @@ static void kvmppc_run_core(struct kvmppc_vcore *vc) · 275 unmodified lines
12641360
12651361 ret = RESUME_GUEST;
12661362 if (vcpu->arch.trap)
1267− ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1268− vcpu->arch.run_task);
1363+ ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1364+ vcpu->arch.run_task);
12691365
12701366 vcpu->arch.ret = ret;
12711367 vcpu->arch.trap = 0;
@@ -1424,7 +1520,7 @@ static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) · 152 unmodified lines
14241520 return vcpu->arch.ret;
14251521 }
14261522
1427−int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1523+static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
14281524 {
14291525 int r;
14301526 int srcu_idx;
@@ -1546,7 +1642,8 @@ static const struct file_operations kvm_rma_fops = { · 115 unmodified lines
15461642 .release = kvm_rma_release,
15471643 };
15481644
1549−long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1645+static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1646+ struct kvm_allocate_rma *ret)
15501647 {
15511648 long fd;
15521649 struct kvm_rma_info *ri;
@@ -1592,7 +1689,8 @@ static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps, · 39 unmodified lines
15921689 (*sps)++;
15931690 }
15941691
1595−int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1692+static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1693+ struct kvm_ppc_smmu_info *info)
15961694 {
15971695 struct kvm_ppc_one_seg_page_size *sps;
15981696
@@ -1613,7 +1711,8 @@ int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info) · 14 unmodified lines
16131711 /*
16141712 * Get (and clear) the dirty memory log for a memory slot.
16151713 */
1616−int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1714+static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1715+ struct kvm_dirty_log *log)
16171716 {
16181717 struct kvm_memory_slot *memslot;
16191718 int r;
@@ -1667,8 +1766,8 @@ static void unpin_slot(struct kvm_memory_slot *memslot) · 47 unmodified lines
16671766 }
16681767 }
16691768
1670−void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1671− struct kvm_memory_slot *dont)
1769+static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1770+ struct kvm_memory_slot *dont)
16721771 {
16731772 if (!dont || free->arch.rmap != dont->arch.rmap) {
16741773 vfree(free->arch.rmap);
@@ -1681,8 +1780,8 @@ void kvmppc_core_free_memslot(struct kvm_memory_slot *free, · 6 unmodified lines
16811780 }
16821781 }
16831782
1684−int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1685− unsigned long npages)
1783+static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1784+ unsigned long npages)
16861785 {
16871786 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
16881787 if (!slot->arch.rmap)
@@ -1692,9 +1791,9 @@ int kvmppc_core_create_memslot(struct kvm_memory_slot *slot, · 3 unmodified lines
16921791 return 0;
16931792 }
16941793
1695−int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1696− struct kvm_memory_slot *memslot,
1697− struct kvm_userspace_memory_region *mem)
1794+static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1795+ struct kvm_memory_slot *memslot,
1796+ struct kvm_userspace_memory_region *mem)
16981797 {
16991798 unsigned long *phys;
17001799
@@ -1710,9 +1809,9 @@ int kvmppc_core_prepare_memory_region(struct kvm *kvm, · 9 unmodified lines
17101809 return 0;
17111810 }
17121811
1713−void kvmppc_core_commit_memory_region(struct kvm *kvm,
1714− struct kvm_userspace_memory_region *mem,
1715− const struct kvm_memory_slot *old)
1812+static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1813+ struct kvm_userspace_memory_region *mem,
1814+ const struct kvm_memory_slot *old)
17161815 {
17171816 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
17181817 struct kvm_memory_slot *memslot;
@@ -1729,6 +1828,37 @@ void kvmppc_core_commit_memory_region(struct kvm *kvm, · 10 unmodified lines
17291828 }
17301829 }
17311830
1831+/*
1832+ * Update LPCR values in kvm->arch and in vcores.
1833+ * Caller must hold kvm->lock.
1834+ */
1835+void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1836+{
1837+ long int i;
1838+ u32 cores_done = 0;
1839+
1840+ if ((kvm->arch.lpcr & mask) == lpcr)
1841+ return;
1842+
1843+ kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
1844+
1845+ for (i = 0; i < KVM_MAX_VCORES; ++i) {
1846+ struct kvmppc_vcore *vc = kvm->arch.vcores[i];
1847+ if (!vc)
1848+ continue;
1849+ spin_lock(&vc->lock);
1850+ vc->lpcr = (vc->lpcr & ~mask) | lpcr;
1851+ spin_unlock(&vc->lock);
1852+ if (++cores_done >= kvm->arch.online_vcores)
1853+ break;
1854+ }
1855+}
1856+
1857+static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
1858+{
1859+ return;
1860+}
1861+
17321862 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
17331863 {
17341864 int err = 0;
@@ -1737,7 +1867,8 @@ static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) · 2 unmodified lines
17371867 unsigned long hva;
17381868 struct kvm_memory_slot *memslot;
17391869 struct vm_area_struct *vma;
1740− unsigned long lpcr, senc;
1870+ unsigned long lpcr = 0, senc;
1871+ unsigned long lpcr_mask = 0;
17411872 unsigned long psize, porder;
17421873 unsigned long rma_size;
17431874 unsigned long rmls;
@@ -1802,9 +1933,9 @@ static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) · 58 unmodified lines
18021933 senc = slb_pgsize_encoding(psize);
18031934 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
18041935 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1805− lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1806− lpcr |= senc << (LPCR_VRMASD_SH - 4);
1807− kvm->arch.lpcr = lpcr;
1936+ lpcr_mask = LPCR_VRMASD;
1937+ /* the -4 is to account for senc values starting at 0x10 */
1938+ lpcr = senc << (LPCR_VRMASD_SH - 4);
18081939
18091940 /* Create HPTEs in the hash page table for the VRMA */
18101941 kvmppc_map_vrma(vcpu, memslot, porder);
@@ -1825,23 +1956,21 @@ static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) · 14 unmodified lines
18251956 kvm->arch.rma = ri;
18261957
18271958 /* Update LPCR and RMOR */
1828− lpcr = kvm->arch.lpcr;
18291959 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
18301960 /* PPC970; insert RMLS value (split field) in HID4 */
1831− lpcr &= ~((1ul << HID4_RMLS0_SH) |
1832− (3ul << HID4_RMLS2_SH));
1833− lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1961+ lpcr_mask = (1ul << HID4_RMLS0_SH) |
1962+ (3ul << HID4_RMLS2_SH) | HID4_RMOR;
1963+ lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
18341964 ((rmls & 3) << HID4_RMLS2_SH);
18351965 /* RMOR is also in HID4 */
18361966 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
18371967 << HID4_RMOR_SH;
18381968 } else {
18391969 /* POWER7 */
1840− lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1841− lpcr |= rmls << LPCR_RMLS_SH;
1970+ lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
1971+ lpcr = rmls << LPCR_RMLS_SH;
18421972 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
18431973 }
1844− kvm->arch.lpcr = lpcr;
18451974 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
18461975 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
18471976
@@ -1860,6 +1989,8 @@ static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) · 12 unmodified lines
18601989 }
18611990 }
18621991
1992+ kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
1993+
18631994 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
18641995 smp_wmb();
18651996 kvm->arch.rma_setup_done = 1;
@@ -1875,7 +2006,7 @@ static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) · 9 unmodified lines
18752006 goto out_srcu;
18762007 }
18772008
1878−int kvmppc_core_init_vm(struct kvm *kvm)
2009+static int kvmppc_core_init_vm_hv(struct kvm *kvm)
18792010 {
18802011 unsigned long lpcr, lpid;
18812012
@@ -1893,9 +2024,6 @@ int kvmppc_core_init_vm(struct kvm *kvm) · 11 unmodified lines
18932024 */
18942025 cpumask_setall(&kvm->arch.need_tlb_flush);
18952026
1896− INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
1897− INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
1898−
18992027 kvm->arch.rma = NULL;
19002028
19012029 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
@@ -1931,61 +2059,162 @@ int kvmppc_core_init_vm(struct kvm *kvm) · 29 unmodified lines
19312059 return 0;
19322060 }
19332061
1934−void kvmppc_core_destroy_vm(struct kvm *kvm)
2062+static void kvmppc_free_vcores(struct kvm *kvm)
2063+{
2064+ long int i;
2065+
2066+ for (i = 0; i < KVM_MAX_VCORES; ++i)
2067+ kfree(kvm->arch.vcores[i]);
2068+ kvm->arch.online_vcores = 0;
2069+}
2070+
2071+static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
19352072 {
19362073 uninhibit_secondary_onlining();
19372074
2075+ kvmppc_free_vcores(kvm);
19382076 if (kvm->arch.rma) {
19392077 kvm_release_rma(kvm->arch.rma);
19402078 kvm->arch.rma = NULL;
19412079 }
19422080
1943− kvmppc_rtas_tokens_free(kvm);
1944−
19452081 kvmppc_free_hpt(kvm);
1946− WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
19472082 }
19482083
1949−/* These are stubs for now */
1950−void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
2084+/* We don't need to emulate any privileged instructions or dcbz */
2085+static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2086+ unsigned int inst, int *advance)
19512087 {
2088+ return EMULATE_FAIL;
19522089 }
19532090
1954−/* We don't need to emulate any privileged instructions or dcbz */
1955−int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1956− unsigned int inst, int *advance)
2091+static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2092+ ulong spr_val)
19572093 {
19582094 return EMULATE_FAIL;
19592095 }
19602096
1961−int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
2097+static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2098+ ulong *spr_val)
19622099 {
19632100 return EMULATE_FAIL;
19642101 }
19652102
1966−int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
2103+static int kvmppc_core_check_processor_compat_hv(void)
19672104 {
1968− return EMULATE_FAIL;
2105+ if (!cpu_has_feature(CPU_FTR_HVMODE))
2106+ return -EIO;
2107+ return 0;
19692108 }
19702109
1971−static int kvmppc_book3s_hv_init(void)
2110+static long kvm_arch_vm_ioctl_hv(struct file *filp,
2111+ unsigned int ioctl, unsigned long arg)
19722112 {
1973− int r;
2113+ struct kvm *kvm __maybe_unused = filp->private_data;
2114+ void __user *argp = (void __user *)arg;
2115+ long r;
19742116
1975− r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
2117+ switch (ioctl) {
19762118
1977− if (r)
2119+ case KVM_ALLOCATE_RMA: {
2120+ struct kvm_allocate_rma rma;
2121+ struct kvm *kvm = filp->private_data;
2122+
2123+ r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2124+ if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2125+ r = -EFAULT;
2126+ break;
2127+ }
2128+
2129+ case KVM_PPC_ALLOCATE_HTAB: {
2130+ u32 htab_order;
2131+
2132+ r = -EFAULT;
2133+ if (get_user(htab_order, (u32 __user *)argp))
2134+ break;
2135+ r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2136+ if (r)
2137+ break;
2138+ r = -EFAULT;
2139+ if (put_user(htab_order, (u32 __user *)argp))
2140+ break;
2141+ r = 0;
2142+ break;
2143+ }
2144+
2145+ case KVM_PPC_GET_HTAB_FD: {
2146+ struct kvm_get_htab_fd ghf;
2147+
2148+ r = -EFAULT;
2149+ if (copy_from_user(&ghf, argp, sizeof(ghf)))
2150+ break;
2151+ r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2152+ break;
2153+ }
2154+
2155+ default:
2156+ r = -ENOTTY;
2157+ }
2158+
2159+ return r;
2160+}
2161+
2162+static struct kvmppc_ops kvm_ops_hv = {
2163+ .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2164+ .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2165+ .get_one_reg = kvmppc_get_one_reg_hv,
2166+ .set_one_reg = kvmppc_set_one_reg_hv,
2167+ .vcpu_load = kvmppc_core_vcpu_load_hv,
2168+ .vcpu_put = kvmppc_core_vcpu_put_hv,
2169+ .set_msr = kvmppc_set_msr_hv,
2170+ .vcpu_run = kvmppc_vcpu_run_hv,
2171+ .vcpu_create = kvmppc_core_vcpu_create_hv,
2172+ .vcpu_free = kvmppc_core_vcpu_free_hv,
2173+ .check_requests = kvmppc_core_check_requests_hv,
2174+ .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2175+ .flush_memslot = kvmppc_core_flush_memslot_hv,
2176+ .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2177+ .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2178+ .unmap_hva = kvm_unmap_hva_hv,
2179+ .unmap_hva_range = kvm_unmap_hva_range_hv,
2180+ .age_hva = kvm_age_hva_hv,
2181+ .test_age_hva = kvm_test_age_hva_hv,
2182+ .set_spte_hva = kvm_set_spte_hva_hv,
2183+ .mmu_destroy = kvmppc_mmu_destroy_hv,
2184+ .free_memslot = kvmppc_core_free_memslot_hv,
2185+ .create_memslot = kvmppc_core_create_memslot_hv,
2186+ .init_vm = kvmppc_core_init_vm_hv,
2187+ .destroy_vm = kvmppc_core_destroy_vm_hv,
2188+ .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2189+ .emulate_op = kvmppc_core_emulate_op_hv,
2190+ .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2191+ .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2192+ .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2193+ .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2194+};
2195+
2196+static int kvmppc_book3s_init_hv(void)
2197+{
2198+ int r;
2199+ /*
2200+ * FIXME!! Do we need to check on all cpus ?
2201+ */
2202+ r = kvmppc_core_check_processor_compat_hv();
2203+ if (r < 0)
19782204 return r;
19792205
1980− r = kvmppc_mmu_hv_init();
2206+ kvm_ops_hv.owner = THIS_MODULE;
2207+ kvmppc_hv_ops = &kvm_ops_hv;
19812208
2209+ r = kvmppc_mmu_hv_init();
19822210 return r;
19832211 }
19842212
1985−static void kvmppc_book3s_hv_exit(void)
2213+static void kvmppc_book3s_exit_hv(void)
19862214 {
1987− kvm_exit();
2215+ kvmppc_hv_ops = NULL;
19882216 }
19892217
1990−module_init(kvmppc_book3s_hv_init);
1991−module_exit(kvmppc_book3s_hv_exit);
2218+module_init(kvmppc_book3s_init_hv);
2219+module_exit(kvmppc_book3s_exit_hv);
2220+MODULE_LICENSE("GPL");
arch/powerpc/kvm/book3s_hv_interrupts.S+0−3
@@ -158,9 +158,6 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201) · 157 unmodified lines
158158 * Interrupts are enabled again at this point.
159159 */
160160
161−.global kvmppc_handler_highmem
162−kvmppc_handler_highmem:
163−
164161 /*
165162 * Register usage at this point:
166163 *
arch/powerpc/kvm/book3s_hv_rmhandlers.S+356−262
@@ -33,30 +33,6 @@ · 32 unmodified lines
3333 #error Need to fix lppaca and SLB shadow accesses in little endian mode
3434 #endif
3535
36−/*****************************************************************************
37− * *
38− * Real Mode handlers that need to be in the linear mapping *
39− * *
40− ****************************************************************************/
41−
42− .globl kvmppc_skip_interrupt
43−kvmppc_skip_interrupt:
44− mfspr r13,SPRN_SRR0
45− addi r13,r13,4
46− mtspr SPRN_SRR0,r13
47− GET_SCRATCH0(r13)
48− rfid
49− b .
50−
51− .globl kvmppc_skip_Hinterrupt
52−kvmppc_skip_Hinterrupt:
53− mfspr r13,SPRN_HSRR0
54− addi r13,r13,4
55− mtspr SPRN_HSRR0,r13
56− GET_SCRATCH0(r13)
57− hrfid
58− b .
59−
6036 /*
6137 * Call kvmppc_hv_entry in real mode.
6238 * Must be called with interrupts hard-disabled.
@@ -66,8 +42,11 @@ kvmppc_skip_Hinterrupt: · 3 unmodified lines
6642 * LR = return address to continue at after eventually re-enabling MMU
6743 */
6844 _GLOBAL(kvmppc_hv_entry_trampoline)
45+ mflr r0
46+ std r0, PPC_LR_STKOFF(r1)
47+ stdu r1, -112(r1)
6948 mfmsr r10
70− LOAD_REG_ADDR(r5, kvmppc_hv_entry)
49+ LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
7150 li r0,MSR_RI
7251 andc r0,r10,r0
7352 li r6,MSR_IR | MSR_DR
@@ -77,11 +56,103 @@ _GLOBAL(kvmppc_hv_entry_trampoline) · 3 unmodified lines
7756 mtsrr1 r6
7857 RFI
7958
80−/******************************************************************************
81− * *
82− * Entry code *
83− * *
84− *****************************************************************************/
59+kvmppc_call_hv_entry:
60+ bl kvmppc_hv_entry
61+
62+ /* Back from guest - restore host state and return to caller */
63+
64+ /* Restore host DABR and DABRX */
65+ ld r5,HSTATE_DABR(r13)
66+ li r6,7
67+ mtspr SPRN_DABR,r5
68+ mtspr SPRN_DABRX,r6
69+
70+ /* Restore SPRG3 */
71+ ld r3,PACA_SPRG3(r13)
72+ mtspr SPRN_SPRG3,r3
73+
74+ /*
75+ * Reload DEC. HDEC interrupts were disabled when
76+ * we reloaded the host's LPCR value.
77+ */
78+ ld r3, HSTATE_DECEXP(r13)
79+ mftb r4
80+ subf r4, r4, r3
81+ mtspr SPRN_DEC, r4
82+
83+ /* Reload the host's PMU registers */
84+ ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
85+ lbz r4, LPPACA_PMCINUSE(r3)
86+ cmpwi r4, 0
87+ beq 23f /* skip if not */
88+ lwz r3, HSTATE_PMC(r13)
89+ lwz r4, HSTATE_PMC + 4(r13)
90+ lwz r5, HSTATE_PMC + 8(r13)
91+ lwz r6, HSTATE_PMC + 12(r13)
92+ lwz r8, HSTATE_PMC + 16(r13)
93+ lwz r9, HSTATE_PMC + 20(r13)
94+BEGIN_FTR_SECTION
95+ lwz r10, HSTATE_PMC + 24(r13)
96+ lwz r11, HSTATE_PMC + 28(r13)
97+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
98+ mtspr SPRN_PMC1, r3
99+ mtspr SPRN_PMC2, r4
100+ mtspr SPRN_PMC3, r5
101+ mtspr SPRN_PMC4, r6
102+ mtspr SPRN_PMC5, r8
103+ mtspr SPRN_PMC6, r9
104+BEGIN_FTR_SECTION
105+ mtspr SPRN_PMC7, r10
106+ mtspr SPRN_PMC8, r11
107+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
108+ ld r3, HSTATE_MMCR(r13)
109+ ld r4, HSTATE_MMCR + 8(r13)
110+ ld r5, HSTATE_MMCR + 16(r13)
111+ mtspr SPRN_MMCR1, r4
112+ mtspr SPRN_MMCRA, r5
113+ mtspr SPRN_MMCR0, r3
114+ isync
115+23:
116+
117+ /*
118+ * For external and machine check interrupts, we need
119+ * to call the Linux handler to process the interrupt.
120+ * We do that by jumping to absolute address 0x500 for
121+ * external interrupts, or the machine_check_fwnmi label
122+ * for machine checks (since firmware might have patched
123+ * the vector area at 0x200). The [h]rfid at the end of the
124+ * handler will return to the book3s_hv_interrupts.S code.
125+ * For other interrupts we do the rfid to get back
126+ * to the book3s_hv_interrupts.S code here.
127+ */
128+ ld r8, 112+PPC_LR_STKOFF(r1)
129+ addi r1, r1, 112
130+ ld r7, HSTATE_HOST_MSR(r13)
131+
132+ cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
133+ cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
134+BEGIN_FTR_SECTION
135+ beq 11f
136+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
137+
138+ /* RFI into the highmem handler, or branch to interrupt handler */
139+ mfmsr r6
140+ li r0, MSR_RI
141+ andc r6, r6, r0
142+ mtmsrd r6, 1 /* Clear RI in MSR */
143+ mtsrr0 r8
144+ mtsrr1 r7
145+ beqa 0x500 /* external interrupt (PPC970) */
146+ beq cr1, 13f /* machine check */
147+ RFI
148+
149+ /* On POWER7, we have external interrupts set to use HSRR0/1 */
150+11: mtspr SPRN_HSRR0, r8
151+ mtspr SPRN_HSRR1, r7
152+ ba 0x500
153+
154+13: b machine_check_fwnmi
155+
85156
86157 /*
87158 * We come in here when wakened from nap mode on a secondary hw thread.
@@ -137,7 +208,7 @@ kvm_start_guest: · 49 unmodified lines
137208 cmpdi r4,0
138209 /* if we have no vcpu to run, go back to sleep */
139210 beq kvm_no_guest
140− b kvmppc_hv_entry
211+ b 30f
141212
142213 27: /* XXX should handle hypervisor maintenance interrupts etc. here */
143214 b kvm_no_guest
@@ -147,6 +218,57 @@ kvm_start_guest: · 3 unmodified lines
147218 stw r8,HSTATE_SAVED_XIRR(r13)
148219 b kvm_no_guest
149220
221+30: bl kvmppc_hv_entry
222+
223+ /* Back from the guest, go back to nap */
224+ /* Clear our vcpu pointer so we don't come back in early */
225+ li r0, 0
226+ std r0, HSTATE_KVM_VCPU(r13)
227+ lwsync
228+ /* Clear any pending IPI - we're an offline thread */
229+ ld r5, HSTATE_XICS_PHYS(r13)
230+ li r7, XICS_XIRR
231+ lwzcix r3, r5, r7 /* ack any pending interrupt */
232+ rlwinm. r0, r3, 0, 0xffffff /* any pending? */
233+ beq 37f
234+ sync
235+ li r0, 0xff
236+ li r6, XICS_MFRR
237+ stbcix r0, r5, r6 /* clear the IPI */
238+ stwcix r3, r5, r7 /* EOI it */
239+37: sync
240+
241+ /* increment the nap count and then go to nap mode */
242+ ld r4, HSTATE_KVM_VCORE(r13)
243+ addi r4, r4, VCORE_NAP_COUNT
244+ lwsync /* make previous updates visible */
245+51: lwarx r3, 0, r4
246+ addi r3, r3, 1
247+ stwcx. r3, 0, r4
248+ bne 51b
249+
250+kvm_no_guest:
251+ li r0, KVM_HWTHREAD_IN_NAP
252+ stb r0, HSTATE_HWTHREAD_STATE(r13)
253+ li r3, LPCR_PECE0
254+ mfspr r4, SPRN_LPCR
255+ rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
256+ mtspr SPRN_LPCR, r4
257+ isync
258+ std r0, HSTATE_SCRATCH0(r13)
259+ ptesync
260+ ld r0, HSTATE_SCRATCH0(r13)
261+1: cmpd r0, r0
262+ bne 1b
263+ nap
264+ b .
265+
266+/******************************************************************************
267+ * *
268+ * Entry code *
269+ * *
270+ *****************************************************************************/
271+
150272 .global kvmppc_hv_entry
151273 kvmppc_hv_entry:
152274
@@ -159,7 +281,8 @@ kvmppc_hv_entry: · 6 unmodified lines
159281 * all other volatile GPRS = free
160282 */
161283 mflr r0
162− std r0, HSTATE_VMHANDLER(r13)
284+ std r0, PPC_LR_STKOFF(r1)
285+ stdu r1, -112(r1)
163286
164287 /* Set partition DABR */
165288 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
@@ -200,8 +323,12 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201) · 34 unmodified lines
200323 ld r3, VCPU_MMCR(r4)
201324 ld r5, VCPU_MMCR + 8(r4)
202325 ld r6, VCPU_MMCR + 16(r4)
326+ ld r7, VCPU_SIAR(r4)
327+ ld r8, VCPU_SDAR(r4)
203328 mtspr SPRN_MMCR1, r5
204329 mtspr SPRN_MMCRA, r6
330+ mtspr SPRN_SIAR, r7
331+ mtspr SPRN_SDAR, r8
205332 mtspr SPRN_MMCR0, r3
206333 isync
207334
@@ -254,22 +381,15 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206) · 46 unmodified lines
254381 /* Save R1 in the PACA */
255382 std r1, HSTATE_HOST_R1(r13)
256383
257− /* Increment yield count if they have a VPA */
258− ld r3, VCPU_VPA(r4)
259− cmpdi r3, 0
260− beq 25f
261− lwz r5, LPPACA_YIELDCOUNT(r3)
262− addi r5, r5, 1
263− stw r5, LPPACA_YIELDCOUNT(r3)
264− li r6, 1
265− stb r6, VCPU_VPA_DIRTY(r4)
266−25:
267384 /* Load up DAR and DSISR */
268385 ld r5, VCPU_DAR(r4)
269386 lwz r6, VCPU_DSISR(r4)
270387 mtspr SPRN_DAR, r5
271388 mtspr SPRN_DSISR, r6
272389
390+ li r6, KVM_GUEST_MODE_HOST_HV
391+ stb r6, HSTATE_IN_GUEST(r13)
392+
273393 BEGIN_FTR_SECTION
274394 /* Restore AMR and UAMOR, set AMOR to all 1s */
275395 ld r5,VCPU_AMR(r4)
@@ -343,7 +463,28 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201) · 67 unmodified lines
343463 bdnz 28b
344464 ptesync
345465
346−22: li r0,1
466+ /* Add timebase offset onto timebase */
467+22: ld r8,VCORE_TB_OFFSET(r5)
468+ cmpdi r8,0
469+ beq 37f
470+ mftb r6 /* current host timebase */
471+ add r8,r8,r6
472+ mtspr SPRN_TBU40,r8 /* update upper 40 bits */
473+ mftb r7 /* check if lower 24 bits overflowed */
474+ clrldi r6,r6,40
475+ clrldi r7,r7,40
476+ cmpld r7,r6
477+ bge 37f
478+ addis r8,r8,0x100 /* if so, increment upper 40 bits */
479+ mtspr SPRN_TBU40,r8
480+
481+ /* Load guest PCR value to select appropriate compat mode */
482+37: ld r7, VCORE_PCR(r5)
483+ cmpdi r7, 0
484+ beq 38f
485+ mtspr SPRN_PCR, r7
486+38:
487+ li r0,1
347488 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
348489 b 10f
349490
@@ -353,12 +494,22 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201) · 3 unmodified lines
353494 beq 20b
354495
355496 /* Set LPCR and RMOR. */
356−10: ld r8,KVM_LPCR(r9)
497+10: ld r8,VCORE_LPCR(r5)
357498 mtspr SPRN_LPCR,r8
358499 ld r8,KVM_RMOR(r9)
359500 mtspr SPRN_RMOR,r8
360501 isync
361502
503+ /* Increment yield count if they have a VPA */
504+ ld r3, VCPU_VPA(r4)
505+ cmpdi r3, 0
506+ beq 25f
507+ lwz r5, LPPACA_YIELDCOUNT(r3)
508+ addi r5, r5, 1
509+ stw r5, LPPACA_YIELDCOUNT(r3)
510+ li r6, 1
511+ stb r6, VCPU_VPA_DIRTY(r4)
512+25:
362513 /* Check if HDEC expires soon */
363514 mfspr r3,SPRN_HDEC
364515 cmpwi r3,10
@@ -405,7 +556,8 @@ toc_tlbie_lock: · 40 unmodified lines
405556 bne 24b
406557 isync
407558
408− ld r7,KVM_LPCR(r9) /* use kvm->arch.lpcr to store HID4 */
559+ ld r5,HSTATE_KVM_VCORE(r13)
560+ ld r7,VCORE_LPCR(r5) /* use vcore->lpcr to store HID4 */
409561 li r0,0x18f
410562 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
411563 or r0,r7,r0
@@ -541,7 +693,7 @@ fast_guest_return: · 129 unmodified lines
541693 mtspr SPRN_HSRR1,r11
542694
543695 /* Activate guest mode, so faults get handled by KVM */
544− li r9, KVM_GUEST_MODE_GUEST
696+ li r9, KVM_GUEST_MODE_GUEST_HV
545697 stb r9, HSTATE_IN_GUEST(r13)
546698
547699 /* Enter guest */
@@ -550,13 +702,15 @@ BEGIN_FTR_SECTION · 2 unmodified lines
550702 ld r5, VCPU_CFAR(r4)
551703 mtspr SPRN_CFAR, r5
552704 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
705+BEGIN_FTR_SECTION
706+ ld r0, VCPU_PPR(r4)
707+END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
553708
554709 ld r5, VCPU_LR(r4)
555710 lwz r6, VCPU_CR(r4)
556711 mtlr r5
557712 mtcr r6
558713
559− ld r0, VCPU_GPR(R0)(r4)
560714 ld r1, VCPU_GPR(R1)(r4)
561715 ld r2, VCPU_GPR(R2)(r4)
562716 ld r3, VCPU_GPR(R3)(r4)
@@ -570,6 +724,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_CFAR) · 7 unmodified lines
570724 ld r12, VCPU_GPR(R12)(r4)
571725 ld r13, VCPU_GPR(R13)(r4)
572726
727+BEGIN_FTR_SECTION
728+ mtspr SPRN_PPR, r0
729+END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
730+ ld r0, VCPU_GPR(R0)(r4)
573731 ld r4, VCPU_GPR(R4)(r4)
574732
575733 hrfid
@@ -584,8 +742,8 @@ END_FTR_SECTION_IFSET(CPU_FTR_CFAR) · 8 unmodified lines
584742 /*
585743 * We come here from the first-level interrupt handlers.
586744 */
587− .globl kvmppc_interrupt
588−kvmppc_interrupt:
745+ .globl kvmppc_interrupt_hv
746+kvmppc_interrupt_hv:
589747 /*
590748 * Register contents:
591749 * R12 = interrupt vector
@@ -595,6 +753,19 @@ kvmppc_interrupt: · 3 unmodified lines
595753 */
596754 /* abuse host_r2 as third scratch area; we get r2 from PACATOC(r13) */
597755 std r9, HSTATE_HOST_R2(r13)
756+
757+ lbz r9, HSTATE_IN_GUEST(r13)
758+ cmpwi r9, KVM_GUEST_MODE_HOST_HV
759+ beq kvmppc_bad_host_intr
760+#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
761+ cmpwi r9, KVM_GUEST_MODE_GUEST
762+ ld r9, HSTATE_HOST_R2(r13)
763+ beq kvmppc_interrupt_pr
764+#endif
765+ /* We're now back in the host but in guest MMU context */
766+ li r9, KVM_GUEST_MODE_HOST_HV
767+ stb r9, HSTATE_IN_GUEST(r13)
768+
598769 ld r9, HSTATE_KVM_VCPU(r13)
599770
600771 /* Save registers */
@@ -620,6 +791,10 @@ BEGIN_FTR_SECTION · 19 unmodified lines
620791 ld r3, HSTATE_CFAR(r13)
621792 std r3, VCPU_CFAR(r9)
622793 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
794+BEGIN_FTR_SECTION
795+ ld r4, HSTATE_PPR(r13)
796+ std r4, VCPU_PPR(r9)
797+END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
623798
624799 /* Restore R1/R2 so we can handle faults */
625800 ld r1, HSTATE_HOST_R1(r13)
@@ -642,10 +817,6 @@ END_FTR_SECTION_IFSET(CPU_FTR_CFAR) · 16 unmodified lines
642817 std r3, VCPU_GPR(R13)(r9)
643818 std r4, VCPU_LR(r9)
644819
645− /* Unset guest mode */
646− li r0, KVM_GUEST_MODE_NONE
647− stb r0, HSTATE_IN_GUEST(r13)
648−
649820 stw r12,VCPU_TRAP(r9)
650821
651822 /* Save HEIR (HV emulation assist reg) in last_inst
@@ -696,46 +867,11 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206) · 44 unmodified lines
696867 * set, we know the host wants us out so let's do it now
697868 */
698869 do_ext_interrupt:
699− lbz r0, HSTATE_HOST_IPI(r13)
700− cmpwi r0, 0
701− bne ext_interrupt_to_host
702−
703− /* Now read the interrupt from the ICP */
704− ld r5, HSTATE_XICS_PHYS(r13)
705− li r7, XICS_XIRR
706− cmpdi r5, 0
707− beq- ext_interrupt_to_host
708− lwzcix r3, r5, r7
709− rlwinm. r0, r3, 0, 0xffffff
710− sync
711− beq 3f /* if nothing pending in the ICP */
712−
713− /* We found something in the ICP...
714− *
715− * If it's not an IPI, stash it in the PACA and return to
716− * the host, we don't (yet) handle directing real external
717− * interrupts directly to the guest
718− */
719− cmpwi r0, XICS_IPI
720− bne ext_stash_for_host
721−
722− /* It's an IPI, clear the MFRR and EOI it */
723− li r0, 0xff
724− li r6, XICS_MFRR
725− stbcix r0, r5, r6 /* clear the IPI */
726− stwcix r3, r5, r7 /* EOI it */
727− sync
728−
729− /* We need to re-check host IPI now in case it got set in the
730− * meantime. If it's clear, we bounce the interrupt to the
731− * guest
732− */
733− lbz r0, HSTATE_HOST_IPI(r13)
734− cmpwi r0, 0
735− bne- 1f
870+ bl kvmppc_read_intr
871+ cmpdi r3, 0
872+ bgt ext_interrupt_to_host
736873
737874 /* Allright, looks like an IPI for the guest, we need to set MER */
738−3:
739875 /* Check if any CPU is heading out to the host, if so head out too */
740876 ld r5, HSTATE_KVM_VCORE(r13)
741877 lwz r0, VCORE_ENTRY_EXIT(r5)
@@ -764,27 +900,9 @@ do_ext_interrupt: · 22 unmodified lines
764900 mtspr SPRN_LPCR, r8
765901 b fast_guest_return
766902
767− /* We raced with the host, we need to resend that IPI, bummer */
768−1: li r0, IPI_PRIORITY
769− stbcix r0, r5, r6 /* set the IPI */
770− sync
771− b ext_interrupt_to_host
772−
773−ext_stash_for_host:
774− /* It's not an IPI and it's for the host, stash it in the PACA
775− * before exit, it will be picked up by the host ICP driver
776− */
777− stw r3, HSTATE_SAVED_XIRR(r13)
778903 ext_interrupt_to_host:
779904
780905 guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
781− /* Save DEC */
782− mfspr r5,SPRN_DEC
783− mftb r6
784− extsw r5,r5
785− add r5,r5,r6
786− std r5,VCPU_DEC_EXPIRES(r9)
787−
788906 /* Save more register state */
789907 mfdar r6
790908 mfdsisr r7
@@ -954,7 +1072,30 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201) · 163 unmodified lines
9541072 mtspr SPRN_SDR1,r6 /* switch to partition page table */
9551073 mtspr SPRN_LPID,r7
9561074 isync
957− li r0,0
1075+
1076+ /* Subtract timebase offset from timebase */
1077+ ld r8,VCORE_TB_OFFSET(r5)
1078+ cmpdi r8,0
1079+ beq 17f
1080+ mftb r6 /* current host timebase */
1081+ subf r8,r8,r6
1082+ mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1083+ mftb r7 /* check if lower 24 bits overflowed */
1084+ clrldi r6,r6,40
1085+ clrldi r7,r7,40
1086+ cmpld r7,r6
1087+ bge 17f
1088+ addis r8,r8,0x100 /* if so, increment upper 40 bits */
1089+ mtspr SPRN_TBU40,r8
1090+
1091+ /* Reset PCR */
1092+17: ld r0, VCORE_PCR(r5)
1093+ cmpdi r0, 0
1094+ beq 18f
1095+ li r0, 0
1096+ mtspr SPRN_PCR, r0
1097+18:
1098+ /* Signal secondary CPUs to continue */
9581099 stb r0,VCORE_IN_GUEST(r5)
9591100 lis r8,0x7fff /* MAX_INT@h */
9601101 mtspr SPRN_HDEC,r8
@@ -1052,6 +1193,13 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201) · 91 unmodified lines
10521193 1: addi r8,r8,16
10531194 .endr
10541195
1196+ /* Save DEC */
1197+ mfspr r5,SPRN_DEC
1198+ mftb r6
1199+ extsw r5,r5
1200+ add r5,r5,r6
1201+ std r5,VCPU_DEC_EXPIRES(r9)
1202+
10551203 /* Save and reset AMR and UAMOR before turning on the MMU */
10561204 BEGIN_FTR_SECTION
10571205 mfspr r5,SPRN_AMR
@@ -1062,6 +1210,10 @@ BEGIN_FTR_SECTION · 4 unmodified lines
10621210 mtspr SPRN_AMR,r6
10631211 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
10641212
1213+ /* Unset guest mode */
1214+ li r0, KVM_GUEST_MODE_NONE
1215+ stb r0, HSTATE_IN_GUEST(r13)
1216+
10651217 /* Switch DSCR back to host value */
10661218 BEGIN_FTR_SECTION
10671219 mfspr r8, SPRN_DSCR
@@ -1134,9 +1286,13 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206) · 66 unmodified lines
11341286 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
11351287 b 22f
11361288 21: mfspr r5, SPRN_MMCR1
1289+ mfspr r7, SPRN_SIAR
1290+ mfspr r8, SPRN_SDAR
11371291 std r4, VCPU_MMCR(r9)
11381292 std r5, VCPU_MMCR + 8(r9)
11391293 std r6, VCPU_MMCR + 16(r9)
1294+ std r7, VCPU_SIAR(r9)
1295+ std r8, VCPU_SDAR(r9)
11401296 mfspr r3, SPRN_PMC1
11411297 mfspr r4, SPRN_PMC2
11421298 mfspr r5, SPRN_PMC3
@@ -1158,103 +1314,30 @@ BEGIN_FTR_SECTION · 15 unmodified lines
11581314 stw r11, VCPU_PMC + 28(r9)
11591315 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
11601316 22:
1317+ ld r0, 112+PPC_LR_STKOFF(r1)
1318+ addi r1, r1, 112
1319+ mtlr r0
1320+ blr
1321+secondary_too_late:
1322+ ld r5,HSTATE_KVM_VCORE(r13)
1323+ HMT_LOW
1324+13: lbz r3,VCORE_IN_GUEST(r5)
1325+ cmpwi r3,0
1326+ bne 13b
1327+ HMT_MEDIUM
1328+ li r0, KVM_GUEST_MODE_NONE
1329+ stb r0, HSTATE_IN_GUEST(r13)
1330+ ld r11,PACA_SLBSHADOWPTR(r13)
11611331
1162− /* Secondary threads go off to take a nap on POWER7 */
1163−BEGIN_FTR_SECTION
1164− lwz r0,VCPU_PTID(r9)
1165− cmpwi r0,0
1166− bne secondary_nap
1167−END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1168−
1169− /* Restore host DABR and DABRX */
1170− ld r5,HSTATE_DABR(r13)
1171− li r6,7
1172− mtspr SPRN_DABR,r5
1173− mtspr SPRN_DABRX,r6
1174−
1175− /* Restore SPRG3 */
1176− ld r3,PACA_SPRG3(r13)
1177− mtspr SPRN_SPRG3,r3
1178−
1179− /*
1180− * Reload DEC. HDEC interrupts were disabled when
1181− * we reloaded the host's LPCR value.
1182− */
1183− ld r3, HSTATE_DECEXP(r13)
1184− mftb r4
1185− subf r4, r4, r3
1186− mtspr SPRN_DEC, r4
1187−
1188− /* Reload the host's PMU registers */
1189− ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
1190− lbz r4, LPPACA_PMCINUSE(r3)
1191− cmpwi r4, 0
1192− beq 23f /* skip if not */
1193− lwz r3, HSTATE_PMC(r13)
1194− lwz r4, HSTATE_PMC + 4(r13)
1195− lwz r5, HSTATE_PMC + 8(r13)
1196− lwz r6, HSTATE_PMC + 12(r13)
1197− lwz r8, HSTATE_PMC + 16(r13)
1198− lwz r9, HSTATE_PMC + 20(r13)
1199−BEGIN_FTR_SECTION
1200− lwz r10, HSTATE_PMC + 24(r13)
1201− lwz r11, HSTATE_PMC + 28(r13)
1202−END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1203− mtspr SPRN_PMC1, r3
1204− mtspr SPRN_PMC2, r4
1205− mtspr SPRN_PMC3, r5
1206− mtspr SPRN_PMC4, r6
1207− mtspr SPRN_PMC5, r8
1208− mtspr SPRN_PMC6, r9
1209−BEGIN_FTR_SECTION
1210− mtspr SPRN_PMC7, r10
1211− mtspr SPRN_PMC8, r11
1212−END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1213− ld r3, HSTATE_MMCR(r13)
1214− ld r4, HSTATE_MMCR + 8(r13)
1215− ld r5, HSTATE_MMCR + 16(r13)
1216− mtspr SPRN_MMCR1, r4
1217− mtspr SPRN_MMCRA, r5
1218− mtspr SPRN_MMCR0, r3
1219− isync
1220−23:
1221− /*
1222− * For external and machine check interrupts, we need
1223− * to call the Linux handler to process the interrupt.
1224− * We do that by jumping to absolute address 0x500 for
1225− * external interrupts, or the machine_check_fwnmi label
1226− * for machine checks (since firmware might have patched
1227− * the vector area at 0x200). The [h]rfid at the end of the
1228− * handler will return to the book3s_hv_interrupts.S code.
1229− * For other interrupts we do the rfid to get back
1230− * to the book3s_hv_interrupts.S code here.
1231− */
1232− ld r8, HSTATE_VMHANDLER(r13)
1233− ld r7, HSTATE_HOST_MSR(r13)
1234−
1235− cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1236− cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1237−BEGIN_FTR_SECTION
1238− beq 11f
1239−END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1240−
1241− /* RFI into the highmem handler, or branch to interrupt handler */
1242− mfmsr r6
1243− li r0, MSR_RI
1244− andc r6, r6, r0
1245− mtmsrd r6, 1 /* Clear RI in MSR */
1246− mtsrr0 r8
1247− mtsrr1 r7
1248− beqa 0x500 /* external interrupt (PPC970) */
1249− beq cr1, 13f /* machine check */
1250− RFI
1251−
1252− /* On POWER7, we have external interrupts set to use HSRR0/1 */
1253−11: mtspr SPRN_HSRR0, r8
1254− mtspr SPRN_HSRR1, r7
1255− ba 0x500
1256−
1257−13: b machine_check_fwnmi
1332+ .rept SLB_NUM_BOLTED
1333+ ld r5,SLBSHADOW_SAVEAREA(r11)
1334+ ld r6,SLBSHADOW_SAVEAREA+8(r11)
1335+ andis. r7,r5,SLB_ESID_V@h
1336+ beq 1f
1337+ slbmte r6,r5
1338+1: addi r11,r11,16
1339+ .endr
1340+ b 22b
12581341
12591342 /*
12601343 * Check whether an HDSI is an HPTE not found fault or something else.
@@ -1333,7 +1416,7 @@ fast_interrupt_c_return: · 72 unmodified lines
13331416 stw r8, VCPU_LAST_INST(r9)
13341417
13351418 /* Unset guest mode. */
1336− li r0, KVM_GUEST_MODE_NONE
1419+ li r0, KVM_GUEST_MODE_HOST_HV
13371420 stb r0, HSTATE_IN_GUEST(r13)
13381421 b guest_exit_cont
13391422
@@ -1701,67 +1784,70 @@ machine_check_realmode: · 361 unmodified lines
17011784 rotldi r11, r11, 63
17021785 b fast_interrupt_c_return
17031786
1704−secondary_too_late:
1705− ld r5,HSTATE_KVM_VCORE(r13)
1706− HMT_LOW
1707−13: lbz r3,VCORE_IN_GUEST(r5)
1708− cmpwi r3,0
1709− bne 13b
1710− HMT_MEDIUM
1711− ld r11,PACA_SLBSHADOWPTR(r13)
1712−
1713− .rept SLB_NUM_BOLTED
1714− ld r5,SLBSHADOW_SAVEAREA(r11)
1715− ld r6,SLBSHADOW_SAVEAREA+8(r11)
1716− andis. r7,r5,SLB_ESID_V@h
1717− beq 1f
1718− slbmte r6,r5
1719−1: addi r11,r11,16
1720− .endr
1787+/*
1788+ * Determine what sort of external interrupt is pending (if any).
1789+ * Returns:
1790+ * 0 if no interrupt is pending
1791+ * 1 if an interrupt is pending that needs to be handled by the host
1792+ * -1 if there was a guest wakeup IPI (which has now been cleared)
1793+ */
1794+kvmppc_read_intr:
1795+ /* see if a host IPI is pending */
1796+ li r3, 1
1797+ lbz r0, HSTATE_HOST_IPI(r13)
1798+ cmpwi r0, 0
1799+ bne 1f
17211800
1722−secondary_nap:
1723− /* Clear our vcpu pointer so we don't come back in early */
1724− li r0, 0
1725− std r0, HSTATE_KVM_VCPU(r13)
1726− lwsync
1727− /* Clear any pending IPI - assume we're a secondary thread */
1728− ld r5, HSTATE_XICS_PHYS(r13)
1801+ /* Now read the interrupt from the ICP */
1802+ ld r6, HSTATE_XICS_PHYS(r13)
17291803 li r7, XICS_XIRR
1730− lwzcix r3, r5, r7 /* ack any pending interrupt */
1731− rlwinm. r0, r3, 0, 0xffffff /* any pending? */
1732− beq 37f
1804+ cmpdi r6, 0
1805+ beq- 1f
1806+ lwzcix r0, r6, r7
1807+ rlwinm. r3, r0, 0, 0xffffff
17331808 sync
1734− li r0, 0xff
1735− li r6, XICS_MFRR
1736− stbcix r0, r5, r6 /* clear the IPI */
1737− stwcix r3, r5, r7 /* EOI it */
1738−37: sync
1809+ beq 1f /* if nothing pending in the ICP */
17391810
1740− /* increment the nap count and then go to nap mode */
1741− ld r4, HSTATE_KVM_VCORE(r13)
1742− addi r4, r4, VCORE_NAP_COUNT
1743− lwsync /* make previous updates visible */
1744−51: lwarx r3, 0, r4
1745− addi r3, r3, 1
1746− stwcx. r3, 0, r4
1747− bne 51b
1811+ /* We found something in the ICP...
1812+ *
1813+ * If it's not an IPI, stash it in the PACA and return to
1814+ * the host, we don't (yet) handle directing real external
1815+ * interrupts directly to the guest
1816+ */
1817+ cmpwi r3, XICS_IPI /* if there is, is it an IPI? */
1818+ li r3, 1
1819+ bne 42f
17481820
1749−kvm_no_guest:
1750− li r0, KVM_HWTHREAD_IN_NAP
1751− stb r0, HSTATE_HWTHREAD_STATE(r13)
1821+ /* It's an IPI, clear the MFRR and EOI it */
1822+ li r3, 0xff
1823+ li r8, XICS_MFRR
1824+ stbcix r3, r6, r8 /* clear the IPI */
1825+ stwcix r0, r6, r7 /* EOI it */
1826+ sync
17521827
1753− li r3, LPCR_PECE0
1754− mfspr r4, SPRN_LPCR
1755− rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
1756− mtspr SPRN_LPCR, r4
1757− isync
1758− std r0, HSTATE_SCRATCH0(r13)
1759− ptesync
1760− ld r0, HSTATE_SCRATCH0(r13)
1761−1: cmpd r0, r0
1762− bne 1b
1763− nap
1764− b .
1828+ /* We need to re-check host IPI now in case it got set in the
1829+ * meantime. If it's clear, we bounce the interrupt to the
1830+ * guest
1831+ */
1832+ lbz r0, HSTATE_HOST_IPI(r13)
1833+ cmpwi r0, 0
1834+ bne- 43f
1835+
1836+ /* OK, it's an IPI for us */
1837+ li r3, -1
1838+1: blr
1839+
1840+42: /* It's not an IPI and it's for the host, stash it in the PACA
1841+ * before exit, it will be picked up by the host ICP driver
1842+ */
1843+ stw r0, HSTATE_SAVED_XIRR(r13)
1844+ b 1b
1845+
1846+43: /* We raced with the host, we need to resend that IPI, bummer */
1847+ li r0, IPI_PRIORITY
1848+ stbcix r0, r6, r8 /* set the IPI */
1849+ sync
1850+ b 1b
17651851
17661852 /*
17671853 * Save away FP, VMX and VSX registers.
@@ -1879,3 +1965,11 @@ END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) · 111 unmodified lines
18791965 lwz r7,VCPU_VRSAVE(r4)
18801966 mtspr SPRN_VRSAVE,r7
18811967 blr
1968+
1969+/*
1970+ * We come here if we get any exception or interrupt while we are
1971+ * executing host real mode code while in guest MMU context.
1972+ * For now just spin, but we should do something better.
1973+ */
1974+kvmppc_bad_host_intr:
1975+ b .
arch/powerpc/kvm/book3s_interrupts.S+26−6
@@ -26,8 +26,12 @@ · 25 unmodified lines
2626
2727 #if defined(CONFIG_PPC_BOOK3S_64)
2828 #define FUNC(name) GLUE(.,name)
29+#define GET_SHADOW_VCPU(reg) addi reg, r13, PACA_SVCPU
30+
2931 #elif defined(CONFIG_PPC_BOOK3S_32)
3032 #define FUNC(name) name
33+#define GET_SHADOW_VCPU(reg) lwz reg, (THREAD + THREAD_KVM_SVCPU)(r2)
34+
3135 #endif /* CONFIG_PPC_BOOK3S_XX */
3236
3337 #define VCPU_LOAD_NVGPRS(vcpu) \
@@ -87,8 +91,14 @@ kvm_start_entry: · 53 unmodified lines
8791 VCPU_LOAD_NVGPRS(r4)
8892
8993 kvm_start_lightweight:
94+ /* Copy registers into shadow vcpu so we can access them in real mode */
95+ GET_SHADOW_VCPU(r3)
96+ bl FUNC(kvmppc_copy_to_svcpu)
97+ nop
98+ REST_GPR(4, r1)
9099
91100 #ifdef CONFIG_PPC_BOOK3S_64
101+ /* Get the dcbz32 flag */
92102 PPC_LL r3, VCPU_HFLAGS(r4)
93103 rldicl r3, r3, 0, 63 /* r3 &= 1 */
94104 stb r3, HSTATE_RESTORE_HID5(r13)
@@ -111,9 +121,6 @@ kvm_start_lightweight: · 16 unmodified lines
111121 *
112122 */
113123
114−.global kvmppc_handler_highmem
115−kvmppc_handler_highmem:
116−
117124 /*
118125 * Register usage at this point:
119126 *
@@ -125,18 +132,31 @@ kvmppc_handler_highmem: · 5 unmodified lines
125132 *
126133 */
127134
128− /* R7 = vcpu */
129− PPC_LL r7, GPR4(r1)
135+ /* Transfer reg values from shadow vcpu back to vcpu struct */
136+ /* On 64-bit, interrupts are still off at this point */
137+ PPC_LL r3, GPR4(r1) /* vcpu pointer */
138+ GET_SHADOW_VCPU(r4)
139+ bl FUNC(kvmppc_copy_from_svcpu)
140+ nop
130141
131142 #ifdef CONFIG_PPC_BOOK3S_64
143+ /* Re-enable interrupts */
144+ ld r3, HSTATE_HOST_MSR(r13)
145+ ori r3, r3, MSR_EE
146+ MTMSR_EERI(r3)
147+
132148 /*
133149 * Reload kernel SPRG3 value.
134150 * No need to save guest value as usermode can't modify SPRG3.
135151 */
136152 ld r3, PACA_SPRG3(r13)
137153 mtspr SPRN_SPRG3, r3
154+
138155 #endif /* CONFIG_PPC_BOOK3S_64 */
139156
157+ /* R7 = vcpu */
158+ PPC_LL r7, GPR4(r1)
159+
140160 PPC_STL r14, VCPU_GPR(R14)(r7)
141161 PPC_STL r15, VCPU_GPR(R15)(r7)
142162 PPC_STL r16, VCPU_GPR(R16)(r7)
@@ -161,7 +181,7 @@ kvmppc_handler_highmem: · 18 unmodified lines
161181
162182 /* Restore r3 (kvm_run) and r4 (vcpu) */
163183 REST_2GPRS(3, r1)
164− bl FUNC(kvmppc_handle_exit)
184+ bl FUNC(kvmppc_handle_exit_pr)
165185
166186 /* If RESUME_GUEST, get back in the loop */
167187 cmpwi r3, RESUME_GUEST
arch/powerpc/kvm/book3s_mmu_hpte.c+61−5
@@ -28,7 +28,7 @@ · 27 unmodified lines
2828 #include <asm/mmu_context.h>
2929 #include <asm/hw_irq.h>
3030
31−#include "trace.h"
31+#include "trace_pr.h"
3232
3333 #define PTE_SIZE 12
3434
@@ -56,6 +56,14 @@ static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) · 21 unmodified lines
5656 HPTEG_HASH_BITS_VPTE_LONG);
5757 }
5858
59+#ifdef CONFIG_PPC_BOOK3S_64
60+static inline u64 kvmppc_mmu_hash_vpte_64k(u64 vpage)
61+{
62+ return hash_64((vpage & 0xffffffff0ULL) >> 4,
63+ HPTEG_HASH_BITS_VPTE_64K);
64+}
65+#endif
66+
5967 void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
6068 {
6169 u64 index;
@@ -83,6 +91,15 @@ void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte) · 21 unmodified lines
8391 hlist_add_head_rcu(&pte->list_vpte_long,
8492 &vcpu3s->hpte_hash_vpte_long[index]);
8593
94+#ifdef CONFIG_PPC_BOOK3S_64
95+ /* Add to vPTE_64k list */
96+ index = kvmppc_mmu_hash_vpte_64k(pte->pte.vpage);
97+ hlist_add_head_rcu(&pte->list_vpte_64k,
98+ &vcpu3s->hpte_hash_vpte_64k[index]);
99+#endif
100+
101+ vcpu3s->hpte_cache_count++;
102+
86103 spin_unlock(&vcpu3s->mmu_lock);
87104 }
88105
@@ -113,10 +130,13 @@ static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte) · 24 unmodified lines
113130 hlist_del_init_rcu(&pte->list_pte_long);
114131 hlist_del_init_rcu(&pte->list_vpte);
115132 hlist_del_init_rcu(&pte->list_vpte_long);
133+#ifdef CONFIG_PPC_BOOK3S_64
134+ hlist_del_init_rcu(&pte->list_vpte_64k);
135+#endif
136+ vcpu3s->hpte_cache_count--;
116137
117138 spin_unlock(&vcpu3s->mmu_lock);
118139
119− vcpu3s->hpte_cache_count--;
120140 call_rcu(&pte->rcu_head, free_pte_rcu);
121141 }
122142
@@ -219,6 +239,29 @@ static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp) · 96 unmodified lines
219239 rcu_read_unlock();
220240 }
221241
242+#ifdef CONFIG_PPC_BOOK3S_64
243+/* Flush with mask 0xffffffff0 */
244+static void kvmppc_mmu_pte_vflush_64k(struct kvm_vcpu *vcpu, u64 guest_vp)
245+{
246+ struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
247+ struct hlist_head *list;
248+ struct hpte_cache *pte;
249+ u64 vp_mask = 0xffffffff0ULL;
250+
251+ list = &vcpu3s->hpte_hash_vpte_64k[
252+ kvmppc_mmu_hash_vpte_64k(guest_vp)];
253+
254+ rcu_read_lock();
255+
256+ /* Check the list for matching entries and invalidate */
257+ hlist_for_each_entry_rcu(pte, list, list_vpte_64k)
258+ if ((pte->pte.vpage & vp_mask) == guest_vp)
259+ invalidate_pte(vcpu, pte);
260+
261+ rcu_read_unlock();
262+}
263+#endif
264+
222265 /* Flush with mask 0xffffff000 */
223266 static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu *vcpu, u64 guest_vp)
224267 {
@@ -249,6 +292,11 @@ void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask) · 24 unmodified lines
249292 case 0xfffffffffULL:
250293 kvmppc_mmu_pte_vflush_short(vcpu, guest_vp);
251294 break;
295+#ifdef CONFIG_PPC_BOOK3S_64
296+ case 0xffffffff0ULL:
297+ kvmppc_mmu_pte_vflush_64k(vcpu, guest_vp);
298+ break;
299+#endif
252300 case 0xffffff000ULL:
253301 kvmppc_mmu_pte_vflush_long(vcpu, guest_vp);
254302 break;
@@ -285,15 +333,19 @@ struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu) · 30 unmodified lines
285333 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
286334 struct hpte_cache *pte;
287335
288− pte = kmem_cache_zalloc(hpte_cache, GFP_KERNEL);
289− vcpu3s->hpte_cache_count++;
290−
291336 if (vcpu3s->hpte_cache_count == HPTEG_CACHE_NUM)
292337 kvmppc_mmu_pte_flush_all(vcpu);
293338
339+ pte = kmem_cache_zalloc(hpte_cache, GFP_KERNEL);
340+
294341 return pte;
295342 }
296343
344+void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte)
345+{
346+ kmem_cache_free(hpte_cache, pte);
347+}
348+
297349 void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu)
298350 {
299351 kvmppc_mmu_pte_flush(vcpu, 0, 0);
@@ -320,6 +372,10 @@ int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu) · 20 unmodified lines
320372 ARRAY_SIZE(vcpu3s->hpte_hash_vpte));
321373 kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte_long,
322374 ARRAY_SIZE(vcpu3s->hpte_hash_vpte_long));
375+#ifdef CONFIG_PPC_BOOK3S_64
376+ kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte_64k,
377+ ARRAY_SIZE(vcpu3s->hpte_hash_vpte_64k));
378+#endif
323379
324380 spin_lock_init(&vcpu3s->mmu_lock);
325381
arch/powerpc/kvm/book3s_pr.c+361−137
@@ -40,8 +40,12 @@ · 39 unmodified lines
4040 #include <linux/sched.h>
4141 #include <linux/vmalloc.h>
4242 #include <linux/highmem.h>
43+#include <linux/module.h>
4344
44−#include "trace.h"
45+#include "book3s.h"
46+
47+#define CREATE_TRACE_POINTS
48+#include "trace_pr.h"
4549
4650 /* #define EXIT_DEBUG */
4751 /* #define DEBUG_EXT */
@@ -56,29 +60,25 @@ static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr, · 8 unmodified lines
5660 #define HW_PAGE_SIZE PAGE_SIZE
5761 #endif
5862
59−void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
63+static void kvmppc_core_vcpu_load_pr(struct kvm_vcpu *vcpu, int cpu)
6064 {
6165 #ifdef CONFIG_PPC_BOOK3S_64
6266 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
6367 memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb));
64− memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
65− sizeof(get_paca()->shadow_vcpu));
6668 svcpu->slb_max = to_book3s(vcpu)->slb_shadow_max;
6769 svcpu_put(svcpu);
6870 #endif
6971 vcpu->cpu = smp_processor_id();
7072 #ifdef CONFIG_PPC_BOOK3S_32
71− current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
73+ current->thread.kvm_shadow_vcpu = vcpu->arch.shadow_vcpu;
7274 #endif
7375 }
7476
75−void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
77+static void kvmppc_core_vcpu_put_pr(struct kvm_vcpu *vcpu)
7678 {
7779 #ifdef CONFIG_PPC_BOOK3S_64
7880 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
7981 memcpy(to_book3s(vcpu)->slb_shadow, svcpu->slb, sizeof(svcpu->slb));
80− memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
81− sizeof(get_paca()->shadow_vcpu));
8282 to_book3s(vcpu)->slb_shadow_max = svcpu->slb_max;
8383 svcpu_put(svcpu);
8484 #endif
@@ -87,7 +87,61 @@ void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu) · 2 unmodified lines
8787 vcpu->cpu = -1;
8888 }
8989
90−int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
90+/* Copy data needed by real-mode code from vcpu to shadow vcpu */
91+void kvmppc_copy_to_svcpu(struct kvmppc_book3s_shadow_vcpu *svcpu,
92+ struct kvm_vcpu *vcpu)
93+{
94+ svcpu->gpr[0] = vcpu->arch.gpr[0];
95+ svcpu->gpr[1] = vcpu->arch.gpr[1];
96+ svcpu->gpr[2] = vcpu->arch.gpr[2];
97+ svcpu->gpr[3] = vcpu->arch.gpr[3];
98+ svcpu->gpr[4] = vcpu->arch.gpr[4];
99+ svcpu->gpr[5] = vcpu->arch.gpr[5];
100+ svcpu->gpr[6] = vcpu->arch.gpr[6];
101+ svcpu->gpr[7] = vcpu->arch.gpr[7];
102+ svcpu->gpr[8] = vcpu->arch.gpr[8];
103+ svcpu->gpr[9] = vcpu->arch.gpr[9];
104+ svcpu->gpr[10] = vcpu->arch.gpr[10];
105+ svcpu->gpr[11] = vcpu->arch.gpr[11];
106+ svcpu->gpr[12] = vcpu->arch.gpr[12];
107+ svcpu->gpr[13] = vcpu->arch.gpr[13];
108+ svcpu->cr = vcpu->arch.cr;
109+ svcpu->xer = vcpu->arch.xer;
110+ svcpu->ctr = vcpu->arch.ctr;
111+ svcpu->lr = vcpu->arch.lr;
112+ svcpu->pc = vcpu->arch.pc;
113+}
114+
115+/* Copy data touched by real-mode code from shadow vcpu back to vcpu */
116+void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu,
117+ struct kvmppc_book3s_shadow_vcpu *svcpu)
118+{
119+ vcpu->arch.gpr[0] = svcpu->gpr[0];
120+ vcpu->arch.gpr[1] = svcpu->gpr[1];
121+ vcpu->arch.gpr[2] = svcpu->gpr[2];
122+ vcpu->arch.gpr[3] = svcpu->gpr[3];
123+ vcpu->arch.gpr[4] = svcpu->gpr[4];
124+ vcpu->arch.gpr[5] = svcpu->gpr[5];
125+ vcpu->arch.gpr[6] = svcpu->gpr[6];
126+ vcpu->arch.gpr[7] = svcpu->gpr[7];
127+ vcpu->arch.gpr[8] = svcpu->gpr[8];
128+ vcpu->arch.gpr[9] = svcpu->gpr[9];
129+ vcpu->arch.gpr[10] = svcpu->gpr[10];
130+ vcpu->arch.gpr[11] = svcpu->gpr[11];
131+ vcpu->arch.gpr[12] = svcpu->gpr[12];
132+ vcpu->arch.gpr[13] = svcpu->gpr[13];
133+ vcpu->arch.cr = svcpu->cr;
134+ vcpu->arch.xer = svcpu->xer;
135+ vcpu->arch.ctr = svcpu->ctr;
136+ vcpu->arch.lr = svcpu->lr;
137+ vcpu->arch.pc = svcpu->pc;
138+ vcpu->arch.shadow_srr1 = svcpu->shadow_srr1;
139+ vcpu->arch.fault_dar = svcpu->fault_dar;
140+ vcpu->arch.fault_dsisr = svcpu->fault_dsisr;
141+ vcpu->arch.last_inst = svcpu->last_inst;
142+}
143+
144+static int kvmppc_core_check_requests_pr(struct kvm_vcpu *vcpu)
91145 {
92146 int r = 1; /* Indicate we want to get back into the guest */
93147
@@ -100,44 +154,69 @@ int kvmppc_core_check_requests(struct kvm_vcpu *vcpu) · 6 unmodified lines
100154 }
101155
102156 /************* MMU Notifiers *************/
157+static void do_kvm_unmap_hva(struct kvm *kvm, unsigned long start,
158+ unsigned long end)
159+{
160+ long i;
161+ struct kvm_vcpu *vcpu;
162+ struct kvm_memslots *slots;
163+ struct kvm_memory_slot *memslot;
164+
165+ slots = kvm_memslots(kvm);
166+ kvm_for_each_memslot(memslot, slots) {
167+ unsigned long hva_start, hva_end;
168+ gfn_t gfn, gfn_end;
169+
170+ hva_start = max(start, memslot->userspace_addr);
171+ hva_end = min(end, memslot->userspace_addr +
172+ (memslot->npages << PAGE_SHIFT));
173+ if (hva_start >= hva_end)
174+ continue;
175+ /*
176+ * {gfn(page) | page intersects with [hva_start, hva_end)} =
177+ * {gfn, gfn+1, ..., gfn_end-1}.
178+ */
179+ gfn = hva_to_gfn_memslot(hva_start, memslot);
180+ gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
181+ kvm_for_each_vcpu(i, vcpu, kvm)
182+ kvmppc_mmu_pte_pflush(vcpu, gfn << PAGE_SHIFT,
183+ gfn_end << PAGE_SHIFT);
184+ }
185+}
103186
104−int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
187+static int kvm_unmap_hva_pr(struct kvm *kvm, unsigned long hva)
105188 {
106189 trace_kvm_unmap_hva(hva);
107190
108− /*
109− * Flush all shadow tlb entries everywhere. This is slow, but
110− * we are 100% sure that we catch the to be unmapped page
111− */
112− kvm_flush_remote_tlbs(kvm);
191+ do_kvm_unmap_hva(kvm, hva, hva + PAGE_SIZE);
113192
114193 return 0;
115194 }
116195
117−int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
196+static int kvm_unmap_hva_range_pr(struct kvm *kvm, unsigned long start,
197+ unsigned long end)
118198 {
119− /* kvm_unmap_hva flushes everything anyways */
120− kvm_unmap_hva(kvm, start);
199+ do_kvm_unmap_hva(kvm, start, end);
121200
122201 return 0;
123202 }
124203
125−int kvm_age_hva(struct kvm *kvm, unsigned long hva)
204+static int kvm_age_hva_pr(struct kvm *kvm, unsigned long hva)
126205 {
127206 /* XXX could be more clever ;) */
128207 return 0;
129208 }
130209
131−int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
210+static int kvm_test_age_hva_pr(struct kvm *kvm, unsigned long hva)
132211 {
133212 /* XXX could be more clever ;) */
134213 return 0;
135214 }
136215
137−void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
216+static void kvm_set_spte_hva_pr(struct kvm *kvm, unsigned long hva, pte_t pte)
138217 {
139218 /* The page will get remapped properly on its next fault */
140− kvm_unmap_hva(kvm, hva);
219+ do_kvm_unmap_hva(kvm, hva, hva + PAGE_SIZE);
141220 }
142221
143222 /*****************************************/
@@ -159,7 +238,7 @@ static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu) · 15 unmodified lines
159238 vcpu->arch.shadow_msr = smsr;
160239 }
161240
162−void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
241+static void kvmppc_set_msr_pr(struct kvm_vcpu *vcpu, u64 msr)
163242 {
164243 ulong old_msr = vcpu->arch.shared->msr;
165244
@@ -219,7 +298,7 @@ void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr) · 53 unmodified lines
219298 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
220299 }
221300
222−void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
301+void kvmppc_set_pvr_pr(struct kvm_vcpu *vcpu, u32 pvr)
223302 {
224303 u32 host_pvr;
225304
@@ -256,6 +335,23 @@ void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr) · 30 unmodified lines
256335 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
257336 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
258337
338+ /*
339+ * If they're asking for POWER6 or later, set the flag
340+ * indicating that we can do multiple large page sizes
341+ * and 1TB segments.
342+ * Also set the flag that indicates that tlbie has the large
343+ * page bit in the RB operand instead of the instruction.
344+ */
345+ switch (PVR_VER(pvr)) {
346+ case PVR_POWER6:
347+ case PVR_POWER7:
348+ case PVR_POWER7p:
349+ case PVR_POWER8:
350+ vcpu->arch.hflags |= BOOK3S_HFLAG_MULTI_PGSIZE |
351+ BOOK3S_HFLAG_NEW_TLBIE;
352+ break;
353+ }
354+
259355 #ifdef CONFIG_PPC_BOOK3S_32
260356 /* 32 bit Book3S always has 32 byte dcbz */
261357 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
@@ -334,6 +430,7 @@ int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, · 72 unmodified lines
334430 ulong eaddr, int vec)
335431 {
336432 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
433+ bool iswrite = false;
337434 int r = RESUME_GUEST;
338435 int relocated;
339436 int page_found = 0;
@@ -344,10 +441,12 @@ int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, · 4 unmodified lines
344441 u64 vsid;
345442
346443 relocated = data ? dr : ir;
444+ if (data && (vcpu->arch.fault_dsisr & DSISR_ISSTORE))
445+ iswrite = true;
347446
348447 /* Resolve real address if translation turned on */
349448 if (relocated) {
350− page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
449+ page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data, iswrite);
351450 } else {
352451 pte.may_execute = true;
353452 pte.may_read = true;
@@ -355,6 +454,7 @@ int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, · 1 unmodified line
355454 pte.raddr = eaddr & KVM_PAM;
356455 pte.eaddr = eaddr;
357456 pte.vpage = eaddr >> 12;
457+ pte.page_size = MMU_PAGE_64K;
358458 }
359459
360460 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
@@ -388,22 +488,18 @@ int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, · 27 unmodified lines
388488
389489 if (page_found == -ENOENT) {
390490 /* Page not found in guest PTE entries */
391− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
392491 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
393− vcpu->arch.shared->dsisr = svcpu->fault_dsisr;
492+ vcpu->arch.shared->dsisr = vcpu->arch.fault_dsisr;
394493 vcpu->arch.shared->msr |=
395− (svcpu->shadow_srr1 & 0x00000000f8000000ULL);
396− svcpu_put(svcpu);
494+ vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL;
397495 kvmppc_book3s_queue_irqprio(vcpu, vec);
398496 } else if (page_found == -EPERM) {
399497 /* Storage protection */
400− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
401498 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
402− vcpu->arch.shared->dsisr = svcpu->fault_dsisr & ~DSISR_NOHPTE;
499+ vcpu->arch.shared->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
403500 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
404501 vcpu->arch.shared->msr |=
405− svcpu->shadow_srr1 & 0x00000000f8000000ULL;
406− svcpu_put(svcpu);
502+ vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL;
407503 kvmppc_book3s_queue_irqprio(vcpu, vec);
408504 } else if (page_found == -EINVAL) {
409505 /* Page not found in guest SLB */
@@ -411,12 +507,20 @@ int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, · 1 unmodified line
411507 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
412508 } else if (!is_mmio &&
413509 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
510+ if (data && !(vcpu->arch.fault_dsisr & DSISR_NOHPTE)) {
511+ /*
512+ * There is already a host HPTE there, presumably
513+ * a read-only one for a page the guest thinks
514+ * is writable, so get rid of it first.
515+ */
516+ kvmppc_mmu_unmap_page(vcpu, &pte);
517+ }
414518 /* The guest's PTE is not mapped yet. Map on the host */
415− kvmppc_mmu_map_page(vcpu, &pte);
519+ kvmppc_mmu_map_page(vcpu, &pte, iswrite);
416520 if (data)
417521 vcpu->stat.sp_storage++;
418522 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
419− (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
523+ (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
420524 kvmppc_patch_dcbz(vcpu, &pte);
421525 } else {
422526 /* MMIO */
@@ -619,13 +723,15 @@ static void kvmppc_handle_lost_ext(struct kvm_vcpu *vcpu) · 196 unmodified lines
619723
620724 if (lost_ext & MSR_FP)
621725 kvmppc_load_up_fpu();
726+#ifdef CONFIG_ALTIVEC
622727 if (lost_ext & MSR_VEC)
623728 kvmppc_load_up_altivec();
729+#endif
624730 current->thread.regs->msr |= lost_ext;
625731 }
626732
627−int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
628− unsigned int exit_nr)
733+int kvmppc_handle_exit_pr(struct kvm_run *run, struct kvm_vcpu *vcpu,
734+ unsigned int exit_nr)
629735 {
630736 int r = RESUME_HOST;
631737 int s;
@@ -643,25 +749,32 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, · 11 unmodified lines
643749 switch (exit_nr) {
644750 case BOOK3S_INTERRUPT_INST_STORAGE:
645751 {
646− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
647− ulong shadow_srr1 = svcpu->shadow_srr1;
752+ ulong shadow_srr1 = vcpu->arch.shadow_srr1;
648753 vcpu->stat.pf_instruc++;
649754
650755 #ifdef CONFIG_PPC_BOOK3S_32
651756 /* We set segments as unused segments when invalidating them. So
652757 * treat the respective fault as segment fault. */
653− if (svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] == SR_INVALID) {
654− kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
655− r = RESUME_GUEST;
758+ {
759+ struct kvmppc_book3s_shadow_vcpu *svcpu;
760+ u32 sr;
761+
762+ svcpu = svcpu_get(vcpu);
763+ sr = svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT];
656764 svcpu_put(svcpu);
657− break;
765+ if (sr == SR_INVALID) {
766+ kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
767+ r = RESUME_GUEST;
768+ break;
769+ }
658770 }
659771 #endif
660− svcpu_put(svcpu);
661772
662773 /* only care about PTEG not found errors, but leave NX alone */
663774 if (shadow_srr1 & 0x40000000) {
775+ int idx = srcu_read_lock(&vcpu->kvm->srcu);
664776 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
777+ srcu_read_unlock(&vcpu->kvm->srcu, idx);
665778 vcpu->stat.sp_instruc++;
666779 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
667780 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
@@ -682,25 +795,36 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, · 14 unmodified lines
682795 case BOOK3S_INTERRUPT_DATA_STORAGE:
683796 {
684797 ulong dar = kvmppc_get_fault_dar(vcpu);
685− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
686− u32 fault_dsisr = svcpu->fault_dsisr;
798+ u32 fault_dsisr = vcpu->arch.fault_dsisr;
687799 vcpu->stat.pf_storage++;
688800
689801 #ifdef CONFIG_PPC_BOOK3S_32
690802 /* We set segments as unused segments when invalidating them. So
691803 * treat the respective fault as segment fault. */
692− if ((svcpu->sr[dar >> SID_SHIFT]) == SR_INVALID) {
693− kvmppc_mmu_map_segment(vcpu, dar);
694− r = RESUME_GUEST;
804+ {
805+ struct kvmppc_book3s_shadow_vcpu *svcpu;
806+ u32 sr;
807+
808+ svcpu = svcpu_get(vcpu);
809+ sr = svcpu->sr[dar >> SID_SHIFT];
695810 svcpu_put(svcpu);
696− break;
811+ if (sr == SR_INVALID) {
812+ kvmppc_mmu_map_segment(vcpu, dar);
813+ r = RESUME_GUEST;
814+ break;
815+ }
697816 }
698817 #endif
699− svcpu_put(svcpu);
700818
701− /* The only case we need to handle is missing shadow PTEs */
702− if (fault_dsisr & DSISR_NOHPTE) {
819+ /*
820+ * We need to handle missing shadow PTEs, and
821+ * protection faults due to us mapping a page read-only
822+ * when the guest thinks it is writable.
823+ */
824+ if (fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT)) {
825+ int idx = srcu_read_lock(&vcpu->kvm->srcu);
703826 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
827+ srcu_read_unlock(&vcpu->kvm->srcu, idx);
704828 } else {
705829 vcpu->arch.shared->dar = dar;
706830 vcpu->arch.shared->dsisr = fault_dsisr;
@@ -743,13 +867,10 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, · 36 unmodified lines
743867 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
744868 {
745869 enum emulation_result er;
746− struct kvmppc_book3s_shadow_vcpu *svcpu;
747870 ulong flags;
748871
749872 program_interrupt:
750− svcpu = svcpu_get(vcpu);
751− flags = svcpu->shadow_srr1 & 0x1f0000ull;
752− svcpu_put(svcpu);
873+ flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
753874
754875 if (vcpu->arch.shared->msr & MSR_PR) {
755876 #ifdef EXIT_DEBUG
@@ -798,7 +919,7 @@ program_interrupt: · 42 unmodified lines
798919 ulong cmd = kvmppc_get_gpr(vcpu, 3);
799920 int i;
800921
801−#ifdef CONFIG_KVM_BOOK3S_64_PR
922+#ifdef CONFIG_PPC_BOOK3S_64
802923 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
803924 r = RESUME_GUEST;
804925 break;
@@ -881,9 +1002,7 @@ program_interrupt: · 76 unmodified lines
8811002 break;
8821003 default:
8831004 {
884− struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
885− ulong shadow_srr1 = svcpu->shadow_srr1;
886− svcpu_put(svcpu);
1005+ ulong shadow_srr1 = vcpu->arch.shadow_srr1;
8871006 /* Ugh - bork here! What did we get? */
8881007 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
8891008 exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
@@ -920,8 +1039,8 @@ program_interrupt: · 30 unmodified lines
9201039 return r;
9211040 }
9221041
923−int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
924− struct kvm_sregs *sregs)
1042+static int kvm_arch_vcpu_ioctl_get_sregs_pr(struct kvm_vcpu *vcpu,
1043+ struct kvm_sregs *sregs)
9251044 {
9261045 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
9271046 int i;
@@ -947,13 +1066,13 @@ int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, · 19 unmodified lines
9471066 return 0;
9481067 }
9491068
950−int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
951− struct kvm_sregs *sregs)
1069+static int kvm_arch_vcpu_ioctl_set_sregs_pr(struct kvm_vcpu *vcpu,
1070+ struct kvm_sregs *sregs)
9521071 {
9531072 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
9541073 int i;
9551074
956− kvmppc_set_pvr(vcpu, sregs->pvr);
1075+ kvmppc_set_pvr_pr(vcpu, sregs->pvr);
9571076
9581077 vcpu3s->sdr1 = sregs->u.s.sdr1;
9591078 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
@@ -983,7 +1102,8 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · 23 unmodified lines
9831102 return 0;
9841103 }
9851104
986−int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
1105+static int kvmppc_get_one_reg_pr(struct kvm_vcpu *vcpu, u64 id,
1106+ union kvmppc_one_reg *val)
9871107 {
9881108 int r = 0;
9891109
@@ -1012,7 +1132,8 @@ int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val) · 22 unmodified lines
10121132 return r;
10131133 }
10141134
1015−int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
1135+static int kvmppc_set_one_reg_pr(struct kvm_vcpu *vcpu, u64 id,
1136+ union kvmppc_one_reg *val)
10161137 {
10171138 int r = 0;
10181139
@@ -1042,28 +1163,30 @@ int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val) · 23 unmodified lines
10421163 return r;
10431164 }
10441165
1045−int kvmppc_core_check_processor_compat(void)
1046−{
1047− return 0;
1048−}
1049−
1050−struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1166+static struct kvm_vcpu *kvmppc_core_vcpu_create_pr(struct kvm *kvm,
1167+ unsigned int id)
10511168 {
10521169 struct kvmppc_vcpu_book3s *vcpu_book3s;
10531170 struct kvm_vcpu *vcpu;
10541171 int err = -ENOMEM;
10551172 unsigned long p;
10561173
1057− vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
1058− if (!vcpu_book3s)
1174+ vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1175+ if (!vcpu)
10591176 goto out;
10601177
1061− vcpu_book3s->shadow_vcpu =
1062− kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1063− if (!vcpu_book3s->shadow_vcpu)
1178+ vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
1179+ if (!vcpu_book3s)
10641180 goto free_vcpu;
1181+ vcpu->arch.book3s = vcpu_book3s;
1182+
1183+#ifdef CONFIG_KVM_BOOK3S_32
1184+ vcpu->arch.shadow_vcpu =
1185+ kzalloc(sizeof(*vcpu->arch.shadow_vcpu), GFP_KERNEL);
1186+ if (!vcpu->arch.shadow_vcpu)
1187+ goto free_vcpu3s;
1188+#endif
10651189
1066− vcpu = &vcpu_book3s->vcpu;
10671190 err = kvm_vcpu_init(vcpu, kvm, id);
10681191 if (err)
10691192 goto free_shadow_vcpu;
@@ -1076,13 +1199,19 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) · 6 unmodified lines
10761199 vcpu->arch.shared = (void *)(p + PAGE_SIZE - 4096);
10771200
10781201 #ifdef CONFIG_PPC_BOOK3S_64
1079− /* default to book3s_64 (970fx) */
1202+ /*
1203+ * Default to the same as the host if we're on sufficiently
1204+ * recent machine that we have 1TB segments;
1205+ * otherwise default to PPC970FX.
1206+ */
10801207 vcpu->arch.pvr = 0x3C0301;
1208+ if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1209+ vcpu->arch.pvr = mfspr(SPRN_PVR);
10811210 #else
10821211 /* default to book3s_32 (750) */
10831212 vcpu->arch.pvr = 0x84202;
10841213 #endif
1085− kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1214+ kvmppc_set_pvr_pr(vcpu, vcpu->arch.pvr);
10861215 vcpu->arch.slb_nr = 64;
10871216
10881217 vcpu->arch.shadow_msr = MSR_USER64;
@@ -1096,24 +1225,31 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) · 7 unmodified lines
10961225 uninit_vcpu:
10971226 kvm_vcpu_uninit(vcpu);
10981227 free_shadow_vcpu:
1099− kfree(vcpu_book3s->shadow_vcpu);
1100−free_vcpu:
1228+#ifdef CONFIG_KVM_BOOK3S_32
1229+ kfree(vcpu->arch.shadow_vcpu);
1230+free_vcpu3s:
1231+#endif
11011232 vfree(vcpu_book3s);
1233+free_vcpu:
1234+ kmem_cache_free(kvm_vcpu_cache, vcpu);
11021235 out:
11031236 return ERR_PTR(err);
11041237 }
11051238
1106−void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1239+static void kvmppc_core_vcpu_free_pr(struct kvm_vcpu *vcpu)
11071240 {
11081241 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
11091242
11101243 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
11111244 kvm_vcpu_uninit(vcpu);
1112− kfree(vcpu_book3s->shadow_vcpu);
1245+#ifdef CONFIG_KVM_BOOK3S_32
1246+ kfree(vcpu->arch.shadow_vcpu);
1247+#endif
11131248 vfree(vcpu_book3s);
1249+ kmem_cache_free(kvm_vcpu_cache, vcpu);
11141250 }
11151251
1116−int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1252+static int kvmppc_vcpu_run_pr(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
11171253 {
11181254 int ret;
11191255 struct thread_fp_state fp;
@@ -1216,8 +1352,8 @@ out: · 96 unmodified lines
12161352 /*
12171353 * Get (and clear) the dirty memory log for a memory slot.
12181354 */
1219−int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1220− struct kvm_dirty_log *log)
1355+static int kvm_vm_ioctl_get_dirty_log_pr(struct kvm *kvm,
1356+ struct kvm_dirty_log *log)
12211357 {
12221358 struct kvm_memory_slot *memslot;
12231359 struct kvm_vcpu *vcpu;
@@ -1252,67 +1388,100 @@ out: · 28 unmodified lines
12521388 return r;
12531389 }
12541390
1255−#ifdef CONFIG_PPC64
1256−int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1391+static void kvmppc_core_flush_memslot_pr(struct kvm *kvm,
1392+ struct kvm_memory_slot *memslot)
12571393 {
1258− info->flags = KVM_PPC_1T_SEGMENTS;
1259−
1260− /* SLB is always 64 entries */
1261− info->slb_size = 64;
1262−
1263− /* Standard 4k base page size segment */
1264− info->sps[0].page_shift = 12;
1265− info->sps[0].slb_enc = 0;
1266− info->sps[0].enc[0].page_shift = 12;
1267− info->sps[0].enc[0].pte_enc = 0;
1268−
1269− /* Standard 16M large page size segment */
1270− info->sps[1].page_shift = 24;
1271− info->sps[1].slb_enc = SLB_VSID_L;
1272− info->sps[1].enc[0].page_shift = 24;
1273− info->sps[1].enc[0].pte_enc = 0;
1394+ return;
1395+}
12741396
1397+static int kvmppc_core_prepare_memory_region_pr(struct kvm *kvm,
1398+ struct kvm_memory_slot *memslot,
1399+ struct kvm_userspace_memory_region *mem)
1400+{
12751401 return 0;
12761402 }
1277−#endif /* CONFIG_PPC64 */
12781403
1279−void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1280− struct kvm_memory_slot *dont)
1404+static void kvmppc_core_commit_memory_region_pr(struct kvm *kvm,
1405+ struct kvm_userspace_memory_region *mem,
1406+ const struct kvm_memory_slot *old)
12811407 {
1408+ return;
12821409 }
12831410
1284−int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1285− unsigned long npages)
1411+static void kvmppc_core_free_memslot_pr(struct kvm_memory_slot *free,
1412+ struct kvm_memory_slot *dont)
12861413 {
1287− return 0;
1414+ return;
12881415 }
12891416
1290−int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1291− struct kvm_memory_slot *memslot,
1292− struct kvm_userspace_memory_region *mem)
1417+static int kvmppc_core_create_memslot_pr(struct kvm_memory_slot *slot,
1418+ unsigned long npages)
12931419 {
12941420 return 0;
12951421 }
12961422
1297−void kvmppc_core_commit_memory_region(struct kvm *kvm,
1298− struct kvm_userspace_memory_region *mem,
1299− const struct kvm_memory_slot *old)
1423+
1424+#ifdef CONFIG_PPC64
1425+static int kvm_vm_ioctl_get_smmu_info_pr(struct kvm *kvm,
1426+ struct kvm_ppc_smmu_info *info)
13001427 {
1301−}
1428+ long int i;
1429+ struct kvm_vcpu *vcpu;
1430+
1431+ info->flags = 0;
1432+
1433+ /* SLB is always 64 entries */
1434+ info->slb_size = 64;
1435+
1436+ /* Standard 4k base page size segment */
1437+ info->sps[0].page_shift = 12;
1438+ info->sps[0].slb_enc = 0;
1439+ info->sps[0].enc[0].page_shift = 12;
1440+ info->sps[0].enc[0].pte_enc = 0;
1441+
1442+ /*
1443+ * 64k large page size.
1444+ * We only want to put this in if the CPUs we're emulating
1445+ * support it, but unfortunately we don't have a vcpu easily
1446+ * to hand here to test. Just pick the first vcpu, and if
1447+ * that doesn't exist yet, report the minimum capability,
1448+ * i.e., no 64k pages.
1449+ * 1T segment support goes along with 64k pages.
1450+ */
1451+ i = 1;
1452+ vcpu = kvm_get_vcpu(kvm, 0);
1453+ if (vcpu && (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE)) {
1454+ info->flags = KVM_PPC_1T_SEGMENTS;
1455+ info->sps[i].page_shift = 16;
1456+ info->sps[i].slb_enc = SLB_VSID_L | SLB_VSID_LP_01;
1457+ info->sps[i].enc[0].page_shift = 16;
1458+ info->sps[i].enc[0].pte_enc = 1;
1459+ ++i;
1460+ }
1461+
1462+ /* Standard 16M large page size segment */
1463+ info->sps[i].page_shift = 24;
1464+ info->sps[i].slb_enc = SLB_VSID_L;
1465+ info->sps[i].enc[0].page_shift = 24;
1466+ info->sps[i].enc[0].pte_enc = 0;
13021467
1303−void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
1468+ return 0;
1469+}
1470+#else
1471+static int kvm_vm_ioctl_get_smmu_info_pr(struct kvm *kvm,
1472+ struct kvm_ppc_smmu_info *info)
13041473 {
1474+ /* We should not get called */
1475+ BUG();
13051476 }
1477+#endif /* CONFIG_PPC64 */
13061478
13071479 static unsigned int kvm_global_user_count = 0;
13081480 static DEFINE_SPINLOCK(kvm_global_user_count_lock);
13091481
1310−int kvmppc_core_init_vm(struct kvm *kvm)
1482+static int kvmppc_core_init_vm_pr(struct kvm *kvm)
13111483 {
1312−#ifdef CONFIG_PPC64
1313− INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
1314− INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
1315−#endif
1484+ mutex_init(&kvm->arch.hpt_mutex);
13161485
13171486 if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
13181487 spin_lock(&kvm_global_user_count_lock);
@@ -1323,7 +1492,7 @@ int kvmppc_core_init_vm(struct kvm *kvm) · 4 unmodified lines
13231492 return 0;
13241493 }
13251494
1326−void kvmppc_core_destroy_vm(struct kvm *kvm)
1495+static void kvmppc_core_destroy_vm_pr(struct kvm *kvm)
13271496 {
13281497 #ifdef CONFIG_PPC64
13291498 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
@@ -1338,26 +1507,81 @@ void kvmppc_core_destroy_vm(struct kvm *kvm) · 8 unmodified lines
13381507 }
13391508 }
13401509
1341−static int kvmppc_book3s_init(void)
1510+static int kvmppc_core_check_processor_compat_pr(void)
13421511 {
1343− int r;
1512+ /* we are always compatible */
1513+ return 0;
1514+}
13441515
1345− r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1346− THIS_MODULE);
1516+static long kvm_arch_vm_ioctl_pr(struct file *filp,
1517+ unsigned int ioctl, unsigned long arg)
1518+{
1519+ return -ENOTTY;
1520+}
13471521
1348− if (r)
1522+static struct kvmppc_ops kvm_ops_pr = {
1523+ .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_pr,
1524+ .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_pr,
1525+ .get_one_reg = kvmppc_get_one_reg_pr,
1526+ .set_one_reg = kvmppc_set_one_reg_pr,
1527+ .vcpu_load = kvmppc_core_vcpu_load_pr,
1528+ .vcpu_put = kvmppc_core_vcpu_put_pr,
1529+ .set_msr = kvmppc_set_msr_pr,
1530+ .vcpu_run = kvmppc_vcpu_run_pr,
1531+ .vcpu_create = kvmppc_core_vcpu_create_pr,
1532+ .vcpu_free = kvmppc_core_vcpu_free_pr,
1533+ .check_requests = kvmppc_core_check_requests_pr,
1534+ .get_dirty_log = kvm_vm_ioctl_get_dirty_log_pr,
1535+ .flush_memslot = kvmppc_core_flush_memslot_pr,
1536+ .prepare_memory_region = kvmppc_core_prepare_memory_region_pr,
1537+ .commit_memory_region = kvmppc_core_commit_memory_region_pr,
1538+ .unmap_hva = kvm_unmap_hva_pr,
1539+ .unmap_hva_range = kvm_unmap_hva_range_pr,
1540+ .age_hva = kvm_age_hva_pr,
1541+ .test_age_hva = kvm_test_age_hva_pr,
1542+ .set_spte_hva = kvm_set_spte_hva_pr,
1543+ .mmu_destroy = kvmppc_mmu_destroy_pr,
1544+ .free_memslot = kvmppc_core_free_memslot_pr,
1545+ .create_memslot = kvmppc_core_create_memslot_pr,
1546+ .init_vm = kvmppc_core_init_vm_pr,
1547+ .destroy_vm = kvmppc_core_destroy_vm_pr,
1548+ .get_smmu_info = kvm_vm_ioctl_get_smmu_info_pr,
1549+ .emulate_op = kvmppc_core_emulate_op_pr,
1550+ .emulate_mtspr = kvmppc_core_emulate_mtspr_pr,
1551+ .emulate_mfspr = kvmppc_core_emulate_mfspr_pr,
1552+ .fast_vcpu_kick = kvm_vcpu_kick,
1553+ .arch_vm_ioctl = kvm_arch_vm_ioctl_pr,
1554+};
1555+
1556+
1557+int kvmppc_book3s_init_pr(void)
1558+{
1559+ int r;
1560+
1561+ r = kvmppc_core_check_processor_compat_pr();
1562+ if (r < 0)
13491563 return r;
13501564
1351− r = kvmppc_mmu_hpte_sysinit();
1565+ kvm_ops_pr.owner = THIS_MODULE;
1566+ kvmppc_pr_ops = &kvm_ops_pr;
13521567
1568+ r = kvmppc_mmu_hpte_sysinit();
13531569 return r;
13541570 }
13551571
1356−static void kvmppc_book3s_exit(void)
1572+void kvmppc_book3s_exit_pr(void)
13571573 {
1574+ kvmppc_pr_ops = NULL;
13581575 kvmppc_mmu_hpte_sysexit();
1359− kvm_exit();
13601576 }
13611577
1362−module_init(kvmppc_book3s_init);
1363−module_exit(kvmppc_book3s_exit);
1578+/*
1579+ * We only support separate modules for book3s 64
1580+ */
1581+#ifdef CONFIG_PPC_BOOK3S_64
1582+
1583+module_init(kvmppc_book3s_init_pr);
1584+module_exit(kvmppc_book3s_exit_pr);
1585+
1586+MODULE_LICENSE("GPL");
1587+#endif
arch/powerpc/kvm/book3s_pr_papr.c+37−15
@@ -21,6 +21,8 @@ · 20 unmodified lines
2121 #include <asm/kvm_ppc.h>
2222 #include <asm/kvm_book3s.h>
2323
24+#define HPTE_SIZE 16 /* bytes per HPT entry */
25+
2426 static unsigned long get_pteg_addr(struct kvm_vcpu *vcpu, long pte_index)
2527 {
2628 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
@@ -40,32 +42,41 @@ static int kvmppc_h_pr_enter(struct kvm_vcpu *vcpu) · 13 unmodified lines
4042 long pte_index = kvmppc_get_gpr(vcpu, 5);
4143 unsigned long pteg[2 * 8];
4244 unsigned long pteg_addr, i, *hpte;
45+ long int ret;
4346
47+ i = pte_index & 7;
4448 pte_index &= ~7UL;
4549 pteg_addr = get_pteg_addr(vcpu, pte_index);
4650
51+ mutex_lock(&vcpu->kvm->arch.hpt_mutex);
4752 copy_from_user(pteg, (void __user *)pteg_addr, sizeof(pteg));
4853 hpte = pteg;
4954
55+ ret = H_PTEG_FULL;
5056 if (likely((flags & H_EXACT) == 0)) {
51− pte_index &= ~7UL;
5257 for (i = 0; ; ++i) {
5358 if (i == 8)
54− return H_PTEG_FULL;
59+ goto done;
5560 if ((*hpte & HPTE_V_VALID) == 0)
5661 break;
5762 hpte += 2;
5863 }
5964 } else {
60− i = kvmppc_get_gpr(vcpu, 5) & 7UL;
6165 hpte += i * 2;
66+ if (*hpte & HPTE_V_VALID)
67+ goto done;
6268 }
6369
6470 hpte[0] = kvmppc_get_gpr(vcpu, 6);
6571 hpte[1] = kvmppc_get_gpr(vcpu, 7);
66− copy_to_user((void __user *)pteg_addr, pteg, sizeof(pteg));
67− kvmppc_set_gpr(vcpu, 3, H_SUCCESS);
72+ pteg_addr += i * HPTE_SIZE;
73+ copy_to_user((void __user *)pteg_addr, hpte, HPTE_SIZE);
6874 kvmppc_set_gpr(vcpu, 4, pte_index | i);
75+ ret = H_SUCCESS;
76+
77+ done:
78+ mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
79+ kvmppc_set_gpr(vcpu, 3, ret);
6980
7081 return EMULATE_DONE;
7182 }
@@ -77,26 +88,31 @@ static int kvmppc_h_pr_remove(struct kvm_vcpu *vcpu) · 5 unmodified lines
7788 unsigned long avpn = kvmppc_get_gpr(vcpu, 6);
7889 unsigned long v = 0, pteg, rb;
7990 unsigned long pte[2];
91+ long int ret;
8092
8193 pteg = get_pteg_addr(vcpu, pte_index);
94+ mutex_lock(&vcpu->kvm->arch.hpt_mutex);
8295 copy_from_user(pte, (void __user *)pteg, sizeof(pte));
8396
97+ ret = H_NOT_FOUND;
8498 if ((pte[0] & HPTE_V_VALID) == 0 ||
8599 ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn) ||
86− ((flags & H_ANDCOND) && (pte[0] & avpn) != 0)) {
87− kvmppc_set_gpr(vcpu, 3, H_NOT_FOUND);
88− return EMULATE_DONE;
89− }
100+ ((flags & H_ANDCOND) && (pte[0] & avpn) != 0))
101+ goto done;
90102
91103 copy_to_user((void __user *)pteg, &v, sizeof(v));
92104
93105 rb = compute_tlbie_rb(pte[0], pte[1], pte_index);
94106 vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
95107
96− kvmppc_set_gpr(vcpu, 3, H_SUCCESS);
108+ ret = H_SUCCESS;
97109 kvmppc_set_gpr(vcpu, 4, pte[0]);
98110 kvmppc_set_gpr(vcpu, 5, pte[1]);
99111
112+ done:
113+ mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
114+ kvmppc_set_gpr(vcpu, 3, ret);
115+
100116 return EMULATE_DONE;
101117 }
102118
@@ -124,6 +140,7 @@ static int kvmppc_h_pr_bulk_remove(struct kvm_vcpu *vcpu) · 21 unmodified lines
124140 int paramnr = 4;
125141 int ret = H_SUCCESS;
126142
143+ mutex_lock(&vcpu->kvm->arch.hpt_mutex);
127144 for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
128145 unsigned long tsh = kvmppc_get_gpr(vcpu, paramnr+(2*i));
129146 unsigned long tsl = kvmppc_get_gpr(vcpu, paramnr+(2*i)+1);
@@ -172,6 +189,7 @@ static int kvmppc_h_pr_bulk_remove(struct kvm_vcpu *vcpu) · 42 unmodified lines
172189 }
173190 kvmppc_set_gpr(vcpu, paramnr+(2*i), tsh);
174191 }
192+ mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
175193 kvmppc_set_gpr(vcpu, 3, ret);
176194
177195 return EMULATE_DONE;
@@ -184,15 +202,16 @@ static int kvmppc_h_pr_protect(struct kvm_vcpu *vcpu) · 6 unmodified lines
184202 unsigned long avpn = kvmppc_get_gpr(vcpu, 6);
185203 unsigned long rb, pteg, r, v;
186204 unsigned long pte[2];
205+ long int ret;
187206
188207 pteg = get_pteg_addr(vcpu, pte_index);
208+ mutex_lock(&vcpu->kvm->arch.hpt_mutex);
189209 copy_from_user(pte, (void __user *)pteg, sizeof(pte));
190210
211+ ret = H_NOT_FOUND;
191212 if ((pte[0] & HPTE_V_VALID) == 0 ||
192− ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn)) {
193− kvmppc_set_gpr(vcpu, 3, H_NOT_FOUND);
194− return EMULATE_DONE;
195− }
213+ ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn))
214+ goto done;
196215
197216 v = pte[0];
198217 r = pte[1];
@@ -207,8 +226,11 @@ static int kvmppc_h_pr_protect(struct kvm_vcpu *vcpu) · 8 unmodified lines
207226 rb = compute_tlbie_rb(v, r, pte_index);
208227 vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
209228 copy_to_user((void __user *)pteg, pte, sizeof(pte));
229+ ret = H_SUCCESS;
210230
211− kvmppc_set_gpr(vcpu, 3, H_SUCCESS);
231+ done:
232+ mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
233+ kvmppc_set_gpr(vcpu, 3, ret);
212234
213235 return EMULATE_DONE;
214236 }
arch/powerpc/kvm/book3s_rmhandlers.S+5−27
@@ -38,32 +38,6 @@ · 37 unmodified lines
3838
3939 #define FUNC(name) GLUE(.,name)
4040
41− .globl kvmppc_skip_interrupt
42−kvmppc_skip_interrupt:
43− /*
44− * Here all GPRs are unchanged from when the interrupt happened
45− * except for r13, which is saved in SPRG_SCRATCH0.
46− */
47− mfspr r13, SPRN_SRR0
48− addi r13, r13, 4
49− mtspr SPRN_SRR0, r13
50− GET_SCRATCH0(r13)
51− rfid
52− b .
53−
54− .globl kvmppc_skip_Hinterrupt
55−kvmppc_skip_Hinterrupt:
56− /*
57− * Here all GPRs are unchanged from when the interrupt happened
58− * except for r13, which is saved in SPRG_SCRATCH0.
59− */
60− mfspr r13, SPRN_HSRR0
61− addi r13, r13, 4
62− mtspr SPRN_HSRR0, r13
63− GET_SCRATCH0(r13)
64− hrfid
65− b .
66−
6741 #elif defined(CONFIG_PPC_BOOK3S_32)
6842
6943 #define FUNC(name) name
@@ -179,11 +153,15 @@ _GLOBAL(kvmppc_entry_trampoline) · 109 unmodified lines
179153
180154 li r6, MSR_IR | MSR_DR
181155 andc r6, r5, r6 /* Clear DR and IR in MSR value */
156+#ifdef CONFIG_PPC_BOOK3S_32
182157 /*
183158 * Set EE in HOST_MSR so that it's enabled when we get into our
184− * C exit handler function
159+ * C exit handler function. On 64-bit we delay enabling
160+ * interrupts until we have finished transferring stuff
161+ * to or from the PACA.
185162 */
186163 ori r5, r5, MSR_EE
164+#endif
187165 mtsrr0 r7
188166 mtsrr1 r6
189167 RFI
arch/powerpc/kvm/book3s_rtas.c+1−0
@@ -260,6 +260,7 @@ fail: · 259 unmodified lines
260260 */
261261 return rc;
262262 }
263+EXPORT_SYMBOL_GPL(kvmppc_rtas_hcall);
263264
264265 void kvmppc_rtas_tokens_free(struct kvm *kvm)
265266 {
arch/powerpc/kvm/book3s_segment.S+2−2
@@ -161,8 +161,8 @@ kvmppc_handler_trampoline_enter_end: · 160 unmodified lines
161161 .global kvmppc_handler_trampoline_exit
162162 kvmppc_handler_trampoline_exit:
163163
164−.global kvmppc_interrupt
165−kvmppc_interrupt:
164+.global kvmppc_interrupt_pr
165+kvmppc_interrupt_pr:
166166
167167 /* Register usage at this point:
168168 *
arch/powerpc/kvm/book3s_xics.c+4−3
@@ -818,7 +818,7 @@ int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req) · 817 unmodified lines
818818 }
819819
820820 /* Check for real mode returning too hard */
821− if (xics->real_mode)
821+ if (xics->real_mode && is_kvmppc_hv_enabled(vcpu->kvm))
822822 return kvmppc_xics_rm_complete(vcpu, req);
823823
824824 switch (req) {
@@ -840,6 +840,7 @@ int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req) · 15 unmodified lines
840840
841841 return rc;
842842 }
843+EXPORT_SYMBOL_GPL(kvmppc_xics_hcall);
843844
844845
845846 /* -- Initialisation code etc. -- */
@@ -1250,13 +1251,13 @@ static int kvmppc_xics_create(struct kvm_device *dev, u32 type) · 404 unmodified lines
12501251
12511252 xics_debugfs_init(xics);
12521253
1253−#ifdef CONFIG_KVM_BOOK3S_64_HV
1254+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
12541255 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
12551256 /* Enable real mode support */
12561257 xics->real_mode = ENABLE_REALMODE;
12571258 xics->real_mode_dbg = DEBUG_REALMODE;
12581259 }
1259−#endif /* CONFIG_KVM_BOOK3S_64_HV */
1260+#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
12601261
12611262 return 0;
12621263 }
arch/powerpc/kvm/booke.c+299−38
@@ -40,7 +40,9 @@ · 39 unmodified lines
4040
4141 #include "timing.h"
4242 #include "booke.h"
43−#include "trace.h"
43+
44+#define CREATE_TRACE_POINTS
45+#include "trace_booke.h"
4446
4547 unsigned long kvmppc_booke_handlers;
4648
@@ -133,6 +135,29 @@ static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu) · 86 unmodified lines
133135 #endif
134136 }
135137
138+static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
139+{
140+ /* Synchronize guest's desire to get debug interrupts into shadow MSR */
141+#ifndef CONFIG_KVM_BOOKE_HV
142+ vcpu->arch.shadow_msr &= ~MSR_DE;
143+ vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
144+#endif
145+
146+ /* Force enable debug interrupts when user space wants to debug */
147+ if (vcpu->guest_debug) {
148+#ifdef CONFIG_KVM_BOOKE_HV
149+ /*
150+ * Since there is no shadow MSR, sync MSR_DE into the guest
151+ * visible MSR.
152+ */
153+ vcpu->arch.shared->msr |= MSR_DE;
154+#else
155+ vcpu->arch.shadow_msr |= MSR_DE;
156+ vcpu->arch.shared->msr &= ~MSR_DE;
157+#endif
158+ }
159+}
160+
136161 /*
137162 * Helper function for "full" MSR writes. No need to call this if only
138163 * EE/CE/ME/DE/RI are changing.
@@ -150,6 +175,7 @@ void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr) · 11 unmodified lines
150175 kvmppc_mmu_msr_notify(vcpu, old_msr);
151176 kvmppc_vcpu_sync_spe(vcpu);
152177 kvmppc_vcpu_sync_fpu(vcpu);
178+ kvmppc_vcpu_sync_debug(vcpu);
153179 }
154180
155181 static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
@@ -655,6 +681,7 @@ int kvmppc_core_check_requests(struct kvm_vcpu *vcpu) · 499 unmodified lines
655681 int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
656682 {
657683 int ret, s;
684+ struct thread_struct thread;
658685 #ifdef CONFIG_PPC_FPU
659686 struct thread_fp_state fp;
660687 int fpexc_mode;
@@ -695,6 +722,12 @@ int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) · 34 unmodified lines
695722 kvmppc_load_guest_fp(vcpu);
696723 #endif
697724
725+ /* Switch to guest debug context */
726+ thread.debug = vcpu->arch.shadow_dbg_reg;
727+ switch_booke_debug_regs(&thread);
728+ thread.debug = current->thread.debug;
729+ current->thread.debug = vcpu->arch.shadow_dbg_reg;
730+
698731 kvmppc_fix_ee_before_entry();
699732
700733 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
@@ -702,6 +735,10 @@ int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) · 1 unmodified line
702735 /* No need for kvm_guest_exit. It's done in handle_exit.
703736 We also get here with interrupts enabled. */
704737
738+ /* Switch back to user space debug context */
739+ switch_booke_debug_regs(&thread);
740+ current->thread.debug = thread.debug;
741+
705742 #ifdef CONFIG_PPC_FPU
706743 kvmppc_save_guest_fp(vcpu);
707744
@@ -757,6 +794,30 @@ static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) · 49 unmodified lines
757794 }
758795 }
759796
797+static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
798+{
799+ struct debug_reg *dbg_reg = &(vcpu->arch.shadow_dbg_reg);
800+ u32 dbsr = vcpu->arch.dbsr;
801+
802+ run->debug.arch.status = 0;
803+ run->debug.arch.address = vcpu->arch.pc;
804+
805+ if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
806+ run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
807+ } else {
808+ if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
809+ run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
810+ else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
811+ run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
812+ if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
813+ run->debug.arch.address = dbg_reg->dac1;
814+ else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
815+ run->debug.arch.address = dbg_reg->dac2;
816+ }
817+
818+ return RESUME_HOST;
819+}
820+
760821 static void kvmppc_fill_pt_regs(struct pt_regs *regs)
761822 {
762823 ulong r1, ip, msr, lr;
@@ -817,6 +878,11 @@ static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu, · 54 unmodified lines
817878 case BOOKE_INTERRUPT_CRITICAL:
818879 unknown_exception(&regs);
819880 break;
881+ case BOOKE_INTERRUPT_DEBUG:
882+ /* Save DBSR before preemption is enabled */
883+ vcpu->arch.dbsr = mfspr(SPRN_DBSR);
884+ kvmppc_clear_dbsr();
885+ break;
820886 }
821887 }
822888
@@ -1134,18 +1200,10 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, · 311 unmodified lines
11341200 }
11351201
11361202 case BOOKE_INTERRUPT_DEBUG: {
1137− u32 dbsr;
1138−
1139− vcpu->arch.pc = mfspr(SPRN_CSRR0);
1140−
1141− /* clear IAC events in DBSR register */
1142− dbsr = mfspr(SPRN_DBSR);
1143− dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
1144− mtspr(SPRN_DBSR, dbsr);
1145−
1146− run->exit_reason = KVM_EXIT_DEBUG;
1203+ r = kvmppc_handle_debug(run, vcpu);
1204+ if (r == RESUME_HOST)
1205+ run->exit_reason = KVM_EXIT_DEBUG;
11471206 kvmppc_account_exit(vcpu, DEBUG_EXITS);
1148− r = RESUME_HOST;
11491207 break;
11501208 }
11511209
@@ -1196,7 +1254,7 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) · 44 unmodified lines
11961254 kvmppc_set_msr(vcpu, 0);
11971255
11981256 #ifndef CONFIG_KVM_BOOKE_HV
1199− vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
1257+ vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
12001258 vcpu->arch.shadow_pid = 1;
12011259 vcpu->arch.shared->msr = 0;
12021260 #endif
@@ -1358,7 +1416,7 @@ static int set_sregs_arch206(struct kvm_vcpu *vcpu, · 155 unmodified lines
13581416 return 0;
13591417 }
13601418
1361−void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1419+int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
13621420 {
13631421 sregs->u.e.features |= KVM_SREGS_E_IVOR;
13641422
@@ -1378,6 +1436,7 @@ void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) · 13 unmodified lines
13781436 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
13791437 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
13801438 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1439+ return 0;
13811440 }
13821441
13831442 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
@@ -1412,8 +1471,7 @@ int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, · 28 unmodified lines
14121471
14131472 get_sregs_base(vcpu, sregs);
14141473 get_sregs_arch206(vcpu, sregs);
1415− kvmppc_core_get_sregs(vcpu, sregs);
1416− return 0;
1474+ return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
14171475 }
14181476
14191477 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
@@ -1432,7 +1490,7 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · 12 unmodified lines
14321490 if (ret < 0)
14331491 return ret;
14341492
1435− return kvmppc_core_set_sregs(vcpu, sregs);
1493+ return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
14361494 }
14371495
14381496 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
@@ -1440,7 +1498,6 @@ int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 1 unmodified line
14401498 int r = 0;
14411499 union kvmppc_one_reg val;
14421500 int size;
1443− long int i;
14441501
14451502 size = one_reg_size(reg->id);
14461503 if (size > sizeof(val))
@@ -1448,16 +1505,24 @@ int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 1 unmodified line
14481505
14491506 switch (reg->id) {
14501507 case KVM_REG_PPC_IAC1:
1508+ val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac1);
1509+ break;
14511510 case KVM_REG_PPC_IAC2:
1511+ val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac2);
1512+ break;
1513+#if CONFIG_PPC_ADV_DEBUG_IACS > 2
14521514 case KVM_REG_PPC_IAC3:
1515+ val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac3);
1516+ break;
14531517 case KVM_REG_PPC_IAC4:
1454− i = reg->id - KVM_REG_PPC_IAC1;
1455− val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac[i]);
1518+ val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac4);
14561519 break;
1520+#endif
14571521 case KVM_REG_PPC_DAC1:
1522+ val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac1);
1523+ break;
14581524 case KVM_REG_PPC_DAC2:
1459− i = reg->id - KVM_REG_PPC_DAC1;
1460− val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac[i]);
1525+ val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac2);
14611526 break;
14621527 case KVM_REG_PPC_EPR: {
14631528 u32 epr = get_guest_epr(vcpu);
@@ -1476,10 +1541,13 @@ int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 12 unmodified lines
14761541 val = get_reg_val(reg->id, vcpu->arch.tsr);
14771542 break;
14781543 case KVM_REG_PPC_DEBUG_INST:
1479− val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV);
1544+ val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV_DEBUG);
1545+ break;
1546+ case KVM_REG_PPC_VRSAVE:
1547+ val = get_reg_val(reg->id, vcpu->arch.vrsave);
14801548 break;
14811549 default:
1482− r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1550+ r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, reg->id, &val);
14831551 break;
14841552 }
14851553
@@ -1497,7 +1565,6 @@ int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 11 unmodified lines
14971565 int r = 0;
14981566 union kvmppc_one_reg val;
14991567 int size;
1500− long int i;
15011568
15021569 size = one_reg_size(reg->id);
15031570 if (size > sizeof(val))
@@ -1508,16 +1575,24 @@ int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 4 unmodified lines
15081575
15091576 switch (reg->id) {
15101577 case KVM_REG_PPC_IAC1:
1578+ vcpu->arch.dbg_reg.iac1 = set_reg_val(reg->id, val);
1579+ break;
15111580 case KVM_REG_PPC_IAC2:
1581+ vcpu->arch.dbg_reg.iac2 = set_reg_val(reg->id, val);
1582+ break;
1583+#if CONFIG_PPC_ADV_DEBUG_IACS > 2
15121584 case KVM_REG_PPC_IAC3:
1585+ vcpu->arch.dbg_reg.iac3 = set_reg_val(reg->id, val);
1586+ break;
15131587 case KVM_REG_PPC_IAC4:
1514− i = reg->id - KVM_REG_PPC_IAC1;
1515− vcpu->arch.dbg_reg.iac[i] = set_reg_val(reg->id, val);
1588+ vcpu->arch.dbg_reg.iac4 = set_reg_val(reg->id, val);
15161589 break;
1590+#endif
15171591 case KVM_REG_PPC_DAC1:
1592+ vcpu->arch.dbg_reg.dac1 = set_reg_val(reg->id, val);
1593+ break;
15181594 case KVM_REG_PPC_DAC2:
1519− i = reg->id - KVM_REG_PPC_DAC1;
1520− vcpu->arch.dbg_reg.dac[i] = set_reg_val(reg->id, val);
1595+ vcpu->arch.dbg_reg.dac2 = set_reg_val(reg->id, val);
15211596 break;
15221597 case KVM_REG_PPC_EPR: {
15231598 u32 new_epr = set_reg_val(reg->id, val);
@@ -1551,20 +1626,17 @@ int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) · 27 unmodified lines
15511626 kvmppc_set_tcr(vcpu, tcr);
15521627 break;
15531628 }
1629+ case KVM_REG_PPC_VRSAVE:
1630+ vcpu->arch.vrsave = set_reg_val(reg->id, val);
1631+ break;
15541632 default:
1555− r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1633+ r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, reg->id, &val);
15561634 break;
15571635 }
15581636
15591637 return r;
15601638 }
15611639
1562−int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1563− struct kvm_guest_debug *dbg)
1564−{
1565− return -EINVAL;
1566−}
1567−
15681640 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
15691641 {
15701642 return -ENOTSUPP;
@@ -1589,12 +1661,12 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) · 18 unmodified lines
15891661 return -ENOTSUPP;
15901662 }
15911663
1592−void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1664+void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
15931665 struct kvm_memory_slot *dont)
15941666 {
15951667 }
15961668
1597−int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1669+int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
15981670 unsigned long npages)
15991671 {
16001672 return 0;
@@ -1670,6 +1742,157 @@ void kvmppc_decrementer_func(unsigned long data) · 69 unmodified lines
16701742 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
16711743 }
16721744
1745+static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1746+ uint64_t addr, int index)
1747+{
1748+ switch (index) {
1749+ case 0:
1750+ dbg_reg->dbcr0 |= DBCR0_IAC1;
1751+ dbg_reg->iac1 = addr;
1752+ break;
1753+ case 1:
1754+ dbg_reg->dbcr0 |= DBCR0_IAC2;
1755+ dbg_reg->iac2 = addr;
1756+ break;
1757+#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1758+ case 2:
1759+ dbg_reg->dbcr0 |= DBCR0_IAC3;
1760+ dbg_reg->iac3 = addr;
1761+ break;
1762+ case 3:
1763+ dbg_reg->dbcr0 |= DBCR0_IAC4;
1764+ dbg_reg->iac4 = addr;
1765+ break;
1766+#endif
1767+ default:
1768+ return -EINVAL;
1769+ }
1770+
1771+ dbg_reg->dbcr0 |= DBCR0_IDM;
1772+ return 0;
1773+}
1774+
1775+static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1776+ int type, int index)
1777+{
1778+ switch (index) {
1779+ case 0:
1780+ if (type & KVMPPC_DEBUG_WATCH_READ)
1781+ dbg_reg->dbcr0 |= DBCR0_DAC1R;
1782+ if (type & KVMPPC_DEBUG_WATCH_WRITE)
1783+ dbg_reg->dbcr0 |= DBCR0_DAC1W;
1784+ dbg_reg->dac1 = addr;
1785+ break;
1786+ case 1:
1787+ if (type & KVMPPC_DEBUG_WATCH_READ)
1788+ dbg_reg->dbcr0 |= DBCR0_DAC2R;
1789+ if (type & KVMPPC_DEBUG_WATCH_WRITE)
1790+ dbg_reg->dbcr0 |= DBCR0_DAC2W;
1791+ dbg_reg->dac2 = addr;
1792+ break;
1793+ default:
1794+ return -EINVAL;
1795+ }
1796+
1797+ dbg_reg->dbcr0 |= DBCR0_IDM;
1798+ return 0;
1799+}
1800+void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
1801+{
1802+ /* XXX: Add similar MSR protection for BookE-PR */
1803+#ifdef CONFIG_KVM_BOOKE_HV
1804+ BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1805+ if (set) {
1806+ if (prot_bitmap & MSR_UCLE)
1807+ vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1808+ if (prot_bitmap & MSR_DE)
1809+ vcpu->arch.shadow_msrp |= MSRP_DEP;
1810+ if (prot_bitmap & MSR_PMM)
1811+ vcpu->arch.shadow_msrp |= MSRP_PMMP;
1812+ } else {
1813+ if (prot_bitmap & MSR_UCLE)
1814+ vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1815+ if (prot_bitmap & MSR_DE)
1816+ vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1817+ if (prot_bitmap & MSR_PMM)
1818+ vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1819+ }
1820+#endif
1821+}
1822+
1823+int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1824+ struct kvm_guest_debug *dbg)
1825+{
1826+ struct debug_reg *dbg_reg;
1827+ int n, b = 0, w = 0;
1828+
1829+ if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
1830+ vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1831+ vcpu->guest_debug = 0;
1832+ kvm_guest_protect_msr(vcpu, MSR_DE, false);
1833+ return 0;
1834+ }
1835+
1836+ kvm_guest_protect_msr(vcpu, MSR_DE, true);
1837+ vcpu->guest_debug = dbg->control;
1838+ vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1839+ /* Set DBCR0_EDM in guest visible DBCR0 register. */
1840+ vcpu->arch.dbg_reg.dbcr0 = DBCR0_EDM;
1841+
1842+ if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1843+ vcpu->arch.shadow_dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1844+
1845+ /* Code below handles only HW breakpoints */
1846+ dbg_reg = &(vcpu->arch.shadow_dbg_reg);
1847+
1848+#ifdef CONFIG_KVM_BOOKE_HV
1849+ /*
1850+ * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
1851+ * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
1852+ */
1853+ dbg_reg->dbcr1 = 0;
1854+ dbg_reg->dbcr2 = 0;
1855+#else
1856+ /*
1857+ * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
1858+ * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
1859+ * is set.
1860+ */
1861+ dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
1862+ DBCR1_IAC4US;
1863+ dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
1864+#endif
1865+
1866+ if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1867+ return 0;
1868+
1869+ for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
1870+ uint64_t addr = dbg->arch.bp[n].addr;
1871+ uint32_t type = dbg->arch.bp[n].type;
1872+
1873+ if (type == KVMPPC_DEBUG_NONE)
1874+ continue;
1875+
1876+ if (type & !(KVMPPC_DEBUG_WATCH_READ |
1877+ KVMPPC_DEBUG_WATCH_WRITE |
1878+ KVMPPC_DEBUG_BREAKPOINT))
1879+ return -EINVAL;
1880+
1881+ if (type & KVMPPC_DEBUG_BREAKPOINT) {
1882+ /* Setting H/W breakpoint */
1883+ if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
1884+ return -EINVAL;
1885+ } else {
1886+ /* Setting H/W watchpoint */
1887+ if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
1888+ type, w++))
1889+ return -EINVAL;
1890+ }
1891+ }
1892+
1893+ return 0;
1894+}
1895+
16731896 void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
16741897 {
16751898 vcpu->cpu = smp_processor_id();
@@ -1680,6 +1903,44 @@ void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu) · 4 unmodified lines
16801903 {
16811904 current->thread.kvm_vcpu = NULL;
16821905 vcpu->cpu = -1;
1906+
1907+ /* Clear pending debug event in DBSR */
1908+ kvmppc_clear_dbsr();
1909+}
1910+
1911+void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1912+{
1913+ vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
1914+}
1915+
1916+int kvmppc_core_init_vm(struct kvm *kvm)
1917+{
1918+ return kvm->arch.kvm_ops->init_vm(kvm);
1919+}
1920+
1921+struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1922+{
1923+ return kvm->arch.kvm_ops->vcpu_create(kvm, id);
1924+}
1925+
1926+void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1927+{
1928+ vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
1929+}
1930+
1931+void kvmppc_core_destroy_vm(struct kvm *kvm)
1932+{
1933+ kvm->arch.kvm_ops->destroy_vm(kvm);
1934+}
1935+
1936+void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1937+{
1938+ vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
1939+}
1940+
1941+void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
1942+{
1943+ vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
16831944 }
16841945
16851946 int __init kvmppc_booke_init(void)
arch/powerpc/kvm/booke.h+29−0
@@ -99,6 +99,30 @@ enum int_class { · 98 unmodified lines
9999
100100 void kvmppc_set_pending_interrupt(struct kvm_vcpu *vcpu, enum int_class type);
101101
102+extern void kvmppc_mmu_destroy_44x(struct kvm_vcpu *vcpu);
103+extern int kvmppc_core_emulate_op_44x(struct kvm_run *run, struct kvm_vcpu *vcpu,
104+ unsigned int inst, int *advance);
105+extern int kvmppc_core_emulate_mtspr_44x(struct kvm_vcpu *vcpu, int sprn,
106+ ulong spr_val);
107+extern int kvmppc_core_emulate_mfspr_44x(struct kvm_vcpu *vcpu, int sprn,
108+ ulong *spr_val);
109+extern void kvmppc_mmu_destroy_e500(struct kvm_vcpu *vcpu);
110+extern int kvmppc_core_emulate_op_e500(struct kvm_run *run,
111+ struct kvm_vcpu *vcpu,
112+ unsigned int inst, int *advance);
113+extern int kvmppc_core_emulate_mtspr_e500(struct kvm_vcpu *vcpu, int sprn,
114+ ulong spr_val);
115+extern int kvmppc_core_emulate_mfspr_e500(struct kvm_vcpu *vcpu, int sprn,
116+ ulong *spr_val);
117+extern void kvmppc_mmu_destroy_e500(struct kvm_vcpu *vcpu);
118+extern int kvmppc_core_emulate_op_e500(struct kvm_run *run,
119+ struct kvm_vcpu *vcpu,
120+ unsigned int inst, int *advance);
121+extern int kvmppc_core_emulate_mtspr_e500(struct kvm_vcpu *vcpu, int sprn,
122+ ulong spr_val);
123+extern int kvmppc_core_emulate_mfspr_e500(struct kvm_vcpu *vcpu, int sprn,
124+ ulong *spr_val);
125+
102126 /*
103127 * Load up guest vcpu FP state if it's needed.
104128 * It also set the MSR_FP in thread so that host know
@@ -129,4 +153,9 @@ static inline void kvmppc_save_guest_fp(struct kvm_vcpu *vcpu) · 24 unmodified lines
129153 giveup_fpu(current);
130154 #endif
131155 }
156+
157+static inline void kvmppc_clear_dbsr(void)
158+{
159+ mtspr(SPRN_DBSR, mfspr(SPRN_DBSR));
160+}
132161 #endif /* __KVM_BOOKE_H__ */
arch/powerpc/kvm/e500.c+44−15
@@ -305,7 +305,7 @@ void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu) · 304 unmodified lines
305305 {
306306 }
307307
308−void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
308+static void kvmppc_core_vcpu_load_e500(struct kvm_vcpu *vcpu, int cpu)
309309 {
310310 kvmppc_booke_vcpu_load(vcpu, cpu);
311311
@@ -313,7 +313,7 @@ void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · 1 unmodified line
313313 kvmppc_e500_recalc_shadow_pid(to_e500(vcpu));
314314 }
315315
316−void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
316+static void kvmppc_core_vcpu_put_e500(struct kvm_vcpu *vcpu)
317317 {
318318 #ifdef CONFIG_SPE
319319 if (vcpu->arch.shadow_msr & MSR_SPE)
@@ -367,7 +367,8 @@ int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu) · 47 unmodified lines
367367 return 0;
368368 }
369369
370−void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
370+static int kvmppc_core_get_sregs_e500(struct kvm_vcpu *vcpu,
371+ struct kvm_sregs *sregs)
371372 {
372373 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
373374
@@ -388,9 +389,11 @@ void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) · 14 unmodified lines
388389
389390 kvmppc_get_sregs_ivor(vcpu, sregs);
390391 kvmppc_get_sregs_e500_tlb(vcpu, sregs);
392+ return 0;
391393 }
392394
393−int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
395+static int kvmppc_core_set_sregs_e500(struct kvm_vcpu *vcpu,
396+ struct kvm_sregs *sregs)
394397 {
395398 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
396399 int ret;
@@ -425,21 +428,22 @@ int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) · 28 unmodified lines
425428 return kvmppc_set_sregs_ivor(vcpu, sregs);
426429 }
427430
428−int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
429− union kvmppc_one_reg *val)
431+static int kvmppc_get_one_reg_e500(struct kvm_vcpu *vcpu, u64 id,
432+ union kvmppc_one_reg *val)
430433 {
431434 int r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
432435 return r;
433436 }
434437
435−int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
436− union kvmppc_one_reg *val)
438+static int kvmppc_set_one_reg_e500(struct kvm_vcpu *vcpu, u64 id,
439+ union kvmppc_one_reg *val)
437440 {
438441 int r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
439442 return r;
440443 }
441444
442−struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
445+static struct kvm_vcpu *kvmppc_core_vcpu_create_e500(struct kvm *kvm,
446+ unsigned int id)
443447 {
444448 struct kvmppc_vcpu_e500 *vcpu_e500;
445449 struct kvm_vcpu *vcpu;
@@ -481,7 +485,7 @@ out: · 35 unmodified lines
481485 return ERR_PTR(err);
482486 }
483487
484−void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
488+static void kvmppc_core_vcpu_free_e500(struct kvm_vcpu *vcpu)
485489 {
486490 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
487491
@@ -492,15 +496,32 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) · 4 unmodified lines
492496 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
493497 }
494498
495−int kvmppc_core_init_vm(struct kvm *kvm)
499+static int kvmppc_core_init_vm_e500(struct kvm *kvm)
496500 {
497501 return 0;
498502 }
499503
500−void kvmppc_core_destroy_vm(struct kvm *kvm)
504+static void kvmppc_core_destroy_vm_e500(struct kvm *kvm)
501505 {
502506 }
503507
508+static struct kvmppc_ops kvm_ops_e500 = {
509+ .get_sregs = kvmppc_core_get_sregs_e500,
510+ .set_sregs = kvmppc_core_set_sregs_e500,
511+ .get_one_reg = kvmppc_get_one_reg_e500,
512+ .set_one_reg = kvmppc_set_one_reg_e500,
513+ .vcpu_load = kvmppc_core_vcpu_load_e500,
514+ .vcpu_put = kvmppc_core_vcpu_put_e500,
515+ .vcpu_create = kvmppc_core_vcpu_create_e500,
516+ .vcpu_free = kvmppc_core_vcpu_free_e500,
517+ .mmu_destroy = kvmppc_mmu_destroy_e500,
518+ .init_vm = kvmppc_core_init_vm_e500,
519+ .destroy_vm = kvmppc_core_destroy_vm_e500,
520+ .emulate_op = kvmppc_core_emulate_op_e500,
521+ .emulate_mtspr = kvmppc_core_emulate_mtspr_e500,
522+ .emulate_mfspr = kvmppc_core_emulate_mfspr_e500,
523+};
524+
504525 static int __init kvmppc_e500_init(void)
505526 {
506527 int r, i;
@@ -512,11 +533,11 @@ static int __init kvmppc_e500_init(void) · 5 unmodified lines
512533
513534 r = kvmppc_core_check_processor_compat();
514535 if (r)
515− return r;
536+ goto err_out;
516537
517538 r = kvmppc_booke_init();
518539 if (r)
519− return r;
540+ goto err_out;
520541
521542 /* copy extra E500 exception handlers */
522543 ivor[0] = mfspr(SPRN_IVOR32);
@@ -534,11 +555,19 @@ static int __init kvmppc_e500_init(void) · 11 unmodified lines
534555 flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
535556 ivor[max_ivor] + handler_len);
536557
537− return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
558+ r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
559+ if (r)
560+ goto err_out;
561+ kvm_ops_e500.owner = THIS_MODULE;
562+ kvmppc_pr_ops = &kvm_ops_e500;
563+
564+err_out:
565+ return r;
538566 }
539567
540568 static void __exit kvmppc_e500_exit(void)
541569 {
570+ kvmppc_pr_ops = NULL;
542571 kvmppc_booke_exit();
543572 }
544573
arch/powerpc/kvm/e500.h+1−1
@@ -117,7 +117,7 @@ static inline struct kvmppc_vcpu_e500 *to_e500(struct kvm_vcpu *vcpu) · 116 unmodified lines
117117 #define E500_TLB_USER_PERM_MASK (MAS3_UX|MAS3_UR|MAS3_UW)
118118 #define E500_TLB_SUPER_PERM_MASK (MAS3_SX|MAS3_SR|MAS3_SW)
119119 #define MAS2_ATTRIB_MASK \
120− (MAS2_X0 | MAS2_X1)
120+ (MAS2_X0 | MAS2_X1 | MAS2_E | MAS2_G)
121121 #define MAS3_ATTRIB_MASK \
122122 (MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3 \
123123 | E500_TLB_USER_PERM_MASK | E500_TLB_SUPER_PERM_MASK)
arch/powerpc/kvm/e500_emulate.c+30−4
@@ -26,6 +26,7 @@ · 25 unmodified lines
2626 #define XOP_TLBRE 946
2727 #define XOP_TLBWE 978
2828 #define XOP_TLBILX 18
29+#define XOP_EHPRIV 270
2930
3031 #ifdef CONFIG_KVM_E500MC
3132 static int dbell2prio(ulong param)
@@ -82,8 +83,28 @@ static int kvmppc_e500_emul_msgsnd(struct kvm_vcpu *vcpu, int rb) · 50 unmodified lines
8283 }
8384 #endif
8485
85−int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
86− unsigned int inst, int *advance)
86+static int kvmppc_e500_emul_ehpriv(struct kvm_run *run, struct kvm_vcpu *vcpu,
87+ unsigned int inst, int *advance)
88+{
89+ int emulated = EMULATE_DONE;
90+
91+ switch (get_oc(inst)) {
92+ case EHPRIV_OC_DEBUG:
93+ run->exit_reason = KVM_EXIT_DEBUG;
94+ run->debug.arch.address = vcpu->arch.pc;
95+ run->debug.arch.status = 0;
96+ kvmppc_account_exit(vcpu, DEBUG_EXITS);
97+ emulated = EMULATE_EXIT_USER;
98+ *advance = 0;
99+ break;
100+ default:
101+ emulated = EMULATE_FAIL;
102+ }
103+ return emulated;
104+}
105+
106+int kvmppc_core_emulate_op_e500(struct kvm_run *run, struct kvm_vcpu *vcpu,
107+ unsigned int inst, int *advance)
87108 {
88109 int emulated = EMULATE_DONE;
89110 int ra = get_ra(inst);
@@ -130,6 +151,11 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, · 40 unmodified lines
130151 emulated = kvmppc_e500_emul_tlbivax(vcpu, ea);
131152 break;
132153
154+ case XOP_EHPRIV:
155+ emulated = kvmppc_e500_emul_ehpriv(run, vcpu, inst,
156+ advance);
157+ break;
158+
133159 default:
134160 emulated = EMULATE_FAIL;
135161 }
@@ -146,7 +172,7 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, · 10 unmodified lines
146172 return emulated;
147173 }
148174
149−int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
175+int kvmppc_core_emulate_mtspr_e500(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
150176 {
151177 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
152178 int emulated = EMULATE_DONE;
@@ -237,7 +263,7 @@ int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val) · 84 unmodified lines
237263 return emulated;
238264 }
239265
240−int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
266+int kvmppc_core_emulate_mfspr_e500(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
241267 {
242268 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
243269 int emulated = EMULATE_DONE;
arch/powerpc/kvm/e500_mmu.c+2−2
@@ -32,7 +32,7 @@ · 31 unmodified lines
3232 #include <asm/kvm_ppc.h>
3333
3434 #include "e500.h"
35−#include "trace.h"
35+#include "trace_booke.h"
3636 #include "timing.h"
3737 #include "e500_mmu_host.h"
3838
@@ -536,7 +536,7 @@ gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index, · 497 unmodified lines
536536 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
537537 }
538538
539−void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
539+void kvmppc_mmu_destroy_e500(struct kvm_vcpu *vcpu)
540540 {
541541 }
542542
arch/powerpc/kvm/e500_mmu_host.c+5−1
@@ -32,10 +32,11 @@ · 31 unmodified lines
3232 #include <asm/kvm_ppc.h>
3333
3434 #include "e500.h"
35−#include "trace.h"
3635 #include "timing.h"
3736 #include "e500_mmu_host.h"
3837
38+#include "trace_booke.h"
39+
3940 #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
4041
4142 static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
@@ -253,6 +254,9 @@ static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref, · 211 unmodified lines
253254 ref->pfn = pfn;
254255 ref->flags |= E500_TLB_VALID;
255256
257+ /* Mark the page accessed */
258+ kvm_set_pfn_accessed(pfn);
259+
256260 if (tlbe_is_writable(gtlbe))
257261 kvm_set_pfn_dirty(pfn);
258262 }
arch/powerpc/kvm/e500mc.c+43−15
@@ -110,7 +110,7 @@ void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr) · 109 unmodified lines
110110
111111 static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu_on_cpu);
112112
113−void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
113+static void kvmppc_core_vcpu_load_e500mc(struct kvm_vcpu *vcpu, int cpu)
114114 {
115115 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
116116
@@ -147,7 +147,7 @@ void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · 30 unmodified lines
147147 kvmppc_load_guest_fp(vcpu);
148148 }
149149
150−void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
150+static void kvmppc_core_vcpu_put_e500mc(struct kvm_vcpu *vcpu)
151151 {
152152 vcpu->arch.eplc = mfspr(SPRN_EPLC);
153153 vcpu->arch.epsc = mfspr(SPRN_EPSC);
@@ -204,7 +204,8 @@ int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu) · 50 unmodified lines
204204 return 0;
205205 }
206206
207−void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
207+static int kvmppc_core_get_sregs_e500mc(struct kvm_vcpu *vcpu,
208+ struct kvm_sregs *sregs)
208209 {
209210 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
210211
@@ -224,10 +225,11 @@ void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) · 13 unmodified lines
224225 sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
225226 sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
226227
227− kvmppc_get_sregs_ivor(vcpu, sregs);
228+ return kvmppc_get_sregs_ivor(vcpu, sregs);
228229 }
229230
230−int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
231+static int kvmppc_core_set_sregs_e500mc(struct kvm_vcpu *vcpu,
232+ struct kvm_sregs *sregs)
231233 {
232234 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
233235 int ret;
@@ -260,21 +262,22 @@ int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) · 26 unmodified lines
260262 return kvmppc_set_sregs_ivor(vcpu, sregs);
261263 }
262264
263−int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
264− union kvmppc_one_reg *val)
265+static int kvmppc_get_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
266+ union kvmppc_one_reg *val)
265267 {
266268 int r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
267269 return r;
268270 }
269271
270−int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
271− union kvmppc_one_reg *val)
272+static int kvmppc_set_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
273+ union kvmppc_one_reg *val)
272274 {
273275 int r = kvmppc_set_one_reg_e500_tlb(vcpu, id, val);
274276 return r;
275277 }
276278
277−struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
279+static struct kvm_vcpu *kvmppc_core_vcpu_create_e500mc(struct kvm *kvm,
280+ unsigned int id)
278281 {
279282 struct kvmppc_vcpu_e500 *vcpu_e500;
280283 struct kvm_vcpu *vcpu;
@@ -315,7 +318,7 @@ out: · 34 unmodified lines
315318 return ERR_PTR(err);
316319 }
317320
318−void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
321+static void kvmppc_core_vcpu_free_e500mc(struct kvm_vcpu *vcpu)
319322 {
320323 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
321324
@@ -325,7 +328,7 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) · 3 unmodified lines
325328 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
326329 }
327330
328−int kvmppc_core_init_vm(struct kvm *kvm)
331+static int kvmppc_core_init_vm_e500mc(struct kvm *kvm)
329332 {
330333 int lpid;
331334
@@ -337,27 +340,52 @@ int kvmppc_core_init_vm(struct kvm *kvm) · 5 unmodified lines
337340 return 0;
338341 }
339342
340−void kvmppc_core_destroy_vm(struct kvm *kvm)
343+static void kvmppc_core_destroy_vm_e500mc(struct kvm *kvm)
341344 {
342345 kvmppc_free_lpid(kvm->arch.lpid);
343346 }
344347
348+static struct kvmppc_ops kvm_ops_e500mc = {
349+ .get_sregs = kvmppc_core_get_sregs_e500mc,
350+ .set_sregs = kvmppc_core_set_sregs_e500mc,
351+ .get_one_reg = kvmppc_get_one_reg_e500mc,
352+ .set_one_reg = kvmppc_set_one_reg_e500mc,
353+ .vcpu_load = kvmppc_core_vcpu_load_e500mc,
354+ .vcpu_put = kvmppc_core_vcpu_put_e500mc,
355+ .vcpu_create = kvmppc_core_vcpu_create_e500mc,
356+ .vcpu_free = kvmppc_core_vcpu_free_e500mc,
357+ .mmu_destroy = kvmppc_mmu_destroy_e500,
358+ .init_vm = kvmppc_core_init_vm_e500mc,
359+ .destroy_vm = kvmppc_core_destroy_vm_e500mc,
360+ .emulate_op = kvmppc_core_emulate_op_e500,
361+ .emulate_mtspr = kvmppc_core_emulate_mtspr_e500,
362+ .emulate_mfspr = kvmppc_core_emulate_mfspr_e500,
363+};
364+
345365 static int __init kvmppc_e500mc_init(void)
346366 {
347367 int r;
348368
349369 r = kvmppc_booke_init();
350370 if (r)
351− return r;
371+ goto err_out;
352372
353373 kvmppc_init_lpid(64);
354374 kvmppc_claim_lpid(0); /* host */
355375
356− return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
376+ r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
377+ if (r)
378+ goto err_out;
379+ kvm_ops_e500mc.owner = THIS_MODULE;
380+ kvmppc_pr_ops = &kvm_ops_e500mc;
381+
382+err_out:
383+ return r;
357384 }
358385
359386 static void __exit kvmppc_e500mc_exit(void)
360387 {
388+ kvmppc_pr_ops = NULL;
361389 kvmppc_booke_exit();
362390 }
363391
arch/powerpc/kvm/emulate.c+7−5
@@ -130,8 +130,8 @@ static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs) · 129 unmodified lines
130130 case SPRN_PIR: break;
131131
132132 default:
133− emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
134− spr_val);
133+ emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn,
134+ spr_val);
135135 if (emulated == EMULATE_FAIL)
136136 printk(KERN_INFO "mtspr: unknown spr "
137137 "0x%x\n", sprn);
@@ -191,8 +191,8 @@ static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt) · 53 unmodified lines
191191 spr_val = kvmppc_get_dec(vcpu, get_tb());
192192 break;
193193 default:
194− emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
195− &spr_val);
194+ emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn,
195+ &spr_val);
196196 if (unlikely(emulated == EMULATE_FAIL)) {
197197 printk(KERN_INFO "mfspr: unknown spr "
198198 "0x%x\n", sprn);
@@ -464,7 +464,8 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) · 265 unmodified lines
464464 }
465465
466466 if (emulated == EMULATE_FAIL) {
467− emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
467+ emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
468+ &advance);
468469 if (emulated == EMULATE_AGAIN) {
469470 advance = 0;
470471 } else if (emulated == EMULATE_FAIL) {
@@ -483,3 +484,4 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) · 12 unmodified lines
483484
484485 return emulated;
485486 }
487+EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);
arch/powerpc/kvm/powerpc.c+95−76
@@ -26,6 +26,7 @@ · 25 unmodified lines
2626 #include <linux/fs.h>
2727 #include <linux/slab.h>
2828 #include <linux/file.h>
29+#include <linux/module.h>
2930 #include <asm/cputable.h>
3031 #include <asm/uaccess.h>
3132 #include <asm/kvm_ppc.h>
@@ -39,6 +40,12 @@ · 7 unmodified lines
3940 #define CREATE_TRACE_POINTS
4041 #include "trace.h"
4142
43+struct kvmppc_ops *kvmppc_hv_ops;
44+EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
45+struct kvmppc_ops *kvmppc_pr_ops;
46+EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
47+
48+
4249 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
4350 {
4451 return !!(v->arch.pending_exceptions) ||
@@ -50,7 +57,6 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · 5 unmodified lines
5057 return 1;
5158 }
5259
53−#ifndef CONFIG_KVM_BOOK3S_64_HV
5460 /*
5561 * Common checks before entering the guest world. Call with interrupts
5662 * disabled.
@@ -125,7 +131,7 @@ int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu) · 68 unmodified lines
125131
126132 return r;
127133 }
128−#endif /* CONFIG_KVM_BOOK3S_64_HV */
134+EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
129135
130136 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
131137 {
@@ -179,6 +185,7 @@ int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) · 47 unmodified lines
179185
180186 return r;
181187 }
188+EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
182189
183190 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
184191 {
@@ -192,11 +199,9 @@ int kvmppc_sanity_check(struct kvm_vcpu *vcpu) · 7 unmodified lines
192199 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
193200 goto out;
194201
195−#ifdef CONFIG_KVM_BOOK3S_64_HV
196202 /* HV KVM can only do PAPR mode for now */
197− if (!vcpu->arch.papr_enabled)
203+ if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
198204 goto out;
199−#endif
200205
201206 #ifdef CONFIG_KVM_BOOKE_HV
202207 if (!cpu_has_feature(CPU_FTR_EMB_HV))
@@ -209,6 +214,7 @@ out: · 6 unmodified lines
209214 vcpu->arch.sane = r;
210215 return r ? 0 : -EINVAL;
211216 }
217+EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
212218
213219 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
214220 {
@@ -243,6 +249,7 @@ int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) · 28 unmodified lines
243249
244250 return r;
245251 }
252+EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
246253
247254 int kvm_arch_hardware_enable(void *garbage)
248255 {
@@ -269,10 +276,35 @@ void kvm_arch_check_processor_compat(void *rtn) · 20 unmodified lines
269276
270277 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
271278 {
272− if (type)
273− return -EINVAL;
274−
279+ struct kvmppc_ops *kvm_ops = NULL;
280+ /*
281+ * if we have both HV and PR enabled, default is HV
282+ */
283+ if (type == 0) {
284+ if (kvmppc_hv_ops)
285+ kvm_ops = kvmppc_hv_ops;
286+ else
287+ kvm_ops = kvmppc_pr_ops;
288+ if (!kvm_ops)
289+ goto err_out;
290+ } else if (type == KVM_VM_PPC_HV) {
291+ if (!kvmppc_hv_ops)
292+ goto err_out;
293+ kvm_ops = kvmppc_hv_ops;
294+ } else if (type == KVM_VM_PPC_PR) {
295+ if (!kvmppc_pr_ops)
296+ goto err_out;
297+ kvm_ops = kvmppc_pr_ops;
298+ } else
299+ goto err_out;
300+
301+ if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
302+ return -ENOENT;
303+
304+ kvm->arch.kvm_ops = kvm_ops;
275305 return kvmppc_core_init_vm(kvm);
306+err_out:
307+ return -EINVAL;
276308 }
277309
278310 void kvm_arch_destroy_vm(struct kvm *kvm)
@@ -292,6 +324,9 @@ void kvm_arch_destroy_vm(struct kvm *kvm) · 13 unmodified lines
292324 kvmppc_core_destroy_vm(kvm);
293325
294326 mutex_unlock(&kvm->lock);
327+
328+ /* drop the module reference */
329+ module_put(kvm->arch.kvm_ops->owner);
295330 }
296331
297332 void kvm_arch_sync_events(struct kvm *kvm)
@@ -301,6 +336,10 @@ void kvm_arch_sync_events(struct kvm *kvm) · 3 unmodified lines
301336 int kvm_dev_ioctl_check_extension(long ext)
302337 {
303338 int r;
339+ /* FIXME!!
340+ * Should some of this be vm ioctl ? is it possible now ?
341+ */
342+ int hv_enabled = kvmppc_hv_ops ? 1 : 0;
304343
305344 switch (ext) {
306345 #ifdef CONFIG_BOOKE
@@ -320,22 +359,26 @@ int kvm_dev_ioctl_check_extension(long ext) · 13 unmodified lines
320359 case KVM_CAP_DEVICE_CTRL:
321360 r = 1;
322361 break;
323−#ifndef CONFIG_KVM_BOOK3S_64_HV
324362 case KVM_CAP_PPC_PAIRED_SINGLES:
325363 case KVM_CAP_PPC_OSI:
326364 case KVM_CAP_PPC_GET_PVINFO:
327365 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
328366 case KVM_CAP_SW_TLB:
329367 #endif
330−#ifdef CONFIG_KVM_MPIC
331− case KVM_CAP_IRQ_MPIC:
332−#endif
333− r = 1;
368+ /* We support this only for PR */
369+ r = !hv_enabled;
334370 break;
371+#ifdef CONFIG_KVM_MMIO
335372 case KVM_CAP_COALESCED_MMIO:
336373 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
337374 break;
338375 #endif
376+#ifdef CONFIG_KVM_MPIC
377+ case KVM_CAP_IRQ_MPIC:
378+ r = 1;
379+ break;
380+#endif
381+
339382 #ifdef CONFIG_PPC_BOOK3S_64
340383 case KVM_CAP_SPAPR_TCE:
341384 case KVM_CAP_PPC_ALLOC_HTAB:
@@ -346,32 +389,37 @@ int kvm_dev_ioctl_check_extension(long ext) · 4 unmodified lines
346389 r = 1;
347390 break;
348391 #endif /* CONFIG_PPC_BOOK3S_64 */
349−#ifdef CONFIG_KVM_BOOK3S_64_HV
392+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
350393 case KVM_CAP_PPC_SMT:
351− r = threads_per_core;
394+ if (hv_enabled)
395+ r = threads_per_core;
396+ else
397+ r = 0;
352398 break;
353399 case KVM_CAP_PPC_RMA:
354− r = 1;
400+ r = hv_enabled;
355401 /* PPC970 requires an RMA */
356− if (cpu_has_feature(CPU_FTR_ARCH_201))
402+ if (r && cpu_has_feature(CPU_FTR_ARCH_201))
357403 r = 2;
358404 break;
359405 #endif
360406 case KVM_CAP_SYNC_MMU:
361−#ifdef CONFIG_KVM_BOOK3S_64_HV
362− r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
407+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
408+ if (hv_enabled)
409+ r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
410+ else
411+ r = 0;
363412 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
364413 r = 1;
365414 #else
366415 r = 0;
367− break;
368416 #endif
369−#ifdef CONFIG_KVM_BOOK3S_64_HV
417+ break;
418+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
370419 case KVM_CAP_PPC_HTAB_FD:
371− r = 1;
420+ r = hv_enabled;
372421 break;
373422 #endif
374− break;
375423 case KVM_CAP_NR_VCPUS:
376424 /*
377425 * Recommending a number of CPUs is somewhat arbitrary; we
@@ -379,11 +427,10 @@ int kvm_dev_ioctl_check_extension(long ext) · 1 unmodified line
379427 * will have secondary threads "offline"), and for other KVM
380428 * implementations just count online CPUs.
381429 */
382−#ifdef CONFIG_KVM_BOOK3S_64_HV
383− r = num_present_cpus();
384−#else
385− r = num_online_cpus();
386−#endif
430+ if (hv_enabled)
431+ r = num_present_cpus();
432+ else
433+ r = num_online_cpus();
387434 break;
388435 case KVM_CAP_MAX_VCPUS:
389436 r = KVM_MAX_VCPUS;
@@ -407,15 +454,16 @@ long kvm_arch_dev_ioctl(struct file *filp, · 17 unmodified lines
407454 return -EINVAL;
408455 }
409456
410−void kvm_arch_free_memslot(struct kvm_memory_slot *free,
457+void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
411458 struct kvm_memory_slot *dont)
412459 {
413− kvmppc_core_free_memslot(free, dont);
460+ kvmppc_core_free_memslot(kvm, free, dont);
414461 }
415462
416−int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
463+int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
464+ unsigned long npages)
417465 {
418− return kvmppc_core_create_memslot(slot, npages);
466+ return kvmppc_core_create_memslot(kvm, slot, npages);
419467 }
420468
421469 void kvm_arch_memslots_updated(struct kvm *kvm)
@@ -659,6 +707,7 @@ int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, · 237 unmodified lines
659707
660708 return EMULATE_DO_MMIO;
661709 }
710+EXPORT_SYMBOL_GPL(kvmppc_handle_load);
662711
663712 /* Same as above, but sign extends */
664713 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
@@ -720,6 +769,7 @@ int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, · 55 unmodified lines
720769
721770 return EMULATE_DO_MMIO;
722771 }
772+EXPORT_SYMBOL_GPL(kvmppc_handle_store);
723773
724774 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
725775 {
@@ -1024,52 +1074,12 @@ long kvm_arch_vm_ioctl(struct file *filp, · 298 unmodified lines
10241074 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
10251075 goto out;
10261076 }
1027−#endif /* CONFIG_PPC_BOOK3S_64 */
1028−
1029−#ifdef CONFIG_KVM_BOOK3S_64_HV
1030− case KVM_ALLOCATE_RMA: {
1031− struct kvm_allocate_rma rma;
1032− struct kvm *kvm = filp->private_data;
1033−
1034− r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
1035− if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
1036− r = -EFAULT;
1037− break;
1038− }
1039−
1040− case KVM_PPC_ALLOCATE_HTAB: {
1041− u32 htab_order;
1042−
1043− r = -EFAULT;
1044− if (get_user(htab_order, (u32 __user *)argp))
1045− break;
1046− r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
1047− if (r)
1048− break;
1049− r = -EFAULT;
1050− if (put_user(htab_order, (u32 __user *)argp))
1051− break;
1052− r = 0;
1053− break;
1054− }
1055−
1056− case KVM_PPC_GET_HTAB_FD: {
1057− struct kvm_get_htab_fd ghf;
1058−
1059− r = -EFAULT;
1060− if (copy_from_user(&ghf, argp, sizeof(ghf)))
1061− break;
1062− r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
1063− break;
1064− }
1065−#endif /* CONFIG_KVM_BOOK3S_64_HV */
1066−
1067−#ifdef CONFIG_PPC_BOOK3S_64
10681077 case KVM_PPC_GET_SMMU_INFO: {
10691078 struct kvm_ppc_smmu_info info;
1079+ struct kvm *kvm = filp->private_data;
10701080
10711081 memset(&info, 0, sizeof(info));
1072− r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
1082+ r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
10731083 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
10741084 r = -EFAULT;
10751085 break;
@@ -1080,11 +1090,15 @@ long kvm_arch_vm_ioctl(struct file *filp, · 4 unmodified lines
10801090 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
10811091 break;
10821092 }
1083−#endif /* CONFIG_PPC_BOOK3S_64 */
1093+ default: {
1094+ struct kvm *kvm = filp->private_data;
1095+ r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
1096+ }
1097+#else /* CONFIG_PPC_BOOK3S_64 */
10841098 default:
10851099 r = -ENOTTY;
1100+#endif
10861101 }
1087−
10881102 out:
10891103 return r;
10901104 }
@@ -1106,22 +1120,26 @@ long kvmppc_alloc_lpid(void) · 15 unmodified lines
11061120
11071121 return lpid;
11081122 }
1123+EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
11091124
11101125 void kvmppc_claim_lpid(long lpid)
11111126 {
11121127 set_bit(lpid, lpid_inuse);
11131128 }
1129+EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
11141130
11151131 void kvmppc_free_lpid(long lpid)
11161132 {
11171133 clear_bit(lpid, lpid_inuse);
11181134 }
1135+EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
11191136
11201137 void kvmppc_init_lpid(unsigned long nr_lpids_param)
11211138 {
11221139 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
11231140 memset(lpid_inuse, 0, sizeof(lpid_inuse));
11241141 }
1142+EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
11251143
11261144 int kvm_arch_init(void *opaque)
11271145 {
@@ -1130,4 +1148,5 @@ int kvm_arch_init(void *opaque) · 2 unmodified lines
11301148
11311149 void kvm_arch_exit(void)
11321150 {
1151+
11331152 }
arch/powerpc/kvm/trace.h+0−429
@@ -31,126 +31,6 @@ TRACE_EVENT(kvm_ppc_instr, · 30 unmodified lines
3131 __entry->inst, __entry->pc, __entry->emulate)
3232 );
3333
34−#ifdef CONFIG_PPC_BOOK3S
35−#define kvm_trace_symbol_exit \
36− {0x100, "SYSTEM_RESET"}, \
37− {0x200, "MACHINE_CHECK"}, \
38− {0x300, "DATA_STORAGE"}, \
39− {0x380, "DATA_SEGMENT"}, \
40− {0x400, "INST_STORAGE"}, \
41− {0x480, "INST_SEGMENT"}, \
42− {0x500, "EXTERNAL"}, \
43− {0x501, "EXTERNAL_LEVEL"}, \
44− {0x502, "EXTERNAL_HV"}, \
45− {0x600, "ALIGNMENT"}, \
46− {0x700, "PROGRAM"}, \
47− {0x800, "FP_UNAVAIL"}, \
48− {0x900, "DECREMENTER"}, \
49− {0x980, "HV_DECREMENTER"}, \
50− {0xc00, "SYSCALL"}, \
51− {0xd00, "TRACE"}, \
52− {0xe00, "H_DATA_STORAGE"}, \
53− {0xe20, "H_INST_STORAGE"}, \
54− {0xe40, "H_EMUL_ASSIST"}, \
55− {0xf00, "PERFMON"}, \
56− {0xf20, "ALTIVEC"}, \
57− {0xf40, "VSX"}
58−#else
59−#define kvm_trace_symbol_exit \
60− {0, "CRITICAL"}, \
61− {1, "MACHINE_CHECK"}, \
62− {2, "DATA_STORAGE"}, \
63− {3, "INST_STORAGE"}, \
64− {4, "EXTERNAL"}, \
65− {5, "ALIGNMENT"}, \
66− {6, "PROGRAM"}, \
67− {7, "FP_UNAVAIL"}, \
68− {8, "SYSCALL"}, \
69− {9, "AP_UNAVAIL"}, \
70− {10, "DECREMENTER"}, \
71− {11, "FIT"}, \
72− {12, "WATCHDOG"}, \
73− {13, "DTLB_MISS"}, \
74− {14, "ITLB_MISS"}, \
75− {15, "DEBUG"}, \
76− {32, "SPE_UNAVAIL"}, \
77− {33, "SPE_FP_DATA"}, \
78− {34, "SPE_FP_ROUND"}, \
79− {35, "PERFORMANCE_MONITOR"}, \
80− {36, "DOORBELL"}, \
81− {37, "DOORBELL_CRITICAL"}, \
82− {38, "GUEST_DBELL"}, \
83− {39, "GUEST_DBELL_CRIT"}, \
84− {40, "HV_SYSCALL"}, \
85− {41, "HV_PRIV"}
86−#endif
87−
88−TRACE_EVENT(kvm_exit,
89− TP_PROTO(unsigned int exit_nr, struct kvm_vcpu *vcpu),
90− TP_ARGS(exit_nr, vcpu),
91−
92− TP_STRUCT__entry(
93− __field( unsigned int, exit_nr )
94− __field( unsigned long, pc )
95− __field( unsigned long, msr )
96− __field( unsigned long, dar )
97−#ifdef CONFIG_KVM_BOOK3S_PR
98− __field( unsigned long, srr1 )
99−#endif
100− __field( unsigned long, last_inst )
101− ),
102−
103− TP_fast_assign(
104−#ifdef CONFIG_KVM_BOOK3S_PR
105− struct kvmppc_book3s_shadow_vcpu *svcpu;
106−#endif
107− __entry->exit_nr = exit_nr;
108− __entry->pc = kvmppc_get_pc(vcpu);
109− __entry->dar = kvmppc_get_fault_dar(vcpu);
110− __entry->msr = vcpu->arch.shared->msr;
111−#ifdef CONFIG_KVM_BOOK3S_PR
112− svcpu = svcpu_get(vcpu);
113− __entry->srr1 = svcpu->shadow_srr1;
114− svcpu_put(svcpu);
115−#endif
116− __entry->last_inst = vcpu->arch.last_inst;
117− ),
118−
119− TP_printk("exit=%s"
120− " | pc=0x%lx"
121− " | msr=0x%lx"
122− " | dar=0x%lx"
123−#ifdef CONFIG_KVM_BOOK3S_PR
124− " | srr1=0x%lx"
125−#endif
126− " | last_inst=0x%lx"
127− ,
128− __print_symbolic(__entry->exit_nr, kvm_trace_symbol_exit),
129− __entry->pc,
130− __entry->msr,
131− __entry->dar,
132−#ifdef CONFIG_KVM_BOOK3S_PR
133− __entry->srr1,
134−#endif
135− __entry->last_inst
136− )
137−);
138−
139−TRACE_EVENT(kvm_unmap_hva,
140− TP_PROTO(unsigned long hva),
141− TP_ARGS(hva),
142−
143− TP_STRUCT__entry(
144− __field( unsigned long, hva )
145− ),
146−
147− TP_fast_assign(
148− __entry->hva = hva;
149− ),
150−
151− TP_printk("unmap hva 0x%lx\n", __entry->hva)
152−);
153−
15434 TRACE_EVENT(kvm_stlb_inval,
15535 TP_PROTO(unsigned int stlb_index),
15636 TP_ARGS(stlb_index),
@@ -236,315 +116,6 @@ TRACE_EVENT(kvm_check_requests, · 79 unmodified lines
236116 __entry->cpu_nr, __entry->requests)
237117 );
238118
239−
240−/*************************************************************************
241− * Book3S trace points *
242− *************************************************************************/
243−
244−#ifdef CONFIG_KVM_BOOK3S_PR
245−
246−TRACE_EVENT(kvm_book3s_reenter,
247− TP_PROTO(int r, struct kvm_vcpu *vcpu),
248− TP_ARGS(r, vcpu),
249−
250− TP_STRUCT__entry(
251− __field( unsigned int, r )
252− __field( unsigned long, pc )
253− ),
254−
255− TP_fast_assign(
256− __entry->r = r;
257− __entry->pc = kvmppc_get_pc(vcpu);
258− ),
259−
260− TP_printk("reentry r=%d | pc=0x%lx", __entry->r, __entry->pc)
261−);
262−
263−#ifdef CONFIG_PPC_BOOK3S_64
264−
265−TRACE_EVENT(kvm_book3s_64_mmu_map,
266− TP_PROTO(int rflags, ulong hpteg, ulong va, pfn_t hpaddr,
267− struct kvmppc_pte *orig_pte),
268− TP_ARGS(rflags, hpteg, va, hpaddr, orig_pte),
269−
270− TP_STRUCT__entry(
271− __field( unsigned char, flag_w )
272− __field( unsigned char, flag_x )
273− __field( unsigned long, eaddr )
274− __field( unsigned long, hpteg )
275− __field( unsigned long, va )
276− __field( unsigned long long, vpage )
277− __field( unsigned long, hpaddr )
278− ),
279−
280− TP_fast_assign(
281− __entry->flag_w = ((rflags & HPTE_R_PP) == 3) ? '-' : 'w';
282− __entry->flag_x = (rflags & HPTE_R_N) ? '-' : 'x';
283− __entry->eaddr = orig_pte->eaddr;
284− __entry->hpteg = hpteg;
285− __entry->va = va;
286− __entry->vpage = orig_pte->vpage;
287− __entry->hpaddr = hpaddr;
288− ),
289−
290− TP_printk("KVM: %c%c Map 0x%lx: [%lx] 0x%lx (0x%llx) -> %lx",
291− __entry->flag_w, __entry->flag_x, __entry->eaddr,
292− __entry->hpteg, __entry->va, __entry->vpage, __entry->hpaddr)
293−);
294−
295−#endif /* CONFIG_PPC_BOOK3S_64 */
296−
297−TRACE_EVENT(kvm_book3s_mmu_map,
298− TP_PROTO(struct hpte_cache *pte),
299− TP_ARGS(pte),
300−
301− TP_STRUCT__entry(
302− __field( u64, host_vpn )
303− __field( u64, pfn )
304− __field( ulong, eaddr )
305− __field( u64, vpage )
306− __field( ulong, raddr )
307− __field( int, flags )
308− ),
309−
310− TP_fast_assign(
311− __entry->host_vpn = pte->host_vpn;
312− __entry->pfn = pte->pfn;
313− __entry->eaddr = pte->pte.eaddr;
314− __entry->vpage = pte->pte.vpage;
315− __entry->raddr = pte->pte.raddr;
316− __entry->flags = (pte->pte.may_read ? 0x4 : 0) |
317− (pte->pte.may_write ? 0x2 : 0) |
318− (pte->pte.may_execute ? 0x1 : 0);
319− ),
320−
321− TP_printk("Map: hvpn=%llx pfn=%llx ea=%lx vp=%llx ra=%lx [%x]",
322− __entry->host_vpn, __entry->pfn, __entry->eaddr,
323− __entry->vpage, __entry->raddr, __entry->flags)
324−);
325−
326−TRACE_EVENT(kvm_book3s_mmu_invalidate,
327− TP_PROTO(struct hpte_cache *pte),
328− TP_ARGS(pte),
329−
330− TP_STRUCT__entry(
331− __field( u64, host_vpn )
332− __field( u64, pfn )
333− __field( ulong, eaddr )
334− __field( u64, vpage )
335− __field( ulong, raddr )
336− __field( int, flags )
337− ),
338−
339− TP_fast_assign(
340− __entry->host_vpn = pte->host_vpn;
341− __entry->pfn = pte->pfn;
342− __entry->eaddr = pte->pte.eaddr;
343− __entry->vpage = pte->pte.vpage;
344− __entry->raddr = pte->pte.raddr;
345− __entry->flags = (pte->pte.may_read ? 0x4 : 0) |
346− (pte->pte.may_write ? 0x2 : 0) |
347− (pte->pte.may_execute ? 0x1 : 0);
348− ),
349−
350− TP_printk("Flush: hva=%llx pfn=%llx ea=%lx vp=%llx ra=%lx [%x]",
351− __entry->host_vpn, __entry->pfn, __entry->eaddr,
352− __entry->vpage, __entry->raddr, __entry->flags)
353−);
354−
355−TRACE_EVENT(kvm_book3s_mmu_flush,
356− TP_PROTO(const char *type, struct kvm_vcpu *vcpu, unsigned long long p1,
357− unsigned long long p2),
358− TP_ARGS(type, vcpu, p1, p2),
359−
360− TP_STRUCT__entry(
361− __field( int, count )
362− __field( unsigned long long, p1 )
363− __field( unsigned long long, p2 )
364− __field( const char *, type )
365− ),
366−
367− TP_fast_assign(
368− __entry->count = to_book3s(vcpu)->hpte_cache_count;
369− __entry->p1 = p1;
370− __entry->p2 = p2;
371− __entry->type = type;
372− ),
373−
374− TP_printk("Flush %d %sPTEs: %llx - %llx",
375− __entry->count, __entry->type, __entry->p1, __entry->p2)
376−);
377−
378−TRACE_EVENT(kvm_book3s_slb_found,
379− TP_PROTO(unsigned long long gvsid, unsigned long long hvsid),
380− TP_ARGS(gvsid, hvsid),
381−
382− TP_STRUCT__entry(
383− __field( unsigned long long, gvsid )
384− __field( unsigned long long, hvsid )
385− ),
386−
387− TP_fast_assign(
388− __entry->gvsid = gvsid;
389− __entry->hvsid = hvsid;
390− ),
391−
392− TP_printk("%llx -> %llx", __entry->gvsid, __entry->hvsid)
393−);
394−
395−TRACE_EVENT(kvm_book3s_slb_fail,
396− TP_PROTO(u16 sid_map_mask, unsigned long long gvsid),
397− TP_ARGS(sid_map_mask, gvsid),
398−
399− TP_STRUCT__entry(
400− __field( unsigned short, sid_map_mask )
401− __field( unsigned long long, gvsid )
402− ),
403−
404− TP_fast_assign(
405− __entry->sid_map_mask = sid_map_mask;
406− __entry->gvsid = gvsid;
407− ),
408−
409− TP_printk("%x/%x: %llx", __entry->sid_map_mask,
410− SID_MAP_MASK - __entry->sid_map_mask, __entry->gvsid)
411−);
412−
413−TRACE_EVENT(kvm_book3s_slb_map,
414− TP_PROTO(u16 sid_map_mask, unsigned long long gvsid,
415− unsigned long long hvsid),
416− TP_ARGS(sid_map_mask, gvsid, hvsid),
417−
418− TP_STRUCT__entry(
419− __field( unsigned short, sid_map_mask )
420− __field( unsigned long long, guest_vsid )
421− __field( unsigned long long, host_vsid )
422− ),
423−
424− TP_fast_assign(
425− __entry->sid_map_mask = sid_map_mask;
426− __entry->guest_vsid = gvsid;
427− __entry->host_vsid = hvsid;
428− ),
429−
430− TP_printk("%x: %llx -> %llx", __entry->sid_map_mask,
431− __entry->guest_vsid, __entry->host_vsid)
432−);
433−
434−TRACE_EVENT(kvm_book3s_slbmte,
435− TP_PROTO(u64 slb_vsid, u64 slb_esid),
436− TP_ARGS(slb_vsid, slb_esid),
437−
438− TP_STRUCT__entry(
439− __field( u64, slb_vsid )
440− __field( u64, slb_esid )
441− ),
442−
443− TP_fast_assign(
444− __entry->slb_vsid = slb_vsid;
445− __entry->slb_esid = slb_esid;
446− ),
447−
448− TP_printk("%llx, %llx", __entry->slb_vsid, __entry->slb_esid)
449−);
450−
451−#endif /* CONFIG_PPC_BOOK3S */
452−
453−
454−/*************************************************************************
455− * Book3E trace points *
456− *************************************************************************/
457−
458−#ifdef CONFIG_BOOKE
459−
460−TRACE_EVENT(kvm_booke206_stlb_write,
461− TP_PROTO(__u32 mas0, __u32 mas8, __u32 mas1, __u64 mas2, __u64 mas7_3),
462− TP_ARGS(mas0, mas8, mas1, mas2, mas7_3),
463−
464− TP_STRUCT__entry(
465− __field( __u32, mas0 )
466− __field( __u32, mas8 )
467− __field( __u32, mas1 )
468− __field( __u64, mas2 )
469− __field( __u64, mas7_3 )
470− ),
471−
472− TP_fast_assign(
473− __entry->mas0 = mas0;
474− __entry->mas8 = mas8;
475− __entry->mas1 = mas1;
476− __entry->mas2 = mas2;
477− __entry->mas7_3 = mas7_3;
478− ),
479−
480− TP_printk("mas0=%x mas8=%x mas1=%x mas2=%llx mas7_3=%llx",
481− __entry->mas0, __entry->mas8, __entry->mas1,
482− __entry->mas2, __entry->mas7_3)
483−);
484−
485−TRACE_EVENT(kvm_booke206_gtlb_write,
486− TP_PROTO(__u32 mas0, __u32 mas1, __u64 mas2, __u64 mas7_3),
487− TP_ARGS(mas0, mas1, mas2, mas7_3),
488−
489− TP_STRUCT__entry(
490− __field( __u32, mas0 )
491− __field( __u32, mas1 )
492− __field( __u64, mas2 )
493− __field( __u64, mas7_3 )
494− ),
495−
496− TP_fast_assign(
497− __entry->mas0 = mas0;
498− __entry->mas1 = mas1;
499− __entry->mas2 = mas2;
500− __entry->mas7_3 = mas7_3;
501− ),
502−
503− TP_printk("mas0=%x mas1=%x mas2=%llx mas7_3=%llx",
504− __entry->mas0, __entry->mas1,
505− __entry->mas2, __entry->mas7_3)
506−);
507−
508−TRACE_EVENT(kvm_booke206_ref_release,
509− TP_PROTO(__u64 pfn, __u32 flags),
510− TP_ARGS(pfn, flags),
511−
512− TP_STRUCT__entry(
513− __field( __u64, pfn )
514− __field( __u32, flags )
515− ),
516−
517− TP_fast_assign(
518− __entry->pfn = pfn;
519− __entry->flags = flags;
520− ),
521−
522− TP_printk("pfn=%llx flags=%x",
523− __entry->pfn, __entry->flags)
524−);
525−
526−TRACE_EVENT(kvm_booke_queue_irqprio,
527− TP_PROTO(struct kvm_vcpu *vcpu, unsigned int priority),
528− TP_ARGS(vcpu, priority),
529−
530− TP_STRUCT__entry(
531− __field( __u32, cpu_nr )
532− __field( __u32, priority )
533− __field( unsigned long, pending )
534− ),
535−
536− TP_fast_assign(
537− __entry->cpu_nr = vcpu->vcpu_id;
538− __entry->priority = priority;
539− __entry->pending = vcpu->arch.pending_exceptions;
540− ),
541−
542− TP_printk("vcpu=%x prio=%x pending=%lx",
543− __entry->cpu_nr, __entry->priority, __entry->pending)
544−);
545−
546−#endif
547−
548119 #endif /* _TRACE_KVM_H */
549120
550121 /* This part must be outside protection */
arch/powerpc/kvm/trace_booke.h+177−0
@@ -0,0 +1,177 @@
1+#if !defined(_TRACE_KVM_BOOKE_H) || defined(TRACE_HEADER_MULTI_READ)
2+#define _TRACE_KVM_BOOKE_H
3+
4+#include <linux/tracepoint.h>
5+
6+#undef TRACE_SYSTEM
7+#define TRACE_SYSTEM kvm_booke
8+#define TRACE_INCLUDE_PATH .
9+#define TRACE_INCLUDE_FILE trace_booke
10+
11+#define kvm_trace_symbol_exit \
12+ {0, "CRITICAL"}, \
13+ {1, "MACHINE_CHECK"}, \
14+ {2, "DATA_STORAGE"}, \
15+ {3, "INST_STORAGE"}, \
16+ {4, "EXTERNAL"}, \
17+ {5, "ALIGNMENT"}, \
18+ {6, "PROGRAM"}, \
19+ {7, "FP_UNAVAIL"}, \
20+ {8, "SYSCALL"}, \
21+ {9, "AP_UNAVAIL"}, \
22+ {10, "DECREMENTER"}, \
23+ {11, "FIT"}, \
24+ {12, "WATCHDOG"}, \
25+ {13, "DTLB_MISS"}, \
26+ {14, "ITLB_MISS"}, \
27+ {15, "DEBUG"}, \
28+ {32, "SPE_UNAVAIL"}, \
29+ {33, "SPE_FP_DATA"}, \
30+ {34, "SPE_FP_ROUND"}, \
31+ {35, "PERFORMANCE_MONITOR"}, \
32+ {36, "DOORBELL"}, \
33+ {37, "DOORBELL_CRITICAL"}, \
34+ {38, "GUEST_DBELL"}, \
35+ {39, "GUEST_DBELL_CRIT"}, \
36+ {40, "HV_SYSCALL"}, \
37+ {41, "HV_PRIV"}
38+
39+TRACE_EVENT(kvm_exit,
40+ TP_PROTO(unsigned int exit_nr, struct kvm_vcpu *vcpu),
41+ TP_ARGS(exit_nr, vcpu),
42+
43+ TP_STRUCT__entry(
44+ __field( unsigned int, exit_nr )
45+ __field( unsigned long, pc )
46+ __field( unsigned long, msr )
47+ __field( unsigned long, dar )
48+ __field( unsigned long, last_inst )
49+ ),
50+
51+ TP_fast_assign(
52+ __entry->exit_nr = exit_nr;
53+ __entry->pc = kvmppc_get_pc(vcpu);
54+ __entry->dar = kvmppc_get_fault_dar(vcpu);
55+ __entry->msr = vcpu->arch.shared->msr;
56+ __entry->last_inst = vcpu->arch.last_inst;
57+ ),
58+
59+ TP_printk("exit=%s"
60+ " | pc=0x%lx"
61+ " | msr=0x%lx"
62+ " | dar=0x%lx"
63+ " | last_inst=0x%lx"
64+ ,
65+ __print_symbolic(__entry->exit_nr, kvm_trace_symbol_exit),
66+ __entry->pc,
67+ __entry->msr,
68+ __entry->dar,
69+ __entry->last_inst
70+ )
71+);
72+
73+TRACE_EVENT(kvm_unmap_hva,
74+ TP_PROTO(unsigned long hva),
75+ TP_ARGS(hva),
76+
77+ TP_STRUCT__entry(
78+ __field( unsigned long, hva )
79+ ),
80+
81+ TP_fast_assign(
82+ __entry->hva = hva;
83+ ),
84+
85+ TP_printk("unmap hva 0x%lx\n", __entry->hva)
86+);
87+
88+TRACE_EVENT(kvm_booke206_stlb_write,
89+ TP_PROTO(__u32 mas0, __u32 mas8, __u32 mas1, __u64 mas2, __u64 mas7_3),
90+ TP_ARGS(mas0, mas8, mas1, mas2, mas7_3),
91+
92+ TP_STRUCT__entry(
93+ __field( __u32, mas0 )
94+ __field( __u32, mas8 )
95+ __field( __u32, mas1 )
96+ __field( __u64, mas2 )
97+ __field( __u64, mas7_3 )
98+ ),
99+
100+ TP_fast_assign(
101+ __entry->mas0 = mas0;
102+ __entry->mas8 = mas8;
103+ __entry->mas1 = mas1;
104+ __entry->mas2 = mas2;
105+ __entry->mas7_3 = mas7_3;
106+ ),
107+
108+ TP_printk("mas0=%x mas8=%x mas1=%x mas2=%llx mas7_3=%llx",
109+ __entry->mas0, __entry->mas8, __entry->mas1,
110+ __entry->mas2, __entry->mas7_3)
111+);
112+
113+TRACE_EVENT(kvm_booke206_gtlb_write,
114+ TP_PROTO(__u32 mas0, __u32 mas1, __u64 mas2, __u64 mas7_3),
115+ TP_ARGS(mas0, mas1, mas2, mas7_3),
116+
117+ TP_STRUCT__entry(
118+ __field( __u32, mas0 )
119+ __field( __u32, mas1 )
120+ __field( __u64, mas2 )
121+ __field( __u64, mas7_3 )
122+ ),
123+
124+ TP_fast_assign(
125+ __entry->mas0 = mas0;
126+ __entry->mas1 = mas1;
127+ __entry->mas2 = mas2;
128+ __entry->mas7_3 = mas7_3;
129+ ),
130+
131+ TP_printk("mas0=%x mas1=%x mas2=%llx mas7_3=%llx",
132+ __entry->mas0, __entry->mas1,
133+ __entry->mas2, __entry->mas7_3)
134+);
135+
136+TRACE_EVENT(kvm_booke206_ref_release,
137+ TP_PROTO(__u64 pfn, __u32 flags),
138+ TP_ARGS(pfn, flags),
139+
140+ TP_STRUCT__entry(
141+ __field( __u64, pfn )
142+ __field( __u32, flags )
143+ ),
144+
145+ TP_fast_assign(
146+ __entry->pfn = pfn;
147+ __entry->flags = flags;
148+ ),
149+
150+ TP_printk("pfn=%llx flags=%x",
151+ __entry->pfn, __entry->flags)
152+);
153+
154+TRACE_EVENT(kvm_booke_queue_irqprio,
155+ TP_PROTO(struct kvm_vcpu *vcpu, unsigned int priority),
156+ TP_ARGS(vcpu, priority),
157+
158+ TP_STRUCT__entry(
159+ __field( __u32, cpu_nr )
160+ __field( __u32, priority )
161+ __field( unsigned long, pending )
162+ ),
163+
164+ TP_fast_assign(
165+ __entry->cpu_nr = vcpu->vcpu_id;
166+ __entry->priority = priority;
167+ __entry->pending = vcpu->arch.pending_exceptions;
168+ ),
169+
170+ TP_printk("vcpu=%x prio=%x pending=%lx",
171+ __entry->cpu_nr, __entry->priority, __entry->pending)
172+);
173+
174+#endif
175+
176+/* This part must be outside protection */
177+#include <trace/define_trace.h>
arch/powerpc/kvm/trace_pr.h+297−0
@@ -0,0 +1,297 @@
1+
2+#if !defined(_TRACE_KVM_PR_H) || defined(TRACE_HEADER_MULTI_READ)
3+#define _TRACE_KVM_PR_H
4+
5+#include <linux/tracepoint.h>
6+
7+#undef TRACE_SYSTEM
8+#define TRACE_SYSTEM kvm_pr
9+#define TRACE_INCLUDE_PATH .
10+#define TRACE_INCLUDE_FILE trace_pr
11+
12+#define kvm_trace_symbol_exit \
13+ {0x100, "SYSTEM_RESET"}, \
14+ {0x200, "MACHINE_CHECK"}, \
15+ {0x300, "DATA_STORAGE"}, \
16+ {0x380, "DATA_SEGMENT"}, \
17+ {0x400, "INST_STORAGE"}, \
18+ {0x480, "INST_SEGMENT"}, \
19+ {0x500, "EXTERNAL"}, \
20+ {0x501, "EXTERNAL_LEVEL"}, \
21+ {0x502, "EXTERNAL_HV"}, \
22+ {0x600, "ALIGNMENT"}, \
23+ {0x700, "PROGRAM"}, \
24+ {0x800, "FP_UNAVAIL"}, \
25+ {0x900, "DECREMENTER"}, \
26+ {0x980, "HV_DECREMENTER"}, \
27+ {0xc00, "SYSCALL"}, \
28+ {0xd00, "TRACE"}, \
29+ {0xe00, "H_DATA_STORAGE"}, \
30+ {0xe20, "H_INST_STORAGE"}, \
31+ {0xe40, "H_EMUL_ASSIST"}, \
32+ {0xf00, "PERFMON"}, \
33+ {0xf20, "ALTIVEC"}, \
34+ {0xf40, "VSX"}
35+
36+TRACE_EVENT(kvm_book3s_reenter,
37+ TP_PROTO(int r, struct kvm_vcpu *vcpu),
38+ TP_ARGS(r, vcpu),
39+
40+ TP_STRUCT__entry(
41+ __field( unsigned int, r )
42+ __field( unsigned long, pc )
43+ ),
44+
45+ TP_fast_assign(
46+ __entry->r = r;
47+ __entry->pc = kvmppc_get_pc(vcpu);
48+ ),
49+
50+ TP_printk("reentry r=%d | pc=0x%lx", __entry->r, __entry->pc)
51+);
52+
53+#ifdef CONFIG_PPC_BOOK3S_64
54+
55+TRACE_EVENT(kvm_book3s_64_mmu_map,
56+ TP_PROTO(int rflags, ulong hpteg, ulong va, pfn_t hpaddr,
57+ struct kvmppc_pte *orig_pte),
58+ TP_ARGS(rflags, hpteg, va, hpaddr, orig_pte),
59+
60+ TP_STRUCT__entry(
61+ __field( unsigned char, flag_w )
62+ __field( unsigned char, flag_x )
63+ __field( unsigned long, eaddr )
64+ __field( unsigned long, hpteg )
65+ __field( unsigned long, va )
66+ __field( unsigned long long, vpage )
67+ __field( unsigned long, hpaddr )
68+ ),
69+
70+ TP_fast_assign(
71+ __entry->flag_w = ((rflags & HPTE_R_PP) == 3) ? '-' : 'w';
72+ __entry->flag_x = (rflags & HPTE_R_N) ? '-' : 'x';
73+ __entry->eaddr = orig_pte->eaddr;
74+ __entry->hpteg = hpteg;
75+ __entry->va = va;
76+ __entry->vpage = orig_pte->vpage;
77+ __entry->hpaddr = hpaddr;
78+ ),
79+
80+ TP_printk("KVM: %c%c Map 0x%lx: [%lx] 0x%lx (0x%llx) -> %lx",
81+ __entry->flag_w, __entry->flag_x, __entry->eaddr,
82+ __entry->hpteg, __entry->va, __entry->vpage, __entry->hpaddr)
83+);
84+
85+#endif /* CONFIG_PPC_BOOK3S_64 */
86+
87+TRACE_EVENT(kvm_book3s_mmu_map,
88+ TP_PROTO(struct hpte_cache *pte),
89+ TP_ARGS(pte),
90+
91+ TP_STRUCT__entry(
92+ __field( u64, host_vpn )
93+ __field( u64, pfn )
94+ __field( ulong, eaddr )
95+ __field( u64, vpage )
96+ __field( ulong, raddr )
97+ __field( int, flags )
98+ ),
99+
100+ TP_fast_assign(
101+ __entry->host_vpn = pte->host_vpn;
102+ __entry->pfn = pte->pfn;
103+ __entry->eaddr = pte->pte.eaddr;
104+ __entry->vpage = pte->pte.vpage;
105+ __entry->raddr = pte->pte.raddr;
106+ __entry->flags = (pte->pte.may_read ? 0x4 : 0) |
107+ (pte->pte.may_write ? 0x2 : 0) |
108+ (pte->pte.may_execute ? 0x1 : 0);
109+ ),
110+
111+ TP_printk("Map: hvpn=%llx pfn=%llx ea=%lx vp=%llx ra=%lx [%x]",
112+ __entry->host_vpn, __entry->pfn, __entry->eaddr,
113+ __entry->vpage, __entry->raddr, __entry->flags)
114+);
115+
116+TRACE_EVENT(kvm_book3s_mmu_invalidate,
117+ TP_PROTO(struct hpte_cache *pte),
118+ TP_ARGS(pte),
119+
120+ TP_STRUCT__entry(
121+ __field( u64, host_vpn )
122+ __field( u64, pfn )
123+ __field( ulong, eaddr )
124+ __field( u64, vpage )
125+ __field( ulong, raddr )
126+ __field( int, flags )
127+ ),
128+
129+ TP_fast_assign(
130+ __entry->host_vpn = pte->host_vpn;
131+ __entry->pfn = pte->pfn;
132+ __entry->eaddr = pte->pte.eaddr;
133+ __entry->vpage = pte->pte.vpage;
134+ __entry->raddr = pte->pte.raddr;
135+ __entry->flags = (pte->pte.may_read ? 0x4 : 0) |
136+ (pte->pte.may_write ? 0x2 : 0) |
137+ (pte->pte.may_execute ? 0x1 : 0);
138+ ),
139+
140+ TP_printk("Flush: hva=%llx pfn=%llx ea=%lx vp=%llx ra=%lx [%x]",
141+ __entry->host_vpn, __entry->pfn, __entry->eaddr,
142+ __entry->vpage, __entry->raddr, __entry->flags)
143+);
144+
145+TRACE_EVENT(kvm_book3s_mmu_flush,
146+ TP_PROTO(const char *type, struct kvm_vcpu *vcpu, unsigned long long p1,
147+ unsigned long long p2),
148+ TP_ARGS(type, vcpu, p1, p2),
149+
150+ TP_STRUCT__entry(
151+ __field( int, count )
152+ __field( unsigned long long, p1 )
153+ __field( unsigned long long, p2 )
154+ __field( const char *, type )
155+ ),
156+
157+ TP_fast_assign(
158+ __entry->count = to_book3s(vcpu)->hpte_cache_count;
159+ __entry->p1 = p1;
160+ __entry->p2 = p2;
161+ __entry->type = type;
162+ ),
163+
164+ TP_printk("Flush %d %sPTEs: %llx - %llx",
165+ __entry->count, __entry->type, __entry->p1, __entry->p2)
166+);
167+
168+TRACE_EVENT(kvm_book3s_slb_found,
169+ TP_PROTO(unsigned long long gvsid, unsigned long long hvsid),
170+ TP_ARGS(gvsid, hvsid),
171+
172+ TP_STRUCT__entry(
173+ __field( unsigned long long, gvsid )
174+ __field( unsigned long long, hvsid )
175+ ),
176+
177+ TP_fast_assign(
178+ __entry->gvsid = gvsid;
179+ __entry->hvsid = hvsid;
180+ ),
181+
182+ TP_printk("%llx -> %llx", __entry->gvsid, __entry->hvsid)
183+);
184+
185+TRACE_EVENT(kvm_book3s_slb_fail,
186+ TP_PROTO(u16 sid_map_mask, unsigned long long gvsid),
187+ TP_ARGS(sid_map_mask, gvsid),
188+
189+ TP_STRUCT__entry(
190+ __field( unsigned short, sid_map_mask )
191+ __field( unsigned long long, gvsid )
192+ ),
193+
194+ TP_fast_assign(
195+ __entry->sid_map_mask = sid_map_mask;
196+ __entry->gvsid = gvsid;
197+ ),
198+
199+ TP_printk("%x/%x: %llx", __entry->sid_map_mask,
200+ SID_MAP_MASK - __entry->sid_map_mask, __entry->gvsid)
201+);
202+
203+TRACE_EVENT(kvm_book3s_slb_map,
204+ TP_PROTO(u16 sid_map_mask, unsigned long long gvsid,
205+ unsigned long long hvsid),
206+ TP_ARGS(sid_map_mask, gvsid, hvsid),
207+
208+ TP_STRUCT__entry(
209+ __field( unsigned short, sid_map_mask )
210+ __field( unsigned long long, guest_vsid )
211+ __field( unsigned long long, host_vsid )
212+ ),
213+
214+ TP_fast_assign(
215+ __entry->sid_map_mask = sid_map_mask;
216+ __entry->guest_vsid = gvsid;
217+ __entry->host_vsid = hvsid;
218+ ),
219+
220+ TP_printk("%x: %llx -> %llx", __entry->sid_map_mask,
221+ __entry->guest_vsid, __entry->host_vsid)
222+);
223+
224+TRACE_EVENT(kvm_book3s_slbmte,
225+ TP_PROTO(u64 slb_vsid, u64 slb_esid),
226+ TP_ARGS(slb_vsid, slb_esid),
227+
228+ TP_STRUCT__entry(
229+ __field( u64, slb_vsid )
230+ __field( u64, slb_esid )
231+ ),
232+
233+ TP_fast_assign(
234+ __entry->slb_vsid = slb_vsid;
235+ __entry->slb_esid = slb_esid;
236+ ),
237+
238+ TP_printk("%llx, %llx", __entry->slb_vsid, __entry->slb_esid)
239+);
240+
241+TRACE_EVENT(kvm_exit,
242+ TP_PROTO(unsigned int exit_nr, struct kvm_vcpu *vcpu),
243+ TP_ARGS(exit_nr, vcpu),
244+
245+ TP_STRUCT__entry(
246+ __field( unsigned int, exit_nr )
247+ __field( unsigned long, pc )
248+ __field( unsigned long, msr )
249+ __field( unsigned long, dar )
250+ __field( unsigned long, srr1 )
251+ __field( unsigned long, last_inst )
252+ ),
253+
254+ TP_fast_assign(
255+ __entry->exit_nr = exit_nr;
256+ __entry->pc = kvmppc_get_pc(vcpu);
257+ __entry->dar = kvmppc_get_fault_dar(vcpu);
258+ __entry->msr = vcpu->arch.shared->msr;
259+ __entry->srr1 = vcpu->arch.shadow_srr1;
260+ __entry->last_inst = vcpu->arch.last_inst;
261+ ),
262+
263+ TP_printk("exit=%s"
264+ " | pc=0x%lx"
265+ " | msr=0x%lx"
266+ " | dar=0x%lx"
267+ " | srr1=0x%lx"
268+ " | last_inst=0x%lx"
269+ ,
270+ __print_symbolic(__entry->exit_nr, kvm_trace_symbol_exit),
271+ __entry->pc,
272+ __entry->msr,
273+ __entry->dar,
274+ __entry->srr1,
275+ __entry->last_inst
276+ )
277+);
278+
279+TRACE_EVENT(kvm_unmap_hva,
280+ TP_PROTO(unsigned long hva),
281+ TP_ARGS(hva),
282+
283+ TP_STRUCT__entry(
284+ __field( unsigned long, hva )
285+ ),
286+
287+ TP_fast_assign(
288+ __entry->hva = hva;
289+ ),
290+
291+ TP_printk("unmap hva 0x%lx\n", __entry->hva)
292+);
293+
294+#endif /* _TRACE_KVM_H */
295+
296+/* This part must be outside protection */
297+#include <trace/define_trace.h>
arch/s390/include/asm/kvm_host.h+0−8
@@ -38,13 +38,6 @@ struct sca_block { · 37 unmodified lines
3838 struct sca_entry cpu[64];
3939 } __attribute__((packed));
4040
41−#define KVM_NR_PAGE_SIZES 2
42−#define KVM_HPAGE_GFN_SHIFT(x) (((x) - 1) * 8)
43−#define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
44−#define KVM_HPAGE_SIZE(x) (1UL << KVM_HPAGE_SHIFT(x))
45−#define KVM_HPAGE_MASK(x) (~(KVM_HPAGE_SIZE(x) - 1))
46−#define KVM_PAGES_PER_HPAGE(x) (KVM_HPAGE_SIZE(x) / PAGE_SIZE)
47−
4841 #define CPUSTAT_STOPPED 0x80000000
4942 #define CPUSTAT_WAIT 0x10000000
5043 #define CPUSTAT_ECALL_PEND 0x08000000
@@ -220,7 +213,6 @@ struct kvm_s390_interrupt_info { · 169 unmodified lines
220213 /* for local_interrupt.action_flags */
221214 #define ACTION_STORE_ON_STOP (1<<0)
222215 #define ACTION_STOP_ON_STOP (1<<1)
223−#define ACTION_RELOADVCPU_ON_STOP (1<<2)
224216
225217 struct kvm_s390_local_interrupt {
226218 spinlock_t lock;
arch/s390/kvm/diag.c+1−3
@@ -107,14 +107,13 @@ static int __diag_ipl_functions(struct kvm_vcpu *vcpu) · 106 unmodified lines
107107
108108 static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
109109 {
110− int ret, idx;
110+ int ret;
111111
112112 /* No virtio-ccw notification? Get out quickly. */
113113 if (!vcpu->kvm->arch.css_support ||
114114 (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
115115 return -EOPNOTSUPP;
116116
117− idx = srcu_read_lock(&vcpu->kvm->srcu);
118117 /*
119118 * The layout is as follows:
120119 * - gpr 2 contains the subchannel id (passed as addr)
@@ -125,7 +124,6 @@ static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu) · 4 unmodified lines
125124 vcpu->run->s.regs.gprs[2],
126125 8, &vcpu->run->s.regs.gprs[3],
127126 vcpu->run->s.regs.gprs[4]);
128− srcu_read_unlock(&vcpu->kvm->srcu, idx);
129127
130128 /*
131129 * Return cookie in gpr 2, but don't overwrite the register if the
arch/s390/kvm/gaccess.h+14−7
@@ -18,20 +18,27 @@ · 17 unmodified lines
1818 #include <asm/uaccess.h>
1919 #include "kvm-s390.h"
2020
21+/* Convert real to absolute address by applying the prefix of the CPU */
22+static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu,
23+ unsigned long gaddr)
24+{
25+ unsigned long prefix = vcpu->arch.sie_block->prefix;
26+ if (gaddr < 2 * PAGE_SIZE)
27+ gaddr += prefix;
28+ else if (gaddr >= prefix && gaddr < prefix + 2 * PAGE_SIZE)
29+ gaddr -= prefix;
30+ return gaddr;
31+}
32+
2133 static inline void __user *__gptr_to_uptr(struct kvm_vcpu *vcpu,
2234 void __user *gptr,
2335 int prefixing)
2436 {
25− unsigned long prefix = vcpu->arch.sie_block->prefix;
2637 unsigned long gaddr = (unsigned long) gptr;
2738 unsigned long uaddr;
2839
29− if (prefixing) {
30− if (gaddr < 2 * PAGE_SIZE)
31− gaddr += prefix;
32− else if ((gaddr >= prefix) && (gaddr < prefix + 2 * PAGE_SIZE))
33− gaddr -= prefix;
34− }
40+ if (prefixing)
41+ gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
3542 uaddr = gmap_fault(gaddr, vcpu->arch.gmap);
3643 if (IS_ERR_VALUE(uaddr))
3744 uaddr = -EFAULT;
arch/s390/kvm/intercept.c+0−6
@@ -62,12 +62,6 @@ static int handle_stop(struct kvm_vcpu *vcpu) · 61 unmodified lines
6262
6363 trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
6464
65− if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
66− vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
67− rc = SIE_INTERCEPT_RERUNVCPU;
68− vcpu->run->exit_reason = KVM_EXIT_INTR;
69− }
70−
7165 if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
7266 atomic_set_mask(CPUSTAT_STOPPED,
7367 &vcpu->arch.sie_block->cpuflags);
arch/s390/kvm/interrupt.c+3−0
@@ -436,6 +436,7 @@ int kvm_s390_handle_wait(struct kvm_vcpu *vcpu) · 435 unmodified lines
436436 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
437437 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
438438 no_timer:
439+ srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
439440 spin_lock(&vcpu->arch.local_int.float_int->lock);
440441 spin_lock_bh(&vcpu->arch.local_int.lock);
441442 add_wait_queue(&vcpu->wq, &wait);
@@ -455,6 +456,8 @@ no_timer: · 13 unmodified lines
455456 remove_wait_queue(&vcpu->wq, &wait);
456457 spin_unlock_bh(&vcpu->arch.local_int.lock);
457458 spin_unlock(&vcpu->arch.local_int.float_int->lock);
459+ vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
460+
458461 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
459462 return 0;
460463 }
arch/s390/kvm/kvm-s390.c+63−33
@@ -695,9 +695,9 @@ static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) · 694 unmodified lines
695695 return 0;
696696 }
697697
698−static int __vcpu_run(struct kvm_vcpu *vcpu)
698+static int vcpu_pre_run(struct kvm_vcpu *vcpu)
699699 {
700− int rc;
700+ int rc, cpuflags;
701701
702702 memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
703703
@@ -715,28 +715,24 @@ static int __vcpu_run(struct kvm_vcpu *vcpu) · 11 unmodified lines
715715 return rc;
716716
717717 vcpu->arch.sie_block->icptcode = 0;
718− VCPU_EVENT(vcpu, 6, "entering sie flags %x",
719− atomic_read(&vcpu->arch.sie_block->cpuflags));
720− trace_kvm_s390_sie_enter(vcpu,
721− atomic_read(&vcpu->arch.sie_block->cpuflags));
718+ cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
719+ VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
720+ trace_kvm_s390_sie_enter(vcpu, cpuflags);
722721
723− /*
724− * As PF_VCPU will be used in fault handler, between guest_enter
725− * and guest_exit should be no uaccess.
726− */
727− preempt_disable();
728− kvm_guest_enter();
729− preempt_enable();
730− rc = sie64a(vcpu->arch.sie_block, vcpu->run->s.regs.gprs);
731− kvm_guest_exit();
722+ return 0;
723+}
724+
725+static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
726+{
727+ int rc;
732728
733729 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
734730 vcpu->arch.sie_block->icptcode);
735731 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
736732
737− if (rc > 0)
733+ if (exit_reason >= 0) {
738734 rc = 0;
739− if (rc < 0) {
735+ } else {
740736 if (kvm_is_ucontrol(vcpu->kvm)) {
741737 rc = SIE_INTERCEPT_UCONTROL;
742738 } else {
@@ -747,6 +743,49 @@ static int __vcpu_run(struct kvm_vcpu *vcpu) · 4 unmodified lines
747743 }
748744
749745 memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
746+
747+ if (rc == 0) {
748+ if (kvm_is_ucontrol(vcpu->kvm))
749+ rc = -EOPNOTSUPP;
750+ else
751+ rc = kvm_handle_sie_intercept(vcpu);
752+ }
753+
754+ return rc;
755+}
756+
757+static int __vcpu_run(struct kvm_vcpu *vcpu)
758+{
759+ int rc, exit_reason;
760+
761+ /*
762+ * We try to hold kvm->srcu during most of vcpu_run (except when run-
763+ * ning the guest), so that memslots (and other stuff) are protected
764+ */
765+ vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
766+
767+ do {
768+ rc = vcpu_pre_run(vcpu);
769+ if (rc)
770+ break;
771+
772+ srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
773+ /*
774+ * As PF_VCPU will be used in fault handler, between
775+ * guest_enter and guest_exit should be no uaccess.
776+ */
777+ preempt_disable();
778+ kvm_guest_enter();
779+ preempt_enable();
780+ exit_reason = sie64a(vcpu->arch.sie_block,
781+ vcpu->run->s.regs.gprs);
782+ kvm_guest_exit();
783+ vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
784+
785+ rc = vcpu_post_run(vcpu, exit_reason);
786+ } while (!signal_pending(current) && !rc);
787+
788+ srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
750789 return rc;
751790 }
752791
@@ -755,7 +794,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) · 2 unmodified lines
755794 int rc;
756795 sigset_t sigsaved;
757796
758−rerun_vcpu:
759797 if (vcpu->sigset_active)
760798 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
761799
@@ -788,19 +826,7 @@ rerun_vcpu: · 26 unmodified lines
788826 }
789827
790828 might_fault();
791−
792− do {
793− rc = __vcpu_run(vcpu);
794− if (rc)
795− break;
796− if (kvm_is_ucontrol(vcpu->kvm))
797− rc = -EOPNOTSUPP;
798− else
799− rc = kvm_handle_sie_intercept(vcpu);
800− } while (!signal_pending(current) && !rc);
801−
802− if (rc == SIE_INTERCEPT_RERUNVCPU)
803− goto rerun_vcpu;
829+ rc = __vcpu_run(vcpu);
804830
805831 if (signal_pending(current) && !rc) {
806832 kvm_run->exit_reason = KVM_EXIT_INTR;
@@ -958,6 +984,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, · 151 unmodified lines
958984 {
959985 struct kvm_vcpu *vcpu = filp->private_data;
960986 void __user *argp = (void __user *)arg;
987+ int idx;
961988 long r;
962989
963990 switch (ioctl) {
@@ -971,7 +998,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp, · 7 unmodified lines
971998 break;
972999 }
9731000 case KVM_S390_STORE_STATUS:
1001+ idx = srcu_read_lock(&vcpu->kvm->srcu);
9741002 r = kvm_s390_vcpu_store_status(vcpu, arg);
1003+ srcu_read_unlock(&vcpu->kvm->srcu, idx);
9751004 break;
9761005 case KVM_S390_SET_INITIAL_PSW: {
9771006 psw_t psw;
@@ -1067,12 +1096,13 @@ int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) · 89 unmodified lines
10671096 return VM_FAULT_SIGBUS;
10681097 }
10691098
1070−void kvm_arch_free_memslot(struct kvm_memory_slot *free,
1099+void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
10711100 struct kvm_memory_slot *dont)
10721101 {
10731102 }
10741103
1075−int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
1104+int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1105+ unsigned long npages)
10761106 {
10771107 return 0;
10781108 }
arch/s390/kvm/kvm-s390.h+5−4
@@ -28,8 +28,7 @@ typedef int (*intercept_handler_t)(struct kvm_vcpu *vcpu); · 27 unmodified lines
2828 extern unsigned long *vfacilities;
2929
3030 /* negativ values are error codes, positive values for internal conditions */
31−#define SIE_INTERCEPT_RERUNVCPU (1<<0)
32−#define SIE_INTERCEPT_UCONTROL (1<<1)
31+#define SIE_INTERCEPT_UCONTROL (1<<0)
3332 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu);
3433
3534 #define VM_EVENT(d_kvm, d_loglevel, d_string, d_args...)\
@@ -91,8 +90,10 @@ static inline void kvm_s390_get_base_disp_sse(struct kvm_vcpu *vcpu, · 55 unmodified lines
9190
9291 static inline void kvm_s390_get_regs_rre(struct kvm_vcpu *vcpu, int *r1, int *r2)
9392 {
94− *r1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 20;
95− *r2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
93+ if (r1)
94+ *r1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 20;
95+ if (r2)
96+ *r2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
9697 }
9798
9899 static inline u64 kvm_s390_get_base_disp_rsy(struct kvm_vcpu *vcpu)
arch/s390/kvm/priv.c+61−0
@@ -30,6 +30,38 @@ · 29 unmodified lines
3030 #include "kvm-s390.h"
3131 #include "trace.h"
3232
33+/* Handle SCK (SET CLOCK) interception */
34+static int handle_set_clock(struct kvm_vcpu *vcpu)
35+{
36+ struct kvm_vcpu *cpup;
37+ s64 hostclk, val;
38+ u64 op2;
39+ int i;
40+
41+ if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
42+ return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
43+
44+ op2 = kvm_s390_get_base_disp_s(vcpu);
45+ if (op2 & 7) /* Operand must be on a doubleword boundary */
46+ return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
47+ if (get_guest(vcpu, val, (u64 __user *) op2))
48+ return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
49+
50+ if (store_tod_clock(&hostclk)) {
51+ kvm_s390_set_psw_cc(vcpu, 3);
52+ return 0;
53+ }
54+ val = (val - hostclk) & ~0x3fUL;
55+
56+ mutex_lock(&vcpu->kvm->lock);
57+ kvm_for_each_vcpu(i, cpup, vcpu->kvm)
58+ cpup->arch.sie_block->epoch = val;
59+ mutex_unlock(&vcpu->kvm->lock);
60+
61+ kvm_s390_set_psw_cc(vcpu, 0);
62+ return 0;
63+}
64+
3365 static int handle_set_prefix(struct kvm_vcpu *vcpu)
3466 {
3567 u64 operand2;
@@ -128,6 +160,33 @@ static int handle_skey(struct kvm_vcpu *vcpu) · 92 unmodified lines
128160 return 0;
129161 }
130162
163+static int handle_test_block(struct kvm_vcpu *vcpu)
164+{
165+ unsigned long hva;
166+ gpa_t addr;
167+ int reg2;
168+
169+ if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
170+ return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
171+
172+ kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
173+ addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
174+ addr = kvm_s390_real_to_abs(vcpu, addr);
175+
176+ hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
177+ if (kvm_is_error_hva(hva))
178+ return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
179+ /*
180+ * We don't expect errors on modern systems, and do not care
181+ * about storage keys (yet), so let's just clear the page.
182+ */
183+ if (clear_user((void __user *)hva, PAGE_SIZE) != 0)
184+ return -EFAULT;
185+ kvm_s390_set_psw_cc(vcpu, 0);
186+ vcpu->run->s.regs.gprs[0] = 0;
187+ return 0;
188+}
189+
131190 static int handle_tpi(struct kvm_vcpu *vcpu)
132191 {
133192 struct kvm_s390_interrupt_info *inti;
@@ -438,12 +497,14 @@ out_exception: · 304 unmodified lines
438497
439498 static const intercept_handler_t b2_handlers[256] = {
440499 [0x02] = handle_stidp,
500+ [0x04] = handle_set_clock,
441501 [0x10] = handle_set_prefix,
442502 [0x11] = handle_store_prefix,
443503 [0x12] = handle_store_cpu_address,
444504 [0x29] = handle_skey,
445505 [0x2a] = handle_skey,
446506 [0x2b] = handle_skey,
507+ [0x2c] = handle_test_block,
447508 [0x30] = handle_io_inst,
448509 [0x31] = handle_io_inst,
449510 [0x32] = handle_io_inst,
arch/x86/include/asm/kvm_emulate.h+7−3
@@ -274,13 +274,17 @@ struct x86_emulate_ctxt { · 273 unmodified lines
274274
275275 bool guest_mode; /* guest running a nested guest */
276276 bool perm_ok; /* do not check permissions if true */
277− bool only_vendor_specific_insn;
277+ bool ud; /* inject an #UD if host doesn't support insn */
278278
279279 bool have_exception;
280280 struct x86_exception exception;
281281
282− /* decode cache */
283− u8 twobyte;
282+ /*
283+ * decode cache
284+ */
285+
286+ /* current opcode length in bytes */
287+ u8 opcode_len;
284288 u8 b;
285289 u8 intercept;
286290 u8 lock_prefix;
arch/x86/include/asm/kvm_host.h+15−8
@@ -79,6 +79,13 @@ · 78 unmodified lines
7979 #define KVM_HPAGE_MASK(x) (~(KVM_HPAGE_SIZE(x) - 1))
8080 #define KVM_PAGES_PER_HPAGE(x) (KVM_HPAGE_SIZE(x) / PAGE_SIZE)
8181
82+static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
83+{
84+ /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
85+ return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
86+ (base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
87+}
88+
8289 #define SELECTOR_TI_MASK (1 << 2)
8390 #define SELECTOR_RPL_MASK 0x03
8491
@@ -253,7 +260,6 @@ struct kvm_pio_request { · 168 unmodified lines
253260 * mode.
254261 */
255262 struct kvm_mmu {
256− void (*new_cr3)(struct kvm_vcpu *vcpu);
257263 void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long root);
258264 unsigned long (*get_cr3)(struct kvm_vcpu *vcpu);
259265 u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index);
@@ -261,7 +267,6 @@ struct kvm_mmu { · 1 unmodified line
261267 bool prefault);
262268 void (*inject_page_fault)(struct kvm_vcpu *vcpu,
263269 struct x86_exception *fault);
264− void (*free)(struct kvm_vcpu *vcpu);
265270 gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva, u32 access,
266271 struct x86_exception *exception);
267272 gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access);
@@ -389,6 +394,8 @@ struct kvm_vcpu_arch { · 121 unmodified lines
389394
390395 struct fpu guest_fpu;
391396 u64 xcr0;
397+ u64 guest_supported_xcr0;
398+ u32 guest_xstate_size;
392399
393400 struct kvm_pio_request pio;
394401 void *pio_data;
@@ -557,7 +564,9 @@ struct kvm_arch { · 162 unmodified lines
557564
558565 struct list_head assigned_dev_head;
559566 struct iommu_domain *iommu_domain;
560− int iommu_flags;
567+ bool iommu_noncoherent;
568+#define __KVM_HAVE_ARCH_NONCOHERENT_DMA
569+ atomic_t noncoherent_dma_count;
561570 struct kvm_pic *vpic;
562571 struct kvm_ioapic *vioapic;
563572 struct kvm_pit *vpit;
@@ -780,11 +789,11 @@ void kvm_mmu_module_exit(void); · 216 unmodified lines
780789
781790 void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
782791 int kvm_mmu_create(struct kvm_vcpu *vcpu);
783−int kvm_mmu_setup(struct kvm_vcpu *vcpu);
792+void kvm_mmu_setup(struct kvm_vcpu *vcpu);
784793 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
785794 u64 dirty_mask, u64 nx_mask, u64 x_mask);
786795
787−int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
796+void kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
788797 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
789798 void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
790799 struct kvm_memory_slot *slot,
@@ -922,13 +931,11 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu); · 131 unmodified lines
922931 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code,
923932 void *insn, int insn_len);
924933 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
934+void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu);
925935
926936 void kvm_enable_tdp(void);
927937 void kvm_disable_tdp(void);
928938
929−int complete_pio(struct kvm_vcpu *vcpu);
930−bool kvm_check_iopl(struct kvm_vcpu *vcpu);
931−
932939 static inline gpa_t translate_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access)
933940 {
934941 return gpa;
arch/x86/include/asm/pvclock.h+2−0
@@ -14,6 +14,8 @@ void pvclock_read_wallclock(struct pvclock_wall_clock *wall, · 13 unmodified lines
1414 struct timespec *ts);
1515 void pvclock_resume(void);
1616
17+void pvclock_touch_watchdogs(void);
18+
1719 /*
1820 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
1921 * yielding a 64-bit result.
arch/x86/include/uapi/asm/kvm.h+3−3
@@ -211,9 +211,9 @@ struct kvm_cpuid_entry2 { · 210 unmodified lines
211211 __u32 padding[3];
212212 };
213213
214−#define KVM_CPUID_FLAG_SIGNIFCANT_INDEX 1
215−#define KVM_CPUID_FLAG_STATEFUL_FUNC 2
216−#define KVM_CPUID_FLAG_STATE_READ_NEXT 4
214+#define KVM_CPUID_FLAG_SIGNIFCANT_INDEX BIT(0)
215+#define KVM_CPUID_FLAG_STATEFUL_FUNC BIT(1)
216+#define KVM_CPUID_FLAG_STATE_READ_NEXT BIT(2)
217217
218218 /* for KVM_SET_CPUID2 */
219219 struct kvm_cpuid2 {
arch/x86/include/uapi/asm/msr-index.h+1−0
@@ -536,6 +536,7 @@ · 535 unmodified lines
536536
537537 /* MSR_IA32_VMX_MISC bits */
538538 #define MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS (1ULL << 29)
539+#define MSR_IA32_VMX_MISC_PREEMPTION_TIMER_SCALE 0x1F
539540 /* AMD-V MSRs */
540541
541542 #define MSR_VM_CR 0xc0010114
arch/x86/kernel/kvmclock.c+1−0
@@ -139,6 +139,7 @@ bool kvm_check_and_clear_guest_paused(void) · 138 unmodified lines
139139 src = &hv_clock[cpu].pvti;
140140 if ((src->flags & PVCLOCK_GUEST_STOPPED) != 0) {
141141 src->flags &= ~PVCLOCK_GUEST_STOPPED;
142+ pvclock_touch_watchdogs();
142143 ret = true;
143144 }
144145
arch/x86/kernel/pvclock.c+13−0
@@ -43,6 +43,14 @@ unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src) · 42 unmodified lines
4343 return pv_tsc_khz;
4444 }
4545
46+void pvclock_touch_watchdogs(void)
47+{
48+ touch_softlockup_watchdog_sync();
49+ clocksource_touch_watchdog();
50+ rcu_cpu_stall_reset();
51+ reset_hung_task_detector();
52+}
53+
4654 static atomic64_t last_value = ATOMIC64_INIT(0);
4755
4856 void pvclock_resume(void)
@@ -74,6 +82,11 @@ cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) · 25 unmodified lines
7482 version = __pvclock_read_cycles(src, &ret, &flags);
7583 } while ((src->version & 1) || version != src->version);
7684
85+ if (unlikely((flags & PVCLOCK_GUEST_STOPPED) != 0)) {
86+ src->flags &= ~PVCLOCK_GUEST_STOPPED;
87+ pvclock_touch_watchdogs();
88+ }
89+
7790 if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
7891 (flags & PVCLOCK_TSC_STABLE_BIT))
7992 return ret;
arch/x86/kvm/Kconfig+1−0
@@ -38,6 +38,7 @@ config KVM · 37 unmodified lines
3838 select PERF_EVENTS
3939 select HAVE_KVM_MSI
4040 select HAVE_KVM_CPU_RELAX_INTERCEPT
41+ select KVM_VFIO
4142 ---help---
4243 Support hosting fully virtualized guest machines using hardware
4344 virtualization extensions. You will need a fairly recent
arch/x86/kvm/Makefile+1−1
@@ -9,7 +9,7 @@ KVM := ../../../virt/kvm · 8 unmodified lines
99
1010 kvm-y += $(KVM)/kvm_main.o $(KVM)/ioapic.o \
1111 $(KVM)/coalesced_mmio.o $(KVM)/irq_comm.o \
12− $(KVM)/eventfd.o $(KVM)/irqchip.o
12+ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o
1313 kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += $(KVM)/assigned-dev.o $(KVM)/iommu.o
1414 kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
1515
arch/x86/kvm/cpuid.c+106−9
@@ -23,6 +23,26 @@ · 22 unmodified lines
2323 #include "mmu.h"
2424 #include "trace.h"
2525
26+static u32 xstate_required_size(u64 xstate_bv)
27+{
28+ int feature_bit = 0;
29+ u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
30+
31+ xstate_bv &= ~XSTATE_FPSSE;
32+ while (xstate_bv) {
33+ if (xstate_bv & 0x1) {
34+ u32 eax, ebx, ecx, edx;
35+ cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
36+ ret = max(ret, eax + ebx);
37+ }
38+
39+ xstate_bv >>= 1;
40+ feature_bit++;
41+ }
42+
43+ return ret;
44+}
45+
2646 void kvm_update_cpuid(struct kvm_vcpu *vcpu)
2747 {
2848 struct kvm_cpuid_entry2 *best;
@@ -46,6 +66,18 @@ void kvm_update_cpuid(struct kvm_vcpu *vcpu) · 17 unmodified lines
4666 apic->lapic_timer.timer_mode_mask = 1 << 17;
4767 }
4868
69+ best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
70+ if (!best) {
71+ vcpu->arch.guest_supported_xcr0 = 0;
72+ vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
73+ } else {
74+ vcpu->arch.guest_supported_xcr0 =
75+ (best->eax | ((u64)best->edx << 32)) &
76+ host_xcr0 & KVM_SUPPORTED_XCR0;
77+ vcpu->arch.guest_xstate_size =
78+ xstate_required_size(vcpu->arch.guest_supported_xcr0);
79+ }
80+
4981 kvm_pmu_cpuid_update(vcpu);
5082 }
5183
@@ -182,13 +214,35 @@ static bool supported_xcr0_bit(unsigned bit) · 130 unmodified lines
182214 {
183215 u64 mask = ((u64)1 << bit);
184216
185− return mask & (XSTATE_FP | XSTATE_SSE | XSTATE_YMM) & host_xcr0;
217+ return mask & KVM_SUPPORTED_XCR0 & host_xcr0;
186218 }
187219
188220 #define F(x) bit(X86_FEATURE_##x)
189221
190−static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
191− u32 index, int *nent, int maxnent)
222+static int __do_cpuid_ent_emulated(struct kvm_cpuid_entry2 *entry,
223+ u32 func, u32 index, int *nent, int maxnent)
224+{
225+ switch (func) {
226+ case 0:
227+ entry->eax = 1; /* only one leaf currently */
228+ ++*nent;
229+ break;
230+ case 1:
231+ entry->ecx = F(MOVBE);
232+ ++*nent;
233+ break;
234+ default:
235+ break;
236+ }
237+
238+ entry->function = func;
239+ entry->index = index;
240+
241+ return 0;
242+}
243+
244+static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
245+ u32 index, int *nent, int maxnent)
192246 {
193247 int r;
194248 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
@@ -383,6 +437,8 @@ static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, · 188 unmodified lines
383437 case 0xd: {
384438 int idx, i;
385439
440+ entry->eax &= host_xcr0 & KVM_SUPPORTED_XCR0;
441+ entry->edx &= (host_xcr0 & KVM_SUPPORTED_XCR0) >> 32;
386442 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
387443 for (idx = 1, i = 1; idx < 64; ++idx) {
388444 if (*nent >= maxnent)
@@ -481,6 +537,15 @@ out: · 92 unmodified lines
481537 return r;
482538 }
483539
540+static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 func,
541+ u32 idx, int *nent, int maxnent, unsigned int type)
542+{
543+ if (type == KVM_GET_EMULATED_CPUID)
544+ return __do_cpuid_ent_emulated(entry, func, idx, nent, maxnent);
545+
546+ return __do_cpuid_ent(entry, func, idx, nent, maxnent);
547+}
548+
484549 #undef F
485550
486551 struct kvm_cpuid_param {
@@ -495,8 +560,36 @@ static bool is_centaur_cpu(const struct kvm_cpuid_param *param) · 8 unmodified lines
495560 return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR;
496561 }
497562
498−int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
499− struct kvm_cpuid_entry2 __user *entries)
563+static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
564+ __u32 num_entries, unsigned int ioctl_type)
565+{
566+ int i;
567+ __u32 pad[3];
568+
569+ if (ioctl_type != KVM_GET_EMULATED_CPUID)
570+ return false;
571+
572+ /*
573+ * We want to make sure that ->padding is being passed clean from
574+ * userspace in case we want to use it for something in the future.
575+ *
576+ * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
577+ * have to give ourselves satisfied only with the emulated side. /me
578+ * sheds a tear.
579+ */
580+ for (i = 0; i < num_entries; i++) {
581+ if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
582+ return true;
583+
584+ if (pad[0] || pad[1] || pad[2])
585+ return true;
586+ }
587+ return false;
588+}
589+
590+int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
591+ struct kvm_cpuid_entry2 __user *entries,
592+ unsigned int type)
500593 {
501594 struct kvm_cpuid_entry2 *cpuid_entries;
502595 int limit, nent = 0, r = -E2BIG, i;
@@ -513,8 +606,12 @@ int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, · 10 unmodified lines
513606 goto out;
514607 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
515608 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
609+
610+ if (sanity_check_entries(entries, cpuid->nent, type))
611+ return -EINVAL;
612+
516613 r = -ENOMEM;
517− cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
614+ cpuid_entries = vzalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
518615 if (!cpuid_entries)
519616 goto out;
520617
@@ -526,7 +623,7 @@ int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, · 5 unmodified lines
526623 continue;
527624
528625 r = do_cpuid_ent(&cpuid_entries[nent], ent->func, ent->idx,
529− &nent, cpuid->nent);
626+ &nent, cpuid->nent, type);
530627
531628 if (r)
532629 goto out_free;
@@ -537,7 +634,7 @@ int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, · 4 unmodified lines
537634 limit = cpuid_entries[nent - 1].eax;
538635 for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func)
539636 r = do_cpuid_ent(&cpuid_entries[nent], func, ent->idx,
540− &nent, cpuid->nent);
637+ &nent, cpuid->nent, type);
541638
542639 if (r)
543640 goto out_free;
@@ -661,6 +758,7 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx) · 117 unmodified lines
661758 *edx = best->edx;
662759 } else
663760 *eax = *ebx = *ecx = *edx = 0;
761+ trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx);
664762 }
665763 EXPORT_SYMBOL_GPL(kvm_cpuid);
666764
@@ -676,6 +774,5 @@ void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) · 9 unmodified lines
676774 kvm_register_write(vcpu, VCPU_REGS_RCX, ecx);
677775 kvm_register_write(vcpu, VCPU_REGS_RDX, edx);
678776 kvm_x86_ops->skip_emulated_instruction(vcpu);
679− trace_kvm_cpuid(function, eax, ebx, ecx, edx);
680777 }
681778 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
arch/x86/kvm/cpuid.h+3−2
@@ -6,8 +6,9 @@ · 5 unmodified lines
66 void kvm_update_cpuid(struct kvm_vcpu *vcpu);
77 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
88 u32 function, u32 index);
9−int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
10− struct kvm_cpuid_entry2 __user *entries);
9+int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
10+ struct kvm_cpuid_entry2 __user *entries,
11+ unsigned int type);
1112 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1213 struct kvm_cpuid *cpuid,
1314 struct kvm_cpuid_entry __user *entries);
arch/x86/kvm/emulate.c+107−23
@@ -130,7 +130,7 @@ · 129 unmodified lines
130130 #define Mov (1<<20)
131131 /* Misc flags */
132132 #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
133−#define VendorSpecific (1<<22) /* Vendor specific instruction */
133+#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
134134 #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
135135 #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
136136 #define Undefined (1<<25) /* No Such Instruction */
@@ -785,9 +785,10 @@ static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, · 648 unmodified lines
785785 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
786786 */
787787 static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
788− int highbyte_regs)
788+ int byteop)
789789 {
790790 void *p;
791+ int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
791792
792793 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
793794 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
@@ -1024,7 +1025,6 @@ static void decode_register_operand(struct x86_emulate_ctxt *ctxt, · 230 unmodified lines
10241025 struct operand *op)
10251026 {
10261027 unsigned reg = ctxt->modrm_reg;
1027− int highbyte_regs = ctxt->rex_prefix == 0;
10281028
10291029 if (!(ctxt->d & ModRM))
10301030 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
@@ -1045,13 +1045,9 @@ static void decode_register_operand(struct x86_emulate_ctxt *ctxt, · 14 unmodified lines
10451045 }
10461046
10471047 op->type = OP_REG;
1048− if (ctxt->d & ByteOp) {
1049− op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
1050− op->bytes = 1;
1051− } else {
1052− op->addr.reg = decode_register(ctxt, reg, 0);
1053− op->bytes = ctxt->op_bytes;
1054− }
1048+ op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1049+ op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1050+
10551051 fetch_register_operand(op);
10561052 op->orig_val = op->val;
10571053 }
@@ -1082,12 +1078,10 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt, · 24 unmodified lines
10821078 ctxt->modrm_seg = VCPU_SREG_DS;
10831079
10841080 if (ctxt->modrm_mod == 3) {
1085− int highbyte_regs = ctxt->rex_prefix == 0;
1086−
10871081 op->type = OP_REG;
10881082 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
10891083 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
1090− highbyte_regs && (ctxt->d & ByteOp));
1084+ ctxt->d & ByteOp);
10911085 if (ctxt->d & Sse) {
10921086 op->type = OP_XMM;
10931087 op->bytes = 16;
@@ -2961,6 +2955,46 @@ static int em_mov(struct x86_emulate_ctxt *ctxt) · 1867 unmodified lines
29612955 return X86EMUL_CONTINUE;
29622956 }
29632957
2958+#define FFL(x) bit(X86_FEATURE_##x)
2959+
2960+static int em_movbe(struct x86_emulate_ctxt *ctxt)
2961+{
2962+ u32 ebx, ecx, edx, eax = 1;
2963+ u16 tmp;
2964+
2965+ /*
2966+ * Check MOVBE is set in the guest-visible CPUID leaf.
2967+ */
2968+ ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2969+ if (!(ecx & FFL(MOVBE)))
2970+ return emulate_ud(ctxt);
2971+
2972+ switch (ctxt->op_bytes) {
2973+ case 2:
2974+ /*
2975+ * From MOVBE definition: "...When the operand size is 16 bits,
2976+ * the upper word of the destination register remains unchanged
2977+ * ..."
2978+ *
2979+ * Both casting ->valptr and ->val to u16 breaks strict aliasing
2980+ * rules so we have to do the operation almost per hand.
2981+ */
2982+ tmp = (u16)ctxt->src.val;
2983+ ctxt->dst.val &= ~0xffffUL;
2984+ ctxt->dst.val |= (unsigned long)swab16(tmp);
2985+ break;
2986+ case 4:
2987+ ctxt->dst.val = swab32((u32)ctxt->src.val);
2988+ break;
2989+ case 8:
2990+ ctxt->dst.val = swab64(ctxt->src.val);
2991+ break;
2992+ default:
2993+ return X86EMUL_PROPAGATE_FAULT;
2994+ }
2995+ return X86EMUL_CONTINUE;
2996+}
2997+
29642998 static int em_cr_write(struct x86_emulate_ctxt *ctxt)
29652999 {
29663000 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
@@ -3256,6 +3290,18 @@ static int em_cpuid(struct x86_emulate_ctxt *ctxt) · 289 unmodified lines
32563290 return X86EMUL_CONTINUE;
32573291 }
32583292
3293+static int em_sahf(struct x86_emulate_ctxt *ctxt)
3294+{
3295+ u32 flags;
3296+
3297+ flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3298+ flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3299+
3300+ ctxt->eflags &= ~0xffUL;
3301+ ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3302+ return X86EMUL_CONTINUE;
3303+}
3304+
32593305 static int em_lahf(struct x86_emulate_ctxt *ctxt)
32603306 {
32613307 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
@@ -3502,7 +3548,7 @@ static const struct opcode group7_rm1[] = { · 240 unmodified lines
35023548
35033549 static const struct opcode group7_rm3[] = {
35043550 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3505− II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3551+ II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
35063552 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
35073553 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
35083554 DIP(SrcNone | Prot | Priv, stgi, check_svme),
@@ -3587,7 +3633,7 @@ static const struct group_dual group7 = { { · 78 unmodified lines
35873633 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
35883634 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
35893635 }, {
3590− I(SrcNone | Priv | VendorSpecific, em_vmcall),
3636+ I(SrcNone | Priv | EmulateOnUD, em_vmcall),
35913637 EXT(0, group7_rm1),
35923638 N, EXT(0, group7_rm3),
35933639 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
@@ -3750,7 +3796,8 @@ static const struct opcode opcode_table[256] = { · 156 unmodified lines
37503796 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
37513797 I(SrcImmFAddr | No64, em_call_far), N,
37523798 II(ImplicitOps | Stack, em_pushf, pushf),
3753− II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
3799+ II(ImplicitOps | Stack, em_popf, popf),
3800+ I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
37543801 /* 0xA0 - 0xA7 */
37553802 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
37563803 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
@@ -3810,7 +3857,7 @@ static const struct opcode opcode_table[256] = { · 53 unmodified lines
38103857 static const struct opcode twobyte_table[256] = {
38113858 /* 0x00 - 0x0F */
38123859 G(0, group6), GD(0, &group7), N, N,
3813− N, I(ImplicitOps | VendorSpecific, em_syscall),
3860+ N, I(ImplicitOps | EmulateOnUD, em_syscall),
38143861 II(ImplicitOps | Priv, em_clts, clts), N,
38153862 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
38163863 N, D(ImplicitOps | ModRM), N, N,
@@ -3830,8 +3877,8 @@ static const struct opcode twobyte_table[256] = { · 13 unmodified lines
38303877 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
38313878 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
38323879 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
3833− I(ImplicitOps | VendorSpecific, em_sysenter),
3834− I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
3880+ I(ImplicitOps | EmulateOnUD, em_sysenter),
3881+ I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
38353882 N, N,
38363883 N, N, N, N, N, N, N, N,
38373884 /* 0x40 - 0x4F */
@@ -3892,6 +3939,30 @@ static const struct opcode twobyte_table[256] = { · 54 unmodified lines
38923939 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
38933940 };
38943941
3942+static const struct gprefix three_byte_0f_38_f0 = {
3943+ I(DstReg | SrcMem | Mov, em_movbe), N, N, N
3944+};
3945+
3946+static const struct gprefix three_byte_0f_38_f1 = {
3947+ I(DstMem | SrcReg | Mov, em_movbe), N, N, N
3948+};
3949+
3950+/*
3951+ * Insns below are selected by the prefix which indexed by the third opcode
3952+ * byte.
3953+ */
3954+static const struct opcode opcode_map_0f_38[256] = {
3955+ /* 0x00 - 0x7f */
3956+ X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3957+ /* 0x80 - 0xef */
3958+ X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3959+ /* 0xf0 - 0xf1 */
3960+ GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
3961+ GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
3962+ /* 0xf2 - 0xff */
3963+ N, N, X4(N), X8(N)
3964+};
3965+
38953966 #undef D
38963967 #undef N
38973968 #undef G
@@ -4040,7 +4111,8 @@ static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, · 142 unmodified lines
40404111 case OpMem8:
40414112 ctxt->memop.bytes = 1;
40424113 if (ctxt->memop.type == OP_REG) {
4043− ctxt->memop.addr.reg = decode_register(ctxt, ctxt->modrm_rm, 1);
4114+ ctxt->memop.addr.reg = decode_register(ctxt,
4115+ ctxt->modrm_rm, true);
40444116 fetch_register_operand(&ctxt->memop);
40454117 }
40464118 goto mem_common;
@@ -4126,6 +4198,7 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · 79 unmodified lines
41264198 ctxt->_eip = ctxt->eip;
41274199 ctxt->fetch.start = ctxt->_eip;
41284200 ctxt->fetch.end = ctxt->fetch.start + insn_len;
4201+ ctxt->opcode_len = 1;
41294202 if (insn_len > 0)
41304203 memcpy(ctxt->fetch.data, insn, insn_len);
41314204
@@ -4208,9 +4281,16 @@ done_prefixes: · 76 unmodified lines
42084281 opcode = opcode_table[ctxt->b];
42094282 /* Two-byte opcode? */
42104283 if (ctxt->b == 0x0f) {
4211− ctxt->twobyte = 1;
4284+ ctxt->opcode_len = 2;
42124285 ctxt->b = insn_fetch(u8, ctxt);
42134286 opcode = twobyte_table[ctxt->b];
4287+
4288+ /* 0F_38 opcode map */
4289+ if (ctxt->b == 0x38) {
4290+ ctxt->opcode_len = 3;
4291+ ctxt->b = insn_fetch(u8, ctxt);
4292+ opcode = opcode_map_0f_38[ctxt->b];
4293+ }
42144294 }
42154295 ctxt->d = opcode.flags;
42164296
@@ -4267,7 +4347,7 @@ done_prefixes: · 50 unmodified lines
42674347 if (ctxt->d == 0 || (ctxt->d & NotImpl))
42684348 return EMULATION_FAILED;
42694349
4270− if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
4350+ if (!(ctxt->d & EmulateOnUD) && ctxt->ud)
42714351 return EMULATION_FAILED;
42724352
42734353 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
@@ -4540,8 +4620,10 @@ special_insn: · 266 unmodified lines
45404620 goto writeback;
45414621 }
45424622
4543− if (ctxt->twobyte)
4623+ if (ctxt->opcode_len == 2)
45444624 goto twobyte_insn;
4625+ else if (ctxt->opcode_len == 3)
4626+ goto threebyte_insn;
45454627
45464628 switch (ctxt->b) {
45474629 case 0x63: /* movsxd */
@@ -4726,6 +4808,8 @@ twobyte_insn: · 178 unmodified lines
47264808 goto cannot_emulate;
47274809 }
47284810
4811+threebyte_insn:
4812+
47294813 if (rc != X86EMUL_CONTINUE)
47304814 goto done;
47314815
arch/x86/kvm/mmu.c+34−81
@@ -2570,11 +2570,6 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, · 2569 unmodified lines
25702570 kvm_release_pfn_clean(pfn);
25712571 }
25722572
2573−static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2574−{
2575− mmu_free_roots(vcpu);
2576−}
2577−
25782573 static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
25792574 bool no_dirty_log)
25802575 {
@@ -3424,18 +3419,11 @@ out_unlock: · 843 unmodified lines
34243419 return 0;
34253420 }
34263421
3427−static void nonpaging_free(struct kvm_vcpu *vcpu)
3428−{
3429− mmu_free_roots(vcpu);
3430−}
3431−
3432−static int nonpaging_init_context(struct kvm_vcpu *vcpu,
3433− struct kvm_mmu *context)
3422+static void nonpaging_init_context(struct kvm_vcpu *vcpu,
3423+ struct kvm_mmu *context)
34343424 {
3435− context->new_cr3 = nonpaging_new_cr3;
34363425 context->page_fault = nonpaging_page_fault;
34373426 context->gva_to_gpa = nonpaging_gva_to_gpa;
3438− context->free = nonpaging_free;
34393427 context->sync_page = nonpaging_sync_page;
34403428 context->invlpg = nonpaging_invlpg;
34413429 context->update_pte = nonpaging_update_pte;
@@ -3444,7 +3432,6 @@ static int nonpaging_init_context(struct kvm_vcpu *vcpu, · 2 unmodified lines
34443432 context->root_hpa = INVALID_PAGE;
34453433 context->direct_map = true;
34463434 context->nx = false;
3447− return 0;
34483435 }
34493436
34503437 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
@@ -3454,9 +3441,8 @@ void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) · 3 unmodified lines
34543441 }
34553442 EXPORT_SYMBOL_GPL(kvm_mmu_flush_tlb);
34563443
3457−static void paging_new_cr3(struct kvm_vcpu *vcpu)
3444+void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu)
34583445 {
3459− pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
34603446 mmu_free_roots(vcpu);
34613447 }
34623448
@@ -3471,11 +3457,6 @@ static void inject_page_fault(struct kvm_vcpu *vcpu, · 8 unmodified lines
34713457 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
34723458 }
34733459
3474−static void paging_free(struct kvm_vcpu *vcpu)
3475−{
3476− nonpaging_free(vcpu);
3477−}
3478−
34793460 static bool sync_mmio_spte(struct kvm *kvm, u64 *sptep, gfn_t gfn,
34803461 unsigned access, int *nr_present)
34813462 {
@@ -3665,9 +3646,9 @@ static void update_last_pte_bitmap(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) · 183 unmodified lines
36653646 mmu->last_pte_bitmap = map;
36663647 }
36673648
3668−static int paging64_init_context_common(struct kvm_vcpu *vcpu,
3669− struct kvm_mmu *context,
3670− int level)
3649+static void paging64_init_context_common(struct kvm_vcpu *vcpu,
3650+ struct kvm_mmu *context,
3651+ int level)
36713652 {
36723653 context->nx = is_nx(vcpu);
36733654 context->root_level = level;
@@ -3677,27 +3658,24 @@ static int paging64_init_context_common(struct kvm_vcpu *vcpu, · 3 unmodified lines
36773658 update_last_pte_bitmap(vcpu, context);
36783659
36793660 ASSERT(is_pae(vcpu));
3680− context->new_cr3 = paging_new_cr3;
36813661 context->page_fault = paging64_page_fault;
36823662 context->gva_to_gpa = paging64_gva_to_gpa;
36833663 context->sync_page = paging64_sync_page;
36843664 context->invlpg = paging64_invlpg;
36853665 context->update_pte = paging64_update_pte;
3686− context->free = paging_free;
36873666 context->shadow_root_level = level;
36883667 context->root_hpa = INVALID_PAGE;
36893668 context->direct_map = false;
3690− return 0;
36913669 }
36923670
3693−static int paging64_init_context(struct kvm_vcpu *vcpu,
3694− struct kvm_mmu *context)
3671+static void paging64_init_context(struct kvm_vcpu *vcpu,
3672+ struct kvm_mmu *context)
36953673 {
3696− return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
3674+ paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
36973675 }
36983676
3699−static int paging32_init_context(struct kvm_vcpu *vcpu,
3700− struct kvm_mmu *context)
3677+static void paging32_init_context(struct kvm_vcpu *vcpu,
3678+ struct kvm_mmu *context)
37013679 {
37023680 context->nx = false;
37033681 context->root_level = PT32_ROOT_LEVEL;
@@ -3706,33 +3684,28 @@ static int paging32_init_context(struct kvm_vcpu *vcpu, · 2 unmodified lines
37063684 update_permission_bitmask(vcpu, context, false);
37073685 update_last_pte_bitmap(vcpu, context);
37083686
3709− context->new_cr3 = paging_new_cr3;
37103687 context->page_fault = paging32_page_fault;
37113688 context->gva_to_gpa = paging32_gva_to_gpa;
3712− context->free = paging_free;
37133689 context->sync_page = paging32_sync_page;
37143690 context->invlpg = paging32_invlpg;
37153691 context->update_pte = paging32_update_pte;
37163692 context->shadow_root_level = PT32E_ROOT_LEVEL;
37173693 context->root_hpa = INVALID_PAGE;
37183694 context->direct_map = false;
3719− return 0;
37203695 }
37213696
3722−static int paging32E_init_context(struct kvm_vcpu *vcpu,
3723− struct kvm_mmu *context)
3697+static void paging32E_init_context(struct kvm_vcpu *vcpu,
3698+ struct kvm_mmu *context)
37243699 {
3725− return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
3700+ paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
37263701 }
37273702
3728−static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
3703+static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
37293704 {
37303705 struct kvm_mmu *context = vcpu->arch.walk_mmu;
37313706
37323707 context->base_role.word = 0;
3733− context->new_cr3 = nonpaging_new_cr3;
37343708 context->page_fault = tdp_page_fault;
3735− context->free = nonpaging_free;
37363709 context->sync_page = nonpaging_sync_page;
37373710 context->invlpg = nonpaging_invlpg;
37383711 context->update_pte = nonpaging_update_pte;
@@ -3767,37 +3740,32 @@ static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) · 28 unmodified lines
37673740
37683741 update_permission_bitmask(vcpu, context, false);
37693742 update_last_pte_bitmap(vcpu, context);
3770−
3771− return 0;
37723743 }
37733744
3774−int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
3745+void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
37753746 {
3776− int r;
37773747 bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
37783748 ASSERT(vcpu);
37793749 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
37803750
37813751 if (!is_paging(vcpu))
3782− r = nonpaging_init_context(vcpu, context);
3752+ nonpaging_init_context(vcpu, context);
37833753 else if (is_long_mode(vcpu))
3784− r = paging64_init_context(vcpu, context);
3754+ paging64_init_context(vcpu, context);
37853755 else if (is_pae(vcpu))
3786− r = paging32E_init_context(vcpu, context);
3756+ paging32E_init_context(vcpu, context);
37873757 else
3788− r = paging32_init_context(vcpu, context);
3758+ paging32_init_context(vcpu, context);
37893759
37903760 vcpu->arch.mmu.base_role.nxe = is_nx(vcpu);
37913761 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
37923762 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
37933763 vcpu->arch.mmu.base_role.smep_andnot_wp
37943764 = smep && !is_write_protection(vcpu);
3795−
3796− return r;
37973765 }
37983766 EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
37993767
3800−int kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context,
3768+void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context,
38013769 bool execonly)
38023770 {
38033771 ASSERT(vcpu);
@@ -3806,37 +3774,30 @@ int kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context, · 2 unmodified lines
38063774 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
38073775
38083776 context->nx = true;
3809− context->new_cr3 = paging_new_cr3;
38103777 context->page_fault = ept_page_fault;
38113778 context->gva_to_gpa = ept_gva_to_gpa;
38123779 context->sync_page = ept_sync_page;
38133780 context->invlpg = ept_invlpg;
38143781 context->update_pte = ept_update_pte;
3815− context->free = paging_free;
38163782 context->root_level = context->shadow_root_level;
38173783 context->root_hpa = INVALID_PAGE;
38183784 context->direct_map = false;
38193785
38203786 update_permission_bitmask(vcpu, context, true);
38213787 reset_rsvds_bits_mask_ept(vcpu, context, execonly);
3822−
3823− return 0;
38243788 }
38253789 EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu);
38263790
3827−static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
3791+static void init_kvm_softmmu(struct kvm_vcpu *vcpu)
38283792 {
3829− int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
3830−
3793+ kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
38313794 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
38323795 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
38333796 vcpu->arch.walk_mmu->get_pdptr = kvm_pdptr_read;
38343797 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
3835−
3836− return r;
38373798 }
38383799
3839−static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
3800+static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
38403801 {
38413802 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
38423803
@@ -3873,11 +3834,9 @@ static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu) · 30 unmodified lines
38733834
38743835 update_permission_bitmask(vcpu, g_context, false);
38753836 update_last_pte_bitmap(vcpu, g_context);
3876−
3877− return 0;
38783837 }
38793838
3880−static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3839+static void init_kvm_mmu(struct kvm_vcpu *vcpu)
38813840 {
38823841 if (mmu_is_nested(vcpu))
38833842 return init_kvm_nested_mmu(vcpu);
@@ -3887,18 +3846,12 @@ static int init_kvm_mmu(struct kvm_vcpu *vcpu) · 3 unmodified lines
38873846 return init_kvm_softmmu(vcpu);
38883847 }
38893848
3890−static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3849+void kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
38913850 {
38923851 ASSERT(vcpu);
3893− if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3894− /* mmu.free() should set root_hpa = INVALID_PAGE */
3895− vcpu->arch.mmu.free(vcpu);
3896−}
38973852
3898−int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3899−{
3900− destroy_kvm_mmu(vcpu);
3901− return init_kvm_mmu(vcpu);
3853+ kvm_mmu_unload(vcpu);
3854+ init_kvm_mmu(vcpu);
39023855 }
39033856 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
39043857
@@ -3923,6 +3876,7 @@ EXPORT_SYMBOL_GPL(kvm_mmu_load); · 18 unmodified lines
39233876 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
39243877 {
39253878 mmu_free_roots(vcpu);
3879+ WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa));
39263880 }
39273881 EXPORT_SYMBOL_GPL(kvm_mmu_unload);
39283882
@@ -4281,12 +4235,12 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) · 352 unmodified lines
42814235 return alloc_mmu_pages(vcpu);
42824236 }
42834237
4284−int kvm_mmu_setup(struct kvm_vcpu *vcpu)
4238+void kvm_mmu_setup(struct kvm_vcpu *vcpu)
42854239 {
42864240 ASSERT(vcpu);
42874241 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
42884242
4289− return init_kvm_mmu(vcpu);
4243+ init_kvm_mmu(vcpu);
42904244 }
42914245
42924246 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
@@ -4428,7 +4382,7 @@ mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) · 135 unmodified lines
44284382 int nr_to_scan = sc->nr_to_scan;
44294383 unsigned long freed = 0;
44304384
4431− raw_spin_lock(&kvm_lock);
4385+ spin_lock(&kvm_lock);
44324386
44334387 list_for_each_entry(kvm, &vm_list, vm_list) {
44344388 int idx;
@@ -4478,9 +4432,8 @@ unlock: · 43 unmodified lines
44784432 break;
44794433 }
44804434
4481− raw_spin_unlock(&kvm_lock);
4435+ spin_unlock(&kvm_lock);
44824436 return freed;
4483−
44844437 }
44854438
44864439 static unsigned long
@@ -4574,7 +4527,7 @@ void kvm_mmu_destroy(struct kvm_vcpu *vcpu) · 87 unmodified lines
45744527 {
45754528 ASSERT(vcpu);
45764529
4577− destroy_kvm_mmu(vcpu);
4530+ kvm_mmu_unload(vcpu);
45784531 free_mmu_pages(vcpu);
45794532 mmu_free_memory_caches(vcpu);
45804533 }
arch/x86/kvm/mmu.h+2−2
@@ -70,8 +70,8 @@ enum { · 69 unmodified lines
7070 };
7171
7272 int handle_mmio_page_fault_common(struct kvm_vcpu *vcpu, u64 addr, bool direct);
73−int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context);
74−int kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context,
73+void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context);
74+void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context,
7575 bool execonly);
7676
7777 static inline unsigned int kvm_mmu_available_pages(struct kvm *kvm)
arch/x86/kvm/svm.c+2−6
@@ -1959,11 +1959,9 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, · 1958 unmodified lines
19591959 nested_svm_vmexit(svm);
19601960 }
19611961
1962−static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1962+static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
19631963 {
1964− int r;
1965−
1966− r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1964+ kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
19671965
19681966 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
19691967 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
@@ -1971,8 +1969,6 @@ static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) · 1 unmodified line
19711969 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
19721970 vcpu->arch.mmu.shadow_root_level = get_npt_level();
19731971 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1974−
1975− return r;
19761972 }
19771973
19781974 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx.c+113−45
@@ -1498,7 +1498,7 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · 1497 unmodified lines
14981498 break;
14991499
15001500 if (i == NR_AUTOLOAD_MSRS) {
1501− printk_once(KERN_WARNING"Not enough mst switch entries. "
1501+ printk_once(KERN_WARNING "Not enough msr switch entries. "
15021502 "Can't add msr %x\n", msr);
15031503 return;
15041504 } else if (i == m->nr) {
@@ -1898,16 +1898,12 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu) · 393 unmodified lines
18981898 /*
18991899 * KVM wants to inject page-faults which it got to the guest. This function
19001900 * checks whether in a nested guest, we need to inject them to L1 or L2.
1901− * This function assumes it is called with the exit reason in vmcs02 being
1902− * a #PF exception (this is the only case in which KVM injects a #PF when L2
1903− * is running).
19041901 */
1905−static int nested_pf_handled(struct kvm_vcpu *vcpu)
1902+static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
19061903 {
19071904 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
19081905
1909− /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
1910− if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR)))
1906+ if (!(vmcs12->exception_bitmap & (1u << nr)))
19111907 return 0;
19121908
19131909 nested_vmx_vmexit(vcpu);
@@ -1921,8 +1917,8 @@ static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, · 7 unmodified lines
19211917 struct vcpu_vmx *vmx = to_vmx(vcpu);
19221918 u32 intr_info = nr | INTR_INFO_VALID_MASK;
19231919
1924− if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
1925− !vmx->nested.nested_run_pending && nested_pf_handled(vcpu))
1920+ if (!reinject && is_guest_mode(vcpu) &&
1921+ nested_vmx_check_exception(vcpu, nr))
19261922 return;
19271923
19281924 if (has_error_code) {
@@ -2204,9 +2200,15 @@ static __init void nested_vmx_setup_ctls_msrs(void) · 275 unmodified lines
22042200 #ifdef CONFIG_X86_64
22052201 VM_EXIT_HOST_ADDR_SPACE_SIZE |
22062202 #endif
2207− VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2203+ VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
2204+ VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
2205+ if (!(nested_vmx_pinbased_ctls_high & PIN_BASED_VMX_PREEMPTION_TIMER) ||
2206+ !(nested_vmx_exit_ctls_high & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)) {
2207+ nested_vmx_exit_ctls_high &= ~VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
2208+ nested_vmx_pinbased_ctls_high &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
2209+ }
22082210 nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2209− VM_EXIT_LOAD_IA32_EFER);
2211+ VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER);
22102212
22112213 /* entry controls */
22122214 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
@@ -2226,7 +2228,8 @@ static __init void nested_vmx_setup_ctls_msrs(void) · 13 unmodified lines
22262228 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
22272229 nested_vmx_procbased_ctls_low = 0;
22282230 nested_vmx_procbased_ctls_high &=
2229− CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
2231+ CPU_BASED_VIRTUAL_INTR_PENDING |
2232+ CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
22302233 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
22312234 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
22322235 CPU_BASED_CR3_STORE_EXITING |
@@ -2252,13 +2255,15 @@ static __init void nested_vmx_setup_ctls_msrs(void) · 19 unmodified lines
22522255 nested_vmx_secondary_ctls_low = 0;
22532256 nested_vmx_secondary_ctls_high &=
22542257 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2258+ SECONDARY_EXEC_UNRESTRICTED_GUEST |
22552259 SECONDARY_EXEC_WBINVD_EXITING;
22562260
22572261 if (enable_ept) {
22582262 /* nested EPT: emulate EPT also to L1 */
22592263 nested_vmx_secondary_ctls_high |= SECONDARY_EXEC_ENABLE_EPT;
22602264 nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
2261− VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
2265+ VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2266+ VMX_EPT_INVEPT_BIT;
22622267 nested_vmx_ept_caps &= vmx_capability.ept;
22632268 /*
22642269 * Since invept is completely emulated we support both global
@@ -3380,8 +3385,10 @@ static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) · 1115 unmodified lines
33803385 if (enable_ept) {
33813386 eptp = construct_eptp(cr3);
33823387 vmcs_write64(EPT_POINTER, eptp);
3383− guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
3384− vcpu->kvm->arch.ept_identity_map_addr;
3388+ if (is_paging(vcpu) || is_guest_mode(vcpu))
3389+ guest_cr3 = kvm_read_cr3(vcpu);
3390+ else
3391+ guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
33853392 ept_load_pdptrs(vcpu);
33863393 }
33873394
@@ -4879,6 +4886,17 @@ vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) · 1491 unmodified lines
48794886 hypercall[2] = 0xc1;
48804887 }
48814888
4889+static bool nested_cr0_valid(struct vmcs12 *vmcs12, unsigned long val)
4890+{
4891+ unsigned long always_on = VMXON_CR0_ALWAYSON;
4892+
4893+ if (nested_vmx_secondary_ctls_high &
4894+ SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4895+ nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4896+ always_on &= ~(X86_CR0_PE | X86_CR0_PG);
4897+ return (val & always_on) == always_on;
4898+}
4899+
48824900 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
48834901 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
48844902 {
@@ -4897,9 +4915,7 @@ static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) · 12 unmodified lines
48974915 val = (val & ~vmcs12->cr0_guest_host_mask) |
48984916 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
48994917
4900− /* TODO: will have to take unrestricted guest mode into
4901− * account */
4902− if ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)
4918+ if (!nested_cr0_valid(vmcs12, val))
49034919 return 1;
49044920
49054921 if (kvm_set_cr0(vcpu, val))
@@ -6627,6 +6643,9 @@ static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) · 1721 unmodified lines
66276643 return 0;
66286644 else if (is_page_fault(intr_info))
66296645 return enable_ept;
6646+ else if (is_no_device(intr_info) &&
6647+ !(nested_read_cr0(vmcs12) & X86_CR0_TS))
6648+ return 0;
66306649 return vmcs12->exception_bitmap &
66316650 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
66326651 case EXIT_REASON_EXTERNAL_INTERRUPT:
@@ -6722,6 +6741,27 @@ static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) · 89 unmodified lines
67226741 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
67236742 }
67246743
6744+static void nested_adjust_preemption_timer(struct kvm_vcpu *vcpu)
6745+{
6746+ u64 delta_tsc_l1;
6747+ u32 preempt_val_l1, preempt_val_l2, preempt_scale;
6748+
6749+ if (!(get_vmcs12(vcpu)->pin_based_vm_exec_control &
6750+ PIN_BASED_VMX_PREEMPTION_TIMER))
6751+ return;
6752+ preempt_scale = native_read_msr(MSR_IA32_VMX_MISC) &
6753+ MSR_IA32_VMX_MISC_PREEMPTION_TIMER_SCALE;
6754+ preempt_val_l2 = vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
6755+ delta_tsc_l1 = vmx_read_l1_tsc(vcpu, native_read_tsc())
6756+ - vcpu->arch.last_guest_tsc;
6757+ preempt_val_l1 = delta_tsc_l1 >> preempt_scale;
6758+ if (preempt_val_l2 <= preempt_val_l1)
6759+ preempt_val_l2 = 0;
6760+ else
6761+ preempt_val_l2 -= preempt_val_l1;
6762+ vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, preempt_val_l2);
6763+}
6764+
67256765 /*
67266766 * The guest has exited. See if we can fix it or if we need userspace
67276767 * assistance.
@@ -6736,20 +6776,6 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu) · 8 unmodified lines
67366776 if (vmx->emulation_required)
67376777 return handle_invalid_guest_state(vcpu);
67386778
6739− /*
6740− * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
6741− * we did not inject a still-pending event to L1 now because of
6742− * nested_run_pending, we need to re-enable this bit.
6743− */
6744− if (vmx->nested.nested_run_pending)
6745− kvm_make_request(KVM_REQ_EVENT, vcpu);
6746−
6747− if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH ||
6748− exit_reason == EXIT_REASON_VMRESUME))
6749− vmx->nested.nested_run_pending = 1;
6750− else
6751− vmx->nested.nested_run_pending = 0;
6752−
67536779 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
67546780 nested_vmx_vmexit(vcpu);
67556781 return 1;
@@ -7061,9 +7087,9 @@ static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, · 305 unmodified lines
70617087 case INTR_TYPE_HARD_EXCEPTION:
70627088 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
70637089 u32 err = vmcs_read32(error_code_field);
7064− kvm_queue_exception_e(vcpu, vector, err);
7090+ kvm_requeue_exception_e(vcpu, vector, err);
70657091 } else
7066− kvm_queue_exception(vcpu, vector);
7092+ kvm_requeue_exception(vcpu, vector);
70677093 break;
70687094 case INTR_TYPE_SOFT_INTR:
70697095 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
@@ -7146,6 +7172,8 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) · 76 unmodified lines
71467172 atomic_switch_perf_msrs(vmx);
71477173 debugctlmsr = get_debugctlmsr();
71487174
7175+ if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending)
7176+ nested_adjust_preemption_timer(vcpu);
71497177 vmx->__launched = vmx->loaded_vmcs->launched;
71507178 asm(
71517179 /* Store host registers */
@@ -7284,6 +7312,16 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) · 132 unmodified lines
72847312 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
72857313 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
72867314
7315+ /*
7316+ * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
7317+ * we did not inject a still-pending event to L1 now because of
7318+ * nested_run_pending, we need to re-enable this bit.
7319+ */
7320+ if (vmx->nested.nested_run_pending)
7321+ kvm_make_request(KVM_REQ_EVENT, vcpu);
7322+
7323+ vmx->nested.nested_run_pending = 0;
7324+
72877325 vmx_complete_atomic_exit(vmx);
72887326 vmx_recover_nmi_blocking(vmx);
72897327 vmx_complete_interrupts(vmx);
@@ -7410,8 +7448,7 @@ static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) · 120 unmodified lines
74107448 */
74117449 if (is_mmio)
74127450 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
7413− else if (vcpu->kvm->arch.iommu_domain &&
7414− !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
7451+ else if (kvm_arch_has_noncoherent_dma(vcpu->kvm))
74157452 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
74167453 VMX_EPT_MT_EPTE_SHIFT;
74177454 else
@@ -7501,9 +7538,9 @@ static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) · 83 unmodified lines
75017538 return get_vmcs12(vcpu)->ept_pointer;
75027539 }
75037540
7504−static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
7541+static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
75057542 {
7506− int r = kvm_init_shadow_ept_mmu(vcpu, &vcpu->arch.mmu,
7543+ kvm_init_shadow_ept_mmu(vcpu, &vcpu->arch.mmu,
75077544 nested_vmx_ept_caps & VMX_EPT_EXECUTE_ONLY_BIT);
75087545
75097546 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
@@ -7511,8 +7548,6 @@ static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) · 1 unmodified line
75117548 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
75127549
75137550 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
7514−
7515− return r;
75167551 }
75177552
75187553 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
@@ -7520,6 +7555,20 @@ static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) · 1 unmodified line
75207555 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
75217556 }
75227557
7558+static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
7559+ struct x86_exception *fault)
7560+{
7561+ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7562+
7563+ WARN_ON(!is_guest_mode(vcpu));
7564+
7565+ /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
7566+ if (vmcs12->exception_bitmap & (1u << PF_VECTOR))
7567+ nested_vmx_vmexit(vcpu);
7568+ else
7569+ kvm_inject_page_fault(vcpu, fault);
7570+}
7571+
75237572 /*
75247573 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
75257574 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
@@ -7533,6 +7582,7 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) · 7 unmodified lines
75337582 {
75347583 struct vcpu_vmx *vmx = to_vmx(vcpu);
75357584 u32 exec_control;
7585+ u32 exit_control;
75367586
75377587 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
75387588 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
@@ -7706,7 +7756,10 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) · 167 unmodified lines
77067756 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
77077757 * bits are further modified by vmx_set_efer() below.
77087758 */
7709− vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
7759+ exit_control = vmcs_config.vmexit_ctrl;
7760+ if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
7761+ exit_control |= VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
7762+ vmcs_write32(VM_EXIT_CONTROLS, exit_control);
77107763
77117764 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
77127765 * emulated by vmx_set_efer(), below.
@@ -7773,6 +7826,9 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) · 60 unmodified lines
77737826 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
77747827 kvm_mmu_reset_context(vcpu);
77757828
7829+ if (!enable_ept)
7830+ vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
7831+
77767832 /*
77777833 * L1 may access the L2's PDPTR, so save them to construct vmcs12
77787834 */
@@ -7876,7 +7932,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) · 97 unmodified lines
78767932 return 1;
78777933 }
78787934
7879− if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7935+ if (!nested_cr0_valid(vmcs12, vmcs12->guest_cr0) ||
78807936 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
78817937 nested_vmx_entry_failure(vcpu, vmcs12,
78827938 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
@@ -7938,6 +7994,8 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) · 55 unmodified lines
79387994
79397995 enter_guest_mode(vcpu);
79407996
7997+ vmx->nested.nested_run_pending = 1;
7998+
79417999 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
79428000
79438001 cpu = get_cpu();
@@ -8005,7 +8063,7 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, · 61 unmodified lines
80058063 u32 idt_vectoring;
80068064 unsigned int nr;
80078065
8008− if (vcpu->arch.exception.pending) {
8066+ if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
80098067 nr = vcpu->arch.exception.nr;
80108068 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
80118069
@@ -8023,7 +8081,7 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, · 11 unmodified lines
80238081 }
80248082
80258083 vmcs12->idt_vectoring_info_field = idt_vectoring;
8026− } else if (vcpu->arch.nmi_pending) {
8084+ } else if (vcpu->arch.nmi_injected) {
80278085 vmcs12->idt_vectoring_info_field =
80288086 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
80298087 } else if (vcpu->arch.interrupt.pending) {
@@ -8105,6 +8163,11 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) · 75 unmodified lines
81058163 vmcs12->guest_pending_dbg_exceptions =
81068164 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
81078165
8166+ if ((vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER) &&
8167+ (vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER))
8168+ vmcs12->vmx_preemption_timer_value =
8169+ vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
8170+
81088171 /*
81098172 * In some cases (usually, nested EPT), L2 is allowed to change its
81108173 * own CR3 without exiting. If it has changed it, we must keep it.
@@ -8130,6 +8193,8 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) · 19 unmodified lines
81308193 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
81318194 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
81328195 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
8196+ if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
8197+ vmcs12->guest_ia32_efer = vcpu->arch.efer;
81338198 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
81348199 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
81358200 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
@@ -8201,7 +8266,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, · 65 unmodified lines
82018266 * fpu_active (which may have changed).
82028267 * Note that vmx_set_cr0 refers to efer set above.
82038268 */
8204− kvm_set_cr0(vcpu, vmcs12->host_cr0);
8269+ vmx_set_cr0(vcpu, vmcs12->host_cr0);
82058270 /*
82068271 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
82078272 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
@@ -8224,6 +8289,9 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, · 16 unmodified lines
82248289 kvm_set_cr3(vcpu, vmcs12->host_cr3);
82258290 kvm_mmu_reset_context(vcpu);
82268291
8292+ if (!enable_ept)
8293+ vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
8294+
82278295 if (enable_vpid) {
82288296 /*
82298297 * Trivially support vpid by letting L2s share their parent
arch/x86/kvm/x86.c+77−31
@@ -577,6 +577,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu) · 576 unmodified lines
577577 int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
578578 {
579579 u64 xcr0;
580+ u64 valid_bits;
580581
581582 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
582583 if (index != XCR_XFEATURE_ENABLED_MASK)
@@ -586,8 +587,16 @@ int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) · 3 unmodified lines
586587 return 1;
587588 if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
588589 return 1;
589− if (xcr0 & ~host_xcr0)
590+
591+ /*
592+ * Do not allow the guest to set bits that we do not support
593+ * saving. However, xcr0 bit 0 is always set, even if the
594+ * emulated CPU does not support XSAVE (see fx_init).
595+ */
596+ valid_bits = vcpu->arch.guest_supported_xcr0 | XSTATE_FP;
597+ if (xcr0 & ~valid_bits)
590598 return 1;
599+
591600 kvm_put_guest_xcr0(vcpu);
592601 vcpu->arch.xcr0 = xcr0;
593602 return 0;
@@ -684,7 +693,7 @@ int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) · 90 unmodified lines
684693
685694 vcpu->arch.cr3 = cr3;
686695 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
687− vcpu->arch.mmu.new_cr3(vcpu);
696+ kvm_mmu_new_cr3(vcpu);
688697 return 0;
689698 }
690699 EXPORT_SYMBOL_GPL(kvm_set_cr3);
@@ -2564,6 +2573,7 @@ int kvm_dev_ioctl_check_extension(long ext) · 1873 unmodified lines
25642573 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
25652574 case KVM_CAP_SET_TSS_ADDR:
25662575 case KVM_CAP_EXT_CPUID:
2576+ case KVM_CAP_EXT_EMUL_CPUID:
25672577 case KVM_CAP_CLOCKSOURCE:
25682578 case KVM_CAP_PIT:
25692579 case KVM_CAP_NOP_IO_DELAY:
@@ -2673,15 +2683,17 @@ long kvm_arch_dev_ioctl(struct file *filp, · 103 unmodified lines
26732683 r = 0;
26742684 break;
26752685 }
2676− case KVM_GET_SUPPORTED_CPUID: {
2686+ case KVM_GET_SUPPORTED_CPUID:
2687+ case KVM_GET_EMULATED_CPUID: {
26772688 struct kvm_cpuid2 __user *cpuid_arg = argp;
26782689 struct kvm_cpuid2 cpuid;
26792690
26802691 r = -EFAULT;
26812692 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
26822693 goto out;
2683− r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
2684− cpuid_arg->entries);
2694+
2695+ r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
2696+ ioctl);
26852697 if (r)
26862698 goto out;
26872699
@@ -2715,8 +2727,7 @@ static void wbinvd_ipi(void *garbage) · 27 unmodified lines
27152727
27162728 static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
27172729 {
2718− return vcpu->kvm->arch.iommu_domain &&
2719− !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY);
2730+ return kvm_arch_has_noncoherent_dma(vcpu->kvm);
27202731 }
27212732
27222733 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
@@ -2984,11 +2995,13 @@ static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, · 261 unmodified lines
29842995 static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
29852996 struct kvm_xsave *guest_xsave)
29862997 {
2987− if (cpu_has_xsave)
2998+ if (cpu_has_xsave) {
29882999 memcpy(guest_xsave->region,
29893000 &vcpu->arch.guest_fpu.state->xsave,
2990− xstate_size);
2991− else {
3001+ vcpu->arch.guest_xstate_size);
3002+ *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] &=
3003+ vcpu->arch.guest_supported_xcr0 | XSTATE_FPSSE;
3004+ } else {
29923005 memcpy(guest_xsave->region,
29933006 &vcpu->arch.guest_fpu.state->fxsave,
29943007 sizeof(struct i387_fxsave_struct));
@@ -3003,10 +3016,19 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, · 8 unmodified lines
30033016 u64 xstate_bv =
30043017 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
30053018
3006− if (cpu_has_xsave)
3019+ if (cpu_has_xsave) {
3020+ /*
3021+ * Here we allow setting states that are not present in
3022+ * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3023+ * with old userspace.
3024+ */
3025+ if (xstate_bv & ~KVM_SUPPORTED_XCR0)
3026+ return -EINVAL;
3027+ if (xstate_bv & ~host_xcr0)
3028+ return -EINVAL;
30073029 memcpy(&vcpu->arch.guest_fpu.state->xsave,
3008− guest_xsave->region, xstate_size);
3009− else {
3030+ guest_xsave->region, vcpu->arch.guest_xstate_size);
3031+ } else {
30103032 if (xstate_bv & ~XSTATE_FPSSE)
30113033 return -EINVAL;
30123034 memcpy(&vcpu->arch.guest_fpu.state->fxsave,
@@ -3042,9 +3064,9 @@ static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu, · 29 unmodified lines
30423064
30433065 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
30443066 /* Only support XCR0 currently */
3045− if (guest_xcrs->xcrs[0].xcr == XCR_XFEATURE_ENABLED_MASK) {
3067+ if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
30463068 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
3047− guest_xcrs->xcrs[0].value);
3069+ guest_xcrs->xcrs[i].value);
30483070 break;
30493071 }
30503072 if (r)
@@ -4775,8 +4797,8 @@ static void inject_emulated_exception(struct kvm_vcpu *vcpu) · 1724 unmodified lines
47754797
47764798 static void init_decode_cache(struct x86_emulate_ctxt *ctxt)
47774799 {
4778− memset(&ctxt->twobyte, 0,
4779− (void *)&ctxt->_regs - (void *)&ctxt->twobyte);
4800+ memset(&ctxt->opcode_len, 0,
4801+ (void *)&ctxt->_regs - (void *)&ctxt->opcode_len);
47804802
47814803 ctxt->fetch.start = 0;
47824804 ctxt->fetch.end = 0;
@@ -5094,8 +5116,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, · 311 unmodified lines
50945116 ctxt->have_exception = false;
50955117 ctxt->perm_ok = false;
50965118
5097− ctxt->only_vendor_specific_insn
5098− = emulation_type & EMULTYPE_TRAP_UD;
5119+ ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
50995120
51005121 r = x86_decode_insn(ctxt, insn, insn_len);
51015122
@@ -5263,7 +5284,7 @@ static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long va · 161 unmodified lines
52635284
52645285 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
52655286
5266− raw_spin_lock(&kvm_lock);
5287+ spin_lock(&kvm_lock);
52675288 list_for_each_entry(kvm, &vm_list, vm_list) {
52685289 kvm_for_each_vcpu(i, vcpu, kvm) {
52695290 if (vcpu->cpu != freq->cpu)
@@ -5273,7 +5294,7 @@ static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long va · 3 unmodified lines
52735294 send_ipi = 1;
52745295 }
52755296 }
5276− raw_spin_unlock(&kvm_lock);
5297+ spin_unlock(&kvm_lock);
52775298
52785299 if (freq->old < freq->new && send_ipi) {
52795300 /*
@@ -5426,12 +5447,12 @@ static void pvclock_gtod_update_fn(struct work_struct *work) · 146 unmodified lines
54265447 struct kvm_vcpu *vcpu;
54275448 int i;
54285449
5429− raw_spin_lock(&kvm_lock);
5450+ spin_lock(&kvm_lock);
54305451 list_for_each_entry(kvm, &vm_list, vm_list)
54315452 kvm_for_each_vcpu(i, vcpu, kvm)
54325453 set_bit(KVM_REQ_MASTERCLOCK_UPDATE, &vcpu->requests);
54335454 atomic_set(&kvm_guest_has_master_clock, 0);
5434− raw_spin_unlock(&kvm_lock);
5455+ spin_unlock(&kvm_lock);
54355456 }
54365457
54375458 static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
@@ -5945,10 +5966,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · 507 unmodified lines
59455966
59465967 vcpu->mode = IN_GUEST_MODE;
59475968
5969+ srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
5970+
59485971 /* We should set ->mode before check ->requests,
59495972 * see the comment in make_all_cpus_request.
59505973 */
5951− smp_mb();
5974+ smp_mb__after_srcu_read_unlock();
59525975
59535976 local_irq_disable();
59545977
@@ -5958,12 +5981,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · 3 unmodified lines
59585981 smp_wmb();
59595982 local_irq_enable();
59605983 preempt_enable();
5984+ vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
59615985 r = 1;
59625986 goto cancel_injection;
59635987 }
59645988
5965− srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
5966−
59675989 if (req_immediate_exit)
59685990 smp_send_reschedule(vcpu->cpu);
59695991
@@ -6688,7 +6710,7 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) · 718 unmodified lines
66886710 if (r)
66896711 return r;
66906712 kvm_vcpu_reset(vcpu);
6691− r = kvm_mmu_setup(vcpu);
6713+ kvm_mmu_setup(vcpu);
66926714 vcpu_put(vcpu);
66936715
66946716 return r;
@@ -6940,6 +6962,10 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) · 245 unmodified lines
69406962
69416963 vcpu->arch.ia32_tsc_adjust_msr = 0x0;
69426964 vcpu->arch.pv_time_enabled = false;
6965+
6966+ vcpu->arch.guest_supported_xcr0 = 0;
6967+ vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
6968+
69436969 kvm_async_pf_hash_reset(vcpu);
69446970 kvm_pmu_init(vcpu);
69456971
@@ -6981,6 +7007,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · 35 unmodified lines
69817007 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
69827008 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
69837009 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
7010+ atomic_set(&kvm->arch.noncoherent_dma_count, 0);
69847011
69857012 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
69867013 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
@@ -7065,7 +7092,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm) · 78 unmodified lines
70657092 kfree(rcu_dereference_check(kvm->arch.apic_map, 1));
70667093 }
70677094
7068−void kvm_arch_free_memslot(struct kvm_memory_slot *free,
7095+void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
70697096 struct kvm_memory_slot *dont)
70707097 {
70717098 int i;
@@ -7086,7 +7113,8 @@ void kvm_arch_free_memslot(struct kvm_memory_slot *free, · 14 unmodified lines
70867113 }
70877114 }
70887115
7089−int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
7116+int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
7117+ unsigned long npages)
70907118 {
70917119 int i;
70927120
@@ -7283,7 +7311,7 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) · 190 unmodified lines
72837311 int r;
72847312
72857313 if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
7286− is_error_page(work->page))
7314+ work->wakeup_all)
72877315 return;
72887316
72897317 r = kvm_mmu_reload(vcpu);
@@ -7393,7 +7421,7 @@ void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, · 103 unmodified lines
73937421 struct x86_exception fault;
73947422
73957423 trace_kvm_async_pf_ready(work->arch.token, work->gva);
7396− if (is_error_page(work->page))
7424+ if (work->wakeup_all)
73977425 work->arch.token = ~0; /* broadcast wakeup */
73987426 else
73997427 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
@@ -7420,6 +7448,24 @@ bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) · 20 unmodified lines
74207448 kvm_x86_ops->interrupt_allowed(vcpu);
74217449 }
74227450
7451+void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
7452+{
7453+ atomic_inc(&kvm->arch.noncoherent_dma_count);
7454+}
7455+EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
7456+
7457+void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
7458+{
7459+ atomic_dec(&kvm->arch.noncoherent_dma_count);
7460+}
7461+EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
7462+
7463+bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
7464+{
7465+ return atomic_read(&kvm->arch.noncoherent_dma_count);
7466+}
7467+EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
7468+
74237469 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
74247470 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
74257471 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
arch/x86/kvm/x86.h+1−0
@@ -122,6 +122,7 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt, · 121 unmodified lines
122122 gva_t addr, void *val, unsigned int bytes,
123123 struct x86_exception *exception);
124124
125+#define KVM_SUPPORTED_XCR0 (XSTATE_FP | XSTATE_SSE | XSTATE_YMM)
125126 extern u64 host_xcr0;
126127
127128 extern struct static_key kvm_no_apic_vcpu;
include/linux/kvm_host.h+26−16
@@ -142,7 +142,7 @@ struct kvm; · 141 unmodified lines
142142 struct kvm_vcpu;
143143 extern struct kmem_cache *kvm_vcpu_cache;
144144
145−extern raw_spinlock_t kvm_lock;
145+extern spinlock_t kvm_lock;
146146 extern struct list_head vm_list;
147147
148148 struct kvm_io_range {
@@ -189,8 +189,7 @@ struct kvm_async_pf { · 40 unmodified lines
189189 gva_t gva;
190190 unsigned long addr;
191191 struct kvm_arch_async_pf arch;
192− struct page *page;
193− bool done;
192+ bool wakeup_all;
194193 };
195194
196195 void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
@@ -508,9 +507,10 @@ int kvm_set_memory_region(struct kvm *kvm, · 311 unmodified lines
508507 struct kvm_userspace_memory_region *mem);
509508 int __kvm_set_memory_region(struct kvm *kvm,
510509 struct kvm_userspace_memory_region *mem);
511−void kvm_arch_free_memslot(struct kvm_memory_slot *free,
510+void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
512511 struct kvm_memory_slot *dont);
513−int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages);
512+int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
513+ unsigned long npages);
514514 void kvm_arch_memslots_updated(struct kvm *kvm);
515515 int kvm_arch_prepare_memory_region(struct kvm *kvm,
516516 struct kvm_memory_slot *memslot,
@@ -671,6 +671,25 @@ static inline void kvm_arch_free_vm(struct kvm *kvm) · 154 unmodified lines
671671 }
672672 #endif
673673
674+#ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA
675+void kvm_arch_register_noncoherent_dma(struct kvm *kvm);
676+void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm);
677+bool kvm_arch_has_noncoherent_dma(struct kvm *kvm);
678+#else
679+static inline void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
680+{
681+}
682+
683+static inline void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
684+{
685+}
686+
687+static inline bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
688+{
689+ return false;
690+}
691+#endif
692+
674693 static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
675694 {
676695 #ifdef __KVM_HAVE_ARCH_WQP
@@ -747,9 +766,6 @@ void kvm_unregister_irq_ack_notifier(struct kvm *kvm, · 70 unmodified lines
747766 int kvm_request_irq_source_id(struct kvm *kvm);
748767 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
749768
750−/* For vcpu->arch.iommu_flags */
751−#define KVM_IOMMU_CACHE_COHERENCY 0x1
752−
753769 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
754770 int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
755771 void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
@@ -789,7 +805,7 @@ static inline void kvm_guest_enter(void) · 33 unmodified lines
789805
790806 /* KVM does not hold any references to rcu protected data when it
791807 * switches CPU into a guest mode. In fact switching to a guest mode
792− * is very similar to exiting to userspase from rcu point of view. In
808+ * is very similar to exiting to userspace from rcu point of view. In
793809 * addition CPU may stay in a guest mode for quite a long time (up to
794810 * one time slice). Lets treat guest mode as quiescent state, just like
795811 * we do with user-mode execution.
@@ -842,13 +858,6 @@ static inline int memslot_id(struct kvm *kvm, gfn_t gfn) · 46 unmodified lines
842858 return gfn_to_memslot(kvm, gfn)->id;
843859 }
844860
845−static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
846−{
847− /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
848− return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
849− (base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
850−}
851−
852861 static inline gfn_t
853862 hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
854863 {
@@ -1066,6 +1075,7 @@ struct kvm_device *kvm_device_from_filp(struct file *filp); · 211 unmodified lines
10661075
10671076 extern struct kvm_device_ops kvm_mpic_ops;
10681077 extern struct kvm_device_ops kvm_xics_ops;
1078+extern struct kvm_device_ops kvm_vfio_ops;
10691079
10701080 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
10711081
include/linux/sched.h+8−0
@@ -286,6 +286,14 @@ static inline void lockup_detector_init(void) · 285 unmodified lines
286286 }
287287 #endif
288288
289+#ifdef CONFIG_DETECT_HUNG_TASK
290+void reset_hung_task_detector(void);
291+#else
292+static inline void reset_hung_task_detector(void)
293+{
294+}
295+#endif
296+
289297 /* Attach to any functions which should be ignored in wchan output. */
290298 #define __sched __attribute__((__section__(".sched.text")))
291299
include/linux/srcu.h+14−0
@@ -237,4 +237,18 @@ static inline void srcu_read_unlock(struct srcu_struct *sp, int idx) · 236 unmodified lines
237237 __srcu_read_unlock(sp, idx);
238238 }
239239
240+/**
241+ * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock
242+ *
243+ * Converts the preceding srcu_read_unlock into a two-way memory barrier.
244+ *
245+ * Call this after srcu_read_unlock, to guarantee that all memory operations
246+ * that occur after smp_mb__after_srcu_read_unlock will appear to happen after
247+ * the preceding srcu_read_unlock.
248+ */
249+static inline void smp_mb__after_srcu_read_unlock(void)
250+{
251+ /* __srcu_read_unlock has smp_mb() internally so nothing to do here. */
252+}
253+
240254 #endif
include/trace/events/kvm.h+4−6
@@ -296,23 +296,21 @@ DEFINE_EVENT(kvm_async_pf_nopresent_ready, kvm_async_pf_ready, · 295 unmodified lines
296296
297297 TRACE_EVENT(
298298 kvm_async_pf_completed,
299− TP_PROTO(unsigned long address, struct page *page, u64 gva),
300− TP_ARGS(address, page, gva),
299+ TP_PROTO(unsigned long address, u64 gva),
300+ TP_ARGS(address, gva),
301301
302302 TP_STRUCT__entry(
303303 __field(unsigned long, address)
304− __field(pfn_t, pfn)
305304 __field(u64, gva)
306305 ),
307306
308307 TP_fast_assign(
309308 __entry->address = address;
310− __entry->pfn = page ? page_to_pfn(page) : 0;
311309 __entry->gva = gva;
312310 ),
313311
314− TP_printk("gva %#llx address %#lx pfn %#llx", __entry->gva,
315− __entry->address, __entry->pfn)
312+ TP_printk("gva %#llx address %#lx", __entry->gva,
313+ __entry->address)
316314 );
317315
318316 #endif
include/uapi/linux/kvm.h+11−0
@@ -518,6 +518,10 @@ struct kvm_ppc_smmu_info { · 517 unmodified lines
518518 /* machine type bits, to be used as argument to KVM_CREATE_VM */
519519 #define KVM_VM_S390_UCONTROL 1
520520
521+/* on ppc, 0 indicate default, 1 should force HV and 2 PR */
522+#define KVM_VM_PPC_HV 1
523+#define KVM_VM_PPC_PR 2
524+
521525 #define KVM_S390_SIE_PAGE_OFFSET 1
522526
523527 /*
@@ -541,6 +545,7 @@ struct kvm_ppc_smmu_info { · 17 unmodified lines
541545 #define KVM_TRACE_ENABLE __KVM_DEPRECATED_MAIN_W_0x06
542546 #define KVM_TRACE_PAUSE __KVM_DEPRECATED_MAIN_0x07
543547 #define KVM_TRACE_DISABLE __KVM_DEPRECATED_MAIN_0x08
548+#define KVM_GET_EMULATED_CPUID _IOWR(KVMIO, 0x09, struct kvm_cpuid2)
544549
545550 /*
546551 * Extension capability list.
@@ -668,6 +673,7 @@ struct kvm_ppc_smmu_info { · 121 unmodified lines
668673 #define KVM_CAP_IRQ_XICS 92
669674 #define KVM_CAP_ARM_EL1_32BIT 93
670675 #define KVM_CAP_SPAPR_MULTITCE 94
676+#define KVM_CAP_EXT_EMUL_CPUID 95
671677
672678 #ifdef KVM_CAP_IRQ_ROUTING
673679
@@ -843,6 +849,10 @@ struct kvm_device_attr { · 169 unmodified lines
843849 #define KVM_DEV_TYPE_FSL_MPIC_20 1
844850 #define KVM_DEV_TYPE_FSL_MPIC_42 2
845851 #define KVM_DEV_TYPE_XICS 3
852+#define KVM_DEV_TYPE_VFIO 4
853+#define KVM_DEV_VFIO_GROUP 1
854+#define KVM_DEV_VFIO_GROUP_ADD 1
855+#define KVM_DEV_VFIO_GROUP_DEL 2
846856
847857 /*
848858 * ioctls for VM fds
@@ -1012,6 +1022,7 @@ struct kvm_s390_ucas_mapping { · 163 unmodified lines
10121022 /* VM is being stopped by host */
10131023 #define KVM_KVMCLOCK_CTRL _IO(KVMIO, 0xad)
10141024 #define KVM_ARM_VCPU_INIT _IOW(KVMIO, 0xae, struct kvm_vcpu_init)
1025+#define KVM_ARM_PREFERRED_TARGET _IOR(KVMIO, 0xaf, struct kvm_vcpu_init)
10151026 #define KVM_GET_REG_LIST _IOWR(KVMIO, 0xb0, struct kvm_reg_list)
10161027
10171028 #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)
kernel/hung_task.c+11−0
@@ -207,6 +207,14 @@ int proc_dohung_task_timeout_secs(struct ctl_table *table, int write, · 206 unmodified lines
207207 return ret;
208208 }
209209
210+static atomic_t reset_hung_task = ATOMIC_INIT(0);
211+
212+void reset_hung_task_detector(void)
213+{
214+ atomic_set(&reset_hung_task, 1);
215+}
216+EXPORT_SYMBOL_GPL(reset_hung_task_detector);
217+
210218 /*
211219 * kthread which checks for tasks stuck in D state
212220 */
@@ -220,6 +228,9 @@ static int watchdog(void *dummy) · 7 unmodified lines
220228 while (schedule_timeout_interruptible(timeout_jiffies(timeout)))
221229 timeout = sysctl_hung_task_timeout_secs;
222230
231+ if (atomic_xchg(&reset_hung_task, 0))
232+ continue;
233+
223234 check_hung_uninterruptible_tasks(timeout);
224235 }
225236
virt/kvm/Kconfig+3−0
@@ -27,3 +27,6 @@ config HAVE_KVM_MSI · 26 unmodified lines
2727
2828 config HAVE_KVM_CPU_RELAX_INTERCEPT
2929 bool
30+
31+config KVM_VFIO
32+ bool
virt/kvm/async_pf.c+6−16
@@ -56,7 +56,6 @@ void kvm_async_pf_vcpu_init(struct kvm_vcpu *vcpu) · 55 unmodified lines
5656
5757 static void async_pf_execute(struct work_struct *work)
5858 {
59− struct page *page = NULL;
6059 struct kvm_async_pf *apf =
6160 container_of(work, struct kvm_async_pf, work);
6261 struct mm_struct *mm = apf->mm;
@@ -68,14 +67,12 @@ static void async_pf_execute(struct work_struct *work) · 5 unmodified lines
6867
6968 use_mm(mm);
7069 down_read(&mm->mmap_sem);
71− get_user_pages(current, mm, addr, 1, 1, 0, &page, NULL);
70+ get_user_pages(current, mm, addr, 1, 1, 0, NULL, NULL);
7271 up_read(&mm->mmap_sem);
7372 unuse_mm(mm);
7473
7574 spin_lock(&vcpu->async_pf.lock);
7675 list_add_tail(&apf->link, &vcpu->async_pf.done);
77− apf->page = page;
78− apf->done = true;
7976 spin_unlock(&vcpu->async_pf.lock);
8077
8178 /*
@@ -83,7 +80,7 @@ static void async_pf_execute(struct work_struct *work) · 1 unmodified line
8380 * this point
8481 */
8582
86− trace_kvm_async_pf_completed(addr, page, gva);
83+ trace_kvm_async_pf_completed(addr, gva);
8784
8885 if (waitqueue_active(&vcpu->wq))
8986 wake_up_interruptible(&vcpu->wq);
@@ -99,9 +96,8 @@ void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu) · 9 unmodified lines
9996 struct kvm_async_pf *work =
10097 list_entry(vcpu->async_pf.queue.next,
10198 typeof(*work), queue);
102− cancel_work_sync(&work->work);
10399 list_del(&work->queue);
104− if (!work->done) { /* work was canceled */
100+ if (cancel_work_sync(&work->work)) {
105101 mmdrop(work->mm);
106102 kvm_put_kvm(vcpu->kvm); /* == work->vcpu->kvm */
107103 kmem_cache_free(async_pf_cache, work);
@@ -114,8 +110,6 @@ void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu) · 6 unmodified lines
114110 list_entry(vcpu->async_pf.done.next,
115111 typeof(*work), link);
116112 list_del(&work->link);
117− if (!is_error_page(work->page))
118− kvm_release_page_clean(work->page);
119113 kmem_cache_free(async_pf_cache, work);
120114 }
121115 spin_unlock(&vcpu->async_pf.lock);
@@ -135,14 +129,11 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu) · 13 unmodified lines
135129 list_del(&work->link);
136130 spin_unlock(&vcpu->async_pf.lock);
137131
138− if (work->page)
139− kvm_arch_async_page_ready(vcpu, work);
132+ kvm_arch_async_page_ready(vcpu, work);
140133 kvm_arch_async_page_present(vcpu, work);
141134
142135 list_del(&work->queue);
143136 vcpu->async_pf.queued--;
144− if (!is_error_page(work->page))
145− kvm_release_page_clean(work->page);
146137 kmem_cache_free(async_pf_cache, work);
147138 }
148139 }
@@ -165,8 +156,7 @@ int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn, · 16 unmodified lines
165156 if (!work)
166157 return 0;
167158
168− work->page = NULL;
169− work->done = false;
159+ work->wakeup_all = false;
170160 work->vcpu = vcpu;
171161 work->gva = gva;
172162 work->addr = gfn_to_hva(vcpu->kvm, gfn);
@@ -206,7 +196,7 @@ int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu) · 33 unmodified lines
206196 if (!work)
207197 return -ENOMEM;
208198
209− work->page = KVM_ERR_PTR_BAD_PAGE;
199+ work->wakeup_all = true;
210200 INIT_LIST_HEAD(&work->queue); /* for list_del to work */
211201
212202 spin_lock(&vcpu->async_pf.lock);
virt/kvm/iommu.c+20−18
@@ -79,7 +79,7 @@ int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot) · 78 unmodified lines
7979 flags = IOMMU_READ;
8080 if (!(slot->flags & KVM_MEM_READONLY))
8181 flags |= IOMMU_WRITE;
82− if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
82+ if (!kvm->arch.iommu_noncoherent)
8383 flags |= IOMMU_CACHE;
8484
8585
@@ -103,6 +103,10 @@ int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot) · 17 unmodified lines
103103 while ((gfn << PAGE_SHIFT) & (page_size - 1))
104104 page_size >>= 1;
105105
106+ /* Make sure hva is aligned to the page size we want to map */
107+ while (__gfn_to_hva_memslot(slot, gfn) & (page_size - 1))
108+ page_size >>= 1;
109+
106110 /*
107111 * Pin all pages we are about to map in memory. This is
108112 * important because we unmap and unpin in 4kb steps later.
@@ -140,6 +144,9 @@ static int kvm_iommu_map_memslots(struct kvm *kvm) · 31 unmodified lines
140144 struct kvm_memslots *slots;
141145 struct kvm_memory_slot *memslot;
142146
147+ if (kvm->arch.iommu_noncoherent)
148+ kvm_arch_register_noncoherent_dma(kvm);
149+
143150 idx = srcu_read_lock(&kvm->srcu);
144151 slots = kvm_memslots(kvm);
145152
@@ -158,7 +165,8 @@ int kvm_assign_device(struct kvm *kvm, · 12 unmodified lines
158165 {
159166 struct pci_dev *pdev = NULL;
160167 struct iommu_domain *domain = kvm->arch.iommu_domain;
161− int r, last_flags;
168+ int r;
169+ bool noncoherent;
162170
163171 /* check if iommu exists and in use */
164172 if (!domain)
@@ -174,15 +182,13 @@ int kvm_assign_device(struct kvm *kvm, · 9 unmodified lines
174182 return r;
175183 }
176184
177− last_flags = kvm->arch.iommu_flags;
178− if (iommu_domain_has_cap(kvm->arch.iommu_domain,
179− IOMMU_CAP_CACHE_COHERENCY))
180− kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
185+ noncoherent = !iommu_domain_has_cap(kvm->arch.iommu_domain,
186+ IOMMU_CAP_CACHE_COHERENCY);
181187
182188 /* Check if need to update IOMMU page table for guest memory */
183− if ((last_flags ^ kvm->arch.iommu_flags) ==
184− KVM_IOMMU_CACHE_COHERENCY) {
189+ if (noncoherent != kvm->arch.iommu_noncoherent) {
185190 kvm_iommu_unmap_memslots(kvm);
191+ kvm->arch.iommu_noncoherent = noncoherent;
186192 r = kvm_iommu_map_memslots(kvm);
187193 if (r)
188194 goto out_unmap;
@@ -190,11 +196,7 @@ int kvm_assign_device(struct kvm *kvm, · 1 unmodified line
190196
191197 pdev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
192198
193− printk(KERN_DEBUG "assign device %x:%x:%x.%x\n",
194− assigned_dev->host_segnr,
195− assigned_dev->host_busnr,
196− PCI_SLOT(assigned_dev->host_devfn),
197− PCI_FUNC(assigned_dev->host_devfn));
199+ dev_info(&pdev->dev, "kvm assign device\n");
198200
199201 return 0;
200202 out_unmap:
@@ -220,11 +222,7 @@ int kvm_deassign_device(struct kvm *kvm, · 19 unmodified lines
220222
221223 pdev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
222224
223− printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n",
224− assigned_dev->host_segnr,
225− assigned_dev->host_busnr,
226− PCI_SLOT(assigned_dev->host_devfn),
227− PCI_FUNC(assigned_dev->host_devfn));
225+ dev_info(&pdev->dev, "kvm deassign device\n");
228226
229227 return 0;
230228 }
@@ -336,6 +334,9 @@ static int kvm_iommu_unmap_memslots(struct kvm *kvm) · 105 unmodified lines
336334
337335 srcu_read_unlock(&kvm->srcu, idx);
338336
337+ if (kvm->arch.iommu_noncoherent)
338+ kvm_arch_unregister_noncoherent_dma(kvm);
339+
339340 return 0;
340341 }
341342
@@ -350,6 +351,7 @@ int kvm_iommu_unmap_guest(struct kvm *kvm) · 8 unmodified lines
350351 mutex_lock(&kvm->slots_lock);
351352 kvm_iommu_unmap_memslots(kvm);
352353 kvm->arch.iommu_domain = NULL;
354+ kvm->arch.iommu_noncoherent = false;
353355 mutex_unlock(&kvm->slots_lock);
354356
355357 iommu_domain_free(domain);
virt/kvm/kvm_main.c+55−79
@@ -70,7 +70,8 @@ MODULE_LICENSE("GPL"); · 69 unmodified lines
7070 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
7171 */
7272
73−DEFINE_RAW_SPINLOCK(kvm_lock);
73+DEFINE_SPINLOCK(kvm_lock);
74+static DEFINE_RAW_SPINLOCK(kvm_count_lock);
7475 LIST_HEAD(vm_list);
7576
7677 static cpumask_var_t cpus_hardware_enabled;
@@ -186,6 +187,7 @@ void kvm_flush_remote_tlbs(struct kvm *kvm) · 109 unmodified lines
186187 ++kvm->stat.remote_tlb_flush;
187188 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
188189 }
190+EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
189191
190192 void kvm_reload_remote_mmus(struct kvm *kvm)
191193 {
@@ -490,9 +492,9 @@ static struct kvm *kvm_create_vm(unsigned long type) · 298 unmodified lines
490492 if (r)
491493 goto out_err;
492494
493− raw_spin_lock(&kvm_lock);
495+ spin_lock(&kvm_lock);
494496 list_add(&kvm->vm_list, &vm_list);
495− raw_spin_unlock(&kvm_lock);
497+ spin_unlock(&kvm_lock);
496498
497499 return kvm;
498500
@@ -540,13 +542,13 @@ static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot) · 41 unmodified lines
540542 /*
541543 * Free any memory in @free but not in @dont.
542544 */
543−static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
545+static void kvm_free_physmem_slot(struct kvm *kvm, struct kvm_memory_slot *free,
544546 struct kvm_memory_slot *dont)
545547 {
546548 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
547549 kvm_destroy_dirty_bitmap(free);
548550
549− kvm_arch_free_memslot(free, dont);
551+ kvm_arch_free_memslot(kvm, free, dont);
550552
551553 free->npages = 0;
552554 }
@@ -557,7 +559,7 @@ void kvm_free_physmem(struct kvm *kvm) · 4 unmodified lines
557559 struct kvm_memory_slot *memslot;
558560
559561 kvm_for_each_memslot(memslot, slots)
560− kvm_free_physmem_slot(memslot, NULL);
562+ kvm_free_physmem_slot(kvm, memslot, NULL);
561563
562564 kfree(kvm->memslots);
563565 }
@@ -581,9 +583,9 @@ static void kvm_destroy_vm(struct kvm *kvm) · 17 unmodified lines
581583 struct mm_struct *mm = kvm->mm;
582584
583585 kvm_arch_sync_events(kvm);
584− raw_spin_lock(&kvm_lock);
586+ spin_lock(&kvm_lock);
585587 list_del(&kvm->vm_list);
586− raw_spin_unlock(&kvm_lock);
588+ spin_unlock(&kvm_lock);
587589 kvm_free_irq_routing(kvm);
588590 for (i = 0; i < KVM_NR_BUSES; i++)
589591 kvm_io_bus_destroy(kvm->buses[i]);
@@ -821,7 +823,7 @@ int __kvm_set_memory_region(struct kvm *kvm, · 231 unmodified lines
821823 if (change == KVM_MR_CREATE) {
822824 new.userspace_addr = mem->userspace_addr;
823825
824− if (kvm_arch_create_memslot(&new, npages))
826+ if (kvm_arch_create_memslot(kvm, &new, npages))
825827 goto out_free;
826828 }
827829
@@ -872,6 +874,19 @@ int __kvm_set_memory_region(struct kvm *kvm, · 44 unmodified lines
872874 goto out_free;
873875 }
874876
877+ /* actual memory is freed via old in kvm_free_physmem_slot below */
878+ if (change == KVM_MR_DELETE) {
879+ new.dirty_bitmap = NULL;
880+ memset(&new.arch, 0, sizeof(new.arch));
881+ }
882+
883+ old_memslots = install_new_memslots(kvm, slots, &new);
884+
885+ kvm_arch_commit_memory_region(kvm, mem, &old, change);
886+
887+ kvm_free_physmem_slot(kvm, &old, &new);
888+ kfree(old_memslots);
889+
875890 /*
876891 * IOMMU mapping: New slots need to be mapped. Old slots need to be
877892 * un-mapped and re-mapped if their base changes. Since base change
@@ -883,29 +898,15 @@ int __kvm_set_memory_region(struct kvm *kvm, · 5 unmodified lines
883898 */
884899 if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
885900 r = kvm_iommu_map_pages(kvm, &new);
886− if (r)
887− goto out_slots;
888− }
889−
890− /* actual memory is freed via old in kvm_free_physmem_slot below */
891− if (change == KVM_MR_DELETE) {
892− new.dirty_bitmap = NULL;
893− memset(&new.arch, 0, sizeof(new.arch));
901+ return r;
894902 }
895903
896− old_memslots = install_new_memslots(kvm, slots, &new);
897−
898− kvm_arch_commit_memory_region(kvm, mem, &old, change);
899−
900− kvm_free_physmem_slot(&old, &new);
901− kfree(old_memslots);
902−
903904 return 0;
904905
905906 out_slots:
906907 kfree(slots);
907908 out_free:
908− kvm_free_physmem_slot(&new, &old);
909+ kvm_free_physmem_slot(kvm, &new, &old);
909910 out:
910911 return r;
911912 }
@@ -964,6 +965,7 @@ int kvm_get_dirty_log(struct kvm *kvm, · 52 unmodified lines
964965 out:
965966 return r;
966967 }
968+EXPORT_SYMBOL_GPL(kvm_get_dirty_log);
967969
968970 bool kvm_largepages_enabled(void)
969971 {
@@ -1654,6 +1656,7 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn) · 684 unmodified lines
16541656 memslot = gfn_to_memslot(kvm, gfn);
16551657 mark_page_dirty_in_slot(kvm, memslot, gfn);
16561658 }
1659+EXPORT_SYMBOL_GPL(mark_page_dirty);
16571660
16581661 /*
16591662 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
@@ -1679,6 +1682,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) · 19 unmodified lines
16791682
16801683 finish_wait(&vcpu->wq, &wait);
16811684 }
1685+EXPORT_SYMBOL_GPL(kvm_vcpu_block);
16821686
16831687 #ifndef CONFIG_S390
16841688 /*
@@ -2270,6 +2274,11 @@ static int kvm_ioctl_create_device(struct kvm *kvm, · 585 unmodified lines
22702274 case KVM_DEV_TYPE_XICS:
22712275 ops = &kvm_xics_ops;
22722276 break;
2277+#endif
2278+#ifdef CONFIG_KVM_VFIO
2279+ case KVM_DEV_TYPE_VFIO:
2280+ ops = &kvm_vfio_ops;
2281+ break;
22732282 #endif
22742283 default:
22752284 return -ENODEV;
@@ -2519,44 +2528,12 @@ out: · 243 unmodified lines
25192528 }
25202529 #endif
25212530
2522−static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2523−{
2524− struct page *page[1];
2525− unsigned long addr;
2526− int npages;
2527− gfn_t gfn = vmf->pgoff;
2528− struct kvm *kvm = vma->vm_file->private_data;
2529−
2530− addr = gfn_to_hva(kvm, gfn);
2531− if (kvm_is_error_hva(addr))
2532− return VM_FAULT_SIGBUS;
2533−
2534− npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2535− NULL);
2536− if (unlikely(npages != 1))
2537− return VM_FAULT_SIGBUS;
2538−
2539− vmf->page = page[0];
2540− return 0;
2541−}
2542−
2543−static const struct vm_operations_struct kvm_vm_vm_ops = {
2544− .fault = kvm_vm_fault,
2545−};
2546−
2547−static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2548−{
2549− vma->vm_ops = &kvm_vm_vm_ops;
2550− return 0;
2551−}
2552−
25532531 static struct file_operations kvm_vm_fops = {
25542532 .release = kvm_vm_release,
25552533 .unlocked_ioctl = kvm_vm_ioctl,
25562534 #ifdef CONFIG_COMPAT
25572535 .compat_ioctl = kvm_vm_compat_ioctl,
25582536 #endif
2559− .mmap = kvm_vm_mmap,
25602537 .llseek = noop_llseek,
25612538 };
25622539
@@ -2683,11 +2660,12 @@ static void hardware_enable_nolock(void *junk) · 120 unmodified lines
26832660 }
26842661 }
26852662
2686−static void hardware_enable(void *junk)
2663+static void hardware_enable(void)
26872664 {
2688− raw_spin_lock(&kvm_lock);
2689− hardware_enable_nolock(junk);
2690− raw_spin_unlock(&kvm_lock);
2665+ raw_spin_lock(&kvm_count_lock);
2666+ if (kvm_usage_count)
2667+ hardware_enable_nolock(NULL);
2668+ raw_spin_unlock(&kvm_count_lock);
26912669 }
26922670
26932671 static void hardware_disable_nolock(void *junk)
@@ -2700,11 +2678,12 @@ static void hardware_disable_nolock(void *junk) · 6 unmodified lines
27002678 kvm_arch_hardware_disable(NULL);
27012679 }
27022680
2703−static void hardware_disable(void *junk)
2681+static void hardware_disable(void)
27042682 {
2705− raw_spin_lock(&kvm_lock);
2706− hardware_disable_nolock(junk);
2707− raw_spin_unlock(&kvm_lock);
2683+ raw_spin_lock(&kvm_count_lock);
2684+ if (kvm_usage_count)
2685+ hardware_disable_nolock(NULL);
2686+ raw_spin_unlock(&kvm_count_lock);
27082687 }
27092688
27102689 static void hardware_disable_all_nolock(void)
@@ -2718,16 +2697,16 @@ static void hardware_disable_all_nolock(void) · 7 unmodified lines
27182697
27192698 static void hardware_disable_all(void)
27202699 {
2721− raw_spin_lock(&kvm_lock);
2700+ raw_spin_lock(&kvm_count_lock);
27222701 hardware_disable_all_nolock();
2723− raw_spin_unlock(&kvm_lock);
2702+ raw_spin_unlock(&kvm_count_lock);
27242703 }
27252704
27262705 static int hardware_enable_all(void)
27272706 {
27282707 int r = 0;
27292708
2730− raw_spin_lock(&kvm_lock);
2709+ raw_spin_lock(&kvm_count_lock);
27312710
27322711 kvm_usage_count++;
27332712 if (kvm_usage_count == 1) {
@@ -2740,7 +2719,7 @@ static int hardware_enable_all(void) · 6 unmodified lines
27402719 }
27412720 }
27422721
2743− raw_spin_unlock(&kvm_lock);
2722+ raw_spin_unlock(&kvm_count_lock);
27442723
27452724 return r;
27462725 }
@@ -2750,20 +2729,17 @@ static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, · 3 unmodified lines
27502729 {
27512730 int cpu = (long)v;
27522731
2753− if (!kvm_usage_count)
2754− return NOTIFY_OK;
2755−
27562732 val &= ~CPU_TASKS_FROZEN;
27572733 switch (val) {
27582734 case CPU_DYING:
27592735 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
27602736 cpu);
2761− hardware_disable(NULL);
2737+ hardware_disable();
27622738 break;
27632739 case CPU_STARTING:
27642740 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
27652741 cpu);
2766− hardware_enable(NULL);
2742+ hardware_enable();
27672743 break;
27682744 }
27692745 return NOTIFY_OK;
@@ -3056,10 +3032,10 @@ static int vm_stat_get(void *_offset, u64 *val) · 286 unmodified lines
30563032 struct kvm *kvm;
30573033
30583034 *val = 0;
3059− raw_spin_lock(&kvm_lock);
3035+ spin_lock(&kvm_lock);
30603036 list_for_each_entry(kvm, &vm_list, vm_list)
30613037 *val += *(u32 *)((void *)kvm + offset);
3062− raw_spin_unlock(&kvm_lock);
3038+ spin_unlock(&kvm_lock);
30633039 return 0;
30643040 }
30653041
@@ -3073,12 +3049,12 @@ static int vcpu_stat_get(void *_offset, u64 *val) · 7 unmodified lines
30733049 int i;
30743050
30753051 *val = 0;
3076− raw_spin_lock(&kvm_lock);
3052+ spin_lock(&kvm_lock);
30773053 list_for_each_entry(kvm, &vm_list, vm_list)
30783054 kvm_for_each_vcpu(i, vcpu, kvm)
30793055 *val += *(u32 *)((void *)vcpu + offset);
30803056
3081− raw_spin_unlock(&kvm_lock);
3057+ spin_unlock(&kvm_lock);
30823058 return 0;
30833059 }
30843060
@@ -3133,7 +3109,7 @@ static int kvm_suspend(void) · 48 unmodified lines
31333109 static void kvm_resume(void)
31343110 {
31353111 if (kvm_usage_count) {
3136− WARN_ON(raw_spin_is_locked(&kvm_lock));
3112+ WARN_ON(raw_spin_is_locked(&kvm_count_lock));
31373113 hardware_enable_nolock(NULL);
31383114 }
31393115 }
virt/kvm/vfio.c+264−0
@@ -0,0 +1,264 @@
1+/*
2+ * VFIO-KVM bridge pseudo device
3+ *
4+ * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
5+ * Author: Alex Williamson <alex.williamson@redhat.com>
6+ *
7+ * This program is free software; you can redistribute it and/or modify
8+ * it under the terms of the GNU General Public License version 2 as
9+ * published by the Free Software Foundation.
10+ */
11+
12+#include <linux/errno.h>
13+#include <linux/file.h>
14+#include <linux/kvm_host.h>
15+#include <linux/list.h>
16+#include <linux/module.h>
17+#include <linux/mutex.h>
18+#include <linux/slab.h>
19+#include <linux/uaccess.h>
20+#include <linux/vfio.h>
21+
22+struct kvm_vfio_group {
23+ struct list_head node;
24+ struct vfio_group *vfio_group;
25+};
26+
27+struct kvm_vfio {
28+ struct list_head group_list;
29+ struct mutex lock;
30+ bool noncoherent;
31+};
32+
33+static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
34+{
35+ struct vfio_group *vfio_group;
36+ struct vfio_group *(*fn)(struct file *);
37+
38+ fn = symbol_get(vfio_group_get_external_user);
39+ if (!fn)
40+ return ERR_PTR(-EINVAL);
41+
42+ vfio_group = fn(filep);
43+
44+ symbol_put(vfio_group_get_external_user);
45+
46+ return vfio_group;
47+}
48+
49+static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
50+{
51+ void (*fn)(struct vfio_group *);
52+
53+ fn = symbol_get(vfio_group_put_external_user);
54+ if (!fn)
55+ return;
56+
57+ fn(vfio_group);
58+
59+ symbol_put(vfio_group_put_external_user);
60+}
61+
62+/*
63+ * Groups can use the same or different IOMMU domains. If the same then
64+ * adding a new group may change the coherency of groups we've previously
65+ * been told about. We don't want to care about any of that so we retest
66+ * each group and bail as soon as we find one that's noncoherent. This
67+ * means we only ever [un]register_noncoherent_dma once for the whole device.
68+ */
69+static void kvm_vfio_update_coherency(struct kvm_device *dev)
70+{
71+ struct kvm_vfio *kv = dev->private;
72+ bool noncoherent = false;
73+ struct kvm_vfio_group *kvg;
74+
75+ mutex_lock(&kv->lock);
76+
77+ list_for_each_entry(kvg, &kv->group_list, node) {
78+ /*
79+ * TODO: We need an interface to check the coherency of
80+ * the IOMMU domain this group is using. For now, assume
81+ * it's always noncoherent.
82+ */
83+ noncoherent = true;
84+ break;
85+ }
86+
87+ if (noncoherent != kv->noncoherent) {
88+ kv->noncoherent = noncoherent;
89+
90+ if (kv->noncoherent)
91+ kvm_arch_register_noncoherent_dma(dev->kvm);
92+ else
93+ kvm_arch_unregister_noncoherent_dma(dev->kvm);
94+ }
95+
96+ mutex_unlock(&kv->lock);
97+}
98+
99+static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)
100+{
101+ struct kvm_vfio *kv = dev->private;
102+ struct vfio_group *vfio_group;
103+ struct kvm_vfio_group *kvg;
104+ void __user *argp = (void __user *)arg;
105+ struct fd f;
106+ int32_t fd;
107+ int ret;
108+
109+ switch (attr) {
110+ case KVM_DEV_VFIO_GROUP_ADD:
111+ if (get_user(fd, (int32_t __user *)argp))
112+ return -EFAULT;
113+
114+ f = fdget(fd);
115+ if (!f.file)
116+ return -EBADF;
117+
118+ vfio_group = kvm_vfio_group_get_external_user(f.file);
119+ fdput(f);
120+
121+ if (IS_ERR(vfio_group))
122+ return PTR_ERR(vfio_group);
123+
124+ mutex_lock(&kv->lock);
125+
126+ list_for_each_entry(kvg, &kv->group_list, node) {
127+ if (kvg->vfio_group == vfio_group) {
128+ mutex_unlock(&kv->lock);
129+ kvm_vfio_group_put_external_user(vfio_group);
130+ return -EEXIST;
131+ }
132+ }
133+
134+ kvg = kzalloc(sizeof(*kvg), GFP_KERNEL);
135+ if (!kvg) {
136+ mutex_unlock(&kv->lock);
137+ kvm_vfio_group_put_external_user(vfio_group);
138+ return -ENOMEM;
139+ }
140+
141+ list_add_tail(&kvg->node, &kv->group_list);
142+ kvg->vfio_group = vfio_group;
143+
144+ mutex_unlock(&kv->lock);
145+
146+ kvm_vfio_update_coherency(dev);
147+
148+ return 0;
149+
150+ case KVM_DEV_VFIO_GROUP_DEL:
151+ if (get_user(fd, (int32_t __user *)argp))
152+ return -EFAULT;
153+
154+ f = fdget(fd);
155+ if (!f.file)
156+ return -EBADF;
157+
158+ vfio_group = kvm_vfio_group_get_external_user(f.file);
159+ fdput(f);
160+
161+ if (IS_ERR(vfio_group))
162+ return PTR_ERR(vfio_group);
163+
164+ ret = -ENOENT;
165+
166+ mutex_lock(&kv->lock);
167+
168+ list_for_each_entry(kvg, &kv->group_list, node) {
169+ if (kvg->vfio_group != vfio_group)
170+ continue;
171+
172+ list_del(&kvg->node);
173+ kvm_vfio_group_put_external_user(kvg->vfio_group);
174+ kfree(kvg);
175+ ret = 0;
176+ break;
177+ }
178+
179+ mutex_unlock(&kv->lock);
180+
181+ kvm_vfio_group_put_external_user(vfio_group);
182+
183+ kvm_vfio_update_coherency(dev);
184+
185+ return ret;
186+ }
187+
188+ return -ENXIO;
189+}
190+
191+static int kvm_vfio_set_attr(struct kvm_device *dev,
192+ struct kvm_device_attr *attr)
193+{
194+ switch (attr->group) {
195+ case KVM_DEV_VFIO_GROUP:
196+ return kvm_vfio_set_group(dev, attr->attr, attr->addr);
197+ }
198+
199+ return -ENXIO;
200+}
201+
202+static int kvm_vfio_has_attr(struct kvm_device *dev,
203+ struct kvm_device_attr *attr)
204+{
205+ switch (attr->group) {
206+ case KVM_DEV_VFIO_GROUP:
207+ switch (attr->attr) {
208+ case KVM_DEV_VFIO_GROUP_ADD:
209+ case KVM_DEV_VFIO_GROUP_DEL:
210+ return 0;
211+ }
212+
213+ break;
214+ }
215+
216+ return -ENXIO;
217+}
218+
219+static void kvm_vfio_destroy(struct kvm_device *dev)
220+{
221+ struct kvm_vfio *kv = dev->private;
222+ struct kvm_vfio_group *kvg, *tmp;
223+
224+ list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
225+ kvm_vfio_group_put_external_user(kvg->vfio_group);
226+ list_del(&kvg->node);
227+ kfree(kvg);
228+ }
229+
230+ kvm_vfio_update_coherency(dev);
231+
232+ kfree(kv);
233+ kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */
234+}
235+
236+static int kvm_vfio_create(struct kvm_device *dev, u32 type)
237+{
238+ struct kvm_device *tmp;
239+ struct kvm_vfio *kv;
240+
241+ /* Only one VFIO "device" per VM */
242+ list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
243+ if (tmp->ops == &kvm_vfio_ops)
244+ return -EBUSY;
245+
246+ kv = kzalloc(sizeof(*kv), GFP_KERNEL);
247+ if (!kv)
248+ return -ENOMEM;
249+
250+ INIT_LIST_HEAD(&kv->group_list);
251+ mutex_init(&kv->lock);
252+
253+ dev->private = kv;
254+
255+ return 0;
256+}
257+
258+struct kvm_device_ops kvm_vfio_ops = {
259+ .name = "kvm-vfio",
260+ .create = kvm_vfio_create,
261+ .destroy = kvm_vfio_destroy,
262+ .set_attr = kvm_vfio_set_attr,
263+ .has_attr = kvm_vfio_has_attr,
264+};

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