blob: e28d798f72777e7f17fd0b8e474b73b7d11d199d [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
Nadav Amitdd598092014-09-16 15:10:03 +030056int kvm_update_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity00b27a32011-11-23 16:30:32 +020057{
58 struct kvm_cpuid_entry2 *best;
59 struct kvm_lapic *apic = vcpu->arch.apic;
60
61 best = kvm_find_cpuid_entry(vcpu, 1, 0);
62 if (!best)
Nadav Amitdd598092014-09-16 15:10:03 +030063 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +020064
65 /* Update OSXSAVE bit */
66 if (cpu_has_xsave && best->function == 0x1) {
67 best->ecx &= ~(bit(X86_FEATURE_OSXSAVE));
68 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
69 best->ecx |= bit(X86_FEATURE_OSXSAVE);
70 }
71
72 if (apic) {
73 if (best->ecx & bit(X86_FEATURE_TSC_DEADLINE_TIMER))
74 apic->lapic_timer.timer_mode_mask = 3 << 17;
75 else
76 apic->lapic_timer.timer_mode_mask = 1 << 17;
77 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020078
Paolo Bonzinid7876f12013-10-02 16:06:15 +020079 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020080 if (!best) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020081 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020082 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
83 } else {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020084 vcpu->arch.guest_supported_xcr0 =
85 (best->eax | ((u64)best->edx << 32)) &
Paolo Bonzini4ff41732014-02-24 12:15:16 +010086 kvm_supported_xcr0();
Liu, Jinsong56c103e2014-02-21 17:39:02 +000087 vcpu->arch.guest_xstate_size = best->ebx =
88 xstate_required_size(vcpu->arch.xcr0);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020089 }
Paolo Bonzinid7876f12013-10-02 16:06:15 +020090
Nadav Amitdd598092014-09-16 15:10:03 +030091 /*
92 * The existing code assumes virtual address is 48-bit in the canonical
93 * address checks; exit if it is ever changed.
94 */
95 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
96 if (best && ((best->eax & 0xff00) >> 8) != 48 &&
97 ((best->eax & 0xff00) >> 8) != 0)
98 return -EINVAL;
99
Gleb Natapovf5132b02011-11-10 14:57:22 +0200100 kvm_pmu_cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300101 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200102}
103
104static int is_efer_nx(void)
105{
106 unsigned long long efer = 0;
107
108 rdmsrl_safe(MSR_EFER, &efer);
109 return efer & EFER_NX;
110}
111
112static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
113{
114 int i;
115 struct kvm_cpuid_entry2 *e, *entry;
116
117 entry = NULL;
118 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
119 e = &vcpu->arch.cpuid_entries[i];
120 if (e->function == 0x80000001) {
121 entry = e;
122 break;
123 }
124 }
Nadav Amit6689fbe2014-08-20 16:38:19 +0300125 if (entry && (entry->edx & bit(X86_FEATURE_NX)) && !is_efer_nx()) {
126 entry->edx &= ~bit(X86_FEATURE_NX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200127 printk(KERN_INFO "kvm: guest NX capability removed\n");
128 }
129}
130
131/* when an old userspace process fills a new kernel module */
132int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
133 struct kvm_cpuid *cpuid,
134 struct kvm_cpuid_entry __user *entries)
135{
136 int r, i;
137 struct kvm_cpuid_entry *cpuid_entries;
138
139 r = -E2BIG;
140 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
141 goto out;
142 r = -ENOMEM;
143 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
144 if (!cpuid_entries)
145 goto out;
146 r = -EFAULT;
147 if (copy_from_user(cpuid_entries, entries,
148 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
149 goto out_free;
150 for (i = 0; i < cpuid->nent; i++) {
151 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
152 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
153 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
154 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
155 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
156 vcpu->arch.cpuid_entries[i].index = 0;
157 vcpu->arch.cpuid_entries[i].flags = 0;
158 vcpu->arch.cpuid_entries[i].padding[0] = 0;
159 vcpu->arch.cpuid_entries[i].padding[1] = 0;
160 vcpu->arch.cpuid_entries[i].padding[2] = 0;
161 }
162 vcpu->arch.cpuid_nent = cpuid->nent;
163 cpuid_fix_nx_cap(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200164 kvm_apic_set_version(vcpu);
165 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300166 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200167
168out_free:
169 vfree(cpuid_entries);
170out:
171 return r;
172}
173
174int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
175 struct kvm_cpuid2 *cpuid,
176 struct kvm_cpuid_entry2 __user *entries)
177{
178 int r;
179
180 r = -E2BIG;
181 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
182 goto out;
183 r = -EFAULT;
184 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
185 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
186 goto out;
187 vcpu->arch.cpuid_nent = cpuid->nent;
188 kvm_apic_set_version(vcpu);
189 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300190 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200191out:
192 return r;
193}
194
195int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
196 struct kvm_cpuid2 *cpuid,
197 struct kvm_cpuid_entry2 __user *entries)
198{
199 int r;
200
201 r = -E2BIG;
202 if (cpuid->nent < vcpu->arch.cpuid_nent)
203 goto out;
204 r = -EFAULT;
205 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
206 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
207 goto out;
208 return 0;
209
210out:
211 cpuid->nent = vcpu->arch.cpuid_nent;
212 return r;
213}
214
215static void cpuid_mask(u32 *word, int wordnum)
216{
217 *word &= boot_cpu_data.x86_capability[wordnum];
218}
219
220static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
221 u32 index)
222{
223 entry->function = function;
224 entry->index = index;
225 cpuid_count(entry->function, entry->index,
226 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
227 entry->flags = 0;
228}
229
Avi Kivity00b27a32011-11-23 16:30:32 +0200230#define F(x) bit(X86_FEATURE_##x)
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) |
Feng Wude935ae2014-04-01 17:46:36 +0800320 F(ADX) | F(SMAP);
Avi Kivity00b27a32011-11-23 16:30:32 +0200321
322 /* all calls to cpuid_count() should be made on the same cpu */
323 get_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200324
325 r = -E2BIG;
326
327 if (*nent >= maxnent)
328 goto out;
329
Avi Kivity00b27a32011-11-23 16:30:32 +0200330 do_cpuid_1_ent(entry, function, index);
331 ++*nent;
332
333 switch (function) {
334 case 0:
335 entry->eax = min(entry->eax, (u32)0xd);
336 break;
337 case 1:
338 entry->edx &= kvm_supported_word0_x86_features;
339 cpuid_mask(&entry->edx, 0);
340 entry->ecx &= kvm_supported_word4_x86_features;
341 cpuid_mask(&entry->ecx, 4);
342 /* we support x2apic emulation even if host does not support
343 * it since we emulate x2apic in software */
344 entry->ecx |= F(X2APIC);
345 break;
346 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
347 * may return different values. This forces us to get_cpu() before
348 * issuing the first command, and also to emulate this annoying behavior
349 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
350 case 2: {
351 int t, times = entry->eax & 0xff;
352
353 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
354 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Sasha Levin831bf662011-11-28 11:20:29 +0200355 for (t = 1; t < times; ++t) {
356 if (*nent >= maxnent)
357 goto out;
358
Avi Kivity00b27a32011-11-23 16:30:32 +0200359 do_cpuid_1_ent(&entry[t], function, 0);
360 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
361 ++*nent;
362 }
363 break;
364 }
365 /* function 4 has additional index. */
366 case 4: {
367 int i, cache_type;
368
369 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
370 /* read more entries until cache_type is zero */
Sasha Levin831bf662011-11-28 11:20:29 +0200371 for (i = 1; ; ++i) {
372 if (*nent >= maxnent)
373 goto out;
374
Avi Kivity00b27a32011-11-23 16:30:32 +0200375 cache_type = entry[i - 1].eax & 0x1f;
376 if (!cache_type)
377 break;
378 do_cpuid_1_ent(&entry[i], function, i);
379 entry[i].flags |=
380 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
381 ++*nent;
382 }
383 break;
384 }
385 case 7: {
386 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Guo Chaobbbda792012-06-28 15:20:58 +0800387 /* Mask ebx against host capability word 9 */
Avi Kivity00b27a32011-11-23 16:30:32 +0200388 if (index == 0) {
389 entry->ebx &= kvm_supported_word9_x86_features;
390 cpuid_mask(&entry->ebx, 9);
Will Auldba904632012-11-29 12:42:50 -0800391 // TSC_ADJUST is emulated
392 entry->ebx |= F(TSC_ADJUST);
Avi Kivity00b27a32011-11-23 16:30:32 +0200393 } else
394 entry->ebx = 0;
395 entry->eax = 0;
396 entry->ecx = 0;
397 entry->edx = 0;
398 break;
399 }
400 case 9:
401 break;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200402 case 0xa: { /* Architectural Performance Monitoring */
403 struct x86_pmu_capability cap;
404 union cpuid10_eax eax;
405 union cpuid10_edx edx;
406
407 perf_get_x86_pmu_capability(&cap);
408
409 /*
410 * Only support guest architectural pmu on a host
411 * with architectural pmu.
412 */
413 if (!cap.version)
414 memset(&cap, 0, sizeof(cap));
415
416 eax.split.version_id = min(cap.version, 2);
417 eax.split.num_counters = cap.num_counters_gp;
418 eax.split.bit_width = cap.bit_width_gp;
419 eax.split.mask_length = cap.events_mask_len;
420
421 edx.split.num_counters_fixed = cap.num_counters_fixed;
422 edx.split.bit_width_fixed = cap.bit_width_fixed;
423 edx.split.reserved = 0;
424
425 entry->eax = eax.full;
426 entry->ebx = cap.events_mask;
427 entry->ecx = 0;
428 entry->edx = edx.full;
429 break;
430 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200431 /* function 0xb has additional index. */
432 case 0xb: {
433 int i, level_type;
434
435 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
436 /* read more entries until level_type is zero */
Sasha Levin831bf662011-11-28 11:20:29 +0200437 for (i = 1; ; ++i) {
438 if (*nent >= maxnent)
439 goto out;
440
Avi Kivity00b27a32011-11-23 16:30:32 +0200441 level_type = entry[i - 1].ecx & 0xff00;
442 if (!level_type)
443 break;
444 do_cpuid_1_ent(&entry[i], function, i);
445 entry[i].flags |=
446 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
447 ++*nent;
448 }
449 break;
450 }
451 case 0xd: {
452 int idx, i;
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100453 u64 supported = kvm_supported_xcr0();
Avi Kivity00b27a32011-11-23 16:30:32 +0200454
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100455 entry->eax &= supported;
456 entry->edx &= supported >> 32;
Avi Kivity00b27a32011-11-23 16:30:32 +0200457 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Sasha Levin831bf662011-11-28 11:20:29 +0200458 for (idx = 1, i = 1; idx < 64; ++idx) {
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100459 u64 mask = ((u64)1 << idx);
Sasha Levin831bf662011-11-28 11:20:29 +0200460 if (*nent >= maxnent)
461 goto out;
462
Avi Kivity00b27a32011-11-23 16:30:32 +0200463 do_cpuid_1_ent(&entry[i], function, idx);
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100464 if (entry[i].eax == 0 || !(supported & mask))
Avi Kivity00b27a32011-11-23 16:30:32 +0200465 continue;
466 entry[i].flags |=
467 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
468 ++*nent;
469 ++i;
470 }
471 break;
472 }
473 case KVM_CPUID_SIGNATURE: {
Mathias Krause326d07c2012-08-30 01:30:13 +0200474 static const char signature[12] = "KVMKVMKVM\0\0";
475 const u32 *sigptr = (const u32 *)signature;
Michael S. Tsirkin57c22e52012-05-02 17:55:56 +0300476 entry->eax = KVM_CPUID_FEATURES;
Avi Kivity00b27a32011-11-23 16:30:32 +0200477 entry->ebx = sigptr[0];
478 entry->ecx = sigptr[1];
479 entry->edx = sigptr[2];
480 break;
481 }
482 case KVM_CPUID_FEATURES:
483 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
484 (1 << KVM_FEATURE_NOP_IO_DELAY) |
485 (1 << KVM_FEATURE_CLOCKSOURCE2) |
486 (1 << KVM_FEATURE_ASYNC_PF) |
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300487 (1 << KVM_FEATURE_PV_EOI) |
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +0530488 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
489 (1 << KVM_FEATURE_PV_UNHALT);
Avi Kivity00b27a32011-11-23 16:30:32 +0200490
491 if (sched_info_on())
492 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
493
494 entry->ebx = 0;
495 entry->ecx = 0;
496 entry->edx = 0;
497 break;
498 case 0x80000000:
499 entry->eax = min(entry->eax, 0x8000001a);
500 break;
501 case 0x80000001:
502 entry->edx &= kvm_supported_word1_x86_features;
503 cpuid_mask(&entry->edx, 1);
504 entry->ecx &= kvm_supported_word6_x86_features;
505 cpuid_mask(&entry->ecx, 6);
506 break;
Marcelo Tosattie4c9a5a12014-04-26 22:30:23 -0300507 case 0x80000007: /* Advanced power management */
508 /* invariant TSC is CPUID.80000007H:EDX[8] */
509 entry->edx &= (1 << 8);
510 /* mask against host */
511 entry->edx &= boot_cpu_data.x86_power;
512 entry->eax = entry->ebx = entry->ecx = 0;
513 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200514 case 0x80000008: {
515 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
516 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
517 unsigned phys_as = entry->eax & 0xff;
518
519 if (!g_phys_as)
520 g_phys_as = phys_as;
521 entry->eax = g_phys_as | (virt_as << 8);
522 entry->ebx = entry->edx = 0;
523 break;
524 }
525 case 0x80000019:
526 entry->ecx = entry->edx = 0;
527 break;
528 case 0x8000001a:
529 break;
530 case 0x8000001d:
531 break;
532 /*Add support for Centaur's CPUID instruction*/
533 case 0xC0000000:
534 /*Just support up to 0xC0000004 now*/
535 entry->eax = min(entry->eax, 0xC0000004);
536 break;
537 case 0xC0000001:
538 entry->edx &= kvm_supported_word5_x86_features;
539 cpuid_mask(&entry->edx, 5);
540 break;
541 case 3: /* Processor serial number */
542 case 5: /* MONITOR/MWAIT */
543 case 6: /* Thermal management */
Avi Kivity00b27a32011-11-23 16:30:32 +0200544 case 0xC0000002:
545 case 0xC0000003:
546 case 0xC0000004:
547 default:
548 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
549 break;
550 }
551
552 kvm_x86_ops->set_supported_cpuid(function, entry);
553
Sasha Levin831bf662011-11-28 11:20:29 +0200554 r = 0;
555
556out:
Avi Kivity00b27a32011-11-23 16:30:32 +0200557 put_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200558
559 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200560}
561
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200562static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 func,
563 u32 idx, int *nent, int maxnent, unsigned int type)
564{
565 if (type == KVM_GET_EMULATED_CPUID)
566 return __do_cpuid_ent_emulated(entry, func, idx, nent, maxnent);
567
568 return __do_cpuid_ent(entry, func, idx, nent, maxnent);
569}
570
Avi Kivity00b27a32011-11-23 16:30:32 +0200571#undef F
572
Sasha Levin831bf662011-11-28 11:20:29 +0200573struct kvm_cpuid_param {
574 u32 func;
575 u32 idx;
576 bool has_leaf_count;
Mathias Krause326d07c2012-08-30 01:30:13 +0200577 bool (*qualifier)(const struct kvm_cpuid_param *param);
Sasha Levin831bf662011-11-28 11:20:29 +0200578};
579
Mathias Krause326d07c2012-08-30 01:30:13 +0200580static bool is_centaur_cpu(const struct kvm_cpuid_param *param)
Sasha Levin831bf662011-11-28 11:20:29 +0200581{
582 return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR;
583}
584
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200585static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
586 __u32 num_entries, unsigned int ioctl_type)
587{
588 int i;
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100589 __u32 pad[3];
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200590
591 if (ioctl_type != KVM_GET_EMULATED_CPUID)
592 return false;
593
594 /*
595 * We want to make sure that ->padding is being passed clean from
596 * userspace in case we want to use it for something in the future.
597 *
598 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
599 * have to give ourselves satisfied only with the emulated side. /me
600 * sheds a tear.
601 */
602 for (i = 0; i < num_entries; i++) {
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100603 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
604 return true;
605
606 if (pad[0] || pad[1] || pad[2])
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200607 return true;
608 }
609 return false;
610}
611
612int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
613 struct kvm_cpuid_entry2 __user *entries,
614 unsigned int type)
Avi Kivity00b27a32011-11-23 16:30:32 +0200615{
616 struct kvm_cpuid_entry2 *cpuid_entries;
Sasha Levin831bf662011-11-28 11:20:29 +0200617 int limit, nent = 0, r = -E2BIG, i;
Avi Kivity00b27a32011-11-23 16:30:32 +0200618 u32 func;
Mathias Krause326d07c2012-08-30 01:30:13 +0200619 static const struct kvm_cpuid_param param[] = {
Sasha Levin831bf662011-11-28 11:20:29 +0200620 { .func = 0, .has_leaf_count = true },
621 { .func = 0x80000000, .has_leaf_count = true },
622 { .func = 0xC0000000, .qualifier = is_centaur_cpu, .has_leaf_count = true },
623 { .func = KVM_CPUID_SIGNATURE },
624 { .func = KVM_CPUID_FEATURES },
625 };
Avi Kivity00b27a32011-11-23 16:30:32 +0200626
627 if (cpuid->nent < 1)
628 goto out;
629 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
630 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200631
632 if (sanity_check_entries(entries, cpuid->nent, type))
633 return -EINVAL;
634
Avi Kivity00b27a32011-11-23 16:30:32 +0200635 r = -ENOMEM;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100636 cpuid_entries = vzalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
Avi Kivity00b27a32011-11-23 16:30:32 +0200637 if (!cpuid_entries)
638 goto out;
639
Sasha Levin831bf662011-11-28 11:20:29 +0200640 r = 0;
641 for (i = 0; i < ARRAY_SIZE(param); i++) {
Mathias Krause326d07c2012-08-30 01:30:13 +0200642 const struct kvm_cpuid_param *ent = &param[i];
Avi Kivity00b27a32011-11-23 16:30:32 +0200643
Sasha Levin831bf662011-11-28 11:20:29 +0200644 if (ent->qualifier && !ent->qualifier(ent))
645 continue;
Avi Kivity00b27a32011-11-23 16:30:32 +0200646
Sasha Levin831bf662011-11-28 11:20:29 +0200647 r = do_cpuid_ent(&cpuid_entries[nent], ent->func, ent->idx,
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200648 &nent, cpuid->nent, type);
Avi Kivity00b27a32011-11-23 16:30:32 +0200649
Sasha Levin831bf662011-11-28 11:20:29 +0200650 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +0200651 goto out_free;
652
Sasha Levin831bf662011-11-28 11:20:29 +0200653 if (!ent->has_leaf_count)
654 continue;
655
Avi Kivity00b27a32011-11-23 16:30:32 +0200656 limit = cpuid_entries[nent - 1].eax;
Sasha Levin831bf662011-11-28 11:20:29 +0200657 for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func)
658 r = do_cpuid_ent(&cpuid_entries[nent], func, ent->idx,
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200659 &nent, cpuid->nent, type);
Avi Kivity00b27a32011-11-23 16:30:32 +0200660
Sasha Levin831bf662011-11-28 11:20:29 +0200661 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +0200662 goto out_free;
663 }
664
Avi Kivity00b27a32011-11-23 16:30:32 +0200665 r = -EFAULT;
666 if (copy_to_user(entries, cpuid_entries,
667 nent * sizeof(struct kvm_cpuid_entry2)))
668 goto out_free;
669 cpuid->nent = nent;
670 r = 0;
671
672out_free:
673 vfree(cpuid_entries);
674out:
675 return r;
676}
677
678static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
679{
680 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
681 int j, nent = vcpu->arch.cpuid_nent;
682
683 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
684 /* when no next entry is found, the current entry[i] is reselected */
685 for (j = i + 1; ; j = (j + 1) % nent) {
686 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
687 if (ej->function == e->function) {
688 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
689 return j;
690 }
691 }
692 return 0; /* silence gcc, even though control never reaches here */
693}
694
695/* find an entry with matching function, matching index (if needed), and that
696 * should be read next (if it's stateful) */
697static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
698 u32 function, u32 index)
699{
700 if (e->function != function)
701 return 0;
702 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
703 return 0;
704 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
705 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
706 return 0;
707 return 1;
708}
709
710struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
711 u32 function, u32 index)
712{
713 int i;
714 struct kvm_cpuid_entry2 *best = NULL;
715
716 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
717 struct kvm_cpuid_entry2 *e;
718
719 e = &vcpu->arch.cpuid_entries[i];
720 if (is_matching_cpuid_entry(e, function, index)) {
721 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
722 move_to_next_stateful_cpuid_entry(vcpu, i);
723 best = e;
724 break;
725 }
726 }
727 return best;
728}
729EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
730
731int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
732{
733 struct kvm_cpuid_entry2 *best;
734
735 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
736 if (!best || best->eax < 0x80000008)
737 goto not_found;
738 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
739 if (best)
740 return best->eax & 0xff;
741not_found:
742 return 36;
743}
Bandan Das3573e222014-05-06 02:19:16 -0400744EXPORT_SYMBOL_GPL(cpuid_maxphyaddr);
Avi Kivity00b27a32011-11-23 16:30:32 +0200745
746/*
747 * If no match is found, check whether we exceed the vCPU's limit
748 * and return the content of the highest valid _standard_ leaf instead.
749 * This is to satisfy the CPUID specification.
750 */
751static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
752 u32 function, u32 index)
753{
754 struct kvm_cpuid_entry2 *maxlevel;
755
756 maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
757 if (!maxlevel || maxlevel->eax >= function)
758 return NULL;
759 if (function & 0x80000000) {
760 maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
761 if (!maxlevel)
762 return NULL;
763 }
764 return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
765}
766
Avi Kivity62046e52012-06-07 14:07:48 +0300767void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
Avi Kivity00b27a32011-11-23 16:30:32 +0200768{
Avi Kivity62046e52012-06-07 14:07:48 +0300769 u32 function = *eax, index = *ecx;
Avi Kivity00b27a32011-11-23 16:30:32 +0200770 struct kvm_cpuid_entry2 *best;
771
Avi Kivity00b27a32011-11-23 16:30:32 +0200772 best = kvm_find_cpuid_entry(vcpu, function, index);
773
774 if (!best)
775 best = check_cpuid_limit(vcpu, function, index);
776
777 if (best) {
Avi Kivity62046e52012-06-07 14:07:48 +0300778 *eax = best->eax;
779 *ebx = best->ebx;
780 *ecx = best->ecx;
781 *edx = best->edx;
782 } else
783 *eax = *ebx = *ecx = *edx = 0;
Gleb Natapova9d4e432013-11-04 15:52:43 +0200784 trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx);
Avi Kivity62046e52012-06-07 14:07:48 +0300785}
Julian Stecklina66f7b722012-12-05 15:26:19 +0100786EXPORT_SYMBOL_GPL(kvm_cpuid);
Avi Kivity62046e52012-06-07 14:07:48 +0300787
788void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
789{
790 u32 function, eax, ebx, ecx, edx;
791
792 function = eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
793 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
794 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx);
795 kvm_register_write(vcpu, VCPU_REGS_RAX, eax);
796 kvm_register_write(vcpu, VCPU_REGS_RBX, ebx);
797 kvm_register_write(vcpu, VCPU_REGS_RCX, ecx);
798 kvm_register_write(vcpu, VCPU_REGS_RDX, edx);
Avi Kivity00b27a32011-11-23 16:30:32 +0200799 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200800}
801EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);