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>
20 * Scott Wood <scottwood@freescale.com>
21 * Varun Sethi <varun.sethi@freescale.com>
24 #include <linux/errno.h>
25 #include <linux/err.h>
26 #include <linux/kvm_host.h>
27 #include <linux/gfp.h>
28 #include <linux/module.h>
29 #include <linux/vmalloc.h>
32 #include <asm/cputable.h>
33 #include <asm/uaccess.h>
34 #include <asm/kvm_ppc.h>
35 #include <asm/cacheflush.h>
36 #include <asm/dbell.h>
37 #include <asm/hw_irq.h>
43 unsigned long kvmppc_booke_handlers
;
45 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
46 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
48 struct kvm_stats_debugfs_item debugfs_entries
[] = {
49 { "mmio", VCPU_STAT(mmio_exits
) },
50 { "dcr", VCPU_STAT(dcr_exits
) },
51 { "sig", VCPU_STAT(signal_exits
) },
52 { "itlb_r", VCPU_STAT(itlb_real_miss_exits
) },
53 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits
) },
54 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits
) },
55 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits
) },
56 { "sysc", VCPU_STAT(syscall_exits
) },
57 { "isi", VCPU_STAT(isi_exits
) },
58 { "dsi", VCPU_STAT(dsi_exits
) },
59 { "inst_emu", VCPU_STAT(emulated_inst_exits
) },
60 { "dec", VCPU_STAT(dec_exits
) },
61 { "ext_intr", VCPU_STAT(ext_intr_exits
) },
62 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
63 { "doorbell", VCPU_STAT(dbell_exits
) },
64 { "guest doorbell", VCPU_STAT(gdbell_exits
) },
68 /* TODO: use vcpu_printf() */
69 void kvmppc_dump_vcpu(struct kvm_vcpu
*vcpu
)
73 printk("pc: %08lx msr: %08llx\n", vcpu
->arch
.pc
, vcpu
->arch
.shared
->msr
);
74 printk("lr: %08lx ctr: %08lx\n", vcpu
->arch
.lr
, vcpu
->arch
.ctr
);
75 printk("srr0: %08llx srr1: %08llx\n", vcpu
->arch
.shared
->srr0
,
76 vcpu
->arch
.shared
->srr1
);
78 printk("exceptions: %08lx\n", vcpu
->arch
.pending_exceptions
);
80 for (i
= 0; i
< 32; i
+= 4) {
81 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i
,
82 kvmppc_get_gpr(vcpu
, i
),
83 kvmppc_get_gpr(vcpu
, i
+1),
84 kvmppc_get_gpr(vcpu
, i
+2),
85 kvmppc_get_gpr(vcpu
, i
+3));
90 void kvmppc_vcpu_disable_spe(struct kvm_vcpu
*vcpu
)
94 kvmppc_save_guest_spe(vcpu
);
95 vcpu
->arch
.shadow_msr
&= ~MSR_SPE
;
99 static void kvmppc_vcpu_enable_spe(struct kvm_vcpu
*vcpu
)
103 kvmppc_load_guest_spe(vcpu
);
104 vcpu
->arch
.shadow_msr
|= MSR_SPE
;
108 static void kvmppc_vcpu_sync_spe(struct kvm_vcpu
*vcpu
)
110 if (vcpu
->arch
.shared
->msr
& MSR_SPE
) {
111 if (!(vcpu
->arch
.shadow_msr
& MSR_SPE
))
112 kvmppc_vcpu_enable_spe(vcpu
);
113 } else if (vcpu
->arch
.shadow_msr
& MSR_SPE
) {
114 kvmppc_vcpu_disable_spe(vcpu
);
118 static void kvmppc_vcpu_sync_spe(struct kvm_vcpu
*vcpu
)
124 * Helper function for "full" MSR writes. No need to call this if only
125 * EE/CE/ME/DE/RI are changing.
127 void kvmppc_set_msr(struct kvm_vcpu
*vcpu
, u32 new_msr
)
129 u32 old_msr
= vcpu
->arch
.shared
->msr
;
131 #ifdef CONFIG_KVM_BOOKE_HV
135 vcpu
->arch
.shared
->msr
= new_msr
;
137 kvmppc_mmu_msr_notify(vcpu
, old_msr
);
138 kvmppc_vcpu_sync_spe(vcpu
);
141 static void kvmppc_booke_queue_irqprio(struct kvm_vcpu
*vcpu
,
142 unsigned int priority
)
144 set_bit(priority
, &vcpu
->arch
.pending_exceptions
);
147 static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu
*vcpu
,
148 ulong dear_flags
, ulong esr_flags
)
150 vcpu
->arch
.queued_dear
= dear_flags
;
151 vcpu
->arch
.queued_esr
= esr_flags
;
152 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_DTLB_MISS
);
155 static void kvmppc_core_queue_data_storage(struct kvm_vcpu
*vcpu
,
156 ulong dear_flags
, ulong esr_flags
)
158 vcpu
->arch
.queued_dear
= dear_flags
;
159 vcpu
->arch
.queued_esr
= esr_flags
;
160 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_DATA_STORAGE
);
163 static void kvmppc_core_queue_inst_storage(struct kvm_vcpu
*vcpu
,
166 vcpu
->arch
.queued_esr
= esr_flags
;
167 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_INST_STORAGE
);
170 void kvmppc_core_queue_program(struct kvm_vcpu
*vcpu
, ulong esr_flags
)
172 vcpu
->arch
.queued_esr
= esr_flags
;
173 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_PROGRAM
);
176 void kvmppc_core_queue_dec(struct kvm_vcpu
*vcpu
)
178 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_DECREMENTER
);
181 int kvmppc_core_pending_dec(struct kvm_vcpu
*vcpu
)
183 return test_bit(BOOKE_IRQPRIO_DECREMENTER
, &vcpu
->arch
.pending_exceptions
);
186 void kvmppc_core_dequeue_dec(struct kvm_vcpu
*vcpu
)
188 clear_bit(BOOKE_IRQPRIO_DECREMENTER
, &vcpu
->arch
.pending_exceptions
);
191 void kvmppc_core_queue_external(struct kvm_vcpu
*vcpu
,
192 struct kvm_interrupt
*irq
)
194 unsigned int prio
= BOOKE_IRQPRIO_EXTERNAL
;
196 if (irq
->irq
== KVM_INTERRUPT_SET_LEVEL
)
197 prio
= BOOKE_IRQPRIO_EXTERNAL_LEVEL
;
199 kvmppc_booke_queue_irqprio(vcpu
, prio
);
202 void kvmppc_core_dequeue_external(struct kvm_vcpu
*vcpu
,
203 struct kvm_interrupt
*irq
)
205 clear_bit(BOOKE_IRQPRIO_EXTERNAL
, &vcpu
->arch
.pending_exceptions
);
206 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL
, &vcpu
->arch
.pending_exceptions
);
209 static void set_guest_srr(struct kvm_vcpu
*vcpu
, unsigned long srr0
, u32 srr1
)
211 #ifdef CONFIG_KVM_BOOKE_HV
212 mtspr(SPRN_GSRR0
, srr0
);
213 mtspr(SPRN_GSRR1
, srr1
);
215 vcpu
->arch
.shared
->srr0
= srr0
;
216 vcpu
->arch
.shared
->srr1
= srr1
;
220 static void set_guest_csrr(struct kvm_vcpu
*vcpu
, unsigned long srr0
, u32 srr1
)
222 vcpu
->arch
.csrr0
= srr0
;
223 vcpu
->arch
.csrr1
= srr1
;
226 static void set_guest_dsrr(struct kvm_vcpu
*vcpu
, unsigned long srr0
, u32 srr1
)
228 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC
)) {
229 vcpu
->arch
.dsrr0
= srr0
;
230 vcpu
->arch
.dsrr1
= srr1
;
232 set_guest_csrr(vcpu
, srr0
, srr1
);
236 static void set_guest_mcsrr(struct kvm_vcpu
*vcpu
, unsigned long srr0
, u32 srr1
)
238 vcpu
->arch
.mcsrr0
= srr0
;
239 vcpu
->arch
.mcsrr1
= srr1
;
242 static unsigned long get_guest_dear(struct kvm_vcpu
*vcpu
)
244 #ifdef CONFIG_KVM_BOOKE_HV
245 return mfspr(SPRN_GDEAR
);
247 return vcpu
->arch
.shared
->dar
;
251 static void set_guest_dear(struct kvm_vcpu
*vcpu
, unsigned long dear
)
253 #ifdef CONFIG_KVM_BOOKE_HV
254 mtspr(SPRN_GDEAR
, dear
);
256 vcpu
->arch
.shared
->dar
= dear
;
260 static unsigned long get_guest_esr(struct kvm_vcpu
*vcpu
)
262 #ifdef CONFIG_KVM_BOOKE_HV
263 return mfspr(SPRN_GESR
);
265 return vcpu
->arch
.shared
->esr
;
269 static void set_guest_esr(struct kvm_vcpu
*vcpu
, u32 esr
)
271 #ifdef CONFIG_KVM_BOOKE_HV
272 mtspr(SPRN_GESR
, esr
);
274 vcpu
->arch
.shared
->esr
= esr
;
278 /* Deliver the interrupt of the corresponding priority, if possible. */
279 static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu
*vcpu
,
280 unsigned int priority
)
284 bool update_esr
= false, update_dear
= false;
285 ulong crit_raw
= vcpu
->arch
.shared
->critical
;
286 ulong crit_r1
= kvmppc_get_gpr(vcpu
, 1);
288 bool keep_irq
= false;
289 enum int_class int_class
;
291 /* Truncate crit indicators in 32 bit mode */
292 if (!(vcpu
->arch
.shared
->msr
& MSR_SF
)) {
293 crit_raw
&= 0xffffffff;
294 crit_r1
&= 0xffffffff;
297 /* Critical section when crit == r1 */
298 crit
= (crit_raw
== crit_r1
);
299 /* ... and we're in supervisor mode */
300 crit
= crit
&& !(vcpu
->arch
.shared
->msr
& MSR_PR
);
302 if (priority
== BOOKE_IRQPRIO_EXTERNAL_LEVEL
) {
303 priority
= BOOKE_IRQPRIO_EXTERNAL
;
308 case BOOKE_IRQPRIO_DTLB_MISS
:
309 case BOOKE_IRQPRIO_DATA_STORAGE
:
312 case BOOKE_IRQPRIO_INST_STORAGE
:
313 case BOOKE_IRQPRIO_PROGRAM
:
316 case BOOKE_IRQPRIO_ITLB_MISS
:
317 case BOOKE_IRQPRIO_SYSCALL
:
318 case BOOKE_IRQPRIO_FP_UNAVAIL
:
319 case BOOKE_IRQPRIO_SPE_UNAVAIL
:
320 case BOOKE_IRQPRIO_SPE_FP_DATA
:
321 case BOOKE_IRQPRIO_SPE_FP_ROUND
:
322 case BOOKE_IRQPRIO_AP_UNAVAIL
:
323 case BOOKE_IRQPRIO_ALIGNMENT
:
325 msr_mask
= MSR_CE
| MSR_ME
| MSR_DE
;
326 int_class
= INT_CLASS_NONCRIT
;
328 case BOOKE_IRQPRIO_CRITICAL
:
329 case BOOKE_IRQPRIO_DBELL_CRIT
:
330 allowed
= vcpu
->arch
.shared
->msr
& MSR_CE
;
331 allowed
= allowed
&& !crit
;
333 int_class
= INT_CLASS_CRIT
;
335 case BOOKE_IRQPRIO_MACHINE_CHECK
:
336 allowed
= vcpu
->arch
.shared
->msr
& MSR_ME
;
337 allowed
= allowed
&& !crit
;
338 int_class
= INT_CLASS_MC
;
340 case BOOKE_IRQPRIO_DECREMENTER
:
341 case BOOKE_IRQPRIO_FIT
:
344 case BOOKE_IRQPRIO_EXTERNAL
:
345 case BOOKE_IRQPRIO_DBELL
:
346 allowed
= vcpu
->arch
.shared
->msr
& MSR_EE
;
347 allowed
= allowed
&& !crit
;
348 msr_mask
= MSR_CE
| MSR_ME
| MSR_DE
;
349 int_class
= INT_CLASS_NONCRIT
;
351 case BOOKE_IRQPRIO_DEBUG
:
352 allowed
= vcpu
->arch
.shared
->msr
& MSR_DE
;
353 allowed
= allowed
&& !crit
;
355 int_class
= INT_CLASS_CRIT
;
361 case INT_CLASS_NONCRIT
:
362 set_guest_srr(vcpu
, vcpu
->arch
.pc
,
363 vcpu
->arch
.shared
->msr
);
366 set_guest_csrr(vcpu
, vcpu
->arch
.pc
,
367 vcpu
->arch
.shared
->msr
);
370 set_guest_dsrr(vcpu
, vcpu
->arch
.pc
,
371 vcpu
->arch
.shared
->msr
);
374 set_guest_mcsrr(vcpu
, vcpu
->arch
.pc
,
375 vcpu
->arch
.shared
->msr
);
379 vcpu
->arch
.pc
= vcpu
->arch
.ivpr
| vcpu
->arch
.ivor
[priority
];
380 if (update_esr
== true)
381 set_guest_esr(vcpu
, vcpu
->arch
.queued_esr
);
382 if (update_dear
== true)
383 set_guest_dear(vcpu
, vcpu
->arch
.queued_dear
);
384 kvmppc_set_msr(vcpu
, vcpu
->arch
.shared
->msr
& msr_mask
);
387 clear_bit(priority
, &vcpu
->arch
.pending_exceptions
);
390 #ifdef CONFIG_KVM_BOOKE_HV
392 * If an interrupt is pending but masked, raise a guest doorbell
393 * so that we are notified when the guest enables the relevant
396 if (vcpu
->arch
.pending_exceptions
& BOOKE_IRQMASK_EE
)
397 kvmppc_set_pending_interrupt(vcpu
, INT_CLASS_NONCRIT
);
398 if (vcpu
->arch
.pending_exceptions
& BOOKE_IRQMASK_CE
)
399 kvmppc_set_pending_interrupt(vcpu
, INT_CLASS_CRIT
);
400 if (vcpu
->arch
.pending_exceptions
& BOOKE_IRQPRIO_MACHINE_CHECK
)
401 kvmppc_set_pending_interrupt(vcpu
, INT_CLASS_MC
);
407 static void update_timer_ints(struct kvm_vcpu
*vcpu
)
409 if ((vcpu
->arch
.tcr
& TCR_DIE
) && (vcpu
->arch
.tsr
& TSR_DIS
))
410 kvmppc_core_queue_dec(vcpu
);
412 kvmppc_core_dequeue_dec(vcpu
);
415 static void kvmppc_core_check_exceptions(struct kvm_vcpu
*vcpu
)
417 unsigned long *pending
= &vcpu
->arch
.pending_exceptions
;
418 unsigned int priority
;
420 if (vcpu
->requests
) {
421 if (kvm_check_request(KVM_REQ_PENDING_TIMER
, vcpu
)) {
423 update_timer_ints(vcpu
);
427 priority
= __ffs(*pending
);
428 while (priority
< BOOKE_IRQPRIO_MAX
) {
429 if (kvmppc_booke_irqprio_deliver(vcpu
, priority
))
432 priority
= find_next_bit(pending
,
433 BITS_PER_BYTE
* sizeof(*pending
),
437 /* Tell the guest about our interrupt status */
438 vcpu
->arch
.shared
->int_pending
= !!*pending
;
441 /* Check pending exceptions and deliver one, if possible. */
442 int kvmppc_core_prepare_to_enter(struct kvm_vcpu
*vcpu
)
445 WARN_ON_ONCE(!irqs_disabled());
447 kvmppc_core_check_exceptions(vcpu
);
449 if (vcpu
->arch
.shared
->msr
& MSR_WE
) {
451 kvm_vcpu_block(vcpu
);
452 clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
455 kvmppc_set_exit_type(vcpu
, EMULATED_MTMSRWE_EXITS
);
463 * Common checks before entering the guest world. Call with interrupts
466 * returns !0 if a signal is pending and check_signal is true
468 static int kvmppc_prepare_to_enter(struct kvm_vcpu
*vcpu
)
472 WARN_ON_ONCE(!irqs_disabled());
474 if (need_resched()) {
481 if (signal_pending(current
)) {
486 if (kvmppc_core_prepare_to_enter(vcpu
)) {
487 /* interrupts got enabled in between, so we
488 are back at square 1 */
498 int kvmppc_vcpu_run(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
501 #ifdef CONFIG_PPC_FPU
507 if (!vcpu
->arch
.sane
) {
508 kvm_run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
513 if (kvmppc_prepare_to_enter(vcpu
)) {
514 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
521 #ifdef CONFIG_PPC_FPU
522 /* Save userspace FPU state in stack */
524 memcpy(fpr
, current
->thread
.fpr
, sizeof(current
->thread
.fpr
));
525 fpscr
= current
->thread
.fpscr
.val
;
526 fpexc_mode
= current
->thread
.fpexc_mode
;
528 /* Restore guest FPU state to thread */
529 memcpy(current
->thread
.fpr
, vcpu
->arch
.fpr
, sizeof(vcpu
->arch
.fpr
));
530 current
->thread
.fpscr
.val
= vcpu
->arch
.fpscr
;
533 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
534 * as always using the FPU. Kernel usage of FP (via
535 * enable_kernel_fp()) in this thread must not occur while
536 * vcpu->fpu_active is set.
538 vcpu
->fpu_active
= 1;
540 kvmppc_load_guest_fp(vcpu
);
543 ret
= __kvmppc_vcpu_run(kvm_run
, vcpu
);
545 #ifdef CONFIG_PPC_FPU
546 kvmppc_save_guest_fp(vcpu
);
548 vcpu
->fpu_active
= 0;
550 /* Save guest FPU state from thread */
551 memcpy(vcpu
->arch
.fpr
, current
->thread
.fpr
, sizeof(vcpu
->arch
.fpr
));
552 vcpu
->arch
.fpscr
= current
->thread
.fpscr
.val
;
554 /* Restore userspace FPU state from stack */
555 memcpy(current
->thread
.fpr
, fpr
, sizeof(current
->thread
.fpr
));
556 current
->thread
.fpscr
.val
= fpscr
;
557 current
->thread
.fpexc_mode
= fpexc_mode
;
567 static int emulation_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
569 enum emulation_result er
;
571 er
= kvmppc_emulate_instruction(run
, vcpu
);
574 /* don't overwrite subtypes, just account kvm_stats */
575 kvmppc_account_exit_stat(vcpu
, EMULATED_INST_EXITS
);
576 /* Future optimization: only reload non-volatiles if
577 * they were actually modified by emulation. */
578 return RESUME_GUEST_NV
;
581 run
->exit_reason
= KVM_EXIT_DCR
;
585 printk(KERN_CRIT
"%s: emulation at %lx failed (%08x)\n",
586 __func__
, vcpu
->arch
.pc
, vcpu
->arch
.last_inst
);
587 /* For debugging, encode the failing instruction and
588 * report it to userspace. */
589 run
->hw
.hardware_exit_reason
= ~0ULL << 32;
590 run
->hw
.hardware_exit_reason
|= vcpu
->arch
.last_inst
;
591 kvmppc_core_queue_program(vcpu
, ESR_PIL
);
599 static void kvmppc_fill_pt_regs(struct pt_regs
*regs
)
601 ulong r1
, ip
, msr
, lr
;
603 asm("mr %0, 1" : "=r"(r1
));
604 asm("mflr %0" : "=r"(lr
));
605 asm("mfmsr %0" : "=r"(msr
));
606 asm("bl 1f; 1: mflr %0" : "=r"(ip
));
608 memset(regs
, 0, sizeof(*regs
));
616 * For interrupts needed to be handled by host interrupt handlers,
617 * corresponding host handler are called from here in similar way
618 * (but not exact) as they are called from low level handler
619 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
621 static void kvmppc_restart_interrupt(struct kvm_vcpu
*vcpu
,
622 unsigned int exit_nr
)
627 case BOOKE_INTERRUPT_EXTERNAL
:
628 kvmppc_fill_pt_regs(®s
);
631 case BOOKE_INTERRUPT_DECREMENTER
:
632 kvmppc_fill_pt_regs(®s
);
633 timer_interrupt(®s
);
635 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
636 case BOOKE_INTERRUPT_DOORBELL
:
637 kvmppc_fill_pt_regs(®s
);
638 doorbell_exception(®s
);
641 case BOOKE_INTERRUPT_MACHINE_CHECK
:
644 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR
:
645 kvmppc_fill_pt_regs(®s
);
646 performance_monitor_exception(®s
);
648 case BOOKE_INTERRUPT_WATCHDOG
:
649 kvmppc_fill_pt_regs(®s
);
650 #ifdef CONFIG_BOOKE_WDT
651 WatchdogException(®s
);
653 unknown_exception(®s
);
656 case BOOKE_INTERRUPT_CRITICAL
:
657 unknown_exception(®s
);
665 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
667 int kvmppc_handle_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
668 unsigned int exit_nr
)
672 /* update before a new last_exit_type is rewritten */
673 kvmppc_update_timing_stats(vcpu
);
675 /* restart interrupts if they were meant for the host */
676 kvmppc_restart_interrupt(vcpu
, exit_nr
);
680 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
681 run
->ready_for_interrupt_injection
= 1;
684 case BOOKE_INTERRUPT_MACHINE_CHECK
:
685 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR
));
686 kvmppc_dump_vcpu(vcpu
);
687 /* For debugging, send invalid exit reason to user space */
688 run
->hw
.hardware_exit_reason
= ~1ULL << 32;
689 run
->hw
.hardware_exit_reason
|= mfspr(SPRN_MCSR
);
693 case BOOKE_INTERRUPT_EXTERNAL
:
694 kvmppc_account_exit(vcpu
, EXT_INTR_EXITS
);
698 case BOOKE_INTERRUPT_DECREMENTER
:
699 kvmppc_account_exit(vcpu
, DEC_EXITS
);
703 case BOOKE_INTERRUPT_WATCHDOG
:
707 case BOOKE_INTERRUPT_DOORBELL
:
708 kvmppc_account_exit(vcpu
, DBELL_EXITS
);
712 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT
:
713 kvmppc_account_exit(vcpu
, GDBELL_EXITS
);
716 * We are here because there is a pending guest interrupt
717 * which could not be delivered as MSR_CE or MSR_ME was not
718 * set. Once we break from here we will retry delivery.
723 case BOOKE_INTERRUPT_GUEST_DBELL
:
724 kvmppc_account_exit(vcpu
, GDBELL_EXITS
);
727 * We are here because there is a pending guest interrupt
728 * which could not be delivered as MSR_EE was not set. Once
729 * we break from here we will retry delivery.
734 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR
:
738 case BOOKE_INTERRUPT_HV_PRIV
:
739 r
= emulation_exit(run
, vcpu
);
742 case BOOKE_INTERRUPT_PROGRAM
:
743 if (vcpu
->arch
.shared
->msr
& (MSR_PR
| MSR_GS
)) {
745 * Program traps generated by user-level software must
746 * be handled by the guest kernel.
748 * In GS mode, hypervisor privileged instructions trap
749 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
750 * actual program interrupts, handled by the guest.
752 kvmppc_core_queue_program(vcpu
, vcpu
->arch
.fault_esr
);
754 kvmppc_account_exit(vcpu
, USR_PR_INST
);
758 r
= emulation_exit(run
, vcpu
);
761 case BOOKE_INTERRUPT_FP_UNAVAIL
:
762 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_FP_UNAVAIL
);
763 kvmppc_account_exit(vcpu
, FP_UNAVAIL
);
768 case BOOKE_INTERRUPT_SPE_UNAVAIL
: {
769 if (vcpu
->arch
.shared
->msr
& MSR_SPE
)
770 kvmppc_vcpu_enable_spe(vcpu
);
772 kvmppc_booke_queue_irqprio(vcpu
,
773 BOOKE_IRQPRIO_SPE_UNAVAIL
);
778 case BOOKE_INTERRUPT_SPE_FP_DATA
:
779 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_SPE_FP_DATA
);
783 case BOOKE_INTERRUPT_SPE_FP_ROUND
:
784 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_SPE_FP_ROUND
);
788 case BOOKE_INTERRUPT_SPE_UNAVAIL
:
790 * Guest wants SPE, but host kernel doesn't support it. Send
791 * an "unimplemented operation" program check to the guest.
793 kvmppc_core_queue_program(vcpu
, ESR_PUO
| ESR_SPV
);
798 * These really should never happen without CONFIG_SPE,
799 * as we should never enable the real MSR[SPE] in the guest.
801 case BOOKE_INTERRUPT_SPE_FP_DATA
:
802 case BOOKE_INTERRUPT_SPE_FP_ROUND
:
803 printk(KERN_CRIT
"%s: unexpected SPE interrupt %u at %08lx\n",
804 __func__
, exit_nr
, vcpu
->arch
.pc
);
805 run
->hw
.hardware_exit_reason
= exit_nr
;
810 case BOOKE_INTERRUPT_DATA_STORAGE
:
811 kvmppc_core_queue_data_storage(vcpu
, vcpu
->arch
.fault_dear
,
812 vcpu
->arch
.fault_esr
);
813 kvmppc_account_exit(vcpu
, DSI_EXITS
);
817 case BOOKE_INTERRUPT_INST_STORAGE
:
818 kvmppc_core_queue_inst_storage(vcpu
, vcpu
->arch
.fault_esr
);
819 kvmppc_account_exit(vcpu
, ISI_EXITS
);
823 #ifdef CONFIG_KVM_BOOKE_HV
824 case BOOKE_INTERRUPT_HV_SYSCALL
:
825 if (!(vcpu
->arch
.shared
->msr
& MSR_PR
)) {
826 kvmppc_set_gpr(vcpu
, 3, kvmppc_kvm_pv(vcpu
));
829 * hcall from guest userspace -- send privileged
830 * instruction program check.
832 kvmppc_core_queue_program(vcpu
, ESR_PPR
);
838 case BOOKE_INTERRUPT_SYSCALL
:
839 if (!(vcpu
->arch
.shared
->msr
& MSR_PR
) &&
840 (((u32
)kvmppc_get_gpr(vcpu
, 0)) == KVM_SC_MAGIC_R0
)) {
841 /* KVM PV hypercalls */
842 kvmppc_set_gpr(vcpu
, 3, kvmppc_kvm_pv(vcpu
));
846 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_SYSCALL
);
848 kvmppc_account_exit(vcpu
, SYSCALL_EXITS
);
853 case BOOKE_INTERRUPT_DTLB_MISS
: {
854 unsigned long eaddr
= vcpu
->arch
.fault_dear
;
859 #ifdef CONFIG_KVM_E500V2
860 if (!(vcpu
->arch
.shared
->msr
& MSR_PR
) &&
861 (eaddr
& PAGE_MASK
) == vcpu
->arch
.magic_page_ea
) {
862 kvmppc_map_magic(vcpu
);
863 kvmppc_account_exit(vcpu
, DTLB_VIRT_MISS_EXITS
);
870 /* Check the guest TLB. */
871 gtlb_index
= kvmppc_mmu_dtlb_index(vcpu
, eaddr
);
872 if (gtlb_index
< 0) {
873 /* The guest didn't have a mapping for it. */
874 kvmppc_core_queue_dtlb_miss(vcpu
,
875 vcpu
->arch
.fault_dear
,
876 vcpu
->arch
.fault_esr
);
877 kvmppc_mmu_dtlb_miss(vcpu
);
878 kvmppc_account_exit(vcpu
, DTLB_REAL_MISS_EXITS
);
883 gpaddr
= kvmppc_mmu_xlate(vcpu
, gtlb_index
, eaddr
);
884 gfn
= gpaddr
>> PAGE_SHIFT
;
886 if (kvm_is_visible_gfn(vcpu
->kvm
, gfn
)) {
887 /* The guest TLB had a mapping, but the shadow TLB
888 * didn't, and it is RAM. This could be because:
889 * a) the entry is mapping the host kernel, or
890 * b) the guest used a large mapping which we're faking
891 * Either way, we need to satisfy the fault without
892 * invoking the guest. */
893 kvmppc_mmu_map(vcpu
, eaddr
, gpaddr
, gtlb_index
);
894 kvmppc_account_exit(vcpu
, DTLB_VIRT_MISS_EXITS
);
897 /* Guest has mapped and accessed a page which is not
899 vcpu
->arch
.paddr_accessed
= gpaddr
;
900 vcpu
->arch
.vaddr_accessed
= eaddr
;
901 r
= kvmppc_emulate_mmio(run
, vcpu
);
902 kvmppc_account_exit(vcpu
, MMIO_EXITS
);
908 case BOOKE_INTERRUPT_ITLB_MISS
: {
909 unsigned long eaddr
= vcpu
->arch
.pc
;
916 /* Check the guest TLB. */
917 gtlb_index
= kvmppc_mmu_itlb_index(vcpu
, eaddr
);
918 if (gtlb_index
< 0) {
919 /* The guest didn't have a mapping for it. */
920 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_ITLB_MISS
);
921 kvmppc_mmu_itlb_miss(vcpu
);
922 kvmppc_account_exit(vcpu
, ITLB_REAL_MISS_EXITS
);
926 kvmppc_account_exit(vcpu
, ITLB_VIRT_MISS_EXITS
);
928 gpaddr
= kvmppc_mmu_xlate(vcpu
, gtlb_index
, eaddr
);
929 gfn
= gpaddr
>> PAGE_SHIFT
;
931 if (kvm_is_visible_gfn(vcpu
->kvm
, gfn
)) {
932 /* The guest TLB had a mapping, but the shadow TLB
933 * didn't. This could be because:
934 * a) the entry is mapping the host kernel, or
935 * b) the guest used a large mapping which we're faking
936 * Either way, we need to satisfy the fault without
937 * invoking the guest. */
938 kvmppc_mmu_map(vcpu
, eaddr
, gpaddr
, gtlb_index
);
940 /* Guest mapped and leaped at non-RAM! */
941 kvmppc_booke_queue_irqprio(vcpu
, BOOKE_IRQPRIO_MACHINE_CHECK
);
947 case BOOKE_INTERRUPT_DEBUG
: {
950 vcpu
->arch
.pc
= mfspr(SPRN_CSRR0
);
952 /* clear IAC events in DBSR register */
953 dbsr
= mfspr(SPRN_DBSR
);
954 dbsr
&= DBSR_IAC1
| DBSR_IAC2
| DBSR_IAC3
| DBSR_IAC4
;
955 mtspr(SPRN_DBSR
, dbsr
);
957 run
->exit_reason
= KVM_EXIT_DEBUG
;
958 kvmppc_account_exit(vcpu
, DEBUG_EXITS
);
964 printk(KERN_EMERG
"exit_nr %d\n", exit_nr
);
969 * To avoid clobbering exit_reason, only check for signals if we
970 * aren't already exiting to userspace for some other reason.
972 if (!(r
& RESUME_HOST
)) {
974 if (kvmppc_prepare_to_enter(vcpu
)) {
975 run
->exit_reason
= KVM_EXIT_INTR
;
976 r
= (-EINTR
<< 2) | RESUME_HOST
| (r
& RESUME_FLAG_NV
);
977 kvmppc_account_exit(vcpu
, SIGNAL_EXITS
);
984 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
985 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
991 vcpu
->arch
.shared
->pir
= vcpu
->vcpu_id
;
992 kvmppc_set_gpr(vcpu
, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
993 kvmppc_set_msr(vcpu
, 0);
995 #ifndef CONFIG_KVM_BOOKE_HV
996 vcpu
->arch
.shadow_msr
= MSR_USER
| MSR_DE
| MSR_IS
| MSR_DS
;
997 vcpu
->arch
.shadow_pid
= 1;
998 vcpu
->arch
.shared
->msr
= 0;
1001 /* Eye-catching numbers so we know if the guest takes an interrupt
1002 * before it's programmed its own IVPR/IVORs. */
1003 vcpu
->arch
.ivpr
= 0x55550000;
1004 for (i
= 0; i
< BOOKE_IRQPRIO_MAX
; i
++)
1005 vcpu
->arch
.ivor
[i
] = 0x7700 | i
* 4;
1007 kvmppc_init_timing_stats(vcpu
);
1009 r
= kvmppc_core_vcpu_setup(vcpu
);
1010 kvmppc_sanity_check(vcpu
);
1014 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
1018 regs
->pc
= vcpu
->arch
.pc
;
1019 regs
->cr
= kvmppc_get_cr(vcpu
);
1020 regs
->ctr
= vcpu
->arch
.ctr
;
1021 regs
->lr
= vcpu
->arch
.lr
;
1022 regs
->xer
= kvmppc_get_xer(vcpu
);
1023 regs
->msr
= vcpu
->arch
.shared
->msr
;
1024 regs
->srr0
= vcpu
->arch
.shared
->srr0
;
1025 regs
->srr1
= vcpu
->arch
.shared
->srr1
;
1026 regs
->pid
= vcpu
->arch
.pid
;
1027 regs
->sprg0
= vcpu
->arch
.shared
->sprg0
;
1028 regs
->sprg1
= vcpu
->arch
.shared
->sprg1
;
1029 regs
->sprg2
= vcpu
->arch
.shared
->sprg2
;
1030 regs
->sprg3
= vcpu
->arch
.shared
->sprg3
;
1031 regs
->sprg4
= vcpu
->arch
.shared
->sprg4
;
1032 regs
->sprg5
= vcpu
->arch
.shared
->sprg5
;
1033 regs
->sprg6
= vcpu
->arch
.shared
->sprg6
;
1034 regs
->sprg7
= vcpu
->arch
.shared
->sprg7
;
1036 for (i
= 0; i
< ARRAY_SIZE(regs
->gpr
); i
++)
1037 regs
->gpr
[i
] = kvmppc_get_gpr(vcpu
, i
);
1042 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
1046 vcpu
->arch
.pc
= regs
->pc
;
1047 kvmppc_set_cr(vcpu
, regs
->cr
);
1048 vcpu
->arch
.ctr
= regs
->ctr
;
1049 vcpu
->arch
.lr
= regs
->lr
;
1050 kvmppc_set_xer(vcpu
, regs
->xer
);
1051 kvmppc_set_msr(vcpu
, regs
->msr
);
1052 vcpu
->arch
.shared
->srr0
= regs
->srr0
;
1053 vcpu
->arch
.shared
->srr1
= regs
->srr1
;
1054 kvmppc_set_pid(vcpu
, regs
->pid
);
1055 vcpu
->arch
.shared
->sprg0
= regs
->sprg0
;
1056 vcpu
->arch
.shared
->sprg1
= regs
->sprg1
;
1057 vcpu
->arch
.shared
->sprg2
= regs
->sprg2
;
1058 vcpu
->arch
.shared
->sprg3
= regs
->sprg3
;
1059 vcpu
->arch
.shared
->sprg4
= regs
->sprg4
;
1060 vcpu
->arch
.shared
->sprg5
= regs
->sprg5
;
1061 vcpu
->arch
.shared
->sprg6
= regs
->sprg6
;
1062 vcpu
->arch
.shared
->sprg7
= regs
->sprg7
;
1064 for (i
= 0; i
< ARRAY_SIZE(regs
->gpr
); i
++)
1065 kvmppc_set_gpr(vcpu
, i
, regs
->gpr
[i
]);
1070 static void get_sregs_base(struct kvm_vcpu
*vcpu
,
1071 struct kvm_sregs
*sregs
)
1075 sregs
->u
.e
.features
|= KVM_SREGS_E_BASE
;
1077 sregs
->u
.e
.csrr0
= vcpu
->arch
.csrr0
;
1078 sregs
->u
.e
.csrr1
= vcpu
->arch
.csrr1
;
1079 sregs
->u
.e
.mcsr
= vcpu
->arch
.mcsr
;
1080 sregs
->u
.e
.esr
= get_guest_esr(vcpu
);
1081 sregs
->u
.e
.dear
= get_guest_dear(vcpu
);
1082 sregs
->u
.e
.tsr
= vcpu
->arch
.tsr
;
1083 sregs
->u
.e
.tcr
= vcpu
->arch
.tcr
;
1084 sregs
->u
.e
.dec
= kvmppc_get_dec(vcpu
, tb
);
1086 sregs
->u
.e
.vrsave
= vcpu
->arch
.vrsave
;
1089 static int set_sregs_base(struct kvm_vcpu
*vcpu
,
1090 struct kvm_sregs
*sregs
)
1092 if (!(sregs
->u
.e
.features
& KVM_SREGS_E_BASE
))
1095 vcpu
->arch
.csrr0
= sregs
->u
.e
.csrr0
;
1096 vcpu
->arch
.csrr1
= sregs
->u
.e
.csrr1
;
1097 vcpu
->arch
.mcsr
= sregs
->u
.e
.mcsr
;
1098 set_guest_esr(vcpu
, sregs
->u
.e
.esr
);
1099 set_guest_dear(vcpu
, sregs
->u
.e
.dear
);
1100 vcpu
->arch
.vrsave
= sregs
->u
.e
.vrsave
;
1101 kvmppc_set_tcr(vcpu
, sregs
->u
.e
.tcr
);
1103 if (sregs
->u
.e
.update_special
& KVM_SREGS_E_UPDATE_DEC
) {
1104 vcpu
->arch
.dec
= sregs
->u
.e
.dec
;
1105 kvmppc_emulate_dec(vcpu
);
1108 if (sregs
->u
.e
.update_special
& KVM_SREGS_E_UPDATE_TSR
) {
1109 vcpu
->arch
.tsr
= sregs
->u
.e
.tsr
;
1110 update_timer_ints(vcpu
);
1116 static void get_sregs_arch206(struct kvm_vcpu
*vcpu
,
1117 struct kvm_sregs
*sregs
)
1119 sregs
->u
.e
.features
|= KVM_SREGS_E_ARCH206
;
1121 sregs
->u
.e
.pir
= vcpu
->vcpu_id
;
1122 sregs
->u
.e
.mcsrr0
= vcpu
->arch
.mcsrr0
;
1123 sregs
->u
.e
.mcsrr1
= vcpu
->arch
.mcsrr1
;
1124 sregs
->u
.e
.decar
= vcpu
->arch
.decar
;
1125 sregs
->u
.e
.ivpr
= vcpu
->arch
.ivpr
;
1128 static int set_sregs_arch206(struct kvm_vcpu
*vcpu
,
1129 struct kvm_sregs
*sregs
)
1131 if (!(sregs
->u
.e
.features
& KVM_SREGS_E_ARCH206
))
1134 if (sregs
->u
.e
.pir
!= vcpu
->vcpu_id
)
1137 vcpu
->arch
.mcsrr0
= sregs
->u
.e
.mcsrr0
;
1138 vcpu
->arch
.mcsrr1
= sregs
->u
.e
.mcsrr1
;
1139 vcpu
->arch
.decar
= sregs
->u
.e
.decar
;
1140 vcpu
->arch
.ivpr
= sregs
->u
.e
.ivpr
;
1145 void kvmppc_get_sregs_ivor(struct kvm_vcpu
*vcpu
, struct kvm_sregs
*sregs
)
1147 sregs
->u
.e
.features
|= KVM_SREGS_E_IVOR
;
1149 sregs
->u
.e
.ivor_low
[0] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_CRITICAL
];
1150 sregs
->u
.e
.ivor_low
[1] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_MACHINE_CHECK
];
1151 sregs
->u
.e
.ivor_low
[2] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DATA_STORAGE
];
1152 sregs
->u
.e
.ivor_low
[3] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_INST_STORAGE
];
1153 sregs
->u
.e
.ivor_low
[4] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_EXTERNAL
];
1154 sregs
->u
.e
.ivor_low
[5] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ALIGNMENT
];
1155 sregs
->u
.e
.ivor_low
[6] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_PROGRAM
];
1156 sregs
->u
.e
.ivor_low
[7] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FP_UNAVAIL
];
1157 sregs
->u
.e
.ivor_low
[8] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_SYSCALL
];
1158 sregs
->u
.e
.ivor_low
[9] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_AP_UNAVAIL
];
1159 sregs
->u
.e
.ivor_low
[10] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DECREMENTER
];
1160 sregs
->u
.e
.ivor_low
[11] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FIT
];
1161 sregs
->u
.e
.ivor_low
[12] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_WATCHDOG
];
1162 sregs
->u
.e
.ivor_low
[13] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DTLB_MISS
];
1163 sregs
->u
.e
.ivor_low
[14] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ITLB_MISS
];
1164 sregs
->u
.e
.ivor_low
[15] = vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DEBUG
];
1167 int kvmppc_set_sregs_ivor(struct kvm_vcpu
*vcpu
, struct kvm_sregs
*sregs
)
1169 if (!(sregs
->u
.e
.features
& KVM_SREGS_E_IVOR
))
1172 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_CRITICAL
] = sregs
->u
.e
.ivor_low
[0];
1173 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_MACHINE_CHECK
] = sregs
->u
.e
.ivor_low
[1];
1174 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DATA_STORAGE
] = sregs
->u
.e
.ivor_low
[2];
1175 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_INST_STORAGE
] = sregs
->u
.e
.ivor_low
[3];
1176 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_EXTERNAL
] = sregs
->u
.e
.ivor_low
[4];
1177 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ALIGNMENT
] = sregs
->u
.e
.ivor_low
[5];
1178 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_PROGRAM
] = sregs
->u
.e
.ivor_low
[6];
1179 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FP_UNAVAIL
] = sregs
->u
.e
.ivor_low
[7];
1180 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_SYSCALL
] = sregs
->u
.e
.ivor_low
[8];
1181 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_AP_UNAVAIL
] = sregs
->u
.e
.ivor_low
[9];
1182 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DECREMENTER
] = sregs
->u
.e
.ivor_low
[10];
1183 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FIT
] = sregs
->u
.e
.ivor_low
[11];
1184 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_WATCHDOG
] = sregs
->u
.e
.ivor_low
[12];
1185 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DTLB_MISS
] = sregs
->u
.e
.ivor_low
[13];
1186 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ITLB_MISS
] = sregs
->u
.e
.ivor_low
[14];
1187 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DEBUG
] = sregs
->u
.e
.ivor_low
[15];
1192 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
1193 struct kvm_sregs
*sregs
)
1195 sregs
->pvr
= vcpu
->arch
.pvr
;
1197 get_sregs_base(vcpu
, sregs
);
1198 get_sregs_arch206(vcpu
, sregs
);
1199 kvmppc_core_get_sregs(vcpu
, sregs
);
1203 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
1204 struct kvm_sregs
*sregs
)
1208 if (vcpu
->arch
.pvr
!= sregs
->pvr
)
1211 ret
= set_sregs_base(vcpu
, sregs
);
1215 ret
= set_sregs_arch206(vcpu
, sregs
);
1219 return kvmppc_core_set_sregs(vcpu
, sregs
);
1222 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu
*vcpu
, struct kvm_one_reg
*reg
)
1227 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu
*vcpu
, struct kvm_one_reg
*reg
)
1232 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1237 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1242 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
1243 struct kvm_translation
*tr
)
1247 r
= kvmppc_core_vcpu_translate(vcpu
, tr
);
1251 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
, struct kvm_dirty_log
*log
)
1256 int kvmppc_core_prepare_memory_region(struct kvm
*kvm
,
1257 struct kvm_userspace_memory_region
*mem
)
1262 void kvmppc_core_commit_memory_region(struct kvm
*kvm
,
1263 struct kvm_userspace_memory_region
*mem
)
1267 void kvmppc_set_tcr(struct kvm_vcpu
*vcpu
, u32 new_tcr
)
1269 vcpu
->arch
.tcr
= new_tcr
;
1270 update_timer_ints(vcpu
);
1273 void kvmppc_set_tsr_bits(struct kvm_vcpu
*vcpu
, u32 tsr_bits
)
1275 set_bits(tsr_bits
, &vcpu
->arch
.tsr
);
1277 kvm_make_request(KVM_REQ_PENDING_TIMER
, vcpu
);
1278 kvm_vcpu_kick(vcpu
);
1281 void kvmppc_clr_tsr_bits(struct kvm_vcpu
*vcpu
, u32 tsr_bits
)
1283 clear_bits(tsr_bits
, &vcpu
->arch
.tsr
);
1284 update_timer_ints(vcpu
);
1287 void kvmppc_decrementer_func(unsigned long data
)
1289 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)data
;
1291 if (vcpu
->arch
.tcr
& TCR_ARE
) {
1292 vcpu
->arch
.dec
= vcpu
->arch
.decar
;
1293 kvmppc_emulate_dec(vcpu
);
1296 kvmppc_set_tsr_bits(vcpu
, TSR_DIS
);
1299 void kvmppc_booke_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1301 current
->thread
.kvm_vcpu
= vcpu
;
1304 void kvmppc_booke_vcpu_put(struct kvm_vcpu
*vcpu
)
1306 current
->thread
.kvm_vcpu
= NULL
;
1309 int __init
kvmppc_booke_init(void)
1311 #ifndef CONFIG_KVM_BOOKE_HV
1312 unsigned long ivor
[16];
1313 unsigned long max_ivor
= 0;
1316 /* We install our own exception handlers by hijacking IVPR. IVPR must
1317 * be 16-bit aligned, so we need a 64KB allocation. */
1318 kvmppc_booke_handlers
= __get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
1320 if (!kvmppc_booke_handlers
)
1323 /* XXX make sure our handlers are smaller than Linux's */
1325 /* Copy our interrupt handlers to match host IVORs. That way we don't
1326 * have to swap the IVORs on every guest/host transition. */
1327 ivor
[0] = mfspr(SPRN_IVOR0
);
1328 ivor
[1] = mfspr(SPRN_IVOR1
);
1329 ivor
[2] = mfspr(SPRN_IVOR2
);
1330 ivor
[3] = mfspr(SPRN_IVOR3
);
1331 ivor
[4] = mfspr(SPRN_IVOR4
);
1332 ivor
[5] = mfspr(SPRN_IVOR5
);
1333 ivor
[6] = mfspr(SPRN_IVOR6
);
1334 ivor
[7] = mfspr(SPRN_IVOR7
);
1335 ivor
[8] = mfspr(SPRN_IVOR8
);
1336 ivor
[9] = mfspr(SPRN_IVOR9
);
1337 ivor
[10] = mfspr(SPRN_IVOR10
);
1338 ivor
[11] = mfspr(SPRN_IVOR11
);
1339 ivor
[12] = mfspr(SPRN_IVOR12
);
1340 ivor
[13] = mfspr(SPRN_IVOR13
);
1341 ivor
[14] = mfspr(SPRN_IVOR14
);
1342 ivor
[15] = mfspr(SPRN_IVOR15
);
1344 for (i
= 0; i
< 16; i
++) {
1345 if (ivor
[i
] > max_ivor
)
1348 memcpy((void *)kvmppc_booke_handlers
+ ivor
[i
],
1349 kvmppc_handlers_start
+ i
* kvmppc_handler_len
,
1350 kvmppc_handler_len
);
1352 flush_icache_range(kvmppc_booke_handlers
,
1353 kvmppc_booke_handlers
+ max_ivor
+ kvmppc_handler_len
);
1354 #endif /* !BOOKE_HV */
1358 void __exit
kvmppc_booke_exit(void)
1360 free_pages(kvmppc_booke_handlers
, VCPU_SIZE_ORDER
);