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