blob: a7f031b6fc98f55aa8a6d5d8cab8bd59dc27c32d [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
91static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
92{
93 ulong smsr = vcpu->arch.shared->msr;
94
95 /* Guest MSR values */
96 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
97 /* Process MSR values */
98 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
99 /* External providers the guest reserved */
100 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
101 /* 64-bit Process MSR values */
102#ifdef CONFIG_PPC_BOOK3S_64
103 smsr |= MSR_ISF | MSR_HV;
104#endif
105 vcpu->arch.shadow_msr = smsr;
106}
107
108void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
109{
110 ulong old_msr = vcpu->arch.shared->msr;
111
112#ifdef EXIT_DEBUG
113 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
114#endif
115
116 msr &= to_book3s(vcpu)->msr_mask;
117 vcpu->arch.shared->msr = msr;
118 kvmppc_recalc_shadow_msr(vcpu);
119
120 if (msr & MSR_POW) {
121 if (!vcpu->arch.pending_exceptions) {
122 kvm_vcpu_block(vcpu);
123 vcpu->stat.halt_wakeup++;
124
125 /* Unset POW bit after we woke up */
126 msr &= ~MSR_POW;
127 vcpu->arch.shared->msr = msr;
128 }
129 }
130
131 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
132 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
133 kvmppc_mmu_flush_segments(vcpu);
134 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
135
136 /* Preload magic page segment when in kernel mode */
137 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
138 struct kvm_vcpu_arch *a = &vcpu->arch;
139
140 if (msr & MSR_DR)
141 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
142 else
143 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
144 }
145 }
146
147 /* Preload FPU if it's enabled */
148 if (vcpu->arch.shared->msr & MSR_FP)
149 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
150}
151
152void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
153{
154 u32 host_pvr;
155
156 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
157 vcpu->arch.pvr = pvr;
158#ifdef CONFIG_PPC_BOOK3S_64
159 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
160 kvmppc_mmu_book3s_64_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200161 if (!to_book3s(vcpu)->hior_explicit)
162 to_book3s(vcpu)->hior = 0xfff00000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000163 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200164 vcpu->arch.cpu_type = KVM_CPU_3S_64;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000165 } else
166#endif
167 {
168 kvmppc_mmu_book3s_32_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200169 if (!to_book3s(vcpu)->hior_explicit)
170 to_book3s(vcpu)->hior = 0;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000171 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200172 vcpu->arch.cpu_type = KVM_CPU_3S_32;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000173 }
174
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200175 kvmppc_sanity_check(vcpu);
176
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000177 /* If we are in hypervisor level on 970, we can tell the CPU to
178 * treat DCBZ as 32 bytes store */
179 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
180 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
181 !strcmp(cur_cpu_spec->platform, "ppc970"))
182 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
183
184 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
185 really needs them in a VM on Cell and force disable them. */
186 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
187 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
188
189#ifdef CONFIG_PPC_BOOK3S_32
190 /* 32 bit Book3S always has 32 byte dcbz */
191 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
192#endif
193
194 /* On some CPUs we can execute paired single operations natively */
195 asm ( "mfpvr %0" : "=r"(host_pvr));
196 switch (host_pvr) {
197 case 0x00080200: /* lonestar 2.0 */
198 case 0x00088202: /* lonestar 2.2 */
199 case 0x70000100: /* gekko 1.0 */
200 case 0x00080100: /* gekko 2.0 */
201 case 0x00083203: /* gekko 2.3a */
202 case 0x00083213: /* gekko 2.3b */
203 case 0x00083204: /* gekko 2.4 */
204 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
205 case 0x00087200: /* broadway */
206 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
207 /* Enable HID2.PSE - in case we need it later */
208 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
209 }
210}
211
212/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
213 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
214 * emulate 32 bytes dcbz length.
215 *
216 * The Book3s_64 inventors also realized this case and implemented a special bit
217 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
218 *
219 * My approach here is to patch the dcbz instruction on executing pages.
220 */
221static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
222{
223 struct page *hpage;
224 u64 hpage_offset;
225 u32 *page;
226 int i;
227
228 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
229 if (is_error_page(hpage)) {
230 kvm_release_page_clean(hpage);
231 return;
232 }
233
234 hpage_offset = pte->raddr & ~PAGE_MASK;
235 hpage_offset &= ~0xFFFULL;
236 hpage_offset /= 4;
237
238 get_page(hpage);
Cong Wang2480b202011-11-25 23:14:16 +0800239 page = kmap_atomic(hpage);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000240
241 /* patch dcbz into reserved instruction, so we trap */
242 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
243 if ((page[i] & 0xff0007ff) == INS_DCBZ)
244 page[i] &= 0xfffffff7;
245
Cong Wang2480b202011-11-25 23:14:16 +0800246 kunmap_atomic(page);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000247 put_page(hpage);
248}
249
250static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
251{
252 ulong mp_pa = vcpu->arch.magic_page_pa;
253
254 if (unlikely(mp_pa) &&
255 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
256 return 1;
257 }
258
259 return kvm_is_visible_gfn(vcpu->kvm, gfn);
260}
261
262int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
263 ulong eaddr, int vec)
264{
265 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
266 int r = RESUME_GUEST;
267 int relocated;
268 int page_found = 0;
269 struct kvmppc_pte pte;
270 bool is_mmio = false;
271 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
272 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
273 u64 vsid;
274
275 relocated = data ? dr : ir;
276
277 /* Resolve real address if translation turned on */
278 if (relocated) {
279 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
280 } else {
281 pte.may_execute = true;
282 pte.may_read = true;
283 pte.may_write = true;
284 pte.raddr = eaddr & KVM_PAM;
285 pte.eaddr = eaddr;
286 pte.vpage = eaddr >> 12;
287 }
288
289 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
290 case 0:
291 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
292 break;
293 case MSR_DR:
294 case MSR_IR:
295 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
296
297 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
298 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
299 else
300 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
301 pte.vpage |= vsid;
302
303 if (vsid == -1)
304 page_found = -EINVAL;
305 break;
306 }
307
308 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
309 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
310 /*
311 * If we do the dcbz hack, we have to NX on every execution,
312 * so we can patch the executing code. This renders our guest
313 * NX-less.
314 */
315 pte.may_execute = !data;
316 }
317
318 if (page_found == -ENOENT) {
319 /* Page not found in guest PTE entries */
Alexander Graf468a12c2011-12-09 14:44:13 +0100320 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000321 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100322 vcpu->arch.shared->dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000323 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100324 (svcpu->shadow_srr1 & 0x00000000f8000000ULL);
325 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000326 kvmppc_book3s_queue_irqprio(vcpu, vec);
327 } else if (page_found == -EPERM) {
328 /* Storage protection */
Alexander Graf468a12c2011-12-09 14:44:13 +0100329 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000330 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100331 vcpu->arch.shared->dsisr = svcpu->fault_dsisr & ~DSISR_NOHPTE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000332 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
333 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100334 svcpu->shadow_srr1 & 0x00000000f8000000ULL;
335 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000336 kvmppc_book3s_queue_irqprio(vcpu, vec);
337 } else if (page_found == -EINVAL) {
338 /* Page not found in guest SLB */
339 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
340 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
341 } else if (!is_mmio &&
342 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
343 /* The guest's PTE is not mapped yet. Map on the host */
344 kvmppc_mmu_map_page(vcpu, &pte);
345 if (data)
346 vcpu->stat.sp_storage++;
347 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
348 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
349 kvmppc_patch_dcbz(vcpu, &pte);
350 } else {
351 /* MMIO */
352 vcpu->stat.mmio_exits++;
353 vcpu->arch.paddr_accessed = pte.raddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100354 vcpu->arch.vaddr_accessed = pte.eaddr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000355 r = kvmppc_emulate_mmio(run, vcpu);
356 if ( r == RESUME_HOST_NV )
357 r = RESUME_HOST;
358 }
359
360 return r;
361}
362
363static inline int get_fpr_index(int i)
364{
365#ifdef CONFIG_VSX
366 i *= 2;
367#endif
368 return i;
369}
370
371/* Give up external provider (FPU, Altivec, VSX) */
372void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
373{
374 struct thread_struct *t = &current->thread;
375 u64 *vcpu_fpr = vcpu->arch.fpr;
376#ifdef CONFIG_VSX
377 u64 *vcpu_vsx = vcpu->arch.vsr;
378#endif
379 u64 *thread_fpr = (u64*)t->fpr;
380 int i;
381
382 if (!(vcpu->arch.guest_owned_ext & msr))
383 return;
384
385#ifdef DEBUG_EXT
386 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
387#endif
388
389 switch (msr) {
390 case MSR_FP:
391 giveup_fpu(current);
392 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
393 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
394
395 vcpu->arch.fpscr = t->fpscr.val;
396 break;
397 case MSR_VEC:
398#ifdef CONFIG_ALTIVEC
399 giveup_altivec(current);
400 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
401 vcpu->arch.vscr = t->vscr;
402#endif
403 break;
404 case MSR_VSX:
405#ifdef CONFIG_VSX
406 __giveup_vsx(current);
407 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
408 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
409#endif
410 break;
411 default:
412 BUG();
413 }
414
415 vcpu->arch.guest_owned_ext &= ~msr;
416 current->thread.regs->msr &= ~msr;
417 kvmppc_recalc_shadow_msr(vcpu);
418}
419
420static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
421{
422 ulong srr0 = kvmppc_get_pc(vcpu);
423 u32 last_inst = kvmppc_get_last_inst(vcpu);
424 int ret;
425
426 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
427 if (ret == -ENOENT) {
428 ulong msr = vcpu->arch.shared->msr;
429
430 msr = kvmppc_set_field(msr, 33, 33, 1);
431 msr = kvmppc_set_field(msr, 34, 36, 0);
432 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
433 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
434 return EMULATE_AGAIN;
435 }
436
437 return EMULATE_DONE;
438}
439
440static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
441{
442
443 /* Need to do paired single emulation? */
444 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
445 return EMULATE_DONE;
446
447 /* Read out the instruction */
448 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
449 /* Need to emulate */
450 return EMULATE_FAIL;
451
452 return EMULATE_AGAIN;
453}
454
455/* Handle external providers (FPU, Altivec, VSX) */
456static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
457 ulong msr)
458{
459 struct thread_struct *t = &current->thread;
460 u64 *vcpu_fpr = vcpu->arch.fpr;
461#ifdef CONFIG_VSX
462 u64 *vcpu_vsx = vcpu->arch.vsr;
463#endif
464 u64 *thread_fpr = (u64*)t->fpr;
465 int i;
466
467 /* When we have paired singles, we emulate in software */
468 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
469 return RESUME_GUEST;
470
471 if (!(vcpu->arch.shared->msr & msr)) {
472 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
473 return RESUME_GUEST;
474 }
475
476 /* We already own the ext */
477 if (vcpu->arch.guest_owned_ext & msr) {
478 return RESUME_GUEST;
479 }
480
481#ifdef DEBUG_EXT
482 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
483#endif
484
485 current->thread.regs->msr |= msr;
486
487 switch (msr) {
488 case MSR_FP:
489 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
490 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
491
492 t->fpscr.val = vcpu->arch.fpscr;
493 t->fpexc_mode = 0;
494 kvmppc_load_up_fpu();
495 break;
496 case MSR_VEC:
497#ifdef CONFIG_ALTIVEC
498 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
499 t->vscr = vcpu->arch.vscr;
500 t->vrsave = -1;
501 kvmppc_load_up_altivec();
502#endif
503 break;
504 case MSR_VSX:
505#ifdef CONFIG_VSX
506 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
507 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
508 kvmppc_load_up_vsx();
509#endif
510 break;
511 default:
512 BUG();
513 }
514
515 vcpu->arch.guest_owned_ext |= msr;
516
517 kvmppc_recalc_shadow_msr(vcpu);
518
519 return RESUME_GUEST;
520}
521
522int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
523 unsigned int exit_nr)
524{
525 int r = RESUME_HOST;
526
527 vcpu->stat.sum_exits++;
528
529 run->exit_reason = KVM_EXIT_UNKNOWN;
530 run->ready_for_interrupt_injection = 1;
531
532 trace_kvm_book3s_exit(exit_nr, vcpu);
Alexander Graf7d827142011-12-09 15:46:21 +0100533 preempt_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000534 kvm_resched(vcpu);
535 switch (exit_nr) {
536 case BOOK3S_INTERRUPT_INST_STORAGE:
Alexander Graf468a12c2011-12-09 14:44:13 +0100537 {
538 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
539 ulong shadow_srr1 = svcpu->shadow_srr1;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000540 vcpu->stat.pf_instruc++;
541
542#ifdef CONFIG_PPC_BOOK3S_32
543 /* We set segments as unused segments when invalidating them. So
544 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100545 if (svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000546 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
547 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100548 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000549 break;
550 }
551#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100552 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000553
554 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graf468a12c2011-12-09 14:44:13 +0100555 if (shadow_srr1 & 0x40000000) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000556 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
557 vcpu->stat.sp_instruc++;
558 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
559 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
560 /*
561 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
562 * so we can't use the NX bit inside the guest. Let's cross our fingers,
563 * that no guest that needs the dcbz hack does NX.
564 */
565 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
566 r = RESUME_GUEST;
567 } else {
Alexander Graf468a12c2011-12-09 14:44:13 +0100568 vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000569 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
570 r = RESUME_GUEST;
571 }
572 break;
Alexander Graf468a12c2011-12-09 14:44:13 +0100573 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000574 case BOOK3S_INTERRUPT_DATA_STORAGE:
575 {
576 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100577 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
578 u32 fault_dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000579 vcpu->stat.pf_storage++;
580
581#ifdef CONFIG_PPC_BOOK3S_32
582 /* We set segments as unused segments when invalidating them. So
583 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100584 if ((svcpu->sr[dar >> SID_SHIFT]) == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000585 kvmppc_mmu_map_segment(vcpu, dar);
586 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100587 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000588 break;
589 }
590#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100591 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000592
593 /* The only case we need to handle is missing shadow PTEs */
Alexander Graf468a12c2011-12-09 14:44:13 +0100594 if (fault_dsisr & DSISR_NOHPTE) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000595 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
596 } else {
597 vcpu->arch.shared->dar = dar;
Alexander Graf468a12c2011-12-09 14:44:13 +0100598 vcpu->arch.shared->dsisr = fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000599 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
600 r = RESUME_GUEST;
601 }
602 break;
603 }
604 case BOOK3S_INTERRUPT_DATA_SEGMENT:
605 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
606 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
607 kvmppc_book3s_queue_irqprio(vcpu,
608 BOOK3S_INTERRUPT_DATA_SEGMENT);
609 }
610 r = RESUME_GUEST;
611 break;
612 case BOOK3S_INTERRUPT_INST_SEGMENT:
613 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
614 kvmppc_book3s_queue_irqprio(vcpu,
615 BOOK3S_INTERRUPT_INST_SEGMENT);
616 }
617 r = RESUME_GUEST;
618 break;
619 /* We're good on these - the host merely wanted to get our attention */
620 case BOOK3S_INTERRUPT_DECREMENTER:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100621 case BOOK3S_INTERRUPT_HV_DECREMENTER:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000622 vcpu->stat.dec_exits++;
623 r = RESUME_GUEST;
624 break;
625 case BOOK3S_INTERRUPT_EXTERNAL:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100626 case BOOK3S_INTERRUPT_EXTERNAL_LEVEL:
627 case BOOK3S_INTERRUPT_EXTERNAL_HV:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000628 vcpu->stat.ext_intr_exits++;
629 r = RESUME_GUEST;
630 break;
631 case BOOK3S_INTERRUPT_PERFMON:
632 r = RESUME_GUEST;
633 break;
634 case BOOK3S_INTERRUPT_PROGRAM:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100635 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000636 {
637 enum emulation_result er;
Alexander Graf468a12c2011-12-09 14:44:13 +0100638 struct kvmppc_book3s_shadow_vcpu *svcpu;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000639 ulong flags;
640
641program_interrupt:
Alexander Graf468a12c2011-12-09 14:44:13 +0100642 svcpu = svcpu_get(vcpu);
643 flags = svcpu->shadow_srr1 & 0x1f0000ull;
644 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000645
646 if (vcpu->arch.shared->msr & MSR_PR) {
647#ifdef EXIT_DEBUG
648 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
649#endif
650 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
651 (INS_DCBZ & 0xfffffff7)) {
652 kvmppc_core_queue_program(vcpu, flags);
653 r = RESUME_GUEST;
654 break;
655 }
656 }
657
658 vcpu->stat.emulated_inst_exits++;
659 er = kvmppc_emulate_instruction(run, vcpu);
660 switch (er) {
661 case EMULATE_DONE:
662 r = RESUME_GUEST_NV;
663 break;
664 case EMULATE_AGAIN:
665 r = RESUME_GUEST;
666 break;
667 case EMULATE_FAIL:
668 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
669 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
670 kvmppc_core_queue_program(vcpu, flags);
671 r = RESUME_GUEST;
672 break;
673 case EMULATE_DO_MMIO:
674 run->exit_reason = KVM_EXIT_MMIO;
675 r = RESUME_HOST_NV;
676 break;
677 default:
678 BUG();
679 }
680 break;
681 }
682 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafa668f2b2011-08-08 17:26:24 +0200683 if (vcpu->arch.papr_enabled &&
684 (kvmppc_get_last_inst(vcpu) == 0x44000022) &&
685 !(vcpu->arch.shared->msr & MSR_PR)) {
686 /* SC 1 papr hypercalls */
687 ulong cmd = kvmppc_get_gpr(vcpu, 3);
688 int i;
689
Andreas Schwab96f38d72011-11-08 07:17:39 +0000690#ifdef CONFIG_KVM_BOOK3S_64_PR
Alexander Grafa668f2b2011-08-08 17:26:24 +0200691 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
692 r = RESUME_GUEST;
693 break;
694 }
Andreas Schwab96f38d72011-11-08 07:17:39 +0000695#endif
Alexander Grafa668f2b2011-08-08 17:26:24 +0200696
697 run->papr_hcall.nr = cmd;
698 for (i = 0; i < 9; ++i) {
699 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
700 run->papr_hcall.args[i] = gpr;
701 }
702 run->exit_reason = KVM_EXIT_PAPR_HCALL;
703 vcpu->arch.hcall_needed = 1;
704 r = RESUME_HOST;
705 } else if (vcpu->arch.osi_enabled &&
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000706 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
707 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
708 /* MOL hypercalls */
709 u64 *gprs = run->osi.gprs;
710 int i;
711
712 run->exit_reason = KVM_EXIT_OSI;
713 for (i = 0; i < 32; i++)
714 gprs[i] = kvmppc_get_gpr(vcpu, i);
715 vcpu->arch.osi_needed = 1;
716 r = RESUME_HOST_NV;
717 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
718 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
719 /* KVM PV hypercalls */
720 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
721 r = RESUME_GUEST;
722 } else {
723 /* Guest syscalls */
724 vcpu->stat.syscall_exits++;
725 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
726 r = RESUME_GUEST;
727 }
728 break;
729 case BOOK3S_INTERRUPT_FP_UNAVAIL:
730 case BOOK3S_INTERRUPT_ALTIVEC:
731 case BOOK3S_INTERRUPT_VSX:
732 {
733 int ext_msr = 0;
734
735 switch (exit_nr) {
736 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
737 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
738 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
739 }
740
741 switch (kvmppc_check_ext(vcpu, exit_nr)) {
742 case EMULATE_DONE:
743 /* everything ok - let's enable the ext */
744 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
745 break;
746 case EMULATE_FAIL:
747 /* we need to emulate this instruction */
748 goto program_interrupt;
749 break;
750 default:
751 /* nothing to worry about - go again */
752 break;
753 }
754 break;
755 }
756 case BOOK3S_INTERRUPT_ALIGNMENT:
757 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
758 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
759 kvmppc_get_last_inst(vcpu));
760 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
761 kvmppc_get_last_inst(vcpu));
762 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
763 }
764 r = RESUME_GUEST;
765 break;
766 case BOOK3S_INTERRUPT_MACHINE_CHECK:
767 case BOOK3S_INTERRUPT_TRACE:
768 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
769 r = RESUME_GUEST;
770 break;
771 default:
Alexander Graf468a12c2011-12-09 14:44:13 +0100772 {
773 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
774 ulong shadow_srr1 = svcpu->shadow_srr1;
775 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000776 /* Ugh - bork here! What did we get? */
777 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Graf468a12c2011-12-09 14:44:13 +0100778 exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000779 r = RESUME_HOST;
780 BUG();
781 break;
782 }
Alexander Graf468a12c2011-12-09 14:44:13 +0100783 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000784
Alexander Graf592f5d82012-03-13 23:31:02 +0100785 preempt_disable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000786 if (!(r & RESUME_HOST)) {
787 /* To avoid clobbering exit_reason, only check for signals if
788 * we aren't already exiting to userspace for some other
789 * reason. */
Alexander Grafe371f712011-12-19 13:36:55 +0100790
791 /*
792 * Interrupts could be timers for the guest which we have to
793 * inject again, so let's postpone them until we're in the guest
794 * and if we really did time things so badly, then we just exit
795 * again due to a host external interrupt.
796 */
797 __hard_irq_disable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000798 if (signal_pending(current)) {
Alexander Grafe371f712011-12-19 13:36:55 +0100799 __hard_irq_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000800#ifdef EXIT_DEBUG
801 printk(KERN_EMERG "KVM: Going back to host\n");
802#endif
803 vcpu->stat.signal_exits++;
804 run->exit_reason = KVM_EXIT_INTR;
805 r = -EINTR;
806 } else {
807 /* In case an interrupt came in that was triggered
808 * from userspace (like DEC), we need to check what
809 * to inject now! */
Scott Wood7e28e60e2011-11-08 18:23:20 -0600810 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000811 }
812 }
813
814 trace_kvm_book3s_reenter(r, vcpu);
815
816 return r;
817}
818
819int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
820 struct kvm_sregs *sregs)
821{
822 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
823 int i;
824
825 sregs->pvr = vcpu->arch.pvr;
826
827 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
828 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
829 for (i = 0; i < 64; i++) {
830 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
831 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
832 }
833 } else {
834 for (i = 0; i < 16; i++)
835 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
836
837 for (i = 0; i < 8; i++) {
838 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
839 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
840 }
841 }
842
843 return 0;
844}
845
846int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
847 struct kvm_sregs *sregs)
848{
849 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
850 int i;
851
852 kvmppc_set_pvr(vcpu, sregs->pvr);
853
854 vcpu3s->sdr1 = sregs->u.s.sdr1;
855 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
856 for (i = 0; i < 64; i++) {
857 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
858 sregs->u.s.ppc64.slb[i].slbe);
859 }
860 } else {
861 for (i = 0; i < 16; i++) {
862 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
863 }
864 for (i = 0; i < 8; i++) {
865 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
866 (u32)sregs->u.s.ppc32.ibat[i]);
867 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
868 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
869 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
870 (u32)sregs->u.s.ppc32.dbat[i]);
871 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
872 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
873 }
874 }
875
876 /* Flush the MMU after messing with the segments */
877 kvmppc_mmu_pte_flush(vcpu, 0, 0);
878
879 return 0;
880}
881
Paul Mackerras31f34382011-12-12 12:26:50 +0000882int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
883{
884 int r = -EINVAL;
885
886 switch (reg->id) {
887 case KVM_REG_PPC_HIOR:
Alexander Grafb8e6f8a2012-03-13 19:59:39 +0100888 r = copy_to_user((u64 __user *)(long)reg->addr,
889 &to_book3s(vcpu)->hior, sizeof(u64));
Paul Mackerras31f34382011-12-12 12:26:50 +0000890 break;
891 default:
892 break;
893 }
894
895 return r;
896}
897
898int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
899{
900 int r = -EINVAL;
901
902 switch (reg->id) {
903 case KVM_REG_PPC_HIOR:
Alexander Grafb8e6f8a2012-03-13 19:59:39 +0100904 r = copy_from_user(&to_book3s(vcpu)->hior,
905 (u64 __user *)(long)reg->addr, sizeof(u64));
Paul Mackerras31f34382011-12-12 12:26:50 +0000906 if (!r)
907 to_book3s(vcpu)->hior_explicit = true;
908 break;
909 default:
910 break;
911 }
912
913 return r;
914}
915
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000916int kvmppc_core_check_processor_compat(void)
917{
918 return 0;
919}
920
921struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
922{
923 struct kvmppc_vcpu_book3s *vcpu_book3s;
924 struct kvm_vcpu *vcpu;
925 int err = -ENOMEM;
926 unsigned long p;
927
928 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
929 if (!vcpu_book3s)
930 goto out;
931
932 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
933 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
934 if (!vcpu_book3s->shadow_vcpu)
935 goto free_vcpu;
936
937 vcpu = &vcpu_book3s->vcpu;
938 err = kvm_vcpu_init(vcpu, kvm, id);
939 if (err)
940 goto free_shadow_vcpu;
941
942 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
943 /* the real shared page fills the last 4k of our page */
944 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
945 if (!p)
946 goto uninit_vcpu;
947
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000948#ifdef CONFIG_PPC_BOOK3S_64
949 /* default to book3s_64 (970fx) */
950 vcpu->arch.pvr = 0x3C0301;
951#else
952 /* default to book3s_32 (750) */
953 vcpu->arch.pvr = 0x84202;
954#endif
955 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
956 vcpu->arch.slb_nr = 64;
957
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000958 vcpu->arch.shadow_msr = MSR_USER64;
959
960 err = kvmppc_mmu_init(vcpu);
961 if (err < 0)
962 goto uninit_vcpu;
963
964 return vcpu;
965
966uninit_vcpu:
967 kvm_vcpu_uninit(vcpu);
968free_shadow_vcpu:
969 kfree(vcpu_book3s->shadow_vcpu);
970free_vcpu:
971 vfree(vcpu_book3s);
972out:
973 return ERR_PTR(err);
974}
975
976void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
977{
978 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
979
980 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
981 kvm_vcpu_uninit(vcpu);
982 kfree(vcpu_book3s->shadow_vcpu);
983 vfree(vcpu_book3s);
984}
985
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000986int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000987{
988 int ret;
989 double fpr[32][TS_FPRWIDTH];
990 unsigned int fpscr;
991 int fpexc_mode;
992#ifdef CONFIG_ALTIVEC
993 vector128 vr[32];
994 vector128 vscr;
995 unsigned long uninitialized_var(vrsave);
996 int used_vr;
997#endif
998#ifdef CONFIG_VSX
999 int used_vsr;
1000#endif
1001 ulong ext_msr;
1002
Alexander Graf7d827142011-12-09 15:46:21 +01001003 preempt_disable();
1004
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001005 /* Check if we can run the vcpu at all */
1006 if (!vcpu->arch.sane) {
1007 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
Alexander Graf7d827142011-12-09 15:46:21 +01001008 ret = -EINVAL;
1009 goto out;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001010 }
1011
Scott Wood25051b5a2011-11-08 18:23:23 -06001012 kvmppc_core_prepare_to_enter(vcpu);
1013
Alexander Grafe371f712011-12-19 13:36:55 +01001014 /*
1015 * Interrupts could be timers for the guest which we have to inject
1016 * again, so let's postpone them until we're in the guest and if we
1017 * really did time things so badly, then we just exit again due to
1018 * a host external interrupt.
1019 */
1020 __hard_irq_disable();
1021
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001022 /* No need to go into the guest when all we do is going out */
1023 if (signal_pending(current)) {
Alexander Grafe371f712011-12-19 13:36:55 +01001024 __hard_irq_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001025 kvm_run->exit_reason = KVM_EXIT_INTR;
Alexander Graf7d827142011-12-09 15:46:21 +01001026 ret = -EINTR;
1027 goto out;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001028 }
1029
1030 /* Save FPU state in stack */
1031 if (current->thread.regs->msr & MSR_FP)
1032 giveup_fpu(current);
1033 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1034 fpscr = current->thread.fpscr.val;
1035 fpexc_mode = current->thread.fpexc_mode;
1036
1037#ifdef CONFIG_ALTIVEC
1038 /* Save Altivec state in stack */
1039 used_vr = current->thread.used_vr;
1040 if (used_vr) {
1041 if (current->thread.regs->msr & MSR_VEC)
1042 giveup_altivec(current);
1043 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1044 vscr = current->thread.vscr;
1045 vrsave = current->thread.vrsave;
1046 }
1047#endif
1048
1049#ifdef CONFIG_VSX
1050 /* Save VSX state in stack */
1051 used_vsr = current->thread.used_vsr;
1052 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
1053 __giveup_vsx(current);
1054#endif
1055
1056 /* Remember the MSR with disabled extensions */
1057 ext_msr = current->thread.regs->msr;
1058
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001059 /* Preload FPU if it's enabled */
1060 if (vcpu->arch.shared->msr & MSR_FP)
1061 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1062
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001063 kvm_guest_enter();
1064
1065 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
1066
1067 kvm_guest_exit();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001068
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001069 current->thread.regs->msr = ext_msr;
1070
1071 /* Make sure we save the guest FPU/Altivec/VSX state */
1072 kvmppc_giveup_ext(vcpu, MSR_FP);
1073 kvmppc_giveup_ext(vcpu, MSR_VEC);
1074 kvmppc_giveup_ext(vcpu, MSR_VSX);
1075
1076 /* Restore FPU state from stack */
1077 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1078 current->thread.fpscr.val = fpscr;
1079 current->thread.fpexc_mode = fpexc_mode;
1080
1081#ifdef CONFIG_ALTIVEC
1082 /* Restore Altivec state from stack */
1083 if (used_vr && current->thread.used_vr) {
1084 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1085 current->thread.vscr = vscr;
1086 current->thread.vrsave = vrsave;
1087 }
1088 current->thread.used_vr = used_vr;
1089#endif
1090
1091#ifdef CONFIG_VSX
1092 current->thread.used_vsr = used_vsr;
1093#endif
1094
Alexander Graf7d827142011-12-09 15:46:21 +01001095out:
1096 preempt_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001097 return ret;
1098}
1099
Paul Mackerras82ed3612011-12-15 02:03:22 +00001100/*
1101 * Get (and clear) the dirty memory log for a memory slot.
1102 */
1103int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1104 struct kvm_dirty_log *log)
1105{
1106 struct kvm_memory_slot *memslot;
1107 struct kvm_vcpu *vcpu;
1108 ulong ga, ga_end;
1109 int is_dirty = 0;
1110 int r;
1111 unsigned long n;
1112
1113 mutex_lock(&kvm->slots_lock);
1114
1115 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1116 if (r)
1117 goto out;
1118
1119 /* If nothing is dirty, don't bother messing with page tables. */
1120 if (is_dirty) {
1121 memslot = id_to_memslot(kvm->memslots, log->slot);
1122
1123 ga = memslot->base_gfn << PAGE_SHIFT;
1124 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1125
1126 kvm_for_each_vcpu(n, vcpu, kvm)
1127 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1128
1129 n = kvm_dirty_bitmap_bytes(memslot);
1130 memset(memslot->dirty_bitmap, 0, n);
1131 }
1132
1133 r = 0;
1134out:
1135 mutex_unlock(&kvm->slots_lock);
1136 return r;
1137}
1138
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001139int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1140 struct kvm_userspace_memory_region *mem)
1141{
1142 return 0;
1143}
1144
1145void kvmppc_core_commit_memory_region(struct kvm *kvm,
1146 struct kvm_userspace_memory_region *mem)
1147{
1148}
1149
1150int kvmppc_core_init_vm(struct kvm *kvm)
1151{
1152 return 0;
1153}
1154
1155void kvmppc_core_destroy_vm(struct kvm *kvm)
1156{
1157}
1158
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001159static int kvmppc_book3s_init(void)
1160{
1161 int r;
1162
1163 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1164 THIS_MODULE);
1165
1166 if (r)
1167 return r;
1168
1169 r = kvmppc_mmu_hpte_sysinit();
1170
1171 return r;
1172}
1173
1174static void kvmppc_book3s_exit(void)
1175{
1176 kvmppc_mmu_hpte_sysexit();
1177 kvm_exit();
1178}
1179
1180module_init(kvmppc_book3s_init);
1181module_exit(kvmppc_book3s_exit);