xref: /openbmc/linux/arch/x86/kvm/mmu.h (revision 0f04a2ac4fe96bbf05b7ac7d3e94598db550d6b8)
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"
7edf88417SAvi Kivity 
88c6d6adcSSheng Yang #define PT64_PT_BITS 9
98c6d6adcSSheng Yang #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
108c6d6adcSSheng Yang #define PT32_PT_BITS 10
118c6d6adcSSheng Yang #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
128c6d6adcSSheng Yang 
138c6d6adcSSheng Yang #define PT_WRITABLE_SHIFT 1
14be94f6b7SHuaitong Han #define PT_USER_SHIFT 2
158c6d6adcSSheng Yang 
168c6d6adcSSheng Yang #define PT_PRESENT_MASK (1ULL << 0)
178c6d6adcSSheng Yang #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
18be94f6b7SHuaitong Han #define PT_USER_MASK (1ULL << PT_USER_SHIFT)
198c6d6adcSSheng Yang #define PT_PWT_MASK (1ULL << 3)
208c6d6adcSSheng Yang #define PT_PCD_MASK (1ULL << 4)
211b7fcd32SAvi Kivity #define PT_ACCESSED_SHIFT 5
221b7fcd32SAvi Kivity #define PT_ACCESSED_MASK (1ULL << PT_ACCESSED_SHIFT)
238ea667f2SAvi Kivity #define PT_DIRTY_SHIFT 6
248ea667f2SAvi Kivity #define PT_DIRTY_MASK (1ULL << PT_DIRTY_SHIFT)
256fd01b71SAvi Kivity #define PT_PAGE_SIZE_SHIFT 7
266fd01b71SAvi Kivity #define PT_PAGE_SIZE_MASK (1ULL << PT_PAGE_SIZE_SHIFT)
278c6d6adcSSheng Yang #define PT_PAT_MASK (1ULL << 7)
288c6d6adcSSheng Yang #define PT_GLOBAL_MASK (1ULL << 8)
298c6d6adcSSheng Yang #define PT64_NX_SHIFT 63
308c6d6adcSSheng Yang #define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
318c6d6adcSSheng Yang 
328c6d6adcSSheng Yang #define PT_PAT_SHIFT 7
338c6d6adcSSheng Yang #define PT_DIR_PAT_SHIFT 12
348c6d6adcSSheng Yang #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
358c6d6adcSSheng Yang 
368c6d6adcSSheng Yang #define PT32_DIR_PSE36_SIZE 4
378c6d6adcSSheng Yang #define PT32_DIR_PSE36_SHIFT 13
388c6d6adcSSheng Yang #define PT32_DIR_PSE36_MASK \
398c6d6adcSSheng Yang 	(((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
408c6d6adcSSheng Yang 
41855feb67SYu Zhang #define PT64_ROOT_5LEVEL 5
422a7266a8SYu Zhang #define PT64_ROOT_4LEVEL 4
438c6d6adcSSheng Yang #define PT32_ROOT_LEVEL 2
448c6d6adcSSheng Yang #define PT32E_ROOT_LEVEL 3
458c6d6adcSSheng Yang 
46d1431483STiejun Chen static inline u64 rsvd_bits(int s, int e)
47d1431483STiejun Chen {
48d1cd3ce9SYu Zhang 	if (e < s)
49d1cd3ce9SYu Zhang 		return 0;
50d1cd3ce9SYu Zhang 
51d1431483STiejun Chen 	return ((1ULL << (e - s + 1)) - 1) << s;
52d1431483STiejun Chen }
53d1431483STiejun Chen 
54e7581cacSPaolo Bonzini void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 access_mask);
55b37fbea6SXiao Guangrong 
56c258b62bSXiao Guangrong void
57c258b62bSXiao Guangrong reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context);
58c258b62bSXiao Guangrong 
591c53da3fSJunaid Shahid void kvm_init_mmu(struct kvm_vcpu *vcpu, bool reset_roots);
60*0f04a2acSVitaly Kuznetsov void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, u32 cr0, u32 cr4, u32 efer,
61*0f04a2acSVitaly Kuznetsov 			     gpa_t nested_cr3);
62ae1e2d10SPaolo Bonzini void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
6350c28f21SJunaid Shahid 			     bool accessed_dirty, gpa_t new_eptp);
649bc1f09fSWanpeng Li bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu);
651261bfa3SWanpeng Li int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
66d0006530SPaolo Bonzini 				u64 fault_address, char *insn, int insn_len);
6794d8b056SMarcelo Tosatti 
68edf88417SAvi Kivity static inline int kvm_mmu_reload(struct kvm_vcpu *vcpu)
69edf88417SAvi Kivity {
7044dd3ffaSVitaly Kuznetsov 	if (likely(vcpu->arch.mmu->root_hpa != INVALID_PAGE))
71edf88417SAvi Kivity 		return 0;
72edf88417SAvi Kivity 
73edf88417SAvi Kivity 	return kvm_mmu_load(vcpu);
74edf88417SAvi Kivity }
75edf88417SAvi Kivity 
76c9470a2eSJunaid Shahid static inline unsigned long kvm_get_pcid(struct kvm_vcpu *vcpu, gpa_t cr3)
77c9470a2eSJunaid Shahid {
78c9470a2eSJunaid Shahid 	BUILD_BUG_ON((X86_CR3_PCID_MASK & PAGE_MASK) != 0);
79c9470a2eSJunaid Shahid 
80c9470a2eSJunaid Shahid 	return kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE)
81c9470a2eSJunaid Shahid 	       ? cr3 & X86_CR3_PCID_MASK
82c9470a2eSJunaid Shahid 	       : 0;
83c9470a2eSJunaid Shahid }
84c9470a2eSJunaid Shahid 
85c9470a2eSJunaid Shahid static inline unsigned long kvm_get_active_pcid(struct kvm_vcpu *vcpu)
86c9470a2eSJunaid Shahid {
87c9470a2eSJunaid Shahid 	return kvm_get_pcid(vcpu, kvm_read_cr3(vcpu));
88c9470a2eSJunaid Shahid }
89c9470a2eSJunaid Shahid 
90689f3bf2SPaolo Bonzini static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu)
916e42782fSJunaid Shahid {
9244dd3ffaSVitaly Kuznetsov 	if (VALID_PAGE(vcpu->arch.mmu->root_hpa))
93afaf0b2fSSean Christopherson 		kvm_x86_ops.load_mmu_pgd(vcpu, vcpu->arch.mmu->root_hpa |
94c9470a2eSJunaid Shahid 					       kvm_get_active_pcid(vcpu));
956e42782fSJunaid Shahid }
966e42782fSJunaid Shahid 
977a02674dSSean Christopherson int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,
987a02674dSSean Christopherson 		       bool prefault);
997a02674dSSean Christopherson 
1007a02674dSSean Christopherson static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
1017a02674dSSean Christopherson 					u32 err, bool prefault)
1027a02674dSSean Christopherson {
1037a02674dSSean Christopherson #ifdef CONFIG_RETPOLINE
1047a02674dSSean Christopherson 	if (likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault))
1057a02674dSSean Christopherson 		return kvm_tdp_page_fault(vcpu, cr2_or_gpa, err, prefault);
1067a02674dSSean Christopherson #endif
1077a02674dSSean Christopherson 	return vcpu->arch.mmu->page_fault(vcpu, cr2_or_gpa, err, prefault);
1087a02674dSSean Christopherson }
1097a02674dSSean Christopherson 
110198c74f4SXiao Guangrong /*
111198c74f4SXiao Guangrong  * Currently, we have two sorts of write-protection, a) the first one
112198c74f4SXiao Guangrong  * write-protects guest page to sync the guest modification, b) another one is
113198c74f4SXiao Guangrong  * used to sync dirty bitmap when we do KVM_GET_DIRTY_LOG. The differences
114198c74f4SXiao Guangrong  * between these two sorts are:
115198c74f4SXiao Guangrong  * 1) the first case clears SPTE_MMU_WRITEABLE bit.
116198c74f4SXiao Guangrong  * 2) the first case requires flushing tlb immediately avoiding corrupting
117198c74f4SXiao Guangrong  *    shadow page table between all vcpus so it should be in the protection of
118198c74f4SXiao Guangrong  *    mmu-lock. And the another case does not need to flush tlb until returning
119198c74f4SXiao Guangrong  *    the dirty bitmap to userspace since it only write-protects the page
120198c74f4SXiao Guangrong  *    logged in the bitmap, that means the page in the dirty bitmap is not
121198c74f4SXiao Guangrong  *    missed, so it can flush tlb out of mmu-lock.
122198c74f4SXiao Guangrong  *
123198c74f4SXiao Guangrong  * So, there is the problem: the first case can meet the corrupted tlb caused
124198c74f4SXiao Guangrong  * by another case which write-protects pages but without flush tlb
125198c74f4SXiao Guangrong  * immediately. In order to making the first case be aware this problem we let
126198c74f4SXiao Guangrong  * it flush tlb if we try to write-protect a spte whose SPTE_MMU_WRITEABLE bit
127198c74f4SXiao Guangrong  * is set, it works since another case never touches SPTE_MMU_WRITEABLE bit.
128198c74f4SXiao Guangrong  *
129198c74f4SXiao Guangrong  * Anyway, whenever a spte is updated (only permission and status bits are
130198c74f4SXiao Guangrong  * changed) we need to check whether the spte with SPTE_MMU_WRITEABLE becomes
131198c74f4SXiao Guangrong  * readonly, if that happens, we need to flush tlb. Fortunately,
132198c74f4SXiao Guangrong  * mmu_spte_update() has already handled it perfectly.
133198c74f4SXiao Guangrong  *
134198c74f4SXiao Guangrong  * The rules to use SPTE_MMU_WRITEABLE and PT_WRITABLE_MASK:
135198c74f4SXiao Guangrong  * - if we want to see if it has writable tlb entry or if the spte can be
136198c74f4SXiao Guangrong  *   writable on the mmu mapping, check SPTE_MMU_WRITEABLE, this is the most
137198c74f4SXiao Guangrong  *   case, otherwise
138198c74f4SXiao Guangrong  * - if we fix page fault on the spte or do write-protection by dirty logging,
139198c74f4SXiao Guangrong  *   check PT_WRITABLE_MASK.
140198c74f4SXiao Guangrong  *
141198c74f4SXiao Guangrong  * TODO: introduce APIs to split these two cases.
142198c74f4SXiao Guangrong  */
143bebb106aSXiao Guangrong static inline int is_writable_pte(unsigned long pte)
144bebb106aSXiao Guangrong {
145bebb106aSXiao Guangrong 	return pte & PT_WRITABLE_MASK;
146bebb106aSXiao Guangrong }
147bebb106aSXiao Guangrong 
148bebb106aSXiao Guangrong static inline bool is_write_protection(struct kvm_vcpu *vcpu)
149bebb106aSXiao Guangrong {
150bebb106aSXiao Guangrong 	return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
151bebb106aSXiao Guangrong }
152bebb106aSXiao Guangrong 
15397d64b78SAvi Kivity /*
154f13577e8SPaolo Bonzini  * Check if a given access (described through the I/D, W/R and U/S bits of a
155f13577e8SPaolo Bonzini  * page fault error code pfec) causes a permission fault with the given PTE
156f13577e8SPaolo Bonzini  * access rights (in ACC_* format).
157f13577e8SPaolo Bonzini  *
158f13577e8SPaolo Bonzini  * Return zero if the access does not fault; return the page fault error code
159f13577e8SPaolo Bonzini  * if the access faults.
16097d64b78SAvi Kivity  */
161f13577e8SPaolo Bonzini static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
162be94f6b7SHuaitong Han 				  unsigned pte_access, unsigned pte_pkey,
163be94f6b7SHuaitong Han 				  unsigned pfec)
164bebb106aSXiao Guangrong {
165afaf0b2fSSean Christopherson 	int cpl = kvm_x86_ops.get_cpl(vcpu);
166afaf0b2fSSean Christopherson 	unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
16797ec8c06SFeng Wu 
16897ec8c06SFeng Wu 	/*
16997ec8c06SFeng Wu 	 * If CPL < 3, SMAP prevention are disabled if EFLAGS.AC = 1.
17097ec8c06SFeng Wu 	 *
17197ec8c06SFeng Wu 	 * If CPL = 3, SMAP applies to all supervisor-mode data accesses
17297ec8c06SFeng Wu 	 * (these are implicit supervisor accesses) regardless of the value
17397ec8c06SFeng Wu 	 * of EFLAGS.AC.
17497ec8c06SFeng Wu 	 *
17597ec8c06SFeng Wu 	 * This computes (cpl < 3) && (rflags & X86_EFLAGS_AC), leaving
17697ec8c06SFeng Wu 	 * the result in X86_EFLAGS_AC. We then insert it in place of
17797ec8c06SFeng Wu 	 * the PFERR_RSVD_MASK bit; this bit will always be zero in pfec,
17897ec8c06SFeng Wu 	 * but it will be one in index if SMAP checks are being overridden.
17997ec8c06SFeng Wu 	 * It is important to keep this branchless.
18097ec8c06SFeng Wu 	 */
18197ec8c06SFeng Wu 	unsigned long smap = (cpl - 3) & (rflags & X86_EFLAGS_AC);
18297ec8c06SFeng Wu 	int index = (pfec >> 1) +
18397ec8c06SFeng Wu 		    (smap >> (X86_EFLAGS_AC_BIT - PFERR_RSVD_BIT + 1));
184be94f6b7SHuaitong Han 	bool fault = (mmu->permissions[index] >> pte_access) & 1;
1857a98205dSXiao Guangrong 	u32 errcode = PFERR_PRESENT_MASK;
18697ec8c06SFeng Wu 
187be94f6b7SHuaitong Han 	WARN_ON(pfec & (PFERR_PK_MASK | PFERR_RSVD_MASK));
188be94f6b7SHuaitong Han 	if (unlikely(mmu->pkru_mask)) {
189be94f6b7SHuaitong Han 		u32 pkru_bits, offset;
190be94f6b7SHuaitong Han 
191be94f6b7SHuaitong Han 		/*
192be94f6b7SHuaitong Han 		* PKRU defines 32 bits, there are 16 domains and 2
193be94f6b7SHuaitong Han 		* attribute bits per domain in pkru.  pte_pkey is the
194be94f6b7SHuaitong Han 		* index of the protection domain, so pte_pkey * 2 is
195be94f6b7SHuaitong Han 		* is the index of the first bit for the domain.
196be94f6b7SHuaitong Han 		*/
197b9dd21e1SPaolo Bonzini 		pkru_bits = (vcpu->arch.pkru >> (pte_pkey * 2)) & 3;
198be94f6b7SHuaitong Han 
199be94f6b7SHuaitong Han 		/* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */
2007a98205dSXiao Guangrong 		offset = (pfec & ~1) +
201be94f6b7SHuaitong Han 			((pte_access & PT_USER_MASK) << (PFERR_RSVD_BIT - PT_USER_SHIFT));
202be94f6b7SHuaitong Han 
203be94f6b7SHuaitong Han 		pkru_bits &= mmu->pkru_mask >> offset;
2047a98205dSXiao Guangrong 		errcode |= -pkru_bits & PFERR_PK_MASK;
205be94f6b7SHuaitong Han 		fault |= (pkru_bits != 0);
206be94f6b7SHuaitong Han 	}
207be94f6b7SHuaitong Han 
2087a98205dSXiao Guangrong 	return -(u32)fault & errcode;
209bebb106aSXiao Guangrong }
21097d64b78SAvi Kivity 
211efdfe536SXiao Guangrong void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end);
212547ffaedSXiao Guangrong 
2136ca9a6f3SSean Christopherson int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu);
2141aa9b957SJunaid Shahid 
2151aa9b957SJunaid Shahid int kvm_mmu_post_init_vm(struct kvm *kvm);
2161aa9b957SJunaid Shahid void kvm_mmu_pre_destroy_vm(struct kvm *kvm);
2171aa9b957SJunaid Shahid 
218edf88417SAvi Kivity #endif
219