blob: dd98fdf33d99a5b9ca653a398504d1dedcefe574 [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>
Christoffer Dall00536ec2017-12-27 20:01:52 +010029#include <asm/kvm_hyp.h>
Marc Zyngier83a49792012-12-10 13:27:52 +000030#include <asm/kvm_mmio.h>
31#include <asm/ptrace.h>
Andre Przywara4429fc62014-06-02 15:37:13 +020032#include <asm/cputype.h>
Marc Zyngier68908bf2015-01-29 15:47:55 +000033#include <asm/virt.h>
Marc Zyngier83a49792012-12-10 13:27:52 +000034
Marc Zyngierb5476312013-02-06 19:40:29 +000035unsigned long *vcpu_reg32(const struct kvm_vcpu *vcpu, u8 reg_num);
Christoffer Dalla8928192017-12-27 21:59:09 +010036unsigned long vcpu_read_spsr32(const struct kvm_vcpu *vcpu);
37void vcpu_write_spsr32(struct kvm_vcpu *vcpu, unsigned long v);
Marc Zyngierb5476312013-02-06 19:40:29 +000038
Marc Zyngier27b190b2013-02-06 19:54:04 +000039bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
40void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr);
41
Marc Zyngier83a49792012-12-10 13:27:52 +000042void kvm_inject_undefined(struct kvm_vcpu *vcpu);
Marc Zyngier10cf3392016-09-06 14:02:01 +010043void kvm_inject_vabt(struct kvm_vcpu *vcpu);
Marc Zyngier83a49792012-12-10 13:27:52 +000044void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr);
45void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr);
Marc Zyngier74a64a92017-10-29 02:18:09 +000046void kvm_inject_undef32(struct kvm_vcpu *vcpu);
47void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr);
48void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr);
Marc Zyngier83a49792012-12-10 13:27:52 +000049
Christoffer Dalle72341c2017-12-13 22:56:48 +010050static inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
51{
52 return !(vcpu->arch.hcr_el2 & HCR_RW);
53}
54
Christoffer Dallb856a592014-10-16 17:21:16 +020055static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
56{
57 vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
Marc Zyngier68908bf2015-01-29 15:47:55 +000058 if (is_kernel_in_hyp_mode())
59 vcpu->arch.hcr_el2 |= HCR_E2H;
Dongjiu Geng558daf62018-01-15 19:39:06 +000060 if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN)) {
61 /* route synchronous external abort exceptions to EL2 */
62 vcpu->arch.hcr_el2 |= HCR_TEA;
63 /* trap error record accesses */
64 vcpu->arch.hcr_el2 |= HCR_TERR;
65 }
Marc Zyngiere48d53a2018-04-06 12:27:28 +010066 if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
67 vcpu->arch.hcr_el2 |= HCR_FWB;
Dongjiu Geng558daf62018-01-15 19:39:06 +000068
Marc Zyngier801f6772015-01-11 14:10:11 +010069 if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features))
70 vcpu->arch.hcr_el2 &= ~HCR_RW;
Dave Martin005781b2017-12-01 15:19:40 +000071
72 /*
73 * TID3: trap feature register accesses that we virtualise.
74 * For now this is conditional, since no AArch32 feature regs
75 * are currently virtualised.
76 */
Christoffer Dalle72341c2017-12-13 22:56:48 +010077 if (!vcpu_el1_is_32bit(vcpu))
Dave Martin005781b2017-12-01 15:19:40 +000078 vcpu->arch.hcr_el2 |= HCR_TID3;
Christoffer Dallb856a592014-10-16 17:21:16 +020079}
80
Christoffer Dall3df59d82017-08-03 12:09:05 +020081static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
Marc Zyngier3c1e7162014-12-19 16:05:31 +000082{
Christoffer Dall3df59d82017-08-03 12:09:05 +020083 return (unsigned long *)&vcpu->arch.hcr_el2;
Marc Zyngier3c1e7162014-12-19 16:05:31 +000084}
85
James Morse4715c142018-01-15 19:39:01 +000086static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
87{
88 vcpu->arch.vsesr_el2 = vsesr;
89}
90
Marc Zyngier83a49792012-12-10 13:27:52 +000091static inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
92{
93 return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pc;
94}
95
Christoffer Dall6d4bd902017-12-27 20:51:04 +010096static inline unsigned long *__vcpu_elr_el1(const struct kvm_vcpu *vcpu)
Marc Zyngier83a49792012-12-10 13:27:52 +000097{
98 return (unsigned long *)&vcpu_gp_regs(vcpu)->elr_el1;
99}
100
Christoffer Dall6d4bd902017-12-27 20:51:04 +0100101static inline unsigned long vcpu_read_elr_el1(const struct kvm_vcpu *vcpu)
102{
103 if (vcpu->arch.sysregs_loaded_on_cpu)
104 return read_sysreg_el1(elr);
105 else
106 return *__vcpu_elr_el1(vcpu);
107}
108
109static inline void vcpu_write_elr_el1(const struct kvm_vcpu *vcpu, unsigned long v)
110{
111 if (vcpu->arch.sysregs_loaded_on_cpu)
112 write_sysreg_el1(v, elr);
113 else
114 *__vcpu_elr_el1(vcpu) = v;
115}
116
Marc Zyngier83a49792012-12-10 13:27:52 +0000117static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
118{
119 return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pstate;
120}
121
122static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
123{
Marc Zyngierb5476312013-02-06 19:40:29 +0000124 return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT);
Marc Zyngier83a49792012-12-10 13:27:52 +0000125}
126
127static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
128{
Marc Zyngier27b190b2013-02-06 19:54:04 +0000129 if (vcpu_mode_is_32bit(vcpu))
130 return kvm_condition_valid32(vcpu);
131
132 return true;
Marc Zyngier83a49792012-12-10 13:27:52 +0000133}
134
135static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr)
136{
Marc Zyngier27b190b2013-02-06 19:54:04 +0000137 if (vcpu_mode_is_32bit(vcpu))
138 kvm_skip_instr32(vcpu, is_wide_instr);
139 else
140 *vcpu_pc(vcpu) += 4;
Marc Zyngier83a49792012-12-10 13:27:52 +0000141}
142
143static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
144{
Marc Zyngierb5476312013-02-06 19:40:29 +0000145 *vcpu_cpsr(vcpu) |= COMPAT_PSR_T_BIT;
Marc Zyngier83a49792012-12-10 13:27:52 +0000146}
147
Marc Zyngierc0f09632015-11-16 10:28:17 +0000148/*
Pavel Fedinf6be5632015-12-04 15:03:14 +0300149 * vcpu_get_reg and vcpu_set_reg should always be passed a register number
150 * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on
151 * AArch32 with banked registers.
Marc Zyngierc0f09632015-11-16 10:28:17 +0000152 */
Pavel Fedinbc45a512015-12-04 15:03:11 +0300153static inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
154 u8 reg_num)
155{
156 return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs.regs[reg_num];
157}
158
159static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
160 unsigned long val)
161{
162 if (reg_num != 31)
163 vcpu_gp_regs(vcpu)->regs.regs[reg_num] = val;
164}
165
Christoffer Dall00536ec2017-12-27 20:01:52 +0100166static inline unsigned long vcpu_read_spsr(const struct kvm_vcpu *vcpu)
Marc Zyngier83a49792012-12-10 13:27:52 +0000167{
Christoffer Dalla8928192017-12-27 21:59:09 +0100168 if (vcpu_mode_is_32bit(vcpu))
169 return vcpu_read_spsr32(vcpu);
Christoffer Dall00536ec2017-12-27 20:01:52 +0100170
171 if (vcpu->arch.sysregs_loaded_on_cpu)
172 return read_sysreg_el1(spsr);
173 else
Christoffer Dalla8928192017-12-27 21:59:09 +0100174 return vcpu_gp_regs(vcpu)->spsr[KVM_SPSR_EL1];
Christoffer Dall00536ec2017-12-27 20:01:52 +0100175}
176
Christoffer Dalla8928192017-12-27 21:59:09 +0100177static inline void vcpu_write_spsr(struct kvm_vcpu *vcpu, unsigned long v)
Christoffer Dall00536ec2017-12-27 20:01:52 +0100178{
Christoffer Dall00536ec2017-12-27 20:01:52 +0100179 if (vcpu_mode_is_32bit(vcpu)) {
Christoffer Dalla8928192017-12-27 21:59:09 +0100180 vcpu_write_spsr32(vcpu, v);
181 return;
Christoffer Dall00536ec2017-12-27 20:01:52 +0100182 }
183
184 if (vcpu->arch.sysregs_loaded_on_cpu)
185 write_sysreg_el1(v, spsr);
186 else
Christoffer Dalla8928192017-12-27 21:59:09 +0100187 vcpu_gp_regs(vcpu)->spsr[KVM_SPSR_EL1] = v;
Marc Zyngier83a49792012-12-10 13:27:52 +0000188}
189
190static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
191{
Shannon Zhao9586a2e2016-01-13 17:16:39 +0800192 u32 mode;
Marc Zyngier83a49792012-12-10 13:27:52 +0000193
Shannon Zhao9586a2e2016-01-13 17:16:39 +0800194 if (vcpu_mode_is_32bit(vcpu)) {
195 mode = *vcpu_cpsr(vcpu) & COMPAT_PSR_MODE_MASK;
Marc Zyngierb5476312013-02-06 19:40:29 +0000196 return mode > COMPAT_PSR_MODE_USR;
Shannon Zhao9586a2e2016-01-13 17:16:39 +0800197 }
198
199 mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
Marc Zyngierb5476312013-02-06 19:40:29 +0000200
Marc Zyngier83a49792012-12-10 13:27:52 +0000201 return mode != PSR_MODE_EL0t;
202}
203
204static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu)
205{
206 return vcpu->arch.fault.esr_el2;
207}
208
Marc Zyngier3e51d432016-09-06 09:28:41 +0100209static inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
210{
211 u32 esr = kvm_vcpu_get_hsr(vcpu);
212
213 if (esr & ESR_ELx_CV)
214 return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
215
216 return -1;
217}
218
Marc Zyngier83a49792012-12-10 13:27:52 +0000219static inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
220{
221 return vcpu->arch.fault.far_el2;
222}
223
224static inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
225{
226 return ((phys_addr_t)vcpu->arch.fault.hpfar_el2 & HPFAR_MASK) << 8;
227}
228
James Morse0067df42018-01-15 19:39:05 +0000229static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu)
230{
231 return vcpu->arch.fault.disr_el1;
232}
233
Wei Huang0d97f8842015-01-12 11:53:36 -0500234static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
235{
Paolo Bonzini1c6007d2015-01-23 13:39:51 +0100236 return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_xVC_IMM_MASK;
Wei Huang0d97f8842015-01-12 11:53:36 -0500237}
238
Marc Zyngier83a49792012-12-10 13:27:52 +0000239static inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu)
240{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000241 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_ISV);
Marc Zyngier83a49792012-12-10 13:27:52 +0000242}
243
Marc Zyngier83a49792012-12-10 13:27:52 +0000244static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu)
245{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000246 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SSE);
Marc Zyngier83a49792012-12-10 13:27:52 +0000247}
248
249static inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu)
250{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000251 return (kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
Marc Zyngier83a49792012-12-10 13:27:52 +0000252}
253
Marc Zyngier83a49792012-12-10 13:27:52 +0000254static inline bool kvm_vcpu_dabt_iss1tw(const struct kvm_vcpu *vcpu)
255{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000256 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_S1PTW);
Marc Zyngier83a49792012-12-10 13:27:52 +0000257}
258
Will Deacon60e21a02016-09-29 12:37:01 +0100259static inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu)
260{
261 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_WNR) ||
262 kvm_vcpu_dabt_iss1tw(vcpu); /* AF/DBM update */
263}
264
Marc Zyngier57c841f2016-01-29 15:01:28 +0000265static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu)
266{
267 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_CM);
268}
269
Marc Zyngier83a49792012-12-10 13:27:52 +0000270static inline int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)
271{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000272 return 1 << ((kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT);
Marc Zyngier83a49792012-12-10 13:27:52 +0000273}
274
275/* This one is not specific to Data Abort */
276static inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu)
277{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000278 return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_IL);
Marc Zyngier83a49792012-12-10 13:27:52 +0000279}
280
281static inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
282{
Mark Rutland561454e2016-05-31 12:33:02 +0100283 return ESR_ELx_EC(kvm_vcpu_get_hsr(vcpu));
Marc Zyngier83a49792012-12-10 13:27:52 +0000284}
285
286static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
287{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000288 return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW;
Marc Zyngier83a49792012-12-10 13:27:52 +0000289}
290
291static inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)
292{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000293 return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC;
Christoffer Dall0496daa52014-09-26 12:29:34 +0200294}
295
296static inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vcpu)
297{
Mark Rutlandc6d01a92014-11-24 13:59:30 +0000298 return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC_TYPE;
Marc Zyngier83a49792012-12-10 13:27:52 +0000299}
300
James Morsebb428922017-07-18 13:37:41 +0100301static inline bool kvm_vcpu_dabt_isextabt(const struct kvm_vcpu *vcpu)
302{
Dongjiu Genga2b83132017-10-30 14:05:18 +0800303 switch (kvm_vcpu_trap_get_fault(vcpu)) {
James Morsebb428922017-07-18 13:37:41 +0100304 case FSC_SEA:
305 case FSC_SEA_TTW0:
306 case FSC_SEA_TTW1:
307 case FSC_SEA_TTW2:
308 case FSC_SEA_TTW3:
309 case FSC_SECC:
310 case FSC_SECC_TTW0:
311 case FSC_SECC_TTW1:
312 case FSC_SECC_TTW2:
313 case FSC_SECC_TTW3:
314 return true;
315 default:
316 return false;
317 }
318}
319
Marc Zyngierc6671862017-04-27 19:06:48 +0100320static inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
321{
322 u32 esr = kvm_vcpu_get_hsr(vcpu);
323 return (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
324}
325
Andre Przywara4429fc62014-06-02 15:37:13 +0200326static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
Marc Zyngier79c64882013-10-18 18:19:03 +0100327{
Christoffer Dall8d404c42016-03-16 15:38:53 +0100328 return vcpu_read_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
Marc Zyngier79c64882013-10-18 18:19:03 +0100329}
330
Marc Zyngierce94fe92013-11-05 14:12:15 +0000331static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
332{
Christoffer Dall8d404c42016-03-16 15:38:53 +0100333 if (vcpu_mode_is_32bit(vcpu)) {
Marc Zyngierce94fe92013-11-05 14:12:15 +0000334 *vcpu_cpsr(vcpu) |= COMPAT_PSR_E_BIT;
Christoffer Dall8d404c42016-03-16 15:38:53 +0100335 } else {
336 u64 sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1);
337 sctlr |= (1 << 25);
James Morse1975fa52018-05-02 12:17:02 +0100338 vcpu_write_sys_reg(vcpu, sctlr, SCTLR_EL1);
Christoffer Dall8d404c42016-03-16 15:38:53 +0100339 }
Marc Zyngierce94fe92013-11-05 14:12:15 +0000340}
341
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000342static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
343{
344 if (vcpu_mode_is_32bit(vcpu))
345 return !!(*vcpu_cpsr(vcpu) & COMPAT_PSR_E_BIT);
346
Christoffer Dall8d404c42016-03-16 15:38:53 +0100347 return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & (1 << 25));
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000348}
349
350static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
351 unsigned long data,
352 unsigned int len)
353{
354 if (kvm_vcpu_is_be(vcpu)) {
355 switch (len) {
356 case 1:
357 return data & 0xff;
358 case 2:
359 return be16_to_cpu(data & 0xffff);
360 case 4:
361 return be32_to_cpu(data & 0xffffffff);
362 default:
363 return be64_to_cpu(data);
364 }
Victor Kamenskyb3007082014-06-12 09:30:08 -0700365 } else {
366 switch (len) {
367 case 1:
368 return data & 0xff;
369 case 2:
370 return le16_to_cpu(data & 0xffff);
371 case 4:
372 return le32_to_cpu(data & 0xffffffff);
373 default:
374 return le64_to_cpu(data);
375 }
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000376 }
377
378 return data; /* Leave LE untouched */
379}
380
381static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
382 unsigned long data,
383 unsigned int len)
384{
385 if (kvm_vcpu_is_be(vcpu)) {
386 switch (len) {
387 case 1:
388 return data & 0xff;
389 case 2:
390 return cpu_to_be16(data & 0xffff);
391 case 4:
392 return cpu_to_be32(data & 0xffffffff);
393 default:
394 return cpu_to_be64(data);
395 }
Victor Kamenskyb3007082014-06-12 09:30:08 -0700396 } else {
397 switch (len) {
398 case 1:
399 return data & 0xff;
400 case 2:
401 return cpu_to_le16(data & 0xffff);
402 case 4:
403 return cpu_to_le32(data & 0xffffffff);
404 default:
405 return cpu_to_le64(data);
406 }
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000407 }
408
409 return data; /* Leave LE untouched */
410}
411
Marc Zyngier83a49792012-12-10 13:27:52 +0000412#endif /* __ARM64_KVM_EMULATE_H__ */