xref: /linux/arch/arm64/kvm/nested.c (revision 3385a832525527801012f5993d1f6a7a3d23092c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 - Columbia University and Linaro Ltd.
4  * Author: Jintack Lim <jintack.lim@linaro.org>
5  */
6 
7 #include <linux/bitfield.h>
8 #include <linux/interval_tree.h>
9 #include <linux/kvm.h>
10 #include <linux/kvm_host.h>
11 
12 #include <asm/fixmap.h>
13 #include <asm/kvm_arm.h>
14 #include <asm/kvm_emulate.h>
15 #include <asm/kvm_mmu.h>
16 #include <asm/kvm_nested.h>
17 #include <asm/sysreg.h>
18 
19 #include "sys_regs.h"
20 #include "vgic/vgic.h"
21 
22 struct vncr_tlb {
23 	/* The guest's VNCR_EL2 */
24 	u64			gva;
25 	struct s1_walk_info	wi;
26 	struct s1_walk_result	wr;
27 
28 	u64			hpa;
29 	bool			hpa_writable;
30 
31 	/* -1 when not mapped on a CPU */
32 	atomic_t		cpu;
33 
34 	/*
35 	 * true if the TLB is valid. Can only be changed with the
36 	 * mmu_lock held.
37 	 */
38 	bool			valid;
39 };
40 
41 /*
42  * Ratio of live shadow S2 MMU per vcpu. This is a trade-off between
43  * memory usage and potential number of different sets of S2 PTs in
44  * the guests. Running out of S2 MMUs only affects performance (we
45  * will invalidate them more often).
46  */
47 #define S2_MMU_PER_VCPU		2
48 #define S2_MMU_PER_VM		(KVM_MAX_VCPUS * S2_MMU_PER_VCPU)
49 
50 int kvm_init_nested(struct kvm *kvm)
51 {
52 	kvm->arch.nested_mmus = kvmalloc_objs(struct kvm_s2_mmu *,
53 					      S2_MMU_PER_VM,
54 					      GFP_KERNEL_ACCOUNT);
55 	kvm->arch.nested_mmus_size = 0;
56 	atomic_set(&kvm->arch.vncr_tlb_count, 0);
57 	spin_lock_init(&kvm->arch.guest_s2_tracking_lock);
58 
59 	return kvm->arch.nested_mmus ? 0 : -ENOMEM;
60 }
61 
62 void kvm_destroy_nested(struct kvm *kvm)
63 {
64 	for (int i = 0; i < kvm->arch.nested_mmus_size; i+= S2_MMU_PER_VCPU)
65 		kvfree(kvm->arch.nested_mmus[i]);
66 
67 	kvm->arch.nested_mmus_size = 0;
68 	kvfree(kvm->arch.nested_mmus);
69 }
70 
71 static int init_nested_s2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu)
72 {
73 	/*
74 	 * We only initialise the IPA range on the canonical MMU, which
75 	 * defines the contract between KVM and userspace on where the
76 	 * "hardware" is in the IPA space. This affects the validity of MMIO
77 	 * exits forwarded to userspace, for example.
78 	 *
79 	 * For nested S2s, we use the PARange as exposed to the guest, as it
80 	 * is allowed to use it at will to expose whatever memory map it
81 	 * wants to its own guests as it would be on real HW.
82 	 */
83 	return kvm_init_stage2_mmu(kvm, mmu, kvm_get_pa_bits(kvm));
84 }
85 
86 int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
87 {
88 	struct kvm *kvm = vcpu->kvm;
89 	int num_mmus;
90 
91 	lockdep_assert_held(&kvm->arch.config_lock);
92 
93 	if (test_bit(KVM_ARM_VCPU_HAS_EL2_E2H0, kvm->arch.vcpu_features) &&
94 	    !cpus_have_final_cap(ARM64_HAS_HCR_NV1))
95 		return -EINVAL;
96 
97 	if (!vcpu->arch.ctxt.vncr_array)
98 		vcpu->arch.ctxt.vncr_array = (u64 *)__get_free_page(GFP_KERNEL_ACCOUNT |
99 								    __GFP_ZERO);
100 
101 	if (!vcpu->arch.ctxt.vncr_array)
102 		return -ENOMEM;
103 
104 	num_mmus = atomic_read(&kvm->online_vcpus) * S2_MMU_PER_VCPU;
105 
106 	if (num_mmus > kvm->arch.nested_mmus_size) {
107 		struct kvm_s2_mmu *tmp;
108 		int i, ret = 0;
109 
110 		tmp = kvzalloc_objs(*tmp, S2_MMU_PER_VCPU, GFP_KERNEL_ACCOUNT);
111 		if (!tmp)
112 			ret = -ENOMEM;
113 
114 		for (i = 0; !ret && i < S2_MMU_PER_VCPU; i++) {
115 			ret = init_nested_s2_mmu(kvm, &tmp[i]);
116 			if (ret)
117 				break;
118 		}
119 
120 		if (ret) {
121 			while (--i >= 0)
122 				kvm_free_stage2_pgd(&tmp[i]);
123 
124 			kvfree(tmp);
125 			free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
126 			vcpu->arch.ctxt.vncr_array = NULL;
127 			return ret;
128 		}
129 
130 		for (i = 0; i < S2_MMU_PER_VCPU; i++)
131 			kvm_nested_s2_ptdump_create_debugfs(&tmp[i], i + kvm->arch.nested_mmus_size);
132 
133 		guard(write_lock)(&kvm->mmu_lock);
134 
135 		for (i = 0; i < S2_MMU_PER_VCPU; i++) {
136 			tmp[i].s2_mmu_idx = i + kvm->arch.nested_mmus_size;
137 			kvm->arch.nested_mmus[i + kvm->arch.nested_mmus_size] = &tmp[i];
138 		}
139 
140 		kvm->arch.nested_mmus_size += S2_MMU_PER_VCPU;
141 	}
142 
143 	/*
144 	 * We can't rely on the S2 MMU staying the canonical one until
145 	 * the first run, as userspace can restore a nested context.
146 	 * Nuke the hw_mmu early to ensure that we pick the correct one
147 	 * at load time.
148 	 */
149 	vcpu->arch.hw_mmu = NULL;
150 
151 	return 0;
152 }
153 
154 struct s2_walk_info {
155 	u64		baddr;
156 	unsigned int	max_oa_bits;
157 	unsigned int	pgshift;
158 	unsigned int	sl;
159 	unsigned int	t0sz;
160 	bool		be;
161 	bool		ha;
162 };
163 
164 static u32 compute_fsc(int level, u32 fsc)
165 {
166 	return fsc | (level & 0x3);
167 }
168 
169 static int esr_s2_fault(struct kvm_vcpu *vcpu, int level, u32 fsc)
170 {
171 	u32 esr;
172 
173 	esr = kvm_vcpu_get_esr(vcpu) & ~ESR_ELx_FSC;
174 	esr |= compute_fsc(level, fsc);
175 	return esr;
176 }
177 
178 static int get_ia_size(struct s2_walk_info *wi)
179 {
180 	return 64 - wi->t0sz;
181 }
182 
183 static int check_base_s2_limits(struct kvm_vcpu *vcpu, struct s2_walk_info *wi,
184 				int level, int input_size, int stride)
185 {
186 	int start_size, pa_max;
187 
188 	pa_max = kvm_get_pa_bits(vcpu->kvm);
189 
190 	/* Check translation limits */
191 	switch (BIT(wi->pgshift)) {
192 	case SZ_64K:
193 		if (level == 0 || (level == 1 && pa_max <= 42))
194 			return -EFAULT;
195 		break;
196 	case SZ_16K:
197 		if (level == 0 || (level == 1 && pa_max <= 40))
198 			return -EFAULT;
199 		break;
200 	case SZ_4K:
201 		if (level < 0 || (level == 0 && pa_max <= 42))
202 			return -EFAULT;
203 		break;
204 	}
205 
206 	/* Check input size limits */
207 	if (input_size > pa_max)
208 		return -EFAULT;
209 
210 	/* Check number of entries in starting level table */
211 	start_size = input_size - ((3 - level) * stride + wi->pgshift);
212 	if (start_size < 1 || start_size > stride + 4)
213 		return -EFAULT;
214 
215 	return 0;
216 }
217 
218 /* Check if output is within boundaries */
219 static int check_output_size(struct s2_walk_info *wi, phys_addr_t output)
220 {
221 	unsigned int output_size = wi->max_oa_bits;
222 
223 	if (output_size != 48 && (output & GENMASK_ULL(47, output_size)))
224 		return -1;
225 
226 	return 0;
227 }
228 
229 static int read_guest_s2_desc(struct kvm_vcpu *vcpu, phys_addr_t pa, u64 *desc,
230 			      struct s2_walk_info *wi)
231 {
232 	u64 val;
233 	int r;
234 
235 	r = kvm_read_guest(vcpu->kvm, pa, &val, sizeof(val));
236 	if (r)
237 		return r;
238 
239 	/*
240 	 * Handle reversedescriptors if endianness differs between the
241 	 * host and the guest hypervisor.
242 	 */
243 	if (wi->be)
244 		*desc = be64_to_cpu((__force __be64)val);
245 	else
246 		*desc = le64_to_cpu((__force __le64)val);
247 
248 	return 0;
249 }
250 
251 static int swap_guest_s2_desc(struct kvm_vcpu *vcpu, phys_addr_t pa, u64 old, u64 new,
252 			      struct s2_walk_info *wi)
253 {
254 	if (wi->be) {
255 		old = (__force u64)cpu_to_be64(old);
256 		new = (__force u64)cpu_to_be64(new);
257 	} else {
258 		old = (__force u64)cpu_to_le64(old);
259 		new = (__force u64)cpu_to_le64(new);
260 	}
261 
262 	return __kvm_at_swap_desc(vcpu->kvm, pa, old, new);
263 }
264 
265 /*
266  * This is essentially a C-version of the pseudo code from the ARM ARM
267  * AArch64.TranslationTableWalk  function.  I strongly recommend looking at
268  * that pseudocode in trying to understand this.
269  *
270  * Must be called with the kvm->srcu read lock held
271  */
272 static int walk_nested_s2_pgd(struct kvm_vcpu *vcpu, phys_addr_t ipa,
273 			      struct s2_walk_info *wi, struct kvm_s2_trans *out)
274 {
275 	int first_block_level, level, stride, input_size, base_lower_bound;
276 	phys_addr_t base_addr;
277 	unsigned int addr_top, addr_bottom;
278 	u64 desc, new_desc;  /* page table entry */
279 	int ret;
280 	phys_addr_t paddr;
281 
282 	switch (BIT(wi->pgshift)) {
283 	default:
284 	case SZ_64K:
285 	case SZ_16K:
286 		level = 3 - wi->sl;
287 		first_block_level = 2;
288 		break;
289 	case SZ_4K:
290 		level = 2 - wi->sl;
291 		first_block_level = 1;
292 		break;
293 	}
294 
295 	stride = wi->pgshift - 3;
296 	input_size = get_ia_size(wi);
297 	if (input_size > 48 || input_size < 25)
298 		return -EFAULT;
299 
300 	ret = check_base_s2_limits(vcpu, wi, level, input_size, stride);
301 	if (WARN_ON(ret)) {
302 		out->esr = compute_fsc(0, ESR_ELx_FSC_FAULT);
303 		return ret;
304 	}
305 
306 	base_lower_bound = 3 + input_size - ((3 - level) * stride +
307 			   wi->pgshift);
308 	base_addr = wi->baddr & GENMASK_ULL(47, base_lower_bound);
309 
310 	if (check_output_size(wi, base_addr)) {
311 		/* R_BFHQH */
312 		out->esr = compute_fsc(0, ESR_ELx_FSC_ADDRSZ);
313 		return 1;
314 	}
315 
316 	addr_top = input_size - 1;
317 
318 	while (1) {
319 		phys_addr_t index;
320 
321 		addr_bottom = (3 - level) * stride + wi->pgshift;
322 		index = (ipa & GENMASK_ULL(addr_top, addr_bottom))
323 			>> (addr_bottom - 3);
324 
325 		paddr = base_addr | index;
326 		ret = read_guest_s2_desc(vcpu, paddr, &desc, wi);
327 		if (ret < 0) {
328 			out->esr = ESR_ELx_FSC_SEA_TTW(level);
329 			return ret;
330 		}
331 
332 		new_desc = desc;
333 
334 		/* Check for valid descriptor at this point */
335 		if (!(desc & KVM_PTE_VALID)) {
336 			out->esr = compute_fsc(level, ESR_ELx_FSC_FAULT);
337 			out->desc = desc;
338 			return 1;
339 		}
340 
341 		if (FIELD_GET(KVM_PTE_TYPE, desc) == KVM_PTE_TYPE_BLOCK) {
342 			if (level < 3)
343 				break;
344 
345 			out->esr = compute_fsc(level, ESR_ELx_FSC_FAULT);
346 			out->desc = desc;
347 			return 1;
348 		}
349 
350 		/* We're at the final level */
351 		if (level == 3)
352 			break;
353 
354 		if (check_output_size(wi, desc)) {
355 			out->esr = compute_fsc(level, ESR_ELx_FSC_ADDRSZ);
356 			out->desc = desc;
357 			return 1;
358 		}
359 
360 		base_addr = desc & GENMASK_ULL(47, wi->pgshift);
361 
362 		level += 1;
363 		addr_top = addr_bottom - 1;
364 	}
365 
366 	if (level < first_block_level) {
367 		out->esr = compute_fsc(level, ESR_ELx_FSC_FAULT);
368 		out->desc = desc;
369 		return 1;
370 	}
371 
372 	if (check_output_size(wi, desc)) {
373 		out->esr = compute_fsc(level, ESR_ELx_FSC_ADDRSZ);
374 		out->desc = desc;
375 		return 1;
376 	}
377 
378 	if (wi->ha)
379 		new_desc |= KVM_PTE_LEAF_ATTR_LO_S2_AF;
380 
381 	if (new_desc != desc) {
382 		ret = swap_guest_s2_desc(vcpu, paddr, desc, new_desc, wi);
383 		if (ret == -EAGAIN)
384 			return ret;
385 		if (ret) {
386 			out->esr = ESR_ELx_FSC_SEA_TTW(level);
387 			out->desc = desc;
388 			return 1;
389 		}
390 
391 		desc = new_desc;
392 	}
393 
394 	if (!(desc & KVM_PTE_LEAF_ATTR_LO_S2_AF)) {
395 		out->esr = compute_fsc(level, ESR_ELx_FSC_ACCESS);
396 		out->desc = desc;
397 		return 1;
398 	}
399 
400 	addr_bottom += contiguous_bit_shift(desc, wi, level);
401 
402 	/* Calculate and return the result */
403 	paddr = (desc & GENMASK_ULL(47, addr_bottom)) |
404 		(ipa & GENMASK_ULL(addr_bottom - 1, 0));
405 	out->output = paddr;
406 	out->block_size = 1UL << ((3 - level) * stride + wi->pgshift);
407 	out->readable = desc & KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;
408 	out->writable = desc & KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
409 	out->level = level;
410 	out->desc = desc;
411 	return 0;
412 }
413 
414 #define _has_tgran_2(__r, __sz)						\
415 	({								\
416 		u64 _s1, _s2, _mmfr0 = __r;				\
417 									\
418 		_s2 = SYS_FIELD_GET(ID_AA64MMFR0_EL1,			\
419 				    TGRAN##__sz##_2, _mmfr0);		\
420 									\
421 		_s1 = SYS_FIELD_GET(ID_AA64MMFR0_EL1,			\
422 				    TGRAN##__sz, _mmfr0);		\
423 									\
424 		((_s2 != ID_AA64MMFR0_EL1_TGRAN##__sz##_2_NI &&		\
425 		  _s2 != ID_AA64MMFR0_EL1_TGRAN##__sz##_2_TGRAN##__sz) || \
426 		 (_s2 == ID_AA64MMFR0_EL1_TGRAN##__sz##_2_TGRAN##__sz && \
427 		  _s1 != ID_AA64MMFR0_EL1_TGRAN##__sz##_NI));		\
428 	})
429 
430 static bool has_tgran_2(u64 mmfr0, unsigned int shift)
431 {
432 	switch (shift) {
433 	case 12:
434 		return _has_tgran_2(mmfr0, 4);
435 	case 14:
436 		return _has_tgran_2(mmfr0, 16);
437 	case 16:
438 		return _has_tgran_2(mmfr0, 64);
439 	default:
440 		BUG();
441 	}
442 }
443 
444 static unsigned int fallback_tgran2_shift(u64 mmfr0)
445 {
446 	if (has_tgran_2(mmfr0, PAGE_SHIFT))
447 		return PAGE_SHIFT;
448 	else if (has_tgran_2(mmfr0, 12))
449 		return 12;
450 	else if (has_tgran_2(mmfr0, 14))
451 		return 14;
452 	else if (has_tgran_2(mmfr0, 16))
453 		return 16;
454 	else
455 		return PAGE_SHIFT;
456 }
457 
458 static unsigned int vtcr_to_tg0_pgshift(struct kvm *kvm, u64 vtcr)
459 {
460 	u64 tg0 = FIELD_GET(VTCR_EL2_TG0_MASK, vtcr);
461 	u64 mmfr0 = kvm_read_vm_id_reg(kvm, SYS_ID_AA64MMFR0_EL1);
462 	unsigned int shift;
463 
464 	switch (tg0) {
465 	case VTCR_EL2_TG0_4K:
466 		shift = 12;
467 		break;
468 	case VTCR_EL2_TG0_16K:
469 		shift = 14;
470 		break;
471 	case VTCR_EL2_TG0_64K:
472 	/* IMPDEF: treat any other value as 64k, subject to fallback */
473 	default:
474 		shift = 16;
475 	}
476 
477 	/*
478 	 * If TGx is programmed to an unimplemented value (not advertised in
479 	 * ID_AA64MMFR0_EL1), we should treat it as if an implemented value is
480 	 * written, as per the architecture. Choose an available one while
481 	 * prioritizing PAGE_SIZE.
482 	 */
483 	if (!has_tgran_2(mmfr0, shift))
484 		return fallback_tgran2_shift(mmfr0);
485 
486 	return shift;
487 }
488 
489 static size_t vtcr_to_tg0_pgsize(struct kvm *kvm, u64 vtcr)
490 {
491 	return BIT(vtcr_to_tg0_pgshift(kvm, vtcr));
492 }
493 
494 static void setup_s2_walk(struct kvm_vcpu *vcpu, struct s2_walk_info *wi)
495 {
496 	u64 vtcr = vcpu_read_sys_reg(vcpu, VTCR_EL2);
497 
498 	wi->baddr = vcpu_read_sys_reg(vcpu, VTTBR_EL2);
499 	wi->t0sz = vtcr & VTCR_EL2_T0SZ_MASK;
500 	wi->pgshift = vtcr_to_tg0_pgshift(vcpu->kvm, vtcr);
501 	wi->sl = FIELD_GET(VTCR_EL2_SL0_MASK, vtcr);
502 	/* Global limit for now, should eventually be per-VM */
503 	wi->max_oa_bits = min(get_kvm_ipa_limit(),
504 			      ps_to_output_size(FIELD_GET(VTCR_EL2_PS_MASK, vtcr), false));
505 	wi->ha = vtcr & VTCR_EL2_HA;
506 	wi->be = vcpu_read_sys_reg(vcpu, SCTLR_EL2) & SCTLR_ELx_EE;
507 }
508 
509 int kvm_walk_nested_s2(struct kvm_vcpu *vcpu, phys_addr_t gipa,
510 		       struct kvm_s2_trans *result)
511 {
512 	struct s2_walk_info wi;
513 	int ret;
514 
515 	result->esr = 0;
516 
517 	if (!vcpu_has_nv(vcpu))
518 		return 0;
519 
520 	setup_s2_walk(vcpu, &wi);
521 
522 	ret = walk_nested_s2_pgd(vcpu, gipa, &wi, result);
523 	if (ret)
524 		result->esr |= (kvm_vcpu_get_esr(vcpu) & ~ESR_ELx_FSC);
525 
526 	return ret;
527 }
528 
529 static unsigned int __ttl_to_size(u8 ttl)
530 {
531 	int level = ttl & 3;
532 	int gran = (ttl >> 2) & 3;
533 	unsigned int max_size = 0;
534 
535 	switch (gran) {
536 	case TLBI_TTL_TG_4K:
537 		switch (level) {
538 		case 0:
539 			break;
540 		case 1:
541 			max_size = SZ_1G;
542 			break;
543 		case 2:
544 			max_size = SZ_2M;
545 			break;
546 		case 3:
547 			max_size = SZ_4K;
548 			break;
549 		}
550 		break;
551 	case TLBI_TTL_TG_16K:
552 		switch (level) {
553 		case 0:
554 		case 1:
555 			break;
556 		case 2:
557 			max_size = SZ_32M;
558 			break;
559 		case 3:
560 			max_size = SZ_16K;
561 			break;
562 		}
563 		break;
564 	case TLBI_TTL_TG_64K:
565 		switch (level) {
566 		case 0:
567 		case 1:
568 			/* No 52bit IPA support */
569 			break;
570 		case 2:
571 			max_size = SZ_512M;
572 			break;
573 		case 3:
574 			max_size = SZ_64K;
575 			break;
576 		}
577 		break;
578 	default:			/* No size information */
579 		break;
580 	}
581 
582 	return max_size;
583 }
584 
585 static unsigned int ttl_to_size(u8 ttl)
586 {
587 	return __ttl_to_size(ttl) ?: SZ_1G;
588 }
589 
590 static u8 pgshift_level_to_ttl(u16 shift, s8 level)
591 {
592 	u8 ttl;
593 
594 	/*
595 	 * If we don't have a proper level, fallback to the maximum
596 	 * size.
597 	 */
598 	if (level < 0)
599 		return 0;
600 
601 	switch(shift) {
602 	case 12:
603 		ttl = TLBI_TTL_TG_4K;
604 		break;
605 	case 14:
606 		ttl = TLBI_TTL_TG_16K;
607 		break;
608 	case 16:
609 		ttl = TLBI_TTL_TG_64K;
610 		break;
611 	default:
612 		BUG();
613 	}
614 
615 	ttl <<= 2;
616 	ttl |= level & 3;
617 
618 	return ttl;
619 }
620 
621 /*
622  * Compute the equivalent of the TTL field by parsing the shadow PT.  The
623  * granule size is extracted from the cached VTCR_EL2.TG0 while the level is
624  * retrieved from first entry carrying the level as a tag.
625  */
626 static u8 get_guest_mapping_ttl(struct kvm_s2_mmu *mmu, u64 addr)
627 {
628 	size_t tg0_size = vtcr_to_tg0_pgsize(kvm_s2_mmu_to_kvm(mmu), mmu->tlb_vtcr);
629 	u64 tmp, sz = 0;
630 	kvm_pte_t pte;
631 	u8 ttl, level;
632 
633 	lockdep_assert_held_write(&kvm_s2_mmu_to_kvm(mmu)->mmu_lock);
634 
635 	switch (tg0_size) {
636 	case SZ_4K:
637 		ttl = (TLBI_TTL_TG_4K << 2);
638 		break;
639 	case SZ_16K:
640 		ttl = (TLBI_TTL_TG_16K << 2);
641 		break;
642 	case SZ_64K:
643 	default:	    /* IMPDEF: treat any other value as 64k */
644 		ttl = (TLBI_TTL_TG_64K << 2);
645 		break;
646 	}
647 
648 	tmp = addr;
649 
650 again:
651 	/* Iteratively compute the block sizes for a particular granule size */
652 	switch (tg0_size) {
653 	case SZ_4K:
654 		if	(sz < SZ_4K)	sz = SZ_4K;
655 		else if (sz < SZ_2M)	sz = SZ_2M;
656 		else if (sz < SZ_1G)	sz = SZ_1G;
657 		else			sz = 0;
658 		break;
659 	case SZ_16K:
660 		if	(sz < SZ_16K)	sz = SZ_16K;
661 		else if (sz < SZ_32M)	sz = SZ_32M;
662 		else			sz = 0;
663 		break;
664 	case SZ_64K:
665 	default:	    /* IMPDEF: treat any other value as 64k */
666 		if	(sz < SZ_64K)	sz = SZ_64K;
667 		else if (sz < SZ_512M)	sz = SZ_512M;
668 		else			sz = 0;
669 		break;
670 	}
671 
672 	if (sz == 0)
673 		return 0;
674 
675 	tmp &= ~(sz - 1);
676 	if (kvm_pgtable_get_leaf(mmu->pgt, tmp, &pte, NULL, 0))
677 		goto again;
678 	if (!(pte & PTE_VALID))
679 		goto again;
680 	level = FIELD_GET(KVM_NV_GUEST_MAP_SZ, pte);
681 	if (!level)
682 		goto again;
683 
684 	ttl |= level;
685 
686 	/*
687 	 * We now have found some level information in the shadow S2. Check
688 	 * that the resulting range is actually including the original IPA.
689 	 */
690 	sz = ttl_to_size(ttl);
691 	if (addr < (tmp + sz))
692 		return ttl;
693 
694 	return 0;
695 }
696 
697 unsigned long compute_tlb_inval_range(struct kvm_s2_mmu *mmu, u64 val)
698 {
699 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
700 	unsigned long max_size;
701 	u8 ttl;
702 
703 	ttl = FIELD_GET(TLBI_TTL_MASK, val);
704 
705 	if (!ttl || !kvm_has_feat(kvm, ID_AA64MMFR2_EL1, TTL, IMP)) {
706 		/* No TTL, check the shadow S2 for a hint */
707 		u64 addr = (val & GENMASK_ULL(35, 0)) << 12;
708 		ttl = get_guest_mapping_ttl(mmu, addr);
709 	}
710 
711 	/*
712 	 * Don't use the default 1GB fallback, as we can adapt to the
713 	 * max mapping size we allow at S2.
714 	 */
715 	max_size = __ttl_to_size(ttl);
716 
717 	if (!max_size) {
718 		/* Compute the maximum extent of the invalidation */
719 		switch (vtcr_to_tg0_pgsize(kvm, mmu->tlb_vtcr)) {
720 		case SZ_4K:
721 			max_size = SZ_1G;
722 			break;
723 		case SZ_16K:
724 			max_size = SZ_32M;
725 			break;
726 		case SZ_64K:
727 		default:    /* IMPDEF: treat any other value as 64k */
728 			/*
729 			 * No, we do not support 52bit IPA in nested yet. Once
730 			 * we do, this should be 4TB.
731 			 */
732 			max_size = SZ_512M;
733 			break;
734 		}
735 	}
736 
737 	WARN_ON(!max_size);
738 	return max_size;
739 }
740 
741 /*
742  * We can have multiple *different* MMU contexts with the same VMID:
743  *
744  * - S2 being enabled or not, hence differing by the HCR_EL2.VM bit
745  *
746  * - Multiple vcpus using private S2s (huh huh...), hence differing by the
747  *   VBBTR_EL2.BADDR address
748  *
749  * - A combination of the above...
750  *
751  * We can always identify which MMU context to pick at run-time.  However,
752  * TLB invalidation involving a VMID must take action on all the TLBs using
753  * this particular VMID. This translates into applying the same invalidation
754  * operation to all the contexts that are using this VMID. Moar phun!
755  */
756 void kvm_s2_mmu_iterate_by_vmid(struct kvm *kvm, u16 vmid,
757 				const union tlbi_info *info,
758 				void (*tlbi_callback)(struct kvm_s2_mmu *,
759 						      const union tlbi_info *))
760 {
761 	write_lock(&kvm->mmu_lock);
762 
763 	for (int i = 0; i < kvm->arch.nested_mmus_size; i++) {
764 		struct kvm_s2_mmu *mmu = kvm->arch.nested_mmus[i];
765 
766 		if (!kvm_s2_mmu_valid(mmu))
767 			continue;
768 
769 		if (vmid == get_vmid(mmu->tlb_vttbr))
770 			tlbi_callback(mmu, info);
771 	}
772 
773 	write_unlock(&kvm->mmu_lock);
774 }
775 
776 struct kvm_s2_mmu *lookup_s2_mmu(struct kvm_vcpu *vcpu)
777 {
778 	struct kvm *kvm = vcpu->kvm;
779 	bool nested_stage2_enabled;
780 	u64 vttbr, vtcr, hcr;
781 
782 	lockdep_assert_held_write(&kvm->mmu_lock);
783 
784 	vttbr = vcpu_read_sys_reg(vcpu, VTTBR_EL2);
785 	vtcr = vcpu_read_sys_reg(vcpu, VTCR_EL2);
786 	hcr = vcpu_read_sys_reg(vcpu, HCR_EL2);
787 
788 	nested_stage2_enabled = hcr & HCR_EL2_VM;
789 
790 	/* Don't consider the CnP bit for the vttbr match */
791 	vttbr &= ~VTTBR_CNP_BIT;
792 
793 	/*
794 	 * Two possibilities when looking up a S2 MMU context:
795 	 *
796 	 * - either S2 is enabled in the guest, and we need a context that is
797 	 *   S2-enabled and matches the full VTTBR (VMID+BADDR) and VTCR,
798 	 *   which makes it safe from a TLB conflict perspective (a broken
799 	 *   guest won't be able to generate them),
800 	 *
801 	 * - or S2 is disabled, and we need a context that is S2-disabled
802 	 *   and matches the VMID only, as all TLBs are tagged by VMID even
803 	 *   if S2 translation is disabled.
804 	 */
805 	for (int i = 0; i < kvm->arch.nested_mmus_size; i++) {
806 		struct kvm_s2_mmu *mmu = kvm->arch.nested_mmus[i];
807 
808 		if (!kvm_s2_mmu_valid(mmu))
809 			continue;
810 
811 		if (nested_stage2_enabled &&
812 		    mmu->nested_stage2_enabled &&
813 		    vttbr == mmu->tlb_vttbr &&
814 		    vtcr == mmu->tlb_vtcr)
815 			return mmu;
816 
817 		if (!nested_stage2_enabled &&
818 		    !mmu->nested_stage2_enabled &&
819 		    get_vmid(vttbr) == get_vmid(mmu->tlb_vttbr))
820 			return mmu;
821 	}
822 	return NULL;
823 }
824 
825 static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
826 {
827 	struct kvm *kvm = vcpu->kvm;
828 	struct kvm_s2_mmu *s2_mmu;
829 	int i;
830 
831 	lockdep_assert_held_write(&vcpu->kvm->mmu_lock);
832 
833 	s2_mmu = lookup_s2_mmu(vcpu);
834 	if (s2_mmu)
835 		goto out;
836 
837 	/*
838 	 * Make sure we don't always search from the same point, or we
839 	 * will always reuse a potentially active context, leaving
840 	 * free contexts unused.
841 	 */
842 	for (i = kvm->arch.nested_mmus_next;
843 	     i < (kvm->arch.nested_mmus_size + kvm->arch.nested_mmus_next);
844 	     i++) {
845 		s2_mmu = kvm->arch.nested_mmus[i % kvm->arch.nested_mmus_size];
846 
847 		if (atomic_read(&s2_mmu->refcnt) == 0)
848 			break;
849 	}
850 	BUG_ON(atomic_read(&s2_mmu->refcnt)); /* We have struct MMUs to spare */
851 
852 	/* Set the scene for the next search */
853 	kvm->arch.nested_mmus_next = (i + 1) % kvm->arch.nested_mmus_size;
854 
855 	/* Make sure we don't forget to do the laundry */
856 	if (kvm_s2_mmu_valid(s2_mmu))
857 		s2_mmu->pending_unmap = true;
858 
859 	/*
860 	 * The virtual VMID (modulo CnP) will be used as a key when matching
861 	 * an existing kvm_s2_mmu.
862 	 *
863 	 * We cache VTCR at allocation time, once and for all. It'd be great
864 	 * if the guest didn't screw that one up, as this is not very
865 	 * forgiving...
866 	 */
867 	s2_mmu->tlb_vttbr = vcpu_read_sys_reg(vcpu, VTTBR_EL2) & ~VTTBR_CNP_BIT;
868 	s2_mmu->tlb_vtcr = vcpu_read_sys_reg(vcpu, VTCR_EL2);
869 	s2_mmu->nested_stage2_enabled = vcpu_read_sys_reg(vcpu, HCR_EL2) & HCR_EL2_VM;
870 
871 out:
872 	atomic_inc(&s2_mmu->refcnt);
873 
874 	/*
875 	 * Set the vCPU request to perform an unmap, even if the pending unmap
876 	 * originates from another vCPU. This guarantees that the MMU has been
877 	 * completely unmapped before any vCPU actually uses it, and allows
878 	 * multiple vCPUs to lend a hand with completing the unmap.
879 	 */
880 	if (s2_mmu->pending_unmap)
881 		kvm_make_request(KVM_REQ_NESTED_S2_UNMAP, vcpu);
882 
883 	return s2_mmu;
884 }
885 
886 #define S2_MMU_IDX_MASK	GENMASK_ULL(11, 0)
887 
888 static void tag_s2_mapping_mmu(struct kvm_guest_s2_mapping *mapping,
889 			       struct kvm_s2_mmu *mmu)
890 {
891 	BUILD_BUG_ON(S2_MMU_PER_VM > SZ_4K);
892 	mapping->nested.start &= ~S2_MMU_IDX_MASK;
893 	mapping->nested.start |= mmu->s2_mmu_idx;
894 }
895 
896 static struct kvm_s2_mmu *s2_mapping_to_mmu(struct kvm *kvm,
897 					    struct kvm_guest_s2_mapping *mapping)
898 {
899 	return kvm->arch.nested_mmus[mapping->nested.start & S2_MMU_IDX_MASK];
900 }
901 
902 static unsigned long s2_mapping_to_nested_start(struct kvm_guest_s2_mapping *mapping)
903 {
904 	return mapping->nested.start & ~S2_MMU_IDX_MASK;
905 }
906 
907 void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,
908 				 gpa_t nested_ipa, size_t map_size,
909 				 struct kvm_guest_s2_mapping *mapping)
910 {
911 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
912 
913 	lockdep_assert_held_read(&kvm->mmu_lock);
914 
915 	canonical_ipa = ALIGN_DOWN(canonical_ipa, map_size);
916 	nested_ipa = ALIGN_DOWN(nested_ipa, map_size);
917 
918 	mapping->canonical.start = canonical_ipa;
919 	mapping->canonical.last  = canonical_ipa + map_size - 1;
920 
921 	mapping->nested.start    = nested_ipa;
922 	mapping->nested.last     = nested_ipa + map_size - 1;
923 
924 	tag_s2_mapping_mmu(mapping, mmu);
925 
926 	guard(spinlock)(&kvm->arch.guest_s2_tracking_lock);
927 	interval_tree_insert(&mapping->nested, &mmu->guest_s2_mappings);
928 	interval_tree_insert(&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings);
929 }
930 
931 void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa,
932 				  size_t size)
933 {
934 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
935 	struct interval_tree_node *node, *next;
936 	struct kvm_guest_s2_mapping *mapping;
937 	gpa_t nipa_end = nipa + size - 1;
938 
939 	/*
940 	 * See kvm_nested_unmap_cipa_range() for why guest_s2_tracking_lock
941 	 * isn't taken here.
942 	 */
943 	lockdep_assert_held_write(&kvm->mmu_lock);
944 
945 	node = interval_tree_iter_first(&mmu->guest_s2_mappings, nipa, nipa_end);
946 	while (node) {
947 		unsigned long nested_start;
948 
949 		next = interval_tree_iter_next(node, nipa, nipa_end);
950 		mapping = container_of(node, struct kvm_guest_s2_mapping,
951 				       nested);
952 		/*
953 		 * Tracking must be conservative on removal, only remove
954 		 * mappings that are within the unmap range.
955 		 */
956 		nested_start = s2_mapping_to_nested_start(mapping);
957 		if (nipa <= nested_start && nipa_end >= mapping->nested.last) {
958 			interval_tree_remove(&mapping->nested, &mmu->guest_s2_mappings);
959 			interval_tree_remove(&mapping->canonical,
960 					     &kvm->arch.mmu.guest_s2_mappings);
961 			kfree(mapping);
962 		}
963 		node = next;
964 	}
965 }
966 
967 void kvm_init_nested_s2_mmu(struct kvm_s2_mmu *mmu)
968 {
969 	/* CnP being set denotes an invalid entry */
970 	mmu->tlb_vttbr = VTTBR_CNP_BIT;
971 	mmu->nested_stage2_enabled = false;
972 	atomic_set(&mmu->refcnt, 0);
973 }
974 
975 void kvm_vcpu_load_hw_mmu(struct kvm_vcpu *vcpu)
976 {
977 	/*
978 	 * If the vCPU kept its reference on the MMU after the last put,
979 	 * keep rolling with it.
980 	 */
981 	if (is_hyp_ctxt(vcpu)) {
982 		if (!vcpu->arch.hw_mmu)
983 			vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
984 	} else {
985 		if (!vcpu->arch.hw_mmu) {
986 			scoped_guard(write_lock, &vcpu->kvm->mmu_lock)
987 				vcpu->arch.hw_mmu = get_s2_mmu_nested(vcpu);
988 		}
989 
990 		if (__vcpu_sys_reg(vcpu, HCR_EL2) & HCR_EL2_NV)
991 			kvm_make_request(KVM_REQ_MAP_L1_VNCR_EL2, vcpu);
992 	}
993 }
994 
995 /*
996  * Unmapping an L1 VNCR can happen concurrently without the mmu lock being
997  * effective (vcpu_put() vs TLBI handling). The atomic_xchg below ensures
998  * that only one CPU sets it to -1 while getting a valid CPU number back.
999  */
1000 static int unmap_l1_vncr(struct vncr_tlb *vt)
1001 {
1002 	int cpu = atomic_xchg_relaxed(&vt->cpu, -1);
1003 
1004 	if (cpu != -1)
1005 		clear_fixmap(vncr_fixmap(cpu));
1006 
1007 	return cpu;
1008 }
1009 
1010 static void this_cpu_reset_vncr_fixmap(struct kvm_vcpu *vcpu)
1011 {
1012 	if (!host_data_test_flag(L1_VNCR_MAPPED))
1013 		return;
1014 
1015 	BUG_ON(is_hyp_ctxt(vcpu));
1016 
1017 	/*
1018 	 * Unconditionally unmap the local VNCR if we have lost the race
1019 	 * against a concurrent TLBI. Otherwise we could end-up running
1020 	 * another vcpu with VNCR still mapped if the TLBI thread is
1021 	 * preempted between the exchange and the clear_fixmap().
1022 	 *
1023 	 * Note that we do not care about the TLBI nuking the fixmap behind
1024 	 * the back of an running vcpu. This will only generate a fault and
1025 	 * possibly a retranslation.
1026 	 */
1027 	if (unmap_l1_vncr(vcpu->arch.vncr_tlb) == -1)
1028 		clear_fixmap(vncr_fixmap(smp_processor_id()));
1029 	host_data_clear_flag(L1_VNCR_MAPPED);
1030 }
1031 
1032 void kvm_vcpu_put_hw_mmu(struct kvm_vcpu *vcpu)
1033 {
1034 	/* Unconditionally drop the VNCR mapping if we have one */
1035 	this_cpu_reset_vncr_fixmap(vcpu);
1036 
1037 	/*
1038 	 * Keep a reference on the associated stage-2 MMU if the vCPU is
1039 	 * scheduling out and not in WFI emulation, suggesting it is likely to
1040 	 * reuse the MMU sometime soon.
1041 	 */
1042 	if (vcpu->scheduled_out && !vcpu_get_flag(vcpu, IN_WFI))
1043 		return;
1044 
1045 	if (kvm_is_nested_s2_mmu(vcpu->kvm, vcpu->arch.hw_mmu))
1046 		atomic_dec(&vcpu->arch.hw_mmu->refcnt);
1047 
1048 	vcpu->arch.hw_mmu = NULL;
1049 }
1050 
1051 /*
1052  * Returns non-zero if permission fault is handled by injecting it to the next
1053  * level hypervisor.
1054  */
1055 int kvm_s2_handle_perm_fault(struct kvm_vcpu *vcpu, struct kvm_s2_trans *trans)
1056 {
1057 	bool forward_fault = false;
1058 
1059 	trans->esr = 0;
1060 
1061 	if (!kvm_vcpu_trap_is_permission_fault(vcpu))
1062 		return 0;
1063 
1064 	if (kvm_vcpu_trap_is_iabt(vcpu)) {
1065 		if (vcpu_mode_priv(vcpu))
1066 			forward_fault = !kvm_s2_trans_exec_el1(vcpu->kvm, trans);
1067 		else
1068 			forward_fault = !kvm_s2_trans_exec_el0(vcpu->kvm, trans);
1069 	} else {
1070 		bool write_fault = kvm_is_write_fault(vcpu);
1071 
1072 		forward_fault = ((write_fault && !trans->writable) ||
1073 				 (!write_fault && !trans->readable));
1074 	}
1075 
1076 	if (forward_fault)
1077 		trans->esr = esr_s2_fault(vcpu, trans->level, ESR_ELx_FSC_PERM);
1078 
1079 	return forward_fault;
1080 }
1081 
1082 int kvm_inject_s2_fault(struct kvm_vcpu *vcpu, u64 esr_el2)
1083 {
1084 	vcpu_write_sys_reg(vcpu, vcpu->arch.fault.far_el2, FAR_EL2);
1085 	vcpu_write_sys_reg(vcpu, vcpu->arch.fault.hpfar_el2, HPFAR_EL2);
1086 
1087 	return kvm_inject_nested_sync(vcpu, esr_el2);
1088 }
1089 
1090 u16 get_asid_by_regime(struct kvm_vcpu *vcpu, enum trans_regime regime)
1091 {
1092 	enum vcpu_sysreg ttbr_elx;
1093 	u64 tcr;
1094 	u16 asid;
1095 
1096 	switch (regime) {
1097 	case TR_EL10:
1098 		tcr = vcpu_read_sys_reg(vcpu, TCR_EL1);
1099 		ttbr_elx = (tcr & TCR_A1) ? TTBR1_EL1 : TTBR0_EL1;
1100 		break;
1101 	case TR_EL20:
1102 		tcr = vcpu_read_sys_reg(vcpu, TCR_EL2);
1103 		ttbr_elx = (tcr & TCR_A1) ? TTBR1_EL2 : TTBR0_EL2;
1104 		break;
1105 	default:
1106 		BUG();
1107 	}
1108 
1109 	asid = FIELD_GET(TTBRx_EL1_ASID, vcpu_read_sys_reg(vcpu, ttbr_elx));
1110 	if (!kvm_has_feat_enum(vcpu->kvm, ID_AA64MMFR0_EL1, ASIDBITS, 16) ||
1111 	    !(tcr & TCR_ASID16))
1112 		asid &= GENMASK(7, 0);
1113 
1114 	return asid;
1115 }
1116 
1117 static void invalidate_vncr(struct kvm *kvm, struct vncr_tlb *vt)
1118 {
1119 	BUG_ON(!vt->valid);
1120 	vt->valid = false;
1121 	unmap_l1_vncr(vt);
1122 	atomic_dec(&kvm->arch.vncr_tlb_count);
1123 }
1124 
1125 static bool vncr_tlb_intersects(struct vncr_tlb *vt, u64 addr,
1126 				u64 scope_start, u64 scope_size)
1127 {
1128 	u64 tlb_size, tlb_start, tlb_end, scope_end;
1129 
1130 	tlb_size = ttl_to_size(pgshift_level_to_ttl(vt->wi.pgshift, vt->wr.level));
1131 
1132 	tlb_start = addr & ~(tlb_size - 1);
1133 	tlb_end = tlb_start + tlb_size - 1;
1134 	scope_end = scope_start + scope_size - 1;
1135 
1136 	return !(tlb_end < scope_start || tlb_start > scope_end);
1137 }
1138 
1139 /*
1140  * VNCR TLB invalidation occurs from MMU notifiers or TLBI instructions, and
1141  * either can race against a vcpu not being onlined yet (no pseudo-TLB
1142  * allocated). Similarly, the TLB might be invalid.  Skip those, as they
1143  * obviously don't participate in the invalidation at this stage.
1144  */
1145 #define kvm_for_each_vncr_tlb(idx, vcpup, tlbp, kvm)	\
1146 	kvm_for_each_vcpu(idx, vcpup, kvm)		\
1147 		if (((tlbp) = vcpup->arch.vncr_tlb) &&	\
1148 		    (tlbp)->valid)
1149 
1150 static void kvm_invalidate_vncr_ipa(struct kvm *kvm, u64 start, u64 end)
1151 {
1152 	struct kvm_vcpu *vcpu;
1153 	struct vncr_tlb *vt;
1154 	unsigned long i;
1155 
1156 	lockdep_assert_held_write(&kvm->mmu_lock);
1157 
1158 	if (!kvm_has_feat(kvm, ID_AA64MMFR4_EL1, NV_frac, NV2_ONLY))
1159 		return;
1160 
1161 	/*
1162 	 * Note that invalidating the VNCR on the back of an MMU notifier
1163 	 * doesn't require messing with the invalidation counter for a
1164 	 * parallel walk. The notifier itself will have bumped the counter,
1165 	 * making sure we rewalk.
1166 	 */
1167 	kvm_for_each_vncr_tlb(i, vcpu, vt, kvm)
1168 		if (vncr_tlb_intersects(vt, vt->wr.pa, start, end - start))
1169 			invalidate_vncr(kvm, vt);
1170 }
1171 
1172 struct s1e2_tlbi_scope {
1173 	enum {
1174 		TLBI_ALL,
1175 		TLBI_VA,
1176 		TLBI_VAA,
1177 		TLBI_ASID,
1178 	} type;
1179 
1180 	u16 asid;
1181 	u64 va;
1182 	u64 size;
1183 };
1184 
1185 static void invalidate_vncr_va(struct kvm *kvm,
1186 			       struct s1e2_tlbi_scope *scope)
1187 {
1188 	struct kvm_vcpu *vcpu;
1189 	struct vncr_tlb *vt;
1190 	unsigned long i;
1191 
1192 	lockdep_assert_held_write(&kvm->mmu_lock);
1193 
1194 	/*
1195 	 * We might be performing a parallel S1 walk, so bump up the
1196 	 * invalidation counter even in the absence of an actual VNCR TLB
1197 	 * invalidation, as this could indicate that the guest has gone
1198 	 * through a BBM sequence.
1199 	 */
1200 	kvm->mmu_invalidate_seq++;
1201 	smp_wmb();
1202 
1203 	kvm_for_each_vncr_tlb(i, vcpu, vt, kvm) {
1204 		switch (scope->type) {
1205 		case TLBI_ALL:
1206 			break;
1207 
1208 		case TLBI_VA:
1209 			if (!vncr_tlb_intersects(vt, vt->gva, scope->va, scope->size))
1210 				continue;
1211 			if (vt->wr.nG && vt->wr.asid != scope->asid)
1212 				continue;
1213 			break;
1214 
1215 		case TLBI_VAA:
1216 			if (!vncr_tlb_intersects(vt, vt->gva, scope->va, scope->size))
1217 				continue;
1218 			break;
1219 
1220 		case TLBI_ASID:
1221 			if (!vt->wr.nG || vt->wr.asid != scope->asid)
1222 				continue;
1223 			break;
1224 		}
1225 
1226 		invalidate_vncr(kvm, vt);
1227 	}
1228 }
1229 
1230 #define tlbi_va_s1_to_va(v)	(u64)sign_extend64((v) << 12, 48)
1231 
1232 static void compute_s1_tlbi_range(struct kvm_vcpu *vcpu, u32 inst, u64 val,
1233 				  struct s1e2_tlbi_scope *scope)
1234 {
1235 	switch (inst) {
1236 	case OP_TLBI_ALLE2:
1237 	case OP_TLBI_ALLE2IS:
1238 	case OP_TLBI_ALLE2OS:
1239 	case OP_TLBI_VMALLE1:
1240 	case OP_TLBI_VMALLE1IS:
1241 	case OP_TLBI_VMALLE1OS:
1242 	case OP_TLBI_ALLE2NXS:
1243 	case OP_TLBI_ALLE2ISNXS:
1244 	case OP_TLBI_ALLE2OSNXS:
1245 	case OP_TLBI_VMALLE1NXS:
1246 	case OP_TLBI_VMALLE1ISNXS:
1247 	case OP_TLBI_VMALLE1OSNXS:
1248 		scope->type = TLBI_ALL;
1249 		break;
1250 	case OP_TLBI_VAE2:
1251 	case OP_TLBI_VAE2IS:
1252 	case OP_TLBI_VAE2OS:
1253 	case OP_TLBI_VAE1:
1254 	case OP_TLBI_VAE1IS:
1255 	case OP_TLBI_VAE1OS:
1256 	case OP_TLBI_VAE2NXS:
1257 	case OP_TLBI_VAE2ISNXS:
1258 	case OP_TLBI_VAE2OSNXS:
1259 	case OP_TLBI_VAE1NXS:
1260 	case OP_TLBI_VAE1ISNXS:
1261 	case OP_TLBI_VAE1OSNXS:
1262 	case OP_TLBI_VALE2:
1263 	case OP_TLBI_VALE2IS:
1264 	case OP_TLBI_VALE2OS:
1265 	case OP_TLBI_VALE1:
1266 	case OP_TLBI_VALE1IS:
1267 	case OP_TLBI_VALE1OS:
1268 	case OP_TLBI_VALE2NXS:
1269 	case OP_TLBI_VALE2ISNXS:
1270 	case OP_TLBI_VALE2OSNXS:
1271 	case OP_TLBI_VALE1NXS:
1272 	case OP_TLBI_VALE1ISNXS:
1273 	case OP_TLBI_VALE1OSNXS:
1274 		scope->type = TLBI_VA;
1275 		scope->size = ttl_to_size(FIELD_GET(TLBI_TTL_MASK, val));
1276 		scope->va = tlbi_va_s1_to_va(val) & ~(scope->size - 1);
1277 		scope->asid = FIELD_GET(TLBIR_ASID_MASK, val);
1278 		break;
1279 	case OP_TLBI_ASIDE1:
1280 	case OP_TLBI_ASIDE1IS:
1281 	case OP_TLBI_ASIDE1OS:
1282 	case OP_TLBI_ASIDE1NXS:
1283 	case OP_TLBI_ASIDE1ISNXS:
1284 	case OP_TLBI_ASIDE1OSNXS:
1285 		scope->type = TLBI_ASID;
1286 		scope->asid = FIELD_GET(TLBIR_ASID_MASK, val);
1287 		break;
1288 	case OP_TLBI_VAAE1:
1289 	case OP_TLBI_VAAE1IS:
1290 	case OP_TLBI_VAAE1OS:
1291 	case OP_TLBI_VAAE1NXS:
1292 	case OP_TLBI_VAAE1ISNXS:
1293 	case OP_TLBI_VAAE1OSNXS:
1294 	case OP_TLBI_VAALE1:
1295 	case OP_TLBI_VAALE1IS:
1296 	case OP_TLBI_VAALE1OS:
1297 	case OP_TLBI_VAALE1NXS:
1298 	case OP_TLBI_VAALE1ISNXS:
1299 	case OP_TLBI_VAALE1OSNXS:
1300 		scope->type = TLBI_VAA;
1301 		scope->size = ttl_to_size(FIELD_GET(TLBI_TTL_MASK, val));
1302 		scope->va = tlbi_va_s1_to_va(val) & ~(scope->size - 1);
1303 		break;
1304 	case OP_TLBI_RVAE2:
1305 	case OP_TLBI_RVAE2IS:
1306 	case OP_TLBI_RVAE2OS:
1307 	case OP_TLBI_RVAE1:
1308 	case OP_TLBI_RVAE1IS:
1309 	case OP_TLBI_RVAE1OS:
1310 	case OP_TLBI_RVAE2NXS:
1311 	case OP_TLBI_RVAE2ISNXS:
1312 	case OP_TLBI_RVAE2OSNXS:
1313 	case OP_TLBI_RVAE1NXS:
1314 	case OP_TLBI_RVAE1ISNXS:
1315 	case OP_TLBI_RVAE1OSNXS:
1316 	case OP_TLBI_RVALE2:
1317 	case OP_TLBI_RVALE2IS:
1318 	case OP_TLBI_RVALE2OS:
1319 	case OP_TLBI_RVALE1:
1320 	case OP_TLBI_RVALE1IS:
1321 	case OP_TLBI_RVALE1OS:
1322 	case OP_TLBI_RVALE2NXS:
1323 	case OP_TLBI_RVALE2ISNXS:
1324 	case OP_TLBI_RVALE2OSNXS:
1325 	case OP_TLBI_RVALE1NXS:
1326 	case OP_TLBI_RVALE1ISNXS:
1327 	case OP_TLBI_RVALE1OSNXS:
1328 		scope->type = TLBI_VA;
1329 		scope->va = decode_range_tlbi(val, &scope->size, &scope->asid);
1330 		break;
1331 	case OP_TLBI_RVAAE1:
1332 	case OP_TLBI_RVAAE1IS:
1333 	case OP_TLBI_RVAAE1OS:
1334 	case OP_TLBI_RVAAE1NXS:
1335 	case OP_TLBI_RVAAE1ISNXS:
1336 	case OP_TLBI_RVAAE1OSNXS:
1337 	case OP_TLBI_RVAALE1:
1338 	case OP_TLBI_RVAALE1IS:
1339 	case OP_TLBI_RVAALE1OS:
1340 	case OP_TLBI_RVAALE1NXS:
1341 	case OP_TLBI_RVAALE1ISNXS:
1342 	case OP_TLBI_RVAALE1OSNXS:
1343 		scope->type = TLBI_VAA;
1344 		scope->va = decode_range_tlbi(val, &scope->size, NULL);
1345 		break;
1346 	}
1347 }
1348 
1349 void kvm_handle_s1e2_tlbi(struct kvm_vcpu *vcpu, u32 inst, u64 val)
1350 {
1351 	struct s1e2_tlbi_scope scope = {};
1352 
1353 	compute_s1_tlbi_range(vcpu, inst, val, &scope);
1354 
1355 	guard(write_lock)(&vcpu->kvm->mmu_lock);
1356 	invalidate_vncr_va(vcpu->kvm, &scope);
1357 }
1358 
1359 static void kvm_invalidate_vncr_ipa_all(struct kvm *kvm)
1360 {
1361 	struct kvm_pgtable *pgt = kvm->arch.mmu.pgt;
1362 
1363 	lockdep_assert_held_write(&kvm->mmu_lock);
1364 
1365 	/* if the mmu lock was dropped, pgt teardown may have raced. */
1366 	if (pgt)
1367 		kvm_invalidate_vncr_ipa(kvm, 0, BIT(pgt->ia_bits));
1368 }
1369 
1370 void kvm_nested_s2_wp(struct kvm *kvm)
1371 {
1372 	int i;
1373 
1374 	lockdep_assert_held_write(&kvm->mmu_lock);
1375 
1376 	if (!kvm->arch.nested_mmus_size)
1377 		return;
1378 
1379 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
1380 		struct kvm_s2_mmu *mmu = kvm->arch.nested_mmus[i];
1381 
1382 		if (kvm_s2_mmu_valid(mmu))
1383 			kvm_stage2_wp_range(mmu, 0, kvm_phys_size(mmu));
1384 	}
1385 
1386 	kvm_invalidate_vncr_ipa_all(kvm);
1387 }
1388 
1389 void kvm_nested_unmap_cipa_range(struct kvm *kvm, gpa_t cipa, size_t unmap_size,
1390 				 bool may_block)
1391 {
1392 	gpa_t cipa_end = cipa + unmap_size - 1;
1393 	struct kvm_guest_s2_mapping *mapping;
1394 	struct interval_tree_node *node;
1395 	size_t mapping_size;
1396 
1397 	/*
1398 	 * Guest s2 tracking interval trees are only accessed while holding the
1399 	 * mmu_lock, hence we don't have to take guest_s2_tracking_lock if the
1400 	 * mmu_lock is held for write. This saves us from having to manually
1401 	 * lock/unlock guest_s2_tracking_lock below around
1402 	 * cond_resched_rwlock_write().
1403 	 */
1404 	lockdep_assert_held_write(&kvm->mmu_lock);
1405 
1406 	if (!kvm->arch.nested_mmus_size)
1407 		return;
1408 
1409 	while ((node = interval_tree_iter_first(&kvm->arch.mmu.guest_s2_mappings,
1410 						cipa, cipa_end))) {
1411 		unsigned long nested_start;
1412 		struct kvm_s2_mmu *mmu;
1413 
1414 		mapping = container_of(node, struct kvm_guest_s2_mapping,
1415 				       canonical);
1416 		nested_start = s2_mapping_to_nested_start(mapping);
1417 		mmu = s2_mapping_to_mmu(kvm, mapping);
1418 
1419 		/* We could race against MMU teardown, which frees mmu->pgt. */
1420 		if (mmu->pgt) {
1421 			mapping_size = mapping->nested.last - nested_start + 1;
1422 
1423 			if (WARN_ON_ONCE(kvm_pgtable_stage2_unmap(mmu->pgt, nested_start,
1424 								  mapping_size)))
1425 				return;
1426 
1427 			interval_tree_remove(&mapping->nested, &mmu->guest_s2_mappings);
1428 		}
1429 		interval_tree_remove(node, &kvm->arch.mmu.guest_s2_mappings);
1430 		kfree(mapping);
1431 
1432 		if (may_block)
1433 			cond_resched_rwlock_write(&kvm->mmu_lock);
1434 	}
1435 
1436 	kvm_invalidate_vncr_ipa(kvm, cipa, cipa + unmap_size);
1437 }
1438 
1439 void kvm_nested_s2_unmap(struct kvm *kvm, bool may_block)
1440 {
1441 	int i;
1442 
1443 	lockdep_assert_held_write(&kvm->mmu_lock);
1444 
1445 	if (!kvm->arch.nested_mmus_size)
1446 		return;
1447 
1448 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
1449 		struct kvm_s2_mmu *mmu = kvm->arch.nested_mmus[i];
1450 
1451 		if (kvm_s2_mmu_valid(mmu))
1452 			kvm_stage2_unmap_range(mmu, 0, kvm_phys_size(mmu), may_block);
1453 	}
1454 
1455 	kvm_invalidate_vncr_ipa_all(kvm);
1456 }
1457 
1458 void kvm_nested_s2_flush(struct kvm *kvm)
1459 {
1460 	int i;
1461 
1462 	lockdep_assert_held_write(&kvm->mmu_lock);
1463 
1464 	if (!kvm->arch.nested_mmus_size)
1465 		return;
1466 
1467 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
1468 		struct kvm_s2_mmu *mmu = kvm->arch.nested_mmus[i];
1469 
1470 		if (kvm_s2_mmu_valid(mmu))
1471 			kvm_stage2_flush_range(mmu, 0, kvm_phys_size(mmu));
1472 	}
1473 }
1474 
1475 void kvm_arch_flush_shadow_all(struct kvm *kvm)
1476 {
1477 	for (int i = 0; i < kvm->arch.nested_mmus_size; i++) {
1478 		struct kvm_s2_mmu *mmu = kvm->arch.nested_mmus[i];
1479 
1480 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
1481 			kvm_free_stage2_pgd(mmu);
1482 	}
1483 	kvm_uninit_stage2_mmu(kvm);
1484 }
1485 
1486 /*
1487  * Dealing with VNCR_EL2 exposed by the *guest* is a complicated matter:
1488  *
1489  * - We introduce an internal representation of a vcpu-private TLB,
1490  *   representing the mapping between the guest VA contained in VNCR_EL2,
1491  *   the IPA the guest's EL2 PTs point to, and the actual PA this lives at.
1492  *
1493  * - On translation fault from a nested VNCR access, we create such a TLB.
1494  *   If there is no mapping to describe, the guest inherits the fault.
1495  *   Crucially, no actual mapping is done at this stage.
1496  *
1497  * - On vcpu_load() in a non-HYP context with HCR_EL2.NV==1, if the above
1498  *   TLB exists, we map it in the fixmap for this CPU, and run with it. We
1499  *   have to respect the permissions dictated by the guest, but not the
1500  *   memory type (FWB is a must).
1501  *
1502  * - Note that we usually don't do a vcpu_load() on the back of a fault
1503  *   (unless we are preempted), so the resolution of a translation fault
1504  *   must go via a request that will map the VNCR page in the fixmap.
1505  *   vcpu_load() might as well use the same mechanism.
1506  *
1507  * - On vcpu_put() in a non-HYP context with HCR_EL2.NV==1, if the TLB was
1508  *   mapped, we unmap it. Yes it is that simple. The TLB still exists
1509  *   though, and may be reused at a later load.
1510  *
1511  * - On permission fault, we simply forward the fault to the guest's EL2.
1512  *   Get out of my way.
1513  *
1514  * - On any TLBI for the EL2&0 translation regime, we must find any TLB that
1515  *   intersects with the TLBI request, invalidate it, and unmap the page
1516  *   from the fixmap. Because we need to look at all the vcpu-private TLBs,
1517  *   this requires some wide-ranging locking to ensure that nothing races
1518  *   against it. This requires some refcounting to avoid the search when
1519  *   no such TLB is present (see below).
1520  *
1521  * - On MMU notifiers, we must invalidate our TLB in a similar way, but
1522  *   looking at the IPA instead. The funny part is that there may not be a
1523  *   stage-2 mapping for this page if L1 hasn't accessed it using LD/ST
1524  *   instructions.
1525  *
1526  * - vncr_tlb_count tracks the number of valid VNCR TLBs VM-wide. This isn't
1527  *   the number of *mapped* L1 VNCR pages, which is likely be a subset (and
1528  *   by definition, a TLBI handled from L1 runs with the canonical VNCR
1529  *   page, not the L1's). The innermost trap handling code checks this to
1530  *   find out whether to return to the guest ASAP (no L1 TLBs) or to visit
1531  *   this part of the world for some extra invalidation work.
1532  */
1533 
1534 int kvm_vcpu_allocate_vncr_tlb(struct kvm_vcpu *vcpu)
1535 {
1536 	if (!kvm_has_feat(vcpu->kvm, ID_AA64MMFR4_EL1, NV_frac, NV2_ONLY))
1537 		return 0;
1538 
1539 	if (!vcpu->arch.vncr_tlb) {
1540 		struct vncr_tlb *vt = kzalloc_obj(*vcpu->arch.vncr_tlb,
1541 						  GFP_KERNEL_ACCOUNT);
1542 
1543 		/*
1544 		 * Taking the lock on assignment ensures that the TLB is
1545 		 * seen as initialised when following the pointer (release
1546 		 * semantics of the unlock), and avoids having acquires on
1547 		 * each user which already take the lock.
1548 		 */
1549 		scoped_guard(write_lock, &vcpu->kvm->mmu_lock)
1550 			vcpu->arch.vncr_tlb = vt;
1551 	}
1552 
1553 	if (!vcpu->arch.vncr_tlb)
1554 		return -ENOMEM;
1555 
1556 	return 0;
1557 }
1558 
1559 static u64 read_vncr_el2(struct kvm_vcpu *vcpu)
1560 {
1561 	return (u64)sign_extend64(__vcpu_sys_reg(vcpu, VNCR_EL2), 48);
1562 }
1563 
1564 static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
1565 {
1566 	struct kvm_memory_slot *memslot;
1567 	bool write_fault, writable;
1568 	unsigned long mmu_seq;
1569 	struct vncr_tlb *vt;
1570 	struct page *page = NULL;
1571 	u64 va, pfn, gfn;
1572 	int ret;
1573 
1574 	vt = vcpu->arch.vncr_tlb;
1575 
1576 	/*
1577 	 * If we're about to walk the EL2 S1 PTs, we must invalidate the
1578 	 * current TLB, as it could be sampled from another vcpu doing a
1579 	 * TLBI *IS. A real CPU wouldn't do that, but we only keep a single
1580 	 * translation, so not much of a choice.
1581 	 *
1582 	 * We also prepare the next walk wilst we're at it.
1583 	 */
1584 	scoped_guard(write_lock, &vcpu->kvm->mmu_lock) {
1585 		this_cpu_reset_vncr_fixmap(vcpu);
1586 		if (vt->valid)
1587 			invalidate_vncr(vcpu->kvm, vt);
1588 
1589 		vt->wi = (struct s1_walk_info) {
1590 			.regime	= TR_EL20,
1591 			.as_el0	= false,
1592 			.pan	= false,
1593 		};
1594 		vt->wr = (struct s1_walk_result){};
1595 	}
1596 
1597 	guard(srcu)(&vcpu->kvm->srcu);
1598 
1599 	va =  read_vncr_el2(vcpu);
1600 
1601 	mmu_seq = vcpu->kvm->mmu_invalidate_seq;
1602 	smp_rmb();
1603 
1604 	ret = __kvm_translate_va(vcpu, &vt->wi, &vt->wr, va);
1605 	if (ret)
1606 		return ret;
1607 
1608 	write_fault = kvm_is_write_fault(vcpu);
1609 
1610 	gfn = vt->wr.pa >> PAGE_SHIFT;
1611 	memslot = gfn_to_memslot(vcpu->kvm, gfn);
1612 	if (!memslot) {
1613 		fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
1614 		return -EFAULT;
1615 	}
1616 
1617 	*is_gmem = kvm_slot_has_gmem(memslot);
1618 	if (!*is_gmem) {
1619 		pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0,
1620 					&writable, &page);
1621 		if (is_error_noslot_pfn(pfn)) {
1622 			fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
1623 			return -EFAULT;
1624 		}
1625 	} else {
1626 		ret = kvm_gmem_get_pfn(vcpu->kvm, memslot, gfn, &pfn, NULL);
1627 		if (ret) {
1628 			kvm_prepare_memory_fault_exit(vcpu, vt->wr.pa, PAGE_SIZE,
1629 					      write_fault, false, false);
1630 			return ret;
1631 		}
1632 
1633 		writable = !(memslot->flags & KVM_MEM_READONLY);
1634 	}
1635 
1636 	/*
1637 	 * FIXME: This check is too restrictive as KVM allows cacheable memory
1638 	 * attributes for PFNMAP VMAs that have cacheable attributes in host
1639 	 * stage-1.
1640 	 */
1641 	if (!pfn_is_map_memory(pfn)) {
1642 		kvm_release_faultin_page(vcpu->kvm, page, true, false);
1643 		fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
1644 		return -EINVAL;
1645 	}
1646 
1647 	scoped_guard(write_lock, &vcpu->kvm->mmu_lock) {
1648 		if (mmu_invalidate_retry(vcpu->kvm, mmu_seq)) {
1649 			kvm_release_faultin_page(vcpu->kvm, page, true, false);
1650 			return -EAGAIN;
1651 		}
1652 
1653 		vt->gva = va;
1654 		vt->hpa = pfn << PAGE_SHIFT;
1655 		vt->hpa_writable = writable;
1656 		vt->valid = true;
1657 		atomic_set(&vt->cpu, -1);
1658 
1659 		kvm_make_request(KVM_REQ_MAP_L1_VNCR_EL2, vcpu);
1660 		kvm_release_faultin_page(vcpu->kvm, page, false, vt->wr.pw && vt->hpa_writable);
1661 	}
1662 
1663 	if (vt->wr.pw && vt->hpa_writable)
1664 		mark_page_dirty(vcpu->kvm, gfn);
1665 
1666 	return 0;
1667 }
1668 
1669 static void handle_vncr_perm(struct kvm_vcpu *vcpu)
1670 {
1671 	struct vncr_tlb *vt = vcpu->arch.vncr_tlb;
1672 	u64 esr = kvm_vcpu_get_esr(vcpu);
1673 	u64 fsc;
1674 
1675 	/*
1676 	 * Promote to an external abort if the stage-1 permits writes but the
1677 	 * HPA is read-only (e.g. RO memslot).
1678 	 */
1679 	if (kvm_is_write_fault(vcpu) && vt->wr.pw && !vt->hpa_writable)
1680 		fsc = ESR_ELx_FSC_EXTABT;
1681 	/*
1682 	 * Otherwise, inject a permission fault using the guest's translation
1683 	 * level rather than the host's.
1684 	 */
1685 	else
1686 		fsc = ESR_ELx_FSC_PERM_L(vt->wr.level);
1687 
1688 	esr &= ~ESR_ELx_FSC;
1689 	esr |= FIELD_PREP(ESR_ELx_FSC, fsc);
1690 
1691 	kvm_inject_nested_sync(vcpu, esr);
1692 }
1693 
1694 int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu)
1695 {
1696 	struct vncr_tlb *vt = vcpu->arch.vncr_tlb;
1697 	u64 esr = kvm_vcpu_get_esr(vcpu);
1698 	bool is_gmem = false;
1699 	bool perm;
1700 	int ret;
1701 
1702 	WARN_ON_ONCE(!(esr & ESR_ELx_VNCR));
1703 
1704 	if (kvm_vcpu_abt_issea(vcpu))
1705 		return kvm_handle_guest_sea(vcpu);
1706 
1707 	if (!esr_fsc_is_translation_fault(esr) && !esr_fsc_is_permission_fault(esr)) {
1708 		KVM_BUG(1, vcpu->kvm, "Unhandled VNCR abort, ESR=%llx\n", esr);
1709 		return -EIO;
1710 	}
1711 
1712 	/*
1713 	 * Speculatively increment the TLB count to make sure concurrent
1714 	 * TLBIs will take the slow path, and will interact with the retry
1715 	 * mechanism. Drop it again on error.
1716 	 */
1717 	atomic_inc(&vcpu->kvm->arch.vncr_tlb_count);
1718 	smp_mb__after_atomic();
1719 
1720 	ret = kvm_translate_vncr(vcpu, &is_gmem);
1721 	if (ret) {
1722 		smp_mb__before_atomic();
1723 		atomic_dec(&vcpu->kvm->arch.vncr_tlb_count);
1724 	}
1725 
1726 	switch (ret) {
1727 	case -EAGAIN:
1728 		/* Let's try again... */
1729 		return 1;
1730 	case -ENOMEM:
1731 		/*
1732 		 * For guest_memfd, this indicates that it failed to
1733 		 * create a folio to back the memory. Inform userspace.
1734 		 */
1735 		if (is_gmem)
1736 			return 0;
1737 		/* Otherwise, let's try again... */
1738 		break;
1739 	case -EFAULT:
1740 	case -EIO:
1741 	case -EHWPOISON:
1742 		if (is_gmem)
1743 			return 0;
1744 		fallthrough;
1745 	case -EINVAL:
1746 	case -ENOENT:
1747 	case -EACCES:
1748 		/*
1749 		 * Translation failed, inject the corresponding
1750 		 * exception back to EL2.
1751 		 */
1752 		esr &= ~ESR_ELx_FSC;
1753 		esr |= FIELD_PREP(ESR_ELx_FSC, vt->wr.fst);
1754 
1755 		kvm_inject_nested_sync(vcpu, esr);
1756 		break;
1757 	case 0:
1758 		perm = kvm_is_write_fault(vcpu) ? vt->wr.pw && vt->hpa_writable : vt->wr.pr;
1759 		if (!perm)
1760 			handle_vncr_perm(vcpu);
1761 		break;
1762 	}
1763 
1764 	return 1;
1765 }
1766 
1767 static void kvm_map_l1_vncr(struct kvm_vcpu *vcpu)
1768 {
1769 	struct vncr_tlb *vt = vcpu->arch.vncr_tlb;
1770 	pgprot_t prot;
1771 
1772 	guard(preempt)();
1773 	guard(read_lock)(&vcpu->kvm->mmu_lock);
1774 
1775 	/*
1776 	 * The request to map VNCR may have raced against some other
1777 	 * event, such as an interrupt, and may not be valid anymore.
1778 	 */
1779 	if (is_hyp_ctxt(vcpu))
1780 		return;
1781 
1782 	/*
1783 	 * Check that the pseudo-TLB is valid and that VNCR_EL2 still
1784 	 * contains the expected value. If it doesn't, we simply bail out
1785 	 * without a mapping -- a transformed MSR/MRS will generate the
1786 	 * fault and allows us to populate the pseudo-TLB.
1787 	 */
1788 	if (!vt->valid)
1789 		return;
1790 
1791 	/* We cache the MMU state in the TLB. Check that it matches. */
1792 	if (!!(vcpu_read_sys_reg(vcpu, SCTLR_EL2) & SCTLR_ELx_M) != s1_walk_translated(&vt->wr))
1793 		return;
1794 
1795 	if (read_vncr_el2(vcpu) != vt->gva)
1796 		return;
1797 
1798 	if (vt->wr.nG && get_asid_by_regime(vcpu, TR_EL20) != vt->wr.asid)
1799 		return;
1800 
1801 	if (vt->hpa_writable && vt->wr.pw && vt->wr.pr)
1802 		prot = PAGE_KERNEL;
1803 	else if (vt->wr.pr)
1804 		prot = PAGE_KERNEL_RO;
1805 	else
1806 		prot = PAGE_NONE;
1807 
1808 	/*
1809 	 * We can't map write-only (or no permission at all) in the kernel,
1810 	 * but the guest can do it if using POE, so we'll have to turn a
1811 	 * translation fault into a permission fault at runtime.
1812 	 * FIXME: WO doesn't work at all, need POE support in the kernel.
1813 	 */
1814 	if (pgprot_val(prot) != pgprot_val(PAGE_NONE)) {
1815 		atomic_set(&vt->cpu, smp_processor_id());
1816 		__set_fixmap(vncr_fixmap(atomic_read(&vt->cpu)), vt->hpa, prot);
1817 		host_data_set_flag(L1_VNCR_MAPPED);
1818 	}
1819 }
1820 
1821 /*
1822  * Our emulated CPU doesn't support all the possible features. For the
1823  * sake of simplicity (and probably mental sanity), wipe out a number
1824  * of feature bits we don't intend to support for the time being.
1825  * This list should get updated as new features get added to the NV
1826  * support, and new extension to the architecture.
1827  */
1828 u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val)
1829 {
1830 	u64 orig_val = val;
1831 
1832 	switch (reg) {
1833 	case SYS_ID_AA64ISAR1_EL1:
1834 		/* Support everything but LS64 and Spec Invalidation */
1835 		val &= ~(ID_AA64ISAR1_EL1_LS64	|
1836 			 ID_AA64ISAR1_EL1_SPECRES);
1837 		break;
1838 
1839 	case SYS_ID_AA64PFR0_EL1:
1840 		/* No RME, AMU, MPAM, or S-EL2 */
1841 		val &= ~(ID_AA64PFR0_EL1_RME	|
1842 			 ID_AA64PFR0_EL1_AMU	|
1843 			 ID_AA64PFR0_EL1_MPAM	|
1844 			 ID_AA64PFR0_EL1_SEL2	|
1845 			 ID_AA64PFR0_EL1_EL3	|
1846 			 ID_AA64PFR0_EL1_EL2	|
1847 			 ID_AA64PFR0_EL1_EL1	|
1848 			 ID_AA64PFR0_EL1_EL0);
1849 		/* 64bit only at any EL */
1850 		val |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, EL0, IMP);
1851 		val |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, EL1, IMP);
1852 		val |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, EL2, IMP);
1853 		val |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, EL3, IMP);
1854 		break;
1855 
1856 	case SYS_ID_AA64PFR1_EL1:
1857 		/* Only support BTI, SSBS, CSV2_frac */
1858 		val &= ~(ID_AA64PFR1_EL1_PFAR		|
1859 			 ID_AA64PFR1_EL1_MTEX		|
1860 			 ID_AA64PFR1_EL1_THE		|
1861 			 ID_AA64PFR1_EL1_GCS		|
1862 			 ID_AA64PFR1_EL1_MTE_frac	|
1863 			 ID_AA64PFR1_EL1_NMI		|
1864 			 ID_AA64PFR1_EL1_SME		|
1865 			 ID_AA64PFR1_EL1_RES0		|
1866 			 ID_AA64PFR1_EL1_MPAM_frac	|
1867 			 ID_AA64PFR1_EL1_MTE);
1868 		break;
1869 
1870 	case SYS_ID_AA64PFR2_EL1:
1871 		/* GICv5 is not yet supported for NV */
1872 		val &= ~ID_AA64PFR2_EL1_GCIE;
1873 		break;
1874 
1875 	case SYS_ID_AA64MMFR0_EL1:
1876 		/* Hide ExS, Secure Memory */
1877 		val &= ~(ID_AA64MMFR0_EL1_EXS		|
1878 			 ID_AA64MMFR0_EL1_TGRAN4_2	|
1879 			 ID_AA64MMFR0_EL1_TGRAN16_2	|
1880 			 ID_AA64MMFR0_EL1_TGRAN64_2	|
1881 			 ID_AA64MMFR0_EL1_SNSMEM);
1882 
1883 		/* Hide CNTPOFF if present */
1884 		val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64MMFR0_EL1, ECV, IMP);
1885 
1886 		/* NV does not support FEAT_LPA2. */
1887 		if (SYS_FIELD_GET(ID_AA64MMFR0_EL1, TGRAN4, val) == ID_AA64MMFR0_EL1_TGRAN4_52_BIT) {
1888 			val &= ~ID_AA64MMFR0_EL1_TGRAN4;
1889 			val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR0_EL1, TGRAN4, IMP);
1890 		}
1891 
1892 		if (SYS_FIELD_GET(ID_AA64MMFR0_EL1, TGRAN16, val) == ID_AA64MMFR0_EL1_TGRAN16_52_BIT) {
1893 			val &= ~ID_AA64MMFR0_EL1_TGRAN16;
1894 			val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR0_EL1, TGRAN16, IMP);
1895 		}
1896 
1897 		/* Disallow unsupported S2 page sizes */
1898 		switch (PAGE_SIZE) {
1899 		case SZ_64K:
1900 			val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR0_EL1, TGRAN16_2, NI);
1901 			fallthrough;
1902 		case SZ_16K:
1903 			val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR0_EL1, TGRAN4_2, NI);
1904 			fallthrough;
1905 		case SZ_4K:
1906 			/* Support everything */
1907 			break;
1908 		}
1909 
1910 		/*
1911 		 * Since we can't support a guest S2 page size smaller
1912 		 * than the host's own page size (due to KVM only
1913 		 * populating its own S2 using the kernel's page
1914 		 * size), advertise the limitation using FEAT_GTG.
1915 		 */
1916 		switch (PAGE_SIZE) {
1917 		case SZ_4K:
1918 			if (_has_tgran_2(orig_val, 4))
1919 				val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR0_EL1, TGRAN4_2, IMP);
1920 			fallthrough;
1921 		case SZ_16K:
1922 			if (_has_tgran_2(orig_val, 16))
1923 				val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR0_EL1, TGRAN16_2, IMP);
1924 			fallthrough;
1925 		case SZ_64K:
1926 			if (_has_tgran_2(orig_val, 64))
1927 				val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR0_EL1, TGRAN64_2, IMP);
1928 			break;
1929 		}
1930 
1931 		/* Cap PARange to 48bits */
1932 		val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64MMFR0_EL1, PARANGE, 48);
1933 		break;
1934 
1935 	case SYS_ID_AA64MMFR1_EL1:
1936 		val &= ~(ID_AA64MMFR1_EL1_CMOW		|
1937 			 ID_AA64MMFR1_EL1_nTLBPA	|
1938 			 ID_AA64MMFR1_EL1_ETS);
1939 
1940 		/* FEAT_E2H0 implies no VHE */
1941 		if (test_bit(KVM_ARM_VCPU_HAS_EL2_E2H0, kvm->arch.vcpu_features))
1942 			val &= ~ID_AA64MMFR1_EL1_VH;
1943 
1944 		val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64MMFR1_EL1, HAFDBS, AF);
1945 		break;
1946 
1947 	case SYS_ID_AA64MMFR2_EL1:
1948 		val &= ~(ID_AA64MMFR2_EL1_BBM	|
1949 			 ID_AA64MMFR2_EL1_TTL	|
1950 			 GENMASK_ULL(47, 44)	|
1951 			 ID_AA64MMFR2_EL1_ST	|
1952 			 ID_AA64MMFR2_EL1_CCIDX	|
1953 			 ID_AA64MMFR2_EL1_VARange);
1954 
1955 		/* Force TTL support */
1956 		val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR2_EL1, TTL, IMP);
1957 		break;
1958 
1959 	case SYS_ID_AA64MMFR4_EL1:
1960 		/*
1961 		 * You get EITHER
1962 		 *
1963 		 * - FEAT_VHE without FEAT_E2H0
1964 		 * - FEAT_NV limited to FEAT_NV2(p1)/NV3
1965 		 * - HCR_EL2.NV1 being RES0
1966 		 *
1967 		 * OR
1968 		 *
1969 		 * - FEAT_E2H0 without FEAT_VHE nor FEAT_NV
1970 		 *
1971 		 * Life is too short for anything else.
1972 		 */
1973 		if (test_bit(KVM_ARM_VCPU_HAS_EL2_E2H0, kvm->arch.vcpu_features)) {
1974 			val = 0;
1975 		} else {
1976 			val &= ID_AA64MMFR4_EL1_NV_frac;
1977 			if (cpus_have_final_cap(ARM64_HAS_NV3))
1978 				val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64MMFR4_EL1, NV_frac, NV3);
1979 			else if (cpus_have_final_cap(ARM64_HAS_NV2P1))
1980 				val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64MMFR4_EL1, NV_frac, NV2P1);
1981 			else
1982 				val = SYS_FIELD_PREP_ENUM(ID_AA64MMFR4_EL1, NV_frac, NV2_ONLY);
1983 			val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR4_EL1, E2H0, NI_NV1);
1984 		}
1985 		break;
1986 
1987 	case SYS_ID_AA64DFR0_EL1:
1988 		/* Only limited support for PMU, Debug, BPs, WPs, and HPMN0 */
1989 		val &= ~(ID_AA64DFR0_EL1_ExtTrcBuff	|
1990 			 ID_AA64DFR0_EL1_BRBE		|
1991 			 ID_AA64DFR0_EL1_MTPMU		|
1992 			 ID_AA64DFR0_EL1_TraceBuffer	|
1993 			 ID_AA64DFR0_EL1_TraceFilt	|
1994 			 ID_AA64DFR0_EL1_PMSVer		|
1995 			 ID_AA64DFR0_EL1_CTX_CMPs	|
1996 			 ID_AA64DFR0_EL1_SEBEP		|
1997 			 ID_AA64DFR0_EL1_PMSS		|
1998 			 ID_AA64DFR0_EL1_TraceVer);
1999 
2000 		/*
2001 		 * FEAT_Debugv8p9 requires support for extended breakpoints /
2002 		 * watchpoints.
2003 		 */
2004 		val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64DFR0_EL1, DebugVer, V8P8);
2005 		break;
2006 	}
2007 
2008 	return val;
2009 }
2010 
2011 u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *vcpu,
2012 			     enum vcpu_sysreg sr, u64 v)
2013 {
2014 	struct resx resx;
2015 
2016 	resx = kvm_get_sysreg_resx(vcpu->kvm, sr);
2017 	v &= ~resx.res0;
2018 	v |= resx.res1;
2019 
2020 	return v;
2021 }
2022 
2023 static __always_inline void set_sysreg_masks(struct kvm *kvm, int sr, struct resx resx)
2024 {
2025 	BUILD_BUG_ON(!__builtin_constant_p(sr));
2026 	BUILD_BUG_ON(sr < __SANITISED_REG_START__);
2027 	BUILD_BUG_ON(sr >= NR_SYS_REGS);
2028 
2029 	kvm_set_sysreg_resx(kvm, sr, resx);
2030 }
2031 
2032 int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu)
2033 {
2034 	struct kvm *kvm = vcpu->kvm;
2035 	struct resx resx;
2036 
2037 	lockdep_assert_held(&kvm->arch.config_lock);
2038 
2039 	if (kvm->arch.sysreg_masks)
2040 		goto out;
2041 
2042 	kvm->arch.sysreg_masks = kzalloc_obj(*(kvm->arch.sysreg_masks),
2043 					     GFP_KERNEL_ACCOUNT);
2044 	if (!kvm->arch.sysreg_masks)
2045 		return -ENOMEM;
2046 
2047 	/* VTTBR_EL2 */
2048 	resx = (typeof(resx)){};
2049 	if (!kvm_has_feat_enum(kvm, ID_AA64MMFR1_EL1, VMIDBits, 16))
2050 		resx.res0 |= GENMASK(63, 56);
2051 	if (!kvm_has_feat(kvm, ID_AA64MMFR2_EL1, CnP, IMP))
2052 		resx.res0 |= VTTBR_CNP_BIT;
2053 	set_sysreg_masks(kvm, VTTBR_EL2, resx);
2054 
2055 	/* VTCR_EL2 */
2056 	resx = get_reg_fixed_bits(kvm, VTCR_EL2);
2057 	set_sysreg_masks(kvm, VTCR_EL2, resx);
2058 
2059 	/* VMPIDR_EL2 */
2060 	resx.res0 = GENMASK(63, 40) | GENMASK(30, 24);
2061 	resx.res1 = BIT(31);
2062 	set_sysreg_masks(kvm, VMPIDR_EL2, resx);
2063 
2064 	/* HCR_EL2 */
2065 	resx = get_reg_fixed_bits(kvm, HCR_EL2);
2066 	set_sysreg_masks(kvm, HCR_EL2, resx);
2067 
2068 	/* NVHCR_EL2 */
2069 	resx = get_reg_fixed_bits(kvm, NVHCR_EL2);
2070 	set_sysreg_masks(kvm, NVHCR_EL2, resx);
2071 
2072 	/* HCRX_EL2 */
2073 	resx = get_reg_fixed_bits(kvm, HCRX_EL2);
2074 	set_sysreg_masks(kvm, HCRX_EL2, resx);
2075 
2076 	/* HFG[RW]TR_EL2 */
2077 	resx = get_reg_fixed_bits(kvm, HFGRTR_EL2);
2078 	set_sysreg_masks(kvm, HFGRTR_EL2, resx);
2079 	resx = get_reg_fixed_bits(kvm, HFGWTR_EL2);
2080 	set_sysreg_masks(kvm, HFGWTR_EL2, resx);
2081 
2082 	/* HDFG[RW]TR_EL2 */
2083 	resx = get_reg_fixed_bits(kvm, HDFGRTR_EL2);
2084 	set_sysreg_masks(kvm, HDFGRTR_EL2, resx);
2085 	resx = get_reg_fixed_bits(kvm, HDFGWTR_EL2);
2086 	set_sysreg_masks(kvm, HDFGWTR_EL2, resx);
2087 
2088 	/* HFGITR_EL2 */
2089 	resx = get_reg_fixed_bits(kvm, HFGITR_EL2);
2090 	set_sysreg_masks(kvm, HFGITR_EL2, resx);
2091 
2092 	/* HAFGRTR_EL2 - not a lot to see here */
2093 	resx = get_reg_fixed_bits(kvm, HAFGRTR_EL2);
2094 	set_sysreg_masks(kvm, HAFGRTR_EL2, resx);
2095 
2096 	/* HFG[RW]TR2_EL2 */
2097 	resx = get_reg_fixed_bits(kvm, HFGRTR2_EL2);
2098 	set_sysreg_masks(kvm, HFGRTR2_EL2, resx);
2099 	resx = get_reg_fixed_bits(kvm, HFGWTR2_EL2);
2100 	set_sysreg_masks(kvm, HFGWTR2_EL2, resx);
2101 
2102 	/* HDFG[RW]TR2_EL2 */
2103 	resx = get_reg_fixed_bits(kvm, HDFGRTR2_EL2);
2104 	set_sysreg_masks(kvm, HDFGRTR2_EL2, resx);
2105 	resx = get_reg_fixed_bits(kvm, HDFGWTR2_EL2);
2106 	set_sysreg_masks(kvm, HDFGWTR2_EL2, resx);
2107 
2108 	/* HFGITR2_EL2 */
2109 	resx = get_reg_fixed_bits(kvm, HFGITR2_EL2);
2110 	set_sysreg_masks(kvm, HFGITR2_EL2, resx);
2111 
2112 	/* TCR_EL2 */
2113 	resx = get_reg_fixed_bits(kvm, TCR_EL2);
2114 	set_sysreg_masks(kvm, TCR_EL2, resx);
2115 
2116 	/* TCR2_EL2 */
2117 	resx = get_reg_fixed_bits(kvm, TCR2_EL2);
2118 	set_sysreg_masks(kvm, TCR2_EL2, resx);
2119 
2120 	/* SCTLR_EL1 */
2121 	resx = get_reg_fixed_bits(kvm, SCTLR_EL1);
2122 	set_sysreg_masks(kvm, SCTLR_EL1, resx);
2123 
2124 	/* SCTLR_EL2 */
2125 	resx = get_reg_fixed_bits(kvm, SCTLR_EL2);
2126 	set_sysreg_masks(kvm, SCTLR_EL2, resx);
2127 
2128 	/* SCTLR2_ELx */
2129 	resx = get_reg_fixed_bits(kvm, SCTLR2_EL1);
2130 	set_sysreg_masks(kvm, SCTLR2_EL1, resx);
2131 	resx = get_reg_fixed_bits(kvm, SCTLR2_EL2);
2132 	set_sysreg_masks(kvm, SCTLR2_EL2, resx);
2133 
2134 	/* MDCR_EL2 */
2135 	resx = get_reg_fixed_bits(kvm, MDCR_EL2);
2136 	set_sysreg_masks(kvm, MDCR_EL2, resx);
2137 
2138 	/* CNTHCTL_EL2 */
2139 	resx.res0 = GENMASK(63, 20);
2140 	resx.res1 = 0;
2141 	if (!kvm_has_feat(kvm, ID_AA64PFR0_EL1, RME, IMP))
2142 		resx.res0 |= CNTHCTL_CNTPMASK | CNTHCTL_CNTVMASK;
2143 	if (!kvm_has_feat(kvm, ID_AA64MMFR0_EL1, ECV, CNTPOFF)) {
2144 		resx.res0 |= CNTHCTL_ECV;
2145 		if (!kvm_has_feat(kvm, ID_AA64MMFR0_EL1, ECV, IMP))
2146 			resx.res0 |= (CNTHCTL_EL1TVT | CNTHCTL_EL1TVCT |
2147 				      CNTHCTL_EL1NVPCT | CNTHCTL_EL1NVVCT);
2148 	}
2149 	if (!kvm_has_feat(kvm, ID_AA64MMFR1_EL1, VH, IMP))
2150 		resx.res0 |= GENMASK(11, 8);
2151 	set_sysreg_masks(kvm, CNTHCTL_EL2, resx);
2152 
2153 	/* ICH_HCR_EL2 */
2154 	resx.res0 = ICH_HCR_EL2_RES0;
2155 	resx.res1 = ICH_HCR_EL2_RES1;
2156 	if (!(vgic_ich_vtr() & ICH_VTR_EL2_TDS))
2157 		resx.res0 |= ICH_HCR_EL2_TDIR;
2158 	/* No GICv4 is presented to the guest */
2159 	resx.res0 |= ICH_HCR_EL2_DVIM | ICH_HCR_EL2_vSGIEOICount;
2160 	set_sysreg_masks(kvm, ICH_HCR_EL2, resx);
2161 
2162 	/* VNCR_EL2 */
2163 	resx.res0 = VNCR_EL2_RES0;
2164 	resx.res1 = VNCR_EL2_RES1;
2165 	set_sysreg_masks(kvm, VNCR_EL2, resx);
2166 
2167 	/* ZCR_EL2 - bits 8:4 are RAZ/WI so treat them as RES0 */
2168 	resx.res0 = ZCR_ELx_RES0 | GENMASK_ULL(8, 4);
2169 	resx.res1 = ZCR_ELx_RES1;
2170 	set_sysreg_masks(kvm, ZCR_EL2, resx);
2171 
2172 out:
2173 	for (enum vcpu_sysreg sr = __SANITISED_REG_START__; sr < NR_SYS_REGS; sr++)
2174 		__vcpu_rmw_sys_reg(vcpu, sr, |=, 0);
2175 
2176 	return 0;
2177 }
2178 
2179 void check_nested_vcpu_requests(struct kvm_vcpu *vcpu)
2180 {
2181 	if (kvm_check_request(KVM_REQ_NESTED_S2_UNMAP, vcpu)) {
2182 		struct kvm_s2_mmu *mmu = vcpu->arch.hw_mmu;
2183 
2184 		write_lock(&vcpu->kvm->mmu_lock);
2185 		if (mmu->pending_unmap) {
2186 			kvm_stage2_unmap_range(mmu, 0, kvm_phys_size(mmu), true);
2187 			mmu->pending_unmap = false;
2188 		}
2189 		write_unlock(&vcpu->kvm->mmu_lock);
2190 	}
2191 
2192 	if (kvm_check_request(KVM_REQ_MAP_L1_VNCR_EL2, vcpu))
2193 		kvm_map_l1_vncr(vcpu);
2194 
2195 	/* Must be last, as may switch context! */
2196 	if (kvm_check_request(KVM_REQ_GUEST_HYP_IRQ_PENDING, vcpu))
2197 		kvm_inject_nested_irq(vcpu);
2198 }
2199 
2200 /*
2201  * One of the many architectural bugs in FEAT_NV2 is that the guest hypervisor
2202  * can write to HCR_EL2 behind our back, potentially changing the exception
2203  * routing / masking for even the host context.
2204  *
2205  * What follows is some slop to (1) react to exception routing / masking and (2)
2206  * preserve the pending SError state across translation regimes.
2207  *
2208  * Also generate the maintenance interrupt state, and finally check that if
2209  * we need to take an interrupt targetting the guest hypervisor.
2210  */
2211 void kvm_nested_flush_hwstate(struct kvm_vcpu *vcpu)
2212 {
2213 	if (!vcpu_has_nv(vcpu))
2214 		return;
2215 
2216 	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING)))
2217 		kvm_inject_serror_esr(vcpu, vcpu_get_vsesr(vcpu));
2218 
2219 	vgic_v3_nested_update_mi(vcpu);
2220 
2221 	if (vgic_state_is_nested(vcpu) && kvm_vgic_vcpu_pending_irq(vcpu))
2222 		kvm_make_request(KVM_REQ_GUEST_HYP_IRQ_PENDING, vcpu);
2223 }
2224 
2225 void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu)
2226 {
2227 	unsigned long *hcr = vcpu_hcr(vcpu);
2228 
2229 	if (!vcpu_has_nv(vcpu))
2230 		return;
2231 
2232 	/*
2233 	 * We previously decided that an SError was deliverable to the guest.
2234 	 * Reap the pending state from HCR_EL2 and...
2235 	 */
2236 	if (unlikely(__test_and_clear_bit(__ffs(HCR_EL2_VSE), hcr)))
2237 		vcpu_set_flag(vcpu, NESTED_SERROR_PENDING);
2238 
2239 	/*
2240 	 * Re-attempt SError injection in case the deliverability has changed,
2241 	 * which is necessary to faithfully emulate WFI the case of a pending
2242 	 * SError being a wakeup condition.
2243 	 */
2244 	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING)))
2245 		kvm_inject_serror_esr(vcpu, vcpu_get_vsesr(vcpu));
2246 }
2247 
2248 /*
2249  * KVM unconditionally sets most of these traps anyway but use an allowlist
2250  * to document the guest hypervisor traps that may take precedence and guard
2251  * against future changes to the non-nested trap configuration.
2252  */
2253 #define NV_MDCR_GUEST_INCLUDE	(MDCR_EL2_TDE	|	\
2254 				 MDCR_EL2_TDA	|	\
2255 				 MDCR_EL2_TDRA	|	\
2256 				 MDCR_EL2_TTRF	|	\
2257 				 MDCR_EL2_TPMS	|	\
2258 				 MDCR_EL2_TPM	|	\
2259 				 MDCR_EL2_TPMCR	|	\
2260 				 MDCR_EL2_TDCC	|	\
2261 				 MDCR_EL2_TDOSA)
2262 
2263 void kvm_nested_setup_mdcr_el2(struct kvm_vcpu *vcpu)
2264 {
2265 	u64 guest_mdcr = __vcpu_sys_reg(vcpu, MDCR_EL2);
2266 
2267 	if (is_nested_ctxt(vcpu))
2268 		vcpu->arch.mdcr_el2 |= (guest_mdcr & NV_MDCR_GUEST_INCLUDE);
2269 	/*
2270 	 * In yet another example where FEAT_NV2 is fscking broken, accesses
2271 	 * to MDSCR_EL1 are redirected to the VNCR despite having an effect
2272 	 * at EL2. Use a big hammer to apply sanity.
2273 	 *
2274 	 * Unless of course we have FEAT_FGT, in which case we can precisely
2275 	 * trap MDSCR_EL1.
2276 	 */
2277 	else if (!cpus_have_final_cap(ARM64_HAS_FGT))
2278 		vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
2279 }
2280