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