aboutsummaryrefslogtreecommitdiffstats
path: root/arch/powerpc/kvm
diff options
context:
space:
mode:
authorPaul Mackerras <paulus@samba.org>2011-12-12 07:36:37 -0500
committerAvi Kivity <avi@redhat.com>2012-03-05 07:52:37 -0500
commit697d3899dcb4bcd918d060a92db57b794e56b077 (patch)
tree173cdd849eca204fec8b64ea520b619372c3d970 /arch/powerpc/kvm
parent06ce2c63d933e347f8a199f123a8a293619ab3d2 (diff)
KVM: PPC: Implement MMIO emulation support for Book3S HV guests
This provides the low-level support for MMIO emulation in Book3S HV guests. When the guest tries to map a page which is not covered by any memslot, that page is taken to be an MMIO emulation page. Instead of inserting a valid HPTE, we insert an HPTE that has the valid bit clear but another hypervisor software-use bit set, which we call HPTE_V_ABSENT, to indicate that this is an absent page. An absent page is treated much like a valid page as far as guest hcalls (H_ENTER, H_REMOVE, H_READ etc.) are concerned, except of course that an absent HPTE doesn't need to be invalidated with tlbie since it was never valid as far as the hardware is concerned. When the guest accesses a page for which there is an absent HPTE, it will take a hypervisor data storage interrupt (HDSI) since we now set the VPM1 bit in the LPCR. Our HDSI handler for HPTE-not-present faults looks up the hash table and if it finds an absent HPTE mapping the requested virtual address, will switch to kernel mode and handle the fault in kvmppc_book3s_hv_page_fault(), which at present just calls kvmppc_hv_emulate_mmio() to set up the MMIO emulation. This is based on an earlier patch by Benjamin Herrenschmidt, but since heavily reworked. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Alexander Graf <agraf@suse.de> Signed-off-by: Avi Kivity <avi@redhat.com>
Diffstat (limited to 'arch/powerpc/kvm')
-rw-r--r--arch/powerpc/kvm/book3s_64_mmu_hv.c228
-rw-r--r--arch/powerpc/kvm/book3s_hv.c21
-rw-r--r--arch/powerpc/kvm/book3s_hv_rm_mmu.c262
-rw-r--r--arch/powerpc/kvm/book3s_hv_rmhandlers.S127
4 files changed, 561 insertions, 77 deletions
diff --git a/arch/powerpc/kvm/book3s_64_mmu_hv.c b/arch/powerpc/kvm/book3s_64_mmu_hv.c
index b904c40a17b..2d31519b863 100644
--- a/arch/powerpc/kvm/book3s_64_mmu_hv.c
+++ b/arch/powerpc/kvm/book3s_64_mmu_hv.c
@@ -34,8 +34,6 @@
34#include <asm/ppc-opcode.h> 34#include <asm/ppc-opcode.h>
35#include <asm/cputable.h> 35#include <asm/cputable.h>
36 36
37#define VRMA_VSID 0x1ffffffUL /* 1TB VSID reserved for VRMA */
38
39/* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */ 37/* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
40#define MAX_LPID_970 63 38#define MAX_LPID_970 63
41#define NR_LPIDS (LPID_RSVD + 1) 39#define NR_LPIDS (LPID_RSVD + 1)
@@ -298,16 +296,18 @@ long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
298 if (!psize) 296 if (!psize)
299 return H_PARAMETER; 297 return H_PARAMETER;
300 298
299 pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
300
301 /* Find the memslot (if any) for this address */ 301 /* Find the memslot (if any) for this address */
302 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1); 302 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
303 gfn = gpa >> PAGE_SHIFT; 303 gfn = gpa >> PAGE_SHIFT;
304 memslot = gfn_to_memslot(kvm, gfn); 304 memslot = gfn_to_memslot(kvm, gfn);
305 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) 305 if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) {
306 return H_PARAMETER; 306 if (!slot_is_aligned(memslot, psize))
307 if (!slot_is_aligned(memslot, psize)) 307 return H_PARAMETER;
308 return H_PARAMETER; 308 if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
309 if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0) 309 return H_PARAMETER;
310 return H_PARAMETER; 310 }
311 311
312 preempt_disable(); 312 preempt_disable();
313 ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel); 313 ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel);
@@ -321,10 +321,218 @@ long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
321 321
322} 322}
323 323
324static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
325 gva_t eaddr)
326{
327 u64 mask;
328 int i;
329
330 for (i = 0; i < vcpu->arch.slb_nr; i++) {
331 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
332 continue;
333
334 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
335 mask = ESID_MASK_1T;
336 else
337 mask = ESID_MASK;
338
339 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
340 return &vcpu->arch.slb[i];
341 }
342 return NULL;
343}
344
345static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
346 unsigned long ea)
347{
348 unsigned long ra_mask;
349
350 ra_mask = hpte_page_size(v, r) - 1;
351 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
352}
353
324static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, 354static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
325 struct kvmppc_pte *gpte, bool data) 355 struct kvmppc_pte *gpte, bool data)
326{ 356{
327 return -ENOENT; 357 struct kvm *kvm = vcpu->kvm;
358 struct kvmppc_slb *slbe;
359 unsigned long slb_v;
360 unsigned long pp, key;
361 unsigned long v, gr;
362 unsigned long *hptep;
363 int index;
364 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
365
366 /* Get SLB entry */
367 if (virtmode) {
368 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
369 if (!slbe)
370 return -EINVAL;
371 slb_v = slbe->origv;
372 } else {
373 /* real mode access */
374 slb_v = vcpu->kvm->arch.vrma_slb_v;
375 }
376
377 /* Find the HPTE in the hash table */
378 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
379 HPTE_V_VALID | HPTE_V_ABSENT);
380 if (index < 0)
381 return -ENOENT;
382 hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
383 v = hptep[0] & ~HPTE_V_HVLOCK;
384 gr = kvm->arch.revmap[index].guest_rpte;
385
386 /* Unlock the HPTE */
387 asm volatile("lwsync" : : : "memory");
388 hptep[0] = v;
389
390 gpte->eaddr = eaddr;
391 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
392
393 /* Get PP bits and key for permission check */
394 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
395 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
396 key &= slb_v;
397
398 /* Calculate permissions */
399 gpte->may_read = hpte_read_permission(pp, key);
400 gpte->may_write = hpte_write_permission(pp, key);
401 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
402
403 /* Storage key permission check for POWER7 */
404 if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) {
405 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
406 if (amrfield & 1)
407 gpte->may_read = 0;
408 if (amrfield & 2)
409 gpte->may_write = 0;
410 }
411
412 /* Get the guest physical address */
413 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
414 return 0;
415}
416
417/*
418 * Quick test for whether an instruction is a load or a store.
419 * If the instruction is a load or a store, then this will indicate
420 * which it is, at least on server processors. (Embedded processors
421 * have some external PID instructions that don't follow the rule
422 * embodied here.) If the instruction isn't a load or store, then
423 * this doesn't return anything useful.
424 */
425static int instruction_is_store(unsigned int instr)
426{
427 unsigned int mask;
428
429 mask = 0x10000000;
430 if ((instr & 0xfc000000) == 0x7c000000)
431 mask = 0x100; /* major opcode 31 */
432 return (instr & mask) != 0;
433}
434
435static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
436 unsigned long gpa, int is_store)
437{
438 int ret;
439 u32 last_inst;
440 unsigned long srr0 = kvmppc_get_pc(vcpu);
441
442 /* We try to load the last instruction. We don't let
443 * emulate_instruction do it as it doesn't check what
444 * kvmppc_ld returns.
445 * If we fail, we just return to the guest and try executing it again.
446 */
447 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) {
448 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
449 if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED)
450 return RESUME_GUEST;
451 vcpu->arch.last_inst = last_inst;
452 }
453
454 /*
455 * WARNING: We do not know for sure whether the instruction we just
456 * read from memory is the same that caused the fault in the first
457 * place. If the instruction we read is neither an load or a store,
458 * then it can't access memory, so we don't need to worry about
459 * enforcing access permissions. So, assuming it is a load or
460 * store, we just check that its direction (load or store) is
461 * consistent with the original fault, since that's what we
462 * checked the access permissions against. If there is a mismatch
463 * we just return and retry the instruction.
464 */
465
466 if (instruction_is_store(vcpu->arch.last_inst) != !!is_store)
467 return RESUME_GUEST;
468
469 /*
470 * Emulated accesses are emulated by looking at the hash for
471 * translation once, then performing the access later. The
472 * translation could be invalidated in the meantime in which
473 * point performing the subsequent memory access on the old
474 * physical address could possibly be a security hole for the
475 * guest (but not the host).
476 *
477 * This is less of an issue for MMIO stores since they aren't
478 * globally visible. It could be an issue for MMIO loads to
479 * a certain extent but we'll ignore it for now.
480 */
481
482 vcpu->arch.paddr_accessed = gpa;
483 return kvmppc_emulate_mmio(run, vcpu);
484}
485
486int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
487 unsigned long ea, unsigned long dsisr)
488{
489 struct kvm *kvm = vcpu->kvm;
490 unsigned long *hptep, hpte[3];
491 unsigned long psize;
492 unsigned long gfn;
493 struct kvm_memory_slot *memslot;
494 struct revmap_entry *rev;
495 long index;
496
497 /*
498 * Real-mode code has already searched the HPT and found the
499 * entry we're interested in. Lock the entry and check that
500 * it hasn't changed. If it has, just return and re-execute the
501 * instruction.
502 */
503 if (ea != vcpu->arch.pgfault_addr)
504 return RESUME_GUEST;
505 index = vcpu->arch.pgfault_index;
506 hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
507 rev = &kvm->arch.revmap[index];
508 preempt_disable();
509 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
510 cpu_relax();
511 hpte[0] = hptep[0] & ~HPTE_V_HVLOCK;
512 hpte[1] = hptep[1];
513 hpte[2] = rev->guest_rpte;
514 asm volatile("lwsync" : : : "memory");
515 hptep[0] = hpte[0];
516 preempt_enable();
517
518 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
519 hpte[1] != vcpu->arch.pgfault_hpte[1])
520 return RESUME_GUEST;
521
522 /* Translate the logical address and get the page */
523 psize = hpte_page_size(hpte[0], hpte[1]);
524 gfn = hpte_rpn(hpte[2], psize);
525 memslot = gfn_to_memslot(kvm, gfn);
526
527 /* No memslot means it's an emulated MMIO region */
528 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
529 unsigned long gpa = (gfn << PAGE_SHIFT) | (ea & (psize - 1));
530 return kvmppc_hv_emulate_mmio(run, vcpu, gpa,
531 dsisr & DSISR_ISSTORE);
532 }
533
534 /* should never get here otherwise */
535 return -EFAULT;
328} 536}
329 537
330void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa, 538void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
index 6ed0a84ef91..45aabb9a527 100644
--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -326,19 +326,18 @@ static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
326 break; 326 break;
327 } 327 }
328 /* 328 /*
329 * We get these next two if the guest does a bad real-mode access, 329 * We get this if the guest accesses a page which it thinks
330 * as we have enabled VRMA (virtualized real mode area) mode in the 330 * it has mapped but which is not actually present, because
331 * LPCR. We just generate an appropriate DSI/ISI to the guest. 331 * it is for an emulated I/O device.
332 * Any other HDSI interrupt has been handled already.
332 */ 333 */
333 case BOOK3S_INTERRUPT_H_DATA_STORAGE: 334 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
334 vcpu->arch.shregs.dsisr = vcpu->arch.fault_dsisr; 335 r = kvmppc_book3s_hv_page_fault(run, vcpu,
335 vcpu->arch.shregs.dar = vcpu->arch.fault_dar; 336 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
336 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE, 0);
337 r = RESUME_GUEST;
338 break; 337 break;
339 case BOOK3S_INTERRUPT_H_INST_STORAGE: 338 case BOOK3S_INTERRUPT_H_INST_STORAGE:
340 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_INST_STORAGE, 339 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_INST_STORAGE,
341 0x08000000); 340 vcpu->arch.shregs.msr & 0x58000000);
342 r = RESUME_GUEST; 341 r = RESUME_GUEST;
343 break; 342 break;
344 /* 343 /*
@@ -1195,6 +1194,8 @@ static int kvmppc_hv_setup_rma(struct kvm_vcpu *vcpu)
1195 1194
1196 /* Update VRMASD field in the LPCR */ 1195 /* Update VRMASD field in the LPCR */
1197 senc = slb_pgsize_encoding(psize); 1196 senc = slb_pgsize_encoding(psize);
1197 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1198 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1198 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD; 1199 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1199 lpcr |= senc << (LPCR_VRMASD_SH - 4); 1200 lpcr |= senc << (LPCR_VRMASD_SH - 4);
1200 kvm->arch.lpcr = lpcr; 1201 kvm->arch.lpcr = lpcr;
@@ -1291,7 +1292,9 @@ int kvmppc_core_init_vm(struct kvm *kvm)
1291 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR); 1292 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1292 lpcr &= LPCR_PECE | LPCR_LPES; 1293 lpcr &= LPCR_PECE | LPCR_LPES;
1293 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE | 1294 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
1294 LPCR_VPM0 | LPCR_VRMA_L; 1295 LPCR_VPM0 | LPCR_VPM1;
1296 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1297 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1295 } 1298 }
1296 kvm->arch.lpcr = lpcr; 1299 kvm->arch.lpcr = lpcr;
1297 1300
diff --git a/arch/powerpc/kvm/book3s_hv_rm_mmu.c b/arch/powerpc/kvm/book3s_hv_rm_mmu.c
index 5b31caa4b31..a5176dc37e7 100644
--- a/arch/powerpc/kvm/book3s_hv_rm_mmu.c
+++ b/arch/powerpc/kvm/book3s_hv_rm_mmu.c
@@ -136,13 +136,23 @@ long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
136 psize = hpte_page_size(pteh, ptel); 136 psize = hpte_page_size(pteh, ptel);
137 if (!psize) 137 if (!psize)
138 return H_PARAMETER; 138 return H_PARAMETER;
139 pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
139 140
140 /* Find the memslot (if any) for this address */ 141 /* Find the memslot (if any) for this address */
141 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1); 142 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
142 gfn = gpa >> PAGE_SHIFT; 143 gfn = gpa >> PAGE_SHIFT;
143 memslot = builtin_gfn_to_memslot(kvm, gfn); 144 memslot = builtin_gfn_to_memslot(kvm, gfn);
144 if (!(memslot && !(memslot->flags & KVM_MEMSLOT_INVALID))) 145 pa = 0;
145 return H_PARAMETER; 146 rmap = NULL;
147 if (!(memslot && !(memslot->flags & KVM_MEMSLOT_INVALID))) {
148 /* PPC970 can't do emulated MMIO */
149 if (!cpu_has_feature(CPU_FTR_ARCH_206))
150 return H_PARAMETER;
151 /* Emulated MMIO - mark this with key=31 */
152 pteh |= HPTE_V_ABSENT;
153 ptel |= HPTE_R_KEY_HI | HPTE_R_KEY_LO;
154 goto do_insert;
155 }
146 156
147 /* Check if the requested page fits entirely in the memslot. */ 157 /* Check if the requested page fits entirely in the memslot. */
148 if (!slot_is_aligned(memslot, psize)) 158 if (!slot_is_aligned(memslot, psize))
@@ -170,6 +180,7 @@ long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
170 180
171 ptel &= ~(HPTE_R_PP0 - psize); 181 ptel &= ~(HPTE_R_PP0 - psize);
172 ptel |= pa; 182 ptel |= pa;
183 pteh |= HPTE_V_VALID;
173 184
174 /* Check WIMG */ 185 /* Check WIMG */
175 if (!hpte_cache_flags_ok(ptel, is_io)) { 186 if (!hpte_cache_flags_ok(ptel, is_io)) {
@@ -182,9 +193,8 @@ long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
182 ptel &= ~(HPTE_R_W|HPTE_R_I|HPTE_R_G); 193 ptel &= ~(HPTE_R_W|HPTE_R_I|HPTE_R_G);
183 ptel |= HPTE_R_M; 194 ptel |= HPTE_R_M;
184 } 195 }
185 pteh &= ~0x60UL;
186 pteh |= HPTE_V_VALID;
187 196
197 do_insert:
188 if (pte_index >= HPT_NPTE) 198 if (pte_index >= HPT_NPTE)
189 return H_PARAMETER; 199 return H_PARAMETER;
190 if (likely((flags & H_EXACT) == 0)) { 200 if (likely((flags & H_EXACT) == 0)) {
@@ -192,7 +202,8 @@ long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
192 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4)); 202 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
193 for (i = 0; i < 8; ++i) { 203 for (i = 0; i < 8; ++i) {
194 if ((*hpte & HPTE_V_VALID) == 0 && 204 if ((*hpte & HPTE_V_VALID) == 0 &&
195 try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID)) 205 try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
206 HPTE_V_ABSENT))
196 break; 207 break;
197 hpte += 2; 208 hpte += 2;
198 } 209 }
@@ -207,7 +218,7 @@ long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
207 for (i = 0; i < 8; ++i) { 218 for (i = 0; i < 8; ++i) {
208 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK)) 219 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
209 cpu_relax(); 220 cpu_relax();
210 if ((*hpte & HPTE_V_VALID) == 0) 221 if (!(*hpte & (HPTE_V_VALID | HPTE_V_ABSENT)))
211 break; 222 break;
212 *hpte &= ~HPTE_V_HVLOCK; 223 *hpte &= ~HPTE_V_HVLOCK;
213 hpte += 2; 224 hpte += 2;
@@ -218,11 +229,12 @@ long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
218 pte_index += i; 229 pte_index += i;
219 } else { 230 } else {
220 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4)); 231 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
221 if (!try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID)) { 232 if (!try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
233 HPTE_V_ABSENT)) {
222 /* Lock the slot and check again */ 234 /* Lock the slot and check again */
223 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK)) 235 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
224 cpu_relax(); 236 cpu_relax();
225 if (*hpte & HPTE_V_VALID) { 237 if (*hpte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
226 *hpte &= ~HPTE_V_HVLOCK; 238 *hpte &= ~HPTE_V_HVLOCK;
227 return H_PTEG_FULL; 239 return H_PTEG_FULL;
228 } 240 }
@@ -237,10 +249,12 @@ long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
237 rev->guest_rpte = g_ptel; 249 rev->guest_rpte = g_ptel;
238 250
239 /* Link HPTE into reverse-map chain */ 251 /* Link HPTE into reverse-map chain */
240 if (realmode) 252 if (pteh & HPTE_V_VALID) {
241 rmap = real_vmalloc_addr(rmap); 253 if (realmode)
242 lock_rmap(rmap); 254 rmap = real_vmalloc_addr(rmap);
243 kvmppc_add_revmap_chain(kvm, rev, rmap, pte_index, realmode); 255 lock_rmap(rmap);
256 kvmppc_add_revmap_chain(kvm, rev, rmap, pte_index, realmode);
257 }
244 258
245 hpte[1] = ptel; 259 hpte[1] = ptel;
246 260
@@ -287,7 +301,7 @@ long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
287 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4)); 301 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
288 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK)) 302 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
289 cpu_relax(); 303 cpu_relax();
290 if ((hpte[0] & HPTE_V_VALID) == 0 || 304 if ((hpte[0] & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
291 ((flags & H_AVPN) && (hpte[0] & ~0x7fUL) != avpn) || 305 ((flags & H_AVPN) && (hpte[0] & ~0x7fUL) != avpn) ||
292 ((flags & H_ANDCOND) && (hpte[0] & avpn) != 0)) { 306 ((flags & H_ANDCOND) && (hpte[0] & avpn) != 0)) {
293 hpte[0] &= ~HPTE_V_HVLOCK; 307 hpte[0] &= ~HPTE_V_HVLOCK;
@@ -298,11 +312,14 @@ long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
298 vcpu->arch.gpr[4] = v = hpte[0] & ~HPTE_V_HVLOCK; 312 vcpu->arch.gpr[4] = v = hpte[0] & ~HPTE_V_HVLOCK;
299 vcpu->arch.gpr[5] = r = hpte[1]; 313 vcpu->arch.gpr[5] = r = hpte[1];
300 rb = compute_tlbie_rb(v, r, pte_index); 314 rb = compute_tlbie_rb(v, r, pte_index);
301 remove_revmap_chain(kvm, pte_index, v); 315 if (v & HPTE_V_VALID)
316 remove_revmap_chain(kvm, pte_index, v);
302 smp_wmb(); 317 smp_wmb();
303 hpte[0] = 0; 318 hpte[0] = 0;
319 if (!(v & HPTE_V_VALID))
320 return H_SUCCESS;
304 if (!(flags & H_LOCAL)) { 321 if (!(flags & H_LOCAL)) {
305 while(!try_lock_tlbie(&kvm->arch.tlbie_lock)) 322 while (!try_lock_tlbie(&kvm->arch.tlbie_lock))
306 cpu_relax(); 323 cpu_relax();
307 asm volatile("ptesync" : : : "memory"); 324 asm volatile("ptesync" : : : "memory");
308 asm volatile(PPC_TLBIE(%1,%0)"; eieio; tlbsync" 325 asm volatile(PPC_TLBIE(%1,%0)"; eieio; tlbsync"
@@ -349,7 +366,7 @@ long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu)
349 while (!try_lock_hpte(hp, HPTE_V_HVLOCK)) 366 while (!try_lock_hpte(hp, HPTE_V_HVLOCK))
350 cpu_relax(); 367 cpu_relax();
351 found = 0; 368 found = 0;
352 if (hp[0] & HPTE_V_VALID) { 369 if (hp[0] & (HPTE_V_ABSENT | HPTE_V_VALID)) {
353 switch (flags & 3) { 370 switch (flags & 3) {
354 case 0: /* absolute */ 371 case 0: /* absolute */
355 found = 1; 372 found = 1;
@@ -372,8 +389,10 @@ long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu)
372 /* insert R and C bits from PTE */ 389 /* insert R and C bits from PTE */
373 flags |= (hp[1] >> 5) & 0x0c; 390 flags |= (hp[1] >> 5) & 0x0c;
374 args[i * 2] = ((0x80 | flags) << 56) + pte_index; 391 args[i * 2] = ((0x80 | flags) << 56) + pte_index;
375 tlbrb[n_inval++] = compute_tlbie_rb(hp[0], hp[1], pte_index); 392 if (hp[0] & HPTE_V_VALID) {
376 remove_revmap_chain(kvm, pte_index, hp[0]); 393 tlbrb[n_inval++] = compute_tlbie_rb(hp[0], hp[1], pte_index);
394 remove_revmap_chain(kvm, pte_index, hp[0]);
395 }
377 smp_wmb(); 396 smp_wmb();
378 hp[0] = 0; 397 hp[0] = 0;
379 } 398 }
@@ -409,14 +428,16 @@ long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
409 428
410 if (pte_index >= HPT_NPTE) 429 if (pte_index >= HPT_NPTE)
411 return H_PARAMETER; 430 return H_PARAMETER;
431
412 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4)); 432 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
413 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK)) 433 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
414 cpu_relax(); 434 cpu_relax();
415 if ((hpte[0] & HPTE_V_VALID) == 0 || 435 if ((hpte[0] & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
416 ((flags & H_AVPN) && (hpte[0] & ~0x7fUL) != avpn)) { 436 ((flags & H_AVPN) && (hpte[0] & ~0x7fUL) != avpn)) {
417 hpte[0] &= ~HPTE_V_HVLOCK; 437 hpte[0] &= ~HPTE_V_HVLOCK;
418 return H_NOT_FOUND; 438 return H_NOT_FOUND;
419 } 439 }
440
420 if (atomic_read(&kvm->online_vcpus) == 1) 441 if (atomic_read(&kvm->online_vcpus) == 1)
421 flags |= H_LOCAL; 442 flags |= H_LOCAL;
422 v = hpte[0]; 443 v = hpte[0];
@@ -435,20 +456,22 @@ long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
435 r = (hpte[1] & ~mask) | bits; 456 r = (hpte[1] & ~mask) | bits;
436 457
437 /* Update HPTE */ 458 /* Update HPTE */
438 rb = compute_tlbie_rb(v, r, pte_index); 459 if (v & HPTE_V_VALID) {
439 hpte[0] = v & ~HPTE_V_VALID; 460 rb = compute_tlbie_rb(v, r, pte_index);
440 if (!(flags & H_LOCAL)) { 461 hpte[0] = v & ~HPTE_V_VALID;
441 while(!try_lock_tlbie(&kvm->arch.tlbie_lock)) 462 if (!(flags & H_LOCAL)) {
442 cpu_relax(); 463 while(!try_lock_tlbie(&kvm->arch.tlbie_lock))
443 asm volatile("ptesync" : : : "memory"); 464 cpu_relax();
444 asm volatile(PPC_TLBIE(%1,%0)"; eieio; tlbsync" 465 asm volatile("ptesync" : : : "memory");
445 : : "r" (rb), "r" (kvm->arch.lpid)); 466 asm volatile(PPC_TLBIE(%1,%0)"; eieio; tlbsync"
446 asm volatile("ptesync" : : : "memory"); 467 : : "r" (rb), "r" (kvm->arch.lpid));
447 kvm->arch.tlbie_lock = 0; 468 asm volatile("ptesync" : : : "memory");
448 } else { 469 kvm->arch.tlbie_lock = 0;
449 asm volatile("ptesync" : : : "memory"); 470 } else {
450 asm volatile("tlbiel %0" : : "r" (rb)); 471 asm volatile("ptesync" : : : "memory");
451 asm volatile("ptesync" : : : "memory"); 472 asm volatile("tlbiel %0" : : "r" (rb));
473 asm volatile("ptesync" : : : "memory");
474 }
452 } 475 }
453 hpte[1] = r; 476 hpte[1] = r;
454 eieio(); 477 eieio();
@@ -461,7 +484,7 @@ long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
461 unsigned long pte_index) 484 unsigned long pte_index)
462{ 485{
463 struct kvm *kvm = vcpu->kvm; 486 struct kvm *kvm = vcpu->kvm;
464 unsigned long *hpte, r; 487 unsigned long *hpte, v, r;
465 int i, n = 1; 488 int i, n = 1;
466 struct revmap_entry *rev = NULL; 489 struct revmap_entry *rev = NULL;
467 490
@@ -475,15 +498,182 @@ long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
475 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]); 498 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
476 for (i = 0; i < n; ++i, ++pte_index) { 499 for (i = 0; i < n; ++i, ++pte_index) {
477 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4)); 500 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
501 v = hpte[0] & ~HPTE_V_HVLOCK;
478 r = hpte[1]; 502 r = hpte[1];
479 if (hpte[0] & HPTE_V_VALID) { 503 if (v & HPTE_V_ABSENT) {
504 v &= ~HPTE_V_ABSENT;
505 v |= HPTE_V_VALID;
506 }
507 if (v & HPTE_V_VALID) {
480 if (rev) 508 if (rev)
481 r = rev[i].guest_rpte; 509 r = rev[i].guest_rpte;
482 else 510 else
483 r = hpte[1] | HPTE_R_RPN; 511 r = hpte[1] | HPTE_R_RPN;
484 } 512 }
485 vcpu->arch.gpr[4 + i * 2] = hpte[0]; 513 vcpu->arch.gpr[4 + i * 2] = v;
486 vcpu->arch.gpr[5 + i * 2] = r; 514 vcpu->arch.gpr[5 + i * 2] = r;
487 } 515 }
488 return H_SUCCESS; 516 return H_SUCCESS;
489} 517}
518
519static int slb_base_page_shift[4] = {
520 24, /* 16M */
521 16, /* 64k */
522 34, /* 16G */
523 20, /* 1M, unsupported */
524};
525
526long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, unsigned long slb_v,
527 unsigned long valid)
528{
529 unsigned int i;
530 unsigned int pshift;
531 unsigned long somask;
532 unsigned long vsid, hash;
533 unsigned long avpn;
534 unsigned long *hpte;
535 unsigned long mask, val;
536 unsigned long v, r;
537
538 /* Get page shift, work out hash and AVPN etc. */
539 mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_SECONDARY;
540 val = 0;
541 pshift = 12;
542 if (slb_v & SLB_VSID_L) {
543 mask |= HPTE_V_LARGE;
544 val |= HPTE_V_LARGE;
545 pshift = slb_base_page_shift[(slb_v & SLB_VSID_LP) >> 4];
546 }
547 if (slb_v & SLB_VSID_B_1T) {
548 somask = (1UL << 40) - 1;
549 vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T;
550 vsid ^= vsid << 25;
551 } else {
552 somask = (1UL << 28) - 1;
553 vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT;
554 }
555 hash = (vsid ^ ((eaddr & somask) >> pshift)) & HPT_HASH_MASK;
556 avpn = slb_v & ~(somask >> 16); /* also includes B */
557 avpn |= (eaddr & somask) >> 16;
558
559 if (pshift >= 24)
560 avpn &= ~((1UL << (pshift - 16)) - 1);
561 else
562 avpn &= ~0x7fUL;
563 val |= avpn;
564
565 for (;;) {
566 hpte = (unsigned long *)(kvm->arch.hpt_virt + (hash << 7));
567
568 for (i = 0; i < 16; i += 2) {
569 /* Read the PTE racily */
570 v = hpte[i] & ~HPTE_V_HVLOCK;
571
572 /* Check valid/absent, hash, segment size and AVPN */
573 if (!(v & valid) || (v & mask) != val)
574 continue;
575
576 /* Lock the PTE and read it under the lock */
577 while (!try_lock_hpte(&hpte[i], HPTE_V_HVLOCK))
578 cpu_relax();
579 v = hpte[i] & ~HPTE_V_HVLOCK;
580 r = hpte[i+1];
581
582 /*
583 * Check the HPTE again, including large page size
584 * Since we don't currently allow any MPSS (mixed
585 * page-size segment) page sizes, it is sufficient
586 * to check against the actual page size.
587 */
588 if ((v & valid) && (v & mask) == val &&
589 hpte_page_size(v, r) == (1ul << pshift))
590 /* Return with the HPTE still locked */
591 return (hash << 3) + (i >> 1);
592
593 /* Unlock and move on */
594 hpte[i] = v;
595 }
596
597 if (val & HPTE_V_SECONDARY)
598 break;
599 val |= HPTE_V_SECONDARY;
600 hash = hash ^ HPT_HASH_MASK;
601 }
602 return -1;
603}
604EXPORT_SYMBOL(kvmppc_hv_find_lock_hpte);
605
606/*
607 * Called in real mode to check whether an HPTE not found fault
608 * is due to accessing an emulated MMIO page.
609 * Returns a possibly modified status (DSISR) value if not
610 * (i.e. pass the interrupt to the guest),
611 * -1 to pass the fault up to host kernel mode code, -2 to do that
612 * and also load the instruction word,
613 * or 0 if we should make the guest retry the access.
614 */
615long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
616 unsigned long slb_v, unsigned int status)
617{
618 struct kvm *kvm = vcpu->kvm;
619 long int index;
620 unsigned long v, r, gr;
621 unsigned long *hpte;
622 unsigned long valid;
623 struct revmap_entry *rev;
624 unsigned long pp, key;
625
626 valid = HPTE_V_VALID | HPTE_V_ABSENT;
627 index = kvmppc_hv_find_lock_hpte(kvm, addr, slb_v, valid);
628 if (index < 0)
629 return status; /* there really was no HPTE */
630
631 hpte = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
632 v = hpte[0] & ~HPTE_V_HVLOCK;
633 r = hpte[1];
634 rev = real_vmalloc_addr(&kvm->arch.revmap[index]);
635 gr = rev->guest_rpte;
636
637 /* Unlock the HPTE */
638 asm volatile("lwsync" : : : "memory");
639 hpte[0] = v;
640
641 /* If the HPTE is valid by now, retry the instruction */
642 if (v & HPTE_V_VALID)
643 return 0;
644
645 /* Check access permissions to the page */
646 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
647 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
648 if (status & DSISR_ISSTORE) {
649 /* check write permission */
650 if (!hpte_write_permission(pp, slb_v & key))
651 goto protfault;
652 } else {
653 if (!hpte_read_permission(pp, slb_v & key))
654 goto protfault;
655 }
656
657 /* Check storage key, if applicable */
658 if (vcpu->arch.shregs.msr & MSR_DR) {
659 unsigned int perm = hpte_get_skey_perm(gr, vcpu->arch.amr);
660 if (status & DSISR_ISSTORE)
661 perm >>= 1;
662 if (perm & 1)
663 return (status & ~DSISR_NOHPTE) | DSISR_KEYFAULT;
664 }
665
666 /* Save HPTE info for virtual-mode handler */
667 vcpu->arch.pgfault_addr = addr;
668 vcpu->arch.pgfault_index = index;
669 vcpu->arch.pgfault_hpte[0] = v;
670 vcpu->arch.pgfault_hpte[1] = r;
671
672 if (vcpu->arch.shregs.msr & MSR_IR)
673 return -2; /* MMIO emulation - load instr word */
674
675 return -1; /* send fault up to host kernel mode */
676
677 protfault:
678 return (status & ~DSISR_NOHPTE) | DSISR_PROTFAULT;
679}
diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
index 5c8b26183f5..d07b64d5f37 100644
--- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
@@ -601,6 +601,28 @@ kvmppc_interrupt:
601 601
602 stw r12,VCPU_TRAP(r9) 602 stw r12,VCPU_TRAP(r9)
603 603
604 /* Save HEIR (HV emulation assist reg) in last_inst
605 if this is an HEI (HV emulation interrupt, e40) */
606 li r3,KVM_INST_FETCH_FAILED
607BEGIN_FTR_SECTION
608 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
609 bne 11f
610 mfspr r3,SPRN_HEIR
611END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
61211: stw r3,VCPU_LAST_INST(r9)
613
614 /* these are volatile across C function calls */
615 mfctr r3
616 mfxer r4
617 std r3, VCPU_CTR(r9)
618 stw r4, VCPU_XER(r9)
619
620BEGIN_FTR_SECTION
621 /* If this is a page table miss then see if it's theirs or ours */
622 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
623 beq kvmppc_hdsi
624END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
625
604 /* See if this is a leftover HDEC interrupt */ 626 /* See if this is a leftover HDEC interrupt */
605 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER 627 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
606 bne 2f 628 bne 2f
@@ -608,7 +630,7 @@ kvmppc_interrupt:
608 cmpwi r3,0 630 cmpwi r3,0
609 bge ignore_hdec 631 bge ignore_hdec
6102: 6322:
611 /* See if this is something we can handle in real mode */ 633 /* See if this is an hcall we can handle in real mode */
612 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL 634 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
613 beq hcall_try_real_mode 635 beq hcall_try_real_mode
614 636
@@ -624,6 +646,7 @@ BEGIN_FTR_SECTION
6241: 6461:
625END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206) 647END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
626 648
649nohpte_cont:
627hcall_real_cont: /* r9 = vcpu, r12 = trap, r13 = paca */ 650hcall_real_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
628 /* Save DEC */ 651 /* Save DEC */
629 mfspr r5,SPRN_DEC 652 mfspr r5,SPRN_DEC
@@ -632,36 +655,21 @@ hcall_real_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
632 add r5,r5,r6 655 add r5,r5,r6
633 std r5,VCPU_DEC_EXPIRES(r9) 656 std r5,VCPU_DEC_EXPIRES(r9)
634 657
635 /* Save HEIR (HV emulation assist reg) in last_inst
636 if this is an HEI (HV emulation interrupt, e40) */
637 li r3,-1
638BEGIN_FTR_SECTION
639 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
640 bne 11f
641 mfspr r3,SPRN_HEIR
642END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
64311: stw r3,VCPU_LAST_INST(r9)
644
645 /* Save more register state */ 658 /* Save more register state */
646 mfxer r5
647 mfdar r6 659 mfdar r6
648 mfdsisr r7 660 mfdsisr r7
649 mfctr r8
650
651 stw r5, VCPU_XER(r9)
652 std r6, VCPU_DAR(r9) 661 std r6, VCPU_DAR(r9)
653 stw r7, VCPU_DSISR(r9) 662 stw r7, VCPU_DSISR(r9)
654 std r8, VCPU_CTR(r9)
655 /* grab HDAR & HDSISR if HV data storage interrupt (HDSI) */
656BEGIN_FTR_SECTION 663BEGIN_FTR_SECTION
664 /* don't overwrite fault_dar/fault_dsisr if HDSI */
657 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE 665 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
658 beq 6f 666 beq 6f
659END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206) 667END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
6607: std r6, VCPU_FAULT_DAR(r9) 668 std r6, VCPU_FAULT_DAR(r9)
661 stw r7, VCPU_FAULT_DSISR(r9) 669 stw r7, VCPU_FAULT_DSISR(r9)
662 670
663 /* Save guest CTRL register, set runlatch to 1 */ 671 /* Save guest CTRL register, set runlatch to 1 */
664 mfspr r6,SPRN_CTRLF 6726: mfspr r6,SPRN_CTRLF
665 stw r6,VCPU_CTRL(r9) 673 stw r6,VCPU_CTRL(r9)
666 andi. r0,r6,1 674 andi. r0,r6,1
667 bne 4f 675 bne 4f
@@ -1094,9 +1102,84 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1094 mtspr SPRN_HSRR1, r7 1102 mtspr SPRN_HSRR1, r7
1095 ba 0x500 1103 ba 0x500
1096 1104
10976: mfspr r6,SPRN_HDAR 1105/*
1098 mfspr r7,SPRN_HDSISR 1106 * Check whether an HDSI is an HPTE not found fault or something else.
1099 b 7b 1107 * If it is an HPTE not found fault that is due to the guest accessing
1108 * a page that they have mapped but which we have paged out, then
1109 * we continue on with the guest exit path. In all other cases,
1110 * reflect the HDSI to the guest as a DSI.
1111 */
1112kvmppc_hdsi:
1113 mfspr r4, SPRN_HDAR
1114 mfspr r6, SPRN_HDSISR
1115 /* HPTE not found fault? */
1116 andis. r0, r6, DSISR_NOHPTE@h
1117 beq 1f /* if not, send it to the guest */
1118 andi. r0, r11, MSR_DR /* data relocation enabled? */
1119 beq 3f
1120 clrrdi r0, r4, 28
1121 PPC_SLBFEE_DOT(r5, r0) /* if so, look up SLB */
1122 bne 1f /* if no SLB entry found */
11234: std r4, VCPU_FAULT_DAR(r9)
1124 stw r6, VCPU_FAULT_DSISR(r9)
1125
1126 /* Search the hash table. */
1127 mr r3, r9 /* vcpu pointer */
1128 bl .kvmppc_hpte_hv_fault
1129 ld r9, HSTATE_KVM_VCPU(r13)
1130 ld r10, VCPU_PC(r9)
1131 ld r11, VCPU_MSR(r9)
1132 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1133 cmpdi r3, 0 /* retry the instruction */
1134 beq 6f
1135 cmpdi r3, -1 /* handle in kernel mode */
1136 beq nohpte_cont
1137 cmpdi r3, -2 /* MMIO emulation; need instr word */
1138 beq 2f
1139
1140 /* Synthesize a DSI for the guest */
1141 ld r4, VCPU_FAULT_DAR(r9)
1142 mr r6, r3
11431: mtspr SPRN_DAR, r4
1144 mtspr SPRN_DSISR, r6
1145 mtspr SPRN_SRR0, r10
1146 mtspr SPRN_SRR1, r11
1147 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
1148 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1149 rotldi r11, r11, 63
11506: ld r7, VCPU_CTR(r9)
1151 lwz r8, VCPU_XER(r9)
1152 mtctr r7
1153 mtxer r8
1154 mr r4, r9
1155 b fast_guest_return
1156
11573: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1158 ld r5, KVM_VRMA_SLB_V(r5)
1159 b 4b
1160
1161 /* If this is for emulated MMIO, load the instruction word */
11622: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1163
1164 /* Set guest mode to 'jump over instruction' so if lwz faults
1165 * we'll just continue at the next IP. */
1166 li r0, KVM_GUEST_MODE_SKIP
1167 stb r0, HSTATE_IN_GUEST(r13)
1168
1169 /* Do the access with MSR:DR enabled */
1170 mfmsr r3
1171 ori r4, r3, MSR_DR /* Enable paging for data */
1172 mtmsrd r4
1173 lwz r8, 0(r10)
1174 mtmsrd r3
1175
1176 /* Store the result */
1177 stw r8, VCPU_LAST_INST(r9)
1178
1179 /* Unset guest mode. */
1180 li r0, KVM_GUEST_MODE_NONE
1181 stb r0, HSTATE_IN_GUEST(r13)
1182 b nohpte_cont
1100 1183
1101/* 1184/*
1102 * Try to handle an hcall in real mode. 1185 * Try to handle an hcall in real mode.