2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
9 * This file is derived from arch/powerpc/kvm/44x.c,
10 * by Hollis Blanchard <hollisb@us.ibm.com>.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License, version 2, as
14 * published by the Free Software Foundation.
17 #include <linux/kvm_host.h>
18 #include <linux/err.h>
21 #include <asm/cputable.h>
22 #include <asm/cacheflush.h>
23 #include <asm/tlbflush.h>
24 #include <asm/uaccess.h>
26 #include <asm/kvm_ppc.h>
27 #include <asm/kvm_book3s.h>
28 #include <asm/mmu_context.h>
29 #include <linux/sched.h>
30 #include <linux/vmalloc.h>
32 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
34 /* #define EXIT_DEBUG */
35 /* #define EXIT_DEBUG_SIMPLE */
36 /* #define DEBUG_EXT */
38 static void kvmppc_giveup_ext(struct kvm_vcpu
*vcpu
, ulong msr
);
40 struct kvm_stats_debugfs_item debugfs_entries
[] = {
41 { "exits", VCPU_STAT(sum_exits
) },
42 { "mmio", VCPU_STAT(mmio_exits
) },
43 { "sig", VCPU_STAT(signal_exits
) },
44 { "sysc", VCPU_STAT(syscall_exits
) },
45 { "inst_emu", VCPU_STAT(emulated_inst_exits
) },
46 { "dec", VCPU_STAT(dec_exits
) },
47 { "ext_intr", VCPU_STAT(ext_intr_exits
) },
48 { "queue_intr", VCPU_STAT(queue_intr
) },
49 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
50 { "pf_storage", VCPU_STAT(pf_storage
) },
51 { "sp_storage", VCPU_STAT(sp_storage
) },
52 { "pf_instruc", VCPU_STAT(pf_instruc
) },
53 { "sp_instruc", VCPU_STAT(sp_instruc
) },
54 { "ld", VCPU_STAT(ld
) },
55 { "ld_slow", VCPU_STAT(ld_slow
) },
56 { "st", VCPU_STAT(st
) },
57 { "st_slow", VCPU_STAT(st_slow
) },
61 void kvmppc_core_load_host_debugstate(struct kvm_vcpu
*vcpu
)
65 void kvmppc_core_load_guest_debugstate(struct kvm_vcpu
*vcpu
)
69 void kvmppc_core_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
71 memcpy(get_paca()->kvm_slb
, to_book3s(vcpu
)->slb_shadow
, sizeof(get_paca()->kvm_slb
));
72 memcpy(&get_paca()->shadow_vcpu
, &to_book3s(vcpu
)->shadow_vcpu
,
73 sizeof(get_paca()->shadow_vcpu
));
74 get_paca()->kvm_slb_max
= to_book3s(vcpu
)->slb_shadow_max
;
77 void kvmppc_core_vcpu_put(struct kvm_vcpu
*vcpu
)
79 memcpy(to_book3s(vcpu
)->slb_shadow
, get_paca()->kvm_slb
, sizeof(get_paca()->kvm_slb
));
80 memcpy(&to_book3s(vcpu
)->shadow_vcpu
, &get_paca()->shadow_vcpu
,
81 sizeof(get_paca()->shadow_vcpu
));
82 to_book3s(vcpu
)->slb_shadow_max
= get_paca()->kvm_slb_max
;
84 kvmppc_giveup_ext(vcpu
, MSR_FP
);
85 kvmppc_giveup_ext(vcpu
, MSR_VEC
);
86 kvmppc_giveup_ext(vcpu
, MSR_VSX
);
89 #if defined(EXIT_DEBUG)
90 static u32
kvmppc_get_dec(struct kvm_vcpu
*vcpu
)
92 u64 jd
= mftb() - vcpu
->arch
.dec_jiffies
;
93 return vcpu
->arch
.dec
- jd
;
97 static void kvmppc_recalc_shadow_msr(struct kvm_vcpu
*vcpu
)
99 vcpu
->arch
.shadow_msr
= vcpu
->arch
.msr
;
100 /* Guest MSR values */
101 vcpu
->arch
.shadow_msr
&= MSR_FE0
| MSR_FE1
| MSR_SF
| MSR_SE
|
103 /* Process MSR values */
104 vcpu
->arch
.shadow_msr
|= MSR_ME
| MSR_RI
| MSR_IR
| MSR_DR
| MSR_PR
|
106 /* External providers the guest reserved */
107 vcpu
->arch
.shadow_msr
|= (vcpu
->arch
.msr
& vcpu
->arch
.guest_owned_ext
);
108 /* 64-bit Process MSR values */
109 #ifdef CONFIG_PPC_BOOK3S_64
110 vcpu
->arch
.shadow_msr
|= MSR_ISF
| MSR_HV
;
114 void kvmppc_set_msr(struct kvm_vcpu
*vcpu
, u64 msr
)
116 ulong old_msr
= vcpu
->arch
.msr
;
119 printk(KERN_INFO
"KVM: Set MSR to 0x%llx\n", msr
);
122 msr
&= to_book3s(vcpu
)->msr_mask
;
123 vcpu
->arch
.msr
= msr
;
124 kvmppc_recalc_shadow_msr(vcpu
);
126 if (msr
& (MSR_WE
|MSR_POW
)) {
127 if (!vcpu
->arch
.pending_exceptions
) {
128 kvm_vcpu_block(vcpu
);
129 vcpu
->stat
.halt_wakeup
++;
133 if (((vcpu
->arch
.msr
& (MSR_IR
|MSR_DR
)) != (old_msr
& (MSR_IR
|MSR_DR
))) ||
134 (vcpu
->arch
.msr
& MSR_PR
) != (old_msr
& MSR_PR
)) {
135 kvmppc_mmu_flush_segments(vcpu
);
136 kvmppc_mmu_map_segment(vcpu
, vcpu
->arch
.pc
);
140 void kvmppc_inject_interrupt(struct kvm_vcpu
*vcpu
, int vec
, u64 flags
)
142 vcpu
->arch
.srr0
= vcpu
->arch
.pc
;
143 vcpu
->arch
.srr1
= vcpu
->arch
.msr
| flags
;
144 vcpu
->arch
.pc
= to_book3s(vcpu
)->hior
+ vec
;
145 vcpu
->arch
.mmu
.reset_msr(vcpu
);
148 static int kvmppc_book3s_vec2irqprio(unsigned int vec
)
153 case 0x100: prio
= BOOK3S_IRQPRIO_SYSTEM_RESET
; break;
154 case 0x200: prio
= BOOK3S_IRQPRIO_MACHINE_CHECK
; break;
155 case 0x300: prio
= BOOK3S_IRQPRIO_DATA_STORAGE
; break;
156 case 0x380: prio
= BOOK3S_IRQPRIO_DATA_SEGMENT
; break;
157 case 0x400: prio
= BOOK3S_IRQPRIO_INST_STORAGE
; break;
158 case 0x480: prio
= BOOK3S_IRQPRIO_INST_SEGMENT
; break;
159 case 0x500: prio
= BOOK3S_IRQPRIO_EXTERNAL
; break;
160 case 0x600: prio
= BOOK3S_IRQPRIO_ALIGNMENT
; break;
161 case 0x700: prio
= BOOK3S_IRQPRIO_PROGRAM
; break;
162 case 0x800: prio
= BOOK3S_IRQPRIO_FP_UNAVAIL
; break;
163 case 0x900: prio
= BOOK3S_IRQPRIO_DECREMENTER
; break;
164 case 0xc00: prio
= BOOK3S_IRQPRIO_SYSCALL
; break;
165 case 0xd00: prio
= BOOK3S_IRQPRIO_DEBUG
; break;
166 case 0xf20: prio
= BOOK3S_IRQPRIO_ALTIVEC
; break;
167 case 0xf40: prio
= BOOK3S_IRQPRIO_VSX
; break;
168 default: prio
= BOOK3S_IRQPRIO_MAX
; break;
174 static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu
*vcpu
,
177 clear_bit(kvmppc_book3s_vec2irqprio(vec
),
178 &vcpu
->arch
.pending_exceptions
);
181 void kvmppc_book3s_queue_irqprio(struct kvm_vcpu
*vcpu
, unsigned int vec
)
183 vcpu
->stat
.queue_intr
++;
185 set_bit(kvmppc_book3s_vec2irqprio(vec
),
186 &vcpu
->arch
.pending_exceptions
);
188 printk(KERN_INFO
"Queueing interrupt %x\n", vec
);
193 void kvmppc_core_queue_program(struct kvm_vcpu
*vcpu
, ulong flags
)
195 to_book3s(vcpu
)->prog_flags
= flags
;
196 kvmppc_book3s_queue_irqprio(vcpu
, BOOK3S_INTERRUPT_PROGRAM
);
199 void kvmppc_core_queue_dec(struct kvm_vcpu
*vcpu
)
201 kvmppc_book3s_queue_irqprio(vcpu
, BOOK3S_INTERRUPT_DECREMENTER
);
204 int kvmppc_core_pending_dec(struct kvm_vcpu
*vcpu
)
206 return test_bit(BOOK3S_INTERRUPT_DECREMENTER
>> 7, &vcpu
->arch
.pending_exceptions
);
209 void kvmppc_core_dequeue_dec(struct kvm_vcpu
*vcpu
)
211 kvmppc_book3s_dequeue_irqprio(vcpu
, BOOK3S_INTERRUPT_DECREMENTER
);
214 void kvmppc_core_queue_external(struct kvm_vcpu
*vcpu
,
215 struct kvm_interrupt
*irq
)
217 kvmppc_book3s_queue_irqprio(vcpu
, BOOK3S_INTERRUPT_EXTERNAL
);
220 int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu
*vcpu
, unsigned int priority
)
227 case BOOK3S_IRQPRIO_DECREMENTER
:
228 deliver
= vcpu
->arch
.msr
& MSR_EE
;
229 vec
= BOOK3S_INTERRUPT_DECREMENTER
;
231 case BOOK3S_IRQPRIO_EXTERNAL
:
232 deliver
= vcpu
->arch
.msr
& MSR_EE
;
233 vec
= BOOK3S_INTERRUPT_EXTERNAL
;
235 case BOOK3S_IRQPRIO_SYSTEM_RESET
:
236 vec
= BOOK3S_INTERRUPT_SYSTEM_RESET
;
238 case BOOK3S_IRQPRIO_MACHINE_CHECK
:
239 vec
= BOOK3S_INTERRUPT_MACHINE_CHECK
;
241 case BOOK3S_IRQPRIO_DATA_STORAGE
:
242 vec
= BOOK3S_INTERRUPT_DATA_STORAGE
;
244 case BOOK3S_IRQPRIO_INST_STORAGE
:
245 vec
= BOOK3S_INTERRUPT_INST_STORAGE
;
247 case BOOK3S_IRQPRIO_DATA_SEGMENT
:
248 vec
= BOOK3S_INTERRUPT_DATA_SEGMENT
;
250 case BOOK3S_IRQPRIO_INST_SEGMENT
:
251 vec
= BOOK3S_INTERRUPT_INST_SEGMENT
;
253 case BOOK3S_IRQPRIO_ALIGNMENT
:
254 vec
= BOOK3S_INTERRUPT_ALIGNMENT
;
256 case BOOK3S_IRQPRIO_PROGRAM
:
257 vec
= BOOK3S_INTERRUPT_PROGRAM
;
258 flags
= to_book3s(vcpu
)->prog_flags
;
260 case BOOK3S_IRQPRIO_VSX
:
261 vec
= BOOK3S_INTERRUPT_VSX
;
263 case BOOK3S_IRQPRIO_ALTIVEC
:
264 vec
= BOOK3S_INTERRUPT_ALTIVEC
;
266 case BOOK3S_IRQPRIO_FP_UNAVAIL
:
267 vec
= BOOK3S_INTERRUPT_FP_UNAVAIL
;
269 case BOOK3S_IRQPRIO_SYSCALL
:
270 vec
= BOOK3S_INTERRUPT_SYSCALL
;
272 case BOOK3S_IRQPRIO_DEBUG
:
273 vec
= BOOK3S_INTERRUPT_TRACE
;
275 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR
:
276 vec
= BOOK3S_INTERRUPT_PERFMON
;
280 printk(KERN_ERR
"KVM: Unknown interrupt: 0x%x\n", priority
);
285 printk(KERN_INFO
"Deliver interrupt 0x%x? %x\n", vec
, deliver
);
289 kvmppc_inject_interrupt(vcpu
, vec
, flags
);
294 void kvmppc_core_deliver_interrupts(struct kvm_vcpu
*vcpu
)
296 unsigned long *pending
= &vcpu
->arch
.pending_exceptions
;
297 unsigned int priority
;
300 if (vcpu
->arch
.pending_exceptions
)
301 printk(KERN_EMERG
"KVM: Check pending: %lx\n", vcpu
->arch
.pending_exceptions
);
303 priority
= __ffs(*pending
);
304 while (priority
<= (sizeof(unsigned int) * 8)) {
305 if (kvmppc_book3s_irqprio_deliver(vcpu
, priority
) &&
306 (priority
!= BOOK3S_IRQPRIO_DECREMENTER
)) {
307 /* DEC interrupts get cleared by mtdec */
308 clear_bit(priority
, &vcpu
->arch
.pending_exceptions
);
312 priority
= find_next_bit(pending
,
313 BITS_PER_BYTE
* sizeof(*pending
),
318 void kvmppc_set_pvr(struct kvm_vcpu
*vcpu
, u32 pvr
)
320 vcpu
->arch
.hflags
&= ~BOOK3S_HFLAG_SLB
;
321 vcpu
->arch
.pvr
= pvr
;
322 if ((pvr
>= 0x330000) && (pvr
< 0x70330000)) {
323 kvmppc_mmu_book3s_64_init(vcpu
);
324 to_book3s(vcpu
)->hior
= 0xfff00000;
325 to_book3s(vcpu
)->msr_mask
= 0xffffffffffffffffULL
;
327 kvmppc_mmu_book3s_32_init(vcpu
);
328 to_book3s(vcpu
)->hior
= 0;
329 to_book3s(vcpu
)->msr_mask
= 0xffffffffULL
;
332 /* If we are in hypervisor level on 970, we can tell the CPU to
333 * treat DCBZ as 32 bytes store */
334 vcpu
->arch
.hflags
&= ~BOOK3S_HFLAG_DCBZ32
;
335 if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) && (mfmsr() & MSR_HV
) &&
336 !strcmp(cur_cpu_spec
->platform
, "ppc970"))
337 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_DCBZ32
;
341 /* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
342 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
343 * emulate 32 bytes dcbz length.
345 * The Book3s_64 inventors also realized this case and implemented a special bit
346 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
348 * My approach here is to patch the dcbz instruction on executing pages.
350 static void kvmppc_patch_dcbz(struct kvm_vcpu
*vcpu
, struct kvmppc_pte
*pte
)
352 bool touched
= false;
357 hpage
= gfn_to_hva(vcpu
->kvm
, pte
->raddr
>> PAGE_SHIFT
);
358 if (kvm_is_error_hva(hpage
))
361 hpage
|= pte
->raddr
& ~PAGE_MASK
;
364 page
= vmalloc(HW_PAGE_SIZE
);
366 if (copy_from_user(page
, (void __user
*)hpage
, HW_PAGE_SIZE
))
369 for (i
=0; i
< HW_PAGE_SIZE
/ 4; i
++)
370 if ((page
[i
] & 0xff0007ff) == INS_DCBZ
) {
371 page
[i
] &= 0xfffffff7; // reserved instruction, so we trap
376 copy_to_user((void __user
*)hpage
, page
, HW_PAGE_SIZE
);
382 static int kvmppc_xlate(struct kvm_vcpu
*vcpu
, ulong eaddr
, bool data
,
383 struct kvmppc_pte
*pte
)
385 int relocated
= (vcpu
->arch
.msr
& (data
? MSR_DR
: MSR_IR
));
389 r
= vcpu
->arch
.mmu
.xlate(vcpu
, eaddr
, pte
, data
);
392 pte
->raddr
= eaddr
& 0xffffffff;
393 pte
->vpage
= eaddr
>> 12;
394 switch (vcpu
->arch
.msr
& (MSR_DR
|MSR_IR
)) {
396 pte
->vpage
|= VSID_REAL
;
398 pte
->vpage
|= VSID_REAL_DR
;
400 pte
->vpage
|= VSID_REAL_IR
;
402 pte
->may_read
= true;
403 pte
->may_write
= true;
404 pte
->may_execute
= true;
411 static hva_t
kvmppc_bad_hva(void)
416 static hva_t
kvmppc_pte_to_hva(struct kvm_vcpu
*vcpu
, struct kvmppc_pte
*pte
,
421 if (read
&& !pte
->may_read
)
424 if (!read
&& !pte
->may_write
)
427 hpage
= gfn_to_hva(vcpu
->kvm
, pte
->raddr
>> PAGE_SHIFT
);
428 if (kvm_is_error_hva(hpage
))
431 return hpage
| (pte
->raddr
& ~PAGE_MASK
);
433 return kvmppc_bad_hva();
436 int kvmppc_st(struct kvm_vcpu
*vcpu
, ulong eaddr
, int size
, void *ptr
)
438 struct kvmppc_pte pte
;
443 if (kvmppc_xlate(vcpu
, eaddr
, false, &pte
))
446 hva
= kvmppc_pte_to_hva(vcpu
, &pte
, false);
447 if (kvm_is_error_hva(hva
))
450 if (copy_to_user((void __user
*)hva
, ptr
, size
)) {
451 printk(KERN_INFO
"kvmppc_st at 0x%lx failed\n", hva
);
461 int kvmppc_ld(struct kvm_vcpu
*vcpu
, ulong eaddr
, int size
, void *ptr
,
464 struct kvmppc_pte pte
;
469 if (kvmppc_xlate(vcpu
, eaddr
, data
, &pte
))
472 hva
= kvmppc_pte_to_hva(vcpu
, &pte
, true);
473 if (kvm_is_error_hva(hva
))
476 if (copy_from_user(ptr
, (void __user
*)hva
, size
)) {
477 printk(KERN_INFO
"kvmppc_ld at 0x%lx failed\n", hva
);
487 static int kvmppc_visible_gfn(struct kvm_vcpu
*vcpu
, gfn_t gfn
)
489 return kvm_is_visible_gfn(vcpu
->kvm
, gfn
);
492 int kvmppc_handle_pagefault(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
493 ulong eaddr
, int vec
)
495 bool data
= (vec
== BOOK3S_INTERRUPT_DATA_STORAGE
);
496 int r
= RESUME_GUEST
;
499 struct kvmppc_pte pte
;
500 bool is_mmio
= false;
502 if ( vec
== BOOK3S_INTERRUPT_DATA_STORAGE
) {
503 relocated
= (vcpu
->arch
.msr
& MSR_DR
);
505 relocated
= (vcpu
->arch
.msr
& MSR_IR
);
508 /* Resolve real address if translation turned on */
510 page_found
= vcpu
->arch
.mmu
.xlate(vcpu
, eaddr
, &pte
, data
);
512 pte
.may_execute
= true;
514 pte
.may_write
= true;
515 pte
.raddr
= eaddr
& 0xffffffff;
517 pte
.vpage
= eaddr
>> 12;
518 switch (vcpu
->arch
.msr
& (MSR_DR
|MSR_IR
)) {
520 pte
.vpage
|= VSID_REAL
;
522 pte
.vpage
|= VSID_REAL_DR
;
524 pte
.vpage
|= VSID_REAL_IR
;
528 if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) &&
529 (!(vcpu
->arch
.hflags
& BOOK3S_HFLAG_DCBZ32
))) {
531 * If we do the dcbz hack, we have to NX on every execution,
532 * so we can patch the executing code. This renders our guest
535 pte
.may_execute
= !data
;
538 if (page_found
== -ENOENT
) {
539 /* Page not found in guest PTE entries */
540 vcpu
->arch
.dear
= vcpu
->arch
.fault_dear
;
541 to_book3s(vcpu
)->dsisr
= vcpu
->arch
.fault_dsisr
;
542 vcpu
->arch
.msr
|= (vcpu
->arch
.shadow_srr1
& 0x00000000f8000000ULL
);
543 kvmppc_book3s_queue_irqprio(vcpu
, vec
);
544 } else if (page_found
== -EPERM
) {
545 /* Storage protection */
546 vcpu
->arch
.dear
= vcpu
->arch
.fault_dear
;
547 to_book3s(vcpu
)->dsisr
= vcpu
->arch
.fault_dsisr
& ~DSISR_NOHPTE
;
548 to_book3s(vcpu
)->dsisr
|= DSISR_PROTFAULT
;
549 vcpu
->arch
.msr
|= (vcpu
->arch
.shadow_srr1
& 0x00000000f8000000ULL
);
550 kvmppc_book3s_queue_irqprio(vcpu
, vec
);
551 } else if (page_found
== -EINVAL
) {
552 /* Page not found in guest SLB */
553 vcpu
->arch
.dear
= vcpu
->arch
.fault_dear
;
554 kvmppc_book3s_queue_irqprio(vcpu
, vec
+ 0x80);
555 } else if (!is_mmio
&&
556 kvmppc_visible_gfn(vcpu
, pte
.raddr
>> PAGE_SHIFT
)) {
557 /* The guest's PTE is not mapped yet. Map on the host */
558 kvmppc_mmu_map_page(vcpu
, &pte
);
560 vcpu
->stat
.sp_storage
++;
561 else if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) &&
562 (!(vcpu
->arch
.hflags
& BOOK3S_HFLAG_DCBZ32
)))
563 kvmppc_patch_dcbz(vcpu
, &pte
);
566 vcpu
->stat
.mmio_exits
++;
567 vcpu
->arch
.paddr_accessed
= pte
.raddr
;
568 r
= kvmppc_emulate_mmio(run
, vcpu
);
569 if ( r
== RESUME_HOST_NV
)
576 static inline int get_fpr_index(int i
)
584 /* Give up external provider (FPU, Altivec, VSX) */
585 static void kvmppc_giveup_ext(struct kvm_vcpu
*vcpu
, ulong msr
)
587 struct thread_struct
*t
= ¤t
->thread
;
588 u64
*vcpu_fpr
= vcpu
->arch
.fpr
;
589 u64
*vcpu_vsx
= vcpu
->arch
.vsr
;
590 u64
*thread_fpr
= (u64
*)t
->fpr
;
593 if (!(vcpu
->arch
.guest_owned_ext
& msr
))
597 printk(KERN_INFO
"Giving up ext 0x%lx\n", msr
);
603 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.fpr
); i
++)
604 vcpu_fpr
[i
] = thread_fpr
[get_fpr_index(i
)];
606 vcpu
->arch
.fpscr
= t
->fpscr
.val
;
609 #ifdef CONFIG_ALTIVEC
610 giveup_altivec(current
);
611 memcpy(vcpu
->arch
.vr
, t
->vr
, sizeof(vcpu
->arch
.vr
));
612 vcpu
->arch
.vscr
= t
->vscr
;
617 __giveup_vsx(current
);
618 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.vsr
); i
++)
619 vcpu_vsx
[i
] = thread_fpr
[get_fpr_index(i
) + 1];
626 vcpu
->arch
.guest_owned_ext
&= ~msr
;
627 current
->thread
.regs
->msr
&= ~msr
;
628 kvmppc_recalc_shadow_msr(vcpu
);
631 /* Handle external providers (FPU, Altivec, VSX) */
632 static int kvmppc_handle_ext(struct kvm_vcpu
*vcpu
, unsigned int exit_nr
,
635 struct thread_struct
*t
= ¤t
->thread
;
636 u64
*vcpu_fpr
= vcpu
->arch
.fpr
;
637 u64
*vcpu_vsx
= vcpu
->arch
.vsr
;
638 u64
*thread_fpr
= (u64
*)t
->fpr
;
641 if (!(vcpu
->arch
.msr
& msr
)) {
642 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
647 printk(KERN_INFO
"Loading up ext 0x%lx\n", msr
);
650 current
->thread
.regs
->msr
|= msr
;
654 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.fpr
); i
++)
655 thread_fpr
[get_fpr_index(i
)] = vcpu_fpr
[i
];
657 t
->fpscr
.val
= vcpu
->arch
.fpscr
;
659 kvmppc_load_up_fpu();
662 #ifdef CONFIG_ALTIVEC
663 memcpy(t
->vr
, vcpu
->arch
.vr
, sizeof(vcpu
->arch
.vr
));
664 t
->vscr
= vcpu
->arch
.vscr
;
666 kvmppc_load_up_altivec();
671 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.vsr
); i
++)
672 thread_fpr
[get_fpr_index(i
) + 1] = vcpu_vsx
[i
];
673 kvmppc_load_up_vsx();
680 vcpu
->arch
.guest_owned_ext
|= msr
;
682 kvmppc_recalc_shadow_msr(vcpu
);
687 int kvmppc_handle_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
688 unsigned int exit_nr
)
692 vcpu
->stat
.sum_exits
++;
694 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
695 run
->ready_for_interrupt_injection
= 1;
697 printk(KERN_EMERG
"exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
698 exit_nr
, vcpu
->arch
.pc
, vcpu
->arch
.fault_dear
,
699 kvmppc_get_dec(vcpu
), vcpu
->arch
.msr
);
700 #elif defined (EXIT_DEBUG_SIMPLE)
701 if ((exit_nr
!= 0x900) && (exit_nr
!= 0x500))
702 printk(KERN_EMERG
"exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
703 exit_nr
, vcpu
->arch
.pc
, vcpu
->arch
.fault_dear
,
708 case BOOK3S_INTERRUPT_INST_STORAGE
:
709 vcpu
->stat
.pf_instruc
++;
710 /* only care about PTEG not found errors, but leave NX alone */
711 if (vcpu
->arch
.shadow_srr1
& 0x40000000) {
712 r
= kvmppc_handle_pagefault(run
, vcpu
, vcpu
->arch
.pc
, exit_nr
);
713 vcpu
->stat
.sp_instruc
++;
714 } else if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) &&
715 (!(vcpu
->arch
.hflags
& BOOK3S_HFLAG_DCBZ32
))) {
717 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
718 * so we can't use the NX bit inside the guest. Let's cross our fingers,
719 * that no guest that needs the dcbz hack does NX.
721 kvmppc_mmu_pte_flush(vcpu
, vcpu
->arch
.pc
, ~0xFFFULL
);
723 vcpu
->arch
.msr
|= vcpu
->arch
.shadow_srr1
& 0x58000000;
724 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
725 kvmppc_mmu_pte_flush(vcpu
, vcpu
->arch
.pc
, ~0xFFFULL
);
729 case BOOK3S_INTERRUPT_DATA_STORAGE
:
730 vcpu
->stat
.pf_storage
++;
731 /* The only case we need to handle is missing shadow PTEs */
732 if (vcpu
->arch
.fault_dsisr
& DSISR_NOHPTE
) {
733 r
= kvmppc_handle_pagefault(run
, vcpu
, vcpu
->arch
.fault_dear
, exit_nr
);
735 vcpu
->arch
.dear
= vcpu
->arch
.fault_dear
;
736 to_book3s(vcpu
)->dsisr
= vcpu
->arch
.fault_dsisr
;
737 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
738 kvmppc_mmu_pte_flush(vcpu
, vcpu
->arch
.dear
, ~0xFFFULL
);
742 case BOOK3S_INTERRUPT_DATA_SEGMENT
:
743 if (kvmppc_mmu_map_segment(vcpu
, vcpu
->arch
.fault_dear
) < 0) {
744 vcpu
->arch
.dear
= vcpu
->arch
.fault_dear
;
745 kvmppc_book3s_queue_irqprio(vcpu
,
746 BOOK3S_INTERRUPT_DATA_SEGMENT
);
750 case BOOK3S_INTERRUPT_INST_SEGMENT
:
751 if (kvmppc_mmu_map_segment(vcpu
, vcpu
->arch
.pc
) < 0) {
752 kvmppc_book3s_queue_irqprio(vcpu
,
753 BOOK3S_INTERRUPT_INST_SEGMENT
);
757 /* We're good on these - the host merely wanted to get our attention */
758 case BOOK3S_INTERRUPT_DECREMENTER
:
759 vcpu
->stat
.dec_exits
++;
762 case BOOK3S_INTERRUPT_EXTERNAL
:
763 vcpu
->stat
.ext_intr_exits
++;
766 case BOOK3S_INTERRUPT_PROGRAM
:
768 enum emulation_result er
;
771 flags
= vcpu
->arch
.shadow_srr1
& 0x1f0000ull
;
773 if (vcpu
->arch
.msr
& MSR_PR
) {
775 printk(KERN_INFO
"Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", vcpu
->arch
.pc
, vcpu
->arch
.last_inst
);
777 if ((vcpu
->arch
.last_inst
& 0xff0007ff) !=
778 (INS_DCBZ
& 0xfffffff7)) {
779 kvmppc_core_queue_program(vcpu
, flags
);
785 vcpu
->stat
.emulated_inst_exits
++;
786 er
= kvmppc_emulate_instruction(run
, vcpu
);
792 printk(KERN_CRIT
"%s: emulation at %lx failed (%08x)\n",
793 __func__
, vcpu
->arch
.pc
, vcpu
->arch
.last_inst
);
794 kvmppc_core_queue_program(vcpu
, flags
);
802 case BOOK3S_INTERRUPT_SYSCALL
:
804 printk(KERN_INFO
"Syscall Nr %d\n", (int)kvmppc_get_gpr(vcpu
, 0));
806 vcpu
->stat
.syscall_exits
++;
807 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
810 case BOOK3S_INTERRUPT_FP_UNAVAIL
:
811 r
= kvmppc_handle_ext(vcpu
, exit_nr
, MSR_FP
);
813 case BOOK3S_INTERRUPT_ALTIVEC
:
814 r
= kvmppc_handle_ext(vcpu
, exit_nr
, MSR_VEC
);
816 case BOOK3S_INTERRUPT_VSX
:
817 r
= kvmppc_handle_ext(vcpu
, exit_nr
, MSR_VSX
);
819 case BOOK3S_INTERRUPT_MACHINE_CHECK
:
820 case BOOK3S_INTERRUPT_TRACE
:
821 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
825 /* Ugh - bork here! What did we get? */
826 printk(KERN_EMERG
"exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
827 exit_nr
, vcpu
->arch
.pc
, vcpu
->arch
.shadow_srr1
);
834 if (!(r
& RESUME_HOST
)) {
835 /* To avoid clobbering exit_reason, only check for signals if
836 * we aren't already exiting to userspace for some other
838 if (signal_pending(current
)) {
840 printk(KERN_EMERG
"KVM: Going back to host\n");
842 vcpu
->stat
.signal_exits
++;
843 run
->exit_reason
= KVM_EXIT_INTR
;
846 /* In case an interrupt came in that was triggered
847 * from userspace (like DEC), we need to check what
849 kvmppc_core_deliver_interrupts(vcpu
);
854 printk(KERN_EMERG
"KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu
, vcpu
->arch
.pc
, r
);
860 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
865 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
869 regs
->pc
= vcpu
->arch
.pc
;
870 regs
->cr
= kvmppc_get_cr(vcpu
);
871 regs
->ctr
= vcpu
->arch
.ctr
;
872 regs
->lr
= vcpu
->arch
.lr
;
873 regs
->xer
= kvmppc_get_xer(vcpu
);
874 regs
->msr
= vcpu
->arch
.msr
;
875 regs
->srr0
= vcpu
->arch
.srr0
;
876 regs
->srr1
= vcpu
->arch
.srr1
;
877 regs
->pid
= vcpu
->arch
.pid
;
878 regs
->sprg0
= vcpu
->arch
.sprg0
;
879 regs
->sprg1
= vcpu
->arch
.sprg1
;
880 regs
->sprg2
= vcpu
->arch
.sprg2
;
881 regs
->sprg3
= vcpu
->arch
.sprg3
;
882 regs
->sprg5
= vcpu
->arch
.sprg4
;
883 regs
->sprg6
= vcpu
->arch
.sprg5
;
884 regs
->sprg7
= vcpu
->arch
.sprg6
;
886 for (i
= 0; i
< ARRAY_SIZE(regs
->gpr
); i
++)
887 regs
->gpr
[i
] = kvmppc_get_gpr(vcpu
, i
);
892 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
896 vcpu
->arch
.pc
= regs
->pc
;
897 kvmppc_set_cr(vcpu
, regs
->cr
);
898 vcpu
->arch
.ctr
= regs
->ctr
;
899 vcpu
->arch
.lr
= regs
->lr
;
900 kvmppc_set_xer(vcpu
, regs
->xer
);
901 kvmppc_set_msr(vcpu
, regs
->msr
);
902 vcpu
->arch
.srr0
= regs
->srr0
;
903 vcpu
->arch
.srr1
= regs
->srr1
;
904 vcpu
->arch
.sprg0
= regs
->sprg0
;
905 vcpu
->arch
.sprg1
= regs
->sprg1
;
906 vcpu
->arch
.sprg2
= regs
->sprg2
;
907 vcpu
->arch
.sprg3
= regs
->sprg3
;
908 vcpu
->arch
.sprg5
= regs
->sprg4
;
909 vcpu
->arch
.sprg6
= regs
->sprg5
;
910 vcpu
->arch
.sprg7
= regs
->sprg6
;
912 for (i
= 0; i
< ARRAY_SIZE(regs
->gpr
); i
++)
913 kvmppc_set_gpr(vcpu
, i
, regs
->gpr
[i
]);
918 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
919 struct kvm_sregs
*sregs
)
921 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
924 sregs
->pvr
= vcpu
->arch
.pvr
;
926 sregs
->u
.s
.sdr1
= to_book3s(vcpu
)->sdr1
;
927 if (vcpu
->arch
.hflags
& BOOK3S_HFLAG_SLB
) {
928 for (i
= 0; i
< 64; i
++) {
929 sregs
->u
.s
.ppc64
.slb
[i
].slbe
= vcpu3s
->slb
[i
].orige
| i
;
930 sregs
->u
.s
.ppc64
.slb
[i
].slbv
= vcpu3s
->slb
[i
].origv
;
933 for (i
= 0; i
< 16; i
++) {
934 sregs
->u
.s
.ppc32
.sr
[i
] = vcpu3s
->sr
[i
].raw
;
935 sregs
->u
.s
.ppc32
.sr
[i
] = vcpu3s
->sr
[i
].raw
;
937 for (i
= 0; i
< 8; i
++) {
938 sregs
->u
.s
.ppc32
.ibat
[i
] = vcpu3s
->ibat
[i
].raw
;
939 sregs
->u
.s
.ppc32
.dbat
[i
] = vcpu3s
->dbat
[i
].raw
;
945 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
946 struct kvm_sregs
*sregs
)
948 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
951 kvmppc_set_pvr(vcpu
, sregs
->pvr
);
953 vcpu3s
->sdr1
= sregs
->u
.s
.sdr1
;
954 if (vcpu
->arch
.hflags
& BOOK3S_HFLAG_SLB
) {
955 for (i
= 0; i
< 64; i
++) {
956 vcpu
->arch
.mmu
.slbmte(vcpu
, sregs
->u
.s
.ppc64
.slb
[i
].slbv
,
957 sregs
->u
.s
.ppc64
.slb
[i
].slbe
);
960 for (i
= 0; i
< 16; i
++) {
961 vcpu
->arch
.mmu
.mtsrin(vcpu
, i
, sregs
->u
.s
.ppc32
.sr
[i
]);
963 for (i
= 0; i
< 8; i
++) {
964 kvmppc_set_bat(vcpu
, &(vcpu3s
->ibat
[i
]), false,
965 (u32
)sregs
->u
.s
.ppc32
.ibat
[i
]);
966 kvmppc_set_bat(vcpu
, &(vcpu3s
->ibat
[i
]), true,
967 (u32
)(sregs
->u
.s
.ppc32
.ibat
[i
] >> 32));
968 kvmppc_set_bat(vcpu
, &(vcpu3s
->dbat
[i
]), false,
969 (u32
)sregs
->u
.s
.ppc32
.dbat
[i
]);
970 kvmppc_set_bat(vcpu
, &(vcpu3s
->dbat
[i
]), true,
971 (u32
)(sregs
->u
.s
.ppc32
.dbat
[i
] >> 32));
975 /* Flush the MMU after messing with the segments */
976 kvmppc_mmu_pte_flush(vcpu
, 0, 0);
980 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
985 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
990 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
991 struct kvm_translation
*tr
)
997 * Get (and clear) the dirty memory log for a memory slot.
999 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1000 struct kvm_dirty_log
*log
)
1002 struct kvm_memory_slot
*memslot
;
1003 struct kvm_vcpu
*vcpu
;
1008 mutex_lock(&kvm
->slots_lock
);
1010 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1014 /* If nothing is dirty, don't bother messing with page tables. */
1016 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
1018 ga
= memslot
->base_gfn
<< PAGE_SHIFT
;
1019 ga_end
= ga
+ (memslot
->npages
<< PAGE_SHIFT
);
1021 kvm_for_each_vcpu(n
, vcpu
, kvm
)
1022 kvmppc_mmu_pte_pflush(vcpu
, ga
, ga_end
);
1024 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1025 memset(memslot
->dirty_bitmap
, 0, n
);
1030 mutex_unlock(&kvm
->slots_lock
);
1034 int kvmppc_core_check_processor_compat(void)
1039 struct kvm_vcpu
*kvmppc_core_vcpu_create(struct kvm
*kvm
, unsigned int id
)
1041 struct kvmppc_vcpu_book3s
*vcpu_book3s
;
1042 struct kvm_vcpu
*vcpu
;
1045 vcpu_book3s
= (struct kvmppc_vcpu_book3s
*)__get_free_pages( GFP_KERNEL
| __GFP_ZERO
,
1046 get_order(sizeof(struct kvmppc_vcpu_book3s
)));
1052 vcpu
= &vcpu_book3s
->vcpu
;
1053 err
= kvm_vcpu_init(vcpu
, kvm
, id
);
1057 vcpu
->arch
.host_retip
= kvm_return_point
;
1058 vcpu
->arch
.host_msr
= mfmsr();
1059 /* default to book3s_64 (970fx) */
1060 vcpu
->arch
.pvr
= 0x3C0301;
1061 kvmppc_set_pvr(vcpu
, vcpu
->arch
.pvr
);
1062 vcpu_book3s
->slb_nr
= 64;
1064 /* remember where some real-mode handlers are */
1065 vcpu
->arch
.trampoline_lowmem
= kvmppc_trampoline_lowmem
;
1066 vcpu
->arch
.trampoline_enter
= kvmppc_trampoline_enter
;
1067 vcpu
->arch
.highmem_handler
= (ulong
)kvmppc_handler_highmem
;
1068 vcpu
->arch
.rmcall
= *(ulong
*)kvmppc_rmcall
;
1070 vcpu
->arch
.shadow_msr
= MSR_USER64
;
1072 err
= __init_new_context();
1075 vcpu_book3s
->context_id
= err
;
1077 vcpu_book3s
->vsid_max
= ((vcpu_book3s
->context_id
+ 1) << USER_ESID_BITS
) - 1;
1078 vcpu_book3s
->vsid_first
= vcpu_book3s
->context_id
<< USER_ESID_BITS
;
1079 vcpu_book3s
->vsid_next
= vcpu_book3s
->vsid_first
;
1084 free_pages((long)vcpu_book3s
, get_order(sizeof(struct kvmppc_vcpu_book3s
)));
1086 return ERR_PTR(err
);
1089 void kvmppc_core_vcpu_free(struct kvm_vcpu
*vcpu
)
1091 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
1093 __destroy_context(vcpu_book3s
->context_id
);
1094 kvm_vcpu_uninit(vcpu
);
1095 free_pages((long)vcpu_book3s
, get_order(sizeof(struct kvmppc_vcpu_book3s
)));
1098 extern int __kvmppc_vcpu_entry(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
);
1099 int __kvmppc_vcpu_run(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
1102 struct thread_struct ext_bkp
;
1103 bool save_vec
= current
->thread
.used_vr
;
1104 bool save_vsx
= current
->thread
.used_vsr
;
1107 /* No need to go into the guest when all we do is going out */
1108 if (signal_pending(current
)) {
1109 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1113 /* Save FPU state in stack */
1114 if (current
->thread
.regs
->msr
& MSR_FP
)
1115 giveup_fpu(current
);
1116 memcpy(ext_bkp
.fpr
, current
->thread
.fpr
, sizeof(current
->thread
.fpr
));
1117 ext_bkp
.fpscr
= current
->thread
.fpscr
;
1118 ext_bkp
.fpexc_mode
= current
->thread
.fpexc_mode
;
1120 #ifdef CONFIG_ALTIVEC
1121 /* Save Altivec state in stack */
1123 if (current
->thread
.regs
->msr
& MSR_VEC
)
1124 giveup_altivec(current
);
1125 memcpy(ext_bkp
.vr
, current
->thread
.vr
, sizeof(ext_bkp
.vr
));
1126 ext_bkp
.vscr
= current
->thread
.vscr
;
1127 ext_bkp
.vrsave
= current
->thread
.vrsave
;
1129 ext_bkp
.used_vr
= current
->thread
.used_vr
;
1133 /* Save VSX state in stack */
1134 if (save_vsx
&& (current
->thread
.regs
->msr
& MSR_VSX
))
1135 __giveup_vsx(current
);
1136 ext_bkp
.used_vsr
= current
->thread
.used_vsr
;
1139 /* Remember the MSR with disabled extensions */
1140 ext_msr
= current
->thread
.regs
->msr
;
1142 /* XXX we get called with irq disabled - change that! */
1145 ret
= __kvmppc_vcpu_entry(kvm_run
, vcpu
);
1147 local_irq_disable();
1149 current
->thread
.regs
->msr
= ext_msr
;
1151 /* Make sure we save the guest FPU/Altivec/VSX state */
1152 kvmppc_giveup_ext(vcpu
, MSR_FP
);
1153 kvmppc_giveup_ext(vcpu
, MSR_VEC
);
1154 kvmppc_giveup_ext(vcpu
, MSR_VSX
);
1156 /* Restore FPU state from stack */
1157 memcpy(current
->thread
.fpr
, ext_bkp
.fpr
, sizeof(ext_bkp
.fpr
));
1158 current
->thread
.fpscr
= ext_bkp
.fpscr
;
1159 current
->thread
.fpexc_mode
= ext_bkp
.fpexc_mode
;
1161 #ifdef CONFIG_ALTIVEC
1162 /* Restore Altivec state from stack */
1163 if (save_vec
&& current
->thread
.used_vr
) {
1164 memcpy(current
->thread
.vr
, ext_bkp
.vr
, sizeof(ext_bkp
.vr
));
1165 current
->thread
.vscr
= ext_bkp
.vscr
;
1166 current
->thread
.vrsave
= ext_bkp
.vrsave
;
1168 current
->thread
.used_vr
= ext_bkp
.used_vr
;
1172 current
->thread
.used_vsr
= ext_bkp
.used_vsr
;
1178 static int kvmppc_book3s_init(void)
1180 return kvm_init(NULL
, sizeof(struct kvmppc_vcpu_book3s
), THIS_MODULE
);
1183 static void kvmppc_book3s_exit(void)
1188 module_init(kvmppc_book3s_init
);
1189 module_exit(kvmppc_book3s_exit
);