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 #include <asm/cpufeature.h> 76cedbb8b7SMarc Zyngier 7737c43753SMarc Zyngier /* 7837c43753SMarc Zyngier * Convert a kernel VA into a HYP VA. 7937c43753SMarc Zyngier * reg: VA to be converted. 80fd81e6bfSMarc Zyngier * 812b4d1606SMarc Zyngier * The actual code generation takes place in kvm_update_va_mask, and 822b4d1606SMarc Zyngier * the instructions below are only there to reserve the space and 832b4d1606SMarc Zyngier * perform the register allocation (kvm_update_va_mask uses the 842b4d1606SMarc Zyngier * specific registers encoded in the instructions). 8537c43753SMarc Zyngier */ 8637c43753SMarc Zyngier .macro kern_hyp_va reg 872b4d1606SMarc Zyngier alternative_cb kvm_update_va_mask 88ed57cac8SMarc Zyngier and \reg, \reg, #1 /* mask with va_mask */ 89ed57cac8SMarc Zyngier ror \reg, \reg, #1 /* rotate to the first tag bit */ 90ed57cac8SMarc Zyngier add \reg, \reg, #0 /* insert the low 12 bits of the tag */ 91ed57cac8SMarc Zyngier add \reg, \reg, #0, lsl 12 /* insert the top 12 bits of the tag */ 92ed57cac8SMarc Zyngier ror \reg, \reg, #63 /* rotate back */ 932b4d1606SMarc Zyngier alternative_cb_end 9437c43753SMarc Zyngier .endm 9537c43753SMarc Zyngier 9637c43753SMarc Zyngier #else 9737c43753SMarc Zyngier 9838f791a4SChristoffer Dall #include <asm/pgalloc.h> 9902f7760eSWill Deacon #include <asm/cache.h> 10037c43753SMarc Zyngier #include <asm/cacheflush.h> 101e4c5a685SArd Biesheuvel #include <asm/mmu_context.h> 102e4c5a685SArd Biesheuvel #include <asm/pgtable.h> 10337c43753SMarc Zyngier 1042b4d1606SMarc Zyngier void kvm_update_va_mask(struct alt_instr *alt, 1052b4d1606SMarc Zyngier __le32 *origptr, __le32 *updptr, int nr_inst); 1062b4d1606SMarc Zyngier 107fd81e6bfSMarc Zyngier static inline unsigned long __kern_hyp_va(unsigned long v) 108fd81e6bfSMarc Zyngier { 109ed57cac8SMarc Zyngier asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n" 110ed57cac8SMarc Zyngier "ror %0, %0, #1\n" 111ed57cac8SMarc Zyngier "add %0, %0, #0\n" 112ed57cac8SMarc Zyngier "add %0, %0, #0, lsl 12\n" 113ed57cac8SMarc Zyngier "ror %0, %0, #63\n", 1142b4d1606SMarc Zyngier kvm_update_va_mask) 1152b4d1606SMarc Zyngier : "+r" (v)); 116fd81e6bfSMarc Zyngier return v; 117fd81e6bfSMarc Zyngier } 118fd81e6bfSMarc Zyngier 11994d0e598SMarc Zyngier #define kern_hyp_va(v) ((typeof(v))(__kern_hyp_va((unsigned long)(v)))) 12037c43753SMarc Zyngier 12137c43753SMarc Zyngier /* 12244a497abSMarc Zyngier * Obtain the PC-relative address of a kernel symbol 12344a497abSMarc Zyngier * s: symbol 12444a497abSMarc Zyngier * 12544a497abSMarc Zyngier * The goal of this macro is to return a symbol's address based on a 12644a497abSMarc Zyngier * PC-relative computation, as opposed to a loading the VA from a 12744a497abSMarc Zyngier * constant pool or something similar. This works well for HYP, as an 12844a497abSMarc Zyngier * absolute VA is guaranteed to be wrong. Only use this if trying to 12944a497abSMarc Zyngier * obtain the address of a symbol (i.e. not something you obtained by 13044a497abSMarc Zyngier * following a pointer). 13144a497abSMarc Zyngier */ 13244a497abSMarc Zyngier #define hyp_symbol_addr(s) \ 13344a497abSMarc Zyngier ({ \ 13444a497abSMarc Zyngier typeof(s) *addr; \ 13544a497abSMarc Zyngier asm("adrp %0, %1\n" \ 13644a497abSMarc Zyngier "add %0, %0, :lo12:%1\n" \ 13744a497abSMarc Zyngier : "=r" (addr) : "S" (&s)); \ 13844a497abSMarc Zyngier addr; \ 13944a497abSMarc Zyngier }) 14044a497abSMarc Zyngier 14144a497abSMarc Zyngier /* 142dbff124eSJoel Schopp * We currently only support a 40bit IPA. 14337c43753SMarc Zyngier */ 144dbff124eSJoel Schopp #define KVM_PHYS_SHIFT (40) 14537c43753SMarc Zyngier #define KVM_PHYS_SIZE (1UL << KVM_PHYS_SHIFT) 14637c43753SMarc Zyngier #define KVM_PHYS_MASK (KVM_PHYS_SIZE - 1UL) 14737c43753SMarc Zyngier 148c0ef6326SSuzuki K Poulose #include <asm/stage2_pgtable.h> 149c0ef6326SSuzuki K Poulose 150c8dddecdSMarc Zyngier int create_hyp_mappings(void *from, void *to, pgprot_t prot); 151807a3784SMarc Zyngier int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, 1521bb32a44SMarc Zyngier void __iomem **kaddr, 1531bb32a44SMarc Zyngier void __iomem **haddr); 154dc2e4633SMarc Zyngier int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, 155dc2e4633SMarc Zyngier void **haddr); 15637c43753SMarc Zyngier void free_hyp_pgds(void); 15737c43753SMarc Zyngier 158957db105SChristoffer Dall void stage2_unmap_vm(struct kvm *kvm); 15937c43753SMarc Zyngier int kvm_alloc_stage2_pgd(struct kvm *kvm); 16037c43753SMarc Zyngier void kvm_free_stage2_pgd(struct kvm *kvm); 16137c43753SMarc Zyngier int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, 162c40f2f8fSArd Biesheuvel phys_addr_t pa, unsigned long size, bool writable); 16337c43753SMarc Zyngier 16437c43753SMarc Zyngier int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run); 16537c43753SMarc Zyngier 16637c43753SMarc Zyngier void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu); 16737c43753SMarc Zyngier 16837c43753SMarc Zyngier phys_addr_t kvm_mmu_get_httbr(void); 16937c43753SMarc Zyngier phys_addr_t kvm_get_idmap_vector(void); 17037c43753SMarc Zyngier int kvm_mmu_init(void); 17137c43753SMarc Zyngier void kvm_clear_hyp_idmap(void); 17237c43753SMarc Zyngier 17337c43753SMarc Zyngier #define kvm_set_pte(ptep, pte) set_pte(ptep, pte) 174ad361f09SChristoffer Dall #define kvm_set_pmd(pmdp, pmd) set_pmd(pmdp, pmd) 17537c43753SMarc Zyngier 17606485053SCatalin Marinas static inline pte_t kvm_s2pte_mkwrite(pte_t pte) 17737c43753SMarc Zyngier { 17806485053SCatalin Marinas pte_val(pte) |= PTE_S2_RDWR; 17906485053SCatalin Marinas return pte; 18037c43753SMarc Zyngier } 18137c43753SMarc Zyngier 18206485053SCatalin Marinas static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd) 183ad361f09SChristoffer Dall { 18406485053SCatalin Marinas pmd_val(pmd) |= PMD_S2_RDWR; 18506485053SCatalin Marinas return pmd; 186ad361f09SChristoffer Dall } 187ad361f09SChristoffer Dall 188d0e22b4aSMarc Zyngier static inline pte_t kvm_s2pte_mkexec(pte_t pte) 189d0e22b4aSMarc Zyngier { 190d0e22b4aSMarc Zyngier pte_val(pte) &= ~PTE_S2_XN; 191d0e22b4aSMarc Zyngier return pte; 192d0e22b4aSMarc Zyngier } 193d0e22b4aSMarc Zyngier 194d0e22b4aSMarc Zyngier static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd) 195d0e22b4aSMarc Zyngier { 196d0e22b4aSMarc Zyngier pmd_val(pmd) &= ~PMD_S2_XN; 197d0e22b4aSMarc Zyngier return pmd; 198d0e22b4aSMarc Zyngier } 199d0e22b4aSMarc Zyngier 20020a004e7SWill Deacon static inline void kvm_set_s2pte_readonly(pte_t *ptep) 2018199ed0eSMario Smarduch { 2020966253dSCatalin Marinas pteval_t old_pteval, pteval; 20306485053SCatalin Marinas 20420a004e7SWill Deacon pteval = READ_ONCE(pte_val(*ptep)); 2050966253dSCatalin Marinas do { 2060966253dSCatalin Marinas old_pteval = pteval; 2070966253dSCatalin Marinas pteval &= ~PTE_S2_RDWR; 2080966253dSCatalin Marinas pteval |= PTE_S2_RDONLY; 20920a004e7SWill Deacon pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval); 2100966253dSCatalin Marinas } while (pteval != old_pteval); 2118199ed0eSMario Smarduch } 2128199ed0eSMario Smarduch 21320a004e7SWill Deacon static inline bool kvm_s2pte_readonly(pte_t *ptep) 2148199ed0eSMario Smarduch { 21520a004e7SWill Deacon return (READ_ONCE(pte_val(*ptep)) & PTE_S2_RDWR) == PTE_S2_RDONLY; 2168199ed0eSMario Smarduch } 2178199ed0eSMario Smarduch 21820a004e7SWill Deacon static inline bool kvm_s2pte_exec(pte_t *ptep) 2197a3796d2SMarc Zyngier { 22020a004e7SWill Deacon return !(READ_ONCE(pte_val(*ptep)) & PTE_S2_XN); 2217a3796d2SMarc Zyngier } 2227a3796d2SMarc Zyngier 22320a004e7SWill Deacon static inline void kvm_set_s2pmd_readonly(pmd_t *pmdp) 2248199ed0eSMario Smarduch { 22520a004e7SWill Deacon kvm_set_s2pte_readonly((pte_t *)pmdp); 2268199ed0eSMario Smarduch } 2278199ed0eSMario Smarduch 22820a004e7SWill Deacon static inline bool kvm_s2pmd_readonly(pmd_t *pmdp) 2298199ed0eSMario Smarduch { 23020a004e7SWill Deacon return kvm_s2pte_readonly((pte_t *)pmdp); 23138f791a4SChristoffer Dall } 23238f791a4SChristoffer Dall 23320a004e7SWill Deacon static inline bool kvm_s2pmd_exec(pmd_t *pmdp) 2347a3796d2SMarc Zyngier { 23520a004e7SWill Deacon return !(READ_ONCE(pmd_val(*pmdp)) & PMD_S2_XN); 2367a3796d2SMarc Zyngier } 2377a3796d2SMarc Zyngier 2384f853a71SChristoffer Dall static inline bool kvm_page_empty(void *ptr) 2394f853a71SChristoffer Dall { 2404f853a71SChristoffer Dall struct page *ptr_page = virt_to_page(ptr); 2414f853a71SChristoffer Dall return page_count(ptr_page) == 1; 2424f853a71SChristoffer Dall } 2434f853a71SChristoffer Dall 24466f877faSSuzuki K Poulose #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep) 24538f791a4SChristoffer Dall 24638f791a4SChristoffer Dall #ifdef __PAGETABLE_PMD_FOLDED 24766f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) (0) 2484f853a71SChristoffer Dall #else 24966f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp) 2504f853a71SChristoffer Dall #endif 25138f791a4SChristoffer Dall 25238f791a4SChristoffer Dall #ifdef __PAGETABLE_PUD_FOLDED 25366f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) (0) 25438f791a4SChristoffer Dall #else 25566f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) kvm_page_empty(pudp) 25638f791a4SChristoffer Dall #endif 2574f853a71SChristoffer Dall 25837c43753SMarc Zyngier struct kvm; 25937c43753SMarc Zyngier 2602d58b733SMarc Zyngier #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l)) 2612d58b733SMarc Zyngier 2622d58b733SMarc Zyngier static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu) 2632d58b733SMarc Zyngier { 2648d404c4cSChristoffer Dall return (vcpu_read_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101; 2652d58b733SMarc Zyngier } 2662d58b733SMarc Zyngier 26717ab9d57SMarc Zyngier static inline void __clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size) 26837c43753SMarc Zyngier { 2690d3e4d4fSMarc Zyngier void *va = page_address(pfn_to_page(pfn)); 2700d3e4d4fSMarc Zyngier 2710d3e4d4fSMarc Zyngier kvm_flush_dcache_to_poc(va, size); 272a15f6939SMarc Zyngier } 2732d58b733SMarc Zyngier 27417ab9d57SMarc Zyngier static inline void __invalidate_icache_guest_page(kvm_pfn_t pfn, 275a15f6939SMarc Zyngier unsigned long size) 276a15f6939SMarc Zyngier { 27787da236eSWill Deacon if (icache_is_aliasing()) { 27837c43753SMarc Zyngier /* any kind of VIPT cache */ 27937c43753SMarc Zyngier __flush_icache_all(); 28087da236eSWill Deacon } else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) { 28187da236eSWill Deacon /* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */ 282a15f6939SMarc Zyngier void *va = page_address(pfn_to_page(pfn)); 283a15f6939SMarc Zyngier 2844fee9473SMarc Zyngier invalidate_icache_range((unsigned long)va, 28587da236eSWill Deacon (unsigned long)va + size); 28637c43753SMarc Zyngier } 28737c43753SMarc Zyngier } 28837c43753SMarc Zyngier 289363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pte(pte_t pte) 290363ef89fSMarc Zyngier { 291363ef89fSMarc Zyngier struct page *page = pte_page(pte); 292363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE); 293363ef89fSMarc Zyngier } 294363ef89fSMarc Zyngier 295363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pmd(pmd_t pmd) 296363ef89fSMarc Zyngier { 297363ef89fSMarc Zyngier struct page *page = pmd_page(pmd); 298363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE); 299363ef89fSMarc Zyngier } 300363ef89fSMarc Zyngier 301363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pud(pud_t pud) 302363ef89fSMarc Zyngier { 303363ef89fSMarc Zyngier struct page *page = pud_page(pud); 304363ef89fSMarc Zyngier kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE); 305363ef89fSMarc Zyngier } 306363ef89fSMarc Zyngier 3072077be67SLaura Abbott #define kvm_virt_to_phys(x) __pa_symbol(x) 30837c43753SMarc Zyngier 3093c1e7165SMarc Zyngier void kvm_set_way_flush(struct kvm_vcpu *vcpu); 3103c1e7165SMarc Zyngier void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled); 3119d218a1fSMarc Zyngier 312e4c5a685SArd Biesheuvel static inline bool __kvm_cpu_uses_extended_idmap(void) 313e4c5a685SArd Biesheuvel { 314fa2a8445SKristina Martsenko return __cpu_uses_extended_idmap_level(); 315fa2a8445SKristina Martsenko } 316fa2a8445SKristina Martsenko 317fa2a8445SKristina Martsenko static inline unsigned long __kvm_idmap_ptrs_per_pgd(void) 318fa2a8445SKristina Martsenko { 319fa2a8445SKristina Martsenko return idmap_ptrs_per_pgd; 320e4c5a685SArd Biesheuvel } 321e4c5a685SArd Biesheuvel 32219338304SKristina Martsenko /* 32319338304SKristina Martsenko * Can't use pgd_populate here, because the extended idmap adds an extra level 32419338304SKristina Martsenko * above CONFIG_PGTABLE_LEVELS (which is 2 or 3 if we're using the extended 32519338304SKristina Martsenko * idmap), and pgd_populate is only available if CONFIG_PGTABLE_LEVELS = 4. 32619338304SKristina Martsenko */ 327e4c5a685SArd Biesheuvel static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd, 328e4c5a685SArd Biesheuvel pgd_t *hyp_pgd, 329e4c5a685SArd Biesheuvel pgd_t *merged_hyp_pgd, 330e4c5a685SArd Biesheuvel unsigned long hyp_idmap_start) 331e4c5a685SArd Biesheuvel { 332e4c5a685SArd Biesheuvel int idmap_idx; 33375387b92SKristina Martsenko u64 pgd_addr; 334e4c5a685SArd Biesheuvel 335e4c5a685SArd Biesheuvel /* 336e4c5a685SArd Biesheuvel * Use the first entry to access the HYP mappings. It is 337e4c5a685SArd Biesheuvel * guaranteed to be free, otherwise we wouldn't use an 338e4c5a685SArd Biesheuvel * extended idmap. 339e4c5a685SArd Biesheuvel */ 340e4c5a685SArd Biesheuvel VM_BUG_ON(pgd_val(merged_hyp_pgd[0])); 34175387b92SKristina Martsenko pgd_addr = __phys_to_pgd_val(__pa(hyp_pgd)); 34275387b92SKristina Martsenko merged_hyp_pgd[0] = __pgd(pgd_addr | PMD_TYPE_TABLE); 343e4c5a685SArd Biesheuvel 344e4c5a685SArd Biesheuvel /* 345e4c5a685SArd Biesheuvel * Create another extended level entry that points to the boot HYP map, 346e4c5a685SArd Biesheuvel * which contains an ID mapping of the HYP init code. We essentially 347e4c5a685SArd Biesheuvel * merge the boot and runtime HYP maps by doing so, but they don't 348e4c5a685SArd Biesheuvel * overlap anyway, so this is fine. 349e4c5a685SArd Biesheuvel */ 350e4c5a685SArd Biesheuvel idmap_idx = hyp_idmap_start >> VA_BITS; 351e4c5a685SArd Biesheuvel VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx])); 35275387b92SKristina Martsenko pgd_addr = __phys_to_pgd_val(__pa(boot_hyp_pgd)); 35375387b92SKristina Martsenko merged_hyp_pgd[idmap_idx] = __pgd(pgd_addr | PMD_TYPE_TABLE); 354e4c5a685SArd Biesheuvel } 355e4c5a685SArd Biesheuvel 35620475f78SVladimir Murzin static inline unsigned int kvm_get_vmid_bits(void) 35720475f78SVladimir Murzin { 35846823dd1SDave Martin int reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 35920475f78SVladimir Murzin 36028c5dcb2SSuzuki K Poulose return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8; 36120475f78SVladimir Murzin } 36220475f78SVladimir Murzin 363*bf308242SAndre Przywara /* 364*bf308242SAndre Przywara * We are not in the kvm->srcu critical section most of the time, so we take 365*bf308242SAndre Przywara * the SRCU read lock here. Since we copy the data from the user page, we 366*bf308242SAndre Przywara * can immediately drop the lock again. 367*bf308242SAndre Przywara */ 368*bf308242SAndre Przywara static inline int kvm_read_guest_lock(struct kvm *kvm, 369*bf308242SAndre Przywara gpa_t gpa, void *data, unsigned long len) 370*bf308242SAndre Przywara { 371*bf308242SAndre Przywara int srcu_idx = srcu_read_lock(&kvm->srcu); 372*bf308242SAndre Przywara int ret = kvm_read_guest(kvm, gpa, data, len); 373*bf308242SAndre Przywara 374*bf308242SAndre Przywara srcu_read_unlock(&kvm->srcu, srcu_idx); 375*bf308242SAndre Przywara 376*bf308242SAndre Przywara return ret; 377*bf308242SAndre Przywara } 378*bf308242SAndre Przywara 379dee39247SMarc Zyngier #ifdef CONFIG_KVM_INDIRECT_VECTORS 380dee39247SMarc Zyngier /* 381dee39247SMarc Zyngier * EL2 vectors can be mapped and rerouted in a number of ways, 382dee39247SMarc Zyngier * depending on the kernel configuration and CPU present: 383dee39247SMarc Zyngier * 384dee39247SMarc Zyngier * - If the CPU has the ARM64_HARDEN_BRANCH_PREDICTOR cap, the 385dee39247SMarc Zyngier * hardening sequence is placed in one of the vector slots, which is 386dee39247SMarc Zyngier * executed before jumping to the real vectors. 387dee39247SMarc Zyngier * 388dee39247SMarc Zyngier * - If the CPU has both the ARM64_HARDEN_EL2_VECTORS cap and the 389dee39247SMarc Zyngier * ARM64_HARDEN_BRANCH_PREDICTOR cap, the slot containing the 390dee39247SMarc Zyngier * hardening sequence is mapped next to the idmap page, and executed 391dee39247SMarc Zyngier * before jumping to the real vectors. 392dee39247SMarc Zyngier * 393dee39247SMarc Zyngier * - If the CPU only has the ARM64_HARDEN_EL2_VECTORS cap, then an 394dee39247SMarc Zyngier * empty slot is selected, mapped next to the idmap page, and 395dee39247SMarc Zyngier * executed before jumping to the real vectors. 396dee39247SMarc Zyngier * 397dee39247SMarc Zyngier * Note that ARM64_HARDEN_EL2_VECTORS is somewhat incompatible with 398dee39247SMarc Zyngier * VHE, as we don't have hypervisor-specific mappings. If the system 399dee39247SMarc Zyngier * is VHE and yet selects this capability, it will be ignored. 400dee39247SMarc Zyngier */ 4016840bdd7SMarc Zyngier #include <asm/mmu.h> 4026840bdd7SMarc Zyngier 403dee39247SMarc Zyngier extern void *__kvm_bp_vect_base; 404dee39247SMarc Zyngier extern int __kvm_harden_el2_vector_slot; 405dee39247SMarc Zyngier 4066840bdd7SMarc Zyngier static inline void *kvm_get_hyp_vector(void) 4076840bdd7SMarc Zyngier { 4086840bdd7SMarc Zyngier struct bp_hardening_data *data = arm64_get_bp_hardening_data(); 409dee39247SMarc Zyngier void *vect = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 410dee39247SMarc Zyngier int slot = -1; 4116840bdd7SMarc Zyngier 412dee39247SMarc Zyngier if (cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR) && data->fn) { 413dee39247SMarc Zyngier vect = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs_start)); 414dee39247SMarc Zyngier slot = data->hyp_vectors_slot; 4156840bdd7SMarc Zyngier } 4166840bdd7SMarc Zyngier 417dee39247SMarc Zyngier if (this_cpu_has_cap(ARM64_HARDEN_EL2_VECTORS) && !has_vhe()) { 418dee39247SMarc Zyngier vect = __kvm_bp_vect_base; 419dee39247SMarc Zyngier if (slot == -1) 420dee39247SMarc Zyngier slot = __kvm_harden_el2_vector_slot; 421dee39247SMarc Zyngier } 422dee39247SMarc Zyngier 423dee39247SMarc Zyngier if (slot != -1) 424dee39247SMarc Zyngier vect += slot * SZ_2K; 425dee39247SMarc Zyngier 4266840bdd7SMarc Zyngier return vect; 4276840bdd7SMarc Zyngier } 4286840bdd7SMarc Zyngier 429dee39247SMarc Zyngier /* This is only called on a !VHE system */ 4306840bdd7SMarc Zyngier static inline int kvm_map_vectors(void) 4316840bdd7SMarc Zyngier { 432dee39247SMarc Zyngier /* 433dee39247SMarc Zyngier * HBP = ARM64_HARDEN_BRANCH_PREDICTOR 434dee39247SMarc Zyngier * HEL2 = ARM64_HARDEN_EL2_VECTORS 435dee39247SMarc Zyngier * 436dee39247SMarc Zyngier * !HBP + !HEL2 -> use direct vectors 437dee39247SMarc Zyngier * HBP + !HEL2 -> use hardened vectors in place 438dee39247SMarc Zyngier * !HBP + HEL2 -> allocate one vector slot and use exec mapping 439dee39247SMarc Zyngier * HBP + HEL2 -> use hardened vertors and use exec mapping 440dee39247SMarc Zyngier */ 441dee39247SMarc Zyngier if (cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR)) { 442dee39247SMarc Zyngier __kvm_bp_vect_base = kvm_ksym_ref(__bp_harden_hyp_vecs_start); 443dee39247SMarc Zyngier __kvm_bp_vect_base = kern_hyp_va(__kvm_bp_vect_base); 4446840bdd7SMarc Zyngier } 4456840bdd7SMarc Zyngier 446dee39247SMarc Zyngier if (cpus_have_const_cap(ARM64_HARDEN_EL2_VECTORS)) { 447dee39247SMarc Zyngier phys_addr_t vect_pa = __pa_symbol(__bp_harden_hyp_vecs_start); 448dee39247SMarc Zyngier unsigned long size = (__bp_harden_hyp_vecs_end - 449dee39247SMarc Zyngier __bp_harden_hyp_vecs_start); 450dee39247SMarc Zyngier 451dee39247SMarc Zyngier /* 452dee39247SMarc Zyngier * Always allocate a spare vector slot, as we don't 453dee39247SMarc Zyngier * know yet which CPUs have a BP hardening slot that 454dee39247SMarc Zyngier * we can reuse. 455dee39247SMarc Zyngier */ 456dee39247SMarc Zyngier __kvm_harden_el2_vector_slot = atomic_inc_return(&arm64_el2_vector_last_slot); 457dee39247SMarc Zyngier BUG_ON(__kvm_harden_el2_vector_slot >= BP_HARDEN_EL2_SLOTS); 458dee39247SMarc Zyngier return create_hyp_exec_mappings(vect_pa, size, 459dee39247SMarc Zyngier &__kvm_bp_vect_base); 460dee39247SMarc Zyngier } 461dee39247SMarc Zyngier 462dee39247SMarc Zyngier return 0; 463dee39247SMarc Zyngier } 4646840bdd7SMarc Zyngier #else 4656840bdd7SMarc Zyngier static inline void *kvm_get_hyp_vector(void) 4666840bdd7SMarc Zyngier { 4673c5e8123SMarc Zyngier return kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 4686840bdd7SMarc Zyngier } 4696840bdd7SMarc Zyngier 4706840bdd7SMarc Zyngier static inline int kvm_map_vectors(void) 4716840bdd7SMarc Zyngier { 4726840bdd7SMarc Zyngier return 0; 4736840bdd7SMarc Zyngier } 4746840bdd7SMarc Zyngier #endif 4756840bdd7SMarc Zyngier 476529c4b05SKristina Martsenko #define kvm_phys_to_vttbr(addr) phys_to_ttbr(addr) 477529c4b05SKristina Martsenko 47837c43753SMarc Zyngier #endif /* __ASSEMBLY__ */ 47937c43753SMarc Zyngier #endif /* __ARM64_KVM_MMU_H__ */ 480