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> |
Hollis Blanchard | 83aae4a | 2008-07-25 13:54:52 -0500 | [diff] [blame] | 36 | #include <asm/tlbflush.h> |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 37 | #include <asm/cputhreads.h> |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame] | 38 | #include <asm/irqflags.h> |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 39 | #include <asm/iommu.h> |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 40 | #include "timing.h" |
Alexander Graf | 5efdb4b | 2013-04-17 00:37:57 +0200 | [diff] [blame] | 41 | #include "irq.h" |
Paul Mackerras | fad7b9b | 2008-12-23 14:57:26 +1100 | [diff] [blame] | 42 | #include "../mm/mmu_decl.h" |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 43 | |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 44 | #define CREATE_TRACE_POINTS |
| 45 | #include "trace.h" |
| 46 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 47 | struct kvmppc_ops *kvmppc_hv_ops; |
| 48 | EXPORT_SYMBOL_GPL(kvmppc_hv_ops); |
| 49 | struct kvmppc_ops *kvmppc_pr_ops; |
| 50 | EXPORT_SYMBOL_GPL(kvmppc_pr_ops); |
| 51 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 52 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 53 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
| 54 | { |
Liu Yu-B13201 | 9202e07 | 2012-07-03 05:48:52 +0000 | [diff] [blame] | 55 | return !!(v->arch.pending_exceptions) || |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 56 | v->requests; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 59 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 60 | { |
| 61 | return 1; |
| 62 | } |
| 63 | |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 64 | /* |
| 65 | * Common checks before entering the guest world. Call with interrupts |
| 66 | * disabled. |
| 67 | * |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 68 | * returns: |
| 69 | * |
| 70 | * == 1 if we're ready to go into guest state |
| 71 | * <= 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] | 72 | */ |
| 73 | int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu) |
| 74 | { |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 75 | int r; |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 76 | |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 77 | WARN_ON(irqs_disabled()); |
| 78 | hard_irq_disable(); |
| 79 | |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 80 | while (true) { |
| 81 | if (need_resched()) { |
| 82 | local_irq_enable(); |
| 83 | cond_resched(); |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 84 | hard_irq_disable(); |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 85 | continue; |
| 86 | } |
| 87 | |
| 88 | if (signal_pending(current)) { |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 89 | kvmppc_account_exit(vcpu, SIGNAL_EXITS); |
| 90 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
| 91 | r = -EINTR; |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 92 | break; |
| 93 | } |
| 94 | |
Scott Wood | 5bd1cf1 | 2012-08-22 15:03:50 +0000 | [diff] [blame] | 95 | vcpu->mode = IN_GUEST_MODE; |
| 96 | |
| 97 | /* |
| 98 | * Reading vcpu->requests must happen after setting vcpu->mode, |
| 99 | * so we don't miss a request because the requester sees |
| 100 | * OUTSIDE_GUEST_MODE and assumes we'll be checking requests |
| 101 | * before next entering the guest (and thus doesn't IPI). |
Lan Tianyu | 489153c | 2016-03-13 11:10:30 +0800 | [diff] [blame] | 102 | * This also orders the write to mode from any reads |
| 103 | * to the page tables done while the VCPU is running. |
| 104 | * Please see the comment in kvm_flush_remote_tlbs. |
Scott Wood | 5bd1cf1 | 2012-08-22 15:03:50 +0000 | [diff] [blame] | 105 | */ |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 106 | smp_mb(); |
Scott Wood | 5bd1cf1 | 2012-08-22 15:03:50 +0000 | [diff] [blame] | 107 | |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 108 | if (vcpu->requests) { |
| 109 | /* Make sure we process requests preemptable */ |
| 110 | local_irq_enable(); |
| 111 | trace_kvm_check_requests(vcpu); |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 112 | r = kvmppc_core_check_requests(vcpu); |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 113 | hard_irq_disable(); |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 114 | if (r > 0) |
| 115 | continue; |
| 116 | break; |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (kvmppc_core_prepare_to_enter(vcpu)) { |
| 120 | /* interrupts got enabled in between, so we |
| 121 | are back at square 1 */ |
| 122 | continue; |
| 123 | } |
| 124 | |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 125 | guest_enter_irqoff(); |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 126 | return 1; |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 129 | /* return to host */ |
| 130 | local_irq_enable(); |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 131 | return r; |
| 132 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 133 | EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter); |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 134 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 135 | #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) |
| 136 | static void kvmppc_swab_shared(struct kvm_vcpu *vcpu) |
| 137 | { |
| 138 | struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared; |
| 139 | int i; |
| 140 | |
| 141 | shared->sprg0 = swab64(shared->sprg0); |
| 142 | shared->sprg1 = swab64(shared->sprg1); |
| 143 | shared->sprg2 = swab64(shared->sprg2); |
| 144 | shared->sprg3 = swab64(shared->sprg3); |
| 145 | shared->srr0 = swab64(shared->srr0); |
| 146 | shared->srr1 = swab64(shared->srr1); |
| 147 | shared->dar = swab64(shared->dar); |
| 148 | shared->msr = swab64(shared->msr); |
| 149 | shared->dsisr = swab32(shared->dsisr); |
| 150 | shared->int_pending = swab32(shared->int_pending); |
| 151 | for (i = 0; i < ARRAY_SIZE(shared->sr); i++) |
| 152 | shared->sr[i] = swab32(shared->sr[i]); |
| 153 | } |
| 154 | #endif |
| 155 | |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 156 | int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) |
| 157 | { |
| 158 | int nr = kvmppc_get_gpr(vcpu, 11); |
| 159 | int r; |
| 160 | unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3); |
| 161 | unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4); |
| 162 | unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5); |
| 163 | unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6); |
| 164 | unsigned long r2 = 0; |
| 165 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 166 | if (!(kvmppc_get_msr(vcpu) & MSR_SF)) { |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 167 | /* 32 bit mode */ |
| 168 | param1 &= 0xffffffff; |
| 169 | param2 &= 0xffffffff; |
| 170 | param3 &= 0xffffffff; |
| 171 | param4 &= 0xffffffff; |
| 172 | } |
| 173 | |
| 174 | switch (nr) { |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 175 | case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE): |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 176 | { |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 177 | #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) |
| 178 | /* Book3S can be little endian, find it out here */ |
| 179 | int shared_big_endian = true; |
| 180 | if (vcpu->arch.intr_msr & MSR_LE) |
| 181 | shared_big_endian = false; |
| 182 | if (shared_big_endian != vcpu->arch.shared_big_endian) |
| 183 | kvmppc_swab_shared(vcpu); |
| 184 | vcpu->arch.shared_big_endian = shared_big_endian; |
| 185 | #endif |
| 186 | |
Alexander Graf | f3383cf | 2014-05-12 01:08:32 +0200 | [diff] [blame] | 187 | if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) { |
| 188 | /* |
| 189 | * Older versions of the Linux magic page code had |
| 190 | * a bug where they would map their trampoline code |
| 191 | * NX. If that's the case, remove !PR NX capability. |
| 192 | */ |
| 193 | vcpu->arch.disable_kernel_nx = true; |
| 194 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 195 | } |
| 196 | |
| 197 | vcpu->arch.magic_page_pa = param1 & ~0xfffULL; |
| 198 | vcpu->arch.magic_page_ea = param2 & ~0xfffULL; |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 199 | |
Alexander Graf | 89b68c9 | 2014-07-13 16:37:12 +0200 | [diff] [blame] | 200 | #ifdef CONFIG_PPC_64K_PAGES |
| 201 | /* |
| 202 | * Make sure our 4k magic page is in the same window of a 64k |
| 203 | * page within the guest and within the host's page. |
| 204 | */ |
| 205 | if ((vcpu->arch.magic_page_pa & 0xf000) != |
| 206 | ((ulong)vcpu->arch.shared & 0xf000)) { |
| 207 | void *old_shared = vcpu->arch.shared; |
| 208 | ulong shared = (ulong)vcpu->arch.shared; |
| 209 | void *new_shared; |
| 210 | |
| 211 | shared &= PAGE_MASK; |
| 212 | shared |= vcpu->arch.magic_page_pa & 0xf000; |
| 213 | new_shared = (void*)shared; |
| 214 | memcpy(new_shared, old_shared, 0x1000); |
| 215 | vcpu->arch.shared = new_shared; |
| 216 | } |
| 217 | #endif |
| 218 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 219 | r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7; |
Alexander Graf | 7508e16 | 2010-08-03 11:32:56 +0200 | [diff] [blame] | 220 | |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 221 | r = EV_SUCCESS; |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 222 | break; |
| 223 | } |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 224 | case KVM_HCALL_TOKEN(KVM_HC_FEATURES): |
| 225 | r = EV_SUCCESS; |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 226 | #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2) |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 227 | r2 |= (1 << KVM_FEATURE_MAGIC_PAGE); |
| 228 | #endif |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 229 | |
| 230 | /* Second return value is in r4 */ |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 231 | break; |
Liu Yu-B13201 | 9202e07 | 2012-07-03 05:48:52 +0000 | [diff] [blame] | 232 | case EV_HCALL_TOKEN(EV_IDLE): |
| 233 | r = EV_SUCCESS; |
| 234 | kvm_vcpu_block(vcpu); |
| 235 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
| 236 | break; |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 237 | default: |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 238 | r = EV_UNIMPLEMENTED; |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | |
Alexander Graf | 7508e16 | 2010-08-03 11:32:56 +0200 | [diff] [blame] | 242 | kvmppc_set_gpr(vcpu, 4, r2); |
| 243 | |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 244 | return r; |
| 245 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 246 | EXPORT_SYMBOL_GPL(kvmppc_kvm_pv); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 247 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 248 | int kvmppc_sanity_check(struct kvm_vcpu *vcpu) |
| 249 | { |
| 250 | int r = false; |
| 251 | |
| 252 | /* We have to know what CPU to virtualize */ |
| 253 | if (!vcpu->arch.pvr) |
| 254 | goto out; |
| 255 | |
| 256 | /* PAPR only works with book3s_64 */ |
| 257 | if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled) |
| 258 | goto out; |
| 259 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 260 | /* HV KVM can only do PAPR mode for now */ |
Aneesh Kumar K.V | a78b55d | 2013-10-07 22:18:02 +0530 | [diff] [blame] | 261 | if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm)) |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 262 | goto out; |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 263 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 264 | #ifdef CONFIG_KVM_BOOKE_HV |
| 265 | if (!cpu_has_feature(CPU_FTR_EMB_HV)) |
| 266 | goto out; |
| 267 | #endif |
| 268 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 269 | r = true; |
| 270 | |
| 271 | out: |
| 272 | vcpu->arch.sane = r; |
| 273 | return r ? 0 : -EINVAL; |
| 274 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 275 | EXPORT_SYMBOL_GPL(kvmppc_sanity_check); |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 276 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 277 | int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 278 | { |
| 279 | enum emulation_result er; |
| 280 | int r; |
| 281 | |
Alexander Graf | d69614a | 2014-06-18 14:53:49 +0200 | [diff] [blame] | 282 | er = kvmppc_emulate_loadstore(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 283 | switch (er) { |
| 284 | case EMULATE_DONE: |
| 285 | /* Future optimization: only reload non-volatiles if they were |
| 286 | * actually modified. */ |
| 287 | r = RESUME_GUEST_NV; |
| 288 | break; |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 289 | case EMULATE_AGAIN: |
| 290 | r = RESUME_GUEST; |
| 291 | break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 292 | case EMULATE_DO_MMIO: |
| 293 | run->exit_reason = KVM_EXIT_MMIO; |
| 294 | /* We must reload nonvolatiles because "update" load/store |
| 295 | * instructions modify register state. */ |
| 296 | /* Future optimization: only reload non-volatiles if they were |
| 297 | * actually modified. */ |
| 298 | r = RESUME_HOST_NV; |
| 299 | break; |
| 300 | case EMULATE_FAIL: |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 301 | { |
| 302 | u32 last_inst; |
| 303 | |
Alexander Graf | 8d0eff6 | 2014-09-10 14:37:29 +0200 | [diff] [blame] | 304 | kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 305 | /* XXX Deliver Program interrupt to guest. */ |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 306 | pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 307 | r = RESUME_HOST; |
| 308 | break; |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 309 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 310 | default: |
Alexander Graf | 5a33169 | 2012-12-14 23:46:03 +0100 | [diff] [blame] | 311 | WARN_ON(1); |
| 312 | r = RESUME_GUEST; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return r; |
| 316 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 317 | EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 318 | |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 319 | int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, |
| 320 | bool data) |
| 321 | { |
Alexander Graf | c12fb43 | 2014-06-20 14:43:36 +0200 | [diff] [blame] | 322 | ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 323 | struct kvmppc_pte pte; |
| 324 | int r; |
| 325 | |
| 326 | vcpu->stat.st++; |
| 327 | |
| 328 | r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST, |
| 329 | XLATE_WRITE, &pte); |
| 330 | if (r < 0) |
| 331 | return r; |
| 332 | |
| 333 | *eaddr = pte.raddr; |
| 334 | |
| 335 | if (!pte.may_write) |
| 336 | return -EPERM; |
| 337 | |
Alexander Graf | c12fb43 | 2014-06-20 14:43:36 +0200 | [diff] [blame] | 338 | /* Magic page override */ |
| 339 | if (kvmppc_supports_magic_page(vcpu) && mp_pa && |
| 340 | ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) && |
| 341 | !(kvmppc_get_msr(vcpu) & MSR_PR)) { |
| 342 | void *magic = vcpu->arch.shared; |
| 343 | magic += pte.eaddr & 0xfff; |
| 344 | memcpy(magic, ptr, size); |
| 345 | return EMULATE_DONE; |
| 346 | } |
| 347 | |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 348 | if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size)) |
| 349 | return EMULATE_DO_MMIO; |
| 350 | |
| 351 | return EMULATE_DONE; |
| 352 | } |
| 353 | EXPORT_SYMBOL_GPL(kvmppc_st); |
| 354 | |
| 355 | int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, |
| 356 | bool data) |
| 357 | { |
Alexander Graf | c12fb43 | 2014-06-20 14:43:36 +0200 | [diff] [blame] | 358 | ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 359 | struct kvmppc_pte pte; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 360 | int rc; |
| 361 | |
| 362 | vcpu->stat.ld++; |
| 363 | |
| 364 | rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST, |
| 365 | XLATE_READ, &pte); |
| 366 | if (rc) |
| 367 | return rc; |
| 368 | |
| 369 | *eaddr = pte.raddr; |
| 370 | |
| 371 | if (!pte.may_read) |
| 372 | return -EPERM; |
| 373 | |
| 374 | if (!data && !pte.may_execute) |
| 375 | return -ENOEXEC; |
| 376 | |
Alexander Graf | c12fb43 | 2014-06-20 14:43:36 +0200 | [diff] [blame] | 377 | /* Magic page override */ |
| 378 | if (kvmppc_supports_magic_page(vcpu) && mp_pa && |
| 379 | ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) && |
| 380 | !(kvmppc_get_msr(vcpu) & MSR_PR)) { |
| 381 | void *magic = vcpu->arch.shared; |
| 382 | magic += pte.eaddr & 0xfff; |
| 383 | memcpy(ptr, magic, size); |
| 384 | return EMULATE_DONE; |
| 385 | } |
| 386 | |
Alexander Graf | c45c551 | 2014-06-20 14:17:30 +0200 | [diff] [blame] | 387 | if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size)) |
| 388 | return EMULATE_DO_MMIO; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 389 | |
| 390 | return EMULATE_DONE; |
Alexander Graf | 35c4a73 | 2014-06-20 13:58:16 +0200 | [diff] [blame] | 391 | } |
| 392 | EXPORT_SYMBOL_GPL(kvmppc_ld); |
| 393 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 394 | int kvm_arch_hardware_enable(void) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 395 | { |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 396 | return 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 397 | } |
| 398 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 399 | int kvm_arch_hardware_setup(void) |
| 400 | { |
| 401 | return 0; |
| 402 | } |
| 403 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 404 | void kvm_arch_check_processor_compat(void *rtn) |
| 405 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 406 | *(int *)rtn = kvmppc_core_check_processor_compat(); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 407 | } |
| 408 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 409 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 410 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 411 | struct kvmppc_ops *kvm_ops = NULL; |
| 412 | /* |
| 413 | * if we have both HV and PR enabled, default is HV |
| 414 | */ |
| 415 | if (type == 0) { |
| 416 | if (kvmppc_hv_ops) |
| 417 | kvm_ops = kvmppc_hv_ops; |
| 418 | else |
| 419 | kvm_ops = kvmppc_pr_ops; |
| 420 | if (!kvm_ops) |
| 421 | goto err_out; |
| 422 | } else if (type == KVM_VM_PPC_HV) { |
| 423 | if (!kvmppc_hv_ops) |
| 424 | goto err_out; |
| 425 | kvm_ops = kvmppc_hv_ops; |
| 426 | } else if (type == KVM_VM_PPC_PR) { |
| 427 | if (!kvmppc_pr_ops) |
| 428 | goto err_out; |
| 429 | kvm_ops = kvmppc_pr_ops; |
| 430 | } else |
| 431 | goto err_out; |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 432 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 433 | if (kvm_ops->owner && !try_module_get(kvm_ops->owner)) |
| 434 | return -ENOENT; |
| 435 | |
| 436 | kvm->arch.kvm_ops = kvm_ops; |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 437 | return kvmppc_core_init_vm(kvm); |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 438 | err_out: |
| 439 | return -EINVAL; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 440 | } |
| 441 | |
Luiz Capitulino | 235539b | 2016-09-07 14:47:23 -0400 | [diff] [blame] | 442 | bool kvm_arch_has_vcpu_debugfs(void) |
| 443 | { |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) |
| 448 | { |
| 449 | return 0; |
| 450 | } |
| 451 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 452 | void kvm_arch_destroy_vm(struct kvm *kvm) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 453 | { |
| 454 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 455 | struct kvm_vcpu *vcpu; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 456 | |
Suresh E. Warrier | e17769e | 2015-12-21 16:22:51 -0600 | [diff] [blame] | 457 | #ifdef CONFIG_KVM_XICS |
| 458 | /* |
| 459 | * We call kick_all_cpus_sync() to ensure that all |
| 460 | * CPUs have executed any pending IPIs before we |
| 461 | * continue and free VCPUs structures below. |
| 462 | */ |
| 463 | if (is_kvmppc_hv_enabled(kvm)) |
| 464 | kick_all_cpus_sync(); |
| 465 | #endif |
| 466 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 467 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 468 | kvm_arch_vcpu_free(vcpu); |
| 469 | |
| 470 | mutex_lock(&kvm->lock); |
| 471 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 472 | kvm->vcpus[i] = NULL; |
| 473 | |
| 474 | atomic_set(&kvm->online_vcpus, 0); |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 475 | |
| 476 | kvmppc_core_destroy_vm(kvm); |
| 477 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 478 | mutex_unlock(&kvm->lock); |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 479 | |
| 480 | /* drop the module reference */ |
| 481 | module_put(kvm->arch.kvm_ops->owner); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Alexander Graf | 784aa3d | 2014-07-14 18:27:35 +0200 | [diff] [blame] | 484 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 485 | { |
| 486 | int r; |
Alexander Graf | 7a58777 | 2014-07-14 18:55:19 +0200 | [diff] [blame] | 487 | /* 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] | 488 | int hv_enabled = kvmppc_hv_ops ? 1 : 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 489 | |
Alexander Graf | 7a58777 | 2014-07-14 18:55:19 +0200 | [diff] [blame] | 490 | if (kvm) { |
| 491 | /* |
| 492 | * Hooray - we know which VM type we're running on. Depend on |
| 493 | * that rather than the guess above. |
| 494 | */ |
| 495 | hv_enabled = is_kvmppc_hv_enabled(kvm); |
| 496 | } |
| 497 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 498 | switch (ext) { |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 499 | #ifdef CONFIG_BOOKE |
| 500 | case KVM_CAP_PPC_BOOKE_SREGS: |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 501 | case KVM_CAP_PPC_BOOKE_WATCHDOG: |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 502 | case KVM_CAP_PPC_EPR: |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 503 | #else |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 504 | case KVM_CAP_PPC_SEGSTATE: |
Alexander Graf | 1022fc3 | 2011-09-14 21:45:23 +0200 | [diff] [blame] | 505 | case KVM_CAP_PPC_HIOR: |
Alexander Graf | 930b412 | 2011-08-08 17:29:42 +0200 | [diff] [blame] | 506 | case KVM_CAP_PPC_PAPR: |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 507 | #endif |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 508 | case KVM_CAP_PPC_UNSET_IRQ: |
Alexander Graf | 7b4203e | 2010-08-30 13:50:45 +0200 | [diff] [blame] | 509 | case KVM_CAP_PPC_IRQ_LEVEL: |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 510 | case KVM_CAP_ENABLE_CAP: |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 511 | case KVM_CAP_ENABLE_CAP_VM: |
Alexander Graf | e24ed81 | 2011-09-14 10:02:41 +0200 | [diff] [blame] | 512 | case KVM_CAP_ONE_REG: |
Alexander Graf | 0e673fb | 2012-10-09 00:06:20 +0200 | [diff] [blame] | 513 | case KVM_CAP_IOEVENTFD: |
Scott Wood | 5df554ad | 2013-04-12 14:08:46 +0000 | [diff] [blame] | 514 | case KVM_CAP_DEVICE_CTRL: |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 515 | case KVM_CAP_IMMEDIATE_EXIT: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 516 | r = 1; |
| 517 | break; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 518 | case KVM_CAP_PPC_PAIRED_SINGLES: |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 519 | case KVM_CAP_PPC_OSI: |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 520 | case KVM_CAP_PPC_GET_PVINFO: |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 521 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 522 | case KVM_CAP_SW_TLB: |
| 523 | #endif |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 524 | /* We support this only for PR */ |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 525 | r = !hv_enabled; |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 526 | break; |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 527 | #ifdef CONFIG_KVM_MPIC |
| 528 | case KVM_CAP_IRQ_MPIC: |
| 529 | r = 1; |
| 530 | break; |
| 531 | #endif |
| 532 | |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 533 | #ifdef CONFIG_PPC_BOOK3S_64 |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 534 | case KVM_CAP_SPAPR_TCE: |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 535 | case KVM_CAP_SPAPR_TCE_64: |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 536 | case KVM_CAP_PPC_RTAS: |
Alexander Graf | f2e9104 | 2014-05-22 17:40:15 +0200 | [diff] [blame] | 537 | case KVM_CAP_PPC_FIXUP_HCALL: |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 538 | case KVM_CAP_PPC_ENABLE_HCALL: |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 539 | #ifdef CONFIG_KVM_XICS |
| 540 | case KVM_CAP_IRQ_XICS: |
| 541 | #endif |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 542 | r = 1; |
| 543 | break; |
David Gibson | a8acaec | 2016-11-23 16:14:07 +1100 | [diff] [blame] | 544 | |
| 545 | case KVM_CAP_PPC_ALLOC_HTAB: |
| 546 | r = hv_enabled; |
| 547 | break; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 548 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 549 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 550 | case KVM_CAP_PPC_SMT: |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 551 | r = 0; |
| 552 | if (hv_enabled) { |
| 553 | if (cpu_has_feature(CPU_FTR_ARCH_300)) |
| 554 | r = 1; |
| 555 | else |
| 556 | r = threads_per_subcore; |
| 557 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 558 | break; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 559 | case KVM_CAP_PPC_RMA: |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 560 | r = 0; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 561 | break; |
Michael Ellerman | e928e9c | 2015-03-20 20:39:41 +1100 | [diff] [blame] | 562 | case KVM_CAP_PPC_HWRNG: |
| 563 | r = kvmppc_hwrng_present(); |
| 564 | break; |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 565 | case KVM_CAP_PPC_MMU_RADIX: |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 566 | r = !!(hv_enabled && radix_enabled()); |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 567 | break; |
| 568 | case KVM_CAP_PPC_MMU_HASH_V3: |
Paul Mackerras | 468808b | 2017-01-30 21:21:42 +1100 | [diff] [blame] | 569 | r = !!(hv_enabled && !radix_enabled() && |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 570 | cpu_has_feature(CPU_FTR_ARCH_300)); |
| 571 | break; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 572 | #endif |
Alexander Graf | f4800b1 | 2012-08-07 10:24:14 +0200 | [diff] [blame] | 573 | case KVM_CAP_SYNC_MMU: |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 574 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 575 | r = hv_enabled; |
Alexander Graf | f4800b1 | 2012-08-07 10:24:14 +0200 | [diff] [blame] | 576 | #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 577 | r = 1; |
| 578 | #else |
| 579 | r = 0; |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 580 | #endif |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 581 | break; |
| 582 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 583 | case KVM_CAP_PPC_HTAB_FD: |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 584 | r = hv_enabled; |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 585 | break; |
Alexander Graf | f4800b1 | 2012-08-07 10:24:14 +0200 | [diff] [blame] | 586 | #endif |
Matt Evans | b543403 | 2011-12-07 16:55:57 +0000 | [diff] [blame] | 587 | case KVM_CAP_NR_VCPUS: |
| 588 | /* |
| 589 | * Recommending a number of CPUs is somewhat arbitrary; we |
| 590 | * return the number of present CPUs for -HV (since a host |
| 591 | * will have secondary threads "offline"), and for other KVM |
| 592 | * implementations just count online CPUs. |
| 593 | */ |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 594 | if (hv_enabled) |
Aneesh Kumar K.V | 699cc87 | 2013-10-07 22:17:56 +0530 | [diff] [blame] | 595 | r = num_present_cpus(); |
| 596 | else |
| 597 | r = num_online_cpus(); |
Matt Evans | b543403 | 2011-12-07 16:55:57 +0000 | [diff] [blame] | 598 | break; |
Nikunj A Dadhania | bfec5c2c | 2015-10-16 10:27:53 +0530 | [diff] [blame] | 599 | case KVM_CAP_NR_MEMSLOTS: |
| 600 | r = KVM_USER_MEM_SLOTS; |
| 601 | break; |
Matt Evans | b543403 | 2011-12-07 16:55:57 +0000 | [diff] [blame] | 602 | case KVM_CAP_MAX_VCPUS: |
| 603 | r = KVM_MAX_VCPUS; |
| 604 | break; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 605 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 606 | case KVM_CAP_PPC_GET_SMMU_INFO: |
| 607 | r = 1; |
| 608 | break; |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 609 | case KVM_CAP_SPAPR_MULTITCE: |
| 610 | r = 1; |
| 611 | break; |
David Gibson | 050f233 | 2016-12-20 16:49:07 +1100 | [diff] [blame] | 612 | case KVM_CAP_SPAPR_RESIZE_HPT: |
Paul Mackerras | bcd3bb6 | 2017-02-18 08:30:44 +1100 | [diff] [blame] | 613 | /* Disable this on POWER9 until code handles new HPTE format */ |
| 614 | r = !!hv_enabled && !cpu_has_feature(CPU_FTR_ARCH_300); |
David Gibson | 050f233 | 2016-12-20 16:49:07 +1100 | [diff] [blame] | 615 | break; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 616 | #endif |
Sam Bobroff | 23528bb | 2016-07-20 13:41:36 +1000 | [diff] [blame] | 617 | case KVM_CAP_PPC_HTM: |
| 618 | r = cpu_has_feature(CPU_FTR_TM_COMP) && |
| 619 | is_kvmppc_hv_enabled(kvm); |
| 620 | break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 621 | default: |
| 622 | r = 0; |
| 623 | break; |
| 624 | } |
| 625 | return r; |
| 626 | |
| 627 | } |
| 628 | |
| 629 | long kvm_arch_dev_ioctl(struct file *filp, |
| 630 | unsigned int ioctl, unsigned long arg) |
| 631 | { |
| 632 | return -EINVAL; |
| 633 | } |
| 634 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 635 | 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] | 636 | struct kvm_memory_slot *dont) |
| 637 | { |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 638 | kvmppc_core_free_memslot(kvm, free, dont); |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 639 | } |
| 640 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 641 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
| 642 | unsigned long npages) |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 643 | { |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 644 | return kvmppc_core_create_memslot(kvm, slot, npages); |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 645 | } |
| 646 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 647 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
Takuya Yoshikawa | 462fce4 | 2013-02-27 19:41:56 +0900 | [diff] [blame] | 648 | struct kvm_memory_slot *memslot, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 649 | const struct kvm_userspace_memory_region *mem, |
Takuya Yoshikawa | 7b6195a | 2013-02-27 19:44:34 +0900 | [diff] [blame] | 650 | enum kvm_mr_change change) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 651 | { |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 652 | return kvmppc_core_prepare_memory_region(kvm, memslot, mem); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 653 | } |
| 654 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 655 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 656 | const struct kvm_userspace_memory_region *mem, |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 657 | const struct kvm_memory_slot *old, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 658 | const struct kvm_memory_slot *new, |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 659 | enum kvm_mr_change change) |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 660 | { |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 661 | kvmppc_core_commit_memory_region(kvm, mem, old, new); |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 662 | } |
| 663 | |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 664 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 665 | struct kvm_memory_slot *slot) |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 666 | { |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 667 | kvmppc_core_flush_memslot(kvm, slot); |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 668 | } |
| 669 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 670 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
| 671 | { |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 672 | struct kvm_vcpu *vcpu; |
| 673 | vcpu = kvmppc_core_vcpu_create(kvm, id); |
Matt Evans | 03cdab5 | 2011-12-06 21:19:42 +0000 | [diff] [blame] | 674 | if (!IS_ERR(vcpu)) { |
| 675 | vcpu->arch.wqp = &vcpu->wq; |
Wei Yongjun | 06056bf | 2010-03-09 14:13:43 +0800 | [diff] [blame] | 676 | kvmppc_create_vcpu_debugfs(vcpu, id); |
Matt Evans | 03cdab5 | 2011-12-06 21:19:42 +0000 | [diff] [blame] | 677 | } |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 678 | return vcpu; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 679 | } |
| 680 | |
Dominik Dingel | 31928aa | 2014-12-04 15:47:07 +0100 | [diff] [blame] | 681 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 682 | { |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 683 | } |
| 684 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 685 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 686 | { |
Alexander Graf | a595405 | 2010-02-22 16:52:14 +0100 | [diff] [blame] | 687 | /* Make sure we're not using the vcpu anymore */ |
| 688 | hrtimer_cancel(&vcpu->arch.dec_timer); |
Alexander Graf | a595405 | 2010-02-22 16:52:14 +0100 | [diff] [blame] | 689 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 690 | kvmppc_remove_vcpu_debugfs(vcpu); |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 691 | |
| 692 | switch (vcpu->arch.irq_type) { |
| 693 | case KVMPPC_IRQ_MPIC: |
| 694 | kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu); |
| 695 | break; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 696 | case KVMPPC_IRQ_XICS: |
| 697 | kvmppc_xics_free_icp(vcpu); |
| 698 | break; |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 701 | kvmppc_core_vcpu_free(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 705 | { |
| 706 | kvm_arch_vcpu_free(vcpu); |
| 707 | } |
| 708 | |
| 709 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 710 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 711 | return kvmppc_core_pending_dec(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 712 | } |
| 713 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 714 | static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer) |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 715 | { |
| 716 | struct kvm_vcpu *vcpu; |
| 717 | |
| 718 | vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer); |
Mihai Caraman | d02d4d1 | 2014-09-01 17:19:56 +0300 | [diff] [blame] | 719 | kvmppc_decrementer_func(vcpu); |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 720 | |
| 721 | return HRTIMER_NORESTART; |
| 722 | } |
| 723 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 724 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 725 | { |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 726 | int ret; |
| 727 | |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 728 | hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 729 | vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 730 | vcpu->arch.dec_expires = ~(u64)0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 731 | |
Bharat Bhushan | 09000ad | 2011-03-25 10:32:13 +0530 | [diff] [blame] | 732 | #ifdef CONFIG_KVM_EXIT_TIMING |
| 733 | mutex_init(&vcpu->arch.exit_timing_lock); |
| 734 | #endif |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 735 | ret = kvmppc_subarch_vcpu_init(vcpu); |
| 736 | return ret; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 740 | { |
Hollis Blanchard | ecc0981 | 2009-01-03 16:22:59 -0600 | [diff] [blame] | 741 | kvmppc_mmu_destroy(vcpu); |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 742 | kvmppc_subarch_vcpu_uninit(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 746 | { |
Scott Wood | eab1767 | 2011-04-27 17:24:10 -0500 | [diff] [blame] | 747 | #ifdef CONFIG_BOOKE |
| 748 | /* |
| 749 | * vrsave (formerly usprg0) isn't used by Linux, but may |
| 750 | * be used by the guest. |
| 751 | * |
| 752 | * On non-booke this is associated with Altivec and |
| 753 | * is handled by code in book3s.c. |
| 754 | */ |
| 755 | mtspr(SPRN_VRSAVE, vcpu->arch.vrsave); |
| 756 | #endif |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 757 | kvmppc_core_vcpu_load(vcpu, cpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 761 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 762 | kvmppc_core_vcpu_put(vcpu); |
Scott Wood | eab1767 | 2011-04-27 17:24:10 -0500 | [diff] [blame] | 763 | #ifdef CONFIG_BOOKE |
| 764 | vcpu->arch.vrsave = mfspr(SPRN_VRSAVE); |
| 765 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 766 | } |
| 767 | |
Suresh Warrier | 9576730 | 2016-08-19 15:35:47 +1000 | [diff] [blame] | 768 | /* |
| 769 | * irq_bypass_add_producer and irq_bypass_del_producer are only |
| 770 | * useful if the architecture supports PCI passthrough. |
| 771 | * irq_bypass_stop and irq_bypass_start are not needed and so |
| 772 | * kvm_ops are not defined for them. |
| 773 | */ |
| 774 | bool kvm_arch_has_irq_bypass(void) |
| 775 | { |
| 776 | return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) || |
| 777 | (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer)); |
| 778 | } |
| 779 | |
| 780 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
| 781 | struct irq_bypass_producer *prod) |
| 782 | { |
| 783 | struct kvm_kernel_irqfd *irqfd = |
| 784 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 785 | struct kvm *kvm = irqfd->kvm; |
| 786 | |
| 787 | if (kvm->arch.kvm_ops->irq_bypass_add_producer) |
| 788 | return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod); |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, |
| 794 | struct irq_bypass_producer *prod) |
| 795 | { |
| 796 | struct kvm_kernel_irqfd *irqfd = |
| 797 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 798 | struct kvm *kvm = irqfd->kvm; |
| 799 | |
| 800 | if (kvm->arch.kvm_ops->irq_bypass_del_producer) |
| 801 | kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod); |
| 802 | } |
| 803 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 804 | static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, |
| 805 | struct kvm_run *run) |
| 806 | { |
Denis Kirjanov | 69b6183 | 2010-06-11 11:23:26 +0000 | [diff] [blame] | 807 | u64 uninitialized_var(gpr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 808 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 809 | if (run->mmio.len > sizeof(gpr)) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 810 | printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len); |
| 811 | return; |
| 812 | } |
| 813 | |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 814 | if (!vcpu->arch.mmio_host_swabbed) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 815 | switch (run->mmio.len) { |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 816 | case 8: gpr = *(u64 *)run->mmio.data; break; |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 817 | case 4: gpr = *(u32 *)run->mmio.data; break; |
| 818 | case 2: gpr = *(u16 *)run->mmio.data; break; |
| 819 | case 1: gpr = *(u8 *)run->mmio.data; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 820 | } |
| 821 | } else { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 822 | switch (run->mmio.len) { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 823 | case 8: gpr = swab64(*(u64 *)run->mmio.data); break; |
| 824 | case 4: gpr = swab32(*(u32 *)run->mmio.data); break; |
| 825 | case 2: gpr = swab16(*(u16 *)run->mmio.data); break; |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 826 | case 1: gpr = *(u8 *)run->mmio.data; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 827 | } |
| 828 | } |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 829 | |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 830 | if (vcpu->arch.mmio_sign_extend) { |
| 831 | switch (run->mmio.len) { |
| 832 | #ifdef CONFIG_PPC64 |
| 833 | case 4: |
| 834 | gpr = (s64)(s32)gpr; |
| 835 | break; |
| 836 | #endif |
| 837 | case 2: |
| 838 | gpr = (s64)(s16)gpr; |
| 839 | break; |
| 840 | case 1: |
| 841 | gpr = (s64)(s8)gpr; |
| 842 | break; |
| 843 | } |
| 844 | } |
| 845 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 846 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 847 | |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 848 | switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) { |
| 849 | case KVM_MMIO_REG_GPR: |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 850 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
| 851 | break; |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 852 | case KVM_MMIO_REG_FPR: |
Paul Mackerras | efff191 | 2013-10-15 20:43:02 +1100 | [diff] [blame] | 853 | VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr; |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 854 | break; |
Alexander Graf | 287d561 | 2010-04-01 15:33:21 +0200 | [diff] [blame] | 855 | #ifdef CONFIG_PPC_BOOK3S |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 856 | case KVM_MMIO_REG_QPR: |
| 857 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 858 | break; |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 859 | case KVM_MMIO_REG_FQPR: |
Paul Mackerras | efff191 | 2013-10-15 20:43:02 +1100 | [diff] [blame] | 860 | VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr; |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 861 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 862 | break; |
Alexander Graf | 287d561 | 2010-04-01 15:33:21 +0200 | [diff] [blame] | 863 | #endif |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 864 | default: |
| 865 | BUG(); |
| 866 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 867 | } |
| 868 | |
Paul Mackerras | eb8b056 | 2016-05-05 16:17:10 +1000 | [diff] [blame] | 869 | static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 870 | unsigned int rt, unsigned int bytes, |
| 871 | int is_default_endian, int sign_extend) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 872 | { |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 873 | int idx, ret; |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 874 | bool host_swabbed; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 875 | |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 876 | /* Pity C doesn't have a logical XOR operator */ |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 877 | if (kvmppc_need_byteswap(vcpu)) { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 878 | host_swabbed = is_default_endian; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 879 | } else { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 880 | host_swabbed = !is_default_endian; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 881 | } |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 882 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 883 | if (bytes > sizeof(run->mmio.data)) { |
| 884 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, |
| 885 | run->mmio.len); |
| 886 | } |
| 887 | |
| 888 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; |
| 889 | run->mmio.len = bytes; |
| 890 | run->mmio.is_write = 0; |
| 891 | |
| 892 | vcpu->arch.io_gpr = rt; |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 893 | vcpu->arch.mmio_host_swabbed = host_swabbed; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 894 | vcpu->mmio_needed = 1; |
| 895 | vcpu->mmio_is_write = 0; |
Paul Mackerras | eb8b056 | 2016-05-05 16:17:10 +1000 | [diff] [blame] | 896 | vcpu->arch.mmio_sign_extend = sign_extend; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 897 | |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 898 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 899 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 900 | 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] | 901 | bytes, &run->mmio.data); |
| 902 | |
| 903 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 904 | |
| 905 | if (!ret) { |
Alexander Graf | 0e673fb | 2012-10-09 00:06:20 +0200 | [diff] [blame] | 906 | kvmppc_complete_mmio_load(vcpu, run); |
| 907 | vcpu->mmio_needed = 0; |
| 908 | return EMULATE_DONE; |
| 909 | } |
| 910 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 911 | return EMULATE_DO_MMIO; |
| 912 | } |
Paul Mackerras | eb8b056 | 2016-05-05 16:17:10 +1000 | [diff] [blame] | 913 | |
| 914 | int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 915 | unsigned int rt, unsigned int bytes, |
| 916 | int is_default_endian) |
| 917 | { |
| 918 | return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0); |
| 919 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 920 | EXPORT_SYMBOL_GPL(kvmppc_handle_load); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 921 | |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 922 | /* Same as above, but sign extends */ |
| 923 | 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] | 924 | unsigned int rt, unsigned int bytes, |
| 925 | int is_default_endian) |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 926 | { |
Paul Mackerras | eb8b056 | 2016-05-05 16:17:10 +1000 | [diff] [blame] | 927 | return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1); |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 928 | } |
| 929 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 930 | 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] | 931 | u64 val, unsigned int bytes, int is_default_endian) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 932 | { |
| 933 | void *data = run->mmio.data; |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 934 | int idx, ret; |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 935 | bool host_swabbed; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 936 | |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 937 | /* Pity C doesn't have a logical XOR operator */ |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 938 | if (kvmppc_need_byteswap(vcpu)) { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 939 | host_swabbed = is_default_endian; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 940 | } else { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 941 | host_swabbed = !is_default_endian; |
Cédric Le Goater | 7360177 | 2014-01-09 11:51:16 +0100 | [diff] [blame] | 942 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 943 | |
| 944 | if (bytes > sizeof(run->mmio.data)) { |
| 945 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, |
| 946 | run->mmio.len); |
| 947 | } |
| 948 | |
| 949 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; |
| 950 | run->mmio.len = bytes; |
| 951 | run->mmio.is_write = 1; |
| 952 | vcpu->mmio_needed = 1; |
| 953 | vcpu->mmio_is_write = 1; |
| 954 | |
| 955 | /* Store the value at the lowest bytes in 'data'. */ |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 956 | if (!host_swabbed) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 957 | switch (bytes) { |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 958 | case 8: *(u64 *)data = val; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 959 | case 4: *(u32 *)data = val; break; |
| 960 | case 2: *(u16 *)data = val; break; |
| 961 | case 1: *(u8 *)data = val; break; |
| 962 | } |
| 963 | } else { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 964 | switch (bytes) { |
David Gibson | d078eed | 2015-02-03 16:36:24 +1100 | [diff] [blame] | 965 | case 8: *(u64 *)data = swab64(val); break; |
| 966 | case 4: *(u32 *)data = swab32(val); break; |
| 967 | case 2: *(u16 *)data = swab16(val); break; |
| 968 | case 1: *(u8 *)data = val; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
Scott Wood | ed840ee | 2013-04-26 14:53:39 +0000 | [diff] [blame] | 972 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 973 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 974 | 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] | 975 | bytes, &run->mmio.data); |
| 976 | |
| 977 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 978 | |
| 979 | if (!ret) { |
Alexander Graf | 0e673fb | 2012-10-09 00:06:20 +0200 | [diff] [blame] | 980 | vcpu->mmio_needed = 0; |
| 981 | return EMULATE_DONE; |
| 982 | } |
| 983 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 984 | return EMULATE_DO_MMIO; |
| 985 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 986 | EXPORT_SYMBOL_GPL(kvmppc_handle_store); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 987 | |
Mihai Caraman | 8a41ea5 | 2014-08-20 16:36:24 +0300 | [diff] [blame] | 988 | int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 989 | { |
| 990 | int r = 0; |
| 991 | union kvmppc_one_reg val; |
| 992 | int size; |
| 993 | |
| 994 | size = one_reg_size(reg->id); |
| 995 | if (size > sizeof(val)) |
| 996 | return -EINVAL; |
| 997 | |
| 998 | r = kvmppc_get_one_reg(vcpu, reg->id, &val); |
| 999 | if (r == -EINVAL) { |
| 1000 | r = 0; |
| 1001 | switch (reg->id) { |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1002 | #ifdef CONFIG_ALTIVEC |
| 1003 | case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31: |
| 1004 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1005 | r = -ENXIO; |
| 1006 | break; |
| 1007 | } |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1008 | val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0]; |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1009 | break; |
| 1010 | case KVM_REG_PPC_VSCR: |
| 1011 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1012 | r = -ENXIO; |
| 1013 | break; |
| 1014 | } |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1015 | val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]); |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1016 | break; |
| 1017 | case KVM_REG_PPC_VRSAVE: |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1018 | val = get_reg_val(reg->id, vcpu->arch.vrsave); |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1019 | break; |
| 1020 | #endif /* CONFIG_ALTIVEC */ |
Mihai Caraman | 8a41ea5 | 2014-08-20 16:36:24 +0300 | [diff] [blame] | 1021 | default: |
| 1022 | r = -EINVAL; |
| 1023 | break; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | if (r) |
| 1028 | return r; |
| 1029 | |
| 1030 | if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size)) |
| 1031 | r = -EFAULT; |
| 1032 | |
| 1033 | return r; |
| 1034 | } |
| 1035 | |
| 1036 | int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 1037 | { |
| 1038 | int r; |
| 1039 | union kvmppc_one_reg val; |
| 1040 | int size; |
| 1041 | |
| 1042 | size = one_reg_size(reg->id); |
| 1043 | if (size > sizeof(val)) |
| 1044 | return -EINVAL; |
| 1045 | |
| 1046 | if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size)) |
| 1047 | return -EFAULT; |
| 1048 | |
| 1049 | r = kvmppc_set_one_reg(vcpu, reg->id, &val); |
| 1050 | if (r == -EINVAL) { |
| 1051 | r = 0; |
| 1052 | switch (reg->id) { |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1053 | #ifdef CONFIG_ALTIVEC |
| 1054 | case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31: |
| 1055 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1056 | r = -ENXIO; |
| 1057 | break; |
| 1058 | } |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1059 | vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval; |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1060 | break; |
| 1061 | case KVM_REG_PPC_VSCR: |
| 1062 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1063 | r = -ENXIO; |
| 1064 | break; |
| 1065 | } |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1066 | vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val); |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1067 | break; |
| 1068 | case KVM_REG_PPC_VRSAVE: |
Greg Kurz | b4d7f16 | 2016-01-13 18:28:17 +0100 | [diff] [blame] | 1069 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 1070 | r = -ENXIO; |
| 1071 | break; |
| 1072 | } |
| 1073 | vcpu->arch.vrsave = set_reg_val(reg->id, val); |
Mihai Caraman | 3840edc | 2014-08-20 16:36:25 +0300 | [diff] [blame] | 1074 | break; |
| 1075 | #endif /* CONFIG_ALTIVEC */ |
Mihai Caraman | 8a41ea5 | 2014-08-20 16:36:24 +0300 | [diff] [blame] | 1076 | default: |
| 1077 | r = -EINVAL; |
| 1078 | break; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | return r; |
| 1083 | } |
| 1084 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1085 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 1086 | { |
| 1087 | int r; |
| 1088 | sigset_t sigsaved; |
| 1089 | |
| 1090 | if (vcpu->sigset_active) |
| 1091 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 1092 | |
| 1093 | if (vcpu->mmio_needed) { |
| 1094 | if (!vcpu->mmio_is_write) |
| 1095 | kvmppc_complete_mmio_load(vcpu, run); |
| 1096 | vcpu->mmio_needed = 0; |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 1097 | } else if (vcpu->arch.osi_needed) { |
| 1098 | u64 *gprs = run->osi.gprs; |
| 1099 | int i; |
| 1100 | |
| 1101 | for (i = 0; i < 32; i++) |
| 1102 | kvmppc_set_gpr(vcpu, i, gprs[i]); |
| 1103 | vcpu->arch.osi_needed = 0; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1104 | } else if (vcpu->arch.hcall_needed) { |
| 1105 | int i; |
| 1106 | |
| 1107 | kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret); |
| 1108 | for (i = 0; i < 9; ++i) |
| 1109 | kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]); |
| 1110 | vcpu->arch.hcall_needed = 0; |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 1111 | #ifdef CONFIG_BOOKE |
| 1112 | } else if (vcpu->arch.epr_needed) { |
| 1113 | kvmppc_set_epr(vcpu, run->epr.epr); |
| 1114 | vcpu->arch.epr_needed = 0; |
| 1115 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1116 | } |
| 1117 | |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 1118 | if (run->immediate_exit) |
| 1119 | r = -EINTR; |
| 1120 | else |
| 1121 | r = kvmppc_vcpu_run(run, vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1122 | |
| 1123 | if (vcpu->sigset_active) |
| 1124 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 1125 | |
| 1126 | return r; |
| 1127 | } |
| 1128 | |
| 1129 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) |
| 1130 | { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1131 | if (irq->irq == KVM_INTERRUPT_UNSET) { |
Paul Mackerras | 4fe27d2 | 2013-02-14 14:00:25 +0000 | [diff] [blame] | 1132 | kvmppc_core_dequeue_external(vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1133 | return 0; |
| 1134 | } |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 1135 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1136 | kvmppc_core_queue_external(vcpu, irq); |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 1137 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 1138 | kvm_vcpu_kick(vcpu); |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 1139 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1140 | return 0; |
| 1141 | } |
| 1142 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 1143 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
| 1144 | struct kvm_enable_cap *cap) |
| 1145 | { |
| 1146 | int r; |
| 1147 | |
| 1148 | if (cap->flags) |
| 1149 | return -EINVAL; |
| 1150 | |
| 1151 | switch (cap->cap) { |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 1152 | case KVM_CAP_PPC_OSI: |
| 1153 | r = 0; |
| 1154 | vcpu->arch.osi_enabled = true; |
| 1155 | break; |
Alexander Graf | 930b412 | 2011-08-08 17:29:42 +0200 | [diff] [blame] | 1156 | case KVM_CAP_PPC_PAPR: |
| 1157 | r = 0; |
| 1158 | vcpu->arch.papr_enabled = true; |
| 1159 | break; |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 1160 | case KVM_CAP_PPC_EPR: |
| 1161 | r = 0; |
Scott Wood | 5df554ad | 2013-04-12 14:08:46 +0000 | [diff] [blame] | 1162 | if (cap->args[0]) |
| 1163 | vcpu->arch.epr_flags |= KVMPPC_EPR_USER; |
| 1164 | else |
| 1165 | vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER; |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 1166 | break; |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 1167 | #ifdef CONFIG_BOOKE |
| 1168 | case KVM_CAP_PPC_BOOKE_WATCHDOG: |
| 1169 | r = 0; |
| 1170 | vcpu->arch.watchdog_enabled = true; |
| 1171 | break; |
| 1172 | #endif |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 1173 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1174 | case KVM_CAP_SW_TLB: { |
| 1175 | struct kvm_config_tlb cfg; |
| 1176 | void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0]; |
| 1177 | |
| 1178 | r = -EFAULT; |
| 1179 | if (copy_from_user(&cfg, user_ptr, sizeof(cfg))) |
| 1180 | break; |
| 1181 | |
| 1182 | r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg); |
| 1183 | break; |
| 1184 | } |
| 1185 | #endif |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1186 | #ifdef CONFIG_KVM_MPIC |
| 1187 | case KVM_CAP_IRQ_MPIC: { |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1188 | struct fd f; |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1189 | struct kvm_device *dev; |
| 1190 | |
| 1191 | r = -EBADF; |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1192 | f = fdget(cap->args[0]); |
| 1193 | if (!f.file) |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1194 | break; |
| 1195 | |
| 1196 | r = -EPERM; |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1197 | dev = kvm_device_from_filp(f.file); |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1198 | if (dev) |
| 1199 | r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]); |
| 1200 | |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1201 | fdput(f); |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 1202 | break; |
| 1203 | } |
| 1204 | #endif |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1205 | #ifdef CONFIG_KVM_XICS |
| 1206 | case KVM_CAP_IRQ_XICS: { |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1207 | struct fd f; |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1208 | struct kvm_device *dev; |
| 1209 | |
| 1210 | r = -EBADF; |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1211 | f = fdget(cap->args[0]); |
| 1212 | if (!f.file) |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1213 | break; |
| 1214 | |
| 1215 | r = -EPERM; |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1216 | dev = kvm_device_from_filp(f.file); |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1217 | if (dev) |
| 1218 | r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]); |
| 1219 | |
Al Viro | 70abade | 2013-08-30 15:04:22 -0400 | [diff] [blame] | 1220 | fdput(f); |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 1221 | break; |
| 1222 | } |
| 1223 | #endif /* CONFIG_KVM_XICS */ |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 1224 | default: |
| 1225 | r = -EINVAL; |
| 1226 | break; |
| 1227 | } |
| 1228 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1229 | if (!r) |
| 1230 | r = kvmppc_sanity_check(vcpu); |
| 1231 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 1232 | return r; |
| 1233 | } |
| 1234 | |
Paul Mackerras | 34a75b0 | 2016-08-10 11:27:27 +1000 | [diff] [blame] | 1235 | bool kvm_arch_intc_initialized(struct kvm *kvm) |
| 1236 | { |
| 1237 | #ifdef CONFIG_KVM_MPIC |
| 1238 | if (kvm->arch.mpic) |
| 1239 | return true; |
| 1240 | #endif |
| 1241 | #ifdef CONFIG_KVM_XICS |
| 1242 | if (kvm->arch.xics) |
| 1243 | return true; |
| 1244 | #endif |
| 1245 | return false; |
| 1246 | } |
| 1247 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1248 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 1249 | struct kvm_mp_state *mp_state) |
| 1250 | { |
| 1251 | return -EINVAL; |
| 1252 | } |
| 1253 | |
| 1254 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 1255 | struct kvm_mp_state *mp_state) |
| 1256 | { |
| 1257 | return -EINVAL; |
| 1258 | } |
| 1259 | |
| 1260 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 1261 | unsigned int ioctl, unsigned long arg) |
| 1262 | { |
| 1263 | struct kvm_vcpu *vcpu = filp->private_data; |
| 1264 | void __user *argp = (void __user *)arg; |
| 1265 | long r; |
| 1266 | |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 1267 | switch (ioctl) { |
| 1268 | case KVM_INTERRUPT: { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1269 | struct kvm_interrupt irq; |
| 1270 | r = -EFAULT; |
| 1271 | if (copy_from_user(&irq, argp, sizeof(irq))) |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 1272 | goto out; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1273 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 1274 | goto out; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1275 | } |
Avi Kivity | 19483d1 | 2010-05-13 12:30:43 +0300 | [diff] [blame] | 1276 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 1277 | case KVM_ENABLE_CAP: |
| 1278 | { |
| 1279 | struct kvm_enable_cap cap; |
| 1280 | r = -EFAULT; |
| 1281 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 1282 | goto out; |
| 1283 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); |
| 1284 | break; |
| 1285 | } |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1286 | |
Alexander Graf | e24ed81 | 2011-09-14 10:02:41 +0200 | [diff] [blame] | 1287 | case KVM_SET_ONE_REG: |
| 1288 | case KVM_GET_ONE_REG: |
| 1289 | { |
| 1290 | struct kvm_one_reg reg; |
| 1291 | r = -EFAULT; |
| 1292 | if (copy_from_user(®, argp, sizeof(reg))) |
| 1293 | goto out; |
| 1294 | if (ioctl == KVM_SET_ONE_REG) |
| 1295 | r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®); |
| 1296 | else |
| 1297 | r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®); |
| 1298 | break; |
| 1299 | } |
| 1300 | |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 1301 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1302 | case KVM_DIRTY_TLB: { |
| 1303 | struct kvm_dirty_tlb dirty; |
| 1304 | r = -EFAULT; |
| 1305 | if (copy_from_user(&dirty, argp, sizeof(dirty))) |
| 1306 | goto out; |
| 1307 | r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty); |
| 1308 | break; |
| 1309 | } |
| 1310 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1311 | default: |
| 1312 | r = -EINVAL; |
| 1313 | } |
| 1314 | |
| 1315 | out: |
| 1316 | return r; |
| 1317 | } |
| 1318 | |
Carsten Otte | 5b1c149 | 2012-01-04 10:25:23 +0100 | [diff] [blame] | 1319 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
| 1320 | { |
| 1321 | return VM_FAULT_SIGBUS; |
| 1322 | } |
| 1323 | |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 1324 | static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo) |
| 1325 | { |
Stuart Yoder | 784bafa | 2012-07-03 05:48:51 +0000 | [diff] [blame] | 1326 | u32 inst_nop = 0x60000000; |
| 1327 | #ifdef CONFIG_KVM_BOOKE_HV |
| 1328 | u32 inst_sc1 = 0x44000022; |
Alexander Graf | 2743103 | 2014-04-24 13:39:16 +0200 | [diff] [blame] | 1329 | pvinfo->hcall[0] = cpu_to_be32(inst_sc1); |
| 1330 | pvinfo->hcall[1] = cpu_to_be32(inst_nop); |
| 1331 | pvinfo->hcall[2] = cpu_to_be32(inst_nop); |
| 1332 | pvinfo->hcall[3] = cpu_to_be32(inst_nop); |
Stuart Yoder | 784bafa | 2012-07-03 05:48:51 +0000 | [diff] [blame] | 1333 | #else |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 1334 | u32 inst_lis = 0x3c000000; |
| 1335 | u32 inst_ori = 0x60000000; |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 1336 | u32 inst_sc = 0x44000002; |
| 1337 | u32 inst_imm_mask = 0xffff; |
| 1338 | |
| 1339 | /* |
| 1340 | * The hypercall to get into KVM from within guest context is as |
| 1341 | * follows: |
| 1342 | * |
| 1343 | * lis r0, r0, KVM_SC_MAGIC_R0@h |
| 1344 | * ori r0, KVM_SC_MAGIC_R0@l |
| 1345 | * sc |
| 1346 | * nop |
| 1347 | */ |
Alexander Graf | 2743103 | 2014-04-24 13:39:16 +0200 | [diff] [blame] | 1348 | pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask)); |
| 1349 | pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask)); |
| 1350 | pvinfo->hcall[2] = cpu_to_be32(inst_sc); |
| 1351 | pvinfo->hcall[3] = cpu_to_be32(inst_nop); |
Stuart Yoder | 784bafa | 2012-07-03 05:48:51 +0000 | [diff] [blame] | 1352 | #endif |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 1353 | |
Liu Yu-B13201 | 9202e07 | 2012-07-03 05:48:52 +0000 | [diff] [blame] | 1354 | pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE; |
| 1355 | |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 1356 | return 0; |
| 1357 | } |
| 1358 | |
Alexander Graf | 5efdb4b | 2013-04-17 00:37:57 +0200 | [diff] [blame] | 1359 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, |
| 1360 | bool line_status) |
| 1361 | { |
| 1362 | if (!irqchip_in_kernel(kvm)) |
| 1363 | return -ENXIO; |
| 1364 | |
| 1365 | irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
| 1366 | irq_event->irq, irq_event->level, |
| 1367 | line_status); |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 1371 | |
| 1372 | static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
| 1373 | struct kvm_enable_cap *cap) |
| 1374 | { |
| 1375 | int r; |
| 1376 | |
| 1377 | if (cap->flags) |
| 1378 | return -EINVAL; |
| 1379 | |
| 1380 | switch (cap->cap) { |
| 1381 | #ifdef CONFIG_KVM_BOOK3S_64_HANDLER |
| 1382 | case KVM_CAP_PPC_ENABLE_HCALL: { |
| 1383 | unsigned long hcall = cap->args[0]; |
| 1384 | |
| 1385 | r = -EINVAL; |
| 1386 | if (hcall > MAX_HCALL_OPCODE || (hcall & 3) || |
| 1387 | cap->args[1] > 1) |
| 1388 | break; |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 1389 | if (!kvmppc_book3s_hcall_implemented(kvm, hcall)) |
| 1390 | break; |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 1391 | if (cap->args[1]) |
| 1392 | set_bit(hcall / 4, kvm->arch.enabled_hcalls); |
| 1393 | else |
| 1394 | clear_bit(hcall / 4, kvm->arch.enabled_hcalls); |
| 1395 | r = 0; |
| 1396 | break; |
| 1397 | } |
| 1398 | #endif |
| 1399 | default: |
| 1400 | r = -EINVAL; |
| 1401 | break; |
| 1402 | } |
| 1403 | |
| 1404 | return r; |
| 1405 | } |
| 1406 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1407 | long kvm_arch_vm_ioctl(struct file *filp, |
| 1408 | unsigned int ioctl, unsigned long arg) |
| 1409 | { |
Scott Wood | 5df554ad | 2013-04-12 14:08:46 +0000 | [diff] [blame] | 1410 | struct kvm *kvm __maybe_unused = filp->private_data; |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 1411 | void __user *argp = (void __user *)arg; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1412 | long r; |
| 1413 | |
| 1414 | switch (ioctl) { |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 1415 | case KVM_PPC_GET_PVINFO: { |
| 1416 | struct kvm_ppc_pvinfo pvinfo; |
Vasiliy Kulikov | d8cdddc | 2010-10-30 13:04:24 +0400 | [diff] [blame] | 1417 | memset(&pvinfo, 0, sizeof(pvinfo)); |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 1418 | r = kvm_vm_ioctl_get_pvinfo(&pvinfo); |
| 1419 | if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) { |
| 1420 | r = -EFAULT; |
| 1421 | goto out; |
| 1422 | } |
| 1423 | |
| 1424 | break; |
| 1425 | } |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 1426 | case KVM_ENABLE_CAP: |
| 1427 | { |
| 1428 | struct kvm_enable_cap cap; |
| 1429 | r = -EFAULT; |
| 1430 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 1431 | goto out; |
| 1432 | r = kvm_vm_ioctl_enable_cap(kvm, &cap); |
| 1433 | break; |
| 1434 | } |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 1435 | #ifdef CONFIG_PPC_BOOK3S_64 |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 1436 | case KVM_CREATE_SPAPR_TCE_64: { |
| 1437 | struct kvm_create_spapr_tce_64 create_tce_64; |
| 1438 | |
| 1439 | r = -EFAULT; |
| 1440 | if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64))) |
| 1441 | goto out; |
| 1442 | if (create_tce_64.flags) { |
| 1443 | r = -EINVAL; |
| 1444 | goto out; |
| 1445 | } |
| 1446 | r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64); |
| 1447 | goto out; |
| 1448 | } |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 1449 | case KVM_CREATE_SPAPR_TCE: { |
| 1450 | struct kvm_create_spapr_tce create_tce; |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 1451 | struct kvm_create_spapr_tce_64 create_tce_64; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 1452 | |
| 1453 | r = -EFAULT; |
| 1454 | if (copy_from_user(&create_tce, argp, sizeof(create_tce))) |
| 1455 | goto out; |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 1456 | |
| 1457 | create_tce_64.liobn = create_tce.liobn; |
| 1458 | create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K; |
| 1459 | create_tce_64.offset = 0; |
| 1460 | create_tce_64.size = create_tce.window_size >> |
| 1461 | IOMMU_PAGE_SHIFT_4K; |
| 1462 | create_tce_64.flags = 0; |
| 1463 | r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64); |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 1464 | goto out; |
| 1465 | } |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1466 | case KVM_PPC_GET_SMMU_INFO: { |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1467 | struct kvm_ppc_smmu_info info; |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1468 | struct kvm *kvm = filp->private_data; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1469 | |
| 1470 | memset(&info, 0, sizeof(info)); |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1471 | r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info); |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1472 | if (r >= 0 && copy_to_user(argp, &info, sizeof(info))) |
| 1473 | r = -EFAULT; |
| 1474 | break; |
| 1475 | } |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 1476 | case KVM_PPC_RTAS_DEFINE_TOKEN: { |
| 1477 | struct kvm *kvm = filp->private_data; |
| 1478 | |
| 1479 | r = kvm_vm_ioctl_rtas_define_token(kvm, argp); |
| 1480 | break; |
| 1481 | } |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 1482 | case KVM_PPC_CONFIGURE_V3_MMU: { |
| 1483 | struct kvm *kvm = filp->private_data; |
| 1484 | struct kvm_ppc_mmuv3_cfg cfg; |
| 1485 | |
| 1486 | r = -EINVAL; |
| 1487 | if (!kvm->arch.kvm_ops->configure_mmu) |
| 1488 | goto out; |
| 1489 | r = -EFAULT; |
| 1490 | if (copy_from_user(&cfg, argp, sizeof(cfg))) |
| 1491 | goto out; |
| 1492 | r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg); |
| 1493 | break; |
| 1494 | } |
| 1495 | case KVM_PPC_GET_RMMU_INFO: { |
| 1496 | struct kvm *kvm = filp->private_data; |
| 1497 | struct kvm_ppc_rmmu_info info; |
| 1498 | |
| 1499 | r = -EINVAL; |
| 1500 | if (!kvm->arch.kvm_ops->get_rmmu_info) |
| 1501 | goto out; |
| 1502 | r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info); |
| 1503 | if (r >= 0 && copy_to_user(argp, &info, sizeof(info))) |
| 1504 | r = -EFAULT; |
| 1505 | break; |
| 1506 | } |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1507 | default: { |
| 1508 | struct kvm *kvm = filp->private_data; |
| 1509 | r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg); |
| 1510 | } |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1511 | #else /* CONFIG_PPC_BOOK3S_64 */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1512 | default: |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 1513 | r = -ENOTTY; |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1514 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1515 | } |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 1516 | out: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1517 | return r; |
| 1518 | } |
| 1519 | |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 1520 | static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)]; |
| 1521 | static unsigned long nr_lpids; |
| 1522 | |
| 1523 | long kvmppc_alloc_lpid(void) |
| 1524 | { |
| 1525 | long lpid; |
| 1526 | |
| 1527 | do { |
| 1528 | lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS); |
| 1529 | if (lpid >= nr_lpids) { |
| 1530 | pr_err("%s: No LPIDs free\n", __func__); |
| 1531 | return -ENOMEM; |
| 1532 | } |
| 1533 | } while (test_and_set_bit(lpid, lpid_inuse)); |
| 1534 | |
| 1535 | return lpid; |
| 1536 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 1537 | EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid); |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 1538 | |
| 1539 | void kvmppc_claim_lpid(long lpid) |
| 1540 | { |
| 1541 | set_bit(lpid, lpid_inuse); |
| 1542 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 1543 | EXPORT_SYMBOL_GPL(kvmppc_claim_lpid); |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 1544 | |
| 1545 | void kvmppc_free_lpid(long lpid) |
| 1546 | { |
| 1547 | clear_bit(lpid, lpid_inuse); |
| 1548 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 1549 | EXPORT_SYMBOL_GPL(kvmppc_free_lpid); |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 1550 | |
| 1551 | void kvmppc_init_lpid(unsigned long nr_lpids_param) |
| 1552 | { |
| 1553 | nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param); |
| 1554 | memset(lpid_inuse, 0, sizeof(lpid_inuse)); |
| 1555 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 1556 | EXPORT_SYMBOL_GPL(kvmppc_init_lpid); |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 1557 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1558 | int kvm_arch_init(void *opaque) |
| 1559 | { |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
Paolo Bonzini | 478d6686 | 2014-08-05 11:29:07 +0200 | [diff] [blame] | 1563 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr); |