xref: /openbmc/linux/arch/arm64/include/asm/kvm_mmu.h (revision 529c4b05a3cb2f324aac347042ee6d641478e946)
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 
72d53d9bc6SMarc Zyngier #define HYP_PAGE_OFFSET_HIGH_MASK	((UL(1) << VA_BITS) - 1)
73d53d9bc6SMarc Zyngier #define HYP_PAGE_OFFSET_LOW_MASK	((UL(1) << (VA_BITS - 1)) - 1)
74d53d9bc6SMarc Zyngier 
7537c43753SMarc Zyngier #ifdef __ASSEMBLY__
7637c43753SMarc Zyngier 
77cedbb8b7SMarc Zyngier #include <asm/alternative.h>
78cedbb8b7SMarc Zyngier #include <asm/cpufeature.h>
79cedbb8b7SMarc Zyngier 
8037c43753SMarc Zyngier /*
8137c43753SMarc Zyngier  * Convert a kernel VA into a HYP VA.
8237c43753SMarc Zyngier  * reg: VA to be converted.
83fd81e6bfSMarc Zyngier  *
84fd81e6bfSMarc Zyngier  * This generates the following sequences:
85fd81e6bfSMarc Zyngier  * - High mask:
86fd81e6bfSMarc Zyngier  *		and x0, x0, #HYP_PAGE_OFFSET_HIGH_MASK
87fd81e6bfSMarc Zyngier  *		nop
88fd81e6bfSMarc Zyngier  * - Low mask:
89fd81e6bfSMarc Zyngier  *		and x0, x0, #HYP_PAGE_OFFSET_HIGH_MASK
90fd81e6bfSMarc Zyngier  *		and x0, x0, #HYP_PAGE_OFFSET_LOW_MASK
91fd81e6bfSMarc Zyngier  * - VHE:
92fd81e6bfSMarc Zyngier  *		nop
93fd81e6bfSMarc Zyngier  *		nop
94fd81e6bfSMarc Zyngier  *
95fd81e6bfSMarc Zyngier  * The "low mask" version works because the mask is a strict subset of
96fd81e6bfSMarc Zyngier  * the "high mask", hence performing the first mask for nothing.
97fd81e6bfSMarc Zyngier  * Should be completely invisible on any viable CPU.
9837c43753SMarc Zyngier  */
9937c43753SMarc Zyngier .macro kern_hyp_va	reg
100cedbb8b7SMarc Zyngier alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
101fd81e6bfSMarc Zyngier 	and     \reg, \reg, #HYP_PAGE_OFFSET_HIGH_MASK
102e506236aSMark Rutland alternative_else_nop_endif
103e506236aSMark Rutland alternative_if ARM64_HYP_OFFSET_LOW
104fd81e6bfSMarc Zyngier 	and     \reg, \reg, #HYP_PAGE_OFFSET_LOW_MASK
105e506236aSMark Rutland alternative_else_nop_endif
10637c43753SMarc Zyngier .endm
10737c43753SMarc Zyngier 
10837c43753SMarc Zyngier #else
10937c43753SMarc Zyngier 
11038f791a4SChristoffer Dall #include <asm/pgalloc.h>
11102f7760eSWill Deacon #include <asm/cache.h>
11237c43753SMarc Zyngier #include <asm/cacheflush.h>
113e4c5a685SArd Biesheuvel #include <asm/mmu_context.h>
114e4c5a685SArd Biesheuvel #include <asm/pgtable.h>
11537c43753SMarc Zyngier 
116fd81e6bfSMarc Zyngier static inline unsigned long __kern_hyp_va(unsigned long v)
117fd81e6bfSMarc Zyngier {
118fd81e6bfSMarc Zyngier 	asm volatile(ALTERNATIVE("and %0, %0, %1",
119fd81e6bfSMarc Zyngier 				 "nop",
120fd81e6bfSMarc Zyngier 				 ARM64_HAS_VIRT_HOST_EXTN)
121fd81e6bfSMarc Zyngier 		     : "+r" (v)
122fd81e6bfSMarc Zyngier 		     : "i" (HYP_PAGE_OFFSET_HIGH_MASK));
123fd81e6bfSMarc Zyngier 	asm volatile(ALTERNATIVE("nop",
124fd81e6bfSMarc Zyngier 				 "and %0, %0, %1",
125fd81e6bfSMarc Zyngier 				 ARM64_HYP_OFFSET_LOW)
126fd81e6bfSMarc Zyngier 		     : "+r" (v)
127fd81e6bfSMarc Zyngier 		     : "i" (HYP_PAGE_OFFSET_LOW_MASK));
128fd81e6bfSMarc Zyngier 	return v;
129fd81e6bfSMarc Zyngier }
130fd81e6bfSMarc Zyngier 
13194d0e598SMarc Zyngier #define kern_hyp_va(v) 	((typeof(v))(__kern_hyp_va((unsigned long)(v))))
13237c43753SMarc Zyngier 
13337c43753SMarc 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);
14337c43753SMarc Zyngier int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
14437c43753SMarc Zyngier void free_hyp_pgds(void);
14537c43753SMarc Zyngier 
146957db105SChristoffer Dall void stage2_unmap_vm(struct kvm *kvm);
14737c43753SMarc Zyngier int kvm_alloc_stage2_pgd(struct kvm *kvm);
14837c43753SMarc Zyngier void kvm_free_stage2_pgd(struct kvm *kvm);
14937c43753SMarc Zyngier int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
150c40f2f8fSArd Biesheuvel 			  phys_addr_t pa, unsigned long size, bool writable);
15137c43753SMarc Zyngier 
15237c43753SMarc Zyngier int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
15337c43753SMarc Zyngier 
15437c43753SMarc Zyngier void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
15537c43753SMarc Zyngier 
15637c43753SMarc Zyngier phys_addr_t kvm_mmu_get_httbr(void);
15737c43753SMarc Zyngier phys_addr_t kvm_get_idmap_vector(void);
15837c43753SMarc Zyngier int kvm_mmu_init(void);
15937c43753SMarc Zyngier void kvm_clear_hyp_idmap(void);
16037c43753SMarc Zyngier 
16137c43753SMarc Zyngier #define	kvm_set_pte(ptep, pte)		set_pte(ptep, pte)
162ad361f09SChristoffer Dall #define	kvm_set_pmd(pmdp, pmd)		set_pmd(pmdp, pmd)
16337c43753SMarc Zyngier 
16406485053SCatalin Marinas static inline pte_t kvm_s2pte_mkwrite(pte_t pte)
16537c43753SMarc Zyngier {
16606485053SCatalin Marinas 	pte_val(pte) |= PTE_S2_RDWR;
16706485053SCatalin Marinas 	return pte;
16837c43753SMarc Zyngier }
16937c43753SMarc Zyngier 
17006485053SCatalin Marinas static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd)
171ad361f09SChristoffer Dall {
17206485053SCatalin Marinas 	pmd_val(pmd) |= PMD_S2_RDWR;
17306485053SCatalin Marinas 	return pmd;
174ad361f09SChristoffer Dall }
175ad361f09SChristoffer Dall 
1768199ed0eSMario Smarduch static inline void kvm_set_s2pte_readonly(pte_t *pte)
1778199ed0eSMario Smarduch {
1780966253dSCatalin Marinas 	pteval_t old_pteval, pteval;
17906485053SCatalin Marinas 
1800966253dSCatalin Marinas 	pteval = READ_ONCE(pte_val(*pte));
1810966253dSCatalin Marinas 	do {
1820966253dSCatalin Marinas 		old_pteval = pteval;
1830966253dSCatalin Marinas 		pteval &= ~PTE_S2_RDWR;
1840966253dSCatalin Marinas 		pteval |= PTE_S2_RDONLY;
1850966253dSCatalin Marinas 		pteval = cmpxchg_relaxed(&pte_val(*pte), old_pteval, pteval);
1860966253dSCatalin Marinas 	} while (pteval != old_pteval);
1878199ed0eSMario Smarduch }
1888199ed0eSMario Smarduch 
1898199ed0eSMario Smarduch static inline bool kvm_s2pte_readonly(pte_t *pte)
1908199ed0eSMario Smarduch {
1918199ed0eSMario Smarduch 	return (pte_val(*pte) & PTE_S2_RDWR) == PTE_S2_RDONLY;
1928199ed0eSMario Smarduch }
1938199ed0eSMario Smarduch 
1948199ed0eSMario Smarduch static inline void kvm_set_s2pmd_readonly(pmd_t *pmd)
1958199ed0eSMario Smarduch {
19606485053SCatalin Marinas 	kvm_set_s2pte_readonly((pte_t *)pmd);
1978199ed0eSMario Smarduch }
1988199ed0eSMario Smarduch 
1998199ed0eSMario Smarduch static inline bool kvm_s2pmd_readonly(pmd_t *pmd)
2008199ed0eSMario Smarduch {
20106485053SCatalin Marinas 	return kvm_s2pte_readonly((pte_t *)pmd);
20238f791a4SChristoffer Dall }
20338f791a4SChristoffer Dall 
2044f853a71SChristoffer Dall static inline bool kvm_page_empty(void *ptr)
2054f853a71SChristoffer Dall {
2064f853a71SChristoffer Dall 	struct page *ptr_page = virt_to_page(ptr);
2074f853a71SChristoffer Dall 	return page_count(ptr_page) == 1;
2084f853a71SChristoffer Dall }
2094f853a71SChristoffer Dall 
21066f877faSSuzuki K Poulose #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
21138f791a4SChristoffer Dall 
21238f791a4SChristoffer Dall #ifdef __PAGETABLE_PMD_FOLDED
21366f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) (0)
2144f853a71SChristoffer Dall #else
21566f877faSSuzuki K Poulose #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
2164f853a71SChristoffer Dall #endif
21738f791a4SChristoffer Dall 
21838f791a4SChristoffer Dall #ifdef __PAGETABLE_PUD_FOLDED
21966f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) (0)
22038f791a4SChristoffer Dall #else
22166f877faSSuzuki K Poulose #define hyp_pud_table_empty(pudp) kvm_page_empty(pudp)
22238f791a4SChristoffer Dall #endif
2234f853a71SChristoffer Dall 
22437c43753SMarc Zyngier struct kvm;
22537c43753SMarc Zyngier 
2262d58b733SMarc Zyngier #define kvm_flush_dcache_to_poc(a,l)	__flush_dcache_area((a), (l))
2272d58b733SMarc Zyngier 
2282d58b733SMarc Zyngier static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
2292d58b733SMarc Zyngier {
2302d58b733SMarc Zyngier 	return (vcpu_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101;
2312d58b733SMarc Zyngier }
2322d58b733SMarc Zyngier 
233ba049e93SDan Williams static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu,
234ba049e93SDan Williams 					       kvm_pfn_t pfn,
23513b7756cSMarc Zyngier 					       unsigned long size)
23637c43753SMarc Zyngier {
2370d3e4d4fSMarc Zyngier 	void *va = page_address(pfn_to_page(pfn));
2380d3e4d4fSMarc Zyngier 
2390d3e4d4fSMarc Zyngier 	kvm_flush_dcache_to_poc(va, size);
2402d58b733SMarc Zyngier 
24187da236eSWill Deacon 	if (icache_is_aliasing()) {
24237c43753SMarc Zyngier 		/* any kind of VIPT cache */
24337c43753SMarc Zyngier 		__flush_icache_all();
24487da236eSWill Deacon 	} else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) {
24587da236eSWill Deacon 		/* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */
24687da236eSWill Deacon 		flush_icache_range((unsigned long)va,
24787da236eSWill Deacon 				   (unsigned long)va + size);
24837c43753SMarc Zyngier 	}
24937c43753SMarc Zyngier }
25037c43753SMarc Zyngier 
251363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pte(pte_t pte)
252363ef89fSMarc Zyngier {
253363ef89fSMarc Zyngier 	struct page *page = pte_page(pte);
254363ef89fSMarc Zyngier 	kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE);
255363ef89fSMarc Zyngier }
256363ef89fSMarc Zyngier 
257363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
258363ef89fSMarc Zyngier {
259363ef89fSMarc Zyngier 	struct page *page = pmd_page(pmd);
260363ef89fSMarc Zyngier 	kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE);
261363ef89fSMarc Zyngier }
262363ef89fSMarc Zyngier 
263363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pud(pud_t pud)
264363ef89fSMarc Zyngier {
265363ef89fSMarc Zyngier 	struct page *page = pud_page(pud);
266363ef89fSMarc Zyngier 	kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE);
267363ef89fSMarc Zyngier }
268363ef89fSMarc Zyngier 
2692077be67SLaura Abbott #define kvm_virt_to_phys(x)		__pa_symbol(x)
27037c43753SMarc Zyngier 
2713c1e7165SMarc Zyngier void kvm_set_way_flush(struct kvm_vcpu *vcpu);
2723c1e7165SMarc Zyngier void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
2739d218a1fSMarc Zyngier 
274e4c5a685SArd Biesheuvel static inline bool __kvm_cpu_uses_extended_idmap(void)
275e4c5a685SArd Biesheuvel {
276e4c5a685SArd Biesheuvel 	return __cpu_uses_extended_idmap();
277e4c5a685SArd Biesheuvel }
278e4c5a685SArd Biesheuvel 
279e4c5a685SArd Biesheuvel static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
280e4c5a685SArd Biesheuvel 				       pgd_t *hyp_pgd,
281e4c5a685SArd Biesheuvel 				       pgd_t *merged_hyp_pgd,
282e4c5a685SArd Biesheuvel 				       unsigned long hyp_idmap_start)
283e4c5a685SArd Biesheuvel {
284e4c5a685SArd Biesheuvel 	int idmap_idx;
285e4c5a685SArd Biesheuvel 
286e4c5a685SArd Biesheuvel 	/*
287e4c5a685SArd Biesheuvel 	 * Use the first entry to access the HYP mappings. It is
288e4c5a685SArd Biesheuvel 	 * guaranteed to be free, otherwise we wouldn't use an
289e4c5a685SArd Biesheuvel 	 * extended idmap.
290e4c5a685SArd Biesheuvel 	 */
291e4c5a685SArd Biesheuvel 	VM_BUG_ON(pgd_val(merged_hyp_pgd[0]));
292e4c5a685SArd Biesheuvel 	merged_hyp_pgd[0] = __pgd(__pa(hyp_pgd) | PMD_TYPE_TABLE);
293e4c5a685SArd Biesheuvel 
294e4c5a685SArd Biesheuvel 	/*
295e4c5a685SArd Biesheuvel 	 * Create another extended level entry that points to the boot HYP map,
296e4c5a685SArd Biesheuvel 	 * which contains an ID mapping of the HYP init code. We essentially
297e4c5a685SArd Biesheuvel 	 * merge the boot and runtime HYP maps by doing so, but they don't
298e4c5a685SArd Biesheuvel 	 * overlap anyway, so this is fine.
299e4c5a685SArd Biesheuvel 	 */
300e4c5a685SArd Biesheuvel 	idmap_idx = hyp_idmap_start >> VA_BITS;
301e4c5a685SArd Biesheuvel 	VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx]));
302e4c5a685SArd Biesheuvel 	merged_hyp_pgd[idmap_idx] = __pgd(__pa(boot_hyp_pgd) | PMD_TYPE_TABLE);
303e4c5a685SArd Biesheuvel }
304e4c5a685SArd Biesheuvel 
30520475f78SVladimir Murzin static inline unsigned int kvm_get_vmid_bits(void)
30620475f78SVladimir Murzin {
30746823dd1SDave Martin 	int reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
30820475f78SVladimir Murzin 
30928c5dcb2SSuzuki K Poulose 	return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8;
31020475f78SVladimir Murzin }
31120475f78SVladimir Murzin 
312*529c4b05SKristina Martsenko #define kvm_phys_to_vttbr(addr)		phys_to_ttbr(addr)
313*529c4b05SKristina Martsenko 
31437c43753SMarc Zyngier #endif /* __ASSEMBLY__ */
31537c43753SMarc Zyngier #endif /* __ARM64_KVM_MMU_H__ */
316