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 static inline gfn_t kvm_mmu_max_gfn(void) 6186931ff7SSean Christopherson { 6286931ff7SSean Christopherson /* 6386931ff7SSean Christopherson * Note that this uses the host MAXPHYADDR, not the guest's. 6486931ff7SSean Christopherson * EPT/NPT cannot support GPAs that would exceed host.MAXPHYADDR; 6586931ff7SSean Christopherson * assuming KVM is running on bare metal, guest accesses beyond 6686931ff7SSean Christopherson * host.MAXPHYADDR will hit a #PF(RSVD) and never cause a vmexit 6786931ff7SSean Christopherson * (either EPT Violation/Misconfig or #NPF), and so KVM will never 6886931ff7SSean Christopherson * install a SPTE for such addresses. If KVM is running as a VM 6986931ff7SSean Christopherson * itself, on the other hand, it might see a MAXPHYADDR that is less 7086931ff7SSean Christopherson * than hardware's real MAXPHYADDR. Using the host MAXPHYADDR 7186931ff7SSean Christopherson * disallows such SPTEs entirely and simplifies the TDP MMU. 7286931ff7SSean Christopherson */ 7382897db9SSean Christopherson int max_gpa_bits = likely(tdp_enabled) ? kvm_host.maxphyaddr : 52; 7486931ff7SSean Christopherson 7586931ff7SSean Christopherson return (1ULL << (max_gpa_bits - PAGE_SHIFT)) - 1; 7686931ff7SSean Christopherson } 7786931ff7SSean Christopherson 78b628cb52SGerd Hoffmann u8 kvm_mmu_get_max_tdp_level(void); 79b628cb52SGerd Hoffmann 808120337aSSean Christopherson void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask); 81e54f1ff2SKai Huang void kvm_mmu_set_me_spte_mask(u64 me_value, u64 me_mask); 82e7b7bdeaSSean Christopherson void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only); 83b37fbea6SXiao Guangrong 84c9060662SSean Christopherson void kvm_init_mmu(struct kvm_vcpu *vcpu); 85dbc4739bSSean Christopherson void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr0, 86dbc4739bSSean Christopherson unsigned long cr4, u64 efer, gpa_t nested_cr3); 87ae1e2d10SPaolo Bonzini void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, 88cc022ae1SLai Jiangshan int huge_page_level, bool accessed_dirty, 89cc022ae1SLai Jiangshan gpa_t new_eptp); 909bc1f09fSWanpeng Li bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu); 911261bfa3SWanpeng Li int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code, 92d0006530SPaolo Bonzini u64 fault_address, char *insn, int insn_len); 93cf9f4c0eSSean Christopherson void __kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu, 94cf9f4c0eSSean Christopherson struct kvm_mmu *mmu); 9594d8b056SMarcelo Tosatti 9661a1773eSSean Christopherson int kvm_mmu_load(struct kvm_vcpu *vcpu); 9761a1773eSSean Christopherson void kvm_mmu_unload(struct kvm_vcpu *vcpu); 98527d5cd7SSean Christopherson void kvm_mmu_free_obsolete_roots(struct kvm_vcpu *vcpu); 9961a1773eSSean Christopherson void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu); 10061b05a9fSLai Jiangshan void kvm_mmu_sync_prev_roots(struct kvm_vcpu *vcpu); 10193284446SSean Christopherson void kvm_mmu_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new, 10293284446SSean Christopherson int bytes); 10361a1773eSSean Christopherson 104edf88417SAvi Kivity static inline int kvm_mmu_reload(struct kvm_vcpu *vcpu) 105edf88417SAvi Kivity { 106b9e5603cSPaolo Bonzini if (likely(vcpu->arch.mmu->root.hpa != INVALID_PAGE)) 107edf88417SAvi Kivity return 0; 108edf88417SAvi Kivity 109edf88417SAvi Kivity return kvm_mmu_load(vcpu); 110edf88417SAvi Kivity } 111edf88417SAvi Kivity 112c9470a2eSJunaid Shahid static inline unsigned long kvm_get_pcid(struct kvm_vcpu *vcpu, gpa_t cr3) 113c9470a2eSJunaid Shahid { 114c9470a2eSJunaid Shahid BUILD_BUG_ON((X86_CR3_PCID_MASK & PAGE_MASK) != 0); 115c9470a2eSJunaid Shahid 116607475cfSBinbin Wu return kvm_is_cr4_bit_set(vcpu, X86_CR4_PCIDE) 117c9470a2eSJunaid Shahid ? cr3 & X86_CR3_PCID_MASK 118c9470a2eSJunaid Shahid : 0; 119c9470a2eSJunaid Shahid } 120c9470a2eSJunaid Shahid 121c9470a2eSJunaid Shahid static inline unsigned long kvm_get_active_pcid(struct kvm_vcpu *vcpu) 122c9470a2eSJunaid Shahid { 123c9470a2eSJunaid Shahid return kvm_get_pcid(vcpu, kvm_read_cr3(vcpu)); 124c9470a2eSJunaid Shahid } 125c9470a2eSJunaid Shahid 1263098e6ecSRobert Hoo static inline unsigned long kvm_get_active_cr3_lam_bits(struct kvm_vcpu *vcpu) 1273098e6ecSRobert Hoo { 128183bdd16SBinbin Wu if (!guest_can_use(vcpu, X86_FEATURE_LAM)) 1293098e6ecSRobert Hoo return 0; 1303098e6ecSRobert Hoo 1313098e6ecSRobert Hoo return kvm_read_cr3(vcpu) & (X86_CR3_LAM_U48 | X86_CR3_LAM_U57); 1323098e6ecSRobert Hoo } 1333098e6ecSRobert Hoo 134689f3bf2SPaolo Bonzini static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) 1356e42782fSJunaid Shahid { 136b9e5603cSPaolo Bonzini u64 root_hpa = vcpu->arch.mmu->root.hpa; 1372a40b900SSean Christopherson 1382a40b900SSean Christopherson if (!VALID_PAGE(root_hpa)) 1392a40b900SSean Christopherson return; 1402a40b900SSean Christopherson 141*89604647SWei Wang kvm_x86_call(load_mmu_pgd)(vcpu, root_hpa, 142a972e29cSPaolo Bonzini vcpu->arch.mmu->root_role.level); 1436e42782fSJunaid Shahid } 1446e42782fSJunaid Shahid 145cf9f4c0eSSean Christopherson static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu, 146cf9f4c0eSSean Christopherson struct kvm_mmu *mmu) 147cf9f4c0eSSean Christopherson { 148cf9f4c0eSSean Christopherson /* 149cf9f4c0eSSean Christopherson * When EPT is enabled, KVM may passthrough CR0.WP to the guest, i.e. 150cf9f4c0eSSean Christopherson * @mmu's snapshot of CR0.WP and thus all related paging metadata may 151cf9f4c0eSSean Christopherson * be stale. Refresh CR0.WP and the metadata on-demand when checking 152cf9f4c0eSSean Christopherson * for permission faults. Exempt nested MMUs, i.e. MMUs for shadowing 153cf9f4c0eSSean Christopherson * nEPT and nNPT, as CR0.WP is ignored in both cases. Note, KVM does 154cf9f4c0eSSean Christopherson * need to refresh nested_mmu, a.k.a. the walker used to translate L2 155cf9f4c0eSSean Christopherson * GVAs to GPAs, as that "MMU" needs to honor L2's CR0.WP. 156cf9f4c0eSSean Christopherson */ 157cf9f4c0eSSean Christopherson if (!tdp_enabled || mmu == &vcpu->arch.guest_mmu) 158cf9f4c0eSSean Christopherson return; 159cf9f4c0eSSean Christopherson 160cf9f4c0eSSean Christopherson __kvm_mmu_refresh_passthrough_bits(vcpu, mmu); 161cf9f4c0eSSean Christopherson } 162cf9f4c0eSSean Christopherson 163198c74f4SXiao Guangrong /* 164f13577e8SPaolo Bonzini * Check if a given access (described through the I/D, W/R and U/S bits of a 165f13577e8SPaolo Bonzini * page fault error code pfec) causes a permission fault with the given PTE 166f13577e8SPaolo Bonzini * access rights (in ACC_* format). 167f13577e8SPaolo Bonzini * 168f13577e8SPaolo Bonzini * Return zero if the access does not fault; return the page fault error code 169f13577e8SPaolo Bonzini * if the access faults. 17097d64b78SAvi Kivity */ 171f13577e8SPaolo Bonzini static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, 172be94f6b7SHuaitong Han unsigned pte_access, unsigned pte_pkey, 1735b22bbe7SLai Jiangshan u64 access) 174bebb106aSXiao Guangrong { 1755b22bbe7SLai Jiangshan /* strip nested paging fault error codes */ 1765b22bbe7SLai Jiangshan unsigned int pfec = access; 177*89604647SWei Wang unsigned long rflags = kvm_x86_call(get_rflags)(vcpu); 17897ec8c06SFeng Wu 17997ec8c06SFeng Wu /* 1804f4aa80eSLai Jiangshan * For explicit supervisor accesses, SMAP is disabled if EFLAGS.AC = 1. 1814f4aa80eSLai Jiangshan * For implicit supervisor accesses, SMAP cannot be overridden. 18297ec8c06SFeng Wu * 1834f4aa80eSLai Jiangshan * SMAP works on supervisor accesses only, and not_smap can 1844f4aa80eSLai Jiangshan * be set or not set when user access with neither has any bearing 1854f4aa80eSLai Jiangshan * on the result. 18697ec8c06SFeng Wu * 1874f4aa80eSLai Jiangshan * We put the SMAP checking bit in place of the PFERR_RSVD_MASK bit; 1884f4aa80eSLai Jiangshan * this bit will always be zero in pfec, but it will be one in index 1894f4aa80eSLai Jiangshan * if SMAP checks are being disabled. 19097ec8c06SFeng Wu */ 1914f4aa80eSLai Jiangshan u64 implicit_access = access & PFERR_IMPLICIT_ACCESS; 1924f4aa80eSLai Jiangshan bool not_smap = ((rflags & X86_EFLAGS_AC) | implicit_access) == X86_EFLAGS_AC; 19363b6206eSSean Christopherson int index = (pfec | (not_smap ? PFERR_RSVD_MASK : 0)) >> 1; 1947a98205dSXiao Guangrong u32 errcode = PFERR_PRESENT_MASK; 195cf9f4c0eSSean Christopherson bool fault; 196cf9f4c0eSSean Christopherson 197cf9f4c0eSSean Christopherson kvm_mmu_refresh_passthrough_bits(vcpu, mmu); 198cf9f4c0eSSean Christopherson 199cf9f4c0eSSean Christopherson fault = (mmu->permissions[index] >> pte_access) & 1; 20097ec8c06SFeng Wu 201be94f6b7SHuaitong Han WARN_ON(pfec & (PFERR_PK_MASK | PFERR_RSVD_MASK)); 202be94f6b7SHuaitong Han if (unlikely(mmu->pkru_mask)) { 203be94f6b7SHuaitong Han u32 pkru_bits, offset; 204be94f6b7SHuaitong Han 205be94f6b7SHuaitong Han /* 206be94f6b7SHuaitong Han * PKRU defines 32 bits, there are 16 domains and 2 207be94f6b7SHuaitong Han * attribute bits per domain in pkru. pte_pkey is the 208be94f6b7SHuaitong Han * index of the protection domain, so pte_pkey * 2 is 209be94f6b7SHuaitong Han * is the index of the first bit for the domain. 210be94f6b7SHuaitong Han */ 211b9dd21e1SPaolo Bonzini pkru_bits = (vcpu->arch.pkru >> (pte_pkey * 2)) & 3; 212be94f6b7SHuaitong Han 213be94f6b7SHuaitong Han /* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */ 21463b6206eSSean Christopherson offset = (pfec & ~1) | ((pte_access & PT_USER_MASK) ? PFERR_RSVD_MASK : 0); 215be94f6b7SHuaitong Han 216be94f6b7SHuaitong Han pkru_bits &= mmu->pkru_mask >> offset; 2177a98205dSXiao Guangrong errcode |= -pkru_bits & PFERR_PK_MASK; 218be94f6b7SHuaitong Han fault |= (pkru_bits != 0); 219be94f6b7SHuaitong Han } 220be94f6b7SHuaitong Han 2217a98205dSXiao Guangrong return -(u32)fault & errcode; 222bebb106aSXiao Guangrong } 22397d64b78SAvi Kivity 2240a7b7355SSean Christopherson bool kvm_mmu_may_ignore_guest_pat(void); 2251affe455SYan Zhao 2266ca9a6f3SSean Christopherson int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu); 2271aa9b957SJunaid Shahid 2281aa9b957SJunaid Shahid int kvm_mmu_post_init_vm(struct kvm *kvm); 2291aa9b957SJunaid Shahid void kvm_mmu_pre_destroy_vm(struct kvm *kvm); 2301aa9b957SJunaid Shahid 2311e76a3ceSDavid Stevens static inline bool kvm_shadow_root_allocated(struct kvm *kvm) 232e2209710SBen Gardon { 233d501f747SBen Gardon /* 2341e76a3ceSDavid Stevens * Read shadow_root_allocated before related pointers. Hence, threads 2351e76a3ceSDavid Stevens * reading shadow_root_allocated in any lock context are guaranteed to 2361e76a3ceSDavid Stevens * see the pointers. Pairs with smp_store_release in 2371e76a3ceSDavid Stevens * mmu_first_shadow_root_alloc. 238d501f747SBen Gardon */ 2391e76a3ceSDavid Stevens return smp_load_acquire(&kvm->arch.shadow_root_allocated); 2401e76a3ceSDavid Stevens } 2411e76a3ceSDavid Stevens 2421e76a3ceSDavid Stevens #ifdef CONFIG_X86_64 2431f98f2bdSDavid Matlack extern bool tdp_mmu_enabled; 2441e76a3ceSDavid Stevens #else 2451f98f2bdSDavid Matlack #define tdp_mmu_enabled false 2461e76a3ceSDavid Stevens #endif 2471e76a3ceSDavid Stevens 2481e76a3ceSDavid Stevens static inline bool kvm_memslots_have_rmaps(struct kvm *kvm) 2491e76a3ceSDavid Stevens { 2501f98f2bdSDavid Matlack return !tdp_mmu_enabled || kvm_shadow_root_allocated(kvm); 251e2209710SBen Gardon } 252e2209710SBen Gardon 2534139b197SPeter Xu static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level) 2544139b197SPeter Xu { 2554139b197SPeter Xu /* KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K) must be 0. */ 2564139b197SPeter Xu return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) - 2574139b197SPeter Xu (base_gfn >> KVM_HPAGE_GFN_SHIFT(level)); 2584139b197SPeter Xu } 2594139b197SPeter Xu 2604139b197SPeter Xu static inline unsigned long 2614139b197SPeter Xu __kvm_mmu_slot_lpages(struct kvm_memory_slot *slot, unsigned long npages, 2624139b197SPeter Xu int level) 2634139b197SPeter Xu { 2644139b197SPeter Xu return gfn_to_index(slot->base_gfn + npages - 1, 2654139b197SPeter Xu slot->base_gfn, level) + 1; 2664139b197SPeter Xu } 2674139b197SPeter Xu 2684139b197SPeter Xu static inline unsigned long 2694139b197SPeter Xu kvm_mmu_slot_lpages(struct kvm_memory_slot *slot, int level) 2704139b197SPeter Xu { 2714139b197SPeter Xu return __kvm_mmu_slot_lpages(slot, slot->npages, level); 2724139b197SPeter Xu } 2734139b197SPeter Xu 27471f51d2cSMingwei Zhang static inline void kvm_update_page_stats(struct kvm *kvm, int level, int count) 27571f51d2cSMingwei Zhang { 27671f51d2cSMingwei Zhang atomic64_add(count, &kvm->stat.pages[level - 1]); 27771f51d2cSMingwei Zhang } 278c59a0f57SLai Jiangshan 2795b22bbe7SLai Jiangshan gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u64 access, 280c59a0f57SLai Jiangshan struct x86_exception *exception); 281c59a0f57SLai Jiangshan 282c59a0f57SLai Jiangshan static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu, 283c59a0f57SLai Jiangshan struct kvm_mmu *mmu, 2845b22bbe7SLai Jiangshan gpa_t gpa, u64 access, 285c59a0f57SLai Jiangshan struct x86_exception *exception) 286c59a0f57SLai Jiangshan { 287c59a0f57SLai Jiangshan if (mmu != &vcpu->arch.nested_mmu) 288c59a0f57SLai Jiangshan return gpa; 289c59a0f57SLai Jiangshan return translate_nested_gpa(vcpu, gpa, access, exception); 290c59a0f57SLai Jiangshan } 291edf88417SAvi Kivity #endif 292