blob: 11f09a2b31a06db8de2365fb3e872568a43f265f [file] [log] [blame]
Avi Kivity00b27a32011-11-23 16:30:32 +02001/*
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>
Jan Kiszkabb5a7982011-12-14 17:58:18 +010017#include <linux/vmalloc.h>
18#include <linux/uaccess.h>
Avi Kivity00b27a32011-11-23 16:30:32 +020019#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
Paolo Bonzini4344ee92013-10-02 16:06:16 +020026static u32 xstate_required_size(u64 xstate_bv)
27{
28 int feature_bit = 0;
29 u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
30
Liu, Jinsong56c103e2014-02-21 17:39:02 +000031 xstate_bv &= XSTATE_EXTEND_MASK;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020032 while (xstate_bv) {
33 if (xstate_bv & 0x1) {
34 u32 eax, ebx, ecx, edx;
35 cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
36 ret = max(ret, eax + ebx);
37 }
38
39 xstate_bv >>= 1;
40 feature_bit++;
41 }
42
43 return ret;
44}
45
Paolo Bonzini4ff41732014-02-24 12:15:16 +010046u64 kvm_supported_xcr0(void)
47{
48 u64 xcr0 = KVM_SUPPORTED_XCR0 & host_xcr0;
49
Paolo Bonzini93c4adc2014-03-05 23:19:52 +010050 if (!kvm_x86_ops->mpx_supported())
Paolo Bonzini4ff41732014-02-24 12:15:16 +010051 xcr0 &= ~(XSTATE_BNDREGS | XSTATE_BNDCSR);
52
53 return xcr0;
54}
55
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010056#define F(x) bit(X86_FEATURE_##x)
57
Nadav Amitdd598092014-09-16 15:10:03 +030058int kvm_update_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity00b27a32011-11-23 16:30:32 +020059{
60 struct kvm_cpuid_entry2 *best;
61 struct kvm_lapic *apic = vcpu->arch.apic;
62
63 best = kvm_find_cpuid_entry(vcpu, 1, 0);
64 if (!best)
Nadav Amitdd598092014-09-16 15:10:03 +030065 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +020066
67 /* Update OSXSAVE bit */
68 if (cpu_has_xsave && best->function == 0x1) {
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010069 best->ecx &= ~F(OSXSAVE);
Avi Kivity00b27a32011-11-23 16:30:32 +020070 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010071 best->ecx |= F(OSXSAVE);
Avi Kivity00b27a32011-11-23 16:30:32 +020072 }
73
74 if (apic) {
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010075 if (best->ecx & F(TSC_DEADLINE_TIMER))
Avi Kivity00b27a32011-11-23 16:30:32 +020076 apic->lapic_timer.timer_mode_mask = 3 << 17;
77 else
78 apic->lapic_timer.timer_mode_mask = 1 << 17;
79 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020080
Paolo Bonzinid7876f12013-10-02 16:06:15 +020081 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020082 if (!best) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020083 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020084 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
85 } else {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020086 vcpu->arch.guest_supported_xcr0 =
87 (best->eax | ((u64)best->edx << 32)) &
Paolo Bonzini4ff41732014-02-24 12:15:16 +010088 kvm_supported_xcr0();
Liu, Jinsong56c103e2014-02-21 17:39:02 +000089 vcpu->arch.guest_xstate_size = best->ebx =
90 xstate_required_size(vcpu->arch.xcr0);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020091 }
Paolo Bonzinid7876f12013-10-02 16:06:15 +020092
Nadav Amitdd598092014-09-16 15:10:03 +030093 /*
94 * The existing code assumes virtual address is 48-bit in the canonical
95 * address checks; exit if it is ever changed.
96 */
97 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
98 if (best && ((best->eax & 0xff00) >> 8) != 48 &&
99 ((best->eax & 0xff00) >> 8) != 0)
100 return -EINVAL;
101
Gleb Natapovf5132b02011-11-10 14:57:22 +0200102 kvm_pmu_cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300103 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200104}
105
106static int is_efer_nx(void)
107{
108 unsigned long long efer = 0;
109
110 rdmsrl_safe(MSR_EFER, &efer);
111 return efer & EFER_NX;
112}
113
114static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
115{
116 int i;
117 struct kvm_cpuid_entry2 *e, *entry;
118
119 entry = NULL;
120 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
121 e = &vcpu->arch.cpuid_entries[i];
122 if (e->function == 0x80000001) {
123 entry = e;
124 break;
125 }
126 }
Paolo Bonzini5c404ca2014-12-03 14:34:47 +0100127 if (entry && (entry->edx & F(NX)) && !is_efer_nx()) {
128 entry->edx &= ~F(NX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200129 printk(KERN_INFO "kvm: guest NX capability removed\n");
130 }
131}
132
133/* when an old userspace process fills a new kernel module */
134int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
135 struct kvm_cpuid *cpuid,
136 struct kvm_cpuid_entry __user *entries)
137{
138 int r, i;
139 struct kvm_cpuid_entry *cpuid_entries;
140
141 r = -E2BIG;
142 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
143 goto out;
144 r = -ENOMEM;
145 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
146 if (!cpuid_entries)
147 goto out;
148 r = -EFAULT;
149 if (copy_from_user(cpuid_entries, entries,
150 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
151 goto out_free;
152 for (i = 0; i < cpuid->nent; i++) {
153 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
154 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
155 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
156 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
157 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
158 vcpu->arch.cpuid_entries[i].index = 0;
159 vcpu->arch.cpuid_entries[i].flags = 0;
160 vcpu->arch.cpuid_entries[i].padding[0] = 0;
161 vcpu->arch.cpuid_entries[i].padding[1] = 0;
162 vcpu->arch.cpuid_entries[i].padding[2] = 0;
163 }
164 vcpu->arch.cpuid_nent = cpuid->nent;
165 cpuid_fix_nx_cap(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200166 kvm_apic_set_version(vcpu);
167 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300168 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200169
170out_free:
171 vfree(cpuid_entries);
172out:
173 return r;
174}
175
176int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
177 struct kvm_cpuid2 *cpuid,
178 struct kvm_cpuid_entry2 __user *entries)
179{
180 int r;
181
182 r = -E2BIG;
183 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
184 goto out;
185 r = -EFAULT;
186 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
187 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
188 goto out;
189 vcpu->arch.cpuid_nent = cpuid->nent;
190 kvm_apic_set_version(vcpu);
191 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300192 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200193out:
194 return r;
195}
196
197int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
198 struct kvm_cpuid2 *cpuid,
199 struct kvm_cpuid_entry2 __user *entries)
200{
201 int r;
202
203 r = -E2BIG;
204 if (cpuid->nent < vcpu->arch.cpuid_nent)
205 goto out;
206 r = -EFAULT;
207 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
208 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
209 goto out;
210 return 0;
211
212out:
213 cpuid->nent = vcpu->arch.cpuid_nent;
214 return r;
215}
216
217static void cpuid_mask(u32 *word, int wordnum)
218{
219 *word &= boot_cpu_data.x86_capability[wordnum];
220}
221
222static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
223 u32 index)
224{
225 entry->function = function;
226 entry->index = index;
227 cpuid_count(entry->function, entry->index,
228 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
229 entry->flags = 0;
230}
231
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200232static int __do_cpuid_ent_emulated(struct kvm_cpuid_entry2 *entry,
233 u32 func, u32 index, int *nent, int maxnent)
234{
Borislav Petkov84cffe42013-10-29 12:54:56 +0100235 switch (func) {
236 case 0:
237 entry->eax = 1; /* only one leaf currently */
238 ++*nent;
239 break;
240 case 1:
241 entry->ecx = F(MOVBE);
242 ++*nent;
243 break;
244 default:
245 break;
246 }
247
248 entry->function = func;
249 entry->index = index;
250
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200251 return 0;
252}
253
254static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
255 u32 index, int *nent, int maxnent)
Avi Kivity00b27a32011-11-23 16:30:32 +0200256{
Sasha Levin831bf662011-11-28 11:20:29 +0200257 int r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200258 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
259#ifdef CONFIG_X86_64
260 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
261 ? F(GBPAGES) : 0;
262 unsigned f_lm = F(LM);
263#else
264 unsigned f_gbpages = 0;
265 unsigned f_lm = 0;
266#endif
267 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Mao, Junjiead756a12012-07-02 01:18:48 +0000268 unsigned f_invpcid = kvm_x86_ops->invpcid_supported() ? F(INVPCID) : 0;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +0100269 unsigned f_mpx = kvm_x86_ops->mpx_supported() ? F(MPX) : 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200270
271 /* cpuid 1.edx */
272 const u32 kvm_supported_word0_x86_features =
273 F(FPU) | F(VME) | F(DE) | F(PSE) |
274 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
275 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
276 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
H. Peter Anvin840d2832014-02-27 08:31:30 -0800277 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
Avi Kivity00b27a32011-11-23 16:30:32 +0200278 0 /* Reserved, DS, ACPI */ | F(MMX) |
279 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
280 0 /* HTT, TM, Reserved, PBE */;
281 /* cpuid 0x80000001.edx */
282 const u32 kvm_supported_word1_x86_features =
283 F(FPU) | F(VME) | F(DE) | F(PSE) |
284 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
285 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
286 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
287 F(PAT) | F(PSE36) | 0 /* Reserved */ |
288 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
289 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
290 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
291 /* cpuid 1.ecx */
292 const u32 kvm_supported_word4_x86_features =
Gabriel L. Somlo87c00572014-05-07 16:52:13 -0400293 /* NOTE: MONITOR (and MWAIT) are emulated as NOP,
294 * but *not* advertised to guests via CPUID ! */
Avi Kivity00b27a32011-11-23 16:30:32 +0200295 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
296 0 /* DS-CPL, VMX, SMX, EST */ |
297 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
Liu, Jinsongfb215362011-11-28 03:55:19 -0800298 F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
Mao, Junjiead756a12012-07-02 01:18:48 +0000299 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
Avi Kivity00b27a32011-11-23 16:30:32 +0200300 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
301 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
302 F(F16C) | F(RDRAND);
303 /* cpuid 0x80000001.ecx */
304 const u32 kvm_supported_word6_x86_features =
305 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
306 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500307 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
Avi Kivity00b27a32011-11-23 16:30:32 +0200308 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM);
309
310 /* cpuid 0xC0000001.edx */
311 const u32 kvm_supported_word5_x86_features =
312 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
313 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
314 F(PMM) | F(PMM_EN);
315
316 /* cpuid 7.0.ebx */
317 const u32 kvm_supported_word9_x86_features =
Liu, Jinsong83c52912012-02-28 05:15:46 +0000318 F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
Liu, Jinsong390bd522014-02-24 10:58:09 +0000319 F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | f_mpx | F(RDSEED) |
Chao Peng612263b2014-10-22 17:35:24 +0800320 F(ADX) | F(SMAP) | F(AVX512F) | F(AVX512PF) | F(AVX512ER) |
321 F(AVX512CD);
Avi Kivity00b27a32011-11-23 16:30:32 +0200322
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100323 /* cpuid 0xD.1.eax */
324 const u32 kvm_supported_word10_x86_features =
325 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1);
326
Avi Kivity00b27a32011-11-23 16:30:32 +0200327 /* all calls to cpuid_count() should be made on the same cpu */
328 get_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200329
330 r = -E2BIG;
331
332 if (*nent >= maxnent)
333 goto out;
334
Avi Kivity00b27a32011-11-23 16:30:32 +0200335 do_cpuid_1_ent(entry, function, index);
336 ++*nent;
337
338 switch (function) {
339 case 0:
340 entry->eax = min(entry->eax, (u32)0xd);
341 break;
342 case 1:
343 entry->edx &= kvm_supported_word0_x86_features;
344 cpuid_mask(&entry->edx, 0);
345 entry->ecx &= kvm_supported_word4_x86_features;
346 cpuid_mask(&entry->ecx, 4);
347 /* we support x2apic emulation even if host does not support
348 * it since we emulate x2apic in software */
349 entry->ecx |= F(X2APIC);
350 break;
351 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
352 * may return different values. This forces us to get_cpu() before
353 * issuing the first command, and also to emulate this annoying behavior
354 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
355 case 2: {
356 int t, times = entry->eax & 0xff;
357
358 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
359 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Sasha Levin831bf662011-11-28 11:20:29 +0200360 for (t = 1; t < times; ++t) {
361 if (*nent >= maxnent)
362 goto out;
363
Avi Kivity00b27a32011-11-23 16:30:32 +0200364 do_cpuid_1_ent(&entry[t], function, 0);
365 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
366 ++*nent;
367 }
368 break;
369 }
370 /* function 4 has additional index. */
371 case 4: {
372 int i, cache_type;
373
374 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
375 /* read more entries until cache_type is zero */
Sasha Levin831bf662011-11-28 11:20:29 +0200376 for (i = 1; ; ++i) {
377 if (*nent >= maxnent)
378 goto out;
379
Avi Kivity00b27a32011-11-23 16:30:32 +0200380 cache_type = entry[i - 1].eax & 0x1f;
381 if (!cache_type)
382 break;
383 do_cpuid_1_ent(&entry[i], function, i);
384 entry[i].flags |=
385 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
386 ++*nent;
387 }
388 break;
389 }
390 case 7: {
391 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Guo Chaobbbda792012-06-28 15:20:58 +0800392 /* Mask ebx against host capability word 9 */
Avi Kivity00b27a32011-11-23 16:30:32 +0200393 if (index == 0) {
394 entry->ebx &= kvm_supported_word9_x86_features;
395 cpuid_mask(&entry->ebx, 9);
Will Auldba904632012-11-29 12:42:50 -0800396 // TSC_ADJUST is emulated
397 entry->ebx |= F(TSC_ADJUST);
Avi Kivity00b27a32011-11-23 16:30:32 +0200398 } else
399 entry->ebx = 0;
400 entry->eax = 0;
401 entry->ecx = 0;
402 entry->edx = 0;
403 break;
404 }
405 case 9:
406 break;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200407 case 0xa: { /* Architectural Performance Monitoring */
408 struct x86_pmu_capability cap;
409 union cpuid10_eax eax;
410 union cpuid10_edx edx;
411
412 perf_get_x86_pmu_capability(&cap);
413
414 /*
415 * Only support guest architectural pmu on a host
416 * with architectural pmu.
417 */
418 if (!cap.version)
419 memset(&cap, 0, sizeof(cap));
420
421 eax.split.version_id = min(cap.version, 2);
422 eax.split.num_counters = cap.num_counters_gp;
423 eax.split.bit_width = cap.bit_width_gp;
424 eax.split.mask_length = cap.events_mask_len;
425
426 edx.split.num_counters_fixed = cap.num_counters_fixed;
427 edx.split.bit_width_fixed = cap.bit_width_fixed;
428 edx.split.reserved = 0;
429
430 entry->eax = eax.full;
431 entry->ebx = cap.events_mask;
432 entry->ecx = 0;
433 entry->edx = edx.full;
434 break;
435 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200436 /* function 0xb has additional index. */
437 case 0xb: {
438 int i, level_type;
439
440 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
441 /* read more entries until level_type is zero */
Sasha Levin831bf662011-11-28 11:20:29 +0200442 for (i = 1; ; ++i) {
443 if (*nent >= maxnent)
444 goto out;
445
Avi Kivity00b27a32011-11-23 16:30:32 +0200446 level_type = entry[i - 1].ecx & 0xff00;
447 if (!level_type)
448 break;
449 do_cpuid_1_ent(&entry[i], function, i);
450 entry[i].flags |=
451 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
452 ++*nent;
453 }
454 break;
455 }
456 case 0xd: {
457 int idx, i;
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100458 u64 supported = kvm_supported_xcr0();
Avi Kivity00b27a32011-11-23 16:30:32 +0200459
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100460 entry->eax &= supported;
461 entry->edx &= supported >> 32;
Avi Kivity00b27a32011-11-23 16:30:32 +0200462 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100463 if (!supported)
464 break;
465
Sasha Levin831bf662011-11-28 11:20:29 +0200466 for (idx = 1, i = 1; idx < 64; ++idx) {
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100467 u64 mask = ((u64)1 << idx);
Sasha Levin831bf662011-11-28 11:20:29 +0200468 if (*nent >= maxnent)
469 goto out;
470
Avi Kivity00b27a32011-11-23 16:30:32 +0200471 do_cpuid_1_ent(&entry[i], function, idx);
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100472 if (idx == 1)
473 entry[i].eax &= kvm_supported_word10_x86_features;
474 else if (entry[i].eax == 0 || !(supported & mask))
Avi Kivity00b27a32011-11-23 16:30:32 +0200475 continue;
476 entry[i].flags |=
477 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
478 ++*nent;
479 ++i;
480 }
481 break;
482 }
483 case KVM_CPUID_SIGNATURE: {
Mathias Krause326d07c2012-08-30 01:30:13 +0200484 static const char signature[12] = "KVMKVMKVM\0\0";
485 const u32 *sigptr = (const u32 *)signature;
Michael S. Tsirkin57c22e52012-05-02 17:55:56 +0300486 entry->eax = KVM_CPUID_FEATURES;
Avi Kivity00b27a32011-11-23 16:30:32 +0200487 entry->ebx = sigptr[0];
488 entry->ecx = sigptr[1];
489 entry->edx = sigptr[2];
490 break;
491 }
492 case KVM_CPUID_FEATURES:
493 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
494 (1 << KVM_FEATURE_NOP_IO_DELAY) |
495 (1 << KVM_FEATURE_CLOCKSOURCE2) |
496 (1 << KVM_FEATURE_ASYNC_PF) |
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300497 (1 << KVM_FEATURE_PV_EOI) |
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +0530498 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
499 (1 << KVM_FEATURE_PV_UNHALT);
Avi Kivity00b27a32011-11-23 16:30:32 +0200500
501 if (sched_info_on())
502 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
503
504 entry->ebx = 0;
505 entry->ecx = 0;
506 entry->edx = 0;
507 break;
508 case 0x80000000:
509 entry->eax = min(entry->eax, 0x8000001a);
510 break;
511 case 0x80000001:
512 entry->edx &= kvm_supported_word1_x86_features;
513 cpuid_mask(&entry->edx, 1);
514 entry->ecx &= kvm_supported_word6_x86_features;
515 cpuid_mask(&entry->ecx, 6);
516 break;
Marcelo Tosattie4c9a5a12014-04-26 22:30:23 -0300517 case 0x80000007: /* Advanced power management */
518 /* invariant TSC is CPUID.80000007H:EDX[8] */
519 entry->edx &= (1 << 8);
520 /* mask against host */
521 entry->edx &= boot_cpu_data.x86_power;
522 entry->eax = entry->ebx = entry->ecx = 0;
523 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200524 case 0x80000008: {
525 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
526 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
527 unsigned phys_as = entry->eax & 0xff;
528
529 if (!g_phys_as)
530 g_phys_as = phys_as;
531 entry->eax = g_phys_as | (virt_as << 8);
532 entry->ebx = entry->edx = 0;
533 break;
534 }
535 case 0x80000019:
536 entry->ecx = entry->edx = 0;
537 break;
538 case 0x8000001a:
539 break;
540 case 0x8000001d:
541 break;
542 /*Add support for Centaur's CPUID instruction*/
543 case 0xC0000000:
544 /*Just support up to 0xC0000004 now*/
545 entry->eax = min(entry->eax, 0xC0000004);
546 break;
547 case 0xC0000001:
548 entry->edx &= kvm_supported_word5_x86_features;
549 cpuid_mask(&entry->edx, 5);
550 break;
551 case 3: /* Processor serial number */
552 case 5: /* MONITOR/MWAIT */
553 case 6: /* Thermal management */
Avi Kivity00b27a32011-11-23 16:30:32 +0200554 case 0xC0000002:
555 case 0xC0000003:
556 case 0xC0000004:
557 default:
558 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
559 break;
560 }
561
562 kvm_x86_ops->set_supported_cpuid(function, entry);
563
Sasha Levin831bf662011-11-28 11:20:29 +0200564 r = 0;
565
566out:
Avi Kivity00b27a32011-11-23 16:30:32 +0200567 put_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200568
569 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200570}
571
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200572static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 func,
573 u32 idx, int *nent, int maxnent, unsigned int type)
574{
575 if (type == KVM_GET_EMULATED_CPUID)
576 return __do_cpuid_ent_emulated(entry, func, idx, nent, maxnent);
577
578 return __do_cpuid_ent(entry, func, idx, nent, maxnent);
579}
580
Avi Kivity00b27a32011-11-23 16:30:32 +0200581#undef F
582
Sasha Levin831bf662011-11-28 11:20:29 +0200583struct kvm_cpuid_param {
584 u32 func;
585 u32 idx;
586 bool has_leaf_count;
Mathias Krause326d07c2012-08-30 01:30:13 +0200587 bool (*qualifier)(const struct kvm_cpuid_param *param);
Sasha Levin831bf662011-11-28 11:20:29 +0200588};
589
Mathias Krause326d07c2012-08-30 01:30:13 +0200590static bool is_centaur_cpu(const struct kvm_cpuid_param *param)
Sasha Levin831bf662011-11-28 11:20:29 +0200591{
592 return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR;
593}
594
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200595static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
596 __u32 num_entries, unsigned int ioctl_type)
597{
598 int i;
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100599 __u32 pad[3];
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200600
601 if (ioctl_type != KVM_GET_EMULATED_CPUID)
602 return false;
603
604 /*
605 * We want to make sure that ->padding is being passed clean from
606 * userspace in case we want to use it for something in the future.
607 *
608 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
609 * have to give ourselves satisfied only with the emulated side. /me
610 * sheds a tear.
611 */
612 for (i = 0; i < num_entries; i++) {
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100613 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
614 return true;
615
616 if (pad[0] || pad[1] || pad[2])
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200617 return true;
618 }
619 return false;
620}
621
622int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
623 struct kvm_cpuid_entry2 __user *entries,
624 unsigned int type)
Avi Kivity00b27a32011-11-23 16:30:32 +0200625{
626 struct kvm_cpuid_entry2 *cpuid_entries;
Sasha Levin831bf662011-11-28 11:20:29 +0200627 int limit, nent = 0, r = -E2BIG, i;
Avi Kivity00b27a32011-11-23 16:30:32 +0200628 u32 func;
Mathias Krause326d07c2012-08-30 01:30:13 +0200629 static const struct kvm_cpuid_param param[] = {
Sasha Levin831bf662011-11-28 11:20:29 +0200630 { .func = 0, .has_leaf_count = true },
631 { .func = 0x80000000, .has_leaf_count = true },
632 { .func = 0xC0000000, .qualifier = is_centaur_cpu, .has_leaf_count = true },
633 { .func = KVM_CPUID_SIGNATURE },
634 { .func = KVM_CPUID_FEATURES },
635 };
Avi Kivity00b27a32011-11-23 16:30:32 +0200636
637 if (cpuid->nent < 1)
638 goto out;
639 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
640 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200641
642 if (sanity_check_entries(entries, cpuid->nent, type))
643 return -EINVAL;
644
Avi Kivity00b27a32011-11-23 16:30:32 +0200645 r = -ENOMEM;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100646 cpuid_entries = vzalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
Avi Kivity00b27a32011-11-23 16:30:32 +0200647 if (!cpuid_entries)
648 goto out;
649
Sasha Levin831bf662011-11-28 11:20:29 +0200650 r = 0;
651 for (i = 0; i < ARRAY_SIZE(param); i++) {
Mathias Krause326d07c2012-08-30 01:30:13 +0200652 const struct kvm_cpuid_param *ent = &param[i];
Avi Kivity00b27a32011-11-23 16:30:32 +0200653
Sasha Levin831bf662011-11-28 11:20:29 +0200654 if (ent->qualifier && !ent->qualifier(ent))
655 continue;
Avi Kivity00b27a32011-11-23 16:30:32 +0200656
Sasha Levin831bf662011-11-28 11:20:29 +0200657 r = do_cpuid_ent(&cpuid_entries[nent], ent->func, ent->idx,
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200658 &nent, cpuid->nent, type);
Avi Kivity00b27a32011-11-23 16:30:32 +0200659
Sasha Levin831bf662011-11-28 11:20:29 +0200660 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +0200661 goto out_free;
662
Sasha Levin831bf662011-11-28 11:20:29 +0200663 if (!ent->has_leaf_count)
664 continue;
665
Avi Kivity00b27a32011-11-23 16:30:32 +0200666 limit = cpuid_entries[nent - 1].eax;
Sasha Levin831bf662011-11-28 11:20:29 +0200667 for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func)
668 r = do_cpuid_ent(&cpuid_entries[nent], func, ent->idx,
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200669 &nent, cpuid->nent, type);
Avi Kivity00b27a32011-11-23 16:30:32 +0200670
Sasha Levin831bf662011-11-28 11:20:29 +0200671 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +0200672 goto out_free;
673 }
674
Avi Kivity00b27a32011-11-23 16:30:32 +0200675 r = -EFAULT;
676 if (copy_to_user(entries, cpuid_entries,
677 nent * sizeof(struct kvm_cpuid_entry2)))
678 goto out_free;
679 cpuid->nent = nent;
680 r = 0;
681
682out_free:
683 vfree(cpuid_entries);
684out:
685 return r;
686}
687
688static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
689{
690 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
691 int j, nent = vcpu->arch.cpuid_nent;
692
693 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
694 /* when no next entry is found, the current entry[i] is reselected */
695 for (j = i + 1; ; j = (j + 1) % nent) {
696 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
697 if (ej->function == e->function) {
698 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
699 return j;
700 }
701 }
702 return 0; /* silence gcc, even though control never reaches here */
703}
704
705/* find an entry with matching function, matching index (if needed), and that
706 * should be read next (if it's stateful) */
707static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
708 u32 function, u32 index)
709{
710 if (e->function != function)
711 return 0;
712 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
713 return 0;
714 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
715 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
716 return 0;
717 return 1;
718}
719
720struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
721 u32 function, u32 index)
722{
723 int i;
724 struct kvm_cpuid_entry2 *best = NULL;
725
726 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
727 struct kvm_cpuid_entry2 *e;
728
729 e = &vcpu->arch.cpuid_entries[i];
730 if (is_matching_cpuid_entry(e, function, index)) {
731 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
732 move_to_next_stateful_cpuid_entry(vcpu, i);
733 best = e;
734 break;
735 }
736 }
737 return best;
738}
739EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
740
741int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
742{
743 struct kvm_cpuid_entry2 *best;
744
745 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
746 if (!best || best->eax < 0x80000008)
747 goto not_found;
748 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
749 if (best)
750 return best->eax & 0xff;
751not_found:
752 return 36;
753}
Bandan Das3573e222014-05-06 02:19:16 -0400754EXPORT_SYMBOL_GPL(cpuid_maxphyaddr);
Avi Kivity00b27a32011-11-23 16:30:32 +0200755
756/*
757 * If no match is found, check whether we exceed the vCPU's limit
758 * and return the content of the highest valid _standard_ leaf instead.
759 * This is to satisfy the CPUID specification.
760 */
761static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
762 u32 function, u32 index)
763{
764 struct kvm_cpuid_entry2 *maxlevel;
765
766 maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
767 if (!maxlevel || maxlevel->eax >= function)
768 return NULL;
769 if (function & 0x80000000) {
770 maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
771 if (!maxlevel)
772 return NULL;
773 }
774 return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
775}
776
Avi Kivity62046e52012-06-07 14:07:48 +0300777void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
Avi Kivity00b27a32011-11-23 16:30:32 +0200778{
Avi Kivity62046e52012-06-07 14:07:48 +0300779 u32 function = *eax, index = *ecx;
Avi Kivity00b27a32011-11-23 16:30:32 +0200780 struct kvm_cpuid_entry2 *best;
781
Avi Kivity00b27a32011-11-23 16:30:32 +0200782 best = kvm_find_cpuid_entry(vcpu, function, index);
783
784 if (!best)
785 best = check_cpuid_limit(vcpu, function, index);
786
Marcelo Tosattibc613492014-09-18 18:24:57 -0300787 /*
788 * Perfmon not yet supported for L2 guest.
789 */
790 if (is_guest_mode(vcpu) && function == 0xa)
791 best = NULL;
792
Avi Kivity00b27a32011-11-23 16:30:32 +0200793 if (best) {
Avi Kivity62046e52012-06-07 14:07:48 +0300794 *eax = best->eax;
795 *ebx = best->ebx;
796 *ecx = best->ecx;
797 *edx = best->edx;
798 } else
799 *eax = *ebx = *ecx = *edx = 0;
Gleb Natapova9d4e432013-11-04 15:52:43 +0200800 trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx);
Avi Kivity62046e52012-06-07 14:07:48 +0300801}
Julian Stecklina66f7b722012-12-05 15:26:19 +0100802EXPORT_SYMBOL_GPL(kvm_cpuid);
Avi Kivity62046e52012-06-07 14:07:48 +0300803
804void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
805{
806 u32 function, eax, ebx, ecx, edx;
807
808 function = eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
809 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
810 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx);
811 kvm_register_write(vcpu, VCPU_REGS_RAX, eax);
812 kvm_register_write(vcpu, VCPU_REGS_RBX, ebx);
813 kvm_register_write(vcpu, VCPU_REGS_RCX, ecx);
814 kvm_register_write(vcpu, VCPU_REGS_RDX, edx);
Avi Kivity00b27a32011-11-23 16:30:32 +0200815 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200816}
817EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);