1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
7 #include <linux/types.h>
8 #include <linux/string.h>
10 #include <linux/kvm_host.h>
11 #include <linux/highmem.h>
12 #include <linux/gfp.h>
13 #include <linux/slab.h>
14 #include <linux/hugetlb.h>
15 #include <linux/vmalloc.h>
16 #include <linux/srcu.h>
17 #include <linux/anon_inodes.h>
18 #include <linux/file.h>
19 #include <linux/debugfs.h>
21 #include <asm/kvm_ppc.h>
22 #include <asm/kvm_book3s.h>
23 #include <asm/book3s/64/mmu-hash.h>
24 #include <asm/hvcall.h>
25 #include <asm/synch.h>
26 #include <asm/ppc-opcode.h>
27 #include <asm/cputable.h>
28 #include <asm/pte-walk.h>
32 //#define DEBUG_RESIZE_HPT 1
34 #ifdef DEBUG_RESIZE_HPT
35 #define resize_hpt_debug(resize, ...) \
37 printk(KERN_DEBUG "RESIZE HPT %p: ", resize); \
38 printk(__VA_ARGS__); \
41 #define resize_hpt_debug(resize, ...) \
45 static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
46 long pte_index, unsigned long pteh,
47 unsigned long ptel, unsigned long *pte_idx_ret);
49 struct kvm_resize_hpt {
50 /* These fields read-only after init */
52 struct work_struct work;
55 /* These fields protected by kvm->arch.mmu_setup_lock */
57 /* Possible values and their usage:
58 * <0 an error occurred during allocation,
59 * -EBUSY allocation is in the progress,
60 * 0 allocation made successfuly.
64 /* Private to the work thread, until error != -EBUSY,
65 * then protected by kvm->arch.mmu_setup_lock.
67 struct kvm_hpt_info hpt;
70 int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order)
72 unsigned long hpt = 0;
74 struct page *page = NULL;
75 struct revmap_entry *rev;
78 if ((order < PPC_MIN_HPT_ORDER) || (order > PPC_MAX_HPT_ORDER))
81 page = kvm_alloc_hpt_cma(1ul << (order - PAGE_SHIFT));
83 hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
84 memset((void *)hpt, 0, (1ul << order));
89 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_RETRY_MAYFAIL
90 |__GFP_NOWARN, order - PAGE_SHIFT);
95 /* HPTEs are 2**4 bytes long */
96 npte = 1ul << (order - 4);
98 /* Allocate reverse map array */
99 rev = vmalloc(array_size(npte, sizeof(struct revmap_entry)));
102 kvm_free_hpt_cma(page, 1 << (order - PAGE_SHIFT));
104 free_pages(hpt, order - PAGE_SHIFT);
116 void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info)
118 atomic64_set(&kvm->arch.mmio_update, 0);
119 kvm->arch.hpt = *info;
120 kvm->arch.sdr1 = __pa(info->virt) | (info->order - 18);
122 pr_debug("KVM guest htab at %lx (order %ld), LPID %x\n",
123 info->virt, (long)info->order, kvm->arch.lpid);
126 long kvmppc_alloc_reset_hpt(struct kvm *kvm, int order)
129 struct kvm_hpt_info info;
131 mutex_lock(&kvm->arch.mmu_setup_lock);
132 if (kvm->arch.mmu_ready) {
133 kvm->arch.mmu_ready = 0;
134 /* order mmu_ready vs. vcpus_running */
136 if (atomic_read(&kvm->arch.vcpus_running)) {
137 kvm->arch.mmu_ready = 1;
141 if (kvm_is_radix(kvm)) {
142 err = kvmppc_switch_mmu_to_hpt(kvm);
147 if (kvm->arch.hpt.order == order) {
148 /* We already have a suitable HPT */
150 /* Set the entire HPT to 0, i.e. invalid HPTEs */
151 memset((void *)kvm->arch.hpt.virt, 0, 1ul << order);
153 * Reset all the reverse-mapping chains for all memslots
155 kvmppc_rmap_reset(kvm);
160 if (kvm->arch.hpt.virt) {
161 kvmppc_free_hpt(&kvm->arch.hpt);
162 kvmppc_rmap_reset(kvm);
165 err = kvmppc_allocate_hpt(&info, order);
168 kvmppc_set_hpt(kvm, &info);
172 /* Ensure that each vcpu will flush its TLB on next entry. */
173 cpumask_setall(&kvm->arch.need_tlb_flush);
175 mutex_unlock(&kvm->arch.mmu_setup_lock);
179 void kvmppc_free_hpt(struct kvm_hpt_info *info)
184 kvm_free_hpt_cma(virt_to_page(info->virt),
185 1 << (info->order - PAGE_SHIFT));
187 free_pages(info->virt, info->order - PAGE_SHIFT);
192 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
193 static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
195 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
198 /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
199 static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
201 return (pgsize == 0x10000) ? 0x1000 : 0;
204 void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
205 unsigned long porder)
208 unsigned long npages;
209 unsigned long hp_v, hp_r;
210 unsigned long addr, hash;
212 unsigned long hp0, hp1;
213 unsigned long idx_ret;
215 struct kvm *kvm = vcpu->kvm;
217 psize = 1ul << porder;
218 npages = memslot->npages >> (porder - PAGE_SHIFT);
220 /* VRMA can't be > 1TB */
221 if (npages > 1ul << (40 - porder))
222 npages = 1ul << (40 - porder);
223 /* Can't use more than 1 HPTE per HPTEG */
224 if (npages > kvmppc_hpt_mask(&kvm->arch.hpt) + 1)
225 npages = kvmppc_hpt_mask(&kvm->arch.hpt) + 1;
227 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
228 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
229 hp1 = hpte1_pgsize_encoding(psize) |
230 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
232 for (i = 0; i < npages; ++i) {
234 /* can't use hpt_hash since va > 64 bits */
235 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25)))
236 & kvmppc_hpt_mask(&kvm->arch.hpt);
238 * We assume that the hash table is empty and no
239 * vcpus are using it at this stage. Since we create
240 * at most one HPTE per HPTEG, we just assume entry 7
241 * is available and use it.
243 hash = (hash << 3) + 7;
244 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
246 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
248 if (ret != H_SUCCESS) {
249 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
256 int kvmppc_mmu_hv_init(void)
258 unsigned long host_lpid, rsvd_lpid;
260 if (!mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE))
263 /* POWER7 has 10-bit LPIDs (12-bit in POWER8) */
265 if (cpu_has_feature(CPU_FTR_HVMODE))
266 host_lpid = mfspr(SPRN_LPID);
267 rsvd_lpid = LPID_RSVD;
269 kvmppc_init_lpid(rsvd_lpid + 1);
271 kvmppc_claim_lpid(host_lpid);
272 /* rsvd_lpid is reserved for use in partition switching */
273 kvmppc_claim_lpid(rsvd_lpid);
278 static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
280 unsigned long msr = vcpu->arch.intr_msr;
282 /* If transactional, change to suspend mode on IRQ delivery */
283 if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
286 msr |= vcpu->arch.shregs.msr & MSR_TS_MASK;
287 kvmppc_set_msr(vcpu, msr);
290 static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
291 long pte_index, unsigned long pteh,
292 unsigned long ptel, unsigned long *pte_idx_ret)
296 /* Protect linux PTE lookup from page table destruction */
297 rcu_read_lock_sched(); /* this disables preemption too */
298 ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
299 current->mm->pgd, false, pte_idx_ret);
300 rcu_read_unlock_sched();
301 if (ret == H_TOO_HARD) {
302 /* this can't happen */
303 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
304 ret = H_RESOURCE; /* or something */
310 static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
316 for (i = 0; i < vcpu->arch.slb_nr; i++) {
317 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
320 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
325 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
326 return &vcpu->arch.slb[i];
331 static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
334 unsigned long ra_mask;
336 ra_mask = kvmppc_actual_pgsz(v, r) - 1;
337 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
340 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
341 struct kvmppc_pte *gpte, bool data, bool iswrite)
343 struct kvm *kvm = vcpu->kvm;
344 struct kvmppc_slb *slbe;
346 unsigned long pp, key;
347 unsigned long v, orig_v, gr;
350 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
352 if (kvm_is_radix(vcpu->kvm))
353 return kvmppc_mmu_radix_xlate(vcpu, eaddr, gpte, data, iswrite);
357 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
362 /* real mode access */
363 slb_v = vcpu->kvm->arch.vrma_slb_v;
367 /* Find the HPTE in the hash table */
368 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
369 HPTE_V_VALID | HPTE_V_ABSENT);
374 hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
375 v = orig_v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
376 if (cpu_has_feature(CPU_FTR_ARCH_300))
377 v = hpte_new_to_old_v(v, be64_to_cpu(hptep[1]));
378 gr = kvm->arch.hpt.rev[index].guest_rpte;
380 unlock_hpte(hptep, orig_v);
384 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
386 /* Get PP bits and key for permission check */
387 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
388 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
391 /* Calculate permissions */
392 gpte->may_read = hpte_read_permission(pp, key);
393 gpte->may_write = hpte_write_permission(pp, key);
394 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
396 /* Storage key permission check for POWER7 */
397 if (data && virtmode) {
398 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
405 /* Get the guest physical address */
406 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
411 * Quick test for whether an instruction is a load or a store.
412 * If the instruction is a load or a store, then this will indicate
413 * which it is, at least on server processors. (Embedded processors
414 * have some external PID instructions that don't follow the rule
415 * embodied here.) If the instruction isn't a load or store, then
416 * this doesn't return anything useful.
418 static int instruction_is_store(unsigned int instr)
423 if ((instr & 0xfc000000) == 0x7c000000)
424 mask = 0x100; /* major opcode 31 */
425 return (instr & mask) != 0;
428 int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
429 unsigned long gpa, gva_t ea, int is_store)
434 * Fast path - check if the guest physical address corresponds to a
435 * device on the FAST_MMIO_BUS, if so we can avoid loading the
436 * instruction all together, then we can just handle it and return.
441 idx = srcu_read_lock(&vcpu->kvm->srcu);
442 ret = kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, (gpa_t) gpa, 0,
444 srcu_read_unlock(&vcpu->kvm->srcu, idx);
446 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
452 * If we fail, we just return to the guest and try executing it again.
454 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
459 * WARNING: We do not know for sure whether the instruction we just
460 * read from memory is the same that caused the fault in the first
461 * place. If the instruction we read is neither an load or a store,
462 * then it can't access memory, so we don't need to worry about
463 * enforcing access permissions. So, assuming it is a load or
464 * store, we just check that its direction (load or store) is
465 * consistent with the original fault, since that's what we
466 * checked the access permissions against. If there is a mismatch
467 * we just return and retry the instruction.
470 if (instruction_is_store(last_inst) != !!is_store)
474 * Emulated accesses are emulated by looking at the hash for
475 * translation once, then performing the access later. The
476 * translation could be invalidated in the meantime in which
477 * point performing the subsequent memory access on the old
478 * physical address could possibly be a security hole for the
479 * guest (but not the host).
481 * This is less of an issue for MMIO stores since they aren't
482 * globally visible. It could be an issue for MMIO loads to
483 * a certain extent but we'll ignore it for now.
486 vcpu->arch.paddr_accessed = gpa;
487 vcpu->arch.vaddr_accessed = ea;
488 return kvmppc_emulate_mmio(run, vcpu);
491 int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
492 unsigned long ea, unsigned long dsisr)
494 struct kvm *kvm = vcpu->kvm;
495 unsigned long hpte[3], r;
496 unsigned long hnow_v, hnow_r;
498 unsigned long mmu_seq, psize, pte_size;
499 unsigned long gpa_base, gfn_base;
500 unsigned long gpa, gfn, hva, pfn;
501 struct kvm_memory_slot *memslot;
503 struct revmap_entry *rev;
504 struct page *page, *pages[1];
505 long index, ret, npages;
507 unsigned int writing, write_ok;
508 struct vm_area_struct *vma;
509 unsigned long rcbits;
512 if (kvm_is_radix(kvm))
513 return kvmppc_book3s_radix_page_fault(run, vcpu, ea, dsisr);
516 * Real-mode code has already searched the HPT and found the
517 * entry we're interested in. Lock the entry and check that
518 * it hasn't changed. If it has, just return and re-execute the
521 if (ea != vcpu->arch.pgfault_addr)
524 if (vcpu->arch.pgfault_cache) {
525 mmio_update = atomic64_read(&kvm->arch.mmio_update);
526 if (mmio_update == vcpu->arch.pgfault_cache->mmio_update) {
527 r = vcpu->arch.pgfault_cache->rpte;
528 psize = kvmppc_actual_pgsz(vcpu->arch.pgfault_hpte[0],
530 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
531 gfn_base = gpa_base >> PAGE_SHIFT;
532 gpa = gpa_base | (ea & (psize - 1));
533 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
534 dsisr & DSISR_ISSTORE);
537 index = vcpu->arch.pgfault_index;
538 hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
539 rev = &kvm->arch.hpt.rev[index];
541 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
543 hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
544 hpte[1] = be64_to_cpu(hptep[1]);
545 hpte[2] = r = rev->guest_rpte;
546 unlock_hpte(hptep, hpte[0]);
549 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
550 hpte[0] = hpte_new_to_old_v(hpte[0], hpte[1]);
551 hpte[1] = hpte_new_to_old_r(hpte[1]);
553 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
554 hpte[1] != vcpu->arch.pgfault_hpte[1])
557 /* Translate the logical address and get the page */
558 psize = kvmppc_actual_pgsz(hpte[0], r);
559 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
560 gfn_base = gpa_base >> PAGE_SHIFT;
561 gpa = gpa_base | (ea & (psize - 1));
562 gfn = gpa >> PAGE_SHIFT;
563 memslot = gfn_to_memslot(kvm, gfn);
565 trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr);
567 /* No memslot means it's an emulated MMIO region */
568 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
569 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
570 dsisr & DSISR_ISSTORE);
573 * This should never happen, because of the slot_is_aligned()
574 * check in kvmppc_do_h_enter().
576 if (gfn_base < memslot->base_gfn)
579 /* used to check for invalidations in progress */
580 mmu_seq = kvm->mmu_notifier_seq;
587 pte_size = PAGE_SIZE;
588 writing = (dsisr & DSISR_ISSTORE) != 0;
589 /* If writing != 0, then the HPTE must allow writing, if we get here */
591 hva = gfn_to_hva_memslot(memslot, gfn);
592 npages = get_user_pages_fast(hva, 1, writing ? FOLL_WRITE : 0, pages);
594 /* Check if it's an I/O mapping */
595 down_read(¤t->mm->mmap_sem);
596 vma = find_vma(current->mm, hva);
597 if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
598 (vma->vm_flags & VM_PFNMAP)) {
599 pfn = vma->vm_pgoff +
600 ((hva - vma->vm_start) >> PAGE_SHIFT);
602 is_ci = pte_ci(__pte((pgprot_val(vma->vm_page_prot))));
603 write_ok = vma->vm_flags & VM_WRITE;
605 up_read(¤t->mm->mmap_sem);
610 pfn = page_to_pfn(page);
611 if (PageHuge(page)) {
612 page = compound_head(page);
613 pte_size <<= compound_order(page);
615 /* if the guest wants write access, see if that is OK */
616 if (!writing && hpte_is_writable(r)) {
620 * We need to protect against page table destruction
621 * hugepage split and collapse.
623 local_irq_save(flags);
624 ptep = find_current_mm_pte(current->mm->pgd,
627 pte = kvmppc_read_update_linux_pte(ptep, 1);
628 if (__pte_write(pte))
631 local_irq_restore(flags);
635 if (psize > pte_size)
638 /* Check WIMG vs. the actual page we're accessing */
639 if (!hpte_cache_flags_ok(r, is_ci)) {
643 * Allow guest to map emulated device memory as
644 * uncacheable, but actually make it cacheable.
646 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
650 * Set the HPTE to point to pfn.
651 * Since the pfn is at PAGE_SIZE granularity, make sure we
652 * don't mask out lower-order bits if psize < PAGE_SIZE.
654 if (psize < PAGE_SIZE)
656 r = (r & HPTE_R_KEY_HI) | (r & ~(HPTE_R_PP0 - psize)) |
657 ((pfn << PAGE_SHIFT) & ~(psize - 1));
658 if (hpte_is_writable(r) && !write_ok)
659 r = hpte_make_readonly(r);
662 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
664 hnow_v = be64_to_cpu(hptep[0]);
665 hnow_r = be64_to_cpu(hptep[1]);
666 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
667 hnow_v = hpte_new_to_old_v(hnow_v, hnow_r);
668 hnow_r = hpte_new_to_old_r(hnow_r);
672 * If the HPT is being resized, don't update the HPTE,
673 * instead let the guest retry after the resize operation is complete.
674 * The synchronization for mmu_ready test vs. set is provided
677 if (!kvm->arch.mmu_ready)
680 if ((hnow_v & ~HPTE_V_HVLOCK) != hpte[0] || hnow_r != hpte[1] ||
681 rev->guest_rpte != hpte[2])
682 /* HPTE has been changed under us; let the guest retry */
684 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
686 /* Always put the HPTE in the rmap chain for the page base address */
687 rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn];
690 /* Check if we might have been invalidated; let the guest retry if so */
692 if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
697 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
698 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
699 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
701 if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) {
702 /* HPTE was previously valid, so we need to invalidate it */
704 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
705 kvmppc_invalidate_hpte(kvm, hptep, index);
706 /* don't lose previous R and C bits */
707 r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
709 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
712 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
713 r = hpte_old_to_new_r(hpte[0], r);
714 hpte[0] = hpte_old_to_new_v(hpte[0]);
716 hptep[1] = cpu_to_be64(r);
718 __unlock_hpte(hptep, hpte[0]);
719 asm volatile("ptesync" : : : "memory");
721 if (page && hpte_is_writable(r))
725 trace_kvm_page_fault_exit(vcpu, hpte, ret);
729 * We drop pages[0] here, not page because page might
730 * have been set to the head page of a compound, but
731 * we have to drop the reference on the correct tail
732 * page to match the get inside gup()
739 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
744 void kvmppc_rmap_reset(struct kvm *kvm)
746 struct kvm_memslots *slots;
747 struct kvm_memory_slot *memslot;
750 srcu_idx = srcu_read_lock(&kvm->srcu);
751 slots = kvm_memslots(kvm);
752 kvm_for_each_memslot(memslot, slots) {
753 /* Mutual exclusion with kvm_unmap_hva_range etc. */
754 spin_lock(&kvm->mmu_lock);
756 * This assumes it is acceptable to lose reference and
757 * change bits across a reset.
759 memset(memslot->arch.rmap, 0,
760 memslot->npages * sizeof(*memslot->arch.rmap));
761 spin_unlock(&kvm->mmu_lock);
763 srcu_read_unlock(&kvm->srcu, srcu_idx);
766 typedef int (*hva_handler_fn)(struct kvm *kvm, struct kvm_memory_slot *memslot,
769 static int kvm_handle_hva_range(struct kvm *kvm,
772 hva_handler_fn handler)
776 struct kvm_memslots *slots;
777 struct kvm_memory_slot *memslot;
779 slots = kvm_memslots(kvm);
780 kvm_for_each_memslot(memslot, slots) {
781 unsigned long hva_start, hva_end;
784 hva_start = max(start, memslot->userspace_addr);
785 hva_end = min(end, memslot->userspace_addr +
786 (memslot->npages << PAGE_SHIFT));
787 if (hva_start >= hva_end)
790 * {gfn(page) | page intersects with [hva_start, hva_end)} =
791 * {gfn, gfn+1, ..., gfn_end-1}.
793 gfn = hva_to_gfn_memslot(hva_start, memslot);
794 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
796 for (; gfn < gfn_end; ++gfn) {
797 ret = handler(kvm, memslot, gfn);
805 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
806 hva_handler_fn handler)
808 return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
811 /* Must be called with both HPTE and rmap locked */
812 static void kvmppc_unmap_hpte(struct kvm *kvm, unsigned long i,
813 struct kvm_memory_slot *memslot,
814 unsigned long *rmapp, unsigned long gfn)
816 __be64 *hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
817 struct revmap_entry *rev = kvm->arch.hpt.rev;
819 unsigned long ptel, psize, rcbits;
823 /* chain is now empty */
824 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
826 /* remove i from chain */
830 rev[i].forw = rev[i].back = i;
831 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
834 /* Now check and modify the HPTE */
835 ptel = rev[i].guest_rpte;
836 psize = kvmppc_actual_pgsz(be64_to_cpu(hptep[0]), ptel);
837 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
838 hpte_rpn(ptel, psize) == gfn) {
839 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
840 kvmppc_invalidate_hpte(kvm, hptep, i);
841 hptep[1] &= ~cpu_to_be64(HPTE_R_KEY_HI | HPTE_R_KEY_LO);
842 /* Harvest R and C */
843 rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
844 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
845 if ((rcbits & HPTE_R_C) && memslot->dirty_bitmap)
846 kvmppc_update_dirty_map(memslot, gfn, psize);
847 if (rcbits & ~rev[i].guest_rpte) {
848 rev[i].guest_rpte = ptel | rcbits;
849 note_hpte_modification(kvm, &rev[i]);
854 static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
859 unsigned long *rmapp;
861 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
864 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
870 * To avoid an ABBA deadlock with the HPTE lock bit,
871 * we can't spin on the HPTE lock while holding the
874 i = *rmapp & KVMPPC_RMAP_INDEX;
875 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
876 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
877 /* unlock rmap before spinning on the HPTE lock */
879 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
884 kvmppc_unmap_hpte(kvm, i, memslot, rmapp, gfn);
886 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
891 int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start, unsigned long end)
893 hva_handler_fn handler;
895 handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
896 kvm_handle_hva_range(kvm, start, end, handler);
900 void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
901 struct kvm_memory_slot *memslot)
905 unsigned long *rmapp;
907 gfn = memslot->base_gfn;
908 rmapp = memslot->arch.rmap;
909 if (kvm_is_radix(kvm)) {
910 kvmppc_radix_flush_memslot(kvm, memslot);
914 for (n = memslot->npages; n; --n, ++gfn) {
916 * Testing the present bit without locking is OK because
917 * the memslot has been marked invalid already, and hence
918 * no new HPTEs referencing this page can be created,
919 * thus the present bit can't go from 0 to 1.
921 if (*rmapp & KVMPPC_RMAP_PRESENT)
922 kvm_unmap_rmapp(kvm, memslot, gfn);
927 static int kvm_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
930 struct revmap_entry *rev = kvm->arch.hpt.rev;
931 unsigned long head, i, j;
934 unsigned long *rmapp;
936 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
939 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
940 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
943 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
948 i = head = *rmapp & KVMPPC_RMAP_INDEX;
950 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
953 /* If this HPTE isn't referenced, ignore it */
954 if (!(be64_to_cpu(hptep[1]) & HPTE_R_R))
957 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
958 /* unlock rmap before spinning on the HPTE lock */
960 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
965 /* Now check and modify the HPTE */
966 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
967 (be64_to_cpu(hptep[1]) & HPTE_R_R)) {
968 kvmppc_clear_ref_hpte(kvm, hptep, i);
969 if (!(rev[i].guest_rpte & HPTE_R_R)) {
970 rev[i].guest_rpte |= HPTE_R_R;
971 note_hpte_modification(kvm, &rev[i]);
975 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
976 } while ((i = j) != head);
982 int kvm_age_hva_hv(struct kvm *kvm, unsigned long start, unsigned long end)
984 hva_handler_fn handler;
986 handler = kvm_is_radix(kvm) ? kvm_age_radix : kvm_age_rmapp;
987 return kvm_handle_hva_range(kvm, start, end, handler);
990 static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
993 struct revmap_entry *rev = kvm->arch.hpt.rev;
994 unsigned long head, i, j;
997 unsigned long *rmapp;
999 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1000 if (*rmapp & KVMPPC_RMAP_REFERENCED)
1004 if (*rmapp & KVMPPC_RMAP_REFERENCED)
1007 if (*rmapp & KVMPPC_RMAP_PRESENT) {
1008 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1010 hp = (unsigned long *)(kvm->arch.hpt.virt + (i << 4));
1012 if (be64_to_cpu(hp[1]) & HPTE_R_R)
1014 } while ((i = j) != head);
1023 int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva)
1025 hva_handler_fn handler;
1027 handler = kvm_is_radix(kvm) ? kvm_test_age_radix : kvm_test_age_rmapp;
1028 return kvm_handle_hva(kvm, hva, handler);
1031 void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte)
1033 hva_handler_fn handler;
1035 handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
1036 kvm_handle_hva(kvm, hva, handler);
1039 static int vcpus_running(struct kvm *kvm)
1041 return atomic_read(&kvm->arch.vcpus_running) != 0;
1045 * Returns the number of system pages that are dirty.
1046 * This can be more than 1 if we find a huge-page HPTE.
1048 static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp)
1050 struct revmap_entry *rev = kvm->arch.hpt.rev;
1051 unsigned long head, i, j;
1055 int npages_dirty = 0;
1059 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
1061 return npages_dirty;
1064 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1066 unsigned long hptep1;
1067 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
1071 * Checking the C (changed) bit here is racy since there
1072 * is no guarantee about when the hardware writes it back.
1073 * If the HPTE is not writable then it is stable since the
1074 * page can't be written to, and we would have done a tlbie
1075 * (which forces the hardware to complete any writeback)
1076 * when making the HPTE read-only.
1077 * If vcpus are running then this call is racy anyway
1078 * since the page could get dirtied subsequently, so we
1079 * expect there to be a further call which would pick up
1080 * any delayed C bit writeback.
1081 * Otherwise we need to do the tlbie even if C==0 in
1082 * order to pick up any delayed writeback of C.
1084 hptep1 = be64_to_cpu(hptep[1]);
1085 if (!(hptep1 & HPTE_R_C) &&
1086 (!hpte_is_writable(hptep1) || vcpus_running(kvm)))
1089 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
1090 /* unlock rmap before spinning on the HPTE lock */
1092 while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK))
1097 /* Now check and modify the HPTE */
1098 if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) {
1099 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
1103 /* need to make it temporarily absent so C is stable */
1104 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
1105 kvmppc_invalidate_hpte(kvm, hptep, i);
1106 v = be64_to_cpu(hptep[0]);
1107 r = be64_to_cpu(hptep[1]);
1109 hptep[1] = cpu_to_be64(r & ~HPTE_R_C);
1110 if (!(rev[i].guest_rpte & HPTE_R_C)) {
1111 rev[i].guest_rpte |= HPTE_R_C;
1112 note_hpte_modification(kvm, &rev[i]);
1114 n = kvmppc_actual_pgsz(v, r);
1115 n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT;
1116 if (n > npages_dirty)
1120 v &= ~HPTE_V_ABSENT;
1122 __unlock_hpte(hptep, v);
1123 } while ((i = j) != head);
1126 return npages_dirty;
1129 void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa *vpa,
1130 struct kvm_memory_slot *memslot,
1135 if (!vpa->dirty || !vpa->pinned_addr)
1137 gfn = vpa->gpa >> PAGE_SHIFT;
1138 if (gfn < memslot->base_gfn ||
1139 gfn >= memslot->base_gfn + memslot->npages)
1144 __set_bit_le(gfn - memslot->base_gfn, map);
1147 long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm,
1148 struct kvm_memory_slot *memslot, unsigned long *map)
1151 unsigned long *rmapp;
1154 rmapp = memslot->arch.rmap;
1155 for (i = 0; i < memslot->npages; ++i) {
1156 int npages = kvm_test_clear_dirty_npages(kvm, rmapp);
1158 * Note that if npages > 0 then i must be a multiple of npages,
1159 * since we always put huge-page HPTEs in the rmap chain
1160 * corresponding to their page base address.
1163 set_dirty_bits(map, i, npages);
1170 void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
1171 unsigned long *nb_ret)
1173 struct kvm_memory_slot *memslot;
1174 unsigned long gfn = gpa >> PAGE_SHIFT;
1175 struct page *page, *pages[1];
1177 unsigned long hva, offset;
1180 srcu_idx = srcu_read_lock(&kvm->srcu);
1181 memslot = gfn_to_memslot(kvm, gfn);
1182 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
1184 hva = gfn_to_hva_memslot(memslot, gfn);
1185 npages = get_user_pages_fast(hva, 1, FOLL_WRITE, pages);
1189 srcu_read_unlock(&kvm->srcu, srcu_idx);
1191 offset = gpa & (PAGE_SIZE - 1);
1193 *nb_ret = PAGE_SIZE - offset;
1194 return page_address(page) + offset;
1197 srcu_read_unlock(&kvm->srcu, srcu_idx);
1201 void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
1204 struct page *page = virt_to_page(va);
1205 struct kvm_memory_slot *memslot;
1214 /* We need to mark this page dirty in the memslot dirty_bitmap, if any */
1215 gfn = gpa >> PAGE_SHIFT;
1216 srcu_idx = srcu_read_lock(&kvm->srcu);
1217 memslot = gfn_to_memslot(kvm, gfn);
1218 if (memslot && memslot->dirty_bitmap)
1219 set_bit_le(gfn - memslot->base_gfn, memslot->dirty_bitmap);
1220 srcu_read_unlock(&kvm->srcu, srcu_idx);
1226 static int resize_hpt_allocate(struct kvm_resize_hpt *resize)
1230 rc = kvmppc_allocate_hpt(&resize->hpt, resize->order);
1234 resize_hpt_debug(resize, "resize_hpt_allocate(): HPT @ 0x%lx\n",
1240 static unsigned long resize_hpt_rehash_hpte(struct kvm_resize_hpt *resize,
1243 struct kvm *kvm = resize->kvm;
1244 struct kvm_hpt_info *old = &kvm->arch.hpt;
1245 struct kvm_hpt_info *new = &resize->hpt;
1246 unsigned long old_hash_mask = (1ULL << (old->order - 7)) - 1;
1247 unsigned long new_hash_mask = (1ULL << (new->order - 7)) - 1;
1248 __be64 *hptep, *new_hptep;
1249 unsigned long vpte, rpte, guest_rpte;
1251 struct revmap_entry *rev;
1252 unsigned long apsize, avpn, pteg, hash;
1253 unsigned long new_idx, new_pteg, replace_vpte;
1256 hptep = (__be64 *)(old->virt + (idx << 4));
1258 /* Guest is stopped, so new HPTEs can't be added or faulted
1259 * in, only unmapped or altered by host actions. So, it's
1260 * safe to check this before we take the HPTE lock */
1261 vpte = be64_to_cpu(hptep[0]);
1262 if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
1263 return 0; /* nothing to do */
1265 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
1268 vpte = be64_to_cpu(hptep[0]);
1271 if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
1275 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1276 rpte = be64_to_cpu(hptep[1]);
1277 vpte = hpte_new_to_old_v(vpte, rpte);
1281 rev = &old->rev[idx];
1282 guest_rpte = rev->guest_rpte;
1285 apsize = kvmppc_actual_pgsz(vpte, guest_rpte);
1289 if (vpte & HPTE_V_VALID) {
1290 unsigned long gfn = hpte_rpn(guest_rpte, apsize);
1291 int srcu_idx = srcu_read_lock(&kvm->srcu);
1292 struct kvm_memory_slot *memslot =
1293 __gfn_to_memslot(kvm_memslots(kvm), gfn);
1296 unsigned long *rmapp;
1297 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1300 kvmppc_unmap_hpte(kvm, idx, memslot, rmapp, gfn);
1304 srcu_read_unlock(&kvm->srcu, srcu_idx);
1307 /* Reload PTE after unmap */
1308 vpte = be64_to_cpu(hptep[0]);
1309 BUG_ON(vpte & HPTE_V_VALID);
1310 BUG_ON(!(vpte & HPTE_V_ABSENT));
1313 if (!(vpte & HPTE_V_BOLTED))
1316 rpte = be64_to_cpu(hptep[1]);
1318 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1319 vpte = hpte_new_to_old_v(vpte, rpte);
1320 rpte = hpte_new_to_old_r(rpte);
1323 pshift = kvmppc_hpte_base_page_shift(vpte, rpte);
1324 avpn = HPTE_V_AVPN_VAL(vpte) & ~(((1ul << pshift) - 1) >> 23);
1325 pteg = idx / HPTES_PER_GROUP;
1326 if (vpte & HPTE_V_SECONDARY)
1329 if (!(vpte & HPTE_V_1TB_SEG)) {
1330 unsigned long offset, vsid;
1332 /* We only have 28 - 23 bits of offset in avpn */
1333 offset = (avpn & 0x1f) << 23;
1335 /* We can find more bits from the pteg value */
1337 offset |= ((vsid ^ pteg) & old_hash_mask) << pshift;
1339 hash = vsid ^ (offset >> pshift);
1341 unsigned long offset, vsid;
1343 /* We only have 40 - 23 bits of seg_off in avpn */
1344 offset = (avpn & 0x1ffff) << 23;
1347 offset |= ((vsid ^ (vsid << 25) ^ pteg) & old_hash_mask) << pshift;
1349 hash = vsid ^ (vsid << 25) ^ (offset >> pshift);
1352 new_pteg = hash & new_hash_mask;
1353 if (vpte & HPTE_V_SECONDARY)
1354 new_pteg = ~hash & new_hash_mask;
1356 new_idx = new_pteg * HPTES_PER_GROUP + (idx % HPTES_PER_GROUP);
1357 new_hptep = (__be64 *)(new->virt + (new_idx << 4));
1359 replace_vpte = be64_to_cpu(new_hptep[0]);
1360 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1361 unsigned long replace_rpte = be64_to_cpu(new_hptep[1]);
1362 replace_vpte = hpte_new_to_old_v(replace_vpte, replace_rpte);
1365 if (replace_vpte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
1366 BUG_ON(new->order >= old->order);
1368 if (replace_vpte & HPTE_V_BOLTED) {
1369 if (vpte & HPTE_V_BOLTED)
1370 /* Bolted collision, nothing we can do */
1372 /* Discard the new HPTE */
1376 /* Discard the previous HPTE */
1379 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1380 rpte = hpte_old_to_new_r(vpte, rpte);
1381 vpte = hpte_old_to_new_v(vpte);
1384 new_hptep[1] = cpu_to_be64(rpte);
1385 new->rev[new_idx].guest_rpte = guest_rpte;
1386 /* No need for a barrier, since new HPT isn't active */
1387 new_hptep[0] = cpu_to_be64(vpte);
1388 unlock_hpte(new_hptep, vpte);
1391 unlock_hpte(hptep, vpte);
1395 static int resize_hpt_rehash(struct kvm_resize_hpt *resize)
1397 struct kvm *kvm = resize->kvm;
1401 for (i = 0; i < kvmppc_hpt_npte(&kvm->arch.hpt); i++) {
1402 rc = resize_hpt_rehash_hpte(resize, i);
1410 static void resize_hpt_pivot(struct kvm_resize_hpt *resize)
1412 struct kvm *kvm = resize->kvm;
1413 struct kvm_hpt_info hpt_tmp;
1415 /* Exchange the pending tables in the resize structure with
1416 * the active tables */
1418 resize_hpt_debug(resize, "resize_hpt_pivot()\n");
1420 spin_lock(&kvm->mmu_lock);
1421 asm volatile("ptesync" : : : "memory");
1423 hpt_tmp = kvm->arch.hpt;
1424 kvmppc_set_hpt(kvm, &resize->hpt);
1425 resize->hpt = hpt_tmp;
1427 spin_unlock(&kvm->mmu_lock);
1429 synchronize_srcu_expedited(&kvm->srcu);
1431 if (cpu_has_feature(CPU_FTR_ARCH_300))
1432 kvmppc_setup_partition_table(kvm);
1434 resize_hpt_debug(resize, "resize_hpt_pivot() done\n");
1437 static void resize_hpt_release(struct kvm *kvm, struct kvm_resize_hpt *resize)
1439 if (WARN_ON(!mutex_is_locked(&kvm->arch.mmu_setup_lock)))
1445 if (resize->error != -EBUSY) {
1446 if (resize->hpt.virt)
1447 kvmppc_free_hpt(&resize->hpt);
1451 if (kvm->arch.resize_hpt == resize)
1452 kvm->arch.resize_hpt = NULL;
1455 static void resize_hpt_prepare_work(struct work_struct *work)
1457 struct kvm_resize_hpt *resize = container_of(work,
1458 struct kvm_resize_hpt,
1460 struct kvm *kvm = resize->kvm;
1463 if (WARN_ON(resize->error != -EBUSY))
1466 mutex_lock(&kvm->arch.mmu_setup_lock);
1468 /* Request is still current? */
1469 if (kvm->arch.resize_hpt == resize) {
1470 /* We may request large allocations here:
1471 * do not sleep with kvm->arch.mmu_setup_lock held for a while.
1473 mutex_unlock(&kvm->arch.mmu_setup_lock);
1475 resize_hpt_debug(resize, "resize_hpt_prepare_work(): order = %d\n",
1478 err = resize_hpt_allocate(resize);
1480 /* We have strict assumption about -EBUSY
1481 * when preparing for HPT resize.
1483 if (WARN_ON(err == -EBUSY))
1486 mutex_lock(&kvm->arch.mmu_setup_lock);
1487 /* It is possible that kvm->arch.resize_hpt != resize
1488 * after we grab kvm->arch.mmu_setup_lock again.
1492 resize->error = err;
1494 if (kvm->arch.resize_hpt != resize)
1495 resize_hpt_release(kvm, resize);
1497 mutex_unlock(&kvm->arch.mmu_setup_lock);
1500 long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
1501 struct kvm_ppc_resize_hpt *rhpt)
1503 unsigned long flags = rhpt->flags;
1504 unsigned long shift = rhpt->shift;
1505 struct kvm_resize_hpt *resize;
1508 if (flags != 0 || kvm_is_radix(kvm))
1511 if (shift && ((shift < 18) || (shift > 46)))
1514 mutex_lock(&kvm->arch.mmu_setup_lock);
1516 resize = kvm->arch.resize_hpt;
1519 if (resize->order == shift) {
1520 /* Suitable resize in progress? */
1521 ret = resize->error;
1523 ret = 100; /* estimated time in ms */
1525 resize_hpt_release(kvm, resize);
1530 /* not suitable, cancel it */
1531 resize_hpt_release(kvm, resize);
1536 goto out; /* nothing to do */
1538 /* start new resize */
1540 resize = kzalloc(sizeof(*resize), GFP_KERNEL);
1546 resize->error = -EBUSY;
1547 resize->order = shift;
1549 INIT_WORK(&resize->work, resize_hpt_prepare_work);
1550 kvm->arch.resize_hpt = resize;
1552 schedule_work(&resize->work);
1554 ret = 100; /* estimated time in ms */
1557 mutex_unlock(&kvm->arch.mmu_setup_lock);
1561 static void resize_hpt_boot_vcpu(void *opaque)
1563 /* Nothing to do, just force a KVM exit */
1566 long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
1567 struct kvm_ppc_resize_hpt *rhpt)
1569 unsigned long flags = rhpt->flags;
1570 unsigned long shift = rhpt->shift;
1571 struct kvm_resize_hpt *resize;
1574 if (flags != 0 || kvm_is_radix(kvm))
1577 if (shift && ((shift < 18) || (shift > 46)))
1580 mutex_lock(&kvm->arch.mmu_setup_lock);
1582 resize = kvm->arch.resize_hpt;
1584 /* This shouldn't be possible */
1586 if (WARN_ON(!kvm->arch.mmu_ready))
1589 /* Stop VCPUs from running while we mess with the HPT */
1590 kvm->arch.mmu_ready = 0;
1593 /* Boot all CPUs out of the guest so they re-read
1595 on_each_cpu(resize_hpt_boot_vcpu, NULL, 1);
1598 if (!resize || (resize->order != shift))
1601 ret = resize->error;
1605 ret = resize_hpt_rehash(resize);
1609 resize_hpt_pivot(resize);
1612 /* Let VCPUs run again */
1613 kvm->arch.mmu_ready = 1;
1616 resize_hpt_release(kvm, resize);
1617 mutex_unlock(&kvm->arch.mmu_setup_lock);
1622 * Functions for reading and writing the hash table via reads and
1623 * writes on a file descriptor.
1625 * Reads return the guest view of the hash table, which has to be
1626 * pieced together from the real hash table and the guest_rpte
1627 * values in the revmap array.
1629 * On writes, each HPTE written is considered in turn, and if it
1630 * is valid, it is written to the HPT as if an H_ENTER with the
1631 * exact flag set was done. When the invalid count is non-zero
1632 * in the header written to the stream, the kernel will make
1633 * sure that that many HPTEs are invalid, and invalidate them
1637 struct kvm_htab_ctx {
1638 unsigned long index;
1639 unsigned long flags;
1644 #define HPTE_SIZE (2 * sizeof(unsigned long))
1647 * Returns 1 if this HPT entry has been modified or has pending
1650 static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp)
1652 unsigned long rcbits_unset;
1654 if (revp->guest_rpte & HPTE_GR_MODIFIED)
1657 /* Also need to consider changes in reference and changed bits */
1658 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1659 if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) &&
1660 (be64_to_cpu(hptp[1]) & rcbits_unset))
1666 static long record_hpte(unsigned long flags, __be64 *hptp,
1667 unsigned long *hpte, struct revmap_entry *revp,
1668 int want_valid, int first_pass)
1670 unsigned long v, r, hr;
1671 unsigned long rcbits_unset;
1675 /* Unmodified entries are uninteresting except on the first pass */
1676 dirty = hpte_dirty(revp, hptp);
1677 if (!first_pass && !dirty)
1681 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) {
1683 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
1684 !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED))
1687 if (valid != want_valid)
1691 if (valid || dirty) {
1692 /* lock the HPTE so it's stable and read it */
1694 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
1696 v = be64_to_cpu(hptp[0]);
1697 hr = be64_to_cpu(hptp[1]);
1698 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1699 v = hpte_new_to_old_v(v, hr);
1700 hr = hpte_new_to_old_r(hr);
1703 /* re-evaluate valid and dirty from synchronized HPTE value */
1704 valid = !!(v & HPTE_V_VALID);
1705 dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
1707 /* Harvest R and C into guest view if necessary */
1708 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1709 if (valid && (rcbits_unset & hr)) {
1710 revp->guest_rpte |= (hr &
1711 (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED;
1715 if (v & HPTE_V_ABSENT) {
1716 v &= ~HPTE_V_ABSENT;
1720 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
1723 r = revp->guest_rpte;
1724 /* only clear modified if this is the right sort of entry */
1725 if (valid == want_valid && dirty) {
1726 r &= ~HPTE_GR_MODIFIED;
1727 revp->guest_rpte = r;
1729 unlock_hpte(hptp, be64_to_cpu(hptp[0]));
1731 if (!(valid == want_valid && (first_pass || dirty)))
1734 hpte[0] = cpu_to_be64(v);
1735 hpte[1] = cpu_to_be64(r);
1739 static ssize_t kvm_htab_read(struct file *file, char __user *buf,
1740 size_t count, loff_t *ppos)
1742 struct kvm_htab_ctx *ctx = file->private_data;
1743 struct kvm *kvm = ctx->kvm;
1744 struct kvm_get_htab_header hdr;
1746 struct revmap_entry *revp;
1747 unsigned long i, nb, nw;
1748 unsigned long __user *lbuf;
1749 struct kvm_get_htab_header __user *hptr;
1750 unsigned long flags;
1752 unsigned long hpte[2];
1754 if (!access_ok(buf, count))
1756 if (kvm_is_radix(kvm))
1759 first_pass = ctx->first_pass;
1763 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
1764 revp = kvm->arch.hpt.rev + i;
1765 lbuf = (unsigned long __user *)buf;
1768 while (nb + sizeof(hdr) + HPTE_SIZE < count) {
1769 /* Initialize header */
1770 hptr = (struct kvm_get_htab_header __user *)buf;
1775 lbuf = (unsigned long __user *)(buf + sizeof(hdr));
1777 /* Skip uninteresting entries, i.e. clean on not-first pass */
1779 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
1780 !hpte_dirty(revp, hptp)) {
1788 /* Grab a series of valid entries */
1789 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
1790 hdr.n_valid < 0xffff &&
1791 nb + HPTE_SIZE < count &&
1792 record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
1793 /* valid entry, write it out */
1795 if (__put_user(hpte[0], lbuf) ||
1796 __put_user(hpte[1], lbuf + 1))
1804 /* Now skip invalid entries while we can */
1805 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
1806 hdr.n_invalid < 0xffff &&
1807 record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
1808 /* found an invalid entry */
1815 if (hdr.n_valid || hdr.n_invalid) {
1816 /* write back the header */
1817 if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
1820 buf = (char __user *)lbuf;
1825 /* Check if we've wrapped around the hash table */
1826 if (i >= kvmppc_hpt_npte(&kvm->arch.hpt)) {
1828 ctx->first_pass = 0;
1838 static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
1839 size_t count, loff_t *ppos)
1841 struct kvm_htab_ctx *ctx = file->private_data;
1842 struct kvm *kvm = ctx->kvm;
1843 struct kvm_get_htab_header hdr;
1846 unsigned long __user *lbuf;
1848 unsigned long tmp[2];
1854 if (!access_ok(buf, count))
1856 if (kvm_is_radix(kvm))
1859 /* lock out vcpus from running while we're doing this */
1860 mutex_lock(&kvm->arch.mmu_setup_lock);
1861 mmu_ready = kvm->arch.mmu_ready;
1863 kvm->arch.mmu_ready = 0; /* temporarily */
1864 /* order mmu_ready vs. vcpus_running */
1866 if (atomic_read(&kvm->arch.vcpus_running)) {
1867 kvm->arch.mmu_ready = 1;
1868 mutex_unlock(&kvm->arch.mmu_setup_lock);
1874 for (nb = 0; nb + sizeof(hdr) <= count; ) {
1876 if (__copy_from_user(&hdr, buf, sizeof(hdr)))
1880 if (nb + hdr.n_valid * HPTE_SIZE > count)
1888 if (i >= kvmppc_hpt_npte(&kvm->arch.hpt) ||
1889 i + hdr.n_valid + hdr.n_invalid > kvmppc_hpt_npte(&kvm->arch.hpt))
1892 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
1893 lbuf = (unsigned long __user *)buf;
1894 for (j = 0; j < hdr.n_valid; ++j) {
1899 if (__get_user(hpte_v, lbuf) ||
1900 __get_user(hpte_r, lbuf + 1))
1902 v = be64_to_cpu(hpte_v);
1903 r = be64_to_cpu(hpte_r);
1905 if (!(v & HPTE_V_VALID))
1907 pshift = kvmppc_hpte_base_page_shift(v, r);
1913 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
1914 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1916 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
1918 if (ret != H_SUCCESS) {
1919 pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
1920 "r=%lx\n", ret, i, v, r);
1923 if (!mmu_ready && is_vrma_hpte(v)) {
1924 unsigned long senc, lpcr;
1926 senc = slb_pgsize_encoding(1ul << pshift);
1927 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1928 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1929 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
1930 lpcr = senc << (LPCR_VRMASD_SH - 4);
1931 kvmppc_update_lpcr(kvm, lpcr,
1934 kvmppc_setup_partition_table(kvm);
1942 for (j = 0; j < hdr.n_invalid; ++j) {
1943 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
1944 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1952 /* Order HPTE updates vs. mmu_ready */
1954 kvm->arch.mmu_ready = mmu_ready;
1955 mutex_unlock(&kvm->arch.mmu_setup_lock);
1962 static int kvm_htab_release(struct inode *inode, struct file *filp)
1964 struct kvm_htab_ctx *ctx = filp->private_data;
1966 filp->private_data = NULL;
1967 if (!(ctx->flags & KVM_GET_HTAB_WRITE))
1968 atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
1969 kvm_put_kvm(ctx->kvm);
1974 static const struct file_operations kvm_htab_fops = {
1975 .read = kvm_htab_read,
1976 .write = kvm_htab_write,
1977 .llseek = default_llseek,
1978 .release = kvm_htab_release,
1981 int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
1984 struct kvm_htab_ctx *ctx;
1987 /* reject flags we don't recognize */
1988 if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
1990 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1995 ctx->index = ghf->start_index;
1996 ctx->flags = ghf->flags;
1997 ctx->first_pass = 1;
1999 rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
2000 ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
2007 if (rwflag == O_RDONLY) {
2008 mutex_lock(&kvm->slots_lock);
2009 atomic_inc(&kvm->arch.hpte_mod_interest);
2010 /* make sure kvmppc_do_h_enter etc. see the increment */
2011 synchronize_srcu_expedited(&kvm->srcu);
2012 mutex_unlock(&kvm->slots_lock);
2018 struct debugfs_htab_state {
2021 unsigned long hpt_index;
2027 static int debugfs_htab_open(struct inode *inode, struct file *file)
2029 struct kvm *kvm = inode->i_private;
2030 struct debugfs_htab_state *p;
2032 p = kzalloc(sizeof(*p), GFP_KERNEL);
2038 mutex_init(&p->mutex);
2039 file->private_data = p;
2041 return nonseekable_open(inode, file);
2044 static int debugfs_htab_release(struct inode *inode, struct file *file)
2046 struct debugfs_htab_state *p = file->private_data;
2048 kvm_put_kvm(p->kvm);
2053 static ssize_t debugfs_htab_read(struct file *file, char __user *buf,
2054 size_t len, loff_t *ppos)
2056 struct debugfs_htab_state *p = file->private_data;
2059 unsigned long v, hr, gr;
2064 if (kvm_is_radix(kvm))
2067 ret = mutex_lock_interruptible(&p->mutex);
2071 if (p->chars_left) {
2075 r = copy_to_user(buf, p->buf + p->buf_index, n);
2090 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
2091 for (; len != 0 && i < kvmppc_hpt_npte(&kvm->arch.hpt);
2093 if (!(be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)))
2096 /* lock the HPTE so it's stable and read it */
2098 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
2100 v = be64_to_cpu(hptp[0]) & ~HPTE_V_HVLOCK;
2101 hr = be64_to_cpu(hptp[1]);
2102 gr = kvm->arch.hpt.rev[i].guest_rpte;
2103 unlock_hpte(hptp, v);
2106 if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
2109 n = scnprintf(p->buf, sizeof(p->buf),
2110 "%6lx %.16lx %.16lx %.16lx\n",
2115 r = copy_to_user(buf, p->buf, n);
2131 mutex_unlock(&p->mutex);
2135 static ssize_t debugfs_htab_write(struct file *file, const char __user *buf,
2136 size_t len, loff_t *ppos)
2141 static const struct file_operations debugfs_htab_fops = {
2142 .owner = THIS_MODULE,
2143 .open = debugfs_htab_open,
2144 .release = debugfs_htab_release,
2145 .read = debugfs_htab_read,
2146 .write = debugfs_htab_write,
2147 .llseek = generic_file_llseek,
2150 void kvmppc_mmu_debugfs_init(struct kvm *kvm)
2152 kvm->arch.htab_dentry = debugfs_create_file("htab", 0400,
2153 kvm->arch.debugfs_dir, kvm,
2154 &debugfs_htab_fops);
2157 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
2159 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
2161 vcpu->arch.slb_nr = 32; /* POWER7/POWER8 */
2163 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
2164 mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
2166 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;