blob: 71fa0f1873b3c1c6a19dab11bcfc9e5e513d0edb [file] [log] [blame]
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001/*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 * Paul Mackerras <paulus@samba.org>
8 *
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).
13 *
14 * This file is derived from arch/powerpc/kvm/44x.c,
15 * by Hollis Blanchard <hollisb@us.ibm.com>.
16 *
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.
20 */
21
22#include <linux/kvm_host.h>
Paul Gortmaker93087942011-07-29 16:19:31 +100023#include <linux/export.h>
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000024#include <linux/err.h>
25#include <linux/slab.h>
26
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>
Benjamin Herrenschmidt95327d02012-04-01 17:35:53 +000036#include <asm/switch_to.h>
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000037#include <linux/gfp.h>
38#include <linux/sched.h>
39#include <linux/vmalloc.h>
40#include <linux/highmem.h>
41
42#include "trace.h"
43
44/* #define EXIT_DEBUG */
45/* #define DEBUG_EXT */
46
47static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
48 ulong msr);
49
50/* Some compatibility defines */
51#ifdef CONFIG_PPC_BOOK3S_32
52#define MSR_USER32 MSR_USER
53#define MSR_USER64 MSR_USER
54#define HW_PAGE_SIZE PAGE_SIZE
55#endif
56
57void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
58{
59#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf468a12c2011-12-09 14:44:13 +010060 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
61 memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb));
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000062 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
63 sizeof(get_paca()->shadow_vcpu));
Alexander Graf468a12c2011-12-09 14:44:13 +010064 svcpu->slb_max = to_book3s(vcpu)->slb_shadow_max;
65 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000066#endif
67
68#ifdef CONFIG_PPC_BOOK3S_32
69 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
70#endif
71}
72
73void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
74{
75#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf468a12c2011-12-09 14:44:13 +010076 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
77 memcpy(to_book3s(vcpu)->slb_shadow, svcpu->slb, sizeof(svcpu->slb));
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000078 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
79 sizeof(get_paca()->shadow_vcpu));
Alexander Graf468a12c2011-12-09 14:44:13 +010080 to_book3s(vcpu)->slb_shadow_max = svcpu->slb_max;
81 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000082#endif
83
84 kvmppc_giveup_ext(vcpu, MSR_FP);
85 kvmppc_giveup_ext(vcpu, MSR_VEC);
86 kvmppc_giveup_ext(vcpu, MSR_VSX);
87}
88
Alexander Graf03d25c52012-08-10 12:28:50 +020089void kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
90{
Alexander Graf9b0cb3c2012-08-10 13:23:55 +020091 /* We misuse TLB_FLUSH to indicate that we want to clear
92 all shadow cache entries */
93 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
94 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf03d25c52012-08-10 12:28:50 +020095}
96
Alexander Graf9b0cb3c2012-08-10 13:23:55 +020097/************* MMU Notifiers *************/
98
99int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
100{
101 trace_kvm_unmap_hva(hva);
102
103 /*
104 * Flush all shadow tlb entries everywhere. This is slow, but
105 * we are 100% sure that we catch the to be unmapped page
106 */
107 kvm_flush_remote_tlbs(kvm);
108
109 return 0;
110}
111
112int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
113{
114 /* kvm_unmap_hva flushes everything anyways */
115 kvm_unmap_hva(kvm, start);
116
117 return 0;
118}
119
120int kvm_age_hva(struct kvm *kvm, unsigned long hva)
121{
122 /* XXX could be more clever ;) */
123 return 0;
124}
125
126int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
127{
128 /* XXX could be more clever ;) */
129 return 0;
130}
131
132void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
133{
134 /* The page will get remapped properly on its next fault */
135 kvm_unmap_hva(kvm, hva);
136}
137
138/*****************************************/
139
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000140static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
141{
142 ulong smsr = vcpu->arch.shared->msr;
143
144 /* Guest MSR values */
145 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
146 /* Process MSR values */
147 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
148 /* External providers the guest reserved */
149 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
150 /* 64-bit Process MSR values */
151#ifdef CONFIG_PPC_BOOK3S_64
152 smsr |= MSR_ISF | MSR_HV;
153#endif
154 vcpu->arch.shadow_msr = smsr;
155}
156
157void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
158{
159 ulong old_msr = vcpu->arch.shared->msr;
160
161#ifdef EXIT_DEBUG
162 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
163#endif
164
165 msr &= to_book3s(vcpu)->msr_mask;
166 vcpu->arch.shared->msr = msr;
167 kvmppc_recalc_shadow_msr(vcpu);
168
169 if (msr & MSR_POW) {
170 if (!vcpu->arch.pending_exceptions) {
171 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100172 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000173 vcpu->stat.halt_wakeup++;
174
175 /* Unset POW bit after we woke up */
176 msr &= ~MSR_POW;
177 vcpu->arch.shared->msr = msr;
178 }
179 }
180
181 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
182 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
183 kvmppc_mmu_flush_segments(vcpu);
184 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
185
186 /* Preload magic page segment when in kernel mode */
187 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
188 struct kvm_vcpu_arch *a = &vcpu->arch;
189
190 if (msr & MSR_DR)
191 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
192 else
193 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
194 }
195 }
196
Benjamin Herrenschmidtbbcc9c02012-03-13 21:52:44 +0000197 /*
198 * When switching from 32 to 64-bit, we may have a stale 32-bit
199 * magic page around, we need to flush it. Typically 32-bit magic
200 * page will be instanciated when calling into RTAS. Note: We
201 * assume that such transition only happens while in kernel mode,
202 * ie, we never transition from user 32-bit to kernel 64-bit with
203 * a 32-bit magic page around.
204 */
205 if (vcpu->arch.magic_page_pa &&
206 !(old_msr & MSR_PR) && !(old_msr & MSR_SF) && (msr & MSR_SF)) {
207 /* going from RTAS to normal kernel code */
208 kvmppc_mmu_pte_flush(vcpu, (uint32_t)vcpu->arch.magic_page_pa,
209 ~0xFFFUL);
210 }
211
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000212 /* Preload FPU if it's enabled */
213 if (vcpu->arch.shared->msr & MSR_FP)
214 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
215}
216
217void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
218{
219 u32 host_pvr;
220
221 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
222 vcpu->arch.pvr = pvr;
223#ifdef CONFIG_PPC_BOOK3S_64
224 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
225 kvmppc_mmu_book3s_64_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200226 if (!to_book3s(vcpu)->hior_explicit)
227 to_book3s(vcpu)->hior = 0xfff00000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000228 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200229 vcpu->arch.cpu_type = KVM_CPU_3S_64;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000230 } else
231#endif
232 {
233 kvmppc_mmu_book3s_32_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200234 if (!to_book3s(vcpu)->hior_explicit)
235 to_book3s(vcpu)->hior = 0;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000236 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200237 vcpu->arch.cpu_type = KVM_CPU_3S_32;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000238 }
239
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200240 kvmppc_sanity_check(vcpu);
241
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000242 /* If we are in hypervisor level on 970, we can tell the CPU to
243 * treat DCBZ as 32 bytes store */
244 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
245 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
246 !strcmp(cur_cpu_spec->platform, "ppc970"))
247 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
248
249 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
250 really needs them in a VM on Cell and force disable them. */
251 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
252 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
253
254#ifdef CONFIG_PPC_BOOK3S_32
255 /* 32 bit Book3S always has 32 byte dcbz */
256 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
257#endif
258
259 /* On some CPUs we can execute paired single operations natively */
260 asm ( "mfpvr %0" : "=r"(host_pvr));
261 switch (host_pvr) {
262 case 0x00080200: /* lonestar 2.0 */
263 case 0x00088202: /* lonestar 2.2 */
264 case 0x70000100: /* gekko 1.0 */
265 case 0x00080100: /* gekko 2.0 */
266 case 0x00083203: /* gekko 2.3a */
267 case 0x00083213: /* gekko 2.3b */
268 case 0x00083204: /* gekko 2.4 */
269 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
270 case 0x00087200: /* broadway */
271 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
272 /* Enable HID2.PSE - in case we need it later */
273 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
274 }
275}
276
277/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
278 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
279 * emulate 32 bytes dcbz length.
280 *
281 * The Book3s_64 inventors also realized this case and implemented a special bit
282 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
283 *
284 * My approach here is to patch the dcbz instruction on executing pages.
285 */
286static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
287{
288 struct page *hpage;
289 u64 hpage_offset;
290 u32 *page;
291 int i;
292
293 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800294 if (is_error_page(hpage))
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000295 return;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000296
297 hpage_offset = pte->raddr & ~PAGE_MASK;
298 hpage_offset &= ~0xFFFULL;
299 hpage_offset /= 4;
300
301 get_page(hpage);
Cong Wang2480b202011-11-25 23:14:16 +0800302 page = kmap_atomic(hpage);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000303
304 /* patch dcbz into reserved instruction, so we trap */
305 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
306 if ((page[i] & 0xff0007ff) == INS_DCBZ)
307 page[i] &= 0xfffffff7;
308
Cong Wang2480b202011-11-25 23:14:16 +0800309 kunmap_atomic(page);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000310 put_page(hpage);
311}
312
313static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
314{
315 ulong mp_pa = vcpu->arch.magic_page_pa;
316
Benjamin Herrenschmidtbbcc9c02012-03-13 21:52:44 +0000317 if (!(vcpu->arch.shared->msr & MSR_SF))
318 mp_pa = (uint32_t)mp_pa;
319
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000320 if (unlikely(mp_pa) &&
321 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
322 return 1;
323 }
324
325 return kvm_is_visible_gfn(vcpu->kvm, gfn);
326}
327
328int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
329 ulong eaddr, int vec)
330{
331 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
332 int r = RESUME_GUEST;
333 int relocated;
334 int page_found = 0;
335 struct kvmppc_pte pte;
336 bool is_mmio = false;
337 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
338 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
339 u64 vsid;
340
341 relocated = data ? dr : ir;
342
343 /* Resolve real address if translation turned on */
344 if (relocated) {
345 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
346 } else {
347 pte.may_execute = true;
348 pte.may_read = true;
349 pte.may_write = true;
350 pte.raddr = eaddr & KVM_PAM;
351 pte.eaddr = eaddr;
352 pte.vpage = eaddr >> 12;
353 }
354
355 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
356 case 0:
357 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
358 break;
359 case MSR_DR:
360 case MSR_IR:
361 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
362
363 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
364 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
365 else
366 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
367 pte.vpage |= vsid;
368
369 if (vsid == -1)
370 page_found = -EINVAL;
371 break;
372 }
373
374 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
375 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
376 /*
377 * If we do the dcbz hack, we have to NX on every execution,
378 * so we can patch the executing code. This renders our guest
379 * NX-less.
380 */
381 pte.may_execute = !data;
382 }
383
384 if (page_found == -ENOENT) {
385 /* Page not found in guest PTE entries */
Alexander Graf468a12c2011-12-09 14:44:13 +0100386 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000387 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100388 vcpu->arch.shared->dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000389 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100390 (svcpu->shadow_srr1 & 0x00000000f8000000ULL);
391 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000392 kvmppc_book3s_queue_irqprio(vcpu, vec);
393 } else if (page_found == -EPERM) {
394 /* Storage protection */
Alexander Graf468a12c2011-12-09 14:44:13 +0100395 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000396 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100397 vcpu->arch.shared->dsisr = svcpu->fault_dsisr & ~DSISR_NOHPTE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000398 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
399 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100400 svcpu->shadow_srr1 & 0x00000000f8000000ULL;
401 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000402 kvmppc_book3s_queue_irqprio(vcpu, vec);
403 } else if (page_found == -EINVAL) {
404 /* Page not found in guest SLB */
405 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
406 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
407 } else if (!is_mmio &&
408 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
409 /* The guest's PTE is not mapped yet. Map on the host */
410 kvmppc_mmu_map_page(vcpu, &pte);
411 if (data)
412 vcpu->stat.sp_storage++;
413 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
414 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
415 kvmppc_patch_dcbz(vcpu, &pte);
416 } else {
417 /* MMIO */
418 vcpu->stat.mmio_exits++;
419 vcpu->arch.paddr_accessed = pte.raddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100420 vcpu->arch.vaddr_accessed = pte.eaddr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000421 r = kvmppc_emulate_mmio(run, vcpu);
422 if ( r == RESUME_HOST_NV )
423 r = RESUME_HOST;
424 }
425
426 return r;
427}
428
429static inline int get_fpr_index(int i)
430{
431#ifdef CONFIG_VSX
432 i *= 2;
433#endif
434 return i;
435}
436
437/* Give up external provider (FPU, Altivec, VSX) */
438void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
439{
440 struct thread_struct *t = &current->thread;
441 u64 *vcpu_fpr = vcpu->arch.fpr;
442#ifdef CONFIG_VSX
443 u64 *vcpu_vsx = vcpu->arch.vsr;
444#endif
445 u64 *thread_fpr = (u64*)t->fpr;
446 int i;
447
448 if (!(vcpu->arch.guest_owned_ext & msr))
449 return;
450
451#ifdef DEBUG_EXT
452 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
453#endif
454
455 switch (msr) {
456 case MSR_FP:
457 giveup_fpu(current);
458 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
459 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
460
461 vcpu->arch.fpscr = t->fpscr.val;
462 break;
463 case MSR_VEC:
464#ifdef CONFIG_ALTIVEC
465 giveup_altivec(current);
466 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
467 vcpu->arch.vscr = t->vscr;
468#endif
469 break;
470 case MSR_VSX:
471#ifdef CONFIG_VSX
472 __giveup_vsx(current);
473 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
474 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
475#endif
476 break;
477 default:
478 BUG();
479 }
480
481 vcpu->arch.guest_owned_ext &= ~msr;
482 current->thread.regs->msr &= ~msr;
483 kvmppc_recalc_shadow_msr(vcpu);
484}
485
486static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
487{
488 ulong srr0 = kvmppc_get_pc(vcpu);
489 u32 last_inst = kvmppc_get_last_inst(vcpu);
490 int ret;
491
492 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
493 if (ret == -ENOENT) {
494 ulong msr = vcpu->arch.shared->msr;
495
496 msr = kvmppc_set_field(msr, 33, 33, 1);
497 msr = kvmppc_set_field(msr, 34, 36, 0);
498 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
499 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
500 return EMULATE_AGAIN;
501 }
502
503 return EMULATE_DONE;
504}
505
506static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
507{
508
509 /* Need to do paired single emulation? */
510 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
511 return EMULATE_DONE;
512
513 /* Read out the instruction */
514 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
515 /* Need to emulate */
516 return EMULATE_FAIL;
517
518 return EMULATE_AGAIN;
519}
520
521/* Handle external providers (FPU, Altivec, VSX) */
522static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
523 ulong msr)
524{
525 struct thread_struct *t = &current->thread;
526 u64 *vcpu_fpr = vcpu->arch.fpr;
527#ifdef CONFIG_VSX
528 u64 *vcpu_vsx = vcpu->arch.vsr;
529#endif
530 u64 *thread_fpr = (u64*)t->fpr;
531 int i;
532
533 /* When we have paired singles, we emulate in software */
534 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
535 return RESUME_GUEST;
536
537 if (!(vcpu->arch.shared->msr & msr)) {
538 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
539 return RESUME_GUEST;
540 }
541
542 /* We already own the ext */
543 if (vcpu->arch.guest_owned_ext & msr) {
544 return RESUME_GUEST;
545 }
546
547#ifdef DEBUG_EXT
548 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
549#endif
550
551 current->thread.regs->msr |= msr;
552
553 switch (msr) {
554 case MSR_FP:
555 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
556 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
557
558 t->fpscr.val = vcpu->arch.fpscr;
559 t->fpexc_mode = 0;
560 kvmppc_load_up_fpu();
561 break;
562 case MSR_VEC:
563#ifdef CONFIG_ALTIVEC
564 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
565 t->vscr = vcpu->arch.vscr;
566 t->vrsave = -1;
567 kvmppc_load_up_altivec();
568#endif
569 break;
570 case MSR_VSX:
571#ifdef CONFIG_VSX
572 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
573 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
574 kvmppc_load_up_vsx();
575#endif
576 break;
577 default:
578 BUG();
579 }
580
581 vcpu->arch.guest_owned_ext |= msr;
582
583 kvmppc_recalc_shadow_msr(vcpu);
584
585 return RESUME_GUEST;
586}
587
588int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
589 unsigned int exit_nr)
590{
591 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200592 int s;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000593
594 vcpu->stat.sum_exits++;
595
596 run->exit_reason = KVM_EXIT_UNKNOWN;
597 run->ready_for_interrupt_injection = 1;
598
Alexander Grafbd2be682012-08-13 01:04:19 +0200599 /* We get here with MSR.EE=1 */
Alexander Graf3b1d9d72012-04-30 10:56:12 +0200600
Alexander Graf97c95052012-08-02 15:10:00 +0200601 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200602 kvm_guest_exit();
Alexander Grafc63ddcb2012-08-12 11:27:49 +0200603
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000604 switch (exit_nr) {
605 case BOOK3S_INTERRUPT_INST_STORAGE:
Alexander Graf468a12c2011-12-09 14:44:13 +0100606 {
607 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
608 ulong shadow_srr1 = svcpu->shadow_srr1;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000609 vcpu->stat.pf_instruc++;
610
611#ifdef CONFIG_PPC_BOOK3S_32
612 /* We set segments as unused segments when invalidating them. So
613 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100614 if (svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000615 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
616 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100617 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000618 break;
619 }
620#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100621 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000622
623 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graf468a12c2011-12-09 14:44:13 +0100624 if (shadow_srr1 & 0x40000000) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000625 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
626 vcpu->stat.sp_instruc++;
627 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
628 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
629 /*
630 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
631 * so we can't use the NX bit inside the guest. Let's cross our fingers,
632 * that no guest that needs the dcbz hack does NX.
633 */
634 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
635 r = RESUME_GUEST;
636 } else {
Alexander Graf468a12c2011-12-09 14:44:13 +0100637 vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000638 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
639 r = RESUME_GUEST;
640 }
641 break;
Alexander Graf468a12c2011-12-09 14:44:13 +0100642 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000643 case BOOK3S_INTERRUPT_DATA_STORAGE:
644 {
645 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100646 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
647 u32 fault_dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000648 vcpu->stat.pf_storage++;
649
650#ifdef CONFIG_PPC_BOOK3S_32
651 /* We set segments as unused segments when invalidating them. So
652 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100653 if ((svcpu->sr[dar >> SID_SHIFT]) == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000654 kvmppc_mmu_map_segment(vcpu, dar);
655 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100656 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000657 break;
658 }
659#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100660 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000661
662 /* The only case we need to handle is missing shadow PTEs */
Alexander Graf468a12c2011-12-09 14:44:13 +0100663 if (fault_dsisr & DSISR_NOHPTE) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000664 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
665 } else {
666 vcpu->arch.shared->dar = dar;
Alexander Graf468a12c2011-12-09 14:44:13 +0100667 vcpu->arch.shared->dsisr = fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000668 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
669 r = RESUME_GUEST;
670 }
671 break;
672 }
673 case BOOK3S_INTERRUPT_DATA_SEGMENT:
674 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
675 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
676 kvmppc_book3s_queue_irqprio(vcpu,
677 BOOK3S_INTERRUPT_DATA_SEGMENT);
678 }
679 r = RESUME_GUEST;
680 break;
681 case BOOK3S_INTERRUPT_INST_SEGMENT:
682 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
683 kvmppc_book3s_queue_irqprio(vcpu,
684 BOOK3S_INTERRUPT_INST_SEGMENT);
685 }
686 r = RESUME_GUEST;
687 break;
688 /* We're good on these - the host merely wanted to get our attention */
689 case BOOK3S_INTERRUPT_DECREMENTER:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100690 case BOOK3S_INTERRUPT_HV_DECREMENTER:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000691 vcpu->stat.dec_exits++;
692 r = RESUME_GUEST;
693 break;
694 case BOOK3S_INTERRUPT_EXTERNAL:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100695 case BOOK3S_INTERRUPT_EXTERNAL_LEVEL:
696 case BOOK3S_INTERRUPT_EXTERNAL_HV:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000697 vcpu->stat.ext_intr_exits++;
698 r = RESUME_GUEST;
699 break;
700 case BOOK3S_INTERRUPT_PERFMON:
701 r = RESUME_GUEST;
702 break;
703 case BOOK3S_INTERRUPT_PROGRAM:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100704 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000705 {
706 enum emulation_result er;
Alexander Graf468a12c2011-12-09 14:44:13 +0100707 struct kvmppc_book3s_shadow_vcpu *svcpu;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000708 ulong flags;
709
710program_interrupt:
Alexander Graf468a12c2011-12-09 14:44:13 +0100711 svcpu = svcpu_get(vcpu);
712 flags = svcpu->shadow_srr1 & 0x1f0000ull;
713 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000714
715 if (vcpu->arch.shared->msr & MSR_PR) {
716#ifdef EXIT_DEBUG
717 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
718#endif
719 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
720 (INS_DCBZ & 0xfffffff7)) {
721 kvmppc_core_queue_program(vcpu, flags);
722 r = RESUME_GUEST;
723 break;
724 }
725 }
726
727 vcpu->stat.emulated_inst_exits++;
728 er = kvmppc_emulate_instruction(run, vcpu);
729 switch (er) {
730 case EMULATE_DONE:
731 r = RESUME_GUEST_NV;
732 break;
733 case EMULATE_AGAIN:
734 r = RESUME_GUEST;
735 break;
736 case EMULATE_FAIL:
737 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
738 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
739 kvmppc_core_queue_program(vcpu, flags);
740 r = RESUME_GUEST;
741 break;
742 case EMULATE_DO_MMIO:
743 run->exit_reason = KVM_EXIT_MMIO;
744 r = RESUME_HOST_NV;
745 break;
746 default:
747 BUG();
748 }
749 break;
750 }
751 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafa668f2b2011-08-08 17:26:24 +0200752 if (vcpu->arch.papr_enabled &&
753 (kvmppc_get_last_inst(vcpu) == 0x44000022) &&
754 !(vcpu->arch.shared->msr & MSR_PR)) {
755 /* SC 1 papr hypercalls */
756 ulong cmd = kvmppc_get_gpr(vcpu, 3);
757 int i;
758
Andreas Schwab96f38d72011-11-08 07:17:39 +0000759#ifdef CONFIG_KVM_BOOK3S_64_PR
Alexander Grafa668f2b2011-08-08 17:26:24 +0200760 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
761 r = RESUME_GUEST;
762 break;
763 }
Andreas Schwab96f38d72011-11-08 07:17:39 +0000764#endif
Alexander Grafa668f2b2011-08-08 17:26:24 +0200765
766 run->papr_hcall.nr = cmd;
767 for (i = 0; i < 9; ++i) {
768 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
769 run->papr_hcall.args[i] = gpr;
770 }
771 run->exit_reason = KVM_EXIT_PAPR_HCALL;
772 vcpu->arch.hcall_needed = 1;
773 r = RESUME_HOST;
774 } else if (vcpu->arch.osi_enabled &&
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000775 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
776 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
777 /* MOL hypercalls */
778 u64 *gprs = run->osi.gprs;
779 int i;
780
781 run->exit_reason = KVM_EXIT_OSI;
782 for (i = 0; i < 32; i++)
783 gprs[i] = kvmppc_get_gpr(vcpu, i);
784 vcpu->arch.osi_needed = 1;
785 r = RESUME_HOST_NV;
786 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
787 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
788 /* KVM PV hypercalls */
789 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
790 r = RESUME_GUEST;
791 } else {
792 /* Guest syscalls */
793 vcpu->stat.syscall_exits++;
794 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
795 r = RESUME_GUEST;
796 }
797 break;
798 case BOOK3S_INTERRUPT_FP_UNAVAIL:
799 case BOOK3S_INTERRUPT_ALTIVEC:
800 case BOOK3S_INTERRUPT_VSX:
801 {
802 int ext_msr = 0;
803
804 switch (exit_nr) {
805 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
806 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
807 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
808 }
809
810 switch (kvmppc_check_ext(vcpu, exit_nr)) {
811 case EMULATE_DONE:
812 /* everything ok - let's enable the ext */
813 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
814 break;
815 case EMULATE_FAIL:
816 /* we need to emulate this instruction */
817 goto program_interrupt;
818 break;
819 default:
820 /* nothing to worry about - go again */
821 break;
822 }
823 break;
824 }
825 case BOOK3S_INTERRUPT_ALIGNMENT:
826 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
827 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
828 kvmppc_get_last_inst(vcpu));
829 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
830 kvmppc_get_last_inst(vcpu));
831 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
832 }
833 r = RESUME_GUEST;
834 break;
835 case BOOK3S_INTERRUPT_MACHINE_CHECK:
836 case BOOK3S_INTERRUPT_TRACE:
837 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
838 r = RESUME_GUEST;
839 break;
840 default:
Alexander Graf468a12c2011-12-09 14:44:13 +0100841 {
842 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
843 ulong shadow_srr1 = svcpu->shadow_srr1;
844 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000845 /* Ugh - bork here! What did we get? */
846 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Graf468a12c2011-12-09 14:44:13 +0100847 exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000848 r = RESUME_HOST;
849 BUG();
850 break;
851 }
Alexander Graf468a12c2011-12-09 14:44:13 +0100852 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000853
854 if (!(r & RESUME_HOST)) {
855 /* To avoid clobbering exit_reason, only check for signals if
856 * we aren't already exiting to userspace for some other
857 * reason. */
Alexander Grafe371f712011-12-19 13:36:55 +0100858
859 /*
860 * Interrupts could be timers for the guest which we have to
861 * inject again, so let's postpone them until we're in the guest
862 * and if we really did time things so badly, then we just exit
863 * again due to a host external interrupt.
864 */
Alexander Grafbd2be682012-08-13 01:04:19 +0200865 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200866 s = kvmppc_prepare_to_enter(vcpu);
867 if (s <= 0) {
Alexander Grafbd2be682012-08-13 01:04:19 +0200868 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200869 r = s;
Alexander Graf24afa372012-08-12 12:42:30 +0200870 } else {
Alexander Grafbd2be682012-08-13 01:04:19 +0200871 kvmppc_lazy_ee_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000872 }
873 }
874
875 trace_kvm_book3s_reenter(r, vcpu);
876
877 return r;
878}
879
880int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
881 struct kvm_sregs *sregs)
882{
883 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
884 int i;
885
886 sregs->pvr = vcpu->arch.pvr;
887
888 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
889 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
890 for (i = 0; i < 64; i++) {
891 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
892 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
893 }
894 } else {
895 for (i = 0; i < 16; i++)
896 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
897
898 for (i = 0; i < 8; i++) {
899 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
900 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
901 }
902 }
903
904 return 0;
905}
906
907int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
908 struct kvm_sregs *sregs)
909{
910 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
911 int i;
912
913 kvmppc_set_pvr(vcpu, sregs->pvr);
914
915 vcpu3s->sdr1 = sregs->u.s.sdr1;
916 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
917 for (i = 0; i < 64; i++) {
918 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
919 sregs->u.s.ppc64.slb[i].slbe);
920 }
921 } else {
922 for (i = 0; i < 16; i++) {
923 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
924 }
925 for (i = 0; i < 8; i++) {
926 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
927 (u32)sregs->u.s.ppc32.ibat[i]);
928 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
929 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
930 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
931 (u32)sregs->u.s.ppc32.dbat[i]);
932 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
933 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
934 }
935 }
936
937 /* Flush the MMU after messing with the segments */
938 kvmppc_mmu_pte_flush(vcpu, 0, 0);
939
940 return 0;
941}
942
Paul Mackerras31f34382011-12-12 12:26:50 +0000943int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
944{
945 int r = -EINVAL;
946
947 switch (reg->id) {
948 case KVM_REG_PPC_HIOR:
Alexander Grafb8e6f8a2012-03-13 19:59:39 +0100949 r = copy_to_user((u64 __user *)(long)reg->addr,
950 &to_book3s(vcpu)->hior, sizeof(u64));
Paul Mackerras31f34382011-12-12 12:26:50 +0000951 break;
952 default:
953 break;
954 }
955
956 return r;
957}
958
959int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
960{
961 int r = -EINVAL;
962
963 switch (reg->id) {
964 case KVM_REG_PPC_HIOR:
Alexander Grafb8e6f8a2012-03-13 19:59:39 +0100965 r = copy_from_user(&to_book3s(vcpu)->hior,
966 (u64 __user *)(long)reg->addr, sizeof(u64));
Paul Mackerras31f34382011-12-12 12:26:50 +0000967 if (!r)
968 to_book3s(vcpu)->hior_explicit = true;
969 break;
970 default:
971 break;
972 }
973
974 return r;
975}
976
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000977int kvmppc_core_check_processor_compat(void)
978{
979 return 0;
980}
981
982struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
983{
984 struct kvmppc_vcpu_book3s *vcpu_book3s;
985 struct kvm_vcpu *vcpu;
986 int err = -ENOMEM;
987 unsigned long p;
988
989 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
990 if (!vcpu_book3s)
991 goto out;
992
993 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
994 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
995 if (!vcpu_book3s->shadow_vcpu)
996 goto free_vcpu;
997
998 vcpu = &vcpu_book3s->vcpu;
999 err = kvm_vcpu_init(vcpu, kvm, id);
1000 if (err)
1001 goto free_shadow_vcpu;
1002
1003 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1004 /* the real shared page fills the last 4k of our page */
1005 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1006 if (!p)
1007 goto uninit_vcpu;
1008
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001009#ifdef CONFIG_PPC_BOOK3S_64
1010 /* default to book3s_64 (970fx) */
1011 vcpu->arch.pvr = 0x3C0301;
1012#else
1013 /* default to book3s_32 (750) */
1014 vcpu->arch.pvr = 0x84202;
1015#endif
1016 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1017 vcpu->arch.slb_nr = 64;
1018
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001019 vcpu->arch.shadow_msr = MSR_USER64;
1020
1021 err = kvmppc_mmu_init(vcpu);
1022 if (err < 0)
1023 goto uninit_vcpu;
1024
1025 return vcpu;
1026
1027uninit_vcpu:
1028 kvm_vcpu_uninit(vcpu);
1029free_shadow_vcpu:
1030 kfree(vcpu_book3s->shadow_vcpu);
1031free_vcpu:
1032 vfree(vcpu_book3s);
1033out:
1034 return ERR_PTR(err);
1035}
1036
1037void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1038{
1039 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1040
1041 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
1042 kvm_vcpu_uninit(vcpu);
1043 kfree(vcpu_book3s->shadow_vcpu);
1044 vfree(vcpu_book3s);
1045}
1046
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001047int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001048{
1049 int ret;
1050 double fpr[32][TS_FPRWIDTH];
1051 unsigned int fpscr;
1052 int fpexc_mode;
1053#ifdef CONFIG_ALTIVEC
1054 vector128 vr[32];
1055 vector128 vscr;
1056 unsigned long uninitialized_var(vrsave);
1057 int used_vr;
1058#endif
1059#ifdef CONFIG_VSX
1060 int used_vsr;
1061#endif
1062 ulong ext_msr;
1063
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001064 /* Check if we can run the vcpu at all */
1065 if (!vcpu->arch.sane) {
1066 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
Alexander Graf7d827142011-12-09 15:46:21 +01001067 ret = -EINVAL;
1068 goto out;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001069 }
1070
Alexander Grafe371f712011-12-19 13:36:55 +01001071 /*
1072 * Interrupts could be timers for the guest which we have to inject
1073 * again, so let's postpone them until we're in the guest and if we
1074 * really did time things so badly, then we just exit again due to
1075 * a host external interrupt.
1076 */
Alexander Grafbd2be682012-08-13 01:04:19 +02001077 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001078 ret = kvmppc_prepare_to_enter(vcpu);
1079 if (ret <= 0) {
Alexander Grafbd2be682012-08-13 01:04:19 +02001080 local_irq_enable();
Alexander Graf7d827142011-12-09 15:46:21 +01001081 goto out;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001082 }
1083
1084 /* Save FPU state in stack */
1085 if (current->thread.regs->msr & MSR_FP)
1086 giveup_fpu(current);
1087 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1088 fpscr = current->thread.fpscr.val;
1089 fpexc_mode = current->thread.fpexc_mode;
1090
1091#ifdef CONFIG_ALTIVEC
1092 /* Save Altivec state in stack */
1093 used_vr = current->thread.used_vr;
1094 if (used_vr) {
1095 if (current->thread.regs->msr & MSR_VEC)
1096 giveup_altivec(current);
1097 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1098 vscr = current->thread.vscr;
1099 vrsave = current->thread.vrsave;
1100 }
1101#endif
1102
1103#ifdef CONFIG_VSX
1104 /* Save VSX state in stack */
1105 used_vsr = current->thread.used_vsr;
1106 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
1107 __giveup_vsx(current);
1108#endif
1109
1110 /* Remember the MSR with disabled extensions */
1111 ext_msr = current->thread.regs->msr;
1112
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001113 /* Preload FPU if it's enabled */
1114 if (vcpu->arch.shared->msr & MSR_FP)
1115 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1116
Alexander Grafbd2be682012-08-13 01:04:19 +02001117 kvmppc_lazy_ee_enable();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001118
1119 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
1120
Alexander Graf24afa372012-08-12 12:42:30 +02001121 /* No need for kvm_guest_exit. It's done in handle_exit.
1122 We also get here with interrupts enabled. */
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001123
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001124 current->thread.regs->msr = ext_msr;
1125
1126 /* Make sure we save the guest FPU/Altivec/VSX state */
1127 kvmppc_giveup_ext(vcpu, MSR_FP);
1128 kvmppc_giveup_ext(vcpu, MSR_VEC);
1129 kvmppc_giveup_ext(vcpu, MSR_VSX);
1130
1131 /* Restore FPU state from stack */
1132 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1133 current->thread.fpscr.val = fpscr;
1134 current->thread.fpexc_mode = fpexc_mode;
1135
1136#ifdef CONFIG_ALTIVEC
1137 /* Restore Altivec state from stack */
1138 if (used_vr && current->thread.used_vr) {
1139 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1140 current->thread.vscr = vscr;
1141 current->thread.vrsave = vrsave;
1142 }
1143 current->thread.used_vr = used_vr;
1144#endif
1145
1146#ifdef CONFIG_VSX
1147 current->thread.used_vsr = used_vsr;
1148#endif
1149
Alexander Graf7d827142011-12-09 15:46:21 +01001150out:
Alexander Graf0652eaa2012-08-12 11:34:21 +02001151 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001152 return ret;
1153}
1154
Paul Mackerras82ed3612011-12-15 02:03:22 +00001155/*
1156 * Get (and clear) the dirty memory log for a memory slot.
1157 */
1158int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1159 struct kvm_dirty_log *log)
1160{
1161 struct kvm_memory_slot *memslot;
1162 struct kvm_vcpu *vcpu;
1163 ulong ga, ga_end;
1164 int is_dirty = 0;
1165 int r;
1166 unsigned long n;
1167
1168 mutex_lock(&kvm->slots_lock);
1169
1170 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1171 if (r)
1172 goto out;
1173
1174 /* If nothing is dirty, don't bother messing with page tables. */
1175 if (is_dirty) {
1176 memslot = id_to_memslot(kvm->memslots, log->slot);
1177
1178 ga = memslot->base_gfn << PAGE_SHIFT;
1179 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1180
1181 kvm_for_each_vcpu(n, vcpu, kvm)
1182 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1183
1184 n = kvm_dirty_bitmap_bytes(memslot);
1185 memset(memslot->dirty_bitmap, 0, n);
1186 }
1187
1188 r = 0;
1189out:
1190 mutex_unlock(&kvm->slots_lock);
1191 return r;
1192}
1193
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001194#ifdef CONFIG_PPC64
1195int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1196{
1197 /* No flags */
1198 info->flags = 0;
1199
1200 /* SLB is always 64 entries */
1201 info->slb_size = 64;
1202
1203 /* Standard 4k base page size segment */
1204 info->sps[0].page_shift = 12;
1205 info->sps[0].slb_enc = 0;
1206 info->sps[0].enc[0].page_shift = 12;
1207 info->sps[0].enc[0].pte_enc = 0;
1208
1209 /* Standard 16M large page size segment */
1210 info->sps[1].page_shift = 24;
1211 info->sps[1].slb_enc = SLB_VSID_L;
1212 info->sps[1].enc[0].page_shift = 24;
1213 info->sps[1].enc[0].pte_enc = 0;
1214
1215 return 0;
1216}
1217#endif /* CONFIG_PPC64 */
1218
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001219int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1220 struct kvm_userspace_memory_region *mem)
1221{
1222 return 0;
1223}
1224
1225void kvmppc_core_commit_memory_region(struct kvm *kvm,
1226 struct kvm_userspace_memory_region *mem)
1227{
1228}
1229
1230int kvmppc_core_init_vm(struct kvm *kvm)
1231{
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001232#ifdef CONFIG_PPC64
1233 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
1234#endif
1235
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001236 return 0;
1237}
1238
1239void kvmppc_core_destroy_vm(struct kvm *kvm)
1240{
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001241#ifdef CONFIG_PPC64
1242 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
1243#endif
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001244}
1245
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001246static int kvmppc_book3s_init(void)
1247{
1248 int r;
1249
1250 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1251 THIS_MODULE);
1252
1253 if (r)
1254 return r;
1255
1256 r = kvmppc_mmu_hpte_sysinit();
1257
1258 return r;
1259}
1260
1261static void kvmppc_book3s_exit(void)
1262{
1263 kvmppc_mmu_hpte_sysexit();
1264 kvm_exit();
1265}
1266
1267module_init(kvmppc_book3s_init);
1268module_exit(kvmppc_book3s_exit);