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>
24 #include <asm/plpar_wrappers.h>
25 #include <asm/firmware.h>
28 * Supported radix tree geometry.
29 * Like p9, we support either 5 or 9 bits at the first (lowest) level,
30 * for a page size of 64k or 4k.
32 static int p9_supported_radix_bits[4] = { 5, 9, 9, 13 };
34 unsigned long __kvmhv_copy_tofrom_guest_radix(int lpid, int pid,
35 gva_t eaddr, void *to, void *from,
38 int old_pid, old_lpid;
39 unsigned long quadrant, ret = n;
42 /* Can't access quadrants 1 or 2 in non-HV mode, call the HV to do it */
43 if (kvmhv_on_pseries())
44 return plpar_hcall_norets(H_COPY_TOFROM_GUEST, lpid, pid, eaddr,
45 (to != NULL) ? __pa(to): 0,
46 (from != NULL) ? __pa(from): 0, n);
48 if (eaddr & (0xFFFUL << 52))
55 from = (void *) (eaddr | (quadrant << 62));
57 to = (void *) (eaddr | (quadrant << 62));
61 asm volatile("hwsync" ::: "memory");
63 /* switch the lpid first to avoid running host with unallocated pid */
64 old_lpid = mfspr(SPRN_LPID);
66 mtspr(SPRN_LPID, lpid);
68 old_pid = mfspr(SPRN_PID);
76 ret = __copy_from_user_inatomic(to, (const void __user *)from, n);
78 ret = __copy_to_user_inatomic((void __user *)to, from, n);
81 asm volatile("hwsync" ::: "memory");
83 /* switch the pid first to avoid running host with unallocated pid */
84 if (quadrant == 1 && pid != old_pid)
85 mtspr(SPRN_PID, old_pid);
87 mtspr(SPRN_LPID, old_lpid);
95 static long kvmhv_copy_tofrom_guest_radix(struct kvm_vcpu *vcpu, gva_t eaddr,
96 void *to, void *from, unsigned long n)
98 int lpid = vcpu->kvm->arch.lpid;
99 int pid = vcpu->arch.pid;
101 /* This would cause a data segment intr so don't allow the access */
102 if (eaddr & (0x3FFUL << 52))
105 /* Should we be using the nested lpid */
106 if (vcpu->arch.nested)
107 lpid = vcpu->arch.nested->shadow_lpid;
109 /* If accessing quadrant 3 then pid is expected to be 0 */
110 if (((eaddr >> 62) & 0x3) == 0x3)
113 eaddr &= ~(0xFFFUL << 52);
115 return __kvmhv_copy_tofrom_guest_radix(lpid, pid, eaddr, to, from, n);
118 long kvmhv_copy_from_guest_radix(struct kvm_vcpu *vcpu, gva_t eaddr, void *to,
123 ret = kvmhv_copy_tofrom_guest_radix(vcpu, eaddr, to, NULL, n);
125 memset(to + (n - ret), 0, ret);
130 long kvmhv_copy_to_guest_radix(struct kvm_vcpu *vcpu, gva_t eaddr, void *from,
133 return kvmhv_copy_tofrom_guest_radix(vcpu, eaddr, NULL, from, n);
136 int kvmppc_mmu_walk_radix_tree(struct kvm_vcpu *vcpu, gva_t eaddr,
137 struct kvmppc_pte *gpte, u64 root,
140 struct kvm *kvm = vcpu->kvm;
142 unsigned long rts, bits, offset, index;
146 rts = ((root & RTS1_MASK) >> (RTS1_SHIFT - 3)) |
147 ((root & RTS2_MASK) >> RTS2_SHIFT);
148 bits = root & RPDS_MASK;
149 base = root & RPDB_MASK;
153 /* Current implementations only support 52-bit space */
157 /* Walk each level of the radix tree */
158 for (level = 3; level >= 0; --level) {
160 /* Check a valid size */
161 if (level && bits != p9_supported_radix_bits[level])
163 if (level == 0 && !(bits == 5 || bits == 9))
166 index = (eaddr >> offset) & ((1UL << bits) - 1);
167 /* Check that low bits of page table base are zero */
168 if (base & ((1UL << (bits + 3)) - 1))
170 /* Read the entry from guest memory */
171 addr = base + (index * sizeof(rpte));
173 kvm_vcpu_srcu_read_lock(vcpu);
174 ret = kvm_read_guest(kvm, addr, &rpte, sizeof(rpte));
175 kvm_vcpu_srcu_read_unlock(vcpu);
181 pte = __be64_to_cpu(rpte);
182 if (!(pte & _PAGE_PRESENT))
184 /* Check if a leaf entry */
187 /* Get ready to walk the next level */
188 base = pte & RPDB_MASK;
189 bits = pte & RPDS_MASK;
192 /* Need a leaf at lowest level; 512GB pages not supported */
193 if (level < 0 || level == 3)
196 /* We found a valid leaf PTE */
197 /* Offset is now log base 2 of the page size */
198 gpa = pte & 0x01fffffffffff000ul;
199 if (gpa & ((1ul << offset) - 1))
201 gpa |= eaddr & ((1ul << offset) - 1);
202 for (ps = MMU_PAGE_4K; ps < MMU_PAGE_COUNT; ++ps)
203 if (offset == mmu_psize_defs[ps].shift)
205 gpte->page_size = ps;
206 gpte->page_shift = offset;
211 /* Work out permissions */
212 gpte->may_read = !!(pte & _PAGE_READ);
213 gpte->may_write = !!(pte & _PAGE_WRITE);
214 gpte->may_execute = !!(pte & _PAGE_EXEC);
216 gpte->rc = pte & (_PAGE_ACCESSED | _PAGE_DIRTY);
225 * Used to walk a partition or process table radix tree in guest memory
226 * Note: We exploit the fact that a partition table and a process
227 * table have the same layout, a partition-scoped page table and a
228 * process-scoped page table have the same layout, and the 2nd
229 * doubleword of a partition table entry has the same layout as
232 int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr,
233 struct kvmppc_pte *gpte, u64 table,
234 int table_index, u64 *pte_ret_p)
236 struct kvm *kvm = vcpu->kvm;
238 unsigned long size, ptbl, root;
239 struct prtb_entry entry;
241 if ((table & PRTS_MASK) > 24)
243 size = 1ul << ((table & PRTS_MASK) + 12);
245 /* Is the table big enough to contain this entry? */
246 if ((table_index * sizeof(entry)) >= size)
249 /* Read the table to find the root of the radix tree */
250 ptbl = (table & PRTB_MASK) + (table_index * sizeof(entry));
251 kvm_vcpu_srcu_read_lock(vcpu);
252 ret = kvm_read_guest(kvm, ptbl, &entry, sizeof(entry));
253 kvm_vcpu_srcu_read_unlock(vcpu);
257 /* Root is stored in the first double word */
258 root = be64_to_cpu(entry.prtb0);
260 return kvmppc_mmu_walk_radix_tree(vcpu, eaddr, gpte, root, pte_ret_p);
263 int kvmppc_mmu_radix_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
264 struct kvmppc_pte *gpte, bool data, bool iswrite)
270 /* Work out effective PID */
271 switch (eaddr >> 62) {
273 pid = vcpu->arch.pid;
282 ret = kvmppc_mmu_radix_translate_table(vcpu, eaddr, gpte,
283 vcpu->kvm->arch.process_table, pid, &pte);
287 /* Check privilege (applies only to process scoped translations) */
288 if (kvmppc_get_msr(vcpu) & MSR_PR) {
289 if (pte & _PAGE_PRIVILEGED) {
292 gpte->may_execute = 0;
295 if (!(pte & _PAGE_PRIVILEGED)) {
296 /* Check AMR/IAMR to see if strict mode is in force */
297 if (vcpu->arch.amr & (1ul << 62))
299 if (vcpu->arch.amr & (1ul << 63))
301 if (vcpu->arch.iamr & (1ul << 62))
302 gpte->may_execute = 0;
309 void kvmppc_radix_tlbie_page(struct kvm *kvm, unsigned long addr,
310 unsigned int pshift, unsigned int lpid)
312 unsigned long psize = PAGE_SIZE;
318 psize = 1UL << pshift;
322 addr &= ~(psize - 1);
324 if (!kvmhv_on_pseries()) {
325 radix__flush_tlb_lpid_page(lpid, addr, psize);
329 psi = shift_to_mmu_psize(pshift);
331 if (!firmware_has_feature(FW_FEATURE_RPT_INVALIDATE)) {
332 rb = addr | (mmu_get_ap(psi) << PPC_BITLSHIFT(58));
333 rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(0, 0, 1),
336 rc = pseries_rpt_invalidate(lpid, H_RPTI_TARGET_CMMU,
339 psize_to_rpti_pgsize(psi),
344 pr_err("KVM: TLB page invalidation hcall failed, rc=%ld\n", rc);
347 static void kvmppc_radix_flush_pwc(struct kvm *kvm, unsigned int lpid)
351 if (!kvmhv_on_pseries()) {
352 radix__flush_pwc_lpid(lpid);
356 if (!firmware_has_feature(FW_FEATURE_RPT_INVALIDATE))
357 rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(1, 0, 1),
358 lpid, TLBIEL_INVAL_SET_LPID);
360 rc = pseries_rpt_invalidate(lpid, H_RPTI_TARGET_CMMU,
362 H_RPTI_TYPE_PWC, H_RPTI_PAGE_ALL,
365 pr_err("KVM: TLB PWC invalidation hcall failed, rc=%ld\n", rc);
368 static unsigned long kvmppc_radix_update_pte(struct kvm *kvm, pte_t *ptep,
369 unsigned long clr, unsigned long set,
370 unsigned long addr, unsigned int shift)
372 return __radix_pte_update(ptep, clr, set);
375 static void kvmppc_radix_set_pte_at(struct kvm *kvm, unsigned long addr,
376 pte_t *ptep, pte_t pte)
378 radix__set_pte_at(kvm->mm, addr, ptep, pte, 0);
381 static struct kmem_cache *kvm_pte_cache;
382 static struct kmem_cache *kvm_pmd_cache;
384 static pte_t *kvmppc_pte_alloc(void)
388 pte = kmem_cache_alloc(kvm_pte_cache, GFP_KERNEL);
389 /* pmd_populate() will only reference _pa(pte). */
390 kmemleak_ignore(pte);
395 static void kvmppc_pte_free(pte_t *ptep)
397 kmem_cache_free(kvm_pte_cache, ptep);
400 static pmd_t *kvmppc_pmd_alloc(void)
404 pmd = kmem_cache_alloc(kvm_pmd_cache, GFP_KERNEL);
405 /* pud_populate() will only reference _pa(pmd). */
406 kmemleak_ignore(pmd);
411 static void kvmppc_pmd_free(pmd_t *pmdp)
413 kmem_cache_free(kvm_pmd_cache, pmdp);
416 /* Called with kvm->mmu_lock held */
417 void kvmppc_unmap_pte(struct kvm *kvm, pte_t *pte, unsigned long gpa,
419 const struct kvm_memory_slot *memslot,
424 unsigned long gfn = gpa >> PAGE_SHIFT;
425 unsigned long page_size = PAGE_SIZE;
428 old = kvmppc_radix_update_pte(kvm, pte, ~0UL, 0, gpa, shift);
429 kvmppc_radix_tlbie_page(kvm, gpa, shift, lpid);
431 /* The following only applies to L1 entries */
432 if (lpid != kvm->arch.lpid)
436 memslot = gfn_to_memslot(kvm, gfn);
440 if (shift) { /* 1GB or 2MB page */
441 page_size = 1ul << shift;
442 if (shift == PMD_SHIFT)
443 kvm->stat.num_2M_pages--;
444 else if (shift == PUD_SHIFT)
445 kvm->stat.num_1G_pages--;
448 gpa &= ~(page_size - 1);
449 hpa = old & PTE_RPN_MASK;
450 kvmhv_remove_nest_rmap_range(kvm, memslot, gpa, hpa, page_size);
452 if ((old & _PAGE_DIRTY) && memslot->dirty_bitmap)
453 kvmppc_update_dirty_map(memslot, gfn, page_size);
457 * kvmppc_free_p?d are used to free existing page tables, and recursively
458 * descend and clear and free children.
459 * Callers are responsible for flushing the PWC.
461 * When page tables are being unmapped/freed as part of page fault path
462 * (full == false), valid ptes are generally not expected; however, there
463 * is one situation where they arise, which is when dirty page logging is
464 * turned off for a memslot while the VM is running. The new memslot
465 * becomes visible to page faults before the memslot commit function
466 * gets to flush the memslot, which can lead to a 2MB page mapping being
467 * installed for a guest physical address where there are already 64kB
468 * (or 4kB) mappings (of sub-pages of the same 2MB page).
470 static void kvmppc_unmap_free_pte(struct kvm *kvm, pte_t *pte, bool full,
474 memset(pte, 0, sizeof(long) << RADIX_PTE_INDEX_SIZE);
479 for (it = 0; it < PTRS_PER_PTE; ++it, ++p) {
480 if (pte_val(*p) == 0)
482 kvmppc_unmap_pte(kvm, p,
483 pte_pfn(*p) << PAGE_SHIFT,
484 PAGE_SHIFT, NULL, lpid);
488 kvmppc_pte_free(pte);
491 static void kvmppc_unmap_free_pmd(struct kvm *kvm, pmd_t *pmd, bool full,
497 for (im = 0; im < PTRS_PER_PMD; ++im, ++p) {
498 if (!pmd_present(*p))
500 if (pmd_is_leaf(*p)) {
505 kvmppc_unmap_pte(kvm, (pte_t *)p,
506 pte_pfn(*(pte_t *)p) << PAGE_SHIFT,
507 PMD_SHIFT, NULL, lpid);
512 pte = pte_offset_map(p, 0);
513 kvmppc_unmap_free_pte(kvm, pte, full, lpid);
517 kvmppc_pmd_free(pmd);
520 static void kvmppc_unmap_free_pud(struct kvm *kvm, pud_t *pud,
526 for (iu = 0; iu < PTRS_PER_PUD; ++iu, ++p) {
527 if (!pud_present(*p))
529 if (pud_is_leaf(*p)) {
534 pmd = pmd_offset(p, 0);
535 kvmppc_unmap_free_pmd(kvm, pmd, true, lpid);
539 pud_free(kvm->mm, pud);
542 void kvmppc_free_pgtable_radix(struct kvm *kvm, pgd_t *pgd, unsigned int lpid)
546 for (ig = 0; ig < PTRS_PER_PGD; ++ig, ++pgd) {
547 p4d_t *p4d = p4d_offset(pgd, 0);
550 if (!p4d_present(*p4d))
552 pud = pud_offset(p4d, 0);
553 kvmppc_unmap_free_pud(kvm, pud, lpid);
558 void kvmppc_free_radix(struct kvm *kvm)
560 if (kvm->arch.pgtable) {
561 kvmppc_free_pgtable_radix(kvm, kvm->arch.pgtable,
563 pgd_free(kvm->mm, kvm->arch.pgtable);
564 kvm->arch.pgtable = NULL;
568 static void kvmppc_unmap_free_pmd_entry_table(struct kvm *kvm, pmd_t *pmd,
569 unsigned long gpa, unsigned int lpid)
571 pte_t *pte = pte_offset_kernel(pmd, 0);
574 * Clearing the pmd entry then flushing the PWC ensures that the pte
575 * page no longer be cached by the MMU, so can be freed without
576 * flushing the PWC again.
579 kvmppc_radix_flush_pwc(kvm, lpid);
581 kvmppc_unmap_free_pte(kvm, pte, false, lpid);
584 static void kvmppc_unmap_free_pud_entry_table(struct kvm *kvm, pud_t *pud,
585 unsigned long gpa, unsigned int lpid)
587 pmd_t *pmd = pmd_offset(pud, 0);
590 * Clearing the pud entry then flushing the PWC ensures that the pmd
591 * page and any children pte pages will no longer be cached by the MMU,
592 * so can be freed without flushing the PWC again.
595 kvmppc_radix_flush_pwc(kvm, lpid);
597 kvmppc_unmap_free_pmd(kvm, pmd, false, lpid);
601 * There are a number of bits which may differ between different faults to
602 * the same partition scope entry. RC bits, in the course of cleaning and
603 * aging. And the write bit can change, either the access could have been
604 * upgraded, or a read fault could happen concurrently with a write fault
605 * that sets those bits first.
607 #define PTE_BITS_MUST_MATCH (~(_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED))
609 int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
610 unsigned long gpa, unsigned int level,
611 unsigned long mmu_seq, unsigned int lpid,
612 unsigned long *rmapp, struct rmap_nested **n_rmap)
616 pud_t *pud, *new_pud = NULL;
617 pmd_t *pmd, *new_pmd = NULL;
618 pte_t *ptep, *new_ptep = NULL;
621 /* Traverse the guest's 2nd-level tree, allocate new levels needed */
622 pgd = pgtable + pgd_index(gpa);
623 p4d = p4d_offset(pgd, gpa);
626 if (p4d_present(*p4d))
627 pud = pud_offset(p4d, gpa);
629 new_pud = pud_alloc_one(kvm->mm, gpa);
632 if (pud && pud_present(*pud) && !pud_is_leaf(*pud))
633 pmd = pmd_offset(pud, gpa);
635 new_pmd = kvmppc_pmd_alloc();
637 if (level == 0 && !(pmd && pmd_present(*pmd) && !pmd_is_leaf(*pmd)))
638 new_ptep = kvmppc_pte_alloc();
640 /* Check if we might have been invalidated; let the guest retry if so */
641 spin_lock(&kvm->mmu_lock);
643 if (mmu_notifier_retry(kvm, mmu_seq))
646 /* Now traverse again under the lock and change the tree */
648 if (p4d_none(*p4d)) {
651 p4d_populate(kvm->mm, p4d, new_pud);
654 pud = pud_offset(p4d, gpa);
655 if (pud_is_leaf(*pud)) {
656 unsigned long hgpa = gpa & PUD_MASK;
658 /* Check if we raced and someone else has set the same thing */
660 if (pud_raw(*pud) == pte_raw(pte)) {
664 /* Valid 1GB page here already, add our extra bits */
665 WARN_ON_ONCE((pud_val(*pud) ^ pte_val(pte)) &
666 PTE_BITS_MUST_MATCH);
667 kvmppc_radix_update_pte(kvm, (pte_t *)pud,
668 0, pte_val(pte), hgpa, PUD_SHIFT);
673 * If we raced with another CPU which has just put
674 * a 1GB pte in after we saw a pmd page, try again.
680 /* Valid 1GB page here already, remove it */
681 kvmppc_unmap_pte(kvm, (pte_t *)pud, hgpa, PUD_SHIFT, NULL,
685 if (!pud_none(*pud)) {
687 * There's a page table page here, but we wanted to
688 * install a large page, so remove and free the page
691 kvmppc_unmap_free_pud_entry_table(kvm, pud, gpa, lpid);
693 kvmppc_radix_set_pte_at(kvm, gpa, (pte_t *)pud, pte);
695 kvmhv_insert_nest_rmap(kvm, rmapp, n_rmap);
699 if (pud_none(*pud)) {
702 pud_populate(kvm->mm, pud, new_pmd);
705 pmd = pmd_offset(pud, gpa);
706 if (pmd_is_leaf(*pmd)) {
707 unsigned long lgpa = gpa & PMD_MASK;
709 /* Check if we raced and someone else has set the same thing */
711 if (pmd_raw(*pmd) == pte_raw(pte)) {
715 /* Valid 2MB page here already, add our extra bits */
716 WARN_ON_ONCE((pmd_val(*pmd) ^ pte_val(pte)) &
717 PTE_BITS_MUST_MATCH);
718 kvmppc_radix_update_pte(kvm, pmdp_ptep(pmd),
719 0, pte_val(pte), lgpa, PMD_SHIFT);
725 * If we raced with another CPU which has just put
726 * a 2MB pte in after we saw a pte page, try again.
732 /* Valid 2MB page here already, remove it */
733 kvmppc_unmap_pte(kvm, pmdp_ptep(pmd), lgpa, PMD_SHIFT, NULL,
737 if (!pmd_none(*pmd)) {
739 * There's a page table page here, but we wanted to
740 * install a large page, so remove and free the page
743 kvmppc_unmap_free_pmd_entry_table(kvm, pmd, gpa, lpid);
745 kvmppc_radix_set_pte_at(kvm, gpa, pmdp_ptep(pmd), pte);
747 kvmhv_insert_nest_rmap(kvm, rmapp, n_rmap);
751 if (pmd_none(*pmd)) {
754 pmd_populate(kvm->mm, pmd, new_ptep);
757 ptep = pte_offset_kernel(pmd, gpa);
758 if (pte_present(*ptep)) {
759 /* Check if someone else set the same thing */
760 if (pte_raw(*ptep) == pte_raw(pte)) {
764 /* Valid page here already, add our extra bits */
765 WARN_ON_ONCE((pte_val(*ptep) ^ pte_val(pte)) &
766 PTE_BITS_MUST_MATCH);
767 kvmppc_radix_update_pte(kvm, ptep, 0, pte_val(pte), gpa, 0);
771 kvmppc_radix_set_pte_at(kvm, gpa, ptep, pte);
773 kvmhv_insert_nest_rmap(kvm, rmapp, n_rmap);
777 spin_unlock(&kvm->mmu_lock);
779 pud_free(kvm->mm, new_pud);
781 kvmppc_pmd_free(new_pmd);
783 kvmppc_pte_free(new_ptep);
787 bool kvmppc_hv_handle_set_rc(struct kvm *kvm, bool nested, bool writing,
788 unsigned long gpa, unsigned int lpid)
790 unsigned long pgflags;
795 * Need to set an R or C bit in the 2nd-level tables;
796 * since we are just helping out the hardware here,
797 * it is sufficient to do what the hardware does.
799 pgflags = _PAGE_ACCESSED;
801 pgflags |= _PAGE_DIRTY;
804 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
806 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
808 if (ptep && pte_present(*ptep) && (!writing || pte_write(*ptep))) {
809 kvmppc_radix_update_pte(kvm, ptep, 0, pgflags, gpa, shift);
815 int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu,
817 struct kvm_memory_slot *memslot,
818 bool writing, bool kvm_ro,
819 pte_t *inserted_pte, unsigned int *levelp)
821 struct kvm *kvm = vcpu->kvm;
822 struct page *page = NULL;
823 unsigned long mmu_seq;
824 unsigned long hva, gfn = gpa >> PAGE_SHIFT;
825 bool upgrade_write = false;
826 bool *upgrade_p = &upgrade_write;
828 unsigned int shift, level;
832 /* used to check for invalidations in progress */
833 mmu_seq = kvm->mmu_notifier_seq;
837 * Do a fast check first, since __gfn_to_pfn_memslot doesn't
838 * do it with !atomic && !async, which is how we call it.
839 * We always ask for write permission since the common case
840 * is that the page is writable.
842 hva = gfn_to_hva_memslot(memslot, gfn);
843 if (!kvm_ro && get_user_page_fast_only(hva, FOLL_WRITE, &page)) {
844 upgrade_write = true;
848 /* Call KVM generic code to do the slow-path check */
849 pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL,
850 writing, upgrade_p, NULL);
851 if (is_error_noslot_pfn(pfn))
854 if (pfn_valid(pfn)) {
855 page = pfn_to_page(pfn);
856 if (PageReserved(page))
862 * Read the PTE from the process' radix tree and use that
863 * so we get the shift and attribute bits.
865 spin_lock(&kvm->mmu_lock);
866 ptep = find_kvm_host_pte(kvm, mmu_seq, hva, &shift);
869 pte = READ_ONCE(*ptep);
870 spin_unlock(&kvm->mmu_lock);
872 * If the PTE disappeared temporarily due to a THP
873 * collapse, just return and let the guest try again.
875 if (!pte_present(pte)) {
881 /* If we're logging dirty pages, always map single pages */
882 large_enable = !(memslot->flags & KVM_MEM_LOG_DIRTY_PAGES);
884 /* Get pte level from shift/size */
885 if (large_enable && shift == PUD_SHIFT &&
886 (gpa & (PUD_SIZE - PAGE_SIZE)) ==
887 (hva & (PUD_SIZE - PAGE_SIZE))) {
889 } else if (large_enable && shift == PMD_SHIFT &&
890 (gpa & (PMD_SIZE - PAGE_SIZE)) ==
891 (hva & (PMD_SIZE - PAGE_SIZE))) {
895 if (shift > PAGE_SHIFT) {
897 * If the pte maps more than one page, bring over
898 * bits from the virtual address to get the real
899 * address of the specific single page we want.
901 unsigned long rpnmask = (1ul << shift) - PAGE_SIZE;
902 pte = __pte(pte_val(pte) | (hva & rpnmask));
906 pte = __pte(pte_val(pte) | _PAGE_EXEC | _PAGE_ACCESSED);
907 if (writing || upgrade_write) {
908 if (pte_val(pte) & _PAGE_WRITE)
909 pte = __pte(pte_val(pte) | _PAGE_DIRTY);
911 pte = __pte(pte_val(pte) & ~(_PAGE_WRITE | _PAGE_DIRTY));
914 /* Allocate space in the tree and write the PTE */
915 ret = kvmppc_create_pte(kvm, kvm->arch.pgtable, pte, gpa, level,
916 mmu_seq, kvm->arch.lpid, NULL, NULL);
923 if (!ret && (pte_val(pte) & _PAGE_WRITE))
924 set_page_dirty_lock(page);
928 /* Increment number of large pages if we (successfully) inserted one */
931 kvm->stat.num_2M_pages++;
933 kvm->stat.num_1G_pages++;
939 int kvmppc_book3s_radix_page_fault(struct kvm_vcpu *vcpu,
940 unsigned long ea, unsigned long dsisr)
942 struct kvm *kvm = vcpu->kvm;
943 unsigned long gpa, gfn;
944 struct kvm_memory_slot *memslot;
946 bool writing = !!(dsisr & DSISR_ISSTORE);
949 /* Check for unusual errors */
950 if (dsisr & DSISR_UNSUPP_MMU) {
951 pr_err("KVM: Got unsupported MMU fault\n");
954 if (dsisr & DSISR_BADACCESS) {
955 /* Reflect to the guest as DSI */
956 pr_err("KVM: Got radix HV page fault with DSISR=%lx\n", dsisr);
957 kvmppc_core_queue_data_storage(vcpu, ea, dsisr);
961 /* Translate the logical address */
962 gpa = vcpu->arch.fault_gpa & ~0xfffUL;
963 gpa &= ~0xF000000000000000ul;
964 gfn = gpa >> PAGE_SHIFT;
965 if (!(dsisr & DSISR_PRTABLE_FAULT))
968 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
969 return kvmppc_send_page_to_uv(kvm, gfn);
971 /* Get the corresponding memslot */
972 memslot = gfn_to_memslot(kvm, gfn);
974 /* No memslot means it's an emulated MMIO region */
975 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
976 if (dsisr & (DSISR_PRTABLE_FAULT | DSISR_BADACCESS |
979 * Bad address in guest page table tree, or other
980 * unusual error - reflect it to the guest as DSI.
982 kvmppc_core_queue_data_storage(vcpu, ea, dsisr);
985 return kvmppc_hv_emulate_mmio(vcpu, gpa, ea, writing);
988 if (memslot->flags & KVM_MEM_READONLY) {
990 /* give the guest a DSI */
991 kvmppc_core_queue_data_storage(vcpu, ea, DSISR_ISSTORE |
998 /* Failed to set the reference/change bits */
999 if (dsisr & DSISR_SET_RC) {
1000 spin_lock(&kvm->mmu_lock);
1001 if (kvmppc_hv_handle_set_rc(kvm, false, writing,
1002 gpa, kvm->arch.lpid))
1003 dsisr &= ~DSISR_SET_RC;
1004 spin_unlock(&kvm->mmu_lock);
1006 if (!(dsisr & (DSISR_BAD_FAULT_64S | DSISR_NOHPTE |
1007 DSISR_PROTFAULT | DSISR_SET_RC)))
1008 return RESUME_GUEST;
1011 /* Try to insert a pte */
1012 ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot, writing,
1013 kvm_ro, NULL, NULL);
1015 if (ret == 0 || ret == -EAGAIN)
1020 /* Called with kvm->mmu_lock held */
1021 void kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
1025 unsigned long gpa = gfn << PAGE_SHIFT;
1028 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE) {
1029 uv_page_inval(kvm->arch.lpid, gpa, PAGE_SHIFT);
1033 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
1034 if (ptep && pte_present(*ptep))
1035 kvmppc_unmap_pte(kvm, ptep, gpa, shift, memslot,
1039 /* Called with kvm->mmu_lock held */
1040 bool kvm_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
1044 unsigned long gpa = gfn << PAGE_SHIFT;
1047 unsigned long old, *rmapp;
1049 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
1052 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
1053 if (ptep && pte_present(*ptep) && pte_young(*ptep)) {
1054 old = kvmppc_radix_update_pte(kvm, ptep, _PAGE_ACCESSED, 0,
1056 /* XXX need to flush tlb here? */
1057 /* Also clear bit in ptes in shadow pgtable for nested guests */
1058 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1059 kvmhv_update_nest_rmap_rc_list(kvm, rmapp, _PAGE_ACCESSED, 0,
1067 /* Called with kvm->mmu_lock held */
1068 bool kvm_test_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
1073 unsigned long gpa = gfn << PAGE_SHIFT;
1077 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
1080 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
1081 if (ptep && pte_present(*ptep) && pte_young(*ptep))
1086 /* Returns the number of PAGE_SIZE pages that are dirty */
1087 static int kvm_radix_test_clear_dirty(struct kvm *kvm,
1088 struct kvm_memory_slot *memslot, int pagenum)
1090 unsigned long gfn = memslot->base_gfn + pagenum;
1091 unsigned long gpa = gfn << PAGE_SHIFT;
1095 unsigned long old, *rmapp;
1097 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
1101 * For performance reasons we don't hold kvm->mmu_lock while walking the
1102 * partition scoped table.
1104 ptep = find_kvm_secondary_pte_unlocked(kvm, gpa, &shift);
1108 pte = READ_ONCE(*ptep);
1109 if (pte_present(pte) && pte_dirty(pte)) {
1110 spin_lock(&kvm->mmu_lock);
1112 * Recheck the pte again
1114 if (pte_val(pte) != pte_val(*ptep)) {
1116 * We have KVM_MEM_LOG_DIRTY_PAGES enabled. Hence we can
1117 * only find PAGE_SIZE pte entries here. We can continue
1118 * to use the pte addr returned by above page table
1121 if (!pte_present(*ptep) || !pte_dirty(*ptep)) {
1122 spin_unlock(&kvm->mmu_lock);
1129 old = kvmppc_radix_update_pte(kvm, ptep, _PAGE_DIRTY, 0,
1131 kvmppc_radix_tlbie_page(kvm, gpa, shift, kvm->arch.lpid);
1132 /* Also clear bit in ptes in shadow pgtable for nested guests */
1133 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1134 kvmhv_update_nest_rmap_rc_list(kvm, rmapp, _PAGE_DIRTY, 0,
1137 spin_unlock(&kvm->mmu_lock);
1142 long kvmppc_hv_get_dirty_log_radix(struct kvm *kvm,
1143 struct kvm_memory_slot *memslot, unsigned long *map)
1148 for (i = 0; i < memslot->npages; i = j) {
1149 npages = kvm_radix_test_clear_dirty(kvm, memslot, i);
1152 * Note that if npages > 0 then i must be a multiple of npages,
1153 * since huge pages are only used to back the guest at guest
1154 * real addresses that are a multiple of their size.
1155 * Since we have at most one PTE covering any given guest
1156 * real address, if npages > 1 we can skip to i + npages.
1160 set_dirty_bits(map, i, npages);
1167 void kvmppc_radix_flush_memslot(struct kvm *kvm,
1168 const struct kvm_memory_slot *memslot)
1175 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START)
1176 kvmppc_uvmem_drop_pages(memslot, kvm, true);
1178 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
1181 gpa = memslot->base_gfn << PAGE_SHIFT;
1182 spin_lock(&kvm->mmu_lock);
1183 for (n = memslot->npages; n; --n) {
1184 ptep = find_kvm_secondary_pte(kvm, gpa, &shift);
1185 if (ptep && pte_present(*ptep))
1186 kvmppc_unmap_pte(kvm, ptep, gpa, shift, memslot,
1191 * Increase the mmu notifier sequence number to prevent any page
1192 * fault that read the memslot earlier from writing a PTE.
1194 kvm->mmu_notifier_seq++;
1195 spin_unlock(&kvm->mmu_lock);
1198 static void add_rmmu_ap_encoding(struct kvm_ppc_rmmu_info *info,
1199 int psize, int *indexp)
1201 if (!mmu_psize_defs[psize].shift)
1203 info->ap_encodings[*indexp] = mmu_psize_defs[psize].shift |
1204 (mmu_psize_defs[psize].ap << 29);
1208 int kvmhv_get_rmmu_info(struct kvm *kvm, struct kvm_ppc_rmmu_info *info)
1212 if (!radix_enabled())
1214 memset(info, 0, sizeof(*info));
1217 info->geometries[0].page_shift = 12;
1218 info->geometries[0].level_bits[0] = 9;
1219 for (i = 1; i < 4; ++i)
1220 info->geometries[0].level_bits[i] = p9_supported_radix_bits[i];
1222 info->geometries[1].page_shift = 16;
1223 for (i = 0; i < 4; ++i)
1224 info->geometries[1].level_bits[i] = p9_supported_radix_bits[i];
1227 add_rmmu_ap_encoding(info, MMU_PAGE_4K, &i);
1228 add_rmmu_ap_encoding(info, MMU_PAGE_64K, &i);
1229 add_rmmu_ap_encoding(info, MMU_PAGE_2M, &i);
1230 add_rmmu_ap_encoding(info, MMU_PAGE_1G, &i);
1235 int kvmppc_init_vm_radix(struct kvm *kvm)
1237 kvm->arch.pgtable = pgd_alloc(kvm->mm);
1238 if (!kvm->arch.pgtable)
1243 static void pte_ctor(void *addr)
1245 memset(addr, 0, RADIX_PTE_TABLE_SIZE);
1248 static void pmd_ctor(void *addr)
1250 memset(addr, 0, RADIX_PMD_TABLE_SIZE);
1253 struct debugfs_radix_state {
1264 static int debugfs_radix_open(struct inode *inode, struct file *file)
1266 struct kvm *kvm = inode->i_private;
1267 struct debugfs_radix_state *p;
1269 p = kzalloc(sizeof(*p), GFP_KERNEL);
1275 mutex_init(&p->mutex);
1276 file->private_data = p;
1278 return nonseekable_open(inode, file);
1281 static int debugfs_radix_release(struct inode *inode, struct file *file)
1283 struct debugfs_radix_state *p = file->private_data;
1285 kvm_put_kvm(p->kvm);
1290 static ssize_t debugfs_radix_read(struct file *file, char __user *buf,
1291 size_t len, loff_t *ppos)
1293 struct debugfs_radix_state *p = file->private_data;
1299 struct kvm_nested_guest *nested;
1309 if (!kvm_is_radix(kvm))
1312 ret = mutex_lock_interruptible(&p->mutex);
1316 if (p->chars_left) {
1320 r = copy_to_user(buf, p->buf + p->buf_index, n);
1337 while (len != 0 && p->lpid >= 0) {
1338 if (gpa >= RADIX_PGTABLE_RANGE) {
1342 kvmhv_put_nested(nested);
1345 p->lpid = kvmhv_nested_next_lpid(kvm, p->lpid);
1352 pgt = kvm->arch.pgtable;
1354 nested = kvmhv_get_nested(kvm, p->lpid, false);
1356 gpa = RADIX_PGTABLE_RANGE;
1359 pgt = nested->shadow_pgtable;
1365 n = scnprintf(p->buf, sizeof(p->buf),
1366 "\nNested LPID %d: ", p->lpid);
1367 n += scnprintf(p->buf + n, sizeof(p->buf) - n,
1368 "pgdir: %lx\n", (unsigned long)pgt);
1373 pgdp = pgt + pgd_index(gpa);
1374 p4dp = p4d_offset(pgdp, gpa);
1375 p4d = READ_ONCE(*p4dp);
1376 if (!(p4d_val(p4d) & _PAGE_PRESENT)) {
1377 gpa = (gpa & P4D_MASK) + P4D_SIZE;
1381 pudp = pud_offset(&p4d, gpa);
1382 pud = READ_ONCE(*pudp);
1383 if (!(pud_val(pud) & _PAGE_PRESENT)) {
1384 gpa = (gpa & PUD_MASK) + PUD_SIZE;
1387 if (pud_val(pud) & _PAGE_PTE) {
1393 pmdp = pmd_offset(&pud, gpa);
1394 pmd = READ_ONCE(*pmdp);
1395 if (!(pmd_val(pmd) & _PAGE_PRESENT)) {
1396 gpa = (gpa & PMD_MASK) + PMD_SIZE;
1399 if (pmd_val(pmd) & _PAGE_PTE) {
1405 ptep = pte_offset_kernel(&pmd, gpa);
1406 pte = pte_val(READ_ONCE(*ptep));
1407 if (!(pte & _PAGE_PRESENT)) {
1413 n = scnprintf(p->buf, sizeof(p->buf),
1414 " %lx: %lx %d\n", gpa, pte, shift);
1415 gpa += 1ul << shift;
1420 r = copy_to_user(buf, p->buf, n);
1435 kvmhv_put_nested(nested);
1438 mutex_unlock(&p->mutex);
1442 static ssize_t debugfs_radix_write(struct file *file, const char __user *buf,
1443 size_t len, loff_t *ppos)
1448 static const struct file_operations debugfs_radix_fops = {
1449 .owner = THIS_MODULE,
1450 .open = debugfs_radix_open,
1451 .release = debugfs_radix_release,
1452 .read = debugfs_radix_read,
1453 .write = debugfs_radix_write,
1454 .llseek = generic_file_llseek,
1457 void kvmhv_radix_debugfs_init(struct kvm *kvm)
1459 debugfs_create_file("radix", 0400, kvm->debugfs_dentry, kvm,
1460 &debugfs_radix_fops);
1463 int kvmppc_radix_init(void)
1465 unsigned long size = sizeof(void *) << RADIX_PTE_INDEX_SIZE;
1467 kvm_pte_cache = kmem_cache_create("kvm-pte", size, size, 0, pte_ctor);
1471 size = sizeof(void *) << RADIX_PMD_INDEX_SIZE;
1473 kvm_pmd_cache = kmem_cache_create("kvm-pmd", size, size, 0, pmd_ctor);
1474 if (!kvm_pmd_cache) {
1475 kmem_cache_destroy(kvm_pte_cache);
1482 void kvmppc_radix_exit(void)
1484 kmem_cache_destroy(kvm_pte_cache);
1485 kmem_cache_destroy(kvm_pmd_cache);