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 3282a81bffSMarc Zyngier * kernel address). We need to find out how many bits to mask. 33cedbb8b7SMarc Zyngier * 3482a81bffSMarc Zyngier * We want to build a set of page tables that cover both parts of the 3582a81bffSMarc Zyngier * idmap (the trampoline page used to initialize EL2), and our normal 3682a81bffSMarc Zyngier * runtime VA space, at the same time. 3782a81bffSMarc Zyngier * 3882a81bffSMarc Zyngier * Given that the kernel uses VA_BITS for its entire address space, 3982a81bffSMarc Zyngier * and that half of that space (VA_BITS - 1) is used for the linear 4082a81bffSMarc Zyngier * mapping, we can also limit the EL2 space to (VA_BITS - 1). 4182a81bffSMarc Zyngier * 4282a81bffSMarc Zyngier * The main question is "Within the VA_BITS space, does EL2 use the 4382a81bffSMarc Zyngier * top or the bottom half of that space to shadow the kernel's linear 4482a81bffSMarc Zyngier * mapping?". As we need to idmap the trampoline page, this is 4582a81bffSMarc Zyngier * determined by the range in which this page lives. 4682a81bffSMarc Zyngier * 4782a81bffSMarc Zyngier * If the page is in the bottom half, we have to use the top half. If 4882a81bffSMarc Zyngier * the page is in the top half, we have to use the bottom half: 4982a81bffSMarc Zyngier * 502077be67SLaura Abbott * T = __pa_symbol(__hyp_idmap_text_start) 5182a81bffSMarc Zyngier * if (T & BIT(VA_BITS - 1)) 5282a81bffSMarc Zyngier * HYP_VA_MIN = 0 //idmap in upper half 5382a81bffSMarc Zyngier * else 5482a81bffSMarc Zyngier * HYP_VA_MIN = 1 << (VA_BITS - 1) 5582a81bffSMarc Zyngier * HYP_VA_MAX = HYP_VA_MIN + (1 << (VA_BITS - 1)) - 1 5682a81bffSMarc Zyngier * 5782a81bffSMarc Zyngier * This of course assumes that the trampoline page exists within the 5882a81bffSMarc Zyngier * VA_BITS range. If it doesn't, then it means we're in the odd case 5982a81bffSMarc Zyngier * where the kernel idmap (as well as HYP) uses more levels than the 6082a81bffSMarc Zyngier * kernel runtime page tables (as seen when the kernel is configured 6182a81bffSMarc Zyngier * for 4k pages, 39bits VA, and yet memory lives just above that 6282a81bffSMarc Zyngier * limit, forcing the idmap to use 4 levels of page tables while the 6382a81bffSMarc Zyngier * kernel itself only uses 3). In this particular case, it doesn't 6482a81bffSMarc Zyngier * matter which side of VA_BITS we use, as we're guaranteed not to 6582a81bffSMarc Zyngier * conflict with anything. 6682a81bffSMarc Zyngier * 6782a81bffSMarc Zyngier * When using VHE, there are no separate hyp mappings and all KVM 6882a81bffSMarc Zyngier * functionality is already mapped as part of the main kernel 6982a81bffSMarc Zyngier * mappings, and none of this applies in that case. 7037c43753SMarc Zyngier */ 71d53d9bc6SMarc Zyngier 7237c43753SMarc Zyngier #ifdef __ASSEMBLY__ 7337c43753SMarc Zyngier 74cedbb8b7SMarc Zyngier #include <asm/alternative.h> 75cedbb8b7SMarc Zyngier 7637c43753SMarc Zyngier /* 7737c43753SMarc Zyngier * Convert a kernel VA into a HYP VA. 7837c43753SMarc Zyngier * reg: VA to be converted. 79fd81e6bfSMarc Zyngier * 802b4d1606SMarc Zyngier * The actual code generation takes place in kvm_update_va_mask, and 812b4d1606SMarc Zyngier * the instructions below are only there to reserve the space and 822b4d1606SMarc Zyngier * perform the register allocation (kvm_update_va_mask uses the 832b4d1606SMarc Zyngier * specific registers encoded in the instructions). 8437c43753SMarc Zyngier */ 8537c43753SMarc Zyngier .macro kern_hyp_va reg 862b4d1606SMarc Zyngier alternative_cb kvm_update_va_mask 87ed57cac8SMarc Zyngier and \reg, \reg, #1 /* mask with va_mask */ 88ed57cac8SMarc Zyngier ror \reg, \reg, #1 /* rotate to the first tag bit */ 89ed57cac8SMarc Zyngier add \reg, \reg, #0 /* insert the low 12 bits of the tag */ 90ed57cac8SMarc Zyngier add \reg, \reg, #0, lsl 12 /* insert the top 12 bits of the tag */ 91ed57cac8SMarc Zyngier ror \reg, \reg, #63 /* rotate back */ 922b4d1606SMarc Zyngier alternative_cb_end 9337c43753SMarc Zyngier .endm 9437c43753SMarc Zyngier 9537c43753SMarc Zyngier #else 9637c43753SMarc Zyngier 9738f791a4SChristoffer Dall #include <asm/pgalloc.h> 9802f7760eSWill Deacon #include <asm/cache.h> 9937c43753SMarc Zyngier #include <asm/cacheflush.h> 100e4c5a685SArd Biesheuvel #include <asm/mmu_context.h> 101e4c5a685SArd Biesheuvel #include <asm/pgtable.h> 10237c43753SMarc Zyngier 1032b4d1606SMarc Zyngier void kvm_update_va_mask(struct alt_instr *alt, 1042b4d1606SMarc Zyngier __le32 *origptr, __le32 *updptr, int nr_inst); 1052b4d1606SMarc Zyngier 106fd81e6bfSMarc Zyngier static inline unsigned long __kern_hyp_va(unsigned long v) 107fd81e6bfSMarc Zyngier { 108ed57cac8SMarc Zyngier asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n" 109ed57cac8SMarc Zyngier "ror %0, %0, #1\n" 110ed57cac8SMarc Zyngier "add %0, %0, #0\n" 111ed57cac8SMarc Zyngier "add %0, %0, #0, lsl 12\n" 112ed57cac8SMarc Zyngier "ror %0, %0, #63\n", 1132b4d1606SMarc Zyngier kvm_update_va_mask) 1142b4d1606SMarc Zyngier : "+r" (v)); 115fd81e6bfSMarc Zyngier return v; 116fd81e6bfSMarc Zyngier } 117fd81e6bfSMarc Zyngier 11894d0e598SMarc Zyngier #define kern_hyp_va(v) ((typeof(v))(__kern_hyp_va((unsigned long)(v)))) 11937c43753SMarc Zyngier 12037c43753SMarc Zyngier /* 12144a497abSMarc Zyngier * Obtain the PC-relative address of a kernel symbol 12244a497abSMarc Zyngier * s: symbol 12344a497abSMarc Zyngier * 12444a497abSMarc Zyngier * The goal of this macro is to return a symbol's address based on a 12544a497abSMarc Zyngier * PC-relative computation, as opposed to a loading the VA from a 12644a497abSMarc Zyngier * constant pool or something similar. This works well for HYP, as an 12744a497abSMarc Zyngier * absolute VA is guaranteed to be wrong. Only use this if trying to 12844a497abSMarc Zyngier * obtain the address of a symbol (i.e. not something you obtained by 12944a497abSMarc Zyngier * following a pointer). 13044a497abSMarc Zyngier */ 13144a497abSMarc Zyngier #define hyp_symbol_addr(s) \ 13244a497abSMarc Zyngier ({ \ 13344a497abSMarc Zyngier typeof(s) *addr; \ 13444a497abSMarc Zyngier asm("adrp %0, %1\n" \ 13544a497abSMarc Zyngier "add %0, %0, :lo12:%1\n" \ 13644a497abSMarc Zyngier : "=r" (addr) : "S" (&s)); \ 13744a497abSMarc Zyngier addr; \ 13844a497abSMarc Zyngier }) 13944a497abSMarc Zyngier 14044a497abSMarc Zyngier /* 141dbff124eSJoel Schopp * We currently only support a 40bit IPA. 14237c43753SMarc Zyngier */ 143dbff124eSJoel Schopp #define KVM_PHYS_SHIFT (40) 144e55cac5bSSuzuki K Poulose 145*13ac4bbcSSuzuki K Poulose #define kvm_phys_shift(kvm) VTCR_EL2_IPA(kvm->arch.vtcr) 146e55cac5bSSuzuki K Poulose #define kvm_phys_size(kvm) (_AC(1, ULL) << kvm_phys_shift(kvm)) 147e55cac5bSSuzuki K Poulose #define kvm_phys_mask(kvm) (kvm_phys_size(kvm) - _AC(1, ULL)) 14837c43753SMarc Zyngier 149865b30cdSSuzuki K Poulose static inline bool kvm_page_empty(void *ptr) 150865b30cdSSuzuki K Poulose { 151865b30cdSSuzuki K Poulose struct page *ptr_page = virt_to_page(ptr); 152865b30cdSSuzuki K Poulose return page_count(ptr_page) == 1; 153865b30cdSSuzuki K Poulose } 154865b30cdSSuzuki K Poulose 155c0ef6326SSuzuki K Poulose #include <asm/stage2_pgtable.h> 156c0ef6326SSuzuki K Poulose 157c8dddecdSMarc Zyngier int create_hyp_mappings(void *from, void *to, pgprot_t prot); 158807a3784SMarc Zyngier int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, 1591bb32a44SMarc Zyngier void __iomem **kaddr, 1601bb32a44SMarc Zyngier void __iomem **haddr); 161dc2e4633SMarc Zyngier int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, 162dc2e4633SMarc Zyngier void **haddr); 16337c43753SMarc Zyngier void free_hyp_pgds(void); 16437c43753SMarc Zyngier 165957db105SChristoffer Dall void stage2_unmap_vm(struct kvm *kvm); 16637c43753SMarc Zyngier int kvm_alloc_stage2_pgd(struct kvm *kvm); 16737c43753SMarc Zyngier void kvm_free_stage2_pgd(struct kvm *kvm); 16837c43753SMarc Zyngier int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, 169c40f2f8fSArd Biesheuvel phys_addr_t pa, unsigned long size, bool writable); 17037c43753SMarc Zyngier 17137c43753SMarc Zyngier int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run); 17237c43753SMarc Zyngier 17337c43753SMarc Zyngier void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu); 17437c43753SMarc Zyngier 17537c43753SMarc Zyngier phys_addr_t kvm_mmu_get_httbr(void); 17637c43753SMarc Zyngier phys_addr_t kvm_get_idmap_vector(void); 17737c43753SMarc Zyngier int kvm_mmu_init(void); 17837c43753SMarc Zyngier void kvm_clear_hyp_idmap(void); 17937c43753SMarc Zyngier 1800db9dd8aSMarc Zyngier #define kvm_mk_pmd(ptep) \ 1810db9dd8aSMarc Zyngier __pmd(__phys_to_pmd_val(__pa(ptep)) | PMD_TYPE_TABLE) 1820db9dd8aSMarc Zyngier #define kvm_mk_pud(pmdp) \ 1830db9dd8aSMarc Zyngier __pud(__phys_to_pud_val(__pa(pmdp)) | PMD_TYPE_TABLE) 1840db9dd8aSMarc Zyngier #define kvm_mk_pgd(pudp) \ 1850db9dd8aSMarc Zyngier __pgd(__phys_to_pgd_val(__pa(pudp)) | PUD_TYPE_TABLE) 1860db9dd8aSMarc Zyngier 18706485053SCatalin Marinas static inline pte_t kvm_s2pte_mkwrite(pte_t pte) 18837c43753SMarc Zyngier { 18906485053SCatalin Marinas pte_val(pte) |= PTE_S2_RDWR; 19006485053SCatalin Marinas return pte; 19137c43753SMarc Zyngier } 19237c43753SMarc Zyngier 19306485053SCatalin Marinas static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd) 194ad361f09SChristoffer Dall { 19506485053SCatalin Marinas pmd_val(pmd) |= PMD_S2_RDWR; 19606485053SCatalin Marinas return pmd; 197ad361f09SChristoffer Dall } 198ad361f09SChristoffer Dall 199d0e22b4aSMarc Zyngier static inline pte_t kvm_s2pte_mkexec(pte_t pte) 200d0e22b4aSMarc Zyngier { 201d0e22b4aSMarc Zyngier pte_val(pte) &= ~PTE_S2_XN; 202d0e22b4aSMarc Zyngier return pte; 203d0e22b4aSMarc Zyngier } 204d0e22b4aSMarc Zyngier 205d0e22b4aSMarc Zyngier static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd) 206d0e22b4aSMarc Zyngier { 207d0e22b4aSMarc Zyngier pmd_val(pmd) &= ~PMD_S2_XN; 208d0e22b4aSMarc Zyngier return pmd; 209d0e22b4aSMarc Zyngier } 210d0e22b4aSMarc Zyngier 21120a004e7SWill Deacon static inline void kvm_set_s2pte_readonly(pte_t *ptep) 2128199ed0eSMario Smarduch { 2130966253dSCatalin Marinas pteval_t old_pteval, pteval; 21406485053SCatalin Marinas 21520a004e7SWill Deacon pteval = READ_ONCE(pte_val(*ptep)); 2160966253dSCatalin Marinas do { 2170966253dSCatalin Marinas old_pteval = pteval; 2180966253dSCatalin Marinas pteval &= ~PTE_S2_RDWR; 2190966253dSCatalin Marinas pteval |= PTE_S2_RDONLY; 22020a004e7SWill Deacon pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval); 2210966253dSCatalin Marinas } while (pteval != old_pteval); 2228199ed0eSMario Smarduch } 2238199ed0eSMario Smarduch 22420a004e7SWill Deacon static inline bool kvm_s2pte_readonly(pte_t *ptep) 2258199ed0eSMario Smarduch { 22620a004e7SWill Deacon return (READ_ONCE(pte_val(*ptep)) & PTE_S2_RDWR) == PTE_S2_RDONLY; 2278199ed0eSMario Smarduch } 2288199ed0eSMario Smarduch 22920a004e7SWill Deacon static inline bool kvm_s2pte_exec(pte_t *ptep) 2307a3796d2SMarc Zyngier { 23120a004e7SWill Deacon return !(READ_ONCE(pte_val(*ptep)) & PTE_S2_XN); 2327a3796d2SMarc Zyngier } 2337a3796d2SMarc Zyngier 23420a004e7SWill Deacon static inline void kvm_set_s2pmd_readonly(pmd_t *pmdp) 2358199ed0eSMario Smarduch { 23620a004e7SWill Deacon kvm_set_s2pte_readonly((pte_t *)pmdp); 2378199ed0eSMario Smarduch } 2388199ed0eSMario Smarduch 23920a004e7SWill Deacon static inline bool kvm_s2pmd_readonly(pmd_t *pmdp) 2408199ed0eSMario Smarduch { 24120a004e7SWill Deacon return kvm_s2pte_readonly((pte_t *)pmdp); 24238f791a4SChristoffer Dall } 24338f791a4SChristoffer Dall 24420a004e7SWill Deacon static inline bool kvm_s2pmd_exec(pmd_t *pmdp) 2457a3796d2SMarc Zyngier { 24620a004e7SWill Deacon return !(READ_ONCE(pmd_val(*pmdp)) & PMD_S2_XN); 2477a3796d2SMarc Zyngier } 2487a3796d2SMarc Zyngier 24966f877faSSuzuki K Poulose #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep) 25038f791a4SChristoffer Dall 25138f791a4SChristoffer Dall #ifdef __PAGETABLE_PMD_FOLDED 25266f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) (0) 2534f853a71SChristoffer Dall #else 25466f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp) 2554f853a71SChristoffer Dall #endif 25638f791a4SChristoffer Dall 25738f791a4SChristoffer Dall #ifdef __PAGETABLE_PUD_FOLDED 25866f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) (0) 25938f791a4SChristoffer Dall #else 26066f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) kvm_page_empty(pudp) 26138f791a4SChristoffer Dall #endif 2624f853a71SChristoffer Dall 26337c43753SMarc Zyngier struct kvm; 26437c43753SMarc Zyngier 2652d58b733SMarc Zyngier #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l)) 2662d58b733SMarc Zyngier 2672d58b733SMarc Zyngier static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu) 2682d58b733SMarc Zyngier { 2698d404c4cSChristoffer Dall return (vcpu_read_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101; 2702d58b733SMarc Zyngier } 2712d58b733SMarc Zyngier 27217ab9d57SMarc Zyngier static inline void __clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size) 27337c43753SMarc Zyngier { 2740d3e4d4fSMarc Zyngier void *va = page_address(pfn_to_page(pfn)); 2750d3e4d4fSMarc Zyngier 276e48d53a9SMarc Zyngier /* 277e48d53a9SMarc Zyngier * With FWB, we ensure that the guest always accesses memory using 278e48d53a9SMarc Zyngier * cacheable attributes, and we don't have to clean to PoC when 279e48d53a9SMarc Zyngier * faulting in pages. Furthermore, FWB implies IDC, so cleaning to 280e48d53a9SMarc Zyngier * PoU is not required either in this case. 281e48d53a9SMarc Zyngier */ 282e48d53a9SMarc Zyngier if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) 283e48d53a9SMarc Zyngier return; 284e48d53a9SMarc Zyngier 2850d3e4d4fSMarc Zyngier kvm_flush_dcache_to_poc(va, size); 286a15f6939SMarc Zyngier } 2872d58b733SMarc Zyngier 28817ab9d57SMarc Zyngier static inline void __invalidate_icache_guest_page(kvm_pfn_t pfn, 289a15f6939SMarc Zyngier unsigned long size) 290a15f6939SMarc Zyngier { 29187da236eSWill Deacon if (icache_is_aliasing()) { 29237c43753SMarc Zyngier /* any kind of VIPT cache */ 29337c43753SMarc Zyngier __flush_icache_all(); 29487da236eSWill Deacon } else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) { 29587da236eSWill Deacon /* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */ 296a15f6939SMarc Zyngier void *va = page_address(pfn_to_page(pfn)); 297a15f6939SMarc Zyngier 2984fee9473SMarc Zyngier invalidate_icache_range((unsigned long)va, 29987da236eSWill Deacon (unsigned long)va + size); 30037c43753SMarc Zyngier } 30137c43753SMarc Zyngier } 30237c43753SMarc Zyngier 303363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pte(pte_t pte) 304363ef89fSMarc Zyngier { 305e48d53a9SMarc Zyngier if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) { 306363ef89fSMarc Zyngier struct page *page = pte_page(pte); 307363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE); 308363ef89fSMarc Zyngier } 309e48d53a9SMarc Zyngier } 310363ef89fSMarc Zyngier 311363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pmd(pmd_t pmd) 312363ef89fSMarc Zyngier { 313e48d53a9SMarc Zyngier if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) { 314363ef89fSMarc Zyngier struct page *page = pmd_page(pmd); 315363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE); 316363ef89fSMarc Zyngier } 317e48d53a9SMarc Zyngier } 318363ef89fSMarc Zyngier 319363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pud(pud_t pud) 320363ef89fSMarc Zyngier { 321e48d53a9SMarc Zyngier if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) { 322363ef89fSMarc Zyngier struct page *page = pud_page(pud); 323363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE); 324363ef89fSMarc Zyngier } 325e48d53a9SMarc Zyngier } 326363ef89fSMarc Zyngier 3272077be67SLaura Abbott #define kvm_virt_to_phys(x) __pa_symbol(x) 32837c43753SMarc Zyngier 3293c1e7165SMarc Zyngier void kvm_set_way_flush(struct kvm_vcpu *vcpu); 3303c1e7165SMarc Zyngier void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled); 3319d218a1fSMarc Zyngier 332e4c5a685SArd Biesheuvel static inline bool __kvm_cpu_uses_extended_idmap(void) 333e4c5a685SArd Biesheuvel { 334fa2a8445SKristina Martsenko return __cpu_uses_extended_idmap_level(); 335fa2a8445SKristina Martsenko } 336fa2a8445SKristina Martsenko 337fa2a8445SKristina Martsenko static inline unsigned long __kvm_idmap_ptrs_per_pgd(void) 338fa2a8445SKristina Martsenko { 339fa2a8445SKristina Martsenko return idmap_ptrs_per_pgd; 340e4c5a685SArd Biesheuvel } 341e4c5a685SArd Biesheuvel 34219338304SKristina Martsenko /* 34319338304SKristina Martsenko * Can't use pgd_populate here, because the extended idmap adds an extra level 34419338304SKristina Martsenko * above CONFIG_PGTABLE_LEVELS (which is 2 or 3 if we're using the extended 34519338304SKristina Martsenko * idmap), and pgd_populate is only available if CONFIG_PGTABLE_LEVELS = 4. 34619338304SKristina Martsenko */ 347e4c5a685SArd Biesheuvel static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd, 348e4c5a685SArd Biesheuvel pgd_t *hyp_pgd, 349e4c5a685SArd Biesheuvel pgd_t *merged_hyp_pgd, 350e4c5a685SArd Biesheuvel unsigned long hyp_idmap_start) 351e4c5a685SArd Biesheuvel { 352e4c5a685SArd Biesheuvel int idmap_idx; 35375387b92SKristina Martsenko u64 pgd_addr; 354e4c5a685SArd Biesheuvel 355e4c5a685SArd Biesheuvel /* 356e4c5a685SArd Biesheuvel * Use the first entry to access the HYP mappings. It is 357e4c5a685SArd Biesheuvel * guaranteed to be free, otherwise we wouldn't use an 358e4c5a685SArd Biesheuvel * extended idmap. 359e4c5a685SArd Biesheuvel */ 360e4c5a685SArd Biesheuvel VM_BUG_ON(pgd_val(merged_hyp_pgd[0])); 36175387b92SKristina Martsenko pgd_addr = __phys_to_pgd_val(__pa(hyp_pgd)); 36275387b92SKristina Martsenko merged_hyp_pgd[0] = __pgd(pgd_addr | PMD_TYPE_TABLE); 363e4c5a685SArd Biesheuvel 364e4c5a685SArd Biesheuvel /* 365e4c5a685SArd Biesheuvel * Create another extended level entry that points to the boot HYP map, 366e4c5a685SArd Biesheuvel * which contains an ID mapping of the HYP init code. We essentially 367e4c5a685SArd Biesheuvel * merge the boot and runtime HYP maps by doing so, but they don't 368e4c5a685SArd Biesheuvel * overlap anyway, so this is fine. 369e4c5a685SArd Biesheuvel */ 370e4c5a685SArd Biesheuvel idmap_idx = hyp_idmap_start >> VA_BITS; 371e4c5a685SArd Biesheuvel VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx])); 37275387b92SKristina Martsenko pgd_addr = __phys_to_pgd_val(__pa(boot_hyp_pgd)); 37375387b92SKristina Martsenko merged_hyp_pgd[idmap_idx] = __pgd(pgd_addr | PMD_TYPE_TABLE); 374e4c5a685SArd Biesheuvel } 375e4c5a685SArd Biesheuvel 37620475f78SVladimir Murzin static inline unsigned int kvm_get_vmid_bits(void) 37720475f78SVladimir Murzin { 37846823dd1SDave Martin int reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 37920475f78SVladimir Murzin 38028c5dcb2SSuzuki K Poulose return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8; 38120475f78SVladimir Murzin } 38220475f78SVladimir Murzin 383bf308242SAndre Przywara /* 384bf308242SAndre Przywara * We are not in the kvm->srcu critical section most of the time, so we take 385bf308242SAndre Przywara * the SRCU read lock here. Since we copy the data from the user page, we 386bf308242SAndre Przywara * can immediately drop the lock again. 387bf308242SAndre Przywara */ 388bf308242SAndre Przywara static inline int kvm_read_guest_lock(struct kvm *kvm, 389bf308242SAndre Przywara gpa_t gpa, void *data, unsigned long len) 390bf308242SAndre Przywara { 391bf308242SAndre Przywara int srcu_idx = srcu_read_lock(&kvm->srcu); 392bf308242SAndre Przywara int ret = kvm_read_guest(kvm, gpa, data, len); 393bf308242SAndre Przywara 394bf308242SAndre Przywara srcu_read_unlock(&kvm->srcu, srcu_idx); 395bf308242SAndre Przywara 396bf308242SAndre Przywara return ret; 397bf308242SAndre Przywara } 398bf308242SAndre Przywara 399dee39247SMarc Zyngier #ifdef CONFIG_KVM_INDIRECT_VECTORS 400dee39247SMarc Zyngier /* 401dee39247SMarc Zyngier * EL2 vectors can be mapped and rerouted in a number of ways, 402dee39247SMarc Zyngier * depending on the kernel configuration and CPU present: 403dee39247SMarc Zyngier * 404dee39247SMarc Zyngier * - If the CPU has the ARM64_HARDEN_BRANCH_PREDICTOR cap, the 405dee39247SMarc Zyngier * hardening sequence is placed in one of the vector slots, which is 406dee39247SMarc Zyngier * executed before jumping to the real vectors. 407dee39247SMarc Zyngier * 408dee39247SMarc Zyngier * - If the CPU has both the ARM64_HARDEN_EL2_VECTORS cap and the 409dee39247SMarc Zyngier * ARM64_HARDEN_BRANCH_PREDICTOR cap, the slot containing the 410dee39247SMarc Zyngier * hardening sequence is mapped next to the idmap page, and executed 411dee39247SMarc Zyngier * before jumping to the real vectors. 412dee39247SMarc Zyngier * 413dee39247SMarc Zyngier * - If the CPU only has the ARM64_HARDEN_EL2_VECTORS cap, then an 414dee39247SMarc Zyngier * empty slot is selected, mapped next to the idmap page, and 415dee39247SMarc Zyngier * executed before jumping to the real vectors. 416dee39247SMarc Zyngier * 417dee39247SMarc Zyngier * Note that ARM64_HARDEN_EL2_VECTORS is somewhat incompatible with 418dee39247SMarc Zyngier * VHE, as we don't have hypervisor-specific mappings. If the system 419dee39247SMarc Zyngier * is VHE and yet selects this capability, it will be ignored. 420dee39247SMarc Zyngier */ 4216840bdd7SMarc Zyngier #include <asm/mmu.h> 4226840bdd7SMarc Zyngier 423dee39247SMarc Zyngier extern void *__kvm_bp_vect_base; 424dee39247SMarc Zyngier extern int __kvm_harden_el2_vector_slot; 425dee39247SMarc Zyngier 4266840bdd7SMarc Zyngier static inline void *kvm_get_hyp_vector(void) 4276840bdd7SMarc Zyngier { 4286840bdd7SMarc Zyngier struct bp_hardening_data *data = arm64_get_bp_hardening_data(); 429dee39247SMarc Zyngier void *vect = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 430dee39247SMarc Zyngier int slot = -1; 4316840bdd7SMarc Zyngier 432dee39247SMarc Zyngier if (cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR) && data->fn) { 433dee39247SMarc Zyngier vect = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs_start)); 434dee39247SMarc Zyngier slot = data->hyp_vectors_slot; 4356840bdd7SMarc Zyngier } 4366840bdd7SMarc Zyngier 437dee39247SMarc Zyngier if (this_cpu_has_cap(ARM64_HARDEN_EL2_VECTORS) && !has_vhe()) { 438dee39247SMarc Zyngier vect = __kvm_bp_vect_base; 439dee39247SMarc Zyngier if (slot == -1) 440dee39247SMarc Zyngier slot = __kvm_harden_el2_vector_slot; 441dee39247SMarc Zyngier } 442dee39247SMarc Zyngier 443dee39247SMarc Zyngier if (slot != -1) 444dee39247SMarc Zyngier vect += slot * SZ_2K; 445dee39247SMarc Zyngier 4466840bdd7SMarc Zyngier return vect; 4476840bdd7SMarc Zyngier } 4486840bdd7SMarc Zyngier 449dee39247SMarc Zyngier /* This is only called on a !VHE system */ 4506840bdd7SMarc Zyngier static inline int kvm_map_vectors(void) 4516840bdd7SMarc Zyngier { 452dee39247SMarc Zyngier /* 453dee39247SMarc Zyngier * HBP = ARM64_HARDEN_BRANCH_PREDICTOR 454dee39247SMarc Zyngier * HEL2 = ARM64_HARDEN_EL2_VECTORS 455dee39247SMarc Zyngier * 456dee39247SMarc Zyngier * !HBP + !HEL2 -> use direct vectors 457dee39247SMarc Zyngier * HBP + !HEL2 -> use hardened vectors in place 458dee39247SMarc Zyngier * !HBP + HEL2 -> allocate one vector slot and use exec mapping 459dee39247SMarc Zyngier * HBP + HEL2 -> use hardened vertors and use exec mapping 460dee39247SMarc Zyngier */ 461dee39247SMarc Zyngier if (cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR)) { 462dee39247SMarc Zyngier __kvm_bp_vect_base = kvm_ksym_ref(__bp_harden_hyp_vecs_start); 463dee39247SMarc Zyngier __kvm_bp_vect_base = kern_hyp_va(__kvm_bp_vect_base); 4646840bdd7SMarc Zyngier } 4656840bdd7SMarc Zyngier 466dee39247SMarc Zyngier if (cpus_have_const_cap(ARM64_HARDEN_EL2_VECTORS)) { 467dee39247SMarc Zyngier phys_addr_t vect_pa = __pa_symbol(__bp_harden_hyp_vecs_start); 468dee39247SMarc Zyngier unsigned long size = (__bp_harden_hyp_vecs_end - 469dee39247SMarc Zyngier __bp_harden_hyp_vecs_start); 470dee39247SMarc Zyngier 471dee39247SMarc Zyngier /* 472dee39247SMarc Zyngier * Always allocate a spare vector slot, as we don't 473dee39247SMarc Zyngier * know yet which CPUs have a BP hardening slot that 474dee39247SMarc Zyngier * we can reuse. 475dee39247SMarc Zyngier */ 476dee39247SMarc Zyngier __kvm_harden_el2_vector_slot = atomic_inc_return(&arm64_el2_vector_last_slot); 477dee39247SMarc Zyngier BUG_ON(__kvm_harden_el2_vector_slot >= BP_HARDEN_EL2_SLOTS); 478dee39247SMarc Zyngier return create_hyp_exec_mappings(vect_pa, size, 479dee39247SMarc Zyngier &__kvm_bp_vect_base); 480dee39247SMarc Zyngier } 481dee39247SMarc Zyngier 482dee39247SMarc Zyngier return 0; 483dee39247SMarc Zyngier } 4846840bdd7SMarc Zyngier #else 4856840bdd7SMarc Zyngier static inline void *kvm_get_hyp_vector(void) 4866840bdd7SMarc Zyngier { 4873c5e8123SMarc Zyngier return kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 4886840bdd7SMarc Zyngier } 4896840bdd7SMarc Zyngier 4906840bdd7SMarc Zyngier static inline int kvm_map_vectors(void) 4916840bdd7SMarc Zyngier { 4926840bdd7SMarc Zyngier return 0; 4936840bdd7SMarc Zyngier } 4946840bdd7SMarc Zyngier #endif 4956840bdd7SMarc Zyngier 49655e3748eSMarc Zyngier #ifdef CONFIG_ARM64_SSBD 49755e3748eSMarc Zyngier DECLARE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required); 49855e3748eSMarc Zyngier 49955e3748eSMarc Zyngier static inline int hyp_map_aux_data(void) 50055e3748eSMarc Zyngier { 50155e3748eSMarc Zyngier int cpu, err; 50255e3748eSMarc Zyngier 50355e3748eSMarc Zyngier for_each_possible_cpu(cpu) { 50455e3748eSMarc Zyngier u64 *ptr; 50555e3748eSMarc Zyngier 50655e3748eSMarc Zyngier ptr = per_cpu_ptr(&arm64_ssbd_callback_required, cpu); 50755e3748eSMarc Zyngier err = create_hyp_mappings(ptr, ptr + 1, PAGE_HYP); 50855e3748eSMarc Zyngier if (err) 50955e3748eSMarc Zyngier return err; 51055e3748eSMarc Zyngier } 51155e3748eSMarc Zyngier return 0; 51255e3748eSMarc Zyngier } 51355e3748eSMarc Zyngier #else 51455e3748eSMarc Zyngier static inline int hyp_map_aux_data(void) 51555e3748eSMarc Zyngier { 51655e3748eSMarc Zyngier return 0; 51755e3748eSMarc Zyngier } 51855e3748eSMarc Zyngier #endif 51955e3748eSMarc Zyngier 520529c4b05SKristina Martsenko #define kvm_phys_to_vttbr(addr) phys_to_ttbr(addr) 521529c4b05SKristina Martsenko 52259558330SSuzuki K Poulose /* 52359558330SSuzuki K Poulose * Get the magic number 'x' for VTTBR:BADDR of this KVM instance. 52459558330SSuzuki K Poulose * With v8.2 LVA extensions, 'x' should be a minimum of 6 with 52559558330SSuzuki K Poulose * 52bit IPS. 52659558330SSuzuki K Poulose */ 52759558330SSuzuki K Poulose static inline int arm64_vttbr_x(u32 ipa_shift, u32 levels) 52859558330SSuzuki K Poulose { 52959558330SSuzuki K Poulose int x = ARM64_VTTBR_X(ipa_shift, levels); 53059558330SSuzuki K Poulose 53159558330SSuzuki K Poulose return (IS_ENABLED(CONFIG_ARM64_PA_BITS_52) && x < 6) ? 6 : x; 53259558330SSuzuki K Poulose } 53359558330SSuzuki K Poulose 53459558330SSuzuki K Poulose static inline u64 vttbr_baddr_mask(u32 ipa_shift, u32 levels) 53559558330SSuzuki K Poulose { 53659558330SSuzuki K Poulose unsigned int x = arm64_vttbr_x(ipa_shift, levels); 53759558330SSuzuki K Poulose 53859558330SSuzuki K Poulose return GENMASK_ULL(PHYS_MASK_SHIFT - 1, x); 53959558330SSuzuki K Poulose } 54059558330SSuzuki K Poulose 54159558330SSuzuki K Poulose static inline u64 kvm_vttbr_baddr_mask(struct kvm *kvm) 54259558330SSuzuki K Poulose { 54359558330SSuzuki K Poulose return vttbr_baddr_mask(kvm_phys_shift(kvm), kvm_stage2_levels(kvm)); 54459558330SSuzuki K Poulose } 54559558330SSuzuki K Poulose 54637c43753SMarc Zyngier #endif /* __ASSEMBLY__ */ 54737c43753SMarc Zyngier #endif /* __ARM64_KVM_MMU_H__ */ 548