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