1 // SPDX-License-Identifier: GPL-2.0-only
3 * Fault injection for both 32 and 64bit guests.
5 * Copyright (C) 2012,2013 - ARM Ltd
6 * Author: Marc Zyngier <marc.zyngier@arm.com>
8 * Based on arch/arm/kvm/emulate.c
9 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
10 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
13 #include <linux/kvm_host.h>
14 #include <asm/kvm_emulate.h>
15 #include <asm/kvm_nested.h>
18 static void pend_sync_exception(struct kvm_vcpu
*vcpu
)
20 /* If not nesting, EL1 is the only possible exception target */
21 if (likely(!vcpu_has_nv(vcpu
))) {
22 kvm_pend_exception(vcpu
, EXCEPT_AA64_EL1_SYNC
);
27 * With NV, we need to pick between EL1 and EL2. Note that we
28 * never deal with a nesting exception here, hence never
29 * changing context, and the exception itself can be delayed
30 * until the next entry.
32 switch(*vcpu_cpsr(vcpu
) & PSR_MODE_MASK
) {
35 kvm_pend_exception(vcpu
, EXCEPT_AA64_EL2_SYNC
);
39 kvm_pend_exception(vcpu
, EXCEPT_AA64_EL1_SYNC
);
42 if (vcpu_el2_tge_is_set(vcpu
))
43 kvm_pend_exception(vcpu
, EXCEPT_AA64_EL2_SYNC
);
45 kvm_pend_exception(vcpu
, EXCEPT_AA64_EL1_SYNC
);
52 static bool match_target_el(struct kvm_vcpu
*vcpu
, unsigned long target
)
54 return (vcpu_get_flag(vcpu
, EXCEPT_MASK
) == target
);
57 static void inject_abt64(struct kvm_vcpu
*vcpu
, bool is_iabt
, unsigned long addr
)
59 unsigned long cpsr
= *vcpu_cpsr(vcpu
);
60 bool is_aarch32
= vcpu_mode_is_32bit(vcpu
);
63 pend_sync_exception(vcpu
);
66 * Build an {i,d}abort, depending on the level and the
67 * instruction set. Report an external synchronous abort.
69 if (kvm_vcpu_trap_il_is32bit(vcpu
))
73 * Here, the guest runs in AArch64 mode when in EL1. If we get
74 * an AArch32 fault, it means we managed to trap an EL0 fault.
76 if (is_aarch32
|| (cpsr
& PSR_MODE_MASK
) == PSR_MODE_EL0t
)
77 esr
|= (ESR_ELx_EC_IABT_LOW
<< ESR_ELx_EC_SHIFT
);
79 esr
|= (ESR_ELx_EC_IABT_CUR
<< ESR_ELx_EC_SHIFT
);
82 esr
|= ESR_ELx_EC_DABT_LOW
<< ESR_ELx_EC_SHIFT
;
84 esr
|= ESR_ELx_FSC_EXTABT
;
86 if (match_target_el(vcpu
, unpack_vcpu_flag(EXCEPT_AA64_EL1_SYNC
))) {
87 vcpu_write_sys_reg(vcpu
, addr
, FAR_EL1
);
88 vcpu_write_sys_reg(vcpu
, esr
, ESR_EL1
);
90 vcpu_write_sys_reg(vcpu
, addr
, FAR_EL2
);
91 vcpu_write_sys_reg(vcpu
, esr
, ESR_EL2
);
95 static void inject_undef64(struct kvm_vcpu
*vcpu
)
97 u64 esr
= (ESR_ELx_EC_UNKNOWN
<< ESR_ELx_EC_SHIFT
);
99 pend_sync_exception(vcpu
);
102 * Build an unknown exception, depending on the instruction
105 if (kvm_vcpu_trap_il_is32bit(vcpu
))
108 if (match_target_el(vcpu
, unpack_vcpu_flag(EXCEPT_AA64_EL1_SYNC
)))
109 vcpu_write_sys_reg(vcpu
, esr
, ESR_EL1
);
111 vcpu_write_sys_reg(vcpu
, esr
, ESR_EL2
);
114 #define DFSR_FSC_EXTABT_LPAE 0x10
115 #define DFSR_FSC_EXTABT_nLPAE 0x08
116 #define DFSR_LPAE BIT(9)
117 #define TTBCR_EAE BIT(31)
119 static void inject_undef32(struct kvm_vcpu
*vcpu
)
121 kvm_pend_exception(vcpu
, EXCEPT_AA32_UND
);
125 * Modelled after TakeDataAbortException() and TakePrefetchAbortException
128 static void inject_abt32(struct kvm_vcpu
*vcpu
, bool is_pabt
, u32 addr
)
133 /* Give the guest an IMPLEMENTATION DEFINED exception */
134 if (vcpu_read_sys_reg(vcpu
, TCR_EL1
) & TTBCR_EAE
) {
135 fsr
= DFSR_LPAE
| DFSR_FSC_EXTABT_LPAE
;
137 /* no need to shuffle FS[4] into DFSR[10] as it's 0 */
138 fsr
= DFSR_FSC_EXTABT_nLPAE
;
141 far
= vcpu_read_sys_reg(vcpu
, FAR_EL1
);
144 kvm_pend_exception(vcpu
, EXCEPT_AA32_IABT
);
145 far
&= GENMASK(31, 0);
146 far
|= (u64
)addr
<< 32;
147 vcpu_write_sys_reg(vcpu
, fsr
, IFSR32_EL2
);
149 kvm_pend_exception(vcpu
, EXCEPT_AA32_DABT
);
150 far
&= GENMASK(63, 32);
152 vcpu_write_sys_reg(vcpu
, fsr
, ESR_EL1
);
155 vcpu_write_sys_reg(vcpu
, far
, FAR_EL1
);
159 * kvm_inject_dabt - inject a data abort into the guest
160 * @vcpu: The VCPU to receive the data abort
161 * @addr: The address to report in the DFAR
163 * It is assumed that this code is called from the VCPU thread and that the
164 * VCPU therefore is not currently executing guest code.
166 void kvm_inject_dabt(struct kvm_vcpu
*vcpu
, unsigned long addr
)
168 if (vcpu_el1_is_32bit(vcpu
))
169 inject_abt32(vcpu
, false, addr
);
171 inject_abt64(vcpu
, false, addr
);
175 * kvm_inject_pabt - inject a prefetch abort into the guest
176 * @vcpu: The VCPU to receive the prefetch abort
177 * @addr: The address to report in the DFAR
179 * It is assumed that this code is called from the VCPU thread and that the
180 * VCPU therefore is not currently executing guest code.
182 void kvm_inject_pabt(struct kvm_vcpu
*vcpu
, unsigned long addr
)
184 if (vcpu_el1_is_32bit(vcpu
))
185 inject_abt32(vcpu
, true, addr
);
187 inject_abt64(vcpu
, true, addr
);
190 void kvm_inject_size_fault(struct kvm_vcpu
*vcpu
)
192 unsigned long addr
, esr
;
194 addr
= kvm_vcpu_get_fault_ipa(vcpu
);
195 addr
|= kvm_vcpu_get_hfar(vcpu
) & GENMASK(11, 0);
197 if (kvm_vcpu_trap_is_iabt(vcpu
))
198 kvm_inject_pabt(vcpu
, addr
);
200 kvm_inject_dabt(vcpu
, addr
);
203 * If AArch64 or LPAE, set FSC to 0 to indicate an Address
204 * Size Fault at level 0, as if exceeding PARange.
206 * Non-LPAE guests will only get the external abort, as there
207 * is no way to describe the ASF.
209 if (vcpu_el1_is_32bit(vcpu
) &&
210 !(vcpu_read_sys_reg(vcpu
, TCR_EL1
) & TTBCR_EAE
))
213 esr
= vcpu_read_sys_reg(vcpu
, ESR_EL1
);
214 esr
&= ~GENMASK_ULL(5, 0);
215 vcpu_write_sys_reg(vcpu
, esr
, ESR_EL1
);
219 * kvm_inject_undefined - inject an undefined instruction into the guest
220 * @vcpu: The vCPU in which to inject the exception
222 * It is assumed that this code is called from the VCPU thread and that the
223 * VCPU therefore is not currently executing guest code.
225 void kvm_inject_undefined(struct kvm_vcpu
*vcpu
)
227 if (vcpu_el1_is_32bit(vcpu
))
228 inject_undef32(vcpu
);
230 inject_undef64(vcpu
);
233 void kvm_set_sei_esr(struct kvm_vcpu
*vcpu
, u64 esr
)
235 vcpu_set_vsesr(vcpu
, esr
& ESR_ELx_ISS_MASK
);
236 *vcpu_hcr(vcpu
) |= HCR_VSE
;
240 * kvm_inject_vabt - inject an async abort / SError into the guest
241 * @vcpu: The VCPU to receive the exception
243 * It is assumed that this code is called from the VCPU thread and that the
244 * VCPU therefore is not currently executing guest code.
246 * Systems with the RAS Extensions specify an imp-def ESR (ISV/IDS = 1) with
247 * the remaining ISS all-zeros so that this error is not interpreted as an
248 * uncategorized RAS error. Without the RAS Extensions we can't specify an ESR
249 * value, so the CPU generates an imp-def value.
251 void kvm_inject_vabt(struct kvm_vcpu
*vcpu
)
253 kvm_set_sei_esr(vcpu
, ESR_ELx_ISV
);