tlb.c 6.51 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6
/*
 * Copyright (C) 2015 - ARM Ltd
 * Author: Marc Zyngier <marc.zyngier@arm.com>
 */

7 8
#include <linux/irqflags.h>

9
#include <asm/kvm_hyp.h>
10
#include <asm/kvm_mmu.h>
11
#include <asm/tlbflush.h>
12

13 14 15 16 17 18
struct tlb_inv_context {
	unsigned long	flags;
	u64		tcr;
	u64		sctlr;
};

19
static void __hyp_text __tlb_switch_to_guest_vhe(struct kvm *kvm,
20
						 struct tlb_inv_context *cxt)
21 22 23
{
	u64 val;

24 25
	local_irq_save(cxt->flags);

26
	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
27
		/*
28 29
		 * For CPUs that are affected by ARM errata 1165522 or 1530923,
		 * we cannot trust stage-1 to be in a correct state at that
30 31 32 33 34 35
		 * point. Since we do not want to force a full load of the
		 * vcpu state, we prevent the EL1 page-table walker to
		 * allocate new TLBs. This is done by setting the EPD bits
		 * in the TCR_EL1 register. We also need to prevent it to
		 * allocate IPA->PA walks, so we enable the S1 MMU...
		 */
36
		val = cxt->tcr = read_sysreg_el1(SYS_TCR);
37
		val |= TCR_EPD1_MASK | TCR_EPD0_MASK;
38 39
		write_sysreg_el1(val, SYS_TCR);
		val = cxt->sctlr = read_sysreg_el1(SYS_SCTLR);
40
		val |= SCTLR_ELx_M;
41
		write_sysreg_el1(val, SYS_SCTLR);
42
	}
43

44 45 46 47 48 49
	/*
	 * With VHE enabled, we have HCR_EL2.{E2H,TGE} = {1,1}, and
	 * most TLB operations target EL2/EL0. In order to affect the
	 * guest TLBs (EL1/EL0), we need to change one of these two
	 * bits. Changing E2H is impossible (goodbye TTBR1_EL2), so
	 * let's flip TGE before executing the TLB operation.
50 51 52 53 54
	 *
	 * ARM erratum 1165522 requires some special handling (again),
	 * as we need to make sure both stages of translation are in
	 * place before clearing TGE. __load_guest_stage2() already
	 * has an ISB in order to deal with this.
55
	 */
56
	__load_guest_stage2(kvm);
57 58 59 60 61 62
	val = read_sysreg(hcr_el2);
	val &= ~HCR_TGE;
	write_sysreg(val, hcr_el2);
	isb();
}

63
static void __hyp_text __tlb_switch_to_guest_nvhe(struct kvm *kvm,
64
						  struct tlb_inv_context *cxt)
65
{
66
	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		u64 val;

		/*
		 * For CPUs that are affected by ARM 1319367, we need to
		 * avoid a host Stage-1 walk while we have the guest's
		 * VMID set in the VTTBR in order to invalidate TLBs.
		 * We're guaranteed that the S1 MMU is enabled, so we can
		 * simply set the EPD bits to avoid any further TLB fill.
		 */
		val = cxt->tcr = read_sysreg_el1(SYS_TCR);
		val |= TCR_EPD1_MASK | TCR_EPD0_MASK;
		write_sysreg_el1(val, SYS_TCR);
		isb();
	}

82
	/* __load_guest_stage2() includes an ISB for the workaround. */
83
	__load_guest_stage2(kvm);
84
	asm(ALTERNATIVE("isb", "nop", ARM64_WORKAROUND_SPECULATIVE_AT));
85 86
}

87 88 89 90 91 92 93 94
static void __hyp_text __tlb_switch_to_guest(struct kvm *kvm,
					     struct tlb_inv_context *cxt)
{
	if (has_vhe())
		__tlb_switch_to_guest_vhe(kvm, cxt);
	else
		__tlb_switch_to_guest_nvhe(kvm, cxt);
}
95

96
static void __hyp_text __tlb_switch_to_host_vhe(struct kvm *kvm,
97
						struct tlb_inv_context *cxt)
98 99 100 101 102 103 104
{
	/*
	 * We're done with the TLB operation, let's restore the host's
	 * view of HCR_EL2.
	 */
	write_sysreg(0, vttbr_el2);
	write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
105
	isb();
106

107
	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
108
		/* Restore the registers to what they were */
109 110
		write_sysreg_el1(cxt->tcr, SYS_TCR);
		write_sysreg_el1(cxt->sctlr, SYS_SCTLR);
111 112 113
	}

	local_irq_restore(cxt->flags);
114 115
}

116
static void __hyp_text __tlb_switch_to_host_nvhe(struct kvm *kvm,
117
						 struct tlb_inv_context *cxt)
118 119
{
	write_sysreg(0, vttbr_el2);
120

121
	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
122 123 124 125 126
		/* Ensure write of the host VMID */
		isb();
		/* Restore the host's TCR_EL1 */
		write_sysreg_el1(cxt->tcr, SYS_TCR);
	}
127 128
}

129 130 131 132 133 134 135 136
static void __hyp_text __tlb_switch_to_host(struct kvm *kvm,
					    struct tlb_inv_context *cxt)
{
	if (has_vhe())
		__tlb_switch_to_host_vhe(kvm, cxt);
	else
		__tlb_switch_to_host_nvhe(kvm, cxt);
}
137

138
void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
139
{
140
	struct tlb_inv_context cxt;
141

142 143 144 145
	dsb(ishst);

	/* Switch to requested VMID */
	kvm = kern_hyp_va(kvm);
146
	__tlb_switch_to_guest(kvm, &cxt);
147 148 149 150 151 152 153

	/*
	 * We could do so much better if we had the VA as well.
	 * Instead, we invalidate Stage-2 for this IPA, and the
	 * whole of Stage-1. Weep...
	 */
	ipa >>= 12;
154
	__tlbi(ipas2e1is, ipa);
155 156 157 158 159 160 161 162

	/*
	 * We have to ensure completion of the invalidation at Stage-2,
	 * since a table walk on another CPU could refill a TLB with a
	 * complete (S1 + S2) walk based on the old Stage-2 mapping if
	 * the Stage-1 invalidation happened first.
	 */
	dsb(ish);
163
	__tlbi(vmalle1is);
164 165 166
	dsb(ish);
	isb();

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	/*
	 * If the host is running at EL1 and we have a VPIPT I-cache,
	 * then we must perform I-cache maintenance at EL2 in order for
	 * it to have an effect on the guest. Since the guest cannot hit
	 * I-cache lines allocated with a different VMID, we don't need
	 * to worry about junk out of guest reset (we nuke the I-cache on
	 * VMID rollover), but we do need to be careful when remapping
	 * executable pages for the same guest. This can happen when KSM
	 * takes a CoW fault on an executable page, copies the page into
	 * a page that was previously mapped in the guest and then needs
	 * to invalidate the guest view of the I-cache for that page
	 * from EL1. To solve this, we invalidate the entire I-cache when
	 * unmapping a page from a guest if we have a VPIPT I-cache but
	 * the host is running at EL1. As above, we could do better if
	 * we had the VA.
	 *
	 * The moral of this story is: if you have a VPIPT I-cache, then
	 * you should be running with VHE enabled.
	 */
	if (!has_vhe() && icache_is_vpipt())
		__flush_icache_all();

189
	__tlb_switch_to_host(kvm, &cxt);
190 191
}

192
void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)
193
{
194
	struct tlb_inv_context cxt;
195

196 197 198 199
	dsb(ishst);

	/* Switch to requested VMID */
	kvm = kern_hyp_va(kvm);
200
	__tlb_switch_to_guest(kvm, &cxt);
201

202
	__tlbi(vmalls12e1is);
203 204 205
	dsb(ish);
	isb();

206
	__tlb_switch_to_host(kvm, &cxt);
207 208
}

209 210 211
void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu)
{
	struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm);
212
	struct tlb_inv_context cxt;
213 214

	/* Switch to requested VMID */
215
	__tlb_switch_to_guest(kvm, &cxt);
216

217
	__tlbi(vmalle1);
218 219 220
	dsb(nsh);
	isb();

221
	__tlb_switch_to_host(kvm, &cxt);
222 223
}

224
void __hyp_text __kvm_flush_vm_context(void)
225 226
{
	dsb(ishst);
227
	__tlbi(alle1is);
228 229 230 231 232 233 234 235 236 237 238 239 240

	/*
	 * VIPT and PIPT caches are not affected by VMID, so no maintenance
	 * is necessary across a VMID rollover.
	 *
	 * VPIPT caches constrain lookup and maintenance to the active VMID,
	 * so we need to invalidate lines with a stale VMID to avoid an ABA
	 * race after multiple rollovers.
	 *
	 */
	if (icache_is_vpipt())
		asm volatile("ic ialluis");

241 242
	dsb(ish);
}