2 * i386 CPUID helper functions
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "sysemu/kvm.h"
26 #include "sysemu/cpus.h"
29 #include "qemu/option.h"
30 #include "qemu/config-file.h"
31 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "sysemu/arch_init.h"
39 #if defined(CONFIG_KVM)
40 #include <linux/kvm_para.h>
43 #include "sysemu/sysemu.h"
44 #include "hw/qdev-properties.h"
45 #include "hw/cpu/icc_bus.h"
46 #ifndef CONFIG_USER_ONLY
47 #include "hw/xen/xen.h"
48 #include "hw/i386/apic_internal.h"
51 static void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
52 uint32_t vendor2, uint32_t vendor3)
55 for (i = 0; i < 4; i++) {
56 dst[i] = vendor1 >> (8 * i);
57 dst[i + 4] = vendor2 >> (8 * i);
58 dst[i + 8] = vendor3 >> (8 * i);
60 dst[CPUID_VENDOR_SZ] = '\0';
63 /* feature flags taken from "Intel Processor Identification and the CPUID
64 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
65 * between feature naming conventions, aliases may be added.
67 static const char *feature_name[] = {
68 "fpu", "vme", "de", "pse",
69 "tsc", "msr", "pae", "mce",
70 "cx8", "apic", NULL, "sep",
71 "mtrr", "pge", "mca", "cmov",
72 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
73 NULL, "ds" /* Intel dts */, "acpi", "mmx",
74 "fxsr", "sse", "sse2", "ss",
75 "ht" /* Intel htt */, "tm", "ia64", "pbe",
77 static const char *ext_feature_name[] = {
78 "pni|sse3" /* Intel,AMD sse3 */, "pclmulqdq|pclmuldq", "dtes64", "monitor",
79 "ds_cpl", "vmx", "smx", "est",
80 "tm2", "ssse3", "cid", NULL,
81 "fma", "cx16", "xtpr", "pdcm",
82 NULL, "pcid", "dca", "sse4.1|sse4_1",
83 "sse4.2|sse4_2", "x2apic", "movbe", "popcnt",
84 "tsc-deadline", "aes", "xsave", "osxsave",
85 "avx", "f16c", "rdrand", "hypervisor",
87 /* Feature names that are already defined on feature_name[] but are set on
88 * CPUID[8000_0001].EDX on AMD CPUs don't have their names on
89 * ext2_feature_name[]. They are copied automatically to cpuid_ext2_features
90 * if and only if CPU vendor is AMD.
92 static const char *ext2_feature_name[] = {
93 NULL /* fpu */, NULL /* vme */, NULL /* de */, NULL /* pse */,
94 NULL /* tsc */, NULL /* msr */, NULL /* pae */, NULL /* mce */,
95 NULL /* cx8 */ /* AMD CMPXCHG8B */, NULL /* apic */, NULL, "syscall",
96 NULL /* mtrr */, NULL /* pge */, NULL /* mca */, NULL /* cmov */,
97 NULL /* pat */, NULL /* pse36 */, NULL, NULL /* Linux mp */,
98 "nx|xd", NULL, "mmxext", NULL /* mmx */,
99 NULL /* fxsr */, "fxsr_opt|ffxsr", "pdpe1gb" /* AMD Page1GB */, "rdtscp",
100 NULL, "lm|i64", "3dnowext", "3dnow",
102 static const char *ext3_feature_name[] = {
103 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
104 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
105 "3dnowprefetch", "osvw", "ibs", "xop",
106 "skinit", "wdt", NULL, "lwp",
107 "fma4", "tce", NULL, "nodeid_msr",
108 NULL, "tbm", "topoext", "perfctr_core",
109 "perfctr_nb", NULL, NULL, NULL,
110 NULL, NULL, NULL, NULL,
113 static const char *ext4_feature_name[] = {
114 NULL, NULL, "xstore", "xstore-en",
115 NULL, NULL, "xcrypt", "xcrypt-en",
116 "ace2", "ace2-en", "phe", "phe-en",
117 "pmm", "pmm-en", NULL, NULL,
118 NULL, NULL, NULL, NULL,
119 NULL, NULL, NULL, NULL,
120 NULL, NULL, NULL, NULL,
121 NULL, NULL, NULL, NULL,
124 static const char *kvm_feature_name[] = {
125 "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock",
126 "kvm_asyncpf", "kvm_steal_time", "kvm_pv_eoi", NULL,
127 NULL, NULL, NULL, NULL,
128 NULL, NULL, NULL, NULL,
129 NULL, NULL, NULL, NULL,
130 NULL, NULL, NULL, NULL,
131 NULL, NULL, NULL, NULL,
132 NULL, NULL, NULL, NULL,
135 static const char *svm_feature_name[] = {
136 "npt", "lbrv", "svm_lock", "nrip_save",
137 "tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists",
138 NULL, NULL, "pause_filter", NULL,
139 "pfthreshold", NULL, NULL, NULL,
140 NULL, NULL, NULL, NULL,
141 NULL, NULL, NULL, NULL,
142 NULL, NULL, NULL, NULL,
143 NULL, NULL, NULL, NULL,
146 static const char *cpuid_7_0_ebx_feature_name[] = {
147 "fsgsbase", NULL, NULL, "bmi1", "hle", "avx2", NULL, "smep",
148 "bmi2", "erms", "invpcid", "rtm", NULL, NULL, NULL, NULL,
149 NULL, NULL, "rdseed", "adx", "smap", NULL, NULL, NULL,
150 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
153 typedef struct FeatureWordInfo {
154 const char **feat_names;
155 uint32_t cpuid_eax; /* Input EAX for CPUID */
156 bool cpuid_needs_ecx; /* CPUID instruction uses ECX as input */
157 uint32_t cpuid_ecx; /* Input ECX value for CPUID */
158 int cpuid_reg; /* output register (R_* constant) */
161 static FeatureWordInfo feature_word_info[FEATURE_WORDS] = {
163 .feat_names = feature_name,
164 .cpuid_eax = 1, .cpuid_reg = R_EDX,
167 .feat_names = ext_feature_name,
168 .cpuid_eax = 1, .cpuid_reg = R_ECX,
170 [FEAT_8000_0001_EDX] = {
171 .feat_names = ext2_feature_name,
172 .cpuid_eax = 0x80000001, .cpuid_reg = R_EDX,
174 [FEAT_8000_0001_ECX] = {
175 .feat_names = ext3_feature_name,
176 .cpuid_eax = 0x80000001, .cpuid_reg = R_ECX,
178 [FEAT_C000_0001_EDX] = {
179 .feat_names = ext4_feature_name,
180 .cpuid_eax = 0xC0000001, .cpuid_reg = R_EDX,
183 .feat_names = kvm_feature_name,
184 .cpuid_eax = KVM_CPUID_FEATURES, .cpuid_reg = R_EAX,
187 .feat_names = svm_feature_name,
188 .cpuid_eax = 0x8000000A, .cpuid_reg = R_EDX,
191 .feat_names = cpuid_7_0_ebx_feature_name,
193 .cpuid_needs_ecx = true, .cpuid_ecx = 0,
198 const char *get_register_name_32(unsigned int reg)
200 static const char *reg_names[CPU_NB_REGS32] = {
211 if (reg > CPU_NB_REGS32) {
214 return reg_names[reg];
217 /* collects per-function cpuid data
219 typedef struct model_features_t {
220 uint32_t *guest_feat;
222 FeatureWord feat_word;
226 int enforce_cpuid = 0;
228 static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
229 (1 << KVM_FEATURE_NOP_IO_DELAY) |
230 (1 << KVM_FEATURE_CLOCKSOURCE2) |
231 (1 << KVM_FEATURE_ASYNC_PF) |
232 (1 << KVM_FEATURE_STEAL_TIME) |
233 (1 << KVM_FEATURE_PV_EOI) |
234 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
236 void disable_kvm_pv_eoi(void)
238 kvm_default_features &= ~(1UL << KVM_FEATURE_PV_EOI);
241 void host_cpuid(uint32_t function, uint32_t count,
242 uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
244 #if defined(CONFIG_KVM)
249 : "=a"(vec[0]), "=b"(vec[1]),
250 "=c"(vec[2]), "=d"(vec[3])
251 : "0"(function), "c"(count) : "cc");
253 asm volatile("pusha \n\t"
255 "mov %%eax, 0(%2) \n\t"
256 "mov %%ebx, 4(%2) \n\t"
257 "mov %%ecx, 8(%2) \n\t"
258 "mov %%edx, 12(%2) \n\t"
260 : : "a"(function), "c"(count), "S"(vec)
275 #define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
277 /* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
278 * a substring. ex if !NULL points to the first char after a substring,
279 * otherwise the string is assumed to sized by a terminating nul.
280 * Return lexical ordering of *s1:*s2.
282 static int sstrcmp(const char *s1, const char *e1, const char *s2,
286 if (!*s1 || !*s2 || *s1 != *s2)
289 if (s1 == e1 && s2 == e2)
298 /* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
299 * '|' delimited (possibly empty) strings in which case search for a match
300 * within the alternatives proceeds left to right. Return 0 for success,
301 * non-zero otherwise.
303 static int altcmp(const char *s, const char *e, const char *altstr)
307 for (q = p = altstr; ; ) {
308 while (*p && *p != '|')
310 if ((q == p && !*s) || (q != p && !sstrcmp(s, e, q, p)))
319 /* search featureset for flag *[s..e), if found set corresponding bit in
320 * *pval and return true, otherwise return false
322 static bool lookup_feature(uint32_t *pval, const char *s, const char *e,
323 const char **featureset)
329 for (mask = 1, ppc = featureset; mask; mask <<= 1, ++ppc) {
330 if (*ppc && !altcmp(s, e, *ppc)) {
338 static void add_flagname_to_bitmaps(const char *flagname,
339 FeatureWordArray words)
342 for (w = 0; w < FEATURE_WORDS; w++) {
343 FeatureWordInfo *wi = &feature_word_info[w];
344 if (wi->feat_names &&
345 lookup_feature(&words[w], flagname, NULL, wi->feat_names)) {
349 if (w == FEATURE_WORDS) {
350 fprintf(stderr, "CPU feature %s not found\n", flagname);
354 typedef struct x86_def_t {
359 /* vendor is zero-terminated, 12 character ASCII string */
360 char vendor[CPUID_VENDOR_SZ + 1];
364 FeatureWordArray features;
368 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
369 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
370 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
371 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
372 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
373 CPUID_PSE36 | CPUID_FXSR)
374 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
375 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
376 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
377 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
378 CPUID_PAE | CPUID_SEP | CPUID_APIC)
380 #define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
381 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
382 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
383 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
384 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
385 /* partly implemented:
386 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
387 CPUID_PSE36 (needed for Solaris) */
389 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
390 #define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_PCLMULQDQ | \
391 CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | \
392 CPUID_EXT_SSE41 | CPUID_EXT_SSE42 | CPUID_EXT_POPCNT | \
393 CPUID_EXT_MOVBE | CPUID_EXT_AES | CPUID_EXT_HYPERVISOR)
395 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_SMX,
396 CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_CID, CPUID_EXT_FMA,
397 CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_PCID, CPUID_EXT_DCA,
398 CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER, CPUID_EXT_XSAVE,
399 CPUID_EXT_OSXSAVE, CPUID_EXT_AVX, CPUID_EXT_F16C,
401 #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
402 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
403 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
405 CPUID_EXT2_PDPE1GB */
406 #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
407 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
408 #define TCG_SVM_FEATURES 0
409 #define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP \
410 CPUID_7_0_EBX_BMI1 | CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ADX)
412 CPUID_7_0_EBX_FSGSBASE, CPUID_7_0_EBX_HLE, CPUID_7_0_EBX_AVX2,
413 CPUID_7_0_EBX_ERMS, CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM,
414 CPUID_7_0_EBX_RDSEED */
416 /* built-in CPU model definitions
418 static x86_def_t builtin_x86_defs[] = {
422 .vendor = CPUID_VENDOR_AMD,
426 .features[FEAT_1_EDX] =
428 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
430 .features[FEAT_1_ECX] =
431 CPUID_EXT_SSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT,
432 .features[FEAT_8000_0001_EDX] =
433 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
434 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
435 .features[FEAT_8000_0001_ECX] =
436 CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
437 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
438 .xlevel = 0x8000000A,
443 .vendor = CPUID_VENDOR_AMD,
447 .features[FEAT_1_EDX] =
449 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
450 CPUID_PSE36 | CPUID_VME | CPUID_HT,
451 .features[FEAT_1_ECX] =
452 CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_CX16 |
454 .features[FEAT_8000_0001_EDX] =
455 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
456 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
457 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
458 CPUID_EXT2_FFXSR | CPUID_EXT2_PDPE1GB | CPUID_EXT2_RDTSCP,
459 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
461 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
462 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
463 .features[FEAT_8000_0001_ECX] =
464 CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
465 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
466 .features[FEAT_SVM] =
467 CPUID_SVM_NPT | CPUID_SVM_LBRV,
468 .xlevel = 0x8000001A,
469 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
474 .vendor = CPUID_VENDOR_INTEL,
478 .features[FEAT_1_EDX] =
480 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
481 CPUID_PSE36 | CPUID_VME | CPUID_DTS | CPUID_ACPI | CPUID_SS |
482 CPUID_HT | CPUID_TM | CPUID_PBE,
483 .features[FEAT_1_ECX] =
484 CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
485 CPUID_EXT_DTES64 | CPUID_EXT_DSCPL | CPUID_EXT_VMX | CPUID_EXT_EST |
486 CPUID_EXT_TM2 | CPUID_EXT_CX16 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
487 .features[FEAT_8000_0001_EDX] =
488 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
489 .features[FEAT_8000_0001_ECX] =
491 .xlevel = 0x80000008,
492 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
497 .vendor = CPUID_VENDOR_INTEL,
501 /* Missing: CPUID_VME, CPUID_HT */
502 .features[FEAT_1_EDX] =
504 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
506 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
507 .features[FEAT_1_ECX] =
508 CPUID_EXT_SSE3 | CPUID_EXT_CX16,
509 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
510 .features[FEAT_8000_0001_EDX] =
511 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
512 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
513 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
514 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
515 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
516 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
517 .features[FEAT_8000_0001_ECX] =
519 .xlevel = 0x80000008,
520 .model_id = "Common KVM processor"
525 .vendor = CPUID_VENDOR_INTEL,
529 .features[FEAT_1_EDX] =
531 .features[FEAT_1_ECX] =
532 CPUID_EXT_SSE3 | CPUID_EXT_POPCNT,
533 .xlevel = 0x80000004,
538 .vendor = CPUID_VENDOR_INTEL,
542 .features[FEAT_1_EDX] =
544 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_PSE36,
545 .features[FEAT_1_ECX] =
547 .features[FEAT_8000_0001_EDX] =
548 PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES,
549 .features[FEAT_8000_0001_ECX] =
551 .xlevel = 0x80000008,
552 .model_id = "Common 32-bit KVM processor"
557 .vendor = CPUID_VENDOR_INTEL,
561 .features[FEAT_1_EDX] =
562 PPRO_FEATURES | CPUID_VME |
563 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_DTS | CPUID_ACPI |
564 CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
565 .features[FEAT_1_ECX] =
566 CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_VMX |
567 CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
568 .features[FEAT_8000_0001_EDX] =
570 .xlevel = 0x80000008,
571 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
576 .vendor = CPUID_VENDOR_INTEL,
580 .features[FEAT_1_EDX] =
587 .vendor = CPUID_VENDOR_INTEL,
591 .features[FEAT_1_EDX] =
598 .vendor = CPUID_VENDOR_INTEL,
602 .features[FEAT_1_EDX] =
609 .vendor = CPUID_VENDOR_INTEL,
613 .features[FEAT_1_EDX] =
620 .vendor = CPUID_VENDOR_AMD,
624 .features[FEAT_1_EDX] =
625 PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR |
627 .features[FEAT_8000_0001_EDX] =
628 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
629 CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
630 .xlevel = 0x80000008,
634 /* original is on level 10 */
636 .vendor = CPUID_VENDOR_INTEL,
640 .features[FEAT_1_EDX] =
642 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME | CPUID_DTS |
643 CPUID_ACPI | CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
644 /* Some CPUs got no CPUID_SEP */
645 .features[FEAT_1_ECX] =
646 CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
647 CPUID_EXT_DSCPL | CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR,
648 .features[FEAT_8000_0001_EDX] =
649 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
651 .features[FEAT_8000_0001_ECX] =
653 .xlevel = 0x8000000A,
654 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
659 .vendor = CPUID_VENDOR_INTEL,
663 .features[FEAT_1_EDX] =
664 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
665 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
666 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
667 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
668 CPUID_DE | CPUID_FP87,
669 .features[FEAT_1_ECX] =
670 CPUID_EXT_SSSE3 | CPUID_EXT_SSE3,
671 .features[FEAT_8000_0001_EDX] =
672 CPUID_EXT2_LM | CPUID_EXT2_NX | CPUID_EXT2_SYSCALL,
673 .features[FEAT_8000_0001_ECX] =
675 .xlevel = 0x8000000A,
676 .model_id = "Intel Celeron_4x0 (Conroe/Merom Class Core 2)",
681 .vendor = CPUID_VENDOR_INTEL,
685 .features[FEAT_1_EDX] =
686 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
687 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
688 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
689 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
690 CPUID_DE | CPUID_FP87,
691 .features[FEAT_1_ECX] =
692 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
694 .features[FEAT_8000_0001_EDX] =
695 CPUID_EXT2_LM | CPUID_EXT2_NX | CPUID_EXT2_SYSCALL,
696 .features[FEAT_8000_0001_ECX] =
698 .xlevel = 0x8000000A,
699 .model_id = "Intel Core 2 Duo P9xxx (Penryn Class Core 2)",
704 .vendor = CPUID_VENDOR_INTEL,
708 .features[FEAT_1_EDX] =
709 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
710 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
711 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
712 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
713 CPUID_DE | CPUID_FP87,
714 .features[FEAT_1_ECX] =
715 CPUID_EXT_POPCNT | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
716 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_SSE3,
717 .features[FEAT_8000_0001_EDX] =
718 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
719 .features[FEAT_8000_0001_ECX] =
721 .xlevel = 0x8000000A,
722 .model_id = "Intel Core i7 9xx (Nehalem Class Core i7)",
727 .vendor = CPUID_VENDOR_INTEL,
731 .features[FEAT_1_EDX] =
732 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 .features[FEAT_1_ECX] =
738 CPUID_EXT_AES | CPUID_EXT_POPCNT | CPUID_EXT_SSE42 |
739 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
740 CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3,
741 .features[FEAT_8000_0001_EDX] =
742 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
743 .features[FEAT_8000_0001_ECX] =
745 .xlevel = 0x8000000A,
746 .model_id = "Westmere E56xx/L56xx/X56xx (Nehalem-C)",
749 .name = "SandyBridge",
751 .vendor = CPUID_VENDOR_INTEL,
755 .features[FEAT_1_EDX] =
756 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
757 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
758 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
759 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
760 CPUID_DE | CPUID_FP87,
761 .features[FEAT_1_ECX] =
762 CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
763 CPUID_EXT_TSC_DEADLINE_TIMER | CPUID_EXT_POPCNT |
764 CPUID_EXT_X2APIC | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
765 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ |
767 .features[FEAT_8000_0001_EDX] =
768 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_NX |
770 .features[FEAT_8000_0001_ECX] =
772 .xlevel = 0x8000000A,
773 .model_id = "Intel Xeon E312xx (Sandy Bridge)",
778 .vendor = CPUID_VENDOR_INTEL,
782 .features[FEAT_1_EDX] =
783 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
784 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
785 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
786 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
787 CPUID_DE | CPUID_FP87,
788 .features[FEAT_1_ECX] =
789 CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
790 CPUID_EXT_POPCNT | CPUID_EXT_X2APIC | CPUID_EXT_SSE42 |
791 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
792 CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3 |
793 CPUID_EXT_TSC_DEADLINE_TIMER | CPUID_EXT_FMA | CPUID_EXT_MOVBE |
795 .features[FEAT_8000_0001_EDX] =
796 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_NX |
798 .features[FEAT_8000_0001_ECX] =
800 .features[FEAT_7_0_EBX] =
801 CPUID_7_0_EBX_FSGSBASE | CPUID_7_0_EBX_BMI1 |
802 CPUID_7_0_EBX_HLE | CPUID_7_0_EBX_AVX2 | CPUID_7_0_EBX_SMEP |
803 CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_INVPCID |
805 .xlevel = 0x8000000A,
806 .model_id = "Intel Core Processor (Haswell)",
809 .name = "Opteron_G1",
811 .vendor = CPUID_VENDOR_AMD,
815 .features[FEAT_1_EDX] =
816 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
817 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
818 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
819 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
820 CPUID_DE | CPUID_FP87,
821 .features[FEAT_1_ECX] =
823 .features[FEAT_8000_0001_EDX] =
824 CPUID_EXT2_LM | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
825 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
826 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
827 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
828 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
829 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
830 .xlevel = 0x80000008,
831 .model_id = "AMD Opteron 240 (Gen 1 Class Opteron)",
834 .name = "Opteron_G2",
836 .vendor = CPUID_VENDOR_AMD,
840 .features[FEAT_1_EDX] =
841 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
842 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
843 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
844 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
845 CPUID_DE | CPUID_FP87,
846 .features[FEAT_1_ECX] =
847 CPUID_EXT_CX16 | CPUID_EXT_SSE3,
848 .features[FEAT_8000_0001_EDX] =
849 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_FXSR |
850 CPUID_EXT2_MMX | CPUID_EXT2_NX | CPUID_EXT2_PSE36 |
851 CPUID_EXT2_PAT | CPUID_EXT2_CMOV | CPUID_EXT2_MCA |
852 CPUID_EXT2_PGE | CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL |
853 CPUID_EXT2_APIC | CPUID_EXT2_CX8 | CPUID_EXT2_MCE |
854 CPUID_EXT2_PAE | CPUID_EXT2_MSR | CPUID_EXT2_TSC | CPUID_EXT2_PSE |
855 CPUID_EXT2_DE | CPUID_EXT2_FPU,
856 .features[FEAT_8000_0001_ECX] =
857 CPUID_EXT3_SVM | CPUID_EXT3_LAHF_LM,
858 .xlevel = 0x80000008,
859 .model_id = "AMD Opteron 22xx (Gen 2 Class Opteron)",
862 .name = "Opteron_G3",
864 .vendor = CPUID_VENDOR_AMD,
868 .features[FEAT_1_EDX] =
869 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
870 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
871 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
872 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
873 CPUID_DE | CPUID_FP87,
874 .features[FEAT_1_ECX] =
875 CPUID_EXT_POPCNT | CPUID_EXT_CX16 | CPUID_EXT_MONITOR |
877 .features[FEAT_8000_0001_EDX] =
878 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_FXSR |
879 CPUID_EXT2_MMX | CPUID_EXT2_NX | CPUID_EXT2_PSE36 |
880 CPUID_EXT2_PAT | CPUID_EXT2_CMOV | CPUID_EXT2_MCA |
881 CPUID_EXT2_PGE | CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL |
882 CPUID_EXT2_APIC | CPUID_EXT2_CX8 | CPUID_EXT2_MCE |
883 CPUID_EXT2_PAE | CPUID_EXT2_MSR | CPUID_EXT2_TSC | CPUID_EXT2_PSE |
884 CPUID_EXT2_DE | CPUID_EXT2_FPU,
885 .features[FEAT_8000_0001_ECX] =
886 CPUID_EXT3_MISALIGNSSE | CPUID_EXT3_SSE4A |
887 CPUID_EXT3_ABM | CPUID_EXT3_SVM | CPUID_EXT3_LAHF_LM,
888 .xlevel = 0x80000008,
889 .model_id = "AMD Opteron 23xx (Gen 3 Class Opteron)",
892 .name = "Opteron_G4",
894 .vendor = CPUID_VENDOR_AMD,
898 .features[FEAT_1_EDX] =
899 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
900 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
901 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
902 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
903 CPUID_DE | CPUID_FP87,
904 .features[FEAT_1_ECX] =
905 CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
906 CPUID_EXT_POPCNT | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
907 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ |
909 .features[FEAT_8000_0001_EDX] =
910 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP |
911 CPUID_EXT2_PDPE1GB | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
912 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
913 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
914 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
915 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
916 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
917 .features[FEAT_8000_0001_ECX] =
918 CPUID_EXT3_FMA4 | CPUID_EXT3_XOP |
919 CPUID_EXT3_3DNOWPREFETCH | CPUID_EXT3_MISALIGNSSE |
920 CPUID_EXT3_SSE4A | CPUID_EXT3_ABM | CPUID_EXT3_SVM |
922 .xlevel = 0x8000001A,
923 .model_id = "AMD Opteron 62xx class CPU",
926 .name = "Opteron_G5",
928 .vendor = CPUID_VENDOR_AMD,
932 .features[FEAT_1_EDX] =
933 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
934 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
935 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
936 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
937 CPUID_DE | CPUID_FP87,
938 .features[FEAT_1_ECX] =
939 CPUID_EXT_F16C | CPUID_EXT_AVX | CPUID_EXT_XSAVE |
940 CPUID_EXT_AES | CPUID_EXT_POPCNT | CPUID_EXT_SSE42 |
941 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_FMA |
942 CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3,
943 .features[FEAT_8000_0001_EDX] =
944 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP |
945 CPUID_EXT2_PDPE1GB | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
946 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
947 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
948 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
949 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
950 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
951 .features[FEAT_8000_0001_ECX] =
952 CPUID_EXT3_TBM | CPUID_EXT3_FMA4 | CPUID_EXT3_XOP |
953 CPUID_EXT3_3DNOWPREFETCH | CPUID_EXT3_MISALIGNSSE |
954 CPUID_EXT3_SSE4A | CPUID_EXT3_ABM | CPUID_EXT3_SVM |
956 .xlevel = 0x8000001A,
957 .model_id = "AMD Opteron 63xx class CPU",
962 static int cpu_x86_fill_model_id(char *str)
964 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
967 for (i = 0; i < 3; i++) {
968 host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx);
969 memcpy(str + i * 16 + 0, &eax, 4);
970 memcpy(str + i * 16 + 4, &ebx, 4);
971 memcpy(str + i * 16 + 8, &ecx, 4);
972 memcpy(str + i * 16 + 12, &edx, 4);
978 /* Fill a x86_def_t struct with information about the host CPU, and
979 * the CPU features supported by the host hardware + host kernel
981 * This function may be called only if KVM is enabled.
983 static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
986 KVMState *s = kvm_state;
987 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
989 assert(kvm_enabled());
991 x86_cpu_def->name = "host";
992 host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
993 x86_cpu_vendor_words2str(x86_cpu_def->vendor, ebx, edx, ecx);
995 host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
996 x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
997 x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
998 x86_cpu_def->stepping = eax & 0x0F;
1000 x86_cpu_def->level = kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX);
1001 x86_cpu_def->features[FEAT_1_EDX] =
1002 kvm_arch_get_supported_cpuid(s, 0x1, 0, R_EDX);
1003 x86_cpu_def->features[FEAT_1_ECX] =
1004 kvm_arch_get_supported_cpuid(s, 0x1, 0, R_ECX);
1006 if (x86_cpu_def->level >= 7) {
1007 x86_cpu_def->features[FEAT_7_0_EBX] =
1008 kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX);
1010 x86_cpu_def->features[FEAT_7_0_EBX] = 0;
1013 x86_cpu_def->xlevel = kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX);
1014 x86_cpu_def->features[FEAT_8000_0001_EDX] =
1015 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX);
1016 x86_cpu_def->features[FEAT_8000_0001_ECX] =
1017 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX);
1019 cpu_x86_fill_model_id(x86_cpu_def->model_id);
1021 /* Call Centaur's CPUID instruction. */
1022 if (!strcmp(x86_cpu_def->vendor, CPUID_VENDOR_VIA)) {
1023 host_cpuid(0xC0000000, 0, &eax, &ebx, &ecx, &edx);
1024 eax = kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
1025 if (eax >= 0xC0000001) {
1026 /* Support VIA max extended level */
1027 x86_cpu_def->xlevel2 = eax;
1028 host_cpuid(0xC0000001, 0, &eax, &ebx, &ecx, &edx);
1029 x86_cpu_def->features[FEAT_C000_0001_EDX] =
1030 kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
1034 /* Other KVM-specific feature fields: */
1035 x86_cpu_def->features[FEAT_SVM] =
1036 kvm_arch_get_supported_cpuid(s, 0x8000000A, 0, R_EDX);
1037 x86_cpu_def->features[FEAT_KVM] =
1038 kvm_arch_get_supported_cpuid(s, KVM_CPUID_FEATURES, 0, R_EAX);
1040 #endif /* CONFIG_KVM */
1043 static int unavailable_host_feature(FeatureWordInfo *f, uint32_t mask)
1047 for (i = 0; i < 32; ++i)
1048 if (1 << i & mask) {
1049 const char *reg = get_register_name_32(f->cpuid_reg);
1051 fprintf(stderr, "warning: host doesn't support requested feature: "
1052 "CPUID.%02XH:%s%s%s [bit %d]\n",
1054 f->feat_names[i] ? "." : "",
1055 f->feat_names[i] ? f->feat_names[i] : "", i);
1061 /* Check if all requested cpu flags are making their way to the guest
1063 * Returns 0 if all flags are supported by the host, non-zero otherwise.
1065 * This function may be called only if KVM is enabled.
1067 static int kvm_check_features_against_host(X86CPU *cpu)
1069 CPUX86State *env = &cpu->env;
1073 struct model_features_t ft[] = {
1074 {&env->features[FEAT_1_EDX],
1075 &host_def.features[FEAT_1_EDX],
1077 {&env->features[FEAT_1_ECX],
1078 &host_def.features[FEAT_1_ECX],
1080 {&env->features[FEAT_8000_0001_EDX],
1081 &host_def.features[FEAT_8000_0001_EDX],
1082 FEAT_8000_0001_EDX },
1083 {&env->features[FEAT_8000_0001_ECX],
1084 &host_def.features[FEAT_8000_0001_ECX],
1085 FEAT_8000_0001_ECX },
1086 {&env->features[FEAT_C000_0001_EDX],
1087 &host_def.features[FEAT_C000_0001_EDX],
1088 FEAT_C000_0001_EDX },
1089 {&env->features[FEAT_7_0_EBX],
1090 &host_def.features[FEAT_7_0_EBX],
1092 {&env->features[FEAT_SVM],
1093 &host_def.features[FEAT_SVM],
1095 {&env->features[FEAT_KVM],
1096 &host_def.features[FEAT_KVM],
1100 assert(kvm_enabled());
1102 kvm_cpu_fill_host(&host_def);
1103 for (rv = 0, i = 0; i < ARRAY_SIZE(ft); ++i) {
1104 FeatureWord w = ft[i].feat_word;
1105 FeatureWordInfo *wi = &feature_word_info[w];
1106 for (mask = 1; mask; mask <<= 1) {
1107 if (*ft[i].guest_feat & mask &&
1108 !(*ft[i].host_feat & mask)) {
1109 unavailable_host_feature(wi, mask);
1117 static void x86_cpuid_version_get_family(Object *obj, Visitor *v, void *opaque,
1118 const char *name, Error **errp)
1120 X86CPU *cpu = X86_CPU(obj);
1121 CPUX86State *env = &cpu->env;
1124 value = (env->cpuid_version >> 8) & 0xf;
1126 value += (env->cpuid_version >> 20) & 0xff;
1128 visit_type_int(v, &value, name, errp);
1131 static void x86_cpuid_version_set_family(Object *obj, Visitor *v, void *opaque,
1132 const char *name, Error **errp)
1134 X86CPU *cpu = X86_CPU(obj);
1135 CPUX86State *env = &cpu->env;
1136 const int64_t min = 0;
1137 const int64_t max = 0xff + 0xf;
1140 visit_type_int(v, &value, name, errp);
1141 if (error_is_set(errp)) {
1144 if (value < min || value > max) {
1145 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1146 name ? name : "null", value, min, max);
1150 env->cpuid_version &= ~0xff00f00;
1152 env->cpuid_version |= 0xf00 | ((value - 0x0f) << 20);
1154 env->cpuid_version |= value << 8;
1158 static void x86_cpuid_version_get_model(Object *obj, Visitor *v, void *opaque,
1159 const char *name, Error **errp)
1161 X86CPU *cpu = X86_CPU(obj);
1162 CPUX86State *env = &cpu->env;
1165 value = (env->cpuid_version >> 4) & 0xf;
1166 value |= ((env->cpuid_version >> 16) & 0xf) << 4;
1167 visit_type_int(v, &value, name, errp);
1170 static void x86_cpuid_version_set_model(Object *obj, Visitor *v, void *opaque,
1171 const char *name, Error **errp)
1173 X86CPU *cpu = X86_CPU(obj);
1174 CPUX86State *env = &cpu->env;
1175 const int64_t min = 0;
1176 const int64_t max = 0xff;
1179 visit_type_int(v, &value, name, errp);
1180 if (error_is_set(errp)) {
1183 if (value < min || value > max) {
1184 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1185 name ? name : "null", value, min, max);
1189 env->cpuid_version &= ~0xf00f0;
1190 env->cpuid_version |= ((value & 0xf) << 4) | ((value >> 4) << 16);
1193 static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v,
1194 void *opaque, const char *name,
1197 X86CPU *cpu = X86_CPU(obj);
1198 CPUX86State *env = &cpu->env;
1201 value = env->cpuid_version & 0xf;
1202 visit_type_int(v, &value, name, errp);
1205 static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
1206 void *opaque, const char *name,
1209 X86CPU *cpu = X86_CPU(obj);
1210 CPUX86State *env = &cpu->env;
1211 const int64_t min = 0;
1212 const int64_t max = 0xf;
1215 visit_type_int(v, &value, name, errp);
1216 if (error_is_set(errp)) {
1219 if (value < min || value > max) {
1220 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1221 name ? name : "null", value, min, max);
1225 env->cpuid_version &= ~0xf;
1226 env->cpuid_version |= value & 0xf;
1229 static void x86_cpuid_get_level(Object *obj, Visitor *v, void *opaque,
1230 const char *name, Error **errp)
1232 X86CPU *cpu = X86_CPU(obj);
1234 visit_type_uint32(v, &cpu->env.cpuid_level, name, errp);
1237 static void x86_cpuid_set_level(Object *obj, Visitor *v, void *opaque,
1238 const char *name, Error **errp)
1240 X86CPU *cpu = X86_CPU(obj);
1242 visit_type_uint32(v, &cpu->env.cpuid_level, name, errp);
1245 static void x86_cpuid_get_xlevel(Object *obj, Visitor *v, void *opaque,
1246 const char *name, Error **errp)
1248 X86CPU *cpu = X86_CPU(obj);
1250 visit_type_uint32(v, &cpu->env.cpuid_xlevel, name, errp);
1253 static void x86_cpuid_set_xlevel(Object *obj, Visitor *v, void *opaque,
1254 const char *name, Error **errp)
1256 X86CPU *cpu = X86_CPU(obj);
1258 visit_type_uint32(v, &cpu->env.cpuid_xlevel, name, errp);
1261 static char *x86_cpuid_get_vendor(Object *obj, Error **errp)
1263 X86CPU *cpu = X86_CPU(obj);
1264 CPUX86State *env = &cpu->env;
1267 value = (char *)g_malloc(CPUID_VENDOR_SZ + 1);
1268 x86_cpu_vendor_words2str(value, env->cpuid_vendor1, env->cpuid_vendor2,
1269 env->cpuid_vendor3);
1273 static void x86_cpuid_set_vendor(Object *obj, const char *value,
1276 X86CPU *cpu = X86_CPU(obj);
1277 CPUX86State *env = &cpu->env;
1280 if (strlen(value) != CPUID_VENDOR_SZ) {
1281 error_set(errp, QERR_PROPERTY_VALUE_BAD, "",
1286 env->cpuid_vendor1 = 0;
1287 env->cpuid_vendor2 = 0;
1288 env->cpuid_vendor3 = 0;
1289 for (i = 0; i < 4; i++) {
1290 env->cpuid_vendor1 |= ((uint8_t)value[i ]) << (8 * i);
1291 env->cpuid_vendor2 |= ((uint8_t)value[i + 4]) << (8 * i);
1292 env->cpuid_vendor3 |= ((uint8_t)value[i + 8]) << (8 * i);
1296 static char *x86_cpuid_get_model_id(Object *obj, Error **errp)
1298 X86CPU *cpu = X86_CPU(obj);
1299 CPUX86State *env = &cpu->env;
1303 value = g_malloc(48 + 1);
1304 for (i = 0; i < 48; i++) {
1305 value[i] = env->cpuid_model[i >> 2] >> (8 * (i & 3));
1311 static void x86_cpuid_set_model_id(Object *obj, const char *model_id,
1314 X86CPU *cpu = X86_CPU(obj);
1315 CPUX86State *env = &cpu->env;
1318 if (model_id == NULL) {
1321 len = strlen(model_id);
1322 memset(env->cpuid_model, 0, 48);
1323 for (i = 0; i < 48; i++) {
1327 c = (uint8_t)model_id[i];
1329 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
1333 static void x86_cpuid_get_tsc_freq(Object *obj, Visitor *v, void *opaque,
1334 const char *name, Error **errp)
1336 X86CPU *cpu = X86_CPU(obj);
1339 value = cpu->env.tsc_khz * 1000;
1340 visit_type_int(v, &value, name, errp);
1343 static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,
1344 const char *name, Error **errp)
1346 X86CPU *cpu = X86_CPU(obj);
1347 const int64_t min = 0;
1348 const int64_t max = INT64_MAX;
1351 visit_type_int(v, &value, name, errp);
1352 if (error_is_set(errp)) {
1355 if (value < min || value > max) {
1356 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1357 name ? name : "null", value, min, max);
1361 cpu->env.tsc_khz = value / 1000;
1364 static void x86_cpuid_get_apic_id(Object *obj, Visitor *v, void *opaque,
1365 const char *name, Error **errp)
1367 X86CPU *cpu = X86_CPU(obj);
1368 int64_t value = cpu->env.cpuid_apic_id;
1370 visit_type_int(v, &value, name, errp);
1373 static void x86_cpuid_set_apic_id(Object *obj, Visitor *v, void *opaque,
1374 const char *name, Error **errp)
1376 X86CPU *cpu = X86_CPU(obj);
1377 DeviceState *dev = DEVICE(obj);
1378 const int64_t min = 0;
1379 const int64_t max = UINT32_MAX;
1380 Error *error = NULL;
1383 if (dev->realized) {
1384 error_setg(errp, "Attempt to set property '%s' on '%s' after "
1385 "it was realized", name, object_get_typename(obj));
1389 visit_type_int(v, &value, name, &error);
1391 error_propagate(errp, error);
1394 if (value < min || value > max) {
1395 error_setg(errp, "Property %s.%s doesn't take value %" PRId64
1396 " (minimum: %" PRId64 ", maximum: %" PRId64 ")" ,
1397 object_get_typename(obj), name, value, min, max);
1401 if ((value != cpu->env.cpuid_apic_id) && cpu_exists(value)) {
1402 error_setg(errp, "CPU with APIC ID %" PRIi64 " exists", value);
1405 cpu->env.cpuid_apic_id = value;
1408 static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *name)
1416 if (kvm_enabled() && strcmp(name, "host") == 0) {
1417 kvm_cpu_fill_host(x86_cpu_def);
1421 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
1422 def = &builtin_x86_defs[i];
1423 if (strcmp(name, def->name) == 0) {
1424 memcpy(x86_cpu_def, def, sizeof(*def));
1425 /* sysenter isn't supported in compatibility mode on AMD,
1426 * syscall isn't supported in compatibility mode on Intel.
1427 * Normally we advertise the actual CPU vendor, but you can
1428 * override this using the 'vendor' property if you want to use
1429 * KVM's sysenter/syscall emulation in compatibility mode and
1430 * when doing cross vendor migration
1432 if (kvm_enabled()) {
1433 uint32_t ebx = 0, ecx = 0, edx = 0;
1434 host_cpuid(0, 0, NULL, &ebx, &ecx, &edx);
1435 x86_cpu_vendor_words2str(x86_cpu_def->vendor, ebx, edx, ecx);
1444 /* Convert all '_' in a feature string option name to '-', to make feature
1445 * name conform to QOM property naming rule, which uses '-' instead of '_'.
1447 static inline void feat2prop(char *s)
1449 while ((s = strchr(s, '_'))) {
1454 /* Parse "+feature,-feature,feature=foo" CPU feature string
1456 static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
1458 char *featurestr; /* Single 'key=value" string being parsed */
1459 /* Features to be added */
1460 FeatureWordArray plus_features = { 0 };
1461 /* Features to be removed */
1462 FeatureWordArray minus_features = { 0 };
1464 CPUX86State *env = &cpu->env;
1466 featurestr = features ? strtok(features, ",") : NULL;
1468 while (featurestr) {
1470 if (featurestr[0] == '+') {
1471 add_flagname_to_bitmaps(featurestr + 1, plus_features);
1472 } else if (featurestr[0] == '-') {
1473 add_flagname_to_bitmaps(featurestr + 1, minus_features);
1474 } else if ((val = strchr(featurestr, '='))) {
1476 feat2prop(featurestr);
1477 if (!strcmp(featurestr, "family")) {
1478 object_property_parse(OBJECT(cpu), val, featurestr, errp);
1479 } else if (!strcmp(featurestr, "model")) {
1480 object_property_parse(OBJECT(cpu), val, featurestr, errp);
1481 } else if (!strcmp(featurestr, "stepping")) {
1482 object_property_parse(OBJECT(cpu), val, featurestr, errp);
1483 } else if (!strcmp(featurestr, "level")) {
1484 object_property_parse(OBJECT(cpu), val, featurestr, errp);
1485 } else if (!strcmp(featurestr, "xlevel")) {
1489 numvalue = strtoul(val, &err, 0);
1490 if (!*val || *err) {
1491 error_setg(errp, "bad numerical value %s", val);
1494 if (numvalue < 0x80000000) {
1495 fprintf(stderr, "xlevel value shall always be >= 0x80000000"
1496 ", fixup will be removed in future versions\n");
1497 numvalue += 0x80000000;
1499 snprintf(num, sizeof(num), "%" PRIu32, numvalue);
1500 object_property_parse(OBJECT(cpu), num, featurestr, errp);
1501 } else if (!strcmp(featurestr, "vendor")) {
1502 object_property_parse(OBJECT(cpu), val, featurestr, errp);
1503 } else if (!strcmp(featurestr, "model-id")) {
1504 object_property_parse(OBJECT(cpu), val, featurestr, errp);
1505 } else if (!strcmp(featurestr, "tsc-freq")) {
1510 tsc_freq = strtosz_suffix_unit(val, &err,
1511 STRTOSZ_DEFSUFFIX_B, 1000);
1512 if (tsc_freq < 0 || *err) {
1513 error_setg(errp, "bad numerical value %s", val);
1516 snprintf(num, sizeof(num), "%" PRId64, tsc_freq);
1517 object_property_parse(OBJECT(cpu), num, "tsc-frequency", errp);
1518 } else if (!strcmp(featurestr, "hv-spinlocks")) {
1520 numvalue = strtoul(val, &err, 0);
1521 if (!*val || *err) {
1522 error_setg(errp, "bad numerical value %s", val);
1525 hyperv_set_spinlock_retries(numvalue);
1527 error_setg(errp, "unrecognized feature %s", featurestr);
1530 } else if (!strcmp(featurestr, "check")) {
1532 } else if (!strcmp(featurestr, "enforce")) {
1533 check_cpuid = enforce_cpuid = 1;
1534 } else if (!strcmp(featurestr, "hv_relaxed")) {
1535 hyperv_enable_relaxed_timing(true);
1536 } else if (!strcmp(featurestr, "hv_vapic")) {
1537 hyperv_enable_vapic_recommended(true);
1539 error_setg(errp, "feature string `%s' not in format (+feature|"
1540 "-feature|feature=xyz)", featurestr);
1543 if (error_is_set(errp)) {
1546 featurestr = strtok(NULL, ",");
1548 env->features[FEAT_1_EDX] |= plus_features[FEAT_1_EDX];
1549 env->features[FEAT_1_ECX] |= plus_features[FEAT_1_ECX];
1550 env->features[FEAT_8000_0001_EDX] |= plus_features[FEAT_8000_0001_EDX];
1551 env->features[FEAT_8000_0001_ECX] |= plus_features[FEAT_8000_0001_ECX];
1552 env->features[FEAT_C000_0001_EDX] |= plus_features[FEAT_C000_0001_EDX];
1553 env->features[FEAT_KVM] |= plus_features[FEAT_KVM];
1554 env->features[FEAT_SVM] |= plus_features[FEAT_SVM];
1555 env->features[FEAT_7_0_EBX] |= plus_features[FEAT_7_0_EBX];
1556 env->features[FEAT_1_EDX] &= ~minus_features[FEAT_1_EDX];
1557 env->features[FEAT_1_ECX] &= ~minus_features[FEAT_1_ECX];
1558 env->features[FEAT_8000_0001_EDX] &= ~minus_features[FEAT_8000_0001_EDX];
1559 env->features[FEAT_8000_0001_ECX] &= ~minus_features[FEAT_8000_0001_ECX];
1560 env->features[FEAT_C000_0001_EDX] &= ~minus_features[FEAT_C000_0001_EDX];
1561 env->features[FEAT_KVM] &= ~minus_features[FEAT_KVM];
1562 env->features[FEAT_SVM] &= ~minus_features[FEAT_SVM];
1563 env->features[FEAT_7_0_EBX] &= ~minus_features[FEAT_7_0_EBX];
1569 /* generate a composite string into buf of all cpuid names in featureset
1570 * selected by fbits. indicate truncation at bufsize in the event of overflow.
1571 * if flags, suppress names undefined in featureset.
1573 static void listflags(char *buf, int bufsize, uint32_t fbits,
1574 const char **featureset, uint32_t flags)
1576 const char **p = &featureset[31];
1580 b = 4 <= bufsize ? buf + (bufsize -= 3) - 1 : NULL;
1582 for (q = buf, bit = 31; fbits && bufsize; --p, fbits &= ~(1 << bit), --bit)
1583 if (fbits & 1 << bit && (*p || !flags)) {
1585 nc = snprintf(q, bufsize, "%s%s", q == buf ? "" : " ", *p);
1587 nc = snprintf(q, bufsize, "%s[%d]", q == buf ? "" : " ", bit);
1588 if (bufsize <= nc) {
1590 memcpy(b, "...", sizeof("..."));
1599 /* generate CPU information. */
1600 void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1606 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
1607 def = &builtin_x86_defs[i];
1608 snprintf(buf, sizeof(buf), "%s", def->name);
1609 (*cpu_fprintf)(f, "x86 %16s %-48s\n", buf, def->model_id);
1612 (*cpu_fprintf)(f, "x86 %16s %-48s\n", "host",
1613 "KVM processor with all supported host features "
1614 "(only available in KVM mode)");
1617 (*cpu_fprintf)(f, "\nRecognized CPUID flags:\n");
1618 for (i = 0; i < ARRAY_SIZE(feature_word_info); i++) {
1619 FeatureWordInfo *fw = &feature_word_info[i];
1621 listflags(buf, sizeof(buf), (uint32_t)~0, fw->feat_names, 1);
1622 (*cpu_fprintf)(f, " %s\n", buf);
1626 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
1628 CpuDefinitionInfoList *cpu_list = NULL;
1632 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
1633 CpuDefinitionInfoList *entry;
1634 CpuDefinitionInfo *info;
1636 def = &builtin_x86_defs[i];
1637 info = g_malloc0(sizeof(*info));
1638 info->name = g_strdup(def->name);
1640 entry = g_malloc0(sizeof(*entry));
1641 entry->value = info;
1642 entry->next = cpu_list;
1650 static void filter_features_for_kvm(X86CPU *cpu)
1652 CPUX86State *env = &cpu->env;
1653 KVMState *s = kvm_state;
1655 env->features[FEAT_1_EDX] &=
1656 kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
1657 env->features[FEAT_1_ECX] &=
1658 kvm_arch_get_supported_cpuid(s, 1, 0, R_ECX);
1659 env->features[FEAT_8000_0001_EDX] &=
1660 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX);
1661 env->features[FEAT_8000_0001_ECX] &=
1662 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX);
1663 env->features[FEAT_SVM] &=
1664 kvm_arch_get_supported_cpuid(s, 0x8000000A, 0, R_EDX);
1665 env->features[FEAT_7_0_EBX] &=
1666 kvm_arch_get_supported_cpuid(s, 7, 0, R_EBX);
1667 env->features[FEAT_KVM] &=
1668 kvm_arch_get_supported_cpuid(s, KVM_CPUID_FEATURES, 0, R_EAX);
1669 env->features[FEAT_C000_0001_EDX] &=
1670 kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
1675 static void cpu_x86_register(X86CPU *cpu, const char *name, Error **errp)
1677 CPUX86State *env = &cpu->env;
1678 x86_def_t def1, *def = &def1;
1680 memset(def, 0, sizeof(*def));
1682 if (cpu_x86_find_by_name(def, name) < 0) {
1683 error_setg(errp, "Unable to find CPU definition: %s", name);
1687 if (kvm_enabled()) {
1688 def->features[FEAT_KVM] |= kvm_default_features;
1690 def->features[FEAT_1_ECX] |= CPUID_EXT_HYPERVISOR;
1692 object_property_set_str(OBJECT(cpu), def->vendor, "vendor", errp);
1693 object_property_set_int(OBJECT(cpu), def->level, "level", errp);
1694 object_property_set_int(OBJECT(cpu), def->family, "family", errp);
1695 object_property_set_int(OBJECT(cpu), def->model, "model", errp);
1696 object_property_set_int(OBJECT(cpu), def->stepping, "stepping", errp);
1697 env->features[FEAT_1_EDX] = def->features[FEAT_1_EDX];
1698 env->features[FEAT_1_ECX] = def->features[FEAT_1_ECX];
1699 env->features[FEAT_8000_0001_EDX] = def->features[FEAT_8000_0001_EDX];
1700 env->features[FEAT_8000_0001_ECX] = def->features[FEAT_8000_0001_ECX];
1701 object_property_set_int(OBJECT(cpu), def->xlevel, "xlevel", errp);
1702 env->features[FEAT_KVM] = def->features[FEAT_KVM];
1703 env->features[FEAT_SVM] = def->features[FEAT_SVM];
1704 env->features[FEAT_C000_0001_EDX] = def->features[FEAT_C000_0001_EDX];
1705 env->features[FEAT_7_0_EBX] = def->features[FEAT_7_0_EBX];
1706 env->cpuid_xlevel2 = def->xlevel2;
1708 object_property_set_str(OBJECT(cpu), def->model_id, "model-id", errp);
1711 X86CPU *cpu_x86_create(const char *cpu_model, DeviceState *icc_bridge,
1716 gchar **model_pieces;
1717 char *name, *features;
1718 Error *error = NULL;
1720 model_pieces = g_strsplit(cpu_model, ",", 2);
1721 if (!model_pieces[0]) {
1722 error_setg(&error, "Invalid/empty CPU model name");
1725 name = model_pieces[0];
1726 features = model_pieces[1];
1728 cpu = X86_CPU(object_new(TYPE_X86_CPU));
1729 #ifndef CONFIG_USER_ONLY
1730 if (icc_bridge == NULL) {
1731 error_setg(&error, "Invalid icc-bridge value");
1734 qdev_set_parent_bus(DEVICE(cpu), qdev_get_child_bus(icc_bridge, "icc"));
1735 object_unref(OBJECT(cpu));
1738 env->cpu_model_str = cpu_model;
1740 cpu_x86_register(cpu, name, &error);
1745 cpu_x86_parse_featurestr(cpu, features, &error);
1751 error_propagate(errp, error);
1752 g_strfreev(model_pieces);
1756 X86CPU *cpu_x86_init(const char *cpu_model)
1758 Error *error = NULL;
1761 cpu = cpu_x86_create(cpu_model, NULL, &error);
1766 object_property_set_bool(OBJECT(cpu), true, "realized", &error);
1770 fprintf(stderr, "%s\n", error_get_pretty(error));
1773 object_unref(OBJECT(cpu));
1780 #if !defined(CONFIG_USER_ONLY)
1782 void cpu_clear_apic_feature(CPUX86State *env)
1784 env->features[FEAT_1_EDX] &= ~CPUID_APIC;
1787 #endif /* !CONFIG_USER_ONLY */
1789 /* Initialize list of CPU models, filling some non-static fields if necessary
1791 void x86_cpudef_setup(void)
1794 static const char *model_with_versions[] = { "qemu32", "qemu64", "athlon" };
1796 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); ++i) {
1797 x86_def_t *def = &builtin_x86_defs[i];
1799 /* Look for specific "cpudef" models that */
1800 /* have the QEMU version in .model_id */
1801 for (j = 0; j < ARRAY_SIZE(model_with_versions); j++) {
1802 if (strcmp(model_with_versions[j], def->name) == 0) {
1803 pstrcpy(def->model_id, sizeof(def->model_id),
1804 "QEMU Virtual CPU version ");
1805 pstrcat(def->model_id, sizeof(def->model_id),
1806 qemu_get_version());
1813 static void get_cpuid_vendor(CPUX86State *env, uint32_t *ebx,
1814 uint32_t *ecx, uint32_t *edx)
1816 *ebx = env->cpuid_vendor1;
1817 *edx = env->cpuid_vendor2;
1818 *ecx = env->cpuid_vendor3;
1821 void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
1822 uint32_t *eax, uint32_t *ebx,
1823 uint32_t *ecx, uint32_t *edx)
1825 X86CPU *cpu = x86_env_get_cpu(env);
1826 CPUState *cs = CPU(cpu);
1828 /* test if maximum index reached */
1829 if (index & 0x80000000) {
1830 if (index > env->cpuid_xlevel) {
1831 if (env->cpuid_xlevel2 > 0) {
1832 /* Handle the Centaur's CPUID instruction. */
1833 if (index > env->cpuid_xlevel2) {
1834 index = env->cpuid_xlevel2;
1835 } else if (index < 0xC0000000) {
1836 index = env->cpuid_xlevel;
1839 /* Intel documentation states that invalid EAX input will
1840 * return the same information as EAX=cpuid_level
1841 * (Intel SDM Vol. 2A - Instruction Set Reference - CPUID)
1843 index = env->cpuid_level;
1847 if (index > env->cpuid_level)
1848 index = env->cpuid_level;
1853 *eax = env->cpuid_level;
1854 get_cpuid_vendor(env, ebx, ecx, edx);
1857 *eax = env->cpuid_version;
1858 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1859 *ecx = env->features[FEAT_1_ECX];
1860 *edx = env->features[FEAT_1_EDX];
1861 if (cs->nr_cores * cs->nr_threads > 1) {
1862 *ebx |= (cs->nr_cores * cs->nr_threads) << 16;
1863 *edx |= 1 << 28; /* HTT bit */
1867 /* cache info: needed for Pentium Pro compatibility */
1874 /* cache info: needed for Core compatibility */
1875 if (cs->nr_cores > 1) {
1876 *eax = (cs->nr_cores - 1) << 26;
1881 case 0: /* L1 dcache info */
1887 case 1: /* L1 icache info */
1893 case 2: /* L2 cache info */
1895 if (cs->nr_threads > 1) {
1896 *eax |= (cs->nr_threads - 1) << 14;
1902 default: /* end of info */
1911 /* mwait info: needed for Core compatibility */
1912 *eax = 0; /* Smallest monitor-line size in bytes */
1913 *ebx = 0; /* Largest monitor-line size in bytes */
1914 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
1918 /* Thermal and Power Leaf */
1925 /* Structured Extended Feature Flags Enumeration Leaf */
1927 *eax = 0; /* Maximum ECX value for sub-leaves */
1928 *ebx = env->features[FEAT_7_0_EBX]; /* Feature flags */
1929 *ecx = 0; /* Reserved */
1930 *edx = 0; /* Reserved */
1939 /* Direct Cache Access Information Leaf */
1940 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1946 /* Architectural Performance Monitoring Leaf */
1947 if (kvm_enabled()) {
1948 KVMState *s = cs->kvm_state;
1950 *eax = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EAX);
1951 *ebx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EBX);
1952 *ecx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_ECX);
1953 *edx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EDX);
1962 /* Processor Extended State */
1963 if (!(env->features[FEAT_1_ECX] & CPUID_EXT_XSAVE)) {
1970 if (kvm_enabled()) {
1971 KVMState *s = cs->kvm_state;
1973 *eax = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EAX);
1974 *ebx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EBX);
1975 *ecx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_ECX);
1976 *edx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EDX);
1985 *eax = env->cpuid_xlevel;
1986 *ebx = env->cpuid_vendor1;
1987 *edx = env->cpuid_vendor2;
1988 *ecx = env->cpuid_vendor3;
1991 *eax = env->cpuid_version;
1993 *ecx = env->features[FEAT_8000_0001_ECX];
1994 *edx = env->features[FEAT_8000_0001_EDX];
1996 /* The Linux kernel checks for the CMPLegacy bit and
1997 * discards multiple thread information if it is set.
1998 * So dont set it here for Intel to make Linux guests happy.
2000 if (cs->nr_cores * cs->nr_threads > 1) {
2001 uint32_t tebx, tecx, tedx;
2002 get_cpuid_vendor(env, &tebx, &tecx, &tedx);
2003 if (tebx != CPUID_VENDOR_INTEL_1 ||
2004 tedx != CPUID_VENDOR_INTEL_2 ||
2005 tecx != CPUID_VENDOR_INTEL_3) {
2006 *ecx |= 1 << 1; /* CmpLegacy bit */
2013 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
2014 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
2015 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
2016 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
2019 /* cache info (L1 cache) */
2026 /* cache info (L2 cache) */
2033 /* virtual & phys address size in low 2 bytes. */
2034 /* XXX: This value must match the one used in the MMU code. */
2035 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM) {
2036 /* 64 bit processor */
2037 /* XXX: The physical address space is limited to 42 bits in exec.c. */
2038 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
2040 if (env->features[FEAT_1_EDX] & CPUID_PSE36) {
2041 *eax = 0x00000024; /* 36 bits physical */
2043 *eax = 0x00000020; /* 32 bits physical */
2049 if (cs->nr_cores * cs->nr_threads > 1) {
2050 *ecx |= (cs->nr_cores * cs->nr_threads) - 1;
2054 if (env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM) {
2055 *eax = 0x00000001; /* SVM Revision */
2056 *ebx = 0x00000010; /* nr of ASIDs */
2058 *edx = env->features[FEAT_SVM]; /* optional features */
2067 *eax = env->cpuid_xlevel2;
2073 /* Support for VIA CPU's CPUID instruction */
2074 *eax = env->cpuid_version;
2077 *edx = env->features[FEAT_C000_0001_EDX];
2082 /* Reserved for the future, and now filled with zero */
2089 /* reserved values: zero */
2098 /* CPUClass::reset() */
2099 static void x86_cpu_reset(CPUState *s)
2101 X86CPU *cpu = X86_CPU(s);
2102 X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
2103 CPUX86State *env = &cpu->env;
2106 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
2107 qemu_log("CPU Reset (CPU %d)\n", s->cpu_index);
2108 log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
2111 xcc->parent_reset(s);
2114 memset(env, 0, offsetof(CPUX86State, breakpoints));
2118 env->old_exception = -1;
2120 /* init to reset state */
2122 #ifdef CONFIG_SOFTMMU
2123 env->hflags |= HF_SOFTMMU_MASK;
2125 env->hflags2 |= HF2_GIF_MASK;
2127 cpu_x86_update_cr0(env, 0x60000010);
2128 env->a20_mask = ~0x0;
2129 env->smbase = 0x30000;
2131 env->idt.limit = 0xffff;
2132 env->gdt.limit = 0xffff;
2133 env->ldt.limit = 0xffff;
2134 env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
2135 env->tr.limit = 0xffff;
2136 env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
2138 cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
2139 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK |
2140 DESC_R_MASK | DESC_A_MASK);
2141 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
2142 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2144 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
2145 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2147 cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
2148 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2150 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
2151 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2153 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
2154 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2158 env->regs[R_EDX] = env->cpuid_version;
2163 for (i = 0; i < 8; i++) {
2168 env->mxcsr = 0x1f80;
2170 env->pat = 0x0007040600070406ULL;
2171 env->msr_ia32_misc_enable = MSR_IA32_MISC_ENABLE_DEFAULT;
2173 memset(env->dr, 0, sizeof(env->dr));
2174 env->dr[6] = DR6_FIXED_1;
2175 env->dr[7] = DR7_FIXED_1;
2176 cpu_breakpoint_remove_all(env, BP_CPU);
2177 cpu_watchpoint_remove_all(env, BP_CPU);
2179 #if !defined(CONFIG_USER_ONLY)
2180 /* We hard-wire the BSP to the first CPU. */
2181 if (s->cpu_index == 0) {
2182 apic_designate_bsp(env->apic_state);
2185 s->halted = !cpu_is_bsp(cpu);
2189 #ifndef CONFIG_USER_ONLY
2190 bool cpu_is_bsp(X86CPU *cpu)
2192 return cpu_get_apic_base(cpu->env.apic_state) & MSR_IA32_APICBASE_BSP;
2195 /* TODO: remove me, when reset over QOM tree is implemented */
2196 static void x86_cpu_machine_reset_cb(void *opaque)
2198 X86CPU *cpu = opaque;
2199 cpu_reset(CPU(cpu));
2203 static void mce_init(X86CPU *cpu)
2205 CPUX86State *cenv = &cpu->env;
2208 if (((cenv->cpuid_version >> 8) & 0xf) >= 6
2209 && (cenv->features[FEAT_1_EDX] & (CPUID_MCE | CPUID_MCA)) ==
2210 (CPUID_MCE | CPUID_MCA)) {
2211 cenv->mcg_cap = MCE_CAP_DEF | MCE_BANKS_DEF;
2212 cenv->mcg_ctl = ~(uint64_t)0;
2213 for (bank = 0; bank < MCE_BANKS_DEF; bank++) {
2214 cenv->mce_banks[bank * 4] = ~(uint64_t)0;
2219 #ifndef CONFIG_USER_ONLY
2220 static void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
2222 CPUX86State *env = &cpu->env;
2223 DeviceState *dev = DEVICE(cpu);
2224 APICCommonState *apic;
2225 const char *apic_type = "apic";
2227 if (kvm_irqchip_in_kernel()) {
2228 apic_type = "kvm-apic";
2229 } else if (xen_enabled()) {
2230 apic_type = "xen-apic";
2233 env->apic_state = qdev_try_create(qdev_get_parent_bus(dev), apic_type);
2234 if (env->apic_state == NULL) {
2235 error_setg(errp, "APIC device '%s' could not be created", apic_type);
2239 object_property_add_child(OBJECT(cpu), "apic",
2240 OBJECT(env->apic_state), NULL);
2241 qdev_prop_set_uint8(env->apic_state, "id", env->cpuid_apic_id);
2242 /* TODO: convert to link<> */
2243 apic = APIC_COMMON(env->apic_state);
2247 static void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
2249 CPUX86State *env = &cpu->env;
2251 if (env->apic_state == NULL) {
2255 if (qdev_init(env->apic_state)) {
2256 error_setg(errp, "APIC device '%s' could not be initialized",
2257 object_get_typename(OBJECT(env->apic_state)));
2262 static void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
2267 static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
2269 X86CPU *cpu = X86_CPU(dev);
2270 X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
2271 CPUX86State *env = &cpu->env;
2272 Error *local_err = NULL;
2274 if (env->features[FEAT_7_0_EBX] && env->cpuid_level < 7) {
2275 env->cpuid_level = 7;
2278 /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on
2281 if (env->cpuid_vendor1 == CPUID_VENDOR_AMD_1 &&
2282 env->cpuid_vendor2 == CPUID_VENDOR_AMD_2 &&
2283 env->cpuid_vendor3 == CPUID_VENDOR_AMD_3) {
2284 env->features[FEAT_8000_0001_EDX] &= ~CPUID_EXT2_AMD_ALIASES;
2285 env->features[FEAT_8000_0001_EDX] |= (env->features[FEAT_1_EDX]
2286 & CPUID_EXT2_AMD_ALIASES);
2289 if (!kvm_enabled()) {
2290 env->features[FEAT_1_EDX] &= TCG_FEATURES;
2291 env->features[FEAT_1_ECX] &= TCG_EXT_FEATURES;
2292 env->features[FEAT_8000_0001_EDX] &= (TCG_EXT2_FEATURES
2293 #ifdef TARGET_X86_64
2294 | CPUID_EXT2_SYSCALL | CPUID_EXT2_LM
2297 env->features[FEAT_8000_0001_ECX] &= TCG_EXT3_FEATURES;
2298 env->features[FEAT_SVM] &= TCG_SVM_FEATURES;
2300 if (check_cpuid && kvm_check_features_against_host(cpu)
2302 error_setg(&local_err,
2303 "Host's CPU doesn't support requested features");
2307 filter_features_for_kvm(cpu);
2311 #ifndef CONFIG_USER_ONLY
2312 qemu_register_reset(x86_cpu_machine_reset_cb, cpu);
2314 if (cpu->env.features[FEAT_1_EDX] & CPUID_APIC || smp_cpus > 1) {
2315 x86_cpu_apic_create(cpu, &local_err);
2316 if (local_err != NULL) {
2323 qemu_init_vcpu(&cpu->env);
2325 x86_cpu_apic_realize(cpu, &local_err);
2326 if (local_err != NULL) {
2329 cpu_reset(CPU(cpu));
2331 xcc->parent_realize(dev, &local_err);
2333 if (local_err != NULL) {
2334 error_propagate(errp, local_err);
2339 /* Enables contiguous-apic-ID mode, for compatibility */
2340 static bool compat_apic_id_mode;
2342 void enable_compat_apic_id_mode(void)
2344 compat_apic_id_mode = true;
2347 /* Calculates initial APIC ID for a specific CPU index
2349 * Currently we need to be able to calculate the APIC ID from the CPU index
2350 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
2351 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
2352 * all CPUs up to max_cpus.
2354 uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index)
2356 uint32_t correct_id;
2359 correct_id = x86_apicid_from_cpu_idx(smp_cores, smp_threads, cpu_index);
2360 if (compat_apic_id_mode) {
2361 if (cpu_index != correct_id && !warned) {
2362 error_report("APIC IDs set in compatibility mode, "
2363 "CPU topology won't match the configuration");
2372 static void x86_cpu_initfn(Object *obj)
2374 CPUState *cs = CPU(obj);
2375 X86CPU *cpu = X86_CPU(obj);
2376 CPUX86State *env = &cpu->env;
2382 object_property_add(obj, "family", "int",
2383 x86_cpuid_version_get_family,
2384 x86_cpuid_version_set_family, NULL, NULL, NULL);
2385 object_property_add(obj, "model", "int",
2386 x86_cpuid_version_get_model,
2387 x86_cpuid_version_set_model, NULL, NULL, NULL);
2388 object_property_add(obj, "stepping", "int",
2389 x86_cpuid_version_get_stepping,
2390 x86_cpuid_version_set_stepping, NULL, NULL, NULL);
2391 object_property_add(obj, "level", "int",
2392 x86_cpuid_get_level,
2393 x86_cpuid_set_level, NULL, NULL, NULL);
2394 object_property_add(obj, "xlevel", "int",
2395 x86_cpuid_get_xlevel,
2396 x86_cpuid_set_xlevel, NULL, NULL, NULL);
2397 object_property_add_str(obj, "vendor",
2398 x86_cpuid_get_vendor,
2399 x86_cpuid_set_vendor, NULL);
2400 object_property_add_str(obj, "model-id",
2401 x86_cpuid_get_model_id,
2402 x86_cpuid_set_model_id, NULL);
2403 object_property_add(obj, "tsc-frequency", "int",
2404 x86_cpuid_get_tsc_freq,
2405 x86_cpuid_set_tsc_freq, NULL, NULL, NULL);
2406 object_property_add(obj, "apic-id", "int",
2407 x86_cpuid_get_apic_id,
2408 x86_cpuid_set_apic_id, NULL, NULL, NULL);
2410 env->cpuid_apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);
2412 /* init various static tables used in TCG mode */
2413 if (tcg_enabled() && !inited) {
2415 optimize_flags_init();
2416 #ifndef CONFIG_USER_ONLY
2417 cpu_set_debug_excp_handler(breakpoint_handler);
2422 static int64_t x86_cpu_get_arch_id(CPUState *cs)
2424 X86CPU *cpu = X86_CPU(cs);
2425 CPUX86State *env = &cpu->env;
2427 return env->cpuid_apic_id;
2430 static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
2432 X86CPUClass *xcc = X86_CPU_CLASS(oc);
2433 CPUClass *cc = CPU_CLASS(oc);
2434 DeviceClass *dc = DEVICE_CLASS(oc);
2436 xcc->parent_realize = dc->realize;
2437 dc->realize = x86_cpu_realizefn;
2438 dc->bus_type = TYPE_ICC_BUS;
2440 xcc->parent_reset = cc->reset;
2441 cc->reset = x86_cpu_reset;
2443 cc->do_interrupt = x86_cpu_do_interrupt;
2444 #ifndef CONFIG_USER_ONLY
2445 cc->write_elf64_note = x86_cpu_write_elf64_note;
2446 cc->write_elf64_qemunote = x86_cpu_write_elf64_qemunote;
2447 cc->write_elf32_note = x86_cpu_write_elf32_note;
2448 cc->write_elf32_qemunote = x86_cpu_write_elf32_qemunote;
2450 cpu_class_set_vmsd(cc, &vmstate_x86_cpu);
2452 cc->get_arch_id = x86_cpu_get_arch_id;
2455 static const TypeInfo x86_cpu_type_info = {
2456 .name = TYPE_X86_CPU,
2458 .instance_size = sizeof(X86CPU),
2459 .instance_init = x86_cpu_initfn,
2461 .class_size = sizeof(X86CPUClass),
2462 .class_init = x86_cpu_common_class_init,
2465 static void x86_cpu_register_types(void)
2467 type_register_static(&x86_cpu_type_info);
2470 type_init(x86_cpu_register_types)