xref: /kernel/linux/linux-6.6/arch/x86/kvm/mmu/spte.h (revision 62306a36)
162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#ifndef KVM_X86_MMU_SPTE_H
462306a36Sopenharmony_ci#define KVM_X86_MMU_SPTE_H
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include "mmu.h"
762306a36Sopenharmony_ci#include "mmu_internal.h"
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/*
1062306a36Sopenharmony_ci * A MMU present SPTE is backed by actual memory and may or may not be present
1162306a36Sopenharmony_ci * in hardware.  E.g. MMIO SPTEs are not considered present.  Use bit 11, as it
1262306a36Sopenharmony_ci * is ignored by all flavors of SPTEs and checking a low bit often generates
1362306a36Sopenharmony_ci * better code than for a high bit, e.g. 56+.  MMU present checks are pervasive
1462306a36Sopenharmony_ci * enough that the improved code generation is noticeable in KVM's footprint.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci#define SPTE_MMU_PRESENT_MASK		BIT_ULL(11)
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/*
1962306a36Sopenharmony_ci * TDP SPTES (more specifically, EPT SPTEs) may not have A/D bits, and may also
2062306a36Sopenharmony_ci * be restricted to using write-protection (for L2 when CPU dirty logging, i.e.
2162306a36Sopenharmony_ci * PML, is enabled).  Use bits 52 and 53 to hold the type of A/D tracking that
2262306a36Sopenharmony_ci * is must be employed for a given TDP SPTE.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * Note, the "enabled" mask must be '0', as bits 62:52 are _reserved_ for PAE
2562306a36Sopenharmony_ci * paging, including NPT PAE.  This scheme works because legacy shadow paging
2662306a36Sopenharmony_ci * is guaranteed to have A/D bits and write-protection is forced only for
2762306a36Sopenharmony_ci * TDP with CPU dirty logging (PML).  If NPT ever gains PML-like support, it
2862306a36Sopenharmony_ci * must be restricted to 64-bit KVM.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci#define SPTE_TDP_AD_SHIFT		52
3162306a36Sopenharmony_ci#define SPTE_TDP_AD_MASK		(3ULL << SPTE_TDP_AD_SHIFT)
3262306a36Sopenharmony_ci#define SPTE_TDP_AD_ENABLED		(0ULL << SPTE_TDP_AD_SHIFT)
3362306a36Sopenharmony_ci#define SPTE_TDP_AD_DISABLED		(1ULL << SPTE_TDP_AD_SHIFT)
3462306a36Sopenharmony_ci#define SPTE_TDP_AD_WRPROT_ONLY		(2ULL << SPTE_TDP_AD_SHIFT)
3562306a36Sopenharmony_cistatic_assert(SPTE_TDP_AD_ENABLED == 0);
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#ifdef CONFIG_DYNAMIC_PHYSICAL_MASK
3862306a36Sopenharmony_ci#define SPTE_BASE_ADDR_MASK (physical_mask & ~(u64)(PAGE_SIZE-1))
3962306a36Sopenharmony_ci#else
4062306a36Sopenharmony_ci#define SPTE_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
4162306a36Sopenharmony_ci#endif
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci#define SPTE_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | shadow_user_mask \
4462306a36Sopenharmony_ci			| shadow_x_mask | shadow_nx_mask | shadow_me_mask)
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define ACC_EXEC_MASK    1
4762306a36Sopenharmony_ci#define ACC_WRITE_MASK   PT_WRITABLE_MASK
4862306a36Sopenharmony_ci#define ACC_USER_MASK    PT_USER_MASK
4962306a36Sopenharmony_ci#define ACC_ALL          (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* The mask for the R/X bits in EPT PTEs */
5262306a36Sopenharmony_ci#define SPTE_EPT_READABLE_MASK			0x1ull
5362306a36Sopenharmony_ci#define SPTE_EPT_EXECUTABLE_MASK		0x4ull
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define SPTE_LEVEL_BITS			9
5662306a36Sopenharmony_ci#define SPTE_LEVEL_SHIFT(level)		__PT_LEVEL_SHIFT(level, SPTE_LEVEL_BITS)
5762306a36Sopenharmony_ci#define SPTE_INDEX(address, level)	__PT_INDEX(address, level, SPTE_LEVEL_BITS)
5862306a36Sopenharmony_ci#define SPTE_ENT_PER_PAGE		__PT_ENT_PER_PAGE(SPTE_LEVEL_BITS)
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci/*
6162306a36Sopenharmony_ci * The mask/shift to use for saving the original R/X bits when marking the PTE
6262306a36Sopenharmony_ci * as not-present for access tracking purposes. We do not save the W bit as the
6362306a36Sopenharmony_ci * PTEs being access tracked also need to be dirty tracked, so the W bit will be
6462306a36Sopenharmony_ci * restored only when a write is attempted to the page.  This mask obviously
6562306a36Sopenharmony_ci * must not overlap the A/D type mask.
6662306a36Sopenharmony_ci */
6762306a36Sopenharmony_ci#define SHADOW_ACC_TRACK_SAVED_BITS_MASK (SPTE_EPT_READABLE_MASK | \
6862306a36Sopenharmony_ci					  SPTE_EPT_EXECUTABLE_MASK)
6962306a36Sopenharmony_ci#define SHADOW_ACC_TRACK_SAVED_BITS_SHIFT 54
7062306a36Sopenharmony_ci#define SHADOW_ACC_TRACK_SAVED_MASK	(SHADOW_ACC_TRACK_SAVED_BITS_MASK << \
7162306a36Sopenharmony_ci					 SHADOW_ACC_TRACK_SAVED_BITS_SHIFT)
7262306a36Sopenharmony_cistatic_assert(!(SPTE_TDP_AD_MASK & SHADOW_ACC_TRACK_SAVED_MASK));
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/*
7562306a36Sopenharmony_ci * {DEFAULT,EPT}_SPTE_{HOST,MMU}_WRITABLE are used to keep track of why a given
7662306a36Sopenharmony_ci * SPTE is write-protected. See is_writable_pte() for details.
7762306a36Sopenharmony_ci */
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/* Bits 9 and 10 are ignored by all non-EPT PTEs. */
8062306a36Sopenharmony_ci#define DEFAULT_SPTE_HOST_WRITABLE	BIT_ULL(9)
8162306a36Sopenharmony_ci#define DEFAULT_SPTE_MMU_WRITABLE	BIT_ULL(10)
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/*
8462306a36Sopenharmony_ci * Low ignored bits are at a premium for EPT, use high ignored bits, taking care
8562306a36Sopenharmony_ci * to not overlap the A/D type mask or the saved access bits of access-tracked
8662306a36Sopenharmony_ci * SPTEs when A/D bits are disabled.
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_ci#define EPT_SPTE_HOST_WRITABLE		BIT_ULL(57)
8962306a36Sopenharmony_ci#define EPT_SPTE_MMU_WRITABLE		BIT_ULL(58)
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_cistatic_assert(!(EPT_SPTE_HOST_WRITABLE & SPTE_TDP_AD_MASK));
9262306a36Sopenharmony_cistatic_assert(!(EPT_SPTE_MMU_WRITABLE & SPTE_TDP_AD_MASK));
9362306a36Sopenharmony_cistatic_assert(!(EPT_SPTE_HOST_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));
9462306a36Sopenharmony_cistatic_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci/* Defined only to keep the above static asserts readable. */
9762306a36Sopenharmony_ci#undef SHADOW_ACC_TRACK_SAVED_MASK
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci/*
10062306a36Sopenharmony_ci * Due to limited space in PTEs, the MMIO generation is a 19 bit subset of
10162306a36Sopenharmony_ci * the memslots generation and is derived as follows:
10262306a36Sopenharmony_ci *
10362306a36Sopenharmony_ci * Bits 0-7 of the MMIO generation are propagated to spte bits 3-10
10462306a36Sopenharmony_ci * Bits 8-18 of the MMIO generation are propagated to spte bits 52-62
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * The KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS flag is intentionally not included in
10762306a36Sopenharmony_ci * the MMIO generation number, as doing so would require stealing a bit from
10862306a36Sopenharmony_ci * the "real" generation number and thus effectively halve the maximum number
10962306a36Sopenharmony_ci * of MMIO generations that can be handled before encountering a wrap (which
11062306a36Sopenharmony_ci * requires a full MMU zap).  The flag is instead explicitly queried when
11162306a36Sopenharmony_ci * checking for MMIO spte cache hits.
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci#define MMIO_SPTE_GEN_LOW_START		3
11562306a36Sopenharmony_ci#define MMIO_SPTE_GEN_LOW_END		10
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci#define MMIO_SPTE_GEN_HIGH_START	52
11862306a36Sopenharmony_ci#define MMIO_SPTE_GEN_HIGH_END		62
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci#define MMIO_SPTE_GEN_LOW_MASK		GENMASK_ULL(MMIO_SPTE_GEN_LOW_END, \
12162306a36Sopenharmony_ci						    MMIO_SPTE_GEN_LOW_START)
12262306a36Sopenharmony_ci#define MMIO_SPTE_GEN_HIGH_MASK		GENMASK_ULL(MMIO_SPTE_GEN_HIGH_END, \
12362306a36Sopenharmony_ci						    MMIO_SPTE_GEN_HIGH_START)
12462306a36Sopenharmony_cistatic_assert(!(SPTE_MMU_PRESENT_MASK &
12562306a36Sopenharmony_ci		(MMIO_SPTE_GEN_LOW_MASK | MMIO_SPTE_GEN_HIGH_MASK)));
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci/*
12862306a36Sopenharmony_ci * The SPTE MMIO mask must NOT overlap the MMIO generation bits or the
12962306a36Sopenharmony_ci * MMU-present bit.  The generation obviously co-exists with the magic MMIO
13062306a36Sopenharmony_ci * mask/value, and MMIO SPTEs are considered !MMU-present.
13162306a36Sopenharmony_ci *
13262306a36Sopenharmony_ci * The SPTE MMIO mask is allowed to use hardware "present" bits (i.e. all EPT
13362306a36Sopenharmony_ci * RWX bits), all physical address bits (legal PA bits are used for "fast" MMIO
13462306a36Sopenharmony_ci * and so they're off-limits for generation; additional checks ensure the mask
13562306a36Sopenharmony_ci * doesn't overlap legal PA bits), and bit 63 (carved out for future usage).
13662306a36Sopenharmony_ci */
13762306a36Sopenharmony_ci#define SPTE_MMIO_ALLOWED_MASK (BIT_ULL(63) | GENMASK_ULL(51, 12) | GENMASK_ULL(2, 0))
13862306a36Sopenharmony_cistatic_assert(!(SPTE_MMIO_ALLOWED_MASK &
13962306a36Sopenharmony_ci		(SPTE_MMU_PRESENT_MASK | MMIO_SPTE_GEN_LOW_MASK | MMIO_SPTE_GEN_HIGH_MASK)));
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci#define MMIO_SPTE_GEN_LOW_BITS		(MMIO_SPTE_GEN_LOW_END - MMIO_SPTE_GEN_LOW_START + 1)
14262306a36Sopenharmony_ci#define MMIO_SPTE_GEN_HIGH_BITS		(MMIO_SPTE_GEN_HIGH_END - MMIO_SPTE_GEN_HIGH_START + 1)
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/* remember to adjust the comment above as well if you change these */
14562306a36Sopenharmony_cistatic_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11);
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci#define MMIO_SPTE_GEN_LOW_SHIFT		(MMIO_SPTE_GEN_LOW_START - 0)
14862306a36Sopenharmony_ci#define MMIO_SPTE_GEN_HIGH_SHIFT	(MMIO_SPTE_GEN_HIGH_START - MMIO_SPTE_GEN_LOW_BITS)
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci#define MMIO_SPTE_GEN_MASK		GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0)
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ciextern u64 __read_mostly shadow_host_writable_mask;
15362306a36Sopenharmony_ciextern u64 __read_mostly shadow_mmu_writable_mask;
15462306a36Sopenharmony_ciextern u64 __read_mostly shadow_nx_mask;
15562306a36Sopenharmony_ciextern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
15662306a36Sopenharmony_ciextern u64 __read_mostly shadow_user_mask;
15762306a36Sopenharmony_ciextern u64 __read_mostly shadow_accessed_mask;
15862306a36Sopenharmony_ciextern u64 __read_mostly shadow_dirty_mask;
15962306a36Sopenharmony_ciextern u64 __read_mostly shadow_mmio_value;
16062306a36Sopenharmony_ciextern u64 __read_mostly shadow_mmio_mask;
16162306a36Sopenharmony_ciextern u64 __read_mostly shadow_mmio_access_mask;
16262306a36Sopenharmony_ciextern u64 __read_mostly shadow_present_mask;
16362306a36Sopenharmony_ciextern u64 __read_mostly shadow_memtype_mask;
16462306a36Sopenharmony_ciextern u64 __read_mostly shadow_me_value;
16562306a36Sopenharmony_ciextern u64 __read_mostly shadow_me_mask;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/*
16862306a36Sopenharmony_ci * SPTEs in MMUs without A/D bits are marked with SPTE_TDP_AD_DISABLED;
16962306a36Sopenharmony_ci * shadow_acc_track_mask is the set of bits to be cleared in non-accessed
17062306a36Sopenharmony_ci * pages.
17162306a36Sopenharmony_ci */
17262306a36Sopenharmony_ciextern u64 __read_mostly shadow_acc_track_mask;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci/*
17562306a36Sopenharmony_ci * This mask must be set on all non-zero Non-Present or Reserved SPTEs in order
17662306a36Sopenharmony_ci * to guard against L1TF attacks.
17762306a36Sopenharmony_ci */
17862306a36Sopenharmony_ciextern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/*
18162306a36Sopenharmony_ci * The number of high-order 1 bits to use in the mask above.
18262306a36Sopenharmony_ci */
18362306a36Sopenharmony_ci#define SHADOW_NONPRESENT_OR_RSVD_MASK_LEN 5
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci/*
18662306a36Sopenharmony_ci * If a thread running without exclusive control of the MMU lock must perform a
18762306a36Sopenharmony_ci * multi-part operation on an SPTE, it can set the SPTE to REMOVED_SPTE as a
18862306a36Sopenharmony_ci * non-present intermediate value. Other threads which encounter this value
18962306a36Sopenharmony_ci * should not modify the SPTE.
19062306a36Sopenharmony_ci *
19162306a36Sopenharmony_ci * Use a semi-arbitrary value that doesn't set RWX bits, i.e. is not-present on
19262306a36Sopenharmony_ci * both AMD and Intel CPUs, and doesn't set PFN bits, i.e. doesn't create a L1TF
19362306a36Sopenharmony_ci * vulnerability.  Use only low bits to avoid 64-bit immediates.
19462306a36Sopenharmony_ci *
19562306a36Sopenharmony_ci * Only used by the TDP MMU.
19662306a36Sopenharmony_ci */
19762306a36Sopenharmony_ci#define REMOVED_SPTE	0x5a0ULL
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci/* Removed SPTEs must not be misconstrued as shadow present PTEs. */
20062306a36Sopenharmony_cistatic_assert(!(REMOVED_SPTE & SPTE_MMU_PRESENT_MASK));
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_cistatic inline bool is_removed_spte(u64 spte)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	return spte == REMOVED_SPTE;
20562306a36Sopenharmony_ci}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci/* Get an SPTE's index into its parent's page table (and the spt array). */
20862306a36Sopenharmony_cistatic inline int spte_index(u64 *sptep)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	return ((unsigned long)sptep / sizeof(*sptep)) & (SPTE_ENT_PER_PAGE - 1);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/*
21462306a36Sopenharmony_ci * In some cases, we need to preserve the GFN of a non-present or reserved
21562306a36Sopenharmony_ci * SPTE when we usurp the upper five bits of the physical address space to
21662306a36Sopenharmony_ci * defend against L1TF, e.g. for MMIO SPTEs.  To preserve the GFN, we'll
21762306a36Sopenharmony_ci * shift bits of the GFN that overlap with shadow_nonpresent_or_rsvd_mask
21862306a36Sopenharmony_ci * left into the reserved bits, i.e. the GFN in the SPTE will be split into
21962306a36Sopenharmony_ci * high and low parts.  This mask covers the lower bits of the GFN.
22062306a36Sopenharmony_ci */
22162306a36Sopenharmony_ciextern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_cistatic inline struct kvm_mmu_page *to_shadow_page(hpa_t shadow_page)
22462306a36Sopenharmony_ci{
22562306a36Sopenharmony_ci	struct page *page = pfn_to_page((shadow_page) >> PAGE_SHIFT);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	return (struct kvm_mmu_page *)page_private(page);
22862306a36Sopenharmony_ci}
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_cistatic inline struct kvm_mmu_page *spte_to_child_sp(u64 spte)
23162306a36Sopenharmony_ci{
23262306a36Sopenharmony_ci	return to_shadow_page(spte & SPTE_BASE_ADDR_MASK);
23362306a36Sopenharmony_ci}
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_cistatic inline struct kvm_mmu_page *sptep_to_sp(u64 *sptep)
23662306a36Sopenharmony_ci{
23762306a36Sopenharmony_ci	return to_shadow_page(__pa(sptep));
23862306a36Sopenharmony_ci}
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_cistatic inline struct kvm_mmu_page *root_to_sp(hpa_t root)
24162306a36Sopenharmony_ci{
24262306a36Sopenharmony_ci	if (kvm_mmu_is_dummy_root(root))
24362306a36Sopenharmony_ci		return NULL;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	/*
24662306a36Sopenharmony_ci	 * The "root" may be a special root, e.g. a PAE entry, treat it as a
24762306a36Sopenharmony_ci	 * SPTE to ensure any non-PA bits are dropped.
24862306a36Sopenharmony_ci	 */
24962306a36Sopenharmony_ci	return spte_to_child_sp(root);
25062306a36Sopenharmony_ci}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_cistatic inline bool is_mmio_spte(u64 spte)
25362306a36Sopenharmony_ci{
25462306a36Sopenharmony_ci	return (spte & shadow_mmio_mask) == shadow_mmio_value &&
25562306a36Sopenharmony_ci	       likely(enable_mmio_caching);
25662306a36Sopenharmony_ci}
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_cistatic inline bool is_shadow_present_pte(u64 pte)
25962306a36Sopenharmony_ci{
26062306a36Sopenharmony_ci	return !!(pte & SPTE_MMU_PRESENT_MASK);
26162306a36Sopenharmony_ci}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci/*
26462306a36Sopenharmony_ci * Returns true if A/D bits are supported in hardware and are enabled by KVM.
26562306a36Sopenharmony_ci * When enabled, KVM uses A/D bits for all non-nested MMUs.  Because L1 can
26662306a36Sopenharmony_ci * disable A/D bits in EPTP12, SP and SPTE variants are needed to handle the
26762306a36Sopenharmony_ci * scenario where KVM is using A/D bits for L1, but not L2.
26862306a36Sopenharmony_ci */
26962306a36Sopenharmony_cistatic inline bool kvm_ad_enabled(void)
27062306a36Sopenharmony_ci{
27162306a36Sopenharmony_ci	return !!shadow_accessed_mask;
27262306a36Sopenharmony_ci}
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_cistatic inline bool sp_ad_disabled(struct kvm_mmu_page *sp)
27562306a36Sopenharmony_ci{
27662306a36Sopenharmony_ci	return sp->role.ad_disabled;
27762306a36Sopenharmony_ci}
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_cistatic inline bool spte_ad_enabled(u64 spte)
28062306a36Sopenharmony_ci{
28162306a36Sopenharmony_ci	KVM_MMU_WARN_ON(!is_shadow_present_pte(spte));
28262306a36Sopenharmony_ci	return (spte & SPTE_TDP_AD_MASK) != SPTE_TDP_AD_DISABLED;
28362306a36Sopenharmony_ci}
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_cistatic inline bool spte_ad_need_write_protect(u64 spte)
28662306a36Sopenharmony_ci{
28762306a36Sopenharmony_ci	KVM_MMU_WARN_ON(!is_shadow_present_pte(spte));
28862306a36Sopenharmony_ci	/*
28962306a36Sopenharmony_ci	 * This is benign for non-TDP SPTEs as SPTE_TDP_AD_ENABLED is '0',
29062306a36Sopenharmony_ci	 * and non-TDP SPTEs will never set these bits.  Optimize for 64-bit
29162306a36Sopenharmony_ci	 * TDP and do the A/D type check unconditionally.
29262306a36Sopenharmony_ci	 */
29362306a36Sopenharmony_ci	return (spte & SPTE_TDP_AD_MASK) != SPTE_TDP_AD_ENABLED;
29462306a36Sopenharmony_ci}
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_cistatic inline u64 spte_shadow_accessed_mask(u64 spte)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	KVM_MMU_WARN_ON(!is_shadow_present_pte(spte));
29962306a36Sopenharmony_ci	return spte_ad_enabled(spte) ? shadow_accessed_mask : 0;
30062306a36Sopenharmony_ci}
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_cistatic inline u64 spte_shadow_dirty_mask(u64 spte)
30362306a36Sopenharmony_ci{
30462306a36Sopenharmony_ci	KVM_MMU_WARN_ON(!is_shadow_present_pte(spte));
30562306a36Sopenharmony_ci	return spte_ad_enabled(spte) ? shadow_dirty_mask : 0;
30662306a36Sopenharmony_ci}
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_cistatic inline bool is_access_track_spte(u64 spte)
30962306a36Sopenharmony_ci{
31062306a36Sopenharmony_ci	return !spte_ad_enabled(spte) && (spte & shadow_acc_track_mask) == 0;
31162306a36Sopenharmony_ci}
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_cistatic inline bool is_large_pte(u64 pte)
31462306a36Sopenharmony_ci{
31562306a36Sopenharmony_ci	return pte & PT_PAGE_SIZE_MASK;
31662306a36Sopenharmony_ci}
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_cistatic inline bool is_last_spte(u64 pte, int level)
31962306a36Sopenharmony_ci{
32062306a36Sopenharmony_ci	return (level == PG_LEVEL_4K) || is_large_pte(pte);
32162306a36Sopenharmony_ci}
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_cistatic inline bool is_executable_pte(u64 spte)
32462306a36Sopenharmony_ci{
32562306a36Sopenharmony_ci	return (spte & (shadow_x_mask | shadow_nx_mask)) == shadow_x_mask;
32662306a36Sopenharmony_ci}
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_cistatic inline kvm_pfn_t spte_to_pfn(u64 pte)
32962306a36Sopenharmony_ci{
33062306a36Sopenharmony_ci	return (pte & SPTE_BASE_ADDR_MASK) >> PAGE_SHIFT;
33162306a36Sopenharmony_ci}
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_cistatic inline bool is_accessed_spte(u64 spte)
33462306a36Sopenharmony_ci{
33562306a36Sopenharmony_ci	u64 accessed_mask = spte_shadow_accessed_mask(spte);
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	return accessed_mask ? spte & accessed_mask
33862306a36Sopenharmony_ci			     : !is_access_track_spte(spte);
33962306a36Sopenharmony_ci}
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_cistatic inline bool is_dirty_spte(u64 spte)
34262306a36Sopenharmony_ci{
34362306a36Sopenharmony_ci	u64 dirty_mask = spte_shadow_dirty_mask(spte);
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	return dirty_mask ? spte & dirty_mask : spte & PT_WRITABLE_MASK;
34662306a36Sopenharmony_ci}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_cistatic inline u64 get_rsvd_bits(struct rsvd_bits_validate *rsvd_check, u64 pte,
34962306a36Sopenharmony_ci				int level)
35062306a36Sopenharmony_ci{
35162306a36Sopenharmony_ci	int bit7 = (pte >> 7) & 1;
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	return rsvd_check->rsvd_bits_mask[bit7][level-1];
35462306a36Sopenharmony_ci}
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_cistatic inline bool __is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check,
35762306a36Sopenharmony_ci				      u64 pte, int level)
35862306a36Sopenharmony_ci{
35962306a36Sopenharmony_ci	return pte & get_rsvd_bits(rsvd_check, pte, level);
36062306a36Sopenharmony_ci}
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_cistatic inline bool __is_bad_mt_xwr(struct rsvd_bits_validate *rsvd_check,
36362306a36Sopenharmony_ci				   u64 pte)
36462306a36Sopenharmony_ci{
36562306a36Sopenharmony_ci	return rsvd_check->bad_mt_xwr & BIT_ULL(pte & 0x3f);
36662306a36Sopenharmony_ci}
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_cistatic __always_inline bool is_rsvd_spte(struct rsvd_bits_validate *rsvd_check,
36962306a36Sopenharmony_ci					 u64 spte, int level)
37062306a36Sopenharmony_ci{
37162306a36Sopenharmony_ci	return __is_bad_mt_xwr(rsvd_check, spte) ||
37262306a36Sopenharmony_ci	       __is_rsvd_bits_set(rsvd_check, spte, level);
37362306a36Sopenharmony_ci}
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci/*
37662306a36Sopenharmony_ci * A shadow-present leaf SPTE may be non-writable for 4 possible reasons:
37762306a36Sopenharmony_ci *
37862306a36Sopenharmony_ci *  1. To intercept writes for dirty logging. KVM write-protects huge pages
37962306a36Sopenharmony_ci *     so that they can be split down into the dirty logging
38062306a36Sopenharmony_ci *     granularity (4KiB) whenever the guest writes to them. KVM also
38162306a36Sopenharmony_ci *     write-protects 4KiB pages so that writes can be recorded in the dirty log
38262306a36Sopenharmony_ci *     (e.g. if not using PML). SPTEs are write-protected for dirty logging
38362306a36Sopenharmony_ci *     during the VM-iotcls that enable dirty logging.
38462306a36Sopenharmony_ci *
38562306a36Sopenharmony_ci *  2. To intercept writes to guest page tables that KVM is shadowing. When a
38662306a36Sopenharmony_ci *     guest writes to its page table the corresponding shadow page table will
38762306a36Sopenharmony_ci *     be marked "unsync". That way KVM knows which shadow page tables need to
38862306a36Sopenharmony_ci *     be updated on the next TLB flush, INVLPG, etc. and which do not.
38962306a36Sopenharmony_ci *
39062306a36Sopenharmony_ci *  3. To prevent guest writes to read-only memory, such as for memory in a
39162306a36Sopenharmony_ci *     read-only memslot or guest memory backed by a read-only VMA. Writes to
39262306a36Sopenharmony_ci *     such pages are disallowed entirely.
39362306a36Sopenharmony_ci *
39462306a36Sopenharmony_ci *  4. To emulate the Accessed bit for SPTEs without A/D bits.  Note, in this
39562306a36Sopenharmony_ci *     case, the SPTE is access-protected, not just write-protected!
39662306a36Sopenharmony_ci *
39762306a36Sopenharmony_ci * For cases #1 and #4, KVM can safely make such SPTEs writable without taking
39862306a36Sopenharmony_ci * mmu_lock as capturing the Accessed/Dirty state doesn't require taking it.
39962306a36Sopenharmony_ci * To differentiate #1 and #4 from #2 and #3, KVM uses two software-only bits
40062306a36Sopenharmony_ci * in the SPTE:
40162306a36Sopenharmony_ci *
40262306a36Sopenharmony_ci *  shadow_mmu_writable_mask, aka MMU-writable -
40362306a36Sopenharmony_ci *    Cleared on SPTEs that KVM is currently write-protecting for shadow paging
40462306a36Sopenharmony_ci *    purposes (case 2 above).
40562306a36Sopenharmony_ci *
40662306a36Sopenharmony_ci *  shadow_host_writable_mask, aka Host-writable -
40762306a36Sopenharmony_ci *    Cleared on SPTEs that are not host-writable (case 3 above)
40862306a36Sopenharmony_ci *
40962306a36Sopenharmony_ci * Note, not all possible combinations of PT_WRITABLE_MASK,
41062306a36Sopenharmony_ci * shadow_mmu_writable_mask, and shadow_host_writable_mask are valid. A given
41162306a36Sopenharmony_ci * SPTE can be in only one of the following states, which map to the
41262306a36Sopenharmony_ci * aforementioned 3 cases:
41362306a36Sopenharmony_ci *
41462306a36Sopenharmony_ci *   shadow_host_writable_mask | shadow_mmu_writable_mask | PT_WRITABLE_MASK
41562306a36Sopenharmony_ci *   ------------------------- | ------------------------ | ----------------
41662306a36Sopenharmony_ci *   1                         | 1                        | 1       (writable)
41762306a36Sopenharmony_ci *   1                         | 1                        | 0       (case 1)
41862306a36Sopenharmony_ci *   1                         | 0                        | 0       (case 2)
41962306a36Sopenharmony_ci *   0                         | 0                        | 0       (case 3)
42062306a36Sopenharmony_ci *
42162306a36Sopenharmony_ci * The valid combinations of these bits are checked by
42262306a36Sopenharmony_ci * check_spte_writable_invariants() whenever an SPTE is modified.
42362306a36Sopenharmony_ci *
42462306a36Sopenharmony_ci * Clearing the MMU-writable bit is always done under the MMU lock and always
42562306a36Sopenharmony_ci * accompanied by a TLB flush before dropping the lock to avoid corrupting the
42662306a36Sopenharmony_ci * shadow page tables between vCPUs. Write-protecting an SPTE for dirty logging
42762306a36Sopenharmony_ci * (which does not clear the MMU-writable bit), does not flush TLBs before
42862306a36Sopenharmony_ci * dropping the lock, as it only needs to synchronize guest writes with the
42962306a36Sopenharmony_ci * dirty bitmap. Similarly, making the SPTE inaccessible (and non-writable) for
43062306a36Sopenharmony_ci * access-tracking via the clear_young() MMU notifier also does not flush TLBs.
43162306a36Sopenharmony_ci *
43262306a36Sopenharmony_ci * So, there is the problem: clearing the MMU-writable bit can encounter a
43362306a36Sopenharmony_ci * write-protected SPTE while CPUs still have writable mappings for that SPTE
43462306a36Sopenharmony_ci * cached in their TLB. To address this, KVM always flushes TLBs when
43562306a36Sopenharmony_ci * write-protecting SPTEs if the MMU-writable bit is set on the old SPTE.
43662306a36Sopenharmony_ci *
43762306a36Sopenharmony_ci * The Host-writable bit is not modified on present SPTEs, it is only set or
43862306a36Sopenharmony_ci * cleared when an SPTE is first faulted in from non-present and then remains
43962306a36Sopenharmony_ci * immutable.
44062306a36Sopenharmony_ci */
44162306a36Sopenharmony_cistatic inline bool is_writable_pte(unsigned long pte)
44262306a36Sopenharmony_ci{
44362306a36Sopenharmony_ci	return pte & PT_WRITABLE_MASK;
44462306a36Sopenharmony_ci}
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci/* Note: spte must be a shadow-present leaf SPTE. */
44762306a36Sopenharmony_cistatic inline void check_spte_writable_invariants(u64 spte)
44862306a36Sopenharmony_ci{
44962306a36Sopenharmony_ci	if (spte & shadow_mmu_writable_mask)
45062306a36Sopenharmony_ci		WARN_ONCE(!(spte & shadow_host_writable_mask),
45162306a36Sopenharmony_ci			  KBUILD_MODNAME ": MMU-writable SPTE is not Host-writable: %llx",
45262306a36Sopenharmony_ci			  spte);
45362306a36Sopenharmony_ci	else
45462306a36Sopenharmony_ci		WARN_ONCE(is_writable_pte(spte),
45562306a36Sopenharmony_ci			  KBUILD_MODNAME ": Writable SPTE is not MMU-writable: %llx", spte);
45662306a36Sopenharmony_ci}
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_cistatic inline bool is_mmu_writable_spte(u64 spte)
45962306a36Sopenharmony_ci{
46062306a36Sopenharmony_ci	return spte & shadow_mmu_writable_mask;
46162306a36Sopenharmony_ci}
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_cistatic inline u64 get_mmio_spte_generation(u64 spte)
46462306a36Sopenharmony_ci{
46562306a36Sopenharmony_ci	u64 gen;
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	gen = (spte & MMIO_SPTE_GEN_LOW_MASK) >> MMIO_SPTE_GEN_LOW_SHIFT;
46862306a36Sopenharmony_ci	gen |= (spte & MMIO_SPTE_GEN_HIGH_MASK) >> MMIO_SPTE_GEN_HIGH_SHIFT;
46962306a36Sopenharmony_ci	return gen;
47062306a36Sopenharmony_ci}
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_cibool spte_has_volatile_bits(u64 spte);
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_cibool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
47562306a36Sopenharmony_ci	       const struct kvm_memory_slot *slot,
47662306a36Sopenharmony_ci	       unsigned int pte_access, gfn_t gfn, kvm_pfn_t pfn,
47762306a36Sopenharmony_ci	       u64 old_spte, bool prefetch, bool can_unsync,
47862306a36Sopenharmony_ci	       bool host_writable, u64 *new_spte);
47962306a36Sopenharmony_ciu64 make_huge_page_split_spte(struct kvm *kvm, u64 huge_spte,
48062306a36Sopenharmony_ci		      	      union kvm_mmu_page_role role, int index);
48162306a36Sopenharmony_ciu64 make_nonleaf_spte(u64 *child_pt, bool ad_disabled);
48262306a36Sopenharmony_ciu64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access);
48362306a36Sopenharmony_ciu64 mark_spte_for_access_track(u64 spte);
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci/* Restore an acc-track PTE back to a regular PTE */
48662306a36Sopenharmony_cistatic inline u64 restore_acc_track_spte(u64 spte)
48762306a36Sopenharmony_ci{
48862306a36Sopenharmony_ci	u64 saved_bits = (spte >> SHADOW_ACC_TRACK_SAVED_BITS_SHIFT)
48962306a36Sopenharmony_ci			 & SHADOW_ACC_TRACK_SAVED_BITS_MASK;
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	spte &= ~shadow_acc_track_mask;
49262306a36Sopenharmony_ci	spte &= ~(SHADOW_ACC_TRACK_SAVED_BITS_MASK <<
49362306a36Sopenharmony_ci		  SHADOW_ACC_TRACK_SAVED_BITS_SHIFT);
49462306a36Sopenharmony_ci	spte |= saved_bits;
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci	return spte;
49762306a36Sopenharmony_ci}
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ciu64 kvm_mmu_changed_pte_notifier_make_spte(u64 old_spte, kvm_pfn_t new_pfn);
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_civoid __init kvm_mmu_spte_module_init(void);
50262306a36Sopenharmony_civoid kvm_mmu_reset_all_pte_masks(void);
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci#endif
505