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