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.
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.
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.
15 * Copyright IBM Corp. 2007
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
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>
27 #include <asm/cputable.h>
28 #include <asm/uaccess.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/tlbflush.h>
33 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
38 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
40 return !!(v->arch.pending_exceptions);
43 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
45 return !(v->arch.msr & MSR_WE);
49 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
51 enum emulation_result er;
54 er = kvmppc_emulate_instruction(run, vcpu);
57 /* Future optimization: only reload non-volatiles if they were
58 * actually modified. */
62 run->exit_reason = KVM_EXIT_MMIO;
63 /* We must reload nonvolatiles because "update" load/store
64 * instructions modify register state. */
65 /* Future optimization: only reload non-volatiles if they were
66 * actually modified. */
70 /* XXX Deliver Program interrupt to guest. */
71 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
72 vcpu->arch.last_inst);
82 void kvm_arch_hardware_enable(void *garbage)
86 void kvm_arch_hardware_disable(void *garbage)
90 int kvm_arch_hardware_setup(void)
95 void kvm_arch_hardware_unsetup(void)
99 void kvm_arch_check_processor_compat(void *rtn)
103 if (strcmp(cur_cpu_spec->platform, "ppc440") == 0)
111 struct kvm *kvm_arch_create_vm(void)
115 kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
117 return ERR_PTR(-ENOMEM);
122 static void kvmppc_free_vcpus(struct kvm *kvm)
126 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
128 kvm_arch_vcpu_free(kvm->vcpus[i]);
129 kvm->vcpus[i] = NULL;
134 void kvm_arch_destroy_vm(struct kvm *kvm)
136 kvmppc_free_vcpus(kvm);
137 kvm_free_physmem(kvm);
141 int kvm_dev_ioctl_check_extension(long ext)
146 case KVM_CAP_USER_MEMORY:
149 case KVM_CAP_COALESCED_MMIO:
150 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
160 long kvm_arch_dev_ioctl(struct file *filp,
161 unsigned int ioctl, unsigned long arg)
166 int kvm_arch_set_memory_region(struct kvm *kvm,
167 struct kvm_userspace_memory_region *mem,
168 struct kvm_memory_slot old,
174 void kvm_arch_flush_shadow(struct kvm *kvm)
178 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
180 struct kvm_vcpu *vcpu;
183 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
189 err = kvm_vcpu_init(vcpu, kvm, id);
196 kmem_cache_free(kvm_vcpu_cache, vcpu);
201 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
203 kvm_vcpu_uninit(vcpu);
204 kmem_cache_free(kvm_vcpu_cache, vcpu);
207 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
209 kvm_arch_vcpu_free(vcpu);
212 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
214 unsigned int priority = exception_priority[BOOKE_INTERRUPT_DECREMENTER];
216 return test_bit(priority, &vcpu->arch.pending_exceptions);
219 static void kvmppc_decrementer_func(unsigned long data)
221 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
223 kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER);
225 if (waitqueue_active(&vcpu->wq)) {
226 wake_up_interruptible(&vcpu->wq);
227 vcpu->stat.halt_wakeup++;
231 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
233 setup_timer(&vcpu->arch.dec_timer, kvmppc_decrementer_func,
234 (unsigned long)vcpu);
239 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
243 /* Note: clearing MSR[DE] just means that the debug interrupt will not be
244 * delivered *immediately*. Instead, it simply sets the appropriate DBSR bits.
245 * If those DBSR bits are still set when MSR[DE] is re-enabled, the interrupt
246 * will be delivered as an "imprecise debug event" (which is indicated by
249 static void kvmppc_disable_debug_interrupts(void)
251 mtmsr(mfmsr() & ~MSR_DE);
254 static void kvmppc_restore_host_debug_state(struct kvm_vcpu *vcpu)
256 kvmppc_disable_debug_interrupts();
258 mtspr(SPRN_IAC1, vcpu->arch.host_iac[0]);
259 mtspr(SPRN_IAC2, vcpu->arch.host_iac[1]);
260 mtspr(SPRN_IAC3, vcpu->arch.host_iac[2]);
261 mtspr(SPRN_IAC4, vcpu->arch.host_iac[3]);
262 mtspr(SPRN_DBCR1, vcpu->arch.host_dbcr1);
263 mtspr(SPRN_DBCR2, vcpu->arch.host_dbcr2);
264 mtspr(SPRN_DBCR0, vcpu->arch.host_dbcr0);
265 mtmsr(vcpu->arch.host_msr);
268 static void kvmppc_load_guest_debug_registers(struct kvm_vcpu *vcpu)
270 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
273 vcpu->arch.host_msr = mfmsr();
274 kvmppc_disable_debug_interrupts();
276 /* Save host debug register state. */
277 vcpu->arch.host_iac[0] = mfspr(SPRN_IAC1);
278 vcpu->arch.host_iac[1] = mfspr(SPRN_IAC2);
279 vcpu->arch.host_iac[2] = mfspr(SPRN_IAC3);
280 vcpu->arch.host_iac[3] = mfspr(SPRN_IAC4);
281 vcpu->arch.host_dbcr0 = mfspr(SPRN_DBCR0);
282 vcpu->arch.host_dbcr1 = mfspr(SPRN_DBCR1);
283 vcpu->arch.host_dbcr2 = mfspr(SPRN_DBCR2);
285 /* set registers up for guest */
288 mtspr(SPRN_IAC1, dbg->bp[0]);
289 dbcr0 |= DBCR0_IAC1 | DBCR0_IDM;
292 mtspr(SPRN_IAC2, dbg->bp[1]);
293 dbcr0 |= DBCR0_IAC2 | DBCR0_IDM;
296 mtspr(SPRN_IAC3, dbg->bp[2]);
297 dbcr0 |= DBCR0_IAC3 | DBCR0_IDM;
300 mtspr(SPRN_IAC4, dbg->bp[3]);
301 dbcr0 |= DBCR0_IAC4 | DBCR0_IDM;
304 mtspr(SPRN_DBCR0, dbcr0);
305 mtspr(SPRN_DBCR1, 0);
306 mtspr(SPRN_DBCR2, 0);
309 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
313 if (vcpu->guest_debug.enabled)
314 kvmppc_load_guest_debug_registers(vcpu);
316 /* Mark every guest entry in the shadow TLB entry modified, so that they
317 * will all be reloaded on the next vcpu run (instead of being
318 * demand-faulted). */
319 for (i = 0; i <= tlb_44x_hwater; i++)
320 kvmppc_tlbe_set_modified(vcpu, i);
323 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
325 if (vcpu->guest_debug.enabled)
326 kvmppc_restore_host_debug_state(vcpu);
328 /* Don't leave guest TLB entries resident when being de-scheduled. */
329 /* XXX It would be nice to differentiate between heavyweight exit and
330 * sched_out here, since we could avoid the TLB flush for heavyweight
335 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
336 struct kvm_debug_guest *dbg)
340 vcpu->guest_debug.enabled = dbg->enabled;
341 if (vcpu->guest_debug.enabled) {
342 for (i=0; i < ARRAY_SIZE(vcpu->guest_debug.bp); i++) {
343 if (dbg->breakpoints[i].enabled)
344 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
346 vcpu->guest_debug.bp[i] = 0;
353 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
356 u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
357 *gpr = run->dcr.data;
360 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
363 u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
365 if (run->mmio.len > sizeof(*gpr)) {
366 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
370 if (vcpu->arch.mmio_is_bigendian) {
371 switch (run->mmio.len) {
372 case 4: *gpr = *(u32 *)run->mmio.data; break;
373 case 2: *gpr = *(u16 *)run->mmio.data; break;
374 case 1: *gpr = *(u8 *)run->mmio.data; break;
377 /* Convert BE data from userland back to LE. */
378 switch (run->mmio.len) {
379 case 4: *gpr = ld_le32((u32 *)run->mmio.data); break;
380 case 2: *gpr = ld_le16((u16 *)run->mmio.data); break;
381 case 1: *gpr = *(u8 *)run->mmio.data; break;
386 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
387 unsigned int rt, unsigned int bytes, int is_bigendian)
389 if (bytes > sizeof(run->mmio.data)) {
390 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
394 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
395 run->mmio.len = bytes;
396 run->mmio.is_write = 0;
398 vcpu->arch.io_gpr = rt;
399 vcpu->arch.mmio_is_bigendian = is_bigendian;
400 vcpu->mmio_needed = 1;
401 vcpu->mmio_is_write = 0;
403 return EMULATE_DO_MMIO;
406 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
407 u32 val, unsigned int bytes, int is_bigendian)
409 void *data = run->mmio.data;
411 if (bytes > sizeof(run->mmio.data)) {
412 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
416 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
417 run->mmio.len = bytes;
418 run->mmio.is_write = 1;
419 vcpu->mmio_needed = 1;
420 vcpu->mmio_is_write = 1;
422 /* Store the value at the lowest bytes in 'data'. */
425 case 4: *(u32 *)data = val; break;
426 case 2: *(u16 *)data = val; break;
427 case 1: *(u8 *)data = val; break;
430 /* Store LE value into 'data'. */
432 case 4: st_le32(data, val); break;
433 case 2: st_le16(data, val); break;
434 case 1: *(u8 *)data = val; break;
438 return EMULATE_DO_MMIO;
441 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
448 if (vcpu->sigset_active)
449 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
451 if (vcpu->mmio_needed) {
452 if (!vcpu->mmio_is_write)
453 kvmppc_complete_mmio_load(vcpu, run);
454 vcpu->mmio_needed = 0;
455 } else if (vcpu->arch.dcr_needed) {
456 if (!vcpu->arch.dcr_is_write)
457 kvmppc_complete_dcr_load(vcpu, run);
458 vcpu->arch.dcr_needed = 0;
461 kvmppc_check_and_deliver_interrupts(vcpu);
465 r = __kvmppc_vcpu_run(run, vcpu);
469 if (vcpu->sigset_active)
470 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
477 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
479 kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
481 if (waitqueue_active(&vcpu->wq)) {
482 wake_up_interruptible(&vcpu->wq);
483 vcpu->stat.halt_wakeup++;
489 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
490 struct kvm_mp_state *mp_state)
495 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
496 struct kvm_mp_state *mp_state)
501 long kvm_arch_vcpu_ioctl(struct file *filp,
502 unsigned int ioctl, unsigned long arg)
504 struct kvm_vcpu *vcpu = filp->private_data;
505 void __user *argp = (void __user *)arg;
509 case KVM_INTERRUPT: {
510 struct kvm_interrupt irq;
512 if (copy_from_user(&irq, argp, sizeof(irq)))
514 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
525 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
530 long kvm_arch_vm_ioctl(struct file *filp,
531 unsigned int ioctl, unsigned long arg)
543 int kvm_arch_init(void *opaque)
548 void kvm_arch_exit(void)