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