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 54*e7581cacSPaolo 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); 60ad896af0SPaolo Bonzini void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu); 61ae1e2d10SPaolo Bonzini void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, 6250c28f21SJunaid Shahid bool accessed_dirty, gpa_t new_eptp); 639bc1f09fSWanpeng Li bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu); 641261bfa3SWanpeng Li int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code, 65d0006530SPaolo Bonzini u64 fault_address, char *insn, int insn_len); 6694d8b056SMarcelo Tosatti 67bc8a3d89SBen Gardon static inline unsigned long kvm_mmu_available_pages(struct kvm *kvm) 68e0df7b9fSDave Hansen { 695d218814SMarcelo Tosatti if (kvm->arch.n_max_mmu_pages > kvm->arch.n_used_mmu_pages) 7049d5ca26SDave Hansen return kvm->arch.n_max_mmu_pages - 7149d5ca26SDave Hansen kvm->arch.n_used_mmu_pages; 725d218814SMarcelo Tosatti 735d218814SMarcelo Tosatti return 0; 74e0df7b9fSDave Hansen } 75e0df7b9fSDave Hansen 76edf88417SAvi Kivity static inline int kvm_mmu_reload(struct kvm_vcpu *vcpu) 77edf88417SAvi Kivity { 7844dd3ffaSVitaly Kuznetsov if (likely(vcpu->arch.mmu->root_hpa != INVALID_PAGE)) 79edf88417SAvi Kivity return 0; 80edf88417SAvi Kivity 81edf88417SAvi Kivity return kvm_mmu_load(vcpu); 82edf88417SAvi Kivity } 83edf88417SAvi Kivity 84c9470a2eSJunaid Shahid static inline unsigned long kvm_get_pcid(struct kvm_vcpu *vcpu, gpa_t cr3) 85c9470a2eSJunaid Shahid { 86c9470a2eSJunaid Shahid BUILD_BUG_ON((X86_CR3_PCID_MASK & PAGE_MASK) != 0); 87c9470a2eSJunaid Shahid 88c9470a2eSJunaid Shahid return kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE) 89c9470a2eSJunaid Shahid ? cr3 & X86_CR3_PCID_MASK 90c9470a2eSJunaid Shahid : 0; 91c9470a2eSJunaid Shahid } 92c9470a2eSJunaid Shahid 93c9470a2eSJunaid Shahid static inline unsigned long kvm_get_active_pcid(struct kvm_vcpu *vcpu) 94c9470a2eSJunaid Shahid { 95c9470a2eSJunaid Shahid return kvm_get_pcid(vcpu, kvm_read_cr3(vcpu)); 96c9470a2eSJunaid Shahid } 97c9470a2eSJunaid Shahid 98689f3bf2SPaolo Bonzini static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) 996e42782fSJunaid Shahid { 10044dd3ffaSVitaly Kuznetsov if (VALID_PAGE(vcpu->arch.mmu->root_hpa)) 101afaf0b2fSSean Christopherson kvm_x86_ops.load_mmu_pgd(vcpu, vcpu->arch.mmu->root_hpa | 102c9470a2eSJunaid Shahid kvm_get_active_pcid(vcpu)); 1036e42782fSJunaid Shahid } 1046e42782fSJunaid Shahid 1057a02674dSSean Christopherson int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, 1067a02674dSSean Christopherson bool prefault); 1077a02674dSSean Christopherson 1087a02674dSSean Christopherson static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, 1097a02674dSSean Christopherson u32 err, bool prefault) 1107a02674dSSean Christopherson { 1117a02674dSSean Christopherson #ifdef CONFIG_RETPOLINE 1127a02674dSSean Christopherson if (likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault)) 1137a02674dSSean Christopherson return kvm_tdp_page_fault(vcpu, cr2_or_gpa, err, prefault); 1147a02674dSSean Christopherson #endif 1157a02674dSSean Christopherson return vcpu->arch.mmu->page_fault(vcpu, cr2_or_gpa, err, prefault); 1167a02674dSSean Christopherson } 1177a02674dSSean Christopherson 118198c74f4SXiao Guangrong /* 119198c74f4SXiao Guangrong * Currently, we have two sorts of write-protection, a) the first one 120198c74f4SXiao Guangrong * write-protects guest page to sync the guest modification, b) another one is 121198c74f4SXiao Guangrong * used to sync dirty bitmap when we do KVM_GET_DIRTY_LOG. The differences 122198c74f4SXiao Guangrong * between these two sorts are: 123198c74f4SXiao Guangrong * 1) the first case clears SPTE_MMU_WRITEABLE bit. 124198c74f4SXiao Guangrong * 2) the first case requires flushing tlb immediately avoiding corrupting 125198c74f4SXiao Guangrong * shadow page table between all vcpus so it should be in the protection of 126198c74f4SXiao Guangrong * mmu-lock. And the another case does not need to flush tlb until returning 127198c74f4SXiao Guangrong * the dirty bitmap to userspace since it only write-protects the page 128198c74f4SXiao Guangrong * logged in the bitmap, that means the page in the dirty bitmap is not 129198c74f4SXiao Guangrong * missed, so it can flush tlb out of mmu-lock. 130198c74f4SXiao Guangrong * 131198c74f4SXiao Guangrong * So, there is the problem: the first case can meet the corrupted tlb caused 132198c74f4SXiao Guangrong * by another case which write-protects pages but without flush tlb 133198c74f4SXiao Guangrong * immediately. In order to making the first case be aware this problem we let 134198c74f4SXiao Guangrong * it flush tlb if we try to write-protect a spte whose SPTE_MMU_WRITEABLE bit 135198c74f4SXiao Guangrong * is set, it works since another case never touches SPTE_MMU_WRITEABLE bit. 136198c74f4SXiao Guangrong * 137198c74f4SXiao Guangrong * Anyway, whenever a spte is updated (only permission and status bits are 138198c74f4SXiao Guangrong * changed) we need to check whether the spte with SPTE_MMU_WRITEABLE becomes 139198c74f4SXiao Guangrong * readonly, if that happens, we need to flush tlb. Fortunately, 140198c74f4SXiao Guangrong * mmu_spte_update() has already handled it perfectly. 141198c74f4SXiao Guangrong * 142198c74f4SXiao Guangrong * The rules to use SPTE_MMU_WRITEABLE and PT_WRITABLE_MASK: 143198c74f4SXiao Guangrong * - if we want to see if it has writable tlb entry or if the spte can be 144198c74f4SXiao Guangrong * writable on the mmu mapping, check SPTE_MMU_WRITEABLE, this is the most 145198c74f4SXiao Guangrong * case, otherwise 146198c74f4SXiao Guangrong * - if we fix page fault on the spte or do write-protection by dirty logging, 147198c74f4SXiao Guangrong * check PT_WRITABLE_MASK. 148198c74f4SXiao Guangrong * 149198c74f4SXiao Guangrong * TODO: introduce APIs to split these two cases. 150198c74f4SXiao Guangrong */ 151bebb106aSXiao Guangrong static inline int is_writable_pte(unsigned long pte) 152bebb106aSXiao Guangrong { 153bebb106aSXiao Guangrong return pte & PT_WRITABLE_MASK; 154bebb106aSXiao Guangrong } 155bebb106aSXiao Guangrong 156bebb106aSXiao Guangrong static inline bool is_write_protection(struct kvm_vcpu *vcpu) 157bebb106aSXiao Guangrong { 158bebb106aSXiao Guangrong return kvm_read_cr0_bits(vcpu, X86_CR0_WP); 159bebb106aSXiao Guangrong } 160bebb106aSXiao Guangrong 16197d64b78SAvi Kivity /* 162f13577e8SPaolo Bonzini * Check if a given access (described through the I/D, W/R and U/S bits of a 163f13577e8SPaolo Bonzini * page fault error code pfec) causes a permission fault with the given PTE 164f13577e8SPaolo Bonzini * access rights (in ACC_* format). 165f13577e8SPaolo Bonzini * 166f13577e8SPaolo Bonzini * Return zero if the access does not fault; return the page fault error code 167f13577e8SPaolo Bonzini * if the access faults. 16897d64b78SAvi Kivity */ 169f13577e8SPaolo Bonzini static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, 170be94f6b7SHuaitong Han unsigned pte_access, unsigned pte_pkey, 171be94f6b7SHuaitong Han unsigned pfec) 172bebb106aSXiao Guangrong { 173afaf0b2fSSean Christopherson int cpl = kvm_x86_ops.get_cpl(vcpu); 174afaf0b2fSSean Christopherson unsigned long rflags = kvm_x86_ops.get_rflags(vcpu); 17597ec8c06SFeng Wu 17697ec8c06SFeng Wu /* 17797ec8c06SFeng Wu * If CPL < 3, SMAP prevention are disabled if EFLAGS.AC = 1. 17897ec8c06SFeng Wu * 17997ec8c06SFeng Wu * If CPL = 3, SMAP applies to all supervisor-mode data accesses 18097ec8c06SFeng Wu * (these are implicit supervisor accesses) regardless of the value 18197ec8c06SFeng Wu * of EFLAGS.AC. 18297ec8c06SFeng Wu * 18397ec8c06SFeng Wu * This computes (cpl < 3) && (rflags & X86_EFLAGS_AC), leaving 18497ec8c06SFeng Wu * the result in X86_EFLAGS_AC. We then insert it in place of 18597ec8c06SFeng Wu * the PFERR_RSVD_MASK bit; this bit will always be zero in pfec, 18697ec8c06SFeng Wu * but it will be one in index if SMAP checks are being overridden. 18797ec8c06SFeng Wu * It is important to keep this branchless. 18897ec8c06SFeng Wu */ 18997ec8c06SFeng Wu unsigned long smap = (cpl - 3) & (rflags & X86_EFLAGS_AC); 19097ec8c06SFeng Wu int index = (pfec >> 1) + 19197ec8c06SFeng Wu (smap >> (X86_EFLAGS_AC_BIT - PFERR_RSVD_BIT + 1)); 192be94f6b7SHuaitong Han bool fault = (mmu->permissions[index] >> pte_access) & 1; 1937a98205dSXiao Guangrong u32 errcode = PFERR_PRESENT_MASK; 19497ec8c06SFeng Wu 195be94f6b7SHuaitong Han WARN_ON(pfec & (PFERR_PK_MASK | PFERR_RSVD_MASK)); 196be94f6b7SHuaitong Han if (unlikely(mmu->pkru_mask)) { 197be94f6b7SHuaitong Han u32 pkru_bits, offset; 198be94f6b7SHuaitong Han 199be94f6b7SHuaitong Han /* 200be94f6b7SHuaitong Han * PKRU defines 32 bits, there are 16 domains and 2 201be94f6b7SHuaitong Han * attribute bits per domain in pkru. pte_pkey is the 202be94f6b7SHuaitong Han * index of the protection domain, so pte_pkey * 2 is 203be94f6b7SHuaitong Han * is the index of the first bit for the domain. 204be94f6b7SHuaitong Han */ 205b9dd21e1SPaolo Bonzini pkru_bits = (vcpu->arch.pkru >> (pte_pkey * 2)) & 3; 206be94f6b7SHuaitong Han 207be94f6b7SHuaitong Han /* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */ 2087a98205dSXiao Guangrong offset = (pfec & ~1) + 209be94f6b7SHuaitong Han ((pte_access & PT_USER_MASK) << (PFERR_RSVD_BIT - PT_USER_SHIFT)); 210be94f6b7SHuaitong Han 211be94f6b7SHuaitong Han pkru_bits &= mmu->pkru_mask >> offset; 2127a98205dSXiao Guangrong errcode |= -pkru_bits & PFERR_PK_MASK; 213be94f6b7SHuaitong Han fault |= (pkru_bits != 0); 214be94f6b7SHuaitong Han } 215be94f6b7SHuaitong Han 2167a98205dSXiao Guangrong return -(u32)fault & errcode; 217bebb106aSXiao Guangrong } 21897d64b78SAvi Kivity 219efdfe536SXiao Guangrong void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); 220547ffaedSXiao Guangrong 221547ffaedSXiao Guangrong void kvm_mmu_gfn_disallow_lpage(struct kvm_memory_slot *slot, gfn_t gfn); 222547ffaedSXiao Guangrong void kvm_mmu_gfn_allow_lpage(struct kvm_memory_slot *slot, gfn_t gfn); 223aeecee2eSXiao Guangrong bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm, 224aeecee2eSXiao Guangrong struct kvm_memory_slot *slot, u64 gfn); 225bab4165eSBandan Das int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu); 2261aa9b957SJunaid Shahid 2271aa9b957SJunaid Shahid int kvm_mmu_post_init_vm(struct kvm *kvm); 2281aa9b957SJunaid Shahid void kvm_mmu_pre_destroy_vm(struct kvm *kvm); 2291aa9b957SJunaid Shahid 230edf88417SAvi Kivity #endif 231