xref: /openbmc/linux/arch/x86/kvm/mmu/tdp_iter.h (revision bef7a78d)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #ifndef __KVM_X86_MMU_TDP_ITER_H
4 #define __KVM_X86_MMU_TDP_ITER_H
5 
6 #include <linux/kvm_host.h>
7 
8 #include "mmu.h"
9 
10 /*
11  * A TDP iterator performs a pre-order walk over a TDP paging structure.
12  */
13 struct tdp_iter {
14 	/*
15 	 * The iterator will traverse the paging structure towards the mapping
16 	 * for this GFN.
17 	 */
18 	gfn_t goal_gfn;
19 	/* Pointers to the page tables traversed to reach the current SPTE */
20 	u64 *pt_path[PT64_ROOT_MAX_LEVEL];
21 	/* A pointer to the current SPTE */
22 	u64 *sptep;
23 	/* The lowest GFN mapped by the current SPTE */
24 	gfn_t gfn;
25 	/* The level of the root page given to the iterator */
26 	int root_level;
27 	/* The lowest level the iterator should traverse to */
28 	int min_level;
29 	/* The iterator's current level within the paging structure */
30 	int level;
31 	/* A snapshot of the value at sptep */
32 	u64 old_spte;
33 	/*
34 	 * Whether the iterator has a valid state. This will be false if the
35 	 * iterator walks off the end of the paging structure.
36 	 */
37 	bool valid;
38 };
39 
40 /*
41  * Iterates over every SPTE mapping the GFN range [start, end) in a
42  * preorder traversal.
43  */
44 #define for_each_tdp_pte_min_level(iter, root, root_level, min_level, start, end) \
45 	for (tdp_iter_start(&iter, root, root_level, min_level, start); \
46 	     iter.valid && iter.gfn < end;		     \
47 	     tdp_iter_next(&iter))
48 
49 #define for_each_tdp_pte(iter, root, root_level, start, end) \
50 	for_each_tdp_pte_min_level(iter, root, root_level, PG_LEVEL_4K, start, end)
51 
52 u64 *spte_to_child_pt(u64 pte, int level);
53 
54 void tdp_iter_start(struct tdp_iter *iter, u64 *root_pt, int root_level,
55 		    int min_level, gfn_t goal_gfn);
56 void tdp_iter_next(struct tdp_iter *iter);
57 void tdp_iter_refresh_walk(struct tdp_iter *iter);
58 u64 *tdp_iter_root_pt(struct tdp_iter *iter);
59 
60 #endif /* __KVM_X86_MMU_TDP_ITER_H */
61