1c9180b72SBen Gardon // SPDX-License-Identifier: GPL-2.0
2c9180b72SBen Gardon
3c9180b72SBen Gardon #ifndef __KVM_X86_MMU_TDP_ITER_H
4c9180b72SBen Gardon #define __KVM_X86_MMU_TDP_ITER_H
5c9180b72SBen Gardon
6c9180b72SBen Gardon #include <linux/kvm_host.h>
7c9180b72SBen Gardon
8c9180b72SBen Gardon #include "mmu.h"
9ba3a6120SSean Christopherson #include "spte.h"
10c9180b72SBen Gardon
11c9180b72SBen Gardon /*
120e587aa7SSean Christopherson * TDP MMU SPTEs are RCU protected to allow paging structures (non-leaf SPTEs)
13bb95dfb9SSean Christopherson * to be zapped while holding mmu_lock for read, and to allow TLB flushes to be
14bb95dfb9SSean Christopherson * batched without having to collect the list of zapped SPs. Flows that can
15bb95dfb9SSean Christopherson * remove SPs must service pending TLB flushes prior to dropping RCU protection.
160e587aa7SSean Christopherson */
kvm_tdp_mmu_read_spte(tdp_ptep_t sptep)170e587aa7SSean Christopherson static inline u64 kvm_tdp_mmu_read_spte(tdp_ptep_t sptep)
180e587aa7SSean Christopherson {
190e587aa7SSean Christopherson return READ_ONCE(*rcu_dereference(sptep));
200e587aa7SSean Christopherson }
21ba3a6120SSean Christopherson
kvm_tdp_mmu_write_spte_atomic(tdp_ptep_t sptep,u64 new_spte)22ba3a6120SSean Christopherson static inline u64 kvm_tdp_mmu_write_spte_atomic(tdp_ptep_t sptep, u64 new_spte)
230e587aa7SSean Christopherson {
24ba3a6120SSean Christopherson return xchg(rcu_dereference(sptep), new_spte);
25ba3a6120SSean Christopherson }
26ba3a6120SSean Christopherson
__kvm_tdp_mmu_write_spte(tdp_ptep_t sptep,u64 new_spte)27ba3a6120SSean Christopherson static inline void __kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 new_spte)
28ba3a6120SSean Christopherson {
29ba3a6120SSean Christopherson WRITE_ONCE(*rcu_dereference(sptep), new_spte);
30ba3a6120SSean Christopherson }
31ba3a6120SSean Christopherson
32ba3a6120SSean Christopherson /*
3341e07665SVipin Sharma * SPTEs must be modified atomically if they are shadow-present, leaf
3441e07665SVipin Sharma * SPTEs, and have volatile bits, i.e. has bits that can be set outside
3541e07665SVipin Sharma * of mmu_lock. The Writable bit can be set by KVM's fast page fault
3641e07665SVipin Sharma * handler, and Accessed and Dirty bits can be set by the CPU.
37ba3a6120SSean Christopherson *
38ba3a6120SSean Christopherson * Note, non-leaf SPTEs do have Accessed bits and those bits are
39ba3a6120SSean Christopherson * technically volatile, but KVM doesn't consume the Accessed bit of
40ba3a6120SSean Christopherson * non-leaf SPTEs, i.e. KVM doesn't care if it clobbers the bit. This
41ba3a6120SSean Christopherson * logic needs to be reassessed if KVM were to use non-leaf Accessed
42ba3a6120SSean Christopherson * bits, e.g. to skip stepping down into child SPTEs when aging SPTEs.
43ba3a6120SSean Christopherson */
kvm_tdp_mmu_spte_need_atomic_write(u64 old_spte,int level)4441e07665SVipin Sharma static inline bool kvm_tdp_mmu_spte_need_atomic_write(u64 old_spte, int level)
4541e07665SVipin Sharma {
4641e07665SVipin Sharma return is_shadow_present_pte(old_spte) &&
4741e07665SVipin Sharma is_last_spte(old_spte, level) &&
4841e07665SVipin Sharma spte_has_volatile_bits(old_spte);
4941e07665SVipin Sharma }
5041e07665SVipin Sharma
kvm_tdp_mmu_write_spte(tdp_ptep_t sptep,u64 old_spte,u64 new_spte,int level)5141e07665SVipin Sharma static inline u64 kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 old_spte,
5241e07665SVipin Sharma u64 new_spte, int level)
5341e07665SVipin Sharma {
5441e07665SVipin Sharma if (kvm_tdp_mmu_spte_need_atomic_write(old_spte, level))
55ba3a6120SSean Christopherson return kvm_tdp_mmu_write_spte_atomic(sptep, new_spte);
56ba3a6120SSean Christopherson
57ba3a6120SSean Christopherson __kvm_tdp_mmu_write_spte(sptep, new_spte);
58ba3a6120SSean Christopherson return old_spte;
590e587aa7SSean Christopherson }
600e587aa7SSean Christopherson
tdp_mmu_clear_spte_bits(tdp_ptep_t sptep,u64 old_spte,u64 mask,int level)61*89c313f2SVipin Sharma static inline u64 tdp_mmu_clear_spte_bits(tdp_ptep_t sptep, u64 old_spte,
62*89c313f2SVipin Sharma u64 mask, int level)
63*89c313f2SVipin Sharma {
64*89c313f2SVipin Sharma atomic64_t *sptep_atomic;
65*89c313f2SVipin Sharma
66*89c313f2SVipin Sharma if (kvm_tdp_mmu_spte_need_atomic_write(old_spte, level)) {
67*89c313f2SVipin Sharma sptep_atomic = (atomic64_t *)rcu_dereference(sptep);
68*89c313f2SVipin Sharma return (u64)atomic64_fetch_and(~mask, sptep_atomic);
69*89c313f2SVipin Sharma }
70*89c313f2SVipin Sharma
71*89c313f2SVipin Sharma __kvm_tdp_mmu_write_spte(sptep, old_spte & ~mask);
72*89c313f2SVipin Sharma return old_spte;
73*89c313f2SVipin Sharma }
74*89c313f2SVipin Sharma
750e587aa7SSean Christopherson /*
76c9180b72SBen Gardon * A TDP iterator performs a pre-order walk over a TDP paging structure.
77c9180b72SBen Gardon */
78c9180b72SBen Gardon struct tdp_iter {
79c9180b72SBen Gardon /*
80c9180b72SBen Gardon * The iterator will traverse the paging structure towards the mapping
81c9180b72SBen Gardon * for this GFN.
82c9180b72SBen Gardon */
8374953d35SBen Gardon gfn_t next_last_level_gfn;
84ed5e484bSBen Gardon /*
85ed5e484bSBen Gardon * The next_last_level_gfn at the time when the thread last
86ed5e484bSBen Gardon * yielded. Only yielding when the next_last_level_gfn !=
87ed5e484bSBen Gardon * yielded_gfn helps ensure forward progress.
88ed5e484bSBen Gardon */
89ed5e484bSBen Gardon gfn_t yielded_gfn;
90c9180b72SBen Gardon /* Pointers to the page tables traversed to reach the current SPTE */
917cca2d0bSBen Gardon tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL];
92c9180b72SBen Gardon /* A pointer to the current SPTE */
937cca2d0bSBen Gardon tdp_ptep_t sptep;
94c9180b72SBen Gardon /* The lowest GFN mapped by the current SPTE */
95c9180b72SBen Gardon gfn_t gfn;
96c9180b72SBen Gardon /* The level of the root page given to the iterator */
97c9180b72SBen Gardon int root_level;
98c9180b72SBen Gardon /* The lowest level the iterator should traverse to */
99c9180b72SBen Gardon int min_level;
100c9180b72SBen Gardon /* The iterator's current level within the paging structure */
101c9180b72SBen Gardon int level;
10208889894SSean Christopherson /* The address space ID, i.e. SMM vs. regular. */
10308889894SSean Christopherson int as_id;
104c9180b72SBen Gardon /* A snapshot of the value at sptep */
105c9180b72SBen Gardon u64 old_spte;
106c9180b72SBen Gardon /*
107c9180b72SBen Gardon * Whether the iterator has a valid state. This will be false if the
108c9180b72SBen Gardon * iterator walks off the end of the paging structure.
109c9180b72SBen Gardon */
110c9180b72SBen Gardon bool valid;
1113a0f64deSSean Christopherson /*
1123a0f64deSSean Christopherson * True if KVM dropped mmu_lock and yielded in the middle of a walk, in
1133a0f64deSSean Christopherson * which case tdp_iter_next() needs to restart the walk at the root
1143a0f64deSSean Christopherson * level instead of advancing to the next entry.
1153a0f64deSSean Christopherson */
1163a0f64deSSean Christopherson bool yielded;
117c9180b72SBen Gardon };
118c9180b72SBen Gardon
119c9180b72SBen Gardon /*
120c9180b72SBen Gardon * Iterates over every SPTE mapping the GFN range [start, end) in a
121c9180b72SBen Gardon * preorder traversal.
122c9180b72SBen Gardon */
12377aa6075SDavid Matlack #define for_each_tdp_pte_min_level(iter, root, min_level, start, end) \
12477aa6075SDavid Matlack for (tdp_iter_start(&iter, root, min_level, start); \
125c9180b72SBen Gardon iter.valid && iter.gfn < end; \
126c9180b72SBen Gardon tdp_iter_next(&iter))
127c9180b72SBen Gardon
12877aa6075SDavid Matlack #define for_each_tdp_pte(iter, root, start, end) \
12977aa6075SDavid Matlack for_each_tdp_pte_min_level(iter, root, PG_LEVEL_4K, start, end)
130a6a0b05dSBen Gardon
1317cca2d0bSBen Gardon tdp_ptep_t spte_to_child_pt(u64 pte, int level);
132c9180b72SBen Gardon
13377aa6075SDavid Matlack void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,
13474953d35SBen Gardon int min_level, gfn_t next_last_level_gfn);
135c9180b72SBen Gardon void tdp_iter_next(struct tdp_iter *iter);
136b601c3bcSBen Gardon void tdp_iter_restart(struct tdp_iter *iter);
137c9180b72SBen Gardon
138c9180b72SBen Gardon #endif /* __KVM_X86_MMU_TDP_ITER_H */
139