blob: 2da6704057278837c3f97b99f25df57156b2daf4 [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>
36#include <linux/gfp.h>
37#include <linux/sched.h>
38#include <linux/vmalloc.h>
39#include <linux/highmem.h>
40
41#include "trace.h"
42
43/* #define EXIT_DEBUG */
44/* #define DEBUG_EXT */
45
46static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
47 ulong msr);
48
49/* Some compatibility defines */
50#ifdef CONFIG_PPC_BOOK3S_32
51#define MSR_USER32 MSR_USER
52#define MSR_USER64 MSR_USER
53#define HW_PAGE_SIZE PAGE_SIZE
Alexander Grafe371f712011-12-19 13:36:55 +010054#define __hard_irq_disable local_irq_disable
55#define __hard_irq_enable local_irq_enable
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000056#endif
57
58void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
59{
60#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf468a12c2011-12-09 14:44:13 +010061 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
62 memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb));
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000063 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
64 sizeof(get_paca()->shadow_vcpu));
Alexander Graf468a12c2011-12-09 14:44:13 +010065 svcpu->slb_max = to_book3s(vcpu)->slb_shadow_max;
66 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000067#endif
68
69#ifdef CONFIG_PPC_BOOK3S_32
70 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
71#endif
72}
73
74void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
75{
76#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf468a12c2011-12-09 14:44:13 +010077 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
78 memcpy(to_book3s(vcpu)->slb_shadow, svcpu->slb, sizeof(svcpu->slb));
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000079 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
80 sizeof(get_paca()->shadow_vcpu));
Alexander Graf468a12c2011-12-09 14:44:13 +010081 to_book3s(vcpu)->slb_shadow_max = svcpu->slb_max;
82 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000083#endif
84
85 kvmppc_giveup_ext(vcpu, MSR_FP);
86 kvmppc_giveup_ext(vcpu, MSR_VEC);
87 kvmppc_giveup_ext(vcpu, MSR_VSX);
88}
89
90static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
91{
92 ulong smsr = vcpu->arch.shared->msr;
93
94 /* Guest MSR values */
95 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
96 /* Process MSR values */
97 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
98 /* External providers the guest reserved */
99 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
100 /* 64-bit Process MSR values */
101#ifdef CONFIG_PPC_BOOK3S_64
102 smsr |= MSR_ISF | MSR_HV;
103#endif
104 vcpu->arch.shadow_msr = smsr;
105}
106
107void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
108{
109 ulong old_msr = vcpu->arch.shared->msr;
110
111#ifdef EXIT_DEBUG
112 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
113#endif
114
115 msr &= to_book3s(vcpu)->msr_mask;
116 vcpu->arch.shared->msr = msr;
117 kvmppc_recalc_shadow_msr(vcpu);
118
119 if (msr & MSR_POW) {
120 if (!vcpu->arch.pending_exceptions) {
121 kvm_vcpu_block(vcpu);
122 vcpu->stat.halt_wakeup++;
123
124 /* Unset POW bit after we woke up */
125 msr &= ~MSR_POW;
126 vcpu->arch.shared->msr = msr;
127 }
128 }
129
130 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
131 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
132 kvmppc_mmu_flush_segments(vcpu);
133 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
134
135 /* Preload magic page segment when in kernel mode */
136 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
137 struct kvm_vcpu_arch *a = &vcpu->arch;
138
139 if (msr & MSR_DR)
140 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
141 else
142 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
143 }
144 }
145
146 /* Preload FPU if it's enabled */
147 if (vcpu->arch.shared->msr & MSR_FP)
148 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
149}
150
151void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
152{
153 u32 host_pvr;
154
155 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
156 vcpu->arch.pvr = pvr;
157#ifdef CONFIG_PPC_BOOK3S_64
158 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
159 kvmppc_mmu_book3s_64_init(vcpu);
Alexander Grafbb75c622011-11-17 15:26:35 +0100160 to_book3s(vcpu)->hior = 0xfff00000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000161 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200162 vcpu->arch.cpu_type = KVM_CPU_3S_64;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000163 } else
164#endif
165 {
166 kvmppc_mmu_book3s_32_init(vcpu);
Alexander Grafbb75c622011-11-17 15:26:35 +0100167 to_book3s(vcpu)->hior = 0;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000168 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200169 vcpu->arch.cpu_type = KVM_CPU_3S_32;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000170 }
171
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200172 kvmppc_sanity_check(vcpu);
173
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000174 /* If we are in hypervisor level on 970, we can tell the CPU to
175 * treat DCBZ as 32 bytes store */
176 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
177 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
178 !strcmp(cur_cpu_spec->platform, "ppc970"))
179 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
180
181 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
182 really needs them in a VM on Cell and force disable them. */
183 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
184 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
185
186#ifdef CONFIG_PPC_BOOK3S_32
187 /* 32 bit Book3S always has 32 byte dcbz */
188 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
189#endif
190
191 /* On some CPUs we can execute paired single operations natively */
192 asm ( "mfpvr %0" : "=r"(host_pvr));
193 switch (host_pvr) {
194 case 0x00080200: /* lonestar 2.0 */
195 case 0x00088202: /* lonestar 2.2 */
196 case 0x70000100: /* gekko 1.0 */
197 case 0x00080100: /* gekko 2.0 */
198 case 0x00083203: /* gekko 2.3a */
199 case 0x00083213: /* gekko 2.3b */
200 case 0x00083204: /* gekko 2.4 */
201 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
202 case 0x00087200: /* broadway */
203 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
204 /* Enable HID2.PSE - in case we need it later */
205 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
206 }
207}
208
209/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
210 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
211 * emulate 32 bytes dcbz length.
212 *
213 * The Book3s_64 inventors also realized this case and implemented a special bit
214 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
215 *
216 * My approach here is to patch the dcbz instruction on executing pages.
217 */
218static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
219{
220 struct page *hpage;
221 u64 hpage_offset;
222 u32 *page;
223 int i;
224
225 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
226 if (is_error_page(hpage)) {
227 kvm_release_page_clean(hpage);
228 return;
229 }
230
231 hpage_offset = pte->raddr & ~PAGE_MASK;
232 hpage_offset &= ~0xFFFULL;
233 hpage_offset /= 4;
234
235 get_page(hpage);
236 page = kmap_atomic(hpage, KM_USER0);
237
238 /* patch dcbz into reserved instruction, so we trap */
239 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
240 if ((page[i] & 0xff0007ff) == INS_DCBZ)
241 page[i] &= 0xfffffff7;
242
243 kunmap_atomic(page, KM_USER0);
244 put_page(hpage);
245}
246
247static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
248{
249 ulong mp_pa = vcpu->arch.magic_page_pa;
250
251 if (unlikely(mp_pa) &&
252 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
253 return 1;
254 }
255
256 return kvm_is_visible_gfn(vcpu->kvm, gfn);
257}
258
259int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
260 ulong eaddr, int vec)
261{
262 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
263 int r = RESUME_GUEST;
264 int relocated;
265 int page_found = 0;
266 struct kvmppc_pte pte;
267 bool is_mmio = false;
268 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
269 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
270 u64 vsid;
271
272 relocated = data ? dr : ir;
273
274 /* Resolve real address if translation turned on */
275 if (relocated) {
276 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
277 } else {
278 pte.may_execute = true;
279 pte.may_read = true;
280 pte.may_write = true;
281 pte.raddr = eaddr & KVM_PAM;
282 pte.eaddr = eaddr;
283 pte.vpage = eaddr >> 12;
284 }
285
286 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
287 case 0:
288 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
289 break;
290 case MSR_DR:
291 case MSR_IR:
292 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
293
294 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
295 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
296 else
297 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
298 pte.vpage |= vsid;
299
300 if (vsid == -1)
301 page_found = -EINVAL;
302 break;
303 }
304
305 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
306 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
307 /*
308 * If we do the dcbz hack, we have to NX on every execution,
309 * so we can patch the executing code. This renders our guest
310 * NX-less.
311 */
312 pte.may_execute = !data;
313 }
314
315 if (page_found == -ENOENT) {
316 /* Page not found in guest PTE entries */
Alexander Graf468a12c2011-12-09 14:44:13 +0100317 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000318 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100319 vcpu->arch.shared->dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000320 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100321 (svcpu->shadow_srr1 & 0x00000000f8000000ULL);
322 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000323 kvmppc_book3s_queue_irqprio(vcpu, vec);
324 } else if (page_found == -EPERM) {
325 /* Storage protection */
Alexander Graf468a12c2011-12-09 14:44:13 +0100326 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000327 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100328 vcpu->arch.shared->dsisr = svcpu->fault_dsisr & ~DSISR_NOHPTE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000329 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
330 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100331 svcpu->shadow_srr1 & 0x00000000f8000000ULL;
332 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000333 kvmppc_book3s_queue_irqprio(vcpu, vec);
334 } else if (page_found == -EINVAL) {
335 /* Page not found in guest SLB */
336 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
337 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
338 } else if (!is_mmio &&
339 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
340 /* The guest's PTE is not mapped yet. Map on the host */
341 kvmppc_mmu_map_page(vcpu, &pte);
342 if (data)
343 vcpu->stat.sp_storage++;
344 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
345 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
346 kvmppc_patch_dcbz(vcpu, &pte);
347 } else {
348 /* MMIO */
349 vcpu->stat.mmio_exits++;
350 vcpu->arch.paddr_accessed = pte.raddr;
351 r = kvmppc_emulate_mmio(run, vcpu);
352 if ( r == RESUME_HOST_NV )
353 r = RESUME_HOST;
354 }
355
356 return r;
357}
358
359static inline int get_fpr_index(int i)
360{
361#ifdef CONFIG_VSX
362 i *= 2;
363#endif
364 return i;
365}
366
367/* Give up external provider (FPU, Altivec, VSX) */
368void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
369{
370 struct thread_struct *t = &current->thread;
371 u64 *vcpu_fpr = vcpu->arch.fpr;
372#ifdef CONFIG_VSX
373 u64 *vcpu_vsx = vcpu->arch.vsr;
374#endif
375 u64 *thread_fpr = (u64*)t->fpr;
376 int i;
377
378 if (!(vcpu->arch.guest_owned_ext & msr))
379 return;
380
381#ifdef DEBUG_EXT
382 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
383#endif
384
385 switch (msr) {
386 case MSR_FP:
387 giveup_fpu(current);
388 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
389 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
390
391 vcpu->arch.fpscr = t->fpscr.val;
392 break;
393 case MSR_VEC:
394#ifdef CONFIG_ALTIVEC
395 giveup_altivec(current);
396 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
397 vcpu->arch.vscr = t->vscr;
398#endif
399 break;
400 case MSR_VSX:
401#ifdef CONFIG_VSX
402 __giveup_vsx(current);
403 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
404 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
405#endif
406 break;
407 default:
408 BUG();
409 }
410
411 vcpu->arch.guest_owned_ext &= ~msr;
412 current->thread.regs->msr &= ~msr;
413 kvmppc_recalc_shadow_msr(vcpu);
414}
415
416static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
417{
418 ulong srr0 = kvmppc_get_pc(vcpu);
419 u32 last_inst = kvmppc_get_last_inst(vcpu);
420 int ret;
421
422 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
423 if (ret == -ENOENT) {
424 ulong msr = vcpu->arch.shared->msr;
425
426 msr = kvmppc_set_field(msr, 33, 33, 1);
427 msr = kvmppc_set_field(msr, 34, 36, 0);
428 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
429 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
430 return EMULATE_AGAIN;
431 }
432
433 return EMULATE_DONE;
434}
435
436static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
437{
438
439 /* Need to do paired single emulation? */
440 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
441 return EMULATE_DONE;
442
443 /* Read out the instruction */
444 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
445 /* Need to emulate */
446 return EMULATE_FAIL;
447
448 return EMULATE_AGAIN;
449}
450
451/* Handle external providers (FPU, Altivec, VSX) */
452static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
453 ulong msr)
454{
455 struct thread_struct *t = &current->thread;
456 u64 *vcpu_fpr = vcpu->arch.fpr;
457#ifdef CONFIG_VSX
458 u64 *vcpu_vsx = vcpu->arch.vsr;
459#endif
460 u64 *thread_fpr = (u64*)t->fpr;
461 int i;
462
463 /* When we have paired singles, we emulate in software */
464 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
465 return RESUME_GUEST;
466
467 if (!(vcpu->arch.shared->msr & msr)) {
468 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
469 return RESUME_GUEST;
470 }
471
472 /* We already own the ext */
473 if (vcpu->arch.guest_owned_ext & msr) {
474 return RESUME_GUEST;
475 }
476
477#ifdef DEBUG_EXT
478 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
479#endif
480
481 current->thread.regs->msr |= msr;
482
483 switch (msr) {
484 case MSR_FP:
485 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
486 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
487
488 t->fpscr.val = vcpu->arch.fpscr;
489 t->fpexc_mode = 0;
490 kvmppc_load_up_fpu();
491 break;
492 case MSR_VEC:
493#ifdef CONFIG_ALTIVEC
494 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
495 t->vscr = vcpu->arch.vscr;
496 t->vrsave = -1;
497 kvmppc_load_up_altivec();
498#endif
499 break;
500 case MSR_VSX:
501#ifdef CONFIG_VSX
502 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
503 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
504 kvmppc_load_up_vsx();
505#endif
506 break;
507 default:
508 BUG();
509 }
510
511 vcpu->arch.guest_owned_ext |= msr;
512
513 kvmppc_recalc_shadow_msr(vcpu);
514
515 return RESUME_GUEST;
516}
517
518int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
519 unsigned int exit_nr)
520{
521 int r = RESUME_HOST;
522
523 vcpu->stat.sum_exits++;
524
525 run->exit_reason = KVM_EXIT_UNKNOWN;
526 run->ready_for_interrupt_injection = 1;
527
528 trace_kvm_book3s_exit(exit_nr, vcpu);
Alexander Graf7d827142011-12-09 15:46:21 +0100529 preempt_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000530 kvm_resched(vcpu);
531 switch (exit_nr) {
532 case BOOK3S_INTERRUPT_INST_STORAGE:
Alexander Graf468a12c2011-12-09 14:44:13 +0100533 {
534 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
535 ulong shadow_srr1 = svcpu->shadow_srr1;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000536 vcpu->stat.pf_instruc++;
537
538#ifdef CONFIG_PPC_BOOK3S_32
539 /* We set segments as unused segments when invalidating them. So
540 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100541 if (svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000542 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
543 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100544 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000545 break;
546 }
547#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100548 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000549
550 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graf468a12c2011-12-09 14:44:13 +0100551 if (shadow_srr1 & 0x40000000) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000552 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
553 vcpu->stat.sp_instruc++;
554 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
555 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
556 /*
557 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
558 * so we can't use the NX bit inside the guest. Let's cross our fingers,
559 * that no guest that needs the dcbz hack does NX.
560 */
561 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
562 r = RESUME_GUEST;
563 } else {
Alexander Graf468a12c2011-12-09 14:44:13 +0100564 vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000565 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
566 r = RESUME_GUEST;
567 }
568 break;
Alexander Graf468a12c2011-12-09 14:44:13 +0100569 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000570 case BOOK3S_INTERRUPT_DATA_STORAGE:
571 {
572 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100573 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
574 u32 fault_dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000575 vcpu->stat.pf_storage++;
576
577#ifdef CONFIG_PPC_BOOK3S_32
578 /* We set segments as unused segments when invalidating them. So
579 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100580 if ((svcpu->sr[dar >> SID_SHIFT]) == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000581 kvmppc_mmu_map_segment(vcpu, dar);
582 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100583 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000584 break;
585 }
586#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100587 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000588
589 /* The only case we need to handle is missing shadow PTEs */
Alexander Graf468a12c2011-12-09 14:44:13 +0100590 if (fault_dsisr & DSISR_NOHPTE) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000591 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
592 } else {
593 vcpu->arch.shared->dar = dar;
Alexander Graf468a12c2011-12-09 14:44:13 +0100594 vcpu->arch.shared->dsisr = fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000595 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
596 r = RESUME_GUEST;
597 }
598 break;
599 }
600 case BOOK3S_INTERRUPT_DATA_SEGMENT:
601 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
602 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
603 kvmppc_book3s_queue_irqprio(vcpu,
604 BOOK3S_INTERRUPT_DATA_SEGMENT);
605 }
606 r = RESUME_GUEST;
607 break;
608 case BOOK3S_INTERRUPT_INST_SEGMENT:
609 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
610 kvmppc_book3s_queue_irqprio(vcpu,
611 BOOK3S_INTERRUPT_INST_SEGMENT);
612 }
613 r = RESUME_GUEST;
614 break;
615 /* We're good on these - the host merely wanted to get our attention */
616 case BOOK3S_INTERRUPT_DECREMENTER:
617 vcpu->stat.dec_exits++;
618 r = RESUME_GUEST;
619 break;
620 case BOOK3S_INTERRUPT_EXTERNAL:
621 vcpu->stat.ext_intr_exits++;
622 r = RESUME_GUEST;
623 break;
624 case BOOK3S_INTERRUPT_PERFMON:
625 r = RESUME_GUEST;
626 break;
627 case BOOK3S_INTERRUPT_PROGRAM:
628 {
629 enum emulation_result er;
Alexander Graf468a12c2011-12-09 14:44:13 +0100630 struct kvmppc_book3s_shadow_vcpu *svcpu;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000631 ulong flags;
632
633program_interrupt:
Alexander Graf468a12c2011-12-09 14:44:13 +0100634 svcpu = svcpu_get(vcpu);
635 flags = svcpu->shadow_srr1 & 0x1f0000ull;
636 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000637
638 if (vcpu->arch.shared->msr & MSR_PR) {
639#ifdef EXIT_DEBUG
640 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
641#endif
642 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
643 (INS_DCBZ & 0xfffffff7)) {
644 kvmppc_core_queue_program(vcpu, flags);
645 r = RESUME_GUEST;
646 break;
647 }
648 }
649
650 vcpu->stat.emulated_inst_exits++;
651 er = kvmppc_emulate_instruction(run, vcpu);
652 switch (er) {
653 case EMULATE_DONE:
654 r = RESUME_GUEST_NV;
655 break;
656 case EMULATE_AGAIN:
657 r = RESUME_GUEST;
658 break;
659 case EMULATE_FAIL:
660 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
661 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
662 kvmppc_core_queue_program(vcpu, flags);
663 r = RESUME_GUEST;
664 break;
665 case EMULATE_DO_MMIO:
666 run->exit_reason = KVM_EXIT_MMIO;
667 r = RESUME_HOST_NV;
668 break;
669 default:
670 BUG();
671 }
672 break;
673 }
674 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafa668f2b2011-08-08 17:26:24 +0200675 if (vcpu->arch.papr_enabled &&
676 (kvmppc_get_last_inst(vcpu) == 0x44000022) &&
677 !(vcpu->arch.shared->msr & MSR_PR)) {
678 /* SC 1 papr hypercalls */
679 ulong cmd = kvmppc_get_gpr(vcpu, 3);
680 int i;
681
Andreas Schwab96f38d72011-11-08 07:17:39 +0000682#ifdef CONFIG_KVM_BOOK3S_64_PR
Alexander Grafa668f2b2011-08-08 17:26:24 +0200683 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
684 r = RESUME_GUEST;
685 break;
686 }
Andreas Schwab96f38d72011-11-08 07:17:39 +0000687#endif
Alexander Grafa668f2b2011-08-08 17:26:24 +0200688
689 run->papr_hcall.nr = cmd;
690 for (i = 0; i < 9; ++i) {
691 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
692 run->papr_hcall.args[i] = gpr;
693 }
694 run->exit_reason = KVM_EXIT_PAPR_HCALL;
695 vcpu->arch.hcall_needed = 1;
696 r = RESUME_HOST;
697 } else if (vcpu->arch.osi_enabled &&
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000698 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
699 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
700 /* MOL hypercalls */
701 u64 *gprs = run->osi.gprs;
702 int i;
703
704 run->exit_reason = KVM_EXIT_OSI;
705 for (i = 0; i < 32; i++)
706 gprs[i] = kvmppc_get_gpr(vcpu, i);
707 vcpu->arch.osi_needed = 1;
708 r = RESUME_HOST_NV;
709 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
710 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
711 /* KVM PV hypercalls */
712 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
713 r = RESUME_GUEST;
714 } else {
715 /* Guest syscalls */
716 vcpu->stat.syscall_exits++;
717 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
718 r = RESUME_GUEST;
719 }
720 break;
721 case BOOK3S_INTERRUPT_FP_UNAVAIL:
722 case BOOK3S_INTERRUPT_ALTIVEC:
723 case BOOK3S_INTERRUPT_VSX:
724 {
725 int ext_msr = 0;
726
727 switch (exit_nr) {
728 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
729 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
730 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
731 }
732
733 switch (kvmppc_check_ext(vcpu, exit_nr)) {
734 case EMULATE_DONE:
735 /* everything ok - let's enable the ext */
736 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
737 break;
738 case EMULATE_FAIL:
739 /* we need to emulate this instruction */
740 goto program_interrupt;
741 break;
742 default:
743 /* nothing to worry about - go again */
744 break;
745 }
746 break;
747 }
748 case BOOK3S_INTERRUPT_ALIGNMENT:
749 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
750 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
751 kvmppc_get_last_inst(vcpu));
752 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
753 kvmppc_get_last_inst(vcpu));
754 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
755 }
756 r = RESUME_GUEST;
757 break;
758 case BOOK3S_INTERRUPT_MACHINE_CHECK:
759 case BOOK3S_INTERRUPT_TRACE:
760 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
761 r = RESUME_GUEST;
762 break;
763 default:
Alexander Graf468a12c2011-12-09 14:44:13 +0100764 {
765 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
766 ulong shadow_srr1 = svcpu->shadow_srr1;
767 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000768 /* Ugh - bork here! What did we get? */
769 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Graf468a12c2011-12-09 14:44:13 +0100770 exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000771 r = RESUME_HOST;
772 BUG();
773 break;
774 }
Alexander Graf468a12c2011-12-09 14:44:13 +0100775 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000776
777 if (!(r & RESUME_HOST)) {
778 /* To avoid clobbering exit_reason, only check for signals if
779 * we aren't already exiting to userspace for some other
780 * reason. */
Alexander Grafe371f712011-12-19 13:36:55 +0100781
782 /*
783 * Interrupts could be timers for the guest which we have to
784 * inject again, so let's postpone them until we're in the guest
785 * and if we really did time things so badly, then we just exit
786 * again due to a host external interrupt.
787 */
788 __hard_irq_disable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000789 if (signal_pending(current)) {
Alexander Grafe371f712011-12-19 13:36:55 +0100790 __hard_irq_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000791#ifdef EXIT_DEBUG
792 printk(KERN_EMERG "KVM: Going back to host\n");
793#endif
794 vcpu->stat.signal_exits++;
795 run->exit_reason = KVM_EXIT_INTR;
796 r = -EINTR;
797 } else {
Alexander Graf7d827142011-12-09 15:46:21 +0100798 preempt_disable();
799
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000800 /* In case an interrupt came in that was triggered
801 * from userspace (like DEC), we need to check what
802 * to inject now! */
Scott Wood7e28e60e2011-11-08 18:23:20 -0600803 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000804 }
805 }
806
807 trace_kvm_book3s_reenter(r, vcpu);
808
809 return r;
810}
811
812int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
813 struct kvm_sregs *sregs)
814{
815 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
816 int i;
817
818 sregs->pvr = vcpu->arch.pvr;
819
820 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
821 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
822 for (i = 0; i < 64; i++) {
823 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
824 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
825 }
826 } else {
827 for (i = 0; i < 16; i++)
828 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
829
830 for (i = 0; i < 8; i++) {
831 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
832 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
833 }
834 }
835
836 return 0;
837}
838
839int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
840 struct kvm_sregs *sregs)
841{
842 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
843 int i;
844
845 kvmppc_set_pvr(vcpu, sregs->pvr);
846
847 vcpu3s->sdr1 = sregs->u.s.sdr1;
848 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
849 for (i = 0; i < 64; i++) {
850 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
851 sregs->u.s.ppc64.slb[i].slbe);
852 }
853 } else {
854 for (i = 0; i < 16; i++) {
855 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
856 }
857 for (i = 0; i < 8; i++) {
858 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
859 (u32)sregs->u.s.ppc32.ibat[i]);
860 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
861 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
862 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
863 (u32)sregs->u.s.ppc32.dbat[i]);
864 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
865 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
866 }
867 }
868
869 /* Flush the MMU after messing with the segments */
870 kvmppc_mmu_pte_flush(vcpu, 0, 0);
871
872 return 0;
873}
874
875int kvmppc_core_check_processor_compat(void)
876{
877 return 0;
878}
879
880struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
881{
882 struct kvmppc_vcpu_book3s *vcpu_book3s;
883 struct kvm_vcpu *vcpu;
884 int err = -ENOMEM;
885 unsigned long p;
886
887 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
888 if (!vcpu_book3s)
889 goto out;
890
891 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
892 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
893 if (!vcpu_book3s->shadow_vcpu)
894 goto free_vcpu;
895
896 vcpu = &vcpu_book3s->vcpu;
897 err = kvm_vcpu_init(vcpu, kvm, id);
898 if (err)
899 goto free_shadow_vcpu;
900
901 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
902 /* the real shared page fills the last 4k of our page */
903 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
904 if (!p)
905 goto uninit_vcpu;
906
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000907#ifdef CONFIG_PPC_BOOK3S_64
908 /* default to book3s_64 (970fx) */
909 vcpu->arch.pvr = 0x3C0301;
910#else
911 /* default to book3s_32 (750) */
912 vcpu->arch.pvr = 0x84202;
913#endif
914 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
915 vcpu->arch.slb_nr = 64;
916
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000917 vcpu->arch.shadow_msr = MSR_USER64;
918
919 err = kvmppc_mmu_init(vcpu);
920 if (err < 0)
921 goto uninit_vcpu;
922
923 return vcpu;
924
925uninit_vcpu:
926 kvm_vcpu_uninit(vcpu);
927free_shadow_vcpu:
928 kfree(vcpu_book3s->shadow_vcpu);
929free_vcpu:
930 vfree(vcpu_book3s);
931out:
932 return ERR_PTR(err);
933}
934
935void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
936{
937 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
938
939 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
940 kvm_vcpu_uninit(vcpu);
941 kfree(vcpu_book3s->shadow_vcpu);
942 vfree(vcpu_book3s);
943}
944
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000945int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000946{
947 int ret;
948 double fpr[32][TS_FPRWIDTH];
949 unsigned int fpscr;
950 int fpexc_mode;
951#ifdef CONFIG_ALTIVEC
952 vector128 vr[32];
953 vector128 vscr;
954 unsigned long uninitialized_var(vrsave);
955 int used_vr;
956#endif
957#ifdef CONFIG_VSX
958 int used_vsr;
959#endif
960 ulong ext_msr;
961
Alexander Graf7d827142011-12-09 15:46:21 +0100962 preempt_disable();
963
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200964 /* Check if we can run the vcpu at all */
965 if (!vcpu->arch.sane) {
966 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
Alexander Graf7d827142011-12-09 15:46:21 +0100967 ret = -EINVAL;
968 goto out;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200969 }
970
Scott Wood25051b52011-11-08 18:23:23 -0600971 kvmppc_core_prepare_to_enter(vcpu);
972
Alexander Grafe371f712011-12-19 13:36:55 +0100973 /*
974 * Interrupts could be timers for the guest which we have to inject
975 * again, so let's postpone them until we're in the guest and if we
976 * really did time things so badly, then we just exit again due to
977 * a host external interrupt.
978 */
979 __hard_irq_disable();
980
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000981 /* No need to go into the guest when all we do is going out */
982 if (signal_pending(current)) {
Alexander Grafe371f712011-12-19 13:36:55 +0100983 __hard_irq_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000984 kvm_run->exit_reason = KVM_EXIT_INTR;
Alexander Graf7d827142011-12-09 15:46:21 +0100985 ret = -EINTR;
986 goto out;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000987 }
988
989 /* Save FPU state in stack */
990 if (current->thread.regs->msr & MSR_FP)
991 giveup_fpu(current);
992 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
993 fpscr = current->thread.fpscr.val;
994 fpexc_mode = current->thread.fpexc_mode;
995
996#ifdef CONFIG_ALTIVEC
997 /* Save Altivec state in stack */
998 used_vr = current->thread.used_vr;
999 if (used_vr) {
1000 if (current->thread.regs->msr & MSR_VEC)
1001 giveup_altivec(current);
1002 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1003 vscr = current->thread.vscr;
1004 vrsave = current->thread.vrsave;
1005 }
1006#endif
1007
1008#ifdef CONFIG_VSX
1009 /* Save VSX state in stack */
1010 used_vsr = current->thread.used_vsr;
1011 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
1012 __giveup_vsx(current);
1013#endif
1014
1015 /* Remember the MSR with disabled extensions */
1016 ext_msr = current->thread.regs->msr;
1017
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001018 /* Preload FPU if it's enabled */
1019 if (vcpu->arch.shared->msr & MSR_FP)
1020 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1021
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001022 kvm_guest_enter();
1023
1024 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
1025
1026 kvm_guest_exit();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001027
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001028 current->thread.regs->msr = ext_msr;
1029
1030 /* Make sure we save the guest FPU/Altivec/VSX state */
1031 kvmppc_giveup_ext(vcpu, MSR_FP);
1032 kvmppc_giveup_ext(vcpu, MSR_VEC);
1033 kvmppc_giveup_ext(vcpu, MSR_VSX);
1034
1035 /* Restore FPU state from stack */
1036 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1037 current->thread.fpscr.val = fpscr;
1038 current->thread.fpexc_mode = fpexc_mode;
1039
1040#ifdef CONFIG_ALTIVEC
1041 /* Restore Altivec state from stack */
1042 if (used_vr && current->thread.used_vr) {
1043 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1044 current->thread.vscr = vscr;
1045 current->thread.vrsave = vrsave;
1046 }
1047 current->thread.used_vr = used_vr;
1048#endif
1049
1050#ifdef CONFIG_VSX
1051 current->thread.used_vsr = used_vsr;
1052#endif
1053
Alexander Graf7d827142011-12-09 15:46:21 +01001054out:
1055 preempt_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001056 return ret;
1057}
1058
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001059int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1060 struct kvm_userspace_memory_region *mem)
1061{
1062 return 0;
1063}
1064
1065void kvmppc_core_commit_memory_region(struct kvm *kvm,
1066 struct kvm_userspace_memory_region *mem)
1067{
1068}
1069
1070int kvmppc_core_init_vm(struct kvm *kvm)
1071{
1072 return 0;
1073}
1074
1075void kvmppc_core_destroy_vm(struct kvm *kvm)
1076{
1077}
1078
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001079static int kvmppc_book3s_init(void)
1080{
1081 int r;
1082
1083 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1084 THIS_MODULE);
1085
1086 if (r)
1087 return r;
1088
1089 r = kvmppc_mmu_hpte_sysinit();
1090
1091 return r;
1092}
1093
1094static void kvmppc_book3s_exit(void)
1095{
1096 kvmppc_mmu_hpte_sysexit();
1097 kvm_exit();
1098}
1099
1100module_init(kvmppc_book3s_init);
1101module_exit(kvmppc_book3s_exit);