xref: /linux/arch/x86/kvm/mmu/mmu.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Kernel-based Virtual Machine driver for Linux
4  *
5  * This module enables machines with Intel VT-x extensions to run virtual
6  * machines without emulation or binary translation.
7  *
8  * MMU support
9  *
10  * Copyright (C) 2006 Qumranet, Inc.
11  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
12  *
13  * Authors:
14  *   Yaniv Kamay  <yaniv@qumranet.com>
15  *   Avi Kivity   <avi@qumranet.com>
16  */
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include "irq.h"
20 #include "ioapic.h"
21 #include "mmu.h"
22 #include "mmu_internal.h"
23 #include "tdp_mmu.h"
24 #include "x86.h"
25 #include "kvm_cache_regs.h"
26 #include "smm.h"
27 #include "kvm_emulate.h"
28 #include "page_track.h"
29 #include "cpuid.h"
30 #include "spte.h"
31 
32 #include <linux/kvm_host.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/mm.h>
36 #include <linux/highmem.h>
37 #include <linux/moduleparam.h>
38 #include <linux/export.h>
39 #include <linux/swap.h>
40 #include <linux/hugetlb.h>
41 #include <linux/compiler.h>
42 #include <linux/srcu.h>
43 #include <linux/slab.h>
44 #include <linux/sched/signal.h>
45 #include <linux/uaccess.h>
46 #include <linux/hash.h>
47 #include <linux/kern_levels.h>
48 #include <linux/kstrtox.h>
49 #include <linux/kthread.h>
50 #include <linux/wordpart.h>
51 
52 #include <asm/page.h>
53 #include <asm/memtype.h>
54 #include <asm/cmpxchg.h>
55 #include <asm/io.h>
56 #include <asm/set_memory.h>
57 #include <asm/spec-ctrl.h>
58 #include <asm/vmx.h>
59 
60 #include "trace.h"
61 
62 static bool nx_hugepage_mitigation_hard_disabled;
63 
64 int __read_mostly nx_huge_pages = -1;
65 static uint __read_mostly nx_huge_pages_recovery_period_ms;
66 #ifdef CONFIG_PREEMPT_RT
67 /* Recovery can cause latency spikes, disable it for PREEMPT_RT.  */
68 static uint __read_mostly nx_huge_pages_recovery_ratio = 0;
69 #else
70 static uint __read_mostly nx_huge_pages_recovery_ratio = 60;
71 #endif
72 
73 static int get_nx_huge_pages(char *buffer, const struct kernel_param *kp);
74 static int set_nx_huge_pages(const char *val, const struct kernel_param *kp);
75 static int set_nx_huge_pages_recovery_param(const char *val, const struct kernel_param *kp);
76 
77 static const struct kernel_param_ops nx_huge_pages_ops = {
78 	.set = set_nx_huge_pages,
79 	.get = get_nx_huge_pages,
80 };
81 
82 static const struct kernel_param_ops nx_huge_pages_recovery_param_ops = {
83 	.set = set_nx_huge_pages_recovery_param,
84 	.get = param_get_uint,
85 };
86 
87 module_param_cb(nx_huge_pages, &nx_huge_pages_ops, &nx_huge_pages, 0644);
88 __MODULE_PARM_TYPE(nx_huge_pages, "bool");
89 module_param_cb(nx_huge_pages_recovery_ratio, &nx_huge_pages_recovery_param_ops,
90 		&nx_huge_pages_recovery_ratio, 0644);
91 __MODULE_PARM_TYPE(nx_huge_pages_recovery_ratio, "uint");
92 module_param_cb(nx_huge_pages_recovery_period_ms, &nx_huge_pages_recovery_param_ops,
93 		&nx_huge_pages_recovery_period_ms, 0644);
94 __MODULE_PARM_TYPE(nx_huge_pages_recovery_period_ms, "uint");
95 
96 static bool __read_mostly force_flush_and_sync_on_reuse;
97 module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644);
98 
99 /*
100  * When setting this variable to true it enables Two-Dimensional-Paging
101  * where the hardware walks 2 page tables:
102  * 1. the guest-virtual to guest-physical
103  * 2. while doing 1. it walks guest-physical to host-physical
104  * If the hardware supports that we don't need to do shadow paging.
105  */
106 bool tdp_enabled = false;
107 
108 static bool __ro_after_init tdp_mmu_allowed;
109 
110 #ifdef CONFIG_X86_64
111 bool __read_mostly tdp_mmu_enabled = true;
112 module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0444);
113 #endif
114 
115 static int max_huge_page_level __read_mostly;
116 static int tdp_root_level __read_mostly;
117 static int max_tdp_level __read_mostly;
118 
119 #define PTE_PREFETCH_NUM		8
120 
121 #include <trace/events/kvm.h>
122 
123 /* make pte_list_desc fit well in cache lines */
124 #define PTE_LIST_EXT 14
125 
126 /*
127  * struct pte_list_desc is the core data structure used to implement a custom
128  * list for tracking a set of related SPTEs, e.g. all the SPTEs that map a
129  * given GFN when used in the context of rmaps.  Using a custom list allows KVM
130  * to optimize for the common case where many GFNs will have at most a handful
131  * of SPTEs pointing at them, i.e. allows packing multiple SPTEs into a small
132  * memory footprint, which in turn improves runtime performance by exploiting
133  * cache locality.
134  *
135  * A list is comprised of one or more pte_list_desc objects (descriptors).
136  * Each individual descriptor stores up to PTE_LIST_EXT SPTEs.  If a descriptor
137  * is full and a new SPTEs needs to be added, a new descriptor is allocated and
138  * becomes the head of the list.  This means that by definitions, all tail
139  * descriptors are full.
140  *
141  * Note, the meta data fields are deliberately placed at the start of the
142  * structure to optimize the cacheline layout; accessing the descriptor will
143  * touch only a single cacheline so long as @spte_count<=6 (or if only the
144  * descriptors metadata is accessed).
145  */
146 struct pte_list_desc {
147 	struct pte_list_desc *more;
148 	/* The number of PTEs stored in _this_ descriptor. */
149 	u32 spte_count;
150 	/* The number of PTEs stored in all tails of this descriptor. */
151 	u32 tail_count;
152 	u64 *sptes[PTE_LIST_EXT];
153 };
154 
155 struct kvm_shadow_walk_iterator {
156 	u64 addr;
157 	hpa_t shadow_addr;
158 	u64 *sptep;
159 	int level;
160 	unsigned index;
161 };
162 
163 #define for_each_shadow_entry_using_root(_vcpu, _root, _addr, _walker)     \
164 	for (shadow_walk_init_using_root(&(_walker), (_vcpu),              \
165 					 (_root), (_addr));                \
166 	     shadow_walk_okay(&(_walker));			           \
167 	     shadow_walk_next(&(_walker)))
168 
169 #define for_each_shadow_entry(_vcpu, _addr, _walker)            \
170 	for (shadow_walk_init(&(_walker), _vcpu, _addr);	\
171 	     shadow_walk_okay(&(_walker));			\
172 	     shadow_walk_next(&(_walker)))
173 
174 #define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte)	\
175 	for (shadow_walk_init(&(_walker), _vcpu, _addr);		\
176 	     shadow_walk_okay(&(_walker)) &&				\
177 		({ spte = mmu_spte_get_lockless(_walker.sptep); 1; });	\
178 	     __shadow_walk_next(&(_walker), spte))
179 
180 static struct kmem_cache *pte_list_desc_cache;
181 struct kmem_cache *mmu_page_header_cache;
182 
183 static void mmu_spte_set(u64 *sptep, u64 spte);
184 
185 struct kvm_mmu_role_regs {
186 	const unsigned long cr0;
187 	const unsigned long cr4;
188 	const u64 efer;
189 };
190 
191 #define CREATE_TRACE_POINTS
192 #include "mmutrace.h"
193 
194 /*
195  * Yes, lot's of underscores.  They're a hint that you probably shouldn't be
196  * reading from the role_regs.  Once the root_role is constructed, it becomes
197  * the single source of truth for the MMU's state.
198  */
199 #define BUILD_MMU_ROLE_REGS_ACCESSOR(reg, name, flag)			\
200 static inline bool __maybe_unused					\
201 ____is_##reg##_##name(const struct kvm_mmu_role_regs *regs)		\
202 {									\
203 	return !!(regs->reg & flag);					\
204 }
205 BUILD_MMU_ROLE_REGS_ACCESSOR(cr0, pg, X86_CR0_PG);
206 BUILD_MMU_ROLE_REGS_ACCESSOR(cr0, wp, X86_CR0_WP);
207 BUILD_MMU_ROLE_REGS_ACCESSOR(cr4, pse, X86_CR4_PSE);
208 BUILD_MMU_ROLE_REGS_ACCESSOR(cr4, pae, X86_CR4_PAE);
209 BUILD_MMU_ROLE_REGS_ACCESSOR(cr4, smep, X86_CR4_SMEP);
210 BUILD_MMU_ROLE_REGS_ACCESSOR(cr4, smap, X86_CR4_SMAP);
211 BUILD_MMU_ROLE_REGS_ACCESSOR(cr4, pke, X86_CR4_PKE);
212 BUILD_MMU_ROLE_REGS_ACCESSOR(cr4, la57, X86_CR4_LA57);
213 BUILD_MMU_ROLE_REGS_ACCESSOR(efer, nx, EFER_NX);
214 BUILD_MMU_ROLE_REGS_ACCESSOR(efer, lma, EFER_LMA);
215 
216 /*
217  * The MMU itself (with a valid role) is the single source of truth for the
218  * MMU.  Do not use the regs used to build the MMU/role, nor the vCPU.  The
219  * regs don't account for dependencies, e.g. clearing CR4 bits if CR0.PG=1,
220  * and the vCPU may be incorrect/irrelevant.
221  */
222 #define BUILD_MMU_ROLE_ACCESSOR(base_or_ext, reg, name)		\
223 static inline bool __maybe_unused is_##reg##_##name(struct kvm_mmu *mmu)	\
224 {								\
225 	return !!(mmu->cpu_role. base_or_ext . reg##_##name);	\
226 }
227 BUILD_MMU_ROLE_ACCESSOR(base, cr0, wp);
228 BUILD_MMU_ROLE_ACCESSOR(ext,  cr4, pse);
229 BUILD_MMU_ROLE_ACCESSOR(ext,  cr4, smep);
230 BUILD_MMU_ROLE_ACCESSOR(ext,  cr4, smap);
231 BUILD_MMU_ROLE_ACCESSOR(ext,  cr4, pke);
232 BUILD_MMU_ROLE_ACCESSOR(ext,  cr4, la57);
233 BUILD_MMU_ROLE_ACCESSOR(base, efer, nx);
234 BUILD_MMU_ROLE_ACCESSOR(ext,  efer, lma);
235 
236 static inline bool is_cr0_pg(struct kvm_mmu *mmu)
237 {
238         return mmu->cpu_role.base.level > 0;
239 }
240 
241 static inline bool is_cr4_pae(struct kvm_mmu *mmu)
242 {
243         return !mmu->cpu_role.base.has_4_byte_gpte;
244 }
245 
246 static struct kvm_mmu_role_regs vcpu_to_role_regs(struct kvm_vcpu *vcpu)
247 {
248 	struct kvm_mmu_role_regs regs = {
249 		.cr0 = kvm_read_cr0_bits(vcpu, KVM_MMU_CR0_ROLE_BITS),
250 		.cr4 = kvm_read_cr4_bits(vcpu, KVM_MMU_CR4_ROLE_BITS),
251 		.efer = vcpu->arch.efer,
252 	};
253 
254 	return regs;
255 }
256 
257 static unsigned long get_guest_cr3(struct kvm_vcpu *vcpu)
258 {
259 	return kvm_read_cr3(vcpu);
260 }
261 
262 static inline unsigned long kvm_mmu_get_guest_pgd(struct kvm_vcpu *vcpu,
263 						  struct kvm_mmu *mmu)
264 {
265 	if (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) && mmu->get_guest_pgd == get_guest_cr3)
266 		return kvm_read_cr3(vcpu);
267 
268 	return mmu->get_guest_pgd(vcpu);
269 }
270 
271 static inline bool kvm_available_flush_remote_tlbs_range(void)
272 {
273 #if IS_ENABLED(CONFIG_HYPERV)
274 	return kvm_x86_ops.flush_remote_tlbs_range;
275 #else
276 	return false;
277 #endif
278 }
279 
280 static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index);
281 
282 /* Flush the range of guest memory mapped by the given SPTE. */
283 static void kvm_flush_remote_tlbs_sptep(struct kvm *kvm, u64 *sptep)
284 {
285 	struct kvm_mmu_page *sp = sptep_to_sp(sptep);
286 	gfn_t gfn = kvm_mmu_page_get_gfn(sp, spte_index(sptep));
287 
288 	kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level);
289 }
290 
291 static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn,
292 			   unsigned int access)
293 {
294 	u64 spte = make_mmio_spte(vcpu, gfn, access);
295 
296 	trace_mark_mmio_spte(sptep, gfn, spte);
297 	mmu_spte_set(sptep, spte);
298 }
299 
300 static gfn_t get_mmio_spte_gfn(u64 spte)
301 {
302 	u64 gpa = spte & shadow_nonpresent_or_rsvd_lower_gfn_mask;
303 
304 	gpa |= (spte >> SHADOW_NONPRESENT_OR_RSVD_MASK_LEN)
305 	       & shadow_nonpresent_or_rsvd_mask;
306 
307 	return gpa >> PAGE_SHIFT;
308 }
309 
310 static unsigned get_mmio_spte_access(u64 spte)
311 {
312 	return spte & shadow_mmio_access_mask;
313 }
314 
315 static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte)
316 {
317 	u64 kvm_gen, spte_gen, gen;
318 
319 	gen = kvm_vcpu_memslots(vcpu)->generation;
320 	if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
321 		return false;
322 
323 	kvm_gen = gen & MMIO_SPTE_GEN_MASK;
324 	spte_gen = get_mmio_spte_generation(spte);
325 
326 	trace_check_mmio_spte(spte, kvm_gen, spte_gen);
327 	return likely(kvm_gen == spte_gen);
328 }
329 
330 static int is_cpuid_PSE36(void)
331 {
332 	return 1;
333 }
334 
335 #ifdef CONFIG_X86_64
336 static void __set_spte(u64 *sptep, u64 spte)
337 {
338 	KVM_MMU_WARN_ON(is_ept_ve_possible(spte));
339 	WRITE_ONCE(*sptep, spte);
340 }
341 
342 static void __update_clear_spte_fast(u64 *sptep, u64 spte)
343 {
344 	KVM_MMU_WARN_ON(is_ept_ve_possible(spte));
345 	WRITE_ONCE(*sptep, spte);
346 }
347 
348 static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
349 {
350 	KVM_MMU_WARN_ON(is_ept_ve_possible(spte));
351 	return xchg(sptep, spte);
352 }
353 
354 static u64 __get_spte_lockless(u64 *sptep)
355 {
356 	return READ_ONCE(*sptep);
357 }
358 #else
359 union split_spte {
360 	struct {
361 		u32 spte_low;
362 		u32 spte_high;
363 	};
364 	u64 spte;
365 };
366 
367 static void count_spte_clear(u64 *sptep, u64 spte)
368 {
369 	struct kvm_mmu_page *sp =  sptep_to_sp(sptep);
370 
371 	if (is_shadow_present_pte(spte))
372 		return;
373 
374 	/* Ensure the spte is completely set before we increase the count */
375 	smp_wmb();
376 	sp->clear_spte_count++;
377 }
378 
379 static void __set_spte(u64 *sptep, u64 spte)
380 {
381 	union split_spte *ssptep, sspte;
382 
383 	ssptep = (union split_spte *)sptep;
384 	sspte = (union split_spte)spte;
385 
386 	ssptep->spte_high = sspte.spte_high;
387 
388 	/*
389 	 * If we map the spte from nonpresent to present, We should store
390 	 * the high bits firstly, then set present bit, so cpu can not
391 	 * fetch this spte while we are setting the spte.
392 	 */
393 	smp_wmb();
394 
395 	WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
396 }
397 
398 static void __update_clear_spte_fast(u64 *sptep, u64 spte)
399 {
400 	union split_spte *ssptep, sspte;
401 
402 	ssptep = (union split_spte *)sptep;
403 	sspte = (union split_spte)spte;
404 
405 	WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
406 
407 	/*
408 	 * If we map the spte from present to nonpresent, we should clear
409 	 * present bit firstly to avoid vcpu fetch the old high bits.
410 	 */
411 	smp_wmb();
412 
413 	ssptep->spte_high = sspte.spte_high;
414 	count_spte_clear(sptep, spte);
415 }
416 
417 static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
418 {
419 	union split_spte *ssptep, sspte, orig;
420 
421 	ssptep = (union split_spte *)sptep;
422 	sspte = (union split_spte)spte;
423 
424 	/* xchg acts as a barrier before the setting of the high bits */
425 	orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
426 	orig.spte_high = ssptep->spte_high;
427 	ssptep->spte_high = sspte.spte_high;
428 	count_spte_clear(sptep, spte);
429 
430 	return orig.spte;
431 }
432 
433 /*
434  * The idea using the light way get the spte on x86_32 guest is from
435  * gup_get_pte (mm/gup.c).
436  *
437  * An spte tlb flush may be pending, because they are coalesced and
438  * we are running out of the MMU lock.  Therefore
439  * we need to protect against in-progress updates of the spte.
440  *
441  * Reading the spte while an update is in progress may get the old value
442  * for the high part of the spte.  The race is fine for a present->non-present
443  * change (because the high part of the spte is ignored for non-present spte),
444  * but for a present->present change we must reread the spte.
445  *
446  * All such changes are done in two steps (present->non-present and
447  * non-present->present), hence it is enough to count the number of
448  * present->non-present updates: if it changed while reading the spte,
449  * we might have hit the race.  This is done using clear_spte_count.
450  */
451 static u64 __get_spte_lockless(u64 *sptep)
452 {
453 	struct kvm_mmu_page *sp =  sptep_to_sp(sptep);
454 	union split_spte spte, *orig = (union split_spte *)sptep;
455 	int count;
456 
457 retry:
458 	count = sp->clear_spte_count;
459 	smp_rmb();
460 
461 	spte.spte_low = orig->spte_low;
462 	smp_rmb();
463 
464 	spte.spte_high = orig->spte_high;
465 	smp_rmb();
466 
467 	if (unlikely(spte.spte_low != orig->spte_low ||
468 	      count != sp->clear_spte_count))
469 		goto retry;
470 
471 	return spte.spte;
472 }
473 #endif
474 
475 /* Rules for using mmu_spte_set:
476  * Set the sptep from nonpresent to present.
477  * Note: the sptep being assigned *must* be either not present
478  * or in a state where the hardware will not attempt to update
479  * the spte.
480  */
481 static void mmu_spte_set(u64 *sptep, u64 new_spte)
482 {
483 	WARN_ON_ONCE(is_shadow_present_pte(*sptep));
484 	__set_spte(sptep, new_spte);
485 }
486 
487 /* Rules for using mmu_spte_update:
488  * Update the state bits, it means the mapped pfn is not changed.
489  *
490  * Returns true if the TLB needs to be flushed
491  */
492 static bool mmu_spte_update(u64 *sptep, u64 new_spte)
493 {
494 	u64 old_spte = *sptep;
495 
496 	WARN_ON_ONCE(!is_shadow_present_pte(new_spte));
497 	check_spte_writable_invariants(new_spte);
498 
499 	if (!is_shadow_present_pte(old_spte)) {
500 		mmu_spte_set(sptep, new_spte);
501 		return false;
502 	}
503 
504 	if (!spte_has_volatile_bits(old_spte))
505 		__update_clear_spte_fast(sptep, new_spte);
506 	else
507 		old_spte = __update_clear_spte_slow(sptep, new_spte);
508 
509 	WARN_ON_ONCE(!is_shadow_present_pte(old_spte) ||
510 		     spte_to_pfn(old_spte) != spte_to_pfn(new_spte));
511 
512 	return leaf_spte_change_needs_tlb_flush(old_spte, new_spte);
513 }
514 
515 /*
516  * Rules for using mmu_spte_clear_track_bits:
517  * It sets the sptep from present to nonpresent, and track the
518  * state bits, it is used to clear the last level sptep.
519  * Returns the old PTE.
520  */
521 static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)
522 {
523 	u64 old_spte = *sptep;
524 	int level = sptep_to_sp(sptep)->role.level;
525 
526 	if (!is_shadow_present_pte(old_spte) ||
527 	    !spte_has_volatile_bits(old_spte))
528 		__update_clear_spte_fast(sptep, SHADOW_NONPRESENT_VALUE);
529 	else
530 		old_spte = __update_clear_spte_slow(sptep, SHADOW_NONPRESENT_VALUE);
531 
532 	if (!is_shadow_present_pte(old_spte))
533 		return old_spte;
534 
535 	kvm_update_page_stats(kvm, level, -1);
536 	return old_spte;
537 }
538 
539 /*
540  * Rules for using mmu_spte_clear_no_track:
541  * Directly clear spte without caring the state bits of sptep,
542  * it is used to set the upper level spte.
543  */
544 static void mmu_spte_clear_no_track(u64 *sptep)
545 {
546 	__update_clear_spte_fast(sptep, SHADOW_NONPRESENT_VALUE);
547 }
548 
549 static u64 mmu_spte_get_lockless(u64 *sptep)
550 {
551 	return __get_spte_lockless(sptep);
552 }
553 
554 static inline bool is_tdp_mmu_active(struct kvm_vcpu *vcpu)
555 {
556 	return tdp_mmu_enabled && vcpu->arch.mmu->root_role.direct;
557 }
558 
559 static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
560 {
561 	if (is_tdp_mmu_active(vcpu)) {
562 		kvm_tdp_mmu_walk_lockless_begin();
563 	} else {
564 		/*
565 		 * Prevent page table teardown by making any free-er wait during
566 		 * kvm_flush_remote_tlbs() IPI to all active vcpus.
567 		 */
568 		local_irq_disable();
569 
570 		/*
571 		 * Make sure a following spte read is not reordered ahead of the write
572 		 * to vcpu->mode.
573 		 */
574 		smp_store_mb(vcpu->mode, READING_SHADOW_PAGE_TABLES);
575 	}
576 }
577 
578 static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
579 {
580 	if (is_tdp_mmu_active(vcpu)) {
581 		kvm_tdp_mmu_walk_lockless_end();
582 	} else {
583 		/*
584 		 * Make sure the write to vcpu->mode is not reordered in front of
585 		 * reads to sptes.  If it does, kvm_mmu_commit_zap_page() can see us
586 		 * OUTSIDE_GUEST_MODE and proceed to free the shadow page table.
587 		 */
588 		smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE);
589 		local_irq_enable();
590 	}
591 }
592 
593 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect)
594 {
595 	int r;
596 
597 	/* 1 rmap, 1 parent PTE per level, and the prefetched rmaps. */
598 	r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
599 				       1 + PT64_ROOT_MAX_LEVEL + PTE_PREFETCH_NUM);
600 	if (r)
601 		return r;
602 	r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_shadow_page_cache,
603 				       PT64_ROOT_MAX_LEVEL);
604 	if (r)
605 		return r;
606 	if (maybe_indirect) {
607 		r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_shadowed_info_cache,
608 					       PT64_ROOT_MAX_LEVEL);
609 		if (r)
610 			return r;
611 	}
612 	return kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
613 					  PT64_ROOT_MAX_LEVEL);
614 }
615 
616 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
617 {
618 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache);
619 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_shadow_page_cache);
620 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_shadowed_info_cache);
621 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
622 }
623 
624 static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
625 {
626 	kmem_cache_free(pte_list_desc_cache, pte_list_desc);
627 }
628 
629 static bool sp_has_gptes(struct kvm_mmu_page *sp);
630 
631 static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
632 {
633 	if (sp->role.passthrough)
634 		return sp->gfn;
635 
636 	if (sp->shadowed_translation)
637 		return sp->shadowed_translation[index] >> PAGE_SHIFT;
638 
639 	return sp->gfn + (index << ((sp->role.level - 1) * SPTE_LEVEL_BITS));
640 }
641 
642 /*
643  * For leaf SPTEs, fetch the *guest* access permissions being shadowed. Note
644  * that the SPTE itself may have a more constrained access permissions that
645  * what the guest enforces. For example, a guest may create an executable
646  * huge PTE but KVM may disallow execution to mitigate iTLB multihit.
647  */
648 static u32 kvm_mmu_page_get_access(struct kvm_mmu_page *sp, int index)
649 {
650 	if (sp->shadowed_translation)
651 		return sp->shadowed_translation[index] & ACC_ALL;
652 
653 	/*
654 	 * For direct MMUs (e.g. TDP or non-paging guests) or passthrough SPs,
655 	 * KVM is not shadowing any guest page tables, so the "guest access
656 	 * permissions" are just ACC_ALL.
657 	 *
658 	 * For direct SPs in indirect MMUs (shadow paging), i.e. when KVM
659 	 * is shadowing a guest huge page with small pages, the guest access
660 	 * permissions being shadowed are the access permissions of the huge
661 	 * page.
662 	 *
663 	 * In both cases, sp->role.access contains the correct access bits.
664 	 */
665 	return sp->role.access;
666 }
667 
668 static void kvm_mmu_page_set_translation(struct kvm_mmu_page *sp, int index,
669 					 gfn_t gfn, unsigned int access)
670 {
671 	if (sp->shadowed_translation) {
672 		sp->shadowed_translation[index] = (gfn << PAGE_SHIFT) | access;
673 		return;
674 	}
675 
676 	WARN_ONCE(access != kvm_mmu_page_get_access(sp, index),
677 	          "access mismatch under %s page %llx (expected %u, got %u)\n",
678 	          sp->role.passthrough ? "passthrough" : "direct",
679 	          sp->gfn, kvm_mmu_page_get_access(sp, index), access);
680 
681 	WARN_ONCE(gfn != kvm_mmu_page_get_gfn(sp, index),
682 	          "gfn mismatch under %s page %llx (expected %llx, got %llx)\n",
683 	          sp->role.passthrough ? "passthrough" : "direct",
684 	          sp->gfn, kvm_mmu_page_get_gfn(sp, index), gfn);
685 }
686 
687 static void kvm_mmu_page_set_access(struct kvm_mmu_page *sp, int index,
688 				    unsigned int access)
689 {
690 	gfn_t gfn = kvm_mmu_page_get_gfn(sp, index);
691 
692 	kvm_mmu_page_set_translation(sp, index, gfn, access);
693 }
694 
695 /*
696  * Return the pointer to the large page information for a given gfn,
697  * handling slots that are not large page aligned.
698  */
699 static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
700 		const struct kvm_memory_slot *slot, int level)
701 {
702 	unsigned long idx;
703 
704 	idx = gfn_to_index(gfn, slot->base_gfn, level);
705 	return &slot->arch.lpage_info[level - 2][idx];
706 }
707 
708 /*
709  * The most significant bit in disallow_lpage tracks whether or not memory
710  * attributes are mixed, i.e. not identical for all gfns at the current level.
711  * The lower order bits are used to refcount other cases where a hugepage is
712  * disallowed, e.g. if KVM has shadow a page table at the gfn.
713  */
714 #define KVM_LPAGE_MIXED_FLAG	BIT(31)
715 
716 static void update_gfn_disallow_lpage_count(const struct kvm_memory_slot *slot,
717 					    gfn_t gfn, int count)
718 {
719 	struct kvm_lpage_info *linfo;
720 	int old, i;
721 
722 	for (i = PG_LEVEL_2M; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) {
723 		linfo = lpage_info_slot(gfn, slot, i);
724 
725 		old = linfo->disallow_lpage;
726 		linfo->disallow_lpage += count;
727 		WARN_ON_ONCE((old ^ linfo->disallow_lpage) & KVM_LPAGE_MIXED_FLAG);
728 	}
729 }
730 
731 void kvm_mmu_gfn_disallow_lpage(const struct kvm_memory_slot *slot, gfn_t gfn)
732 {
733 	update_gfn_disallow_lpage_count(slot, gfn, 1);
734 }
735 
736 void kvm_mmu_gfn_allow_lpage(const struct kvm_memory_slot *slot, gfn_t gfn)
737 {
738 	update_gfn_disallow_lpage_count(slot, gfn, -1);
739 }
740 
741 static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
742 {
743 	struct kvm_memslots *slots;
744 	struct kvm_memory_slot *slot;
745 	gfn_t gfn;
746 
747 	kvm->arch.indirect_shadow_pages++;
748 	/*
749 	 * Ensure indirect_shadow_pages is elevated prior to re-reading guest
750 	 * child PTEs in FNAME(gpte_changed), i.e. guarantee either in-flight
751 	 * emulated writes are visible before re-reading guest PTEs, or that
752 	 * an emulated write will see the elevated count and acquire mmu_lock
753 	 * to update SPTEs.  Pairs with the smp_mb() in kvm_mmu_track_write().
754 	 */
755 	smp_mb();
756 
757 	gfn = sp->gfn;
758 	slots = kvm_memslots_for_spte_role(kvm, sp->role);
759 	slot = __gfn_to_memslot(slots, gfn);
760 
761 	/* the non-leaf shadow pages are keeping readonly. */
762 	if (sp->role.level > PG_LEVEL_4K)
763 		return __kvm_write_track_add_gfn(kvm, slot, gfn);
764 
765 	kvm_mmu_gfn_disallow_lpage(slot, gfn);
766 
767 	if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn, PG_LEVEL_4K))
768 		kvm_flush_remote_tlbs_gfn(kvm, gfn, PG_LEVEL_4K);
769 }
770 
771 void track_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp)
772 {
773 	/*
774 	 * If it's possible to replace the shadow page with an NX huge page,
775 	 * i.e. if the shadow page is the only thing currently preventing KVM
776 	 * from using a huge page, add the shadow page to the list of "to be
777 	 * zapped for NX recovery" pages.  Note, the shadow page can already be
778 	 * on the list if KVM is reusing an existing shadow page, i.e. if KVM
779 	 * links a shadow page at multiple points.
780 	 */
781 	if (!list_empty(&sp->possible_nx_huge_page_link))
782 		return;
783 
784 	++kvm->stat.nx_lpage_splits;
785 	list_add_tail(&sp->possible_nx_huge_page_link,
786 		      &kvm->arch.possible_nx_huge_pages);
787 }
788 
789 static void account_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp,
790 				 bool nx_huge_page_possible)
791 {
792 	sp->nx_huge_page_disallowed = true;
793 
794 	if (nx_huge_page_possible)
795 		track_possible_nx_huge_page(kvm, sp);
796 }
797 
798 static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
799 {
800 	struct kvm_memslots *slots;
801 	struct kvm_memory_slot *slot;
802 	gfn_t gfn;
803 
804 	kvm->arch.indirect_shadow_pages--;
805 	gfn = sp->gfn;
806 	slots = kvm_memslots_for_spte_role(kvm, sp->role);
807 	slot = __gfn_to_memslot(slots, gfn);
808 	if (sp->role.level > PG_LEVEL_4K)
809 		return __kvm_write_track_remove_gfn(kvm, slot, gfn);
810 
811 	kvm_mmu_gfn_allow_lpage(slot, gfn);
812 }
813 
814 void untrack_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp)
815 {
816 	if (list_empty(&sp->possible_nx_huge_page_link))
817 		return;
818 
819 	--kvm->stat.nx_lpage_splits;
820 	list_del_init(&sp->possible_nx_huge_page_link);
821 }
822 
823 static void unaccount_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp)
824 {
825 	sp->nx_huge_page_disallowed = false;
826 
827 	untrack_possible_nx_huge_page(kvm, sp);
828 }
829 
830 static struct kvm_memory_slot *gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu,
831 							   gfn_t gfn,
832 							   bool no_dirty_log)
833 {
834 	struct kvm_memory_slot *slot;
835 
836 	slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
837 	if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
838 		return NULL;
839 	if (no_dirty_log && kvm_slot_dirty_track_enabled(slot))
840 		return NULL;
841 
842 	return slot;
843 }
844 
845 /*
846  * About rmap_head encoding:
847  *
848  * If the bit zero of rmap_head->val is clear, then it points to the only spte
849  * in this rmap chain. Otherwise, (rmap_head->val & ~1) points to a struct
850  * pte_list_desc containing more mappings.
851  */
852 #define KVM_RMAP_MANY	BIT(0)
853 
854 /*
855  * Returns the number of pointers in the rmap chain, not counting the new one.
856  */
857 static int pte_list_add(struct kvm_mmu_memory_cache *cache, u64 *spte,
858 			struct kvm_rmap_head *rmap_head)
859 {
860 	struct pte_list_desc *desc;
861 	int count = 0;
862 
863 	if (!rmap_head->val) {
864 		rmap_head->val = (unsigned long)spte;
865 	} else if (!(rmap_head->val & KVM_RMAP_MANY)) {
866 		desc = kvm_mmu_memory_cache_alloc(cache);
867 		desc->sptes[0] = (u64 *)rmap_head->val;
868 		desc->sptes[1] = spte;
869 		desc->spte_count = 2;
870 		desc->tail_count = 0;
871 		rmap_head->val = (unsigned long)desc | KVM_RMAP_MANY;
872 		++count;
873 	} else {
874 		desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY);
875 		count = desc->tail_count + desc->spte_count;
876 
877 		/*
878 		 * If the previous head is full, allocate a new head descriptor
879 		 * as tail descriptors are always kept full.
880 		 */
881 		if (desc->spte_count == PTE_LIST_EXT) {
882 			desc = kvm_mmu_memory_cache_alloc(cache);
883 			desc->more = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY);
884 			desc->spte_count = 0;
885 			desc->tail_count = count;
886 			rmap_head->val = (unsigned long)desc | KVM_RMAP_MANY;
887 		}
888 		desc->sptes[desc->spte_count++] = spte;
889 	}
890 	return count;
891 }
892 
893 static void pte_list_desc_remove_entry(struct kvm *kvm,
894 				       struct kvm_rmap_head *rmap_head,
895 				       struct pte_list_desc *desc, int i)
896 {
897 	struct pte_list_desc *head_desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY);
898 	int j = head_desc->spte_count - 1;
899 
900 	/*
901 	 * The head descriptor should never be empty.  A new head is added only
902 	 * when adding an entry and the previous head is full, and heads are
903 	 * removed (this flow) when they become empty.
904 	 */
905 	KVM_BUG_ON_DATA_CORRUPTION(j < 0, kvm);
906 
907 	/*
908 	 * Replace the to-be-freed SPTE with the last valid entry from the head
909 	 * descriptor to ensure that tail descriptors are full at all times.
910 	 * Note, this also means that tail_count is stable for each descriptor.
911 	 */
912 	desc->sptes[i] = head_desc->sptes[j];
913 	head_desc->sptes[j] = NULL;
914 	head_desc->spte_count--;
915 	if (head_desc->spte_count)
916 		return;
917 
918 	/*
919 	 * The head descriptor is empty.  If there are no tail descriptors,
920 	 * nullify the rmap head to mark the list as empty, else point the rmap
921 	 * head at the next descriptor, i.e. the new head.
922 	 */
923 	if (!head_desc->more)
924 		rmap_head->val = 0;
925 	else
926 		rmap_head->val = (unsigned long)head_desc->more | KVM_RMAP_MANY;
927 	mmu_free_pte_list_desc(head_desc);
928 }
929 
930 static void pte_list_remove(struct kvm *kvm, u64 *spte,
931 			    struct kvm_rmap_head *rmap_head)
932 {
933 	struct pte_list_desc *desc;
934 	int i;
935 
936 	if (KVM_BUG_ON_DATA_CORRUPTION(!rmap_head->val, kvm))
937 		return;
938 
939 	if (!(rmap_head->val & KVM_RMAP_MANY)) {
940 		if (KVM_BUG_ON_DATA_CORRUPTION((u64 *)rmap_head->val != spte, kvm))
941 			return;
942 
943 		rmap_head->val = 0;
944 	} else {
945 		desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY);
946 		while (desc) {
947 			for (i = 0; i < desc->spte_count; ++i) {
948 				if (desc->sptes[i] == spte) {
949 					pte_list_desc_remove_entry(kvm, rmap_head,
950 								   desc, i);
951 					return;
952 				}
953 			}
954 			desc = desc->more;
955 		}
956 
957 		KVM_BUG_ON_DATA_CORRUPTION(true, kvm);
958 	}
959 }
960 
961 static void kvm_zap_one_rmap_spte(struct kvm *kvm,
962 				  struct kvm_rmap_head *rmap_head, u64 *sptep)
963 {
964 	mmu_spte_clear_track_bits(kvm, sptep);
965 	pte_list_remove(kvm, sptep, rmap_head);
966 }
967 
968 /* Return true if at least one SPTE was zapped, false otherwise */
969 static bool kvm_zap_all_rmap_sptes(struct kvm *kvm,
970 				   struct kvm_rmap_head *rmap_head)
971 {
972 	struct pte_list_desc *desc, *next;
973 	int i;
974 
975 	if (!rmap_head->val)
976 		return false;
977 
978 	if (!(rmap_head->val & KVM_RMAP_MANY)) {
979 		mmu_spte_clear_track_bits(kvm, (u64 *)rmap_head->val);
980 		goto out;
981 	}
982 
983 	desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY);
984 
985 	for (; desc; desc = next) {
986 		for (i = 0; i < desc->spte_count; i++)
987 			mmu_spte_clear_track_bits(kvm, desc->sptes[i]);
988 		next = desc->more;
989 		mmu_free_pte_list_desc(desc);
990 	}
991 out:
992 	/* rmap_head is meaningless now, remember to reset it */
993 	rmap_head->val = 0;
994 	return true;
995 }
996 
997 unsigned int pte_list_count(struct kvm_rmap_head *rmap_head)
998 {
999 	struct pte_list_desc *desc;
1000 
1001 	if (!rmap_head->val)
1002 		return 0;
1003 	else if (!(rmap_head->val & KVM_RMAP_MANY))
1004 		return 1;
1005 
1006 	desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY);
1007 	return desc->tail_count + desc->spte_count;
1008 }
1009 
1010 static struct kvm_rmap_head *gfn_to_rmap(gfn_t gfn, int level,
1011 					 const struct kvm_memory_slot *slot)
1012 {
1013 	unsigned long idx;
1014 
1015 	idx = gfn_to_index(gfn, slot->base_gfn, level);
1016 	return &slot->arch.rmap[level - PG_LEVEL_4K][idx];
1017 }
1018 
1019 static void rmap_remove(struct kvm *kvm, u64 *spte)
1020 {
1021 	struct kvm_memslots *slots;
1022 	struct kvm_memory_slot *slot;
1023 	struct kvm_mmu_page *sp;
1024 	gfn_t gfn;
1025 	struct kvm_rmap_head *rmap_head;
1026 
1027 	sp = sptep_to_sp(spte);
1028 	gfn = kvm_mmu_page_get_gfn(sp, spte_index(spte));
1029 
1030 	/*
1031 	 * Unlike rmap_add, rmap_remove does not run in the context of a vCPU
1032 	 * so we have to determine which memslots to use based on context
1033 	 * information in sp->role.
1034 	 */
1035 	slots = kvm_memslots_for_spte_role(kvm, sp->role);
1036 
1037 	slot = __gfn_to_memslot(slots, gfn);
1038 	rmap_head = gfn_to_rmap(gfn, sp->role.level, slot);
1039 
1040 	pte_list_remove(kvm, spte, rmap_head);
1041 }
1042 
1043 /*
1044  * Used by the following functions to iterate through the sptes linked by a
1045  * rmap.  All fields are private and not assumed to be used outside.
1046  */
1047 struct rmap_iterator {
1048 	/* private fields */
1049 	struct pte_list_desc *desc;	/* holds the sptep if not NULL */
1050 	int pos;			/* index of the sptep */
1051 };
1052 
1053 /*
1054  * Iteration must be started by this function.  This should also be used after
1055  * removing/dropping sptes from the rmap link because in such cases the
1056  * information in the iterator may not be valid.
1057  *
1058  * Returns sptep if found, NULL otherwise.
1059  */
1060 static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head,
1061 			   struct rmap_iterator *iter)
1062 {
1063 	u64 *sptep;
1064 
1065 	if (!rmap_head->val)
1066 		return NULL;
1067 
1068 	if (!(rmap_head->val & KVM_RMAP_MANY)) {
1069 		iter->desc = NULL;
1070 		sptep = (u64 *)rmap_head->val;
1071 		goto out;
1072 	}
1073 
1074 	iter->desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY);
1075 	iter->pos = 0;
1076 	sptep = iter->desc->sptes[iter->pos];
1077 out:
1078 	BUG_ON(!is_shadow_present_pte(*sptep));
1079 	return sptep;
1080 }
1081 
1082 /*
1083  * Must be used with a valid iterator: e.g. after rmap_get_first().
1084  *
1085  * Returns sptep if found, NULL otherwise.
1086  */
1087 static u64 *rmap_get_next(struct rmap_iterator *iter)
1088 {
1089 	u64 *sptep;
1090 
1091 	if (iter->desc) {
1092 		if (iter->pos < PTE_LIST_EXT - 1) {
1093 			++iter->pos;
1094 			sptep = iter->desc->sptes[iter->pos];
1095 			if (sptep)
1096 				goto out;
1097 		}
1098 
1099 		iter->desc = iter->desc->more;
1100 
1101 		if (iter->desc) {
1102 			iter->pos = 0;
1103 			/* desc->sptes[0] cannot be NULL */
1104 			sptep = iter->desc->sptes[iter->pos];
1105 			goto out;
1106 		}
1107 	}
1108 
1109 	return NULL;
1110 out:
1111 	BUG_ON(!is_shadow_present_pte(*sptep));
1112 	return sptep;
1113 }
1114 
1115 #define for_each_rmap_spte(_rmap_head_, _iter_, _spte_)			\
1116 	for (_spte_ = rmap_get_first(_rmap_head_, _iter_);		\
1117 	     _spte_; _spte_ = rmap_get_next(_iter_))
1118 
1119 static void drop_spte(struct kvm *kvm, u64 *sptep)
1120 {
1121 	u64 old_spte = mmu_spte_clear_track_bits(kvm, sptep);
1122 
1123 	if (is_shadow_present_pte(old_spte))
1124 		rmap_remove(kvm, sptep);
1125 }
1126 
1127 static void drop_large_spte(struct kvm *kvm, u64 *sptep, bool flush)
1128 {
1129 	struct kvm_mmu_page *sp;
1130 
1131 	sp = sptep_to_sp(sptep);
1132 	WARN_ON_ONCE(sp->role.level == PG_LEVEL_4K);
1133 
1134 	drop_spte(kvm, sptep);
1135 
1136 	if (flush)
1137 		kvm_flush_remote_tlbs_sptep(kvm, sptep);
1138 }
1139 
1140 /*
1141  * Write-protect on the specified @sptep, @pt_protect indicates whether
1142  * spte write-protection is caused by protecting shadow page table.
1143  *
1144  * Note: write protection is difference between dirty logging and spte
1145  * protection:
1146  * - for dirty logging, the spte can be set to writable at anytime if
1147  *   its dirty bitmap is properly set.
1148  * - for spte protection, the spte can be writable only after unsync-ing
1149  *   shadow page.
1150  *
1151  * Return true if tlb need be flushed.
1152  */
1153 static bool spte_write_protect(u64 *sptep, bool pt_protect)
1154 {
1155 	u64 spte = *sptep;
1156 
1157 	if (!is_writable_pte(spte) &&
1158 	    !(pt_protect && is_mmu_writable_spte(spte)))
1159 		return false;
1160 
1161 	if (pt_protect)
1162 		spte &= ~shadow_mmu_writable_mask;
1163 	spte = spte & ~PT_WRITABLE_MASK;
1164 
1165 	return mmu_spte_update(sptep, spte);
1166 }
1167 
1168 static bool rmap_write_protect(struct kvm_rmap_head *rmap_head,
1169 			       bool pt_protect)
1170 {
1171 	u64 *sptep;
1172 	struct rmap_iterator iter;
1173 	bool flush = false;
1174 
1175 	for_each_rmap_spte(rmap_head, &iter, sptep)
1176 		flush |= spte_write_protect(sptep, pt_protect);
1177 
1178 	return flush;
1179 }
1180 
1181 static bool spte_clear_dirty(u64 *sptep)
1182 {
1183 	u64 spte = *sptep;
1184 
1185 	KVM_MMU_WARN_ON(!spte_ad_enabled(spte));
1186 	spte &= ~shadow_dirty_mask;
1187 	return mmu_spte_update(sptep, spte);
1188 }
1189 
1190 /*
1191  * Gets the GFN ready for another round of dirty logging by clearing the
1192  *	- D bit on ad-enabled SPTEs, and
1193  *	- W bit on ad-disabled SPTEs.
1194  * Returns true iff any D or W bits were cleared.
1195  */
1196 static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1197 			       const struct kvm_memory_slot *slot)
1198 {
1199 	u64 *sptep;
1200 	struct rmap_iterator iter;
1201 	bool flush = false;
1202 
1203 	for_each_rmap_spte(rmap_head, &iter, sptep)
1204 		if (spte_ad_need_write_protect(*sptep))
1205 			flush |= test_and_clear_bit(PT_WRITABLE_SHIFT,
1206 						    (unsigned long *)sptep);
1207 		else
1208 			flush |= spte_clear_dirty(sptep);
1209 
1210 	return flush;
1211 }
1212 
1213 static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
1214 				     struct kvm_memory_slot *slot,
1215 				     gfn_t gfn_offset, unsigned long mask)
1216 {
1217 	struct kvm_rmap_head *rmap_head;
1218 
1219 	if (tdp_mmu_enabled)
1220 		kvm_tdp_mmu_clear_dirty_pt_masked(kvm, slot,
1221 				slot->base_gfn + gfn_offset, mask, true);
1222 
1223 	if (!kvm_memslots_have_rmaps(kvm))
1224 		return;
1225 
1226 	while (mask) {
1227 		rmap_head = gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
1228 					PG_LEVEL_4K, slot);
1229 		rmap_write_protect(rmap_head, false);
1230 
1231 		/* clear the first set bit */
1232 		mask &= mask - 1;
1233 	}
1234 }
1235 
1236 static void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm,
1237 					 struct kvm_memory_slot *slot,
1238 					 gfn_t gfn_offset, unsigned long mask)
1239 {
1240 	struct kvm_rmap_head *rmap_head;
1241 
1242 	if (tdp_mmu_enabled)
1243 		kvm_tdp_mmu_clear_dirty_pt_masked(kvm, slot,
1244 				slot->base_gfn + gfn_offset, mask, false);
1245 
1246 	if (!kvm_memslots_have_rmaps(kvm))
1247 		return;
1248 
1249 	while (mask) {
1250 		rmap_head = gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
1251 					PG_LEVEL_4K, slot);
1252 		__rmap_clear_dirty(kvm, rmap_head, slot);
1253 
1254 		/* clear the first set bit */
1255 		mask &= mask - 1;
1256 	}
1257 }
1258 
1259 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1260 				struct kvm_memory_slot *slot,
1261 				gfn_t gfn_offset, unsigned long mask)
1262 {
1263 	/*
1264 	 * If the slot was assumed to be "initially all dirty", write-protect
1265 	 * huge pages to ensure they are split to 4KiB on the first write (KVM
1266 	 * dirty logs at 4KiB granularity). If eager page splitting is enabled,
1267 	 * immediately try to split huge pages, e.g. so that vCPUs don't get
1268 	 * saddled with the cost of splitting.
1269 	 *
1270 	 * The gfn_offset is guaranteed to be aligned to 64, but the base_gfn
1271 	 * of memslot has no such restriction, so the range can cross two large
1272 	 * pages.
1273 	 */
1274 	if (kvm_dirty_log_manual_protect_and_init_set(kvm)) {
1275 		gfn_t start = slot->base_gfn + gfn_offset + __ffs(mask);
1276 		gfn_t end = slot->base_gfn + gfn_offset + __fls(mask);
1277 
1278 		if (READ_ONCE(eager_page_split))
1279 			kvm_mmu_try_split_huge_pages(kvm, slot, start, end + 1, PG_LEVEL_4K);
1280 
1281 		kvm_mmu_slot_gfn_write_protect(kvm, slot, start, PG_LEVEL_2M);
1282 
1283 		/* Cross two large pages? */
1284 		if (ALIGN(start << PAGE_SHIFT, PMD_SIZE) !=
1285 		    ALIGN(end << PAGE_SHIFT, PMD_SIZE))
1286 			kvm_mmu_slot_gfn_write_protect(kvm, slot, end,
1287 						       PG_LEVEL_2M);
1288 	}
1289 
1290 	/*
1291 	 * (Re)Enable dirty logging for all 4KiB SPTEs that map the GFNs in
1292 	 * mask.  If PML is enabled and the GFN doesn't need to be write-
1293 	 * protected for other reasons, e.g. shadow paging, clear the Dirty bit.
1294 	 * Otherwise clear the Writable bit.
1295 	 *
1296 	 * Note that kvm_mmu_clear_dirty_pt_masked() is called whenever PML is
1297 	 * enabled but it chooses between clearing the Dirty bit and Writeable
1298 	 * bit based on the context.
1299 	 */
1300 	if (kvm_x86_ops.cpu_dirty_log_size)
1301 		kvm_mmu_clear_dirty_pt_masked(kvm, slot, gfn_offset, mask);
1302 	else
1303 		kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
1304 }
1305 
1306 int kvm_cpu_dirty_log_size(void)
1307 {
1308 	return kvm_x86_ops.cpu_dirty_log_size;
1309 }
1310 
1311 bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
1312 				    struct kvm_memory_slot *slot, u64 gfn,
1313 				    int min_level)
1314 {
1315 	struct kvm_rmap_head *rmap_head;
1316 	int i;
1317 	bool write_protected = false;
1318 
1319 	if (kvm_memslots_have_rmaps(kvm)) {
1320 		for (i = min_level; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) {
1321 			rmap_head = gfn_to_rmap(gfn, i, slot);
1322 			write_protected |= rmap_write_protect(rmap_head, true);
1323 		}
1324 	}
1325 
1326 	if (tdp_mmu_enabled)
1327 		write_protected |=
1328 			kvm_tdp_mmu_write_protect_gfn(kvm, slot, gfn, min_level);
1329 
1330 	return write_protected;
1331 }
1332 
1333 static bool kvm_vcpu_write_protect_gfn(struct kvm_vcpu *vcpu, u64 gfn)
1334 {
1335 	struct kvm_memory_slot *slot;
1336 
1337 	slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
1338 	return kvm_mmu_slot_gfn_write_protect(vcpu->kvm, slot, gfn, PG_LEVEL_4K);
1339 }
1340 
1341 static bool kvm_zap_rmap(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1342 			 const struct kvm_memory_slot *slot)
1343 {
1344 	return kvm_zap_all_rmap_sptes(kvm, rmap_head);
1345 }
1346 
1347 struct slot_rmap_walk_iterator {
1348 	/* input fields. */
1349 	const struct kvm_memory_slot *slot;
1350 	gfn_t start_gfn;
1351 	gfn_t end_gfn;
1352 	int start_level;
1353 	int end_level;
1354 
1355 	/* output fields. */
1356 	gfn_t gfn;
1357 	struct kvm_rmap_head *rmap;
1358 	int level;
1359 
1360 	/* private field. */
1361 	struct kvm_rmap_head *end_rmap;
1362 };
1363 
1364 static void rmap_walk_init_level(struct slot_rmap_walk_iterator *iterator,
1365 				 int level)
1366 {
1367 	iterator->level = level;
1368 	iterator->gfn = iterator->start_gfn;
1369 	iterator->rmap = gfn_to_rmap(iterator->gfn, level, iterator->slot);
1370 	iterator->end_rmap = gfn_to_rmap(iterator->end_gfn, level, iterator->slot);
1371 }
1372 
1373 static void slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator,
1374 				const struct kvm_memory_slot *slot,
1375 				int start_level, int end_level,
1376 				gfn_t start_gfn, gfn_t end_gfn)
1377 {
1378 	iterator->slot = slot;
1379 	iterator->start_level = start_level;
1380 	iterator->end_level = end_level;
1381 	iterator->start_gfn = start_gfn;
1382 	iterator->end_gfn = end_gfn;
1383 
1384 	rmap_walk_init_level(iterator, iterator->start_level);
1385 }
1386 
1387 static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator)
1388 {
1389 	return !!iterator->rmap;
1390 }
1391 
1392 static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator)
1393 {
1394 	while (++iterator->rmap <= iterator->end_rmap) {
1395 		iterator->gfn += KVM_PAGES_PER_HPAGE(iterator->level);
1396 
1397 		if (iterator->rmap->val)
1398 			return;
1399 	}
1400 
1401 	if (++iterator->level > iterator->end_level) {
1402 		iterator->rmap = NULL;
1403 		return;
1404 	}
1405 
1406 	rmap_walk_init_level(iterator, iterator->level);
1407 }
1408 
1409 #define for_each_slot_rmap_range(_slot_, _start_level_, _end_level_,	\
1410 	   _start_gfn, _end_gfn, _iter_)				\
1411 	for (slot_rmap_walk_init(_iter_, _slot_, _start_level_,		\
1412 				 _end_level_, _start_gfn, _end_gfn);	\
1413 	     slot_rmap_walk_okay(_iter_);				\
1414 	     slot_rmap_walk_next(_iter_))
1415 
1416 /* The return value indicates if tlb flush on all vcpus is needed. */
1417 typedef bool (*slot_rmaps_handler) (struct kvm *kvm,
1418 				    struct kvm_rmap_head *rmap_head,
1419 				    const struct kvm_memory_slot *slot);
1420 
1421 static __always_inline bool __walk_slot_rmaps(struct kvm *kvm,
1422 					      const struct kvm_memory_slot *slot,
1423 					      slot_rmaps_handler fn,
1424 					      int start_level, int end_level,
1425 					      gfn_t start_gfn, gfn_t end_gfn,
1426 					      bool can_yield, bool flush_on_yield,
1427 					      bool flush)
1428 {
1429 	struct slot_rmap_walk_iterator iterator;
1430 
1431 	lockdep_assert_held_write(&kvm->mmu_lock);
1432 
1433 	for_each_slot_rmap_range(slot, start_level, end_level, start_gfn,
1434 			end_gfn, &iterator) {
1435 		if (iterator.rmap)
1436 			flush |= fn(kvm, iterator.rmap, slot);
1437 
1438 		if (!can_yield)
1439 			continue;
1440 
1441 		if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
1442 			if (flush && flush_on_yield) {
1443 				kvm_flush_remote_tlbs_range(kvm, start_gfn,
1444 							    iterator.gfn - start_gfn + 1);
1445 				flush = false;
1446 			}
1447 			cond_resched_rwlock_write(&kvm->mmu_lock);
1448 		}
1449 	}
1450 
1451 	return flush;
1452 }
1453 
1454 static __always_inline bool walk_slot_rmaps(struct kvm *kvm,
1455 					    const struct kvm_memory_slot *slot,
1456 					    slot_rmaps_handler fn,
1457 					    int start_level, int end_level,
1458 					    bool flush_on_yield)
1459 {
1460 	return __walk_slot_rmaps(kvm, slot, fn, start_level, end_level,
1461 				 slot->base_gfn, slot->base_gfn + slot->npages - 1,
1462 				 true, flush_on_yield, false);
1463 }
1464 
1465 static __always_inline bool walk_slot_rmaps_4k(struct kvm *kvm,
1466 					       const struct kvm_memory_slot *slot,
1467 					       slot_rmaps_handler fn,
1468 					       bool flush_on_yield)
1469 {
1470 	return walk_slot_rmaps(kvm, slot, fn, PG_LEVEL_4K, PG_LEVEL_4K, flush_on_yield);
1471 }
1472 
1473 static bool __kvm_rmap_zap_gfn_range(struct kvm *kvm,
1474 				     const struct kvm_memory_slot *slot,
1475 				     gfn_t start, gfn_t end, bool can_yield,
1476 				     bool flush)
1477 {
1478 	return __walk_slot_rmaps(kvm, slot, kvm_zap_rmap,
1479 				 PG_LEVEL_4K, KVM_MAX_HUGEPAGE_LEVEL,
1480 				 start, end - 1, can_yield, true, flush);
1481 }
1482 
1483 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
1484 {
1485 	bool flush = false;
1486 
1487 	/*
1488 	 * To prevent races with vCPUs faulting in a gfn using stale data,
1489 	 * zapping a gfn range must be protected by mmu_invalidate_in_progress
1490 	 * (and mmu_invalidate_seq).  The only exception is memslot deletion;
1491 	 * in that case, SRCU synchronization ensures that SPTEs are zapped
1492 	 * after all vCPUs have unlocked SRCU, guaranteeing that vCPUs see the
1493 	 * invalid slot.
1494 	 */
1495 	lockdep_assert_once(kvm->mmu_invalidate_in_progress ||
1496 			    lockdep_is_held(&kvm->slots_lock));
1497 
1498 	if (kvm_memslots_have_rmaps(kvm))
1499 		flush = __kvm_rmap_zap_gfn_range(kvm, range->slot,
1500 						 range->start, range->end,
1501 						 range->may_block, flush);
1502 
1503 	if (tdp_mmu_enabled)
1504 		flush = kvm_tdp_mmu_unmap_gfn_range(kvm, range, flush);
1505 
1506 	if (kvm_x86_ops.set_apic_access_page_addr &&
1507 	    range->slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT)
1508 		kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
1509 
1510 	return flush;
1511 }
1512 
1513 #define RMAP_RECYCLE_THRESHOLD 1000
1514 
1515 static void __rmap_add(struct kvm *kvm,
1516 		       struct kvm_mmu_memory_cache *cache,
1517 		       const struct kvm_memory_slot *slot,
1518 		       u64 *spte, gfn_t gfn, unsigned int access)
1519 {
1520 	struct kvm_mmu_page *sp;
1521 	struct kvm_rmap_head *rmap_head;
1522 	int rmap_count;
1523 
1524 	sp = sptep_to_sp(spte);
1525 	kvm_mmu_page_set_translation(sp, spte_index(spte), gfn, access);
1526 	kvm_update_page_stats(kvm, sp->role.level, 1);
1527 
1528 	rmap_head = gfn_to_rmap(gfn, sp->role.level, slot);
1529 	rmap_count = pte_list_add(cache, spte, rmap_head);
1530 
1531 	if (rmap_count > kvm->stat.max_mmu_rmap_size)
1532 		kvm->stat.max_mmu_rmap_size = rmap_count;
1533 	if (rmap_count > RMAP_RECYCLE_THRESHOLD) {
1534 		kvm_zap_all_rmap_sptes(kvm, rmap_head);
1535 		kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level);
1536 	}
1537 }
1538 
1539 static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot,
1540 		     u64 *spte, gfn_t gfn, unsigned int access)
1541 {
1542 	struct kvm_mmu_memory_cache *cache = &vcpu->arch.mmu_pte_list_desc_cache;
1543 
1544 	__rmap_add(vcpu->kvm, cache, slot, spte, gfn, access);
1545 }
1546 
1547 static bool kvm_rmap_age_gfn_range(struct kvm *kvm,
1548 				   struct kvm_gfn_range *range, bool test_only)
1549 {
1550 	struct slot_rmap_walk_iterator iterator;
1551 	struct rmap_iterator iter;
1552 	bool young = false;
1553 	u64 *sptep;
1554 
1555 	for_each_slot_rmap_range(range->slot, PG_LEVEL_4K, KVM_MAX_HUGEPAGE_LEVEL,
1556 				 range->start, range->end - 1, &iterator) {
1557 		for_each_rmap_spte(iterator.rmap, &iter, sptep) {
1558 			u64 spte = *sptep;
1559 
1560 			if (!is_accessed_spte(spte))
1561 				continue;
1562 
1563 			if (test_only)
1564 				return true;
1565 
1566 			if (spte_ad_enabled(spte)) {
1567 				clear_bit((ffs(shadow_accessed_mask) - 1),
1568 					(unsigned long *)sptep);
1569 			} else {
1570 				/*
1571 				 * WARN if mmu_spte_update() signals the need
1572 				 * for a TLB flush, as Access tracking a SPTE
1573 				 * should never trigger an _immediate_ flush.
1574 				 */
1575 				spte = mark_spte_for_access_track(spte);
1576 				WARN_ON_ONCE(mmu_spte_update(sptep, spte));
1577 			}
1578 			young = true;
1579 		}
1580 	}
1581 	return young;
1582 }
1583 
1584 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
1585 {
1586 	bool young = false;
1587 
1588 	if (kvm_memslots_have_rmaps(kvm))
1589 		young = kvm_rmap_age_gfn_range(kvm, range, false);
1590 
1591 	if (tdp_mmu_enabled)
1592 		young |= kvm_tdp_mmu_age_gfn_range(kvm, range);
1593 
1594 	return young;
1595 }
1596 
1597 bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
1598 {
1599 	bool young = false;
1600 
1601 	if (kvm_memslots_have_rmaps(kvm))
1602 		young = kvm_rmap_age_gfn_range(kvm, range, true);
1603 
1604 	if (tdp_mmu_enabled)
1605 		young |= kvm_tdp_mmu_test_age_gfn(kvm, range);
1606 
1607 	return young;
1608 }
1609 
1610 static void kvm_mmu_check_sptes_at_free(struct kvm_mmu_page *sp)
1611 {
1612 #ifdef CONFIG_KVM_PROVE_MMU
1613 	int i;
1614 
1615 	for (i = 0; i < SPTE_ENT_PER_PAGE; i++) {
1616 		if (KVM_MMU_WARN_ON(is_shadow_present_pte(sp->spt[i])))
1617 			pr_err_ratelimited("SPTE %llx (@ %p) for gfn %llx shadow-present at free",
1618 					   sp->spt[i], &sp->spt[i],
1619 					   kvm_mmu_page_get_gfn(sp, i));
1620 	}
1621 #endif
1622 }
1623 
1624 static void kvm_account_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1625 {
1626 	kvm->arch.n_used_mmu_pages++;
1627 	kvm_account_pgtable_pages((void *)sp->spt, +1);
1628 }
1629 
1630 static void kvm_unaccount_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1631 {
1632 	kvm->arch.n_used_mmu_pages--;
1633 	kvm_account_pgtable_pages((void *)sp->spt, -1);
1634 }
1635 
1636 static void kvm_mmu_free_shadow_page(struct kvm_mmu_page *sp)
1637 {
1638 	kvm_mmu_check_sptes_at_free(sp);
1639 
1640 	hlist_del(&sp->hash_link);
1641 	list_del(&sp->link);
1642 	free_page((unsigned long)sp->spt);
1643 	free_page((unsigned long)sp->shadowed_translation);
1644 	kmem_cache_free(mmu_page_header_cache, sp);
1645 }
1646 
1647 static unsigned kvm_page_table_hashfn(gfn_t gfn)
1648 {
1649 	return hash_64(gfn, KVM_MMU_HASH_SHIFT);
1650 }
1651 
1652 static void mmu_page_add_parent_pte(struct kvm_mmu_memory_cache *cache,
1653 				    struct kvm_mmu_page *sp, u64 *parent_pte)
1654 {
1655 	if (!parent_pte)
1656 		return;
1657 
1658 	pte_list_add(cache, parent_pte, &sp->parent_ptes);
1659 }
1660 
1661 static void mmu_page_remove_parent_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
1662 				       u64 *parent_pte)
1663 {
1664 	pte_list_remove(kvm, parent_pte, &sp->parent_ptes);
1665 }
1666 
1667 static void drop_parent_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
1668 			    u64 *parent_pte)
1669 {
1670 	mmu_page_remove_parent_pte(kvm, sp, parent_pte);
1671 	mmu_spte_clear_no_track(parent_pte);
1672 }
1673 
1674 static void mark_unsync(u64 *spte);
1675 static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
1676 {
1677 	u64 *sptep;
1678 	struct rmap_iterator iter;
1679 
1680 	for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) {
1681 		mark_unsync(sptep);
1682 	}
1683 }
1684 
1685 static void mark_unsync(u64 *spte)
1686 {
1687 	struct kvm_mmu_page *sp;
1688 
1689 	sp = sptep_to_sp(spte);
1690 	if (__test_and_set_bit(spte_index(spte), sp->unsync_child_bitmap))
1691 		return;
1692 	if (sp->unsync_children++)
1693 		return;
1694 	kvm_mmu_mark_parents_unsync(sp);
1695 }
1696 
1697 #define KVM_PAGE_ARRAY_NR 16
1698 
1699 struct kvm_mmu_pages {
1700 	struct mmu_page_and_offset {
1701 		struct kvm_mmu_page *sp;
1702 		unsigned int idx;
1703 	} page[KVM_PAGE_ARRAY_NR];
1704 	unsigned int nr;
1705 };
1706 
1707 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1708 			 int idx)
1709 {
1710 	int i;
1711 
1712 	if (sp->unsync)
1713 		for (i=0; i < pvec->nr; i++)
1714 			if (pvec->page[i].sp == sp)
1715 				return 0;
1716 
1717 	pvec->page[pvec->nr].sp = sp;
1718 	pvec->page[pvec->nr].idx = idx;
1719 	pvec->nr++;
1720 	return (pvec->nr == KVM_PAGE_ARRAY_NR);
1721 }
1722 
1723 static inline void clear_unsync_child_bit(struct kvm_mmu_page *sp, int idx)
1724 {
1725 	--sp->unsync_children;
1726 	WARN_ON_ONCE((int)sp->unsync_children < 0);
1727 	__clear_bit(idx, sp->unsync_child_bitmap);
1728 }
1729 
1730 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1731 			   struct kvm_mmu_pages *pvec)
1732 {
1733 	int i, ret, nr_unsync_leaf = 0;
1734 
1735 	for_each_set_bit(i, sp->unsync_child_bitmap, 512) {
1736 		struct kvm_mmu_page *child;
1737 		u64 ent = sp->spt[i];
1738 
1739 		if (!is_shadow_present_pte(ent) || is_large_pte(ent)) {
1740 			clear_unsync_child_bit(sp, i);
1741 			continue;
1742 		}
1743 
1744 		child = spte_to_child_sp(ent);
1745 
1746 		if (child->unsync_children) {
1747 			if (mmu_pages_add(pvec, child, i))
1748 				return -ENOSPC;
1749 
1750 			ret = __mmu_unsync_walk(child, pvec);
1751 			if (!ret) {
1752 				clear_unsync_child_bit(sp, i);
1753 				continue;
1754 			} else if (ret > 0) {
1755 				nr_unsync_leaf += ret;
1756 			} else
1757 				return ret;
1758 		} else if (child->unsync) {
1759 			nr_unsync_leaf++;
1760 			if (mmu_pages_add(pvec, child, i))
1761 				return -ENOSPC;
1762 		} else
1763 			clear_unsync_child_bit(sp, i);
1764 	}
1765 
1766 	return nr_unsync_leaf;
1767 }
1768 
1769 #define INVALID_INDEX (-1)
1770 
1771 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1772 			   struct kvm_mmu_pages *pvec)
1773 {
1774 	pvec->nr = 0;
1775 	if (!sp->unsync_children)
1776 		return 0;
1777 
1778 	mmu_pages_add(pvec, sp, INVALID_INDEX);
1779 	return __mmu_unsync_walk(sp, pvec);
1780 }
1781 
1782 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1783 {
1784 	WARN_ON_ONCE(!sp->unsync);
1785 	trace_kvm_mmu_sync_page(sp);
1786 	sp->unsync = 0;
1787 	--kvm->stat.mmu_unsync;
1788 }
1789 
1790 static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1791 				     struct list_head *invalid_list);
1792 static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1793 				    struct list_head *invalid_list);
1794 
1795 static bool sp_has_gptes(struct kvm_mmu_page *sp)
1796 {
1797 	if (sp->role.direct)
1798 		return false;
1799 
1800 	if (sp->role.passthrough)
1801 		return false;
1802 
1803 	return true;
1804 }
1805 
1806 #define for_each_valid_sp(_kvm, _sp, _list)				\
1807 	hlist_for_each_entry(_sp, _list, hash_link)			\
1808 		if (is_obsolete_sp((_kvm), (_sp))) {			\
1809 		} else
1810 
1811 #define for_each_gfn_valid_sp_with_gptes(_kvm, _sp, _gfn)		\
1812 	for_each_valid_sp(_kvm, _sp,					\
1813 	  &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)])	\
1814 		if ((_sp)->gfn != (_gfn) || !sp_has_gptes(_sp)) {} else
1815 
1816 static bool kvm_sync_page_check(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1817 {
1818 	union kvm_mmu_page_role root_role = vcpu->arch.mmu->root_role;
1819 
1820 	/*
1821 	 * Ignore various flags when verifying that it's safe to sync a shadow
1822 	 * page using the current MMU context.
1823 	 *
1824 	 *  - level: not part of the overall MMU role and will never match as the MMU's
1825 	 *           level tracks the root level
1826 	 *  - access: updated based on the new guest PTE
1827 	 *  - quadrant: not part of the overall MMU role (similar to level)
1828 	 */
1829 	const union kvm_mmu_page_role sync_role_ign = {
1830 		.level = 0xf,
1831 		.access = 0x7,
1832 		.quadrant = 0x3,
1833 		.passthrough = 0x1,
1834 	};
1835 
1836 	/*
1837 	 * Direct pages can never be unsync, and KVM should never attempt to
1838 	 * sync a shadow page for a different MMU context, e.g. if the role
1839 	 * differs then the memslot lookup (SMM vs. non-SMM) will be bogus, the
1840 	 * reserved bits checks will be wrong, etc...
1841 	 */
1842 	if (WARN_ON_ONCE(sp->role.direct || !vcpu->arch.mmu->sync_spte ||
1843 			 (sp->role.word ^ root_role.word) & ~sync_role_ign.word))
1844 		return false;
1845 
1846 	return true;
1847 }
1848 
1849 static int kvm_sync_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int i)
1850 {
1851 	/* sp->spt[i] has initial value of shadow page table allocation */
1852 	if (sp->spt[i] == SHADOW_NONPRESENT_VALUE)
1853 		return 0;
1854 
1855 	return vcpu->arch.mmu->sync_spte(vcpu, sp, i);
1856 }
1857 
1858 static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1859 {
1860 	int flush = 0;
1861 	int i;
1862 
1863 	if (!kvm_sync_page_check(vcpu, sp))
1864 		return -1;
1865 
1866 	for (i = 0; i < SPTE_ENT_PER_PAGE; i++) {
1867 		int ret = kvm_sync_spte(vcpu, sp, i);
1868 
1869 		if (ret < -1)
1870 			return -1;
1871 		flush |= ret;
1872 	}
1873 
1874 	/*
1875 	 * Note, any flush is purely for KVM's correctness, e.g. when dropping
1876 	 * an existing SPTE or clearing W/A/D bits to ensure an mmu_notifier
1877 	 * unmap or dirty logging event doesn't fail to flush.  The guest is
1878 	 * responsible for flushing the TLB to ensure any changes in protection
1879 	 * bits are recognized, i.e. until the guest flushes or page faults on
1880 	 * a relevant address, KVM is architecturally allowed to let vCPUs use
1881 	 * cached translations with the old protection bits.
1882 	 */
1883 	return flush;
1884 }
1885 
1886 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1887 			 struct list_head *invalid_list)
1888 {
1889 	int ret = __kvm_sync_page(vcpu, sp);
1890 
1891 	if (ret < 0)
1892 		kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
1893 	return ret;
1894 }
1895 
1896 static bool kvm_mmu_remote_flush_or_zap(struct kvm *kvm,
1897 					struct list_head *invalid_list,
1898 					bool remote_flush)
1899 {
1900 	if (!remote_flush && list_empty(invalid_list))
1901 		return false;
1902 
1903 	if (!list_empty(invalid_list))
1904 		kvm_mmu_commit_zap_page(kvm, invalid_list);
1905 	else
1906 		kvm_flush_remote_tlbs(kvm);
1907 	return true;
1908 }
1909 
1910 static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
1911 {
1912 	if (sp->role.invalid)
1913 		return true;
1914 
1915 	/* TDP MMU pages do not use the MMU generation. */
1916 	return !is_tdp_mmu_page(sp) &&
1917 	       unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
1918 }
1919 
1920 struct mmu_page_path {
1921 	struct kvm_mmu_page *parent[PT64_ROOT_MAX_LEVEL];
1922 	unsigned int idx[PT64_ROOT_MAX_LEVEL];
1923 };
1924 
1925 #define for_each_sp(pvec, sp, parents, i)			\
1926 		for (i = mmu_pages_first(&pvec, &parents);	\
1927 			i < pvec.nr && ({ sp = pvec.page[i].sp; 1;});	\
1928 			i = mmu_pages_next(&pvec, &parents, i))
1929 
1930 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1931 			  struct mmu_page_path *parents,
1932 			  int i)
1933 {
1934 	int n;
1935 
1936 	for (n = i+1; n < pvec->nr; n++) {
1937 		struct kvm_mmu_page *sp = pvec->page[n].sp;
1938 		unsigned idx = pvec->page[n].idx;
1939 		int level = sp->role.level;
1940 
1941 		parents->idx[level-1] = idx;
1942 		if (level == PG_LEVEL_4K)
1943 			break;
1944 
1945 		parents->parent[level-2] = sp;
1946 	}
1947 
1948 	return n;
1949 }
1950 
1951 static int mmu_pages_first(struct kvm_mmu_pages *pvec,
1952 			   struct mmu_page_path *parents)
1953 {
1954 	struct kvm_mmu_page *sp;
1955 	int level;
1956 
1957 	if (pvec->nr == 0)
1958 		return 0;
1959 
1960 	WARN_ON_ONCE(pvec->page[0].idx != INVALID_INDEX);
1961 
1962 	sp = pvec->page[0].sp;
1963 	level = sp->role.level;
1964 	WARN_ON_ONCE(level == PG_LEVEL_4K);
1965 
1966 	parents->parent[level-2] = sp;
1967 
1968 	/* Also set up a sentinel.  Further entries in pvec are all
1969 	 * children of sp, so this element is never overwritten.
1970 	 */
1971 	parents->parent[level-1] = NULL;
1972 	return mmu_pages_next(pvec, parents, 0);
1973 }
1974 
1975 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1976 {
1977 	struct kvm_mmu_page *sp;
1978 	unsigned int level = 0;
1979 
1980 	do {
1981 		unsigned int idx = parents->idx[level];
1982 		sp = parents->parent[level];
1983 		if (!sp)
1984 			return;
1985 
1986 		WARN_ON_ONCE(idx == INVALID_INDEX);
1987 		clear_unsync_child_bit(sp, idx);
1988 		level++;
1989 	} while (!sp->unsync_children);
1990 }
1991 
1992 static int mmu_sync_children(struct kvm_vcpu *vcpu,
1993 			     struct kvm_mmu_page *parent, bool can_yield)
1994 {
1995 	int i;
1996 	struct kvm_mmu_page *sp;
1997 	struct mmu_page_path parents;
1998 	struct kvm_mmu_pages pages;
1999 	LIST_HEAD(invalid_list);
2000 	bool flush = false;
2001 
2002 	while (mmu_unsync_walk(parent, &pages)) {
2003 		bool protected = false;
2004 
2005 		for_each_sp(pages, sp, parents, i)
2006 			protected |= kvm_vcpu_write_protect_gfn(vcpu, sp->gfn);
2007 
2008 		if (protected) {
2009 			kvm_mmu_remote_flush_or_zap(vcpu->kvm, &invalid_list, true);
2010 			flush = false;
2011 		}
2012 
2013 		for_each_sp(pages, sp, parents, i) {
2014 			kvm_unlink_unsync_page(vcpu->kvm, sp);
2015 			flush |= kvm_sync_page(vcpu, sp, &invalid_list) > 0;
2016 			mmu_pages_clear_parents(&parents);
2017 		}
2018 		if (need_resched() || rwlock_needbreak(&vcpu->kvm->mmu_lock)) {
2019 			kvm_mmu_remote_flush_or_zap(vcpu->kvm, &invalid_list, flush);
2020 			if (!can_yield) {
2021 				kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
2022 				return -EINTR;
2023 			}
2024 
2025 			cond_resched_rwlock_write(&vcpu->kvm->mmu_lock);
2026 			flush = false;
2027 		}
2028 	}
2029 
2030 	kvm_mmu_remote_flush_or_zap(vcpu->kvm, &invalid_list, flush);
2031 	return 0;
2032 }
2033 
2034 static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
2035 {
2036 	atomic_set(&sp->write_flooding_count,  0);
2037 }
2038 
2039 static void clear_sp_write_flooding_count(u64 *spte)
2040 {
2041 	__clear_sp_write_flooding_count(sptep_to_sp(spte));
2042 }
2043 
2044 /*
2045  * The vCPU is required when finding indirect shadow pages; the shadow
2046  * page may already exist and syncing it needs the vCPU pointer in
2047  * order to read guest page tables.  Direct shadow pages are never
2048  * unsync, thus @vcpu can be NULL if @role.direct is true.
2049  */
2050 static struct kvm_mmu_page *kvm_mmu_find_shadow_page(struct kvm *kvm,
2051 						     struct kvm_vcpu *vcpu,
2052 						     gfn_t gfn,
2053 						     struct hlist_head *sp_list,
2054 						     union kvm_mmu_page_role role)
2055 {
2056 	struct kvm_mmu_page *sp;
2057 	int ret;
2058 	int collisions = 0;
2059 	LIST_HEAD(invalid_list);
2060 
2061 	for_each_valid_sp(kvm, sp, sp_list) {
2062 		if (sp->gfn != gfn) {
2063 			collisions++;
2064 			continue;
2065 		}
2066 
2067 		if (sp->role.word != role.word) {
2068 			/*
2069 			 * If the guest is creating an upper-level page, zap
2070 			 * unsync pages for the same gfn.  While it's possible
2071 			 * the guest is using recursive page tables, in all
2072 			 * likelihood the guest has stopped using the unsync
2073 			 * page and is installing a completely unrelated page.
2074 			 * Unsync pages must not be left as is, because the new
2075 			 * upper-level page will be write-protected.
2076 			 */
2077 			if (role.level > PG_LEVEL_4K && sp->unsync)
2078 				kvm_mmu_prepare_zap_page(kvm, sp,
2079 							 &invalid_list);
2080 			continue;
2081 		}
2082 
2083 		/* unsync and write-flooding only apply to indirect SPs. */
2084 		if (sp->role.direct)
2085 			goto out;
2086 
2087 		if (sp->unsync) {
2088 			if (KVM_BUG_ON(!vcpu, kvm))
2089 				break;
2090 
2091 			/*
2092 			 * The page is good, but is stale.  kvm_sync_page does
2093 			 * get the latest guest state, but (unlike mmu_unsync_children)
2094 			 * it doesn't write-protect the page or mark it synchronized!
2095 			 * This way the validity of the mapping is ensured, but the
2096 			 * overhead of write protection is not incurred until the
2097 			 * guest invalidates the TLB mapping.  This allows multiple
2098 			 * SPs for a single gfn to be unsync.
2099 			 *
2100 			 * If the sync fails, the page is zapped.  If so, break
2101 			 * in order to rebuild it.
2102 			 */
2103 			ret = kvm_sync_page(vcpu, sp, &invalid_list);
2104 			if (ret < 0)
2105 				break;
2106 
2107 			WARN_ON_ONCE(!list_empty(&invalid_list));
2108 			if (ret > 0)
2109 				kvm_flush_remote_tlbs(kvm);
2110 		}
2111 
2112 		__clear_sp_write_flooding_count(sp);
2113 
2114 		goto out;
2115 	}
2116 
2117 	sp = NULL;
2118 	++kvm->stat.mmu_cache_miss;
2119 
2120 out:
2121 	kvm_mmu_commit_zap_page(kvm, &invalid_list);
2122 
2123 	if (collisions > kvm->stat.max_mmu_page_hash_collisions)
2124 		kvm->stat.max_mmu_page_hash_collisions = collisions;
2125 	return sp;
2126 }
2127 
2128 /* Caches used when allocating a new shadow page. */
2129 struct shadow_page_caches {
2130 	struct kvm_mmu_memory_cache *page_header_cache;
2131 	struct kvm_mmu_memory_cache *shadow_page_cache;
2132 	struct kvm_mmu_memory_cache *shadowed_info_cache;
2133 };
2134 
2135 static struct kvm_mmu_page *kvm_mmu_alloc_shadow_page(struct kvm *kvm,
2136 						      struct shadow_page_caches *caches,
2137 						      gfn_t gfn,
2138 						      struct hlist_head *sp_list,
2139 						      union kvm_mmu_page_role role)
2140 {
2141 	struct kvm_mmu_page *sp;
2142 
2143 	sp = kvm_mmu_memory_cache_alloc(caches->page_header_cache);
2144 	sp->spt = kvm_mmu_memory_cache_alloc(caches->shadow_page_cache);
2145 	if (!role.direct && role.level <= KVM_MAX_HUGEPAGE_LEVEL)
2146 		sp->shadowed_translation = kvm_mmu_memory_cache_alloc(caches->shadowed_info_cache);
2147 
2148 	set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
2149 
2150 	INIT_LIST_HEAD(&sp->possible_nx_huge_page_link);
2151 
2152 	/*
2153 	 * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages()
2154 	 * depends on valid pages being added to the head of the list.  See
2155 	 * comments in kvm_zap_obsolete_pages().
2156 	 */
2157 	sp->mmu_valid_gen = kvm->arch.mmu_valid_gen;
2158 	list_add(&sp->link, &kvm->arch.active_mmu_pages);
2159 	kvm_account_mmu_page(kvm, sp);
2160 
2161 	sp->gfn = gfn;
2162 	sp->role = role;
2163 	hlist_add_head(&sp->hash_link, sp_list);
2164 	if (sp_has_gptes(sp))
2165 		account_shadowed(kvm, sp);
2166 
2167 	return sp;
2168 }
2169 
2170 /* Note, @vcpu may be NULL if @role.direct is true; see kvm_mmu_find_shadow_page. */
2171 static struct kvm_mmu_page *__kvm_mmu_get_shadow_page(struct kvm *kvm,
2172 						      struct kvm_vcpu *vcpu,
2173 						      struct shadow_page_caches *caches,
2174 						      gfn_t gfn,
2175 						      union kvm_mmu_page_role role)
2176 {
2177 	struct hlist_head *sp_list;
2178 	struct kvm_mmu_page *sp;
2179 	bool created = false;
2180 
2181 	sp_list = &kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)];
2182 
2183 	sp = kvm_mmu_find_shadow_page(kvm, vcpu, gfn, sp_list, role);
2184 	if (!sp) {
2185 		created = true;
2186 		sp = kvm_mmu_alloc_shadow_page(kvm, caches, gfn, sp_list, role);
2187 	}
2188 
2189 	trace_kvm_mmu_get_page(sp, created);
2190 	return sp;
2191 }
2192 
2193 static struct kvm_mmu_page *kvm_mmu_get_shadow_page(struct kvm_vcpu *vcpu,
2194 						    gfn_t gfn,
2195 						    union kvm_mmu_page_role role)
2196 {
2197 	struct shadow_page_caches caches = {
2198 		.page_header_cache = &vcpu->arch.mmu_page_header_cache,
2199 		.shadow_page_cache = &vcpu->arch.mmu_shadow_page_cache,
2200 		.shadowed_info_cache = &vcpu->arch.mmu_shadowed_info_cache,
2201 	};
2202 
2203 	return __kvm_mmu_get_shadow_page(vcpu->kvm, vcpu, &caches, gfn, role);
2204 }
2205 
2206 static union kvm_mmu_page_role kvm_mmu_child_role(u64 *sptep, bool direct,
2207 						  unsigned int access)
2208 {
2209 	struct kvm_mmu_page *parent_sp = sptep_to_sp(sptep);
2210 	union kvm_mmu_page_role role;
2211 
2212 	role = parent_sp->role;
2213 	role.level--;
2214 	role.access = access;
2215 	role.direct = direct;
2216 	role.passthrough = 0;
2217 
2218 	/*
2219 	 * If the guest has 4-byte PTEs then that means it's using 32-bit,
2220 	 * 2-level, non-PAE paging. KVM shadows such guests with PAE paging
2221 	 * (i.e. 8-byte PTEs). The difference in PTE size means that KVM must
2222 	 * shadow each guest page table with multiple shadow page tables, which
2223 	 * requires extra bookkeeping in the role.
2224 	 *
2225 	 * Specifically, to shadow the guest's page directory (which covers a
2226 	 * 4GiB address space), KVM uses 4 PAE page directories, each mapping
2227 	 * 1GiB of the address space. @role.quadrant encodes which quarter of
2228 	 * the address space each maps.
2229 	 *
2230 	 * To shadow the guest's page tables (which each map a 4MiB region), KVM
2231 	 * uses 2 PAE page tables, each mapping a 2MiB region. For these,
2232 	 * @role.quadrant encodes which half of the region they map.
2233 	 *
2234 	 * Concretely, a 4-byte PDE consumes bits 31:22, while an 8-byte PDE
2235 	 * consumes bits 29:21.  To consume bits 31:30, KVM's uses 4 shadow
2236 	 * PDPTEs; those 4 PAE page directories are pre-allocated and their
2237 	 * quadrant is assigned in mmu_alloc_root().   A 4-byte PTE consumes
2238 	 * bits 21:12, while an 8-byte PTE consumes bits 20:12.  To consume
2239 	 * bit 21 in the PTE (the child here), KVM propagates that bit to the
2240 	 * quadrant, i.e. sets quadrant to '0' or '1'.  The parent 8-byte PDE
2241 	 * covers bit 21 (see above), thus the quadrant is calculated from the
2242 	 * _least_ significant bit of the PDE index.
2243 	 */
2244 	if (role.has_4_byte_gpte) {
2245 		WARN_ON_ONCE(role.level != PG_LEVEL_4K);
2246 		role.quadrant = spte_index(sptep) & 1;
2247 	}
2248 
2249 	return role;
2250 }
2251 
2252 static struct kvm_mmu_page *kvm_mmu_get_child_sp(struct kvm_vcpu *vcpu,
2253 						 u64 *sptep, gfn_t gfn,
2254 						 bool direct, unsigned int access)
2255 {
2256 	union kvm_mmu_page_role role;
2257 
2258 	if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep))
2259 		return ERR_PTR(-EEXIST);
2260 
2261 	role = kvm_mmu_child_role(sptep, direct, access);
2262 	return kvm_mmu_get_shadow_page(vcpu, gfn, role);
2263 }
2264 
2265 static void shadow_walk_init_using_root(struct kvm_shadow_walk_iterator *iterator,
2266 					struct kvm_vcpu *vcpu, hpa_t root,
2267 					u64 addr)
2268 {
2269 	iterator->addr = addr;
2270 	iterator->shadow_addr = root;
2271 	iterator->level = vcpu->arch.mmu->root_role.level;
2272 
2273 	if (iterator->level >= PT64_ROOT_4LEVEL &&
2274 	    vcpu->arch.mmu->cpu_role.base.level < PT64_ROOT_4LEVEL &&
2275 	    !vcpu->arch.mmu->root_role.direct)
2276 		iterator->level = PT32E_ROOT_LEVEL;
2277 
2278 	if (iterator->level == PT32E_ROOT_LEVEL) {
2279 		/*
2280 		 * prev_root is currently only used for 64-bit hosts. So only
2281 		 * the active root_hpa is valid here.
2282 		 */
2283 		BUG_ON(root != vcpu->arch.mmu->root.hpa);
2284 
2285 		iterator->shadow_addr
2286 			= vcpu->arch.mmu->pae_root[(addr >> 30) & 3];
2287 		iterator->shadow_addr &= SPTE_BASE_ADDR_MASK;
2288 		--iterator->level;
2289 		if (!iterator->shadow_addr)
2290 			iterator->level = 0;
2291 	}
2292 }
2293 
2294 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
2295 			     struct kvm_vcpu *vcpu, u64 addr)
2296 {
2297 	shadow_walk_init_using_root(iterator, vcpu, vcpu->arch.mmu->root.hpa,
2298 				    addr);
2299 }
2300 
2301 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
2302 {
2303 	if (iterator->level < PG_LEVEL_4K)
2304 		return false;
2305 
2306 	iterator->index = SPTE_INDEX(iterator->addr, iterator->level);
2307 	iterator->sptep	= ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
2308 	return true;
2309 }
2310 
2311 static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
2312 			       u64 spte)
2313 {
2314 	if (!is_shadow_present_pte(spte) || is_last_spte(spte, iterator->level)) {
2315 		iterator->level = 0;
2316 		return;
2317 	}
2318 
2319 	iterator->shadow_addr = spte & SPTE_BASE_ADDR_MASK;
2320 	--iterator->level;
2321 }
2322 
2323 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
2324 {
2325 	__shadow_walk_next(iterator, *iterator->sptep);
2326 }
2327 
2328 static void __link_shadow_page(struct kvm *kvm,
2329 			       struct kvm_mmu_memory_cache *cache, u64 *sptep,
2330 			       struct kvm_mmu_page *sp, bool flush)
2331 {
2332 	u64 spte;
2333 
2334 	BUILD_BUG_ON(VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK);
2335 
2336 	/*
2337 	 * If an SPTE is present already, it must be a leaf and therefore
2338 	 * a large one.  Drop it, and flush the TLB if needed, before
2339 	 * installing sp.
2340 	 */
2341 	if (is_shadow_present_pte(*sptep))
2342 		drop_large_spte(kvm, sptep, flush);
2343 
2344 	spte = make_nonleaf_spte(sp->spt, sp_ad_disabled(sp));
2345 
2346 	mmu_spte_set(sptep, spte);
2347 
2348 	mmu_page_add_parent_pte(cache, sp, sptep);
2349 
2350 	/*
2351 	 * The non-direct sub-pagetable must be updated before linking.  For
2352 	 * L1 sp, the pagetable is updated via kvm_sync_page() in
2353 	 * kvm_mmu_find_shadow_page() without write-protecting the gfn,
2354 	 * so sp->unsync can be true or false.  For higher level non-direct
2355 	 * sp, the pagetable is updated/synced via mmu_sync_children() in
2356 	 * FNAME(fetch)(), so sp->unsync_children can only be false.
2357 	 * WARN_ON_ONCE() if anything happens unexpectedly.
2358 	 */
2359 	if (WARN_ON_ONCE(sp->unsync_children) || sp->unsync)
2360 		mark_unsync(sptep);
2361 }
2362 
2363 static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep,
2364 			     struct kvm_mmu_page *sp)
2365 {
2366 	__link_shadow_page(vcpu->kvm, &vcpu->arch.mmu_pte_list_desc_cache, sptep, sp, true);
2367 }
2368 
2369 static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2370 				   unsigned direct_access)
2371 {
2372 	if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
2373 		struct kvm_mmu_page *child;
2374 
2375 		/*
2376 		 * For the direct sp, if the guest pte's dirty bit
2377 		 * changed form clean to dirty, it will corrupt the
2378 		 * sp's access: allow writable in the read-only sp,
2379 		 * so we should update the spte at this point to get
2380 		 * a new sp with the correct access.
2381 		 */
2382 		child = spte_to_child_sp(*sptep);
2383 		if (child->role.access == direct_access)
2384 			return;
2385 
2386 		drop_parent_pte(vcpu->kvm, child, sptep);
2387 		kvm_flush_remote_tlbs_sptep(vcpu->kvm, sptep);
2388 	}
2389 }
2390 
2391 /* Returns the number of zapped non-leaf child shadow pages. */
2392 static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
2393 			    u64 *spte, struct list_head *invalid_list)
2394 {
2395 	u64 pte;
2396 	struct kvm_mmu_page *child;
2397 
2398 	pte = *spte;
2399 	if (is_shadow_present_pte(pte)) {
2400 		if (is_last_spte(pte, sp->role.level)) {
2401 			drop_spte(kvm, spte);
2402 		} else {
2403 			child = spte_to_child_sp(pte);
2404 			drop_parent_pte(kvm, child, spte);
2405 
2406 			/*
2407 			 * Recursively zap nested TDP SPs, parentless SPs are
2408 			 * unlikely to be used again in the near future.  This
2409 			 * avoids retaining a large number of stale nested SPs.
2410 			 */
2411 			if (tdp_enabled && invalid_list &&
2412 			    child->role.guest_mode && !child->parent_ptes.val)
2413 				return kvm_mmu_prepare_zap_page(kvm, child,
2414 								invalid_list);
2415 		}
2416 	} else if (is_mmio_spte(kvm, pte)) {
2417 		mmu_spte_clear_no_track(spte);
2418 	}
2419 	return 0;
2420 }
2421 
2422 static int kvm_mmu_page_unlink_children(struct kvm *kvm,
2423 					struct kvm_mmu_page *sp,
2424 					struct list_head *invalid_list)
2425 {
2426 	int zapped = 0;
2427 	unsigned i;
2428 
2429 	for (i = 0; i < SPTE_ENT_PER_PAGE; ++i)
2430 		zapped += mmu_page_zap_pte(kvm, sp, sp->spt + i, invalid_list);
2431 
2432 	return zapped;
2433 }
2434 
2435 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
2436 {
2437 	u64 *sptep;
2438 	struct rmap_iterator iter;
2439 
2440 	while ((sptep = rmap_get_first(&sp->parent_ptes, &iter)))
2441 		drop_parent_pte(kvm, sp, sptep);
2442 }
2443 
2444 static int mmu_zap_unsync_children(struct kvm *kvm,
2445 				   struct kvm_mmu_page *parent,
2446 				   struct list_head *invalid_list)
2447 {
2448 	int i, zapped = 0;
2449 	struct mmu_page_path parents;
2450 	struct kvm_mmu_pages pages;
2451 
2452 	if (parent->role.level == PG_LEVEL_4K)
2453 		return 0;
2454 
2455 	while (mmu_unsync_walk(parent, &pages)) {
2456 		struct kvm_mmu_page *sp;
2457 
2458 		for_each_sp(pages, sp, parents, i) {
2459 			kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
2460 			mmu_pages_clear_parents(&parents);
2461 			zapped++;
2462 		}
2463 	}
2464 
2465 	return zapped;
2466 }
2467 
2468 static bool __kvm_mmu_prepare_zap_page(struct kvm *kvm,
2469 				       struct kvm_mmu_page *sp,
2470 				       struct list_head *invalid_list,
2471 				       int *nr_zapped)
2472 {
2473 	bool list_unstable, zapped_root = false;
2474 
2475 	lockdep_assert_held_write(&kvm->mmu_lock);
2476 	trace_kvm_mmu_prepare_zap_page(sp);
2477 	++kvm->stat.mmu_shadow_zapped;
2478 	*nr_zapped = mmu_zap_unsync_children(kvm, sp, invalid_list);
2479 	*nr_zapped += kvm_mmu_page_unlink_children(kvm, sp, invalid_list);
2480 	kvm_mmu_unlink_parents(kvm, sp);
2481 
2482 	/* Zapping children means active_mmu_pages has become unstable. */
2483 	list_unstable = *nr_zapped;
2484 
2485 	if (!sp->role.invalid && sp_has_gptes(sp))
2486 		unaccount_shadowed(kvm, sp);
2487 
2488 	if (sp->unsync)
2489 		kvm_unlink_unsync_page(kvm, sp);
2490 	if (!sp->root_count) {
2491 		/* Count self */
2492 		(*nr_zapped)++;
2493 
2494 		/*
2495 		 * Already invalid pages (previously active roots) are not on
2496 		 * the active page list.  See list_del() in the "else" case of
2497 		 * !sp->root_count.
2498 		 */
2499 		if (sp->role.invalid)
2500 			list_add(&sp->link, invalid_list);
2501 		else
2502 			list_move(&sp->link, invalid_list);
2503 		kvm_unaccount_mmu_page(kvm, sp);
2504 	} else {
2505 		/*
2506 		 * Remove the active root from the active page list, the root
2507 		 * will be explicitly freed when the root_count hits zero.
2508 		 */
2509 		list_del(&sp->link);
2510 
2511 		/*
2512 		 * Obsolete pages cannot be used on any vCPUs, see the comment
2513 		 * in kvm_mmu_zap_all_fast().  Note, is_obsolete_sp() also
2514 		 * treats invalid shadow pages as being obsolete.
2515 		 */
2516 		zapped_root = !is_obsolete_sp(kvm, sp);
2517 	}
2518 
2519 	if (sp->nx_huge_page_disallowed)
2520 		unaccount_nx_huge_page(kvm, sp);
2521 
2522 	sp->role.invalid = 1;
2523 
2524 	/*
2525 	 * Make the request to free obsolete roots after marking the root
2526 	 * invalid, otherwise other vCPUs may not see it as invalid.
2527 	 */
2528 	if (zapped_root)
2529 		kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_FREE_OBSOLETE_ROOTS);
2530 	return list_unstable;
2531 }
2532 
2533 static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
2534 				     struct list_head *invalid_list)
2535 {
2536 	int nr_zapped;
2537 
2538 	__kvm_mmu_prepare_zap_page(kvm, sp, invalid_list, &nr_zapped);
2539 	return nr_zapped;
2540 }
2541 
2542 static void kvm_mmu_commit_zap_page(struct kvm *kvm,
2543 				    struct list_head *invalid_list)
2544 {
2545 	struct kvm_mmu_page *sp, *nsp;
2546 
2547 	if (list_empty(invalid_list))
2548 		return;
2549 
2550 	/*
2551 	 * We need to make sure everyone sees our modifications to
2552 	 * the page tables and see changes to vcpu->mode here. The barrier
2553 	 * in the kvm_flush_remote_tlbs() achieves this. This pairs
2554 	 * with vcpu_enter_guest and walk_shadow_page_lockless_begin/end.
2555 	 *
2556 	 * In addition, kvm_flush_remote_tlbs waits for all vcpus to exit
2557 	 * guest mode and/or lockless shadow page table walks.
2558 	 */
2559 	kvm_flush_remote_tlbs(kvm);
2560 
2561 	list_for_each_entry_safe(sp, nsp, invalid_list, link) {
2562 		WARN_ON_ONCE(!sp->role.invalid || sp->root_count);
2563 		kvm_mmu_free_shadow_page(sp);
2564 	}
2565 }
2566 
2567 static unsigned long kvm_mmu_zap_oldest_mmu_pages(struct kvm *kvm,
2568 						  unsigned long nr_to_zap)
2569 {
2570 	unsigned long total_zapped = 0;
2571 	struct kvm_mmu_page *sp, *tmp;
2572 	LIST_HEAD(invalid_list);
2573 	bool unstable;
2574 	int nr_zapped;
2575 
2576 	if (list_empty(&kvm->arch.active_mmu_pages))
2577 		return 0;
2578 
2579 restart:
2580 	list_for_each_entry_safe_reverse(sp, tmp, &kvm->arch.active_mmu_pages, link) {
2581 		/*
2582 		 * Don't zap active root pages, the page itself can't be freed
2583 		 * and zapping it will just force vCPUs to realloc and reload.
2584 		 */
2585 		if (sp->root_count)
2586 			continue;
2587 
2588 		unstable = __kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list,
2589 						      &nr_zapped);
2590 		total_zapped += nr_zapped;
2591 		if (total_zapped >= nr_to_zap)
2592 			break;
2593 
2594 		if (unstable)
2595 			goto restart;
2596 	}
2597 
2598 	kvm_mmu_commit_zap_page(kvm, &invalid_list);
2599 
2600 	kvm->stat.mmu_recycled += total_zapped;
2601 	return total_zapped;
2602 }
2603 
2604 static inline unsigned long kvm_mmu_available_pages(struct kvm *kvm)
2605 {
2606 	if (kvm->arch.n_max_mmu_pages > kvm->arch.n_used_mmu_pages)
2607 		return kvm->arch.n_max_mmu_pages -
2608 			kvm->arch.n_used_mmu_pages;
2609 
2610 	return 0;
2611 }
2612 
2613 static int make_mmu_pages_available(struct kvm_vcpu *vcpu)
2614 {
2615 	unsigned long avail = kvm_mmu_available_pages(vcpu->kvm);
2616 
2617 	if (likely(avail >= KVM_MIN_FREE_MMU_PAGES))
2618 		return 0;
2619 
2620 	kvm_mmu_zap_oldest_mmu_pages(vcpu->kvm, KVM_REFILL_PAGES - avail);
2621 
2622 	/*
2623 	 * Note, this check is intentionally soft, it only guarantees that one
2624 	 * page is available, while the caller may end up allocating as many as
2625 	 * four pages, e.g. for PAE roots or for 5-level paging.  Temporarily
2626 	 * exceeding the (arbitrary by default) limit will not harm the host,
2627 	 * being too aggressive may unnecessarily kill the guest, and getting an
2628 	 * exact count is far more trouble than it's worth, especially in the
2629 	 * page fault paths.
2630 	 */
2631 	if (!kvm_mmu_available_pages(vcpu->kvm))
2632 		return -ENOSPC;
2633 	return 0;
2634 }
2635 
2636 /*
2637  * Changing the number of mmu pages allocated to the vm
2638  * Note: if goal_nr_mmu_pages is too small, you will get dead lock
2639  */
2640 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long goal_nr_mmu_pages)
2641 {
2642 	write_lock(&kvm->mmu_lock);
2643 
2644 	if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
2645 		kvm_mmu_zap_oldest_mmu_pages(kvm, kvm->arch.n_used_mmu_pages -
2646 						  goal_nr_mmu_pages);
2647 
2648 		goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
2649 	}
2650 
2651 	kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
2652 
2653 	write_unlock(&kvm->mmu_lock);
2654 }
2655 
2656 bool __kvm_mmu_unprotect_gfn_and_retry(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
2657 				       bool always_retry)
2658 {
2659 	struct kvm *kvm = vcpu->kvm;
2660 	LIST_HEAD(invalid_list);
2661 	struct kvm_mmu_page *sp;
2662 	gpa_t gpa = cr2_or_gpa;
2663 	bool r = false;
2664 
2665 	/*
2666 	 * Bail early if there aren't any write-protected shadow pages to avoid
2667 	 * unnecessarily taking mmu_lock lock, e.g. if the gfn is write-tracked
2668 	 * by a third party.  Reading indirect_shadow_pages without holding
2669 	 * mmu_lock is safe, as this is purely an optimization, i.e. a false
2670 	 * positive is benign, and a false negative will simply result in KVM
2671 	 * skipping the unprotect+retry path, which is also an optimization.
2672 	 */
2673 	if (!READ_ONCE(kvm->arch.indirect_shadow_pages))
2674 		goto out;
2675 
2676 	if (!vcpu->arch.mmu->root_role.direct) {
2677 		gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
2678 		if (gpa == INVALID_GPA)
2679 			goto out;
2680 	}
2681 
2682 	write_lock(&kvm->mmu_lock);
2683 	for_each_gfn_valid_sp_with_gptes(kvm, sp, gpa_to_gfn(gpa))
2684 		kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
2685 
2686 	/*
2687 	 * Snapshot the result before zapping, as zapping will remove all list
2688 	 * entries, i.e. checking the list later would yield a false negative.
2689 	 */
2690 	r = !list_empty(&invalid_list);
2691 	kvm_mmu_commit_zap_page(kvm, &invalid_list);
2692 	write_unlock(&kvm->mmu_lock);
2693 
2694 out:
2695 	if (r || always_retry) {
2696 		vcpu->arch.last_retry_eip = kvm_rip_read(vcpu);
2697 		vcpu->arch.last_retry_addr = cr2_or_gpa;
2698 	}
2699 	return r;
2700 }
2701 
2702 static void kvm_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
2703 {
2704 	trace_kvm_mmu_unsync_page(sp);
2705 	++kvm->stat.mmu_unsync;
2706 	sp->unsync = 1;
2707 
2708 	kvm_mmu_mark_parents_unsync(sp);
2709 }
2710 
2711 /*
2712  * Attempt to unsync any shadow pages that can be reached by the specified gfn,
2713  * KVM is creating a writable mapping for said gfn.  Returns 0 if all pages
2714  * were marked unsync (or if there is no shadow page), -EPERM if the SPTE must
2715  * be write-protected.
2716  */
2717 int mmu_try_to_unsync_pages(struct kvm *kvm, const struct kvm_memory_slot *slot,
2718 			    gfn_t gfn, bool synchronizing, bool prefetch)
2719 {
2720 	struct kvm_mmu_page *sp;
2721 	bool locked = false;
2722 
2723 	/*
2724 	 * Force write-protection if the page is being tracked.  Note, the page
2725 	 * track machinery is used to write-protect upper-level shadow pages,
2726 	 * i.e. this guards the role.level == 4K assertion below!
2727 	 */
2728 	if (kvm_gfn_is_write_tracked(kvm, slot, gfn))
2729 		return -EPERM;
2730 
2731 	/*
2732 	 * The page is not write-tracked, mark existing shadow pages unsync
2733 	 * unless KVM is synchronizing an unsync SP.  In that case, KVM must
2734 	 * complete emulation of the guest TLB flush before allowing shadow
2735 	 * pages to become unsync (writable by the guest).
2736 	 */
2737 	for_each_gfn_valid_sp_with_gptes(kvm, sp, gfn) {
2738 		if (synchronizing)
2739 			return -EPERM;
2740 
2741 		if (sp->unsync)
2742 			continue;
2743 
2744 		if (prefetch)
2745 			return -EEXIST;
2746 
2747 		/*
2748 		 * TDP MMU page faults require an additional spinlock as they
2749 		 * run with mmu_lock held for read, not write, and the unsync
2750 		 * logic is not thread safe.  Take the spinklock regardless of
2751 		 * the MMU type to avoid extra conditionals/parameters, there's
2752 		 * no meaningful penalty if mmu_lock is held for write.
2753 		 */
2754 		if (!locked) {
2755 			locked = true;
2756 			spin_lock(&kvm->arch.mmu_unsync_pages_lock);
2757 
2758 			/*
2759 			 * Recheck after taking the spinlock, a different vCPU
2760 			 * may have since marked the page unsync.  A false
2761 			 * negative on the unprotected check above is not
2762 			 * possible as clearing sp->unsync _must_ hold mmu_lock
2763 			 * for write, i.e. unsync cannot transition from 1->0
2764 			 * while this CPU holds mmu_lock for read (or write).
2765 			 */
2766 			if (READ_ONCE(sp->unsync))
2767 				continue;
2768 		}
2769 
2770 		WARN_ON_ONCE(sp->role.level != PG_LEVEL_4K);
2771 		kvm_unsync_page(kvm, sp);
2772 	}
2773 	if (locked)
2774 		spin_unlock(&kvm->arch.mmu_unsync_pages_lock);
2775 
2776 	/*
2777 	 * We need to ensure that the marking of unsync pages is visible
2778 	 * before the SPTE is updated to allow writes because
2779 	 * kvm_mmu_sync_roots() checks the unsync flags without holding
2780 	 * the MMU lock and so can race with this. If the SPTE was updated
2781 	 * before the page had been marked as unsync-ed, something like the
2782 	 * following could happen:
2783 	 *
2784 	 * CPU 1                    CPU 2
2785 	 * ---------------------------------------------------------------------
2786 	 * 1.2 Host updates SPTE
2787 	 *     to be writable
2788 	 *                      2.1 Guest writes a GPTE for GVA X.
2789 	 *                          (GPTE being in the guest page table shadowed
2790 	 *                           by the SP from CPU 1.)
2791 	 *                          This reads SPTE during the page table walk.
2792 	 *                          Since SPTE.W is read as 1, there is no
2793 	 *                          fault.
2794 	 *
2795 	 *                      2.2 Guest issues TLB flush.
2796 	 *                          That causes a VM Exit.
2797 	 *
2798 	 *                      2.3 Walking of unsync pages sees sp->unsync is
2799 	 *                          false and skips the page.
2800 	 *
2801 	 *                      2.4 Guest accesses GVA X.
2802 	 *                          Since the mapping in the SP was not updated,
2803 	 *                          so the old mapping for GVA X incorrectly
2804 	 *                          gets used.
2805 	 * 1.1 Host marks SP
2806 	 *     as unsync
2807 	 *     (sp->unsync = true)
2808 	 *
2809 	 * The write barrier below ensures that 1.1 happens before 1.2 and thus
2810 	 * the situation in 2.4 does not arise.  It pairs with the read barrier
2811 	 * in is_unsync_root(), placed between 2.1's load of SPTE.W and 2.3.
2812 	 */
2813 	smp_wmb();
2814 
2815 	return 0;
2816 }
2817 
2818 static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot,
2819 			u64 *sptep, unsigned int pte_access, gfn_t gfn,
2820 			kvm_pfn_t pfn, struct kvm_page_fault *fault)
2821 {
2822 	struct kvm_mmu_page *sp = sptep_to_sp(sptep);
2823 	int level = sp->role.level;
2824 	int was_rmapped = 0;
2825 	int ret = RET_PF_FIXED;
2826 	bool flush = false;
2827 	bool wrprot;
2828 	u64 spte;
2829 
2830 	/* Prefetching always gets a writable pfn.  */
2831 	bool host_writable = !fault || fault->map_writable;
2832 	bool prefetch = !fault || fault->prefetch;
2833 	bool write_fault = fault && fault->write;
2834 
2835 	if (unlikely(is_noslot_pfn(pfn))) {
2836 		vcpu->stat.pf_mmio_spte_created++;
2837 		mark_mmio_spte(vcpu, sptep, gfn, pte_access);
2838 		return RET_PF_EMULATE;
2839 	}
2840 
2841 	if (is_shadow_present_pte(*sptep)) {
2842 		if (prefetch)
2843 			return RET_PF_SPURIOUS;
2844 
2845 		/*
2846 		 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2847 		 * the parent of the now unreachable PTE.
2848 		 */
2849 		if (level > PG_LEVEL_4K && !is_large_pte(*sptep)) {
2850 			struct kvm_mmu_page *child;
2851 			u64 pte = *sptep;
2852 
2853 			child = spte_to_child_sp(pte);
2854 			drop_parent_pte(vcpu->kvm, child, sptep);
2855 			flush = true;
2856 		} else if (pfn != spte_to_pfn(*sptep)) {
2857 			drop_spte(vcpu->kvm, sptep);
2858 			flush = true;
2859 		} else
2860 			was_rmapped = 1;
2861 	}
2862 
2863 	wrprot = make_spte(vcpu, sp, slot, pte_access, gfn, pfn, *sptep, prefetch,
2864 			   false, host_writable, &spte);
2865 
2866 	if (*sptep == spte) {
2867 		ret = RET_PF_SPURIOUS;
2868 	} else {
2869 		flush |= mmu_spte_update(sptep, spte);
2870 		trace_kvm_mmu_set_spte(level, gfn, sptep);
2871 	}
2872 
2873 	if (wrprot && write_fault)
2874 		ret = RET_PF_WRITE_PROTECTED;
2875 
2876 	if (flush)
2877 		kvm_flush_remote_tlbs_gfn(vcpu->kvm, gfn, level);
2878 
2879 	if (!was_rmapped) {
2880 		WARN_ON_ONCE(ret == RET_PF_SPURIOUS);
2881 		rmap_add(vcpu, slot, sptep, gfn, pte_access);
2882 	} else {
2883 		/* Already rmapped but the pte_access bits may have changed. */
2884 		kvm_mmu_page_set_access(sp, spte_index(sptep), pte_access);
2885 	}
2886 
2887 	return ret;
2888 }
2889 
2890 static bool kvm_mmu_prefetch_sptes(struct kvm_vcpu *vcpu, gfn_t gfn, u64 *sptep,
2891 				   int nr_pages, unsigned int access)
2892 {
2893 	struct page *pages[PTE_PREFETCH_NUM];
2894 	struct kvm_memory_slot *slot;
2895 	int i;
2896 
2897 	if (WARN_ON_ONCE(nr_pages > PTE_PREFETCH_NUM))
2898 		return false;
2899 
2900 	slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK);
2901 	if (!slot)
2902 		return false;
2903 
2904 	nr_pages = kvm_prefetch_pages(slot, gfn, pages, nr_pages);
2905 	if (nr_pages <= 0)
2906 		return false;
2907 
2908 	for (i = 0; i < nr_pages; i++, gfn++, sptep++) {
2909 		mmu_set_spte(vcpu, slot, sptep, access, gfn,
2910 			     page_to_pfn(pages[i]), NULL);
2911 
2912 		/*
2913 		 * KVM always prefetches writable pages from the primary MMU,
2914 		 * and KVM can make its SPTE writable in the fast page handler,
2915 		 * without notifying the primary MMU.  Mark pages/folios dirty
2916 		 * now to ensure file data is written back if it ends up being
2917 		 * written by the guest.  Because KVM's prefetching GUPs
2918 		 * writable PTEs, the probability of unnecessary writeback is
2919 		 * extremely low.
2920 		 */
2921 		kvm_release_page_dirty(pages[i]);
2922 	}
2923 
2924 	return true;
2925 }
2926 
2927 static bool direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2928 				     struct kvm_mmu_page *sp,
2929 				     u64 *start, u64 *end)
2930 {
2931 	gfn_t gfn = kvm_mmu_page_get_gfn(sp, spte_index(start));
2932 	unsigned int access = sp->role.access;
2933 
2934 	return kvm_mmu_prefetch_sptes(vcpu, gfn, start, end - start, access);
2935 }
2936 
2937 static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2938 				  struct kvm_mmu_page *sp, u64 *sptep)
2939 {
2940 	u64 *spte, *start = NULL;
2941 	int i;
2942 
2943 	WARN_ON_ONCE(!sp->role.direct);
2944 
2945 	i = spte_index(sptep) & ~(PTE_PREFETCH_NUM - 1);
2946 	spte = sp->spt + i;
2947 
2948 	for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2949 		if (is_shadow_present_pte(*spte) || spte == sptep) {
2950 			if (!start)
2951 				continue;
2952 			if (!direct_pte_prefetch_many(vcpu, sp, start, spte))
2953 				return;
2954 
2955 			start = NULL;
2956 		} else if (!start)
2957 			start = spte;
2958 	}
2959 	if (start)
2960 		direct_pte_prefetch_many(vcpu, sp, start, spte);
2961 }
2962 
2963 static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2964 {
2965 	struct kvm_mmu_page *sp;
2966 
2967 	sp = sptep_to_sp(sptep);
2968 
2969 	/*
2970 	 * Without accessed bits, there's no way to distinguish between
2971 	 * actually accessed translations and prefetched, so disable pte
2972 	 * prefetch if accessed bits aren't available.
2973 	 */
2974 	if (sp_ad_disabled(sp))
2975 		return;
2976 
2977 	if (sp->role.level > PG_LEVEL_4K)
2978 		return;
2979 
2980 	/*
2981 	 * If addresses are being invalidated, skip prefetching to avoid
2982 	 * accidentally prefetching those addresses.
2983 	 */
2984 	if (unlikely(vcpu->kvm->mmu_invalidate_in_progress))
2985 		return;
2986 
2987 	__direct_pte_prefetch(vcpu, sp, sptep);
2988 }
2989 
2990 /*
2991  * Lookup the mapping level for @gfn in the current mm.
2992  *
2993  * WARNING!  Use of host_pfn_mapping_level() requires the caller and the end
2994  * consumer to be tied into KVM's handlers for MMU notifier events!
2995  *
2996  * There are several ways to safely use this helper:
2997  *
2998  * - Check mmu_invalidate_retry_gfn() after grabbing the mapping level, before
2999  *   consuming it.  In this case, mmu_lock doesn't need to be held during the
3000  *   lookup, but it does need to be held while checking the MMU notifier.
3001  *
3002  * - Hold mmu_lock AND ensure there is no in-progress MMU notifier invalidation
3003  *   event for the hva.  This can be done by explicit checking the MMU notifier
3004  *   or by ensuring that KVM already has a valid mapping that covers the hva.
3005  *
3006  * - Do not use the result to install new mappings, e.g. use the host mapping
3007  *   level only to decide whether or not to zap an entry.  In this case, it's
3008  *   not required to hold mmu_lock (though it's highly likely the caller will
3009  *   want to hold mmu_lock anyways, e.g. to modify SPTEs).
3010  *
3011  * Note!  The lookup can still race with modifications to host page tables, but
3012  * the above "rules" ensure KVM will not _consume_ the result of the walk if a
3013  * race with the primary MMU occurs.
3014  */
3015 static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
3016 				  const struct kvm_memory_slot *slot)
3017 {
3018 	int level = PG_LEVEL_4K;
3019 	unsigned long hva;
3020 	unsigned long flags;
3021 	pgd_t pgd;
3022 	p4d_t p4d;
3023 	pud_t pud;
3024 	pmd_t pmd;
3025 
3026 	/*
3027 	 * Note, using the already-retrieved memslot and __gfn_to_hva_memslot()
3028 	 * is not solely for performance, it's also necessary to avoid the
3029 	 * "writable" check in __gfn_to_hva_many(), which will always fail on
3030 	 * read-only memslots due to gfn_to_hva() assuming writes.  Earlier
3031 	 * page fault steps have already verified the guest isn't writing a
3032 	 * read-only memslot.
3033 	 */
3034 	hva = __gfn_to_hva_memslot(slot, gfn);
3035 
3036 	/*
3037 	 * Disable IRQs to prevent concurrent tear down of host page tables,
3038 	 * e.g. if the primary MMU promotes a P*D to a huge page and then frees
3039 	 * the original page table.
3040 	 */
3041 	local_irq_save(flags);
3042 
3043 	/*
3044 	 * Read each entry once.  As above, a non-leaf entry can be promoted to
3045 	 * a huge page _during_ this walk.  Re-reading the entry could send the
3046 	 * walk into the weeks, e.g. p*d_leaf() returns false (sees the old
3047 	 * value) and then p*d_offset() walks into the target huge page instead
3048 	 * of the old page table (sees the new value).
3049 	 */
3050 	pgd = READ_ONCE(*pgd_offset(kvm->mm, hva));
3051 	if (pgd_none(pgd))
3052 		goto out;
3053 
3054 	p4d = READ_ONCE(*p4d_offset(&pgd, hva));
3055 	if (p4d_none(p4d) || !p4d_present(p4d))
3056 		goto out;
3057 
3058 	pud = READ_ONCE(*pud_offset(&p4d, hva));
3059 	if (pud_none(pud) || !pud_present(pud))
3060 		goto out;
3061 
3062 	if (pud_leaf(pud)) {
3063 		level = PG_LEVEL_1G;
3064 		goto out;
3065 	}
3066 
3067 	pmd = READ_ONCE(*pmd_offset(&pud, hva));
3068 	if (pmd_none(pmd) || !pmd_present(pmd))
3069 		goto out;
3070 
3071 	if (pmd_leaf(pmd))
3072 		level = PG_LEVEL_2M;
3073 
3074 out:
3075 	local_irq_restore(flags);
3076 	return level;
3077 }
3078 
3079 static int __kvm_mmu_max_mapping_level(struct kvm *kvm,
3080 				       const struct kvm_memory_slot *slot,
3081 				       gfn_t gfn, int max_level, bool is_private)
3082 {
3083 	struct kvm_lpage_info *linfo;
3084 	int host_level;
3085 
3086 	max_level = min(max_level, max_huge_page_level);
3087 	for ( ; max_level > PG_LEVEL_4K; max_level--) {
3088 		linfo = lpage_info_slot(gfn, slot, max_level);
3089 		if (!linfo->disallow_lpage)
3090 			break;
3091 	}
3092 
3093 	if (is_private)
3094 		return max_level;
3095 
3096 	if (max_level == PG_LEVEL_4K)
3097 		return PG_LEVEL_4K;
3098 
3099 	host_level = host_pfn_mapping_level(kvm, gfn, slot);
3100 	return min(host_level, max_level);
3101 }
3102 
3103 int kvm_mmu_max_mapping_level(struct kvm *kvm,
3104 			      const struct kvm_memory_slot *slot, gfn_t gfn)
3105 {
3106 	bool is_private = kvm_slot_can_be_private(slot) &&
3107 			  kvm_mem_is_private(kvm, gfn);
3108 
3109 	return __kvm_mmu_max_mapping_level(kvm, slot, gfn, PG_LEVEL_NUM, is_private);
3110 }
3111 
3112 void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
3113 {
3114 	struct kvm_memory_slot *slot = fault->slot;
3115 	kvm_pfn_t mask;
3116 
3117 	fault->huge_page_disallowed = fault->exec && fault->nx_huge_page_workaround_enabled;
3118 
3119 	if (unlikely(fault->max_level == PG_LEVEL_4K))
3120 		return;
3121 
3122 	if (is_error_noslot_pfn(fault->pfn))
3123 		return;
3124 
3125 	if (kvm_slot_dirty_track_enabled(slot))
3126 		return;
3127 
3128 	/*
3129 	 * Enforce the iTLB multihit workaround after capturing the requested
3130 	 * level, which will be used to do precise, accurate accounting.
3131 	 */
3132 	fault->req_level = __kvm_mmu_max_mapping_level(vcpu->kvm, slot,
3133 						       fault->gfn, fault->max_level,
3134 						       fault->is_private);
3135 	if (fault->req_level == PG_LEVEL_4K || fault->huge_page_disallowed)
3136 		return;
3137 
3138 	/*
3139 	 * mmu_invalidate_retry() was successful and mmu_lock is held, so
3140 	 * the pmd can't be split from under us.
3141 	 */
3142 	fault->goal_level = fault->req_level;
3143 	mask = KVM_PAGES_PER_HPAGE(fault->goal_level) - 1;
3144 	VM_BUG_ON((fault->gfn & mask) != (fault->pfn & mask));
3145 	fault->pfn &= ~mask;
3146 }
3147 
3148 void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_level)
3149 {
3150 	if (cur_level > PG_LEVEL_4K &&
3151 	    cur_level == fault->goal_level &&
3152 	    is_shadow_present_pte(spte) &&
3153 	    !is_large_pte(spte) &&
3154 	    spte_to_child_sp(spte)->nx_huge_page_disallowed) {
3155 		/*
3156 		 * A small SPTE exists for this pfn, but FNAME(fetch),
3157 		 * direct_map(), or kvm_tdp_mmu_map() would like to create a
3158 		 * large PTE instead: just force them to go down another level,
3159 		 * patching back for them into pfn the next 9 bits of the
3160 		 * address.
3161 		 */
3162 		u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
3163 				KVM_PAGES_PER_HPAGE(cur_level - 1);
3164 		fault->pfn |= fault->gfn & page_mask;
3165 		fault->goal_level--;
3166 	}
3167 }
3168 
3169 static int direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
3170 {
3171 	struct kvm_shadow_walk_iterator it;
3172 	struct kvm_mmu_page *sp;
3173 	int ret;
3174 	gfn_t base_gfn = fault->gfn;
3175 
3176 	kvm_mmu_hugepage_adjust(vcpu, fault);
3177 
3178 	trace_kvm_mmu_spte_requested(fault);
3179 	for_each_shadow_entry(vcpu, fault->addr, it) {
3180 		/*
3181 		 * We cannot overwrite existing page tables with an NX
3182 		 * large page, as the leaf could be executable.
3183 		 */
3184 		if (fault->nx_huge_page_workaround_enabled)
3185 			disallowed_hugepage_adjust(fault, *it.sptep, it.level);
3186 
3187 		base_gfn = gfn_round_for_level(fault->gfn, it.level);
3188 		if (it.level == fault->goal_level)
3189 			break;
3190 
3191 		sp = kvm_mmu_get_child_sp(vcpu, it.sptep, base_gfn, true, ACC_ALL);
3192 		if (sp == ERR_PTR(-EEXIST))
3193 			continue;
3194 
3195 		link_shadow_page(vcpu, it.sptep, sp);
3196 		if (fault->huge_page_disallowed)
3197 			account_nx_huge_page(vcpu->kvm, sp,
3198 					     fault->req_level >= it.level);
3199 	}
3200 
3201 	if (WARN_ON_ONCE(it.level != fault->goal_level))
3202 		return -EFAULT;
3203 
3204 	ret = mmu_set_spte(vcpu, fault->slot, it.sptep, ACC_ALL,
3205 			   base_gfn, fault->pfn, fault);
3206 	if (ret == RET_PF_SPURIOUS)
3207 		return ret;
3208 
3209 	direct_pte_prefetch(vcpu, it.sptep);
3210 	return ret;
3211 }
3212 
3213 static void kvm_send_hwpoison_signal(struct kvm_memory_slot *slot, gfn_t gfn)
3214 {
3215 	unsigned long hva = gfn_to_hva_memslot(slot, gfn);
3216 
3217 	send_sig_mceerr(BUS_MCEERR_AR, (void __user *)hva, PAGE_SHIFT, current);
3218 }
3219 
3220 static int kvm_handle_error_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
3221 {
3222 	if (is_sigpending_pfn(fault->pfn)) {
3223 		kvm_handle_signal_exit(vcpu);
3224 		return -EINTR;
3225 	}
3226 
3227 	/*
3228 	 * Do not cache the mmio info caused by writing the readonly gfn
3229 	 * into the spte otherwise read access on readonly gfn also can
3230 	 * caused mmio page fault and treat it as mmio access.
3231 	 */
3232 	if (fault->pfn == KVM_PFN_ERR_RO_FAULT)
3233 		return RET_PF_EMULATE;
3234 
3235 	if (fault->pfn == KVM_PFN_ERR_HWPOISON) {
3236 		kvm_send_hwpoison_signal(fault->slot, fault->gfn);
3237 		return RET_PF_RETRY;
3238 	}
3239 
3240 	return -EFAULT;
3241 }
3242 
3243 static int kvm_handle_noslot_fault(struct kvm_vcpu *vcpu,
3244 				   struct kvm_page_fault *fault,
3245 				   unsigned int access)
3246 {
3247 	gva_t gva = fault->is_tdp ? 0 : fault->addr;
3248 
3249 	if (fault->is_private) {
3250 		kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
3251 		return -EFAULT;
3252 	}
3253 
3254 	vcpu_cache_mmio_info(vcpu, gva, fault->gfn,
3255 			     access & shadow_mmio_access_mask);
3256 
3257 	fault->slot = NULL;
3258 	fault->pfn = KVM_PFN_NOSLOT;
3259 	fault->map_writable = false;
3260 
3261 	/*
3262 	 * If MMIO caching is disabled, emulate immediately without
3263 	 * touching the shadow page tables as attempting to install an
3264 	 * MMIO SPTE will just be an expensive nop.
3265 	 */
3266 	if (unlikely(!enable_mmio_caching))
3267 		return RET_PF_EMULATE;
3268 
3269 	/*
3270 	 * Do not create an MMIO SPTE for a gfn greater than host.MAXPHYADDR,
3271 	 * any guest that generates such gfns is running nested and is being
3272 	 * tricked by L0 userspace (you can observe gfn > L1.MAXPHYADDR if and
3273 	 * only if L1's MAXPHYADDR is inaccurate with respect to the
3274 	 * hardware's).
3275 	 */
3276 	if (unlikely(fault->gfn > kvm_mmu_max_gfn()))
3277 		return RET_PF_EMULATE;
3278 
3279 	return RET_PF_CONTINUE;
3280 }
3281 
3282 static bool page_fault_can_be_fast(struct kvm *kvm, struct kvm_page_fault *fault)
3283 {
3284 	/*
3285 	 * Page faults with reserved bits set, i.e. faults on MMIO SPTEs, only
3286 	 * reach the common page fault handler if the SPTE has an invalid MMIO
3287 	 * generation number.  Refreshing the MMIO generation needs to go down
3288 	 * the slow path.  Note, EPT Misconfigs do NOT set the PRESENT flag!
3289 	 */
3290 	if (fault->rsvd)
3291 		return false;
3292 
3293 	/*
3294 	 * For hardware-protected VMs, certain conditions like attempting to
3295 	 * perform a write to a page which is not in the state that the guest
3296 	 * expects it to be in can result in a nested/extended #PF. In this
3297 	 * case, the below code might misconstrue this situation as being the
3298 	 * result of a write-protected access, and treat it as a spurious case
3299 	 * rather than taking any action to satisfy the real source of the #PF
3300 	 * such as generating a KVM_EXIT_MEMORY_FAULT. This can lead to the
3301 	 * guest spinning on a #PF indefinitely, so don't attempt the fast path
3302 	 * in this case.
3303 	 *
3304 	 * Note that the kvm_mem_is_private() check might race with an
3305 	 * attribute update, but this will either result in the guest spinning
3306 	 * on RET_PF_SPURIOUS until the update completes, or an actual spurious
3307 	 * case might go down the slow path. Either case will resolve itself.
3308 	 */
3309 	if (kvm->arch.has_private_mem &&
3310 	    fault->is_private != kvm_mem_is_private(kvm, fault->gfn))
3311 		return false;
3312 
3313 	/*
3314 	 * #PF can be fast if:
3315 	 *
3316 	 * 1. The shadow page table entry is not present and A/D bits are
3317 	 *    disabled _by KVM_, which could mean that the fault is potentially
3318 	 *    caused by access tracking (if enabled).  If A/D bits are enabled
3319 	 *    by KVM, but disabled by L1 for L2, KVM is forced to disable A/D
3320 	 *    bits for L2 and employ access tracking, but the fast page fault
3321 	 *    mechanism only supports direct MMUs.
3322 	 * 2. The shadow page table entry is present, the access is a write,
3323 	 *    and no reserved bits are set (MMIO SPTEs cannot be "fixed"), i.e.
3324 	 *    the fault was caused by a write-protection violation.  If the
3325 	 *    SPTE is MMU-writable (determined later), the fault can be fixed
3326 	 *    by setting the Writable bit, which can be done out of mmu_lock.
3327 	 */
3328 	if (!fault->present)
3329 		return !kvm_ad_enabled;
3330 
3331 	/*
3332 	 * Note, instruction fetches and writes are mutually exclusive, ignore
3333 	 * the "exec" flag.
3334 	 */
3335 	return fault->write;
3336 }
3337 
3338 /*
3339  * Returns true if the SPTE was fixed successfully. Otherwise,
3340  * someone else modified the SPTE from its original value.
3341  */
3342 static bool fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu,
3343 				    struct kvm_page_fault *fault,
3344 				    u64 *sptep, u64 old_spte, u64 new_spte)
3345 {
3346 	/*
3347 	 * Theoretically we could also set dirty bit (and flush TLB) here in
3348 	 * order to eliminate unnecessary PML logging. See comments in
3349 	 * set_spte. But fast_page_fault is very unlikely to happen with PML
3350 	 * enabled, so we do not do this. This might result in the same GPA
3351 	 * to be logged in PML buffer again when the write really happens, and
3352 	 * eventually to be called by mark_page_dirty twice. But it's also no
3353 	 * harm. This also avoids the TLB flush needed after setting dirty bit
3354 	 * so non-PML cases won't be impacted.
3355 	 *
3356 	 * Compare with make_spte() where instead shadow_dirty_mask is set.
3357 	 */
3358 	if (!try_cmpxchg64(sptep, &old_spte, new_spte))
3359 		return false;
3360 
3361 	if (is_writable_pte(new_spte) && !is_writable_pte(old_spte))
3362 		mark_page_dirty_in_slot(vcpu->kvm, fault->slot, fault->gfn);
3363 
3364 	return true;
3365 }
3366 
3367 static bool is_access_allowed(struct kvm_page_fault *fault, u64 spte)
3368 {
3369 	if (fault->exec)
3370 		return is_executable_pte(spte);
3371 
3372 	if (fault->write)
3373 		return is_writable_pte(spte);
3374 
3375 	/* Fault was on Read access */
3376 	return spte & PT_PRESENT_MASK;
3377 }
3378 
3379 /*
3380  * Returns the last level spte pointer of the shadow page walk for the given
3381  * gpa, and sets *spte to the spte value. This spte may be non-preset. If no
3382  * walk could be performed, returns NULL and *spte does not contain valid data.
3383  *
3384  * Contract:
3385  *  - Must be called between walk_shadow_page_lockless_{begin,end}.
3386  *  - The returned sptep must not be used after walk_shadow_page_lockless_end.
3387  */
3388 static u64 *fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, gpa_t gpa, u64 *spte)
3389 {
3390 	struct kvm_shadow_walk_iterator iterator;
3391 	u64 old_spte;
3392 	u64 *sptep = NULL;
3393 
3394 	for_each_shadow_entry_lockless(vcpu, gpa, iterator, old_spte) {
3395 		sptep = iterator.sptep;
3396 		*spte = old_spte;
3397 	}
3398 
3399 	return sptep;
3400 }
3401 
3402 /*
3403  * Returns one of RET_PF_INVALID, RET_PF_FIXED or RET_PF_SPURIOUS.
3404  */
3405 static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
3406 {
3407 	struct kvm_mmu_page *sp;
3408 	int ret = RET_PF_INVALID;
3409 	u64 spte;
3410 	u64 *sptep;
3411 	uint retry_count = 0;
3412 
3413 	if (!page_fault_can_be_fast(vcpu->kvm, fault))
3414 		return ret;
3415 
3416 	walk_shadow_page_lockless_begin(vcpu);
3417 
3418 	do {
3419 		u64 new_spte;
3420 
3421 		if (tdp_mmu_enabled)
3422 			sptep = kvm_tdp_mmu_fast_pf_get_last_sptep(vcpu, fault->gfn, &spte);
3423 		else
3424 			sptep = fast_pf_get_last_sptep(vcpu, fault->addr, &spte);
3425 
3426 		/*
3427 		 * It's entirely possible for the mapping to have been zapped
3428 		 * by a different task, but the root page should always be
3429 		 * available as the vCPU holds a reference to its root(s).
3430 		 */
3431 		if (WARN_ON_ONCE(!sptep))
3432 			spte = FROZEN_SPTE;
3433 
3434 		if (!is_shadow_present_pte(spte))
3435 			break;
3436 
3437 		sp = sptep_to_sp(sptep);
3438 		if (!is_last_spte(spte, sp->role.level))
3439 			break;
3440 
3441 		/*
3442 		 * Check whether the memory access that caused the fault would
3443 		 * still cause it if it were to be performed right now. If not,
3444 		 * then this is a spurious fault caused by TLB lazily flushed,
3445 		 * or some other CPU has already fixed the PTE after the
3446 		 * current CPU took the fault.
3447 		 *
3448 		 * Need not check the access of upper level table entries since
3449 		 * they are always ACC_ALL.
3450 		 */
3451 		if (is_access_allowed(fault, spte)) {
3452 			ret = RET_PF_SPURIOUS;
3453 			break;
3454 		}
3455 
3456 		new_spte = spte;
3457 
3458 		/*
3459 		 * KVM only supports fixing page faults outside of MMU lock for
3460 		 * direct MMUs, nested MMUs are always indirect, and KVM always
3461 		 * uses A/D bits for non-nested MMUs.  Thus, if A/D bits are
3462 		 * enabled, the SPTE can't be an access-tracked SPTE.
3463 		 */
3464 		if (unlikely(!kvm_ad_enabled) && is_access_track_spte(spte))
3465 			new_spte = restore_acc_track_spte(new_spte) |
3466 				   shadow_accessed_mask;
3467 
3468 		/*
3469 		 * To keep things simple, only SPTEs that are MMU-writable can
3470 		 * be made fully writable outside of mmu_lock, e.g. only SPTEs
3471 		 * that were write-protected for dirty-logging or access
3472 		 * tracking are handled here.  Don't bother checking if the
3473 		 * SPTE is writable to prioritize running with A/D bits enabled.
3474 		 * The is_access_allowed() check above handles the common case
3475 		 * of the fault being spurious, and the SPTE is known to be
3476 		 * shadow-present, i.e. except for access tracking restoration
3477 		 * making the new SPTE writable, the check is wasteful.
3478 		 */
3479 		if (fault->write && is_mmu_writable_spte(spte)) {
3480 			new_spte |= PT_WRITABLE_MASK;
3481 
3482 			/*
3483 			 * Do not fix write-permission on the large spte when
3484 			 * dirty logging is enabled. Since we only dirty the
3485 			 * first page into the dirty-bitmap in
3486 			 * fast_pf_fix_direct_spte(), other pages are missed
3487 			 * if its slot has dirty logging enabled.
3488 			 *
3489 			 * Instead, we let the slow page fault path create a
3490 			 * normal spte to fix the access.
3491 			 */
3492 			if (sp->role.level > PG_LEVEL_4K &&
3493 			    kvm_slot_dirty_track_enabled(fault->slot))
3494 				break;
3495 		}
3496 
3497 		/* Verify that the fault can be handled in the fast path */
3498 		if (new_spte == spte ||
3499 		    !is_access_allowed(fault, new_spte))
3500 			break;
3501 
3502 		/*
3503 		 * Currently, fast page fault only works for direct mapping
3504 		 * since the gfn is not stable for indirect shadow page. See
3505 		 * Documentation/virt/kvm/locking.rst to get more detail.
3506 		 */
3507 		if (fast_pf_fix_direct_spte(vcpu, fault, sptep, spte, new_spte)) {
3508 			ret = RET_PF_FIXED;
3509 			break;
3510 		}
3511 
3512 		if (++retry_count > 4) {
3513 			pr_warn_once("Fast #PF retrying more than 4 times.\n");
3514 			break;
3515 		}
3516 
3517 	} while (true);
3518 
3519 	trace_fast_page_fault(vcpu, fault, sptep, spte, ret);
3520 	walk_shadow_page_lockless_end(vcpu);
3521 
3522 	if (ret != RET_PF_INVALID)
3523 		vcpu->stat.pf_fast++;
3524 
3525 	return ret;
3526 }
3527 
3528 static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa,
3529 			       struct list_head *invalid_list)
3530 {
3531 	struct kvm_mmu_page *sp;
3532 
3533 	if (!VALID_PAGE(*root_hpa))
3534 		return;
3535 
3536 	sp = root_to_sp(*root_hpa);
3537 	if (WARN_ON_ONCE(!sp))
3538 		return;
3539 
3540 	if (is_tdp_mmu_page(sp)) {
3541 		lockdep_assert_held_read(&kvm->mmu_lock);
3542 		kvm_tdp_mmu_put_root(kvm, sp);
3543 	} else {
3544 		lockdep_assert_held_write(&kvm->mmu_lock);
3545 		if (!--sp->root_count && sp->role.invalid)
3546 			kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
3547 	}
3548 
3549 	*root_hpa = INVALID_PAGE;
3550 }
3551 
3552 /* roots_to_free must be some combination of the KVM_MMU_ROOT_* flags */
3553 void kvm_mmu_free_roots(struct kvm *kvm, struct kvm_mmu *mmu,
3554 			ulong roots_to_free)
3555 {
3556 	bool is_tdp_mmu = tdp_mmu_enabled && mmu->root_role.direct;
3557 	int i;
3558 	LIST_HEAD(invalid_list);
3559 	bool free_active_root;
3560 
3561 	WARN_ON_ONCE(roots_to_free & ~KVM_MMU_ROOTS_ALL);
3562 
3563 	BUILD_BUG_ON(KVM_MMU_NUM_PREV_ROOTS >= BITS_PER_LONG);
3564 
3565 	/* Before acquiring the MMU lock, see if we need to do any real work. */
3566 	free_active_root = (roots_to_free & KVM_MMU_ROOT_CURRENT)
3567 		&& VALID_PAGE(mmu->root.hpa);
3568 
3569 	if (!free_active_root) {
3570 		for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
3571 			if ((roots_to_free & KVM_MMU_ROOT_PREVIOUS(i)) &&
3572 			    VALID_PAGE(mmu->prev_roots[i].hpa))
3573 				break;
3574 
3575 		if (i == KVM_MMU_NUM_PREV_ROOTS)
3576 			return;
3577 	}
3578 
3579 	if (is_tdp_mmu)
3580 		read_lock(&kvm->mmu_lock);
3581 	else
3582 		write_lock(&kvm->mmu_lock);
3583 
3584 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
3585 		if (roots_to_free & KVM_MMU_ROOT_PREVIOUS(i))
3586 			mmu_free_root_page(kvm, &mmu->prev_roots[i].hpa,
3587 					   &invalid_list);
3588 
3589 	if (free_active_root) {
3590 		if (kvm_mmu_is_dummy_root(mmu->root.hpa)) {
3591 			/* Nothing to cleanup for dummy roots. */
3592 		} else if (root_to_sp(mmu->root.hpa)) {
3593 			mmu_free_root_page(kvm, &mmu->root.hpa, &invalid_list);
3594 		} else if (mmu->pae_root) {
3595 			for (i = 0; i < 4; ++i) {
3596 				if (!IS_VALID_PAE_ROOT(mmu->pae_root[i]))
3597 					continue;
3598 
3599 				mmu_free_root_page(kvm, &mmu->pae_root[i],
3600 						   &invalid_list);
3601 				mmu->pae_root[i] = INVALID_PAE_ROOT;
3602 			}
3603 		}
3604 		mmu->root.hpa = INVALID_PAGE;
3605 		mmu->root.pgd = 0;
3606 	}
3607 
3608 	if (is_tdp_mmu) {
3609 		read_unlock(&kvm->mmu_lock);
3610 		WARN_ON_ONCE(!list_empty(&invalid_list));
3611 	} else {
3612 		kvm_mmu_commit_zap_page(kvm, &invalid_list);
3613 		write_unlock(&kvm->mmu_lock);
3614 	}
3615 }
3616 EXPORT_SYMBOL_GPL(kvm_mmu_free_roots);
3617 
3618 void kvm_mmu_free_guest_mode_roots(struct kvm *kvm, struct kvm_mmu *mmu)
3619 {
3620 	unsigned long roots_to_free = 0;
3621 	struct kvm_mmu_page *sp;
3622 	hpa_t root_hpa;
3623 	int i;
3624 
3625 	/*
3626 	 * This should not be called while L2 is active, L2 can't invalidate
3627 	 * _only_ its own roots, e.g. INVVPID unconditionally exits.
3628 	 */
3629 	WARN_ON_ONCE(mmu->root_role.guest_mode);
3630 
3631 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
3632 		root_hpa = mmu->prev_roots[i].hpa;
3633 		if (!VALID_PAGE(root_hpa))
3634 			continue;
3635 
3636 		sp = root_to_sp(root_hpa);
3637 		if (!sp || sp->role.guest_mode)
3638 			roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
3639 	}
3640 
3641 	kvm_mmu_free_roots(kvm, mmu, roots_to_free);
3642 }
3643 EXPORT_SYMBOL_GPL(kvm_mmu_free_guest_mode_roots);
3644 
3645 static hpa_t mmu_alloc_root(struct kvm_vcpu *vcpu, gfn_t gfn, int quadrant,
3646 			    u8 level)
3647 {
3648 	union kvm_mmu_page_role role = vcpu->arch.mmu->root_role;
3649 	struct kvm_mmu_page *sp;
3650 
3651 	role.level = level;
3652 	role.quadrant = quadrant;
3653 
3654 	WARN_ON_ONCE(quadrant && !role.has_4_byte_gpte);
3655 	WARN_ON_ONCE(role.direct && role.has_4_byte_gpte);
3656 
3657 	sp = kvm_mmu_get_shadow_page(vcpu, gfn, role);
3658 	++sp->root_count;
3659 
3660 	return __pa(sp->spt);
3661 }
3662 
3663 static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
3664 {
3665 	struct kvm_mmu *mmu = vcpu->arch.mmu;
3666 	u8 shadow_root_level = mmu->root_role.level;
3667 	hpa_t root;
3668 	unsigned i;
3669 	int r;
3670 
3671 	if (tdp_mmu_enabled)
3672 		return kvm_tdp_mmu_alloc_root(vcpu);
3673 
3674 	write_lock(&vcpu->kvm->mmu_lock);
3675 	r = make_mmu_pages_available(vcpu);
3676 	if (r < 0)
3677 		goto out_unlock;
3678 
3679 	if (shadow_root_level >= PT64_ROOT_4LEVEL) {
3680 		root = mmu_alloc_root(vcpu, 0, 0, shadow_root_level);
3681 		mmu->root.hpa = root;
3682 	} else if (shadow_root_level == PT32E_ROOT_LEVEL) {
3683 		if (WARN_ON_ONCE(!mmu->pae_root)) {
3684 			r = -EIO;
3685 			goto out_unlock;
3686 		}
3687 
3688 		for (i = 0; i < 4; ++i) {
3689 			WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i]));
3690 
3691 			root = mmu_alloc_root(vcpu, i << (30 - PAGE_SHIFT), 0,
3692 					      PT32_ROOT_LEVEL);
3693 			mmu->pae_root[i] = root | PT_PRESENT_MASK |
3694 					   shadow_me_value;
3695 		}
3696 		mmu->root.hpa = __pa(mmu->pae_root);
3697 	} else {
3698 		WARN_ONCE(1, "Bad TDP root level = %d\n", shadow_root_level);
3699 		r = -EIO;
3700 		goto out_unlock;
3701 	}
3702 
3703 	/* root.pgd is ignored for direct MMUs. */
3704 	mmu->root.pgd = 0;
3705 out_unlock:
3706 	write_unlock(&vcpu->kvm->mmu_lock);
3707 	return r;
3708 }
3709 
3710 static int mmu_first_shadow_root_alloc(struct kvm *kvm)
3711 {
3712 	struct kvm_memslots *slots;
3713 	struct kvm_memory_slot *slot;
3714 	int r = 0, i, bkt;
3715 
3716 	/*
3717 	 * Check if this is the first shadow root being allocated before
3718 	 * taking the lock.
3719 	 */
3720 	if (kvm_shadow_root_allocated(kvm))
3721 		return 0;
3722 
3723 	mutex_lock(&kvm->slots_arch_lock);
3724 
3725 	/* Recheck, under the lock, whether this is the first shadow root. */
3726 	if (kvm_shadow_root_allocated(kvm))
3727 		goto out_unlock;
3728 
3729 	/*
3730 	 * Check if anything actually needs to be allocated, e.g. all metadata
3731 	 * will be allocated upfront if TDP is disabled.
3732 	 */
3733 	if (kvm_memslots_have_rmaps(kvm) &&
3734 	    kvm_page_track_write_tracking_enabled(kvm))
3735 		goto out_success;
3736 
3737 	for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
3738 		slots = __kvm_memslots(kvm, i);
3739 		kvm_for_each_memslot(slot, bkt, slots) {
3740 			/*
3741 			 * Both of these functions are no-ops if the target is
3742 			 * already allocated, so unconditionally calling both
3743 			 * is safe.  Intentionally do NOT free allocations on
3744 			 * failure to avoid having to track which allocations
3745 			 * were made now versus when the memslot was created.
3746 			 * The metadata is guaranteed to be freed when the slot
3747 			 * is freed, and will be kept/used if userspace retries
3748 			 * KVM_RUN instead of killing the VM.
3749 			 */
3750 			r = memslot_rmap_alloc(slot, slot->npages);
3751 			if (r)
3752 				goto out_unlock;
3753 			r = kvm_page_track_write_tracking_alloc(slot);
3754 			if (r)
3755 				goto out_unlock;
3756 		}
3757 	}
3758 
3759 	/*
3760 	 * Ensure that shadow_root_allocated becomes true strictly after
3761 	 * all the related pointers are set.
3762 	 */
3763 out_success:
3764 	smp_store_release(&kvm->arch.shadow_root_allocated, true);
3765 
3766 out_unlock:
3767 	mutex_unlock(&kvm->slots_arch_lock);
3768 	return r;
3769 }
3770 
3771 static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
3772 {
3773 	struct kvm_mmu *mmu = vcpu->arch.mmu;
3774 	u64 pdptrs[4], pm_mask;
3775 	gfn_t root_gfn, root_pgd;
3776 	int quadrant, i, r;
3777 	hpa_t root;
3778 
3779 	root_pgd = kvm_mmu_get_guest_pgd(vcpu, mmu);
3780 	root_gfn = (root_pgd & __PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
3781 
3782 	if (!kvm_vcpu_is_visible_gfn(vcpu, root_gfn)) {
3783 		mmu->root.hpa = kvm_mmu_get_dummy_root();
3784 		return 0;
3785 	}
3786 
3787 	/*
3788 	 * On SVM, reading PDPTRs might access guest memory, which might fault
3789 	 * and thus might sleep.  Grab the PDPTRs before acquiring mmu_lock.
3790 	 */
3791 	if (mmu->cpu_role.base.level == PT32E_ROOT_LEVEL) {
3792 		for (i = 0; i < 4; ++i) {
3793 			pdptrs[i] = mmu->get_pdptr(vcpu, i);
3794 			if (!(pdptrs[i] & PT_PRESENT_MASK))
3795 				continue;
3796 
3797 			if (!kvm_vcpu_is_visible_gfn(vcpu, pdptrs[i] >> PAGE_SHIFT))
3798 				pdptrs[i] = 0;
3799 		}
3800 	}
3801 
3802 	r = mmu_first_shadow_root_alloc(vcpu->kvm);
3803 	if (r)
3804 		return r;
3805 
3806 	write_lock(&vcpu->kvm->mmu_lock);
3807 	r = make_mmu_pages_available(vcpu);
3808 	if (r < 0)
3809 		goto out_unlock;
3810 
3811 	/*
3812 	 * Do we shadow a long mode page table? If so we need to
3813 	 * write-protect the guests page table root.
3814 	 */
3815 	if (mmu->cpu_role.base.level >= PT64_ROOT_4LEVEL) {
3816 		root = mmu_alloc_root(vcpu, root_gfn, 0,
3817 				      mmu->root_role.level);
3818 		mmu->root.hpa = root;
3819 		goto set_root_pgd;
3820 	}
3821 
3822 	if (WARN_ON_ONCE(!mmu->pae_root)) {
3823 		r = -EIO;
3824 		goto out_unlock;
3825 	}
3826 
3827 	/*
3828 	 * We shadow a 32 bit page table. This may be a legacy 2-level
3829 	 * or a PAE 3-level page table. In either case we need to be aware that
3830 	 * the shadow page table may be a PAE or a long mode page table.
3831 	 */
3832 	pm_mask = PT_PRESENT_MASK | shadow_me_value;
3833 	if (mmu->root_role.level >= PT64_ROOT_4LEVEL) {
3834 		pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
3835 
3836 		if (WARN_ON_ONCE(!mmu->pml4_root)) {
3837 			r = -EIO;
3838 			goto out_unlock;
3839 		}
3840 		mmu->pml4_root[0] = __pa(mmu->pae_root) | pm_mask;
3841 
3842 		if (mmu->root_role.level == PT64_ROOT_5LEVEL) {
3843 			if (WARN_ON_ONCE(!mmu->pml5_root)) {
3844 				r = -EIO;
3845 				goto out_unlock;
3846 			}
3847 			mmu->pml5_root[0] = __pa(mmu->pml4_root) | pm_mask;
3848 		}
3849 	}
3850 
3851 	for (i = 0; i < 4; ++i) {
3852 		WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i]));
3853 
3854 		if (mmu->cpu_role.base.level == PT32E_ROOT_LEVEL) {
3855 			if (!(pdptrs[i] & PT_PRESENT_MASK)) {
3856 				mmu->pae_root[i] = INVALID_PAE_ROOT;
3857 				continue;
3858 			}
3859 			root_gfn = pdptrs[i] >> PAGE_SHIFT;
3860 		}
3861 
3862 		/*
3863 		 * If shadowing 32-bit non-PAE page tables, each PAE page
3864 		 * directory maps one quarter of the guest's non-PAE page
3865 		 * directory. Othwerise each PAE page direct shadows one guest
3866 		 * PAE page directory so that quadrant should be 0.
3867 		 */
3868 		quadrant = (mmu->cpu_role.base.level == PT32_ROOT_LEVEL) ? i : 0;
3869 
3870 		root = mmu_alloc_root(vcpu, root_gfn, quadrant, PT32_ROOT_LEVEL);
3871 		mmu->pae_root[i] = root | pm_mask;
3872 	}
3873 
3874 	if (mmu->root_role.level == PT64_ROOT_5LEVEL)
3875 		mmu->root.hpa = __pa(mmu->pml5_root);
3876 	else if (mmu->root_role.level == PT64_ROOT_4LEVEL)
3877 		mmu->root.hpa = __pa(mmu->pml4_root);
3878 	else
3879 		mmu->root.hpa = __pa(mmu->pae_root);
3880 
3881 set_root_pgd:
3882 	mmu->root.pgd = root_pgd;
3883 out_unlock:
3884 	write_unlock(&vcpu->kvm->mmu_lock);
3885 
3886 	return r;
3887 }
3888 
3889 static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
3890 {
3891 	struct kvm_mmu *mmu = vcpu->arch.mmu;
3892 	bool need_pml5 = mmu->root_role.level > PT64_ROOT_4LEVEL;
3893 	u64 *pml5_root = NULL;
3894 	u64 *pml4_root = NULL;
3895 	u64 *pae_root;
3896 
3897 	/*
3898 	 * When shadowing 32-bit or PAE NPT with 64-bit NPT, the PML4 and PDP
3899 	 * tables are allocated and initialized at root creation as there is no
3900 	 * equivalent level in the guest's NPT to shadow.  Allocate the tables
3901 	 * on demand, as running a 32-bit L1 VMM on 64-bit KVM is very rare.
3902 	 */
3903 	if (mmu->root_role.direct ||
3904 	    mmu->cpu_role.base.level >= PT64_ROOT_4LEVEL ||
3905 	    mmu->root_role.level < PT64_ROOT_4LEVEL)
3906 		return 0;
3907 
3908 	/*
3909 	 * NPT, the only paging mode that uses this horror, uses a fixed number
3910 	 * of levels for the shadow page tables, e.g. all MMUs are 4-level or
3911 	 * all MMus are 5-level.  Thus, this can safely require that pml5_root
3912 	 * is allocated if the other roots are valid and pml5 is needed, as any
3913 	 * prior MMU would also have required pml5.
3914 	 */
3915 	if (mmu->pae_root && mmu->pml4_root && (!need_pml5 || mmu->pml5_root))
3916 		return 0;
3917 
3918 	/*
3919 	 * The special roots should always be allocated in concert.  Yell and
3920 	 * bail if KVM ends up in a state where only one of the roots is valid.
3921 	 */
3922 	if (WARN_ON_ONCE(!tdp_enabled || mmu->pae_root || mmu->pml4_root ||
3923 			 (need_pml5 && mmu->pml5_root)))
3924 		return -EIO;
3925 
3926 	/*
3927 	 * Unlike 32-bit NPT, the PDP table doesn't need to be in low mem, and
3928 	 * doesn't need to be decrypted.
3929 	 */
3930 	pae_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3931 	if (!pae_root)
3932 		return -ENOMEM;
3933 
3934 #ifdef CONFIG_X86_64
3935 	pml4_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3936 	if (!pml4_root)
3937 		goto err_pml4;
3938 
3939 	if (need_pml5) {
3940 		pml5_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3941 		if (!pml5_root)
3942 			goto err_pml5;
3943 	}
3944 #endif
3945 
3946 	mmu->pae_root = pae_root;
3947 	mmu->pml4_root = pml4_root;
3948 	mmu->pml5_root = pml5_root;
3949 
3950 	return 0;
3951 
3952 #ifdef CONFIG_X86_64
3953 err_pml5:
3954 	free_page((unsigned long)pml4_root);
3955 err_pml4:
3956 	free_page((unsigned long)pae_root);
3957 	return -ENOMEM;
3958 #endif
3959 }
3960 
3961 static bool is_unsync_root(hpa_t root)
3962 {
3963 	struct kvm_mmu_page *sp;
3964 
3965 	if (!VALID_PAGE(root) || kvm_mmu_is_dummy_root(root))
3966 		return false;
3967 
3968 	/*
3969 	 * The read barrier orders the CPU's read of SPTE.W during the page table
3970 	 * walk before the reads of sp->unsync/sp->unsync_children here.
3971 	 *
3972 	 * Even if another CPU was marking the SP as unsync-ed simultaneously,
3973 	 * any guest page table changes are not guaranteed to be visible anyway
3974 	 * until this VCPU issues a TLB flush strictly after those changes are
3975 	 * made.  We only need to ensure that the other CPU sets these flags
3976 	 * before any actual changes to the page tables are made.  The comments
3977 	 * in mmu_try_to_unsync_pages() describe what could go wrong if this
3978 	 * requirement isn't satisfied.
3979 	 */
3980 	smp_rmb();
3981 	sp = root_to_sp(root);
3982 
3983 	/*
3984 	 * PAE roots (somewhat arbitrarily) aren't backed by shadow pages, the
3985 	 * PDPTEs for a given PAE root need to be synchronized individually.
3986 	 */
3987 	if (WARN_ON_ONCE(!sp))
3988 		return false;
3989 
3990 	if (sp->unsync || sp->unsync_children)
3991 		return true;
3992 
3993 	return false;
3994 }
3995 
3996 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
3997 {
3998 	int i;
3999 	struct kvm_mmu_page *sp;
4000 
4001 	if (vcpu->arch.mmu->root_role.direct)
4002 		return;
4003 
4004 	if (!VALID_PAGE(vcpu->arch.mmu->root.hpa))
4005 		return;
4006 
4007 	vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
4008 
4009 	if (vcpu->arch.mmu->cpu_role.base.level >= PT64_ROOT_4LEVEL) {
4010 		hpa_t root = vcpu->arch.mmu->root.hpa;
4011 
4012 		if (!is_unsync_root(root))
4013 			return;
4014 
4015 		sp = root_to_sp(root);
4016 
4017 		write_lock(&vcpu->kvm->mmu_lock);
4018 		mmu_sync_children(vcpu, sp, true);
4019 		write_unlock(&vcpu->kvm->mmu_lock);
4020 		return;
4021 	}
4022 
4023 	write_lock(&vcpu->kvm->mmu_lock);
4024 
4025 	for (i = 0; i < 4; ++i) {
4026 		hpa_t root = vcpu->arch.mmu->pae_root[i];
4027 
4028 		if (IS_VALID_PAE_ROOT(root)) {
4029 			sp = spte_to_child_sp(root);
4030 			mmu_sync_children(vcpu, sp, true);
4031 		}
4032 	}
4033 
4034 	write_unlock(&vcpu->kvm->mmu_lock);
4035 }
4036 
4037 void kvm_mmu_sync_prev_roots(struct kvm_vcpu *vcpu)
4038 {
4039 	unsigned long roots_to_free = 0;
4040 	int i;
4041 
4042 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
4043 		if (is_unsync_root(vcpu->arch.mmu->prev_roots[i].hpa))
4044 			roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
4045 
4046 	/* sync prev_roots by simply freeing them */
4047 	kvm_mmu_free_roots(vcpu->kvm, vcpu->arch.mmu, roots_to_free);
4048 }
4049 
4050 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
4051 				  gpa_t vaddr, u64 access,
4052 				  struct x86_exception *exception)
4053 {
4054 	if (exception)
4055 		exception->error_code = 0;
4056 	return kvm_translate_gpa(vcpu, mmu, vaddr, access, exception);
4057 }
4058 
4059 static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
4060 {
4061 	/*
4062 	 * A nested guest cannot use the MMIO cache if it is using nested
4063 	 * page tables, because cr2 is a nGPA while the cache stores GPAs.
4064 	 */
4065 	if (mmu_is_nested(vcpu))
4066 		return false;
4067 
4068 	if (direct)
4069 		return vcpu_match_mmio_gpa(vcpu, addr);
4070 
4071 	return vcpu_match_mmio_gva(vcpu, addr);
4072 }
4073 
4074 /*
4075  * Return the level of the lowest level SPTE added to sptes.
4076  * That SPTE may be non-present.
4077  *
4078  * Must be called between walk_shadow_page_lockless_{begin,end}.
4079  */
4080 static int get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, int *root_level)
4081 {
4082 	struct kvm_shadow_walk_iterator iterator;
4083 	int leaf = -1;
4084 	u64 spte;
4085 
4086 	for (shadow_walk_init(&iterator, vcpu, addr),
4087 	     *root_level = iterator.level;
4088 	     shadow_walk_okay(&iterator);
4089 	     __shadow_walk_next(&iterator, spte)) {
4090 		leaf = iterator.level;
4091 		spte = mmu_spte_get_lockless(iterator.sptep);
4092 
4093 		sptes[leaf] = spte;
4094 	}
4095 
4096 	return leaf;
4097 }
4098 
4099 static int get_sptes_lockless(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,
4100 			      int *root_level)
4101 {
4102 	int leaf;
4103 
4104 	walk_shadow_page_lockless_begin(vcpu);
4105 
4106 	if (is_tdp_mmu_active(vcpu))
4107 		leaf = kvm_tdp_mmu_get_walk(vcpu, addr, sptes, root_level);
4108 	else
4109 		leaf = get_walk(vcpu, addr, sptes, root_level);
4110 
4111 	walk_shadow_page_lockless_end(vcpu);
4112 	return leaf;
4113 }
4114 
4115 /* return true if reserved bit(s) are detected on a valid, non-MMIO SPTE. */
4116 static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
4117 {
4118 	u64 sptes[PT64_ROOT_MAX_LEVEL + 1];
4119 	struct rsvd_bits_validate *rsvd_check;
4120 	int root, leaf, level;
4121 	bool reserved = false;
4122 
4123 	leaf = get_sptes_lockless(vcpu, addr, sptes, &root);
4124 	if (unlikely(leaf < 0)) {
4125 		*sptep = 0ull;
4126 		return reserved;
4127 	}
4128 
4129 	*sptep = sptes[leaf];
4130 
4131 	/*
4132 	 * Skip reserved bits checks on the terminal leaf if it's not a valid
4133 	 * SPTE.  Note, this also (intentionally) skips MMIO SPTEs, which, by
4134 	 * design, always have reserved bits set.  The purpose of the checks is
4135 	 * to detect reserved bits on non-MMIO SPTEs. i.e. buggy SPTEs.
4136 	 */
4137 	if (!is_shadow_present_pte(sptes[leaf]))
4138 		leaf++;
4139 
4140 	rsvd_check = &vcpu->arch.mmu->shadow_zero_check;
4141 
4142 	for (level = root; level >= leaf; level--)
4143 		reserved |= is_rsvd_spte(rsvd_check, sptes[level], level);
4144 
4145 	if (reserved) {
4146 		pr_err("%s: reserved bits set on MMU-present spte, addr 0x%llx, hierarchy:\n",
4147 		       __func__, addr);
4148 		for (level = root; level >= leaf; level--)
4149 			pr_err("------ spte = 0x%llx level = %d, rsvd bits = 0x%llx",
4150 			       sptes[level], level,
4151 			       get_rsvd_bits(rsvd_check, sptes[level], level));
4152 	}
4153 
4154 	return reserved;
4155 }
4156 
4157 static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
4158 {
4159 	u64 spte;
4160 	bool reserved;
4161 
4162 	if (mmio_info_in_cache(vcpu, addr, direct))
4163 		return RET_PF_EMULATE;
4164 
4165 	reserved = get_mmio_spte(vcpu, addr, &spte);
4166 	if (WARN_ON_ONCE(reserved))
4167 		return -EINVAL;
4168 
4169 	if (is_mmio_spte(vcpu->kvm, spte)) {
4170 		gfn_t gfn = get_mmio_spte_gfn(spte);
4171 		unsigned int access = get_mmio_spte_access(spte);
4172 
4173 		if (!check_mmio_spte(vcpu, spte))
4174 			return RET_PF_INVALID;
4175 
4176 		if (direct)
4177 			addr = 0;
4178 
4179 		trace_handle_mmio_page_fault(addr, gfn, access);
4180 		vcpu_cache_mmio_info(vcpu, addr, gfn, access);
4181 		return RET_PF_EMULATE;
4182 	}
4183 
4184 	/*
4185 	 * If the page table is zapped by other cpus, let CPU fault again on
4186 	 * the address.
4187 	 */
4188 	return RET_PF_RETRY;
4189 }
4190 
4191 static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
4192 					 struct kvm_page_fault *fault)
4193 {
4194 	if (unlikely(fault->rsvd))
4195 		return false;
4196 
4197 	if (!fault->present || !fault->write)
4198 		return false;
4199 
4200 	/*
4201 	 * guest is writing the page which is write tracked which can
4202 	 * not be fixed by page fault handler.
4203 	 */
4204 	if (kvm_gfn_is_write_tracked(vcpu->kvm, fault->slot, fault->gfn))
4205 		return true;
4206 
4207 	return false;
4208 }
4209 
4210 static void shadow_page_table_clear_flood(struct kvm_vcpu *vcpu, gva_t addr)
4211 {
4212 	struct kvm_shadow_walk_iterator iterator;
4213 	u64 spte;
4214 
4215 	walk_shadow_page_lockless_begin(vcpu);
4216 	for_each_shadow_entry_lockless(vcpu, addr, iterator, spte)
4217 		clear_sp_write_flooding_count(iterator.sptep);
4218 	walk_shadow_page_lockless_end(vcpu);
4219 }
4220 
4221 static u32 alloc_apf_token(struct kvm_vcpu *vcpu)
4222 {
4223 	/* make sure the token value is not 0 */
4224 	u32 id = vcpu->arch.apf.id;
4225 
4226 	if (id << 12 == 0)
4227 		vcpu->arch.apf.id = 1;
4228 
4229 	return (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
4230 }
4231 
4232 static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu,
4233 				    struct kvm_page_fault *fault)
4234 {
4235 	struct kvm_arch_async_pf arch;
4236 
4237 	arch.token = alloc_apf_token(vcpu);
4238 	arch.gfn = fault->gfn;
4239 	arch.error_code = fault->error_code;
4240 	arch.direct_map = vcpu->arch.mmu->root_role.direct;
4241 	arch.cr3 = kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu);
4242 
4243 	return kvm_setup_async_pf(vcpu, fault->addr,
4244 				  kvm_vcpu_gfn_to_hva(vcpu, fault->gfn), &arch);
4245 }
4246 
4247 void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
4248 {
4249 	int r;
4250 
4251 	if (WARN_ON_ONCE(work->arch.error_code & PFERR_PRIVATE_ACCESS))
4252 		return;
4253 
4254 	if ((vcpu->arch.mmu->root_role.direct != work->arch.direct_map) ||
4255 	      work->wakeup_all)
4256 		return;
4257 
4258 	r = kvm_mmu_reload(vcpu);
4259 	if (unlikely(r))
4260 		return;
4261 
4262 	if (!vcpu->arch.mmu->root_role.direct &&
4263 	      work->arch.cr3 != kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu))
4264 		return;
4265 
4266 	r = kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, work->arch.error_code,
4267 				  true, NULL, NULL);
4268 
4269 	/*
4270 	 * Account fixed page faults, otherwise they'll never be counted, but
4271 	 * ignore stats for all other return times.  Page-ready "faults" aren't
4272 	 * truly spurious and never trigger emulation
4273 	 */
4274 	if (r == RET_PF_FIXED)
4275 		vcpu->stat.pf_fixed++;
4276 }
4277 
4278 static inline u8 kvm_max_level_for_order(int order)
4279 {
4280 	BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G);
4281 
4282 	KVM_MMU_WARN_ON(order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G) &&
4283 			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M) &&
4284 			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K));
4285 
4286 	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G))
4287 		return PG_LEVEL_1G;
4288 
4289 	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M))
4290 		return PG_LEVEL_2M;
4291 
4292 	return PG_LEVEL_4K;
4293 }
4294 
4295 static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn,
4296 					u8 max_level, int gmem_order)
4297 {
4298 	u8 req_max_level;
4299 
4300 	if (max_level == PG_LEVEL_4K)
4301 		return PG_LEVEL_4K;
4302 
4303 	max_level = min(kvm_max_level_for_order(gmem_order), max_level);
4304 	if (max_level == PG_LEVEL_4K)
4305 		return PG_LEVEL_4K;
4306 
4307 	req_max_level = kvm_x86_call(private_max_mapping_level)(kvm, pfn);
4308 	if (req_max_level)
4309 		max_level = min(max_level, req_max_level);
4310 
4311 	return max_level;
4312 }
4313 
4314 static void kvm_mmu_finish_page_fault(struct kvm_vcpu *vcpu,
4315 				      struct kvm_page_fault *fault, int r)
4316 {
4317 	kvm_release_faultin_page(vcpu->kvm, fault->refcounted_page,
4318 				 r == RET_PF_RETRY, fault->map_writable);
4319 }
4320 
4321 static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu,
4322 				       struct kvm_page_fault *fault)
4323 {
4324 	int max_order, r;
4325 
4326 	if (!kvm_slot_can_be_private(fault->slot)) {
4327 		kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
4328 		return -EFAULT;
4329 	}
4330 
4331 	r = kvm_gmem_get_pfn(vcpu->kvm, fault->slot, fault->gfn, &fault->pfn,
4332 			     &fault->refcounted_page, &max_order);
4333 	if (r) {
4334 		kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
4335 		return r;
4336 	}
4337 
4338 	fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY);
4339 	fault->max_level = kvm_max_private_mapping_level(vcpu->kvm, fault->pfn,
4340 							 fault->max_level, max_order);
4341 
4342 	return RET_PF_CONTINUE;
4343 }
4344 
4345 static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
4346 				 struct kvm_page_fault *fault)
4347 {
4348 	unsigned int foll = fault->write ? FOLL_WRITE : 0;
4349 
4350 	if (fault->is_private)
4351 		return kvm_mmu_faultin_pfn_private(vcpu, fault);
4352 
4353 	foll |= FOLL_NOWAIT;
4354 	fault->pfn = __kvm_faultin_pfn(fault->slot, fault->gfn, foll,
4355 				       &fault->map_writable, &fault->refcounted_page);
4356 
4357 	/*
4358 	 * If resolving the page failed because I/O is needed to fault-in the
4359 	 * page, then either set up an asynchronous #PF to do the I/O, or if
4360 	 * doing an async #PF isn't possible, retry with I/O allowed.  All
4361 	 * other failures are terminal, i.e. retrying won't help.
4362 	 */
4363 	if (fault->pfn != KVM_PFN_ERR_NEEDS_IO)
4364 		return RET_PF_CONTINUE;
4365 
4366 	if (!fault->prefetch && kvm_can_do_async_pf(vcpu)) {
4367 		trace_kvm_try_async_get_page(fault->addr, fault->gfn);
4368 		if (kvm_find_async_pf_gfn(vcpu, fault->gfn)) {
4369 			trace_kvm_async_pf_repeated_fault(fault->addr, fault->gfn);
4370 			kvm_make_request(KVM_REQ_APF_HALT, vcpu);
4371 			return RET_PF_RETRY;
4372 		} else if (kvm_arch_setup_async_pf(vcpu, fault)) {
4373 			return RET_PF_RETRY;
4374 		}
4375 	}
4376 
4377 	/*
4378 	 * Allow gup to bail on pending non-fatal signals when it's also allowed
4379 	 * to wait for IO.  Note, gup always bails if it is unable to quickly
4380 	 * get a page and a fatal signal, i.e. SIGKILL, is pending.
4381 	 */
4382 	foll |= FOLL_INTERRUPTIBLE;
4383 	foll &= ~FOLL_NOWAIT;
4384 	fault->pfn = __kvm_faultin_pfn(fault->slot, fault->gfn, foll,
4385 				       &fault->map_writable, &fault->refcounted_page);
4386 
4387 	return RET_PF_CONTINUE;
4388 }
4389 
4390 static int kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
4391 			       struct kvm_page_fault *fault, unsigned int access)
4392 {
4393 	struct kvm_memory_slot *slot = fault->slot;
4394 	int ret;
4395 
4396 	/*
4397 	 * Note that the mmu_invalidate_seq also serves to detect a concurrent
4398 	 * change in attributes.  is_page_fault_stale() will detect an
4399 	 * invalidation relate to fault->fn and resume the guest without
4400 	 * installing a mapping in the page tables.
4401 	 */
4402 	fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq;
4403 	smp_rmb();
4404 
4405 	/*
4406 	 * Now that we have a snapshot of mmu_invalidate_seq we can check for a
4407 	 * private vs. shared mismatch.
4408 	 */
4409 	if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) {
4410 		kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
4411 		return -EFAULT;
4412 	}
4413 
4414 	if (unlikely(!slot))
4415 		return kvm_handle_noslot_fault(vcpu, fault, access);
4416 
4417 	/*
4418 	 * Retry the page fault if the gfn hit a memslot that is being deleted
4419 	 * or moved.  This ensures any existing SPTEs for the old memslot will
4420 	 * be zapped before KVM inserts a new MMIO SPTE for the gfn.
4421 	 */
4422 	if (slot->flags & KVM_MEMSLOT_INVALID)
4423 		return RET_PF_RETRY;
4424 
4425 	if (slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT) {
4426 		/*
4427 		 * Don't map L1's APIC access page into L2, KVM doesn't support
4428 		 * using APICv/AVIC to accelerate L2 accesses to L1's APIC,
4429 		 * i.e. the access needs to be emulated.  Emulating access to
4430 		 * L1's APIC is also correct if L1 is accelerating L2's own
4431 		 * virtual APIC, but for some reason L1 also maps _L1's_ APIC
4432 		 * into L2.  Note, vcpu_is_mmio_gpa() always treats access to
4433 		 * the APIC as MMIO.  Allow an MMIO SPTE to be created, as KVM
4434 		 * uses different roots for L1 vs. L2, i.e. there is no danger
4435 		 * of breaking APICv/AVIC for L1.
4436 		 */
4437 		if (is_guest_mode(vcpu))
4438 			return kvm_handle_noslot_fault(vcpu, fault, access);
4439 
4440 		/*
4441 		 * If the APIC access page exists but is disabled, go directly
4442 		 * to emulation without caching the MMIO access or creating a
4443 		 * MMIO SPTE.  That way the cache doesn't need to be purged
4444 		 * when the AVIC is re-enabled.
4445 		 */
4446 		if (!kvm_apicv_activated(vcpu->kvm))
4447 			return RET_PF_EMULATE;
4448 	}
4449 
4450 	/*
4451 	 * Check for a relevant mmu_notifier invalidation event before getting
4452 	 * the pfn from the primary MMU, and before acquiring mmu_lock.
4453 	 *
4454 	 * For mmu_lock, if there is an in-progress invalidation and the kernel
4455 	 * allows preemption, the invalidation task may drop mmu_lock and yield
4456 	 * in response to mmu_lock being contended, which is *very* counter-
4457 	 * productive as this vCPU can't actually make forward progress until
4458 	 * the invalidation completes.
4459 	 *
4460 	 * Retrying now can also avoid unnessary lock contention in the primary
4461 	 * MMU, as the primary MMU doesn't necessarily hold a single lock for
4462 	 * the duration of the invalidation, i.e. faulting in a conflicting pfn
4463 	 * can cause the invalidation to take longer by holding locks that are
4464 	 * needed to complete the invalidation.
4465 	 *
4466 	 * Do the pre-check even for non-preemtible kernels, i.e. even if KVM
4467 	 * will never yield mmu_lock in response to contention, as this vCPU is
4468 	 * *guaranteed* to need to retry, i.e. waiting until mmu_lock is held
4469 	 * to detect retry guarantees the worst case latency for the vCPU.
4470 	 */
4471 	if (mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn))
4472 		return RET_PF_RETRY;
4473 
4474 	ret = __kvm_mmu_faultin_pfn(vcpu, fault);
4475 	if (ret != RET_PF_CONTINUE)
4476 		return ret;
4477 
4478 	if (unlikely(is_error_pfn(fault->pfn)))
4479 		return kvm_handle_error_pfn(vcpu, fault);
4480 
4481 	if (WARN_ON_ONCE(!fault->slot || is_noslot_pfn(fault->pfn)))
4482 		return kvm_handle_noslot_fault(vcpu, fault, access);
4483 
4484 	/*
4485 	 * Check again for a relevant mmu_notifier invalidation event purely to
4486 	 * avoid contending mmu_lock.  Most invalidations will be detected by
4487 	 * the previous check, but checking is extremely cheap relative to the
4488 	 * overall cost of failing to detect the invalidation until after
4489 	 * mmu_lock is acquired.
4490 	 */
4491 	if (mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn)) {
4492 		kvm_mmu_finish_page_fault(vcpu, fault, RET_PF_RETRY);
4493 		return RET_PF_RETRY;
4494 	}
4495 
4496 	return RET_PF_CONTINUE;
4497 }
4498 
4499 /*
4500  * Returns true if the page fault is stale and needs to be retried, i.e. if the
4501  * root was invalidated by a memslot update or a relevant mmu_notifier fired.
4502  */
4503 static bool is_page_fault_stale(struct kvm_vcpu *vcpu,
4504 				struct kvm_page_fault *fault)
4505 {
4506 	struct kvm_mmu_page *sp = root_to_sp(vcpu->arch.mmu->root.hpa);
4507 
4508 	/* Special roots, e.g. pae_root, are not backed by shadow pages. */
4509 	if (sp && is_obsolete_sp(vcpu->kvm, sp))
4510 		return true;
4511 
4512 	/*
4513 	 * Roots without an associated shadow page are considered invalid if
4514 	 * there is a pending request to free obsolete roots.  The request is
4515 	 * only a hint that the current root _may_ be obsolete and needs to be
4516 	 * reloaded, e.g. if the guest frees a PGD that KVM is tracking as a
4517 	 * previous root, then __kvm_mmu_prepare_zap_page() signals all vCPUs
4518 	 * to reload even if no vCPU is actively using the root.
4519 	 */
4520 	if (!sp && kvm_test_request(KVM_REQ_MMU_FREE_OBSOLETE_ROOTS, vcpu))
4521 		return true;
4522 
4523 	/*
4524 	 * Check for a relevant mmu_notifier invalidation event one last time
4525 	 * now that mmu_lock is held, as the "unsafe" checks performed without
4526 	 * holding mmu_lock can get false negatives.
4527 	 */
4528 	return fault->slot &&
4529 	       mmu_invalidate_retry_gfn(vcpu->kvm, fault->mmu_seq, fault->gfn);
4530 }
4531 
4532 static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
4533 {
4534 	int r;
4535 
4536 	/* Dummy roots are used only for shadowing bad guest roots. */
4537 	if (WARN_ON_ONCE(kvm_mmu_is_dummy_root(vcpu->arch.mmu->root.hpa)))
4538 		return RET_PF_RETRY;
4539 
4540 	if (page_fault_handle_page_track(vcpu, fault))
4541 		return RET_PF_WRITE_PROTECTED;
4542 
4543 	r = fast_page_fault(vcpu, fault);
4544 	if (r != RET_PF_INVALID)
4545 		return r;
4546 
4547 	r = mmu_topup_memory_caches(vcpu, false);
4548 	if (r)
4549 		return r;
4550 
4551 	r = kvm_mmu_faultin_pfn(vcpu, fault, ACC_ALL);
4552 	if (r != RET_PF_CONTINUE)
4553 		return r;
4554 
4555 	r = RET_PF_RETRY;
4556 	write_lock(&vcpu->kvm->mmu_lock);
4557 
4558 	if (is_page_fault_stale(vcpu, fault))
4559 		goto out_unlock;
4560 
4561 	r = make_mmu_pages_available(vcpu);
4562 	if (r)
4563 		goto out_unlock;
4564 
4565 	r = direct_map(vcpu, fault);
4566 
4567 out_unlock:
4568 	kvm_mmu_finish_page_fault(vcpu, fault, r);
4569 	write_unlock(&vcpu->kvm->mmu_lock);
4570 	return r;
4571 }
4572 
4573 static int nonpaging_page_fault(struct kvm_vcpu *vcpu,
4574 				struct kvm_page_fault *fault)
4575 {
4576 	/* This path builds a PAE pagetable, we can map 2mb pages at maximum. */
4577 	fault->max_level = PG_LEVEL_2M;
4578 	return direct_page_fault(vcpu, fault);
4579 }
4580 
4581 int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
4582 				u64 fault_address, char *insn, int insn_len)
4583 {
4584 	int r = 1;
4585 	u32 flags = vcpu->arch.apf.host_apf_flags;
4586 
4587 #ifndef CONFIG_X86_64
4588 	/* A 64-bit CR2 should be impossible on 32-bit KVM. */
4589 	if (WARN_ON_ONCE(fault_address >> 32))
4590 		return -EFAULT;
4591 #endif
4592 	/*
4593 	 * Legacy #PF exception only have a 32-bit error code.  Simply drop the
4594 	 * upper bits as KVM doesn't use them for #PF (because they are never
4595 	 * set), and to ensure there are no collisions with KVM-defined bits.
4596 	 */
4597 	if (WARN_ON_ONCE(error_code >> 32))
4598 		error_code = lower_32_bits(error_code);
4599 
4600 	/*
4601 	 * Restrict KVM-defined flags to bits 63:32 so that it's impossible for
4602 	 * them to conflict with #PF error codes, which are limited to 32 bits.
4603 	 */
4604 	BUILD_BUG_ON(lower_32_bits(PFERR_SYNTHETIC_MASK));
4605 
4606 	vcpu->arch.l1tf_flush_l1d = true;
4607 	if (!flags) {
4608 		trace_kvm_page_fault(vcpu, fault_address, error_code);
4609 
4610 		r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn,
4611 				insn_len);
4612 	} else if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) {
4613 		vcpu->arch.apf.host_apf_flags = 0;
4614 		local_irq_disable();
4615 		kvm_async_pf_task_wait_schedule(fault_address);
4616 		local_irq_enable();
4617 	} else {
4618 		WARN_ONCE(1, "Unexpected host async PF flags: %x\n", flags);
4619 	}
4620 
4621 	return r;
4622 }
4623 EXPORT_SYMBOL_GPL(kvm_handle_page_fault);
4624 
4625 #ifdef CONFIG_X86_64
4626 static int kvm_tdp_mmu_page_fault(struct kvm_vcpu *vcpu,
4627 				  struct kvm_page_fault *fault)
4628 {
4629 	int r;
4630 
4631 	if (page_fault_handle_page_track(vcpu, fault))
4632 		return RET_PF_WRITE_PROTECTED;
4633 
4634 	r = fast_page_fault(vcpu, fault);
4635 	if (r != RET_PF_INVALID)
4636 		return r;
4637 
4638 	r = mmu_topup_memory_caches(vcpu, false);
4639 	if (r)
4640 		return r;
4641 
4642 	r = kvm_mmu_faultin_pfn(vcpu, fault, ACC_ALL);
4643 	if (r != RET_PF_CONTINUE)
4644 		return r;
4645 
4646 	r = RET_PF_RETRY;
4647 	read_lock(&vcpu->kvm->mmu_lock);
4648 
4649 	if (is_page_fault_stale(vcpu, fault))
4650 		goto out_unlock;
4651 
4652 	r = kvm_tdp_mmu_map(vcpu, fault);
4653 
4654 out_unlock:
4655 	kvm_mmu_finish_page_fault(vcpu, fault, r);
4656 	read_unlock(&vcpu->kvm->mmu_lock);
4657 	return r;
4658 }
4659 #endif
4660 
4661 bool kvm_mmu_may_ignore_guest_pat(void)
4662 {
4663 	/*
4664 	 * When EPT is enabled (shadow_memtype_mask is non-zero), and the VM
4665 	 * has non-coherent DMA (DMA doesn't snoop CPU caches), KVM's ABI is to
4666 	 * honor the memtype from the guest's PAT so that guest accesses to
4667 	 * memory that is DMA'd aren't cached against the guest's wishes.  As a
4668 	 * result, KVM _may_ ignore guest PAT, whereas without non-coherent DMA,
4669 	 * KVM _always_ ignores guest PAT (when EPT is enabled).
4670 	 */
4671 	return shadow_memtype_mask;
4672 }
4673 
4674 int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
4675 {
4676 #ifdef CONFIG_X86_64
4677 	if (tdp_mmu_enabled)
4678 		return kvm_tdp_mmu_page_fault(vcpu, fault);
4679 #endif
4680 
4681 	return direct_page_fault(vcpu, fault);
4682 }
4683 
4684 static int kvm_tdp_map_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code,
4685 			    u8 *level)
4686 {
4687 	int r;
4688 
4689 	/*
4690 	 * Restrict to TDP page fault, since that's the only case where the MMU
4691 	 * is indexed by GPA.
4692 	 */
4693 	if (vcpu->arch.mmu->page_fault != kvm_tdp_page_fault)
4694 		return -EOPNOTSUPP;
4695 
4696 	do {
4697 		if (signal_pending(current))
4698 			return -EINTR;
4699 		cond_resched();
4700 		r = kvm_mmu_do_page_fault(vcpu, gpa, error_code, true, NULL, level);
4701 	} while (r == RET_PF_RETRY);
4702 
4703 	if (r < 0)
4704 		return r;
4705 
4706 	switch (r) {
4707 	case RET_PF_FIXED:
4708 	case RET_PF_SPURIOUS:
4709 	case RET_PF_WRITE_PROTECTED:
4710 		return 0;
4711 
4712 	case RET_PF_EMULATE:
4713 		return -ENOENT;
4714 
4715 	case RET_PF_RETRY:
4716 	case RET_PF_CONTINUE:
4717 	case RET_PF_INVALID:
4718 	default:
4719 		WARN_ONCE(1, "could not fix page fault during prefault");
4720 		return -EIO;
4721 	}
4722 }
4723 
4724 long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
4725 				    struct kvm_pre_fault_memory *range)
4726 {
4727 	u64 error_code = PFERR_GUEST_FINAL_MASK;
4728 	u8 level = PG_LEVEL_4K;
4729 	u64 end;
4730 	int r;
4731 
4732 	if (!vcpu->kvm->arch.pre_fault_allowed)
4733 		return -EOPNOTSUPP;
4734 
4735 	/*
4736 	 * reload is efficient when called repeatedly, so we can do it on
4737 	 * every iteration.
4738 	 */
4739 	r = kvm_mmu_reload(vcpu);
4740 	if (r)
4741 		return r;
4742 
4743 	if (kvm_arch_has_private_mem(vcpu->kvm) &&
4744 	    kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(range->gpa)))
4745 		error_code |= PFERR_PRIVATE_ACCESS;
4746 
4747 	/*
4748 	 * Shadow paging uses GVA for kvm page fault, so restrict to
4749 	 * two-dimensional paging.
4750 	 */
4751 	r = kvm_tdp_map_page(vcpu, range->gpa, error_code, &level);
4752 	if (r < 0)
4753 		return r;
4754 
4755 	/*
4756 	 * If the mapping that covers range->gpa can use a huge page, it
4757 	 * may start below it or end after range->gpa + range->size.
4758 	 */
4759 	end = (range->gpa & KVM_HPAGE_MASK(level)) + KVM_HPAGE_SIZE(level);
4760 	return min(range->size, end - range->gpa);
4761 }
4762 
4763 static void nonpaging_init_context(struct kvm_mmu *context)
4764 {
4765 	context->page_fault = nonpaging_page_fault;
4766 	context->gva_to_gpa = nonpaging_gva_to_gpa;
4767 	context->sync_spte = NULL;
4768 }
4769 
4770 static inline bool is_root_usable(struct kvm_mmu_root_info *root, gpa_t pgd,
4771 				  union kvm_mmu_page_role role)
4772 {
4773 	struct kvm_mmu_page *sp;
4774 
4775 	if (!VALID_PAGE(root->hpa))
4776 		return false;
4777 
4778 	if (!role.direct && pgd != root->pgd)
4779 		return false;
4780 
4781 	sp = root_to_sp(root->hpa);
4782 	if (WARN_ON_ONCE(!sp))
4783 		return false;
4784 
4785 	return role.word == sp->role.word;
4786 }
4787 
4788 /*
4789  * Find out if a previously cached root matching the new pgd/role is available,
4790  * and insert the current root as the MRU in the cache.
4791  * If a matching root is found, it is assigned to kvm_mmu->root and
4792  * true is returned.
4793  * If no match is found, kvm_mmu->root is left invalid, the LRU root is
4794  * evicted to make room for the current root, and false is returned.
4795  */
4796 static bool cached_root_find_and_keep_current(struct kvm *kvm, struct kvm_mmu *mmu,
4797 					      gpa_t new_pgd,
4798 					      union kvm_mmu_page_role new_role)
4799 {
4800 	uint i;
4801 
4802 	if (is_root_usable(&mmu->root, new_pgd, new_role))
4803 		return true;
4804 
4805 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
4806 		/*
4807 		 * The swaps end up rotating the cache like this:
4808 		 *   C   0 1 2 3   (on entry to the function)
4809 		 *   0   C 1 2 3
4810 		 *   1   C 0 2 3
4811 		 *   2   C 0 1 3
4812 		 *   3   C 0 1 2   (on exit from the loop)
4813 		 */
4814 		swap(mmu->root, mmu->prev_roots[i]);
4815 		if (is_root_usable(&mmu->root, new_pgd, new_role))
4816 			return true;
4817 	}
4818 
4819 	kvm_mmu_free_roots(kvm, mmu, KVM_MMU_ROOT_CURRENT);
4820 	return false;
4821 }
4822 
4823 /*
4824  * Find out if a previously cached root matching the new pgd/role is available.
4825  * On entry, mmu->root is invalid.
4826  * If a matching root is found, it is assigned to kvm_mmu->root, the LRU entry
4827  * of the cache becomes invalid, and true is returned.
4828  * If no match is found, kvm_mmu->root is left invalid and false is returned.
4829  */
4830 static bool cached_root_find_without_current(struct kvm *kvm, struct kvm_mmu *mmu,
4831 					     gpa_t new_pgd,
4832 					     union kvm_mmu_page_role new_role)
4833 {
4834 	uint i;
4835 
4836 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
4837 		if (is_root_usable(&mmu->prev_roots[i], new_pgd, new_role))
4838 			goto hit;
4839 
4840 	return false;
4841 
4842 hit:
4843 	swap(mmu->root, mmu->prev_roots[i]);
4844 	/* Bubble up the remaining roots.  */
4845 	for (; i < KVM_MMU_NUM_PREV_ROOTS - 1; i++)
4846 		mmu->prev_roots[i] = mmu->prev_roots[i + 1];
4847 	mmu->prev_roots[i].hpa = INVALID_PAGE;
4848 	return true;
4849 }
4850 
4851 static bool fast_pgd_switch(struct kvm *kvm, struct kvm_mmu *mmu,
4852 			    gpa_t new_pgd, union kvm_mmu_page_role new_role)
4853 {
4854 	/*
4855 	 * Limit reuse to 64-bit hosts+VMs without "special" roots in order to
4856 	 * avoid having to deal with PDPTEs and other complexities.
4857 	 */
4858 	if (VALID_PAGE(mmu->root.hpa) && !root_to_sp(mmu->root.hpa))
4859 		kvm_mmu_free_roots(kvm, mmu, KVM_MMU_ROOT_CURRENT);
4860 
4861 	if (VALID_PAGE(mmu->root.hpa))
4862 		return cached_root_find_and_keep_current(kvm, mmu, new_pgd, new_role);
4863 	else
4864 		return cached_root_find_without_current(kvm, mmu, new_pgd, new_role);
4865 }
4866 
4867 void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd)
4868 {
4869 	struct kvm_mmu *mmu = vcpu->arch.mmu;
4870 	union kvm_mmu_page_role new_role = mmu->root_role;
4871 
4872 	/*
4873 	 * Return immediately if no usable root was found, kvm_mmu_reload()
4874 	 * will establish a valid root prior to the next VM-Enter.
4875 	 */
4876 	if (!fast_pgd_switch(vcpu->kvm, mmu, new_pgd, new_role))
4877 		return;
4878 
4879 	/*
4880 	 * It's possible that the cached previous root page is obsolete because
4881 	 * of a change in the MMU generation number. However, changing the
4882 	 * generation number is accompanied by KVM_REQ_MMU_FREE_OBSOLETE_ROOTS,
4883 	 * which will free the root set here and allocate a new one.
4884 	 */
4885 	kvm_make_request(KVM_REQ_LOAD_MMU_PGD, vcpu);
4886 
4887 	if (force_flush_and_sync_on_reuse) {
4888 		kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
4889 		kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
4890 	}
4891 
4892 	/*
4893 	 * The last MMIO access's GVA and GPA are cached in the VCPU. When
4894 	 * switching to a new CR3, that GVA->GPA mapping may no longer be
4895 	 * valid. So clear any cached MMIO info even when we don't need to sync
4896 	 * the shadow page tables.
4897 	 */
4898 	vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
4899 
4900 	/*
4901 	 * If this is a direct root page, it doesn't have a write flooding
4902 	 * count. Otherwise, clear the write flooding count.
4903 	 */
4904 	if (!new_role.direct) {
4905 		struct kvm_mmu_page *sp = root_to_sp(vcpu->arch.mmu->root.hpa);
4906 
4907 		if (!WARN_ON_ONCE(!sp))
4908 			__clear_sp_write_flooding_count(sp);
4909 	}
4910 }
4911 EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);
4912 
4913 static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
4914 			   unsigned int access)
4915 {
4916 	if (unlikely(is_mmio_spte(vcpu->kvm, *sptep))) {
4917 		if (gfn != get_mmio_spte_gfn(*sptep)) {
4918 			mmu_spte_clear_no_track(sptep);
4919 			return true;
4920 		}
4921 
4922 		mark_mmio_spte(vcpu, sptep, gfn, access);
4923 		return true;
4924 	}
4925 
4926 	return false;
4927 }
4928 
4929 #define PTTYPE_EPT 18 /* arbitrary */
4930 #define PTTYPE PTTYPE_EPT
4931 #include "paging_tmpl.h"
4932 #undef PTTYPE
4933 
4934 #define PTTYPE 64
4935 #include "paging_tmpl.h"
4936 #undef PTTYPE
4937 
4938 #define PTTYPE 32
4939 #include "paging_tmpl.h"
4940 #undef PTTYPE
4941 
4942 static void __reset_rsvds_bits_mask(struct rsvd_bits_validate *rsvd_check,
4943 				    u64 pa_bits_rsvd, int level, bool nx,
4944 				    bool gbpages, bool pse, bool amd)
4945 {
4946 	u64 gbpages_bit_rsvd = 0;
4947 	u64 nonleaf_bit8_rsvd = 0;
4948 	u64 high_bits_rsvd;
4949 
4950 	rsvd_check->bad_mt_xwr = 0;
4951 
4952 	if (!gbpages)
4953 		gbpages_bit_rsvd = rsvd_bits(7, 7);
4954 
4955 	if (level == PT32E_ROOT_LEVEL)
4956 		high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 62);
4957 	else
4958 		high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 51);
4959 
4960 	/* Note, NX doesn't exist in PDPTEs, this is handled below. */
4961 	if (!nx)
4962 		high_bits_rsvd |= rsvd_bits(63, 63);
4963 
4964 	/*
4965 	 * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for
4966 	 * leaf entries) on AMD CPUs only.
4967 	 */
4968 	if (amd)
4969 		nonleaf_bit8_rsvd = rsvd_bits(8, 8);
4970 
4971 	switch (level) {
4972 	case PT32_ROOT_LEVEL:
4973 		/* no rsvd bits for 2 level 4K page table entries */
4974 		rsvd_check->rsvd_bits_mask[0][1] = 0;
4975 		rsvd_check->rsvd_bits_mask[0][0] = 0;
4976 		rsvd_check->rsvd_bits_mask[1][0] =
4977 			rsvd_check->rsvd_bits_mask[0][0];
4978 
4979 		if (!pse) {
4980 			rsvd_check->rsvd_bits_mask[1][1] = 0;
4981 			break;
4982 		}
4983 
4984 		if (is_cpuid_PSE36())
4985 			/* 36bits PSE 4MB page */
4986 			rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
4987 		else
4988 			/* 32 bits PSE 4MB page */
4989 			rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
4990 		break;
4991 	case PT32E_ROOT_LEVEL:
4992 		rsvd_check->rsvd_bits_mask[0][2] = rsvd_bits(63, 63) |
4993 						   high_bits_rsvd |
4994 						   rsvd_bits(5, 8) |
4995 						   rsvd_bits(1, 2);	/* PDPTE */
4996 		rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd;	/* PDE */
4997 		rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd;	/* PTE */
4998 		rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd |
4999 						   rsvd_bits(13, 20);	/* large page */
5000 		rsvd_check->rsvd_bits_mask[1][0] =
5001 			rsvd_check->rsvd_bits_mask[0][0];
5002 		break;
5003 	case PT64_ROOT_5LEVEL:
5004 		rsvd_check->rsvd_bits_mask[0][4] = high_bits_rsvd |
5005 						   nonleaf_bit8_rsvd |
5006 						   rsvd_bits(7, 7);
5007 		rsvd_check->rsvd_bits_mask[1][4] =
5008 			rsvd_check->rsvd_bits_mask[0][4];
5009 		fallthrough;
5010 	case PT64_ROOT_4LEVEL:
5011 		rsvd_check->rsvd_bits_mask[0][3] = high_bits_rsvd |
5012 						   nonleaf_bit8_rsvd |
5013 						   rsvd_bits(7, 7);
5014 		rsvd_check->rsvd_bits_mask[0][2] = high_bits_rsvd |
5015 						   gbpages_bit_rsvd;
5016 		rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd;
5017 		rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd;
5018 		rsvd_check->rsvd_bits_mask[1][3] =
5019 			rsvd_check->rsvd_bits_mask[0][3];
5020 		rsvd_check->rsvd_bits_mask[1][2] = high_bits_rsvd |
5021 						   gbpages_bit_rsvd |
5022 						   rsvd_bits(13, 29);
5023 		rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd |
5024 						   rsvd_bits(13, 20); /* large page */
5025 		rsvd_check->rsvd_bits_mask[1][0] =
5026 			rsvd_check->rsvd_bits_mask[0][0];
5027 		break;
5028 	}
5029 }
5030 
5031 static void reset_guest_rsvds_bits_mask(struct kvm_vcpu *vcpu,
5032 					struct kvm_mmu *context)
5033 {
5034 	__reset_rsvds_bits_mask(&context->guest_rsvd_check,
5035 				vcpu->arch.reserved_gpa_bits,
5036 				context->cpu_role.base.level, is_efer_nx(context),
5037 				guest_can_use(vcpu, X86_FEATURE_GBPAGES),
5038 				is_cr4_pse(context),
5039 				guest_cpuid_is_amd_compatible(vcpu));
5040 }
5041 
5042 static void __reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check,
5043 					u64 pa_bits_rsvd, bool execonly,
5044 					int huge_page_level)
5045 {
5046 	u64 high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 51);
5047 	u64 large_1g_rsvd = 0, large_2m_rsvd = 0;
5048 	u64 bad_mt_xwr;
5049 
5050 	if (huge_page_level < PG_LEVEL_1G)
5051 		large_1g_rsvd = rsvd_bits(7, 7);
5052 	if (huge_page_level < PG_LEVEL_2M)
5053 		large_2m_rsvd = rsvd_bits(7, 7);
5054 
5055 	rsvd_check->rsvd_bits_mask[0][4] = high_bits_rsvd | rsvd_bits(3, 7);
5056 	rsvd_check->rsvd_bits_mask[0][3] = high_bits_rsvd | rsvd_bits(3, 7);
5057 	rsvd_check->rsvd_bits_mask[0][2] = high_bits_rsvd | rsvd_bits(3, 6) | large_1g_rsvd;
5058 	rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd | rsvd_bits(3, 6) | large_2m_rsvd;
5059 	rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd;
5060 
5061 	/* large page */
5062 	rsvd_check->rsvd_bits_mask[1][4] = rsvd_check->rsvd_bits_mask[0][4];
5063 	rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3];
5064 	rsvd_check->rsvd_bits_mask[1][2] = high_bits_rsvd | rsvd_bits(12, 29) | large_1g_rsvd;
5065 	rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd | rsvd_bits(12, 20) | large_2m_rsvd;
5066 	rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0];
5067 
5068 	bad_mt_xwr = 0xFFull << (2 * 8);	/* bits 3..5 must not be 2 */
5069 	bad_mt_xwr |= 0xFFull << (3 * 8);	/* bits 3..5 must not be 3 */
5070 	bad_mt_xwr |= 0xFFull << (7 * 8);	/* bits 3..5 must not be 7 */
5071 	bad_mt_xwr |= REPEAT_BYTE(1ull << 2);	/* bits 0..2 must not be 010 */
5072 	bad_mt_xwr |= REPEAT_BYTE(1ull << 6);	/* bits 0..2 must not be 110 */
5073 	if (!execonly) {
5074 		/* bits 0..2 must not be 100 unless VMX capabilities allow it */
5075 		bad_mt_xwr |= REPEAT_BYTE(1ull << 4);
5076 	}
5077 	rsvd_check->bad_mt_xwr = bad_mt_xwr;
5078 }
5079 
5080 static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,
5081 		struct kvm_mmu *context, bool execonly, int huge_page_level)
5082 {
5083 	__reset_rsvds_bits_mask_ept(&context->guest_rsvd_check,
5084 				    vcpu->arch.reserved_gpa_bits, execonly,
5085 				    huge_page_level);
5086 }
5087 
5088 static inline u64 reserved_hpa_bits(void)
5089 {
5090 	return rsvd_bits(kvm_host.maxphyaddr, 63);
5091 }
5092 
5093 /*
5094  * the page table on host is the shadow page table for the page
5095  * table in guest or amd nested guest, its mmu features completely
5096  * follow the features in guest.
5097  */
5098 static void reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
5099 					struct kvm_mmu *context)
5100 {
5101 	/* @amd adds a check on bit of SPTEs, which KVM shouldn't use anyways. */
5102 	bool is_amd = true;
5103 	/* KVM doesn't use 2-level page tables for the shadow MMU. */
5104 	bool is_pse = false;
5105 	struct rsvd_bits_validate *shadow_zero_check;
5106 	int i;
5107 
5108 	WARN_ON_ONCE(context->root_role.level < PT32E_ROOT_LEVEL);
5109 
5110 	shadow_zero_check = &context->shadow_zero_check;
5111 	__reset_rsvds_bits_mask(shadow_zero_check, reserved_hpa_bits(),
5112 				context->root_role.level,
5113 				context->root_role.efer_nx,
5114 				guest_can_use(vcpu, X86_FEATURE_GBPAGES),
5115 				is_pse, is_amd);
5116 
5117 	if (!shadow_me_mask)
5118 		return;
5119 
5120 	for (i = context->root_role.level; --i >= 0;) {
5121 		/*
5122 		 * So far shadow_me_value is a constant during KVM's life
5123 		 * time.  Bits in shadow_me_value are allowed to be set.
5124 		 * Bits in shadow_me_mask but not in shadow_me_value are
5125 		 * not allowed to be set.
5126 		 */
5127 		shadow_zero_check->rsvd_bits_mask[0][i] |= shadow_me_mask;
5128 		shadow_zero_check->rsvd_bits_mask[1][i] |= shadow_me_mask;
5129 		shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_value;
5130 		shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_value;
5131 	}
5132 
5133 }
5134 
5135 static inline bool boot_cpu_is_amd(void)
5136 {
5137 	WARN_ON_ONCE(!tdp_enabled);
5138 	return shadow_x_mask == 0;
5139 }
5140 
5141 /*
5142  * the direct page table on host, use as much mmu features as
5143  * possible, however, kvm currently does not do execution-protection.
5144  */
5145 static void reset_tdp_shadow_zero_bits_mask(struct kvm_mmu *context)
5146 {
5147 	struct rsvd_bits_validate *shadow_zero_check;
5148 	int i;
5149 
5150 	shadow_zero_check = &context->shadow_zero_check;
5151 
5152 	if (boot_cpu_is_amd())
5153 		__reset_rsvds_bits_mask(shadow_zero_check, reserved_hpa_bits(),
5154 					context->root_role.level, true,
5155 					boot_cpu_has(X86_FEATURE_GBPAGES),
5156 					false, true);
5157 	else
5158 		__reset_rsvds_bits_mask_ept(shadow_zero_check,
5159 					    reserved_hpa_bits(), false,
5160 					    max_huge_page_level);
5161 
5162 	if (!shadow_me_mask)
5163 		return;
5164 
5165 	for (i = context->root_role.level; --i >= 0;) {
5166 		shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask;
5167 		shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask;
5168 	}
5169 }
5170 
5171 /*
5172  * as the comments in reset_shadow_zero_bits_mask() except it
5173  * is the shadow page table for intel nested guest.
5174  */
5175 static void
5176 reset_ept_shadow_zero_bits_mask(struct kvm_mmu *context, bool execonly)
5177 {
5178 	__reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
5179 				    reserved_hpa_bits(), execonly,
5180 				    max_huge_page_level);
5181 }
5182 
5183 #define BYTE_MASK(access) \
5184 	((1 & (access) ? 2 : 0) | \
5185 	 (2 & (access) ? 4 : 0) | \
5186 	 (3 & (access) ? 8 : 0) | \
5187 	 (4 & (access) ? 16 : 0) | \
5188 	 (5 & (access) ? 32 : 0) | \
5189 	 (6 & (access) ? 64 : 0) | \
5190 	 (7 & (access) ? 128 : 0))
5191 
5192 
5193 static void update_permission_bitmask(struct kvm_mmu *mmu, bool ept)
5194 {
5195 	unsigned byte;
5196 
5197 	const u8 x = BYTE_MASK(ACC_EXEC_MASK);
5198 	const u8 w = BYTE_MASK(ACC_WRITE_MASK);
5199 	const u8 u = BYTE_MASK(ACC_USER_MASK);
5200 
5201 	bool cr4_smep = is_cr4_smep(mmu);
5202 	bool cr4_smap = is_cr4_smap(mmu);
5203 	bool cr0_wp = is_cr0_wp(mmu);
5204 	bool efer_nx = is_efer_nx(mmu);
5205 
5206 	for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) {
5207 		unsigned pfec = byte << 1;
5208 
5209 		/*
5210 		 * Each "*f" variable has a 1 bit for each UWX value
5211 		 * that causes a fault with the given PFEC.
5212 		 */
5213 
5214 		/* Faults from writes to non-writable pages */
5215 		u8 wf = (pfec & PFERR_WRITE_MASK) ? (u8)~w : 0;
5216 		/* Faults from user mode accesses to supervisor pages */
5217 		u8 uf = (pfec & PFERR_USER_MASK) ? (u8)~u : 0;
5218 		/* Faults from fetches of non-executable pages*/
5219 		u8 ff = (pfec & PFERR_FETCH_MASK) ? (u8)~x : 0;
5220 		/* Faults from kernel mode fetches of user pages */
5221 		u8 smepf = 0;
5222 		/* Faults from kernel mode accesses of user pages */
5223 		u8 smapf = 0;
5224 
5225 		if (!ept) {
5226 			/* Faults from kernel mode accesses to user pages */
5227 			u8 kf = (pfec & PFERR_USER_MASK) ? 0 : u;
5228 
5229 			/* Not really needed: !nx will cause pte.nx to fault */
5230 			if (!efer_nx)
5231 				ff = 0;
5232 
5233 			/* Allow supervisor writes if !cr0.wp */
5234 			if (!cr0_wp)
5235 				wf = (pfec & PFERR_USER_MASK) ? wf : 0;
5236 
5237 			/* Disallow supervisor fetches of user code if cr4.smep */
5238 			if (cr4_smep)
5239 				smepf = (pfec & PFERR_FETCH_MASK) ? kf : 0;
5240 
5241 			/*
5242 			 * SMAP:kernel-mode data accesses from user-mode
5243 			 * mappings should fault. A fault is considered
5244 			 * as a SMAP violation if all of the following
5245 			 * conditions are true:
5246 			 *   - X86_CR4_SMAP is set in CR4
5247 			 *   - A user page is accessed
5248 			 *   - The access is not a fetch
5249 			 *   - The access is supervisor mode
5250 			 *   - If implicit supervisor access or X86_EFLAGS_AC is clear
5251 			 *
5252 			 * Here, we cover the first four conditions.
5253 			 * The fifth is computed dynamically in permission_fault();
5254 			 * PFERR_RSVD_MASK bit will be set in PFEC if the access is
5255 			 * *not* subject to SMAP restrictions.
5256 			 */
5257 			if (cr4_smap)
5258 				smapf = (pfec & (PFERR_RSVD_MASK|PFERR_FETCH_MASK)) ? 0 : kf;
5259 		}
5260 
5261 		mmu->permissions[byte] = ff | uf | wf | smepf | smapf;
5262 	}
5263 }
5264 
5265 /*
5266 * PKU is an additional mechanism by which the paging controls access to
5267 * user-mode addresses based on the value in the PKRU register.  Protection
5268 * key violations are reported through a bit in the page fault error code.
5269 * Unlike other bits of the error code, the PK bit is not known at the
5270 * call site of e.g. gva_to_gpa; it must be computed directly in
5271 * permission_fault based on two bits of PKRU, on some machine state (CR4,
5272 * CR0, EFER, CPL), and on other bits of the error code and the page tables.
5273 *
5274 * In particular the following conditions come from the error code, the
5275 * page tables and the machine state:
5276 * - PK is always zero unless CR4.PKE=1 and EFER.LMA=1
5277 * - PK is always zero if RSVD=1 (reserved bit set) or F=1 (instruction fetch)
5278 * - PK is always zero if U=0 in the page tables
5279 * - PKRU.WD is ignored if CR0.WP=0 and the access is a supervisor access.
5280 *
5281 * The PKRU bitmask caches the result of these four conditions.  The error
5282 * code (minus the P bit) and the page table's U bit form an index into the
5283 * PKRU bitmask.  Two bits of the PKRU bitmask are then extracted and ANDed
5284 * with the two bits of the PKRU register corresponding to the protection key.
5285 * For the first three conditions above the bits will be 00, thus masking
5286 * away both AD and WD.  For all reads or if the last condition holds, WD
5287 * only will be masked away.
5288 */
5289 static void update_pkru_bitmask(struct kvm_mmu *mmu)
5290 {
5291 	unsigned bit;
5292 	bool wp;
5293 
5294 	mmu->pkru_mask = 0;
5295 
5296 	if (!is_cr4_pke(mmu))
5297 		return;
5298 
5299 	wp = is_cr0_wp(mmu);
5300 
5301 	for (bit = 0; bit < ARRAY_SIZE(mmu->permissions); ++bit) {
5302 		unsigned pfec, pkey_bits;
5303 		bool check_pkey, check_write, ff, uf, wf, pte_user;
5304 
5305 		pfec = bit << 1;
5306 		ff = pfec & PFERR_FETCH_MASK;
5307 		uf = pfec & PFERR_USER_MASK;
5308 		wf = pfec & PFERR_WRITE_MASK;
5309 
5310 		/* PFEC.RSVD is replaced by ACC_USER_MASK. */
5311 		pte_user = pfec & PFERR_RSVD_MASK;
5312 
5313 		/*
5314 		 * Only need to check the access which is not an
5315 		 * instruction fetch and is to a user page.
5316 		 */
5317 		check_pkey = (!ff && pte_user);
5318 		/*
5319 		 * write access is controlled by PKRU if it is a
5320 		 * user access or CR0.WP = 1.
5321 		 */
5322 		check_write = check_pkey && wf && (uf || wp);
5323 
5324 		/* PKRU.AD stops both read and write access. */
5325 		pkey_bits = !!check_pkey;
5326 		/* PKRU.WD stops write access. */
5327 		pkey_bits |= (!!check_write) << 1;
5328 
5329 		mmu->pkru_mask |= (pkey_bits & 3) << pfec;
5330 	}
5331 }
5332 
5333 static void reset_guest_paging_metadata(struct kvm_vcpu *vcpu,
5334 					struct kvm_mmu *mmu)
5335 {
5336 	if (!is_cr0_pg(mmu))
5337 		return;
5338 
5339 	reset_guest_rsvds_bits_mask(vcpu, mmu);
5340 	update_permission_bitmask(mmu, false);
5341 	update_pkru_bitmask(mmu);
5342 }
5343 
5344 static void paging64_init_context(struct kvm_mmu *context)
5345 {
5346 	context->page_fault = paging64_page_fault;
5347 	context->gva_to_gpa = paging64_gva_to_gpa;
5348 	context->sync_spte = paging64_sync_spte;
5349 }
5350 
5351 static void paging32_init_context(struct kvm_mmu *context)
5352 {
5353 	context->page_fault = paging32_page_fault;
5354 	context->gva_to_gpa = paging32_gva_to_gpa;
5355 	context->sync_spte = paging32_sync_spte;
5356 }
5357 
5358 static union kvm_cpu_role kvm_calc_cpu_role(struct kvm_vcpu *vcpu,
5359 					    const struct kvm_mmu_role_regs *regs)
5360 {
5361 	union kvm_cpu_role role = {0};
5362 
5363 	role.base.access = ACC_ALL;
5364 	role.base.smm = is_smm(vcpu);
5365 	role.base.guest_mode = is_guest_mode(vcpu);
5366 	role.ext.valid = 1;
5367 
5368 	if (!____is_cr0_pg(regs)) {
5369 		role.base.direct = 1;
5370 		return role;
5371 	}
5372 
5373 	role.base.efer_nx = ____is_efer_nx(regs);
5374 	role.base.cr0_wp = ____is_cr0_wp(regs);
5375 	role.base.smep_andnot_wp = ____is_cr4_smep(regs) && !____is_cr0_wp(regs);
5376 	role.base.smap_andnot_wp = ____is_cr4_smap(regs) && !____is_cr0_wp(regs);
5377 	role.base.has_4_byte_gpte = !____is_cr4_pae(regs);
5378 
5379 	if (____is_efer_lma(regs))
5380 		role.base.level = ____is_cr4_la57(regs) ? PT64_ROOT_5LEVEL
5381 							: PT64_ROOT_4LEVEL;
5382 	else if (____is_cr4_pae(regs))
5383 		role.base.level = PT32E_ROOT_LEVEL;
5384 	else
5385 		role.base.level = PT32_ROOT_LEVEL;
5386 
5387 	role.ext.cr4_smep = ____is_cr4_smep(regs);
5388 	role.ext.cr4_smap = ____is_cr4_smap(regs);
5389 	role.ext.cr4_pse = ____is_cr4_pse(regs);
5390 
5391 	/* PKEY and LA57 are active iff long mode is active. */
5392 	role.ext.cr4_pke = ____is_efer_lma(regs) && ____is_cr4_pke(regs);
5393 	role.ext.cr4_la57 = ____is_efer_lma(regs) && ____is_cr4_la57(regs);
5394 	role.ext.efer_lma = ____is_efer_lma(regs);
5395 	return role;
5396 }
5397 
5398 void __kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,
5399 					struct kvm_mmu *mmu)
5400 {
5401 	const bool cr0_wp = kvm_is_cr0_bit_set(vcpu, X86_CR0_WP);
5402 
5403 	BUILD_BUG_ON((KVM_MMU_CR0_ROLE_BITS & KVM_POSSIBLE_CR0_GUEST_BITS) != X86_CR0_WP);
5404 	BUILD_BUG_ON((KVM_MMU_CR4_ROLE_BITS & KVM_POSSIBLE_CR4_GUEST_BITS));
5405 
5406 	if (is_cr0_wp(mmu) == cr0_wp)
5407 		return;
5408 
5409 	mmu->cpu_role.base.cr0_wp = cr0_wp;
5410 	reset_guest_paging_metadata(vcpu, mmu);
5411 }
5412 
5413 static inline int kvm_mmu_get_tdp_level(struct kvm_vcpu *vcpu)
5414 {
5415 	/* tdp_root_level is architecture forced level, use it if nonzero */
5416 	if (tdp_root_level)
5417 		return tdp_root_level;
5418 
5419 	/* Use 5-level TDP if and only if it's useful/necessary. */
5420 	if (max_tdp_level == 5 && cpuid_maxphyaddr(vcpu) <= 48)
5421 		return 4;
5422 
5423 	return max_tdp_level;
5424 }
5425 
5426 u8 kvm_mmu_get_max_tdp_level(void)
5427 {
5428 	return tdp_root_level ? tdp_root_level : max_tdp_level;
5429 }
5430 
5431 static union kvm_mmu_page_role
5432 kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu,
5433 				union kvm_cpu_role cpu_role)
5434 {
5435 	union kvm_mmu_page_role role = {0};
5436 
5437 	role.access = ACC_ALL;
5438 	role.cr0_wp = true;
5439 	role.efer_nx = true;
5440 	role.smm = cpu_role.base.smm;
5441 	role.guest_mode = cpu_role.base.guest_mode;
5442 	role.ad_disabled = !kvm_ad_enabled;
5443 	role.level = kvm_mmu_get_tdp_level(vcpu);
5444 	role.direct = true;
5445 	role.has_4_byte_gpte = false;
5446 
5447 	return role;
5448 }
5449 
5450 static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu,
5451 			     union kvm_cpu_role cpu_role)
5452 {
5453 	struct kvm_mmu *context = &vcpu->arch.root_mmu;
5454 	union kvm_mmu_page_role root_role = kvm_calc_tdp_mmu_root_page_role(vcpu, cpu_role);
5455 
5456 	if (cpu_role.as_u64 == context->cpu_role.as_u64 &&
5457 	    root_role.word == context->root_role.word)
5458 		return;
5459 
5460 	context->cpu_role.as_u64 = cpu_role.as_u64;
5461 	context->root_role.word = root_role.word;
5462 	context->page_fault = kvm_tdp_page_fault;
5463 	context->sync_spte = NULL;
5464 	context->get_guest_pgd = get_guest_cr3;
5465 	context->get_pdptr = kvm_pdptr_read;
5466 	context->inject_page_fault = kvm_inject_page_fault;
5467 
5468 	if (!is_cr0_pg(context))
5469 		context->gva_to_gpa = nonpaging_gva_to_gpa;
5470 	else if (is_cr4_pae(context))
5471 		context->gva_to_gpa = paging64_gva_to_gpa;
5472 	else
5473 		context->gva_to_gpa = paging32_gva_to_gpa;
5474 
5475 	reset_guest_paging_metadata(vcpu, context);
5476 	reset_tdp_shadow_zero_bits_mask(context);
5477 }
5478 
5479 static void shadow_mmu_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *context,
5480 				    union kvm_cpu_role cpu_role,
5481 				    union kvm_mmu_page_role root_role)
5482 {
5483 	if (cpu_role.as_u64 == context->cpu_role.as_u64 &&
5484 	    root_role.word == context->root_role.word)
5485 		return;
5486 
5487 	context->cpu_role.as_u64 = cpu_role.as_u64;
5488 	context->root_role.word = root_role.word;
5489 
5490 	if (!is_cr0_pg(context))
5491 		nonpaging_init_context(context);
5492 	else if (is_cr4_pae(context))
5493 		paging64_init_context(context);
5494 	else
5495 		paging32_init_context(context);
5496 
5497 	reset_guest_paging_metadata(vcpu, context);
5498 	reset_shadow_zero_bits_mask(vcpu, context);
5499 }
5500 
5501 static void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu,
5502 				union kvm_cpu_role cpu_role)
5503 {
5504 	struct kvm_mmu *context = &vcpu->arch.root_mmu;
5505 	union kvm_mmu_page_role root_role;
5506 
5507 	root_role = cpu_role.base;
5508 
5509 	/* KVM uses PAE paging whenever the guest isn't using 64-bit paging. */
5510 	root_role.level = max_t(u32, root_role.level, PT32E_ROOT_LEVEL);
5511 
5512 	/*
5513 	 * KVM forces EFER.NX=1 when TDP is disabled, reflect it in the MMU role.
5514 	 * KVM uses NX when TDP is disabled to handle a variety of scenarios,
5515 	 * notably for huge SPTEs if iTLB multi-hit mitigation is enabled and
5516 	 * to generate correct permissions for CR0.WP=0/CR4.SMEP=1/EFER.NX=0.
5517 	 * The iTLB multi-hit workaround can be toggled at any time, so assume
5518 	 * NX can be used by any non-nested shadow MMU to avoid having to reset
5519 	 * MMU contexts.
5520 	 */
5521 	root_role.efer_nx = true;
5522 
5523 	shadow_mmu_init_context(vcpu, context, cpu_role, root_role);
5524 }
5525 
5526 void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr0,
5527 			     unsigned long cr4, u64 efer, gpa_t nested_cr3)
5528 {
5529 	struct kvm_mmu *context = &vcpu->arch.guest_mmu;
5530 	struct kvm_mmu_role_regs regs = {
5531 		.cr0 = cr0,
5532 		.cr4 = cr4 & ~X86_CR4_PKE,
5533 		.efer = efer,
5534 	};
5535 	union kvm_cpu_role cpu_role = kvm_calc_cpu_role(vcpu, &regs);
5536 	union kvm_mmu_page_role root_role;
5537 
5538 	/* NPT requires CR0.PG=1. */
5539 	WARN_ON_ONCE(cpu_role.base.direct);
5540 
5541 	root_role = cpu_role.base;
5542 	root_role.level = kvm_mmu_get_tdp_level(vcpu);
5543 	if (root_role.level == PT64_ROOT_5LEVEL &&
5544 	    cpu_role.base.level == PT64_ROOT_4LEVEL)
5545 		root_role.passthrough = 1;
5546 
5547 	shadow_mmu_init_context(vcpu, context, cpu_role, root_role);
5548 	kvm_mmu_new_pgd(vcpu, nested_cr3);
5549 }
5550 EXPORT_SYMBOL_GPL(kvm_init_shadow_npt_mmu);
5551 
5552 static union kvm_cpu_role
5553 kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty,
5554 				   bool execonly, u8 level)
5555 {
5556 	union kvm_cpu_role role = {0};
5557 
5558 	/*
5559 	 * KVM does not support SMM transfer monitors, and consequently does not
5560 	 * support the "entry to SMM" control either.  role.base.smm is always 0.
5561 	 */
5562 	WARN_ON_ONCE(is_smm(vcpu));
5563 	role.base.level = level;
5564 	role.base.has_4_byte_gpte = false;
5565 	role.base.direct = false;
5566 	role.base.ad_disabled = !accessed_dirty;
5567 	role.base.guest_mode = true;
5568 	role.base.access = ACC_ALL;
5569 
5570 	role.ext.word = 0;
5571 	role.ext.execonly = execonly;
5572 	role.ext.valid = 1;
5573 
5574 	return role;
5575 }
5576 
5577 void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
5578 			     int huge_page_level, bool accessed_dirty,
5579 			     gpa_t new_eptp)
5580 {
5581 	struct kvm_mmu *context = &vcpu->arch.guest_mmu;
5582 	u8 level = vmx_eptp_page_walk_level(new_eptp);
5583 	union kvm_cpu_role new_mode =
5584 		kvm_calc_shadow_ept_root_page_role(vcpu, accessed_dirty,
5585 						   execonly, level);
5586 
5587 	if (new_mode.as_u64 != context->cpu_role.as_u64) {
5588 		/* EPT, and thus nested EPT, does not consume CR0, CR4, nor EFER. */
5589 		context->cpu_role.as_u64 = new_mode.as_u64;
5590 		context->root_role.word = new_mode.base.word;
5591 
5592 		context->page_fault = ept_page_fault;
5593 		context->gva_to_gpa = ept_gva_to_gpa;
5594 		context->sync_spte = ept_sync_spte;
5595 
5596 		update_permission_bitmask(context, true);
5597 		context->pkru_mask = 0;
5598 		reset_rsvds_bits_mask_ept(vcpu, context, execonly, huge_page_level);
5599 		reset_ept_shadow_zero_bits_mask(context, execonly);
5600 	}
5601 
5602 	kvm_mmu_new_pgd(vcpu, new_eptp);
5603 }
5604 EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu);
5605 
5606 static void init_kvm_softmmu(struct kvm_vcpu *vcpu,
5607 			     union kvm_cpu_role cpu_role)
5608 {
5609 	struct kvm_mmu *context = &vcpu->arch.root_mmu;
5610 
5611 	kvm_init_shadow_mmu(vcpu, cpu_role);
5612 
5613 	context->get_guest_pgd     = get_guest_cr3;
5614 	context->get_pdptr         = kvm_pdptr_read;
5615 	context->inject_page_fault = kvm_inject_page_fault;
5616 }
5617 
5618 static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu,
5619 				union kvm_cpu_role new_mode)
5620 {
5621 	struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
5622 
5623 	if (new_mode.as_u64 == g_context->cpu_role.as_u64)
5624 		return;
5625 
5626 	g_context->cpu_role.as_u64   = new_mode.as_u64;
5627 	g_context->get_guest_pgd     = get_guest_cr3;
5628 	g_context->get_pdptr         = kvm_pdptr_read;
5629 	g_context->inject_page_fault = kvm_inject_page_fault;
5630 
5631 	/*
5632 	 * L2 page tables are never shadowed, so there is no need to sync
5633 	 * SPTEs.
5634 	 */
5635 	g_context->sync_spte         = NULL;
5636 
5637 	/*
5638 	 * Note that arch.mmu->gva_to_gpa translates l2_gpa to l1_gpa using
5639 	 * L1's nested page tables (e.g. EPT12). The nested translation
5640 	 * of l2_gva to l1_gpa is done by arch.nested_mmu.gva_to_gpa using
5641 	 * L2's page tables as the first level of translation and L1's
5642 	 * nested page tables as the second level of translation. Basically
5643 	 * the gva_to_gpa functions between mmu and nested_mmu are swapped.
5644 	 */
5645 	if (!is_paging(vcpu))
5646 		g_context->gva_to_gpa = nonpaging_gva_to_gpa;
5647 	else if (is_long_mode(vcpu))
5648 		g_context->gva_to_gpa = paging64_gva_to_gpa;
5649 	else if (is_pae(vcpu))
5650 		g_context->gva_to_gpa = paging64_gva_to_gpa;
5651 	else
5652 		g_context->gva_to_gpa = paging32_gva_to_gpa;
5653 
5654 	reset_guest_paging_metadata(vcpu, g_context);
5655 }
5656 
5657 void kvm_init_mmu(struct kvm_vcpu *vcpu)
5658 {
5659 	struct kvm_mmu_role_regs regs = vcpu_to_role_regs(vcpu);
5660 	union kvm_cpu_role cpu_role = kvm_calc_cpu_role(vcpu, &regs);
5661 
5662 	if (mmu_is_nested(vcpu))
5663 		init_kvm_nested_mmu(vcpu, cpu_role);
5664 	else if (tdp_enabled)
5665 		init_kvm_tdp_mmu(vcpu, cpu_role);
5666 	else
5667 		init_kvm_softmmu(vcpu, cpu_role);
5668 }
5669 EXPORT_SYMBOL_GPL(kvm_init_mmu);
5670 
5671 void kvm_mmu_after_set_cpuid(struct kvm_vcpu *vcpu)
5672 {
5673 	/*
5674 	 * Invalidate all MMU roles to force them to reinitialize as CPUID
5675 	 * information is factored into reserved bit calculations.
5676 	 *
5677 	 * Correctly handling multiple vCPU models with respect to paging and
5678 	 * physical address properties) in a single VM would require tracking
5679 	 * all relevant CPUID information in kvm_mmu_page_role. That is very
5680 	 * undesirable as it would increase the memory requirements for
5681 	 * gfn_write_track (see struct kvm_mmu_page_role comments).  For now
5682 	 * that problem is swept under the rug; KVM's CPUID API is horrific and
5683 	 * it's all but impossible to solve it without introducing a new API.
5684 	 */
5685 	vcpu->arch.root_mmu.root_role.invalid = 1;
5686 	vcpu->arch.guest_mmu.root_role.invalid = 1;
5687 	vcpu->arch.nested_mmu.root_role.invalid = 1;
5688 	vcpu->arch.root_mmu.cpu_role.ext.valid = 0;
5689 	vcpu->arch.guest_mmu.cpu_role.ext.valid = 0;
5690 	vcpu->arch.nested_mmu.cpu_role.ext.valid = 0;
5691 	kvm_mmu_reset_context(vcpu);
5692 
5693 	/*
5694 	 * Changing guest CPUID after KVM_RUN is forbidden, see the comment in
5695 	 * kvm_arch_vcpu_ioctl().
5696 	 */
5697 	KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm);
5698 }
5699 
5700 void kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
5701 {
5702 	kvm_mmu_unload(vcpu);
5703 	kvm_init_mmu(vcpu);
5704 }
5705 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
5706 
5707 int kvm_mmu_load(struct kvm_vcpu *vcpu)
5708 {
5709 	int r;
5710 
5711 	r = mmu_topup_memory_caches(vcpu, !vcpu->arch.mmu->root_role.direct);
5712 	if (r)
5713 		goto out;
5714 	r = mmu_alloc_special_roots(vcpu);
5715 	if (r)
5716 		goto out;
5717 	if (vcpu->arch.mmu->root_role.direct)
5718 		r = mmu_alloc_direct_roots(vcpu);
5719 	else
5720 		r = mmu_alloc_shadow_roots(vcpu);
5721 	if (r)
5722 		goto out;
5723 
5724 	kvm_mmu_sync_roots(vcpu);
5725 
5726 	kvm_mmu_load_pgd(vcpu);
5727 
5728 	/*
5729 	 * Flush any TLB entries for the new root, the provenance of the root
5730 	 * is unknown.  Even if KVM ensures there are no stale TLB entries
5731 	 * for a freed root, in theory another hypervisor could have left
5732 	 * stale entries.  Flushing on alloc also allows KVM to skip the TLB
5733 	 * flush when freeing a root (see kvm_tdp_mmu_put_root()).
5734 	 */
5735 	kvm_x86_call(flush_tlb_current)(vcpu);
5736 out:
5737 	return r;
5738 }
5739 
5740 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
5741 {
5742 	struct kvm *kvm = vcpu->kvm;
5743 
5744 	kvm_mmu_free_roots(kvm, &vcpu->arch.root_mmu, KVM_MMU_ROOTS_ALL);
5745 	WARN_ON_ONCE(VALID_PAGE(vcpu->arch.root_mmu.root.hpa));
5746 	kvm_mmu_free_roots(kvm, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
5747 	WARN_ON_ONCE(VALID_PAGE(vcpu->arch.guest_mmu.root.hpa));
5748 	vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
5749 }
5750 
5751 static bool is_obsolete_root(struct kvm *kvm, hpa_t root_hpa)
5752 {
5753 	struct kvm_mmu_page *sp;
5754 
5755 	if (!VALID_PAGE(root_hpa))
5756 		return false;
5757 
5758 	/*
5759 	 * When freeing obsolete roots, treat roots as obsolete if they don't
5760 	 * have an associated shadow page, as it's impossible to determine if
5761 	 * such roots are fresh or stale.  This does mean KVM will get false
5762 	 * positives and free roots that don't strictly need to be freed, but
5763 	 * such false positives are relatively rare:
5764 	 *
5765 	 *  (a) only PAE paging and nested NPT have roots without shadow pages
5766 	 *      (or any shadow paging flavor with a dummy root, see note below)
5767 	 *  (b) remote reloads due to a memslot update obsoletes _all_ roots
5768 	 *  (c) KVM doesn't track previous roots for PAE paging, and the guest
5769 	 *      is unlikely to zap an in-use PGD.
5770 	 *
5771 	 * Note!  Dummy roots are unique in that they are obsoleted by memslot
5772 	 * _creation_!  See also FNAME(fetch).
5773 	 */
5774 	sp = root_to_sp(root_hpa);
5775 	return !sp || is_obsolete_sp(kvm, sp);
5776 }
5777 
5778 static void __kvm_mmu_free_obsolete_roots(struct kvm *kvm, struct kvm_mmu *mmu)
5779 {
5780 	unsigned long roots_to_free = 0;
5781 	int i;
5782 
5783 	if (is_obsolete_root(kvm, mmu->root.hpa))
5784 		roots_to_free |= KVM_MMU_ROOT_CURRENT;
5785 
5786 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5787 		if (is_obsolete_root(kvm, mmu->prev_roots[i].hpa))
5788 			roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5789 	}
5790 
5791 	if (roots_to_free)
5792 		kvm_mmu_free_roots(kvm, mmu, roots_to_free);
5793 }
5794 
5795 void kvm_mmu_free_obsolete_roots(struct kvm_vcpu *vcpu)
5796 {
5797 	__kvm_mmu_free_obsolete_roots(vcpu->kvm, &vcpu->arch.root_mmu);
5798 	__kvm_mmu_free_obsolete_roots(vcpu->kvm, &vcpu->arch.guest_mmu);
5799 }
5800 
5801 static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
5802 				    int *bytes)
5803 {
5804 	u64 gentry = 0;
5805 	int r;
5806 
5807 	/*
5808 	 * Assume that the pte write on a page table of the same type
5809 	 * as the current vcpu paging mode since we update the sptes only
5810 	 * when they have the same mode.
5811 	 */
5812 	if (is_pae(vcpu) && *bytes == 4) {
5813 		/* Handle a 32-bit guest writing two halves of a 64-bit gpte */
5814 		*gpa &= ~(gpa_t)7;
5815 		*bytes = 8;
5816 	}
5817 
5818 	if (*bytes == 4 || *bytes == 8) {
5819 		r = kvm_vcpu_read_guest_atomic(vcpu, *gpa, &gentry, *bytes);
5820 		if (r)
5821 			gentry = 0;
5822 	}
5823 
5824 	return gentry;
5825 }
5826 
5827 /*
5828  * If we're seeing too many writes to a page, it may no longer be a page table,
5829  * or we may be forking, in which case it is better to unmap the page.
5830  */
5831 static bool detect_write_flooding(struct kvm_mmu_page *sp)
5832 {
5833 	/*
5834 	 * Skip write-flooding detected for the sp whose level is 1, because
5835 	 * it can become unsync, then the guest page is not write-protected.
5836 	 */
5837 	if (sp->role.level == PG_LEVEL_4K)
5838 		return false;
5839 
5840 	atomic_inc(&sp->write_flooding_count);
5841 	return atomic_read(&sp->write_flooding_count) >= 3;
5842 }
5843 
5844 /*
5845  * Misaligned accesses are too much trouble to fix up; also, they usually
5846  * indicate a page is not used as a page table.
5847  */
5848 static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
5849 				    int bytes)
5850 {
5851 	unsigned offset, pte_size, misaligned;
5852 
5853 	offset = offset_in_page(gpa);
5854 	pte_size = sp->role.has_4_byte_gpte ? 4 : 8;
5855 
5856 	/*
5857 	 * Sometimes, the OS only writes the last one bytes to update status
5858 	 * bits, for example, in linux, andb instruction is used in clear_bit().
5859 	 */
5860 	if (!(offset & (pte_size - 1)) && bytes == 1)
5861 		return false;
5862 
5863 	misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
5864 	misaligned |= bytes < 4;
5865 
5866 	return misaligned;
5867 }
5868 
5869 static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
5870 {
5871 	unsigned page_offset, quadrant;
5872 	u64 *spte;
5873 	int level;
5874 
5875 	page_offset = offset_in_page(gpa);
5876 	level = sp->role.level;
5877 	*nspte = 1;
5878 	if (sp->role.has_4_byte_gpte) {
5879 		page_offset <<= 1;	/* 32->64 */
5880 		/*
5881 		 * A 32-bit pde maps 4MB while the shadow pdes map
5882 		 * only 2MB.  So we need to double the offset again
5883 		 * and zap two pdes instead of one.
5884 		 */
5885 		if (level == PT32_ROOT_LEVEL) {
5886 			page_offset &= ~7; /* kill rounding error */
5887 			page_offset <<= 1;
5888 			*nspte = 2;
5889 		}
5890 		quadrant = page_offset >> PAGE_SHIFT;
5891 		page_offset &= ~PAGE_MASK;
5892 		if (quadrant != sp->role.quadrant)
5893 			return NULL;
5894 	}
5895 
5896 	spte = &sp->spt[page_offset / sizeof(*spte)];
5897 	return spte;
5898 }
5899 
5900 void kvm_mmu_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
5901 			 int bytes)
5902 {
5903 	gfn_t gfn = gpa >> PAGE_SHIFT;
5904 	struct kvm_mmu_page *sp;
5905 	LIST_HEAD(invalid_list);
5906 	u64 entry, gentry, *spte;
5907 	int npte;
5908 	bool flush = false;
5909 
5910 	/*
5911 	 * When emulating guest writes, ensure the written value is visible to
5912 	 * any task that is handling page faults before checking whether or not
5913 	 * KVM is shadowing a guest PTE.  This ensures either KVM will create
5914 	 * the correct SPTE in the page fault handler, or this task will see
5915 	 * a non-zero indirect_shadow_pages.  Pairs with the smp_mb() in
5916 	 * account_shadowed().
5917 	 */
5918 	smp_mb();
5919 	if (!vcpu->kvm->arch.indirect_shadow_pages)
5920 		return;
5921 
5922 	write_lock(&vcpu->kvm->mmu_lock);
5923 
5924 	gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, &bytes);
5925 
5926 	++vcpu->kvm->stat.mmu_pte_write;
5927 
5928 	for_each_gfn_valid_sp_with_gptes(vcpu->kvm, sp, gfn) {
5929 		if (detect_write_misaligned(sp, gpa, bytes) ||
5930 		      detect_write_flooding(sp)) {
5931 			kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
5932 			++vcpu->kvm->stat.mmu_flooded;
5933 			continue;
5934 		}
5935 
5936 		spte = get_written_sptes(sp, gpa, &npte);
5937 		if (!spte)
5938 			continue;
5939 
5940 		while (npte--) {
5941 			entry = *spte;
5942 			mmu_page_zap_pte(vcpu->kvm, sp, spte, NULL);
5943 			if (gentry && sp->role.level != PG_LEVEL_4K)
5944 				++vcpu->kvm->stat.mmu_pde_zapped;
5945 			if (is_shadow_present_pte(entry))
5946 				flush = true;
5947 			++spte;
5948 		}
5949 	}
5950 	kvm_mmu_remote_flush_or_zap(vcpu->kvm, &invalid_list, flush);
5951 	write_unlock(&vcpu->kvm->mmu_lock);
5952 }
5953 
5954 static bool is_write_to_guest_page_table(u64 error_code)
5955 {
5956 	const u64 mask = PFERR_GUEST_PAGE_MASK | PFERR_WRITE_MASK | PFERR_PRESENT_MASK;
5957 
5958 	return (error_code & mask) == mask;
5959 }
5960 
5961 static int kvm_mmu_write_protect_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
5962 				       u64 error_code, int *emulation_type)
5963 {
5964 	bool direct = vcpu->arch.mmu->root_role.direct;
5965 
5966 	/*
5967 	 * Do not try to unprotect and retry if the vCPU re-faulted on the same
5968 	 * RIP with the same address that was previously unprotected, as doing
5969 	 * so will likely put the vCPU into an infinite.  E.g. if the vCPU uses
5970 	 * a non-page-table modifying instruction on the PDE that points to the
5971 	 * instruction, then unprotecting the gfn will unmap the instruction's
5972 	 * code, i.e. make it impossible for the instruction to ever complete.
5973 	 */
5974 	if (vcpu->arch.last_retry_eip == kvm_rip_read(vcpu) &&
5975 	    vcpu->arch.last_retry_addr == cr2_or_gpa)
5976 		return RET_PF_EMULATE;
5977 
5978 	/*
5979 	 * Reset the unprotect+retry values that guard against infinite loops.
5980 	 * The values will be refreshed if KVM explicitly unprotects a gfn and
5981 	 * retries, in all other cases it's safe to retry in the future even if
5982 	 * the next page fault happens on the same RIP+address.
5983 	 */
5984 	vcpu->arch.last_retry_eip = 0;
5985 	vcpu->arch.last_retry_addr = 0;
5986 
5987 	/*
5988 	 * It should be impossible to reach this point with an MMIO cache hit,
5989 	 * as RET_PF_WRITE_PROTECTED is returned if and only if there's a valid,
5990 	 * writable memslot, and creating a memslot should invalidate the MMIO
5991 	 * cache by way of changing the memslot generation.  WARN and disallow
5992 	 * retry if MMIO is detected, as retrying MMIO emulation is pointless
5993 	 * and could put the vCPU into an infinite loop because the processor
5994 	 * will keep faulting on the non-existent MMIO address.
5995 	 */
5996 	if (WARN_ON_ONCE(mmio_info_in_cache(vcpu, cr2_or_gpa, direct)))
5997 		return RET_PF_EMULATE;
5998 
5999 	/*
6000 	 * Before emulating the instruction, check to see if the access was due
6001 	 * to a read-only violation while the CPU was walking non-nested NPT
6002 	 * page tables, i.e. for a direct MMU, for _guest_ page tables in L1.
6003 	 * If L1 is sharing (a subset of) its page tables with L2, e.g. by
6004 	 * having nCR3 share lower level page tables with hCR3, then when KVM
6005 	 * (L0) write-protects the nested NPTs, i.e. npt12 entries, KVM is also
6006 	 * unknowingly write-protecting L1's guest page tables, which KVM isn't
6007 	 * shadowing.
6008 	 *
6009 	 * Because the CPU (by default) walks NPT page tables using a write
6010 	 * access (to ensure the CPU can do A/D updates), page walks in L1 can
6011 	 * trigger write faults for the above case even when L1 isn't modifying
6012 	 * PTEs.  As a result, KVM will unnecessarily emulate (or at least, try
6013 	 * to emulate) an excessive number of L1 instructions; because L1's MMU
6014 	 * isn't shadowed by KVM, there is no need to write-protect L1's gPTEs
6015 	 * and thus no need to emulate in order to guarantee forward progress.
6016 	 *
6017 	 * Try to unprotect the gfn, i.e. zap any shadow pages, so that L1 can
6018 	 * proceed without triggering emulation.  If one or more shadow pages
6019 	 * was zapped, skip emulation and resume L1 to let it natively execute
6020 	 * the instruction.  If no shadow pages were zapped, then the write-
6021 	 * fault is due to something else entirely, i.e. KVM needs to emulate,
6022 	 * as resuming the guest will put it into an infinite loop.
6023 	 *
6024 	 * Note, this code also applies to Intel CPUs, even though it is *very*
6025 	 * unlikely that an L1 will share its page tables (IA32/PAE/paging64
6026 	 * format) with L2's page tables (EPT format).
6027 	 *
6028 	 * For indirect MMUs, i.e. if KVM is shadowing the current MMU, try to
6029 	 * unprotect the gfn and retry if an event is awaiting reinjection.  If
6030 	 * KVM emulates multiple instructions before completing event injection,
6031 	 * the event could be delayed beyond what is architecturally allowed,
6032 	 * e.g. KVM could inject an IRQ after the TPR has been raised.
6033 	 */
6034 	if (((direct && is_write_to_guest_page_table(error_code)) ||
6035 	     (!direct && kvm_event_needs_reinjection(vcpu))) &&
6036 	    kvm_mmu_unprotect_gfn_and_retry(vcpu, cr2_or_gpa))
6037 		return RET_PF_RETRY;
6038 
6039 	/*
6040 	 * The gfn is write-protected, but if KVM detects its emulating an
6041 	 * instruction that is unlikely to be used to modify page tables, or if
6042 	 * emulation fails, KVM can try to unprotect the gfn and let the CPU
6043 	 * re-execute the instruction that caused the page fault.  Do not allow
6044 	 * retrying an instruction from a nested guest as KVM is only explicitly
6045 	 * shadowing L1's page tables, i.e. unprotecting something for L1 isn't
6046 	 * going to magically fix whatever issue caused L2 to fail.
6047 	 */
6048 	if (!is_guest_mode(vcpu))
6049 		*emulation_type |= EMULTYPE_ALLOW_RETRY_PF;
6050 
6051 	return RET_PF_EMULATE;
6052 }
6053 
6054 int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
6055 		       void *insn, int insn_len)
6056 {
6057 	int r, emulation_type = EMULTYPE_PF;
6058 	bool direct = vcpu->arch.mmu->root_role.direct;
6059 
6060 	if (WARN_ON_ONCE(!VALID_PAGE(vcpu->arch.mmu->root.hpa)))
6061 		return RET_PF_RETRY;
6062 
6063 	/*
6064 	 * Except for reserved faults (emulated MMIO is shared-only), set the
6065 	 * PFERR_PRIVATE_ACCESS flag for software-protected VMs based on the gfn's
6066 	 * current attributes, which are the source of truth for such VMs.  Note,
6067 	 * this wrong for nested MMUs as the GPA is an L2 GPA, but KVM doesn't
6068 	 * currently supported nested virtualization (among many other things)
6069 	 * for software-protected VMs.
6070 	 */
6071 	if (IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) &&
6072 	    !(error_code & PFERR_RSVD_MASK) &&
6073 	    vcpu->kvm->arch.vm_type == KVM_X86_SW_PROTECTED_VM &&
6074 	    kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(cr2_or_gpa)))
6075 		error_code |= PFERR_PRIVATE_ACCESS;
6076 
6077 	r = RET_PF_INVALID;
6078 	if (unlikely(error_code & PFERR_RSVD_MASK)) {
6079 		if (WARN_ON_ONCE(error_code & PFERR_PRIVATE_ACCESS))
6080 			return -EFAULT;
6081 
6082 		r = handle_mmio_page_fault(vcpu, cr2_or_gpa, direct);
6083 		if (r == RET_PF_EMULATE)
6084 			goto emulate;
6085 	}
6086 
6087 	if (r == RET_PF_INVALID) {
6088 		vcpu->stat.pf_taken++;
6089 
6090 		r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, error_code, false,
6091 					  &emulation_type, NULL);
6092 		if (KVM_BUG_ON(r == RET_PF_INVALID, vcpu->kvm))
6093 			return -EIO;
6094 	}
6095 
6096 	if (r < 0)
6097 		return r;
6098 
6099 	if (r == RET_PF_WRITE_PROTECTED)
6100 		r = kvm_mmu_write_protect_fault(vcpu, cr2_or_gpa, error_code,
6101 						&emulation_type);
6102 
6103 	if (r == RET_PF_FIXED)
6104 		vcpu->stat.pf_fixed++;
6105 	else if (r == RET_PF_EMULATE)
6106 		vcpu->stat.pf_emulate++;
6107 	else if (r == RET_PF_SPURIOUS)
6108 		vcpu->stat.pf_spurious++;
6109 
6110 	if (r != RET_PF_EMULATE)
6111 		return 1;
6112 
6113 emulate:
6114 	return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, insn,
6115 				       insn_len);
6116 }
6117 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
6118 
6119 void kvm_mmu_print_sptes(struct kvm_vcpu *vcpu, gpa_t gpa, const char *msg)
6120 {
6121 	u64 sptes[PT64_ROOT_MAX_LEVEL + 1];
6122 	int root_level, leaf, level;
6123 
6124 	leaf = get_sptes_lockless(vcpu, gpa, sptes, &root_level);
6125 	if (unlikely(leaf < 0))
6126 		return;
6127 
6128 	pr_err("%s %llx", msg, gpa);
6129 	for (level = root_level; level >= leaf; level--)
6130 		pr_cont(", spte[%d] = 0x%llx", level, sptes[level]);
6131 	pr_cont("\n");
6132 }
6133 EXPORT_SYMBOL_GPL(kvm_mmu_print_sptes);
6134 
6135 static void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
6136 				      u64 addr, hpa_t root_hpa)
6137 {
6138 	struct kvm_shadow_walk_iterator iterator;
6139 
6140 	vcpu_clear_mmio_info(vcpu, addr);
6141 
6142 	/*
6143 	 * Walking and synchronizing SPTEs both assume they are operating in
6144 	 * the context of the current MMU, and would need to be reworked if
6145 	 * this is ever used to sync the guest_mmu, e.g. to emulate INVEPT.
6146 	 */
6147 	if (WARN_ON_ONCE(mmu != vcpu->arch.mmu))
6148 		return;
6149 
6150 	if (!VALID_PAGE(root_hpa))
6151 		return;
6152 
6153 	write_lock(&vcpu->kvm->mmu_lock);
6154 	for_each_shadow_entry_using_root(vcpu, root_hpa, addr, iterator) {
6155 		struct kvm_mmu_page *sp = sptep_to_sp(iterator.sptep);
6156 
6157 		if (sp->unsync) {
6158 			int ret = kvm_sync_spte(vcpu, sp, iterator.index);
6159 
6160 			if (ret < 0)
6161 				mmu_page_zap_pte(vcpu->kvm, sp, iterator.sptep, NULL);
6162 			if (ret)
6163 				kvm_flush_remote_tlbs_sptep(vcpu->kvm, iterator.sptep);
6164 		}
6165 
6166 		if (!sp->unsync_children)
6167 			break;
6168 	}
6169 	write_unlock(&vcpu->kvm->mmu_lock);
6170 }
6171 
6172 void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
6173 			     u64 addr, unsigned long roots)
6174 {
6175 	int i;
6176 
6177 	WARN_ON_ONCE(roots & ~KVM_MMU_ROOTS_ALL);
6178 
6179 	/* It's actually a GPA for vcpu->arch.guest_mmu.  */
6180 	if (mmu != &vcpu->arch.guest_mmu) {
6181 		/* INVLPG on a non-canonical address is a NOP according to the SDM.  */
6182 		if (is_noncanonical_invlpg_address(addr, vcpu))
6183 			return;
6184 
6185 		kvm_x86_call(flush_tlb_gva)(vcpu, addr);
6186 	}
6187 
6188 	if (!mmu->sync_spte)
6189 		return;
6190 
6191 	if (roots & KVM_MMU_ROOT_CURRENT)
6192 		__kvm_mmu_invalidate_addr(vcpu, mmu, addr, mmu->root.hpa);
6193 
6194 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
6195 		if (roots & KVM_MMU_ROOT_PREVIOUS(i))
6196 			__kvm_mmu_invalidate_addr(vcpu, mmu, addr, mmu->prev_roots[i].hpa);
6197 	}
6198 }
6199 EXPORT_SYMBOL_GPL(kvm_mmu_invalidate_addr);
6200 
6201 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
6202 {
6203 	/*
6204 	 * INVLPG is required to invalidate any global mappings for the VA,
6205 	 * irrespective of PCID.  Blindly sync all roots as it would take
6206 	 * roughly the same amount of work/time to determine whether any of the
6207 	 * previous roots have a global mapping.
6208 	 *
6209 	 * Mappings not reachable via the current or previous cached roots will
6210 	 * be synced when switching to that new cr3, so nothing needs to be
6211 	 * done here for them.
6212 	 */
6213 	kvm_mmu_invalidate_addr(vcpu, vcpu->arch.walk_mmu, gva, KVM_MMU_ROOTS_ALL);
6214 	++vcpu->stat.invlpg;
6215 }
6216 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
6217 
6218 
6219 void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid)
6220 {
6221 	struct kvm_mmu *mmu = vcpu->arch.mmu;
6222 	unsigned long roots = 0;
6223 	uint i;
6224 
6225 	if (pcid == kvm_get_active_pcid(vcpu))
6226 		roots |= KVM_MMU_ROOT_CURRENT;
6227 
6228 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
6229 		if (VALID_PAGE(mmu->prev_roots[i].hpa) &&
6230 		    pcid == kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd))
6231 			roots |= KVM_MMU_ROOT_PREVIOUS(i);
6232 	}
6233 
6234 	if (roots)
6235 		kvm_mmu_invalidate_addr(vcpu, mmu, gva, roots);
6236 	++vcpu->stat.invlpg;
6237 
6238 	/*
6239 	 * Mappings not reachable via the current cr3 or the prev_roots will be
6240 	 * synced when switching to that cr3, so nothing needs to be done here
6241 	 * for them.
6242 	 */
6243 }
6244 
6245 void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
6246 		       int tdp_max_root_level, int tdp_huge_page_level)
6247 {
6248 	tdp_enabled = enable_tdp;
6249 	tdp_root_level = tdp_forced_root_level;
6250 	max_tdp_level = tdp_max_root_level;
6251 
6252 #ifdef CONFIG_X86_64
6253 	tdp_mmu_enabled = tdp_mmu_allowed && tdp_enabled;
6254 #endif
6255 	/*
6256 	 * max_huge_page_level reflects KVM's MMU capabilities irrespective
6257 	 * of kernel support, e.g. KVM may be capable of using 1GB pages when
6258 	 * the kernel is not.  But, KVM never creates a page size greater than
6259 	 * what is used by the kernel for any given HVA, i.e. the kernel's
6260 	 * capabilities are ultimately consulted by kvm_mmu_hugepage_adjust().
6261 	 */
6262 	if (tdp_enabled)
6263 		max_huge_page_level = tdp_huge_page_level;
6264 	else if (boot_cpu_has(X86_FEATURE_GBPAGES))
6265 		max_huge_page_level = PG_LEVEL_1G;
6266 	else
6267 		max_huge_page_level = PG_LEVEL_2M;
6268 }
6269 EXPORT_SYMBOL_GPL(kvm_configure_mmu);
6270 
6271 static void free_mmu_pages(struct kvm_mmu *mmu)
6272 {
6273 	if (!tdp_enabled && mmu->pae_root)
6274 		set_memory_encrypted((unsigned long)mmu->pae_root, 1);
6275 	free_page((unsigned long)mmu->pae_root);
6276 	free_page((unsigned long)mmu->pml4_root);
6277 	free_page((unsigned long)mmu->pml5_root);
6278 }
6279 
6280 static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
6281 {
6282 	struct page *page;
6283 	int i;
6284 
6285 	mmu->root.hpa = INVALID_PAGE;
6286 	mmu->root.pgd = 0;
6287 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
6288 		mmu->prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID;
6289 
6290 	/* vcpu->arch.guest_mmu isn't used when !tdp_enabled. */
6291 	if (!tdp_enabled && mmu == &vcpu->arch.guest_mmu)
6292 		return 0;
6293 
6294 	/*
6295 	 * When using PAE paging, the four PDPTEs are treated as 'root' pages,
6296 	 * while the PDP table is a per-vCPU construct that's allocated at MMU
6297 	 * creation.  When emulating 32-bit mode, cr3 is only 32 bits even on
6298 	 * x86_64.  Therefore we need to allocate the PDP table in the first
6299 	 * 4GB of memory, which happens to fit the DMA32 zone.  TDP paging
6300 	 * generally doesn't use PAE paging and can skip allocating the PDP
6301 	 * table.  The main exception, handled here, is SVM's 32-bit NPT.  The
6302 	 * other exception is for shadowing L1's 32-bit or PAE NPT on 64-bit
6303 	 * KVM; that horror is handled on-demand by mmu_alloc_special_roots().
6304 	 */
6305 	if (tdp_enabled && kvm_mmu_get_tdp_level(vcpu) > PT32E_ROOT_LEVEL)
6306 		return 0;
6307 
6308 	page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_DMA32);
6309 	if (!page)
6310 		return -ENOMEM;
6311 
6312 	mmu->pae_root = page_address(page);
6313 
6314 	/*
6315 	 * CR3 is only 32 bits when PAE paging is used, thus it's impossible to
6316 	 * get the CPU to treat the PDPTEs as encrypted.  Decrypt the page so
6317 	 * that KVM's writes and the CPU's reads get along.  Note, this is
6318 	 * only necessary when using shadow paging, as 64-bit NPT can get at
6319 	 * the C-bit even when shadowing 32-bit NPT, and SME isn't supported
6320 	 * by 32-bit kernels (when KVM itself uses 32-bit NPT).
6321 	 */
6322 	if (!tdp_enabled)
6323 		set_memory_decrypted((unsigned long)mmu->pae_root, 1);
6324 	else
6325 		WARN_ON_ONCE(shadow_me_value);
6326 
6327 	for (i = 0; i < 4; ++i)
6328 		mmu->pae_root[i] = INVALID_PAE_ROOT;
6329 
6330 	return 0;
6331 }
6332 
6333 int kvm_mmu_create(struct kvm_vcpu *vcpu)
6334 {
6335 	int ret;
6336 
6337 	vcpu->arch.mmu_pte_list_desc_cache.kmem_cache = pte_list_desc_cache;
6338 	vcpu->arch.mmu_pte_list_desc_cache.gfp_zero = __GFP_ZERO;
6339 
6340 	vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache;
6341 	vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO;
6342 
6343 	vcpu->arch.mmu_shadow_page_cache.init_value =
6344 		SHADOW_NONPRESENT_VALUE;
6345 	if (!vcpu->arch.mmu_shadow_page_cache.init_value)
6346 		vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO;
6347 
6348 	vcpu->arch.mmu = &vcpu->arch.root_mmu;
6349 	vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
6350 
6351 	ret = __kvm_mmu_create(vcpu, &vcpu->arch.guest_mmu);
6352 	if (ret)
6353 		return ret;
6354 
6355 	ret = __kvm_mmu_create(vcpu, &vcpu->arch.root_mmu);
6356 	if (ret)
6357 		goto fail_allocate_root;
6358 
6359 	return ret;
6360  fail_allocate_root:
6361 	free_mmu_pages(&vcpu->arch.guest_mmu);
6362 	return ret;
6363 }
6364 
6365 #define BATCH_ZAP_PAGES	10
6366 static void kvm_zap_obsolete_pages(struct kvm *kvm)
6367 {
6368 	struct kvm_mmu_page *sp, *node;
6369 	int nr_zapped, batch = 0;
6370 	LIST_HEAD(invalid_list);
6371 	bool unstable;
6372 
6373 	lockdep_assert_held(&kvm->slots_lock);
6374 
6375 restart:
6376 	list_for_each_entry_safe_reverse(sp, node,
6377 	      &kvm->arch.active_mmu_pages, link) {
6378 		/*
6379 		 * No obsolete valid page exists before a newly created page
6380 		 * since active_mmu_pages is a FIFO list.
6381 		 */
6382 		if (!is_obsolete_sp(kvm, sp))
6383 			break;
6384 
6385 		/*
6386 		 * Invalid pages should never land back on the list of active
6387 		 * pages.  Skip the bogus page, otherwise we'll get stuck in an
6388 		 * infinite loop if the page gets put back on the list (again).
6389 		 */
6390 		if (WARN_ON_ONCE(sp->role.invalid))
6391 			continue;
6392 
6393 		/*
6394 		 * No need to flush the TLB since we're only zapping shadow
6395 		 * pages with an obsolete generation number and all vCPUS have
6396 		 * loaded a new root, i.e. the shadow pages being zapped cannot
6397 		 * be in active use by the guest.
6398 		 */
6399 		if (batch >= BATCH_ZAP_PAGES &&
6400 		    cond_resched_rwlock_write(&kvm->mmu_lock)) {
6401 			batch = 0;
6402 			goto restart;
6403 		}
6404 
6405 		unstable = __kvm_mmu_prepare_zap_page(kvm, sp,
6406 				&invalid_list, &nr_zapped);
6407 		batch += nr_zapped;
6408 
6409 		if (unstable)
6410 			goto restart;
6411 	}
6412 
6413 	/*
6414 	 * Kick all vCPUs (via remote TLB flush) before freeing the page tables
6415 	 * to ensure KVM is not in the middle of a lockless shadow page table
6416 	 * walk, which may reference the pages.  The remote TLB flush itself is
6417 	 * not required and is simply a convenient way to kick vCPUs as needed.
6418 	 * KVM performs a local TLB flush when allocating a new root (see
6419 	 * kvm_mmu_load()), and the reload in the caller ensure no vCPUs are
6420 	 * running with an obsolete MMU.
6421 	 */
6422 	kvm_mmu_commit_zap_page(kvm, &invalid_list);
6423 }
6424 
6425 /*
6426  * Fast invalidate all shadow pages and use lock-break technique
6427  * to zap obsolete pages.
6428  *
6429  * It's required when memslot is being deleted or VM is being
6430  * destroyed, in these cases, we should ensure that KVM MMU does
6431  * not use any resource of the being-deleted slot or all slots
6432  * after calling the function.
6433  */
6434 static void kvm_mmu_zap_all_fast(struct kvm *kvm)
6435 {
6436 	lockdep_assert_held(&kvm->slots_lock);
6437 
6438 	write_lock(&kvm->mmu_lock);
6439 	trace_kvm_mmu_zap_all_fast(kvm);
6440 
6441 	/*
6442 	 * Toggle mmu_valid_gen between '0' and '1'.  Because slots_lock is
6443 	 * held for the entire duration of zapping obsolete pages, it's
6444 	 * impossible for there to be multiple invalid generations associated
6445 	 * with *valid* shadow pages at any given time, i.e. there is exactly
6446 	 * one valid generation and (at most) one invalid generation.
6447 	 */
6448 	kvm->arch.mmu_valid_gen = kvm->arch.mmu_valid_gen ? 0 : 1;
6449 
6450 	/*
6451 	 * In order to ensure all vCPUs drop their soon-to-be invalid roots,
6452 	 * invalidating TDP MMU roots must be done while holding mmu_lock for
6453 	 * write and in the same critical section as making the reload request,
6454 	 * e.g. before kvm_zap_obsolete_pages() could drop mmu_lock and yield.
6455 	 */
6456 	if (tdp_mmu_enabled)
6457 		kvm_tdp_mmu_invalidate_all_roots(kvm);
6458 
6459 	/*
6460 	 * Notify all vcpus to reload its shadow page table and flush TLB.
6461 	 * Then all vcpus will switch to new shadow page table with the new
6462 	 * mmu_valid_gen.
6463 	 *
6464 	 * Note: we need to do this under the protection of mmu_lock,
6465 	 * otherwise, vcpu would purge shadow page but miss tlb flush.
6466 	 */
6467 	kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_FREE_OBSOLETE_ROOTS);
6468 
6469 	kvm_zap_obsolete_pages(kvm);
6470 
6471 	write_unlock(&kvm->mmu_lock);
6472 
6473 	/*
6474 	 * Zap the invalidated TDP MMU roots, all SPTEs must be dropped before
6475 	 * returning to the caller, e.g. if the zap is in response to a memslot
6476 	 * deletion, mmu_notifier callbacks will be unable to reach the SPTEs
6477 	 * associated with the deleted memslot once the update completes, and
6478 	 * Deferring the zap until the final reference to the root is put would
6479 	 * lead to use-after-free.
6480 	 */
6481 	if (tdp_mmu_enabled)
6482 		kvm_tdp_mmu_zap_invalidated_roots(kvm);
6483 }
6484 
6485 void kvm_mmu_init_vm(struct kvm *kvm)
6486 {
6487 	kvm->arch.shadow_mmio_value = shadow_mmio_value;
6488 	INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
6489 	INIT_LIST_HEAD(&kvm->arch.possible_nx_huge_pages);
6490 	spin_lock_init(&kvm->arch.mmu_unsync_pages_lock);
6491 
6492 	if (tdp_mmu_enabled)
6493 		kvm_mmu_init_tdp_mmu(kvm);
6494 
6495 	kvm->arch.split_page_header_cache.kmem_cache = mmu_page_header_cache;
6496 	kvm->arch.split_page_header_cache.gfp_zero = __GFP_ZERO;
6497 
6498 	kvm->arch.split_shadow_page_cache.gfp_zero = __GFP_ZERO;
6499 
6500 	kvm->arch.split_desc_cache.kmem_cache = pte_list_desc_cache;
6501 	kvm->arch.split_desc_cache.gfp_zero = __GFP_ZERO;
6502 }
6503 
6504 static void mmu_free_vm_memory_caches(struct kvm *kvm)
6505 {
6506 	kvm_mmu_free_memory_cache(&kvm->arch.split_desc_cache);
6507 	kvm_mmu_free_memory_cache(&kvm->arch.split_page_header_cache);
6508 	kvm_mmu_free_memory_cache(&kvm->arch.split_shadow_page_cache);
6509 }
6510 
6511 void kvm_mmu_uninit_vm(struct kvm *kvm)
6512 {
6513 	if (tdp_mmu_enabled)
6514 		kvm_mmu_uninit_tdp_mmu(kvm);
6515 
6516 	mmu_free_vm_memory_caches(kvm);
6517 }
6518 
6519 static bool kvm_rmap_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
6520 {
6521 	const struct kvm_memory_slot *memslot;
6522 	struct kvm_memslots *slots;
6523 	struct kvm_memslot_iter iter;
6524 	bool flush = false;
6525 	gfn_t start, end;
6526 	int i;
6527 
6528 	if (!kvm_memslots_have_rmaps(kvm))
6529 		return flush;
6530 
6531 	for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
6532 		slots = __kvm_memslots(kvm, i);
6533 
6534 		kvm_for_each_memslot_in_gfn_range(&iter, slots, gfn_start, gfn_end) {
6535 			memslot = iter.slot;
6536 			start = max(gfn_start, memslot->base_gfn);
6537 			end = min(gfn_end, memslot->base_gfn + memslot->npages);
6538 			if (WARN_ON_ONCE(start >= end))
6539 				continue;
6540 
6541 			flush = __kvm_rmap_zap_gfn_range(kvm, memslot, start,
6542 							 end, true, flush);
6543 		}
6544 	}
6545 
6546 	return flush;
6547 }
6548 
6549 /*
6550  * Invalidate (zap) SPTEs that cover GFNs from gfn_start and up to gfn_end
6551  * (not including it)
6552  */
6553 void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
6554 {
6555 	bool flush;
6556 
6557 	if (WARN_ON_ONCE(gfn_end <= gfn_start))
6558 		return;
6559 
6560 	write_lock(&kvm->mmu_lock);
6561 
6562 	kvm_mmu_invalidate_begin(kvm);
6563 
6564 	kvm_mmu_invalidate_range_add(kvm, gfn_start, gfn_end);
6565 
6566 	flush = kvm_rmap_zap_gfn_range(kvm, gfn_start, gfn_end);
6567 
6568 	if (tdp_mmu_enabled)
6569 		flush = kvm_tdp_mmu_zap_leafs(kvm, gfn_start, gfn_end, flush);
6570 
6571 	if (flush)
6572 		kvm_flush_remote_tlbs_range(kvm, gfn_start, gfn_end - gfn_start);
6573 
6574 	kvm_mmu_invalidate_end(kvm);
6575 
6576 	write_unlock(&kvm->mmu_lock);
6577 }
6578 
6579 static bool slot_rmap_write_protect(struct kvm *kvm,
6580 				    struct kvm_rmap_head *rmap_head,
6581 				    const struct kvm_memory_slot *slot)
6582 {
6583 	return rmap_write_protect(rmap_head, false);
6584 }
6585 
6586 void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
6587 				      const struct kvm_memory_slot *memslot,
6588 				      int start_level)
6589 {
6590 	if (kvm_memslots_have_rmaps(kvm)) {
6591 		write_lock(&kvm->mmu_lock);
6592 		walk_slot_rmaps(kvm, memslot, slot_rmap_write_protect,
6593 				start_level, KVM_MAX_HUGEPAGE_LEVEL, false);
6594 		write_unlock(&kvm->mmu_lock);
6595 	}
6596 
6597 	if (tdp_mmu_enabled) {
6598 		read_lock(&kvm->mmu_lock);
6599 		kvm_tdp_mmu_wrprot_slot(kvm, memslot, start_level);
6600 		read_unlock(&kvm->mmu_lock);
6601 	}
6602 }
6603 
6604 static inline bool need_topup(struct kvm_mmu_memory_cache *cache, int min)
6605 {
6606 	return kvm_mmu_memory_cache_nr_free_objects(cache) < min;
6607 }
6608 
6609 static bool need_topup_split_caches_or_resched(struct kvm *kvm)
6610 {
6611 	if (need_resched() || rwlock_needbreak(&kvm->mmu_lock))
6612 		return true;
6613 
6614 	/*
6615 	 * In the worst case, SPLIT_DESC_CACHE_MIN_NR_OBJECTS descriptors are needed
6616 	 * to split a single huge page. Calculating how many are actually needed
6617 	 * is possible but not worth the complexity.
6618 	 */
6619 	return need_topup(&kvm->arch.split_desc_cache, SPLIT_DESC_CACHE_MIN_NR_OBJECTS) ||
6620 	       need_topup(&kvm->arch.split_page_header_cache, 1) ||
6621 	       need_topup(&kvm->arch.split_shadow_page_cache, 1);
6622 }
6623 
6624 static int topup_split_caches(struct kvm *kvm)
6625 {
6626 	/*
6627 	 * Allocating rmap list entries when splitting huge pages for nested
6628 	 * MMUs is uncommon as KVM needs to use a list if and only if there is
6629 	 * more than one rmap entry for a gfn, i.e. requires an L1 gfn to be
6630 	 * aliased by multiple L2 gfns and/or from multiple nested roots with
6631 	 * different roles.  Aliasing gfns when using TDP is atypical for VMMs;
6632 	 * a few gfns are often aliased during boot, e.g. when remapping BIOS,
6633 	 * but aliasing rarely occurs post-boot or for many gfns.  If there is
6634 	 * only one rmap entry, rmap->val points directly at that one entry and
6635 	 * doesn't need to allocate a list.  Buffer the cache by the default
6636 	 * capacity so that KVM doesn't have to drop mmu_lock to topup if KVM
6637 	 * encounters an aliased gfn or two.
6638 	 */
6639 	const int capacity = SPLIT_DESC_CACHE_MIN_NR_OBJECTS +
6640 			     KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE;
6641 	int r;
6642 
6643 	lockdep_assert_held(&kvm->slots_lock);
6644 
6645 	r = __kvm_mmu_topup_memory_cache(&kvm->arch.split_desc_cache, capacity,
6646 					 SPLIT_DESC_CACHE_MIN_NR_OBJECTS);
6647 	if (r)
6648 		return r;
6649 
6650 	r = kvm_mmu_topup_memory_cache(&kvm->arch.split_page_header_cache, 1);
6651 	if (r)
6652 		return r;
6653 
6654 	return kvm_mmu_topup_memory_cache(&kvm->arch.split_shadow_page_cache, 1);
6655 }
6656 
6657 static struct kvm_mmu_page *shadow_mmu_get_sp_for_split(struct kvm *kvm, u64 *huge_sptep)
6658 {
6659 	struct kvm_mmu_page *huge_sp = sptep_to_sp(huge_sptep);
6660 	struct shadow_page_caches caches = {};
6661 	union kvm_mmu_page_role role;
6662 	unsigned int access;
6663 	gfn_t gfn;
6664 
6665 	gfn = kvm_mmu_page_get_gfn(huge_sp, spte_index(huge_sptep));
6666 	access = kvm_mmu_page_get_access(huge_sp, spte_index(huge_sptep));
6667 
6668 	/*
6669 	 * Note, huge page splitting always uses direct shadow pages, regardless
6670 	 * of whether the huge page itself is mapped by a direct or indirect
6671 	 * shadow page, since the huge page region itself is being directly
6672 	 * mapped with smaller pages.
6673 	 */
6674 	role = kvm_mmu_child_role(huge_sptep, /*direct=*/true, access);
6675 
6676 	/* Direct SPs do not require a shadowed_info_cache. */
6677 	caches.page_header_cache = &kvm->arch.split_page_header_cache;
6678 	caches.shadow_page_cache = &kvm->arch.split_shadow_page_cache;
6679 
6680 	/* Safe to pass NULL for vCPU since requesting a direct SP. */
6681 	return __kvm_mmu_get_shadow_page(kvm, NULL, &caches, gfn, role);
6682 }
6683 
6684 static void shadow_mmu_split_huge_page(struct kvm *kvm,
6685 				       const struct kvm_memory_slot *slot,
6686 				       u64 *huge_sptep)
6687 
6688 {
6689 	struct kvm_mmu_memory_cache *cache = &kvm->arch.split_desc_cache;
6690 	u64 huge_spte = READ_ONCE(*huge_sptep);
6691 	struct kvm_mmu_page *sp;
6692 	bool flush = false;
6693 	u64 *sptep, spte;
6694 	gfn_t gfn;
6695 	int index;
6696 
6697 	sp = shadow_mmu_get_sp_for_split(kvm, huge_sptep);
6698 
6699 	for (index = 0; index < SPTE_ENT_PER_PAGE; index++) {
6700 		sptep = &sp->spt[index];
6701 		gfn = kvm_mmu_page_get_gfn(sp, index);
6702 
6703 		/*
6704 		 * The SP may already have populated SPTEs, e.g. if this huge
6705 		 * page is aliased by multiple sptes with the same access
6706 		 * permissions. These entries are guaranteed to map the same
6707 		 * gfn-to-pfn translation since the SP is direct, so no need to
6708 		 * modify them.
6709 		 *
6710 		 * However, if a given SPTE points to a lower level page table,
6711 		 * that lower level page table may only be partially populated.
6712 		 * Installing such SPTEs would effectively unmap a potion of the
6713 		 * huge page. Unmapping guest memory always requires a TLB flush
6714 		 * since a subsequent operation on the unmapped regions would
6715 		 * fail to detect the need to flush.
6716 		 */
6717 		if (is_shadow_present_pte(*sptep)) {
6718 			flush |= !is_last_spte(*sptep, sp->role.level);
6719 			continue;
6720 		}
6721 
6722 		spte = make_small_spte(kvm, huge_spte, sp->role, index);
6723 		mmu_spte_set(sptep, spte);
6724 		__rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access);
6725 	}
6726 
6727 	__link_shadow_page(kvm, cache, huge_sptep, sp, flush);
6728 }
6729 
6730 static int shadow_mmu_try_split_huge_page(struct kvm *kvm,
6731 					  const struct kvm_memory_slot *slot,
6732 					  u64 *huge_sptep)
6733 {
6734 	struct kvm_mmu_page *huge_sp = sptep_to_sp(huge_sptep);
6735 	int level, r = 0;
6736 	gfn_t gfn;
6737 	u64 spte;
6738 
6739 	/* Grab information for the tracepoint before dropping the MMU lock. */
6740 	gfn = kvm_mmu_page_get_gfn(huge_sp, spte_index(huge_sptep));
6741 	level = huge_sp->role.level;
6742 	spte = *huge_sptep;
6743 
6744 	if (kvm_mmu_available_pages(kvm) <= KVM_MIN_FREE_MMU_PAGES) {
6745 		r = -ENOSPC;
6746 		goto out;
6747 	}
6748 
6749 	if (need_topup_split_caches_or_resched(kvm)) {
6750 		write_unlock(&kvm->mmu_lock);
6751 		cond_resched();
6752 		/*
6753 		 * If the topup succeeds, return -EAGAIN to indicate that the
6754 		 * rmap iterator should be restarted because the MMU lock was
6755 		 * dropped.
6756 		 */
6757 		r = topup_split_caches(kvm) ?: -EAGAIN;
6758 		write_lock(&kvm->mmu_lock);
6759 		goto out;
6760 	}
6761 
6762 	shadow_mmu_split_huge_page(kvm, slot, huge_sptep);
6763 
6764 out:
6765 	trace_kvm_mmu_split_huge_page(gfn, spte, level, r);
6766 	return r;
6767 }
6768 
6769 static bool shadow_mmu_try_split_huge_pages(struct kvm *kvm,
6770 					    struct kvm_rmap_head *rmap_head,
6771 					    const struct kvm_memory_slot *slot)
6772 {
6773 	struct rmap_iterator iter;
6774 	struct kvm_mmu_page *sp;
6775 	u64 *huge_sptep;
6776 	int r;
6777 
6778 restart:
6779 	for_each_rmap_spte(rmap_head, &iter, huge_sptep) {
6780 		sp = sptep_to_sp(huge_sptep);
6781 
6782 		/* TDP MMU is enabled, so rmap only contains nested MMU SPs. */
6783 		if (WARN_ON_ONCE(!sp->role.guest_mode))
6784 			continue;
6785 
6786 		/* The rmaps should never contain non-leaf SPTEs. */
6787 		if (WARN_ON_ONCE(!is_large_pte(*huge_sptep)))
6788 			continue;
6789 
6790 		/* SPs with level >PG_LEVEL_4K should never by unsync. */
6791 		if (WARN_ON_ONCE(sp->unsync))
6792 			continue;
6793 
6794 		/* Don't bother splitting huge pages on invalid SPs. */
6795 		if (sp->role.invalid)
6796 			continue;
6797 
6798 		r = shadow_mmu_try_split_huge_page(kvm, slot, huge_sptep);
6799 
6800 		/*
6801 		 * The split succeeded or needs to be retried because the MMU
6802 		 * lock was dropped. Either way, restart the iterator to get it
6803 		 * back into a consistent state.
6804 		 */
6805 		if (!r || r == -EAGAIN)
6806 			goto restart;
6807 
6808 		/* The split failed and shouldn't be retried (e.g. -ENOMEM). */
6809 		break;
6810 	}
6811 
6812 	return false;
6813 }
6814 
6815 static void kvm_shadow_mmu_try_split_huge_pages(struct kvm *kvm,
6816 						const struct kvm_memory_slot *slot,
6817 						gfn_t start, gfn_t end,
6818 						int target_level)
6819 {
6820 	int level;
6821 
6822 	/*
6823 	 * Split huge pages starting with KVM_MAX_HUGEPAGE_LEVEL and working
6824 	 * down to the target level. This ensures pages are recursively split
6825 	 * all the way to the target level. There's no need to split pages
6826 	 * already at the target level.
6827 	 */
6828 	for (level = KVM_MAX_HUGEPAGE_LEVEL; level > target_level; level--)
6829 		__walk_slot_rmaps(kvm, slot, shadow_mmu_try_split_huge_pages,
6830 				  level, level, start, end - 1, true, true, false);
6831 }
6832 
6833 /* Must be called with the mmu_lock held in write-mode. */
6834 void kvm_mmu_try_split_huge_pages(struct kvm *kvm,
6835 				   const struct kvm_memory_slot *memslot,
6836 				   u64 start, u64 end,
6837 				   int target_level)
6838 {
6839 	if (!tdp_mmu_enabled)
6840 		return;
6841 
6842 	if (kvm_memslots_have_rmaps(kvm))
6843 		kvm_shadow_mmu_try_split_huge_pages(kvm, memslot, start, end, target_level);
6844 
6845 	kvm_tdp_mmu_try_split_huge_pages(kvm, memslot, start, end, target_level, false);
6846 
6847 	/*
6848 	 * A TLB flush is unnecessary at this point for the same reasons as in
6849 	 * kvm_mmu_slot_try_split_huge_pages().
6850 	 */
6851 }
6852 
6853 void kvm_mmu_slot_try_split_huge_pages(struct kvm *kvm,
6854 					const struct kvm_memory_slot *memslot,
6855 					int target_level)
6856 {
6857 	u64 start = memslot->base_gfn;
6858 	u64 end = start + memslot->npages;
6859 
6860 	if (!tdp_mmu_enabled)
6861 		return;
6862 
6863 	if (kvm_memslots_have_rmaps(kvm)) {
6864 		write_lock(&kvm->mmu_lock);
6865 		kvm_shadow_mmu_try_split_huge_pages(kvm, memslot, start, end, target_level);
6866 		write_unlock(&kvm->mmu_lock);
6867 	}
6868 
6869 	read_lock(&kvm->mmu_lock);
6870 	kvm_tdp_mmu_try_split_huge_pages(kvm, memslot, start, end, target_level, true);
6871 	read_unlock(&kvm->mmu_lock);
6872 
6873 	/*
6874 	 * No TLB flush is necessary here. KVM will flush TLBs after
6875 	 * write-protecting and/or clearing dirty on the newly split SPTEs to
6876 	 * ensure that guest writes are reflected in the dirty log before the
6877 	 * ioctl to enable dirty logging on this memslot completes. Since the
6878 	 * split SPTEs retain the write and dirty bits of the huge SPTE, it is
6879 	 * safe for KVM to decide if a TLB flush is necessary based on the split
6880 	 * SPTEs.
6881 	 */
6882 }
6883 
6884 static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
6885 					 struct kvm_rmap_head *rmap_head,
6886 					 const struct kvm_memory_slot *slot)
6887 {
6888 	u64 *sptep;
6889 	struct rmap_iterator iter;
6890 	int need_tlb_flush = 0;
6891 	struct kvm_mmu_page *sp;
6892 
6893 restart:
6894 	for_each_rmap_spte(rmap_head, &iter, sptep) {
6895 		sp = sptep_to_sp(sptep);
6896 
6897 		/*
6898 		 * We cannot do huge page mapping for indirect shadow pages,
6899 		 * which are found on the last rmap (level = 1) when not using
6900 		 * tdp; such shadow pages are synced with the page table in
6901 		 * the guest, and the guest page table is using 4K page size
6902 		 * mapping if the indirect sp has level = 1.
6903 		 */
6904 		if (sp->role.direct &&
6905 		    sp->role.level < kvm_mmu_max_mapping_level(kvm, slot, sp->gfn)) {
6906 			kvm_zap_one_rmap_spte(kvm, rmap_head, sptep);
6907 
6908 			if (kvm_available_flush_remote_tlbs_range())
6909 				kvm_flush_remote_tlbs_sptep(kvm, sptep);
6910 			else
6911 				need_tlb_flush = 1;
6912 
6913 			goto restart;
6914 		}
6915 	}
6916 
6917 	return need_tlb_flush;
6918 }
6919 EXPORT_SYMBOL_GPL(kvm_zap_gfn_range);
6920 
6921 static void kvm_rmap_zap_collapsible_sptes(struct kvm *kvm,
6922 					   const struct kvm_memory_slot *slot)
6923 {
6924 	/*
6925 	 * Note, use KVM_MAX_HUGEPAGE_LEVEL - 1 since there's no need to zap
6926 	 * pages that are already mapped at the maximum hugepage level.
6927 	 */
6928 	if (walk_slot_rmaps(kvm, slot, kvm_mmu_zap_collapsible_spte,
6929 			    PG_LEVEL_4K, KVM_MAX_HUGEPAGE_LEVEL - 1, true))
6930 		kvm_flush_remote_tlbs_memslot(kvm, slot);
6931 }
6932 
6933 void kvm_mmu_recover_huge_pages(struct kvm *kvm,
6934 				const struct kvm_memory_slot *slot)
6935 {
6936 	if (kvm_memslots_have_rmaps(kvm)) {
6937 		write_lock(&kvm->mmu_lock);
6938 		kvm_rmap_zap_collapsible_sptes(kvm, slot);
6939 		write_unlock(&kvm->mmu_lock);
6940 	}
6941 
6942 	if (tdp_mmu_enabled) {
6943 		read_lock(&kvm->mmu_lock);
6944 		kvm_tdp_mmu_recover_huge_pages(kvm, slot);
6945 		read_unlock(&kvm->mmu_lock);
6946 	}
6947 }
6948 
6949 void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
6950 				   const struct kvm_memory_slot *memslot)
6951 {
6952 	if (kvm_memslots_have_rmaps(kvm)) {
6953 		write_lock(&kvm->mmu_lock);
6954 		/*
6955 		 * Clear dirty bits only on 4k SPTEs since the legacy MMU only
6956 		 * support dirty logging at a 4k granularity.
6957 		 */
6958 		walk_slot_rmaps_4k(kvm, memslot, __rmap_clear_dirty, false);
6959 		write_unlock(&kvm->mmu_lock);
6960 	}
6961 
6962 	if (tdp_mmu_enabled) {
6963 		read_lock(&kvm->mmu_lock);
6964 		kvm_tdp_mmu_clear_dirty_slot(kvm, memslot);
6965 		read_unlock(&kvm->mmu_lock);
6966 	}
6967 
6968 	/*
6969 	 * The caller will flush the TLBs after this function returns.
6970 	 *
6971 	 * It's also safe to flush TLBs out of mmu lock here as currently this
6972 	 * function is only used for dirty logging, in which case flushing TLB
6973 	 * out of mmu lock also guarantees no dirty pages will be lost in
6974 	 * dirty_bitmap.
6975 	 */
6976 }
6977 
6978 static void kvm_mmu_zap_all(struct kvm *kvm)
6979 {
6980 	struct kvm_mmu_page *sp, *node;
6981 	LIST_HEAD(invalid_list);
6982 	int ign;
6983 
6984 	write_lock(&kvm->mmu_lock);
6985 restart:
6986 	list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) {
6987 		if (WARN_ON_ONCE(sp->role.invalid))
6988 			continue;
6989 		if (__kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list, &ign))
6990 			goto restart;
6991 		if (cond_resched_rwlock_write(&kvm->mmu_lock))
6992 			goto restart;
6993 	}
6994 
6995 	kvm_mmu_commit_zap_page(kvm, &invalid_list);
6996 
6997 	if (tdp_mmu_enabled)
6998 		kvm_tdp_mmu_zap_all(kvm);
6999 
7000 	write_unlock(&kvm->mmu_lock);
7001 }
7002 
7003 void kvm_arch_flush_shadow_all(struct kvm *kvm)
7004 {
7005 	kvm_mmu_zap_all(kvm);
7006 }
7007 
7008 static void kvm_mmu_zap_memslot_pages_and_flush(struct kvm *kvm,
7009 						struct kvm_memory_slot *slot,
7010 						bool flush)
7011 {
7012 	LIST_HEAD(invalid_list);
7013 	unsigned long i;
7014 
7015 	if (list_empty(&kvm->arch.active_mmu_pages))
7016 		goto out_flush;
7017 
7018 	/*
7019 	 * Since accounting information is stored in struct kvm_arch_memory_slot,
7020 	 * all MMU pages that are shadowing guest PTEs must be zapped before the
7021 	 * memslot is deleted, as freeing such pages after the memslot is freed
7022 	 * will result in use-after-free, e.g. in unaccount_shadowed().
7023 	 */
7024 	for (i = 0; i < slot->npages; i++) {
7025 		struct kvm_mmu_page *sp;
7026 		gfn_t gfn = slot->base_gfn + i;
7027 
7028 		for_each_gfn_valid_sp_with_gptes(kvm, sp, gfn)
7029 			kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
7030 
7031 		if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
7032 			kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
7033 			flush = false;
7034 			cond_resched_rwlock_write(&kvm->mmu_lock);
7035 		}
7036 	}
7037 
7038 out_flush:
7039 	kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
7040 }
7041 
7042 static void kvm_mmu_zap_memslot(struct kvm *kvm,
7043 				struct kvm_memory_slot *slot)
7044 {
7045 	struct kvm_gfn_range range = {
7046 		.slot = slot,
7047 		.start = slot->base_gfn,
7048 		.end = slot->base_gfn + slot->npages,
7049 		.may_block = true,
7050 	};
7051 	bool flush;
7052 
7053 	write_lock(&kvm->mmu_lock);
7054 	flush = kvm_unmap_gfn_range(kvm, &range);
7055 	kvm_mmu_zap_memslot_pages_and_flush(kvm, slot, flush);
7056 	write_unlock(&kvm->mmu_lock);
7057 }
7058 
7059 static inline bool kvm_memslot_flush_zap_all(struct kvm *kvm)
7060 {
7061 	return kvm->arch.vm_type == KVM_X86_DEFAULT_VM &&
7062 	       kvm_check_has_quirk(kvm, KVM_X86_QUIRK_SLOT_ZAP_ALL);
7063 }
7064 
7065 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
7066 				   struct kvm_memory_slot *slot)
7067 {
7068 	if (kvm_memslot_flush_zap_all(kvm))
7069 		kvm_mmu_zap_all_fast(kvm);
7070 	else
7071 		kvm_mmu_zap_memslot(kvm, slot);
7072 }
7073 
7074 void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen)
7075 {
7076 	WARN_ON_ONCE(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS);
7077 
7078 	gen &= MMIO_SPTE_GEN_MASK;
7079 
7080 	/*
7081 	 * Generation numbers are incremented in multiples of the number of
7082 	 * address spaces in order to provide unique generations across all
7083 	 * address spaces.  Strip what is effectively the address space
7084 	 * modifier prior to checking for a wrap of the MMIO generation so
7085 	 * that a wrap in any address space is detected.
7086 	 */
7087 	gen &= ~((u64)kvm_arch_nr_memslot_as_ids(kvm) - 1);
7088 
7089 	/*
7090 	 * The very rare case: if the MMIO generation number has wrapped,
7091 	 * zap all shadow pages.
7092 	 */
7093 	if (unlikely(gen == 0)) {
7094 		kvm_debug_ratelimited("zapping shadow pages for mmio generation wraparound\n");
7095 		kvm_mmu_zap_all_fast(kvm);
7096 	}
7097 }
7098 
7099 static void mmu_destroy_caches(void)
7100 {
7101 	kmem_cache_destroy(pte_list_desc_cache);
7102 	kmem_cache_destroy(mmu_page_header_cache);
7103 }
7104 
7105 static int get_nx_huge_pages(char *buffer, const struct kernel_param *kp)
7106 {
7107 	if (nx_hugepage_mitigation_hard_disabled)
7108 		return sysfs_emit(buffer, "never\n");
7109 
7110 	return param_get_bool(buffer, kp);
7111 }
7112 
7113 static bool get_nx_auto_mode(void)
7114 {
7115 	/* Return true when CPU has the bug, and mitigations are ON */
7116 	return boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT) && !cpu_mitigations_off();
7117 }
7118 
7119 static void __set_nx_huge_pages(bool val)
7120 {
7121 	nx_huge_pages = itlb_multihit_kvm_mitigation = val;
7122 }
7123 
7124 static int set_nx_huge_pages(const char *val, const struct kernel_param *kp)
7125 {
7126 	bool old_val = nx_huge_pages;
7127 	bool new_val;
7128 
7129 	if (nx_hugepage_mitigation_hard_disabled)
7130 		return -EPERM;
7131 
7132 	/* In "auto" mode deploy workaround only if CPU has the bug. */
7133 	if (sysfs_streq(val, "off")) {
7134 		new_val = 0;
7135 	} else if (sysfs_streq(val, "force")) {
7136 		new_val = 1;
7137 	} else if (sysfs_streq(val, "auto")) {
7138 		new_val = get_nx_auto_mode();
7139 	} else if (sysfs_streq(val, "never")) {
7140 		new_val = 0;
7141 
7142 		mutex_lock(&kvm_lock);
7143 		if (!list_empty(&vm_list)) {
7144 			mutex_unlock(&kvm_lock);
7145 			return -EBUSY;
7146 		}
7147 		nx_hugepage_mitigation_hard_disabled = true;
7148 		mutex_unlock(&kvm_lock);
7149 	} else if (kstrtobool(val, &new_val) < 0) {
7150 		return -EINVAL;
7151 	}
7152 
7153 	__set_nx_huge_pages(new_val);
7154 
7155 	if (new_val != old_val) {
7156 		struct kvm *kvm;
7157 
7158 		mutex_lock(&kvm_lock);
7159 
7160 		list_for_each_entry(kvm, &vm_list, vm_list) {
7161 			mutex_lock(&kvm->slots_lock);
7162 			kvm_mmu_zap_all_fast(kvm);
7163 			mutex_unlock(&kvm->slots_lock);
7164 
7165 			vhost_task_wake(kvm->arch.nx_huge_page_recovery_thread);
7166 		}
7167 		mutex_unlock(&kvm_lock);
7168 	}
7169 
7170 	return 0;
7171 }
7172 
7173 /*
7174  * nx_huge_pages needs to be resolved to true/false when kvm.ko is loaded, as
7175  * its default value of -1 is technically undefined behavior for a boolean.
7176  * Forward the module init call to SPTE code so that it too can handle module
7177  * params that need to be resolved/snapshot.
7178  */
7179 void __init kvm_mmu_x86_module_init(void)
7180 {
7181 	if (nx_huge_pages == -1)
7182 		__set_nx_huge_pages(get_nx_auto_mode());
7183 
7184 	/*
7185 	 * Snapshot userspace's desire to enable the TDP MMU. Whether or not the
7186 	 * TDP MMU is actually enabled is determined in kvm_configure_mmu()
7187 	 * when the vendor module is loaded.
7188 	 */
7189 	tdp_mmu_allowed = tdp_mmu_enabled;
7190 
7191 	kvm_mmu_spte_module_init();
7192 }
7193 
7194 /*
7195  * The bulk of the MMU initialization is deferred until the vendor module is
7196  * loaded as many of the masks/values may be modified by VMX or SVM, i.e. need
7197  * to be reset when a potentially different vendor module is loaded.
7198  */
7199 int kvm_mmu_vendor_module_init(void)
7200 {
7201 	int ret = -ENOMEM;
7202 
7203 	/*
7204 	 * MMU roles use union aliasing which is, generally speaking, an
7205 	 * undefined behavior. However, we supposedly know how compilers behave
7206 	 * and the current status quo is unlikely to change. Guardians below are
7207 	 * supposed to let us know if the assumption becomes false.
7208 	 */
7209 	BUILD_BUG_ON(sizeof(union kvm_mmu_page_role) != sizeof(u32));
7210 	BUILD_BUG_ON(sizeof(union kvm_mmu_extended_role) != sizeof(u32));
7211 	BUILD_BUG_ON(sizeof(union kvm_cpu_role) != sizeof(u64));
7212 
7213 	kvm_mmu_reset_all_pte_masks();
7214 
7215 	pte_list_desc_cache = KMEM_CACHE(pte_list_desc, SLAB_ACCOUNT);
7216 	if (!pte_list_desc_cache)
7217 		goto out;
7218 
7219 	mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
7220 						  sizeof(struct kvm_mmu_page),
7221 						  0, SLAB_ACCOUNT, NULL);
7222 	if (!mmu_page_header_cache)
7223 		goto out;
7224 
7225 	return 0;
7226 
7227 out:
7228 	mmu_destroy_caches();
7229 	return ret;
7230 }
7231 
7232 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
7233 {
7234 	kvm_mmu_unload(vcpu);
7235 	free_mmu_pages(&vcpu->arch.root_mmu);
7236 	free_mmu_pages(&vcpu->arch.guest_mmu);
7237 	mmu_free_memory_caches(vcpu);
7238 }
7239 
7240 void kvm_mmu_vendor_module_exit(void)
7241 {
7242 	mmu_destroy_caches();
7243 }
7244 
7245 /*
7246  * Calculate the effective recovery period, accounting for '0' meaning "let KVM
7247  * select a halving time of 1 hour".  Returns true if recovery is enabled.
7248  */
7249 static bool calc_nx_huge_pages_recovery_period(uint *period)
7250 {
7251 	/*
7252 	 * Use READ_ONCE to get the params, this may be called outside of the
7253 	 * param setters, e.g. by the kthread to compute its next timeout.
7254 	 */
7255 	bool enabled = READ_ONCE(nx_huge_pages);
7256 	uint ratio = READ_ONCE(nx_huge_pages_recovery_ratio);
7257 
7258 	if (!enabled || !ratio)
7259 		return false;
7260 
7261 	*period = READ_ONCE(nx_huge_pages_recovery_period_ms);
7262 	if (!*period) {
7263 		/* Make sure the period is not less than one second.  */
7264 		ratio = min(ratio, 3600u);
7265 		*period = 60 * 60 * 1000 / ratio;
7266 	}
7267 	return true;
7268 }
7269 
7270 static int set_nx_huge_pages_recovery_param(const char *val, const struct kernel_param *kp)
7271 {
7272 	bool was_recovery_enabled, is_recovery_enabled;
7273 	uint old_period, new_period;
7274 	int err;
7275 
7276 	if (nx_hugepage_mitigation_hard_disabled)
7277 		return -EPERM;
7278 
7279 	was_recovery_enabled = calc_nx_huge_pages_recovery_period(&old_period);
7280 
7281 	err = param_set_uint(val, kp);
7282 	if (err)
7283 		return err;
7284 
7285 	is_recovery_enabled = calc_nx_huge_pages_recovery_period(&new_period);
7286 
7287 	if (is_recovery_enabled &&
7288 	    (!was_recovery_enabled || old_period > new_period)) {
7289 		struct kvm *kvm;
7290 
7291 		mutex_lock(&kvm_lock);
7292 
7293 		list_for_each_entry(kvm, &vm_list, vm_list)
7294 			vhost_task_wake(kvm->arch.nx_huge_page_recovery_thread);
7295 
7296 		mutex_unlock(&kvm_lock);
7297 	}
7298 
7299 	return err;
7300 }
7301 
7302 static void kvm_recover_nx_huge_pages(struct kvm *kvm)
7303 {
7304 	unsigned long nx_lpage_splits = kvm->stat.nx_lpage_splits;
7305 	struct kvm_memory_slot *slot;
7306 	int rcu_idx;
7307 	struct kvm_mmu_page *sp;
7308 	unsigned int ratio;
7309 	LIST_HEAD(invalid_list);
7310 	bool flush = false;
7311 	ulong to_zap;
7312 
7313 	rcu_idx = srcu_read_lock(&kvm->srcu);
7314 	write_lock(&kvm->mmu_lock);
7315 
7316 	/*
7317 	 * Zapping TDP MMU shadow pages, including the remote TLB flush, must
7318 	 * be done under RCU protection, because the pages are freed via RCU
7319 	 * callback.
7320 	 */
7321 	rcu_read_lock();
7322 
7323 	ratio = READ_ONCE(nx_huge_pages_recovery_ratio);
7324 	to_zap = ratio ? DIV_ROUND_UP(nx_lpage_splits, ratio) : 0;
7325 	for ( ; to_zap; --to_zap) {
7326 		if (list_empty(&kvm->arch.possible_nx_huge_pages))
7327 			break;
7328 
7329 		/*
7330 		 * We use a separate list instead of just using active_mmu_pages
7331 		 * because the number of shadow pages that be replaced with an
7332 		 * NX huge page is expected to be relatively small compared to
7333 		 * the total number of shadow pages.  And because the TDP MMU
7334 		 * doesn't use active_mmu_pages.
7335 		 */
7336 		sp = list_first_entry(&kvm->arch.possible_nx_huge_pages,
7337 				      struct kvm_mmu_page,
7338 				      possible_nx_huge_page_link);
7339 		WARN_ON_ONCE(!sp->nx_huge_page_disallowed);
7340 		WARN_ON_ONCE(!sp->role.direct);
7341 
7342 		/*
7343 		 * Unaccount and do not attempt to recover any NX Huge Pages
7344 		 * that are being dirty tracked, as they would just be faulted
7345 		 * back in as 4KiB pages. The NX Huge Pages in this slot will be
7346 		 * recovered, along with all the other huge pages in the slot,
7347 		 * when dirty logging is disabled.
7348 		 *
7349 		 * Since gfn_to_memslot() is relatively expensive, it helps to
7350 		 * skip it if it the test cannot possibly return true.  On the
7351 		 * other hand, if any memslot has logging enabled, chances are
7352 		 * good that all of them do, in which case unaccount_nx_huge_page()
7353 		 * is much cheaper than zapping the page.
7354 		 *
7355 		 * If a memslot update is in progress, reading an incorrect value
7356 		 * of kvm->nr_memslots_dirty_logging is not a problem: if it is
7357 		 * becoming zero, gfn_to_memslot() will be done unnecessarily; if
7358 		 * it is becoming nonzero, the page will be zapped unnecessarily.
7359 		 * Either way, this only affects efficiency in racy situations,
7360 		 * and not correctness.
7361 		 */
7362 		slot = NULL;
7363 		if (atomic_read(&kvm->nr_memslots_dirty_logging)) {
7364 			struct kvm_memslots *slots;
7365 
7366 			slots = kvm_memslots_for_spte_role(kvm, sp->role);
7367 			slot = __gfn_to_memslot(slots, sp->gfn);
7368 			WARN_ON_ONCE(!slot);
7369 		}
7370 
7371 		if (slot && kvm_slot_dirty_track_enabled(slot))
7372 			unaccount_nx_huge_page(kvm, sp);
7373 		else if (is_tdp_mmu_page(sp))
7374 			flush |= kvm_tdp_mmu_zap_sp(kvm, sp);
7375 		else
7376 			kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
7377 		WARN_ON_ONCE(sp->nx_huge_page_disallowed);
7378 
7379 		if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
7380 			kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
7381 			rcu_read_unlock();
7382 
7383 			cond_resched_rwlock_write(&kvm->mmu_lock);
7384 			flush = false;
7385 
7386 			rcu_read_lock();
7387 		}
7388 	}
7389 	kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
7390 
7391 	rcu_read_unlock();
7392 
7393 	write_unlock(&kvm->mmu_lock);
7394 	srcu_read_unlock(&kvm->srcu, rcu_idx);
7395 }
7396 
7397 static void kvm_nx_huge_page_recovery_worker_kill(void *data)
7398 {
7399 }
7400 
7401 static bool kvm_nx_huge_page_recovery_worker(void *data)
7402 {
7403 	struct kvm *kvm = data;
7404 	bool enabled;
7405 	uint period;
7406 	long remaining_time;
7407 
7408 	enabled = calc_nx_huge_pages_recovery_period(&period);
7409 	if (!enabled)
7410 		return false;
7411 
7412 	remaining_time = kvm->arch.nx_huge_page_last + msecs_to_jiffies(period)
7413 		- get_jiffies_64();
7414 	if (remaining_time > 0) {
7415 		schedule_timeout(remaining_time);
7416 		/* check for signals and come back */
7417 		return true;
7418 	}
7419 
7420 	__set_current_state(TASK_RUNNING);
7421 	kvm_recover_nx_huge_pages(kvm);
7422 	kvm->arch.nx_huge_page_last = get_jiffies_64();
7423 	return true;
7424 }
7425 
7426 int kvm_mmu_post_init_vm(struct kvm *kvm)
7427 {
7428 	if (nx_hugepage_mitigation_hard_disabled)
7429 		return 0;
7430 
7431 	kvm->arch.nx_huge_page_last = get_jiffies_64();
7432 	kvm->arch.nx_huge_page_recovery_thread = vhost_task_create(
7433 		kvm_nx_huge_page_recovery_worker, kvm_nx_huge_page_recovery_worker_kill,
7434 		kvm, "kvm-nx-lpage-recovery");
7435 
7436 	if (!kvm->arch.nx_huge_page_recovery_thread)
7437 		return -ENOMEM;
7438 
7439 	vhost_task_start(kvm->arch.nx_huge_page_recovery_thread);
7440 	return 0;
7441 }
7442 
7443 void kvm_mmu_pre_destroy_vm(struct kvm *kvm)
7444 {
7445 	if (kvm->arch.nx_huge_page_recovery_thread)
7446 		vhost_task_stop(kvm->arch.nx_huge_page_recovery_thread);
7447 }
7448 
7449 #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
7450 bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
7451 					struct kvm_gfn_range *range)
7452 {
7453 	/*
7454 	 * Zap SPTEs even if the slot can't be mapped PRIVATE.  KVM x86 only
7455 	 * supports KVM_MEMORY_ATTRIBUTE_PRIVATE, and so it *seems* like KVM
7456 	 * can simply ignore such slots.  But if userspace is making memory
7457 	 * PRIVATE, then KVM must prevent the guest from accessing the memory
7458 	 * as shared.  And if userspace is making memory SHARED and this point
7459 	 * is reached, then at least one page within the range was previously
7460 	 * PRIVATE, i.e. the slot's possible hugepage ranges are changing.
7461 	 * Zapping SPTEs in this case ensures KVM will reassess whether or not
7462 	 * a hugepage can be used for affected ranges.
7463 	 */
7464 	if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm)))
7465 		return false;
7466 
7467 	return kvm_unmap_gfn_range(kvm, range);
7468 }
7469 
7470 static bool hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn,
7471 				int level)
7472 {
7473 	return lpage_info_slot(gfn, slot, level)->disallow_lpage & KVM_LPAGE_MIXED_FLAG;
7474 }
7475 
7476 static void hugepage_clear_mixed(struct kvm_memory_slot *slot, gfn_t gfn,
7477 				 int level)
7478 {
7479 	lpage_info_slot(gfn, slot, level)->disallow_lpage &= ~KVM_LPAGE_MIXED_FLAG;
7480 }
7481 
7482 static void hugepage_set_mixed(struct kvm_memory_slot *slot, gfn_t gfn,
7483 			       int level)
7484 {
7485 	lpage_info_slot(gfn, slot, level)->disallow_lpage |= KVM_LPAGE_MIXED_FLAG;
7486 }
7487 
7488 static bool hugepage_has_attrs(struct kvm *kvm, struct kvm_memory_slot *slot,
7489 			       gfn_t gfn, int level, unsigned long attrs)
7490 {
7491 	const unsigned long start = gfn;
7492 	const unsigned long end = start + KVM_PAGES_PER_HPAGE(level);
7493 
7494 	if (level == PG_LEVEL_2M)
7495 		return kvm_range_has_memory_attributes(kvm, start, end, ~0, attrs);
7496 
7497 	for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) {
7498 		if (hugepage_test_mixed(slot, gfn, level - 1) ||
7499 		    attrs != kvm_get_memory_attributes(kvm, gfn))
7500 			return false;
7501 	}
7502 	return true;
7503 }
7504 
7505 bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,
7506 					 struct kvm_gfn_range *range)
7507 {
7508 	unsigned long attrs = range->arg.attributes;
7509 	struct kvm_memory_slot *slot = range->slot;
7510 	int level;
7511 
7512 	lockdep_assert_held_write(&kvm->mmu_lock);
7513 	lockdep_assert_held(&kvm->slots_lock);
7514 
7515 	/*
7516 	 * Calculate which ranges can be mapped with hugepages even if the slot
7517 	 * can't map memory PRIVATE.  KVM mustn't create a SHARED hugepage over
7518 	 * a range that has PRIVATE GFNs, and conversely converting a range to
7519 	 * SHARED may now allow hugepages.
7520 	 */
7521 	if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm)))
7522 		return false;
7523 
7524 	/*
7525 	 * The sequence matters here: upper levels consume the result of lower
7526 	 * level's scanning.
7527 	 */
7528 	for (level = PG_LEVEL_2M; level <= KVM_MAX_HUGEPAGE_LEVEL; level++) {
7529 		gfn_t nr_pages = KVM_PAGES_PER_HPAGE(level);
7530 		gfn_t gfn = gfn_round_for_level(range->start, level);
7531 
7532 		/* Process the head page if it straddles the range. */
7533 		if (gfn != range->start || gfn + nr_pages > range->end) {
7534 			/*
7535 			 * Skip mixed tracking if the aligned gfn isn't covered
7536 			 * by the memslot, KVM can't use a hugepage due to the
7537 			 * misaligned address regardless of memory attributes.
7538 			 */
7539 			if (gfn >= slot->base_gfn &&
7540 			    gfn + nr_pages <= slot->base_gfn + slot->npages) {
7541 				if (hugepage_has_attrs(kvm, slot, gfn, level, attrs))
7542 					hugepage_clear_mixed(slot, gfn, level);
7543 				else
7544 					hugepage_set_mixed(slot, gfn, level);
7545 			}
7546 			gfn += nr_pages;
7547 		}
7548 
7549 		/*
7550 		 * Pages entirely covered by the range are guaranteed to have
7551 		 * only the attributes which were just set.
7552 		 */
7553 		for ( ; gfn + nr_pages <= range->end; gfn += nr_pages)
7554 			hugepage_clear_mixed(slot, gfn, level);
7555 
7556 		/*
7557 		 * Process the last tail page if it straddles the range and is
7558 		 * contained by the memslot.  Like the head page, KVM can't
7559 		 * create a hugepage if the slot size is misaligned.
7560 		 */
7561 		if (gfn < range->end &&
7562 		    (gfn + nr_pages) <= (slot->base_gfn + slot->npages)) {
7563 			if (hugepage_has_attrs(kvm, slot, gfn, level, attrs))
7564 				hugepage_clear_mixed(slot, gfn, level);
7565 			else
7566 				hugepage_set_mixed(slot, gfn, level);
7567 		}
7568 	}
7569 	return false;
7570 }
7571 
7572 void kvm_mmu_init_memslot_memory_attributes(struct kvm *kvm,
7573 					    struct kvm_memory_slot *slot)
7574 {
7575 	int level;
7576 
7577 	if (!kvm_arch_has_private_mem(kvm))
7578 		return;
7579 
7580 	for (level = PG_LEVEL_2M; level <= KVM_MAX_HUGEPAGE_LEVEL; level++) {
7581 		/*
7582 		 * Don't bother tracking mixed attributes for pages that can't
7583 		 * be huge due to alignment, i.e. process only pages that are
7584 		 * entirely contained by the memslot.
7585 		 */
7586 		gfn_t end = gfn_round_for_level(slot->base_gfn + slot->npages, level);
7587 		gfn_t start = gfn_round_for_level(slot->base_gfn, level);
7588 		gfn_t nr_pages = KVM_PAGES_PER_HPAGE(level);
7589 		gfn_t gfn;
7590 
7591 		if (start < slot->base_gfn)
7592 			start += nr_pages;
7593 
7594 		/*
7595 		 * Unlike setting attributes, every potential hugepage needs to
7596 		 * be manually checked as the attributes may already be mixed.
7597 		 */
7598 		for (gfn = start; gfn < end; gfn += nr_pages) {
7599 			unsigned long attrs = kvm_get_memory_attributes(kvm, gfn);
7600 
7601 			if (hugepage_has_attrs(kvm, slot, gfn, level, attrs))
7602 				hugepage_clear_mixed(slot, gfn, level);
7603 			else
7604 				hugepage_set_mixed(slot, gfn, level);
7605 		}
7606 	}
7607 }
7608 #endif
7609