xref: /openbmc/linux/arch/x86/kvm/mmu.h (revision cf9f4c0eb1699d306e348b1fd0225af7b2c282d3)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2edf88417SAvi Kivity #ifndef __KVM_X86_MMU_H
3edf88417SAvi Kivity #define __KVM_X86_MMU_H
4edf88417SAvi Kivity 
5edf88417SAvi Kivity #include <linux/kvm_host.h>
6fc78f519SAvi Kivity #include "kvm_cache_regs.h"
789786147SMohammed Gamal #include "cpuid.h"
8edf88417SAvi Kivity 
90c29397aSSean Christopherson extern bool __read_mostly enable_mmio_caching;
100c29397aSSean Christopherson 
118c6d6adcSSheng Yang #define PT_WRITABLE_SHIFT 1
12be94f6b7SHuaitong Han #define PT_USER_SHIFT 2
138c6d6adcSSheng Yang 
148c6d6adcSSheng Yang #define PT_PRESENT_MASK (1ULL << 0)
158c6d6adcSSheng Yang #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
16be94f6b7SHuaitong Han #define PT_USER_MASK (1ULL << PT_USER_SHIFT)
178c6d6adcSSheng Yang #define PT_PWT_MASK (1ULL << 3)
188c6d6adcSSheng Yang #define PT_PCD_MASK (1ULL << 4)
191b7fcd32SAvi Kivity #define PT_ACCESSED_SHIFT 5
201b7fcd32SAvi Kivity #define PT_ACCESSED_MASK (1ULL << PT_ACCESSED_SHIFT)
218ea667f2SAvi Kivity #define PT_DIRTY_SHIFT 6
228ea667f2SAvi Kivity #define PT_DIRTY_MASK (1ULL << PT_DIRTY_SHIFT)
236fd01b71SAvi Kivity #define PT_PAGE_SIZE_SHIFT 7
246fd01b71SAvi Kivity #define PT_PAGE_SIZE_MASK (1ULL << PT_PAGE_SIZE_SHIFT)
258c6d6adcSSheng Yang #define PT_PAT_MASK (1ULL << 7)
268c6d6adcSSheng Yang #define PT_GLOBAL_MASK (1ULL << 8)
278c6d6adcSSheng Yang #define PT64_NX_SHIFT 63
288c6d6adcSSheng Yang #define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
298c6d6adcSSheng Yang 
308c6d6adcSSheng Yang #define PT_PAT_SHIFT 7
318c6d6adcSSheng Yang #define PT_DIR_PAT_SHIFT 12
328c6d6adcSSheng Yang #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
338c6d6adcSSheng Yang 
34855feb67SYu Zhang #define PT64_ROOT_5LEVEL 5
352a7266a8SYu Zhang #define PT64_ROOT_4LEVEL 4
368c6d6adcSSheng Yang #define PT32_ROOT_LEVEL 2
378c6d6adcSSheng Yang #define PT32E_ROOT_LEVEL 3
388c6d6adcSSheng Yang 
39a91a7c70SLai Jiangshan #define KVM_MMU_CR4_ROLE_BITS (X86_CR4_PSE | X86_CR4_PAE | X86_CR4_LA57 | \
40a91a7c70SLai Jiangshan 			       X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE)
4120f632bdSSean Christopherson 
4220f632bdSSean Christopherson #define KVM_MMU_CR0_ROLE_BITS (X86_CR0_PG | X86_CR0_WP)
43d6174299SPaolo Bonzini #define KVM_MMU_EFER_ROLE_BITS (EFER_LME | EFER_NX)
4420f632bdSSean Christopherson 
45eb79cd00SSean Christopherson static __always_inline u64 rsvd_bits(int s, int e)
46d1431483STiejun Chen {
47eb79cd00SSean Christopherson 	BUILD_BUG_ON(__builtin_constant_p(e) && __builtin_constant_p(s) && e < s);
48eb79cd00SSean Christopherson 
49eb79cd00SSean Christopherson 	if (__builtin_constant_p(e))
50eb79cd00SSean Christopherson 		BUILD_BUG_ON(e > 63);
51eb79cd00SSean Christopherson 	else
52eb79cd00SSean Christopherson 		e &= 63;
53eb79cd00SSean Christopherson 
54d1cd3ce9SYu Zhang 	if (e < s)
55d1cd3ce9SYu Zhang 		return 0;
56d1cd3ce9SYu Zhang 
572f80d502SPaolo Bonzini 	return ((2ULL << (e - s)) - 1) << s;
58d1431483STiejun Chen }
59d1431483STiejun Chen 
6086931ff7SSean Christopherson /*
6186931ff7SSean Christopherson  * The number of non-reserved physical address bits irrespective of features
6286931ff7SSean Christopherson  * that repurpose legal bits, e.g. MKTME.
6386931ff7SSean Christopherson  */
6486931ff7SSean Christopherson extern u8 __read_mostly shadow_phys_bits;
6586931ff7SSean Christopherson 
6686931ff7SSean Christopherson static inline gfn_t kvm_mmu_max_gfn(void)
6786931ff7SSean Christopherson {
6886931ff7SSean Christopherson 	/*
6986931ff7SSean Christopherson 	 * Note that this uses the host MAXPHYADDR, not the guest's.
7086931ff7SSean Christopherson 	 * EPT/NPT cannot support GPAs that would exceed host.MAXPHYADDR;
7186931ff7SSean Christopherson 	 * assuming KVM is running on bare metal, guest accesses beyond
7286931ff7SSean Christopherson 	 * host.MAXPHYADDR will hit a #PF(RSVD) and never cause a vmexit
7386931ff7SSean Christopherson 	 * (either EPT Violation/Misconfig or #NPF), and so KVM will never
7486931ff7SSean Christopherson 	 * install a SPTE for such addresses.  If KVM is running as a VM
7586931ff7SSean Christopherson 	 * itself, on the other hand, it might see a MAXPHYADDR that is less
7686931ff7SSean Christopherson 	 * than hardware's real MAXPHYADDR.  Using the host MAXPHYADDR
7786931ff7SSean Christopherson 	 * disallows such SPTEs entirely and simplifies the TDP MMU.
7886931ff7SSean Christopherson 	 */
7986931ff7SSean Christopherson 	int max_gpa_bits = likely(tdp_enabled) ? shadow_phys_bits : 52;
8086931ff7SSean Christopherson 
8186931ff7SSean Christopherson 	return (1ULL << (max_gpa_bits - PAGE_SHIFT)) - 1;
8286931ff7SSean Christopherson }
8386931ff7SSean Christopherson 
843c5c3245SKai Huang static inline u8 kvm_get_shadow_phys_bits(void)
853c5c3245SKai Huang {
863c5c3245SKai Huang 	/*
873c5c3245SKai Huang 	 * boot_cpu_data.x86_phys_bits is reduced when MKTME or SME are detected
883c5c3245SKai Huang 	 * in CPU detection code, but the processor treats those reduced bits as
893c5c3245SKai Huang 	 * 'keyID' thus they are not reserved bits. Therefore KVM needs to look at
903c5c3245SKai Huang 	 * the physical address bits reported by CPUID.
913c5c3245SKai Huang 	 */
923c5c3245SKai Huang 	if (likely(boot_cpu_data.extended_cpuid_level >= 0x80000008))
933c5c3245SKai Huang 		return cpuid_eax(0x80000008) & 0xff;
943c5c3245SKai Huang 
953c5c3245SKai Huang 	/*
963c5c3245SKai Huang 	 * Quite weird to have VMX or SVM but not MAXPHYADDR; probably a VM with
973c5c3245SKai Huang 	 * custom CPUID.  Proceed with whatever the kernel found since these features
983c5c3245SKai Huang 	 * aren't virtualizable (SME/SEV also require CPUIDs higher than 0x80000008).
993c5c3245SKai Huang 	 */
1003c5c3245SKai Huang 	return boot_cpu_data.x86_phys_bits;
1013c5c3245SKai Huang }
1023c5c3245SKai Huang 
1038120337aSSean Christopherson void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask);
104e54f1ff2SKai Huang void kvm_mmu_set_me_spte_mask(u64 me_value, u64 me_mask);
105e7b7bdeaSSean Christopherson void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only);
106b37fbea6SXiao Guangrong 
107c9060662SSean Christopherson void kvm_init_mmu(struct kvm_vcpu *vcpu);
108dbc4739bSSean Christopherson void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr0,
109dbc4739bSSean Christopherson 			     unsigned long cr4, u64 efer, gpa_t nested_cr3);
110ae1e2d10SPaolo Bonzini void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
111cc022ae1SLai Jiangshan 			     int huge_page_level, bool accessed_dirty,
112cc022ae1SLai Jiangshan 			     gpa_t new_eptp);
1139bc1f09fSWanpeng Li bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu);
1141261bfa3SWanpeng Li int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
115d0006530SPaolo Bonzini 				u64 fault_address, char *insn, int insn_len);
116*cf9f4c0eSSean Christopherson void __kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,
117*cf9f4c0eSSean Christopherson 					struct kvm_mmu *mmu);
11894d8b056SMarcelo Tosatti 
11961a1773eSSean Christopherson int kvm_mmu_load(struct kvm_vcpu *vcpu);
12061a1773eSSean Christopherson void kvm_mmu_unload(struct kvm_vcpu *vcpu);
121527d5cd7SSean Christopherson void kvm_mmu_free_obsolete_roots(struct kvm_vcpu *vcpu);
12261a1773eSSean Christopherson void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
12361b05a9fSLai Jiangshan void kvm_mmu_sync_prev_roots(struct kvm_vcpu *vcpu);
12461a1773eSSean Christopherson 
125edf88417SAvi Kivity static inline int kvm_mmu_reload(struct kvm_vcpu *vcpu)
126edf88417SAvi Kivity {
127b9e5603cSPaolo Bonzini 	if (likely(vcpu->arch.mmu->root.hpa != INVALID_PAGE))
128edf88417SAvi Kivity 		return 0;
129edf88417SAvi Kivity 
130edf88417SAvi Kivity 	return kvm_mmu_load(vcpu);
131edf88417SAvi Kivity }
132edf88417SAvi Kivity 
133c9470a2eSJunaid Shahid static inline unsigned long kvm_get_pcid(struct kvm_vcpu *vcpu, gpa_t cr3)
134c9470a2eSJunaid Shahid {
135c9470a2eSJunaid Shahid 	BUILD_BUG_ON((X86_CR3_PCID_MASK & PAGE_MASK) != 0);
136c9470a2eSJunaid Shahid 
137607475cfSBinbin Wu 	return kvm_is_cr4_bit_set(vcpu, X86_CR4_PCIDE)
138c9470a2eSJunaid Shahid 	       ? cr3 & X86_CR3_PCID_MASK
139c9470a2eSJunaid Shahid 	       : 0;
140c9470a2eSJunaid Shahid }
141c9470a2eSJunaid Shahid 
142c9470a2eSJunaid Shahid static inline unsigned long kvm_get_active_pcid(struct kvm_vcpu *vcpu)
143c9470a2eSJunaid Shahid {
144c9470a2eSJunaid Shahid 	return kvm_get_pcid(vcpu, kvm_read_cr3(vcpu));
145c9470a2eSJunaid Shahid }
146c9470a2eSJunaid Shahid 
147689f3bf2SPaolo Bonzini static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu)
1486e42782fSJunaid Shahid {
149b9e5603cSPaolo Bonzini 	u64 root_hpa = vcpu->arch.mmu->root.hpa;
1502a40b900SSean Christopherson 
1512a40b900SSean Christopherson 	if (!VALID_PAGE(root_hpa))
1522a40b900SSean Christopherson 		return;
1532a40b900SSean Christopherson 
154e83bc09cSSean Christopherson 	static_call(kvm_x86_load_mmu_pgd)(vcpu, root_hpa,
155a972e29cSPaolo Bonzini 					  vcpu->arch.mmu->root_role.level);
1566e42782fSJunaid Shahid }
1576e42782fSJunaid Shahid 
158*cf9f4c0eSSean Christopherson static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,
159*cf9f4c0eSSean Christopherson 						    struct kvm_mmu *mmu)
160*cf9f4c0eSSean Christopherson {
161*cf9f4c0eSSean Christopherson 	/*
162*cf9f4c0eSSean Christopherson 	 * When EPT is enabled, KVM may passthrough CR0.WP to the guest, i.e.
163*cf9f4c0eSSean Christopherson 	 * @mmu's snapshot of CR0.WP and thus all related paging metadata may
164*cf9f4c0eSSean Christopherson 	 * be stale.  Refresh CR0.WP and the metadata on-demand when checking
165*cf9f4c0eSSean Christopherson 	 * for permission faults.  Exempt nested MMUs, i.e. MMUs for shadowing
166*cf9f4c0eSSean Christopherson 	 * nEPT and nNPT, as CR0.WP is ignored in both cases.  Note, KVM does
167*cf9f4c0eSSean Christopherson 	 * need to refresh nested_mmu, a.k.a. the walker used to translate L2
168*cf9f4c0eSSean Christopherson 	 * GVAs to GPAs, as that "MMU" needs to honor L2's CR0.WP.
169*cf9f4c0eSSean Christopherson 	 */
170*cf9f4c0eSSean Christopherson 	if (!tdp_enabled || mmu == &vcpu->arch.guest_mmu)
171*cf9f4c0eSSean Christopherson 		return;
172*cf9f4c0eSSean Christopherson 
173*cf9f4c0eSSean Christopherson 	__kvm_mmu_refresh_passthrough_bits(vcpu, mmu);
174*cf9f4c0eSSean Christopherson }
175*cf9f4c0eSSean Christopherson 
176198c74f4SXiao Guangrong /*
177f13577e8SPaolo Bonzini  * Check if a given access (described through the I/D, W/R and U/S bits of a
178f13577e8SPaolo Bonzini  * page fault error code pfec) causes a permission fault with the given PTE
179f13577e8SPaolo Bonzini  * access rights (in ACC_* format).
180f13577e8SPaolo Bonzini  *
181f13577e8SPaolo Bonzini  * Return zero if the access does not fault; return the page fault error code
182f13577e8SPaolo Bonzini  * if the access faults.
18397d64b78SAvi Kivity  */
184f13577e8SPaolo Bonzini static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
185be94f6b7SHuaitong Han 				  unsigned pte_access, unsigned pte_pkey,
1865b22bbe7SLai Jiangshan 				  u64 access)
187bebb106aSXiao Guangrong {
1885b22bbe7SLai Jiangshan 	/* strip nested paging fault error codes */
1895b22bbe7SLai Jiangshan 	unsigned int pfec = access;
190b3646477SJason Baron 	unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu);
19197ec8c06SFeng Wu 
19297ec8c06SFeng Wu 	/*
1934f4aa80eSLai Jiangshan 	 * For explicit supervisor accesses, SMAP is disabled if EFLAGS.AC = 1.
1944f4aa80eSLai Jiangshan 	 * For implicit supervisor accesses, SMAP cannot be overridden.
19597ec8c06SFeng Wu 	 *
1964f4aa80eSLai Jiangshan 	 * SMAP works on supervisor accesses only, and not_smap can
1974f4aa80eSLai Jiangshan 	 * be set or not set when user access with neither has any bearing
1984f4aa80eSLai Jiangshan 	 * on the result.
19997ec8c06SFeng Wu 	 *
2004f4aa80eSLai Jiangshan 	 * We put the SMAP checking bit in place of the PFERR_RSVD_MASK bit;
2014f4aa80eSLai Jiangshan 	 * this bit will always be zero in pfec, but it will be one in index
2024f4aa80eSLai Jiangshan 	 * if SMAP checks are being disabled.
20397ec8c06SFeng Wu 	 */
2044f4aa80eSLai Jiangshan 	u64 implicit_access = access & PFERR_IMPLICIT_ACCESS;
2054f4aa80eSLai Jiangshan 	bool not_smap = ((rflags & X86_EFLAGS_AC) | implicit_access) == X86_EFLAGS_AC;
2064f4aa80eSLai Jiangshan 	int index = (pfec + (not_smap << PFERR_RSVD_BIT)) >> 1;
2077a98205dSXiao Guangrong 	u32 errcode = PFERR_PRESENT_MASK;
208*cf9f4c0eSSean Christopherson 	bool fault;
209*cf9f4c0eSSean Christopherson 
210*cf9f4c0eSSean Christopherson 	kvm_mmu_refresh_passthrough_bits(vcpu, mmu);
211*cf9f4c0eSSean Christopherson 
212*cf9f4c0eSSean Christopherson 	fault = (mmu->permissions[index] >> pte_access) & 1;
21397ec8c06SFeng Wu 
214be94f6b7SHuaitong Han 	WARN_ON(pfec & (PFERR_PK_MASK | PFERR_RSVD_MASK));
215be94f6b7SHuaitong Han 	if (unlikely(mmu->pkru_mask)) {
216be94f6b7SHuaitong Han 		u32 pkru_bits, offset;
217be94f6b7SHuaitong Han 
218be94f6b7SHuaitong Han 		/*
219be94f6b7SHuaitong Han 		* PKRU defines 32 bits, there are 16 domains and 2
220be94f6b7SHuaitong Han 		* attribute bits per domain in pkru.  pte_pkey is the
221be94f6b7SHuaitong Han 		* index of the protection domain, so pte_pkey * 2 is
222be94f6b7SHuaitong Han 		* is the index of the first bit for the domain.
223be94f6b7SHuaitong Han 		*/
224b9dd21e1SPaolo Bonzini 		pkru_bits = (vcpu->arch.pkru >> (pte_pkey * 2)) & 3;
225be94f6b7SHuaitong Han 
226be94f6b7SHuaitong Han 		/* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */
2277a98205dSXiao Guangrong 		offset = (pfec & ~1) +
228be94f6b7SHuaitong Han 			((pte_access & PT_USER_MASK) << (PFERR_RSVD_BIT - PT_USER_SHIFT));
229be94f6b7SHuaitong Han 
230be94f6b7SHuaitong Han 		pkru_bits &= mmu->pkru_mask >> offset;
2317a98205dSXiao Guangrong 		errcode |= -pkru_bits & PFERR_PK_MASK;
232be94f6b7SHuaitong Han 		fault |= (pkru_bits != 0);
233be94f6b7SHuaitong Han 	}
234be94f6b7SHuaitong Han 
2357a98205dSXiao Guangrong 	return -(u32)fault & errcode;
236bebb106aSXiao Guangrong }
23797d64b78SAvi Kivity 
238efdfe536SXiao Guangrong void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end);
239547ffaedSXiao Guangrong 
2406ca9a6f3SSean Christopherson int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu);
2411aa9b957SJunaid Shahid 
2421aa9b957SJunaid Shahid int kvm_mmu_post_init_vm(struct kvm *kvm);
2431aa9b957SJunaid Shahid void kvm_mmu_pre_destroy_vm(struct kvm *kvm);
2441aa9b957SJunaid Shahid 
2451e76a3ceSDavid Stevens static inline bool kvm_shadow_root_allocated(struct kvm *kvm)
246e2209710SBen Gardon {
247d501f747SBen Gardon 	/*
2481e76a3ceSDavid Stevens 	 * Read shadow_root_allocated before related pointers. Hence, threads
2491e76a3ceSDavid Stevens 	 * reading shadow_root_allocated in any lock context are guaranteed to
2501e76a3ceSDavid Stevens 	 * see the pointers. Pairs with smp_store_release in
2511e76a3ceSDavid Stevens 	 * mmu_first_shadow_root_alloc.
252d501f747SBen Gardon 	 */
2531e76a3ceSDavid Stevens 	return smp_load_acquire(&kvm->arch.shadow_root_allocated);
2541e76a3ceSDavid Stevens }
2551e76a3ceSDavid Stevens 
2561e76a3ceSDavid Stevens #ifdef CONFIG_X86_64
2571f98f2bdSDavid Matlack extern bool tdp_mmu_enabled;
2581e76a3ceSDavid Stevens #else
2591f98f2bdSDavid Matlack #define tdp_mmu_enabled false
2601e76a3ceSDavid Stevens #endif
2611e76a3ceSDavid Stevens 
2621e76a3ceSDavid Stevens static inline bool kvm_memslots_have_rmaps(struct kvm *kvm)
2631e76a3ceSDavid Stevens {
2641f98f2bdSDavid Matlack 	return !tdp_mmu_enabled || kvm_shadow_root_allocated(kvm);
265e2209710SBen Gardon }
266e2209710SBen Gardon 
2674139b197SPeter Xu static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
2684139b197SPeter Xu {
2694139b197SPeter Xu 	/* KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K) must be 0. */
2704139b197SPeter Xu 	return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
2714139b197SPeter Xu 		(base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
2724139b197SPeter Xu }
2734139b197SPeter Xu 
2744139b197SPeter Xu static inline unsigned long
2754139b197SPeter Xu __kvm_mmu_slot_lpages(struct kvm_memory_slot *slot, unsigned long npages,
2764139b197SPeter Xu 		      int level)
2774139b197SPeter Xu {
2784139b197SPeter Xu 	return gfn_to_index(slot->base_gfn + npages - 1,
2794139b197SPeter Xu 			    slot->base_gfn, level) + 1;
2804139b197SPeter Xu }
2814139b197SPeter Xu 
2824139b197SPeter Xu static inline unsigned long
2834139b197SPeter Xu kvm_mmu_slot_lpages(struct kvm_memory_slot *slot, int level)
2844139b197SPeter Xu {
2854139b197SPeter Xu 	return __kvm_mmu_slot_lpages(slot, slot->npages, level);
2864139b197SPeter Xu }
2874139b197SPeter Xu 
28871f51d2cSMingwei Zhang static inline void kvm_update_page_stats(struct kvm *kvm, int level, int count)
28971f51d2cSMingwei Zhang {
29071f51d2cSMingwei Zhang 	atomic64_add(count, &kvm->stat.pages[level - 1]);
29171f51d2cSMingwei Zhang }
292c59a0f57SLai Jiangshan 
2935b22bbe7SLai Jiangshan gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u64 access,
294c59a0f57SLai Jiangshan 			   struct x86_exception *exception);
295c59a0f57SLai Jiangshan 
296c59a0f57SLai Jiangshan static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu,
297c59a0f57SLai Jiangshan 				      struct kvm_mmu *mmu,
2985b22bbe7SLai Jiangshan 				      gpa_t gpa, u64 access,
299c59a0f57SLai Jiangshan 				      struct x86_exception *exception)
300c59a0f57SLai Jiangshan {
301c59a0f57SLai Jiangshan 	if (mmu != &vcpu->arch.nested_mmu)
302c59a0f57SLai Jiangshan 		return gpa;
303c59a0f57SLai Jiangshan 	return translate_nested_gpa(vcpu, gpa, access, exception);
304c59a0f57SLai Jiangshan }
305edf88417SAvi Kivity #endif
306