target-arm: Fix handling of AL condition in IT instruction
[qemu/ar7.git] / target-i386 / cpuid.c
blob56938e2a7c2cccf91575ba0e192a6790ffb898dc
1 /*
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/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <inttypes.h>
24 #include "cpu.h"
25 #include "kvm.h"
27 #include "qemu-option.h"
28 #include "qemu-config.h"
30 /* feature flags taken from "Intel Processor Identification and the CPUID
31 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
32 * between feature naming conventions, aliases may be added.
34 static const char *feature_name[] = {
35 "fpu", "vme", "de", "pse",
36 "tsc", "msr", "pae", "mce",
37 "cx8", "apic", NULL, "sep",
38 "mtrr", "pge", "mca", "cmov",
39 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
40 NULL, "ds" /* Intel dts */, "acpi", "mmx",
41 "fxsr", "sse", "sse2", "ss",
42 "ht" /* Intel htt */, "tm", "ia64", "pbe",
44 static const char *ext_feature_name[] = {
45 "pni|sse3" /* Intel,AMD sse3 */, "pclmuldq", "dtes64", "monitor",
46 "ds_cpl", "vmx", "smx", "est",
47 "tm2", "ssse3", "cid", NULL,
48 "fma", "cx16", "xtpr", "pdcm",
49 NULL, NULL, "dca", "sse4.1|sse4_1",
50 "sse4.2|sse4_2", "x2apic", "movbe", "popcnt",
51 NULL, "aes", "xsave", "osxsave",
52 "avx", NULL, NULL, "hypervisor",
54 static const char *ext2_feature_name[] = {
55 "fpu", "vme", "de", "pse",
56 "tsc", "msr", "pae", "mce",
57 "cx8" /* AMD CMPXCHG8B */, "apic", NULL, "syscall",
58 "mtrr", "pge", "mca", "cmov",
59 "pat", "pse36", NULL, NULL /* Linux mp */,
60 "nx" /* Intel xd */, NULL, "mmxext", "mmx",
61 "fxsr", "fxsr_opt" /* AMD ffxsr */, "pdpe1gb" /* AMD Page1GB */, "rdtscp",
62 NULL, "lm" /* Intel 64 */, "3dnowext", "3dnow",
64 static const char *ext3_feature_name[] = {
65 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
66 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
67 "3dnowprefetch", "osvw", "ibs", "xop",
68 "skinit", "wdt", NULL, NULL,
69 "fma4", NULL, "cvt16", "nodeid_msr",
70 NULL, NULL, NULL, NULL,
71 NULL, NULL, NULL, NULL,
72 NULL, NULL, NULL, NULL,
75 static const char *kvm_feature_name[] = {
76 "kvmclock", "kvm_nopiodelay", "kvm_mmu", NULL, NULL, NULL, NULL, NULL,
77 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
78 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
79 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
82 /* collects per-function cpuid data
84 typedef struct model_features_t {
85 uint32_t *guest_feat;
86 uint32_t *host_feat;
87 uint32_t check_feat;
88 const char **flag_names;
89 uint32_t cpuid;
90 } model_features_t;
92 int check_cpuid = 0;
93 int enforce_cpuid = 0;
95 static void host_cpuid(uint32_t function, uint32_t count,
96 uint32_t *eax, uint32_t *ebx,
97 uint32_t *ecx, uint32_t *edx)
99 #if defined(CONFIG_KVM)
100 uint32_t vec[4];
102 #ifdef __x86_64__
103 asm volatile("cpuid"
104 : "=a"(vec[0]), "=b"(vec[1]),
105 "=c"(vec[2]), "=d"(vec[3])
106 : "0"(function), "c"(count) : "cc");
107 #else
108 asm volatile("pusha \n\t"
109 "cpuid \n\t"
110 "mov %%eax, 0(%2) \n\t"
111 "mov %%ebx, 4(%2) \n\t"
112 "mov %%ecx, 8(%2) \n\t"
113 "mov %%edx, 12(%2) \n\t"
114 "popa"
115 : : "a"(function), "c"(count), "S"(vec)
116 : "memory", "cc");
117 #endif
119 if (eax)
120 *eax = vec[0];
121 if (ebx)
122 *ebx = vec[1];
123 if (ecx)
124 *ecx = vec[2];
125 if (edx)
126 *edx = vec[3];
127 #endif
130 #define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
132 /* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
133 * a substring. ex if !NULL points to the first char after a substring,
134 * otherwise the string is assumed to sized by a terminating nul.
135 * Return lexical ordering of *s1:*s2.
137 static int sstrcmp(const char *s1, const char *e1, const char *s2,
138 const char *e2)
140 for (;;) {
141 if (!*s1 || !*s2 || *s1 != *s2)
142 return (*s1 - *s2);
143 ++s1, ++s2;
144 if (s1 == e1 && s2 == e2)
145 return (0);
146 else if (s1 == e1)
147 return (*s2);
148 else if (s2 == e2)
149 return (*s1);
153 /* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
154 * '|' delimited (possibly empty) strings in which case search for a match
155 * within the alternatives proceeds left to right. Return 0 for success,
156 * non-zero otherwise.
158 static int altcmp(const char *s, const char *e, const char *altstr)
160 const char *p, *q;
162 for (q = p = altstr; ; ) {
163 while (*p && *p != '|')
164 ++p;
165 if ((q == p && !*s) || (q != p && !sstrcmp(s, e, q, p)))
166 return (0);
167 if (!*p)
168 return (1);
169 else
170 q = ++p;
174 /* search featureset for flag *[s..e), if found set corresponding bit in
175 * *pval and return success, otherwise return zero
177 static int lookup_feature(uint32_t *pval, const char *s, const char *e,
178 const char **featureset)
180 uint32_t mask;
181 const char **ppc;
183 for (mask = 1, ppc = featureset; mask; mask <<= 1, ++ppc)
184 if (*ppc && !altcmp(s, e, *ppc)) {
185 *pval |= mask;
186 break;
188 return (mask ? 1 : 0);
191 static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features,
192 uint32_t *ext_features,
193 uint32_t *ext2_features,
194 uint32_t *ext3_features,
195 uint32_t *kvm_features)
197 if (!lookup_feature(features, flagname, NULL, feature_name) &&
198 !lookup_feature(ext_features, flagname, NULL, ext_feature_name) &&
199 !lookup_feature(ext2_features, flagname, NULL, ext2_feature_name) &&
200 !lookup_feature(ext3_features, flagname, NULL, ext3_feature_name) &&
201 !lookup_feature(kvm_features, flagname, NULL, kvm_feature_name))
202 fprintf(stderr, "CPU feature %s not found\n", flagname);
205 typedef struct x86_def_t {
206 struct x86_def_t *next;
207 const char *name;
208 uint32_t level;
209 uint32_t vendor1, vendor2, vendor3;
210 int family;
211 int model;
212 int stepping;
213 uint32_t features, ext_features, ext2_features, ext3_features, kvm_features;
214 uint32_t xlevel;
215 char model_id[48];
216 int vendor_override;
217 uint32_t flags;
218 } x86_def_t;
220 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
221 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
222 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
223 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
224 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
225 CPUID_PSE36 | CPUID_FXSR)
226 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
227 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
228 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
229 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
230 CPUID_PAE | CPUID_SEP | CPUID_APIC)
231 #define EXT2_FEATURE_MASK 0x0183F3FF
233 #define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
234 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
235 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
236 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
237 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
238 /* partly implemented:
239 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
240 CPUID_PSE36 (needed for Solaris) */
241 /* missing:
242 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
243 #define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \
244 CPUID_EXT_CX16 | CPUID_EXT_POPCNT | CPUID_EXT_XSAVE | \
245 CPUID_EXT_HYPERVISOR)
246 /* missing:
247 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
248 CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM */
249 #define TCG_EXT2_FEATURES ((TCG_FEATURES & EXT2_FEATURE_MASK) | \
250 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
251 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
252 /* missing:
253 CPUID_EXT2_PDPE1GB */
254 #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
255 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
257 /* maintains list of cpu model definitions
259 static x86_def_t *x86_defs = {NULL};
261 /* built-in cpu model definitions (deprecated)
263 static x86_def_t builtin_x86_defs[] = {
265 .name = "qemu64",
266 .level = 4,
267 .vendor1 = CPUID_VENDOR_AMD_1,
268 .vendor2 = CPUID_VENDOR_AMD_2,
269 .vendor3 = CPUID_VENDOR_AMD_3,
270 .family = 6,
271 .model = 2,
272 .stepping = 3,
273 .features = PPRO_FEATURES |
274 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
275 CPUID_PSE36,
276 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT,
277 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) |
278 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
279 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
280 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
281 .xlevel = 0x8000000A,
282 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
285 .name = "phenom",
286 .level = 5,
287 .vendor1 = CPUID_VENDOR_AMD_1,
288 .vendor2 = CPUID_VENDOR_AMD_2,
289 .vendor3 = CPUID_VENDOR_AMD_3,
290 .family = 16,
291 .model = 2,
292 .stepping = 3,
293 .features = PPRO_FEATURES |
294 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
295 CPUID_PSE36 | CPUID_VME | CPUID_HT,
296 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_CX16 |
297 CPUID_EXT_POPCNT,
298 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) |
299 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
300 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
301 CPUID_EXT2_FFXSR | CPUID_EXT2_PDPE1GB | CPUID_EXT2_RDTSCP,
302 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
303 CPUID_EXT3_CR8LEG,
304 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
305 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
306 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
307 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
308 .xlevel = 0x8000001A,
309 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
312 .name = "core2duo",
313 .level = 10,
314 .family = 6,
315 .model = 15,
316 .stepping = 11,
317 .features = PPRO_FEATURES |
318 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
319 CPUID_PSE36 | CPUID_VME | CPUID_DTS | CPUID_ACPI | CPUID_SS |
320 CPUID_HT | CPUID_TM | CPUID_PBE,
321 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
322 CPUID_EXT_DTES64 | CPUID_EXT_DSCPL | CPUID_EXT_VMX | CPUID_EXT_EST |
323 CPUID_EXT_TM2 | CPUID_EXT_CX16 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
324 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
325 .ext3_features = CPUID_EXT3_LAHF_LM,
326 .xlevel = 0x80000008,
327 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
330 .name = "kvm64",
331 .level = 5,
332 .vendor1 = CPUID_VENDOR_INTEL_1,
333 .vendor2 = CPUID_VENDOR_INTEL_2,
334 .vendor3 = CPUID_VENDOR_INTEL_3,
335 .family = 15,
336 .model = 6,
337 .stepping = 1,
338 /* Missing: CPUID_VME, CPUID_HT */
339 .features = PPRO_FEATURES |
340 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
341 CPUID_PSE36,
342 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
343 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16,
344 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
345 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) |
346 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
347 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
348 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
349 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
350 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
351 .ext3_features = 0,
352 .xlevel = 0x80000008,
353 .model_id = "Common KVM processor"
356 .name = "qemu32",
357 .level = 4,
358 .family = 6,
359 .model = 3,
360 .stepping = 3,
361 .features = PPRO_FEATURES,
362 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_POPCNT,
363 .xlevel = 0x80000004,
364 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
367 .name = "coreduo",
368 .level = 10,
369 .family = 6,
370 .model = 14,
371 .stepping = 8,
372 .features = PPRO_FEATURES | CPUID_VME |
373 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_DTS | CPUID_ACPI |
374 CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
375 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_VMX |
376 CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
377 .ext2_features = CPUID_EXT2_NX,
378 .xlevel = 0x80000008,
379 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
382 .name = "486",
383 .level = 1,
384 .family = 4,
385 .model = 0,
386 .stepping = 0,
387 .features = I486_FEATURES,
388 .xlevel = 0,
391 .name = "pentium",
392 .level = 1,
393 .family = 5,
394 .model = 4,
395 .stepping = 3,
396 .features = PENTIUM_FEATURES,
397 .xlevel = 0,
400 .name = "pentium2",
401 .level = 2,
402 .family = 6,
403 .model = 5,
404 .stepping = 2,
405 .features = PENTIUM2_FEATURES,
406 .xlevel = 0,
409 .name = "pentium3",
410 .level = 2,
411 .family = 6,
412 .model = 7,
413 .stepping = 3,
414 .features = PENTIUM3_FEATURES,
415 .xlevel = 0,
418 .name = "athlon",
419 .level = 2,
420 .vendor1 = CPUID_VENDOR_AMD_1,
421 .vendor2 = CPUID_VENDOR_AMD_2,
422 .vendor3 = CPUID_VENDOR_AMD_3,
423 .family = 6,
424 .model = 2,
425 .stepping = 3,
426 .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR | CPUID_MCA,
427 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
428 .xlevel = 0x80000008,
429 /* XXX: put another string ? */
430 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
433 .name = "n270",
434 /* original is on level 10 */
435 .level = 5,
436 .family = 6,
437 .model = 28,
438 .stepping = 2,
439 .features = PPRO_FEATURES |
440 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME | CPUID_DTS |
441 CPUID_ACPI | CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
442 /* Some CPUs got no CPUID_SEP */
443 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
444 CPUID_EXT_DSCPL | CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR,
445 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | CPUID_EXT2_NX,
446 .ext3_features = CPUID_EXT3_LAHF_LM,
447 .xlevel = 0x8000000A,
448 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
452 static int cpu_x86_fill_model_id(char *str)
454 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
455 int i;
457 for (i = 0; i < 3; i++) {
458 host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx);
459 memcpy(str + i * 16 + 0, &eax, 4);
460 memcpy(str + i * 16 + 4, &ebx, 4);
461 memcpy(str + i * 16 + 8, &ecx, 4);
462 memcpy(str + i * 16 + 12, &edx, 4);
464 return 0;
467 static int cpu_x86_fill_host(x86_def_t *x86_cpu_def)
469 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
471 x86_cpu_def->name = "host";
472 host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
473 x86_cpu_def->level = eax;
474 x86_cpu_def->vendor1 = ebx;
475 x86_cpu_def->vendor2 = edx;
476 x86_cpu_def->vendor3 = ecx;
478 host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
479 x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
480 x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
481 x86_cpu_def->stepping = eax & 0x0F;
482 x86_cpu_def->ext_features = ecx;
483 x86_cpu_def->features = edx;
485 host_cpuid(0x80000000, 0, &eax, &ebx, &ecx, &edx);
486 x86_cpu_def->xlevel = eax;
488 host_cpuid(0x80000001, 0, &eax, &ebx, &ecx, &edx);
489 x86_cpu_def->ext2_features = edx;
490 x86_cpu_def->ext3_features = ecx;
491 cpu_x86_fill_model_id(x86_cpu_def->model_id);
492 x86_cpu_def->vendor_override = 0;
494 return 0;
497 static int unavailable_host_feature(struct model_features_t *f, uint32_t mask)
499 int i;
501 for (i = 0; i < 32; ++i)
502 if (1 << i & mask) {
503 fprintf(stderr, "warning: host cpuid %04x_%04x lacks requested"
504 " flag '%s' [0x%08x]\n",
505 f->cpuid >> 16, f->cpuid & 0xffff,
506 f->flag_names[i] ? f->flag_names[i] : "[reserved]", mask);
507 break;
509 return 0;
512 /* best effort attempt to inform user requested cpu flags aren't making
513 * their way to the guest. Note: ft[].check_feat ideally should be
514 * specified via a guest_def field to suppress report of extraneous flags.
516 static int check_features_against_host(x86_def_t *guest_def)
518 x86_def_t host_def;
519 uint32_t mask;
520 int rv, i;
521 struct model_features_t ft[] = {
522 {&guest_def->features, &host_def.features,
523 ~0, feature_name, 0x00000000},
524 {&guest_def->ext_features, &host_def.ext_features,
525 ~CPUID_EXT_HYPERVISOR, ext_feature_name, 0x00000001},
526 {&guest_def->ext2_features, &host_def.ext2_features,
527 ~PPRO_FEATURES, ext2_feature_name, 0x80000000},
528 {&guest_def->ext3_features, &host_def.ext3_features,
529 ~CPUID_EXT3_SVM, ext3_feature_name, 0x80000001}};
531 cpu_x86_fill_host(&host_def);
532 for (rv = 0, i = 0; i < sizeof (ft) / sizeof (ft[0]); ++i)
533 for (mask = 1; mask; mask <<= 1)
534 if (ft[i].check_feat & mask && *ft[i].guest_feat & mask &&
535 !(*ft[i].host_feat & mask)) {
536 unavailable_host_feature(&ft[i], mask);
537 rv = 1;
539 return rv;
542 static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
544 unsigned int i;
545 x86_def_t *def;
547 char *s = strdup(cpu_model);
548 char *featurestr, *name = strtok(s, ",");
549 uint32_t plus_features = 0, plus_ext_features = 0, plus_ext2_features = 0, plus_ext3_features = 0, plus_kvm_features = 0;
550 uint32_t minus_features = 0, minus_ext_features = 0, minus_ext2_features = 0, minus_ext3_features = 0, minus_kvm_features = 0;
551 uint32_t numvalue;
553 for (def = x86_defs; def; def = def->next)
554 if (!strcmp(name, def->name))
555 break;
556 if (kvm_enabled() && strcmp(name, "host") == 0) {
557 cpu_x86_fill_host(x86_cpu_def);
558 } else if (!def) {
559 goto error;
560 } else {
561 memcpy(x86_cpu_def, def, sizeof(*def));
564 plus_kvm_features = ~0; /* not supported bits will be filtered out later */
566 add_flagname_to_bitmaps("hypervisor", &plus_features,
567 &plus_ext_features, &plus_ext2_features, &plus_ext3_features,
568 &plus_kvm_features);
570 featurestr = strtok(NULL, ",");
572 while (featurestr) {
573 char *val;
574 if (featurestr[0] == '+') {
575 add_flagname_to_bitmaps(featurestr + 1, &plus_features, &plus_ext_features, &plus_ext2_features, &plus_ext3_features, &plus_kvm_features);
576 } else if (featurestr[0] == '-') {
577 add_flagname_to_bitmaps(featurestr + 1, &minus_features, &minus_ext_features, &minus_ext2_features, &minus_ext3_features, &minus_kvm_features);
578 } else if ((val = strchr(featurestr, '='))) {
579 *val = 0; val++;
580 if (!strcmp(featurestr, "family")) {
581 char *err;
582 numvalue = strtoul(val, &err, 0);
583 if (!*val || *err) {
584 fprintf(stderr, "bad numerical value %s\n", val);
585 goto error;
587 x86_cpu_def->family = numvalue;
588 } else if (!strcmp(featurestr, "model")) {
589 char *err;
590 numvalue = strtoul(val, &err, 0);
591 if (!*val || *err || numvalue > 0xff) {
592 fprintf(stderr, "bad numerical value %s\n", val);
593 goto error;
595 x86_cpu_def->model = numvalue;
596 } else if (!strcmp(featurestr, "stepping")) {
597 char *err;
598 numvalue = strtoul(val, &err, 0);
599 if (!*val || *err || numvalue > 0xf) {
600 fprintf(stderr, "bad numerical value %s\n", val);
601 goto error;
603 x86_cpu_def->stepping = numvalue ;
604 } else if (!strcmp(featurestr, "level")) {
605 char *err;
606 numvalue = strtoul(val, &err, 0);
607 if (!*val || *err) {
608 fprintf(stderr, "bad numerical value %s\n", val);
609 goto error;
611 x86_cpu_def->level = numvalue;
612 } else if (!strcmp(featurestr, "xlevel")) {
613 char *err;
614 numvalue = strtoul(val, &err, 0);
615 if (!*val || *err) {
616 fprintf(stderr, "bad numerical value %s\n", val);
617 goto error;
619 if (numvalue < 0x80000000) {
620 numvalue += 0x80000000;
622 x86_cpu_def->xlevel = numvalue;
623 } else if (!strcmp(featurestr, "vendor")) {
624 if (strlen(val) != 12) {
625 fprintf(stderr, "vendor string must be 12 chars long\n");
626 goto error;
628 x86_cpu_def->vendor1 = 0;
629 x86_cpu_def->vendor2 = 0;
630 x86_cpu_def->vendor3 = 0;
631 for(i = 0; i < 4; i++) {
632 x86_cpu_def->vendor1 |= ((uint8_t)val[i ]) << (8 * i);
633 x86_cpu_def->vendor2 |= ((uint8_t)val[i + 4]) << (8 * i);
634 x86_cpu_def->vendor3 |= ((uint8_t)val[i + 8]) << (8 * i);
636 x86_cpu_def->vendor_override = 1;
637 } else if (!strcmp(featurestr, "model_id")) {
638 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
639 val);
640 } else {
641 fprintf(stderr, "unrecognized feature %s\n", featurestr);
642 goto error;
644 } else if (!strcmp(featurestr, "check")) {
645 check_cpuid = 1;
646 } else if (!strcmp(featurestr, "enforce")) {
647 check_cpuid = enforce_cpuid = 1;
648 } else {
649 fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
650 goto error;
652 featurestr = strtok(NULL, ",");
654 x86_cpu_def->features |= plus_features;
655 x86_cpu_def->ext_features |= plus_ext_features;
656 x86_cpu_def->ext2_features |= plus_ext2_features;
657 x86_cpu_def->ext3_features |= plus_ext3_features;
658 x86_cpu_def->kvm_features |= plus_kvm_features;
659 x86_cpu_def->features &= ~minus_features;
660 x86_cpu_def->ext_features &= ~minus_ext_features;
661 x86_cpu_def->ext2_features &= ~minus_ext2_features;
662 x86_cpu_def->ext3_features &= ~minus_ext3_features;
663 x86_cpu_def->kvm_features &= ~minus_kvm_features;
664 if (check_cpuid) {
665 if (check_features_against_host(x86_cpu_def) && enforce_cpuid)
666 goto error;
668 free(s);
669 return 0;
671 error:
672 free(s);
673 return -1;
676 /* generate a composite string into buf of all cpuid names in featureset
677 * selected by fbits. indicate truncation at bufsize in the event of overflow.
678 * if flags, suppress names undefined in featureset.
680 static void listflags(char *buf, int bufsize, uint32_t fbits,
681 const char **featureset, uint32_t flags)
683 const char **p = &featureset[31];
684 char *q, *b, bit;
685 int nc;
687 b = 4 <= bufsize ? buf + (bufsize -= 3) - 1 : NULL;
688 *buf = '\0';
689 for (q = buf, bit = 31; fbits && bufsize; --p, fbits &= ~(1 << bit), --bit)
690 if (fbits & 1 << bit && (*p || !flags)) {
691 if (*p)
692 nc = snprintf(q, bufsize, "%s%s", q == buf ? "" : " ", *p);
693 else
694 nc = snprintf(q, bufsize, "%s[%d]", q == buf ? "" : " ", bit);
695 if (bufsize <= nc) {
696 if (b) {
697 memcpy(b, "...", sizeof("..."));
699 return;
701 q += nc;
702 bufsize -= nc;
706 /* generate CPU information:
707 * -? list model names
708 * -?model list model names/IDs
709 * -?dump output all model (x86_def_t) data
710 * -?cpuid list all recognized cpuid flag names
712 void x86_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
713 const char *optarg)
715 unsigned char model = !strcmp("?model", optarg);
716 unsigned char dump = !strcmp("?dump", optarg);
717 unsigned char cpuid = !strcmp("?cpuid", optarg);
718 x86_def_t *def;
719 char buf[256];
721 if (cpuid) {
722 (*cpu_fprintf)(f, "Recognized CPUID flags:\n");
723 listflags(buf, sizeof (buf), (uint32_t)~0, feature_name, 1);
724 (*cpu_fprintf)(f, " f_edx: %s\n", buf);
725 listflags(buf, sizeof (buf), (uint32_t)~0, ext_feature_name, 1);
726 (*cpu_fprintf)(f, " f_ecx: %s\n", buf);
727 listflags(buf, sizeof (buf), (uint32_t)~0, ext2_feature_name, 1);
728 (*cpu_fprintf)(f, " extf_edx: %s\n", buf);
729 listflags(buf, sizeof (buf), (uint32_t)~0, ext3_feature_name, 1);
730 (*cpu_fprintf)(f, " extf_ecx: %s\n", buf);
731 return;
733 for (def = x86_defs; def; def = def->next) {
734 snprintf(buf, sizeof (buf), def->flags ? "[%s]": "%s", def->name);
735 if (model || dump) {
736 (*cpu_fprintf)(f, "x86 %16s %-48s\n", buf, def->model_id);
737 } else {
738 (*cpu_fprintf)(f, "x86 %16s\n", buf);
740 if (dump) {
741 memcpy(buf, &def->vendor1, sizeof (def->vendor1));
742 memcpy(buf + 4, &def->vendor2, sizeof (def->vendor2));
743 memcpy(buf + 8, &def->vendor3, sizeof (def->vendor3));
744 buf[12] = '\0';
745 (*cpu_fprintf)(f,
746 " family %d model %d stepping %d level %d xlevel 0x%x"
747 " vendor \"%s\"\n",
748 def->family, def->model, def->stepping, def->level,
749 def->xlevel, buf);
750 listflags(buf, sizeof (buf), def->features, feature_name, 0);
751 (*cpu_fprintf)(f, " feature_edx %08x (%s)\n", def->features,
752 buf);
753 listflags(buf, sizeof (buf), def->ext_features, ext_feature_name,
755 (*cpu_fprintf)(f, " feature_ecx %08x (%s)\n", def->ext_features,
756 buf);
757 listflags(buf, sizeof (buf), def->ext2_features, ext2_feature_name,
759 (*cpu_fprintf)(f, " extfeature_edx %08x (%s)\n",
760 def->ext2_features, buf);
761 listflags(buf, sizeof (buf), def->ext3_features, ext3_feature_name,
763 (*cpu_fprintf)(f, " extfeature_ecx %08x (%s)\n",
764 def->ext3_features, buf);
765 (*cpu_fprintf)(f, "\n");
768 if (kvm_enabled()) {
769 (*cpu_fprintf)(f, "x86 %16s\n", "[host]");
773 int cpu_x86_register (CPUX86State *env, const char *cpu_model)
775 x86_def_t def1, *def = &def1;
777 if (cpu_x86_find_by_name(def, cpu_model) < 0)
778 return -1;
779 if (def->vendor1) {
780 env->cpuid_vendor1 = def->vendor1;
781 env->cpuid_vendor2 = def->vendor2;
782 env->cpuid_vendor3 = def->vendor3;
783 } else {
784 env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1;
785 env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2;
786 env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3;
788 env->cpuid_vendor_override = def->vendor_override;
789 env->cpuid_level = def->level;
790 if (def->family > 0x0f)
791 env->cpuid_version = 0xf00 | ((def->family - 0x0f) << 20);
792 else
793 env->cpuid_version = def->family << 8;
794 env->cpuid_version |= ((def->model & 0xf) << 4) | ((def->model >> 4) << 16);
795 env->cpuid_version |= def->stepping;
796 env->cpuid_features = def->features;
797 env->pat = 0x0007040600070406ULL;
798 env->cpuid_ext_features = def->ext_features;
799 env->cpuid_ext2_features = def->ext2_features;
800 env->cpuid_ext3_features = def->ext3_features;
801 env->cpuid_xlevel = def->xlevel;
802 env->cpuid_kvm_features = def->kvm_features;
803 if (!kvm_enabled()) {
804 env->cpuid_features &= TCG_FEATURES;
805 env->cpuid_ext_features &= TCG_EXT_FEATURES;
806 env->cpuid_ext2_features &= (TCG_EXT2_FEATURES
807 #ifdef TARGET_X86_64
808 | CPUID_EXT2_SYSCALL | CPUID_EXT2_LM
809 #endif
811 env->cpuid_ext3_features &= TCG_EXT3_FEATURES;
814 const char *model_id = def->model_id;
815 int c, len, i;
816 if (!model_id)
817 model_id = "";
818 len = strlen(model_id);
819 for(i = 0; i < 48; i++) {
820 if (i >= len)
821 c = '\0';
822 else
823 c = (uint8_t)model_id[i];
824 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
827 return 0;
830 #if !defined(CONFIG_USER_ONLY)
831 /* copy vendor id string to 32 bit register, nul pad as needed
833 static void cpyid(const char *s, uint32_t *id)
835 char *d = (char *)id;
836 char i;
838 for (i = sizeof (*id); i--; )
839 *d++ = *s ? *s++ : '\0';
842 /* interpret radix and convert from string to arbitrary scalar,
843 * otherwise flag failure
845 #define setscalar(pval, str, perr) \
847 char *pend; \
848 unsigned long ul; \
850 ul = strtoul(str, &pend, 0); \
851 *str && !*pend ? (*pval = ul) : (*perr = 1); \
854 /* map cpuid options to feature bits, otherwise return failure
855 * (option tags in *str are delimited by whitespace)
857 static void setfeatures(uint32_t *pval, const char *str,
858 const char **featureset, int *perr)
860 const char *p, *q;
862 for (q = p = str; *p || *q; q = p) {
863 while (iswhite(*p))
864 q = ++p;
865 while (*p && !iswhite(*p))
866 ++p;
867 if (!*q && !*p)
868 return;
869 if (!lookup_feature(pval, q, p, featureset)) {
870 fprintf(stderr, "error: feature \"%.*s\" not available in set\n",
871 (int)(p - q), q);
872 *perr = 1;
873 return;
878 /* map config file options to x86_def_t form
880 static int cpudef_setfield(const char *name, const char *str, void *opaque)
882 x86_def_t *def = opaque;
883 int err = 0;
885 if (!strcmp(name, "name")) {
886 def->name = strdup(str);
887 } else if (!strcmp(name, "model_id")) {
888 strncpy(def->model_id, str, sizeof (def->model_id));
889 } else if (!strcmp(name, "level")) {
890 setscalar(&def->level, str, &err)
891 } else if (!strcmp(name, "vendor")) {
892 cpyid(&str[0], &def->vendor1);
893 cpyid(&str[4], &def->vendor2);
894 cpyid(&str[8], &def->vendor3);
895 } else if (!strcmp(name, "family")) {
896 setscalar(&def->family, str, &err)
897 } else if (!strcmp(name, "model")) {
898 setscalar(&def->model, str, &err)
899 } else if (!strcmp(name, "stepping")) {
900 setscalar(&def->stepping, str, &err)
901 } else if (!strcmp(name, "feature_edx")) {
902 setfeatures(&def->features, str, feature_name, &err);
903 } else if (!strcmp(name, "feature_ecx")) {
904 setfeatures(&def->ext_features, str, ext_feature_name, &err);
905 } else if (!strcmp(name, "extfeature_edx")) {
906 setfeatures(&def->ext2_features, str, ext2_feature_name, &err);
907 } else if (!strcmp(name, "extfeature_ecx")) {
908 setfeatures(&def->ext3_features, str, ext3_feature_name, &err);
909 } else if (!strcmp(name, "xlevel")) {
910 setscalar(&def->xlevel, str, &err)
911 } else {
912 fprintf(stderr, "error: unknown option [%s = %s]\n", name, str);
913 return (1);
915 if (err) {
916 fprintf(stderr, "error: bad option value [%s = %s]\n", name, str);
917 return (1);
919 return (0);
922 /* register config file entry as x86_def_t
924 static int cpudef_register(QemuOpts *opts, void *opaque)
926 x86_def_t *def = qemu_mallocz(sizeof (x86_def_t));
928 qemu_opt_foreach(opts, cpudef_setfield, def, 1);
929 def->next = x86_defs;
930 x86_defs = def;
931 return (0);
933 #endif /* !CONFIG_USER_ONLY */
935 /* register "cpudef" models defined in configuration file. Here we first
936 * preload any built-in definitions
938 void x86_cpudef_setup(void)
940 int i;
942 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); ++i) {
943 builtin_x86_defs[i].next = x86_defs;
944 builtin_x86_defs[i].flags = 1;
945 x86_defs = &builtin_x86_defs[i];
947 #if !defined(CONFIG_USER_ONLY)
948 qemu_opts_foreach(&qemu_cpudef_opts, cpudef_register, NULL, 0);
949 #endif
952 static void get_cpuid_vendor(CPUX86State *env, uint32_t *ebx,
953 uint32_t *ecx, uint32_t *edx)
955 *ebx = env->cpuid_vendor1;
956 *edx = env->cpuid_vendor2;
957 *ecx = env->cpuid_vendor3;
959 /* sysenter isn't supported on compatibility mode on AMD, syscall
960 * isn't supported in compatibility mode on Intel.
961 * Normally we advertise the actual cpu vendor, but you can override
962 * this if you want to use KVM's sysenter/syscall emulation
963 * in compatibility mode and when doing cross vendor migration
965 if (kvm_enabled() && env->cpuid_vendor_override) {
966 host_cpuid(0, 0, NULL, ebx, ecx, edx);
970 void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
971 uint32_t *eax, uint32_t *ebx,
972 uint32_t *ecx, uint32_t *edx)
974 /* test if maximum index reached */
975 if (index & 0x80000000) {
976 if (index > env->cpuid_xlevel)
977 index = env->cpuid_level;
978 } else {
979 if (index > env->cpuid_level)
980 index = env->cpuid_level;
983 switch(index) {
984 case 0:
985 *eax = env->cpuid_level;
986 get_cpuid_vendor(env, ebx, ecx, edx);
987 break;
988 case 1:
989 *eax = env->cpuid_version;
990 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
991 *ecx = env->cpuid_ext_features;
992 *edx = env->cpuid_features;
993 if (env->nr_cores * env->nr_threads > 1) {
994 *ebx |= (env->nr_cores * env->nr_threads) << 16;
995 *edx |= 1 << 28; /* HTT bit */
997 break;
998 case 2:
999 /* cache info: needed for Pentium Pro compatibility */
1000 *eax = 1;
1001 *ebx = 0;
1002 *ecx = 0;
1003 *edx = 0x2c307d;
1004 break;
1005 case 4:
1006 /* cache info: needed for Core compatibility */
1007 if (env->nr_cores > 1) {
1008 *eax = (env->nr_cores - 1) << 26;
1009 } else {
1010 *eax = 0;
1012 switch (count) {
1013 case 0: /* L1 dcache info */
1014 *eax |= 0x0000121;
1015 *ebx = 0x1c0003f;
1016 *ecx = 0x000003f;
1017 *edx = 0x0000001;
1018 break;
1019 case 1: /* L1 icache info */
1020 *eax |= 0x0000122;
1021 *ebx = 0x1c0003f;
1022 *ecx = 0x000003f;
1023 *edx = 0x0000001;
1024 break;
1025 case 2: /* L2 cache info */
1026 *eax |= 0x0000143;
1027 if (env->nr_threads > 1) {
1028 *eax |= (env->nr_threads - 1) << 14;
1030 *ebx = 0x3c0003f;
1031 *ecx = 0x0000fff;
1032 *edx = 0x0000001;
1033 break;
1034 default: /* end of info */
1035 *eax = 0;
1036 *ebx = 0;
1037 *ecx = 0;
1038 *edx = 0;
1039 break;
1041 break;
1042 case 5:
1043 /* mwait info: needed for Core compatibility */
1044 *eax = 0; /* Smallest monitor-line size in bytes */
1045 *ebx = 0; /* Largest monitor-line size in bytes */
1046 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
1047 *edx = 0;
1048 break;
1049 case 6:
1050 /* Thermal and Power Leaf */
1051 *eax = 0;
1052 *ebx = 0;
1053 *ecx = 0;
1054 *edx = 0;
1055 break;
1056 case 9:
1057 /* Direct Cache Access Information Leaf */
1058 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1059 *ebx = 0;
1060 *ecx = 0;
1061 *edx = 0;
1062 break;
1063 case 0xA:
1064 /* Architectural Performance Monitoring Leaf */
1065 *eax = 0;
1066 *ebx = 0;
1067 *ecx = 0;
1068 *edx = 0;
1069 break;
1070 case 0x80000000:
1071 *eax = env->cpuid_xlevel;
1072 *ebx = env->cpuid_vendor1;
1073 *edx = env->cpuid_vendor2;
1074 *ecx = env->cpuid_vendor3;
1075 break;
1076 case 0x80000001:
1077 *eax = env->cpuid_version;
1078 *ebx = 0;
1079 *ecx = env->cpuid_ext3_features;
1080 *edx = env->cpuid_ext2_features;
1082 /* The Linux kernel checks for the CMPLegacy bit and
1083 * discards multiple thread information if it is set.
1084 * So dont set it here for Intel to make Linux guests happy.
1086 if (env->nr_cores * env->nr_threads > 1) {
1087 uint32_t tebx, tecx, tedx;
1088 get_cpuid_vendor(env, &tebx, &tecx, &tedx);
1089 if (tebx != CPUID_VENDOR_INTEL_1 ||
1090 tedx != CPUID_VENDOR_INTEL_2 ||
1091 tecx != CPUID_VENDOR_INTEL_3) {
1092 *ecx |= 1 << 1; /* CmpLegacy bit */
1096 if (kvm_enabled()) {
1097 /* Nested SVM not yet supported in upstream QEMU */
1098 *ecx &= ~CPUID_EXT3_SVM;
1100 break;
1101 case 0x80000002:
1102 case 0x80000003:
1103 case 0x80000004:
1104 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
1105 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
1106 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
1107 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
1108 break;
1109 case 0x80000005:
1110 /* cache info (L1 cache) */
1111 *eax = 0x01ff01ff;
1112 *ebx = 0x01ff01ff;
1113 *ecx = 0x40020140;
1114 *edx = 0x40020140;
1115 break;
1116 case 0x80000006:
1117 /* cache info (L2 cache) */
1118 *eax = 0;
1119 *ebx = 0x42004200;
1120 *ecx = 0x02008140;
1121 *edx = 0;
1122 break;
1123 case 0x80000008:
1124 /* virtual & phys address size in low 2 bytes. */
1125 /* XXX: This value must match the one used in the MMU code. */
1126 if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
1127 /* 64 bit processor */
1128 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1129 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
1130 } else {
1131 if (env->cpuid_features & CPUID_PSE36)
1132 *eax = 0x00000024; /* 36 bits physical */
1133 else
1134 *eax = 0x00000020; /* 32 bits physical */
1136 *ebx = 0;
1137 *ecx = 0;
1138 *edx = 0;
1139 if (env->nr_cores * env->nr_threads > 1) {
1140 *ecx |= (env->nr_cores * env->nr_threads) - 1;
1142 break;
1143 case 0x8000000A:
1144 *eax = 0x00000001; /* SVM Revision */
1145 *ebx = 0x00000010; /* nr of ASIDs */
1146 *ecx = 0;
1147 *edx = 0; /* optional features */
1148 break;
1149 default:
1150 /* reserved values: zero */
1151 *eax = 0;
1152 *ebx = 0;
1153 *ecx = 0;
1154 *edx = 0;
1155 break;