blob: 3cc535591bdf9b59eea588f26dc4909040823357 [file] [log] [blame]
Marc Zyngier83a49792012-12-10 13:27:52 +00001/*
2 * Copyright (C) 2012,2013 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * Derived from arch/arm/include/kvm_emulate.h
6 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
7 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef __ARM64_KVM_EMULATE_H__
23#define __ARM64_KVM_EMULATE_H__
24
25#include <linux/kvm_host.h>
Mark Rutlandc6d01a92014-11-24 13:59:30 +000026
27#include <asm/esr.h>
Marc Zyngier83a49792012-12-10 13:27:52 +000028#include <asm/kvm_arm.h>
29#include <asm/kvm_mmio.h>
30#include <asm/ptrace.h>
Andre Przywara4429fc62014-06-02 15:37:13 +020031#include <asm/cputype.h>
Marc Zyngier68908bf2015-01-29 15:47:55 +000032#include <asm/virt.h>
Marc Zyngier83a49792012-12-10 13:27:52 +000033
Marc Zyngierb5476312013-02-06 19:40:29 +000034unsigned long *vcpu_reg32(const struct kvm_vcpu *vcpu, u8 reg_num);
35unsigned long *vcpu_spsr32(const struct kvm_vcpu *vcpu);
36
Marc Zyngier27b190b2013-02-06 19:54:04 +000037bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
38void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr);
39
Marc Zyngier83a49792012-12-10 13:27:52 +000040void kvm_inject_undefined(struct kvm_vcpu *vcpu);
Marc Zyngier10cf3392016-09-06 14:02:01 +010041void kvm_inject_vabt(struct kvm_vcpu *vcpu);
Marc Zyngier83a49792012-12-10 13:27:52 +000042void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr);
43void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr);
Marc Zyngier74a64a92017-10-29 02:18:09 +000044void kvm_inject_undef32(struct kvm_vcpu *vcpu);
45void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr);
46void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr);
Marc Zyngier83a49792012-12-10 13:27:52 +000047
Christoffer Dalle72341c2017-12-13 22:56:48 +010048static inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
49{
50 return !(vcpu->arch.hcr_el2 & HCR_RW);
51}
52
Christoffer Dallb856a592014-10-16 17:21:16 +020053static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
54{
55 vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
Marc Zyngier68908bf2015-01-29 15:47:55 +000056 if (is_kernel_in_hyp_mode())
57 vcpu->arch.hcr_el2 |= HCR_E2H;
Dongjiu Geng558daf62018-01-15 19:39:06 +000058 if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN)) {
59 /* route synchronous external abort exceptions to EL2 */
60 vcpu->arch.hcr_el2 |= HCR_TEA;
61 /* trap error record accesses */
62 vcpu->arch.hcr_el2 |= HCR_TERR;
63 }
64
Marc Zyngier801f6772015-01-11 14:10:11 +010065 if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features))
66 vcpu->arch.hcr_el2 &= ~HCR_RW;
Dave Martin005781b2017-12-01 15:19:40 +000067
68 /*
69 * TID3: trap feature register accesses that we virtualise.
70 * For now this is conditional, since no AArch32 feature regs
71 * are currently virtualised.
72 */
Christoffer Dalle72341c2017-12-13 22:56:48 +010073 if (!vcpu_el1_is_32bit(vcpu))
Dave Martin005781b2017-12-01 15:19:40 +000074 vcpu->arch.hcr_el2 |= HCR_TID3;
Christoffer Dallb856a592014-10-16 17:21:16 +020075}
76
Christoffer Dall3df59d82017-08-03 12:09:05 +020077static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
Marc Zyngier3c1e7162014-12-19 16:05:31 +000078{
Christoffer Dall3df59d82017-08-03 12:09:05 +020079 return (unsigned long *)&vcpu->arch.hcr_el2;
Marc Zyngier3c1e7162014-12-19 16:05:31 +000080}
81
James Morse4715c142018-01-15 19:39:01 +000082static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
83{
84 vcpu->arch.vsesr_el2 = vsesr;
85}
86
Marc Zyngier83a49792012-12-10 13:27:52 +000087static inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
88{
89 return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pc;
90}
91
92static inline unsigned long *vcpu_elr_el1(const struct kvm_vcpu *vcpu)
93{
94 return (unsigned long *)&vcpu_gp_regs(vcpu)->elr_el1;
95}
96
97static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
98{
99 return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pstate;
100}
101
102static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
103{
Marc Zyngierb5476312013-02-06 19:40:29 +0000104 return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT);
Marc Zyngier83a49792012-12-10 13:27:52 +0000105}
106
107static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
108{
Marc Zyngier27b190b2013-02-06 19:54:04 +0000109 if (vcpu_mode_is_32bit(vcpu))
110 return kvm_condition_valid32(vcpu);
111
112 return true;
Marc Zyngier83a49792012-12-10 13:27:52 +0000113}
114
115static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr)
116{
Marc Zyngier27b190b2013-02-06 19:54:04 +0000117 if (vcpu_mode_is_32bit(vcpu))
118 kvm_skip_instr32(vcpu, is_wide_instr);
119 else
120 *vcpu_pc(vcpu) += 4;
Marc Zyngier83a49792012-12-10 13:27:52 +0000121}
122
123static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
124{
Marc Zyngierb5476312013-02-06 19:40:29 +0000125 *vcpu_cpsr(vcpu) |= COMPAT_PSR_T_BIT;
Marc Zyngier83a49792012-12-10 13:27:52 +0000126}
127
Marc Zyngierc0f09632015-11-16 10:28:17 +0000128/*
Pavel Fedinf6be5632015-12-04 15:03:14 +0300129 * vcpu_get_reg and vcpu_set_reg should always be passed a register number
130 * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on
131 * AArch32 with banked registers.
Marc Zyngierc0f09632015-11-16 10:28:17 +0000132 */
Pavel Fedinbc45a512015-12-04 15:03:11 +0300133static inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
134 u8 reg_num)
135{
136 return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs.regs[reg_num];
137}
138
139static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
140 unsigned long val)
141{
142 if (reg_num != 31)
143 vcpu_gp_regs(vcpu)->regs.regs[reg_num] = val;
144}
145
Marc Zyngier83a49792012-12-10 13:27:52 +0000146/* Get vcpu SPSR for current mode */
147static inline unsigned long *vcpu_spsr(const struct kvm_vcpu *vcpu)
148{
Marc Zyngierb5476312013-02-06 19:40:29 +0000149 if (vcpu_mode_is_32bit(vcpu))
150 return vcpu_spsr32(vcpu);
151
Marc Zyngier83a49792012-12-10 13:27:52 +0000152 return (unsigned long *)&vcpu_gp_regs(vcpu)->spsr[KVM_SPSR_EL1];
153}
154
155static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
156{
Shannon Zhao9586a2e2016-01-13 17:16:39 +0800157 u32 mode;
Marc Zyngier83a49792012-12-10 13:27:52 +0000158
Shannon Zhao9586a2e2016-01-13 17:16:39 +0800159 if (vcpu_mode_is_32bit(vcpu)) {
160 mode = *vcpu_cpsr(vcpu) & COMPAT_PSR_MODE_MASK;
Marc Zyngierb5476312013-02-06 19:40:29 +0000161 return mode > COMPAT_PSR_MODE_USR;
Shannon Zhao9586a2e2016-01-13 17:16:39 +0800162 }
163
164 mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
Marc Zyngierb5476312013-02-06 19:40:29 +0000165
Marc Zyngier83a49792012-12-10 13:27:52 +0000166 return mode != PSR_MODE_EL0t;
167}
168
169static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu)
170{
171 return vcpu->arch.fault.esr_el2;
172}
173
Marc Zyngier3e51d432016-09-06 09:28:41 +0100174static inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
175{
176 u32 esr = kvm_vcpu_get_hsr(vcpu);
177
178 if (esr & ESR_ELx_CV)
179 return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
180
181 return -1;
182}
183
Marc Zyngier83a49792012-12-10 13:27:52 +0000184static inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
185{
186 return vcpu->arch.fault.far_el2;
187}
188
189static inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
190{
191 return ((phys_addr_t)vcpu->arch.fault.hpfar_el2 & HPFAR_MASK) << 8;
192}
193
James Morse0067df42018-01-15 19:39:05 +0000194static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu)
195{
196 return vcpu->arch.fault.disr_el1;
197}
198
Wei Huang0d97f8842015-01-12 11:53:36 -0500199static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
200{
Paolo Bonzini1c6007d2015-01-23 13:39:51 +0100201 return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_xVC_IMM_MASK;
Wei Huang0d97f8842015-01-12 11:53:36 -0500202}
203
Marc Zyngier83a49792012-12-10 13:27:52 +0000204static inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu)
205{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000206 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_ISV);
Marc Zyngier83a49792012-12-10 13:27:52 +0000207}
208
Marc Zyngier83a49792012-12-10 13:27:52 +0000209static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu)
210{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000211 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SSE);
Marc Zyngier83a49792012-12-10 13:27:52 +0000212}
213
214static inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu)
215{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000216 return (kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
Marc Zyngier83a49792012-12-10 13:27:52 +0000217}
218
Marc Zyngier83a49792012-12-10 13:27:52 +0000219static inline bool kvm_vcpu_dabt_iss1tw(const struct kvm_vcpu *vcpu)
220{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000221 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_S1PTW);
Marc Zyngier83a49792012-12-10 13:27:52 +0000222}
223
Will Deacon60e21a02016-09-29 12:37:01 +0100224static inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu)
225{
226 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_WNR) ||
227 kvm_vcpu_dabt_iss1tw(vcpu); /* AF/DBM update */
228}
229
Marc Zyngier57c841f2016-01-29 15:01:28 +0000230static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu)
231{
232 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_CM);
233}
234
Marc Zyngier83a49792012-12-10 13:27:52 +0000235static inline int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)
236{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000237 return 1 << ((kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT);
Marc Zyngier83a49792012-12-10 13:27:52 +0000238}
239
240/* This one is not specific to Data Abort */
241static inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu)
242{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000243 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_IL);
Marc Zyngier83a49792012-12-10 13:27:52 +0000244}
245
246static inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
247{
Mark Rutland561454e2016-05-31 12:33:02 +0100248 return ESR_ELx_EC(kvm_vcpu_get_hsr(vcpu));
Marc Zyngier83a49792012-12-10 13:27:52 +0000249}
250
251static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
252{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000253 return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW;
Marc Zyngier83a49792012-12-10 13:27:52 +0000254}
255
256static inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)
257{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000258 return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC;
Christoffer Dall0496daa52014-09-26 12:29:34 +0200259}
260
261static inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vcpu)
262{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000263 return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC_TYPE;
Marc Zyngier83a49792012-12-10 13:27:52 +0000264}
265
James Morsebb428922017-07-18 13:37:41 +0100266static inline bool kvm_vcpu_dabt_isextabt(const struct kvm_vcpu *vcpu)
267{
Dongjiu Genga2b83132017-10-30 14:05:18 +0800268 switch (kvm_vcpu_trap_get_fault(vcpu)) {
James Morsebb428922017-07-18 13:37:41 +0100269 case FSC_SEA:
270 case FSC_SEA_TTW0:
271 case FSC_SEA_TTW1:
272 case FSC_SEA_TTW2:
273 case FSC_SEA_TTW3:
274 case FSC_SECC:
275 case FSC_SECC_TTW0:
276 case FSC_SECC_TTW1:
277 case FSC_SECC_TTW2:
278 case FSC_SECC_TTW3:
279 return true;
280 default:
281 return false;
282 }
283}
284
Marc Zyngierc6671862017-04-27 19:06:48 +0100285static inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
286{
287 u32 esr = kvm_vcpu_get_hsr(vcpu);
288 return (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
289}
290
Andre Przywara4429fc62014-06-02 15:37:13 +0200291static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
Marc Zyngier79c64882013-10-18 18:19:03 +0100292{
Andre Przywara4429fc62014-06-02 15:37:13 +0200293 return vcpu_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
Marc Zyngier79c64882013-10-18 18:19:03 +0100294}
295
Marc Zyngierce94fe92013-11-05 14:12:15 +0000296static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
297{
298 if (vcpu_mode_is_32bit(vcpu))
299 *vcpu_cpsr(vcpu) |= COMPAT_PSR_E_BIT;
300 else
301 vcpu_sys_reg(vcpu, SCTLR_EL1) |= (1 << 25);
302}
303
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000304static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
305{
306 if (vcpu_mode_is_32bit(vcpu))
307 return !!(*vcpu_cpsr(vcpu) & COMPAT_PSR_E_BIT);
308
309 return !!(vcpu_sys_reg(vcpu, SCTLR_EL1) & (1 << 25));
310}
311
312static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
313 unsigned long data,
314 unsigned int len)
315{
316 if (kvm_vcpu_is_be(vcpu)) {
317 switch (len) {
318 case 1:
319 return data & 0xff;
320 case 2:
321 return be16_to_cpu(data & 0xffff);
322 case 4:
323 return be32_to_cpu(data & 0xffffffff);
324 default:
325 return be64_to_cpu(data);
326 }
Victor Kamenskyb3007082014-06-12 09:30:08 -0700327 } else {
328 switch (len) {
329 case 1:
330 return data & 0xff;
331 case 2:
332 return le16_to_cpu(data & 0xffff);
333 case 4:
334 return le32_to_cpu(data & 0xffffffff);
335 default:
336 return le64_to_cpu(data);
337 }
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000338 }
339
340 return data; /* Leave LE untouched */
341}
342
343static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
344 unsigned long data,
345 unsigned int len)
346{
347 if (kvm_vcpu_is_be(vcpu)) {
348 switch (len) {
349 case 1:
350 return data & 0xff;
351 case 2:
352 return cpu_to_be16(data & 0xffff);
353 case 4:
354 return cpu_to_be32(data & 0xffffffff);
355 default:
356 return cpu_to_be64(data);
357 }
Victor Kamenskyb3007082014-06-12 09:30:08 -0700358 } else {
359 switch (len) {
360 case 1:
361 return data & 0xff;
362 case 2:
363 return cpu_to_le16(data & 0xffff);
364 case 4:
365 return cpu_to_le32(data & 0xffffffff);
366 default:
367 return cpu_to_le64(data);
368 }
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000369 }
370
371 return data; /* Leave LE untouched */
372}
373
Marc Zyngier83a49792012-12-10 13:27:52 +0000374#endif /* __ARM64_KVM_EMULATE_H__ */