2 * i386 helpers (without register variable usage)
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "qemu-common.h"
32 #include "qemu-option.h"
33 #include "qemu-config.h"
35 /* feature flags taken from "Intel Processor Identification and the CPUID
36 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
37 * between feature naming conventions, aliases may be added.
39 static const char *feature_name
[] = {
40 "fpu", "vme", "de", "pse",
41 "tsc", "msr", "pae", "mce",
42 "cx8", "apic", NULL
, "sep",
43 "mtrr", "pge", "mca", "cmov",
44 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
45 NULL
, "ds" /* Intel dts */, "acpi", "mmx",
46 "fxsr", "sse", "sse2", "ss",
47 "ht" /* Intel htt */, "tm", "ia64", "pbe",
49 static const char *ext_feature_name
[] = {
50 "pni|sse3" /* Intel,AMD sse3 */, NULL
, NULL
, "monitor",
51 "ds_cpl", "vmx", NULL
/* Linux smx */, "est",
52 "tm2", "ssse3", "cid", NULL
,
53 NULL
, "cx16", "xtpr", NULL
,
54 NULL
, NULL
, "dca", "sse4.1|sse4_1",
55 "sse4.2|sse4_2", "x2apic", NULL
, "popcnt",
56 NULL
, NULL
, NULL
, NULL
,
57 NULL
, NULL
, NULL
, "hypervisor",
59 static const char *ext2_feature_name
[] = {
60 "fpu", "vme", "de", "pse",
61 "tsc", "msr", "pae", "mce",
62 "cx8" /* AMD CMPXCHG8B */, "apic", NULL
, "syscall",
63 "mtrr", "pge", "mca", "cmov",
64 "pat", "pse36", NULL
, NULL
/* Linux mp */,
65 "nx" /* Intel xd */, NULL
, "mmxext", "mmx",
66 "fxsr", "fxsr_opt" /* AMD ffxsr */, "pdpe1gb" /* AMD Page1GB */, "rdtscp",
67 NULL
, "lm" /* Intel 64 */, "3dnowext", "3dnow",
69 static const char *ext3_feature_name
[] = {
70 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
71 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
72 "3dnowprefetch", "osvw", NULL
/* Linux ibs */, NULL
,
73 "skinit", "wdt", NULL
, NULL
,
74 NULL
, NULL
, NULL
, NULL
,
75 NULL
, NULL
, NULL
, NULL
,
76 NULL
, NULL
, NULL
, NULL
,
77 NULL
, NULL
, NULL
, NULL
,
80 static const char *kvm_feature_name
[] = {
81 "kvmclock", "kvm_nopiodelay", "kvm_mmu", NULL
, NULL
, NULL
, NULL
, NULL
,
82 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
83 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
84 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
87 /* collects per-function cpuid data
89 typedef struct model_features_t
{
93 const char **flag_names
;
98 int enforce_cpuid
= 0;
100 static void host_cpuid(uint32_t function
, uint32_t count
, uint32_t *eax
,
101 uint32_t *ebx
, uint32_t *ecx
, uint32_t *edx
);
103 #define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
105 /* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
106 * a substring. ex if !NULL points to the first char after a substring,
107 * otherwise the string is assumed to sized by a terminating nul.
108 * Return lexical ordering of *s1:*s2.
110 static int sstrcmp(const char *s1
, const char *e1
, const char *s2
,
114 if (!*s1
|| !*s2
|| *s1
!= *s2
)
117 if (s1
== e1
&& s2
== e2
)
126 /* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
127 * '|' delimited (possibly empty) strings in which case search for a match
128 * within the alternatives proceeds left to right. Return 0 for success,
129 * non-zero otherwise.
131 static int altcmp(const char *s
, const char *e
, const char *altstr
)
135 for (q
= p
= altstr
; ; ) {
136 while (*p
&& *p
!= '|')
138 if ((q
== p
&& !*s
) || (q
!= p
&& !sstrcmp(s
, e
, q
, p
)))
147 /* search featureset for flag *[s..e), if found set corresponding bit in
148 * *pval and return success, otherwise return zero
150 static int lookup_feature(uint32_t *pval
, const char *s
, const char *e
,
151 const char **featureset
)
156 for (mask
= 1, ppc
= featureset
; mask
; mask
<<= 1, ++ppc
)
157 if (*ppc
&& !altcmp(s
, e
, *ppc
)) {
161 return (mask
? 1 : 0);
164 static void add_flagname_to_bitmaps(const char *flagname
, uint32_t *features
,
165 uint32_t *ext_features
,
166 uint32_t *ext2_features
,
167 uint32_t *ext3_features
,
168 uint32_t *kvm_features
)
170 if (!lookup_feature(features
, flagname
, NULL
, feature_name
) &&
171 !lookup_feature(ext_features
, flagname
, NULL
, ext_feature_name
) &&
172 !lookup_feature(ext2_features
, flagname
, NULL
, ext2_feature_name
) &&
173 !lookup_feature(ext3_features
, flagname
, NULL
, ext3_feature_name
) &&
174 !lookup_feature(kvm_features
, flagname
, NULL
, kvm_feature_name
))
175 fprintf(stderr
, "CPU feature %s not found\n", flagname
);
178 typedef struct x86_def_t
{
179 struct x86_def_t
*next
;
182 uint32_t vendor1
, vendor2
, vendor3
;
186 uint32_t features
, ext_features
, ext2_features
, ext3_features
, kvm_features
;
193 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
194 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
195 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
196 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
197 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
198 CPUID_PSE36 | CPUID_FXSR)
199 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
200 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
201 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
202 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
203 CPUID_PAE | CPUID_SEP | CPUID_APIC)
205 /* maintains list of cpu model definitions
207 static x86_def_t
*x86_defs
= {NULL
};
209 /* built-in cpu model definitions (deprecated)
211 static x86_def_t builtin_x86_defs
[] = {
216 .vendor1
= CPUID_VENDOR_AMD_1
,
217 .vendor2
= CPUID_VENDOR_AMD_2
,
218 .vendor3
= CPUID_VENDOR_AMD_3
,
222 .features
= PPRO_FEATURES
|
223 /* these features are needed for Win64 and aren't fully implemented */
224 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
225 /* this feature is needed for Solaris and isn't fully implemented */
227 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
| CPUID_EXT_POPCNT
,
228 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) |
229 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
230 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
231 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
232 .xlevel
= 0x8000000A,
233 .model_id
= "QEMU Virtual CPU version " QEMU_VERSION
,
238 .vendor1
= CPUID_VENDOR_AMD_1
,
239 .vendor2
= CPUID_VENDOR_AMD_2
,
240 .vendor3
= CPUID_VENDOR_AMD_3
,
244 /* Missing: CPUID_VME, CPUID_HT */
245 .features
= PPRO_FEATURES
|
246 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
248 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_CX16
|
250 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
251 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) |
252 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
253 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
| CPUID_EXT2_MMXEXT
|
255 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
257 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
258 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
259 .ext3_features
= CPUID_EXT3_LAHF_LM
| CPUID_EXT3_SVM
|
260 CPUID_EXT3_ABM
| CPUID_EXT3_SSE4A
,
261 .xlevel
= 0x8000001A,
262 .model_id
= "AMD Phenom(tm) 9550 Quad-Core Processor"
270 /* The original CPU also implements these features:
271 CPUID_VME, CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
272 CPUID_TM, CPUID_PBE */
273 .features
= PPRO_FEATURES
|
274 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
276 /* The original CPU also implements these ext features:
277 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
278 CPUID_EXT_TM2, CPUID_EXT_CX16, CPUID_EXT_XTPR, CPUID_EXT_PDCM */
279 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
,
280 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
281 .ext3_features
= CPUID_EXT3_LAHF_LM
,
282 .xlevel
= 0x80000008,
283 .model_id
= "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
288 .vendor1
= CPUID_VENDOR_INTEL_1
,
289 .vendor2
= CPUID_VENDOR_INTEL_2
,
290 .vendor3
= CPUID_VENDOR_INTEL_3
,
294 /* Missing: CPUID_VME, CPUID_HT */
295 .features
= PPRO_FEATURES
|
296 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
298 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
299 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_CX16
,
300 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
301 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) |
302 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
303 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
304 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
305 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
306 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
308 .xlevel
= 0x80000008,
309 .model_id
= "Common KVM processor"
318 .features
= PPRO_FEATURES
,
319 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_POPCNT
,
321 .model_id
= "QEMU Virtual CPU version " QEMU_VERSION
,
329 /* The original CPU also implements these features:
330 CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
331 CPUID_TM, CPUID_PBE */
332 .features
= PPRO_FEATURES
| CPUID_VME
|
333 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
,
334 /* The original CPU also implements these ext features:
335 CPUID_EXT_VMX, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_XTPR,
337 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
,
338 .ext2_features
= CPUID_EXT2_NX
,
339 .xlevel
= 0x80000008,
340 .model_id
= "Genuine Intel(R) CPU T2600 @ 2.16GHz",
348 .features
= I486_FEATURES
,
357 .features
= PENTIUM_FEATURES
,
366 .features
= PENTIUM2_FEATURES
,
375 .features
= PENTIUM3_FEATURES
,
381 .vendor1
= CPUID_VENDOR_AMD_1
,
382 .vendor2
= CPUID_VENDOR_AMD_2
,
383 .vendor3
= CPUID_VENDOR_AMD_3
,
387 .features
= PPRO_FEATURES
| CPUID_PSE36
| CPUID_VME
| CPUID_MTRR
| CPUID_MCA
,
388 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) | CPUID_EXT2_MMXEXT
| CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
389 .xlevel
= 0x80000008,
390 /* XXX: put another string ? */
391 .model_id
= "QEMU Virtual CPU version " QEMU_VERSION
,
395 /* original is on level 10 */
400 .features
= PPRO_FEATURES
|
401 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_VME
,
402 /* Missing: CPUID_DTS | CPUID_ACPI | CPUID_SS |
403 * CPUID_HT | CPUID_TM | CPUID_PBE */
404 /* Some CPUs got no CPUID_SEP */
405 .ext_features
= CPUID_EXT_MONITOR
|
406 CPUID_EXT_SSE3
/* PNI */ | CPUID_EXT_SSSE3
,
407 /* Missing: CPUID_EXT_DSCPL | CPUID_EXT_EST |
408 * CPUID_EXT_TM2 | CPUID_EXT_XTPR */
409 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) | CPUID_EXT2_NX
,
410 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
411 .xlevel
= 0x8000000A,
412 .model_id
= "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
416 static int cpu_x86_fill_model_id(char *str
)
418 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
421 for (i
= 0; i
< 3; i
++) {
422 host_cpuid(0x80000002 + i
, 0, &eax
, &ebx
, &ecx
, &edx
);
423 memcpy(str
+ i
* 16 + 0, &eax
, 4);
424 memcpy(str
+ i
* 16 + 4, &ebx
, 4);
425 memcpy(str
+ i
* 16 + 8, &ecx
, 4);
426 memcpy(str
+ i
* 16 + 12, &edx
, 4);
431 static int cpu_x86_fill_host(x86_def_t
*x86_cpu_def
)
433 uint32_t eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
435 x86_cpu_def
->name
= "host";
436 host_cpuid(0x0, 0, &eax
, &ebx
, &ecx
, &edx
);
437 x86_cpu_def
->level
= eax
;
438 x86_cpu_def
->vendor1
= ebx
;
439 x86_cpu_def
->vendor2
= edx
;
440 x86_cpu_def
->vendor3
= ecx
;
442 host_cpuid(0x1, 0, &eax
, &ebx
, &ecx
, &edx
);
443 x86_cpu_def
->family
= ((eax
>> 8) & 0x0F) + ((eax
>> 20) & 0xFF);
444 x86_cpu_def
->model
= ((eax
>> 4) & 0x0F) | ((eax
& 0xF0000) >> 12);
445 x86_cpu_def
->stepping
= eax
& 0x0F;
446 x86_cpu_def
->ext_features
= ecx
;
447 x86_cpu_def
->features
= edx
;
449 host_cpuid(0x80000000, 0, &eax
, &ebx
, &ecx
, &edx
);
450 x86_cpu_def
->xlevel
= eax
;
452 host_cpuid(0x80000001, 0, &eax
, &ebx
, &ecx
, &edx
);
453 x86_cpu_def
->ext2_features
= edx
;
454 x86_cpu_def
->ext3_features
= ecx
;
455 cpu_x86_fill_model_id(x86_cpu_def
->model_id
);
456 x86_cpu_def
->vendor_override
= 0;
461 static int unavailable_host_feature(struct model_features_t
*f
, uint32_t mask
)
465 for (i
= 0; i
< 32; ++i
)
467 fprintf(stderr
, "warning: host cpuid %04x_%04x lacks requested"
468 " flag '%s' [0x%08x]\n",
469 f
->cpuid
>> 16, f
->cpuid
& 0xffff,
470 f
->flag_names
[i
] ? f
->flag_names
[i
] : "[reserved]", mask
);
476 /* best effort attempt to inform user requested cpu flags aren't making
477 * their way to the guest. Note: ft[].check_feat ideally should be
478 * specified via a guest_def field to suppress report of extraneous flags.
480 static int check_features_against_host(x86_def_t
*guest_def
)
485 struct model_features_t ft
[] = {
486 {&guest_def
->features
, &host_def
.features
,
487 ~0, feature_name
, 0x00000000},
488 {&guest_def
->ext_features
, &host_def
.ext_features
,
489 ~CPUID_EXT_HYPERVISOR
, ext_feature_name
, 0x00000001},
490 {&guest_def
->ext2_features
, &host_def
.ext2_features
,
491 ~PPRO_FEATURES
, ext2_feature_name
, 0x80000000},
492 {&guest_def
->ext3_features
, &host_def
.ext3_features
,
493 ~CPUID_EXT3_SVM
, ext3_feature_name
, 0x80000001}};
495 cpu_x86_fill_host(&host_def
);
496 for (rv
= 0, i
= 0; i
< sizeof (ft
) / sizeof (ft
[0]); ++i
)
497 for (mask
= 1; mask
; mask
<<= 1)
498 if (ft
[i
].check_feat
& mask
&& *ft
[i
].guest_feat
& mask
&&
499 !(*ft
[i
].host_feat
& mask
)) {
500 unavailable_host_feature(&ft
[i
], mask
);
506 static int cpu_x86_find_by_name(x86_def_t
*x86_cpu_def
, const char *cpu_model
)
511 char *s
= strdup(cpu_model
);
512 char *featurestr
, *name
= strtok(s
, ",");
513 uint32_t plus_features
= 0, plus_ext_features
= 0, plus_ext2_features
= 0, plus_ext3_features
= 0, plus_kvm_features
= 0;
514 uint32_t minus_features
= 0, minus_ext_features
= 0, minus_ext2_features
= 0, minus_ext3_features
= 0, minus_kvm_features
= 0;
517 for (def
= x86_defs
; def
; def
= def
->next
)
518 if (!strcmp(name
, def
->name
))
520 if (kvm_enabled() && strcmp(name
, "host") == 0) {
521 cpu_x86_fill_host(x86_cpu_def
);
525 memcpy(x86_cpu_def
, def
, sizeof(*def
));
528 plus_kvm_features
= ~0; /* not supported bits will be filtered out later */
530 add_flagname_to_bitmaps("hypervisor", &plus_features
,
531 &plus_ext_features
, &plus_ext2_features
, &plus_ext3_features
,
534 featurestr
= strtok(NULL
, ",");
538 if (featurestr
[0] == '+') {
539 add_flagname_to_bitmaps(featurestr
+ 1, &plus_features
, &plus_ext_features
, &plus_ext2_features
, &plus_ext3_features
, &plus_kvm_features
);
540 } else if (featurestr
[0] == '-') {
541 add_flagname_to_bitmaps(featurestr
+ 1, &minus_features
, &minus_ext_features
, &minus_ext2_features
, &minus_ext3_features
, &minus_kvm_features
);
542 } else if ((val
= strchr(featurestr
, '='))) {
544 if (!strcmp(featurestr
, "family")) {
546 numvalue
= strtoul(val
, &err
, 0);
548 fprintf(stderr
, "bad numerical value %s\n", val
);
551 x86_cpu_def
->family
= numvalue
;
552 } else if (!strcmp(featurestr
, "model")) {
554 numvalue
= strtoul(val
, &err
, 0);
555 if (!*val
|| *err
|| numvalue
> 0xff) {
556 fprintf(stderr
, "bad numerical value %s\n", val
);
559 x86_cpu_def
->model
= numvalue
;
560 } else if (!strcmp(featurestr
, "stepping")) {
562 numvalue
= strtoul(val
, &err
, 0);
563 if (!*val
|| *err
|| numvalue
> 0xf) {
564 fprintf(stderr
, "bad numerical value %s\n", val
);
567 x86_cpu_def
->stepping
= numvalue
;
568 } else if (!strcmp(featurestr
, "level")) {
570 numvalue
= strtoul(val
, &err
, 0);
572 fprintf(stderr
, "bad numerical value %s\n", val
);
575 x86_cpu_def
->level
= numvalue
;
576 } else if (!strcmp(featurestr
, "xlevel")) {
578 numvalue
= strtoul(val
, &err
, 0);
580 fprintf(stderr
, "bad numerical value %s\n", val
);
583 if (numvalue
< 0x80000000) {
584 numvalue
+= 0x80000000;
586 x86_cpu_def
->xlevel
= numvalue
;
587 } else if (!strcmp(featurestr
, "vendor")) {
588 if (strlen(val
) != 12) {
589 fprintf(stderr
, "vendor string must be 12 chars long\n");
592 x86_cpu_def
->vendor1
= 0;
593 x86_cpu_def
->vendor2
= 0;
594 x86_cpu_def
->vendor3
= 0;
595 for(i
= 0; i
< 4; i
++) {
596 x86_cpu_def
->vendor1
|= ((uint8_t)val
[i
]) << (8 * i
);
597 x86_cpu_def
->vendor2
|= ((uint8_t)val
[i
+ 4]) << (8 * i
);
598 x86_cpu_def
->vendor3
|= ((uint8_t)val
[i
+ 8]) << (8 * i
);
600 x86_cpu_def
->vendor_override
= 1;
601 } else if (!strcmp(featurestr
, "model_id")) {
602 pstrcpy(x86_cpu_def
->model_id
, sizeof(x86_cpu_def
->model_id
),
605 fprintf(stderr
, "unrecognized feature %s\n", featurestr
);
608 } else if (!strcmp(featurestr
, "check")) {
610 } else if (!strcmp(featurestr
, "enforce")) {
611 check_cpuid
= enforce_cpuid
= 1;
613 fprintf(stderr
, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr
);
616 featurestr
= strtok(NULL
, ",");
618 x86_cpu_def
->features
|= plus_features
;
619 x86_cpu_def
->ext_features
|= plus_ext_features
;
620 x86_cpu_def
->ext2_features
|= plus_ext2_features
;
621 x86_cpu_def
->ext3_features
|= plus_ext3_features
;
622 x86_cpu_def
->kvm_features
|= plus_kvm_features
;
623 x86_cpu_def
->features
&= ~minus_features
;
624 x86_cpu_def
->ext_features
&= ~minus_ext_features
;
625 x86_cpu_def
->ext2_features
&= ~minus_ext2_features
;
626 x86_cpu_def
->ext3_features
&= ~minus_ext3_features
;
627 x86_cpu_def
->kvm_features
&= ~minus_kvm_features
;
629 if (check_features_against_host(x86_cpu_def
) && enforce_cpuid
)
640 /* generate a composite string into buf of all cpuid names in featureset
641 * selected by fbits. indicate truncation at bufsize in the event of overflow.
642 * if flags, suppress names undefined in featureset.
644 static void listflags(char *buf
, int bufsize
, uint32_t fbits
,
645 const char **featureset
, uint32_t flags
)
647 const char **p
= &featureset
[31];
651 b
= 4 <= bufsize
? buf
+ (bufsize
-= 3) - 1 : NULL
;
653 for (q
= buf
, bit
= 31; fbits
&& bufsize
; --p
, fbits
&= ~(1 << bit
), --bit
)
654 if (fbits
& 1 << bit
&& (*p
|| !flags
)) {
656 nc
= snprintf(q
, bufsize
, "%s%s", q
== buf
? "" : " ", *p
);
658 nc
= snprintf(q
, bufsize
, "%s[%d]", q
== buf
? "" : " ", bit
);
669 /* generate CPU information:
670 * -? list model names
671 * -?model list model names/IDs
672 * -?dump output all model (x86_def_t) data
673 * -?cpuid list all recognized cpuid flag names
675 void x86_cpu_list (FILE *f
, int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
678 unsigned char model
= !strcmp("?model", optarg
);
679 unsigned char dump
= !strcmp("?dump", optarg
);
680 unsigned char cpuid
= !strcmp("?cpuid", optarg
);
685 (*cpu_fprintf
)(f
, "Recognized CPUID flags:\n");
686 listflags(buf
, sizeof (buf
), (uint32_t)~0, feature_name
, 1);
687 (*cpu_fprintf
)(f
, " f_edx: %s\n", buf
);
688 listflags(buf
, sizeof (buf
), (uint32_t)~0, ext_feature_name
, 1);
689 (*cpu_fprintf
)(f
, " f_ecx: %s\n", buf
);
690 listflags(buf
, sizeof (buf
), (uint32_t)~0, ext2_feature_name
, 1);
691 (*cpu_fprintf
)(f
, " extf_edx: %s\n", buf
);
692 listflags(buf
, sizeof (buf
), (uint32_t)~0, ext3_feature_name
, 1);
693 (*cpu_fprintf
)(f
, " extf_ecx: %s\n", buf
);
696 for (def
= x86_defs
; def
; def
= def
->next
) {
697 snprintf(buf
, sizeof (buf
), def
->flags
? "[%s]": "%s", def
->name
);
699 (*cpu_fprintf
)(f
, "x86 %16s %-48s\n", buf
, def
->model_id
);
701 (*cpu_fprintf
)(f
, "x86 %16s\n", buf
);
704 memcpy(buf
, &def
->vendor1
, sizeof (def
->vendor1
));
705 memcpy(buf
+ 4, &def
->vendor2
, sizeof (def
->vendor2
));
706 memcpy(buf
+ 8, &def
->vendor3
, sizeof (def
->vendor3
));
709 " family %d model %d stepping %d level %d xlevel 0x%x"
711 def
->family
, def
->model
, def
->stepping
, def
->level
,
713 listflags(buf
, sizeof (buf
), def
->features
, feature_name
, 0);
714 (*cpu_fprintf
)(f
, " feature_edx %08x (%s)\n", def
->features
,
716 listflags(buf
, sizeof (buf
), def
->ext_features
, ext_feature_name
,
718 (*cpu_fprintf
)(f
, " feature_ecx %08x (%s)\n", def
->ext_features
,
720 listflags(buf
, sizeof (buf
), def
->ext2_features
, ext2_feature_name
,
722 (*cpu_fprintf
)(f
, " extfeature_edx %08x (%s)\n",
723 def
->ext2_features
, buf
);
724 listflags(buf
, sizeof (buf
), def
->ext3_features
, ext3_feature_name
,
726 (*cpu_fprintf
)(f
, " extfeature_ecx %08x (%s)\n",
727 def
->ext3_features
, buf
);
728 (*cpu_fprintf
)(f
, "\n");
733 static int cpu_x86_register (CPUX86State
*env
, const char *cpu_model
)
735 x86_def_t def1
, *def
= &def1
;
737 if (cpu_x86_find_by_name(def
, cpu_model
) < 0)
740 env
->cpuid_vendor1
= def
->vendor1
;
741 env
->cpuid_vendor2
= def
->vendor2
;
742 env
->cpuid_vendor3
= def
->vendor3
;
744 env
->cpuid_vendor1
= CPUID_VENDOR_INTEL_1
;
745 env
->cpuid_vendor2
= CPUID_VENDOR_INTEL_2
;
746 env
->cpuid_vendor3
= CPUID_VENDOR_INTEL_3
;
748 env
->cpuid_vendor_override
= def
->vendor_override
;
749 env
->cpuid_level
= def
->level
;
750 if (def
->family
> 0x0f)
751 env
->cpuid_version
= 0xf00 | ((def
->family
- 0x0f) << 20);
753 env
->cpuid_version
= def
->family
<< 8;
754 env
->cpuid_version
|= ((def
->model
& 0xf) << 4) | ((def
->model
>> 4) << 16);
755 env
->cpuid_version
|= def
->stepping
;
756 env
->cpuid_features
= def
->features
;
757 env
->pat
= 0x0007040600070406ULL
;
758 env
->cpuid_ext_features
= def
->ext_features
;
759 env
->cpuid_ext2_features
= def
->ext2_features
;
760 env
->cpuid_xlevel
= def
->xlevel
;
761 env
->cpuid_kvm_features
= def
->kvm_features
;
763 const char *model_id
= def
->model_id
;
767 len
= strlen(model_id
);
768 for(i
= 0; i
< 48; i
++) {
772 c
= (uint8_t)model_id
[i
];
773 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
779 #if !defined(CONFIG_LINUX_USER)
780 /* copy vendor id string to 32 bit register, nul pad as needed
782 static void cpyid(const char *s
, uint32_t *id
)
784 char *d
= (char *)id
;
787 for (i
= sizeof (*id
); i
--; )
788 *d
++ = *s
? *s
++ : '\0';
791 /* interpret radix and convert from string to arbitrary scalar,
792 * otherwise flag failure
794 #define setscalar(pval, str, perr) \
799 ul = strtoul(str, &pend, 0); \
800 *str && !*pend ? (*pval = ul) : (*perr = 1); \
803 /* map cpuid options to feature bits, otherwise return failure
804 * (option tags in *str are delimited by whitespace)
806 static void setfeatures(uint32_t *pval
, const char *str
,
807 const char **featureset
, int *perr
)
811 for (q
= p
= str
; *p
|| *q
; q
= p
) {
814 while (*p
&& !iswhite(*p
))
818 if (!lookup_feature(pval
, q
, p
, featureset
)) {
819 fprintf(stderr
, "error: feature \"%.*s\" not available in set\n",
827 /* map config file options to x86_def_t form
829 static int cpudef_setfield(const char *name
, const char *str
, void *opaque
)
831 x86_def_t
*def
= opaque
;
834 if (!strcmp(name
, "name")) {
835 def
->name
= strdup(str
);
836 } else if (!strcmp(name
, "model_id")) {
837 strncpy(def
->model_id
, str
, sizeof (def
->model_id
));
838 } else if (!strcmp(name
, "level")) {
839 setscalar(&def
->level
, str
, &err
)
840 } else if (!strcmp(name
, "vendor")) {
841 cpyid(&str
[0], &def
->vendor1
);
842 cpyid(&str
[4], &def
->vendor2
);
843 cpyid(&str
[8], &def
->vendor3
);
844 } else if (!strcmp(name
, "family")) {
845 setscalar(&def
->family
, str
, &err
)
846 } else if (!strcmp(name
, "model")) {
847 setscalar(&def
->model
, str
, &err
)
848 } else if (!strcmp(name
, "stepping")) {
849 setscalar(&def
->stepping
, str
, &err
)
850 } else if (!strcmp(name
, "feature_edx")) {
851 setfeatures(&def
->features
, str
, feature_name
, &err
);
852 } else if (!strcmp(name
, "feature_ecx")) {
853 setfeatures(&def
->ext_features
, str
, ext_feature_name
, &err
);
854 } else if (!strcmp(name
, "extfeature_edx")) {
855 setfeatures(&def
->ext2_features
, str
, ext2_feature_name
, &err
);
856 } else if (!strcmp(name
, "extfeature_ecx")) {
857 setfeatures(&def
->ext3_features
, str
, ext3_feature_name
, &err
);
858 } else if (!strcmp(name
, "xlevel")) {
859 setscalar(&def
->xlevel
, str
, &err
)
861 fprintf(stderr
, "error: unknown option [%s = %s]\n", name
, str
);
865 fprintf(stderr
, "error: bad option value [%s = %s]\n", name
, str
);
871 /* register config file entry as x86_def_t
873 static int cpudef_register(QemuOpts
*opts
, void *opaque
)
875 x86_def_t
*def
= qemu_mallocz(sizeof (x86_def_t
));
877 qemu_opt_foreach(opts
, cpudef_setfield
, def
, 1);
878 def
->next
= x86_defs
;
882 #endif /* !CONFIG_LINUX_USER */
884 /* register "cpudef" models defined in configuration file. Here we first
885 * preload any built-in definitions
887 void x86_cpudef_setup(void)
891 for (i
= 0; i
< ARRAY_SIZE(builtin_x86_defs
); ++i
) {
892 builtin_x86_defs
[i
].next
= x86_defs
;
893 builtin_x86_defs
[i
].flags
= 1;
894 x86_defs
= &builtin_x86_defs
[i
];
896 #if !defined(CONFIG_LINUX_USER)
897 qemu_opts_foreach(&qemu_cpudef_opts
, cpudef_register
, NULL
, 0);
901 /* NOTE: must be called outside the CPU execute loop */
902 void cpu_reset(CPUX86State
*env
)
906 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
907 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
908 log_cpu_state(env
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
911 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
915 env
->old_exception
= -1;
917 /* init to reset state */
919 #ifdef CONFIG_SOFTMMU
920 env
->hflags
|= HF_SOFTMMU_MASK
;
922 env
->hflags2
|= HF2_GIF_MASK
;
924 cpu_x86_update_cr0(env
, 0x60000010);
925 env
->a20_mask
= ~0x0;
926 env
->smbase
= 0x30000;
928 env
->idt
.limit
= 0xffff;
929 env
->gdt
.limit
= 0xffff;
930 env
->ldt
.limit
= 0xffff;
931 env
->ldt
.flags
= DESC_P_MASK
| (2 << DESC_TYPE_SHIFT
);
932 env
->tr
.limit
= 0xffff;
933 env
->tr
.flags
= DESC_P_MASK
| (11 << DESC_TYPE_SHIFT
);
935 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff,
936 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
|
937 DESC_R_MASK
| DESC_A_MASK
);
938 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff,
939 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
941 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff,
942 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
944 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff,
945 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
947 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff,
948 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
950 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff,
951 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
955 env
->regs
[R_EDX
] = env
->cpuid_version
;
960 for(i
= 0;i
< 8; i
++)
966 memset(env
->dr
, 0, sizeof(env
->dr
));
967 env
->dr
[6] = DR6_FIXED_1
;
968 env
->dr
[7] = DR7_FIXED_1
;
969 cpu_breakpoint_remove_all(env
, BP_CPU
);
970 cpu_watchpoint_remove_all(env
, BP_CPU
);
975 void cpu_x86_close(CPUX86State
*env
)
980 /***********************************************************/
983 static const char *cc_op_str
[] = {
1039 cpu_x86_dump_seg_cache(CPUState
*env
, FILE *f
,
1040 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
1041 const char *name
, struct SegmentCache
*sc
)
1043 #ifdef TARGET_X86_64
1044 if (env
->hflags
& HF_CS64_MASK
) {
1045 cpu_fprintf(f
, "%-3s=%04x %016" PRIx64
" %08x %08x", name
,
1046 sc
->selector
, sc
->base
, sc
->limit
, sc
->flags
);
1050 cpu_fprintf(f
, "%-3s=%04x %08x %08x %08x", name
, sc
->selector
,
1051 (uint32_t)sc
->base
, sc
->limit
, sc
->flags
);
1054 if (!(env
->hflags
& HF_PE_MASK
) || !(sc
->flags
& DESC_P_MASK
))
1057 cpu_fprintf(f
, " DPL=%d ", (sc
->flags
& DESC_DPL_MASK
) >> DESC_DPL_SHIFT
);
1058 if (sc
->flags
& DESC_S_MASK
) {
1059 if (sc
->flags
& DESC_CS_MASK
) {
1060 cpu_fprintf(f
, (sc
->flags
& DESC_L_MASK
) ? "CS64" :
1061 ((sc
->flags
& DESC_B_MASK
) ? "CS32" : "CS16"));
1062 cpu_fprintf(f
, " [%c%c", (sc
->flags
& DESC_C_MASK
) ? 'C' : '-',
1063 (sc
->flags
& DESC_R_MASK
) ? 'R' : '-');
1065 cpu_fprintf(f
, (sc
->flags
& DESC_B_MASK
) ? "DS " : "DS16");
1066 cpu_fprintf(f
, " [%c%c", (sc
->flags
& DESC_E_MASK
) ? 'E' : '-',
1067 (sc
->flags
& DESC_W_MASK
) ? 'W' : '-');
1069 cpu_fprintf(f
, "%c]", (sc
->flags
& DESC_A_MASK
) ? 'A' : '-');
1071 static const char *sys_type_name
[2][16] = {
1073 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
1074 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
1075 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
1076 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
1079 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
1080 "Reserved", "Reserved", "Reserved", "Reserved",
1081 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
1082 "Reserved", "IntGate64", "TrapGate64"
1085 cpu_fprintf(f
, sys_type_name
[(env
->hflags
& HF_LMA_MASK
) ? 1 : 0]
1086 [(sc
->flags
& DESC_TYPE_MASK
)
1087 >> DESC_TYPE_SHIFT
]);
1090 cpu_fprintf(f
, "\n");
1093 void cpu_dump_state(CPUState
*env
, FILE *f
,
1094 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
1098 char cc_op_name
[32];
1099 static const char *seg_name
[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
1101 cpu_synchronize_state(env
);
1103 eflags
= env
->eflags
;
1104 #ifdef TARGET_X86_64
1105 if (env
->hflags
& HF_CS64_MASK
) {
1107 "RAX=%016" PRIx64
" RBX=%016" PRIx64
" RCX=%016" PRIx64
" RDX=%016" PRIx64
"\n"
1108 "RSI=%016" PRIx64
" RDI=%016" PRIx64
" RBP=%016" PRIx64
" RSP=%016" PRIx64
"\n"
1109 "R8 =%016" PRIx64
" R9 =%016" PRIx64
" R10=%016" PRIx64
" R11=%016" PRIx64
"\n"
1110 "R12=%016" PRIx64
" R13=%016" PRIx64
" R14=%016" PRIx64
" R15=%016" PRIx64
"\n"
1111 "RIP=%016" PRIx64
" RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
1129 eflags
& DF_MASK
? 'D' : '-',
1130 eflags
& CC_O
? 'O' : '-',
1131 eflags
& CC_S
? 'S' : '-',
1132 eflags
& CC_Z
? 'Z' : '-',
1133 eflags
& CC_A
? 'A' : '-',
1134 eflags
& CC_P
? 'P' : '-',
1135 eflags
& CC_C
? 'C' : '-',
1136 env
->hflags
& HF_CPL_MASK
,
1137 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
1138 (env
->a20_mask
>> 20) & 1,
1139 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
1144 cpu_fprintf(f
, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
1145 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
1146 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
1147 (uint32_t)env
->regs
[R_EAX
],
1148 (uint32_t)env
->regs
[R_EBX
],
1149 (uint32_t)env
->regs
[R_ECX
],
1150 (uint32_t)env
->regs
[R_EDX
],
1151 (uint32_t)env
->regs
[R_ESI
],
1152 (uint32_t)env
->regs
[R_EDI
],
1153 (uint32_t)env
->regs
[R_EBP
],
1154 (uint32_t)env
->regs
[R_ESP
],
1155 (uint32_t)env
->eip
, eflags
,
1156 eflags
& DF_MASK
? 'D' : '-',
1157 eflags
& CC_O
? 'O' : '-',
1158 eflags
& CC_S
? 'S' : '-',
1159 eflags
& CC_Z
? 'Z' : '-',
1160 eflags
& CC_A
? 'A' : '-',
1161 eflags
& CC_P
? 'P' : '-',
1162 eflags
& CC_C
? 'C' : '-',
1163 env
->hflags
& HF_CPL_MASK
,
1164 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
1165 (env
->a20_mask
>> 20) & 1,
1166 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
1170 for(i
= 0; i
< 6; i
++) {
1171 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, seg_name
[i
],
1174 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, "LDT", &env
->ldt
);
1175 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, "TR", &env
->tr
);
1177 #ifdef TARGET_X86_64
1178 if (env
->hflags
& HF_LMA_MASK
) {
1179 cpu_fprintf(f
, "GDT= %016" PRIx64
" %08x\n",
1180 env
->gdt
.base
, env
->gdt
.limit
);
1181 cpu_fprintf(f
, "IDT= %016" PRIx64
" %08x\n",
1182 env
->idt
.base
, env
->idt
.limit
);
1183 cpu_fprintf(f
, "CR0=%08x CR2=%016" PRIx64
" CR3=%016" PRIx64
" CR4=%08x\n",
1184 (uint32_t)env
->cr
[0],
1187 (uint32_t)env
->cr
[4]);
1188 for(i
= 0; i
< 4; i
++)
1189 cpu_fprintf(f
, "DR%d=%016" PRIx64
" ", i
, env
->dr
[i
]);
1190 cpu_fprintf(f
, "\nDR6=%016" PRIx64
" DR7=%016" PRIx64
"\n",
1191 env
->dr
[6], env
->dr
[7]);
1195 cpu_fprintf(f
, "GDT= %08x %08x\n",
1196 (uint32_t)env
->gdt
.base
, env
->gdt
.limit
);
1197 cpu_fprintf(f
, "IDT= %08x %08x\n",
1198 (uint32_t)env
->idt
.base
, env
->idt
.limit
);
1199 cpu_fprintf(f
, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
1200 (uint32_t)env
->cr
[0],
1201 (uint32_t)env
->cr
[2],
1202 (uint32_t)env
->cr
[3],
1203 (uint32_t)env
->cr
[4]);
1204 for(i
= 0; i
< 4; i
++)
1205 cpu_fprintf(f
, "DR%d=%08x ", i
, env
->dr
[i
]);
1206 cpu_fprintf(f
, "\nDR6=%08x DR7=%08x\n", env
->dr
[6], env
->dr
[7]);
1208 if (flags
& X86_DUMP_CCOP
) {
1209 if ((unsigned)env
->cc_op
< CC_OP_NB
)
1210 snprintf(cc_op_name
, sizeof(cc_op_name
), "%s", cc_op_str
[env
->cc_op
]);
1212 snprintf(cc_op_name
, sizeof(cc_op_name
), "[%d]", env
->cc_op
);
1213 #ifdef TARGET_X86_64
1214 if (env
->hflags
& HF_CS64_MASK
) {
1215 cpu_fprintf(f
, "CCS=%016" PRIx64
" CCD=%016" PRIx64
" CCO=%-8s\n",
1216 env
->cc_src
, env
->cc_dst
,
1221 cpu_fprintf(f
, "CCS=%08x CCD=%08x CCO=%-8s\n",
1222 (uint32_t)env
->cc_src
, (uint32_t)env
->cc_dst
,
1226 if (flags
& X86_DUMP_FPU
) {
1229 for(i
= 0; i
< 8; i
++) {
1230 fptag
|= ((!env
->fptags
[i
]) << i
);
1232 cpu_fprintf(f
, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
1234 (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11,
1239 #if defined(USE_X86LDOUBLE)
1247 tmp
.d
= env
->fpregs
[i
].d
;
1248 cpu_fprintf(f
, "FPR%d=%016" PRIx64
" %04x",
1249 i
, tmp
.l
.lower
, tmp
.l
.upper
);
1251 cpu_fprintf(f
, "FPR%d=%016" PRIx64
,
1252 i
, env
->fpregs
[i
].mmx
.q
);
1255 cpu_fprintf(f
, "\n");
1257 cpu_fprintf(f
, " ");
1259 if (env
->hflags
& HF_CS64_MASK
)
1264 cpu_fprintf(f
, "XMM%02d=%08x%08x%08x%08x",
1266 env
->xmm_regs
[i
].XMM_L(3),
1267 env
->xmm_regs
[i
].XMM_L(2),
1268 env
->xmm_regs
[i
].XMM_L(1),
1269 env
->xmm_regs
[i
].XMM_L(0));
1271 cpu_fprintf(f
, "\n");
1273 cpu_fprintf(f
, " ");
1278 /***********************************************************/
1280 /* XXX: add PGE support */
1282 void cpu_x86_set_a20(CPUX86State
*env
, int a20_state
)
1284 a20_state
= (a20_state
!= 0);
1285 if (a20_state
!= ((env
->a20_mask
>> 20) & 1)) {
1286 #if defined(DEBUG_MMU)
1287 printf("A20 update: a20=%d\n", a20_state
);
1289 /* if the cpu is currently executing code, we must unlink it and
1290 all the potentially executing TB */
1291 cpu_interrupt(env
, CPU_INTERRUPT_EXITTB
);
1293 /* when a20 is changed, all the MMU mappings are invalid, so
1294 we must flush everything */
1296 env
->a20_mask
= ~(1 << 20) | (a20_state
<< 20);
1300 void cpu_x86_update_cr0(CPUX86State
*env
, uint32_t new_cr0
)
1304 #if defined(DEBUG_MMU)
1305 printf("CR0 update: CR0=0x%08x\n", new_cr0
);
1307 if ((new_cr0
& (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
)) !=
1308 (env
->cr
[0] & (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
))) {
1312 #ifdef TARGET_X86_64
1313 if (!(env
->cr
[0] & CR0_PG_MASK
) && (new_cr0
& CR0_PG_MASK
) &&
1314 (env
->efer
& MSR_EFER_LME
)) {
1315 /* enter in long mode */
1316 /* XXX: generate an exception */
1317 if (!(env
->cr
[4] & CR4_PAE_MASK
))
1319 env
->efer
|= MSR_EFER_LMA
;
1320 env
->hflags
|= HF_LMA_MASK
;
1321 } else if ((env
->cr
[0] & CR0_PG_MASK
) && !(new_cr0
& CR0_PG_MASK
) &&
1322 (env
->efer
& MSR_EFER_LMA
)) {
1323 /* exit long mode */
1324 env
->efer
&= ~MSR_EFER_LMA
;
1325 env
->hflags
&= ~(HF_LMA_MASK
| HF_CS64_MASK
);
1326 env
->eip
&= 0xffffffff;
1329 env
->cr
[0] = new_cr0
| CR0_ET_MASK
;
1331 /* update PE flag in hidden flags */
1332 pe_state
= (env
->cr
[0] & CR0_PE_MASK
);
1333 env
->hflags
= (env
->hflags
& ~HF_PE_MASK
) | (pe_state
<< HF_PE_SHIFT
);
1334 /* ensure that ADDSEG is always set in real mode */
1335 env
->hflags
|= ((pe_state
^ 1) << HF_ADDSEG_SHIFT
);
1336 /* update FPU flags */
1337 env
->hflags
= (env
->hflags
& ~(HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
)) |
1338 ((new_cr0
<< (HF_MP_SHIFT
- 1)) & (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
));
1341 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
1343 void cpu_x86_update_cr3(CPUX86State
*env
, target_ulong new_cr3
)
1345 env
->cr
[3] = new_cr3
;
1346 if (env
->cr
[0] & CR0_PG_MASK
) {
1347 #if defined(DEBUG_MMU)
1348 printf("CR3 update: CR3=" TARGET_FMT_lx
"\n", new_cr3
);
1354 void cpu_x86_update_cr4(CPUX86State
*env
, uint32_t new_cr4
)
1356 #if defined(DEBUG_MMU)
1357 printf("CR4 update: CR4=%08x\n", (uint32_t)env
->cr
[4]);
1359 if ((new_cr4
& (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
)) !=
1360 (env
->cr
[4] & (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
))) {
1364 if (!(env
->cpuid_features
& CPUID_SSE
))
1365 new_cr4
&= ~CR4_OSFXSR_MASK
;
1366 if (new_cr4
& CR4_OSFXSR_MASK
)
1367 env
->hflags
|= HF_OSFXSR_MASK
;
1369 env
->hflags
&= ~HF_OSFXSR_MASK
;
1371 env
->cr
[4] = new_cr4
;
1374 #if defined(CONFIG_USER_ONLY)
1376 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
1377 int is_write
, int mmu_idx
, int is_softmmu
)
1379 /* user mode only emulation */
1382 env
->error_code
= (is_write
<< PG_ERROR_W_BIT
);
1383 env
->error_code
|= PG_ERROR_U_MASK
;
1384 env
->exception_index
= EXCP0E_PAGE
;
1388 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
1395 /* XXX: This value should match the one returned by CPUID
1397 # if defined(TARGET_X86_64)
1398 # define PHYS_ADDR_MASK 0xfffffff000LL
1400 # define PHYS_ADDR_MASK 0xffffff000LL
1404 -1 = cannot handle fault
1405 0 = nothing more to do
1406 1 = generate PF fault
1407 2 = soft MMU activation required for this block
1409 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
1410 int is_write1
, int mmu_idx
, int is_softmmu
)
1413 target_ulong pde_addr
, pte_addr
;
1414 int error_code
, is_dirty
, prot
, page_size
, ret
, is_write
, is_user
;
1415 target_phys_addr_t paddr
;
1416 uint32_t page_offset
;
1417 target_ulong vaddr
, virt_addr
;
1419 is_user
= mmu_idx
== MMU_USER_IDX
;
1420 #if defined(DEBUG_MMU)
1421 printf("MMU fault: addr=" TARGET_FMT_lx
" w=%d u=%d eip=" TARGET_FMT_lx
"\n",
1422 addr
, is_write1
, is_user
, env
->eip
);
1424 is_write
= is_write1
& 1;
1426 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1428 virt_addr
= addr
& TARGET_PAGE_MASK
;
1429 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1434 if (env
->cr
[4] & CR4_PAE_MASK
) {
1436 target_ulong pdpe_addr
;
1438 #ifdef TARGET_X86_64
1439 if (env
->hflags
& HF_LMA_MASK
) {
1440 uint64_t pml4e_addr
, pml4e
;
1443 /* test virtual address sign extension */
1444 sext
= (int64_t)addr
>> 47;
1445 if (sext
!= 0 && sext
!= -1) {
1446 env
->error_code
= 0;
1447 env
->exception_index
= EXCP0D_GPF
;
1451 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
1453 pml4e
= ldq_phys(pml4e_addr
);
1454 if (!(pml4e
& PG_PRESENT_MASK
)) {
1458 if (!(env
->efer
& MSR_EFER_NXE
) && (pml4e
& PG_NX_MASK
)) {
1459 error_code
= PG_ERROR_RSVD_MASK
;
1462 if (!(pml4e
& PG_ACCESSED_MASK
)) {
1463 pml4e
|= PG_ACCESSED_MASK
;
1464 stl_phys_notdirty(pml4e_addr
, pml4e
);
1466 ptep
= pml4e
^ PG_NX_MASK
;
1467 pdpe_addr
= ((pml4e
& PHYS_ADDR_MASK
) + (((addr
>> 30) & 0x1ff) << 3)) &
1469 pdpe
= ldq_phys(pdpe_addr
);
1470 if (!(pdpe
& PG_PRESENT_MASK
)) {
1474 if (!(env
->efer
& MSR_EFER_NXE
) && (pdpe
& PG_NX_MASK
)) {
1475 error_code
= PG_ERROR_RSVD_MASK
;
1478 ptep
&= pdpe
^ PG_NX_MASK
;
1479 if (!(pdpe
& PG_ACCESSED_MASK
)) {
1480 pdpe
|= PG_ACCESSED_MASK
;
1481 stl_phys_notdirty(pdpe_addr
, pdpe
);
1486 /* XXX: load them when cr3 is loaded ? */
1487 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
1489 pdpe
= ldq_phys(pdpe_addr
);
1490 if (!(pdpe
& PG_PRESENT_MASK
)) {
1494 ptep
= PG_NX_MASK
| PG_USER_MASK
| PG_RW_MASK
;
1497 pde_addr
= ((pdpe
& PHYS_ADDR_MASK
) + (((addr
>> 21) & 0x1ff) << 3)) &
1499 pde
= ldq_phys(pde_addr
);
1500 if (!(pde
& PG_PRESENT_MASK
)) {
1504 if (!(env
->efer
& MSR_EFER_NXE
) && (pde
& PG_NX_MASK
)) {
1505 error_code
= PG_ERROR_RSVD_MASK
;
1508 ptep
&= pde
^ PG_NX_MASK
;
1509 if (pde
& PG_PSE_MASK
) {
1511 page_size
= 2048 * 1024;
1513 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
1514 goto do_fault_protect
;
1516 if (!(ptep
& PG_USER_MASK
))
1517 goto do_fault_protect
;
1518 if (is_write
&& !(ptep
& PG_RW_MASK
))
1519 goto do_fault_protect
;
1521 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1522 is_write
&& !(ptep
& PG_RW_MASK
))
1523 goto do_fault_protect
;
1525 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
1526 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
1527 pde
|= PG_ACCESSED_MASK
;
1529 pde
|= PG_DIRTY_MASK
;
1530 stl_phys_notdirty(pde_addr
, pde
);
1532 /* align to page_size */
1533 pte
= pde
& ((PHYS_ADDR_MASK
& ~(page_size
- 1)) | 0xfff);
1534 virt_addr
= addr
& ~(page_size
- 1);
1537 if (!(pde
& PG_ACCESSED_MASK
)) {
1538 pde
|= PG_ACCESSED_MASK
;
1539 stl_phys_notdirty(pde_addr
, pde
);
1541 pte_addr
= ((pde
& PHYS_ADDR_MASK
) + (((addr
>> 12) & 0x1ff) << 3)) &
1543 pte
= ldq_phys(pte_addr
);
1544 if (!(pte
& PG_PRESENT_MASK
)) {
1548 if (!(env
->efer
& MSR_EFER_NXE
) && (pte
& PG_NX_MASK
)) {
1549 error_code
= PG_ERROR_RSVD_MASK
;
1552 /* combine pde and pte nx, user and rw protections */
1553 ptep
&= pte
^ PG_NX_MASK
;
1555 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
1556 goto do_fault_protect
;
1558 if (!(ptep
& PG_USER_MASK
))
1559 goto do_fault_protect
;
1560 if (is_write
&& !(ptep
& PG_RW_MASK
))
1561 goto do_fault_protect
;
1563 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1564 is_write
&& !(ptep
& PG_RW_MASK
))
1565 goto do_fault_protect
;
1567 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
1568 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
1569 pte
|= PG_ACCESSED_MASK
;
1571 pte
|= PG_DIRTY_MASK
;
1572 stl_phys_notdirty(pte_addr
, pte
);
1575 virt_addr
= addr
& ~0xfff;
1576 pte
= pte
& (PHYS_ADDR_MASK
| 0xfff);
1581 /* page directory entry */
1582 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) &
1584 pde
= ldl_phys(pde_addr
);
1585 if (!(pde
& PG_PRESENT_MASK
)) {
1589 /* if PSE bit is set, then we use a 4MB page */
1590 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1591 page_size
= 4096 * 1024;
1593 if (!(pde
& PG_USER_MASK
))
1594 goto do_fault_protect
;
1595 if (is_write
&& !(pde
& PG_RW_MASK
))
1596 goto do_fault_protect
;
1598 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1599 is_write
&& !(pde
& PG_RW_MASK
))
1600 goto do_fault_protect
;
1602 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
1603 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
1604 pde
|= PG_ACCESSED_MASK
;
1606 pde
|= PG_DIRTY_MASK
;
1607 stl_phys_notdirty(pde_addr
, pde
);
1610 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
1612 virt_addr
= addr
& ~(page_size
- 1);
1614 if (!(pde
& PG_ACCESSED_MASK
)) {
1615 pde
|= PG_ACCESSED_MASK
;
1616 stl_phys_notdirty(pde_addr
, pde
);
1619 /* page directory entry */
1620 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) &
1622 pte
= ldl_phys(pte_addr
);
1623 if (!(pte
& PG_PRESENT_MASK
)) {
1627 /* combine pde and pte user and rw protections */
1630 if (!(ptep
& PG_USER_MASK
))
1631 goto do_fault_protect
;
1632 if (is_write
&& !(ptep
& PG_RW_MASK
))
1633 goto do_fault_protect
;
1635 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1636 is_write
&& !(ptep
& PG_RW_MASK
))
1637 goto do_fault_protect
;
1639 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
1640 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
1641 pte
|= PG_ACCESSED_MASK
;
1643 pte
|= PG_DIRTY_MASK
;
1644 stl_phys_notdirty(pte_addr
, pte
);
1647 virt_addr
= addr
& ~0xfff;
1650 /* the page can be put in the TLB */
1652 if (!(ptep
& PG_NX_MASK
))
1654 if (pte
& PG_DIRTY_MASK
) {
1655 /* only set write access if already dirty... otherwise wait
1658 if (ptep
& PG_RW_MASK
)
1661 if (!(env
->cr
[0] & CR0_WP_MASK
) ||
1662 (ptep
& PG_RW_MASK
))
1667 pte
= pte
& env
->a20_mask
;
1669 /* Even if 4MB pages, we map only one 4KB page in the cache to
1670 avoid filling it too fast */
1671 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
1672 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
1673 vaddr
= virt_addr
+ page_offset
;
1675 ret
= tlb_set_page_exec(env
, vaddr
, paddr
, prot
, mmu_idx
, is_softmmu
);
1678 error_code
= PG_ERROR_P_MASK
;
1680 error_code
|= (is_write
<< PG_ERROR_W_BIT
);
1682 error_code
|= PG_ERROR_U_MASK
;
1683 if (is_write1
== 2 &&
1684 (env
->efer
& MSR_EFER_NXE
) &&
1685 (env
->cr
[4] & CR4_PAE_MASK
))
1686 error_code
|= PG_ERROR_I_D_MASK
;
1687 if (env
->intercept_exceptions
& (1 << EXCP0E_PAGE
)) {
1688 /* cr2 is not modified in case of exceptions */
1689 stq_phys(env
->vm_vmcb
+ offsetof(struct vmcb
, control
.exit_info_2
),
1694 env
->error_code
= error_code
;
1695 env
->exception_index
= EXCP0E_PAGE
;
1699 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
1701 target_ulong pde_addr
, pte_addr
;
1703 target_phys_addr_t paddr
;
1704 uint32_t page_offset
;
1707 if (env
->cr
[4] & CR4_PAE_MASK
) {
1708 target_ulong pdpe_addr
;
1711 #ifdef TARGET_X86_64
1712 if (env
->hflags
& HF_LMA_MASK
) {
1713 uint64_t pml4e_addr
, pml4e
;
1716 /* test virtual address sign extension */
1717 sext
= (int64_t)addr
>> 47;
1718 if (sext
!= 0 && sext
!= -1)
1721 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
1723 pml4e
= ldq_phys(pml4e_addr
);
1724 if (!(pml4e
& PG_PRESENT_MASK
))
1727 pdpe_addr
= ((pml4e
& ~0xfff) + (((addr
>> 30) & 0x1ff) << 3)) &
1729 pdpe
= ldq_phys(pdpe_addr
);
1730 if (!(pdpe
& PG_PRESENT_MASK
))
1735 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
1737 pdpe
= ldq_phys(pdpe_addr
);
1738 if (!(pdpe
& PG_PRESENT_MASK
))
1742 pde_addr
= ((pdpe
& ~0xfff) + (((addr
>> 21) & 0x1ff) << 3)) &
1744 pde
= ldq_phys(pde_addr
);
1745 if (!(pde
& PG_PRESENT_MASK
)) {
1748 if (pde
& PG_PSE_MASK
) {
1750 page_size
= 2048 * 1024;
1751 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
1754 pte_addr
= ((pde
& ~0xfff) + (((addr
>> 12) & 0x1ff) << 3)) &
1757 pte
= ldq_phys(pte_addr
);
1759 if (!(pte
& PG_PRESENT_MASK
))
1764 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1768 /* page directory entry */
1769 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) & env
->a20_mask
;
1770 pde
= ldl_phys(pde_addr
);
1771 if (!(pde
& PG_PRESENT_MASK
))
1773 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1774 pte
= pde
& ~0x003ff000; /* align to 4MB */
1775 page_size
= 4096 * 1024;
1777 /* page directory entry */
1778 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) & env
->a20_mask
;
1779 pte
= ldl_phys(pte_addr
);
1780 if (!(pte
& PG_PRESENT_MASK
))
1785 pte
= pte
& env
->a20_mask
;
1788 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
1789 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
1793 void hw_breakpoint_insert(CPUState
*env
, int index
)
1797 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
1799 if (hw_breakpoint_enabled(env
->dr
[7], index
))
1800 err
= cpu_breakpoint_insert(env
, env
->dr
[index
], BP_CPU
,
1801 &env
->cpu_breakpoint
[index
]);
1804 type
= BP_CPU
| BP_MEM_WRITE
;
1807 /* No support for I/O watchpoints yet */
1810 type
= BP_CPU
| BP_MEM_ACCESS
;
1812 err
= cpu_watchpoint_insert(env
, env
->dr
[index
],
1813 hw_breakpoint_len(env
->dr
[7], index
),
1814 type
, &env
->cpu_watchpoint
[index
]);
1818 env
->cpu_breakpoint
[index
] = NULL
;
1821 void hw_breakpoint_remove(CPUState
*env
, int index
)
1823 if (!env
->cpu_breakpoint
[index
])
1825 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
1827 if (hw_breakpoint_enabled(env
->dr
[7], index
))
1828 cpu_breakpoint_remove_by_ref(env
, env
->cpu_breakpoint
[index
]);
1832 cpu_watchpoint_remove_by_ref(env
, env
->cpu_watchpoint
[index
]);
1835 /* No support for I/O watchpoints yet */
1840 int check_hw_breakpoints(CPUState
*env
, int force_dr6_update
)
1844 int hit_enabled
= 0;
1846 dr6
= env
->dr
[6] & ~0xf;
1847 for (reg
= 0; reg
< 4; reg
++) {
1848 type
= hw_breakpoint_type(env
->dr
[7], reg
);
1849 if ((type
== 0 && env
->dr
[reg
] == env
->eip
) ||
1850 ((type
& 1) && env
->cpu_watchpoint
[reg
] &&
1851 (env
->cpu_watchpoint
[reg
]->flags
& BP_WATCHPOINT_HIT
))) {
1853 if (hw_breakpoint_enabled(env
->dr
[7], reg
))
1857 if (hit_enabled
|| force_dr6_update
)
1862 static CPUDebugExcpHandler
*prev_debug_excp_handler
;
1864 void raise_exception(int exception_index
);
1866 static void breakpoint_handler(CPUState
*env
)
1870 if (env
->watchpoint_hit
) {
1871 if (env
->watchpoint_hit
->flags
& BP_CPU
) {
1872 env
->watchpoint_hit
= NULL
;
1873 if (check_hw_breakpoints(env
, 0))
1874 raise_exception(EXCP01_DB
);
1876 cpu_resume_from_signal(env
, NULL
);
1879 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
)
1880 if (bp
->pc
== env
->eip
) {
1881 if (bp
->flags
& BP_CPU
) {
1882 check_hw_breakpoints(env
, 1);
1883 raise_exception(EXCP01_DB
);
1888 if (prev_debug_excp_handler
)
1889 prev_debug_excp_handler(env
);
1892 /* This should come from sysemu.h - if we could include it here... */
1893 void qemu_system_reset_request(void);
1895 void cpu_inject_x86_mce(CPUState
*cenv
, int bank
, uint64_t status
,
1896 uint64_t mcg_status
, uint64_t addr
, uint64_t misc
)
1898 uint64_t mcg_cap
= cenv
->mcg_cap
;
1899 unsigned bank_num
= mcg_cap
& 0xff;
1900 uint64_t *banks
= cenv
->mce_banks
;
1902 if (bank
>= bank_num
|| !(status
& MCI_STATUS_VAL
))
1906 * if MSR_MCG_CTL is not all 1s, the uncorrected error
1907 * reporting is disabled
1909 if ((status
& MCI_STATUS_UC
) && (mcg_cap
& MCG_CTL_P
) &&
1910 cenv
->mcg_ctl
!= ~(uint64_t)0)
1914 * if MSR_MCi_CTL is not all 1s, the uncorrected error
1915 * reporting is disabled for the bank
1917 if ((status
& MCI_STATUS_UC
) && banks
[0] != ~(uint64_t)0)
1919 if (status
& MCI_STATUS_UC
) {
1920 if ((cenv
->mcg_status
& MCG_STATUS_MCIP
) ||
1921 !(cenv
->cr
[4] & CR4_MCE_MASK
)) {
1922 fprintf(stderr
, "injects mce exception while previous "
1923 "one is in progress!\n");
1924 qemu_log_mask(CPU_LOG_RESET
, "Triple fault\n");
1925 qemu_system_reset_request();
1928 if (banks
[1] & MCI_STATUS_VAL
)
1929 status
|= MCI_STATUS_OVER
;
1932 cenv
->mcg_status
= mcg_status
;
1934 cpu_interrupt(cenv
, CPU_INTERRUPT_MCE
);
1935 } else if (!(banks
[1] & MCI_STATUS_VAL
)
1936 || !(banks
[1] & MCI_STATUS_UC
)) {
1937 if (banks
[1] & MCI_STATUS_VAL
)
1938 status
|= MCI_STATUS_OVER
;
1943 banks
[1] |= MCI_STATUS_OVER
;
1945 #endif /* !CONFIG_USER_ONLY */
1947 static void mce_init(CPUX86State
*cenv
)
1949 unsigned int bank
, bank_num
;
1951 if (((cenv
->cpuid_version
>> 8)&0xf) >= 6
1952 && (cenv
->cpuid_features
&(CPUID_MCE
|CPUID_MCA
)) == (CPUID_MCE
|CPUID_MCA
)) {
1953 cenv
->mcg_cap
= MCE_CAP_DEF
| MCE_BANKS_DEF
;
1954 cenv
->mcg_ctl
= ~(uint64_t)0;
1955 bank_num
= MCE_BANKS_DEF
;
1956 for (bank
= 0; bank
< bank_num
; bank
++)
1957 cenv
->mce_banks
[bank
*4] = ~(uint64_t)0;
1961 static void host_cpuid(uint32_t function
, uint32_t count
,
1962 uint32_t *eax
, uint32_t *ebx
,
1963 uint32_t *ecx
, uint32_t *edx
)
1965 #if defined(CONFIG_KVM)
1969 asm volatile("cpuid"
1970 : "=a"(vec
[0]), "=b"(vec
[1]),
1971 "=c"(vec
[2]), "=d"(vec
[3])
1972 : "0"(function
), "c"(count
) : "cc");
1974 asm volatile("pusha \n\t"
1976 "mov %%eax, 0(%2) \n\t"
1977 "mov %%ebx, 4(%2) \n\t"
1978 "mov %%ecx, 8(%2) \n\t"
1979 "mov %%edx, 12(%2) \n\t"
1981 : : "a"(function
), "c"(count
), "S"(vec
)
1996 static void get_cpuid_vendor(CPUX86State
*env
, uint32_t *ebx
,
1997 uint32_t *ecx
, uint32_t *edx
)
1999 *ebx
= env
->cpuid_vendor1
;
2000 *edx
= env
->cpuid_vendor2
;
2001 *ecx
= env
->cpuid_vendor3
;
2003 /* sysenter isn't supported on compatibility mode on AMD, syscall
2004 * isn't supported in compatibility mode on Intel.
2005 * Normally we advertise the actual cpu vendor, but you can override
2006 * this if you want to use KVM's sysenter/syscall emulation
2007 * in compatibility mode and when doing cross vendor migration
2009 if (kvm_enabled() && env
->cpuid_vendor_override
) {
2010 host_cpuid(0, 0, NULL
, ebx
, ecx
, edx
);
2014 void cpu_x86_cpuid(CPUX86State
*env
, uint32_t index
, uint32_t count
,
2015 uint32_t *eax
, uint32_t *ebx
,
2016 uint32_t *ecx
, uint32_t *edx
)
2018 /* test if maximum index reached */
2019 if (index
& 0x80000000) {
2020 if (index
> env
->cpuid_xlevel
)
2021 index
= env
->cpuid_level
;
2023 if (index
> env
->cpuid_level
)
2024 index
= env
->cpuid_level
;
2029 *eax
= env
->cpuid_level
;
2030 get_cpuid_vendor(env
, ebx
, ecx
, edx
);
2033 *eax
= env
->cpuid_version
;
2034 *ebx
= (env
->cpuid_apic_id
<< 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
2035 *ecx
= env
->cpuid_ext_features
;
2036 *edx
= env
->cpuid_features
;
2037 if (env
->nr_cores
* env
->nr_threads
> 1) {
2038 *ebx
|= (env
->nr_cores
* env
->nr_threads
) << 16;
2039 *edx
|= 1 << 28; /* HTT bit */
2043 /* cache info: needed for Pentium Pro compatibility */
2050 /* cache info: needed for Core compatibility */
2051 if (env
->nr_cores
> 1) {
2052 *eax
= (env
->nr_cores
- 1) << 26;
2057 case 0: /* L1 dcache info */
2063 case 1: /* L1 icache info */
2069 case 2: /* L2 cache info */
2071 if (env
->nr_threads
> 1) {
2072 *eax
|= (env
->nr_threads
- 1) << 14;
2078 default: /* end of info */
2087 /* mwait info: needed for Core compatibility */
2088 *eax
= 0; /* Smallest monitor-line size in bytes */
2089 *ebx
= 0; /* Largest monitor-line size in bytes */
2090 *ecx
= CPUID_MWAIT_EMX
| CPUID_MWAIT_IBE
;
2094 /* Thermal and Power Leaf */
2101 /* Direct Cache Access Information Leaf */
2102 *eax
= 0; /* Bits 0-31 in DCA_CAP MSR */
2108 /* Architectural Performance Monitoring Leaf */
2115 *eax
= env
->cpuid_xlevel
;
2116 *ebx
= env
->cpuid_vendor1
;
2117 *edx
= env
->cpuid_vendor2
;
2118 *ecx
= env
->cpuid_vendor3
;
2121 *eax
= env
->cpuid_version
;
2123 *ecx
= env
->cpuid_ext3_features
;
2124 *edx
= env
->cpuid_ext2_features
;
2126 /* The Linux kernel checks for the CMPLegacy bit and
2127 * discards multiple thread information if it is set.
2128 * So dont set it here for Intel to make Linux guests happy.
2130 if (env
->nr_cores
* env
->nr_threads
> 1) {
2131 uint32_t tebx
, tecx
, tedx
;
2132 get_cpuid_vendor(env
, &tebx
, &tecx
, &tedx
);
2133 if (tebx
!= CPUID_VENDOR_INTEL_1
||
2134 tedx
!= CPUID_VENDOR_INTEL_2
||
2135 tecx
!= CPUID_VENDOR_INTEL_3
) {
2136 *ecx
|= 1 << 1; /* CmpLegacy bit */
2140 if (kvm_enabled()) {
2141 /* Nested SVM not yet supported in upstream QEMU */
2142 *ecx
&= ~CPUID_EXT3_SVM
;
2148 *eax
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 0];
2149 *ebx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 1];
2150 *ecx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 2];
2151 *edx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 3];
2154 /* cache info (L1 cache) */
2161 /* cache info (L2 cache) */
2168 /* virtual & phys address size in low 2 bytes. */
2169 /* XXX: This value must match the one used in the MMU code. */
2170 if (env
->cpuid_ext2_features
& CPUID_EXT2_LM
) {
2171 /* 64 bit processor */
2172 /* XXX: The physical address space is limited to 42 bits in exec.c. */
2173 *eax
= 0x00003028; /* 48 bits virtual, 40 bits physical */
2175 if (env
->cpuid_features
& CPUID_PSE36
)
2176 *eax
= 0x00000024; /* 36 bits physical */
2178 *eax
= 0x00000020; /* 32 bits physical */
2183 if (env
->nr_cores
* env
->nr_threads
> 1) {
2184 *ecx
|= (env
->nr_cores
* env
->nr_threads
) - 1;
2188 *eax
= 0x00000001; /* SVM Revision */
2189 *ebx
= 0x00000010; /* nr of ASIDs */
2191 *edx
= 0; /* optional features */
2194 /* reserved values: zero */
2204 int cpu_x86_get_descr_debug(CPUX86State
*env
, unsigned int selector
,
2205 target_ulong
*base
, unsigned int *limit
,
2206 unsigned int *flags
)
2217 index
= selector
& ~7;
2218 ptr
= dt
->base
+ index
;
2219 if ((index
+ 7) > dt
->limit
2220 || cpu_memory_rw_debug(env
, ptr
, (uint8_t *)&e1
, sizeof(e1
), 0) != 0
2221 || cpu_memory_rw_debug(env
, ptr
+4, (uint8_t *)&e2
, sizeof(e2
), 0) != 0)
2224 *base
= ((e1
>> 16) | ((e2
& 0xff) << 16) | (e2
& 0xff000000));
2225 *limit
= (e1
& 0xffff) | (e2
& 0x000f0000);
2226 if (e2
& DESC_G_MASK
)
2227 *limit
= (*limit
<< 12) | 0xfff;
2233 CPUX86State
*cpu_x86_init(const char *cpu_model
)
2238 env
= qemu_mallocz(sizeof(CPUX86State
));
2240 env
->cpu_model_str
= cpu_model
;
2242 /* init various static tables */
2245 optimize_flags_init();
2246 #ifndef CONFIG_USER_ONLY
2247 prev_debug_excp_handler
=
2248 cpu_set_debug_excp_handler(breakpoint_handler
);
2251 if (cpu_x86_register(env
, cpu_model
) < 0) {
2257 qemu_init_vcpu(env
);
2262 #if !defined(CONFIG_USER_ONLY)
2263 void do_cpu_init(CPUState
*env
)
2265 int sipi
= env
->interrupt_request
& CPU_INTERRUPT_SIPI
;
2267 env
->interrupt_request
= sipi
;
2268 apic_init_reset(env
);
2271 void do_cpu_sipi(CPUState
*env
)
2276 void do_cpu_init(CPUState
*env
)
2279 void do_cpu_sipi(CPUState
*env
)