Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / arch / arm64 / kvm / inject_fault.c
blobb47df73e98d782a55192051c9f3e537ddfd10941
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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/esr.h>
17 static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr)
19 unsigned long cpsr = *vcpu_cpsr(vcpu);
20 bool is_aarch32 = vcpu_mode_is_32bit(vcpu);
21 u32 esr = 0;
23 vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA64_EL1 |
24 KVM_ARM64_EXCEPT_AA64_ELx_SYNC |
25 KVM_ARM64_PENDING_EXCEPTION);
27 vcpu_write_sys_reg(vcpu, addr, FAR_EL1);
30 * Build an {i,d}abort, depending on the level and the
31 * instruction set. Report an external synchronous abort.
33 if (kvm_vcpu_trap_il_is32bit(vcpu))
34 esr |= ESR_ELx_IL;
37 * Here, the guest runs in AArch64 mode when in EL1. If we get
38 * an AArch32 fault, it means we managed to trap an EL0 fault.
40 if (is_aarch32 || (cpsr & PSR_MODE_MASK) == PSR_MODE_EL0t)
41 esr |= (ESR_ELx_EC_IABT_LOW << ESR_ELx_EC_SHIFT);
42 else
43 esr |= (ESR_ELx_EC_IABT_CUR << ESR_ELx_EC_SHIFT);
45 if (!is_iabt)
46 esr |= ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT;
48 vcpu_write_sys_reg(vcpu, esr | ESR_ELx_FSC_EXTABT, ESR_EL1);
51 static void inject_undef64(struct kvm_vcpu *vcpu)
53 u32 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
55 vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA64_EL1 |
56 KVM_ARM64_EXCEPT_AA64_ELx_SYNC |
57 KVM_ARM64_PENDING_EXCEPTION);
60 * Build an unknown exception, depending on the instruction
61 * set.
63 if (kvm_vcpu_trap_il_is32bit(vcpu))
64 esr |= ESR_ELx_IL;
66 vcpu_write_sys_reg(vcpu, esr, ESR_EL1);
69 #define DFSR_FSC_EXTABT_LPAE 0x10
70 #define DFSR_FSC_EXTABT_nLPAE 0x08
71 #define DFSR_LPAE BIT(9)
72 #define TTBCR_EAE BIT(31)
74 static void inject_undef32(struct kvm_vcpu *vcpu)
76 vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA32_UND |
77 KVM_ARM64_PENDING_EXCEPTION);
81 * Modelled after TakeDataAbortException() and TakePrefetchAbortException
82 * pseudocode.
84 static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt, u32 addr)
86 u64 far;
87 u32 fsr;
89 /* Give the guest an IMPLEMENTATION DEFINED exception */
90 if (vcpu_read_sys_reg(vcpu, TCR_EL1) & TTBCR_EAE) {
91 fsr = DFSR_LPAE | DFSR_FSC_EXTABT_LPAE;
92 } else {
93 /* no need to shuffle FS[4] into DFSR[10] as its 0 */
94 fsr = DFSR_FSC_EXTABT_nLPAE;
97 far = vcpu_read_sys_reg(vcpu, FAR_EL1);
99 if (is_pabt) {
100 vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA32_IABT |
101 KVM_ARM64_PENDING_EXCEPTION);
102 far &= GENMASK(31, 0);
103 far |= (u64)addr << 32;
104 vcpu_write_sys_reg(vcpu, fsr, IFSR32_EL2);
105 } else { /* !iabt */
106 vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA32_DABT |
107 KVM_ARM64_PENDING_EXCEPTION);
108 far &= GENMASK(63, 32);
109 far |= addr;
110 vcpu_write_sys_reg(vcpu, fsr, ESR_EL1);
113 vcpu_write_sys_reg(vcpu, far, FAR_EL1);
117 * kvm_inject_dabt - inject a data abort into the guest
118 * @vcpu: The VCPU to receive the data abort
119 * @addr: The address to report in the DFAR
121 * It is assumed that this code is called from the VCPU thread and that the
122 * VCPU therefore is not currently executing guest code.
124 void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
126 if (vcpu_el1_is_32bit(vcpu))
127 inject_abt32(vcpu, false, addr);
128 else
129 inject_abt64(vcpu, false, addr);
133 * kvm_inject_pabt - inject a prefetch abort into the guest
134 * @vcpu: The VCPU to receive the prefetch abort
135 * @addr: The address to report in the DFAR
137 * It is assumed that this code is called from the VCPU thread and that the
138 * VCPU therefore is not currently executing guest code.
140 void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
142 if (vcpu_el1_is_32bit(vcpu))
143 inject_abt32(vcpu, true, addr);
144 else
145 inject_abt64(vcpu, true, addr);
149 * kvm_inject_undefined - inject an undefined instruction into the guest
150 * @vcpu: The vCPU in which to inject the exception
152 * It is assumed that this code is called from the VCPU thread and that the
153 * VCPU therefore is not currently executing guest code.
155 void kvm_inject_undefined(struct kvm_vcpu *vcpu)
157 if (vcpu_el1_is_32bit(vcpu))
158 inject_undef32(vcpu);
159 else
160 inject_undef64(vcpu);
163 void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 esr)
165 vcpu_set_vsesr(vcpu, esr & ESR_ELx_ISS_MASK);
166 *vcpu_hcr(vcpu) |= HCR_VSE;
170 * kvm_inject_vabt - inject an async abort / SError into the guest
171 * @vcpu: The VCPU to receive the exception
173 * It is assumed that this code is called from the VCPU thread and that the
174 * VCPU therefore is not currently executing guest code.
176 * Systems with the RAS Extensions specify an imp-def ESR (ISV/IDS = 1) with
177 * the remaining ISS all-zeros so that this error is not interpreted as an
178 * uncategorized RAS error. Without the RAS Extensions we can't specify an ESR
179 * value, so the CPU generates an imp-def value.
181 void kvm_inject_vabt(struct kvm_vcpu *vcpu)
183 kvm_set_sei_esr(vcpu, ESR_ELx_ISV);