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