18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2012,2013 - ARM Ltd 48c2ecf20Sopenharmony_ci * Author: Marc Zyngier <marc.zyngier@arm.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#ifndef __ARM64_KVM_MMU_H__ 88c2ecf20Sopenharmony_ci#define __ARM64_KVM_MMU_H__ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <asm/page.h> 118c2ecf20Sopenharmony_ci#include <asm/memory.h> 128c2ecf20Sopenharmony_ci#include <asm/mmu.h> 138c2ecf20Sopenharmony_ci#include <asm/cpufeature.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* 168c2ecf20Sopenharmony_ci * As ARMv8.0 only has the TTBR0_EL2 register, we cannot express 178c2ecf20Sopenharmony_ci * "negative" addresses. This makes it impossible to directly share 188c2ecf20Sopenharmony_ci * mappings with the kernel. 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * Instead, give the HYP mode its own VA region at a fixed offset from 218c2ecf20Sopenharmony_ci * the kernel by just masking the top bits (which are all ones for a 228c2ecf20Sopenharmony_ci * kernel address). We need to find out how many bits to mask. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * We want to build a set of page tables that cover both parts of the 258c2ecf20Sopenharmony_ci * idmap (the trampoline page used to initialize EL2), and our normal 268c2ecf20Sopenharmony_ci * runtime VA space, at the same time. 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * Given that the kernel uses VA_BITS for its entire address space, 298c2ecf20Sopenharmony_ci * and that half of that space (VA_BITS - 1) is used for the linear 308c2ecf20Sopenharmony_ci * mapping, we can also limit the EL2 space to (VA_BITS - 1). 318c2ecf20Sopenharmony_ci * 328c2ecf20Sopenharmony_ci * The main question is "Within the VA_BITS space, does EL2 use the 338c2ecf20Sopenharmony_ci * top or the bottom half of that space to shadow the kernel's linear 348c2ecf20Sopenharmony_ci * mapping?". As we need to idmap the trampoline page, this is 358c2ecf20Sopenharmony_ci * determined by the range in which this page lives. 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * If the page is in the bottom half, we have to use the top half. If 388c2ecf20Sopenharmony_ci * the page is in the top half, we have to use the bottom half: 398c2ecf20Sopenharmony_ci * 408c2ecf20Sopenharmony_ci * T = __pa_symbol(__hyp_idmap_text_start) 418c2ecf20Sopenharmony_ci * if (T & BIT(VA_BITS - 1)) 428c2ecf20Sopenharmony_ci * HYP_VA_MIN = 0 //idmap in upper half 438c2ecf20Sopenharmony_ci * else 448c2ecf20Sopenharmony_ci * HYP_VA_MIN = 1 << (VA_BITS - 1) 458c2ecf20Sopenharmony_ci * HYP_VA_MAX = HYP_VA_MIN + (1 << (VA_BITS - 1)) - 1 468c2ecf20Sopenharmony_ci * 478c2ecf20Sopenharmony_ci * When using VHE, there are no separate hyp mappings and all KVM 488c2ecf20Sopenharmony_ci * functionality is already mapped as part of the main kernel 498c2ecf20Sopenharmony_ci * mappings, and none of this applies in that case. 508c2ecf20Sopenharmony_ci */ 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#ifdef __ASSEMBLY__ 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#include <asm/alternative.h> 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* 578c2ecf20Sopenharmony_ci * Convert a kernel VA into a HYP VA. 588c2ecf20Sopenharmony_ci * reg: VA to be converted. 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci * The actual code generation takes place in kvm_update_va_mask, and 618c2ecf20Sopenharmony_ci * the instructions below are only there to reserve the space and 628c2ecf20Sopenharmony_ci * perform the register allocation (kvm_update_va_mask uses the 638c2ecf20Sopenharmony_ci * specific registers encoded in the instructions). 648c2ecf20Sopenharmony_ci */ 658c2ecf20Sopenharmony_ci.macro kern_hyp_va reg 668c2ecf20Sopenharmony_cialternative_cb kvm_update_va_mask 678c2ecf20Sopenharmony_ci and \reg, \reg, #1 /* mask with va_mask */ 688c2ecf20Sopenharmony_ci ror \reg, \reg, #1 /* rotate to the first tag bit */ 698c2ecf20Sopenharmony_ci add \reg, \reg, #0 /* insert the low 12 bits of the tag */ 708c2ecf20Sopenharmony_ci add \reg, \reg, #0, lsl 12 /* insert the top 12 bits of the tag */ 718c2ecf20Sopenharmony_ci ror \reg, \reg, #63 /* rotate back */ 728c2ecf20Sopenharmony_cialternative_cb_end 738c2ecf20Sopenharmony_ci.endm 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci#else 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci#include <linux/pgtable.h> 788c2ecf20Sopenharmony_ci#include <asm/pgalloc.h> 798c2ecf20Sopenharmony_ci#include <asm/cache.h> 808c2ecf20Sopenharmony_ci#include <asm/cacheflush.h> 818c2ecf20Sopenharmony_ci#include <asm/mmu_context.h> 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_civoid kvm_update_va_mask(struct alt_instr *alt, 848c2ecf20Sopenharmony_ci __le32 *origptr, __le32 *updptr, int nr_inst); 858c2ecf20Sopenharmony_civoid kvm_compute_layout(void); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic __always_inline unsigned long __kern_hyp_va(unsigned long v) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n" 908c2ecf20Sopenharmony_ci "ror %0, %0, #1\n" 918c2ecf20Sopenharmony_ci "add %0, %0, #0\n" 928c2ecf20Sopenharmony_ci "add %0, %0, #0, lsl 12\n" 938c2ecf20Sopenharmony_ci "ror %0, %0, #63\n", 948c2ecf20Sopenharmony_ci kvm_update_va_mask) 958c2ecf20Sopenharmony_ci : "+r" (v)); 968c2ecf20Sopenharmony_ci return v; 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci#define kern_hyp_va(v) ((typeof(v))(__kern_hyp_va((unsigned long)(v)))) 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci/* 1028c2ecf20Sopenharmony_ci * We currently support using a VM-specified IPA size. For backward 1038c2ecf20Sopenharmony_ci * compatibility, the default IPA size is fixed to 40bits. 1048c2ecf20Sopenharmony_ci */ 1058c2ecf20Sopenharmony_ci#define KVM_PHYS_SHIFT (40) 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci#define kvm_phys_shift(kvm) VTCR_EL2_IPA(kvm->arch.vtcr) 1088c2ecf20Sopenharmony_ci#define kvm_phys_size(kvm) (_AC(1, ULL) << kvm_phys_shift(kvm)) 1098c2ecf20Sopenharmony_ci#define kvm_phys_mask(kvm) (kvm_phys_size(kvm) - _AC(1, ULL)) 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci#include <asm/kvm_pgtable.h> 1128c2ecf20Sopenharmony_ci#include <asm/stage2_pgtable.h> 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ciint create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot); 1158c2ecf20Sopenharmony_ciint create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, 1168c2ecf20Sopenharmony_ci void __iomem **kaddr, 1178c2ecf20Sopenharmony_ci void __iomem **haddr); 1188c2ecf20Sopenharmony_ciint create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, 1198c2ecf20Sopenharmony_ci void **haddr); 1208c2ecf20Sopenharmony_civoid free_hyp_pgds(void); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_civoid stage2_unmap_vm(struct kvm *kvm); 1238c2ecf20Sopenharmony_ciint kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu); 1248c2ecf20Sopenharmony_civoid kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu); 1258c2ecf20Sopenharmony_ciint kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, 1268c2ecf20Sopenharmony_ci phys_addr_t pa, unsigned long size, bool writable); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ciint kvm_handle_guest_abort(struct kvm_vcpu *vcpu); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ciphys_addr_t kvm_mmu_get_httbr(void); 1318c2ecf20Sopenharmony_ciphys_addr_t kvm_get_idmap_vector(void); 1328c2ecf20Sopenharmony_ciint kvm_mmu_init(void); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistruct kvm; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci#define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l)) 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci return (vcpu_read_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101; 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic inline void __clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci void *va = page_address(pfn_to_page(pfn)); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci /* 1488c2ecf20Sopenharmony_ci * With FWB, we ensure that the guest always accesses memory using 1498c2ecf20Sopenharmony_ci * cacheable attributes, and we don't have to clean to PoC when 1508c2ecf20Sopenharmony_ci * faulting in pages. Furthermore, FWB implies IDC, so cleaning to 1518c2ecf20Sopenharmony_ci * PoU is not required either in this case. 1528c2ecf20Sopenharmony_ci */ 1538c2ecf20Sopenharmony_ci if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) 1548c2ecf20Sopenharmony_ci return; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci kvm_flush_dcache_to_poc(va, size); 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic inline void __invalidate_icache_guest_page(kvm_pfn_t pfn, 1608c2ecf20Sopenharmony_ci unsigned long size) 1618c2ecf20Sopenharmony_ci{ 1628c2ecf20Sopenharmony_ci if (icache_is_aliasing()) { 1638c2ecf20Sopenharmony_ci /* any kind of VIPT cache */ 1648c2ecf20Sopenharmony_ci __flush_icache_all(); 1658c2ecf20Sopenharmony_ci } else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) { 1668c2ecf20Sopenharmony_ci /* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */ 1678c2ecf20Sopenharmony_ci void *va = page_address(pfn_to_page(pfn)); 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci invalidate_icache_range((unsigned long)va, 1708c2ecf20Sopenharmony_ci (unsigned long)va + size); 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_civoid kvm_set_way_flush(struct kvm_vcpu *vcpu); 1758c2ecf20Sopenharmony_civoid kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_cistatic inline unsigned int kvm_get_vmid_bits(void) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci int reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci return get_vmid_bits(reg); 1828c2ecf20Sopenharmony_ci} 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci/* 1858c2ecf20Sopenharmony_ci * We are not in the kvm->srcu critical section most of the time, so we take 1868c2ecf20Sopenharmony_ci * the SRCU read lock here. Since we copy the data from the user page, we 1878c2ecf20Sopenharmony_ci * can immediately drop the lock again. 1888c2ecf20Sopenharmony_ci */ 1898c2ecf20Sopenharmony_cistatic inline int kvm_read_guest_lock(struct kvm *kvm, 1908c2ecf20Sopenharmony_ci gpa_t gpa, void *data, unsigned long len) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci int srcu_idx = srcu_read_lock(&kvm->srcu); 1938c2ecf20Sopenharmony_ci int ret = kvm_read_guest(kvm, gpa, data, len); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci srcu_read_unlock(&kvm->srcu, srcu_idx); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci return ret; 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_cistatic inline int kvm_write_guest_lock(struct kvm *kvm, gpa_t gpa, 2018c2ecf20Sopenharmony_ci const void *data, unsigned long len) 2028c2ecf20Sopenharmony_ci{ 2038c2ecf20Sopenharmony_ci int srcu_idx = srcu_read_lock(&kvm->srcu); 2048c2ecf20Sopenharmony_ci int ret = kvm_write_guest(kvm, gpa, data, len); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci srcu_read_unlock(&kvm->srcu, srcu_idx); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci return ret; 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci/* 2128c2ecf20Sopenharmony_ci * EL2 vectors can be mapped and rerouted in a number of ways, 2138c2ecf20Sopenharmony_ci * depending on the kernel configuration and CPU present: 2148c2ecf20Sopenharmony_ci * 2158c2ecf20Sopenharmony_ci * - If the CPU is affected by Spectre-v2, the hardening sequence is 2168c2ecf20Sopenharmony_ci * placed in one of the vector slots, which is executed before jumping 2178c2ecf20Sopenharmony_ci * to the real vectors. 2188c2ecf20Sopenharmony_ci * 2198c2ecf20Sopenharmony_ci * - If the CPU also has the ARM64_HARDEN_EL2_VECTORS cap, the slot 2208c2ecf20Sopenharmony_ci * containing the hardening sequence is mapped next to the idmap page, 2218c2ecf20Sopenharmony_ci * and executed before jumping to the real vectors. 2228c2ecf20Sopenharmony_ci * 2238c2ecf20Sopenharmony_ci * - If the CPU only has the ARM64_HARDEN_EL2_VECTORS cap, then an 2248c2ecf20Sopenharmony_ci * empty slot is selected, mapped next to the idmap page, and 2258c2ecf20Sopenharmony_ci * executed before jumping to the real vectors. 2268c2ecf20Sopenharmony_ci * 2278c2ecf20Sopenharmony_ci * Note that ARM64_HARDEN_EL2_VECTORS is somewhat incompatible with 2288c2ecf20Sopenharmony_ci * VHE, as we don't have hypervisor-specific mappings. If the system 2298c2ecf20Sopenharmony_ci * is VHE and yet selects this capability, it will be ignored. 2308c2ecf20Sopenharmony_ci */ 2318c2ecf20Sopenharmony_ciextern void *__kvm_bp_vect_base; 2328c2ecf20Sopenharmony_ciextern int __kvm_harden_el2_vector_slot; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cistatic inline void *kvm_get_hyp_vector(void) 2358c2ecf20Sopenharmony_ci{ 2368c2ecf20Sopenharmony_ci struct bp_hardening_data *data = arm64_get_bp_hardening_data(); 2378c2ecf20Sopenharmony_ci void *vect = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 2388c2ecf20Sopenharmony_ci int slot = -1; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci if ((cpus_have_const_cap(ARM64_SPECTRE_V2) || 2418c2ecf20Sopenharmony_ci cpus_have_const_cap(ARM64_SPECTRE_BHB)) && data->template_start) { 2428c2ecf20Sopenharmony_ci vect = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs)); 2438c2ecf20Sopenharmony_ci slot = data->hyp_vectors_slot; 2448c2ecf20Sopenharmony_ci } 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci if (this_cpu_has_cap(ARM64_HARDEN_EL2_VECTORS) && !has_vhe()) { 2478c2ecf20Sopenharmony_ci vect = __kvm_bp_vect_base; 2488c2ecf20Sopenharmony_ci if (slot == -1) 2498c2ecf20Sopenharmony_ci slot = __kvm_harden_el2_vector_slot; 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci if (slot != -1) 2538c2ecf20Sopenharmony_ci vect += slot * SZ_2K; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci return vect; 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci#define kvm_phys_to_vttbr(addr) phys_to_ttbr(addr) 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_cistatic __always_inline u64 kvm_get_vttbr(struct kvm_s2_mmu *mmu) 2618c2ecf20Sopenharmony_ci{ 2628c2ecf20Sopenharmony_ci struct kvm_vmid *vmid = &mmu->vmid; 2638c2ecf20Sopenharmony_ci u64 vmid_field, baddr; 2648c2ecf20Sopenharmony_ci u64 cnp = system_supports_cnp() ? VTTBR_CNP_BIT : 0; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci baddr = mmu->pgd_phys; 2678c2ecf20Sopenharmony_ci vmid_field = (u64)vmid->vmid << VTTBR_VMID_SHIFT; 2688c2ecf20Sopenharmony_ci return kvm_phys_to_vttbr(baddr) | vmid_field | cnp; 2698c2ecf20Sopenharmony_ci} 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci/* 2728c2ecf20Sopenharmony_ci * Must be called from hyp code running at EL2 with an updated VTTBR 2738c2ecf20Sopenharmony_ci * and interrupts disabled. 2748c2ecf20Sopenharmony_ci */ 2758c2ecf20Sopenharmony_cistatic __always_inline void __load_guest_stage2(struct kvm_s2_mmu *mmu) 2768c2ecf20Sopenharmony_ci{ 2778c2ecf20Sopenharmony_ci write_sysreg(kern_hyp_va(mmu->kvm)->arch.vtcr, vtcr_el2); 2788c2ecf20Sopenharmony_ci write_sysreg(kvm_get_vttbr(mmu), vttbr_el2); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci /* 2818c2ecf20Sopenharmony_ci * ARM errata 1165522 and 1530923 require the actual execution of the 2828c2ecf20Sopenharmony_ci * above before we can switch to the EL1/EL0 translation regime used by 2838c2ecf20Sopenharmony_ci * the guest. 2848c2ecf20Sopenharmony_ci */ 2858c2ecf20Sopenharmony_ci asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); 2868c2ecf20Sopenharmony_ci} 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */ 2898c2ecf20Sopenharmony_ci#endif /* __ARM64_KVM_MMU_H__ */ 290