]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/x86/kvm/cpuid.h
KVM: x86: Fix implicit enum conversion goof in scattered reverse CPUID code
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / kvm / cpuid.h
1 /* SPDX-License-Identifier: GPL-2.0 */
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
10 /*
11 * Hardware-defined CPUID leafs that are scattered in the kernel, but need to
12 * be directly used by KVM. Note, these word values conflict with the kernel's
13 * "bug" caps, but KVM doesn't use those.
14 */
15 enum kvm_only_cpuid_leafs {
16 CPUID_12_EAX = NCAPINTS,
17 NR_KVM_CPU_CAPS,
18
19 NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,
20 };
21
22 #define KVM_X86_FEATURE(w, f) ((w)*32 + (f))
23
24 /* Intel-defined SGX sub-features, CPUID level 0x12 (EAX). */
25 #define KVM_X86_FEATURE_SGX1 KVM_X86_FEATURE(CPUID_12_EAX, 0)
26 #define KVM_X86_FEATURE_SGX2 KVM_X86_FEATURE(CPUID_12_EAX, 1)
27
28 extern u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
29 void kvm_set_cpu_caps(void);
30
31 void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu);
32 void kvm_update_pv_runtime(struct kvm_vcpu *vcpu);
33 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
34 u32 function, u32 index);
35 int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
36 struct kvm_cpuid_entry2 __user *entries,
37 unsigned int type);
38 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
39 struct kvm_cpuid *cpuid,
40 struct kvm_cpuid_entry __user *entries);
41 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
42 struct kvm_cpuid2 *cpuid,
43 struct kvm_cpuid_entry2 __user *entries);
44 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
45 struct kvm_cpuid2 *cpuid,
46 struct kvm_cpuid_entry2 __user *entries);
47 bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
48 u32 *ecx, u32 *edx, bool exact_only);
49
50 int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu);
51 u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu);
52
53 static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
54 {
55 return vcpu->arch.maxphyaddr;
56 }
57
58 static inline bool kvm_vcpu_is_legal_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
59 {
60 return !(gpa & vcpu->arch.reserved_gpa_bits);
61 }
62
63 static inline bool kvm_vcpu_is_illegal_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
64 {
65 return !kvm_vcpu_is_legal_gpa(vcpu, gpa);
66 }
67
68 static inline bool kvm_vcpu_is_legal_aligned_gpa(struct kvm_vcpu *vcpu,
69 gpa_t gpa, gpa_t alignment)
70 {
71 return IS_ALIGNED(gpa, alignment) && kvm_vcpu_is_legal_gpa(vcpu, gpa);
72 }
73
74 static inline bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
75 {
76 return kvm_vcpu_is_legal_aligned_gpa(vcpu, gpa, PAGE_SIZE);
77 }
78
79 struct cpuid_reg {
80 u32 function;
81 u32 index;
82 int reg;
83 };
84
85 static const struct cpuid_reg reverse_cpuid[] = {
86 [CPUID_1_EDX] = { 1, 0, CPUID_EDX},
87 [CPUID_8000_0001_EDX] = {0x80000001, 0, CPUID_EDX},
88 [CPUID_8086_0001_EDX] = {0x80860001, 0, CPUID_EDX},
89 [CPUID_1_ECX] = { 1, 0, CPUID_ECX},
90 [CPUID_C000_0001_EDX] = {0xc0000001, 0, CPUID_EDX},
91 [CPUID_8000_0001_ECX] = {0x80000001, 0, CPUID_ECX},
92 [CPUID_7_0_EBX] = { 7, 0, CPUID_EBX},
93 [CPUID_D_1_EAX] = { 0xd, 1, CPUID_EAX},
94 [CPUID_8000_0008_EBX] = {0x80000008, 0, CPUID_EBX},
95 [CPUID_6_EAX] = { 6, 0, CPUID_EAX},
96 [CPUID_8000_000A_EDX] = {0x8000000a, 0, CPUID_EDX},
97 [CPUID_7_ECX] = { 7, 0, CPUID_ECX},
98 [CPUID_8000_0007_EBX] = {0x80000007, 0, CPUID_EBX},
99 [CPUID_7_EDX] = { 7, 0, CPUID_EDX},
100 [CPUID_7_1_EAX] = { 7, 1, CPUID_EAX},
101 [CPUID_12_EAX] = {0x00000012, 0, CPUID_EAX},
102 };
103
104 /*
105 * Reverse CPUID and its derivatives can only be used for hardware-defined
106 * feature words, i.e. words whose bits directly correspond to a CPUID leaf.
107 * Retrieving a feature bit or masking guest CPUID from a Linux-defined word
108 * is nonsensical as the bit number/mask is an arbitrary software-defined value
109 * and can't be used by KVM to query/control guest capabilities. And obviously
110 * the leaf being queried must have an entry in the lookup table.
111 */
112 static __always_inline void reverse_cpuid_check(unsigned int x86_leaf)
113 {
114 BUILD_BUG_ON(x86_leaf == CPUID_LNX_1);
115 BUILD_BUG_ON(x86_leaf == CPUID_LNX_2);
116 BUILD_BUG_ON(x86_leaf == CPUID_LNX_3);
117 BUILD_BUG_ON(x86_leaf == CPUID_LNX_4);
118 BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid));
119 BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0);
120 }
121
122 /*
123 * Translate feature bits that are scattered in the kernel's cpufeatures word
124 * into KVM feature words that align with hardware's definitions.
125 */
126 static __always_inline u32 __feature_translate(int x86_feature)
127 {
128 if (x86_feature == X86_FEATURE_SGX1)
129 return KVM_X86_FEATURE_SGX1;
130 else if (x86_feature == X86_FEATURE_SGX2)
131 return KVM_X86_FEATURE_SGX2;
132
133 return x86_feature;
134 }
135
136 static __always_inline u32 __feature_leaf(int x86_feature)
137 {
138 return __feature_translate(x86_feature) / 32;
139 }
140
141 /*
142 * Retrieve the bit mask from an X86_FEATURE_* definition. Features contain
143 * the hardware defined bit number (stored in bits 4:0) and a software defined
144 * "word" (stored in bits 31:5). The word is used to index into arrays of
145 * bit masks that hold the per-cpu feature capabilities, e.g. this_cpu_has().
146 */
147 static __always_inline u32 __feature_bit(int x86_feature)
148 {
149 x86_feature = __feature_translate(x86_feature);
150
151 reverse_cpuid_check(x86_feature / 32);
152 return 1 << (x86_feature & 31);
153 }
154
155 #define feature_bit(name) __feature_bit(X86_FEATURE_##name)
156
157 static __always_inline struct cpuid_reg x86_feature_cpuid(unsigned int x86_feature)
158 {
159 unsigned int x86_leaf = __feature_leaf(x86_feature);
160
161 reverse_cpuid_check(x86_leaf);
162 return reverse_cpuid[x86_leaf];
163 }
164
165 static __always_inline u32 *__cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry,
166 u32 reg)
167 {
168 switch (reg) {
169 case CPUID_EAX:
170 return &entry->eax;
171 case CPUID_EBX:
172 return &entry->ebx;
173 case CPUID_ECX:
174 return &entry->ecx;
175 case CPUID_EDX:
176 return &entry->edx;
177 default:
178 BUILD_BUG();
179 return NULL;
180 }
181 }
182
183 static __always_inline u32 *cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry,
184 unsigned int x86_feature)
185 {
186 const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
187
188 return __cpuid_entry_get_reg(entry, cpuid.reg);
189 }
190
191 static __always_inline u32 cpuid_entry_get(struct kvm_cpuid_entry2 *entry,
192 unsigned int x86_feature)
193 {
194 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
195
196 return *reg & __feature_bit(x86_feature);
197 }
198
199 static __always_inline bool cpuid_entry_has(struct kvm_cpuid_entry2 *entry,
200 unsigned int x86_feature)
201 {
202 return cpuid_entry_get(entry, x86_feature);
203 }
204
205 static __always_inline void cpuid_entry_clear(struct kvm_cpuid_entry2 *entry,
206 unsigned int x86_feature)
207 {
208 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
209
210 *reg &= ~__feature_bit(x86_feature);
211 }
212
213 static __always_inline void cpuid_entry_set(struct kvm_cpuid_entry2 *entry,
214 unsigned int x86_feature)
215 {
216 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
217
218 *reg |= __feature_bit(x86_feature);
219 }
220
221 static __always_inline void cpuid_entry_change(struct kvm_cpuid_entry2 *entry,
222 unsigned int x86_feature,
223 bool set)
224 {
225 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
226
227 /*
228 * Open coded instead of using cpuid_entry_{clear,set}() to coerce the
229 * compiler into using CMOV instead of Jcc when possible.
230 */
231 if (set)
232 *reg |= __feature_bit(x86_feature);
233 else
234 *reg &= ~__feature_bit(x86_feature);
235 }
236
237 static __always_inline void cpuid_entry_override(struct kvm_cpuid_entry2 *entry,
238 unsigned int leaf)
239 {
240 u32 *reg = cpuid_entry_get_reg(entry, leaf * 32);
241
242 BUILD_BUG_ON(leaf >= ARRAY_SIZE(kvm_cpu_caps));
243 *reg = kvm_cpu_caps[leaf];
244 }
245
246 static __always_inline u32 *guest_cpuid_get_register(struct kvm_vcpu *vcpu,
247 unsigned int x86_feature)
248 {
249 const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
250 struct kvm_cpuid_entry2 *entry;
251
252 entry = kvm_find_cpuid_entry(vcpu, cpuid.function, cpuid.index);
253 if (!entry)
254 return NULL;
255
256 return __cpuid_entry_get_reg(entry, cpuid.reg);
257 }
258
259 static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu,
260 unsigned int x86_feature)
261 {
262 u32 *reg;
263
264 reg = guest_cpuid_get_register(vcpu, x86_feature);
265 if (!reg)
266 return false;
267
268 return *reg & __feature_bit(x86_feature);
269 }
270
271 static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu,
272 unsigned int x86_feature)
273 {
274 u32 *reg;
275
276 reg = guest_cpuid_get_register(vcpu, x86_feature);
277 if (reg)
278 *reg &= ~__feature_bit(x86_feature);
279 }
280
281 static inline bool guest_cpuid_is_amd_or_hygon(struct kvm_vcpu *vcpu)
282 {
283 struct kvm_cpuid_entry2 *best;
284
285 best = kvm_find_cpuid_entry(vcpu, 0, 0);
286 return best &&
287 (is_guest_vendor_amd(best->ebx, best->ecx, best->edx) ||
288 is_guest_vendor_hygon(best->ebx, best->ecx, best->edx));
289 }
290
291 static inline bool guest_cpuid_is_intel(struct kvm_vcpu *vcpu)
292 {
293 struct kvm_cpuid_entry2 *best;
294
295 best = kvm_find_cpuid_entry(vcpu, 0, 0);
296 return best && is_guest_vendor_intel(best->ebx, best->ecx, best->edx);
297 }
298
299 static inline int guest_cpuid_family(struct kvm_vcpu *vcpu)
300 {
301 struct kvm_cpuid_entry2 *best;
302
303 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
304 if (!best)
305 return -1;
306
307 return x86_family(best->eax);
308 }
309
310 static inline int guest_cpuid_model(struct kvm_vcpu *vcpu)
311 {
312 struct kvm_cpuid_entry2 *best;
313
314 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
315 if (!best)
316 return -1;
317
318 return x86_model(best->eax);
319 }
320
321 static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
322 {
323 struct kvm_cpuid_entry2 *best;
324
325 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
326 if (!best)
327 return -1;
328
329 return x86_stepping(best->eax);
330 }
331
332 static inline bool guest_has_spec_ctrl_msr(struct kvm_vcpu *vcpu)
333 {
334 return (guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) ||
335 guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) ||
336 guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) ||
337 guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD));
338 }
339
340 static inline bool guest_has_pred_cmd_msr(struct kvm_vcpu *vcpu)
341 {
342 return (guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) ||
343 guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB));
344 }
345
346 static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
347 {
348 return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
349 }
350
351 static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
352 {
353 return vcpu->arch.msr_misc_features_enables &
354 MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
355 }
356
357 static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature)
358 {
359 unsigned int x86_leaf = __feature_leaf(x86_feature);
360
361 reverse_cpuid_check(x86_leaf);
362 kvm_cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature);
363 }
364
365 static __always_inline void kvm_cpu_cap_set(unsigned int x86_feature)
366 {
367 unsigned int x86_leaf = __feature_leaf(x86_feature);
368
369 reverse_cpuid_check(x86_leaf);
370 kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature);
371 }
372
373 static __always_inline u32 kvm_cpu_cap_get(unsigned int x86_feature)
374 {
375 unsigned int x86_leaf = __feature_leaf(x86_feature);
376
377 reverse_cpuid_check(x86_leaf);
378 return kvm_cpu_caps[x86_leaf] & __feature_bit(x86_feature);
379 }
380
381 static __always_inline bool kvm_cpu_cap_has(unsigned int x86_feature)
382 {
383 return !!kvm_cpu_cap_get(x86_feature);
384 }
385
386 static __always_inline void kvm_cpu_cap_check_and_set(unsigned int x86_feature)
387 {
388 if (boot_cpu_has(x86_feature))
389 kvm_cpu_cap_set(x86_feature);
390 }
391
392 static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu,
393 unsigned int kvm_feature)
394 {
395 if (!vcpu->arch.pv_cpuid.enforce)
396 return true;
397
398 return vcpu->arch.pv_cpuid.features & (1u << kvm_feature);
399 }
400
401 #endif