blob: 73ed11c41bacfe691f60607c5519b179ae13f3cf [file] [log] [blame]
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001/*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 * Paul Mackerras <paulus@samba.org>
8 *
9 * Description:
10 * Functions relating to running KVM on Book 3S processors where
11 * we don't have access to hypervisor mode, and we run the guest
12 * in problem state (user mode).
13 *
14 * This file is derived from arch/powerpc/kvm/44x.c,
15 * by Hollis Blanchard <hollisb@us.ibm.com>.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License, version 2, as
19 * published by the Free Software Foundation.
20 */
21
22#include <linux/kvm_host.h>
Paul Gortmaker93087942011-07-29 16:19:31 +100023#include <linux/export.h>
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000024#include <linux/err.h>
25#include <linux/slab.h>
26
27#include <asm/reg.h>
28#include <asm/cputable.h>
29#include <asm/cacheflush.h>
30#include <asm/tlbflush.h>
31#include <asm/uaccess.h>
32#include <asm/io.h>
33#include <asm/kvm_ppc.h>
34#include <asm/kvm_book3s.h>
35#include <asm/mmu_context.h>
Benjamin Herrenschmidt95327d02012-04-01 17:35:53 +000036#include <asm/switch_to.h>
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000037#include <linux/gfp.h>
38#include <linux/sched.h>
39#include <linux/vmalloc.h>
40#include <linux/highmem.h>
41
42#include "trace.h"
43
44/* #define EXIT_DEBUG */
45/* #define DEBUG_EXT */
46
47static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
48 ulong msr);
49
50/* Some compatibility defines */
51#ifdef CONFIG_PPC_BOOK3S_32
52#define MSR_USER32 MSR_USER
53#define MSR_USER64 MSR_USER
54#define HW_PAGE_SIZE PAGE_SIZE
55#endif
56
57void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
58{
59#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf468a12c2011-12-09 14:44:13 +010060 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
61 memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb));
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000062 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
63 sizeof(get_paca()->shadow_vcpu));
Alexander Graf468a12c2011-12-09 14:44:13 +010064 svcpu->slb_max = to_book3s(vcpu)->slb_shadow_max;
65 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000066#endif
Paul Mackerrasa47d72f2012-09-20 19:35:51 +000067 vcpu->cpu = smp_processor_id();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000068#ifdef CONFIG_PPC_BOOK3S_32
69 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
70#endif
71}
72
73void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
74{
75#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf468a12c2011-12-09 14:44:13 +010076 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
77 memcpy(to_book3s(vcpu)->slb_shadow, svcpu->slb, sizeof(svcpu->slb));
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000078 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
79 sizeof(get_paca()->shadow_vcpu));
Alexander Graf468a12c2011-12-09 14:44:13 +010080 to_book3s(vcpu)->slb_shadow_max = svcpu->slb_max;
81 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000082#endif
83
Paul Mackerras28c483b2012-11-04 18:16:46 +000084 kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX);
Paul Mackerrasa47d72f2012-09-20 19:35:51 +000085 vcpu->cpu = -1;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000086}
87
Alexander Graf7c973a22012-08-13 12:50:35 +020088int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf03d25c52012-08-10 12:28:50 +020089{
Alexander Graf7c973a22012-08-13 12:50:35 +020090 int r = 1; /* Indicate we want to get back into the guest */
91
Alexander Graf9b0cb3c2012-08-10 13:23:55 +020092 /* We misuse TLB_FLUSH to indicate that we want to clear
93 all shadow cache entries */
94 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
95 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf7c973a22012-08-13 12:50:35 +020096
97 return r;
Alexander Graf03d25c52012-08-10 12:28:50 +020098}
99
Alexander Graf9b0cb3c2012-08-10 13:23:55 +0200100/************* MMU Notifiers *************/
101
102int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
103{
104 trace_kvm_unmap_hva(hva);
105
106 /*
107 * Flush all shadow tlb entries everywhere. This is slow, but
108 * we are 100% sure that we catch the to be unmapped page
109 */
110 kvm_flush_remote_tlbs(kvm);
111
112 return 0;
113}
114
115int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
116{
117 /* kvm_unmap_hva flushes everything anyways */
118 kvm_unmap_hva(kvm, start);
119
120 return 0;
121}
122
123int kvm_age_hva(struct kvm *kvm, unsigned long hva)
124{
125 /* XXX could be more clever ;) */
126 return 0;
127}
128
129int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
130{
131 /* XXX could be more clever ;) */
132 return 0;
133}
134
135void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
136{
137 /* The page will get remapped properly on its next fault */
138 kvm_unmap_hva(kvm, hva);
139}
140
141/*****************************************/
142
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000143static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
144{
145 ulong smsr = vcpu->arch.shared->msr;
146
147 /* Guest MSR values */
Paul Mackerras3a2e7b02012-11-04 18:17:28 +0000148 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000149 /* Process MSR values */
150 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
151 /* External providers the guest reserved */
152 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
153 /* 64-bit Process MSR values */
154#ifdef CONFIG_PPC_BOOK3S_64
155 smsr |= MSR_ISF | MSR_HV;
156#endif
157 vcpu->arch.shadow_msr = smsr;
158}
159
160void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
161{
162 ulong old_msr = vcpu->arch.shared->msr;
163
164#ifdef EXIT_DEBUG
165 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
166#endif
167
168 msr &= to_book3s(vcpu)->msr_mask;
169 vcpu->arch.shared->msr = msr;
170 kvmppc_recalc_shadow_msr(vcpu);
171
172 if (msr & MSR_POW) {
173 if (!vcpu->arch.pending_exceptions) {
174 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100175 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000176 vcpu->stat.halt_wakeup++;
177
178 /* Unset POW bit after we woke up */
179 msr &= ~MSR_POW;
180 vcpu->arch.shared->msr = msr;
181 }
182 }
183
184 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
185 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
186 kvmppc_mmu_flush_segments(vcpu);
187 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
188
189 /* Preload magic page segment when in kernel mode */
190 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
191 struct kvm_vcpu_arch *a = &vcpu->arch;
192
193 if (msr & MSR_DR)
194 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
195 else
196 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
197 }
198 }
199
Benjamin Herrenschmidtbbcc9c02012-03-13 21:52:44 +0000200 /*
201 * When switching from 32 to 64-bit, we may have a stale 32-bit
202 * magic page around, we need to flush it. Typically 32-bit magic
203 * page will be instanciated when calling into RTAS. Note: We
204 * assume that such transition only happens while in kernel mode,
205 * ie, we never transition from user 32-bit to kernel 64-bit with
206 * a 32-bit magic page around.
207 */
208 if (vcpu->arch.magic_page_pa &&
209 !(old_msr & MSR_PR) && !(old_msr & MSR_SF) && (msr & MSR_SF)) {
210 /* going from RTAS to normal kernel code */
211 kvmppc_mmu_pte_flush(vcpu, (uint32_t)vcpu->arch.magic_page_pa,
212 ~0xFFFUL);
213 }
214
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000215 /* Preload FPU if it's enabled */
216 if (vcpu->arch.shared->msr & MSR_FP)
217 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
218}
219
220void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
221{
222 u32 host_pvr;
223
224 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
225 vcpu->arch.pvr = pvr;
226#ifdef CONFIG_PPC_BOOK3S_64
227 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
228 kvmppc_mmu_book3s_64_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200229 if (!to_book3s(vcpu)->hior_explicit)
230 to_book3s(vcpu)->hior = 0xfff00000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000231 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200232 vcpu->arch.cpu_type = KVM_CPU_3S_64;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000233 } else
234#endif
235 {
236 kvmppc_mmu_book3s_32_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200237 if (!to_book3s(vcpu)->hior_explicit)
238 to_book3s(vcpu)->hior = 0;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000239 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200240 vcpu->arch.cpu_type = KVM_CPU_3S_32;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000241 }
242
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200243 kvmppc_sanity_check(vcpu);
244
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000245 /* If we are in hypervisor level on 970, we can tell the CPU to
246 * treat DCBZ as 32 bytes store */
247 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
248 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
249 !strcmp(cur_cpu_spec->platform, "ppc970"))
250 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
251
252 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
253 really needs them in a VM on Cell and force disable them. */
254 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
255 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
256
257#ifdef CONFIG_PPC_BOOK3S_32
258 /* 32 bit Book3S always has 32 byte dcbz */
259 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
260#endif
261
262 /* On some CPUs we can execute paired single operations natively */
263 asm ( "mfpvr %0" : "=r"(host_pvr));
264 switch (host_pvr) {
265 case 0x00080200: /* lonestar 2.0 */
266 case 0x00088202: /* lonestar 2.2 */
267 case 0x70000100: /* gekko 1.0 */
268 case 0x00080100: /* gekko 2.0 */
269 case 0x00083203: /* gekko 2.3a */
270 case 0x00083213: /* gekko 2.3b */
271 case 0x00083204: /* gekko 2.4 */
272 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
273 case 0x00087200: /* broadway */
274 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
275 /* Enable HID2.PSE - in case we need it later */
276 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
277 }
278}
279
280/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
281 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
282 * emulate 32 bytes dcbz length.
283 *
284 * The Book3s_64 inventors also realized this case and implemented a special bit
285 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
286 *
287 * My approach here is to patch the dcbz instruction on executing pages.
288 */
289static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
290{
291 struct page *hpage;
292 u64 hpage_offset;
293 u32 *page;
294 int i;
295
296 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800297 if (is_error_page(hpage))
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000298 return;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000299
300 hpage_offset = pte->raddr & ~PAGE_MASK;
301 hpage_offset &= ~0xFFFULL;
302 hpage_offset /= 4;
303
304 get_page(hpage);
Cong Wang2480b202011-11-25 23:14:16 +0800305 page = kmap_atomic(hpage);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000306
307 /* patch dcbz into reserved instruction, so we trap */
308 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
309 if ((page[i] & 0xff0007ff) == INS_DCBZ)
310 page[i] &= 0xfffffff7;
311
Cong Wang2480b202011-11-25 23:14:16 +0800312 kunmap_atomic(page);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000313 put_page(hpage);
314}
315
316static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
317{
318 ulong mp_pa = vcpu->arch.magic_page_pa;
319
Benjamin Herrenschmidtbbcc9c02012-03-13 21:52:44 +0000320 if (!(vcpu->arch.shared->msr & MSR_SF))
321 mp_pa = (uint32_t)mp_pa;
322
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000323 if (unlikely(mp_pa) &&
324 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
325 return 1;
326 }
327
328 return kvm_is_visible_gfn(vcpu->kvm, gfn);
329}
330
331int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
332 ulong eaddr, int vec)
333{
334 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
335 int r = RESUME_GUEST;
336 int relocated;
337 int page_found = 0;
338 struct kvmppc_pte pte;
339 bool is_mmio = false;
340 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
341 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
342 u64 vsid;
343
344 relocated = data ? dr : ir;
345
346 /* Resolve real address if translation turned on */
347 if (relocated) {
348 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
349 } else {
350 pte.may_execute = true;
351 pte.may_read = true;
352 pte.may_write = true;
353 pte.raddr = eaddr & KVM_PAM;
354 pte.eaddr = eaddr;
355 pte.vpage = eaddr >> 12;
356 }
357
358 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
359 case 0:
360 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
361 break;
362 case MSR_DR:
363 case MSR_IR:
364 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
365
366 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
367 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
368 else
369 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
370 pte.vpage |= vsid;
371
372 if (vsid == -1)
373 page_found = -EINVAL;
374 break;
375 }
376
377 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
378 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
379 /*
380 * If we do the dcbz hack, we have to NX on every execution,
381 * so we can patch the executing code. This renders our guest
382 * NX-less.
383 */
384 pte.may_execute = !data;
385 }
386
387 if (page_found == -ENOENT) {
388 /* Page not found in guest PTE entries */
Alexander Graf468a12c2011-12-09 14:44:13 +0100389 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000390 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100391 vcpu->arch.shared->dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000392 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100393 (svcpu->shadow_srr1 & 0x00000000f8000000ULL);
394 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000395 kvmppc_book3s_queue_irqprio(vcpu, vec);
396 } else if (page_found == -EPERM) {
397 /* Storage protection */
Alexander Graf468a12c2011-12-09 14:44:13 +0100398 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000399 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100400 vcpu->arch.shared->dsisr = svcpu->fault_dsisr & ~DSISR_NOHPTE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000401 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
402 vcpu->arch.shared->msr |=
Alexander Graf468a12c2011-12-09 14:44:13 +0100403 svcpu->shadow_srr1 & 0x00000000f8000000ULL;
404 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000405 kvmppc_book3s_queue_irqprio(vcpu, vec);
406 } else if (page_found == -EINVAL) {
407 /* Page not found in guest SLB */
408 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
409 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
410 } else if (!is_mmio &&
411 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
412 /* The guest's PTE is not mapped yet. Map on the host */
413 kvmppc_mmu_map_page(vcpu, &pte);
414 if (data)
415 vcpu->stat.sp_storage++;
416 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
417 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
418 kvmppc_patch_dcbz(vcpu, &pte);
419 } else {
420 /* MMIO */
421 vcpu->stat.mmio_exits++;
422 vcpu->arch.paddr_accessed = pte.raddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100423 vcpu->arch.vaddr_accessed = pte.eaddr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000424 r = kvmppc_emulate_mmio(run, vcpu);
425 if ( r == RESUME_HOST_NV )
426 r = RESUME_HOST;
427 }
428
429 return r;
430}
431
432static inline int get_fpr_index(int i)
433{
Paul Mackerras28c483b2012-11-04 18:16:46 +0000434 return i * TS_FPRWIDTH;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000435}
436
437/* Give up external provider (FPU, Altivec, VSX) */
438void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
439{
440 struct thread_struct *t = &current->thread;
441 u64 *vcpu_fpr = vcpu->arch.fpr;
442#ifdef CONFIG_VSX
443 u64 *vcpu_vsx = vcpu->arch.vsr;
444#endif
445 u64 *thread_fpr = (u64*)t->fpr;
446 int i;
447
Paul Mackerras28c483b2012-11-04 18:16:46 +0000448 /*
449 * VSX instructions can access FP and vector registers, so if
450 * we are giving up VSX, make sure we give up FP and VMX as well.
451 */
452 if (msr & MSR_VSX)
453 msr |= MSR_FP | MSR_VEC;
454
455 msr &= vcpu->arch.guest_owned_ext;
456 if (!msr)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000457 return;
458
459#ifdef DEBUG_EXT
460 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
461#endif
462
Paul Mackerras28c483b2012-11-04 18:16:46 +0000463 if (msr & MSR_FP) {
464 /*
465 * Note that on CPUs with VSX, giveup_fpu stores
466 * both the traditional FP registers and the added VSX
467 * registers into thread.fpr[].
468 */
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000469 giveup_fpu(current);
470 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
471 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
472
473 vcpu->arch.fpscr = t->fpscr.val;
Paul Mackerras28c483b2012-11-04 18:16:46 +0000474
475#ifdef CONFIG_VSX
476 if (cpu_has_feature(CPU_FTR_VSX))
477 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
478 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
479#endif
480 }
481
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000482#ifdef CONFIG_ALTIVEC
Paul Mackerras28c483b2012-11-04 18:16:46 +0000483 if (msr & MSR_VEC) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000484 giveup_altivec(current);
485 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
486 vcpu->arch.vscr = t->vscr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000487 }
Paul Mackerras28c483b2012-11-04 18:16:46 +0000488#endif
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000489
Paul Mackerras28c483b2012-11-04 18:16:46 +0000490 vcpu->arch.guest_owned_ext &= ~(msr | MSR_VSX);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000491 kvmppc_recalc_shadow_msr(vcpu);
492}
493
494static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
495{
496 ulong srr0 = kvmppc_get_pc(vcpu);
497 u32 last_inst = kvmppc_get_last_inst(vcpu);
498 int ret;
499
500 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
501 if (ret == -ENOENT) {
502 ulong msr = vcpu->arch.shared->msr;
503
504 msr = kvmppc_set_field(msr, 33, 33, 1);
505 msr = kvmppc_set_field(msr, 34, 36, 0);
506 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
507 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
508 return EMULATE_AGAIN;
509 }
510
511 return EMULATE_DONE;
512}
513
514static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
515{
516
517 /* Need to do paired single emulation? */
518 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
519 return EMULATE_DONE;
520
521 /* Read out the instruction */
522 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
523 /* Need to emulate */
524 return EMULATE_FAIL;
525
526 return EMULATE_AGAIN;
527}
528
529/* Handle external providers (FPU, Altivec, VSX) */
530static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
531 ulong msr)
532{
533 struct thread_struct *t = &current->thread;
534 u64 *vcpu_fpr = vcpu->arch.fpr;
535#ifdef CONFIG_VSX
536 u64 *vcpu_vsx = vcpu->arch.vsr;
537#endif
538 u64 *thread_fpr = (u64*)t->fpr;
539 int i;
540
541 /* When we have paired singles, we emulate in software */
542 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
543 return RESUME_GUEST;
544
545 if (!(vcpu->arch.shared->msr & msr)) {
546 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
547 return RESUME_GUEST;
548 }
549
Paul Mackerras28c483b2012-11-04 18:16:46 +0000550 if (msr == MSR_VSX) {
551 /* No VSX? Give an illegal instruction interrupt */
552#ifdef CONFIG_VSX
553 if (!cpu_has_feature(CPU_FTR_VSX))
554#endif
555 {
556 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
557 return RESUME_GUEST;
558 }
559
560 /*
561 * We have to load up all the FP and VMX registers before
562 * we can let the guest use VSX instructions.
563 */
564 msr = MSR_FP | MSR_VEC | MSR_VSX;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000565 }
566
Paul Mackerras28c483b2012-11-04 18:16:46 +0000567 /* See if we already own all the ext(s) needed */
568 msr &= ~vcpu->arch.guest_owned_ext;
569 if (!msr)
570 return RESUME_GUEST;
571
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000572#ifdef DEBUG_EXT
573 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
574#endif
575
576 current->thread.regs->msr |= msr;
577
Paul Mackerras28c483b2012-11-04 18:16:46 +0000578 if (msr & MSR_FP) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000579 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
580 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
Paul Mackerras28c483b2012-11-04 18:16:46 +0000581#ifdef CONFIG_VSX
582 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
583 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
584#endif
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000585 t->fpscr.val = vcpu->arch.fpscr;
586 t->fpexc_mode = 0;
587 kvmppc_load_up_fpu();
Paul Mackerras28c483b2012-11-04 18:16:46 +0000588 }
589
590 if (msr & MSR_VEC) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000591#ifdef CONFIG_ALTIVEC
592 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
593 t->vscr = vcpu->arch.vscr;
594 t->vrsave = -1;
595 kvmppc_load_up_altivec();
596#endif
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000597 }
598
599 vcpu->arch.guest_owned_ext |= msr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000600 kvmppc_recalc_shadow_msr(vcpu);
601
602 return RESUME_GUEST;
603}
604
605int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
606 unsigned int exit_nr)
607{
608 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200609 int s;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000610
611 vcpu->stat.sum_exits++;
612
613 run->exit_reason = KVM_EXIT_UNKNOWN;
614 run->ready_for_interrupt_injection = 1;
615
Alexander Grafbd2be682012-08-13 01:04:19 +0200616 /* We get here with MSR.EE=1 */
Alexander Graf3b1d9d72012-04-30 10:56:12 +0200617
Alexander Graf97c95052012-08-02 15:10:00 +0200618 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200619 kvm_guest_exit();
Alexander Grafc63ddcb2012-08-12 11:27:49 +0200620
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000621 switch (exit_nr) {
622 case BOOK3S_INTERRUPT_INST_STORAGE:
Alexander Graf468a12c2011-12-09 14:44:13 +0100623 {
624 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
625 ulong shadow_srr1 = svcpu->shadow_srr1;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000626 vcpu->stat.pf_instruc++;
627
628#ifdef CONFIG_PPC_BOOK3S_32
629 /* We set segments as unused segments when invalidating them. So
630 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100631 if (svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000632 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
633 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100634 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000635 break;
636 }
637#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100638 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000639
640 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graf468a12c2011-12-09 14:44:13 +0100641 if (shadow_srr1 & 0x40000000) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000642 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
643 vcpu->stat.sp_instruc++;
644 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
645 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
646 /*
647 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
648 * so we can't use the NX bit inside the guest. Let's cross our fingers,
649 * that no guest that needs the dcbz hack does NX.
650 */
651 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
652 r = RESUME_GUEST;
653 } else {
Alexander Graf468a12c2011-12-09 14:44:13 +0100654 vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000655 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
656 r = RESUME_GUEST;
657 }
658 break;
Alexander Graf468a12c2011-12-09 14:44:13 +0100659 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000660 case BOOK3S_INTERRUPT_DATA_STORAGE:
661 {
662 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf468a12c2011-12-09 14:44:13 +0100663 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
664 u32 fault_dsisr = svcpu->fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000665 vcpu->stat.pf_storage++;
666
667#ifdef CONFIG_PPC_BOOK3S_32
668 /* We set segments as unused segments when invalidating them. So
669 * treat the respective fault as segment fault. */
Alexander Graf468a12c2011-12-09 14:44:13 +0100670 if ((svcpu->sr[dar >> SID_SHIFT]) == SR_INVALID) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000671 kvmppc_mmu_map_segment(vcpu, dar);
672 r = RESUME_GUEST;
Alexander Graf468a12c2011-12-09 14:44:13 +0100673 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000674 break;
675 }
676#endif
Alexander Graf468a12c2011-12-09 14:44:13 +0100677 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000678
679 /* The only case we need to handle is missing shadow PTEs */
Alexander Graf468a12c2011-12-09 14:44:13 +0100680 if (fault_dsisr & DSISR_NOHPTE) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000681 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
682 } else {
683 vcpu->arch.shared->dar = dar;
Alexander Graf468a12c2011-12-09 14:44:13 +0100684 vcpu->arch.shared->dsisr = fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000685 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
686 r = RESUME_GUEST;
687 }
688 break;
689 }
690 case BOOK3S_INTERRUPT_DATA_SEGMENT:
691 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
692 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
693 kvmppc_book3s_queue_irqprio(vcpu,
694 BOOK3S_INTERRUPT_DATA_SEGMENT);
695 }
696 r = RESUME_GUEST;
697 break;
698 case BOOK3S_INTERRUPT_INST_SEGMENT:
699 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
700 kvmppc_book3s_queue_irqprio(vcpu,
701 BOOK3S_INTERRUPT_INST_SEGMENT);
702 }
703 r = RESUME_GUEST;
704 break;
705 /* We're good on these - the host merely wanted to get our attention */
706 case BOOK3S_INTERRUPT_DECREMENTER:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100707 case BOOK3S_INTERRUPT_HV_DECREMENTER:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000708 vcpu->stat.dec_exits++;
709 r = RESUME_GUEST;
710 break;
711 case BOOK3S_INTERRUPT_EXTERNAL:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100712 case BOOK3S_INTERRUPT_EXTERNAL_LEVEL:
713 case BOOK3S_INTERRUPT_EXTERNAL_HV:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000714 vcpu->stat.ext_intr_exits++;
715 r = RESUME_GUEST;
716 break;
717 case BOOK3S_INTERRUPT_PERFMON:
718 r = RESUME_GUEST;
719 break;
720 case BOOK3S_INTERRUPT_PROGRAM:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100721 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000722 {
723 enum emulation_result er;
Alexander Graf468a12c2011-12-09 14:44:13 +0100724 struct kvmppc_book3s_shadow_vcpu *svcpu;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000725 ulong flags;
726
727program_interrupt:
Alexander Graf468a12c2011-12-09 14:44:13 +0100728 svcpu = svcpu_get(vcpu);
729 flags = svcpu->shadow_srr1 & 0x1f0000ull;
730 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000731
732 if (vcpu->arch.shared->msr & MSR_PR) {
733#ifdef EXIT_DEBUG
734 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
735#endif
736 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
737 (INS_DCBZ & 0xfffffff7)) {
738 kvmppc_core_queue_program(vcpu, flags);
739 r = RESUME_GUEST;
740 break;
741 }
742 }
743
744 vcpu->stat.emulated_inst_exits++;
745 er = kvmppc_emulate_instruction(run, vcpu);
746 switch (er) {
747 case EMULATE_DONE:
748 r = RESUME_GUEST_NV;
749 break;
750 case EMULATE_AGAIN:
751 r = RESUME_GUEST;
752 break;
753 case EMULATE_FAIL:
754 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
755 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
756 kvmppc_core_queue_program(vcpu, flags);
757 r = RESUME_GUEST;
758 break;
759 case EMULATE_DO_MMIO:
760 run->exit_reason = KVM_EXIT_MMIO;
761 r = RESUME_HOST_NV;
762 break;
Alexander Graf50c7bb82012-12-14 23:42:05 +0100763 case EMULATE_DO_PAPR:
764 run->exit_reason = KVM_EXIT_PAPR_HCALL;
765 vcpu->arch.hcall_needed = 1;
766 r = RESUME_HOST_NV;
767 break;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000768 default:
769 BUG();
770 }
771 break;
772 }
773 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafa668f2b2011-08-08 17:26:24 +0200774 if (vcpu->arch.papr_enabled &&
775 (kvmppc_get_last_inst(vcpu) == 0x44000022) &&
776 !(vcpu->arch.shared->msr & MSR_PR)) {
777 /* SC 1 papr hypercalls */
778 ulong cmd = kvmppc_get_gpr(vcpu, 3);
779 int i;
780
Andreas Schwab96f38d72011-11-08 07:17:39 +0000781#ifdef CONFIG_KVM_BOOK3S_64_PR
Alexander Grafa668f2b2011-08-08 17:26:24 +0200782 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
783 r = RESUME_GUEST;
784 break;
785 }
Andreas Schwab96f38d72011-11-08 07:17:39 +0000786#endif
Alexander Grafa668f2b2011-08-08 17:26:24 +0200787
788 run->papr_hcall.nr = cmd;
789 for (i = 0; i < 9; ++i) {
790 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
791 run->papr_hcall.args[i] = gpr;
792 }
793 run->exit_reason = KVM_EXIT_PAPR_HCALL;
794 vcpu->arch.hcall_needed = 1;
795 r = RESUME_HOST;
796 } else if (vcpu->arch.osi_enabled &&
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000797 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
798 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
799 /* MOL hypercalls */
800 u64 *gprs = run->osi.gprs;
801 int i;
802
803 run->exit_reason = KVM_EXIT_OSI;
804 for (i = 0; i < 32; i++)
805 gprs[i] = kvmppc_get_gpr(vcpu, i);
806 vcpu->arch.osi_needed = 1;
807 r = RESUME_HOST_NV;
808 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
809 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
810 /* KVM PV hypercalls */
811 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
812 r = RESUME_GUEST;
813 } else {
814 /* Guest syscalls */
815 vcpu->stat.syscall_exits++;
816 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
817 r = RESUME_GUEST;
818 }
819 break;
820 case BOOK3S_INTERRUPT_FP_UNAVAIL:
821 case BOOK3S_INTERRUPT_ALTIVEC:
822 case BOOK3S_INTERRUPT_VSX:
823 {
824 int ext_msr = 0;
825
826 switch (exit_nr) {
827 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
828 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
829 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
830 }
831
832 switch (kvmppc_check_ext(vcpu, exit_nr)) {
833 case EMULATE_DONE:
834 /* everything ok - let's enable the ext */
835 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
836 break;
837 case EMULATE_FAIL:
838 /* we need to emulate this instruction */
839 goto program_interrupt;
840 break;
841 default:
842 /* nothing to worry about - go again */
843 break;
844 }
845 break;
846 }
847 case BOOK3S_INTERRUPT_ALIGNMENT:
848 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
849 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
850 kvmppc_get_last_inst(vcpu));
851 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
852 kvmppc_get_last_inst(vcpu));
853 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
854 }
855 r = RESUME_GUEST;
856 break;
857 case BOOK3S_INTERRUPT_MACHINE_CHECK:
858 case BOOK3S_INTERRUPT_TRACE:
859 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
860 r = RESUME_GUEST;
861 break;
862 default:
Alexander Graf468a12c2011-12-09 14:44:13 +0100863 {
864 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
865 ulong shadow_srr1 = svcpu->shadow_srr1;
866 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000867 /* Ugh - bork here! What did we get? */
868 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Graf468a12c2011-12-09 14:44:13 +0100869 exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000870 r = RESUME_HOST;
871 BUG();
872 break;
873 }
Alexander Graf468a12c2011-12-09 14:44:13 +0100874 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000875
876 if (!(r & RESUME_HOST)) {
877 /* To avoid clobbering exit_reason, only check for signals if
878 * we aren't already exiting to userspace for some other
879 * reason. */
Alexander Grafe371f712011-12-19 13:36:55 +0100880
881 /*
882 * Interrupts could be timers for the guest which we have to
883 * inject again, so let's postpone them until we're in the guest
884 * and if we really did time things so badly, then we just exit
885 * again due to a host external interrupt.
886 */
Alexander Grafbd2be682012-08-13 01:04:19 +0200887 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200888 s = kvmppc_prepare_to_enter(vcpu);
889 if (s <= 0) {
Alexander Grafbd2be682012-08-13 01:04:19 +0200890 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200891 r = s;
Alexander Graf24afa372012-08-12 12:42:30 +0200892 } else {
Alexander Grafbd2be682012-08-13 01:04:19 +0200893 kvmppc_lazy_ee_enable();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000894 }
895 }
896
897 trace_kvm_book3s_reenter(r, vcpu);
898
899 return r;
900}
901
902int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
903 struct kvm_sregs *sregs)
904{
905 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
906 int i;
907
908 sregs->pvr = vcpu->arch.pvr;
909
910 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
911 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
912 for (i = 0; i < 64; i++) {
913 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
914 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
915 }
916 } else {
917 for (i = 0; i < 16; i++)
918 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
919
920 for (i = 0; i < 8; i++) {
921 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
922 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
923 }
924 }
925
926 return 0;
927}
928
929int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
930 struct kvm_sregs *sregs)
931{
932 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
933 int i;
934
935 kvmppc_set_pvr(vcpu, sregs->pvr);
936
937 vcpu3s->sdr1 = sregs->u.s.sdr1;
938 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
939 for (i = 0; i < 64; i++) {
940 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
941 sregs->u.s.ppc64.slb[i].slbe);
942 }
943 } else {
944 for (i = 0; i < 16; i++) {
945 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
946 }
947 for (i = 0; i < 8; i++) {
948 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
949 (u32)sregs->u.s.ppc32.ibat[i]);
950 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
951 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
952 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
953 (u32)sregs->u.s.ppc32.dbat[i]);
954 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
955 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
956 }
957 }
958
959 /* Flush the MMU after messing with the segments */
960 kvmppc_mmu_pte_flush(vcpu, 0, 0);
961
962 return 0;
963}
964
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000965int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000966{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000967 int r = 0;
Paul Mackerras31f34382011-12-12 12:26:50 +0000968
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000969 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000970 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000971 *val = get_reg_val(id, to_book3s(vcpu)->hior);
Paul Mackerras31f34382011-12-12 12:26:50 +0000972 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000973#ifdef CONFIG_VSX
974 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
975 long int i = id - KVM_REG_PPC_VSR0;
976
977 if (!cpu_has_feature(CPU_FTR_VSX)) {
978 r = -ENXIO;
979 break;
980 }
981 val->vsxval[0] = vcpu->arch.fpr[i];
982 val->vsxval[1] = vcpu->arch.vsr[i];
983 break;
984 }
985#endif /* CONFIG_VSX */
Paul Mackerras31f34382011-12-12 12:26:50 +0000986 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000987 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000988 break;
989 }
990
991 return r;
992}
993
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000994int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000995{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000996 int r = 0;
Paul Mackerras31f34382011-12-12 12:26:50 +0000997
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000998 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000999 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001000 to_book3s(vcpu)->hior = set_reg_val(id, *val);
1001 to_book3s(vcpu)->hior_explicit = true;
Paul Mackerras31f34382011-12-12 12:26:50 +00001002 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001003#ifdef CONFIG_VSX
1004 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
1005 long int i = id - KVM_REG_PPC_VSR0;
1006
1007 if (!cpu_has_feature(CPU_FTR_VSX)) {
1008 r = -ENXIO;
1009 break;
1010 }
1011 vcpu->arch.fpr[i] = val->vsxval[0];
1012 vcpu->arch.vsr[i] = val->vsxval[1];
1013 break;
1014 }
1015#endif /* CONFIG_VSX */
Paul Mackerras31f34382011-12-12 12:26:50 +00001016 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001017 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001018 break;
1019 }
1020
1021 return r;
1022}
1023
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001024int kvmppc_core_check_processor_compat(void)
1025{
1026 return 0;
1027}
1028
1029struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1030{
1031 struct kvmppc_vcpu_book3s *vcpu_book3s;
1032 struct kvm_vcpu *vcpu;
1033 int err = -ENOMEM;
1034 unsigned long p;
1035
1036 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
1037 if (!vcpu_book3s)
1038 goto out;
1039
1040 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1041 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1042 if (!vcpu_book3s->shadow_vcpu)
1043 goto free_vcpu;
1044
1045 vcpu = &vcpu_book3s->vcpu;
1046 err = kvm_vcpu_init(vcpu, kvm, id);
1047 if (err)
1048 goto free_shadow_vcpu;
1049
1050 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1051 /* the real shared page fills the last 4k of our page */
1052 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1053 if (!p)
1054 goto uninit_vcpu;
1055
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001056#ifdef CONFIG_PPC_BOOK3S_64
1057 /* default to book3s_64 (970fx) */
1058 vcpu->arch.pvr = 0x3C0301;
1059#else
1060 /* default to book3s_32 (750) */
1061 vcpu->arch.pvr = 0x84202;
1062#endif
1063 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1064 vcpu->arch.slb_nr = 64;
1065
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001066 vcpu->arch.shadow_msr = MSR_USER64;
1067
1068 err = kvmppc_mmu_init(vcpu);
1069 if (err < 0)
1070 goto uninit_vcpu;
1071
1072 return vcpu;
1073
1074uninit_vcpu:
1075 kvm_vcpu_uninit(vcpu);
1076free_shadow_vcpu:
1077 kfree(vcpu_book3s->shadow_vcpu);
1078free_vcpu:
1079 vfree(vcpu_book3s);
1080out:
1081 return ERR_PTR(err);
1082}
1083
1084void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1085{
1086 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1087
1088 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
1089 kvm_vcpu_uninit(vcpu);
1090 kfree(vcpu_book3s->shadow_vcpu);
1091 vfree(vcpu_book3s);
1092}
1093
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001094int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001095{
1096 int ret;
1097 double fpr[32][TS_FPRWIDTH];
1098 unsigned int fpscr;
1099 int fpexc_mode;
1100#ifdef CONFIG_ALTIVEC
1101 vector128 vr[32];
1102 vector128 vscr;
1103 unsigned long uninitialized_var(vrsave);
1104 int used_vr;
1105#endif
1106#ifdef CONFIG_VSX
1107 int used_vsr;
1108#endif
1109 ulong ext_msr;
1110
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001111 /* Check if we can run the vcpu at all */
1112 if (!vcpu->arch.sane) {
1113 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
Alexander Graf7d827142011-12-09 15:46:21 +01001114 ret = -EINVAL;
1115 goto out;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001116 }
1117
Alexander Grafe371f712011-12-19 13:36:55 +01001118 /*
1119 * Interrupts could be timers for the guest which we have to inject
1120 * again, so let's postpone them until we're in the guest and if we
1121 * really did time things so badly, then we just exit again due to
1122 * a host external interrupt.
1123 */
Alexander Grafbd2be682012-08-13 01:04:19 +02001124 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001125 ret = kvmppc_prepare_to_enter(vcpu);
1126 if (ret <= 0) {
Alexander Grafbd2be682012-08-13 01:04:19 +02001127 local_irq_enable();
Alexander Graf7d827142011-12-09 15:46:21 +01001128 goto out;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001129 }
1130
1131 /* Save FPU state in stack */
1132 if (current->thread.regs->msr & MSR_FP)
1133 giveup_fpu(current);
1134 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1135 fpscr = current->thread.fpscr.val;
1136 fpexc_mode = current->thread.fpexc_mode;
1137
1138#ifdef CONFIG_ALTIVEC
1139 /* Save Altivec state in stack */
1140 used_vr = current->thread.used_vr;
1141 if (used_vr) {
1142 if (current->thread.regs->msr & MSR_VEC)
1143 giveup_altivec(current);
1144 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1145 vscr = current->thread.vscr;
1146 vrsave = current->thread.vrsave;
1147 }
1148#endif
1149
1150#ifdef CONFIG_VSX
1151 /* Save VSX state in stack */
1152 used_vsr = current->thread.used_vsr;
1153 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Paul Mackerras28c483b2012-11-04 18:16:46 +00001154 __giveup_vsx(current);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001155#endif
1156
1157 /* Remember the MSR with disabled extensions */
1158 ext_msr = current->thread.regs->msr;
1159
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001160 /* Preload FPU if it's enabled */
1161 if (vcpu->arch.shared->msr & MSR_FP)
1162 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1163
Alexander Grafbd2be682012-08-13 01:04:19 +02001164 kvmppc_lazy_ee_enable();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001165
1166 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
1167
Alexander Graf24afa372012-08-12 12:42:30 +02001168 /* No need for kvm_guest_exit. It's done in handle_exit.
1169 We also get here with interrupts enabled. */
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001170
Paul Mackerras28c483b2012-11-04 18:16:46 +00001171 /* Make sure we save the guest FPU/Altivec/VSX state */
1172 kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX);
1173
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001174 current->thread.regs->msr = ext_msr;
1175
Paul Mackerras28c483b2012-11-04 18:16:46 +00001176 /* Restore FPU/VSX state from stack */
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001177 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1178 current->thread.fpscr.val = fpscr;
1179 current->thread.fpexc_mode = fpexc_mode;
1180
1181#ifdef CONFIG_ALTIVEC
1182 /* Restore Altivec state from stack */
1183 if (used_vr && current->thread.used_vr) {
1184 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1185 current->thread.vscr = vscr;
1186 current->thread.vrsave = vrsave;
1187 }
1188 current->thread.used_vr = used_vr;
1189#endif
1190
1191#ifdef CONFIG_VSX
1192 current->thread.used_vsr = used_vsr;
1193#endif
1194
Alexander Graf7d827142011-12-09 15:46:21 +01001195out:
Alexander Graf0652eaa2012-08-12 11:34:21 +02001196 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001197 return ret;
1198}
1199
Paul Mackerras82ed3612011-12-15 02:03:22 +00001200/*
1201 * Get (and clear) the dirty memory log for a memory slot.
1202 */
1203int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1204 struct kvm_dirty_log *log)
1205{
1206 struct kvm_memory_slot *memslot;
1207 struct kvm_vcpu *vcpu;
1208 ulong ga, ga_end;
1209 int is_dirty = 0;
1210 int r;
1211 unsigned long n;
1212
1213 mutex_lock(&kvm->slots_lock);
1214
1215 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1216 if (r)
1217 goto out;
1218
1219 /* If nothing is dirty, don't bother messing with page tables. */
1220 if (is_dirty) {
1221 memslot = id_to_memslot(kvm->memslots, log->slot);
1222
1223 ga = memslot->base_gfn << PAGE_SHIFT;
1224 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1225
1226 kvm_for_each_vcpu(n, vcpu, kvm)
1227 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1228
1229 n = kvm_dirty_bitmap_bytes(memslot);
1230 memset(memslot->dirty_bitmap, 0, n);
1231 }
1232
1233 r = 0;
1234out:
1235 mutex_unlock(&kvm->slots_lock);
1236 return r;
1237}
1238
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001239#ifdef CONFIG_PPC64
1240int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1241{
1242 /* No flags */
1243 info->flags = 0;
1244
1245 /* SLB is always 64 entries */
1246 info->slb_size = 64;
1247
1248 /* Standard 4k base page size segment */
1249 info->sps[0].page_shift = 12;
1250 info->sps[0].slb_enc = 0;
1251 info->sps[0].enc[0].page_shift = 12;
1252 info->sps[0].enc[0].pte_enc = 0;
1253
1254 /* Standard 16M large page size segment */
1255 info->sps[1].page_shift = 24;
1256 info->sps[1].slb_enc = SLB_VSID_L;
1257 info->sps[1].enc[0].page_shift = 24;
1258 info->sps[1].enc[0].pte_enc = 0;
1259
1260 return 0;
1261}
1262#endif /* CONFIG_PPC64 */
1263
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001264void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1265 struct kvm_memory_slot *dont)
1266{
1267}
1268
1269int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1270 unsigned long npages)
1271{
1272 return 0;
1273}
1274
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001275int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001276 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001277 struct kvm_userspace_memory_region *mem)
1278{
1279 return 0;
1280}
1281
1282void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001283 struct kvm_userspace_memory_region *mem,
1284 struct kvm_memory_slot old)
1285{
1286}
1287
1288void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001289{
1290}
1291
1292int kvmppc_core_init_vm(struct kvm *kvm)
1293{
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001294#ifdef CONFIG_PPC64
1295 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
1296#endif
1297
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001298 return 0;
1299}
1300
1301void kvmppc_core_destroy_vm(struct kvm *kvm)
1302{
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001303#ifdef CONFIG_PPC64
1304 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
1305#endif
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001306}
1307
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001308static int kvmppc_book3s_init(void)
1309{
1310 int r;
1311
1312 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1313 THIS_MODULE);
1314
1315 if (r)
1316 return r;
1317
1318 r = kvmppc_mmu_hpte_sysinit();
1319
1320 return r;
1321}
1322
1323static void kvmppc_book3s_exit(void)
1324{
1325 kvmppc_mmu_hpte_sysexit();
1326 kvm_exit();
1327}
1328
1329module_init(kvmppc_book3s_init);
1330module_exit(kvmppc_book3s_exit);