• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012,2013 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  *
6  * Derived from arch/arm/include/kvm_emulate.h
7  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
8  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
9  */
10 
11 #ifndef __ARM64_KVM_EMULATE_H__
12 #define __ARM64_KVM_EMULATE_H__
13 
14 #include <linux/kvm_host.h>
15 
16 #include <asm/debug-monitors.h>
17 #include <asm/esr.h>
18 #include <asm/kvm_arm.h>
19 #include <asm/kvm_hyp.h>
20 #include <asm/ptrace.h>
21 #include <asm/cputype.h>
22 #include <asm/virt.h>
23 
24 #define CURRENT_EL_SP_EL0_VECTOR	0x0
25 #define CURRENT_EL_SP_ELx_VECTOR	0x200
26 #define LOWER_EL_AArch64_VECTOR		0x400
27 #define LOWER_EL_AArch32_VECTOR		0x600
28 
29 enum exception_type {
30 	except_type_sync	= 0,
31 	except_type_irq		= 0x80,
32 	except_type_fiq		= 0x100,
33 	except_type_serror	= 0x180,
34 };
35 
36 bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
37 void kvm_skip_instr32(struct kvm_vcpu *vcpu);
38 
39 void kvm_inject_undefined(struct kvm_vcpu *vcpu);
40 void kvm_inject_vabt(struct kvm_vcpu *vcpu);
41 void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr);
42 void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr);
43 void kvm_inject_size_fault(struct kvm_vcpu *vcpu);
44 
45 unsigned long get_except64_offset(unsigned long psr, unsigned long target_mode,
46 				  enum exception_type type);
47 unsigned long get_except64_cpsr(unsigned long old, bool has_mte,
48 				unsigned long sctlr, unsigned long mode);
49 
50 void kvm_vcpu_wfi(struct kvm_vcpu *vcpu);
51 
52 #if defined(__KVM_VHE_HYPERVISOR__) || defined(__KVM_NVHE_HYPERVISOR__)
vcpu_el1_is_32bit(struct kvm_vcpu * vcpu)53 static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
54 {
55 	return !(vcpu->arch.hcr_el2 & HCR_RW);
56 }
57 #else
vcpu_el1_is_32bit(struct kvm_vcpu * vcpu)58 static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
59 {
60 	struct kvm *kvm = vcpu->kvm;
61 
62 	WARN_ON_ONCE(!test_bit(KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED,
63 			       &kvm->arch.flags));
64 
65 	return test_bit(KVM_ARCH_FLAG_EL1_32BIT, &kvm->arch.flags);
66 }
67 #endif
68 
vcpu_reset_hcr(struct kvm_vcpu * vcpu)69 static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
70 {
71 	vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
72 	if (is_kernel_in_hyp_mode())
73 		vcpu->arch.hcr_el2 |= HCR_E2H;
74 	if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN)) {
75 		/* route synchronous external abort exceptions to EL2 */
76 		vcpu->arch.hcr_el2 |= HCR_TEA;
77 		/* trap error record accesses */
78 		vcpu->arch.hcr_el2 |= HCR_TERR;
79 	}
80 
81 	if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
82 		vcpu->arch.hcr_el2 |= HCR_FWB;
83 	} else {
84 		/*
85 		 * For non-FWB CPUs, we trap VM ops (HCR_EL2.TVM) until M+C
86 		 * get set in SCTLR_EL1 such that we can detect when the guest
87 		 * MMU gets turned on and do the necessary cache maintenance
88 		 * then.
89 		 */
90 		vcpu->arch.hcr_el2 |= HCR_TVM;
91 	}
92 
93 	if (vcpu_el1_is_32bit(vcpu))
94 		vcpu->arch.hcr_el2 &= ~HCR_RW;
95 
96 	if (cpus_have_const_cap(ARM64_MISMATCHED_CACHE_TYPE) ||
97 	    vcpu_el1_is_32bit(vcpu))
98 		vcpu->arch.hcr_el2 |= HCR_TID2;
99 
100 	if (kvm_has_mte(vcpu->kvm))
101 		vcpu->arch.hcr_el2 |= HCR_ATA;
102 }
103 
vcpu_hcr(struct kvm_vcpu * vcpu)104 static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
105 {
106 	return (unsigned long *)&vcpu->arch.hcr_el2;
107 }
108 
vcpu_clear_wfx_traps(struct kvm_vcpu * vcpu)109 static inline void vcpu_clear_wfx_traps(struct kvm_vcpu *vcpu)
110 {
111 	vcpu->arch.hcr_el2 &= ~HCR_TWE;
112 	if (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
113 	    vcpu->kvm->arch.vgic.nassgireq)
114 		vcpu->arch.hcr_el2 &= ~HCR_TWI;
115 	else
116 		vcpu->arch.hcr_el2 |= HCR_TWI;
117 }
118 
vcpu_set_wfx_traps(struct kvm_vcpu * vcpu)119 static inline void vcpu_set_wfx_traps(struct kvm_vcpu *vcpu)
120 {
121 	vcpu->arch.hcr_el2 |= HCR_TWE;
122 	vcpu->arch.hcr_el2 |= HCR_TWI;
123 }
124 
vcpu_ptrauth_enable(struct kvm_vcpu * vcpu)125 static inline void vcpu_ptrauth_enable(struct kvm_vcpu *vcpu)
126 {
127 	vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK);
128 }
129 
vcpu_ptrauth_disable(struct kvm_vcpu * vcpu)130 static inline void vcpu_ptrauth_disable(struct kvm_vcpu *vcpu)
131 {
132 	vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK);
133 }
134 
vcpu_get_vsesr(struct kvm_vcpu * vcpu)135 static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu)
136 {
137 	return vcpu->arch.vsesr_el2;
138 }
139 
vcpu_set_vsesr(struct kvm_vcpu * vcpu,u64 vsesr)140 static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
141 {
142 	vcpu->arch.vsesr_el2 = vsesr;
143 }
144 
vcpu_pc(const struct kvm_vcpu * vcpu)145 static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
146 {
147 	return (unsigned long *)&vcpu_gp_regs(vcpu)->pc;
148 }
149 
vcpu_cpsr(const struct kvm_vcpu * vcpu)150 static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
151 {
152 	return (unsigned long *)&vcpu_gp_regs(vcpu)->pstate;
153 }
154 
vcpu_mode_is_32bit(const struct kvm_vcpu * vcpu)155 static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
156 {
157 	return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT);
158 }
159 
kvm_condition_valid(const struct kvm_vcpu * vcpu)160 static __always_inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
161 {
162 	if (vcpu_mode_is_32bit(vcpu))
163 		return kvm_condition_valid32(vcpu);
164 
165 	return true;
166 }
167 
vcpu_set_thumb(struct kvm_vcpu * vcpu)168 static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
169 {
170 	*vcpu_cpsr(vcpu) |= PSR_AA32_T_BIT;
171 }
172 
173 /*
174  * vcpu_get_reg and vcpu_set_reg should always be passed a register number
175  * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on
176  * AArch32 with banked registers.
177  */
vcpu_get_reg(const struct kvm_vcpu * vcpu,u8 reg_num)178 static __always_inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
179 					 u8 reg_num)
180 {
181 	return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs[reg_num];
182 }
183 
vcpu_set_reg(struct kvm_vcpu * vcpu,u8 reg_num,unsigned long val)184 static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
185 				unsigned long val)
186 {
187 	if (reg_num != 31)
188 		vcpu_gp_regs(vcpu)->regs[reg_num] = val;
189 }
190 
191 /*
192  * The layout of SPSR for an AArch32 state is different when observed from an
193  * AArch64 SPSR_ELx or an AArch32 SPSR_*. This function generates the AArch32
194  * view given an AArch64 view.
195  *
196  * In ARM DDI 0487E.a see:
197  *
198  * - The AArch64 view (SPSR_EL2) in section C5.2.18, page C5-426
199  * - The AArch32 view (SPSR_abt) in section G8.2.126, page G8-6256
200  * - The AArch32 view (SPSR_und) in section G8.2.132, page G8-6280
201  *
202  * Which show the following differences:
203  *
204  * | Bit | AA64 | AA32 | Notes                       |
205  * +-----+------+------+-----------------------------|
206  * | 24  | DIT  | J    | J is RES0 in ARMv8          |
207  * | 21  | SS   | DIT  | SS doesn't exist in AArch32 |
208  *
209  * ... and all other bits are (currently) common.
210  */
host_spsr_to_spsr32(unsigned long spsr)211 static inline unsigned long host_spsr_to_spsr32(unsigned long spsr)
212 {
213 	const unsigned long overlap = BIT(24) | BIT(21);
214 	unsigned long dit = !!(spsr & PSR_AA32_DIT_BIT);
215 
216 	spsr &= ~overlap;
217 
218 	spsr |= dit << 21;
219 
220 	return spsr;
221 }
222 
vcpu_mode_priv(const struct kvm_vcpu * vcpu)223 static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
224 {
225 	u32 mode;
226 
227 	if (vcpu_mode_is_32bit(vcpu)) {
228 		mode = *vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK;
229 		return mode > PSR_AA32_MODE_USR;
230 	}
231 
232 	mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
233 
234 	return mode != PSR_MODE_EL0t;
235 }
236 
kvm_vcpu_get_esr(const struct kvm_vcpu * vcpu)237 static __always_inline u32 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
238 {
239 	return vcpu->arch.fault.esr_el2;
240 }
241 
kvm_vcpu_get_condition(const struct kvm_vcpu * vcpu)242 static __always_inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
243 {
244 	u32 esr = kvm_vcpu_get_esr(vcpu);
245 
246 	if (esr & ESR_ELx_CV)
247 		return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
248 
249 	return -1;
250 }
251 
kvm_vcpu_get_hfar(const struct kvm_vcpu * vcpu)252 static __always_inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
253 {
254 	return vcpu->arch.fault.far_el2;
255 }
256 
kvm_vcpu_get_fault_ipa(const struct kvm_vcpu * vcpu)257 static __always_inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
258 {
259 	return ((phys_addr_t)vcpu->arch.fault.hpfar_el2 & HPFAR_MASK) << 8;
260 }
261 
kvm_vcpu_get_disr(const struct kvm_vcpu * vcpu)262 static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu)
263 {
264 	return vcpu->arch.fault.disr_el1;
265 }
266 
kvm_vcpu_hvc_get_imm(const struct kvm_vcpu * vcpu)267 static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
268 {
269 	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_xVC_IMM_MASK;
270 }
271 
kvm_vcpu_dabt_isvalid(const struct kvm_vcpu * vcpu)272 static __always_inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu)
273 {
274 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_ISV);
275 }
276 
kvm_vcpu_dabt_iss_nisv_sanitized(const struct kvm_vcpu * vcpu)277 static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized(const struct kvm_vcpu *vcpu)
278 {
279 	return kvm_vcpu_get_esr(vcpu) & (ESR_ELx_CM | ESR_ELx_WNR | ESR_ELx_FSC);
280 }
281 
kvm_vcpu_dabt_issext(const struct kvm_vcpu * vcpu)282 static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu)
283 {
284 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SSE);
285 }
286 
kvm_vcpu_dabt_issf(const struct kvm_vcpu * vcpu)287 static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
288 {
289 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SF);
290 }
291 
kvm_vcpu_dabt_get_rd(const struct kvm_vcpu * vcpu)292 static __always_inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu)
293 {
294 	return (kvm_vcpu_get_esr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
295 }
296 
kvm_vcpu_abt_iss1tw(const struct kvm_vcpu * vcpu)297 static __always_inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu)
298 {
299 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_S1PTW);
300 }
301 
302 /* Always check for S1PTW *before* using this. */
kvm_vcpu_dabt_iswrite(const struct kvm_vcpu * vcpu)303 static __always_inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu)
304 {
305 	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_WNR;
306 }
307 
kvm_vcpu_dabt_is_cm(const struct kvm_vcpu * vcpu)308 static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu)
309 {
310 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_CM);
311 }
312 
kvm_vcpu_dabt_get_as(const struct kvm_vcpu * vcpu)313 static __always_inline unsigned int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)
314 {
315 	return 1 << ((kvm_vcpu_get_esr(vcpu) & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT);
316 }
317 
318 /* This one is not specific to Data Abort */
kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu * vcpu)319 static __always_inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu)
320 {
321 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_IL);
322 }
323 
kvm_vcpu_trap_get_class(const struct kvm_vcpu * vcpu)324 static __always_inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
325 {
326 	return ESR_ELx_EC(kvm_vcpu_get_esr(vcpu));
327 }
328 
kvm_vcpu_trap_is_iabt(const struct kvm_vcpu * vcpu)329 static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
330 {
331 	return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW;
332 }
333 
kvm_vcpu_trap_is_exec_fault(const struct kvm_vcpu * vcpu)334 static inline bool kvm_vcpu_trap_is_exec_fault(const struct kvm_vcpu *vcpu)
335 {
336 	return kvm_vcpu_trap_is_iabt(vcpu) && !kvm_vcpu_abt_iss1tw(vcpu);
337 }
338 
kvm_vcpu_trap_get_fault(const struct kvm_vcpu * vcpu)339 static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)
340 {
341 	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC;
342 }
343 
kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu * vcpu)344 static __always_inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vcpu)
345 {
346 	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC_TYPE;
347 }
348 
kvm_vcpu_trap_get_fault_level(const struct kvm_vcpu * vcpu)349 static __always_inline u8 kvm_vcpu_trap_get_fault_level(const struct kvm_vcpu *vcpu)
350 {
351 	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC_LEVEL;
352 }
353 
kvm_vcpu_abt_issea(const struct kvm_vcpu * vcpu)354 static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
355 {
356 	switch (kvm_vcpu_trap_get_fault(vcpu)) {
357 	case FSC_SEA:
358 	case FSC_SEA_TTW0:
359 	case FSC_SEA_TTW1:
360 	case FSC_SEA_TTW2:
361 	case FSC_SEA_TTW3:
362 	case FSC_SECC:
363 	case FSC_SECC_TTW0:
364 	case FSC_SECC_TTW1:
365 	case FSC_SECC_TTW2:
366 	case FSC_SECC_TTW3:
367 		return true;
368 	default:
369 		return false;
370 	}
371 }
372 
kvm_vcpu_sys_get_rt(struct kvm_vcpu * vcpu)373 static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
374 {
375 	u32 esr = kvm_vcpu_get_esr(vcpu);
376 	return ESR_ELx_SYS64_ISS_RT(esr);
377 }
378 
kvm_is_write_fault(struct kvm_vcpu * vcpu)379 static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
380 {
381 	if (kvm_vcpu_abt_iss1tw(vcpu)) {
382 		/*
383 		 * Only a permission fault on a S1PTW should be
384 		 * considered as a write. Otherwise, page tables baked
385 		 * in a read-only memslot will result in an exception
386 		 * being delivered in the guest.
387 		 *
388 		 * The drawback is that we end-up faulting twice if the
389 		 * guest is using any of HW AF/DB: a translation fault
390 		 * to map the page containing the PT (read only at
391 		 * first), then a permission fault to allow the flags
392 		 * to be set.
393 		 */
394 		switch (kvm_vcpu_trap_get_fault_type(vcpu)) {
395 		case ESR_ELx_FSC_PERM:
396 			return true;
397 		default:
398 			return false;
399 		}
400 	}
401 
402 	if (kvm_vcpu_trap_is_iabt(vcpu))
403 		return false;
404 
405 	return kvm_vcpu_dabt_iswrite(vcpu);
406 }
407 
kvm_vcpu_get_mpidr_aff(struct kvm_vcpu * vcpu)408 static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
409 {
410 	return vcpu_read_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
411 }
412 
kvm_vcpu_set_be(struct kvm_vcpu * vcpu)413 static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
414 {
415 	if (vcpu_mode_is_32bit(vcpu)) {
416 		*vcpu_cpsr(vcpu) |= PSR_AA32_E_BIT;
417 	} else {
418 		u64 sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1);
419 		sctlr |= SCTLR_ELx_EE;
420 		vcpu_write_sys_reg(vcpu, sctlr, SCTLR_EL1);
421 	}
422 }
423 
kvm_vcpu_is_be(struct kvm_vcpu * vcpu)424 static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
425 {
426 	if (vcpu_mode_is_32bit(vcpu))
427 		return !!(*vcpu_cpsr(vcpu) & PSR_AA32_E_BIT);
428 
429 	if (vcpu_mode_priv(vcpu))
430 		return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & SCTLR_ELx_EE);
431 	else
432 		return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & SCTLR_EL1_E0E);
433 }
434 
vcpu_data_guest_to_host(struct kvm_vcpu * vcpu,unsigned long data,unsigned int len)435 static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
436 						    unsigned long data,
437 						    unsigned int len)
438 {
439 	if (kvm_vcpu_is_be(vcpu)) {
440 		switch (len) {
441 		case 1:
442 			return data & 0xff;
443 		case 2:
444 			return be16_to_cpu(data & 0xffff);
445 		case 4:
446 			return be32_to_cpu(data & 0xffffffff);
447 		default:
448 			return be64_to_cpu(data);
449 		}
450 	} else {
451 		switch (len) {
452 		case 1:
453 			return data & 0xff;
454 		case 2:
455 			return le16_to_cpu(data & 0xffff);
456 		case 4:
457 			return le32_to_cpu(data & 0xffffffff);
458 		default:
459 			return le64_to_cpu(data);
460 		}
461 	}
462 
463 	return data;		/* Leave LE untouched */
464 }
465 
vcpu_data_host_to_guest(struct kvm_vcpu * vcpu,unsigned long data,unsigned int len)466 static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
467 						    unsigned long data,
468 						    unsigned int len)
469 {
470 	if (kvm_vcpu_is_be(vcpu)) {
471 		switch (len) {
472 		case 1:
473 			return data & 0xff;
474 		case 2:
475 			return cpu_to_be16(data & 0xffff);
476 		case 4:
477 			return cpu_to_be32(data & 0xffffffff);
478 		default:
479 			return cpu_to_be64(data);
480 		}
481 	} else {
482 		switch (len) {
483 		case 1:
484 			return data & 0xff;
485 		case 2:
486 			return cpu_to_le16(data & 0xffff);
487 		case 4:
488 			return cpu_to_le32(data & 0xffffffff);
489 		default:
490 			return cpu_to_le64(data);
491 		}
492 	}
493 
494 	return data;		/* Leave LE untouched */
495 }
496 
kvm_incr_pc(struct kvm_vcpu * vcpu)497 static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu)
498 {
499 	WARN_ON(vcpu_get_flag(vcpu, PENDING_EXCEPTION));
500 	vcpu_set_flag(vcpu, INCREMENT_PC);
501 }
502 
503 #define kvm_pend_exception(v, e)					\
504 	do {								\
505 		WARN_ON(vcpu_get_flag((v), INCREMENT_PC));		\
506 		vcpu_set_flag((v), PENDING_EXCEPTION);			\
507 		vcpu_set_flag((v), e);					\
508 	} while (0)
509 
510 
vcpu_has_feature(struct kvm_vcpu * vcpu,int feature)511 static inline bool vcpu_has_feature(struct kvm_vcpu *vcpu, int feature)
512 {
513 	return test_bit(feature, vcpu->arch.features);
514 }
515 
kvm_vcpu_enable_ptrauth(struct kvm_vcpu * vcpu)516 static inline int kvm_vcpu_enable_ptrauth(struct kvm_vcpu *vcpu)
517 {
518 	/*
519 	 * For now make sure that both address/generic pointer authentication
520 	 * features are requested by the userspace together and the system
521 	 * supports these capabilities.
522 	 */
523 	if (!vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_ADDRESS) ||
524 	    !vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_GENERIC) ||
525 	    !system_has_full_ptr_auth())
526 		return -EINVAL;
527 
528 	vcpu_set_flag(vcpu, GUEST_HAS_PTRAUTH);
529 	return 0;
530 }
531 
532 /* Reset a vcpu's core registers. */
kvm_reset_vcpu_core(struct kvm_vcpu * vcpu)533 static inline void kvm_reset_vcpu_core(struct kvm_vcpu *vcpu)
534 {
535 	u32 pstate;
536 
537 	if (vcpu_el1_is_32bit(vcpu)) {
538 		pstate = VCPU_RESET_PSTATE_SVC;
539 	} else {
540 		pstate = VCPU_RESET_PSTATE_EL1;
541 	}
542 
543 	/* Reset core registers */
544 	memset(vcpu_gp_regs(vcpu), 0, sizeof(*vcpu_gp_regs(vcpu)));
545 	memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
546 	vcpu->arch.ctxt.spsr_abt = 0;
547 	vcpu->arch.ctxt.spsr_und = 0;
548 	vcpu->arch.ctxt.spsr_irq = 0;
549 	vcpu->arch.ctxt.spsr_fiq = 0;
550 	vcpu_gp_regs(vcpu)->pstate = pstate;
551 }
552 
553 /* PSCI reset handling for a vcpu. */
kvm_reset_vcpu_psci(struct kvm_vcpu * vcpu,struct vcpu_reset_state * reset_state)554 static inline void kvm_reset_vcpu_psci(struct kvm_vcpu *vcpu,
555 				       struct vcpu_reset_state *reset_state)
556 {
557 	unsigned long target_pc = reset_state->pc;
558 
559 	/* Gracefully handle Thumb2 entry point */
560 	if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
561 		target_pc &= ~1UL;
562 		vcpu_set_thumb(vcpu);
563 	}
564 
565 	/* Propagate caller endianness */
566 	if (reset_state->be)
567 		kvm_vcpu_set_be(vcpu);
568 
569 	*vcpu_pc(vcpu) = target_pc;
570 	vcpu_set_reg(vcpu, 0, reset_state->r0);
571 }
572 
573 #endif /* __ARM64_KVM_EMULATE_H__ */
574