xref: /kvmtool/x86/cpuid.c (revision faae833a746f1c59f64fb05e9c08e62b5b0002eb)
15c3d55faSPekka Enberg #include "kvm/kvm-cpu.h"
25c3d55faSPekka Enberg 
3a1fe6bc5SPekka Enberg #include "kvm/kvm.h"
4a1fe6bc5SPekka Enberg #include "kvm/util.h"
5a1fe6bc5SPekka Enberg 
6a1fe6bc5SPekka Enberg #include <sys/ioctl.h>
7a1fe6bc5SPekka Enberg #include <stdlib.h>
8a1fe6bc5SPekka Enberg 
9a1fe6bc5SPekka Enberg #define	MAX_KVM_CPUID_ENTRIES		100
10a1fe6bc5SPekka Enberg 
filter_cpuid(struct kvm_cpuid2 * kvm_cpuid,int cpu_id)11*20b93be5SMuchun Song static void filter_cpuid(struct kvm_cpuid2 *kvm_cpuid, int cpu_id)
121e1b3818SPekka Enberg {
131e1b3818SPekka Enberg 	unsigned int i;
141e1b3818SPekka Enberg 
151e1b3818SPekka Enberg 	/*
161e1b3818SPekka Enberg 	 * Filter CPUID functions that are not supported by the hypervisor.
171e1b3818SPekka Enberg 	 */
181e1b3818SPekka Enberg 	for (i = 0; i < kvm_cpuid->nent; i++) {
191e1b3818SPekka Enberg 		struct kvm_cpuid_entry2 *entry = &kvm_cpuid->entries[i];
201e1b3818SPekka Enberg 
211e1b3818SPekka Enberg 		switch (entry->function) {
220d5b41d9SSasha Levin 		case 1:
23*20b93be5SMuchun Song 			entry->ebx &= ~(0xff << 24);
24*20b93be5SMuchun Song 			entry->ebx |= cpu_id << 24;
250d5b41d9SSasha Levin 			/* Set X86_FEATURE_HYPERVISOR */
260d5b41d9SSasha Levin 			if (entry->index == 0)
270d5b41d9SSasha Levin 				entry->ecx |= (1 << 31);
280d5b41d9SSasha Levin 			break;
29abc73d8dSCyrill Gorcunov 		case 6:
300d5b41d9SSasha Levin 			/* Clear X86_FEATURE_EPB */
31abc73d8dSCyrill Gorcunov 			entry->ecx = entry->ecx & ~(1 << 3);
32abc73d8dSCyrill Gorcunov 			break;
337800e6faSCyrill Gorcunov 		case 10: { /* Architectural Performance Monitoring */
347800e6faSCyrill Gorcunov 			union cpuid10_eax {
357800e6faSCyrill Gorcunov 				struct {
367800e6faSCyrill Gorcunov 					unsigned int version_id		:8;
377800e6faSCyrill Gorcunov 					unsigned int num_counters	:8;
387800e6faSCyrill Gorcunov 					unsigned int bit_width		:8;
397800e6faSCyrill Gorcunov 					unsigned int mask_length	:8;
407800e6faSCyrill Gorcunov 				} split;
417800e6faSCyrill Gorcunov 				unsigned int full;
427800e6faSCyrill Gorcunov 			} eax;
437800e6faSCyrill Gorcunov 
447800e6faSCyrill Gorcunov 			/*
457800e6faSCyrill Gorcunov 			 * If the host has perf system running,
467800e6faSCyrill Gorcunov 			 * but no architectural events available
477800e6faSCyrill Gorcunov 			 * through kvm pmu -- disable perf support,
487800e6faSCyrill Gorcunov 			 * thus guest won't even try to access msr
497800e6faSCyrill Gorcunov 			 * registers.
507800e6faSCyrill Gorcunov 			 */
517800e6faSCyrill Gorcunov 			if (entry->eax) {
527800e6faSCyrill Gorcunov 				eax.full = entry->eax;
537800e6faSCyrill Gorcunov 				if (eax.split.version_id != 2 ||
547800e6faSCyrill Gorcunov 				    !eax.split.num_counters)
557800e6faSCyrill Gorcunov 					entry->eax = 0;
567800e6faSCyrill Gorcunov 			}
571e1b3818SPekka Enberg 			break;
587800e6faSCyrill Gorcunov 		}
591e1b3818SPekka Enberg 		default:
601e1b3818SPekka Enberg 			/* Keep the CPUID function as -is */
611e1b3818SPekka Enberg 			break;
621e1b3818SPekka Enberg 		};
631e1b3818SPekka Enberg 	}
641e1b3818SPekka Enberg }
651e1b3818SPekka Enberg 
kvm_cpu__setup_cpuid(struct kvm_cpu * vcpu)6643835ac9SSasha Levin void kvm_cpu__setup_cpuid(struct kvm_cpu *vcpu)
67a1fe6bc5SPekka Enberg {
68a1fe6bc5SPekka Enberg 	struct kvm_cpuid2 *kvm_cpuid;
69a1fe6bc5SPekka Enberg 
703a60be06SSasha Levin 	kvm_cpuid = calloc(1, sizeof(*kvm_cpuid) +
713a60be06SSasha Levin 				MAX_KVM_CPUID_ENTRIES * sizeof(*kvm_cpuid->entries));
72a1fe6bc5SPekka Enberg 
73bb988cf5SAsias He 	kvm_cpuid->nent = MAX_KVM_CPUID_ENTRIES;
7443835ac9SSasha Levin 	if (ioctl(vcpu->kvm->sys_fd, KVM_GET_SUPPORTED_CPUID, kvm_cpuid) < 0)
75bb988cf5SAsias He 		die_perror("KVM_GET_SUPPORTED_CPUID failed");
76a1fe6bc5SPekka Enberg 
77*20b93be5SMuchun Song 	filter_cpuid(kvm_cpuid, vcpu->cpu_id);
781e1b3818SPekka Enberg 
7943835ac9SSasha Levin 	if (ioctl(vcpu->vcpu_fd, KVM_SET_CPUID2, kvm_cpuid) < 0)
80a1fe6bc5SPekka Enberg 		die_perror("KVM_SET_CPUID2 failed");
81a1fe6bc5SPekka Enberg 
82bb988cf5SAsias He 	free(kvm_cpuid);
83a1fe6bc5SPekka Enberg }
84