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> |
| 26 | #include <asm/kvm_asm.h> |
| 27 | #include <asm/kvm_arm.h> |
| 28 | #include <asm/kvm_mmio.h> |
| 29 | #include <asm/ptrace.h> |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame^] | 30 | #include <asm/cputype.h> |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 31 | |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 32 | unsigned long *vcpu_reg32(const struct kvm_vcpu *vcpu, u8 reg_num); |
| 33 | unsigned long *vcpu_spsr32(const struct kvm_vcpu *vcpu); |
| 34 | |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 35 | bool kvm_condition_valid32(const struct kvm_vcpu *vcpu); |
| 36 | void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr); |
| 37 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 38 | void kvm_inject_undefined(struct kvm_vcpu *vcpu); |
| 39 | void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr); |
| 40 | void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr); |
| 41 | |
Christoffer Dall | b856a59 | 2014-10-16 17:21:16 +0200 | [diff] [blame] | 42 | static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu) |
| 43 | { |
| 44 | vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS; |
| 45 | } |
| 46 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 47 | static inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu) |
| 48 | { |
| 49 | return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pc; |
| 50 | } |
| 51 | |
| 52 | static inline unsigned long *vcpu_elr_el1(const struct kvm_vcpu *vcpu) |
| 53 | { |
| 54 | return (unsigned long *)&vcpu_gp_regs(vcpu)->elr_el1; |
| 55 | } |
| 56 | |
| 57 | static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu) |
| 58 | { |
| 59 | return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pstate; |
| 60 | } |
| 61 | |
| 62 | static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu) |
| 63 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 64 | return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu) |
| 68 | { |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 69 | if (vcpu_mode_is_32bit(vcpu)) |
| 70 | return kvm_condition_valid32(vcpu); |
| 71 | |
| 72 | return true; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr) |
| 76 | { |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 77 | if (vcpu_mode_is_32bit(vcpu)) |
| 78 | kvm_skip_instr32(vcpu, is_wide_instr); |
| 79 | else |
| 80 | *vcpu_pc(vcpu) += 4; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu) |
| 84 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 85 | *vcpu_cpsr(vcpu) |= COMPAT_PSR_T_BIT; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static inline unsigned long *vcpu_reg(const struct kvm_vcpu *vcpu, u8 reg_num) |
| 89 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 90 | if (vcpu_mode_is_32bit(vcpu)) |
| 91 | return vcpu_reg32(vcpu, reg_num); |
| 92 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 93 | return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.regs[reg_num]; |
| 94 | } |
| 95 | |
| 96 | /* Get vcpu SPSR for current mode */ |
| 97 | static inline unsigned long *vcpu_spsr(const struct kvm_vcpu *vcpu) |
| 98 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 99 | if (vcpu_mode_is_32bit(vcpu)) |
| 100 | return vcpu_spsr32(vcpu); |
| 101 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 102 | return (unsigned long *)&vcpu_gp_regs(vcpu)->spsr[KVM_SPSR_EL1]; |
| 103 | } |
| 104 | |
| 105 | static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu) |
| 106 | { |
| 107 | u32 mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK; |
| 108 | |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 109 | if (vcpu_mode_is_32bit(vcpu)) |
| 110 | return mode > COMPAT_PSR_MODE_USR; |
| 111 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 112 | return mode != PSR_MODE_EL0t; |
| 113 | } |
| 114 | |
| 115 | static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu) |
| 116 | { |
| 117 | return vcpu->arch.fault.esr_el2; |
| 118 | } |
| 119 | |
| 120 | static inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu) |
| 121 | { |
| 122 | return vcpu->arch.fault.far_el2; |
| 123 | } |
| 124 | |
| 125 | static inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu) |
| 126 | { |
| 127 | return ((phys_addr_t)vcpu->arch.fault.hpfar_el2 & HPFAR_MASK) << 8; |
| 128 | } |
| 129 | |
Wei Huang | 0d97f884 | 2015-01-12 11:53:36 -0500 | [diff] [blame] | 130 | static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu) |
| 131 | { |
| 132 | return kvm_vcpu_get_hsr(vcpu) & ESR_EL2_HVC_IMM_MASK; |
| 133 | } |
| 134 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 135 | static inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu) |
| 136 | { |
| 137 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_EL2_ISV); |
| 138 | } |
| 139 | |
| 140 | static inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu) |
| 141 | { |
| 142 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_EL2_WNR); |
| 143 | } |
| 144 | |
| 145 | static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu) |
| 146 | { |
| 147 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_EL2_SSE); |
| 148 | } |
| 149 | |
| 150 | static inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu) |
| 151 | { |
| 152 | return (kvm_vcpu_get_hsr(vcpu) & ESR_EL2_SRT_MASK) >> ESR_EL2_SRT_SHIFT; |
| 153 | } |
| 154 | |
| 155 | static inline bool kvm_vcpu_dabt_isextabt(const struct kvm_vcpu *vcpu) |
| 156 | { |
| 157 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_EL2_EA); |
| 158 | } |
| 159 | |
| 160 | static inline bool kvm_vcpu_dabt_iss1tw(const struct kvm_vcpu *vcpu) |
| 161 | { |
| 162 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_EL2_S1PTW); |
| 163 | } |
| 164 | |
| 165 | static inline int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu) |
| 166 | { |
| 167 | return 1 << ((kvm_vcpu_get_hsr(vcpu) & ESR_EL2_SAS) >> ESR_EL2_SAS_SHIFT); |
| 168 | } |
| 169 | |
| 170 | /* This one is not specific to Data Abort */ |
| 171 | static inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu) |
| 172 | { |
| 173 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_EL2_IL); |
| 174 | } |
| 175 | |
| 176 | static inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu) |
| 177 | { |
| 178 | return kvm_vcpu_get_hsr(vcpu) >> ESR_EL2_EC_SHIFT; |
| 179 | } |
| 180 | |
| 181 | static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu) |
| 182 | { |
| 183 | return kvm_vcpu_trap_get_class(vcpu) == ESR_EL2_EC_IABT; |
| 184 | } |
| 185 | |
| 186 | static inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu) |
| 187 | { |
Christoffer Dall | 0496daa5 | 2014-09-26 12:29:34 +0200 | [diff] [blame] | 188 | return kvm_vcpu_get_hsr(vcpu) & ESR_EL2_FSC; |
| 189 | } |
| 190 | |
| 191 | static inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vcpu) |
| 192 | { |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 193 | return kvm_vcpu_get_hsr(vcpu) & ESR_EL2_FSC_TYPE; |
| 194 | } |
| 195 | |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame^] | 196 | 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] | 197 | { |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame^] | 198 | return vcpu_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK; |
Marc Zyngier | 79c6488 | 2013-10-18 18:19:03 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 201 | static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu) |
| 202 | { |
| 203 | if (vcpu_mode_is_32bit(vcpu)) |
| 204 | *vcpu_cpsr(vcpu) |= COMPAT_PSR_E_BIT; |
| 205 | else |
| 206 | vcpu_sys_reg(vcpu, SCTLR_EL1) |= (1 << 25); |
| 207 | } |
| 208 | |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 209 | static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu) |
| 210 | { |
| 211 | if (vcpu_mode_is_32bit(vcpu)) |
| 212 | return !!(*vcpu_cpsr(vcpu) & COMPAT_PSR_E_BIT); |
| 213 | |
| 214 | return !!(vcpu_sys_reg(vcpu, SCTLR_EL1) & (1 << 25)); |
| 215 | } |
| 216 | |
| 217 | static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu, |
| 218 | unsigned long data, |
| 219 | unsigned int len) |
| 220 | { |
| 221 | if (kvm_vcpu_is_be(vcpu)) { |
| 222 | switch (len) { |
| 223 | case 1: |
| 224 | return data & 0xff; |
| 225 | case 2: |
| 226 | return be16_to_cpu(data & 0xffff); |
| 227 | case 4: |
| 228 | return be32_to_cpu(data & 0xffffffff); |
| 229 | default: |
| 230 | return be64_to_cpu(data); |
| 231 | } |
Victor Kamensky | b300708 | 2014-06-12 09:30:08 -0700 | [diff] [blame] | 232 | } else { |
| 233 | switch (len) { |
| 234 | case 1: |
| 235 | return data & 0xff; |
| 236 | case 2: |
| 237 | return le16_to_cpu(data & 0xffff); |
| 238 | case 4: |
| 239 | return le32_to_cpu(data & 0xffffffff); |
| 240 | default: |
| 241 | return le64_to_cpu(data); |
| 242 | } |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return data; /* Leave LE untouched */ |
| 246 | } |
| 247 | |
| 248 | static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu, |
| 249 | unsigned long data, |
| 250 | unsigned int len) |
| 251 | { |
| 252 | if (kvm_vcpu_is_be(vcpu)) { |
| 253 | switch (len) { |
| 254 | case 1: |
| 255 | return data & 0xff; |
| 256 | case 2: |
| 257 | return cpu_to_be16(data & 0xffff); |
| 258 | case 4: |
| 259 | return cpu_to_be32(data & 0xffffffff); |
| 260 | default: |
| 261 | return cpu_to_be64(data); |
| 262 | } |
Victor Kamensky | b300708 | 2014-06-12 09:30:08 -0700 | [diff] [blame] | 263 | } else { |
| 264 | switch (len) { |
| 265 | case 1: |
| 266 | return data & 0xff; |
| 267 | case 2: |
| 268 | return cpu_to_le16(data & 0xffff); |
| 269 | case 4: |
| 270 | return cpu_to_le32(data & 0xffffffff); |
| 271 | default: |
| 272 | return cpu_to_le64(data); |
| 273 | } |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return data; /* Leave LE untouched */ |
| 277 | } |
| 278 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 279 | #endif /* __ARM64_KVM_EMULATE_H__ */ |