blob: cae2defd14623d3fd0094597583cb71e774e9f82 [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
Alexander Grafe371f712011-12-19 13:36:55 +010055#define __hard_irq_disable local_irq_disable
56#define __hard_irq_enable local_irq_enable
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000057#endif
58
59void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
60{
61#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf468a12c2011-12-09 14:44:13 +010062 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
63 memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb));
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000064 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
65 sizeof(get_paca()->shadow_vcpu));
Alexander Graf468a12c2011-12-09 14:44:13 +010066 svcpu->slb_max = to_book3s(vcpu)->slb_shadow_max;
67 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000068#endif
69
70#ifdef CONFIG_PPC_BOOK3S_32
71 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
72#endif
73}
74
75void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
76{
77#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf468a12c2011-12-09 14:44:13 +010078 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
79 memcpy(to_book3s(vcpu)->slb_shadow, svcpu->slb, sizeof(svcpu->slb));
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000080 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
81 sizeof(get_paca()->shadow_vcpu));
Alexander Graf468a12c2011-12-09 14:44:13 +010082 to_book3s(vcpu)->slb_shadow_max = svcpu->slb_max;
83 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000084#endif
85
86 kvmppc_giveup_ext(vcpu, MSR_FP);
87 kvmppc_giveup_ext(vcpu, MSR_VEC);
88 kvmppc_giveup_ext(vcpu, MSR_VSX);
89}
90
Alexander Graf03d25c52012-08-10 12:28:50 +020091void kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
92{
93}
94
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000095static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
96{
97 ulong smsr = vcpu->arch.shared->msr;
98
99 /* Guest MSR values */
100 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
101 /* Process MSR values */
102 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
103 /* External providers the guest reserved */
104 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
105 /* 64-bit Process MSR values */
106#ifdef CONFIG_PPC_BOOK3S_64
107 smsr |= MSR_ISF | MSR_HV;
108#endif
109 vcpu->arch.shadow_msr = smsr;
110}
111
112void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
113{
114 ulong old_msr = vcpu->arch.shared->msr;
115
116#ifdef EXIT_DEBUG
117 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
118#endif
119
120 msr &= to_book3s(vcpu)->msr_mask;
121 vcpu->arch.shared->msr = msr;
122 kvmppc_recalc_shadow_msr(vcpu);
123
124 if (msr & MSR_POW) {
125 if (!vcpu->arch.pending_exceptions) {
126 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100127 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000128 vcpu->stat.halt_wakeup++;
129
130 /* Unset POW bit after we woke up */
131 msr &= ~MSR_POW;
132 vcpu->arch.shared->msr = msr;
133 }
134 }
135
136 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
137 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
138 kvmppc_mmu_flush_segments(vcpu);
139 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
140
141 /* Preload magic page segment when in kernel mode */
142 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
143 struct kvm_vcpu_arch *a = &vcpu->arch;
144
145 if (msr & MSR_DR)
146 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
147 else
148 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
149 }
150 }
151
Benjamin Herrenschmidtbbcc9c02012-03-13 21:52:44 +0000152 /*
153 * When switching from 32 to 64-bit, we may have a stale 32-bit
154 * magic page around, we need to flush it. Typically 32-bit magic
155 * page will be instanciated when calling into RTAS. Note: We
156 * assume that such transition only happens while in kernel mode,
157 * ie, we never transition from user 32-bit to kernel 64-bit with
158 * a 32-bit magic page around.
159 */
160 if (vcpu->arch.magic_page_pa &&
161 !(old_msr & MSR_PR) && !(old_msr & MSR_SF) && (msr & MSR_SF)) {
162 /* going from RTAS to normal kernel code */
163 kvmppc_mmu_pte_flush(vcpu, (uint32_t)vcpu->arch.magic_page_pa,
164 ~0xFFFUL);
165 }
166
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000167 /* Preload FPU if it's enabled */
168 if (vcpu->arch.shared->msr & MSR_FP)
169 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
170}
171
172void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
173{
174 u32 host_pvr;
175
176 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
177 vcpu->arch.pvr = pvr;
178#ifdef CONFIG_PPC_BOOK3S_64
179 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
180 kvmppc_mmu_book3s_64_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200181 if (!to_book3s(vcpu)->hior_explicit)
182 to_book3s(vcpu)->hior = 0xfff00000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000183 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200184 vcpu->arch.cpu_type = KVM_CPU_3S_64;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000185 } else
186#endif
187 {
188 kvmppc_mmu_book3s_32_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200189 if (!to_book3s(vcpu)->hior_explicit)
190 to_book3s(vcpu)->hior = 0;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000191 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200192 vcpu->arch.cpu_type = KVM_CPU_3S_32;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000193 }
194
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200195 kvmppc_sanity_check(vcpu);
196
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000197 /* If we are in hypervisor level on 970, we can tell the CPU to
198 * treat DCBZ as 32 bytes store */
199 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
200 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
201 !strcmp(cur_cpu_spec->platform, "ppc970"))
202 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
203
204 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
205 really needs them in a VM on Cell and force disable them. */
206 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
207 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
208
209#ifdef CONFIG_PPC_BOOK3S_32
210 /* 32 bit Book3S always has 32 byte dcbz */
211 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
212#endif
213
214 /* On some CPUs we can execute paired single operations natively */
215 asm ( "mfpvr %0" : "=r"(host_pvr));
216 switch (host_pvr) {
217 case 0x00080200: /* lonestar 2.0 */
218 case 0x00088202: /* lonestar 2.2 */
219 case 0x70000100: /* gekko 1.0 */
220 case 0x00080100: /* gekko 2.0 */
221 case 0x00083203: /* gekko 2.3a */
222 case 0x00083213: /* gekko 2.3b */
223 case 0x00083204: /* gekko 2.4 */
224 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
225 case 0x00087200: /* broadway */
226 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
227 /* Enable HID2.PSE - in case we need it later */
228 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
229 }
230}
231
232/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
233 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
234 * emulate 32 bytes dcbz length.
235 *
236 * The Book3s_64 inventors also realized this case and implemented a special bit
237 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
238 *
239 * My approach here is to patch the dcbz instruction on executing pages.
240 */
241static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
242{
243 struct page *hpage;
244 u64 hpage_offset;
245 u32 *page;
246 int i;
247
248 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800249 if (is_error_page(hpage))
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000250 return;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000251
252 hpage_offset = pte->raddr & ~PAGE_MASK;
253 hpage_offset &= ~0xFFFULL;
254 hpage_offset /= 4;
255
256 get_page(hpage);
Cong Wang2480b202011-11-25 23:14:16 +0800257 page = kmap_atomic(hpage);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000258
259 /* patch dcbz into reserved instruction, so we trap */
260 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
261 if ((page[i] & 0xff0007ff) == INS_DCBZ)
262 page[i] &= 0xfffffff7;
263
Cong Wang2480b202011-11-25 23:14:16 +0800264 kunmap_atomic(page);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000265 put_page(hpage);
266}
267
268static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
269{
270 ulong mp_pa = vcpu->arch.magic_page_pa;
271
Benjamin Herrenschmidtbbcc9c02012-03-13 21:52:44 +0000272 if (!(vcpu->arch.shared->msr & MSR_SF))
273 mp_pa = (uint32_t)mp_pa;
274
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000275 if (unlikely(mp_pa) &&
276 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
277 return 1;
278 }
279
280 return kvm_is_visible_gfn(vcpu->kvm, gfn);
281}
282
283int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
284 ulong eaddr, int vec)
285{
286 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
287 int r = RESUME_GUEST;
288 int relocated;
289 int page_found = 0;
290 struct kvmppc_pte pte;
291 bool is_mmio = false;
292 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
293 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
294 u64 vsid;
295
296 relocated = data ? dr : ir;
297
298 /* Resolve real address if translation turned on */
299 if (relocated) {
300 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
301 } else {
302 pte.may_execute = true;
303 pte.may_read = true;
304 pte.may_write = true;
305 pte.raddr = eaddr & KVM_PAM;
306 pte.eaddr = eaddr;
307 pte.vpage = eaddr >> 12;
308 }
309
310 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
311 case 0:
312 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
313 break;
314 case MSR_DR:
315 case MSR_IR:
316 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
317
318 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
319 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
320 else
321 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
322 pte.vpage |= vsid;
323
324 if (vsid == -1)
325 page_found = -EINVAL;
326 break;
327 }
328
329 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
330 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
331 /*
332 * If we do the dcbz hack, we have to NX on every execution,
333 * so we can patch the executing code. This renders our guest
334 * NX-less.
335 */
336 pte.may_execute = !data;
337 }
338
339 if (page_found == -ENOENT) {
340 /* Page not found in guest PTE entries */
Alexander Graf468a12c2011-12-09 14:44:13 +0100341 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000342 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100343 vcpu->arch.shared->dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000344 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100345 (svcpu->shadow_srr1 & 0x00000000f8000000ULL);
346 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000347 kvmppc_book3s_queue_irqprio(vcpu, vec);
348 } else if (page_found == -EPERM) {
349 /* Storage protection */
Alexander Graf468a12c2011-12-09 14:44:13 +0100350 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000351 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100352 vcpu->arch.shared->dsisr = svcpu->fault_dsisr & ~DSISR_NOHPTE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000353 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
354 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100355 svcpu->shadow_srr1 & 0x00000000f8000000ULL;
356 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000357 kvmppc_book3s_queue_irqprio(vcpu, vec);
358 } else if (page_found == -EINVAL) {
359 /* Page not found in guest SLB */
360 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
361 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
362 } else if (!is_mmio &&
363 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
364 /* The guest's PTE is not mapped yet. Map on the host */
365 kvmppc_mmu_map_page(vcpu, &pte);
366 if (data)
367 vcpu->stat.sp_storage++;
368 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
369 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
370 kvmppc_patch_dcbz(vcpu, &pte);
371 } else {
372 /* MMIO */
373 vcpu->stat.mmio_exits++;
374 vcpu->arch.paddr_accessed = pte.raddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100375 vcpu->arch.vaddr_accessed = pte.eaddr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000376 r = kvmppc_emulate_mmio(run, vcpu);
377 if ( r == RESUME_HOST_NV )
378 r = RESUME_HOST;
379 }
380
381 return r;
382}
383
384static inline int get_fpr_index(int i)
385{
386#ifdef CONFIG_VSX
387 i *= 2;
388#endif
389 return i;
390}
391
392/* Give up external provider (FPU, Altivec, VSX) */
393void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
394{
395 struct thread_struct *t = &current->thread;
396 u64 *vcpu_fpr = vcpu->arch.fpr;
397#ifdef CONFIG_VSX
398 u64 *vcpu_vsx = vcpu->arch.vsr;
399#endif
400 u64 *thread_fpr = (u64*)t->fpr;
401 int i;
402
403 if (!(vcpu->arch.guest_owned_ext & msr))
404 return;
405
406#ifdef DEBUG_EXT
407 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
408#endif
409
410 switch (msr) {
411 case MSR_FP:
412 giveup_fpu(current);
413 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
414 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
415
416 vcpu->arch.fpscr = t->fpscr.val;
417 break;
418 case MSR_VEC:
419#ifdef CONFIG_ALTIVEC
420 giveup_altivec(current);
421 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
422 vcpu->arch.vscr = t->vscr;
423#endif
424 break;
425 case MSR_VSX:
426#ifdef CONFIG_VSX
427 __giveup_vsx(current);
428 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
429 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
430#endif
431 break;
432 default:
433 BUG();
434 }
435
436 vcpu->arch.guest_owned_ext &= ~msr;
437 current->thread.regs->msr &= ~msr;
438 kvmppc_recalc_shadow_msr(vcpu);
439}
440
441static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
442{
443 ulong srr0 = kvmppc_get_pc(vcpu);
444 u32 last_inst = kvmppc_get_last_inst(vcpu);
445 int ret;
446
447 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
448 if (ret == -ENOENT) {
449 ulong msr = vcpu->arch.shared->msr;
450
451 msr = kvmppc_set_field(msr, 33, 33, 1);
452 msr = kvmppc_set_field(msr, 34, 36, 0);
453 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
454 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
455 return EMULATE_AGAIN;
456 }
457
458 return EMULATE_DONE;
459}
460
461static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
462{
463
464 /* Need to do paired single emulation? */
465 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
466 return EMULATE_DONE;
467
468 /* Read out the instruction */
469 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
470 /* Need to emulate */
471 return EMULATE_FAIL;
472
473 return EMULATE_AGAIN;
474}
475
476/* Handle external providers (FPU, Altivec, VSX) */
477static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
478 ulong msr)
479{
480 struct thread_struct *t = &current->thread;
481 u64 *vcpu_fpr = vcpu->arch.fpr;
482#ifdef CONFIG_VSX
483 u64 *vcpu_vsx = vcpu->arch.vsr;
484#endif
485 u64 *thread_fpr = (u64*)t->fpr;
486 int i;
487
488 /* When we have paired singles, we emulate in software */
489 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
490 return RESUME_GUEST;
491
492 if (!(vcpu->arch.shared->msr & msr)) {
493 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
494 return RESUME_GUEST;
495 }
496
497 /* We already own the ext */
498 if (vcpu->arch.guest_owned_ext & msr) {
499 return RESUME_GUEST;
500 }
501
502#ifdef DEBUG_EXT
503 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
504#endif
505
506 current->thread.regs->msr |= msr;
507
508 switch (msr) {
509 case MSR_FP:
510 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
511 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
512
513 t->fpscr.val = vcpu->arch.fpscr;
514 t->fpexc_mode = 0;
515 kvmppc_load_up_fpu();
516 break;
517 case MSR_VEC:
518#ifdef CONFIG_ALTIVEC
519 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
520 t->vscr = vcpu->arch.vscr;
521 t->vrsave = -1;
522 kvmppc_load_up_altivec();
523#endif
524 break;
525 case MSR_VSX:
526#ifdef CONFIG_VSX
527 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
528 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
529 kvmppc_load_up_vsx();
530#endif
531 break;
532 default:
533 BUG();
534 }
535
536 vcpu->arch.guest_owned_ext |= msr;
537
538 kvmppc_recalc_shadow_msr(vcpu);
539
540 return RESUME_GUEST;
541}
542
543int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
544 unsigned int exit_nr)
545{
546 int r = RESUME_HOST;
547
548 vcpu->stat.sum_exits++;
549
550 run->exit_reason = KVM_EXIT_UNKNOWN;
551 run->ready_for_interrupt_injection = 1;
552
Alexander Graf3b1d9d72012-04-30 10:56:12 +0200553 /* We get here with MSR.EE=0, so enable it to be a nice citizen */
554 __hard_irq_enable();
555
Alexander Graf97c95052012-08-02 15:10:00 +0200556 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf7d827142011-12-09 15:46:21 +0100557 preempt_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000558 kvm_resched(vcpu);
559 switch (exit_nr) {
560 case BOOK3S_INTERRUPT_INST_STORAGE:
Alexander Graf468a12c2011-12-09 14:44:13 +0100561 {
562 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
563 ulong shadow_srr1 = svcpu->shadow_srr1;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000564 vcpu->stat.pf_instruc++;
565
566#ifdef CONFIG_PPC_BOOK3S_32
567 /* We set segments as unused segments when invalidating them. So
568 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100569 if (svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000570 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
571 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100572 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000573 break;
574 }
575#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100576 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000577
578 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graf468a12c2011-12-09 14:44:13 +0100579 if (shadow_srr1 & 0x40000000) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000580 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
581 vcpu->stat.sp_instruc++;
582 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
583 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
584 /*
585 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
586 * so we can't use the NX bit inside the guest. Let's cross our fingers,
587 * that no guest that needs the dcbz hack does NX.
588 */
589 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
590 r = RESUME_GUEST;
591 } else {
Alexander Graf468a12c2011-12-09 14:44:13 +0100592 vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000593 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
594 r = RESUME_GUEST;
595 }
596 break;
Alexander Graf468a12c2011-12-09 14:44:13 +0100597 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000598 case BOOK3S_INTERRUPT_DATA_STORAGE:
599 {
600 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100601 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
602 u32 fault_dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000603 vcpu->stat.pf_storage++;
604
605#ifdef CONFIG_PPC_BOOK3S_32
606 /* We set segments as unused segments when invalidating them. So
607 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100608 if ((svcpu->sr[dar >> SID_SHIFT]) == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000609 kvmppc_mmu_map_segment(vcpu, dar);
610 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100611 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000612 break;
613 }
614#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100615 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000616
617 /* The only case we need to handle is missing shadow PTEs */
Alexander Graf468a12c2011-12-09 14:44:13 +0100618 if (fault_dsisr & DSISR_NOHPTE) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000619 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
620 } else {
621 vcpu->arch.shared->dar = dar;
Alexander Graf468a12c2011-12-09 14:44:13 +0100622 vcpu->arch.shared->dsisr = fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000623 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
624 r = RESUME_GUEST;
625 }
626 break;
627 }
628 case BOOK3S_INTERRUPT_DATA_SEGMENT:
629 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
630 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
631 kvmppc_book3s_queue_irqprio(vcpu,
632 BOOK3S_INTERRUPT_DATA_SEGMENT);
633 }
634 r = RESUME_GUEST;
635 break;
636 case BOOK3S_INTERRUPT_INST_SEGMENT:
637 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
638 kvmppc_book3s_queue_irqprio(vcpu,
639 BOOK3S_INTERRUPT_INST_SEGMENT);
640 }
641 r = RESUME_GUEST;
642 break;
643 /* We're good on these - the host merely wanted to get our attention */
644 case BOOK3S_INTERRUPT_DECREMENTER:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100645 case BOOK3S_INTERRUPT_HV_DECREMENTER:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000646 vcpu->stat.dec_exits++;
647 r = RESUME_GUEST;
648 break;
649 case BOOK3S_INTERRUPT_EXTERNAL:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100650 case BOOK3S_INTERRUPT_EXTERNAL_LEVEL:
651 case BOOK3S_INTERRUPT_EXTERNAL_HV:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000652 vcpu->stat.ext_intr_exits++;
653 r = RESUME_GUEST;
654 break;
655 case BOOK3S_INTERRUPT_PERFMON:
656 r = RESUME_GUEST;
657 break;
658 case BOOK3S_INTERRUPT_PROGRAM:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100659 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000660 {
661 enum emulation_result er;
Alexander Graf468a12c2011-12-09 14:44:13 +0100662 struct kvmppc_book3s_shadow_vcpu *svcpu;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000663 ulong flags;
664
665program_interrupt:
Alexander Graf468a12c2011-12-09 14:44:13 +0100666 svcpu = svcpu_get(vcpu);
667 flags = svcpu->shadow_srr1 & 0x1f0000ull;
668 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000669
670 if (vcpu->arch.shared->msr & MSR_PR) {
671#ifdef EXIT_DEBUG
672 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
673#endif
674 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
675 (INS_DCBZ & 0xfffffff7)) {
676 kvmppc_core_queue_program(vcpu, flags);
677 r = RESUME_GUEST;
678 break;
679 }
680 }
681
682 vcpu->stat.emulated_inst_exits++;
683 er = kvmppc_emulate_instruction(run, vcpu);
684 switch (er) {
685 case EMULATE_DONE:
686 r = RESUME_GUEST_NV;
687 break;
688 case EMULATE_AGAIN:
689 r = RESUME_GUEST;
690 break;
691 case EMULATE_FAIL:
692 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
693 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
694 kvmppc_core_queue_program(vcpu, flags);
695 r = RESUME_GUEST;
696 break;
697 case EMULATE_DO_MMIO:
698 run->exit_reason = KVM_EXIT_MMIO;
699 r = RESUME_HOST_NV;
700 break;
701 default:
702 BUG();
703 }
704 break;
705 }
706 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafa668f2b2011-08-08 17:26:24 +0200707 if (vcpu->arch.papr_enabled &&
708 (kvmppc_get_last_inst(vcpu) == 0x44000022) &&
709 !(vcpu->arch.shared->msr & MSR_PR)) {
710 /* SC 1 papr hypercalls */
711 ulong cmd = kvmppc_get_gpr(vcpu, 3);
712 int i;
713
Andreas Schwab96f38d72011-11-08 07:17:39 +0000714#ifdef CONFIG_KVM_BOOK3S_64_PR
Alexander Grafa668f2b2011-08-08 17:26:24 +0200715 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
716 r = RESUME_GUEST;
717 break;
718 }
Andreas Schwab96f38d72011-11-08 07:17:39 +0000719#endif
Alexander Grafa668f2b2011-08-08 17:26:24 +0200720
721 run->papr_hcall.nr = cmd;
722 for (i = 0; i < 9; ++i) {
723 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
724 run->papr_hcall.args[i] = gpr;
725 }
726 run->exit_reason = KVM_EXIT_PAPR_HCALL;
727 vcpu->arch.hcall_needed = 1;
728 r = RESUME_HOST;
729 } else if (vcpu->arch.osi_enabled &&
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000730 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
731 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
732 /* MOL hypercalls */
733 u64 *gprs = run->osi.gprs;
734 int i;
735
736 run->exit_reason = KVM_EXIT_OSI;
737 for (i = 0; i < 32; i++)
738 gprs[i] = kvmppc_get_gpr(vcpu, i);
739 vcpu->arch.osi_needed = 1;
740 r = RESUME_HOST_NV;
741 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
742 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
743 /* KVM PV hypercalls */
744 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
745 r = RESUME_GUEST;
746 } else {
747 /* Guest syscalls */
748 vcpu->stat.syscall_exits++;
749 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
750 r = RESUME_GUEST;
751 }
752 break;
753 case BOOK3S_INTERRUPT_FP_UNAVAIL:
754 case BOOK3S_INTERRUPT_ALTIVEC:
755 case BOOK3S_INTERRUPT_VSX:
756 {
757 int ext_msr = 0;
758
759 switch (exit_nr) {
760 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
761 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
762 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
763 }
764
765 switch (kvmppc_check_ext(vcpu, exit_nr)) {
766 case EMULATE_DONE:
767 /* everything ok - let's enable the ext */
768 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
769 break;
770 case EMULATE_FAIL:
771 /* we need to emulate this instruction */
772 goto program_interrupt;
773 break;
774 default:
775 /* nothing to worry about - go again */
776 break;
777 }
778 break;
779 }
780 case BOOK3S_INTERRUPT_ALIGNMENT:
781 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
782 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
783 kvmppc_get_last_inst(vcpu));
784 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
785 kvmppc_get_last_inst(vcpu));
786 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
787 }
788 r = RESUME_GUEST;
789 break;
790 case BOOK3S_INTERRUPT_MACHINE_CHECK:
791 case BOOK3S_INTERRUPT_TRACE:
792 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
793 r = RESUME_GUEST;
794 break;
795 default:
Alexander Graf468a12c2011-12-09 14:44:13 +0100796 {
797 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
798 ulong shadow_srr1 = svcpu->shadow_srr1;
799 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000800 /* Ugh - bork here! What did we get? */
801 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Graf468a12c2011-12-09 14:44:13 +0100802 exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000803 r = RESUME_HOST;
804 BUG();
805 break;
806 }
Alexander Graf468a12c2011-12-09 14:44:13 +0100807 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000808
Alexander Graf592f5d82012-03-13 23:31:02 +0100809 preempt_disable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000810 if (!(r & RESUME_HOST)) {
811 /* To avoid clobbering exit_reason, only check for signals if
812 * we aren't already exiting to userspace for some other
813 * reason. */
Alexander Grafe371f712011-12-19 13:36:55 +0100814
815 /*
816 * Interrupts could be timers for the guest which we have to
817 * inject again, so let's postpone them until we're in the guest
818 * and if we really did time things so badly, then we just exit
819 * again due to a host external interrupt.
820 */
821 __hard_irq_disable();
Alexander Graf03d25c52012-08-10 12:28:50 +0200822 if (kvmppc_prepare_to_enter(vcpu)) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000823 run->exit_reason = KVM_EXIT_INTR;
824 r = -EINTR;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000825 }
826 }
827
828 trace_kvm_book3s_reenter(r, vcpu);
829
830 return r;
831}
832
833int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
834 struct kvm_sregs *sregs)
835{
836 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
837 int i;
838
839 sregs->pvr = vcpu->arch.pvr;
840
841 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
842 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
843 for (i = 0; i < 64; i++) {
844 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
845 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
846 }
847 } else {
848 for (i = 0; i < 16; i++)
849 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
850
851 for (i = 0; i < 8; i++) {
852 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
853 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
854 }
855 }
856
857 return 0;
858}
859
860int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
861 struct kvm_sregs *sregs)
862{
863 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
864 int i;
865
866 kvmppc_set_pvr(vcpu, sregs->pvr);
867
868 vcpu3s->sdr1 = sregs->u.s.sdr1;
869 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
870 for (i = 0; i < 64; i++) {
871 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
872 sregs->u.s.ppc64.slb[i].slbe);
873 }
874 } else {
875 for (i = 0; i < 16; i++) {
876 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
877 }
878 for (i = 0; i < 8; i++) {
879 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
880 (u32)sregs->u.s.ppc32.ibat[i]);
881 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
882 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
883 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
884 (u32)sregs->u.s.ppc32.dbat[i]);
885 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
886 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
887 }
888 }
889
890 /* Flush the MMU after messing with the segments */
891 kvmppc_mmu_pte_flush(vcpu, 0, 0);
892
893 return 0;
894}
895
Paul Mackerras31f34382011-12-12 12:26:50 +0000896int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
897{
898 int r = -EINVAL;
899
900 switch (reg->id) {
901 case KVM_REG_PPC_HIOR:
Alexander Grafb8e6f8a2012-03-13 19:59:39 +0100902 r = copy_to_user((u64 __user *)(long)reg->addr,
903 &to_book3s(vcpu)->hior, sizeof(u64));
Paul Mackerras31f34382011-12-12 12:26:50 +0000904 break;
905 default:
906 break;
907 }
908
909 return r;
910}
911
912int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
913{
914 int r = -EINVAL;
915
916 switch (reg->id) {
917 case KVM_REG_PPC_HIOR:
Alexander Grafb8e6f8a2012-03-13 19:59:39 +0100918 r = copy_from_user(&to_book3s(vcpu)->hior,
919 (u64 __user *)(long)reg->addr, sizeof(u64));
Paul Mackerras31f34382011-12-12 12:26:50 +0000920 if (!r)
921 to_book3s(vcpu)->hior_explicit = true;
922 break;
923 default:
924 break;
925 }
926
927 return r;
928}
929
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000930int kvmppc_core_check_processor_compat(void)
931{
932 return 0;
933}
934
935struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
936{
937 struct kvmppc_vcpu_book3s *vcpu_book3s;
938 struct kvm_vcpu *vcpu;
939 int err = -ENOMEM;
940 unsigned long p;
941
942 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
943 if (!vcpu_book3s)
944 goto out;
945
946 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
947 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
948 if (!vcpu_book3s->shadow_vcpu)
949 goto free_vcpu;
950
951 vcpu = &vcpu_book3s->vcpu;
952 err = kvm_vcpu_init(vcpu, kvm, id);
953 if (err)
954 goto free_shadow_vcpu;
955
956 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
957 /* the real shared page fills the last 4k of our page */
958 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
959 if (!p)
960 goto uninit_vcpu;
961
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000962#ifdef CONFIG_PPC_BOOK3S_64
963 /* default to book3s_64 (970fx) */
964 vcpu->arch.pvr = 0x3C0301;
965#else
966 /* default to book3s_32 (750) */
967 vcpu->arch.pvr = 0x84202;
968#endif
969 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
970 vcpu->arch.slb_nr = 64;
971
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000972 vcpu->arch.shadow_msr = MSR_USER64;
973
974 err = kvmppc_mmu_init(vcpu);
975 if (err < 0)
976 goto uninit_vcpu;
977
978 return vcpu;
979
980uninit_vcpu:
981 kvm_vcpu_uninit(vcpu);
982free_shadow_vcpu:
983 kfree(vcpu_book3s->shadow_vcpu);
984free_vcpu:
985 vfree(vcpu_book3s);
986out:
987 return ERR_PTR(err);
988}
989
990void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
991{
992 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
993
994 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
995 kvm_vcpu_uninit(vcpu);
996 kfree(vcpu_book3s->shadow_vcpu);
997 vfree(vcpu_book3s);
998}
999
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001000int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001001{
1002 int ret;
1003 double fpr[32][TS_FPRWIDTH];
1004 unsigned int fpscr;
1005 int fpexc_mode;
1006#ifdef CONFIG_ALTIVEC
1007 vector128 vr[32];
1008 vector128 vscr;
1009 unsigned long uninitialized_var(vrsave);
1010 int used_vr;
1011#endif
1012#ifdef CONFIG_VSX
1013 int used_vsr;
1014#endif
1015 ulong ext_msr;
1016
Alexander Graf7d827142011-12-09 15:46:21 +01001017 preempt_disable();
1018
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001019 /* Check if we can run the vcpu at all */
1020 if (!vcpu->arch.sane) {
1021 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
Alexander Graf7d827142011-12-09 15:46:21 +01001022 ret = -EINVAL;
1023 goto out;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001024 }
1025
Alexander Grafe371f712011-12-19 13:36:55 +01001026 /*
1027 * Interrupts could be timers for the guest which we have to inject
1028 * again, so let's postpone them until we're in the guest and if we
1029 * really did time things so badly, then we just exit again due to
1030 * a host external interrupt.
1031 */
1032 __hard_irq_disable();
Alexander Graf03d25c52012-08-10 12:28:50 +02001033 if (kvmppc_prepare_to_enter(vcpu)) {
Alexander Grafe371f712011-12-19 13:36:55 +01001034 __hard_irq_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001035 kvm_run->exit_reason = KVM_EXIT_INTR;
Alexander Graf7d827142011-12-09 15:46:21 +01001036 ret = -EINTR;
1037 goto out;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001038 }
1039
1040 /* Save FPU state in stack */
1041 if (current->thread.regs->msr & MSR_FP)
1042 giveup_fpu(current);
1043 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1044 fpscr = current->thread.fpscr.val;
1045 fpexc_mode = current->thread.fpexc_mode;
1046
1047#ifdef CONFIG_ALTIVEC
1048 /* Save Altivec state in stack */
1049 used_vr = current->thread.used_vr;
1050 if (used_vr) {
1051 if (current->thread.regs->msr & MSR_VEC)
1052 giveup_altivec(current);
1053 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1054 vscr = current->thread.vscr;
1055 vrsave = current->thread.vrsave;
1056 }
1057#endif
1058
1059#ifdef CONFIG_VSX
1060 /* Save VSX state in stack */
1061 used_vsr = current->thread.used_vsr;
1062 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
1063 __giveup_vsx(current);
1064#endif
1065
1066 /* Remember the MSR with disabled extensions */
1067 ext_msr = current->thread.regs->msr;
1068
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001069 /* Preload FPU if it's enabled */
1070 if (vcpu->arch.shared->msr & MSR_FP)
1071 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1072
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001073 kvm_guest_enter();
1074
1075 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
1076
1077 kvm_guest_exit();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001078
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001079 current->thread.regs->msr = ext_msr;
1080
1081 /* Make sure we save the guest FPU/Altivec/VSX state */
1082 kvmppc_giveup_ext(vcpu, MSR_FP);
1083 kvmppc_giveup_ext(vcpu, MSR_VEC);
1084 kvmppc_giveup_ext(vcpu, MSR_VSX);
1085
1086 /* Restore FPU state from stack */
1087 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1088 current->thread.fpscr.val = fpscr;
1089 current->thread.fpexc_mode = fpexc_mode;
1090
1091#ifdef CONFIG_ALTIVEC
1092 /* Restore Altivec state from stack */
1093 if (used_vr && current->thread.used_vr) {
1094 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1095 current->thread.vscr = vscr;
1096 current->thread.vrsave = vrsave;
1097 }
1098 current->thread.used_vr = used_vr;
1099#endif
1100
1101#ifdef CONFIG_VSX
1102 current->thread.used_vsr = used_vsr;
1103#endif
1104
Alexander Graf7d827142011-12-09 15:46:21 +01001105out:
1106 preempt_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001107 return ret;
1108}
1109
Paul Mackerras82ed3612011-12-15 02:03:22 +00001110/*
1111 * Get (and clear) the dirty memory log for a memory slot.
1112 */
1113int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1114 struct kvm_dirty_log *log)
1115{
1116 struct kvm_memory_slot *memslot;
1117 struct kvm_vcpu *vcpu;
1118 ulong ga, ga_end;
1119 int is_dirty = 0;
1120 int r;
1121 unsigned long n;
1122
1123 mutex_lock(&kvm->slots_lock);
1124
1125 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1126 if (r)
1127 goto out;
1128
1129 /* If nothing is dirty, don't bother messing with page tables. */
1130 if (is_dirty) {
1131 memslot = id_to_memslot(kvm->memslots, log->slot);
1132
1133 ga = memslot->base_gfn << PAGE_SHIFT;
1134 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1135
1136 kvm_for_each_vcpu(n, vcpu, kvm)
1137 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1138
1139 n = kvm_dirty_bitmap_bytes(memslot);
1140 memset(memslot->dirty_bitmap, 0, n);
1141 }
1142
1143 r = 0;
1144out:
1145 mutex_unlock(&kvm->slots_lock);
1146 return r;
1147}
1148
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001149#ifdef CONFIG_PPC64
1150int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1151{
1152 /* No flags */
1153 info->flags = 0;
1154
1155 /* SLB is always 64 entries */
1156 info->slb_size = 64;
1157
1158 /* Standard 4k base page size segment */
1159 info->sps[0].page_shift = 12;
1160 info->sps[0].slb_enc = 0;
1161 info->sps[0].enc[0].page_shift = 12;
1162 info->sps[0].enc[0].pte_enc = 0;
1163
1164 /* Standard 16M large page size segment */
1165 info->sps[1].page_shift = 24;
1166 info->sps[1].slb_enc = SLB_VSID_L;
1167 info->sps[1].enc[0].page_shift = 24;
1168 info->sps[1].enc[0].pte_enc = 0;
1169
1170 return 0;
1171}
1172#endif /* CONFIG_PPC64 */
1173
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001174int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1175 struct kvm_userspace_memory_region *mem)
1176{
1177 return 0;
1178}
1179
1180void kvmppc_core_commit_memory_region(struct kvm *kvm,
1181 struct kvm_userspace_memory_region *mem)
1182{
1183}
1184
1185int kvmppc_core_init_vm(struct kvm *kvm)
1186{
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001187#ifdef CONFIG_PPC64
1188 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
1189#endif
1190
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001191 return 0;
1192}
1193
1194void kvmppc_core_destroy_vm(struct kvm *kvm)
1195{
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001196#ifdef CONFIG_PPC64
1197 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
1198#endif
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001199}
1200
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001201static int kvmppc_book3s_init(void)
1202{
1203 int r;
1204
1205 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1206 THIS_MODULE);
1207
1208 if (r)
1209 return r;
1210
1211 r = kvmppc_mmu_hpte_sysinit();
1212
1213 return r;
1214}
1215
1216static void kvmppc_book3s_exit(void)
1217{
1218 kvmppc_mmu_hpte_sysexit();
1219 kvm_exit();
1220}
1221
1222module_init(kvmppc_book3s_init);
1223module_exit(kvmppc_book3s_exit);