Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License, version 2, as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * You should have received a copy of the GNU General Public License |
| 12 | * along with this program; if not, write to the Free Software |
| 13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 14 | * |
| 15 | * Copyright IBM Corp. 2007 |
| 16 | * |
| 17 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> |
| 18 | * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> |
| 19 | */ |
| 20 | |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/kvm_host.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 25 | #include <linux/hrtimer.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 26 | #include <linux/sched/signal.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 27 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 29 | #include <linux/file.h> |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 30 | #include <linux/module.h> |
Suresh Warrier | 9576730 | 2016-08-19 15:35:47 +1000 | [diff] [blame] | 31 | #include <linux/irqbypass.h> |
| 32 | #include <linux/kvm_irqfd.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 33 | #include <asm/cputable.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 34 | #include <linux/uaccess.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 35 | #include <asm/kvm_ppc.h> |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 36 | #include <asm/cputhreads.h> |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame] | 37 | #include <asm/irqflags.h> |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 38 | #include <asm/iommu.h> |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 39 | #include <asm/switch_to.h> |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 40 | #include <asm/xive.h> |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 41 | #ifdef CONFIG_PPC_PSERIES |
| 42 | #include <asm/hvcall.h> |
| 43 | #include <asm/plpar_wrappers.h> |
| 44 | #endif |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 45 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 46 | #include "timing.h" |
Alexander Graf | 5efdb4b | 2013-04-17 00:37:57 +0200 | [diff] [blame] | 47 | #include "irq.h" |
Paul Mackerras | fad7b9b | 2008-12-23 14:57:26 +1100 | [diff] [blame] | 48 | #include "../mm/mmu_decl.h" |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 49 | |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 50 | #define CREATE_TRACE_POINTS |
| 51 | #include "trace.h" |
| 52 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 53 | struct kvmppc_ops *kvmppc_hv_ops; |
| 54 | EXPORT_SYMBOL_GPL(kvmppc_hv_ops); |
| 55 | struct kvmppc_ops *kvmppc_pr_ops; |
| 56 | EXPORT_SYMBOL_GPL(kvmppc_pr_ops); |
| 57 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 58 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 59 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
| 60 | { |
Radim Krčmář | 2fa6e1e | 2017-06-04 14:43:52 +0200 | [diff] [blame] | 61 | return !!(v->arch.pending_exceptions) || kvm_request_pending(v); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 64 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
| 65 | { |
| 66 | return false; |
| 67 | } |
| 68 | |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 69 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 70 | { |
| 71 | return 1; |
| 72 | } |
| 73 | |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 74 | /* |
| 75 | * Common checks before entering the guest world. Call with interrupts |
| 76 | * disabled. |
| 77 | * |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 78 | * returns: |
| 79 | * |
| 80 | * == 1 if we're ready to go into guest state |
| 81 | * <= 0 if we need to go back to the host with return value |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 82 | */ |
| 83 | int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu) |
| 84 | { |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 85 | int r; |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 86 | |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 87 | WARN_ON(irqs_disabled()); |
| 88 | hard_irq_disable(); |
| 89 | |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 90 | while (true) { |
| 91 | if (need_resched()) { |
| 92 | local_irq_enable(); |
| 93 | cond_resched(); |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 94 | hard_irq_disable(); |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 95 | continue; |
| 96 | } |
| 97 | |
| 98 | if (signal_pending(current)) { |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 99 | kvmppc_account_exit(vcpu, SIGNAL_EXITS); |
| 100 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
| 101 | r = -EINTR; |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 102 | break; |
| 103 | } |
| 104 | |
Scott Wood | 5bd1cf1 | 2012-08-22 15:03:50 +0000 | [diff] [blame] | 105 | vcpu->mode = IN_GUEST_MODE; |
| 106 | |
| 107 | /* |
| 108 | * Reading vcpu->requests must happen after setting vcpu->mode, |
| 109 | * so we don't miss a request because the requester sees |
| 110 | * OUTSIDE_GUEST_MODE and assumes we'll be checking requests |
| 111 | * before next entering the guest (and thus doesn't IPI). |
Lan Tianyu | 489153c | 2016-03-13 11:10:30 +0800 | [diff] [blame] | 112 | * This also orders the write to mode from any reads |
| 113 | * to the page tables done while the VCPU is running. |
| 114 | * Please see the comment in kvm_flush_remote_tlbs. |
Scott Wood | 5bd1cf1 | 2012-08-22 15:03:50 +0000 | [diff] [blame] | 115 | */ |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 116 | smp_mb(); |
Scott Wood | 5bd1cf1 | 2012-08-22 15:03:50 +0000 | [diff] [blame] | 117 | |
Radim Krčmář | 2fa6e1e | 2017-06-04 14:43:52 +0200 | [diff] [blame] | 118 | if (kvm_request_pending(vcpu)) { |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 119 | /* Make sure we process requests preemptable */ |
| 120 | local_irq_enable(); |
| 121 | trace_kvm_check_requests(vcpu); |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 122 | r = kvmppc_core_check_requests(vcpu); |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 123 | hard_irq_disable(); |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 124 | if (r > 0) |
| 125 | continue; |
| 126 | break; |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (kvmppc_core_prepare_to_enter(vcpu)) { |
| 130 | /* interrupts got enabled in between, so we |
| 131 | are back at square 1 */ |
| 132 | continue; |
| 133 | } |
| 134 | |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 135 | guest_enter_irqoff(); |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 136 | return 1; |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 139 | /* return to host */ |
| 140 | local_irq_enable(); |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 141 | return r; |
| 142 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 143 | EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter); |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 144 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 145 | #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) |
| 146 | static void kvmppc_swab_shared(struct kvm_vcpu *vcpu) |
| 147 | { |
| 148 | struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared; |
| 149 | int i; |
| 150 | |
| 151 | shared->sprg0 = swab64(shared->sprg0); |
| 152 | shared->sprg1 = swab64(shared->sprg1); |
| 153 | shared->sprg2 = swab64(shared->sprg2); |
| 154 | shared->sprg3 = swab64(shared->sprg3); |
| 155 | shared->srr0 = swab64(shared->srr0); |
| 156 | shared->srr1 = swab64(shared->srr1); |
| 157 | shared->dar = swab64(shared->dar); |
| 158 | shared->msr = swab64(shared->msr); |
| 159 | shared->dsisr = swab32(shared->dsisr); |
| 160 | shared->int_pending = swab32(shared->int_pending); |
| 161 | for (i = 0; i < ARRAY_SIZE(shared->sr); i++) |
| 162 | shared->sr[i] = swab32(shared->sr[i]); |
| 163 | } |
| 164 | #endif |
| 165 | |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 166 | int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) |
| 167 | { |
| 168 | int nr = kvmppc_get_gpr(vcpu, 11); |
| 169 | int r; |
| 170 | unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3); |
| 171 | unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4); |
| 172 | unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5); |
| 173 | unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6); |
| 174 | unsigned long r2 = 0; |
| 175 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 176 | if (!(kvmppc_get_msr(vcpu) & MSR_SF)) { |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 177 | /* 32 bit mode */ |
| 178 | param1 &= 0xffffffff; |
| 179 | param2 &= 0xffffffff; |
| 180 | param3 &= 0xffffffff; |
| 181 | param4 &= 0xffffffff; |
| 182 | } |
| 183 | |
| 184 | switch (nr) { |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 185 | case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE): |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 186 | { |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 187 | #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) |
| 188 | /* Book3S can be little endian, find it out here */ |
| 189 | int shared_big_endian = true; |
| 190 | if (vcpu->arch.intr_msr & MSR_LE) |
| 191 | shared_big_endian = false; |
| 192 | if (shared_big_endian != vcpu->arch.shared_big_endian) |
| 193 | kvmppc_swab_shared(vcpu); |
| 194 | vcpu->arch.shared_big_endian = shared_big_endian; |
| 195 | #endif |
| 196 | |
Alexander Graf | f3383cf | 2014-05-12 01:08:32 +0200 | [diff] [blame] | 197 | if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) { |
| 198 | /* |
| 199 | * Older versions of the Linux magic page code had |
| 200 | * a bug where they would map their trampoline code |
| 201 | * NX. If that's the case, remove !PR NX capability. |
| 202 | */ |
| 203 | vcpu->arch.disable_kernel_nx = true; |
| 204 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 205 | } |
| 206 | |
| 207 | vcpu->arch.magic_page_pa = param1 & ~0xfffULL; |
| 208 | vcpu->arch.magic_page_ea = param2 & ~0xfffULL; |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 209 | |
Alexander Graf | 89b68c9 | 2014-07-13 16:37:12 +0200 | [diff] [blame] | 210 | #ifdef CONFIG_PPC_64K_PAGES |
| 211 | /* |
| 212 | * Make sure our 4k magic page is in the same window of a 64k |
| 213 | * page within the guest and within the host's page. |
| 214 | */ |
| 215 | if ((vcpu->arch.magic_page_pa & 0xf000) != |
| 216 | ((ulong)vcpu->arch.shared & 0xf000)) { |
| 217 | void *old_shared = vcpu->arch.shared; |
| 218 | ulong shared = (ulong)vcpu->arch.shared; |
| 219 | void *new_shared; |
| 220 | |
| 221 | shared &= PAGE_MASK; |
| 222 | shared |= vcpu->arch.magic_page_pa & 0xf000; |
| 223 | new_shared = (void*)shared; |
| 224 | memcpy(new_shared, old_shared, 0x1000); |
| 225 | vcpu->arch.shared = new_shared; |
| 226 | } |
| 227 | #endif |
| 228 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 229 | r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7; |
Alexander Graf | 7508e16 | 2010-08-03 11:32:56 +0200 | [diff] [blame] | 230 | |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 231 | r = EV_SUCCESS; |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 232 | break; |
| 233 | } |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 234 | case KVM_HCALL_TOKEN(KVM_HC_FEATURES): |
| 235 | r = EV_SUCCESS; |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 236 | #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2) |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 237 | r2 |= (1 << KVM_FEATURE_MAGIC_PAGE); |
| 238 | #endif |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 239 | |
| 240 | /* Second return value is in r4 */ |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 241 | break; |
Liu Yu-B13201 | 9202e07 | 2012-07-03 05:48:52 +0000 | [diff] [blame] | 242 | case EV_HCALL_TOKEN(EV_IDLE): |
| 243 | r = EV_SUCCESS; |
| 244 | kvm_vcpu_block(vcpu); |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 245 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
Liu Yu-B13201 | 9202e07 | 2012-07-03 05:48:52 +0000 | [diff] [blame] | 246 | break; |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 247 | default: |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 248 | r = EV_UNIMPLEMENTED; |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | |
Alexander Graf | 7508e16 | 2010-08-03 11:32:56 +0200 | [diff] [blame] | 252 | kvmppc_set_gpr(vcpu, 4, r2); |
| 253 | |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 254 | return r; |
| 255 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 256 | EXPORT_SYMBOL_GPL(kvmppc_kvm_pv); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 257 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 258 | int kvmppc_sanity_check(struct kvm_vcpu *vcpu) |
| 259 | { |
| 260 | int r = false; |
| 261 | |
| 262 | /* We have to know what CPU to virtualize */ |
| 263 | if (!vcpu->arch.pvr) |
| 264 | goto out; |
| 265 | |
| 266 | /* PAPR only works with book3s_64 */ |
| 267 | if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled) |
| 268 | goto out; |
| 269 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 270 | /* HV KVM can only do PAPR mode for now */ |
Aneesh Kumar K.V | a78b55d | 2013-10-07 22:18:02 +0530 | [diff] [blame] | 271 | if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm)) |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 272 | goto out; |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 273 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 274 | #ifdef CONFIG_KVM_BOOKE_HV |
| 275 | if (!cpu_has_feature(CPU_FTR_EMB_HV)) |
| 276 | goto out; |
| 277 | #endif |
| 278 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 279 | r = true; |
| 280 | |
| 281 | out: |
| 282 | vcpu->arch.sane = r; |
| 283 | return r ? 0 : -EINVAL; |
| 284 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 285 | EXPORT_SYMBOL_GPL(kvmppc_sanity_check); |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 286 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 287 | int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 288 | { |
| 289 | enum emulation_result er; |
| 290 | int r; |
| 291 | |
Alexander Graf | d69614a | 2014-06-18 14:53:49 +0200 | [diff] [blame] | 292 | er = kvmppc_emulate_loadstore(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 293 | switch (er) { |
| 294 | case EMULATE_DONE: |
| 295 | /* Future optimization: only reload non-volatiles if they were |
| 296 | * actually modified. */ |
| 297 | r = RESUME_GUEST_NV; |
| 298 | break; |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 299 | case EMULATE_AGAIN: |
| 300 | r = RESUME_GUEST; |
| 301 | break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 302 | case EMULATE_DO_MMIO: |
| 303 | run->exit_reason = KVM_EXIT_MMIO; |
| 304 | /* We must reload nonvolatiles because "update" load/store |
| 305 | * instructions modify register state. */ |
| 306 | /* Future optimization: only reload non-volatiles if they were |
| 307 | * actually modified. */ |
| 308 | r = RESUME_HOST_NV; |
| 309 | break; |
| 310 | case EMULATE_FAIL: |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 311 | { |
| 312 | u32 last_inst; |
| 313 | |
Alexander Graf | 8d0eff6 | 2014-09-10 14:37:29 +0200 | [diff] [blame] | 314 | kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 315 | /* XXX Deliver Program interrupt to guest. */ |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 316 | pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 317 | r = RESUME_HOST; |
| 318 | break; |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 319 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 320 | default: |
Alexander Graf | 5a33169 | 2012-12-14 23:46:03 +0100 | [diff] [blame] | 321 | WARN_ON(1); |
| 322 | r = RESUME_GUEST; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return r; |
| 326 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 327 | EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 328 | |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 329 | int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, |
| 330 | bool data) |
| 331 | { |
Alexander Graf | c12fb43 | 2014-06-20 14:43:36 +0200 | [diff] [blame] | 332 | ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 333 | struct kvmppc_pte pte; |
Suraj Jitindar Singh | cc6929c | 2018-12-14 16:29:07 +1100 | [diff] [blame] | 334 | int r = -EINVAL; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 335 | |
| 336 | vcpu->stat.st++; |
| 337 | |
Suraj Jitindar Singh | cc6929c | 2018-12-14 16:29:07 +1100 | [diff] [blame] | 338 | if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->store_to_eaddr) |
| 339 | r = vcpu->kvm->arch.kvm_ops->store_to_eaddr(vcpu, eaddr, ptr, |
| 340 | size); |
| 341 | |
| 342 | if ((!r) || (r == -EAGAIN)) |
| 343 | return r; |
| 344 | |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 345 | r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST, |
| 346 | XLATE_WRITE, &pte); |
| 347 | if (r < 0) |
| 348 | return r; |
| 349 | |
| 350 | *eaddr = pte.raddr; |
| 351 | |
| 352 | if (!pte.may_write) |
| 353 | return -EPERM; |
| 354 | |
Alexander Graf | c12fb43 | 2014-06-20 14:43:36 +0200 | [diff] [blame] | 355 | /* Magic page override */ |
| 356 | if (kvmppc_supports_magic_page(vcpu) && mp_pa && |
| 357 | ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) && |
| 358 | !(kvmppc_get_msr(vcpu) & MSR_PR)) { |
| 359 | void *magic = vcpu->arch.shared; |
| 360 | magic += pte.eaddr & 0xfff; |
| 361 | memcpy(magic, ptr, size); |
| 362 | return EMULATE_DONE; |
| 363 | } |
| 364 | |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 365 | if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size)) |
| 366 | return EMULATE_DO_MMIO; |
| 367 | |
| 368 | return EMULATE_DONE; |
| 369 | } |
| 370 | EXPORT_SYMBOL_GPL(kvmppc_st); |
| 371 | |
| 372 | int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, |
| 373 | bool data) |
| 374 | { |
Alexander Graf | c12fb43 | 2014-06-20 14:43:36 +0200 | [diff] [blame] | 375 | ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 376 | struct kvmppc_pte pte; |
Suraj Jitindar Singh | cc6929c | 2018-12-14 16:29:07 +1100 | [diff] [blame] | 377 | int rc = -EINVAL; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 378 | |
| 379 | vcpu->stat.ld++; |
| 380 | |
Suraj Jitindar Singh | cc6929c | 2018-12-14 16:29:07 +1100 | [diff] [blame] | 381 | if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->load_from_eaddr) |
| 382 | rc = vcpu->kvm->arch.kvm_ops->load_from_eaddr(vcpu, eaddr, ptr, |
| 383 | size); |
| 384 | |
| 385 | if ((!rc) || (rc == -EAGAIN)) |
| 386 | return rc; |
| 387 | |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 388 | rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST, |
| 389 | XLATE_READ, &pte); |
| 390 | if (rc) |
| 391 | return rc; |
| 392 | |
| 393 | *eaddr = pte.raddr; |
| 394 | |
| 395 | if (!pte.may_read) |
| 396 | return -EPERM; |
| 397 | |
| 398 | if (!data && !pte.may_execute) |
| 399 | return -ENOEXEC; |
| 400 | |
Alexander Graf | c12fb43 | 2014-06-20 14:43:36 +0200 | [diff] [blame] | 401 | /* Magic page override */ |
| 402 | if (kvmppc_supports_magic_page(vcpu) && mp_pa && |
| 403 | ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) && |
| 404 | !(kvmppc_get_msr(vcpu) & MSR_PR)) { |
| 405 | void *magic = vcpu->arch.shared; |
| 406 | magic += pte.eaddr & 0xfff; |
| 407 | memcpy(ptr, magic, size); |
| 408 | return EMULATE_DONE; |
| 409 | } |
| 410 | |
Alexander Graf | c45c551 | 2014-06-20 14:17:30 +0200 | [diff] [blame] | 411 | if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size)) |
| 412 | return EMULATE_DO_MMIO; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 413 | |
| 414 | return EMULATE_DONE; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 415 | } |
| 416 | EXPORT_SYMBOL_GPL(kvmppc_ld); |
| 417 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 418 | int kvm_arch_hardware_enable(void) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 419 | { |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 420 | return 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 421 | } |
| 422 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 423 | int kvm_arch_hardware_setup(void) |
| 424 | { |
| 425 | return 0; |
| 426 | } |
| 427 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 428 | void kvm_arch_check_processor_compat(void *rtn) |
| 429 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 430 | *(int *)rtn = kvmppc_core_check_processor_compat(); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 431 | } |
| 432 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 433 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 434 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 435 | struct kvmppc_ops *kvm_ops = NULL; |
| 436 | /* |
| 437 | * if we have both HV and PR enabled, default is HV |
| 438 | */ |
| 439 | if (type == 0) { |
| 440 | if (kvmppc_hv_ops) |
| 441 | kvm_ops = kvmppc_hv_ops; |
| 442 | else |
| 443 | kvm_ops = kvmppc_pr_ops; |
| 444 | if (!kvm_ops) |
| 445 | goto err_out; |
| 446 | } else if (type == KVM_VM_PPC_HV) { |
| 447 | if (!kvmppc_hv_ops) |
| 448 | goto err_out; |
| 449 | kvm_ops = kvmppc_hv_ops; |
| 450 | } else if (type == KVM_VM_PPC_PR) { |
| 451 | if (!kvmppc_pr_ops) |
| 452 | goto err_out; |
| 453 | kvm_ops = kvmppc_pr_ops; |
| 454 | } else |
| 455 | goto err_out; |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 456 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 457 | if (kvm_ops->owner && !try_module_get(kvm_ops->owner)) |
| 458 | return -ENOENT; |
| 459 | |
| 460 | kvm->arch.kvm_ops = kvm_ops; |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 461 | return kvmppc_core_init_vm(kvm); |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 462 | err_out: |
| 463 | return -EINVAL; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 464 | } |
| 465 | |
Luiz Capitulino | 235539b | 2016-09-07 14:47:23 -0400 | [diff] [blame] | 466 | bool kvm_arch_has_vcpu_debugfs(void) |
| 467 | { |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) |
| 472 | { |
| 473 | return 0; |
| 474 | } |
| 475 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 476 | void kvm_arch_destroy_vm(struct kvm *kvm) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 477 | { |
| 478 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 479 | struct kvm_vcpu *vcpu; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 480 | |
Suresh E. Warrier | e17769e | 2015-12-21 16:22:51 -0600 | [diff] [blame] | 481 | #ifdef CONFIG_KVM_XICS |
| 482 | /* |
| 483 | * We call kick_all_cpus_sync() to ensure that all |
| 484 | * CPUs have executed any pending IPIs before we |
| 485 | * continue and free VCPUs structures below. |
| 486 | */ |
| 487 | if (is_kvmppc_hv_enabled(kvm)) |
| 488 | kick_all_cpus_sync(); |
| 489 | #endif |
| 490 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 491 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 492 | kvm_arch_vcpu_free(vcpu); |
| 493 | |
| 494 | mutex_lock(&kvm->lock); |
| 495 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 496 | kvm->vcpus[i] = NULL; |
| 497 | |
| 498 | atomic_set(&kvm->online_vcpus, 0); |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 499 | |
| 500 | kvmppc_core_destroy_vm(kvm); |
| 501 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 502 | mutex_unlock(&kvm->lock); |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 503 | |
| 504 | /* drop the module reference */ |
| 505 | module_put(kvm->arch.kvm_ops->owner); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 506 | } |
| 507 | |
Alexander Graf | 784aa3d | 2014-07-14 18:27:35 +0200 | [diff] [blame] | 508 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 509 | { |
| 510 | int r; |
Alexander Graf | 7a58777 | 2014-07-14 18:55:19 +0200 | [diff] [blame] | 511 | /* Assume we're using HV mode when the HV module is loaded */ |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 512 | int hv_enabled = kvmppc_hv_ops ? 1 : 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 513 | |
Alexander Graf | 7a58777 | 2014-07-14 18:55:19 +0200 | [diff] [blame] | 514 | if (kvm) { |
| 515 | /* |
| 516 | * Hooray - we know which VM type we're running on. Depend on |
| 517 | * that rather than the guess above. |
| 518 | */ |
| 519 | hv_enabled = is_kvmppc_hv_enabled(kvm); |
| 520 | } |
| 521 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 522 | switch (ext) { |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 523 | #ifdef CONFIG_BOOKE |
| 524 | case KVM_CAP_PPC_BOOKE_SREGS: |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 525 | case KVM_CAP_PPC_BOOKE_WATCHDOG: |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 526 | case KVM_CAP_PPC_EPR: |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 527 | #else |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 528 | case KVM_CAP_PPC_SEGSTATE: |
Alexander Graf | 1022fc3 | 2011-09-14 21:45:23 +0200 | [diff] [blame] | 529 | case KVM_CAP_PPC_HIOR: |
Alexander Graf | 930b412 | 2011-08-08 17:29:42 +0200 | [diff] [blame] | 530 | case KVM_CAP_PPC_PAPR: |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 531 | #endif |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 532 | case KVM_CAP_PPC_UNSET_IRQ: |
Alexander Graf | 7b4203e | 2010-08-30 13:50:45 +0200 | [diff] [blame] | 533 | case KVM_CAP_PPC_IRQ_LEVEL: |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 534 | case KVM_CAP_ENABLE_CAP: |
Alexander Graf | e24ed81 | 2011-09-14 10:02:41 +0200 | [diff] [blame] | 535 | case KVM_CAP_ONE_REG: |
Alexander Graf | 0e673fb | 2012-10-09 00:06:20 +0200 | [diff] [blame] | 536 | case KVM_CAP_IOEVENTFD: |
Scott Wood | 5df554ad | 2013-04-12 14:08:46 +0000 | [diff] [blame] | 537 | case KVM_CAP_DEVICE_CTRL: |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 538 | case KVM_CAP_IMMEDIATE_EXIT: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 539 | r = 1; |
| 540 | break; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 541 | case KVM_CAP_PPC_PAIRED_SINGLES: |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 542 | case KVM_CAP_PPC_OSI: |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 543 | case KVM_CAP_PPC_GET_PVINFO: |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 544 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 545 | case KVM_CAP_SW_TLB: |
| 546 | #endif |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 547 | /* We support this only for PR */ |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 548 | r = !hv_enabled; |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 549 | break; |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 550 | #ifdef CONFIG_KVM_MPIC |
| 551 | case KVM_CAP_IRQ_MPIC: |
| 552 | r = 1; |
| 553 | break; |
| 554 | #endif |
| 555 | |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 556 | #ifdef CONFIG_PPC_BOOK3S_64 |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 557 | case KVM_CAP_SPAPR_TCE: |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 558 | case KVM_CAP_SPAPR_TCE_64: |
Suraj Jitindar Singh | 693ac10 | 2018-12-14 16:29:03 +1100 | [diff] [blame] | 559 | r = 1; |
| 560 | break; |
Alexey Kardashevskiy | 121f80b | 2017-03-22 15:21:56 +1100 | [diff] [blame] | 561 | case KVM_CAP_SPAPR_TCE_VFIO: |
Suraj Jitindar Singh | 693ac10 | 2018-12-14 16:29:03 +1100 | [diff] [blame] | 562 | r = !!cpu_has_feature(CPU_FTR_HVMODE); |
| 563 | break; |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 564 | case KVM_CAP_PPC_RTAS: |
Alexander Graf | f2e9104 | 2014-05-22 17:40:15 +0200 | [diff] [blame] | 565 | case KVM_CAP_PPC_FIXUP_HCALL: |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 566 | case KVM_CAP_PPC_ENABLE_HCALL: |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 567 | #ifdef CONFIG_KVM_XICS |
| 568 | case KVM_CAP_IRQ_XICS: |
| 569 | #endif |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 570 | case KVM_CAP_PPC_GET_CPU_CHAR: |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 571 | r = 1; |
| 572 | break; |
Cédric Le Goater | eacc56b | 2019-04-18 12:39:28 +0200 | [diff] [blame] | 573 | #ifdef CONFIG_KVM_XIVE |
| 574 | case KVM_CAP_PPC_IRQ_XIVE: |
| 575 | /* |
Cédric Le Goater | 3fab2d1 | 2019-04-18 12:39:40 +0200 | [diff] [blame^] | 576 | * We need XIVE to be enabled on the platform (implies |
| 577 | * a POWER9 processor) and the PowerNV platform, as |
| 578 | * nested is not yet supported. |
Cédric Le Goater | eacc56b | 2019-04-18 12:39:28 +0200 | [diff] [blame] | 579 | */ |
Cédric Le Goater | 3fab2d1 | 2019-04-18 12:39:40 +0200 | [diff] [blame^] | 580 | r = xive_enabled() && !!cpu_has_feature(CPU_FTR_HVMODE); |
Cédric Le Goater | eacc56b | 2019-04-18 12:39:28 +0200 | [diff] [blame] | 581 | break; |
| 582 | #endif |
David Gibson | a8acaec | 2016-11-23 16:14:07 +1100 | [diff] [blame] | 583 | |
| 584 | case KVM_CAP_PPC_ALLOC_HTAB: |
| 585 | r = hv_enabled; |
| 586 | break; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 587 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 588 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 589 | case KVM_CAP_PPC_SMT: |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 590 | r = 0; |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 591 | if (kvm) { |
| 592 | if (kvm->arch.emul_smt_mode > 1) |
| 593 | r = kvm->arch.emul_smt_mode; |
| 594 | else |
| 595 | r = kvm->arch.smt_mode; |
| 596 | } else if (hv_enabled) { |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 597 | if (cpu_has_feature(CPU_FTR_ARCH_300)) |
| 598 | r = 1; |
| 599 | else |
| 600 | r = threads_per_subcore; |
| 601 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 602 | break; |
Paul Mackerras | 2ed4f9d | 2017-06-21 16:01:27 +1000 | [diff] [blame] | 603 | case KVM_CAP_PPC_SMT_POSSIBLE: |
| 604 | r = 1; |
| 605 | if (hv_enabled) { |
| 606 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
| 607 | r = ((threads_per_subcore << 1) - 1); |
| 608 | else |
| 609 | /* P9 can emulate dbells, so allow any mode */ |
| 610 | r = 8 | 4 | 2 | 1; |
| 611 | } |
| 612 | break; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 613 | case KVM_CAP_PPC_RMA: |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 614 | r = 0; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 615 | break; |
Michael Ellerman | e928e9c | 2015-03-20 20:39:41 +1100 | [diff] [blame] | 616 | case KVM_CAP_PPC_HWRNG: |
| 617 | r = kvmppc_hwrng_present(); |
| 618 | break; |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 619 | case KVM_CAP_PPC_MMU_RADIX: |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 620 | r = !!(hv_enabled && radix_enabled()); |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 621 | break; |
| 622 | case KVM_CAP_PPC_MMU_HASH_V3: |
Paul Mackerras | de760db | 2018-10-08 16:31:16 +1100 | [diff] [blame] | 623 | r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) && |
| 624 | cpu_has_feature(CPU_FTR_HVMODE)); |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 625 | break; |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 626 | case KVM_CAP_PPC_NESTED_HV: |
| 627 | r = !!(hv_enabled && kvmppc_hv_ops->enable_nested && |
| 628 | !kvmppc_hv_ops->enable_nested(NULL)); |
| 629 | break; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 630 | #endif |
Alexander Graf | f4800b1 | 2012-08-07 10:24:14 +0200 | [diff] [blame] | 631 | case KVM_CAP_SYNC_MMU: |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 632 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 633 | r = hv_enabled; |
Alexander Graf | f4800b1 | 2012-08-07 10:24:14 +0200 | [diff] [blame] | 634 | #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 635 | r = 1; |
| 636 | #else |
| 637 | r = 0; |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 638 | #endif |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 639 | break; |
| 640 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 641 | case KVM_CAP_PPC_HTAB_FD: |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 642 | r = hv_enabled; |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 643 | break; |
Alexander Graf | f4800b1 | 2012-08-07 10:24:14 +0200 | [diff] [blame] | 644 | #endif |
Matt Evans | b543403 | 2011-12-07 16:55:57 +0000 | [diff] [blame] | 645 | case KVM_CAP_NR_VCPUS: |
| 646 | /* |
| 647 | * Recommending a number of CPUs is somewhat arbitrary; we |
| 648 | * return the number of present CPUs for -HV (since a host |
| 649 | * will have secondary threads "offline"), and for other KVM |
| 650 | * implementations just count online CPUs. |
| 651 | */ |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 652 | if (hv_enabled) |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 653 | r = num_present_cpus(); |
| 654 | else |
| 655 | r = num_online_cpus(); |
Matt Evans | b543403 | 2011-12-07 16:55:57 +0000 | [diff] [blame] | 656 | break; |
Nikunj A Dadhania | bfec5c2c | 2015-10-16 10:27:53 +0530 | [diff] [blame] | 657 | case KVM_CAP_NR_MEMSLOTS: |
| 658 | r = KVM_USER_MEM_SLOTS; |
| 659 | break; |
Matt Evans | b543403 | 2011-12-07 16:55:57 +0000 | [diff] [blame] | 660 | case KVM_CAP_MAX_VCPUS: |
| 661 | r = KVM_MAX_VCPUS; |
| 662 | break; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 663 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 664 | case KVM_CAP_PPC_GET_SMMU_INFO: |
| 665 | r = 1; |
| 666 | break; |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 667 | case KVM_CAP_SPAPR_MULTITCE: |
| 668 | r = 1; |
| 669 | break; |
David Gibson | 050f233 | 2016-12-20 16:49:07 +1100 | [diff] [blame] | 670 | case KVM_CAP_SPAPR_RESIZE_HPT: |
David Gibson | 790a9df | 2018-02-02 14:29:08 +1100 | [diff] [blame] | 671 | r = !!hv_enabled; |
David Gibson | 050f233 | 2016-12-20 16:49:07 +1100 | [diff] [blame] | 672 | break; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 673 | #endif |
Aravinda Prasad | 134764e | 2017-05-11 16:32:48 +0530 | [diff] [blame] | 674 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
| 675 | case KVM_CAP_PPC_FWNMI: |
| 676 | r = hv_enabled; |
| 677 | break; |
| 678 | #endif |
Paul Mackerras | 4bb3c7a | 2018-03-21 21:32:01 +1100 | [diff] [blame] | 679 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
Sam Bobroff | 23528bb | 2016-07-20 13:41:36 +1000 | [diff] [blame] | 680 | case KVM_CAP_PPC_HTM: |
Simon Guo | d234d68 | 2018-05-23 15:02:08 +0800 | [diff] [blame] | 681 | r = !!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM) || |
| 682 | (hv_enabled && cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)); |
Sam Bobroff | 23528bb | 2016-07-20 13:41:36 +1000 | [diff] [blame] | 683 | break; |
Paul Mackerras | 4bb3c7a | 2018-03-21 21:32:01 +1100 | [diff] [blame] | 684 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 685 | default: |
| 686 | r = 0; |
| 687 | break; |
| 688 | } |
| 689 | return r; |
| 690 | |
| 691 | } |
| 692 | |
| 693 | long kvm_arch_dev_ioctl(struct file *filp, |
| 694 | unsigned int ioctl, unsigned long arg) |
| 695 | { |
| 696 | return -EINVAL; |
| 697 | } |
| 698 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 699 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 700 | struct kvm_memory_slot *dont) |
| 701 | { |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 702 | kvmppc_core_free_memslot(kvm, free, dont); |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 703 | } |
| 704 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 705 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
| 706 | unsigned long npages) |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 707 | { |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 708 | return kvmppc_core_create_memslot(kvm, slot, npages); |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 709 | } |
| 710 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 711 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
Takuya Yoshikawa | 462fce4 | 2013-02-27 19:41:56 +0900 | [diff] [blame] | 712 | struct kvm_memory_slot *memslot, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 713 | const struct kvm_userspace_memory_region *mem, |
Takuya Yoshikawa | 7b6195a | 2013-02-27 19:44:34 +0900 | [diff] [blame] | 714 | enum kvm_mr_change change) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 715 | { |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 716 | return kvmppc_core_prepare_memory_region(kvm, memslot, mem); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 717 | } |
| 718 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 719 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 720 | const struct kvm_userspace_memory_region *mem, |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 721 | const struct kvm_memory_slot *old, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 722 | const struct kvm_memory_slot *new, |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 723 | enum kvm_mr_change change) |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 724 | { |
Bharata B Rao | f032b73 | 2018-12-12 15:15:30 +1100 | [diff] [blame] | 725 | kvmppc_core_commit_memory_region(kvm, mem, old, new, change); |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 726 | } |
| 727 | |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 728 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 729 | struct kvm_memory_slot *slot) |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 730 | { |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 731 | kvmppc_core_flush_memslot(kvm, slot); |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 732 | } |
| 733 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 734 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
| 735 | { |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 736 | struct kvm_vcpu *vcpu; |
| 737 | vcpu = kvmppc_core_vcpu_create(kvm, id); |
Matt Evans | 03cdab5 | 2011-12-06 21:19:42 +0000 | [diff] [blame] | 738 | if (!IS_ERR(vcpu)) { |
| 739 | vcpu->arch.wqp = &vcpu->wq; |
Wei Yongjun | 06056bf | 2010-03-09 14:13:43 +0800 | [diff] [blame] | 740 | kvmppc_create_vcpu_debugfs(vcpu, id); |
Matt Evans | 03cdab5 | 2011-12-06 21:19:42 +0000 | [diff] [blame] | 741 | } |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 742 | return vcpu; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 743 | } |
| 744 | |
Dominik Dingel | 31928aa | 2014-12-04 15:47:07 +0100 | [diff] [blame] | 745 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 746 | { |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 747 | } |
| 748 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 749 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 750 | { |
Alexander Graf | a595405 | 2010-02-22 16:52:14 +0100 | [diff] [blame] | 751 | /* Make sure we're not using the vcpu anymore */ |
| 752 | hrtimer_cancel(&vcpu->arch.dec_timer); |
Alexander Graf | a595405 | 2010-02-22 16:52:14 +0100 | [diff] [blame] | 753 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 754 | kvmppc_remove_vcpu_debugfs(vcpu); |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 755 | |
| 756 | switch (vcpu->arch.irq_type) { |
| 757 | case KVMPPC_IRQ_MPIC: |
| 758 | kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu); |
| 759 | break; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 760 | case KVMPPC_IRQ_XICS: |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 761 | if (xics_on_xive()) |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 762 | kvmppc_xive_cleanup_vcpu(vcpu); |
| 763 | else |
| 764 | kvmppc_xics_free_icp(vcpu); |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 765 | break; |
Cédric Le Goater | eacc56b | 2019-04-18 12:39:28 +0200 | [diff] [blame] | 766 | case KVMPPC_IRQ_XIVE: |
| 767 | kvmppc_xive_native_cleanup_vcpu(vcpu); |
| 768 | break; |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 771 | kvmppc_core_vcpu_free(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 775 | { |
| 776 | kvm_arch_vcpu_free(vcpu); |
| 777 | } |
| 778 | |
| 779 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 780 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 781 | return kvmppc_core_pending_dec(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 782 | } |
| 783 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 784 | static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer) |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 785 | { |
| 786 | struct kvm_vcpu *vcpu; |
| 787 | |
| 788 | vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer); |
Mihai Caraman | d02d4d1 | 2014-09-01 17:19:56 +0300 | [diff] [blame] | 789 | kvmppc_decrementer_func(vcpu); |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 790 | |
| 791 | return HRTIMER_NORESTART; |
| 792 | } |
| 793 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 794 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 795 | { |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 796 | int ret; |
| 797 | |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 798 | hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 799 | vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup; |
Paul Mackerras | 5855564 | 2018-01-12 20:55:20 +1100 | [diff] [blame] | 800 | vcpu->arch.dec_expires = get_tb(); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 801 | |
Bharat Bhushan | 09000ad | 2011-03-25 10:32:13 +0530 | [diff] [blame] | 802 | #ifdef CONFIG_KVM_EXIT_TIMING |
| 803 | mutex_init(&vcpu->arch.exit_timing_lock); |
| 804 | #endif |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 805 | ret = kvmppc_subarch_vcpu_init(vcpu); |
| 806 | return ret; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 810 | { |
Hollis Blanchard | ecc0981 | 2009-01-03 16:22:59 -0600 | [diff] [blame] | 811 | kvmppc_mmu_destroy(vcpu); |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 812 | kvmppc_subarch_vcpu_uninit(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 816 | { |
Scott Wood | eab1767 | 2011-04-27 17:24:10 -0500 | [diff] [blame] | 817 | #ifdef CONFIG_BOOKE |
| 818 | /* |
| 819 | * vrsave (formerly usprg0) isn't used by Linux, but may |
| 820 | * be used by the guest. |
| 821 | * |
| 822 | * On non-booke this is associated with Altivec and |
| 823 | * is handled by code in book3s.c. |
| 824 | */ |
| 825 | mtspr(SPRN_VRSAVE, vcpu->arch.vrsave); |
| 826 | #endif |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 827 | kvmppc_core_vcpu_load(vcpu, cpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 831 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 832 | kvmppc_core_vcpu_put(vcpu); |
Scott Wood | eab1767 | 2011-04-27 17:24:10 -0500 | [diff] [blame] | 833 | #ifdef CONFIG_BOOKE |
| 834 | vcpu->arch.vrsave = mfspr(SPRN_VRSAVE); |
| 835 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 836 | } |
| 837 | |
Suresh Warrier | 9576730 | 2016-08-19 15:35:47 +1000 | [diff] [blame] | 838 | /* |
| 839 | * irq_bypass_add_producer and irq_bypass_del_producer are only |
| 840 | * useful if the architecture supports PCI passthrough. |
| 841 | * irq_bypass_stop and irq_bypass_start are not needed and so |
| 842 | * kvm_ops are not defined for them. |
| 843 | */ |
| 844 | bool kvm_arch_has_irq_bypass(void) |
| 845 | { |
| 846 | return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) || |
| 847 | (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer)); |
| 848 | } |
| 849 | |
| 850 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
| 851 | struct irq_bypass_producer *prod) |
| 852 | { |
| 853 | struct kvm_kernel_irqfd *irqfd = |
| 854 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 855 | struct kvm *kvm = irqfd->kvm; |
| 856 | |
| 857 | if (kvm->arch.kvm_ops->irq_bypass_add_producer) |
| 858 | return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod); |
| 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, |
| 864 | struct irq_bypass_producer *prod) |
| 865 | { |
| 866 | struct kvm_kernel_irqfd *irqfd = |
| 867 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 868 | struct kvm *kvm = irqfd->kvm; |
| 869 | |
| 870 | if (kvm->arch.kvm_ops->irq_bypass_del_producer) |
| 871 | kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod); |
| 872 | } |
| 873 | |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 874 | #ifdef CONFIG_VSX |
| 875 | static inline int kvmppc_get_vsr_dword_offset(int index) |
| 876 | { |
| 877 | int offset; |
| 878 | |
| 879 | if ((index != 0) && (index != 1)) |
| 880 | return -1; |
| 881 | |
| 882 | #ifdef __BIG_ENDIAN |
| 883 | offset = index; |
| 884 | #else |
| 885 | offset = 1 - index; |
| 886 | #endif |
| 887 | |
| 888 | return offset; |
| 889 | } |
| 890 | |
| 891 | static inline int kvmppc_get_vsr_word_offset(int index) |
| 892 | { |
| 893 | int offset; |
| 894 | |
| 895 | if ((index > 3) || (index < 0)) |
| 896 | return -1; |
| 897 | |
| 898 | #ifdef __BIG_ENDIAN |
| 899 | offset = index; |
| 900 | #else |
| 901 | offset = 3 - index; |
| 902 | #endif |
| 903 | return offset; |
| 904 | } |
| 905 | |
| 906 | static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu, |
| 907 | u64 gpr) |
| 908 | { |
| 909 | union kvmppc_one_reg val; |
| 910 | int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset); |
| 911 | int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; |
| 912 | |
| 913 | if (offset == -1) |
| 914 | return; |
| 915 | |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 916 | if (index >= 32) { |
| 917 | val.vval = VCPU_VSX_VR(vcpu, index - 32); |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 918 | val.vsxval[offset] = gpr; |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 919 | VCPU_VSX_VR(vcpu, index - 32) = val.vval; |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 920 | } else { |
| 921 | VCPU_VSX_FPR(vcpu, index, offset) = gpr; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu, |
| 926 | u64 gpr) |
| 927 | { |
| 928 | union kvmppc_one_reg val; |
| 929 | int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; |
| 930 | |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 931 | if (index >= 32) { |
| 932 | val.vval = VCPU_VSX_VR(vcpu, index - 32); |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 933 | val.vsxval[0] = gpr; |
| 934 | val.vsxval[1] = gpr; |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 935 | VCPU_VSX_VR(vcpu, index - 32) = val.vval; |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 936 | } else { |
| 937 | VCPU_VSX_FPR(vcpu, index, 0) = gpr; |
| 938 | VCPU_VSX_FPR(vcpu, index, 1) = gpr; |
| 939 | } |
| 940 | } |
| 941 | |
Simon Guo | 94dd7fa | 2018-05-21 13:24:20 +0800 | [diff] [blame] | 942 | static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu *vcpu, |
| 943 | u32 gpr) |
| 944 | { |
| 945 | union kvmppc_one_reg val; |
| 946 | int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; |
| 947 | |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 948 | if (index >= 32) { |
Simon Guo | 94dd7fa | 2018-05-21 13:24:20 +0800 | [diff] [blame] | 949 | val.vsx32val[0] = gpr; |
| 950 | val.vsx32val[1] = gpr; |
| 951 | val.vsx32val[2] = gpr; |
| 952 | val.vsx32val[3] = gpr; |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 953 | VCPU_VSX_VR(vcpu, index - 32) = val.vval; |
Simon Guo | 94dd7fa | 2018-05-21 13:24:20 +0800 | [diff] [blame] | 954 | } else { |
| 955 | val.vsx32val[0] = gpr; |
| 956 | val.vsx32val[1] = gpr; |
| 957 | VCPU_VSX_FPR(vcpu, index, 0) = val.vsxval[0]; |
| 958 | VCPU_VSX_FPR(vcpu, index, 1) = val.vsxval[0]; |
| 959 | } |
| 960 | } |
| 961 | |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 962 | static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu, |
| 963 | u32 gpr32) |
| 964 | { |
| 965 | union kvmppc_one_reg val; |
| 966 | int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset); |
| 967 | int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; |
| 968 | int dword_offset, word_offset; |
| 969 | |
| 970 | if (offset == -1) |
| 971 | return; |
| 972 | |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 973 | if (index >= 32) { |
| 974 | val.vval = VCPU_VSX_VR(vcpu, index - 32); |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 975 | val.vsx32val[offset] = gpr32; |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 976 | VCPU_VSX_VR(vcpu, index - 32) = val.vval; |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 977 | } else { |
| 978 | dword_offset = offset / 2; |
| 979 | word_offset = offset % 2; |
| 980 | val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset); |
| 981 | val.vsx32val[word_offset] = gpr32; |
| 982 | VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0]; |
| 983 | } |
| 984 | } |
| 985 | #endif /* CONFIG_VSX */ |
| 986 | |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 987 | #ifdef CONFIG_ALTIVEC |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 988 | static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu *vcpu, |
| 989 | int index, int element_size) |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 990 | { |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 991 | int offset; |
| 992 | int elts = sizeof(vector128)/element_size; |
| 993 | |
| 994 | if ((index < 0) || (index >= elts)) |
| 995 | return -1; |
| 996 | |
| 997 | if (kvmppc_need_byteswap(vcpu)) |
| 998 | offset = elts - index - 1; |
| 999 | else |
| 1000 | offset = index; |
| 1001 | |
| 1002 | return offset; |
| 1003 | } |
| 1004 | |
| 1005 | static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu *vcpu, |
| 1006 | int index) |
| 1007 | { |
| 1008 | return kvmppc_get_vmx_offset_generic(vcpu, index, 8); |
| 1009 | } |
| 1010 | |
| 1011 | static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu *vcpu, |
| 1012 | int index) |
| 1013 | { |
| 1014 | return kvmppc_get_vmx_offset_generic(vcpu, index, 4); |
| 1015 | } |
| 1016 | |
| 1017 | static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu *vcpu, |
| 1018 | int index) |
| 1019 | { |
| 1020 | return kvmppc_get_vmx_offset_generic(vcpu, index, 2); |
| 1021 | } |
| 1022 | |
| 1023 | static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu *vcpu, |
| 1024 | int index) |
| 1025 | { |
| 1026 | return kvmppc_get_vmx_offset_generic(vcpu, index, 1); |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu, |
| 1031 | u64 gpr) |
| 1032 | { |
| 1033 | union kvmppc_one_reg val; |
| 1034 | int offset = kvmppc_get_vmx_dword_offset(vcpu, |
| 1035 | vcpu->arch.mmio_vmx_offset); |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1036 | int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1037 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1038 | if (offset == -1) |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1039 | return; |
| 1040 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1041 | val.vval = VCPU_VSX_VR(vcpu, index); |
| 1042 | val.vsxval[offset] = gpr; |
| 1043 | VCPU_VSX_VR(vcpu, index) = val.vval; |
| 1044 | } |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1045 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1046 | static inline void kvmppc_set_vmx_word(struct kvm_vcpu *vcpu, |
| 1047 | u32 gpr32) |
| 1048 | { |
| 1049 | union kvmppc_one_reg val; |
| 1050 | int offset = kvmppc_get_vmx_word_offset(vcpu, |
| 1051 | vcpu->arch.mmio_vmx_offset); |
| 1052 | int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; |
| 1053 | |
| 1054 | if (offset == -1) |
| 1055 | return; |
| 1056 | |
| 1057 | val.vval = VCPU_VSX_VR(vcpu, index); |
| 1058 | val.vsx32val[offset] = gpr32; |
| 1059 | VCPU_VSX_VR(vcpu, index) = val.vval; |
| 1060 | } |
| 1061 | |
| 1062 | static inline void kvmppc_set_vmx_hword(struct kvm_vcpu *vcpu, |
| 1063 | u16 gpr16) |
| 1064 | { |
| 1065 | union kvmppc_one_reg val; |
| 1066 | int offset = kvmppc_get_vmx_hword_offset(vcpu, |
| 1067 | vcpu->arch.mmio_vmx_offset); |
| 1068 | int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; |
| 1069 | |
| 1070 | if (offset == -1) |
| 1071 | return; |
| 1072 | |
| 1073 | val.vval = VCPU_VSX_VR(vcpu, index); |
| 1074 | val.vsx16val[offset] = gpr16; |
| 1075 | VCPU_VSX_VR(vcpu, index) = val.vval; |
| 1076 | } |
| 1077 | |
| 1078 | static inline void kvmppc_set_vmx_byte(struct kvm_vcpu *vcpu, |
| 1079 | u8 gpr8) |
| 1080 | { |
| 1081 | union kvmppc_one_reg val; |
| 1082 | int offset = kvmppc_get_vmx_byte_offset(vcpu, |
| 1083 | vcpu->arch.mmio_vmx_offset); |
| 1084 | int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; |
| 1085 | |
| 1086 | if (offset == -1) |
| 1087 | return; |
| 1088 | |
| 1089 | val.vval = VCPU_VSX_VR(vcpu, index); |
| 1090 | val.vsx8val[offset] = gpr8; |
| 1091 | VCPU_VSX_VR(vcpu, index) = val.vval; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1092 | } |
| 1093 | #endif /* CONFIG_ALTIVEC */ |
| 1094 | |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1095 | #ifdef CONFIG_PPC_FPU |
| 1096 | static inline u64 sp_to_dp(u32 fprs) |
| 1097 | { |
| 1098 | u64 fprd; |
| 1099 | |
| 1100 | preempt_disable(); |
| 1101 | enable_kernel_fp(); |
| 1102 | asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd) : "m" (fprs) |
| 1103 | : "fr0"); |
| 1104 | preempt_enable(); |
| 1105 | return fprd; |
| 1106 | } |
| 1107 | |
| 1108 | static inline u32 dp_to_sp(u64 fprd) |
| 1109 | { |
| 1110 | u32 fprs; |
| 1111 | |
| 1112 | preempt_disable(); |
| 1113 | enable_kernel_fp(); |
| 1114 | asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs) : "m" (fprd) |
| 1115 | : "fr0"); |
| 1116 | preempt_enable(); |
| 1117 | return fprs; |
| 1118 | } |
| 1119 | |
| 1120 | #else |
| 1121 | #define sp_to_dp(x) (x) |
| 1122 | #define dp_to_sp(x) (x) |
| 1123 | #endif /* CONFIG_PPC_FPU */ |
| 1124 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1125 | static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, |
| 1126 | struct kvm_run *run) |
| 1127 | { |
Denis Kirjanov | 69b6183 | 2010-06-11 11:23:26 +0000 | [diff] [blame] | 1128 | u64 uninitialized_var(gpr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1129 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 1130 | if (run->mmio.len > sizeof(gpr)) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1131 | printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len); |
| 1132 | return; |
| 1133 | } |
| 1134 | |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1135 | if (!vcpu->arch.mmio_host_swabbed) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1136 | switch (run->mmio.len) { |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 1137 | case 8: gpr = *(u64 *)run->mmio.data; break; |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 1138 | case 4: gpr = *(u32 *)run->mmio.data; break; |
| 1139 | case 2: gpr = *(u16 *)run->mmio.data; break; |
| 1140 | case 1: gpr = *(u8 *)run->mmio.data; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1141 | } |
| 1142 | } else { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1143 | switch (run->mmio.len) { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1144 | case 8: gpr = swab64(*(u64 *)run->mmio.data); break; |
| 1145 | case 4: gpr = swab32(*(u32 *)run->mmio.data); break; |
| 1146 | case 2: gpr = swab16(*(u16 *)run->mmio.data); break; |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 1147 | case 1: gpr = *(u8 *)run->mmio.data; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1148 | } |
| 1149 | } |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 1150 | |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1151 | /* conversion between single and double precision */ |
| 1152 | if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4)) |
| 1153 | gpr = sp_to_dp(gpr); |
| 1154 | |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 1155 | if (vcpu->arch.mmio_sign_extend) { |
| 1156 | switch (run->mmio.len) { |
| 1157 | #ifdef CONFIG_PPC64 |
| 1158 | case 4: |
| 1159 | gpr = (s64)(s32)gpr; |
| 1160 | break; |
| 1161 | #endif |
| 1162 | case 2: |
| 1163 | gpr = (s64)(s16)gpr; |
| 1164 | break; |
| 1165 | case 1: |
| 1166 | gpr = (s64)(s8)gpr; |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 1171 | switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) { |
| 1172 | case KVM_MMIO_REG_GPR: |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 1173 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
| 1174 | break; |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 1175 | case KVM_MMIO_REG_FPR: |
Simon Guo | 2e6baa4 | 2018-05-21 13:24:22 +0800 | [diff] [blame] | 1176 | if (vcpu->kvm->arch.kvm_ops->giveup_ext) |
| 1177 | vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_FP); |
| 1178 | |
Paul Mackerras | efff191 | 2013-10-15 20:43:02 +1100 | [diff] [blame] | 1179 | VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr; |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 1180 | break; |
Alexander Graf | 287d561 | 2010-04-01 15:33:21 +0200 | [diff] [blame] | 1181 | #ifdef CONFIG_PPC_BOOK3S |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 1182 | case KVM_MMIO_REG_QPR: |
| 1183 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 1184 | break; |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 1185 | case KVM_MMIO_REG_FQPR: |
Paul Mackerras | efff191 | 2013-10-15 20:43:02 +1100 | [diff] [blame] | 1186 | VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr; |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 1187 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 1188 | break; |
Alexander Graf | 287d561 | 2010-04-01 15:33:21 +0200 | [diff] [blame] | 1189 | #endif |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1190 | #ifdef CONFIG_VSX |
| 1191 | case KVM_MMIO_REG_VSX: |
Simon Guo | 2e6baa4 | 2018-05-21 13:24:22 +0800 | [diff] [blame] | 1192 | if (vcpu->kvm->arch.kvm_ops->giveup_ext) |
| 1193 | vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VSX); |
| 1194 | |
Simon Guo | da2a32b | 2018-05-21 13:24:25 +0800 | [diff] [blame] | 1195 | if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_DWORD) |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1196 | kvmppc_set_vsr_dword(vcpu, gpr); |
Simon Guo | da2a32b | 2018-05-21 13:24:25 +0800 | [diff] [blame] | 1197 | else if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_WORD) |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1198 | kvmppc_set_vsr_word(vcpu, gpr); |
Simon Guo | da2a32b | 2018-05-21 13:24:25 +0800 | [diff] [blame] | 1199 | else if (vcpu->arch.mmio_copy_type == |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1200 | KVMPPC_VSX_COPY_DWORD_LOAD_DUMP) |
| 1201 | kvmppc_set_vsr_dword_dump(vcpu, gpr); |
Simon Guo | da2a32b | 2018-05-21 13:24:25 +0800 | [diff] [blame] | 1202 | else if (vcpu->arch.mmio_copy_type == |
Simon Guo | 94dd7fa | 2018-05-21 13:24:20 +0800 | [diff] [blame] | 1203 | KVMPPC_VSX_COPY_WORD_LOAD_DUMP) |
| 1204 | kvmppc_set_vsr_word_dump(vcpu, gpr); |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1205 | break; |
| 1206 | #endif |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1207 | #ifdef CONFIG_ALTIVEC |
| 1208 | case KVM_MMIO_REG_VMX: |
Simon Guo | 2e6baa4 | 2018-05-21 13:24:22 +0800 | [diff] [blame] | 1209 | if (vcpu->kvm->arch.kvm_ops->giveup_ext) |
| 1210 | vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VEC); |
| 1211 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1212 | if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_DWORD) |
| 1213 | kvmppc_set_vmx_dword(vcpu, gpr); |
| 1214 | else if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_WORD) |
| 1215 | kvmppc_set_vmx_word(vcpu, gpr); |
| 1216 | else if (vcpu->arch.mmio_copy_type == |
| 1217 | KVMPPC_VMX_COPY_HWORD) |
| 1218 | kvmppc_set_vmx_hword(vcpu, gpr); |
| 1219 | else if (vcpu->arch.mmio_copy_type == |
| 1220 | KVMPPC_VMX_COPY_BYTE) |
| 1221 | kvmppc_set_vmx_byte(vcpu, gpr); |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1222 | break; |
| 1223 | #endif |
Suraj Jitindar Singh | 873db2c | 2018-12-14 16:29:08 +1100 | [diff] [blame] | 1224 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
| 1225 | case KVM_MMIO_REG_NESTED_GPR: |
| 1226 | if (kvmppc_need_byteswap(vcpu)) |
| 1227 | gpr = swab64(gpr); |
| 1228 | kvm_vcpu_write_guest(vcpu, vcpu->arch.nested_io_gpr, &gpr, |
| 1229 | sizeof(gpr)); |
| 1230 | break; |
| 1231 | #endif |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 1232 | default: |
| 1233 | BUG(); |
| 1234 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1235 | } |
| 1236 | |
Paul Mackerras | eb8b056 | 2016-05-05 16:17:10 +1000 | [diff] [blame] | 1237 | static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 1238 | unsigned int rt, unsigned int bytes, |
| 1239 | int is_default_endian, int sign_extend) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1240 | { |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 1241 | int idx, ret; |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1242 | bool host_swabbed; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1243 | |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1244 | /* Pity C doesn't have a logical XOR operator */ |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1245 | if (kvmppc_need_byteswap(vcpu)) { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1246 | host_swabbed = is_default_endian; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1247 | } else { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1248 | host_swabbed = !is_default_endian; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1249 | } |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 1250 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1251 | if (bytes > sizeof(run->mmio.data)) { |
| 1252 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, |
| 1253 | run->mmio.len); |
| 1254 | } |
| 1255 | |
| 1256 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; |
| 1257 | run->mmio.len = bytes; |
| 1258 | run->mmio.is_write = 0; |
| 1259 | |
| 1260 | vcpu->arch.io_gpr = rt; |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1261 | vcpu->arch.mmio_host_swabbed = host_swabbed; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1262 | vcpu->mmio_needed = 1; |
| 1263 | vcpu->mmio_is_write = 0; |
Paul Mackerras | eb8b056 | 2016-05-05 16:17:10 +1000 | [diff] [blame] | 1264 | vcpu->arch.mmio_sign_extend = sign_extend; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1265 | |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 1266 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 1267 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 1268 | ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr, |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 1269 | bytes, &run->mmio.data); |
| 1270 | |
| 1271 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 1272 | |
| 1273 | if (!ret) { |
Alexander Graf | 0e673fb | 2012-10-09 00:06:20 +0200 | [diff] [blame] | 1274 | kvmppc_complete_mmio_load(vcpu, run); |
| 1275 | vcpu->mmio_needed = 0; |
| 1276 | return EMULATE_DONE; |
| 1277 | } |
| 1278 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1279 | return EMULATE_DO_MMIO; |
| 1280 | } |
Paul Mackerras | eb8b056 | 2016-05-05 16:17:10 +1000 | [diff] [blame] | 1281 | |
| 1282 | int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 1283 | unsigned int rt, unsigned int bytes, |
| 1284 | int is_default_endian) |
| 1285 | { |
| 1286 | return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0); |
| 1287 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 1288 | EXPORT_SYMBOL_GPL(kvmppc_handle_load); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1289 | |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 1290 | /* Same as above, but sign extends */ |
| 1291 | int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1292 | unsigned int rt, unsigned int bytes, |
| 1293 | int is_default_endian) |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 1294 | { |
Paul Mackerras | eb8b056 | 2016-05-05 16:17:10 +1000 | [diff] [blame] | 1295 | return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1); |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1298 | #ifdef CONFIG_VSX |
| 1299 | int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 1300 | unsigned int rt, unsigned int bytes, |
| 1301 | int is_default_endian, int mmio_sign_extend) |
| 1302 | { |
| 1303 | enum emulation_result emulated = EMULATE_DONE; |
| 1304 | |
Paul Mackerras | 9aa6825 | 2017-11-20 19:56:27 +1100 | [diff] [blame] | 1305 | /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */ |
| 1306 | if (vcpu->arch.mmio_vsx_copy_nums > 4) |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1307 | return EMULATE_FAIL; |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1308 | |
| 1309 | while (vcpu->arch.mmio_vsx_copy_nums) { |
| 1310 | emulated = __kvmppc_handle_load(run, vcpu, rt, bytes, |
| 1311 | is_default_endian, mmio_sign_extend); |
| 1312 | |
| 1313 | if (emulated != EMULATE_DONE) |
| 1314 | break; |
| 1315 | |
| 1316 | vcpu->arch.paddr_accessed += run->mmio.len; |
| 1317 | |
| 1318 | vcpu->arch.mmio_vsx_copy_nums--; |
| 1319 | vcpu->arch.mmio_vsx_offset++; |
| 1320 | } |
| 1321 | return emulated; |
| 1322 | } |
| 1323 | #endif /* CONFIG_VSX */ |
| 1324 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1325 | int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1326 | u64 val, unsigned int bytes, int is_default_endian) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1327 | { |
| 1328 | void *data = run->mmio.data; |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 1329 | int idx, ret; |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1330 | bool host_swabbed; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1331 | |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1332 | /* Pity C doesn't have a logical XOR operator */ |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1333 | if (kvmppc_need_byteswap(vcpu)) { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1334 | host_swabbed = is_default_endian; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1335 | } else { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1336 | host_swabbed = !is_default_endian; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 1337 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1338 | |
| 1339 | if (bytes > sizeof(run->mmio.data)) { |
| 1340 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, |
| 1341 | run->mmio.len); |
| 1342 | } |
| 1343 | |
| 1344 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; |
| 1345 | run->mmio.len = bytes; |
| 1346 | run->mmio.is_write = 1; |
| 1347 | vcpu->mmio_needed = 1; |
| 1348 | vcpu->mmio_is_write = 1; |
| 1349 | |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1350 | if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4)) |
| 1351 | val = dp_to_sp(val); |
| 1352 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1353 | /* Store the value at the lowest bytes in 'data'. */ |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1354 | if (!host_swabbed) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1355 | switch (bytes) { |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 1356 | case 8: *(u64 *)data = val; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1357 | case 4: *(u32 *)data = val; break; |
| 1358 | case 2: *(u16 *)data = val; break; |
| 1359 | case 1: *(u8 *)data = val; break; |
| 1360 | } |
| 1361 | } else { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1362 | switch (bytes) { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 1363 | case 8: *(u64 *)data = swab64(val); break; |
| 1364 | case 4: *(u32 *)data = swab32(val); break; |
| 1365 | case 2: *(u16 *)data = swab16(val); break; |
| 1366 | case 1: *(u8 *)data = val; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 1370 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 1371 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 1372 | ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr, |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 1373 | bytes, &run->mmio.data); |
| 1374 | |
| 1375 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 1376 | |
| 1377 | if (!ret) { |
Alexander Graf | 0e673fb | 2012-10-09 00:06:20 +0200 | [diff] [blame] | 1378 | vcpu->mmio_needed = 0; |
| 1379 | return EMULATE_DONE; |
| 1380 | } |
| 1381 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1382 | return EMULATE_DO_MMIO; |
| 1383 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 1384 | EXPORT_SYMBOL_GPL(kvmppc_handle_store); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1385 | |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1386 | #ifdef CONFIG_VSX |
| 1387 | static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val) |
| 1388 | { |
| 1389 | u32 dword_offset, word_offset; |
| 1390 | union kvmppc_one_reg reg; |
| 1391 | int vsx_offset = 0; |
Simon Guo | da2a32b | 2018-05-21 13:24:25 +0800 | [diff] [blame] | 1392 | int copy_type = vcpu->arch.mmio_copy_type; |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1393 | int result = 0; |
| 1394 | |
| 1395 | switch (copy_type) { |
| 1396 | case KVMPPC_VSX_COPY_DWORD: |
| 1397 | vsx_offset = |
| 1398 | kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset); |
| 1399 | |
| 1400 | if (vsx_offset == -1) { |
| 1401 | result = -1; |
| 1402 | break; |
| 1403 | } |
| 1404 | |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 1405 | if (rs < 32) { |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1406 | *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset); |
| 1407 | } else { |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 1408 | reg.vval = VCPU_VSX_VR(vcpu, rs - 32); |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1409 | *val = reg.vsxval[vsx_offset]; |
| 1410 | } |
| 1411 | break; |
| 1412 | |
| 1413 | case KVMPPC_VSX_COPY_WORD: |
| 1414 | vsx_offset = |
| 1415 | kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset); |
| 1416 | |
| 1417 | if (vsx_offset == -1) { |
| 1418 | result = -1; |
| 1419 | break; |
| 1420 | } |
| 1421 | |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 1422 | if (rs < 32) { |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1423 | dword_offset = vsx_offset / 2; |
| 1424 | word_offset = vsx_offset % 2; |
| 1425 | reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset); |
| 1426 | *val = reg.vsx32val[word_offset]; |
| 1427 | } else { |
Simon Guo | 4eeb855 | 2018-05-28 09:48:26 +0800 | [diff] [blame] | 1428 | reg.vval = VCPU_VSX_VR(vcpu, rs - 32); |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1429 | *val = reg.vsx32val[vsx_offset]; |
| 1430 | } |
| 1431 | break; |
| 1432 | |
| 1433 | default: |
| 1434 | result = -1; |
| 1435 | break; |
| 1436 | } |
| 1437 | |
| 1438 | return result; |
| 1439 | } |
| 1440 | |
| 1441 | int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 1442 | int rs, unsigned int bytes, int is_default_endian) |
| 1443 | { |
| 1444 | u64 val; |
| 1445 | enum emulation_result emulated = EMULATE_DONE; |
| 1446 | |
| 1447 | vcpu->arch.io_gpr = rs; |
| 1448 | |
Paul Mackerras | 9aa6825 | 2017-11-20 19:56:27 +1100 | [diff] [blame] | 1449 | /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */ |
| 1450 | if (vcpu->arch.mmio_vsx_copy_nums > 4) |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1451 | return EMULATE_FAIL; |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1452 | |
| 1453 | while (vcpu->arch.mmio_vsx_copy_nums) { |
| 1454 | if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1) |
| 1455 | return EMULATE_FAIL; |
| 1456 | |
| 1457 | emulated = kvmppc_handle_store(run, vcpu, |
| 1458 | val, bytes, is_default_endian); |
| 1459 | |
| 1460 | if (emulated != EMULATE_DONE) |
| 1461 | break; |
| 1462 | |
| 1463 | vcpu->arch.paddr_accessed += run->mmio.len; |
| 1464 | |
| 1465 | vcpu->arch.mmio_vsx_copy_nums--; |
| 1466 | vcpu->arch.mmio_vsx_offset++; |
| 1467 | } |
| 1468 | |
| 1469 | return emulated; |
| 1470 | } |
| 1471 | |
| 1472 | static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu, |
| 1473 | struct kvm_run *run) |
| 1474 | { |
| 1475 | enum emulation_result emulated = EMULATE_FAIL; |
| 1476 | int r; |
| 1477 | |
| 1478 | vcpu->arch.paddr_accessed += run->mmio.len; |
| 1479 | |
| 1480 | if (!vcpu->mmio_is_write) { |
| 1481 | emulated = kvmppc_handle_vsx_load(run, vcpu, vcpu->arch.io_gpr, |
| 1482 | run->mmio.len, 1, vcpu->arch.mmio_sign_extend); |
| 1483 | } else { |
| 1484 | emulated = kvmppc_handle_vsx_store(run, vcpu, |
| 1485 | vcpu->arch.io_gpr, run->mmio.len, 1); |
| 1486 | } |
| 1487 | |
| 1488 | switch (emulated) { |
| 1489 | case EMULATE_DO_MMIO: |
| 1490 | run->exit_reason = KVM_EXIT_MMIO; |
| 1491 | r = RESUME_HOST; |
| 1492 | break; |
| 1493 | case EMULATE_FAIL: |
| 1494 | pr_info("KVM: MMIO emulation failed (VSX repeat)\n"); |
| 1495 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1496 | run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 1497 | r = RESUME_HOST; |
| 1498 | break; |
| 1499 | default: |
| 1500 | r = RESUME_GUEST; |
| 1501 | break; |
| 1502 | } |
| 1503 | return r; |
| 1504 | } |
| 1505 | #endif /* CONFIG_VSX */ |
| 1506 | |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1507 | #ifdef CONFIG_ALTIVEC |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1508 | int kvmppc_handle_vmx_load(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 1509 | unsigned int rt, unsigned int bytes, int is_default_endian) |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1510 | { |
Paul Mackerras | 6df3877 | 2018-02-13 15:45:21 +1100 | [diff] [blame] | 1511 | enum emulation_result emulated = EMULATE_DONE; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1512 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1513 | if (vcpu->arch.mmio_vsx_copy_nums > 2) |
| 1514 | return EMULATE_FAIL; |
| 1515 | |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1516 | while (vcpu->arch.mmio_vmx_copy_nums) { |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1517 | emulated = __kvmppc_handle_load(run, vcpu, rt, bytes, |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1518 | is_default_endian, 0); |
| 1519 | |
| 1520 | if (emulated != EMULATE_DONE) |
| 1521 | break; |
| 1522 | |
| 1523 | vcpu->arch.paddr_accessed += run->mmio.len; |
| 1524 | vcpu->arch.mmio_vmx_copy_nums--; |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1525 | vcpu->arch.mmio_vmx_offset++; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | return emulated; |
| 1529 | } |
| 1530 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1531 | int kvmppc_get_vmx_dword(struct kvm_vcpu *vcpu, int index, u64 *val) |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1532 | { |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1533 | union kvmppc_one_reg reg; |
| 1534 | int vmx_offset = 0; |
| 1535 | int result = 0; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1536 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1537 | vmx_offset = |
| 1538 | kvmppc_get_vmx_dword_offset(vcpu, vcpu->arch.mmio_vmx_offset); |
| 1539 | |
| 1540 | if (vmx_offset == -1) |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1541 | return -1; |
| 1542 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1543 | reg.vval = VCPU_VSX_VR(vcpu, index); |
| 1544 | *val = reg.vsxval[vmx_offset]; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1545 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1546 | return result; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1547 | } |
| 1548 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1549 | int kvmppc_get_vmx_word(struct kvm_vcpu *vcpu, int index, u64 *val) |
| 1550 | { |
| 1551 | union kvmppc_one_reg reg; |
| 1552 | int vmx_offset = 0; |
| 1553 | int result = 0; |
| 1554 | |
| 1555 | vmx_offset = |
| 1556 | kvmppc_get_vmx_word_offset(vcpu, vcpu->arch.mmio_vmx_offset); |
| 1557 | |
| 1558 | if (vmx_offset == -1) |
| 1559 | return -1; |
| 1560 | |
| 1561 | reg.vval = VCPU_VSX_VR(vcpu, index); |
| 1562 | *val = reg.vsx32val[vmx_offset]; |
| 1563 | |
| 1564 | return result; |
| 1565 | } |
| 1566 | |
| 1567 | int kvmppc_get_vmx_hword(struct kvm_vcpu *vcpu, int index, u64 *val) |
| 1568 | { |
| 1569 | union kvmppc_one_reg reg; |
| 1570 | int vmx_offset = 0; |
| 1571 | int result = 0; |
| 1572 | |
| 1573 | vmx_offset = |
| 1574 | kvmppc_get_vmx_hword_offset(vcpu, vcpu->arch.mmio_vmx_offset); |
| 1575 | |
| 1576 | if (vmx_offset == -1) |
| 1577 | return -1; |
| 1578 | |
| 1579 | reg.vval = VCPU_VSX_VR(vcpu, index); |
| 1580 | *val = reg.vsx16val[vmx_offset]; |
| 1581 | |
| 1582 | return result; |
| 1583 | } |
| 1584 | |
| 1585 | int kvmppc_get_vmx_byte(struct kvm_vcpu *vcpu, int index, u64 *val) |
| 1586 | { |
| 1587 | union kvmppc_one_reg reg; |
| 1588 | int vmx_offset = 0; |
| 1589 | int result = 0; |
| 1590 | |
| 1591 | vmx_offset = |
| 1592 | kvmppc_get_vmx_byte_offset(vcpu, vcpu->arch.mmio_vmx_offset); |
| 1593 | |
| 1594 | if (vmx_offset == -1) |
| 1595 | return -1; |
| 1596 | |
| 1597 | reg.vval = VCPU_VSX_VR(vcpu, index); |
| 1598 | *val = reg.vsx8val[vmx_offset]; |
| 1599 | |
| 1600 | return result; |
| 1601 | } |
| 1602 | |
| 1603 | int kvmppc_handle_vmx_store(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 1604 | unsigned int rs, unsigned int bytes, int is_default_endian) |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1605 | { |
| 1606 | u64 val = 0; |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1607 | unsigned int index = rs & KVM_MMIO_REG_MASK; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1608 | enum emulation_result emulated = EMULATE_DONE; |
| 1609 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1610 | if (vcpu->arch.mmio_vsx_copy_nums > 2) |
| 1611 | return EMULATE_FAIL; |
| 1612 | |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1613 | vcpu->arch.io_gpr = rs; |
| 1614 | |
| 1615 | while (vcpu->arch.mmio_vmx_copy_nums) { |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1616 | switch (vcpu->arch.mmio_copy_type) { |
| 1617 | case KVMPPC_VMX_COPY_DWORD: |
| 1618 | if (kvmppc_get_vmx_dword(vcpu, index, &val) == -1) |
| 1619 | return EMULATE_FAIL; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1620 | |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1621 | break; |
| 1622 | case KVMPPC_VMX_COPY_WORD: |
| 1623 | if (kvmppc_get_vmx_word(vcpu, index, &val) == -1) |
| 1624 | return EMULATE_FAIL; |
| 1625 | break; |
| 1626 | case KVMPPC_VMX_COPY_HWORD: |
| 1627 | if (kvmppc_get_vmx_hword(vcpu, index, &val) == -1) |
| 1628 | return EMULATE_FAIL; |
| 1629 | break; |
| 1630 | case KVMPPC_VMX_COPY_BYTE: |
| 1631 | if (kvmppc_get_vmx_byte(vcpu, index, &val) == -1) |
| 1632 | return EMULATE_FAIL; |
| 1633 | break; |
| 1634 | default: |
| 1635 | return EMULATE_FAIL; |
| 1636 | } |
| 1637 | |
| 1638 | emulated = kvmppc_handle_store(run, vcpu, val, bytes, |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1639 | is_default_endian); |
| 1640 | if (emulated != EMULATE_DONE) |
| 1641 | break; |
| 1642 | |
| 1643 | vcpu->arch.paddr_accessed += run->mmio.len; |
| 1644 | vcpu->arch.mmio_vmx_copy_nums--; |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1645 | vcpu->arch.mmio_vmx_offset++; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | return emulated; |
| 1649 | } |
| 1650 | |
| 1651 | static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu, |
| 1652 | struct kvm_run *run) |
| 1653 | { |
| 1654 | enum emulation_result emulated = EMULATE_FAIL; |
| 1655 | int r; |
| 1656 | |
| 1657 | vcpu->arch.paddr_accessed += run->mmio.len; |
| 1658 | |
| 1659 | if (!vcpu->mmio_is_write) { |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1660 | emulated = kvmppc_handle_vmx_load(run, vcpu, |
| 1661 | vcpu->arch.io_gpr, run->mmio.len, 1); |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1662 | } else { |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1663 | emulated = kvmppc_handle_vmx_store(run, vcpu, |
| 1664 | vcpu->arch.io_gpr, run->mmio.len, 1); |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | switch (emulated) { |
| 1668 | case EMULATE_DO_MMIO: |
| 1669 | run->exit_reason = KVM_EXIT_MMIO; |
| 1670 | r = RESUME_HOST; |
| 1671 | break; |
| 1672 | case EMULATE_FAIL: |
| 1673 | pr_info("KVM: MMIO emulation failed (VMX repeat)\n"); |
| 1674 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1675 | run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 1676 | r = RESUME_HOST; |
| 1677 | break; |
| 1678 | default: |
| 1679 | r = RESUME_GUEST; |
| 1680 | break; |
| 1681 | } |
| 1682 | return r; |
| 1683 | } |
| 1684 | #endif /* CONFIG_ALTIVEC */ |
| 1685 | |
Mihai Caraman | 8a41ea5 | 2014-08-20 16:36:24 +0300 | [diff] [blame] | 1686 | int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 1687 | { |
| 1688 | int r = 0; |
| 1689 | union kvmppc_one_reg val; |
| 1690 | int size; |
| 1691 | |
| 1692 | size = one_reg_size(reg->id); |
| 1693 | if (size > sizeof(val)) |
| 1694 | return -EINVAL; |
| 1695 | |
| 1696 | r = kvmppc_get_one_reg(vcpu, reg->id, &val); |
| 1697 | if (r == -EINVAL) { |
| 1698 | r = 0; |
| 1699 | switch (reg->id) { |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1700 | #ifdef CONFIG_ALTIVEC |
| 1701 | case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31: |
| 1702 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1703 | r = -ENXIO; |
| 1704 | break; |
| 1705 | } |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1706 | val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0]; |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1707 | break; |
| 1708 | case KVM_REG_PPC_VSCR: |
| 1709 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1710 | r = -ENXIO; |
| 1711 | break; |
| 1712 | } |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1713 | val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]); |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1714 | break; |
| 1715 | case KVM_REG_PPC_VRSAVE: |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1716 | val = get_reg_val(reg->id, vcpu->arch.vrsave); |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1717 | break; |
| 1718 | #endif /* CONFIG_ALTIVEC */ |
Mihai Caraman | 8a41ea5 | 2014-08-20 16:36:24 +0300 | [diff] [blame] | 1719 | default: |
| 1720 | r = -EINVAL; |
| 1721 | break; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | if (r) |
| 1726 | return r; |
| 1727 | |
| 1728 | if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size)) |
| 1729 | r = -EFAULT; |
| 1730 | |
| 1731 | return r; |
| 1732 | } |
| 1733 | |
| 1734 | int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 1735 | { |
| 1736 | int r; |
| 1737 | union kvmppc_one_reg val; |
| 1738 | int size; |
| 1739 | |
| 1740 | size = one_reg_size(reg->id); |
| 1741 | if (size > sizeof(val)) |
| 1742 | return -EINVAL; |
| 1743 | |
| 1744 | if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size)) |
| 1745 | return -EFAULT; |
| 1746 | |
| 1747 | r = kvmppc_set_one_reg(vcpu, reg->id, &val); |
| 1748 | if (r == -EINVAL) { |
| 1749 | r = 0; |
| 1750 | switch (reg->id) { |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1751 | #ifdef CONFIG_ALTIVEC |
| 1752 | case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31: |
| 1753 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1754 | r = -ENXIO; |
| 1755 | break; |
| 1756 | } |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1757 | vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval; |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1758 | break; |
| 1759 | case KVM_REG_PPC_VSCR: |
| 1760 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1761 | r = -ENXIO; |
| 1762 | break; |
| 1763 | } |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1764 | vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val); |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1765 | break; |
| 1766 | case KVM_REG_PPC_VRSAVE: |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1767 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1768 | r = -ENXIO; |
| 1769 | break; |
| 1770 | } |
| 1771 | vcpu->arch.vrsave = set_reg_val(reg->id, val); |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1772 | break; |
| 1773 | #endif /* CONFIG_ALTIVEC */ |
Mihai Caraman | 8a41ea5 | 2014-08-20 16:36:24 +0300 | [diff] [blame] | 1774 | default: |
| 1775 | r = -EINVAL; |
| 1776 | break; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | return r; |
| 1781 | } |
| 1782 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1783 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 1784 | { |
| 1785 | int r; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1786 | |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 1787 | vcpu_load(vcpu); |
| 1788 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1789 | if (vcpu->mmio_needed) { |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1790 | vcpu->mmio_needed = 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1791 | if (!vcpu->mmio_is_write) |
| 1792 | kvmppc_complete_mmio_load(vcpu, run); |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1793 | #ifdef CONFIG_VSX |
| 1794 | if (vcpu->arch.mmio_vsx_copy_nums > 0) { |
| 1795 | vcpu->arch.mmio_vsx_copy_nums--; |
| 1796 | vcpu->arch.mmio_vsx_offset++; |
| 1797 | } |
| 1798 | |
| 1799 | if (vcpu->arch.mmio_vsx_copy_nums > 0) { |
| 1800 | r = kvmppc_emulate_mmio_vsx_loadstore(vcpu, run); |
| 1801 | if (r == RESUME_HOST) { |
| 1802 | vcpu->mmio_needed = 1; |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 1803 | goto out; |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | #endif |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1807 | #ifdef CONFIG_ALTIVEC |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1808 | if (vcpu->arch.mmio_vmx_copy_nums > 0) { |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1809 | vcpu->arch.mmio_vmx_copy_nums--; |
Simon Guo | acc9eb9 | 2018-05-21 13:24:26 +0800 | [diff] [blame] | 1810 | vcpu->arch.mmio_vmx_offset++; |
| 1811 | } |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1812 | |
| 1813 | if (vcpu->arch.mmio_vmx_copy_nums > 0) { |
| 1814 | r = kvmppc_emulate_mmio_vmx_loadstore(vcpu, run); |
| 1815 | if (r == RESUME_HOST) { |
| 1816 | vcpu->mmio_needed = 1; |
Radim Krčmář | 1ab03c0 | 2018-02-09 21:36:57 +0100 | [diff] [blame] | 1817 | goto out; |
Jose Ricardo Ziviani | 09f9849 | 2018-02-03 18:24:26 -0200 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | #endif |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 1821 | } else if (vcpu->arch.osi_needed) { |
| 1822 | u64 *gprs = run->osi.gprs; |
| 1823 | int i; |
| 1824 | |
| 1825 | for (i = 0; i < 32; i++) |
| 1826 | kvmppc_set_gpr(vcpu, i, gprs[i]); |
| 1827 | vcpu->arch.osi_needed = 0; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1828 | } else if (vcpu->arch.hcall_needed) { |
| 1829 | int i; |
| 1830 | |
| 1831 | kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret); |
| 1832 | for (i = 0; i < 9; ++i) |
| 1833 | kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]); |
| 1834 | vcpu->arch.hcall_needed = 0; |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 1835 | #ifdef CONFIG_BOOKE |
| 1836 | } else if (vcpu->arch.epr_needed) { |
| 1837 | kvmppc_set_epr(vcpu, run->epr.epr); |
| 1838 | vcpu->arch.epr_needed = 0; |
| 1839 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1840 | } |
| 1841 | |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 1842 | kvm_sigset_activate(vcpu); |
Bin Lu | 6f63e81 | 2017-02-21 21:12:36 +0800 | [diff] [blame] | 1843 | |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 1844 | if (run->immediate_exit) |
| 1845 | r = -EINTR; |
| 1846 | else |
| 1847 | r = kvmppc_vcpu_run(run, vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1848 | |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 1849 | kvm_sigset_deactivate(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1850 | |
Paul Mackerras | c662f77 | 2018-02-13 15:16:01 +1100 | [diff] [blame] | 1851 | #ifdef CONFIG_ALTIVEC |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 1852 | out: |
Paul Mackerras | c662f77 | 2018-02-13 15:16:01 +1100 | [diff] [blame] | 1853 | #endif |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 1854 | vcpu_put(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1855 | return r; |
| 1856 | } |
| 1857 | |
| 1858 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) |
| 1859 | { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1860 | if (irq->irq == KVM_INTERRUPT_UNSET) { |
Paul Mackerras | 4fe27d2 | 2013-02-14 14:00:25 +0000 | [diff] [blame] | 1861 | kvmppc_core_dequeue_external(vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1862 | return 0; |
| 1863 | } |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 1864 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1865 | kvmppc_core_queue_external(vcpu, irq); |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 1866 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 1867 | kvm_vcpu_kick(vcpu); |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 1868 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1869 | return 0; |
| 1870 | } |
| 1871 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 1872 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
| 1873 | struct kvm_enable_cap *cap) |
| 1874 | { |
| 1875 | int r; |
| 1876 | |
| 1877 | if (cap->flags) |
| 1878 | return -EINVAL; |
| 1879 | |
| 1880 | switch (cap->cap) { |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 1881 | case KVM_CAP_PPC_OSI: |
| 1882 | r = 0; |
| 1883 | vcpu->arch.osi_enabled = true; |
| 1884 | break; |
Alexander Graf | 930b412 | 2011-08-08 17:29:42 +0200 | [diff] [blame] | 1885 | case KVM_CAP_PPC_PAPR: |
| 1886 | r = 0; |
| 1887 | vcpu->arch.papr_enabled = true; |
| 1888 | break; |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 1889 | case KVM_CAP_PPC_EPR: |
| 1890 | r = 0; |
Scott Wood | 5df554ad | 2013-04-12 14:08:46 +0000 | [diff] [blame] | 1891 | if (cap->args[0]) |
| 1892 | vcpu->arch.epr_flags |= KVMPPC_EPR_USER; |
| 1893 | else |
| 1894 | vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER; |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 1895 | break; |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 1896 | #ifdef CONFIG_BOOKE |
| 1897 | case KVM_CAP_PPC_BOOKE_WATCHDOG: |
| 1898 | r = 0; |
| 1899 | vcpu->arch.watchdog_enabled = true; |
| 1900 | break; |
| 1901 | #endif |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 1902 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1903 | case KVM_CAP_SW_TLB: { |
| 1904 | struct kvm_config_tlb cfg; |
| 1905 | void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0]; |
| 1906 | |
| 1907 | r = -EFAULT; |
| 1908 | if (copy_from_user(&cfg, user_ptr, sizeof(cfg))) |
| 1909 | break; |
| 1910 | |
| 1911 | r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg); |
| 1912 | break; |
| 1913 | } |
| 1914 | #endif |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1915 | #ifdef CONFIG_KVM_MPIC |
| 1916 | case KVM_CAP_IRQ_MPIC: { |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1917 | struct fd f; |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1918 | struct kvm_device *dev; |
| 1919 | |
| 1920 | r = -EBADF; |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1921 | f = fdget(cap->args[0]); |
| 1922 | if (!f.file) |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | |
| 1925 | r = -EPERM; |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1926 | dev = kvm_device_from_filp(f.file); |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1927 | if (dev) |
| 1928 | r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]); |
| 1929 | |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1930 | fdput(f); |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1931 | break; |
| 1932 | } |
| 1933 | #endif |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1934 | #ifdef CONFIG_KVM_XICS |
| 1935 | case KVM_CAP_IRQ_XICS: { |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1936 | struct fd f; |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1937 | struct kvm_device *dev; |
| 1938 | |
| 1939 | r = -EBADF; |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1940 | f = fdget(cap->args[0]); |
| 1941 | if (!f.file) |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1942 | break; |
| 1943 | |
| 1944 | r = -EPERM; |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1945 | dev = kvm_device_from_filp(f.file); |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 1946 | if (dev) { |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 1947 | if (xics_on_xive()) |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 1948 | r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]); |
| 1949 | else |
| 1950 | r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]); |
| 1951 | } |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1952 | |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1953 | fdput(f); |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1954 | break; |
| 1955 | } |
| 1956 | #endif /* CONFIG_KVM_XICS */ |
Cédric Le Goater | eacc56b | 2019-04-18 12:39:28 +0200 | [diff] [blame] | 1957 | #ifdef CONFIG_KVM_XIVE |
| 1958 | case KVM_CAP_PPC_IRQ_XIVE: { |
| 1959 | struct fd f; |
| 1960 | struct kvm_device *dev; |
| 1961 | |
| 1962 | r = -EBADF; |
| 1963 | f = fdget(cap->args[0]); |
| 1964 | if (!f.file) |
| 1965 | break; |
| 1966 | |
| 1967 | r = -ENXIO; |
| 1968 | if (!xive_enabled()) |
| 1969 | break; |
| 1970 | |
| 1971 | r = -EPERM; |
| 1972 | dev = kvm_device_from_filp(f.file); |
| 1973 | if (dev) |
| 1974 | r = kvmppc_xive_native_connect_vcpu(dev, vcpu, |
| 1975 | cap->args[1]); |
| 1976 | |
| 1977 | fdput(f); |
| 1978 | break; |
| 1979 | } |
| 1980 | #endif /* CONFIG_KVM_XIVE */ |
Aravinda Prasad | 134764e | 2017-05-11 16:32:48 +0530 | [diff] [blame] | 1981 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
| 1982 | case KVM_CAP_PPC_FWNMI: |
| 1983 | r = -EINVAL; |
| 1984 | if (!is_kvmppc_hv_enabled(vcpu->kvm)) |
| 1985 | break; |
| 1986 | r = 0; |
| 1987 | vcpu->kvm->arch.fwnmi_enabled = true; |
| 1988 | break; |
| 1989 | #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 1990 | default: |
| 1991 | r = -EINVAL; |
| 1992 | break; |
| 1993 | } |
| 1994 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1995 | if (!r) |
| 1996 | r = kvmppc_sanity_check(vcpu); |
| 1997 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 1998 | return r; |
| 1999 | } |
| 2000 | |
Paul Mackerras | 34a75b0 | 2016-08-10 11:27:27 +1000 | [diff] [blame] | 2001 | bool kvm_arch_intc_initialized(struct kvm *kvm) |
| 2002 | { |
| 2003 | #ifdef CONFIG_KVM_MPIC |
| 2004 | if (kvm->arch.mpic) |
| 2005 | return true; |
| 2006 | #endif |
| 2007 | #ifdef CONFIG_KVM_XICS |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 2008 | if (kvm->arch.xics || kvm->arch.xive) |
Paul Mackerras | 34a75b0 | 2016-08-10 11:27:27 +1000 | [diff] [blame] | 2009 | return true; |
| 2010 | #endif |
| 2011 | return false; |
| 2012 | } |
| 2013 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2014 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 2015 | struct kvm_mp_state *mp_state) |
| 2016 | { |
| 2017 | return -EINVAL; |
| 2018 | } |
| 2019 | |
| 2020 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 2021 | struct kvm_mp_state *mp_state) |
| 2022 | { |
| 2023 | return -EINVAL; |
| 2024 | } |
| 2025 | |
Paolo Bonzini | 5cb0944 | 2017-12-12 17:41:34 +0100 | [diff] [blame] | 2026 | long kvm_arch_vcpu_async_ioctl(struct file *filp, |
| 2027 | unsigned int ioctl, unsigned long arg) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2028 | { |
| 2029 | struct kvm_vcpu *vcpu = filp->private_data; |
| 2030 | void __user *argp = (void __user *)arg; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2031 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 2032 | if (ioctl == KVM_INTERRUPT) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2033 | struct kvm_interrupt irq; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2034 | if (copy_from_user(&irq, argp, sizeof(irq))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 2035 | return -EFAULT; |
| 2036 | return kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2037 | } |
Paolo Bonzini | 5cb0944 | 2017-12-12 17:41:34 +0100 | [diff] [blame] | 2038 | return -ENOIOCTLCMD; |
| 2039 | } |
| 2040 | |
| 2041 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 2042 | unsigned int ioctl, unsigned long arg) |
| 2043 | { |
| 2044 | struct kvm_vcpu *vcpu = filp->private_data; |
| 2045 | void __user *argp = (void __user *)arg; |
| 2046 | long r; |
Avi Kivity | 19483d1 | 2010-05-13 12:30:43 +0300 | [diff] [blame] | 2047 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 2048 | switch (ioctl) { |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 2049 | case KVM_ENABLE_CAP: |
| 2050 | { |
| 2051 | struct kvm_enable_cap cap; |
| 2052 | r = -EFAULT; |
Simon Guo | b3cebfe | 2018-05-23 15:02:09 +0800 | [diff] [blame] | 2053 | vcpu_load(vcpu); |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 2054 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 2055 | goto out; |
| 2056 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); |
Simon Guo | b3cebfe | 2018-05-23 15:02:09 +0800 | [diff] [blame] | 2057 | vcpu_put(vcpu); |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 2058 | break; |
| 2059 | } |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 2060 | |
Alexander Graf | e24ed81 | 2011-09-14 10:02:41 +0200 | [diff] [blame] | 2061 | case KVM_SET_ONE_REG: |
| 2062 | case KVM_GET_ONE_REG: |
| 2063 | { |
| 2064 | struct kvm_one_reg reg; |
| 2065 | r = -EFAULT; |
| 2066 | if (copy_from_user(®, argp, sizeof(reg))) |
| 2067 | goto out; |
| 2068 | if (ioctl == KVM_SET_ONE_REG) |
| 2069 | r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®); |
| 2070 | else |
| 2071 | r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®); |
| 2072 | break; |
| 2073 | } |
| 2074 | |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 2075 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 2076 | case KVM_DIRTY_TLB: { |
| 2077 | struct kvm_dirty_tlb dirty; |
| 2078 | r = -EFAULT; |
Simon Guo | b3cebfe | 2018-05-23 15:02:09 +0800 | [diff] [blame] | 2079 | vcpu_load(vcpu); |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 2080 | if (copy_from_user(&dirty, argp, sizeof(dirty))) |
| 2081 | goto out; |
| 2082 | r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty); |
Simon Guo | b3cebfe | 2018-05-23 15:02:09 +0800 | [diff] [blame] | 2083 | vcpu_put(vcpu); |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 2084 | break; |
| 2085 | } |
| 2086 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2087 | default: |
| 2088 | r = -EINVAL; |
| 2089 | } |
| 2090 | |
| 2091 | out: |
| 2092 | return r; |
| 2093 | } |
| 2094 | |
Souptick Joarder | 1499fa8 | 2018-04-19 00:49:58 +0530 | [diff] [blame] | 2095 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
Carsten Otte | 5b1c149 | 2012-01-04 10:25:23 +0100 | [diff] [blame] | 2096 | { |
| 2097 | return VM_FAULT_SIGBUS; |
| 2098 | } |
| 2099 | |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 2100 | static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo) |
| 2101 | { |
Stuart Yoder | 784bafa | 2012-07-03 05:48:51 +0000 | [diff] [blame] | 2102 | u32 inst_nop = 0x60000000; |
| 2103 | #ifdef CONFIG_KVM_BOOKE_HV |
| 2104 | u32 inst_sc1 = 0x44000022; |
Alexander Graf | 2743103 | 2014-04-24 13:39:16 +0200 | [diff] [blame] | 2105 | pvinfo->hcall[0] = cpu_to_be32(inst_sc1); |
| 2106 | pvinfo->hcall[1] = cpu_to_be32(inst_nop); |
| 2107 | pvinfo->hcall[2] = cpu_to_be32(inst_nop); |
| 2108 | pvinfo->hcall[3] = cpu_to_be32(inst_nop); |
Stuart Yoder | 784bafa | 2012-07-03 05:48:51 +0000 | [diff] [blame] | 2109 | #else |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 2110 | u32 inst_lis = 0x3c000000; |
| 2111 | u32 inst_ori = 0x60000000; |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 2112 | u32 inst_sc = 0x44000002; |
| 2113 | u32 inst_imm_mask = 0xffff; |
| 2114 | |
| 2115 | /* |
| 2116 | * The hypercall to get into KVM from within guest context is as |
| 2117 | * follows: |
| 2118 | * |
| 2119 | * lis r0, r0, KVM_SC_MAGIC_R0@h |
| 2120 | * ori r0, KVM_SC_MAGIC_R0@l |
| 2121 | * sc |
| 2122 | * nop |
| 2123 | */ |
Alexander Graf | 2743103 | 2014-04-24 13:39:16 +0200 | [diff] [blame] | 2124 | pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask)); |
| 2125 | pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask)); |
| 2126 | pvinfo->hcall[2] = cpu_to_be32(inst_sc); |
| 2127 | pvinfo->hcall[3] = cpu_to_be32(inst_nop); |
Stuart Yoder | 784bafa | 2012-07-03 05:48:51 +0000 | [diff] [blame] | 2128 | #endif |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 2129 | |
Liu Yu-B13201 | 9202e07 | 2012-07-03 05:48:52 +0000 | [diff] [blame] | 2130 | pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE; |
| 2131 | |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 2132 | return 0; |
| 2133 | } |
| 2134 | |
Alexander Graf | 5efdb4b | 2013-04-17 00:37:57 +0200 | [diff] [blame] | 2135 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, |
| 2136 | bool line_status) |
| 2137 | { |
| 2138 | if (!irqchip_in_kernel(kvm)) |
| 2139 | return -ENXIO; |
| 2140 | |
| 2141 | irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
| 2142 | irq_event->irq, irq_event->level, |
| 2143 | line_status); |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2147 | |
Paolo Bonzini | e5d83c7 | 2017-02-16 10:40:56 +0100 | [diff] [blame] | 2148 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
| 2149 | struct kvm_enable_cap *cap) |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2150 | { |
| 2151 | int r; |
| 2152 | |
| 2153 | if (cap->flags) |
| 2154 | return -EINVAL; |
| 2155 | |
| 2156 | switch (cap->cap) { |
| 2157 | #ifdef CONFIG_KVM_BOOK3S_64_HANDLER |
| 2158 | case KVM_CAP_PPC_ENABLE_HCALL: { |
| 2159 | unsigned long hcall = cap->args[0]; |
| 2160 | |
| 2161 | r = -EINVAL; |
| 2162 | if (hcall > MAX_HCALL_OPCODE || (hcall & 3) || |
| 2163 | cap->args[1] > 1) |
| 2164 | break; |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 2165 | if (!kvmppc_book3s_hcall_implemented(kvm, hcall)) |
| 2166 | break; |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2167 | if (cap->args[1]) |
| 2168 | set_bit(hcall / 4, kvm->arch.enabled_hcalls); |
| 2169 | else |
| 2170 | clear_bit(hcall / 4, kvm->arch.enabled_hcalls); |
| 2171 | r = 0; |
| 2172 | break; |
| 2173 | } |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2174 | case KVM_CAP_PPC_SMT: { |
| 2175 | unsigned long mode = cap->args[0]; |
| 2176 | unsigned long flags = cap->args[1]; |
| 2177 | |
| 2178 | r = -EINVAL; |
| 2179 | if (kvm->arch.kvm_ops->set_smt_mode) |
| 2180 | r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags); |
| 2181 | break; |
| 2182 | } |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 2183 | |
| 2184 | case KVM_CAP_PPC_NESTED_HV: |
| 2185 | r = -EINVAL; |
| 2186 | if (!is_kvmppc_hv_enabled(kvm) || |
| 2187 | !kvm->arch.kvm_ops->enable_nested) |
| 2188 | break; |
| 2189 | r = kvm->arch.kvm_ops->enable_nested(kvm); |
| 2190 | break; |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2191 | #endif |
| 2192 | default: |
| 2193 | r = -EINVAL; |
| 2194 | break; |
| 2195 | } |
| 2196 | |
| 2197 | return r; |
| 2198 | } |
| 2199 | |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 2200 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 2201 | /* |
| 2202 | * These functions check whether the underlying hardware is safe |
| 2203 | * against attacks based on observing the effects of speculatively |
| 2204 | * executed instructions, and whether it supplies instructions for |
| 2205 | * use in workarounds. The information comes from firmware, either |
| 2206 | * via the device tree on powernv platforms or from an hcall on |
| 2207 | * pseries platforms. |
| 2208 | */ |
| 2209 | #ifdef CONFIG_PPC_PSERIES |
| 2210 | static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp) |
| 2211 | { |
| 2212 | struct h_cpu_char_result c; |
| 2213 | unsigned long rc; |
| 2214 | |
| 2215 | if (!machine_is(pseries)) |
| 2216 | return -ENOTTY; |
| 2217 | |
| 2218 | rc = plpar_get_cpu_characteristics(&c); |
| 2219 | if (rc == H_SUCCESS) { |
| 2220 | cp->character = c.character; |
| 2221 | cp->behaviour = c.behaviour; |
| 2222 | cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 | |
| 2223 | KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED | |
| 2224 | KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 | |
| 2225 | KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 | |
| 2226 | KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV | |
| 2227 | KVM_PPC_CPU_CHAR_BR_HINT_HONOURED | |
| 2228 | KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF | |
Suraj Jitindar Singh | 2b57ecd | 2019-03-01 14:25:16 +1100 | [diff] [blame] | 2229 | KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS | |
| 2230 | KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST; |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 2231 | cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY | |
| 2232 | KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR | |
Suraj Jitindar Singh | 2b57ecd | 2019-03-01 14:25:16 +1100 | [diff] [blame] | 2233 | KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR | |
| 2234 | KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE; |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 2235 | } |
| 2236 | return 0; |
| 2237 | } |
| 2238 | #else |
| 2239 | static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp) |
| 2240 | { |
| 2241 | return -ENOTTY; |
| 2242 | } |
| 2243 | #endif |
| 2244 | |
| 2245 | static inline bool have_fw_feat(struct device_node *fw_features, |
| 2246 | const char *state, const char *name) |
| 2247 | { |
| 2248 | struct device_node *np; |
| 2249 | bool r = false; |
| 2250 | |
| 2251 | np = of_get_child_by_name(fw_features, name); |
| 2252 | if (np) { |
| 2253 | r = of_property_read_bool(np, state); |
| 2254 | of_node_put(np); |
| 2255 | } |
| 2256 | return r; |
| 2257 | } |
| 2258 | |
| 2259 | static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp) |
| 2260 | { |
| 2261 | struct device_node *np, *fw_features; |
| 2262 | int r; |
| 2263 | |
| 2264 | memset(cp, 0, sizeof(*cp)); |
| 2265 | r = pseries_get_cpu_char(cp); |
| 2266 | if (r != -ENOTTY) |
| 2267 | return r; |
| 2268 | |
| 2269 | np = of_find_node_by_name(NULL, "ibm,opal"); |
| 2270 | if (np) { |
| 2271 | fw_features = of_get_child_by_name(np, "fw-features"); |
| 2272 | of_node_put(np); |
| 2273 | if (!fw_features) |
| 2274 | return 0; |
| 2275 | if (have_fw_feat(fw_features, "enabled", |
| 2276 | "inst-spec-barrier-ori31,31,0")) |
| 2277 | cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31; |
| 2278 | if (have_fw_feat(fw_features, "enabled", |
| 2279 | "fw-bcctrl-serialized")) |
| 2280 | cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED; |
| 2281 | if (have_fw_feat(fw_features, "enabled", |
| 2282 | "inst-l1d-flush-ori30,30,0")) |
| 2283 | cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30; |
| 2284 | if (have_fw_feat(fw_features, "enabled", |
| 2285 | "inst-l1d-flush-trig2")) |
| 2286 | cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2; |
| 2287 | if (have_fw_feat(fw_features, "enabled", |
| 2288 | "fw-l1d-thread-split")) |
| 2289 | cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV; |
| 2290 | if (have_fw_feat(fw_features, "enabled", |
| 2291 | "fw-count-cache-disabled")) |
| 2292 | cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS; |
Suraj Jitindar Singh | 2b57ecd | 2019-03-01 14:25:16 +1100 | [diff] [blame] | 2293 | if (have_fw_feat(fw_features, "enabled", |
| 2294 | "fw-count-cache-flush-bcctr2,0,0")) |
| 2295 | cp->character |= KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST; |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 2296 | cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 | |
| 2297 | KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED | |
| 2298 | KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 | |
| 2299 | KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 | |
| 2300 | KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV | |
Suraj Jitindar Singh | 2b57ecd | 2019-03-01 14:25:16 +1100 | [diff] [blame] | 2301 | KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS | |
| 2302 | KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST; |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 2303 | |
| 2304 | if (have_fw_feat(fw_features, "enabled", |
| 2305 | "speculation-policy-favor-security")) |
| 2306 | cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY; |
| 2307 | if (!have_fw_feat(fw_features, "disabled", |
| 2308 | "needs-l1d-flush-msr-pr-0-to-1")) |
| 2309 | cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR; |
| 2310 | if (!have_fw_feat(fw_features, "disabled", |
| 2311 | "needs-spec-barrier-for-bound-checks")) |
| 2312 | cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR; |
Suraj Jitindar Singh | 2b57ecd | 2019-03-01 14:25:16 +1100 | [diff] [blame] | 2313 | if (have_fw_feat(fw_features, "enabled", |
| 2314 | "needs-count-cache-flush-on-context-switch")) |
| 2315 | cp->behaviour |= KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE; |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 2316 | cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY | |
| 2317 | KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR | |
Suraj Jitindar Singh | 2b57ecd | 2019-03-01 14:25:16 +1100 | [diff] [blame] | 2318 | KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR | |
| 2319 | KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE; |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 2320 | |
| 2321 | of_node_put(fw_features); |
| 2322 | } |
| 2323 | |
| 2324 | return 0; |
| 2325 | } |
| 2326 | #endif |
| 2327 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2328 | long kvm_arch_vm_ioctl(struct file *filp, |
| 2329 | unsigned int ioctl, unsigned long arg) |
| 2330 | { |
Scott Wood | 5df554ad | 2013-04-12 14:08:46 +0000 | [diff] [blame] | 2331 | struct kvm *kvm __maybe_unused = filp->private_data; |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 2332 | void __user *argp = (void __user *)arg; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2333 | long r; |
| 2334 | |
| 2335 | switch (ioctl) { |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 2336 | case KVM_PPC_GET_PVINFO: { |
| 2337 | struct kvm_ppc_pvinfo pvinfo; |
Vasiliy Kulikov | d8cdddc | 2010-10-30 13:04:24 +0400 | [diff] [blame] | 2338 | memset(&pvinfo, 0, sizeof(pvinfo)); |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 2339 | r = kvm_vm_ioctl_get_pvinfo(&pvinfo); |
| 2340 | if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) { |
| 2341 | r = -EFAULT; |
| 2342 | goto out; |
| 2343 | } |
| 2344 | |
| 2345 | break; |
| 2346 | } |
Paul Mackerras | 76d837a | 2017-05-11 14:31:59 +1000 | [diff] [blame] | 2347 | #ifdef CONFIG_SPAPR_TCE_IOMMU |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 2348 | case KVM_CREATE_SPAPR_TCE_64: { |
| 2349 | struct kvm_create_spapr_tce_64 create_tce_64; |
| 2350 | |
| 2351 | r = -EFAULT; |
| 2352 | if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64))) |
| 2353 | goto out; |
| 2354 | if (create_tce_64.flags) { |
| 2355 | r = -EINVAL; |
| 2356 | goto out; |
| 2357 | } |
| 2358 | r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64); |
| 2359 | goto out; |
| 2360 | } |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 2361 | case KVM_CREATE_SPAPR_TCE: { |
| 2362 | struct kvm_create_spapr_tce create_tce; |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 2363 | struct kvm_create_spapr_tce_64 create_tce_64; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 2364 | |
| 2365 | r = -EFAULT; |
| 2366 | if (copy_from_user(&create_tce, argp, sizeof(create_tce))) |
| 2367 | goto out; |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 2368 | |
| 2369 | create_tce_64.liobn = create_tce.liobn; |
| 2370 | create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K; |
| 2371 | create_tce_64.offset = 0; |
| 2372 | create_tce_64.size = create_tce.window_size >> |
| 2373 | IOMMU_PAGE_SHIFT_4K; |
| 2374 | create_tce_64.flags = 0; |
| 2375 | r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64); |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 2376 | goto out; |
| 2377 | } |
Paul Mackerras | 76d837a | 2017-05-11 14:31:59 +1000 | [diff] [blame] | 2378 | #endif |
| 2379 | #ifdef CONFIG_PPC_BOOK3S_64 |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2380 | case KVM_PPC_GET_SMMU_INFO: { |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2381 | struct kvm_ppc_smmu_info info; |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2382 | struct kvm *kvm = filp->private_data; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2383 | |
| 2384 | memset(&info, 0, sizeof(info)); |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2385 | r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info); |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2386 | if (r >= 0 && copy_to_user(argp, &info, sizeof(info))) |
| 2387 | r = -EFAULT; |
| 2388 | break; |
| 2389 | } |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 2390 | case KVM_PPC_RTAS_DEFINE_TOKEN: { |
| 2391 | struct kvm *kvm = filp->private_data; |
| 2392 | |
| 2393 | r = kvm_vm_ioctl_rtas_define_token(kvm, argp); |
| 2394 | break; |
| 2395 | } |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 2396 | case KVM_PPC_CONFIGURE_V3_MMU: { |
| 2397 | struct kvm *kvm = filp->private_data; |
| 2398 | struct kvm_ppc_mmuv3_cfg cfg; |
| 2399 | |
| 2400 | r = -EINVAL; |
| 2401 | if (!kvm->arch.kvm_ops->configure_mmu) |
| 2402 | goto out; |
| 2403 | r = -EFAULT; |
| 2404 | if (copy_from_user(&cfg, argp, sizeof(cfg))) |
| 2405 | goto out; |
| 2406 | r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg); |
| 2407 | break; |
| 2408 | } |
| 2409 | case KVM_PPC_GET_RMMU_INFO: { |
| 2410 | struct kvm *kvm = filp->private_data; |
| 2411 | struct kvm_ppc_rmmu_info info; |
| 2412 | |
| 2413 | r = -EINVAL; |
| 2414 | if (!kvm->arch.kvm_ops->get_rmmu_info) |
| 2415 | goto out; |
| 2416 | r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info); |
| 2417 | if (r >= 0 && copy_to_user(argp, &info, sizeof(info))) |
| 2418 | r = -EFAULT; |
| 2419 | break; |
| 2420 | } |
Paul Mackerras | 3214d01 | 2018-01-15 16:06:47 +1100 | [diff] [blame] | 2421 | case KVM_PPC_GET_CPU_CHAR: { |
| 2422 | struct kvm_ppc_cpu_char cpuchar; |
| 2423 | |
| 2424 | r = kvmppc_get_cpu_char(&cpuchar); |
| 2425 | if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar))) |
| 2426 | r = -EFAULT; |
| 2427 | break; |
| 2428 | } |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2429 | default: { |
| 2430 | struct kvm *kvm = filp->private_data; |
| 2431 | r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg); |
| 2432 | } |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2433 | #else /* CONFIG_PPC_BOOK3S_64 */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2434 | default: |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 2435 | r = -ENOTTY; |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2436 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2437 | } |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 2438 | out: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2439 | return r; |
| 2440 | } |
| 2441 | |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 2442 | static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)]; |
| 2443 | static unsigned long nr_lpids; |
| 2444 | |
| 2445 | long kvmppc_alloc_lpid(void) |
| 2446 | { |
| 2447 | long lpid; |
| 2448 | |
| 2449 | do { |
| 2450 | lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS); |
| 2451 | if (lpid >= nr_lpids) { |
| 2452 | pr_err("%s: No LPIDs free\n", __func__); |
| 2453 | return -ENOMEM; |
| 2454 | } |
| 2455 | } while (test_and_set_bit(lpid, lpid_inuse)); |
| 2456 | |
| 2457 | return lpid; |
| 2458 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 2459 | EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid); |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 2460 | |
| 2461 | void kvmppc_claim_lpid(long lpid) |
| 2462 | { |
| 2463 | set_bit(lpid, lpid_inuse); |
| 2464 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 2465 | EXPORT_SYMBOL_GPL(kvmppc_claim_lpid); |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 2466 | |
| 2467 | void kvmppc_free_lpid(long lpid) |
| 2468 | { |
| 2469 | clear_bit(lpid, lpid_inuse); |
| 2470 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 2471 | EXPORT_SYMBOL_GPL(kvmppc_free_lpid); |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 2472 | |
| 2473 | void kvmppc_init_lpid(unsigned long nr_lpids_param) |
| 2474 | { |
| 2475 | nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param); |
| 2476 | memset(lpid_inuse, 0, sizeof(lpid_inuse)); |
| 2477 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 2478 | EXPORT_SYMBOL_GPL(kvmppc_init_lpid); |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 2479 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 2480 | int kvm_arch_init(void *opaque) |
| 2481 | { |
| 2482 | return 0; |
| 2483 | } |
| 2484 | |
Paolo Bonzini | 478d6686 | 2014-08-05 11:29:07 +0200 | [diff] [blame] | 2485 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr); |