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 /* feature flags taken from "Intel Processor Identification and the CPUID
51 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
52 * between feature naming conventions, aliases may be added.
54 static const char *feature_name
[] = {
55 "fpu", "vme", "de", "pse",
56 "tsc", "msr", "pae", "mce",
57 "cx8", "apic", NULL
, "sep",
58 "mtrr", "pge", "mca", "cmov",
59 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
60 NULL
, "ds" /* Intel dts */, "acpi", "mmx",
61 "fxsr", "sse", "sse2", "ss",
62 "ht" /* Intel htt */, "tm", "ia64", "pbe",
64 static const char *ext_feature_name
[] = {
65 "pni|sse3" /* Intel,AMD sse3 */, "pclmulqdq|pclmuldq", "dtes64", "monitor",
66 "ds_cpl", "vmx", "smx", "est",
67 "tm2", "ssse3", "cid", NULL
,
68 "fma", "cx16", "xtpr", "pdcm",
69 NULL
, "pcid", "dca", "sse4.1|sse4_1",
70 "sse4.2|sse4_2", "x2apic", "movbe", "popcnt",
71 "tsc-deadline", "aes", "xsave", "osxsave",
72 "avx", "f16c", "rdrand", "hypervisor",
74 /* Feature names that are already defined on feature_name[] but are set on
75 * CPUID[8000_0001].EDX on AMD CPUs don't have their names on
76 * ext2_feature_name[]. They are copied automatically to cpuid_ext2_features
77 * if and only if CPU vendor is AMD.
79 static const char *ext2_feature_name
[] = {
80 NULL
/* fpu */, NULL
/* vme */, NULL
/* de */, NULL
/* pse */,
81 NULL
/* tsc */, NULL
/* msr */, NULL
/* pae */, NULL
/* mce */,
82 NULL
/* cx8 */ /* AMD CMPXCHG8B */, NULL
/* apic */, NULL
, "syscall",
83 NULL
/* mtrr */, NULL
/* pge */, NULL
/* mca */, NULL
/* cmov */,
84 NULL
/* pat */, NULL
/* pse36 */, NULL
, NULL
/* Linux mp */,
85 "nx|xd", NULL
, "mmxext", NULL
/* mmx */,
86 NULL
/* fxsr */, "fxsr_opt|ffxsr", "pdpe1gb" /* AMD Page1GB */, "rdtscp",
87 NULL
, "lm|i64", "3dnowext", "3dnow",
89 static const char *ext3_feature_name
[] = {
90 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
91 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
92 "3dnowprefetch", "osvw", "ibs", "xop",
93 "skinit", "wdt", NULL
, "lwp",
94 "fma4", "tce", NULL
, "nodeid_msr",
95 NULL
, "tbm", "topoext", "perfctr_core",
96 "perfctr_nb", NULL
, NULL
, NULL
,
97 NULL
, NULL
, NULL
, NULL
,
100 static const char *ext4_feature_name
[] = {
101 NULL
, NULL
, "xstore", "xstore-en",
102 NULL
, NULL
, "xcrypt", "xcrypt-en",
103 "ace2", "ace2-en", "phe", "phe-en",
104 "pmm", "pmm-en", NULL
, NULL
,
105 NULL
, NULL
, NULL
, NULL
,
106 NULL
, NULL
, NULL
, NULL
,
107 NULL
, NULL
, NULL
, NULL
,
108 NULL
, NULL
, NULL
, NULL
,
111 static const char *kvm_feature_name
[] = {
112 "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock",
113 "kvm_asyncpf", "kvm_steal_time", "kvm_pv_eoi", NULL
,
114 NULL
, NULL
, NULL
, NULL
,
115 NULL
, NULL
, NULL
, NULL
,
116 NULL
, NULL
, NULL
, NULL
,
117 NULL
, NULL
, NULL
, NULL
,
118 NULL
, NULL
, NULL
, NULL
,
119 NULL
, NULL
, NULL
, NULL
,
122 static const char *svm_feature_name
[] = {
123 "npt", "lbrv", "svm_lock", "nrip_save",
124 "tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists",
125 NULL
, NULL
, "pause_filter", NULL
,
126 "pfthreshold", NULL
, NULL
, NULL
,
127 NULL
, NULL
, NULL
, NULL
,
128 NULL
, NULL
, NULL
, NULL
,
129 NULL
, NULL
, NULL
, NULL
,
130 NULL
, NULL
, NULL
, NULL
,
133 static const char *cpuid_7_0_ebx_feature_name
[] = {
134 "fsgsbase", NULL
, NULL
, "bmi1", "hle", "avx2", NULL
, "smep",
135 "bmi2", "erms", "invpcid", "rtm", NULL
, NULL
, NULL
, NULL
,
136 NULL
, NULL
, "rdseed", "adx", "smap", NULL
, NULL
, NULL
,
137 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
140 typedef struct FeatureWordInfo
{
141 const char **feat_names
;
142 uint32_t cpuid_eax
; /* Input EAX for CPUID */
143 int cpuid_reg
; /* R_* register constant */
146 static FeatureWordInfo feature_word_info
[FEATURE_WORDS
] = {
148 .feat_names
= feature_name
,
149 .cpuid_eax
= 1, .cpuid_reg
= R_EDX
,
152 .feat_names
= ext_feature_name
,
153 .cpuid_eax
= 1, .cpuid_reg
= R_ECX
,
155 [FEAT_8000_0001_EDX
] = {
156 .feat_names
= ext2_feature_name
,
157 .cpuid_eax
= 0x80000001, .cpuid_reg
= R_EDX
,
159 [FEAT_8000_0001_ECX
] = {
160 .feat_names
= ext3_feature_name
,
161 .cpuid_eax
= 0x80000001, .cpuid_reg
= R_ECX
,
163 [FEAT_C000_0001_EDX
] = {
164 .feat_names
= ext4_feature_name
,
165 .cpuid_eax
= 0xC0000001, .cpuid_reg
= R_EDX
,
168 .feat_names
= kvm_feature_name
,
169 .cpuid_eax
= KVM_CPUID_FEATURES
, .cpuid_reg
= R_EAX
,
172 .feat_names
= svm_feature_name
,
173 .cpuid_eax
= 0x8000000A, .cpuid_reg
= R_EDX
,
176 .feat_names
= cpuid_7_0_ebx_feature_name
,
177 .cpuid_eax
= 7, .cpuid_reg
= R_EBX
,
181 const char *get_register_name_32(unsigned int reg
)
183 static const char *reg_names
[CPU_NB_REGS32
] = {
194 if (reg
> CPU_NB_REGS32
) {
197 return reg_names
[reg
];
200 /* collects per-function cpuid data
202 typedef struct model_features_t
{
203 uint32_t *guest_feat
;
205 FeatureWord feat_word
;
209 int enforce_cpuid
= 0;
211 static uint32_t kvm_default_features
= (1 << KVM_FEATURE_CLOCKSOURCE
) |
212 (1 << KVM_FEATURE_NOP_IO_DELAY
) |
213 (1 << KVM_FEATURE_CLOCKSOURCE2
) |
214 (1 << KVM_FEATURE_ASYNC_PF
) |
215 (1 << KVM_FEATURE_STEAL_TIME
) |
216 (1 << KVM_FEATURE_PV_EOI
) |
217 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT
);
219 void disable_kvm_pv_eoi(void)
221 kvm_default_features
&= ~(1UL << KVM_FEATURE_PV_EOI
);
224 void host_cpuid(uint32_t function
, uint32_t count
,
225 uint32_t *eax
, uint32_t *ebx
, uint32_t *ecx
, uint32_t *edx
)
227 #if defined(CONFIG_KVM)
232 : "=a"(vec
[0]), "=b"(vec
[1]),
233 "=c"(vec
[2]), "=d"(vec
[3])
234 : "0"(function
), "c"(count
) : "cc");
236 asm volatile("pusha \n\t"
238 "mov %%eax, 0(%2) \n\t"
239 "mov %%ebx, 4(%2) \n\t"
240 "mov %%ecx, 8(%2) \n\t"
241 "mov %%edx, 12(%2) \n\t"
243 : : "a"(function
), "c"(count
), "S"(vec
)
258 #define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
260 /* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
261 * a substring. ex if !NULL points to the first char after a substring,
262 * otherwise the string is assumed to sized by a terminating nul.
263 * Return lexical ordering of *s1:*s2.
265 static int sstrcmp(const char *s1
, const char *e1
, const char *s2
,
269 if (!*s1
|| !*s2
|| *s1
!= *s2
)
272 if (s1
== e1
&& s2
== e2
)
281 /* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
282 * '|' delimited (possibly empty) strings in which case search for a match
283 * within the alternatives proceeds left to right. Return 0 for success,
284 * non-zero otherwise.
286 static int altcmp(const char *s
, const char *e
, const char *altstr
)
290 for (q
= p
= altstr
; ; ) {
291 while (*p
&& *p
!= '|')
293 if ((q
== p
&& !*s
) || (q
!= p
&& !sstrcmp(s
, e
, q
, p
)))
302 /* search featureset for flag *[s..e), if found set corresponding bit in
303 * *pval and return true, otherwise return false
305 static bool lookup_feature(uint32_t *pval
, const char *s
, const char *e
,
306 const char **featureset
)
312 for (mask
= 1, ppc
= featureset
; mask
; mask
<<= 1, ++ppc
) {
313 if (*ppc
&& !altcmp(s
, e
, *ppc
)) {
321 static void add_flagname_to_bitmaps(const char *flagname
,
322 FeatureWordArray words
)
325 for (w
= 0; w
< FEATURE_WORDS
; w
++) {
326 FeatureWordInfo
*wi
= &feature_word_info
[w
];
327 if (wi
->feat_names
&&
328 lookup_feature(&words
[w
], flagname
, NULL
, wi
->feat_names
)) {
332 if (w
== FEATURE_WORDS
) {
333 fprintf(stderr
, "CPU feature %s not found\n", flagname
);
337 typedef struct x86_def_t
{
340 uint32_t vendor1
, vendor2
, vendor3
;
345 uint32_t features
, ext_features
, ext2_features
, ext3_features
;
346 uint32_t kvm_features
, svm_features
;
350 /* Store the results of Centaur's CPUID instructions */
351 uint32_t ext4_features
;
353 /* The feature bits on CPUID[EAX=7,ECX=0].EBX */
354 uint32_t cpuid_7_0_ebx_features
;
357 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
358 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
359 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
360 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
361 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
362 CPUID_PSE36 | CPUID_FXSR)
363 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
364 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
365 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
366 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
367 CPUID_PAE | CPUID_SEP | CPUID_APIC)
369 #define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
370 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
371 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
372 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
373 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
374 /* partly implemented:
375 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
376 CPUID_PSE36 (needed for Solaris) */
378 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
379 #define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \
380 CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT | \
381 CPUID_EXT_HYPERVISOR)
383 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
384 CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_XSAVE */
385 #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
386 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
387 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
389 CPUID_EXT2_PDPE1GB */
390 #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
391 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
392 #define TCG_SVM_FEATURES 0
393 #define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP)
395 /* built-in CPU model definitions
397 static x86_def_t builtin_x86_defs
[] = {
401 .vendor1
= CPUID_VENDOR_AMD_1
,
402 .vendor2
= CPUID_VENDOR_AMD_2
,
403 .vendor3
= CPUID_VENDOR_AMD_3
,
407 .features
= PPRO_FEATURES
|
408 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
410 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
| CPUID_EXT_POPCNT
,
411 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
412 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
413 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
414 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
415 .xlevel
= 0x8000000A,
420 .vendor1
= CPUID_VENDOR_AMD_1
,
421 .vendor2
= CPUID_VENDOR_AMD_2
,
422 .vendor3
= CPUID_VENDOR_AMD_3
,
426 .features
= PPRO_FEATURES
|
427 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
428 CPUID_PSE36
| CPUID_VME
| CPUID_HT
,
429 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_CX16
|
431 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
432 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
433 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
| CPUID_EXT2_MMXEXT
|
434 CPUID_EXT2_FFXSR
| CPUID_EXT2_PDPE1GB
| CPUID_EXT2_RDTSCP
,
435 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
437 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
438 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
439 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
440 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
441 .svm_features
= CPUID_SVM_NPT
| CPUID_SVM_LBRV
,
442 .xlevel
= 0x8000001A,
443 .model_id
= "AMD Phenom(tm) 9550 Quad-Core Processor"
448 .vendor1
= CPUID_VENDOR_INTEL_1
,
449 .vendor2
= CPUID_VENDOR_INTEL_2
,
450 .vendor3
= CPUID_VENDOR_INTEL_3
,
454 .features
= PPRO_FEATURES
|
455 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
456 CPUID_PSE36
| CPUID_VME
| CPUID_DTS
| CPUID_ACPI
| CPUID_SS
|
457 CPUID_HT
| CPUID_TM
| CPUID_PBE
,
458 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
459 CPUID_EXT_DTES64
| CPUID_EXT_DSCPL
| CPUID_EXT_VMX
| CPUID_EXT_EST
|
460 CPUID_EXT_TM2
| CPUID_EXT_CX16
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
461 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
462 .ext3_features
= CPUID_EXT3_LAHF_LM
,
463 .xlevel
= 0x80000008,
464 .model_id
= "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
469 .vendor1
= CPUID_VENDOR_INTEL_1
,
470 .vendor2
= CPUID_VENDOR_INTEL_2
,
471 .vendor3
= CPUID_VENDOR_INTEL_3
,
475 /* Missing: CPUID_VME, CPUID_HT */
476 .features
= PPRO_FEATURES
|
477 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
479 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
480 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
,
481 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
482 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
483 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
484 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
485 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
486 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
487 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
489 .xlevel
= 0x80000008,
490 .model_id
= "Common KVM processor"
495 .vendor1
= CPUID_VENDOR_INTEL_1
,
496 .vendor2
= CPUID_VENDOR_INTEL_2
,
497 .vendor3
= CPUID_VENDOR_INTEL_3
,
501 .features
= PPRO_FEATURES
,
502 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_POPCNT
,
503 .xlevel
= 0x80000004,
508 .vendor1
= CPUID_VENDOR_INTEL_1
,
509 .vendor2
= CPUID_VENDOR_INTEL_2
,
510 .vendor3
= CPUID_VENDOR_INTEL_3
,
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 .vendor1
= CPUID_VENDOR_INTEL_1
,
526 .vendor2
= CPUID_VENDOR_INTEL_2
,
527 .vendor3
= CPUID_VENDOR_INTEL_3
,
531 .features
= PPRO_FEATURES
| CPUID_VME
|
532 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_DTS
| CPUID_ACPI
|
533 CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
534 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_VMX
|
535 CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
536 .ext2_features
= CPUID_EXT2_NX
,
537 .xlevel
= 0x80000008,
538 .model_id
= "Genuine Intel(R) CPU T2600 @ 2.16GHz",
543 .vendor1
= CPUID_VENDOR_INTEL_1
,
544 .vendor2
= CPUID_VENDOR_INTEL_2
,
545 .vendor3
= CPUID_VENDOR_INTEL_3
,
549 .features
= I486_FEATURES
,
555 .vendor1
= CPUID_VENDOR_INTEL_1
,
556 .vendor2
= CPUID_VENDOR_INTEL_2
,
557 .vendor3
= CPUID_VENDOR_INTEL_3
,
561 .features
= PENTIUM_FEATURES
,
567 .vendor1
= CPUID_VENDOR_INTEL_1
,
568 .vendor2
= CPUID_VENDOR_INTEL_2
,
569 .vendor3
= CPUID_VENDOR_INTEL_3
,
573 .features
= PENTIUM2_FEATURES
,
579 .vendor1
= CPUID_VENDOR_INTEL_1
,
580 .vendor2
= CPUID_VENDOR_INTEL_2
,
581 .vendor3
= CPUID_VENDOR_INTEL_3
,
585 .features
= PENTIUM3_FEATURES
,
591 .vendor1
= CPUID_VENDOR_AMD_1
,
592 .vendor2
= CPUID_VENDOR_AMD_2
,
593 .vendor3
= CPUID_VENDOR_AMD_3
,
597 .features
= PPRO_FEATURES
| CPUID_PSE36
| CPUID_VME
| CPUID_MTRR
|
599 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
600 CPUID_EXT2_MMXEXT
| CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
601 .xlevel
= 0x80000008,
605 /* original is on level 10 */
607 .vendor1
= CPUID_VENDOR_INTEL_1
,
608 .vendor2
= CPUID_VENDOR_INTEL_2
,
609 .vendor3
= CPUID_VENDOR_INTEL_3
,
613 .features
= PPRO_FEATURES
|
614 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_VME
| CPUID_DTS
|
615 CPUID_ACPI
| CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
616 /* Some CPUs got no CPUID_SEP */
617 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
618 CPUID_EXT_DSCPL
| CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
,
619 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
621 .ext3_features
= CPUID_EXT3_LAHF_LM
,
622 .xlevel
= 0x8000000A,
623 .model_id
= "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
628 .vendor1
= CPUID_VENDOR_INTEL_1
,
629 .vendor2
= CPUID_VENDOR_INTEL_2
,
630 .vendor3
= CPUID_VENDOR_INTEL_3
,
634 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
635 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
636 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
637 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
638 CPUID_DE
| CPUID_FP87
,
639 .ext_features
= CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
640 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
641 .ext3_features
= CPUID_EXT3_LAHF_LM
,
642 .xlevel
= 0x8000000A,
643 .model_id
= "Intel Celeron_4x0 (Conroe/Merom Class Core 2)",
648 .vendor1
= CPUID_VENDOR_INTEL_1
,
649 .vendor2
= CPUID_VENDOR_INTEL_2
,
650 .vendor3
= CPUID_VENDOR_INTEL_3
,
654 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
655 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
656 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
657 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
658 CPUID_DE
| CPUID_FP87
,
659 .ext_features
= CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
661 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
662 .ext3_features
= CPUID_EXT3_LAHF_LM
,
663 .xlevel
= 0x8000000A,
664 .model_id
= "Intel Core 2 Duo P9xxx (Penryn Class Core 2)",
669 .vendor1
= CPUID_VENDOR_INTEL_1
,
670 .vendor2
= CPUID_VENDOR_INTEL_2
,
671 .vendor3
= CPUID_VENDOR_INTEL_3
,
675 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
676 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
677 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
678 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
679 CPUID_DE
| CPUID_FP87
,
680 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
681 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
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
= "Intel Core i7 9xx (Nehalem Class Core i7)",
690 .vendor1
= CPUID_VENDOR_INTEL_1
,
691 .vendor2
= CPUID_VENDOR_INTEL_2
,
692 .vendor3
= CPUID_VENDOR_INTEL_3
,
696 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
697 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
698 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
699 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
700 CPUID_DE
| CPUID_FP87
,
701 .ext_features
= CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
702 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
704 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
705 .ext3_features
= CPUID_EXT3_LAHF_LM
,
706 .xlevel
= 0x8000000A,
707 .model_id
= "Westmere E56xx/L56xx/X56xx (Nehalem-C)",
710 .name
= "SandyBridge",
712 .vendor1
= CPUID_VENDOR_INTEL_1
,
713 .vendor2
= CPUID_VENDOR_INTEL_2
,
714 .vendor3
= CPUID_VENDOR_INTEL_3
,
718 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
719 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
720 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
721 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
722 CPUID_DE
| CPUID_FP87
,
723 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
724 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_POPCNT
|
725 CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
726 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
728 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
730 .ext3_features
= CPUID_EXT3_LAHF_LM
,
731 .xlevel
= 0x8000000A,
732 .model_id
= "Intel Xeon E312xx (Sandy Bridge)",
737 .vendor1
= CPUID_VENDOR_INTEL_1
,
738 .vendor2
= CPUID_VENDOR_INTEL_2
,
739 .vendor3
= CPUID_VENDOR_INTEL_3
,
743 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
744 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
745 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
746 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
747 CPUID_DE
| CPUID_FP87
,
748 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
749 CPUID_EXT_POPCNT
| CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
|
750 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
751 CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
|
752 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_FMA
| CPUID_EXT_MOVBE
|
754 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
756 .ext3_features
= CPUID_EXT3_LAHF_LM
,
757 .cpuid_7_0_ebx_features
= CPUID_7_0_EBX_FSGSBASE
| CPUID_7_0_EBX_BMI1
|
758 CPUID_7_0_EBX_HLE
| CPUID_7_0_EBX_AVX2
| CPUID_7_0_EBX_SMEP
|
759 CPUID_7_0_EBX_BMI2
| CPUID_7_0_EBX_ERMS
| CPUID_7_0_EBX_INVPCID
|
761 .xlevel
= 0x8000000A,
762 .model_id
= "Intel Core Processor (Haswell)",
765 .name
= "Opteron_G1",
767 .vendor1
= CPUID_VENDOR_AMD_1
,
768 .vendor2
= CPUID_VENDOR_AMD_2
,
769 .vendor3
= CPUID_VENDOR_AMD_3
,
773 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
774 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
775 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
776 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
777 CPUID_DE
| CPUID_FP87
,
778 .ext_features
= CPUID_EXT_SSE3
,
779 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
780 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
781 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
782 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
783 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
784 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
785 .xlevel
= 0x80000008,
786 .model_id
= "AMD Opteron 240 (Gen 1 Class Opteron)",
789 .name
= "Opteron_G2",
791 .vendor1
= CPUID_VENDOR_AMD_1
,
792 .vendor2
= CPUID_VENDOR_AMD_2
,
793 .vendor3
= CPUID_VENDOR_AMD_3
,
797 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
798 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
799 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
800 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
801 CPUID_DE
| CPUID_FP87
,
802 .ext_features
= CPUID_EXT_CX16
| CPUID_EXT_SSE3
,
803 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
804 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
805 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
806 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
807 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
808 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
809 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
810 .ext3_features
= CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
811 .xlevel
= 0x80000008,
812 .model_id
= "AMD Opteron 22xx (Gen 2 Class Opteron)",
815 .name
= "Opteron_G3",
817 .vendor1
= CPUID_VENDOR_AMD_1
,
818 .vendor2
= CPUID_VENDOR_AMD_2
,
819 .vendor3
= CPUID_VENDOR_AMD_3
,
823 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
824 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
825 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
826 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
827 CPUID_DE
| CPUID_FP87
,
828 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_CX16
| CPUID_EXT_MONITOR
|
830 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
831 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
832 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
833 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
834 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
835 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
836 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
837 .ext3_features
= CPUID_EXT3_MISALIGNSSE
| CPUID_EXT3_SSE4A
|
838 CPUID_EXT3_ABM
| CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
839 .xlevel
= 0x80000008,
840 .model_id
= "AMD Opteron 23xx (Gen 3 Class Opteron)",
843 .name
= "Opteron_G4",
845 .vendor1
= CPUID_VENDOR_AMD_1
,
846 .vendor2
= CPUID_VENDOR_AMD_2
,
847 .vendor3
= CPUID_VENDOR_AMD_3
,
851 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
852 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
853 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
854 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
855 CPUID_DE
| CPUID_FP87
,
856 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
857 CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
858 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
860 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
861 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
862 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
863 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
864 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
865 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
866 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
867 .ext3_features
= CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
868 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
869 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
871 .xlevel
= 0x8000001A,
872 .model_id
= "AMD Opteron 62xx class CPU",
875 .name
= "Opteron_G5",
877 .vendor1
= CPUID_VENDOR_AMD_1
,
878 .vendor2
= CPUID_VENDOR_AMD_2
,
879 .vendor3
= CPUID_VENDOR_AMD_3
,
883 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
884 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
885 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
886 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
887 CPUID_DE
| CPUID_FP87
,
888 .ext_features
= CPUID_EXT_F16C
| CPUID_EXT_AVX
| CPUID_EXT_XSAVE
|
889 CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
890 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_FMA
|
891 CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
,
892 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
893 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
894 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
895 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
896 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
897 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
898 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
899 .ext3_features
= CPUID_EXT3_TBM
| CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
900 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
901 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
903 .xlevel
= 0x8000001A,
904 .model_id
= "AMD Opteron 63xx class CPU",
909 static int cpu_x86_fill_model_id(char *str
)
911 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
914 for (i
= 0; i
< 3; i
++) {
915 host_cpuid(0x80000002 + i
, 0, &eax
, &ebx
, &ecx
, &edx
);
916 memcpy(str
+ i
* 16 + 0, &eax
, 4);
917 memcpy(str
+ i
* 16 + 4, &ebx
, 4);
918 memcpy(str
+ i
* 16 + 8, &ecx
, 4);
919 memcpy(str
+ i
* 16 + 12, &edx
, 4);
925 /* Fill a x86_def_t struct with information about the host CPU, and
926 * the CPU features supported by the host hardware + host kernel
928 * This function may be called only if KVM is enabled.
930 static void kvm_cpu_fill_host(x86_def_t
*x86_cpu_def
)
933 KVMState
*s
= kvm_state
;
934 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
936 assert(kvm_enabled());
938 x86_cpu_def
->name
= "host";
939 host_cpuid(0x0, 0, &eax
, &ebx
, &ecx
, &edx
);
940 x86_cpu_def
->vendor1
= ebx
;
941 x86_cpu_def
->vendor2
= edx
;
942 x86_cpu_def
->vendor3
= ecx
;
944 host_cpuid(0x1, 0, &eax
, &ebx
, &ecx
, &edx
);
945 x86_cpu_def
->family
= ((eax
>> 8) & 0x0F) + ((eax
>> 20) & 0xFF);
946 x86_cpu_def
->model
= ((eax
>> 4) & 0x0F) | ((eax
& 0xF0000) >> 12);
947 x86_cpu_def
->stepping
= eax
& 0x0F;
949 x86_cpu_def
->level
= kvm_arch_get_supported_cpuid(s
, 0x0, 0, R_EAX
);
950 x86_cpu_def
->features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_EDX
);
951 x86_cpu_def
->ext_features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_ECX
);
953 if (x86_cpu_def
->level
>= 7) {
954 x86_cpu_def
->cpuid_7_0_ebx_features
=
955 kvm_arch_get_supported_cpuid(s
, 0x7, 0, R_EBX
);
957 x86_cpu_def
->cpuid_7_0_ebx_features
= 0;
960 x86_cpu_def
->xlevel
= kvm_arch_get_supported_cpuid(s
, 0x80000000, 0, R_EAX
);
961 x86_cpu_def
->ext2_features
=
962 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
963 x86_cpu_def
->ext3_features
=
964 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
966 cpu_x86_fill_model_id(x86_cpu_def
->model_id
);
967 x86_cpu_def
->vendor_override
= 0;
969 /* Call Centaur's CPUID instruction. */
970 if (x86_cpu_def
->vendor1
== CPUID_VENDOR_VIA_1
&&
971 x86_cpu_def
->vendor2
== CPUID_VENDOR_VIA_2
&&
972 x86_cpu_def
->vendor3
== CPUID_VENDOR_VIA_3
) {
973 host_cpuid(0xC0000000, 0, &eax
, &ebx
, &ecx
, &edx
);
974 eax
= kvm_arch_get_supported_cpuid(s
, 0xC0000000, 0, R_EAX
);
975 if (eax
>= 0xC0000001) {
976 /* Support VIA max extended level */
977 x86_cpu_def
->xlevel2
= eax
;
978 host_cpuid(0xC0000001, 0, &eax
, &ebx
, &ecx
, &edx
);
979 x86_cpu_def
->ext4_features
=
980 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
984 /* Other KVM-specific feature fields: */
985 x86_cpu_def
->svm_features
=
986 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
987 x86_cpu_def
->kvm_features
=
988 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
990 #endif /* CONFIG_KVM */
993 static int unavailable_host_feature(FeatureWordInfo
*f
, uint32_t mask
)
997 for (i
= 0; i
< 32; ++i
)
999 const char *reg
= get_register_name_32(f
->cpuid_reg
);
1001 fprintf(stderr
, "warning: host doesn't support requested feature: "
1002 "CPUID.%02XH:%s%s%s [bit %d]\n",
1004 f
->feat_names
[i
] ? "." : "",
1005 f
->feat_names
[i
] ? f
->feat_names
[i
] : "", i
);
1011 /* Check if all requested cpu flags are making their way to the guest
1013 * Returns 0 if all flags are supported by the host, non-zero otherwise.
1015 * This function may be called only if KVM is enabled.
1017 static int kvm_check_features_against_host(X86CPU
*cpu
)
1019 CPUX86State
*env
= &cpu
->env
;
1023 struct model_features_t ft
[] = {
1024 {&env
->cpuid_features
, &host_def
.features
,
1026 {&env
->cpuid_ext_features
, &host_def
.ext_features
,
1028 {&env
->cpuid_ext2_features
, &host_def
.ext2_features
,
1029 FEAT_8000_0001_EDX
},
1030 {&env
->cpuid_ext3_features
, &host_def
.ext3_features
,
1031 FEAT_8000_0001_ECX
},
1032 {&env
->cpuid_ext4_features
, &host_def
.ext4_features
,
1033 FEAT_C000_0001_EDX
},
1034 {&env
->cpuid_7_0_ebx_features
, &host_def
.cpuid_7_0_ebx_features
,
1036 {&env
->cpuid_svm_features
, &host_def
.svm_features
,
1038 {&env
->cpuid_kvm_features
, &host_def
.kvm_features
,
1042 assert(kvm_enabled());
1044 kvm_cpu_fill_host(&host_def
);
1045 for (rv
= 0, i
= 0; i
< ARRAY_SIZE(ft
); ++i
) {
1046 FeatureWord w
= ft
[i
].feat_word
;
1047 FeatureWordInfo
*wi
= &feature_word_info
[w
];
1048 for (mask
= 1; mask
; mask
<<= 1) {
1049 if (*ft
[i
].guest_feat
& mask
&&
1050 !(*ft
[i
].host_feat
& mask
)) {
1051 unavailable_host_feature(wi
, mask
);
1059 static void x86_cpuid_version_get_family(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
>> 8) & 0xf;
1068 value
+= (env
->cpuid_version
>> 20) & 0xff;
1070 visit_type_int(v
, &value
, name
, errp
);
1073 static void x86_cpuid_version_set_family(Object
*obj
, Visitor
*v
, void *opaque
,
1074 const char *name
, Error
**errp
)
1076 X86CPU
*cpu
= X86_CPU(obj
);
1077 CPUX86State
*env
= &cpu
->env
;
1078 const int64_t min
= 0;
1079 const int64_t max
= 0xff + 0xf;
1082 visit_type_int(v
, &value
, name
, errp
);
1083 if (error_is_set(errp
)) {
1086 if (value
< min
|| value
> max
) {
1087 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1088 name
? name
: "null", value
, min
, max
);
1092 env
->cpuid_version
&= ~0xff00f00;
1094 env
->cpuid_version
|= 0xf00 | ((value
- 0x0f) << 20);
1096 env
->cpuid_version
|= value
<< 8;
1100 static void x86_cpuid_version_get_model(Object
*obj
, Visitor
*v
, void *opaque
,
1101 const char *name
, Error
**errp
)
1103 X86CPU
*cpu
= X86_CPU(obj
);
1104 CPUX86State
*env
= &cpu
->env
;
1107 value
= (env
->cpuid_version
>> 4) & 0xf;
1108 value
|= ((env
->cpuid_version
>> 16) & 0xf) << 4;
1109 visit_type_int(v
, &value
, name
, errp
);
1112 static void x86_cpuid_version_set_model(Object
*obj
, Visitor
*v
, void *opaque
,
1113 const char *name
, Error
**errp
)
1115 X86CPU
*cpu
= X86_CPU(obj
);
1116 CPUX86State
*env
= &cpu
->env
;
1117 const int64_t min
= 0;
1118 const int64_t max
= 0xff;
1121 visit_type_int(v
, &value
, name
, errp
);
1122 if (error_is_set(errp
)) {
1125 if (value
< min
|| value
> max
) {
1126 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1127 name
? name
: "null", value
, min
, max
);
1131 env
->cpuid_version
&= ~0xf00f0;
1132 env
->cpuid_version
|= ((value
& 0xf) << 4) | ((value
>> 4) << 16);
1135 static void x86_cpuid_version_get_stepping(Object
*obj
, Visitor
*v
,
1136 void *opaque
, const char *name
,
1139 X86CPU
*cpu
= X86_CPU(obj
);
1140 CPUX86State
*env
= &cpu
->env
;
1143 value
= env
->cpuid_version
& 0xf;
1144 visit_type_int(v
, &value
, name
, errp
);
1147 static void x86_cpuid_version_set_stepping(Object
*obj
, Visitor
*v
,
1148 void *opaque
, const char *name
,
1151 X86CPU
*cpu
= X86_CPU(obj
);
1152 CPUX86State
*env
= &cpu
->env
;
1153 const int64_t min
= 0;
1154 const int64_t max
= 0xf;
1157 visit_type_int(v
, &value
, name
, errp
);
1158 if (error_is_set(errp
)) {
1161 if (value
< min
|| value
> max
) {
1162 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1163 name
? name
: "null", value
, min
, max
);
1167 env
->cpuid_version
&= ~0xf;
1168 env
->cpuid_version
|= value
& 0xf;
1171 static void x86_cpuid_get_level(Object
*obj
, Visitor
*v
, void *opaque
,
1172 const char *name
, Error
**errp
)
1174 X86CPU
*cpu
= X86_CPU(obj
);
1176 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1179 static void x86_cpuid_set_level(Object
*obj
, Visitor
*v
, void *opaque
,
1180 const char *name
, Error
**errp
)
1182 X86CPU
*cpu
= X86_CPU(obj
);
1184 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1187 static void x86_cpuid_get_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1188 const char *name
, Error
**errp
)
1190 X86CPU
*cpu
= X86_CPU(obj
);
1192 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1195 static void x86_cpuid_set_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1196 const char *name
, Error
**errp
)
1198 X86CPU
*cpu
= X86_CPU(obj
);
1200 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1203 static char *x86_cpuid_get_vendor(Object
*obj
, Error
**errp
)
1205 X86CPU
*cpu
= X86_CPU(obj
);
1206 CPUX86State
*env
= &cpu
->env
;
1210 value
= (char *)g_malloc(CPUID_VENDOR_SZ
+ 1);
1211 for (i
= 0; i
< 4; i
++) {
1212 value
[i
] = env
->cpuid_vendor1
>> (8 * i
);
1213 value
[i
+ 4] = env
->cpuid_vendor2
>> (8 * i
);
1214 value
[i
+ 8] = env
->cpuid_vendor3
>> (8 * i
);
1216 value
[CPUID_VENDOR_SZ
] = '\0';
1220 static void x86_cpuid_set_vendor(Object
*obj
, const char *value
,
1223 X86CPU
*cpu
= X86_CPU(obj
);
1224 CPUX86State
*env
= &cpu
->env
;
1227 if (strlen(value
) != CPUID_VENDOR_SZ
) {
1228 error_set(errp
, QERR_PROPERTY_VALUE_BAD
, "",
1233 env
->cpuid_vendor1
= 0;
1234 env
->cpuid_vendor2
= 0;
1235 env
->cpuid_vendor3
= 0;
1236 for (i
= 0; i
< 4; i
++) {
1237 env
->cpuid_vendor1
|= ((uint8_t)value
[i
]) << (8 * i
);
1238 env
->cpuid_vendor2
|= ((uint8_t)value
[i
+ 4]) << (8 * i
);
1239 env
->cpuid_vendor3
|= ((uint8_t)value
[i
+ 8]) << (8 * i
);
1241 env
->cpuid_vendor_override
= 1;
1244 static char *x86_cpuid_get_model_id(Object
*obj
, Error
**errp
)
1246 X86CPU
*cpu
= X86_CPU(obj
);
1247 CPUX86State
*env
= &cpu
->env
;
1251 value
= g_malloc(48 + 1);
1252 for (i
= 0; i
< 48; i
++) {
1253 value
[i
] = env
->cpuid_model
[i
>> 2] >> (8 * (i
& 3));
1259 static void x86_cpuid_set_model_id(Object
*obj
, const char *model_id
,
1262 X86CPU
*cpu
= X86_CPU(obj
);
1263 CPUX86State
*env
= &cpu
->env
;
1266 if (model_id
== NULL
) {
1269 len
= strlen(model_id
);
1270 memset(env
->cpuid_model
, 0, 48);
1271 for (i
= 0; i
< 48; i
++) {
1275 c
= (uint8_t)model_id
[i
];
1277 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
1281 static void x86_cpuid_get_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1282 const char *name
, Error
**errp
)
1284 X86CPU
*cpu
= X86_CPU(obj
);
1287 value
= cpu
->env
.tsc_khz
* 1000;
1288 visit_type_int(v
, &value
, name
, errp
);
1291 static void x86_cpuid_set_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1292 const char *name
, Error
**errp
)
1294 X86CPU
*cpu
= X86_CPU(obj
);
1295 const int64_t min
= 0;
1296 const int64_t max
= INT64_MAX
;
1299 visit_type_int(v
, &value
, name
, errp
);
1300 if (error_is_set(errp
)) {
1303 if (value
< min
|| value
> max
) {
1304 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1305 name
? name
: "null", value
, min
, max
);
1309 cpu
->env
.tsc_khz
= value
/ 1000;
1312 static int cpu_x86_find_by_name(x86_def_t
*x86_cpu_def
, const char *name
)
1320 if (kvm_enabled() && strcmp(name
, "host") == 0) {
1321 kvm_cpu_fill_host(x86_cpu_def
);
1325 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); i
++) {
1326 def
= &builtin_x86_defs
[i
];
1327 if (strcmp(name
, def
->name
) == 0) {
1328 memcpy(x86_cpu_def
, def
, sizeof(*def
));
1336 /* Parse "+feature,-feature,feature=foo" CPU feature string
1338 static int cpu_x86_parse_featurestr(x86_def_t
*x86_cpu_def
, char *features
)
1341 char *featurestr
; /* Single 'key=value" string being parsed */
1342 /* Features to be added */
1343 FeatureWordArray plus_features
= { 0 };
1344 /* Features to be removed */
1345 FeatureWordArray minus_features
= { 0 };
1348 featurestr
= features
? strtok(features
, ",") : NULL
;
1350 while (featurestr
) {
1352 if (featurestr
[0] == '+') {
1353 add_flagname_to_bitmaps(featurestr
+ 1, plus_features
);
1354 } else if (featurestr
[0] == '-') {
1355 add_flagname_to_bitmaps(featurestr
+ 1, minus_features
);
1356 } else if ((val
= strchr(featurestr
, '='))) {
1358 if (!strcmp(featurestr
, "family")) {
1360 numvalue
= strtoul(val
, &err
, 0);
1361 if (!*val
|| *err
|| numvalue
> 0xff + 0xf) {
1362 fprintf(stderr
, "bad numerical value %s\n", val
);
1365 x86_cpu_def
->family
= numvalue
;
1366 } else if (!strcmp(featurestr
, "model")) {
1368 numvalue
= strtoul(val
, &err
, 0);
1369 if (!*val
|| *err
|| numvalue
> 0xff) {
1370 fprintf(stderr
, "bad numerical value %s\n", val
);
1373 x86_cpu_def
->model
= numvalue
;
1374 } else if (!strcmp(featurestr
, "stepping")) {
1376 numvalue
= strtoul(val
, &err
, 0);
1377 if (!*val
|| *err
|| numvalue
> 0xf) {
1378 fprintf(stderr
, "bad numerical value %s\n", val
);
1381 x86_cpu_def
->stepping
= numvalue
;
1382 } else if (!strcmp(featurestr
, "level")) {
1384 numvalue
= strtoul(val
, &err
, 0);
1385 if (!*val
|| *err
) {
1386 fprintf(stderr
, "bad numerical value %s\n", val
);
1389 x86_cpu_def
->level
= numvalue
;
1390 } else if (!strcmp(featurestr
, "xlevel")) {
1392 numvalue
= strtoul(val
, &err
, 0);
1393 if (!*val
|| *err
) {
1394 fprintf(stderr
, "bad numerical value %s\n", val
);
1397 if (numvalue
< 0x80000000) {
1398 numvalue
+= 0x80000000;
1400 x86_cpu_def
->xlevel
= numvalue
;
1401 } else if (!strcmp(featurestr
, "vendor")) {
1402 if (strlen(val
) != 12) {
1403 fprintf(stderr
, "vendor string must be 12 chars long\n");
1406 x86_cpu_def
->vendor1
= 0;
1407 x86_cpu_def
->vendor2
= 0;
1408 x86_cpu_def
->vendor3
= 0;
1409 for(i
= 0; i
< 4; i
++) {
1410 x86_cpu_def
->vendor1
|= ((uint8_t)val
[i
]) << (8 * i
);
1411 x86_cpu_def
->vendor2
|= ((uint8_t)val
[i
+ 4]) << (8 * i
);
1412 x86_cpu_def
->vendor3
|= ((uint8_t)val
[i
+ 8]) << (8 * i
);
1414 x86_cpu_def
->vendor_override
= 1;
1415 } else if (!strcmp(featurestr
, "model_id")) {
1416 pstrcpy(x86_cpu_def
->model_id
, sizeof(x86_cpu_def
->model_id
),
1418 } else if (!strcmp(featurestr
, "tsc_freq")) {
1422 tsc_freq
= strtosz_suffix_unit(val
, &err
,
1423 STRTOSZ_DEFSUFFIX_B
, 1000);
1424 if (tsc_freq
< 0 || *err
) {
1425 fprintf(stderr
, "bad numerical value %s\n", val
);
1428 x86_cpu_def
->tsc_khz
= tsc_freq
/ 1000;
1429 } else if (!strcmp(featurestr
, "hv_spinlocks")) {
1431 numvalue
= strtoul(val
, &err
, 0);
1432 if (!*val
|| *err
) {
1433 fprintf(stderr
, "bad numerical value %s\n", val
);
1436 hyperv_set_spinlock_retries(numvalue
);
1438 fprintf(stderr
, "unrecognized feature %s\n", featurestr
);
1441 } else if (!strcmp(featurestr
, "check")) {
1443 } else if (!strcmp(featurestr
, "enforce")) {
1444 check_cpuid
= enforce_cpuid
= 1;
1445 } else if (!strcmp(featurestr
, "hv_relaxed")) {
1446 hyperv_enable_relaxed_timing(true);
1447 } else if (!strcmp(featurestr
, "hv_vapic")) {
1448 hyperv_enable_vapic_recommended(true);
1450 fprintf(stderr
, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr
);
1453 featurestr
= strtok(NULL
, ",");
1455 x86_cpu_def
->features
|= plus_features
[FEAT_1_EDX
];
1456 x86_cpu_def
->ext_features
|= plus_features
[FEAT_1_ECX
];
1457 x86_cpu_def
->ext2_features
|= plus_features
[FEAT_8000_0001_EDX
];
1458 x86_cpu_def
->ext3_features
|= plus_features
[FEAT_8000_0001_ECX
];
1459 x86_cpu_def
->ext4_features
|= plus_features
[FEAT_C000_0001_EDX
];
1460 x86_cpu_def
->kvm_features
|= plus_features
[FEAT_KVM
];
1461 x86_cpu_def
->svm_features
|= plus_features
[FEAT_SVM
];
1462 x86_cpu_def
->cpuid_7_0_ebx_features
|= plus_features
[FEAT_7_0_EBX
];
1463 x86_cpu_def
->features
&= ~minus_features
[FEAT_1_EDX
];
1464 x86_cpu_def
->ext_features
&= ~minus_features
[FEAT_1_ECX
];
1465 x86_cpu_def
->ext2_features
&= ~minus_features
[FEAT_8000_0001_EDX
];
1466 x86_cpu_def
->ext3_features
&= ~minus_features
[FEAT_8000_0001_ECX
];
1467 x86_cpu_def
->ext4_features
&= ~minus_features
[FEAT_C000_0001_EDX
];
1468 x86_cpu_def
->kvm_features
&= ~minus_features
[FEAT_KVM
];
1469 x86_cpu_def
->svm_features
&= ~minus_features
[FEAT_SVM
];
1470 x86_cpu_def
->cpuid_7_0_ebx_features
&= ~minus_features
[FEAT_7_0_EBX
];
1477 /* generate a composite string into buf of all cpuid names in featureset
1478 * selected by fbits. indicate truncation at bufsize in the event of overflow.
1479 * if flags, suppress names undefined in featureset.
1481 static void listflags(char *buf
, int bufsize
, uint32_t fbits
,
1482 const char **featureset
, uint32_t flags
)
1484 const char **p
= &featureset
[31];
1488 b
= 4 <= bufsize
? buf
+ (bufsize
-= 3) - 1 : NULL
;
1490 for (q
= buf
, bit
= 31; fbits
&& bufsize
; --p
, fbits
&= ~(1 << bit
), --bit
)
1491 if (fbits
& 1 << bit
&& (*p
|| !flags
)) {
1493 nc
= snprintf(q
, bufsize
, "%s%s", q
== buf
? "" : " ", *p
);
1495 nc
= snprintf(q
, bufsize
, "%s[%d]", q
== buf
? "" : " ", bit
);
1496 if (bufsize
<= nc
) {
1498 memcpy(b
, "...", sizeof("..."));
1507 /* generate CPU information. */
1508 void x86_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
1514 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); i
++) {
1515 def
= &builtin_x86_defs
[i
];
1516 snprintf(buf
, sizeof(buf
), "%s", def
->name
);
1517 (*cpu_fprintf
)(f
, "x86 %16s %-48s\n", buf
, def
->model_id
);
1519 if (kvm_enabled()) {
1520 (*cpu_fprintf
)(f
, "x86 %16s\n", "[host]");
1522 (*cpu_fprintf
)(f
, "\nRecognized CPUID flags:\n");
1523 listflags(buf
, sizeof(buf
), (uint32_t)~0, feature_name
, 1);
1524 (*cpu_fprintf
)(f
, " %s\n", buf
);
1525 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext_feature_name
, 1);
1526 (*cpu_fprintf
)(f
, " %s\n", buf
);
1527 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext2_feature_name
, 1);
1528 (*cpu_fprintf
)(f
, " %s\n", buf
);
1529 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext3_feature_name
, 1);
1530 (*cpu_fprintf
)(f
, " %s\n", buf
);
1533 CpuDefinitionInfoList
*arch_query_cpu_definitions(Error
**errp
)
1535 CpuDefinitionInfoList
*cpu_list
= NULL
;
1539 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); i
++) {
1540 CpuDefinitionInfoList
*entry
;
1541 CpuDefinitionInfo
*info
;
1543 def
= &builtin_x86_defs
[i
];
1544 info
= g_malloc0(sizeof(*info
));
1545 info
->name
= g_strdup(def
->name
);
1547 entry
= g_malloc0(sizeof(*entry
));
1548 entry
->value
= info
;
1549 entry
->next
= cpu_list
;
1557 static void filter_features_for_kvm(X86CPU
*cpu
)
1559 CPUX86State
*env
= &cpu
->env
;
1560 KVMState
*s
= kvm_state
;
1562 env
->cpuid_features
&=
1563 kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
1564 env
->cpuid_ext_features
&=
1565 kvm_arch_get_supported_cpuid(s
, 1, 0, R_ECX
);
1566 env
->cpuid_ext2_features
&=
1567 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
1568 env
->cpuid_ext3_features
&=
1569 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
1570 env
->cpuid_svm_features
&=
1571 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
1572 env
->cpuid_7_0_ebx_features
&=
1573 kvm_arch_get_supported_cpuid(s
, 7, 0, R_EBX
);
1574 env
->cpuid_kvm_features
&=
1575 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
1576 env
->cpuid_ext4_features
&=
1577 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
1582 int cpu_x86_register(X86CPU
*cpu
, const char *cpu_model
)
1584 CPUX86State
*env
= &cpu
->env
;
1585 x86_def_t def1
, *def
= &def1
;
1586 Error
*error
= NULL
;
1587 char *name
, *features
;
1588 gchar
**model_pieces
;
1590 memset(def
, 0, sizeof(*def
));
1592 model_pieces
= g_strsplit(cpu_model
, ",", 2);
1593 if (!model_pieces
[0]) {
1594 error_setg(&error
, "Invalid/empty CPU model name");
1597 name
= model_pieces
[0];
1598 features
= model_pieces
[1];
1600 if (cpu_x86_find_by_name(def
, name
) < 0) {
1601 error_setg(&error
, "Unable to find CPU definition: %s", name
);
1605 if (kvm_enabled()) {
1606 def
->kvm_features
|= kvm_default_features
;
1608 def
->ext_features
|= CPUID_EXT_HYPERVISOR
;
1610 if (cpu_x86_parse_featurestr(def
, features
) < 0) {
1611 error_setg(&error
, "Invalid cpu_model string format: %s", cpu_model
);
1614 assert(def
->vendor1
);
1615 env
->cpuid_vendor1
= def
->vendor1
;
1616 env
->cpuid_vendor2
= def
->vendor2
;
1617 env
->cpuid_vendor3
= def
->vendor3
;
1618 env
->cpuid_vendor_override
= def
->vendor_override
;
1619 object_property_set_int(OBJECT(cpu
), def
->level
, "level", &error
);
1620 object_property_set_int(OBJECT(cpu
), def
->family
, "family", &error
);
1621 object_property_set_int(OBJECT(cpu
), def
->model
, "model", &error
);
1622 object_property_set_int(OBJECT(cpu
), def
->stepping
, "stepping", &error
);
1623 env
->cpuid_features
= def
->features
;
1624 env
->cpuid_ext_features
= def
->ext_features
;
1625 env
->cpuid_ext2_features
= def
->ext2_features
;
1626 env
->cpuid_ext3_features
= def
->ext3_features
;
1627 object_property_set_int(OBJECT(cpu
), def
->xlevel
, "xlevel", &error
);
1628 env
->cpuid_kvm_features
= def
->kvm_features
;
1629 env
->cpuid_svm_features
= def
->svm_features
;
1630 env
->cpuid_ext4_features
= def
->ext4_features
;
1631 env
->cpuid_7_0_ebx_features
= def
->cpuid_7_0_ebx_features
;
1632 env
->cpuid_xlevel2
= def
->xlevel2
;
1633 object_property_set_int(OBJECT(cpu
), (int64_t)def
->tsc_khz
* 1000,
1634 "tsc-frequency", &error
);
1636 object_property_set_str(OBJECT(cpu
), def
->model_id
, "model-id", &error
);
1639 g_strfreev(model_pieces
);
1641 fprintf(stderr
, "%s\n", error_get_pretty(error
));
1648 #if !defined(CONFIG_USER_ONLY)
1650 void cpu_clear_apic_feature(CPUX86State
*env
)
1652 env
->cpuid_features
&= ~CPUID_APIC
;
1655 #endif /* !CONFIG_USER_ONLY */
1657 /* Initialize list of CPU models, filling some non-static fields if necessary
1659 void x86_cpudef_setup(void)
1662 static const char *model_with_versions
[] = { "qemu32", "qemu64", "athlon" };
1664 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); ++i
) {
1665 x86_def_t
*def
= &builtin_x86_defs
[i
];
1667 /* Look for specific "cpudef" models that */
1668 /* have the QEMU version in .model_id */
1669 for (j
= 0; j
< ARRAY_SIZE(model_with_versions
); j
++) {
1670 if (strcmp(model_with_versions
[j
], def
->name
) == 0) {
1671 pstrcpy(def
->model_id
, sizeof(def
->model_id
),
1672 "QEMU Virtual CPU version ");
1673 pstrcat(def
->model_id
, sizeof(def
->model_id
),
1674 qemu_get_version());
1681 static void get_cpuid_vendor(CPUX86State
*env
, uint32_t *ebx
,
1682 uint32_t *ecx
, uint32_t *edx
)
1684 *ebx
= env
->cpuid_vendor1
;
1685 *edx
= env
->cpuid_vendor2
;
1686 *ecx
= env
->cpuid_vendor3
;
1688 /* sysenter isn't supported on compatibility mode on AMD, syscall
1689 * isn't supported in compatibility mode on Intel.
1690 * Normally we advertise the actual cpu vendor, but you can override
1691 * this if you want to use KVM's sysenter/syscall emulation
1692 * in compatibility mode and when doing cross vendor migration
1694 if (kvm_enabled() && ! env
->cpuid_vendor_override
) {
1695 host_cpuid(0, 0, NULL
, ebx
, ecx
, edx
);
1699 void cpu_x86_cpuid(CPUX86State
*env
, uint32_t index
, uint32_t count
,
1700 uint32_t *eax
, uint32_t *ebx
,
1701 uint32_t *ecx
, uint32_t *edx
)
1703 X86CPU
*cpu
= x86_env_get_cpu(env
);
1704 CPUState
*cs
= CPU(cpu
);
1706 /* test if maximum index reached */
1707 if (index
& 0x80000000) {
1708 if (index
> env
->cpuid_xlevel
) {
1709 if (env
->cpuid_xlevel2
> 0) {
1710 /* Handle the Centaur's CPUID instruction. */
1711 if (index
> env
->cpuid_xlevel2
) {
1712 index
= env
->cpuid_xlevel2
;
1713 } else if (index
< 0xC0000000) {
1714 index
= env
->cpuid_xlevel
;
1717 /* Intel documentation states that invalid EAX input will
1718 * return the same information as EAX=cpuid_level
1719 * (Intel SDM Vol. 2A - Instruction Set Reference - CPUID)
1721 index
= env
->cpuid_level
;
1725 if (index
> env
->cpuid_level
)
1726 index
= env
->cpuid_level
;
1731 *eax
= env
->cpuid_level
;
1732 get_cpuid_vendor(env
, ebx
, ecx
, edx
);
1735 *eax
= env
->cpuid_version
;
1736 *ebx
= (env
->cpuid_apic_id
<< 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1737 *ecx
= env
->cpuid_ext_features
;
1738 *edx
= env
->cpuid_features
;
1739 if (cs
->nr_cores
* cs
->nr_threads
> 1) {
1740 *ebx
|= (cs
->nr_cores
* cs
->nr_threads
) << 16;
1741 *edx
|= 1 << 28; /* HTT bit */
1745 /* cache info: needed for Pentium Pro compatibility */
1752 /* cache info: needed for Core compatibility */
1753 if (cs
->nr_cores
> 1) {
1754 *eax
= (cs
->nr_cores
- 1) << 26;
1759 case 0: /* L1 dcache info */
1765 case 1: /* L1 icache info */
1771 case 2: /* L2 cache info */
1773 if (cs
->nr_threads
> 1) {
1774 *eax
|= (cs
->nr_threads
- 1) << 14;
1780 default: /* end of info */
1789 /* mwait info: needed for Core compatibility */
1790 *eax
= 0; /* Smallest monitor-line size in bytes */
1791 *ebx
= 0; /* Largest monitor-line size in bytes */
1792 *ecx
= CPUID_MWAIT_EMX
| CPUID_MWAIT_IBE
;
1796 /* Thermal and Power Leaf */
1803 /* Structured Extended Feature Flags Enumeration Leaf */
1805 *eax
= 0; /* Maximum ECX value for sub-leaves */
1806 *ebx
= env
->cpuid_7_0_ebx_features
; /* Feature flags */
1807 *ecx
= 0; /* Reserved */
1808 *edx
= 0; /* Reserved */
1817 /* Direct Cache Access Information Leaf */
1818 *eax
= 0; /* Bits 0-31 in DCA_CAP MSR */
1824 /* Architectural Performance Monitoring Leaf */
1825 if (kvm_enabled()) {
1826 KVMState
*s
= cs
->kvm_state
;
1828 *eax
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EAX
);
1829 *ebx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EBX
);
1830 *ecx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_ECX
);
1831 *edx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EDX
);
1840 /* Processor Extended State */
1841 if (!(env
->cpuid_ext_features
& CPUID_EXT_XSAVE
)) {
1848 if (kvm_enabled()) {
1849 KVMState
*s
= cs
->kvm_state
;
1851 *eax
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EAX
);
1852 *ebx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EBX
);
1853 *ecx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_ECX
);
1854 *edx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EDX
);
1863 *eax
= env
->cpuid_xlevel
;
1864 *ebx
= env
->cpuid_vendor1
;
1865 *edx
= env
->cpuid_vendor2
;
1866 *ecx
= env
->cpuid_vendor3
;
1869 *eax
= env
->cpuid_version
;
1871 *ecx
= env
->cpuid_ext3_features
;
1872 *edx
= env
->cpuid_ext2_features
;
1874 /* The Linux kernel checks for the CMPLegacy bit and
1875 * discards multiple thread information if it is set.
1876 * So dont set it here for Intel to make Linux guests happy.
1878 if (cs
->nr_cores
* cs
->nr_threads
> 1) {
1879 uint32_t tebx
, tecx
, tedx
;
1880 get_cpuid_vendor(env
, &tebx
, &tecx
, &tedx
);
1881 if (tebx
!= CPUID_VENDOR_INTEL_1
||
1882 tedx
!= CPUID_VENDOR_INTEL_2
||
1883 tecx
!= CPUID_VENDOR_INTEL_3
) {
1884 *ecx
|= 1 << 1; /* CmpLegacy bit */
1891 *eax
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 0];
1892 *ebx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 1];
1893 *ecx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 2];
1894 *edx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 3];
1897 /* cache info (L1 cache) */
1904 /* cache info (L2 cache) */
1911 /* virtual & phys address size in low 2 bytes. */
1912 /* XXX: This value must match the one used in the MMU code. */
1913 if (env
->cpuid_ext2_features
& CPUID_EXT2_LM
) {
1914 /* 64 bit processor */
1915 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1916 *eax
= 0x00003028; /* 48 bits virtual, 40 bits physical */
1918 if (env
->cpuid_features
& CPUID_PSE36
)
1919 *eax
= 0x00000024; /* 36 bits physical */
1921 *eax
= 0x00000020; /* 32 bits physical */
1926 if (cs
->nr_cores
* cs
->nr_threads
> 1) {
1927 *ecx
|= (cs
->nr_cores
* cs
->nr_threads
) - 1;
1931 if (env
->cpuid_ext3_features
& CPUID_EXT3_SVM
) {
1932 *eax
= 0x00000001; /* SVM Revision */
1933 *ebx
= 0x00000010; /* nr of ASIDs */
1935 *edx
= env
->cpuid_svm_features
; /* optional features */
1944 *eax
= env
->cpuid_xlevel2
;
1950 /* Support for VIA CPU's CPUID instruction */
1951 *eax
= env
->cpuid_version
;
1954 *edx
= env
->cpuid_ext4_features
;
1959 /* Reserved for the future, and now filled with zero */
1966 /* reserved values: zero */
1975 /* CPUClass::reset() */
1976 static void x86_cpu_reset(CPUState
*s
)
1978 X86CPU
*cpu
= X86_CPU(s
);
1979 X86CPUClass
*xcc
= X86_CPU_GET_CLASS(cpu
);
1980 CPUX86State
*env
= &cpu
->env
;
1983 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
1984 qemu_log("CPU Reset (CPU %d)\n", s
->cpu_index
);
1985 log_cpu_state(env
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
1988 xcc
->parent_reset(s
);
1991 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
1995 env
->old_exception
= -1;
1997 /* init to reset state */
1999 #ifdef CONFIG_SOFTMMU
2000 env
->hflags
|= HF_SOFTMMU_MASK
;
2002 env
->hflags2
|= HF2_GIF_MASK
;
2004 cpu_x86_update_cr0(env
, 0x60000010);
2005 env
->a20_mask
= ~0x0;
2006 env
->smbase
= 0x30000;
2008 env
->idt
.limit
= 0xffff;
2009 env
->gdt
.limit
= 0xffff;
2010 env
->ldt
.limit
= 0xffff;
2011 env
->ldt
.flags
= DESC_P_MASK
| (2 << DESC_TYPE_SHIFT
);
2012 env
->tr
.limit
= 0xffff;
2013 env
->tr
.flags
= DESC_P_MASK
| (11 << DESC_TYPE_SHIFT
);
2015 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff,
2016 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
|
2017 DESC_R_MASK
| DESC_A_MASK
);
2018 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff,
2019 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2021 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff,
2022 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2024 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff,
2025 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2027 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff,
2028 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2030 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff,
2031 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2035 env
->regs
[R_EDX
] = env
->cpuid_version
;
2040 for (i
= 0; i
< 8; i
++) {
2045 env
->mxcsr
= 0x1f80;
2047 env
->pat
= 0x0007040600070406ULL
;
2048 env
->msr_ia32_misc_enable
= MSR_IA32_MISC_ENABLE_DEFAULT
;
2050 memset(env
->dr
, 0, sizeof(env
->dr
));
2051 env
->dr
[6] = DR6_FIXED_1
;
2052 env
->dr
[7] = DR7_FIXED_1
;
2053 cpu_breakpoint_remove_all(env
, BP_CPU
);
2054 cpu_watchpoint_remove_all(env
, BP_CPU
);
2056 #if !defined(CONFIG_USER_ONLY)
2057 /* We hard-wire the BSP to the first CPU. */
2058 if (s
->cpu_index
== 0) {
2059 apic_designate_bsp(env
->apic_state
);
2062 env
->halted
= !cpu_is_bsp(cpu
);
2066 #ifndef CONFIG_USER_ONLY
2067 bool cpu_is_bsp(X86CPU
*cpu
)
2069 return cpu_get_apic_base(cpu
->env
.apic_state
) & MSR_IA32_APICBASE_BSP
;
2072 /* TODO: remove me, when reset over QOM tree is implemented */
2073 static void x86_cpu_machine_reset_cb(void *opaque
)
2075 X86CPU
*cpu
= opaque
;
2076 cpu_reset(CPU(cpu
));
2080 static void mce_init(X86CPU
*cpu
)
2082 CPUX86State
*cenv
= &cpu
->env
;
2085 if (((cenv
->cpuid_version
>> 8) & 0xf) >= 6
2086 && (cenv
->cpuid_features
& (CPUID_MCE
| CPUID_MCA
)) ==
2087 (CPUID_MCE
| CPUID_MCA
)) {
2088 cenv
->mcg_cap
= MCE_CAP_DEF
| MCE_BANKS_DEF
;
2089 cenv
->mcg_ctl
= ~(uint64_t)0;
2090 for (bank
= 0; bank
< MCE_BANKS_DEF
; bank
++) {
2091 cenv
->mce_banks
[bank
* 4] = ~(uint64_t)0;
2096 #define MSI_ADDR_BASE 0xfee00000
2098 #ifndef CONFIG_USER_ONLY
2099 static void x86_cpu_apic_init(X86CPU
*cpu
, Error
**errp
)
2101 static int apic_mapped
;
2102 CPUX86State
*env
= &cpu
->env
;
2103 APICCommonState
*apic
;
2104 const char *apic_type
= "apic";
2106 if (kvm_irqchip_in_kernel()) {
2107 apic_type
= "kvm-apic";
2108 } else if (xen_enabled()) {
2109 apic_type
= "xen-apic";
2112 env
->apic_state
= qdev_try_create(NULL
, apic_type
);
2113 if (env
->apic_state
== NULL
) {
2114 error_setg(errp
, "APIC device '%s' could not be created", apic_type
);
2118 object_property_add_child(OBJECT(cpu
), "apic",
2119 OBJECT(env
->apic_state
), NULL
);
2120 qdev_prop_set_uint8(env
->apic_state
, "id", env
->cpuid_apic_id
);
2121 /* TODO: convert to link<> */
2122 apic
= APIC_COMMON(env
->apic_state
);
2125 if (qdev_init(env
->apic_state
)) {
2126 error_setg(errp
, "APIC device '%s' could not be initialized",
2127 object_get_typename(OBJECT(env
->apic_state
)));
2131 /* XXX: mapping more APICs at the same memory location */
2132 if (apic_mapped
== 0) {
2133 /* NOTE: the APIC is directly connected to the CPU - it is not
2134 on the global memory bus. */
2135 /* XXX: what if the base changes? */
2136 sysbus_mmio_map(SYS_BUS_DEVICE(env
->apic_state
), 0, MSI_ADDR_BASE
);
2142 void x86_cpu_realize(Object
*obj
, Error
**errp
)
2144 X86CPU
*cpu
= X86_CPU(obj
);
2145 CPUX86State
*env
= &cpu
->env
;
2147 if (env
->cpuid_7_0_ebx_features
&& env
->cpuid_level
< 7) {
2148 env
->cpuid_level
= 7;
2151 /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on
2154 if (env
->cpuid_vendor1
== CPUID_VENDOR_AMD_1
&&
2155 env
->cpuid_vendor2
== CPUID_VENDOR_AMD_2
&&
2156 env
->cpuid_vendor3
== CPUID_VENDOR_AMD_3
) {
2157 env
->cpuid_ext2_features
&= ~CPUID_EXT2_AMD_ALIASES
;
2158 env
->cpuid_ext2_features
|= (env
->cpuid_features
2159 & CPUID_EXT2_AMD_ALIASES
);
2162 if (!kvm_enabled()) {
2163 env
->cpuid_features
&= TCG_FEATURES
;
2164 env
->cpuid_ext_features
&= TCG_EXT_FEATURES
;
2165 env
->cpuid_ext2_features
&= (TCG_EXT2_FEATURES
2166 #ifdef TARGET_X86_64
2167 | CPUID_EXT2_SYSCALL
| CPUID_EXT2_LM
2170 env
->cpuid_ext3_features
&= TCG_EXT3_FEATURES
;
2171 env
->cpuid_svm_features
&= TCG_SVM_FEATURES
;
2174 filter_features_for_kvm(cpu
);
2176 if (check_cpuid
&& kvm_check_features_against_host(cpu
)
2178 error_setg(errp
, "Host's CPU doesn't support requested features");
2183 #ifndef CONFIG_USER_ONLY
2184 qemu_register_reset(x86_cpu_machine_reset_cb
, cpu
);
2186 if (cpu
->env
.cpuid_features
& CPUID_APIC
|| smp_cpus
> 1) {
2187 x86_cpu_apic_init(cpu
, errp
);
2188 if (error_is_set(errp
)) {
2195 qemu_init_vcpu(&cpu
->env
);
2196 cpu_reset(CPU(cpu
));
2199 /* Enables contiguous-apic-ID mode, for compatibility */
2200 static bool compat_apic_id_mode
;
2202 void enable_compat_apic_id_mode(void)
2204 compat_apic_id_mode
= true;
2207 /* Calculates initial APIC ID for a specific CPU index
2209 * Currently we need to be able to calculate the APIC ID from the CPU index
2210 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
2211 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
2212 * all CPUs up to max_cpus.
2214 uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index
)
2216 uint32_t correct_id
;
2219 correct_id
= x86_apicid_from_cpu_idx(smp_cores
, smp_threads
, cpu_index
);
2220 if (compat_apic_id_mode
) {
2221 if (cpu_index
!= correct_id
&& !warned
) {
2222 error_report("APIC IDs set in compatibility mode, "
2223 "CPU topology won't match the configuration");
2232 static void x86_cpu_initfn(Object
*obj
)
2234 CPUState
*cs
= CPU(obj
);
2235 X86CPU
*cpu
= X86_CPU(obj
);
2236 CPUX86State
*env
= &cpu
->env
;
2241 object_property_add(obj
, "family", "int",
2242 x86_cpuid_version_get_family
,
2243 x86_cpuid_version_set_family
, NULL
, NULL
, NULL
);
2244 object_property_add(obj
, "model", "int",
2245 x86_cpuid_version_get_model
,
2246 x86_cpuid_version_set_model
, NULL
, NULL
, NULL
);
2247 object_property_add(obj
, "stepping", "int",
2248 x86_cpuid_version_get_stepping
,
2249 x86_cpuid_version_set_stepping
, NULL
, NULL
, NULL
);
2250 object_property_add(obj
, "level", "int",
2251 x86_cpuid_get_level
,
2252 x86_cpuid_set_level
, NULL
, NULL
, NULL
);
2253 object_property_add(obj
, "xlevel", "int",
2254 x86_cpuid_get_xlevel
,
2255 x86_cpuid_set_xlevel
, NULL
, NULL
, NULL
);
2256 object_property_add_str(obj
, "vendor",
2257 x86_cpuid_get_vendor
,
2258 x86_cpuid_set_vendor
, NULL
);
2259 object_property_add_str(obj
, "model-id",
2260 x86_cpuid_get_model_id
,
2261 x86_cpuid_set_model_id
, NULL
);
2262 object_property_add(obj
, "tsc-frequency", "int",
2263 x86_cpuid_get_tsc_freq
,
2264 x86_cpuid_set_tsc_freq
, NULL
, NULL
, NULL
);
2266 env
->cpuid_apic_id
= x86_cpu_apic_id_from_index(cs
->cpu_index
);
2268 /* init various static tables used in TCG mode */
2269 if (tcg_enabled() && !inited
) {
2271 optimize_flags_init();
2272 #ifndef CONFIG_USER_ONLY
2273 cpu_set_debug_excp_handler(breakpoint_handler
);
2278 static void x86_cpu_common_class_init(ObjectClass
*oc
, void *data
)
2280 X86CPUClass
*xcc
= X86_CPU_CLASS(oc
);
2281 CPUClass
*cc
= CPU_CLASS(oc
);
2283 xcc
->parent_reset
= cc
->reset
;
2284 cc
->reset
= x86_cpu_reset
;
2287 static const TypeInfo x86_cpu_type_info
= {
2288 .name
= TYPE_X86_CPU
,
2290 .instance_size
= sizeof(X86CPU
),
2291 .instance_init
= x86_cpu_initfn
,
2293 .class_size
= sizeof(X86CPUClass
),
2294 .class_init
= x86_cpu_common_class_init
,
2297 static void x86_cpu_register_types(void)
2299 type_register_static(&x86_cpu_type_info
);
2302 type_init(x86_cpu_register_types
)