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> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 26 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 28 | #include <asm/cputable.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/kvm_ppc.h> |
Hollis Blanchard | 83aae4a | 2008-07-25 13:54:52 -0500 | [diff] [blame] | 31 | #include <asm/tlbflush.h> |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 32 | #include <asm/cputhreads.h> |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 33 | #include "timing.h" |
Paul Mackerras | fad7b9b | 2008-12-23 14:57:26 +1100 | [diff] [blame] | 34 | #include "../mm/mmu_decl.h" |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 35 | |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 36 | #define CREATE_TRACE_POINTS |
| 37 | #include "trace.h" |
| 38 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 39 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
| 40 | { |
Liu Yu-B13201 | 9202e07 | 2012-07-03 05:48:52 +0000 | [diff] [blame] | 41 | return !!(v->arch.pending_exceptions) || |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 42 | v->requests; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 45 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 46 | { |
| 47 | return 1; |
| 48 | } |
| 49 | |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame^] | 50 | #ifndef CONFIG_KVM_BOOK3S_64_HV |
| 51 | /* |
| 52 | * Common checks before entering the guest world. Call with interrupts |
| 53 | * disabled. |
| 54 | * |
| 55 | * returns !0 if a signal is pending and check_signal is true |
| 56 | */ |
| 57 | int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu) |
| 58 | { |
| 59 | int r = 0; |
| 60 | |
| 61 | WARN_ON_ONCE(!irqs_disabled()); |
| 62 | while (true) { |
| 63 | if (need_resched()) { |
| 64 | local_irq_enable(); |
| 65 | cond_resched(); |
| 66 | local_irq_disable(); |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | if (signal_pending(current)) { |
| 71 | r = 1; |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | smp_mb(); |
| 76 | if (vcpu->requests) { |
| 77 | /* Make sure we process requests preemptable */ |
| 78 | local_irq_enable(); |
| 79 | trace_kvm_check_requests(vcpu); |
| 80 | kvmppc_core_check_requests(vcpu); |
| 81 | local_irq_disable(); |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | if (kvmppc_core_prepare_to_enter(vcpu)) { |
| 86 | /* interrupts got enabled in between, so we |
| 87 | are back at square 1 */ |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | if (vcpu->mode == EXITING_GUEST_MODE) { |
| 92 | r = 1; |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | /* Going into guest context! Yay! */ |
| 97 | vcpu->mode = IN_GUEST_MODE; |
| 98 | smp_wmb(); |
| 99 | |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | return r; |
| 104 | } |
| 105 | #endif /* CONFIG_KVM_BOOK3S_64_HV */ |
| 106 | |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 107 | int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) |
| 108 | { |
| 109 | int nr = kvmppc_get_gpr(vcpu, 11); |
| 110 | int r; |
| 111 | unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3); |
| 112 | unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4); |
| 113 | unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5); |
| 114 | unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6); |
| 115 | unsigned long r2 = 0; |
| 116 | |
| 117 | if (!(vcpu->arch.shared->msr & MSR_SF)) { |
| 118 | /* 32 bit mode */ |
| 119 | param1 &= 0xffffffff; |
| 120 | param2 &= 0xffffffff; |
| 121 | param3 &= 0xffffffff; |
| 122 | param4 &= 0xffffffff; |
| 123 | } |
| 124 | |
| 125 | switch (nr) { |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 126 | case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE): |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 127 | { |
| 128 | vcpu->arch.magic_page_pa = param1; |
| 129 | vcpu->arch.magic_page_ea = param2; |
| 130 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 131 | r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7; |
Alexander Graf | 7508e16 | 2010-08-03 11:32:56 +0200 | [diff] [blame] | 132 | |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 133 | r = EV_SUCCESS; |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 134 | break; |
| 135 | } |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 136 | case KVM_HCALL_TOKEN(KVM_HC_FEATURES): |
| 137 | r = EV_SUCCESS; |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 138 | #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2) |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 139 | /* XXX Missing magic page on 44x */ |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 140 | r2 |= (1 << KVM_FEATURE_MAGIC_PAGE); |
| 141 | #endif |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 142 | |
| 143 | /* Second return value is in r4 */ |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 144 | break; |
Liu Yu-B13201 | 9202e07 | 2012-07-03 05:48:52 +0000 | [diff] [blame] | 145 | case EV_HCALL_TOKEN(EV_IDLE): |
| 146 | r = EV_SUCCESS; |
| 147 | kvm_vcpu_block(vcpu); |
| 148 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
| 149 | break; |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 150 | default: |
Stuart Yoder | fdcf8bd | 2012-07-03 05:48:50 +0000 | [diff] [blame] | 151 | r = EV_UNIMPLEMENTED; |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | |
Alexander Graf | 7508e16 | 2010-08-03 11:32:56 +0200 | [diff] [blame] | 155 | kvmppc_set_gpr(vcpu, 4, r2); |
| 156 | |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 157 | return r; |
| 158 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 159 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 160 | int kvmppc_sanity_check(struct kvm_vcpu *vcpu) |
| 161 | { |
| 162 | int r = false; |
| 163 | |
| 164 | /* We have to know what CPU to virtualize */ |
| 165 | if (!vcpu->arch.pvr) |
| 166 | goto out; |
| 167 | |
| 168 | /* PAPR only works with book3s_64 */ |
| 169 | if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled) |
| 170 | goto out; |
| 171 | |
| 172 | #ifdef CONFIG_KVM_BOOK3S_64_HV |
| 173 | /* HV KVM can only do PAPR mode for now */ |
| 174 | if (!vcpu->arch.papr_enabled) |
| 175 | goto out; |
| 176 | #endif |
| 177 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 178 | #ifdef CONFIG_KVM_BOOKE_HV |
| 179 | if (!cpu_has_feature(CPU_FTR_EMB_HV)) |
| 180 | goto out; |
| 181 | #endif |
| 182 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 183 | r = true; |
| 184 | |
| 185 | out: |
| 186 | vcpu->arch.sane = r; |
| 187 | return r ? 0 : -EINVAL; |
| 188 | } |
| 189 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 190 | int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 191 | { |
| 192 | enum emulation_result er; |
| 193 | int r; |
| 194 | |
| 195 | er = kvmppc_emulate_instruction(run, vcpu); |
| 196 | switch (er) { |
| 197 | case EMULATE_DONE: |
| 198 | /* Future optimization: only reload non-volatiles if they were |
| 199 | * actually modified. */ |
| 200 | r = RESUME_GUEST_NV; |
| 201 | break; |
| 202 | case EMULATE_DO_MMIO: |
| 203 | run->exit_reason = KVM_EXIT_MMIO; |
| 204 | /* We must reload nonvolatiles because "update" load/store |
| 205 | * instructions modify register state. */ |
| 206 | /* Future optimization: only reload non-volatiles if they were |
| 207 | * actually modified. */ |
| 208 | r = RESUME_HOST_NV; |
| 209 | break; |
| 210 | case EMULATE_FAIL: |
| 211 | /* XXX Deliver Program interrupt to guest. */ |
| 212 | printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__, |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 213 | kvmppc_get_last_inst(vcpu)); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 214 | r = RESUME_HOST; |
| 215 | break; |
| 216 | default: |
| 217 | BUG(); |
| 218 | } |
| 219 | |
| 220 | return r; |
| 221 | } |
| 222 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 223 | int kvm_arch_hardware_enable(void *garbage) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 224 | { |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 225 | return 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void kvm_arch_hardware_disable(void *garbage) |
| 229 | { |
| 230 | } |
| 231 | |
| 232 | int kvm_arch_hardware_setup(void) |
| 233 | { |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | void kvm_arch_hardware_unsetup(void) |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | void kvm_arch_check_processor_compat(void *rtn) |
| 242 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 243 | *(int *)rtn = kvmppc_core_check_processor_compat(); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 246 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 247 | { |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 248 | if (type) |
| 249 | return -EINVAL; |
| 250 | |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 251 | return kvmppc_core_init_vm(kvm); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 254 | void kvm_arch_destroy_vm(struct kvm *kvm) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 255 | { |
| 256 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 257 | struct kvm_vcpu *vcpu; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 258 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 259 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 260 | kvm_arch_vcpu_free(vcpu); |
| 261 | |
| 262 | mutex_lock(&kvm->lock); |
| 263 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 264 | kvm->vcpus[i] = NULL; |
| 265 | |
| 266 | atomic_set(&kvm->online_vcpus, 0); |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 267 | |
| 268 | kvmppc_core_destroy_vm(kvm); |
| 269 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 270 | mutex_unlock(&kvm->lock); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 273 | void kvm_arch_sync_events(struct kvm *kvm) |
| 274 | { |
| 275 | } |
| 276 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 277 | int kvm_dev_ioctl_check_extension(long ext) |
| 278 | { |
| 279 | int r; |
| 280 | |
| 281 | switch (ext) { |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 282 | #ifdef CONFIG_BOOKE |
| 283 | case KVM_CAP_PPC_BOOKE_SREGS: |
| 284 | #else |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 285 | case KVM_CAP_PPC_SEGSTATE: |
Alexander Graf | 1022fc3 | 2011-09-14 21:45:23 +0200 | [diff] [blame] | 286 | case KVM_CAP_PPC_HIOR: |
Alexander Graf | 930b412 | 2011-08-08 17:29:42 +0200 | [diff] [blame] | 287 | case KVM_CAP_PPC_PAPR: |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 288 | #endif |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 289 | case KVM_CAP_PPC_UNSET_IRQ: |
Alexander Graf | 7b4203e | 2010-08-30 13:50:45 +0200 | [diff] [blame] | 290 | case KVM_CAP_PPC_IRQ_LEVEL: |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 291 | case KVM_CAP_ENABLE_CAP: |
Alexander Graf | e24ed81 | 2011-09-14 10:02:41 +0200 | [diff] [blame] | 292 | case KVM_CAP_ONE_REG: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 293 | r = 1; |
| 294 | break; |
| 295 | #ifndef CONFIG_KVM_BOOK3S_64_HV |
| 296 | case KVM_CAP_PPC_PAIRED_SINGLES: |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 297 | case KVM_CAP_PPC_OSI: |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 298 | case KVM_CAP_PPC_GET_PVINFO: |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 299 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 300 | case KVM_CAP_SW_TLB: |
| 301 | #endif |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 302 | r = 1; |
| 303 | break; |
Laurent Vivier | 588968b | 2008-05-30 16:05:56 +0200 | [diff] [blame] | 304 | case KVM_CAP_COALESCED_MMIO: |
| 305 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 306 | break; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 307 | #endif |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 308 | #ifdef CONFIG_PPC_BOOK3S_64 |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 309 | case KVM_CAP_SPAPR_TCE: |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 310 | case KVM_CAP_PPC_ALLOC_HTAB: |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 311 | r = 1; |
| 312 | break; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 313 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
| 314 | #ifdef CONFIG_KVM_BOOK3S_64_HV |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 315 | case KVM_CAP_PPC_SMT: |
| 316 | r = threads_per_core; |
| 317 | break; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 318 | case KVM_CAP_PPC_RMA: |
| 319 | r = 1; |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 320 | /* PPC970 requires an RMA */ |
| 321 | if (cpu_has_feature(CPU_FTR_ARCH_201)) |
| 322 | r = 2; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 323 | break; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 324 | #endif |
Alexander Graf | f4800b1 | 2012-08-07 10:24:14 +0200 | [diff] [blame] | 325 | case KVM_CAP_SYNC_MMU: |
| 326 | #ifdef CONFIG_KVM_BOOK3S_64_HV |
| 327 | r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0; |
| 328 | #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 329 | r = 1; |
| 330 | #else |
| 331 | r = 0; |
| 332 | #endif |
| 333 | break; |
Matt Evans | b543403 | 2011-12-07 16:55:57 +0000 | [diff] [blame] | 334 | case KVM_CAP_NR_VCPUS: |
| 335 | /* |
| 336 | * Recommending a number of CPUs is somewhat arbitrary; we |
| 337 | * return the number of present CPUs for -HV (since a host |
| 338 | * will have secondary threads "offline"), and for other KVM |
| 339 | * implementations just count online CPUs. |
| 340 | */ |
| 341 | #ifdef CONFIG_KVM_BOOK3S_64_HV |
| 342 | r = num_present_cpus(); |
| 343 | #else |
| 344 | r = num_online_cpus(); |
| 345 | #endif |
| 346 | break; |
| 347 | case KVM_CAP_MAX_VCPUS: |
| 348 | r = KVM_MAX_VCPUS; |
| 349 | break; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 350 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 351 | case KVM_CAP_PPC_GET_SMMU_INFO: |
| 352 | r = 1; |
| 353 | break; |
| 354 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 355 | default: |
| 356 | r = 0; |
| 357 | break; |
| 358 | } |
| 359 | return r; |
| 360 | |
| 361 | } |
| 362 | |
| 363 | long kvm_arch_dev_ioctl(struct file *filp, |
| 364 | unsigned int ioctl, unsigned long arg) |
| 365 | { |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 369 | void kvm_arch_free_memslot(struct kvm_memory_slot *free, |
| 370 | struct kvm_memory_slot *dont) |
| 371 | { |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 372 | if (!dont || free->arch.rmap != dont->arch.rmap) { |
| 373 | vfree(free->arch.rmap); |
| 374 | free->arch.rmap = NULL; |
| 375 | } |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages) |
| 379 | { |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 380 | slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap)); |
| 381 | if (!slot->arch.rmap) |
| 382 | return -ENOMEM; |
| 383 | |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 384 | return 0; |
| 385 | } |
| 386 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 387 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 388 | struct kvm_memory_slot *memslot, |
| 389 | struct kvm_memory_slot old, |
| 390 | struct kvm_userspace_memory_region *mem, |
| 391 | int user_alloc) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 392 | { |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 393 | return kvmppc_core_prepare_memory_region(kvm, mem); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 394 | } |
| 395 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 396 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 397 | struct kvm_userspace_memory_region *mem, |
| 398 | struct kvm_memory_slot old, |
| 399 | int user_alloc) |
| 400 | { |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 401 | kvmppc_core_commit_memory_region(kvm, mem); |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 402 | } |
| 403 | |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 404 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
| 405 | { |
| 406 | } |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 407 | |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 408 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 409 | struct kvm_memory_slot *slot) |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 410 | { |
| 411 | } |
| 412 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 413 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
| 414 | { |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 415 | struct kvm_vcpu *vcpu; |
| 416 | vcpu = kvmppc_core_vcpu_create(kvm, id); |
Matt Evans | 03cdab5 | 2011-12-06 21:19:42 +0000 | [diff] [blame] | 417 | if (!IS_ERR(vcpu)) { |
| 418 | vcpu->arch.wqp = &vcpu->wq; |
Wei Yongjun | 06056bf | 2010-03-09 14:13:43 +0800 | [diff] [blame] | 419 | kvmppc_create_vcpu_debugfs(vcpu, id); |
Matt Evans | 03cdab5 | 2011-12-06 21:19:42 +0000 | [diff] [blame] | 420 | } |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 421 | return vcpu; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 425 | { |
Alexander Graf | a595405 | 2010-02-22 16:52:14 +0100 | [diff] [blame] | 426 | /* Make sure we're not using the vcpu anymore */ |
| 427 | hrtimer_cancel(&vcpu->arch.dec_timer); |
| 428 | tasklet_kill(&vcpu->arch.tasklet); |
| 429 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 430 | kvmppc_remove_vcpu_debugfs(vcpu); |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 431 | kvmppc_core_vcpu_free(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 435 | { |
| 436 | kvm_arch_vcpu_free(vcpu); |
| 437 | } |
| 438 | |
| 439 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 440 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 441 | return kvmppc_core_pending_dec(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 444 | /* |
| 445 | * low level hrtimer wake routine. Because this runs in hardirq context |
| 446 | * we schedule a tasklet to do the real work. |
| 447 | */ |
| 448 | enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer) |
| 449 | { |
| 450 | struct kvm_vcpu *vcpu; |
| 451 | |
| 452 | vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer); |
| 453 | tasklet_schedule(&vcpu->arch.tasklet); |
| 454 | |
| 455 | return HRTIMER_NORESTART; |
| 456 | } |
| 457 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 458 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 459 | { |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 460 | hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); |
| 461 | tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu); |
| 462 | vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 463 | vcpu->arch.dec_expires = ~(u64)0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 464 | |
Bharat Bhushan | 09000ad | 2011-03-25 10:32:13 +0530 | [diff] [blame] | 465 | #ifdef CONFIG_KVM_EXIT_TIMING |
| 466 | mutex_init(&vcpu->arch.exit_timing_lock); |
| 467 | #endif |
| 468 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 473 | { |
Hollis Blanchard | ecc0981 | 2009-01-03 16:22:59 -0600 | [diff] [blame] | 474 | kvmppc_mmu_destroy(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 478 | { |
Scott Wood | eab1767 | 2011-04-27 17:24:10 -0500 | [diff] [blame] | 479 | #ifdef CONFIG_BOOKE |
| 480 | /* |
| 481 | * vrsave (formerly usprg0) isn't used by Linux, but may |
| 482 | * be used by the guest. |
| 483 | * |
| 484 | * On non-booke this is associated with Altivec and |
| 485 | * is handled by code in book3s.c. |
| 486 | */ |
| 487 | mtspr(SPRN_VRSAVE, vcpu->arch.vrsave); |
| 488 | #endif |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 489 | kvmppc_core_vcpu_load(vcpu, cpu); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 490 | vcpu->cpu = smp_processor_id(); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 494 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 495 | kvmppc_core_vcpu_put(vcpu); |
Scott Wood | eab1767 | 2011-04-27 17:24:10 -0500 | [diff] [blame] | 496 | #ifdef CONFIG_BOOKE |
| 497 | vcpu->arch.vrsave = mfspr(SPRN_VRSAVE); |
| 498 | #endif |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 499 | vcpu->cpu = -1; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 500 | } |
| 501 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 502 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
Hollis Blanchard | f5d0906 | 2009-01-04 13:51:09 -0600 | [diff] [blame] | 503 | struct kvm_guest_debug *dbg) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 504 | { |
Hollis Blanchard | f5d0906 | 2009-01-04 13:51:09 -0600 | [diff] [blame] | 505 | return -EINVAL; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu, |
| 509 | struct kvm_run *run) |
| 510 | { |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 511 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, |
| 515 | struct kvm_run *run) |
| 516 | { |
Denis Kirjanov | 69b6183 | 2010-06-11 11:23:26 +0000 | [diff] [blame] | 517 | u64 uninitialized_var(gpr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 518 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 519 | if (run->mmio.len > sizeof(gpr)) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 520 | printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | if (vcpu->arch.mmio_is_bigendian) { |
| 525 | switch (run->mmio.len) { |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 526 | case 8: gpr = *(u64 *)run->mmio.data; break; |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 527 | case 4: gpr = *(u32 *)run->mmio.data; break; |
| 528 | case 2: gpr = *(u16 *)run->mmio.data; break; |
| 529 | case 1: gpr = *(u8 *)run->mmio.data; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 530 | } |
| 531 | } else { |
| 532 | /* Convert BE data from userland back to LE. */ |
| 533 | switch (run->mmio.len) { |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 534 | case 4: gpr = ld_le32((u32 *)run->mmio.data); break; |
| 535 | case 2: gpr = ld_le16((u16 *)run->mmio.data); break; |
| 536 | case 1: gpr = *(u8 *)run->mmio.data; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 537 | } |
| 538 | } |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 539 | |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 540 | if (vcpu->arch.mmio_sign_extend) { |
| 541 | switch (run->mmio.len) { |
| 542 | #ifdef CONFIG_PPC64 |
| 543 | case 4: |
| 544 | gpr = (s64)(s32)gpr; |
| 545 | break; |
| 546 | #endif |
| 547 | case 2: |
| 548 | gpr = (s64)(s16)gpr; |
| 549 | break; |
| 550 | case 1: |
| 551 | gpr = (s64)(s8)gpr; |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 556 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 557 | |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 558 | switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) { |
| 559 | case KVM_MMIO_REG_GPR: |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 560 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
| 561 | break; |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 562 | case KVM_MMIO_REG_FPR: |
| 563 | vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 564 | break; |
Alexander Graf | 287d561 | 2010-04-01 15:33:21 +0200 | [diff] [blame] | 565 | #ifdef CONFIG_PPC_BOOK3S |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 566 | case KVM_MMIO_REG_QPR: |
| 567 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 568 | break; |
Alexander Graf | b3c5d3c | 2012-01-07 02:07:38 +0100 | [diff] [blame] | 569 | case KVM_MMIO_REG_FQPR: |
| 570 | vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; |
| 571 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 572 | break; |
Alexander Graf | 287d561 | 2010-04-01 15:33:21 +0200 | [diff] [blame] | 573 | #endif |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 574 | default: |
| 575 | BUG(); |
| 576 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 580 | unsigned int rt, unsigned int bytes, int is_bigendian) |
| 581 | { |
| 582 | if (bytes > sizeof(run->mmio.data)) { |
| 583 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, |
| 584 | run->mmio.len); |
| 585 | } |
| 586 | |
| 587 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; |
| 588 | run->mmio.len = bytes; |
| 589 | run->mmio.is_write = 0; |
| 590 | |
| 591 | vcpu->arch.io_gpr = rt; |
| 592 | vcpu->arch.mmio_is_bigendian = is_bigendian; |
| 593 | vcpu->mmio_needed = 1; |
| 594 | vcpu->mmio_is_write = 0; |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 595 | vcpu->arch.mmio_sign_extend = 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 596 | |
| 597 | return EMULATE_DO_MMIO; |
| 598 | } |
| 599 | |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 600 | /* Same as above, but sign extends */ |
| 601 | int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 602 | unsigned int rt, unsigned int bytes, int is_bigendian) |
| 603 | { |
| 604 | int r; |
| 605 | |
| 606 | r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian); |
| 607 | vcpu->arch.mmio_sign_extend = 1; |
| 608 | |
| 609 | return r; |
| 610 | } |
| 611 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 612 | int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 613 | u64 val, unsigned int bytes, int is_bigendian) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 614 | { |
| 615 | void *data = run->mmio.data; |
| 616 | |
| 617 | if (bytes > sizeof(run->mmio.data)) { |
| 618 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, |
| 619 | run->mmio.len); |
| 620 | } |
| 621 | |
| 622 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; |
| 623 | run->mmio.len = bytes; |
| 624 | run->mmio.is_write = 1; |
| 625 | vcpu->mmio_needed = 1; |
| 626 | vcpu->mmio_is_write = 1; |
| 627 | |
| 628 | /* Store the value at the lowest bytes in 'data'. */ |
| 629 | if (is_bigendian) { |
| 630 | switch (bytes) { |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 631 | case 8: *(u64 *)data = val; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 632 | case 4: *(u32 *)data = val; break; |
| 633 | case 2: *(u16 *)data = val; break; |
| 634 | case 1: *(u8 *)data = val; break; |
| 635 | } |
| 636 | } else { |
| 637 | /* Store LE value into 'data'. */ |
| 638 | switch (bytes) { |
| 639 | case 4: st_le32(data, val); break; |
| 640 | case 2: st_le16(data, val); break; |
| 641 | case 1: *(u8 *)data = val; break; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | return EMULATE_DO_MMIO; |
| 646 | } |
| 647 | |
| 648 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 649 | { |
| 650 | int r; |
| 651 | sigset_t sigsaved; |
| 652 | |
| 653 | if (vcpu->sigset_active) |
| 654 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 655 | |
| 656 | if (vcpu->mmio_needed) { |
| 657 | if (!vcpu->mmio_is_write) |
| 658 | kvmppc_complete_mmio_load(vcpu, run); |
| 659 | vcpu->mmio_needed = 0; |
| 660 | } else if (vcpu->arch.dcr_needed) { |
| 661 | if (!vcpu->arch.dcr_is_write) |
| 662 | kvmppc_complete_dcr_load(vcpu, run); |
| 663 | vcpu->arch.dcr_needed = 0; |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 664 | } else if (vcpu->arch.osi_needed) { |
| 665 | u64 *gprs = run->osi.gprs; |
| 666 | int i; |
| 667 | |
| 668 | for (i = 0; i < 32; i++) |
| 669 | kvmppc_set_gpr(vcpu, i, gprs[i]); |
| 670 | vcpu->arch.osi_needed = 0; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 671 | } else if (vcpu->arch.hcall_needed) { |
| 672 | int i; |
| 673 | |
| 674 | kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret); |
| 675 | for (i = 0; i < 9; ++i) |
| 676 | kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]); |
| 677 | vcpu->arch.hcall_needed = 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 678 | } |
| 679 | |
Paul Mackerras | df6909e5 | 2011-06-29 00:19:50 +0000 | [diff] [blame] | 680 | r = kvmppc_vcpu_run(run, vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 681 | |
| 682 | if (vcpu->sigset_active) |
| 683 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 684 | |
| 685 | return r; |
| 686 | } |
| 687 | |
| 688 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) |
| 689 | { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 690 | if (irq->irq == KVM_INTERRUPT_UNSET) { |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 691 | kvmppc_core_dequeue_external(vcpu, irq); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 692 | return 0; |
| 693 | } |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 694 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 695 | kvmppc_core_queue_external(vcpu, irq); |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 696 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 697 | kvm_vcpu_kick(vcpu); |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 698 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 702 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
| 703 | struct kvm_enable_cap *cap) |
| 704 | { |
| 705 | int r; |
| 706 | |
| 707 | if (cap->flags) |
| 708 | return -EINVAL; |
| 709 | |
| 710 | switch (cap->cap) { |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 711 | case KVM_CAP_PPC_OSI: |
| 712 | r = 0; |
| 713 | vcpu->arch.osi_enabled = true; |
| 714 | break; |
Alexander Graf | 930b412 | 2011-08-08 17:29:42 +0200 | [diff] [blame] | 715 | case KVM_CAP_PPC_PAPR: |
| 716 | r = 0; |
| 717 | vcpu->arch.papr_enabled = true; |
| 718 | break; |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 719 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 720 | case KVM_CAP_SW_TLB: { |
| 721 | struct kvm_config_tlb cfg; |
| 722 | void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0]; |
| 723 | |
| 724 | r = -EFAULT; |
| 725 | if (copy_from_user(&cfg, user_ptr, sizeof(cfg))) |
| 726 | break; |
| 727 | |
| 728 | r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg); |
| 729 | break; |
| 730 | } |
| 731 | #endif |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 732 | default: |
| 733 | r = -EINVAL; |
| 734 | break; |
| 735 | } |
| 736 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 737 | if (!r) |
| 738 | r = kvmppc_sanity_check(vcpu); |
| 739 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 740 | return r; |
| 741 | } |
| 742 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 743 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 744 | struct kvm_mp_state *mp_state) |
| 745 | { |
| 746 | return -EINVAL; |
| 747 | } |
| 748 | |
| 749 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 750 | struct kvm_mp_state *mp_state) |
| 751 | { |
| 752 | return -EINVAL; |
| 753 | } |
| 754 | |
| 755 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 756 | unsigned int ioctl, unsigned long arg) |
| 757 | { |
| 758 | struct kvm_vcpu *vcpu = filp->private_data; |
| 759 | void __user *argp = (void __user *)arg; |
| 760 | long r; |
| 761 | |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 762 | switch (ioctl) { |
| 763 | case KVM_INTERRUPT: { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 764 | struct kvm_interrupt irq; |
| 765 | r = -EFAULT; |
| 766 | if (copy_from_user(&irq, argp, sizeof(irq))) |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 767 | goto out; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 768 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 769 | goto out; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 770 | } |
Avi Kivity | 19483d1 | 2010-05-13 12:30:43 +0300 | [diff] [blame] | 771 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 772 | case KVM_ENABLE_CAP: |
| 773 | { |
| 774 | struct kvm_enable_cap cap; |
| 775 | r = -EFAULT; |
| 776 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 777 | goto out; |
| 778 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); |
| 779 | break; |
| 780 | } |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 781 | |
Alexander Graf | e24ed81 | 2011-09-14 10:02:41 +0200 | [diff] [blame] | 782 | case KVM_SET_ONE_REG: |
| 783 | case KVM_GET_ONE_REG: |
| 784 | { |
| 785 | struct kvm_one_reg reg; |
| 786 | r = -EFAULT; |
| 787 | if (copy_from_user(®, argp, sizeof(reg))) |
| 788 | goto out; |
| 789 | if (ioctl == KVM_SET_ONE_REG) |
| 790 | r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®); |
| 791 | else |
| 792 | r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®); |
| 793 | break; |
| 794 | } |
| 795 | |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 796 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 797 | case KVM_DIRTY_TLB: { |
| 798 | struct kvm_dirty_tlb dirty; |
| 799 | r = -EFAULT; |
| 800 | if (copy_from_user(&dirty, argp, sizeof(dirty))) |
| 801 | goto out; |
| 802 | r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty); |
| 803 | break; |
| 804 | } |
| 805 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 806 | default: |
| 807 | r = -EINVAL; |
| 808 | } |
| 809 | |
| 810 | out: |
| 811 | return r; |
| 812 | } |
| 813 | |
Carsten Otte | 5b1c149 | 2012-01-04 10:25:23 +0100 | [diff] [blame] | 814 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
| 815 | { |
| 816 | return VM_FAULT_SIGBUS; |
| 817 | } |
| 818 | |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 819 | static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo) |
| 820 | { |
Stuart Yoder | 784bafa | 2012-07-03 05:48:51 +0000 | [diff] [blame] | 821 | u32 inst_nop = 0x60000000; |
| 822 | #ifdef CONFIG_KVM_BOOKE_HV |
| 823 | u32 inst_sc1 = 0x44000022; |
| 824 | pvinfo->hcall[0] = inst_sc1; |
| 825 | pvinfo->hcall[1] = inst_nop; |
| 826 | pvinfo->hcall[2] = inst_nop; |
| 827 | pvinfo->hcall[3] = inst_nop; |
| 828 | #else |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 829 | u32 inst_lis = 0x3c000000; |
| 830 | u32 inst_ori = 0x60000000; |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 831 | u32 inst_sc = 0x44000002; |
| 832 | u32 inst_imm_mask = 0xffff; |
| 833 | |
| 834 | /* |
| 835 | * The hypercall to get into KVM from within guest context is as |
| 836 | * follows: |
| 837 | * |
| 838 | * lis r0, r0, KVM_SC_MAGIC_R0@h |
| 839 | * ori r0, KVM_SC_MAGIC_R0@l |
| 840 | * sc |
| 841 | * nop |
| 842 | */ |
| 843 | pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask); |
| 844 | pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask); |
| 845 | pvinfo->hcall[2] = inst_sc; |
| 846 | pvinfo->hcall[3] = inst_nop; |
Stuart Yoder | 784bafa | 2012-07-03 05:48:51 +0000 | [diff] [blame] | 847 | #endif |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 848 | |
Liu Yu-B13201 | 9202e07 | 2012-07-03 05:48:52 +0000 | [diff] [blame] | 849 | pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE; |
| 850 | |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 851 | return 0; |
| 852 | } |
| 853 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 854 | long kvm_arch_vm_ioctl(struct file *filp, |
| 855 | unsigned int ioctl, unsigned long arg) |
| 856 | { |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 857 | void __user *argp = (void __user *)arg; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 858 | long r; |
| 859 | |
| 860 | switch (ioctl) { |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 861 | case KVM_PPC_GET_PVINFO: { |
| 862 | struct kvm_ppc_pvinfo pvinfo; |
Vasiliy Kulikov | d8cdddc | 2010-10-30 13:04:24 +0400 | [diff] [blame] | 863 | memset(&pvinfo, 0, sizeof(pvinfo)); |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 864 | r = kvm_vm_ioctl_get_pvinfo(&pvinfo); |
| 865 | if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) { |
| 866 | r = -EFAULT; |
| 867 | goto out; |
| 868 | } |
| 869 | |
| 870 | break; |
| 871 | } |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 872 | #ifdef CONFIG_PPC_BOOK3S_64 |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 873 | case KVM_CREATE_SPAPR_TCE: { |
| 874 | struct kvm_create_spapr_tce create_tce; |
| 875 | struct kvm *kvm = filp->private_data; |
| 876 | |
| 877 | r = -EFAULT; |
| 878 | if (copy_from_user(&create_tce, argp, sizeof(create_tce))) |
| 879 | goto out; |
| 880 | r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce); |
| 881 | goto out; |
| 882 | } |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 883 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 884 | |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 885 | #ifdef CONFIG_KVM_BOOK3S_64_HV |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 886 | case KVM_ALLOCATE_RMA: { |
| 887 | struct kvm *kvm = filp->private_data; |
| 888 | struct kvm_allocate_rma rma; |
| 889 | |
| 890 | r = kvm_vm_ioctl_allocate_rma(kvm, &rma); |
| 891 | if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma))) |
| 892 | r = -EFAULT; |
| 893 | break; |
| 894 | } |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 895 | |
| 896 | case KVM_PPC_ALLOCATE_HTAB: { |
| 897 | struct kvm *kvm = filp->private_data; |
| 898 | u32 htab_order; |
| 899 | |
| 900 | r = -EFAULT; |
| 901 | if (get_user(htab_order, (u32 __user *)argp)) |
| 902 | break; |
| 903 | r = kvmppc_alloc_reset_hpt(kvm, &htab_order); |
| 904 | if (r) |
| 905 | break; |
| 906 | r = -EFAULT; |
| 907 | if (put_user(htab_order, (u32 __user *)argp)) |
| 908 | break; |
| 909 | r = 0; |
| 910 | break; |
| 911 | } |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 912 | #endif /* CONFIG_KVM_BOOK3S_64_HV */ |
| 913 | |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 914 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 915 | case KVM_PPC_GET_SMMU_INFO: { |
| 916 | struct kvm *kvm = filp->private_data; |
| 917 | struct kvm_ppc_smmu_info info; |
| 918 | |
| 919 | memset(&info, 0, sizeof(info)); |
| 920 | r = kvm_vm_ioctl_get_smmu_info(kvm, &info); |
| 921 | if (r >= 0 && copy_to_user(argp, &info, sizeof(info))) |
| 922 | r = -EFAULT; |
| 923 | break; |
| 924 | } |
| 925 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 926 | default: |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 927 | r = -ENOTTY; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 928 | } |
| 929 | |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 930 | out: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 931 | return r; |
| 932 | } |
| 933 | |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 934 | static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)]; |
| 935 | static unsigned long nr_lpids; |
| 936 | |
| 937 | long kvmppc_alloc_lpid(void) |
| 938 | { |
| 939 | long lpid; |
| 940 | |
| 941 | do { |
| 942 | lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS); |
| 943 | if (lpid >= nr_lpids) { |
| 944 | pr_err("%s: No LPIDs free\n", __func__); |
| 945 | return -ENOMEM; |
| 946 | } |
| 947 | } while (test_and_set_bit(lpid, lpid_inuse)); |
| 948 | |
| 949 | return lpid; |
| 950 | } |
| 951 | |
| 952 | void kvmppc_claim_lpid(long lpid) |
| 953 | { |
| 954 | set_bit(lpid, lpid_inuse); |
| 955 | } |
| 956 | |
| 957 | void kvmppc_free_lpid(long lpid) |
| 958 | { |
| 959 | clear_bit(lpid, lpid_inuse); |
| 960 | } |
| 961 | |
| 962 | void kvmppc_init_lpid(unsigned long nr_lpids_param) |
| 963 | { |
| 964 | nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param); |
| 965 | memset(lpid_inuse, 0, sizeof(lpid_inuse)); |
| 966 | } |
| 967 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 968 | int kvm_arch_init(void *opaque) |
| 969 | { |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | void kvm_arch_exit(void) |
| 974 | { |
| 975 | } |