1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
7 #ifndef __ARM_KVM_EMULATE_H__
8 #define __ARM_KVM_EMULATE_H__
10 #include <linux/kvm_host.h>
11 #include <asm/kvm_asm.h>
12 #include <asm/kvm_arm.h>
13 #include <asm/cputype.h>
15 /* arm64 compatibility macros */
16 #define PSR_AA32_MODE_FIQ FIQ_MODE
17 #define PSR_AA32_MODE_SVC SVC_MODE
18 #define PSR_AA32_MODE_ABT ABT_MODE
19 #define PSR_AA32_MODE_UND UND_MODE
20 #define PSR_AA32_T_BIT PSR_T_BIT
21 #define PSR_AA32_F_BIT PSR_F_BIT
22 #define PSR_AA32_I_BIT PSR_I_BIT
23 #define PSR_AA32_A_BIT PSR_A_BIT
24 #define PSR_AA32_E_BIT PSR_E_BIT
25 #define PSR_AA32_IT_MASK PSR_IT_MASK
26 #define PSR_AA32_GE_MASK 0x000f0000
27 #define PSR_AA32_DIT_BIT 0x00200000
28 #define PSR_AA32_PAN_BIT 0x00400000
29 #define PSR_AA32_SSBS_BIT 0x00800000
30 #define PSR_AA32_Q_BIT PSR_Q_BIT
31 #define PSR_AA32_V_BIT PSR_V_BIT
32 #define PSR_AA32_C_BIT PSR_C_BIT
33 #define PSR_AA32_Z_BIT PSR_Z_BIT
34 #define PSR_AA32_N_BIT PSR_N_BIT
36 unsigned long *vcpu_reg(struct kvm_vcpu
*vcpu
, u8 reg_num
);
38 static inline unsigned long *vcpu_reg32(struct kvm_vcpu
*vcpu
, u8 reg_num
)
40 return vcpu_reg(vcpu
, reg_num
);
43 unsigned long *__vcpu_spsr(struct kvm_vcpu
*vcpu
);
45 static inline unsigned long vpcu_read_spsr(struct kvm_vcpu
*vcpu
)
47 return *__vcpu_spsr(vcpu
);
50 static inline void vcpu_write_spsr(struct kvm_vcpu
*vcpu
, unsigned long v
)
52 *__vcpu_spsr(vcpu
) = v
;
55 static inline unsigned long host_spsr_to_spsr32(unsigned long spsr
)
60 static inline unsigned long vcpu_get_reg(struct kvm_vcpu
*vcpu
,
63 return *vcpu_reg(vcpu
, reg_num
);
66 static inline void vcpu_set_reg(struct kvm_vcpu
*vcpu
, u8 reg_num
,
69 *vcpu_reg(vcpu
, reg_num
) = val
;
72 bool kvm_condition_valid32(const struct kvm_vcpu
*vcpu
);
73 void kvm_skip_instr32(struct kvm_vcpu
*vcpu
, bool is_wide_instr
);
74 void kvm_inject_undef32(struct kvm_vcpu
*vcpu
);
75 void kvm_inject_dabt32(struct kvm_vcpu
*vcpu
, unsigned long addr
);
76 void kvm_inject_pabt32(struct kvm_vcpu
*vcpu
, unsigned long addr
);
77 void kvm_inject_vabt(struct kvm_vcpu
*vcpu
);
79 static inline void kvm_inject_undefined(struct kvm_vcpu
*vcpu
)
81 kvm_inject_undef32(vcpu
);
84 static inline void kvm_inject_dabt(struct kvm_vcpu
*vcpu
, unsigned long addr
)
86 kvm_inject_dabt32(vcpu
, addr
);
89 static inline void kvm_inject_pabt(struct kvm_vcpu
*vcpu
, unsigned long addr
)
91 kvm_inject_pabt32(vcpu
, addr
);
94 static inline bool kvm_condition_valid(const struct kvm_vcpu
*vcpu
)
96 return kvm_condition_valid32(vcpu
);
99 static inline void kvm_skip_instr(struct kvm_vcpu
*vcpu
, bool is_wide_instr
)
101 kvm_skip_instr32(vcpu
, is_wide_instr
);
104 static inline void vcpu_reset_hcr(struct kvm_vcpu
*vcpu
)
106 vcpu
->arch
.hcr
= HCR_GUEST_MASK
;
109 static inline unsigned long *vcpu_hcr(const struct kvm_vcpu
*vcpu
)
111 return (unsigned long *)&vcpu
->arch
.hcr
;
114 static inline void vcpu_clear_wfx_traps(struct kvm_vcpu
*vcpu
)
116 vcpu
->arch
.hcr
&= ~HCR_TWE
;
119 static inline void vcpu_set_wfx_traps(struct kvm_vcpu
*vcpu
)
121 vcpu
->arch
.hcr
|= HCR_TWE
;
124 static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu
*vcpu
)
129 static inline unsigned long *vcpu_pc(struct kvm_vcpu
*vcpu
)
131 return &vcpu
->arch
.ctxt
.gp_regs
.usr_regs
.ARM_pc
;
134 static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu
*vcpu
)
136 return (unsigned long *)&vcpu
->arch
.ctxt
.gp_regs
.usr_regs
.ARM_cpsr
;
139 static inline void vcpu_set_thumb(struct kvm_vcpu
*vcpu
)
141 *vcpu_cpsr(vcpu
) |= PSR_T_BIT
;
144 static inline bool mode_has_spsr(struct kvm_vcpu
*vcpu
)
146 unsigned long cpsr_mode
= vcpu
->arch
.ctxt
.gp_regs
.usr_regs
.ARM_cpsr
& MODE_MASK
;
147 return (cpsr_mode
> USR_MODE
&& cpsr_mode
< SYSTEM_MODE
);
150 static inline bool vcpu_mode_priv(struct kvm_vcpu
*vcpu
)
152 unsigned long cpsr_mode
= vcpu
->arch
.ctxt
.gp_regs
.usr_regs
.ARM_cpsr
& MODE_MASK
;
153 return cpsr_mode
> USR_MODE
;
156 static inline u32
kvm_vcpu_get_hsr(const struct kvm_vcpu
*vcpu
)
158 return vcpu
->arch
.fault
.hsr
;
161 static inline int kvm_vcpu_get_condition(const struct kvm_vcpu
*vcpu
)
163 u32 hsr
= kvm_vcpu_get_hsr(vcpu
);
166 return (hsr
& HSR_COND
) >> HSR_COND_SHIFT
;
171 static inline unsigned long kvm_vcpu_get_hfar(struct kvm_vcpu
*vcpu
)
173 return vcpu
->arch
.fault
.hxfar
;
176 static inline phys_addr_t
kvm_vcpu_get_fault_ipa(struct kvm_vcpu
*vcpu
)
178 return ((phys_addr_t
)vcpu
->arch
.fault
.hpfar
& HPFAR_MASK
) << 8;
181 static inline bool kvm_vcpu_dabt_isvalid(struct kvm_vcpu
*vcpu
)
183 return kvm_vcpu_get_hsr(vcpu
) & HSR_ISV
;
186 static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized(const struct kvm_vcpu
*vcpu
)
188 return kvm_vcpu_get_hsr(vcpu
) & (HSR_CM
| HSR_WNR
| HSR_FSC
);
191 static inline bool kvm_vcpu_dabt_iswrite(struct kvm_vcpu
*vcpu
)
193 return kvm_vcpu_get_hsr(vcpu
) & HSR_WNR
;
196 static inline bool kvm_vcpu_dabt_issext(struct kvm_vcpu
*vcpu
)
198 return kvm_vcpu_get_hsr(vcpu
) & HSR_SSE
;
201 static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu
*vcpu
)
206 static inline int kvm_vcpu_dabt_get_rd(struct kvm_vcpu
*vcpu
)
208 return (kvm_vcpu_get_hsr(vcpu
) & HSR_SRT_MASK
) >> HSR_SRT_SHIFT
;
211 static inline bool kvm_vcpu_dabt_iss1tw(struct kvm_vcpu
*vcpu
)
213 return kvm_vcpu_get_hsr(vcpu
) & HSR_DABT_S1PTW
;
216 static inline bool kvm_vcpu_dabt_is_cm(struct kvm_vcpu
*vcpu
)
218 return !!(kvm_vcpu_get_hsr(vcpu
) & HSR_DABT_CM
);
221 /* Get Access Size from a data abort */
222 static inline unsigned int kvm_vcpu_dabt_get_as(struct kvm_vcpu
*vcpu
)
224 switch ((kvm_vcpu_get_hsr(vcpu
) >> 22) & 0x3) {
232 kvm_err("Hardware is weird: SAS 0b11 is reserved\n");
237 /* This one is not specific to Data Abort */
238 static inline bool kvm_vcpu_trap_il_is32bit(struct kvm_vcpu
*vcpu
)
240 return kvm_vcpu_get_hsr(vcpu
) & HSR_IL
;
243 static inline u8
kvm_vcpu_trap_get_class(struct kvm_vcpu
*vcpu
)
245 return kvm_vcpu_get_hsr(vcpu
) >> HSR_EC_SHIFT
;
248 static inline bool kvm_vcpu_trap_is_iabt(struct kvm_vcpu
*vcpu
)
250 return kvm_vcpu_trap_get_class(vcpu
) == HSR_EC_IABT
;
253 static inline u8
kvm_vcpu_trap_get_fault(struct kvm_vcpu
*vcpu
)
255 return kvm_vcpu_get_hsr(vcpu
) & HSR_FSC
;
258 static inline u8
kvm_vcpu_trap_get_fault_type(struct kvm_vcpu
*vcpu
)
260 return kvm_vcpu_get_hsr(vcpu
) & HSR_FSC_TYPE
;
263 static inline bool kvm_vcpu_dabt_isextabt(struct kvm_vcpu
*vcpu
)
265 switch (kvm_vcpu_trap_get_fault(vcpu
)) {
282 static inline bool kvm_is_write_fault(struct kvm_vcpu
*vcpu
)
284 if (kvm_vcpu_trap_is_iabt(vcpu
))
287 return kvm_vcpu_dabt_iswrite(vcpu
);
290 static inline u32
kvm_vcpu_hvc_get_imm(struct kvm_vcpu
*vcpu
)
292 return kvm_vcpu_get_hsr(vcpu
) & HSR_HVC_IMM_MASK
;
295 static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu
*vcpu
)
297 return vcpu_cp15(vcpu
, c0_MPIDR
) & MPIDR_HWID_BITMASK
;
300 static inline bool kvm_arm_get_vcpu_workaround_2_flag(struct kvm_vcpu
*vcpu
)
305 static inline void kvm_arm_set_vcpu_workaround_2_flag(struct kvm_vcpu
*vcpu
,
310 static inline void kvm_vcpu_set_be(struct kvm_vcpu
*vcpu
)
312 *vcpu_cpsr(vcpu
) |= PSR_E_BIT
;
315 static inline bool kvm_vcpu_is_be(struct kvm_vcpu
*vcpu
)
317 return !!(*vcpu_cpsr(vcpu
) & PSR_E_BIT
);
320 static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu
*vcpu
,
324 if (kvm_vcpu_is_be(vcpu
)) {
329 return be16_to_cpu(data
& 0xffff);
331 return be32_to_cpu(data
);
338 return le16_to_cpu(data
& 0xffff);
340 return le32_to_cpu(data
);
345 static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu
*vcpu
,
349 if (kvm_vcpu_is_be(vcpu
)) {
354 return cpu_to_be16(data
& 0xffff);
356 return cpu_to_be32(data
);
363 return cpu_to_le16(data
& 0xffff);
365 return cpu_to_le32(data
);
370 static inline void vcpu_ptrauth_setup_lazy(struct kvm_vcpu
*vcpu
) {}
372 #endif /* __ARM_KVM_EMULATE_H__ */