xref: /openbmc/linux/arch/arm64/include/asm/kvm_mmu.h (revision 807a378425d27d377fdf181d1dba91539bac9294)
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
882b4d1606SMarc Zyngier 	and     \reg, \reg, #1
892b4d1606SMarc Zyngier alternative_cb_end
9037c43753SMarc Zyngier .endm
9137c43753SMarc Zyngier 
9237c43753SMarc Zyngier #else
9337c43753SMarc Zyngier 
9438f791a4SChristoffer Dall #include <asm/pgalloc.h>
9502f7760eSWill Deacon #include <asm/cache.h>
9637c43753SMarc Zyngier #include <asm/cacheflush.h>
97e4c5a685SArd Biesheuvel #include <asm/mmu_context.h>
98e4c5a685SArd Biesheuvel #include <asm/pgtable.h>
9937c43753SMarc Zyngier 
1002b4d1606SMarc Zyngier void kvm_update_va_mask(struct alt_instr *alt,
1012b4d1606SMarc Zyngier 			__le32 *origptr, __le32 *updptr, int nr_inst);
1022b4d1606SMarc Zyngier 
103fd81e6bfSMarc Zyngier static inline unsigned long __kern_hyp_va(unsigned long v)
104fd81e6bfSMarc Zyngier {
1052b4d1606SMarc Zyngier 	asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n",
1062b4d1606SMarc Zyngier 				    kvm_update_va_mask)
1072b4d1606SMarc Zyngier 		     : "+r" (v));
108fd81e6bfSMarc Zyngier 	return v;
109fd81e6bfSMarc Zyngier }
110fd81e6bfSMarc Zyngier 
11194d0e598SMarc Zyngier #define kern_hyp_va(v) 	((typeof(v))(__kern_hyp_va((unsigned long)(v))))
11237c43753SMarc Zyngier 
11337c43753SMarc Zyngier /*
11444a497abSMarc Zyngier  * Obtain the PC-relative address of a kernel symbol
11544a497abSMarc Zyngier  * s: symbol
11644a497abSMarc Zyngier  *
11744a497abSMarc Zyngier  * The goal of this macro is to return a symbol's address based on a
11844a497abSMarc Zyngier  * PC-relative computation, as opposed to a loading the VA from a
11944a497abSMarc Zyngier  * constant pool or something similar. This works well for HYP, as an
12044a497abSMarc Zyngier  * absolute VA is guaranteed to be wrong. Only use this if trying to
12144a497abSMarc Zyngier  * obtain the address of a symbol (i.e. not something you obtained by
12244a497abSMarc Zyngier  * following a pointer).
12344a497abSMarc Zyngier  */
12444a497abSMarc Zyngier #define hyp_symbol_addr(s)						\
12544a497abSMarc Zyngier 	({								\
12644a497abSMarc Zyngier 		typeof(s) *addr;					\
12744a497abSMarc Zyngier 		asm("adrp	%0, %1\n"				\
12844a497abSMarc Zyngier 		    "add	%0, %0, :lo12:%1\n"			\
12944a497abSMarc Zyngier 		    : "=r" (addr) : "S" (&s));				\
13044a497abSMarc Zyngier 		addr;							\
13144a497abSMarc Zyngier 	})
13244a497abSMarc Zyngier 
13344a497abSMarc Zyngier /*
134dbff124eSJoel Schopp  * We currently only support a 40bit IPA.
13537c43753SMarc Zyngier  */
136dbff124eSJoel Schopp #define KVM_PHYS_SHIFT	(40)
13737c43753SMarc Zyngier #define KVM_PHYS_SIZE	(1UL << KVM_PHYS_SHIFT)
13837c43753SMarc Zyngier #define KVM_PHYS_MASK	(KVM_PHYS_SIZE - 1UL)
13937c43753SMarc Zyngier 
140c0ef6326SSuzuki K Poulose #include <asm/stage2_pgtable.h>
141c0ef6326SSuzuki K Poulose 
142c8dddecdSMarc Zyngier int create_hyp_mappings(void *from, void *to, pgprot_t prot);
143*807a3784SMarc Zyngier int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
144*807a3784SMarc Zyngier 			   void __iomem **kaddr);
14537c43753SMarc Zyngier void free_hyp_pgds(void);
14637c43753SMarc Zyngier 
147957db105SChristoffer Dall void stage2_unmap_vm(struct kvm *kvm);
14837c43753SMarc Zyngier int kvm_alloc_stage2_pgd(struct kvm *kvm);
14937c43753SMarc Zyngier void kvm_free_stage2_pgd(struct kvm *kvm);
15037c43753SMarc Zyngier int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
151c40f2f8fSArd Biesheuvel 			  phys_addr_t pa, unsigned long size, bool writable);
15237c43753SMarc Zyngier 
15337c43753SMarc Zyngier int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
15437c43753SMarc Zyngier 
15537c43753SMarc Zyngier void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
15637c43753SMarc Zyngier 
15737c43753SMarc Zyngier phys_addr_t kvm_mmu_get_httbr(void);
15837c43753SMarc Zyngier phys_addr_t kvm_get_idmap_vector(void);
15937c43753SMarc Zyngier int kvm_mmu_init(void);
16037c43753SMarc Zyngier void kvm_clear_hyp_idmap(void);
16137c43753SMarc Zyngier 
16237c43753SMarc Zyngier #define	kvm_set_pte(ptep, pte)		set_pte(ptep, pte)
163ad361f09SChristoffer Dall #define	kvm_set_pmd(pmdp, pmd)		set_pmd(pmdp, pmd)
16437c43753SMarc Zyngier 
16506485053SCatalin Marinas static inline pte_t kvm_s2pte_mkwrite(pte_t pte)
16637c43753SMarc Zyngier {
16706485053SCatalin Marinas 	pte_val(pte) |= PTE_S2_RDWR;
16806485053SCatalin Marinas 	return pte;
16937c43753SMarc Zyngier }
17037c43753SMarc Zyngier 
17106485053SCatalin Marinas static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd)
172ad361f09SChristoffer Dall {
17306485053SCatalin Marinas 	pmd_val(pmd) |= PMD_S2_RDWR;
17406485053SCatalin Marinas 	return pmd;
175ad361f09SChristoffer Dall }
176ad361f09SChristoffer Dall 
177d0e22b4aSMarc Zyngier static inline pte_t kvm_s2pte_mkexec(pte_t pte)
178d0e22b4aSMarc Zyngier {
179d0e22b4aSMarc Zyngier 	pte_val(pte) &= ~PTE_S2_XN;
180d0e22b4aSMarc Zyngier 	return pte;
181d0e22b4aSMarc Zyngier }
182d0e22b4aSMarc Zyngier 
183d0e22b4aSMarc Zyngier static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd)
184d0e22b4aSMarc Zyngier {
185d0e22b4aSMarc Zyngier 	pmd_val(pmd) &= ~PMD_S2_XN;
186d0e22b4aSMarc Zyngier 	return pmd;
187d0e22b4aSMarc Zyngier }
188d0e22b4aSMarc Zyngier 
18920a004e7SWill Deacon static inline void kvm_set_s2pte_readonly(pte_t *ptep)
1908199ed0eSMario Smarduch {
1910966253dSCatalin Marinas 	pteval_t old_pteval, pteval;
19206485053SCatalin Marinas 
19320a004e7SWill Deacon 	pteval = READ_ONCE(pte_val(*ptep));
1940966253dSCatalin Marinas 	do {
1950966253dSCatalin Marinas 		old_pteval = pteval;
1960966253dSCatalin Marinas 		pteval &= ~PTE_S2_RDWR;
1970966253dSCatalin Marinas 		pteval |= PTE_S2_RDONLY;
19820a004e7SWill Deacon 		pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval);
1990966253dSCatalin Marinas 	} while (pteval != old_pteval);
2008199ed0eSMario Smarduch }
2018199ed0eSMario Smarduch 
20220a004e7SWill Deacon static inline bool kvm_s2pte_readonly(pte_t *ptep)
2038199ed0eSMario Smarduch {
20420a004e7SWill Deacon 	return (READ_ONCE(pte_val(*ptep)) & PTE_S2_RDWR) == PTE_S2_RDONLY;
2058199ed0eSMario Smarduch }
2068199ed0eSMario Smarduch 
20720a004e7SWill Deacon static inline bool kvm_s2pte_exec(pte_t *ptep)
2087a3796d2SMarc Zyngier {
20920a004e7SWill Deacon 	return !(READ_ONCE(pte_val(*ptep)) & PTE_S2_XN);
2107a3796d2SMarc Zyngier }
2117a3796d2SMarc Zyngier 
21220a004e7SWill Deacon static inline void kvm_set_s2pmd_readonly(pmd_t *pmdp)
2138199ed0eSMario Smarduch {
21420a004e7SWill Deacon 	kvm_set_s2pte_readonly((pte_t *)pmdp);
2158199ed0eSMario Smarduch }
2168199ed0eSMario Smarduch 
21720a004e7SWill Deacon static inline bool kvm_s2pmd_readonly(pmd_t *pmdp)
2188199ed0eSMario Smarduch {
21920a004e7SWill Deacon 	return kvm_s2pte_readonly((pte_t *)pmdp);
22038f791a4SChristoffer Dall }
22138f791a4SChristoffer Dall 
22220a004e7SWill Deacon static inline bool kvm_s2pmd_exec(pmd_t *pmdp)
2237a3796d2SMarc Zyngier {
22420a004e7SWill Deacon 	return !(READ_ONCE(pmd_val(*pmdp)) & PMD_S2_XN);
2257a3796d2SMarc Zyngier }
2267a3796d2SMarc Zyngier 
2274f853a71SChristoffer Dall static inline bool kvm_page_empty(void *ptr)
2284f853a71SChristoffer Dall {
2294f853a71SChristoffer Dall 	struct page *ptr_page = virt_to_page(ptr);
2304f853a71SChristoffer Dall 	return page_count(ptr_page) == 1;
2314f853a71SChristoffer Dall }
2324f853a71SChristoffer Dall 
23366f877faSSuzuki K Poulose #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
23438f791a4SChristoffer Dall 
23538f791a4SChristoffer Dall #ifdef __PAGETABLE_PMD_FOLDED
23666f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) (0)
2374f853a71SChristoffer Dall #else
23866f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
2394f853a71SChristoffer Dall #endif
24038f791a4SChristoffer Dall 
24138f791a4SChristoffer Dall #ifdef __PAGETABLE_PUD_FOLDED
24266f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) (0)
24338f791a4SChristoffer Dall #else
24466f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) kvm_page_empty(pudp)
24538f791a4SChristoffer Dall #endif
2464f853a71SChristoffer Dall 
24737c43753SMarc Zyngier struct kvm;
24837c43753SMarc Zyngier 
2492d58b733SMarc Zyngier #define kvm_flush_dcache_to_poc(a,l)	__flush_dcache_area((a), (l))
2502d58b733SMarc Zyngier 
2512d58b733SMarc Zyngier static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
2522d58b733SMarc Zyngier {
2538d404c4cSChristoffer Dall 	return (vcpu_read_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101;
2542d58b733SMarc Zyngier }
2552d58b733SMarc Zyngier 
25617ab9d57SMarc Zyngier static inline void __clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
25737c43753SMarc Zyngier {
2580d3e4d4fSMarc Zyngier 	void *va = page_address(pfn_to_page(pfn));
2590d3e4d4fSMarc Zyngier 
2600d3e4d4fSMarc Zyngier 	kvm_flush_dcache_to_poc(va, size);
261a15f6939SMarc Zyngier }
2622d58b733SMarc Zyngier 
26317ab9d57SMarc Zyngier static inline void __invalidate_icache_guest_page(kvm_pfn_t pfn,
264a15f6939SMarc Zyngier 						  unsigned long size)
265a15f6939SMarc Zyngier {
26687da236eSWill Deacon 	if (icache_is_aliasing()) {
26737c43753SMarc Zyngier 		/* any kind of VIPT cache */
26837c43753SMarc Zyngier 		__flush_icache_all();
26987da236eSWill Deacon 	} else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) {
27087da236eSWill Deacon 		/* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */
271a15f6939SMarc Zyngier 		void *va = page_address(pfn_to_page(pfn));
272a15f6939SMarc Zyngier 
2734fee9473SMarc Zyngier 		invalidate_icache_range((unsigned long)va,
27487da236eSWill Deacon 					(unsigned long)va + size);
27537c43753SMarc Zyngier 	}
27637c43753SMarc Zyngier }
27737c43753SMarc Zyngier 
278363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pte(pte_t pte)
279363ef89fSMarc Zyngier {
280363ef89fSMarc Zyngier 	struct page *page = pte_page(pte);
281363ef89fSMarc Zyngier 	kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE);
282363ef89fSMarc Zyngier }
283363ef89fSMarc Zyngier 
284363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
285363ef89fSMarc Zyngier {
286363ef89fSMarc Zyngier 	struct page *page = pmd_page(pmd);
287363ef89fSMarc Zyngier 	kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE);
288363ef89fSMarc Zyngier }
289363ef89fSMarc Zyngier 
290363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pud(pud_t pud)
291363ef89fSMarc Zyngier {
292363ef89fSMarc Zyngier 	struct page *page = pud_page(pud);
293363ef89fSMarc Zyngier 	kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE);
294363ef89fSMarc Zyngier }
295363ef89fSMarc Zyngier 
2962077be67SLaura Abbott #define kvm_virt_to_phys(x)		__pa_symbol(x)
29737c43753SMarc Zyngier 
2983c1e7165SMarc Zyngier void kvm_set_way_flush(struct kvm_vcpu *vcpu);
2993c1e7165SMarc Zyngier void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
3009d218a1fSMarc Zyngier 
301e4c5a685SArd Biesheuvel static inline bool __kvm_cpu_uses_extended_idmap(void)
302e4c5a685SArd Biesheuvel {
303fa2a8445SKristina Martsenko 	return __cpu_uses_extended_idmap_level();
304fa2a8445SKristina Martsenko }
305fa2a8445SKristina Martsenko 
306fa2a8445SKristina Martsenko static inline unsigned long __kvm_idmap_ptrs_per_pgd(void)
307fa2a8445SKristina Martsenko {
308fa2a8445SKristina Martsenko 	return idmap_ptrs_per_pgd;
309e4c5a685SArd Biesheuvel }
310e4c5a685SArd Biesheuvel 
31119338304SKristina Martsenko /*
31219338304SKristina Martsenko  * Can't use pgd_populate here, because the extended idmap adds an extra level
31319338304SKristina Martsenko  * above CONFIG_PGTABLE_LEVELS (which is 2 or 3 if we're using the extended
31419338304SKristina Martsenko  * idmap), and pgd_populate is only available if CONFIG_PGTABLE_LEVELS = 4.
31519338304SKristina Martsenko  */
316e4c5a685SArd Biesheuvel static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
317e4c5a685SArd Biesheuvel 				       pgd_t *hyp_pgd,
318e4c5a685SArd Biesheuvel 				       pgd_t *merged_hyp_pgd,
319e4c5a685SArd Biesheuvel 				       unsigned long hyp_idmap_start)
320e4c5a685SArd Biesheuvel {
321e4c5a685SArd Biesheuvel 	int idmap_idx;
32275387b92SKristina Martsenko 	u64 pgd_addr;
323e4c5a685SArd Biesheuvel 
324e4c5a685SArd Biesheuvel 	/*
325e4c5a685SArd Biesheuvel 	 * Use the first entry to access the HYP mappings. It is
326e4c5a685SArd Biesheuvel 	 * guaranteed to be free, otherwise we wouldn't use an
327e4c5a685SArd Biesheuvel 	 * extended idmap.
328e4c5a685SArd Biesheuvel 	 */
329e4c5a685SArd Biesheuvel 	VM_BUG_ON(pgd_val(merged_hyp_pgd[0]));
33075387b92SKristina Martsenko 	pgd_addr = __phys_to_pgd_val(__pa(hyp_pgd));
33175387b92SKristina Martsenko 	merged_hyp_pgd[0] = __pgd(pgd_addr | PMD_TYPE_TABLE);
332e4c5a685SArd Biesheuvel 
333e4c5a685SArd Biesheuvel 	/*
334e4c5a685SArd Biesheuvel 	 * Create another extended level entry that points to the boot HYP map,
335e4c5a685SArd Biesheuvel 	 * which contains an ID mapping of the HYP init code. We essentially
336e4c5a685SArd Biesheuvel 	 * merge the boot and runtime HYP maps by doing so, but they don't
337e4c5a685SArd Biesheuvel 	 * overlap anyway, so this is fine.
338e4c5a685SArd Biesheuvel 	 */
339e4c5a685SArd Biesheuvel 	idmap_idx = hyp_idmap_start >> VA_BITS;
340e4c5a685SArd Biesheuvel 	VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx]));
34175387b92SKristina Martsenko 	pgd_addr = __phys_to_pgd_val(__pa(boot_hyp_pgd));
34275387b92SKristina Martsenko 	merged_hyp_pgd[idmap_idx] = __pgd(pgd_addr | PMD_TYPE_TABLE);
343e4c5a685SArd Biesheuvel }
344e4c5a685SArd Biesheuvel 
34520475f78SVladimir Murzin static inline unsigned int kvm_get_vmid_bits(void)
34620475f78SVladimir Murzin {
34746823dd1SDave Martin 	int reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
34820475f78SVladimir Murzin 
34928c5dcb2SSuzuki K Poulose 	return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8;
35020475f78SVladimir Murzin }
35120475f78SVladimir Murzin 
3526840bdd7SMarc Zyngier #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
3536840bdd7SMarc Zyngier #include <asm/mmu.h>
3546840bdd7SMarc Zyngier 
3556840bdd7SMarc Zyngier static inline void *kvm_get_hyp_vector(void)
3566840bdd7SMarc Zyngier {
3576840bdd7SMarc Zyngier 	struct bp_hardening_data *data = arm64_get_bp_hardening_data();
3586840bdd7SMarc Zyngier 	void *vect = kvm_ksym_ref(__kvm_hyp_vector);
3596840bdd7SMarc Zyngier 
3606840bdd7SMarc Zyngier 	if (data->fn) {
3616840bdd7SMarc Zyngier 		vect = __bp_harden_hyp_vecs_start +
3626840bdd7SMarc Zyngier 		       data->hyp_vectors_slot * SZ_2K;
3636840bdd7SMarc Zyngier 
3646840bdd7SMarc Zyngier 		if (!has_vhe())
3656840bdd7SMarc Zyngier 			vect = lm_alias(vect);
3666840bdd7SMarc Zyngier 	}
3676840bdd7SMarc Zyngier 
3686840bdd7SMarc Zyngier 	return vect;
3696840bdd7SMarc Zyngier }
3706840bdd7SMarc Zyngier 
3716840bdd7SMarc Zyngier static inline int kvm_map_vectors(void)
3726840bdd7SMarc Zyngier {
3736840bdd7SMarc Zyngier 	return create_hyp_mappings(kvm_ksym_ref(__bp_harden_hyp_vecs_start),
3746840bdd7SMarc Zyngier 				   kvm_ksym_ref(__bp_harden_hyp_vecs_end),
3756840bdd7SMarc Zyngier 				   PAGE_HYP_EXEC);
3766840bdd7SMarc Zyngier }
3776840bdd7SMarc Zyngier 
3786840bdd7SMarc Zyngier #else
3796840bdd7SMarc Zyngier static inline void *kvm_get_hyp_vector(void)
3806840bdd7SMarc Zyngier {
3816840bdd7SMarc Zyngier 	return kvm_ksym_ref(__kvm_hyp_vector);
3826840bdd7SMarc Zyngier }
3836840bdd7SMarc Zyngier 
3846840bdd7SMarc Zyngier static inline int kvm_map_vectors(void)
3856840bdd7SMarc Zyngier {
3866840bdd7SMarc Zyngier 	return 0;
3876840bdd7SMarc Zyngier }
3886840bdd7SMarc Zyngier #endif
3896840bdd7SMarc Zyngier 
390529c4b05SKristina Martsenko #define kvm_phys_to_vttbr(addr)		phys_to_ttbr(addr)
391529c4b05SKristina Martsenko 
39237c43753SMarc Zyngier #endif /* __ASSEMBLY__ */
39337c43753SMarc Zyngier #endif /* __ARM64_KVM_MMU_H__ */
394