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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
29 #include "qemu-common.h"
34 /* feature flags taken from "Intel Processor Identification and the CPUID
35 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
36 * about feature names, the Linux name is used. */
37 static const char *feature_name
[] = {
38 "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
39 "cx8", "apic", NULL
, "sep", "mtrr", "pge", "mca", "cmov",
40 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */, NULL
, "ds" /* Intel dts */, "acpi", "mmx",
41 "fxsr", "sse", "sse2", "ss", "ht" /* Intel htt */, "tm", "ia64", "pbe",
43 static const char *ext_feature_name
[] = {
44 "pni" /* Intel,AMD sse3 */, NULL
, NULL
, "monitor", "ds_cpl", "vmx", NULL
/* Linux smx */, "est",
45 "tm2", "ssse3", "cid", NULL
, NULL
, "cx16", "xtpr", NULL
,
46 NULL
, NULL
, "dca", NULL
, NULL
, NULL
, NULL
, "popcnt",
47 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
49 static const char *ext2_feature_name
[] = {
50 "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
51 "cx8" /* AMD CMPXCHG8B */, "apic", NULL
, "syscall", "mtrr", "pge", "mca", "cmov",
52 "pat", "pse36", NULL
, NULL
/* Linux mp */, "nx" /* Intel xd */, NULL
, "mmxext", "mmx",
53 "fxsr", "fxsr_opt" /* AMD ffxsr */, "pdpe1gb" /* AMD Page1GB */, "rdtscp", NULL
, "lm" /* Intel 64 */, "3dnowext", "3dnow",
55 static const char *ext3_feature_name
[] = {
56 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */, "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
57 "3dnowprefetch", "osvw", NULL
/* Linux ibs */, NULL
, "skinit", "wdt", NULL
, NULL
,
58 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
59 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
62 static void add_flagname_to_bitmaps(char *flagname
, uint32_t *features
,
63 uint32_t *ext_features
,
64 uint32_t *ext2_features
,
65 uint32_t *ext3_features
)
70 for ( i
= 0 ; i
< 32 ; i
++ )
71 if (feature_name
[i
] && !strcmp (flagname
, feature_name
[i
])) {
75 for ( i
= 0 ; i
< 32 ; i
++ )
76 if (ext_feature_name
[i
] && !strcmp (flagname
, ext_feature_name
[i
])) {
77 *ext_features
|= 1 << i
;
80 for ( i
= 0 ; i
< 32 ; i
++ )
81 if (ext2_feature_name
[i
] && !strcmp (flagname
, ext2_feature_name
[i
])) {
82 *ext2_features
|= 1 << i
;
85 for ( i
= 0 ; i
< 32 ; i
++ )
86 if (ext3_feature_name
[i
] && !strcmp (flagname
, ext3_feature_name
[i
])) {
87 *ext3_features
|= 1 << i
;
91 fprintf(stderr
, "CPU feature %s not found\n", flagname
);
95 static void kvm_trim_features(uint32_t *features
, uint32_t supported
,
101 for (i
= 0; i
< 32; ++i
) {
103 if ((*features
& mask
) && !(supported
& mask
)) {
109 typedef struct x86_def_t
{
112 uint32_t vendor1
, vendor2
, vendor3
;
116 uint32_t features
, ext_features
, ext2_features
, ext3_features
;
122 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
123 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
124 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX)
125 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
126 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
127 CPUID_PSE36 | CPUID_FXSR)
128 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
129 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
130 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
131 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
132 CPUID_PAE | CPUID_SEP | CPUID_APIC)
133 static x86_def_t x86_defs
[] = {
138 .vendor1
= CPUID_VENDOR_AMD_1
,
139 .vendor2
= CPUID_VENDOR_AMD_2
,
140 .vendor3
= CPUID_VENDOR_AMD_3
,
144 .features
= PPRO_FEATURES
|
145 /* these features are needed for Win64 and aren't fully implemented */
146 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
147 /* this feature is needed for Solaris and isn't fully implemented */
149 .ext_features
= CPUID_EXT_SSE3
,
150 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) |
151 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
152 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
153 .ext3_features
= CPUID_EXT3_SVM
,
154 .xlevel
= 0x8000000A,
155 .model_id
= "QEMU Virtual CPU version " QEMU_VERSION
,
160 .vendor1
= CPUID_VENDOR_AMD_1
,
161 .vendor2
= CPUID_VENDOR_AMD_2
,
162 .vendor3
= CPUID_VENDOR_AMD_3
,
166 /* Missing: CPUID_VME, CPUID_HT */
167 .features
= PPRO_FEATURES
|
168 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
170 /* Missing: CPUID_EXT_CX16, CPUID_EXT_POPCNT */
171 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
,
172 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
173 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) |
174 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
175 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
| CPUID_EXT2_MMXEXT
|
177 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
178 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
179 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
180 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
181 .ext3_features
= CPUID_EXT3_SVM
,
182 .xlevel
= 0x8000001A,
183 .model_id
= "AMD Phenom(tm) 9550 Quad-Core Processor"
191 /* The original CPU also implements these features:
192 CPUID_VME, CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
193 CPUID_TM, CPUID_PBE */
194 .features
= PPRO_FEATURES
|
195 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
197 /* The original CPU also implements these ext features:
198 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
199 CPUID_EXT_TM2, CPUID_EXT_CX16, CPUID_EXT_XTPR, CPUID_EXT_PDCM */
200 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
,
201 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
202 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
203 .xlevel
= 0x80000008,
204 .model_id
= "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
213 .features
= PPRO_FEATURES
,
214 .ext_features
= CPUID_EXT_SSE3
,
216 .model_id
= "QEMU Virtual CPU version " QEMU_VERSION
,
224 /* The original CPU also implements these features:
225 CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
226 CPUID_TM, CPUID_PBE */
227 .features
= PPRO_FEATURES
| CPUID_VME
|
228 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
,
229 /* The original CPU also implements these ext features:
230 CPUID_EXT_VMX, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_XTPR,
232 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
,
233 .ext2_features
= CPUID_EXT2_NX
,
234 .xlevel
= 0x80000008,
235 .model_id
= "Genuine Intel(R) CPU T2600 @ 2.16GHz",
243 .features
= I486_FEATURES
,
252 .features
= PENTIUM_FEATURES
,
261 .features
= PENTIUM2_FEATURES
,
270 .features
= PENTIUM3_FEATURES
,
276 .vendor1
= 0x68747541, /* "Auth" */
277 .vendor2
= 0x69746e65, /* "enti" */
278 .vendor3
= 0x444d4163, /* "cAMD" */
282 .features
= PPRO_FEATURES
| CPUID_PSE36
| CPUID_VME
| CPUID_MTRR
| CPUID_MCA
,
283 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) | CPUID_EXT2_MMXEXT
| CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
284 .xlevel
= 0x80000008,
285 /* XXX: put another string ? */
286 .model_id
= "QEMU Virtual CPU version " QEMU_VERSION
,
290 /* original is on level 10 */
295 .features
= PPRO_FEATURES
|
296 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_VME
,
297 /* Missing: CPUID_DTS | CPUID_ACPI | CPUID_SS |
298 * CPUID_HT | CPUID_TM | CPUID_PBE */
299 /* Some CPUs got no CPUID_SEP */
300 .ext_features
= CPUID_EXT_MONITOR
|
301 CPUID_EXT_SSE3
/* PNI */ | CPUID_EXT_SSSE3
,
302 /* Missing: CPUID_EXT_DSCPL | CPUID_EXT_EST |
303 * CPUID_EXT_TM2 | CPUID_EXT_XTPR */
304 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) | CPUID_EXT2_NX
,
305 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
306 .xlevel
= 0x8000000A,
307 .model_id
= "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
311 static int cpu_x86_find_by_name(x86_def_t
*x86_cpu_def
, const char *cpu_model
)
316 char *s
= strdup(cpu_model
);
317 char *featurestr
, *name
= strtok(s
, ",");
318 uint32_t plus_features
= 0, plus_ext_features
= 0, plus_ext2_features
= 0, plus_ext3_features
= 0;
319 uint32_t minus_features
= 0, minus_ext_features
= 0, minus_ext2_features
= 0, minus_ext3_features
= 0;
320 int family
= -1, model
= -1, stepping
= -1;
323 for (i
= 0; i
< ARRAY_SIZE(x86_defs
); i
++) {
324 if (strcmp(name
, x86_defs
[i
].name
) == 0) {
331 memcpy(x86_cpu_def
, def
, sizeof(*def
));
333 featurestr
= strtok(NULL
, ",");
337 if (featurestr
[0] == '+') {
338 add_flagname_to_bitmaps(featurestr
+ 1, &plus_features
, &plus_ext_features
, &plus_ext2_features
, &plus_ext3_features
);
339 } else if (featurestr
[0] == '-') {
340 add_flagname_to_bitmaps(featurestr
+ 1, &minus_features
, &minus_ext_features
, &minus_ext2_features
, &minus_ext3_features
);
341 } else if ((val
= strchr(featurestr
, '='))) {
343 if (!strcmp(featurestr
, "family")) {
345 family
= strtol(val
, &err
, 10);
346 if (!*val
|| *err
|| family
< 0) {
347 fprintf(stderr
, "bad numerical value %s\n", val
);
350 x86_cpu_def
->family
= family
;
351 } else if (!strcmp(featurestr
, "model")) {
353 model
= strtol(val
, &err
, 10);
354 if (!*val
|| *err
|| model
< 0 || model
> 0xff) {
355 fprintf(stderr
, "bad numerical value %s\n", val
);
358 x86_cpu_def
->model
= model
;
359 } else if (!strcmp(featurestr
, "stepping")) {
361 stepping
= strtol(val
, &err
, 10);
362 if (!*val
|| *err
|| stepping
< 0 || stepping
> 0xf) {
363 fprintf(stderr
, "bad numerical value %s\n", val
);
366 x86_cpu_def
->stepping
= stepping
;
367 } else if (!strcmp(featurestr
, "vendor")) {
368 if (strlen(val
) != 12) {
369 fprintf(stderr
, "vendor string must be 12 chars long\n");
372 x86_cpu_def
->vendor1
= 0;
373 x86_cpu_def
->vendor2
= 0;
374 x86_cpu_def
->vendor3
= 0;
375 for(i
= 0; i
< 4; i
++) {
376 x86_cpu_def
->vendor1
|= ((uint8_t)val
[i
]) << (8 * i
);
377 x86_cpu_def
->vendor2
|= ((uint8_t)val
[i
+ 4]) << (8 * i
);
378 x86_cpu_def
->vendor3
|= ((uint8_t)val
[i
+ 8]) << (8 * i
);
380 x86_cpu_def
->vendor_override
= 1;
381 } else if (!strcmp(featurestr
, "model_id")) {
382 pstrcpy(x86_cpu_def
->model_id
, sizeof(x86_cpu_def
->model_id
),
385 fprintf(stderr
, "unrecognized feature %s\n", featurestr
);
389 fprintf(stderr
, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr
);
392 featurestr
= strtok(NULL
, ",");
394 x86_cpu_def
->features
|= plus_features
;
395 x86_cpu_def
->ext_features
|= plus_ext_features
;
396 x86_cpu_def
->ext2_features
|= plus_ext2_features
;
397 x86_cpu_def
->ext3_features
|= plus_ext3_features
;
398 x86_cpu_def
->features
&= ~minus_features
;
399 x86_cpu_def
->ext_features
&= ~minus_ext_features
;
400 x86_cpu_def
->ext2_features
&= ~minus_ext2_features
;
401 x86_cpu_def
->ext3_features
&= ~minus_ext3_features
;
410 void x86_cpu_list (FILE *f
, int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...))
414 for (i
= 0; i
< ARRAY_SIZE(x86_defs
); i
++)
415 (*cpu_fprintf
)(f
, "x86 %16s\n", x86_defs
[i
].name
);
418 static int cpu_x86_register (CPUX86State
*env
, const char *cpu_model
)
420 x86_def_t def1
, *def
= &def1
;
422 if (cpu_x86_find_by_name(def
, cpu_model
) < 0)
425 env
->cpuid_vendor1
= def
->vendor1
;
426 env
->cpuid_vendor2
= def
->vendor2
;
427 env
->cpuid_vendor3
= def
->vendor3
;
429 env
->cpuid_vendor1
= CPUID_VENDOR_INTEL_1
;
430 env
->cpuid_vendor2
= CPUID_VENDOR_INTEL_2
;
431 env
->cpuid_vendor3
= CPUID_VENDOR_INTEL_3
;
433 env
->cpuid_vendor_override
= def
->vendor_override
;
434 env
->cpuid_level
= def
->level
;
435 if (def
->family
> 0x0f)
436 env
->cpuid_version
= 0xf00 | ((def
->family
- 0x0f) << 20);
438 env
->cpuid_version
= def
->family
<< 8;
439 env
->cpuid_version
|= ((def
->model
& 0xf) << 4) | ((def
->model
>> 4) << 16);
440 env
->cpuid_version
|= def
->stepping
;
441 env
->cpuid_features
= def
->features
;
442 env
->pat
= 0x0007040600070406ULL
;
443 env
->cpuid_ext_features
= def
->ext_features
;
444 env
->cpuid_ext2_features
= def
->ext2_features
;
445 env
->cpuid_xlevel
= def
->xlevel
;
446 env
->cpuid_ext3_features
= def
->ext3_features
;
448 const char *model_id
= def
->model_id
;
452 len
= strlen(model_id
);
453 for(i
= 0; i
< 48; i
++) {
457 c
= (uint8_t)model_id
[i
];
458 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
464 /* NOTE: must be called outside the CPU execute loop */
465 void cpu_reset(CPUX86State
*env
)
469 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
470 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
471 log_cpu_state(env
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
474 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
478 env
->old_exception
= -1;
480 /* init to reset state */
482 #ifdef CONFIG_SOFTMMU
483 env
->hflags
|= HF_SOFTMMU_MASK
;
485 env
->hflags2
|= HF2_GIF_MASK
;
487 cpu_x86_update_cr0(env
, 0x60000010);
488 env
->a20_mask
= ~0x0;
489 env
->smbase
= 0x30000;
491 env
->idt
.limit
= 0xffff;
492 env
->gdt
.limit
= 0xffff;
493 env
->ldt
.limit
= 0xffff;
494 env
->ldt
.flags
= DESC_P_MASK
| (2 << DESC_TYPE_SHIFT
);
495 env
->tr
.limit
= 0xffff;
496 env
->tr
.flags
= DESC_P_MASK
| (11 << DESC_TYPE_SHIFT
);
498 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff,
499 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
|
500 DESC_R_MASK
| DESC_A_MASK
);
501 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff,
502 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
504 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff,
505 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
507 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff,
508 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
510 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff,
511 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
513 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff,
514 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
518 env
->regs
[R_EDX
] = env
->cpuid_version
;
523 for(i
= 0;i
< 8; i
++)
529 memset(env
->dr
, 0, sizeof(env
->dr
));
530 env
->dr
[6] = DR6_FIXED_1
;
531 env
->dr
[7] = DR7_FIXED_1
;
532 cpu_breakpoint_remove_all(env
, BP_CPU
);
533 cpu_watchpoint_remove_all(env
, BP_CPU
);
536 void cpu_x86_close(CPUX86State
*env
)
541 /***********************************************************/
544 static const char *cc_op_str
[] = {
600 cpu_x86_dump_seg_cache(CPUState
*env
, FILE *f
,
601 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
602 const char *name
, struct SegmentCache
*sc
)
605 if (env
->hflags
& HF_CS64_MASK
) {
606 cpu_fprintf(f
, "%-3s=%04x %016" PRIx64
" %08x %08x", name
,
607 sc
->selector
, sc
->base
, sc
->limit
, sc
->flags
);
611 cpu_fprintf(f
, "%-3s=%04x %08x %08x %08x", name
, sc
->selector
,
612 (uint32_t)sc
->base
, sc
->limit
, sc
->flags
);
615 if (!(env
->hflags
& HF_PE_MASK
) || !(sc
->flags
& DESC_P_MASK
))
618 cpu_fprintf(f
, " DPL=%d ", (sc
->flags
& DESC_DPL_MASK
) >> DESC_DPL_SHIFT
);
619 if (sc
->flags
& DESC_S_MASK
) {
620 if (sc
->flags
& DESC_CS_MASK
) {
621 cpu_fprintf(f
, (sc
->flags
& DESC_L_MASK
) ? "CS64" :
622 ((sc
->flags
& DESC_B_MASK
) ? "CS32" : "CS16"));
623 cpu_fprintf(f
, " [%c%c", (sc
->flags
& DESC_C_MASK
) ? 'C' : '-',
624 (sc
->flags
& DESC_R_MASK
) ? 'R' : '-');
626 cpu_fprintf(f
, (sc
->flags
& DESC_B_MASK
) ? "DS " : "DS16");
627 cpu_fprintf(f
, " [%c%c", (sc
->flags
& DESC_E_MASK
) ? 'E' : '-',
628 (sc
->flags
& DESC_W_MASK
) ? 'W' : '-');
630 cpu_fprintf(f
, "%c]", (sc
->flags
& DESC_A_MASK
) ? 'A' : '-');
632 static const char *sys_type_name
[2][16] = {
634 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
635 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
636 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
637 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
640 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
641 "Reserved", "Reserved", "Reserved", "Reserved",
642 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
643 "Reserved", "IntGate64", "TrapGate64"
646 cpu_fprintf(f
, sys_type_name
[(env
->hflags
& HF_LMA_MASK
) ? 1 : 0]
647 [(sc
->flags
& DESC_TYPE_MASK
)
648 >> DESC_TYPE_SHIFT
]);
651 cpu_fprintf(f
, "\n");
654 void cpu_dump_state(CPUState
*env
, FILE *f
,
655 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
660 static const char *seg_name
[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
663 kvm_arch_get_registers(env
);
665 eflags
= env
->eflags
;
667 if (env
->hflags
& HF_CS64_MASK
) {
669 "RAX=%016" PRIx64
" RBX=%016" PRIx64
" RCX=%016" PRIx64
" RDX=%016" PRIx64
"\n"
670 "RSI=%016" PRIx64
" RDI=%016" PRIx64
" RBP=%016" PRIx64
" RSP=%016" PRIx64
"\n"
671 "R8 =%016" PRIx64
" R9 =%016" PRIx64
" R10=%016" PRIx64
" R11=%016" PRIx64
"\n"
672 "R12=%016" PRIx64
" R13=%016" PRIx64
" R14=%016" PRIx64
" R15=%016" PRIx64
"\n"
673 "RIP=%016" PRIx64
" RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
691 eflags
& DF_MASK
? 'D' : '-',
692 eflags
& CC_O
? 'O' : '-',
693 eflags
& CC_S
? 'S' : '-',
694 eflags
& CC_Z
? 'Z' : '-',
695 eflags
& CC_A
? 'A' : '-',
696 eflags
& CC_P
? 'P' : '-',
697 eflags
& CC_C
? 'C' : '-',
698 env
->hflags
& HF_CPL_MASK
,
699 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
700 (int)(env
->a20_mask
>> 20) & 1,
701 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
706 cpu_fprintf(f
, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
707 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
708 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
709 (uint32_t)env
->regs
[R_EAX
],
710 (uint32_t)env
->regs
[R_EBX
],
711 (uint32_t)env
->regs
[R_ECX
],
712 (uint32_t)env
->regs
[R_EDX
],
713 (uint32_t)env
->regs
[R_ESI
],
714 (uint32_t)env
->regs
[R_EDI
],
715 (uint32_t)env
->regs
[R_EBP
],
716 (uint32_t)env
->regs
[R_ESP
],
717 (uint32_t)env
->eip
, eflags
,
718 eflags
& DF_MASK
? 'D' : '-',
719 eflags
& CC_O
? 'O' : '-',
720 eflags
& CC_S
? 'S' : '-',
721 eflags
& CC_Z
? 'Z' : '-',
722 eflags
& CC_A
? 'A' : '-',
723 eflags
& CC_P
? 'P' : '-',
724 eflags
& CC_C
? 'C' : '-',
725 env
->hflags
& HF_CPL_MASK
,
726 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
727 (int)(env
->a20_mask
>> 20) & 1,
728 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
732 for(i
= 0; i
< 6; i
++) {
733 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, seg_name
[i
],
736 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, "LDT", &env
->ldt
);
737 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, "TR", &env
->tr
);
740 if (env
->hflags
& HF_LMA_MASK
) {
741 cpu_fprintf(f
, "GDT= %016" PRIx64
" %08x\n",
742 env
->gdt
.base
, env
->gdt
.limit
);
743 cpu_fprintf(f
, "IDT= %016" PRIx64
" %08x\n",
744 env
->idt
.base
, env
->idt
.limit
);
745 cpu_fprintf(f
, "CR0=%08x CR2=%016" PRIx64
" CR3=%016" PRIx64
" CR4=%08x\n",
746 (uint32_t)env
->cr
[0],
749 (uint32_t)env
->cr
[4]);
750 for(i
= 0; i
< 4; i
++)
751 cpu_fprintf(f
, "DR%d=%016" PRIx64
" ", i
, env
->dr
[i
]);
752 cpu_fprintf(f
, "\nDR6=%016" PRIx64
" DR7=%016" PRIx64
"\n",
753 env
->dr
[6], env
->dr
[7]);
757 cpu_fprintf(f
, "GDT= %08x %08x\n",
758 (uint32_t)env
->gdt
.base
, env
->gdt
.limit
);
759 cpu_fprintf(f
, "IDT= %08x %08x\n",
760 (uint32_t)env
->idt
.base
, env
->idt
.limit
);
761 cpu_fprintf(f
, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
762 (uint32_t)env
->cr
[0],
763 (uint32_t)env
->cr
[2],
764 (uint32_t)env
->cr
[3],
765 (uint32_t)env
->cr
[4]);
766 for(i
= 0; i
< 4; i
++)
767 cpu_fprintf(f
, "DR%d=%08x ", i
, env
->dr
[i
]);
768 cpu_fprintf(f
, "\nDR6=%08x DR7=%08x\n", env
->dr
[6], env
->dr
[7]);
770 if (flags
& X86_DUMP_CCOP
) {
771 if ((unsigned)env
->cc_op
< CC_OP_NB
)
772 snprintf(cc_op_name
, sizeof(cc_op_name
), "%s", cc_op_str
[env
->cc_op
]);
774 snprintf(cc_op_name
, sizeof(cc_op_name
), "[%d]", env
->cc_op
);
776 if (env
->hflags
& HF_CS64_MASK
) {
777 cpu_fprintf(f
, "CCS=%016" PRIx64
" CCD=%016" PRIx64
" CCO=%-8s\n",
778 env
->cc_src
, env
->cc_dst
,
783 cpu_fprintf(f
, "CCS=%08x CCD=%08x CCO=%-8s\n",
784 (uint32_t)env
->cc_src
, (uint32_t)env
->cc_dst
,
788 if (flags
& X86_DUMP_FPU
) {
791 for(i
= 0; i
< 8; i
++) {
792 fptag
|= ((!env
->fptags
[i
]) << i
);
794 cpu_fprintf(f
, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
796 (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11,
801 #if defined(USE_X86LDOUBLE)
809 tmp
.d
= env
->fpregs
[i
].d
;
810 cpu_fprintf(f
, "FPR%d=%016" PRIx64
" %04x",
811 i
, tmp
.l
.lower
, tmp
.l
.upper
);
813 cpu_fprintf(f
, "FPR%d=%016" PRIx64
,
814 i
, env
->fpregs
[i
].mmx
.q
);
817 cpu_fprintf(f
, "\n");
821 if (env
->hflags
& HF_CS64_MASK
)
826 cpu_fprintf(f
, "XMM%02d=%08x%08x%08x%08x",
828 env
->xmm_regs
[i
].XMM_L(3),
829 env
->xmm_regs
[i
].XMM_L(2),
830 env
->xmm_regs
[i
].XMM_L(1),
831 env
->xmm_regs
[i
].XMM_L(0));
833 cpu_fprintf(f
, "\n");
840 /***********************************************************/
842 /* XXX: add PGE support */
844 void cpu_x86_set_a20(CPUX86State
*env
, int a20_state
)
846 a20_state
= (a20_state
!= 0);
847 if (a20_state
!= ((env
->a20_mask
>> 20) & 1)) {
848 #if defined(DEBUG_MMU)
849 printf("A20 update: a20=%d\n", a20_state
);
851 /* if the cpu is currently executing code, we must unlink it and
852 all the potentially executing TB */
853 cpu_interrupt(env
, CPU_INTERRUPT_EXITTB
);
855 /* when a20 is changed, all the MMU mappings are invalid, so
856 we must flush everything */
858 env
->a20_mask
= (~0x100000) | (a20_state
<< 20);
862 void cpu_x86_update_cr0(CPUX86State
*env
, uint32_t new_cr0
)
866 #if defined(DEBUG_MMU)
867 printf("CR0 update: CR0=0x%08x\n", new_cr0
);
869 if ((new_cr0
& (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
)) !=
870 (env
->cr
[0] & (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
))) {
875 if (!(env
->cr
[0] & CR0_PG_MASK
) && (new_cr0
& CR0_PG_MASK
) &&
876 (env
->efer
& MSR_EFER_LME
)) {
877 /* enter in long mode */
878 /* XXX: generate an exception */
879 if (!(env
->cr
[4] & CR4_PAE_MASK
))
881 env
->efer
|= MSR_EFER_LMA
;
882 env
->hflags
|= HF_LMA_MASK
;
883 } else if ((env
->cr
[0] & CR0_PG_MASK
) && !(new_cr0
& CR0_PG_MASK
) &&
884 (env
->efer
& MSR_EFER_LMA
)) {
886 env
->efer
&= ~MSR_EFER_LMA
;
887 env
->hflags
&= ~(HF_LMA_MASK
| HF_CS64_MASK
);
888 env
->eip
&= 0xffffffff;
891 env
->cr
[0] = new_cr0
| CR0_ET_MASK
;
893 /* update PE flag in hidden flags */
894 pe_state
= (env
->cr
[0] & CR0_PE_MASK
);
895 env
->hflags
= (env
->hflags
& ~HF_PE_MASK
) | (pe_state
<< HF_PE_SHIFT
);
896 /* ensure that ADDSEG is always set in real mode */
897 env
->hflags
|= ((pe_state
^ 1) << HF_ADDSEG_SHIFT
);
898 /* update FPU flags */
899 env
->hflags
= (env
->hflags
& ~(HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
)) |
900 ((new_cr0
<< (HF_MP_SHIFT
- 1)) & (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
));
903 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
905 void cpu_x86_update_cr3(CPUX86State
*env
, target_ulong new_cr3
)
907 env
->cr
[3] = new_cr3
;
908 if (env
->cr
[0] & CR0_PG_MASK
) {
909 #if defined(DEBUG_MMU)
910 printf("CR3 update: CR3=" TARGET_FMT_lx
"\n", new_cr3
);
916 void cpu_x86_update_cr4(CPUX86State
*env
, uint32_t new_cr4
)
918 #if defined(DEBUG_MMU)
919 printf("CR4 update: CR4=%08x\n", (uint32_t)env
->cr
[4]);
921 if ((new_cr4
& (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
)) !=
922 (env
->cr
[4] & (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
))) {
926 if (!(env
->cpuid_features
& CPUID_SSE
))
927 new_cr4
&= ~CR4_OSFXSR_MASK
;
928 if (new_cr4
& CR4_OSFXSR_MASK
)
929 env
->hflags
|= HF_OSFXSR_MASK
;
931 env
->hflags
&= ~HF_OSFXSR_MASK
;
933 env
->cr
[4] = new_cr4
;
936 #if defined(CONFIG_USER_ONLY)
938 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
939 int is_write
, int mmu_idx
, int is_softmmu
)
941 /* user mode only emulation */
944 env
->error_code
= (is_write
<< PG_ERROR_W_BIT
);
945 env
->error_code
|= PG_ERROR_U_MASK
;
946 env
->exception_index
= EXCP0E_PAGE
;
950 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
957 /* XXX: This value should match the one returned by CPUID
959 #if defined(CONFIG_KQEMU)
960 #define PHYS_ADDR_MASK 0xfffff000LL
962 # if defined(TARGET_X86_64)
963 # define PHYS_ADDR_MASK 0xfffffff000LL
965 # define PHYS_ADDR_MASK 0xffffff000LL
970 -1 = cannot handle fault
971 0 = nothing more to do
972 1 = generate PF fault
973 2 = soft MMU activation required for this block
975 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
976 int is_write1
, int mmu_idx
, int is_softmmu
)
979 target_ulong pde_addr
, pte_addr
;
980 int error_code
, is_dirty
, prot
, page_size
, ret
, is_write
, is_user
;
981 target_phys_addr_t paddr
;
982 uint32_t page_offset
;
983 target_ulong vaddr
, virt_addr
;
985 is_user
= mmu_idx
== MMU_USER_IDX
;
986 #if defined(DEBUG_MMU)
987 printf("MMU fault: addr=" TARGET_FMT_lx
" w=%d u=%d eip=" TARGET_FMT_lx
"\n",
988 addr
, is_write1
, is_user
, env
->eip
);
990 is_write
= is_write1
& 1;
992 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
994 virt_addr
= addr
& TARGET_PAGE_MASK
;
995 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1000 if (env
->cr
[4] & CR4_PAE_MASK
) {
1002 target_ulong pdpe_addr
;
1004 #ifdef TARGET_X86_64
1005 if (env
->hflags
& HF_LMA_MASK
) {
1006 uint64_t pml4e_addr
, pml4e
;
1009 /* test virtual address sign extension */
1010 sext
= (int64_t)addr
>> 47;
1011 if (sext
!= 0 && sext
!= -1) {
1012 env
->error_code
= 0;
1013 env
->exception_index
= EXCP0D_GPF
;
1017 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
1019 pml4e
= ldq_phys(pml4e_addr
);
1020 if (!(pml4e
& PG_PRESENT_MASK
)) {
1024 if (!(env
->efer
& MSR_EFER_NXE
) && (pml4e
& PG_NX_MASK
)) {
1025 error_code
= PG_ERROR_RSVD_MASK
;
1028 if (!(pml4e
& PG_ACCESSED_MASK
)) {
1029 pml4e
|= PG_ACCESSED_MASK
;
1030 stl_phys_notdirty(pml4e_addr
, pml4e
);
1032 ptep
= pml4e
^ PG_NX_MASK
;
1033 pdpe_addr
= ((pml4e
& PHYS_ADDR_MASK
) + (((addr
>> 30) & 0x1ff) << 3)) &
1035 pdpe
= ldq_phys(pdpe_addr
);
1036 if (!(pdpe
& PG_PRESENT_MASK
)) {
1040 if (!(env
->efer
& MSR_EFER_NXE
) && (pdpe
& PG_NX_MASK
)) {
1041 error_code
= PG_ERROR_RSVD_MASK
;
1044 ptep
&= pdpe
^ PG_NX_MASK
;
1045 if (!(pdpe
& PG_ACCESSED_MASK
)) {
1046 pdpe
|= PG_ACCESSED_MASK
;
1047 stl_phys_notdirty(pdpe_addr
, pdpe
);
1052 /* XXX: load them when cr3 is loaded ? */
1053 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
1055 pdpe
= ldq_phys(pdpe_addr
);
1056 if (!(pdpe
& PG_PRESENT_MASK
)) {
1060 ptep
= PG_NX_MASK
| PG_USER_MASK
| PG_RW_MASK
;
1063 pde_addr
= ((pdpe
& PHYS_ADDR_MASK
) + (((addr
>> 21) & 0x1ff) << 3)) &
1065 pde
= ldq_phys(pde_addr
);
1066 if (!(pde
& PG_PRESENT_MASK
)) {
1070 if (!(env
->efer
& MSR_EFER_NXE
) && (pde
& PG_NX_MASK
)) {
1071 error_code
= PG_ERROR_RSVD_MASK
;
1074 ptep
&= pde
^ PG_NX_MASK
;
1075 if (pde
& PG_PSE_MASK
) {
1077 page_size
= 2048 * 1024;
1079 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
1080 goto do_fault_protect
;
1082 if (!(ptep
& PG_USER_MASK
))
1083 goto do_fault_protect
;
1084 if (is_write
&& !(ptep
& PG_RW_MASK
))
1085 goto do_fault_protect
;
1087 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1088 is_write
&& !(ptep
& PG_RW_MASK
))
1089 goto do_fault_protect
;
1091 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
1092 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
1093 pde
|= PG_ACCESSED_MASK
;
1095 pde
|= PG_DIRTY_MASK
;
1096 stl_phys_notdirty(pde_addr
, pde
);
1098 /* align to page_size */
1099 pte
= pde
& ((PHYS_ADDR_MASK
& ~(page_size
- 1)) | 0xfff);
1100 virt_addr
= addr
& ~(page_size
- 1);
1103 if (!(pde
& PG_ACCESSED_MASK
)) {
1104 pde
|= PG_ACCESSED_MASK
;
1105 stl_phys_notdirty(pde_addr
, pde
);
1107 pte_addr
= ((pde
& PHYS_ADDR_MASK
) + (((addr
>> 12) & 0x1ff) << 3)) &
1109 pte
= ldq_phys(pte_addr
);
1110 if (!(pte
& PG_PRESENT_MASK
)) {
1114 if (!(env
->efer
& MSR_EFER_NXE
) && (pte
& PG_NX_MASK
)) {
1115 error_code
= PG_ERROR_RSVD_MASK
;
1118 /* combine pde and pte nx, user and rw protections */
1119 ptep
&= pte
^ PG_NX_MASK
;
1121 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
1122 goto do_fault_protect
;
1124 if (!(ptep
& PG_USER_MASK
))
1125 goto do_fault_protect
;
1126 if (is_write
&& !(ptep
& PG_RW_MASK
))
1127 goto do_fault_protect
;
1129 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1130 is_write
&& !(ptep
& PG_RW_MASK
))
1131 goto do_fault_protect
;
1133 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
1134 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
1135 pte
|= PG_ACCESSED_MASK
;
1137 pte
|= PG_DIRTY_MASK
;
1138 stl_phys_notdirty(pte_addr
, pte
);
1141 virt_addr
= addr
& ~0xfff;
1142 pte
= pte
& (PHYS_ADDR_MASK
| 0xfff);
1147 /* page directory entry */
1148 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) &
1150 pde
= ldl_phys(pde_addr
);
1151 if (!(pde
& PG_PRESENT_MASK
)) {
1155 /* if PSE bit is set, then we use a 4MB page */
1156 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1157 page_size
= 4096 * 1024;
1159 if (!(pde
& PG_USER_MASK
))
1160 goto do_fault_protect
;
1161 if (is_write
&& !(pde
& PG_RW_MASK
))
1162 goto do_fault_protect
;
1164 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1165 is_write
&& !(pde
& PG_RW_MASK
))
1166 goto do_fault_protect
;
1168 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
1169 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
1170 pde
|= PG_ACCESSED_MASK
;
1172 pde
|= PG_DIRTY_MASK
;
1173 stl_phys_notdirty(pde_addr
, pde
);
1176 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
1178 virt_addr
= addr
& ~(page_size
- 1);
1180 if (!(pde
& PG_ACCESSED_MASK
)) {
1181 pde
|= PG_ACCESSED_MASK
;
1182 stl_phys_notdirty(pde_addr
, pde
);
1185 /* page directory entry */
1186 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) &
1188 pte
= ldl_phys(pte_addr
);
1189 if (!(pte
& PG_PRESENT_MASK
)) {
1193 /* combine pde and pte user and rw protections */
1196 if (!(ptep
& PG_USER_MASK
))
1197 goto do_fault_protect
;
1198 if (is_write
&& !(ptep
& PG_RW_MASK
))
1199 goto do_fault_protect
;
1201 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1202 is_write
&& !(ptep
& PG_RW_MASK
))
1203 goto do_fault_protect
;
1205 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
1206 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
1207 pte
|= PG_ACCESSED_MASK
;
1209 pte
|= PG_DIRTY_MASK
;
1210 stl_phys_notdirty(pte_addr
, pte
);
1213 virt_addr
= addr
& ~0xfff;
1216 /* the page can be put in the TLB */
1218 if (!(ptep
& PG_NX_MASK
))
1220 if (pte
& PG_DIRTY_MASK
) {
1221 /* only set write access if already dirty... otherwise wait
1224 if (ptep
& PG_RW_MASK
)
1227 if (!(env
->cr
[0] & CR0_WP_MASK
) ||
1228 (ptep
& PG_RW_MASK
))
1233 pte
= pte
& env
->a20_mask
;
1235 /* Even if 4MB pages, we map only one 4KB page in the cache to
1236 avoid filling it too fast */
1237 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
1238 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
1239 vaddr
= virt_addr
+ page_offset
;
1241 ret
= tlb_set_page_exec(env
, vaddr
, paddr
, prot
, mmu_idx
, is_softmmu
);
1244 error_code
= PG_ERROR_P_MASK
;
1246 error_code
|= (is_write
<< PG_ERROR_W_BIT
);
1248 error_code
|= PG_ERROR_U_MASK
;
1249 if (is_write1
== 2 &&
1250 (env
->efer
& MSR_EFER_NXE
) &&
1251 (env
->cr
[4] & CR4_PAE_MASK
))
1252 error_code
|= PG_ERROR_I_D_MASK
;
1253 if (env
->intercept_exceptions
& (1 << EXCP0E_PAGE
)) {
1254 /* cr2 is not modified in case of exceptions */
1255 stq_phys(env
->vm_vmcb
+ offsetof(struct vmcb
, control
.exit_info_2
),
1260 env
->error_code
= error_code
;
1261 env
->exception_index
= EXCP0E_PAGE
;
1265 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
1267 target_ulong pde_addr
, pte_addr
;
1269 target_phys_addr_t paddr
;
1270 uint32_t page_offset
;
1273 if (env
->cr
[4] & CR4_PAE_MASK
) {
1274 target_ulong pdpe_addr
;
1277 #ifdef TARGET_X86_64
1278 if (env
->hflags
& HF_LMA_MASK
) {
1279 uint64_t pml4e_addr
, pml4e
;
1282 /* test virtual address sign extension */
1283 sext
= (int64_t)addr
>> 47;
1284 if (sext
!= 0 && sext
!= -1)
1287 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
1289 pml4e
= ldq_phys(pml4e_addr
);
1290 if (!(pml4e
& PG_PRESENT_MASK
))
1293 pdpe_addr
= ((pml4e
& ~0xfff) + (((addr
>> 30) & 0x1ff) << 3)) &
1295 pdpe
= ldq_phys(pdpe_addr
);
1296 if (!(pdpe
& PG_PRESENT_MASK
))
1301 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
1303 pdpe
= ldq_phys(pdpe_addr
);
1304 if (!(pdpe
& PG_PRESENT_MASK
))
1308 pde_addr
= ((pdpe
& ~0xfff) + (((addr
>> 21) & 0x1ff) << 3)) &
1310 pde
= ldq_phys(pde_addr
);
1311 if (!(pde
& PG_PRESENT_MASK
)) {
1314 if (pde
& PG_PSE_MASK
) {
1316 page_size
= 2048 * 1024;
1317 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
1320 pte_addr
= ((pde
& ~0xfff) + (((addr
>> 12) & 0x1ff) << 3)) &
1323 pte
= ldq_phys(pte_addr
);
1325 if (!(pte
& PG_PRESENT_MASK
))
1330 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1334 /* page directory entry */
1335 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) & env
->a20_mask
;
1336 pde
= ldl_phys(pde_addr
);
1337 if (!(pde
& PG_PRESENT_MASK
))
1339 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1340 pte
= pde
& ~0x003ff000; /* align to 4MB */
1341 page_size
= 4096 * 1024;
1343 /* page directory entry */
1344 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) & env
->a20_mask
;
1345 pte
= ldl_phys(pte_addr
);
1346 if (!(pte
& PG_PRESENT_MASK
))
1351 pte
= pte
& env
->a20_mask
;
1354 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
1355 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
1359 void hw_breakpoint_insert(CPUState
*env
, int index
)
1363 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
1365 if (hw_breakpoint_enabled(env
->dr
[7], index
))
1366 err
= cpu_breakpoint_insert(env
, env
->dr
[index
], BP_CPU
,
1367 &env
->cpu_breakpoint
[index
]);
1370 type
= BP_CPU
| BP_MEM_WRITE
;
1373 /* No support for I/O watchpoints yet */
1376 type
= BP_CPU
| BP_MEM_ACCESS
;
1378 err
= cpu_watchpoint_insert(env
, env
->dr
[index
],
1379 hw_breakpoint_len(env
->dr
[7], index
),
1380 type
, &env
->cpu_watchpoint
[index
]);
1384 env
->cpu_breakpoint
[index
] = NULL
;
1387 void hw_breakpoint_remove(CPUState
*env
, int index
)
1389 if (!env
->cpu_breakpoint
[index
])
1391 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
1393 if (hw_breakpoint_enabled(env
->dr
[7], index
))
1394 cpu_breakpoint_remove_by_ref(env
, env
->cpu_breakpoint
[index
]);
1398 cpu_watchpoint_remove_by_ref(env
, env
->cpu_watchpoint
[index
]);
1401 /* No support for I/O watchpoints yet */
1406 int check_hw_breakpoints(CPUState
*env
, int force_dr6_update
)
1410 int hit_enabled
= 0;
1412 dr6
= env
->dr
[6] & ~0xf;
1413 for (reg
= 0; reg
< 4; reg
++) {
1414 type
= hw_breakpoint_type(env
->dr
[7], reg
);
1415 if ((type
== 0 && env
->dr
[reg
] == env
->eip
) ||
1416 ((type
& 1) && env
->cpu_watchpoint
[reg
] &&
1417 (env
->cpu_watchpoint
[reg
]->flags
& BP_WATCHPOINT_HIT
))) {
1419 if (hw_breakpoint_enabled(env
->dr
[7], reg
))
1423 if (hit_enabled
|| force_dr6_update
)
1428 static CPUDebugExcpHandler
*prev_debug_excp_handler
;
1430 void raise_exception(int exception_index
);
1432 static void breakpoint_handler(CPUState
*env
)
1436 if (env
->watchpoint_hit
) {
1437 if (env
->watchpoint_hit
->flags
& BP_CPU
) {
1438 env
->watchpoint_hit
= NULL
;
1439 if (check_hw_breakpoints(env
, 0))
1440 raise_exception(EXCP01_DB
);
1442 cpu_resume_from_signal(env
, NULL
);
1445 TAILQ_FOREACH(bp
, &env
->breakpoints
, entry
)
1446 if (bp
->pc
== env
->eip
) {
1447 if (bp
->flags
& BP_CPU
) {
1448 check_hw_breakpoints(env
, 1);
1449 raise_exception(EXCP01_DB
);
1454 if (prev_debug_excp_handler
)
1455 prev_debug_excp_handler(env
);
1457 #endif /* !CONFIG_USER_ONLY */
1459 static void host_cpuid(uint32_t function
, uint32_t count
,
1460 uint32_t *eax
, uint32_t *ebx
,
1461 uint32_t *ecx
, uint32_t *edx
)
1463 #if defined(CONFIG_KVM)
1467 asm volatile("cpuid"
1468 : "=a"(vec
[0]), "=b"(vec
[1]),
1469 "=c"(vec
[2]), "=d"(vec
[3])
1470 : "0"(function
), "c"(count
) : "cc");
1472 asm volatile("pusha \n\t"
1474 "mov %%eax, 0(%2) \n\t"
1475 "mov %%ebx, 4(%2) \n\t"
1476 "mov %%ecx, 8(%2) \n\t"
1477 "mov %%edx, 12(%2) \n\t"
1479 : : "a"(function
), "c"(count
), "S"(vec
)
1494 void cpu_x86_cpuid(CPUX86State
*env
, uint32_t index
, uint32_t count
,
1495 uint32_t *eax
, uint32_t *ebx
,
1496 uint32_t *ecx
, uint32_t *edx
)
1498 /* test if maximum index reached */
1499 if (index
& 0x80000000) {
1500 if (index
> env
->cpuid_xlevel
)
1501 index
= env
->cpuid_level
;
1503 if (index
> env
->cpuid_level
)
1504 index
= env
->cpuid_level
;
1509 *eax
= env
->cpuid_level
;
1510 *ebx
= env
->cpuid_vendor1
;
1511 *edx
= env
->cpuid_vendor2
;
1512 *ecx
= env
->cpuid_vendor3
;
1514 /* sysenter isn't supported on compatibility mode on AMD. and syscall
1515 * isn't supported in compatibility mode on Intel. so advertise the
1516 * actuall cpu, and say goodbye to migration between different vendors
1517 * is you use compatibility mode. */
1518 if (kvm_enabled() && !env
->cpuid_vendor_override
)
1519 host_cpuid(0, 0, NULL
, ebx
, ecx
, edx
);
1522 *eax
= env
->cpuid_version
;
1523 *ebx
= (env
->cpuid_apic_id
<< 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1524 *ecx
= env
->cpuid_ext_features
;
1525 *edx
= env
->cpuid_features
;
1527 /* "Hypervisor present" bit required for Microsoft SVVP */
1532 /* cache info: needed for Pentium Pro compatibility */
1539 /* cache info: needed for Core compatibility */
1541 case 0: /* L1 dcache info */
1547 case 1: /* L1 icache info */
1553 case 2: /* L2 cache info */
1559 default: /* end of info */
1568 /* mwait info: needed for Core compatibility */
1569 *eax
= 0; /* Smallest monitor-line size in bytes */
1570 *ebx
= 0; /* Largest monitor-line size in bytes */
1571 *ecx
= CPUID_MWAIT_EMX
| CPUID_MWAIT_IBE
;
1575 /* Thermal and Power Leaf */
1582 /* Direct Cache Access Information Leaf */
1583 *eax
= 0; /* Bits 0-31 in DCA_CAP MSR */
1589 /* Architectural Performance Monitoring Leaf */
1596 *eax
= env
->cpuid_xlevel
;
1597 *ebx
= env
->cpuid_vendor1
;
1598 *edx
= env
->cpuid_vendor2
;
1599 *ecx
= env
->cpuid_vendor3
;
1602 *eax
= env
->cpuid_features
;
1604 *ecx
= env
->cpuid_ext3_features
;
1605 *edx
= env
->cpuid_ext2_features
;
1607 if (kvm_enabled()) {
1608 uint32_t h_eax
, h_edx
;
1610 host_cpuid(index
, 0, &h_eax
, NULL
, NULL
, &h_edx
);
1612 /* disable CPU features that the host does not support */
1615 if ((h_edx
& 0x20000000) == 0 /* || !lm_capable_kernel */)
1616 *edx
&= ~0x20000000;
1618 if ((h_edx
& 0x00000800) == 0)
1619 *edx
&= ~0x00000800;
1621 if ((h_edx
& 0x00100000) == 0)
1622 *edx
&= ~0x00100000;
1624 /* disable CPU features that KVM cannot support */
1629 *edx
&= ~0xc0000000;
1635 *eax
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 0];
1636 *ebx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 1];
1637 *ecx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 2];
1638 *edx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 3];
1641 /* cache info (L1 cache) */
1648 /* cache info (L2 cache) */
1655 /* virtual & phys address size in low 2 bytes. */
1656 /* XXX: This value must match the one used in the MMU code. */
1657 if (env
->cpuid_ext2_features
& CPUID_EXT2_LM
) {
1658 /* 64 bit processor */
1659 #if defined(CONFIG_KQEMU)
1660 *eax
= 0x00003020; /* 48 bits virtual, 32 bits physical */
1662 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1663 *eax
= 0x00003028; /* 48 bits virtual, 40 bits physical */
1666 #if defined(CONFIG_KQEMU)
1667 *eax
= 0x00000020; /* 32 bits physical */
1669 if (env
->cpuid_features
& CPUID_PSE36
)
1670 *eax
= 0x00000024; /* 36 bits physical */
1672 *eax
= 0x00000020; /* 32 bits physical */
1680 *eax
= 0x00000001; /* SVM Revision */
1681 *ebx
= 0x00000010; /* nr of ASIDs */
1683 *edx
= 0; /* optional features */
1686 /* reserved values: zero */
1695 CPUX86State
*cpu_x86_init(const char *cpu_model
)
1700 env
= qemu_mallocz(sizeof(CPUX86State
));
1702 env
->cpu_model_str
= cpu_model
;
1704 /* init various static tables */
1707 optimize_flags_init();
1708 #ifndef CONFIG_USER_ONLY
1709 prev_debug_excp_handler
=
1710 cpu_set_debug_excp_handler(breakpoint_handler
);
1713 if (cpu_x86_register(env
, cpu_model
) < 0) {
1722 qemu_init_vcpu(env
);
1724 if (kvm_enabled()) {
1725 kvm_trim_features(&env
->cpuid_features
,
1726 kvm_arch_get_supported_cpuid(env
, 1, R_EDX
),
1728 kvm_trim_features(&env
->cpuid_ext_features
,
1729 kvm_arch_get_supported_cpuid(env
, 1, R_ECX
),
1731 kvm_trim_features(&env
->cpuid_ext2_features
,
1732 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_EDX
),
1734 kvm_trim_features(&env
->cpuid_ext3_features
,
1735 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_ECX
),
1742 #if !defined(CONFIG_USER_ONLY)
1743 void do_cpu_init(CPUState
*env
)
1745 int sipi
= env
->interrupt_request
& CPU_INTERRUPT_SIPI
;
1747 env
->interrupt_request
= sipi
;
1748 apic_init_reset(env
);
1751 void do_cpu_sipi(CPUState
*env
)
1756 void do_cpu_init(CPUState
*env
)
1759 void do_cpu_sipi(CPUState
*env
)