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