2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 * Paul Mackerras <paulus@samba.org>
10 * Functions relating to running KVM on Book 3S processors where
11 * we don't have access to hypervisor mode, and we run the guest
12 * in problem state (user mode).
14 * This file is derived from arch/powerpc/kvm/44x.c,
15 * by Hollis Blanchard <hollisb@us.ibm.com>.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License, version 2, as
19 * published by the Free Software Foundation.
22 #include <linux/kvm_host.h>
23 #include <linux/err.h>
24 #include <linux/slab.h>
27 #include <asm/cputable.h>
28 #include <asm/cacheflush.h>
29 #include <asm/tlbflush.h>
30 #include <asm/uaccess.h>
32 #include <asm/kvm_ppc.h>
33 #include <asm/kvm_book3s.h>
34 #include <asm/mmu_context.h>
35 #include <linux/gfp.h>
36 #include <linux/sched.h>
37 #include <linux/vmalloc.h>
38 #include <linux/highmem.h>
42 /* #define EXIT_DEBUG */
43 /* #define DEBUG_EXT */
45 static int kvmppc_handle_ext(struct kvm_vcpu
*vcpu
, unsigned int exit_nr
,
48 /* Some compatibility defines */
49 #ifdef CONFIG_PPC_BOOK3S_32
50 #define MSR_USER32 MSR_USER
51 #define MSR_USER64 MSR_USER
52 #define HW_PAGE_SIZE PAGE_SIZE
55 void kvmppc_core_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
57 #ifdef CONFIG_PPC_BOOK3S_64
58 memcpy(to_svcpu(vcpu
)->slb
, to_book3s(vcpu
)->slb_shadow
, sizeof(to_svcpu(vcpu
)->slb
));
59 memcpy(&get_paca()->shadow_vcpu
, to_book3s(vcpu
)->shadow_vcpu
,
60 sizeof(get_paca()->shadow_vcpu
));
61 to_svcpu(vcpu
)->slb_max
= to_book3s(vcpu
)->slb_shadow_max
;
64 #ifdef CONFIG_PPC_BOOK3S_32
65 current
->thread
.kvm_shadow_vcpu
= to_book3s(vcpu
)->shadow_vcpu
;
69 void kvmppc_core_vcpu_put(struct kvm_vcpu
*vcpu
)
71 #ifdef CONFIG_PPC_BOOK3S_64
72 memcpy(to_book3s(vcpu
)->slb_shadow
, to_svcpu(vcpu
)->slb
, sizeof(to_svcpu(vcpu
)->slb
));
73 memcpy(to_book3s(vcpu
)->shadow_vcpu
, &get_paca()->shadow_vcpu
,
74 sizeof(get_paca()->shadow_vcpu
));
75 to_book3s(vcpu
)->slb_shadow_max
= to_svcpu(vcpu
)->slb_max
;
78 kvmppc_giveup_ext(vcpu
, MSR_FP
);
79 kvmppc_giveup_ext(vcpu
, MSR_VEC
);
80 kvmppc_giveup_ext(vcpu
, MSR_VSX
);
83 static void kvmppc_recalc_shadow_msr(struct kvm_vcpu
*vcpu
)
85 ulong smsr
= vcpu
->arch
.shared
->msr
;
87 /* Guest MSR values */
88 smsr
&= MSR_FE0
| MSR_FE1
| MSR_SF
| MSR_SE
| MSR_BE
| MSR_DE
;
89 /* Process MSR values */
90 smsr
|= MSR_ME
| MSR_RI
| MSR_IR
| MSR_DR
| MSR_PR
| MSR_EE
;
91 /* External providers the guest reserved */
92 smsr
|= (vcpu
->arch
.shared
->msr
& vcpu
->arch
.guest_owned_ext
);
93 /* 64-bit Process MSR values */
94 #ifdef CONFIG_PPC_BOOK3S_64
95 smsr
|= MSR_ISF
| MSR_HV
;
97 vcpu
->arch
.shadow_msr
= smsr
;
100 void kvmppc_set_msr(struct kvm_vcpu
*vcpu
, u64 msr
)
102 ulong old_msr
= vcpu
->arch
.shared
->msr
;
105 printk(KERN_INFO
"KVM: Set MSR to 0x%llx\n", msr
);
108 msr
&= to_book3s(vcpu
)->msr_mask
;
109 vcpu
->arch
.shared
->msr
= msr
;
110 kvmppc_recalc_shadow_msr(vcpu
);
113 if (!vcpu
->arch
.pending_exceptions
) {
114 kvm_vcpu_block(vcpu
);
115 vcpu
->stat
.halt_wakeup
++;
117 /* Unset POW bit after we woke up */
119 vcpu
->arch
.shared
->msr
= msr
;
123 if ((vcpu
->arch
.shared
->msr
& (MSR_PR
|MSR_IR
|MSR_DR
)) !=
124 (old_msr
& (MSR_PR
|MSR_IR
|MSR_DR
))) {
125 kvmppc_mmu_flush_segments(vcpu
);
126 kvmppc_mmu_map_segment(vcpu
, kvmppc_get_pc(vcpu
));
128 /* Preload magic page segment when in kernel mode */
129 if (!(msr
& MSR_PR
) && vcpu
->arch
.magic_page_pa
) {
130 struct kvm_vcpu_arch
*a
= &vcpu
->arch
;
133 kvmppc_mmu_map_segment(vcpu
, a
->magic_page_ea
);
135 kvmppc_mmu_map_segment(vcpu
, a
->magic_page_pa
);
139 /* Preload FPU if it's enabled */
140 if (vcpu
->arch
.shared
->msr
& MSR_FP
)
141 kvmppc_handle_ext(vcpu
, BOOK3S_INTERRUPT_FP_UNAVAIL
, MSR_FP
);
144 void kvmppc_set_pvr(struct kvm_vcpu
*vcpu
, u32 pvr
)
148 vcpu
->arch
.hflags
&= ~BOOK3S_HFLAG_SLB
;
149 vcpu
->arch
.pvr
= pvr
;
150 #ifdef CONFIG_PPC_BOOK3S_64
151 if ((pvr
>= 0x330000) && (pvr
< 0x70330000)) {
152 kvmppc_mmu_book3s_64_init(vcpu
);
153 to_book3s(vcpu
)->hior
= 0xfff00000;
154 to_book3s(vcpu
)->msr_mask
= 0xffffffffffffffffULL
;
158 kvmppc_mmu_book3s_32_init(vcpu
);
159 to_book3s(vcpu
)->hior
= 0;
160 to_book3s(vcpu
)->msr_mask
= 0xffffffffULL
;
163 /* If we are in hypervisor level on 970, we can tell the CPU to
164 * treat DCBZ as 32 bytes store */
165 vcpu
->arch
.hflags
&= ~BOOK3S_HFLAG_DCBZ32
;
166 if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) && (mfmsr() & MSR_HV
) &&
167 !strcmp(cur_cpu_spec
->platform
, "ppc970"))
168 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_DCBZ32
;
170 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
171 really needs them in a VM on Cell and force disable them. */
172 if (!strcmp(cur_cpu_spec
->platform
, "ppc-cell-be"))
173 to_book3s(vcpu
)->msr_mask
&= ~(MSR_FE0
| MSR_FE1
);
175 #ifdef CONFIG_PPC_BOOK3S_32
176 /* 32 bit Book3S always has 32 byte dcbz */
177 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_DCBZ32
;
180 /* On some CPUs we can execute paired single operations natively */
181 asm ( "mfpvr %0" : "=r"(host_pvr
));
183 case 0x00080200: /* lonestar 2.0 */
184 case 0x00088202: /* lonestar 2.2 */
185 case 0x70000100: /* gekko 1.0 */
186 case 0x00080100: /* gekko 2.0 */
187 case 0x00083203: /* gekko 2.3a */
188 case 0x00083213: /* gekko 2.3b */
189 case 0x00083204: /* gekko 2.4 */
190 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
191 case 0x00087200: /* broadway */
192 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_NATIVE_PS
;
193 /* Enable HID2.PSE - in case we need it later */
194 mtspr(SPRN_HID2_GEKKO
, mfspr(SPRN_HID2_GEKKO
) | (1 << 29));
198 /* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
199 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
200 * emulate 32 bytes dcbz length.
202 * The Book3s_64 inventors also realized this case and implemented a special bit
203 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
205 * My approach here is to patch the dcbz instruction on executing pages.
207 static void kvmppc_patch_dcbz(struct kvm_vcpu
*vcpu
, struct kvmppc_pte
*pte
)
214 hpage
= gfn_to_page(vcpu
->kvm
, pte
->raddr
>> PAGE_SHIFT
);
215 if (is_error_page(hpage
)) {
216 kvm_release_page_clean(hpage
);
220 hpage_offset
= pte
->raddr
& ~PAGE_MASK
;
221 hpage_offset
&= ~0xFFFULL
;
225 page
= kmap_atomic(hpage
, KM_USER0
);
227 /* patch dcbz into reserved instruction, so we trap */
228 for (i
=hpage_offset
; i
< hpage_offset
+ (HW_PAGE_SIZE
/ 4); i
++)
229 if ((page
[i
] & 0xff0007ff) == INS_DCBZ
)
230 page
[i
] &= 0xfffffff7;
232 kunmap_atomic(page
, KM_USER0
);
236 static int kvmppc_visible_gfn(struct kvm_vcpu
*vcpu
, gfn_t gfn
)
238 ulong mp_pa
= vcpu
->arch
.magic_page_pa
;
240 if (unlikely(mp_pa
) &&
241 unlikely((mp_pa
& KVM_PAM
) >> PAGE_SHIFT
== gfn
)) {
245 return kvm_is_visible_gfn(vcpu
->kvm
, gfn
);
248 int kvmppc_handle_pagefault(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
249 ulong eaddr
, int vec
)
251 bool data
= (vec
== BOOK3S_INTERRUPT_DATA_STORAGE
);
252 int r
= RESUME_GUEST
;
255 struct kvmppc_pte pte
;
256 bool is_mmio
= false;
257 bool dr
= (vcpu
->arch
.shared
->msr
& MSR_DR
) ? true : false;
258 bool ir
= (vcpu
->arch
.shared
->msr
& MSR_IR
) ? true : false;
261 relocated
= data
? dr
: ir
;
263 /* Resolve real address if translation turned on */
265 page_found
= vcpu
->arch
.mmu
.xlate(vcpu
, eaddr
, &pte
, data
);
267 pte
.may_execute
= true;
269 pte
.may_write
= true;
270 pte
.raddr
= eaddr
& KVM_PAM
;
272 pte
.vpage
= eaddr
>> 12;
275 switch (vcpu
->arch
.shared
->msr
& (MSR_DR
|MSR_IR
)) {
277 pte
.vpage
|= ((u64
)VSID_REAL
<< (SID_SHIFT
- 12));
281 vcpu
->arch
.mmu
.esid_to_vsid(vcpu
, eaddr
>> SID_SHIFT
, &vsid
);
283 if ((vcpu
->arch
.shared
->msr
& (MSR_DR
|MSR_IR
)) == MSR_DR
)
284 pte
.vpage
|= ((u64
)VSID_REAL_DR
<< (SID_SHIFT
- 12));
286 pte
.vpage
|= ((u64
)VSID_REAL_IR
<< (SID_SHIFT
- 12));
290 page_found
= -EINVAL
;
294 if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) &&
295 (!(vcpu
->arch
.hflags
& BOOK3S_HFLAG_DCBZ32
))) {
297 * If we do the dcbz hack, we have to NX on every execution,
298 * so we can patch the executing code. This renders our guest
301 pte
.may_execute
= !data
;
304 if (page_found
== -ENOENT
) {
305 /* Page not found in guest PTE entries */
306 vcpu
->arch
.shared
->dar
= kvmppc_get_fault_dar(vcpu
);
307 vcpu
->arch
.shared
->dsisr
= to_svcpu(vcpu
)->fault_dsisr
;
308 vcpu
->arch
.shared
->msr
|=
309 (to_svcpu(vcpu
)->shadow_srr1
& 0x00000000f8000000ULL
);
310 kvmppc_book3s_queue_irqprio(vcpu
, vec
);
311 } else if (page_found
== -EPERM
) {
312 /* Storage protection */
313 vcpu
->arch
.shared
->dar
= kvmppc_get_fault_dar(vcpu
);
314 vcpu
->arch
.shared
->dsisr
=
315 to_svcpu(vcpu
)->fault_dsisr
& ~DSISR_NOHPTE
;
316 vcpu
->arch
.shared
->dsisr
|= DSISR_PROTFAULT
;
317 vcpu
->arch
.shared
->msr
|=
318 (to_svcpu(vcpu
)->shadow_srr1
& 0x00000000f8000000ULL
);
319 kvmppc_book3s_queue_irqprio(vcpu
, vec
);
320 } else if (page_found
== -EINVAL
) {
321 /* Page not found in guest SLB */
322 vcpu
->arch
.shared
->dar
= kvmppc_get_fault_dar(vcpu
);
323 kvmppc_book3s_queue_irqprio(vcpu
, vec
+ 0x80);
324 } else if (!is_mmio
&&
325 kvmppc_visible_gfn(vcpu
, pte
.raddr
>> PAGE_SHIFT
)) {
326 /* The guest's PTE is not mapped yet. Map on the host */
327 kvmppc_mmu_map_page(vcpu
, &pte
);
329 vcpu
->stat
.sp_storage
++;
330 else if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) &&
331 (!(vcpu
->arch
.hflags
& BOOK3S_HFLAG_DCBZ32
)))
332 kvmppc_patch_dcbz(vcpu
, &pte
);
335 vcpu
->stat
.mmio_exits
++;
336 vcpu
->arch
.paddr_accessed
= pte
.raddr
;
337 r
= kvmppc_emulate_mmio(run
, vcpu
);
338 if ( r
== RESUME_HOST_NV
)
345 static inline int get_fpr_index(int i
)
353 /* Give up external provider (FPU, Altivec, VSX) */
354 void kvmppc_giveup_ext(struct kvm_vcpu
*vcpu
, ulong msr
)
356 struct thread_struct
*t
= ¤t
->thread
;
357 u64
*vcpu_fpr
= vcpu
->arch
.fpr
;
359 u64
*vcpu_vsx
= vcpu
->arch
.vsr
;
361 u64
*thread_fpr
= (u64
*)t
->fpr
;
364 if (!(vcpu
->arch
.guest_owned_ext
& msr
))
368 printk(KERN_INFO
"Giving up ext 0x%lx\n", msr
);
374 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.fpr
); i
++)
375 vcpu_fpr
[i
] = thread_fpr
[get_fpr_index(i
)];
377 vcpu
->arch
.fpscr
= t
->fpscr
.val
;
380 #ifdef CONFIG_ALTIVEC
381 giveup_altivec(current
);
382 memcpy(vcpu
->arch
.vr
, t
->vr
, sizeof(vcpu
->arch
.vr
));
383 vcpu
->arch
.vscr
= t
->vscr
;
388 __giveup_vsx(current
);
389 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.vsr
); i
++)
390 vcpu_vsx
[i
] = thread_fpr
[get_fpr_index(i
) + 1];
397 vcpu
->arch
.guest_owned_ext
&= ~msr
;
398 current
->thread
.regs
->msr
&= ~msr
;
399 kvmppc_recalc_shadow_msr(vcpu
);
402 static int kvmppc_read_inst(struct kvm_vcpu
*vcpu
)
404 ulong srr0
= kvmppc_get_pc(vcpu
);
405 u32 last_inst
= kvmppc_get_last_inst(vcpu
);
408 ret
= kvmppc_ld(vcpu
, &srr0
, sizeof(u32
), &last_inst
, false);
409 if (ret
== -ENOENT
) {
410 ulong msr
= vcpu
->arch
.shared
->msr
;
412 msr
= kvmppc_set_field(msr
, 33, 33, 1);
413 msr
= kvmppc_set_field(msr
, 34, 36, 0);
414 vcpu
->arch
.shared
->msr
= kvmppc_set_field(msr
, 42, 47, 0);
415 kvmppc_book3s_queue_irqprio(vcpu
, BOOK3S_INTERRUPT_INST_STORAGE
);
416 return EMULATE_AGAIN
;
422 static int kvmppc_check_ext(struct kvm_vcpu
*vcpu
, unsigned int exit_nr
)
425 /* Need to do paired single emulation? */
426 if (!(vcpu
->arch
.hflags
& BOOK3S_HFLAG_PAIRED_SINGLE
))
429 /* Read out the instruction */
430 if (kvmppc_read_inst(vcpu
) == EMULATE_DONE
)
431 /* Need to emulate */
434 return EMULATE_AGAIN
;
437 /* Handle external providers (FPU, Altivec, VSX) */
438 static int kvmppc_handle_ext(struct kvm_vcpu
*vcpu
, unsigned int exit_nr
,
441 struct thread_struct
*t
= ¤t
->thread
;
442 u64
*vcpu_fpr
= vcpu
->arch
.fpr
;
444 u64
*vcpu_vsx
= vcpu
->arch
.vsr
;
446 u64
*thread_fpr
= (u64
*)t
->fpr
;
449 /* When we have paired singles, we emulate in software */
450 if (vcpu
->arch
.hflags
& BOOK3S_HFLAG_PAIRED_SINGLE
)
453 if (!(vcpu
->arch
.shared
->msr
& msr
)) {
454 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
458 /* We already own the ext */
459 if (vcpu
->arch
.guest_owned_ext
& msr
) {
464 printk(KERN_INFO
"Loading up ext 0x%lx\n", msr
);
467 current
->thread
.regs
->msr
|= msr
;
471 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.fpr
); i
++)
472 thread_fpr
[get_fpr_index(i
)] = vcpu_fpr
[i
];
474 t
->fpscr
.val
= vcpu
->arch
.fpscr
;
476 kvmppc_load_up_fpu();
479 #ifdef CONFIG_ALTIVEC
480 memcpy(t
->vr
, vcpu
->arch
.vr
, sizeof(vcpu
->arch
.vr
));
481 t
->vscr
= vcpu
->arch
.vscr
;
483 kvmppc_load_up_altivec();
488 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.vsr
); i
++)
489 thread_fpr
[get_fpr_index(i
) + 1] = vcpu_vsx
[i
];
490 kvmppc_load_up_vsx();
497 vcpu
->arch
.guest_owned_ext
|= msr
;
499 kvmppc_recalc_shadow_msr(vcpu
);
504 int kvmppc_handle_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
505 unsigned int exit_nr
)
509 vcpu
->stat
.sum_exits
++;
511 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
512 run
->ready_for_interrupt_injection
= 1;
514 trace_kvm_book3s_exit(exit_nr
, vcpu
);
517 case BOOK3S_INTERRUPT_INST_STORAGE
:
518 vcpu
->stat
.pf_instruc
++;
520 #ifdef CONFIG_PPC_BOOK3S_32
521 /* We set segments as unused segments when invalidating them. So
522 * treat the respective fault as segment fault. */
523 if (to_svcpu(vcpu
)->sr
[kvmppc_get_pc(vcpu
) >> SID_SHIFT
]
525 kvmppc_mmu_map_segment(vcpu
, kvmppc_get_pc(vcpu
));
531 /* only care about PTEG not found errors, but leave NX alone */
532 if (to_svcpu(vcpu
)->shadow_srr1
& 0x40000000) {
533 r
= kvmppc_handle_pagefault(run
, vcpu
, kvmppc_get_pc(vcpu
), exit_nr
);
534 vcpu
->stat
.sp_instruc
++;
535 } else if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) &&
536 (!(vcpu
->arch
.hflags
& BOOK3S_HFLAG_DCBZ32
))) {
538 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
539 * so we can't use the NX bit inside the guest. Let's cross our fingers,
540 * that no guest that needs the dcbz hack does NX.
542 kvmppc_mmu_pte_flush(vcpu
, kvmppc_get_pc(vcpu
), ~0xFFFUL
);
545 vcpu
->arch
.shared
->msr
|=
546 to_svcpu(vcpu
)->shadow_srr1
& 0x58000000;
547 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
551 case BOOK3S_INTERRUPT_DATA_STORAGE
:
553 ulong dar
= kvmppc_get_fault_dar(vcpu
);
554 vcpu
->stat
.pf_storage
++;
556 #ifdef CONFIG_PPC_BOOK3S_32
557 /* We set segments as unused segments when invalidating them. So
558 * treat the respective fault as segment fault. */
559 if ((to_svcpu(vcpu
)->sr
[dar
>> SID_SHIFT
]) == SR_INVALID
) {
560 kvmppc_mmu_map_segment(vcpu
, dar
);
566 /* The only case we need to handle is missing shadow PTEs */
567 if (to_svcpu(vcpu
)->fault_dsisr
& DSISR_NOHPTE
) {
568 r
= kvmppc_handle_pagefault(run
, vcpu
, dar
, exit_nr
);
570 vcpu
->arch
.shared
->dar
= dar
;
571 vcpu
->arch
.shared
->dsisr
= to_svcpu(vcpu
)->fault_dsisr
;
572 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
577 case BOOK3S_INTERRUPT_DATA_SEGMENT
:
578 if (kvmppc_mmu_map_segment(vcpu
, kvmppc_get_fault_dar(vcpu
)) < 0) {
579 vcpu
->arch
.shared
->dar
= kvmppc_get_fault_dar(vcpu
);
580 kvmppc_book3s_queue_irqprio(vcpu
,
581 BOOK3S_INTERRUPT_DATA_SEGMENT
);
585 case BOOK3S_INTERRUPT_INST_SEGMENT
:
586 if (kvmppc_mmu_map_segment(vcpu
, kvmppc_get_pc(vcpu
)) < 0) {
587 kvmppc_book3s_queue_irqprio(vcpu
,
588 BOOK3S_INTERRUPT_INST_SEGMENT
);
592 /* We're good on these - the host merely wanted to get our attention */
593 case BOOK3S_INTERRUPT_DECREMENTER
:
594 vcpu
->stat
.dec_exits
++;
597 case BOOK3S_INTERRUPT_EXTERNAL
:
598 vcpu
->stat
.ext_intr_exits
++;
601 case BOOK3S_INTERRUPT_PERFMON
:
604 case BOOK3S_INTERRUPT_PROGRAM
:
606 enum emulation_result er
;
610 flags
= to_svcpu(vcpu
)->shadow_srr1
& 0x1f0000ull
;
612 if (vcpu
->arch
.shared
->msr
& MSR_PR
) {
614 printk(KERN_INFO
"Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu
), kvmppc_get_last_inst(vcpu
));
616 if ((kvmppc_get_last_inst(vcpu
) & 0xff0007ff) !=
617 (INS_DCBZ
& 0xfffffff7)) {
618 kvmppc_core_queue_program(vcpu
, flags
);
624 vcpu
->stat
.emulated_inst_exits
++;
625 er
= kvmppc_emulate_instruction(run
, vcpu
);
634 printk(KERN_CRIT
"%s: emulation at %lx failed (%08x)\n",
635 __func__
, kvmppc_get_pc(vcpu
), kvmppc_get_last_inst(vcpu
));
636 kvmppc_core_queue_program(vcpu
, flags
);
639 case EMULATE_DO_MMIO
:
640 run
->exit_reason
= KVM_EXIT_MMIO
;
648 case BOOK3S_INTERRUPT_SYSCALL
:
649 if (vcpu
->arch
.osi_enabled
&&
650 (((u32
)kvmppc_get_gpr(vcpu
, 3)) == OSI_SC_MAGIC_R3
) &&
651 (((u32
)kvmppc_get_gpr(vcpu
, 4)) == OSI_SC_MAGIC_R4
)) {
653 u64
*gprs
= run
->osi
.gprs
;
656 run
->exit_reason
= KVM_EXIT_OSI
;
657 for (i
= 0; i
< 32; i
++)
658 gprs
[i
] = kvmppc_get_gpr(vcpu
, i
);
659 vcpu
->arch
.osi_needed
= 1;
661 } else if (!(vcpu
->arch
.shared
->msr
& MSR_PR
) &&
662 (((u32
)kvmppc_get_gpr(vcpu
, 0)) == KVM_SC_MAGIC_R0
)) {
663 /* KVM PV hypercalls */
664 kvmppc_set_gpr(vcpu
, 3, kvmppc_kvm_pv(vcpu
));
668 vcpu
->stat
.syscall_exits
++;
669 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
673 case BOOK3S_INTERRUPT_FP_UNAVAIL
:
674 case BOOK3S_INTERRUPT_ALTIVEC
:
675 case BOOK3S_INTERRUPT_VSX
:
680 case BOOK3S_INTERRUPT_FP_UNAVAIL
: ext_msr
= MSR_FP
; break;
681 case BOOK3S_INTERRUPT_ALTIVEC
: ext_msr
= MSR_VEC
; break;
682 case BOOK3S_INTERRUPT_VSX
: ext_msr
= MSR_VSX
; break;
685 switch (kvmppc_check_ext(vcpu
, exit_nr
)) {
687 /* everything ok - let's enable the ext */
688 r
= kvmppc_handle_ext(vcpu
, exit_nr
, ext_msr
);
691 /* we need to emulate this instruction */
692 goto program_interrupt
;
695 /* nothing to worry about - go again */
700 case BOOK3S_INTERRUPT_ALIGNMENT
:
701 if (kvmppc_read_inst(vcpu
) == EMULATE_DONE
) {
702 vcpu
->arch
.shared
->dsisr
= kvmppc_alignment_dsisr(vcpu
,
703 kvmppc_get_last_inst(vcpu
));
704 vcpu
->arch
.shared
->dar
= kvmppc_alignment_dar(vcpu
,
705 kvmppc_get_last_inst(vcpu
));
706 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
710 case BOOK3S_INTERRUPT_MACHINE_CHECK
:
711 case BOOK3S_INTERRUPT_TRACE
:
712 kvmppc_book3s_queue_irqprio(vcpu
, exit_nr
);
716 /* Ugh - bork here! What did we get? */
717 printk(KERN_EMERG
"exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
718 exit_nr
, kvmppc_get_pc(vcpu
), to_svcpu(vcpu
)->shadow_srr1
);
725 if (!(r
& RESUME_HOST
)) {
726 /* To avoid clobbering exit_reason, only check for signals if
727 * we aren't already exiting to userspace for some other
729 if (signal_pending(current
)) {
731 printk(KERN_EMERG
"KVM: Going back to host\n");
733 vcpu
->stat
.signal_exits
++;
734 run
->exit_reason
= KVM_EXIT_INTR
;
737 /* In case an interrupt came in that was triggered
738 * from userspace (like DEC), we need to check what
740 kvmppc_core_deliver_interrupts(vcpu
);
744 trace_kvm_book3s_reenter(r
, vcpu
);
749 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
750 struct kvm_sregs
*sregs
)
752 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
755 sregs
->pvr
= vcpu
->arch
.pvr
;
757 sregs
->u
.s
.sdr1
= to_book3s(vcpu
)->sdr1
;
758 if (vcpu
->arch
.hflags
& BOOK3S_HFLAG_SLB
) {
759 for (i
= 0; i
< 64; i
++) {
760 sregs
->u
.s
.ppc64
.slb
[i
].slbe
= vcpu
->arch
.slb
[i
].orige
| i
;
761 sregs
->u
.s
.ppc64
.slb
[i
].slbv
= vcpu
->arch
.slb
[i
].origv
;
764 for (i
= 0; i
< 16; i
++)
765 sregs
->u
.s
.ppc32
.sr
[i
] = vcpu
->arch
.shared
->sr
[i
];
767 for (i
= 0; i
< 8; i
++) {
768 sregs
->u
.s
.ppc32
.ibat
[i
] = vcpu3s
->ibat
[i
].raw
;
769 sregs
->u
.s
.ppc32
.dbat
[i
] = vcpu3s
->dbat
[i
].raw
;
776 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
777 struct kvm_sregs
*sregs
)
779 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
782 kvmppc_set_pvr(vcpu
, sregs
->pvr
);
784 vcpu3s
->sdr1
= sregs
->u
.s
.sdr1
;
785 if (vcpu
->arch
.hflags
& BOOK3S_HFLAG_SLB
) {
786 for (i
= 0; i
< 64; i
++) {
787 vcpu
->arch
.mmu
.slbmte(vcpu
, sregs
->u
.s
.ppc64
.slb
[i
].slbv
,
788 sregs
->u
.s
.ppc64
.slb
[i
].slbe
);
791 for (i
= 0; i
< 16; i
++) {
792 vcpu
->arch
.mmu
.mtsrin(vcpu
, i
, sregs
->u
.s
.ppc32
.sr
[i
]);
794 for (i
= 0; i
< 8; i
++) {
795 kvmppc_set_bat(vcpu
, &(vcpu3s
->ibat
[i
]), false,
796 (u32
)sregs
->u
.s
.ppc32
.ibat
[i
]);
797 kvmppc_set_bat(vcpu
, &(vcpu3s
->ibat
[i
]), true,
798 (u32
)(sregs
->u
.s
.ppc32
.ibat
[i
] >> 32));
799 kvmppc_set_bat(vcpu
, &(vcpu3s
->dbat
[i
]), false,
800 (u32
)sregs
->u
.s
.ppc32
.dbat
[i
]);
801 kvmppc_set_bat(vcpu
, &(vcpu3s
->dbat
[i
]), true,
802 (u32
)(sregs
->u
.s
.ppc32
.dbat
[i
] >> 32));
806 /* Flush the MMU after messing with the segments */
807 kvmppc_mmu_pte_flush(vcpu
, 0, 0);
812 int kvmppc_core_check_processor_compat(void)
817 struct kvm_vcpu
*kvmppc_core_vcpu_create(struct kvm
*kvm
, unsigned int id
)
819 struct kvmppc_vcpu_book3s
*vcpu_book3s
;
820 struct kvm_vcpu
*vcpu
;
824 vcpu_book3s
= vzalloc(sizeof(struct kvmppc_vcpu_book3s
));
828 vcpu_book3s
->shadow_vcpu
= (struct kvmppc_book3s_shadow_vcpu
*)
829 kzalloc(sizeof(*vcpu_book3s
->shadow_vcpu
), GFP_KERNEL
);
830 if (!vcpu_book3s
->shadow_vcpu
)
833 vcpu
= &vcpu_book3s
->vcpu
;
834 err
= kvm_vcpu_init(vcpu
, kvm
, id
);
836 goto free_shadow_vcpu
;
838 p
= __get_free_page(GFP_KERNEL
|__GFP_ZERO
);
839 /* the real shared page fills the last 4k of our page */
840 vcpu
->arch
.shared
= (void*)(p
+ PAGE_SIZE
- 4096);
844 vcpu
->arch
.host_retip
= kvm_return_point
;
845 vcpu
->arch
.host_msr
= mfmsr();
846 #ifdef CONFIG_PPC_BOOK3S_64
847 /* default to book3s_64 (970fx) */
848 vcpu
->arch
.pvr
= 0x3C0301;
850 /* default to book3s_32 (750) */
851 vcpu
->arch
.pvr
= 0x84202;
853 kvmppc_set_pvr(vcpu
, vcpu
->arch
.pvr
);
854 vcpu
->arch
.slb_nr
= 64;
856 /* remember where some real-mode handlers are */
857 vcpu
->arch
.trampoline_lowmem
= __pa(kvmppc_handler_lowmem_trampoline
);
858 vcpu
->arch
.trampoline_enter
= __pa(kvmppc_handler_trampoline_enter
);
859 vcpu
->arch
.highmem_handler
= (ulong
)kvmppc_handler_highmem
;
860 #ifdef CONFIG_PPC_BOOK3S_64
861 vcpu
->arch
.rmcall
= *(ulong
*)kvmppc_rmcall
;
863 vcpu
->arch
.rmcall
= (ulong
)kvmppc_rmcall
;
866 vcpu
->arch
.shadow_msr
= MSR_USER64
;
868 err
= kvmppc_mmu_init(vcpu
);
875 kvm_vcpu_uninit(vcpu
);
877 kfree(vcpu_book3s
->shadow_vcpu
);
884 void kvmppc_core_vcpu_free(struct kvm_vcpu
*vcpu
)
886 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
888 free_page((unsigned long)vcpu
->arch
.shared
& PAGE_MASK
);
889 kvm_vcpu_uninit(vcpu
);
890 kfree(vcpu_book3s
->shadow_vcpu
);
894 int kvmppc_vcpu_run(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
897 double fpr
[32][TS_FPRWIDTH
];
900 #ifdef CONFIG_ALTIVEC
903 unsigned long uninitialized_var(vrsave
);
911 /* No need to go into the guest when all we do is going out */
912 if (signal_pending(current
)) {
913 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
917 /* Save FPU state in stack */
918 if (current
->thread
.regs
->msr
& MSR_FP
)
920 memcpy(fpr
, current
->thread
.fpr
, sizeof(current
->thread
.fpr
));
921 fpscr
= current
->thread
.fpscr
.val
;
922 fpexc_mode
= current
->thread
.fpexc_mode
;
924 #ifdef CONFIG_ALTIVEC
925 /* Save Altivec state in stack */
926 used_vr
= current
->thread
.used_vr
;
928 if (current
->thread
.regs
->msr
& MSR_VEC
)
929 giveup_altivec(current
);
930 memcpy(vr
, current
->thread
.vr
, sizeof(current
->thread
.vr
));
931 vscr
= current
->thread
.vscr
;
932 vrsave
= current
->thread
.vrsave
;
937 /* Save VSX state in stack */
938 used_vsr
= current
->thread
.used_vsr
;
939 if (used_vsr
&& (current
->thread
.regs
->msr
& MSR_VSX
))
940 __giveup_vsx(current
);
943 /* Remember the MSR with disabled extensions */
944 ext_msr
= current
->thread
.regs
->msr
;
946 /* Preload FPU if it's enabled */
947 if (vcpu
->arch
.shared
->msr
& MSR_FP
)
948 kvmppc_handle_ext(vcpu
, BOOK3S_INTERRUPT_FP_UNAVAIL
, MSR_FP
);
952 ret
= __kvmppc_vcpu_run(kvm_run
, vcpu
);
958 current
->thread
.regs
->msr
= ext_msr
;
960 /* Make sure we save the guest FPU/Altivec/VSX state */
961 kvmppc_giveup_ext(vcpu
, MSR_FP
);
962 kvmppc_giveup_ext(vcpu
, MSR_VEC
);
963 kvmppc_giveup_ext(vcpu
, MSR_VSX
);
965 /* Restore FPU state from stack */
966 memcpy(current
->thread
.fpr
, fpr
, sizeof(current
->thread
.fpr
));
967 current
->thread
.fpscr
.val
= fpscr
;
968 current
->thread
.fpexc_mode
= fpexc_mode
;
970 #ifdef CONFIG_ALTIVEC
971 /* Restore Altivec state from stack */
972 if (used_vr
&& current
->thread
.used_vr
) {
973 memcpy(current
->thread
.vr
, vr
, sizeof(current
->thread
.vr
));
974 current
->thread
.vscr
= vscr
;
975 current
->thread
.vrsave
= vrsave
;
977 current
->thread
.used_vr
= used_vr
;
981 current
->thread
.used_vsr
= used_vsr
;
987 int kvmppc_core_prepare_memory_region(struct kvm
*kvm
,
988 struct kvm_userspace_memory_region
*mem
)
993 void kvmppc_core_commit_memory_region(struct kvm
*kvm
,
994 struct kvm_userspace_memory_region
*mem
)
998 int kvmppc_core_init_vm(struct kvm
*kvm
)
1003 void kvmppc_core_destroy_vm(struct kvm
*kvm
)
1007 static int kvmppc_book3s_init(void)
1011 r
= kvm_init(NULL
, sizeof(struct kvmppc_vcpu_book3s
), 0,
1017 r
= kvmppc_mmu_hpte_sysinit();
1022 static void kvmppc_book3s_exit(void)
1024 kvmppc_mmu_hpte_sysexit();
1028 module_init(kvmppc_book3s_init
);
1029 module_exit(kvmppc_book3s_exit
);