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> 2320475f78SVladimir Murzin #include <asm/cpufeature.h> 2437c43753SMarc Zyngier 2537c43753SMarc Zyngier /* 26cedbb8b7SMarc Zyngier * As ARMv8.0 only has 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 32*82a81bffSMarc Zyngier * kernel address). We need to find out how many bits to mask. 33cedbb8b7SMarc Zyngier * 34*82a81bffSMarc Zyngier * We want to build a set of page tables that cover both parts of the 35*82a81bffSMarc Zyngier * idmap (the trampoline page used to initialize EL2), and our normal 36*82a81bffSMarc Zyngier * runtime VA space, at the same time. 37*82a81bffSMarc Zyngier * 38*82a81bffSMarc Zyngier * Given that the kernel uses VA_BITS for its entire address space, 39*82a81bffSMarc Zyngier * and that half of that space (VA_BITS - 1) is used for the linear 40*82a81bffSMarc Zyngier * mapping, we can also limit the EL2 space to (VA_BITS - 1). 41*82a81bffSMarc Zyngier * 42*82a81bffSMarc Zyngier * The main question is "Within the VA_BITS space, does EL2 use the 43*82a81bffSMarc Zyngier * top or the bottom half of that space to shadow the kernel's linear 44*82a81bffSMarc Zyngier * mapping?". As we need to idmap the trampoline page, this is 45*82a81bffSMarc Zyngier * determined by the range in which this page lives. 46*82a81bffSMarc Zyngier * 47*82a81bffSMarc Zyngier * If the page is in the bottom half, we have to use the top half. If 48*82a81bffSMarc Zyngier * the page is in the top half, we have to use the bottom half: 49*82a81bffSMarc Zyngier * 50*82a81bffSMarc Zyngier * T = __virt_to_phys(__hyp_idmap_text_start) 51*82a81bffSMarc Zyngier * if (T & BIT(VA_BITS - 1)) 52*82a81bffSMarc Zyngier * HYP_VA_MIN = 0 //idmap in upper half 53*82a81bffSMarc Zyngier * else 54*82a81bffSMarc Zyngier * HYP_VA_MIN = 1 << (VA_BITS - 1) 55*82a81bffSMarc Zyngier * HYP_VA_MAX = HYP_VA_MIN + (1 << (VA_BITS - 1)) - 1 56*82a81bffSMarc Zyngier * 57*82a81bffSMarc Zyngier * This of course assumes that the trampoline page exists within the 58*82a81bffSMarc Zyngier * VA_BITS range. If it doesn't, then it means we're in the odd case 59*82a81bffSMarc Zyngier * where the kernel idmap (as well as HYP) uses more levels than the 60*82a81bffSMarc Zyngier * kernel runtime page tables (as seen when the kernel is configured 61*82a81bffSMarc Zyngier * for 4k pages, 39bits VA, and yet memory lives just above that 62*82a81bffSMarc Zyngier * limit, forcing the idmap to use 4 levels of page tables while the 63*82a81bffSMarc Zyngier * kernel itself only uses 3). In this particular case, it doesn't 64*82a81bffSMarc Zyngier * matter which side of VA_BITS we use, as we're guaranteed not to 65*82a81bffSMarc Zyngier * conflict with anything. 66*82a81bffSMarc Zyngier * 67*82a81bffSMarc Zyngier * When using VHE, there are no separate hyp mappings and all KVM 68*82a81bffSMarc Zyngier * functionality is already mapped as part of the main kernel 69*82a81bffSMarc Zyngier * mappings, and none of this applies in that case. 7037c43753SMarc Zyngier */ 7137c43753SMarc Zyngier #define HYP_PAGE_OFFSET_SHIFT VA_BITS 7237c43753SMarc Zyngier #define HYP_PAGE_OFFSET_MASK ((UL(1) << HYP_PAGE_OFFSET_SHIFT) - 1) 7337c43753SMarc Zyngier #define HYP_PAGE_OFFSET (PAGE_OFFSET & HYP_PAGE_OFFSET_MASK) 7437c43753SMarc Zyngier 7537c43753SMarc Zyngier /* 7637c43753SMarc Zyngier * Our virtual mapping for the idmap-ed MMU-enable code. Must be 7737c43753SMarc Zyngier * shared across all the page-tables. Conveniently, we use the last 7837c43753SMarc Zyngier * possible page, where no kernel mapping will ever exist. 7937c43753SMarc Zyngier */ 8037c43753SMarc Zyngier #define TRAMPOLINE_VA (HYP_PAGE_OFFSET_MASK & PAGE_MASK) 8137c43753SMarc Zyngier 8237c43753SMarc Zyngier #ifdef __ASSEMBLY__ 8337c43753SMarc Zyngier 84cedbb8b7SMarc Zyngier #include <asm/alternative.h> 85cedbb8b7SMarc Zyngier #include <asm/cpufeature.h> 86cedbb8b7SMarc Zyngier 8737c43753SMarc Zyngier /* 8837c43753SMarc Zyngier * Convert a kernel VA into a HYP VA. 8937c43753SMarc Zyngier * reg: VA to be converted. 9037c43753SMarc Zyngier */ 9137c43753SMarc Zyngier .macro kern_hyp_va reg 92cedbb8b7SMarc Zyngier alternative_if_not ARM64_HAS_VIRT_HOST_EXTN 9337c43753SMarc Zyngier and \reg, \reg, #HYP_PAGE_OFFSET_MASK 94cedbb8b7SMarc Zyngier alternative_else 95cedbb8b7SMarc Zyngier nop 96cedbb8b7SMarc Zyngier alternative_endif 9737c43753SMarc Zyngier .endm 9837c43753SMarc Zyngier 9937c43753SMarc Zyngier #else 10037c43753SMarc Zyngier 10138f791a4SChristoffer Dall #include <asm/pgalloc.h> 10237c43753SMarc Zyngier #include <asm/cachetype.h> 10337c43753SMarc Zyngier #include <asm/cacheflush.h> 104e4c5a685SArd Biesheuvel #include <asm/mmu_context.h> 105e4c5a685SArd Biesheuvel #include <asm/pgtable.h> 10637c43753SMarc Zyngier 10737c43753SMarc Zyngier #define KERN_TO_HYP(kva) ((unsigned long)kva - PAGE_OFFSET + HYP_PAGE_OFFSET) 10837c43753SMarc Zyngier 10937c43753SMarc Zyngier /* 110dbff124eSJoel Schopp * We currently only support a 40bit IPA. 11137c43753SMarc Zyngier */ 112dbff124eSJoel Schopp #define KVM_PHYS_SHIFT (40) 11337c43753SMarc Zyngier #define KVM_PHYS_SIZE (1UL << KVM_PHYS_SHIFT) 11437c43753SMarc Zyngier #define KVM_PHYS_MASK (KVM_PHYS_SIZE - 1UL) 11537c43753SMarc Zyngier 116c0ef6326SSuzuki K Poulose #include <asm/stage2_pgtable.h> 117c0ef6326SSuzuki K Poulose 118c8dddecdSMarc Zyngier int create_hyp_mappings(void *from, void *to, pgprot_t prot); 11937c43753SMarc Zyngier int create_hyp_io_mappings(void *from, void *to, phys_addr_t); 12037c43753SMarc Zyngier void free_boot_hyp_pgd(void); 12137c43753SMarc Zyngier void free_hyp_pgds(void); 12237c43753SMarc Zyngier 123957db105SChristoffer Dall void stage2_unmap_vm(struct kvm *kvm); 12437c43753SMarc Zyngier int kvm_alloc_stage2_pgd(struct kvm *kvm); 12537c43753SMarc Zyngier void kvm_free_stage2_pgd(struct kvm *kvm); 12637c43753SMarc Zyngier int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, 127c40f2f8fSArd Biesheuvel phys_addr_t pa, unsigned long size, bool writable); 12837c43753SMarc Zyngier 12937c43753SMarc Zyngier int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run); 13037c43753SMarc Zyngier 13137c43753SMarc Zyngier void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu); 13237c43753SMarc Zyngier 13337c43753SMarc Zyngier phys_addr_t kvm_mmu_get_httbr(void); 13437c43753SMarc Zyngier phys_addr_t kvm_mmu_get_boot_httbr(void); 13537c43753SMarc Zyngier phys_addr_t kvm_get_idmap_vector(void); 13667f69197SAKASHI Takahiro phys_addr_t kvm_get_idmap_start(void); 13737c43753SMarc Zyngier int kvm_mmu_init(void); 13837c43753SMarc Zyngier void kvm_clear_hyp_idmap(void); 13937c43753SMarc Zyngier 14037c43753SMarc Zyngier #define kvm_set_pte(ptep, pte) set_pte(ptep, pte) 141ad361f09SChristoffer Dall #define kvm_set_pmd(pmdp, pmd) set_pmd(pmdp, pmd) 14237c43753SMarc Zyngier 14337c43753SMarc Zyngier static inline void kvm_clean_pgd(pgd_t *pgd) {} 14438f791a4SChristoffer Dall static inline void kvm_clean_pmd(pmd_t *pmd) {} 14537c43753SMarc Zyngier static inline void kvm_clean_pmd_entry(pmd_t *pmd) {} 14637c43753SMarc Zyngier static inline void kvm_clean_pte(pte_t *pte) {} 14737c43753SMarc Zyngier static inline void kvm_clean_pte_entry(pte_t *pte) {} 14837c43753SMarc Zyngier 14906485053SCatalin Marinas static inline pte_t kvm_s2pte_mkwrite(pte_t pte) 15037c43753SMarc Zyngier { 15106485053SCatalin Marinas pte_val(pte) |= PTE_S2_RDWR; 15206485053SCatalin Marinas return pte; 15337c43753SMarc Zyngier } 15437c43753SMarc Zyngier 15506485053SCatalin Marinas static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd) 156ad361f09SChristoffer Dall { 15706485053SCatalin Marinas pmd_val(pmd) |= PMD_S2_RDWR; 15806485053SCatalin Marinas return pmd; 159ad361f09SChristoffer Dall } 160ad361f09SChristoffer Dall 1618199ed0eSMario Smarduch static inline void kvm_set_s2pte_readonly(pte_t *pte) 1628199ed0eSMario Smarduch { 16306485053SCatalin Marinas pteval_t pteval; 16406485053SCatalin Marinas unsigned long tmp; 16506485053SCatalin Marinas 16606485053SCatalin Marinas asm volatile("// kvm_set_s2pte_readonly\n" 16706485053SCatalin Marinas " prfm pstl1strm, %2\n" 16806485053SCatalin Marinas "1: ldxr %0, %2\n" 16906485053SCatalin Marinas " and %0, %0, %3 // clear PTE_S2_RDWR\n" 17006485053SCatalin Marinas " orr %0, %0, %4 // set PTE_S2_RDONLY\n" 17106485053SCatalin Marinas " stxr %w1, %0, %2\n" 17206485053SCatalin Marinas " cbnz %w1, 1b\n" 17306485053SCatalin Marinas : "=&r" (pteval), "=&r" (tmp), "+Q" (pte_val(*pte)) 17406485053SCatalin Marinas : "L" (~PTE_S2_RDWR), "L" (PTE_S2_RDONLY)); 1758199ed0eSMario Smarduch } 1768199ed0eSMario Smarduch 1778199ed0eSMario Smarduch static inline bool kvm_s2pte_readonly(pte_t *pte) 1788199ed0eSMario Smarduch { 1798199ed0eSMario Smarduch return (pte_val(*pte) & PTE_S2_RDWR) == PTE_S2_RDONLY; 1808199ed0eSMario Smarduch } 1818199ed0eSMario Smarduch 1828199ed0eSMario Smarduch static inline void kvm_set_s2pmd_readonly(pmd_t *pmd) 1838199ed0eSMario Smarduch { 18406485053SCatalin Marinas kvm_set_s2pte_readonly((pte_t *)pmd); 1858199ed0eSMario Smarduch } 1868199ed0eSMario Smarduch 1878199ed0eSMario Smarduch static inline bool kvm_s2pmd_readonly(pmd_t *pmd) 1888199ed0eSMario Smarduch { 18906485053SCatalin Marinas return kvm_s2pte_readonly((pte_t *)pmd); 19038f791a4SChristoffer Dall } 19138f791a4SChristoffer Dall 1924f853a71SChristoffer Dall static inline bool kvm_page_empty(void *ptr) 1934f853a71SChristoffer Dall { 1944f853a71SChristoffer Dall struct page *ptr_page = virt_to_page(ptr); 1954f853a71SChristoffer Dall return page_count(ptr_page) == 1; 1964f853a71SChristoffer Dall } 1974f853a71SChristoffer Dall 19866f877faSSuzuki K Poulose #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep) 19938f791a4SChristoffer Dall 20038f791a4SChristoffer Dall #ifdef __PAGETABLE_PMD_FOLDED 20166f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) (0) 2024f853a71SChristoffer Dall #else 20366f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp) 2044f853a71SChristoffer Dall #endif 20538f791a4SChristoffer Dall 20638f791a4SChristoffer Dall #ifdef __PAGETABLE_PUD_FOLDED 20766f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) (0) 20838f791a4SChristoffer Dall #else 20966f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) kvm_page_empty(pudp) 21038f791a4SChristoffer Dall #endif 2114f853a71SChristoffer Dall 21237c43753SMarc Zyngier struct kvm; 21337c43753SMarc Zyngier 2142d58b733SMarc Zyngier #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l)) 2152d58b733SMarc Zyngier 2162d58b733SMarc Zyngier static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu) 2172d58b733SMarc Zyngier { 2182d58b733SMarc Zyngier return (vcpu_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101; 2192d58b733SMarc Zyngier } 2202d58b733SMarc Zyngier 221ba049e93SDan Williams static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu, 222ba049e93SDan Williams kvm_pfn_t pfn, 223840f4bfbSLaszlo Ersek unsigned long size, 224840f4bfbSLaszlo Ersek bool ipa_uncached) 22537c43753SMarc Zyngier { 2260d3e4d4fSMarc Zyngier void *va = page_address(pfn_to_page(pfn)); 2270d3e4d4fSMarc Zyngier 228840f4bfbSLaszlo Ersek if (!vcpu_has_cache_enabled(vcpu) || ipa_uncached) 2290d3e4d4fSMarc Zyngier kvm_flush_dcache_to_poc(va, size); 2302d58b733SMarc Zyngier 23137c43753SMarc Zyngier if (!icache_is_aliasing()) { /* PIPT */ 2320d3e4d4fSMarc Zyngier flush_icache_range((unsigned long)va, 2330d3e4d4fSMarc Zyngier (unsigned long)va + size); 23437c43753SMarc Zyngier } else if (!icache_is_aivivt()) { /* non ASID-tagged VIVT */ 23537c43753SMarc Zyngier /* any kind of VIPT cache */ 23637c43753SMarc Zyngier __flush_icache_all(); 23737c43753SMarc Zyngier } 23837c43753SMarc Zyngier } 23937c43753SMarc Zyngier 240363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pte(pte_t pte) 241363ef89fSMarc Zyngier { 242363ef89fSMarc Zyngier struct page *page = pte_page(pte); 243363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE); 244363ef89fSMarc Zyngier } 245363ef89fSMarc Zyngier 246363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pmd(pmd_t pmd) 247363ef89fSMarc Zyngier { 248363ef89fSMarc Zyngier struct page *page = pmd_page(pmd); 249363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE); 250363ef89fSMarc Zyngier } 251363ef89fSMarc Zyngier 252363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pud(pud_t pud) 253363ef89fSMarc Zyngier { 254363ef89fSMarc Zyngier struct page *page = pud_page(pud); 255363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE); 256363ef89fSMarc Zyngier } 257363ef89fSMarc Zyngier 2584fda342cSSantosh Shilimkar #define kvm_virt_to_phys(x) __virt_to_phys((unsigned long)(x)) 25937c43753SMarc Zyngier 2603c1e7165SMarc Zyngier void kvm_set_way_flush(struct kvm_vcpu *vcpu); 2613c1e7165SMarc Zyngier void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled); 2629d218a1fSMarc Zyngier 263e4c5a685SArd Biesheuvel static inline bool __kvm_cpu_uses_extended_idmap(void) 264e4c5a685SArd Biesheuvel { 265e4c5a685SArd Biesheuvel return __cpu_uses_extended_idmap(); 266e4c5a685SArd Biesheuvel } 267e4c5a685SArd Biesheuvel 268e4c5a685SArd Biesheuvel static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd, 269e4c5a685SArd Biesheuvel pgd_t *hyp_pgd, 270e4c5a685SArd Biesheuvel pgd_t *merged_hyp_pgd, 271e4c5a685SArd Biesheuvel unsigned long hyp_idmap_start) 272e4c5a685SArd Biesheuvel { 273e4c5a685SArd Biesheuvel int idmap_idx; 274e4c5a685SArd Biesheuvel 275e4c5a685SArd Biesheuvel /* 276e4c5a685SArd Biesheuvel * Use the first entry to access the HYP mappings. It is 277e4c5a685SArd Biesheuvel * guaranteed to be free, otherwise we wouldn't use an 278e4c5a685SArd Biesheuvel * extended idmap. 279e4c5a685SArd Biesheuvel */ 280e4c5a685SArd Biesheuvel VM_BUG_ON(pgd_val(merged_hyp_pgd[0])); 281e4c5a685SArd Biesheuvel merged_hyp_pgd[0] = __pgd(__pa(hyp_pgd) | PMD_TYPE_TABLE); 282e4c5a685SArd Biesheuvel 283e4c5a685SArd Biesheuvel /* 284e4c5a685SArd Biesheuvel * Create another extended level entry that points to the boot HYP map, 285e4c5a685SArd Biesheuvel * which contains an ID mapping of the HYP init code. We essentially 286e4c5a685SArd Biesheuvel * merge the boot and runtime HYP maps by doing so, but they don't 287e4c5a685SArd Biesheuvel * overlap anyway, so this is fine. 288e4c5a685SArd Biesheuvel */ 289e4c5a685SArd Biesheuvel idmap_idx = hyp_idmap_start >> VA_BITS; 290e4c5a685SArd Biesheuvel VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx])); 291e4c5a685SArd Biesheuvel merged_hyp_pgd[idmap_idx] = __pgd(__pa(boot_hyp_pgd) | PMD_TYPE_TABLE); 292e4c5a685SArd Biesheuvel } 293e4c5a685SArd Biesheuvel 29420475f78SVladimir Murzin static inline unsigned int kvm_get_vmid_bits(void) 29520475f78SVladimir Murzin { 29620475f78SVladimir Murzin int reg = read_system_reg(SYS_ID_AA64MMFR1_EL1); 29720475f78SVladimir Murzin 29828c5dcb2SSuzuki K Poulose return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8; 29920475f78SVladimir Murzin } 30020475f78SVladimir Murzin 30137c43753SMarc Zyngier #endif /* __ASSEMBLY__ */ 30237c43753SMarc Zyngier #endif /* __ARM64_KVM_MMU_H__ */ 303