Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012,2013 - ARM Ltd |
| 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
| 5 | * |
| 6 | * Derived from arch/arm/include/kvm_emulate.h |
| 7 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University |
| 8 | * Author: Christoffer Dall <c.dall@virtualopensystems.com> |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef __ARM64_KVM_EMULATE_H__ |
| 12 | #define __ARM64_KVM_EMULATE_H__ |
| 13 | |
| 14 | #include <linux/kvm_host.h> |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 15 | |
Mark Rutland | bd7d95c | 2018-11-09 15:07:11 +0000 | [diff] [blame] | 16 | #include <asm/debug-monitors.h> |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 17 | #include <asm/esr.h> |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 18 | #include <asm/kvm_arm.h> |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 19 | #include <asm/kvm_hyp.h> |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 20 | #include <asm/kvm_mmio.h> |
| 21 | #include <asm/ptrace.h> |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 22 | #include <asm/cputype.h> |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 23 | #include <asm/virt.h> |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 24 | |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 25 | unsigned long *vcpu_reg32(const struct kvm_vcpu *vcpu, u8 reg_num); |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 26 | unsigned long vcpu_read_spsr32(const struct kvm_vcpu *vcpu); |
| 27 | void vcpu_write_spsr32(struct kvm_vcpu *vcpu, unsigned long v); |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 28 | |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 29 | bool kvm_condition_valid32(const struct kvm_vcpu *vcpu); |
| 30 | void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr); |
| 31 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 32 | void kvm_inject_undefined(struct kvm_vcpu *vcpu); |
Marc Zyngier | 10cf339 | 2016-09-06 14:02:01 +0100 | [diff] [blame] | 33 | void kvm_inject_vabt(struct kvm_vcpu *vcpu); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 34 | void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr); |
| 35 | void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr); |
Marc Zyngier | 74a64a9 | 2017-10-29 02:18:09 +0000 | [diff] [blame] | 36 | void kvm_inject_undef32(struct kvm_vcpu *vcpu); |
| 37 | void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr); |
| 38 | void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 39 | |
Christoffer Dall | e72341c | 2017-12-13 22:56:48 +0100 | [diff] [blame] | 40 | static inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) |
| 41 | { |
| 42 | return !(vcpu->arch.hcr_el2 & HCR_RW); |
| 43 | } |
| 44 | |
Christoffer Dall | b856a59 | 2014-10-16 17:21:16 +0200 | [diff] [blame] | 45 | static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu) |
| 46 | { |
| 47 | vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS; |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 48 | if (is_kernel_in_hyp_mode()) |
| 49 | vcpu->arch.hcr_el2 |= HCR_E2H; |
Dongjiu Geng | 558daf6 | 2018-01-15 19:39:06 +0000 | [diff] [blame] | 50 | if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN)) { |
| 51 | /* route synchronous external abort exceptions to EL2 */ |
| 52 | vcpu->arch.hcr_el2 |= HCR_TEA; |
| 53 | /* trap error record accesses */ |
| 54 | vcpu->arch.hcr_el2 |= HCR_TERR; |
| 55 | } |
Marc Zyngier | e48d53a | 2018-04-06 12:27:28 +0100 | [diff] [blame] | 56 | if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) |
| 57 | vcpu->arch.hcr_el2 |= HCR_FWB; |
Dongjiu Geng | 558daf6 | 2018-01-15 19:39:06 +0000 | [diff] [blame] | 58 | |
Marc Zyngier | 801f677 | 2015-01-11 14:10:11 +0100 | [diff] [blame] | 59 | if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) |
| 60 | vcpu->arch.hcr_el2 &= ~HCR_RW; |
Dave Martin | 005781b | 2017-12-01 15:19:40 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * TID3: trap feature register accesses that we virtualise. |
| 64 | * For now this is conditional, since no AArch32 feature regs |
| 65 | * are currently virtualised. |
| 66 | */ |
Christoffer Dall | e72341c | 2017-12-13 22:56:48 +0100 | [diff] [blame] | 67 | if (!vcpu_el1_is_32bit(vcpu)) |
Dave Martin | 005781b | 2017-12-01 15:19:40 +0000 | [diff] [blame] | 68 | vcpu->arch.hcr_el2 |= HCR_TID3; |
Ard Biesheuvel | f7f2b15 | 2019-01-31 14:17:17 +0100 | [diff] [blame] | 69 | |
Ard Biesheuvel | 793acf8 | 2019-01-31 14:17:18 +0100 | [diff] [blame] | 70 | if (cpus_have_const_cap(ARM64_MISMATCHED_CACHE_TYPE) || |
| 71 | vcpu_el1_is_32bit(vcpu)) |
Ard Biesheuvel | f7f2b15 | 2019-01-31 14:17:17 +0100 | [diff] [blame] | 72 | vcpu->arch.hcr_el2 |= HCR_TID2; |
Christoffer Dall | b856a59 | 2014-10-16 17:21:16 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Christoffer Dall | 3df59d8 | 2017-08-03 12:09:05 +0200 | [diff] [blame] | 75 | static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu) |
Marc Zyngier | 3c1e716 | 2014-12-19 16:05:31 +0000 | [diff] [blame] | 76 | { |
Christoffer Dall | 3df59d8 | 2017-08-03 12:09:05 +0200 | [diff] [blame] | 77 | return (unsigned long *)&vcpu->arch.hcr_el2; |
Marc Zyngier | 3c1e716 | 2014-12-19 16:05:31 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Marc Zyngier | de73708 | 2018-06-21 10:43:59 +0100 | [diff] [blame] | 80 | static inline void vcpu_clear_wfe_traps(struct kvm_vcpu *vcpu) |
| 81 | { |
| 82 | vcpu->arch.hcr_el2 &= ~HCR_TWE; |
| 83 | } |
| 84 | |
| 85 | static inline void vcpu_set_wfe_traps(struct kvm_vcpu *vcpu) |
| 86 | { |
| 87 | vcpu->arch.hcr_el2 |= HCR_TWE; |
| 88 | } |
| 89 | |
Mark Rutland | 384b40c | 2019-04-23 10:12:35 +0530 | [diff] [blame] | 90 | static inline void vcpu_ptrauth_enable(struct kvm_vcpu *vcpu) |
| 91 | { |
| 92 | vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK); |
| 93 | } |
| 94 | |
| 95 | static inline void vcpu_ptrauth_disable(struct kvm_vcpu *vcpu) |
| 96 | { |
| 97 | vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK); |
| 98 | } |
| 99 | |
| 100 | static inline void vcpu_ptrauth_setup_lazy(struct kvm_vcpu *vcpu) |
| 101 | { |
| 102 | if (vcpu_has_ptrauth(vcpu)) |
| 103 | vcpu_ptrauth_disable(vcpu); |
| 104 | } |
| 105 | |
Dongjiu Geng | b7b27fa | 2018-07-19 16:24:22 +0100 | [diff] [blame] | 106 | static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu) |
| 107 | { |
| 108 | return vcpu->arch.vsesr_el2; |
| 109 | } |
| 110 | |
James Morse | 4715c14 | 2018-01-15 19:39:01 +0000 | [diff] [blame] | 111 | static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr) |
| 112 | { |
| 113 | vcpu->arch.vsesr_el2 = vsesr; |
| 114 | } |
| 115 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 116 | static inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu) |
| 117 | { |
| 118 | return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pc; |
| 119 | } |
| 120 | |
Christoffer Dall | 6d4bd90 | 2017-12-27 20:51:04 +0100 | [diff] [blame] | 121 | static inline unsigned long *__vcpu_elr_el1(const struct kvm_vcpu *vcpu) |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 122 | { |
| 123 | return (unsigned long *)&vcpu_gp_regs(vcpu)->elr_el1; |
| 124 | } |
| 125 | |
Christoffer Dall | 6d4bd90 | 2017-12-27 20:51:04 +0100 | [diff] [blame] | 126 | static inline unsigned long vcpu_read_elr_el1(const struct kvm_vcpu *vcpu) |
| 127 | { |
| 128 | if (vcpu->arch.sysregs_loaded_on_cpu) |
| 129 | return read_sysreg_el1(elr); |
| 130 | else |
| 131 | return *__vcpu_elr_el1(vcpu); |
| 132 | } |
| 133 | |
| 134 | static inline void vcpu_write_elr_el1(const struct kvm_vcpu *vcpu, unsigned long v) |
| 135 | { |
| 136 | if (vcpu->arch.sysregs_loaded_on_cpu) |
| 137 | write_sysreg_el1(v, elr); |
| 138 | else |
| 139 | *__vcpu_elr_el1(vcpu) = v; |
| 140 | } |
| 141 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 142 | static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu) |
| 143 | { |
| 144 | return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pstate; |
| 145 | } |
| 146 | |
| 147 | static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu) |
| 148 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 149 | return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu) |
| 153 | { |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 154 | if (vcpu_mode_is_32bit(vcpu)) |
| 155 | return kvm_condition_valid32(vcpu); |
| 156 | |
| 157 | return true; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 160 | static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu) |
| 161 | { |
Mark Rutland | 256c096 | 2018-07-05 15:16:53 +0100 | [diff] [blame] | 162 | *vcpu_cpsr(vcpu) |= PSR_AA32_T_BIT; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Marc Zyngier | c0f0963 | 2015-11-16 10:28:17 +0000 | [diff] [blame] | 165 | /* |
Pavel Fedin | f6be563 | 2015-12-04 15:03:14 +0300 | [diff] [blame] | 166 | * vcpu_get_reg and vcpu_set_reg should always be passed a register number |
| 167 | * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on |
| 168 | * AArch32 with banked registers. |
Marc Zyngier | c0f0963 | 2015-11-16 10:28:17 +0000 | [diff] [blame] | 169 | */ |
Pavel Fedin | bc45a51 | 2015-12-04 15:03:11 +0300 | [diff] [blame] | 170 | static inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu, |
| 171 | u8 reg_num) |
| 172 | { |
| 173 | return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs.regs[reg_num]; |
| 174 | } |
| 175 | |
| 176 | static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num, |
| 177 | unsigned long val) |
| 178 | { |
| 179 | if (reg_num != 31) |
| 180 | vcpu_gp_regs(vcpu)->regs.regs[reg_num] = val; |
| 181 | } |
| 182 | |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 183 | static inline unsigned long vcpu_read_spsr(const struct kvm_vcpu *vcpu) |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 184 | { |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 185 | if (vcpu_mode_is_32bit(vcpu)) |
| 186 | return vcpu_read_spsr32(vcpu); |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 187 | |
| 188 | if (vcpu->arch.sysregs_loaded_on_cpu) |
| 189 | return read_sysreg_el1(spsr); |
| 190 | else |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 191 | return vcpu_gp_regs(vcpu)->spsr[KVM_SPSR_EL1]; |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 194 | static inline void vcpu_write_spsr(struct kvm_vcpu *vcpu, unsigned long v) |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 195 | { |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 196 | if (vcpu_mode_is_32bit(vcpu)) { |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 197 | vcpu_write_spsr32(vcpu, v); |
| 198 | return; |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if (vcpu->arch.sysregs_loaded_on_cpu) |
| 202 | write_sysreg_el1(v, spsr); |
| 203 | else |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 204 | vcpu_gp_regs(vcpu)->spsr[KVM_SPSR_EL1] = v; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu) |
| 208 | { |
Shannon Zhao | 9586a2e | 2016-01-13 17:16:39 +0800 | [diff] [blame] | 209 | u32 mode; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 210 | |
Shannon Zhao | 9586a2e | 2016-01-13 17:16:39 +0800 | [diff] [blame] | 211 | if (vcpu_mode_is_32bit(vcpu)) { |
Mark Rutland | 256c096 | 2018-07-05 15:16:53 +0100 | [diff] [blame] | 212 | mode = *vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK; |
| 213 | return mode > PSR_AA32_MODE_USR; |
Shannon Zhao | 9586a2e | 2016-01-13 17:16:39 +0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK; |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 217 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 218 | return mode != PSR_MODE_EL0t; |
| 219 | } |
| 220 | |
| 221 | static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu) |
| 222 | { |
| 223 | return vcpu->arch.fault.esr_el2; |
| 224 | } |
| 225 | |
Marc Zyngier | 3e51d43 | 2016-09-06 09:28:41 +0100 | [diff] [blame] | 226 | static inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu) |
| 227 | { |
| 228 | u32 esr = kvm_vcpu_get_hsr(vcpu); |
| 229 | |
| 230 | if (esr & ESR_ELx_CV) |
| 231 | return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT; |
| 232 | |
| 233 | return -1; |
| 234 | } |
| 235 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 236 | static inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu) |
| 237 | { |
| 238 | return vcpu->arch.fault.far_el2; |
| 239 | } |
| 240 | |
| 241 | static inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu) |
| 242 | { |
| 243 | return ((phys_addr_t)vcpu->arch.fault.hpfar_el2 & HPFAR_MASK) << 8; |
| 244 | } |
| 245 | |
James Morse | 0067df4 | 2018-01-15 19:39:05 +0000 | [diff] [blame] | 246 | static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu) |
| 247 | { |
| 248 | return vcpu->arch.fault.disr_el1; |
| 249 | } |
| 250 | |
Wei Huang | 0d97f884 | 2015-01-12 11:53:36 -0500 | [diff] [blame] | 251 | static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu) |
| 252 | { |
Paolo Bonzini | 1c6007d | 2015-01-23 13:39:51 +0100 | [diff] [blame] | 253 | return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_xVC_IMM_MASK; |
Wei Huang | 0d97f884 | 2015-01-12 11:53:36 -0500 | [diff] [blame] | 254 | } |
| 255 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 256 | static inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu) |
| 257 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 258 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_ISV); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 261 | static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu) |
| 262 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 263 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SSE); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu) |
| 267 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 268 | return (kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 271 | static inline bool kvm_vcpu_dabt_iss1tw(const struct kvm_vcpu *vcpu) |
| 272 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 273 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_S1PTW); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Will Deacon | 60e21a0 | 2016-09-29 12:37:01 +0100 | [diff] [blame] | 276 | static inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu) |
| 277 | { |
| 278 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_WNR) || |
| 279 | kvm_vcpu_dabt_iss1tw(vcpu); /* AF/DBM update */ |
| 280 | } |
| 281 | |
Marc Zyngier | 57c841f | 2016-01-29 15:01:28 +0000 | [diff] [blame] | 282 | static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu) |
| 283 | { |
| 284 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_CM); |
| 285 | } |
| 286 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 287 | static inline int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu) |
| 288 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 289 | return 1 << ((kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* This one is not specific to Data Abort */ |
| 293 | static inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu) |
| 294 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 295 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_IL); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu) |
| 299 | { |
Mark Rutland | 561454e | 2016-05-31 12:33:02 +0100 | [diff] [blame] | 300 | return ESR_ELx_EC(kvm_vcpu_get_hsr(vcpu)); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu) |
| 304 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 305 | return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu) |
| 309 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 310 | return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC; |
Christoffer Dall | 0496daa5 | 2014-09-26 12:29:34 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vcpu) |
| 314 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 315 | return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC_TYPE; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 316 | } |
| 317 | |
James Morse | bb42892 | 2017-07-18 13:37:41 +0100 | [diff] [blame] | 318 | static inline bool kvm_vcpu_dabt_isextabt(const struct kvm_vcpu *vcpu) |
| 319 | { |
Dongjiu Geng | a2b8313 | 2017-10-30 14:05:18 +0800 | [diff] [blame] | 320 | switch (kvm_vcpu_trap_get_fault(vcpu)) { |
James Morse | bb42892 | 2017-07-18 13:37:41 +0100 | [diff] [blame] | 321 | case FSC_SEA: |
| 322 | case FSC_SEA_TTW0: |
| 323 | case FSC_SEA_TTW1: |
| 324 | case FSC_SEA_TTW2: |
| 325 | case FSC_SEA_TTW3: |
| 326 | case FSC_SECC: |
| 327 | case FSC_SECC_TTW0: |
| 328 | case FSC_SECC_TTW1: |
| 329 | case FSC_SECC_TTW2: |
| 330 | case FSC_SECC_TTW3: |
| 331 | return true; |
| 332 | default: |
| 333 | return false; |
| 334 | } |
| 335 | } |
| 336 | |
Marc Zyngier | c667186 | 2017-04-27 19:06:48 +0100 | [diff] [blame] | 337 | static inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu) |
| 338 | { |
| 339 | u32 esr = kvm_vcpu_get_hsr(vcpu); |
Anshuman Khandual | 1c83914 | 2018-09-20 09:36:19 +0530 | [diff] [blame] | 340 | return ESR_ELx_SYS64_ISS_RT(esr); |
Marc Zyngier | c667186 | 2017-04-27 19:06:48 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Christoffer Dall | 64cf98f | 2016-05-01 22:29:58 +0200 | [diff] [blame] | 343 | static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu) |
| 344 | { |
| 345 | if (kvm_vcpu_trap_is_iabt(vcpu)) |
| 346 | return false; |
| 347 | |
| 348 | return kvm_vcpu_dabt_iswrite(vcpu); |
| 349 | } |
| 350 | |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 351 | static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu) |
Marc Zyngier | 79c6488 | 2013-10-18 18:19:03 +0100 | [diff] [blame] | 352 | { |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 353 | return vcpu_read_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK; |
Marc Zyngier | 79c6488 | 2013-10-18 18:19:03 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame^] | 356 | static inline bool kvm_arm_get_vcpu_workaround_2_flag(struct kvm_vcpu *vcpu) |
| 357 | { |
| 358 | return vcpu->arch.workaround_flags & VCPU_WORKAROUND_2_FLAG; |
| 359 | } |
| 360 | |
| 361 | static inline void kvm_arm_set_vcpu_workaround_2_flag(struct kvm_vcpu *vcpu, |
| 362 | bool flag) |
| 363 | { |
| 364 | if (flag) |
| 365 | vcpu->arch.workaround_flags |= VCPU_WORKAROUND_2_FLAG; |
| 366 | else |
| 367 | vcpu->arch.workaround_flags &= ~VCPU_WORKAROUND_2_FLAG; |
| 368 | } |
| 369 | |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 370 | static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu) |
| 371 | { |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 372 | if (vcpu_mode_is_32bit(vcpu)) { |
Mark Rutland | 256c096 | 2018-07-05 15:16:53 +0100 | [diff] [blame] | 373 | *vcpu_cpsr(vcpu) |= PSR_AA32_E_BIT; |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 374 | } else { |
| 375 | u64 sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1); |
| 376 | sctlr |= (1 << 25); |
James Morse | 1975fa5 | 2018-05-02 12:17:02 +0100 | [diff] [blame] | 377 | vcpu_write_sys_reg(vcpu, sctlr, SCTLR_EL1); |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 378 | } |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 381 | static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu) |
| 382 | { |
| 383 | if (vcpu_mode_is_32bit(vcpu)) |
Mark Rutland | 256c096 | 2018-07-05 15:16:53 +0100 | [diff] [blame] | 384 | return !!(*vcpu_cpsr(vcpu) & PSR_AA32_E_BIT); |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 385 | |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 386 | return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & (1 << 25)); |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu, |
| 390 | unsigned long data, |
| 391 | unsigned int len) |
| 392 | { |
| 393 | if (kvm_vcpu_is_be(vcpu)) { |
| 394 | switch (len) { |
| 395 | case 1: |
| 396 | return data & 0xff; |
| 397 | case 2: |
| 398 | return be16_to_cpu(data & 0xffff); |
| 399 | case 4: |
| 400 | return be32_to_cpu(data & 0xffffffff); |
| 401 | default: |
| 402 | return be64_to_cpu(data); |
| 403 | } |
Victor Kamensky | b300708 | 2014-06-12 09:30:08 -0700 | [diff] [blame] | 404 | } else { |
| 405 | switch (len) { |
| 406 | case 1: |
| 407 | return data & 0xff; |
| 408 | case 2: |
| 409 | return le16_to_cpu(data & 0xffff); |
| 410 | case 4: |
| 411 | return le32_to_cpu(data & 0xffffffff); |
| 412 | default: |
| 413 | return le64_to_cpu(data); |
| 414 | } |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | return data; /* Leave LE untouched */ |
| 418 | } |
| 419 | |
| 420 | static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu, |
| 421 | unsigned long data, |
| 422 | unsigned int len) |
| 423 | { |
| 424 | if (kvm_vcpu_is_be(vcpu)) { |
| 425 | switch (len) { |
| 426 | case 1: |
| 427 | return data & 0xff; |
| 428 | case 2: |
| 429 | return cpu_to_be16(data & 0xffff); |
| 430 | case 4: |
| 431 | return cpu_to_be32(data & 0xffffffff); |
| 432 | default: |
| 433 | return cpu_to_be64(data); |
| 434 | } |
Victor Kamensky | b300708 | 2014-06-12 09:30:08 -0700 | [diff] [blame] | 435 | } else { |
| 436 | switch (len) { |
| 437 | case 1: |
| 438 | return data & 0xff; |
| 439 | case 2: |
| 440 | return cpu_to_le16(data & 0xffff); |
| 441 | case 4: |
| 442 | return cpu_to_le32(data & 0xffffffff); |
| 443 | default: |
| 444 | return cpu_to_le64(data); |
| 445 | } |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | return data; /* Leave LE untouched */ |
| 449 | } |
| 450 | |
Mark Rutland | bd7d95c | 2018-11-09 15:07:11 +0000 | [diff] [blame] | 451 | static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr) |
| 452 | { |
| 453 | if (vcpu_mode_is_32bit(vcpu)) |
| 454 | kvm_skip_instr32(vcpu, is_wide_instr); |
| 455 | else |
| 456 | *vcpu_pc(vcpu) += 4; |
| 457 | |
| 458 | /* advance the singlestep state machine */ |
| 459 | *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Skip an instruction which has been emulated at hyp while most guest sysregs |
| 464 | * are live. |
| 465 | */ |
| 466 | static inline void __hyp_text __kvm_skip_instr(struct kvm_vcpu *vcpu) |
| 467 | { |
| 468 | *vcpu_pc(vcpu) = read_sysreg_el2(elr); |
| 469 | vcpu->arch.ctxt.gp_regs.regs.pstate = read_sysreg_el2(spsr); |
| 470 | |
| 471 | kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu)); |
| 472 | |
| 473 | write_sysreg_el2(vcpu->arch.ctxt.gp_regs.regs.pstate, spsr); |
| 474 | write_sysreg_el2(*vcpu_pc(vcpu), elr); |
| 475 | } |
| 476 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 477 | #endif /* __ARM64_KVM_EMULATE_H__ */ |