2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
6 * Copyright 2010-2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/kvm.h>
12 #include <linux/kvm_host.h>
13 #include <linux/hugetlb.h>
14 #include <linux/module.h>
16 #include <asm/tlbflush.h>
17 #include <asm/kvm_ppc.h>
18 #include <asm/kvm_book3s.h>
19 #include <asm/mmu-hash64.h>
20 #include <asm/hvcall.h>
21 #include <asm/synch.h>
22 #include <asm/ppc-opcode.h>
24 /* Translate address of a vmalloc'd thing to a linear map address */
25 static void *real_vmalloc_addr(void *x)
27 unsigned long addr = (unsigned long) x;
30 * assume we don't have huge pages in vmalloc space...
31 * So don't worry about THP collapse/split. Called
32 * Only in realmode, hence won't need irq_save/restore.
34 p = __find_linux_pte_or_hugepte(swapper_pg_dir, addr, NULL);
35 if (!p || !pte_present(*p))
37 addr = (pte_pfn(*p) << PAGE_SHIFT) | (addr & ~PAGE_MASK);
41 /* Return 1 if we need to do a global tlbie, 0 if we can use tlbiel */
42 static int global_invalidates(struct kvm *kvm, unsigned long flags)
47 * If there is only one vcore, and it's currently running,
48 * as indicated by local_paca->kvm_hstate.kvm_vcpu being set,
49 * we can use tlbiel as long as we mark all other physical
50 * cores as potentially having stale TLB entries for this lpid.
51 * Otherwise, don't use tlbiel.
53 if (kvm->arch.online_vcores == 1 && local_paca->kvm_hstate.kvm_vcpu)
59 /* any other core might now have stale TLB entries... */
61 cpumask_setall(&kvm->arch.need_tlb_flush);
62 cpumask_clear_cpu(local_paca->kvm_hstate.kvm_vcore->pcpu,
63 &kvm->arch.need_tlb_flush);
70 * Add this HPTE into the chain for the real page.
71 * Must be called with the chain locked; it unlocks the chain.
73 void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
74 unsigned long *rmap, long pte_index, int realmode)
76 struct revmap_entry *head, *tail;
79 if (*rmap & KVMPPC_RMAP_PRESENT) {
80 i = *rmap & KVMPPC_RMAP_INDEX;
81 head = &kvm->arch.revmap[i];
83 head = real_vmalloc_addr(head);
84 tail = &kvm->arch.revmap[head->back];
86 tail = real_vmalloc_addr(tail);
88 rev->back = head->back;
89 tail->forw = pte_index;
90 head->back = pte_index;
92 rev->forw = rev->back = pte_index;
93 *rmap = (*rmap & ~KVMPPC_RMAP_INDEX) |
94 pte_index | KVMPPC_RMAP_PRESENT;
98 EXPORT_SYMBOL_GPL(kvmppc_add_revmap_chain);
100 /* Remove this HPTE from the chain for a real page */
101 static void remove_revmap_chain(struct kvm *kvm, long pte_index,
102 struct revmap_entry *rev,
103 unsigned long hpte_v, unsigned long hpte_r)
105 struct revmap_entry *next, *prev;
106 unsigned long gfn, ptel, head;
107 struct kvm_memory_slot *memslot;
109 unsigned long rcbits;
111 rcbits = hpte_r & (HPTE_R_R | HPTE_R_C);
112 ptel = rev->guest_rpte |= rcbits;
113 gfn = hpte_rpn(ptel, hpte_page_size(hpte_v, ptel));
114 memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
118 rmap = real_vmalloc_addr(&memslot->arch.rmap[gfn - memslot->base_gfn]);
121 head = *rmap & KVMPPC_RMAP_INDEX;
122 next = real_vmalloc_addr(&kvm->arch.revmap[rev->forw]);
123 prev = real_vmalloc_addr(&kvm->arch.revmap[rev->back]);
124 next->back = rev->back;
125 prev->forw = rev->forw;
126 if (head == pte_index) {
128 if (head == pte_index)
129 *rmap &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
131 *rmap = (*rmap & ~KVMPPC_RMAP_INDEX) | head;
133 *rmap |= rcbits << KVMPPC_RMAP_RC_SHIFT;
137 static inline void unlock_hpte(__be64 *hpte, unsigned long hpte_v)
139 asm volatile(PPC_RELEASE_BARRIER "" : : : "memory");
140 hpte[0] = cpu_to_be64(hpte_v);
143 long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags,
144 long pte_index, unsigned long pteh, unsigned long ptel,
145 pgd_t *pgdir, bool realmode, unsigned long *pte_idx_ret)
147 unsigned long i, pa, gpa, gfn, psize;
148 unsigned long slot_fn, hva;
150 struct revmap_entry *rev;
151 unsigned long g_ptel;
152 struct kvm_memory_slot *memslot;
153 unsigned hpage_shift;
157 unsigned int writing;
158 unsigned long mmu_seq;
159 unsigned long rcbits, irq_flags = 0;
161 psize = hpte_page_size(pteh, ptel);
164 writing = hpte_is_writable(ptel);
165 pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
166 ptel &= ~HPTE_GR_RESERVED;
169 /* used later to detect if we might have been invalidated */
170 mmu_seq = kvm->mmu_notifier_seq;
173 /* Find the memslot (if any) for this address */
174 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
175 gfn = gpa >> PAGE_SHIFT;
176 memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
180 if (!(memslot && !(memslot->flags & KVM_MEMSLOT_INVALID))) {
181 /* Emulated MMIO - mark this with key=31 */
182 pteh |= HPTE_V_ABSENT;
183 ptel |= HPTE_R_KEY_HI | HPTE_R_KEY_LO;
187 /* Check if the requested page fits entirely in the memslot. */
188 if (!slot_is_aligned(memslot, psize))
190 slot_fn = gfn - memslot->base_gfn;
191 rmap = &memslot->arch.rmap[slot_fn];
193 /* Translate to host virtual address */
194 hva = __gfn_to_hva_memslot(memslot, gfn);
196 * If we had a page table table change after lookup, we would
197 * retry via mmu_notifier_retry.
200 ptep = __find_linux_pte_or_hugepte(pgdir, hva, &hpage_shift);
202 local_irq_save(irq_flags);
203 ptep = find_linux_pte_or_hugepte(pgdir, hva, &hpage_shift);
207 unsigned int host_pte_size;
210 host_pte_size = 1ul << hpage_shift;
212 host_pte_size = PAGE_SIZE;
214 * We should always find the guest page size
215 * to <= host page size, if host is using hugepage
217 if (host_pte_size < psize) {
219 local_irq_restore(flags);
222 pte = kvmppc_read_update_linux_pte(ptep, writing, hpage_shift);
223 if (pte_present(pte) && !pte_protnone(pte)) {
224 if (writing && !pte_write(pte))
225 /* make the actual HPTE be read-only */
226 ptel = hpte_make_readonly(ptel);
227 is_io = hpte_cache_bits(pte_val(pte));
228 pa = pte_pfn(pte) << PAGE_SHIFT;
229 pa |= hva & (host_pte_size - 1);
230 pa |= gpa & ~PAGE_MASK;
234 local_irq_restore(irq_flags);
236 ptel &= ~(HPTE_R_PP0 - psize);
240 pteh |= HPTE_V_VALID;
242 pteh |= HPTE_V_ABSENT;
245 if (is_io != ~0ul && !hpte_cache_flags_ok(ptel, is_io)) {
249 * Allow guest to map emulated device memory as
250 * uncacheable, but actually make it cacheable.
252 ptel &= ~(HPTE_R_W|HPTE_R_I|HPTE_R_G);
256 /* Find and lock the HPTEG slot to use */
258 if (pte_index >= kvm->arch.hpt_npte)
260 if (likely((flags & H_EXACT) == 0)) {
262 hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
263 for (i = 0; i < 8; ++i) {
264 if ((be64_to_cpu(*hpte) & HPTE_V_VALID) == 0 &&
265 try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
272 * Since try_lock_hpte doesn't retry (not even stdcx.
273 * failures), it could be that there is a free slot
274 * but we transiently failed to lock it. Try again,
275 * actually locking each slot and checking it.
278 for (i = 0; i < 8; ++i) {
280 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
282 pte = be64_to_cpu(*hpte);
283 if (!(pte & (HPTE_V_VALID | HPTE_V_ABSENT)))
285 *hpte &= ~cpu_to_be64(HPTE_V_HVLOCK);
293 hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
294 if (!try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
296 /* Lock the slot and check again */
299 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
301 pte = be64_to_cpu(*hpte);
302 if (pte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
303 *hpte &= ~cpu_to_be64(HPTE_V_HVLOCK);
309 /* Save away the guest's idea of the second HPTE dword */
310 rev = &kvm->arch.revmap[pte_index];
312 rev = real_vmalloc_addr(rev);
314 rev->guest_rpte = g_ptel;
315 note_hpte_modification(kvm, rev);
318 /* Link HPTE into reverse-map chain */
319 if (pteh & HPTE_V_VALID) {
321 rmap = real_vmalloc_addr(rmap);
323 /* Check for pending invalidations under the rmap chain lock */
324 if (mmu_notifier_retry(kvm, mmu_seq)) {
325 /* inval in progress, write a non-present HPTE */
326 pteh |= HPTE_V_ABSENT;
327 pteh &= ~HPTE_V_VALID;
330 kvmppc_add_revmap_chain(kvm, rev, rmap, pte_index,
332 /* Only set R/C in real HPTE if already set in *rmap */
333 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
334 ptel &= rcbits | ~(HPTE_R_R | HPTE_R_C);
338 hpte[1] = cpu_to_be64(ptel);
340 /* Write the first HPTE dword, unlocking the HPTE and making it valid */
342 hpte[0] = cpu_to_be64(pteh);
343 asm volatile("ptesync" : : : "memory");
345 *pte_idx_ret = pte_index;
348 EXPORT_SYMBOL_GPL(kvmppc_do_h_enter);
350 long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
351 long pte_index, unsigned long pteh, unsigned long ptel)
353 return kvmppc_do_h_enter(vcpu->kvm, flags, pte_index, pteh, ptel,
354 vcpu->arch.pgdir, true, &vcpu->arch.gpr[4]);
357 #ifdef __BIG_ENDIAN__
358 #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
360 #define LOCK_TOKEN (*(u32 *)(&get_paca()->paca_index))
363 static inline int try_lock_tlbie(unsigned int *lock)
365 unsigned int tmp, old;
366 unsigned int token = LOCK_TOKEN;
368 asm volatile("1:lwarx %1,0,%2\n"
375 : "=&r" (tmp), "=&r" (old)
376 : "r" (lock), "r" (token)
381 static void do_tlbies(struct kvm *kvm, unsigned long *rbvalues,
382 long npages, int global, bool need_sync)
387 while (!try_lock_tlbie(&kvm->arch.tlbie_lock))
390 asm volatile("ptesync" : : : "memory");
391 for (i = 0; i < npages; ++i)
392 asm volatile(PPC_TLBIE(%1,%0) : :
393 "r" (rbvalues[i]), "r" (kvm->arch.lpid));
394 asm volatile("eieio; tlbsync; ptesync" : : : "memory");
395 kvm->arch.tlbie_lock = 0;
398 asm volatile("ptesync" : : : "memory");
399 for (i = 0; i < npages; ++i)
400 asm volatile("tlbiel %0" : : "r" (rbvalues[i]));
401 asm volatile("ptesync" : : : "memory");
405 long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
406 unsigned long pte_index, unsigned long avpn,
407 unsigned long *hpret)
410 unsigned long v, r, rb;
411 struct revmap_entry *rev;
414 if (pte_index >= kvm->arch.hpt_npte)
416 hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
417 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
419 pte = be64_to_cpu(hpte[0]);
420 if ((pte & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
421 ((flags & H_AVPN) && (pte & ~0x7fUL) != avpn) ||
422 ((flags & H_ANDCOND) && (pte & avpn) != 0)) {
423 hpte[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
427 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
428 v = pte & ~HPTE_V_HVLOCK;
429 if (v & HPTE_V_VALID) {
432 pte1 = be64_to_cpu(hpte[1]);
433 hpte[0] &= ~cpu_to_be64(HPTE_V_VALID);
434 rb = compute_tlbie_rb(v, pte1, pte_index);
435 do_tlbies(kvm, &rb, 1, global_invalidates(kvm, flags), true);
436 /* Read PTE low word after tlbie to get final R/C values */
437 remove_revmap_chain(kvm, pte_index, rev, v, pte1);
439 r = rev->guest_rpte & ~HPTE_GR_RESERVED;
440 note_hpte_modification(kvm, rev);
441 unlock_hpte(hpte, 0);
447 EXPORT_SYMBOL_GPL(kvmppc_do_h_remove);
449 long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
450 unsigned long pte_index, unsigned long avpn)
452 return kvmppc_do_h_remove(vcpu->kvm, flags, pte_index, avpn,
456 long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu)
458 struct kvm *kvm = vcpu->kvm;
459 unsigned long *args = &vcpu->arch.gpr[4];
460 __be64 *hp, *hptes[4];
461 unsigned long tlbrb[4];
462 long int i, j, k, n, found, indexes[4];
463 unsigned long flags, req, pte_index, rcbits;
465 long int ret = H_SUCCESS;
466 struct revmap_entry *rev, *revs[4];
469 global = global_invalidates(kvm, 0);
470 for (i = 0; i < 4 && ret == H_SUCCESS; ) {
475 flags = pte_index >> 56;
476 pte_index &= ((1ul << 56) - 1);
479 if (req == 3) { /* no more requests */
483 if (req != 1 || flags == 3 ||
484 pte_index >= kvm->arch.hpt_npte) {
485 /* parameter error */
486 args[j] = ((0xa0 | flags) << 56) + pte_index;
490 hp = (__be64 *) (kvm->arch.hpt_virt + (pte_index << 4));
491 /* to avoid deadlock, don't spin except for first */
492 if (!try_lock_hpte(hp, HPTE_V_HVLOCK)) {
495 while (!try_lock_hpte(hp, HPTE_V_HVLOCK))
499 hp0 = be64_to_cpu(hp[0]);
500 if (hp0 & (HPTE_V_ABSENT | HPTE_V_VALID)) {
502 case 0: /* absolute */
505 case 1: /* andcond */
506 if (!(hp0 & args[j + 1]))
510 if ((hp0 & ~0x7fUL) == args[j + 1])
516 hp[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
517 args[j] = ((0x90 | flags) << 56) + pte_index;
521 args[j] = ((0x80 | flags) << 56) + pte_index;
522 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
523 note_hpte_modification(kvm, rev);
525 if (!(hp0 & HPTE_V_VALID)) {
526 /* insert R and C bits from PTE */
527 rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
528 args[j] |= rcbits << (56 - 5);
533 /* leave it locked */
534 hp[0] &= ~cpu_to_be64(HPTE_V_VALID);
535 tlbrb[n] = compute_tlbie_rb(be64_to_cpu(hp[0]),
536 be64_to_cpu(hp[1]), pte_index);
546 /* Now that we've collected a batch, do the tlbies */
547 do_tlbies(kvm, tlbrb, n, global, true);
549 /* Read PTE low words after tlbie to get final R/C values */
550 for (k = 0; k < n; ++k) {
552 pte_index = args[j] & ((1ul << 56) - 1);
555 remove_revmap_chain(kvm, pte_index, rev,
556 be64_to_cpu(hp[0]), be64_to_cpu(hp[1]));
557 rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
558 args[j] |= rcbits << (56 - 5);
566 long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
567 unsigned long pte_index, unsigned long avpn,
570 struct kvm *kvm = vcpu->kvm;
572 struct revmap_entry *rev;
573 unsigned long v, r, rb, mask, bits;
576 if (pte_index >= kvm->arch.hpt_npte)
579 hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
580 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
582 pte = be64_to_cpu(hpte[0]);
583 if ((pte & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
584 ((flags & H_AVPN) && (pte & ~0x7fUL) != avpn)) {
585 hpte[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
590 bits = (flags << 55) & HPTE_R_PP0;
591 bits |= (flags << 48) & HPTE_R_KEY_HI;
592 bits |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
594 /* Update guest view of 2nd HPTE dword */
595 mask = HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
596 HPTE_R_KEY_HI | HPTE_R_KEY_LO;
597 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
599 r = (rev->guest_rpte & ~mask) | bits;
601 note_hpte_modification(kvm, rev);
605 if (v & HPTE_V_VALID) {
607 * If the page is valid, don't let it transition from
608 * readonly to writable. If it should be writable, we'll
609 * take a trap and let the page fault code sort it out.
611 pte = be64_to_cpu(hpte[1]);
612 r = (pte & ~mask) | bits;
613 if (hpte_is_writable(r) && !hpte_is_writable(pte))
614 r = hpte_make_readonly(r);
615 /* If the PTE is changing, invalidate it first */
617 rb = compute_tlbie_rb(v, r, pte_index);
618 hpte[0] = cpu_to_be64((v & ~HPTE_V_VALID) |
620 do_tlbies(kvm, &rb, 1, global_invalidates(kvm, flags),
622 hpte[1] = cpu_to_be64(r);
625 unlock_hpte(hpte, v & ~HPTE_V_HVLOCK);
626 asm volatile("ptesync" : : : "memory");
630 long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
631 unsigned long pte_index)
633 struct kvm *kvm = vcpu->kvm;
637 struct revmap_entry *rev = NULL;
639 if (pte_index >= kvm->arch.hpt_npte)
641 if (flags & H_READ_4) {
645 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
646 for (i = 0; i < n; ++i, ++pte_index) {
647 hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
648 v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
649 r = be64_to_cpu(hpte[1]);
650 if (v & HPTE_V_ABSENT) {
654 if (v & HPTE_V_VALID) {
655 r = rev[i].guest_rpte | (r & (HPTE_R_R | HPTE_R_C));
656 r &= ~HPTE_GR_RESERVED;
658 vcpu->arch.gpr[4 + i * 2] = v;
659 vcpu->arch.gpr[5 + i * 2] = r;
664 void kvmppc_invalidate_hpte(struct kvm *kvm, __be64 *hptep,
665 unsigned long pte_index)
669 hptep[0] &= ~cpu_to_be64(HPTE_V_VALID);
670 rb = compute_tlbie_rb(be64_to_cpu(hptep[0]), be64_to_cpu(hptep[1]),
672 do_tlbies(kvm, &rb, 1, 1, true);
674 EXPORT_SYMBOL_GPL(kvmppc_invalidate_hpte);
676 void kvmppc_clear_ref_hpte(struct kvm *kvm, __be64 *hptep,
677 unsigned long pte_index)
682 rb = compute_tlbie_rb(be64_to_cpu(hptep[0]), be64_to_cpu(hptep[1]),
684 rbyte = (be64_to_cpu(hptep[1]) & ~HPTE_R_R) >> 8;
685 /* modify only the second-last byte, which contains the ref bit */
686 *((char *)hptep + 14) = rbyte;
687 do_tlbies(kvm, &rb, 1, 1, false);
689 EXPORT_SYMBOL_GPL(kvmppc_clear_ref_hpte);
691 static int slb_base_page_shift[4] = {
695 20, /* 1M, unsupported */
698 /* When called from virtmode, this func should be protected by
699 * preempt_disable(), otherwise, the holding of HPTE_V_HVLOCK
700 * can trigger deadlock issue.
702 long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, unsigned long slb_v,
707 unsigned long somask;
708 unsigned long vsid, hash;
711 unsigned long mask, val;
714 /* Get page shift, work out hash and AVPN etc. */
715 mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_SECONDARY;
718 if (slb_v & SLB_VSID_L) {
719 mask |= HPTE_V_LARGE;
721 pshift = slb_base_page_shift[(slb_v & SLB_VSID_LP) >> 4];
723 if (slb_v & SLB_VSID_B_1T) {
724 somask = (1UL << 40) - 1;
725 vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T;
728 somask = (1UL << 28) - 1;
729 vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT;
731 hash = (vsid ^ ((eaddr & somask) >> pshift)) & kvm->arch.hpt_mask;
732 avpn = slb_v & ~(somask >> 16); /* also includes B */
733 avpn |= (eaddr & somask) >> 16;
736 avpn &= ~((1UL << (pshift - 16)) - 1);
742 hpte = (__be64 *)(kvm->arch.hpt_virt + (hash << 7));
744 for (i = 0; i < 16; i += 2) {
745 /* Read the PTE racily */
746 v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
748 /* Check valid/absent, hash, segment size and AVPN */
749 if (!(v & valid) || (v & mask) != val)
752 /* Lock the PTE and read it under the lock */
753 while (!try_lock_hpte(&hpte[i], HPTE_V_HVLOCK))
755 v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
756 r = be64_to_cpu(hpte[i+1]);
759 * Check the HPTE again, including base page size
761 if ((v & valid) && (v & mask) == val &&
762 hpte_base_page_size(v, r) == (1ul << pshift))
763 /* Return with the HPTE still locked */
764 return (hash << 3) + (i >> 1);
766 /* Unlock and move on */
767 hpte[i] = cpu_to_be64(v);
770 if (val & HPTE_V_SECONDARY)
772 val |= HPTE_V_SECONDARY;
773 hash = hash ^ kvm->arch.hpt_mask;
777 EXPORT_SYMBOL(kvmppc_hv_find_lock_hpte);
780 * Called in real mode to check whether an HPTE not found fault
781 * is due to accessing a paged-out page or an emulated MMIO page,
782 * or if a protection fault is due to accessing a page that the
783 * guest wanted read/write access to but which we made read-only.
784 * Returns a possibly modified status (DSISR) value if not
785 * (i.e. pass the interrupt to the guest),
786 * -1 to pass the fault up to host kernel mode code, -2 to do that
787 * and also load the instruction word (for MMIO emulation),
788 * or 0 if we should make the guest retry the access.
790 long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
791 unsigned long slb_v, unsigned int status, bool data)
793 struct kvm *kvm = vcpu->kvm;
795 unsigned long v, r, gr;
798 struct revmap_entry *rev;
799 unsigned long pp, key;
801 /* For protection fault, expect to find a valid HPTE */
802 valid = HPTE_V_VALID;
803 if (status & DSISR_NOHPTE)
804 valid |= HPTE_V_ABSENT;
806 index = kvmppc_hv_find_lock_hpte(kvm, addr, slb_v, valid);
808 if (status & DSISR_NOHPTE)
809 return status; /* there really was no HPTE */
810 return 0; /* for prot fault, HPTE disappeared */
812 hpte = (__be64 *)(kvm->arch.hpt_virt + (index << 4));
813 v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
814 r = be64_to_cpu(hpte[1]);
815 rev = real_vmalloc_addr(&kvm->arch.revmap[index]);
816 gr = rev->guest_rpte;
818 unlock_hpte(hpte, v);
820 /* For not found, if the HPTE is valid by now, retry the instruction */
821 if ((status & DSISR_NOHPTE) && (v & HPTE_V_VALID))
824 /* Check access permissions to the page */
825 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
826 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
827 status &= ~DSISR_NOHPTE; /* DSISR_NOHPTE == SRR1_ISI_NOPT */
829 if (gr & (HPTE_R_N | HPTE_R_G))
830 return status | SRR1_ISI_N_OR_G;
831 if (!hpte_read_permission(pp, slb_v & key))
832 return status | SRR1_ISI_PROT;
833 } else if (status & DSISR_ISSTORE) {
834 /* check write permission */
835 if (!hpte_write_permission(pp, slb_v & key))
836 return status | DSISR_PROTFAULT;
838 if (!hpte_read_permission(pp, slb_v & key))
839 return status | DSISR_PROTFAULT;
842 /* Check storage key, if applicable */
843 if (data && (vcpu->arch.shregs.msr & MSR_DR)) {
844 unsigned int perm = hpte_get_skey_perm(gr, vcpu->arch.amr);
845 if (status & DSISR_ISSTORE)
848 return status | DSISR_KEYFAULT;
851 /* Save HPTE info for virtual-mode handler */
852 vcpu->arch.pgfault_addr = addr;
853 vcpu->arch.pgfault_index = index;
854 vcpu->arch.pgfault_hpte[0] = v;
855 vcpu->arch.pgfault_hpte[1] = r;
857 /* Check the storage key to see if it is possibly emulated MMIO */
858 if (data && (vcpu->arch.shregs.msr & MSR_IR) &&
859 (r & (HPTE_R_KEY_HI | HPTE_R_KEY_LO)) ==
860 (HPTE_R_KEY_HI | HPTE_R_KEY_LO))
861 return -2; /* MMIO emulation - load instr word */
863 return -1; /* send fault up to host kernel mode */