diff options
Diffstat (limited to 'arch/powerpc/kvm/book3s.c')
-rw-r--r-- | arch/powerpc/kvm/book3s.c | 1007 |
1 files changed, 13 insertions, 994 deletions
diff --git a/arch/powerpc/kvm/book3s.c b/arch/powerpc/kvm/book3s.c index 0f95b5cce033..f68a34d16035 100644 --- a/arch/powerpc/kvm/book3s.c +++ b/arch/powerpc/kvm/book3s.c | |||
@@ -17,7 +17,6 @@ | |||
17 | #include <linux/kvm_host.h> | 17 | #include <linux/kvm_host.h> |
18 | #include <linux/err.h> | 18 | #include <linux/err.h> |
19 | #include <linux/slab.h> | 19 | #include <linux/slab.h> |
20 | #include "trace.h" | ||
21 | 20 | ||
22 | #include <asm/reg.h> | 21 | #include <asm/reg.h> |
23 | #include <asm/cputable.h> | 22 | #include <asm/cputable.h> |
@@ -28,25 +27,17 @@ | |||
28 | #include <asm/kvm_ppc.h> | 27 | #include <asm/kvm_ppc.h> |
29 | #include <asm/kvm_book3s.h> | 28 | #include <asm/kvm_book3s.h> |
30 | #include <asm/mmu_context.h> | 29 | #include <asm/mmu_context.h> |
30 | #include <asm/page.h> | ||
31 | #include <linux/gfp.h> | 31 | #include <linux/gfp.h> |
32 | #include <linux/sched.h> | 32 | #include <linux/sched.h> |
33 | #include <linux/vmalloc.h> | 33 | #include <linux/vmalloc.h> |
34 | #include <linux/highmem.h> | 34 | #include <linux/highmem.h> |
35 | 35 | ||
36 | #include "trace.h" | ||
37 | |||
36 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU | 38 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
37 | 39 | ||
38 | /* #define EXIT_DEBUG */ | 40 | /* #define EXIT_DEBUG */ |
39 | /* #define DEBUG_EXT */ | ||
40 | |||
41 | static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr, | ||
42 | ulong msr); | ||
43 | |||
44 | /* Some compatibility defines */ | ||
45 | #ifdef CONFIG_PPC_BOOK3S_32 | ||
46 | #define MSR_USER32 MSR_USER | ||
47 | #define MSR_USER64 MSR_USER | ||
48 | #define HW_PAGE_SIZE PAGE_SIZE | ||
49 | #endif | ||
50 | 41 | ||
51 | struct kvm_stats_debugfs_item debugfs_entries[] = { | 42 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
52 | { "exits", VCPU_STAT(sum_exits) }, | 43 | { "exits", VCPU_STAT(sum_exits) }, |
@@ -77,100 +68,11 @@ void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu) | |||
77 | { | 68 | { |
78 | } | 69 | } |
79 | 70 | ||
80 | void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu) | ||
81 | { | ||
82 | #ifdef CONFIG_PPC_BOOK3S_64 | ||
83 | memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb)); | ||
84 | memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu, | ||
85 | sizeof(get_paca()->shadow_vcpu)); | ||
86 | to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max; | ||
87 | #endif | ||
88 | |||
89 | #ifdef CONFIG_PPC_BOOK3S_32 | ||
90 | current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu; | ||
91 | #endif | ||
92 | } | ||
93 | |||
94 | void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu) | ||
95 | { | ||
96 | #ifdef CONFIG_PPC_BOOK3S_64 | ||
97 | memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb)); | ||
98 | memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu, | ||
99 | sizeof(get_paca()->shadow_vcpu)); | ||
100 | to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max; | ||
101 | #endif | ||
102 | |||
103 | kvmppc_giveup_ext(vcpu, MSR_FP); | ||
104 | kvmppc_giveup_ext(vcpu, MSR_VEC); | ||
105 | kvmppc_giveup_ext(vcpu, MSR_VSX); | ||
106 | } | ||
107 | |||
108 | static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu) | ||
109 | { | ||
110 | ulong smsr = vcpu->arch.shared->msr; | ||
111 | |||
112 | /* Guest MSR values */ | ||
113 | smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE; | ||
114 | /* Process MSR values */ | ||
115 | smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE; | ||
116 | /* External providers the guest reserved */ | ||
117 | smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext); | ||
118 | /* 64-bit Process MSR values */ | ||
119 | #ifdef CONFIG_PPC_BOOK3S_64 | ||
120 | smsr |= MSR_ISF | MSR_HV; | ||
121 | #endif | ||
122 | vcpu->arch.shadow_msr = smsr; | ||
123 | } | ||
124 | |||
125 | void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr) | ||
126 | { | ||
127 | ulong old_msr = vcpu->arch.shared->msr; | ||
128 | |||
129 | #ifdef EXIT_DEBUG | ||
130 | printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr); | ||
131 | #endif | ||
132 | |||
133 | msr &= to_book3s(vcpu)->msr_mask; | ||
134 | vcpu->arch.shared->msr = msr; | ||
135 | kvmppc_recalc_shadow_msr(vcpu); | ||
136 | |||
137 | if (msr & MSR_POW) { | ||
138 | if (!vcpu->arch.pending_exceptions) { | ||
139 | kvm_vcpu_block(vcpu); | ||
140 | vcpu->stat.halt_wakeup++; | ||
141 | |||
142 | /* Unset POW bit after we woke up */ | ||
143 | msr &= ~MSR_POW; | ||
144 | vcpu->arch.shared->msr = msr; | ||
145 | } | ||
146 | } | ||
147 | |||
148 | if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) != | ||
149 | (old_msr & (MSR_PR|MSR_IR|MSR_DR))) { | ||
150 | kvmppc_mmu_flush_segments(vcpu); | ||
151 | kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)); | ||
152 | |||
153 | /* Preload magic page segment when in kernel mode */ | ||
154 | if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) { | ||
155 | struct kvm_vcpu_arch *a = &vcpu->arch; | ||
156 | |||
157 | if (msr & MSR_DR) | ||
158 | kvmppc_mmu_map_segment(vcpu, a->magic_page_ea); | ||
159 | else | ||
160 | kvmppc_mmu_map_segment(vcpu, a->magic_page_pa); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | /* Preload FPU if it's enabled */ | ||
165 | if (vcpu->arch.shared->msr & MSR_FP) | ||
166 | kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP); | ||
167 | } | ||
168 | |||
169 | void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags) | 71 | void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags) |
170 | { | 72 | { |
171 | vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu); | 73 | vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu); |
172 | vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags; | 74 | vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags; |
173 | kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec); | 75 | kvmppc_set_pc(vcpu, kvmppc_interrupt_offset(vcpu) + vec); |
174 | vcpu->arch.mmu.reset_msr(vcpu); | 76 | vcpu->arch.mmu.reset_msr(vcpu); |
175 | } | 77 | } |
176 | 78 | ||
@@ -204,11 +106,13 @@ static int kvmppc_book3s_vec2irqprio(unsigned int vec) | |||
204 | static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu, | 106 | static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu, |
205 | unsigned int vec) | 107 | unsigned int vec) |
206 | { | 108 | { |
109 | unsigned long old_pending = vcpu->arch.pending_exceptions; | ||
110 | |||
207 | clear_bit(kvmppc_book3s_vec2irqprio(vec), | 111 | clear_bit(kvmppc_book3s_vec2irqprio(vec), |
208 | &vcpu->arch.pending_exceptions); | 112 | &vcpu->arch.pending_exceptions); |
209 | 113 | ||
210 | if (!vcpu->arch.pending_exceptions) | 114 | kvmppc_update_int_pending(vcpu, vcpu->arch.pending_exceptions, |
211 | vcpu->arch.shared->int_pending = 0; | 115 | old_pending); |
212 | } | 116 | } |
213 | 117 | ||
214 | void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec) | 118 | void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec) |
@@ -225,8 +129,8 @@ void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec) | |||
225 | 129 | ||
226 | void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags) | 130 | void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags) |
227 | { | 131 | { |
228 | to_book3s(vcpu)->prog_flags = flags; | 132 | /* might as well deliver this straight away */ |
229 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM); | 133 | kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_PROGRAM, flags); |
230 | } | 134 | } |
231 | 135 | ||
232 | void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu) | 136 | void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu) |
@@ -266,21 +170,7 @@ int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority) | |||
266 | { | 170 | { |
267 | int deliver = 1; | 171 | int deliver = 1; |
268 | int vec = 0; | 172 | int vec = 0; |
269 | ulong flags = 0ULL; | 173 | bool crit = kvmppc_critical_section(vcpu); |
270 | ulong crit_raw = vcpu->arch.shared->critical; | ||
271 | ulong crit_r1 = kvmppc_get_gpr(vcpu, 1); | ||
272 | bool crit; | ||
273 | |||
274 | /* Truncate crit indicators in 32 bit mode */ | ||
275 | if (!(vcpu->arch.shared->msr & MSR_SF)) { | ||
276 | crit_raw &= 0xffffffff; | ||
277 | crit_r1 &= 0xffffffff; | ||
278 | } | ||
279 | |||
280 | /* Critical section when crit == r1 */ | ||
281 | crit = (crit_raw == crit_r1); | ||
282 | /* ... and we're in supervisor mode */ | ||
283 | crit = crit && !(vcpu->arch.shared->msr & MSR_PR); | ||
284 | 174 | ||
285 | switch (priority) { | 175 | switch (priority) { |
286 | case BOOK3S_IRQPRIO_DECREMENTER: | 176 | case BOOK3S_IRQPRIO_DECREMENTER: |
@@ -315,7 +205,6 @@ int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority) | |||
315 | break; | 205 | break; |
316 | case BOOK3S_IRQPRIO_PROGRAM: | 206 | case BOOK3S_IRQPRIO_PROGRAM: |
317 | vec = BOOK3S_INTERRUPT_PROGRAM; | 207 | vec = BOOK3S_INTERRUPT_PROGRAM; |
318 | flags = to_book3s(vcpu)->prog_flags; | ||
319 | break; | 208 | break; |
320 | case BOOK3S_IRQPRIO_VSX: | 209 | case BOOK3S_IRQPRIO_VSX: |
321 | vec = BOOK3S_INTERRUPT_VSX; | 210 | vec = BOOK3S_INTERRUPT_VSX; |
@@ -346,7 +235,7 @@ int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority) | |||
346 | #endif | 235 | #endif |
347 | 236 | ||
348 | if (deliver) | 237 | if (deliver) |
349 | kvmppc_inject_interrupt(vcpu, vec, flags); | 238 | kvmppc_inject_interrupt(vcpu, vec, 0); |
350 | 239 | ||
351 | return deliver; | 240 | return deliver; |
352 | } | 241 | } |
@@ -392,64 +281,7 @@ void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu) | |||
392 | } | 281 | } |
393 | 282 | ||
394 | /* Tell the guest about our interrupt status */ | 283 | /* Tell the guest about our interrupt status */ |
395 | if (*pending) | 284 | kvmppc_update_int_pending(vcpu, *pending, old_pending); |
396 | vcpu->arch.shared->int_pending = 1; | ||
397 | else if (old_pending) | ||
398 | vcpu->arch.shared->int_pending = 0; | ||
399 | } | ||
400 | |||
401 | void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr) | ||
402 | { | ||
403 | u32 host_pvr; | ||
404 | |||
405 | vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB; | ||
406 | vcpu->arch.pvr = pvr; | ||
407 | #ifdef CONFIG_PPC_BOOK3S_64 | ||
408 | if ((pvr >= 0x330000) && (pvr < 0x70330000)) { | ||
409 | kvmppc_mmu_book3s_64_init(vcpu); | ||
410 | to_book3s(vcpu)->hior = 0xfff00000; | ||
411 | to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL; | ||
412 | } else | ||
413 | #endif | ||
414 | { | ||
415 | kvmppc_mmu_book3s_32_init(vcpu); | ||
416 | to_book3s(vcpu)->hior = 0; | ||
417 | to_book3s(vcpu)->msr_mask = 0xffffffffULL; | ||
418 | } | ||
419 | |||
420 | /* If we are in hypervisor level on 970, we can tell the CPU to | ||
421 | * treat DCBZ as 32 bytes store */ | ||
422 | vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32; | ||
423 | if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) && | ||
424 | !strcmp(cur_cpu_spec->platform, "ppc970")) | ||
425 | vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32; | ||
426 | |||
427 | /* Cell performs badly if MSR_FEx are set. So let's hope nobody | ||
428 | really needs them in a VM on Cell and force disable them. */ | ||
429 | if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be")) | ||
430 | to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1); | ||
431 | |||
432 | #ifdef CONFIG_PPC_BOOK3S_32 | ||
433 | /* 32 bit Book3S always has 32 byte dcbz */ | ||
434 | vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32; | ||
435 | #endif | ||
436 | |||
437 | /* On some CPUs we can execute paired single operations natively */ | ||
438 | asm ( "mfpvr %0" : "=r"(host_pvr)); | ||
439 | switch (host_pvr) { | ||
440 | case 0x00080200: /* lonestar 2.0 */ | ||
441 | case 0x00088202: /* lonestar 2.2 */ | ||
442 | case 0x70000100: /* gekko 1.0 */ | ||
443 | case 0x00080100: /* gekko 2.0 */ | ||
444 | case 0x00083203: /* gekko 2.3a */ | ||
445 | case 0x00083213: /* gekko 2.3b */ | ||
446 | case 0x00083204: /* gekko 2.4 */ | ||
447 | case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */ | ||
448 | case 0x00087200: /* broadway */ | ||
449 | vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS; | ||
450 | /* Enable HID2.PSE - in case we need it later */ | ||
451 | mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29)); | ||
452 | } | ||
453 | } | 285 | } |
454 | 286 | ||
455 | pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) | 287 | pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
@@ -471,44 +303,6 @@ pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |||
471 | return gfn_to_pfn(vcpu->kvm, gfn); | 303 | return gfn_to_pfn(vcpu->kvm, gfn); |
472 | } | 304 | } |
473 | 305 | ||
474 | /* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To | ||
475 | * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to | ||
476 | * emulate 32 bytes dcbz length. | ||
477 | * | ||
478 | * The Book3s_64 inventors also realized this case and implemented a special bit | ||
479 | * in the HID5 register, which is a hypervisor ressource. Thus we can't use it. | ||
480 | * | ||
481 | * My approach here is to patch the dcbz instruction on executing pages. | ||
482 | */ | ||
483 | static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte) | ||
484 | { | ||
485 | struct page *hpage; | ||
486 | u64 hpage_offset; | ||
487 | u32 *page; | ||
488 | int i; | ||
489 | |||
490 | hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT); | ||
491 | if (is_error_page(hpage)) { | ||
492 | kvm_release_page_clean(hpage); | ||
493 | return; | ||
494 | } | ||
495 | |||
496 | hpage_offset = pte->raddr & ~PAGE_MASK; | ||
497 | hpage_offset &= ~0xFFFULL; | ||
498 | hpage_offset /= 4; | ||
499 | |||
500 | get_page(hpage); | ||
501 | page = kmap_atomic(hpage, KM_USER0); | ||
502 | |||
503 | /* patch dcbz into reserved instruction, so we trap */ | ||
504 | for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++) | ||
505 | if ((page[i] & 0xff0007ff) == INS_DCBZ) | ||
506 | page[i] &= 0xfffffff7; | ||
507 | |||
508 | kunmap_atomic(page, KM_USER0); | ||
509 | put_page(hpage); | ||
510 | } | ||
511 | |||
512 | static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data, | 306 | static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data, |
513 | struct kvmppc_pte *pte) | 307 | struct kvmppc_pte *pte) |
514 | { | 308 | { |
@@ -606,519 +400,6 @@ mmio: | |||
606 | return EMULATE_DO_MMIO; | 400 | return EMULATE_DO_MMIO; |
607 | } | 401 | } |
608 | 402 | ||
609 | static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | ||
610 | { | ||
611 | ulong mp_pa = vcpu->arch.magic_page_pa; | ||
612 | |||
613 | if (unlikely(mp_pa) && | ||
614 | unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) { | ||
615 | return 1; | ||
616 | } | ||
617 | |||
618 | return kvm_is_visible_gfn(vcpu->kvm, gfn); | ||
619 | } | ||
620 | |||
621 | int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, | ||
622 | ulong eaddr, int vec) | ||
623 | { | ||
624 | bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE); | ||
625 | int r = RESUME_GUEST; | ||
626 | int relocated; | ||
627 | int page_found = 0; | ||
628 | struct kvmppc_pte pte; | ||
629 | bool is_mmio = false; | ||
630 | bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false; | ||
631 | bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false; | ||
632 | u64 vsid; | ||
633 | |||
634 | relocated = data ? dr : ir; | ||
635 | |||
636 | /* Resolve real address if translation turned on */ | ||
637 | if (relocated) { | ||
638 | page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data); | ||
639 | } else { | ||
640 | pte.may_execute = true; | ||
641 | pte.may_read = true; | ||
642 | pte.may_write = true; | ||
643 | pte.raddr = eaddr & KVM_PAM; | ||
644 | pte.eaddr = eaddr; | ||
645 | pte.vpage = eaddr >> 12; | ||
646 | } | ||
647 | |||
648 | switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) { | ||
649 | case 0: | ||
650 | pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12)); | ||
651 | break; | ||
652 | case MSR_DR: | ||
653 | case MSR_IR: | ||
654 | vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid); | ||
655 | |||
656 | if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR) | ||
657 | pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12)); | ||
658 | else | ||
659 | pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12)); | ||
660 | pte.vpage |= vsid; | ||
661 | |||
662 | if (vsid == -1) | ||
663 | page_found = -EINVAL; | ||
664 | break; | ||
665 | } | ||
666 | |||
667 | if (vcpu->arch.mmu.is_dcbz32(vcpu) && | ||
668 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) { | ||
669 | /* | ||
670 | * If we do the dcbz hack, we have to NX on every execution, | ||
671 | * so we can patch the executing code. This renders our guest | ||
672 | * NX-less. | ||
673 | */ | ||
674 | pte.may_execute = !data; | ||
675 | } | ||
676 | |||
677 | if (page_found == -ENOENT) { | ||
678 | /* Page not found in guest PTE entries */ | ||
679 | vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu); | ||
680 | vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr; | ||
681 | vcpu->arch.shared->msr |= | ||
682 | (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL); | ||
683 | kvmppc_book3s_queue_irqprio(vcpu, vec); | ||
684 | } else if (page_found == -EPERM) { | ||
685 | /* Storage protection */ | ||
686 | vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu); | ||
687 | vcpu->arch.shared->dsisr = | ||
688 | to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE; | ||
689 | vcpu->arch.shared->dsisr |= DSISR_PROTFAULT; | ||
690 | vcpu->arch.shared->msr |= | ||
691 | (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL); | ||
692 | kvmppc_book3s_queue_irqprio(vcpu, vec); | ||
693 | } else if (page_found == -EINVAL) { | ||
694 | /* Page not found in guest SLB */ | ||
695 | vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu); | ||
696 | kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80); | ||
697 | } else if (!is_mmio && | ||
698 | kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) { | ||
699 | /* The guest's PTE is not mapped yet. Map on the host */ | ||
700 | kvmppc_mmu_map_page(vcpu, &pte); | ||
701 | if (data) | ||
702 | vcpu->stat.sp_storage++; | ||
703 | else if (vcpu->arch.mmu.is_dcbz32(vcpu) && | ||
704 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) | ||
705 | kvmppc_patch_dcbz(vcpu, &pte); | ||
706 | } else { | ||
707 | /* MMIO */ | ||
708 | vcpu->stat.mmio_exits++; | ||
709 | vcpu->arch.paddr_accessed = pte.raddr; | ||
710 | r = kvmppc_emulate_mmio(run, vcpu); | ||
711 | if ( r == RESUME_HOST_NV ) | ||
712 | r = RESUME_HOST; | ||
713 | } | ||
714 | |||
715 | return r; | ||
716 | } | ||
717 | |||
718 | static inline int get_fpr_index(int i) | ||
719 | { | ||
720 | #ifdef CONFIG_VSX | ||
721 | i *= 2; | ||
722 | #endif | ||
723 | return i; | ||
724 | } | ||
725 | |||
726 | /* Give up external provider (FPU, Altivec, VSX) */ | ||
727 | void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr) | ||
728 | { | ||
729 | struct thread_struct *t = ¤t->thread; | ||
730 | u64 *vcpu_fpr = vcpu->arch.fpr; | ||
731 | #ifdef CONFIG_VSX | ||
732 | u64 *vcpu_vsx = vcpu->arch.vsr; | ||
733 | #endif | ||
734 | u64 *thread_fpr = (u64*)t->fpr; | ||
735 | int i; | ||
736 | |||
737 | if (!(vcpu->arch.guest_owned_ext & msr)) | ||
738 | return; | ||
739 | |||
740 | #ifdef DEBUG_EXT | ||
741 | printk(KERN_INFO "Giving up ext 0x%lx\n", msr); | ||
742 | #endif | ||
743 | |||
744 | switch (msr) { | ||
745 | case MSR_FP: | ||
746 | giveup_fpu(current); | ||
747 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++) | ||
748 | vcpu_fpr[i] = thread_fpr[get_fpr_index(i)]; | ||
749 | |||
750 | vcpu->arch.fpscr = t->fpscr.val; | ||
751 | break; | ||
752 | case MSR_VEC: | ||
753 | #ifdef CONFIG_ALTIVEC | ||
754 | giveup_altivec(current); | ||
755 | memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr)); | ||
756 | vcpu->arch.vscr = t->vscr; | ||
757 | #endif | ||
758 | break; | ||
759 | case MSR_VSX: | ||
760 | #ifdef CONFIG_VSX | ||
761 | __giveup_vsx(current); | ||
762 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++) | ||
763 | vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1]; | ||
764 | #endif | ||
765 | break; | ||
766 | default: | ||
767 | BUG(); | ||
768 | } | ||
769 | |||
770 | vcpu->arch.guest_owned_ext &= ~msr; | ||
771 | current->thread.regs->msr &= ~msr; | ||
772 | kvmppc_recalc_shadow_msr(vcpu); | ||
773 | } | ||
774 | |||
775 | static int kvmppc_read_inst(struct kvm_vcpu *vcpu) | ||
776 | { | ||
777 | ulong srr0 = kvmppc_get_pc(vcpu); | ||
778 | u32 last_inst = kvmppc_get_last_inst(vcpu); | ||
779 | int ret; | ||
780 | |||
781 | ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false); | ||
782 | if (ret == -ENOENT) { | ||
783 | ulong msr = vcpu->arch.shared->msr; | ||
784 | |||
785 | msr = kvmppc_set_field(msr, 33, 33, 1); | ||
786 | msr = kvmppc_set_field(msr, 34, 36, 0); | ||
787 | vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0); | ||
788 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE); | ||
789 | return EMULATE_AGAIN; | ||
790 | } | ||
791 | |||
792 | return EMULATE_DONE; | ||
793 | } | ||
794 | |||
795 | static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr) | ||
796 | { | ||
797 | |||
798 | /* Need to do paired single emulation? */ | ||
799 | if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)) | ||
800 | return EMULATE_DONE; | ||
801 | |||
802 | /* Read out the instruction */ | ||
803 | if (kvmppc_read_inst(vcpu) == EMULATE_DONE) | ||
804 | /* Need to emulate */ | ||
805 | return EMULATE_FAIL; | ||
806 | |||
807 | return EMULATE_AGAIN; | ||
808 | } | ||
809 | |||
810 | /* Handle external providers (FPU, Altivec, VSX) */ | ||
811 | static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr, | ||
812 | ulong msr) | ||
813 | { | ||
814 | struct thread_struct *t = ¤t->thread; | ||
815 | u64 *vcpu_fpr = vcpu->arch.fpr; | ||
816 | #ifdef CONFIG_VSX | ||
817 | u64 *vcpu_vsx = vcpu->arch.vsr; | ||
818 | #endif | ||
819 | u64 *thread_fpr = (u64*)t->fpr; | ||
820 | int i; | ||
821 | |||
822 | /* When we have paired singles, we emulate in software */ | ||
823 | if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE) | ||
824 | return RESUME_GUEST; | ||
825 | |||
826 | if (!(vcpu->arch.shared->msr & msr)) { | ||
827 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); | ||
828 | return RESUME_GUEST; | ||
829 | } | ||
830 | |||
831 | /* We already own the ext */ | ||
832 | if (vcpu->arch.guest_owned_ext & msr) { | ||
833 | return RESUME_GUEST; | ||
834 | } | ||
835 | |||
836 | #ifdef DEBUG_EXT | ||
837 | printk(KERN_INFO "Loading up ext 0x%lx\n", msr); | ||
838 | #endif | ||
839 | |||
840 | current->thread.regs->msr |= msr; | ||
841 | |||
842 | switch (msr) { | ||
843 | case MSR_FP: | ||
844 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++) | ||
845 | thread_fpr[get_fpr_index(i)] = vcpu_fpr[i]; | ||
846 | |||
847 | t->fpscr.val = vcpu->arch.fpscr; | ||
848 | t->fpexc_mode = 0; | ||
849 | kvmppc_load_up_fpu(); | ||
850 | break; | ||
851 | case MSR_VEC: | ||
852 | #ifdef CONFIG_ALTIVEC | ||
853 | memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr)); | ||
854 | t->vscr = vcpu->arch.vscr; | ||
855 | t->vrsave = -1; | ||
856 | kvmppc_load_up_altivec(); | ||
857 | #endif | ||
858 | break; | ||
859 | case MSR_VSX: | ||
860 | #ifdef CONFIG_VSX | ||
861 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++) | ||
862 | thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i]; | ||
863 | kvmppc_load_up_vsx(); | ||
864 | #endif | ||
865 | break; | ||
866 | default: | ||
867 | BUG(); | ||
868 | } | ||
869 | |||
870 | vcpu->arch.guest_owned_ext |= msr; | ||
871 | |||
872 | kvmppc_recalc_shadow_msr(vcpu); | ||
873 | |||
874 | return RESUME_GUEST; | ||
875 | } | ||
876 | |||
877 | int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, | ||
878 | unsigned int exit_nr) | ||
879 | { | ||
880 | int r = RESUME_HOST; | ||
881 | |||
882 | vcpu->stat.sum_exits++; | ||
883 | |||
884 | run->exit_reason = KVM_EXIT_UNKNOWN; | ||
885 | run->ready_for_interrupt_injection = 1; | ||
886 | |||
887 | trace_kvm_book3s_exit(exit_nr, vcpu); | ||
888 | kvm_resched(vcpu); | ||
889 | switch (exit_nr) { | ||
890 | case BOOK3S_INTERRUPT_INST_STORAGE: | ||
891 | vcpu->stat.pf_instruc++; | ||
892 | |||
893 | #ifdef CONFIG_PPC_BOOK3S_32 | ||
894 | /* We set segments as unused segments when invalidating them. So | ||
895 | * treat the respective fault as segment fault. */ | ||
896 | if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] | ||
897 | == SR_INVALID) { | ||
898 | kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)); | ||
899 | r = RESUME_GUEST; | ||
900 | break; | ||
901 | } | ||
902 | #endif | ||
903 | |||
904 | /* only care about PTEG not found errors, but leave NX alone */ | ||
905 | if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) { | ||
906 | r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr); | ||
907 | vcpu->stat.sp_instruc++; | ||
908 | } else if (vcpu->arch.mmu.is_dcbz32(vcpu) && | ||
909 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) { | ||
910 | /* | ||
911 | * XXX If we do the dcbz hack we use the NX bit to flush&patch the page, | ||
912 | * so we can't use the NX bit inside the guest. Let's cross our fingers, | ||
913 | * that no guest that needs the dcbz hack does NX. | ||
914 | */ | ||
915 | kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL); | ||
916 | r = RESUME_GUEST; | ||
917 | } else { | ||
918 | vcpu->arch.shared->msr |= | ||
919 | to_svcpu(vcpu)->shadow_srr1 & 0x58000000; | ||
920 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); | ||
921 | r = RESUME_GUEST; | ||
922 | } | ||
923 | break; | ||
924 | case BOOK3S_INTERRUPT_DATA_STORAGE: | ||
925 | { | ||
926 | ulong dar = kvmppc_get_fault_dar(vcpu); | ||
927 | vcpu->stat.pf_storage++; | ||
928 | |||
929 | #ifdef CONFIG_PPC_BOOK3S_32 | ||
930 | /* We set segments as unused segments when invalidating them. So | ||
931 | * treat the respective fault as segment fault. */ | ||
932 | if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) { | ||
933 | kvmppc_mmu_map_segment(vcpu, dar); | ||
934 | r = RESUME_GUEST; | ||
935 | break; | ||
936 | } | ||
937 | #endif | ||
938 | |||
939 | /* The only case we need to handle is missing shadow PTEs */ | ||
940 | if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) { | ||
941 | r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr); | ||
942 | } else { | ||
943 | vcpu->arch.shared->dar = dar; | ||
944 | vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr; | ||
945 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); | ||
946 | r = RESUME_GUEST; | ||
947 | } | ||
948 | break; | ||
949 | } | ||
950 | case BOOK3S_INTERRUPT_DATA_SEGMENT: | ||
951 | if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) { | ||
952 | vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu); | ||
953 | kvmppc_book3s_queue_irqprio(vcpu, | ||
954 | BOOK3S_INTERRUPT_DATA_SEGMENT); | ||
955 | } | ||
956 | r = RESUME_GUEST; | ||
957 | break; | ||
958 | case BOOK3S_INTERRUPT_INST_SEGMENT: | ||
959 | if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) { | ||
960 | kvmppc_book3s_queue_irqprio(vcpu, | ||
961 | BOOK3S_INTERRUPT_INST_SEGMENT); | ||
962 | } | ||
963 | r = RESUME_GUEST; | ||
964 | break; | ||
965 | /* We're good on these - the host merely wanted to get our attention */ | ||
966 | case BOOK3S_INTERRUPT_DECREMENTER: | ||
967 | vcpu->stat.dec_exits++; | ||
968 | r = RESUME_GUEST; | ||
969 | break; | ||
970 | case BOOK3S_INTERRUPT_EXTERNAL: | ||
971 | vcpu->stat.ext_intr_exits++; | ||
972 | r = RESUME_GUEST; | ||
973 | break; | ||
974 | case BOOK3S_INTERRUPT_PERFMON: | ||
975 | r = RESUME_GUEST; | ||
976 | break; | ||
977 | case BOOK3S_INTERRUPT_PROGRAM: | ||
978 | { | ||
979 | enum emulation_result er; | ||
980 | ulong flags; | ||
981 | |||
982 | program_interrupt: | ||
983 | flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull; | ||
984 | |||
985 | if (vcpu->arch.shared->msr & MSR_PR) { | ||
986 | #ifdef EXIT_DEBUG | ||
987 | printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu)); | ||
988 | #endif | ||
989 | if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) != | ||
990 | (INS_DCBZ & 0xfffffff7)) { | ||
991 | kvmppc_core_queue_program(vcpu, flags); | ||
992 | r = RESUME_GUEST; | ||
993 | break; | ||
994 | } | ||
995 | } | ||
996 | |||
997 | vcpu->stat.emulated_inst_exits++; | ||
998 | er = kvmppc_emulate_instruction(run, vcpu); | ||
999 | switch (er) { | ||
1000 | case EMULATE_DONE: | ||
1001 | r = RESUME_GUEST_NV; | ||
1002 | break; | ||
1003 | case EMULATE_AGAIN: | ||
1004 | r = RESUME_GUEST; | ||
1005 | break; | ||
1006 | case EMULATE_FAIL: | ||
1007 | printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n", | ||
1008 | __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu)); | ||
1009 | kvmppc_core_queue_program(vcpu, flags); | ||
1010 | r = RESUME_GUEST; | ||
1011 | break; | ||
1012 | case EMULATE_DO_MMIO: | ||
1013 | run->exit_reason = KVM_EXIT_MMIO; | ||
1014 | r = RESUME_HOST_NV; | ||
1015 | break; | ||
1016 | default: | ||
1017 | BUG(); | ||
1018 | } | ||
1019 | break; | ||
1020 | } | ||
1021 | case BOOK3S_INTERRUPT_SYSCALL: | ||
1022 | if (vcpu->arch.osi_enabled && | ||
1023 | (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) && | ||
1024 | (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) { | ||
1025 | /* MOL hypercalls */ | ||
1026 | u64 *gprs = run->osi.gprs; | ||
1027 | int i; | ||
1028 | |||
1029 | run->exit_reason = KVM_EXIT_OSI; | ||
1030 | for (i = 0; i < 32; i++) | ||
1031 | gprs[i] = kvmppc_get_gpr(vcpu, i); | ||
1032 | vcpu->arch.osi_needed = 1; | ||
1033 | r = RESUME_HOST_NV; | ||
1034 | } else if (!(vcpu->arch.shared->msr & MSR_PR) && | ||
1035 | (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) { | ||
1036 | /* KVM PV hypercalls */ | ||
1037 | kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); | ||
1038 | r = RESUME_GUEST; | ||
1039 | } else { | ||
1040 | /* Guest syscalls */ | ||
1041 | vcpu->stat.syscall_exits++; | ||
1042 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); | ||
1043 | r = RESUME_GUEST; | ||
1044 | } | ||
1045 | break; | ||
1046 | case BOOK3S_INTERRUPT_FP_UNAVAIL: | ||
1047 | case BOOK3S_INTERRUPT_ALTIVEC: | ||
1048 | case BOOK3S_INTERRUPT_VSX: | ||
1049 | { | ||
1050 | int ext_msr = 0; | ||
1051 | |||
1052 | switch (exit_nr) { | ||
1053 | case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break; | ||
1054 | case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break; | ||
1055 | case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break; | ||
1056 | } | ||
1057 | |||
1058 | switch (kvmppc_check_ext(vcpu, exit_nr)) { | ||
1059 | case EMULATE_DONE: | ||
1060 | /* everything ok - let's enable the ext */ | ||
1061 | r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr); | ||
1062 | break; | ||
1063 | case EMULATE_FAIL: | ||
1064 | /* we need to emulate this instruction */ | ||
1065 | goto program_interrupt; | ||
1066 | break; | ||
1067 | default: | ||
1068 | /* nothing to worry about - go again */ | ||
1069 | break; | ||
1070 | } | ||
1071 | break; | ||
1072 | } | ||
1073 | case BOOK3S_INTERRUPT_ALIGNMENT: | ||
1074 | if (kvmppc_read_inst(vcpu) == EMULATE_DONE) { | ||
1075 | vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu, | ||
1076 | kvmppc_get_last_inst(vcpu)); | ||
1077 | vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu, | ||
1078 | kvmppc_get_last_inst(vcpu)); | ||
1079 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); | ||
1080 | } | ||
1081 | r = RESUME_GUEST; | ||
1082 | break; | ||
1083 | case BOOK3S_INTERRUPT_MACHINE_CHECK: | ||
1084 | case BOOK3S_INTERRUPT_TRACE: | ||
1085 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); | ||
1086 | r = RESUME_GUEST; | ||
1087 | break; | ||
1088 | default: | ||
1089 | /* Ugh - bork here! What did we get? */ | ||
1090 | printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n", | ||
1091 | exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1); | ||
1092 | r = RESUME_HOST; | ||
1093 | BUG(); | ||
1094 | break; | ||
1095 | } | ||
1096 | |||
1097 | |||
1098 | if (!(r & RESUME_HOST)) { | ||
1099 | /* To avoid clobbering exit_reason, only check for signals if | ||
1100 | * we aren't already exiting to userspace for some other | ||
1101 | * reason. */ | ||
1102 | if (signal_pending(current)) { | ||
1103 | #ifdef EXIT_DEBUG | ||
1104 | printk(KERN_EMERG "KVM: Going back to host\n"); | ||
1105 | #endif | ||
1106 | vcpu->stat.signal_exits++; | ||
1107 | run->exit_reason = KVM_EXIT_INTR; | ||
1108 | r = -EINTR; | ||
1109 | } else { | ||
1110 | /* In case an interrupt came in that was triggered | ||
1111 | * from userspace (like DEC), we need to check what | ||
1112 | * to inject now! */ | ||
1113 | kvmppc_core_deliver_interrupts(vcpu); | ||
1114 | } | ||
1115 | } | ||
1116 | |||
1117 | trace_kvm_book3s_reenter(r, vcpu); | ||
1118 | |||
1119 | return r; | ||
1120 | } | ||
1121 | |||
1122 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) | 403 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
1123 | { | 404 | { |
1124 | return 0; | 405 | return 0; |
@@ -1179,69 +460,6 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |||
1179 | return 0; | 460 | return 0; |
1180 | } | 461 | } |
1181 | 462 | ||
1182 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | ||
1183 | struct kvm_sregs *sregs) | ||
1184 | { | ||
1185 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); | ||
1186 | int i; | ||
1187 | |||
1188 | sregs->pvr = vcpu->arch.pvr; | ||
1189 | |||
1190 | sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1; | ||
1191 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) { | ||
1192 | for (i = 0; i < 64; i++) { | ||
1193 | sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i; | ||
1194 | sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv; | ||
1195 | } | ||
1196 | } else { | ||
1197 | for (i = 0; i < 16; i++) | ||
1198 | sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i]; | ||
1199 | |||
1200 | for (i = 0; i < 8; i++) { | ||
1201 | sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw; | ||
1202 | sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw; | ||
1203 | } | ||
1204 | } | ||
1205 | |||
1206 | return 0; | ||
1207 | } | ||
1208 | |||
1209 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | ||
1210 | struct kvm_sregs *sregs) | ||
1211 | { | ||
1212 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); | ||
1213 | int i; | ||
1214 | |||
1215 | kvmppc_set_pvr(vcpu, sregs->pvr); | ||
1216 | |||
1217 | vcpu3s->sdr1 = sregs->u.s.sdr1; | ||
1218 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) { | ||
1219 | for (i = 0; i < 64; i++) { | ||
1220 | vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv, | ||
1221 | sregs->u.s.ppc64.slb[i].slbe); | ||
1222 | } | ||
1223 | } else { | ||
1224 | for (i = 0; i < 16; i++) { | ||
1225 | vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]); | ||
1226 | } | ||
1227 | for (i = 0; i < 8; i++) { | ||
1228 | kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false, | ||
1229 | (u32)sregs->u.s.ppc32.ibat[i]); | ||
1230 | kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true, | ||
1231 | (u32)(sregs->u.s.ppc32.ibat[i] >> 32)); | ||
1232 | kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false, | ||
1233 | (u32)sregs->u.s.ppc32.dbat[i]); | ||
1234 | kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true, | ||
1235 | (u32)(sregs->u.s.ppc32.dbat[i] >> 32)); | ||
1236 | } | ||
1237 | } | ||
1238 | |||
1239 | /* Flush the MMU after messing with the segments */ | ||
1240 | kvmppc_mmu_pte_flush(vcpu, 0, 0); | ||
1241 | |||
1242 | return 0; | ||
1243 | } | ||
1244 | |||
1245 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | 463 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
1246 | { | 464 | { |
1247 | return -ENOTSUPP; | 465 | return -ENOTSUPP; |
@@ -1296,202 +514,3 @@ out: | |||
1296 | mutex_unlock(&kvm->slots_lock); | 514 | mutex_unlock(&kvm->slots_lock); |
1297 | return r; | 515 | return r; |
1298 | } | 516 | } |
1299 | |||
1300 | int kvmppc_core_check_processor_compat(void) | ||
1301 | { | ||
1302 | return 0; | ||
1303 | } | ||
1304 | |||
1305 | struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) | ||
1306 | { | ||
1307 | struct kvmppc_vcpu_book3s *vcpu_book3s; | ||
1308 | struct kvm_vcpu *vcpu; | ||
1309 | int err = -ENOMEM; | ||
1310 | unsigned long p; | ||
1311 | |||
1312 | vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s)); | ||
1313 | if (!vcpu_book3s) | ||
1314 | goto out; | ||
1315 | |||
1316 | vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *) | ||
1317 | kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL); | ||
1318 | if (!vcpu_book3s->shadow_vcpu) | ||
1319 | goto free_vcpu; | ||
1320 | |||
1321 | vcpu = &vcpu_book3s->vcpu; | ||
1322 | err = kvm_vcpu_init(vcpu, kvm, id); | ||
1323 | if (err) | ||
1324 | goto free_shadow_vcpu; | ||
1325 | |||
1326 | p = __get_free_page(GFP_KERNEL|__GFP_ZERO); | ||
1327 | /* the real shared page fills the last 4k of our page */ | ||
1328 | vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096); | ||
1329 | if (!p) | ||
1330 | goto uninit_vcpu; | ||
1331 | |||
1332 | vcpu->arch.host_retip = kvm_return_point; | ||
1333 | vcpu->arch.host_msr = mfmsr(); | ||
1334 | #ifdef CONFIG_PPC_BOOK3S_64 | ||
1335 | /* default to book3s_64 (970fx) */ | ||
1336 | vcpu->arch.pvr = 0x3C0301; | ||
1337 | #else | ||
1338 | /* default to book3s_32 (750) */ | ||
1339 | vcpu->arch.pvr = 0x84202; | ||
1340 | #endif | ||
1341 | kvmppc_set_pvr(vcpu, vcpu->arch.pvr); | ||
1342 | vcpu_book3s->slb_nr = 64; | ||
1343 | |||
1344 | /* remember where some real-mode handlers are */ | ||
1345 | vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem; | ||
1346 | vcpu->arch.trampoline_enter = kvmppc_trampoline_enter; | ||
1347 | vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem; | ||
1348 | #ifdef CONFIG_PPC_BOOK3S_64 | ||
1349 | vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall; | ||
1350 | #else | ||
1351 | vcpu->arch.rmcall = (ulong)kvmppc_rmcall; | ||
1352 | #endif | ||
1353 | |||
1354 | vcpu->arch.shadow_msr = MSR_USER64; | ||
1355 | |||
1356 | err = kvmppc_mmu_init(vcpu); | ||
1357 | if (err < 0) | ||
1358 | goto uninit_vcpu; | ||
1359 | |||
1360 | return vcpu; | ||
1361 | |||
1362 | uninit_vcpu: | ||
1363 | kvm_vcpu_uninit(vcpu); | ||
1364 | free_shadow_vcpu: | ||
1365 | kfree(vcpu_book3s->shadow_vcpu); | ||
1366 | free_vcpu: | ||
1367 | vfree(vcpu_book3s); | ||
1368 | out: | ||
1369 | return ERR_PTR(err); | ||
1370 | } | ||
1371 | |||
1372 | void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) | ||
1373 | { | ||
1374 | struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu); | ||
1375 | |||
1376 | free_page((unsigned long)vcpu->arch.shared & PAGE_MASK); | ||
1377 | kvm_vcpu_uninit(vcpu); | ||
1378 | kfree(vcpu_book3s->shadow_vcpu); | ||
1379 | vfree(vcpu_book3s); | ||
1380 | } | ||
1381 | |||
1382 | extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu); | ||
1383 | int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) | ||
1384 | { | ||
1385 | int ret; | ||
1386 | double fpr[32][TS_FPRWIDTH]; | ||
1387 | unsigned int fpscr; | ||
1388 | int fpexc_mode; | ||
1389 | #ifdef CONFIG_ALTIVEC | ||
1390 | vector128 vr[32]; | ||
1391 | vector128 vscr; | ||
1392 | unsigned long uninitialized_var(vrsave); | ||
1393 | int used_vr; | ||
1394 | #endif | ||
1395 | #ifdef CONFIG_VSX | ||
1396 | int used_vsr; | ||
1397 | #endif | ||
1398 | ulong ext_msr; | ||
1399 | |||
1400 | /* No need to go into the guest when all we do is going out */ | ||
1401 | if (signal_pending(current)) { | ||
1402 | kvm_run->exit_reason = KVM_EXIT_INTR; | ||
1403 | return -EINTR; | ||
1404 | } | ||
1405 | |||
1406 | /* Save FPU state in stack */ | ||
1407 | if (current->thread.regs->msr & MSR_FP) | ||
1408 | giveup_fpu(current); | ||
1409 | memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr)); | ||
1410 | fpscr = current->thread.fpscr.val; | ||
1411 | fpexc_mode = current->thread.fpexc_mode; | ||
1412 | |||
1413 | #ifdef CONFIG_ALTIVEC | ||
1414 | /* Save Altivec state in stack */ | ||
1415 | used_vr = current->thread.used_vr; | ||
1416 | if (used_vr) { | ||
1417 | if (current->thread.regs->msr & MSR_VEC) | ||
1418 | giveup_altivec(current); | ||
1419 | memcpy(vr, current->thread.vr, sizeof(current->thread.vr)); | ||
1420 | vscr = current->thread.vscr; | ||
1421 | vrsave = current->thread.vrsave; | ||
1422 | } | ||
1423 | #endif | ||
1424 | |||
1425 | #ifdef CONFIG_VSX | ||
1426 | /* Save VSX state in stack */ | ||
1427 | used_vsr = current->thread.used_vsr; | ||
1428 | if (used_vsr && (current->thread.regs->msr & MSR_VSX)) | ||
1429 | __giveup_vsx(current); | ||
1430 | #endif | ||
1431 | |||
1432 | /* Remember the MSR with disabled extensions */ | ||
1433 | ext_msr = current->thread.regs->msr; | ||
1434 | |||
1435 | /* XXX we get called with irq disabled - change that! */ | ||
1436 | local_irq_enable(); | ||
1437 | |||
1438 | /* Preload FPU if it's enabled */ | ||
1439 | if (vcpu->arch.shared->msr & MSR_FP) | ||
1440 | kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP); | ||
1441 | |||
1442 | ret = __kvmppc_vcpu_entry(kvm_run, vcpu); | ||
1443 | |||
1444 | local_irq_disable(); | ||
1445 | |||
1446 | current->thread.regs->msr = ext_msr; | ||
1447 | |||
1448 | /* Make sure we save the guest FPU/Altivec/VSX state */ | ||
1449 | kvmppc_giveup_ext(vcpu, MSR_FP); | ||
1450 | kvmppc_giveup_ext(vcpu, MSR_VEC); | ||
1451 | kvmppc_giveup_ext(vcpu, MSR_VSX); | ||
1452 | |||
1453 | /* Restore FPU state from stack */ | ||
1454 | memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr)); | ||
1455 | current->thread.fpscr.val = fpscr; | ||
1456 | current->thread.fpexc_mode = fpexc_mode; | ||
1457 | |||
1458 | #ifdef CONFIG_ALTIVEC | ||
1459 | /* Restore Altivec state from stack */ | ||
1460 | if (used_vr && current->thread.used_vr) { | ||
1461 | memcpy(current->thread.vr, vr, sizeof(current->thread.vr)); | ||
1462 | current->thread.vscr = vscr; | ||
1463 | current->thread.vrsave = vrsave; | ||
1464 | } | ||
1465 | current->thread.used_vr = used_vr; | ||
1466 | #endif | ||
1467 | |||
1468 | #ifdef CONFIG_VSX | ||
1469 | current->thread.used_vsr = used_vsr; | ||
1470 | #endif | ||
1471 | |||
1472 | return ret; | ||
1473 | } | ||
1474 | |||
1475 | static int kvmppc_book3s_init(void) | ||
1476 | { | ||
1477 | int r; | ||
1478 | |||
1479 | r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0, | ||
1480 | THIS_MODULE); | ||
1481 | |||
1482 | if (r) | ||
1483 | return r; | ||
1484 | |||
1485 | r = kvmppc_mmu_hpte_sysinit(); | ||
1486 | |||
1487 | return r; | ||
1488 | } | ||
1489 | |||
1490 | static void kvmppc_book3s_exit(void) | ||
1491 | { | ||
1492 | kvmppc_mmu_hpte_sysexit(); | ||
1493 | kvm_exit(); | ||
1494 | } | ||
1495 | |||
1496 | module_init(kvmppc_book3s_init); | ||
1497 | module_exit(kvmppc_book3s_exit); | ||