1 // SPDX-License-Identifier: GPL-2.0
3 * Hyp portion of the (not much of an) Emulation layer for 32bit guests.
5 * Copyright (C) 2012,2013 - ARM Ltd
8 * based on arch/arm/kvm/emulate.c
9 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
13 #include <linux/kvm_host.h>
14 #include <asm/kvm_emulate.h>
15 #include <asm/kvm_hyp.h>
18 * stolen from arch/arm/kernel/opcodes.c
20 * condition code lookup table
21 * index into the table is test code: EQ, NE, ... LT, GT, AL, NV
23 * bit position in short is condition code: NZCV
25 static const unsigned short cc_map[16] = {
26 0xF0F0, /* EQ == Z set */
28 0xCCCC, /* CS == C set */
30 0xFF00, /* MI == N set */
32 0xAAAA, /* VS == V set */
34 0x0C0C, /* HI == C set && Z clear */
35 0xF3F3, /* LS == C clear || Z set */
36 0xAA55, /* GE == (N==V) */
37 0x55AA, /* LT == (N!=V) */
38 0x0A05, /* GT == (!Z && (N==V)) */
39 0xF5FA, /* LE == (Z || (N!=V)) */
40 0xFFFF, /* AL always */
45 * Check if a trapped instruction should have been executed or not.
47 bool __hyp_text kvm_condition_valid32(const struct kvm_vcpu *vcpu)
53 /* Top two bits non-zero? Unconditional. */
54 if (kvm_vcpu_get_hsr(vcpu) >> 30)
57 /* Is condition field valid? */
58 cond = kvm_vcpu_get_condition(vcpu);
62 cpsr = *vcpu_cpsr(vcpu);
65 /* This can happen in Thumb mode: examine IT state. */
68 it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
70 /* it == 0 => unconditional. */
74 /* The cond for this insn works out as the top 4 bits. */
78 cpsr_cond = cpsr >> 28;
80 if (!((cc_map[cond] >> cpsr_cond) & 1))
87 * adjust_itstate - adjust ITSTATE when emulating instructions in IT-block
88 * @vcpu: The VCPU pointer
90 * When exceptions occur while instructions are executed in Thumb IF-THEN
91 * blocks, the ITSTATE field of the CPSR is not advanced (updated), so we have
92 * to do this little bit of work manually. The fields map like this:
94 * IT[7:0] -> CPSR[26:25],CPSR[15:10]
96 static void __hyp_text kvm_adjust_itstate(struct kvm_vcpu *vcpu)
98 unsigned long itbits, cond;
99 unsigned long cpsr = *vcpu_cpsr(vcpu);
100 bool is_arm = !(cpsr & PSR_AA32_T_BIT);
102 if (is_arm || !(cpsr & PSR_AA32_IT_MASK))
105 cond = (cpsr & 0xe000) >> 13;
106 itbits = (cpsr & 0x1c00) >> (10 - 2);
107 itbits |= (cpsr & (0x3 << 25)) >> 25;
109 /* Perform ITAdvance (see page A2-52 in ARM DDI 0406C) */
110 if ((itbits & 0x7) == 0)
113 itbits = (itbits << 1) & 0x1f;
115 cpsr &= ~PSR_AA32_IT_MASK;
117 cpsr |= (itbits & 0x1c) << (10 - 2);
118 cpsr |= (itbits & 0x3) << 25;
119 *vcpu_cpsr(vcpu) = cpsr;
123 * kvm_skip_instr - skip a trapped instruction and proceed to the next
124 * @vcpu: The vcpu pointer
126 void __hyp_text kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr)
130 is_thumb = !!(*vcpu_cpsr(vcpu) & PSR_AA32_T_BIT);
131 if (is_thumb && !is_wide_instr)
135 kvm_adjust_itstate(vcpu);