1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright 2016 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/anon_inodes.h>
12 #include <linux/file.h>
13 #include <linux/debugfs.h>
14 #include <linux/pgtable.h>
16 #include <asm/kvm_ppc.h>
17 #include <asm/kvm_book3s.h>
20 #include <asm/pgalloc.h>
21 #include <asm/pte-walk.h>
22 #include <asm/ultravisor.h>
23 #include <asm/kvm_book3s_uvmem.h>
26 * Supported radix tree geometry.
27 * Like p9, we support either 5 or 9 bits at the first (lowest) level,
28 * for a page size of 64k or 4k.
30 static int p9_supported_radix_bits[4] = { 5, 9, 9, 13 };
32 unsigned long __kvmhv_copy_tofrom_guest_radix(int lpid, int pid,
33 gva_t eaddr, void *to, void *from,
36 int old_pid, old_lpid;
37 unsigned long quadrant, ret = n;
40 /* Can't access quadrants 1 or 2 in non-HV mode, call the HV to do it */
41 if (kvmhv_on_pseries())
42 return plpar_hcall_norets(H_COPY_TOFROM_GUEST, lpid, pid, eaddr,
43 (to != NULL) ? __pa(to): 0,
44 (from != NULL) ? __pa(from): 0, n);
50 from = (void *) (eaddr | (quadrant << 62));
52 to = (void *) (eaddr | (quadrant << 62));
56 /* switch the lpid first to avoid running host with unallocated pid */
57 old_lpid = mfspr(SPRN_LPID);
59 mtspr(SPRN_LPID, lpid);
61 old_pid = mfspr(SPRN_PID);
69 ret = __copy_from_user_inatomic(to, (const void __user *)from, n);
71 ret = __copy_to_user_inatomic((void __user *)to, from, n);
74 /* switch the pid first to avoid running host with unallocated pid */
75 if (quadrant == 1 && pid != old_pid)
76 mtspr(SPRN_PID, old_pid);
78 mtspr(SPRN_LPID, old_lpid);
85 EXPORT_SYMBOL_GPL(__kvmhv_copy_tofrom_guest_radix);
87 static long kvmhv_copy_tofrom_guest_radix(struct kvm_vcpu *vcpu, gva_t eaddr,
88 void *to, void *from, unsigned long n)
90 int lpid = vcpu->kvm->arch.lpid;
91 int pid = vcpu->arch.pid;
93 /* This would cause a data segment intr so don't allow the access */
94 if (eaddr & (0x3FFUL << 52))
97 /* Should we be using the nested lpid */
98 if (vcpu->arch.nested)
99 lpid = vcpu->arch.nested->shadow_lpid;
101 /* If accessing quadrant 3 then pid is expected to be 0 */
102 if (((eaddr >> 62) & 0x3) == 0x3)
105 eaddr &= ~(0xFFFUL << 52);
107 return __kvmhv_copy_tofrom_guest_radix(lpid, pid, eaddr, to, from, n);
110 long kvmhv_copy_from_guest_radix(struct kvm_vcpu *vcpu, gva_t eaddr, void *to,
115 ret = kvmhv_copy_tofrom_guest_radix(vcpu, eaddr, to, NULL, n);
117 memset(to + (n - ret), 0, ret);
121 EXPORT_SYMBOL_GPL(kvmhv_copy_from_guest_radix);
123 long kvmhv_copy_to_guest_radix(struct kvm_vcpu *vcpu, gva_t eaddr, void *from,
126 return kvmhv_copy_tofrom_guest_radix(vcpu, eaddr, NULL, from, n);
128 EXPORT_SYMBOL_GPL(kvmhv_copy_to_guest_radix);
130 int kvmppc_mmu_walk_radix_tree(struct kvm_vcpu *vcpu, gva_t eaddr,
131 struct kvmppc_pte *gpte, u64 root,
134 struct kvm *kvm = vcpu->kvm;
136 unsigned long rts, bits, offset, index;
140 rts = ((root & RTS1_MASK) >> (RTS1_SHIFT - 3)) |
141 ((root & RTS2_MASK) >> RTS2_SHIFT);
142 bits = root & RPDS_MASK;
143 base = root & RPDB_MASK;
147 /* Current implementations only support 52-bit space */
151 /* Walk each level of the radix tree */
152 for (level = 3; level >= 0; --level) {
154 /* Check a valid size */
155 if (level && bits != p9_supported_radix_bits[level])
157 if (level == 0 && !(bits == 5 || bits == 9))
160 index = (eaddr >> offset) & ((1UL << bits) - 1);
161 /* Check that low bits of page table base are zero */
162 if (base & ((1UL << (bits + 3)) - 1))
164 /* Read the entry from guest memory */
165 addr = base + (index * sizeof(rpte));
166 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
167 ret = kvm_read_guest(kvm, addr, &rpte, sizeof(rpte));
168 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
174 pte = __be64_to_cpu(rpte);
175 if (!(pte & _PAGE_PRESENT))
177 /* Check if a leaf entry */
180 /* Get ready to walk the next level */
181 base = pte & RPDB_MASK;
182 bits = pte & RPDS_MASK;
185 /* Need a leaf at lowest level; 512GB pages not supported */
186 if (level < 0 || level == 3)
189 /* We found a valid leaf PTE */
190 /* Offset is now log base 2 of the page size */
191 gpa = pte & 0x01fffffffffff000ul;
192 if (gpa & ((1ul << offset) - 1))
194 gpa |= eaddr & ((1ul << offset) - 1);
195 for (ps = MMU_PAGE_4K; ps < MMU_PAGE_COUNT; ++ps)
196 if (offset == mmu_psize_defs[ps].shift)
198 gpte->page_size = ps;
199 gpte->page_shift = offset;
204 /* Work out permissions */
205 gpte->may_read = !!(pte & _PAGE_READ);
206 gpte->may_write = !!(pte & _PAGE_WRITE);
207 gpte->may_execute = !!(pte & _PAGE_EXEC);
209 gpte->rc = pte & (_PAGE_ACCESSED | _PAGE_DIRTY);
218 * Used to walk a partition or process table radix tree in guest memory
219 * Note: We exploit the fact that a partition table and a process
220 * table have the same layout, a partition-scoped page table and a
221 * process-scoped page table have the same layout, and the 2nd
222 * doubleword of a partition table entry has the same layout as
225 int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr,
226 struct kvmppc_pte *gpte, u64 table,
227 int table_index, u64 *pte_ret_p)
229 struct kvm *kvm = vcpu->kvm;
231 unsigned long size, ptbl, root;
232 struct prtb_entry entry;
234 if ((table & PRTS_MASK) > 24)
236 size = 1ul << ((table & PRTS_MASK) + 12);
238 /* Is the table big enough to contain this entry? */
239 if ((table_index * sizeof(entry)) >= size)
242 /* Read the table to find the root of the radix tree */
243 ptbl = (table & PRTB_MASK) + (table_index * sizeof(entry));
244 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
245 ret = kvm_read_guest(kvm, ptbl, &entry, sizeof(entry));
246 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
250 /* Root is stored in the first double word */
251 root = be64_to_cpu(entry.prtb0);
253 return kvmppc_mmu_walk_radix_tree(vcpu, eaddr, gpte, root, pte_ret_p);
256 int kvmppc_mmu_radix_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
257 struct kvmppc_pte *gpte, bool data, bool iswrite)
263 /* Work out effective PID */
264 switch (eaddr >> 62) {
266 pid = vcpu->arch.pid;
275 ret = kvmppc_mmu_radix_translate_table(vcpu, eaddr, gpte,
276 vcpu->kvm->arch.process_table, pid, &pte);
280 /* Check privilege (applies only to process scoped translations) */
281 if (kvmppc_get_msr(vcpu) & MSR_PR) {
282 if (pte & _PAGE_PRIVILEGED) {
285 gpte->may_execute = 0;
288 if (!(pte & _PAGE_PRIVILEGED)) {
289 /* Check AMR/IAMR to see if strict mode is in force */
290 if (vcpu->arch.amr & (1ul << 62))
292 if (vcpu->arch.amr & (1ul << 63))
294 if (vcpu->arch.iamr & (1ul << 62))
295 gpte->may_execute = 0;
302 void kvmppc_radix_tlbie_page(struct kvm *kvm, unsigned long addr,
303 unsigned int pshift, unsigned int lpid)
305 unsigned long psize = PAGE_SIZE;
311 psize = 1UL << pshift;
315 addr &= ~(psize - 1);
317 if (!kvmhv_on_pseries()) {
318 radix__flush_tlb_lpid_page(lpid, addr, psize);
322 psi = shift_to_mmu_psize(pshift);
323 rb = addr | (mmu_get_ap(psi) << PPC_BITLSHIFT(58));
324 rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(0, 0, 1),
327 pr_err("KVM: TLB page invalidation hcall failed, rc=%ld\n", rc);
330 static void kvmppc_radix_flush_pwc(struct kvm *kvm, unsigned int lpid)
334 if (!kvmhv_on_pseries()) {
335 radix__flush_pwc_lpid(lpid);
339 rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(1, 0, 1),
340 lpid, TLBIEL_INVAL_SET_LPID);
342 pr_err("KVM: TLB PWC invalidation hcall failed, rc=%ld\n", rc);
345 static unsigned long kvmppc_radix_update_pte(struct kvm *kvm, pte_t *ptep,
346 unsigned long clr, unsigned long set,
347 unsigned long addr, unsigned int shift)
349 return __radix_pte_update(ptep, clr, set);
352 static void kvmppc_radix_set_pte_at(struct kvm *kvm, unsigned long addr,
353 pte_t *ptep, pte_t pte)
355 radix__set_pte_at(kvm->mm, addr, ptep, pte, 0);
358 static struct kmem_cache *kvm_pte_cache;
359 static struct kmem_cache *kvm_pmd_cache;
361 static pte_t *kvmppc_pte_alloc(void)
365 pte = kmem_cache_alloc(kvm_pte_cache, GFP_KERNEL);
366 /* pmd_populate() will only reference _pa(pte). */
367 kmemleak_ignore(pte);
372 static void kvmppc_pte_free(pte_t *ptep)
374 kmem_cache_free(kvm_pte_cache, ptep);
377 static pmd_t *kvmppc_pmd_alloc(void)
381 pmd = kmem_cache_alloc(kvm_pmd_cache, GFP_KERNEL);
382 /* pud_populate() will only reference _pa(pmd). */
383 kmemleak_ignore(pmd);
388 static void kvmppc_pmd_free(pmd_t *pmdp)
390 kmem_cache_free(kvm_pmd_cache, pmdp);
393 /* Called with kvm->mmu_lock held */
394 void kvmppc_unmap_pte(struct kvm *kvm, pte_t *pte, unsigned long gpa,
396 const struct kvm_memory_slot *memslot,
401 unsigned long gfn = gpa >> PAGE_SHIFT;
402 unsigned long page_size = PAGE_SIZE;
405 old = kvmppc_radix_update_pte(kvm, pte, ~0UL, 0, gpa, shift);
406 kvmppc_radix_tlbie_page(kvm, gpa, shift, lpid);
408 /* The following only applies to L1 entries */
409 if (lpid != kvm->arch.lpid)
413 memslot = gfn_to_memslot(kvm, gfn);
417 if (shift) { /* 1GB or 2MB page */
418 page_size = 1ul << shift;
419 if (shift == PMD_SHIFT)
420 kvm->stat.num_2M_pages--;
421 else if (shift == PUD_SHIFT)
422 kvm->stat.num_1G_pages--;
425 gpa &= ~(page_size - 1);
426 hpa = old & PTE_RPN_MASK;
427 kvmhv_remove_nest_rmap_range(kvm, memslot, gpa, hpa, page_size);
429 if ((old & _PAGE_DIRTY) && memslot->dirty_bitmap)
430 kvmppc_update_dirty_map(memslot, gfn, page_size);
434 * kvmppc_free_p?d are used to free existing page tables, and recursively
435 * descend and clear and free children.
436 * Callers are responsible for flushing the PWC.
438 * When page tables are being unmapped/freed as part of page fault path
439 * (full == false), valid ptes are generally not expected; however, there
440 * is one situation where they arise, which is when dirty page logging is
441 * turned off for a memslot while the VM is running. The new memslot
442 * becomes visible to page faults before the memslot commit function
443 * gets to flush the memslot, which can lead to a 2MB page mapping being
444 * installed for a guest physical address where there are already 64kB
445 * (or 4kB) mappings (of sub-pages of the same 2MB page).
447 static void kvmppc_unmap_free_pte(struct kvm *kvm, pte_t *pte, bool full,
451 memset(pte, 0, sizeof(long) << RADIX_PTE_INDEX_SIZE);
456 for (it = 0; it < PTRS_PER_PTE; ++it, ++p) {
457 if (pte_val(*p) == 0)
459 kvmppc_unmap_pte(kvm, p,
460 pte_pfn(*p) << PAGE_SHIFT,
461 PAGE_SHIFT, NULL, lpid);
465 kvmppc_pte_free(pte);
468 static void kvmppc_unmap_free_pmd(struct kvm *kvm, pmd_t *pmd, bool full,
474 for (im = 0; im < PTRS_PER_PMD; ++im, ++p) {
475 if (!pmd_present(*p))
477 if (pmd_is_leaf(*p)) {
482 kvmppc_unmap_pte(kvm, (pte_t *)p,
483 pte_pfn(*(pte_t *)p) << PAGE_SHIFT,
484 PMD_SHIFT, NULL, lpid);
489 pte = pte_offset_map(p, 0);
490 kvmppc_unmap_free_pte(kvm, pte, full, lpid);
494 kvmppc_pmd_free(pmd);
497 static void kvmppc_unmap_free_pud(struct kvm *kvm, pud_t *pud,
503 for (iu = 0; iu < PTRS_PER_PUD; ++iu, ++p) {
504 if (!pud_present(*p))
506 if (pud_is_leaf(*p)) {
511 pmd = pmd_offset(p, 0);
512 kvmppc_unmap_free_pmd(kvm, pmd, true, lpid);
516 pud_free(kvm->mm, pud);
519 void kvmppc_free_pgtable_radix(struct kvm *kvm, pgd_t *pgd, unsigned int lpid)
523 for (ig = 0; ig < PTRS_PER_PGD; ++ig, ++pgd) {
524 p4d_t *p4d = p4d_offset(pgd, 0);
527 if (!p4d_present(*p4d))
529 pud = pud_offset(p4d, 0);
530 kvmppc_unmap_free_pud(kvm, pud, lpid);
535 void kvmppc_free_radix(struct kvm *kvm)
537 if (kvm->arch.pgtable) {
538 kvmppc_free_pgtable_radix(kvm, kvm->arch.pgtable,
540 pgd_free(kvm->mm, kvm->arch.pgtable);
541 kvm->arch.pgtable = NULL;
545 static void kvmppc_unmap_free_pmd_entry_table(struct kvm *kvm, pmd_t *pmd,
546 unsigned long gpa, unsigned int lpid)
548 pte_t *pte = pte_offset_kernel(pmd, 0);
551 * Clearing the pmd entry then flushing the PWC ensures that the pte
552 * page no longer be cached by the MMU, so can be freed without
553 * flushing the PWC again.
556 kvmppc_radix_flush_pwc(kvm, lpid);
558 kvmppc_unmap_free_pte(kvm, pte, false, lpid);
561 static void kvmppc_unmap_free_pud_entry_table(struct kvm *kvm, pud_t *pud,
562 unsigned long gpa, unsigned int lpid)
564 pmd_t *pmd = pmd_offset(pud, 0);
567 * Clearing the pud entry then flushing the PWC ensures that the pmd
568 * page and any children pte pages will no longer be cached by the MMU,
569 * so can be freed without flushing the PWC again.
572 kvmppc_radix_flush_pwc(kvm, lpid);
574 kvmppc_unmap_free_pmd(kvm, pmd, false, lpid);
578 * There are a number of bits which may differ between different faults to
579 * the same partition scope entry. RC bits, in the course of cleaning and
580 * aging. And the write bit can change, either the access could have been
581 * upgraded, or a read fault could happen concurrently with a write fault
582 * that sets those bits first.
584 #define PTE_BITS_MUST_MATCH (~(_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED))
586 int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
587 unsigned long gpa, unsigned int level,
588 unsigned long mmu_seq, unsigned int lpid,
589 unsigned long *rmapp, struct rmap_nested **n_rmap)
593 pud_t *pud, *new_pud = NULL;
594 pmd_t *pmd, *new_pmd = NULL;
595 pte_t *ptep, *new_ptep = NULL;
598 /* Traverse the guest's 2nd-level tree, allocate new levels needed */
599 pgd = pgtable + pgd_index(gpa);
600 p4d = p4d_offset(pgd, gpa);
603 if (p4d_present(*p4d))
604 pud = pud_offset(p4d, gpa);
606 new_pud = pud_alloc_one(kvm->mm, gpa);
609 if (pud && pud_present(*pud) && !pud_is_leaf(*pud))
610 pmd = pmd_offset(pud, gpa);
612 new_pmd = kvmppc_pmd_alloc();
614 if (level == 0 && !(pmd && pmd_present(*pmd) && !pmd_is_leaf(*pmd)))
615 new_ptep = kvmppc_pte_alloc();
617 /* Check if we might have been invalidated; let the guest retry if so */
618 spin_lock(&kvm->mmu_lock);
620 if (mmu_notifier_retry(kvm, mmu_seq))
623 /* Now traverse again under the lock and change the tree */
625 if (p4d_none(*p4d)) {
628 p4d_populate(kvm->mm, p4d, new_pud);
631 pud = pud_offset(p4d, gpa);
632 if (pud_is_leaf(*pud)) {
633 unsigned long hgpa = gpa & PUD_MASK;
635 /* Check if we raced and someone else has set the same thing */
637 if (pud_raw(*pud) == pte_raw(pte)) {
641 /* Valid 1GB page here already, add our extra bits */
642 WARN_ON_ONCE((pud_val(*pud) ^ pte_val(pte)) &
643 PTE_BITS_MUST_MATCH);
644 kvmppc_radix_update_pte(kvm, (pte_t *)pud,
645 0, pte_val(pte), hgpa, PUD_SHIFT);
650 * If we raced with another CPU which has just put
651 * a 1GB pte in after we saw a pmd page, try again.
657 /* Valid 1GB page here already, remove it */
658 kvmppc_unmap_pte(kvm, (pte_t *)pud, hgpa, PUD_SHIFT, NULL,
662 if (!pud_none(*pud)) {
664 * There's a page table page here, but we wanted to
665 * install a large page, so remove and free the page
668 kvmppc_unmap_free_pud_entry_table(kvm, pud, gpa, lpid);
670 kvmppc_radix_set_pte_at(kvm, gpa, (pte_t *)pud, pte);
672 kvmhv_insert_nest_rmap(kvm, rmapp, n_rmap);
676 if (pud_none(*pud)) {
679 pud_populate(kvm->mm, pud, new_pmd);
682 pmd = pmd_offset(pud, gpa);
683 if (pmd_is_leaf(*pmd)) {
684 unsigned long lgpa = gpa & PMD_MASK;
686 /* Check if we raced and someone else has set the same thing */
688 if (pmd_raw(*pmd) == pte_raw(pte)) {
692 /* Valid 2MB page here already, add our extra bits */
693 WARN_ON_ONCE((pmd_val(*pmd) ^ pte_val(pte)) &
694 PTE_BITS_MUST_MATCH);
695 kvmppc_radix_update_pte(kvm, pmdp_ptep(pmd),
696 0, pte_val(pte), lgpa, PMD_SHIFT);
702 * If we raced with another CPU which has just put
703 * a 2MB pte in after we saw a pte page, try again.
709 /* Valid 2MB page here already, remove it */
710 kvmppc_unmap_pte(kvm, pmdp_ptep(pmd), lgpa, PMD_SHIFT, NULL,
714 if (!pmd_none(*pmd)) {
716 * There's a page table page here, but we wanted to
717 * install a large page, so remove and free the page
720 kvmppc_unmap_free_pmd_entry_table(kvm, pmd, gpa, lpid);
722 kvmppc_radix_set_pte_at(kvm, gpa, pmdp_ptep(pmd), pte);
724 kvmhv_insert_nest_rmap(kvm, rmapp, n_rmap);
728 if (pmd_none(*pmd)) {
731 pmd_populate(kvm->mm, pmd, new_ptep);
734 ptep = pte_offset_kernel(pmd, gpa);
735 if (pte_present(*ptep)) {
736 /* Check if someone else set the same thing */
737 if (pte_raw(*ptep) == pte_raw(pte)) {
741 /* Valid page here already, add our extra bits */
742 WARN_ON_ONCE((pte_val(*ptep) ^ pte_val(pte)) &
743 PTE_BITS_MUST_MATCH);
744 kvmppc_radix_update_pte(kvm, ptep, 0, pte_val(pte), gpa, 0);
748 kvmppc_radix_set_pte_at(kvm, gpa, ptep, pte);
750 kvmhv_insert_nest_rmap(kvm, rmapp, n_rmap);
754 spin_unlock(&kvm->mmu_lock);
756 pud_free(kvm->mm, new_pud);
758 kvmppc_pmd_free(new_pmd);
760 kvmppc_pte_free(new_ptep);
764 bool kvmppc_hv_handle_set_rc(struct kvm *kvm, bool nested, bool writing,
765 unsigned long gpa, unsigned int lpid)
767 unsigned long pgflags;
772 * Need to set an R or C bit in the 2nd-level tables;
773 * since we are just helping out the hardware here,
774 * it is sufficient to do what the hardware does.
776 pgflags = _PAGE_ACCESSED;
778 pgflags |= _PAGE_DIRTY;
781 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
783 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
785 if (ptep && pte_present(*ptep) && (!writing || pte_write(*ptep))) {
786 kvmppc_radix_update_pte(kvm, ptep, 0, pgflags, gpa, shift);
792 int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu,
794 struct kvm_memory_slot *memslot,
795 bool writing, bool kvm_ro,
796 pte_t *inserted_pte, unsigned int *levelp)
798 struct kvm *kvm = vcpu->kvm;
799 struct page *page = NULL;
800 unsigned long mmu_seq;
801 unsigned long hva, gfn = gpa >> PAGE_SHIFT;
802 bool upgrade_write = false;
803 bool *upgrade_p = &upgrade_write;
805 unsigned int shift, level;
809 /* used to check for invalidations in progress */
810 mmu_seq = kvm->mmu_notifier_seq;
814 * Do a fast check first, since __gfn_to_pfn_memslot doesn't
815 * do it with !atomic && !async, which is how we call it.
816 * We always ask for write permission since the common case
817 * is that the page is writable.
819 hva = gfn_to_hva_memslot(memslot, gfn);
820 if (!kvm_ro && get_user_page_fast_only(hva, FOLL_WRITE, &page)) {
821 upgrade_write = true;
825 /* Call KVM generic code to do the slow-path check */
826 pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL,
828 if (is_error_noslot_pfn(pfn))
831 if (pfn_valid(pfn)) {
832 page = pfn_to_page(pfn);
833 if (PageReserved(page))
839 * Read the PTE from the process' radix tree and use that
840 * so we get the shift and attribute bits.
842 spin_lock(&kvm->mmu_lock);
843 ptep = find_kvm_host_pte(kvm, mmu_seq, hva, &shift);
846 pte = READ_ONCE(*ptep);
847 spin_unlock(&kvm->mmu_lock);
849 * If the PTE disappeared temporarily due to a THP
850 * collapse, just return and let the guest try again.
852 if (!pte_present(pte)) {
858 /* If we're logging dirty pages, always map single pages */
859 large_enable = !(memslot->flags & KVM_MEM_LOG_DIRTY_PAGES);
861 /* Get pte level from shift/size */
862 if (large_enable && shift == PUD_SHIFT &&
863 (gpa & (PUD_SIZE - PAGE_SIZE)) ==
864 (hva & (PUD_SIZE - PAGE_SIZE))) {
866 } else if (large_enable && shift == PMD_SHIFT &&
867 (gpa & (PMD_SIZE - PAGE_SIZE)) ==
868 (hva & (PMD_SIZE - PAGE_SIZE))) {
872 if (shift > PAGE_SHIFT) {
874 * If the pte maps more than one page, bring over
875 * bits from the virtual address to get the real
876 * address of the specific single page we want.
878 unsigned long rpnmask = (1ul << shift) - PAGE_SIZE;
879 pte = __pte(pte_val(pte) | (hva & rpnmask));
883 pte = __pte(pte_val(pte) | _PAGE_EXEC | _PAGE_ACCESSED);
884 if (writing || upgrade_write) {
885 if (pte_val(pte) & _PAGE_WRITE)
886 pte = __pte(pte_val(pte) | _PAGE_DIRTY);
888 pte = __pte(pte_val(pte) & ~(_PAGE_WRITE | _PAGE_DIRTY));
891 /* Allocate space in the tree and write the PTE */
892 ret = kvmppc_create_pte(kvm, kvm->arch.pgtable, pte, gpa, level,
893 mmu_seq, kvm->arch.lpid, NULL, NULL);
900 if (!ret && (pte_val(pte) & _PAGE_WRITE))
901 set_page_dirty_lock(page);
905 /* Increment number of large pages if we (successfully) inserted one */
908 kvm->stat.num_2M_pages++;
910 kvm->stat.num_1G_pages++;
916 int kvmppc_book3s_radix_page_fault(struct kvm_vcpu *vcpu,
917 unsigned long ea, unsigned long dsisr)
919 struct kvm *kvm = vcpu->kvm;
920 unsigned long gpa, gfn;
921 struct kvm_memory_slot *memslot;
923 bool writing = !!(dsisr & DSISR_ISSTORE);
926 /* Check for unusual errors */
927 if (dsisr & DSISR_UNSUPP_MMU) {
928 pr_err("KVM: Got unsupported MMU fault\n");
931 if (dsisr & DSISR_BADACCESS) {
932 /* Reflect to the guest as DSI */
933 pr_err("KVM: Got radix HV page fault with DSISR=%lx\n", dsisr);
934 kvmppc_core_queue_data_storage(vcpu, ea, dsisr);
938 /* Translate the logical address */
939 gpa = vcpu->arch.fault_gpa & ~0xfffUL;
940 gpa &= ~0xF000000000000000ul;
941 gfn = gpa >> PAGE_SHIFT;
942 if (!(dsisr & DSISR_PRTABLE_FAULT))
945 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
946 return kvmppc_send_page_to_uv(kvm, gfn);
948 /* Get the corresponding memslot */
949 memslot = gfn_to_memslot(kvm, gfn);
951 /* No memslot means it's an emulated MMIO region */
952 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
953 if (dsisr & (DSISR_PRTABLE_FAULT | DSISR_BADACCESS |
956 * Bad address in guest page table tree, or other
957 * unusual error - reflect it to the guest as DSI.
959 kvmppc_core_queue_data_storage(vcpu, ea, dsisr);
962 return kvmppc_hv_emulate_mmio(vcpu, gpa, ea, writing);
965 if (memslot->flags & KVM_MEM_READONLY) {
967 /* give the guest a DSI */
968 kvmppc_core_queue_data_storage(vcpu, ea, DSISR_ISSTORE |
975 /* Failed to set the reference/change bits */
976 if (dsisr & DSISR_SET_RC) {
977 spin_lock(&kvm->mmu_lock);
978 if (kvmppc_hv_handle_set_rc(kvm, false, writing,
979 gpa, kvm->arch.lpid))
980 dsisr &= ~DSISR_SET_RC;
981 spin_unlock(&kvm->mmu_lock);
983 if (!(dsisr & (DSISR_BAD_FAULT_64S | DSISR_NOHPTE |
984 DSISR_PROTFAULT | DSISR_SET_RC)))
988 /* Try to insert a pte */
989 ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot, writing,
992 if (ret == 0 || ret == -EAGAIN)
997 /* Called with kvm->mmu_lock held */
998 int kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
1002 unsigned long gpa = gfn << PAGE_SHIFT;
1005 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE) {
1006 uv_page_inval(kvm->arch.lpid, gpa, PAGE_SHIFT);
1010 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
1011 if (ptep && pte_present(*ptep))
1012 kvmppc_unmap_pte(kvm, ptep, gpa, shift, memslot,
1017 /* Called with kvm->mmu_lock held */
1018 int kvm_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
1022 unsigned long gpa = gfn << PAGE_SHIFT;
1025 unsigned long old, *rmapp;
1027 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
1030 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
1031 if (ptep && pte_present(*ptep) && pte_young(*ptep)) {
1032 old = kvmppc_radix_update_pte(kvm, ptep, _PAGE_ACCESSED, 0,
1034 /* XXX need to flush tlb here? */
1035 /* Also clear bit in ptes in shadow pgtable for nested guests */
1036 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1037 kvmhv_update_nest_rmap_rc_list(kvm, rmapp, _PAGE_ACCESSED, 0,
1045 /* Called with kvm->mmu_lock held */
1046 int kvm_test_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
1050 unsigned long gpa = gfn << PAGE_SHIFT;
1054 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
1057 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
1058 if (ptep && pte_present(*ptep) && pte_young(*ptep))
1063 /* Returns the number of PAGE_SIZE pages that are dirty */
1064 static int kvm_radix_test_clear_dirty(struct kvm *kvm,
1065 struct kvm_memory_slot *memslot, int pagenum)
1067 unsigned long gfn = memslot->base_gfn + pagenum;
1068 unsigned long gpa = gfn << PAGE_SHIFT;
1072 unsigned long old, *rmapp;
1074 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
1078 * For performance reasons we don't hold kvm->mmu_lock while walking the
1079 * partition scoped table.
1081 ptep = find_kvm_secondary_pte_unlocked(kvm, gpa, &shift);
1085 pte = READ_ONCE(*ptep);
1086 if (pte_present(pte) && pte_dirty(pte)) {
1087 spin_lock(&kvm->mmu_lock);
1089 * Recheck the pte again
1091 if (pte_val(pte) != pte_val(*ptep)) {
1093 * We have KVM_MEM_LOG_DIRTY_PAGES enabled. Hence we can
1094 * only find PAGE_SIZE pte entries here. We can continue
1095 * to use the pte addr returned by above page table
1098 if (!pte_present(*ptep) || !pte_dirty(*ptep)) {
1099 spin_unlock(&kvm->mmu_lock);
1106 old = kvmppc_radix_update_pte(kvm, ptep, _PAGE_DIRTY, 0,
1108 kvmppc_radix_tlbie_page(kvm, gpa, shift, kvm->arch.lpid);
1109 /* Also clear bit in ptes in shadow pgtable for nested guests */
1110 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1111 kvmhv_update_nest_rmap_rc_list(kvm, rmapp, _PAGE_DIRTY, 0,
1114 spin_unlock(&kvm->mmu_lock);
1119 long kvmppc_hv_get_dirty_log_radix(struct kvm *kvm,
1120 struct kvm_memory_slot *memslot, unsigned long *map)
1125 for (i = 0; i < memslot->npages; i = j) {
1126 npages = kvm_radix_test_clear_dirty(kvm, memslot, i);
1129 * Note that if npages > 0 then i must be a multiple of npages,
1130 * since huge pages are only used to back the guest at guest
1131 * real addresses that are a multiple of their size.
1132 * Since we have at most one PTE covering any given guest
1133 * real address, if npages > 1 we can skip to i + npages.
1137 set_dirty_bits(map, i, npages);
1144 void kvmppc_radix_flush_memslot(struct kvm *kvm,
1145 const struct kvm_memory_slot *memslot)
1152 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START)
1153 kvmppc_uvmem_drop_pages(memslot, kvm, true);
1155 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
1158 gpa = memslot->base_gfn << PAGE_SHIFT;
1159 spin_lock(&kvm->mmu_lock);
1160 for (n = memslot->npages; n; --n) {
1161 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
1162 if (ptep && pte_present(*ptep))
1163 kvmppc_unmap_pte(kvm, ptep, gpa, shift, memslot,
1168 * Increase the mmu notifier sequence number to prevent any page
1169 * fault that read the memslot earlier from writing a PTE.
1171 kvm->mmu_notifier_seq++;
1172 spin_unlock(&kvm->mmu_lock);
1175 static void add_rmmu_ap_encoding(struct kvm_ppc_rmmu_info *info,
1176 int psize, int *indexp)
1178 if (!mmu_psize_defs[psize].shift)
1180 info->ap_encodings[*indexp] = mmu_psize_defs[psize].shift |
1181 (mmu_psize_defs[psize].ap << 29);
1185 int kvmhv_get_rmmu_info(struct kvm *kvm, struct kvm_ppc_rmmu_info *info)
1189 if (!radix_enabled())
1191 memset(info, 0, sizeof(*info));
1194 info->geometries[0].page_shift = 12;
1195 info->geometries[0].level_bits[0] = 9;
1196 for (i = 1; i < 4; ++i)
1197 info->geometries[0].level_bits[i] = p9_supported_radix_bits[i];
1199 info->geometries[1].page_shift = 16;
1200 for (i = 0; i < 4; ++i)
1201 info->geometries[1].level_bits[i] = p9_supported_radix_bits[i];
1204 add_rmmu_ap_encoding(info, MMU_PAGE_4K, &i);
1205 add_rmmu_ap_encoding(info, MMU_PAGE_64K, &i);
1206 add_rmmu_ap_encoding(info, MMU_PAGE_2M, &i);
1207 add_rmmu_ap_encoding(info, MMU_PAGE_1G, &i);
1212 int kvmppc_init_vm_radix(struct kvm *kvm)
1214 kvm->arch.pgtable = pgd_alloc(kvm->mm);
1215 if (!kvm->arch.pgtable)
1220 static void pte_ctor(void *addr)
1222 memset(addr, 0, RADIX_PTE_TABLE_SIZE);
1225 static void pmd_ctor(void *addr)
1227 memset(addr, 0, RADIX_PMD_TABLE_SIZE);
1230 struct debugfs_radix_state {
1241 static int debugfs_radix_open(struct inode *inode, struct file *file)
1243 struct kvm *kvm = inode->i_private;
1244 struct debugfs_radix_state *p;
1246 p = kzalloc(sizeof(*p), GFP_KERNEL);
1252 mutex_init(&p->mutex);
1253 file->private_data = p;
1255 return nonseekable_open(inode, file);
1258 static int debugfs_radix_release(struct inode *inode, struct file *file)
1260 struct debugfs_radix_state *p = file->private_data;
1262 kvm_put_kvm(p->kvm);
1267 static ssize_t debugfs_radix_read(struct file *file, char __user *buf,
1268 size_t len, loff_t *ppos)
1270 struct debugfs_radix_state *p = file->private_data;
1276 struct kvm_nested_guest *nested;
1286 if (!kvm_is_radix(kvm))
1289 ret = mutex_lock_interruptible(&p->mutex);
1293 if (p->chars_left) {
1297 r = copy_to_user(buf, p->buf + p->buf_index, n);
1314 while (len != 0 && p->lpid >= 0) {
1315 if (gpa >= RADIX_PGTABLE_RANGE) {
1319 kvmhv_put_nested(nested);
1322 p->lpid = kvmhv_nested_next_lpid(kvm, p->lpid);
1329 pgt = kvm->arch.pgtable;
1331 nested = kvmhv_get_nested(kvm, p->lpid, false);
1333 gpa = RADIX_PGTABLE_RANGE;
1336 pgt = nested->shadow_pgtable;
1342 n = scnprintf(p->buf, sizeof(p->buf),
1343 "\nNested LPID %d: ", p->lpid);
1344 n += scnprintf(p->buf + n, sizeof(p->buf) - n,
1345 "pgdir: %lx\n", (unsigned long)pgt);
1350 pgdp = pgt + pgd_index(gpa);
1351 p4dp = p4d_offset(pgdp, gpa);
1352 p4d = READ_ONCE(*p4dp);
1353 if (!(p4d_val(p4d) & _PAGE_PRESENT)) {
1354 gpa = (gpa & P4D_MASK) + P4D_SIZE;
1358 pudp = pud_offset(&p4d, gpa);
1359 pud = READ_ONCE(*pudp);
1360 if (!(pud_val(pud) & _PAGE_PRESENT)) {
1361 gpa = (gpa & PUD_MASK) + PUD_SIZE;
1364 if (pud_val(pud) & _PAGE_PTE) {
1370 pmdp = pmd_offset(&pud, gpa);
1371 pmd = READ_ONCE(*pmdp);
1372 if (!(pmd_val(pmd) & _PAGE_PRESENT)) {
1373 gpa = (gpa & PMD_MASK) + PMD_SIZE;
1376 if (pmd_val(pmd) & _PAGE_PTE) {
1382 ptep = pte_offset_kernel(&pmd, gpa);
1383 pte = pte_val(READ_ONCE(*ptep));
1384 if (!(pte & _PAGE_PRESENT)) {
1390 n = scnprintf(p->buf, sizeof(p->buf),
1391 " %lx: %lx %d\n", gpa, pte, shift);
1392 gpa += 1ul << shift;
1397 r = copy_to_user(buf, p->buf, n);
1412 kvmhv_put_nested(nested);
1415 mutex_unlock(&p->mutex);
1419 static ssize_t debugfs_radix_write(struct file *file, const char __user *buf,
1420 size_t len, loff_t *ppos)
1425 static const struct file_operations debugfs_radix_fops = {
1426 .owner = THIS_MODULE,
1427 .open = debugfs_radix_open,
1428 .release = debugfs_radix_release,
1429 .read = debugfs_radix_read,
1430 .write = debugfs_radix_write,
1431 .llseek = generic_file_llseek,
1434 void kvmhv_radix_debugfs_init(struct kvm *kvm)
1436 debugfs_create_file("radix", 0400, kvm->arch.debugfs_dir, kvm,
1437 &debugfs_radix_fops);
1440 int kvmppc_radix_init(void)
1442 unsigned long size = sizeof(void *) << RADIX_PTE_INDEX_SIZE;
1444 kvm_pte_cache = kmem_cache_create("kvm-pte", size, size, 0, pte_ctor);
1448 size = sizeof(void *) << RADIX_PMD_INDEX_SIZE;
1450 kvm_pmd_cache = kmem_cache_create("kvm-pmd", size, size, 0, pmd_ctor);
1451 if (!kvm_pmd_cache) {
1452 kmem_cache_destroy(kvm_pte_cache);
1459 void kvmppc_radix_exit(void)
1461 kmem_cache_destroy(kvm_pte_cache);
1462 kmem_cache_destroy(kvm_pmd_cache);