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"
27 #include "qemu/option.h"
28 #include "qemu/config-file.h"
29 #include "qapi/qmp/qerror.h"
31 #include "qapi/visitor.h"
32 #include "sysemu/arch_init.h"
37 #if defined(CONFIG_KVM)
38 #include <linux/kvm_para.h>
41 #include "sysemu/sysemu.h"
42 #ifndef CONFIG_USER_ONLY
44 #include "hw/sysbus.h"
45 #include "hw/apic_internal.h"
48 /* feature flags taken from "Intel Processor Identification and the CPUID
49 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
50 * between feature naming conventions, aliases may be added.
52 static const char *feature_name
[] = {
53 "fpu", "vme", "de", "pse",
54 "tsc", "msr", "pae", "mce",
55 "cx8", "apic", NULL
, "sep",
56 "mtrr", "pge", "mca", "cmov",
57 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
58 NULL
, "ds" /* Intel dts */, "acpi", "mmx",
59 "fxsr", "sse", "sse2", "ss",
60 "ht" /* Intel htt */, "tm", "ia64", "pbe",
62 static const char *ext_feature_name
[] = {
63 "pni|sse3" /* Intel,AMD sse3 */, "pclmulqdq|pclmuldq", "dtes64", "monitor",
64 "ds_cpl", "vmx", "smx", "est",
65 "tm2", "ssse3", "cid", NULL
,
66 "fma", "cx16", "xtpr", "pdcm",
67 NULL
, "pcid", "dca", "sse4.1|sse4_1",
68 "sse4.2|sse4_2", "x2apic", "movbe", "popcnt",
69 "tsc-deadline", "aes", "xsave", "osxsave",
70 "avx", "f16c", "rdrand", "hypervisor",
72 /* Feature names that are already defined on feature_name[] but are set on
73 * CPUID[8000_0001].EDX on AMD CPUs don't have their names on
74 * ext2_feature_name[]. They are copied automatically to cpuid_ext2_features
75 * if and only if CPU vendor is AMD.
77 static const char *ext2_feature_name
[] = {
78 NULL
/* fpu */, NULL
/* vme */, NULL
/* de */, NULL
/* pse */,
79 NULL
/* tsc */, NULL
/* msr */, NULL
/* pae */, NULL
/* mce */,
80 NULL
/* cx8 */ /* AMD CMPXCHG8B */, NULL
/* apic */, NULL
, "syscall",
81 NULL
/* mtrr */, NULL
/* pge */, NULL
/* mca */, NULL
/* cmov */,
82 NULL
/* pat */, NULL
/* pse36 */, NULL
, NULL
/* Linux mp */,
83 "nx|xd", NULL
, "mmxext", NULL
/* mmx */,
84 NULL
/* fxsr */, "fxsr_opt|ffxsr", "pdpe1gb" /* AMD Page1GB */, "rdtscp",
85 NULL
, "lm|i64", "3dnowext", "3dnow",
87 static const char *ext3_feature_name
[] = {
88 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
89 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
90 "3dnowprefetch", "osvw", "ibs", "xop",
91 "skinit", "wdt", NULL
, "lwp",
92 "fma4", "tce", NULL
, "nodeid_msr",
93 NULL
, "tbm", "topoext", "perfctr_core",
94 "perfctr_nb", NULL
, NULL
, NULL
,
95 NULL
, NULL
, NULL
, NULL
,
98 static const char *ext4_feature_name
[] = {
99 NULL
, NULL
, "xstore", "xstore-en",
100 NULL
, NULL
, "xcrypt", "xcrypt-en",
101 "ace2", "ace2-en", "phe", "phe-en",
102 "pmm", "pmm-en", NULL
, NULL
,
103 NULL
, NULL
, NULL
, NULL
,
104 NULL
, NULL
, NULL
, NULL
,
105 NULL
, NULL
, NULL
, NULL
,
106 NULL
, NULL
, NULL
, NULL
,
109 static const char *kvm_feature_name
[] = {
110 "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock",
111 "kvm_asyncpf", "kvm_steal_time", "kvm_pv_eoi", NULL
,
112 NULL
, NULL
, NULL
, NULL
,
113 NULL
, NULL
, NULL
, NULL
,
114 NULL
, NULL
, NULL
, NULL
,
115 NULL
, NULL
, NULL
, NULL
,
116 NULL
, NULL
, NULL
, NULL
,
117 NULL
, NULL
, NULL
, NULL
,
120 static const char *svm_feature_name
[] = {
121 "npt", "lbrv", "svm_lock", "nrip_save",
122 "tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists",
123 NULL
, NULL
, "pause_filter", NULL
,
124 "pfthreshold", NULL
, NULL
, NULL
,
125 NULL
, NULL
, NULL
, NULL
,
126 NULL
, NULL
, NULL
, NULL
,
127 NULL
, NULL
, NULL
, NULL
,
128 NULL
, NULL
, NULL
, NULL
,
131 static const char *cpuid_7_0_ebx_feature_name
[] = {
132 "fsgsbase", NULL
, NULL
, "bmi1", "hle", "avx2", NULL
, "smep",
133 "bmi2", "erms", "invpcid", "rtm", NULL
, NULL
, NULL
, NULL
,
134 NULL
, NULL
, "rdseed", "adx", "smap", NULL
, NULL
, NULL
,
135 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
138 typedef struct FeatureWordInfo
{
139 const char **feat_names
;
140 uint32_t cpuid_eax
; /* Input EAX for CPUID */
141 int cpuid_reg
; /* R_* register constant */
144 static FeatureWordInfo feature_word_info
[FEATURE_WORDS
] = {
146 .feat_names
= feature_name
,
147 .cpuid_eax
= 1, .cpuid_reg
= R_EDX
,
150 .feat_names
= ext_feature_name
,
151 .cpuid_eax
= 1, .cpuid_reg
= R_ECX
,
153 [FEAT_8000_0001_EDX
] = {
154 .feat_names
= ext2_feature_name
,
155 .cpuid_eax
= 0x80000001, .cpuid_reg
= R_EDX
,
157 [FEAT_8000_0001_ECX
] = {
158 .feat_names
= ext3_feature_name
,
159 .cpuid_eax
= 0x80000001, .cpuid_reg
= R_ECX
,
161 [FEAT_C000_0001_EDX
] = {
162 .feat_names
= ext4_feature_name
,
163 .cpuid_eax
= 0xC0000001, .cpuid_reg
= R_EDX
,
166 .feat_names
= kvm_feature_name
,
167 .cpuid_eax
= KVM_CPUID_FEATURES
, .cpuid_reg
= R_EAX
,
170 .feat_names
= svm_feature_name
,
171 .cpuid_eax
= 0x8000000A, .cpuid_reg
= R_EDX
,
174 .feat_names
= cpuid_7_0_ebx_feature_name
,
175 .cpuid_eax
= 7, .cpuid_reg
= R_EBX
,
179 const char *get_register_name_32(unsigned int reg
)
181 static const char *reg_names
[CPU_NB_REGS32
] = {
192 if (reg
> CPU_NB_REGS32
) {
195 return reg_names
[reg
];
198 /* collects per-function cpuid data
200 typedef struct model_features_t
{
201 uint32_t *guest_feat
;
203 FeatureWord feat_word
;
207 int enforce_cpuid
= 0;
209 static uint32_t kvm_default_features
= (1 << KVM_FEATURE_CLOCKSOURCE
) |
210 (1 << KVM_FEATURE_NOP_IO_DELAY
) |
211 (1 << KVM_FEATURE_CLOCKSOURCE2
) |
212 (1 << KVM_FEATURE_ASYNC_PF
) |
213 (1 << KVM_FEATURE_STEAL_TIME
) |
214 (1 << KVM_FEATURE_PV_EOI
) |
215 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT
);
217 void disable_kvm_pv_eoi(void)
219 kvm_default_features
&= ~(1UL << KVM_FEATURE_PV_EOI
);
222 void host_cpuid(uint32_t function
, uint32_t count
,
223 uint32_t *eax
, uint32_t *ebx
, uint32_t *ecx
, uint32_t *edx
)
225 #if defined(CONFIG_KVM)
230 : "=a"(vec
[0]), "=b"(vec
[1]),
231 "=c"(vec
[2]), "=d"(vec
[3])
232 : "0"(function
), "c"(count
) : "cc");
234 asm volatile("pusha \n\t"
236 "mov %%eax, 0(%2) \n\t"
237 "mov %%ebx, 4(%2) \n\t"
238 "mov %%ecx, 8(%2) \n\t"
239 "mov %%edx, 12(%2) \n\t"
241 : : "a"(function
), "c"(count
), "S"(vec
)
256 #define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
258 /* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
259 * a substring. ex if !NULL points to the first char after a substring,
260 * otherwise the string is assumed to sized by a terminating nul.
261 * Return lexical ordering of *s1:*s2.
263 static int sstrcmp(const char *s1
, const char *e1
, const char *s2
,
267 if (!*s1
|| !*s2
|| *s1
!= *s2
)
270 if (s1
== e1
&& s2
== e2
)
279 /* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
280 * '|' delimited (possibly empty) strings in which case search for a match
281 * within the alternatives proceeds left to right. Return 0 for success,
282 * non-zero otherwise.
284 static int altcmp(const char *s
, const char *e
, const char *altstr
)
288 for (q
= p
= altstr
; ; ) {
289 while (*p
&& *p
!= '|')
291 if ((q
== p
&& !*s
) || (q
!= p
&& !sstrcmp(s
, e
, q
, p
)))
300 /* search featureset for flag *[s..e), if found set corresponding bit in
301 * *pval and return true, otherwise return false
303 static bool lookup_feature(uint32_t *pval
, const char *s
, const char *e
,
304 const char **featureset
)
310 for (mask
= 1, ppc
= featureset
; mask
; mask
<<= 1, ++ppc
) {
311 if (*ppc
&& !altcmp(s
, e
, *ppc
)) {
319 static void add_flagname_to_bitmaps(const char *flagname
,
320 FeatureWordArray words
)
323 for (w
= 0; w
< FEATURE_WORDS
; w
++) {
324 FeatureWordInfo
*wi
= &feature_word_info
[w
];
325 if (wi
->feat_names
&&
326 lookup_feature(&words
[w
], flagname
, NULL
, wi
->feat_names
)) {
330 if (w
== FEATURE_WORDS
) {
331 fprintf(stderr
, "CPU feature %s not found\n", flagname
);
335 typedef struct x86_def_t
{
336 struct x86_def_t
*next
;
339 uint32_t vendor1
, vendor2
, vendor3
;
344 uint32_t features
, ext_features
, ext2_features
, ext3_features
;
345 uint32_t kvm_features
, svm_features
;
349 /* Store the results of Centaur's CPUID instructions */
350 uint32_t ext4_features
;
352 /* The feature bits on CPUID[EAX=7,ECX=0].EBX */
353 uint32_t cpuid_7_0_ebx_features
;
356 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
357 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
358 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
359 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
360 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
361 CPUID_PSE36 | CPUID_FXSR)
362 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
363 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
364 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
365 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
366 CPUID_PAE | CPUID_SEP | CPUID_APIC)
368 #define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
369 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
370 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
371 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
372 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
373 /* partly implemented:
374 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
375 CPUID_PSE36 (needed for Solaris) */
377 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
378 #define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \
379 CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT | \
380 CPUID_EXT_HYPERVISOR)
382 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
383 CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_XSAVE */
384 #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
385 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
386 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
388 CPUID_EXT2_PDPE1GB */
389 #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
390 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
391 #define TCG_SVM_FEATURES 0
392 #define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP)
394 /* maintains list of cpu model definitions
396 static x86_def_t
*x86_defs
= {NULL
};
398 /* built-in cpu model definitions (deprecated)
400 static x86_def_t builtin_x86_defs
[] = {
404 .vendor1
= CPUID_VENDOR_AMD_1
,
405 .vendor2
= CPUID_VENDOR_AMD_2
,
406 .vendor3
= CPUID_VENDOR_AMD_3
,
410 .features
= PPRO_FEATURES
|
411 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
413 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
| CPUID_EXT_POPCNT
,
414 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
415 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
416 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
417 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
418 .xlevel
= 0x8000000A,
423 .vendor1
= CPUID_VENDOR_AMD_1
,
424 .vendor2
= CPUID_VENDOR_AMD_2
,
425 .vendor3
= CPUID_VENDOR_AMD_3
,
429 .features
= PPRO_FEATURES
|
430 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
431 CPUID_PSE36
| CPUID_VME
| CPUID_HT
,
432 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_CX16
|
434 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
435 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
436 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
| CPUID_EXT2_MMXEXT
|
437 CPUID_EXT2_FFXSR
| CPUID_EXT2_PDPE1GB
| CPUID_EXT2_RDTSCP
,
438 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
440 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
441 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
442 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
443 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
444 .svm_features
= CPUID_SVM_NPT
| CPUID_SVM_LBRV
,
445 .xlevel
= 0x8000001A,
446 .model_id
= "AMD Phenom(tm) 9550 Quad-Core Processor"
451 .vendor1
= CPUID_VENDOR_INTEL_1
,
452 .vendor2
= CPUID_VENDOR_INTEL_2
,
453 .vendor3
= CPUID_VENDOR_INTEL_3
,
457 .features
= PPRO_FEATURES
|
458 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
459 CPUID_PSE36
| CPUID_VME
| CPUID_DTS
| CPUID_ACPI
| CPUID_SS
|
460 CPUID_HT
| CPUID_TM
| CPUID_PBE
,
461 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
462 CPUID_EXT_DTES64
| CPUID_EXT_DSCPL
| CPUID_EXT_VMX
| CPUID_EXT_EST
|
463 CPUID_EXT_TM2
| CPUID_EXT_CX16
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
464 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
465 .ext3_features
= CPUID_EXT3_LAHF_LM
,
466 .xlevel
= 0x80000008,
467 .model_id
= "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
472 .vendor1
= CPUID_VENDOR_INTEL_1
,
473 .vendor2
= CPUID_VENDOR_INTEL_2
,
474 .vendor3
= CPUID_VENDOR_INTEL_3
,
478 /* Missing: CPUID_VME, CPUID_HT */
479 .features
= PPRO_FEATURES
|
480 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
482 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
483 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
,
484 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
485 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
486 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
487 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
488 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
489 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
490 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
492 .xlevel
= 0x80000008,
493 .model_id
= "Common KVM processor"
498 .vendor1
= CPUID_VENDOR_INTEL_1
,
499 .vendor2
= CPUID_VENDOR_INTEL_2
,
500 .vendor3
= CPUID_VENDOR_INTEL_3
,
504 .features
= PPRO_FEATURES
,
505 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_POPCNT
,
506 .xlevel
= 0x80000004,
511 .vendor1
= CPUID_VENDOR_INTEL_1
,
512 .vendor2
= CPUID_VENDOR_INTEL_2
,
513 .vendor3
= CPUID_VENDOR_INTEL_3
,
517 .features
= PPRO_FEATURES
|
518 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_PSE36
,
519 .ext_features
= CPUID_EXT_SSE3
,
520 .ext2_features
= PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
,
522 .xlevel
= 0x80000008,
523 .model_id
= "Common 32-bit KVM processor"
528 .vendor1
= CPUID_VENDOR_INTEL_1
,
529 .vendor2
= CPUID_VENDOR_INTEL_2
,
530 .vendor3
= CPUID_VENDOR_INTEL_3
,
534 .features
= PPRO_FEATURES
| CPUID_VME
|
535 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_DTS
| CPUID_ACPI
|
536 CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
537 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_VMX
|
538 CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
539 .ext2_features
= CPUID_EXT2_NX
,
540 .xlevel
= 0x80000008,
541 .model_id
= "Genuine Intel(R) CPU T2600 @ 2.16GHz",
546 .vendor1
= CPUID_VENDOR_INTEL_1
,
547 .vendor2
= CPUID_VENDOR_INTEL_2
,
548 .vendor3
= CPUID_VENDOR_INTEL_3
,
552 .features
= I486_FEATURES
,
558 .vendor1
= CPUID_VENDOR_INTEL_1
,
559 .vendor2
= CPUID_VENDOR_INTEL_2
,
560 .vendor3
= CPUID_VENDOR_INTEL_3
,
564 .features
= PENTIUM_FEATURES
,
570 .vendor1
= CPUID_VENDOR_INTEL_1
,
571 .vendor2
= CPUID_VENDOR_INTEL_2
,
572 .vendor3
= CPUID_VENDOR_INTEL_3
,
576 .features
= PENTIUM2_FEATURES
,
582 .vendor1
= CPUID_VENDOR_INTEL_1
,
583 .vendor2
= CPUID_VENDOR_INTEL_2
,
584 .vendor3
= CPUID_VENDOR_INTEL_3
,
588 .features
= PENTIUM3_FEATURES
,
594 .vendor1
= CPUID_VENDOR_AMD_1
,
595 .vendor2
= CPUID_VENDOR_AMD_2
,
596 .vendor3
= CPUID_VENDOR_AMD_3
,
600 .features
= PPRO_FEATURES
| CPUID_PSE36
| CPUID_VME
| CPUID_MTRR
|
602 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
603 CPUID_EXT2_MMXEXT
| CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
604 .xlevel
= 0x80000008,
608 /* original is on level 10 */
610 .vendor1
= CPUID_VENDOR_INTEL_1
,
611 .vendor2
= CPUID_VENDOR_INTEL_2
,
612 .vendor3
= CPUID_VENDOR_INTEL_3
,
616 .features
= PPRO_FEATURES
|
617 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_VME
| CPUID_DTS
|
618 CPUID_ACPI
| CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
619 /* Some CPUs got no CPUID_SEP */
620 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
621 CPUID_EXT_DSCPL
| CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
,
622 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
624 .ext3_features
= CPUID_EXT3_LAHF_LM
,
625 .xlevel
= 0x8000000A,
626 .model_id
= "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
631 .vendor1
= CPUID_VENDOR_INTEL_1
,
632 .vendor2
= CPUID_VENDOR_INTEL_2
,
633 .vendor3
= CPUID_VENDOR_INTEL_3
,
637 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
638 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
639 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
640 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
641 CPUID_DE
| CPUID_FP87
,
642 .ext_features
= CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
643 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
644 .ext3_features
= CPUID_EXT3_LAHF_LM
,
645 .xlevel
= 0x8000000A,
646 .model_id
= "Intel Celeron_4x0 (Conroe/Merom Class Core 2)",
651 .vendor1
= CPUID_VENDOR_INTEL_1
,
652 .vendor2
= CPUID_VENDOR_INTEL_2
,
653 .vendor3
= CPUID_VENDOR_INTEL_3
,
657 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
658 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
659 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
660 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
661 CPUID_DE
| CPUID_FP87
,
662 .ext_features
= CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
664 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
665 .ext3_features
= CPUID_EXT3_LAHF_LM
,
666 .xlevel
= 0x8000000A,
667 .model_id
= "Intel Core 2 Duo P9xxx (Penryn Class Core 2)",
672 .vendor1
= CPUID_VENDOR_INTEL_1
,
673 .vendor2
= CPUID_VENDOR_INTEL_2
,
674 .vendor3
= CPUID_VENDOR_INTEL_3
,
678 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
679 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
680 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
681 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
682 CPUID_DE
| CPUID_FP87
,
683 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
684 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
685 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
686 .ext3_features
= CPUID_EXT3_LAHF_LM
,
687 .xlevel
= 0x8000000A,
688 .model_id
= "Intel Core i7 9xx (Nehalem Class Core i7)",
693 .vendor1
= CPUID_VENDOR_INTEL_1
,
694 .vendor2
= CPUID_VENDOR_INTEL_2
,
695 .vendor3
= CPUID_VENDOR_INTEL_3
,
699 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
700 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
701 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
702 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
703 CPUID_DE
| CPUID_FP87
,
704 .ext_features
= CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
705 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
707 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
708 .ext3_features
= CPUID_EXT3_LAHF_LM
,
709 .xlevel
= 0x8000000A,
710 .model_id
= "Westmere E56xx/L56xx/X56xx (Nehalem-C)",
713 .name
= "SandyBridge",
715 .vendor1
= CPUID_VENDOR_INTEL_1
,
716 .vendor2
= CPUID_VENDOR_INTEL_2
,
717 .vendor3
= CPUID_VENDOR_INTEL_3
,
721 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
722 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
723 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
724 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
725 CPUID_DE
| CPUID_FP87
,
726 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
727 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_POPCNT
|
728 CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
729 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
731 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
733 .ext3_features
= CPUID_EXT3_LAHF_LM
,
734 .xlevel
= 0x8000000A,
735 .model_id
= "Intel Xeon E312xx (Sandy Bridge)",
740 .vendor1
= CPUID_VENDOR_INTEL_1
,
741 .vendor2
= CPUID_VENDOR_INTEL_2
,
742 .vendor3
= CPUID_VENDOR_INTEL_3
,
746 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
747 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
748 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
749 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
750 CPUID_DE
| CPUID_FP87
,
751 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
752 CPUID_EXT_POPCNT
| CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
|
753 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
754 CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
|
755 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_FMA
| CPUID_EXT_MOVBE
|
757 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
759 .ext3_features
= CPUID_EXT3_LAHF_LM
,
760 .cpuid_7_0_ebx_features
= CPUID_7_0_EBX_FSGSBASE
| CPUID_7_0_EBX_BMI1
|
761 CPUID_7_0_EBX_HLE
| CPUID_7_0_EBX_AVX2
| CPUID_7_0_EBX_SMEP
|
762 CPUID_7_0_EBX_BMI2
| CPUID_7_0_EBX_ERMS
| CPUID_7_0_EBX_INVPCID
|
764 .xlevel
= 0x8000000A,
765 .model_id
= "Intel Core Processor (Haswell)",
768 .name
= "Opteron_G1",
770 .vendor1
= CPUID_VENDOR_AMD_1
,
771 .vendor2
= CPUID_VENDOR_AMD_2
,
772 .vendor3
= CPUID_VENDOR_AMD_3
,
776 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
777 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
778 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
779 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
780 CPUID_DE
| CPUID_FP87
,
781 .ext_features
= CPUID_EXT_SSE3
,
782 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
783 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
784 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
785 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
786 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
787 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
788 .xlevel
= 0x80000008,
789 .model_id
= "AMD Opteron 240 (Gen 1 Class Opteron)",
792 .name
= "Opteron_G2",
794 .vendor1
= CPUID_VENDOR_AMD_1
,
795 .vendor2
= CPUID_VENDOR_AMD_2
,
796 .vendor3
= CPUID_VENDOR_AMD_3
,
800 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
801 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
802 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
803 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
804 CPUID_DE
| CPUID_FP87
,
805 .ext_features
= CPUID_EXT_CX16
| CPUID_EXT_SSE3
,
806 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
807 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
808 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
809 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
810 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
811 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
812 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
813 .ext3_features
= CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
814 .xlevel
= 0x80000008,
815 .model_id
= "AMD Opteron 22xx (Gen 2 Class Opteron)",
818 .name
= "Opteron_G3",
820 .vendor1
= CPUID_VENDOR_AMD_1
,
821 .vendor2
= CPUID_VENDOR_AMD_2
,
822 .vendor3
= CPUID_VENDOR_AMD_3
,
826 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
827 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
828 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
829 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
830 CPUID_DE
| CPUID_FP87
,
831 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_CX16
| CPUID_EXT_MONITOR
|
833 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
834 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
835 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
836 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
837 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
838 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
839 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
840 .ext3_features
= CPUID_EXT3_MISALIGNSSE
| CPUID_EXT3_SSE4A
|
841 CPUID_EXT3_ABM
| CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
842 .xlevel
= 0x80000008,
843 .model_id
= "AMD Opteron 23xx (Gen 3 Class Opteron)",
846 .name
= "Opteron_G4",
848 .vendor1
= CPUID_VENDOR_AMD_1
,
849 .vendor2
= CPUID_VENDOR_AMD_2
,
850 .vendor3
= CPUID_VENDOR_AMD_3
,
854 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
855 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
856 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
857 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
858 CPUID_DE
| CPUID_FP87
,
859 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
860 CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
861 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
863 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
864 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
865 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
866 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
867 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
868 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
869 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
870 .ext3_features
= CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
871 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
872 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
874 .xlevel
= 0x8000001A,
875 .model_id
= "AMD Opteron 62xx class CPU",
878 .name
= "Opteron_G5",
880 .vendor1
= CPUID_VENDOR_AMD_1
,
881 .vendor2
= CPUID_VENDOR_AMD_2
,
882 .vendor3
= CPUID_VENDOR_AMD_3
,
886 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
887 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
888 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
889 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
890 CPUID_DE
| CPUID_FP87
,
891 .ext_features
= CPUID_EXT_F16C
| CPUID_EXT_AVX
| CPUID_EXT_XSAVE
|
892 CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
893 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_FMA
|
894 CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
,
895 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
896 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
897 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
898 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
899 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
900 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
901 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
902 .ext3_features
= CPUID_EXT3_TBM
| CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
903 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
904 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
906 .xlevel
= 0x8000001A,
907 .model_id
= "AMD Opteron 63xx class CPU",
912 static int cpu_x86_fill_model_id(char *str
)
914 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
917 for (i
= 0; i
< 3; i
++) {
918 host_cpuid(0x80000002 + i
, 0, &eax
, &ebx
, &ecx
, &edx
);
919 memcpy(str
+ i
* 16 + 0, &eax
, 4);
920 memcpy(str
+ i
* 16 + 4, &ebx
, 4);
921 memcpy(str
+ i
* 16 + 8, &ecx
, 4);
922 memcpy(str
+ i
* 16 + 12, &edx
, 4);
928 /* Fill a x86_def_t struct with information about the host CPU, and
929 * the CPU features supported by the host hardware + host kernel
931 * This function may be called only if KVM is enabled.
933 static void kvm_cpu_fill_host(x86_def_t
*x86_cpu_def
)
936 KVMState
*s
= kvm_state
;
937 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
939 assert(kvm_enabled());
941 x86_cpu_def
->name
= "host";
942 host_cpuid(0x0, 0, &eax
, &ebx
, &ecx
, &edx
);
943 x86_cpu_def
->vendor1
= ebx
;
944 x86_cpu_def
->vendor2
= edx
;
945 x86_cpu_def
->vendor3
= ecx
;
947 host_cpuid(0x1, 0, &eax
, &ebx
, &ecx
, &edx
);
948 x86_cpu_def
->family
= ((eax
>> 8) & 0x0F) + ((eax
>> 20) & 0xFF);
949 x86_cpu_def
->model
= ((eax
>> 4) & 0x0F) | ((eax
& 0xF0000) >> 12);
950 x86_cpu_def
->stepping
= eax
& 0x0F;
952 x86_cpu_def
->level
= kvm_arch_get_supported_cpuid(s
, 0x0, 0, R_EAX
);
953 x86_cpu_def
->features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_EDX
);
954 x86_cpu_def
->ext_features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_ECX
);
956 if (x86_cpu_def
->level
>= 7) {
957 x86_cpu_def
->cpuid_7_0_ebx_features
=
958 kvm_arch_get_supported_cpuid(s
, 0x7, 0, R_EBX
);
960 x86_cpu_def
->cpuid_7_0_ebx_features
= 0;
963 x86_cpu_def
->xlevel
= kvm_arch_get_supported_cpuid(s
, 0x80000000, 0, R_EAX
);
964 x86_cpu_def
->ext2_features
=
965 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
966 x86_cpu_def
->ext3_features
=
967 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
969 cpu_x86_fill_model_id(x86_cpu_def
->model_id
);
970 x86_cpu_def
->vendor_override
= 0;
972 /* Call Centaur's CPUID instruction. */
973 if (x86_cpu_def
->vendor1
== CPUID_VENDOR_VIA_1
&&
974 x86_cpu_def
->vendor2
== CPUID_VENDOR_VIA_2
&&
975 x86_cpu_def
->vendor3
== CPUID_VENDOR_VIA_3
) {
976 host_cpuid(0xC0000000, 0, &eax
, &ebx
, &ecx
, &edx
);
977 eax
= kvm_arch_get_supported_cpuid(s
, 0xC0000000, 0, R_EAX
);
978 if (eax
>= 0xC0000001) {
979 /* Support VIA max extended level */
980 x86_cpu_def
->xlevel2
= eax
;
981 host_cpuid(0xC0000001, 0, &eax
, &ebx
, &ecx
, &edx
);
982 x86_cpu_def
->ext4_features
=
983 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
987 /* Other KVM-specific feature fields: */
988 x86_cpu_def
->svm_features
=
989 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
990 x86_cpu_def
->kvm_features
=
991 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
993 #endif /* CONFIG_KVM */
996 static int unavailable_host_feature(FeatureWordInfo
*f
, uint32_t mask
)
1000 for (i
= 0; i
< 32; ++i
)
1001 if (1 << i
& mask
) {
1002 const char *reg
= get_register_name_32(f
->cpuid_reg
);
1004 fprintf(stderr
, "warning: host doesn't support requested feature: "
1005 "CPUID.%02XH:%s%s%s [bit %d]\n",
1007 f
->feat_names
[i
] ? "." : "",
1008 f
->feat_names
[i
] ? f
->feat_names
[i
] : "", i
);
1014 /* Check if all requested cpu flags are making their way to the guest
1016 * Returns 0 if all flags are supported by the host, non-zero otherwise.
1018 * This function may be called only if KVM is enabled.
1020 static int kvm_check_features_against_host(X86CPU
*cpu
)
1022 CPUX86State
*env
= &cpu
->env
;
1026 struct model_features_t ft
[] = {
1027 {&env
->cpuid_features
, &host_def
.features
,
1029 {&env
->cpuid_ext_features
, &host_def
.ext_features
,
1031 {&env
->cpuid_ext2_features
, &host_def
.ext2_features
,
1032 FEAT_8000_0001_EDX
},
1033 {&env
->cpuid_ext3_features
, &host_def
.ext3_features
,
1034 FEAT_8000_0001_ECX
},
1035 {&env
->cpuid_ext4_features
, &host_def
.ext4_features
,
1036 FEAT_C000_0001_EDX
},
1037 {&env
->cpuid_7_0_ebx_features
, &host_def
.cpuid_7_0_ebx_features
,
1039 {&env
->cpuid_svm_features
, &host_def
.svm_features
,
1041 {&env
->cpuid_kvm_features
, &host_def
.kvm_features
,
1045 assert(kvm_enabled());
1047 kvm_cpu_fill_host(&host_def
);
1048 for (rv
= 0, i
= 0; i
< ARRAY_SIZE(ft
); ++i
) {
1049 FeatureWord w
= ft
[i
].feat_word
;
1050 FeatureWordInfo
*wi
= &feature_word_info
[w
];
1051 for (mask
= 1; mask
; mask
<<= 1) {
1052 if (*ft
[i
].guest_feat
& mask
&&
1053 !(*ft
[i
].host_feat
& mask
)) {
1054 unavailable_host_feature(wi
, mask
);
1062 static void x86_cpuid_version_get_family(Object
*obj
, Visitor
*v
, void *opaque
,
1063 const char *name
, Error
**errp
)
1065 X86CPU
*cpu
= X86_CPU(obj
);
1066 CPUX86State
*env
= &cpu
->env
;
1069 value
= (env
->cpuid_version
>> 8) & 0xf;
1071 value
+= (env
->cpuid_version
>> 20) & 0xff;
1073 visit_type_int(v
, &value
, name
, errp
);
1076 static void x86_cpuid_version_set_family(Object
*obj
, Visitor
*v
, void *opaque
,
1077 const char *name
, Error
**errp
)
1079 X86CPU
*cpu
= X86_CPU(obj
);
1080 CPUX86State
*env
= &cpu
->env
;
1081 const int64_t min
= 0;
1082 const int64_t max
= 0xff + 0xf;
1085 visit_type_int(v
, &value
, name
, errp
);
1086 if (error_is_set(errp
)) {
1089 if (value
< min
|| value
> max
) {
1090 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1091 name
? name
: "null", value
, min
, max
);
1095 env
->cpuid_version
&= ~0xff00f00;
1097 env
->cpuid_version
|= 0xf00 | ((value
- 0x0f) << 20);
1099 env
->cpuid_version
|= value
<< 8;
1103 static void x86_cpuid_version_get_model(Object
*obj
, Visitor
*v
, void *opaque
,
1104 const char *name
, Error
**errp
)
1106 X86CPU
*cpu
= X86_CPU(obj
);
1107 CPUX86State
*env
= &cpu
->env
;
1110 value
= (env
->cpuid_version
>> 4) & 0xf;
1111 value
|= ((env
->cpuid_version
>> 16) & 0xf) << 4;
1112 visit_type_int(v
, &value
, name
, errp
);
1115 static void x86_cpuid_version_set_model(Object
*obj
, Visitor
*v
, void *opaque
,
1116 const char *name
, Error
**errp
)
1118 X86CPU
*cpu
= X86_CPU(obj
);
1119 CPUX86State
*env
= &cpu
->env
;
1120 const int64_t min
= 0;
1121 const int64_t max
= 0xff;
1124 visit_type_int(v
, &value
, name
, errp
);
1125 if (error_is_set(errp
)) {
1128 if (value
< min
|| value
> max
) {
1129 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1130 name
? name
: "null", value
, min
, max
);
1134 env
->cpuid_version
&= ~0xf00f0;
1135 env
->cpuid_version
|= ((value
& 0xf) << 4) | ((value
>> 4) << 16);
1138 static void x86_cpuid_version_get_stepping(Object
*obj
, Visitor
*v
,
1139 void *opaque
, const char *name
,
1142 X86CPU
*cpu
= X86_CPU(obj
);
1143 CPUX86State
*env
= &cpu
->env
;
1146 value
= env
->cpuid_version
& 0xf;
1147 visit_type_int(v
, &value
, name
, errp
);
1150 static void x86_cpuid_version_set_stepping(Object
*obj
, Visitor
*v
,
1151 void *opaque
, const char *name
,
1154 X86CPU
*cpu
= X86_CPU(obj
);
1155 CPUX86State
*env
= &cpu
->env
;
1156 const int64_t min
= 0;
1157 const int64_t max
= 0xf;
1160 visit_type_int(v
, &value
, name
, errp
);
1161 if (error_is_set(errp
)) {
1164 if (value
< min
|| value
> max
) {
1165 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1166 name
? name
: "null", value
, min
, max
);
1170 env
->cpuid_version
&= ~0xf;
1171 env
->cpuid_version
|= value
& 0xf;
1174 static void x86_cpuid_get_level(Object
*obj
, Visitor
*v
, void *opaque
,
1175 const char *name
, Error
**errp
)
1177 X86CPU
*cpu
= X86_CPU(obj
);
1179 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1182 static void x86_cpuid_set_level(Object
*obj
, Visitor
*v
, void *opaque
,
1183 const char *name
, Error
**errp
)
1185 X86CPU
*cpu
= X86_CPU(obj
);
1187 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1190 static void x86_cpuid_get_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1191 const char *name
, Error
**errp
)
1193 X86CPU
*cpu
= X86_CPU(obj
);
1195 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1198 static void x86_cpuid_set_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1199 const char *name
, Error
**errp
)
1201 X86CPU
*cpu
= X86_CPU(obj
);
1203 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1206 static char *x86_cpuid_get_vendor(Object
*obj
, Error
**errp
)
1208 X86CPU
*cpu
= X86_CPU(obj
);
1209 CPUX86State
*env
= &cpu
->env
;
1213 value
= (char *)g_malloc(CPUID_VENDOR_SZ
+ 1);
1214 for (i
= 0; i
< 4; i
++) {
1215 value
[i
] = env
->cpuid_vendor1
>> (8 * i
);
1216 value
[i
+ 4] = env
->cpuid_vendor2
>> (8 * i
);
1217 value
[i
+ 8] = env
->cpuid_vendor3
>> (8 * i
);
1219 value
[CPUID_VENDOR_SZ
] = '\0';
1223 static void x86_cpuid_set_vendor(Object
*obj
, const char *value
,
1226 X86CPU
*cpu
= X86_CPU(obj
);
1227 CPUX86State
*env
= &cpu
->env
;
1230 if (strlen(value
) != CPUID_VENDOR_SZ
) {
1231 error_set(errp
, QERR_PROPERTY_VALUE_BAD
, "",
1236 env
->cpuid_vendor1
= 0;
1237 env
->cpuid_vendor2
= 0;
1238 env
->cpuid_vendor3
= 0;
1239 for (i
= 0; i
< 4; i
++) {
1240 env
->cpuid_vendor1
|= ((uint8_t)value
[i
]) << (8 * i
);
1241 env
->cpuid_vendor2
|= ((uint8_t)value
[i
+ 4]) << (8 * i
);
1242 env
->cpuid_vendor3
|= ((uint8_t)value
[i
+ 8]) << (8 * i
);
1244 env
->cpuid_vendor_override
= 1;
1247 static char *x86_cpuid_get_model_id(Object
*obj
, Error
**errp
)
1249 X86CPU
*cpu
= X86_CPU(obj
);
1250 CPUX86State
*env
= &cpu
->env
;
1254 value
= g_malloc(48 + 1);
1255 for (i
= 0; i
< 48; i
++) {
1256 value
[i
] = env
->cpuid_model
[i
>> 2] >> (8 * (i
& 3));
1262 static void x86_cpuid_set_model_id(Object
*obj
, const char *model_id
,
1265 X86CPU
*cpu
= X86_CPU(obj
);
1266 CPUX86State
*env
= &cpu
->env
;
1269 if (model_id
== NULL
) {
1272 len
= strlen(model_id
);
1273 memset(env
->cpuid_model
, 0, 48);
1274 for (i
= 0; i
< 48; i
++) {
1278 c
= (uint8_t)model_id
[i
];
1280 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
1284 static void x86_cpuid_get_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1285 const char *name
, Error
**errp
)
1287 X86CPU
*cpu
= X86_CPU(obj
);
1290 value
= cpu
->env
.tsc_khz
* 1000;
1291 visit_type_int(v
, &value
, name
, errp
);
1294 static void x86_cpuid_set_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1295 const char *name
, Error
**errp
)
1297 X86CPU
*cpu
= X86_CPU(obj
);
1298 const int64_t min
= 0;
1299 const int64_t max
= INT64_MAX
;
1302 visit_type_int(v
, &value
, name
, errp
);
1303 if (error_is_set(errp
)) {
1306 if (value
< min
|| value
> max
) {
1307 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1308 name
? name
: "null", value
, min
, max
);
1312 cpu
->env
.tsc_khz
= value
/ 1000;
1315 static int cpu_x86_find_by_name(x86_def_t
*x86_cpu_def
, const char *name
)
1319 for (def
= x86_defs
; def
; def
= def
->next
) {
1320 if (name
&& !strcmp(name
, def
->name
)) {
1324 if (kvm_enabled() && name
&& strcmp(name
, "host") == 0) {
1325 kvm_cpu_fill_host(x86_cpu_def
);
1329 memcpy(x86_cpu_def
, def
, sizeof(*def
));
1335 /* Parse "+feature,-feature,feature=foo" CPU feature string
1337 static int cpu_x86_parse_featurestr(x86_def_t
*x86_cpu_def
, char *features
)
1340 char *featurestr
; /* Single 'key=value" string being parsed */
1341 /* Features to be added */
1342 FeatureWordArray plus_features
= { 0 };
1343 /* Features to be removed */
1344 FeatureWordArray minus_features
= { 0 };
1347 featurestr
= features
? strtok(features
, ",") : NULL
;
1349 while (featurestr
) {
1351 if (featurestr
[0] == '+') {
1352 add_flagname_to_bitmaps(featurestr
+ 1, plus_features
);
1353 } else if (featurestr
[0] == '-') {
1354 add_flagname_to_bitmaps(featurestr
+ 1, minus_features
);
1355 } else if ((val
= strchr(featurestr
, '='))) {
1357 if (!strcmp(featurestr
, "family")) {
1359 numvalue
= strtoul(val
, &err
, 0);
1360 if (!*val
|| *err
|| numvalue
> 0xff + 0xf) {
1361 fprintf(stderr
, "bad numerical value %s\n", val
);
1364 x86_cpu_def
->family
= numvalue
;
1365 } else if (!strcmp(featurestr
, "model")) {
1367 numvalue
= strtoul(val
, &err
, 0);
1368 if (!*val
|| *err
|| numvalue
> 0xff) {
1369 fprintf(stderr
, "bad numerical value %s\n", val
);
1372 x86_cpu_def
->model
= numvalue
;
1373 } else if (!strcmp(featurestr
, "stepping")) {
1375 numvalue
= strtoul(val
, &err
, 0);
1376 if (!*val
|| *err
|| numvalue
> 0xf) {
1377 fprintf(stderr
, "bad numerical value %s\n", val
);
1380 x86_cpu_def
->stepping
= numvalue
;
1381 } else if (!strcmp(featurestr
, "level")) {
1383 numvalue
= strtoul(val
, &err
, 0);
1384 if (!*val
|| *err
) {
1385 fprintf(stderr
, "bad numerical value %s\n", val
);
1388 x86_cpu_def
->level
= numvalue
;
1389 } else if (!strcmp(featurestr
, "xlevel")) {
1391 numvalue
= strtoul(val
, &err
, 0);
1392 if (!*val
|| *err
) {
1393 fprintf(stderr
, "bad numerical value %s\n", val
);
1396 if (numvalue
< 0x80000000) {
1397 numvalue
+= 0x80000000;
1399 x86_cpu_def
->xlevel
= numvalue
;
1400 } else if (!strcmp(featurestr
, "vendor")) {
1401 if (strlen(val
) != 12) {
1402 fprintf(stderr
, "vendor string must be 12 chars long\n");
1405 x86_cpu_def
->vendor1
= 0;
1406 x86_cpu_def
->vendor2
= 0;
1407 x86_cpu_def
->vendor3
= 0;
1408 for(i
= 0; i
< 4; i
++) {
1409 x86_cpu_def
->vendor1
|= ((uint8_t)val
[i
]) << (8 * i
);
1410 x86_cpu_def
->vendor2
|= ((uint8_t)val
[i
+ 4]) << (8 * i
);
1411 x86_cpu_def
->vendor3
|= ((uint8_t)val
[i
+ 8]) << (8 * i
);
1413 x86_cpu_def
->vendor_override
= 1;
1414 } else if (!strcmp(featurestr
, "model_id")) {
1415 pstrcpy(x86_cpu_def
->model_id
, sizeof(x86_cpu_def
->model_id
),
1417 } else if (!strcmp(featurestr
, "tsc_freq")) {
1421 tsc_freq
= strtosz_suffix_unit(val
, &err
,
1422 STRTOSZ_DEFSUFFIX_B
, 1000);
1423 if (tsc_freq
< 0 || *err
) {
1424 fprintf(stderr
, "bad numerical value %s\n", val
);
1427 x86_cpu_def
->tsc_khz
= tsc_freq
/ 1000;
1428 } else if (!strcmp(featurestr
, "hv_spinlocks")) {
1430 numvalue
= strtoul(val
, &err
, 0);
1431 if (!*val
|| *err
) {
1432 fprintf(stderr
, "bad numerical value %s\n", val
);
1435 hyperv_set_spinlock_retries(numvalue
);
1437 fprintf(stderr
, "unrecognized feature %s\n", featurestr
);
1440 } else if (!strcmp(featurestr
, "check")) {
1442 } else if (!strcmp(featurestr
, "enforce")) {
1443 check_cpuid
= enforce_cpuid
= 1;
1444 } else if (!strcmp(featurestr
, "hv_relaxed")) {
1445 hyperv_enable_relaxed_timing(true);
1446 } else if (!strcmp(featurestr
, "hv_vapic")) {
1447 hyperv_enable_vapic_recommended(true);
1449 fprintf(stderr
, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr
);
1452 featurestr
= strtok(NULL
, ",");
1454 x86_cpu_def
->features
|= plus_features
[FEAT_1_EDX
];
1455 x86_cpu_def
->ext_features
|= plus_features
[FEAT_1_ECX
];
1456 x86_cpu_def
->ext2_features
|= plus_features
[FEAT_8000_0001_EDX
];
1457 x86_cpu_def
->ext3_features
|= plus_features
[FEAT_8000_0001_ECX
];
1458 x86_cpu_def
->ext4_features
|= plus_features
[FEAT_C000_0001_EDX
];
1459 x86_cpu_def
->kvm_features
|= plus_features
[FEAT_KVM
];
1460 x86_cpu_def
->svm_features
|= plus_features
[FEAT_SVM
];
1461 x86_cpu_def
->cpuid_7_0_ebx_features
|= plus_features
[FEAT_7_0_EBX
];
1462 x86_cpu_def
->features
&= ~minus_features
[FEAT_1_EDX
];
1463 x86_cpu_def
->ext_features
&= ~minus_features
[FEAT_1_ECX
];
1464 x86_cpu_def
->ext2_features
&= ~minus_features
[FEAT_8000_0001_EDX
];
1465 x86_cpu_def
->ext3_features
&= ~minus_features
[FEAT_8000_0001_ECX
];
1466 x86_cpu_def
->ext4_features
&= ~minus_features
[FEAT_C000_0001_EDX
];
1467 x86_cpu_def
->kvm_features
&= ~minus_features
[FEAT_KVM
];
1468 x86_cpu_def
->svm_features
&= ~minus_features
[FEAT_SVM
];
1469 x86_cpu_def
->cpuid_7_0_ebx_features
&= ~minus_features
[FEAT_7_0_EBX
];
1476 /* generate a composite string into buf of all cpuid names in featureset
1477 * selected by fbits. indicate truncation at bufsize in the event of overflow.
1478 * if flags, suppress names undefined in featureset.
1480 static void listflags(char *buf
, int bufsize
, uint32_t fbits
,
1481 const char **featureset
, uint32_t flags
)
1483 const char **p
= &featureset
[31];
1487 b
= 4 <= bufsize
? buf
+ (bufsize
-= 3) - 1 : NULL
;
1489 for (q
= buf
, bit
= 31; fbits
&& bufsize
; --p
, fbits
&= ~(1 << bit
), --bit
)
1490 if (fbits
& 1 << bit
&& (*p
|| !flags
)) {
1492 nc
= snprintf(q
, bufsize
, "%s%s", q
== buf
? "" : " ", *p
);
1494 nc
= snprintf(q
, bufsize
, "%s[%d]", q
== buf
? "" : " ", bit
);
1495 if (bufsize
<= nc
) {
1497 memcpy(b
, "...", sizeof("..."));
1506 /* generate CPU information. */
1507 void x86_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
1512 for (def
= x86_defs
; def
; def
= def
->next
) {
1513 snprintf(buf
, sizeof(buf
), "%s", def
->name
);
1514 (*cpu_fprintf
)(f
, "x86 %16s %-48s\n", buf
, def
->model_id
);
1516 if (kvm_enabled()) {
1517 (*cpu_fprintf
)(f
, "x86 %16s\n", "[host]");
1519 (*cpu_fprintf
)(f
, "\nRecognized CPUID flags:\n");
1520 listflags(buf
, sizeof(buf
), (uint32_t)~0, feature_name
, 1);
1521 (*cpu_fprintf
)(f
, " %s\n", buf
);
1522 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext_feature_name
, 1);
1523 (*cpu_fprintf
)(f
, " %s\n", buf
);
1524 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext2_feature_name
, 1);
1525 (*cpu_fprintf
)(f
, " %s\n", buf
);
1526 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext3_feature_name
, 1);
1527 (*cpu_fprintf
)(f
, " %s\n", buf
);
1530 CpuDefinitionInfoList
*arch_query_cpu_definitions(Error
**errp
)
1532 CpuDefinitionInfoList
*cpu_list
= NULL
;
1535 for (def
= x86_defs
; def
; def
= def
->next
) {
1536 CpuDefinitionInfoList
*entry
;
1537 CpuDefinitionInfo
*info
;
1539 info
= g_malloc0(sizeof(*info
));
1540 info
->name
= g_strdup(def
->name
);
1542 entry
= g_malloc0(sizeof(*entry
));
1543 entry
->value
= info
;
1544 entry
->next
= cpu_list
;
1552 static void filter_features_for_kvm(X86CPU
*cpu
)
1554 CPUX86State
*env
= &cpu
->env
;
1555 KVMState
*s
= kvm_state
;
1557 env
->cpuid_features
&=
1558 kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
1559 env
->cpuid_ext_features
&=
1560 kvm_arch_get_supported_cpuid(s
, 1, 0, R_ECX
);
1561 env
->cpuid_ext2_features
&=
1562 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
1563 env
->cpuid_ext3_features
&=
1564 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
1565 env
->cpuid_svm_features
&=
1566 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
1567 env
->cpuid_7_0_ebx_features
&=
1568 kvm_arch_get_supported_cpuid(s
, 7, 0, R_EBX
);
1569 env
->cpuid_kvm_features
&=
1570 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
1571 env
->cpuid_ext4_features
&=
1572 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
1577 int cpu_x86_register(X86CPU
*cpu
, const char *cpu_model
)
1579 CPUX86State
*env
= &cpu
->env
;
1580 x86_def_t def1
, *def
= &def1
;
1581 Error
*error
= NULL
;
1582 char *name
, *features
;
1583 gchar
**model_pieces
;
1585 memset(def
, 0, sizeof(*def
));
1587 model_pieces
= g_strsplit(cpu_model
, ",", 2);
1588 if (!model_pieces
[0]) {
1589 error_setg(&error
, "Invalid/empty CPU model name");
1592 name
= model_pieces
[0];
1593 features
= model_pieces
[1];
1595 if (cpu_x86_find_by_name(def
, name
) < 0) {
1596 error_setg(&error
, "Unable to find CPU definition: %s", name
);
1600 if (kvm_enabled()) {
1601 def
->kvm_features
|= kvm_default_features
;
1603 def
->ext_features
|= CPUID_EXT_HYPERVISOR
;
1605 if (cpu_x86_parse_featurestr(def
, features
) < 0) {
1606 error_setg(&error
, "Invalid cpu_model string format: %s", cpu_model
);
1609 assert(def
->vendor1
);
1610 env
->cpuid_vendor1
= def
->vendor1
;
1611 env
->cpuid_vendor2
= def
->vendor2
;
1612 env
->cpuid_vendor3
= def
->vendor3
;
1613 env
->cpuid_vendor_override
= def
->vendor_override
;
1614 object_property_set_int(OBJECT(cpu
), def
->level
, "level", &error
);
1615 object_property_set_int(OBJECT(cpu
), def
->family
, "family", &error
);
1616 object_property_set_int(OBJECT(cpu
), def
->model
, "model", &error
);
1617 object_property_set_int(OBJECT(cpu
), def
->stepping
, "stepping", &error
);
1618 env
->cpuid_features
= def
->features
;
1619 env
->cpuid_ext_features
= def
->ext_features
;
1620 env
->cpuid_ext2_features
= def
->ext2_features
;
1621 env
->cpuid_ext3_features
= def
->ext3_features
;
1622 object_property_set_int(OBJECT(cpu
), def
->xlevel
, "xlevel", &error
);
1623 env
->cpuid_kvm_features
= def
->kvm_features
;
1624 env
->cpuid_svm_features
= def
->svm_features
;
1625 env
->cpuid_ext4_features
= def
->ext4_features
;
1626 env
->cpuid_7_0_ebx_features
= def
->cpuid_7_0_ebx_features
;
1627 env
->cpuid_xlevel2
= def
->xlevel2
;
1628 object_property_set_int(OBJECT(cpu
), (int64_t)def
->tsc_khz
* 1000,
1629 "tsc-frequency", &error
);
1631 object_property_set_str(OBJECT(cpu
), def
->model_id
, "model-id", &error
);
1634 g_strfreev(model_pieces
);
1636 fprintf(stderr
, "%s\n", error_get_pretty(error
));
1643 #if !defined(CONFIG_USER_ONLY)
1645 void cpu_clear_apic_feature(CPUX86State
*env
)
1647 env
->cpuid_features
&= ~CPUID_APIC
;
1650 #endif /* !CONFIG_USER_ONLY */
1652 /* Initialize list of CPU models, filling some non-static fields if necessary
1654 void x86_cpudef_setup(void)
1657 static const char *model_with_versions
[] = { "qemu32", "qemu64", "athlon" };
1659 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); ++i
) {
1660 x86_def_t
*def
= &builtin_x86_defs
[i
];
1661 def
->next
= x86_defs
;
1663 /* Look for specific "cpudef" models that */
1664 /* have the QEMU version in .model_id */
1665 for (j
= 0; j
< ARRAY_SIZE(model_with_versions
); j
++) {
1666 if (strcmp(model_with_versions
[j
], def
->name
) == 0) {
1667 pstrcpy(def
->model_id
, sizeof(def
->model_id
),
1668 "QEMU Virtual CPU version ");
1669 pstrcat(def
->model_id
, sizeof(def
->model_id
),
1670 qemu_get_version());
1679 static void get_cpuid_vendor(CPUX86State
*env
, uint32_t *ebx
,
1680 uint32_t *ecx
, uint32_t *edx
)
1682 *ebx
= env
->cpuid_vendor1
;
1683 *edx
= env
->cpuid_vendor2
;
1684 *ecx
= env
->cpuid_vendor3
;
1686 /* sysenter isn't supported on compatibility mode on AMD, syscall
1687 * isn't supported in compatibility mode on Intel.
1688 * Normally we advertise the actual cpu vendor, but you can override
1689 * this if you want to use KVM's sysenter/syscall emulation
1690 * in compatibility mode and when doing cross vendor migration
1692 if (kvm_enabled() && ! env
->cpuid_vendor_override
) {
1693 host_cpuid(0, 0, NULL
, ebx
, ecx
, edx
);
1697 void cpu_x86_cpuid(CPUX86State
*env
, uint32_t index
, uint32_t count
,
1698 uint32_t *eax
, uint32_t *ebx
,
1699 uint32_t *ecx
, uint32_t *edx
)
1701 X86CPU
*cpu
= x86_env_get_cpu(env
);
1702 CPUState
*cs
= CPU(cpu
);
1704 /* test if maximum index reached */
1705 if (index
& 0x80000000) {
1706 if (index
> env
->cpuid_xlevel
) {
1707 if (env
->cpuid_xlevel2
> 0) {
1708 /* Handle the Centaur's CPUID instruction. */
1709 if (index
> env
->cpuid_xlevel2
) {
1710 index
= env
->cpuid_xlevel2
;
1711 } else if (index
< 0xC0000000) {
1712 index
= env
->cpuid_xlevel
;
1715 /* Intel documentation states that invalid EAX input will
1716 * return the same information as EAX=cpuid_level
1717 * (Intel SDM Vol. 2A - Instruction Set Reference - CPUID)
1719 index
= env
->cpuid_level
;
1723 if (index
> env
->cpuid_level
)
1724 index
= env
->cpuid_level
;
1729 *eax
= env
->cpuid_level
;
1730 get_cpuid_vendor(env
, ebx
, ecx
, edx
);
1733 *eax
= env
->cpuid_version
;
1734 *ebx
= (env
->cpuid_apic_id
<< 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1735 *ecx
= env
->cpuid_ext_features
;
1736 *edx
= env
->cpuid_features
;
1737 if (cs
->nr_cores
* cs
->nr_threads
> 1) {
1738 *ebx
|= (cs
->nr_cores
* cs
->nr_threads
) << 16;
1739 *edx
|= 1 << 28; /* HTT bit */
1743 /* cache info: needed for Pentium Pro compatibility */
1750 /* cache info: needed for Core compatibility */
1751 if (cs
->nr_cores
> 1) {
1752 *eax
= (cs
->nr_cores
- 1) << 26;
1757 case 0: /* L1 dcache info */
1763 case 1: /* L1 icache info */
1769 case 2: /* L2 cache info */
1771 if (cs
->nr_threads
> 1) {
1772 *eax
|= (cs
->nr_threads
- 1) << 14;
1778 default: /* end of info */
1787 /* mwait info: needed for Core compatibility */
1788 *eax
= 0; /* Smallest monitor-line size in bytes */
1789 *ebx
= 0; /* Largest monitor-line size in bytes */
1790 *ecx
= CPUID_MWAIT_EMX
| CPUID_MWAIT_IBE
;
1794 /* Thermal and Power Leaf */
1801 /* Structured Extended Feature Flags Enumeration Leaf */
1803 *eax
= 0; /* Maximum ECX value for sub-leaves */
1804 *ebx
= env
->cpuid_7_0_ebx_features
; /* Feature flags */
1805 *ecx
= 0; /* Reserved */
1806 *edx
= 0; /* Reserved */
1815 /* Direct Cache Access Information Leaf */
1816 *eax
= 0; /* Bits 0-31 in DCA_CAP MSR */
1822 /* Architectural Performance Monitoring Leaf */
1823 if (kvm_enabled()) {
1824 KVMState
*s
= cs
->kvm_state
;
1826 *eax
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EAX
);
1827 *ebx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EBX
);
1828 *ecx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_ECX
);
1829 *edx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EDX
);
1838 /* Processor Extended State */
1839 if (!(env
->cpuid_ext_features
& CPUID_EXT_XSAVE
)) {
1846 if (kvm_enabled()) {
1847 KVMState
*s
= cs
->kvm_state
;
1849 *eax
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EAX
);
1850 *ebx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EBX
);
1851 *ecx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_ECX
);
1852 *edx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EDX
);
1861 *eax
= env
->cpuid_xlevel
;
1862 *ebx
= env
->cpuid_vendor1
;
1863 *edx
= env
->cpuid_vendor2
;
1864 *ecx
= env
->cpuid_vendor3
;
1867 *eax
= env
->cpuid_version
;
1869 *ecx
= env
->cpuid_ext3_features
;
1870 *edx
= env
->cpuid_ext2_features
;
1872 /* The Linux kernel checks for the CMPLegacy bit and
1873 * discards multiple thread information if it is set.
1874 * So dont set it here for Intel to make Linux guests happy.
1876 if (cs
->nr_cores
* cs
->nr_threads
> 1) {
1877 uint32_t tebx
, tecx
, tedx
;
1878 get_cpuid_vendor(env
, &tebx
, &tecx
, &tedx
);
1879 if (tebx
!= CPUID_VENDOR_INTEL_1
||
1880 tedx
!= CPUID_VENDOR_INTEL_2
||
1881 tecx
!= CPUID_VENDOR_INTEL_3
) {
1882 *ecx
|= 1 << 1; /* CmpLegacy bit */
1889 *eax
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 0];
1890 *ebx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 1];
1891 *ecx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 2];
1892 *edx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 3];
1895 /* cache info (L1 cache) */
1902 /* cache info (L2 cache) */
1909 /* virtual & phys address size in low 2 bytes. */
1910 /* XXX: This value must match the one used in the MMU code. */
1911 if (env
->cpuid_ext2_features
& CPUID_EXT2_LM
) {
1912 /* 64 bit processor */
1913 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1914 *eax
= 0x00003028; /* 48 bits virtual, 40 bits physical */
1916 if (env
->cpuid_features
& CPUID_PSE36
)
1917 *eax
= 0x00000024; /* 36 bits physical */
1919 *eax
= 0x00000020; /* 32 bits physical */
1924 if (cs
->nr_cores
* cs
->nr_threads
> 1) {
1925 *ecx
|= (cs
->nr_cores
* cs
->nr_threads
) - 1;
1929 if (env
->cpuid_ext3_features
& CPUID_EXT3_SVM
) {
1930 *eax
= 0x00000001; /* SVM Revision */
1931 *ebx
= 0x00000010; /* nr of ASIDs */
1933 *edx
= env
->cpuid_svm_features
; /* optional features */
1942 *eax
= env
->cpuid_xlevel2
;
1948 /* Support for VIA CPU's CPUID instruction */
1949 *eax
= env
->cpuid_version
;
1952 *edx
= env
->cpuid_ext4_features
;
1957 /* Reserved for the future, and now filled with zero */
1964 /* reserved values: zero */
1973 /* CPUClass::reset() */
1974 static void x86_cpu_reset(CPUState
*s
)
1976 X86CPU
*cpu
= X86_CPU(s
);
1977 X86CPUClass
*xcc
= X86_CPU_GET_CLASS(cpu
);
1978 CPUX86State
*env
= &cpu
->env
;
1981 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
1982 qemu_log("CPU Reset (CPU %d)\n", s
->cpu_index
);
1983 log_cpu_state(env
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
1986 xcc
->parent_reset(s
);
1989 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
1993 env
->old_exception
= -1;
1995 /* init to reset state */
1997 #ifdef CONFIG_SOFTMMU
1998 env
->hflags
|= HF_SOFTMMU_MASK
;
2000 env
->hflags2
|= HF2_GIF_MASK
;
2002 cpu_x86_update_cr0(env
, 0x60000010);
2003 env
->a20_mask
= ~0x0;
2004 env
->smbase
= 0x30000;
2006 env
->idt
.limit
= 0xffff;
2007 env
->gdt
.limit
= 0xffff;
2008 env
->ldt
.limit
= 0xffff;
2009 env
->ldt
.flags
= DESC_P_MASK
| (2 << DESC_TYPE_SHIFT
);
2010 env
->tr
.limit
= 0xffff;
2011 env
->tr
.flags
= DESC_P_MASK
| (11 << DESC_TYPE_SHIFT
);
2013 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff,
2014 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
|
2015 DESC_R_MASK
| DESC_A_MASK
);
2016 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff,
2017 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2019 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff,
2020 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2022 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff,
2023 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2025 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff,
2026 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2028 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff,
2029 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
2033 env
->regs
[R_EDX
] = env
->cpuid_version
;
2038 for (i
= 0; i
< 8; i
++) {
2043 env
->mxcsr
= 0x1f80;
2045 env
->pat
= 0x0007040600070406ULL
;
2046 env
->msr_ia32_misc_enable
= MSR_IA32_MISC_ENABLE_DEFAULT
;
2048 memset(env
->dr
, 0, sizeof(env
->dr
));
2049 env
->dr
[6] = DR6_FIXED_1
;
2050 env
->dr
[7] = DR7_FIXED_1
;
2051 cpu_breakpoint_remove_all(env
, BP_CPU
);
2052 cpu_watchpoint_remove_all(env
, BP_CPU
);
2054 #if !defined(CONFIG_USER_ONLY)
2055 /* We hard-wire the BSP to the first CPU. */
2056 if (s
->cpu_index
== 0) {
2057 apic_designate_bsp(env
->apic_state
);
2060 env
->halted
= !cpu_is_bsp(cpu
);
2064 #ifndef CONFIG_USER_ONLY
2065 bool cpu_is_bsp(X86CPU
*cpu
)
2067 return cpu_get_apic_base(cpu
->env
.apic_state
) & MSR_IA32_APICBASE_BSP
;
2070 /* TODO: remove me, when reset over QOM tree is implemented */
2071 static void x86_cpu_machine_reset_cb(void *opaque
)
2073 X86CPU
*cpu
= opaque
;
2074 cpu_reset(CPU(cpu
));
2078 static void mce_init(X86CPU
*cpu
)
2080 CPUX86State
*cenv
= &cpu
->env
;
2083 if (((cenv
->cpuid_version
>> 8) & 0xf) >= 6
2084 && (cenv
->cpuid_features
& (CPUID_MCE
| CPUID_MCA
)) ==
2085 (CPUID_MCE
| CPUID_MCA
)) {
2086 cenv
->mcg_cap
= MCE_CAP_DEF
| MCE_BANKS_DEF
;
2087 cenv
->mcg_ctl
= ~(uint64_t)0;
2088 for (bank
= 0; bank
< MCE_BANKS_DEF
; bank
++) {
2089 cenv
->mce_banks
[bank
* 4] = ~(uint64_t)0;
2094 #define MSI_ADDR_BASE 0xfee00000
2096 #ifndef CONFIG_USER_ONLY
2097 static void x86_cpu_apic_init(X86CPU
*cpu
, Error
**errp
)
2099 static int apic_mapped
;
2100 CPUX86State
*env
= &cpu
->env
;
2101 APICCommonState
*apic
;
2102 const char *apic_type
= "apic";
2104 if (kvm_irqchip_in_kernel()) {
2105 apic_type
= "kvm-apic";
2106 } else if (xen_enabled()) {
2107 apic_type
= "xen-apic";
2110 env
->apic_state
= qdev_try_create(NULL
, apic_type
);
2111 if (env
->apic_state
== NULL
) {
2112 error_setg(errp
, "APIC device '%s' could not be created", apic_type
);
2116 object_property_add_child(OBJECT(cpu
), "apic",
2117 OBJECT(env
->apic_state
), NULL
);
2118 qdev_prop_set_uint8(env
->apic_state
, "id", env
->cpuid_apic_id
);
2119 /* TODO: convert to link<> */
2120 apic
= APIC_COMMON(env
->apic_state
);
2123 if (qdev_init(env
->apic_state
)) {
2124 error_setg(errp
, "APIC device '%s' could not be initialized",
2125 object_get_typename(OBJECT(env
->apic_state
)));
2129 /* XXX: mapping more APICs at the same memory location */
2130 if (apic_mapped
== 0) {
2131 /* NOTE: the APIC is directly connected to the CPU - it is not
2132 on the global memory bus. */
2133 /* XXX: what if the base changes? */
2134 sysbus_mmio_map(SYS_BUS_DEVICE(env
->apic_state
), 0, MSI_ADDR_BASE
);
2140 void x86_cpu_realize(Object
*obj
, Error
**errp
)
2142 X86CPU
*cpu
= X86_CPU(obj
);
2143 CPUX86State
*env
= &cpu
->env
;
2145 if (env
->cpuid_7_0_ebx_features
&& env
->cpuid_level
< 7) {
2146 env
->cpuid_level
= 7;
2149 /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on
2152 if (env
->cpuid_vendor1
== CPUID_VENDOR_AMD_1
&&
2153 env
->cpuid_vendor2
== CPUID_VENDOR_AMD_2
&&
2154 env
->cpuid_vendor3
== CPUID_VENDOR_AMD_3
) {
2155 env
->cpuid_ext2_features
&= ~CPUID_EXT2_AMD_ALIASES
;
2156 env
->cpuid_ext2_features
|= (env
->cpuid_features
2157 & CPUID_EXT2_AMD_ALIASES
);
2160 if (!kvm_enabled()) {
2161 env
->cpuid_features
&= TCG_FEATURES
;
2162 env
->cpuid_ext_features
&= TCG_EXT_FEATURES
;
2163 env
->cpuid_ext2_features
&= (TCG_EXT2_FEATURES
2164 #ifdef TARGET_X86_64
2165 | CPUID_EXT2_SYSCALL
| CPUID_EXT2_LM
2168 env
->cpuid_ext3_features
&= TCG_EXT3_FEATURES
;
2169 env
->cpuid_svm_features
&= TCG_SVM_FEATURES
;
2172 filter_features_for_kvm(cpu
);
2174 if (check_cpuid
&& kvm_check_features_against_host(cpu
)
2176 error_setg(errp
, "Host's CPU doesn't support requested features");
2181 #ifndef CONFIG_USER_ONLY
2182 qemu_register_reset(x86_cpu_machine_reset_cb
, cpu
);
2184 if (cpu
->env
.cpuid_features
& CPUID_APIC
|| smp_cpus
> 1) {
2185 x86_cpu_apic_init(cpu
, errp
);
2186 if (error_is_set(errp
)) {
2193 qemu_init_vcpu(&cpu
->env
);
2194 cpu_reset(CPU(cpu
));
2197 /* Calculates initial APIC ID for a specific CPU index
2199 * Currently we need to be able to calculate the APIC ID from the CPU index
2200 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
2201 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
2202 * all CPUs up to max_cpus.
2204 uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index
)
2206 /* right now APIC ID == CPU index. this will eventually change to use
2207 * the CPU topology configuration properly
2212 static void x86_cpu_initfn(Object
*obj
)
2214 CPUState
*cs
= CPU(obj
);
2215 X86CPU
*cpu
= X86_CPU(obj
);
2216 CPUX86State
*env
= &cpu
->env
;
2221 object_property_add(obj
, "family", "int",
2222 x86_cpuid_version_get_family
,
2223 x86_cpuid_version_set_family
, NULL
, NULL
, NULL
);
2224 object_property_add(obj
, "model", "int",
2225 x86_cpuid_version_get_model
,
2226 x86_cpuid_version_set_model
, NULL
, NULL
, NULL
);
2227 object_property_add(obj
, "stepping", "int",
2228 x86_cpuid_version_get_stepping
,
2229 x86_cpuid_version_set_stepping
, NULL
, NULL
, NULL
);
2230 object_property_add(obj
, "level", "int",
2231 x86_cpuid_get_level
,
2232 x86_cpuid_set_level
, NULL
, NULL
, NULL
);
2233 object_property_add(obj
, "xlevel", "int",
2234 x86_cpuid_get_xlevel
,
2235 x86_cpuid_set_xlevel
, NULL
, NULL
, NULL
);
2236 object_property_add_str(obj
, "vendor",
2237 x86_cpuid_get_vendor
,
2238 x86_cpuid_set_vendor
, NULL
);
2239 object_property_add_str(obj
, "model-id",
2240 x86_cpuid_get_model_id
,
2241 x86_cpuid_set_model_id
, NULL
);
2242 object_property_add(obj
, "tsc-frequency", "int",
2243 x86_cpuid_get_tsc_freq
,
2244 x86_cpuid_set_tsc_freq
, NULL
, NULL
, NULL
);
2246 env
->cpuid_apic_id
= x86_cpu_apic_id_from_index(cs
->cpu_index
);
2248 /* init various static tables used in TCG mode */
2249 if (tcg_enabled() && !inited
) {
2251 optimize_flags_init();
2252 #ifndef CONFIG_USER_ONLY
2253 cpu_set_debug_excp_handler(breakpoint_handler
);
2258 static void x86_cpu_common_class_init(ObjectClass
*oc
, void *data
)
2260 X86CPUClass
*xcc
= X86_CPU_CLASS(oc
);
2261 CPUClass
*cc
= CPU_CLASS(oc
);
2263 xcc
->parent_reset
= cc
->reset
;
2264 cc
->reset
= x86_cpu_reset
;
2267 static const TypeInfo x86_cpu_type_info
= {
2268 .name
= TYPE_X86_CPU
,
2270 .instance_size
= sizeof(X86CPU
),
2271 .instance_init
= x86_cpu_initfn
,
2273 .class_size
= sizeof(X86CPUClass
),
2274 .class_init
= x86_cpu_common_class_init
,
2277 static void x86_cpu_register_types(void)
2279 type_register_static(&x86_cpu_type_info
);
2282 type_init(x86_cpu_register_types
)