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 /* collects per-function cpuid data
129 typedef struct model_features_t {
130 uint32_t *guest_feat;
133 const char **flag_names;
138 int enforce_cpuid = 0;
140 #if defined(CONFIG_KVM)
141 static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
142 (1 << KVM_FEATURE_NOP_IO_DELAY) |
143 (1 << KVM_FEATURE_MMU_OP) |
144 (1 << KVM_FEATURE_CLOCKSOURCE2) |
145 (1 << KVM_FEATURE_ASYNC_PF) |
146 (1 << KVM_FEATURE_STEAL_TIME) |
147 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
148 static const uint32_t kvm_pv_eoi_features = (0x1 << KVM_FEATURE_PV_EOI);
150 static uint32_t kvm_default_features = 0;
151 static const uint32_t kvm_pv_eoi_features = 0;
154 void enable_kvm_pv_eoi(void)
156 kvm_default_features |= kvm_pv_eoi_features;
159 void host_cpuid(uint32_t function, uint32_t count,
160 uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
162 #if defined(CONFIG_KVM)
167 : "=a"(vec[0]), "=b"(vec[1]),
168 "=c"(vec[2]), "=d"(vec[3])
169 : "0"(function), "c"(count) : "cc");
171 asm volatile("pusha \n\t"
173 "mov %%eax, 0(%2) \n\t"
174 "mov %%ebx, 4(%2) \n\t"
175 "mov %%ecx, 8(%2) \n\t"
176 "mov %%edx, 12(%2) \n\t"
178 : : "a"(function), "c"(count), "S"(vec)
193 #define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
195 /* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
196 * a substring. ex if !NULL points to the first char after a substring,
197 * otherwise the string is assumed to sized by a terminating nul.
198 * Return lexical ordering of *s1:*s2.
200 static int sstrcmp(const char *s1, const char *e1, const char *s2,
204 if (!*s1 || !*s2 || *s1 != *s2)
207 if (s1 == e1 && s2 == e2)
216 /* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
217 * '|' delimited (possibly empty) strings in which case search for a match
218 * within the alternatives proceeds left to right. Return 0 for success,
219 * non-zero otherwise.
221 static int altcmp(const char *s, const char *e, const char *altstr)
225 for (q = p = altstr; ; ) {
226 while (*p && *p != '|')
228 if ((q == p && !*s) || (q != p && !sstrcmp(s, e, q, p)))
237 /* search featureset for flag *[s..e), if found set corresponding bit in
238 * *pval and return true, otherwise return false
240 static bool lookup_feature(uint32_t *pval, const char *s, const char *e,
241 const char **featureset)
247 for (mask = 1, ppc = featureset; mask; mask <<= 1, ++ppc) {
248 if (*ppc && !altcmp(s, e, *ppc)) {
256 static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features,
257 uint32_t *ext_features,
258 uint32_t *ext2_features,
259 uint32_t *ext3_features,
260 uint32_t *kvm_features,
261 uint32_t *svm_features,
262 uint32_t *cpuid_7_0_ebx_features)
264 if (!lookup_feature(features, flagname, NULL, feature_name) &&
265 !lookup_feature(ext_features, flagname, NULL, ext_feature_name) &&
266 !lookup_feature(ext2_features, flagname, NULL, ext2_feature_name) &&
267 !lookup_feature(ext3_features, flagname, NULL, ext3_feature_name) &&
268 !lookup_feature(kvm_features, flagname, NULL, kvm_feature_name) &&
269 !lookup_feature(svm_features, flagname, NULL, svm_feature_name) &&
270 !lookup_feature(cpuid_7_0_ebx_features, flagname, NULL,
271 cpuid_7_0_ebx_feature_name))
272 fprintf(stderr, "CPU feature %s not found\n", flagname);
275 typedef struct x86_def_t {
276 struct x86_def_t *next;
279 uint32_t vendor1, vendor2, vendor3;
284 uint32_t features, ext_features, ext2_features, ext3_features;
285 uint32_t kvm_features, svm_features;
289 /* Store the results of Centaur's CPUID instructions */
290 uint32_t ext4_features;
292 /* The feature bits on CPUID[EAX=7,ECX=0].EBX */
293 uint32_t cpuid_7_0_ebx_features;
296 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
297 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
298 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
299 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
300 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
301 CPUID_PSE36 | CPUID_FXSR)
302 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
303 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
304 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
305 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
306 CPUID_PAE | CPUID_SEP | CPUID_APIC)
308 #define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
309 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
310 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
311 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
312 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
313 /* partly implemented:
314 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
315 CPUID_PSE36 (needed for Solaris) */
317 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
318 #define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \
319 CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT | \
320 CPUID_EXT_HYPERVISOR)
322 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
323 CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_XSAVE */
324 #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
325 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
326 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
328 CPUID_EXT2_PDPE1GB */
329 #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
330 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
331 #define TCG_SVM_FEATURES 0
332 #define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP)
334 /* maintains list of cpu model definitions
336 static x86_def_t *x86_defs = {NULL};
338 /* built-in cpu model definitions (deprecated)
340 static x86_def_t builtin_x86_defs[] = {
344 .vendor1 = CPUID_VENDOR_AMD_1,
345 .vendor2 = CPUID_VENDOR_AMD_2,
346 .vendor3 = CPUID_VENDOR_AMD_3,
350 .features = PPRO_FEATURES |
351 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
353 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT,
354 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
355 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
356 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
357 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
358 .xlevel = 0x8000000A,
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 |
371 CPUID_PSE36 | CPUID_VME | CPUID_HT,
372 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_CX16 |
374 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
375 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
376 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
377 CPUID_EXT2_FFXSR | CPUID_EXT2_PDPE1GB | CPUID_EXT2_RDTSCP,
378 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
380 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
381 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
382 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
383 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
384 .svm_features = CPUID_SVM_NPT | CPUID_SVM_LBRV,
385 .xlevel = 0x8000001A,
386 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
394 .features = PPRO_FEATURES |
395 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
396 CPUID_PSE36 | CPUID_VME | CPUID_DTS | CPUID_ACPI | CPUID_SS |
397 CPUID_HT | CPUID_TM | CPUID_PBE,
398 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
399 CPUID_EXT_DTES64 | CPUID_EXT_DSCPL | CPUID_EXT_VMX | CPUID_EXT_EST |
400 CPUID_EXT_TM2 | CPUID_EXT_CX16 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
401 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
402 .ext3_features = CPUID_EXT3_LAHF_LM,
403 .xlevel = 0x80000008,
404 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
409 .vendor1 = CPUID_VENDOR_INTEL_1,
410 .vendor2 = CPUID_VENDOR_INTEL_2,
411 .vendor3 = CPUID_VENDOR_INTEL_3,
415 /* Missing: CPUID_VME, CPUID_HT */
416 .features = PPRO_FEATURES |
417 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
419 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
420 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16,
421 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
422 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
423 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
424 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
425 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
426 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
427 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
429 .xlevel = 0x80000008,
430 .model_id = "Common KVM processor"
438 .features = PPRO_FEATURES,
439 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_POPCNT,
440 .xlevel = 0x80000004,
448 .features = PPRO_FEATURES |
449 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_PSE36,
450 .ext_features = CPUID_EXT_SSE3,
451 .ext2_features = PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES,
453 .xlevel = 0x80000008,
454 .model_id = "Common 32-bit KVM processor"
462 .features = PPRO_FEATURES | CPUID_VME |
463 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_DTS | CPUID_ACPI |
464 CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
465 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_VMX |
466 CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
467 .ext2_features = CPUID_EXT2_NX,
468 .xlevel = 0x80000008,
469 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
477 .features = I486_FEATURES,
486 .features = PENTIUM_FEATURES,
495 .features = PENTIUM2_FEATURES,
504 .features = PENTIUM3_FEATURES,
510 .vendor1 = CPUID_VENDOR_AMD_1,
511 .vendor2 = CPUID_VENDOR_AMD_2,
512 .vendor3 = CPUID_VENDOR_AMD_3,
516 .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR |
518 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
519 CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
520 .xlevel = 0x80000008,
524 /* original is on level 10 */
529 .features = PPRO_FEATURES |
530 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME | CPUID_DTS |
531 CPUID_ACPI | CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
532 /* Some CPUs got no CPUID_SEP */
533 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
534 CPUID_EXT_DSCPL | CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR,
535 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
537 .ext3_features = CPUID_EXT3_LAHF_LM,
538 .xlevel = 0x8000000A,
539 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
544 .vendor1 = CPUID_VENDOR_INTEL_1,
545 .vendor2 = CPUID_VENDOR_INTEL_2,
546 .vendor3 = CPUID_VENDOR_INTEL_3,
550 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
551 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
552 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
553 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
554 CPUID_DE | CPUID_FP87,
555 .ext_features = CPUID_EXT_SSSE3 | CPUID_EXT_SSE3,
556 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_NX | CPUID_EXT2_SYSCALL,
557 .ext3_features = CPUID_EXT3_LAHF_LM,
558 .xlevel = 0x8000000A,
559 .model_id = "Intel Celeron_4x0 (Conroe/Merom Class Core 2)",
564 .vendor1 = CPUID_VENDOR_INTEL_1,
565 .vendor2 = CPUID_VENDOR_INTEL_2,
566 .vendor3 = CPUID_VENDOR_INTEL_3,
570 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
571 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
572 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
573 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
574 CPUID_DE | CPUID_FP87,
575 .ext_features = CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
577 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_NX | CPUID_EXT2_SYSCALL,
578 .ext3_features = CPUID_EXT3_LAHF_LM,
579 .xlevel = 0x8000000A,
580 .model_id = "Intel Core 2 Duo P9xxx (Penryn Class Core 2)",
585 .vendor1 = CPUID_VENDOR_INTEL_1,
586 .vendor2 = CPUID_VENDOR_INTEL_2,
587 .vendor3 = CPUID_VENDOR_INTEL_3,
591 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
592 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
593 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
594 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
595 CPUID_DE | CPUID_FP87,
596 .ext_features = CPUID_EXT_POPCNT | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
597 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_SSE3,
598 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
599 .ext3_features = CPUID_EXT3_LAHF_LM,
600 .xlevel = 0x8000000A,
601 .model_id = "Intel Core i7 9xx (Nehalem Class Core i7)",
606 .vendor1 = CPUID_VENDOR_INTEL_1,
607 .vendor2 = CPUID_VENDOR_INTEL_2,
608 .vendor3 = CPUID_VENDOR_INTEL_3,
612 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
613 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
614 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
615 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
616 CPUID_DE | CPUID_FP87,
617 .ext_features = CPUID_EXT_AES | CPUID_EXT_POPCNT | CPUID_EXT_SSE42 |
618 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
620 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
621 .ext3_features = CPUID_EXT3_LAHF_LM,
622 .xlevel = 0x8000000A,
623 .model_id = "Westmere E56xx/L56xx/X56xx (Nehalem-C)",
626 .name = "SandyBridge",
628 .vendor1 = CPUID_VENDOR_INTEL_1,
629 .vendor2 = CPUID_VENDOR_INTEL_2,
630 .vendor3 = CPUID_VENDOR_INTEL_3,
634 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
635 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
636 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
637 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
638 CPUID_DE | CPUID_FP87,
639 .ext_features = CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
640 CPUID_EXT_TSC_DEADLINE_TIMER | CPUID_EXT_POPCNT |
641 CPUID_EXT_X2APIC | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
642 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ |
644 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_NX |
646 .ext3_features = CPUID_EXT3_LAHF_LM,
647 .xlevel = 0x8000000A,
648 .model_id = "Intel Xeon E312xx (Sandy Bridge)",
653 .vendor1 = CPUID_VENDOR_INTEL_1,
654 .vendor2 = CPUID_VENDOR_INTEL_2,
655 .vendor3 = CPUID_VENDOR_INTEL_3,
659 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
660 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
661 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
662 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
663 CPUID_DE | CPUID_FP87,
664 .ext_features = CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
665 CPUID_EXT_POPCNT | CPUID_EXT_X2APIC | CPUID_EXT_SSE42 |
666 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
667 CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3 |
668 CPUID_EXT_TSC_DEADLINE_TIMER | CPUID_EXT_FMA | CPUID_EXT_MOVBE |
670 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_NX |
672 .ext3_features = CPUID_EXT3_LAHF_LM,
673 .cpuid_7_0_ebx_features = CPUID_7_0_EBX_FSGSBASE | CPUID_7_0_EBX_BMI1 |
674 CPUID_7_0_EBX_HLE | CPUID_7_0_EBX_AVX2 | CPUID_7_0_EBX_SMEP |
675 CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_INVPCID |
677 .xlevel = 0x8000000A,
678 .model_id = "Intel Core Processor (Haswell)",
681 .name = "Opteron_G1",
683 .vendor1 = CPUID_VENDOR_AMD_1,
684 .vendor2 = CPUID_VENDOR_AMD_2,
685 .vendor3 = CPUID_VENDOR_AMD_3,
689 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
690 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
691 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
692 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
693 CPUID_DE | CPUID_FP87,
694 .ext_features = CPUID_EXT_SSE3,
695 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
696 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
697 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
698 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
699 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
700 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
701 .xlevel = 0x80000008,
702 .model_id = "AMD Opteron 240 (Gen 1 Class Opteron)",
705 .name = "Opteron_G2",
707 .vendor1 = CPUID_VENDOR_AMD_1,
708 .vendor2 = CPUID_VENDOR_AMD_2,
709 .vendor3 = CPUID_VENDOR_AMD_3,
713 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
714 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
715 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
716 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
717 CPUID_DE | CPUID_FP87,
718 .ext_features = CPUID_EXT_CX16 | CPUID_EXT_SSE3,
719 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_FXSR |
720 CPUID_EXT2_MMX | CPUID_EXT2_NX | CPUID_EXT2_PSE36 |
721 CPUID_EXT2_PAT | CPUID_EXT2_CMOV | CPUID_EXT2_MCA |
722 CPUID_EXT2_PGE | CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL |
723 CPUID_EXT2_APIC | CPUID_EXT2_CX8 | CPUID_EXT2_MCE |
724 CPUID_EXT2_PAE | CPUID_EXT2_MSR | CPUID_EXT2_TSC | CPUID_EXT2_PSE |
725 CPUID_EXT2_DE | CPUID_EXT2_FPU,
726 .ext3_features = CPUID_EXT3_SVM | CPUID_EXT3_LAHF_LM,
727 .xlevel = 0x80000008,
728 .model_id = "AMD Opteron 22xx (Gen 2 Class Opteron)",
731 .name = "Opteron_G3",
733 .vendor1 = CPUID_VENDOR_AMD_1,
734 .vendor2 = CPUID_VENDOR_AMD_2,
735 .vendor3 = CPUID_VENDOR_AMD_3,
739 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
740 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
741 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
742 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
743 CPUID_DE | CPUID_FP87,
744 .ext_features = CPUID_EXT_POPCNT | CPUID_EXT_CX16 | CPUID_EXT_MONITOR |
746 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_FXSR |
747 CPUID_EXT2_MMX | CPUID_EXT2_NX | CPUID_EXT2_PSE36 |
748 CPUID_EXT2_PAT | CPUID_EXT2_CMOV | CPUID_EXT2_MCA |
749 CPUID_EXT2_PGE | CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL |
750 CPUID_EXT2_APIC | CPUID_EXT2_CX8 | CPUID_EXT2_MCE |
751 CPUID_EXT2_PAE | CPUID_EXT2_MSR | CPUID_EXT2_TSC | CPUID_EXT2_PSE |
752 CPUID_EXT2_DE | CPUID_EXT2_FPU,
753 .ext3_features = CPUID_EXT3_MISALIGNSSE | CPUID_EXT3_SSE4A |
754 CPUID_EXT3_ABM | CPUID_EXT3_SVM | CPUID_EXT3_LAHF_LM,
755 .xlevel = 0x80000008,
756 .model_id = "AMD Opteron 23xx (Gen 3 Class Opteron)",
759 .name = "Opteron_G4",
761 .vendor1 = CPUID_VENDOR_AMD_1,
762 .vendor2 = CPUID_VENDOR_AMD_2,
763 .vendor3 = CPUID_VENDOR_AMD_3,
767 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
768 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
769 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
770 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
771 CPUID_DE | CPUID_FP87,
772 .ext_features = CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
773 CPUID_EXT_POPCNT | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
774 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ |
776 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP |
777 CPUID_EXT2_PDPE1GB | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
778 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
779 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
780 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
781 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
782 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
783 .ext3_features = CPUID_EXT3_FMA4 | CPUID_EXT3_XOP |
784 CPUID_EXT3_3DNOWPREFETCH | CPUID_EXT3_MISALIGNSSE |
785 CPUID_EXT3_SSE4A | CPUID_EXT3_ABM | CPUID_EXT3_SVM |
787 .xlevel = 0x8000001A,
788 .model_id = "AMD Opteron 62xx class CPU",
791 .name = "Opteron_G5",
793 .vendor1 = CPUID_VENDOR_AMD_1,
794 .vendor2 = CPUID_VENDOR_AMD_2,
795 .vendor3 = CPUID_VENDOR_AMD_3,
799 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
800 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
801 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
802 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
803 CPUID_DE | CPUID_FP87,
804 .ext_features = CPUID_EXT_F16C | CPUID_EXT_AVX | CPUID_EXT_XSAVE |
805 CPUID_EXT_AES | CPUID_EXT_POPCNT | CPUID_EXT_SSE42 |
806 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_FMA |
807 CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3,
808 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP |
809 CPUID_EXT2_PDPE1GB | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
810 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
811 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
812 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
813 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
814 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
815 .ext3_features = CPUID_EXT3_TBM | CPUID_EXT3_FMA4 | CPUID_EXT3_XOP |
816 CPUID_EXT3_3DNOWPREFETCH | CPUID_EXT3_MISALIGNSSE |
817 CPUID_EXT3_SSE4A | CPUID_EXT3_ABM | CPUID_EXT3_SVM |
819 .xlevel = 0x8000001A,
820 .model_id = "AMD Opteron 63xx class CPU",
825 static int cpu_x86_fill_model_id(char *str)
827 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
830 for (i = 0; i < 3; i++) {
831 host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx);
832 memcpy(str + i * 16 + 0, &eax, 4);
833 memcpy(str + i * 16 + 4, &ebx, 4);
834 memcpy(str + i * 16 + 8, &ecx, 4);
835 memcpy(str + i * 16 + 12, &edx, 4);
841 /* Fill a x86_def_t struct with information about the host CPU, and
842 * the CPU features supported by the host hardware + host kernel
844 * This function may be called only if KVM is enabled.
846 static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
849 KVMState *s = kvm_state;
850 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
852 assert(kvm_enabled());
854 x86_cpu_def->name = "host";
855 host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
856 x86_cpu_def->vendor1 = ebx;
857 x86_cpu_def->vendor2 = edx;
858 x86_cpu_def->vendor3 = ecx;
860 host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
861 x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
862 x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
863 x86_cpu_def->stepping = eax & 0x0F;
865 x86_cpu_def->level = kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX);
866 x86_cpu_def->features = kvm_arch_get_supported_cpuid(s, 0x1, 0, R_EDX);
867 x86_cpu_def->ext_features = kvm_arch_get_supported_cpuid(s, 0x1, 0, R_ECX);
869 if (x86_cpu_def->level >= 7) {
870 x86_cpu_def->cpuid_7_0_ebx_features =
871 kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX);
873 x86_cpu_def->cpuid_7_0_ebx_features = 0;
876 x86_cpu_def->xlevel = kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX);
877 x86_cpu_def->ext2_features =
878 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX);
879 x86_cpu_def->ext3_features =
880 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX);
882 cpu_x86_fill_model_id(x86_cpu_def->model_id);
883 x86_cpu_def->vendor_override = 0;
885 /* Call Centaur's CPUID instruction. */
886 if (x86_cpu_def->vendor1 == CPUID_VENDOR_VIA_1 &&
887 x86_cpu_def->vendor2 == CPUID_VENDOR_VIA_2 &&
888 x86_cpu_def->vendor3 == CPUID_VENDOR_VIA_3) {
889 host_cpuid(0xC0000000, 0, &eax, &ebx, &ecx, &edx);
890 eax = kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
891 if (eax >= 0xC0000001) {
892 /* Support VIA max extended level */
893 x86_cpu_def->xlevel2 = eax;
894 host_cpuid(0xC0000001, 0, &eax, &ebx, &ecx, &edx);
895 x86_cpu_def->ext4_features =
896 kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
901 * Every SVM feature requires emulation support in KVM - so we can't just
902 * read the host features here. KVM might even support SVM features not
903 * available on the host hardware. Just set all bits and mask out the
904 * unsupported ones later.
906 x86_cpu_def->svm_features = -1;
907 #endif /* CONFIG_KVM */
910 static int unavailable_host_feature(struct model_features_t *f, uint32_t mask)
914 for (i = 0; i < 32; ++i)
916 fprintf(stderr, "warning: host cpuid %04x_%04x lacks requested"
917 " flag '%s' [0x%08x]\n",
918 f->cpuid >> 16, f->cpuid & 0xffff,
919 f->flag_names[i] ? f->flag_names[i] : "[reserved]", mask);
925 /* best effort attempt to inform user requested cpu flags aren't making
926 * their way to the guest. Note: ft[].check_feat ideally should be
927 * specified via a guest_def field to suppress report of extraneous flags.
929 * This function may be called only if KVM is enabled.
931 static int kvm_check_features_against_host(x86_def_t *guest_def)
936 struct model_features_t ft[] = {
937 {&guest_def->features, &host_def.features,
938 ~0, feature_name, 0x00000000},
939 {&guest_def->ext_features, &host_def.ext_features,
940 ~CPUID_EXT_HYPERVISOR, ext_feature_name, 0x00000001},
941 {&guest_def->ext2_features, &host_def.ext2_features,
942 ~PPRO_FEATURES, ext2_feature_name, 0x80000000},
943 {&guest_def->ext3_features, &host_def.ext3_features,
944 ~CPUID_EXT3_SVM, ext3_feature_name, 0x80000001}};
946 assert(kvm_enabled());
948 kvm_cpu_fill_host(&host_def);
949 for (rv = 0, i = 0; i < ARRAY_SIZE(ft); ++i)
950 for (mask = 1; mask; mask <<= 1)
951 if (ft[i].check_feat & mask && *ft[i].guest_feat & mask &&
952 !(*ft[i].host_feat & mask)) {
953 unavailable_host_feature(&ft[i], mask);
959 static void x86_cpuid_version_get_family(Object *obj, Visitor *v, void *opaque,
960 const char *name, Error **errp)
962 X86CPU *cpu = X86_CPU(obj);
963 CPUX86State *env = &cpu->env;
966 value = (env->cpuid_version >> 8) & 0xf;
968 value += (env->cpuid_version >> 20) & 0xff;
970 visit_type_int(v, &value, name, errp);
973 static void x86_cpuid_version_set_family(Object *obj, Visitor *v, void *opaque,
974 const char *name, Error **errp)
976 X86CPU *cpu = X86_CPU(obj);
977 CPUX86State *env = &cpu->env;
978 const int64_t min = 0;
979 const int64_t max = 0xff + 0xf;
982 visit_type_int(v, &value, name, errp);
983 if (error_is_set(errp)) {
986 if (value < min || value > max) {
987 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
988 name ? name : "null", value, min, max);
992 env->cpuid_version &= ~0xff00f00;
994 env->cpuid_version |= 0xf00 | ((value - 0x0f) << 20);
996 env->cpuid_version |= value << 8;
1000 static void x86_cpuid_version_get_model(Object *obj, Visitor *v, void *opaque,
1001 const char *name, Error **errp)
1003 X86CPU *cpu = X86_CPU(obj);
1004 CPUX86State *env = &cpu->env;
1007 value = (env->cpuid_version >> 4) & 0xf;
1008 value |= ((env->cpuid_version >> 16) & 0xf) << 4;
1009 visit_type_int(v, &value, name, errp);
1012 static void x86_cpuid_version_set_model(Object *obj, Visitor *v, void *opaque,
1013 const char *name, Error **errp)
1015 X86CPU *cpu = X86_CPU(obj);
1016 CPUX86State *env = &cpu->env;
1017 const int64_t min = 0;
1018 const int64_t max = 0xff;
1021 visit_type_int(v, &value, name, errp);
1022 if (error_is_set(errp)) {
1025 if (value < min || value > max) {
1026 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1027 name ? name : "null", value, min, max);
1031 env->cpuid_version &= ~0xf00f0;
1032 env->cpuid_version |= ((value & 0xf) << 4) | ((value >> 4) << 16);
1035 static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v,
1036 void *opaque, const char *name,
1039 X86CPU *cpu = X86_CPU(obj);
1040 CPUX86State *env = &cpu->env;
1043 value = env->cpuid_version & 0xf;
1044 visit_type_int(v, &value, name, errp);
1047 static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
1048 void *opaque, const char *name,
1051 X86CPU *cpu = X86_CPU(obj);
1052 CPUX86State *env = &cpu->env;
1053 const int64_t min = 0;
1054 const int64_t max = 0xf;
1057 visit_type_int(v, &value, name, errp);
1058 if (error_is_set(errp)) {
1061 if (value < min || value > max) {
1062 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1063 name ? name : "null", value, min, max);
1067 env->cpuid_version &= ~0xf;
1068 env->cpuid_version |= value & 0xf;
1071 static void x86_cpuid_get_level(Object *obj, Visitor *v, void *opaque,
1072 const char *name, Error **errp)
1074 X86CPU *cpu = X86_CPU(obj);
1076 visit_type_uint32(v, &cpu->env.cpuid_level, name, errp);
1079 static void x86_cpuid_set_level(Object *obj, Visitor *v, void *opaque,
1080 const char *name, Error **errp)
1082 X86CPU *cpu = X86_CPU(obj);
1084 visit_type_uint32(v, &cpu->env.cpuid_level, name, errp);
1087 static void x86_cpuid_get_xlevel(Object *obj, Visitor *v, void *opaque,
1088 const char *name, Error **errp)
1090 X86CPU *cpu = X86_CPU(obj);
1092 visit_type_uint32(v, &cpu->env.cpuid_xlevel, name, errp);
1095 static void x86_cpuid_set_xlevel(Object *obj, Visitor *v, void *opaque,
1096 const char *name, Error **errp)
1098 X86CPU *cpu = X86_CPU(obj);
1100 visit_type_uint32(v, &cpu->env.cpuid_xlevel, name, errp);
1103 static char *x86_cpuid_get_vendor(Object *obj, Error **errp)
1105 X86CPU *cpu = X86_CPU(obj);
1106 CPUX86State *env = &cpu->env;
1110 value = (char *)g_malloc(CPUID_VENDOR_SZ + 1);
1111 for (i = 0; i < 4; i++) {
1112 value[i ] = env->cpuid_vendor1 >> (8 * i);
1113 value[i + 4] = env->cpuid_vendor2 >> (8 * i);
1114 value[i + 8] = env->cpuid_vendor3 >> (8 * i);
1116 value[CPUID_VENDOR_SZ] = '\0';
1120 static void x86_cpuid_set_vendor(Object *obj, const char *value,
1123 X86CPU *cpu = X86_CPU(obj);
1124 CPUX86State *env = &cpu->env;
1127 if (strlen(value) != CPUID_VENDOR_SZ) {
1128 error_set(errp, QERR_PROPERTY_VALUE_BAD, "",
1133 env->cpuid_vendor1 = 0;
1134 env->cpuid_vendor2 = 0;
1135 env->cpuid_vendor3 = 0;
1136 for (i = 0; i < 4; i++) {
1137 env->cpuid_vendor1 |= ((uint8_t)value[i ]) << (8 * i);
1138 env->cpuid_vendor2 |= ((uint8_t)value[i + 4]) << (8 * i);
1139 env->cpuid_vendor3 |= ((uint8_t)value[i + 8]) << (8 * i);
1141 env->cpuid_vendor_override = 1;
1144 static char *x86_cpuid_get_model_id(Object *obj, Error **errp)
1146 X86CPU *cpu = X86_CPU(obj);
1147 CPUX86State *env = &cpu->env;
1151 value = g_malloc(48 + 1);
1152 for (i = 0; i < 48; i++) {
1153 value[i] = env->cpuid_model[i >> 2] >> (8 * (i & 3));
1159 static void x86_cpuid_set_model_id(Object *obj, const char *model_id,
1162 X86CPU *cpu = X86_CPU(obj);
1163 CPUX86State *env = &cpu->env;
1166 if (model_id == NULL) {
1169 len = strlen(model_id);
1170 memset(env->cpuid_model, 0, 48);
1171 for (i = 0; i < 48; i++) {
1175 c = (uint8_t)model_id[i];
1177 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
1181 static void x86_cpuid_get_tsc_freq(Object *obj, Visitor *v, void *opaque,
1182 const char *name, Error **errp)
1184 X86CPU *cpu = X86_CPU(obj);
1187 value = cpu->env.tsc_khz * 1000;
1188 visit_type_int(v, &value, name, errp);
1191 static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,
1192 const char *name, Error **errp)
1194 X86CPU *cpu = X86_CPU(obj);
1195 const int64_t min = 0;
1196 const int64_t max = INT64_MAX;
1199 visit_type_int(v, &value, name, errp);
1200 if (error_is_set(errp)) {
1203 if (value < min || value > max) {
1204 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1205 name ? name : "null", value, min, max);
1209 cpu->env.tsc_khz = value / 1000;
1212 static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *name)
1216 for (def = x86_defs; def; def = def->next) {
1217 if (name && !strcmp(name, def->name)) {
1221 if (kvm_enabled() && name && strcmp(name, "host") == 0) {
1222 kvm_cpu_fill_host(x86_cpu_def);
1226 memcpy(x86_cpu_def, def, sizeof(*def));
1232 /* Parse "+feature,-feature,feature=foo" CPU feature string
1234 static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features)
1237 char *featurestr; /* Single 'key=value" string being parsed */
1238 /* Features to be added */
1239 uint32_t plus_features = 0, plus_ext_features = 0;
1240 uint32_t plus_ext2_features = 0, plus_ext3_features = 0;
1241 uint32_t plus_kvm_features = kvm_default_features, plus_svm_features = 0;
1242 uint32_t plus_7_0_ebx_features = 0;
1243 /* Features to be removed */
1244 uint32_t minus_features = 0, minus_ext_features = 0;
1245 uint32_t minus_ext2_features = 0, minus_ext3_features = 0;
1246 uint32_t minus_kvm_features = 0, minus_svm_features = 0;
1247 uint32_t minus_7_0_ebx_features = 0;
1250 add_flagname_to_bitmaps("hypervisor", &plus_features,
1251 &plus_ext_features, &plus_ext2_features, &plus_ext3_features,
1252 &plus_kvm_features, &plus_svm_features, &plus_7_0_ebx_features);
1254 featurestr = features ? strtok(features, ",") : NULL;
1256 while (featurestr) {
1258 if (featurestr[0] == '+') {
1259 add_flagname_to_bitmaps(featurestr + 1, &plus_features,
1260 &plus_ext_features, &plus_ext2_features,
1261 &plus_ext3_features, &plus_kvm_features,
1262 &plus_svm_features, &plus_7_0_ebx_features);
1263 } else if (featurestr[0] == '-') {
1264 add_flagname_to_bitmaps(featurestr + 1, &minus_features,
1265 &minus_ext_features, &minus_ext2_features,
1266 &minus_ext3_features, &minus_kvm_features,
1267 &minus_svm_features, &minus_7_0_ebx_features);
1268 } else if ((val = strchr(featurestr, '='))) {
1270 if (!strcmp(featurestr, "family")) {
1272 numvalue = strtoul(val, &err, 0);
1273 if (!*val || *err || numvalue > 0xff + 0xf) {
1274 fprintf(stderr, "bad numerical value %s\n", val);
1277 x86_cpu_def->family = numvalue;
1278 } else if (!strcmp(featurestr, "model")) {
1280 numvalue = strtoul(val, &err, 0);
1281 if (!*val || *err || numvalue > 0xff) {
1282 fprintf(stderr, "bad numerical value %s\n", val);
1285 x86_cpu_def->model = numvalue;
1286 } else if (!strcmp(featurestr, "stepping")) {
1288 numvalue = strtoul(val, &err, 0);
1289 if (!*val || *err || numvalue > 0xf) {
1290 fprintf(stderr, "bad numerical value %s\n", val);
1293 x86_cpu_def->stepping = numvalue ;
1294 } else if (!strcmp(featurestr, "level")) {
1296 numvalue = strtoul(val, &err, 0);
1297 if (!*val || *err) {
1298 fprintf(stderr, "bad numerical value %s\n", val);
1301 x86_cpu_def->level = numvalue;
1302 } else if (!strcmp(featurestr, "xlevel")) {
1304 numvalue = strtoul(val, &err, 0);
1305 if (!*val || *err) {
1306 fprintf(stderr, "bad numerical value %s\n", val);
1309 if (numvalue < 0x80000000) {
1310 numvalue += 0x80000000;
1312 x86_cpu_def->xlevel = numvalue;
1313 } else if (!strcmp(featurestr, "vendor")) {
1314 if (strlen(val) != 12) {
1315 fprintf(stderr, "vendor string must be 12 chars long\n");
1318 x86_cpu_def->vendor1 = 0;
1319 x86_cpu_def->vendor2 = 0;
1320 x86_cpu_def->vendor3 = 0;
1321 for(i = 0; i < 4; i++) {
1322 x86_cpu_def->vendor1 |= ((uint8_t)val[i ]) << (8 * i);
1323 x86_cpu_def->vendor2 |= ((uint8_t)val[i + 4]) << (8 * i);
1324 x86_cpu_def->vendor3 |= ((uint8_t)val[i + 8]) << (8 * i);
1326 x86_cpu_def->vendor_override = 1;
1327 } else if (!strcmp(featurestr, "model_id")) {
1328 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
1330 } else if (!strcmp(featurestr, "tsc_freq")) {
1334 tsc_freq = strtosz_suffix_unit(val, &err,
1335 STRTOSZ_DEFSUFFIX_B, 1000);
1336 if (tsc_freq < 0 || *err) {
1337 fprintf(stderr, "bad numerical value %s\n", val);
1340 x86_cpu_def->tsc_khz = tsc_freq / 1000;
1341 } else if (!strcmp(featurestr, "hv_spinlocks")) {
1343 numvalue = strtoul(val, &err, 0);
1344 if (!*val || *err) {
1345 fprintf(stderr, "bad numerical value %s\n", val);
1348 hyperv_set_spinlock_retries(numvalue);
1350 fprintf(stderr, "unrecognized feature %s\n", featurestr);
1353 } else if (!strcmp(featurestr, "check")) {
1355 } else if (!strcmp(featurestr, "enforce")) {
1356 check_cpuid = enforce_cpuid = 1;
1357 } else if (!strcmp(featurestr, "hv_relaxed")) {
1358 hyperv_enable_relaxed_timing(true);
1359 } else if (!strcmp(featurestr, "hv_vapic")) {
1360 hyperv_enable_vapic_recommended(true);
1362 fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
1365 featurestr = strtok(NULL, ",");
1367 x86_cpu_def->features |= plus_features;
1368 x86_cpu_def->ext_features |= plus_ext_features;
1369 x86_cpu_def->ext2_features |= plus_ext2_features;
1370 x86_cpu_def->ext3_features |= plus_ext3_features;
1371 x86_cpu_def->kvm_features |= plus_kvm_features;
1372 x86_cpu_def->svm_features |= plus_svm_features;
1373 x86_cpu_def->cpuid_7_0_ebx_features |= plus_7_0_ebx_features;
1374 x86_cpu_def->features &= ~minus_features;
1375 x86_cpu_def->ext_features &= ~minus_ext_features;
1376 x86_cpu_def->ext2_features &= ~minus_ext2_features;
1377 x86_cpu_def->ext3_features &= ~minus_ext3_features;
1378 x86_cpu_def->kvm_features &= ~minus_kvm_features;
1379 x86_cpu_def->svm_features &= ~minus_svm_features;
1380 x86_cpu_def->cpuid_7_0_ebx_features &= ~minus_7_0_ebx_features;
1381 if (check_cpuid && kvm_enabled()) {
1382 if (kvm_check_features_against_host(x86_cpu_def) && enforce_cpuid)
1391 /* generate a composite string into buf of all cpuid names in featureset
1392 * selected by fbits. indicate truncation at bufsize in the event of overflow.
1393 * if flags, suppress names undefined in featureset.
1395 static void listflags(char *buf, int bufsize, uint32_t fbits,
1396 const char **featureset, uint32_t flags)
1398 const char **p = &featureset[31];
1402 b = 4 <= bufsize ? buf + (bufsize -= 3) - 1 : NULL;
1404 for (q = buf, bit = 31; fbits && bufsize; --p, fbits &= ~(1 << bit), --bit)
1405 if (fbits & 1 << bit && (*p || !flags)) {
1407 nc = snprintf(q, bufsize, "%s%s", q == buf ? "" : " ", *p);
1409 nc = snprintf(q, bufsize, "%s[%d]", q == buf ? "" : " ", bit);
1410 if (bufsize <= nc) {
1412 memcpy(b, "...", sizeof("..."));
1421 /* generate CPU information. */
1422 void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1427 for (def = x86_defs; def; def = def->next) {
1428 snprintf(buf, sizeof(buf), "%s", def->name);
1429 (*cpu_fprintf)(f, "x86 %16s %-48s\n", buf, def->model_id);
1431 if (kvm_enabled()) {
1432 (*cpu_fprintf)(f, "x86 %16s\n", "[host]");
1434 (*cpu_fprintf)(f, "\nRecognized CPUID flags:\n");
1435 listflags(buf, sizeof(buf), (uint32_t)~0, feature_name, 1);
1436 (*cpu_fprintf)(f, " %s\n", buf);
1437 listflags(buf, sizeof(buf), (uint32_t)~0, ext_feature_name, 1);
1438 (*cpu_fprintf)(f, " %s\n", buf);
1439 listflags(buf, sizeof(buf), (uint32_t)~0, ext2_feature_name, 1);
1440 (*cpu_fprintf)(f, " %s\n", buf);
1441 listflags(buf, sizeof(buf), (uint32_t)~0, ext3_feature_name, 1);
1442 (*cpu_fprintf)(f, " %s\n", buf);
1445 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
1447 CpuDefinitionInfoList *cpu_list = NULL;
1450 for (def = x86_defs; def; def = def->next) {
1451 CpuDefinitionInfoList *entry;
1452 CpuDefinitionInfo *info;
1454 info = g_malloc0(sizeof(*info));
1455 info->name = g_strdup(def->name);
1457 entry = g_malloc0(sizeof(*entry));
1458 entry->value = info;
1459 entry->next = cpu_list;
1467 static void filter_features_for_kvm(X86CPU *cpu)
1469 CPUX86State *env = &cpu->env;
1470 KVMState *s = kvm_state;
1472 env->cpuid_features &=
1473 kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
1474 env->cpuid_ext_features &=
1475 kvm_arch_get_supported_cpuid(s, 1, 0, R_ECX);
1476 env->cpuid_ext2_features &=
1477 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX);
1478 env->cpuid_ext3_features &=
1479 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX);
1480 env->cpuid_svm_features &=
1481 kvm_arch_get_supported_cpuid(s, 0x8000000A, 0, R_EDX);
1482 env->cpuid_7_0_ebx_features &=
1483 kvm_arch_get_supported_cpuid(s, 7, 0, R_EBX);
1484 env->cpuid_kvm_features &=
1485 kvm_arch_get_supported_cpuid(s, KVM_CPUID_FEATURES, 0, R_EAX);
1486 env->cpuid_ext4_features &=
1487 kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
1492 int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
1494 CPUX86State *env = &cpu->env;
1495 x86_def_t def1, *def = &def1;
1496 Error *error = NULL;
1497 char *name, *features;
1498 gchar **model_pieces;
1500 memset(def, 0, sizeof(*def));
1502 model_pieces = g_strsplit(cpu_model, ",", 2);
1503 if (!model_pieces[0]) {
1506 name = model_pieces[0];
1507 features = model_pieces[1];
1509 if (cpu_x86_find_by_name(def, name) < 0) {
1513 if (cpu_x86_parse_featurestr(def, features) < 0) {
1517 env->cpuid_vendor1 = def->vendor1;
1518 env->cpuid_vendor2 = def->vendor2;
1519 env->cpuid_vendor3 = def->vendor3;
1521 env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1;
1522 env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2;
1523 env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3;
1525 env->cpuid_vendor_override = def->vendor_override;
1526 object_property_set_int(OBJECT(cpu), def->level, "level", &error);
1527 object_property_set_int(OBJECT(cpu), def->family, "family", &error);
1528 object_property_set_int(OBJECT(cpu), def->model, "model", &error);
1529 object_property_set_int(OBJECT(cpu), def->stepping, "stepping", &error);
1530 env->cpuid_features = def->features;
1531 env->cpuid_ext_features = def->ext_features;
1532 env->cpuid_ext2_features = def->ext2_features;
1533 env->cpuid_ext3_features = def->ext3_features;
1534 object_property_set_int(OBJECT(cpu), def->xlevel, "xlevel", &error);
1535 env->cpuid_kvm_features = def->kvm_features;
1536 env->cpuid_svm_features = def->svm_features;
1537 env->cpuid_ext4_features = def->ext4_features;
1538 env->cpuid_7_0_ebx_features = def->cpuid_7_0_ebx_features;
1539 env->cpuid_xlevel2 = def->xlevel2;
1540 object_property_set_int(OBJECT(cpu), (int64_t)def->tsc_khz * 1000,
1541 "tsc-frequency", &error);
1543 /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on
1546 if (env->cpuid_vendor1 == CPUID_VENDOR_AMD_1 &&
1547 env->cpuid_vendor2 == CPUID_VENDOR_AMD_2 &&
1548 env->cpuid_vendor3 == CPUID_VENDOR_AMD_3) {
1549 env->cpuid_ext2_features &= ~CPUID_EXT2_AMD_ALIASES;
1550 env->cpuid_ext2_features |= (def->features & CPUID_EXT2_AMD_ALIASES);
1553 if (!kvm_enabled()) {
1554 env->cpuid_features &= TCG_FEATURES;
1555 env->cpuid_ext_features &= TCG_EXT_FEATURES;
1556 env->cpuid_ext2_features &= (TCG_EXT2_FEATURES
1557 #ifdef TARGET_X86_64
1558 | CPUID_EXT2_SYSCALL | CPUID_EXT2_LM
1561 env->cpuid_ext3_features &= TCG_EXT3_FEATURES;
1562 env->cpuid_svm_features &= TCG_SVM_FEATURES;
1565 filter_features_for_kvm(cpu);
1568 object_property_set_str(OBJECT(cpu), def->model_id, "model-id", &error);
1570 fprintf(stderr, "%s\n", error_get_pretty(error));
1575 g_strfreev(model_pieces);
1578 g_strfreev(model_pieces);
1582 #if !defined(CONFIG_USER_ONLY)
1584 void cpu_clear_apic_feature(CPUX86State *env)
1586 env->cpuid_features &= ~CPUID_APIC;
1589 #endif /* !CONFIG_USER_ONLY */
1591 /* Initialize list of CPU models, filling some non-static fields if necessary
1593 void x86_cpudef_setup(void)
1596 static const char *model_with_versions[] = { "qemu32", "qemu64", "athlon" };
1598 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); ++i) {
1599 x86_def_t *def = &builtin_x86_defs[i];
1600 def->next = x86_defs;
1602 /* Look for specific "cpudef" models that */
1603 /* have the QEMU version in .model_id */
1604 for (j = 0; j < ARRAY_SIZE(model_with_versions); j++) {
1605 if (strcmp(model_with_versions[j], def->name) == 0) {
1606 pstrcpy(def->model_id, sizeof(def->model_id),
1607 "QEMU Virtual CPU version ");
1608 pstrcat(def->model_id, sizeof(def->model_id),
1609 qemu_get_version());
1618 static void get_cpuid_vendor(CPUX86State *env, uint32_t *ebx,
1619 uint32_t *ecx, uint32_t *edx)
1621 *ebx = env->cpuid_vendor1;
1622 *edx = env->cpuid_vendor2;
1623 *ecx = env->cpuid_vendor3;
1625 /* sysenter isn't supported on compatibility mode on AMD, syscall
1626 * isn't supported in compatibility mode on Intel.
1627 * Normally we advertise the actual cpu vendor, but you can override
1628 * this if you want to use KVM's sysenter/syscall emulation
1629 * in compatibility mode and when doing cross vendor migration
1631 if (kvm_enabled() && ! env->cpuid_vendor_override) {
1632 host_cpuid(0, 0, NULL, ebx, ecx, edx);
1636 void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
1637 uint32_t *eax, uint32_t *ebx,
1638 uint32_t *ecx, uint32_t *edx)
1640 X86CPU *cpu = x86_env_get_cpu(env);
1641 CPUState *cs = CPU(cpu);
1643 /* test if maximum index reached */
1644 if (index & 0x80000000) {
1645 if (index > env->cpuid_xlevel) {
1646 if (env->cpuid_xlevel2 > 0) {
1647 /* Handle the Centaur's CPUID instruction. */
1648 if (index > env->cpuid_xlevel2) {
1649 index = env->cpuid_xlevel2;
1650 } else if (index < 0xC0000000) {
1651 index = env->cpuid_xlevel;
1654 /* Intel documentation states that invalid EAX input will
1655 * return the same information as EAX=cpuid_level
1656 * (Intel SDM Vol. 2A - Instruction Set Reference - CPUID)
1658 index = env->cpuid_level;
1662 if (index > env->cpuid_level)
1663 index = env->cpuid_level;
1668 *eax = env->cpuid_level;
1669 get_cpuid_vendor(env, ebx, ecx, edx);
1672 *eax = env->cpuid_version;
1673 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1674 *ecx = env->cpuid_ext_features;
1675 *edx = env->cpuid_features;
1676 if (env->nr_cores * env->nr_threads > 1) {
1677 *ebx |= (env->nr_cores * env->nr_threads) << 16;
1678 *edx |= 1 << 28; /* HTT bit */
1682 /* cache info: needed for Pentium Pro compatibility */
1689 /* cache info: needed for Core compatibility */
1690 if (env->nr_cores > 1) {
1691 *eax = (env->nr_cores - 1) << 26;
1696 case 0: /* L1 dcache info */
1702 case 1: /* L1 icache info */
1708 case 2: /* L2 cache info */
1710 if (env->nr_threads > 1) {
1711 *eax |= (env->nr_threads - 1) << 14;
1717 default: /* end of info */
1726 /* mwait info: needed for Core compatibility */
1727 *eax = 0; /* Smallest monitor-line size in bytes */
1728 *ebx = 0; /* Largest monitor-line size in bytes */
1729 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
1733 /* Thermal and Power Leaf */
1740 /* Structured Extended Feature Flags Enumeration Leaf */
1742 *eax = 0; /* Maximum ECX value for sub-leaves */
1743 *ebx = env->cpuid_7_0_ebx_features; /* Feature flags */
1744 *ecx = 0; /* Reserved */
1745 *edx = 0; /* Reserved */
1754 /* Direct Cache Access Information Leaf */
1755 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1761 /* Architectural Performance Monitoring Leaf */
1762 if (kvm_enabled()) {
1763 KVMState *s = cs->kvm_state;
1765 *eax = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EAX);
1766 *ebx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EBX);
1767 *ecx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_ECX);
1768 *edx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EDX);
1777 /* Processor Extended State */
1778 if (!(env->cpuid_ext_features & CPUID_EXT_XSAVE)) {
1785 if (kvm_enabled()) {
1786 KVMState *s = cs->kvm_state;
1788 *eax = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EAX);
1789 *ebx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EBX);
1790 *ecx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_ECX);
1791 *edx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EDX);
1800 *eax = env->cpuid_xlevel;
1801 *ebx = env->cpuid_vendor1;
1802 *edx = env->cpuid_vendor2;
1803 *ecx = env->cpuid_vendor3;
1806 *eax = env->cpuid_version;
1808 *ecx = env->cpuid_ext3_features;
1809 *edx = env->cpuid_ext2_features;
1811 /* The Linux kernel checks for the CMPLegacy bit and
1812 * discards multiple thread information if it is set.
1813 * So dont set it here for Intel to make Linux guests happy.
1815 if (env->nr_cores * env->nr_threads > 1) {
1816 uint32_t tebx, tecx, tedx;
1817 get_cpuid_vendor(env, &tebx, &tecx, &tedx);
1818 if (tebx != CPUID_VENDOR_INTEL_1 ||
1819 tedx != CPUID_VENDOR_INTEL_2 ||
1820 tecx != CPUID_VENDOR_INTEL_3) {
1821 *ecx |= 1 << 1; /* CmpLegacy bit */
1828 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
1829 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
1830 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
1831 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
1834 /* cache info (L1 cache) */
1841 /* cache info (L2 cache) */
1848 /* virtual & phys address size in low 2 bytes. */
1849 /* XXX: This value must match the one used in the MMU code. */
1850 if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
1851 /* 64 bit processor */
1852 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1853 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
1855 if (env->cpuid_features & CPUID_PSE36)
1856 *eax = 0x00000024; /* 36 bits physical */
1858 *eax = 0x00000020; /* 32 bits physical */
1863 if (env->nr_cores * env->nr_threads > 1) {
1864 *ecx |= (env->nr_cores * env->nr_threads) - 1;
1868 if (env->cpuid_ext3_features & CPUID_EXT3_SVM) {
1869 *eax = 0x00000001; /* SVM Revision */
1870 *ebx = 0x00000010; /* nr of ASIDs */
1872 *edx = env->cpuid_svm_features; /* optional features */
1881 *eax = env->cpuid_xlevel2;
1887 /* Support for VIA CPU's CPUID instruction */
1888 *eax = env->cpuid_version;
1891 *edx = env->cpuid_ext4_features;
1896 /* Reserved for the future, and now filled with zero */
1903 /* reserved values: zero */
1912 /* CPUClass::reset() */
1913 static void x86_cpu_reset(CPUState *s)
1915 X86CPU *cpu = X86_CPU(s);
1916 X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
1917 CPUX86State *env = &cpu->env;
1920 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
1921 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
1922 log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
1925 xcc->parent_reset(s);
1928 memset(env, 0, offsetof(CPUX86State, breakpoints));
1932 env->old_exception = -1;
1934 /* init to reset state */
1936 #ifdef CONFIG_SOFTMMU
1937 env->hflags |= HF_SOFTMMU_MASK;
1939 env->hflags2 |= HF2_GIF_MASK;
1941 cpu_x86_update_cr0(env, 0x60000010);
1942 env->a20_mask = ~0x0;
1943 env->smbase = 0x30000;
1945 env->idt.limit = 0xffff;
1946 env->gdt.limit = 0xffff;
1947 env->ldt.limit = 0xffff;
1948 env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
1949 env->tr.limit = 0xffff;
1950 env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
1952 cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
1953 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK |
1954 DESC_R_MASK | DESC_A_MASK);
1955 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
1956 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1958 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
1959 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1961 cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
1962 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1964 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
1965 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1967 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
1968 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1972 env->regs[R_EDX] = env->cpuid_version;
1977 for (i = 0; i < 8; i++) {
1982 env->mxcsr = 0x1f80;
1984 env->pat = 0x0007040600070406ULL;
1985 env->msr_ia32_misc_enable = MSR_IA32_MISC_ENABLE_DEFAULT;
1987 memset(env->dr, 0, sizeof(env->dr));
1988 env->dr[6] = DR6_FIXED_1;
1989 env->dr[7] = DR7_FIXED_1;
1990 cpu_breakpoint_remove_all(env, BP_CPU);
1991 cpu_watchpoint_remove_all(env, BP_CPU);
1993 #if !defined(CONFIG_USER_ONLY)
1994 /* We hard-wire the BSP to the first CPU. */
1995 if (env->cpu_index == 0) {
1996 apic_designate_bsp(env->apic_state);
1999 env->halted = !cpu_is_bsp(cpu);
2003 #ifndef CONFIG_USER_ONLY
2004 bool cpu_is_bsp(X86CPU *cpu)
2006 return cpu_get_apic_base(cpu->env.apic_state) & MSR_IA32_APICBASE_BSP;
2009 /* TODO: remove me, when reset over QOM tree is implemented */
2010 static void x86_cpu_machine_reset_cb(void *opaque)
2012 X86CPU *cpu = opaque;
2013 cpu_reset(CPU(cpu));
2017 static void mce_init(X86CPU *cpu)
2019 CPUX86State *cenv = &cpu->env;
2022 if (((cenv->cpuid_version >> 8) & 0xf) >= 6
2023 && (cenv->cpuid_features & (CPUID_MCE | CPUID_MCA)) ==
2024 (CPUID_MCE | CPUID_MCA)) {
2025 cenv->mcg_cap = MCE_CAP_DEF | MCE_BANKS_DEF;
2026 cenv->mcg_ctl = ~(uint64_t)0;
2027 for (bank = 0; bank < MCE_BANKS_DEF; bank++) {
2028 cenv->mce_banks[bank * 4] = ~(uint64_t)0;
2033 #define MSI_ADDR_BASE 0xfee00000
2035 #ifndef CONFIG_USER_ONLY
2036 static void x86_cpu_apic_init(X86CPU *cpu, Error **errp)
2038 static int apic_mapped;
2039 CPUX86State *env = &cpu->env;
2040 APICCommonState *apic;
2041 const char *apic_type = "apic";
2043 if (kvm_irqchip_in_kernel()) {
2044 apic_type = "kvm-apic";
2045 } else if (xen_enabled()) {
2046 apic_type = "xen-apic";
2049 env->apic_state = qdev_try_create(NULL, apic_type);
2050 if (env->apic_state == NULL) {
2051 error_setg(errp, "APIC device '%s' could not be created", apic_type);
2055 object_property_add_child(OBJECT(cpu), "apic",
2056 OBJECT(env->apic_state), NULL);
2057 qdev_prop_set_uint8(env->apic_state, "id", env->cpuid_apic_id);
2058 /* TODO: convert to link<> */
2059 apic = APIC_COMMON(env->apic_state);
2062 if (qdev_init(env->apic_state)) {
2063 error_setg(errp, "APIC device '%s' could not be initialized",
2064 object_get_typename(OBJECT(env->apic_state)));
2068 /* XXX: mapping more APICs at the same memory location */
2069 if (apic_mapped == 0) {
2070 /* NOTE: the APIC is directly connected to the CPU - it is not
2071 on the global memory bus. */
2072 /* XXX: what if the base changes? */
2073 sysbus_mmio_map(sysbus_from_qdev(env->apic_state), 0, MSI_ADDR_BASE);
2079 void x86_cpu_realize(Object *obj, Error **errp)
2081 X86CPU *cpu = X86_CPU(obj);
2082 CPUX86State *env = &cpu->env;
2084 if (env->cpuid_7_0_ebx_features && env->cpuid_level < 7) {
2085 env->cpuid_level = 7;
2088 #ifndef CONFIG_USER_ONLY
2089 qemu_register_reset(x86_cpu_machine_reset_cb, cpu);
2091 if (cpu->env.cpuid_features & CPUID_APIC || smp_cpus > 1) {
2092 x86_cpu_apic_init(cpu, errp);
2093 if (error_is_set(errp)) {
2100 qemu_init_vcpu(&cpu->env);
2101 cpu_reset(CPU(cpu));
2104 static void x86_cpu_initfn(Object *obj)
2106 X86CPU *cpu = X86_CPU(obj);
2107 CPUX86State *env = &cpu->env;
2112 object_property_add(obj, "family", "int",
2113 x86_cpuid_version_get_family,
2114 x86_cpuid_version_set_family, NULL, NULL, NULL);
2115 object_property_add(obj, "model", "int",
2116 x86_cpuid_version_get_model,
2117 x86_cpuid_version_set_model, NULL, NULL, NULL);
2118 object_property_add(obj, "stepping", "int",
2119 x86_cpuid_version_get_stepping,
2120 x86_cpuid_version_set_stepping, NULL, NULL, NULL);
2121 object_property_add(obj, "level", "int",
2122 x86_cpuid_get_level,
2123 x86_cpuid_set_level, NULL, NULL, NULL);
2124 object_property_add(obj, "xlevel", "int",
2125 x86_cpuid_get_xlevel,
2126 x86_cpuid_set_xlevel, NULL, NULL, NULL);
2127 object_property_add_str(obj, "vendor",
2128 x86_cpuid_get_vendor,
2129 x86_cpuid_set_vendor, NULL);
2130 object_property_add_str(obj, "model-id",
2131 x86_cpuid_get_model_id,
2132 x86_cpuid_set_model_id, NULL);
2133 object_property_add(obj, "tsc-frequency", "int",
2134 x86_cpuid_get_tsc_freq,
2135 x86_cpuid_set_tsc_freq, NULL, NULL, NULL);
2137 env->cpuid_apic_id = env->cpu_index;
2139 /* init various static tables used in TCG mode */
2140 if (tcg_enabled() && !inited) {
2142 optimize_flags_init();
2143 #ifndef CONFIG_USER_ONLY
2144 cpu_set_debug_excp_handler(breakpoint_handler);
2149 static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
2151 X86CPUClass *xcc = X86_CPU_CLASS(oc);
2152 CPUClass *cc = CPU_CLASS(oc);
2154 xcc->parent_reset = cc->reset;
2155 cc->reset = x86_cpu_reset;
2158 static const TypeInfo x86_cpu_type_info = {
2159 .name = TYPE_X86_CPU,
2161 .instance_size = sizeof(X86CPU),
2162 .instance_init = x86_cpu_initfn,
2164 .class_size = sizeof(X86CPUClass),
2165 .class_init = x86_cpu_common_class_init,
2168 static void x86_cpu_register_types(void)
2170 type_register_static(&x86_cpu_type_info);
2173 type_init(x86_cpu_register_types)