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
;
121 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
122 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
123 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX)
124 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
125 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
126 CPUID_PSE36 | CPUID_FXSR)
127 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
128 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
129 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
130 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
131 CPUID_PAE | CPUID_SEP | CPUID_APIC)
132 static x86_def_t x86_defs
[] = {
137 .vendor1
= CPUID_VENDOR_AMD_1
,
138 .vendor2
= CPUID_VENDOR_AMD_2
,
139 .vendor3
= CPUID_VENDOR_AMD_3
,
143 .features
= PPRO_FEATURES
|
144 /* these features are needed for Win64 and aren't fully implemented */
145 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
146 /* this feature is needed for Solaris and isn't fully implemented */
148 .ext_features
= CPUID_EXT_SSE3
,
149 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) |
150 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
151 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
152 .ext3_features
= CPUID_EXT3_SVM
,
153 .xlevel
= 0x8000000A,
154 .model_id
= "QEMU Virtual CPU version " QEMU_VERSION
,
159 .vendor1
= CPUID_VENDOR_AMD_1
,
160 .vendor2
= CPUID_VENDOR_AMD_2
,
161 .vendor3
= CPUID_VENDOR_AMD_3
,
165 /* Missing: CPUID_VME, CPUID_HT */
166 .features
= PPRO_FEATURES
|
167 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
169 /* Missing: CPUID_EXT_CX16, CPUID_EXT_POPCNT */
170 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
,
171 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
172 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) |
173 CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
|
174 CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
| CPUID_EXT2_MMXEXT
|
176 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
177 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
178 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
179 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
180 .ext3_features
= CPUID_EXT3_SVM
,
181 .xlevel
= 0x8000001A,
182 .model_id
= "AMD Phenom(tm) 9550 Quad-Core Processor"
190 /* The original CPU also implements these features:
191 CPUID_VME, CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
192 CPUID_TM, CPUID_PBE */
193 .features
= PPRO_FEATURES
|
194 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
|
196 /* The original CPU also implements these ext features:
197 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
198 CPUID_EXT_TM2, CPUID_EXT_CX16, CPUID_EXT_XTPR, CPUID_EXT_PDCM */
199 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
| CPUID_EXT_SSSE3
,
200 .ext2_features
= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
,
201 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
202 .xlevel
= 0x80000008,
203 .model_id
= "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
212 .features
= PPRO_FEATURES
,
213 .ext_features
= CPUID_EXT_SSE3
,
215 .model_id
= "QEMU Virtual CPU version " QEMU_VERSION
,
223 /* The original CPU also implements these features:
224 CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
225 CPUID_TM, CPUID_PBE */
226 .features
= PPRO_FEATURES
| CPUID_VME
|
227 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
,
228 /* The original CPU also implements these ext features:
229 CPUID_EXT_VMX, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_XTPR,
231 .ext_features
= CPUID_EXT_SSE3
| CPUID_EXT_MONITOR
,
232 .ext2_features
= CPUID_EXT2_NX
,
233 .xlevel
= 0x80000008,
234 .model_id
= "Genuine Intel(R) CPU T2600 @ 2.16GHz",
242 .features
= I486_FEATURES
,
251 .features
= PENTIUM_FEATURES
,
260 .features
= PENTIUM2_FEATURES
,
269 .features
= PENTIUM3_FEATURES
,
275 .vendor1
= 0x68747541, /* "Auth" */
276 .vendor2
= 0x69746e65, /* "enti" */
277 .vendor3
= 0x444d4163, /* "cAMD" */
281 .features
= PPRO_FEATURES
| CPUID_PSE36
| CPUID_VME
| CPUID_MTRR
| CPUID_MCA
,
282 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) | CPUID_EXT2_MMXEXT
| CPUID_EXT2_3DNOW
| CPUID_EXT2_3DNOWEXT
,
283 .xlevel
= 0x80000008,
284 /* XXX: put another string ? */
285 .model_id
= "QEMU Virtual CPU version " QEMU_VERSION
,
289 /* original is on level 10 */
294 .features
= PPRO_FEATURES
|
295 CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
| CPUID_VME
,
296 /* Missing: CPUID_DTS | CPUID_ACPI | CPUID_SS |
297 * CPUID_HT | CPUID_TM | CPUID_PBE */
298 /* Some CPUs got no CPUID_SEP */
299 .ext_features
= CPUID_EXT_MONITOR
|
300 CPUID_EXT_SSE3
/* PNI */ | CPUID_EXT_SSSE3
,
301 /* Missing: CPUID_EXT_DSCPL | CPUID_EXT_EST |
302 * CPUID_EXT_TM2 | CPUID_EXT_XTPR */
303 .ext2_features
= (PPRO_FEATURES
& 0x0183F3FF) | CPUID_EXT2_NX
,
304 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
305 .xlevel
= 0x8000000A,
306 .model_id
= "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
310 static int cpu_x86_find_by_name(x86_def_t
*x86_cpu_def
, const char *cpu_model
)
315 char *s
= strdup(cpu_model
);
316 char *featurestr
, *name
= strtok(s
, ",");
317 uint32_t plus_features
= 0, plus_ext_features
= 0, plus_ext2_features
= 0, plus_ext3_features
= 0;
318 uint32_t minus_features
= 0, minus_ext_features
= 0, minus_ext2_features
= 0, minus_ext3_features
= 0;
319 int family
= -1, model
= -1, stepping
= -1;
322 for (i
= 0; i
< ARRAY_SIZE(x86_defs
); i
++) {
323 if (strcmp(name
, x86_defs
[i
].name
) == 0) {
330 memcpy(x86_cpu_def
, def
, sizeof(*def
));
332 featurestr
= strtok(NULL
, ",");
336 if (featurestr
[0] == '+') {
337 add_flagname_to_bitmaps(featurestr
+ 1, &plus_features
, &plus_ext_features
, &plus_ext2_features
, &plus_ext3_features
);
338 } else if (featurestr
[0] == '-') {
339 add_flagname_to_bitmaps(featurestr
+ 1, &minus_features
, &minus_ext_features
, &minus_ext2_features
, &minus_ext3_features
);
340 } else if ((val
= strchr(featurestr
, '='))) {
342 if (!strcmp(featurestr
, "family")) {
344 family
= strtol(val
, &err
, 10);
345 if (!*val
|| *err
|| family
< 0) {
346 fprintf(stderr
, "bad numerical value %s\n", val
);
349 x86_cpu_def
->family
= family
;
350 } else if (!strcmp(featurestr
, "model")) {
352 model
= strtol(val
, &err
, 10);
353 if (!*val
|| *err
|| model
< 0 || model
> 0xff) {
354 fprintf(stderr
, "bad numerical value %s\n", val
);
357 x86_cpu_def
->model
= model
;
358 } else if (!strcmp(featurestr
, "stepping")) {
360 stepping
= strtol(val
, &err
, 10);
361 if (!*val
|| *err
|| stepping
< 0 || stepping
> 0xf) {
362 fprintf(stderr
, "bad numerical value %s\n", val
);
365 x86_cpu_def
->stepping
= stepping
;
366 } else if (!strcmp(featurestr
, "vendor")) {
367 if (strlen(val
) != 12) {
368 fprintf(stderr
, "vendor string must be 12 chars long\n");
371 x86_cpu_def
->vendor1
= 0;
372 x86_cpu_def
->vendor2
= 0;
373 x86_cpu_def
->vendor3
= 0;
374 for(i
= 0; i
< 4; i
++) {
375 x86_cpu_def
->vendor1
|= ((uint8_t)val
[i
]) << (8 * i
);
376 x86_cpu_def
->vendor2
|= ((uint8_t)val
[i
+ 4]) << (8 * i
);
377 x86_cpu_def
->vendor3
|= ((uint8_t)val
[i
+ 8]) << (8 * i
);
379 } else if (!strcmp(featurestr
, "model_id")) {
380 pstrcpy(x86_cpu_def
->model_id
, sizeof(x86_cpu_def
->model_id
),
383 fprintf(stderr
, "unrecognized feature %s\n", featurestr
);
387 fprintf(stderr
, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr
);
390 featurestr
= strtok(NULL
, ",");
392 x86_cpu_def
->features
|= plus_features
;
393 x86_cpu_def
->ext_features
|= plus_ext_features
;
394 x86_cpu_def
->ext2_features
|= plus_ext2_features
;
395 x86_cpu_def
->ext3_features
|= plus_ext3_features
;
396 x86_cpu_def
->features
&= ~minus_features
;
397 x86_cpu_def
->ext_features
&= ~minus_ext_features
;
398 x86_cpu_def
->ext2_features
&= ~minus_ext2_features
;
399 x86_cpu_def
->ext3_features
&= ~minus_ext3_features
;
408 void x86_cpu_list (FILE *f
, int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...))
412 for (i
= 0; i
< ARRAY_SIZE(x86_defs
); i
++)
413 (*cpu_fprintf
)(f
, "x86 %16s\n", x86_defs
[i
].name
);
416 static int cpu_x86_register (CPUX86State
*env
, const char *cpu_model
)
418 x86_def_t def1
, *def
= &def1
;
420 if (cpu_x86_find_by_name(def
, cpu_model
) < 0)
423 env
->cpuid_vendor1
= def
->vendor1
;
424 env
->cpuid_vendor2
= def
->vendor2
;
425 env
->cpuid_vendor3
= def
->vendor3
;
427 env
->cpuid_vendor1
= CPUID_VENDOR_INTEL_1
;
428 env
->cpuid_vendor2
= CPUID_VENDOR_INTEL_2
;
429 env
->cpuid_vendor3
= CPUID_VENDOR_INTEL_3
;
431 env
->cpuid_level
= def
->level
;
432 if (def
->family
> 0x0f)
433 env
->cpuid_version
= 0xf00 | ((def
->family
- 0x0f) << 20);
435 env
->cpuid_version
= def
->family
<< 8;
436 env
->cpuid_version
|= ((def
->model
& 0xf) << 4) | ((def
->model
>> 4) << 16);
437 env
->cpuid_version
|= def
->stepping
;
438 env
->cpuid_features
= def
->features
;
439 env
->pat
= 0x0007040600070406ULL
;
440 env
->cpuid_ext_features
= def
->ext_features
;
441 env
->cpuid_ext2_features
= def
->ext2_features
;
442 env
->cpuid_xlevel
= def
->xlevel
;
443 env
->cpuid_ext3_features
= def
->ext3_features
;
445 const char *model_id
= def
->model_id
;
449 len
= strlen(model_id
);
450 for(i
= 0; i
< 48; i
++) {
454 c
= (uint8_t)model_id
[i
];
455 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
461 /* NOTE: must be called outside the CPU execute loop */
462 void cpu_reset(CPUX86State
*env
)
466 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
467 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
468 log_cpu_state(env
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
471 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
475 env
->old_exception
= -1;
477 /* init to reset state */
479 #ifdef CONFIG_SOFTMMU
480 env
->hflags
|= HF_SOFTMMU_MASK
;
482 env
->hflags2
|= HF2_GIF_MASK
;
484 cpu_x86_update_cr0(env
, 0x60000010);
485 env
->a20_mask
= ~0x0;
486 env
->smbase
= 0x30000;
488 env
->idt
.limit
= 0xffff;
489 env
->gdt
.limit
= 0xffff;
490 env
->ldt
.limit
= 0xffff;
491 env
->ldt
.flags
= DESC_P_MASK
| (2 << DESC_TYPE_SHIFT
);
492 env
->tr
.limit
= 0xffff;
493 env
->tr
.flags
= DESC_P_MASK
| (11 << DESC_TYPE_SHIFT
);
495 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff,
496 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
| DESC_R_MASK
);
497 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff,
498 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
);
499 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff,
500 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
);
501 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff,
502 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
);
503 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff,
504 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
);
505 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff,
506 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
);
509 env
->regs
[R_EDX
] = env
->cpuid_version
;
514 for(i
= 0;i
< 8; i
++)
520 memset(env
->dr
, 0, sizeof(env
->dr
));
521 env
->dr
[6] = DR6_FIXED_1
;
522 env
->dr
[7] = DR7_FIXED_1
;
523 cpu_breakpoint_remove_all(env
, BP_CPU
);
524 cpu_watchpoint_remove_all(env
, BP_CPU
);
527 void cpu_x86_close(CPUX86State
*env
)
532 /***********************************************************/
535 static const char *cc_op_str
[] = {
591 cpu_x86_dump_seg_cache(CPUState
*env
, FILE *f
,
592 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
593 const char *name
, struct SegmentCache
*sc
)
596 if (env
->hflags
& HF_CS64_MASK
) {
597 cpu_fprintf(f
, "%-3s=%04x %016" PRIx64
" %08x %08x", name
,
598 sc
->selector
, sc
->base
, sc
->limit
, sc
->flags
);
602 cpu_fprintf(f
, "%-3s=%04x %08x %08x %08x", name
, sc
->selector
,
603 (uint32_t)sc
->base
, sc
->limit
, sc
->flags
);
606 if (!(env
->hflags
& HF_PE_MASK
) || !(sc
->flags
& DESC_P_MASK
))
609 cpu_fprintf(f
, " DPL=%d ", (sc
->flags
& DESC_DPL_MASK
) >> DESC_DPL_SHIFT
);
610 if (sc
->flags
& DESC_S_MASK
) {
611 if (sc
->flags
& DESC_CS_MASK
) {
612 cpu_fprintf(f
, (sc
->flags
& DESC_L_MASK
) ? "CS64" :
613 ((sc
->flags
& DESC_B_MASK
) ? "CS32" : "CS16"));
614 cpu_fprintf(f
, " [%c%c", (sc
->flags
& DESC_C_MASK
) ? 'C' : '-',
615 (sc
->flags
& DESC_R_MASK
) ? 'R' : '-');
617 cpu_fprintf(f
, (sc
->flags
& DESC_B_MASK
) ? "DS " : "DS16");
618 cpu_fprintf(f
, " [%c%c", (sc
->flags
& DESC_E_MASK
) ? 'E' : '-',
619 (sc
->flags
& DESC_W_MASK
) ? 'W' : '-');
621 cpu_fprintf(f
, "%c]", (sc
->flags
& DESC_A_MASK
) ? 'A' : '-');
623 static const char *sys_type_name
[2][16] = {
625 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
626 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
627 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
628 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
631 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
632 "Reserved", "Reserved", "Reserved", "Reserved",
633 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
634 "Reserved", "IntGate64", "TrapGate64"
637 cpu_fprintf(f
, sys_type_name
[(env
->hflags
& HF_LMA_MASK
) ? 1 : 0]
638 [(sc
->flags
& DESC_TYPE_MASK
)
639 >> DESC_TYPE_SHIFT
]);
642 cpu_fprintf(f
, "\n");
645 void cpu_dump_state(CPUState
*env
, FILE *f
,
646 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
651 static const char *seg_name
[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
654 kvm_arch_get_registers(env
);
656 eflags
= env
->eflags
;
658 if (env
->hflags
& HF_CS64_MASK
) {
660 "RAX=%016" PRIx64
" RBX=%016" PRIx64
" RCX=%016" PRIx64
" RDX=%016" PRIx64
"\n"
661 "RSI=%016" PRIx64
" RDI=%016" PRIx64
" RBP=%016" PRIx64
" RSP=%016" PRIx64
"\n"
662 "R8 =%016" PRIx64
" R9 =%016" PRIx64
" R10=%016" PRIx64
" R11=%016" PRIx64
"\n"
663 "R12=%016" PRIx64
" R13=%016" PRIx64
" R14=%016" PRIx64
" R15=%016" PRIx64
"\n"
664 "RIP=%016" PRIx64
" RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
682 eflags
& DF_MASK
? 'D' : '-',
683 eflags
& CC_O
? 'O' : '-',
684 eflags
& CC_S
? 'S' : '-',
685 eflags
& CC_Z
? 'Z' : '-',
686 eflags
& CC_A
? 'A' : '-',
687 eflags
& CC_P
? 'P' : '-',
688 eflags
& CC_C
? 'C' : '-',
689 env
->hflags
& HF_CPL_MASK
,
690 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
691 (int)(env
->a20_mask
>> 20) & 1,
692 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
697 cpu_fprintf(f
, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
698 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
699 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
700 (uint32_t)env
->regs
[R_EAX
],
701 (uint32_t)env
->regs
[R_EBX
],
702 (uint32_t)env
->regs
[R_ECX
],
703 (uint32_t)env
->regs
[R_EDX
],
704 (uint32_t)env
->regs
[R_ESI
],
705 (uint32_t)env
->regs
[R_EDI
],
706 (uint32_t)env
->regs
[R_EBP
],
707 (uint32_t)env
->regs
[R_ESP
],
708 (uint32_t)env
->eip
, eflags
,
709 eflags
& DF_MASK
? 'D' : '-',
710 eflags
& CC_O
? 'O' : '-',
711 eflags
& CC_S
? 'S' : '-',
712 eflags
& CC_Z
? 'Z' : '-',
713 eflags
& CC_A
? 'A' : '-',
714 eflags
& CC_P
? 'P' : '-',
715 eflags
& CC_C
? 'C' : '-',
716 env
->hflags
& HF_CPL_MASK
,
717 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
718 (int)(env
->a20_mask
>> 20) & 1,
719 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
723 for(i
= 0; i
< 6; i
++) {
724 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, seg_name
[i
],
727 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, "LDT", &env
->ldt
);
728 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, "TR", &env
->tr
);
731 if (env
->hflags
& HF_LMA_MASK
) {
732 cpu_fprintf(f
, "GDT= %016" PRIx64
" %08x\n",
733 env
->gdt
.base
, env
->gdt
.limit
);
734 cpu_fprintf(f
, "IDT= %016" PRIx64
" %08x\n",
735 env
->idt
.base
, env
->idt
.limit
);
736 cpu_fprintf(f
, "CR0=%08x CR2=%016" PRIx64
" CR3=%016" PRIx64
" CR4=%08x\n",
737 (uint32_t)env
->cr
[0],
740 (uint32_t)env
->cr
[4]);
741 for(i
= 0; i
< 4; i
++)
742 cpu_fprintf(f
, "DR%d=%016" PRIx64
" ", i
, env
->dr
[i
]);
743 cpu_fprintf(f
, "\nDR6=%016" PRIx64
" DR7=%016" PRIx64
"\n",
744 env
->dr
[6], env
->dr
[7]);
748 cpu_fprintf(f
, "GDT= %08x %08x\n",
749 (uint32_t)env
->gdt
.base
, env
->gdt
.limit
);
750 cpu_fprintf(f
, "IDT= %08x %08x\n",
751 (uint32_t)env
->idt
.base
, env
->idt
.limit
);
752 cpu_fprintf(f
, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
753 (uint32_t)env
->cr
[0],
754 (uint32_t)env
->cr
[2],
755 (uint32_t)env
->cr
[3],
756 (uint32_t)env
->cr
[4]);
757 for(i
= 0; i
< 4; i
++)
758 cpu_fprintf(f
, "DR%d=%08x ", i
, env
->dr
[i
]);
759 cpu_fprintf(f
, "\nDR6=%08x DR7=%08x\n", env
->dr
[6], env
->dr
[7]);
761 if (flags
& X86_DUMP_CCOP
) {
762 if ((unsigned)env
->cc_op
< CC_OP_NB
)
763 snprintf(cc_op_name
, sizeof(cc_op_name
), "%s", cc_op_str
[env
->cc_op
]);
765 snprintf(cc_op_name
, sizeof(cc_op_name
), "[%d]", env
->cc_op
);
767 if (env
->hflags
& HF_CS64_MASK
) {
768 cpu_fprintf(f
, "CCS=%016" PRIx64
" CCD=%016" PRIx64
" CCO=%-8s\n",
769 env
->cc_src
, env
->cc_dst
,
774 cpu_fprintf(f
, "CCS=%08x CCD=%08x CCO=%-8s\n",
775 (uint32_t)env
->cc_src
, (uint32_t)env
->cc_dst
,
779 if (flags
& X86_DUMP_FPU
) {
782 for(i
= 0; i
< 8; i
++) {
783 fptag
|= ((!env
->fptags
[i
]) << i
);
785 cpu_fprintf(f
, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
787 (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11,
792 #if defined(USE_X86LDOUBLE)
800 tmp
.d
= env
->fpregs
[i
].d
;
801 cpu_fprintf(f
, "FPR%d=%016" PRIx64
" %04x",
802 i
, tmp
.l
.lower
, tmp
.l
.upper
);
804 cpu_fprintf(f
, "FPR%d=%016" PRIx64
,
805 i
, env
->fpregs
[i
].mmx
.q
);
808 cpu_fprintf(f
, "\n");
812 if (env
->hflags
& HF_CS64_MASK
)
817 cpu_fprintf(f
, "XMM%02d=%08x%08x%08x%08x",
819 env
->xmm_regs
[i
].XMM_L(3),
820 env
->xmm_regs
[i
].XMM_L(2),
821 env
->xmm_regs
[i
].XMM_L(1),
822 env
->xmm_regs
[i
].XMM_L(0));
824 cpu_fprintf(f
, "\n");
831 /***********************************************************/
833 /* XXX: add PGE support */
835 void cpu_x86_set_a20(CPUX86State
*env
, int a20_state
)
837 a20_state
= (a20_state
!= 0);
838 if (a20_state
!= ((env
->a20_mask
>> 20) & 1)) {
839 #if defined(DEBUG_MMU)
840 printf("A20 update: a20=%d\n", a20_state
);
842 /* if the cpu is currently executing code, we must unlink it and
843 all the potentially executing TB */
844 cpu_interrupt(env
, CPU_INTERRUPT_EXITTB
);
846 /* when a20 is changed, all the MMU mappings are invalid, so
847 we must flush everything */
849 env
->a20_mask
= (~0x100000) | (a20_state
<< 20);
853 void cpu_x86_update_cr0(CPUX86State
*env
, uint32_t new_cr0
)
857 #if defined(DEBUG_MMU)
858 printf("CR0 update: CR0=0x%08x\n", new_cr0
);
860 if ((new_cr0
& (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
)) !=
861 (env
->cr
[0] & (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
))) {
866 if (!(env
->cr
[0] & CR0_PG_MASK
) && (new_cr0
& CR0_PG_MASK
) &&
867 (env
->efer
& MSR_EFER_LME
)) {
868 /* enter in long mode */
869 /* XXX: generate an exception */
870 if (!(env
->cr
[4] & CR4_PAE_MASK
))
872 env
->efer
|= MSR_EFER_LMA
;
873 env
->hflags
|= HF_LMA_MASK
;
874 } else if ((env
->cr
[0] & CR0_PG_MASK
) && !(new_cr0
& CR0_PG_MASK
) &&
875 (env
->efer
& MSR_EFER_LMA
)) {
877 env
->efer
&= ~MSR_EFER_LMA
;
878 env
->hflags
&= ~(HF_LMA_MASK
| HF_CS64_MASK
);
879 env
->eip
&= 0xffffffff;
882 env
->cr
[0] = new_cr0
| CR0_ET_MASK
;
884 /* update PE flag in hidden flags */
885 pe_state
= (env
->cr
[0] & CR0_PE_MASK
);
886 env
->hflags
= (env
->hflags
& ~HF_PE_MASK
) | (pe_state
<< HF_PE_SHIFT
);
887 /* ensure that ADDSEG is always set in real mode */
888 env
->hflags
|= ((pe_state
^ 1) << HF_ADDSEG_SHIFT
);
889 /* update FPU flags */
890 env
->hflags
= (env
->hflags
& ~(HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
)) |
891 ((new_cr0
<< (HF_MP_SHIFT
- 1)) & (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
));
894 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
896 void cpu_x86_update_cr3(CPUX86State
*env
, target_ulong new_cr3
)
898 env
->cr
[3] = new_cr3
;
899 if (env
->cr
[0] & CR0_PG_MASK
) {
900 #if defined(DEBUG_MMU)
901 printf("CR3 update: CR3=" TARGET_FMT_lx
"\n", new_cr3
);
907 void cpu_x86_update_cr4(CPUX86State
*env
, uint32_t new_cr4
)
909 #if defined(DEBUG_MMU)
910 printf("CR4 update: CR4=%08x\n", (uint32_t)env
->cr
[4]);
912 if ((new_cr4
& (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
)) !=
913 (env
->cr
[4] & (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
))) {
917 if (!(env
->cpuid_features
& CPUID_SSE
))
918 new_cr4
&= ~CR4_OSFXSR_MASK
;
919 if (new_cr4
& CR4_OSFXSR_MASK
)
920 env
->hflags
|= HF_OSFXSR_MASK
;
922 env
->hflags
&= ~HF_OSFXSR_MASK
;
924 env
->cr
[4] = new_cr4
;
927 #if defined(CONFIG_USER_ONLY)
929 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
930 int is_write
, int mmu_idx
, int is_softmmu
)
932 /* user mode only emulation */
935 env
->error_code
= (is_write
<< PG_ERROR_W_BIT
);
936 env
->error_code
|= PG_ERROR_U_MASK
;
937 env
->exception_index
= EXCP0E_PAGE
;
941 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
948 /* XXX: This value should match the one returned by CPUID
950 #if defined(CONFIG_KQEMU)
951 #define PHYS_ADDR_MASK 0xfffff000LL
953 # if defined(TARGET_X86_64)
954 # define PHYS_ADDR_MASK 0xfffffff000LL
956 # define PHYS_ADDR_MASK 0xffffff000LL
961 -1 = cannot handle fault
962 0 = nothing more to do
963 1 = generate PF fault
964 2 = soft MMU activation required for this block
966 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
967 int is_write1
, int mmu_idx
, int is_softmmu
)
970 target_ulong pde_addr
, pte_addr
;
971 int error_code
, is_dirty
, prot
, page_size
, ret
, is_write
, is_user
;
972 target_phys_addr_t paddr
;
973 uint32_t page_offset
;
974 target_ulong vaddr
, virt_addr
;
976 is_user
= mmu_idx
== MMU_USER_IDX
;
977 #if defined(DEBUG_MMU)
978 printf("MMU fault: addr=" TARGET_FMT_lx
" w=%d u=%d eip=" TARGET_FMT_lx
"\n",
979 addr
, is_write1
, is_user
, env
->eip
);
981 is_write
= is_write1
& 1;
983 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
985 virt_addr
= addr
& TARGET_PAGE_MASK
;
986 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
991 if (env
->cr
[4] & CR4_PAE_MASK
) {
993 target_ulong pdpe_addr
;
996 if (env
->hflags
& HF_LMA_MASK
) {
997 uint64_t pml4e_addr
, pml4e
;
1000 /* test virtual address sign extension */
1001 sext
= (int64_t)addr
>> 47;
1002 if (sext
!= 0 && sext
!= -1) {
1003 env
->error_code
= 0;
1004 env
->exception_index
= EXCP0D_GPF
;
1008 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
1010 pml4e
= ldq_phys(pml4e_addr
);
1011 if (!(pml4e
& PG_PRESENT_MASK
)) {
1015 if (!(env
->efer
& MSR_EFER_NXE
) && (pml4e
& PG_NX_MASK
)) {
1016 error_code
= PG_ERROR_RSVD_MASK
;
1019 if (!(pml4e
& PG_ACCESSED_MASK
)) {
1020 pml4e
|= PG_ACCESSED_MASK
;
1021 stl_phys_notdirty(pml4e_addr
, pml4e
);
1023 ptep
= pml4e
^ PG_NX_MASK
;
1024 pdpe_addr
= ((pml4e
& PHYS_ADDR_MASK
) + (((addr
>> 30) & 0x1ff) << 3)) &
1026 pdpe
= ldq_phys(pdpe_addr
);
1027 if (!(pdpe
& PG_PRESENT_MASK
)) {
1031 if (!(env
->efer
& MSR_EFER_NXE
) && (pdpe
& PG_NX_MASK
)) {
1032 error_code
= PG_ERROR_RSVD_MASK
;
1035 ptep
&= pdpe
^ PG_NX_MASK
;
1036 if (!(pdpe
& PG_ACCESSED_MASK
)) {
1037 pdpe
|= PG_ACCESSED_MASK
;
1038 stl_phys_notdirty(pdpe_addr
, pdpe
);
1043 /* XXX: load them when cr3 is loaded ? */
1044 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
1046 pdpe
= ldq_phys(pdpe_addr
);
1047 if (!(pdpe
& PG_PRESENT_MASK
)) {
1051 ptep
= PG_NX_MASK
| PG_USER_MASK
| PG_RW_MASK
;
1054 pde_addr
= ((pdpe
& PHYS_ADDR_MASK
) + (((addr
>> 21) & 0x1ff) << 3)) &
1056 pde
= ldq_phys(pde_addr
);
1057 if (!(pde
& PG_PRESENT_MASK
)) {
1061 if (!(env
->efer
& MSR_EFER_NXE
) && (pde
& PG_NX_MASK
)) {
1062 error_code
= PG_ERROR_RSVD_MASK
;
1065 ptep
&= pde
^ PG_NX_MASK
;
1066 if (pde
& PG_PSE_MASK
) {
1068 page_size
= 2048 * 1024;
1070 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
1071 goto do_fault_protect
;
1073 if (!(ptep
& PG_USER_MASK
))
1074 goto do_fault_protect
;
1075 if (is_write
&& !(ptep
& PG_RW_MASK
))
1076 goto do_fault_protect
;
1078 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1079 is_write
&& !(ptep
& PG_RW_MASK
))
1080 goto do_fault_protect
;
1082 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
1083 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
1084 pde
|= PG_ACCESSED_MASK
;
1086 pde
|= PG_DIRTY_MASK
;
1087 stl_phys_notdirty(pde_addr
, pde
);
1089 /* align to page_size */
1090 pte
= pde
& ((PHYS_ADDR_MASK
& ~(page_size
- 1)) | 0xfff);
1091 virt_addr
= addr
& ~(page_size
- 1);
1094 if (!(pde
& PG_ACCESSED_MASK
)) {
1095 pde
|= PG_ACCESSED_MASK
;
1096 stl_phys_notdirty(pde_addr
, pde
);
1098 pte_addr
= ((pde
& PHYS_ADDR_MASK
) + (((addr
>> 12) & 0x1ff) << 3)) &
1100 pte
= ldq_phys(pte_addr
);
1101 if (!(pte
& PG_PRESENT_MASK
)) {
1105 if (!(env
->efer
& MSR_EFER_NXE
) && (pte
& PG_NX_MASK
)) {
1106 error_code
= PG_ERROR_RSVD_MASK
;
1109 /* combine pde and pte nx, user and rw protections */
1110 ptep
&= pte
^ PG_NX_MASK
;
1112 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
1113 goto do_fault_protect
;
1115 if (!(ptep
& PG_USER_MASK
))
1116 goto do_fault_protect
;
1117 if (is_write
&& !(ptep
& PG_RW_MASK
))
1118 goto do_fault_protect
;
1120 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1121 is_write
&& !(ptep
& PG_RW_MASK
))
1122 goto do_fault_protect
;
1124 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
1125 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
1126 pte
|= PG_ACCESSED_MASK
;
1128 pte
|= PG_DIRTY_MASK
;
1129 stl_phys_notdirty(pte_addr
, pte
);
1132 virt_addr
= addr
& ~0xfff;
1133 pte
= pte
& (PHYS_ADDR_MASK
| 0xfff);
1138 /* page directory entry */
1139 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) &
1141 pde
= ldl_phys(pde_addr
);
1142 if (!(pde
& PG_PRESENT_MASK
)) {
1146 /* if PSE bit is set, then we use a 4MB page */
1147 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1148 page_size
= 4096 * 1024;
1150 if (!(pde
& PG_USER_MASK
))
1151 goto do_fault_protect
;
1152 if (is_write
&& !(pde
& PG_RW_MASK
))
1153 goto do_fault_protect
;
1155 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1156 is_write
&& !(pde
& PG_RW_MASK
))
1157 goto do_fault_protect
;
1159 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
1160 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
1161 pde
|= PG_ACCESSED_MASK
;
1163 pde
|= PG_DIRTY_MASK
;
1164 stl_phys_notdirty(pde_addr
, pde
);
1167 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
1169 virt_addr
= addr
& ~(page_size
- 1);
1171 if (!(pde
& PG_ACCESSED_MASK
)) {
1172 pde
|= PG_ACCESSED_MASK
;
1173 stl_phys_notdirty(pde_addr
, pde
);
1176 /* page directory entry */
1177 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) &
1179 pte
= ldl_phys(pte_addr
);
1180 if (!(pte
& PG_PRESENT_MASK
)) {
1184 /* combine pde and pte user and rw protections */
1187 if (!(ptep
& PG_USER_MASK
))
1188 goto do_fault_protect
;
1189 if (is_write
&& !(ptep
& PG_RW_MASK
))
1190 goto do_fault_protect
;
1192 if ((env
->cr
[0] & CR0_WP_MASK
) &&
1193 is_write
&& !(ptep
& PG_RW_MASK
))
1194 goto do_fault_protect
;
1196 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
1197 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
1198 pte
|= PG_ACCESSED_MASK
;
1200 pte
|= PG_DIRTY_MASK
;
1201 stl_phys_notdirty(pte_addr
, pte
);
1204 virt_addr
= addr
& ~0xfff;
1207 /* the page can be put in the TLB */
1209 if (!(ptep
& PG_NX_MASK
))
1211 if (pte
& PG_DIRTY_MASK
) {
1212 /* only set write access if already dirty... otherwise wait
1215 if (ptep
& PG_RW_MASK
)
1218 if (!(env
->cr
[0] & CR0_WP_MASK
) ||
1219 (ptep
& PG_RW_MASK
))
1224 pte
= pte
& env
->a20_mask
;
1226 /* Even if 4MB pages, we map only one 4KB page in the cache to
1227 avoid filling it too fast */
1228 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
1229 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
1230 vaddr
= virt_addr
+ page_offset
;
1232 ret
= tlb_set_page_exec(env
, vaddr
, paddr
, prot
, mmu_idx
, is_softmmu
);
1235 error_code
= PG_ERROR_P_MASK
;
1237 error_code
|= (is_write
<< PG_ERROR_W_BIT
);
1239 error_code
|= PG_ERROR_U_MASK
;
1240 if (is_write1
== 2 &&
1241 (env
->efer
& MSR_EFER_NXE
) &&
1242 (env
->cr
[4] & CR4_PAE_MASK
))
1243 error_code
|= PG_ERROR_I_D_MASK
;
1244 if (env
->intercept_exceptions
& (1 << EXCP0E_PAGE
)) {
1245 /* cr2 is not modified in case of exceptions */
1246 stq_phys(env
->vm_vmcb
+ offsetof(struct vmcb
, control
.exit_info_2
),
1251 env
->error_code
= error_code
;
1252 env
->exception_index
= EXCP0E_PAGE
;
1256 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
1258 target_ulong pde_addr
, pte_addr
;
1260 target_phys_addr_t paddr
;
1261 uint32_t page_offset
;
1264 if (env
->cr
[4] & CR4_PAE_MASK
) {
1265 target_ulong pdpe_addr
;
1268 #ifdef TARGET_X86_64
1269 if (env
->hflags
& HF_LMA_MASK
) {
1270 uint64_t pml4e_addr
, pml4e
;
1273 /* test virtual address sign extension */
1274 sext
= (int64_t)addr
>> 47;
1275 if (sext
!= 0 && sext
!= -1)
1278 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
1280 pml4e
= ldq_phys(pml4e_addr
);
1281 if (!(pml4e
& PG_PRESENT_MASK
))
1284 pdpe_addr
= ((pml4e
& ~0xfff) + (((addr
>> 30) & 0x1ff) << 3)) &
1286 pdpe
= ldq_phys(pdpe_addr
);
1287 if (!(pdpe
& PG_PRESENT_MASK
))
1292 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
1294 pdpe
= ldq_phys(pdpe_addr
);
1295 if (!(pdpe
& PG_PRESENT_MASK
))
1299 pde_addr
= ((pdpe
& ~0xfff) + (((addr
>> 21) & 0x1ff) << 3)) &
1301 pde
= ldq_phys(pde_addr
);
1302 if (!(pde
& PG_PRESENT_MASK
)) {
1305 if (pde
& PG_PSE_MASK
) {
1307 page_size
= 2048 * 1024;
1308 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
1311 pte_addr
= ((pde
& ~0xfff) + (((addr
>> 12) & 0x1ff) << 3)) &
1314 pte
= ldq_phys(pte_addr
);
1316 if (!(pte
& PG_PRESENT_MASK
))
1321 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1325 /* page directory entry */
1326 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) & env
->a20_mask
;
1327 pde
= ldl_phys(pde_addr
);
1328 if (!(pde
& PG_PRESENT_MASK
))
1330 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1331 pte
= pde
& ~0x003ff000; /* align to 4MB */
1332 page_size
= 4096 * 1024;
1334 /* page directory entry */
1335 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) & env
->a20_mask
;
1336 pte
= ldl_phys(pte_addr
);
1337 if (!(pte
& PG_PRESENT_MASK
))
1342 pte
= pte
& env
->a20_mask
;
1345 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
1346 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
1350 void hw_breakpoint_insert(CPUState
*env
, int index
)
1354 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
1356 if (hw_breakpoint_enabled(env
->dr
[7], index
))
1357 err
= cpu_breakpoint_insert(env
, env
->dr
[index
], BP_CPU
,
1358 &env
->cpu_breakpoint
[index
]);
1361 type
= BP_CPU
| BP_MEM_WRITE
;
1364 /* No support for I/O watchpoints yet */
1367 type
= BP_CPU
| BP_MEM_ACCESS
;
1369 err
= cpu_watchpoint_insert(env
, env
->dr
[index
],
1370 hw_breakpoint_len(env
->dr
[7], index
),
1371 type
, &env
->cpu_watchpoint
[index
]);
1375 env
->cpu_breakpoint
[index
] = NULL
;
1378 void hw_breakpoint_remove(CPUState
*env
, int index
)
1380 if (!env
->cpu_breakpoint
[index
])
1382 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
1384 if (hw_breakpoint_enabled(env
->dr
[7], index
))
1385 cpu_breakpoint_remove_by_ref(env
, env
->cpu_breakpoint
[index
]);
1389 cpu_watchpoint_remove_by_ref(env
, env
->cpu_watchpoint
[index
]);
1392 /* No support for I/O watchpoints yet */
1397 int check_hw_breakpoints(CPUState
*env
, int force_dr6_update
)
1401 int hit_enabled
= 0;
1403 dr6
= env
->dr
[6] & ~0xf;
1404 for (reg
= 0; reg
< 4; reg
++) {
1405 type
= hw_breakpoint_type(env
->dr
[7], reg
);
1406 if ((type
== 0 && env
->dr
[reg
] == env
->eip
) ||
1407 ((type
& 1) && env
->cpu_watchpoint
[reg
] &&
1408 (env
->cpu_watchpoint
[reg
]->flags
& BP_WATCHPOINT_HIT
))) {
1410 if (hw_breakpoint_enabled(env
->dr
[7], reg
))
1414 if (hit_enabled
|| force_dr6_update
)
1419 static CPUDebugExcpHandler
*prev_debug_excp_handler
;
1421 void raise_exception(int exception_index
);
1423 static void breakpoint_handler(CPUState
*env
)
1427 if (env
->watchpoint_hit
) {
1428 if (env
->watchpoint_hit
->flags
& BP_CPU
) {
1429 env
->watchpoint_hit
= NULL
;
1430 if (check_hw_breakpoints(env
, 0))
1431 raise_exception(EXCP01_DB
);
1433 cpu_resume_from_signal(env
, NULL
);
1436 TAILQ_FOREACH(bp
, &env
->breakpoints
, entry
)
1437 if (bp
->pc
== env
->eip
) {
1438 if (bp
->flags
& BP_CPU
) {
1439 check_hw_breakpoints(env
, 1);
1440 raise_exception(EXCP01_DB
);
1445 if (prev_debug_excp_handler
)
1446 prev_debug_excp_handler(env
);
1448 #endif /* !CONFIG_USER_ONLY */
1450 static void host_cpuid(uint32_t function
, uint32_t count
,
1451 uint32_t *eax
, uint32_t *ebx
,
1452 uint32_t *ecx
, uint32_t *edx
)
1454 #if defined(CONFIG_KVM)
1458 asm volatile("cpuid"
1459 : "=a"(vec
[0]), "=b"(vec
[1]),
1460 "=c"(vec
[2]), "=d"(vec
[3])
1461 : "0"(function
), "c"(count
) : "cc");
1463 asm volatile("pusha \n\t"
1465 "mov %%eax, 0(%2) \n\t"
1466 "mov %%ebx, 4(%2) \n\t"
1467 "mov %%ecx, 8(%2) \n\t"
1468 "mov %%edx, 12(%2) \n\t"
1470 : : "a"(function
), "c"(count
), "S"(vec
)
1485 void cpu_x86_cpuid(CPUX86State
*env
, uint32_t index
, uint32_t count
,
1486 uint32_t *eax
, uint32_t *ebx
,
1487 uint32_t *ecx
, uint32_t *edx
)
1489 /* test if maximum index reached */
1490 if (index
& 0x80000000) {
1491 if (index
> env
->cpuid_xlevel
)
1492 index
= env
->cpuid_level
;
1494 if (index
> env
->cpuid_level
)
1495 index
= env
->cpuid_level
;
1500 *eax
= env
->cpuid_level
;
1501 *ebx
= env
->cpuid_vendor1
;
1502 *edx
= env
->cpuid_vendor2
;
1503 *ecx
= env
->cpuid_vendor3
;
1505 /* sysenter isn't supported on compatibility mode on AMD. and syscall
1506 * isn't supported in compatibility mode on Intel. so advertise the
1507 * actuall cpu, and say goodbye to migration between different vendors
1508 * is you use compatibility mode. */
1510 host_cpuid(0, 0, NULL
, ebx
, ecx
, edx
);
1513 *eax
= env
->cpuid_version
;
1514 *ebx
= (env
->cpuid_apic_id
<< 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1515 *ecx
= env
->cpuid_ext_features
;
1516 *edx
= env
->cpuid_features
;
1518 /* "Hypervisor present" bit required for Microsoft SVVP */
1523 /* cache info: needed for Pentium Pro compatibility */
1530 /* cache info: needed for Core compatibility */
1532 case 0: /* L1 dcache info */
1538 case 1: /* L1 icache info */
1544 case 2: /* L2 cache info */
1550 default: /* end of info */
1559 /* mwait info: needed for Core compatibility */
1560 *eax
= 0; /* Smallest monitor-line size in bytes */
1561 *ebx
= 0; /* Largest monitor-line size in bytes */
1562 *ecx
= CPUID_MWAIT_EMX
| CPUID_MWAIT_IBE
;
1566 /* Thermal and Power Leaf */
1573 /* Direct Cache Access Information Leaf */
1574 *eax
= 0; /* Bits 0-31 in DCA_CAP MSR */
1580 /* Architectural Performance Monitoring Leaf */
1587 *eax
= env
->cpuid_xlevel
;
1588 *ebx
= env
->cpuid_vendor1
;
1589 *edx
= env
->cpuid_vendor2
;
1590 *ecx
= env
->cpuid_vendor3
;
1593 *eax
= env
->cpuid_features
;
1595 *ecx
= env
->cpuid_ext3_features
;
1596 *edx
= env
->cpuid_ext2_features
;
1598 if (kvm_enabled()) {
1599 uint32_t h_eax
, h_edx
;
1601 host_cpuid(index
, 0, &h_eax
, NULL
, NULL
, &h_edx
);
1603 /* disable CPU features that the host does not support */
1606 if ((h_edx
& 0x20000000) == 0 /* || !lm_capable_kernel */)
1607 *edx
&= ~0x20000000;
1609 if ((h_edx
& 0x00000800) == 0)
1610 *edx
&= ~0x00000800;
1612 if ((h_edx
& 0x00100000) == 0)
1613 *edx
&= ~0x00100000;
1615 /* disable CPU features that KVM cannot support */
1620 *edx
&= ~0xc0000000;
1626 *eax
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 0];
1627 *ebx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 1];
1628 *ecx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 2];
1629 *edx
= env
->cpuid_model
[(index
- 0x80000002) * 4 + 3];
1632 /* cache info (L1 cache) */
1639 /* cache info (L2 cache) */
1646 /* virtual & phys address size in low 2 bytes. */
1647 /* XXX: This value must match the one used in the MMU code. */
1648 if (env
->cpuid_ext2_features
& CPUID_EXT2_LM
) {
1649 /* 64 bit processor */
1650 #if defined(CONFIG_KQEMU)
1651 *eax
= 0x00003020; /* 48 bits virtual, 32 bits physical */
1653 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1654 *eax
= 0x00003028; /* 48 bits virtual, 40 bits physical */
1657 #if defined(CONFIG_KQEMU)
1658 *eax
= 0x00000020; /* 32 bits physical */
1660 if (env
->cpuid_features
& CPUID_PSE36
)
1661 *eax
= 0x00000024; /* 36 bits physical */
1663 *eax
= 0x00000020; /* 32 bits physical */
1671 *eax
= 0x00000001; /* SVM Revision */
1672 *ebx
= 0x00000010; /* nr of ASIDs */
1674 *edx
= 0; /* optional features */
1677 /* reserved values: zero */
1686 CPUX86State
*cpu_x86_init(const char *cpu_model
)
1691 env
= qemu_mallocz(sizeof(CPUX86State
));
1693 env
->cpu_model_str
= cpu_model
;
1695 /* init various static tables */
1698 optimize_flags_init();
1699 #ifndef CONFIG_USER_ONLY
1700 prev_debug_excp_handler
=
1701 cpu_set_debug_excp_handler(breakpoint_handler
);
1704 if (cpu_x86_register(env
, cpu_model
) < 0) {
1713 qemu_init_vcpu(env
);
1715 if (kvm_enabled()) {
1716 kvm_trim_features(&env
->cpuid_features
,
1717 kvm_arch_get_supported_cpuid(env
, 1, R_EDX
),
1719 kvm_trim_features(&env
->cpuid_ext_features
,
1720 kvm_arch_get_supported_cpuid(env
, 1, R_ECX
),
1722 kvm_trim_features(&env
->cpuid_ext2_features
,
1723 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_EDX
),
1725 kvm_trim_features(&env
->cpuid_ext3_features
,
1726 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_ECX
),