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 typedef u64 __rcu *tdp_ptep_t;
11 
12 /*
13  * A TDP iterator performs a pre-order walk over a TDP paging structure.
14  */
15 struct tdp_iter {
16 	/*
17 	 * The iterator will traverse the paging structure towards the mapping
18 	 * for this GFN.
19 	 */
20 	gfn_t next_last_level_gfn;
21 	/*
22 	 * The next_last_level_gfn at the time when the thread last
23 	 * yielded. Only yielding when the next_last_level_gfn !=
24 	 * yielded_gfn helps ensure forward progress.
25 	 */
26 	gfn_t yielded_gfn;
27 	/* Pointers to the page tables traversed to reach the current SPTE */
28 	tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL];
29 	/* A pointer to the current SPTE */
30 	tdp_ptep_t sptep;
31 	/* The lowest GFN mapped by the current SPTE */
32 	gfn_t gfn;
33 	/* The level of the root page given to the iterator */
34 	int root_level;
35 	/* The lowest level the iterator should traverse to */
36 	int min_level;
37 	/* The iterator's current level within the paging structure */
38 	int level;
39 	/* The address space ID, i.e. SMM vs. regular. */
40 	int as_id;
41 	/* A snapshot of the value at sptep */
42 	u64 old_spte;
43 	/*
44 	 * Whether the iterator has a valid state. This will be false if the
45 	 * iterator walks off the end of the paging structure.
46 	 */
47 	bool valid;
48 	/*
49 	 * True if KVM dropped mmu_lock and yielded in the middle of a walk, in
50 	 * which case tdp_iter_next() needs to restart the walk at the root
51 	 * level instead of advancing to the next entry.
52 	 */
53 	bool yielded;
54 };
55 
56 /*
57  * Iterates over every SPTE mapping the GFN range [start, end) in a
58  * preorder traversal.
59  */
60 #define for_each_tdp_pte_min_level(iter, root, root_level, min_level, start, end) \
61 	for (tdp_iter_start(&iter, root, root_level, min_level, start); \
62 	     iter.valid && iter.gfn < end;		     \
63 	     tdp_iter_next(&iter))
64 
65 #define for_each_tdp_pte(iter, root, root_level, start, end) \
66 	for_each_tdp_pte_min_level(iter, root, root_level, PG_LEVEL_4K, start, end)
67 
68 tdp_ptep_t spte_to_child_pt(u64 pte, int level);
69 
70 void tdp_iter_start(struct tdp_iter *iter, u64 *root_pt, int root_level,
71 		    int min_level, gfn_t next_last_level_gfn);
72 void tdp_iter_next(struct tdp_iter *iter);
73 void tdp_iter_restart(struct tdp_iter *iter);
74 
75 #endif /* __KVM_X86_MMU_TDP_ITER_H */
76