1 // SPDX-License-Identifier: GPL-2.0-only
3 * Stand-alone page-table allocator for hyp stage-1 and guest stage-2.
4 * No bombay mix was harmed in the writing of this file.
6 * Copyright (C) 2020 Google LLC
7 * Author: Will Deacon <will@kernel.org>
10 #include <linux/bitfield.h>
11 #include <asm/kvm_pgtable.h>
12 #include <asm/stage2_pgtable.h>
15 #define KVM_PTE_TYPE BIT(1)
16 #define KVM_PTE_TYPE_BLOCK 0
17 #define KVM_PTE_TYPE_PAGE 1
18 #define KVM_PTE_TYPE_TABLE 1
20 #define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2)
22 #define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2)
23 #define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6)
24 #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO \
25 ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 2 : 3; })
26 #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW \
27 ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 0 : 1; })
28 #define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8)
29 #define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3
30 #define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10)
32 #define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2)
33 #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6)
34 #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7)
35 #define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8)
36 #define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3
37 #define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10)
39 #define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 51)
41 #define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55)
43 #define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54)
45 #define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54)
47 #define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \
48 KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \
49 KVM_PTE_LEAF_ATTR_HI_S2_XN)
51 #define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2)
52 #define KVM_MAX_OWNER_ID 1
55 * Used to indicate a pte for which a 'break-before-make' sequence is in
58 #define KVM_INVALID_PTE_LOCKED BIT(10)
60 struct kvm_pgtable_walk_data {
61 struct kvm_pgtable_walker *walker;
68 static bool kvm_phys_is_valid(u64 phys)
70 return phys < BIT(id_aa64mmfr0_parange_to_phys_shift(ID_AA64MMFR0_EL1_PARANGE_MAX));
73 static bool kvm_block_mapping_supported(const struct kvm_pgtable_visit_ctx *ctx, u64 phys)
75 u64 granule = kvm_granule_size(ctx->level);
77 if (!kvm_level_supports_block_mapping(ctx->level))
80 if (granule > (ctx->end - ctx->addr))
83 if (kvm_phys_is_valid(phys) && !IS_ALIGNED(phys, granule))
86 return IS_ALIGNED(ctx->addr, granule);
89 static u32 kvm_pgtable_idx(struct kvm_pgtable_walk_data *data, u32 level)
91 u64 shift = kvm_granule_shift(level);
92 u64 mask = BIT(PAGE_SHIFT - 3) - 1;
94 return (data->addr >> shift) & mask;
97 static u32 kvm_pgd_page_idx(struct kvm_pgtable *pgt, u64 addr)
99 u64 shift = kvm_granule_shift(pgt->start_level - 1); /* May underflow */
100 u64 mask = BIT(pgt->ia_bits) - 1;
102 return (addr & mask) >> shift;
105 static u32 kvm_pgd_pages(u32 ia_bits, u32 start_level)
107 struct kvm_pgtable pgt = {
109 .start_level = start_level,
112 return kvm_pgd_page_idx(&pgt, -1ULL) + 1;
115 static bool kvm_pte_table(kvm_pte_t pte, u32 level)
117 if (level == KVM_PGTABLE_MAX_LEVELS - 1)
120 if (!kvm_pte_valid(pte))
123 return FIELD_GET(KVM_PTE_TYPE, pte) == KVM_PTE_TYPE_TABLE;
126 static kvm_pte_t *kvm_pte_follow(kvm_pte_t pte, struct kvm_pgtable_mm_ops *mm_ops)
128 return mm_ops->phys_to_virt(kvm_pte_to_phys(pte));
131 static void kvm_clear_pte(kvm_pte_t *ptep)
133 WRITE_ONCE(*ptep, 0);
136 static kvm_pte_t kvm_init_table_pte(kvm_pte_t *childp, struct kvm_pgtable_mm_ops *mm_ops)
138 kvm_pte_t pte = kvm_phys_to_pte(mm_ops->virt_to_phys(childp));
140 pte |= FIELD_PREP(KVM_PTE_TYPE, KVM_PTE_TYPE_TABLE);
141 pte |= KVM_PTE_VALID;
145 static kvm_pte_t kvm_init_valid_leaf_pte(u64 pa, kvm_pte_t attr, u32 level)
147 kvm_pte_t pte = kvm_phys_to_pte(pa);
148 u64 type = (level == KVM_PGTABLE_MAX_LEVELS - 1) ? KVM_PTE_TYPE_PAGE :
151 pte |= attr & (KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI);
152 pte |= FIELD_PREP(KVM_PTE_TYPE, type);
153 pte |= KVM_PTE_VALID;
158 static kvm_pte_t kvm_init_invalid_leaf_owner(u8 owner_id)
160 return FIELD_PREP(KVM_INVALID_PTE_OWNER_MASK, owner_id);
163 static int kvm_pgtable_visitor_cb(struct kvm_pgtable_walk_data *data,
164 const struct kvm_pgtable_visit_ctx *ctx,
165 enum kvm_pgtable_walk_flags visit)
167 struct kvm_pgtable_walker *walker = data->walker;
169 /* Ensure the appropriate lock is held (e.g. RCU lock for stage-2 MMU) */
170 WARN_ON_ONCE(kvm_pgtable_walk_shared(ctx) && !kvm_pgtable_walk_lock_held());
171 return walker->cb(ctx, visit);
174 static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker,
178 * Visitor callbacks return EAGAIN when the conditions that led to a
179 * fault are no longer reflected in the page tables due to a race to
180 * update a PTE. In the context of a fault handler this is interpreted
181 * as a signal to retry guest execution.
183 * Ignore the return code altogether for walkers outside a fault handler
184 * (e.g. write protecting a range of memory) and chug along with the
188 return !(walker->flags & KVM_PGTABLE_WALK_HANDLE_FAULT);
193 static int __kvm_pgtable_walk(struct kvm_pgtable_walk_data *data,
194 struct kvm_pgtable_mm_ops *mm_ops, kvm_pteref_t pgtable, u32 level);
196 static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,
197 struct kvm_pgtable_mm_ops *mm_ops,
198 kvm_pteref_t pteref, u32 level)
200 enum kvm_pgtable_walk_flags flags = data->walker->flags;
201 kvm_pte_t *ptep = kvm_dereference_pteref(data->walker, pteref);
202 struct kvm_pgtable_visit_ctx ctx = {
204 .old = READ_ONCE(*ptep),
205 .arg = data->walker->arg,
207 .start = data->start,
215 bool table = kvm_pte_table(ctx.old, level);
217 if (table && (ctx.flags & KVM_PGTABLE_WALK_TABLE_PRE))
218 ret = kvm_pgtable_visitor_cb(data, &ctx, KVM_PGTABLE_WALK_TABLE_PRE);
220 if (!table && (ctx.flags & KVM_PGTABLE_WALK_LEAF)) {
221 ret = kvm_pgtable_visitor_cb(data, &ctx, KVM_PGTABLE_WALK_LEAF);
222 ctx.old = READ_ONCE(*ptep);
223 table = kvm_pte_table(ctx.old, level);
226 if (!kvm_pgtable_walk_continue(data->walker, ret))
230 data->addr = ALIGN_DOWN(data->addr, kvm_granule_size(level));
231 data->addr += kvm_granule_size(level);
235 childp = (kvm_pteref_t)kvm_pte_follow(ctx.old, mm_ops);
236 ret = __kvm_pgtable_walk(data, mm_ops, childp, level + 1);
237 if (!kvm_pgtable_walk_continue(data->walker, ret))
240 if (ctx.flags & KVM_PGTABLE_WALK_TABLE_POST)
241 ret = kvm_pgtable_visitor_cb(data, &ctx, KVM_PGTABLE_WALK_TABLE_POST);
244 if (kvm_pgtable_walk_continue(data->walker, ret))
250 static int __kvm_pgtable_walk(struct kvm_pgtable_walk_data *data,
251 struct kvm_pgtable_mm_ops *mm_ops, kvm_pteref_t pgtable, u32 level)
256 if (WARN_ON_ONCE(level >= KVM_PGTABLE_MAX_LEVELS))
259 for (idx = kvm_pgtable_idx(data, level); idx < PTRS_PER_PTE; ++idx) {
260 kvm_pteref_t pteref = &pgtable[idx];
262 if (data->addr >= data->end)
265 ret = __kvm_pgtable_visit(data, mm_ops, pteref, level);
273 static int _kvm_pgtable_walk(struct kvm_pgtable *pgt, struct kvm_pgtable_walk_data *data)
277 u64 limit = BIT(pgt->ia_bits);
279 if (data->addr > limit || data->end > limit)
285 for (idx = kvm_pgd_page_idx(pgt, data->addr); data->addr < data->end; ++idx) {
286 kvm_pteref_t pteref = &pgt->pgd[idx * PTRS_PER_PTE];
288 ret = __kvm_pgtable_walk(data, pgt->mm_ops, pteref, pgt->start_level);
296 int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size,
297 struct kvm_pgtable_walker *walker)
299 struct kvm_pgtable_walk_data walk_data = {
300 .start = ALIGN_DOWN(addr, PAGE_SIZE),
301 .addr = ALIGN_DOWN(addr, PAGE_SIZE),
302 .end = PAGE_ALIGN(walk_data.addr + size),
307 r = kvm_pgtable_walk_begin(walker);
311 r = _kvm_pgtable_walk(pgt, &walk_data);
312 kvm_pgtable_walk_end(walker);
317 struct leaf_walk_data {
322 static int leaf_walker(const struct kvm_pgtable_visit_ctx *ctx,
323 enum kvm_pgtable_walk_flags visit)
325 struct leaf_walk_data *data = ctx->arg;
327 data->pte = ctx->old;
328 data->level = ctx->level;
333 int kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr,
334 kvm_pte_t *ptep, u32 *level)
336 struct leaf_walk_data data;
337 struct kvm_pgtable_walker walker = {
339 .flags = KVM_PGTABLE_WALK_LEAF,
344 ret = kvm_pgtable_walk(pgt, ALIGN_DOWN(addr, PAGE_SIZE),
356 struct hyp_map_data {
361 static int hyp_set_prot_attr(enum kvm_pgtable_prot prot, kvm_pte_t *ptep)
363 bool device = prot & KVM_PGTABLE_PROT_DEVICE;
364 u32 mtype = device ? MT_DEVICE_nGnRE : MT_NORMAL;
365 kvm_pte_t attr = FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX, mtype);
366 u32 sh = KVM_PTE_LEAF_ATTR_LO_S1_SH_IS;
367 u32 ap = (prot & KVM_PGTABLE_PROT_W) ? KVM_PTE_LEAF_ATTR_LO_S1_AP_RW :
368 KVM_PTE_LEAF_ATTR_LO_S1_AP_RO;
370 if (!(prot & KVM_PGTABLE_PROT_R))
373 if (prot & KVM_PGTABLE_PROT_X) {
374 if (prot & KVM_PGTABLE_PROT_W)
380 attr |= KVM_PTE_LEAF_ATTR_HI_S1_XN;
383 attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_AP, ap);
384 attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_SH, sh);
385 attr |= KVM_PTE_LEAF_ATTR_LO_S1_AF;
386 attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW;
392 enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte)
394 enum kvm_pgtable_prot prot = pte & KVM_PTE_LEAF_ATTR_HI_SW;
397 if (!kvm_pte_valid(pte))
400 if (!(pte & KVM_PTE_LEAF_ATTR_HI_S1_XN))
401 prot |= KVM_PGTABLE_PROT_X;
403 ap = FIELD_GET(KVM_PTE_LEAF_ATTR_LO_S1_AP, pte);
404 if (ap == KVM_PTE_LEAF_ATTR_LO_S1_AP_RO)
405 prot |= KVM_PGTABLE_PROT_R;
406 else if (ap == KVM_PTE_LEAF_ATTR_LO_S1_AP_RW)
407 prot |= KVM_PGTABLE_PROT_RW;
412 static bool hyp_map_walker_try_leaf(const struct kvm_pgtable_visit_ctx *ctx,
413 struct hyp_map_data *data)
415 u64 phys = data->phys + (ctx->addr - ctx->start);
418 if (!kvm_block_mapping_supported(ctx, phys))
421 new = kvm_init_valid_leaf_pte(phys, data->attr, ctx->level);
424 if (!kvm_pte_valid(ctx->old))
425 ctx->mm_ops->get_page(ctx->ptep);
426 else if (WARN_ON((ctx->old ^ new) & ~KVM_PTE_LEAF_ATTR_HI_SW))
429 smp_store_release(ctx->ptep, new);
433 static int hyp_map_walker(const struct kvm_pgtable_visit_ctx *ctx,
434 enum kvm_pgtable_walk_flags visit)
436 kvm_pte_t *childp, new;
437 struct hyp_map_data *data = ctx->arg;
438 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
440 if (hyp_map_walker_try_leaf(ctx, data))
443 if (WARN_ON(ctx->level == KVM_PGTABLE_MAX_LEVELS - 1))
446 childp = (kvm_pte_t *)mm_ops->zalloc_page(NULL);
450 new = kvm_init_table_pte(childp, mm_ops);
451 mm_ops->get_page(ctx->ptep);
452 smp_store_release(ctx->ptep, new);
457 int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
458 enum kvm_pgtable_prot prot)
461 struct hyp_map_data map_data = {
462 .phys = ALIGN_DOWN(phys, PAGE_SIZE),
464 struct kvm_pgtable_walker walker = {
465 .cb = hyp_map_walker,
466 .flags = KVM_PGTABLE_WALK_LEAF,
470 ret = hyp_set_prot_attr(prot, &map_data.attr);
474 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
480 static int hyp_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,
481 enum kvm_pgtable_walk_flags visit)
483 kvm_pte_t *childp = NULL;
484 u64 granule = kvm_granule_size(ctx->level);
485 u64 *unmapped = ctx->arg;
486 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
488 if (!kvm_pte_valid(ctx->old))
491 if (kvm_pte_table(ctx->old, ctx->level)) {
492 childp = kvm_pte_follow(ctx->old, mm_ops);
494 if (mm_ops->page_count(childp) != 1)
497 kvm_clear_pte(ctx->ptep);
499 __tlbi_level(vae2is, __TLBI_VADDR(ctx->addr, 0), ctx->level);
501 if (ctx->end - ctx->addr < granule)
504 kvm_clear_pte(ctx->ptep);
506 __tlbi_level(vale2is, __TLBI_VADDR(ctx->addr, 0), ctx->level);
507 *unmapped += granule;
512 mm_ops->put_page(ctx->ptep);
515 mm_ops->put_page(childp);
520 u64 kvm_pgtable_hyp_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
523 struct kvm_pgtable_walker walker = {
524 .cb = hyp_unmap_walker,
526 .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
529 if (!pgt->mm_ops->page_count)
532 kvm_pgtable_walk(pgt, addr, size, &walker);
536 int kvm_pgtable_hyp_init(struct kvm_pgtable *pgt, u32 va_bits,
537 struct kvm_pgtable_mm_ops *mm_ops)
539 u64 levels = ARM64_HW_PGTABLE_LEVELS(va_bits);
541 pgt->pgd = (kvm_pteref_t)mm_ops->zalloc_page(NULL);
545 pgt->ia_bits = va_bits;
546 pgt->start_level = KVM_PGTABLE_MAX_LEVELS - levels;
547 pgt->mm_ops = mm_ops;
549 pgt->force_pte_cb = NULL;
554 static int hyp_free_walker(const struct kvm_pgtable_visit_ctx *ctx,
555 enum kvm_pgtable_walk_flags visit)
557 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
559 if (!kvm_pte_valid(ctx->old))
562 mm_ops->put_page(ctx->ptep);
564 if (kvm_pte_table(ctx->old, ctx->level))
565 mm_ops->put_page(kvm_pte_follow(ctx->old, mm_ops));
570 void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt)
572 struct kvm_pgtable_walker walker = {
573 .cb = hyp_free_walker,
574 .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
577 WARN_ON(kvm_pgtable_walk(pgt, 0, BIT(pgt->ia_bits), &walker));
578 pgt->mm_ops->put_page(kvm_dereference_pteref(&walker, pgt->pgd));
582 struct stage2_map_data {
590 struct kvm_s2_mmu *mmu;
593 /* Force mappings to page granularity */
597 u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift)
599 u64 vtcr = VTCR_EL2_FLAGS;
602 vtcr |= kvm_get_parange(mmfr0) << VTCR_EL2_PS_SHIFT;
603 vtcr |= VTCR_EL2_T0SZ(phys_shift);
605 * Use a minimum 2 level page table to prevent splitting
606 * host PMD huge pages at stage2.
608 lvls = stage2_pgtable_levels(phys_shift);
611 vtcr |= VTCR_EL2_LVLS_TO_SL0(lvls);
613 #ifdef CONFIG_ARM64_HW_AFDBM
615 * Enable the Hardware Access Flag management, unconditionally
616 * on all CPUs. The features is RES0 on CPUs without the support
617 * and must be ignored by the CPUs.
620 #endif /* CONFIG_ARM64_HW_AFDBM */
622 /* Set the vmid bits */
623 vtcr |= (get_vmid_bits(mmfr1) == 16) ?
630 static bool stage2_has_fwb(struct kvm_pgtable *pgt)
632 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
635 return !(pgt->flags & KVM_PGTABLE_S2_NOFWB);
638 #define KVM_S2_MEMATTR(pgt, attr) PAGE_S2_MEMATTR(attr, stage2_has_fwb(pgt))
640 static int stage2_set_prot_attr(struct kvm_pgtable *pgt, enum kvm_pgtable_prot prot,
643 bool device = prot & KVM_PGTABLE_PROT_DEVICE;
644 kvm_pte_t attr = device ? KVM_S2_MEMATTR(pgt, DEVICE_nGnRE) :
645 KVM_S2_MEMATTR(pgt, NORMAL);
646 u32 sh = KVM_PTE_LEAF_ATTR_LO_S2_SH_IS;
648 if (!(prot & KVM_PGTABLE_PROT_X))
649 attr |= KVM_PTE_LEAF_ATTR_HI_S2_XN;
653 if (prot & KVM_PGTABLE_PROT_R)
654 attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;
656 if (prot & KVM_PGTABLE_PROT_W)
657 attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
659 attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, sh);
660 attr |= KVM_PTE_LEAF_ATTR_LO_S2_AF;
661 attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW;
667 enum kvm_pgtable_prot kvm_pgtable_stage2_pte_prot(kvm_pte_t pte)
669 enum kvm_pgtable_prot prot = pte & KVM_PTE_LEAF_ATTR_HI_SW;
671 if (!kvm_pte_valid(pte))
674 if (pte & KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R)
675 prot |= KVM_PGTABLE_PROT_R;
676 if (pte & KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W)
677 prot |= KVM_PGTABLE_PROT_W;
678 if (!(pte & KVM_PTE_LEAF_ATTR_HI_S2_XN))
679 prot |= KVM_PGTABLE_PROT_X;
684 static bool stage2_pte_needs_update(kvm_pte_t old, kvm_pte_t new)
686 if (!kvm_pte_valid(old) || !kvm_pte_valid(new))
689 return ((old ^ new) & (~KVM_PTE_LEAF_ATTR_S2_PERMS));
692 static bool stage2_pte_is_counted(kvm_pte_t pte)
695 * The refcount tracks valid entries as well as invalid entries if they
696 * encode ownership of a page to another entity than the page-table
697 * owner, whose id is 0.
702 static bool stage2_pte_is_locked(kvm_pte_t pte)
704 return !kvm_pte_valid(pte) && (pte & KVM_INVALID_PTE_LOCKED);
707 static bool stage2_try_set_pte(const struct kvm_pgtable_visit_ctx *ctx, kvm_pte_t new)
709 if (!kvm_pgtable_walk_shared(ctx)) {
710 WRITE_ONCE(*ctx->ptep, new);
714 return cmpxchg(ctx->ptep, ctx->old, new) == ctx->old;
718 * stage2_try_break_pte() - Invalidates a pte according to the
719 * 'break-before-make' requirements of the
722 * @ctx: context of the visited pte.
725 * Returns: true if the pte was successfully broken.
727 * If the removed pte was valid, performs the necessary serialization and TLB
728 * invalidation for the old value. For counted ptes, drops the reference count
729 * on the containing table page.
731 static bool stage2_try_break_pte(const struct kvm_pgtable_visit_ctx *ctx,
732 struct kvm_s2_mmu *mmu)
734 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
736 if (stage2_pte_is_locked(ctx->old)) {
738 * Should never occur if this walker has exclusive access to the
741 WARN_ON(!kvm_pgtable_walk_shared(ctx));
745 if (!stage2_try_set_pte(ctx, KVM_INVALID_PTE_LOCKED))
749 * Perform the appropriate TLB invalidation based on the evicted pte
752 if (kvm_pte_table(ctx->old, ctx->level))
753 kvm_call_hyp(__kvm_tlb_flush_vmid, mmu);
754 else if (kvm_pte_valid(ctx->old))
755 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, ctx->addr, ctx->level);
757 if (stage2_pte_is_counted(ctx->old))
758 mm_ops->put_page(ctx->ptep);
763 static void stage2_make_pte(const struct kvm_pgtable_visit_ctx *ctx, kvm_pte_t new)
765 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
767 WARN_ON(!stage2_pte_is_locked(*ctx->ptep));
769 if (stage2_pte_is_counted(new))
770 mm_ops->get_page(ctx->ptep);
772 smp_store_release(ctx->ptep, new);
775 static void stage2_put_pte(const struct kvm_pgtable_visit_ctx *ctx, struct kvm_s2_mmu *mmu,
776 struct kvm_pgtable_mm_ops *mm_ops)
779 * Clear the existing PTE, and perform break-before-make with
780 * TLB maintenance if it was valid.
782 if (kvm_pte_valid(ctx->old)) {
783 kvm_clear_pte(ctx->ptep);
784 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, ctx->addr, ctx->level);
787 mm_ops->put_page(ctx->ptep);
790 static bool stage2_pte_cacheable(struct kvm_pgtable *pgt, kvm_pte_t pte)
792 u64 memattr = pte & KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR;
793 return memattr == KVM_S2_MEMATTR(pgt, NORMAL);
796 static bool stage2_pte_executable(kvm_pte_t pte)
798 return !(pte & KVM_PTE_LEAF_ATTR_HI_S2_XN);
801 static u64 stage2_map_walker_phys_addr(const struct kvm_pgtable_visit_ctx *ctx,
802 const struct stage2_map_data *data)
804 u64 phys = data->phys;
807 * Stage-2 walks to update ownership data are communicated to the map
808 * walker using an invalid PA. Avoid offsetting an already invalid PA,
809 * which could overflow and make the address valid again.
811 if (!kvm_phys_is_valid(phys))
815 * Otherwise, work out the correct PA based on how far the walk has
818 return phys + (ctx->addr - ctx->start);
821 static bool stage2_leaf_mapping_allowed(const struct kvm_pgtable_visit_ctx *ctx,
822 struct stage2_map_data *data)
824 u64 phys = stage2_map_walker_phys_addr(ctx, data);
826 if (data->force_pte && (ctx->level < (KVM_PGTABLE_MAX_LEVELS - 1)))
829 return kvm_block_mapping_supported(ctx, phys);
832 static int stage2_map_walker_try_leaf(const struct kvm_pgtable_visit_ctx *ctx,
833 struct stage2_map_data *data)
836 u64 phys = stage2_map_walker_phys_addr(ctx, data);
837 u64 granule = kvm_granule_size(ctx->level);
838 struct kvm_pgtable *pgt = data->mmu->pgt;
839 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
841 if (!stage2_leaf_mapping_allowed(ctx, data))
844 if (kvm_phys_is_valid(phys))
845 new = kvm_init_valid_leaf_pte(phys, data->attr, ctx->level);
847 new = kvm_init_invalid_leaf_owner(data->owner_id);
850 * Skip updating the PTE if we are trying to recreate the exact
851 * same mapping or only change the access permissions. Instead,
852 * the vCPU will exit one more time from guest if still needed
853 * and then go through the path of relaxing permissions.
855 if (!stage2_pte_needs_update(ctx->old, new))
858 if (!stage2_try_break_pte(ctx, data->mmu))
861 /* Perform CMOs before installation of the guest stage-2 PTE */
862 if (mm_ops->dcache_clean_inval_poc && stage2_pte_cacheable(pgt, new))
863 mm_ops->dcache_clean_inval_poc(kvm_pte_follow(new, mm_ops),
866 if (mm_ops->icache_inval_pou && stage2_pte_executable(new))
867 mm_ops->icache_inval_pou(kvm_pte_follow(new, mm_ops), granule);
869 stage2_make_pte(ctx, new);
874 static int stage2_map_walk_table_pre(const struct kvm_pgtable_visit_ctx *ctx,
875 struct stage2_map_data *data)
877 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
878 kvm_pte_t *childp = kvm_pte_follow(ctx->old, mm_ops);
881 if (!stage2_leaf_mapping_allowed(ctx, data))
884 ret = stage2_map_walker_try_leaf(ctx, data);
888 mm_ops->free_removed_table(childp, ctx->level);
892 static int stage2_map_walk_leaf(const struct kvm_pgtable_visit_ctx *ctx,
893 struct stage2_map_data *data)
895 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
896 kvm_pte_t *childp, new;
899 ret = stage2_map_walker_try_leaf(ctx, data);
903 if (WARN_ON(ctx->level == KVM_PGTABLE_MAX_LEVELS - 1))
909 childp = mm_ops->zalloc_page(data->memcache);
913 if (!stage2_try_break_pte(ctx, data->mmu)) {
914 mm_ops->put_page(childp);
919 * If we've run into an existing block mapping then replace it with
920 * a table. Accesses beyond 'end' that fall within the new table
921 * will be mapped lazily.
923 new = kvm_init_table_pte(childp, mm_ops);
924 stage2_make_pte(ctx, new);
930 * The TABLE_PRE callback runs for table entries on the way down, looking
931 * for table entries which we could conceivably replace with a block entry
932 * for this mapping. If it finds one it replaces the entry and calls
933 * kvm_pgtable_mm_ops::free_removed_table() to tear down the detached table.
935 * Otherwise, the LEAF callback performs the mapping at the existing leaves
938 static int stage2_map_walker(const struct kvm_pgtable_visit_ctx *ctx,
939 enum kvm_pgtable_walk_flags visit)
941 struct stage2_map_data *data = ctx->arg;
944 case KVM_PGTABLE_WALK_TABLE_PRE:
945 return stage2_map_walk_table_pre(ctx, data);
946 case KVM_PGTABLE_WALK_LEAF:
947 return stage2_map_walk_leaf(ctx, data);
953 int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
954 u64 phys, enum kvm_pgtable_prot prot,
955 void *mc, enum kvm_pgtable_walk_flags flags)
958 struct stage2_map_data map_data = {
959 .phys = ALIGN_DOWN(phys, PAGE_SIZE),
962 .force_pte = pgt->force_pte_cb && pgt->force_pte_cb(addr, addr + size, prot),
964 struct kvm_pgtable_walker walker = {
965 .cb = stage2_map_walker,
967 KVM_PGTABLE_WALK_TABLE_PRE |
968 KVM_PGTABLE_WALK_LEAF,
972 if (WARN_ON((pgt->flags & KVM_PGTABLE_S2_IDMAP) && (addr != phys)))
975 ret = stage2_set_prot_attr(pgt, prot, &map_data.attr);
979 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
984 int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size,
985 void *mc, u8 owner_id)
988 struct stage2_map_data map_data = {
989 .phys = KVM_PHYS_INVALID,
992 .owner_id = owner_id,
995 struct kvm_pgtable_walker walker = {
996 .cb = stage2_map_walker,
997 .flags = KVM_PGTABLE_WALK_TABLE_PRE |
998 KVM_PGTABLE_WALK_LEAF,
1002 if (owner_id > KVM_MAX_OWNER_ID)
1005 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
1009 static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,
1010 enum kvm_pgtable_walk_flags visit)
1012 struct kvm_pgtable *pgt = ctx->arg;
1013 struct kvm_s2_mmu *mmu = pgt->mmu;
1014 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
1015 kvm_pte_t *childp = NULL;
1016 bool need_flush = false;
1018 if (!kvm_pte_valid(ctx->old)) {
1019 if (stage2_pte_is_counted(ctx->old)) {
1020 kvm_clear_pte(ctx->ptep);
1021 mm_ops->put_page(ctx->ptep);
1026 if (kvm_pte_table(ctx->old, ctx->level)) {
1027 childp = kvm_pte_follow(ctx->old, mm_ops);
1029 if (mm_ops->page_count(childp) != 1)
1031 } else if (stage2_pte_cacheable(pgt, ctx->old)) {
1032 need_flush = !stage2_has_fwb(pgt);
1036 * This is similar to the map() path in that we unmap the entire
1037 * block entry and rely on the remaining portions being faulted
1040 stage2_put_pte(ctx, mmu, mm_ops);
1042 if (need_flush && mm_ops->dcache_clean_inval_poc)
1043 mm_ops->dcache_clean_inval_poc(kvm_pte_follow(ctx->old, mm_ops),
1044 kvm_granule_size(ctx->level));
1047 mm_ops->put_page(childp);
1052 int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
1054 struct kvm_pgtable_walker walker = {
1055 .cb = stage2_unmap_walker,
1057 .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
1060 return kvm_pgtable_walk(pgt, addr, size, &walker);
1063 struct stage2_attr_data {
1070 static int stage2_attr_walker(const struct kvm_pgtable_visit_ctx *ctx,
1071 enum kvm_pgtable_walk_flags visit)
1073 kvm_pte_t pte = ctx->old;
1074 struct stage2_attr_data *data = ctx->arg;
1075 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
1077 if (!kvm_pte_valid(ctx->old))
1080 data->level = ctx->level;
1082 pte &= ~data->attr_clr;
1083 pte |= data->attr_set;
1086 * We may race with the CPU trying to set the access flag here,
1087 * but worst-case the access flag update gets lost and will be
1088 * set on the next access instead.
1090 if (data->pte != pte) {
1092 * Invalidate instruction cache before updating the guest
1093 * stage-2 PTE if we are going to add executable permission.
1095 if (mm_ops->icache_inval_pou &&
1096 stage2_pte_executable(pte) && !stage2_pte_executable(ctx->old))
1097 mm_ops->icache_inval_pou(kvm_pte_follow(pte, mm_ops),
1098 kvm_granule_size(ctx->level));
1100 if (!stage2_try_set_pte(ctx, pte))
1107 static int stage2_update_leaf_attrs(struct kvm_pgtable *pgt, u64 addr,
1108 u64 size, kvm_pte_t attr_set,
1109 kvm_pte_t attr_clr, kvm_pte_t *orig_pte,
1110 u32 *level, enum kvm_pgtable_walk_flags flags)
1113 kvm_pte_t attr_mask = KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI;
1114 struct stage2_attr_data data = {
1115 .attr_set = attr_set & attr_mask,
1116 .attr_clr = attr_clr & attr_mask,
1118 struct kvm_pgtable_walker walker = {
1119 .cb = stage2_attr_walker,
1121 .flags = flags | KVM_PGTABLE_WALK_LEAF,
1124 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
1129 *orig_pte = data.pte;
1132 *level = data.level;
1136 int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
1138 return stage2_update_leaf_attrs(pgt, addr, size, 0,
1139 KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
1143 kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr)
1148 ret = stage2_update_leaf_attrs(pgt, addr, 1, KVM_PTE_LEAF_ATTR_LO_S2_AF, 0,
1150 KVM_PGTABLE_WALK_HANDLE_FAULT |
1151 KVM_PGTABLE_WALK_SHARED);
1158 kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr)
1161 stage2_update_leaf_attrs(pgt, addr, 1, 0, KVM_PTE_LEAF_ATTR_LO_S2_AF,
1164 * "But where's the TLBI?!", you scream.
1165 * "Over in the core code", I sigh.
1167 * See the '->clear_flush_young()' callback on the KVM mmu notifier.
1172 bool kvm_pgtable_stage2_is_young(struct kvm_pgtable *pgt, u64 addr)
1175 stage2_update_leaf_attrs(pgt, addr, 1, 0, 0, &pte, NULL, 0);
1176 return pte & KVM_PTE_LEAF_ATTR_LO_S2_AF;
1179 int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,
1180 enum kvm_pgtable_prot prot)
1184 kvm_pte_t set = 0, clr = 0;
1186 if (prot & KVM_PTE_LEAF_ATTR_HI_SW)
1189 if (prot & KVM_PGTABLE_PROT_R)
1190 set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;
1192 if (prot & KVM_PGTABLE_PROT_W)
1193 set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
1195 if (prot & KVM_PGTABLE_PROT_X)
1196 clr |= KVM_PTE_LEAF_ATTR_HI_S2_XN;
1198 ret = stage2_update_leaf_attrs(pgt, addr, 1, set, clr, NULL, &level,
1199 KVM_PGTABLE_WALK_HANDLE_FAULT |
1200 KVM_PGTABLE_WALK_SHARED);
1202 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, pgt->mmu, addr, level);
1206 static int stage2_flush_walker(const struct kvm_pgtable_visit_ctx *ctx,
1207 enum kvm_pgtable_walk_flags visit)
1209 struct kvm_pgtable *pgt = ctx->arg;
1210 struct kvm_pgtable_mm_ops *mm_ops = pgt->mm_ops;
1212 if (!kvm_pte_valid(ctx->old) || !stage2_pte_cacheable(pgt, ctx->old))
1215 if (mm_ops->dcache_clean_inval_poc)
1216 mm_ops->dcache_clean_inval_poc(kvm_pte_follow(ctx->old, mm_ops),
1217 kvm_granule_size(ctx->level));
1221 int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
1223 struct kvm_pgtable_walker walker = {
1224 .cb = stage2_flush_walker,
1225 .flags = KVM_PGTABLE_WALK_LEAF,
1229 if (stage2_has_fwb(pgt))
1232 return kvm_pgtable_walk(pgt, addr, size, &walker);
1236 int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
1237 struct kvm_pgtable_mm_ops *mm_ops,
1238 enum kvm_pgtable_stage2_flags flags,
1239 kvm_pgtable_force_pte_cb_t force_pte_cb)
1242 u64 vtcr = mmu->arch->vtcr;
1243 u32 ia_bits = VTCR_EL2_IPA(vtcr);
1244 u32 sl0 = FIELD_GET(VTCR_EL2_SL0_MASK, vtcr);
1245 u32 start_level = VTCR_EL2_TGRAN_SL0_BASE - sl0;
1247 pgd_sz = kvm_pgd_pages(ia_bits, start_level) * PAGE_SIZE;
1248 pgt->pgd = (kvm_pteref_t)mm_ops->zalloc_pages_exact(pgd_sz);
1252 pgt->ia_bits = ia_bits;
1253 pgt->start_level = start_level;
1254 pgt->mm_ops = mm_ops;
1257 pgt->force_pte_cb = force_pte_cb;
1259 /* Ensure zeroed PGD pages are visible to the hardware walker */
1264 size_t kvm_pgtable_stage2_pgd_size(u64 vtcr)
1266 u32 ia_bits = VTCR_EL2_IPA(vtcr);
1267 u32 sl0 = FIELD_GET(VTCR_EL2_SL0_MASK, vtcr);
1268 u32 start_level = VTCR_EL2_TGRAN_SL0_BASE - sl0;
1270 return kvm_pgd_pages(ia_bits, start_level) * PAGE_SIZE;
1273 static int stage2_free_walker(const struct kvm_pgtable_visit_ctx *ctx,
1274 enum kvm_pgtable_walk_flags visit)
1276 struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
1278 if (!stage2_pte_is_counted(ctx->old))
1281 mm_ops->put_page(ctx->ptep);
1283 if (kvm_pte_table(ctx->old, ctx->level))
1284 mm_ops->put_page(kvm_pte_follow(ctx->old, mm_ops));
1289 void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt)
1292 struct kvm_pgtable_walker walker = {
1293 .cb = stage2_free_walker,
1294 .flags = KVM_PGTABLE_WALK_LEAF |
1295 KVM_PGTABLE_WALK_TABLE_POST,
1298 WARN_ON(kvm_pgtable_walk(pgt, 0, BIT(pgt->ia_bits), &walker));
1299 pgd_sz = kvm_pgd_pages(pgt->ia_bits, pgt->start_level) * PAGE_SIZE;
1300 pgt->mm_ops->free_pages_exact(kvm_dereference_pteref(&walker, pgt->pgd), pgd_sz);
1304 void kvm_pgtable_stage2_free_removed(struct kvm_pgtable_mm_ops *mm_ops, void *pgtable, u32 level)
1306 kvm_pteref_t ptep = (kvm_pteref_t)pgtable;
1307 struct kvm_pgtable_walker walker = {
1308 .cb = stage2_free_walker,
1309 .flags = KVM_PGTABLE_WALK_LEAF |
1310 KVM_PGTABLE_WALK_TABLE_POST,
1312 struct kvm_pgtable_walk_data data = {
1316 * At this point the IPA really doesn't matter, as the page
1317 * table being traversed has already been removed from the stage
1318 * 2. Set an appropriate range to cover the entire page table.
1321 .end = kvm_granule_size(level),
1324 WARN_ON(__kvm_pgtable_walk(&data, mm_ops, ptep, level + 1));