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