Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: arm64: Remove unused page-table code

Now that KVM is using the generic page-table code to manage the guest
stage-2 page-tables, we can remove a bunch of unused macros, #defines
and static inline functions from the old implementation.

Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Gavin Shan <gshan@redhat.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20200911132529.19844-20-will@kernel.org

authored by

Will Deacon and committed by
Marc Zyngier
3f26ab58 063deeb1

-1172
-171
arch/arm64/include/asm/kvm_mmu.h
··· 107 107 #define kvm_phys_size(kvm) (_AC(1, ULL) << kvm_phys_shift(kvm)) 108 108 #define kvm_phys_mask(kvm) (kvm_phys_size(kvm) - _AC(1, ULL)) 109 109 110 - static inline bool kvm_page_empty(void *ptr) 111 - { 112 - struct page *ptr_page = virt_to_page(ptr); 113 - return page_count(ptr_page) == 1; 114 - } 115 - 116 110 #include <asm/kvm_pgtable.h> 117 111 #include <asm/stage2_pgtable.h> 118 112 ··· 129 135 phys_addr_t kvm_mmu_get_httbr(void); 130 136 phys_addr_t kvm_get_idmap_vector(void); 131 137 int kvm_mmu_init(void); 132 - #define kvm_mk_pmd(ptep) \ 133 - __pmd(__phys_to_pmd_val(__pa(ptep)) | PMD_TYPE_TABLE) 134 - #define kvm_mk_pud(pmdp) \ 135 - __pud(__phys_to_pud_val(__pa(pmdp)) | PMD_TYPE_TABLE) 136 - #define kvm_mk_p4d(pmdp) \ 137 - __p4d(__phys_to_p4d_val(__pa(pmdp)) | PUD_TYPE_TABLE) 138 - 139 - #define kvm_set_pud(pudp, pud) set_pud(pudp, pud) 140 - 141 - #define kvm_pfn_pte(pfn, prot) pfn_pte(pfn, prot) 142 - #define kvm_pfn_pmd(pfn, prot) pfn_pmd(pfn, prot) 143 - #define kvm_pfn_pud(pfn, prot) pfn_pud(pfn, prot) 144 - 145 - #define kvm_pud_pfn(pud) pud_pfn(pud) 146 - 147 - #define kvm_pmd_mkhuge(pmd) pmd_mkhuge(pmd) 148 - #define kvm_pud_mkhuge(pud) pud_mkhuge(pud) 149 - 150 - static inline pte_t kvm_s2pte_mkwrite(pte_t pte) 151 - { 152 - pte_val(pte) |= PTE_S2_RDWR; 153 - return pte; 154 - } 155 - 156 - static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd) 157 - { 158 - pmd_val(pmd) |= PMD_S2_RDWR; 159 - return pmd; 160 - } 161 - 162 - static inline pud_t kvm_s2pud_mkwrite(pud_t pud) 163 - { 164 - pud_val(pud) |= PUD_S2_RDWR; 165 - return pud; 166 - } 167 - 168 - static inline pte_t kvm_s2pte_mkexec(pte_t pte) 169 - { 170 - pte_val(pte) &= ~PTE_S2_XN; 171 - return pte; 172 - } 173 - 174 - static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd) 175 - { 176 - pmd_val(pmd) &= ~PMD_S2_XN; 177 - return pmd; 178 - } 179 - 180 - static inline pud_t kvm_s2pud_mkexec(pud_t pud) 181 - { 182 - pud_val(pud) &= ~PUD_S2_XN; 183 - return pud; 184 - } 185 - 186 - static inline void kvm_set_s2pte_readonly(pte_t *ptep) 187 - { 188 - pteval_t old_pteval, pteval; 189 - 190 - pteval = READ_ONCE(pte_val(*ptep)); 191 - do { 192 - old_pteval = pteval; 193 - pteval &= ~PTE_S2_RDWR; 194 - pteval |= PTE_S2_RDONLY; 195 - pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval); 196 - } while (pteval != old_pteval); 197 - } 198 - 199 - static inline bool kvm_s2pte_readonly(pte_t *ptep) 200 - { 201 - return (READ_ONCE(pte_val(*ptep)) & PTE_S2_RDWR) == PTE_S2_RDONLY; 202 - } 203 - 204 - static inline bool kvm_s2pte_exec(pte_t *ptep) 205 - { 206 - return !(READ_ONCE(pte_val(*ptep)) & PTE_S2_XN); 207 - } 208 - 209 - static inline void kvm_set_s2pmd_readonly(pmd_t *pmdp) 210 - { 211 - kvm_set_s2pte_readonly((pte_t *)pmdp); 212 - } 213 - 214 - static inline bool kvm_s2pmd_readonly(pmd_t *pmdp) 215 - { 216 - return kvm_s2pte_readonly((pte_t *)pmdp); 217 - } 218 - 219 - static inline bool kvm_s2pmd_exec(pmd_t *pmdp) 220 - { 221 - return !(READ_ONCE(pmd_val(*pmdp)) & PMD_S2_XN); 222 - } 223 - 224 - static inline void kvm_set_s2pud_readonly(pud_t *pudp) 225 - { 226 - kvm_set_s2pte_readonly((pte_t *)pudp); 227 - } 228 - 229 - static inline bool kvm_s2pud_readonly(pud_t *pudp) 230 - { 231 - return kvm_s2pte_readonly((pte_t *)pudp); 232 - } 233 - 234 - static inline bool kvm_s2pud_exec(pud_t *pudp) 235 - { 236 - return !(READ_ONCE(pud_val(*pudp)) & PUD_S2_XN); 237 - } 238 - 239 - static inline pud_t kvm_s2pud_mkyoung(pud_t pud) 240 - { 241 - return pud_mkyoung(pud); 242 - } 243 - 244 - static inline bool kvm_s2pud_young(pud_t pud) 245 - { 246 - return pud_young(pud); 247 - } 248 - 249 138 250 139 struct kvm; 251 140 ··· 167 290 168 291 invalidate_icache_range((unsigned long)va, 169 292 (unsigned long)va + size); 170 - } 171 - } 172 - 173 - static inline void __kvm_flush_dcache_pte(pte_t pte) 174 - { 175 - if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) { 176 - struct page *page = pte_page(pte); 177 - kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE); 178 - } 179 - } 180 - 181 - static inline void __kvm_flush_dcache_pmd(pmd_t pmd) 182 - { 183 - if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) { 184 - struct page *page = pmd_page(pmd); 185 - kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE); 186 - } 187 - } 188 - 189 - static inline void __kvm_flush_dcache_pud(pud_t pud) 190 - { 191 - if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) { 192 - struct page *page = pud_page(pud); 193 - kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE); 194 293 } 195 294 } 196 295 ··· 329 476 #endif 330 477 331 478 #define kvm_phys_to_vttbr(addr) phys_to_ttbr(addr) 332 - 333 - /* 334 - * Get the magic number 'x' for VTTBR:BADDR of this KVM instance. 335 - * With v8.2 LVA extensions, 'x' should be a minimum of 6 with 336 - * 52bit IPS. 337 - */ 338 - static inline int arm64_vttbr_x(u32 ipa_shift, u32 levels) 339 - { 340 - int x = ARM64_VTTBR_X(ipa_shift, levels); 341 - 342 - return (IS_ENABLED(CONFIG_ARM64_PA_BITS_52) && x < 6) ? 6 : x; 343 - } 344 - 345 - static inline u64 vttbr_baddr_mask(u32 ipa_shift, u32 levels) 346 - { 347 - unsigned int x = arm64_vttbr_x(ipa_shift, levels); 348 - 349 - return GENMASK_ULL(PHYS_MASK_SHIFT - 1, x); 350 - } 351 - 352 - static inline u64 kvm_vttbr_baddr_mask(struct kvm *kvm) 353 - { 354 - return vttbr_baddr_mask(kvm_phys_shift(kvm), kvm_stage2_levels(kvm)); 355 - } 356 479 357 480 static __always_inline u64 kvm_get_vttbr(struct kvm_s2_mmu *mmu) 358 481 {
-18
arch/arm64/include/asm/pgtable-hwdef.h
··· 156 156 #define PTE_CONT (_AT(pteval_t, 1) << 52) /* Contiguous range */ 157 157 #define PTE_PXN (_AT(pteval_t, 1) << 53) /* Privileged XN */ 158 158 #define PTE_UXN (_AT(pteval_t, 1) << 54) /* User XN */ 159 - #define PTE_HYP_XN (_AT(pteval_t, 1) << 54) /* HYP XN */ 160 159 161 160 #define PTE_ADDR_LOW (((_AT(pteval_t, 1) << (48 - PAGE_SHIFT)) - 1) << PAGE_SHIFT) 162 161 #ifdef CONFIG_ARM64_PA_BITS_52 ··· 170 171 */ 171 172 #define PTE_ATTRINDX(t) (_AT(pteval_t, (t)) << 2) 172 173 #define PTE_ATTRINDX_MASK (_AT(pteval_t, 7) << 2) 173 - 174 - /* 175 - * 2nd stage PTE definitions 176 - */ 177 - #define PTE_S2_RDONLY (_AT(pteval_t, 1) << 6) /* HAP[2:1] */ 178 - #define PTE_S2_RDWR (_AT(pteval_t, 3) << 6) /* HAP[2:1] */ 179 - #define PTE_S2_XN (_AT(pteval_t, 2) << 53) /* XN[1:0] */ 180 - #define PTE_S2_SW_RESVD (_AT(pteval_t, 15) << 55) /* Reserved for SW */ 181 - 182 - #define PMD_S2_RDONLY (_AT(pmdval_t, 1) << 6) /* HAP[2:1] */ 183 - #define PMD_S2_RDWR (_AT(pmdval_t, 3) << 6) /* HAP[2:1] */ 184 - #define PMD_S2_XN (_AT(pmdval_t, 2) << 53) /* XN[1:0] */ 185 - #define PMD_S2_SW_RESVD (_AT(pmdval_t, 15) << 55) /* Reserved for SW */ 186 - 187 - #define PUD_S2_RDONLY (_AT(pudval_t, 1) << 6) /* HAP[2:1] */ 188 - #define PUD_S2_RDWR (_AT(pudval_t, 3) << 6) /* HAP[2:1] */ 189 - #define PUD_S2_XN (_AT(pudval_t, 2) << 53) /* XN[1:0] */ 190 174 191 175 /* 192 176 * Memory Attribute override for Stage-2 (MemAttr[3:0])
-13
arch/arm64/include/asm/pgtable-prot.h
··· 73 73 __val; \ 74 74 }) 75 75 76 - #define PAGE_S2_XN \ 77 - ({ \ 78 - u64 __val; \ 79 - if (cpus_have_const_cap(ARM64_HAS_CACHE_DIC)) \ 80 - __val = 0; \ 81 - else \ 82 - __val = PTE_S2_XN; \ 83 - __val; \ 84 - }) 85 - 86 - #define PAGE_S2 __pgprot(_PROT_DEFAULT | PAGE_S2_MEMATTR(NORMAL) | PTE_S2_RDONLY | PAGE_S2_XN) 87 - #define PAGE_S2_DEVICE __pgprot(_PROT_DEFAULT | PAGE_S2_MEMATTR(DEVICE_nGnRE) | PTE_S2_RDONLY | PTE_S2_XN) 88 - 89 76 #define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_PROT_NONE | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN) 90 77 /* shared+writable pages are clean by default, hence PTE_RDONLY|PTE_WRITE */ 91 78 #define PAGE_SHARED __pgprot(_PAGE_DEFAULT | PTE_USER | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN | PTE_WRITE)
-215
arch/arm64/include/asm/stage2_pgtable.h
··· 8 8 #ifndef __ARM64_S2_PGTABLE_H_ 9 9 #define __ARM64_S2_PGTABLE_H_ 10 10 11 - #include <linux/hugetlb.h> 12 11 #include <linux/pgtable.h> 13 12 14 13 /* ··· 36 37 #define stage2_pgdir_mask(kvm) ~(stage2_pgdir_size(kvm) - 1) 37 38 38 39 /* 39 - * The number of PTRS across all concatenated stage2 tables given by the 40 - * number of bits resolved at the initial level. 41 - * If we force more levels than necessary, we may have (stage2_pgdir_shift > IPA), 42 - * in which case, stage2_pgd_ptrs will have one entry. 43 - */ 44 - #define pgd_ptrs_shift(ipa, pgdir_shift) \ 45 - ((ipa) > (pgdir_shift) ? ((ipa) - (pgdir_shift)) : 0) 46 - #define __s2_pgd_ptrs(ipa, lvls) \ 47 - (1 << (pgd_ptrs_shift((ipa), pt_levels_pgdir_shift(lvls)))) 48 - #define __s2_pgd_size(ipa, lvls) (__s2_pgd_ptrs((ipa), (lvls)) * sizeof(pgd_t)) 49 - 50 - #define stage2_pgd_ptrs(kvm) __s2_pgd_ptrs(kvm_phys_shift(kvm), kvm_stage2_levels(kvm)) 51 - #define stage2_pgd_size(kvm) __s2_pgd_size(kvm_phys_shift(kvm), kvm_stage2_levels(kvm)) 52 - 53 - /* 54 40 * kvm_mmmu_cache_min_pages() is the number of pages required to install 55 41 * a stage-2 translation. We pre-allocate the entry level page table at 56 42 * the VM creation. 57 43 */ 58 44 #define kvm_mmu_cache_min_pages(kvm) (kvm_stage2_levels(kvm) - 1) 59 - 60 - /* Stage2 PUD definitions when the level is present */ 61 - static inline bool kvm_stage2_has_pud(struct kvm *kvm) 62 - { 63 - return (CONFIG_PGTABLE_LEVELS > 3) && (kvm_stage2_levels(kvm) > 3); 64 - } 65 - 66 - #define S2_PUD_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(1) 67 - #define S2_PUD_SIZE (1UL << S2_PUD_SHIFT) 68 - #define S2_PUD_MASK (~(S2_PUD_SIZE - 1)) 69 - 70 - #define stage2_pgd_none(kvm, pgd) pgd_none(pgd) 71 - #define stage2_pgd_clear(kvm, pgd) pgd_clear(pgd) 72 - #define stage2_pgd_present(kvm, pgd) pgd_present(pgd) 73 - #define stage2_pgd_populate(kvm, pgd, p4d) pgd_populate(NULL, pgd, p4d) 74 - 75 - static inline p4d_t *stage2_p4d_offset(struct kvm *kvm, 76 - pgd_t *pgd, unsigned long address) 77 - { 78 - return p4d_offset(pgd, address); 79 - } 80 - 81 - static inline void stage2_p4d_free(struct kvm *kvm, p4d_t *p4d) 82 - { 83 - } 84 - 85 - static inline bool stage2_p4d_table_empty(struct kvm *kvm, p4d_t *p4dp) 86 - { 87 - return false; 88 - } 89 - 90 - static inline phys_addr_t stage2_p4d_addr_end(struct kvm *kvm, 91 - phys_addr_t addr, phys_addr_t end) 92 - { 93 - return end; 94 - } 95 - 96 - static inline bool stage2_p4d_none(struct kvm *kvm, p4d_t p4d) 97 - { 98 - if (kvm_stage2_has_pud(kvm)) 99 - return p4d_none(p4d); 100 - else 101 - return 0; 102 - } 103 - 104 - static inline void stage2_p4d_clear(struct kvm *kvm, p4d_t *p4dp) 105 - { 106 - if (kvm_stage2_has_pud(kvm)) 107 - p4d_clear(p4dp); 108 - } 109 - 110 - static inline bool stage2_p4d_present(struct kvm *kvm, p4d_t p4d) 111 - { 112 - if (kvm_stage2_has_pud(kvm)) 113 - return p4d_present(p4d); 114 - else 115 - return 1; 116 - } 117 - 118 - static inline void stage2_p4d_populate(struct kvm *kvm, p4d_t *p4d, pud_t *pud) 119 - { 120 - if (kvm_stage2_has_pud(kvm)) 121 - p4d_populate(NULL, p4d, pud); 122 - } 123 - 124 - static inline pud_t *stage2_pud_offset(struct kvm *kvm, 125 - p4d_t *p4d, unsigned long address) 126 - { 127 - if (kvm_stage2_has_pud(kvm)) 128 - return pud_offset(p4d, address); 129 - else 130 - return (pud_t *)p4d; 131 - } 132 - 133 - static inline void stage2_pud_free(struct kvm *kvm, pud_t *pud) 134 - { 135 - if (kvm_stage2_has_pud(kvm)) 136 - free_page((unsigned long)pud); 137 - } 138 - 139 - static inline bool stage2_pud_table_empty(struct kvm *kvm, pud_t *pudp) 140 - { 141 - if (kvm_stage2_has_pud(kvm)) 142 - return kvm_page_empty(pudp); 143 - else 144 - return false; 145 - } 146 - 147 - static inline phys_addr_t 148 - stage2_pud_addr_end(struct kvm *kvm, phys_addr_t addr, phys_addr_t end) 149 - { 150 - if (kvm_stage2_has_pud(kvm)) { 151 - phys_addr_t boundary = (addr + S2_PUD_SIZE) & S2_PUD_MASK; 152 - 153 - return (boundary - 1 < end - 1) ? boundary : end; 154 - } else { 155 - return end; 156 - } 157 - } 158 - 159 - /* Stage2 PMD definitions when the level is present */ 160 - static inline bool kvm_stage2_has_pmd(struct kvm *kvm) 161 - { 162 - return (CONFIG_PGTABLE_LEVELS > 2) && (kvm_stage2_levels(kvm) > 2); 163 - } 164 - 165 - #define S2_PMD_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(2) 166 - #define S2_PMD_SIZE (1UL << S2_PMD_SHIFT) 167 - #define S2_PMD_MASK (~(S2_PMD_SIZE - 1)) 168 - 169 - static inline bool stage2_pud_none(struct kvm *kvm, pud_t pud) 170 - { 171 - if (kvm_stage2_has_pmd(kvm)) 172 - return pud_none(pud); 173 - else 174 - return 0; 175 - } 176 - 177 - static inline void stage2_pud_clear(struct kvm *kvm, pud_t *pud) 178 - { 179 - if (kvm_stage2_has_pmd(kvm)) 180 - pud_clear(pud); 181 - } 182 - 183 - static inline bool stage2_pud_present(struct kvm *kvm, pud_t pud) 184 - { 185 - if (kvm_stage2_has_pmd(kvm)) 186 - return pud_present(pud); 187 - else 188 - return 1; 189 - } 190 - 191 - static inline void stage2_pud_populate(struct kvm *kvm, pud_t *pud, pmd_t *pmd) 192 - { 193 - if (kvm_stage2_has_pmd(kvm)) 194 - pud_populate(NULL, pud, pmd); 195 - } 196 - 197 - static inline pmd_t *stage2_pmd_offset(struct kvm *kvm, 198 - pud_t *pud, unsigned long address) 199 - { 200 - if (kvm_stage2_has_pmd(kvm)) 201 - return pmd_offset(pud, address); 202 - else 203 - return (pmd_t *)pud; 204 - } 205 - 206 - static inline void stage2_pmd_free(struct kvm *kvm, pmd_t *pmd) 207 - { 208 - if (kvm_stage2_has_pmd(kvm)) 209 - free_page((unsigned long)pmd); 210 - } 211 - 212 - static inline bool stage2_pud_huge(struct kvm *kvm, pud_t pud) 213 - { 214 - if (kvm_stage2_has_pmd(kvm)) 215 - return pud_huge(pud); 216 - else 217 - return 0; 218 - } 219 - 220 - static inline bool stage2_pmd_table_empty(struct kvm *kvm, pmd_t *pmdp) 221 - { 222 - if (kvm_stage2_has_pmd(kvm)) 223 - return kvm_page_empty(pmdp); 224 - else 225 - return 0; 226 - } 227 - 228 - static inline phys_addr_t 229 - stage2_pmd_addr_end(struct kvm *kvm, phys_addr_t addr, phys_addr_t end) 230 - { 231 - if (kvm_stage2_has_pmd(kvm)) { 232 - phys_addr_t boundary = (addr + S2_PMD_SIZE) & S2_PMD_MASK; 233 - 234 - return (boundary - 1 < end - 1) ? boundary : end; 235 - } else { 236 - return end; 237 - } 238 - } 239 - 240 - static inline bool stage2_pte_table_empty(struct kvm *kvm, pte_t *ptep) 241 - { 242 - return kvm_page_empty(ptep); 243 - } 244 - 245 - static inline unsigned long stage2_pgd_index(struct kvm *kvm, phys_addr_t addr) 246 - { 247 - return (((addr) >> stage2_pgdir_shift(kvm)) & (stage2_pgd_ptrs(kvm) - 1)); 248 - } 249 45 250 46 static inline phys_addr_t 251 47 stage2_pgd_addr_end(struct kvm *kvm, phys_addr_t addr, phys_addr_t end) ··· 49 255 50 256 return (boundary - 1 < end - 1) ? boundary : end; 51 257 } 52 - 53 - /* 54 - * Level values for the ARMv8.4-TTL extension, mapping PUD/PMD/PTE and 55 - * the architectural page-table level. 56 - */ 57 - #define S2_NO_LEVEL_HINT 0 58 - #define S2_PUD_LEVEL 1 59 - #define S2_PMD_LEVEL 2 60 - #define S2_PTE_LEVEL 3 61 258 62 259 #endif /* __ARM64_S2_PGTABLE_H_ */
-755
arch/arm64/kvm/mmu.c
··· 31 31 32 32 static unsigned long io_map_base; 33 33 34 - #define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0) 35 - #define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1) 36 - 37 - static bool is_iomap(unsigned long flags) 38 - { 39 - return flags & KVM_S2PTE_FLAG_IS_IOMAP; 40 - } 41 34 42 35 /* 43 36 * Release kvm_mmu_lock periodically if the memory region is large. Otherwise, ··· 83 90 kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu); 84 91 } 85 92 86 - static void kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu, phys_addr_t ipa, 87 - int level) 88 - { 89 - kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, ipa, level); 90 - } 91 - 92 - /* 93 - * D-Cache management functions. They take the page table entries by 94 - * value, as they are flushing the cache using the kernel mapping (or 95 - * kmap on 32bit). 96 - */ 97 - static void kvm_flush_dcache_pte(pte_t pte) 98 - { 99 - __kvm_flush_dcache_pte(pte); 100 - } 101 - 102 - static void kvm_flush_dcache_pmd(pmd_t pmd) 103 - { 104 - __kvm_flush_dcache_pmd(pmd); 105 - } 106 - 107 - static void kvm_flush_dcache_pud(pud_t pud) 108 - { 109 - __kvm_flush_dcache_pud(pud); 110 - } 111 - 112 93 static bool kvm_is_device_pfn(unsigned long pfn) 113 94 { 114 95 return !pfn_valid(pfn); 115 - } 116 - 117 - /** 118 - * stage2_dissolve_pmd() - clear and flush huge PMD entry 119 - * @mmu: pointer to mmu structure to operate on 120 - * @addr: IPA 121 - * @pmd: pmd pointer for IPA 122 - * 123 - * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. 124 - */ 125 - static void stage2_dissolve_pmd(struct kvm_s2_mmu *mmu, phys_addr_t addr, pmd_t *pmd) 126 - { 127 - if (!pmd_thp_or_huge(*pmd)) 128 - return; 129 - 130 - pmd_clear(pmd); 131 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PMD_LEVEL); 132 - put_page(virt_to_page(pmd)); 133 - } 134 - 135 - /** 136 - * stage2_dissolve_pud() - clear and flush huge PUD entry 137 - * @mmu: pointer to mmu structure to operate on 138 - * @addr: IPA 139 - * @pud: pud pointer for IPA 140 - * 141 - * Function clears a PUD entry, flushes addr 1st and 2nd stage TLBs. 142 - */ 143 - static void stage2_dissolve_pud(struct kvm_s2_mmu *mmu, phys_addr_t addr, pud_t *pudp) 144 - { 145 - struct kvm *kvm = mmu->kvm; 146 - 147 - if (!stage2_pud_huge(kvm, *pudp)) 148 - return; 149 - 150 - stage2_pud_clear(kvm, pudp); 151 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PUD_LEVEL); 152 - put_page(virt_to_page(pudp)); 153 - } 154 - 155 - static void clear_stage2_pgd_entry(struct kvm_s2_mmu *mmu, pgd_t *pgd, phys_addr_t addr) 156 - { 157 - struct kvm *kvm = mmu->kvm; 158 - p4d_t *p4d_table __maybe_unused = stage2_p4d_offset(kvm, pgd, 0UL); 159 - stage2_pgd_clear(kvm, pgd); 160 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_NO_LEVEL_HINT); 161 - stage2_p4d_free(kvm, p4d_table); 162 - put_page(virt_to_page(pgd)); 163 - } 164 - 165 - static void clear_stage2_p4d_entry(struct kvm_s2_mmu *mmu, p4d_t *p4d, phys_addr_t addr) 166 - { 167 - struct kvm *kvm = mmu->kvm; 168 - pud_t *pud_table __maybe_unused = stage2_pud_offset(kvm, p4d, 0); 169 - stage2_p4d_clear(kvm, p4d); 170 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_NO_LEVEL_HINT); 171 - stage2_pud_free(kvm, pud_table); 172 - put_page(virt_to_page(p4d)); 173 - } 174 - 175 - static void clear_stage2_pud_entry(struct kvm_s2_mmu *mmu, pud_t *pud, phys_addr_t addr) 176 - { 177 - struct kvm *kvm = mmu->kvm; 178 - pmd_t *pmd_table __maybe_unused = stage2_pmd_offset(kvm, pud, 0); 179 - 180 - VM_BUG_ON(stage2_pud_huge(kvm, *pud)); 181 - stage2_pud_clear(kvm, pud); 182 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_NO_LEVEL_HINT); 183 - stage2_pmd_free(kvm, pmd_table); 184 - put_page(virt_to_page(pud)); 185 - } 186 - 187 - static void clear_stage2_pmd_entry(struct kvm_s2_mmu *mmu, pmd_t *pmd, phys_addr_t addr) 188 - { 189 - pte_t *pte_table = pte_offset_kernel(pmd, 0); 190 - VM_BUG_ON(pmd_thp_or_huge(*pmd)); 191 - pmd_clear(pmd); 192 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_NO_LEVEL_HINT); 193 - free_page((unsigned long)pte_table); 194 - put_page(virt_to_page(pmd)); 195 - } 196 - 197 - static inline void kvm_set_pte(pte_t *ptep, pte_t new_pte) 198 - { 199 - WRITE_ONCE(*ptep, new_pte); 200 - dsb(ishst); 201 - } 202 - 203 - static inline void kvm_set_pmd(pmd_t *pmdp, pmd_t new_pmd) 204 - { 205 - WRITE_ONCE(*pmdp, new_pmd); 206 - dsb(ishst); 207 - } 208 - 209 - static inline void kvm_pmd_populate(pmd_t *pmdp, pte_t *ptep) 210 - { 211 - kvm_set_pmd(pmdp, kvm_mk_pmd(ptep)); 212 - } 213 - 214 - static inline void kvm_pud_populate(pud_t *pudp, pmd_t *pmdp) 215 - { 216 - WRITE_ONCE(*pudp, kvm_mk_pud(pmdp)); 217 - dsb(ishst); 218 - } 219 - 220 - static inline void kvm_p4d_populate(p4d_t *p4dp, pud_t *pudp) 221 - { 222 - WRITE_ONCE(*p4dp, kvm_mk_p4d(pudp)); 223 - dsb(ishst); 224 - } 225 - 226 - static inline void kvm_pgd_populate(pgd_t *pgdp, p4d_t *p4dp) 227 - { 228 - #ifndef __PAGETABLE_P4D_FOLDED 229 - WRITE_ONCE(*pgdp, kvm_mk_pgd(p4dp)); 230 - dsb(ishst); 231 - #endif 232 96 } 233 97 234 98 /* ··· 112 262 * we then fully enforce cacheability of RAM, no matter what the guest 113 263 * does. 114 264 */ 115 - static void unmap_stage2_ptes(struct kvm_s2_mmu *mmu, pmd_t *pmd, 116 - phys_addr_t addr, phys_addr_t end) 117 - { 118 - phys_addr_t start_addr = addr; 119 - pte_t *pte, *start_pte; 120 - 121 - start_pte = pte = pte_offset_kernel(pmd, addr); 122 - do { 123 - if (!pte_none(*pte)) { 124 - pte_t old_pte = *pte; 125 - 126 - kvm_set_pte(pte, __pte(0)); 127 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PTE_LEVEL); 128 - 129 - /* No need to invalidate the cache for device mappings */ 130 - if (!kvm_is_device_pfn(pte_pfn(old_pte))) 131 - kvm_flush_dcache_pte(old_pte); 132 - 133 - put_page(virt_to_page(pte)); 134 - } 135 - } while (pte++, addr += PAGE_SIZE, addr != end); 136 - 137 - if (stage2_pte_table_empty(mmu->kvm, start_pte)) 138 - clear_stage2_pmd_entry(mmu, pmd, start_addr); 139 - } 140 - 141 - static void unmap_stage2_pmds(struct kvm_s2_mmu *mmu, pud_t *pud, 142 - phys_addr_t addr, phys_addr_t end) 143 - { 144 - struct kvm *kvm = mmu->kvm; 145 - phys_addr_t next, start_addr = addr; 146 - pmd_t *pmd, *start_pmd; 147 - 148 - start_pmd = pmd = stage2_pmd_offset(kvm, pud, addr); 149 - do { 150 - next = stage2_pmd_addr_end(kvm, addr, end); 151 - if (!pmd_none(*pmd)) { 152 - if (pmd_thp_or_huge(*pmd)) { 153 - pmd_t old_pmd = *pmd; 154 - 155 - pmd_clear(pmd); 156 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PMD_LEVEL); 157 - 158 - kvm_flush_dcache_pmd(old_pmd); 159 - 160 - put_page(virt_to_page(pmd)); 161 - } else { 162 - unmap_stage2_ptes(mmu, pmd, addr, next); 163 - } 164 - } 165 - } while (pmd++, addr = next, addr != end); 166 - 167 - if (stage2_pmd_table_empty(kvm, start_pmd)) 168 - clear_stage2_pud_entry(mmu, pud, start_addr); 169 - } 170 - 171 - static void unmap_stage2_puds(struct kvm_s2_mmu *mmu, p4d_t *p4d, 172 - phys_addr_t addr, phys_addr_t end) 173 - { 174 - struct kvm *kvm = mmu->kvm; 175 - phys_addr_t next, start_addr = addr; 176 - pud_t *pud, *start_pud; 177 - 178 - start_pud = pud = stage2_pud_offset(kvm, p4d, addr); 179 - do { 180 - next = stage2_pud_addr_end(kvm, addr, end); 181 - if (!stage2_pud_none(kvm, *pud)) { 182 - if (stage2_pud_huge(kvm, *pud)) { 183 - pud_t old_pud = *pud; 184 - 185 - stage2_pud_clear(kvm, pud); 186 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PUD_LEVEL); 187 - kvm_flush_dcache_pud(old_pud); 188 - put_page(virt_to_page(pud)); 189 - } else { 190 - unmap_stage2_pmds(mmu, pud, addr, next); 191 - } 192 - } 193 - } while (pud++, addr = next, addr != end); 194 - 195 - if (stage2_pud_table_empty(kvm, start_pud)) 196 - clear_stage2_p4d_entry(mmu, p4d, start_addr); 197 - } 198 - 199 - static void unmap_stage2_p4ds(struct kvm_s2_mmu *mmu, pgd_t *pgd, 200 - phys_addr_t addr, phys_addr_t end) 201 - { 202 - struct kvm *kvm = mmu->kvm; 203 - phys_addr_t next, start_addr = addr; 204 - p4d_t *p4d, *start_p4d; 205 - 206 - start_p4d = p4d = stage2_p4d_offset(kvm, pgd, addr); 207 - do { 208 - next = stage2_p4d_addr_end(kvm, addr, end); 209 - if (!stage2_p4d_none(kvm, *p4d)) 210 - unmap_stage2_puds(mmu, p4d, addr, next); 211 - } while (p4d++, addr = next, addr != end); 212 - 213 - if (stage2_p4d_table_empty(kvm, start_p4d)) 214 - clear_stage2_pgd_entry(mmu, pgd, start_addr); 215 - } 216 - 217 265 /** 218 266 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range 219 267 * @kvm: The VM pointer ··· 138 390 static void unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size) 139 391 { 140 392 __unmap_stage2_range(mmu, start, size, true); 141 - } 142 - 143 - static void stage2_flush_ptes(struct kvm_s2_mmu *mmu, pmd_t *pmd, 144 - phys_addr_t addr, phys_addr_t end) 145 - { 146 - pte_t *pte; 147 - 148 - pte = pte_offset_kernel(pmd, addr); 149 - do { 150 - if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte))) 151 - kvm_flush_dcache_pte(*pte); 152 - } while (pte++, addr += PAGE_SIZE, addr != end); 153 - } 154 - 155 - static void stage2_flush_pmds(struct kvm_s2_mmu *mmu, pud_t *pud, 156 - phys_addr_t addr, phys_addr_t end) 157 - { 158 - struct kvm *kvm = mmu->kvm; 159 - pmd_t *pmd; 160 - phys_addr_t next; 161 - 162 - pmd = stage2_pmd_offset(kvm, pud, addr); 163 - do { 164 - next = stage2_pmd_addr_end(kvm, addr, end); 165 - if (!pmd_none(*pmd)) { 166 - if (pmd_thp_or_huge(*pmd)) 167 - kvm_flush_dcache_pmd(*pmd); 168 - else 169 - stage2_flush_ptes(mmu, pmd, addr, next); 170 - } 171 - } while (pmd++, addr = next, addr != end); 172 - } 173 - 174 - static void stage2_flush_puds(struct kvm_s2_mmu *mmu, p4d_t *p4d, 175 - phys_addr_t addr, phys_addr_t end) 176 - { 177 - struct kvm *kvm = mmu->kvm; 178 - pud_t *pud; 179 - phys_addr_t next; 180 - 181 - pud = stage2_pud_offset(kvm, p4d, addr); 182 - do { 183 - next = stage2_pud_addr_end(kvm, addr, end); 184 - if (!stage2_pud_none(kvm, *pud)) { 185 - if (stage2_pud_huge(kvm, *pud)) 186 - kvm_flush_dcache_pud(*pud); 187 - else 188 - stage2_flush_pmds(mmu, pud, addr, next); 189 - } 190 - } while (pud++, addr = next, addr != end); 191 - } 192 - 193 - static void stage2_flush_p4ds(struct kvm_s2_mmu *mmu, pgd_t *pgd, 194 - phys_addr_t addr, phys_addr_t end) 195 - { 196 - struct kvm *kvm = mmu->kvm; 197 - p4d_t *p4d; 198 - phys_addr_t next; 199 - 200 - p4d = stage2_p4d_offset(kvm, pgd, addr); 201 - do { 202 - next = stage2_p4d_addr_end(kvm, addr, end); 203 - if (!stage2_p4d_none(kvm, *p4d)) 204 - stage2_flush_puds(mmu, p4d, addr, next); 205 - } while (p4d++, addr = next, addr != end); 206 393 } 207 394 208 395 static void stage2_flush_memslot(struct kvm *kvm, ··· 488 805 } 489 806 } 490 807 491 - static p4d_t *stage2_get_p4d(struct kvm_s2_mmu *mmu, struct kvm_mmu_memory_cache *cache, 492 - phys_addr_t addr) 493 - { 494 - struct kvm *kvm = mmu->kvm; 495 - pgd_t *pgd; 496 - p4d_t *p4d; 497 - 498 - pgd = mmu->pgd + stage2_pgd_index(kvm, addr); 499 - if (stage2_pgd_none(kvm, *pgd)) { 500 - if (!cache) 501 - return NULL; 502 - p4d = kvm_mmu_memory_cache_alloc(cache); 503 - stage2_pgd_populate(kvm, pgd, p4d); 504 - get_page(virt_to_page(pgd)); 505 - } 506 - 507 - return stage2_p4d_offset(kvm, pgd, addr); 508 - } 509 - 510 - static pud_t *stage2_get_pud(struct kvm_s2_mmu *mmu, struct kvm_mmu_memory_cache *cache, 511 - phys_addr_t addr) 512 - { 513 - struct kvm *kvm = mmu->kvm; 514 - p4d_t *p4d; 515 - pud_t *pud; 516 - 517 - p4d = stage2_get_p4d(mmu, cache, addr); 518 - if (stage2_p4d_none(kvm, *p4d)) { 519 - if (!cache) 520 - return NULL; 521 - pud = kvm_mmu_memory_cache_alloc(cache); 522 - stage2_p4d_populate(kvm, p4d, pud); 523 - get_page(virt_to_page(p4d)); 524 - } 525 - 526 - return stage2_pud_offset(kvm, p4d, addr); 527 - } 528 - 529 - static pmd_t *stage2_get_pmd(struct kvm_s2_mmu *mmu, struct kvm_mmu_memory_cache *cache, 530 - phys_addr_t addr) 531 - { 532 - struct kvm *kvm = mmu->kvm; 533 - pud_t *pud; 534 - pmd_t *pmd; 535 - 536 - pud = stage2_get_pud(mmu, cache, addr); 537 - if (!pud || stage2_pud_huge(kvm, *pud)) 538 - return NULL; 539 - 540 - if (stage2_pud_none(kvm, *pud)) { 541 - if (!cache) 542 - return NULL; 543 - pmd = kvm_mmu_memory_cache_alloc(cache); 544 - stage2_pud_populate(kvm, pud, pmd); 545 - get_page(virt_to_page(pud)); 546 - } 547 - 548 - return stage2_pmd_offset(kvm, pud, addr); 549 - } 550 - 551 - static int stage2_set_pmd_huge(struct kvm_s2_mmu *mmu, 552 - struct kvm_mmu_memory_cache *cache, 553 - phys_addr_t addr, const pmd_t *new_pmd) 554 - { 555 - pmd_t *pmd, old_pmd; 556 - 557 - retry: 558 - pmd = stage2_get_pmd(mmu, cache, addr); 559 - VM_BUG_ON(!pmd); 560 - 561 - old_pmd = *pmd; 562 - /* 563 - * Multiple vcpus faulting on the same PMD entry, can 564 - * lead to them sequentially updating the PMD with the 565 - * same value. Following the break-before-make 566 - * (pmd_clear() followed by tlb_flush()) process can 567 - * hinder forward progress due to refaults generated 568 - * on missing translations. 569 - * 570 - * Skip updating the page table if the entry is 571 - * unchanged. 572 - */ 573 - if (pmd_val(old_pmd) == pmd_val(*new_pmd)) 574 - return 0; 575 - 576 - if (pmd_present(old_pmd)) { 577 - /* 578 - * If we already have PTE level mapping for this block, 579 - * we must unmap it to avoid inconsistent TLB state and 580 - * leaking the table page. We could end up in this situation 581 - * if the memory slot was marked for dirty logging and was 582 - * reverted, leaving PTE level mappings for the pages accessed 583 - * during the period. So, unmap the PTE level mapping for this 584 - * block and retry, as we could have released the upper level 585 - * table in the process. 586 - * 587 - * Normal THP split/merge follows mmu_notifier callbacks and do 588 - * get handled accordingly. 589 - */ 590 - if (!pmd_thp_or_huge(old_pmd)) { 591 - unmap_stage2_range(mmu, addr & S2_PMD_MASK, S2_PMD_SIZE); 592 - goto retry; 593 - } 594 - /* 595 - * Mapping in huge pages should only happen through a 596 - * fault. If a page is merged into a transparent huge 597 - * page, the individual subpages of that huge page 598 - * should be unmapped through MMU notifiers before we 599 - * get here. 600 - * 601 - * Merging of CompoundPages is not supported; they 602 - * should become splitting first, unmapped, merged, 603 - * and mapped back in on-demand. 604 - */ 605 - WARN_ON_ONCE(pmd_pfn(old_pmd) != pmd_pfn(*new_pmd)); 606 - pmd_clear(pmd); 607 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PMD_LEVEL); 608 - } else { 609 - get_page(virt_to_page(pmd)); 610 - } 611 - 612 - kvm_set_pmd(pmd, *new_pmd); 613 - return 0; 614 - } 615 - 616 - static int stage2_set_pud_huge(struct kvm_s2_mmu *mmu, 617 - struct kvm_mmu_memory_cache *cache, 618 - phys_addr_t addr, const pud_t *new_pudp) 619 - { 620 - struct kvm *kvm = mmu->kvm; 621 - pud_t *pudp, old_pud; 622 - 623 - retry: 624 - pudp = stage2_get_pud(mmu, cache, addr); 625 - VM_BUG_ON(!pudp); 626 - 627 - old_pud = *pudp; 628 - 629 - /* 630 - * A large number of vcpus faulting on the same stage 2 entry, 631 - * can lead to a refault due to the stage2_pud_clear()/tlb_flush(). 632 - * Skip updating the page tables if there is no change. 633 - */ 634 - if (pud_val(old_pud) == pud_val(*new_pudp)) 635 - return 0; 636 - 637 - if (stage2_pud_present(kvm, old_pud)) { 638 - /* 639 - * If we already have table level mapping for this block, unmap 640 - * the range for this block and retry. 641 - */ 642 - if (!stage2_pud_huge(kvm, old_pud)) { 643 - unmap_stage2_range(mmu, addr & S2_PUD_MASK, S2_PUD_SIZE); 644 - goto retry; 645 - } 646 - 647 - WARN_ON_ONCE(kvm_pud_pfn(old_pud) != kvm_pud_pfn(*new_pudp)); 648 - stage2_pud_clear(kvm, pudp); 649 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PUD_LEVEL); 650 - } else { 651 - get_page(virt_to_page(pudp)); 652 - } 653 - 654 - kvm_set_pud(pudp, *new_pudp); 655 - return 0; 656 - } 657 - 658 - /* 659 - * stage2_get_leaf_entry - walk the stage2 VM page tables and return 660 - * true if a valid and present leaf-entry is found. A pointer to the 661 - * leaf-entry is returned in the appropriate level variable - pudpp, 662 - * pmdpp, ptepp. 663 - */ 664 - static bool stage2_get_leaf_entry(struct kvm_s2_mmu *mmu, phys_addr_t addr, 665 - pud_t **pudpp, pmd_t **pmdpp, pte_t **ptepp) 666 - { 667 - struct kvm *kvm = mmu->kvm; 668 - pud_t *pudp; 669 - pmd_t *pmdp; 670 - pte_t *ptep; 671 - 672 - *pudpp = NULL; 673 - *pmdpp = NULL; 674 - *ptepp = NULL; 675 - 676 - pudp = stage2_get_pud(mmu, NULL, addr); 677 - if (!pudp || stage2_pud_none(kvm, *pudp) || !stage2_pud_present(kvm, *pudp)) 678 - return false; 679 - 680 - if (stage2_pud_huge(kvm, *pudp)) { 681 - *pudpp = pudp; 682 - return true; 683 - } 684 - 685 - pmdp = stage2_pmd_offset(kvm, pudp, addr); 686 - if (!pmdp || pmd_none(*pmdp) || !pmd_present(*pmdp)) 687 - return false; 688 - 689 - if (pmd_thp_or_huge(*pmdp)) { 690 - *pmdpp = pmdp; 691 - return true; 692 - } 693 - 694 - ptep = pte_offset_kernel(pmdp, addr); 695 - if (!ptep || pte_none(*ptep) || !pte_present(*ptep)) 696 - return false; 697 - 698 - *ptepp = ptep; 699 - return true; 700 - } 701 - 702 - static bool stage2_is_exec(struct kvm_s2_mmu *mmu, phys_addr_t addr, unsigned long sz) 703 - { 704 - pud_t *pudp; 705 - pmd_t *pmdp; 706 - pte_t *ptep; 707 - bool found; 708 - 709 - found = stage2_get_leaf_entry(mmu, addr, &pudp, &pmdp, &ptep); 710 - if (!found) 711 - return false; 712 - 713 - if (pudp) 714 - return sz <= PUD_SIZE && kvm_s2pud_exec(pudp); 715 - else if (pmdp) 716 - return sz <= PMD_SIZE && kvm_s2pmd_exec(pmdp); 717 - else 718 - return sz == PAGE_SIZE && kvm_s2pte_exec(ptep); 719 - } 720 - 721 - static int stage2_set_pte(struct kvm_s2_mmu *mmu, 722 - struct kvm_mmu_memory_cache *cache, 723 - phys_addr_t addr, const pte_t *new_pte, 724 - unsigned long flags) 725 - { 726 - struct kvm *kvm = mmu->kvm; 727 - pud_t *pud; 728 - pmd_t *pmd; 729 - pte_t *pte, old_pte; 730 - bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP; 731 - bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE; 732 - 733 - VM_BUG_ON(logging_active && !cache); 734 - 735 - /* Create stage-2 page table mapping - Levels 0 and 1 */ 736 - pud = stage2_get_pud(mmu, cache, addr); 737 - if (!pud) { 738 - /* 739 - * Ignore calls from kvm_set_spte_hva for unallocated 740 - * address ranges. 741 - */ 742 - return 0; 743 - } 744 - 745 - /* 746 - * While dirty page logging - dissolve huge PUD, then continue 747 - * on to allocate page. 748 - */ 749 - if (logging_active) 750 - stage2_dissolve_pud(mmu, addr, pud); 751 - 752 - if (stage2_pud_none(kvm, *pud)) { 753 - if (!cache) 754 - return 0; /* ignore calls from kvm_set_spte_hva */ 755 - pmd = kvm_mmu_memory_cache_alloc(cache); 756 - stage2_pud_populate(kvm, pud, pmd); 757 - get_page(virt_to_page(pud)); 758 - } 759 - 760 - pmd = stage2_pmd_offset(kvm, pud, addr); 761 - if (!pmd) { 762 - /* 763 - * Ignore calls from kvm_set_spte_hva for unallocated 764 - * address ranges. 765 - */ 766 - return 0; 767 - } 768 - 769 - /* 770 - * While dirty page logging - dissolve huge PMD, then continue on to 771 - * allocate page. 772 - */ 773 - if (logging_active) 774 - stage2_dissolve_pmd(mmu, addr, pmd); 775 - 776 - /* Create stage-2 page mappings - Level 2 */ 777 - if (pmd_none(*pmd)) { 778 - if (!cache) 779 - return 0; /* ignore calls from kvm_set_spte_hva */ 780 - pte = kvm_mmu_memory_cache_alloc(cache); 781 - kvm_pmd_populate(pmd, pte); 782 - get_page(virt_to_page(pmd)); 783 - } 784 - 785 - pte = pte_offset_kernel(pmd, addr); 786 - 787 - if (iomap && pte_present(*pte)) 788 - return -EFAULT; 789 - 790 - /* Create 2nd stage page table mapping - Level 3 */ 791 - old_pte = *pte; 792 - if (pte_present(old_pte)) { 793 - /* Skip page table update if there is no change */ 794 - if (pte_val(old_pte) == pte_val(*new_pte)) 795 - return 0; 796 - 797 - kvm_set_pte(pte, __pte(0)); 798 - kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PTE_LEVEL); 799 - } else { 800 - get_page(virt_to_page(pte)); 801 - } 802 - 803 - kvm_set_pte(pte, *new_pte); 804 - return 0; 805 - } 806 - 807 - #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG 808 - static int stage2_ptep_test_and_clear_young(pte_t *pte) 809 - { 810 - if (pte_young(*pte)) { 811 - *pte = pte_mkold(*pte); 812 - return 1; 813 - } 814 - return 0; 815 - } 816 - #else 817 - static int stage2_ptep_test_and_clear_young(pte_t *pte) 818 - { 819 - return __ptep_test_and_clear_young(pte); 820 - } 821 - #endif 822 - 823 - static int stage2_pmdp_test_and_clear_young(pmd_t *pmd) 824 - { 825 - return stage2_ptep_test_and_clear_young((pte_t *)pmd); 826 - } 827 - 828 - static int stage2_pudp_test_and_clear_young(pud_t *pud) 829 - { 830 - return stage2_ptep_test_and_clear_young((pte_t *)pud); 831 - } 832 - 833 808 /** 834 809 * kvm_phys_addr_ioremap - map a device range to guest IPA 835 810 * ··· 528 1187 529 1188 kvm_mmu_free_memory_cache(&cache); 530 1189 return ret; 531 - } 532 - 533 - /** 534 - * stage2_wp_ptes - write protect PMD range 535 - * @pmd: pointer to pmd entry 536 - * @addr: range start address 537 - * @end: range end address 538 - */ 539 - static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end) 540 - { 541 - pte_t *pte; 542 - 543 - pte = pte_offset_kernel(pmd, addr); 544 - do { 545 - if (!pte_none(*pte)) { 546 - if (!kvm_s2pte_readonly(pte)) 547 - kvm_set_s2pte_readonly(pte); 548 - } 549 - } while (pte++, addr += PAGE_SIZE, addr != end); 550 - } 551 - 552 - /** 553 - * stage2_wp_pmds - write protect PUD range 554 - * kvm: kvm instance for the VM 555 - * @pud: pointer to pud entry 556 - * @addr: range start address 557 - * @end: range end address 558 - */ 559 - static void stage2_wp_pmds(struct kvm_s2_mmu *mmu, pud_t *pud, 560 - phys_addr_t addr, phys_addr_t end) 561 - { 562 - struct kvm *kvm = mmu->kvm; 563 - pmd_t *pmd; 564 - phys_addr_t next; 565 - 566 - pmd = stage2_pmd_offset(kvm, pud, addr); 567 - 568 - do { 569 - next = stage2_pmd_addr_end(kvm, addr, end); 570 - if (!pmd_none(*pmd)) { 571 - if (pmd_thp_or_huge(*pmd)) { 572 - if (!kvm_s2pmd_readonly(pmd)) 573 - kvm_set_s2pmd_readonly(pmd); 574 - } else { 575 - stage2_wp_ptes(pmd, addr, next); 576 - } 577 - } 578 - } while (pmd++, addr = next, addr != end); 579 - } 580 - 581 - /** 582 - * stage2_wp_puds - write protect P4D range 583 - * @p4d: pointer to p4d entry 584 - * @addr: range start address 585 - * @end: range end address 586 - */ 587 - static void stage2_wp_puds(struct kvm_s2_mmu *mmu, p4d_t *p4d, 588 - phys_addr_t addr, phys_addr_t end) 589 - { 590 - struct kvm *kvm = mmu->kvm; 591 - pud_t *pud; 592 - phys_addr_t next; 593 - 594 - pud = stage2_pud_offset(kvm, p4d, addr); 595 - do { 596 - next = stage2_pud_addr_end(kvm, addr, end); 597 - if (!stage2_pud_none(kvm, *pud)) { 598 - if (stage2_pud_huge(kvm, *pud)) { 599 - if (!kvm_s2pud_readonly(pud)) 600 - kvm_set_s2pud_readonly(pud); 601 - } else { 602 - stage2_wp_pmds(mmu, pud, addr, next); 603 - } 604 - } 605 - } while (pud++, addr = next, addr != end); 606 - } 607 - 608 - /** 609 - * stage2_wp_p4ds - write protect PGD range 610 - * @pgd: pointer to pgd entry 611 - * @addr: range start address 612 - * @end: range end address 613 - */ 614 - static void stage2_wp_p4ds(struct kvm_s2_mmu *mmu, pgd_t *pgd, 615 - phys_addr_t addr, phys_addr_t end) 616 - { 617 - struct kvm *kvm = mmu->kvm; 618 - p4d_t *p4d; 619 - phys_addr_t next; 620 - 621 - p4d = stage2_p4d_offset(kvm, pgd, addr); 622 - do { 623 - next = stage2_p4d_addr_end(kvm, addr, end); 624 - if (!stage2_p4d_none(kvm, *p4d)) 625 - stage2_wp_puds(mmu, p4d, addr, next); 626 - } while (p4d++, addr = next, addr != end); 627 1190 } 628 1191 629 1192 /**