1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4 #include "mmu_internal.h"
9 * Recalculates the pointer to the SPTE for the current GFN and level and
12 static void tdp_iter_refresh_sptep(struct tdp_iter *iter)
14 iter->sptep = iter->pt_path[iter->level - 1] +
15 SPTE_INDEX(iter->gfn << PAGE_SHIFT, iter->level);
16 iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
20 * Return the TDP iterator to the root PT and allow it to continue its
21 * traversal over the paging structure from there.
23 void tdp_iter_restart(struct tdp_iter *iter)
25 iter->yielded = false;
26 iter->yielded_gfn = iter->next_last_level_gfn;
27 iter->level = iter->root_level;
29 iter->gfn = gfn_round_for_level(iter->next_last_level_gfn, iter->level);
30 tdp_iter_refresh_sptep(iter);
36 * Sets a TDP iterator to walk a pre-order traversal of the paging structure
37 * rooted at root_pt, starting with the walk to translate next_last_level_gfn.
39 void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,
40 int min_level, gfn_t next_last_level_gfn)
42 int root_level = root->role.level;
44 WARN_ON(root_level < 1);
45 WARN_ON(root_level > PT64_ROOT_MAX_LEVEL);
47 iter->next_last_level_gfn = next_last_level_gfn;
48 iter->root_level = root_level;
49 iter->min_level = min_level;
50 iter->pt_path[iter->root_level - 1] = (tdp_ptep_t)root->spt;
51 iter->as_id = kvm_mmu_page_as_id(root);
53 tdp_iter_restart(iter);
57 * Given an SPTE and its level, returns a pointer containing the host virtual
58 * address of the child page table referenced by the SPTE. Returns null if
59 * there is no such entry.
61 tdp_ptep_t spte_to_child_pt(u64 spte, int level)
64 * There's no child entry if this entry isn't present or is a
67 if (!is_shadow_present_pte(spte) || is_last_spte(spte, level))
70 return (tdp_ptep_t)__va(spte_to_pfn(spte) << PAGE_SHIFT);
74 * Steps down one level in the paging structure towards the goal GFN. Returns
75 * true if the iterator was able to step down a level, false otherwise.
77 static bool try_step_down(struct tdp_iter *iter)
81 if (iter->level == iter->min_level)
85 * Reread the SPTE before stepping down to avoid traversing into page
86 * tables that are no longer linked from this entry.
88 iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
90 child_pt = spte_to_child_pt(iter->old_spte, iter->level);
95 iter->pt_path[iter->level - 1] = child_pt;
96 iter->gfn = gfn_round_for_level(iter->next_last_level_gfn, iter->level);
97 tdp_iter_refresh_sptep(iter);
103 * Steps to the next entry in the current page table, at the current page table
104 * level. The next entry could point to a page backing guest memory or another
105 * page table, or it could be non-present. Returns true if the iterator was
106 * able to step to the next entry in the page table, false if the iterator was
107 * already at the end of the current page table.
109 static bool try_step_side(struct tdp_iter *iter)
112 * Check if the iterator is already at the end of the current page
115 if (SPTE_INDEX(iter->gfn << PAGE_SHIFT, iter->level) ==
116 (SPTE_ENT_PER_PAGE - 1))
119 iter->gfn += KVM_PAGES_PER_HPAGE(iter->level);
120 iter->next_last_level_gfn = iter->gfn;
122 iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
128 * Tries to traverse back up a level in the paging structure so that the walk
129 * can continue from the next entry in the parent page table. Returns true on a
130 * successful step up, false if already in the root page.
132 static bool try_step_up(struct tdp_iter *iter)
134 if (iter->level == iter->root_level)
138 iter->gfn = gfn_round_for_level(iter->gfn, iter->level);
139 tdp_iter_refresh_sptep(iter);
145 * Step to the next SPTE in a pre-order traversal of the paging structure.
146 * To get to the next SPTE, the iterator either steps down towards the goal
147 * GFN, if at a present, non-last-level SPTE, or over to a SPTE mapping a
150 * The basic algorithm is as follows:
151 * 1. If the current SPTE is a non-last-level SPTE, step down into the page
152 * table it points to.
153 * 2. If the iterator cannot step down, it will try to step to the next SPTE
154 * in the current page of the paging structure.
155 * 3. If the iterator cannot step to the next entry in the current page, it will
156 * try to step up to the parent paging structure page. In this case, that
157 * SPTE will have already been visited, and so the iterator must also step
160 void tdp_iter_next(struct tdp_iter *iter)
163 tdp_iter_restart(iter);
167 if (try_step_down(iter))
171 if (try_step_side(iter))
173 } while (try_step_up(iter));