xref: /openbmc/linux/arch/x86/kvm/cpuid.c (revision bb5a798ad58996e4d666ead1016705854d5ca616)
1 /*
2  * Kernel-based Virtual Machine driver for Linux
3  * cpuid support routines
4  *
5  * derived from arch/x86/kvm/x86.c
6  *
7  * Copyright 2011 Red Hat, Inc. and/or its affiliates.
8  * Copyright IBM Corporation, 2008
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2.  See
11  * the COPYING file in the top-level directory.
12  *
13  */
14 
15 #include <linux/kvm_host.h>
16 #include <linux/module.h>
17 #include <linux/vmalloc.h>
18 #include <linux/uaccess.h>
19 #include <asm/user.h>
20 #include <asm/xsave.h>
21 #include "cpuid.h"
22 #include "lapic.h"
23 #include "mmu.h"
24 #include "trace.h"
25 
26 void kvm_update_cpuid(struct kvm_vcpu *vcpu)
27 {
28 	struct kvm_cpuid_entry2 *best;
29 	struct kvm_lapic *apic = vcpu->arch.apic;
30 
31 	best = kvm_find_cpuid_entry(vcpu, 1, 0);
32 	if (!best)
33 		return;
34 
35 	/* Update OSXSAVE bit */
36 	if (cpu_has_xsave && best->function == 0x1) {
37 		best->ecx &= ~(bit(X86_FEATURE_OSXSAVE));
38 		if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
39 			best->ecx |= bit(X86_FEATURE_OSXSAVE);
40 	}
41 
42 	if (apic) {
43 		if (best->ecx & bit(X86_FEATURE_TSC_DEADLINE_TIMER))
44 			apic->lapic_timer.timer_mode_mask = 3 << 17;
45 		else
46 			apic->lapic_timer.timer_mode_mask = 1 << 17;
47 	}
48 }
49 
50 static int is_efer_nx(void)
51 {
52 	unsigned long long efer = 0;
53 
54 	rdmsrl_safe(MSR_EFER, &efer);
55 	return efer & EFER_NX;
56 }
57 
58 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
59 {
60 	int i;
61 	struct kvm_cpuid_entry2 *e, *entry;
62 
63 	entry = NULL;
64 	for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
65 		e = &vcpu->arch.cpuid_entries[i];
66 		if (e->function == 0x80000001) {
67 			entry = e;
68 			break;
69 		}
70 	}
71 	if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
72 		entry->edx &= ~(1 << 20);
73 		printk(KERN_INFO "kvm: guest NX capability removed\n");
74 	}
75 }
76 
77 /* when an old userspace process fills a new kernel module */
78 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
79 			     struct kvm_cpuid *cpuid,
80 			     struct kvm_cpuid_entry __user *entries)
81 {
82 	int r, i;
83 	struct kvm_cpuid_entry *cpuid_entries;
84 
85 	r = -E2BIG;
86 	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
87 		goto out;
88 	r = -ENOMEM;
89 	cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
90 	if (!cpuid_entries)
91 		goto out;
92 	r = -EFAULT;
93 	if (copy_from_user(cpuid_entries, entries,
94 			   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
95 		goto out_free;
96 	for (i = 0; i < cpuid->nent; i++) {
97 		vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
98 		vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
99 		vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
100 		vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
101 		vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
102 		vcpu->arch.cpuid_entries[i].index = 0;
103 		vcpu->arch.cpuid_entries[i].flags = 0;
104 		vcpu->arch.cpuid_entries[i].padding[0] = 0;
105 		vcpu->arch.cpuid_entries[i].padding[1] = 0;
106 		vcpu->arch.cpuid_entries[i].padding[2] = 0;
107 	}
108 	vcpu->arch.cpuid_nent = cpuid->nent;
109 	cpuid_fix_nx_cap(vcpu);
110 	r = 0;
111 	kvm_apic_set_version(vcpu);
112 	kvm_x86_ops->cpuid_update(vcpu);
113 	kvm_update_cpuid(vcpu);
114 
115 out_free:
116 	vfree(cpuid_entries);
117 out:
118 	return r;
119 }
120 
121 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
122 			      struct kvm_cpuid2 *cpuid,
123 			      struct kvm_cpuid_entry2 __user *entries)
124 {
125 	int r;
126 
127 	r = -E2BIG;
128 	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
129 		goto out;
130 	r = -EFAULT;
131 	if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
132 			   cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
133 		goto out;
134 	vcpu->arch.cpuid_nent = cpuid->nent;
135 	kvm_apic_set_version(vcpu);
136 	kvm_x86_ops->cpuid_update(vcpu);
137 	kvm_update_cpuid(vcpu);
138 	return 0;
139 
140 out:
141 	return r;
142 }
143 
144 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
145 			      struct kvm_cpuid2 *cpuid,
146 			      struct kvm_cpuid_entry2 __user *entries)
147 {
148 	int r;
149 
150 	r = -E2BIG;
151 	if (cpuid->nent < vcpu->arch.cpuid_nent)
152 		goto out;
153 	r = -EFAULT;
154 	if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
155 			 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
156 		goto out;
157 	return 0;
158 
159 out:
160 	cpuid->nent = vcpu->arch.cpuid_nent;
161 	return r;
162 }
163 
164 static void cpuid_mask(u32 *word, int wordnum)
165 {
166 	*word &= boot_cpu_data.x86_capability[wordnum];
167 }
168 
169 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
170 			   u32 index)
171 {
172 	entry->function = function;
173 	entry->index = index;
174 	cpuid_count(entry->function, entry->index,
175 		    &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
176 	entry->flags = 0;
177 }
178 
179 static bool supported_xcr0_bit(unsigned bit)
180 {
181 	u64 mask = ((u64)1 << bit);
182 
183 	return mask & (XSTATE_FP | XSTATE_SSE | XSTATE_YMM) & host_xcr0;
184 }
185 
186 #define F(x) bit(X86_FEATURE_##x)
187 
188 static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
189 			 u32 index, int *nent, int maxnent)
190 {
191 	int r;
192 	unsigned f_nx = is_efer_nx() ? F(NX) : 0;
193 #ifdef CONFIG_X86_64
194 	unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
195 				? F(GBPAGES) : 0;
196 	unsigned f_lm = F(LM);
197 #else
198 	unsigned f_gbpages = 0;
199 	unsigned f_lm = 0;
200 #endif
201 	unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
202 
203 	/* cpuid 1.edx */
204 	const u32 kvm_supported_word0_x86_features =
205 		F(FPU) | F(VME) | F(DE) | F(PSE) |
206 		F(TSC) | F(MSR) | F(PAE) | F(MCE) |
207 		F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
208 		F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
209 		F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
210 		0 /* Reserved, DS, ACPI */ | F(MMX) |
211 		F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
212 		0 /* HTT, TM, Reserved, PBE */;
213 	/* cpuid 0x80000001.edx */
214 	const u32 kvm_supported_word1_x86_features =
215 		F(FPU) | F(VME) | F(DE) | F(PSE) |
216 		F(TSC) | F(MSR) | F(PAE) | F(MCE) |
217 		F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
218 		F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
219 		F(PAT) | F(PSE36) | 0 /* Reserved */ |
220 		f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
221 		F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
222 		0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
223 	/* cpuid 1.ecx */
224 	const u32 kvm_supported_word4_x86_features =
225 		F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
226 		0 /* DS-CPL, VMX, SMX, EST */ |
227 		0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
228 		F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
229 		0 /* Reserved, DCA */ | F(XMM4_1) |
230 		F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
231 		0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
232 		F(F16C) | F(RDRAND);
233 	/* cpuid 0x80000001.ecx */
234 	const u32 kvm_supported_word6_x86_features =
235 		F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
236 		F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
237 		F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(XOP) |
238 		0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM);
239 
240 	/* cpuid 0xC0000001.edx */
241 	const u32 kvm_supported_word5_x86_features =
242 		F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
243 		F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
244 		F(PMM) | F(PMM_EN);
245 
246 	/* cpuid 7.0.ebx */
247 	const u32 kvm_supported_word9_x86_features =
248 		F(FSGSBASE) | F(BMI1) | F(AVX2) | F(SMEP) | F(BMI2) | F(ERMS);
249 
250 	/* all calls to cpuid_count() should be made on the same cpu */
251 	get_cpu();
252 
253 	r = -E2BIG;
254 
255 	if (*nent >= maxnent)
256 		goto out;
257 
258 	do_cpuid_1_ent(entry, function, index);
259 	++*nent;
260 
261 	switch (function) {
262 	case 0:
263 		entry->eax = min(entry->eax, (u32)0xd);
264 		break;
265 	case 1:
266 		entry->edx &= kvm_supported_word0_x86_features;
267 		cpuid_mask(&entry->edx, 0);
268 		entry->ecx &= kvm_supported_word4_x86_features;
269 		cpuid_mask(&entry->ecx, 4);
270 		/* we support x2apic emulation even if host does not support
271 		 * it since we emulate x2apic in software */
272 		entry->ecx |= F(X2APIC);
273 		break;
274 	/* function 2 entries are STATEFUL. That is, repeated cpuid commands
275 	 * may return different values. This forces us to get_cpu() before
276 	 * issuing the first command, and also to emulate this annoying behavior
277 	 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
278 	case 2: {
279 		int t, times = entry->eax & 0xff;
280 
281 		entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
282 		entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
283 		for (t = 1; t < times; ++t) {
284 			if (*nent >= maxnent)
285 				goto out;
286 
287 			do_cpuid_1_ent(&entry[t], function, 0);
288 			entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
289 			++*nent;
290 		}
291 		break;
292 	}
293 	/* function 4 has additional index. */
294 	case 4: {
295 		int i, cache_type;
296 
297 		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
298 		/* read more entries until cache_type is zero */
299 		for (i = 1; ; ++i) {
300 			if (*nent >= maxnent)
301 				goto out;
302 
303 			cache_type = entry[i - 1].eax & 0x1f;
304 			if (!cache_type)
305 				break;
306 			do_cpuid_1_ent(&entry[i], function, i);
307 			entry[i].flags |=
308 			       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
309 			++*nent;
310 		}
311 		break;
312 	}
313 	case 7: {
314 		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
315 		/* Mask ebx against host capbability word 9 */
316 		if (index == 0) {
317 			entry->ebx &= kvm_supported_word9_x86_features;
318 			cpuid_mask(&entry->ebx, 9);
319 		} else
320 			entry->ebx = 0;
321 		entry->eax = 0;
322 		entry->ecx = 0;
323 		entry->edx = 0;
324 		break;
325 	}
326 	case 9:
327 		break;
328 	/* function 0xb has additional index. */
329 	case 0xb: {
330 		int i, level_type;
331 
332 		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
333 		/* read more entries until level_type is zero */
334 		for (i = 1; ; ++i) {
335 			if (*nent >= maxnent)
336 				goto out;
337 
338 			level_type = entry[i - 1].ecx & 0xff00;
339 			if (!level_type)
340 				break;
341 			do_cpuid_1_ent(&entry[i], function, i);
342 			entry[i].flags |=
343 			       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
344 			++*nent;
345 		}
346 		break;
347 	}
348 	case 0xd: {
349 		int idx, i;
350 
351 		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
352 		for (idx = 1, i = 1; idx < 64; ++idx) {
353 			if (*nent >= maxnent)
354 				goto out;
355 
356 			do_cpuid_1_ent(&entry[i], function, idx);
357 			if (entry[i].eax == 0 || !supported_xcr0_bit(idx))
358 				continue;
359 			entry[i].flags |=
360 			       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
361 			++*nent;
362 			++i;
363 		}
364 		break;
365 	}
366 	case KVM_CPUID_SIGNATURE: {
367 		char signature[12] = "KVMKVMKVM\0\0";
368 		u32 *sigptr = (u32 *)signature;
369 		entry->eax = 0;
370 		entry->ebx = sigptr[0];
371 		entry->ecx = sigptr[1];
372 		entry->edx = sigptr[2];
373 		break;
374 	}
375 	case KVM_CPUID_FEATURES:
376 		entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
377 			     (1 << KVM_FEATURE_NOP_IO_DELAY) |
378 			     (1 << KVM_FEATURE_CLOCKSOURCE2) |
379 			     (1 << KVM_FEATURE_ASYNC_PF) |
380 			     (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
381 
382 		if (sched_info_on())
383 			entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
384 
385 		entry->ebx = 0;
386 		entry->ecx = 0;
387 		entry->edx = 0;
388 		break;
389 	case 0x80000000:
390 		entry->eax = min(entry->eax, 0x8000001a);
391 		break;
392 	case 0x80000001:
393 		entry->edx &= kvm_supported_word1_x86_features;
394 		cpuid_mask(&entry->edx, 1);
395 		entry->ecx &= kvm_supported_word6_x86_features;
396 		cpuid_mask(&entry->ecx, 6);
397 		break;
398 	case 0x80000008: {
399 		unsigned g_phys_as = (entry->eax >> 16) & 0xff;
400 		unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
401 		unsigned phys_as = entry->eax & 0xff;
402 
403 		if (!g_phys_as)
404 			g_phys_as = phys_as;
405 		entry->eax = g_phys_as | (virt_as << 8);
406 		entry->ebx = entry->edx = 0;
407 		break;
408 	}
409 	case 0x80000019:
410 		entry->ecx = entry->edx = 0;
411 		break;
412 	case 0x8000001a:
413 		break;
414 	case 0x8000001d:
415 		break;
416 	/*Add support for Centaur's CPUID instruction*/
417 	case 0xC0000000:
418 		/*Just support up to 0xC0000004 now*/
419 		entry->eax = min(entry->eax, 0xC0000004);
420 		break;
421 	case 0xC0000001:
422 		entry->edx &= kvm_supported_word5_x86_features;
423 		cpuid_mask(&entry->edx, 5);
424 		break;
425 	case 3: /* Processor serial number */
426 	case 5: /* MONITOR/MWAIT */
427 	case 6: /* Thermal management */
428 	case 0xA: /* Architectural Performance Monitoring */
429 	case 0x80000007: /* Advanced power management */
430 	case 0xC0000002:
431 	case 0xC0000003:
432 	case 0xC0000004:
433 	default:
434 		entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
435 		break;
436 	}
437 
438 	kvm_x86_ops->set_supported_cpuid(function, entry);
439 
440 	r = 0;
441 
442 out:
443 	put_cpu();
444 
445 	return r;
446 }
447 
448 #undef F
449 
450 struct kvm_cpuid_param {
451 	u32 func;
452 	u32 idx;
453 	bool has_leaf_count;
454 	bool (*qualifier)(struct kvm_cpuid_param *param);
455 };
456 
457 static bool is_centaur_cpu(struct kvm_cpuid_param *param)
458 {
459 	return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR;
460 }
461 
462 int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
463 				      struct kvm_cpuid_entry2 __user *entries)
464 {
465 	struct kvm_cpuid_entry2 *cpuid_entries;
466 	int limit, nent = 0, r = -E2BIG, i;
467 	u32 func;
468 	static struct kvm_cpuid_param param[] = {
469 		{ .func = 0, .has_leaf_count = true },
470 		{ .func = 0x80000000, .has_leaf_count = true },
471 		{ .func = 0xC0000000, .qualifier = is_centaur_cpu, .has_leaf_count = true },
472 		{ .func = KVM_CPUID_SIGNATURE },
473 		{ .func = KVM_CPUID_FEATURES },
474 	};
475 
476 	if (cpuid->nent < 1)
477 		goto out;
478 	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
479 		cpuid->nent = KVM_MAX_CPUID_ENTRIES;
480 	r = -ENOMEM;
481 	cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
482 	if (!cpuid_entries)
483 		goto out;
484 
485 	r = 0;
486 	for (i = 0; i < ARRAY_SIZE(param); i++) {
487 		struct kvm_cpuid_param *ent = &param[i];
488 
489 		if (ent->qualifier && !ent->qualifier(ent))
490 			continue;
491 
492 		r = do_cpuid_ent(&cpuid_entries[nent], ent->func, ent->idx,
493 				&nent, cpuid->nent);
494 
495 		if (r)
496 			goto out_free;
497 
498 		if (!ent->has_leaf_count)
499 			continue;
500 
501 		limit = cpuid_entries[nent - 1].eax;
502 		for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func)
503 			r = do_cpuid_ent(&cpuid_entries[nent], func, ent->idx,
504 				     &nent, cpuid->nent);
505 
506 		if (r)
507 			goto out_free;
508 	}
509 
510 	r = -EFAULT;
511 	if (copy_to_user(entries, cpuid_entries,
512 			 nent * sizeof(struct kvm_cpuid_entry2)))
513 		goto out_free;
514 	cpuid->nent = nent;
515 	r = 0;
516 
517 out_free:
518 	vfree(cpuid_entries);
519 out:
520 	return r;
521 }
522 
523 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
524 {
525 	struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
526 	int j, nent = vcpu->arch.cpuid_nent;
527 
528 	e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
529 	/* when no next entry is found, the current entry[i] is reselected */
530 	for (j = i + 1; ; j = (j + 1) % nent) {
531 		struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
532 		if (ej->function == e->function) {
533 			ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
534 			return j;
535 		}
536 	}
537 	return 0; /* silence gcc, even though control never reaches here */
538 }
539 
540 /* find an entry with matching function, matching index (if needed), and that
541  * should be read next (if it's stateful) */
542 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
543 	u32 function, u32 index)
544 {
545 	if (e->function != function)
546 		return 0;
547 	if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
548 		return 0;
549 	if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
550 	    !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
551 		return 0;
552 	return 1;
553 }
554 
555 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
556 					      u32 function, u32 index)
557 {
558 	int i;
559 	struct kvm_cpuid_entry2 *best = NULL;
560 
561 	for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
562 		struct kvm_cpuid_entry2 *e;
563 
564 		e = &vcpu->arch.cpuid_entries[i];
565 		if (is_matching_cpuid_entry(e, function, index)) {
566 			if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
567 				move_to_next_stateful_cpuid_entry(vcpu, i);
568 			best = e;
569 			break;
570 		}
571 	}
572 	return best;
573 }
574 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
575 
576 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
577 {
578 	struct kvm_cpuid_entry2 *best;
579 
580 	best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
581 	if (!best || best->eax < 0x80000008)
582 		goto not_found;
583 	best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
584 	if (best)
585 		return best->eax & 0xff;
586 not_found:
587 	return 36;
588 }
589 
590 /*
591  * If no match is found, check whether we exceed the vCPU's limit
592  * and return the content of the highest valid _standard_ leaf instead.
593  * This is to satisfy the CPUID specification.
594  */
595 static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
596                                                   u32 function, u32 index)
597 {
598 	struct kvm_cpuid_entry2 *maxlevel;
599 
600 	maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
601 	if (!maxlevel || maxlevel->eax >= function)
602 		return NULL;
603 	if (function & 0x80000000) {
604 		maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
605 		if (!maxlevel)
606 			return NULL;
607 	}
608 	return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
609 }
610 
611 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
612 {
613 	u32 function, index;
614 	struct kvm_cpuid_entry2 *best;
615 
616 	function = kvm_register_read(vcpu, VCPU_REGS_RAX);
617 	index = kvm_register_read(vcpu, VCPU_REGS_RCX);
618 	kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
619 	kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
620 	kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
621 	kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
622 	best = kvm_find_cpuid_entry(vcpu, function, index);
623 
624 	if (!best)
625 		best = check_cpuid_limit(vcpu, function, index);
626 
627 	if (best) {
628 		kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
629 		kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
630 		kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
631 		kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
632 	}
633 	kvm_x86_ops->skip_emulated_instruction(vcpu);
634 	trace_kvm_cpuid(function,
635 			kvm_register_read(vcpu, VCPU_REGS_RAX),
636 			kvm_register_read(vcpu, VCPU_REGS_RBX),
637 			kvm_register_read(vcpu, VCPU_REGS_RCX),
638 			kvm_register_read(vcpu, VCPU_REGS_RDX));
639 }
640 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
641