Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 26 | |
| 27 | #include <asm/esr.h> |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 28 | #include <asm/kvm_arm.h> |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 29 | #include <asm/kvm_hyp.h> |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 30 | #include <asm/kvm_mmio.h> |
| 31 | #include <asm/ptrace.h> |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 32 | #include <asm/cputype.h> |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 33 | #include <asm/virt.h> |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 34 | |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 35 | unsigned long *vcpu_reg32(const struct kvm_vcpu *vcpu, u8 reg_num); |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 36 | unsigned long vcpu_read_spsr32(const struct kvm_vcpu *vcpu); |
| 37 | void vcpu_write_spsr32(struct kvm_vcpu *vcpu, unsigned long v); |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 38 | |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 39 | bool kvm_condition_valid32(const struct kvm_vcpu *vcpu); |
| 40 | void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr); |
| 41 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 42 | void kvm_inject_undefined(struct kvm_vcpu *vcpu); |
Marc Zyngier | 10cf339 | 2016-09-06 14:02:01 +0100 | [diff] [blame] | 43 | void kvm_inject_vabt(struct kvm_vcpu *vcpu); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 44 | void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr); |
| 45 | void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr); |
Marc Zyngier | 74a64a9 | 2017-10-29 02:18:09 +0000 | [diff] [blame] | 46 | void kvm_inject_undef32(struct kvm_vcpu *vcpu); |
| 47 | void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr); |
| 48 | void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 49 | |
Christoffer Dall | e72341c | 2017-12-13 22:56:48 +0100 | [diff] [blame] | 50 | static inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) |
| 51 | { |
| 52 | return !(vcpu->arch.hcr_el2 & HCR_RW); |
| 53 | } |
| 54 | |
Christoffer Dall | b856a59 | 2014-10-16 17:21:16 +0200 | [diff] [blame] | 55 | static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu) |
| 56 | { |
| 57 | vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS; |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 58 | if (is_kernel_in_hyp_mode()) |
| 59 | vcpu->arch.hcr_el2 |= HCR_E2H; |
Dongjiu Geng | 558daf6 | 2018-01-15 19:39:06 +0000 | [diff] [blame] | 60 | 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 Zyngier | e48d53a | 2018-04-06 12:27:28 +0100 | [diff] [blame^] | 66 | if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) |
| 67 | vcpu->arch.hcr_el2 |= HCR_FWB; |
Dongjiu Geng | 558daf6 | 2018-01-15 19:39:06 +0000 | [diff] [blame] | 68 | |
Marc Zyngier | 801f677 | 2015-01-11 14:10:11 +0100 | [diff] [blame] | 69 | if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) |
| 70 | vcpu->arch.hcr_el2 &= ~HCR_RW; |
Dave Martin | 005781b | 2017-12-01 15:19:40 +0000 | [diff] [blame] | 71 | |
| 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 Dall | e72341c | 2017-12-13 22:56:48 +0100 | [diff] [blame] | 77 | if (!vcpu_el1_is_32bit(vcpu)) |
Dave Martin | 005781b | 2017-12-01 15:19:40 +0000 | [diff] [blame] | 78 | vcpu->arch.hcr_el2 |= HCR_TID3; |
Christoffer Dall | b856a59 | 2014-10-16 17:21:16 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Christoffer Dall | 3df59d8 | 2017-08-03 12:09:05 +0200 | [diff] [blame] | 81 | static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu) |
Marc Zyngier | 3c1e716 | 2014-12-19 16:05:31 +0000 | [diff] [blame] | 82 | { |
Christoffer Dall | 3df59d8 | 2017-08-03 12:09:05 +0200 | [diff] [blame] | 83 | return (unsigned long *)&vcpu->arch.hcr_el2; |
Marc Zyngier | 3c1e716 | 2014-12-19 16:05:31 +0000 | [diff] [blame] | 84 | } |
| 85 | |
James Morse | 4715c14 | 2018-01-15 19:39:01 +0000 | [diff] [blame] | 86 | static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr) |
| 87 | { |
| 88 | vcpu->arch.vsesr_el2 = vsesr; |
| 89 | } |
| 90 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 91 | static inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu) |
| 92 | { |
| 93 | return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pc; |
| 94 | } |
| 95 | |
Christoffer Dall | 6d4bd90 | 2017-12-27 20:51:04 +0100 | [diff] [blame] | 96 | static inline unsigned long *__vcpu_elr_el1(const struct kvm_vcpu *vcpu) |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 97 | { |
| 98 | return (unsigned long *)&vcpu_gp_regs(vcpu)->elr_el1; |
| 99 | } |
| 100 | |
Christoffer Dall | 6d4bd90 | 2017-12-27 20:51:04 +0100 | [diff] [blame] | 101 | static 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 | |
| 109 | static 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 Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 117 | static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu) |
| 118 | { |
| 119 | return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pstate; |
| 120 | } |
| 121 | |
| 122 | static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu) |
| 123 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 124 | return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu) |
| 128 | { |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 129 | if (vcpu_mode_is_32bit(vcpu)) |
| 130 | return kvm_condition_valid32(vcpu); |
| 131 | |
| 132 | return true; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr) |
| 136 | { |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 137 | if (vcpu_mode_is_32bit(vcpu)) |
| 138 | kvm_skip_instr32(vcpu, is_wide_instr); |
| 139 | else |
| 140 | *vcpu_pc(vcpu) += 4; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu) |
| 144 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 145 | *vcpu_cpsr(vcpu) |= COMPAT_PSR_T_BIT; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Marc Zyngier | c0f0963 | 2015-11-16 10:28:17 +0000 | [diff] [blame] | 148 | /* |
Pavel Fedin | f6be563 | 2015-12-04 15:03:14 +0300 | [diff] [blame] | 149 | * 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 Zyngier | c0f0963 | 2015-11-16 10:28:17 +0000 | [diff] [blame] | 152 | */ |
Pavel Fedin | bc45a51 | 2015-12-04 15:03:11 +0300 | [diff] [blame] | 153 | static 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 | |
| 159 | static 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 Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 166 | static inline unsigned long vcpu_read_spsr(const struct kvm_vcpu *vcpu) |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 167 | { |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 168 | if (vcpu_mode_is_32bit(vcpu)) |
| 169 | return vcpu_read_spsr32(vcpu); |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 170 | |
| 171 | if (vcpu->arch.sysregs_loaded_on_cpu) |
| 172 | return read_sysreg_el1(spsr); |
| 173 | else |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 174 | return vcpu_gp_regs(vcpu)->spsr[KVM_SPSR_EL1]; |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 177 | 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] | 178 | { |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 179 | if (vcpu_mode_is_32bit(vcpu)) { |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 180 | vcpu_write_spsr32(vcpu, v); |
| 181 | return; |
Christoffer Dall | 00536ec | 2017-12-27 20:01:52 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (vcpu->arch.sysregs_loaded_on_cpu) |
| 185 | write_sysreg_el1(v, spsr); |
| 186 | else |
Christoffer Dall | a892819 | 2017-12-27 21:59:09 +0100 | [diff] [blame] | 187 | vcpu_gp_regs(vcpu)->spsr[KVM_SPSR_EL1] = v; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu) |
| 191 | { |
Shannon Zhao | 9586a2e | 2016-01-13 17:16:39 +0800 | [diff] [blame] | 192 | u32 mode; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 193 | |
Shannon Zhao | 9586a2e | 2016-01-13 17:16:39 +0800 | [diff] [blame] | 194 | if (vcpu_mode_is_32bit(vcpu)) { |
| 195 | mode = *vcpu_cpsr(vcpu) & COMPAT_PSR_MODE_MASK; |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 196 | return mode > COMPAT_PSR_MODE_USR; |
Shannon Zhao | 9586a2e | 2016-01-13 17:16:39 +0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK; |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 200 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 201 | return mode != PSR_MODE_EL0t; |
| 202 | } |
| 203 | |
| 204 | static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu) |
| 205 | { |
| 206 | return vcpu->arch.fault.esr_el2; |
| 207 | } |
| 208 | |
Marc Zyngier | 3e51d43 | 2016-09-06 09:28:41 +0100 | [diff] [blame] | 209 | static 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 Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 219 | static inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu) |
| 220 | { |
| 221 | return vcpu->arch.fault.far_el2; |
| 222 | } |
| 223 | |
| 224 | static 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 Morse | 0067df4 | 2018-01-15 19:39:05 +0000 | [diff] [blame] | 229 | static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu) |
| 230 | { |
| 231 | return vcpu->arch.fault.disr_el1; |
| 232 | } |
| 233 | |
Wei Huang | 0d97f884 | 2015-01-12 11:53:36 -0500 | [diff] [blame] | 234 | static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu) |
| 235 | { |
Paolo Bonzini | 1c6007d | 2015-01-23 13:39:51 +0100 | [diff] [blame] | 236 | return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_xVC_IMM_MASK; |
Wei Huang | 0d97f884 | 2015-01-12 11:53:36 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 239 | static inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu) |
| 240 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 241 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_ISV); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 244 | static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu) |
| 245 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 246 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SSE); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu) |
| 250 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 251 | 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] | 252 | } |
| 253 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 254 | static inline bool kvm_vcpu_dabt_iss1tw(const struct kvm_vcpu *vcpu) |
| 255 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 256 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_S1PTW); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Will Deacon | 60e21a0 | 2016-09-29 12:37:01 +0100 | [diff] [blame] | 259 | static 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 Zyngier | 57c841f | 2016-01-29 15:01:28 +0000 | [diff] [blame] | 265 | static 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 Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 270 | static inline int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu) |
| 271 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 272 | 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] | 273 | } |
| 274 | |
| 275 | /* This one is not specific to Data Abort */ |
| 276 | static inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu) |
| 277 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 278 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_IL); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu) |
| 282 | { |
Mark Rutland | 561454e | 2016-05-31 12:33:02 +0100 | [diff] [blame] | 283 | return ESR_ELx_EC(kvm_vcpu_get_hsr(vcpu)); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu) |
| 287 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 288 | return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu) |
| 292 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 293 | return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC; |
Christoffer Dall | 0496daa5 | 2014-09-26 12:29:34 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vcpu) |
| 297 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 298 | return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC_TYPE; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 299 | } |
| 300 | |
James Morse | bb42892 | 2017-07-18 13:37:41 +0100 | [diff] [blame] | 301 | static inline bool kvm_vcpu_dabt_isextabt(const struct kvm_vcpu *vcpu) |
| 302 | { |
Dongjiu Geng | a2b8313 | 2017-10-30 14:05:18 +0800 | [diff] [blame] | 303 | switch (kvm_vcpu_trap_get_fault(vcpu)) { |
James Morse | bb42892 | 2017-07-18 13:37:41 +0100 | [diff] [blame] | 304 | 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 Zyngier | c667186 | 2017-04-27 19:06:48 +0100 | [diff] [blame] | 320 | static 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 Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 326 | 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] | 327 | { |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 328 | return vcpu_read_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK; |
Marc Zyngier | 79c6488 | 2013-10-18 18:19:03 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 331 | static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu) |
| 332 | { |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 333 | if (vcpu_mode_is_32bit(vcpu)) { |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 334 | *vcpu_cpsr(vcpu) |= COMPAT_PSR_E_BIT; |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 335 | } else { |
| 336 | u64 sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1); |
| 337 | sctlr |= (1 << 25); |
James Morse | 1975fa5 | 2018-05-02 12:17:02 +0100 | [diff] [blame] | 338 | vcpu_write_sys_reg(vcpu, sctlr, SCTLR_EL1); |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 339 | } |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 342 | static 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 Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 347 | return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & (1 << 25)); |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static 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 Kamensky | b300708 | 2014-06-12 09:30:08 -0700 | [diff] [blame] | 365 | } 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 Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return data; /* Leave LE untouched */ |
| 379 | } |
| 380 | |
| 381 | static 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 Kamensky | b300708 | 2014-06-12 09:30:08 -0700 | [diff] [blame] | 396 | } 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 Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return data; /* Leave LE untouched */ |
| 410 | } |
| 411 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 412 | #endif /* __ARM64_KVM_EMULATE_H__ */ |