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 *kvm_feature_name
[] = {
99 "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock",
100 "kvm_asyncpf", "kvm_steal_time", "kvm_pv_eoi", NULL
,
101 NULL
, NULL
, NULL
, NULL
,
102 NULL
, NULL
, 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 *svm_feature_name
[] = {
110 "npt", "lbrv", "svm_lock", "nrip_save",
111 "tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists",
112 NULL
, NULL
, "pause_filter", NULL
,
113 "pfthreshold", 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 *cpuid_7_0_ebx_feature_name
[] = {
121 "fsgsbase", NULL
, NULL
, "bmi1", "hle", "avx2", NULL
, "smep",
122 "bmi2", "erms", "invpcid", "rtm", NULL
, NULL
, NULL
, NULL
,
123 NULL
, NULL
, "rdseed", "adx", "smap", NULL
, NULL
, NULL
,
124 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
127 const char *get_register_name_32(unsigned int reg
)
129 static const char *reg_names
[CPU_NB_REGS32
] = {
140 if (reg
> CPU_NB_REGS32
) {
143 return reg_names
[reg
];
146 /* collects per-function cpuid data
148 typedef struct model_features_t
{
149 uint32_t *guest_feat
;
151 const char **flag_names
;
157 int enforce_cpuid
= 0;
159 #if defined(CONFIG_KVM)
160 static uint32_t kvm_default_features
= (1 << KVM_FEATURE_CLOCKSOURCE
) |
161 (1 << KVM_FEATURE_NOP_IO_DELAY
) |
162 (1 << KVM_FEATURE_MMU_OP
) |
163 (1 << KVM_FEATURE_CLOCKSOURCE2
) |
164 (1 << KVM_FEATURE_ASYNC_PF
) |
165 (1 << KVM_FEATURE_STEAL_TIME
) |
166 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT
);
167 static const uint32_t kvm_pv_eoi_features
= (0x1 << KVM_FEATURE_PV_EOI
);
169 static uint32_t kvm_default_features
= 0;
170 static const uint32_t kvm_pv_eoi_features
= 0;
173 void enable_kvm_pv_eoi(void)
175 kvm_default_features
|= kvm_pv_eoi_features
;
178 void host_cpuid(uint32_t function
, uint32_t count
,
179 uint32_t *eax
, uint32_t *ebx
, uint32_t *ecx
, uint32_t *edx
)
181 #if defined(CONFIG_KVM)
186 : "=a"(vec
[0]), "=b"(vec
[1]),
187 "=c"(vec
[2]), "=d"(vec
[3])
188 : "0"(function
), "c"(count
) : "cc");
190 asm volatile("pusha \n\t"
192 "mov %%eax, 0(%2) \n\t"
193 "mov %%ebx, 4(%2) \n\t"
194 "mov %%ecx, 8(%2) \n\t"
195 "mov %%edx, 12(%2) \n\t"
197 : : "a"(function
), "c"(count
), "S"(vec
)
212 #define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
214 /* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
215 * a substring. ex if !NULL points to the first char after a substring,
216 * otherwise the string is assumed to sized by a terminating nul.
217 * Return lexical ordering of *s1:*s2.
219 static int sstrcmp(const char *s1
, const char *e1
, const char *s2
,
223 if (!*s1
|| !*s2
|| *s1
!= *s2
)
226 if (s1
== e1
&& s2
== e2
)
235 /* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
236 * '|' delimited (possibly empty) strings in which case search for a match
237 * within the alternatives proceeds left to right. Return 0 for success,
238 * non-zero otherwise.
240 static int altcmp(const char *s
, const char *e
, const char *altstr
)
244 for (q
= p
= altstr
; ; ) {
245 while (*p
&& *p
!= '|')
247 if ((q
== p
&& !*s
) || (q
!= p
&& !sstrcmp(s
, e
, q
, p
)))
256 /* search featureset for flag *[s..e), if found set corresponding bit in
257 * *pval and return true, otherwise return false
259 static bool lookup_feature(uint32_t *pval
, const char *s
, const char *e
,
260 const char **featureset
)
266 for (mask
= 1, ppc
= featureset
; mask
; mask
<<= 1, ++ppc
) {
267 if (*ppc
&& !altcmp(s
, e
, *ppc
)) {
275 static void add_flagname_to_bitmaps(const char *flagname
, uint32_t *features
,
276 uint32_t *ext_features
,
277 uint32_t *ext2_features
,
278 uint32_t *ext3_features
,
279 uint32_t *kvm_features
,
280 uint32_t *svm_features
,
281 uint32_t *cpuid_7_0_ebx_features
)
283 if (!lookup_feature(features
, flagname
, NULL
, feature_name
) &&
284 !lookup_feature(ext_features
, flagname
, NULL
, ext_feature_name
) &&
285 !lookup_feature(ext2_features
, flagname
, NULL
, ext2_feature_name
) &&
286 !lookup_feature(ext3_features
, flagname
, NULL
, ext3_feature_name
) &&
287 !lookup_feature(kvm_features
, flagname
, NULL
, kvm_feature_name
) &&
288 !lookup_feature(svm_features
, flagname
, NULL
, svm_feature_name
) &&
289 !lookup_feature(cpuid_7_0_ebx_features
, flagname
, NULL
,
290 cpuid_7_0_ebx_feature_name
))
291 fprintf(stderr
, "CPU feature %s not found\n", flagname
);
294 typedef struct x86_def_t
{
295 struct x86_def_t
*next
;
298 uint32_t vendor1
, vendor2
, vendor3
;
303 uint32_t features
, ext_features
, ext2_features
, ext3_features
;
304 uint32_t kvm_features
, svm_features
;
308 /* Store the results of Centaur's CPUID instructions */
309 uint32_t ext4_features
;
311 /* The feature bits on CPUID[EAX=7,ECX=0].EBX */
312 uint32_t cpuid_7_0_ebx_features
;
315 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
316 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
317 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
318 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
319 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
320 CPUID_PSE36 | CPUID_FXSR)
321 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
322 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
323 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
324 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
325 CPUID_PAE | CPUID_SEP | CPUID_APIC)
327 #define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
328 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
329 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
330 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
331 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
332 /* partly implemented:
333 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
334 CPUID_PSE36 (needed for Solaris) */
336 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
337 #define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \
338 CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT | \
339 CPUID_EXT_HYPERVISOR)
341 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
342 CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_XSAVE */
343 #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
344 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
345 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
347 CPUID_EXT2_PDPE1GB */
348 #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
349 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
350 #define TCG_SVM_FEATURES 0
351 #define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP)
353 /* maintains list of cpu model definitions
355 static x86_def_t
*x86_defs
= {NULL
};
357 /* built-in cpu model definitions (deprecated)
359 static x86_def_t builtin_x86_defs
[] = {
363 .vendor1
= CPUID_VENDOR_AMD_1
,
364 .vendor2
= CPUID_VENDOR_AMD_2
,
365 .vendor3
= CPUID_VENDOR_AMD_3
,
369 .features
= PPRO_FEATURES
|
370 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
372 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
| CPUID_EXT_POPCNT
,
373 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
374 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
375 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
376 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
377 .xlevel
= 0x8000000A,
382 .vendor1
= CPUID_VENDOR_AMD_1
,
383 .vendor2
= CPUID_VENDOR_AMD_2
,
384 .vendor3
= CPUID_VENDOR_AMD_3
,
388 .features
= PPRO_FEATURES
|
389 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
390 CPUID_PSE36
| CPUID_VME
| CPUID_HT
,
391 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_CX16
|
393 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
394 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
395 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
| CPUID_EXT2_MMXEXT
|
396 CPUID_EXT2_FFXSR
| CPUID_EXT2_PDPE1GB
| CPUID_EXT2_RDTSCP
,
397 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
399 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
400 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
401 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
402 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
403 .svm_features
= CPUID_SVM_NPT
| CPUID_SVM_LBRV
,
404 .xlevel
= 0x8000001A,
405 .model_id
= "AMD Phenom(tm) 9550 Quad-Core Processor"
413 .features
= PPRO_FEATURES
|
414 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
415 CPUID_PSE36
| CPUID_VME
| CPUID_DTS
| CPUID_ACPI
| CPUID_SS
|
416 CPUID_HT
| CPUID_TM
| CPUID_PBE
,
417 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
418 CPUID_EXT_DTES64
| CPUID_EXT_DSCPL
| CPUID_EXT_VMX
| CPUID_EXT_EST
|
419 CPUID_EXT_TM2
| CPUID_EXT_CX16
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
420 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
421 .ext3_features
= CPUID_EXT3_LAHF_LM
,
422 .xlevel
= 0x80000008,
423 .model_id
= "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
428 .vendor1
= CPUID_VENDOR_INTEL_1
,
429 .vendor2
= CPUID_VENDOR_INTEL_2
,
430 .vendor3
= CPUID_VENDOR_INTEL_3
,
434 /* Missing: CPUID_VME, CPUID_HT */
435 .features
= PPRO_FEATURES
|
436 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
438 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
439 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
,
440 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
441 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
442 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
443 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
444 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
445 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
446 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
448 .xlevel
= 0x80000008,
449 .model_id
= "Common KVM processor"
457 .features
= PPRO_FEATURES
,
458 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_POPCNT
,
459 .xlevel
= 0x80000004,
467 .features
= PPRO_FEATURES
|
468 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_PSE36
,
469 .ext_features
= CPUID_EXT_SSE3
,
470 .ext2_features
= PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
,
472 .xlevel
= 0x80000008,
473 .model_id
= "Common 32-bit KVM processor"
481 .features
= PPRO_FEATURES
| CPUID_VME
|
482 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_DTS
| CPUID_ACPI
|
483 CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
484 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_VMX
|
485 CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
| CPUID_EXT_PDCM
,
486 .ext2_features
= CPUID_EXT2_NX
,
487 .xlevel
= 0x80000008,
488 .model_id
= "Genuine Intel(R) CPU T2600 @ 2.16GHz",
496 .features
= I486_FEATURES
,
505 .features
= PENTIUM_FEATURES
,
514 .features
= PENTIUM2_FEATURES
,
523 .features
= PENTIUM3_FEATURES
,
529 .vendor1
= CPUID_VENDOR_AMD_1
,
530 .vendor2
= CPUID_VENDOR_AMD_2
,
531 .vendor3
= CPUID_VENDOR_AMD_3
,
535 .features
= PPRO_FEATURES
| CPUID_PSE36
| CPUID_VME
| CPUID_MTRR
|
537 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
538 CPUID_EXT2_MMXEXT
| CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
539 .xlevel
= 0x80000008,
543 /* original is on level 10 */
548 .features
= PPRO_FEATURES
|
549 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_VME
| CPUID_DTS
|
550 CPUID_ACPI
| CPUID_SS
| CPUID_HT
| CPUID_TM
| CPUID_PBE
,
551 /* Some CPUs got no CPUID_SEP */
552 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
|
553 CPUID_EXT_DSCPL
| CPUID_EXT_EST
| CPUID_EXT_TM2
| CPUID_EXT_XTPR
,
554 .ext2_features
= (PPRO_FEATURES
& CPUID_EXT2_AMD_ALIASES
) |
556 .ext3_features
= CPUID_EXT3_LAHF_LM
,
557 .xlevel
= 0x8000000A,
558 .model_id
= "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
563 .vendor1
= CPUID_VENDOR_INTEL_1
,
564 .vendor2
= CPUID_VENDOR_INTEL_2
,
565 .vendor3
= CPUID_VENDOR_INTEL_3
,
569 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
570 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
571 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
572 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
573 CPUID_DE
| CPUID_FP87
,
574 .ext_features
= CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
575 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
576 .ext3_features
= CPUID_EXT3_LAHF_LM
,
577 .xlevel
= 0x8000000A,
578 .model_id
= "Intel Celeron_4x0 (Conroe/Merom Class Core 2)",
583 .vendor1
= CPUID_VENDOR_INTEL_1
,
584 .vendor2
= CPUID_VENDOR_INTEL_2
,
585 .vendor3
= CPUID_VENDOR_INTEL_3
,
589 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
590 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
591 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
592 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
593 CPUID_DE
| CPUID_FP87
,
594 .ext_features
= CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
596 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_NX
| CPUID_EXT2_SYSCALL
,
597 .ext3_features
= CPUID_EXT3_LAHF_LM
,
598 .xlevel
= 0x8000000A,
599 .model_id
= "Intel Core 2 Duo P9xxx (Penryn Class Core 2)",
604 .vendor1
= CPUID_VENDOR_INTEL_1
,
605 .vendor2
= CPUID_VENDOR_INTEL_2
,
606 .vendor3
= CPUID_VENDOR_INTEL_3
,
610 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
611 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
612 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
613 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
614 CPUID_DE
| CPUID_FP87
,
615 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
616 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_SSE3
,
617 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
618 .ext3_features
= CPUID_EXT3_LAHF_LM
,
619 .xlevel
= 0x8000000A,
620 .model_id
= "Intel Core i7 9xx (Nehalem Class Core i7)",
625 .vendor1
= CPUID_VENDOR_INTEL_1
,
626 .vendor2
= CPUID_VENDOR_INTEL_2
,
627 .vendor3
= CPUID_VENDOR_INTEL_3
,
631 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
632 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
633 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
634 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
635 CPUID_DE
| CPUID_FP87
,
636 .ext_features
= CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
637 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
639 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
640 .ext3_features
= CPUID_EXT3_LAHF_LM
,
641 .xlevel
= 0x8000000A,
642 .model_id
= "Westmere E56xx/L56xx/X56xx (Nehalem-C)",
645 .name
= "SandyBridge",
647 .vendor1
= CPUID_VENDOR_INTEL_1
,
648 .vendor2
= CPUID_VENDOR_INTEL_2
,
649 .vendor3
= CPUID_VENDOR_INTEL_3
,
653 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
654 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
655 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
656 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
657 CPUID_DE
| CPUID_FP87
,
658 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
659 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_POPCNT
|
660 CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
661 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
663 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
665 .ext3_features
= CPUID_EXT3_LAHF_LM
,
666 .xlevel
= 0x8000000A,
667 .model_id
= "Intel Xeon E312xx (Sandy Bridge)",
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_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
684 CPUID_EXT_POPCNT
| CPUID_EXT_X2APIC
| CPUID_EXT_SSE42
|
685 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_SSSE3
|
686 CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
|
687 CPUID_EXT_TSC_DEADLINE_TIMER
| CPUID_EXT_FMA
| CPUID_EXT_MOVBE
|
689 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_NX
|
691 .ext3_features
= CPUID_EXT3_LAHF_LM
,
692 .cpuid_7_0_ebx_features
= CPUID_7_0_EBX_FSGSBASE
| CPUID_7_0_EBX_BMI1
|
693 CPUID_7_0_EBX_HLE
| CPUID_7_0_EBX_AVX2
| CPUID_7_0_EBX_SMEP
|
694 CPUID_7_0_EBX_BMI2
| CPUID_7_0_EBX_ERMS
| CPUID_7_0_EBX_INVPCID
|
696 .xlevel
= 0x8000000A,
697 .model_id
= "Intel Core Processor (Haswell)",
700 .name
= "Opteron_G1",
702 .vendor1
= CPUID_VENDOR_AMD_1
,
703 .vendor2
= CPUID_VENDOR_AMD_2
,
704 .vendor3
= CPUID_VENDOR_AMD_3
,
708 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
709 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
710 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
711 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
712 CPUID_DE
| CPUID_FP87
,
713 .ext_features
= CPUID_EXT_SSE3
,
714 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
715 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
716 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
717 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
718 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
719 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
720 .xlevel
= 0x80000008,
721 .model_id
= "AMD Opteron 240 (Gen 1 Class Opteron)",
724 .name
= "Opteron_G2",
726 .vendor1
= CPUID_VENDOR_AMD_1
,
727 .vendor2
= CPUID_VENDOR_AMD_2
,
728 .vendor3
= CPUID_VENDOR_AMD_3
,
732 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
733 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
734 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
735 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
736 CPUID_DE
| CPUID_FP87
,
737 .ext_features
= CPUID_EXT_CX16
| CPUID_EXT_SSE3
,
738 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
739 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
740 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
741 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
742 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
743 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
744 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
745 .ext3_features
= CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
746 .xlevel
= 0x80000008,
747 .model_id
= "AMD Opteron 22xx (Gen 2 Class Opteron)",
750 .name
= "Opteron_G3",
752 .vendor1
= CPUID_VENDOR_AMD_1
,
753 .vendor2
= CPUID_VENDOR_AMD_2
,
754 .vendor3
= CPUID_VENDOR_AMD_3
,
758 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
759 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
760 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
761 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
762 CPUID_DE
| CPUID_FP87
,
763 .ext_features
= CPUID_EXT_POPCNT
| CPUID_EXT_CX16
| CPUID_EXT_MONITOR
|
765 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
| CPUID_EXT2_FXSR
|
766 CPUID_EXT2_MMX
| CPUID_EXT2_NX
| CPUID_EXT2_PSE36
|
767 CPUID_EXT2_PAT
| CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
|
768 CPUID_EXT2_PGE
| CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
|
769 CPUID_EXT2_APIC
| CPUID_EXT2_CX8
| CPUID_EXT2_MCE
|
770 CPUID_EXT2_PAE
| CPUID_EXT2_MSR
| CPUID_EXT2_TSC
| CPUID_EXT2_PSE
|
771 CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
772 .ext3_features
= CPUID_EXT3_MISALIGNSSE
| CPUID_EXT3_SSE4A
|
773 CPUID_EXT3_ABM
| CPUID_EXT3_SVM
| CPUID_EXT3_LAHF_LM
,
774 .xlevel
= 0x80000008,
775 .model_id
= "AMD Opteron 23xx (Gen 3 Class Opteron)",
778 .name
= "Opteron_G4",
780 .vendor1
= CPUID_VENDOR_AMD_1
,
781 .vendor2
= CPUID_VENDOR_AMD_2
,
782 .vendor3
= CPUID_VENDOR_AMD_3
,
786 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
787 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
788 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
789 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
790 CPUID_DE
| CPUID_FP87
,
791 .ext_features
= CPUID_EXT_AVX
| CPUID_EXT_XSAVE
| CPUID_EXT_AES
|
792 CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
| CPUID_EXT_SSE41
|
793 CPUID_EXT_CX16
| CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
|
795 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
796 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
797 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
798 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
799 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
800 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
801 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
802 .ext3_features
= CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
803 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
804 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
806 .xlevel
= 0x8000001A,
807 .model_id
= "AMD Opteron 62xx class CPU",
810 .name
= "Opteron_G5",
812 .vendor1
= CPUID_VENDOR_AMD_1
,
813 .vendor2
= CPUID_VENDOR_AMD_2
,
814 .vendor3
= CPUID_VENDOR_AMD_3
,
818 .features
= CPUID_SSE2
| CPUID_SSE
| CPUID_FXSR
| CPUID_MMX
|
819 CPUID_CLFLUSH
| CPUID_PSE36
| CPUID_PAT
| CPUID_CMOV
| CPUID_MCA
|
820 CPUID_PGE
| CPUID_MTRR
| CPUID_SEP
| CPUID_APIC
| CPUID_CX8
|
821 CPUID_MCE
| CPUID_PAE
| CPUID_MSR
| CPUID_TSC
| CPUID_PSE
|
822 CPUID_DE
| CPUID_FP87
,
823 .ext_features
= CPUID_EXT_F16C
| CPUID_EXT_AVX
| CPUID_EXT_XSAVE
|
824 CPUID_EXT_AES
| CPUID_EXT_POPCNT
| CPUID_EXT_SSE42
|
825 CPUID_EXT_SSE41
| CPUID_EXT_CX16
| CPUID_EXT_FMA
|
826 CPUID_EXT_SSSE3
| CPUID_EXT_PCLMULQDQ
| CPUID_EXT_SSE3
,
827 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_RDTSCP
|
828 CPUID_EXT2_PDPE1GB
| CPUID_EXT2_FXSR
| CPUID_EXT2_MMX
|
829 CPUID_EXT2_NX
| CPUID_EXT2_PSE36
| CPUID_EXT2_PAT
|
830 CPUID_EXT2_CMOV
| CPUID_EXT2_MCA
| CPUID_EXT2_PGE
|
831 CPUID_EXT2_MTRR
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_APIC
|
832 CPUID_EXT2_CX8
| CPUID_EXT2_MCE
| CPUID_EXT2_PAE
| CPUID_EXT2_MSR
|
833 CPUID_EXT2_TSC
| CPUID_EXT2_PSE
| CPUID_EXT2_DE
| CPUID_EXT2_FPU
,
834 .ext3_features
= CPUID_EXT3_TBM
| CPUID_EXT3_FMA4
| CPUID_EXT3_XOP
|
835 CPUID_EXT3_3DNOWPREFETCH
| CPUID_EXT3_MISALIGNSSE
|
836 CPUID_EXT3_SSE4A
| CPUID_EXT3_ABM
| CPUID_EXT3_SVM
|
838 .xlevel
= 0x8000001A,
839 .model_id
= "AMD Opteron 63xx class CPU",
844 static int cpu_x86_fill_model_id(char *str
)
846 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
849 for (i
= 0; i
< 3; i
++) {
850 host_cpuid(0x80000002 + i
, 0, &eax
, &ebx
, &ecx
, &edx
);
851 memcpy(str
+ i
* 16 + 0, &eax
, 4);
852 memcpy(str
+ i
* 16 + 4, &ebx
, 4);
853 memcpy(str
+ i
* 16 + 8, &ecx
, 4);
854 memcpy(str
+ i
* 16 + 12, &edx
, 4);
860 /* Fill a x86_def_t struct with information about the host CPU, and
861 * the CPU features supported by the host hardware + host kernel
863 * This function may be called only if KVM is enabled.
865 static void kvm_cpu_fill_host(x86_def_t
*x86_cpu_def
)
868 KVMState
*s
= kvm_state
;
869 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
871 assert(kvm_enabled());
873 x86_cpu_def
->name
= "host";
874 host_cpuid(0x0, 0, &eax
, &ebx
, &ecx
, &edx
);
875 x86_cpu_def
->vendor1
= ebx
;
876 x86_cpu_def
->vendor2
= edx
;
877 x86_cpu_def
->vendor3
= ecx
;
879 host_cpuid(0x1, 0, &eax
, &ebx
, &ecx
, &edx
);
880 x86_cpu_def
->family
= ((eax
>> 8) & 0x0F) + ((eax
>> 20) & 0xFF);
881 x86_cpu_def
->model
= ((eax
>> 4) & 0x0F) | ((eax
& 0xF0000) >> 12);
882 x86_cpu_def
->stepping
= eax
& 0x0F;
884 x86_cpu_def
->level
= kvm_arch_get_supported_cpuid(s
, 0x0, 0, R_EAX
);
885 x86_cpu_def
->features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_EDX
);
886 x86_cpu_def
->ext_features
= kvm_arch_get_supported_cpuid(s
, 0x1, 0, R_ECX
);
888 if (x86_cpu_def
->level
>= 7) {
889 x86_cpu_def
->cpuid_7_0_ebx_features
=
890 kvm_arch_get_supported_cpuid(s
, 0x7, 0, R_EBX
);
892 x86_cpu_def
->cpuid_7_0_ebx_features
= 0;
895 x86_cpu_def
->xlevel
= kvm_arch_get_supported_cpuid(s
, 0x80000000, 0, R_EAX
);
896 x86_cpu_def
->ext2_features
=
897 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
898 x86_cpu_def
->ext3_features
=
899 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
901 cpu_x86_fill_model_id(x86_cpu_def
->model_id
);
902 x86_cpu_def
->vendor_override
= 0;
904 /* Call Centaur's CPUID instruction. */
905 if (x86_cpu_def
->vendor1
== CPUID_VENDOR_VIA_1
&&
906 x86_cpu_def
->vendor2
== CPUID_VENDOR_VIA_2
&&
907 x86_cpu_def
->vendor3
== CPUID_VENDOR_VIA_3
) {
908 host_cpuid(0xC0000000, 0, &eax
, &ebx
, &ecx
, &edx
);
909 eax
= kvm_arch_get_supported_cpuid(s
, 0xC0000000, 0, R_EAX
);
910 if (eax
>= 0xC0000001) {
911 /* Support VIA max extended level */
912 x86_cpu_def
->xlevel2
= eax
;
913 host_cpuid(0xC0000001, 0, &eax
, &ebx
, &ecx
, &edx
);
914 x86_cpu_def
->ext4_features
=
915 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
919 /* Other KVM-specific feature fields: */
920 x86_cpu_def
->svm_features
=
921 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
922 x86_cpu_def
->kvm_features
=
923 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
925 #endif /* CONFIG_KVM */
928 static int unavailable_host_feature(struct model_features_t
*f
, uint32_t mask
)
932 for (i
= 0; i
< 32; ++i
)
934 const char *reg
= get_register_name_32(f
->reg
);
936 fprintf(stderr
, "warning: host doesn't support requested feature: "
937 "CPUID.%02XH:%s%s%s [bit %d]\n",
939 f
->flag_names
[i
] ? "." : "",
940 f
->flag_names
[i
] ? f
->flag_names
[i
] : "", i
);
946 /* best effort attempt to inform user requested cpu flags aren't making
947 * their way to the guest.
949 * This function may be called only if KVM is enabled.
951 static int kvm_check_features_against_host(x86_def_t
*guest_def
)
956 struct model_features_t ft
[] = {
957 {&guest_def
->features
, &host_def
.features
,
958 feature_name
, 0x00000001, R_EDX
},
959 {&guest_def
->ext_features
, &host_def
.ext_features
,
960 ext_feature_name
, 0x00000001, R_ECX
},
961 {&guest_def
->ext2_features
, &host_def
.ext2_features
,
962 ext2_feature_name
, 0x80000001, R_EDX
},
963 {&guest_def
->ext3_features
, &host_def
.ext3_features
,
964 ext3_feature_name
, 0x80000001, R_ECX
}
967 assert(kvm_enabled());
969 kvm_cpu_fill_host(&host_def
);
970 for (rv
= 0, i
= 0; i
< ARRAY_SIZE(ft
); ++i
)
971 for (mask
= 1; mask
; mask
<<= 1)
972 if (*ft
[i
].guest_feat
& mask
&&
973 !(*ft
[i
].host_feat
& mask
)) {
974 unavailable_host_feature(&ft
[i
], mask
);
980 static void x86_cpuid_version_get_family(Object
*obj
, Visitor
*v
, void *opaque
,
981 const char *name
, Error
**errp
)
983 X86CPU
*cpu
= X86_CPU(obj
);
984 CPUX86State
*env
= &cpu
->env
;
987 value
= (env
->cpuid_version
>> 8) & 0xf;
989 value
+= (env
->cpuid_version
>> 20) & 0xff;
991 visit_type_int(v
, &value
, name
, errp
);
994 static void x86_cpuid_version_set_family(Object
*obj
, Visitor
*v
, void *opaque
,
995 const char *name
, Error
**errp
)
997 X86CPU
*cpu
= X86_CPU(obj
);
998 CPUX86State
*env
= &cpu
->env
;
999 const int64_t min
= 0;
1000 const int64_t max
= 0xff + 0xf;
1003 visit_type_int(v
, &value
, name
, errp
);
1004 if (error_is_set(errp
)) {
1007 if (value
< min
|| value
> max
) {
1008 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1009 name
? name
: "null", value
, min
, max
);
1013 env
->cpuid_version
&= ~0xff00f00;
1015 env
->cpuid_version
|= 0xf00 | ((value
- 0x0f) << 20);
1017 env
->cpuid_version
|= value
<< 8;
1021 static void x86_cpuid_version_get_model(Object
*obj
, Visitor
*v
, void *opaque
,
1022 const char *name
, Error
**errp
)
1024 X86CPU
*cpu
= X86_CPU(obj
);
1025 CPUX86State
*env
= &cpu
->env
;
1028 value
= (env
->cpuid_version
>> 4) & 0xf;
1029 value
|= ((env
->cpuid_version
>> 16) & 0xf) << 4;
1030 visit_type_int(v
, &value
, name
, errp
);
1033 static void x86_cpuid_version_set_model(Object
*obj
, Visitor
*v
, void *opaque
,
1034 const char *name
, Error
**errp
)
1036 X86CPU
*cpu
= X86_CPU(obj
);
1037 CPUX86State
*env
= &cpu
->env
;
1038 const int64_t min
= 0;
1039 const int64_t max
= 0xff;
1042 visit_type_int(v
, &value
, name
, errp
);
1043 if (error_is_set(errp
)) {
1046 if (value
< min
|| value
> max
) {
1047 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1048 name
? name
: "null", value
, min
, max
);
1052 env
->cpuid_version
&= ~0xf00f0;
1053 env
->cpuid_version
|= ((value
& 0xf) << 4) | ((value
>> 4) << 16);
1056 static void x86_cpuid_version_get_stepping(Object
*obj
, Visitor
*v
,
1057 void *opaque
, const char *name
,
1060 X86CPU
*cpu
= X86_CPU(obj
);
1061 CPUX86State
*env
= &cpu
->env
;
1064 value
= env
->cpuid_version
& 0xf;
1065 visit_type_int(v
, &value
, name
, errp
);
1068 static void x86_cpuid_version_set_stepping(Object
*obj
, Visitor
*v
,
1069 void *opaque
, const char *name
,
1072 X86CPU
*cpu
= X86_CPU(obj
);
1073 CPUX86State
*env
= &cpu
->env
;
1074 const int64_t min
= 0;
1075 const int64_t max
= 0xf;
1078 visit_type_int(v
, &value
, name
, errp
);
1079 if (error_is_set(errp
)) {
1082 if (value
< min
|| value
> max
) {
1083 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1084 name
? name
: "null", value
, min
, max
);
1088 env
->cpuid_version
&= ~0xf;
1089 env
->cpuid_version
|= value
& 0xf;
1092 static void x86_cpuid_get_level(Object
*obj
, Visitor
*v
, void *opaque
,
1093 const char *name
, Error
**errp
)
1095 X86CPU
*cpu
= X86_CPU(obj
);
1097 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1100 static void x86_cpuid_set_level(Object
*obj
, Visitor
*v
, void *opaque
,
1101 const char *name
, Error
**errp
)
1103 X86CPU
*cpu
= X86_CPU(obj
);
1105 visit_type_uint32(v
, &cpu
->env
.cpuid_level
, name
, errp
);
1108 static void x86_cpuid_get_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1109 const char *name
, Error
**errp
)
1111 X86CPU
*cpu
= X86_CPU(obj
);
1113 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1116 static void x86_cpuid_set_xlevel(Object
*obj
, Visitor
*v
, void *opaque
,
1117 const char *name
, Error
**errp
)
1119 X86CPU
*cpu
= X86_CPU(obj
);
1121 visit_type_uint32(v
, &cpu
->env
.cpuid_xlevel
, name
, errp
);
1124 static char *x86_cpuid_get_vendor(Object
*obj
, Error
**errp
)
1126 X86CPU
*cpu
= X86_CPU(obj
);
1127 CPUX86State
*env
= &cpu
->env
;
1131 value
= (char *)g_malloc(CPUID_VENDOR_SZ
+ 1);
1132 for (i
= 0; i
< 4; i
++) {
1133 value
[i
] = env
->cpuid_vendor1
>> (8 * i
);
1134 value
[i
+ 4] = env
->cpuid_vendor2
>> (8 * i
);
1135 value
[i
+ 8] = env
->cpuid_vendor3
>> (8 * i
);
1137 value
[CPUID_VENDOR_SZ
] = '\0';
1141 static void x86_cpuid_set_vendor(Object
*obj
, const char *value
,
1144 X86CPU
*cpu
= X86_CPU(obj
);
1145 CPUX86State
*env
= &cpu
->env
;
1148 if (strlen(value
) != CPUID_VENDOR_SZ
) {
1149 error_set(errp
, QERR_PROPERTY_VALUE_BAD
, "",
1154 env
->cpuid_vendor1
= 0;
1155 env
->cpuid_vendor2
= 0;
1156 env
->cpuid_vendor3
= 0;
1157 for (i
= 0; i
< 4; i
++) {
1158 env
->cpuid_vendor1
|= ((uint8_t)value
[i
]) << (8 * i
);
1159 env
->cpuid_vendor2
|= ((uint8_t)value
[i
+ 4]) << (8 * i
);
1160 env
->cpuid_vendor3
|= ((uint8_t)value
[i
+ 8]) << (8 * i
);
1162 env
->cpuid_vendor_override
= 1;
1165 static char *x86_cpuid_get_model_id(Object
*obj
, Error
**errp
)
1167 X86CPU
*cpu
= X86_CPU(obj
);
1168 CPUX86State
*env
= &cpu
->env
;
1172 value
= g_malloc(48 + 1);
1173 for (i
= 0; i
< 48; i
++) {
1174 value
[i
] = env
->cpuid_model
[i
>> 2] >> (8 * (i
& 3));
1180 static void x86_cpuid_set_model_id(Object
*obj
, const char *model_id
,
1183 X86CPU
*cpu
= X86_CPU(obj
);
1184 CPUX86State
*env
= &cpu
->env
;
1187 if (model_id
== NULL
) {
1190 len
= strlen(model_id
);
1191 memset(env
->cpuid_model
, 0, 48);
1192 for (i
= 0; i
< 48; i
++) {
1196 c
= (uint8_t)model_id
[i
];
1198 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
1202 static void x86_cpuid_get_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1203 const char *name
, Error
**errp
)
1205 X86CPU
*cpu
= X86_CPU(obj
);
1208 value
= cpu
->env
.tsc_khz
* 1000;
1209 visit_type_int(v
, &value
, name
, errp
);
1212 static void x86_cpuid_set_tsc_freq(Object
*obj
, Visitor
*v
, void *opaque
,
1213 const char *name
, Error
**errp
)
1215 X86CPU
*cpu
= X86_CPU(obj
);
1216 const int64_t min
= 0;
1217 const int64_t max
= INT64_MAX
;
1220 visit_type_int(v
, &value
, name
, errp
);
1221 if (error_is_set(errp
)) {
1224 if (value
< min
|| value
> max
) {
1225 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
, "",
1226 name
? name
: "null", value
, min
, max
);
1230 cpu
->env
.tsc_khz
= value
/ 1000;
1233 static int cpu_x86_find_by_name(x86_def_t
*x86_cpu_def
, const char *name
)
1237 for (def
= x86_defs
; def
; def
= def
->next
) {
1238 if (name
&& !strcmp(name
, def
->name
)) {
1242 if (kvm_enabled() && name
&& strcmp(name
, "host") == 0) {
1243 kvm_cpu_fill_host(x86_cpu_def
);
1247 memcpy(x86_cpu_def
, def
, sizeof(*def
));
1253 /* Parse "+feature,-feature,feature=foo" CPU feature string
1255 static int cpu_x86_parse_featurestr(x86_def_t
*x86_cpu_def
, char *features
)
1258 char *featurestr
; /* Single 'key=value" string being parsed */
1259 /* Features to be added */
1260 uint32_t plus_features
= 0, plus_ext_features
= 0;
1261 uint32_t plus_ext2_features
= 0, plus_ext3_features
= 0;
1262 uint32_t plus_kvm_features
= kvm_default_features
, plus_svm_features
= 0;
1263 uint32_t plus_7_0_ebx_features
= 0;
1264 /* Features to be removed */
1265 uint32_t minus_features
= 0, minus_ext_features
= 0;
1266 uint32_t minus_ext2_features
= 0, minus_ext3_features
= 0;
1267 uint32_t minus_kvm_features
= 0, minus_svm_features
= 0;
1268 uint32_t minus_7_0_ebx_features
= 0;
1271 add_flagname_to_bitmaps("hypervisor", &plus_features
,
1272 &plus_ext_features
, &plus_ext2_features
, &plus_ext3_features
,
1273 &plus_kvm_features
, &plus_svm_features
, &plus_7_0_ebx_features
);
1275 featurestr
= features
? strtok(features
, ",") : NULL
;
1277 while (featurestr
) {
1279 if (featurestr
[0] == '+') {
1280 add_flagname_to_bitmaps(featurestr
+ 1, &plus_features
,
1281 &plus_ext_features
, &plus_ext2_features
,
1282 &plus_ext3_features
, &plus_kvm_features
,
1283 &plus_svm_features
, &plus_7_0_ebx_features
);
1284 } else if (featurestr
[0] == '-') {
1285 add_flagname_to_bitmaps(featurestr
+ 1, &minus_features
,
1286 &minus_ext_features
, &minus_ext2_features
,
1287 &minus_ext3_features
, &minus_kvm_features
,
1288 &minus_svm_features
, &minus_7_0_ebx_features
);
1289 } else if ((val
= strchr(featurestr
, '='))) {
1291 if (!strcmp(featurestr
, "family")) {
1293 numvalue
= strtoul(val
, &err
, 0);
1294 if (!*val
|| *err
|| numvalue
> 0xff + 0xf) {
1295 fprintf(stderr
, "bad numerical value %s\n", val
);
1298 x86_cpu_def
->family
= numvalue
;
1299 } else if (!strcmp(featurestr
, "model")) {
1301 numvalue
= strtoul(val
, &err
, 0);
1302 if (!*val
|| *err
|| numvalue
> 0xff) {
1303 fprintf(stderr
, "bad numerical value %s\n", val
);
1306 x86_cpu_def
->model
= numvalue
;
1307 } else if (!strcmp(featurestr
, "stepping")) {
1309 numvalue
= strtoul(val
, &err
, 0);
1310 if (!*val
|| *err
|| numvalue
> 0xf) {
1311 fprintf(stderr
, "bad numerical value %s\n", val
);
1314 x86_cpu_def
->stepping
= numvalue
;
1315 } else if (!strcmp(featurestr
, "level")) {
1317 numvalue
= strtoul(val
, &err
, 0);
1318 if (!*val
|| *err
) {
1319 fprintf(stderr
, "bad numerical value %s\n", val
);
1322 x86_cpu_def
->level
= numvalue
;
1323 } else if (!strcmp(featurestr
, "xlevel")) {
1325 numvalue
= strtoul(val
, &err
, 0);
1326 if (!*val
|| *err
) {
1327 fprintf(stderr
, "bad numerical value %s\n", val
);
1330 if (numvalue
< 0x80000000) {
1331 numvalue
+= 0x80000000;
1333 x86_cpu_def
->xlevel
= numvalue
;
1334 } else if (!strcmp(featurestr
, "vendor")) {
1335 if (strlen(val
) != 12) {
1336 fprintf(stderr
, "vendor string must be 12 chars long\n");
1339 x86_cpu_def
->vendor1
= 0;
1340 x86_cpu_def
->vendor2
= 0;
1341 x86_cpu_def
->vendor3
= 0;
1342 for(i
= 0; i
< 4; i
++) {
1343 x86_cpu_def
->vendor1
|= ((uint8_t)val
[i
]) << (8 * i
);
1344 x86_cpu_def
->vendor2
|= ((uint8_t)val
[i
+ 4]) << (8 * i
);
1345 x86_cpu_def
->vendor3
|= ((uint8_t)val
[i
+ 8]) << (8 * i
);
1347 x86_cpu_def
->vendor_override
= 1;
1348 } else if (!strcmp(featurestr
, "model_id")) {
1349 pstrcpy(x86_cpu_def
->model_id
, sizeof(x86_cpu_def
->model_id
),
1351 } else if (!strcmp(featurestr
, "tsc_freq")) {
1355 tsc_freq
= strtosz_suffix_unit(val
, &err
,
1356 STRTOSZ_DEFSUFFIX_B
, 1000);
1357 if (tsc_freq
< 0 || *err
) {
1358 fprintf(stderr
, "bad numerical value %s\n", val
);
1361 x86_cpu_def
->tsc_khz
= tsc_freq
/ 1000;
1362 } else if (!strcmp(featurestr
, "hv_spinlocks")) {
1364 numvalue
= strtoul(val
, &err
, 0);
1365 if (!*val
|| *err
) {
1366 fprintf(stderr
, "bad numerical value %s\n", val
);
1369 hyperv_set_spinlock_retries(numvalue
);
1371 fprintf(stderr
, "unrecognized feature %s\n", featurestr
);
1374 } else if (!strcmp(featurestr
, "check")) {
1376 } else if (!strcmp(featurestr
, "enforce")) {
1377 check_cpuid
= enforce_cpuid
= 1;
1378 } else if (!strcmp(featurestr
, "hv_relaxed")) {
1379 hyperv_enable_relaxed_timing(true);
1380 } else if (!strcmp(featurestr
, "hv_vapic")) {
1381 hyperv_enable_vapic_recommended(true);
1383 fprintf(stderr
, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr
);
1386 featurestr
= strtok(NULL
, ",");
1388 x86_cpu_def
->features
|= plus_features
;
1389 x86_cpu_def
->ext_features
|= plus_ext_features
;
1390 x86_cpu_def
->ext2_features
|= plus_ext2_features
;
1391 x86_cpu_def
->ext3_features
|= plus_ext3_features
;
1392 x86_cpu_def
->kvm_features
|= plus_kvm_features
;
1393 x86_cpu_def
->svm_features
|= plus_svm_features
;
1394 x86_cpu_def
->cpuid_7_0_ebx_features
|= plus_7_0_ebx_features
;
1395 x86_cpu_def
->features
&= ~minus_features
;
1396 x86_cpu_def
->ext_features
&= ~minus_ext_features
;
1397 x86_cpu_def
->ext2_features
&= ~minus_ext2_features
;
1398 x86_cpu_def
->ext3_features
&= ~minus_ext3_features
;
1399 x86_cpu_def
->kvm_features
&= ~minus_kvm_features
;
1400 x86_cpu_def
->svm_features
&= ~minus_svm_features
;
1401 x86_cpu_def
->cpuid_7_0_ebx_features
&= ~minus_7_0_ebx_features
;
1402 if (check_cpuid
&& kvm_enabled()) {
1403 if (kvm_check_features_against_host(x86_cpu_def
) && enforce_cpuid
)
1412 /* generate a composite string into buf of all cpuid names in featureset
1413 * selected by fbits. indicate truncation at bufsize in the event of overflow.
1414 * if flags, suppress names undefined in featureset.
1416 static void listflags(char *buf
, int bufsize
, uint32_t fbits
,
1417 const char **featureset
, uint32_t flags
)
1419 const char **p
= &featureset
[31];
1423 b
= 4 <= bufsize
? buf
+ (bufsize
-= 3) - 1 : NULL
;
1425 for (q
= buf
, bit
= 31; fbits
&& bufsize
; --p
, fbits
&= ~(1 << bit
), --bit
)
1426 if (fbits
& 1 << bit
&& (*p
|| !flags
)) {
1428 nc
= snprintf(q
, bufsize
, "%s%s", q
== buf
? "" : " ", *p
);
1430 nc
= snprintf(q
, bufsize
, "%s[%d]", q
== buf
? "" : " ", bit
);
1431 if (bufsize
<= nc
) {
1433 memcpy(b
, "...", sizeof("..."));
1442 /* generate CPU information. */
1443 void x86_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
1448 for (def
= x86_defs
; def
; def
= def
->next
) {
1449 snprintf(buf
, sizeof(buf
), "%s", def
->name
);
1450 (*cpu_fprintf
)(f
, "x86 %16s %-48s\n", buf
, def
->model_id
);
1452 if (kvm_enabled()) {
1453 (*cpu_fprintf
)(f
, "x86 %16s\n", "[host]");
1455 (*cpu_fprintf
)(f
, "\nRecognized CPUID flags:\n");
1456 listflags(buf
, sizeof(buf
), (uint32_t)~0, feature_name
, 1);
1457 (*cpu_fprintf
)(f
, " %s\n", buf
);
1458 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext_feature_name
, 1);
1459 (*cpu_fprintf
)(f
, " %s\n", buf
);
1460 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext2_feature_name
, 1);
1461 (*cpu_fprintf
)(f
, " %s\n", buf
);
1462 listflags(buf
, sizeof(buf
), (uint32_t)~0, ext3_feature_name
, 1);
1463 (*cpu_fprintf
)(f
, " %s\n", buf
);
1466 CpuDefinitionInfoList
*arch_query_cpu_definitions(Error
**errp
)
1468 CpuDefinitionInfoList
*cpu_list
= NULL
;
1471 for (def
= x86_defs
; def
; def
= def
->next
) {
1472 CpuDefinitionInfoList
*entry
;
1473 CpuDefinitionInfo
*info
;
1475 info
= g_malloc0(sizeof(*info
));
1476 info
->name
= g_strdup(def
->name
);
1478 entry
= g_malloc0(sizeof(*entry
));
1479 entry
->value
= info
;
1480 entry
->next
= cpu_list
;
1488 static void filter_features_for_kvm(X86CPU
*cpu
)
1490 CPUX86State
*env
= &cpu
->env
;
1491 KVMState
*s
= kvm_state
;
1493 env
->cpuid_features
&=
1494 kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
1495 env
->cpuid_ext_features
&=
1496 kvm_arch_get_supported_cpuid(s
, 1, 0, R_ECX
);
1497 env
->cpuid_ext2_features
&=
1498 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
);
1499 env
->cpuid_ext3_features
&=
1500 kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_ECX
);
1501 env
->cpuid_svm_features
&=
1502 kvm_arch_get_supported_cpuid(s
, 0x8000000A, 0, R_EDX
);
1503 env
->cpuid_7_0_ebx_features
&=
1504 kvm_arch_get_supported_cpuid(s
, 7, 0, R_EBX
);
1505 env
->cpuid_kvm_features
&=
1506 kvm_arch_get_supported_cpuid(s
, KVM_CPUID_FEATURES
, 0, R_EAX
);
1507 env
->cpuid_ext4_features
&=
1508 kvm_arch_get_supported_cpuid(s
, 0xC0000001, 0, R_EDX
);
1513 int cpu_x86_register(X86CPU
*cpu
, const char *cpu_model
)
1515 CPUX86State
*env
= &cpu
->env
;
1516 x86_def_t def1
, *def
= &def1
;
1517 Error
*error
= NULL
;
1518 char *name
, *features
;
1519 gchar
**model_pieces
;
1521 memset(def
, 0, sizeof(*def
));
1523 model_pieces
= g_strsplit(cpu_model
, ",", 2);
1524 if (!model_pieces
[0]) {
1527 name
= model_pieces
[0];
1528 features
= model_pieces
[1];
1530 if (cpu_x86_find_by_name(def
, name
) < 0) {
1534 if (cpu_x86_parse_featurestr(def
, features
) < 0) {
1538 env
->cpuid_vendor1
= def
->vendor1
;
1539 env
->cpuid_vendor2
= def
->vendor2
;
1540 env
->cpuid_vendor3
= def
->vendor3
;
1542 env
->cpuid_vendor1
= CPUID_VENDOR_INTEL_1
;
1543 env
->cpuid_vendor2
= CPUID_VENDOR_INTEL_2
;
1544 env
->cpuid_vendor3
= CPUID_VENDOR_INTEL_3
;
1546 env
->cpuid_vendor_override
= def
->vendor_override
;
1547 object_property_set_int(OBJECT(cpu
), def
->level
, "level", &error
);
1548 object_property_set_int(OBJECT(cpu
), def
->family
, "family", &error
);
1549 object_property_set_int(OBJECT(cpu
), def
->model
, "model", &error
);
1550 object_property_set_int(OBJECT(cpu
), def
->stepping
, "stepping", &error
);
1551 env
->cpuid_features
= def
->features
;
1552 env
->cpuid_ext_features
= def
->ext_features
;
1553 env
->cpuid_ext2_features
= def
->ext2_features
;
1554 env
->cpuid_ext3_features
= def
->ext3_features
;
1555 object_property_set_int(OBJECT(cpu
), def
->xlevel
, "xlevel", &error
);
1556 env
->cpuid_kvm_features
= def
->kvm_features
;
1557 env
->cpuid_svm_features
= def
->svm_features
;
1558 env
->cpuid_ext4_features
= def
->ext4_features
;
1559 env
->cpuid_7_0_ebx_features
= def
->cpuid_7_0_ebx_features
;
1560 env
->cpuid_xlevel2
= def
->xlevel2
;
1561 object_property_set_int(OBJECT(cpu
), (int64_t)def
->tsc_khz
* 1000,
1562 "tsc-frequency", &error
);
1564 /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on
1567 if (env
->cpuid_vendor1
== CPUID_VENDOR_AMD_1
&&
1568 env
->cpuid_vendor2
== CPUID_VENDOR_AMD_2
&&
1569 env
->cpuid_vendor3
== CPUID_VENDOR_AMD_3
) {
1570 env
->cpuid_ext2_features
&= ~CPUID_EXT2_AMD_ALIASES
;
1571 env
->cpuid_ext2_features
|= (def
->features
& CPUID_EXT2_AMD_ALIASES
);
1574 if (!kvm_enabled()) {
1575 env
->cpuid_features
&= TCG_FEATURES
;
1576 env
->cpuid_ext_features
&= TCG_EXT_FEATURES
;
1577 env
->cpuid_ext2_features
&= (TCG_EXT2_FEATURES
1578 #ifdef TARGET_X86_64
1579 | CPUID_EXT2_SYSCALL
| CPUID_EXT2_LM
1582 env
->cpuid_ext3_features
&= TCG_EXT3_FEATURES
;
1583 env
->cpuid_svm_features
&= TCG_SVM_FEATURES
;
1586 filter_features_for_kvm(cpu
);
1589 object_property_set_str(OBJECT(cpu
), def
->model_id
, "model-id", &error
);
1591 fprintf(stderr
, "%s\n", error_get_pretty(error
));
1596 g_strfreev(model_pieces
);
1599 g_strfreev(model_pieces
);
1603 #if !defined(CONFIG_USER_ONLY)
1605 void cpu_clear_apic_feature(CPUX86State
*env
)
1607 env
->cpuid_features
&= ~CPUID_APIC
;
1610 #endif /* !CONFIG_USER_ONLY */
1612 /* Initialize list of CPU models, filling some non-static fields if necessary
1614 void x86_cpudef_setup(void)
1617 static const char *model_with_versions
[] = { "qemu32", "qemu64", "athlon" };
1619 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); ++i
) {
1620 x86_def_t
*def
= &builtin_x86_defs
[i
];
1621 def
->next
= x86_defs
;
1623 /* Look for specific "cpudef" models that */
1624 /* have the QEMU version in .model_id */
1625 for (j
= 0; j
< ARRAY_SIZE(model_with_versions
); j
++) {
1626 if (strcmp(model_with_versions
[j
], def
->name
) == 0) {
1627 pstrcpy(def
->model_id
, sizeof(def
->model_id
),
1628 "QEMU Virtual CPU version ");
1629 pstrcat(def
->model_id
, sizeof(def
->model_id
),
1630 qemu_get_version());
1639 static void get_cpuid_vendor(CPUX86State
*env
, uint32_t *ebx
,
1640 uint32_t *ecx
, uint32_t *edx
)
1642 *ebx
= env
->cpuid_vendor1
;
1643 *edx
= env
->cpuid_vendor2
;
1644 *ecx
= env
->cpuid_vendor3
;
1646 /* sysenter isn't supported on compatibility mode on AMD, syscall
1647 * isn't supported in compatibility mode on Intel.
1648 * Normally we advertise the actual cpu vendor, but you can override
1649 * this if you want to use KVM's sysenter/syscall emulation
1650 * in compatibility mode and when doing cross vendor migration
1652 if (kvm_enabled() && ! env
->cpuid_vendor_override
) {
1653 host_cpuid(0, 0, NULL
, ebx
, ecx
, edx
);
1657 void cpu_x86_cpuid(CPUX86State
*env
, uint32_t index
, uint32_t count
,
1658 uint32_t *eax
, uint32_t *ebx
,
1659 uint32_t *ecx
, uint32_t *edx
)
1661 X86CPU
*cpu
= x86_env_get_cpu(env
);
1662 CPUState
*cs
= CPU(cpu
);
1664 /* test if maximum index reached */
1665 if (index
& 0x80000000) {
1666 if (index
> env
->cpuid_xlevel
) {
1667 if (env
->cpuid_xlevel2
> 0) {
1668 /* Handle the Centaur's CPUID instruction. */
1669 if (index
> env
->cpuid_xlevel2
) {
1670 index
= env
->cpuid_xlevel2
;
1671 } else if (index
< 0xC0000000) {
1672 index
= env
->cpuid_xlevel
;
1675 /* Intel documentation states that invalid EAX input will
1676 * return the same information as EAX=cpuid_level
1677 * (Intel SDM Vol. 2A - Instruction Set Reference - CPUID)
1679 index
= env
->cpuid_level
;
1683 if (index
> env
->cpuid_level
)
1684 index
= env
->cpuid_level
;
1689 *eax
= env
->cpuid_level
;
1690 get_cpuid_vendor(env
, ebx
, ecx
, edx
);
1693 *eax
= env
->cpuid_version
;
1694 *ebx
= (env
->cpuid_apic_id
<< 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1695 *ecx
= env
->cpuid_ext_features
;
1696 *edx
= env
->cpuid_features
;
1697 if (env
->nr_cores
* env
->nr_threads
> 1) {
1698 *ebx
|= (env
->nr_cores
* env
->nr_threads
) << 16;
1699 *edx
|= 1 << 28; /* HTT bit */
1703 /* cache info: needed for Pentium Pro compatibility */
1710 /* cache info: needed for Core compatibility */
1711 if (env
->nr_cores
> 1) {
1712 *eax
= (env
->nr_cores
- 1) << 26;
1717 case 0: /* L1 dcache info */
1723 case 1: /* L1 icache info */
1729 case 2: /* L2 cache info */
1731 if (env
->nr_threads
> 1) {
1732 *eax
|= (env
->nr_threads
- 1) << 14;
1738 default: /* end of info */
1747 /* mwait info: needed for Core compatibility */
1748 *eax
= 0; /* Smallest monitor-line size in bytes */
1749 *ebx
= 0; /* Largest monitor-line size in bytes */
1750 *ecx
= CPUID_MWAIT_EMX
| CPUID_MWAIT_IBE
;
1754 /* Thermal and Power Leaf */
1761 /* Structured Extended Feature Flags Enumeration Leaf */
1763 *eax
= 0; /* Maximum ECX value for sub-leaves */
1764 *ebx
= env
->cpuid_7_0_ebx_features
; /* Feature flags */
1765 *ecx
= 0; /* Reserved */
1766 *edx
= 0; /* Reserved */
1775 /* Direct Cache Access Information Leaf */
1776 *eax
= 0; /* Bits 0-31 in DCA_CAP MSR */
1782 /* Architectural Performance Monitoring Leaf */
1783 if (kvm_enabled()) {
1784 KVMState
*s
= cs
->kvm_state
;
1786 *eax
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EAX
);
1787 *ebx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EBX
);
1788 *ecx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_ECX
);
1789 *edx
= kvm_arch_get_supported_cpuid(s
, 0xA, count
, R_EDX
);
1798 /* Processor Extended State */
1799 if (!(env
->cpuid_ext_features
& CPUID_EXT_XSAVE
)) {
1806 if (kvm_enabled()) {
1807 KVMState
*s
= cs
->kvm_state
;
1809 *eax
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EAX
);
1810 *ebx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EBX
);
1811 *ecx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_ECX
);
1812 *edx
= kvm_arch_get_supported_cpuid(s
, 0xd, count
, R_EDX
);
1821 *eax
= env
->cpuid_xlevel
;
1822 *ebx
= env
->cpuid_vendor1
;
1823 *edx
= env
->cpuid_vendor2
;
1824 *ecx
= env
->cpuid_vendor3
;
1827 *eax
= env
->cpuid_version
;
1829 *ecx
= env
->cpuid_ext3_features
;
1830 *edx
= env
->cpuid_ext2_features
;
1832 /* The Linux kernel checks for the CMPLegacy bit and
1833 * discards multiple thread information if it is set.
1834 * So dont set it here for Intel to make Linux guests happy.
1836 if (env
->nr_cores
* env
->nr_threads
> 1) {
1837 uint32_t tebx
, tecx
, tedx
;
1838 get_cpuid_vendor(env
, &tebx
, &tecx
, &tedx
);
1839 if (tebx
!= CPUID_VENDOR_INTEL_1
||
1840 tedx
!= CPUID_VENDOR_INTEL_2
||
1841 tecx
!= CPUID_VENDOR_INTEL_3
) {
1842 *ecx
|= 1 << 1; /* CmpLegacy bit */
1849 *eax
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 0];
1850 *ebx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 1];
1851 *ecx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 2];
1852 *edx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 3];
1855 /* cache info (L1 cache) */
1862 /* cache info (L2 cache) */
1869 /* virtual & phys address size in low 2 bytes. */
1870 /* XXX: This value must match the one used in the MMU code. */
1871 if (env
->cpuid_ext2_features
& CPUID_EXT2_LM
) {
1872 /* 64 bit processor */
1873 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1874 *eax
= 0x00003028; /* 48 bits virtual, 40 bits physical */
1876 if (env
->cpuid_features
& CPUID_PSE36
)
1877 *eax
= 0x00000024; /* 36 bits physical */
1879 *eax
= 0x00000020; /* 32 bits physical */
1884 if (env
->nr_cores
* env
->nr_threads
> 1) {
1885 *ecx
|= (env
->nr_cores
* env
->nr_threads
) - 1;
1889 if (env
->cpuid_ext3_features
& CPUID_EXT3_SVM
) {
1890 *eax
= 0x00000001; /* SVM Revision */
1891 *ebx
= 0x00000010; /* nr of ASIDs */
1893 *edx
= env
->cpuid_svm_features
; /* optional features */
1902 *eax
= env
->cpuid_xlevel2
;
1908 /* Support for VIA CPU's CPUID instruction */
1909 *eax
= env
->cpuid_version
;
1912 *edx
= env
->cpuid_ext4_features
;
1917 /* Reserved for the future, and now filled with zero */
1924 /* reserved values: zero */
1933 /* CPUClass::reset() */
1934 static void x86_cpu_reset(CPUState
*s
)
1936 X86CPU
*cpu
= X86_CPU(s
);
1937 X86CPUClass
*xcc
= X86_CPU_GET_CLASS(cpu
);
1938 CPUX86State
*env
= &cpu
->env
;
1941 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
1942 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
1943 log_cpu_state(env
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
1946 xcc
->parent_reset(s
);
1949 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
1953 env
->old_exception
= -1;
1955 /* init to reset state */
1957 #ifdef CONFIG_SOFTMMU
1958 env
->hflags
|= HF_SOFTMMU_MASK
;
1960 env
->hflags2
|= HF2_GIF_MASK
;
1962 cpu_x86_update_cr0(env
, 0x60000010);
1963 env
->a20_mask
= ~0x0;
1964 env
->smbase
= 0x30000;
1966 env
->idt
.limit
= 0xffff;
1967 env
->gdt
.limit
= 0xffff;
1968 env
->ldt
.limit
= 0xffff;
1969 env
->ldt
.flags
= DESC_P_MASK
| (2 << DESC_TYPE_SHIFT
);
1970 env
->tr
.limit
= 0xffff;
1971 env
->tr
.flags
= DESC_P_MASK
| (11 << DESC_TYPE_SHIFT
);
1973 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff,
1974 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
|
1975 DESC_R_MASK
| DESC_A_MASK
);
1976 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff,
1977 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1979 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff,
1980 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1982 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff,
1983 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1985 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff,
1986 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1988 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff,
1989 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
1993 env
->regs
[R_EDX
] = env
->cpuid_version
;
1998 for (i
= 0; i
< 8; i
++) {
2003 env
->mxcsr
= 0x1f80;
2005 env
->pat
= 0x0007040600070406ULL
;
2006 env
->msr_ia32_misc_enable
= MSR_IA32_MISC_ENABLE_DEFAULT
;
2008 memset(env
->dr
, 0, sizeof(env
->dr
));
2009 env
->dr
[6] = DR6_FIXED_1
;
2010 env
->dr
[7] = DR7_FIXED_1
;
2011 cpu_breakpoint_remove_all(env
, BP_CPU
);
2012 cpu_watchpoint_remove_all(env
, BP_CPU
);
2014 #if !defined(CONFIG_USER_ONLY)
2015 /* We hard-wire the BSP to the first CPU. */
2016 if (env
->cpu_index
== 0) {
2017 apic_designate_bsp(env
->apic_state
);
2020 env
->halted
= !cpu_is_bsp(cpu
);
2024 #ifndef CONFIG_USER_ONLY
2025 bool cpu_is_bsp(X86CPU
*cpu
)
2027 return cpu_get_apic_base(cpu
->env
.apic_state
) & MSR_IA32_APICBASE_BSP
;
2030 /* TODO: remove me, when reset over QOM tree is implemented */
2031 static void x86_cpu_machine_reset_cb(void *opaque
)
2033 X86CPU
*cpu
= opaque
;
2034 cpu_reset(CPU(cpu
));
2038 static void mce_init(X86CPU
*cpu
)
2040 CPUX86State
*cenv
= &cpu
->env
;
2043 if (((cenv
->cpuid_version
>> 8) & 0xf) >= 6
2044 && (cenv
->cpuid_features
& (CPUID_MCE
| CPUID_MCA
)) ==
2045 (CPUID_MCE
| CPUID_MCA
)) {
2046 cenv
->mcg_cap
= MCE_CAP_DEF
| MCE_BANKS_DEF
;
2047 cenv
->mcg_ctl
= ~(uint64_t)0;
2048 for (bank
= 0; bank
< MCE_BANKS_DEF
; bank
++) {
2049 cenv
->mce_banks
[bank
* 4] = ~(uint64_t)0;
2054 #define MSI_ADDR_BASE 0xfee00000
2056 #ifndef CONFIG_USER_ONLY
2057 static void x86_cpu_apic_init(X86CPU
*cpu
, Error
**errp
)
2059 static int apic_mapped
;
2060 CPUX86State
*env
= &cpu
->env
;
2061 APICCommonState
*apic
;
2062 const char *apic_type
= "apic";
2064 if (kvm_irqchip_in_kernel()) {
2065 apic_type
= "kvm-apic";
2066 } else if (xen_enabled()) {
2067 apic_type
= "xen-apic";
2070 env
->apic_state
= qdev_try_create(NULL
, apic_type
);
2071 if (env
->apic_state
== NULL
) {
2072 error_setg(errp
, "APIC device '%s' could not be created", apic_type
);
2076 object_property_add_child(OBJECT(cpu
), "apic",
2077 OBJECT(env
->apic_state
), NULL
);
2078 qdev_prop_set_uint8(env
->apic_state
, "id", env
->cpuid_apic_id
);
2079 /* TODO: convert to link<> */
2080 apic
= APIC_COMMON(env
->apic_state
);
2083 if (qdev_init(env
->apic_state
)) {
2084 error_setg(errp
, "APIC device '%s' could not be initialized",
2085 object_get_typename(OBJECT(env
->apic_state
)));
2089 /* XXX: mapping more APICs at the same memory location */
2090 if (apic_mapped
== 0) {
2091 /* NOTE: the APIC is directly connected to the CPU - it is not
2092 on the global memory bus. */
2093 /* XXX: what if the base changes? */
2094 sysbus_mmio_map(sysbus_from_qdev(env
->apic_state
), 0, MSI_ADDR_BASE
);
2100 void x86_cpu_realize(Object
*obj
, Error
**errp
)
2102 X86CPU
*cpu
= X86_CPU(obj
);
2103 CPUX86State
*env
= &cpu
->env
;
2105 if (env
->cpuid_7_0_ebx_features
&& env
->cpuid_level
< 7) {
2106 env
->cpuid_level
= 7;
2109 #ifndef CONFIG_USER_ONLY
2110 qemu_register_reset(x86_cpu_machine_reset_cb
, cpu
);
2112 if (cpu
->env
.cpuid_features
& CPUID_APIC
|| smp_cpus
> 1) {
2113 x86_cpu_apic_init(cpu
, errp
);
2114 if (error_is_set(errp
)) {
2121 qemu_init_vcpu(&cpu
->env
);
2122 cpu_reset(CPU(cpu
));
2125 static void x86_cpu_initfn(Object
*obj
)
2127 X86CPU
*cpu
= X86_CPU(obj
);
2128 CPUX86State
*env
= &cpu
->env
;
2133 object_property_add(obj
, "family", "int",
2134 x86_cpuid_version_get_family
,
2135 x86_cpuid_version_set_family
, NULL
, NULL
, NULL
);
2136 object_property_add(obj
, "model", "int",
2137 x86_cpuid_version_get_model
,
2138 x86_cpuid_version_set_model
, NULL
, NULL
, NULL
);
2139 object_property_add(obj
, "stepping", "int",
2140 x86_cpuid_version_get_stepping
,
2141 x86_cpuid_version_set_stepping
, NULL
, NULL
, NULL
);
2142 object_property_add(obj
, "level", "int",
2143 x86_cpuid_get_level
,
2144 x86_cpuid_set_level
, NULL
, NULL
, NULL
);
2145 object_property_add(obj
, "xlevel", "int",
2146 x86_cpuid_get_xlevel
,
2147 x86_cpuid_set_xlevel
, NULL
, NULL
, NULL
);
2148 object_property_add_str(obj
, "vendor",
2149 x86_cpuid_get_vendor
,
2150 x86_cpuid_set_vendor
, NULL
);
2151 object_property_add_str(obj
, "model-id",
2152 x86_cpuid_get_model_id
,
2153 x86_cpuid_set_model_id
, NULL
);
2154 object_property_add(obj
, "tsc-frequency", "int",
2155 x86_cpuid_get_tsc_freq
,
2156 x86_cpuid_set_tsc_freq
, NULL
, NULL
, NULL
);
2158 env
->cpuid_apic_id
= env
->cpu_index
;
2160 /* init various static tables used in TCG mode */
2161 if (tcg_enabled() && !inited
) {
2163 optimize_flags_init();
2164 #ifndef CONFIG_USER_ONLY
2165 cpu_set_debug_excp_handler(breakpoint_handler
);
2170 static void x86_cpu_common_class_init(ObjectClass
*oc
, void *data
)
2172 X86CPUClass
*xcc
= X86_CPU_CLASS(oc
);
2173 CPUClass
*cc
= CPU_CLASS(oc
);
2175 xcc
->parent_reset
= cc
->reset
;
2176 cc
->reset
= x86_cpu_reset
;
2179 static const TypeInfo x86_cpu_type_info
= {
2180 .name
= TYPE_X86_CPU
,
2182 .instance_size
= sizeof(X86CPU
),
2183 .instance_init
= x86_cpu_initfn
,
2185 .class_size
= sizeof(X86CPUClass
),
2186 .class_init
= x86_cpu_common_class_init
,
2189 static void x86_cpu_register_types(void)
2191 type_register_static(&x86_cpu_type_info
);
2194 type_init(x86_cpu_register_types
)