powerpc: remove the second argument of k[un]map_atomic()
[linux-2.6/cjktty.git] / arch / powerpc / kvm / book3s_pr.c
blob220fcdf26978efbf3bfd485772cfd7fb2453519b
1 /*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 * Paul Mackerras <paulus@samba.org>
9 * Description:
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>
27 #include <asm/reg.h>
28 #include <asm/cputable.h>
29 #include <asm/cacheflush.h>
30 #include <asm/tlbflush.h>
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
33 #include <asm/kvm_ppc.h>
34 #include <asm/kvm_book3s.h>
35 #include <asm/mmu_context.h>
36 #include <linux/gfp.h>
37 #include <linux/sched.h>
38 #include <linux/vmalloc.h>
39 #include <linux/highmem.h>
41 #include "trace.h"
43 /* #define EXIT_DEBUG */
44 /* #define DEBUG_EXT */
46 static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
47 ulong msr);
49 /* Some compatibility defines */
50 #ifdef CONFIG_PPC_BOOK3S_32
51 #define MSR_USER32 MSR_USER
52 #define MSR_USER64 MSR_USER
53 #define HW_PAGE_SIZE PAGE_SIZE
54 #endif
56 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
58 #ifdef CONFIG_PPC_BOOK3S_64
59 memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb));
60 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
61 sizeof(get_paca()->shadow_vcpu));
62 to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max;
63 #endif
65 #ifdef CONFIG_PPC_BOOK3S_32
66 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
67 #endif
70 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
72 #ifdef CONFIG_PPC_BOOK3S_64
73 memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb));
74 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
75 sizeof(get_paca()->shadow_vcpu));
76 to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max;
77 #endif
79 kvmppc_giveup_ext(vcpu, MSR_FP);
80 kvmppc_giveup_ext(vcpu, MSR_VEC);
81 kvmppc_giveup_ext(vcpu, MSR_VSX);
84 static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
86 ulong smsr = vcpu->arch.shared->msr;
88 /* Guest MSR values */
89 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
90 /* Process MSR values */
91 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
92 /* External providers the guest reserved */
93 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
94 /* 64-bit Process MSR values */
95 #ifdef CONFIG_PPC_BOOK3S_64
96 smsr |= MSR_ISF | MSR_HV;
97 #endif
98 vcpu->arch.shadow_msr = smsr;
101 void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
103 ulong old_msr = vcpu->arch.shared->msr;
105 #ifdef EXIT_DEBUG
106 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
107 #endif
109 msr &= to_book3s(vcpu)->msr_mask;
110 vcpu->arch.shared->msr = msr;
111 kvmppc_recalc_shadow_msr(vcpu);
113 if (msr & MSR_POW) {
114 if (!vcpu->arch.pending_exceptions) {
115 kvm_vcpu_block(vcpu);
116 vcpu->stat.halt_wakeup++;
118 /* Unset POW bit after we woke up */
119 msr &= ~MSR_POW;
120 vcpu->arch.shared->msr = msr;
124 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
125 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
126 kvmppc_mmu_flush_segments(vcpu);
127 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
129 /* Preload magic page segment when in kernel mode */
130 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
131 struct kvm_vcpu_arch *a = &vcpu->arch;
133 if (msr & MSR_DR)
134 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
135 else
136 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
140 /* Preload FPU if it's enabled */
141 if (vcpu->arch.shared->msr & MSR_FP)
142 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
145 void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
147 u32 host_pvr;
149 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
150 vcpu->arch.pvr = pvr;
151 #ifdef CONFIG_PPC_BOOK3S_64
152 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
153 kvmppc_mmu_book3s_64_init(vcpu);
154 to_book3s(vcpu)->hior = 0xfff00000;
155 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
156 vcpu->arch.cpu_type = KVM_CPU_3S_64;
157 } else
158 #endif
160 kvmppc_mmu_book3s_32_init(vcpu);
161 to_book3s(vcpu)->hior = 0;
162 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
163 vcpu->arch.cpu_type = KVM_CPU_3S_32;
166 kvmppc_sanity_check(vcpu);
168 /* If we are in hypervisor level on 970, we can tell the CPU to
169 * treat DCBZ as 32 bytes store */
170 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
171 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
172 !strcmp(cur_cpu_spec->platform, "ppc970"))
173 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
175 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
176 really needs them in a VM on Cell and force disable them. */
177 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
178 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
180 #ifdef CONFIG_PPC_BOOK3S_32
181 /* 32 bit Book3S always has 32 byte dcbz */
182 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
183 #endif
185 /* On some CPUs we can execute paired single operations natively */
186 asm ( "mfpvr %0" : "=r"(host_pvr));
187 switch (host_pvr) {
188 case 0x00080200: /* lonestar 2.0 */
189 case 0x00088202: /* lonestar 2.2 */
190 case 0x70000100: /* gekko 1.0 */
191 case 0x00080100: /* gekko 2.0 */
192 case 0x00083203: /* gekko 2.3a */
193 case 0x00083213: /* gekko 2.3b */
194 case 0x00083204: /* gekko 2.4 */
195 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
196 case 0x00087200: /* broadway */
197 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
198 /* Enable HID2.PSE - in case we need it later */
199 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
203 /* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
204 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
205 * emulate 32 bytes dcbz length.
207 * The Book3s_64 inventors also realized this case and implemented a special bit
208 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
210 * My approach here is to patch the dcbz instruction on executing pages.
212 static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
214 struct page *hpage;
215 u64 hpage_offset;
216 u32 *page;
217 int i;
219 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
220 if (is_error_page(hpage)) {
221 kvm_release_page_clean(hpage);
222 return;
225 hpage_offset = pte->raddr & ~PAGE_MASK;
226 hpage_offset &= ~0xFFFULL;
227 hpage_offset /= 4;
229 get_page(hpage);
230 page = kmap_atomic(hpage);
232 /* patch dcbz into reserved instruction, so we trap */
233 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
234 if ((page[i] & 0xff0007ff) == INS_DCBZ)
235 page[i] &= 0xfffffff7;
237 kunmap_atomic(page);
238 put_page(hpage);
241 static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
243 ulong mp_pa = vcpu->arch.magic_page_pa;
245 if (unlikely(mp_pa) &&
246 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
247 return 1;
250 return kvm_is_visible_gfn(vcpu->kvm, gfn);
253 int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
254 ulong eaddr, int vec)
256 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
257 int r = RESUME_GUEST;
258 int relocated;
259 int page_found = 0;
260 struct kvmppc_pte pte;
261 bool is_mmio = false;
262 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
263 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
264 u64 vsid;
266 relocated = data ? dr : ir;
268 /* Resolve real address if translation turned on */
269 if (relocated) {
270 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
271 } else {
272 pte.may_execute = true;
273 pte.may_read = true;
274 pte.may_write = true;
275 pte.raddr = eaddr & KVM_PAM;
276 pte.eaddr = eaddr;
277 pte.vpage = eaddr >> 12;
280 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
281 case 0:
282 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
283 break;
284 case MSR_DR:
285 case MSR_IR:
286 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
288 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
289 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
290 else
291 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
292 pte.vpage |= vsid;
294 if (vsid == -1)
295 page_found = -EINVAL;
296 break;
299 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
300 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
302 * If we do the dcbz hack, we have to NX on every execution,
303 * so we can patch the executing code. This renders our guest
304 * NX-less.
306 pte.may_execute = !data;
309 if (page_found == -ENOENT) {
310 /* Page not found in guest PTE entries */
311 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
312 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
313 vcpu->arch.shared->msr |=
314 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
315 kvmppc_book3s_queue_irqprio(vcpu, vec);
316 } else if (page_found == -EPERM) {
317 /* Storage protection */
318 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
319 vcpu->arch.shared->dsisr =
320 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
321 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
322 vcpu->arch.shared->msr |=
323 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
324 kvmppc_book3s_queue_irqprio(vcpu, vec);
325 } else if (page_found == -EINVAL) {
326 /* Page not found in guest SLB */
327 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
328 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
329 } else if (!is_mmio &&
330 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
331 /* The guest's PTE is not mapped yet. Map on the host */
332 kvmppc_mmu_map_page(vcpu, &pte);
333 if (data)
334 vcpu->stat.sp_storage++;
335 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
336 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
337 kvmppc_patch_dcbz(vcpu, &pte);
338 } else {
339 /* MMIO */
340 vcpu->stat.mmio_exits++;
341 vcpu->arch.paddr_accessed = pte.raddr;
342 r = kvmppc_emulate_mmio(run, vcpu);
343 if ( r == RESUME_HOST_NV )
344 r = RESUME_HOST;
347 return r;
350 static inline int get_fpr_index(int i)
352 #ifdef CONFIG_VSX
353 i *= 2;
354 #endif
355 return i;
358 /* Give up external provider (FPU, Altivec, VSX) */
359 void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
361 struct thread_struct *t = &current->thread;
362 u64 *vcpu_fpr = vcpu->arch.fpr;
363 #ifdef CONFIG_VSX
364 u64 *vcpu_vsx = vcpu->arch.vsr;
365 #endif
366 u64 *thread_fpr = (u64*)t->fpr;
367 int i;
369 if (!(vcpu->arch.guest_owned_ext & msr))
370 return;
372 #ifdef DEBUG_EXT
373 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
374 #endif
376 switch (msr) {
377 case MSR_FP:
378 giveup_fpu(current);
379 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
380 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
382 vcpu->arch.fpscr = t->fpscr.val;
383 break;
384 case MSR_VEC:
385 #ifdef CONFIG_ALTIVEC
386 giveup_altivec(current);
387 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
388 vcpu->arch.vscr = t->vscr;
389 #endif
390 break;
391 case MSR_VSX:
392 #ifdef CONFIG_VSX
393 __giveup_vsx(current);
394 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
395 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
396 #endif
397 break;
398 default:
399 BUG();
402 vcpu->arch.guest_owned_ext &= ~msr;
403 current->thread.regs->msr &= ~msr;
404 kvmppc_recalc_shadow_msr(vcpu);
407 static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
409 ulong srr0 = kvmppc_get_pc(vcpu);
410 u32 last_inst = kvmppc_get_last_inst(vcpu);
411 int ret;
413 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
414 if (ret == -ENOENT) {
415 ulong msr = vcpu->arch.shared->msr;
417 msr = kvmppc_set_field(msr, 33, 33, 1);
418 msr = kvmppc_set_field(msr, 34, 36, 0);
419 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
420 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
421 return EMULATE_AGAIN;
424 return EMULATE_DONE;
427 static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
430 /* Need to do paired single emulation? */
431 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
432 return EMULATE_DONE;
434 /* Read out the instruction */
435 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
436 /* Need to emulate */
437 return EMULATE_FAIL;
439 return EMULATE_AGAIN;
442 /* Handle external providers (FPU, Altivec, VSX) */
443 static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
444 ulong msr)
446 struct thread_struct *t = &current->thread;
447 u64 *vcpu_fpr = vcpu->arch.fpr;
448 #ifdef CONFIG_VSX
449 u64 *vcpu_vsx = vcpu->arch.vsr;
450 #endif
451 u64 *thread_fpr = (u64*)t->fpr;
452 int i;
454 /* When we have paired singles, we emulate in software */
455 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
456 return RESUME_GUEST;
458 if (!(vcpu->arch.shared->msr & msr)) {
459 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
460 return RESUME_GUEST;
463 /* We already own the ext */
464 if (vcpu->arch.guest_owned_ext & msr) {
465 return RESUME_GUEST;
468 #ifdef DEBUG_EXT
469 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
470 #endif
472 current->thread.regs->msr |= msr;
474 switch (msr) {
475 case MSR_FP:
476 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
477 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
479 t->fpscr.val = vcpu->arch.fpscr;
480 t->fpexc_mode = 0;
481 kvmppc_load_up_fpu();
482 break;
483 case MSR_VEC:
484 #ifdef CONFIG_ALTIVEC
485 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
486 t->vscr = vcpu->arch.vscr;
487 t->vrsave = -1;
488 kvmppc_load_up_altivec();
489 #endif
490 break;
491 case MSR_VSX:
492 #ifdef CONFIG_VSX
493 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
494 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
495 kvmppc_load_up_vsx();
496 #endif
497 break;
498 default:
499 BUG();
502 vcpu->arch.guest_owned_ext |= msr;
504 kvmppc_recalc_shadow_msr(vcpu);
506 return RESUME_GUEST;
509 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
510 unsigned int exit_nr)
512 int r = RESUME_HOST;
514 vcpu->stat.sum_exits++;
516 run->exit_reason = KVM_EXIT_UNKNOWN;
517 run->ready_for_interrupt_injection = 1;
519 trace_kvm_book3s_exit(exit_nr, vcpu);
520 kvm_resched(vcpu);
521 switch (exit_nr) {
522 case BOOK3S_INTERRUPT_INST_STORAGE:
523 vcpu->stat.pf_instruc++;
525 #ifdef CONFIG_PPC_BOOK3S_32
526 /* We set segments as unused segments when invalidating them. So
527 * treat the respective fault as segment fault. */
528 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
529 == SR_INVALID) {
530 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
531 r = RESUME_GUEST;
532 break;
534 #endif
536 /* only care about PTEG not found errors, but leave NX alone */
537 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
538 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
539 vcpu->stat.sp_instruc++;
540 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
541 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
543 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
544 * so we can't use the NX bit inside the guest. Let's cross our fingers,
545 * that no guest that needs the dcbz hack does NX.
547 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
548 r = RESUME_GUEST;
549 } else {
550 vcpu->arch.shared->msr |=
551 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
552 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
553 r = RESUME_GUEST;
555 break;
556 case BOOK3S_INTERRUPT_DATA_STORAGE:
558 ulong dar = kvmppc_get_fault_dar(vcpu);
559 vcpu->stat.pf_storage++;
561 #ifdef CONFIG_PPC_BOOK3S_32
562 /* We set segments as unused segments when invalidating them. So
563 * treat the respective fault as segment fault. */
564 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
565 kvmppc_mmu_map_segment(vcpu, dar);
566 r = RESUME_GUEST;
567 break;
569 #endif
571 /* The only case we need to handle is missing shadow PTEs */
572 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
573 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
574 } else {
575 vcpu->arch.shared->dar = dar;
576 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
577 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
578 r = RESUME_GUEST;
580 break;
582 case BOOK3S_INTERRUPT_DATA_SEGMENT:
583 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
584 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
585 kvmppc_book3s_queue_irqprio(vcpu,
586 BOOK3S_INTERRUPT_DATA_SEGMENT);
588 r = RESUME_GUEST;
589 break;
590 case BOOK3S_INTERRUPT_INST_SEGMENT:
591 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
592 kvmppc_book3s_queue_irqprio(vcpu,
593 BOOK3S_INTERRUPT_INST_SEGMENT);
595 r = RESUME_GUEST;
596 break;
597 /* We're good on these - the host merely wanted to get our attention */
598 case BOOK3S_INTERRUPT_DECREMENTER:
599 vcpu->stat.dec_exits++;
600 r = RESUME_GUEST;
601 break;
602 case BOOK3S_INTERRUPT_EXTERNAL:
603 vcpu->stat.ext_intr_exits++;
604 r = RESUME_GUEST;
605 break;
606 case BOOK3S_INTERRUPT_PERFMON:
607 r = RESUME_GUEST;
608 break;
609 case BOOK3S_INTERRUPT_PROGRAM:
611 enum emulation_result er;
612 ulong flags;
614 program_interrupt:
615 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
617 if (vcpu->arch.shared->msr & MSR_PR) {
618 #ifdef EXIT_DEBUG
619 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
620 #endif
621 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
622 (INS_DCBZ & 0xfffffff7)) {
623 kvmppc_core_queue_program(vcpu, flags);
624 r = RESUME_GUEST;
625 break;
629 vcpu->stat.emulated_inst_exits++;
630 er = kvmppc_emulate_instruction(run, vcpu);
631 switch (er) {
632 case EMULATE_DONE:
633 r = RESUME_GUEST_NV;
634 break;
635 case EMULATE_AGAIN:
636 r = RESUME_GUEST;
637 break;
638 case EMULATE_FAIL:
639 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
640 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
641 kvmppc_core_queue_program(vcpu, flags);
642 r = RESUME_GUEST;
643 break;
644 case EMULATE_DO_MMIO:
645 run->exit_reason = KVM_EXIT_MMIO;
646 r = RESUME_HOST_NV;
647 break;
648 default:
649 BUG();
651 break;
653 case BOOK3S_INTERRUPT_SYSCALL:
654 if (vcpu->arch.papr_enabled &&
655 (kvmppc_get_last_inst(vcpu) == 0x44000022) &&
656 !(vcpu->arch.shared->msr & MSR_PR)) {
657 /* SC 1 papr hypercalls */
658 ulong cmd = kvmppc_get_gpr(vcpu, 3);
659 int i;
661 #ifdef CONFIG_KVM_BOOK3S_64_PR
662 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
663 r = RESUME_GUEST;
664 break;
666 #endif
668 run->papr_hcall.nr = cmd;
669 for (i = 0; i < 9; ++i) {
670 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
671 run->papr_hcall.args[i] = gpr;
673 run->exit_reason = KVM_EXIT_PAPR_HCALL;
674 vcpu->arch.hcall_needed = 1;
675 r = RESUME_HOST;
676 } else if (vcpu->arch.osi_enabled &&
677 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
678 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
679 /* MOL hypercalls */
680 u64 *gprs = run->osi.gprs;
681 int i;
683 run->exit_reason = KVM_EXIT_OSI;
684 for (i = 0; i < 32; i++)
685 gprs[i] = kvmppc_get_gpr(vcpu, i);
686 vcpu->arch.osi_needed = 1;
687 r = RESUME_HOST_NV;
688 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
689 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
690 /* KVM PV hypercalls */
691 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
692 r = RESUME_GUEST;
693 } else {
694 /* Guest syscalls */
695 vcpu->stat.syscall_exits++;
696 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
697 r = RESUME_GUEST;
699 break;
700 case BOOK3S_INTERRUPT_FP_UNAVAIL:
701 case BOOK3S_INTERRUPT_ALTIVEC:
702 case BOOK3S_INTERRUPT_VSX:
704 int ext_msr = 0;
706 switch (exit_nr) {
707 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
708 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
709 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
712 switch (kvmppc_check_ext(vcpu, exit_nr)) {
713 case EMULATE_DONE:
714 /* everything ok - let's enable the ext */
715 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
716 break;
717 case EMULATE_FAIL:
718 /* we need to emulate this instruction */
719 goto program_interrupt;
720 break;
721 default:
722 /* nothing to worry about - go again */
723 break;
725 break;
727 case BOOK3S_INTERRUPT_ALIGNMENT:
728 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
729 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
730 kvmppc_get_last_inst(vcpu));
731 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
732 kvmppc_get_last_inst(vcpu));
733 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
735 r = RESUME_GUEST;
736 break;
737 case BOOK3S_INTERRUPT_MACHINE_CHECK:
738 case BOOK3S_INTERRUPT_TRACE:
739 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
740 r = RESUME_GUEST;
741 break;
742 default:
743 /* Ugh - bork here! What did we get? */
744 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
745 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
746 r = RESUME_HOST;
747 BUG();
748 break;
752 if (!(r & RESUME_HOST)) {
753 /* To avoid clobbering exit_reason, only check for signals if
754 * we aren't already exiting to userspace for some other
755 * reason. */
756 if (signal_pending(current)) {
757 #ifdef EXIT_DEBUG
758 printk(KERN_EMERG "KVM: Going back to host\n");
759 #endif
760 vcpu->stat.signal_exits++;
761 run->exit_reason = KVM_EXIT_INTR;
762 r = -EINTR;
763 } else {
764 /* In case an interrupt came in that was triggered
765 * from userspace (like DEC), we need to check what
766 * to inject now! */
767 kvmppc_core_deliver_interrupts(vcpu);
771 trace_kvm_book3s_reenter(r, vcpu);
773 return r;
776 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
777 struct kvm_sregs *sregs)
779 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
780 int i;
782 sregs->pvr = vcpu->arch.pvr;
784 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
785 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
786 for (i = 0; i < 64; i++) {
787 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
788 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
790 } else {
791 for (i = 0; i < 16; i++)
792 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
794 for (i = 0; i < 8; i++) {
795 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
796 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
800 return 0;
803 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
804 struct kvm_sregs *sregs)
806 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
807 int i;
809 kvmppc_set_pvr(vcpu, sregs->pvr);
811 vcpu3s->sdr1 = sregs->u.s.sdr1;
812 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
813 for (i = 0; i < 64; i++) {
814 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
815 sregs->u.s.ppc64.slb[i].slbe);
817 } else {
818 for (i = 0; i < 16; i++) {
819 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
821 for (i = 0; i < 8; i++) {
822 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
823 (u32)sregs->u.s.ppc32.ibat[i]);
824 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
825 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
826 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
827 (u32)sregs->u.s.ppc32.dbat[i]);
828 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
829 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
833 /* Flush the MMU after messing with the segments */
834 kvmppc_mmu_pte_flush(vcpu, 0, 0);
836 return 0;
839 int kvmppc_core_check_processor_compat(void)
841 return 0;
844 struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
846 struct kvmppc_vcpu_book3s *vcpu_book3s;
847 struct kvm_vcpu *vcpu;
848 int err = -ENOMEM;
849 unsigned long p;
851 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
852 if (!vcpu_book3s)
853 goto out;
855 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
856 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
857 if (!vcpu_book3s->shadow_vcpu)
858 goto free_vcpu;
860 vcpu = &vcpu_book3s->vcpu;
861 err = kvm_vcpu_init(vcpu, kvm, id);
862 if (err)
863 goto free_shadow_vcpu;
865 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
866 /* the real shared page fills the last 4k of our page */
867 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
868 if (!p)
869 goto uninit_vcpu;
871 #ifdef CONFIG_PPC_BOOK3S_64
872 /* default to book3s_64 (970fx) */
873 vcpu->arch.pvr = 0x3C0301;
874 #else
875 /* default to book3s_32 (750) */
876 vcpu->arch.pvr = 0x84202;
877 #endif
878 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
879 vcpu->arch.slb_nr = 64;
881 vcpu->arch.shadow_msr = MSR_USER64;
883 err = kvmppc_mmu_init(vcpu);
884 if (err < 0)
885 goto uninit_vcpu;
887 return vcpu;
889 uninit_vcpu:
890 kvm_vcpu_uninit(vcpu);
891 free_shadow_vcpu:
892 kfree(vcpu_book3s->shadow_vcpu);
893 free_vcpu:
894 vfree(vcpu_book3s);
895 out:
896 return ERR_PTR(err);
899 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
901 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
903 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
904 kvm_vcpu_uninit(vcpu);
905 kfree(vcpu_book3s->shadow_vcpu);
906 vfree(vcpu_book3s);
909 int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
911 int ret;
912 double fpr[32][TS_FPRWIDTH];
913 unsigned int fpscr;
914 int fpexc_mode;
915 #ifdef CONFIG_ALTIVEC
916 vector128 vr[32];
917 vector128 vscr;
918 unsigned long uninitialized_var(vrsave);
919 int used_vr;
920 #endif
921 #ifdef CONFIG_VSX
922 int used_vsr;
923 #endif
924 ulong ext_msr;
926 /* Check if we can run the vcpu at all */
927 if (!vcpu->arch.sane) {
928 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
929 return -EINVAL;
932 /* No need to go into the guest when all we do is going out */
933 if (signal_pending(current)) {
934 kvm_run->exit_reason = KVM_EXIT_INTR;
935 return -EINTR;
938 /* Save FPU state in stack */
939 if (current->thread.regs->msr & MSR_FP)
940 giveup_fpu(current);
941 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
942 fpscr = current->thread.fpscr.val;
943 fpexc_mode = current->thread.fpexc_mode;
945 #ifdef CONFIG_ALTIVEC
946 /* Save Altivec state in stack */
947 used_vr = current->thread.used_vr;
948 if (used_vr) {
949 if (current->thread.regs->msr & MSR_VEC)
950 giveup_altivec(current);
951 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
952 vscr = current->thread.vscr;
953 vrsave = current->thread.vrsave;
955 #endif
957 #ifdef CONFIG_VSX
958 /* Save VSX state in stack */
959 used_vsr = current->thread.used_vsr;
960 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
961 __giveup_vsx(current);
962 #endif
964 /* Remember the MSR with disabled extensions */
965 ext_msr = current->thread.regs->msr;
967 /* Preload FPU if it's enabled */
968 if (vcpu->arch.shared->msr & MSR_FP)
969 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
971 kvm_guest_enter();
973 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
975 kvm_guest_exit();
977 local_irq_disable();
979 current->thread.regs->msr = ext_msr;
981 /* Make sure we save the guest FPU/Altivec/VSX state */
982 kvmppc_giveup_ext(vcpu, MSR_FP);
983 kvmppc_giveup_ext(vcpu, MSR_VEC);
984 kvmppc_giveup_ext(vcpu, MSR_VSX);
986 /* Restore FPU state from stack */
987 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
988 current->thread.fpscr.val = fpscr;
989 current->thread.fpexc_mode = fpexc_mode;
991 #ifdef CONFIG_ALTIVEC
992 /* Restore Altivec state from stack */
993 if (used_vr && current->thread.used_vr) {
994 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
995 current->thread.vscr = vscr;
996 current->thread.vrsave = vrsave;
998 current->thread.used_vr = used_vr;
999 #endif
1001 #ifdef CONFIG_VSX
1002 current->thread.used_vsr = used_vsr;
1003 #endif
1005 return ret;
1008 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1009 struct kvm_userspace_memory_region *mem)
1011 return 0;
1014 void kvmppc_core_commit_memory_region(struct kvm *kvm,
1015 struct kvm_userspace_memory_region *mem)
1019 int kvmppc_core_init_vm(struct kvm *kvm)
1021 return 0;
1024 void kvmppc_core_destroy_vm(struct kvm *kvm)
1028 static int kvmppc_book3s_init(void)
1030 int r;
1032 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1033 THIS_MODULE);
1035 if (r)
1036 return r;
1038 r = kvmppc_mmu_hpte_sysinit();
1040 return r;
1043 static void kvmppc_book3s_exit(void)
1045 kvmppc_mmu_hpte_sysexit();
1046 kvm_exit();
1049 module_init(kvmppc_book3s_init);
1050 module_exit(kvmppc_book3s_exit);