]> git.proxmox.com Git - mirror_qemu.git/blame - target-i386/cpu.c
target-i386: Add "family" property to X86CPU
[mirror_qemu.git] / target-i386 / cpu.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
71ad61d3
AF
30#include "qapi/qapi-visit-core.h"
31
28f52cc0
VR
32#include "hyperv.h"
33
c6dc6f63
AP
34/* feature flags taken from "Intel Processor Identification and the CPUID
35 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
36 * between feature naming conventions, aliases may be added.
37 */
38static const char *feature_name[] = {
39 "fpu", "vme", "de", "pse",
40 "tsc", "msr", "pae", "mce",
41 "cx8", "apic", NULL, "sep",
42 "mtrr", "pge", "mca", "cmov",
43 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
44 NULL, "ds" /* Intel dts */, "acpi", "mmx",
45 "fxsr", "sse", "sse2", "ss",
46 "ht" /* Intel htt */, "tm", "ia64", "pbe",
47};
48static const char *ext_feature_name[] = {
f370be3c 49 "pni|sse3" /* Intel,AMD sse3 */, "pclmulqdq|pclmuldq", "dtes64", "monitor",
e117f772 50 "ds_cpl", "vmx", "smx", "est",
c6dc6f63 51 "tm2", "ssse3", "cid", NULL,
e117f772 52 "fma", "cx16", "xtpr", "pdcm",
c6dc6f63 53 NULL, NULL, "dca", "sse4.1|sse4_1",
e117f772 54 "sse4.2|sse4_2", "x2apic", "movbe", "popcnt",
eaf3f097 55 "tsc-deadline", "aes", "xsave", "osxsave",
e117f772 56 "avx", NULL, NULL, "hypervisor",
c6dc6f63
AP
57};
58static const char *ext2_feature_name[] = {
59 "fpu", "vme", "de", "pse",
60 "tsc", "msr", "pae", "mce",
61 "cx8" /* AMD CMPXCHG8B */, "apic", NULL, "syscall",
62 "mtrr", "pge", "mca", "cmov",
63 "pat", "pse36", NULL, NULL /* Linux mp */,
3ac8ebfe 64 "nx|xd", NULL, "mmxext", "mmx",
f370be3c 65 "fxsr", "fxsr_opt|ffxsr", "pdpe1gb" /* AMD Page1GB */, "rdtscp",
3ac8ebfe 66 NULL, "lm|i64", "3dnowext", "3dnow",
c6dc6f63
AP
67};
68static const char *ext3_feature_name[] = {
69 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
70 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
e117f772 71 "3dnowprefetch", "osvw", "ibs", "xop",
c6dc6f63 72 "skinit", "wdt", NULL, NULL,
e117f772 73 "fma4", NULL, "cvt16", "nodeid_msr",
c6dc6f63
AP
74 NULL, NULL, NULL, NULL,
75 NULL, NULL, NULL, NULL,
76 NULL, NULL, NULL, NULL,
77};
78
79static const char *kvm_feature_name[] = {
642258c6 80 "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock", "kvm_asyncpf", NULL, NULL, NULL,
c6dc6f63
AP
81 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
82 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
83 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
84};
85
296acb64
JR
86static const char *svm_feature_name[] = {
87 "npt", "lbrv", "svm_lock", "nrip_save",
88 "tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists",
89 NULL, NULL, "pause_filter", NULL,
90 "pfthreshold", NULL, NULL, NULL,
91 NULL, NULL, NULL, NULL,
92 NULL, NULL, NULL, NULL,
93 NULL, NULL, NULL, NULL,
94 NULL, NULL, NULL, NULL,
95};
96
c6dc6f63
AP
97/* collects per-function cpuid data
98 */
99typedef struct model_features_t {
100 uint32_t *guest_feat;
101 uint32_t *host_feat;
102 uint32_t check_feat;
103 const char **flag_names;
104 uint32_t cpuid;
105 } model_features_t;
106
107int check_cpuid = 0;
108int enforce_cpuid = 0;
109
bb44e0d1
JK
110void host_cpuid(uint32_t function, uint32_t count,
111 uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
bdde476a
AP
112{
113#if defined(CONFIG_KVM)
a1fd24af
AL
114 uint32_t vec[4];
115
116#ifdef __x86_64__
117 asm volatile("cpuid"
118 : "=a"(vec[0]), "=b"(vec[1]),
119 "=c"(vec[2]), "=d"(vec[3])
120 : "0"(function), "c"(count) : "cc");
121#else
122 asm volatile("pusha \n\t"
123 "cpuid \n\t"
124 "mov %%eax, 0(%2) \n\t"
125 "mov %%ebx, 4(%2) \n\t"
126 "mov %%ecx, 8(%2) \n\t"
127 "mov %%edx, 12(%2) \n\t"
128 "popa"
129 : : "a"(function), "c"(count), "S"(vec)
130 : "memory", "cc");
131#endif
132
bdde476a 133 if (eax)
a1fd24af 134 *eax = vec[0];
bdde476a 135 if (ebx)
a1fd24af 136 *ebx = vec[1];
bdde476a 137 if (ecx)
a1fd24af 138 *ecx = vec[2];
bdde476a 139 if (edx)
a1fd24af 140 *edx = vec[3];
bdde476a
AP
141#endif
142}
c6dc6f63
AP
143
144#define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
145
146/* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
147 * a substring. ex if !NULL points to the first char after a substring,
148 * otherwise the string is assumed to sized by a terminating nul.
149 * Return lexical ordering of *s1:*s2.
150 */
151static int sstrcmp(const char *s1, const char *e1, const char *s2,
152 const char *e2)
153{
154 for (;;) {
155 if (!*s1 || !*s2 || *s1 != *s2)
156 return (*s1 - *s2);
157 ++s1, ++s2;
158 if (s1 == e1 && s2 == e2)
159 return (0);
160 else if (s1 == e1)
161 return (*s2);
162 else if (s2 == e2)
163 return (*s1);
164 }
165}
166
167/* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
168 * '|' delimited (possibly empty) strings in which case search for a match
169 * within the alternatives proceeds left to right. Return 0 for success,
170 * non-zero otherwise.
171 */
172static int altcmp(const char *s, const char *e, const char *altstr)
173{
174 const char *p, *q;
175
176 for (q = p = altstr; ; ) {
177 while (*p && *p != '|')
178 ++p;
179 if ((q == p && !*s) || (q != p && !sstrcmp(s, e, q, p)))
180 return (0);
181 if (!*p)
182 return (1);
183 else
184 q = ++p;
185 }
186}
187
188/* search featureset for flag *[s..e), if found set corresponding bit in
e41e0fc6 189 * *pval and return true, otherwise return false
c6dc6f63 190 */
e41e0fc6
JK
191static bool lookup_feature(uint32_t *pval, const char *s, const char *e,
192 const char **featureset)
c6dc6f63
AP
193{
194 uint32_t mask;
195 const char **ppc;
e41e0fc6 196 bool found = false;
c6dc6f63 197
e41e0fc6 198 for (mask = 1, ppc = featureset; mask; mask <<= 1, ++ppc) {
c6dc6f63
AP
199 if (*ppc && !altcmp(s, e, *ppc)) {
200 *pval |= mask;
e41e0fc6 201 found = true;
c6dc6f63 202 }
e41e0fc6
JK
203 }
204 return found;
c6dc6f63
AP
205}
206
207static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features,
208 uint32_t *ext_features,
209 uint32_t *ext2_features,
210 uint32_t *ext3_features,
296acb64
JR
211 uint32_t *kvm_features,
212 uint32_t *svm_features)
c6dc6f63
AP
213{
214 if (!lookup_feature(features, flagname, NULL, feature_name) &&
215 !lookup_feature(ext_features, flagname, NULL, ext_feature_name) &&
216 !lookup_feature(ext2_features, flagname, NULL, ext2_feature_name) &&
217 !lookup_feature(ext3_features, flagname, NULL, ext3_feature_name) &&
296acb64
JR
218 !lookup_feature(kvm_features, flagname, NULL, kvm_feature_name) &&
219 !lookup_feature(svm_features, flagname, NULL, svm_feature_name))
c6dc6f63
AP
220 fprintf(stderr, "CPU feature %s not found\n", flagname);
221}
222
223typedef struct x86_def_t {
224 struct x86_def_t *next;
225 const char *name;
226 uint32_t level;
227 uint32_t vendor1, vendor2, vendor3;
228 int family;
229 int model;
230 int stepping;
b862d1fe 231 int tsc_khz;
296acb64
JR
232 uint32_t features, ext_features, ext2_features, ext3_features;
233 uint32_t kvm_features, svm_features;
c6dc6f63
AP
234 uint32_t xlevel;
235 char model_id[48];
236 int vendor_override;
237 uint32_t flags;
b3baa152
BW
238 /* Store the results of Centaur's CPUID instructions */
239 uint32_t ext4_features;
240 uint32_t xlevel2;
c6dc6f63
AP
241} x86_def_t;
242
243#define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
244#define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
245 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
246#define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
247 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
248 CPUID_PSE36 | CPUID_FXSR)
249#define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
250#define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
251 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
252 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
253 CPUID_PAE | CPUID_SEP | CPUID_APIC)
42673936 254#define EXT2_FEATURE_MASK 0x0183F3FF
c6dc6f63 255
551a2dec
AP
256#define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
257 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
258 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
259 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
260 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
8560efed
AJ
261 /* partly implemented:
262 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
263 CPUID_PSE36 (needed for Solaris) */
264 /* missing:
265 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
551a2dec 266#define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \
8713f8ff 267 CPUID_EXT_CX16 | CPUID_EXT_POPCNT | \
551a2dec 268 CPUID_EXT_HYPERVISOR)
8560efed
AJ
269 /* missing:
270 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
8713f8ff 271 CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_XSAVE */
551a2dec
AP
272#define TCG_EXT2_FEATURES ((TCG_FEATURES & EXT2_FEATURE_MASK) | \
273 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
274 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
8560efed
AJ
275 /* missing:
276 CPUID_EXT2_PDPE1GB */
551a2dec
AP
277#define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
278 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
296acb64 279#define TCG_SVM_FEATURES 0
551a2dec 280
c6dc6f63
AP
281/* maintains list of cpu model definitions
282 */
283static x86_def_t *x86_defs = {NULL};
284
285/* built-in cpu model definitions (deprecated)
286 */
287static x86_def_t builtin_x86_defs[] = {
c6dc6f63
AP
288 {
289 .name = "qemu64",
290 .level = 4,
291 .vendor1 = CPUID_VENDOR_AMD_1,
292 .vendor2 = CPUID_VENDOR_AMD_2,
293 .vendor3 = CPUID_VENDOR_AMD_3,
294 .family = 6,
295 .model = 2,
296 .stepping = 3,
297 .features = PPRO_FEATURES |
c6dc6f63 298 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
c6dc6f63
AP
299 CPUID_PSE36,
300 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT,
42673936 301 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) |
c6dc6f63
AP
302 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
303 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
304 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
305 .xlevel = 0x8000000A,
306 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
307 },
308 {
309 .name = "phenom",
310 .level = 5,
311 .vendor1 = CPUID_VENDOR_AMD_1,
312 .vendor2 = CPUID_VENDOR_AMD_2,
313 .vendor3 = CPUID_VENDOR_AMD_3,
314 .family = 16,
315 .model = 2,
316 .stepping = 3,
c6dc6f63
AP
317 .features = PPRO_FEATURES |
318 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
8560efed 319 CPUID_PSE36 | CPUID_VME | CPUID_HT,
c6dc6f63
AP
320 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_CX16 |
321 CPUID_EXT_POPCNT,
42673936 322 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) |
c6dc6f63
AP
323 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
324 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
8560efed 325 CPUID_EXT2_FFXSR | CPUID_EXT2_PDPE1GB | CPUID_EXT2_RDTSCP,
c6dc6f63
AP
326 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
327 CPUID_EXT3_CR8LEG,
328 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
329 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
330 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
331 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
296acb64 332 .svm_features = CPUID_SVM_NPT | CPUID_SVM_LBRV,
c6dc6f63
AP
333 .xlevel = 0x8000001A,
334 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
335 },
336 {
337 .name = "core2duo",
338 .level = 10,
339 .family = 6,
340 .model = 15,
341 .stepping = 11,
c6dc6f63
AP
342 .features = PPRO_FEATURES |
343 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
8560efed
AJ
344 CPUID_PSE36 | CPUID_VME | CPUID_DTS | CPUID_ACPI | CPUID_SS |
345 CPUID_HT | CPUID_TM | CPUID_PBE,
346 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
347 CPUID_EXT_DTES64 | CPUID_EXT_DSCPL | CPUID_EXT_VMX | CPUID_EXT_EST |
348 CPUID_EXT_TM2 | CPUID_EXT_CX16 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
c6dc6f63
AP
349 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
350 .ext3_features = CPUID_EXT3_LAHF_LM,
351 .xlevel = 0x80000008,
352 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
353 },
354 {
355 .name = "kvm64",
356 .level = 5,
357 .vendor1 = CPUID_VENDOR_INTEL_1,
358 .vendor2 = CPUID_VENDOR_INTEL_2,
359 .vendor3 = CPUID_VENDOR_INTEL_3,
360 .family = 15,
361 .model = 6,
362 .stepping = 1,
363 /* Missing: CPUID_VME, CPUID_HT */
364 .features = PPRO_FEATURES |
365 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
366 CPUID_PSE36,
367 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
368 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16,
369 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
42673936 370 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) |
c6dc6f63
AP
371 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
372 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
373 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
374 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
375 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
376 .ext3_features = 0,
377 .xlevel = 0x80000008,
378 .model_id = "Common KVM processor"
379 },
c6dc6f63
AP
380 {
381 .name = "qemu32",
382 .level = 4,
383 .family = 6,
384 .model = 3,
385 .stepping = 3,
386 .features = PPRO_FEATURES,
387 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_POPCNT,
58012d66 388 .xlevel = 0x80000004,
c6dc6f63
AP
389 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
390 },
eafaf1e5
AP
391 {
392 .name = "kvm32",
393 .level = 5,
394 .family = 15,
395 .model = 6,
396 .stepping = 1,
397 .features = PPRO_FEATURES |
398 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_PSE36,
399 .ext_features = CPUID_EXT_SSE3,
400 .ext2_features = PPRO_FEATURES & EXT2_FEATURE_MASK,
401 .ext3_features = 0,
402 .xlevel = 0x80000008,
403 .model_id = "Common 32-bit KVM processor"
404 },
c6dc6f63
AP
405 {
406 .name = "coreduo",
407 .level = 10,
408 .family = 6,
409 .model = 14,
410 .stepping = 8,
c6dc6f63 411 .features = PPRO_FEATURES | CPUID_VME |
8560efed
AJ
412 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_DTS | CPUID_ACPI |
413 CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
414 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_VMX |
415 CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
c6dc6f63
AP
416 .ext2_features = CPUID_EXT2_NX,
417 .xlevel = 0x80000008,
418 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
419 },
420 {
421 .name = "486",
58012d66 422 .level = 1,
c6dc6f63
AP
423 .family = 4,
424 .model = 0,
425 .stepping = 0,
426 .features = I486_FEATURES,
427 .xlevel = 0,
428 },
429 {
430 .name = "pentium",
431 .level = 1,
432 .family = 5,
433 .model = 4,
434 .stepping = 3,
435 .features = PENTIUM_FEATURES,
436 .xlevel = 0,
437 },
438 {
439 .name = "pentium2",
440 .level = 2,
441 .family = 6,
442 .model = 5,
443 .stepping = 2,
444 .features = PENTIUM2_FEATURES,
445 .xlevel = 0,
446 },
447 {
448 .name = "pentium3",
449 .level = 2,
450 .family = 6,
451 .model = 7,
452 .stepping = 3,
453 .features = PENTIUM3_FEATURES,
454 .xlevel = 0,
455 },
456 {
457 .name = "athlon",
458 .level = 2,
459 .vendor1 = CPUID_VENDOR_AMD_1,
460 .vendor2 = CPUID_VENDOR_AMD_2,
461 .vendor3 = CPUID_VENDOR_AMD_3,
462 .family = 6,
463 .model = 2,
464 .stepping = 3,
465 .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR | CPUID_MCA,
42673936 466 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
c6dc6f63
AP
467 .xlevel = 0x80000008,
468 /* XXX: put another string ? */
469 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
470 },
471 {
472 .name = "n270",
473 /* original is on level 10 */
474 .level = 5,
475 .family = 6,
476 .model = 28,
477 .stepping = 2,
478 .features = PPRO_FEATURES |
8560efed
AJ
479 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME | CPUID_DTS |
480 CPUID_ACPI | CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
c6dc6f63 481 /* Some CPUs got no CPUID_SEP */
8560efed
AJ
482 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
483 CPUID_EXT_DSCPL | CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR,
42673936 484 .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | CPUID_EXT2_NX,
8560efed 485 .ext3_features = CPUID_EXT3_LAHF_LM,
c6dc6f63
AP
486 .xlevel = 0x8000000A,
487 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
488 },
489};
490
491static int cpu_x86_fill_model_id(char *str)
492{
493 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
494 int i;
495
496 for (i = 0; i < 3; i++) {
497 host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx);
498 memcpy(str + i * 16 + 0, &eax, 4);
499 memcpy(str + i * 16 + 4, &ebx, 4);
500 memcpy(str + i * 16 + 8, &ecx, 4);
501 memcpy(str + i * 16 + 12, &edx, 4);
502 }
503 return 0;
504}
505
506static int cpu_x86_fill_host(x86_def_t *x86_cpu_def)
507{
508 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
509
510 x86_cpu_def->name = "host";
511 host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
512 x86_cpu_def->level = eax;
513 x86_cpu_def->vendor1 = ebx;
514 x86_cpu_def->vendor2 = edx;
515 x86_cpu_def->vendor3 = ecx;
516
517 host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
518 x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
519 x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
520 x86_cpu_def->stepping = eax & 0x0F;
521 x86_cpu_def->ext_features = ecx;
522 x86_cpu_def->features = edx;
523
524 host_cpuid(0x80000000, 0, &eax, &ebx, &ecx, &edx);
525 x86_cpu_def->xlevel = eax;
526
527 host_cpuid(0x80000001, 0, &eax, &ebx, &ecx, &edx);
528 x86_cpu_def->ext2_features = edx;
529 x86_cpu_def->ext3_features = ecx;
530 cpu_x86_fill_model_id(x86_cpu_def->model_id);
531 x86_cpu_def->vendor_override = 0;
532
b3baa152
BW
533 /* Call Centaur's CPUID instruction. */
534 if (x86_cpu_def->vendor1 == CPUID_VENDOR_VIA_1 &&
535 x86_cpu_def->vendor2 == CPUID_VENDOR_VIA_2 &&
536 x86_cpu_def->vendor3 == CPUID_VENDOR_VIA_3) {
537 host_cpuid(0xC0000000, 0, &eax, &ebx, &ecx, &edx);
538 if (eax >= 0xC0000001) {
539 /* Support VIA max extended level */
540 x86_cpu_def->xlevel2 = eax;
541 host_cpuid(0xC0000001, 0, &eax, &ebx, &ecx, &edx);
542 x86_cpu_def->ext4_features = edx;
543 }
544 }
296acb64
JR
545
546 /*
547 * Every SVM feature requires emulation support in KVM - so we can't just
548 * read the host features here. KVM might even support SVM features not
549 * available on the host hardware. Just set all bits and mask out the
550 * unsupported ones later.
551 */
552 x86_cpu_def->svm_features = -1;
553
c6dc6f63
AP
554 return 0;
555}
556
557static int unavailable_host_feature(struct model_features_t *f, uint32_t mask)
558{
559 int i;
560
561 for (i = 0; i < 32; ++i)
562 if (1 << i & mask) {
563 fprintf(stderr, "warning: host cpuid %04x_%04x lacks requested"
564 " flag '%s' [0x%08x]\n",
565 f->cpuid >> 16, f->cpuid & 0xffff,
566 f->flag_names[i] ? f->flag_names[i] : "[reserved]", mask);
567 break;
568 }
569 return 0;
570}
571
572/* best effort attempt to inform user requested cpu flags aren't making
573 * their way to the guest. Note: ft[].check_feat ideally should be
574 * specified via a guest_def field to suppress report of extraneous flags.
575 */
576static int check_features_against_host(x86_def_t *guest_def)
577{
578 x86_def_t host_def;
579 uint32_t mask;
580 int rv, i;
581 struct model_features_t ft[] = {
582 {&guest_def->features, &host_def.features,
583 ~0, feature_name, 0x00000000},
584 {&guest_def->ext_features, &host_def.ext_features,
585 ~CPUID_EXT_HYPERVISOR, ext_feature_name, 0x00000001},
586 {&guest_def->ext2_features, &host_def.ext2_features,
587 ~PPRO_FEATURES, ext2_feature_name, 0x80000000},
588 {&guest_def->ext3_features, &host_def.ext3_features,
589 ~CPUID_EXT3_SVM, ext3_feature_name, 0x80000001}};
590
591 cpu_x86_fill_host(&host_def);
66fe09ee 592 for (rv = 0, i = 0; i < ARRAY_SIZE(ft); ++i)
c6dc6f63
AP
593 for (mask = 1; mask; mask <<= 1)
594 if (ft[i].check_feat & mask && *ft[i].guest_feat & mask &&
595 !(*ft[i].host_feat & mask)) {
596 unavailable_host_feature(&ft[i], mask);
597 rv = 1;
598 }
599 return rv;
600}
601
71ad61d3
AF
602static void x86_cpuid_version_set_family(Object *obj, Visitor *v, void *opaque,
603 const char *name, Error **errp)
ed5e1ec3 604{
71ad61d3
AF
605 X86CPU *cpu = X86_CPU(obj);
606 CPUX86State *env = &cpu->env;
607 const int64_t min = 0;
608 const int64_t max = 0xff + 0xf;
609 int64_t value;
610
611 visit_type_int(v, &value, name, errp);
612 if (error_is_set(errp)) {
613 return;
614 }
615 if (value < min || value > max) {
616 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
617 name ? name : "null", value, min, max);
618 return;
619 }
620
ed5e1ec3 621 env->cpuid_version &= ~0xff00f00;
71ad61d3
AF
622 if (value > 0x0f) {
623 env->cpuid_version |= 0xf00 | ((value - 0x0f) << 20);
ed5e1ec3 624 } else {
71ad61d3 625 env->cpuid_version |= value << 8;
ed5e1ec3
AF
626 }
627}
628
b0704cbd
AF
629static void x86_cpuid_version_set_model(CPUX86State *env, int model)
630{
631 env->cpuid_version &= ~0xf00f0;
632 env->cpuid_version |= ((model & 0xf) << 4) | ((model >> 4) << 16);
633}
634
38c3dc46
AF
635static void x86_cpuid_version_set_stepping(CPUX86State *env, int stepping)
636{
637 env->cpuid_version &= ~0xf;
638 env->cpuid_version |= stepping & 0xf;
639}
640
dcce6675
AF
641static void x86_cpuid_set_model_id(CPUX86State *env, const char *model_id)
642{
643 int c, len, i;
644
645 if (model_id == NULL) {
646 model_id = "";
647 }
648 len = strlen(model_id);
d0a6acf4 649 memset(env->cpuid_model, 0, 48);
dcce6675
AF
650 for (i = 0; i < 48; i++) {
651 if (i >= len) {
652 c = '\0';
653 } else {
654 c = (uint8_t)model_id[i];
655 }
656 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
657 }
658}
659
c6dc6f63
AP
660static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
661{
662 unsigned int i;
663 x86_def_t *def;
664
d3c481b3 665 char *s = g_strdup(cpu_model);
c6dc6f63 666 char *featurestr, *name = strtok(s, ",");
296acb64
JR
667 /* Features to be added*/
668 uint32_t plus_features = 0, plus_ext_features = 0;
669 uint32_t plus_ext2_features = 0, plus_ext3_features = 0;
670 uint32_t plus_kvm_features = 0, plus_svm_features = 0;
671 /* Features to be removed */
672 uint32_t minus_features = 0, minus_ext_features = 0;
673 uint32_t minus_ext2_features = 0, minus_ext3_features = 0;
674 uint32_t minus_kvm_features = 0, minus_svm_features = 0;
c6dc6f63
AP
675 uint32_t numvalue;
676
677 for (def = x86_defs; def; def = def->next)
04c5b17a 678 if (name && !strcmp(name, def->name))
c6dc6f63 679 break;
04c5b17a 680 if (kvm_enabled() && name && strcmp(name, "host") == 0) {
c6dc6f63
AP
681 cpu_x86_fill_host(x86_cpu_def);
682 } else if (!def) {
683 goto error;
684 } else {
685 memcpy(x86_cpu_def, def, sizeof(*def));
686 }
687
688 plus_kvm_features = ~0; /* not supported bits will be filtered out later */
689
690 add_flagname_to_bitmaps("hypervisor", &plus_features,
691 &plus_ext_features, &plus_ext2_features, &plus_ext3_features,
296acb64 692 &plus_kvm_features, &plus_svm_features);
c6dc6f63
AP
693
694 featurestr = strtok(NULL, ",");
695
696 while (featurestr) {
697 char *val;
698 if (featurestr[0] == '+') {
296acb64
JR
699 add_flagname_to_bitmaps(featurestr + 1, &plus_features,
700 &plus_ext_features, &plus_ext2_features,
701 &plus_ext3_features, &plus_kvm_features,
702 &plus_svm_features);
c6dc6f63 703 } else if (featurestr[0] == '-') {
296acb64
JR
704 add_flagname_to_bitmaps(featurestr + 1, &minus_features,
705 &minus_ext_features, &minus_ext2_features,
706 &minus_ext3_features, &minus_kvm_features,
707 &minus_svm_features);
c6dc6f63
AP
708 } else if ((val = strchr(featurestr, '='))) {
709 *val = 0; val++;
710 if (!strcmp(featurestr, "family")) {
711 char *err;
712 numvalue = strtoul(val, &err, 0);
a88a677f 713 if (!*val || *err || numvalue > 0xff + 0xf) {
c6dc6f63
AP
714 fprintf(stderr, "bad numerical value %s\n", val);
715 goto error;
716 }
717 x86_cpu_def->family = numvalue;
718 } else if (!strcmp(featurestr, "model")) {
719 char *err;
720 numvalue = strtoul(val, &err, 0);
721 if (!*val || *err || numvalue > 0xff) {
722 fprintf(stderr, "bad numerical value %s\n", val);
723 goto error;
724 }
725 x86_cpu_def->model = numvalue;
726 } else if (!strcmp(featurestr, "stepping")) {
727 char *err;
728 numvalue = strtoul(val, &err, 0);
729 if (!*val || *err || numvalue > 0xf) {
730 fprintf(stderr, "bad numerical value %s\n", val);
731 goto error;
732 }
733 x86_cpu_def->stepping = numvalue ;
734 } else if (!strcmp(featurestr, "level")) {
735 char *err;
736 numvalue = strtoul(val, &err, 0);
737 if (!*val || *err) {
738 fprintf(stderr, "bad numerical value %s\n", val);
739 goto error;
740 }
741 x86_cpu_def->level = numvalue;
742 } else if (!strcmp(featurestr, "xlevel")) {
743 char *err;
744 numvalue = strtoul(val, &err, 0);
745 if (!*val || *err) {
746 fprintf(stderr, "bad numerical value %s\n", val);
747 goto error;
748 }
749 if (numvalue < 0x80000000) {
2f7a21c4 750 numvalue += 0x80000000;
c6dc6f63
AP
751 }
752 x86_cpu_def->xlevel = numvalue;
753 } else if (!strcmp(featurestr, "vendor")) {
754 if (strlen(val) != 12) {
755 fprintf(stderr, "vendor string must be 12 chars long\n");
756 goto error;
757 }
758 x86_cpu_def->vendor1 = 0;
759 x86_cpu_def->vendor2 = 0;
760 x86_cpu_def->vendor3 = 0;
761 for(i = 0; i < 4; i++) {
762 x86_cpu_def->vendor1 |= ((uint8_t)val[i ]) << (8 * i);
763 x86_cpu_def->vendor2 |= ((uint8_t)val[i + 4]) << (8 * i);
764 x86_cpu_def->vendor3 |= ((uint8_t)val[i + 8]) << (8 * i);
765 }
766 x86_cpu_def->vendor_override = 1;
767 } else if (!strcmp(featurestr, "model_id")) {
768 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
769 val);
b862d1fe
JR
770 } else if (!strcmp(featurestr, "tsc_freq")) {
771 int64_t tsc_freq;
772 char *err;
773
774 tsc_freq = strtosz_suffix_unit(val, &err,
775 STRTOSZ_DEFSUFFIX_B, 1000);
45009a30 776 if (tsc_freq < 0 || *err) {
b862d1fe
JR
777 fprintf(stderr, "bad numerical value %s\n", val);
778 goto error;
779 }
780 x86_cpu_def->tsc_khz = tsc_freq / 1000;
28f52cc0
VR
781 } else if (!strcmp(featurestr, "hv_spinlocks")) {
782 char *err;
783 numvalue = strtoul(val, &err, 0);
784 if (!*val || *err) {
785 fprintf(stderr, "bad numerical value %s\n", val);
786 goto error;
787 }
788 hyperv_set_spinlock_retries(numvalue);
c6dc6f63
AP
789 } else {
790 fprintf(stderr, "unrecognized feature %s\n", featurestr);
791 goto error;
792 }
793 } else if (!strcmp(featurestr, "check")) {
794 check_cpuid = 1;
795 } else if (!strcmp(featurestr, "enforce")) {
796 check_cpuid = enforce_cpuid = 1;
28f52cc0
VR
797 } else if (!strcmp(featurestr, "hv_relaxed")) {
798 hyperv_enable_relaxed_timing(true);
799 } else if (!strcmp(featurestr, "hv_vapic")) {
800 hyperv_enable_vapic_recommended(true);
c6dc6f63
AP
801 } else {
802 fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
803 goto error;
804 }
805 featurestr = strtok(NULL, ",");
806 }
807 x86_cpu_def->features |= plus_features;
808 x86_cpu_def->ext_features |= plus_ext_features;
809 x86_cpu_def->ext2_features |= plus_ext2_features;
810 x86_cpu_def->ext3_features |= plus_ext3_features;
811 x86_cpu_def->kvm_features |= plus_kvm_features;
296acb64 812 x86_cpu_def->svm_features |= plus_svm_features;
c6dc6f63
AP
813 x86_cpu_def->features &= ~minus_features;
814 x86_cpu_def->ext_features &= ~minus_ext_features;
815 x86_cpu_def->ext2_features &= ~minus_ext2_features;
816 x86_cpu_def->ext3_features &= ~minus_ext3_features;
817 x86_cpu_def->kvm_features &= ~minus_kvm_features;
296acb64 818 x86_cpu_def->svm_features &= ~minus_svm_features;
c6dc6f63
AP
819 if (check_cpuid) {
820 if (check_features_against_host(x86_cpu_def) && enforce_cpuid)
821 goto error;
822 }
d3c481b3 823 g_free(s);
c6dc6f63
AP
824 return 0;
825
826error:
d3c481b3 827 g_free(s);
c6dc6f63
AP
828 return -1;
829}
830
831/* generate a composite string into buf of all cpuid names in featureset
832 * selected by fbits. indicate truncation at bufsize in the event of overflow.
833 * if flags, suppress names undefined in featureset.
834 */
835static void listflags(char *buf, int bufsize, uint32_t fbits,
836 const char **featureset, uint32_t flags)
837{
838 const char **p = &featureset[31];
839 char *q, *b, bit;
840 int nc;
841
842 b = 4 <= bufsize ? buf + (bufsize -= 3) - 1 : NULL;
843 *buf = '\0';
844 for (q = buf, bit = 31; fbits && bufsize; --p, fbits &= ~(1 << bit), --bit)
845 if (fbits & 1 << bit && (*p || !flags)) {
846 if (*p)
847 nc = snprintf(q, bufsize, "%s%s", q == buf ? "" : " ", *p);
848 else
849 nc = snprintf(q, bufsize, "%s[%d]", q == buf ? "" : " ", bit);
850 if (bufsize <= nc) {
851 if (b) {
852 memcpy(b, "...", sizeof("..."));
853 }
854 return;
855 }
856 q += nc;
857 bufsize -= nc;
858 }
859}
860
861/* generate CPU information:
862 * -? list model names
863 * -?model list model names/IDs
864 * -?dump output all model (x86_def_t) data
865 * -?cpuid list all recognized cpuid flag names
866 */
9a78eead 867void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
c6dc6f63
AP
868{
869 unsigned char model = !strcmp("?model", optarg);
870 unsigned char dump = !strcmp("?dump", optarg);
871 unsigned char cpuid = !strcmp("?cpuid", optarg);
872 x86_def_t *def;
873 char buf[256];
874
875 if (cpuid) {
876 (*cpu_fprintf)(f, "Recognized CPUID flags:\n");
877 listflags(buf, sizeof (buf), (uint32_t)~0, feature_name, 1);
878 (*cpu_fprintf)(f, " f_edx: %s\n", buf);
879 listflags(buf, sizeof (buf), (uint32_t)~0, ext_feature_name, 1);
880 (*cpu_fprintf)(f, " f_ecx: %s\n", buf);
881 listflags(buf, sizeof (buf), (uint32_t)~0, ext2_feature_name, 1);
882 (*cpu_fprintf)(f, " extf_edx: %s\n", buf);
883 listflags(buf, sizeof (buf), (uint32_t)~0, ext3_feature_name, 1);
884 (*cpu_fprintf)(f, " extf_ecx: %s\n", buf);
885 return;
886 }
887 for (def = x86_defs; def; def = def->next) {
888 snprintf(buf, sizeof (buf), def->flags ? "[%s]": "%s", def->name);
889 if (model || dump) {
890 (*cpu_fprintf)(f, "x86 %16s %-48s\n", buf, def->model_id);
891 } else {
892 (*cpu_fprintf)(f, "x86 %16s\n", buf);
893 }
894 if (dump) {
895 memcpy(buf, &def->vendor1, sizeof (def->vendor1));
896 memcpy(buf + 4, &def->vendor2, sizeof (def->vendor2));
897 memcpy(buf + 8, &def->vendor3, sizeof (def->vendor3));
898 buf[12] = '\0';
899 (*cpu_fprintf)(f,
900 " family %d model %d stepping %d level %d xlevel 0x%x"
901 " vendor \"%s\"\n",
902 def->family, def->model, def->stepping, def->level,
903 def->xlevel, buf);
904 listflags(buf, sizeof (buf), def->features, feature_name, 0);
905 (*cpu_fprintf)(f, " feature_edx %08x (%s)\n", def->features,
906 buf);
907 listflags(buf, sizeof (buf), def->ext_features, ext_feature_name,
908 0);
909 (*cpu_fprintf)(f, " feature_ecx %08x (%s)\n", def->ext_features,
910 buf);
911 listflags(buf, sizeof (buf), def->ext2_features, ext2_feature_name,
912 0);
913 (*cpu_fprintf)(f, " extfeature_edx %08x (%s)\n",
914 def->ext2_features, buf);
915 listflags(buf, sizeof (buf), def->ext3_features, ext3_feature_name,
916 0);
917 (*cpu_fprintf)(f, " extfeature_ecx %08x (%s)\n",
918 def->ext3_features, buf);
919 (*cpu_fprintf)(f, "\n");
920 }
921 }
ed2c54d4
AP
922 if (kvm_enabled()) {
923 (*cpu_fprintf)(f, "x86 %16s\n", "[host]");
924 }
c6dc6f63
AP
925}
926
61dcd775 927int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
c6dc6f63 928{
61dcd775 929 CPUX86State *env = &cpu->env;
c6dc6f63 930 x86_def_t def1, *def = &def1;
71ad61d3 931 Error *error = NULL;
c6dc6f63 932
db0ad1ba
JR
933 memset(def, 0, sizeof(*def));
934
c6dc6f63
AP
935 if (cpu_x86_find_by_name(def, cpu_model) < 0)
936 return -1;
937 if (def->vendor1) {
938 env->cpuid_vendor1 = def->vendor1;
939 env->cpuid_vendor2 = def->vendor2;
940 env->cpuid_vendor3 = def->vendor3;
941 } else {
942 env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1;
943 env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2;
944 env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3;
945 }
946 env->cpuid_vendor_override = def->vendor_override;
947 env->cpuid_level = def->level;
71ad61d3 948 object_property_set_int(OBJECT(cpu), def->family, "family", &error);
b0704cbd 949 x86_cpuid_version_set_model(env, def->model);
38c3dc46 950 x86_cpuid_version_set_stepping(env, def->stepping);
c6dc6f63 951 env->cpuid_features = def->features;
c6dc6f63
AP
952 env->cpuid_ext_features = def->ext_features;
953 env->cpuid_ext2_features = def->ext2_features;
4d067ed7 954 env->cpuid_ext3_features = def->ext3_features;
c6dc6f63
AP
955 env->cpuid_xlevel = def->xlevel;
956 env->cpuid_kvm_features = def->kvm_features;
296acb64 957 env->cpuid_svm_features = def->svm_features;
b3baa152
BW
958 env->cpuid_ext4_features = def->ext4_features;
959 env->cpuid_xlevel2 = def->xlevel2;
b862d1fe 960 env->tsc_khz = def->tsc_khz;
551a2dec
AP
961 if (!kvm_enabled()) {
962 env->cpuid_features &= TCG_FEATURES;
963 env->cpuid_ext_features &= TCG_EXT_FEATURES;
964 env->cpuid_ext2_features &= (TCG_EXT2_FEATURES
965#ifdef TARGET_X86_64
966 | CPUID_EXT2_SYSCALL | CPUID_EXT2_LM
967#endif
968 );
969 env->cpuid_ext3_features &= TCG_EXT3_FEATURES;
296acb64 970 env->cpuid_svm_features &= TCG_SVM_FEATURES;
551a2dec 971 }
dcce6675 972 x86_cpuid_set_model_id(env, def->model_id);
71ad61d3
AF
973 if (error_is_set(&error)) {
974 error_free(error);
975 return -1;
976 }
c6dc6f63
AP
977 return 0;
978}
979
980#if !defined(CONFIG_USER_ONLY)
981/* copy vendor id string to 32 bit register, nul pad as needed
982 */
983static void cpyid(const char *s, uint32_t *id)
984{
985 char *d = (char *)id;
986 char i;
987
988 for (i = sizeof (*id); i--; )
989 *d++ = *s ? *s++ : '\0';
990}
991
992/* interpret radix and convert from string to arbitrary scalar,
993 * otherwise flag failure
994 */
995#define setscalar(pval, str, perr) \
996{ \
997 char *pend; \
998 unsigned long ul; \
999 \
1000 ul = strtoul(str, &pend, 0); \
1001 *str && !*pend ? (*pval = ul) : (*perr = 1); \
1002}
1003
1004/* map cpuid options to feature bits, otherwise return failure
1005 * (option tags in *str are delimited by whitespace)
1006 */
1007static void setfeatures(uint32_t *pval, const char *str,
1008 const char **featureset, int *perr)
1009{
1010 const char *p, *q;
1011
1012 for (q = p = str; *p || *q; q = p) {
1013 while (iswhite(*p))
1014 q = ++p;
1015 while (*p && !iswhite(*p))
1016 ++p;
1017 if (!*q && !*p)
1018 return;
1019 if (!lookup_feature(pval, q, p, featureset)) {
1020 fprintf(stderr, "error: feature \"%.*s\" not available in set\n",
1021 (int)(p - q), q);
1022 *perr = 1;
1023 return;
1024 }
1025 }
1026}
1027
1028/* map config file options to x86_def_t form
1029 */
1030static int cpudef_setfield(const char *name, const char *str, void *opaque)
1031{
1032 x86_def_t *def = opaque;
1033 int err = 0;
1034
1035 if (!strcmp(name, "name")) {
99e1dec0 1036 g_free((void *)def->name);
d3c481b3 1037 def->name = g_strdup(str);
c6dc6f63
AP
1038 } else if (!strcmp(name, "model_id")) {
1039 strncpy(def->model_id, str, sizeof (def->model_id));
1040 } else if (!strcmp(name, "level")) {
1041 setscalar(&def->level, str, &err)
1042 } else if (!strcmp(name, "vendor")) {
1043 cpyid(&str[0], &def->vendor1);
1044 cpyid(&str[4], &def->vendor2);
1045 cpyid(&str[8], &def->vendor3);
1046 } else if (!strcmp(name, "family")) {
1047 setscalar(&def->family, str, &err)
1048 } else if (!strcmp(name, "model")) {
1049 setscalar(&def->model, str, &err)
1050 } else if (!strcmp(name, "stepping")) {
1051 setscalar(&def->stepping, str, &err)
1052 } else if (!strcmp(name, "feature_edx")) {
1053 setfeatures(&def->features, str, feature_name, &err);
1054 } else if (!strcmp(name, "feature_ecx")) {
1055 setfeatures(&def->ext_features, str, ext_feature_name, &err);
1056 } else if (!strcmp(name, "extfeature_edx")) {
1057 setfeatures(&def->ext2_features, str, ext2_feature_name, &err);
1058 } else if (!strcmp(name, "extfeature_ecx")) {
1059 setfeatures(&def->ext3_features, str, ext3_feature_name, &err);
1060 } else if (!strcmp(name, "xlevel")) {
1061 setscalar(&def->xlevel, str, &err)
1062 } else {
1063 fprintf(stderr, "error: unknown option [%s = %s]\n", name, str);
1064 return (1);
1065 }
1066 if (err) {
1067 fprintf(stderr, "error: bad option value [%s = %s]\n", name, str);
1068 return (1);
1069 }
1070 return (0);
1071}
1072
1073/* register config file entry as x86_def_t
1074 */
1075static int cpudef_register(QemuOpts *opts, void *opaque)
1076{
7267c094 1077 x86_def_t *def = g_malloc0(sizeof (x86_def_t));
c6dc6f63
AP
1078
1079 qemu_opt_foreach(opts, cpudef_setfield, def, 1);
1080 def->next = x86_defs;
1081 x86_defs = def;
1082 return (0);
1083}
0e26b7b8
BS
1084
1085void cpu_clear_apic_feature(CPUX86State *env)
1086{
1087 env->cpuid_features &= ~CPUID_APIC;
1088}
1089
c6dc6f63
AP
1090#endif /* !CONFIG_USER_ONLY */
1091
1092/* register "cpudef" models defined in configuration file. Here we first
1093 * preload any built-in definitions
1094 */
1095void x86_cpudef_setup(void)
1096{
1097 int i;
1098
1099 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); ++i) {
1100 builtin_x86_defs[i].next = x86_defs;
1101 builtin_x86_defs[i].flags = 1;
1102 x86_defs = &builtin_x86_defs[i];
1103 }
1104#if !defined(CONFIG_USER_ONLY)
3329f07b 1105 qemu_opts_foreach(qemu_find_opts("cpudef"), cpudef_register, NULL, 0);
c6dc6f63
AP
1106#endif
1107}
1108
c6dc6f63
AP
1109static void get_cpuid_vendor(CPUX86State *env, uint32_t *ebx,
1110 uint32_t *ecx, uint32_t *edx)
1111{
1112 *ebx = env->cpuid_vendor1;
1113 *edx = env->cpuid_vendor2;
1114 *ecx = env->cpuid_vendor3;
1115
1116 /* sysenter isn't supported on compatibility mode on AMD, syscall
1117 * isn't supported in compatibility mode on Intel.
1118 * Normally we advertise the actual cpu vendor, but you can override
1119 * this if you want to use KVM's sysenter/syscall emulation
1120 * in compatibility mode and when doing cross vendor migration
1121 */
89354998 1122 if (kvm_enabled() && ! env->cpuid_vendor_override) {
c6dc6f63
AP
1123 host_cpuid(0, 0, NULL, ebx, ecx, edx);
1124 }
1125}
1126
1127void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
1128 uint32_t *eax, uint32_t *ebx,
1129 uint32_t *ecx, uint32_t *edx)
1130{
1131 /* test if maximum index reached */
1132 if (index & 0x80000000) {
b3baa152
BW
1133 if (index > env->cpuid_xlevel) {
1134 if (env->cpuid_xlevel2 > 0) {
1135 /* Handle the Centaur's CPUID instruction. */
1136 if (index > env->cpuid_xlevel2) {
1137 index = env->cpuid_xlevel2;
1138 } else if (index < 0xC0000000) {
1139 index = env->cpuid_xlevel;
1140 }
1141 } else {
1142 index = env->cpuid_xlevel;
1143 }
1144 }
c6dc6f63
AP
1145 } else {
1146 if (index > env->cpuid_level)
1147 index = env->cpuid_level;
1148 }
1149
1150 switch(index) {
1151 case 0:
1152 *eax = env->cpuid_level;
1153 get_cpuid_vendor(env, ebx, ecx, edx);
1154 break;
1155 case 1:
1156 *eax = env->cpuid_version;
1157 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1158 *ecx = env->cpuid_ext_features;
1159 *edx = env->cpuid_features;
1160 if (env->nr_cores * env->nr_threads > 1) {
1161 *ebx |= (env->nr_cores * env->nr_threads) << 16;
1162 *edx |= 1 << 28; /* HTT bit */
1163 }
1164 break;
1165 case 2:
1166 /* cache info: needed for Pentium Pro compatibility */
1167 *eax = 1;
1168 *ebx = 0;
1169 *ecx = 0;
1170 *edx = 0x2c307d;
1171 break;
1172 case 4:
1173 /* cache info: needed for Core compatibility */
1174 if (env->nr_cores > 1) {
2f7a21c4 1175 *eax = (env->nr_cores - 1) << 26;
c6dc6f63 1176 } else {
2f7a21c4 1177 *eax = 0;
c6dc6f63
AP
1178 }
1179 switch (count) {
1180 case 0: /* L1 dcache info */
1181 *eax |= 0x0000121;
1182 *ebx = 0x1c0003f;
1183 *ecx = 0x000003f;
1184 *edx = 0x0000001;
1185 break;
1186 case 1: /* L1 icache info */
1187 *eax |= 0x0000122;
1188 *ebx = 0x1c0003f;
1189 *ecx = 0x000003f;
1190 *edx = 0x0000001;
1191 break;
1192 case 2: /* L2 cache info */
1193 *eax |= 0x0000143;
1194 if (env->nr_threads > 1) {
1195 *eax |= (env->nr_threads - 1) << 14;
1196 }
1197 *ebx = 0x3c0003f;
1198 *ecx = 0x0000fff;
1199 *edx = 0x0000001;
1200 break;
1201 default: /* end of info */
1202 *eax = 0;
1203 *ebx = 0;
1204 *ecx = 0;
1205 *edx = 0;
1206 break;
1207 }
1208 break;
1209 case 5:
1210 /* mwait info: needed for Core compatibility */
1211 *eax = 0; /* Smallest monitor-line size in bytes */
1212 *ebx = 0; /* Largest monitor-line size in bytes */
1213 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
1214 *edx = 0;
1215 break;
1216 case 6:
1217 /* Thermal and Power Leaf */
1218 *eax = 0;
1219 *ebx = 0;
1220 *ecx = 0;
1221 *edx = 0;
1222 break;
f7911686
YW
1223 case 7:
1224 if (kvm_enabled()) {
ba9bc59e
JK
1225 KVMState *s = env->kvm_state;
1226
1227 *eax = kvm_arch_get_supported_cpuid(s, 0x7, count, R_EAX);
1228 *ebx = kvm_arch_get_supported_cpuid(s, 0x7, count, R_EBX);
1229 *ecx = kvm_arch_get_supported_cpuid(s, 0x7, count, R_ECX);
1230 *edx = kvm_arch_get_supported_cpuid(s, 0x7, count, R_EDX);
f7911686
YW
1231 } else {
1232 *eax = 0;
1233 *ebx = 0;
1234 *ecx = 0;
1235 *edx = 0;
1236 }
1237 break;
c6dc6f63
AP
1238 case 9:
1239 /* Direct Cache Access Information Leaf */
1240 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1241 *ebx = 0;
1242 *ecx = 0;
1243 *edx = 0;
1244 break;
1245 case 0xA:
1246 /* Architectural Performance Monitoring Leaf */
a0fa8208
GN
1247 if (kvm_enabled()) {
1248 KVMState *s = env->kvm_state;
1249
1250 *eax = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EAX);
1251 *ebx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EBX);
1252 *ecx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_ECX);
1253 *edx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EDX);
1254 } else {
1255 *eax = 0;
1256 *ebx = 0;
1257 *ecx = 0;
1258 *edx = 0;
1259 }
c6dc6f63 1260 break;
51e49430
SY
1261 case 0xD:
1262 /* Processor Extended State */
1263 if (!(env->cpuid_ext_features & CPUID_EXT_XSAVE)) {
1264 *eax = 0;
1265 *ebx = 0;
1266 *ecx = 0;
1267 *edx = 0;
1268 break;
1269 }
1270 if (kvm_enabled()) {
ba9bc59e
JK
1271 KVMState *s = env->kvm_state;
1272
1273 *eax = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EAX);
1274 *ebx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EBX);
1275 *ecx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_ECX);
1276 *edx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EDX);
51e49430
SY
1277 } else {
1278 *eax = 0;
1279 *ebx = 0;
1280 *ecx = 0;
1281 *edx = 0;
1282 }
1283 break;
c6dc6f63
AP
1284 case 0x80000000:
1285 *eax = env->cpuid_xlevel;
1286 *ebx = env->cpuid_vendor1;
1287 *edx = env->cpuid_vendor2;
1288 *ecx = env->cpuid_vendor3;
1289 break;
1290 case 0x80000001:
1291 *eax = env->cpuid_version;
1292 *ebx = 0;
1293 *ecx = env->cpuid_ext3_features;
1294 *edx = env->cpuid_ext2_features;
1295
1296 /* The Linux kernel checks for the CMPLegacy bit and
1297 * discards multiple thread information if it is set.
1298 * So dont set it here for Intel to make Linux guests happy.
1299 */
1300 if (env->nr_cores * env->nr_threads > 1) {
1301 uint32_t tebx, tecx, tedx;
1302 get_cpuid_vendor(env, &tebx, &tecx, &tedx);
1303 if (tebx != CPUID_VENDOR_INTEL_1 ||
1304 tedx != CPUID_VENDOR_INTEL_2 ||
1305 tecx != CPUID_VENDOR_INTEL_3) {
1306 *ecx |= 1 << 1; /* CmpLegacy bit */
1307 }
1308 }
c6dc6f63
AP
1309 break;
1310 case 0x80000002:
1311 case 0x80000003:
1312 case 0x80000004:
1313 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
1314 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
1315 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
1316 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
1317 break;
1318 case 0x80000005:
1319 /* cache info (L1 cache) */
1320 *eax = 0x01ff01ff;
1321 *ebx = 0x01ff01ff;
1322 *ecx = 0x40020140;
1323 *edx = 0x40020140;
1324 break;
1325 case 0x80000006:
1326 /* cache info (L2 cache) */
1327 *eax = 0;
1328 *ebx = 0x42004200;
1329 *ecx = 0x02008140;
1330 *edx = 0;
1331 break;
1332 case 0x80000008:
1333 /* virtual & phys address size in low 2 bytes. */
1334/* XXX: This value must match the one used in the MMU code. */
1335 if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
1336 /* 64 bit processor */
1337/* XXX: The physical address space is limited to 42 bits in exec.c. */
1338 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
1339 } else {
1340 if (env->cpuid_features & CPUID_PSE36)
1341 *eax = 0x00000024; /* 36 bits physical */
1342 else
1343 *eax = 0x00000020; /* 32 bits physical */
1344 }
1345 *ebx = 0;
1346 *ecx = 0;
1347 *edx = 0;
1348 if (env->nr_cores * env->nr_threads > 1) {
1349 *ecx |= (env->nr_cores * env->nr_threads) - 1;
1350 }
1351 break;
1352 case 0x8000000A:
296acb64
JR
1353 if (env->cpuid_ext3_features & CPUID_EXT3_SVM) {
1354 *eax = 0x00000001; /* SVM Revision */
1355 *ebx = 0x00000010; /* nr of ASIDs */
1356 *ecx = 0;
1357 *edx = env->cpuid_svm_features; /* optional features */
1358 } else {
1359 *eax = 0;
1360 *ebx = 0;
1361 *ecx = 0;
1362 *edx = 0;
1363 }
c6dc6f63 1364 break;
b3baa152
BW
1365 case 0xC0000000:
1366 *eax = env->cpuid_xlevel2;
1367 *ebx = 0;
1368 *ecx = 0;
1369 *edx = 0;
1370 break;
1371 case 0xC0000001:
1372 /* Support for VIA CPU's CPUID instruction */
1373 *eax = env->cpuid_version;
1374 *ebx = 0;
1375 *ecx = 0;
1376 *edx = env->cpuid_ext4_features;
1377 break;
1378 case 0xC0000002:
1379 case 0xC0000003:
1380 case 0xC0000004:
1381 /* Reserved for the future, and now filled with zero */
1382 *eax = 0;
1383 *ebx = 0;
1384 *ecx = 0;
1385 *edx = 0;
1386 break;
c6dc6f63
AP
1387 default:
1388 /* reserved values: zero */
1389 *eax = 0;
1390 *ebx = 0;
1391 *ecx = 0;
1392 *edx = 0;
1393 break;
1394 }
1395}
5fd2087a
AF
1396
1397/* CPUClass::reset() */
1398static void x86_cpu_reset(CPUState *s)
1399{
1400 X86CPU *cpu = X86_CPU(s);
1401 X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
1402 CPUX86State *env = &cpu->env;
c1958aea
AF
1403 int i;
1404
1405 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
1406 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
1407 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
1408 }
5fd2087a
AF
1409
1410 xcc->parent_reset(s);
1411
c1958aea
AF
1412
1413 memset(env, 0, offsetof(CPUX86State, breakpoints));
1414
1415 tlb_flush(env, 1);
1416
1417 env->old_exception = -1;
1418
1419 /* init to reset state */
1420
1421#ifdef CONFIG_SOFTMMU
1422 env->hflags |= HF_SOFTMMU_MASK;
1423#endif
1424 env->hflags2 |= HF2_GIF_MASK;
1425
1426 cpu_x86_update_cr0(env, 0x60000010);
1427 env->a20_mask = ~0x0;
1428 env->smbase = 0x30000;
1429
1430 env->idt.limit = 0xffff;
1431 env->gdt.limit = 0xffff;
1432 env->ldt.limit = 0xffff;
1433 env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
1434 env->tr.limit = 0xffff;
1435 env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
1436
1437 cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
1438 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK |
1439 DESC_R_MASK | DESC_A_MASK);
1440 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
1441 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1442 DESC_A_MASK);
1443 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
1444 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1445 DESC_A_MASK);
1446 cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
1447 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1448 DESC_A_MASK);
1449 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
1450 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1451 DESC_A_MASK);
1452 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
1453 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1454 DESC_A_MASK);
1455
1456 env->eip = 0xfff0;
1457 env->regs[R_EDX] = env->cpuid_version;
1458
1459 env->eflags = 0x2;
1460
1461 /* FPU init */
1462 for (i = 0; i < 8; i++) {
1463 env->fptags[i] = 1;
1464 }
1465 env->fpuc = 0x37f;
1466
1467 env->mxcsr = 0x1f80;
1468
1469 env->pat = 0x0007040600070406ULL;
1470 env->msr_ia32_misc_enable = MSR_IA32_MISC_ENABLE_DEFAULT;
1471
1472 memset(env->dr, 0, sizeof(env->dr));
1473 env->dr[6] = DR6_FIXED_1;
1474 env->dr[7] = DR7_FIXED_1;
1475 cpu_breakpoint_remove_all(env, BP_CPU);
1476 cpu_watchpoint_remove_all(env, BP_CPU);
5fd2087a
AF
1477}
1478
de024815
AF
1479static void mce_init(X86CPU *cpu)
1480{
1481 CPUX86State *cenv = &cpu->env;
1482 unsigned int bank;
1483
1484 if (((cenv->cpuid_version >> 8) & 0xf) >= 6
1485 && (cenv->cpuid_features & (CPUID_MCE | CPUID_MCA)) ==
1486 (CPUID_MCE | CPUID_MCA)) {
1487 cenv->mcg_cap = MCE_CAP_DEF | MCE_BANKS_DEF;
1488 cenv->mcg_ctl = ~(uint64_t)0;
1489 for (bank = 0; bank < MCE_BANKS_DEF; bank++) {
1490 cenv->mce_banks[bank * 4] = ~(uint64_t)0;
1491 }
1492 }
1493}
1494
1495static void x86_cpu_initfn(Object *obj)
1496{
1497 X86CPU *cpu = X86_CPU(obj);
1498 CPUX86State *env = &cpu->env;
1499
1500 cpu_exec_init(env);
71ad61d3
AF
1501
1502 object_property_add(obj, "family", "int",
1503 NULL,
1504 x86_cpuid_version_set_family, NULL, NULL, NULL);
1505
de024815
AF
1506 env->cpuid_apic_id = env->cpu_index;
1507 mce_init(cpu);
1508}
1509
5fd2087a
AF
1510static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
1511{
1512 X86CPUClass *xcc = X86_CPU_CLASS(oc);
1513 CPUClass *cc = CPU_CLASS(oc);
1514
1515 xcc->parent_reset = cc->reset;
1516 cc->reset = x86_cpu_reset;
1517}
1518
1519static const TypeInfo x86_cpu_type_info = {
1520 .name = TYPE_X86_CPU,
1521 .parent = TYPE_CPU,
1522 .instance_size = sizeof(X86CPU),
de024815 1523 .instance_init = x86_cpu_initfn,
5fd2087a
AF
1524 .abstract = false,
1525 .class_size = sizeof(X86CPUClass),
1526 .class_init = x86_cpu_common_class_init,
1527};
1528
1529static void x86_cpu_register_types(void)
1530{
1531 type_register_static(&x86_cpu_type_info);
1532}
1533
1534type_init(x86_cpu_register_types)