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