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