OSDN Git Service

KVM: cpuid: rename do_cpuid_1_ent
[tomoyo/tomoyo-test1.git] / arch / x86 / kvm / cpuid.c
1 /*
2  * Kernel-based Virtual Machine driver for Linux
3  * cpuid support routines
4  *
5  * derived from arch/x86/kvm/x86.c
6  *
7  * Copyright 2011 Red Hat, Inc. and/or its affiliates.
8  * Copyright IBM Corporation, 2008
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2.  See
11  * the COPYING file in the top-level directory.
12  *
13  */
14
15 #include <linux/kvm_host.h>
16 #include <linux/export.h>
17 #include <linux/vmalloc.h>
18 #include <linux/uaccess.h>
19 #include <linux/sched/stat.h>
20
21 #include <asm/processor.h>
22 #include <asm/user.h>
23 #include <asm/fpu/xstate.h>
24 #include "cpuid.h"
25 #include "lapic.h"
26 #include "mmu.h"
27 #include "trace.h"
28 #include "pmu.h"
29
30 static u32 xstate_required_size(u64 xstate_bv, bool compacted)
31 {
32         int feature_bit = 0;
33         u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
34
35         xstate_bv &= XFEATURE_MASK_EXTEND;
36         while (xstate_bv) {
37                 if (xstate_bv & 0x1) {
38                         u32 eax, ebx, ecx, edx, offset;
39                         cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
40                         offset = compacted ? ret : ebx;
41                         ret = max(ret, offset + eax);
42                 }
43
44                 xstate_bv >>= 1;
45                 feature_bit++;
46         }
47
48         return ret;
49 }
50
51 bool kvm_mpx_supported(void)
52 {
53         return ((host_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
54                  && kvm_x86_ops->mpx_supported());
55 }
56 EXPORT_SYMBOL_GPL(kvm_mpx_supported);
57
58 u64 kvm_supported_xcr0(void)
59 {
60         u64 xcr0 = KVM_SUPPORTED_XCR0 & host_xcr0;
61
62         if (!kvm_mpx_supported())
63                 xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
64
65         return xcr0;
66 }
67
68 #define F(x) bit(X86_FEATURE_##x)
69
70 int kvm_update_cpuid(struct kvm_vcpu *vcpu)
71 {
72         struct kvm_cpuid_entry2 *best;
73         struct kvm_lapic *apic = vcpu->arch.apic;
74
75         best = kvm_find_cpuid_entry(vcpu, 1, 0);
76         if (!best)
77                 return 0;
78
79         /* Update OSXSAVE bit */
80         if (boot_cpu_has(X86_FEATURE_XSAVE) && best->function == 0x1) {
81                 best->ecx &= ~F(OSXSAVE);
82                 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
83                         best->ecx |= F(OSXSAVE);
84         }
85
86         best->edx &= ~F(APIC);
87         if (vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE)
88                 best->edx |= F(APIC);
89
90         if (apic) {
91                 if (best->ecx & F(TSC_DEADLINE_TIMER))
92                         apic->lapic_timer.timer_mode_mask = 3 << 17;
93                 else
94                         apic->lapic_timer.timer_mode_mask = 1 << 17;
95         }
96
97         best = kvm_find_cpuid_entry(vcpu, 7, 0);
98         if (best) {
99                 /* Update OSPKE bit */
100                 if (boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7) {
101                         best->ecx &= ~F(OSPKE);
102                         if (kvm_read_cr4_bits(vcpu, X86_CR4_PKE))
103                                 best->ecx |= F(OSPKE);
104                 }
105         }
106
107         best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
108         if (!best) {
109                 vcpu->arch.guest_supported_xcr0 = 0;
110                 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
111         } else {
112                 vcpu->arch.guest_supported_xcr0 =
113                         (best->eax | ((u64)best->edx << 32)) &
114                         kvm_supported_xcr0();
115                 vcpu->arch.guest_xstate_size = best->ebx =
116                         xstate_required_size(vcpu->arch.xcr0, false);
117         }
118
119         best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
120         if (best && (best->eax & (F(XSAVES) | F(XSAVEC))))
121                 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
122
123         /*
124          * The existing code assumes virtual address is 48-bit or 57-bit in the
125          * canonical address checks; exit if it is ever changed.
126          */
127         best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
128         if (best) {
129                 int vaddr_bits = (best->eax & 0xff00) >> 8;
130
131                 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
132                         return -EINVAL;
133         }
134
135         best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
136         if (kvm_hlt_in_guest(vcpu->kvm) && best &&
137                 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
138                 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
139
140         if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
141                 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
142                 if (best) {
143                         if (vcpu->arch.ia32_misc_enable_msr & MSR_IA32_MISC_ENABLE_MWAIT)
144                                 best->ecx |= F(MWAIT);
145                         else
146                                 best->ecx &= ~F(MWAIT);
147                 }
148         }
149
150         /* Update physical-address width */
151         vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
152         kvm_mmu_reset_context(vcpu);
153
154         kvm_pmu_refresh(vcpu);
155         return 0;
156 }
157
158 static int is_efer_nx(void)
159 {
160         unsigned long long efer = 0;
161
162         rdmsrl_safe(MSR_EFER, &efer);
163         return efer & EFER_NX;
164 }
165
166 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
167 {
168         int i;
169         struct kvm_cpuid_entry2 *e, *entry;
170
171         entry = NULL;
172         for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
173                 e = &vcpu->arch.cpuid_entries[i];
174                 if (e->function == 0x80000001) {
175                         entry = e;
176                         break;
177                 }
178         }
179         if (entry && (entry->edx & F(NX)) && !is_efer_nx()) {
180                 entry->edx &= ~F(NX);
181                 printk(KERN_INFO "kvm: guest NX capability removed\n");
182         }
183 }
184
185 int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
186 {
187         struct kvm_cpuid_entry2 *best;
188
189         best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
190         if (!best || best->eax < 0x80000008)
191                 goto not_found;
192         best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
193         if (best)
194                 return best->eax & 0xff;
195 not_found:
196         return 36;
197 }
198 EXPORT_SYMBOL_GPL(cpuid_query_maxphyaddr);
199
200 /* when an old userspace process fills a new kernel module */
201 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
202                              struct kvm_cpuid *cpuid,
203                              struct kvm_cpuid_entry __user *entries)
204 {
205         int r, i;
206         struct kvm_cpuid_entry *cpuid_entries = NULL;
207
208         r = -E2BIG;
209         if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
210                 goto out;
211         r = -ENOMEM;
212         if (cpuid->nent) {
213                 cpuid_entries =
214                         vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
215                                            cpuid->nent));
216                 if (!cpuid_entries)
217                         goto out;
218                 r = -EFAULT;
219                 if (copy_from_user(cpuid_entries, entries,
220                                    cpuid->nent * sizeof(struct kvm_cpuid_entry)))
221                         goto out;
222         }
223         for (i = 0; i < cpuid->nent; i++) {
224                 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
225                 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
226                 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
227                 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
228                 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
229                 vcpu->arch.cpuid_entries[i].index = 0;
230                 vcpu->arch.cpuid_entries[i].flags = 0;
231                 vcpu->arch.cpuid_entries[i].padding[0] = 0;
232                 vcpu->arch.cpuid_entries[i].padding[1] = 0;
233                 vcpu->arch.cpuid_entries[i].padding[2] = 0;
234         }
235         vcpu->arch.cpuid_nent = cpuid->nent;
236         cpuid_fix_nx_cap(vcpu);
237         kvm_apic_set_version(vcpu);
238         kvm_x86_ops->cpuid_update(vcpu);
239         r = kvm_update_cpuid(vcpu);
240
241 out:
242         vfree(cpuid_entries);
243         return r;
244 }
245
246 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
247                               struct kvm_cpuid2 *cpuid,
248                               struct kvm_cpuid_entry2 __user *entries)
249 {
250         int r;
251
252         r = -E2BIG;
253         if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
254                 goto out;
255         r = -EFAULT;
256         if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
257                            cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
258                 goto out;
259         vcpu->arch.cpuid_nent = cpuid->nent;
260         kvm_apic_set_version(vcpu);
261         kvm_x86_ops->cpuid_update(vcpu);
262         r = kvm_update_cpuid(vcpu);
263 out:
264         return r;
265 }
266
267 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
268                               struct kvm_cpuid2 *cpuid,
269                               struct kvm_cpuid_entry2 __user *entries)
270 {
271         int r;
272
273         r = -E2BIG;
274         if (cpuid->nent < vcpu->arch.cpuid_nent)
275                 goto out;
276         r = -EFAULT;
277         if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
278                          vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
279                 goto out;
280         return 0;
281
282 out:
283         cpuid->nent = vcpu->arch.cpuid_nent;
284         return r;
285 }
286
287 static void cpuid_mask(u32 *word, int wordnum)
288 {
289         *word &= boot_cpu_data.x86_capability[wordnum];
290 }
291
292 static void do_host_cpuid(struct kvm_cpuid_entry2 *entry, u32 function,
293                            u32 index)
294 {
295         entry->function = function;
296         entry->index = index;
297         entry->flags = 0;
298
299         cpuid_count(entry->function, entry->index,
300                     &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
301
302         switch (function) {
303         case 2:
304                 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
305                 break;
306         case 4:
307         case 7:
308         case 0xb:
309         case 0xd:
310         case 0x14:
311         case 0x8000001d:
312                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
313                 break;
314         }
315 }
316
317 static int __do_cpuid_func_emulated(struct kvm_cpuid_entry2 *entry,
318                                     u32 func, int *nent, int maxnent)
319 {
320         entry->function = func;
321         entry->index = 0;
322         entry->flags = 0;
323
324         switch (func) {
325         case 0:
326                 entry->eax = 7;
327                 ++*nent;
328                 break;
329         case 1:
330                 entry->ecx = F(MOVBE);
331                 ++*nent;
332                 break;
333         case 7:
334                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
335                 entry->eax = 0;
336                 entry->ecx = F(RDPID);
337                 ++*nent;
338         default:
339                 break;
340         }
341
342         return 0;
343 }
344
345 static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)
346 {
347         unsigned f_invpcid = kvm_x86_ops->invpcid_supported() ? F(INVPCID) : 0;
348         unsigned f_mpx = kvm_mpx_supported() ? F(MPX) : 0;
349         unsigned f_umip = kvm_x86_ops->umip_emulated() ? F(UMIP) : 0;
350         unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0;
351         unsigned f_la57;
352
353         /* cpuid 7.0.ebx */
354         const u32 kvm_cpuid_7_0_ebx_x86_features =
355                 F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
356                 F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | f_mpx | F(RDSEED) |
357                 F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
358                 F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
359                 F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | f_intel_pt;
360
361         /* cpuid 7.0.ecx*/
362         const u32 kvm_cpuid_7_0_ecx_x86_features =
363                 F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ |
364                 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
365                 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
366                 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B);
367
368         /* cpuid 7.0.edx*/
369         const u32 kvm_cpuid_7_0_edx_x86_features =
370                 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
371                 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
372                 F(MD_CLEAR);
373
374         switch (index) {
375         case 0:
376                 entry->eax = 0;
377                 entry->ebx &= kvm_cpuid_7_0_ebx_x86_features;
378                 cpuid_mask(&entry->ebx, CPUID_7_0_EBX);
379                 /* TSC_ADJUST is emulated */
380                 entry->ebx |= F(TSC_ADJUST);
381
382                 entry->ecx &= kvm_cpuid_7_0_ecx_x86_features;
383                 f_la57 = entry->ecx & F(LA57);
384                 cpuid_mask(&entry->ecx, CPUID_7_ECX);
385                 /* Set LA57 based on hardware capability. */
386                 entry->ecx |= f_la57;
387                 entry->ecx |= f_umip;
388                 /* PKU is not yet implemented for shadow paging. */
389                 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
390                         entry->ecx &= ~F(PKU);
391
392                 entry->edx &= kvm_cpuid_7_0_edx_x86_features;
393                 cpuid_mask(&entry->edx, CPUID_7_EDX);
394                 /*
395                  * We emulate ARCH_CAPABILITIES in software even
396                  * if the host doesn't support it.
397                  */
398                 entry->edx |= F(ARCH_CAPABILITIES);
399                 break;
400         default:
401                 WARN_ON_ONCE(1);
402                 entry->eax = 0;
403                 entry->ebx = 0;
404                 entry->ecx = 0;
405                 entry->edx = 0;
406                 break;
407         }
408 }
409
410 static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function,
411                                   int *nent, int maxnent)
412 {
413         int r;
414         unsigned f_nx = is_efer_nx() ? F(NX) : 0;
415 #ifdef CONFIG_X86_64
416         unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
417                                 ? F(GBPAGES) : 0;
418         unsigned f_lm = F(LM);
419 #else
420         unsigned f_gbpages = 0;
421         unsigned f_lm = 0;
422 #endif
423         unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
424         unsigned f_xsaves = kvm_x86_ops->xsaves_supported() ? F(XSAVES) : 0;
425         unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0;
426
427         /* cpuid 1.edx */
428         const u32 kvm_cpuid_1_edx_x86_features =
429                 F(FPU) | F(VME) | F(DE) | F(PSE) |
430                 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
431                 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
432                 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
433                 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
434                 0 /* Reserved, DS, ACPI */ | F(MMX) |
435                 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
436                 0 /* HTT, TM, Reserved, PBE */;
437         /* cpuid 0x80000001.edx */
438         const u32 kvm_cpuid_8000_0001_edx_x86_features =
439                 F(FPU) | F(VME) | F(DE) | F(PSE) |
440                 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
441                 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
442                 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
443                 F(PAT) | F(PSE36) | 0 /* Reserved */ |
444                 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
445                 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
446                 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
447         /* cpuid 1.ecx */
448         const u32 kvm_cpuid_1_ecx_x86_features =
449                 /* NOTE: MONITOR (and MWAIT) are emulated as NOP,
450                  * but *not* advertised to guests via CPUID ! */
451                 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
452                 0 /* DS-CPL, VMX, SMX, EST */ |
453                 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
454                 F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
455                 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
456                 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
457                 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
458                 F(F16C) | F(RDRAND);
459         /* cpuid 0x80000001.ecx */
460         const u32 kvm_cpuid_8000_0001_ecx_x86_features =
461                 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
462                 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
463                 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
464                 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
465                 F(TOPOEXT) | F(PERFCTR_CORE);
466
467         /* cpuid 0x80000008.ebx */
468         const u32 kvm_cpuid_8000_0008_ebx_x86_features =
469                 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
470                 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON);
471
472         /* cpuid 0xC0000001.edx */
473         const u32 kvm_cpuid_C000_0001_edx_x86_features =
474                 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
475                 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
476                 F(PMM) | F(PMM_EN);
477
478         /* cpuid 0xD.1.eax */
479         const u32 kvm_cpuid_D_1_eax_x86_features =
480                 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | f_xsaves;
481
482         /* all calls to cpuid_count() should be made on the same cpu */
483         get_cpu();
484
485         r = -E2BIG;
486
487         if (*nent >= maxnent)
488                 goto out;
489
490         do_host_cpuid(entry, function, 0);
491         ++*nent;
492
493         switch (function) {
494         case 0:
495                 /* Limited to the highest leaf implemented in KVM. */
496                 entry->eax = min(entry->eax, 0x1fU);
497                 break;
498         case 1:
499                 entry->edx &= kvm_cpuid_1_edx_x86_features;
500                 cpuid_mask(&entry->edx, CPUID_1_EDX);
501                 entry->ecx &= kvm_cpuid_1_ecx_x86_features;
502                 cpuid_mask(&entry->ecx, CPUID_1_ECX);
503                 /* we support x2apic emulation even if host does not support
504                  * it since we emulate x2apic in software */
505                 entry->ecx |= F(X2APIC);
506                 break;
507         /* function 2 entries are STATEFUL. That is, repeated cpuid commands
508          * may return different values. This forces us to get_cpu() before
509          * issuing the first command, and also to emulate this annoying behavior
510          * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
511         case 2: {
512                 int t, times = entry->eax & 0xff;
513
514                 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
515                 for (t = 1; t < times; ++t) {
516                         if (*nent >= maxnent)
517                                 goto out;
518
519                         do_host_cpuid(&entry[t], function, 0);
520                         ++*nent;
521                 }
522                 break;
523         }
524         /* functions 4 and 0x8000001d have additional index. */
525         case 4:
526         case 0x8000001d: {
527                 int i, cache_type;
528
529                 /* read more entries until cache_type is zero */
530                 for (i = 1; ; ++i) {
531                         if (*nent >= maxnent)
532                                 goto out;
533
534                         cache_type = entry[i - 1].eax & 0x1f;
535                         if (!cache_type)
536                                 break;
537                         do_host_cpuid(&entry[i], function, i);
538                         ++*nent;
539                 }
540                 break;
541         }
542         case 6: /* Thermal management */
543                 entry->eax = 0x4; /* allow ARAT */
544                 entry->ebx = 0;
545                 entry->ecx = 0;
546                 entry->edx = 0;
547                 break;
548         /* function 7 has additional index. */
549         case 7: {
550                 int i;
551
552                 for (i = 0; ; ) {
553                         do_cpuid_7_mask(&entry[i], i);
554                         if (i == entry->eax)
555                                 break;
556                         if (*nent >= maxnent)
557                                 goto out;
558
559                         ++i;
560                         do_host_cpuid(&entry[i], function, i);
561                         ++*nent;
562                 }
563                 break;
564         }
565         case 9:
566                 break;
567         case 0xa: { /* Architectural Performance Monitoring */
568                 struct x86_pmu_capability cap;
569                 union cpuid10_eax eax;
570                 union cpuid10_edx edx;
571
572                 perf_get_x86_pmu_capability(&cap);
573
574                 /*
575                  * Only support guest architectural pmu on a host
576                  * with architectural pmu.
577                  */
578                 if (!cap.version)
579                         memset(&cap, 0, sizeof(cap));
580
581                 eax.split.version_id = min(cap.version, 2);
582                 eax.split.num_counters = cap.num_counters_gp;
583                 eax.split.bit_width = cap.bit_width_gp;
584                 eax.split.mask_length = cap.events_mask_len;
585
586                 edx.split.num_counters_fixed = cap.num_counters_fixed;
587                 edx.split.bit_width_fixed = cap.bit_width_fixed;
588                 edx.split.reserved = 0;
589
590                 entry->eax = eax.full;
591                 entry->ebx = cap.events_mask;
592                 entry->ecx = 0;
593                 entry->edx = edx.full;
594                 break;
595         }
596         /*
597          * Per Intel's SDM, the 0x1f is a superset of 0xb,
598          * thus they can be handled by common code.
599          */
600         case 0x1f:
601         case 0xb: {
602                 int i, level_type;
603
604                 /* read more entries until level_type is zero */
605                 for (i = 1; ; ++i) {
606                         if (*nent >= maxnent)
607                                 goto out;
608
609                         level_type = entry[i - 1].ecx & 0xff00;
610                         if (!level_type)
611                                 break;
612                         do_host_cpuid(&entry[i], function, i);
613                         ++*nent;
614                 }
615                 break;
616         }
617         case 0xd: {
618                 int idx, i;
619                 u64 supported = kvm_supported_xcr0();
620
621                 entry->eax &= supported;
622                 entry->ebx = xstate_required_size(supported, false);
623                 entry->ecx = entry->ebx;
624                 entry->edx &= supported >> 32;
625                 if (!supported)
626                         break;
627
628                 for (idx = 1, i = 1; idx < 64; ++idx) {
629                         u64 mask = ((u64)1 << idx);
630                         if (*nent >= maxnent)
631                                 goto out;
632
633                         do_host_cpuid(&entry[i], function, idx);
634                         if (idx == 1) {
635                                 entry[i].eax &= kvm_cpuid_D_1_eax_x86_features;
636                                 cpuid_mask(&entry[i].eax, CPUID_D_1_EAX);
637                                 entry[i].ebx = 0;
638                                 if (entry[i].eax & (F(XSAVES)|F(XSAVEC)))
639                                         entry[i].ebx =
640                                                 xstate_required_size(supported,
641                                                                      true);
642                         } else {
643                                 if (entry[i].eax == 0 || !(supported & mask))
644                                         continue;
645                                 if (WARN_ON_ONCE(entry[i].ecx & 1))
646                                         continue;
647                         }
648                         entry[i].ecx = 0;
649                         entry[i].edx = 0;
650                         ++*nent;
651                         ++i;
652                 }
653                 break;
654         }
655         /* Intel PT */
656         case 0x14: {
657                 int t, times = entry->eax;
658
659                 if (!f_intel_pt)
660                         break;
661
662                 for (t = 1; t <= times; ++t) {
663                         if (*nent >= maxnent)
664                                 goto out;
665                         do_host_cpuid(&entry[t], function, t);
666                         ++*nent;
667                 }
668                 break;
669         }
670         case KVM_CPUID_SIGNATURE: {
671                 static const char signature[12] = "KVMKVMKVM\0\0";
672                 const u32 *sigptr = (const u32 *)signature;
673                 entry->eax = KVM_CPUID_FEATURES;
674                 entry->ebx = sigptr[0];
675                 entry->ecx = sigptr[1];
676                 entry->edx = sigptr[2];
677                 break;
678         }
679         case KVM_CPUID_FEATURES:
680                 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
681                              (1 << KVM_FEATURE_NOP_IO_DELAY) |
682                              (1 << KVM_FEATURE_CLOCKSOURCE2) |
683                              (1 << KVM_FEATURE_ASYNC_PF) |
684                              (1 << KVM_FEATURE_PV_EOI) |
685                              (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
686                              (1 << KVM_FEATURE_PV_UNHALT) |
687                              (1 << KVM_FEATURE_PV_TLB_FLUSH) |
688                              (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
689                              (1 << KVM_FEATURE_PV_SEND_IPI) |
690                              (1 << KVM_FEATURE_POLL_CONTROL) |
691                              (1 << KVM_FEATURE_PV_SCHED_YIELD);
692
693                 if (sched_info_on())
694                         entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
695
696                 entry->ebx = 0;
697                 entry->ecx = 0;
698                 entry->edx = 0;
699                 break;
700         case 0x80000000:
701                 entry->eax = min(entry->eax, 0x8000001f);
702                 break;
703         case 0x80000001:
704                 entry->edx &= kvm_cpuid_8000_0001_edx_x86_features;
705                 cpuid_mask(&entry->edx, CPUID_8000_0001_EDX);
706                 entry->ecx &= kvm_cpuid_8000_0001_ecx_x86_features;
707                 cpuid_mask(&entry->ecx, CPUID_8000_0001_ECX);
708                 break;
709         case 0x80000007: /* Advanced power management */
710                 /* invariant TSC is CPUID.80000007H:EDX[8] */
711                 entry->edx &= (1 << 8);
712                 /* mask against host */
713                 entry->edx &= boot_cpu_data.x86_power;
714                 entry->eax = entry->ebx = entry->ecx = 0;
715                 break;
716         case 0x80000008: {
717                 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
718                 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
719                 unsigned phys_as = entry->eax & 0xff;
720
721                 if (!g_phys_as)
722                         g_phys_as = phys_as;
723                 entry->eax = g_phys_as | (virt_as << 8);
724                 entry->edx = 0;
725                 /*
726                  * IBRS, IBPB and VIRT_SSBD aren't necessarily present in
727                  * hardware cpuid
728                  */
729                 if (boot_cpu_has(X86_FEATURE_AMD_IBPB))
730                         entry->ebx |= F(AMD_IBPB);
731                 if (boot_cpu_has(X86_FEATURE_AMD_IBRS))
732                         entry->ebx |= F(AMD_IBRS);
733                 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
734                         entry->ebx |= F(VIRT_SSBD);
735                 entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features;
736                 cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
737                 /*
738                  * The preference is to use SPEC CTRL MSR instead of the
739                  * VIRT_SPEC MSR.
740                  */
741                 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
742                     !boot_cpu_has(X86_FEATURE_AMD_SSBD))
743                         entry->ebx |= F(VIRT_SSBD);
744                 break;
745         }
746         case 0x80000019:
747                 entry->ecx = entry->edx = 0;
748                 break;
749         case 0x8000001a:
750         case 0x8000001e:
751                 break;
752         /*Add support for Centaur's CPUID instruction*/
753         case 0xC0000000:
754                 /*Just support up to 0xC0000004 now*/
755                 entry->eax = min(entry->eax, 0xC0000004);
756                 break;
757         case 0xC0000001:
758                 entry->edx &= kvm_cpuid_C000_0001_edx_x86_features;
759                 cpuid_mask(&entry->edx, CPUID_C000_0001_EDX);
760                 break;
761         case 3: /* Processor serial number */
762         case 5: /* MONITOR/MWAIT */
763         case 0xC0000002:
764         case 0xC0000003:
765         case 0xC0000004:
766         default:
767                 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
768                 break;
769         }
770
771         kvm_x86_ops->set_supported_cpuid(function, entry);
772
773         r = 0;
774
775 out:
776         put_cpu();
777
778         return r;
779 }
780
781 static int do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 func,
782                          int *nent, int maxnent, unsigned int type)
783 {
784         if (type == KVM_GET_EMULATED_CPUID)
785                 return __do_cpuid_func_emulated(entry, func, nent, maxnent);
786
787         return __do_cpuid_func(entry, func, nent, maxnent);
788 }
789
790 #undef F
791
792 struct kvm_cpuid_param {
793         u32 func;
794         bool has_leaf_count;
795         bool (*qualifier)(const struct kvm_cpuid_param *param);
796 };
797
798 static bool is_centaur_cpu(const struct kvm_cpuid_param *param)
799 {
800         return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR;
801 }
802
803 static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
804                                  __u32 num_entries, unsigned int ioctl_type)
805 {
806         int i;
807         __u32 pad[3];
808
809         if (ioctl_type != KVM_GET_EMULATED_CPUID)
810                 return false;
811
812         /*
813          * We want to make sure that ->padding is being passed clean from
814          * userspace in case we want to use it for something in the future.
815          *
816          * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
817          * have to give ourselves satisfied only with the emulated side. /me
818          * sheds a tear.
819          */
820         for (i = 0; i < num_entries; i++) {
821                 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
822                         return true;
823
824                 if (pad[0] || pad[1] || pad[2])
825                         return true;
826         }
827         return false;
828 }
829
830 int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
831                             struct kvm_cpuid_entry2 __user *entries,
832                             unsigned int type)
833 {
834         struct kvm_cpuid_entry2 *cpuid_entries;
835         int limit, nent = 0, r = -E2BIG, i;
836         u32 func;
837         static const struct kvm_cpuid_param param[] = {
838                 { .func = 0, .has_leaf_count = true },
839                 { .func = 0x80000000, .has_leaf_count = true },
840                 { .func = 0xC0000000, .qualifier = is_centaur_cpu, .has_leaf_count = true },
841                 { .func = KVM_CPUID_SIGNATURE },
842                 { .func = KVM_CPUID_FEATURES },
843         };
844
845         if (cpuid->nent < 1)
846                 goto out;
847         if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
848                 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
849
850         if (sanity_check_entries(entries, cpuid->nent, type))
851                 return -EINVAL;
852
853         r = -ENOMEM;
854         cpuid_entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
855                                            cpuid->nent));
856         if (!cpuid_entries)
857                 goto out;
858
859         r = 0;
860         for (i = 0; i < ARRAY_SIZE(param); i++) {
861                 const struct kvm_cpuid_param *ent = &param[i];
862
863                 if (ent->qualifier && !ent->qualifier(ent))
864                         continue;
865
866                 r = do_cpuid_func(&cpuid_entries[nent], ent->func,
867                                   &nent, cpuid->nent, type);
868
869                 if (r)
870                         goto out_free;
871
872                 if (!ent->has_leaf_count)
873                         continue;
874
875                 limit = cpuid_entries[nent - 1].eax;
876                 for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func)
877                         r = do_cpuid_func(&cpuid_entries[nent], func,
878                                           &nent, cpuid->nent, type);
879
880                 if (r)
881                         goto out_free;
882         }
883
884         r = -EFAULT;
885         if (copy_to_user(entries, cpuid_entries,
886                          nent * sizeof(struct kvm_cpuid_entry2)))
887                 goto out_free;
888         cpuid->nent = nent;
889         r = 0;
890
891 out_free:
892         vfree(cpuid_entries);
893 out:
894         return r;
895 }
896
897 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
898 {
899         struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
900         struct kvm_cpuid_entry2 *ej;
901         int j = i;
902         int nent = vcpu->arch.cpuid_nent;
903
904         e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
905         /* when no next entry is found, the current entry[i] is reselected */
906         do {
907                 j = (j + 1) % nent;
908                 ej = &vcpu->arch.cpuid_entries[j];
909         } while (ej->function != e->function);
910
911         ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
912
913         return j;
914 }
915
916 /* find an entry with matching function, matching index (if needed), and that
917  * should be read next (if it's stateful) */
918 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
919         u32 function, u32 index)
920 {
921         if (e->function != function)
922                 return 0;
923         if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
924                 return 0;
925         if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
926             !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
927                 return 0;
928         return 1;
929 }
930
931 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
932                                               u32 function, u32 index)
933 {
934         int i;
935         struct kvm_cpuid_entry2 *best = NULL;
936
937         for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
938                 struct kvm_cpuid_entry2 *e;
939
940                 e = &vcpu->arch.cpuid_entries[i];
941                 if (is_matching_cpuid_entry(e, function, index)) {
942                         if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
943                                 move_to_next_stateful_cpuid_entry(vcpu, i);
944                         best = e;
945                         break;
946                 }
947         }
948         return best;
949 }
950 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
951
952 /*
953  * If no match is found, check whether we exceed the vCPU's limit
954  * and return the content of the highest valid _standard_ leaf instead.
955  * This is to satisfy the CPUID specification.
956  */
957 static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
958                                                   u32 function, u32 index)
959 {
960         struct kvm_cpuid_entry2 *maxlevel;
961
962         maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
963         if (!maxlevel || maxlevel->eax >= function)
964                 return NULL;
965         if (function & 0x80000000) {
966                 maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
967                 if (!maxlevel)
968                         return NULL;
969         }
970         return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
971 }
972
973 bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
974                u32 *ecx, u32 *edx, bool check_limit)
975 {
976         u32 function = *eax, index = *ecx;
977         struct kvm_cpuid_entry2 *best;
978         bool entry_found = true;
979
980         best = kvm_find_cpuid_entry(vcpu, function, index);
981
982         if (!best) {
983                 entry_found = false;
984                 if (!check_limit)
985                         goto out;
986
987                 best = check_cpuid_limit(vcpu, function, index);
988         }
989
990 out:
991         if (best) {
992                 *eax = best->eax;
993                 *ebx = best->ebx;
994                 *ecx = best->ecx;
995                 *edx = best->edx;
996         } else
997                 *eax = *ebx = *ecx = *edx = 0;
998         trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx, entry_found);
999         return entry_found;
1000 }
1001 EXPORT_SYMBOL_GPL(kvm_cpuid);
1002
1003 int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1004 {
1005         u32 eax, ebx, ecx, edx;
1006
1007         if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1008                 return 1;
1009
1010         eax = kvm_rax_read(vcpu);
1011         ecx = kvm_rcx_read(vcpu);
1012         kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, true);
1013         kvm_rax_write(vcpu, eax);
1014         kvm_rbx_write(vcpu, ebx);
1015         kvm_rcx_write(vcpu, ecx);
1016         kvm_rdx_write(vcpu, edx);
1017         return kvm_skip_emulated_instruction(vcpu);
1018 }
1019 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);