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 long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
279 long pte_index, unsigned long pteh,
280 unsigned long ptel, unsigned long *pte_idx_ret)
284 /* Protect linux PTE lookup from page table destruction */
285 rcu_read_lock_sched(); /* this disables preemption too */
286 ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
287 kvm->mm->pgd, false, pte_idx_ret);
288 rcu_read_unlock_sched();
289 if (ret == H_TOO_HARD) {
290 /* this can't happen */
291 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
292 ret = H_RESOURCE; /* or something */
298 static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
304 for (i = 0; i < vcpu->arch.slb_nr; i++) {
305 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
308 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
313 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
314 return &vcpu->arch.slb[i];
319 static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
322 unsigned long ra_mask;
324 ra_mask = kvmppc_actual_pgsz(v, r) - 1;
325 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
328 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
329 struct kvmppc_pte *gpte, bool data, bool iswrite)
331 struct kvm *kvm = vcpu->kvm;
332 struct kvmppc_slb *slbe;
334 unsigned long pp, key;
335 unsigned long v, orig_v, gr;
338 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
340 if (kvm_is_radix(vcpu->kvm))
341 return kvmppc_mmu_radix_xlate(vcpu, eaddr, gpte, data, iswrite);
345 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
350 /* real mode access */
351 slb_v = vcpu->kvm->arch.vrma_slb_v;
355 /* Find the HPTE in the hash table */
356 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
357 HPTE_V_VALID | HPTE_V_ABSENT);
362 hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
363 v = orig_v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
364 if (cpu_has_feature(CPU_FTR_ARCH_300))
365 v = hpte_new_to_old_v(v, be64_to_cpu(hptep[1]));
366 gr = kvm->arch.hpt.rev[index].guest_rpte;
368 unlock_hpte(hptep, orig_v);
372 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
374 /* Get PP bits and key for permission check */
375 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
376 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
379 /* Calculate permissions */
380 gpte->may_read = hpte_read_permission(pp, key);
381 gpte->may_write = hpte_write_permission(pp, key);
382 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
384 /* Storage key permission check for POWER7 */
385 if (data && virtmode) {
386 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
393 /* Get the guest physical address */
394 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
399 * Quick test for whether an instruction is a load or a store.
400 * If the instruction is a load or a store, then this will indicate
401 * which it is, at least on server processors. (Embedded processors
402 * have some external PID instructions that don't follow the rule
403 * embodied here.) If the instruction isn't a load or store, then
404 * this doesn't return anything useful.
406 static int instruction_is_store(unsigned int instr)
411 if ((instr & 0xfc000000) == 0x7c000000)
412 mask = 0x100; /* major opcode 31 */
413 return (instr & mask) != 0;
416 int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
417 unsigned long gpa, gva_t ea, int is_store)
422 * Fast path - check if the guest physical address corresponds to a
423 * device on the FAST_MMIO_BUS, if so we can avoid loading the
424 * instruction all together, then we can just handle it and return.
429 idx = srcu_read_lock(&vcpu->kvm->srcu);
430 ret = kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, (gpa_t) gpa, 0,
432 srcu_read_unlock(&vcpu->kvm->srcu, idx);
434 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
440 * If we fail, we just return to the guest and try executing it again.
442 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
447 * WARNING: We do not know for sure whether the instruction we just
448 * read from memory is the same that caused the fault in the first
449 * place. If the instruction we read is neither an load or a store,
450 * then it can't access memory, so we don't need to worry about
451 * enforcing access permissions. So, assuming it is a load or
452 * store, we just check that its direction (load or store) is
453 * consistent with the original fault, since that's what we
454 * checked the access permissions against. If there is a mismatch
455 * we just return and retry the instruction.
458 if (instruction_is_store(last_inst) != !!is_store)
462 * Emulated accesses are emulated by looking at the hash for
463 * translation once, then performing the access later. The
464 * translation could be invalidated in the meantime in which
465 * point performing the subsequent memory access on the old
466 * physical address could possibly be a security hole for the
467 * guest (but not the host).
469 * This is less of an issue for MMIO stores since they aren't
470 * globally visible. It could be an issue for MMIO loads to
471 * a certain extent but we'll ignore it for now.
474 vcpu->arch.paddr_accessed = gpa;
475 vcpu->arch.vaddr_accessed = ea;
476 return kvmppc_emulate_mmio(run, vcpu);
479 int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
480 unsigned long ea, unsigned long dsisr)
482 struct kvm *kvm = vcpu->kvm;
483 unsigned long hpte[3], r;
484 unsigned long hnow_v, hnow_r;
486 unsigned long mmu_seq, psize, pte_size;
487 unsigned long gpa_base, gfn_base;
488 unsigned long gpa, gfn, hva, pfn, hpa;
489 struct kvm_memory_slot *memslot;
491 struct revmap_entry *rev;
495 bool writing, write_ok;
497 unsigned long rcbits;
501 if (kvm_is_radix(kvm))
502 return kvmppc_book3s_radix_page_fault(run, vcpu, ea, dsisr);
505 * Real-mode code has already searched the HPT and found the
506 * entry we're interested in. Lock the entry and check that
507 * it hasn't changed. If it has, just return and re-execute the
510 if (ea != vcpu->arch.pgfault_addr)
513 if (vcpu->arch.pgfault_cache) {
514 mmio_update = atomic64_read(&kvm->arch.mmio_update);
515 if (mmio_update == vcpu->arch.pgfault_cache->mmio_update) {
516 r = vcpu->arch.pgfault_cache->rpte;
517 psize = kvmppc_actual_pgsz(vcpu->arch.pgfault_hpte[0],
519 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
520 gfn_base = gpa_base >> PAGE_SHIFT;
521 gpa = gpa_base | (ea & (psize - 1));
522 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
523 dsisr & DSISR_ISSTORE);
526 index = vcpu->arch.pgfault_index;
527 hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
528 rev = &kvm->arch.hpt.rev[index];
530 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
532 hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
533 hpte[1] = be64_to_cpu(hptep[1]);
534 hpte[2] = r = rev->guest_rpte;
535 unlock_hpte(hptep, hpte[0]);
538 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
539 hpte[0] = hpte_new_to_old_v(hpte[0], hpte[1]);
540 hpte[1] = hpte_new_to_old_r(hpte[1]);
542 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
543 hpte[1] != vcpu->arch.pgfault_hpte[1])
546 /* Translate the logical address and get the page */
547 psize = kvmppc_actual_pgsz(hpte[0], r);
548 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
549 gfn_base = gpa_base >> PAGE_SHIFT;
550 gpa = gpa_base | (ea & (psize - 1));
551 gfn = gpa >> PAGE_SHIFT;
552 memslot = gfn_to_memslot(kvm, gfn);
554 trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr);
556 /* No memslot means it's an emulated MMIO region */
557 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
558 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
559 dsisr & DSISR_ISSTORE);
562 * This should never happen, because of the slot_is_aligned()
563 * check in kvmppc_do_h_enter().
565 if (gfn_base < memslot->base_gfn)
568 /* used to check for invalidations in progress */
569 mmu_seq = kvm->mmu_notifier_seq;
574 writing = (dsisr & DSISR_ISSTORE) != 0;
575 /* If writing != 0, then the HPTE must allow writing, if we get here */
577 hva = gfn_to_hva_memslot(memslot, gfn);
580 * Do a fast check first, since __gfn_to_pfn_memslot doesn't
581 * do it with !atomic && !async, which is how we call it.
582 * We always ask for write permission since the common case
583 * is that the page is writable.
585 if (__get_user_pages_fast(hva, 1, 1, &page) == 1) {
588 /* Call KVM generic code to do the slow-path check */
589 pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL,
591 if (is_error_noslot_pfn(pfn))
594 if (pfn_valid(pfn)) {
595 page = pfn_to_page(pfn);
596 if (PageReserved(page))
602 * Read the PTE from the process' radix tree and use that
603 * so we get the shift and attribute bits.
606 ptep = __find_linux_pte(vcpu->arch.pgdir, hva, NULL, &shift);
608 * If the PTE disappeared temporarily due to a THP
609 * collapse, just return and let the guest try again.
619 hpa = pte_pfn(pte) << PAGE_SHIFT;
620 pte_size = PAGE_SIZE;
622 pte_size = 1ul << shift;
625 if (psize > pte_size)
627 if (pte_size > psize)
628 hpa |= hva & (pte_size - psize);
630 /* Check WIMG vs. the actual page we're accessing */
631 if (!hpte_cache_flags_ok(r, is_ci)) {
635 * Allow guest to map emulated device memory as
636 * uncacheable, but actually make it cacheable.
638 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
642 * Set the HPTE to point to hpa.
643 * Since the hpa is at PAGE_SIZE granularity, make sure we
644 * don't mask out lower-order bits if psize < PAGE_SIZE.
646 if (psize < PAGE_SIZE)
648 r = (r & HPTE_R_KEY_HI) | (r & ~(HPTE_R_PP0 - psize)) | hpa;
649 if (hpte_is_writable(r) && !write_ok)
650 r = hpte_make_readonly(r);
653 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
655 hnow_v = be64_to_cpu(hptep[0]);
656 hnow_r = be64_to_cpu(hptep[1]);
657 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
658 hnow_v = hpte_new_to_old_v(hnow_v, hnow_r);
659 hnow_r = hpte_new_to_old_r(hnow_r);
663 * If the HPT is being resized, don't update the HPTE,
664 * instead let the guest retry after the resize operation is complete.
665 * The synchronization for mmu_ready test vs. set is provided
668 if (!kvm->arch.mmu_ready)
671 if ((hnow_v & ~HPTE_V_HVLOCK) != hpte[0] || hnow_r != hpte[1] ||
672 rev->guest_rpte != hpte[2])
673 /* HPTE has been changed under us; let the guest retry */
675 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
677 /* Always put the HPTE in the rmap chain for the page base address */
678 rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn];
681 /* Check if we might have been invalidated; let the guest retry if so */
683 if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
688 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
689 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
690 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
692 if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) {
693 /* HPTE was previously valid, so we need to invalidate it */
695 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
696 kvmppc_invalidate_hpte(kvm, hptep, index);
697 /* don't lose previous R and C bits */
698 r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
700 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
703 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
704 r = hpte_old_to_new_r(hpte[0], r);
705 hpte[0] = hpte_old_to_new_v(hpte[0]);
707 hptep[1] = cpu_to_be64(r);
709 __unlock_hpte(hptep, hpte[0]);
710 asm volatile("ptesync" : : : "memory");
712 if (page && hpte_is_writable(r))
713 set_page_dirty_lock(page);
716 trace_kvm_page_fault_exit(vcpu, hpte, ret);
723 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
728 void kvmppc_rmap_reset(struct kvm *kvm)
730 struct kvm_memslots *slots;
731 struct kvm_memory_slot *memslot;
734 srcu_idx = srcu_read_lock(&kvm->srcu);
735 slots = kvm_memslots(kvm);
736 kvm_for_each_memslot(memslot, slots) {
737 /* Mutual exclusion with kvm_unmap_hva_range etc. */
738 spin_lock(&kvm->mmu_lock);
740 * This assumes it is acceptable to lose reference and
741 * change bits across a reset.
743 memset(memslot->arch.rmap, 0,
744 memslot->npages * sizeof(*memslot->arch.rmap));
745 spin_unlock(&kvm->mmu_lock);
747 srcu_read_unlock(&kvm->srcu, srcu_idx);
750 typedef int (*hva_handler_fn)(struct kvm *kvm, struct kvm_memory_slot *memslot,
753 static int kvm_handle_hva_range(struct kvm *kvm,
756 hva_handler_fn handler)
760 struct kvm_memslots *slots;
761 struct kvm_memory_slot *memslot;
763 slots = kvm_memslots(kvm);
764 kvm_for_each_memslot(memslot, slots) {
765 unsigned long hva_start, hva_end;
768 hva_start = max(start, memslot->userspace_addr);
769 hva_end = min(end, memslot->userspace_addr +
770 (memslot->npages << PAGE_SHIFT));
771 if (hva_start >= hva_end)
774 * {gfn(page) | page intersects with [hva_start, hva_end)} =
775 * {gfn, gfn+1, ..., gfn_end-1}.
777 gfn = hva_to_gfn_memslot(hva_start, memslot);
778 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
780 for (; gfn < gfn_end; ++gfn) {
781 ret = handler(kvm, memslot, gfn);
789 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
790 hva_handler_fn handler)
792 return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
795 /* Must be called with both HPTE and rmap locked */
796 static void kvmppc_unmap_hpte(struct kvm *kvm, unsigned long i,
797 struct kvm_memory_slot *memslot,
798 unsigned long *rmapp, unsigned long gfn)
800 __be64 *hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
801 struct revmap_entry *rev = kvm->arch.hpt.rev;
803 unsigned long ptel, psize, rcbits;
807 /* chain is now empty */
808 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
810 /* remove i from chain */
814 rev[i].forw = rev[i].back = i;
815 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
818 /* Now check and modify the HPTE */
819 ptel = rev[i].guest_rpte;
820 psize = kvmppc_actual_pgsz(be64_to_cpu(hptep[0]), ptel);
821 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
822 hpte_rpn(ptel, psize) == gfn) {
823 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
824 kvmppc_invalidate_hpte(kvm, hptep, i);
825 hptep[1] &= ~cpu_to_be64(HPTE_R_KEY_HI | HPTE_R_KEY_LO);
826 /* Harvest R and C */
827 rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
828 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
829 if ((rcbits & HPTE_R_C) && memslot->dirty_bitmap)
830 kvmppc_update_dirty_map(memslot, gfn, psize);
831 if (rcbits & ~rev[i].guest_rpte) {
832 rev[i].guest_rpte = ptel | rcbits;
833 note_hpte_modification(kvm, &rev[i]);
838 static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
843 unsigned long *rmapp;
845 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
848 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
854 * To avoid an ABBA deadlock with the HPTE lock bit,
855 * we can't spin on the HPTE lock while holding the
858 i = *rmapp & KVMPPC_RMAP_INDEX;
859 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
860 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
861 /* unlock rmap before spinning on the HPTE lock */
863 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
868 kvmppc_unmap_hpte(kvm, i, memslot, rmapp, gfn);
870 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
875 int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start, unsigned long end)
877 hva_handler_fn handler;
879 handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
880 kvm_handle_hva_range(kvm, start, end, handler);
884 void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
885 struct kvm_memory_slot *memslot)
889 unsigned long *rmapp;
891 gfn = memslot->base_gfn;
892 rmapp = memslot->arch.rmap;
893 if (kvm_is_radix(kvm)) {
894 kvmppc_radix_flush_memslot(kvm, memslot);
898 for (n = memslot->npages; n; --n, ++gfn) {
900 * Testing the present bit without locking is OK because
901 * the memslot has been marked invalid already, and hence
902 * no new HPTEs referencing this page can be created,
903 * thus the present bit can't go from 0 to 1.
905 if (*rmapp & KVMPPC_RMAP_PRESENT)
906 kvm_unmap_rmapp(kvm, memslot, gfn);
911 static int kvm_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
914 struct revmap_entry *rev = kvm->arch.hpt.rev;
915 unsigned long head, i, j;
918 unsigned long *rmapp;
920 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
923 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
924 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
927 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
932 i = head = *rmapp & KVMPPC_RMAP_INDEX;
934 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
937 /* If this HPTE isn't referenced, ignore it */
938 if (!(be64_to_cpu(hptep[1]) & HPTE_R_R))
941 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
942 /* unlock rmap before spinning on the HPTE lock */
944 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
949 /* Now check and modify the HPTE */
950 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
951 (be64_to_cpu(hptep[1]) & HPTE_R_R)) {
952 kvmppc_clear_ref_hpte(kvm, hptep, i);
953 if (!(rev[i].guest_rpte & HPTE_R_R)) {
954 rev[i].guest_rpte |= HPTE_R_R;
955 note_hpte_modification(kvm, &rev[i]);
959 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
960 } while ((i = j) != head);
966 int kvm_age_hva_hv(struct kvm *kvm, unsigned long start, unsigned long end)
968 hva_handler_fn handler;
970 handler = kvm_is_radix(kvm) ? kvm_age_radix : kvm_age_rmapp;
971 return kvm_handle_hva_range(kvm, start, end, handler);
974 static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
977 struct revmap_entry *rev = kvm->arch.hpt.rev;
978 unsigned long head, i, j;
981 unsigned long *rmapp;
983 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
984 if (*rmapp & KVMPPC_RMAP_REFERENCED)
988 if (*rmapp & KVMPPC_RMAP_REFERENCED)
991 if (*rmapp & KVMPPC_RMAP_PRESENT) {
992 i = head = *rmapp & KVMPPC_RMAP_INDEX;
994 hp = (unsigned long *)(kvm->arch.hpt.virt + (i << 4));
996 if (be64_to_cpu(hp[1]) & HPTE_R_R)
998 } while ((i = j) != head);
1007 int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva)
1009 hva_handler_fn handler;
1011 handler = kvm_is_radix(kvm) ? kvm_test_age_radix : kvm_test_age_rmapp;
1012 return kvm_handle_hva(kvm, hva, handler);
1015 void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte)
1017 hva_handler_fn handler;
1019 handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
1020 kvm_handle_hva(kvm, hva, handler);
1023 static int vcpus_running(struct kvm *kvm)
1025 return atomic_read(&kvm->arch.vcpus_running) != 0;
1029 * Returns the number of system pages that are dirty.
1030 * This can be more than 1 if we find a huge-page HPTE.
1032 static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp)
1034 struct revmap_entry *rev = kvm->arch.hpt.rev;
1035 unsigned long head, i, j;
1039 int npages_dirty = 0;
1043 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
1045 return npages_dirty;
1048 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1050 unsigned long hptep1;
1051 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
1055 * Checking the C (changed) bit here is racy since there
1056 * is no guarantee about when the hardware writes it back.
1057 * If the HPTE is not writable then it is stable since the
1058 * page can't be written to, and we would have done a tlbie
1059 * (which forces the hardware to complete any writeback)
1060 * when making the HPTE read-only.
1061 * If vcpus are running then this call is racy anyway
1062 * since the page could get dirtied subsequently, so we
1063 * expect there to be a further call which would pick up
1064 * any delayed C bit writeback.
1065 * Otherwise we need to do the tlbie even if C==0 in
1066 * order to pick up any delayed writeback of C.
1068 hptep1 = be64_to_cpu(hptep[1]);
1069 if (!(hptep1 & HPTE_R_C) &&
1070 (!hpte_is_writable(hptep1) || vcpus_running(kvm)))
1073 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
1074 /* unlock rmap before spinning on the HPTE lock */
1076 while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK))
1081 /* Now check and modify the HPTE */
1082 if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) {
1083 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
1087 /* need to make it temporarily absent so C is stable */
1088 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
1089 kvmppc_invalidate_hpte(kvm, hptep, i);
1090 v = be64_to_cpu(hptep[0]);
1091 r = be64_to_cpu(hptep[1]);
1093 hptep[1] = cpu_to_be64(r & ~HPTE_R_C);
1094 if (!(rev[i].guest_rpte & HPTE_R_C)) {
1095 rev[i].guest_rpte |= HPTE_R_C;
1096 note_hpte_modification(kvm, &rev[i]);
1098 n = kvmppc_actual_pgsz(v, r);
1099 n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT;
1100 if (n > npages_dirty)
1104 v &= ~HPTE_V_ABSENT;
1106 __unlock_hpte(hptep, v);
1107 } while ((i = j) != head);
1110 return npages_dirty;
1113 void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa *vpa,
1114 struct kvm_memory_slot *memslot,
1119 if (!vpa->dirty || !vpa->pinned_addr)
1121 gfn = vpa->gpa >> PAGE_SHIFT;
1122 if (gfn < memslot->base_gfn ||
1123 gfn >= memslot->base_gfn + memslot->npages)
1128 __set_bit_le(gfn - memslot->base_gfn, map);
1131 long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm,
1132 struct kvm_memory_slot *memslot, unsigned long *map)
1135 unsigned long *rmapp;
1138 rmapp = memslot->arch.rmap;
1139 for (i = 0; i < memslot->npages; ++i) {
1140 int npages = kvm_test_clear_dirty_npages(kvm, rmapp);
1142 * Note that if npages > 0 then i must be a multiple of npages,
1143 * since we always put huge-page HPTEs in the rmap chain
1144 * corresponding to their page base address.
1147 set_dirty_bits(map, i, npages);
1154 void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
1155 unsigned long *nb_ret)
1157 struct kvm_memory_slot *memslot;
1158 unsigned long gfn = gpa >> PAGE_SHIFT;
1159 struct page *page, *pages[1];
1161 unsigned long hva, offset;
1164 srcu_idx = srcu_read_lock(&kvm->srcu);
1165 memslot = gfn_to_memslot(kvm, gfn);
1166 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
1168 hva = gfn_to_hva_memslot(memslot, gfn);
1169 npages = get_user_pages_fast(hva, 1, FOLL_WRITE, pages);
1173 srcu_read_unlock(&kvm->srcu, srcu_idx);
1175 offset = gpa & (PAGE_SIZE - 1);
1177 *nb_ret = PAGE_SIZE - offset;
1178 return page_address(page) + offset;
1181 srcu_read_unlock(&kvm->srcu, srcu_idx);
1185 void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
1188 struct page *page = virt_to_page(va);
1189 struct kvm_memory_slot *memslot;
1198 /* We need to mark this page dirty in the memslot dirty_bitmap, if any */
1199 gfn = gpa >> PAGE_SHIFT;
1200 srcu_idx = srcu_read_lock(&kvm->srcu);
1201 memslot = gfn_to_memslot(kvm, gfn);
1202 if (memslot && memslot->dirty_bitmap)
1203 set_bit_le(gfn - memslot->base_gfn, memslot->dirty_bitmap);
1204 srcu_read_unlock(&kvm->srcu, srcu_idx);
1210 static int resize_hpt_allocate(struct kvm_resize_hpt *resize)
1214 rc = kvmppc_allocate_hpt(&resize->hpt, resize->order);
1218 resize_hpt_debug(resize, "resize_hpt_allocate(): HPT @ 0x%lx\n",
1224 static unsigned long resize_hpt_rehash_hpte(struct kvm_resize_hpt *resize,
1227 struct kvm *kvm = resize->kvm;
1228 struct kvm_hpt_info *old = &kvm->arch.hpt;
1229 struct kvm_hpt_info *new = &resize->hpt;
1230 unsigned long old_hash_mask = (1ULL << (old->order - 7)) - 1;
1231 unsigned long new_hash_mask = (1ULL << (new->order - 7)) - 1;
1232 __be64 *hptep, *new_hptep;
1233 unsigned long vpte, rpte, guest_rpte;
1235 struct revmap_entry *rev;
1236 unsigned long apsize, avpn, pteg, hash;
1237 unsigned long new_idx, new_pteg, replace_vpte;
1240 hptep = (__be64 *)(old->virt + (idx << 4));
1242 /* Guest is stopped, so new HPTEs can't be added or faulted
1243 * in, only unmapped or altered by host actions. So, it's
1244 * safe to check this before we take the HPTE lock */
1245 vpte = be64_to_cpu(hptep[0]);
1246 if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
1247 return 0; /* nothing to do */
1249 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
1252 vpte = be64_to_cpu(hptep[0]);
1255 if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
1259 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1260 rpte = be64_to_cpu(hptep[1]);
1261 vpte = hpte_new_to_old_v(vpte, rpte);
1265 rev = &old->rev[idx];
1266 guest_rpte = rev->guest_rpte;
1269 apsize = kvmppc_actual_pgsz(vpte, guest_rpte);
1273 if (vpte & HPTE_V_VALID) {
1274 unsigned long gfn = hpte_rpn(guest_rpte, apsize);
1275 int srcu_idx = srcu_read_lock(&kvm->srcu);
1276 struct kvm_memory_slot *memslot =
1277 __gfn_to_memslot(kvm_memslots(kvm), gfn);
1280 unsigned long *rmapp;
1281 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1284 kvmppc_unmap_hpte(kvm, idx, memslot, rmapp, gfn);
1288 srcu_read_unlock(&kvm->srcu, srcu_idx);
1291 /* Reload PTE after unmap */
1292 vpte = be64_to_cpu(hptep[0]);
1293 BUG_ON(vpte & HPTE_V_VALID);
1294 BUG_ON(!(vpte & HPTE_V_ABSENT));
1297 if (!(vpte & HPTE_V_BOLTED))
1300 rpte = be64_to_cpu(hptep[1]);
1302 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1303 vpte = hpte_new_to_old_v(vpte, rpte);
1304 rpte = hpte_new_to_old_r(rpte);
1307 pshift = kvmppc_hpte_base_page_shift(vpte, rpte);
1308 avpn = HPTE_V_AVPN_VAL(vpte) & ~(((1ul << pshift) - 1) >> 23);
1309 pteg = idx / HPTES_PER_GROUP;
1310 if (vpte & HPTE_V_SECONDARY)
1313 if (!(vpte & HPTE_V_1TB_SEG)) {
1314 unsigned long offset, vsid;
1316 /* We only have 28 - 23 bits of offset in avpn */
1317 offset = (avpn & 0x1f) << 23;
1319 /* We can find more bits from the pteg value */
1321 offset |= ((vsid ^ pteg) & old_hash_mask) << pshift;
1323 hash = vsid ^ (offset >> pshift);
1325 unsigned long offset, vsid;
1327 /* We only have 40 - 23 bits of seg_off in avpn */
1328 offset = (avpn & 0x1ffff) << 23;
1331 offset |= ((vsid ^ (vsid << 25) ^ pteg) & old_hash_mask) << pshift;
1333 hash = vsid ^ (vsid << 25) ^ (offset >> pshift);
1336 new_pteg = hash & new_hash_mask;
1337 if (vpte & HPTE_V_SECONDARY)
1338 new_pteg = ~hash & new_hash_mask;
1340 new_idx = new_pteg * HPTES_PER_GROUP + (idx % HPTES_PER_GROUP);
1341 new_hptep = (__be64 *)(new->virt + (new_idx << 4));
1343 replace_vpte = be64_to_cpu(new_hptep[0]);
1344 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1345 unsigned long replace_rpte = be64_to_cpu(new_hptep[1]);
1346 replace_vpte = hpte_new_to_old_v(replace_vpte, replace_rpte);
1349 if (replace_vpte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
1350 BUG_ON(new->order >= old->order);
1352 if (replace_vpte & HPTE_V_BOLTED) {
1353 if (vpte & HPTE_V_BOLTED)
1354 /* Bolted collision, nothing we can do */
1356 /* Discard the new HPTE */
1360 /* Discard the previous HPTE */
1363 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1364 rpte = hpte_old_to_new_r(vpte, rpte);
1365 vpte = hpte_old_to_new_v(vpte);
1368 new_hptep[1] = cpu_to_be64(rpte);
1369 new->rev[new_idx].guest_rpte = guest_rpte;
1370 /* No need for a barrier, since new HPT isn't active */
1371 new_hptep[0] = cpu_to_be64(vpte);
1372 unlock_hpte(new_hptep, vpte);
1375 unlock_hpte(hptep, vpte);
1379 static int resize_hpt_rehash(struct kvm_resize_hpt *resize)
1381 struct kvm *kvm = resize->kvm;
1385 for (i = 0; i < kvmppc_hpt_npte(&kvm->arch.hpt); i++) {
1386 rc = resize_hpt_rehash_hpte(resize, i);
1394 static void resize_hpt_pivot(struct kvm_resize_hpt *resize)
1396 struct kvm *kvm = resize->kvm;
1397 struct kvm_hpt_info hpt_tmp;
1399 /* Exchange the pending tables in the resize structure with
1400 * the active tables */
1402 resize_hpt_debug(resize, "resize_hpt_pivot()\n");
1404 spin_lock(&kvm->mmu_lock);
1405 asm volatile("ptesync" : : : "memory");
1407 hpt_tmp = kvm->arch.hpt;
1408 kvmppc_set_hpt(kvm, &resize->hpt);
1409 resize->hpt = hpt_tmp;
1411 spin_unlock(&kvm->mmu_lock);
1413 synchronize_srcu_expedited(&kvm->srcu);
1415 if (cpu_has_feature(CPU_FTR_ARCH_300))
1416 kvmppc_setup_partition_table(kvm);
1418 resize_hpt_debug(resize, "resize_hpt_pivot() done\n");
1421 static void resize_hpt_release(struct kvm *kvm, struct kvm_resize_hpt *resize)
1423 if (WARN_ON(!mutex_is_locked(&kvm->arch.mmu_setup_lock)))
1429 if (resize->error != -EBUSY) {
1430 if (resize->hpt.virt)
1431 kvmppc_free_hpt(&resize->hpt);
1435 if (kvm->arch.resize_hpt == resize)
1436 kvm->arch.resize_hpt = NULL;
1439 static void resize_hpt_prepare_work(struct work_struct *work)
1441 struct kvm_resize_hpt *resize = container_of(work,
1442 struct kvm_resize_hpt,
1444 struct kvm *kvm = resize->kvm;
1447 if (WARN_ON(resize->error != -EBUSY))
1450 mutex_lock(&kvm->arch.mmu_setup_lock);
1452 /* Request is still current? */
1453 if (kvm->arch.resize_hpt == resize) {
1454 /* We may request large allocations here:
1455 * do not sleep with kvm->arch.mmu_setup_lock held for a while.
1457 mutex_unlock(&kvm->arch.mmu_setup_lock);
1459 resize_hpt_debug(resize, "resize_hpt_prepare_work(): order = %d\n",
1462 err = resize_hpt_allocate(resize);
1464 /* We have strict assumption about -EBUSY
1465 * when preparing for HPT resize.
1467 if (WARN_ON(err == -EBUSY))
1470 mutex_lock(&kvm->arch.mmu_setup_lock);
1471 /* It is possible that kvm->arch.resize_hpt != resize
1472 * after we grab kvm->arch.mmu_setup_lock again.
1476 resize->error = err;
1478 if (kvm->arch.resize_hpt != resize)
1479 resize_hpt_release(kvm, resize);
1481 mutex_unlock(&kvm->arch.mmu_setup_lock);
1484 long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
1485 struct kvm_ppc_resize_hpt *rhpt)
1487 unsigned long flags = rhpt->flags;
1488 unsigned long shift = rhpt->shift;
1489 struct kvm_resize_hpt *resize;
1492 if (flags != 0 || kvm_is_radix(kvm))
1495 if (shift && ((shift < 18) || (shift > 46)))
1498 mutex_lock(&kvm->arch.mmu_setup_lock);
1500 resize = kvm->arch.resize_hpt;
1503 if (resize->order == shift) {
1504 /* Suitable resize in progress? */
1505 ret = resize->error;
1507 ret = 100; /* estimated time in ms */
1509 resize_hpt_release(kvm, resize);
1514 /* not suitable, cancel it */
1515 resize_hpt_release(kvm, resize);
1520 goto out; /* nothing to do */
1522 /* start new resize */
1524 resize = kzalloc(sizeof(*resize), GFP_KERNEL);
1530 resize->error = -EBUSY;
1531 resize->order = shift;
1533 INIT_WORK(&resize->work, resize_hpt_prepare_work);
1534 kvm->arch.resize_hpt = resize;
1536 schedule_work(&resize->work);
1538 ret = 100; /* estimated time in ms */
1541 mutex_unlock(&kvm->arch.mmu_setup_lock);
1545 static void resize_hpt_boot_vcpu(void *opaque)
1547 /* Nothing to do, just force a KVM exit */
1550 long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
1551 struct kvm_ppc_resize_hpt *rhpt)
1553 unsigned long flags = rhpt->flags;
1554 unsigned long shift = rhpt->shift;
1555 struct kvm_resize_hpt *resize;
1558 if (flags != 0 || kvm_is_radix(kvm))
1561 if (shift && ((shift < 18) || (shift > 46)))
1564 mutex_lock(&kvm->arch.mmu_setup_lock);
1566 resize = kvm->arch.resize_hpt;
1568 /* This shouldn't be possible */
1570 if (WARN_ON(!kvm->arch.mmu_ready))
1573 /* Stop VCPUs from running while we mess with the HPT */
1574 kvm->arch.mmu_ready = 0;
1577 /* Boot all CPUs out of the guest so they re-read
1579 on_each_cpu(resize_hpt_boot_vcpu, NULL, 1);
1582 if (!resize || (resize->order != shift))
1585 ret = resize->error;
1589 ret = resize_hpt_rehash(resize);
1593 resize_hpt_pivot(resize);
1596 /* Let VCPUs run again */
1597 kvm->arch.mmu_ready = 1;
1600 resize_hpt_release(kvm, resize);
1601 mutex_unlock(&kvm->arch.mmu_setup_lock);
1606 * Functions for reading and writing the hash table via reads and
1607 * writes on a file descriptor.
1609 * Reads return the guest view of the hash table, which has to be
1610 * pieced together from the real hash table and the guest_rpte
1611 * values in the revmap array.
1613 * On writes, each HPTE written is considered in turn, and if it
1614 * is valid, it is written to the HPT as if an H_ENTER with the
1615 * exact flag set was done. When the invalid count is non-zero
1616 * in the header written to the stream, the kernel will make
1617 * sure that that many HPTEs are invalid, and invalidate them
1621 struct kvm_htab_ctx {
1622 unsigned long index;
1623 unsigned long flags;
1628 #define HPTE_SIZE (2 * sizeof(unsigned long))
1631 * Returns 1 if this HPT entry has been modified or has pending
1634 static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp)
1636 unsigned long rcbits_unset;
1638 if (revp->guest_rpte & HPTE_GR_MODIFIED)
1641 /* Also need to consider changes in reference and changed bits */
1642 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1643 if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) &&
1644 (be64_to_cpu(hptp[1]) & rcbits_unset))
1650 static long record_hpte(unsigned long flags, __be64 *hptp,
1651 unsigned long *hpte, struct revmap_entry *revp,
1652 int want_valid, int first_pass)
1654 unsigned long v, r, hr;
1655 unsigned long rcbits_unset;
1659 /* Unmodified entries are uninteresting except on the first pass */
1660 dirty = hpte_dirty(revp, hptp);
1661 if (!first_pass && !dirty)
1665 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) {
1667 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
1668 !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED))
1671 if (valid != want_valid)
1675 if (valid || dirty) {
1676 /* lock the HPTE so it's stable and read it */
1678 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
1680 v = be64_to_cpu(hptp[0]);
1681 hr = be64_to_cpu(hptp[1]);
1682 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1683 v = hpte_new_to_old_v(v, hr);
1684 hr = hpte_new_to_old_r(hr);
1687 /* re-evaluate valid and dirty from synchronized HPTE value */
1688 valid = !!(v & HPTE_V_VALID);
1689 dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
1691 /* Harvest R and C into guest view if necessary */
1692 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1693 if (valid && (rcbits_unset & hr)) {
1694 revp->guest_rpte |= (hr &
1695 (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED;
1699 if (v & HPTE_V_ABSENT) {
1700 v &= ~HPTE_V_ABSENT;
1704 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
1707 r = revp->guest_rpte;
1708 /* only clear modified if this is the right sort of entry */
1709 if (valid == want_valid && dirty) {
1710 r &= ~HPTE_GR_MODIFIED;
1711 revp->guest_rpte = r;
1713 unlock_hpte(hptp, be64_to_cpu(hptp[0]));
1715 if (!(valid == want_valid && (first_pass || dirty)))
1718 hpte[0] = cpu_to_be64(v);
1719 hpte[1] = cpu_to_be64(r);
1723 static ssize_t kvm_htab_read(struct file *file, char __user *buf,
1724 size_t count, loff_t *ppos)
1726 struct kvm_htab_ctx *ctx = file->private_data;
1727 struct kvm *kvm = ctx->kvm;
1728 struct kvm_get_htab_header hdr;
1730 struct revmap_entry *revp;
1731 unsigned long i, nb, nw;
1732 unsigned long __user *lbuf;
1733 struct kvm_get_htab_header __user *hptr;
1734 unsigned long flags;
1736 unsigned long hpte[2];
1738 if (!access_ok(buf, count))
1740 if (kvm_is_radix(kvm))
1743 first_pass = ctx->first_pass;
1747 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
1748 revp = kvm->arch.hpt.rev + i;
1749 lbuf = (unsigned long __user *)buf;
1752 while (nb + sizeof(hdr) + HPTE_SIZE < count) {
1753 /* Initialize header */
1754 hptr = (struct kvm_get_htab_header __user *)buf;
1759 lbuf = (unsigned long __user *)(buf + sizeof(hdr));
1761 /* Skip uninteresting entries, i.e. clean on not-first pass */
1763 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
1764 !hpte_dirty(revp, hptp)) {
1772 /* Grab a series of valid entries */
1773 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
1774 hdr.n_valid < 0xffff &&
1775 nb + HPTE_SIZE < count &&
1776 record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
1777 /* valid entry, write it out */
1779 if (__put_user(hpte[0], lbuf) ||
1780 __put_user(hpte[1], lbuf + 1))
1788 /* Now skip invalid entries while we can */
1789 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
1790 hdr.n_invalid < 0xffff &&
1791 record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
1792 /* found an invalid entry */
1799 if (hdr.n_valid || hdr.n_invalid) {
1800 /* write back the header */
1801 if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
1804 buf = (char __user *)lbuf;
1809 /* Check if we've wrapped around the hash table */
1810 if (i >= kvmppc_hpt_npte(&kvm->arch.hpt)) {
1812 ctx->first_pass = 0;
1822 static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
1823 size_t count, loff_t *ppos)
1825 struct kvm_htab_ctx *ctx = file->private_data;
1826 struct kvm *kvm = ctx->kvm;
1827 struct kvm_get_htab_header hdr;
1830 unsigned long __user *lbuf;
1832 unsigned long tmp[2];
1838 if (!access_ok(buf, count))
1840 if (kvm_is_radix(kvm))
1843 /* lock out vcpus from running while we're doing this */
1844 mutex_lock(&kvm->arch.mmu_setup_lock);
1845 mmu_ready = kvm->arch.mmu_ready;
1847 kvm->arch.mmu_ready = 0; /* temporarily */
1848 /* order mmu_ready vs. vcpus_running */
1850 if (atomic_read(&kvm->arch.vcpus_running)) {
1851 kvm->arch.mmu_ready = 1;
1852 mutex_unlock(&kvm->arch.mmu_setup_lock);
1858 for (nb = 0; nb + sizeof(hdr) <= count; ) {
1860 if (__copy_from_user(&hdr, buf, sizeof(hdr)))
1864 if (nb + hdr.n_valid * HPTE_SIZE > count)
1872 if (i >= kvmppc_hpt_npte(&kvm->arch.hpt) ||
1873 i + hdr.n_valid + hdr.n_invalid > kvmppc_hpt_npte(&kvm->arch.hpt))
1876 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
1877 lbuf = (unsigned long __user *)buf;
1878 for (j = 0; j < hdr.n_valid; ++j) {
1883 if (__get_user(hpte_v, lbuf) ||
1884 __get_user(hpte_r, lbuf + 1))
1886 v = be64_to_cpu(hpte_v);
1887 r = be64_to_cpu(hpte_r);
1889 if (!(v & HPTE_V_VALID))
1891 pshift = kvmppc_hpte_base_page_shift(v, r);
1897 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
1898 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1900 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
1902 if (ret != H_SUCCESS) {
1903 pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
1904 "r=%lx\n", ret, i, v, r);
1907 if (!mmu_ready && is_vrma_hpte(v)) {
1908 unsigned long senc, lpcr;
1910 senc = slb_pgsize_encoding(1ul << pshift);
1911 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1912 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1913 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
1914 lpcr = senc << (LPCR_VRMASD_SH - 4);
1915 kvmppc_update_lpcr(kvm, lpcr,
1918 kvmppc_setup_partition_table(kvm);
1926 for (j = 0; j < hdr.n_invalid; ++j) {
1927 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
1928 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1936 /* Order HPTE updates vs. mmu_ready */
1938 kvm->arch.mmu_ready = mmu_ready;
1939 mutex_unlock(&kvm->arch.mmu_setup_lock);
1946 static int kvm_htab_release(struct inode *inode, struct file *filp)
1948 struct kvm_htab_ctx *ctx = filp->private_data;
1950 filp->private_data = NULL;
1951 if (!(ctx->flags & KVM_GET_HTAB_WRITE))
1952 atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
1953 kvm_put_kvm(ctx->kvm);
1958 static const struct file_operations kvm_htab_fops = {
1959 .read = kvm_htab_read,
1960 .write = kvm_htab_write,
1961 .llseek = default_llseek,
1962 .release = kvm_htab_release,
1965 int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
1968 struct kvm_htab_ctx *ctx;
1971 /* reject flags we don't recognize */
1972 if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
1974 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1979 ctx->index = ghf->start_index;
1980 ctx->flags = ghf->flags;
1981 ctx->first_pass = 1;
1983 rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
1984 ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
1987 kvm_put_kvm_no_destroy(kvm);
1991 if (rwflag == O_RDONLY) {
1992 mutex_lock(&kvm->slots_lock);
1993 atomic_inc(&kvm->arch.hpte_mod_interest);
1994 /* make sure kvmppc_do_h_enter etc. see the increment */
1995 synchronize_srcu_expedited(&kvm->srcu);
1996 mutex_unlock(&kvm->slots_lock);
2002 struct debugfs_htab_state {
2005 unsigned long hpt_index;
2011 static int debugfs_htab_open(struct inode *inode, struct file *file)
2013 struct kvm *kvm = inode->i_private;
2014 struct debugfs_htab_state *p;
2016 p = kzalloc(sizeof(*p), GFP_KERNEL);
2022 mutex_init(&p->mutex);
2023 file->private_data = p;
2025 return nonseekable_open(inode, file);
2028 static int debugfs_htab_release(struct inode *inode, struct file *file)
2030 struct debugfs_htab_state *p = file->private_data;
2032 kvm_put_kvm(p->kvm);
2037 static ssize_t debugfs_htab_read(struct file *file, char __user *buf,
2038 size_t len, loff_t *ppos)
2040 struct debugfs_htab_state *p = file->private_data;
2043 unsigned long v, hr, gr;
2048 if (kvm_is_radix(kvm))
2051 ret = mutex_lock_interruptible(&p->mutex);
2055 if (p->chars_left) {
2059 r = copy_to_user(buf, p->buf + p->buf_index, n);
2074 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
2075 for (; len != 0 && i < kvmppc_hpt_npte(&kvm->arch.hpt);
2077 if (!(be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)))
2080 /* lock the HPTE so it's stable and read it */
2082 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
2084 v = be64_to_cpu(hptp[0]) & ~HPTE_V_HVLOCK;
2085 hr = be64_to_cpu(hptp[1]);
2086 gr = kvm->arch.hpt.rev[i].guest_rpte;
2087 unlock_hpte(hptp, v);
2090 if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
2093 n = scnprintf(p->buf, sizeof(p->buf),
2094 "%6lx %.16lx %.16lx %.16lx\n",
2099 r = copy_to_user(buf, p->buf, n);
2115 mutex_unlock(&p->mutex);
2119 static ssize_t debugfs_htab_write(struct file *file, const char __user *buf,
2120 size_t len, loff_t *ppos)
2125 static const struct file_operations debugfs_htab_fops = {
2126 .owner = THIS_MODULE,
2127 .open = debugfs_htab_open,
2128 .release = debugfs_htab_release,
2129 .read = debugfs_htab_read,
2130 .write = debugfs_htab_write,
2131 .llseek = generic_file_llseek,
2134 void kvmppc_mmu_debugfs_init(struct kvm *kvm)
2136 debugfs_create_file("htab", 0400, kvm->arch.debugfs_dir, kvm,
2137 &debugfs_htab_fops);
2140 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
2142 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
2144 vcpu->arch.slb_nr = 32; /* POWER7/POWER8 */
2146 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
2148 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;