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
{
338 struct x86_def_t
*next
;
341 uint32_t vendor1
, vendor2
, vendor3
;
346 uint32_t features
, ext_features
, ext2_features
, ext3_features
;
347 uint32_t kvm_features
, svm_features
;
351 /* Store the results of Centaur's CPUID instructions */
352 uint32_t ext4_features
;
354 /* The feature bits on CPUID[EAX=7,ECX=0].EBX */
355 uint32_t cpuid_7_0_ebx_features
;
358 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
359 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
360 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
361 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
362 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
363 CPUID_PSE36 | CPUID_FXSR)
364 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
365 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
366 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
367 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
368 CPUID_PAE | CPUID_SEP | CPUID_APIC)
370 #define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
371 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
372 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
373 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
374 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
375 /* partly implemented:
376 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
377 CPUID_PSE36 (needed for Solaris) */
379 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
380 #define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \
381 CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT | \
382 CPUID_EXT_HYPERVISOR)
384 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
385 CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_XSAVE */
386 #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
387 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
388 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
390 CPUID_EXT2_PDPE1GB */
391 #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
392 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
393 #define TCG_SVM_FEATURES 0
394 #define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP)
396 /* maintains list of cpu model definitions
398 static x86_def_t
*x86_defs
= {NULL
};
400 /* built-in cpu model definitions (deprecated)
402 static x86_def_t builtin_x86_defs
[] = {
406 .vendor1
= CPUID_VENDOR_AMD_1
,
407 .vendor2
= CPUID_VENDOR_AMD_2
,
408 .vendor3
= CPUID_VENDOR_AMD_3
,
412 .features
= PPRO_FEATURES
|
413 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
415 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
| CPUID_EXT_POPCNT
,
416 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
417 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
418 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
419 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
420 .xlevel
= 0x8000000A,
425 .vendor1
= CPUID_VENDOR_AMD_1
,
426 .vendor2
= CPUID_VENDOR_AMD_2
,
427 .vendor3
= CPUID_VENDOR_AMD_3
,
431 .features
= PPRO_FEATURES
|
432 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
433 CPUID_PSE36
| CPUID_VME
| CPUID_HT
,
434 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_CX16
|
436 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
437 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
438 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
| CPUID_EXT2_MMXEXT
|
439 CPUID_EXT2_FFXSR
| CPUID_EXT2_PDPE1GB
| CPUID_EXT2_RDTSCP
,
440 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
442 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
443 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
444 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
445 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
446 .svm_features
= CPUID_SVM_NPT
| CPUID_SVM_LBRV
,
447 .xlevel
= 0x8000001A,
448 .model_id
= "AMD Phenom(tm) 9550 Quad-Core Processor"
453 .vendor1
= CPUID_VENDOR_INTEL_1
,
454 .vendor2
= CPUID_VENDOR_INTEL_2
,
455 .vendor3
= CPUID_VENDOR_INTEL_3
,
459 .features
= PPRO_FEATURES
|
460 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
461 CPUID_PSE36
| CPUID_VME
| CPUID_DTS
| CPUID_ACPI
| CPUID_SS
|
462 CPUID_HT
| CPUID_TM
| CPUID_PBE
,
463 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
464 CPUID_EXT_DTES64
| CPUID_EXT_DSCPL
| CPUID_EXT_VMX
| CPUID_EXT_EST
|
465 CPUID_EXT_TM2
| CPUID_EXT_CX16
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
466 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
467 .ext3_features
= CPUID_EXT3_LAHF_LM
,
468 .xlevel
= 0x80000008,
469 .model_id
= "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
474 .vendor1
= CPUID_VENDOR_INTEL_1
,
475 .vendor2
= CPUID_VENDOR_INTEL_2
,
476 .vendor3
= CPUID_VENDOR_INTEL_3
,
480 /* Missing: CPUID_VME, CPUID_HT */
481 .features
= PPRO_FEATURES
|
482 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
484 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
485 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
,
486 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
487 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
488 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
489 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
490 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
491 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
492 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
494 .xlevel
= 0x80000008,
495 .model_id
= "Common KVM processor"
500 .vendor1
= CPUID_VENDOR_INTEL_1
,
501 .vendor2
= CPUID_VENDOR_INTEL_2
,
502 .vendor3
= CPUID_VENDOR_INTEL_3
,
506 .features
= PPRO_FEATURES
,
507 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_POPCNT
,
508 .xlevel
= 0x80000004,
513 .vendor1
= CPUID_VENDOR_INTEL_1
,
514 .vendor2
= CPUID_VENDOR_INTEL_2
,
515 .vendor3
= CPUID_VENDOR_INTEL_3
,
519 .features
= PPRO_FEATURES
|
520 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_PSE36
,
521 .ext_features
= CPUID_EXT_SSE3
,
522 .ext2_features
= PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
,
524 .xlevel
= 0x80000008,
525 .model_id
= "Common 32-bit KVM processor"
530 .vendor1
= CPUID_VENDOR_INTEL_1
,
531 .vendor2
= CPUID_VENDOR_INTEL_2
,
532 .vendor3
= CPUID_VENDOR_INTEL_3
,
536 .features
= PPRO_FEATURES
| CPUID_VME
|
537 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_DTS
| CPUID_ACPI
|
538 CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
539 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_VMX
|
540 CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
541 .ext2_features
= CPUID_EXT2_NX
,
542 .xlevel
= 0x80000008,
543 .model_id
= "Genuine Intel(R) CPU T2600 @ 2.16GHz",
548 .vendor1
= CPUID_VENDOR_INTEL_1
,
549 .vendor2
= CPUID_VENDOR_INTEL_2
,
550 .vendor3
= CPUID_VENDOR_INTEL_3
,
554 .features
= I486_FEATURES
,
560 .vendor1
= CPUID_VENDOR_INTEL_1
,
561 .vendor2
= CPUID_VENDOR_INTEL_2
,
562 .vendor3
= CPUID_VENDOR_INTEL_3
,
566 .features
= PENTIUM_FEATURES
,
572 .vendor1
= CPUID_VENDOR_INTEL_1
,
573 .vendor2
= CPUID_VENDOR_INTEL_2
,
574 .vendor3
= CPUID_VENDOR_INTEL_3
,
578 .features
= PENTIUM2_FEATURES
,
584 .vendor1
= CPUID_VENDOR_INTEL_1
,
585 .vendor2
= CPUID_VENDOR_INTEL_2
,
586 .vendor3
= CPUID_VENDOR_INTEL_3
,
590 .features
= PENTIUM3_FEATURES
,
596 .vendor1
= CPUID_VENDOR_AMD_1
,
597 .vendor2
= CPUID_VENDOR_AMD_2
,
598 .vendor3
= CPUID_VENDOR_AMD_3
,
602 .features
= PPRO_FEATURES
| CPUID_PSE36
| CPUID_VME
| CPUID_MTRR
|
604 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
605 CPUID_EXT2_MMXEXT
| CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
606 .xlevel
= 0x80000008,
610 /* original is on level 10 */
612 .vendor1
= CPUID_VENDOR_INTEL_1
,
613 .vendor2
= CPUID_VENDOR_INTEL_2
,
614 .vendor3
= CPUID_VENDOR_INTEL_3
,
618 .features
= PPRO_FEATURES
|
619 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_VME
| CPUID_DTS
|
620 CPUID_ACPI
| CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
621 /* Some CPUs got no CPUID_SEP */
622 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
623 CPUID_EXT_DSCPL
| CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
,
624 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
626 .ext3_features
= CPUID_EXT3_LAHF_LM
,
627 .xlevel
= 0x8000000A,
628 .model_id
= "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
633 .vendor1
= CPUID_VENDOR_INTEL_1
,
634 .vendor2
= CPUID_VENDOR_INTEL_2
,
635 .vendor3
= CPUID_VENDOR_INTEL_3
,
639 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
640 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
641 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
642 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
643 CPUID_DE
| CPUID_FP87
,
644 .ext_features
= CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
645 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
646 .ext3_features
= CPUID_EXT3_LAHF_LM
,
647 .xlevel
= 0x8000000A,
648 .model_id
= "Intel Celeron_4x0 (Conroe/Merom Class Core 2)",
653 .vendor1
= CPUID_VENDOR_INTEL_1
,
654 .vendor2
= CPUID_VENDOR_INTEL_2
,
655 .vendor3
= CPUID_VENDOR_INTEL_3
,
659 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
660 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
661 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
662 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
663 CPUID_DE
| CPUID_FP87
,
664 .ext_features
= CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
666 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
667 .ext3_features
= CPUID_EXT3_LAHF_LM
,
668 .xlevel
= 0x8000000A,
669 .model_id
= "Intel Core 2 Duo P9xxx (Penryn Class Core 2)",
674 .vendor1
= CPUID_VENDOR_INTEL_1
,
675 .vendor2
= CPUID_VENDOR_INTEL_2
,
676 .vendor3
= CPUID_VENDOR_INTEL_3
,
680 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
681 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
682 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
683 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
684 CPUID_DE
| CPUID_FP87
,
685 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
686 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
687 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
688 .ext3_features
= CPUID_EXT3_LAHF_LM
,
689 .xlevel
= 0x8000000A,
690 .model_id
= "Intel Core i7 9xx (Nehalem Class Core i7)",
695 .vendor1
= CPUID_VENDOR_INTEL_1
,
696 .vendor2
= CPUID_VENDOR_INTEL_2
,
697 .vendor3
= CPUID_VENDOR_INTEL_3
,
701 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
702 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
703 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
704 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
705 CPUID_DE
| CPUID_FP87
,
706 .ext_features
= CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
707 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
709 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
710 .ext3_features
= CPUID_EXT3_LAHF_LM
,
711 .xlevel
= 0x8000000A,
712 .model_id
= "Westmere E56xx/L56xx/X56xx (Nehalem-C)",
715 .name
= "SandyBridge",
717 .vendor1
= CPUID_VENDOR_INTEL_1
,
718 .vendor2
= CPUID_VENDOR_INTEL_2
,
719 .vendor3
= CPUID_VENDOR_INTEL_3
,
723 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
724 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
725 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
726 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
727 CPUID_DE
| CPUID_FP87
,
728 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
729 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_POPCNT
|
730 CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
731 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
733 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
735 .ext3_features
= CPUID_EXT3_LAHF_LM
,
736 .xlevel
= 0x8000000A,
737 .model_id
= "Intel Xeon E312xx (Sandy Bridge)",
742 .vendor1
= CPUID_VENDOR_INTEL_1
,
743 .vendor2
= CPUID_VENDOR_INTEL_2
,
744 .vendor3
= CPUID_VENDOR_INTEL_3
,
748 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
749 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
750 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
751 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
752 CPUID_DE
| CPUID_FP87
,
753 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
754 CPUID_EXT_POPCNT
| CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
|
755 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
756 CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
|
757 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_FMA
| CPUID_EXT_MOVBE
|
759 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
761 .ext3_features
= CPUID_EXT3_LAHF_LM
,
762 .cpuid_7_0_ebx_features
= CPUID_7_0_EBX_FSGSBASE
| CPUID_7_0_EBX_BMI1
|
763 CPUID_7_0_EBX_HLE
| CPUID_7_0_EBX_AVX2
| CPUID_7_0_EBX_SMEP
|
764 CPUID_7_0_EBX_BMI2
| CPUID_7_0_EBX_ERMS
| CPUID_7_0_EBX_INVPCID
|
766 .xlevel
= 0x8000000A,
767 .model_id
= "Intel Core Processor (Haswell)",
770 .name
= "Opteron_G1",
772 .vendor1
= CPUID_VENDOR_AMD_1
,
773 .vendor2
= CPUID_VENDOR_AMD_2
,
774 .vendor3
= CPUID_VENDOR_AMD_3
,
778 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
779 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
780 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
781 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
782 CPUID_DE
| CPUID_FP87
,
783 .ext_features
= CPUID_EXT_SSE3
,
784 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
785 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
786 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
787 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
788 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
789 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
790 .xlevel
= 0x80000008,
791 .model_id
= "AMD Opteron 240 (Gen 1 Class Opteron)",
794 .name
= "Opteron_G2",
796 .vendor1
= CPUID_VENDOR_AMD_1
,
797 .vendor2
= CPUID_VENDOR_AMD_2
,
798 .vendor3
= CPUID_VENDOR_AMD_3
,
802 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
803 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
804 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
805 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
806 CPUID_DE
| CPUID_FP87
,
807 .ext_features
= CPUID_EXT_CX16
| CPUID_EXT_SSE3
,
808 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
809 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
810 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
811 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
812 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
813 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
814 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
815 .ext3_features
= CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
816 .xlevel
= 0x80000008,
817 .model_id
= "AMD Opteron 22xx (Gen 2 Class Opteron)",
820 .name
= "Opteron_G3",
822 .vendor1
= CPUID_VENDOR_AMD_1
,
823 .vendor2
= CPUID_VENDOR_AMD_2
,
824 .vendor3
= CPUID_VENDOR_AMD_3
,
828 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
829 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
830 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
831 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
832 CPUID_DE
| CPUID_FP87
,
833 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_CX16
| CPUID_EXT_MONITOR
|
835 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
836 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
837 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
838 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
839 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
840 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
841 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
842 .ext3_features
= CPUID_EXT3_MISALIGNSSE
| CPUID_EXT3_SSE4A
|
843 CPUID_EXT3_ABM
| CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
844 .xlevel
= 0x80000008,
845 .model_id
= "AMD Opteron 23xx (Gen 3 Class Opteron)",
848 .name
= "Opteron_G4",
850 .vendor1
= CPUID_VENDOR_AMD_1
,
851 .vendor2
= CPUID_VENDOR_AMD_2
,
852 .vendor3
= CPUID_VENDOR_AMD_3
,
856 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
857 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
858 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
859 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
860 CPUID_DE
| CPUID_FP87
,
861 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
862 CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
863 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
865 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
866 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
867 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
868 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
869 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
870 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
871 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
872 .ext3_features
= CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
873 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
874 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
876 .xlevel
= 0x8000001A,
877 .model_id
= "AMD Opteron 62xx class CPU",
880 .name
= "Opteron_G5",
882 .vendor1
= CPUID_VENDOR_AMD_1
,
883 .vendor2
= CPUID_VENDOR_AMD_2
,
884 .vendor3
= CPUID_VENDOR_AMD_3
,
888 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
889 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
890 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
891 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
892 CPUID_DE
| CPUID_FP87
,
893 .ext_features
= CPUID_EXT_F16C
| CPUID_EXT_AVX
| CPUID_EXT_XSAVE
|
894 CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
895 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_FMA
|
896 CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
,
897 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
898 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
899 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
900 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
901 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
902 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
903 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
904 .ext3_features
= CPUID_EXT3_TBM
| CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
905 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
906 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
908 .xlevel
= 0x8000001A,
909 .model_id
= "AMD Opteron 63xx class CPU",
914 static int cpu_x86_fill_model_id(char *str
)
916 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
919 for (i
= 0; i
< 3; i
++) {
920 host_cpuid(0x80000002 + i
, 0, &eax
, &ebx
, &ecx
, &edx
);
921 memcpy(str
+ i
* 16 + 0, &eax
, 4);
922 memcpy(str
+ i
* 16 + 4, &ebx
, 4);
923 memcpy(str
+ i
* 16 + 8, &ecx
, 4);
924 memcpy(str
+ i
* 16 + 12, &edx
, 4);
930 /* Fill a x86_def_t struct with information about the host CPU, and
931 * the CPU features supported by the host hardware + host kernel
933 * This function may be called only if KVM is enabled.
935 static void kvm_cpu_fill_host(x86_def_t
*x86_cpu_def
)
938 KVMState
*s
= kvm_state
;
939 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
941 assert(kvm_enabled());
943 x86_cpu_def
->name
= "host";
944 host_cpuid(0x0, 0, &eax
, &ebx
, &ecx
, &edx
);
945 x86_cpu_def
->vendor1
= ebx
;
946 x86_cpu_def
->vendor2
= edx
;
947 x86_cpu_def
->vendor3
= ecx
;
949 host_cpuid(0x1, 0, &eax
, &ebx
, &ecx
, &edx
);
950 x86_cpu_def
->family
= ((eax
>> 8) & 0x0F) + ((eax
>> 20) & 0xFF);
951 x86_cpu_def
->model
= ((eax
>> 4) & 0x0F) | ((eax
& 0xF0000) >> 12);
952 x86_cpu_def
->stepping
= eax
& 0x0F;
954 x86_cpu_def
->level
= kvm_arch_get_supported_cpuid(s
, 0x0, 0, R_EAX
);
955 x86_cpu_def
->features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_EDX
);
956 x86_cpu_def
->ext_features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_ECX
);
958 if (x86_cpu_def
->level
>= 7) {
959 x86_cpu_def
->cpuid_7_0_ebx_features
=
960 kvm_arch_get_supported_cpuid(s
, 0x7, 0, R_EBX
);
962 x86_cpu_def
->cpuid_7_0_ebx_features
= 0;
965 x86_cpu_def
->xlevel
= kvm_arch_get_supported_cpuid(s
, 0x80000000, 0, R_EAX
);
966 x86_cpu_def
->ext2_features
=
967 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
968 x86_cpu_def
->ext3_features
=
969 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
971 cpu_x86_fill_model_id(x86_cpu_def
->model_id
);
972 x86_cpu_def
->vendor_override
= 0;
974 /* Call Centaur's CPUID instruction. */
975 if (x86_cpu_def
->vendor1
== CPUID_VENDOR_VIA_1
&&
976 x86_cpu_def
->vendor2
== CPUID_VENDOR_VIA_2
&&
977 x86_cpu_def
->vendor3
== CPUID_VENDOR_VIA_3
) {
978 host_cpuid(0xC0000000, 0, &eax
, &ebx
, &ecx
, &edx
);
979 eax
= kvm_arch_get_supported_cpuid(s
, 0xC0000000, 0, R_EAX
);
980 if (eax
>= 0xC0000001) {
981 /* Support VIA max extended level */
982 x86_cpu_def
->xlevel2
= eax
;
983 host_cpuid(0xC0000001, 0, &eax
, &ebx
, &ecx
, &edx
);
984 x86_cpu_def
->ext4_features
=
985 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
989 /* Other KVM-specific feature fields: */
990 x86_cpu_def
->svm_features
=
991 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
992 x86_cpu_def
->kvm_features
=
993 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
995 #endif /* CONFIG_KVM */
998 static int unavailable_host_feature(FeatureWordInfo
*f
, uint32_t mask
)
1002 for (i
= 0; i
< 32; ++i
)
1003 if (1 << i
& mask
) {
1004 const char *reg
= get_register_name_32(f
->cpuid_reg
);
1006 fprintf(stderr
, "warning: host doesn't support requested feature: "
1007 "CPUID.%02XH:%s%s%s [bit %d]\n",
1009 f
->feat_names
[i
] ? "." : "",
1010 f
->feat_names
[i
] ? f
->feat_names
[i
] : "", i
);
1016 /* Check if all requested cpu flags are making their way to the guest
1018 * Returns 0 if all flags are supported by the host, non-zero otherwise.
1020 * This function may be called only if KVM is enabled.
1022 static int kvm_check_features_against_host(X86CPU
*cpu
)
1024 CPUX86State
*env
= &cpu
->env
;
1028 struct model_features_t ft
[] = {
1029 {&env
->cpuid_features
, &host_def
.features
,
1031 {&env
->cpuid_ext_features
, &host_def
.ext_features
,
1033 {&env
->cpuid_ext2_features
, &host_def
.ext2_features
,
1034 FEAT_8000_0001_EDX
},
1035 {&env
->cpuid_ext3_features
, &host_def
.ext3_features
,
1036 FEAT_8000_0001_ECX
},
1037 {&env
->cpuid_ext4_features
, &host_def
.ext4_features
,
1038 FEAT_C000_0001_EDX
},
1039 {&env
->cpuid_7_0_ebx_features
, &host_def
.cpuid_7_0_ebx_features
,
1041 {&env
->cpuid_svm_features
, &host_def
.svm_features
,
1043 {&env
->cpuid_kvm_features
, &host_def
.kvm_features
,
1047 assert(kvm_enabled());
1049 kvm_cpu_fill_host(&host_def
);
1050 for (rv
= 0, i
= 0; i
< ARRAY_SIZE(ft
); ++i
) {
1051 FeatureWord w
= ft
[i
].feat_word
;
1052 FeatureWordInfo
*wi
= &feature_word_info
[w
];
1053 for (mask
= 1; mask
; mask
<<= 1) {
1054 if (*ft
[i
].guest_feat
& mask
&&
1055 !(*ft
[i
].host_feat
& mask
)) {
1056 unavailable_host_feature(wi
, mask
);
1064 static void x86_cpuid_version_get_family(Object
*obj
, Visitor
*v
, void *opaque
,
1065 const char *name
, Error
**errp
)
1067 X86CPU
*cpu
= X86_CPU(obj
);
1068 CPUX86State
*env
= &cpu
->env
;
1071 value
= (env
->cpuid_version
>> 8) & 0xf;
1073 value
+= (env
->cpuid_version
>> 20) & 0xff;
1075 visit_type_int(v
, &value
, name
, errp
);
1078 static void x86_cpuid_version_set_family(Object
*obj
, Visitor
*v
, void *opaque
,
1079 const char *name
, Error
**errp
)
1081 X86CPU
*cpu
= X86_CPU(obj
);
1082 CPUX86State
*env
= &cpu
->env
;
1083 const int64_t min
= 0;
1084 const int64_t max
= 0xff + 0xf;
1087 visit_type_int(v
, &value
, name
, errp
);
1088 if (error_is_set(errp
)) {
1091 if (value
< min
|| value
> max
) {
1092 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1093 name
? name
: "null", value
, min
, max
);
1097 env
->cpuid_version
&= ~0xff00f00;
1099 env
->cpuid_version
|= 0xf00 | ((value
- 0x0f) << 20);
1101 env
->cpuid_version
|= value
<< 8;
1105 static void x86_cpuid_version_get_model(Object
*obj
, Visitor
*v
, void *opaque
,
1106 const char *name
, Error
**errp
)
1108 X86CPU
*cpu
= X86_CPU(obj
);
1109 CPUX86State
*env
= &cpu
->env
;
1112 value
= (env
->cpuid_version
>> 4) & 0xf;
1113 value
|= ((env
->cpuid_version
>> 16) & 0xf) << 4;
1114 visit_type_int(v
, &value
, name
, errp
);
1117 static void x86_cpuid_version_set_model(Object
*obj
, Visitor
*v
, void *opaque
,
1118 const char *name
, Error
**errp
)
1120 X86CPU
*cpu
= X86_CPU(obj
);
1121 CPUX86State
*env
= &cpu
->env
;
1122 const int64_t min
= 0;
1123 const int64_t max
= 0xff;
1126 visit_type_int(v
, &value
, name
, errp
);
1127 if (error_is_set(errp
)) {
1130 if (value
< min
|| value
> max
) {
1131 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1132 name
? name
: "null", value
, min
, max
);
1136 env
->cpuid_version
&= ~0xf00f0;
1137 env
->cpuid_version
|= ((value
& 0xf) << 4) | ((value
>> 4) << 16);
1140 static void x86_cpuid_version_get_stepping(Object
*obj
, Visitor
*v
,
1141 void *opaque
, const char *name
,
1144 X86CPU
*cpu
= X86_CPU(obj
);
1145 CPUX86State
*env
= &cpu
->env
;
1148 value
= env
->cpuid_version
& 0xf;
1149 visit_type_int(v
, &value
, name
, errp
);
1152 static void x86_cpuid_version_set_stepping(Object
*obj
, Visitor
*v
,
1153 void *opaque
, const char *name
,
1156 X86CPU
*cpu
= X86_CPU(obj
);
1157 CPUX86State
*env
= &cpu
->env
;
1158 const int64_t min
= 0;
1159 const int64_t max
= 0xf;
1162 visit_type_int(v
, &value
, name
, errp
);
1163 if (error_is_set(errp
)) {
1166 if (value
< min
|| value
> max
) {
1167 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1168 name
? name
: "null", value
, min
, max
);
1172 env
->cpuid_version
&= ~0xf;
1173 env
->cpuid_version
|= value
& 0xf;
1176 static void x86_cpuid_get_level(Object
*obj
, Visitor
*v
, void *opaque
,
1177 const char *name
, Error
**errp
)
1179 X86CPU
*cpu
= X86_CPU(obj
);
1181 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1184 static void x86_cpuid_set_level(Object
*obj
, Visitor
*v
, void *opaque
,
1185 const char *name
, Error
**errp
)
1187 X86CPU
*cpu
= X86_CPU(obj
);
1189 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1192 static void x86_cpuid_get_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1193 const char *name
, Error
**errp
)
1195 X86CPU
*cpu
= X86_CPU(obj
);
1197 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1200 static void x86_cpuid_set_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1201 const char *name
, Error
**errp
)
1203 X86CPU
*cpu
= X86_CPU(obj
);
1205 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1208 static char *x86_cpuid_get_vendor(Object
*obj
, Error
**errp
)
1210 X86CPU
*cpu
= X86_CPU(obj
);
1211 CPUX86State
*env
= &cpu
->env
;
1215 value
= (char *)g_malloc(CPUID_VENDOR_SZ
+ 1);
1216 for (i
= 0; i
< 4; i
++) {
1217 value
[i
] = env
->cpuid_vendor1
>> (8 * i
);
1218 value
[i
+ 4] = env
->cpuid_vendor2
>> (8 * i
);
1219 value
[i
+ 8] = env
->cpuid_vendor3
>> (8 * i
);
1221 value
[CPUID_VENDOR_SZ
] = '\0';
1225 static void x86_cpuid_set_vendor(Object
*obj
, const char *value
,
1228 X86CPU
*cpu
= X86_CPU(obj
);
1229 CPUX86State
*env
= &cpu
->env
;
1232 if (strlen(value
) != CPUID_VENDOR_SZ
) {
1233 error_set(errp
, QERR_PROPERTY_VALUE_BAD
, "",
1238 env
->cpuid_vendor1
= 0;
1239 env
->cpuid_vendor2
= 0;
1240 env
->cpuid_vendor3
= 0;
1241 for (i
= 0; i
< 4; i
++) {
1242 env
->cpuid_vendor1
|= ((uint8_t)value
[i
]) << (8 * i
);
1243 env
->cpuid_vendor2
|= ((uint8_t)value
[i
+ 4]) << (8 * i
);
1244 env
->cpuid_vendor3
|= ((uint8_t)value
[i
+ 8]) << (8 * i
);
1246 env
->cpuid_vendor_override
= 1;
1249 static char *x86_cpuid_get_model_id(Object
*obj
, Error
**errp
)
1251 X86CPU
*cpu
= X86_CPU(obj
);
1252 CPUX86State
*env
= &cpu
->env
;
1256 value
= g_malloc(48 + 1);
1257 for (i
= 0; i
< 48; i
++) {
1258 value
[i
] = env
->cpuid_model
[i
>> 2] >> (8 * (i
& 3));
1264 static void x86_cpuid_set_model_id(Object
*obj
, const char *model_id
,
1267 X86CPU
*cpu
= X86_CPU(obj
);
1268 CPUX86State
*env
= &cpu
->env
;
1271 if (model_id
== NULL
) {
1274 len
= strlen(model_id
);
1275 memset(env
->cpuid_model
, 0, 48);
1276 for (i
= 0; i
< 48; i
++) {
1280 c
= (uint8_t)model_id
[i
];
1282 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
1286 static void x86_cpuid_get_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1287 const char *name
, Error
**errp
)
1289 X86CPU
*cpu
= X86_CPU(obj
);
1292 value
= cpu
->env
.tsc_khz
* 1000;
1293 visit_type_int(v
, &value
, name
, errp
);
1296 static void x86_cpuid_set_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1297 const char *name
, Error
**errp
)
1299 X86CPU
*cpu
= X86_CPU(obj
);
1300 const int64_t min
= 0;
1301 const int64_t max
= INT64_MAX
;
1304 visit_type_int(v
, &value
, name
, errp
);
1305 if (error_is_set(errp
)) {
1308 if (value
< min
|| value
> max
) {
1309 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1310 name
? name
: "null", value
, min
, max
);
1314 cpu
->env
.tsc_khz
= value
/ 1000;
1317 static int cpu_x86_find_by_name(x86_def_t
*x86_cpu_def
, const char *name
)
1321 for (def
= x86_defs
; def
; def
= def
->next
) {
1322 if (name
&& !strcmp(name
, def
->name
)) {
1326 if (kvm_enabled() && name
&& strcmp(name
, "host") == 0) {
1327 kvm_cpu_fill_host(x86_cpu_def
);
1331 memcpy(x86_cpu_def
, def
, sizeof(*def
));
1337 /* Parse "+feature,-feature,feature=foo" CPU feature string
1339 static int cpu_x86_parse_featurestr(x86_def_t
*x86_cpu_def
, char *features
)
1342 char *featurestr
; /* Single 'key=value" string being parsed */
1343 /* Features to be added */
1344 FeatureWordArray plus_features
= { 0 };
1345 /* Features to be removed */
1346 FeatureWordArray minus_features
= { 0 };
1349 featurestr
= features
? strtok(features
, ",") : NULL
;
1351 while (featurestr
) {
1353 if (featurestr
[0] == '+') {
1354 add_flagname_to_bitmaps(featurestr
+ 1, plus_features
);
1355 } else if (featurestr
[0] == '-') {
1356 add_flagname_to_bitmaps(featurestr
+ 1, minus_features
);
1357 } else if ((val
= strchr(featurestr
, '='))) {
1359 if (!strcmp(featurestr
, "family")) {
1361 numvalue
= strtoul(val
, &err
, 0);
1362 if (!*val
|| *err
|| numvalue
> 0xff + 0xf) {
1363 fprintf(stderr
, "bad numerical value %s\n", val
);
1366 x86_cpu_def
->family
= numvalue
;
1367 } else if (!strcmp(featurestr
, "model")) {
1369 numvalue
= strtoul(val
, &err
, 0);
1370 if (!*val
|| *err
|| numvalue
> 0xff) {
1371 fprintf(stderr
, "bad numerical value %s\n", val
);
1374 x86_cpu_def
->model
= numvalue
;
1375 } else if (!strcmp(featurestr
, "stepping")) {
1377 numvalue
= strtoul(val
, &err
, 0);
1378 if (!*val
|| *err
|| numvalue
> 0xf) {
1379 fprintf(stderr
, "bad numerical value %s\n", val
);
1382 x86_cpu_def
->stepping
= numvalue
;
1383 } else if (!strcmp(featurestr
, "level")) {
1385 numvalue
= strtoul(val
, &err
, 0);
1386 if (!*val
|| *err
) {
1387 fprintf(stderr
, "bad numerical value %s\n", val
);
1390 x86_cpu_def
->level
= numvalue
;
1391 } else if (!strcmp(featurestr
, "xlevel")) {
1393 numvalue
= strtoul(val
, &err
, 0);
1394 if (!*val
|| *err
) {
1395 fprintf(stderr
, "bad numerical value %s\n", val
);
1398 if (numvalue
< 0x80000000) {
1399 numvalue
+= 0x80000000;
1401 x86_cpu_def
->xlevel
= numvalue
;
1402 } else if (!strcmp(featurestr
, "vendor")) {
1403 if (strlen(val
) != 12) {
1404 fprintf(stderr
, "vendor string must be 12 chars long\n");
1407 x86_cpu_def
->vendor1
= 0;
1408 x86_cpu_def
->vendor2
= 0;
1409 x86_cpu_def
->vendor3
= 0;
1410 for(i
= 0; i
< 4; i
++) {
1411 x86_cpu_def
->vendor1
|= ((uint8_t)val
[i
]) << (8 * i
);
1412 x86_cpu_def
->vendor2
|= ((uint8_t)val
[i
+ 4]) << (8 * i
);
1413 x86_cpu_def
->vendor3
|= ((uint8_t)val
[i
+ 8]) << (8 * i
);
1415 x86_cpu_def
->vendor_override
= 1;
1416 } else if (!strcmp(featurestr
, "model_id")) {
1417 pstrcpy(x86_cpu_def
->model_id
, sizeof(x86_cpu_def
->model_id
),
1419 } else if (!strcmp(featurestr
, "tsc_freq")) {
1423 tsc_freq
= strtosz_suffix_unit(val
, &err
,
1424 STRTOSZ_DEFSUFFIX_B
, 1000);
1425 if (tsc_freq
< 0 || *err
) {
1426 fprintf(stderr
, "bad numerical value %s\n", val
);
1429 x86_cpu_def
->tsc_khz
= tsc_freq
/ 1000;
1430 } else if (!strcmp(featurestr
, "hv_spinlocks")) {
1432 numvalue
= strtoul(val
, &err
, 0);
1433 if (!*val
|| *err
) {
1434 fprintf(stderr
, "bad numerical value %s\n", val
);
1437 hyperv_set_spinlock_retries(numvalue
);
1439 fprintf(stderr
, "unrecognized feature %s\n", featurestr
);
1442 } else if (!strcmp(featurestr
, "check")) {
1444 } else if (!strcmp(featurestr
, "enforce")) {
1445 check_cpuid
= enforce_cpuid
= 1;
1446 } else if (!strcmp(featurestr
, "hv_relaxed")) {
1447 hyperv_enable_relaxed_timing(true);
1448 } else if (!strcmp(featurestr
, "hv_vapic")) {
1449 hyperv_enable_vapic_recommended(true);
1451 fprintf(stderr
, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr
);
1454 featurestr
= strtok(NULL
, ",");
1456 x86_cpu_def
->features
|= plus_features
[FEAT_1_EDX
];
1457 x86_cpu_def
->ext_features
|= plus_features
[FEAT_1_ECX
];
1458 x86_cpu_def
->ext2_features
|= plus_features
[FEAT_8000_0001_EDX
];
1459 x86_cpu_def
->ext3_features
|= plus_features
[FEAT_8000_0001_ECX
];
1460 x86_cpu_def
->ext4_features
|= plus_features
[FEAT_C000_0001_EDX
];
1461 x86_cpu_def
->kvm_features
|= plus_features
[FEAT_KVM
];
1462 x86_cpu_def
->svm_features
|= plus_features
[FEAT_SVM
];
1463 x86_cpu_def
->cpuid_7_0_ebx_features
|= plus_features
[FEAT_7_0_EBX
];
1464 x86_cpu_def
->features
&= ~minus_features
[FEAT_1_EDX
];
1465 x86_cpu_def
->ext_features
&= ~minus_features
[FEAT_1_ECX
];
1466 x86_cpu_def
->ext2_features
&= ~minus_features
[FEAT_8000_0001_EDX
];
1467 x86_cpu_def
->ext3_features
&= ~minus_features
[FEAT_8000_0001_ECX
];
1468 x86_cpu_def
->ext4_features
&= ~minus_features
[FEAT_C000_0001_EDX
];
1469 x86_cpu_def
->kvm_features
&= ~minus_features
[FEAT_KVM
];
1470 x86_cpu_def
->svm_features
&= ~minus_features
[FEAT_SVM
];
1471 x86_cpu_def
->cpuid_7_0_ebx_features
&= ~minus_features
[FEAT_7_0_EBX
];
1478 /* generate a composite string into buf of all cpuid names in featureset
1479 * selected by fbits. indicate truncation at bufsize in the event of overflow.
1480 * if flags, suppress names undefined in featureset.
1482 static void listflags(char *buf
, int bufsize
, uint32_t fbits
,
1483 const char **featureset
, uint32_t flags
)
1485 const char **p
= &featureset
[31];
1489 b
= 4 <= bufsize
? buf
+ (bufsize
-= 3) - 1 : NULL
;
1491 for (q
= buf
, bit
= 31; fbits
&& bufsize
; --p
, fbits
&= ~(1 << bit
), --bit
)
1492 if (fbits
& 1 << bit
&& (*p
|| !flags
)) {
1494 nc
= snprintf(q
, bufsize
, "%s%s", q
== buf
? "" : " ", *p
);
1496 nc
= snprintf(q
, bufsize
, "%s[%d]", q
== buf
? "" : " ", bit
);
1497 if (bufsize
<= nc
) {
1499 memcpy(b
, "...", sizeof("..."));
1508 /* generate CPU information. */
1509 void x86_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
1514 for (def
= x86_defs
; def
; def
= def
->next
) {
1515 snprintf(buf
, sizeof(buf
), "%s", def
->name
);
1516 (*cpu_fprintf
)(f
, "x86 %16s %-48s\n", buf
, def
->model_id
);
1518 if (kvm_enabled()) {
1519 (*cpu_fprintf
)(f
, "x86 %16s\n", "[host]");
1521 (*cpu_fprintf
)(f
, "\nRecognized CPUID flags:\n");
1522 listflags(buf
, sizeof(buf
), (uint32_t)~0, feature_name
, 1);
1523 (*cpu_fprintf
)(f
, " %s\n", buf
);
1524 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext_feature_name
, 1);
1525 (*cpu_fprintf
)(f
, " %s\n", buf
);
1526 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext2_feature_name
, 1);
1527 (*cpu_fprintf
)(f
, " %s\n", buf
);
1528 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext3_feature_name
, 1);
1529 (*cpu_fprintf
)(f
, " %s\n", buf
);
1532 CpuDefinitionInfoList
*arch_query_cpu_definitions(Error
**errp
)
1534 CpuDefinitionInfoList
*cpu_list
= NULL
;
1537 for (def
= x86_defs
; def
; def
= def
->next
) {
1538 CpuDefinitionInfoList
*entry
;
1539 CpuDefinitionInfo
*info
;
1541 info
= g_malloc0(sizeof(*info
));
1542 info
->name
= g_strdup(def
->name
);
1544 entry
= g_malloc0(sizeof(*entry
));
1545 entry
->value
= info
;
1546 entry
->next
= cpu_list
;
1554 static void filter_features_for_kvm(X86CPU
*cpu
)
1556 CPUX86State
*env
= &cpu
->env
;
1557 KVMState
*s
= kvm_state
;
1559 env
->cpuid_features
&=
1560 kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
1561 env
->cpuid_ext_features
&=
1562 kvm_arch_get_supported_cpuid(s
, 1, 0, R_ECX
);
1563 env
->cpuid_ext2_features
&=
1564 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
1565 env
->cpuid_ext3_features
&=
1566 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
1567 env
->cpuid_svm_features
&=
1568 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
1569 env
->cpuid_7_0_ebx_features
&=
1570 kvm_arch_get_supported_cpuid(s
, 7, 0, R_EBX
);
1571 env
->cpuid_kvm_features
&=
1572 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
1573 env
->cpuid_ext4_features
&=
1574 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
1579 int cpu_x86_register(X86CPU
*cpu
, const char *cpu_model
)
1581 CPUX86State
*env
= &cpu
->env
;
1582 x86_def_t def1
, *def
= &def1
;
1583 Error
*error
= NULL
;
1584 char *name
, *features
;
1585 gchar
**model_pieces
;
1587 memset(def
, 0, sizeof(*def
));
1589 model_pieces
= g_strsplit(cpu_model
, ",", 2);
1590 if (!model_pieces
[0]) {
1591 error_setg(&error
, "Invalid/empty CPU model name");
1594 name
= model_pieces
[0];
1595 features
= model_pieces
[1];
1597 if (cpu_x86_find_by_name(def
, name
) < 0) {
1598 error_setg(&error
, "Unable to find CPU definition: %s", name
);
1602 if (kvm_enabled()) {
1603 def
->kvm_features
|= kvm_default_features
;
1605 def
->ext_features
|= CPUID_EXT_HYPERVISOR
;
1607 if (cpu_x86_parse_featurestr(def
, features
) < 0) {
1608 error_setg(&error
, "Invalid cpu_model string format: %s", cpu_model
);
1611 assert(def
->vendor1
);
1612 env
->cpuid_vendor1
= def
->vendor1
;
1613 env
->cpuid_vendor2
= def
->vendor2
;
1614 env
->cpuid_vendor3
= def
->vendor3
;
1615 env
->cpuid_vendor_override
= def
->vendor_override
;
1616 object_property_set_int(OBJECT(cpu
), def
->level
, "level", &error
);
1617 object_property_set_int(OBJECT(cpu
), def
->family
, "family", &error
);
1618 object_property_set_int(OBJECT(cpu
), def
->model
, "model", &error
);
1619 object_property_set_int(OBJECT(cpu
), def
->stepping
, "stepping", &error
);
1620 env
->cpuid_features
= def
->features
;
1621 env
->cpuid_ext_features
= def
->ext_features
;
1622 env
->cpuid_ext2_features
= def
->ext2_features
;
1623 env
->cpuid_ext3_features
= def
->ext3_features
;
1624 object_property_set_int(OBJECT(cpu
), def
->xlevel
, "xlevel", &error
);
1625 env
->cpuid_kvm_features
= def
->kvm_features
;
1626 env
->cpuid_svm_features
= def
->svm_features
;
1627 env
->cpuid_ext4_features
= def
->ext4_features
;
1628 env
->cpuid_7_0_ebx_features
= def
->cpuid_7_0_ebx_features
;
1629 env
->cpuid_xlevel2
= def
->xlevel2
;
1630 object_property_set_int(OBJECT(cpu
), (int64_t)def
->tsc_khz
* 1000,
1631 "tsc-frequency", &error
);
1633 object_property_set_str(OBJECT(cpu
), def
->model_id
, "model-id", &error
);
1636 g_strfreev(model_pieces
);
1638 fprintf(stderr
, "%s\n", error_get_pretty(error
));
1645 #if !defined(CONFIG_USER_ONLY)
1647 void cpu_clear_apic_feature(CPUX86State
*env
)
1649 env
->cpuid_features
&= ~CPUID_APIC
;
1652 #endif /* !CONFIG_USER_ONLY */
1654 /* Initialize list of CPU models, filling some non-static fields if necessary
1656 void x86_cpudef_setup(void)
1659 static const char *model_with_versions
[] = { "qemu32", "qemu64", "athlon" };
1661 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); ++i
) {
1662 x86_def_t
*def
= &builtin_x86_defs
[i
];
1663 def
->next
= x86_defs
;
1665 /* Look for specific "cpudef" models that */
1666 /* have the QEMU version in .model_id */
1667 for (j
= 0; j
< ARRAY_SIZE(model_with_versions
); j
++) {
1668 if (strcmp(model_with_versions
[j
], def
->name
) == 0) {
1669 pstrcpy(def
->model_id
, sizeof(def
->model_id
),
1670 "QEMU Virtual CPU version ");
1671 pstrcat(def
->model_id
, sizeof(def
->model_id
),
1672 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
)