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