blob: 2f84ed807184b9f3dffac0ee7f5be3c350926a10 [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>
Ian Munsiea413f472012-12-03 18:36:13 +000037#include <asm/firmware.h>
Paul Mackerrasdeb26c22013-02-04 18:11:44 +000038#include <asm/hvcall.h>
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000039#include <linux/gfp.h>
40#include <linux/sched.h>
41#include <linux/vmalloc.h>
42#include <linux/highmem.h>
43
44#include "trace.h"
45
46/* #define EXIT_DEBUG */
47/* #define DEBUG_EXT */
48
49static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
50 ulong msr);
51
52/* Some compatibility defines */
53#ifdef CONFIG_PPC_BOOK3S_32
54#define MSR_USER32 MSR_USER
55#define MSR_USER64 MSR_USER
56#define HW_PAGE_SIZE PAGE_SIZE
57#endif
58
59void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
60{
61#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf468a12c2011-12-09 14:44:13 +010062 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
63 memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb));
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
Paul Mackerras3ff95502013-09-20 14:52:49 +100069 current->thread.kvm_shadow_vcpu = vcpu->arch.shadow_vcpu;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000070#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));
Alexander Graf468a12c2011-12-09 14:44:13 +010078 to_book3s(vcpu)->slb_shadow_max = svcpu->slb_max;
79 svcpu_put(svcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000080#endif
81
Paul Mackerras28c483b2012-11-04 18:16:46 +000082 kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX);
Paul Mackerrasa47d72f2012-09-20 19:35:51 +000083 vcpu->cpu = -1;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +000084}
85
Paul Mackerrasa2d56022013-09-20 14:52:43 +100086/* Copy data needed by real-mode code from vcpu to shadow vcpu */
87void kvmppc_copy_to_svcpu(struct kvmppc_book3s_shadow_vcpu *svcpu,
88 struct kvm_vcpu *vcpu)
89{
90 svcpu->gpr[0] = vcpu->arch.gpr[0];
91 svcpu->gpr[1] = vcpu->arch.gpr[1];
92 svcpu->gpr[2] = vcpu->arch.gpr[2];
93 svcpu->gpr[3] = vcpu->arch.gpr[3];
94 svcpu->gpr[4] = vcpu->arch.gpr[4];
95 svcpu->gpr[5] = vcpu->arch.gpr[5];
96 svcpu->gpr[6] = vcpu->arch.gpr[6];
97 svcpu->gpr[7] = vcpu->arch.gpr[7];
98 svcpu->gpr[8] = vcpu->arch.gpr[8];
99 svcpu->gpr[9] = vcpu->arch.gpr[9];
100 svcpu->gpr[10] = vcpu->arch.gpr[10];
101 svcpu->gpr[11] = vcpu->arch.gpr[11];
102 svcpu->gpr[12] = vcpu->arch.gpr[12];
103 svcpu->gpr[13] = vcpu->arch.gpr[13];
104 svcpu->cr = vcpu->arch.cr;
105 svcpu->xer = vcpu->arch.xer;
106 svcpu->ctr = vcpu->arch.ctr;
107 svcpu->lr = vcpu->arch.lr;
108 svcpu->pc = vcpu->arch.pc;
109}
110
111/* Copy data touched by real-mode code from shadow vcpu back to vcpu */
112void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu,
113 struct kvmppc_book3s_shadow_vcpu *svcpu)
114{
115 vcpu->arch.gpr[0] = svcpu->gpr[0];
116 vcpu->arch.gpr[1] = svcpu->gpr[1];
117 vcpu->arch.gpr[2] = svcpu->gpr[2];
118 vcpu->arch.gpr[3] = svcpu->gpr[3];
119 vcpu->arch.gpr[4] = svcpu->gpr[4];
120 vcpu->arch.gpr[5] = svcpu->gpr[5];
121 vcpu->arch.gpr[6] = svcpu->gpr[6];
122 vcpu->arch.gpr[7] = svcpu->gpr[7];
123 vcpu->arch.gpr[8] = svcpu->gpr[8];
124 vcpu->arch.gpr[9] = svcpu->gpr[9];
125 vcpu->arch.gpr[10] = svcpu->gpr[10];
126 vcpu->arch.gpr[11] = svcpu->gpr[11];
127 vcpu->arch.gpr[12] = svcpu->gpr[12];
128 vcpu->arch.gpr[13] = svcpu->gpr[13];
129 vcpu->arch.cr = svcpu->cr;
130 vcpu->arch.xer = svcpu->xer;
131 vcpu->arch.ctr = svcpu->ctr;
132 vcpu->arch.lr = svcpu->lr;
133 vcpu->arch.pc = svcpu->pc;
134 vcpu->arch.shadow_srr1 = svcpu->shadow_srr1;
135 vcpu->arch.fault_dar = svcpu->fault_dar;
136 vcpu->arch.fault_dsisr = svcpu->fault_dsisr;
137 vcpu->arch.last_inst = svcpu->last_inst;
138}
139
Alexander Graf7c973a22012-08-13 12:50:35 +0200140int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf03d25c52012-08-10 12:28:50 +0200141{
Alexander Graf7c973a22012-08-13 12:50:35 +0200142 int r = 1; /* Indicate we want to get back into the guest */
143
Alexander Graf9b0cb3c2012-08-10 13:23:55 +0200144 /* We misuse TLB_FLUSH to indicate that we want to clear
145 all shadow cache entries */
146 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
147 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf7c973a22012-08-13 12:50:35 +0200148
149 return r;
Alexander Graf03d25c52012-08-10 12:28:50 +0200150}
151
Alexander Graf9b0cb3c2012-08-10 13:23:55 +0200152/************* MMU Notifiers *************/
153
154int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
155{
156 trace_kvm_unmap_hva(hva);
157
158 /*
159 * Flush all shadow tlb entries everywhere. This is slow, but
160 * we are 100% sure that we catch the to be unmapped page
161 */
162 kvm_flush_remote_tlbs(kvm);
163
164 return 0;
165}
166
167int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
168{
169 /* kvm_unmap_hva flushes everything anyways */
170 kvm_unmap_hva(kvm, start);
171
172 return 0;
173}
174
175int kvm_age_hva(struct kvm *kvm, unsigned long hva)
176{
177 /* XXX could be more clever ;) */
178 return 0;
179}
180
181int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
182{
183 /* XXX could be more clever ;) */
184 return 0;
185}
186
187void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
188{
189 /* The page will get remapped properly on its next fault */
190 kvm_unmap_hva(kvm, hva);
191}
192
193/*****************************************/
194
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000195static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
196{
197 ulong smsr = vcpu->arch.shared->msr;
198
199 /* Guest MSR values */
Paul Mackerras3a2e7b02012-11-04 18:17:28 +0000200 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000201 /* Process MSR values */
202 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
203 /* External providers the guest reserved */
204 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
205 /* 64-bit Process MSR values */
206#ifdef CONFIG_PPC_BOOK3S_64
207 smsr |= MSR_ISF | MSR_HV;
208#endif
209 vcpu->arch.shadow_msr = smsr;
210}
211
212void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
213{
214 ulong old_msr = vcpu->arch.shared->msr;
215
216#ifdef EXIT_DEBUG
217 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
218#endif
219
220 msr &= to_book3s(vcpu)->msr_mask;
221 vcpu->arch.shared->msr = msr;
222 kvmppc_recalc_shadow_msr(vcpu);
223
224 if (msr & MSR_POW) {
225 if (!vcpu->arch.pending_exceptions) {
226 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100227 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000228 vcpu->stat.halt_wakeup++;
229
230 /* Unset POW bit after we woke up */
231 msr &= ~MSR_POW;
232 vcpu->arch.shared->msr = msr;
233 }
234 }
235
236 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
237 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
238 kvmppc_mmu_flush_segments(vcpu);
239 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
240
241 /* Preload magic page segment when in kernel mode */
242 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
243 struct kvm_vcpu_arch *a = &vcpu->arch;
244
245 if (msr & MSR_DR)
246 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
247 else
248 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
249 }
250 }
251
Benjamin Herrenschmidtbbcc9c02012-03-13 21:52:44 +0000252 /*
253 * When switching from 32 to 64-bit, we may have a stale 32-bit
254 * magic page around, we need to flush it. Typically 32-bit magic
255 * page will be instanciated when calling into RTAS. Note: We
256 * assume that such transition only happens while in kernel mode,
257 * ie, we never transition from user 32-bit to kernel 64-bit with
258 * a 32-bit magic page around.
259 */
260 if (vcpu->arch.magic_page_pa &&
261 !(old_msr & MSR_PR) && !(old_msr & MSR_SF) && (msr & MSR_SF)) {
262 /* going from RTAS to normal kernel code */
263 kvmppc_mmu_pte_flush(vcpu, (uint32_t)vcpu->arch.magic_page_pa,
264 ~0xFFFUL);
265 }
266
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000267 /* Preload FPU if it's enabled */
268 if (vcpu->arch.shared->msr & MSR_FP)
269 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
270}
271
272void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
273{
274 u32 host_pvr;
275
276 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
277 vcpu->arch.pvr = pvr;
278#ifdef CONFIG_PPC_BOOK3S_64
279 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
280 kvmppc_mmu_book3s_64_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200281 if (!to_book3s(vcpu)->hior_explicit)
282 to_book3s(vcpu)->hior = 0xfff00000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000283 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200284 vcpu->arch.cpu_type = KVM_CPU_3S_64;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000285 } else
286#endif
287 {
288 kvmppc_mmu_book3s_32_init(vcpu);
Alexander Graf1022fc32011-09-14 21:45:23 +0200289 if (!to_book3s(vcpu)->hior_explicit)
290 to_book3s(vcpu)->hior = 0;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000291 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200292 vcpu->arch.cpu_type = KVM_CPU_3S_32;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000293 }
294
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200295 kvmppc_sanity_check(vcpu);
296
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000297 /* If we are in hypervisor level on 970, we can tell the CPU to
298 * treat DCBZ as 32 bytes store */
299 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
300 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
301 !strcmp(cur_cpu_spec->platform, "ppc970"))
302 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
303
304 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
305 really needs them in a VM on Cell and force disable them. */
306 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
307 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
308
Paul Mackerrasa4a0f252013-09-20 14:52:44 +1000309 /*
310 * If they're asking for POWER6 or later, set the flag
311 * indicating that we can do multiple large page sizes
312 * and 1TB segments.
313 * Also set the flag that indicates that tlbie has the large
314 * page bit in the RB operand instead of the instruction.
315 */
316 switch (PVR_VER(pvr)) {
317 case PVR_POWER6:
318 case PVR_POWER7:
319 case PVR_POWER7p:
320 case PVR_POWER8:
321 vcpu->arch.hflags |= BOOK3S_HFLAG_MULTI_PGSIZE |
322 BOOK3S_HFLAG_NEW_TLBIE;
323 break;
324 }
325
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000326#ifdef CONFIG_PPC_BOOK3S_32
327 /* 32 bit Book3S always has 32 byte dcbz */
328 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
329#endif
330
331 /* On some CPUs we can execute paired single operations natively */
332 asm ( "mfpvr %0" : "=r"(host_pvr));
333 switch (host_pvr) {
334 case 0x00080200: /* lonestar 2.0 */
335 case 0x00088202: /* lonestar 2.2 */
336 case 0x70000100: /* gekko 1.0 */
337 case 0x00080100: /* gekko 2.0 */
338 case 0x00083203: /* gekko 2.3a */
339 case 0x00083213: /* gekko 2.3b */
340 case 0x00083204: /* gekko 2.4 */
341 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
342 case 0x00087200: /* broadway */
343 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
344 /* Enable HID2.PSE - in case we need it later */
345 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
346 }
347}
348
349/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
350 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
351 * emulate 32 bytes dcbz length.
352 *
353 * The Book3s_64 inventors also realized this case and implemented a special bit
354 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
355 *
356 * My approach here is to patch the dcbz instruction on executing pages.
357 */
358static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
359{
360 struct page *hpage;
361 u64 hpage_offset;
362 u32 *page;
363 int i;
364
365 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800366 if (is_error_page(hpage))
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000367 return;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000368
369 hpage_offset = pte->raddr & ~PAGE_MASK;
370 hpage_offset &= ~0xFFFULL;
371 hpage_offset /= 4;
372
373 get_page(hpage);
Cong Wang2480b202011-11-25 23:14:16 +0800374 page = kmap_atomic(hpage);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000375
376 /* patch dcbz into reserved instruction, so we trap */
377 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
378 if ((page[i] & 0xff0007ff) == INS_DCBZ)
379 page[i] &= 0xfffffff7;
380
Cong Wang2480b202011-11-25 23:14:16 +0800381 kunmap_atomic(page);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000382 put_page(hpage);
383}
384
385static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
386{
387 ulong mp_pa = vcpu->arch.magic_page_pa;
388
Benjamin Herrenschmidtbbcc9c02012-03-13 21:52:44 +0000389 if (!(vcpu->arch.shared->msr & MSR_SF))
390 mp_pa = (uint32_t)mp_pa;
391
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000392 if (unlikely(mp_pa) &&
393 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
394 return 1;
395 }
396
397 return kvm_is_visible_gfn(vcpu->kvm, gfn);
398}
399
400int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
401 ulong eaddr, int vec)
402{
403 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
Paul Mackerras93b159b2013-09-20 14:52:51 +1000404 bool iswrite = false;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000405 int r = RESUME_GUEST;
406 int relocated;
407 int page_found = 0;
408 struct kvmppc_pte pte;
409 bool is_mmio = false;
410 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
411 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
412 u64 vsid;
413
414 relocated = data ? dr : ir;
Paul Mackerras93b159b2013-09-20 14:52:51 +1000415 if (data && (vcpu->arch.fault_dsisr & DSISR_ISSTORE))
416 iswrite = true;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000417
418 /* Resolve real address if translation turned on */
419 if (relocated) {
Paul Mackerras93b159b2013-09-20 14:52:51 +1000420 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data, iswrite);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000421 } else {
422 pte.may_execute = true;
423 pte.may_read = true;
424 pte.may_write = true;
425 pte.raddr = eaddr & KVM_PAM;
426 pte.eaddr = eaddr;
427 pte.vpage = eaddr >> 12;
Paul Mackerrasc9029c32013-09-20 14:52:45 +1000428 pte.page_size = MMU_PAGE_64K;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000429 }
430
431 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
432 case 0:
433 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
434 break;
435 case MSR_DR:
436 case MSR_IR:
437 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
438
439 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
440 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
441 else
442 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
443 pte.vpage |= vsid;
444
445 if (vsid == -1)
446 page_found = -EINVAL;
447 break;
448 }
449
450 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
451 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
452 /*
453 * If we do the dcbz hack, we have to NX on every execution,
454 * so we can patch the executing code. This renders our guest
455 * NX-less.
456 */
457 pte.may_execute = !data;
458 }
459
460 if (page_found == -ENOENT) {
461 /* Page not found in guest PTE entries */
462 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000463 vcpu->arch.shared->dsisr = vcpu->arch.fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000464 vcpu->arch.shared->msr |=
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000465 vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000466 kvmppc_book3s_queue_irqprio(vcpu, vec);
467 } else if (page_found == -EPERM) {
468 /* Storage protection */
469 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000470 vcpu->arch.shared->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000471 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
472 vcpu->arch.shared->msr |=
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000473 vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000474 kvmppc_book3s_queue_irqprio(vcpu, vec);
475 } else if (page_found == -EINVAL) {
476 /* Page not found in guest SLB */
477 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
478 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
479 } else if (!is_mmio &&
480 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
Paul Mackerras93b159b2013-09-20 14:52:51 +1000481 if (data && !(vcpu->arch.fault_dsisr & DSISR_NOHPTE)) {
482 /*
483 * There is already a host HPTE there, presumably
484 * a read-only one for a page the guest thinks
485 * is writable, so get rid of it first.
486 */
487 kvmppc_mmu_unmap_page(vcpu, &pte);
488 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000489 /* The guest's PTE is not mapped yet. Map on the host */
Paul Mackerras93b159b2013-09-20 14:52:51 +1000490 kvmppc_mmu_map_page(vcpu, &pte, iswrite);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000491 if (data)
492 vcpu->stat.sp_storage++;
493 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
Paul Mackerras93b159b2013-09-20 14:52:51 +1000494 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000495 kvmppc_patch_dcbz(vcpu, &pte);
496 } else {
497 /* MMIO */
498 vcpu->stat.mmio_exits++;
499 vcpu->arch.paddr_accessed = pte.raddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100500 vcpu->arch.vaddr_accessed = pte.eaddr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000501 r = kvmppc_emulate_mmio(run, vcpu);
502 if ( r == RESUME_HOST_NV )
503 r = RESUME_HOST;
504 }
505
506 return r;
507}
508
509static inline int get_fpr_index(int i)
510{
Paul Mackerras28c483b2012-11-04 18:16:46 +0000511 return i * TS_FPRWIDTH;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000512}
513
514/* Give up external provider (FPU, Altivec, VSX) */
515void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
516{
517 struct thread_struct *t = &current->thread;
518 u64 *vcpu_fpr = vcpu->arch.fpr;
519#ifdef CONFIG_VSX
520 u64 *vcpu_vsx = vcpu->arch.vsr;
521#endif
522 u64 *thread_fpr = (u64*)t->fpr;
523 int i;
524
Paul Mackerras28c483b2012-11-04 18:16:46 +0000525 /*
526 * VSX instructions can access FP and vector registers, so if
527 * we are giving up VSX, make sure we give up FP and VMX as well.
528 */
529 if (msr & MSR_VSX)
530 msr |= MSR_FP | MSR_VEC;
531
532 msr &= vcpu->arch.guest_owned_ext;
533 if (!msr)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000534 return;
535
536#ifdef DEBUG_EXT
537 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
538#endif
539
Paul Mackerras28c483b2012-11-04 18:16:46 +0000540 if (msr & MSR_FP) {
541 /*
542 * Note that on CPUs with VSX, giveup_fpu stores
543 * both the traditional FP registers and the added VSX
544 * registers into thread.fpr[].
545 */
Paul Mackerras9d1ffdd2013-08-06 14:14:33 +1000546 if (current->thread.regs->msr & MSR_FP)
547 giveup_fpu(current);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000548 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
549 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
550
551 vcpu->arch.fpscr = t->fpscr.val;
Paul Mackerras28c483b2012-11-04 18:16:46 +0000552
553#ifdef CONFIG_VSX
554 if (cpu_has_feature(CPU_FTR_VSX))
555 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
556 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
557#endif
558 }
559
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000560#ifdef CONFIG_ALTIVEC
Paul Mackerras28c483b2012-11-04 18:16:46 +0000561 if (msr & MSR_VEC) {
Paul Mackerras9d1ffdd2013-08-06 14:14:33 +1000562 if (current->thread.regs->msr & MSR_VEC)
563 giveup_altivec(current);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000564 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
565 vcpu->arch.vscr = t->vscr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000566 }
Paul Mackerras28c483b2012-11-04 18:16:46 +0000567#endif
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000568
Paul Mackerras28c483b2012-11-04 18:16:46 +0000569 vcpu->arch.guest_owned_ext &= ~(msr | MSR_VSX);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000570 kvmppc_recalc_shadow_msr(vcpu);
571}
572
573static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
574{
575 ulong srr0 = kvmppc_get_pc(vcpu);
576 u32 last_inst = kvmppc_get_last_inst(vcpu);
577 int ret;
578
579 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
580 if (ret == -ENOENT) {
581 ulong msr = vcpu->arch.shared->msr;
582
583 msr = kvmppc_set_field(msr, 33, 33, 1);
584 msr = kvmppc_set_field(msr, 34, 36, 0);
585 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
586 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
587 return EMULATE_AGAIN;
588 }
589
590 return EMULATE_DONE;
591}
592
593static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
594{
595
596 /* Need to do paired single emulation? */
597 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
598 return EMULATE_DONE;
599
600 /* Read out the instruction */
601 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
602 /* Need to emulate */
603 return EMULATE_FAIL;
604
605 return EMULATE_AGAIN;
606}
607
608/* Handle external providers (FPU, Altivec, VSX) */
609static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
610 ulong msr)
611{
612 struct thread_struct *t = &current->thread;
613 u64 *vcpu_fpr = vcpu->arch.fpr;
614#ifdef CONFIG_VSX
615 u64 *vcpu_vsx = vcpu->arch.vsr;
616#endif
617 u64 *thread_fpr = (u64*)t->fpr;
618 int i;
619
620 /* When we have paired singles, we emulate in software */
621 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
622 return RESUME_GUEST;
623
624 if (!(vcpu->arch.shared->msr & msr)) {
625 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
626 return RESUME_GUEST;
627 }
628
Paul Mackerras28c483b2012-11-04 18:16:46 +0000629 if (msr == MSR_VSX) {
630 /* No VSX? Give an illegal instruction interrupt */
631#ifdef CONFIG_VSX
632 if (!cpu_has_feature(CPU_FTR_VSX))
633#endif
634 {
635 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
636 return RESUME_GUEST;
637 }
638
639 /*
640 * We have to load up all the FP and VMX registers before
641 * we can let the guest use VSX instructions.
642 */
643 msr = MSR_FP | MSR_VEC | MSR_VSX;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000644 }
645
Paul Mackerras28c483b2012-11-04 18:16:46 +0000646 /* See if we already own all the ext(s) needed */
647 msr &= ~vcpu->arch.guest_owned_ext;
648 if (!msr)
649 return RESUME_GUEST;
650
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000651#ifdef DEBUG_EXT
652 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
653#endif
654
Paul Mackerras28c483b2012-11-04 18:16:46 +0000655 if (msr & MSR_FP) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000656 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
657 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
Paul Mackerras28c483b2012-11-04 18:16:46 +0000658#ifdef CONFIG_VSX
659 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
660 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
661#endif
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000662 t->fpscr.val = vcpu->arch.fpscr;
663 t->fpexc_mode = 0;
664 kvmppc_load_up_fpu();
Paul Mackerras28c483b2012-11-04 18:16:46 +0000665 }
666
667 if (msr & MSR_VEC) {
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000668#ifdef CONFIG_ALTIVEC
669 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
670 t->vscr = vcpu->arch.vscr;
671 t->vrsave = -1;
672 kvmppc_load_up_altivec();
673#endif
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000674 }
675
Paul Mackerras9d1ffdd2013-08-06 14:14:33 +1000676 current->thread.regs->msr |= msr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000677 vcpu->arch.guest_owned_ext |= msr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000678 kvmppc_recalc_shadow_msr(vcpu);
679
680 return RESUME_GUEST;
681}
682
Paul Mackerras9d1ffdd2013-08-06 14:14:33 +1000683/*
684 * Kernel code using FP or VMX could have flushed guest state to
685 * the thread_struct; if so, get it back now.
686 */
687static void kvmppc_handle_lost_ext(struct kvm_vcpu *vcpu)
688{
689 unsigned long lost_ext;
690
691 lost_ext = vcpu->arch.guest_owned_ext & ~current->thread.regs->msr;
692 if (!lost_ext)
693 return;
694
695 if (lost_ext & MSR_FP)
696 kvmppc_load_up_fpu();
Paul Mackerrasf2481772013-09-20 14:52:42 +1000697#ifdef CONFIG_ALTIVEC
Paul Mackerras9d1ffdd2013-08-06 14:14:33 +1000698 if (lost_ext & MSR_VEC)
699 kvmppc_load_up_altivec();
Paul Mackerrasf2481772013-09-20 14:52:42 +1000700#endif
Paul Mackerras9d1ffdd2013-08-06 14:14:33 +1000701 current->thread.regs->msr |= lost_ext;
702}
703
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000704int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
705 unsigned int exit_nr)
706{
707 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200708 int s;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000709
710 vcpu->stat.sum_exits++;
711
712 run->exit_reason = KVM_EXIT_UNKNOWN;
713 run->ready_for_interrupt_injection = 1;
714
Alexander Grafbd2be682012-08-13 01:04:19 +0200715 /* We get here with MSR.EE=1 */
Alexander Graf3b1d9d72012-04-30 10:56:12 +0200716
Alexander Graf97c95052012-08-02 15:10:00 +0200717 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200718 kvm_guest_exit();
Alexander Grafc63ddcb2012-08-12 11:27:49 +0200719
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000720 switch (exit_nr) {
721 case BOOK3S_INTERRUPT_INST_STORAGE:
Alexander Graf468a12c2011-12-09 14:44:13 +0100722 {
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000723 ulong shadow_srr1 = vcpu->arch.shadow_srr1;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000724 vcpu->stat.pf_instruc++;
725
726#ifdef CONFIG_PPC_BOOK3S_32
727 /* We set segments as unused segments when invalidating them. So
728 * treat the respective fault as segment fault. */
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000729 {
730 struct kvmppc_book3s_shadow_vcpu *svcpu;
731 u32 sr;
732
733 svcpu = svcpu_get(vcpu);
734 sr = svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT];
Alexander Graf468a12c2011-12-09 14:44:13 +0100735 svcpu_put(svcpu);
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000736 if (sr == SR_INVALID) {
737 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
738 r = RESUME_GUEST;
739 break;
740 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000741 }
742#endif
743
744 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graf468a12c2011-12-09 14:44:13 +0100745 if (shadow_srr1 & 0x40000000) {
Paul Mackerras93b159b2013-09-20 14:52:51 +1000746 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000747 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Paul Mackerras93b159b2013-09-20 14:52:51 +1000748 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000749 vcpu->stat.sp_instruc++;
750 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
751 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
752 /*
753 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
754 * so we can't use the NX bit inside the guest. Let's cross our fingers,
755 * that no guest that needs the dcbz hack does NX.
756 */
757 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
758 r = RESUME_GUEST;
759 } else {
Alexander Graf468a12c2011-12-09 14:44:13 +0100760 vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000761 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
762 r = RESUME_GUEST;
763 }
764 break;
Alexander Graf468a12c2011-12-09 14:44:13 +0100765 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000766 case BOOK3S_INTERRUPT_DATA_STORAGE:
767 {
768 ulong dar = kvmppc_get_fault_dar(vcpu);
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000769 u32 fault_dsisr = vcpu->arch.fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000770 vcpu->stat.pf_storage++;
771
772#ifdef CONFIG_PPC_BOOK3S_32
773 /* We set segments as unused segments when invalidating them. So
774 * treat the respective fault as segment fault. */
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000775 {
776 struct kvmppc_book3s_shadow_vcpu *svcpu;
777 u32 sr;
778
779 svcpu = svcpu_get(vcpu);
780 sr = svcpu->sr[dar >> SID_SHIFT];
Alexander Graf468a12c2011-12-09 14:44:13 +0100781 svcpu_put(svcpu);
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000782 if (sr == SR_INVALID) {
783 kvmppc_mmu_map_segment(vcpu, dar);
784 r = RESUME_GUEST;
785 break;
786 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000787 }
788#endif
789
Paul Mackerras93b159b2013-09-20 14:52:51 +1000790 /*
791 * We need to handle missing shadow PTEs, and
792 * protection faults due to us mapping a page read-only
793 * when the guest thinks it is writable.
794 */
795 if (fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT)) {
796 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000797 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Paul Mackerras93b159b2013-09-20 14:52:51 +1000798 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000799 } else {
800 vcpu->arch.shared->dar = dar;
Alexander Graf468a12c2011-12-09 14:44:13 +0100801 vcpu->arch.shared->dsisr = fault_dsisr;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000802 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
803 r = RESUME_GUEST;
804 }
805 break;
806 }
807 case BOOK3S_INTERRUPT_DATA_SEGMENT:
808 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
809 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
810 kvmppc_book3s_queue_irqprio(vcpu,
811 BOOK3S_INTERRUPT_DATA_SEGMENT);
812 }
813 r = RESUME_GUEST;
814 break;
815 case BOOK3S_INTERRUPT_INST_SEGMENT:
816 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
817 kvmppc_book3s_queue_irqprio(vcpu,
818 BOOK3S_INTERRUPT_INST_SEGMENT);
819 }
820 r = RESUME_GUEST;
821 break;
822 /* We're good on these - the host merely wanted to get our attention */
823 case BOOK3S_INTERRUPT_DECREMENTER:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100824 case BOOK3S_INTERRUPT_HV_DECREMENTER:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000825 vcpu->stat.dec_exits++;
826 r = RESUME_GUEST;
827 break;
828 case BOOK3S_INTERRUPT_EXTERNAL:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100829 case BOOK3S_INTERRUPT_EXTERNAL_LEVEL:
830 case BOOK3S_INTERRUPT_EXTERNAL_HV:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000831 vcpu->stat.ext_intr_exits++;
832 r = RESUME_GUEST;
833 break;
834 case BOOK3S_INTERRUPT_PERFMON:
835 r = RESUME_GUEST;
836 break;
837 case BOOK3S_INTERRUPT_PROGRAM:
Alexander Graf4f225ae2012-03-13 23:05:16 +0100838 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000839 {
840 enum emulation_result er;
841 ulong flags;
842
843program_interrupt:
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000844 flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000845
846 if (vcpu->arch.shared->msr & MSR_PR) {
847#ifdef EXIT_DEBUG
848 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
849#endif
850 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
851 (INS_DCBZ & 0xfffffff7)) {
852 kvmppc_core_queue_program(vcpu, flags);
853 r = RESUME_GUEST;
854 break;
855 }
856 }
857
858 vcpu->stat.emulated_inst_exits++;
859 er = kvmppc_emulate_instruction(run, vcpu);
860 switch (er) {
861 case EMULATE_DONE:
862 r = RESUME_GUEST_NV;
863 break;
864 case EMULATE_AGAIN:
865 r = RESUME_GUEST;
866 break;
867 case EMULATE_FAIL:
868 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
869 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
870 kvmppc_core_queue_program(vcpu, flags);
871 r = RESUME_GUEST;
872 break;
873 case EMULATE_DO_MMIO:
874 run->exit_reason = KVM_EXIT_MMIO;
875 r = RESUME_HOST_NV;
876 break;
Bharat Bhushanc402a3f2013-04-08 00:32:13 +0000877 case EMULATE_EXIT_USER:
Alexander Graf50c7bb82012-12-14 23:42:05 +0100878 r = RESUME_HOST_NV;
879 break;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000880 default:
881 BUG();
882 }
883 break;
884 }
885 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafa668f2b2011-08-08 17:26:24 +0200886 if (vcpu->arch.papr_enabled &&
Paul Mackerras8b23de22013-08-06 14:15:19 +1000887 (kvmppc_get_last_sc(vcpu) == 0x44000022) &&
Alexander Grafa668f2b2011-08-08 17:26:24 +0200888 !(vcpu->arch.shared->msr & MSR_PR)) {
889 /* SC 1 papr hypercalls */
890 ulong cmd = kvmppc_get_gpr(vcpu, 3);
891 int i;
892
Andreas Schwab96f38d72011-11-08 07:17:39 +0000893#ifdef CONFIG_KVM_BOOK3S_64_PR
Alexander Grafa668f2b2011-08-08 17:26:24 +0200894 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
895 r = RESUME_GUEST;
896 break;
897 }
Andreas Schwab96f38d72011-11-08 07:17:39 +0000898#endif
Alexander Grafa668f2b2011-08-08 17:26:24 +0200899
900 run->papr_hcall.nr = cmd;
901 for (i = 0; i < 9; ++i) {
902 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
903 run->papr_hcall.args[i] = gpr;
904 }
905 run->exit_reason = KVM_EXIT_PAPR_HCALL;
906 vcpu->arch.hcall_needed = 1;
907 r = RESUME_HOST;
908 } else if (vcpu->arch.osi_enabled &&
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000909 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
910 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
911 /* MOL hypercalls */
912 u64 *gprs = run->osi.gprs;
913 int i;
914
915 run->exit_reason = KVM_EXIT_OSI;
916 for (i = 0; i < 32; i++)
917 gprs[i] = kvmppc_get_gpr(vcpu, i);
918 vcpu->arch.osi_needed = 1;
919 r = RESUME_HOST_NV;
920 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
921 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
922 /* KVM PV hypercalls */
923 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
924 r = RESUME_GUEST;
925 } else {
926 /* Guest syscalls */
927 vcpu->stat.syscall_exits++;
928 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
929 r = RESUME_GUEST;
930 }
931 break;
932 case BOOK3S_INTERRUPT_FP_UNAVAIL:
933 case BOOK3S_INTERRUPT_ALTIVEC:
934 case BOOK3S_INTERRUPT_VSX:
935 {
936 int ext_msr = 0;
937
938 switch (exit_nr) {
939 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
940 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
941 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
942 }
943
944 switch (kvmppc_check_ext(vcpu, exit_nr)) {
945 case EMULATE_DONE:
946 /* everything ok - let's enable the ext */
947 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
948 break;
949 case EMULATE_FAIL:
950 /* we need to emulate this instruction */
951 goto program_interrupt;
952 break;
953 default:
954 /* nothing to worry about - go again */
955 break;
956 }
957 break;
958 }
959 case BOOK3S_INTERRUPT_ALIGNMENT:
960 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
961 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
962 kvmppc_get_last_inst(vcpu));
963 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
964 kvmppc_get_last_inst(vcpu));
965 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
966 }
967 r = RESUME_GUEST;
968 break;
969 case BOOK3S_INTERRUPT_MACHINE_CHECK:
970 case BOOK3S_INTERRUPT_TRACE:
971 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
972 r = RESUME_GUEST;
973 break;
974 default:
Alexander Graf468a12c2011-12-09 14:44:13 +0100975 {
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000976 ulong shadow_srr1 = vcpu->arch.shadow_srr1;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000977 /* Ugh - bork here! What did we get? */
978 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Graf468a12c2011-12-09 14:44:13 +0100979 exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000980 r = RESUME_HOST;
981 BUG();
982 break;
983 }
Alexander Graf468a12c2011-12-09 14:44:13 +0100984 }
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000985
986 if (!(r & RESUME_HOST)) {
987 /* To avoid clobbering exit_reason, only check for signals if
988 * we aren't already exiting to userspace for some other
989 * reason. */
Alexander Grafe371f712011-12-19 13:36:55 +0100990
991 /*
992 * Interrupts could be timers for the guest which we have to
993 * inject again, so let's postpone them until we're in the guest
994 * and if we really did time things so badly, then we just exit
995 * again due to a host external interrupt.
996 */
Alexander Grafbd2be682012-08-13 01:04:19 +0200997 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200998 s = kvmppc_prepare_to_enter(vcpu);
999 if (s <= 0) {
Alexander Grafbd2be682012-08-13 01:04:19 +02001000 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001001 r = s;
Alexander Graf24afa372012-08-12 12:42:30 +02001002 } else {
Scott Wood5f1c2482013-07-10 17:47:39 -05001003 kvmppc_fix_ee_before_entry();
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001004 }
Paul Mackerras9d1ffdd2013-08-06 14:14:33 +10001005 kvmppc_handle_lost_ext(vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001006 }
1007
1008 trace_kvm_book3s_reenter(r, vcpu);
1009
1010 return r;
1011}
1012
1013int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1014 struct kvm_sregs *sregs)
1015{
1016 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1017 int i;
1018
1019 sregs->pvr = vcpu->arch.pvr;
1020
1021 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1022 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1023 for (i = 0; i < 64; i++) {
1024 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
1025 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1026 }
1027 } else {
1028 for (i = 0; i < 16; i++)
1029 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
1030
1031 for (i = 0; i < 8; i++) {
1032 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1033 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1034 }
1035 }
1036
1037 return 0;
1038}
1039
1040int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1041 struct kvm_sregs *sregs)
1042{
1043 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1044 int i;
1045
1046 kvmppc_set_pvr(vcpu, sregs->pvr);
1047
1048 vcpu3s->sdr1 = sregs->u.s.sdr1;
1049 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1050 for (i = 0; i < 64; i++) {
1051 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1052 sregs->u.s.ppc64.slb[i].slbe);
1053 }
1054 } else {
1055 for (i = 0; i < 16; i++) {
1056 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1057 }
1058 for (i = 0; i < 8; i++) {
1059 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1060 (u32)sregs->u.s.ppc32.ibat[i]);
1061 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1062 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1063 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1064 (u32)sregs->u.s.ppc32.dbat[i]);
1065 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1066 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1067 }
1068 }
1069
1070 /* Flush the MMU after messing with the segments */
1071 kvmppc_mmu_pte_flush(vcpu, 0, 0);
1072
1073 return 0;
1074}
1075
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001076int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001077{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001078 int r = 0;
Paul Mackerras31f34382011-12-12 12:26:50 +00001079
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001080 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001081 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001082 *val = get_reg_val(id, to_book3s(vcpu)->hior);
Paul Mackerras31f34382011-12-12 12:26:50 +00001083 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001084#ifdef CONFIG_VSX
1085 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
1086 long int i = id - KVM_REG_PPC_VSR0;
1087
1088 if (!cpu_has_feature(CPU_FTR_VSX)) {
1089 r = -ENXIO;
1090 break;
1091 }
1092 val->vsxval[0] = vcpu->arch.fpr[i];
1093 val->vsxval[1] = vcpu->arch.vsr[i];
1094 break;
1095 }
1096#endif /* CONFIG_VSX */
Paul Mackerras31f34382011-12-12 12:26:50 +00001097 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001098 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001099 break;
1100 }
1101
1102 return r;
1103}
1104
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001105int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001106{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001107 int r = 0;
Paul Mackerras31f34382011-12-12 12:26:50 +00001108
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001109 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001110 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001111 to_book3s(vcpu)->hior = set_reg_val(id, *val);
1112 to_book3s(vcpu)->hior_explicit = true;
Paul Mackerras31f34382011-12-12 12:26:50 +00001113 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001114#ifdef CONFIG_VSX
1115 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
1116 long int i = id - KVM_REG_PPC_VSR0;
1117
1118 if (!cpu_has_feature(CPU_FTR_VSX)) {
1119 r = -ENXIO;
1120 break;
1121 }
1122 vcpu->arch.fpr[i] = val->vsxval[0];
1123 vcpu->arch.vsr[i] = val->vsxval[1];
1124 break;
1125 }
1126#endif /* CONFIG_VSX */
Paul Mackerras31f34382011-12-12 12:26:50 +00001127 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001128 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001129 break;
1130 }
1131
1132 return r;
1133}
1134
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001135int kvmppc_core_check_processor_compat(void)
1136{
1137 return 0;
1138}
1139
1140struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1141{
1142 struct kvmppc_vcpu_book3s *vcpu_book3s;
1143 struct kvm_vcpu *vcpu;
1144 int err = -ENOMEM;
1145 unsigned long p;
1146
Paul Mackerras3ff95502013-09-20 14:52:49 +10001147 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1148 if (!vcpu)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001149 goto out;
1150
Paul Mackerras3ff95502013-09-20 14:52:49 +10001151 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
1152 if (!vcpu_book3s)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001153 goto free_vcpu;
Paul Mackerras3ff95502013-09-20 14:52:49 +10001154 vcpu->arch.book3s = vcpu_book3s;
1155
1156#ifdef CONFIG_KVM_BOOK3S_32
1157 vcpu->arch.shadow_vcpu =
1158 kzalloc(sizeof(*vcpu->arch.shadow_vcpu), GFP_KERNEL);
1159 if (!vcpu->arch.shadow_vcpu)
1160 goto free_vcpu3s;
Paul Mackerrasa2d56022013-09-20 14:52:43 +10001161#endif
Paul Mackerras3ff95502013-09-20 14:52:49 +10001162
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001163 err = kvm_vcpu_init(vcpu, kvm, id);
1164 if (err)
1165 goto free_shadow_vcpu;
1166
Thadeu Lima de Souza Cascardo7c7b4062013-07-17 12:10:29 -03001167 err = -ENOMEM;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001168 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001169 if (!p)
1170 goto uninit_vcpu;
Thadeu Lima de Souza Cascardo7c7b4062013-07-17 12:10:29 -03001171 /* the real shared page fills the last 4k of our page */
1172 vcpu->arch.shared = (void *)(p + PAGE_SIZE - 4096);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001173
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001174#ifdef CONFIG_PPC_BOOK3S_64
Paul Mackerrasa4a0f252013-09-20 14:52:44 +10001175 /*
1176 * Default to the same as the host if we're on sufficiently
1177 * recent machine that we have 1TB segments;
1178 * otherwise default to PPC970FX.
1179 */
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001180 vcpu->arch.pvr = 0x3C0301;
Paul Mackerrasa4a0f252013-09-20 14:52:44 +10001181 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1182 vcpu->arch.pvr = mfspr(SPRN_PVR);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001183#else
1184 /* default to book3s_32 (750) */
1185 vcpu->arch.pvr = 0x84202;
1186#endif
1187 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1188 vcpu->arch.slb_nr = 64;
1189
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001190 vcpu->arch.shadow_msr = MSR_USER64;
1191
1192 err = kvmppc_mmu_init(vcpu);
1193 if (err < 0)
1194 goto uninit_vcpu;
1195
1196 return vcpu;
1197
1198uninit_vcpu:
1199 kvm_vcpu_uninit(vcpu);
1200free_shadow_vcpu:
Paul Mackerrasa2d56022013-09-20 14:52:43 +10001201#ifdef CONFIG_KVM_BOOK3S_32
Paul Mackerras3ff95502013-09-20 14:52:49 +10001202 kfree(vcpu->arch.shadow_vcpu);
1203free_vcpu3s:
Paul Mackerrasa2d56022013-09-20 14:52:43 +10001204#endif
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001205 vfree(vcpu_book3s);
Paul Mackerras3ff95502013-09-20 14:52:49 +10001206free_vcpu:
1207 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001208out:
1209 return ERR_PTR(err);
1210}
1211
1212void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1213{
1214 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1215
1216 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
1217 kvm_vcpu_uninit(vcpu);
Paul Mackerras3ff95502013-09-20 14:52:49 +10001218#ifdef CONFIG_KVM_BOOK3S_32
1219 kfree(vcpu->arch.shadow_vcpu);
1220#endif
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001221 vfree(vcpu_book3s);
Paul Mackerras3ff95502013-09-20 14:52:49 +10001222 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001223}
1224
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001225int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001226{
1227 int ret;
1228 double fpr[32][TS_FPRWIDTH];
1229 unsigned int fpscr;
1230 int fpexc_mode;
1231#ifdef CONFIG_ALTIVEC
1232 vector128 vr[32];
1233 vector128 vscr;
1234 unsigned long uninitialized_var(vrsave);
1235 int used_vr;
1236#endif
1237#ifdef CONFIG_VSX
1238 int used_vsr;
1239#endif
1240 ulong ext_msr;
1241
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001242 /* Check if we can run the vcpu at all */
1243 if (!vcpu->arch.sane) {
1244 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
Alexander Graf7d827142011-12-09 15:46:21 +01001245 ret = -EINVAL;
1246 goto out;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001247 }
1248
Alexander Grafe371f712011-12-19 13:36:55 +01001249 /*
1250 * Interrupts could be timers for the guest which we have to inject
1251 * again, so let's postpone them until we're in the guest and if we
1252 * really did time things so badly, then we just exit again due to
1253 * a host external interrupt.
1254 */
Alexander Grafbd2be682012-08-13 01:04:19 +02001255 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001256 ret = kvmppc_prepare_to_enter(vcpu);
1257 if (ret <= 0) {
Alexander Grafbd2be682012-08-13 01:04:19 +02001258 local_irq_enable();
Alexander Graf7d827142011-12-09 15:46:21 +01001259 goto out;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001260 }
1261
1262 /* Save FPU state in stack */
1263 if (current->thread.regs->msr & MSR_FP)
1264 giveup_fpu(current);
1265 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1266 fpscr = current->thread.fpscr.val;
1267 fpexc_mode = current->thread.fpexc_mode;
1268
1269#ifdef CONFIG_ALTIVEC
1270 /* Save Altivec state in stack */
1271 used_vr = current->thread.used_vr;
1272 if (used_vr) {
1273 if (current->thread.regs->msr & MSR_VEC)
1274 giveup_altivec(current);
1275 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1276 vscr = current->thread.vscr;
1277 vrsave = current->thread.vrsave;
1278 }
1279#endif
1280
1281#ifdef CONFIG_VSX
1282 /* Save VSX state in stack */
1283 used_vsr = current->thread.used_vsr;
1284 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Paul Mackerras28c483b2012-11-04 18:16:46 +00001285 __giveup_vsx(current);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001286#endif
1287
1288 /* Remember the MSR with disabled extensions */
1289 ext_msr = current->thread.regs->msr;
1290
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001291 /* Preload FPU if it's enabled */
1292 if (vcpu->arch.shared->msr & MSR_FP)
1293 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1294
Scott Wood5f1c2482013-07-10 17:47:39 -05001295 kvmppc_fix_ee_before_entry();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +00001296
1297 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
1298
Alexander Graf24afa372012-08-12 12:42:30 +02001299 /* No need for kvm_guest_exit. It's done in handle_exit.
1300 We also get here with interrupts enabled. */
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001301
Paul Mackerras28c483b2012-11-04 18:16:46 +00001302 /* Make sure we save the guest FPU/Altivec/VSX state */
1303 kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX);
1304
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001305 current->thread.regs->msr = ext_msr;
1306
Paul Mackerras28c483b2012-11-04 18:16:46 +00001307 /* Restore FPU/VSX state from stack */
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001308 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1309 current->thread.fpscr.val = fpscr;
1310 current->thread.fpexc_mode = fpexc_mode;
1311
1312#ifdef CONFIG_ALTIVEC
1313 /* Restore Altivec state from stack */
1314 if (used_vr && current->thread.used_vr) {
1315 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1316 current->thread.vscr = vscr;
1317 current->thread.vrsave = vrsave;
1318 }
1319 current->thread.used_vr = used_vr;
1320#endif
1321
1322#ifdef CONFIG_VSX
1323 current->thread.used_vsr = used_vsr;
1324#endif
1325
Alexander Graf7d827142011-12-09 15:46:21 +01001326out:
Alexander Graf0652eaa2012-08-12 11:34:21 +02001327 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001328 return ret;
1329}
1330
Paul Mackerras82ed3612011-12-15 02:03:22 +00001331/*
1332 * Get (and clear) the dirty memory log for a memory slot.
1333 */
1334int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1335 struct kvm_dirty_log *log)
1336{
1337 struct kvm_memory_slot *memslot;
1338 struct kvm_vcpu *vcpu;
1339 ulong ga, ga_end;
1340 int is_dirty = 0;
1341 int r;
1342 unsigned long n;
1343
1344 mutex_lock(&kvm->slots_lock);
1345
1346 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1347 if (r)
1348 goto out;
1349
1350 /* If nothing is dirty, don't bother messing with page tables. */
1351 if (is_dirty) {
1352 memslot = id_to_memslot(kvm->memslots, log->slot);
1353
1354 ga = memslot->base_gfn << PAGE_SHIFT;
1355 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1356
1357 kvm_for_each_vcpu(n, vcpu, kvm)
1358 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1359
1360 n = kvm_dirty_bitmap_bytes(memslot);
1361 memset(memslot->dirty_bitmap, 0, n);
1362 }
1363
1364 r = 0;
1365out:
1366 mutex_unlock(&kvm->slots_lock);
1367 return r;
1368}
1369
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001370#ifdef CONFIG_PPC64
1371int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1372{
Paul Mackerrasa4a0f252013-09-20 14:52:44 +10001373 long int i;
1374 struct kvm_vcpu *vcpu;
1375
1376 info->flags = 0;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001377
1378 /* SLB is always 64 entries */
1379 info->slb_size = 64;
1380
1381 /* Standard 4k base page size segment */
1382 info->sps[0].page_shift = 12;
1383 info->sps[0].slb_enc = 0;
1384 info->sps[0].enc[0].page_shift = 12;
1385 info->sps[0].enc[0].pte_enc = 0;
1386
Paul Mackerrasa4a0f252013-09-20 14:52:44 +10001387 /*
1388 * 64k large page size.
1389 * We only want to put this in if the CPUs we're emulating
1390 * support it, but unfortunately we don't have a vcpu easily
1391 * to hand here to test. Just pick the first vcpu, and if
1392 * that doesn't exist yet, report the minimum capability,
1393 * i.e., no 64k pages.
1394 * 1T segment support goes along with 64k pages.
1395 */
1396 i = 1;
1397 vcpu = kvm_get_vcpu(kvm, 0);
1398 if (vcpu && (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE)) {
1399 info->flags = KVM_PPC_1T_SEGMENTS;
1400 info->sps[i].page_shift = 16;
1401 info->sps[i].slb_enc = SLB_VSID_L | SLB_VSID_LP_01;
1402 info->sps[i].enc[0].page_shift = 16;
1403 info->sps[i].enc[0].pte_enc = 1;
1404 ++i;
1405 }
1406
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001407 /* Standard 16M large page size segment */
Paul Mackerrasa4a0f252013-09-20 14:52:44 +10001408 info->sps[i].page_shift = 24;
1409 info->sps[i].slb_enc = SLB_VSID_L;
1410 info->sps[i].enc[0].page_shift = 24;
1411 info->sps[i].enc[0].pte_enc = 0;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001412
1413 return 0;
1414}
1415#endif /* CONFIG_PPC64 */
1416
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001417void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1418 struct kvm_memory_slot *dont)
1419{
1420}
1421
1422int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1423 unsigned long npages)
1424{
1425 return 0;
1426}
1427
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001428int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001429 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001430 struct kvm_userspace_memory_region *mem)
1431{
1432 return 0;
1433}
1434
1435void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001436 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001437 const struct kvm_memory_slot *old)
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001438{
1439}
1440
1441void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001442{
1443}
1444
Ian Munsiea413f472012-12-03 18:36:13 +00001445static unsigned int kvm_global_user_count = 0;
1446static DEFINE_SPINLOCK(kvm_global_user_count_lock);
1447
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001448int kvmppc_core_init_vm(struct kvm *kvm)
1449{
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001450#ifdef CONFIG_PPC64
1451 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001452 INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001453#endif
Paul Mackerras9308ab82013-09-20 14:52:48 +10001454 mutex_init(&kvm->arch.hpt_mutex);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001455
Ian Munsiea413f472012-12-03 18:36:13 +00001456 if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
1457 spin_lock(&kvm_global_user_count_lock);
1458 if (++kvm_global_user_count == 1)
1459 pSeries_disable_reloc_on_exc();
1460 spin_unlock(&kvm_global_user_count_lock);
1461 }
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001462 return 0;
1463}
1464
1465void kvmppc_core_destroy_vm(struct kvm *kvm)
1466{
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001467#ifdef CONFIG_PPC64
1468 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
1469#endif
Ian Munsiea413f472012-12-03 18:36:13 +00001470
1471 if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
1472 spin_lock(&kvm_global_user_count_lock);
1473 BUG_ON(kvm_global_user_count == 0);
1474 if (--kvm_global_user_count == 0)
1475 pSeries_enable_reloc_on_exc();
1476 spin_unlock(&kvm_global_user_count_lock);
1477 }
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001478}
1479
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001480static int kvmppc_book3s_init(void)
1481{
1482 int r;
1483
Paul Mackerras3ff95502013-09-20 14:52:49 +10001484 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +00001485
1486 if (r)
1487 return r;
1488
1489 r = kvmppc_mmu_hpte_sysinit();
1490
1491 return r;
1492}
1493
1494static void kvmppc_book3s_exit(void)
1495{
1496 kvmppc_mmu_hpte_sysexit();
1497 kvm_exit();
1498}
1499
1500module_init(kvmppc_book3s_init);
1501module_exit(kvmppc_book3s_exit);