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/export.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
28 #include <asm/cputable.h>
29 #include <asm/cacheflush.h>
30 #include <asm/tlbflush.h>
31 #include <asm/uaccess.h>
33 #include <asm/kvm_ppc.h>
34 #include <asm/kvm_book3s.h>
35 #include <asm/mmu_context.h>
36 #include <asm/switch_to.h>
37 #include <asm/firmware.h>
38 #include <asm/hvcall.h>
39 #include <linux/gfp.h>
40 #include <linux/sched.h>
41 #include <linux/vmalloc.h>
42 #include <linux/highmem.h>
46 /* #define EXIT_DEBUG */
47 /* #define DEBUG_EXT */
49 static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
52 /* Some compatibility defines */
53 #ifdef CONFIG_PPC_BOOK3S_32
54 #define MSR_USER32 MSR_USER
55 #define MSR_USER64 MSR_USER
56 #define HW_PAGE_SIZE PAGE_SIZE
59 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
61 #ifdef CONFIG_PPC_BOOK3S_64
62 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
63 memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb));
64 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
65 sizeof(get_paca()->shadow_vcpu));
66 svcpu->slb_max = to_book3s(vcpu)->slb_shadow_max;
69 vcpu->cpu = smp_processor_id();
70 #ifdef CONFIG_PPC_BOOK3S_32
71 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
75 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
77 #ifdef CONFIG_PPC_BOOK3S_64
78 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
79 memcpy(to_book3s(vcpu)->slb_shadow, svcpu->slb, sizeof(svcpu->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 = svcpu->slb_max;
86 kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX);
90 int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
92 int r = 1; /* Indicate we want to get back into the guest */
94 /* We misuse TLB_FLUSH to indicate that we want to clear
95 all shadow cache entries */
96 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
97 kvmppc_mmu_pte_flush(vcpu, 0, 0);
102 /************* MMU Notifiers *************/
104 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
106 trace_kvm_unmap_hva(hva);
109 * Flush all shadow tlb entries everywhere. This is slow, but
110 * we are 100% sure that we catch the to be unmapped page
112 kvm_flush_remote_tlbs(kvm);
117 int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
119 /* kvm_unmap_hva flushes everything anyways */
120 kvm_unmap_hva(kvm, start);
125 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
127 /* XXX could be more clever ;) */
131 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
133 /* XXX could be more clever ;) */
137 void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
139 /* The page will get remapped properly on its next fault */
140 kvm_unmap_hva(kvm, hva);
143 /*****************************************/
145 static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
147 ulong smsr = vcpu->arch.shared->msr;
149 /* Guest MSR values */
150 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE;
151 /* Process MSR values */
152 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
153 /* External providers the guest reserved */
154 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
155 /* 64-bit Process MSR values */
156 #ifdef CONFIG_PPC_BOOK3S_64
157 smsr |= MSR_ISF | MSR_HV;
159 vcpu->arch.shadow_msr = smsr;
162 void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
164 ulong old_msr = vcpu->arch.shared->msr;
167 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
170 msr &= to_book3s(vcpu)->msr_mask;
171 vcpu->arch.shared->msr = msr;
172 kvmppc_recalc_shadow_msr(vcpu);
175 if (!vcpu->arch.pending_exceptions) {
176 kvm_vcpu_block(vcpu);
177 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
178 vcpu->stat.halt_wakeup++;
180 /* Unset POW bit after we woke up */
182 vcpu->arch.shared->msr = msr;
186 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
187 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
188 kvmppc_mmu_flush_segments(vcpu);
189 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
191 /* Preload magic page segment when in kernel mode */
192 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
193 struct kvm_vcpu_arch *a = &vcpu->arch;
196 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
198 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
203 * When switching from 32 to 64-bit, we may have a stale 32-bit
204 * magic page around, we need to flush it. Typically 32-bit magic
205 * page will be instanciated when calling into RTAS. Note: We
206 * assume that such transition only happens while in kernel mode,
207 * ie, we never transition from user 32-bit to kernel 64-bit with
208 * a 32-bit magic page around.
210 if (vcpu->arch.magic_page_pa &&
211 !(old_msr & MSR_PR) && !(old_msr & MSR_SF) && (msr & MSR_SF)) {
212 /* going from RTAS to normal kernel code */
213 kvmppc_mmu_pte_flush(vcpu, (uint32_t)vcpu->arch.magic_page_pa,
217 /* Preload FPU if it's enabled */
218 if (vcpu->arch.shared->msr & MSR_FP)
219 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
222 void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
226 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
227 vcpu->arch.pvr = pvr;
228 #ifdef CONFIG_PPC_BOOK3S_64
229 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
230 kvmppc_mmu_book3s_64_init(vcpu);
231 if (!to_book3s(vcpu)->hior_explicit)
232 to_book3s(vcpu)->hior = 0xfff00000;
233 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
234 vcpu->arch.cpu_type = KVM_CPU_3S_64;
238 kvmppc_mmu_book3s_32_init(vcpu);
239 if (!to_book3s(vcpu)->hior_explicit)
240 to_book3s(vcpu)->hior = 0;
241 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
242 vcpu->arch.cpu_type = KVM_CPU_3S_32;
245 kvmppc_sanity_check(vcpu);
247 /* If we are in hypervisor level on 970, we can tell the CPU to
248 * treat DCBZ as 32 bytes store */
249 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
250 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
251 !strcmp(cur_cpu_spec->platform, "ppc970"))
252 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
254 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
255 really needs them in a VM on Cell and force disable them. */
256 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
257 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
259 #ifdef CONFIG_PPC_BOOK3S_32
260 /* 32 bit Book3S always has 32 byte dcbz */
261 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
264 /* On some CPUs we can execute paired single operations natively */
265 asm ( "mfpvr %0" : "=r"(host_pvr));
267 case 0x00080200: /* lonestar 2.0 */
268 case 0x00088202: /* lonestar 2.2 */
269 case 0x70000100: /* gekko 1.0 */
270 case 0x00080100: /* gekko 2.0 */
271 case 0x00083203: /* gekko 2.3a */
272 case 0x00083213: /* gekko 2.3b */
273 case 0x00083204: /* gekko 2.4 */
274 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
275 case 0x00087200: /* broadway */
276 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
277 /* Enable HID2.PSE - in case we need it later */
278 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
282 /* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
283 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
284 * emulate 32 bytes dcbz length.
286 * The Book3s_64 inventors also realized this case and implemented a special bit
287 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
289 * My approach here is to patch the dcbz instruction on executing pages.
291 static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
298 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
299 if (is_error_page(hpage))
302 hpage_offset = pte->raddr & ~PAGE_MASK;
303 hpage_offset &= ~0xFFFULL;
307 page = kmap_atomic(hpage);
309 /* patch dcbz into reserved instruction, so we trap */
310 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
311 if ((page[i] & 0xff0007ff) == INS_DCBZ)
312 page[i] &= 0xfffffff7;
318 static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
320 ulong mp_pa = vcpu->arch.magic_page_pa;
322 if (!(vcpu->arch.shared->msr & MSR_SF))
323 mp_pa = (uint32_t)mp_pa;
325 if (unlikely(mp_pa) &&
326 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
330 return kvm_is_visible_gfn(vcpu->kvm, gfn);
333 int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
334 ulong eaddr, int vec)
336 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
337 int r = RESUME_GUEST;
340 struct kvmppc_pte pte;
341 bool is_mmio = false;
342 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
343 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
346 relocated = data ? dr : ir;
348 /* Resolve real address if translation turned on */
350 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
352 pte.may_execute = true;
354 pte.may_write = true;
355 pte.raddr = eaddr & KVM_PAM;
357 pte.vpage = eaddr >> 12;
360 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
362 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
366 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
368 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
369 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
371 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
375 page_found = -EINVAL;
379 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
380 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
382 * If we do the dcbz hack, we have to NX on every execution,
383 * so we can patch the executing code. This renders our guest
386 pte.may_execute = !data;
389 if (page_found == -ENOENT) {
390 /* Page not found in guest PTE entries */
391 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
392 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
393 vcpu->arch.shared->dsisr = svcpu->fault_dsisr;
394 vcpu->arch.shared->msr |=
395 (svcpu->shadow_srr1 & 0x00000000f8000000ULL);
397 kvmppc_book3s_queue_irqprio(vcpu, vec);
398 } else if (page_found == -EPERM) {
399 /* Storage protection */
400 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
401 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
402 vcpu->arch.shared->dsisr = svcpu->fault_dsisr & ~DSISR_NOHPTE;
403 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
404 vcpu->arch.shared->msr |=
405 svcpu->shadow_srr1 & 0x00000000f8000000ULL;
407 kvmppc_book3s_queue_irqprio(vcpu, vec);
408 } else if (page_found == -EINVAL) {
409 /* Page not found in guest SLB */
410 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
411 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
412 } else if (!is_mmio &&
413 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
414 /* The guest's PTE is not mapped yet. Map on the host */
415 kvmppc_mmu_map_page(vcpu, &pte);
417 vcpu->stat.sp_storage++;
418 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
419 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
420 kvmppc_patch_dcbz(vcpu, &pte);
423 vcpu->stat.mmio_exits++;
424 vcpu->arch.paddr_accessed = pte.raddr;
425 vcpu->arch.vaddr_accessed = pte.eaddr;
426 r = kvmppc_emulate_mmio(run, vcpu);
427 if ( r == RESUME_HOST_NV )
434 static inline int get_fpr_index(int i)
436 return i * TS_FPRWIDTH;
439 /* Give up external provider (FPU, Altivec, VSX) */
440 void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
442 struct thread_struct *t = ¤t->thread;
443 u64 *vcpu_fpr = vcpu->arch.fpr;
445 u64 *vcpu_vsx = vcpu->arch.vsr;
447 u64 *thread_fpr = (u64*)t->fpr;
451 * VSX instructions can access FP and vector registers, so if
452 * we are giving up VSX, make sure we give up FP and VMX as well.
455 msr |= MSR_FP | MSR_VEC;
457 msr &= vcpu->arch.guest_owned_ext;
462 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
467 * Note that on CPUs with VSX, giveup_fpu stores
468 * both the traditional FP registers and the added VSX
469 * registers into thread.fpr[].
472 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
473 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
475 vcpu->arch.fpscr = t->fpscr.val;
478 if (cpu_has_feature(CPU_FTR_VSX))
479 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
480 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
484 #ifdef CONFIG_ALTIVEC
486 giveup_altivec(current);
487 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
488 vcpu->arch.vscr = t->vscr;
492 vcpu->arch.guest_owned_ext &= ~(msr | MSR_VSX);
493 kvmppc_recalc_shadow_msr(vcpu);
496 static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
498 ulong srr0 = kvmppc_get_pc(vcpu);
499 u32 last_inst = kvmppc_get_last_inst(vcpu);
502 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
503 if (ret == -ENOENT) {
504 ulong msr = vcpu->arch.shared->msr;
506 msr = kvmppc_set_field(msr, 33, 33, 1);
507 msr = kvmppc_set_field(msr, 34, 36, 0);
508 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
509 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
510 return EMULATE_AGAIN;
516 static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
519 /* Need to do paired single emulation? */
520 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
523 /* Read out the instruction */
524 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
525 /* Need to emulate */
528 return EMULATE_AGAIN;
531 /* Handle external providers (FPU, Altivec, VSX) */
532 static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
535 struct thread_struct *t = ¤t->thread;
536 u64 *vcpu_fpr = vcpu->arch.fpr;
538 u64 *vcpu_vsx = vcpu->arch.vsr;
540 u64 *thread_fpr = (u64*)t->fpr;
543 /* When we have paired singles, we emulate in software */
544 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
547 if (!(vcpu->arch.shared->msr & msr)) {
548 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
552 if (msr == MSR_VSX) {
553 /* No VSX? Give an illegal instruction interrupt */
555 if (!cpu_has_feature(CPU_FTR_VSX))
558 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
563 * We have to load up all the FP and VMX registers before
564 * we can let the guest use VSX instructions.
566 msr = MSR_FP | MSR_VEC | MSR_VSX;
569 /* See if we already own all the ext(s) needed */
570 msr &= ~vcpu->arch.guest_owned_ext;
575 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
578 current->thread.regs->msr |= msr;
581 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
582 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
584 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
585 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
587 t->fpscr.val = vcpu->arch.fpscr;
589 kvmppc_load_up_fpu();
593 #ifdef CONFIG_ALTIVEC
594 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
595 t->vscr = vcpu->arch.vscr;
597 kvmppc_load_up_altivec();
601 vcpu->arch.guest_owned_ext |= msr;
602 kvmppc_recalc_shadow_msr(vcpu);
607 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
608 unsigned int exit_nr)
613 vcpu->stat.sum_exits++;
615 run->exit_reason = KVM_EXIT_UNKNOWN;
616 run->ready_for_interrupt_injection = 1;
618 /* We get here with MSR.EE=1 */
620 trace_kvm_exit(exit_nr, vcpu);
624 case BOOK3S_INTERRUPT_INST_STORAGE:
626 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
627 ulong shadow_srr1 = svcpu->shadow_srr1;
628 vcpu->stat.pf_instruc++;
630 #ifdef CONFIG_PPC_BOOK3S_32
631 /* We set segments as unused segments when invalidating them. So
632 * treat the respective fault as segment fault. */
633 if (svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] == SR_INVALID) {
634 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
642 /* only care about PTEG not found errors, but leave NX alone */
643 if (shadow_srr1 & 0x40000000) {
644 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
645 vcpu->stat.sp_instruc++;
646 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
647 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
649 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
650 * so we can't use the NX bit inside the guest. Let's cross our fingers,
651 * that no guest that needs the dcbz hack does NX.
653 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
656 vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000;
657 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
662 case BOOK3S_INTERRUPT_DATA_STORAGE:
664 ulong dar = kvmppc_get_fault_dar(vcpu);
665 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
666 u32 fault_dsisr = svcpu->fault_dsisr;
667 vcpu->stat.pf_storage++;
669 #ifdef CONFIG_PPC_BOOK3S_32
670 /* We set segments as unused segments when invalidating them. So
671 * treat the respective fault as segment fault. */
672 if ((svcpu->sr[dar >> SID_SHIFT]) == SR_INVALID) {
673 kvmppc_mmu_map_segment(vcpu, dar);
681 /* The only case we need to handle is missing shadow PTEs */
682 if (fault_dsisr & DSISR_NOHPTE) {
683 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
685 vcpu->arch.shared->dar = dar;
686 vcpu->arch.shared->dsisr = fault_dsisr;
687 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
692 case BOOK3S_INTERRUPT_DATA_SEGMENT:
693 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
694 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
695 kvmppc_book3s_queue_irqprio(vcpu,
696 BOOK3S_INTERRUPT_DATA_SEGMENT);
700 case BOOK3S_INTERRUPT_INST_SEGMENT:
701 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
702 kvmppc_book3s_queue_irqprio(vcpu,
703 BOOK3S_INTERRUPT_INST_SEGMENT);
707 /* We're good on these - the host merely wanted to get our attention */
708 case BOOK3S_INTERRUPT_DECREMENTER:
709 case BOOK3S_INTERRUPT_HV_DECREMENTER:
710 vcpu->stat.dec_exits++;
713 case BOOK3S_INTERRUPT_EXTERNAL:
714 case BOOK3S_INTERRUPT_EXTERNAL_LEVEL:
715 case BOOK3S_INTERRUPT_EXTERNAL_HV:
716 vcpu->stat.ext_intr_exits++;
719 case BOOK3S_INTERRUPT_PERFMON:
722 case BOOK3S_INTERRUPT_PROGRAM:
723 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
725 enum emulation_result er;
726 struct kvmppc_book3s_shadow_vcpu *svcpu;
730 svcpu = svcpu_get(vcpu);
731 flags = svcpu->shadow_srr1 & 0x1f0000ull;
734 if (vcpu->arch.shared->msr & MSR_PR) {
736 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
738 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
739 (INS_DCBZ & 0xfffffff7)) {
740 kvmppc_core_queue_program(vcpu, flags);
746 vcpu->stat.emulated_inst_exits++;
747 er = kvmppc_emulate_instruction(run, vcpu);
756 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
757 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
758 kvmppc_core_queue_program(vcpu, flags);
761 case EMULATE_DO_MMIO:
762 run->exit_reason = KVM_EXIT_MMIO;
765 case EMULATE_EXIT_USER:
773 case BOOK3S_INTERRUPT_SYSCALL:
774 if (vcpu->arch.papr_enabled &&
775 (kvmppc_get_last_inst(vcpu) == 0x44000022) &&
776 !(vcpu->arch.shared->msr & MSR_PR)) {
777 /* SC 1 papr hypercalls */
778 ulong cmd = kvmppc_get_gpr(vcpu, 3);
781 #ifdef CONFIG_KVM_BOOK3S_64_PR
782 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
788 run->papr_hcall.nr = cmd;
789 for (i = 0; i < 9; ++i) {
790 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
791 run->papr_hcall.args[i] = gpr;
793 run->exit_reason = KVM_EXIT_PAPR_HCALL;
794 vcpu->arch.hcall_needed = 1;
796 } else if (vcpu->arch.osi_enabled &&
797 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
798 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
800 u64 *gprs = run->osi.gprs;
803 run->exit_reason = KVM_EXIT_OSI;
804 for (i = 0; i < 32; i++)
805 gprs[i] = kvmppc_get_gpr(vcpu, i);
806 vcpu->arch.osi_needed = 1;
808 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
809 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
810 /* KVM PV hypercalls */
811 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
815 vcpu->stat.syscall_exits++;
816 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
820 case BOOK3S_INTERRUPT_FP_UNAVAIL:
821 case BOOK3S_INTERRUPT_ALTIVEC:
822 case BOOK3S_INTERRUPT_VSX:
827 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
828 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
829 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
832 switch (kvmppc_check_ext(vcpu, exit_nr)) {
834 /* everything ok - let's enable the ext */
835 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
838 /* we need to emulate this instruction */
839 goto program_interrupt;
842 /* nothing to worry about - go again */
847 case BOOK3S_INTERRUPT_ALIGNMENT:
848 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
849 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
850 kvmppc_get_last_inst(vcpu));
851 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
852 kvmppc_get_last_inst(vcpu));
853 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
857 case BOOK3S_INTERRUPT_MACHINE_CHECK:
858 case BOOK3S_INTERRUPT_TRACE:
859 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
864 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
865 ulong shadow_srr1 = svcpu->shadow_srr1;
867 /* Ugh - bork here! What did we get? */
868 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
869 exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
876 if (!(r & RESUME_HOST)) {
877 /* To avoid clobbering exit_reason, only check for signals if
878 * we aren't already exiting to userspace for some other
882 * Interrupts could be timers for the guest which we have to
883 * inject again, so let's postpone them until we're in the guest
884 * and if we really did time things so badly, then we just exit
885 * again due to a host external interrupt.
888 s = kvmppc_prepare_to_enter(vcpu);
893 kvmppc_lazy_ee_enable();
897 trace_kvm_book3s_reenter(r, vcpu);
902 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
903 struct kvm_sregs *sregs)
905 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
908 sregs->pvr = vcpu->arch.pvr;
910 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
911 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
912 for (i = 0; i < 64; i++) {
913 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
914 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
917 for (i = 0; i < 16; i++)
918 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
920 for (i = 0; i < 8; i++) {
921 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
922 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
929 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
930 struct kvm_sregs *sregs)
932 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
935 kvmppc_set_pvr(vcpu, sregs->pvr);
937 vcpu3s->sdr1 = sregs->u.s.sdr1;
938 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
939 for (i = 0; i < 64; i++) {
940 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
941 sregs->u.s.ppc64.slb[i].slbe);
944 for (i = 0; i < 16; i++) {
945 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
947 for (i = 0; i < 8; i++) {
948 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
949 (u32)sregs->u.s.ppc32.ibat[i]);
950 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
951 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
952 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
953 (u32)sregs->u.s.ppc32.dbat[i]);
954 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
955 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
959 /* Flush the MMU after messing with the segments */
960 kvmppc_mmu_pte_flush(vcpu, 0, 0);
965 int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
970 case KVM_REG_PPC_HIOR:
971 *val = get_reg_val(id, to_book3s(vcpu)->hior);
974 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
975 long int i = id - KVM_REG_PPC_VSR0;
977 if (!cpu_has_feature(CPU_FTR_VSX)) {
981 val->vsxval[0] = vcpu->arch.fpr[i];
982 val->vsxval[1] = vcpu->arch.vsr[i];
985 #endif /* CONFIG_VSX */
994 int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
999 case KVM_REG_PPC_HIOR:
1000 to_book3s(vcpu)->hior = set_reg_val(id, *val);
1001 to_book3s(vcpu)->hior_explicit = true;
1004 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
1005 long int i = id - KVM_REG_PPC_VSR0;
1007 if (!cpu_has_feature(CPU_FTR_VSX)) {
1011 vcpu->arch.fpr[i] = val->vsxval[0];
1012 vcpu->arch.vsr[i] = val->vsxval[1];
1015 #endif /* CONFIG_VSX */
1024 int kvmppc_core_check_processor_compat(void)
1029 struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1031 struct kvmppc_vcpu_book3s *vcpu_book3s;
1032 struct kvm_vcpu *vcpu;
1036 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
1040 vcpu_book3s->shadow_vcpu =
1041 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1042 if (!vcpu_book3s->shadow_vcpu)
1045 vcpu = &vcpu_book3s->vcpu;
1046 err = kvm_vcpu_init(vcpu, kvm, id);
1048 goto free_shadow_vcpu;
1050 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1051 /* the real shared page fills the last 4k of our page */
1052 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1056 #ifdef CONFIG_PPC_BOOK3S_64
1057 /* default to book3s_64 (970fx) */
1058 vcpu->arch.pvr = 0x3C0301;
1060 /* default to book3s_32 (750) */
1061 vcpu->arch.pvr = 0x84202;
1063 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1064 vcpu->arch.slb_nr = 64;
1066 vcpu->arch.shadow_msr = MSR_USER64;
1068 err = kvmppc_mmu_init(vcpu);
1075 kvm_vcpu_uninit(vcpu);
1077 kfree(vcpu_book3s->shadow_vcpu);
1081 return ERR_PTR(err);
1084 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1086 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1088 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
1089 kvm_vcpu_uninit(vcpu);
1090 kfree(vcpu_book3s->shadow_vcpu);
1094 int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1097 double fpr[32][TS_FPRWIDTH];
1100 #ifdef CONFIG_ALTIVEC
1103 unsigned long uninitialized_var(vrsave);
1111 /* Check if we can run the vcpu at all */
1112 if (!vcpu->arch.sane) {
1113 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1119 * Interrupts could be timers for the guest which we have to inject
1120 * again, so let's postpone them until we're in the guest and if we
1121 * really did time things so badly, then we just exit again due to
1122 * a host external interrupt.
1124 local_irq_disable();
1125 ret = kvmppc_prepare_to_enter(vcpu);
1131 /* Save FPU state in stack */
1132 if (current->thread.regs->msr & MSR_FP)
1133 giveup_fpu(current);
1134 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1135 fpscr = current->thread.fpscr.val;
1136 fpexc_mode = current->thread.fpexc_mode;
1138 #ifdef CONFIG_ALTIVEC
1139 /* Save Altivec state in stack */
1140 used_vr = current->thread.used_vr;
1142 if (current->thread.regs->msr & MSR_VEC)
1143 giveup_altivec(current);
1144 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1145 vscr = current->thread.vscr;
1146 vrsave = current->thread.vrsave;
1151 /* Save VSX state in stack */
1152 used_vsr = current->thread.used_vsr;
1153 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
1154 __giveup_vsx(current);
1157 /* Remember the MSR with disabled extensions */
1158 ext_msr = current->thread.regs->msr;
1160 /* Preload FPU if it's enabled */
1161 if (vcpu->arch.shared->msr & MSR_FP)
1162 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1164 kvmppc_lazy_ee_enable();
1166 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
1168 /* No need for kvm_guest_exit. It's done in handle_exit.
1169 We also get here with interrupts enabled. */
1171 /* Make sure we save the guest FPU/Altivec/VSX state */
1172 kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX);
1174 current->thread.regs->msr = ext_msr;
1176 /* Restore FPU/VSX state from stack */
1177 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1178 current->thread.fpscr.val = fpscr;
1179 current->thread.fpexc_mode = fpexc_mode;
1181 #ifdef CONFIG_ALTIVEC
1182 /* Restore Altivec state from stack */
1183 if (used_vr && current->thread.used_vr) {
1184 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1185 current->thread.vscr = vscr;
1186 current->thread.vrsave = vrsave;
1188 current->thread.used_vr = used_vr;
1192 current->thread.used_vsr = used_vsr;
1196 vcpu->mode = OUTSIDE_GUEST_MODE;
1201 * Get (and clear) the dirty memory log for a memory slot.
1203 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1204 struct kvm_dirty_log *log)
1206 struct kvm_memory_slot *memslot;
1207 struct kvm_vcpu *vcpu;
1213 mutex_lock(&kvm->slots_lock);
1215 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1219 /* If nothing is dirty, don't bother messing with page tables. */
1221 memslot = id_to_memslot(kvm->memslots, log->slot);
1223 ga = memslot->base_gfn << PAGE_SHIFT;
1224 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1226 kvm_for_each_vcpu(n, vcpu, kvm)
1227 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1229 n = kvm_dirty_bitmap_bytes(memslot);
1230 memset(memslot->dirty_bitmap, 0, n);
1235 mutex_unlock(&kvm->slots_lock);
1240 int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1242 info->flags = KVM_PPC_1T_SEGMENTS;
1244 /* SLB is always 64 entries */
1245 info->slb_size = 64;
1247 /* Standard 4k base page size segment */
1248 info->sps[0].page_shift = 12;
1249 info->sps[0].slb_enc = 0;
1250 info->sps[0].enc[0].page_shift = 12;
1251 info->sps[0].enc[0].pte_enc = 0;
1253 /* Standard 16M large page size segment */
1254 info->sps[1].page_shift = 24;
1255 info->sps[1].slb_enc = SLB_VSID_L;
1256 info->sps[1].enc[0].page_shift = 24;
1257 info->sps[1].enc[0].pte_enc = 0;
1261 #endif /* CONFIG_PPC64 */
1263 void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1264 struct kvm_memory_slot *dont)
1268 int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1269 unsigned long npages)
1274 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1275 struct kvm_memory_slot *memslot,
1276 struct kvm_userspace_memory_region *mem)
1281 void kvmppc_core_commit_memory_region(struct kvm *kvm,
1282 struct kvm_userspace_memory_region *mem,
1283 const struct kvm_memory_slot *old)
1287 void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
1291 static unsigned int kvm_global_user_count = 0;
1292 static DEFINE_SPINLOCK(kvm_global_user_count_lock);
1294 int kvmppc_core_init_vm(struct kvm *kvm)
1297 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
1298 INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
1301 if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
1302 spin_lock(&kvm_global_user_count_lock);
1303 if (++kvm_global_user_count == 1)
1304 pSeries_disable_reloc_on_exc();
1305 spin_unlock(&kvm_global_user_count_lock);
1310 void kvmppc_core_destroy_vm(struct kvm *kvm)
1313 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
1316 if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
1317 spin_lock(&kvm_global_user_count_lock);
1318 BUG_ON(kvm_global_user_count == 0);
1319 if (--kvm_global_user_count == 0)
1320 pSeries_enable_reloc_on_exc();
1321 spin_unlock(&kvm_global_user_count_lock);
1325 static int kvmppc_book3s_init(void)
1329 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1335 r = kvmppc_mmu_hpte_sysinit();
1340 static void kvmppc_book3s_exit(void)
1342 kvmppc_mmu_hpte_sysexit();
1346 module_init(kvmppc_book3s_init);
1347 module_exit(kvmppc_book3s_exit);