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 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_host.h>
22 #include <linux/highmem.h>
23 #include <linux/gfp.h>
24 #include <linux/slab.h>
25 #include <linux/hugetlb.h>
26 #include <linux/vmalloc.h>
27 #include <linux/srcu.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/file.h>
31 #include <asm/tlbflush.h>
32 #include <asm/kvm_ppc.h>
33 #include <asm/kvm_book3s.h>
34 #include <asm/mmu-hash64.h>
35 #include <asm/hvcall.h>
36 #include <asm/synch.h>
37 #include <asm/ppc-opcode.h>
38 #include <asm/cputable.h>
42 /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
43 #define MAX_LPID_970 63
45 /* Power architecture requires HPT is at least 256kB */
46 #define PPC_MIN_HPT_ORDER 18
48 static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
49 long pte_index, unsigned long pteh,
50 unsigned long ptel, unsigned long *pte_idx_ret);
51 static void kvmppc_rmap_reset(struct kvm *kvm);
53 long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp)
55 unsigned long hpt = 0;
56 struct revmap_entry *rev;
57 struct page *page = NULL;
58 long order = KVM_DEFAULT_HPT_ORDER;
62 if (order < PPC_MIN_HPT_ORDER)
63 order = PPC_MIN_HPT_ORDER;
66 kvm->arch.hpt_cma_alloc = 0;
67 page = kvm_alloc_hpt(1ul << (order - PAGE_SHIFT));
69 hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
70 memset((void *)hpt, 0, (1ul << order));
71 kvm->arch.hpt_cma_alloc = 1;
74 /* Lastly try successively smaller sizes from the page allocator */
75 while (!hpt && order > PPC_MIN_HPT_ORDER) {
76 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
77 __GFP_NOWARN, order - PAGE_SHIFT);
85 kvm->arch.hpt_virt = hpt;
86 kvm->arch.hpt_order = order;
87 /* HPTEs are 2**4 bytes long */
88 kvm->arch.hpt_npte = 1ul << (order - 4);
89 /* 128 (2**7) bytes in each HPTEG */
90 kvm->arch.hpt_mask = (1ul << (order - 7)) - 1;
92 /* Allocate reverse map array */
93 rev = vmalloc(sizeof(struct revmap_entry) * kvm->arch.hpt_npte);
95 pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
98 kvm->arch.revmap = rev;
99 kvm->arch.sdr1 = __pa(hpt) | (order - 18);
101 pr_info("KVM guest htab at %lx (order %ld), LPID %x\n",
102 hpt, order, kvm->arch.lpid);
105 *htab_orderp = order;
109 if (kvm->arch.hpt_cma_alloc)
110 kvm_release_hpt(page, 1 << (order - PAGE_SHIFT));
112 free_pages(hpt, order - PAGE_SHIFT);
116 long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp)
121 mutex_lock(&kvm->lock);
122 if (kvm->arch.rma_setup_done) {
123 kvm->arch.rma_setup_done = 0;
124 /* order rma_setup_done vs. vcpus_running */
126 if (atomic_read(&kvm->arch.vcpus_running)) {
127 kvm->arch.rma_setup_done = 1;
131 if (kvm->arch.hpt_virt) {
132 order = kvm->arch.hpt_order;
133 /* Set the entire HPT to 0, i.e. invalid HPTEs */
134 memset((void *)kvm->arch.hpt_virt, 0, 1ul << order);
136 * Reset all the reverse-mapping chains for all memslots
138 kvmppc_rmap_reset(kvm);
139 /* Ensure that each vcpu will flush its TLB on next entry. */
140 cpumask_setall(&kvm->arch.need_tlb_flush);
141 *htab_orderp = order;
144 err = kvmppc_alloc_hpt(kvm, htab_orderp);
145 order = *htab_orderp;
148 mutex_unlock(&kvm->lock);
152 void kvmppc_free_hpt(struct kvm *kvm)
154 kvmppc_free_lpid(kvm->arch.lpid);
155 vfree(kvm->arch.revmap);
156 if (kvm->arch.hpt_cma_alloc)
157 kvm_release_hpt(virt_to_page(kvm->arch.hpt_virt),
158 1 << (kvm->arch.hpt_order - PAGE_SHIFT));
160 free_pages(kvm->arch.hpt_virt,
161 kvm->arch.hpt_order - PAGE_SHIFT);
164 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
165 static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
167 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
170 /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
171 static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
173 return (pgsize == 0x10000) ? 0x1000 : 0;
176 void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
177 unsigned long porder)
180 unsigned long npages;
181 unsigned long hp_v, hp_r;
182 unsigned long addr, hash;
184 unsigned long hp0, hp1;
185 unsigned long idx_ret;
187 struct kvm *kvm = vcpu->kvm;
189 psize = 1ul << porder;
190 npages = memslot->npages >> (porder - PAGE_SHIFT);
192 /* VRMA can't be > 1TB */
193 if (npages > 1ul << (40 - porder))
194 npages = 1ul << (40 - porder);
195 /* Can't use more than 1 HPTE per HPTEG */
196 if (npages > kvm->arch.hpt_mask + 1)
197 npages = kvm->arch.hpt_mask + 1;
199 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
200 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
201 hp1 = hpte1_pgsize_encoding(psize) |
202 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
204 for (i = 0; i < npages; ++i) {
206 /* can't use hpt_hash since va > 64 bits */
207 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & kvm->arch.hpt_mask;
209 * We assume that the hash table is empty and no
210 * vcpus are using it at this stage. Since we create
211 * at most one HPTE per HPTEG, we just assume entry 7
212 * is available and use it.
214 hash = (hash << 3) + 7;
215 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
217 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
219 if (ret != H_SUCCESS) {
220 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
227 int kvmppc_mmu_hv_init(void)
229 unsigned long host_lpid, rsvd_lpid;
231 if (!cpu_has_feature(CPU_FTR_HVMODE))
234 /* POWER7 has 10-bit LPIDs, PPC970 and e500mc have 6-bit LPIDs */
235 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
236 host_lpid = mfspr(SPRN_LPID); /* POWER7 */
237 rsvd_lpid = LPID_RSVD;
239 host_lpid = 0; /* PPC970 */
240 rsvd_lpid = MAX_LPID_970;
243 kvmppc_init_lpid(rsvd_lpid + 1);
245 kvmppc_claim_lpid(host_lpid);
246 /* rsvd_lpid is reserved for use in partition switching */
247 kvmppc_claim_lpid(rsvd_lpid);
252 static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
254 unsigned long msr = vcpu->arch.intr_msr;
256 /* If transactional, change to suspend mode on IRQ delivery */
257 if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
260 msr |= vcpu->arch.shregs.msr & MSR_TS_MASK;
261 kvmppc_set_msr(vcpu, msr);
265 * This is called to get a reference to a guest page if there isn't
266 * one already in the memslot->arch.slot_phys[] array.
268 static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn,
269 struct kvm_memory_slot *memslot,
274 struct page *page, *hpage, *pages[1];
275 unsigned long s, pgsize;
276 unsigned long *physp;
277 unsigned int is_io, got, pgorder;
278 struct vm_area_struct *vma;
279 unsigned long pfn, i, npages;
281 physp = memslot->arch.slot_phys;
284 if (physp[gfn - memslot->base_gfn])
292 start = gfn_to_hva_memslot(memslot, gfn);
294 /* Instantiate and get the page we want access to */
295 np = get_user_pages_fast(start, 1, 1, pages);
297 /* Look up the vma for the page */
298 down_read(¤t->mm->mmap_sem);
299 vma = find_vma(current->mm, start);
300 if (!vma || vma->vm_start > start ||
301 start + psize > vma->vm_end ||
302 !(vma->vm_flags & VM_PFNMAP))
304 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
305 pfn = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
306 /* check alignment of pfn vs. requested page size */
307 if (psize > PAGE_SIZE && (pfn & ((psize >> PAGE_SHIFT) - 1)))
309 up_read(¤t->mm->mmap_sem);
313 got = KVMPPC_GOT_PAGE;
315 /* See if this is a large page */
317 if (PageHuge(page)) {
318 hpage = compound_head(page);
319 s <<= compound_order(hpage);
320 /* Get the whole large page if slot alignment is ok */
321 if (s > psize && slot_is_aligned(memslot, s) &&
322 !(memslot->userspace_addr & (s - 1))) {
332 pfn = page_to_pfn(page);
335 npages = pgsize >> PAGE_SHIFT;
336 pgorder = __ilog2(npages);
337 physp += (gfn - memslot->base_gfn) & ~(npages - 1);
338 spin_lock(&kvm->arch.slot_phys_lock);
339 for (i = 0; i < npages; ++i) {
341 physp[i] = ((pfn + i) << PAGE_SHIFT) +
342 got + is_io + pgorder;
346 spin_unlock(&kvm->arch.slot_phys_lock);
355 up_read(¤t->mm->mmap_sem);
359 long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
360 long pte_index, unsigned long pteh,
361 unsigned long ptel, unsigned long *pte_idx_ret)
363 unsigned long psize, gpa, gfn;
364 struct kvm_memory_slot *memslot;
367 if (kvm->arch.using_mmu_notifiers)
370 psize = hpte_page_size(pteh, ptel);
374 pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
376 /* Find the memslot (if any) for this address */
377 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
378 gfn = gpa >> PAGE_SHIFT;
379 memslot = gfn_to_memslot(kvm, gfn);
380 if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) {
381 if (!slot_is_aligned(memslot, psize))
383 if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
388 /* Protect linux PTE lookup from page table destruction */
389 rcu_read_lock_sched(); /* this disables preemption too */
390 ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
391 current->mm->pgd, false, pte_idx_ret);
392 rcu_read_unlock_sched();
393 if (ret == H_TOO_HARD) {
394 /* this can't happen */
395 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
396 ret = H_RESOURCE; /* or something */
403 * We come here on a H_ENTER call from the guest when we are not
404 * using mmu notifiers and we don't have the requested page pinned
407 long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
408 long pte_index, unsigned long pteh,
411 return kvmppc_virtmode_do_h_enter(vcpu->kvm, flags, pte_index,
412 pteh, ptel, &vcpu->arch.gpr[4]);
415 static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
421 for (i = 0; i < vcpu->arch.slb_nr; i++) {
422 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
425 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
430 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
431 return &vcpu->arch.slb[i];
436 static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
439 unsigned long ra_mask;
441 ra_mask = hpte_page_size(v, r) - 1;
442 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
445 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
446 struct kvmppc_pte *gpte, bool data, bool iswrite)
448 struct kvm *kvm = vcpu->kvm;
449 struct kvmppc_slb *slbe;
451 unsigned long pp, key;
455 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
459 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
464 /* real mode access */
465 slb_v = vcpu->kvm->arch.vrma_slb_v;
469 /* Find the HPTE in the hash table */
470 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
471 HPTE_V_VALID | HPTE_V_ABSENT);
476 hptep = (__be64 *)(kvm->arch.hpt_virt + (index << 4));
477 v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
478 gr = kvm->arch.revmap[index].guest_rpte;
480 /* Unlock the HPTE */
481 asm volatile("lwsync" : : : "memory");
482 hptep[0] = cpu_to_be64(v);
486 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
488 /* Get PP bits and key for permission check */
489 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
490 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
493 /* Calculate permissions */
494 gpte->may_read = hpte_read_permission(pp, key);
495 gpte->may_write = hpte_write_permission(pp, key);
496 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
498 /* Storage key permission check for POWER7 */
499 if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) {
500 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
507 /* Get the guest physical address */
508 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
513 * Quick test for whether an instruction is a load or a store.
514 * If the instruction is a load or a store, then this will indicate
515 * which it is, at least on server processors. (Embedded processors
516 * have some external PID instructions that don't follow the rule
517 * embodied here.) If the instruction isn't a load or store, then
518 * this doesn't return anything useful.
520 static int instruction_is_store(unsigned int instr)
525 if ((instr & 0xfc000000) == 0x7c000000)
526 mask = 0x100; /* major opcode 31 */
527 return (instr & mask) != 0;
530 static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
531 unsigned long gpa, gva_t ea, int is_store)
536 * If we fail, we just return to the guest and try executing it again.
538 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
543 * WARNING: We do not know for sure whether the instruction we just
544 * read from memory is the same that caused the fault in the first
545 * place. If the instruction we read is neither an load or a store,
546 * then it can't access memory, so we don't need to worry about
547 * enforcing access permissions. So, assuming it is a load or
548 * store, we just check that its direction (load or store) is
549 * consistent with the original fault, since that's what we
550 * checked the access permissions against. If there is a mismatch
551 * we just return and retry the instruction.
554 if (instruction_is_store(last_inst) != !!is_store)
558 * Emulated accesses are emulated by looking at the hash for
559 * translation once, then performing the access later. The
560 * translation could be invalidated in the meantime in which
561 * point performing the subsequent memory access on the old
562 * physical address could possibly be a security hole for the
563 * guest (but not the host).
565 * This is less of an issue for MMIO stores since they aren't
566 * globally visible. It could be an issue for MMIO loads to
567 * a certain extent but we'll ignore it for now.
570 vcpu->arch.paddr_accessed = gpa;
571 vcpu->arch.vaddr_accessed = ea;
572 return kvmppc_emulate_mmio(run, vcpu);
575 int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
576 unsigned long ea, unsigned long dsisr)
578 struct kvm *kvm = vcpu->kvm;
579 unsigned long hpte[3], r;
581 unsigned long mmu_seq, psize, pte_size;
582 unsigned long gpa_base, gfn_base;
583 unsigned long gpa, gfn, hva, pfn;
584 struct kvm_memory_slot *memslot;
586 struct revmap_entry *rev;
587 struct page *page, *pages[1];
588 long index, ret, npages;
590 unsigned int writing, write_ok;
591 struct vm_area_struct *vma;
592 unsigned long rcbits;
595 * Real-mode code has already searched the HPT and found the
596 * entry we're interested in. Lock the entry and check that
597 * it hasn't changed. If it has, just return and re-execute the
600 if (ea != vcpu->arch.pgfault_addr)
602 index = vcpu->arch.pgfault_index;
603 hptep = (__be64 *)(kvm->arch.hpt_virt + (index << 4));
604 rev = &kvm->arch.revmap[index];
606 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
608 hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
609 hpte[1] = be64_to_cpu(hptep[1]);
610 hpte[2] = r = rev->guest_rpte;
611 asm volatile("lwsync" : : : "memory");
612 hptep[0] = cpu_to_be64(hpte[0]);
615 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
616 hpte[1] != vcpu->arch.pgfault_hpte[1])
619 /* Translate the logical address and get the page */
620 psize = hpte_page_size(hpte[0], r);
621 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
622 gfn_base = gpa_base >> PAGE_SHIFT;
623 gpa = gpa_base | (ea & (psize - 1));
624 gfn = gpa >> PAGE_SHIFT;
625 memslot = gfn_to_memslot(kvm, gfn);
627 trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr);
629 /* No memslot means it's an emulated MMIO region */
630 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
631 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
632 dsisr & DSISR_ISSTORE);
634 if (!kvm->arch.using_mmu_notifiers)
635 return -EFAULT; /* should never get here */
638 * This should never happen, because of the slot_is_aligned()
639 * check in kvmppc_do_h_enter().
641 if (gfn_base < memslot->base_gfn)
644 /* used to check for invalidations in progress */
645 mmu_seq = kvm->mmu_notifier_seq;
652 pte_size = PAGE_SIZE;
653 writing = (dsisr & DSISR_ISSTORE) != 0;
654 /* If writing != 0, then the HPTE must allow writing, if we get here */
656 hva = gfn_to_hva_memslot(memslot, gfn);
657 npages = get_user_pages_fast(hva, 1, writing, pages);
659 /* Check if it's an I/O mapping */
660 down_read(¤t->mm->mmap_sem);
661 vma = find_vma(current->mm, hva);
662 if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
663 (vma->vm_flags & VM_PFNMAP)) {
664 pfn = vma->vm_pgoff +
665 ((hva - vma->vm_start) >> PAGE_SHIFT);
667 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
668 write_ok = vma->vm_flags & VM_WRITE;
670 up_read(¤t->mm->mmap_sem);
675 pfn = page_to_pfn(page);
676 if (PageHuge(page)) {
677 page = compound_head(page);
678 pte_size <<= compound_order(page);
680 /* if the guest wants write access, see if that is OK */
681 if (!writing && hpte_is_writable(r)) {
682 unsigned int hugepage_shift;
686 * We need to protect against page table destruction
687 * while looking up and updating the pte.
689 rcu_read_lock_sched();
690 ptep = find_linux_pte_or_hugepte(current->mm->pgd,
691 hva, &hugepage_shift);
693 pte = kvmppc_read_update_linux_pte(ptep, 1,
698 rcu_read_unlock_sched();
702 if (psize > pte_size)
705 /* Check WIMG vs. the actual page we're accessing */
706 if (!hpte_cache_flags_ok(r, is_io)) {
711 * Allow guest to map emulated device memory as
712 * uncacheable, but actually make it cacheable.
714 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
718 * Set the HPTE to point to pfn.
719 * Since the pfn is at PAGE_SIZE granularity, make sure we
720 * don't mask out lower-order bits if psize < PAGE_SIZE.
722 if (psize < PAGE_SIZE)
724 r = (r & ~(HPTE_R_PP0 - psize)) | ((pfn << PAGE_SHIFT) & ~(psize - 1));
725 if (hpte_is_writable(r) && !write_ok)
726 r = hpte_make_readonly(r);
729 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
731 if ((be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK) != hpte[0] ||
732 be64_to_cpu(hptep[1]) != hpte[1] ||
733 rev->guest_rpte != hpte[2])
734 /* HPTE has been changed under us; let the guest retry */
736 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
738 /* Always put the HPTE in the rmap chain for the page base address */
739 rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn];
742 /* Check if we might have been invalidated; let the guest retry if so */
744 if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
749 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
750 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
751 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
753 if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) {
754 /* HPTE was previously valid, so we need to invalidate it */
756 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
757 kvmppc_invalidate_hpte(kvm, hptep, index);
758 /* don't lose previous R and C bits */
759 r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
761 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
764 hptep[1] = cpu_to_be64(r);
766 hptep[0] = cpu_to_be64(hpte[0]);
767 asm volatile("ptesync" : : : "memory");
769 if (page && hpte_is_writable(r))
773 trace_kvm_page_fault_exit(vcpu, hpte, ret);
777 * We drop pages[0] here, not page because page might
778 * have been set to the head page of a compound, but
779 * we have to drop the reference on the correct tail
780 * page to match the get inside gup()
787 hptep[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
792 static void kvmppc_rmap_reset(struct kvm *kvm)
794 struct kvm_memslots *slots;
795 struct kvm_memory_slot *memslot;
798 srcu_idx = srcu_read_lock(&kvm->srcu);
799 slots = kvm->memslots;
800 kvm_for_each_memslot(memslot, slots) {
802 * This assumes it is acceptable to lose reference and
803 * change bits across a reset.
805 memset(memslot->arch.rmap, 0,
806 memslot->npages * sizeof(*memslot->arch.rmap));
808 srcu_read_unlock(&kvm->srcu, srcu_idx);
811 static int kvm_handle_hva_range(struct kvm *kvm,
814 int (*handler)(struct kvm *kvm,
815 unsigned long *rmapp,
820 struct kvm_memslots *slots;
821 struct kvm_memory_slot *memslot;
823 slots = kvm_memslots(kvm);
824 kvm_for_each_memslot(memslot, slots) {
825 unsigned long hva_start, hva_end;
828 hva_start = max(start, memslot->userspace_addr);
829 hva_end = min(end, memslot->userspace_addr +
830 (memslot->npages << PAGE_SHIFT));
831 if (hva_start >= hva_end)
834 * {gfn(page) | page intersects with [hva_start, hva_end)} =
835 * {gfn, gfn+1, ..., gfn_end-1}.
837 gfn = hva_to_gfn_memslot(hva_start, memslot);
838 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
840 for (; gfn < gfn_end; ++gfn) {
841 gfn_t gfn_offset = gfn - memslot->base_gfn;
843 ret = handler(kvm, &memslot->arch.rmap[gfn_offset], gfn);
851 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
852 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
855 return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
858 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
861 struct revmap_entry *rev = kvm->arch.revmap;
862 unsigned long h, i, j;
864 unsigned long ptel, psize, rcbits;
868 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
874 * To avoid an ABBA deadlock with the HPTE lock bit,
875 * we can't spin on the HPTE lock while holding the
878 i = *rmapp & KVMPPC_RMAP_INDEX;
879 hptep = (__be64 *) (kvm->arch.hpt_virt + (i << 4));
880 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
881 /* unlock rmap before spinning on the HPTE lock */
883 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
889 /* chain is now empty */
890 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
892 /* remove i from chain */
896 rev[i].forw = rev[i].back = i;
897 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
900 /* Now check and modify the HPTE */
901 ptel = rev[i].guest_rpte;
902 psize = hpte_page_size(be64_to_cpu(hptep[0]), ptel);
903 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
904 hpte_rpn(ptel, psize) == gfn) {
905 if (kvm->arch.using_mmu_notifiers)
906 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
907 kvmppc_invalidate_hpte(kvm, hptep, i);
908 /* Harvest R and C */
909 rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
910 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
911 if (rcbits & ~rev[i].guest_rpte) {
912 rev[i].guest_rpte = ptel | rcbits;
913 note_hpte_modification(kvm, &rev[i]);
917 hptep[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
922 int kvm_unmap_hva_hv(struct kvm *kvm, unsigned long hva)
924 if (kvm->arch.using_mmu_notifiers)
925 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
929 int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start, unsigned long end)
931 if (kvm->arch.using_mmu_notifiers)
932 kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp);
936 void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
937 struct kvm_memory_slot *memslot)
939 unsigned long *rmapp;
943 rmapp = memslot->arch.rmap;
944 gfn = memslot->base_gfn;
945 for (n = memslot->npages; n; --n) {
947 * Testing the present bit without locking is OK because
948 * the memslot has been marked invalid already, and hence
949 * no new HPTEs referencing this page can be created,
950 * thus the present bit can't go from 0 to 1.
952 if (*rmapp & KVMPPC_RMAP_PRESENT)
953 kvm_unmap_rmapp(kvm, rmapp, gfn);
959 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
962 struct revmap_entry *rev = kvm->arch.revmap;
963 unsigned long head, i, j;
969 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
970 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
973 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
978 i = head = *rmapp & KVMPPC_RMAP_INDEX;
980 hptep = (__be64 *) (kvm->arch.hpt_virt + (i << 4));
983 /* If this HPTE isn't referenced, ignore it */
984 if (!(be64_to_cpu(hptep[1]) & HPTE_R_R))
987 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
988 /* unlock rmap before spinning on the HPTE lock */
990 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
995 /* Now check and modify the HPTE */
996 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
997 (be64_to_cpu(hptep[1]) & HPTE_R_R)) {
998 kvmppc_clear_ref_hpte(kvm, hptep, i);
999 if (!(rev[i].guest_rpte & HPTE_R_R)) {
1000 rev[i].guest_rpte |= HPTE_R_R;
1001 note_hpte_modification(kvm, &rev[i]);
1005 hptep[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
1006 } while ((i = j) != head);
1012 int kvm_age_hva_hv(struct kvm *kvm, unsigned long start, unsigned long end)
1014 if (!kvm->arch.using_mmu_notifiers)
1016 return kvm_handle_hva_range(kvm, start, end, kvm_age_rmapp);
1019 static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1022 struct revmap_entry *rev = kvm->arch.revmap;
1023 unsigned long head, i, j;
1027 if (*rmapp & KVMPPC_RMAP_REFERENCED)
1031 if (*rmapp & KVMPPC_RMAP_REFERENCED)
1034 if (*rmapp & KVMPPC_RMAP_PRESENT) {
1035 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1037 hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
1039 if (be64_to_cpu(hp[1]) & HPTE_R_R)
1041 } while ((i = j) != head);
1050 int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva)
1052 if (!kvm->arch.using_mmu_notifiers)
1054 return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
1057 void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte)
1059 if (!kvm->arch.using_mmu_notifiers)
1061 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
1064 static int vcpus_running(struct kvm *kvm)
1066 return atomic_read(&kvm->arch.vcpus_running) != 0;
1070 * Returns the number of system pages that are dirty.
1071 * This can be more than 1 if we find a huge-page HPTE.
1073 static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp)
1075 struct revmap_entry *rev = kvm->arch.revmap;
1076 unsigned long head, i, j;
1080 int npages_dirty = 0;
1084 if (*rmapp & KVMPPC_RMAP_CHANGED) {
1085 *rmapp &= ~KVMPPC_RMAP_CHANGED;
1088 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
1090 return npages_dirty;
1093 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1095 unsigned long hptep1;
1096 hptep = (__be64 *) (kvm->arch.hpt_virt + (i << 4));
1100 * Checking the C (changed) bit here is racy since there
1101 * is no guarantee about when the hardware writes it back.
1102 * If the HPTE is not writable then it is stable since the
1103 * page can't be written to, and we would have done a tlbie
1104 * (which forces the hardware to complete any writeback)
1105 * when making the HPTE read-only.
1106 * If vcpus are running then this call is racy anyway
1107 * since the page could get dirtied subsequently, so we
1108 * expect there to be a further call which would pick up
1109 * any delayed C bit writeback.
1110 * Otherwise we need to do the tlbie even if C==0 in
1111 * order to pick up any delayed writeback of C.
1113 hptep1 = be64_to_cpu(hptep[1]);
1114 if (!(hptep1 & HPTE_R_C) &&
1115 (!hpte_is_writable(hptep1) || vcpus_running(kvm)))
1118 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
1119 /* unlock rmap before spinning on the HPTE lock */
1121 while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK))
1126 /* Now check and modify the HPTE */
1127 if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) {
1128 /* unlock and continue */
1129 hptep[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
1133 /* need to make it temporarily absent so C is stable */
1134 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
1135 kvmppc_invalidate_hpte(kvm, hptep, i);
1136 v = be64_to_cpu(hptep[0]);
1137 r = be64_to_cpu(hptep[1]);
1139 hptep[1] = cpu_to_be64(r & ~HPTE_R_C);
1140 if (!(rev[i].guest_rpte & HPTE_R_C)) {
1141 rev[i].guest_rpte |= HPTE_R_C;
1142 note_hpte_modification(kvm, &rev[i]);
1144 n = hpte_page_size(v, r);
1145 n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT;
1146 if (n > npages_dirty)
1150 v &= ~(HPTE_V_ABSENT | HPTE_V_HVLOCK);
1152 hptep[0] = cpu_to_be64(v);
1153 } while ((i = j) != head);
1156 return npages_dirty;
1159 static void harvest_vpa_dirty(struct kvmppc_vpa *vpa,
1160 struct kvm_memory_slot *memslot,
1165 if (!vpa->dirty || !vpa->pinned_addr)
1167 gfn = vpa->gpa >> PAGE_SHIFT;
1168 if (gfn < memslot->base_gfn ||
1169 gfn >= memslot->base_gfn + memslot->npages)
1174 __set_bit_le(gfn - memslot->base_gfn, map);
1177 long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot,
1181 unsigned long *rmapp;
1182 struct kvm_vcpu *vcpu;
1185 rmapp = memslot->arch.rmap;
1186 for (i = 0; i < memslot->npages; ++i) {
1187 int npages = kvm_test_clear_dirty_npages(kvm, rmapp);
1189 * Note that if npages > 0 then i must be a multiple of npages,
1190 * since we always put huge-page HPTEs in the rmap chain
1191 * corresponding to their page base address.
1194 for (j = i; npages; ++j, --npages)
1195 __set_bit_le(j, map);
1199 /* Harvest dirty bits from VPA and DTL updates */
1200 /* Note: we never modify the SLB shadow buffer areas */
1201 kvm_for_each_vcpu(i, vcpu, kvm) {
1202 spin_lock(&vcpu->arch.vpa_update_lock);
1203 harvest_vpa_dirty(&vcpu->arch.vpa, memslot, map);
1204 harvest_vpa_dirty(&vcpu->arch.dtl, memslot, map);
1205 spin_unlock(&vcpu->arch.vpa_update_lock);
1211 void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
1212 unsigned long *nb_ret)
1214 struct kvm_memory_slot *memslot;
1215 unsigned long gfn = gpa >> PAGE_SHIFT;
1216 struct page *page, *pages[1];
1218 unsigned long hva, offset;
1220 unsigned long *physp;
1223 srcu_idx = srcu_read_lock(&kvm->srcu);
1224 memslot = gfn_to_memslot(kvm, gfn);
1225 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
1227 if (!kvm->arch.using_mmu_notifiers) {
1228 physp = memslot->arch.slot_phys;
1231 physp += gfn - memslot->base_gfn;
1234 if (kvmppc_get_guest_page(kvm, gfn, memslot,
1239 page = pfn_to_page(pa >> PAGE_SHIFT);
1242 hva = gfn_to_hva_memslot(memslot, gfn);
1243 npages = get_user_pages_fast(hva, 1, 1, pages);
1248 srcu_read_unlock(&kvm->srcu, srcu_idx);
1250 offset = gpa & (PAGE_SIZE - 1);
1252 *nb_ret = PAGE_SIZE - offset;
1253 return page_address(page) + offset;
1256 srcu_read_unlock(&kvm->srcu, srcu_idx);
1260 void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
1263 struct page *page = virt_to_page(va);
1264 struct kvm_memory_slot *memslot;
1266 unsigned long *rmap;
1271 if (!dirty || !kvm->arch.using_mmu_notifiers)
1274 /* We need to mark this page dirty in the rmap chain */
1275 gfn = gpa >> PAGE_SHIFT;
1276 srcu_idx = srcu_read_lock(&kvm->srcu);
1277 memslot = gfn_to_memslot(kvm, gfn);
1279 rmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
1281 *rmap |= KVMPPC_RMAP_CHANGED;
1284 srcu_read_unlock(&kvm->srcu, srcu_idx);
1288 * Functions for reading and writing the hash table via reads and
1289 * writes on a file descriptor.
1291 * Reads return the guest view of the hash table, which has to be
1292 * pieced together from the real hash table and the guest_rpte
1293 * values in the revmap array.
1295 * On writes, each HPTE written is considered in turn, and if it
1296 * is valid, it is written to the HPT as if an H_ENTER with the
1297 * exact flag set was done. When the invalid count is non-zero
1298 * in the header written to the stream, the kernel will make
1299 * sure that that many HPTEs are invalid, and invalidate them
1303 struct kvm_htab_ctx {
1304 unsigned long index;
1305 unsigned long flags;
1310 #define HPTE_SIZE (2 * sizeof(unsigned long))
1313 * Returns 1 if this HPT entry has been modified or has pending
1316 static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp)
1318 unsigned long rcbits_unset;
1320 if (revp->guest_rpte & HPTE_GR_MODIFIED)
1323 /* Also need to consider changes in reference and changed bits */
1324 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1325 if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) &&
1326 (be64_to_cpu(hptp[1]) & rcbits_unset))
1332 static long record_hpte(unsigned long flags, __be64 *hptp,
1333 unsigned long *hpte, struct revmap_entry *revp,
1334 int want_valid, int first_pass)
1337 unsigned long rcbits_unset;
1341 /* Unmodified entries are uninteresting except on the first pass */
1342 dirty = hpte_dirty(revp, hptp);
1343 if (!first_pass && !dirty)
1347 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) {
1349 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
1350 !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED))
1353 if (valid != want_valid)
1357 if (valid || dirty) {
1358 /* lock the HPTE so it's stable and read it */
1360 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
1362 v = be64_to_cpu(hptp[0]);
1364 /* re-evaluate valid and dirty from synchronized HPTE value */
1365 valid = !!(v & HPTE_V_VALID);
1366 dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
1368 /* Harvest R and C into guest view if necessary */
1369 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1370 if (valid && (rcbits_unset & be64_to_cpu(hptp[1]))) {
1371 revp->guest_rpte |= (be64_to_cpu(hptp[1]) &
1372 (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED;
1376 if (v & HPTE_V_ABSENT) {
1377 v &= ~HPTE_V_ABSENT;
1381 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
1384 r = revp->guest_rpte;
1385 /* only clear modified if this is the right sort of entry */
1386 if (valid == want_valid && dirty) {
1387 r &= ~HPTE_GR_MODIFIED;
1388 revp->guest_rpte = r;
1390 asm volatile(PPC_RELEASE_BARRIER "" : : : "memory");
1391 hptp[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
1393 if (!(valid == want_valid && (first_pass || dirty)))
1396 hpte[0] = cpu_to_be64(v);
1397 hpte[1] = cpu_to_be64(r);
1401 static ssize_t kvm_htab_read(struct file *file, char __user *buf,
1402 size_t count, loff_t *ppos)
1404 struct kvm_htab_ctx *ctx = file->private_data;
1405 struct kvm *kvm = ctx->kvm;
1406 struct kvm_get_htab_header hdr;
1408 struct revmap_entry *revp;
1409 unsigned long i, nb, nw;
1410 unsigned long __user *lbuf;
1411 struct kvm_get_htab_header __user *hptr;
1412 unsigned long flags;
1414 unsigned long hpte[2];
1416 if (!access_ok(VERIFY_WRITE, buf, count))
1419 first_pass = ctx->first_pass;
1423 hptp = (__be64 *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
1424 revp = kvm->arch.revmap + i;
1425 lbuf = (unsigned long __user *)buf;
1428 while (nb + sizeof(hdr) + HPTE_SIZE < count) {
1429 /* Initialize header */
1430 hptr = (struct kvm_get_htab_header __user *)buf;
1435 lbuf = (unsigned long __user *)(buf + sizeof(hdr));
1437 /* Skip uninteresting entries, i.e. clean on not-first pass */
1439 while (i < kvm->arch.hpt_npte &&
1440 !hpte_dirty(revp, hptp)) {
1448 /* Grab a series of valid entries */
1449 while (i < kvm->arch.hpt_npte &&
1450 hdr.n_valid < 0xffff &&
1451 nb + HPTE_SIZE < count &&
1452 record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
1453 /* valid entry, write it out */
1455 if (__put_user(hpte[0], lbuf) ||
1456 __put_user(hpte[1], lbuf + 1))
1464 /* Now skip invalid entries while we can */
1465 while (i < kvm->arch.hpt_npte &&
1466 hdr.n_invalid < 0xffff &&
1467 record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
1468 /* found an invalid entry */
1475 if (hdr.n_valid || hdr.n_invalid) {
1476 /* write back the header */
1477 if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
1480 buf = (char __user *)lbuf;
1485 /* Check if we've wrapped around the hash table */
1486 if (i >= kvm->arch.hpt_npte) {
1488 ctx->first_pass = 0;
1498 static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
1499 size_t count, loff_t *ppos)
1501 struct kvm_htab_ctx *ctx = file->private_data;
1502 struct kvm *kvm = ctx->kvm;
1503 struct kvm_get_htab_header hdr;
1506 unsigned long __user *lbuf;
1508 unsigned long tmp[2];
1513 if (!access_ok(VERIFY_READ, buf, count))
1516 /* lock out vcpus from running while we're doing this */
1517 mutex_lock(&kvm->lock);
1518 rma_setup = kvm->arch.rma_setup_done;
1520 kvm->arch.rma_setup_done = 0; /* temporarily */
1521 /* order rma_setup_done vs. vcpus_running */
1523 if (atomic_read(&kvm->arch.vcpus_running)) {
1524 kvm->arch.rma_setup_done = 1;
1525 mutex_unlock(&kvm->lock);
1531 for (nb = 0; nb + sizeof(hdr) <= count; ) {
1533 if (__copy_from_user(&hdr, buf, sizeof(hdr)))
1537 if (nb + hdr.n_valid * HPTE_SIZE > count)
1545 if (i >= kvm->arch.hpt_npte ||
1546 i + hdr.n_valid + hdr.n_invalid > kvm->arch.hpt_npte)
1549 hptp = (__be64 *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
1550 lbuf = (unsigned long __user *)buf;
1551 for (j = 0; j < hdr.n_valid; ++j) {
1556 if (__get_user(hpte_v, lbuf) ||
1557 __get_user(hpte_r, lbuf + 1))
1559 v = be64_to_cpu(hpte_v);
1560 r = be64_to_cpu(hpte_r);
1562 if (!(v & HPTE_V_VALID))
1567 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
1568 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1570 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
1572 if (ret != H_SUCCESS) {
1573 pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
1574 "r=%lx\n", ret, i, v, r);
1577 if (!rma_setup && is_vrma_hpte(v)) {
1578 unsigned long psize = hpte_base_page_size(v, r);
1579 unsigned long senc = slb_pgsize_encoding(psize);
1582 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1583 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1584 lpcr = senc << (LPCR_VRMASD_SH - 4);
1585 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
1592 for (j = 0; j < hdr.n_invalid; ++j) {
1593 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
1594 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1602 /* Order HPTE updates vs. rma_setup_done */
1604 kvm->arch.rma_setup_done = rma_setup;
1605 mutex_unlock(&kvm->lock);
1612 static int kvm_htab_release(struct inode *inode, struct file *filp)
1614 struct kvm_htab_ctx *ctx = filp->private_data;
1616 filp->private_data = NULL;
1617 if (!(ctx->flags & KVM_GET_HTAB_WRITE))
1618 atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
1619 kvm_put_kvm(ctx->kvm);
1624 static const struct file_operations kvm_htab_fops = {
1625 .read = kvm_htab_read,
1626 .write = kvm_htab_write,
1627 .llseek = default_llseek,
1628 .release = kvm_htab_release,
1631 int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
1634 struct kvm_htab_ctx *ctx;
1637 /* reject flags we don't recognize */
1638 if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
1640 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1645 ctx->index = ghf->start_index;
1646 ctx->flags = ghf->flags;
1647 ctx->first_pass = 1;
1649 rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
1650 ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
1656 if (rwflag == O_RDONLY) {
1657 mutex_lock(&kvm->slots_lock);
1658 atomic_inc(&kvm->arch.hpte_mod_interest);
1659 /* make sure kvmppc_do_h_enter etc. see the increment */
1660 synchronize_srcu_expedited(&kvm->srcu);
1661 mutex_unlock(&kvm->slots_lock);
1667 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
1669 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
1671 if (cpu_has_feature(CPU_FTR_ARCH_206))
1672 vcpu->arch.slb_nr = 32; /* POWER7 */
1674 vcpu->arch.slb_nr = 64;
1676 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
1677 mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
1679 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;