1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/bits.h>
25#include <linux/kvm_host.h>
26#include <asm/kvm_emulate.h>
27#include <asm/kvm_hyp.h>
28
29#define DFSR_FSC_EXTABT_LPAE 0x10
30#define DFSR_FSC_EXTABT_nLPAE 0x08
31#define DFSR_LPAE BIT(9)
32
33
34
35
36static const u8 return_offsets[8][2] = {
37 [0] = { 0, 0 },
38 [1] = { 4, 2 },
39 [2] = { 0, 0 },
40 [3] = { 4, 4 },
41 [4] = { 8, 8 },
42 [5] = { 0, 0 },
43 [6] = { 4, 4 },
44 [7] = { 4, 4 },
45};
46
47static bool pre_fault_synchronize(struct kvm_vcpu *vcpu)
48{
49 preempt_disable();
50 if (vcpu->arch.sysregs_loaded_on_cpu) {
51 kvm_arch_vcpu_put(vcpu);
52 return true;
53 }
54
55 preempt_enable();
56 return false;
57}
58
59static void post_fault_synchronize(struct kvm_vcpu *vcpu, bool loaded)
60{
61 if (loaded) {
62 kvm_arch_vcpu_load(vcpu, smp_processor_id());
63 preempt_enable();
64 }
65}
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85static unsigned long get_except32_cpsr(struct kvm_vcpu *vcpu, u32 mode)
86{
87 u32 sctlr = vcpu_cp15(vcpu, c1_SCTLR);
88 unsigned long old, new;
89
90 old = *vcpu_cpsr(vcpu);
91 new = 0;
92
93 new |= (old & PSR_AA32_N_BIT);
94 new |= (old & PSR_AA32_Z_BIT);
95 new |= (old & PSR_AA32_C_BIT);
96 new |= (old & PSR_AA32_V_BIT);
97 new |= (old & PSR_AA32_Q_BIT);
98
99
100
101
102
103 new |= (old & PSR_AA32_DIT_BIT);
104
105
106
107 if (sctlr & BIT(31))
108 new |= PSR_AA32_SSBS_BIT;
109
110
111
112
113 new |= (old & PSR_AA32_PAN_BIT);
114 if (!(sctlr & BIT(23)))
115 new |= PSR_AA32_PAN_BIT;
116
117
118
119
120
121
122 new |= (old & PSR_AA32_GE_MASK);
123
124
125
126
127
128
129
130 if (sctlr & BIT(25))
131 new |= PSR_AA32_E_BIT;
132
133
134
135
136
137 new |= (old & PSR_AA32_A_BIT);
138 if (mode != PSR_AA32_MODE_UND && mode != PSR_AA32_MODE_SVC)
139 new |= PSR_AA32_A_BIT;
140
141
142
143
144 new |= PSR_AA32_I_BIT;
145
146
147
148
149
150 new |= (old & PSR_AA32_F_BIT);
151 if (mode == PSR_AA32_MODE_FIQ)
152 new |= PSR_AA32_F_BIT;
153
154
155
156
157 if (sctlr & BIT(30))
158 new |= PSR_AA32_T_BIT;
159
160 new |= mode;
161
162 return new;
163}
164
165static void prepare_fault32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
166{
167 unsigned long spsr = *vcpu_cpsr(vcpu);
168 bool is_thumb = (spsr & PSR_AA32_T_BIT);
169 u32 return_offset = return_offsets[vect_offset >> 2][is_thumb];
170 u32 sctlr = vcpu_cp15(vcpu, c1_SCTLR);
171
172 *vcpu_cpsr(vcpu) = get_except32_cpsr(vcpu, mode);
173
174
175 vcpu_write_spsr(vcpu, host_spsr_to_spsr32(spsr));
176 *vcpu_reg32(vcpu, 14) = *vcpu_pc(vcpu) + return_offset;
177
178
179 if (sctlr & (1 << 13))
180 vect_offset += 0xffff0000;
181 else
182 vect_offset += vcpu_cp15(vcpu, c12_VBAR);
183
184 *vcpu_pc(vcpu) = vect_offset;
185}
186
187void kvm_inject_undef32(struct kvm_vcpu *vcpu)
188{
189 bool loaded = pre_fault_synchronize(vcpu);
190
191 prepare_fault32(vcpu, PSR_AA32_MODE_UND, 4);
192 post_fault_synchronize(vcpu, loaded);
193}
194
195
196
197
198
199static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt,
200 unsigned long addr)
201{
202 u32 vect_offset;
203 u32 *far, *fsr;
204 bool is_lpae;
205 bool loaded;
206
207 loaded = pre_fault_synchronize(vcpu);
208
209 if (is_pabt) {
210 vect_offset = 12;
211 far = &vcpu_cp15(vcpu, c6_IFAR);
212 fsr = &vcpu_cp15(vcpu, c5_IFSR);
213 } else {
214 vect_offset = 16;
215 far = &vcpu_cp15(vcpu, c6_DFAR);
216 fsr = &vcpu_cp15(vcpu, c5_DFSR);
217 }
218
219 prepare_fault32(vcpu, PSR_AA32_MODE_ABT, vect_offset);
220
221 *far = addr;
222
223
224 is_lpae = (vcpu_cp15(vcpu, c2_TTBCR) >> 31);
225 if (is_lpae) {
226 *fsr = DFSR_LPAE | DFSR_FSC_EXTABT_LPAE;
227 } else {
228
229 *fsr = DFSR_FSC_EXTABT_nLPAE;
230 }
231
232 post_fault_synchronize(vcpu, loaded);
233}
234
235void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr)
236{
237 inject_abt32(vcpu, false, addr);
238}
239
240void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr)
241{
242 inject_abt32(vcpu, true, addr);
243}
244