1
2#ifndef ARCH_X86_KVM_CPUID_H
3#define ARCH_X86_KVM_CPUID_H
4
5#include "x86.h"
6#include <asm/cpu.h>
7#include <asm/processor.h>
8#include <uapi/asm/kvm_para.h>
9
10extern u32 kvm_cpu_caps[NCAPINTS] __read_mostly;
11void kvm_set_cpu_caps(void);
12
13void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu);
14void kvm_update_pv_runtime(struct kvm_vcpu *vcpu);
15struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
16 u32 function, u32 index);
17int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
18 struct kvm_cpuid_entry2 __user *entries,
19 unsigned int type);
20int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
21 struct kvm_cpuid *cpuid,
22 struct kvm_cpuid_entry __user *entries);
23int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
24 struct kvm_cpuid2 *cpuid,
25 struct kvm_cpuid_entry2 __user *entries);
26int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
27 struct kvm_cpuid2 *cpuid,
28 struct kvm_cpuid_entry2 __user *entries);
29bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
30 u32 *ecx, u32 *edx, bool exact_only);
31
32int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu);
33
34static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
35{
36 return vcpu->arch.maxphyaddr;
37}
38
39static inline bool kvm_vcpu_is_illegal_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
40{
41 return (gpa >= BIT_ULL(cpuid_maxphyaddr(vcpu)));
42}
43
44struct cpuid_reg {
45 u32 function;
46 u32 index;
47 int reg;
48};
49
50static const struct cpuid_reg reverse_cpuid[] = {
51 [CPUID_1_EDX] = { 1, 0, CPUID_EDX},
52 [CPUID_8000_0001_EDX] = {0x80000001, 0, CPUID_EDX},
53 [CPUID_8086_0001_EDX] = {0x80860001, 0, CPUID_EDX},
54 [CPUID_1_ECX] = { 1, 0, CPUID_ECX},
55 [CPUID_C000_0001_EDX] = {0xc0000001, 0, CPUID_EDX},
56 [CPUID_8000_0001_ECX] = {0x80000001, 0, CPUID_ECX},
57 [CPUID_7_0_EBX] = { 7, 0, CPUID_EBX},
58 [CPUID_D_1_EAX] = { 0xd, 1, CPUID_EAX},
59 [CPUID_8000_0008_EBX] = {0x80000008, 0, CPUID_EBX},
60 [CPUID_6_EAX] = { 6, 0, CPUID_EAX},
61 [CPUID_8000_000A_EDX] = {0x8000000a, 0, CPUID_EDX},
62 [CPUID_7_ECX] = { 7, 0, CPUID_ECX},
63 [CPUID_8000_0007_EBX] = {0x80000007, 0, CPUID_EBX},
64 [CPUID_7_EDX] = { 7, 0, CPUID_EDX},
65 [CPUID_7_1_EAX] = { 7, 1, CPUID_EAX},
66};
67
68
69
70
71
72
73
74
75
76static __always_inline void reverse_cpuid_check(unsigned int x86_leaf)
77{
78 BUILD_BUG_ON(x86_leaf == CPUID_LNX_1);
79 BUILD_BUG_ON(x86_leaf == CPUID_LNX_2);
80 BUILD_BUG_ON(x86_leaf == CPUID_LNX_3);
81 BUILD_BUG_ON(x86_leaf == CPUID_LNX_4);
82 BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid));
83 BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0);
84}
85
86
87
88
89
90
91
92static __always_inline u32 __feature_bit(int x86_feature)
93{
94 reverse_cpuid_check(x86_feature / 32);
95 return 1 << (x86_feature & 31);
96}
97
98#define feature_bit(name) __feature_bit(X86_FEATURE_##name)
99
100static __always_inline struct cpuid_reg x86_feature_cpuid(unsigned int x86_feature)
101{
102 unsigned int x86_leaf = x86_feature / 32;
103
104 reverse_cpuid_check(x86_leaf);
105 return reverse_cpuid[x86_leaf];
106}
107
108static __always_inline u32 *__cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry,
109 u32 reg)
110{
111 switch (reg) {
112 case CPUID_EAX:
113 return &entry->eax;
114 case CPUID_EBX:
115 return &entry->ebx;
116 case CPUID_ECX:
117 return &entry->ecx;
118 case CPUID_EDX:
119 return &entry->edx;
120 default:
121 BUILD_BUG();
122 return NULL;
123 }
124}
125
126static __always_inline u32 *cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry,
127 unsigned int x86_feature)
128{
129 const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
130
131 return __cpuid_entry_get_reg(entry, cpuid.reg);
132}
133
134static __always_inline u32 cpuid_entry_get(struct kvm_cpuid_entry2 *entry,
135 unsigned int x86_feature)
136{
137 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
138
139 return *reg & __feature_bit(x86_feature);
140}
141
142static __always_inline bool cpuid_entry_has(struct kvm_cpuid_entry2 *entry,
143 unsigned int x86_feature)
144{
145 return cpuid_entry_get(entry, x86_feature);
146}
147
148static __always_inline void cpuid_entry_clear(struct kvm_cpuid_entry2 *entry,
149 unsigned int x86_feature)
150{
151 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
152
153 *reg &= ~__feature_bit(x86_feature);
154}
155
156static __always_inline void cpuid_entry_set(struct kvm_cpuid_entry2 *entry,
157 unsigned int x86_feature)
158{
159 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
160
161 *reg |= __feature_bit(x86_feature);
162}
163
164static __always_inline void cpuid_entry_change(struct kvm_cpuid_entry2 *entry,
165 unsigned int x86_feature,
166 bool set)
167{
168 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
169
170
171
172
173
174 if (set)
175 *reg |= __feature_bit(x86_feature);
176 else
177 *reg &= ~__feature_bit(x86_feature);
178}
179
180static __always_inline void cpuid_entry_override(struct kvm_cpuid_entry2 *entry,
181 enum cpuid_leafs leaf)
182{
183 u32 *reg = cpuid_entry_get_reg(entry, leaf * 32);
184
185 BUILD_BUG_ON(leaf >= ARRAY_SIZE(kvm_cpu_caps));
186 *reg = kvm_cpu_caps[leaf];
187}
188
189static __always_inline u32 *guest_cpuid_get_register(struct kvm_vcpu *vcpu,
190 unsigned int x86_feature)
191{
192 const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
193 struct kvm_cpuid_entry2 *entry;
194
195 entry = kvm_find_cpuid_entry(vcpu, cpuid.function, cpuid.index);
196 if (!entry)
197 return NULL;
198
199 return __cpuid_entry_get_reg(entry, cpuid.reg);
200}
201
202static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu,
203 unsigned int x86_feature)
204{
205 u32 *reg;
206
207 reg = guest_cpuid_get_register(vcpu, x86_feature);
208 if (!reg)
209 return false;
210
211 return *reg & __feature_bit(x86_feature);
212}
213
214static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu,
215 unsigned int x86_feature)
216{
217 u32 *reg;
218
219 reg = guest_cpuid_get_register(vcpu, x86_feature);
220 if (reg)
221 *reg &= ~__feature_bit(x86_feature);
222}
223
224static inline bool guest_cpuid_is_amd_or_hygon(struct kvm_vcpu *vcpu)
225{
226 struct kvm_cpuid_entry2 *best;
227
228 best = kvm_find_cpuid_entry(vcpu, 0, 0);
229 return best &&
230 (is_guest_vendor_amd(best->ebx, best->ecx, best->edx) ||
231 is_guest_vendor_hygon(best->ebx, best->ecx, best->edx));
232}
233
234static inline int guest_cpuid_family(struct kvm_vcpu *vcpu)
235{
236 struct kvm_cpuid_entry2 *best;
237
238 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
239 if (!best)
240 return -1;
241
242 return x86_family(best->eax);
243}
244
245static inline int guest_cpuid_model(struct kvm_vcpu *vcpu)
246{
247 struct kvm_cpuid_entry2 *best;
248
249 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
250 if (!best)
251 return -1;
252
253 return x86_model(best->eax);
254}
255
256static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
257{
258 struct kvm_cpuid_entry2 *best;
259
260 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
261 if (!best)
262 return -1;
263
264 return x86_stepping(best->eax);
265}
266
267static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
268{
269 return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
270}
271
272static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
273{
274 return vcpu->arch.msr_misc_features_enables &
275 MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
276}
277
278static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature)
279{
280 unsigned int x86_leaf = x86_feature / 32;
281
282 reverse_cpuid_check(x86_leaf);
283 kvm_cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature);
284}
285
286static __always_inline void kvm_cpu_cap_set(unsigned int x86_feature)
287{
288 unsigned int x86_leaf = x86_feature / 32;
289
290 reverse_cpuid_check(x86_leaf);
291 kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature);
292}
293
294static __always_inline u32 kvm_cpu_cap_get(unsigned int x86_feature)
295{
296 unsigned int x86_leaf = x86_feature / 32;
297
298 reverse_cpuid_check(x86_leaf);
299 return kvm_cpu_caps[x86_leaf] & __feature_bit(x86_feature);
300}
301
302static __always_inline bool kvm_cpu_cap_has(unsigned int x86_feature)
303{
304 return !!kvm_cpu_cap_get(x86_feature);
305}
306
307static __always_inline void kvm_cpu_cap_check_and_set(unsigned int x86_feature)
308{
309 if (boot_cpu_has(x86_feature))
310 kvm_cpu_cap_set(x86_feature);
311}
312
313static inline bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
314{
315 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
316}
317
318static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu,
319 unsigned int kvm_feature)
320{
321 if (!vcpu->arch.pv_cpuid.enforce)
322 return true;
323
324 return vcpu->arch.pv_cpuid.features & (1u << kvm_feature);
325}
326
327#endif
328