2 * i386 CPUID helper functions
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "sysemu/kvm.h"
26 #include "sysemu/cpus.h"
29 #include "qemu/option.h"
30 #include "qemu/config-file.h"
31 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "sysemu/arch_init.h"
39 #if defined(CONFIG_KVM)
40 #include <linux/kvm_para.h>
43 #include "sysemu/sysemu.h"
44 #ifndef CONFIG_USER_ONLY
46 #include "hw/sysbus.h"
47 #include "hw/apic_internal.h"
50 static void x86_cpu_vendor_words2str(char *dst
, uint32_t vendor1
,
51 uint32_t vendor2
, uint32_t vendor3
)
54 for (i
= 0; i
< 4; i
++) {
55 dst
[i
] = vendor1
>> (8 * i
);
56 dst
[i
+ 4] = vendor2
>> (8 * i
);
57 dst
[i
+ 8] = vendor3
>> (8 * i
);
59 dst
[CPUID_VENDOR_SZ
] = '\0';
62 /* feature flags taken from "Intel Processor Identification and the CPUID
63 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
64 * between feature naming conventions, aliases may be added.
66 static const char *feature_name
[] = {
67 "fpu", "vme", "de", "pse",
68 "tsc", "msr", "pae", "mce",
69 "cx8", "apic", NULL
, "sep",
70 "mtrr", "pge", "mca", "cmov",
71 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
72 NULL
, "ds" /* Intel dts */, "acpi", "mmx",
73 "fxsr", "sse", "sse2", "ss",
74 "ht" /* Intel htt */, "tm", "ia64", "pbe",
76 static const char *ext_feature_name
[] = {
77 "pni|sse3" /* Intel,AMD sse3 */, "pclmulqdq|pclmuldq", "dtes64", "monitor",
78 "ds_cpl", "vmx", "smx", "est",
79 "tm2", "ssse3", "cid", NULL
,
80 "fma", "cx16", "xtpr", "pdcm",
81 NULL
, "pcid", "dca", "sse4.1|sse4_1",
82 "sse4.2|sse4_2", "x2apic", "movbe", "popcnt",
83 "tsc-deadline", "aes", "xsave", "osxsave",
84 "avx", "f16c", "rdrand", "hypervisor",
86 /* Feature names that are already defined on feature_name[] but are set on
87 * CPUID[8000_0001].EDX on AMD CPUs don't have their names on
88 * ext2_feature_name[]. They are copied automatically to cpuid_ext2_features
89 * if and only if CPU vendor is AMD.
91 static const char *ext2_feature_name
[] = {
92 NULL
/* fpu */, NULL
/* vme */, NULL
/* de */, NULL
/* pse */,
93 NULL
/* tsc */, NULL
/* msr */, NULL
/* pae */, NULL
/* mce */,
94 NULL
/* cx8 */ /* AMD CMPXCHG8B */, NULL
/* apic */, NULL
, "syscall",
95 NULL
/* mtrr */, NULL
/* pge */, NULL
/* mca */, NULL
/* cmov */,
96 NULL
/* pat */, NULL
/* pse36 */, NULL
, NULL
/* Linux mp */,
97 "nx|xd", NULL
, "mmxext", NULL
/* mmx */,
98 NULL
/* fxsr */, "fxsr_opt|ffxsr", "pdpe1gb" /* AMD Page1GB */, "rdtscp",
99 NULL
, "lm|i64", "3dnowext", "3dnow",
101 static const char *ext3_feature_name
[] = {
102 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
103 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
104 "3dnowprefetch", "osvw", "ibs", "xop",
105 "skinit", "wdt", NULL
, "lwp",
106 "fma4", "tce", NULL
, "nodeid_msr",
107 NULL
, "tbm", "topoext", "perfctr_core",
108 "perfctr_nb", NULL
, NULL
, NULL
,
109 NULL
, NULL
, NULL
, NULL
,
112 static const char *ext4_feature_name
[] = {
113 NULL
, NULL
, "xstore", "xstore-en",
114 NULL
, NULL
, "xcrypt", "xcrypt-en",
115 "ace2", "ace2-en", "phe", "phe-en",
116 "pmm", "pmm-en", NULL
, NULL
,
117 NULL
, NULL
, NULL
, NULL
,
118 NULL
, NULL
, NULL
, NULL
,
119 NULL
, NULL
, NULL
, NULL
,
120 NULL
, NULL
, NULL
, NULL
,
123 static const char *kvm_feature_name
[] = {
124 "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock",
125 "kvm_asyncpf", "kvm_steal_time", "kvm_pv_eoi", NULL
,
126 NULL
, NULL
, NULL
, NULL
,
127 NULL
, NULL
, NULL
, NULL
,
128 NULL
, NULL
, NULL
, NULL
,
129 NULL
, NULL
, NULL
, NULL
,
130 NULL
, NULL
, NULL
, NULL
,
131 NULL
, NULL
, NULL
, NULL
,
134 static const char *svm_feature_name
[] = {
135 "npt", "lbrv", "svm_lock", "nrip_save",
136 "tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists",
137 NULL
, NULL
, "pause_filter", NULL
,
138 "pfthreshold", NULL
, NULL
, NULL
,
139 NULL
, NULL
, NULL
, NULL
,
140 NULL
, NULL
, NULL
, NULL
,
141 NULL
, NULL
, NULL
, NULL
,
142 NULL
, NULL
, NULL
, NULL
,
145 static const char *cpuid_7_0_ebx_feature_name
[] = {
146 "fsgsbase", NULL
, NULL
, "bmi1", "hle", "avx2", NULL
, "smep",
147 "bmi2", "erms", "invpcid", "rtm", NULL
, NULL
, NULL
, NULL
,
148 NULL
, NULL
, "rdseed", "adx", "smap", NULL
, NULL
, NULL
,
149 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
152 typedef struct FeatureWordInfo
{
153 const char **feat_names
;
154 uint32_t cpuid_eax
; /* Input EAX for CPUID */
155 int cpuid_reg
; /* R_* register constant */
158 static FeatureWordInfo feature_word_info
[FEATURE_WORDS
] = {
160 .feat_names
= feature_name
,
161 .cpuid_eax
= 1, .cpuid_reg
= R_EDX
,
164 .feat_names
= ext_feature_name
,
165 .cpuid_eax
= 1, .cpuid_reg
= R_ECX
,
167 [FEAT_8000_0001_EDX
] = {
168 .feat_names
= ext2_feature_name
,
169 .cpuid_eax
= 0x80000001, .cpuid_reg
= R_EDX
,
171 [FEAT_8000_0001_ECX
] = {
172 .feat_names
= ext3_feature_name
,
173 .cpuid_eax
= 0x80000001, .cpuid_reg
= R_ECX
,
175 [FEAT_C000_0001_EDX
] = {
176 .feat_names
= ext4_feature_name
,
177 .cpuid_eax
= 0xC0000001, .cpuid_reg
= R_EDX
,
180 .feat_names
= kvm_feature_name
,
181 .cpuid_eax
= KVM_CPUID_FEATURES
, .cpuid_reg
= R_EAX
,
184 .feat_names
= svm_feature_name
,
185 .cpuid_eax
= 0x8000000A, .cpuid_reg
= R_EDX
,
188 .feat_names
= cpuid_7_0_ebx_feature_name
,
189 .cpuid_eax
= 7, .cpuid_reg
= R_EBX
,
193 const char *get_register_name_32(unsigned int reg
)
195 static const char *reg_names
[CPU_NB_REGS32
] = {
206 if (reg
> CPU_NB_REGS32
) {
209 return reg_names
[reg
];
212 /* collects per-function cpuid data
214 typedef struct model_features_t
{
215 uint32_t *guest_feat
;
217 FeatureWord feat_word
;
221 int enforce_cpuid
= 0;
223 static uint32_t kvm_default_features
= (1 << KVM_FEATURE_CLOCKSOURCE
) |
224 (1 << KVM_FEATURE_NOP_IO_DELAY
) |
225 (1 << KVM_FEATURE_CLOCKSOURCE2
) |
226 (1 << KVM_FEATURE_ASYNC_PF
) |
227 (1 << KVM_FEATURE_STEAL_TIME
) |
228 (1 << KVM_FEATURE_PV_EOI
) |
229 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT
);
231 void disable_kvm_pv_eoi(void)
233 kvm_default_features
&= ~(1UL << KVM_FEATURE_PV_EOI
);
236 void host_cpuid(uint32_t function
, uint32_t count
,
237 uint32_t *eax
, uint32_t *ebx
, uint32_t *ecx
, uint32_t *edx
)
239 #if defined(CONFIG_KVM)
244 : "=a"(vec
[0]), "=b"(vec
[1]),
245 "=c"(vec
[2]), "=d"(vec
[3])
246 : "0"(function
), "c"(count
) : "cc");
248 asm volatile("pusha \n\t"
250 "mov %%eax, 0(%2) \n\t"
251 "mov %%ebx, 4(%2) \n\t"
252 "mov %%ecx, 8(%2) \n\t"
253 "mov %%edx, 12(%2) \n\t"
255 : : "a"(function
), "c"(count
), "S"(vec
)
270 #define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
272 /* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
273 * a substring. ex if !NULL points to the first char after a substring,
274 * otherwise the string is assumed to sized by a terminating nul.
275 * Return lexical ordering of *s1:*s2.
277 static int sstrcmp(const char *s1
, const char *e1
, const char *s2
,
281 if (!*s1
|| !*s2
|| *s1
!= *s2
)
284 if (s1
== e1
&& s2
== e2
)
293 /* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
294 * '|' delimited (possibly empty) strings in which case search for a match
295 * within the alternatives proceeds left to right. Return 0 for success,
296 * non-zero otherwise.
298 static int altcmp(const char *s
, const char *e
, const char *altstr
)
302 for (q
= p
= altstr
; ; ) {
303 while (*p
&& *p
!= '|')
305 if ((q
== p
&& !*s
) || (q
!= p
&& !sstrcmp(s
, e
, q
, p
)))
314 /* search featureset for flag *[s..e), if found set corresponding bit in
315 * *pval and return true, otherwise return false
317 static bool lookup_feature(uint32_t *pval
, const char *s
, const char *e
,
318 const char **featureset
)
324 for (mask
= 1, ppc
= featureset
; mask
; mask
<<= 1, ++ppc
) {
325 if (*ppc
&& !altcmp(s
, e
, *ppc
)) {
333 static void add_flagname_to_bitmaps(const char *flagname
,
334 FeatureWordArray words
)
337 for (w
= 0; w
< FEATURE_WORDS
; w
++) {
338 FeatureWordInfo
*wi
= &feature_word_info
[w
];
339 if (wi
->feat_names
&&
340 lookup_feature(&words
[w
], flagname
, NULL
, wi
->feat_names
)) {
344 if (w
== FEATURE_WORDS
) {
345 fprintf(stderr
, "CPU feature %s not found\n", flagname
);
349 typedef struct x86_def_t
{
352 /* vendor is zero-terminated, 12 character ASCII string */
353 char vendor
[CPUID_VENDOR_SZ
+ 1];
358 uint32_t features
, ext_features
, ext2_features
, ext3_features
;
359 uint32_t kvm_features
, svm_features
;
362 /* Store the results of Centaur's CPUID instructions */
363 uint32_t ext4_features
;
365 /* The feature bits on CPUID[EAX=7,ECX=0].EBX */
366 uint32_t cpuid_7_0_ebx_features
;
369 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
370 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
371 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
372 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
373 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
374 CPUID_PSE36 | CPUID_FXSR)
375 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
376 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
377 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
378 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
379 CPUID_PAE | CPUID_SEP | CPUID_APIC)
381 #define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
382 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
383 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
384 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
385 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
386 /* partly implemented:
387 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
388 CPUID_PSE36 (needed for Solaris) */
390 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
391 #define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \
392 CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT | \
393 CPUID_EXT_HYPERVISOR)
395 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
396 CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_XSAVE */
397 #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
398 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
399 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
401 CPUID_EXT2_PDPE1GB */
402 #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
403 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
404 #define TCG_SVM_FEATURES 0
405 #define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP)
407 /* built-in CPU model definitions
409 static x86_def_t builtin_x86_defs
[] = {
413 .vendor
= CPUID_VENDOR_AMD
,
417 .features
= PPRO_FEATURES
|
418 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
420 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
| CPUID_EXT_POPCNT
,
421 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
422 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
423 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
424 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
425 .xlevel
= 0x8000000A,
430 .vendor
= CPUID_VENDOR_AMD
,
434 .features
= PPRO_FEATURES
|
435 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
436 CPUID_PSE36
| CPUID_VME
| CPUID_HT
,
437 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_CX16
|
439 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
440 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
441 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
| CPUID_EXT2_MMXEXT
|
442 CPUID_EXT2_FFXSR
| CPUID_EXT2_PDPE1GB
| CPUID_EXT2_RDTSCP
,
443 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
445 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
446 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
447 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
448 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
449 .svm_features
= CPUID_SVM_NPT
| CPUID_SVM_LBRV
,
450 .xlevel
= 0x8000001A,
451 .model_id
= "AMD Phenom(tm) 9550 Quad-Core Processor"
456 .vendor
= CPUID_VENDOR_INTEL
,
460 .features
= PPRO_FEATURES
|
461 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
462 CPUID_PSE36
| CPUID_VME
| CPUID_DTS
| CPUID_ACPI
| CPUID_SS
|
463 CPUID_HT
| CPUID_TM
| CPUID_PBE
,
464 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
465 CPUID_EXT_DTES64
| CPUID_EXT_DSCPL
| CPUID_EXT_VMX
| CPUID_EXT_EST
|
466 CPUID_EXT_TM2
| CPUID_EXT_CX16
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
467 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
468 .ext3_features
= CPUID_EXT3_LAHF_LM
,
469 .xlevel
= 0x80000008,
470 .model_id
= "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
475 .vendor
= CPUID_VENDOR_INTEL
,
479 /* Missing: CPUID_VME, CPUID_HT */
480 .features
= PPRO_FEATURES
|
481 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
483 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
484 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
,
485 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
486 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
487 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
488 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
489 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
490 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
491 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
493 .xlevel
= 0x80000008,
494 .model_id
= "Common KVM processor"
499 .vendor
= CPUID_VENDOR_INTEL
,
503 .features
= PPRO_FEATURES
,
504 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_POPCNT
,
505 .xlevel
= 0x80000004,
510 .vendor
= CPUID_VENDOR_INTEL
,
514 .features
= PPRO_FEATURES
|
515 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_PSE36
,
516 .ext_features
= CPUID_EXT_SSE3
,
517 .ext2_features
= PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
,
519 .xlevel
= 0x80000008,
520 .model_id
= "Common 32-bit KVM processor"
525 .vendor
= CPUID_VENDOR_INTEL
,
529 .features
= PPRO_FEATURES
| CPUID_VME
|
530 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_DTS
| CPUID_ACPI
|
531 CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
532 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_VMX
|
533 CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
534 .ext2_features
= CPUID_EXT2_NX
,
535 .xlevel
= 0x80000008,
536 .model_id
= "Genuine Intel(R) CPU T2600 @ 2.16GHz",
541 .vendor
= CPUID_VENDOR_INTEL
,
545 .features
= I486_FEATURES
,
551 .vendor
= CPUID_VENDOR_INTEL
,
555 .features
= PENTIUM_FEATURES
,
561 .vendor
= CPUID_VENDOR_INTEL
,
565 .features
= PENTIUM2_FEATURES
,
571 .vendor
= CPUID_VENDOR_INTEL
,
575 .features
= PENTIUM3_FEATURES
,
581 .vendor
= CPUID_VENDOR_AMD
,
585 .features
= PPRO_FEATURES
| CPUID_PSE36
| CPUID_VME
| CPUID_MTRR
|
587 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
588 CPUID_EXT2_MMXEXT
| CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
589 .xlevel
= 0x80000008,
593 /* original is on level 10 */
595 .vendor
= CPUID_VENDOR_INTEL
,
599 .features
= PPRO_FEATURES
|
600 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_VME
| CPUID_DTS
|
601 CPUID_ACPI
| CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
602 /* Some CPUs got no CPUID_SEP */
603 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
604 CPUID_EXT_DSCPL
| CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
,
605 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
607 .ext3_features
= CPUID_EXT3_LAHF_LM
,
608 .xlevel
= 0x8000000A,
609 .model_id
= "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
614 .vendor
= CPUID_VENDOR_INTEL
,
618 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
619 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
620 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
621 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
622 CPUID_DE
| CPUID_FP87
,
623 .ext_features
= CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
624 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
625 .ext3_features
= CPUID_EXT3_LAHF_LM
,
626 .xlevel
= 0x8000000A,
627 .model_id
= "Intel Celeron_4x0 (Conroe/Merom Class Core 2)",
632 .vendor
= CPUID_VENDOR_INTEL
,
636 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
637 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
638 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
639 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
640 CPUID_DE
| CPUID_FP87
,
641 .ext_features
= CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
643 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
644 .ext3_features
= CPUID_EXT3_LAHF_LM
,
645 .xlevel
= 0x8000000A,
646 .model_id
= "Intel Core 2 Duo P9xxx (Penryn Class Core 2)",
651 .vendor
= CPUID_VENDOR_INTEL
,
655 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
656 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
657 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
658 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
659 CPUID_DE
| CPUID_FP87
,
660 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
661 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
662 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
663 .ext3_features
= CPUID_EXT3_LAHF_LM
,
664 .xlevel
= 0x8000000A,
665 .model_id
= "Intel Core i7 9xx (Nehalem Class Core i7)",
670 .vendor
= CPUID_VENDOR_INTEL
,
674 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
675 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
676 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
677 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
678 CPUID_DE
| CPUID_FP87
,
679 .ext_features
= CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
680 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
682 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
683 .ext3_features
= CPUID_EXT3_LAHF_LM
,
684 .xlevel
= 0x8000000A,
685 .model_id
= "Westmere E56xx/L56xx/X56xx (Nehalem-C)",
688 .name
= "SandyBridge",
690 .vendor
= CPUID_VENDOR_INTEL
,
694 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
695 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
696 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
697 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
698 CPUID_DE
| CPUID_FP87
,
699 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
700 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_POPCNT
|
701 CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
702 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
704 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
706 .ext3_features
= CPUID_EXT3_LAHF_LM
,
707 .xlevel
= 0x8000000A,
708 .model_id
= "Intel Xeon E312xx (Sandy Bridge)",
713 .vendor
= CPUID_VENDOR_INTEL
,
717 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
718 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
719 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
720 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
721 CPUID_DE
| CPUID_FP87
,
722 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
723 CPUID_EXT_POPCNT
| CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
|
724 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
725 CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
|
726 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_FMA
| CPUID_EXT_MOVBE
|
728 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
730 .ext3_features
= CPUID_EXT3_LAHF_LM
,
731 .cpuid_7_0_ebx_features
= CPUID_7_0_EBX_FSGSBASE
| CPUID_7_0_EBX_BMI1
|
732 CPUID_7_0_EBX_HLE
| CPUID_7_0_EBX_AVX2
| CPUID_7_0_EBX_SMEP
|
733 CPUID_7_0_EBX_BMI2
| CPUID_7_0_EBX_ERMS
| CPUID_7_0_EBX_INVPCID
|
735 .xlevel
= 0x8000000A,
736 .model_id
= "Intel Core Processor (Haswell)",
739 .name
= "Opteron_G1",
741 .vendor
= CPUID_VENDOR_AMD
,
745 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
746 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
747 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
748 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
749 CPUID_DE
| CPUID_FP87
,
750 .ext_features
= CPUID_EXT_SSE3
,
751 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
752 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
753 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
754 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
755 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
756 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
757 .xlevel
= 0x80000008,
758 .model_id
= "AMD Opteron 240 (Gen 1 Class Opteron)",
761 .name
= "Opteron_G2",
763 .vendor
= CPUID_VENDOR_AMD
,
767 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
768 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
769 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
770 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
771 CPUID_DE
| CPUID_FP87
,
772 .ext_features
= CPUID_EXT_CX16
| CPUID_EXT_SSE3
,
773 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
774 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
775 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
776 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
777 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
778 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
779 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
780 .ext3_features
= CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
781 .xlevel
= 0x80000008,
782 .model_id
= "AMD Opteron 22xx (Gen 2 Class Opteron)",
785 .name
= "Opteron_G3",
787 .vendor
= CPUID_VENDOR_AMD
,
791 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
792 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
793 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
794 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
795 CPUID_DE
| CPUID_FP87
,
796 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_CX16
| CPUID_EXT_MONITOR
|
798 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
799 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
800 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
801 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
802 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
803 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
804 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
805 .ext3_features
= CPUID_EXT3_MISALIGNSSE
| CPUID_EXT3_SSE4A
|
806 CPUID_EXT3_ABM
| CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
807 .xlevel
= 0x80000008,
808 .model_id
= "AMD Opteron 23xx (Gen 3 Class Opteron)",
811 .name
= "Opteron_G4",
813 .vendor
= CPUID_VENDOR_AMD
,
817 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
818 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
819 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
820 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
821 CPUID_DE
| CPUID_FP87
,
822 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
823 CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
824 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
826 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
827 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
828 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
829 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
830 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
831 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
832 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
833 .ext3_features
= CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
834 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
835 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
837 .xlevel
= 0x8000001A,
838 .model_id
= "AMD Opteron 62xx class CPU",
841 .name
= "Opteron_G5",
843 .vendor
= CPUID_VENDOR_AMD
,
847 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
848 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
849 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
850 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
851 CPUID_DE
| CPUID_FP87
,
852 .ext_features
= CPUID_EXT_F16C
| CPUID_EXT_AVX
| CPUID_EXT_XSAVE
|
853 CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
854 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_FMA
|
855 CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
,
856 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
857 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
858 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
859 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
860 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
861 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
862 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
863 .ext3_features
= CPUID_EXT3_TBM
| CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
864 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
865 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
867 .xlevel
= 0x8000001A,
868 .model_id
= "AMD Opteron 63xx class CPU",
873 static int cpu_x86_fill_model_id(char *str
)
875 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
878 for (i
= 0; i
< 3; i
++) {
879 host_cpuid(0x80000002 + i
, 0, &eax
, &ebx
, &ecx
, &edx
);
880 memcpy(str
+ i
* 16 + 0, &eax
, 4);
881 memcpy(str
+ i
* 16 + 4, &ebx
, 4);
882 memcpy(str
+ i
* 16 + 8, &ecx
, 4);
883 memcpy(str
+ i
* 16 + 12, &edx
, 4);
889 /* Fill a x86_def_t struct with information about the host CPU, and
890 * the CPU features supported by the host hardware + host kernel
892 * This function may be called only if KVM is enabled.
894 static void kvm_cpu_fill_host(x86_def_t
*x86_cpu_def
)
897 KVMState
*s
= kvm_state
;
898 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
900 assert(kvm_enabled());
902 x86_cpu_def
->name
= "host";
903 host_cpuid(0x0, 0, &eax
, &ebx
, &ecx
, &edx
);
904 x86_cpu_vendor_words2str(x86_cpu_def
->vendor
, ebx
, edx
, ecx
);
906 host_cpuid(0x1, 0, &eax
, &ebx
, &ecx
, &edx
);
907 x86_cpu_def
->family
= ((eax
>> 8) & 0x0F) + ((eax
>> 20) & 0xFF);
908 x86_cpu_def
->model
= ((eax
>> 4) & 0x0F) | ((eax
& 0xF0000) >> 12);
909 x86_cpu_def
->stepping
= eax
& 0x0F;
911 x86_cpu_def
->level
= kvm_arch_get_supported_cpuid(s
, 0x0, 0, R_EAX
);
912 x86_cpu_def
->features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_EDX
);
913 x86_cpu_def
->ext_features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_ECX
);
915 if (x86_cpu_def
->level
>= 7) {
916 x86_cpu_def
->cpuid_7_0_ebx_features
=
917 kvm_arch_get_supported_cpuid(s
, 0x7, 0, R_EBX
);
919 x86_cpu_def
->cpuid_7_0_ebx_features
= 0;
922 x86_cpu_def
->xlevel
= kvm_arch_get_supported_cpuid(s
, 0x80000000, 0, R_EAX
);
923 x86_cpu_def
->ext2_features
=
924 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
925 x86_cpu_def
->ext3_features
=
926 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
928 cpu_x86_fill_model_id(x86_cpu_def
->model_id
);
930 /* Call Centaur's CPUID instruction. */
931 if (!strcmp(x86_cpu_def
->vendor
, CPUID_VENDOR_VIA
)) {
932 host_cpuid(0xC0000000, 0, &eax
, &ebx
, &ecx
, &edx
);
933 eax
= kvm_arch_get_supported_cpuid(s
, 0xC0000000, 0, R_EAX
);
934 if (eax
>= 0xC0000001) {
935 /* Support VIA max extended level */
936 x86_cpu_def
->xlevel2
= eax
;
937 host_cpuid(0xC0000001, 0, &eax
, &ebx
, &ecx
, &edx
);
938 x86_cpu_def
->ext4_features
=
939 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
943 /* Other KVM-specific feature fields: */
944 x86_cpu_def
->svm_features
=
945 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
946 x86_cpu_def
->kvm_features
=
947 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
949 #endif /* CONFIG_KVM */
952 static int unavailable_host_feature(FeatureWordInfo
*f
, uint32_t mask
)
956 for (i
= 0; i
< 32; ++i
)
958 const char *reg
= get_register_name_32(f
->cpuid_reg
);
960 fprintf(stderr
, "warning: host doesn't support requested feature: "
961 "CPUID.%02XH:%s%s%s [bit %d]\n",
963 f
->feat_names
[i
] ? "." : "",
964 f
->feat_names
[i
] ? f
->feat_names
[i
] : "", i
);
970 /* Check if all requested cpu flags are making their way to the guest
972 * Returns 0 if all flags are supported by the host, non-zero otherwise.
974 * This function may be called only if KVM is enabled.
976 static int kvm_check_features_against_host(X86CPU
*cpu
)
978 CPUX86State
*env
= &cpu
->env
;
982 struct model_features_t ft
[] = {
983 {&env
->cpuid_features
, &host_def
.features
,
985 {&env
->cpuid_ext_features
, &host_def
.ext_features
,
987 {&env
->cpuid_ext2_features
, &host_def
.ext2_features
,
988 FEAT_8000_0001_EDX
},
989 {&env
->cpuid_ext3_features
, &host_def
.ext3_features
,
990 FEAT_8000_0001_ECX
},
991 {&env
->cpuid_ext4_features
, &host_def
.ext4_features
,
992 FEAT_C000_0001_EDX
},
993 {&env
->cpuid_7_0_ebx_features
, &host_def
.cpuid_7_0_ebx_features
,
995 {&env
->cpuid_svm_features
, &host_def
.svm_features
,
997 {&env
->cpuid_kvm_features
, &host_def
.kvm_features
,
1001 assert(kvm_enabled());
1003 kvm_cpu_fill_host(&host_def
);
1004 for (rv
= 0, i
= 0; i
< ARRAY_SIZE(ft
); ++i
) {
1005 FeatureWord w
= ft
[i
].feat_word
;
1006 FeatureWordInfo
*wi
= &feature_word_info
[w
];
1007 for (mask
= 1; mask
; mask
<<= 1) {
1008 if (*ft
[i
].guest_feat
& mask
&&
1009 !(*ft
[i
].host_feat
& mask
)) {
1010 unavailable_host_feature(wi
, mask
);
1018 static void x86_cpuid_version_get_family(Object
*obj
, Visitor
*v
, void *opaque
,
1019 const char *name
, Error
**errp
)
1021 X86CPU
*cpu
= X86_CPU(obj
);
1022 CPUX86State
*env
= &cpu
->env
;
1025 value
= (env
->cpuid_version
>> 8) & 0xf;
1027 value
+= (env
->cpuid_version
>> 20) & 0xff;
1029 visit_type_int(v
, &value
, name
, errp
);
1032 static void x86_cpuid_version_set_family(Object
*obj
, Visitor
*v
, void *opaque
,
1033 const char *name
, Error
**errp
)
1035 X86CPU
*cpu
= X86_CPU(obj
);
1036 CPUX86State
*env
= &cpu
->env
;
1037 const int64_t min
= 0;
1038 const int64_t max
= 0xff + 0xf;
1041 visit_type_int(v
, &value
, name
, errp
);
1042 if (error_is_set(errp
)) {
1045 if (value
< min
|| value
> max
) {
1046 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1047 name
? name
: "null", value
, min
, max
);
1051 env
->cpuid_version
&= ~0xff00f00;
1053 env
->cpuid_version
|= 0xf00 | ((value
- 0x0f) << 20);
1055 env
->cpuid_version
|= value
<< 8;
1059 static void x86_cpuid_version_get_model(Object
*obj
, Visitor
*v
, void *opaque
,
1060 const char *name
, Error
**errp
)
1062 X86CPU
*cpu
= X86_CPU(obj
);
1063 CPUX86State
*env
= &cpu
->env
;
1066 value
= (env
->cpuid_version
>> 4) & 0xf;
1067 value
|= ((env
->cpuid_version
>> 16) & 0xf) << 4;
1068 visit_type_int(v
, &value
, name
, errp
);
1071 static void x86_cpuid_version_set_model(Object
*obj
, Visitor
*v
, void *opaque
,
1072 const char *name
, Error
**errp
)
1074 X86CPU
*cpu
= X86_CPU(obj
);
1075 CPUX86State
*env
= &cpu
->env
;
1076 const int64_t min
= 0;
1077 const int64_t max
= 0xff;
1080 visit_type_int(v
, &value
, name
, errp
);
1081 if (error_is_set(errp
)) {
1084 if (value
< min
|| value
> max
) {
1085 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1086 name
? name
: "null", value
, min
, max
);
1090 env
->cpuid_version
&= ~0xf00f0;
1091 env
->cpuid_version
|= ((value
& 0xf) << 4) | ((value
>> 4) << 16);
1094 static void x86_cpuid_version_get_stepping(Object
*obj
, Visitor
*v
,
1095 void *opaque
, const char *name
,
1098 X86CPU
*cpu
= X86_CPU(obj
);
1099 CPUX86State
*env
= &cpu
->env
;
1102 value
= env
->cpuid_version
& 0xf;
1103 visit_type_int(v
, &value
, name
, errp
);
1106 static void x86_cpuid_version_set_stepping(Object
*obj
, Visitor
*v
,
1107 void *opaque
, const char *name
,
1110 X86CPU
*cpu
= X86_CPU(obj
);
1111 CPUX86State
*env
= &cpu
->env
;
1112 const int64_t min
= 0;
1113 const int64_t max
= 0xf;
1116 visit_type_int(v
, &value
, name
, errp
);
1117 if (error_is_set(errp
)) {
1120 if (value
< min
|| value
> max
) {
1121 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1122 name
? name
: "null", value
, min
, max
);
1126 env
->cpuid_version
&= ~0xf;
1127 env
->cpuid_version
|= value
& 0xf;
1130 static void x86_cpuid_get_level(Object
*obj
, Visitor
*v
, void *opaque
,
1131 const char *name
, Error
**errp
)
1133 X86CPU
*cpu
= X86_CPU(obj
);
1135 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1138 static void x86_cpuid_set_level(Object
*obj
, Visitor
*v
, void *opaque
,
1139 const char *name
, Error
**errp
)
1141 X86CPU
*cpu
= X86_CPU(obj
);
1143 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1146 static void x86_cpuid_get_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1147 const char *name
, Error
**errp
)
1149 X86CPU
*cpu
= X86_CPU(obj
);
1151 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1154 static void x86_cpuid_set_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1155 const char *name
, Error
**errp
)
1157 X86CPU
*cpu
= X86_CPU(obj
);
1159 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1162 static char *x86_cpuid_get_vendor(Object
*obj
, Error
**errp
)
1164 X86CPU
*cpu
= X86_CPU(obj
);
1165 CPUX86State
*env
= &cpu
->env
;
1168 value
= (char *)g_malloc(CPUID_VENDOR_SZ
+ 1);
1169 x86_cpu_vendor_words2str(value
, env
->cpuid_vendor1
, env
->cpuid_vendor2
,
1170 env
->cpuid_vendor3
);
1174 static void x86_cpuid_set_vendor(Object
*obj
, const char *value
,
1177 X86CPU
*cpu
= X86_CPU(obj
);
1178 CPUX86State
*env
= &cpu
->env
;
1181 if (strlen(value
) != CPUID_VENDOR_SZ
) {
1182 error_set(errp
, QERR_PROPERTY_VALUE_BAD
, "",
1187 env
->cpuid_vendor1
= 0;
1188 env
->cpuid_vendor2
= 0;
1189 env
->cpuid_vendor3
= 0;
1190 for (i
= 0; i
< 4; i
++) {
1191 env
->cpuid_vendor1
|= ((uint8_t)value
[i
]) << (8 * i
);
1192 env
->cpuid_vendor2
|= ((uint8_t)value
[i
+ 4]) << (8 * i
);
1193 env
->cpuid_vendor3
|= ((uint8_t)value
[i
+ 8]) << (8 * i
);
1197 static char *x86_cpuid_get_model_id(Object
*obj
, Error
**errp
)
1199 X86CPU
*cpu
= X86_CPU(obj
);
1200 CPUX86State
*env
= &cpu
->env
;
1204 value
= g_malloc(48 + 1);
1205 for (i
= 0; i
< 48; i
++) {
1206 value
[i
] = env
->cpuid_model
[i
>> 2] >> (8 * (i
& 3));
1212 static void x86_cpuid_set_model_id(Object
*obj
, const char *model_id
,
1215 X86CPU
*cpu
= X86_CPU(obj
);
1216 CPUX86State
*env
= &cpu
->env
;
1219 if (model_id
== NULL
) {
1222 len
= strlen(model_id
);
1223 memset(env
->cpuid_model
, 0, 48);
1224 for (i
= 0; i
< 48; i
++) {
1228 c
= (uint8_t)model_id
[i
];
1230 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
1234 static void x86_cpuid_get_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1235 const char *name
, Error
**errp
)
1237 X86CPU
*cpu
= X86_CPU(obj
);
1240 value
= cpu
->env
.tsc_khz
* 1000;
1241 visit_type_int(v
, &value
, name
, errp
);
1244 static void x86_cpuid_set_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1245 const char *name
, Error
**errp
)
1247 X86CPU
*cpu
= X86_CPU(obj
);
1248 const int64_t min
= 0;
1249 const int64_t max
= INT64_MAX
;
1252 visit_type_int(v
, &value
, name
, errp
);
1253 if (error_is_set(errp
)) {
1256 if (value
< min
|| value
> max
) {
1257 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1258 name
? name
: "null", value
, min
, max
);
1262 cpu
->env
.tsc_khz
= value
/ 1000;
1265 static int cpu_x86_find_by_name(x86_def_t
*x86_cpu_def
, const char *name
)
1273 if (kvm_enabled() && strcmp(name
, "host") == 0) {
1274 kvm_cpu_fill_host(x86_cpu_def
);
1278 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); i
++) {
1279 def
= &builtin_x86_defs
[i
];
1280 if (strcmp(name
, def
->name
) == 0) {
1281 memcpy(x86_cpu_def
, def
, sizeof(*def
));
1282 /* sysenter isn't supported in compatibility mode on AMD,
1283 * syscall isn't supported in compatibility mode on Intel.
1284 * Normally we advertise the actual CPU vendor, but you can
1285 * override this using the 'vendor' property if you want to use
1286 * KVM's sysenter/syscall emulation in compatibility mode and
1287 * when doing cross vendor migration
1289 if (kvm_enabled()) {
1290 uint32_t ebx
= 0, ecx
= 0, edx
= 0;
1291 host_cpuid(0, 0, NULL
, &ebx
, &ecx
, &edx
);
1292 x86_cpu_vendor_words2str(x86_cpu_def
->vendor
, ebx
, edx
, ecx
);
1301 /* Parse "+feature,-feature,feature=foo" CPU feature string
1303 static int cpu_x86_parse_featurestr(x86_def_t
*x86_cpu_def
, char *features
)
1305 char *featurestr
; /* Single 'key=value" string being parsed */
1306 /* Features to be added */
1307 FeatureWordArray plus_features
= { 0 };
1308 /* Features to be removed */
1309 FeatureWordArray minus_features
= { 0 };
1312 featurestr
= features
? strtok(features
, ",") : NULL
;
1314 while (featurestr
) {
1316 if (featurestr
[0] == '+') {
1317 add_flagname_to_bitmaps(featurestr
+ 1, plus_features
);
1318 } else if (featurestr
[0] == '-') {
1319 add_flagname_to_bitmaps(featurestr
+ 1, minus_features
);
1320 } else if ((val
= strchr(featurestr
, '='))) {
1322 if (!strcmp(featurestr
, "family")) {
1324 numvalue
= strtoul(val
, &err
, 0);
1325 if (!*val
|| *err
|| numvalue
> 0xff + 0xf) {
1326 fprintf(stderr
, "bad numerical value %s\n", val
);
1329 x86_cpu_def
->family
= numvalue
;
1330 } else if (!strcmp(featurestr
, "model")) {
1332 numvalue
= strtoul(val
, &err
, 0);
1333 if (!*val
|| *err
|| numvalue
> 0xff) {
1334 fprintf(stderr
, "bad numerical value %s\n", val
);
1337 x86_cpu_def
->model
= numvalue
;
1338 } else if (!strcmp(featurestr
, "stepping")) {
1340 numvalue
= strtoul(val
, &err
, 0);
1341 if (!*val
|| *err
|| numvalue
> 0xf) {
1342 fprintf(stderr
, "bad numerical value %s\n", val
);
1345 x86_cpu_def
->stepping
= numvalue
;
1346 } else if (!strcmp(featurestr
, "level")) {
1348 numvalue
= strtoul(val
, &err
, 0);
1349 if (!*val
|| *err
) {
1350 fprintf(stderr
, "bad numerical value %s\n", val
);
1353 x86_cpu_def
->level
= numvalue
;
1354 } else if (!strcmp(featurestr
, "xlevel")) {
1356 numvalue
= strtoul(val
, &err
, 0);
1357 if (!*val
|| *err
) {
1358 fprintf(stderr
, "bad numerical value %s\n", val
);
1361 if (numvalue
< 0x80000000) {
1362 fprintf(stderr
, "xlevel value shall always be >= 0x80000000"
1363 ", fixup will be removed in future versions\n");
1364 numvalue
+= 0x80000000;
1366 x86_cpu_def
->xlevel
= numvalue
;
1367 } else if (!strcmp(featurestr
, "vendor")) {
1368 pstrcpy(x86_cpu_def
->vendor
, sizeof(x86_cpu_def
->vendor
), val
);
1369 } else if (!strcmp(featurestr
, "model_id")) {
1370 pstrcpy(x86_cpu_def
->model_id
, sizeof(x86_cpu_def
->model_id
),
1372 } else if (!strcmp(featurestr
, "tsc_freq")) {
1376 tsc_freq
= strtosz_suffix_unit(val
, &err
,
1377 STRTOSZ_DEFSUFFIX_B
, 1000);
1378 if (tsc_freq
< 0 || *err
) {
1379 fprintf(stderr
, "bad numerical value %s\n", val
);
1382 x86_cpu_def
->tsc_khz
= tsc_freq
/ 1000;
1383 } else if (!strcmp(featurestr
, "hv_spinlocks")) {
1385 numvalue
= strtoul(val
, &err
, 0);
1386 if (!*val
|| *err
) {
1387 fprintf(stderr
, "bad numerical value %s\n", val
);
1390 hyperv_set_spinlock_retries(numvalue
);
1392 fprintf(stderr
, "unrecognized feature %s\n", featurestr
);
1395 } else if (!strcmp(featurestr
, "check")) {
1397 } else if (!strcmp(featurestr
, "enforce")) {
1398 check_cpuid
= enforce_cpuid
= 1;
1399 } else if (!strcmp(featurestr
, "hv_relaxed")) {
1400 hyperv_enable_relaxed_timing(true);
1401 } else if (!strcmp(featurestr
, "hv_vapic")) {
1402 hyperv_enable_vapic_recommended(true);
1404 fprintf(stderr
, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr
);
1407 featurestr
= strtok(NULL
, ",");
1409 x86_cpu_def
->features
|= plus_features
[FEAT_1_EDX
];
1410 x86_cpu_def
->ext_features
|= plus_features
[FEAT_1_ECX
];
1411 x86_cpu_def
->ext2_features
|= plus_features
[FEAT_8000_0001_EDX
];
1412 x86_cpu_def
->ext3_features
|= plus_features
[FEAT_8000_0001_ECX
];
1413 x86_cpu_def
->ext4_features
|= plus_features
[FEAT_C000_0001_EDX
];
1414 x86_cpu_def
->kvm_features
|= plus_features
[FEAT_KVM
];
1415 x86_cpu_def
->svm_features
|= plus_features
[FEAT_SVM
];
1416 x86_cpu_def
->cpuid_7_0_ebx_features
|= plus_features
[FEAT_7_0_EBX
];
1417 x86_cpu_def
->features
&= ~minus_features
[FEAT_1_EDX
];
1418 x86_cpu_def
->ext_features
&= ~minus_features
[FEAT_1_ECX
];
1419 x86_cpu_def
->ext2_features
&= ~minus_features
[FEAT_8000_0001_EDX
];
1420 x86_cpu_def
->ext3_features
&= ~minus_features
[FEAT_8000_0001_ECX
];
1421 x86_cpu_def
->ext4_features
&= ~minus_features
[FEAT_C000_0001_EDX
];
1422 x86_cpu_def
->kvm_features
&= ~minus_features
[FEAT_KVM
];
1423 x86_cpu_def
->svm_features
&= ~minus_features
[FEAT_SVM
];
1424 x86_cpu_def
->cpuid_7_0_ebx_features
&= ~minus_features
[FEAT_7_0_EBX
];
1431 /* generate a composite string into buf of all cpuid names in featureset
1432 * selected by fbits. indicate truncation at bufsize in the event of overflow.
1433 * if flags, suppress names undefined in featureset.
1435 static void listflags(char *buf
, int bufsize
, uint32_t fbits
,
1436 const char **featureset
, uint32_t flags
)
1438 const char **p
= &featureset
[31];
1442 b
= 4 <= bufsize
? buf
+ (bufsize
-= 3) - 1 : NULL
;
1444 for (q
= buf
, bit
= 31; fbits
&& bufsize
; --p
, fbits
&= ~(1 << bit
), --bit
)
1445 if (fbits
& 1 << bit
&& (*p
|| !flags
)) {
1447 nc
= snprintf(q
, bufsize
, "%s%s", q
== buf
? "" : " ", *p
);
1449 nc
= snprintf(q
, bufsize
, "%s[%d]", q
== buf
? "" : " ", bit
);
1450 if (bufsize
<= nc
) {
1452 memcpy(b
, "...", sizeof("..."));
1461 /* generate CPU information. */
1462 void x86_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
1468 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); i
++) {
1469 def
= &builtin_x86_defs
[i
];
1470 snprintf(buf
, sizeof(buf
), "%s", def
->name
);
1471 (*cpu_fprintf
)(f
, "x86 %16s %-48s\n", buf
, def
->model_id
);
1473 if (kvm_enabled()) {
1474 (*cpu_fprintf
)(f
, "x86 %16s\n", "[host]");
1476 (*cpu_fprintf
)(f
, "\nRecognized CPUID flags:\n");
1477 listflags(buf
, sizeof(buf
), (uint32_t)~0, feature_name
, 1);
1478 (*cpu_fprintf
)(f
, " %s\n", buf
);
1479 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext_feature_name
, 1);
1480 (*cpu_fprintf
)(f
, " %s\n", buf
);
1481 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext2_feature_name
, 1);
1482 (*cpu_fprintf
)(f
, " %s\n", buf
);
1483 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext3_feature_name
, 1);
1484 (*cpu_fprintf
)(f
, " %s\n", buf
);
1487 CpuDefinitionInfoList
*arch_query_cpu_definitions(Error
**errp
)
1489 CpuDefinitionInfoList
*cpu_list
= NULL
;
1493 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); i
++) {
1494 CpuDefinitionInfoList
*entry
;
1495 CpuDefinitionInfo
*info
;
1497 def
= &builtin_x86_defs
[i
];
1498 info
= g_malloc0(sizeof(*info
));
1499 info
->name
= g_strdup(def
->name
);
1501 entry
= g_malloc0(sizeof(*entry
));
1502 entry
->value
= info
;
1503 entry
->next
= cpu_list
;
1511 static void filter_features_for_kvm(X86CPU
*cpu
)
1513 CPUX86State
*env
= &cpu
->env
;
1514 KVMState
*s
= kvm_state
;
1516 env
->cpuid_features
&=
1517 kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
1518 env
->cpuid_ext_features
&=
1519 kvm_arch_get_supported_cpuid(s
, 1, 0, R_ECX
);
1520 env
->cpuid_ext2_features
&=
1521 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
1522 env
->cpuid_ext3_features
&=
1523 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
1524 env
->cpuid_svm_features
&=
1525 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
1526 env
->cpuid_7_0_ebx_features
&=
1527 kvm_arch_get_supported_cpuid(s
, 7, 0, R_EBX
);
1528 env
->cpuid_kvm_features
&=
1529 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
1530 env
->cpuid_ext4_features
&=
1531 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
1536 int cpu_x86_register(X86CPU
*cpu
, const char *cpu_model
)
1538 CPUX86State
*env
= &cpu
->env
;
1539 x86_def_t def1
, *def
= &def1
;
1540 Error
*error
= NULL
;
1541 char *name
, *features
;
1542 gchar
**model_pieces
;
1544 memset(def
, 0, sizeof(*def
));
1546 model_pieces
= g_strsplit(cpu_model
, ",", 2);
1547 if (!model_pieces
[0]) {
1548 error_setg(&error
, "Invalid/empty CPU model name");
1551 name
= model_pieces
[0];
1552 features
= model_pieces
[1];
1554 if (cpu_x86_find_by_name(def
, name
) < 0) {
1555 error_setg(&error
, "Unable to find CPU definition: %s", name
);
1559 if (kvm_enabled()) {
1560 def
->kvm_features
|= kvm_default_features
;
1562 def
->ext_features
|= CPUID_EXT_HYPERVISOR
;
1564 if (cpu_x86_parse_featurestr(def
, features
) < 0) {
1565 error_setg(&error
, "Invalid cpu_model string format: %s", cpu_model
);
1568 object_property_set_str(OBJECT(cpu
), def
->vendor
, "vendor", &error
);
1569 object_property_set_int(OBJECT(cpu
), def
->level
, "level", &error
);
1570 object_property_set_int(OBJECT(cpu
), def
->family
, "family", &error
);
1571 object_property_set_int(OBJECT(cpu
), def
->model
, "model", &error
);
1572 object_property_set_int(OBJECT(cpu
), def
->stepping
, "stepping", &error
);
1573 env
->cpuid_features
= def
->features
;
1574 env
->cpuid_ext_features
= def
->ext_features
;
1575 env
->cpuid_ext2_features
= def
->ext2_features
;
1576 env
->cpuid_ext3_features
= def
->ext3_features
;
1577 object_property_set_int(OBJECT(cpu
), def
->xlevel
, "xlevel", &error
);
1578 env
->cpuid_kvm_features
= def
->kvm_features
;
1579 env
->cpuid_svm_features
= def
->svm_features
;
1580 env
->cpuid_ext4_features
= def
->ext4_features
;
1581 env
->cpuid_7_0_ebx_features
= def
->cpuid_7_0_ebx_features
;
1582 env
->cpuid_xlevel2
= def
->xlevel2
;
1583 object_property_set_int(OBJECT(cpu
), (int64_t)def
->tsc_khz
* 1000,
1584 "tsc-frequency", &error
);
1586 object_property_set_str(OBJECT(cpu
), def
->model_id
, "model-id", &error
);
1589 g_strfreev(model_pieces
);
1591 fprintf(stderr
, "%s\n", error_get_pretty(error
));
1598 #if !defined(CONFIG_USER_ONLY)
1600 void cpu_clear_apic_feature(CPUX86State
*env
)
1602 env
->cpuid_features
&= ~CPUID_APIC
;
1605 #endif /* !CONFIG_USER_ONLY */
1607 /* Initialize list of CPU models, filling some non-static fields if necessary
1609 void x86_cpudef_setup(void)
1612 static const char *model_with_versions
[] = { "qemu32", "qemu64", "athlon" };
1614 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); ++i
) {
1615 x86_def_t
*def
= &builtin_x86_defs
[i
];
1617 /* Look for specific "cpudef" models that */
1618 /* have the QEMU version in .model_id */
1619 for (j
= 0; j
< ARRAY_SIZE(model_with_versions
); j
++) {
1620 if (strcmp(model_with_versions
[j
], def
->name
) == 0) {
1621 pstrcpy(def
->model_id
, sizeof(def
->model_id
),
1622 "QEMU Virtual CPU version ");
1623 pstrcat(def
->model_id
, sizeof(def
->model_id
),
1624 qemu_get_version());
1631 static void get_cpuid_vendor(CPUX86State
*env
, uint32_t *ebx
,
1632 uint32_t *ecx
, uint32_t *edx
)
1634 *ebx
= env
->cpuid_vendor1
;
1635 *edx
= env
->cpuid_vendor2
;
1636 *ecx
= env
->cpuid_vendor3
;
1639 void cpu_x86_cpuid(CPUX86State
*env
, uint32_t index
, uint32_t count
,
1640 uint32_t *eax
, uint32_t *ebx
,
1641 uint32_t *ecx
, uint32_t *edx
)
1643 X86CPU
*cpu
= x86_env_get_cpu(env
);
1644 CPUState
*cs
= CPU(cpu
);
1646 /* test if maximum index reached */
1647 if (index
& 0x80000000) {
1648 if (index
> env
->cpuid_xlevel
) {
1649 if (env
->cpuid_xlevel2
> 0) {
1650 /* Handle the Centaur's CPUID instruction. */
1651 if (index
> env
->cpuid_xlevel2
) {
1652 index
= env
->cpuid_xlevel2
;
1653 } else if (index
< 0xC0000000) {
1654 index
= env
->cpuid_xlevel
;
1657 /* Intel documentation states that invalid EAX input will
1658 * return the same information as EAX=cpuid_level
1659 * (Intel SDM Vol. 2A - Instruction Set Reference - CPUID)
1661 index
= env
->cpuid_level
;
1665 if (index
> env
->cpuid_level
)
1666 index
= env
->cpuid_level
;
1671 *eax
= env
->cpuid_level
;
1672 get_cpuid_vendor(env
, ebx
, ecx
, edx
);
1675 *eax
= env
->cpuid_version
;
1676 *ebx
= (env
->cpuid_apic_id
<< 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1677 *ecx
= env
->cpuid_ext_features
;
1678 *edx
= env
->cpuid_features
;
1679 if (cs
->nr_cores
* cs
->nr_threads
> 1) {
1680 *ebx
|= (cs
->nr_cores
* cs
->nr_threads
) << 16;
1681 *edx
|= 1 << 28; /* HTT bit */
1685 /* cache info: needed for Pentium Pro compatibility */
1692 /* cache info: needed for Core compatibility */
1693 if (cs
->nr_cores
> 1) {
1694 *eax
= (cs
->nr_cores
- 1) << 26;
1699 case 0: /* L1 dcache info */
1705 case 1: /* L1 icache info */
1711 case 2: /* L2 cache info */
1713 if (cs
->nr_threads
> 1) {
1714 *eax
|= (cs
->nr_threads
- 1) << 14;
1720 default: /* end of info */
1729 /* mwait info: needed for Core compatibility */
1730 *eax
= 0; /* Smallest monitor-line size in bytes */
1731 *ebx
= 0; /* Largest monitor-line size in bytes */
1732 *ecx
= CPUID_MWAIT_EMX
| CPUID_MWAIT_IBE
;
1736 /* Thermal and Power Leaf */
1743 /* Structured Extended Feature Flags Enumeration Leaf */
1745 *eax
= 0; /* Maximum ECX value for sub-leaves */
1746 *ebx
= env
->cpuid_7_0_ebx_features
; /* Feature flags */
1747 *ecx
= 0; /* Reserved */
1748 *edx
= 0; /* Reserved */
1757 /* Direct Cache Access Information Leaf */
1758 *eax
= 0; /* Bits 0-31 in DCA_CAP MSR */
1764 /* Architectural Performance Monitoring Leaf */
1765 if (kvm_enabled()) {
1766 KVMState
*s
= cs
->kvm_state
;
1768 *eax
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EAX
);
1769 *ebx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EBX
);
1770 *ecx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_ECX
);
1771 *edx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EDX
);
1780 /* Processor Extended State */
1781 if (!(env
->cpuid_ext_features
& CPUID_EXT_XSAVE
)) {
1788 if (kvm_enabled()) {
1789 KVMState
*s
= cs
->kvm_state
;
1791 *eax
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EAX
);
1792 *ebx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EBX
);
1793 *ecx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_ECX
);
1794 *edx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EDX
);
1803 *eax
= env
->cpuid_xlevel
;
1804 *ebx
= env
->cpuid_vendor1
;
1805 *edx
= env
->cpuid_vendor2
;
1806 *ecx
= env
->cpuid_vendor3
;
1809 *eax
= env
->cpuid_version
;
1811 *ecx
= env
->cpuid_ext3_features
;
1812 *edx
= env
->cpuid_ext2_features
;
1814 /* The Linux kernel checks for the CMPLegacy bit and
1815 * discards multiple thread information if it is set.
1816 * So dont set it here for Intel to make Linux guests happy.
1818 if (cs
->nr_cores
* cs
->nr_threads
> 1) {
1819 uint32_t tebx
, tecx
, tedx
;
1820 get_cpuid_vendor(env
, &tebx
, &tecx
, &tedx
);
1821 if (tebx
!= CPUID_VENDOR_INTEL_1
||
1822 tedx
!= CPUID_VENDOR_INTEL_2
||
1823 tecx
!= CPUID_VENDOR_INTEL_3
) {
1824 *ecx
|= 1 << 1; /* CmpLegacy bit */
1831 *eax
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 0];
1832 *ebx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 1];
1833 *ecx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 2];
1834 *edx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 3];
1837 /* cache info (L1 cache) */
1844 /* cache info (L2 cache) */
1851 /* virtual & phys address size in low 2 bytes. */
1852 /* XXX: This value must match the one used in the MMU code. */
1853 if (env
->cpuid_ext2_features
& CPUID_EXT2_LM
) {
1854 /* 64 bit processor */
1855 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1856 *eax
= 0x00003028; /* 48 bits virtual, 40 bits physical */
1858 if (env
->cpuid_features
& CPUID_PSE36
)
1859 *eax
= 0x00000024; /* 36 bits physical */
1861 *eax
= 0x00000020; /* 32 bits physical */
1866 if (cs
->nr_cores
* cs
->nr_threads
> 1) {
1867 *ecx
|= (cs
->nr_cores
* cs
->nr_threads
) - 1;
1871 if (env
->cpuid_ext3_features
& CPUID_EXT3_SVM
) {
1872 *eax
= 0x00000001; /* SVM Revision */
1873 *ebx
= 0x00000010; /* nr of ASIDs */
1875 *edx
= env
->cpuid_svm_features
; /* optional features */
1884 *eax
= env
->cpuid_xlevel2
;
1890 /* Support for VIA CPU's CPUID instruction */
1891 *eax
= env
->cpuid_version
;
1894 *edx
= env
->cpuid_ext4_features
;
1899 /* Reserved for the future, and now filled with zero */
1906 /* reserved values: zero */
1915 /* CPUClass::reset() */
1916 static void x86_cpu_reset(CPUState
*s
)
1918 X86CPU
*cpu
= X86_CPU(s
);
1919 X86CPUClass
*xcc
= X86_CPU_GET_CLASS(cpu
);
1920 CPUX86State
*env
= &cpu
->env
;
1923 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
1924 qemu_log("CPU Reset (CPU %d)\n", s
->cpu_index
);
1925 log_cpu_state(env
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
1928 xcc
->parent_reset(s
);
1931 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
1935 env
->old_exception
= -1;
1937 /* init to reset state */
1939 #ifdef CONFIG_SOFTMMU
1940 env
->hflags
|= HF_SOFTMMU_MASK
;
1942 env
->hflags2
|= HF2_GIF_MASK
;
1944 cpu_x86_update_cr0(env
, 0x60000010);
1945 env
->a20_mask
= ~0x0;
1946 env
->smbase
= 0x30000;
1948 env
->idt
.limit
= 0xffff;
1949 env
->gdt
.limit
= 0xffff;
1950 env
->ldt
.limit
= 0xffff;
1951 env
->ldt
.flags
= DESC_P_MASK
| (2 << DESC_TYPE_SHIFT
);
1952 env
->tr
.limit
= 0xffff;
1953 env
->tr
.flags
= DESC_P_MASK
| (11 << DESC_TYPE_SHIFT
);
1955 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff,
1956 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
|
1957 DESC_R_MASK
| DESC_A_MASK
);
1958 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff,
1959 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1961 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff,
1962 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1964 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff,
1965 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1967 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff,
1968 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1970 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff,
1971 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1975 env
->regs
[R_EDX
] = env
->cpuid_version
;
1980 for (i
= 0; i
< 8; i
++) {
1985 env
->mxcsr
= 0x1f80;
1987 env
->pat
= 0x0007040600070406ULL
;
1988 env
->msr_ia32_misc_enable
= MSR_IA32_MISC_ENABLE_DEFAULT
;
1990 memset(env
->dr
, 0, sizeof(env
->dr
));
1991 env
->dr
[6] = DR6_FIXED_1
;
1992 env
->dr
[7] = DR7_FIXED_1
;
1993 cpu_breakpoint_remove_all(env
, BP_CPU
);
1994 cpu_watchpoint_remove_all(env
, BP_CPU
);
1996 #if !defined(CONFIG_USER_ONLY)
1997 /* We hard-wire the BSP to the first CPU. */
1998 if (s
->cpu_index
== 0) {
1999 apic_designate_bsp(env
->apic_state
);
2002 env
->halted
= !cpu_is_bsp(cpu
);
2006 #ifndef CONFIG_USER_ONLY
2007 bool cpu_is_bsp(X86CPU
*cpu
)
2009 return cpu_get_apic_base(cpu
->env
.apic_state
) & MSR_IA32_APICBASE_BSP
;
2012 /* TODO: remove me, when reset over QOM tree is implemented */
2013 static void x86_cpu_machine_reset_cb(void *opaque
)
2015 X86CPU
*cpu
= opaque
;
2016 cpu_reset(CPU(cpu
));
2020 static void mce_init(X86CPU
*cpu
)
2022 CPUX86State
*cenv
= &cpu
->env
;
2025 if (((cenv
->cpuid_version
>> 8) & 0xf) >= 6
2026 && (cenv
->cpuid_features
& (CPUID_MCE
| CPUID_MCA
)) ==
2027 (CPUID_MCE
| CPUID_MCA
)) {
2028 cenv
->mcg_cap
= MCE_CAP_DEF
| MCE_BANKS_DEF
;
2029 cenv
->mcg_ctl
= ~(uint64_t)0;
2030 for (bank
= 0; bank
< MCE_BANKS_DEF
; bank
++) {
2031 cenv
->mce_banks
[bank
* 4] = ~(uint64_t)0;
2036 #define MSI_ADDR_BASE 0xfee00000
2038 #ifndef CONFIG_USER_ONLY
2039 static void x86_cpu_apic_init(X86CPU
*cpu
, Error
**errp
)
2041 static int apic_mapped
;
2042 CPUX86State
*env
= &cpu
->env
;
2043 APICCommonState
*apic
;
2044 const char *apic_type
= "apic";
2046 if (kvm_irqchip_in_kernel()) {
2047 apic_type
= "kvm-apic";
2048 } else if (xen_enabled()) {
2049 apic_type
= "xen-apic";
2052 env
->apic_state
= qdev_try_create(NULL
, apic_type
);
2053 if (env
->apic_state
== NULL
) {
2054 error_setg(errp
, "APIC device '%s' could not be created", apic_type
);
2058 object_property_add_child(OBJECT(cpu
), "apic",
2059 OBJECT(env
->apic_state
), NULL
);
2060 qdev_prop_set_uint8(env
->apic_state
, "id", env
->cpuid_apic_id
);
2061 /* TODO: convert to link<> */
2062 apic
= APIC_COMMON(env
->apic_state
);
2065 if (qdev_init(env
->apic_state
)) {
2066 error_setg(errp
, "APIC device '%s' could not be initialized",
2067 object_get_typename(OBJECT(env
->apic_state
)));
2071 /* XXX: mapping more APICs at the same memory location */
2072 if (apic_mapped
== 0) {
2073 /* NOTE: the APIC is directly connected to the CPU - it is not
2074 on the global memory bus. */
2075 /* XXX: what if the base changes? */
2076 sysbus_mmio_map(SYS_BUS_DEVICE(env
->apic_state
), 0, MSI_ADDR_BASE
);
2082 void x86_cpu_realize(Object
*obj
, Error
**errp
)
2084 X86CPU
*cpu
= X86_CPU(obj
);
2085 CPUX86State
*env
= &cpu
->env
;
2087 if (env
->cpuid_7_0_ebx_features
&& env
->cpuid_level
< 7) {
2088 env
->cpuid_level
= 7;
2091 /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on
2094 if (env
->cpuid_vendor1
== CPUID_VENDOR_AMD_1
&&
2095 env
->cpuid_vendor2
== CPUID_VENDOR_AMD_2
&&
2096 env
->cpuid_vendor3
== CPUID_VENDOR_AMD_3
) {
2097 env
->cpuid_ext2_features
&= ~CPUID_EXT2_AMD_ALIASES
;
2098 env
->cpuid_ext2_features
|= (env
->cpuid_features
2099 & CPUID_EXT2_AMD_ALIASES
);
2102 if (!kvm_enabled()) {
2103 env
->cpuid_features
&= TCG_FEATURES
;
2104 env
->cpuid_ext_features
&= TCG_EXT_FEATURES
;
2105 env
->cpuid_ext2_features
&= (TCG_EXT2_FEATURES
2106 #ifdef TARGET_X86_64
2107 | CPUID_EXT2_SYSCALL
| CPUID_EXT2_LM
2110 env
->cpuid_ext3_features
&= TCG_EXT3_FEATURES
;
2111 env
->cpuid_svm_features
&= TCG_SVM_FEATURES
;
2114 filter_features_for_kvm(cpu
);
2116 if (check_cpuid
&& kvm_check_features_against_host(cpu
)
2118 error_setg(errp
, "Host's CPU doesn't support requested features");
2123 #ifndef CONFIG_USER_ONLY
2124 qemu_register_reset(x86_cpu_machine_reset_cb
, cpu
);
2126 if (cpu
->env
.cpuid_features
& CPUID_APIC
|| smp_cpus
> 1) {
2127 x86_cpu_apic_init(cpu
, errp
);
2128 if (error_is_set(errp
)) {
2135 qemu_init_vcpu(&cpu
->env
);
2136 cpu_reset(CPU(cpu
));
2139 /* Enables contiguous-apic-ID mode, for compatibility */
2140 static bool compat_apic_id_mode
;
2142 void enable_compat_apic_id_mode(void)
2144 compat_apic_id_mode
= true;
2147 /* Calculates initial APIC ID for a specific CPU index
2149 * Currently we need to be able to calculate the APIC ID from the CPU index
2150 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
2151 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
2152 * all CPUs up to max_cpus.
2154 uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index
)
2156 uint32_t correct_id
;
2159 correct_id
= x86_apicid_from_cpu_idx(smp_cores
, smp_threads
, cpu_index
);
2160 if (compat_apic_id_mode
) {
2161 if (cpu_index
!= correct_id
&& !warned
) {
2162 error_report("APIC IDs set in compatibility mode, "
2163 "CPU topology won't match the configuration");
2172 static void x86_cpu_initfn(Object
*obj
)
2174 CPUState
*cs
= CPU(obj
);
2175 X86CPU
*cpu
= X86_CPU(obj
);
2176 CPUX86State
*env
= &cpu
->env
;
2181 object_property_add(obj
, "family", "int",
2182 x86_cpuid_version_get_family
,
2183 x86_cpuid_version_set_family
, NULL
, NULL
, NULL
);
2184 object_property_add(obj
, "model", "int",
2185 x86_cpuid_version_get_model
,
2186 x86_cpuid_version_set_model
, NULL
, NULL
, NULL
);
2187 object_property_add(obj
, "stepping", "int",
2188 x86_cpuid_version_get_stepping
,
2189 x86_cpuid_version_set_stepping
, NULL
, NULL
, NULL
);
2190 object_property_add(obj
, "level", "int",
2191 x86_cpuid_get_level
,
2192 x86_cpuid_set_level
, NULL
, NULL
, NULL
);
2193 object_property_add(obj
, "xlevel", "int",
2194 x86_cpuid_get_xlevel
,
2195 x86_cpuid_set_xlevel
, NULL
, NULL
, NULL
);
2196 object_property_add_str(obj
, "vendor",
2197 x86_cpuid_get_vendor
,
2198 x86_cpuid_set_vendor
, NULL
);
2199 object_property_add_str(obj
, "model-id",
2200 x86_cpuid_get_model_id
,
2201 x86_cpuid_set_model_id
, NULL
);
2202 object_property_add(obj
, "tsc-frequency", "int",
2203 x86_cpuid_get_tsc_freq
,
2204 x86_cpuid_set_tsc_freq
, NULL
, NULL
, NULL
);
2206 env
->cpuid_apic_id
= x86_cpu_apic_id_from_index(cs
->cpu_index
);
2208 /* init various static tables used in TCG mode */
2209 if (tcg_enabled() && !inited
) {
2211 optimize_flags_init();
2212 #ifndef CONFIG_USER_ONLY
2213 cpu_set_debug_excp_handler(breakpoint_handler
);
2218 static void x86_cpu_common_class_init(ObjectClass
*oc
, void *data
)
2220 X86CPUClass
*xcc
= X86_CPU_CLASS(oc
);
2221 CPUClass
*cc
= CPU_CLASS(oc
);
2223 xcc
->parent_reset
= cc
->reset
;
2224 cc
->reset
= x86_cpu_reset
;
2227 static const TypeInfo x86_cpu_type_info
= {
2228 .name
= TYPE_X86_CPU
,
2230 .instance_size
= sizeof(X86CPU
),
2231 .instance_init
= x86_cpu_initfn
,
2233 .class_size
= sizeof(X86CPUClass
),
2234 .class_init
= x86_cpu_common_class_init
,
2237 static void x86_cpu_register_types(void)
2239 type_register_static(&x86_cpu_type_info
);
2242 type_init(x86_cpu_register_types
)