1 // SPDX-License-Identifier: GPL-2.0
4 #include "mmu_internal.h"
9 static bool __read_mostly tdp_mmu_enabled = false;
11 static bool is_tdp_mmu_enabled(void)
14 return tdp_enabled && READ_ONCE(tdp_mmu_enabled);
17 #endif /* CONFIG_X86_64 */
20 /* Initializes the TDP MMU for the VM, if enabled. */
21 void kvm_mmu_init_tdp_mmu(struct kvm *kvm)
23 if (!is_tdp_mmu_enabled())
26 /* This should not be changed for the lifetime of the VM. */
27 kvm->arch.tdp_mmu_enabled = true;
29 INIT_LIST_HEAD(&kvm->arch.tdp_mmu_roots);
32 void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)
34 if (!kvm->arch.tdp_mmu_enabled)
37 WARN_ON(!list_empty(&kvm->arch.tdp_mmu_roots));
40 #define for_each_tdp_mmu_root(_kvm, _root) \
41 list_for_each_entry(_root, &_kvm->arch.tdp_mmu_roots, link)
43 bool is_tdp_mmu_root(struct kvm *kvm, hpa_t hpa)
45 struct kvm_mmu_page *sp;
47 sp = to_shadow_page(hpa);
49 return sp->tdp_mmu_page && sp->root_count;
52 static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
53 gfn_t start, gfn_t end);
55 void kvm_tdp_mmu_free_root(struct kvm *kvm, struct kvm_mmu_page *root)
57 gfn_t max_gfn = 1ULL << (boot_cpu_data.x86_phys_bits - PAGE_SHIFT);
59 lockdep_assert_held(&kvm->mmu_lock);
61 WARN_ON(root->root_count);
62 WARN_ON(!root->tdp_mmu_page);
64 list_del(&root->link);
66 zap_gfn_range(kvm, root, 0, max_gfn);
68 free_page((unsigned long)root->spt);
69 kmem_cache_free(mmu_page_header_cache, root);
72 static union kvm_mmu_page_role page_role_for_level(struct kvm_vcpu *vcpu,
75 union kvm_mmu_page_role role;
77 role = vcpu->arch.mmu->mmu_role.base;
80 role.gpte_is_8_bytes = true;
81 role.access = ACC_ALL;
86 static struct kvm_mmu_page *alloc_tdp_mmu_page(struct kvm_vcpu *vcpu, gfn_t gfn,
89 struct kvm_mmu_page *sp;
91 sp = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
92 sp->spt = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_shadow_page_cache);
93 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
95 sp->role.word = page_role_for_level(vcpu, level).word;
97 sp->tdp_mmu_page = true;
102 static struct kvm_mmu_page *get_tdp_mmu_vcpu_root(struct kvm_vcpu *vcpu)
104 union kvm_mmu_page_role role;
105 struct kvm *kvm = vcpu->kvm;
106 struct kvm_mmu_page *root;
108 role = page_role_for_level(vcpu, vcpu->arch.mmu->shadow_root_level);
110 spin_lock(&kvm->mmu_lock);
112 /* Check for an existing root before allocating a new one. */
113 for_each_tdp_mmu_root(kvm, root) {
114 if (root->role.word == role.word) {
115 kvm_mmu_get_root(kvm, root);
116 spin_unlock(&kvm->mmu_lock);
121 root = alloc_tdp_mmu_page(vcpu, 0, vcpu->arch.mmu->shadow_root_level);
122 root->root_count = 1;
124 list_add(&root->link, &kvm->arch.tdp_mmu_roots);
126 spin_unlock(&kvm->mmu_lock);
131 hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)
133 struct kvm_mmu_page *root;
135 root = get_tdp_mmu_vcpu_root(vcpu);
139 return __pa(root->spt);
142 static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
143 u64 old_spte, u64 new_spte, int level);
145 static int kvm_mmu_page_as_id(struct kvm_mmu_page *sp)
147 return sp->role.smm ? 1 : 0;
151 * handle_changed_spte - handle bookkeeping associated with an SPTE change
153 * @as_id: the address space of the paging structure the SPTE was a part of
154 * @gfn: the base GFN that was mapped by the SPTE
155 * @old_spte: The value of the SPTE before the change
156 * @new_spte: The value of the SPTE after the change
157 * @level: the level of the PT the SPTE is part of in the paging structure
159 * Handle bookkeeping that might result from the modification of a SPTE.
160 * This function must be called for all TDP SPTE modifications.
162 static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
163 u64 old_spte, u64 new_spte, int level)
165 bool was_present = is_shadow_present_pte(old_spte);
166 bool is_present = is_shadow_present_pte(new_spte);
167 bool was_leaf = was_present && is_last_spte(old_spte, level);
168 bool is_leaf = is_present && is_last_spte(new_spte, level);
169 bool pfn_changed = spte_to_pfn(old_spte) != spte_to_pfn(new_spte);
174 WARN_ON(level > PT64_ROOT_MAX_LEVEL);
175 WARN_ON(level < PG_LEVEL_4K);
176 WARN_ON(gfn % KVM_PAGES_PER_HPAGE(level));
179 * If this warning were to trigger it would indicate that there was a
180 * missing MMU notifier or a race with some notifier handler.
181 * A present, leaf SPTE should never be directly replaced with another
182 * present leaf SPTE pointing to a differnt PFN. A notifier handler
183 * should be zapping the SPTE before the main MM's page table is
184 * changed, or the SPTE should be zeroed, and the TLBs flushed by the
185 * thread before replacement.
187 if (was_leaf && is_leaf && pfn_changed) {
188 pr_err("Invalid SPTE change: cannot replace a present leaf\n"
189 "SPTE with another present leaf SPTE mapping a\n"
191 "as_id: %d gfn: %llx old_spte: %llx new_spte: %llx level: %d",
192 as_id, gfn, old_spte, new_spte, level);
195 * Crash the host to prevent error propagation and guest data
201 if (old_spte == new_spte)
205 * The only times a SPTE should be changed from a non-present to
206 * non-present state is when an MMIO entry is installed/modified/
207 * removed. In that case, there is nothing to do here.
209 if (!was_present && !is_present) {
211 * If this change does not involve a MMIO SPTE, it is
212 * unexpected. Log the change, though it should not impact the
213 * guest since both the former and current SPTEs are nonpresent.
215 if (WARN_ON(!is_mmio_spte(old_spte) && !is_mmio_spte(new_spte)))
216 pr_err("Unexpected SPTE change! Nonpresent SPTEs\n"
217 "should not be replaced with another,\n"
218 "different nonpresent SPTE, unless one or both\n"
220 "as_id: %d gfn: %llx old_spte: %llx new_spte: %llx level: %d",
221 as_id, gfn, old_spte, new_spte, level);
226 if (was_leaf && is_dirty_spte(old_spte) &&
227 (!is_dirty_spte(new_spte) || pfn_changed))
228 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
231 * Recursively handle child PTs if the change removed a subtree from
232 * the paging structure.
234 if (was_present && !was_leaf && (pfn_changed || !is_present)) {
235 pt = spte_to_child_pt(old_spte, level);
237 for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
238 old_child_spte = READ_ONCE(*(pt + i));
239 WRITE_ONCE(*(pt + i), 0);
240 handle_changed_spte(kvm, as_id,
241 gfn + (i * KVM_PAGES_PER_HPAGE(level - 1)),
242 old_child_spte, 0, level - 1);
245 kvm_flush_remote_tlbs_with_address(kvm, gfn,
246 KVM_PAGES_PER_HPAGE(level));
248 free_page((unsigned long)pt);
252 static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
253 u64 old_spte, u64 new_spte, int level)
255 __handle_changed_spte(kvm, as_id, gfn, old_spte, new_spte, level);
258 static inline void tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,
261 u64 *root_pt = tdp_iter_root_pt(iter);
262 struct kvm_mmu_page *root = sptep_to_sp(root_pt);
263 int as_id = kvm_mmu_page_as_id(root);
265 *iter->sptep = new_spte;
267 handle_changed_spte(kvm, as_id, iter->gfn, iter->old_spte, new_spte,
271 #define tdp_root_for_each_pte(_iter, _root, _start, _end) \
272 for_each_tdp_pte(_iter, _root->spt, _root->role.level, _start, _end)
275 * Flush the TLB if the process should drop kvm->mmu_lock.
276 * Return whether the caller still needs to flush the tlb.
278 static bool tdp_mmu_iter_flush_cond_resched(struct kvm *kvm, struct tdp_iter *iter)
280 if (need_resched() || spin_needbreak(&kvm->mmu_lock)) {
281 kvm_flush_remote_tlbs(kvm);
282 cond_resched_lock(&kvm->mmu_lock);
283 tdp_iter_refresh_walk(iter);
291 * Tears down the mappings for the range of gfns, [start, end), and frees the
292 * non-root pages mapping GFNs strictly within that range. Returns true if
293 * SPTEs have been cleared and a TLB flush is needed before releasing the
296 static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
297 gfn_t start, gfn_t end)
299 struct tdp_iter iter;
300 bool flush_needed = false;
302 tdp_root_for_each_pte(iter, root, start, end) {
303 if (!is_shadow_present_pte(iter.old_spte))
307 * If this is a non-last-level SPTE that covers a larger range
308 * than should be zapped, continue, and zap the mappings at a
311 if ((iter.gfn < start ||
312 iter.gfn + KVM_PAGES_PER_HPAGE(iter.level) > end) &&
313 !is_last_spte(iter.old_spte, iter.level))
316 tdp_mmu_set_spte(kvm, &iter, 0);
318 flush_needed = tdp_mmu_iter_flush_cond_resched(kvm, &iter);
324 * Tears down the mappings for the range of gfns, [start, end), and frees the
325 * non-root pages mapping GFNs strictly within that range. Returns true if
326 * SPTEs have been cleared and a TLB flush is needed before releasing the
329 bool kvm_tdp_mmu_zap_gfn_range(struct kvm *kvm, gfn_t start, gfn_t end)
331 struct kvm_mmu_page *root;
334 for_each_tdp_mmu_root(kvm, root) {
336 * Take a reference on the root so that it cannot be freed if
337 * this thread releases the MMU lock and yields in this loop.
339 kvm_mmu_get_root(kvm, root);
341 flush |= zap_gfn_range(kvm, root, start, end);
343 kvm_mmu_put_root(kvm, root);
349 void kvm_tdp_mmu_zap_all(struct kvm *kvm)
351 gfn_t max_gfn = 1ULL << (boot_cpu_data.x86_phys_bits - PAGE_SHIFT);
354 flush = kvm_tdp_mmu_zap_gfn_range(kvm, 0, max_gfn);
356 kvm_flush_remote_tlbs(kvm);