1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/kvm_host.h>
25#include <asm/esr.h>
26#include <asm/kvm_emulate.h>
27
28
29
30
31
32
33
34
35
36static const unsigned short cc_map[16] = {
37 0xF0F0,
38 0x0F0F,
39 0xCCCC,
40 0x3333,
41 0xFF00,
42 0x00FF,
43 0xAAAA,
44 0x5555,
45 0x0C0C,
46 0xF3F3,
47 0xAA55,
48 0x55AA,
49 0x0A05,
50 0xF5FA,
51 0xFFFF,
52 0
53};
54
55static int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
56{
57 u32 esr = kvm_vcpu_get_hsr(vcpu);
58
59 if (esr & ESR_ELx_CV)
60 return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
61
62 return -1;
63}
64
65
66
67
68bool kvm_condition_valid32(const struct kvm_vcpu *vcpu)
69{
70 unsigned long cpsr;
71 u32 cpsr_cond;
72 int cond;
73
74
75 if (kvm_vcpu_get_hsr(vcpu) >> 30)
76 return true;
77
78
79 cond = kvm_vcpu_get_condition(vcpu);
80 if (cond == 0xE)
81 return true;
82
83 cpsr = *vcpu_cpsr(vcpu);
84
85 if (cond < 0) {
86
87 unsigned long it;
88
89 it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
90
91
92 if (it == 0)
93 return true;
94
95
96 cond = (it >> 4);
97 }
98
99 cpsr_cond = cpsr >> 28;
100
101 if (!((cc_map[cond] >> cpsr_cond) & 1))
102 return false;
103
104 return true;
105}
106
107
108
109
110
111
112
113
114
115
116
117static void kvm_adjust_itstate(struct kvm_vcpu *vcpu)
118{
119 unsigned long itbits, cond;
120 unsigned long cpsr = *vcpu_cpsr(vcpu);
121 bool is_arm = !(cpsr & COMPAT_PSR_T_BIT);
122
123 BUG_ON(is_arm && (cpsr & COMPAT_PSR_IT_MASK));
124
125 if (!(cpsr & COMPAT_PSR_IT_MASK))
126 return;
127
128 cond = (cpsr & 0xe000) >> 13;
129 itbits = (cpsr & 0x1c00) >> (10 - 2);
130 itbits |= (cpsr & (0x3 << 25)) >> 25;
131
132
133 if ((itbits & 0x7) == 0)
134 itbits = cond = 0;
135 else
136 itbits = (itbits << 1) & 0x1f;
137
138 cpsr &= ~COMPAT_PSR_IT_MASK;
139 cpsr |= cond << 13;
140 cpsr |= (itbits & 0x1c) << (10 - 2);
141 cpsr |= (itbits & 0x3) << 25;
142 *vcpu_cpsr(vcpu) = cpsr;
143}
144
145
146
147
148
149void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr)
150{
151 bool is_thumb;
152
153 is_thumb = !!(*vcpu_cpsr(vcpu) & COMPAT_PSR_T_BIT);
154 if (is_thumb && !is_wide_instr)
155 *vcpu_pc(vcpu) += 2;
156 else
157 *vcpu_pc(vcpu) += 4;
158 kvm_adjust_itstate(vcpu);
159}
160