xref: /linux/arch/arm64/include/asm/kvm_nested.h (revision 361564a8d4305100ee4140ff7b345d1bf2c1a1b2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ARM64_KVM_NESTED_H
3 #define __ARM64_KVM_NESTED_H
4 
5 #include <linux/bitfield.h>
6 #include <linux/kvm_host.h>
7 #include <asm/kvm_emulate.h>
8 #include <asm/kvm_pgtable.h>
9 
10 static inline bool vcpu_has_nv(const struct kvm_vcpu *vcpu)
11 {
12 	return (!__is_defined(__KVM_NVHE_HYPERVISOR__) &&
13 		cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) &&
14 		vcpu_has_feature(vcpu, KVM_ARM_VCPU_HAS_EL2));
15 }
16 
17 /* Translation helpers from non-VHE EL2 to EL1 */
18 static inline u64 tcr_el2_ps_to_tcr_el1_ips(u64 tcr_el2)
19 {
20 	return (u64)FIELD_GET(TCR_EL2_PS_MASK, tcr_el2) << TCR_IPS_SHIFT;
21 }
22 
23 #define __xlate_tcr_nvhe_to_vhe(nfld, vfld, nvhe, vhe)				\
24 	FIELD_MODIFY(VHE_TCR_EL2_##vfld, vhe, FIELD_GET(TCR_EL2_##nfld, *(nvhe)))
25 
26 #define __xlate_tcr_nvhe_to_vhe_field(fld, nvhe, vhe)				\
27 	__xlate_tcr_nvhe_to_vhe(fld, fld, nvhe, vhe)
28 
29 static inline u64 translate_tcr_el2_to_tcr_el1(u64 nvhe)
30 {
31 	u64 vhe = VHE_TCR_EL2_EPD1 |			/* disable TTBR1_EL1 */
32 		  tcr_el2_ps_to_tcr_el1_ips(nvhe);
33 
34 	__xlate_tcr_nvhe_to_vhe(MTX,	MTX0,	&nvhe,	&vhe);
35 	__xlate_tcr_nvhe_to_vhe(TCMA,	TCMA0,	&nvhe,	&vhe);
36 	__xlate_tcr_nvhe_to_vhe(TBID,	TBID0,	&nvhe,	&vhe);
37 	__xlate_tcr_nvhe_to_vhe(HWU62,	HWU062,	&nvhe,	&vhe);
38 	__xlate_tcr_nvhe_to_vhe(HWU61,	HWU061,	&nvhe,	&vhe);
39 	__xlate_tcr_nvhe_to_vhe(HWU60,	HWU060,	&nvhe,	&vhe);
40 	__xlate_tcr_nvhe_to_vhe(HWU59,	HWU059,	&nvhe,	&vhe);
41 	__xlate_tcr_nvhe_to_vhe(HPD,	HPD0,	&nvhe,	&vhe);
42 	__xlate_tcr_nvhe_to_vhe(TBI,	TBI0,	&nvhe,	&vhe);
43 	__xlate_tcr_nvhe_to_vhe_field(	DS,	&nvhe,	&vhe);
44 	__xlate_tcr_nvhe_to_vhe_field(	HA,	&nvhe,	&vhe);
45 	__xlate_tcr_nvhe_to_vhe_field(	HD,	&nvhe,	&vhe);
46 	__xlate_tcr_nvhe_to_vhe_field(	TG0,	&nvhe,	&vhe);
47 	__xlate_tcr_nvhe_to_vhe_field(	SH0,	&nvhe,	&vhe);
48 	__xlate_tcr_nvhe_to_vhe_field(	ORGN0,	&nvhe,	&vhe);
49 	__xlate_tcr_nvhe_to_vhe_field(	IRGN0,	&nvhe,	&vhe);
50 	__xlate_tcr_nvhe_to_vhe_field(	T0SZ,	&nvhe,	&vhe);
51 
52 	return vhe;
53 }
54 
55 static inline u64 translate_cptr_el2_to_cpacr_el1(u64 cptr_el2)
56 {
57 	u64 cpacr_el1 = CPACR_EL1_RES1;
58 
59 	if (cptr_el2 & CPTR_EL2_TTA)
60 		cpacr_el1 |= CPACR_EL1_TTA;
61 	if (!(cptr_el2 & CPTR_EL2_TFP))
62 		cpacr_el1 |= CPACR_EL1_FPEN;
63 	if (!(cptr_el2 & CPTR_EL2_TZ))
64 		cpacr_el1 |= CPACR_EL1_ZEN;
65 
66 	cpacr_el1 |= cptr_el2 & (CPTR_EL2_TCPAC | CPTR_EL2_TAM);
67 
68 	return cpacr_el1;
69 }
70 
71 static inline u64 translate_sctlr_el2_to_sctlr_el1(u64 val)
72 {
73 	/* Only preserve the minimal set of bits we support */
74 	val &= (SCTLR_ELx_M | SCTLR_ELx_A | SCTLR_ELx_C | SCTLR_ELx_SA |
75 		SCTLR_ELx_I | SCTLR_ELx_IESB | SCTLR_ELx_WXN | SCTLR_ELx_EE);
76 	val |= SCTLR_EL1_RES1;
77 
78 	return val;
79 }
80 
81 static inline u64 translate_ttbr0_el2_to_ttbr0_el1(u64 ttbr0)
82 {
83 	/* Clear the ASID field */
84 	return ttbr0 & ~GENMASK_ULL(63, 48);
85 }
86 
87 extern bool forward_smc_trap(struct kvm_vcpu *vcpu);
88 extern bool forward_debug_exception(struct kvm_vcpu *vcpu);
89 extern int kvm_init_nested(struct kvm *kvm);
90 extern void kvm_destroy_nested(struct kvm *kvm);
91 extern int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu);
92 extern void kvm_init_nested_s2_mmu(struct kvm_s2_mmu *mmu);
93 extern struct kvm_s2_mmu *lookup_s2_mmu(struct kvm_vcpu *vcpu);
94 
95 union tlbi_info;
96 
97 extern void kvm_s2_mmu_iterate_by_vmid(struct kvm *kvm, u16 vmid,
98 				       const union tlbi_info *info,
99 				       void (*)(struct kvm_s2_mmu *,
100 						const union tlbi_info *));
101 extern void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,
102 					gpa_t nested_ipa, size_t map_size,
103 					struct kvm_guest_s2_mapping *mapping);
104 extern void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu,
105 					 gpa_t nipa, size_t size);
106 extern void kvm_vcpu_load_hw_mmu(struct kvm_vcpu *vcpu);
107 extern void kvm_vcpu_put_hw_mmu(struct kvm_vcpu *vcpu);
108 
109 extern void check_nested_vcpu_requests(struct kvm_vcpu *vcpu);
110 extern void kvm_nested_flush_hwstate(struct kvm_vcpu *vcpu);
111 extern void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu);
112 
113 extern void kvm_nested_setup_mdcr_el2(struct kvm_vcpu *vcpu);
114 
115 struct kvm_s2_trans {
116 	phys_addr_t output;
117 	unsigned long block_size;
118 	bool writable;
119 	bool readable;
120 	int level;
121 	u32 esr;
122 	u64 desc;
123 };
124 
125 static inline phys_addr_t kvm_s2_trans_output(struct kvm_s2_trans *trans)
126 {
127 	return trans->output;
128 }
129 
130 static inline unsigned long kvm_s2_trans_size(struct kvm_s2_trans *trans)
131 {
132 	return trans->block_size;
133 }
134 
135 static inline u32 kvm_s2_trans_esr(struct kvm_s2_trans *trans)
136 {
137 	return trans->esr;
138 }
139 
140 static inline bool kvm_s2_trans_readable(struct kvm_s2_trans *trans)
141 {
142 	return trans->readable;
143 }
144 
145 static inline bool kvm_s2_trans_writable(struct kvm_s2_trans *trans)
146 {
147 	return trans->writable;
148 }
149 
150 static inline bool kvm_has_xnx(struct kvm *kvm)
151 {
152 	return cpus_have_final_cap(ARM64_HAS_XNX) &&
153 		kvm_has_feat(kvm, ID_AA64MMFR1_EL1, XNX, IMP);
154 }
155 
156 static inline bool kvm_s2_trans_exec_el0(struct kvm *kvm, struct kvm_s2_trans *trans)
157 {
158 	u8 xn = FIELD_GET(KVM_PTE_LEAF_ATTR_HI_S2_XN, trans->desc);
159 
160 	if (!kvm_has_xnx(kvm))
161 		xn &= 0b10;
162 
163 	switch (xn) {
164 	case 0b00:
165 	case 0b01:
166 		return true;
167 	default:
168 		return false;
169 	}
170 }
171 
172 static inline bool kvm_s2_trans_exec_el1(struct kvm *kvm, struct kvm_s2_trans *trans)
173 {
174 	u8 xn = FIELD_GET(KVM_PTE_LEAF_ATTR_HI_S2_XN, trans->desc);
175 
176 	if (!kvm_has_xnx(kvm))
177 		xn &= 0b10;
178 
179 	switch (xn) {
180 	case 0b00:
181 	case 0b11:
182 		return true;
183 	default:
184 		return false;
185 	}
186 }
187 
188 extern int kvm_walk_nested_s2(struct kvm_vcpu *vcpu, phys_addr_t gipa,
189 			      struct kvm_s2_trans *result);
190 extern int kvm_s2_handle_perm_fault(struct kvm_vcpu *vcpu,
191 				    struct kvm_s2_trans *trans);
192 extern int kvm_inject_s2_fault(struct kvm_vcpu *vcpu, u64 esr_el2);
193 extern void kvm_nested_s2_wp(struct kvm *kvm);
194 extern void kvm_nested_unmap_cipa_range(struct kvm *kvm, gpa_t cipa,
195 					size_t unmap_size, bool may_block);
196 extern void kvm_nested_s2_unmap(struct kvm *kvm, bool may_block);
197 extern void kvm_nested_s2_flush(struct kvm *kvm);
198 
199 unsigned long compute_tlb_inval_range(struct kvm_s2_mmu *mmu, u64 val);
200 
201 static inline bool kvm_supported_tlbi_s1e1_op(struct kvm_vcpu *vpcu, u32 instr)
202 {
203 	struct kvm *kvm = vpcu->kvm;
204 	u8 CRm = sys_reg_CRm(instr);
205 
206 	if (!(sys_reg_Op0(instr) == TLBI_Op0 &&
207 	      sys_reg_Op1(instr) == TLBI_Op1_EL1))
208 		return false;
209 
210 	if (!(sys_reg_CRn(instr) == TLBI_CRn_XS ||
211 	      (sys_reg_CRn(instr) == TLBI_CRn_nXS &&
212 	       kvm_has_feat(kvm, ID_AA64ISAR1_EL1, XS, IMP))))
213 		return false;
214 
215 	if (CRm == TLBI_CRm_nROS &&
216 	    !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, OS))
217 		return false;
218 
219 	if ((CRm == TLBI_CRm_RIS || CRm == TLBI_CRm_ROS ||
220 	     CRm == TLBI_CRm_RNS) &&
221 	    !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, RANGE))
222 		return false;
223 
224 	return true;
225 }
226 
227 static inline bool kvm_supported_tlbi_s1e2_op(struct kvm_vcpu *vpcu, u32 instr)
228 {
229 	struct kvm *kvm = vpcu->kvm;
230 	u8 CRm = sys_reg_CRm(instr);
231 
232 	if (!(sys_reg_Op0(instr) == TLBI_Op0 &&
233 	      sys_reg_Op1(instr) == TLBI_Op1_EL2))
234 		return false;
235 
236 	if (!(sys_reg_CRn(instr) == TLBI_CRn_XS ||
237 	      (sys_reg_CRn(instr) == TLBI_CRn_nXS &&
238 	       kvm_has_feat(kvm, ID_AA64ISAR1_EL1, XS, IMP))))
239 		return false;
240 
241 	if (CRm == TLBI_CRm_IPAIS || CRm == TLBI_CRm_IPAONS)
242 		return false;
243 
244 	if (CRm == TLBI_CRm_nROS &&
245 	    !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, OS))
246 		return false;
247 
248 	if ((CRm == TLBI_CRm_RIS || CRm == TLBI_CRm_ROS ||
249 	     CRm == TLBI_CRm_RNS) &&
250 	    !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, RANGE))
251 		return false;
252 
253 	return true;
254 }
255 
256 int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu);
257 u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val);
258 
259 #ifdef CONFIG_ARM64_PTR_AUTH
260 bool kvm_auth_eretax(struct kvm_vcpu *vcpu, u64 *elr);
261 #else
262 static inline bool kvm_auth_eretax(struct kvm_vcpu *vcpu, u64 *elr)
263 {
264 	/* We really should never execute this... */
265 	WARN_ON_ONCE(1);
266 	*elr = 0xbad9acc0debadbad;
267 	return false;
268 }
269 #endif
270 
271 #define KVM_NV_GUEST_MAP_SZ	(KVM_PGTABLE_PROT_SW1 | KVM_PGTABLE_PROT_SW0)
272 
273 static inline u64 kvm_encode_nested_level(struct kvm_s2_trans *trans)
274 {
275 	return FIELD_PREP(KVM_NV_GUEST_MAP_SZ, trans->level);
276 }
277 
278 /* Adjust alignment for the contiguous bit as per StageOA() */
279 #define contiguous_bit_shift(d, wi, l)					\
280 	({								\
281 		u8 shift = 0;						\
282 									\
283 		if ((d) & PTE_CONT) {					\
284 			switch (BIT((wi)->pgshift)) {			\
285 			case SZ_4K:					\
286 				shift = 4;				\
287 				break;					\
288 			case SZ_16K:					\
289 				shift = (l) == 2 ? 5 : 7;		\
290 				break;					\
291 			case SZ_64K:					\
292 				shift = 5;				\
293 				break;					\
294 			}						\
295 		}							\
296 									\
297 		shift;							\
298 	})
299 
300 static inline u64 decode_range_tlbi(u64 val, u64 *range, u16 *asid)
301 {
302 	u64 base, tg, num, scale;
303 	int shift;
304 
305 	tg	= FIELD_GET(GENMASK(47, 46), val);
306 
307 	switch(tg) {
308 	case 1:
309 		shift = 12;
310 		break;
311 	case 2:
312 		shift = 14;
313 		break;
314 	case 3:
315 	default:		/* IMPDEF: handle tg==0 as 64k */
316 		shift = 16;
317 		break;
318 	}
319 
320 	base	= (val & GENMASK(36, 0)) << shift;
321 
322 	/*
323 	 * We only deal with at most 48bit VA/IPA, so 48 is where we
324 	 * sign-extend from. Should we support FEAT_L{VP}A* at some point,
325 	 * this will need to be revisited.
326 	 */
327 	base	= (u64)sign_extend64(base, 48);
328 
329 	if (asid)
330 		*asid = FIELD_GET(TLBIR_ASID_MASK, val);
331 
332 	scale	= FIELD_GET(GENMASK(45, 44), val);
333 	num	= FIELD_GET(GENMASK(43, 39), val);
334 	*range	= __TLBI_RANGE_PAGES(num, scale) << shift;
335 
336 	/* Cap the range to the correct half of the address space */
337 	if (!(base & BIT(48)))
338 		*range = min(*range, (BIT(48) - base));
339 	else
340 		*range = min(*range, ~base + 1);
341 
342 	return base;
343 }
344 
345 static inline unsigned int ps_to_output_size(unsigned int ps, bool pa52bit)
346 {
347 	switch (ps) {
348 	case 0: return 32;
349 	case 1: return 36;
350 	case 2: return 40;
351 	case 3: return 42;
352 	case 4: return 44;
353 	case 5: return 48;
354 	case 6: if (pa52bit)
355 			return 52;
356 		fallthrough;
357 	default:
358 		return 48;
359 	}
360 }
361 
362 enum trans_regime {
363 	TR_EL10,
364 	TR_EL20,
365 	TR_EL2,
366 };
367 
368 struct s1_walk_info;
369 
370 struct s1_walk_context {
371 	struct s1_walk_info	*wi;
372 	u64			table_ipa;
373 	int			level;
374 };
375 
376 struct s1_walk_filter {
377 	int	(*fn)(struct s1_walk_context *, void *);
378 	void	*priv;
379 };
380 
381 struct s1_walk_info {
382 	struct s1_walk_filter	*filter;
383 	u64	     		baddr;
384 	enum trans_regime	regime;
385 	unsigned int		max_oa_bits;
386 	unsigned int		pgshift;
387 	unsigned int		txsz;
388 	int 	     		sl;
389 	u8			sh;
390 	bool			as_el0;
391 	bool	     		hpd;
392 	bool			e0poe;
393 	bool			poe;
394 	bool			pan;
395 	bool	     		be;
396 	bool	     		s2;
397 	bool			pa52bit;
398 	bool			ha;
399 };
400 
401 struct s1_walk_result {
402 	union {
403 		struct {
404 			u64	desc;
405 			u64	pa;
406 			s8	level;
407 			u8	APTable;
408 			bool	nG;
409 			u16	asid;
410 			bool	UXNTable;
411 			bool	PXNTable;
412 			bool	uwxn;
413 			bool	uov;
414 			bool	ur;
415 			bool	uw;
416 			bool	ux;
417 			bool	pwxn;
418 			bool	pov;
419 			bool	pr;
420 			bool	pw;
421 			bool	px;
422 		};
423 		struct {
424 			u8	fst;
425 			bool	ptw;
426 			bool	s2;
427 		};
428 	};
429 	bool	failed;
430 };
431 
432 #define S1_MMU_DISABLED		(-127)
433 
434 static inline void fail_s1_walk(struct s1_walk_result *wr, u8 fst, bool s1ptw)
435 {
436 	wr->fst		= fst;
437 	wr->ptw		= s1ptw;
438 	wr->s2		= s1ptw;
439 	wr->failed	= true;
440 }
441 
442 static inline bool s1_walk_translated(struct s1_walk_result *wr)
443 {
444 	return wr->level != S1_MMU_DISABLED;
445 }
446 
447 int __kvm_translate_va(struct kvm_vcpu *vcpu, struct s1_walk_info *wi,
448 		       struct s1_walk_result *wr, u64 va);
449 int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa,
450 			     int *level);
451 
452 /* VNCR management */
453 int kvm_vcpu_allocate_vncr_tlb(struct kvm_vcpu *vcpu);
454 int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu);
455 void kvm_handle_s1e2_tlbi(struct kvm_vcpu *vcpu, u32 inst, u64 val);
456 
457 u16 get_asid_by_regime(struct kvm_vcpu *vcpu, enum trans_regime regime);
458 
459 #define vncr_fixmap(c)						\
460 	({							\
461 		u32 __c = (c);					\
462 		BUG_ON(__c >= NR_CPUS);				\
463 		(FIX_VNCR - __c);				\
464 	})
465 
466 int __kvm_at_swap_desc(struct kvm *kvm, gpa_t ipa, u64 old, u64 new);
467 
468 #endif /* __ARM64_KVM_NESTED_H */
469