blob: 6aafc2825c1cbdc9b4a003660830c05b8d643d07 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Marc Zyngieraa8eff92012-12-17 12:27:42 +00002/*
Marc Zyngiere82e0302013-02-06 11:29:35 +00003 * Fault injection for both 32 and 64bit guests.
Marc Zyngieraa8eff92012-12-17 12:27:42 +00004 *
5 * Copyright (C) 2012,2013 - ARM Ltd
6 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 *
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>
Marc Zyngieraa8eff92012-12-17 12:27:42 +000011 */
12
13#include <linux/kvm_host.h>
14#include <asm/kvm_emulate.h>
15#include <asm/esr.h>
16
Marc Zyngier8fc153c2016-01-06 18:29:19 +000017#define CURRENT_EL_SP_EL0_VECTOR 0x0
18#define CURRENT_EL_SP_ELx_VECTOR 0x200
19#define LOWER_EL_AArch64_VECTOR 0x400
20#define LOWER_EL_AArch32_VECTOR 0x600
Marc Zyngieraa8eff92012-12-17 12:27:42 +000021
Marc Zyngier8fc153c2016-01-06 18:29:19 +000022enum exception_type {
23 except_type_sync = 0,
24 except_type_irq = 0x80,
25 except_type_fiq = 0x100,
26 except_type_serror = 0x180,
27};
28
29static u64 get_except_vector(struct kvm_vcpu *vcpu, enum exception_type type)
30{
31 u64 exc_offset;
32
33 switch (*vcpu_cpsr(vcpu) & (PSR_MODE_MASK | PSR_MODE32_BIT)) {
34 case PSR_MODE_EL1t:
35 exc_offset = CURRENT_EL_SP_EL0_VECTOR;
36 break;
37 case PSR_MODE_EL1h:
38 exc_offset = CURRENT_EL_SP_ELx_VECTOR;
39 break;
40 case PSR_MODE_EL0t:
41 exc_offset = LOWER_EL_AArch64_VECTOR;
42 break;
43 default:
44 exc_offset = LOWER_EL_AArch32_VECTOR;
45 }
46
Christoffer Dall8d404c42016-03-16 15:38:53 +010047 return vcpu_read_sys_reg(vcpu, VBAR_EL1) + exc_offset + type;
Marc Zyngier8fc153c2016-01-06 18:29:19 +000048}
49
Mark Rutlanda4253722020-01-08 13:43:22 +000050/*
51 * When an exception is taken, most PSTATE fields are left unchanged in the
52 * handler. However, some are explicitly overridden (e.g. M[4:0]). Luckily all
53 * of the inherited bits have the same position in the AArch64/AArch32 SPSR_ELx
54 * layouts, so we don't need to shuffle these for exceptions from AArch32 EL0.
55 *
56 * For the SPSR_ELx layout for AArch64, see ARM DDI 0487E.a page C5-429.
57 * For the SPSR_ELx layout for AArch32, see ARM DDI 0487E.a page C5-426.
58 *
59 * Here we manipulate the fields in order of the AArch64 SPSR_ELx layout, from
60 * MSB to LSB.
61 */
62static unsigned long get_except64_pstate(struct kvm_vcpu *vcpu)
63{
64 unsigned long sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1);
65 unsigned long old, new;
66
67 old = *vcpu_cpsr(vcpu);
68 new = 0;
69
70 new |= (old & PSR_N_BIT);
71 new |= (old & PSR_Z_BIT);
72 new |= (old & PSR_C_BIT);
73 new |= (old & PSR_V_BIT);
74
75 // TODO: TCO (if/when ARMv8.5-MemTag is exposed to guests)
76
77 new |= (old & PSR_DIT_BIT);
78
79 // PSTATE.UAO is set to zero upon any exception to AArch64
80 // See ARM DDI 0487E.a, page D5-2579.
81
82 // PSTATE.PAN is unchanged unless SCTLR_ELx.SPAN == 0b0
83 // SCTLR_ELx.SPAN is RES1 when ARMv8.1-PAN is not implemented
84 // See ARM DDI 0487E.a, page D5-2578.
85 new |= (old & PSR_PAN_BIT);
86 if (!(sctlr & SCTLR_EL1_SPAN))
87 new |= PSR_PAN_BIT;
88
89 // PSTATE.SS is set to zero upon any exception to AArch64
90 // See ARM DDI 0487E.a, page D2-2452.
91
92 // PSTATE.IL is set to zero upon any exception to AArch64
93 // See ARM DDI 0487E.a, page D1-2306.
94
95 // PSTATE.SSBS is set to SCTLR_ELx.DSSBS upon any exception to AArch64
96 // See ARM DDI 0487E.a, page D13-3258
97 if (sctlr & SCTLR_ELx_DSSBS)
98 new |= PSR_SSBS_BIT;
99
100 // PSTATE.BTYPE is set to zero upon any exception to AArch64
101 // See ARM DDI 0487E.a, pages D1-2293 to D1-2294.
102
103 new |= PSR_D_BIT;
104 new |= PSR_A_BIT;
105 new |= PSR_I_BIT;
106 new |= PSR_F_BIT;
107
108 new |= PSR_MODE_EL1h;
109
110 return new;
111}
112
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000113static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr)
114{
115 unsigned long cpsr = *vcpu_cpsr(vcpu);
Andrew Jones89581f02016-07-22 10:38:46 -0400116 bool is_aarch32 = vcpu_mode_is_32bit(vcpu);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000117 u32 esr = 0;
118
Christoffer Dall6d4bd902017-12-27 20:51:04 +0100119 vcpu_write_elr_el1(vcpu, *vcpu_pc(vcpu));
Marc Zyngier8fc153c2016-01-06 18:29:19 +0000120 *vcpu_pc(vcpu) = get_except_vector(vcpu, except_type_sync);
Andrew Jones89581f02016-07-22 10:38:46 -0400121
Mark Rutlanda4253722020-01-08 13:43:22 +0000122 *vcpu_cpsr(vcpu) = get_except64_pstate(vcpu);
Christoffer Dall00536ec2017-12-27 20:01:52 +0100123 vcpu_write_spsr(vcpu, cpsr);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000124
Christoffer Dall8d404c42016-03-16 15:38:53 +0100125 vcpu_write_sys_reg(vcpu, addr, FAR_EL1);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000126
127 /*
128 * Build an {i,d}abort, depending on the level and the
129 * instruction set. Report an external synchronous abort.
130 */
131 if (kvm_vcpu_trap_il_is32bit(vcpu))
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000132 esr |= ESR_ELx_IL;
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000133
134 /*
135 * Here, the guest runs in AArch64 mode when in EL1. If we get
136 * an AArch32 fault, it means we managed to trap an EL0 fault.
137 */
138 if (is_aarch32 || (cpsr & PSR_MODE_MASK) == PSR_MODE_EL0t)
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000139 esr |= (ESR_ELx_EC_IABT_LOW << ESR_ELx_EC_SHIFT);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000140 else
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000141 esr |= (ESR_ELx_EC_IABT_CUR << ESR_ELx_EC_SHIFT);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000142
143 if (!is_iabt)
Matt Evanse4fe9e72016-05-16 13:54:56 +0100144 esr |= ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT;
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000145
Christoffer Dall8d404c42016-03-16 15:38:53 +0100146 vcpu_write_sys_reg(vcpu, esr | ESR_ELx_FSC_EXTABT, ESR_EL1);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000147}
148
149static void inject_undef64(struct kvm_vcpu *vcpu)
150{
151 unsigned long cpsr = *vcpu_cpsr(vcpu);
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000152 u32 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000153
Christoffer Dall6d4bd902017-12-27 20:51:04 +0100154 vcpu_write_elr_el1(vcpu, *vcpu_pc(vcpu));
Marc Zyngier8fc153c2016-01-06 18:29:19 +0000155 *vcpu_pc(vcpu) = get_except_vector(vcpu, except_type_sync);
Andrew Jones89581f02016-07-22 10:38:46 -0400156
Mark Rutlanda4253722020-01-08 13:43:22 +0000157 *vcpu_cpsr(vcpu) = get_except64_pstate(vcpu);
Christoffer Dall00536ec2017-12-27 20:01:52 +0100158 vcpu_write_spsr(vcpu, cpsr);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000159
160 /*
161 * Build an unknown exception, depending on the instruction
162 * set.
163 */
164 if (kvm_vcpu_trap_il_is32bit(vcpu))
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000165 esr |= ESR_ELx_IL;
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000166
Christoffer Dall8d404c42016-03-16 15:38:53 +0100167 vcpu_write_sys_reg(vcpu, esr, ESR_EL1);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000168}
169
170/**
171 * kvm_inject_dabt - inject a data abort into the guest
Christoffer Dallda345172019-10-11 13:07:06 +0200172 * @vcpu: The VCPU to receive the data abort
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000173 * @addr: The address to report in the DFAR
174 *
175 * It is assumed that this code is called from the VCPU thread and that the
176 * VCPU therefore is not currently executing guest code.
177 */
178void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
179{
Christoffer Dalle72341c2017-12-13 22:56:48 +0100180 if (vcpu_el1_is_32bit(vcpu))
Marc Zyngier74a64a92017-10-29 02:18:09 +0000181 kvm_inject_dabt32(vcpu, addr);
Marc Zyngier126c69a2015-08-27 16:10:01 +0100182 else
183 inject_abt64(vcpu, false, addr);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000184}
185
186/**
187 * kvm_inject_pabt - inject a prefetch abort into the guest
Christoffer Dallda345172019-10-11 13:07:06 +0200188 * @vcpu: The VCPU to receive the prefetch abort
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000189 * @addr: The address to report in the DFAR
190 *
191 * It is assumed that this code is called from the VCPU thread and that the
192 * VCPU therefore is not currently executing guest code.
193 */
194void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
195{
Christoffer Dalle72341c2017-12-13 22:56:48 +0100196 if (vcpu_el1_is_32bit(vcpu))
Marc Zyngier74a64a92017-10-29 02:18:09 +0000197 kvm_inject_pabt32(vcpu, addr);
Marc Zyngier126c69a2015-08-27 16:10:01 +0100198 else
199 inject_abt64(vcpu, true, addr);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000200}
201
202/**
203 * kvm_inject_undefined - inject an undefined instruction into the guest
204 *
205 * It is assumed that this code is called from the VCPU thread and that the
206 * VCPU therefore is not currently executing guest code.
207 */
208void kvm_inject_undefined(struct kvm_vcpu *vcpu)
209{
Christoffer Dalle72341c2017-12-13 22:56:48 +0100210 if (vcpu_el1_is_32bit(vcpu))
Marc Zyngier74a64a92017-10-29 02:18:09 +0000211 kvm_inject_undef32(vcpu);
Marc Zyngier126c69a2015-08-27 16:10:01 +0100212 else
213 inject_undef64(vcpu);
Marc Zyngieraa8eff92012-12-17 12:27:42 +0000214}
Marc Zyngier10cf3392016-09-06 14:02:01 +0100215
Dongjiu Gengb7b27fa2018-07-19 16:24:22 +0100216void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 esr)
James Morse4715c142018-01-15 19:39:01 +0000217{
Dongjiu Gengb7b27fa2018-07-19 16:24:22 +0100218 vcpu_set_vsesr(vcpu, esr & ESR_ELx_ISS_MASK);
Christoffer Dall3df59d82017-08-03 12:09:05 +0200219 *vcpu_hcr(vcpu) |= HCR_VSE;
James Morse4715c142018-01-15 19:39:01 +0000220}
221
Marc Zyngier10cf3392016-09-06 14:02:01 +0100222/**
223 * kvm_inject_vabt - inject an async abort / SError into the guest
224 * @vcpu: The VCPU to receive the exception
225 *
226 * It is assumed that this code is called from the VCPU thread and that the
227 * VCPU therefore is not currently executing guest code.
James Morse4715c142018-01-15 19:39:01 +0000228 *
229 * Systems with the RAS Extensions specify an imp-def ESR (ISV/IDS = 1) with
230 * the remaining ISS all-zeros so that this error is not interpreted as an
231 * uncategorized RAS error. Without the RAS Extensions we can't specify an ESR
232 * value, so the CPU generates an imp-def value.
Marc Zyngier10cf3392016-09-06 14:02:01 +0100233 */
234void kvm_inject_vabt(struct kvm_vcpu *vcpu)
235{
Dongjiu Gengb7b27fa2018-07-19 16:24:22 +0100236 kvm_set_sei_esr(vcpu, ESR_ELx_ISV);
Marc Zyngier10cf3392016-09-06 14:02:01 +0100237}