cpuid.c 24.0 KB
Newer Older
A
Avi Kivity 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Kernel-based Virtual Machine driver for Linux
 * cpuid support routines
 *
 * derived from arch/x86/kvm/x86.c
 *
 * Copyright 2011 Red Hat, Inc. and/or its affiliates.
 * Copyright IBM Corporation, 2008
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

#include <linux/kvm_host.h>
16
#include <linux/export.h>
17 18
#include <linux/vmalloc.h>
#include <linux/uaccess.h>
19 20
#include <linux/sched/stat.h>

21
#include <asm/processor.h>
A
Avi Kivity 已提交
22
#include <asm/user.h>
23
#include <asm/fpu/xstate.h>
A
Avi Kivity 已提交
24 25 26 27
#include "cpuid.h"
#include "lapic.h"
#include "mmu.h"
#include "trace.h"
28
#include "pmu.h"
A
Avi Kivity 已提交
29

30
static u32 xstate_required_size(u64 xstate_bv, bool compacted)
31 32 33 34
{
	int feature_bit = 0;
	u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;

D
Dave Hansen 已提交
35
	xstate_bv &= XFEATURE_MASK_EXTEND;
36 37
	while (xstate_bv) {
		if (xstate_bv & 0x1) {
38
		        u32 eax, ebx, ecx, edx, offset;
39
		        cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
40 41
			offset = compacted ? ret : ebx;
			ret = max(ret, offset + eax);
42 43 44 45 46 47 48 49 50
		}

		xstate_bv >>= 1;
		feature_bit++;
	}

	return ret;
}

51 52 53 54 55 56 57
bool kvm_mpx_supported(void)
{
	return ((host_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
		 && kvm_x86_ops->mpx_supported());
}
EXPORT_SYMBOL_GPL(kvm_mpx_supported);

58 59 60 61
u64 kvm_supported_xcr0(void)
{
	u64 xcr0 = KVM_SUPPORTED_XCR0 & host_xcr0;

62
	if (!kvm_mpx_supported())
D
Dave Hansen 已提交
63
		xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
64 65 66 67

	return xcr0;
}

68 69
#define F(x) bit(X86_FEATURE_##x)

70
/* For scattered features from cpufeatures.h; we currently expose none */
71 72
#define KF(x) bit(KVM_CPUID_BIT_##x)

73
int kvm_update_cpuid(struct kvm_vcpu *vcpu)
A
Avi Kivity 已提交
74 75 76 77 78 79
{
	struct kvm_cpuid_entry2 *best;
	struct kvm_lapic *apic = vcpu->arch.apic;

	best = kvm_find_cpuid_entry(vcpu, 1, 0);
	if (!best)
80
		return 0;
A
Avi Kivity 已提交
81 82

	/* Update OSXSAVE bit */
83
	if (boot_cpu_has(X86_FEATURE_XSAVE) && best->function == 0x1) {
84
		best->ecx &= ~F(OSXSAVE);
A
Avi Kivity 已提交
85
		if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
86
			best->ecx |= F(OSXSAVE);
A
Avi Kivity 已提交
87 88
	}

89 90 91 92
	best->edx &= ~F(APIC);
	if (vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE)
		best->edx |= F(APIC);

A
Avi Kivity 已提交
93
	if (apic) {
94
		if (best->ecx & F(TSC_DEADLINE_TIMER))
A
Avi Kivity 已提交
95 96 97 98
			apic->lapic_timer.timer_mode_mask = 3 << 17;
		else
			apic->lapic_timer.timer_mode_mask = 1 << 17;
	}
99

100 101 102 103 104 105 106 107 108 109
	best = kvm_find_cpuid_entry(vcpu, 7, 0);
	if (best) {
		/* Update OSPKE bit */
		if (boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7) {
			best->ecx &= ~F(OSPKE);
			if (kvm_read_cr4_bits(vcpu, X86_CR4_PKE))
				best->ecx |= F(OSPKE);
		}
	}

110
	best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
111
	if (!best) {
112
		vcpu->arch.guest_supported_xcr0 = 0;
113 114
		vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
	} else {
115 116
		vcpu->arch.guest_supported_xcr0 =
			(best->eax | ((u64)best->edx << 32)) &
117
			kvm_supported_xcr0();
118
		vcpu->arch.guest_xstate_size = best->ebx =
119
			xstate_required_size(vcpu->arch.xcr0, false);
120
	}
121

122 123 124 125
	best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
	if (best && (best->eax & (F(XSAVES) | F(XSAVEC))))
		best->ebx = xstate_required_size(vcpu->arch.xcr0, true);

126
	/*
127 128
	 * The existing code assumes virtual address is 48-bit or 57-bit in the
	 * canonical address checks; exit if it is ever changed.
129 130
	 */
	best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
131 132 133 134 135 136
	if (best) {
		int vaddr_bits = (best->eax & 0xff00) >> 8;

		if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
			return -EINVAL;
	}
137

138 139
	/* Update physical-address width */
	vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
140
	kvm_mmu_reset_context(vcpu);
141

142
	kvm_pmu_refresh(vcpu);
143
	return 0;
A
Avi Kivity 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
}

static int is_efer_nx(void)
{
	unsigned long long efer = 0;

	rdmsrl_safe(MSR_EFER, &efer);
	return efer & EFER_NX;
}

static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
{
	int i;
	struct kvm_cpuid_entry2 *e, *entry;

	entry = NULL;
	for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
		e = &vcpu->arch.cpuid_entries[i];
		if (e->function == 0x80000001) {
			entry = e;
			break;
		}
	}
167 168
	if (entry && (entry->edx & F(NX)) && !is_efer_nx()) {
		entry->edx &= ~F(NX);
A
Avi Kivity 已提交
169 170 171 172
		printk(KERN_INFO "kvm: guest NX capability removed\n");
	}
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
{
	struct kvm_cpuid_entry2 *best;

	best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
	if (!best || best->eax < 0x80000008)
		goto not_found;
	best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
	if (best)
		return best->eax & 0xff;
not_found:
	return 36;
}
EXPORT_SYMBOL_GPL(cpuid_query_maxphyaddr);

A
Avi Kivity 已提交
188 189 190 191 192 193
/* when an old userspace process fills a new kernel module */
int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
			     struct kvm_cpuid *cpuid,
			     struct kvm_cpuid_entry __user *entries)
{
	int r, i;
194
	struct kvm_cpuid_entry *cpuid_entries = NULL;
A
Avi Kivity 已提交
195 196 197 198 199

	r = -E2BIG;
	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
		goto out;
	r = -ENOMEM;
200 201 202 203 204 205 206 207 208 209
	if (cpuid->nent) {
		cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) *
					cpuid->nent);
		if (!cpuid_entries)
			goto out;
		r = -EFAULT;
		if (copy_from_user(cpuid_entries, entries,
				   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
			goto out;
	}
A
Avi Kivity 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	for (i = 0; i < cpuid->nent; i++) {
		vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
		vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
		vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
		vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
		vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
		vcpu->arch.cpuid_entries[i].index = 0;
		vcpu->arch.cpuid_entries[i].flags = 0;
		vcpu->arch.cpuid_entries[i].padding[0] = 0;
		vcpu->arch.cpuid_entries[i].padding[1] = 0;
		vcpu->arch.cpuid_entries[i].padding[2] = 0;
	}
	vcpu->arch.cpuid_nent = cpuid->nent;
	cpuid_fix_nx_cap(vcpu);
	kvm_apic_set_version(vcpu);
	kvm_x86_ops->cpuid_update(vcpu);
226
	r = kvm_update_cpuid(vcpu);
A
Avi Kivity 已提交
227 228

out:
229
	vfree(cpuid_entries);
A
Avi Kivity 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	return r;
}

int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
			      struct kvm_cpuid2 *cpuid,
			      struct kvm_cpuid_entry2 __user *entries)
{
	int r;

	r = -E2BIG;
	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
		goto out;
	r = -EFAULT;
	if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
			   cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
		goto out;
	vcpu->arch.cpuid_nent = cpuid->nent;
	kvm_apic_set_version(vcpu);
	kvm_x86_ops->cpuid_update(vcpu);
249
	r = kvm_update_cpuid(vcpu);
A
Avi Kivity 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
out:
	return r;
}

int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
			      struct kvm_cpuid2 *cpuid,
			      struct kvm_cpuid_entry2 __user *entries)
{
	int r;

	r = -E2BIG;
	if (cpuid->nent < vcpu->arch.cpuid_nent)
		goto out;
	r = -EFAULT;
	if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
			 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
		goto out;
	return 0;

out:
	cpuid->nent = vcpu->arch.cpuid_nent;
	return r;
}

static void cpuid_mask(u32 *word, int wordnum)
{
	*word &= boot_cpu_data.x86_capability[wordnum];
}

static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
			   u32 index)
{
	entry->function = function;
	entry->index = index;
	cpuid_count(entry->function, entry->index,
		    &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
	entry->flags = 0;
}

B
Borislav Petkov 已提交
289 290 291
static int __do_cpuid_ent_emulated(struct kvm_cpuid_entry2 *entry,
				   u32 func, u32 index, int *nent, int maxnent)
{
B
Borislav Petkov 已提交
292 293
	switch (func) {
	case 0:
P
Paolo Bonzini 已提交
294
		entry->eax = 7;
B
Borislav Petkov 已提交
295 296 297 298 299 300
		++*nent;
		break;
	case 1:
		entry->ecx = F(MOVBE);
		++*nent;
		break;
P
Paolo Bonzini 已提交
301 302 303 304 305
	case 7:
		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
		if (index == 0)
			entry->ecx = F(RDPID);
		++*nent;
B
Borislav Petkov 已提交
306 307 308 309 310 311 312
	default:
		break;
	}

	entry->function = func;
	entry->index = index;

B
Borislav Petkov 已提交
313 314 315 316 317
	return 0;
}

static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
				 u32 index, int *nent, int maxnent)
A
Avi Kivity 已提交
318
{
319
	int r;
A
Avi Kivity 已提交
320 321 322 323 324 325 326 327 328 329
	unsigned f_nx = is_efer_nx() ? F(NX) : 0;
#ifdef CONFIG_X86_64
	unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
				? F(GBPAGES) : 0;
	unsigned f_lm = F(LM);
#else
	unsigned f_gbpages = 0;
	unsigned f_lm = 0;
#endif
	unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
330
	unsigned f_invpcid = kvm_x86_ops->invpcid_supported() ? F(INVPCID) : 0;
331
	unsigned f_mpx = kvm_mpx_supported() ? F(MPX) : 0;
332
	unsigned f_xsaves = kvm_x86_ops->xsaves_supported() ? F(XSAVES) : 0;
333
	unsigned f_umip = kvm_x86_ops->umip_emulated() ? F(UMIP) : 0;
A
Avi Kivity 已提交
334 335

	/* cpuid 1.edx */
336
	const u32 kvm_cpuid_1_edx_x86_features =
A
Avi Kivity 已提交
337 338 339 340
		F(FPU) | F(VME) | F(DE) | F(PSE) |
		F(TSC) | F(MSR) | F(PAE) | F(MCE) |
		F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
		F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
341
		F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
A
Avi Kivity 已提交
342 343 344 345
		0 /* Reserved, DS, ACPI */ | F(MMX) |
		F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
		0 /* HTT, TM, Reserved, PBE */;
	/* cpuid 0x80000001.edx */
346
	const u32 kvm_cpuid_8000_0001_edx_x86_features =
A
Avi Kivity 已提交
347 348 349 350 351 352 353 354 355
		F(FPU) | F(VME) | F(DE) | F(PSE) |
		F(TSC) | F(MSR) | F(PAE) | F(MCE) |
		F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
		F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
		F(PAT) | F(PSE36) | 0 /* Reserved */ |
		f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
		F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
		0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
	/* cpuid 1.ecx */
356
	const u32 kvm_cpuid_1_ecx_x86_features =
357 358
		/* NOTE: MONITOR (and MWAIT) are emulated as NOP,
		 * but *not* advertised to guests via CPUID ! */
A
Avi Kivity 已提交
359 360 361
		F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
		0 /* DS-CPL, VMX, SMX, EST */ |
		0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
362
		F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
363
		F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
A
Avi Kivity 已提交
364 365 366 367
		F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
		0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
		F(F16C) | F(RDRAND);
	/* cpuid 0x80000001.ecx */
368
	const u32 kvm_cpuid_8000_0001_ecx_x86_features =
A
Avi Kivity 已提交
369 370
		F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
		F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
371
		F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
372 373
		0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
		F(TOPOEXT);
A
Avi Kivity 已提交
374

A
Ashok Raj 已提交
375 376
	/* cpuid 0x80000008.ebx */
	const u32 kvm_cpuid_8000_0008_ebx_x86_features =
377
		F(IBPB) | F(IBRS);
A
Ashok Raj 已提交
378

A
Avi Kivity 已提交
379
	/* cpuid 0xC0000001.edx */
380
	const u32 kvm_cpuid_C000_0001_edx_x86_features =
A
Avi Kivity 已提交
381 382 383 384 385
		F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
		F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
		F(PMM) | F(PMM_EN);

	/* cpuid 7.0.ebx */
386
	const u32 kvm_cpuid_7_0_ebx_x86_features =
387
		F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
388
		F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | f_mpx | F(RDSEED) |
389 390 391
		F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
		F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
		F(SHA_NI) | F(AVX512BW) | F(AVX512VL);
A
Avi Kivity 已提交
392

P
Paolo Bonzini 已提交
393
	/* cpuid 0xD.1.eax */
394
	const u32 kvm_cpuid_D_1_eax_x86_features =
395
		F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | f_xsaves;
P
Paolo Bonzini 已提交
396

397
	/* cpuid 7.0.ecx*/
398
	const u32 kvm_cpuid_7_0_ecx_x86_features =
P
Paolo Bonzini 已提交
399
		F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ |
Y
Yang Zhong 已提交
400 401
		F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
		F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG);
402

403 404
	/* cpuid 7.0.edx*/
	const u32 kvm_cpuid_7_0_edx_x86_features =
405 406
		F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
		F(ARCH_CAPABILITIES);
407

A
Avi Kivity 已提交
408 409
	/* all calls to cpuid_count() should be made on the same cpu */
	get_cpu();
410 411 412 413 414 415

	r = -E2BIG;

	if (*nent >= maxnent)
		goto out;

A
Avi Kivity 已提交
416 417 418 419 420 421 422 423
	do_cpuid_1_ent(entry, function, index);
	++*nent;

	switch (function) {
	case 0:
		entry->eax = min(entry->eax, (u32)0xd);
		break;
	case 1:
424 425 426 427
		entry->edx &= kvm_cpuid_1_edx_x86_features;
		cpuid_mask(&entry->edx, CPUID_1_EDX);
		entry->ecx &= kvm_cpuid_1_ecx_x86_features;
		cpuid_mask(&entry->ecx, CPUID_1_ECX);
A
Avi Kivity 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440
		/* we support x2apic emulation even if host does not support
		 * it since we emulate x2apic in software */
		entry->ecx |= F(X2APIC);
		break;
	/* function 2 entries are STATEFUL. That is, repeated cpuid commands
	 * may return different values. This forces us to get_cpu() before
	 * issuing the first command, and also to emulate this annoying behavior
	 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
	case 2: {
		int t, times = entry->eax & 0xff;

		entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
		entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
441 442 443 444
		for (t = 1; t < times; ++t) {
			if (*nent >= maxnent)
				goto out;

A
Avi Kivity 已提交
445 446 447 448 449 450 451 452 453 454 455 456
			do_cpuid_1_ent(&entry[t], function, 0);
			entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
			++*nent;
		}
		break;
	}
	/* function 4 has additional index. */
	case 4: {
		int i, cache_type;

		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
		/* read more entries until cache_type is zero */
457 458 459 460
		for (i = 1; ; ++i) {
			if (*nent >= maxnent)
				goto out;

A
Avi Kivity 已提交
461 462 463 464 465 466 467 468 469 470
			cache_type = entry[i - 1].eax & 0x1f;
			if (!cache_type)
				break;
			do_cpuid_1_ent(&entry[i], function, i);
			entry[i].flags |=
			       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
			++*nent;
		}
		break;
	}
J
Jan Kiszka 已提交
471 472 473 474 475 476
	case 6: /* Thermal management */
		entry->eax = 0x4; /* allow ARAT */
		entry->ebx = 0;
		entry->ecx = 0;
		entry->edx = 0;
		break;
A
Avi Kivity 已提交
477 478
	case 7: {
		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
G
Guo Chao 已提交
479
		/* Mask ebx against host capability word 9 */
A
Avi Kivity 已提交
480
		if (index == 0) {
481 482
			entry->ebx &= kvm_cpuid_7_0_ebx_x86_features;
			cpuid_mask(&entry->ebx, CPUID_7_0_EBX);
W
Will Auld 已提交
483 484
			// TSC_ADJUST is emulated
			entry->ebx |= F(TSC_ADJUST);
485 486
			entry->ecx &= kvm_cpuid_7_0_ecx_x86_features;
			cpuid_mask(&entry->ecx, CPUID_7_ECX);
487
			entry->ecx |= f_umip;
488
			/* PKU is not yet implemented for shadow paging. */
489
			if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
490
				entry->ecx &= ~F(PKU);
491
			entry->edx &= kvm_cpuid_7_0_edx_x86_features;
492
			cpuid_mask(&entry->edx, CPUID_7_EDX);
493
		} else {
A
Avi Kivity 已提交
494
			entry->ebx = 0;
495
			entry->ecx = 0;
496
			entry->edx = 0;
497
		}
A
Avi Kivity 已提交
498 499 500 501 502
		entry->eax = 0;
		break;
	}
	case 9:
		break;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	case 0xa: { /* Architectural Performance Monitoring */
		struct x86_pmu_capability cap;
		union cpuid10_eax eax;
		union cpuid10_edx edx;

		perf_get_x86_pmu_capability(&cap);

		/*
		 * Only support guest architectural pmu on a host
		 * with architectural pmu.
		 */
		if (!cap.version)
			memset(&cap, 0, sizeof(cap));

		eax.split.version_id = min(cap.version, 2);
		eax.split.num_counters = cap.num_counters_gp;
		eax.split.bit_width = cap.bit_width_gp;
		eax.split.mask_length = cap.events_mask_len;

		edx.split.num_counters_fixed = cap.num_counters_fixed;
		edx.split.bit_width_fixed = cap.bit_width_fixed;
		edx.split.reserved = 0;

		entry->eax = eax.full;
		entry->ebx = cap.events_mask;
		entry->ecx = 0;
		entry->edx = edx.full;
		break;
	}
A
Avi Kivity 已提交
532 533 534 535 536 537
	/* function 0xb has additional index. */
	case 0xb: {
		int i, level_type;

		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
		/* read more entries until level_type is zero */
538 539 540 541
		for (i = 1; ; ++i) {
			if (*nent >= maxnent)
				goto out;

A
Avi Kivity 已提交
542 543 544 545 546 547 548 549 550 551 552 553
			level_type = entry[i - 1].ecx & 0xff00;
			if (!level_type)
				break;
			do_cpuid_1_ent(&entry[i], function, i);
			entry[i].flags |=
			       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
			++*nent;
		}
		break;
	}
	case 0xd: {
		int idx, i;
554
		u64 supported = kvm_supported_xcr0();
A
Avi Kivity 已提交
555

556
		entry->eax &= supported;
557 558
		entry->ebx = xstate_required_size(supported, false);
		entry->ecx = entry->ebx;
559
		entry->edx &= supported >> 32;
A
Avi Kivity 已提交
560
		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
P
Paolo Bonzini 已提交
561 562 563
		if (!supported)
			break;

564
		for (idx = 1, i = 1; idx < 64; ++idx) {
565
			u64 mask = ((u64)1 << idx);
566 567 568
			if (*nent >= maxnent)
				goto out;

A
Avi Kivity 已提交
569
			do_cpuid_1_ent(&entry[i], function, idx);
570
			if (idx == 1) {
571
				entry[i].eax &= kvm_cpuid_D_1_eax_x86_features;
572
				cpuid_mask(&entry[i].eax, CPUID_D_1_EAX);
573 574 575 576 577
				entry[i].ebx = 0;
				if (entry[i].eax & (F(XSAVES)|F(XSAVEC)))
					entry[i].ebx =
						xstate_required_size(supported,
								     true);
578 579 580 581 582 583 584 585
			} else {
				if (entry[i].eax == 0 || !(supported & mask))
					continue;
				if (WARN_ON_ONCE(entry[i].ecx & 1))
					continue;
			}
			entry[i].ecx = 0;
			entry[i].edx = 0;
A
Avi Kivity 已提交
586 587 588 589 590 591 592 593
			entry[i].flags |=
			       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
			++*nent;
			++i;
		}
		break;
	}
	case KVM_CPUID_SIGNATURE: {
594 595
		static const char signature[12] = "KVMKVMKVM\0\0";
		const u32 *sigptr = (const u32 *)signature;
596
		entry->eax = KVM_CPUID_FEATURES;
A
Avi Kivity 已提交
597 598 599 600 601 602 603 604 605 606
		entry->ebx = sigptr[0];
		entry->ecx = sigptr[1];
		entry->edx = sigptr[2];
		break;
	}
	case KVM_CPUID_FEATURES:
		entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
			     (1 << KVM_FEATURE_NOP_IO_DELAY) |
			     (1 << KVM_FEATURE_CLOCKSOURCE2) |
			     (1 << KVM_FEATURE_ASYNC_PF) |
607
			     (1 << KVM_FEATURE_PV_EOI) |
608
			     (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
609
			     (1 << KVM_FEATURE_PV_UNHALT) |
610 611
			     (1 << KVM_FEATURE_PV_TLB_FLUSH) |
			     (1 << KVM_FEATURE_ASYNC_PF_VMEXIT);
A
Avi Kivity 已提交
612 613 614 615 616 617 618 619 620

		if (sched_info_on())
			entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);

		entry->ebx = 0;
		entry->ecx = 0;
		entry->edx = 0;
		break;
	case 0x80000000:
621
		entry->eax = min(entry->eax, 0x8000001f);
A
Avi Kivity 已提交
622 623
		break;
	case 0x80000001:
624 625 626 627
		entry->edx &= kvm_cpuid_8000_0001_edx_x86_features;
		cpuid_mask(&entry->edx, CPUID_8000_0001_EDX);
		entry->ecx &= kvm_cpuid_8000_0001_ecx_x86_features;
		cpuid_mask(&entry->ecx, CPUID_8000_0001_ECX);
A
Avi Kivity 已提交
628
		break;
629 630 631 632 633 634 635
	case 0x80000007: /* Advanced power management */
		/* invariant TSC is CPUID.80000007H:EDX[8] */
		entry->edx &= (1 << 8);
		/* mask against host */
		entry->edx &= boot_cpu_data.x86_power;
		entry->eax = entry->ebx = entry->ecx = 0;
		break;
A
Avi Kivity 已提交
636 637 638 639 640 641 642 643
	case 0x80000008: {
		unsigned g_phys_as = (entry->eax >> 16) & 0xff;
		unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
		unsigned phys_as = entry->eax & 0xff;

		if (!g_phys_as)
			g_phys_as = phys_as;
		entry->eax = g_phys_as | (virt_as << 8);
A
Ashok Raj 已提交
644
		entry->edx = 0;
645
		/* IBRS and IBPB aren't necessarily present in hardware cpuid */
A
Ashok Raj 已提交
646 647
		if (boot_cpu_has(X86_FEATURE_IBPB))
			entry->ebx |= F(IBPB);
648 649
		if (boot_cpu_has(X86_FEATURE_IBRS))
			entry->ebx |= F(IBRS);
A
Ashok Raj 已提交
650 651
		entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features;
		cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
A
Avi Kivity 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
		break;
	}
	case 0x80000019:
		entry->ecx = entry->edx = 0;
		break;
	case 0x8000001a:
		break;
	case 0x8000001d:
		break;
	/*Add support for Centaur's CPUID instruction*/
	case 0xC0000000:
		/*Just support up to 0xC0000004 now*/
		entry->eax = min(entry->eax, 0xC0000004);
		break;
	case 0xC0000001:
667 668
		entry->edx &= kvm_cpuid_C000_0001_edx_x86_features;
		cpuid_mask(&entry->edx, CPUID_C000_0001_EDX);
A
Avi Kivity 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681
		break;
	case 3: /* Processor serial number */
	case 5: /* MONITOR/MWAIT */
	case 0xC0000002:
	case 0xC0000003:
	case 0xC0000004:
	default:
		entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
		break;
	}

	kvm_x86_ops->set_supported_cpuid(function, entry);

682 683 684
	r = 0;

out:
A
Avi Kivity 已提交
685
	put_cpu();
686 687

	return r;
A
Avi Kivity 已提交
688 689
}

B
Borislav Petkov 已提交
690 691 692 693 694 695 696 697 698
static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 func,
			u32 idx, int *nent, int maxnent, unsigned int type)
{
	if (type == KVM_GET_EMULATED_CPUID)
		return __do_cpuid_ent_emulated(entry, func, idx, nent, maxnent);

	return __do_cpuid_ent(entry, func, idx, nent, maxnent);
}

A
Avi Kivity 已提交
699 700
#undef F

701 702 703 704
struct kvm_cpuid_param {
	u32 func;
	u32 idx;
	bool has_leaf_count;
705
	bool (*qualifier)(const struct kvm_cpuid_param *param);
706 707
};

708
static bool is_centaur_cpu(const struct kvm_cpuid_param *param)
709 710 711 712
{
	return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR;
}

B
Borislav Petkov 已提交
713 714 715 716
static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
				 __u32 num_entries, unsigned int ioctl_type)
{
	int i;
B
Borislav Petkov 已提交
717
	__u32 pad[3];
B
Borislav Petkov 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730

	if (ioctl_type != KVM_GET_EMULATED_CPUID)
		return false;

	/*
	 * We want to make sure that ->padding is being passed clean from
	 * userspace in case we want to use it for something in the future.
	 *
	 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
	 * have to give ourselves satisfied only with the emulated side. /me
	 * sheds a tear.
	 */
	for (i = 0; i < num_entries; i++) {
B
Borislav Petkov 已提交
731 732 733 734
		if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
			return true;

		if (pad[0] || pad[1] || pad[2])
B
Borislav Petkov 已提交
735 736 737 738 739 740 741 742
			return true;
	}
	return false;
}

int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
			    struct kvm_cpuid_entry2 __user *entries,
			    unsigned int type)
A
Avi Kivity 已提交
743 744
{
	struct kvm_cpuid_entry2 *cpuid_entries;
745
	int limit, nent = 0, r = -E2BIG, i;
A
Avi Kivity 已提交
746
	u32 func;
747
	static const struct kvm_cpuid_param param[] = {
748 749 750 751 752 753
		{ .func = 0, .has_leaf_count = true },
		{ .func = 0x80000000, .has_leaf_count = true },
		{ .func = 0xC0000000, .qualifier = is_centaur_cpu, .has_leaf_count = true },
		{ .func = KVM_CPUID_SIGNATURE },
		{ .func = KVM_CPUID_FEATURES },
	};
A
Avi Kivity 已提交
754 755 756 757 758

	if (cpuid->nent < 1)
		goto out;
	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
		cpuid->nent = KVM_MAX_CPUID_ENTRIES;
B
Borislav Petkov 已提交
759 760 761 762

	if (sanity_check_entries(entries, cpuid->nent, type))
		return -EINVAL;

A
Avi Kivity 已提交
763
	r = -ENOMEM;
B
Borislav Petkov 已提交
764
	cpuid_entries = vzalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
A
Avi Kivity 已提交
765 766 767
	if (!cpuid_entries)
		goto out;

768 769
	r = 0;
	for (i = 0; i < ARRAY_SIZE(param); i++) {
770
		const struct kvm_cpuid_param *ent = &param[i];
A
Avi Kivity 已提交
771

772 773
		if (ent->qualifier && !ent->qualifier(ent))
			continue;
A
Avi Kivity 已提交
774

775
		r = do_cpuid_ent(&cpuid_entries[nent], ent->func, ent->idx,
B
Borislav Petkov 已提交
776
				&nent, cpuid->nent, type);
A
Avi Kivity 已提交
777

778
		if (r)
A
Avi Kivity 已提交
779 780
			goto out_free;

781 782 783
		if (!ent->has_leaf_count)
			continue;

A
Avi Kivity 已提交
784
		limit = cpuid_entries[nent - 1].eax;
785 786
		for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func)
			r = do_cpuid_ent(&cpuid_entries[nent], func, ent->idx,
B
Borislav Petkov 已提交
787
				     &nent, cpuid->nent, type);
A
Avi Kivity 已提交
788

789
		if (r)
A
Avi Kivity 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
			goto out_free;
	}

	r = -EFAULT;
	if (copy_to_user(entries, cpuid_entries,
			 nent * sizeof(struct kvm_cpuid_entry2)))
		goto out_free;
	cpuid->nent = nent;
	r = 0;

out_free:
	vfree(cpuid_entries);
out:
	return r;
}

static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
{
	struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
809 810 811
	struct kvm_cpuid_entry2 *ej;
	int j = i;
	int nent = vcpu->arch.cpuid_nent;
A
Avi Kivity 已提交
812 813 814

	e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
	/* when no next entry is found, the current entry[i] is reselected */
815 816 817 818 819 820 821 822
	do {
		j = (j + 1) % nent;
		ej = &vcpu->arch.cpuid_entries[j];
	} while (ej->function != e->function);

	ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;

	return j;
A
Avi Kivity 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
}

/* find an entry with matching function, matching index (if needed), and that
 * should be read next (if it's stateful) */
static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
	u32 function, u32 index)
{
	if (e->function != function)
		return 0;
	if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
		return 0;
	if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
	    !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
		return 0;
	return 1;
}

struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
					      u32 function, u32 index)
{
	int i;
	struct kvm_cpuid_entry2 *best = NULL;

	for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
		struct kvm_cpuid_entry2 *e;

		e = &vcpu->arch.cpuid_entries[i];
		if (is_matching_cpuid_entry(e, function, index)) {
			if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
				move_to_next_stateful_cpuid_entry(vcpu, i);
			best = e;
			break;
		}
	}
	return best;
}
EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);

/*
 * If no match is found, check whether we exceed the vCPU's limit
 * and return the content of the highest valid _standard_ leaf instead.
 * This is to satisfy the CPUID specification.
 */
static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
                                                  u32 function, u32 index)
{
	struct kvm_cpuid_entry2 *maxlevel;

	maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
	if (!maxlevel || maxlevel->eax >= function)
		return NULL;
	if (function & 0x80000000) {
		maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
		if (!maxlevel)
			return NULL;
	}
	return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
}

882 883
bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
	       u32 *ecx, u32 *edx, bool check_limit)
A
Avi Kivity 已提交
884
{
885
	u32 function = *eax, index = *ecx;
A
Avi Kivity 已提交
886
	struct kvm_cpuid_entry2 *best;
887
	bool entry_found = true;
A
Avi Kivity 已提交
888 889 890

	best = kvm_find_cpuid_entry(vcpu, function, index);

891 892 893 894 895
	if (!best) {
		entry_found = false;
		if (!check_limit)
			goto out;

A
Avi Kivity 已提交
896
		best = check_cpuid_limit(vcpu, function, index);
897
	}
A
Avi Kivity 已提交
898

899
out:
A
Avi Kivity 已提交
900
	if (best) {
901 902 903 904 905 906
		*eax = best->eax;
		*ebx = best->ebx;
		*ecx = best->ecx;
		*edx = best->edx;
	} else
		*eax = *ebx = *ecx = *edx = 0;
907 908
	trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx, entry_found);
	return entry_found;
909
}
910
EXPORT_SYMBOL_GPL(kvm_cpuid);
911

912
int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
913
{
914
	u32 eax, ebx, ecx, edx;
915

K
Kyle Huey 已提交
916 917 918
	if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
		return 1;

919
	eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
920
	ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
921
	kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, true);
922 923 924 925
	kvm_register_write(vcpu, VCPU_REGS_RAX, eax);
	kvm_register_write(vcpu, VCPU_REGS_RBX, ebx);
	kvm_register_write(vcpu, VCPU_REGS_RCX, ecx);
	kvm_register_write(vcpu, VCPU_REGS_RDX, edx);
926
	return kvm_skip_emulated_instruction(vcpu);
A
Avi Kivity 已提交
927 928
}
EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);