1 // SPDX-License-Identifier: GPL-2.0-only
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>
31 #include "book3s_hv.h"
34 //#define DEBUG_RESIZE_HPT 1
36 #ifdef DEBUG_RESIZE_HPT
37 #define resize_hpt_debug(resize, ...) \
39 printk(KERN_DEBUG "RESIZE HPT %p: ", resize); \
40 printk(__VA_ARGS__); \
43 #define resize_hpt_debug(resize, ...) \
47 static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
48 long pte_index, unsigned long pteh,
49 unsigned long ptel, unsigned long *pte_idx_ret);
51 struct kvm_resize_hpt {
52 /* These fields read-only after init */
54 struct work_struct work;
57 /* These fields protected by kvm->arch.mmu_setup_lock */
59 /* Possible values and their usage:
60 * <0 an error occurred during allocation,
61 * -EBUSY allocation is in the progress,
62 * 0 allocation made successfully.
66 /* Private to the work thread, until error != -EBUSY,
67 * then protected by kvm->arch.mmu_setup_lock.
69 struct kvm_hpt_info hpt;
72 int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order)
74 unsigned long hpt = 0;
76 struct page *page = NULL;
77 struct revmap_entry *rev;
80 if ((order < PPC_MIN_HPT_ORDER) || (order > PPC_MAX_HPT_ORDER))
83 page = kvm_alloc_hpt_cma(1ul << (order - PAGE_SHIFT));
85 hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
86 memset((void *)hpt, 0, (1ul << order));
91 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_RETRY_MAYFAIL
92 |__GFP_NOWARN, order - PAGE_SHIFT);
97 /* HPTEs are 2**4 bytes long */
98 npte = 1ul << (order - 4);
100 /* Allocate reverse map array */
101 rev = vmalloc(array_size(npte, sizeof(struct revmap_entry)));
104 kvm_free_hpt_cma(page, 1 << (order - PAGE_SHIFT));
106 free_pages(hpt, order - PAGE_SHIFT);
118 void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info)
120 atomic64_set(&kvm->arch.mmio_update, 0);
121 kvm->arch.hpt = *info;
122 kvm->arch.sdr1 = __pa(info->virt) | (info->order - 18);
124 pr_debug("KVM guest htab at %lx (order %ld), LPID %llx\n",
125 info->virt, (long)info->order, kvm->arch.lpid);
128 int kvmppc_alloc_reset_hpt(struct kvm *kvm, int order)
131 struct kvm_hpt_info info;
133 mutex_lock(&kvm->arch.mmu_setup_lock);
134 if (kvm->arch.mmu_ready) {
135 kvm->arch.mmu_ready = 0;
136 /* order mmu_ready vs. vcpus_running */
138 if (atomic_read(&kvm->arch.vcpus_running)) {
139 kvm->arch.mmu_ready = 1;
143 if (kvm_is_radix(kvm)) {
144 err = kvmppc_switch_mmu_to_hpt(kvm);
149 if (kvm->arch.hpt.order == order) {
150 /* We already have a suitable HPT */
152 /* Set the entire HPT to 0, i.e. invalid HPTEs */
153 memset((void *)kvm->arch.hpt.virt, 0, 1ul << order);
155 * Reset all the reverse-mapping chains for all memslots
157 kvmppc_rmap_reset(kvm);
162 if (kvm->arch.hpt.virt) {
163 kvmppc_free_hpt(&kvm->arch.hpt);
164 kvmppc_rmap_reset(kvm);
167 err = kvmppc_allocate_hpt(&info, order);
170 kvmppc_set_hpt(kvm, &info);
174 /* Ensure that each vcpu will flush its TLB on next entry. */
175 cpumask_setall(&kvm->arch.need_tlb_flush);
177 mutex_unlock(&kvm->arch.mmu_setup_lock);
181 void kvmppc_free_hpt(struct kvm_hpt_info *info)
186 kvm_free_hpt_cma(virt_to_page((void *)info->virt),
187 1 << (info->order - PAGE_SHIFT));
189 free_pages(info->virt, info->order - PAGE_SHIFT);
194 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
195 static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
197 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
200 /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
201 static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
203 return (pgsize == 0x10000) ? 0x1000 : 0;
206 void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
207 unsigned long porder)
210 unsigned long npages;
211 unsigned long hp_v, hp_r;
212 unsigned long addr, hash;
214 unsigned long hp0, hp1;
215 unsigned long idx_ret;
217 struct kvm *kvm = vcpu->kvm;
219 psize = 1ul << porder;
220 npages = memslot->npages >> (porder - PAGE_SHIFT);
222 /* VRMA can't be > 1TB */
223 if (npages > 1ul << (40 - porder))
224 npages = 1ul << (40 - porder);
225 /* Can't use more than 1 HPTE per HPTEG */
226 if (npages > kvmppc_hpt_mask(&kvm->arch.hpt) + 1)
227 npages = kvmppc_hpt_mask(&kvm->arch.hpt) + 1;
229 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
230 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
231 hp1 = hpte1_pgsize_encoding(psize) |
232 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
234 for (i = 0; i < npages; ++i) {
236 /* can't use hpt_hash since va > 64 bits */
237 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25)))
238 & kvmppc_hpt_mask(&kvm->arch.hpt);
240 * We assume that the hash table is empty and no
241 * vcpus are using it at this stage. Since we create
242 * at most one HPTE per HPTEG, we just assume entry 7
243 * is available and use it.
245 hash = (hash << 3) + 7;
246 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
248 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
250 if (ret != H_SUCCESS) {
251 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
258 int kvmppc_mmu_hv_init(void)
260 unsigned long nr_lpids;
262 if (!mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE))
265 if (cpu_has_feature(CPU_FTR_HVMODE)) {
266 if (WARN_ON(mfspr(SPRN_LPID) != 0))
268 nr_lpids = 1UL << mmu_lpid_bits;
270 nr_lpids = 1UL << KVM_MAX_NESTED_GUESTS_SHIFT;
273 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
274 /* POWER7 has 10-bit LPIDs, POWER8 has 12-bit LPIDs */
275 if (cpu_has_feature(CPU_FTR_ARCH_207S))
276 WARN_ON(nr_lpids != 1UL << 12);
278 WARN_ON(nr_lpids != 1UL << 10);
281 * Reserve the last implemented LPID use in partition
282 * switching for POWER7 and POWER8.
287 kvmppc_init_lpid(nr_lpids);
292 static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
293 long pte_index, unsigned long pteh,
294 unsigned long ptel, unsigned long *pte_idx_ret)
299 ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
300 kvm->mm->pgd, false, pte_idx_ret);
302 if (ret == H_TOO_HARD) {
303 /* this can't happen */
304 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
305 ret = H_RESOURCE; /* or something */
311 static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
317 for (i = 0; i < vcpu->arch.slb_nr; i++) {
318 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
321 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
326 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
327 return &vcpu->arch.slb[i];
332 static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
335 unsigned long ra_mask;
337 ra_mask = kvmppc_actual_pgsz(v, r) - 1;
338 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
341 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
342 struct kvmppc_pte *gpte, bool data, bool iswrite)
344 struct kvm *kvm = vcpu->kvm;
345 struct kvmppc_slb *slbe;
347 unsigned long pp, key;
348 unsigned long v, orig_v, gr;
351 int virtmode = __kvmppc_get_msr_hv(vcpu) & (data ? MSR_DR : MSR_IR);
353 if (kvm_is_radix(vcpu->kvm))
354 return kvmppc_mmu_radix_xlate(vcpu, eaddr, gpte, data, iswrite);
358 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
363 /* real mode access */
364 slb_v = vcpu->kvm->arch.vrma_slb_v;
368 /* Find the HPTE in the hash table */
369 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
370 HPTE_V_VALID | HPTE_V_ABSENT);
375 hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
376 v = orig_v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
377 if (cpu_has_feature(CPU_FTR_ARCH_300))
378 v = hpte_new_to_old_v(v, be64_to_cpu(hptep[1]));
379 gr = kvm->arch.hpt.rev[index].guest_rpte;
381 unlock_hpte(hptep, orig_v);
385 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
387 /* Get PP bits and key for permission check */
388 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
389 key = (__kvmppc_get_msr_hv(vcpu) & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
392 /* Calculate permissions */
393 gpte->may_read = hpte_read_permission(pp, key);
394 gpte->may_write = hpte_write_permission(pp, key);
395 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
397 /* Storage key permission check for POWER7 */
398 if (data && virtmode) {
399 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
406 /* Get the guest physical address */
407 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
412 * Quick test for whether an instruction is a load or a store.
413 * If the instruction is a load or a store, then this will indicate
414 * which it is, at least on server processors. (Embedded processors
415 * have some external PID instructions that don't follow the rule
416 * embodied here.) If the instruction isn't a load or store, then
417 * this doesn't return anything useful.
419 static int instruction_is_store(ppc_inst_t instr)
425 suffix = ppc_inst_val(instr);
426 if (ppc_inst_prefixed(instr))
427 suffix = ppc_inst_suffix(instr);
428 else if ((suffix & 0xfc000000) == 0x7c000000)
429 mask = 0x100; /* major opcode 31 */
430 return (suffix & mask) != 0;
433 int kvmppc_hv_emulate_mmio(struct kvm_vcpu *vcpu,
434 unsigned long gpa, gva_t ea, int is_store)
436 ppc_inst_t last_inst;
437 bool is_prefixed = !!(kvmppc_get_msr(vcpu) & SRR1_PREFIXED);
440 * Fast path - check if the guest physical address corresponds to a
441 * device on the FAST_MMIO_BUS, if so we can avoid loading the
442 * instruction all together, then we can just handle it and return.
447 idx = srcu_read_lock(&vcpu->kvm->srcu);
448 ret = kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, (gpa_t) gpa, 0,
450 srcu_read_unlock(&vcpu->kvm->srcu, idx);
452 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + (is_prefixed ? 8 : 4));
458 * If we fail, we just return to the guest and try executing it again.
460 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
465 * WARNING: We do not know for sure whether the instruction we just
466 * read from memory is the same that caused the fault in the first
469 * If the fault is prefixed but the instruction is not or vice
470 * versa, try again so that we don't advance pc the wrong amount.
472 if (ppc_inst_prefixed(last_inst) != is_prefixed)
476 * If the instruction we read is neither an load or a store,
477 * then it can't access memory, so we don't need to worry about
478 * enforcing access permissions. So, assuming it is a load or
479 * store, we just check that its direction (load or store) is
480 * consistent with the original fault, since that's what we
481 * checked the access permissions against. If there is a mismatch
482 * we just return and retry the instruction.
485 if (instruction_is_store(last_inst) != !!is_store)
489 * Emulated accesses are emulated by looking at the hash for
490 * translation once, then performing the access later. The
491 * translation could be invalidated in the meantime in which
492 * point performing the subsequent memory access on the old
493 * physical address could possibly be a security hole for the
494 * guest (but not the host).
496 * This is less of an issue for MMIO stores since they aren't
497 * globally visible. It could be an issue for MMIO loads to
498 * a certain extent but we'll ignore it for now.
501 vcpu->arch.paddr_accessed = gpa;
502 vcpu->arch.vaddr_accessed = ea;
503 return kvmppc_emulate_mmio(vcpu);
506 int kvmppc_book3s_hv_page_fault(struct kvm_vcpu *vcpu,
507 unsigned long ea, unsigned long dsisr)
509 struct kvm *kvm = vcpu->kvm;
510 unsigned long hpte[3], r;
511 unsigned long hnow_v, hnow_r;
513 unsigned long mmu_seq, psize, pte_size;
514 unsigned long gpa_base, gfn_base;
515 unsigned long gpa, gfn, hva, pfn, hpa;
516 struct kvm_memory_slot *memslot;
518 struct revmap_entry *rev;
522 bool writing, write_ok;
524 unsigned long rcbits;
528 if (kvm_is_radix(kvm))
529 return kvmppc_book3s_radix_page_fault(vcpu, ea, dsisr);
532 * Real-mode code has already searched the HPT and found the
533 * entry we're interested in. Lock the entry and check that
534 * it hasn't changed. If it has, just return and re-execute the
537 if (ea != vcpu->arch.pgfault_addr)
540 if (vcpu->arch.pgfault_cache) {
541 mmio_update = atomic64_read(&kvm->arch.mmio_update);
542 if (mmio_update == vcpu->arch.pgfault_cache->mmio_update) {
543 r = vcpu->arch.pgfault_cache->rpte;
544 psize = kvmppc_actual_pgsz(vcpu->arch.pgfault_hpte[0],
546 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
547 gfn_base = gpa_base >> PAGE_SHIFT;
548 gpa = gpa_base | (ea & (psize - 1));
549 return kvmppc_hv_emulate_mmio(vcpu, gpa, ea,
550 dsisr & DSISR_ISSTORE);
553 index = vcpu->arch.pgfault_index;
554 hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
555 rev = &kvm->arch.hpt.rev[index];
557 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
559 hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
560 hpte[1] = be64_to_cpu(hptep[1]);
561 hpte[2] = r = rev->guest_rpte;
562 unlock_hpte(hptep, hpte[0]);
565 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
566 hpte[0] = hpte_new_to_old_v(hpte[0], hpte[1]);
567 hpte[1] = hpte_new_to_old_r(hpte[1]);
569 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
570 hpte[1] != vcpu->arch.pgfault_hpte[1])
573 /* Translate the logical address and get the page */
574 psize = kvmppc_actual_pgsz(hpte[0], r);
575 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
576 gfn_base = gpa_base >> PAGE_SHIFT;
577 gpa = gpa_base | (ea & (psize - 1));
578 gfn = gpa >> PAGE_SHIFT;
579 memslot = gfn_to_memslot(kvm, gfn);
581 trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr);
583 /* No memslot means it's an emulated MMIO region */
584 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
585 return kvmppc_hv_emulate_mmio(vcpu, gpa, ea,
586 dsisr & DSISR_ISSTORE);
589 * This should never happen, because of the slot_is_aligned()
590 * check in kvmppc_do_h_enter().
592 if (gfn_base < memslot->base_gfn)
595 /* used to check for invalidations in progress */
596 mmu_seq = kvm->mmu_invalidate_seq;
601 writing = (dsisr & DSISR_ISSTORE) != 0;
602 /* If writing != 0, then the HPTE must allow writing, if we get here */
604 hva = gfn_to_hva_memslot(memslot, gfn);
607 * Do a fast check first, since __gfn_to_pfn_memslot doesn't
608 * do it with !atomic && !async, which is how we call it.
609 * We always ask for write permission since the common case
610 * is that the page is writable.
612 if (get_user_page_fast_only(hva, FOLL_WRITE, &page)) {
615 /* Call KVM generic code to do the slow-path check */
616 pfn = __gfn_to_pfn_memslot(memslot, gfn, false, false, NULL,
617 writing, &write_ok, NULL);
618 if (is_error_noslot_pfn(pfn))
621 if (pfn_valid(pfn)) {
622 page = pfn_to_page(pfn);
623 if (PageReserved(page))
629 * Read the PTE from the process' radix tree and use that
630 * so we get the shift and attribute bits.
632 spin_lock(&kvm->mmu_lock);
633 ptep = find_kvm_host_pte(kvm, mmu_seq, hva, &shift);
636 pte = READ_ONCE(*ptep);
637 spin_unlock(&kvm->mmu_lock);
639 * If the PTE disappeared temporarily due to a THP
640 * collapse, just return and let the guest try again.
642 if (!pte_present(pte)) {
647 hpa = pte_pfn(pte) << PAGE_SHIFT;
648 pte_size = PAGE_SIZE;
650 pte_size = 1ul << shift;
653 if (psize > pte_size)
655 if (pte_size > psize)
656 hpa |= hva & (pte_size - psize);
658 /* Check WIMG vs. the actual page we're accessing */
659 if (!hpte_cache_flags_ok(r, is_ci)) {
663 * Allow guest to map emulated device memory as
664 * uncacheable, but actually make it cacheable.
666 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
670 * Set the HPTE to point to hpa.
671 * Since the hpa is at PAGE_SIZE granularity, make sure we
672 * don't mask out lower-order bits if psize < PAGE_SIZE.
674 if (psize < PAGE_SIZE)
676 r = (r & HPTE_R_KEY_HI) | (r & ~(HPTE_R_PP0 - psize)) | hpa;
677 if (hpte_is_writable(r) && !write_ok)
678 r = hpte_make_readonly(r);
681 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
683 hnow_v = be64_to_cpu(hptep[0]);
684 hnow_r = be64_to_cpu(hptep[1]);
685 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
686 hnow_v = hpte_new_to_old_v(hnow_v, hnow_r);
687 hnow_r = hpte_new_to_old_r(hnow_r);
691 * If the HPT is being resized, don't update the HPTE,
692 * instead let the guest retry after the resize operation is complete.
693 * The synchronization for mmu_ready test vs. set is provided
696 if (!kvm->arch.mmu_ready)
699 if ((hnow_v & ~HPTE_V_HVLOCK) != hpte[0] || hnow_r != hpte[1] ||
700 rev->guest_rpte != hpte[2])
701 /* HPTE has been changed under us; let the guest retry */
703 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
705 /* Always put the HPTE in the rmap chain for the page base address */
706 rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn];
709 /* Check if we might have been invalidated; let the guest retry if so */
711 if (mmu_invalidate_retry(vcpu->kvm, mmu_seq)) {
716 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
717 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
718 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
720 if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) {
721 /* HPTE was previously valid, so we need to invalidate it */
723 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
724 kvmppc_invalidate_hpte(kvm, hptep, index);
725 /* don't lose previous R and C bits */
726 r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
728 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
731 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
732 r = hpte_old_to_new_r(hpte[0], r);
733 hpte[0] = hpte_old_to_new_v(hpte[0]);
735 hptep[1] = cpu_to_be64(r);
737 __unlock_hpte(hptep, hpte[0]);
738 asm volatile("ptesync" : : : "memory");
740 if (page && hpte_is_writable(r))
741 set_page_dirty_lock(page);
744 trace_kvm_page_fault_exit(vcpu, hpte, ret);
751 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
756 void kvmppc_rmap_reset(struct kvm *kvm)
758 struct kvm_memslots *slots;
759 struct kvm_memory_slot *memslot;
762 srcu_idx = srcu_read_lock(&kvm->srcu);
763 slots = kvm_memslots(kvm);
764 kvm_for_each_memslot(memslot, bkt, slots) {
765 /* Mutual exclusion with kvm_unmap_hva_range etc. */
766 spin_lock(&kvm->mmu_lock);
768 * This assumes it is acceptable to lose reference and
769 * change bits across a reset.
771 memset(memslot->arch.rmap, 0,
772 memslot->npages * sizeof(*memslot->arch.rmap));
773 spin_unlock(&kvm->mmu_lock);
775 srcu_read_unlock(&kvm->srcu, srcu_idx);
778 /* Must be called with both HPTE and rmap locked */
779 static void kvmppc_unmap_hpte(struct kvm *kvm, unsigned long i,
780 struct kvm_memory_slot *memslot,
781 unsigned long *rmapp, unsigned long gfn)
783 __be64 *hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
784 struct revmap_entry *rev = kvm->arch.hpt.rev;
786 unsigned long ptel, psize, rcbits;
790 /* chain is now empty */
791 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
793 /* remove i from chain */
797 rev[i].forw = rev[i].back = i;
798 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
801 /* Now check and modify the HPTE */
802 ptel = rev[i].guest_rpte;
803 psize = kvmppc_actual_pgsz(be64_to_cpu(hptep[0]), ptel);
804 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
805 hpte_rpn(ptel, psize) == gfn) {
806 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
807 kvmppc_invalidate_hpte(kvm, hptep, i);
808 hptep[1] &= ~cpu_to_be64(HPTE_R_KEY_HI | HPTE_R_KEY_LO);
809 /* Harvest R and C */
810 rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
811 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
812 if ((rcbits & HPTE_R_C) && memslot->dirty_bitmap)
813 kvmppc_update_dirty_map(memslot, gfn, psize);
814 if (rcbits & ~rev[i].guest_rpte) {
815 rev[i].guest_rpte = ptel | rcbits;
816 note_hpte_modification(kvm, &rev[i]);
821 static void kvm_unmap_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
826 unsigned long *rmapp;
828 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
831 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
837 * To avoid an ABBA deadlock with the HPTE lock bit,
838 * we can't spin on the HPTE lock while holding the
841 i = *rmapp & KVMPPC_RMAP_INDEX;
842 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
843 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
844 /* unlock rmap before spinning on the HPTE lock */
846 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
851 kvmppc_unmap_hpte(kvm, i, memslot, rmapp, gfn);
853 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
857 bool kvm_unmap_gfn_range_hv(struct kvm *kvm, struct kvm_gfn_range *range)
861 if (kvm_is_radix(kvm)) {
862 for (gfn = range->start; gfn < range->end; gfn++)
863 kvm_unmap_radix(kvm, range->slot, gfn);
865 for (gfn = range->start; gfn < range->end; gfn++)
866 kvm_unmap_rmapp(kvm, range->slot, gfn);
872 void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
873 struct kvm_memory_slot *memslot)
877 unsigned long *rmapp;
879 gfn = memslot->base_gfn;
880 rmapp = memslot->arch.rmap;
881 if (kvm_is_radix(kvm)) {
882 kvmppc_radix_flush_memslot(kvm, memslot);
886 for (n = memslot->npages; n; --n, ++gfn) {
888 * Testing the present bit without locking is OK because
889 * the memslot has been marked invalid already, and hence
890 * no new HPTEs referencing this page can be created,
891 * thus the present bit can't go from 0 to 1.
893 if (*rmapp & KVMPPC_RMAP_PRESENT)
894 kvm_unmap_rmapp(kvm, memslot, gfn);
899 static bool kvm_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
902 struct revmap_entry *rev = kvm->arch.hpt.rev;
903 unsigned long head, i, j;
906 unsigned long *rmapp;
908 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
911 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
912 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
915 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
920 i = head = *rmapp & KVMPPC_RMAP_INDEX;
922 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
925 /* If this HPTE isn't referenced, ignore it */
926 if (!(be64_to_cpu(hptep[1]) & HPTE_R_R))
929 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
930 /* unlock rmap before spinning on the HPTE lock */
932 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
937 /* Now check and modify the HPTE */
938 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
939 (be64_to_cpu(hptep[1]) & HPTE_R_R)) {
940 kvmppc_clear_ref_hpte(kvm, hptep, i);
941 if (!(rev[i].guest_rpte & HPTE_R_R)) {
942 rev[i].guest_rpte |= HPTE_R_R;
943 note_hpte_modification(kvm, &rev[i]);
947 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
948 } while ((i = j) != head);
954 bool kvm_age_gfn_hv(struct kvm *kvm, struct kvm_gfn_range *range)
959 if (kvm_is_radix(kvm)) {
960 for (gfn = range->start; gfn < range->end; gfn++)
961 ret |= kvm_age_radix(kvm, range->slot, gfn);
963 for (gfn = range->start; gfn < range->end; gfn++)
964 ret |= kvm_age_rmapp(kvm, range->slot, gfn);
970 static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
973 struct revmap_entry *rev = kvm->arch.hpt.rev;
974 unsigned long head, i, j;
977 unsigned long *rmapp;
979 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
980 if (*rmapp & KVMPPC_RMAP_REFERENCED)
984 if (*rmapp & KVMPPC_RMAP_REFERENCED)
987 if (*rmapp & KVMPPC_RMAP_PRESENT) {
988 i = head = *rmapp & KVMPPC_RMAP_INDEX;
990 hp = (unsigned long *)(kvm->arch.hpt.virt + (i << 4));
992 if (be64_to_cpu(hp[1]) & HPTE_R_R)
994 } while ((i = j) != head);
1003 bool kvm_test_age_gfn_hv(struct kvm *kvm, struct kvm_gfn_range *range)
1005 WARN_ON(range->start + 1 != range->end);
1007 if (kvm_is_radix(kvm))
1008 return kvm_test_age_radix(kvm, range->slot, range->start);
1010 return kvm_test_age_rmapp(kvm, range->slot, range->start);
1013 bool kvm_set_spte_gfn_hv(struct kvm *kvm, struct kvm_gfn_range *range)
1015 WARN_ON(range->start + 1 != range->end);
1017 if (kvm_is_radix(kvm))
1018 kvm_unmap_radix(kvm, range->slot, range->start);
1020 kvm_unmap_rmapp(kvm, range->slot, range->start);
1025 static int vcpus_running(struct kvm *kvm)
1027 return atomic_read(&kvm->arch.vcpus_running) != 0;
1031 * Returns the number of system pages that are dirty.
1032 * This can be more than 1 if we find a huge-page HPTE.
1034 static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp)
1036 struct revmap_entry *rev = kvm->arch.hpt.rev;
1037 unsigned long head, i, j;
1041 int npages_dirty = 0;
1045 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
1047 return npages_dirty;
1050 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1052 unsigned long hptep1;
1053 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
1057 * Checking the C (changed) bit here is racy since there
1058 * is no guarantee about when the hardware writes it back.
1059 * If the HPTE is not writable then it is stable since the
1060 * page can't be written to, and we would have done a tlbie
1061 * (which forces the hardware to complete any writeback)
1062 * when making the HPTE read-only.
1063 * If vcpus are running then this call is racy anyway
1064 * since the page could get dirtied subsequently, so we
1065 * expect there to be a further call which would pick up
1066 * any delayed C bit writeback.
1067 * Otherwise we need to do the tlbie even if C==0 in
1068 * order to pick up any delayed writeback of C.
1070 hptep1 = be64_to_cpu(hptep[1]);
1071 if (!(hptep1 & HPTE_R_C) &&
1072 (!hpte_is_writable(hptep1) || vcpus_running(kvm)))
1075 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
1076 /* unlock rmap before spinning on the HPTE lock */
1078 while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK))
1083 /* Now check and modify the HPTE */
1084 if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) {
1085 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
1089 /* need to make it temporarily absent so C is stable */
1090 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
1091 kvmppc_invalidate_hpte(kvm, hptep, i);
1092 v = be64_to_cpu(hptep[0]);
1093 r = be64_to_cpu(hptep[1]);
1095 hptep[1] = cpu_to_be64(r & ~HPTE_R_C);
1096 if (!(rev[i].guest_rpte & HPTE_R_C)) {
1097 rev[i].guest_rpte |= HPTE_R_C;
1098 note_hpte_modification(kvm, &rev[i]);
1100 n = kvmppc_actual_pgsz(v, r);
1101 n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT;
1102 if (n > npages_dirty)
1106 v &= ~HPTE_V_ABSENT;
1108 __unlock_hpte(hptep, v);
1109 } while ((i = j) != head);
1112 return npages_dirty;
1115 void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa *vpa,
1116 struct kvm_memory_slot *memslot,
1121 if (!vpa->dirty || !vpa->pinned_addr)
1123 gfn = vpa->gpa >> PAGE_SHIFT;
1124 if (gfn < memslot->base_gfn ||
1125 gfn >= memslot->base_gfn + memslot->npages)
1130 __set_bit_le(gfn - memslot->base_gfn, map);
1133 long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm,
1134 struct kvm_memory_slot *memslot, unsigned long *map)
1137 unsigned long *rmapp;
1140 rmapp = memslot->arch.rmap;
1141 for (i = 0; i < memslot->npages; ++i) {
1142 int npages = kvm_test_clear_dirty_npages(kvm, rmapp);
1144 * Note that if npages > 0 then i must be a multiple of npages,
1145 * since we always put huge-page HPTEs in the rmap chain
1146 * corresponding to their page base address.
1149 set_dirty_bits(map, i, npages);
1156 void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
1157 unsigned long *nb_ret)
1159 struct kvm_memory_slot *memslot;
1160 unsigned long gfn = gpa >> PAGE_SHIFT;
1161 struct page *page, *pages[1];
1163 unsigned long hva, offset;
1166 srcu_idx = srcu_read_lock(&kvm->srcu);
1167 memslot = gfn_to_memslot(kvm, gfn);
1168 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
1170 hva = gfn_to_hva_memslot(memslot, gfn);
1171 npages = get_user_pages_fast(hva, 1, FOLL_WRITE, pages);
1175 srcu_read_unlock(&kvm->srcu, srcu_idx);
1177 offset = gpa & (PAGE_SIZE - 1);
1179 *nb_ret = PAGE_SIZE - offset;
1180 return page_address(page) + offset;
1183 srcu_read_unlock(&kvm->srcu, srcu_idx);
1187 void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
1190 struct page *page = virt_to_page(va);
1191 struct kvm_memory_slot *memslot;
1200 /* We need to mark this page dirty in the memslot dirty_bitmap, if any */
1201 gfn = gpa >> PAGE_SHIFT;
1202 srcu_idx = srcu_read_lock(&kvm->srcu);
1203 memslot = gfn_to_memslot(kvm, gfn);
1204 if (memslot && memslot->dirty_bitmap)
1205 set_bit_le(gfn - memslot->base_gfn, memslot->dirty_bitmap);
1206 srcu_read_unlock(&kvm->srcu, srcu_idx);
1212 static int resize_hpt_allocate(struct kvm_resize_hpt *resize)
1216 rc = kvmppc_allocate_hpt(&resize->hpt, resize->order);
1220 resize_hpt_debug(resize, "%s(): HPT @ 0x%lx\n", __func__,
1226 static unsigned long resize_hpt_rehash_hpte(struct kvm_resize_hpt *resize,
1229 struct kvm *kvm = resize->kvm;
1230 struct kvm_hpt_info *old = &kvm->arch.hpt;
1231 struct kvm_hpt_info *new = &resize->hpt;
1232 unsigned long old_hash_mask = (1ULL << (old->order - 7)) - 1;
1233 unsigned long new_hash_mask = (1ULL << (new->order - 7)) - 1;
1234 __be64 *hptep, *new_hptep;
1235 unsigned long vpte, rpte, guest_rpte;
1237 struct revmap_entry *rev;
1238 unsigned long apsize, avpn, pteg, hash;
1239 unsigned long new_idx, new_pteg, replace_vpte;
1242 hptep = (__be64 *)(old->virt + (idx << 4));
1244 /* Guest is stopped, so new HPTEs can't be added or faulted
1245 * in, only unmapped or altered by host actions. So, it's
1246 * safe to check this before we take the HPTE lock */
1247 vpte = be64_to_cpu(hptep[0]);
1248 if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
1249 return 0; /* nothing to do */
1251 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
1254 vpte = be64_to_cpu(hptep[0]);
1257 if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
1261 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1262 rpte = be64_to_cpu(hptep[1]);
1263 vpte = hpte_new_to_old_v(vpte, rpte);
1267 rev = &old->rev[idx];
1268 guest_rpte = rev->guest_rpte;
1271 apsize = kvmppc_actual_pgsz(vpte, guest_rpte);
1275 if (vpte & HPTE_V_VALID) {
1276 unsigned long gfn = hpte_rpn(guest_rpte, apsize);
1277 int srcu_idx = srcu_read_lock(&kvm->srcu);
1278 struct kvm_memory_slot *memslot =
1279 __gfn_to_memslot(kvm_memslots(kvm), gfn);
1282 unsigned long *rmapp;
1283 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1286 kvmppc_unmap_hpte(kvm, idx, memslot, rmapp, gfn);
1290 srcu_read_unlock(&kvm->srcu, srcu_idx);
1293 /* Reload PTE after unmap */
1294 vpte = be64_to_cpu(hptep[0]);
1295 BUG_ON(vpte & HPTE_V_VALID);
1296 BUG_ON(!(vpte & HPTE_V_ABSENT));
1299 if (!(vpte & HPTE_V_BOLTED))
1302 rpte = be64_to_cpu(hptep[1]);
1304 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1305 vpte = hpte_new_to_old_v(vpte, rpte);
1306 rpte = hpte_new_to_old_r(rpte);
1309 pshift = kvmppc_hpte_base_page_shift(vpte, rpte);
1310 avpn = HPTE_V_AVPN_VAL(vpte) & ~(((1ul << pshift) - 1) >> 23);
1311 pteg = idx / HPTES_PER_GROUP;
1312 if (vpte & HPTE_V_SECONDARY)
1315 if (!(vpte & HPTE_V_1TB_SEG)) {
1316 unsigned long offset, vsid;
1318 /* We only have 28 - 23 bits of offset in avpn */
1319 offset = (avpn & 0x1f) << 23;
1321 /* We can find more bits from the pteg value */
1323 offset |= ((vsid ^ pteg) & old_hash_mask) << pshift;
1325 hash = vsid ^ (offset >> pshift);
1327 unsigned long offset, vsid;
1329 /* We only have 40 - 23 bits of seg_off in avpn */
1330 offset = (avpn & 0x1ffff) << 23;
1333 offset |= ((vsid ^ (vsid << 25) ^ pteg) & old_hash_mask) << pshift;
1335 hash = vsid ^ (vsid << 25) ^ (offset >> pshift);
1338 new_pteg = hash & new_hash_mask;
1339 if (vpte & HPTE_V_SECONDARY)
1340 new_pteg = ~hash & new_hash_mask;
1342 new_idx = new_pteg * HPTES_PER_GROUP + (idx % HPTES_PER_GROUP);
1343 new_hptep = (__be64 *)(new->virt + (new_idx << 4));
1345 replace_vpte = be64_to_cpu(new_hptep[0]);
1346 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1347 unsigned long replace_rpte = be64_to_cpu(new_hptep[1]);
1348 replace_vpte = hpte_new_to_old_v(replace_vpte, replace_rpte);
1351 if (replace_vpte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
1352 BUG_ON(new->order >= old->order);
1354 if (replace_vpte & HPTE_V_BOLTED) {
1355 if (vpte & HPTE_V_BOLTED)
1356 /* Bolted collision, nothing we can do */
1358 /* Discard the new HPTE */
1362 /* Discard the previous HPTE */
1365 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1366 rpte = hpte_old_to_new_r(vpte, rpte);
1367 vpte = hpte_old_to_new_v(vpte);
1370 new_hptep[1] = cpu_to_be64(rpte);
1371 new->rev[new_idx].guest_rpte = guest_rpte;
1372 /* No need for a barrier, since new HPT isn't active */
1373 new_hptep[0] = cpu_to_be64(vpte);
1374 unlock_hpte(new_hptep, vpte);
1377 unlock_hpte(hptep, vpte);
1381 static int resize_hpt_rehash(struct kvm_resize_hpt *resize)
1383 struct kvm *kvm = resize->kvm;
1387 for (i = 0; i < kvmppc_hpt_npte(&kvm->arch.hpt); i++) {
1388 rc = resize_hpt_rehash_hpte(resize, i);
1396 static void resize_hpt_pivot(struct kvm_resize_hpt *resize)
1398 struct kvm *kvm = resize->kvm;
1399 struct kvm_hpt_info hpt_tmp;
1401 /* Exchange the pending tables in the resize structure with
1402 * the active tables */
1404 resize_hpt_debug(resize, "resize_hpt_pivot()\n");
1406 spin_lock(&kvm->mmu_lock);
1407 asm volatile("ptesync" : : : "memory");
1409 hpt_tmp = kvm->arch.hpt;
1410 kvmppc_set_hpt(kvm, &resize->hpt);
1411 resize->hpt = hpt_tmp;
1413 spin_unlock(&kvm->mmu_lock);
1415 synchronize_srcu_expedited(&kvm->srcu);
1417 if (cpu_has_feature(CPU_FTR_ARCH_300))
1418 kvmppc_setup_partition_table(kvm);
1420 resize_hpt_debug(resize, "resize_hpt_pivot() done\n");
1423 static void resize_hpt_release(struct kvm *kvm, struct kvm_resize_hpt *resize)
1425 if (WARN_ON(!mutex_is_locked(&kvm->arch.mmu_setup_lock)))
1431 if (resize->error != -EBUSY) {
1432 if (resize->hpt.virt)
1433 kvmppc_free_hpt(&resize->hpt);
1437 if (kvm->arch.resize_hpt == resize)
1438 kvm->arch.resize_hpt = NULL;
1441 static void resize_hpt_prepare_work(struct work_struct *work)
1443 struct kvm_resize_hpt *resize = container_of(work,
1444 struct kvm_resize_hpt,
1446 struct kvm *kvm = resize->kvm;
1449 if (WARN_ON(resize->error != -EBUSY))
1452 mutex_lock(&kvm->arch.mmu_setup_lock);
1454 /* Request is still current? */
1455 if (kvm->arch.resize_hpt == resize) {
1456 /* We may request large allocations here:
1457 * do not sleep with kvm->arch.mmu_setup_lock held for a while.
1459 mutex_unlock(&kvm->arch.mmu_setup_lock);
1461 resize_hpt_debug(resize, "%s(): order = %d\n", __func__,
1464 err = resize_hpt_allocate(resize);
1466 /* We have strict assumption about -EBUSY
1467 * when preparing for HPT resize.
1469 if (WARN_ON(err == -EBUSY))
1472 mutex_lock(&kvm->arch.mmu_setup_lock);
1473 /* It is possible that kvm->arch.resize_hpt != resize
1474 * after we grab kvm->arch.mmu_setup_lock again.
1478 resize->error = err;
1480 if (kvm->arch.resize_hpt != resize)
1481 resize_hpt_release(kvm, resize);
1483 mutex_unlock(&kvm->arch.mmu_setup_lock);
1486 int kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
1487 struct kvm_ppc_resize_hpt *rhpt)
1489 unsigned long flags = rhpt->flags;
1490 unsigned long shift = rhpt->shift;
1491 struct kvm_resize_hpt *resize;
1494 if (flags != 0 || kvm_is_radix(kvm))
1497 if (shift && ((shift < 18) || (shift > 46)))
1500 mutex_lock(&kvm->arch.mmu_setup_lock);
1502 resize = kvm->arch.resize_hpt;
1505 if (resize->order == shift) {
1506 /* Suitable resize in progress? */
1507 ret = resize->error;
1509 ret = 100; /* estimated time in ms */
1511 resize_hpt_release(kvm, resize);
1516 /* not suitable, cancel it */
1517 resize_hpt_release(kvm, resize);
1522 goto out; /* nothing to do */
1524 /* start new resize */
1526 resize = kzalloc(sizeof(*resize), GFP_KERNEL);
1532 resize->error = -EBUSY;
1533 resize->order = shift;
1535 INIT_WORK(&resize->work, resize_hpt_prepare_work);
1536 kvm->arch.resize_hpt = resize;
1538 schedule_work(&resize->work);
1540 ret = 100; /* estimated time in ms */
1543 mutex_unlock(&kvm->arch.mmu_setup_lock);
1547 static void resize_hpt_boot_vcpu(void *opaque)
1549 /* Nothing to do, just force a KVM exit */
1552 int kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
1553 struct kvm_ppc_resize_hpt *rhpt)
1555 unsigned long flags = rhpt->flags;
1556 unsigned long shift = rhpt->shift;
1557 struct kvm_resize_hpt *resize;
1560 if (flags != 0 || kvm_is_radix(kvm))
1563 if (shift && ((shift < 18) || (shift > 46)))
1566 mutex_lock(&kvm->arch.mmu_setup_lock);
1568 resize = kvm->arch.resize_hpt;
1570 /* This shouldn't be possible */
1572 if (WARN_ON(!kvm->arch.mmu_ready))
1575 /* Stop VCPUs from running while we mess with the HPT */
1576 kvm->arch.mmu_ready = 0;
1579 /* Boot all CPUs out of the guest so they re-read
1581 on_each_cpu(resize_hpt_boot_vcpu, NULL, 1);
1584 if (!resize || (resize->order != shift))
1587 ret = resize->error;
1591 ret = resize_hpt_rehash(resize);
1595 resize_hpt_pivot(resize);
1598 /* Let VCPUs run again */
1599 kvm->arch.mmu_ready = 1;
1602 resize_hpt_release(kvm, resize);
1603 mutex_unlock(&kvm->arch.mmu_setup_lock);
1608 * Functions for reading and writing the hash table via reads and
1609 * writes on a file descriptor.
1611 * Reads return the guest view of the hash table, which has to be
1612 * pieced together from the real hash table and the guest_rpte
1613 * values in the revmap array.
1615 * On writes, each HPTE written is considered in turn, and if it
1616 * is valid, it is written to the HPT as if an H_ENTER with the
1617 * exact flag set was done. When the invalid count is non-zero
1618 * in the header written to the stream, the kernel will make
1619 * sure that that many HPTEs are invalid, and invalidate them
1623 struct kvm_htab_ctx {
1624 unsigned long index;
1625 unsigned long flags;
1630 #define HPTE_SIZE (2 * sizeof(unsigned long))
1633 * Returns 1 if this HPT entry has been modified or has pending
1636 static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp)
1638 unsigned long rcbits_unset;
1640 if (revp->guest_rpte & HPTE_GR_MODIFIED)
1643 /* Also need to consider changes in reference and changed bits */
1644 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1645 if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) &&
1646 (be64_to_cpu(hptp[1]) & rcbits_unset))
1652 static long record_hpte(unsigned long flags, __be64 *hptp,
1653 unsigned long *hpte, struct revmap_entry *revp,
1654 int want_valid, int first_pass)
1656 unsigned long v, r, hr;
1657 unsigned long rcbits_unset;
1661 /* Unmodified entries are uninteresting except on the first pass */
1662 dirty = hpte_dirty(revp, hptp);
1663 if (!first_pass && !dirty)
1667 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) {
1669 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
1670 !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED))
1673 if (valid != want_valid)
1677 if (valid || dirty) {
1678 /* lock the HPTE so it's stable and read it */
1680 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
1682 v = be64_to_cpu(hptp[0]);
1683 hr = be64_to_cpu(hptp[1]);
1684 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1685 v = hpte_new_to_old_v(v, hr);
1686 hr = hpte_new_to_old_r(hr);
1689 /* re-evaluate valid and dirty from synchronized HPTE value */
1690 valid = !!(v & HPTE_V_VALID);
1691 dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
1693 /* Harvest R and C into guest view if necessary */
1694 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1695 if (valid && (rcbits_unset & hr)) {
1696 revp->guest_rpte |= (hr &
1697 (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED;
1701 if (v & HPTE_V_ABSENT) {
1702 v &= ~HPTE_V_ABSENT;
1706 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
1709 r = revp->guest_rpte;
1710 /* only clear modified if this is the right sort of entry */
1711 if (valid == want_valid && dirty) {
1712 r &= ~HPTE_GR_MODIFIED;
1713 revp->guest_rpte = r;
1715 unlock_hpte(hptp, be64_to_cpu(hptp[0]));
1717 if (!(valid == want_valid && (first_pass || dirty)))
1720 hpte[0] = cpu_to_be64(v);
1721 hpte[1] = cpu_to_be64(r);
1725 static ssize_t kvm_htab_read(struct file *file, char __user *buf,
1726 size_t count, loff_t *ppos)
1728 struct kvm_htab_ctx *ctx = file->private_data;
1729 struct kvm *kvm = ctx->kvm;
1730 struct kvm_get_htab_header hdr;
1732 struct revmap_entry *revp;
1733 unsigned long i, nb, nw;
1734 unsigned long __user *lbuf;
1735 struct kvm_get_htab_header __user *hptr;
1736 unsigned long flags;
1738 unsigned long hpte[2];
1740 if (!access_ok(buf, count))
1742 if (kvm_is_radix(kvm))
1745 first_pass = ctx->first_pass;
1749 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
1750 revp = kvm->arch.hpt.rev + i;
1751 lbuf = (unsigned long __user *)buf;
1754 while (nb + sizeof(hdr) + HPTE_SIZE < count) {
1755 /* Initialize header */
1756 hptr = (struct kvm_get_htab_header __user *)buf;
1761 lbuf = (unsigned long __user *)(buf + sizeof(hdr));
1763 /* Skip uninteresting entries, i.e. clean on not-first pass */
1765 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
1766 !hpte_dirty(revp, hptp)) {
1774 /* Grab a series of valid entries */
1775 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
1776 hdr.n_valid < 0xffff &&
1777 nb + HPTE_SIZE < count &&
1778 record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
1779 /* valid entry, write it out */
1781 if (__put_user(hpte[0], lbuf) ||
1782 __put_user(hpte[1], lbuf + 1))
1790 /* Now skip invalid entries while we can */
1791 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
1792 hdr.n_invalid < 0xffff &&
1793 record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
1794 /* found an invalid entry */
1801 if (hdr.n_valid || hdr.n_invalid) {
1802 /* write back the header */
1803 if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
1806 buf = (char __user *)lbuf;
1811 /* Check if we've wrapped around the hash table */
1812 if (i >= kvmppc_hpt_npte(&kvm->arch.hpt)) {
1814 ctx->first_pass = 0;
1824 static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
1825 size_t count, loff_t *ppos)
1827 struct kvm_htab_ctx *ctx = file->private_data;
1828 struct kvm *kvm = ctx->kvm;
1829 struct kvm_get_htab_header hdr;
1832 unsigned long __user *lbuf;
1834 unsigned long tmp[2];
1840 if (!access_ok(buf, count))
1842 if (kvm_is_radix(kvm))
1845 /* lock out vcpus from running while we're doing this */
1846 mutex_lock(&kvm->arch.mmu_setup_lock);
1847 mmu_ready = kvm->arch.mmu_ready;
1849 kvm->arch.mmu_ready = 0; /* temporarily */
1850 /* order mmu_ready vs. vcpus_running */
1852 if (atomic_read(&kvm->arch.vcpus_running)) {
1853 kvm->arch.mmu_ready = 1;
1854 mutex_unlock(&kvm->arch.mmu_setup_lock);
1860 for (nb = 0; nb + sizeof(hdr) <= count; ) {
1862 if (__copy_from_user(&hdr, buf, sizeof(hdr)))
1866 if (nb + hdr.n_valid * HPTE_SIZE > count)
1874 if (i >= kvmppc_hpt_npte(&kvm->arch.hpt) ||
1875 i + hdr.n_valid + hdr.n_invalid > kvmppc_hpt_npte(&kvm->arch.hpt))
1878 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
1879 lbuf = (unsigned long __user *)buf;
1880 for (j = 0; j < hdr.n_valid; ++j) {
1885 if (__get_user(hpte_v, lbuf) ||
1886 __get_user(hpte_r, lbuf + 1))
1888 v = be64_to_cpu(hpte_v);
1889 r = be64_to_cpu(hpte_r);
1891 if (!(v & HPTE_V_VALID))
1893 pshift = kvmppc_hpte_base_page_shift(v, r);
1899 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
1900 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1902 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
1904 if (ret != H_SUCCESS) {
1905 pr_err("%s ret %ld i=%ld v=%lx r=%lx\n", __func__, ret, i, v, r);
1908 if (!mmu_ready && is_vrma_hpte(v)) {
1909 unsigned long senc, lpcr;
1911 senc = slb_pgsize_encoding(1ul << pshift);
1912 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1913 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1914 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
1915 lpcr = senc << (LPCR_VRMASD_SH - 4);
1916 kvmppc_update_lpcr(kvm, lpcr,
1919 kvmppc_setup_partition_table(kvm);
1927 for (j = 0; j < hdr.n_invalid; ++j) {
1928 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
1929 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1937 /* Order HPTE updates vs. mmu_ready */
1939 kvm->arch.mmu_ready = mmu_ready;
1940 mutex_unlock(&kvm->arch.mmu_setup_lock);
1947 static int kvm_htab_release(struct inode *inode, struct file *filp)
1949 struct kvm_htab_ctx *ctx = filp->private_data;
1951 filp->private_data = NULL;
1952 if (!(ctx->flags & KVM_GET_HTAB_WRITE))
1953 atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
1954 kvm_put_kvm(ctx->kvm);
1959 static const struct file_operations kvm_htab_fops = {
1960 .read = kvm_htab_read,
1961 .write = kvm_htab_write,
1962 .llseek = default_llseek,
1963 .release = kvm_htab_release,
1966 int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
1969 struct kvm_htab_ctx *ctx;
1972 /* reject flags we don't recognize */
1973 if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
1975 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1980 ctx->index = ghf->start_index;
1981 ctx->flags = ghf->flags;
1982 ctx->first_pass = 1;
1984 rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
1985 ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
1988 kvm_put_kvm_no_destroy(kvm);
1992 if (rwflag == O_RDONLY) {
1993 mutex_lock(&kvm->slots_lock);
1994 atomic_inc(&kvm->arch.hpte_mod_interest);
1995 /* make sure kvmppc_do_h_enter etc. see the increment */
1996 synchronize_srcu_expedited(&kvm->srcu);
1997 mutex_unlock(&kvm->slots_lock);
2003 struct debugfs_htab_state {
2006 unsigned long hpt_index;
2012 static int debugfs_htab_open(struct inode *inode, struct file *file)
2014 struct kvm *kvm = inode->i_private;
2015 struct debugfs_htab_state *p;
2017 p = kzalloc(sizeof(*p), GFP_KERNEL);
2023 mutex_init(&p->mutex);
2024 file->private_data = p;
2026 return nonseekable_open(inode, file);
2029 static int debugfs_htab_release(struct inode *inode, struct file *file)
2031 struct debugfs_htab_state *p = file->private_data;
2033 kvm_put_kvm(p->kvm);
2038 static ssize_t debugfs_htab_read(struct file *file, char __user *buf,
2039 size_t len, loff_t *ppos)
2041 struct debugfs_htab_state *p = file->private_data;
2044 unsigned long v, hr, gr;
2049 if (kvm_is_radix(kvm))
2052 ret = mutex_lock_interruptible(&p->mutex);
2056 if (p->chars_left) {
2060 r = copy_to_user(buf, p->buf + p->buf_index, n);
2075 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
2076 for (; len != 0 && i < kvmppc_hpt_npte(&kvm->arch.hpt);
2078 if (!(be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)))
2081 /* lock the HPTE so it's stable and read it */
2083 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
2085 v = be64_to_cpu(hptp[0]) & ~HPTE_V_HVLOCK;
2086 hr = be64_to_cpu(hptp[1]);
2087 gr = kvm->arch.hpt.rev[i].guest_rpte;
2088 unlock_hpte(hptp, v);
2091 if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
2094 n = scnprintf(p->buf, sizeof(p->buf),
2095 "%6lx %.16lx %.16lx %.16lx\n",
2100 r = copy_to_user(buf, p->buf, n);
2116 mutex_unlock(&p->mutex);
2120 static ssize_t debugfs_htab_write(struct file *file, const char __user *buf,
2121 size_t len, loff_t *ppos)
2126 static const struct file_operations debugfs_htab_fops = {
2127 .owner = THIS_MODULE,
2128 .open = debugfs_htab_open,
2129 .release = debugfs_htab_release,
2130 .read = debugfs_htab_read,
2131 .write = debugfs_htab_write,
2132 .llseek = generic_file_llseek,
2135 void kvmppc_mmu_debugfs_init(struct kvm *kvm)
2137 debugfs_create_file("htab", 0400, kvm->debugfs_dentry, kvm,
2138 &debugfs_htab_fops);
2141 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
2143 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
2145 vcpu->arch.slb_nr = 32; /* POWER7/POWER8 */
2147 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
2149 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;