137c43753SMarc Zyngier /* 237c43753SMarc Zyngier * Copyright (C) 2012,2013 - ARM Ltd 337c43753SMarc Zyngier * Author: Marc Zyngier <marc.zyngier@arm.com> 437c43753SMarc Zyngier * 537c43753SMarc Zyngier * This program is free software; you can redistribute it and/or modify 637c43753SMarc Zyngier * it under the terms of the GNU General Public License version 2 as 737c43753SMarc Zyngier * published by the Free Software Foundation. 837c43753SMarc Zyngier * 937c43753SMarc Zyngier * This program is distributed in the hope that it will be useful, 1037c43753SMarc Zyngier * but WITHOUT ANY WARRANTY; without even the implied warranty of 1137c43753SMarc Zyngier * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1237c43753SMarc Zyngier * GNU General Public License for more details. 1337c43753SMarc Zyngier * 1437c43753SMarc Zyngier * You should have received a copy of the GNU General Public License 1537c43753SMarc Zyngier * along with this program. If not, see <http://www.gnu.org/licenses/>. 1637c43753SMarc Zyngier */ 1737c43753SMarc Zyngier 1837c43753SMarc Zyngier #ifndef __ARM64_KVM_MMU_H__ 1937c43753SMarc Zyngier #define __ARM64_KVM_MMU_H__ 2037c43753SMarc Zyngier 2137c43753SMarc Zyngier #include <asm/page.h> 2237c43753SMarc Zyngier #include <asm/memory.h> 23*20475f78SVladimir Murzin #include <asm/cpufeature.h> 2437c43753SMarc Zyngier 2537c43753SMarc Zyngier /* 2637c43753SMarc Zyngier * As we only have the TTBR0_EL2 register, we cannot express 2737c43753SMarc Zyngier * "negative" addresses. This makes it impossible to directly share 2837c43753SMarc Zyngier * mappings with the kernel. 2937c43753SMarc Zyngier * 3037c43753SMarc Zyngier * Instead, give the HYP mode its own VA region at a fixed offset from 3137c43753SMarc Zyngier * the kernel by just masking the top bits (which are all ones for a 3237c43753SMarc Zyngier * kernel address). 3337c43753SMarc Zyngier */ 3437c43753SMarc Zyngier #define HYP_PAGE_OFFSET_SHIFT VA_BITS 3537c43753SMarc Zyngier #define HYP_PAGE_OFFSET_MASK ((UL(1) << HYP_PAGE_OFFSET_SHIFT) - 1) 3637c43753SMarc Zyngier #define HYP_PAGE_OFFSET (PAGE_OFFSET & HYP_PAGE_OFFSET_MASK) 3737c43753SMarc Zyngier 3837c43753SMarc Zyngier /* 3937c43753SMarc Zyngier * Our virtual mapping for the idmap-ed MMU-enable code. Must be 4037c43753SMarc Zyngier * shared across all the page-tables. Conveniently, we use the last 4137c43753SMarc Zyngier * possible page, where no kernel mapping will ever exist. 4237c43753SMarc Zyngier */ 4337c43753SMarc Zyngier #define TRAMPOLINE_VA (HYP_PAGE_OFFSET_MASK & PAGE_MASK) 4437c43753SMarc Zyngier 4538f791a4SChristoffer Dall /* 4638f791a4SChristoffer Dall * KVM_MMU_CACHE_MIN_PAGES is the number of stage2 page table translation 4738f791a4SChristoffer Dall * levels in addition to the PGD and potentially the PUD which are 4838f791a4SChristoffer Dall * pre-allocated (we pre-allocate the fake PGD and the PUD when the Stage-2 4938f791a4SChristoffer Dall * tables use one level of tables less than the kernel. 5038f791a4SChristoffer Dall */ 5138f791a4SChristoffer Dall #ifdef CONFIG_ARM64_64K_PAGES 5238f791a4SChristoffer Dall #define KVM_MMU_CACHE_MIN_PAGES 1 5338f791a4SChristoffer Dall #else 5438f791a4SChristoffer Dall #define KVM_MMU_CACHE_MIN_PAGES 2 5538f791a4SChristoffer Dall #endif 5638f791a4SChristoffer Dall 5737c43753SMarc Zyngier #ifdef __ASSEMBLY__ 5837c43753SMarc Zyngier 5937c43753SMarc Zyngier /* 6037c43753SMarc Zyngier * Convert a kernel VA into a HYP VA. 6137c43753SMarc Zyngier * reg: VA to be converted. 6237c43753SMarc Zyngier */ 6337c43753SMarc Zyngier .macro kern_hyp_va reg 6437c43753SMarc Zyngier and \reg, \reg, #HYP_PAGE_OFFSET_MASK 6537c43753SMarc Zyngier .endm 6637c43753SMarc Zyngier 6737c43753SMarc Zyngier #else 6837c43753SMarc Zyngier 6938f791a4SChristoffer Dall #include <asm/pgalloc.h> 7037c43753SMarc Zyngier #include <asm/cachetype.h> 7137c43753SMarc Zyngier #include <asm/cacheflush.h> 72e4c5a685SArd Biesheuvel #include <asm/mmu_context.h> 73e4c5a685SArd Biesheuvel #include <asm/pgtable.h> 7437c43753SMarc Zyngier 7537c43753SMarc Zyngier #define KERN_TO_HYP(kva) ((unsigned long)kva - PAGE_OFFSET + HYP_PAGE_OFFSET) 7637c43753SMarc Zyngier 7737c43753SMarc Zyngier /* 78dbff124eSJoel Schopp * We currently only support a 40bit IPA. 7937c43753SMarc Zyngier */ 80dbff124eSJoel Schopp #define KVM_PHYS_SHIFT (40) 8137c43753SMarc Zyngier #define KVM_PHYS_SIZE (1UL << KVM_PHYS_SHIFT) 8237c43753SMarc Zyngier #define KVM_PHYS_MASK (KVM_PHYS_SIZE - 1UL) 8337c43753SMarc Zyngier 8437c43753SMarc Zyngier int create_hyp_mappings(void *from, void *to); 8537c43753SMarc Zyngier int create_hyp_io_mappings(void *from, void *to, phys_addr_t); 8637c43753SMarc Zyngier void free_boot_hyp_pgd(void); 8737c43753SMarc Zyngier void free_hyp_pgds(void); 8837c43753SMarc Zyngier 89957db105SChristoffer Dall void stage2_unmap_vm(struct kvm *kvm); 9037c43753SMarc Zyngier int kvm_alloc_stage2_pgd(struct kvm *kvm); 9137c43753SMarc Zyngier void kvm_free_stage2_pgd(struct kvm *kvm); 9237c43753SMarc Zyngier int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, 93c40f2f8fSArd Biesheuvel phys_addr_t pa, unsigned long size, bool writable); 9437c43753SMarc Zyngier 9537c43753SMarc Zyngier int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run); 9637c43753SMarc Zyngier 9737c43753SMarc Zyngier void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu); 9837c43753SMarc Zyngier 9937c43753SMarc Zyngier phys_addr_t kvm_mmu_get_httbr(void); 10037c43753SMarc Zyngier phys_addr_t kvm_mmu_get_boot_httbr(void); 10137c43753SMarc Zyngier phys_addr_t kvm_get_idmap_vector(void); 10237c43753SMarc Zyngier int kvm_mmu_init(void); 10337c43753SMarc Zyngier void kvm_clear_hyp_idmap(void); 10437c43753SMarc Zyngier 10537c43753SMarc Zyngier #define kvm_set_pte(ptep, pte) set_pte(ptep, pte) 106ad361f09SChristoffer Dall #define kvm_set_pmd(pmdp, pmd) set_pmd(pmdp, pmd) 10737c43753SMarc Zyngier 10837c43753SMarc Zyngier static inline void kvm_clean_pgd(pgd_t *pgd) {} 10938f791a4SChristoffer Dall static inline void kvm_clean_pmd(pmd_t *pmd) {} 11037c43753SMarc Zyngier static inline void kvm_clean_pmd_entry(pmd_t *pmd) {} 11137c43753SMarc Zyngier static inline void kvm_clean_pte(pte_t *pte) {} 11237c43753SMarc Zyngier static inline void kvm_clean_pte_entry(pte_t *pte) {} 11337c43753SMarc Zyngier 11437c43753SMarc Zyngier static inline void kvm_set_s2pte_writable(pte_t *pte) 11537c43753SMarc Zyngier { 11637c43753SMarc Zyngier pte_val(*pte) |= PTE_S2_RDWR; 11737c43753SMarc Zyngier } 11837c43753SMarc Zyngier 119ad361f09SChristoffer Dall static inline void kvm_set_s2pmd_writable(pmd_t *pmd) 120ad361f09SChristoffer Dall { 121ad361f09SChristoffer Dall pmd_val(*pmd) |= PMD_S2_RDWR; 122ad361f09SChristoffer Dall } 123ad361f09SChristoffer Dall 1248199ed0eSMario Smarduch static inline void kvm_set_s2pte_readonly(pte_t *pte) 1258199ed0eSMario Smarduch { 1268199ed0eSMario Smarduch pte_val(*pte) = (pte_val(*pte) & ~PTE_S2_RDWR) | PTE_S2_RDONLY; 1278199ed0eSMario Smarduch } 1288199ed0eSMario Smarduch 1298199ed0eSMario Smarduch static inline bool kvm_s2pte_readonly(pte_t *pte) 1308199ed0eSMario Smarduch { 1318199ed0eSMario Smarduch return (pte_val(*pte) & PTE_S2_RDWR) == PTE_S2_RDONLY; 1328199ed0eSMario Smarduch } 1338199ed0eSMario Smarduch 1348199ed0eSMario Smarduch static inline void kvm_set_s2pmd_readonly(pmd_t *pmd) 1358199ed0eSMario Smarduch { 1368199ed0eSMario Smarduch pmd_val(*pmd) = (pmd_val(*pmd) & ~PMD_S2_RDWR) | PMD_S2_RDONLY; 1378199ed0eSMario Smarduch } 1388199ed0eSMario Smarduch 1398199ed0eSMario Smarduch static inline bool kvm_s2pmd_readonly(pmd_t *pmd) 1408199ed0eSMario Smarduch { 1418199ed0eSMario Smarduch return (pmd_val(*pmd) & PMD_S2_RDWR) == PMD_S2_RDONLY; 1428199ed0eSMario Smarduch } 1438199ed0eSMario Smarduch 1448199ed0eSMario Smarduch 145a3c8bd31SMarc Zyngier #define kvm_pgd_addr_end(addr, end) pgd_addr_end(addr, end) 146a3c8bd31SMarc Zyngier #define kvm_pud_addr_end(addr, end) pud_addr_end(addr, end) 147a3c8bd31SMarc Zyngier #define kvm_pmd_addr_end(addr, end) pmd_addr_end(addr, end) 148a3c8bd31SMarc Zyngier 14938f791a4SChristoffer Dall /* 15038f791a4SChristoffer Dall * In the case where PGDIR_SHIFT is larger than KVM_PHYS_SHIFT, we can address 15138f791a4SChristoffer Dall * the entire IPA input range with a single pgd entry, and we would only need 15238f791a4SChristoffer Dall * one pgd entry. Note that in this case, the pgd is actually not used by 15338f791a4SChristoffer Dall * the MMU for Stage-2 translations, but is merely a fake pgd used as a data 15438f791a4SChristoffer Dall * structure for the kernel pgtable macros to work. 15538f791a4SChristoffer Dall */ 15638f791a4SChristoffer Dall #if PGDIR_SHIFT > KVM_PHYS_SHIFT 15738f791a4SChristoffer Dall #define PTRS_PER_S2_PGD_SHIFT 0 15838f791a4SChristoffer Dall #else 15938f791a4SChristoffer Dall #define PTRS_PER_S2_PGD_SHIFT (KVM_PHYS_SHIFT - PGDIR_SHIFT) 16038f791a4SChristoffer Dall #endif 16138f791a4SChristoffer Dall #define PTRS_PER_S2_PGD (1 << PTRS_PER_S2_PGD_SHIFT) 16238f791a4SChristoffer Dall 16304b8dc85SMarc Zyngier #define kvm_pgd_index(addr) (((addr) >> PGDIR_SHIFT) & (PTRS_PER_S2_PGD - 1)) 16404b8dc85SMarc Zyngier 16538f791a4SChristoffer Dall /* 16638f791a4SChristoffer Dall * If we are concatenating first level stage-2 page tables, we would have less 16738f791a4SChristoffer Dall * than or equal to 16 pointers in the fake PGD, because that's what the 1689f25e6adSKirill A. Shutemov * architecture allows. In this case, (4 - CONFIG_PGTABLE_LEVELS) 16938f791a4SChristoffer Dall * represents the first level for the host, and we add 1 to go to the next 17038f791a4SChristoffer Dall * level (which uses contatenation) for the stage-2 tables. 17138f791a4SChristoffer Dall */ 17238f791a4SChristoffer Dall #if PTRS_PER_S2_PGD <= 16 1739f25e6adSKirill A. Shutemov #define KVM_PREALLOC_LEVEL (4 - CONFIG_PGTABLE_LEVELS + 1) 17438f791a4SChristoffer Dall #else 17538f791a4SChristoffer Dall #define KVM_PREALLOC_LEVEL (0) 17638f791a4SChristoffer Dall #endif 17738f791a4SChristoffer Dall 17838f791a4SChristoffer Dall static inline void *kvm_get_hwpgd(struct kvm *kvm) 17938f791a4SChristoffer Dall { 18038f791a4SChristoffer Dall pgd_t *pgd = kvm->arch.pgd; 18138f791a4SChristoffer Dall pud_t *pud; 18238f791a4SChristoffer Dall 18338f791a4SChristoffer Dall if (KVM_PREALLOC_LEVEL == 0) 18438f791a4SChristoffer Dall return pgd; 18538f791a4SChristoffer Dall 18638f791a4SChristoffer Dall pud = pud_offset(pgd, 0); 18738f791a4SChristoffer Dall if (KVM_PREALLOC_LEVEL == 1) 18838f791a4SChristoffer Dall return pud; 18938f791a4SChristoffer Dall 19038f791a4SChristoffer Dall BUG_ON(KVM_PREALLOC_LEVEL != 2); 19138f791a4SChristoffer Dall return pmd_offset(pud, 0); 19238f791a4SChristoffer Dall } 19338f791a4SChristoffer Dall 194a987370fSMarc Zyngier static inline unsigned int kvm_get_hwpgd_size(void) 19538f791a4SChristoffer Dall { 196a987370fSMarc Zyngier if (KVM_PREALLOC_LEVEL > 0) 197a987370fSMarc Zyngier return PTRS_PER_S2_PGD * PAGE_SIZE; 198a987370fSMarc Zyngier return PTRS_PER_S2_PGD * sizeof(pgd_t); 19938f791a4SChristoffer Dall } 20038f791a4SChristoffer Dall 2014f853a71SChristoffer Dall static inline bool kvm_page_empty(void *ptr) 2024f853a71SChristoffer Dall { 2034f853a71SChristoffer Dall struct page *ptr_page = virt_to_page(ptr); 2044f853a71SChristoffer Dall return page_count(ptr_page) == 1; 2054f853a71SChristoffer Dall } 2064f853a71SChristoffer Dall 20738f791a4SChristoffer Dall #define kvm_pte_table_empty(kvm, ptep) kvm_page_empty(ptep) 20838f791a4SChristoffer Dall 20938f791a4SChristoffer Dall #ifdef __PAGETABLE_PMD_FOLDED 21038f791a4SChristoffer Dall #define kvm_pmd_table_empty(kvm, pmdp) (0) 2114f853a71SChristoffer Dall #else 21238f791a4SChristoffer Dall #define kvm_pmd_table_empty(kvm, pmdp) \ 21338f791a4SChristoffer Dall (kvm_page_empty(pmdp) && (!(kvm) || KVM_PREALLOC_LEVEL < 2)) 2144f853a71SChristoffer Dall #endif 21538f791a4SChristoffer Dall 21638f791a4SChristoffer Dall #ifdef __PAGETABLE_PUD_FOLDED 21738f791a4SChristoffer Dall #define kvm_pud_table_empty(kvm, pudp) (0) 21838f791a4SChristoffer Dall #else 21938f791a4SChristoffer Dall #define kvm_pud_table_empty(kvm, pudp) \ 22038f791a4SChristoffer Dall (kvm_page_empty(pudp) && (!(kvm) || KVM_PREALLOC_LEVEL < 1)) 22138f791a4SChristoffer Dall #endif 2224f853a71SChristoffer Dall 2234f853a71SChristoffer Dall 22437c43753SMarc Zyngier struct kvm; 22537c43753SMarc Zyngier 2262d58b733SMarc Zyngier #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l)) 2272d58b733SMarc Zyngier 2282d58b733SMarc Zyngier static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu) 2292d58b733SMarc Zyngier { 2302d58b733SMarc Zyngier return (vcpu_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101; 2312d58b733SMarc Zyngier } 2322d58b733SMarc Zyngier 2330d3e4d4fSMarc Zyngier static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu, pfn_t pfn, 234840f4bfbSLaszlo Ersek unsigned long size, 235840f4bfbSLaszlo Ersek bool ipa_uncached) 23637c43753SMarc Zyngier { 2370d3e4d4fSMarc Zyngier void *va = page_address(pfn_to_page(pfn)); 2380d3e4d4fSMarc Zyngier 239840f4bfbSLaszlo Ersek if (!vcpu_has_cache_enabled(vcpu) || ipa_uncached) 2400d3e4d4fSMarc Zyngier kvm_flush_dcache_to_poc(va, size); 2412d58b733SMarc Zyngier 24237c43753SMarc Zyngier if (!icache_is_aliasing()) { /* PIPT */ 2430d3e4d4fSMarc Zyngier flush_icache_range((unsigned long)va, 2440d3e4d4fSMarc Zyngier (unsigned long)va + size); 24537c43753SMarc Zyngier } else if (!icache_is_aivivt()) { /* non ASID-tagged VIVT */ 24637c43753SMarc Zyngier /* any kind of VIPT cache */ 24737c43753SMarc Zyngier __flush_icache_all(); 24837c43753SMarc Zyngier } 24937c43753SMarc Zyngier } 25037c43753SMarc Zyngier 251363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pte(pte_t pte) 252363ef89fSMarc Zyngier { 253363ef89fSMarc Zyngier struct page *page = pte_page(pte); 254363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE); 255363ef89fSMarc Zyngier } 256363ef89fSMarc Zyngier 257363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pmd(pmd_t pmd) 258363ef89fSMarc Zyngier { 259363ef89fSMarc Zyngier struct page *page = pmd_page(pmd); 260363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE); 261363ef89fSMarc Zyngier } 262363ef89fSMarc Zyngier 263363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pud(pud_t pud) 264363ef89fSMarc Zyngier { 265363ef89fSMarc Zyngier struct page *page = pud_page(pud); 266363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE); 267363ef89fSMarc Zyngier } 268363ef89fSMarc Zyngier 2694fda342cSSantosh Shilimkar #define kvm_virt_to_phys(x) __virt_to_phys((unsigned long)(x)) 27037c43753SMarc Zyngier 2713c1e7165SMarc Zyngier void kvm_set_way_flush(struct kvm_vcpu *vcpu); 2723c1e7165SMarc Zyngier void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled); 2739d218a1fSMarc Zyngier 274e4c5a685SArd Biesheuvel static inline bool __kvm_cpu_uses_extended_idmap(void) 275e4c5a685SArd Biesheuvel { 276e4c5a685SArd Biesheuvel return __cpu_uses_extended_idmap(); 277e4c5a685SArd Biesheuvel } 278e4c5a685SArd Biesheuvel 279e4c5a685SArd Biesheuvel static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd, 280e4c5a685SArd Biesheuvel pgd_t *hyp_pgd, 281e4c5a685SArd Biesheuvel pgd_t *merged_hyp_pgd, 282e4c5a685SArd Biesheuvel unsigned long hyp_idmap_start) 283e4c5a685SArd Biesheuvel { 284e4c5a685SArd Biesheuvel int idmap_idx; 285e4c5a685SArd Biesheuvel 286e4c5a685SArd Biesheuvel /* 287e4c5a685SArd Biesheuvel * Use the first entry to access the HYP mappings. It is 288e4c5a685SArd Biesheuvel * guaranteed to be free, otherwise we wouldn't use an 289e4c5a685SArd Biesheuvel * extended idmap. 290e4c5a685SArd Biesheuvel */ 291e4c5a685SArd Biesheuvel VM_BUG_ON(pgd_val(merged_hyp_pgd[0])); 292e4c5a685SArd Biesheuvel merged_hyp_pgd[0] = __pgd(__pa(hyp_pgd) | PMD_TYPE_TABLE); 293e4c5a685SArd Biesheuvel 294e4c5a685SArd Biesheuvel /* 295e4c5a685SArd Biesheuvel * Create another extended level entry that points to the boot HYP map, 296e4c5a685SArd Biesheuvel * which contains an ID mapping of the HYP init code. We essentially 297e4c5a685SArd Biesheuvel * merge the boot and runtime HYP maps by doing so, but they don't 298e4c5a685SArd Biesheuvel * overlap anyway, so this is fine. 299e4c5a685SArd Biesheuvel */ 300e4c5a685SArd Biesheuvel idmap_idx = hyp_idmap_start >> VA_BITS; 301e4c5a685SArd Biesheuvel VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx])); 302e4c5a685SArd Biesheuvel merged_hyp_pgd[idmap_idx] = __pgd(__pa(boot_hyp_pgd) | PMD_TYPE_TABLE); 303e4c5a685SArd Biesheuvel } 304e4c5a685SArd Biesheuvel 305*20475f78SVladimir Murzin static inline unsigned int kvm_get_vmid_bits(void) 306*20475f78SVladimir Murzin { 307*20475f78SVladimir Murzin int reg = read_system_reg(SYS_ID_AA64MMFR1_EL1); 308*20475f78SVladimir Murzin 309*20475f78SVladimir Murzin return (cpuid_feature_extract_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8; 310*20475f78SVladimir Murzin } 311*20475f78SVladimir Murzin 31237c43753SMarc Zyngier #endif /* __ASSEMBLY__ */ 31337c43753SMarc Zyngier #endif /* __ARM64_KVM_MMU_H__ */ 314