162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2012,2013 - ARM Ltd
462306a36Sopenharmony_ci * Author: Marc Zyngier <marc.zyngier@arm.com>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef __ARM64_KVM_MMU_H__
862306a36Sopenharmony_ci#define __ARM64_KVM_MMU_H__
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <asm/page.h>
1162306a36Sopenharmony_ci#include <asm/memory.h>
1262306a36Sopenharmony_ci#include <asm/mmu.h>
1362306a36Sopenharmony_ci#include <asm/cpufeature.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * As ARMv8.0 only has the TTBR0_EL2 register, we cannot express
1762306a36Sopenharmony_ci * "negative" addresses. This makes it impossible to directly share
1862306a36Sopenharmony_ci * mappings with the kernel.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * Instead, give the HYP mode its own VA region at a fixed offset from
2162306a36Sopenharmony_ci * the kernel by just masking the top bits (which are all ones for a
2262306a36Sopenharmony_ci * kernel address). We need to find out how many bits to mask.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * We want to build a set of page tables that cover both parts of the
2562306a36Sopenharmony_ci * idmap (the trampoline page used to initialize EL2), and our normal
2662306a36Sopenharmony_ci * runtime VA space, at the same time.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * Given that the kernel uses VA_BITS for its entire address space,
2962306a36Sopenharmony_ci * and that half of that space (VA_BITS - 1) is used for the linear
3062306a36Sopenharmony_ci * mapping, we can also limit the EL2 space to (VA_BITS - 1).
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * The main question is "Within the VA_BITS space, does EL2 use the
3362306a36Sopenharmony_ci * top or the bottom half of that space to shadow the kernel's linear
3462306a36Sopenharmony_ci * mapping?". As we need to idmap the trampoline page, this is
3562306a36Sopenharmony_ci * determined by the range in which this page lives.
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * If the page is in the bottom half, we have to use the top half. If
3862306a36Sopenharmony_ci * the page is in the top half, we have to use the bottom half:
3962306a36Sopenharmony_ci *
4062306a36Sopenharmony_ci * T = __pa_symbol(__hyp_idmap_text_start)
4162306a36Sopenharmony_ci * if (T & BIT(VA_BITS - 1))
4262306a36Sopenharmony_ci *	HYP_VA_MIN = 0  //idmap in upper half
4362306a36Sopenharmony_ci * else
4462306a36Sopenharmony_ci *	HYP_VA_MIN = 1 << (VA_BITS - 1)
4562306a36Sopenharmony_ci * HYP_VA_MAX = HYP_VA_MIN + (1 << (VA_BITS - 1)) - 1
4662306a36Sopenharmony_ci *
4762306a36Sopenharmony_ci * When using VHE, there are no separate hyp mappings and all KVM
4862306a36Sopenharmony_ci * functionality is already mapped as part of the main kernel
4962306a36Sopenharmony_ci * mappings, and none of this applies in that case.
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#ifdef __ASSEMBLY__
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#include <asm/alternative.h>
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/*
5762306a36Sopenharmony_ci * Convert a kernel VA into a HYP VA.
5862306a36Sopenharmony_ci * reg: VA to be converted.
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci * The actual code generation takes place in kvm_update_va_mask, and
6162306a36Sopenharmony_ci * the instructions below are only there to reserve the space and
6262306a36Sopenharmony_ci * perform the register allocation (kvm_update_va_mask uses the
6362306a36Sopenharmony_ci * specific registers encoded in the instructions).
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_ci.macro kern_hyp_va	reg
6662306a36Sopenharmony_ci#ifndef __KVM_VHE_HYPERVISOR__
6762306a36Sopenharmony_cialternative_cb ARM64_ALWAYS_SYSTEM, kvm_update_va_mask
6862306a36Sopenharmony_ci	and     \reg, \reg, #1		/* mask with va_mask */
6962306a36Sopenharmony_ci	ror	\reg, \reg, #1		/* rotate to the first tag bit */
7062306a36Sopenharmony_ci	add	\reg, \reg, #0		/* insert the low 12 bits of the tag */
7162306a36Sopenharmony_ci	add	\reg, \reg, #0, lsl 12	/* insert the top 12 bits of the tag */
7262306a36Sopenharmony_ci	ror	\reg, \reg, #63		/* rotate back */
7362306a36Sopenharmony_cialternative_cb_end
7462306a36Sopenharmony_ci#endif
7562306a36Sopenharmony_ci.endm
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/*
7862306a36Sopenharmony_ci * Convert a hypervisor VA to a PA
7962306a36Sopenharmony_ci * reg: hypervisor address to be converted in place
8062306a36Sopenharmony_ci * tmp: temporary register
8162306a36Sopenharmony_ci */
8262306a36Sopenharmony_ci.macro hyp_pa reg, tmp
8362306a36Sopenharmony_ci	ldr_l	\tmp, hyp_physvirt_offset
8462306a36Sopenharmony_ci	add	\reg, \reg, \tmp
8562306a36Sopenharmony_ci.endm
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci/*
8862306a36Sopenharmony_ci * Convert a hypervisor VA to a kernel image address
8962306a36Sopenharmony_ci * reg: hypervisor address to be converted in place
9062306a36Sopenharmony_ci * tmp: temporary register
9162306a36Sopenharmony_ci *
9262306a36Sopenharmony_ci * The actual code generation takes place in kvm_get_kimage_voffset, and
9362306a36Sopenharmony_ci * the instructions below are only there to reserve the space and
9462306a36Sopenharmony_ci * perform the register allocation (kvm_get_kimage_voffset uses the
9562306a36Sopenharmony_ci * specific registers encoded in the instructions).
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ci.macro hyp_kimg_va reg, tmp
9862306a36Sopenharmony_ci	/* Convert hyp VA -> PA. */
9962306a36Sopenharmony_ci	hyp_pa	\reg, \tmp
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/* Load kimage_voffset. */
10262306a36Sopenharmony_cialternative_cb ARM64_ALWAYS_SYSTEM, kvm_get_kimage_voffset
10362306a36Sopenharmony_ci	movz	\tmp, #0
10462306a36Sopenharmony_ci	movk	\tmp, #0, lsl #16
10562306a36Sopenharmony_ci	movk	\tmp, #0, lsl #32
10662306a36Sopenharmony_ci	movk	\tmp, #0, lsl #48
10762306a36Sopenharmony_cialternative_cb_end
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	/* Convert PA -> kimg VA. */
11062306a36Sopenharmony_ci	add	\reg, \reg, \tmp
11162306a36Sopenharmony_ci.endm
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci#else
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#include <linux/pgtable.h>
11662306a36Sopenharmony_ci#include <asm/pgalloc.h>
11762306a36Sopenharmony_ci#include <asm/cache.h>
11862306a36Sopenharmony_ci#include <asm/cacheflush.h>
11962306a36Sopenharmony_ci#include <asm/mmu_context.h>
12062306a36Sopenharmony_ci#include <asm/kvm_emulate.h>
12162306a36Sopenharmony_ci#include <asm/kvm_host.h>
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_civoid kvm_update_va_mask(struct alt_instr *alt,
12462306a36Sopenharmony_ci			__le32 *origptr, __le32 *updptr, int nr_inst);
12562306a36Sopenharmony_civoid kvm_compute_layout(void);
12662306a36Sopenharmony_civoid kvm_apply_hyp_relocations(void);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci#define __hyp_pa(x) (((phys_addr_t)(x)) + hyp_physvirt_offset)
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic __always_inline unsigned long __kern_hyp_va(unsigned long v)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci#ifndef __KVM_VHE_HYPERVISOR__
13362306a36Sopenharmony_ci	asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n"
13462306a36Sopenharmony_ci				    "ror %0, %0, #1\n"
13562306a36Sopenharmony_ci				    "add %0, %0, #0\n"
13662306a36Sopenharmony_ci				    "add %0, %0, #0, lsl 12\n"
13762306a36Sopenharmony_ci				    "ror %0, %0, #63\n",
13862306a36Sopenharmony_ci				    ARM64_ALWAYS_SYSTEM,
13962306a36Sopenharmony_ci				    kvm_update_va_mask)
14062306a36Sopenharmony_ci		     : "+r" (v));
14162306a36Sopenharmony_ci#endif
14262306a36Sopenharmony_ci	return v;
14362306a36Sopenharmony_ci}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci#define kern_hyp_va(v) 	((typeof(v))(__kern_hyp_va((unsigned long)(v))))
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci/*
14862306a36Sopenharmony_ci * We currently support using a VM-specified IPA size. For backward
14962306a36Sopenharmony_ci * compatibility, the default IPA size is fixed to 40bits.
15062306a36Sopenharmony_ci */
15162306a36Sopenharmony_ci#define KVM_PHYS_SHIFT	(40)
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci#define kvm_phys_shift(kvm)		VTCR_EL2_IPA(kvm->arch.vtcr)
15462306a36Sopenharmony_ci#define kvm_phys_size(kvm)		(_AC(1, ULL) << kvm_phys_shift(kvm))
15562306a36Sopenharmony_ci#define kvm_phys_mask(kvm)		(kvm_phys_size(kvm) - _AC(1, ULL))
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci#include <asm/kvm_pgtable.h>
15862306a36Sopenharmony_ci#include <asm/stage2_pgtable.h>
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciint kvm_share_hyp(void *from, void *to);
16162306a36Sopenharmony_civoid kvm_unshare_hyp(void *from, void *to);
16262306a36Sopenharmony_ciint create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot);
16362306a36Sopenharmony_ciint __create_hyp_mappings(unsigned long start, unsigned long size,
16462306a36Sopenharmony_ci			  unsigned long phys, enum kvm_pgtable_prot prot);
16562306a36Sopenharmony_ciint hyp_alloc_private_va_range(size_t size, unsigned long *haddr);
16662306a36Sopenharmony_ciint create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
16762306a36Sopenharmony_ci			   void __iomem **kaddr,
16862306a36Sopenharmony_ci			   void __iomem **haddr);
16962306a36Sopenharmony_ciint create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
17062306a36Sopenharmony_ci			     void **haddr);
17162306a36Sopenharmony_ciint create_hyp_stack(phys_addr_t phys_addr, unsigned long *haddr);
17262306a36Sopenharmony_civoid __init free_hyp_pgds(void);
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_civoid stage2_unmap_vm(struct kvm *kvm);
17562306a36Sopenharmony_ciint kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type);
17662306a36Sopenharmony_civoid kvm_uninit_stage2_mmu(struct kvm *kvm);
17762306a36Sopenharmony_civoid kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu);
17862306a36Sopenharmony_ciint kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
17962306a36Sopenharmony_ci			  phys_addr_t pa, unsigned long size, bool writable);
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ciint kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ciphys_addr_t kvm_mmu_get_httbr(void);
18462306a36Sopenharmony_ciphys_addr_t kvm_get_idmap_vector(void);
18562306a36Sopenharmony_ciint __init kvm_mmu_init(u32 *hyp_va_bits);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_cistatic inline void *__kvm_vector_slot2addr(void *base,
18862306a36Sopenharmony_ci					   enum arm64_hyp_spectre_vector slot)
18962306a36Sopenharmony_ci{
19062306a36Sopenharmony_ci	int idx = slot - (slot != HYP_VECTOR_DIRECT);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	return base + (idx * SZ_2K);
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistruct kvm;
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci#define kvm_flush_dcache_to_poc(a,l)	\
19862306a36Sopenharmony_ci	dcache_clean_inval_poc((unsigned long)(a), (unsigned long)(a)+(l))
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistatic inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci	u64 cache_bits = SCTLR_ELx_M | SCTLR_ELx_C;
20362306a36Sopenharmony_ci	int reg;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	if (vcpu_is_el2(vcpu))
20662306a36Sopenharmony_ci		reg = SCTLR_EL2;
20762306a36Sopenharmony_ci	else
20862306a36Sopenharmony_ci		reg = SCTLR_EL1;
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	return (vcpu_read_sys_reg(vcpu, reg) & cache_bits) == cache_bits;
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic inline void __clean_dcache_guest_page(void *va, size_t size)
21462306a36Sopenharmony_ci{
21562306a36Sopenharmony_ci	/*
21662306a36Sopenharmony_ci	 * With FWB, we ensure that the guest always accesses memory using
21762306a36Sopenharmony_ci	 * cacheable attributes, and we don't have to clean to PoC when
21862306a36Sopenharmony_ci	 * faulting in pages. Furthermore, FWB implies IDC, so cleaning to
21962306a36Sopenharmony_ci	 * PoU is not required either in this case.
22062306a36Sopenharmony_ci	 */
22162306a36Sopenharmony_ci	if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
22262306a36Sopenharmony_ci		return;
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	kvm_flush_dcache_to_poc(va, size);
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic inline void __invalidate_icache_guest_page(void *va, size_t size)
22862306a36Sopenharmony_ci{
22962306a36Sopenharmony_ci	if (icache_is_aliasing()) {
23062306a36Sopenharmony_ci		/* any kind of VIPT cache */
23162306a36Sopenharmony_ci		icache_inval_all_pou();
23262306a36Sopenharmony_ci	} else if (read_sysreg(CurrentEL) != CurrentEL_EL1 ||
23362306a36Sopenharmony_ci		   !icache_is_vpipt()) {
23462306a36Sopenharmony_ci		/* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */
23562306a36Sopenharmony_ci		icache_inval_pou((unsigned long)va, (unsigned long)va + size);
23662306a36Sopenharmony_ci	}
23762306a36Sopenharmony_ci}
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_civoid kvm_set_way_flush(struct kvm_vcpu *vcpu);
24062306a36Sopenharmony_civoid kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_cistatic inline unsigned int kvm_get_vmid_bits(void)
24362306a36Sopenharmony_ci{
24462306a36Sopenharmony_ci	int reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	return get_vmid_bits(reg);
24762306a36Sopenharmony_ci}
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci/*
25062306a36Sopenharmony_ci * We are not in the kvm->srcu critical section most of the time, so we take
25162306a36Sopenharmony_ci * the SRCU read lock here. Since we copy the data from the user page, we
25262306a36Sopenharmony_ci * can immediately drop the lock again.
25362306a36Sopenharmony_ci */
25462306a36Sopenharmony_cistatic inline int kvm_read_guest_lock(struct kvm *kvm,
25562306a36Sopenharmony_ci				      gpa_t gpa, void *data, unsigned long len)
25662306a36Sopenharmony_ci{
25762306a36Sopenharmony_ci	int srcu_idx = srcu_read_lock(&kvm->srcu);
25862306a36Sopenharmony_ci	int ret = kvm_read_guest(kvm, gpa, data, len);
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	srcu_read_unlock(&kvm->srcu, srcu_idx);
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	return ret;
26362306a36Sopenharmony_ci}
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_cistatic inline int kvm_write_guest_lock(struct kvm *kvm, gpa_t gpa,
26662306a36Sopenharmony_ci				       const void *data, unsigned long len)
26762306a36Sopenharmony_ci{
26862306a36Sopenharmony_ci	int srcu_idx = srcu_read_lock(&kvm->srcu);
26962306a36Sopenharmony_ci	int ret = kvm_write_guest(kvm, gpa, data, len);
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	srcu_read_unlock(&kvm->srcu, srcu_idx);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	return ret;
27462306a36Sopenharmony_ci}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci#define kvm_phys_to_vttbr(addr)		phys_to_ttbr(addr)
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci/*
27962306a36Sopenharmony_ci * When this is (directly or indirectly) used on the TLB invalidation
28062306a36Sopenharmony_ci * path, we rely on a previously issued DSB so that page table updates
28162306a36Sopenharmony_ci * and VMID reads are correctly ordered.
28262306a36Sopenharmony_ci */
28362306a36Sopenharmony_cistatic __always_inline u64 kvm_get_vttbr(struct kvm_s2_mmu *mmu)
28462306a36Sopenharmony_ci{
28562306a36Sopenharmony_ci	struct kvm_vmid *vmid = &mmu->vmid;
28662306a36Sopenharmony_ci	u64 vmid_field, baddr;
28762306a36Sopenharmony_ci	u64 cnp = system_supports_cnp() ? VTTBR_CNP_BIT : 0;
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	baddr = mmu->pgd_phys;
29062306a36Sopenharmony_ci	vmid_field = atomic64_read(&vmid->id) << VTTBR_VMID_SHIFT;
29162306a36Sopenharmony_ci	vmid_field &= VTTBR_VMID_MASK(kvm_arm_vmid_bits);
29262306a36Sopenharmony_ci	return kvm_phys_to_vttbr(baddr) | vmid_field | cnp;
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci/*
29662306a36Sopenharmony_ci * Must be called from hyp code running at EL2 with an updated VTTBR
29762306a36Sopenharmony_ci * and interrupts disabled.
29862306a36Sopenharmony_ci */
29962306a36Sopenharmony_cistatic __always_inline void __load_stage2(struct kvm_s2_mmu *mmu,
30062306a36Sopenharmony_ci					  struct kvm_arch *arch)
30162306a36Sopenharmony_ci{
30262306a36Sopenharmony_ci	write_sysreg(arch->vtcr, vtcr_el2);
30362306a36Sopenharmony_ci	write_sysreg(kvm_get_vttbr(mmu), vttbr_el2);
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci	/*
30662306a36Sopenharmony_ci	 * ARM errata 1165522 and 1530923 require the actual execution of the
30762306a36Sopenharmony_ci	 * above before we can switch to the EL1/EL0 translation regime used by
30862306a36Sopenharmony_ci	 * the guest.
30962306a36Sopenharmony_ci	 */
31062306a36Sopenharmony_ci	asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT));
31162306a36Sopenharmony_ci}
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_cistatic inline struct kvm *kvm_s2_mmu_to_kvm(struct kvm_s2_mmu *mmu)
31462306a36Sopenharmony_ci{
31562306a36Sopenharmony_ci	return container_of(mmu->arch, struct kvm, arch);
31662306a36Sopenharmony_ci}
31762306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
31862306a36Sopenharmony_ci#endif /* __ARM64_KVM_MMU_H__ */
319