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> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/vmalloc.h> |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 26 | #include <linux/hrtimer.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 27 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 29 | #include <asm/cputable.h> |
| 30 | #include <asm/uaccess.h> |
| 31 | #include <asm/kvm_ppc.h> |
Hollis Blanchard | 83aae4a | 2008-07-25 13:54:52 -0500 | [diff] [blame] | 32 | #include <asm/tlbflush.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 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 41 | return !(v->arch.shared->msr & MSR_WE) || |
| 42 | !!(v->arch.pending_exceptions); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 45 | int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) |
| 46 | { |
| 47 | int nr = kvmppc_get_gpr(vcpu, 11); |
| 48 | int r; |
| 49 | unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3); |
| 50 | unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4); |
| 51 | unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5); |
| 52 | unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6); |
| 53 | unsigned long r2 = 0; |
| 54 | |
| 55 | if (!(vcpu->arch.shared->msr & MSR_SF)) { |
| 56 | /* 32 bit mode */ |
| 57 | param1 &= 0xffffffff; |
| 58 | param2 &= 0xffffffff; |
| 59 | param3 &= 0xffffffff; |
| 60 | param4 &= 0xffffffff; |
| 61 | } |
| 62 | |
| 63 | switch (nr) { |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 64 | case HC_VENDOR_KVM | KVM_HC_PPC_MAP_MAGIC_PAGE: |
| 65 | { |
| 66 | vcpu->arch.magic_page_pa = param1; |
| 67 | vcpu->arch.magic_page_ea = param2; |
| 68 | |
| 69 | r = HC_EV_SUCCESS; |
| 70 | break; |
| 71 | } |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 72 | case HC_VENDOR_KVM | KVM_HC_FEATURES: |
| 73 | r = HC_EV_SUCCESS; |
Alexander Graf | 5fc8740 | 2010-07-29 14:47:55 +0200 | [diff] [blame] | 74 | #if defined(CONFIG_PPC_BOOK3S) /* XXX Missing magic page on BookE */ |
| 75 | r2 |= (1 << KVM_FEATURE_MAGIC_PAGE); |
| 76 | #endif |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 77 | |
| 78 | /* Second return value is in r4 */ |
| 79 | kvmppc_set_gpr(vcpu, 4, r2); |
| 80 | break; |
| 81 | default: |
| 82 | r = HC_EV_UNIMPLEMENTED; |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | return r; |
| 87 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 88 | |
| 89 | int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 90 | { |
| 91 | enum emulation_result er; |
| 92 | int r; |
| 93 | |
| 94 | er = kvmppc_emulate_instruction(run, vcpu); |
| 95 | switch (er) { |
| 96 | case EMULATE_DONE: |
| 97 | /* Future optimization: only reload non-volatiles if they were |
| 98 | * actually modified. */ |
| 99 | r = RESUME_GUEST_NV; |
| 100 | break; |
| 101 | case EMULATE_DO_MMIO: |
| 102 | run->exit_reason = KVM_EXIT_MMIO; |
| 103 | /* We must reload nonvolatiles because "update" load/store |
| 104 | * instructions modify register state. */ |
| 105 | /* Future optimization: only reload non-volatiles if they were |
| 106 | * actually modified. */ |
| 107 | r = RESUME_HOST_NV; |
| 108 | break; |
| 109 | case EMULATE_FAIL: |
| 110 | /* XXX Deliver Program interrupt to guest. */ |
| 111 | printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__, |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 112 | kvmppc_get_last_inst(vcpu)); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 113 | r = RESUME_HOST; |
| 114 | break; |
| 115 | default: |
| 116 | BUG(); |
| 117 | } |
| 118 | |
| 119 | return r; |
| 120 | } |
| 121 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 122 | int kvm_arch_hardware_enable(void *garbage) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 123 | { |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 124 | return 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void kvm_arch_hardware_disable(void *garbage) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | int kvm_arch_hardware_setup(void) |
| 132 | { |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | void kvm_arch_hardware_unsetup(void) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | void kvm_arch_check_processor_compat(void *rtn) |
| 141 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 142 | *(int *)rtn = kvmppc_core_check_processor_compat(); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | struct kvm *kvm_arch_create_vm(void) |
| 146 | { |
| 147 | struct kvm *kvm; |
| 148 | |
| 149 | kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 150 | if (!kvm) |
| 151 | return ERR_PTR(-ENOMEM); |
| 152 | |
| 153 | return kvm; |
| 154 | } |
| 155 | |
| 156 | static void kvmppc_free_vcpus(struct kvm *kvm) |
| 157 | { |
| 158 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 159 | struct kvm_vcpu *vcpu; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 160 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 161 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 162 | kvm_arch_vcpu_free(vcpu); |
| 163 | |
| 164 | mutex_lock(&kvm->lock); |
| 165 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 166 | kvm->vcpus[i] = NULL; |
| 167 | |
| 168 | atomic_set(&kvm->online_vcpus, 0); |
| 169 | mutex_unlock(&kvm->lock); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 172 | void kvm_arch_sync_events(struct kvm *kvm) |
| 173 | { |
| 174 | } |
| 175 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 176 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 177 | { |
| 178 | kvmppc_free_vcpus(kvm); |
| 179 | kvm_free_physmem(kvm); |
Marcelo Tosatti | 6474920 | 2010-01-19 12:45:23 -0200 | [diff] [blame] | 180 | cleanup_srcu_struct(&kvm->srcu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 181 | kfree(kvm); |
| 182 | } |
| 183 | |
| 184 | int kvm_dev_ioctl_check_extension(long ext) |
| 185 | { |
| 186 | int r; |
| 187 | |
| 188 | switch (ext) { |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 189 | case KVM_CAP_PPC_SEGSTATE: |
Alexander Graf | c10207f | 2010-02-19 11:00:45 +0100 | [diff] [blame] | 190 | case KVM_CAP_PPC_PAIRED_SINGLES: |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 191 | case KVM_CAP_PPC_UNSET_IRQ: |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 192 | case KVM_CAP_ENABLE_CAP: |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 193 | case KVM_CAP_PPC_OSI: |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 194 | case KVM_CAP_PPC_GET_PVINFO: |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 195 | r = 1; |
| 196 | break; |
Laurent Vivier | 588968b | 2008-05-30 16:05:56 +0200 | [diff] [blame] | 197 | case KVM_CAP_COALESCED_MMIO: |
| 198 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 199 | break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 200 | default: |
| 201 | r = 0; |
| 202 | break; |
| 203 | } |
| 204 | return r; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | long kvm_arch_dev_ioctl(struct file *filp, |
| 209 | unsigned int ioctl, unsigned long arg) |
| 210 | { |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 214 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 215 | struct kvm_memory_slot *memslot, |
| 216 | struct kvm_memory_slot old, |
| 217 | struct kvm_userspace_memory_region *mem, |
| 218 | int user_alloc) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 219 | { |
| 220 | return 0; |
| 221 | } |
| 222 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 223 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 224 | struct kvm_userspace_memory_region *mem, |
| 225 | struct kvm_memory_slot old, |
| 226 | int user_alloc) |
| 227 | { |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 232 | void kvm_arch_flush_shadow(struct kvm *kvm) |
| 233 | { |
| 234 | } |
| 235 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 236 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
| 237 | { |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 238 | struct kvm_vcpu *vcpu; |
| 239 | vcpu = kvmppc_core_vcpu_create(kvm, id); |
Wei Yongjun | 06056bf | 2010-03-09 14:13:43 +0800 | [diff] [blame] | 240 | if (!IS_ERR(vcpu)) |
| 241 | kvmppc_create_vcpu_debugfs(vcpu, id); |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 242 | return vcpu; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 246 | { |
Alexander Graf | a595405 | 2010-02-22 16:52:14 +0100 | [diff] [blame] | 247 | /* Make sure we're not using the vcpu anymore */ |
| 248 | hrtimer_cancel(&vcpu->arch.dec_timer); |
| 249 | tasklet_kill(&vcpu->arch.tasklet); |
| 250 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 251 | kvmppc_remove_vcpu_debugfs(vcpu); |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 252 | kvmppc_core_vcpu_free(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 256 | { |
| 257 | kvm_arch_vcpu_free(vcpu); |
| 258 | } |
| 259 | |
| 260 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 261 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 262 | return kvmppc_core_pending_dec(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static void kvmppc_decrementer_func(unsigned long data) |
| 266 | { |
| 267 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; |
| 268 | |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 269 | kvmppc_core_queue_dec(vcpu); |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 270 | |
| 271 | if (waitqueue_active(&vcpu->wq)) { |
| 272 | wake_up_interruptible(&vcpu->wq); |
| 273 | vcpu->stat.halt_wakeup++; |
| 274 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 277 | /* |
| 278 | * low level hrtimer wake routine. Because this runs in hardirq context |
| 279 | * we schedule a tasklet to do the real work. |
| 280 | */ |
| 281 | enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer) |
| 282 | { |
| 283 | struct kvm_vcpu *vcpu; |
| 284 | |
| 285 | vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer); |
| 286 | tasklet_schedule(&vcpu->arch.tasklet); |
| 287 | |
| 288 | return HRTIMER_NORESTART; |
| 289 | } |
| 290 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 291 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 292 | { |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 293 | hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); |
| 294 | tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu); |
| 295 | vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 301 | { |
Hollis Blanchard | ecc0981 | 2009-01-03 16:22:59 -0600 | [diff] [blame] | 302 | kvmppc_mmu_destroy(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 306 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 307 | kvmppc_core_vcpu_load(vcpu, cpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 311 | { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 312 | kvmppc_core_vcpu_put(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 315 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
Hollis Blanchard | f5d0906 | 2009-01-04 13:51:09 -0600 | [diff] [blame] | 316 | struct kvm_guest_debug *dbg) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 317 | { |
Hollis Blanchard | f5d0906 | 2009-01-04 13:51:09 -0600 | [diff] [blame] | 318 | return -EINVAL; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu, |
| 322 | struct kvm_run *run) |
| 323 | { |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 324 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, |
| 328 | struct kvm_run *run) |
| 329 | { |
Denis Kirjanov | 69b6183 | 2010-06-11 11:23:26 +0000 | [diff] [blame] | 330 | u64 uninitialized_var(gpr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 331 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 332 | if (run->mmio.len > sizeof(gpr)) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 333 | printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | if (vcpu->arch.mmio_is_bigendian) { |
| 338 | switch (run->mmio.len) { |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 339 | case 8: gpr = *(u64 *)run->mmio.data; break; |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 340 | case 4: gpr = *(u32 *)run->mmio.data; break; |
| 341 | case 2: gpr = *(u16 *)run->mmio.data; break; |
| 342 | case 1: gpr = *(u8 *)run->mmio.data; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 343 | } |
| 344 | } else { |
| 345 | /* Convert BE data from userland back to LE. */ |
| 346 | switch (run->mmio.len) { |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 347 | case 4: gpr = ld_le32((u32 *)run->mmio.data); break; |
| 348 | case 2: gpr = ld_le16((u16 *)run->mmio.data); break; |
| 349 | case 1: gpr = *(u8 *)run->mmio.data; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 350 | } |
| 351 | } |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 352 | |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 353 | if (vcpu->arch.mmio_sign_extend) { |
| 354 | switch (run->mmio.len) { |
| 355 | #ifdef CONFIG_PPC64 |
| 356 | case 4: |
| 357 | gpr = (s64)(s32)gpr; |
| 358 | break; |
| 359 | #endif |
| 360 | case 2: |
| 361 | gpr = (s64)(s16)gpr; |
| 362 | break; |
| 363 | case 1: |
| 364 | gpr = (s64)(s8)gpr; |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 369 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 370 | |
| 371 | switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) { |
| 372 | case KVM_REG_GPR: |
| 373 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
| 374 | break; |
| 375 | case KVM_REG_FPR: |
| 376 | vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; |
| 377 | break; |
Alexander Graf | 287d561 | 2010-04-01 15:33:21 +0200 | [diff] [blame] | 378 | #ifdef CONFIG_PPC_BOOK3S |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 379 | case KVM_REG_QPR: |
| 380 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; |
| 381 | break; |
| 382 | case KVM_REG_FQPR: |
| 383 | vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; |
| 384 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; |
| 385 | break; |
Alexander Graf | 287d561 | 2010-04-01 15:33:21 +0200 | [diff] [blame] | 386 | #endif |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 387 | default: |
| 388 | BUG(); |
| 389 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 393 | unsigned int rt, unsigned int bytes, int is_bigendian) |
| 394 | { |
| 395 | if (bytes > sizeof(run->mmio.data)) { |
| 396 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, |
| 397 | run->mmio.len); |
| 398 | } |
| 399 | |
| 400 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; |
| 401 | run->mmio.len = bytes; |
| 402 | run->mmio.is_write = 0; |
| 403 | |
| 404 | vcpu->arch.io_gpr = rt; |
| 405 | vcpu->arch.mmio_is_bigendian = is_bigendian; |
| 406 | vcpu->mmio_needed = 1; |
| 407 | vcpu->mmio_is_write = 0; |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 408 | vcpu->arch.mmio_sign_extend = 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 409 | |
| 410 | return EMULATE_DO_MMIO; |
| 411 | } |
| 412 | |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 413 | /* Same as above, but sign extends */ |
| 414 | int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 415 | unsigned int rt, unsigned int bytes, int is_bigendian) |
| 416 | { |
| 417 | int r; |
| 418 | |
| 419 | r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian); |
| 420 | vcpu->arch.mmio_sign_extend = 1; |
| 421 | |
| 422 | return r; |
| 423 | } |
| 424 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 425 | int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 426 | u64 val, unsigned int bytes, int is_bigendian) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 427 | { |
| 428 | void *data = run->mmio.data; |
| 429 | |
| 430 | if (bytes > sizeof(run->mmio.data)) { |
| 431 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, |
| 432 | run->mmio.len); |
| 433 | } |
| 434 | |
| 435 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; |
| 436 | run->mmio.len = bytes; |
| 437 | run->mmio.is_write = 1; |
| 438 | vcpu->mmio_needed = 1; |
| 439 | vcpu->mmio_is_write = 1; |
| 440 | |
| 441 | /* Store the value at the lowest bytes in 'data'. */ |
| 442 | if (is_bigendian) { |
| 443 | switch (bytes) { |
Alexander Graf | b104d06 | 2010-02-19 11:00:29 +0100 | [diff] [blame] | 444 | case 8: *(u64 *)data = val; break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 445 | case 4: *(u32 *)data = val; break; |
| 446 | case 2: *(u16 *)data = val; break; |
| 447 | case 1: *(u8 *)data = val; break; |
| 448 | } |
| 449 | } else { |
| 450 | /* Store LE value into 'data'. */ |
| 451 | switch (bytes) { |
| 452 | case 4: st_le32(data, val); break; |
| 453 | case 2: st_le16(data, val); break; |
| 454 | case 1: *(u8 *)data = val; break; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return EMULATE_DO_MMIO; |
| 459 | } |
| 460 | |
| 461 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 462 | { |
| 463 | int r; |
| 464 | sigset_t sigsaved; |
| 465 | |
| 466 | if (vcpu->sigset_active) |
| 467 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 468 | |
| 469 | if (vcpu->mmio_needed) { |
| 470 | if (!vcpu->mmio_is_write) |
| 471 | kvmppc_complete_mmio_load(vcpu, run); |
| 472 | vcpu->mmio_needed = 0; |
| 473 | } else if (vcpu->arch.dcr_needed) { |
| 474 | if (!vcpu->arch.dcr_is_write) |
| 475 | kvmppc_complete_dcr_load(vcpu, run); |
| 476 | vcpu->arch.dcr_needed = 0; |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 477 | } else if (vcpu->arch.osi_needed) { |
| 478 | u64 *gprs = run->osi.gprs; |
| 479 | int i; |
| 480 | |
| 481 | for (i = 0; i < 32; i++) |
| 482 | kvmppc_set_gpr(vcpu, i, gprs[i]); |
| 483 | vcpu->arch.osi_needed = 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 486 | kvmppc_core_deliver_interrupts(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 487 | |
| 488 | local_irq_disable(); |
| 489 | kvm_guest_enter(); |
| 490 | r = __kvmppc_vcpu_run(run, vcpu); |
| 491 | kvm_guest_exit(); |
| 492 | local_irq_enable(); |
| 493 | |
| 494 | if (vcpu->sigset_active) |
| 495 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 496 | |
| 497 | return r; |
| 498 | } |
| 499 | |
| 500 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) |
| 501 | { |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 502 | if (irq->irq == KVM_INTERRUPT_UNSET) |
| 503 | kvmppc_core_dequeue_external(vcpu, irq); |
| 504 | else |
| 505 | kvmppc_core_queue_external(vcpu, irq); |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 506 | |
| 507 | if (waitqueue_active(&vcpu->wq)) { |
| 508 | wake_up_interruptible(&vcpu->wq); |
| 509 | vcpu->stat.halt_wakeup++; |
| 510 | } |
| 511 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 515 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
| 516 | struct kvm_enable_cap *cap) |
| 517 | { |
| 518 | int r; |
| 519 | |
| 520 | if (cap->flags) |
| 521 | return -EINVAL; |
| 522 | |
| 523 | switch (cap->cap) { |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 524 | case KVM_CAP_PPC_OSI: |
| 525 | r = 0; |
| 526 | vcpu->arch.osi_enabled = true; |
| 527 | break; |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 528 | default: |
| 529 | r = -EINVAL; |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | return r; |
| 534 | } |
| 535 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 536 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 537 | struct kvm_mp_state *mp_state) |
| 538 | { |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | |
| 542 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 543 | struct kvm_mp_state *mp_state) |
| 544 | { |
| 545 | return -EINVAL; |
| 546 | } |
| 547 | |
| 548 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 549 | unsigned int ioctl, unsigned long arg) |
| 550 | { |
| 551 | struct kvm_vcpu *vcpu = filp->private_data; |
| 552 | void __user *argp = (void __user *)arg; |
| 553 | long r; |
| 554 | |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 555 | switch (ioctl) { |
| 556 | case KVM_INTERRUPT: { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 557 | struct kvm_interrupt irq; |
| 558 | r = -EFAULT; |
| 559 | if (copy_from_user(&irq, argp, sizeof(irq))) |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 560 | goto out; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 561 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 562 | goto out; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 563 | } |
Avi Kivity | 19483d1 | 2010-05-13 12:30:43 +0300 | [diff] [blame] | 564 | |
Alexander Graf | 71fbfd5 | 2010-03-24 21:48:29 +0100 | [diff] [blame] | 565 | case KVM_ENABLE_CAP: |
| 566 | { |
| 567 | struct kvm_enable_cap cap; |
| 568 | r = -EFAULT; |
| 569 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 570 | goto out; |
| 571 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); |
| 572 | break; |
| 573 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 574 | default: |
| 575 | r = -EINVAL; |
| 576 | } |
| 577 | |
| 578 | out: |
| 579 | return r; |
| 580 | } |
| 581 | |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 582 | static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo) |
| 583 | { |
| 584 | u32 inst_lis = 0x3c000000; |
| 585 | u32 inst_ori = 0x60000000; |
| 586 | u32 inst_nop = 0x60000000; |
| 587 | u32 inst_sc = 0x44000002; |
| 588 | u32 inst_imm_mask = 0xffff; |
| 589 | |
| 590 | /* |
| 591 | * The hypercall to get into KVM from within guest context is as |
| 592 | * follows: |
| 593 | * |
| 594 | * lis r0, r0, KVM_SC_MAGIC_R0@h |
| 595 | * ori r0, KVM_SC_MAGIC_R0@l |
| 596 | * sc |
| 597 | * nop |
| 598 | */ |
| 599 | pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask); |
| 600 | pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask); |
| 601 | pvinfo->hcall[2] = inst_sc; |
| 602 | pvinfo->hcall[3] = inst_nop; |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 607 | long kvm_arch_vm_ioctl(struct file *filp, |
| 608 | unsigned int ioctl, unsigned long arg) |
| 609 | { |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 610 | void __user *argp = (void __user *)arg; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 611 | long r; |
| 612 | |
| 613 | switch (ioctl) { |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 614 | case KVM_PPC_GET_PVINFO: { |
| 615 | struct kvm_ppc_pvinfo pvinfo; |
| 616 | r = kvm_vm_ioctl_get_pvinfo(&pvinfo); |
| 617 | if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) { |
| 618 | r = -EFAULT; |
| 619 | goto out; |
| 620 | } |
| 621 | |
| 622 | break; |
| 623 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 624 | default: |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 625 | r = -ENOTTY; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 626 | } |
| 627 | |
Alexander Graf | 15711e9 | 2010-07-29 14:48:08 +0200 | [diff] [blame] | 628 | out: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 629 | return r; |
| 630 | } |
| 631 | |
| 632 | int kvm_arch_init(void *opaque) |
| 633 | { |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | void kvm_arch_exit(void) |
| 638 | { |
| 639 | } |