GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / arm / include / asm / kvm_emulate.h
1 /*
2  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License, version 2, as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #ifndef __ARM_KVM_EMULATE_H__
20 #define __ARM_KVM_EMULATE_H__
21
22 #include <linux/kvm_host.h>
23 #include <asm/kvm_asm.h>
24 #include <asm/kvm_mmio.h>
25 #include <asm/kvm_arm.h>
26 #include <asm/cputype.h>
27
28 /* arm64 compatibility macros */
29 #define PSR_AA32_MODE_FIQ       FIQ_MODE
30 #define PSR_AA32_MODE_SVC       SVC_MODE
31 #define PSR_AA32_MODE_ABT       ABT_MODE
32 #define PSR_AA32_MODE_UND       UND_MODE
33 #define PSR_AA32_T_BIT          PSR_T_BIT
34 #define PSR_AA32_F_BIT          PSR_F_BIT
35 #define PSR_AA32_I_BIT          PSR_I_BIT
36 #define PSR_AA32_A_BIT          PSR_A_BIT
37 #define PSR_AA32_E_BIT          PSR_E_BIT
38 #define PSR_AA32_IT_MASK        PSR_IT_MASK
39 #define PSR_AA32_GE_MASK        0x000f0000
40 #define PSR_AA32_DIT_BIT        0x00200000
41 #define PSR_AA32_PAN_BIT        0x00400000
42 #define PSR_AA32_SSBS_BIT       0x00800000
43 #define PSR_AA32_Q_BIT          PSR_Q_BIT
44 #define PSR_AA32_V_BIT          PSR_V_BIT
45 #define PSR_AA32_C_BIT          PSR_C_BIT
46 #define PSR_AA32_Z_BIT          PSR_Z_BIT
47 #define PSR_AA32_N_BIT          PSR_N_BIT
48
49 unsigned long *vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num);
50
51 static inline unsigned long *vcpu_reg32(struct kvm_vcpu *vcpu, u8 reg_num)
52 {
53         return vcpu_reg(vcpu, reg_num);
54 }
55
56 unsigned long *__vcpu_spsr(struct kvm_vcpu *vcpu);
57
58 static inline unsigned long vpcu_read_spsr(struct kvm_vcpu *vcpu)
59 {
60         return *__vcpu_spsr(vcpu);
61 }
62
63 static inline void vcpu_write_spsr(struct kvm_vcpu *vcpu, unsigned long v)
64 {
65         *__vcpu_spsr(vcpu) = v;
66 }
67
68 static inline unsigned long host_spsr_to_spsr32(unsigned long spsr)
69 {
70         return spsr;
71 }
72
73 static inline unsigned long vcpu_get_reg(struct kvm_vcpu *vcpu,
74                                          u8 reg_num)
75 {
76         return *vcpu_reg(vcpu, reg_num);
77 }
78
79 static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
80                                 unsigned long val)
81 {
82         *vcpu_reg(vcpu, reg_num) = val;
83 }
84
85 bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
86 void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr);
87 void kvm_inject_undef32(struct kvm_vcpu *vcpu);
88 void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr);
89 void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr);
90 void kvm_inject_vabt(struct kvm_vcpu *vcpu);
91
92 static inline void kvm_inject_undefined(struct kvm_vcpu *vcpu)
93 {
94         kvm_inject_undef32(vcpu);
95 }
96
97 static inline void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
98 {
99         kvm_inject_dabt32(vcpu, addr);
100 }
101
102 static inline void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
103 {
104         kvm_inject_pabt32(vcpu, addr);
105 }
106
107 static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
108 {
109         return kvm_condition_valid32(vcpu);
110 }
111
112 static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr)
113 {
114         kvm_skip_instr32(vcpu, is_wide_instr);
115 }
116
117 static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
118 {
119         vcpu->arch.hcr = HCR_GUEST_MASK;
120 }
121
122 static inline unsigned long *vcpu_hcr(const struct kvm_vcpu *vcpu)
123 {
124         return (unsigned long *)&vcpu->arch.hcr;
125 }
126
127 static inline void vcpu_clear_wfe_traps(struct kvm_vcpu *vcpu)
128 {
129         vcpu->arch.hcr &= ~HCR_TWE;
130 }
131
132 static inline void vcpu_set_wfe_traps(struct kvm_vcpu *vcpu)
133 {
134         vcpu->arch.hcr |= HCR_TWE;
135 }
136
137 static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
138 {
139         return true;
140 }
141
142 static inline unsigned long *vcpu_pc(struct kvm_vcpu *vcpu)
143 {
144         return &vcpu->arch.ctxt.gp_regs.usr_regs.ARM_pc;
145 }
146
147 static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
148 {
149         return (unsigned long *)&vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr;
150 }
151
152 static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
153 {
154         *vcpu_cpsr(vcpu) |= PSR_T_BIT;
155 }
156
157 static inline bool mode_has_spsr(struct kvm_vcpu *vcpu)
158 {
159         unsigned long cpsr_mode = vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr & MODE_MASK;
160         return (cpsr_mode > USR_MODE && cpsr_mode < SYSTEM_MODE);
161 }
162
163 static inline bool vcpu_mode_priv(struct kvm_vcpu *vcpu)
164 {
165         unsigned long cpsr_mode = vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr & MODE_MASK;
166         return cpsr_mode > USR_MODE;
167 }
168
169 static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu)
170 {
171         return vcpu->arch.fault.hsr;
172 }
173
174 static inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
175 {
176         u32 hsr = kvm_vcpu_get_hsr(vcpu);
177
178         if (hsr & HSR_CV)
179                 return (hsr & HSR_COND) >> HSR_COND_SHIFT;
180
181         return -1;
182 }
183
184 static inline unsigned long kvm_vcpu_get_hfar(struct kvm_vcpu *vcpu)
185 {
186         return vcpu->arch.fault.hxfar;
187 }
188
189 static inline phys_addr_t kvm_vcpu_get_fault_ipa(struct kvm_vcpu *vcpu)
190 {
191         return ((phys_addr_t)vcpu->arch.fault.hpfar & HPFAR_MASK) << 8;
192 }
193
194 static inline bool kvm_vcpu_dabt_isvalid(struct kvm_vcpu *vcpu)
195 {
196         return kvm_vcpu_get_hsr(vcpu) & HSR_ISV;
197 }
198
199 static inline bool kvm_vcpu_dabt_iswrite(struct kvm_vcpu *vcpu)
200 {
201         return kvm_vcpu_get_hsr(vcpu) & HSR_WNR;
202 }
203
204 static inline bool kvm_vcpu_dabt_issext(struct kvm_vcpu *vcpu)
205 {
206         return kvm_vcpu_get_hsr(vcpu) & HSR_SSE;
207 }
208
209 static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
210 {
211         return false;
212 }
213
214 static inline int kvm_vcpu_dabt_get_rd(struct kvm_vcpu *vcpu)
215 {
216         return (kvm_vcpu_get_hsr(vcpu) & HSR_SRT_MASK) >> HSR_SRT_SHIFT;
217 }
218
219 static inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu)
220 {
221         return kvm_vcpu_get_hsr(vcpu) & HSR_DABT_S1PTW;
222 }
223
224 static inline bool kvm_vcpu_dabt_is_cm(struct kvm_vcpu *vcpu)
225 {
226         return !!(kvm_vcpu_get_hsr(vcpu) & HSR_DABT_CM);
227 }
228
229 /* Get Access Size from a data abort */
230 static inline int kvm_vcpu_dabt_get_as(struct kvm_vcpu *vcpu)
231 {
232         switch ((kvm_vcpu_get_hsr(vcpu) >> 22) & 0x3) {
233         case 0:
234                 return 1;
235         case 1:
236                 return 2;
237         case 2:
238                 return 4;
239         default:
240                 kvm_err("Hardware is weird: SAS 0b11 is reserved\n");
241                 return -EFAULT;
242         }
243 }
244
245 /* This one is not specific to Data Abort */
246 static inline bool kvm_vcpu_trap_il_is32bit(struct kvm_vcpu *vcpu)
247 {
248         return kvm_vcpu_get_hsr(vcpu) & HSR_IL;
249 }
250
251 static inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
252 {
253         return kvm_vcpu_get_hsr(vcpu) >> HSR_EC_SHIFT;
254 }
255
256 static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
257 {
258         return kvm_vcpu_trap_get_class(vcpu) == HSR_EC_IABT;
259 }
260
261 static inline bool kvm_vcpu_trap_is_exec_fault(const struct kvm_vcpu *vcpu)
262 {
263         return kvm_vcpu_trap_is_iabt(vcpu) && !kvm_vcpu_abt_iss1tw(vcpu);
264 }
265
266 static inline u8 kvm_vcpu_trap_get_fault(struct kvm_vcpu *vcpu)
267 {
268         return kvm_vcpu_get_hsr(vcpu) & HSR_FSC;
269 }
270
271 static inline u8 kvm_vcpu_trap_get_fault_type(struct kvm_vcpu *vcpu)
272 {
273         return kvm_vcpu_get_hsr(vcpu) & HSR_FSC_TYPE;
274 }
275
276 static inline bool kvm_vcpu_dabt_isextabt(struct kvm_vcpu *vcpu)
277 {
278         switch (kvm_vcpu_trap_get_fault(vcpu)) {
279         case FSC_SEA:
280         case FSC_SEA_TTW0:
281         case FSC_SEA_TTW1:
282         case FSC_SEA_TTW2:
283         case FSC_SEA_TTW3:
284         case FSC_SECC:
285         case FSC_SECC_TTW0:
286         case FSC_SECC_TTW1:
287         case FSC_SECC_TTW2:
288         case FSC_SECC_TTW3:
289                 return true;
290         default:
291                 return false;
292         }
293 }
294
295 static inline u32 kvm_vcpu_hvc_get_imm(struct kvm_vcpu *vcpu)
296 {
297         return kvm_vcpu_get_hsr(vcpu) & HSR_HVC_IMM_MASK;
298 }
299
300 static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
301 {
302         return vcpu_cp15(vcpu, c0_MPIDR) & MPIDR_HWID_BITMASK;
303 }
304
305 static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
306 {
307         *vcpu_cpsr(vcpu) |= PSR_E_BIT;
308 }
309
310 static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
311 {
312         return !!(*vcpu_cpsr(vcpu) & PSR_E_BIT);
313 }
314
315 static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
316                                                     unsigned long data,
317                                                     unsigned int len)
318 {
319         if (kvm_vcpu_is_be(vcpu)) {
320                 switch (len) {
321                 case 1:
322                         return data & 0xff;
323                 case 2:
324                         return be16_to_cpu(data & 0xffff);
325                 default:
326                         return be32_to_cpu(data);
327                 }
328         } else {
329                 switch (len) {
330                 case 1:
331                         return data & 0xff;
332                 case 2:
333                         return le16_to_cpu(data & 0xffff);
334                 default:
335                         return le32_to_cpu(data);
336                 }
337         }
338 }
339
340 static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
341                                                     unsigned long data,
342                                                     unsigned int len)
343 {
344         if (kvm_vcpu_is_be(vcpu)) {
345                 switch (len) {
346                 case 1:
347                         return data & 0xff;
348                 case 2:
349                         return cpu_to_be16(data & 0xffff);
350                 default:
351                         return cpu_to_be32(data);
352                 }
353         } else {
354                 switch (len) {
355                 case 1:
356                         return data & 0xff;
357                 case 2:
358                         return cpu_to_le16(data & 0xffff);
359                 default:
360                         return cpu_to_le32(data);
361                 }
362         }
363 }
364
365 #endif /* __ARM_KVM_EMULATE_H__ */