1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012,2013 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #ifndef __ARM64_KVM_MMU_H__
8 #define __ARM64_KVM_MMU_H__
11 #include <asm/memory.h>
12 #include <asm/cpufeature.h>
15 * As ARMv8.0 only has the TTBR0_EL2 register, we cannot express
16 * "negative" addresses. This makes it impossible to directly share
17 * mappings with the kernel.
19 * Instead, give the HYP mode its own VA region at a fixed offset from
20 * the kernel by just masking the top bits (which are all ones for a
21 * kernel address). We need to find out how many bits to mask.
23 * We want to build a set of page tables that cover both parts of the
24 * idmap (the trampoline page used to initialize EL2), and our normal
25 * runtime VA space, at the same time.
27 * Given that the kernel uses VA_BITS for its entire address space,
28 * and that half of that space (VA_BITS - 1) is used for the linear
29 * mapping, we can also limit the EL2 space to (VA_BITS - 1).
31 * The main question is "Within the VA_BITS space, does EL2 use the
32 * top or the bottom half of that space to shadow the kernel's linear
33 * mapping?". As we need to idmap the trampoline page, this is
34 * determined by the range in which this page lives.
36 * If the page is in the bottom half, we have to use the top half. If
37 * the page is in the top half, we have to use the bottom half:
39 * T = __pa_symbol(__hyp_idmap_text_start)
40 * if (T & BIT(VA_BITS - 1))
41 * HYP_VA_MIN = 0 //idmap in upper half
43 * HYP_VA_MIN = 1 << (VA_BITS - 1)
44 * HYP_VA_MAX = HYP_VA_MIN + (1 << (VA_BITS - 1)) - 1
46 * This of course assumes that the trampoline page exists within the
47 * VA_BITS range. If it doesn't, then it means we're in the odd case
48 * where the kernel idmap (as well as HYP) uses more levels than the
49 * kernel runtime page tables (as seen when the kernel is configured
50 * for 4k pages, 39bits VA, and yet memory lives just above that
51 * limit, forcing the idmap to use 4 levels of page tables while the
52 * kernel itself only uses 3). In this particular case, it doesn't
53 * matter which side of VA_BITS we use, as we're guaranteed not to
54 * conflict with anything.
56 * When using VHE, there are no separate hyp mappings and all KVM
57 * functionality is already mapped as part of the main kernel
58 * mappings, and none of this applies in that case.
63 #include <asm/alternative.h>
66 * Convert a kernel VA into a HYP VA.
67 * reg: VA to be converted.
69 * The actual code generation takes place in kvm_update_va_mask, and
70 * the instructions below are only there to reserve the space and
71 * perform the register allocation (kvm_update_va_mask uses the
72 * specific registers encoded in the instructions).
74 .macro kern_hyp_va reg
75 alternative_cb kvm_update_va_mask
76 and \reg, \reg, #1 /* mask with va_mask */
77 ror \reg, \reg, #1 /* rotate to the first tag bit */
78 add \reg, \reg, #0 /* insert the low 12 bits of the tag */
79 add \reg, \reg, #0, lsl 12 /* insert the top 12 bits of the tag */
80 ror \reg, \reg, #63 /* rotate back */
86 #include <linux/pgtable.h>
87 #include <asm/pgalloc.h>
88 #include <asm/cache.h>
89 #include <asm/cacheflush.h>
90 #include <asm/mmu_context.h>
92 void kvm_update_va_mask(struct alt_instr *alt,
93 __le32 *origptr, __le32 *updptr, int nr_inst);
94 void kvm_compute_layout(void);
96 static __always_inline unsigned long __kern_hyp_va(unsigned long v)
98 asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n"
101 "add %0, %0, #0, lsl 12\n"
108 #define kern_hyp_va(v) ((typeof(v))(__kern_hyp_va((unsigned long)(v))))
111 * Obtain the PC-relative address of a kernel symbol
114 * The goal of this macro is to return a symbol's address based on a
115 * PC-relative computation, as opposed to a loading the VA from a
116 * constant pool or something similar. This works well for HYP, as an
117 * absolute VA is guaranteed to be wrong. Only use this if trying to
118 * obtain the address of a symbol (i.e. not something you obtained by
119 * following a pointer).
121 #define hyp_symbol_addr(s) \
124 asm("adrp %0, %1\n" \
125 "add %0, %0, :lo12:%1\n" \
126 : "=r" (addr) : "S" (&s)); \
131 * We currently support using a VM-specified IPA size. For backward
132 * compatibility, the default IPA size is fixed to 40bits.
134 #define KVM_PHYS_SHIFT (40)
136 #define kvm_phys_shift(kvm) VTCR_EL2_IPA(kvm->arch.vtcr)
137 #define kvm_phys_size(kvm) (_AC(1, ULL) << kvm_phys_shift(kvm))
138 #define kvm_phys_mask(kvm) (kvm_phys_size(kvm) - _AC(1, ULL))
140 static inline bool kvm_page_empty(void *ptr)
142 struct page *ptr_page = virt_to_page(ptr);
143 return page_count(ptr_page) == 1;
146 #include <asm/stage2_pgtable.h>
148 int create_hyp_mappings(void *from, void *to, pgprot_t prot);
149 int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
150 void __iomem **kaddr,
151 void __iomem **haddr);
152 int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
154 void free_hyp_pgds(void);
156 void stage2_unmap_vm(struct kvm *kvm);
157 int kvm_alloc_stage2_pgd(struct kvm *kvm);
158 void kvm_free_stage2_pgd(struct kvm *kvm);
159 int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
160 phys_addr_t pa, unsigned long size, bool writable);
162 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
164 void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
166 phys_addr_t kvm_mmu_get_httbr(void);
167 phys_addr_t kvm_get_idmap_vector(void);
168 int kvm_mmu_init(void);
169 void kvm_clear_hyp_idmap(void);
171 #define kvm_mk_pmd(ptep) \
172 __pmd(__phys_to_pmd_val(__pa(ptep)) | PMD_TYPE_TABLE)
173 #define kvm_mk_pud(pmdp) \
174 __pud(__phys_to_pud_val(__pa(pmdp)) | PMD_TYPE_TABLE)
175 #define kvm_mk_p4d(pmdp) \
176 __p4d(__phys_to_p4d_val(__pa(pmdp)) | PUD_TYPE_TABLE)
178 #define kvm_set_pud(pudp, pud) set_pud(pudp, pud)
180 #define kvm_pfn_pte(pfn, prot) pfn_pte(pfn, prot)
181 #define kvm_pfn_pmd(pfn, prot) pfn_pmd(pfn, prot)
182 #define kvm_pfn_pud(pfn, prot) pfn_pud(pfn, prot)
184 #define kvm_pud_pfn(pud) pud_pfn(pud)
186 #define kvm_pmd_mkhuge(pmd) pmd_mkhuge(pmd)
187 #define kvm_pud_mkhuge(pud) pud_mkhuge(pud)
189 static inline pte_t kvm_s2pte_mkwrite(pte_t pte)
191 pte_val(pte) |= PTE_S2_RDWR;
195 static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd)
197 pmd_val(pmd) |= PMD_S2_RDWR;
201 static inline pud_t kvm_s2pud_mkwrite(pud_t pud)
203 pud_val(pud) |= PUD_S2_RDWR;
207 static inline pte_t kvm_s2pte_mkexec(pte_t pte)
209 pte_val(pte) &= ~PTE_S2_XN;
213 static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd)
215 pmd_val(pmd) &= ~PMD_S2_XN;
219 static inline pud_t kvm_s2pud_mkexec(pud_t pud)
221 pud_val(pud) &= ~PUD_S2_XN;
225 static inline void kvm_set_s2pte_readonly(pte_t *ptep)
227 pteval_t old_pteval, pteval;
229 pteval = READ_ONCE(pte_val(*ptep));
232 pteval &= ~PTE_S2_RDWR;
233 pteval |= PTE_S2_RDONLY;
234 pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval);
235 } while (pteval != old_pteval);
238 static inline bool kvm_s2pte_readonly(pte_t *ptep)
240 return (READ_ONCE(pte_val(*ptep)) & PTE_S2_RDWR) == PTE_S2_RDONLY;
243 static inline bool kvm_s2pte_exec(pte_t *ptep)
245 return !(READ_ONCE(pte_val(*ptep)) & PTE_S2_XN);
248 static inline void kvm_set_s2pmd_readonly(pmd_t *pmdp)
250 kvm_set_s2pte_readonly((pte_t *)pmdp);
253 static inline bool kvm_s2pmd_readonly(pmd_t *pmdp)
255 return kvm_s2pte_readonly((pte_t *)pmdp);
258 static inline bool kvm_s2pmd_exec(pmd_t *pmdp)
260 return !(READ_ONCE(pmd_val(*pmdp)) & PMD_S2_XN);
263 static inline void kvm_set_s2pud_readonly(pud_t *pudp)
265 kvm_set_s2pte_readonly((pte_t *)pudp);
268 static inline bool kvm_s2pud_readonly(pud_t *pudp)
270 return kvm_s2pte_readonly((pte_t *)pudp);
273 static inline bool kvm_s2pud_exec(pud_t *pudp)
275 return !(READ_ONCE(pud_val(*pudp)) & PUD_S2_XN);
278 static inline pud_t kvm_s2pud_mkyoung(pud_t pud)
280 return pud_mkyoung(pud);
283 static inline bool kvm_s2pud_young(pud_t pud)
285 return pud_young(pud);
288 #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
290 #ifdef __PAGETABLE_PMD_FOLDED
291 #define hyp_pmd_table_empty(pmdp) (0)
293 #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
296 #ifdef __PAGETABLE_PUD_FOLDED
297 #define hyp_pud_table_empty(pudp) (0)
299 #define hyp_pud_table_empty(pudp) kvm_page_empty(pudp)
302 #ifdef __PAGETABLE_P4D_FOLDED
303 #define hyp_p4d_table_empty(p4dp) (0)
305 #define hyp_p4d_table_empty(p4dp) kvm_page_empty(p4dp)
310 #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l))
312 static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
314 return (vcpu_read_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101;
317 static inline void __clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
319 void *va = page_address(pfn_to_page(pfn));
322 * With FWB, we ensure that the guest always accesses memory using
323 * cacheable attributes, and we don't have to clean to PoC when
324 * faulting in pages. Furthermore, FWB implies IDC, so cleaning to
325 * PoU is not required either in this case.
327 if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
330 kvm_flush_dcache_to_poc(va, size);
333 static inline void __invalidate_icache_guest_page(kvm_pfn_t pfn,
336 if (icache_is_aliasing()) {
337 /* any kind of VIPT cache */
338 __flush_icache_all();
339 } else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) {
340 /* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */
341 void *va = page_address(pfn_to_page(pfn));
343 invalidate_icache_range((unsigned long)va,
344 (unsigned long)va + size);
348 static inline void __kvm_flush_dcache_pte(pte_t pte)
350 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
351 struct page *page = pte_page(pte);
352 kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE);
356 static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
358 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
359 struct page *page = pmd_page(pmd);
360 kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE);
364 static inline void __kvm_flush_dcache_pud(pud_t pud)
366 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
367 struct page *page = pud_page(pud);
368 kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE);
372 void kvm_set_way_flush(struct kvm_vcpu *vcpu);
373 void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
375 static inline bool __kvm_cpu_uses_extended_idmap(void)
377 return __cpu_uses_extended_idmap_level();
380 static inline unsigned long __kvm_idmap_ptrs_per_pgd(void)
382 return idmap_ptrs_per_pgd;
386 * Can't use pgd_populate here, because the extended idmap adds an extra level
387 * above CONFIG_PGTABLE_LEVELS (which is 2 or 3 if we're using the extended
388 * idmap), and pgd_populate is only available if CONFIG_PGTABLE_LEVELS = 4.
390 static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
392 pgd_t *merged_hyp_pgd,
393 unsigned long hyp_idmap_start)
399 * Use the first entry to access the HYP mappings. It is
400 * guaranteed to be free, otherwise we wouldn't use an
403 VM_BUG_ON(pgd_val(merged_hyp_pgd[0]));
404 pgd_addr = __phys_to_pgd_val(__pa(hyp_pgd));
405 merged_hyp_pgd[0] = __pgd(pgd_addr | PMD_TYPE_TABLE);
408 * Create another extended level entry that points to the boot HYP map,
409 * which contains an ID mapping of the HYP init code. We essentially
410 * merge the boot and runtime HYP maps by doing so, but they don't
411 * overlap anyway, so this is fine.
413 idmap_idx = hyp_idmap_start >> VA_BITS;
414 VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx]));
415 pgd_addr = __phys_to_pgd_val(__pa(boot_hyp_pgd));
416 merged_hyp_pgd[idmap_idx] = __pgd(pgd_addr | PMD_TYPE_TABLE);
419 static inline unsigned int kvm_get_vmid_bits(void)
421 int reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
423 return get_vmid_bits(reg);
427 * We are not in the kvm->srcu critical section most of the time, so we take
428 * the SRCU read lock here. Since we copy the data from the user page, we
429 * can immediately drop the lock again.
431 static inline int kvm_read_guest_lock(struct kvm *kvm,
432 gpa_t gpa, void *data, unsigned long len)
434 int srcu_idx = srcu_read_lock(&kvm->srcu);
435 int ret = kvm_read_guest(kvm, gpa, data, len);
437 srcu_read_unlock(&kvm->srcu, srcu_idx);
442 static inline int kvm_write_guest_lock(struct kvm *kvm, gpa_t gpa,
443 const void *data, unsigned long len)
445 int srcu_idx = srcu_read_lock(&kvm->srcu);
446 int ret = kvm_write_guest(kvm, gpa, data, len);
448 srcu_read_unlock(&kvm->srcu, srcu_idx);
453 #ifdef CONFIG_KVM_INDIRECT_VECTORS
455 * EL2 vectors can be mapped and rerouted in a number of ways,
456 * depending on the kernel configuration and CPU present:
458 * - If the CPU has the ARM64_HARDEN_BRANCH_PREDICTOR cap, the
459 * hardening sequence is placed in one of the vector slots, which is
460 * executed before jumping to the real vectors.
462 * - If the CPU has both the ARM64_HARDEN_EL2_VECTORS cap and the
463 * ARM64_HARDEN_BRANCH_PREDICTOR cap, the slot containing the
464 * hardening sequence is mapped next to the idmap page, and executed
465 * before jumping to the real vectors.
467 * - If the CPU only has the ARM64_HARDEN_EL2_VECTORS cap, then an
468 * empty slot is selected, mapped next to the idmap page, and
469 * executed before jumping to the real vectors.
471 * Note that ARM64_HARDEN_EL2_VECTORS is somewhat incompatible with
472 * VHE, as we don't have hypervisor-specific mappings. If the system
473 * is VHE and yet selects this capability, it will be ignored.
477 extern void *__kvm_bp_vect_base;
478 extern int __kvm_harden_el2_vector_slot;
480 /* This is called on both VHE and !VHE systems */
481 static inline void *kvm_get_hyp_vector(void)
483 struct bp_hardening_data *data = arm64_get_bp_hardening_data();
484 void *vect = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
487 if (cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR) && data->fn) {
488 vect = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
489 slot = data->hyp_vectors_slot;
492 if (this_cpu_has_cap(ARM64_HARDEN_EL2_VECTORS) && !has_vhe()) {
493 vect = __kvm_bp_vect_base;
495 slot = __kvm_harden_el2_vector_slot;
499 vect += slot * SZ_2K;
504 /* This is only called on a !VHE system */
505 static inline int kvm_map_vectors(void)
508 * HBP = ARM64_HARDEN_BRANCH_PREDICTOR
509 * HEL2 = ARM64_HARDEN_EL2_VECTORS
511 * !HBP + !HEL2 -> use direct vectors
512 * HBP + !HEL2 -> use hardened vectors in place
513 * !HBP + HEL2 -> allocate one vector slot and use exec mapping
514 * HBP + HEL2 -> use hardened vertors and use exec mapping
516 if (cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR)) {
517 __kvm_bp_vect_base = kvm_ksym_ref(__bp_harden_hyp_vecs);
518 __kvm_bp_vect_base = kern_hyp_va(__kvm_bp_vect_base);
521 if (cpus_have_const_cap(ARM64_HARDEN_EL2_VECTORS)) {
522 phys_addr_t vect_pa = __pa_symbol(__bp_harden_hyp_vecs);
523 unsigned long size = __BP_HARDEN_HYP_VECS_SZ;
526 * Always allocate a spare vector slot, as we don't
527 * know yet which CPUs have a BP hardening slot that
530 __kvm_harden_el2_vector_slot = atomic_inc_return(&arm64_el2_vector_last_slot);
531 BUG_ON(__kvm_harden_el2_vector_slot >= BP_HARDEN_EL2_SLOTS);
532 return create_hyp_exec_mappings(vect_pa, size,
533 &__kvm_bp_vect_base);
539 static inline void *kvm_get_hyp_vector(void)
541 return kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
544 static inline int kvm_map_vectors(void)
550 #ifdef CONFIG_ARM64_SSBD
551 DECLARE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
553 static inline int hyp_map_aux_data(void)
557 for_each_possible_cpu(cpu) {
560 ptr = per_cpu_ptr(&arm64_ssbd_callback_required, cpu);
561 err = create_hyp_mappings(ptr, ptr + 1, PAGE_HYP);
568 static inline int hyp_map_aux_data(void)
574 #define kvm_phys_to_vttbr(addr) phys_to_ttbr(addr)
577 * Get the magic number 'x' for VTTBR:BADDR of this KVM instance.
578 * With v8.2 LVA extensions, 'x' should be a minimum of 6 with
581 static inline int arm64_vttbr_x(u32 ipa_shift, u32 levels)
583 int x = ARM64_VTTBR_X(ipa_shift, levels);
585 return (IS_ENABLED(CONFIG_ARM64_PA_BITS_52) && x < 6) ? 6 : x;
588 static inline u64 vttbr_baddr_mask(u32 ipa_shift, u32 levels)
590 unsigned int x = arm64_vttbr_x(ipa_shift, levels);
592 return GENMASK_ULL(PHYS_MASK_SHIFT - 1, x);
595 static inline u64 kvm_vttbr_baddr_mask(struct kvm *kvm)
597 return vttbr_baddr_mask(kvm_phys_shift(kvm), kvm_stage2_levels(kvm));
600 static __always_inline u64 kvm_get_vttbr(struct kvm *kvm)
602 struct kvm_vmid *vmid = &kvm->arch.vmid;
603 u64 vmid_field, baddr;
604 u64 cnp = system_supports_cnp() ? VTTBR_CNP_BIT : 0;
606 baddr = kvm->arch.pgd_phys;
607 vmid_field = (u64)vmid->vmid << VTTBR_VMID_SHIFT;
608 return kvm_phys_to_vttbr(baddr) | vmid_field | cnp;
612 * Must be called from hyp code running at EL2 with an updated VTTBR
613 * and interrupts disabled.
615 static __always_inline void __load_guest_stage2(struct kvm *kvm)
617 write_sysreg(kvm->arch.vtcr, vtcr_el2);
618 write_sysreg(kvm_get_vttbr(kvm), vttbr_el2);
621 * ARM errata 1165522 and 1530923 require the actual execution of the
622 * above before we can switch to the EL1/EL0 translation regime used by
625 asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT));
628 #endif /* __ASSEMBLY__ */
629 #endif /* __ARM64_KVM_MMU_H__ */