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/cacheflush.h>
35 unsigned long kvmppc_booke_handlers;
37 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
38 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
40 struct kvm_stats_debugfs_item debugfs_entries[] = {
41 { "mmio", VCPU_STAT(mmio_exits) },
42 { "dcr", VCPU_STAT(dcr_exits) },
43 { "sig", VCPU_STAT(signal_exits) },
44 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
45 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
46 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
47 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
48 { "sysc", VCPU_STAT(syscall_exits) },
49 { "isi", VCPU_STAT(isi_exits) },
50 { "dsi", VCPU_STAT(dsi_exits) },
51 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
52 { "dec", VCPU_STAT(dec_exits) },
53 { "ext_intr", VCPU_STAT(ext_intr_exits) },
54 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
58 /* TODO: use vcpu_printf() */
59 void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
63 printk("pc: %08lx msr: %08lx\n", vcpu->arch.pc, vcpu->arch.msr);
64 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
65 printk("srr0: %08lx srr1: %08lx\n", vcpu->arch.srr0, vcpu->arch.srr1);
67 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
69 for (i = 0; i < 32; i += 4) {
70 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
78 static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
79 unsigned int priority)
81 set_bit(priority, &vcpu->arch.pending_exceptions);
84 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu)
86 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
89 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
91 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
94 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
96 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
99 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
100 struct kvm_interrupt *irq)
102 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_EXTERNAL);
105 /* Deliver the interrupt of the corresponding priority, if possible. */
106 static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
107 unsigned int priority)
113 case BOOKE_IRQPRIO_PROGRAM:
114 case BOOKE_IRQPRIO_DTLB_MISS:
115 case BOOKE_IRQPRIO_ITLB_MISS:
116 case BOOKE_IRQPRIO_SYSCALL:
117 case BOOKE_IRQPRIO_DATA_STORAGE:
118 case BOOKE_IRQPRIO_INST_STORAGE:
119 case BOOKE_IRQPRIO_FP_UNAVAIL:
120 case BOOKE_IRQPRIO_AP_UNAVAIL:
121 case BOOKE_IRQPRIO_ALIGNMENT:
123 msr_mask = MSR_CE|MSR_ME|MSR_DE;
125 case BOOKE_IRQPRIO_CRITICAL:
126 case BOOKE_IRQPRIO_WATCHDOG:
127 allowed = vcpu->arch.msr & MSR_CE;
130 case BOOKE_IRQPRIO_MACHINE_CHECK:
131 allowed = vcpu->arch.msr & MSR_ME;
134 case BOOKE_IRQPRIO_EXTERNAL:
135 case BOOKE_IRQPRIO_DECREMENTER:
136 case BOOKE_IRQPRIO_FIT:
137 allowed = vcpu->arch.msr & MSR_EE;
138 msr_mask = MSR_CE|MSR_ME|MSR_DE;
140 case BOOKE_IRQPRIO_DEBUG:
141 allowed = vcpu->arch.msr & MSR_DE;
147 vcpu->arch.srr0 = vcpu->arch.pc;
148 vcpu->arch.srr1 = vcpu->arch.msr;
149 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
150 kvmppc_set_msr(vcpu, vcpu->arch.msr & msr_mask);
152 clear_bit(priority, &vcpu->arch.pending_exceptions);
158 /* Check pending exceptions and deliver one, if possible. */
159 void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
161 unsigned long *pending = &vcpu->arch.pending_exceptions;
162 unsigned int priority;
164 priority = __ffs(*pending);
165 while (priority <= BOOKE_MAX_INTERRUPT) {
166 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
169 priority = find_next_bit(pending,
170 BITS_PER_BYTE * sizeof(*pending),
178 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
180 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
181 unsigned int exit_nr)
183 enum emulation_result er;
188 run->exit_reason = KVM_EXIT_UNKNOWN;
189 run->ready_for_interrupt_injection = 1;
192 case BOOKE_INTERRUPT_MACHINE_CHECK:
193 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
194 kvmppc_dump_vcpu(vcpu);
198 case BOOKE_INTERRUPT_EXTERNAL:
199 vcpu->stat.ext_intr_exits++;
205 case BOOKE_INTERRUPT_DECREMENTER:
206 /* Since we switched IVPR back to the host's value, the host
207 * handled this interrupt the moment we enabled interrupts.
208 * Now we just offer it a chance to reschedule the guest. */
210 /* XXX At this point the TLB still holds our shadow TLB, so if
211 * we do reschedule the host will fault over it. Perhaps we
212 * should politely restore the host's entries to minimize
213 * misses before ceding control. */
214 vcpu->stat.dec_exits++;
220 case BOOKE_INTERRUPT_PROGRAM:
221 if (vcpu->arch.msr & MSR_PR) {
222 /* Program traps generated by user-level software must be handled
223 * by the guest kernel. */
224 vcpu->arch.esr = vcpu->arch.fault_esr;
225 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
230 er = kvmppc_emulate_instruction(run, vcpu);
233 /* Future optimization: only reload non-volatiles if
234 * they were actually modified by emulation. */
235 vcpu->stat.emulated_inst_exits++;
239 run->exit_reason = KVM_EXIT_DCR;
240 vcpu->stat.dcr_exits++;
244 /* XXX Deliver Program interrupt to guest. */
245 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
246 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
247 /* For debugging, encode the failing instruction and
248 * report it to userspace. */
249 run->hw.hardware_exit_reason = ~0ULL << 32;
250 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
258 case BOOKE_INTERRUPT_FP_UNAVAIL:
259 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
263 case BOOKE_INTERRUPT_DATA_STORAGE:
264 vcpu->arch.dear = vcpu->arch.fault_dear;
265 vcpu->arch.esr = vcpu->arch.fault_esr;
266 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
267 vcpu->stat.dsi_exits++;
271 case BOOKE_INTERRUPT_INST_STORAGE:
272 vcpu->arch.esr = vcpu->arch.fault_esr;
273 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
274 vcpu->stat.isi_exits++;
278 case BOOKE_INTERRUPT_SYSCALL:
279 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
280 vcpu->stat.syscall_exits++;
284 case BOOKE_INTERRUPT_DTLB_MISS: {
285 struct kvmppc_44x_tlbe *gtlbe;
286 unsigned long eaddr = vcpu->arch.fault_dear;
289 /* Check the guest TLB. */
290 gtlbe = kvmppc_44x_dtlb_search(vcpu, eaddr);
292 /* The guest didn't have a mapping for it. */
293 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
294 vcpu->arch.dear = vcpu->arch.fault_dear;
295 vcpu->arch.esr = vcpu->arch.fault_esr;
296 vcpu->stat.dtlb_real_miss_exits++;
301 vcpu->arch.paddr_accessed = tlb_xlate(gtlbe, eaddr);
302 gfn = vcpu->arch.paddr_accessed >> PAGE_SHIFT;
304 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
305 /* The guest TLB had a mapping, but the shadow TLB
306 * didn't, and it is RAM. This could be because:
307 * a) the entry is mapping the host kernel, or
308 * b) the guest used a large mapping which we're faking
309 * Either way, we need to satisfy the fault without
310 * invoking the guest. */
311 kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
313 vcpu->stat.dtlb_virt_miss_exits++;
316 /* Guest has mapped and accessed a page which is not
318 r = kvmppc_emulate_mmio(run, vcpu);
319 vcpu->stat.mmio_exits++;
325 case BOOKE_INTERRUPT_ITLB_MISS: {
326 struct kvmppc_44x_tlbe *gtlbe;
327 unsigned long eaddr = vcpu->arch.pc;
332 /* Check the guest TLB. */
333 gtlbe = kvmppc_44x_itlb_search(vcpu, eaddr);
335 /* The guest didn't have a mapping for it. */
336 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
337 vcpu->stat.itlb_real_miss_exits++;
341 vcpu->stat.itlb_virt_miss_exits++;
343 gfn = tlb_xlate(gtlbe, eaddr) >> PAGE_SHIFT;
345 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
346 /* The guest TLB had a mapping, but the shadow TLB
347 * didn't. This could be because:
348 * a) the entry is mapping the host kernel, or
349 * b) the guest used a large mapping which we're faking
350 * Either way, we need to satisfy the fault without
351 * invoking the guest. */
352 kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
355 /* Guest mapped and leaped at non-RAM! */
356 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
362 case BOOKE_INTERRUPT_DEBUG: {
365 vcpu->arch.pc = mfspr(SPRN_CSRR0);
367 /* clear IAC events in DBSR register */
368 dbsr = mfspr(SPRN_DBSR);
369 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
370 mtspr(SPRN_DBSR, dbsr);
372 run->exit_reason = KVM_EXIT_DEBUG;
378 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
384 kvmppc_core_deliver_interrupts(vcpu);
386 if (!(r & RESUME_HOST)) {
387 /* To avoid clobbering exit_reason, only check for signals if
388 * we aren't already exiting to userspace for some other
390 if (signal_pending(current)) {
391 run->exit_reason = KVM_EXIT_INTR;
392 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
393 vcpu->stat.signal_exits++;
400 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
401 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
405 vcpu->arch.gpr[1] = (16<<20) - 8; /* -8 for the callee-save LR slot */
407 vcpu->arch.shadow_pid = 1;
409 /* Eye-catching number so we know if the guest takes an interrupt
410 * before it's programmed its own IVPR. */
411 vcpu->arch.ivpr = 0x55550000;
413 return kvmppc_core_vcpu_setup(vcpu);
416 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
420 regs->pc = vcpu->arch.pc;
421 regs->cr = vcpu->arch.cr;
422 regs->ctr = vcpu->arch.ctr;
423 regs->lr = vcpu->arch.lr;
424 regs->xer = vcpu->arch.xer;
425 regs->msr = vcpu->arch.msr;
426 regs->srr0 = vcpu->arch.srr0;
427 regs->srr1 = vcpu->arch.srr1;
428 regs->pid = vcpu->arch.pid;
429 regs->sprg0 = vcpu->arch.sprg0;
430 regs->sprg1 = vcpu->arch.sprg1;
431 regs->sprg2 = vcpu->arch.sprg2;
432 regs->sprg3 = vcpu->arch.sprg3;
433 regs->sprg5 = vcpu->arch.sprg4;
434 regs->sprg6 = vcpu->arch.sprg5;
435 regs->sprg7 = vcpu->arch.sprg6;
437 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
438 regs->gpr[i] = vcpu->arch.gpr[i];
443 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
447 vcpu->arch.pc = regs->pc;
448 vcpu->arch.cr = regs->cr;
449 vcpu->arch.ctr = regs->ctr;
450 vcpu->arch.lr = regs->lr;
451 vcpu->arch.xer = regs->xer;
452 kvmppc_set_msr(vcpu, regs->msr);
453 vcpu->arch.srr0 = regs->srr0;
454 vcpu->arch.srr1 = regs->srr1;
455 vcpu->arch.sprg0 = regs->sprg0;
456 vcpu->arch.sprg1 = regs->sprg1;
457 vcpu->arch.sprg2 = regs->sprg2;
458 vcpu->arch.sprg3 = regs->sprg3;
459 vcpu->arch.sprg5 = regs->sprg4;
460 vcpu->arch.sprg6 = regs->sprg5;
461 vcpu->arch.sprg7 = regs->sprg6;
463 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gpr); i++)
464 vcpu->arch.gpr[i] = regs->gpr[i];
469 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
470 struct kvm_sregs *sregs)
475 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
476 struct kvm_sregs *sregs)
481 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
486 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
491 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
492 struct kvm_translation *tr)
494 return kvmppc_core_vcpu_translate(vcpu, tr);
497 int kvmppc_booke_init(void)
499 unsigned long ivor[16];
500 unsigned long max_ivor = 0;
503 /* We install our own exception handlers by hijacking IVPR. IVPR must
504 * be 16-bit aligned, so we need a 64KB allocation. */
505 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
507 if (!kvmppc_booke_handlers)
510 /* XXX make sure our handlers are smaller than Linux's */
512 /* Copy our interrupt handlers to match host IVORs. That way we don't
513 * have to swap the IVORs on every guest/host transition. */
514 ivor[0] = mfspr(SPRN_IVOR0);
515 ivor[1] = mfspr(SPRN_IVOR1);
516 ivor[2] = mfspr(SPRN_IVOR2);
517 ivor[3] = mfspr(SPRN_IVOR3);
518 ivor[4] = mfspr(SPRN_IVOR4);
519 ivor[5] = mfspr(SPRN_IVOR5);
520 ivor[6] = mfspr(SPRN_IVOR6);
521 ivor[7] = mfspr(SPRN_IVOR7);
522 ivor[8] = mfspr(SPRN_IVOR8);
523 ivor[9] = mfspr(SPRN_IVOR9);
524 ivor[10] = mfspr(SPRN_IVOR10);
525 ivor[11] = mfspr(SPRN_IVOR11);
526 ivor[12] = mfspr(SPRN_IVOR12);
527 ivor[13] = mfspr(SPRN_IVOR13);
528 ivor[14] = mfspr(SPRN_IVOR14);
529 ivor[15] = mfspr(SPRN_IVOR15);
531 for (i = 0; i < 16; i++) {
532 if (ivor[i] > max_ivor)
535 memcpy((void *)kvmppc_booke_handlers + ivor[i],
536 kvmppc_handlers_start + i * kvmppc_handler_len,
539 flush_icache_range(kvmppc_booke_handlers,
540 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
545 void __exit kvmppc_booke_exit(void)
547 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);