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