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 { "exits", VCPU_STAT(sum_exits) },
42 { "mmio", VCPU_STAT(mmio_exits) },
43 { "dcr", VCPU_STAT(dcr_exits) },
44 { "sig", VCPU_STAT(signal_exits) },
45 { "light", VCPU_STAT(light_exits) },
46 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
47 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
48 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
49 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
50 { "sysc", VCPU_STAT(syscall_exits) },
51 { "isi", VCPU_STAT(isi_exits) },
52 { "dsi", VCPU_STAT(dsi_exits) },
53 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
54 { "dec", VCPU_STAT(dec_exits) },
55 { "ext_intr", VCPU_STAT(ext_intr_exits) },
56 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
60 static const u32 interrupt_msr_mask[16] = {
61 [BOOKE_INTERRUPT_CRITICAL] = MSR_ME,
62 [BOOKE_INTERRUPT_MACHINE_CHECK] = 0,
63 [BOOKE_INTERRUPT_DATA_STORAGE] = MSR_CE|MSR_ME|MSR_DE,
64 [BOOKE_INTERRUPT_INST_STORAGE] = MSR_CE|MSR_ME|MSR_DE,
65 [BOOKE_INTERRUPT_EXTERNAL] = MSR_CE|MSR_ME|MSR_DE,
66 [BOOKE_INTERRUPT_ALIGNMENT] = MSR_CE|MSR_ME|MSR_DE,
67 [BOOKE_INTERRUPT_PROGRAM] = MSR_CE|MSR_ME|MSR_DE,
68 [BOOKE_INTERRUPT_FP_UNAVAIL] = MSR_CE|MSR_ME|MSR_DE,
69 [BOOKE_INTERRUPT_SYSCALL] = MSR_CE|MSR_ME|MSR_DE,
70 [BOOKE_INTERRUPT_AP_UNAVAIL] = MSR_CE|MSR_ME|MSR_DE,
71 [BOOKE_INTERRUPT_DECREMENTER] = MSR_CE|MSR_ME|MSR_DE,
72 [BOOKE_INTERRUPT_FIT] = MSR_CE|MSR_ME|MSR_DE,
73 [BOOKE_INTERRUPT_WATCHDOG] = MSR_ME,
74 [BOOKE_INTERRUPT_DTLB_MISS] = MSR_CE|MSR_ME|MSR_DE,
75 [BOOKE_INTERRUPT_ITLB_MISS] = MSR_CE|MSR_ME|MSR_DE,
76 [BOOKE_INTERRUPT_DEBUG] = MSR_ME,
79 const unsigned char exception_priority[] = {
80 [BOOKE_INTERRUPT_DATA_STORAGE] = 0,
81 [BOOKE_INTERRUPT_INST_STORAGE] = 1,
82 [BOOKE_INTERRUPT_ALIGNMENT] = 2,
83 [BOOKE_INTERRUPT_PROGRAM] = 3,
84 [BOOKE_INTERRUPT_FP_UNAVAIL] = 4,
85 [BOOKE_INTERRUPT_SYSCALL] = 5,
86 [BOOKE_INTERRUPT_AP_UNAVAIL] = 6,
87 [BOOKE_INTERRUPT_DTLB_MISS] = 7,
88 [BOOKE_INTERRUPT_ITLB_MISS] = 8,
89 [BOOKE_INTERRUPT_MACHINE_CHECK] = 9,
90 [BOOKE_INTERRUPT_DEBUG] = 10,
91 [BOOKE_INTERRUPT_CRITICAL] = 11,
92 [BOOKE_INTERRUPT_WATCHDOG] = 12,
93 [BOOKE_INTERRUPT_EXTERNAL] = 13,
94 [BOOKE_INTERRUPT_FIT] = 14,
95 [BOOKE_INTERRUPT_DECREMENTER] = 15,
98 const unsigned char priority_exception[] = {
99 BOOKE_INTERRUPT_DATA_STORAGE,
100 BOOKE_INTERRUPT_INST_STORAGE,
101 BOOKE_INTERRUPT_ALIGNMENT,
102 BOOKE_INTERRUPT_PROGRAM,
103 BOOKE_INTERRUPT_FP_UNAVAIL,
104 BOOKE_INTERRUPT_SYSCALL,
105 BOOKE_INTERRUPT_AP_UNAVAIL,
106 BOOKE_INTERRUPT_DTLB_MISS,
107 BOOKE_INTERRUPT_ITLB_MISS,
108 BOOKE_INTERRUPT_MACHINE_CHECK,
109 BOOKE_INTERRUPT_DEBUG,
110 BOOKE_INTERRUPT_CRITICAL,
111 BOOKE_INTERRUPT_WATCHDOG,
112 BOOKE_INTERRUPT_EXTERNAL,
114 BOOKE_INTERRUPT_DECREMENTER,
118 /* TODO: use vcpu_printf() */
119 void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
123 printk("pc: %08lx msr: %08lx\n", vcpu->arch.pc, vcpu->arch.msr);
124 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
125 printk("srr0: %08lx srr1: %08lx\n", vcpu->arch.srr0, vcpu->arch.srr1);
127 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
129 for (i = 0; i < 32; i += 4) {
130 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
134 vcpu->arch.gpr[i+3]);
138 static void kvmppc_booke_queue_exception(struct kvm_vcpu *vcpu, int exception)
140 unsigned int priority = exception_priority[exception];
141 set_bit(priority, &vcpu->arch.pending_exceptions);
144 static void kvmppc_booke_clear_exception(struct kvm_vcpu *vcpu, int exception)
146 unsigned int priority = exception_priority[exception];
147 clear_bit(priority, &vcpu->arch.pending_exceptions);
150 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu)
152 kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM);
155 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
157 kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER);
160 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
162 unsigned int priority = exception_priority[BOOKE_INTERRUPT_DECREMENTER];
163 return test_bit(priority, &vcpu->arch.pending_exceptions);
166 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
167 struct kvm_interrupt *irq)
169 kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
172 /* Check if we are ready to deliver the interrupt */
173 static int kvmppc_can_deliver_interrupt(struct kvm_vcpu *vcpu, int interrupt)
178 case BOOKE_INTERRUPT_CRITICAL:
179 r = vcpu->arch.msr & MSR_CE;
181 case BOOKE_INTERRUPT_MACHINE_CHECK:
182 r = vcpu->arch.msr & MSR_ME;
184 case BOOKE_INTERRUPT_EXTERNAL:
185 r = vcpu->arch.msr & MSR_EE;
187 case BOOKE_INTERRUPT_DECREMENTER:
188 r = vcpu->arch.msr & MSR_EE;
190 case BOOKE_INTERRUPT_FIT:
191 r = vcpu->arch.msr & MSR_EE;
193 case BOOKE_INTERRUPT_WATCHDOG:
194 r = vcpu->arch.msr & MSR_CE;
196 case BOOKE_INTERRUPT_DEBUG:
197 r = vcpu->arch.msr & MSR_DE;
206 static void kvmppc_booke_deliver_interrupt(struct kvm_vcpu *vcpu, int interrupt)
209 case BOOKE_INTERRUPT_DECREMENTER:
210 vcpu->arch.tsr |= TSR_DIS;
214 vcpu->arch.srr0 = vcpu->arch.pc;
215 vcpu->arch.srr1 = vcpu->arch.msr;
216 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[interrupt];
217 kvmppc_set_msr(vcpu, vcpu->arch.msr & interrupt_msr_mask[interrupt]);
220 /* Check pending exceptions and deliver one, if possible. */
221 void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
223 unsigned long *pending = &vcpu->arch.pending_exceptions;
224 unsigned int exception;
225 unsigned int priority;
227 priority = find_first_bit(pending, BITS_PER_BYTE * sizeof(*pending));
228 while (priority <= BOOKE_MAX_INTERRUPT) {
229 exception = priority_exception[priority];
230 if (kvmppc_can_deliver_interrupt(vcpu, exception)) {
231 kvmppc_booke_clear_exception(vcpu, exception);
232 kvmppc_booke_deliver_interrupt(vcpu, exception);
236 priority = find_next_bit(pending,
237 BITS_PER_BYTE * sizeof(*pending),
245 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
247 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
248 unsigned int exit_nr)
250 enum emulation_result er;
255 run->exit_reason = KVM_EXIT_UNKNOWN;
256 run->ready_for_interrupt_injection = 1;
259 case BOOKE_INTERRUPT_MACHINE_CHECK:
260 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
261 kvmppc_dump_vcpu(vcpu);
265 case BOOKE_INTERRUPT_EXTERNAL:
266 case BOOKE_INTERRUPT_DECREMENTER:
267 /* Since we switched IVPR back to the host's value, the host
268 * handled this interrupt the moment we enabled interrupts.
269 * Now we just offer it a chance to reschedule the guest. */
271 /* XXX At this point the TLB still holds our shadow TLB, so if
272 * we do reschedule the host will fault over it. Perhaps we
273 * should politely restore the host's entries to minimize
274 * misses before ceding control. */
277 if (exit_nr == BOOKE_INTERRUPT_DECREMENTER)
278 vcpu->stat.dec_exits++;
280 vcpu->stat.ext_intr_exits++;
284 case BOOKE_INTERRUPT_PROGRAM:
285 if (vcpu->arch.msr & MSR_PR) {
286 /* Program traps generated by user-level software must be handled
287 * by the guest kernel. */
288 vcpu->arch.esr = vcpu->arch.fault_esr;
289 kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM);
294 er = kvmppc_emulate_instruction(run, vcpu);
297 /* Future optimization: only reload non-volatiles if
298 * they were actually modified by emulation. */
299 vcpu->stat.emulated_inst_exits++;
303 run->exit_reason = KVM_EXIT_DCR;
307 /* XXX Deliver Program interrupt to guest. */
308 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
309 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
310 /* For debugging, encode the failing instruction and
311 * report it to userspace. */
312 run->hw.hardware_exit_reason = ~0ULL << 32;
313 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
321 case BOOKE_INTERRUPT_FP_UNAVAIL:
322 kvmppc_booke_queue_exception(vcpu, exit_nr);
326 case BOOKE_INTERRUPT_DATA_STORAGE:
327 vcpu->arch.dear = vcpu->arch.fault_dear;
328 vcpu->arch.esr = vcpu->arch.fault_esr;
329 kvmppc_booke_queue_exception(vcpu, exit_nr);
330 vcpu->stat.dsi_exits++;
334 case BOOKE_INTERRUPT_INST_STORAGE:
335 vcpu->arch.esr = vcpu->arch.fault_esr;
336 kvmppc_booke_queue_exception(vcpu, exit_nr);
337 vcpu->stat.isi_exits++;
341 case BOOKE_INTERRUPT_SYSCALL:
342 kvmppc_booke_queue_exception(vcpu, exit_nr);
343 vcpu->stat.syscall_exits++;
347 case BOOKE_INTERRUPT_DTLB_MISS: {
348 struct kvmppc_44x_tlbe *gtlbe;
349 unsigned long eaddr = vcpu->arch.fault_dear;
352 /* Check the guest TLB. */
353 gtlbe = kvmppc_44x_dtlb_search(vcpu, eaddr);
355 /* The guest didn't have a mapping for it. */
356 kvmppc_booke_queue_exception(vcpu, exit_nr);
357 vcpu->arch.dear = vcpu->arch.fault_dear;
358 vcpu->arch.esr = vcpu->arch.fault_esr;
359 vcpu->stat.dtlb_real_miss_exits++;
364 vcpu->arch.paddr_accessed = tlb_xlate(gtlbe, eaddr);
365 gfn = vcpu->arch.paddr_accessed >> PAGE_SHIFT;
367 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
368 /* The guest TLB had a mapping, but the shadow TLB
369 * didn't, and it is RAM. This could be because:
370 * a) the entry is mapping the host kernel, or
371 * b) the guest used a large mapping which we're faking
372 * Either way, we need to satisfy the fault without
373 * invoking the guest. */
374 kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
376 vcpu->stat.dtlb_virt_miss_exits++;
379 /* Guest has mapped and accessed a page which is not
381 r = kvmppc_emulate_mmio(run, vcpu);
387 case BOOKE_INTERRUPT_ITLB_MISS: {
388 struct kvmppc_44x_tlbe *gtlbe;
389 unsigned long eaddr = vcpu->arch.pc;
394 /* Check the guest TLB. */
395 gtlbe = kvmppc_44x_itlb_search(vcpu, eaddr);
397 /* The guest didn't have a mapping for it. */
398 kvmppc_booke_queue_exception(vcpu, exit_nr);
399 vcpu->stat.itlb_real_miss_exits++;
403 vcpu->stat.itlb_virt_miss_exits++;
405 gfn = tlb_xlate(gtlbe, eaddr) >> PAGE_SHIFT;
407 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
408 /* The guest TLB had a mapping, but the shadow TLB
409 * didn't. This could be because:
410 * a) the entry is mapping the host kernel, or
411 * b) the guest used a large mapping which we're faking
412 * Either way, we need to satisfy the fault without
413 * invoking the guest. */
414 kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
417 /* Guest mapped and leaped at non-RAM! */
418 kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_MACHINE_CHECK);
424 case BOOKE_INTERRUPT_DEBUG: {
427 vcpu->arch.pc = mfspr(SPRN_CSRR0);
429 /* clear IAC events in DBSR register */
430 dbsr = mfspr(SPRN_DBSR);
431 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
432 mtspr(SPRN_DBSR, dbsr);
434 run->exit_reason = KVM_EXIT_DEBUG;
440 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
446 kvmppc_core_deliver_interrupts(vcpu);
448 /* Do some exit accounting. */
449 vcpu->stat.sum_exits++;
450 if (!(r & RESUME_HOST)) {
451 /* To avoid clobbering exit_reason, only check for signals if
452 * we aren't already exiting to userspace for some other
454 if (signal_pending(current)) {
455 run->exit_reason = KVM_EXIT_INTR;
456 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
458 vcpu->stat.signal_exits++;
460 vcpu->stat.light_exits++;
463 switch (run->exit_reason) {
465 vcpu->stat.mmio_exits++;
468 vcpu->stat.dcr_exits++;
471 vcpu->stat.signal_exits++;
479 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
480 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
484 vcpu->arch.gpr[1] = (16<<20) - 8; /* -8 for the callee-save LR slot */
486 vcpu->arch.shadow_pid = 1;
488 /* Eye-catching number so we know if the guest takes an interrupt
489 * before it's programmed its own IVPR. */
490 vcpu->arch.ivpr = 0x55550000;
492 return kvmppc_core_vcpu_setup(vcpu);
495 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
499 regs->pc = vcpu->arch.pc;
500 regs->cr = vcpu->arch.cr;
501 regs->ctr = vcpu->arch.ctr;
502 regs->lr = vcpu->arch.lr;
503 regs->xer = vcpu->arch.xer;
504 regs->msr = vcpu->arch.msr;
505 regs->srr0 = vcpu->arch.srr0;
506 regs->srr1 = vcpu->arch.srr1;
507 regs->pid = vcpu->arch.pid;
508 regs->sprg0 = vcpu->arch.sprg0;
509 regs->sprg1 = vcpu->arch.sprg1;
510 regs->sprg2 = vcpu->arch.sprg2;
511 regs->sprg3 = vcpu->arch.sprg3;
512 regs->sprg5 = vcpu->arch.sprg4;
513 regs->sprg6 = vcpu->arch.sprg5;
514 regs->sprg7 = vcpu->arch.sprg6;
516 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
517 regs->gpr[i] = vcpu->arch.gpr[i];
522 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
526 vcpu->arch.pc = regs->pc;
527 vcpu->arch.cr = regs->cr;
528 vcpu->arch.ctr = regs->ctr;
529 vcpu->arch.lr = regs->lr;
530 vcpu->arch.xer = regs->xer;
531 vcpu->arch.msr = regs->msr;
532 vcpu->arch.srr0 = regs->srr0;
533 vcpu->arch.srr1 = regs->srr1;
534 vcpu->arch.sprg0 = regs->sprg0;
535 vcpu->arch.sprg1 = regs->sprg1;
536 vcpu->arch.sprg2 = regs->sprg2;
537 vcpu->arch.sprg3 = regs->sprg3;
538 vcpu->arch.sprg5 = regs->sprg4;
539 vcpu->arch.sprg6 = regs->sprg5;
540 vcpu->arch.sprg7 = regs->sprg6;
542 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gpr); i++)
543 vcpu->arch.gpr[i] = regs->gpr[i];
548 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
549 struct kvm_sregs *sregs)
554 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
555 struct kvm_sregs *sregs)
560 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
565 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
570 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
571 struct kvm_translation *tr)
573 return kvmppc_core_vcpu_translate(vcpu, tr);
576 int kvmppc_booke_init(void)
578 unsigned long ivor[16];
579 unsigned long max_ivor = 0;
582 /* We install our own exception handlers by hijacking IVPR. IVPR must
583 * be 16-bit aligned, so we need a 64KB allocation. */
584 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
586 if (!kvmppc_booke_handlers)
589 /* XXX make sure our handlers are smaller than Linux's */
591 /* Copy our interrupt handlers to match host IVORs. That way we don't
592 * have to swap the IVORs on every guest/host transition. */
593 ivor[0] = mfspr(SPRN_IVOR0);
594 ivor[1] = mfspr(SPRN_IVOR1);
595 ivor[2] = mfspr(SPRN_IVOR2);
596 ivor[3] = mfspr(SPRN_IVOR3);
597 ivor[4] = mfspr(SPRN_IVOR4);
598 ivor[5] = mfspr(SPRN_IVOR5);
599 ivor[6] = mfspr(SPRN_IVOR6);
600 ivor[7] = mfspr(SPRN_IVOR7);
601 ivor[8] = mfspr(SPRN_IVOR8);
602 ivor[9] = mfspr(SPRN_IVOR9);
603 ivor[10] = mfspr(SPRN_IVOR10);
604 ivor[11] = mfspr(SPRN_IVOR11);
605 ivor[12] = mfspr(SPRN_IVOR12);
606 ivor[13] = mfspr(SPRN_IVOR13);
607 ivor[14] = mfspr(SPRN_IVOR14);
608 ivor[15] = mfspr(SPRN_IVOR15);
610 for (i = 0; i < 16; i++) {
611 if (ivor[i] > max_ivor)
614 memcpy((void *)kvmppc_booke_handlers + ivor[i],
615 kvmppc_handlers_start + i * kvmppc_handler_len,
618 flush_icache_range(kvmppc_booke_handlers,
619 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
624 void __exit kvmppc_booke_exit(void)
626 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);