1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3
4 #include "mmu_internal.h"
5 #include "tdp_iter.h"
6 #include "spte.h"
7
8 /*
9 * Recalculates the pointer to the SPTE for the current GFN and level and
10 * reread the SPTE.
11 */
tdp_iter_refresh_sptep(struct tdp_iter * iter)12 static void tdp_iter_refresh_sptep(struct tdp_iter *iter)
13 {
14 iter->sptep = iter->pt_path[iter->level - 1] +
15 SPTE_INDEX((iter->gfn | iter->gfn_bits) << PAGE_SHIFT, iter->level);
16 iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
17 }
18
19 /*
20 * Return the TDP iterator to the root PT and allow it to continue its
21 * traversal over the paging structure from there.
22 */
tdp_iter_restart(struct tdp_iter * iter)23 void tdp_iter_restart(struct tdp_iter *iter)
24 {
25 iter->yielded = false;
26 iter->yielded_gfn = iter->next_last_level_gfn;
27 iter->level = iter->root_level;
28
29 iter->gfn = gfn_round_for_level(iter->next_last_level_gfn, iter->level);
30 tdp_iter_refresh_sptep(iter);
31
32 iter->valid = true;
33 }
34
35 /*
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.
38 */
tdp_iter_start(struct tdp_iter * iter,struct kvm_mmu_page * root,int min_level,gfn_t next_last_level_gfn,gfn_t gfn_bits)39 void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,
40 int min_level, gfn_t next_last_level_gfn, gfn_t gfn_bits)
41 {
42 if (WARN_ON_ONCE(!root || (root->role.level < 1) ||
43 (root->role.level > PT64_ROOT_MAX_LEVEL) ||
44 (gfn_bits && next_last_level_gfn >= gfn_bits))) {
45 iter->valid = false;
46 return;
47 }
48
49 iter->next_last_level_gfn = next_last_level_gfn;
50 iter->gfn_bits = gfn_bits;
51 iter->root_level = root->role.level;
52 iter->min_level = min_level;
53 iter->pt_path[iter->root_level - 1] = (tdp_ptep_t)root->spt;
54 iter->as_id = kvm_mmu_page_as_id(root);
55
56 tdp_iter_restart(iter);
57 }
58
59 /*
60 * Given an SPTE and its level, returns a pointer containing the host virtual
61 * address of the child page table referenced by the SPTE. Returns null if
62 * there is no such entry.
63 */
spte_to_child_pt(u64 spte,int level)64 tdp_ptep_t spte_to_child_pt(u64 spte, int level)
65 {
66 /*
67 * There's no child entry if this entry isn't present or is a
68 * last-level entry.
69 */
70 if (!is_shadow_present_pte(spte) || is_last_spte(spte, level))
71 return NULL;
72
73 return (tdp_ptep_t)__va(spte_to_pfn(spte) << PAGE_SHIFT);
74 }
75
76 /*
77 * Steps down one level in the paging structure towards the goal GFN. Returns
78 * true if the iterator was able to step down a level, false otherwise.
79 */
try_step_down(struct tdp_iter * iter)80 static bool try_step_down(struct tdp_iter *iter)
81 {
82 tdp_ptep_t child_pt;
83
84 if (iter->level == iter->min_level)
85 return false;
86
87 /*
88 * Reread the SPTE before stepping down to avoid traversing into page
89 * tables that are no longer linked from this entry.
90 */
91 iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
92
93 child_pt = spte_to_child_pt(iter->old_spte, iter->level);
94 if (!child_pt)
95 return false;
96
97 iter->level--;
98 iter->pt_path[iter->level - 1] = child_pt;
99 iter->gfn = gfn_round_for_level(iter->next_last_level_gfn, iter->level);
100 tdp_iter_refresh_sptep(iter);
101
102 return true;
103 }
104
105 /*
106 * Steps to the next entry in the current page table, at the current page table
107 * level. The next entry could point to a page backing guest memory or another
108 * page table, or it could be non-present. Returns true if the iterator was
109 * able to step to the next entry in the page table, false if the iterator was
110 * already at the end of the current page table.
111 */
try_step_side(struct tdp_iter * iter)112 static bool try_step_side(struct tdp_iter *iter)
113 {
114 /*
115 * Check if the iterator is already at the end of the current page
116 * table.
117 */
118 if (SPTE_INDEX((iter->gfn | iter->gfn_bits) << PAGE_SHIFT, iter->level) ==
119 (SPTE_ENT_PER_PAGE - 1))
120 return false;
121
122 iter->gfn += KVM_PAGES_PER_HPAGE(iter->level);
123 iter->next_last_level_gfn = iter->gfn;
124 iter->sptep++;
125 iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
126
127 return true;
128 }
129
130 /*
131 * Tries to traverse back up a level in the paging structure so that the walk
132 * can continue from the next entry in the parent page table. Returns true on a
133 * successful step up, false if already in the root page.
134 */
try_step_up(struct tdp_iter * iter)135 static bool try_step_up(struct tdp_iter *iter)
136 {
137 if (iter->level == iter->root_level)
138 return false;
139
140 iter->level++;
141 iter->gfn = gfn_round_for_level(iter->gfn, iter->level);
142 tdp_iter_refresh_sptep(iter);
143
144 return true;
145 }
146
147 /*
148 * Step to the next SPTE in a pre-order traversal of the paging structure.
149 * To get to the next SPTE, the iterator either steps down towards the goal
150 * GFN, if at a present, non-last-level SPTE, or over to a SPTE mapping a
151 * higher GFN.
152 *
153 * The basic algorithm is as follows:
154 * 1. If the current SPTE is a non-last-level SPTE, step down into the page
155 * table it points to.
156 * 2. If the iterator cannot step down, it will try to step to the next SPTE
157 * in the current page of the paging structure.
158 * 3. If the iterator cannot step to the next entry in the current page, it will
159 * try to step up to the parent paging structure page. In this case, that
160 * SPTE will have already been visited, and so the iterator must also step
161 * to the side again.
162 */
tdp_iter_next(struct tdp_iter * iter)163 void tdp_iter_next(struct tdp_iter *iter)
164 {
165 if (iter->yielded) {
166 tdp_iter_restart(iter);
167 return;
168 }
169
170 if (try_step_down(iter))
171 return;
172
173 do {
174 if (try_step_side(iter))
175 return;
176 } while (try_step_up(iter));
177 iter->valid = false;
178 }
179
180