Merge branch 'akpm'
[linux-2.6/next.git] / arch / powerpc / kvm / booke.c
blobee45fa01220ef11679ea081cdb5c9ccf4e1ab4d0
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.
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
16 * Copyright 2010-2011 Freescale Semiconductor, Inc.
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
22 #include <linux/errno.h>
23 #include <linux/err.h>
24 #include <linux/kvm_host.h>
25 #include <linux/gfp.h>
26 #include <linux/module.h>
27 #include <linux/vmalloc.h>
28 #include <linux/fs.h>
30 #include <asm/cputable.h>
31 #include <asm/uaccess.h>
32 #include <asm/kvm_ppc.h>
33 #include "timing.h"
34 #include <asm/cacheflush.h>
36 #include "booke.h"
38 unsigned long kvmppc_booke_handlers;
40 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
41 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
43 struct kvm_stats_debugfs_item debugfs_entries[] = {
44 { "mmio", VCPU_STAT(mmio_exits) },
45 { "dcr", VCPU_STAT(dcr_exits) },
46 { "sig", VCPU_STAT(signal_exits) },
47 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
48 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
49 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
50 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
51 { "sysc", VCPU_STAT(syscall_exits) },
52 { "isi", VCPU_STAT(isi_exits) },
53 { "dsi", VCPU_STAT(dsi_exits) },
54 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
55 { "dec", VCPU_STAT(dec_exits) },
56 { "ext_intr", VCPU_STAT(ext_intr_exits) },
57 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
58 { NULL }
61 /* TODO: use vcpu_printf() */
62 void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
64 int i;
66 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
67 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
68 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
69 vcpu->arch.shared->srr1);
71 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
73 for (i = 0; i < 32; i += 4) {
74 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
75 kvmppc_get_gpr(vcpu, i),
76 kvmppc_get_gpr(vcpu, i+1),
77 kvmppc_get_gpr(vcpu, i+2),
78 kvmppc_get_gpr(vcpu, i+3));
82 #ifdef CONFIG_SPE
83 void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
85 preempt_disable();
86 enable_kernel_spe();
87 kvmppc_save_guest_spe(vcpu);
88 vcpu->arch.shadow_msr &= ~MSR_SPE;
89 preempt_enable();
92 static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
94 preempt_disable();
95 enable_kernel_spe();
96 kvmppc_load_guest_spe(vcpu);
97 vcpu->arch.shadow_msr |= MSR_SPE;
98 preempt_enable();
101 static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
103 if (vcpu->arch.shared->msr & MSR_SPE) {
104 if (!(vcpu->arch.shadow_msr & MSR_SPE))
105 kvmppc_vcpu_enable_spe(vcpu);
106 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
107 kvmppc_vcpu_disable_spe(vcpu);
110 #else
111 static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
114 #endif
117 * Helper function for "full" MSR writes. No need to call this if only
118 * EE/CE/ME/DE/RI are changing.
120 void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
122 u32 old_msr = vcpu->arch.shared->msr;
124 vcpu->arch.shared->msr = new_msr;
126 kvmppc_mmu_msr_notify(vcpu, old_msr);
128 if (vcpu->arch.shared->msr & MSR_WE) {
129 kvm_vcpu_block(vcpu);
130 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
133 kvmppc_vcpu_sync_spe(vcpu);
136 static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
137 unsigned int priority)
139 set_bit(priority, &vcpu->arch.pending_exceptions);
142 static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
143 ulong dear_flags, ulong esr_flags)
145 vcpu->arch.queued_dear = dear_flags;
146 vcpu->arch.queued_esr = esr_flags;
147 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
150 static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
151 ulong dear_flags, ulong esr_flags)
153 vcpu->arch.queued_dear = dear_flags;
154 vcpu->arch.queued_esr = esr_flags;
155 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
158 static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
159 ulong esr_flags)
161 vcpu->arch.queued_esr = esr_flags;
162 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
165 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
167 vcpu->arch.queued_esr = esr_flags;
168 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
171 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
173 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
176 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
178 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
181 void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
183 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
186 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
187 struct kvm_interrupt *irq)
189 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
191 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
192 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
194 kvmppc_booke_queue_irqprio(vcpu, prio);
197 void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
198 struct kvm_interrupt *irq)
200 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
201 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
204 /* Deliver the interrupt of the corresponding priority, if possible. */
205 static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
206 unsigned int priority)
208 int allowed = 0;
209 ulong uninitialized_var(msr_mask);
210 bool update_esr = false, update_dear = false;
211 ulong crit_raw = vcpu->arch.shared->critical;
212 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
213 bool crit;
214 bool keep_irq = false;
216 /* Truncate crit indicators in 32 bit mode */
217 if (!(vcpu->arch.shared->msr & MSR_SF)) {
218 crit_raw &= 0xffffffff;
219 crit_r1 &= 0xffffffff;
222 /* Critical section when crit == r1 */
223 crit = (crit_raw == crit_r1);
224 /* ... and we're in supervisor mode */
225 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
227 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
228 priority = BOOKE_IRQPRIO_EXTERNAL;
229 keep_irq = true;
232 switch (priority) {
233 case BOOKE_IRQPRIO_DTLB_MISS:
234 case BOOKE_IRQPRIO_DATA_STORAGE:
235 update_dear = true;
236 /* fall through */
237 case BOOKE_IRQPRIO_INST_STORAGE:
238 case BOOKE_IRQPRIO_PROGRAM:
239 update_esr = true;
240 /* fall through */
241 case BOOKE_IRQPRIO_ITLB_MISS:
242 case BOOKE_IRQPRIO_SYSCALL:
243 case BOOKE_IRQPRIO_FP_UNAVAIL:
244 case BOOKE_IRQPRIO_SPE_UNAVAIL:
245 case BOOKE_IRQPRIO_SPE_FP_DATA:
246 case BOOKE_IRQPRIO_SPE_FP_ROUND:
247 case BOOKE_IRQPRIO_AP_UNAVAIL:
248 case BOOKE_IRQPRIO_ALIGNMENT:
249 allowed = 1;
250 msr_mask = MSR_CE|MSR_ME|MSR_DE;
251 break;
252 case BOOKE_IRQPRIO_CRITICAL:
253 case BOOKE_IRQPRIO_WATCHDOG:
254 allowed = vcpu->arch.shared->msr & MSR_CE;
255 msr_mask = MSR_ME;
256 break;
257 case BOOKE_IRQPRIO_MACHINE_CHECK:
258 allowed = vcpu->arch.shared->msr & MSR_ME;
259 msr_mask = 0;
260 break;
261 case BOOKE_IRQPRIO_EXTERNAL:
262 case BOOKE_IRQPRIO_DECREMENTER:
263 case BOOKE_IRQPRIO_FIT:
264 allowed = vcpu->arch.shared->msr & MSR_EE;
265 allowed = allowed && !crit;
266 msr_mask = MSR_CE|MSR_ME|MSR_DE;
267 break;
268 case BOOKE_IRQPRIO_DEBUG:
269 allowed = vcpu->arch.shared->msr & MSR_DE;
270 msr_mask = MSR_ME;
271 break;
274 if (allowed) {
275 vcpu->arch.shared->srr0 = vcpu->arch.pc;
276 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr;
277 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
278 if (update_esr == true)
279 vcpu->arch.esr = vcpu->arch.queued_esr;
280 if (update_dear == true)
281 vcpu->arch.shared->dar = vcpu->arch.queued_dear;
282 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
284 if (!keep_irq)
285 clear_bit(priority, &vcpu->arch.pending_exceptions);
288 return allowed;
291 /* Check pending exceptions and deliver one, if possible. */
292 void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
294 unsigned long *pending = &vcpu->arch.pending_exceptions;
295 unsigned long old_pending = vcpu->arch.pending_exceptions;
296 unsigned int priority;
298 priority = __ffs(*pending);
299 while (priority <= BOOKE_IRQPRIO_MAX) {
300 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
301 break;
303 priority = find_next_bit(pending,
304 BITS_PER_BYTE * sizeof(*pending),
305 priority + 1);
308 /* Tell the guest about our interrupt status */
309 if (*pending)
310 vcpu->arch.shared->int_pending = 1;
311 else if (old_pending)
312 vcpu->arch.shared->int_pending = 0;
315 int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
317 int ret;
319 local_irq_disable();
320 kvm_guest_enter();
321 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
322 kvm_guest_exit();
323 local_irq_enable();
325 return ret;
329 * kvmppc_handle_exit
331 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
333 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
334 unsigned int exit_nr)
336 enum emulation_result er;
337 int r = RESUME_HOST;
339 /* update before a new last_exit_type is rewritten */
340 kvmppc_update_timing_stats(vcpu);
342 local_irq_enable();
344 run->exit_reason = KVM_EXIT_UNKNOWN;
345 run->ready_for_interrupt_injection = 1;
347 switch (exit_nr) {
348 case BOOKE_INTERRUPT_MACHINE_CHECK:
349 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
350 kvmppc_dump_vcpu(vcpu);
351 r = RESUME_HOST;
352 break;
354 case BOOKE_INTERRUPT_EXTERNAL:
355 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
356 if (need_resched())
357 cond_resched();
358 r = RESUME_GUEST;
359 break;
361 case BOOKE_INTERRUPT_DECREMENTER:
362 /* Since we switched IVPR back to the host's value, the host
363 * handled this interrupt the moment we enabled interrupts.
364 * Now we just offer it a chance to reschedule the guest. */
365 kvmppc_account_exit(vcpu, DEC_EXITS);
366 if (need_resched())
367 cond_resched();
368 r = RESUME_GUEST;
369 break;
371 case BOOKE_INTERRUPT_PROGRAM:
372 if (vcpu->arch.shared->msr & MSR_PR) {
373 /* Program traps generated by user-level software must be handled
374 * by the guest kernel. */
375 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
376 r = RESUME_GUEST;
377 kvmppc_account_exit(vcpu, USR_PR_INST);
378 break;
381 er = kvmppc_emulate_instruction(run, vcpu);
382 switch (er) {
383 case EMULATE_DONE:
384 /* don't overwrite subtypes, just account kvm_stats */
385 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
386 /* Future optimization: only reload non-volatiles if
387 * they were actually modified by emulation. */
388 r = RESUME_GUEST_NV;
389 break;
390 case EMULATE_DO_DCR:
391 run->exit_reason = KVM_EXIT_DCR;
392 r = RESUME_HOST;
393 break;
394 case EMULATE_FAIL:
395 /* XXX Deliver Program interrupt to guest. */
396 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
397 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
398 /* For debugging, encode the failing instruction and
399 * report it to userspace. */
400 run->hw.hardware_exit_reason = ~0ULL << 32;
401 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
402 r = RESUME_HOST;
403 break;
404 default:
405 BUG();
407 break;
409 case BOOKE_INTERRUPT_FP_UNAVAIL:
410 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
411 kvmppc_account_exit(vcpu, FP_UNAVAIL);
412 r = RESUME_GUEST;
413 break;
415 #ifdef CONFIG_SPE
416 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
417 if (vcpu->arch.shared->msr & MSR_SPE)
418 kvmppc_vcpu_enable_spe(vcpu);
419 else
420 kvmppc_booke_queue_irqprio(vcpu,
421 BOOKE_IRQPRIO_SPE_UNAVAIL);
422 r = RESUME_GUEST;
423 break;
426 case BOOKE_INTERRUPT_SPE_FP_DATA:
427 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
428 r = RESUME_GUEST;
429 break;
431 case BOOKE_INTERRUPT_SPE_FP_ROUND:
432 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
433 r = RESUME_GUEST;
434 break;
435 #else
436 case BOOKE_INTERRUPT_SPE_UNAVAIL:
438 * Guest wants SPE, but host kernel doesn't support it. Send
439 * an "unimplemented operation" program check to the guest.
441 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
442 r = RESUME_GUEST;
443 break;
446 * These really should never happen without CONFIG_SPE,
447 * as we should never enable the real MSR[SPE] in the guest.
449 case BOOKE_INTERRUPT_SPE_FP_DATA:
450 case BOOKE_INTERRUPT_SPE_FP_ROUND:
451 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
452 __func__, exit_nr, vcpu->arch.pc);
453 run->hw.hardware_exit_reason = exit_nr;
454 r = RESUME_HOST;
455 break;
456 #endif
458 case BOOKE_INTERRUPT_DATA_STORAGE:
459 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
460 vcpu->arch.fault_esr);
461 kvmppc_account_exit(vcpu, DSI_EXITS);
462 r = RESUME_GUEST;
463 break;
465 case BOOKE_INTERRUPT_INST_STORAGE:
466 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
467 kvmppc_account_exit(vcpu, ISI_EXITS);
468 r = RESUME_GUEST;
469 break;
471 case BOOKE_INTERRUPT_SYSCALL:
472 if (!(vcpu->arch.shared->msr & MSR_PR) &&
473 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
474 /* KVM PV hypercalls */
475 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
476 r = RESUME_GUEST;
477 } else {
478 /* Guest syscalls */
479 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
481 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
482 r = RESUME_GUEST;
483 break;
485 case BOOKE_INTERRUPT_DTLB_MISS: {
486 unsigned long eaddr = vcpu->arch.fault_dear;
487 int gtlb_index;
488 gpa_t gpaddr;
489 gfn_t gfn;
491 #ifdef CONFIG_KVM_E500
492 if (!(vcpu->arch.shared->msr & MSR_PR) &&
493 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
494 kvmppc_map_magic(vcpu);
495 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
496 r = RESUME_GUEST;
498 break;
500 #endif
502 /* Check the guest TLB. */
503 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
504 if (gtlb_index < 0) {
505 /* The guest didn't have a mapping for it. */
506 kvmppc_core_queue_dtlb_miss(vcpu,
507 vcpu->arch.fault_dear,
508 vcpu->arch.fault_esr);
509 kvmppc_mmu_dtlb_miss(vcpu);
510 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
511 r = RESUME_GUEST;
512 break;
515 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
516 gfn = gpaddr >> PAGE_SHIFT;
518 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
519 /* The guest TLB had a mapping, but the shadow TLB
520 * didn't, and it is RAM. This could be because:
521 * a) the entry is mapping the host kernel, or
522 * b) the guest used a large mapping which we're faking
523 * Either way, we need to satisfy the fault without
524 * invoking the guest. */
525 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
526 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
527 r = RESUME_GUEST;
528 } else {
529 /* Guest has mapped and accessed a page which is not
530 * actually RAM. */
531 vcpu->arch.paddr_accessed = gpaddr;
532 r = kvmppc_emulate_mmio(run, vcpu);
533 kvmppc_account_exit(vcpu, MMIO_EXITS);
536 break;
539 case BOOKE_INTERRUPT_ITLB_MISS: {
540 unsigned long eaddr = vcpu->arch.pc;
541 gpa_t gpaddr;
542 gfn_t gfn;
543 int gtlb_index;
545 r = RESUME_GUEST;
547 /* Check the guest TLB. */
548 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
549 if (gtlb_index < 0) {
550 /* The guest didn't have a mapping for it. */
551 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
552 kvmppc_mmu_itlb_miss(vcpu);
553 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
554 break;
557 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
559 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
560 gfn = gpaddr >> PAGE_SHIFT;
562 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
563 /* The guest TLB had a mapping, but the shadow TLB
564 * didn't. This could be because:
565 * a) the entry is mapping the host kernel, or
566 * b) the guest used a large mapping which we're faking
567 * Either way, we need to satisfy the fault without
568 * invoking the guest. */
569 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
570 } else {
571 /* Guest mapped and leaped at non-RAM! */
572 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
575 break;
578 case BOOKE_INTERRUPT_DEBUG: {
579 u32 dbsr;
581 vcpu->arch.pc = mfspr(SPRN_CSRR0);
583 /* clear IAC events in DBSR register */
584 dbsr = mfspr(SPRN_DBSR);
585 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
586 mtspr(SPRN_DBSR, dbsr);
588 run->exit_reason = KVM_EXIT_DEBUG;
589 kvmppc_account_exit(vcpu, DEBUG_EXITS);
590 r = RESUME_HOST;
591 break;
594 default:
595 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
596 BUG();
599 local_irq_disable();
601 kvmppc_core_deliver_interrupts(vcpu);
603 if (!(r & RESUME_HOST)) {
604 /* To avoid clobbering exit_reason, only check for signals if
605 * we aren't already exiting to userspace for some other
606 * reason. */
607 if (signal_pending(current)) {
608 run->exit_reason = KVM_EXIT_INTR;
609 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
610 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
614 return r;
617 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
618 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
620 int i;
622 vcpu->arch.pc = 0;
623 vcpu->arch.shared->msr = 0;
624 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
625 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
627 vcpu->arch.shadow_pid = 1;
629 /* Eye-catching numbers so we know if the guest takes an interrupt
630 * before it's programmed its own IVPR/IVORs. */
631 vcpu->arch.ivpr = 0x55550000;
632 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
633 vcpu->arch.ivor[i] = 0x7700 | i * 4;
635 kvmppc_init_timing_stats(vcpu);
637 return kvmppc_core_vcpu_setup(vcpu);
640 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
642 int i;
644 regs->pc = vcpu->arch.pc;
645 regs->cr = kvmppc_get_cr(vcpu);
646 regs->ctr = vcpu->arch.ctr;
647 regs->lr = vcpu->arch.lr;
648 regs->xer = kvmppc_get_xer(vcpu);
649 regs->msr = vcpu->arch.shared->msr;
650 regs->srr0 = vcpu->arch.shared->srr0;
651 regs->srr1 = vcpu->arch.shared->srr1;
652 regs->pid = vcpu->arch.pid;
653 regs->sprg0 = vcpu->arch.shared->sprg0;
654 regs->sprg1 = vcpu->arch.shared->sprg1;
655 regs->sprg2 = vcpu->arch.shared->sprg2;
656 regs->sprg3 = vcpu->arch.shared->sprg3;
657 regs->sprg4 = vcpu->arch.sprg4;
658 regs->sprg5 = vcpu->arch.sprg5;
659 regs->sprg6 = vcpu->arch.sprg6;
660 regs->sprg7 = vcpu->arch.sprg7;
662 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
663 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
665 return 0;
668 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
670 int i;
672 vcpu->arch.pc = regs->pc;
673 kvmppc_set_cr(vcpu, regs->cr);
674 vcpu->arch.ctr = regs->ctr;
675 vcpu->arch.lr = regs->lr;
676 kvmppc_set_xer(vcpu, regs->xer);
677 kvmppc_set_msr(vcpu, regs->msr);
678 vcpu->arch.shared->srr0 = regs->srr0;
679 vcpu->arch.shared->srr1 = regs->srr1;
680 kvmppc_set_pid(vcpu, regs->pid);
681 vcpu->arch.shared->sprg0 = regs->sprg0;
682 vcpu->arch.shared->sprg1 = regs->sprg1;
683 vcpu->arch.shared->sprg2 = regs->sprg2;
684 vcpu->arch.shared->sprg3 = regs->sprg3;
685 vcpu->arch.sprg4 = regs->sprg4;
686 vcpu->arch.sprg5 = regs->sprg5;
687 vcpu->arch.sprg6 = regs->sprg6;
688 vcpu->arch.sprg7 = regs->sprg7;
690 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
691 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
693 return 0;
696 static void get_sregs_base(struct kvm_vcpu *vcpu,
697 struct kvm_sregs *sregs)
699 u64 tb = get_tb();
701 sregs->u.e.features |= KVM_SREGS_E_BASE;
703 sregs->u.e.csrr0 = vcpu->arch.csrr0;
704 sregs->u.e.csrr1 = vcpu->arch.csrr1;
705 sregs->u.e.mcsr = vcpu->arch.mcsr;
706 sregs->u.e.esr = vcpu->arch.esr;
707 sregs->u.e.dear = vcpu->arch.shared->dar;
708 sregs->u.e.tsr = vcpu->arch.tsr;
709 sregs->u.e.tcr = vcpu->arch.tcr;
710 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
711 sregs->u.e.tb = tb;
712 sregs->u.e.vrsave = vcpu->arch.vrsave;
715 static int set_sregs_base(struct kvm_vcpu *vcpu,
716 struct kvm_sregs *sregs)
718 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
719 return 0;
721 vcpu->arch.csrr0 = sregs->u.e.csrr0;
722 vcpu->arch.csrr1 = sregs->u.e.csrr1;
723 vcpu->arch.mcsr = sregs->u.e.mcsr;
724 vcpu->arch.esr = sregs->u.e.esr;
725 vcpu->arch.shared->dar = sregs->u.e.dear;
726 vcpu->arch.vrsave = sregs->u.e.vrsave;
727 vcpu->arch.tcr = sregs->u.e.tcr;
729 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC)
730 vcpu->arch.dec = sregs->u.e.dec;
732 kvmppc_emulate_dec(vcpu);
734 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
736 * FIXME: existing KVM timer handling is incomplete.
737 * TSR cannot be read by the guest, and its value in
738 * vcpu->arch is always zero. For now, just handle
739 * the case where the caller is trying to inject a
740 * decrementer interrupt.
743 if ((sregs->u.e.tsr & TSR_DIS) &&
744 (vcpu->arch.tcr & TCR_DIE))
745 kvmppc_core_queue_dec(vcpu);
748 return 0;
751 static void get_sregs_arch206(struct kvm_vcpu *vcpu,
752 struct kvm_sregs *sregs)
754 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
756 sregs->u.e.pir = 0;
757 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
758 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
759 sregs->u.e.decar = vcpu->arch.decar;
760 sregs->u.e.ivpr = vcpu->arch.ivpr;
763 static int set_sregs_arch206(struct kvm_vcpu *vcpu,
764 struct kvm_sregs *sregs)
766 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
767 return 0;
769 if (sregs->u.e.pir != 0)
770 return -EINVAL;
772 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
773 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
774 vcpu->arch.decar = sregs->u.e.decar;
775 vcpu->arch.ivpr = sregs->u.e.ivpr;
777 return 0;
780 void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
782 sregs->u.e.features |= KVM_SREGS_E_IVOR;
784 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
785 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
786 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
787 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
788 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
789 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
790 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
791 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
792 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
793 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
794 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
795 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
796 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
797 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
798 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
799 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
802 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
804 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
805 return 0;
807 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
808 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
809 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
810 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
811 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
812 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
813 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
814 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
815 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
816 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
817 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
818 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
819 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
820 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
821 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
822 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
824 return 0;
827 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
828 struct kvm_sregs *sregs)
830 sregs->pvr = vcpu->arch.pvr;
832 get_sregs_base(vcpu, sregs);
833 get_sregs_arch206(vcpu, sregs);
834 kvmppc_core_get_sregs(vcpu, sregs);
835 return 0;
838 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
839 struct kvm_sregs *sregs)
841 int ret;
843 if (vcpu->arch.pvr != sregs->pvr)
844 return -EINVAL;
846 ret = set_sregs_base(vcpu, sregs);
847 if (ret < 0)
848 return ret;
850 ret = set_sregs_arch206(vcpu, sregs);
851 if (ret < 0)
852 return ret;
854 return kvmppc_core_set_sregs(vcpu, sregs);
857 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
859 return -ENOTSUPP;
862 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
864 return -ENOTSUPP;
867 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
868 struct kvm_translation *tr)
870 int r;
872 r = kvmppc_core_vcpu_translate(vcpu, tr);
873 return r;
876 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
878 return -ENOTSUPP;
881 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
882 struct kvm_userspace_memory_region *mem)
884 return 0;
887 void kvmppc_core_commit_memory_region(struct kvm *kvm,
888 struct kvm_userspace_memory_region *mem)
892 int kvmppc_core_init_vm(struct kvm *kvm)
894 return 0;
897 void kvmppc_core_destroy_vm(struct kvm *kvm)
901 int __init kvmppc_booke_init(void)
903 unsigned long ivor[16];
904 unsigned long max_ivor = 0;
905 int i;
907 /* We install our own exception handlers by hijacking IVPR. IVPR must
908 * be 16-bit aligned, so we need a 64KB allocation. */
909 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
910 VCPU_SIZE_ORDER);
911 if (!kvmppc_booke_handlers)
912 return -ENOMEM;
914 /* XXX make sure our handlers are smaller than Linux's */
916 /* Copy our interrupt handlers to match host IVORs. That way we don't
917 * have to swap the IVORs on every guest/host transition. */
918 ivor[0] = mfspr(SPRN_IVOR0);
919 ivor[1] = mfspr(SPRN_IVOR1);
920 ivor[2] = mfspr(SPRN_IVOR2);
921 ivor[3] = mfspr(SPRN_IVOR3);
922 ivor[4] = mfspr(SPRN_IVOR4);
923 ivor[5] = mfspr(SPRN_IVOR5);
924 ivor[6] = mfspr(SPRN_IVOR6);
925 ivor[7] = mfspr(SPRN_IVOR7);
926 ivor[8] = mfspr(SPRN_IVOR8);
927 ivor[9] = mfspr(SPRN_IVOR9);
928 ivor[10] = mfspr(SPRN_IVOR10);
929 ivor[11] = mfspr(SPRN_IVOR11);
930 ivor[12] = mfspr(SPRN_IVOR12);
931 ivor[13] = mfspr(SPRN_IVOR13);
932 ivor[14] = mfspr(SPRN_IVOR14);
933 ivor[15] = mfspr(SPRN_IVOR15);
935 for (i = 0; i < 16; i++) {
936 if (ivor[i] > max_ivor)
937 max_ivor = ivor[i];
939 memcpy((void *)kvmppc_booke_handlers + ivor[i],
940 kvmppc_handlers_start + i * kvmppc_handler_len,
941 kvmppc_handler_len);
943 flush_icache_range(kvmppc_booke_handlers,
944 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
946 return 0;
949 void __exit kvmppc_booke_exit(void)
951 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
952 kvm_exit();