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> |
| 29 | #include <asm/kvm_mmio.h> |
| 30 | #include <asm/ptrace.h> |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 31 | #include <asm/cputype.h> |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 32 | #include <asm/virt.h> |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 33 | |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 34 | unsigned long *vcpu_reg32(const struct kvm_vcpu *vcpu, u8 reg_num); |
| 35 | unsigned long *vcpu_spsr32(const struct kvm_vcpu *vcpu); |
| 36 | |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 37 | bool kvm_condition_valid32(const struct kvm_vcpu *vcpu); |
| 38 | void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr); |
| 39 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 40 | void kvm_inject_undefined(struct kvm_vcpu *vcpu); |
Marc Zyngier | 10cf339 | 2016-09-06 14:02:01 +0100 | [diff] [blame] | 41 | void kvm_inject_vabt(struct kvm_vcpu *vcpu); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 42 | void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr); |
| 43 | void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr); |
Marc Zyngier | 74a64a9 | 2017-10-29 02:18:09 +0000 | [diff] [blame] | 44 | void kvm_inject_undef32(struct kvm_vcpu *vcpu); |
| 45 | void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr); |
| 46 | void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 47 | |
Christoffer Dall | b856a59 | 2014-10-16 17:21:16 +0200 | [diff] [blame] | 48 | static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu) |
| 49 | { |
| 50 | vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS; |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 51 | if (is_kernel_in_hyp_mode()) |
| 52 | vcpu->arch.hcr_el2 |= HCR_E2H; |
Marc Zyngier | 801f677 | 2015-01-11 14:10:11 +0100 | [diff] [blame] | 53 | if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) |
| 54 | vcpu->arch.hcr_el2 &= ~HCR_RW; |
Christoffer Dall | b856a59 | 2014-10-16 17:21:16 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Marc Zyngier | 3c1e716 | 2014-12-19 16:05:31 +0000 | [diff] [blame] | 57 | static inline unsigned long vcpu_get_hcr(struct kvm_vcpu *vcpu) |
| 58 | { |
| 59 | return vcpu->arch.hcr_el2; |
| 60 | } |
| 61 | |
| 62 | static inline void vcpu_set_hcr(struct kvm_vcpu *vcpu, unsigned long hcr) |
| 63 | { |
| 64 | vcpu->arch.hcr_el2 = hcr; |
| 65 | } |
| 66 | |
James Morse | 4715c14 | 2018-01-15 19:39:01 +0000 | [diff] [blame] | 67 | static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr) |
| 68 | { |
| 69 | vcpu->arch.vsesr_el2 = vsesr; |
| 70 | } |
| 71 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 72 | static inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu) |
| 73 | { |
| 74 | return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pc; |
| 75 | } |
| 76 | |
| 77 | static inline unsigned long *vcpu_elr_el1(const struct kvm_vcpu *vcpu) |
| 78 | { |
| 79 | return (unsigned long *)&vcpu_gp_regs(vcpu)->elr_el1; |
| 80 | } |
| 81 | |
| 82 | static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu) |
| 83 | { |
| 84 | return (unsigned long *)&vcpu_gp_regs(vcpu)->regs.pstate; |
| 85 | } |
| 86 | |
| 87 | static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu) |
| 88 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 89 | return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu) |
| 93 | { |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 94 | if (vcpu_mode_is_32bit(vcpu)) |
| 95 | return kvm_condition_valid32(vcpu); |
| 96 | |
| 97 | return true; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr) |
| 101 | { |
Marc Zyngier | 27b190b | 2013-02-06 19:54:04 +0000 | [diff] [blame] | 102 | if (vcpu_mode_is_32bit(vcpu)) |
| 103 | kvm_skip_instr32(vcpu, is_wide_instr); |
| 104 | else |
| 105 | *vcpu_pc(vcpu) += 4; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu) |
| 109 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 110 | *vcpu_cpsr(vcpu) |= COMPAT_PSR_T_BIT; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Marc Zyngier | c0f0963 | 2015-11-16 10:28:17 +0000 | [diff] [blame] | 113 | /* |
Pavel Fedin | f6be563 | 2015-12-04 15:03:14 +0300 | [diff] [blame] | 114 | * vcpu_get_reg and vcpu_set_reg should always be passed a register number |
| 115 | * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on |
| 116 | * AArch32 with banked registers. |
Marc Zyngier | c0f0963 | 2015-11-16 10:28:17 +0000 | [diff] [blame] | 117 | */ |
Pavel Fedin | bc45a51 | 2015-12-04 15:03:11 +0300 | [diff] [blame] | 118 | static inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu, |
| 119 | u8 reg_num) |
| 120 | { |
| 121 | return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs.regs[reg_num]; |
| 122 | } |
| 123 | |
| 124 | static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num, |
| 125 | unsigned long val) |
| 126 | { |
| 127 | if (reg_num != 31) |
| 128 | vcpu_gp_regs(vcpu)->regs.regs[reg_num] = val; |
| 129 | } |
| 130 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 131 | /* Get vcpu SPSR for current mode */ |
| 132 | static inline unsigned long *vcpu_spsr(const struct kvm_vcpu *vcpu) |
| 133 | { |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 134 | if (vcpu_mode_is_32bit(vcpu)) |
| 135 | return vcpu_spsr32(vcpu); |
| 136 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 137 | return (unsigned long *)&vcpu_gp_regs(vcpu)->spsr[KVM_SPSR_EL1]; |
| 138 | } |
| 139 | |
| 140 | static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu) |
| 141 | { |
Shannon Zhao | 9586a2e | 2016-01-13 17:16:39 +0800 | [diff] [blame] | 142 | u32 mode; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 143 | |
Shannon Zhao | 9586a2e | 2016-01-13 17:16:39 +0800 | [diff] [blame] | 144 | if (vcpu_mode_is_32bit(vcpu)) { |
| 145 | mode = *vcpu_cpsr(vcpu) & COMPAT_PSR_MODE_MASK; |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 146 | return mode > COMPAT_PSR_MODE_USR; |
Shannon Zhao | 9586a2e | 2016-01-13 17:16:39 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK; |
Marc Zyngier | b547631 | 2013-02-06 19:40:29 +0000 | [diff] [blame] | 150 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 151 | return mode != PSR_MODE_EL0t; |
| 152 | } |
| 153 | |
| 154 | static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu) |
| 155 | { |
| 156 | return vcpu->arch.fault.esr_el2; |
| 157 | } |
| 158 | |
Marc Zyngier | 3e51d43 | 2016-09-06 09:28:41 +0100 | [diff] [blame] | 159 | static inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu) |
| 160 | { |
| 161 | u32 esr = kvm_vcpu_get_hsr(vcpu); |
| 162 | |
| 163 | if (esr & ESR_ELx_CV) |
| 164 | return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT; |
| 165 | |
| 166 | return -1; |
| 167 | } |
| 168 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 169 | static inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu) |
| 170 | { |
| 171 | return vcpu->arch.fault.far_el2; |
| 172 | } |
| 173 | |
| 174 | static inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu) |
| 175 | { |
| 176 | return ((phys_addr_t)vcpu->arch.fault.hpfar_el2 & HPFAR_MASK) << 8; |
| 177 | } |
| 178 | |
James Morse | 0067df4 | 2018-01-15 19:39:05 +0000 | [diff] [blame^] | 179 | static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu) |
| 180 | { |
| 181 | return vcpu->arch.fault.disr_el1; |
| 182 | } |
| 183 | |
Wei Huang | 0d97f884 | 2015-01-12 11:53:36 -0500 | [diff] [blame] | 184 | static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu) |
| 185 | { |
Paolo Bonzini | 1c6007d | 2015-01-23 13:39:51 +0100 | [diff] [blame] | 186 | return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_xVC_IMM_MASK; |
Wei Huang | 0d97f884 | 2015-01-12 11:53:36 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 189 | static inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu) |
| 190 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 191 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_ISV); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 194 | static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu) |
| 195 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 196 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SSE); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu) |
| 200 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 201 | 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] | 202 | } |
| 203 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 204 | static inline bool kvm_vcpu_dabt_iss1tw(const struct kvm_vcpu *vcpu) |
| 205 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 206 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_S1PTW); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Will Deacon | 60e21a0 | 2016-09-29 12:37:01 +0100 | [diff] [blame] | 209 | static inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu) |
| 210 | { |
| 211 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_WNR) || |
| 212 | kvm_vcpu_dabt_iss1tw(vcpu); /* AF/DBM update */ |
| 213 | } |
| 214 | |
Marc Zyngier | 57c841f | 2016-01-29 15:01:28 +0000 | [diff] [blame] | 215 | static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu) |
| 216 | { |
| 217 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_CM); |
| 218 | } |
| 219 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 220 | static inline int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu) |
| 221 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 222 | 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] | 223 | } |
| 224 | |
| 225 | /* This one is not specific to Data Abort */ |
| 226 | static inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu) |
| 227 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 228 | return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_IL); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu) |
| 232 | { |
Mark Rutland | 561454e | 2016-05-31 12:33:02 +0100 | [diff] [blame] | 233 | return ESR_ELx_EC(kvm_vcpu_get_hsr(vcpu)); |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu) |
| 237 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 238 | return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu) |
| 242 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 243 | return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC; |
Christoffer Dall | 0496daa5 | 2014-09-26 12:29:34 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vcpu) |
| 247 | { |
Mark Rutland | c6d01a9 | 2014-11-24 13:59:30 +0000 | [diff] [blame] | 248 | return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC_TYPE; |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 249 | } |
| 250 | |
James Morse | bb42892 | 2017-07-18 13:37:41 +0100 | [diff] [blame] | 251 | static inline bool kvm_vcpu_dabt_isextabt(const struct kvm_vcpu *vcpu) |
| 252 | { |
Dongjiu Geng | a2b8313 | 2017-10-30 14:05:18 +0800 | [diff] [blame] | 253 | switch (kvm_vcpu_trap_get_fault(vcpu)) { |
James Morse | bb42892 | 2017-07-18 13:37:41 +0100 | [diff] [blame] | 254 | case FSC_SEA: |
| 255 | case FSC_SEA_TTW0: |
| 256 | case FSC_SEA_TTW1: |
| 257 | case FSC_SEA_TTW2: |
| 258 | case FSC_SEA_TTW3: |
| 259 | case FSC_SECC: |
| 260 | case FSC_SECC_TTW0: |
| 261 | case FSC_SECC_TTW1: |
| 262 | case FSC_SECC_TTW2: |
| 263 | case FSC_SECC_TTW3: |
| 264 | return true; |
| 265 | default: |
| 266 | return false; |
| 267 | } |
| 268 | } |
| 269 | |
Marc Zyngier | c667186 | 2017-04-27 19:06:48 +0100 | [diff] [blame] | 270 | static inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu) |
| 271 | { |
| 272 | u32 esr = kvm_vcpu_get_hsr(vcpu); |
| 273 | return (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT; |
| 274 | } |
| 275 | |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 276 | 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] | 277 | { |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 278 | return vcpu_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK; |
Marc Zyngier | 79c6488 | 2013-10-18 18:19:03 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 281 | static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu) |
| 282 | { |
| 283 | if (vcpu_mode_is_32bit(vcpu)) |
| 284 | *vcpu_cpsr(vcpu) |= COMPAT_PSR_E_BIT; |
| 285 | else |
| 286 | vcpu_sys_reg(vcpu, SCTLR_EL1) |= (1 << 25); |
| 287 | } |
| 288 | |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 289 | static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu) |
| 290 | { |
| 291 | if (vcpu_mode_is_32bit(vcpu)) |
| 292 | return !!(*vcpu_cpsr(vcpu) & COMPAT_PSR_E_BIT); |
| 293 | |
| 294 | return !!(vcpu_sys_reg(vcpu, SCTLR_EL1) & (1 << 25)); |
| 295 | } |
| 296 | |
| 297 | static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu, |
| 298 | unsigned long data, |
| 299 | unsigned int len) |
| 300 | { |
| 301 | if (kvm_vcpu_is_be(vcpu)) { |
| 302 | switch (len) { |
| 303 | case 1: |
| 304 | return data & 0xff; |
| 305 | case 2: |
| 306 | return be16_to_cpu(data & 0xffff); |
| 307 | case 4: |
| 308 | return be32_to_cpu(data & 0xffffffff); |
| 309 | default: |
| 310 | return be64_to_cpu(data); |
| 311 | } |
Victor Kamensky | b300708 | 2014-06-12 09:30:08 -0700 | [diff] [blame] | 312 | } else { |
| 313 | switch (len) { |
| 314 | case 1: |
| 315 | return data & 0xff; |
| 316 | case 2: |
| 317 | return le16_to_cpu(data & 0xffff); |
| 318 | case 4: |
| 319 | return le32_to_cpu(data & 0xffffffff); |
| 320 | default: |
| 321 | return le64_to_cpu(data); |
| 322 | } |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return data; /* Leave LE untouched */ |
| 326 | } |
| 327 | |
| 328 | static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu, |
| 329 | unsigned long data, |
| 330 | unsigned int len) |
| 331 | { |
| 332 | if (kvm_vcpu_is_be(vcpu)) { |
| 333 | switch (len) { |
| 334 | case 1: |
| 335 | return data & 0xff; |
| 336 | case 2: |
| 337 | return cpu_to_be16(data & 0xffff); |
| 338 | case 4: |
| 339 | return cpu_to_be32(data & 0xffffffff); |
| 340 | default: |
| 341 | return cpu_to_be64(data); |
| 342 | } |
Victor Kamensky | b300708 | 2014-06-12 09:30:08 -0700 | [diff] [blame] | 343 | } else { |
| 344 | switch (len) { |
| 345 | case 1: |
| 346 | return data & 0xff; |
| 347 | case 2: |
| 348 | return cpu_to_le16(data & 0xffff); |
| 349 | case 4: |
| 350 | return cpu_to_le32(data & 0xffffffff); |
| 351 | default: |
| 352 | return cpu_to_le64(data); |
| 353 | } |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | return data; /* Leave LE untouched */ |
| 357 | } |
| 358 | |
Marc Zyngier | 83a4979 | 2012-12-10 13:27:52 +0000 | [diff] [blame] | 359 | #endif /* __ARM64_KVM_EMULATE_H__ */ |