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, "hypervisor",
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(const 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 typedef struct x86_def_t {
98 uint32_t vendor1, vendor2, vendor3;
102 uint32_t features, ext_features, ext2_features, ext3_features;
108 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
109 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
110 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX)
111 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
112 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
113 CPUID_PSE36 | CPUID_FXSR)
114 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
115 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
116 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
117 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
118 CPUID_PAE | CPUID_SEP | CPUID_APIC)
119 static x86_def_t x86_defs[] = {
124 .vendor1 = CPUID_VENDOR_AMD_1,
125 .vendor2 = CPUID_VENDOR_AMD_2,
126 .vendor3 = CPUID_VENDOR_AMD_3,
130 .features = PPRO_FEATURES |
131 /* these features are needed for Win64 and aren't fully implemented */
132 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
133 /* this feature is needed for Solaris and isn't fully implemented */
135 .ext_features = CPUID_EXT_SSE3,
136 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
137 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
138 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
139 .ext3_features = CPUID_EXT3_SVM,
140 .xlevel = 0x8000000A,
141 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
146 .vendor1 = CPUID_VENDOR_AMD_1,
147 .vendor2 = CPUID_VENDOR_AMD_2,
148 .vendor3 = CPUID_VENDOR_AMD_3,
152 /* Missing: CPUID_VME, CPUID_HT */
153 .features = PPRO_FEATURES |
154 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
156 /* Missing: CPUID_EXT_CX16, CPUID_EXT_POPCNT */
157 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR,
158 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
159 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
160 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
161 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
163 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
164 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
165 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
166 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
167 .ext3_features = CPUID_EXT3_SVM,
168 .xlevel = 0x8000001A,
169 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
177 /* The original CPU also implements these features:
178 CPUID_VME, CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
179 CPUID_TM, CPUID_PBE */
180 .features = PPRO_FEATURES |
181 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
183 /* The original CPU also implements these ext features:
184 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
185 CPUID_EXT_TM2, CPUID_EXT_CX16, CPUID_EXT_XTPR, CPUID_EXT_PDCM */
186 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3,
187 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
188 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
189 .xlevel = 0x80000008,
190 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
199 .features = PPRO_FEATURES,
200 .ext_features = CPUID_EXT_SSE3,
202 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
210 /* The original CPU also implements these features:
211 CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
212 CPUID_TM, CPUID_PBE */
213 .features = PPRO_FEATURES | CPUID_VME |
214 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA,
215 /* The original CPU also implements these ext features:
216 CPUID_EXT_VMX, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_XTPR,
218 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR,
219 .ext2_features = CPUID_EXT2_NX,
220 .xlevel = 0x80000008,
221 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
229 .features = I486_FEATURES,
238 .features = PENTIUM_FEATURES,
247 .features = PENTIUM2_FEATURES,
256 .features = PENTIUM3_FEATURES,
262 .vendor1 = CPUID_VENDOR_AMD_1,
263 .vendor2 = CPUID_VENDOR_AMD_2,
264 .vendor3 = CPUID_VENDOR_AMD_3,
268 .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR | CPUID_MCA,
269 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) | CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
270 .xlevel = 0x80000008,
271 /* XXX: put another string ? */
272 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
276 /* original is on level 10 */
281 .features = PPRO_FEATURES |
282 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME,
283 /* Missing: CPUID_DTS | CPUID_ACPI | CPUID_SS |
284 * CPUID_HT | CPUID_TM | CPUID_PBE */
285 /* Some CPUs got no CPUID_SEP */
286 .ext_features = CPUID_EXT_MONITOR |
287 CPUID_EXT_SSE3 /* PNI */ | CPUID_EXT_SSSE3,
288 /* Missing: CPUID_EXT_DSCPL | CPUID_EXT_EST |
289 * CPUID_EXT_TM2 | CPUID_EXT_XTPR */
290 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) | CPUID_EXT2_NX,
291 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
292 .xlevel = 0x8000000A,
293 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
297 static void host_cpuid(uint32_t function, uint32_t count, uint32_t *eax,
298 uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
300 static int cpu_x86_fill_model_id(char *str)
302 uint32_t eax, ebx, ecx, edx;
305 for (i = 0; i < 3; i++) {
306 host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx);
307 memcpy(str + i * 16 + 0, &eax, 4);
308 memcpy(str + i * 16 + 4, &ebx, 4);
309 memcpy(str + i * 16 + 8, &ecx, 4);
310 memcpy(str + i * 16 + 12, &edx, 4);
315 static int cpu_x86_fill_host(x86_def_t *x86_cpu_def)
317 uint32_t eax, ebx, ecx, edx;
319 x86_cpu_def->name = "host";
320 host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
321 x86_cpu_def->level = eax;
322 x86_cpu_def->vendor1 = ebx;
323 x86_cpu_def->vendor2 = edx;
324 x86_cpu_def->vendor3 = ecx;
326 host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
327 x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
328 x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
329 x86_cpu_def->stepping = eax & 0x0F;
330 x86_cpu_def->ext_features = ecx;
331 x86_cpu_def->features = edx;
333 host_cpuid(0x80000000, 0, &eax, &ebx, &ecx, &edx);
334 x86_cpu_def->xlevel = eax;
336 host_cpuid(0x80000001, 0, &eax, &ebx, &ecx, &edx);
337 x86_cpu_def->ext2_features = edx;
338 x86_cpu_def->ext3_features = ecx;
339 cpu_x86_fill_model_id(x86_cpu_def->model_id);
340 x86_cpu_def->vendor_override = 0;
345 static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
350 char *s = strdup(cpu_model);
351 char *featurestr, *name = strtok(s, ",");
352 uint32_t plus_features = 0, plus_ext_features = 0, plus_ext2_features = 0, plus_ext3_features = 0;
353 uint32_t minus_features = 0, minus_ext_features = 0, minus_ext2_features = 0, minus_ext3_features = 0;
354 int family = -1, model = -1, stepping = -1;
357 for (i = 0; i < ARRAY_SIZE(x86_defs); i++) {
358 if (strcmp(name, x86_defs[i].name) == 0) {
364 if (strcmp(name, "host") != 0) {
367 cpu_x86_fill_host(x86_cpu_def);
369 memcpy(x86_cpu_def, def, sizeof(*def));
372 add_flagname_to_bitmaps("hypervisor", &plus_features,
373 &plus_ext_features, &plus_ext2_features, &plus_ext3_features);
375 featurestr = strtok(NULL, ",");
379 if (featurestr[0] == '+') {
380 add_flagname_to_bitmaps(featurestr + 1, &plus_features, &plus_ext_features, &plus_ext2_features, &plus_ext3_features);
381 } else if (featurestr[0] == '-') {
382 add_flagname_to_bitmaps(featurestr + 1, &minus_features, &minus_ext_features, &minus_ext2_features, &minus_ext3_features);
383 } else if ((val = strchr(featurestr, '='))) {
385 if (!strcmp(featurestr, "family")) {
387 family = strtol(val, &err, 10);
388 if (!*val || *err || family < 0) {
389 fprintf(stderr, "bad numerical value %s\n", val);
392 x86_cpu_def->family = family;
393 } else if (!strcmp(featurestr, "model")) {
395 model = strtol(val, &err, 10);
396 if (!*val || *err || model < 0 || model > 0xff) {
397 fprintf(stderr, "bad numerical value %s\n", val);
400 x86_cpu_def->model = model;
401 } else if (!strcmp(featurestr, "stepping")) {
403 stepping = strtol(val, &err, 10);
404 if (!*val || *err || stepping < 0 || stepping > 0xf) {
405 fprintf(stderr, "bad numerical value %s\n", val);
408 x86_cpu_def->stepping = stepping;
409 } else if (!strcmp(featurestr, "vendor")) {
410 if (strlen(val) != 12) {
411 fprintf(stderr, "vendor string must be 12 chars long\n");
414 x86_cpu_def->vendor1 = 0;
415 x86_cpu_def->vendor2 = 0;
416 x86_cpu_def->vendor3 = 0;
417 for(i = 0; i < 4; i++) {
418 x86_cpu_def->vendor1 |= ((uint8_t)val[i ]) << (8 * i);
419 x86_cpu_def->vendor2 |= ((uint8_t)val[i + 4]) << (8 * i);
420 x86_cpu_def->vendor3 |= ((uint8_t)val[i + 8]) << (8 * i);
422 x86_cpu_def->vendor_override = 1;
423 } else if (!strcmp(featurestr, "model_id")) {
424 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
427 fprintf(stderr, "unrecognized feature %s\n", featurestr);
431 fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
434 featurestr = strtok(NULL, ",");
436 x86_cpu_def->features |= plus_features;
437 x86_cpu_def->ext_features |= plus_ext_features;
438 x86_cpu_def->ext2_features |= plus_ext2_features;
439 x86_cpu_def->ext3_features |= plus_ext3_features;
440 x86_cpu_def->features &= ~minus_features;
441 x86_cpu_def->ext_features &= ~minus_ext_features;
442 x86_cpu_def->ext2_features &= ~minus_ext2_features;
443 x86_cpu_def->ext3_features &= ~minus_ext3_features;
452 void x86_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
456 for (i = 0; i < ARRAY_SIZE(x86_defs); i++)
457 (*cpu_fprintf)(f, "x86 %16s\n", x86_defs[i].name);
460 static int cpu_x86_register (CPUX86State *env, const char *cpu_model)
462 x86_def_t def1, *def = &def1;
464 if (cpu_x86_find_by_name(def, cpu_model) < 0)
467 env->cpuid_vendor1 = def->vendor1;
468 env->cpuid_vendor2 = def->vendor2;
469 env->cpuid_vendor3 = def->vendor3;
471 env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1;
472 env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2;
473 env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3;
475 env->cpuid_vendor_override = def->vendor_override;
476 env->cpuid_level = def->level;
477 if (def->family > 0x0f)
478 env->cpuid_version = 0xf00 | ((def->family - 0x0f) << 20);
480 env->cpuid_version = def->family << 8;
481 env->cpuid_version |= ((def->model & 0xf) << 4) | ((def->model >> 4) << 16);
482 env->cpuid_version |= def->stepping;
483 env->cpuid_features = def->features;
484 env->pat = 0x0007040600070406ULL;
485 env->cpuid_ext_features = def->ext_features;
486 env->cpuid_ext2_features = def->ext2_features;
487 env->cpuid_xlevel = def->xlevel;
488 env->cpuid_ext3_features = def->ext3_features;
490 const char *model_id = def->model_id;
494 len = strlen(model_id);
495 for(i = 0; i < 48; i++) {
499 c = (uint8_t)model_id[i];
500 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
506 /* NOTE: must be called outside the CPU execute loop */
507 void cpu_reset(CPUX86State *env)
511 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
512 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
513 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
516 memset(env, 0, offsetof(CPUX86State, breakpoints));
520 env->old_exception = -1;
522 /* init to reset state */
524 #ifdef CONFIG_SOFTMMU
525 env->hflags |= HF_SOFTMMU_MASK;
527 env->hflags2 |= HF2_GIF_MASK;
529 cpu_x86_update_cr0(env, 0x60000010);
530 env->a20_mask = ~0x0;
531 env->smbase = 0x30000;
533 env->idt.limit = 0xffff;
534 env->gdt.limit = 0xffff;
535 env->ldt.limit = 0xffff;
536 env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
537 env->tr.limit = 0xffff;
538 env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
540 cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
541 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK |
542 DESC_R_MASK | DESC_A_MASK);
543 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
544 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
546 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
547 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
549 cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
550 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
552 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
553 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
555 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
556 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
560 env->regs[R_EDX] = env->cpuid_version;
565 for(i = 0;i < 8; i++)
571 memset(env->dr, 0, sizeof(env->dr));
572 env->dr[6] = DR6_FIXED_1;
573 env->dr[7] = DR7_FIXED_1;
574 cpu_breakpoint_remove_all(env, BP_CPU);
575 cpu_watchpoint_remove_all(env, BP_CPU);
578 void cpu_x86_close(CPUX86State *env)
583 /***********************************************************/
586 static const char *cc_op_str[] = {
642 cpu_x86_dump_seg_cache(CPUState *env, FILE *f,
643 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
644 const char *name, struct SegmentCache *sc)
647 if (env->hflags & HF_CS64_MASK) {
648 cpu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name,
649 sc->selector, sc->base, sc->limit, sc->flags);
653 cpu_fprintf(f, "%-3s=%04x %08x %08x %08x", name, sc->selector,
654 (uint32_t)sc->base, sc->limit, sc->flags);
657 if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK))
660 cpu_fprintf(f, " DPL=%d ", (sc->flags & DESC_DPL_MASK) >> DESC_DPL_SHIFT);
661 if (sc->flags & DESC_S_MASK) {
662 if (sc->flags & DESC_CS_MASK) {
663 cpu_fprintf(f, (sc->flags & DESC_L_MASK) ? "CS64" :
664 ((sc->flags & DESC_B_MASK) ? "CS32" : "CS16"));
665 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_C_MASK) ? 'C' : '-',
666 (sc->flags & DESC_R_MASK) ? 'R' : '-');
668 cpu_fprintf(f, (sc->flags & DESC_B_MASK) ? "DS " : "DS16");
669 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_E_MASK) ? 'E' : '-',
670 (sc->flags & DESC_W_MASK) ? 'W' : '-');
672 cpu_fprintf(f, "%c]", (sc->flags & DESC_A_MASK) ? 'A' : '-');
674 static const char *sys_type_name[2][16] = {
676 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
677 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
678 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
679 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
682 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
683 "Reserved", "Reserved", "Reserved", "Reserved",
684 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
685 "Reserved", "IntGate64", "TrapGate64"
688 cpu_fprintf(f, sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0]
689 [(sc->flags & DESC_TYPE_MASK)
690 >> DESC_TYPE_SHIFT]);
693 cpu_fprintf(f, "\n");
696 void cpu_dump_state(CPUState *env, FILE *f,
697 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
702 static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
705 kvm_arch_get_registers(env);
707 eflags = env->eflags;
709 if (env->hflags & HF_CS64_MASK) {
711 "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n"
712 "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n"
713 "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n"
714 "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n"
715 "RIP=%016" PRIx64 " RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
733 eflags & DF_MASK ? 'D' : '-',
734 eflags & CC_O ? 'O' : '-',
735 eflags & CC_S ? 'S' : '-',
736 eflags & CC_Z ? 'Z' : '-',
737 eflags & CC_A ? 'A' : '-',
738 eflags & CC_P ? 'P' : '-',
739 eflags & CC_C ? 'C' : '-',
740 env->hflags & HF_CPL_MASK,
741 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
742 (int)(env->a20_mask >> 20) & 1,
743 (env->hflags >> HF_SMM_SHIFT) & 1,
748 cpu_fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
749 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
750 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
751 (uint32_t)env->regs[R_EAX],
752 (uint32_t)env->regs[R_EBX],
753 (uint32_t)env->regs[R_ECX],
754 (uint32_t)env->regs[R_EDX],
755 (uint32_t)env->regs[R_ESI],
756 (uint32_t)env->regs[R_EDI],
757 (uint32_t)env->regs[R_EBP],
758 (uint32_t)env->regs[R_ESP],
759 (uint32_t)env->eip, eflags,
760 eflags & DF_MASK ? 'D' : '-',
761 eflags & CC_O ? 'O' : '-',
762 eflags & CC_S ? 'S' : '-',
763 eflags & CC_Z ? 'Z' : '-',
764 eflags & CC_A ? 'A' : '-',
765 eflags & CC_P ? 'P' : '-',
766 eflags & CC_C ? 'C' : '-',
767 env->hflags & HF_CPL_MASK,
768 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
769 (int)(env->a20_mask >> 20) & 1,
770 (env->hflags >> HF_SMM_SHIFT) & 1,
774 for(i = 0; i < 6; i++) {
775 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, seg_name[i],
778 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "LDT", &env->ldt);
779 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "TR", &env->tr);
782 if (env->hflags & HF_LMA_MASK) {
783 cpu_fprintf(f, "GDT= %016" PRIx64 " %08x\n",
784 env->gdt.base, env->gdt.limit);
785 cpu_fprintf(f, "IDT= %016" PRIx64 " %08x\n",
786 env->idt.base, env->idt.limit);
787 cpu_fprintf(f, "CR0=%08x CR2=%016" PRIx64 " CR3=%016" PRIx64 " CR4=%08x\n",
788 (uint32_t)env->cr[0],
791 (uint32_t)env->cr[4]);
792 for(i = 0; i < 4; i++)
793 cpu_fprintf(f, "DR%d=%016" PRIx64 " ", i, env->dr[i]);
794 cpu_fprintf(f, "\nDR6=%016" PRIx64 " DR7=%016" PRIx64 "\n",
795 env->dr[6], env->dr[7]);
799 cpu_fprintf(f, "GDT= %08x %08x\n",
800 (uint32_t)env->gdt.base, env->gdt.limit);
801 cpu_fprintf(f, "IDT= %08x %08x\n",
802 (uint32_t)env->idt.base, env->idt.limit);
803 cpu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
804 (uint32_t)env->cr[0],
805 (uint32_t)env->cr[2],
806 (uint32_t)env->cr[3],
807 (uint32_t)env->cr[4]);
808 for(i = 0; i < 4; i++)
809 cpu_fprintf(f, "DR%d=%08x ", i, env->dr[i]);
810 cpu_fprintf(f, "\nDR6=%08x DR7=%08x\n", env->dr[6], env->dr[7]);
812 if (flags & X86_DUMP_CCOP) {
813 if ((unsigned)env->cc_op < CC_OP_NB)
814 snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]);
816 snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
818 if (env->hflags & HF_CS64_MASK) {
819 cpu_fprintf(f, "CCS=%016" PRIx64 " CCD=%016" PRIx64 " CCO=%-8s\n",
820 env->cc_src, env->cc_dst,
825 cpu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
826 (uint32_t)env->cc_src, (uint32_t)env->cc_dst,
830 if (flags & X86_DUMP_FPU) {
833 for(i = 0; i < 8; i++) {
834 fptag |= ((!env->fptags[i]) << i);
836 cpu_fprintf(f, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
838 (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11,
843 #if defined(USE_X86LDOUBLE)
851 tmp.d = env->fpregs[i].d;
852 cpu_fprintf(f, "FPR%d=%016" PRIx64 " %04x",
853 i, tmp.l.lower, tmp.l.upper);
855 cpu_fprintf(f, "FPR%d=%016" PRIx64,
856 i, env->fpregs[i].mmx.q);
859 cpu_fprintf(f, "\n");
863 if (env->hflags & HF_CS64_MASK)
868 cpu_fprintf(f, "XMM%02d=%08x%08x%08x%08x",
870 env->xmm_regs[i].XMM_L(3),
871 env->xmm_regs[i].XMM_L(2),
872 env->xmm_regs[i].XMM_L(1),
873 env->xmm_regs[i].XMM_L(0));
875 cpu_fprintf(f, "\n");
882 /***********************************************************/
884 /* XXX: add PGE support */
886 void cpu_x86_set_a20(CPUX86State *env, int a20_state)
888 a20_state = (a20_state != 0);
889 if (a20_state != ((env->a20_mask >> 20) & 1)) {
890 #if defined(DEBUG_MMU)
891 printf("A20 update: a20=%d\n", a20_state);
893 /* if the cpu is currently executing code, we must unlink it and
894 all the potentially executing TB */
895 cpu_interrupt(env, CPU_INTERRUPT_EXITTB);
897 /* when a20 is changed, all the MMU mappings are invalid, so
898 we must flush everything */
900 env->a20_mask = (~0x100000) | (a20_state << 20);
904 void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
908 #if defined(DEBUG_MMU)
909 printf("CR0 update: CR0=0x%08x\n", new_cr0);
911 if ((new_cr0 & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK)) !=
912 (env->cr[0] & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK))) {
917 if (!(env->cr[0] & CR0_PG_MASK) && (new_cr0 & CR0_PG_MASK) &&
918 (env->efer & MSR_EFER_LME)) {
919 /* enter in long mode */
920 /* XXX: generate an exception */
921 if (!(env->cr[4] & CR4_PAE_MASK))
923 env->efer |= MSR_EFER_LMA;
924 env->hflags |= HF_LMA_MASK;
925 } else if ((env->cr[0] & CR0_PG_MASK) && !(new_cr0 & CR0_PG_MASK) &&
926 (env->efer & MSR_EFER_LMA)) {
928 env->efer &= ~MSR_EFER_LMA;
929 env->hflags &= ~(HF_LMA_MASK | HF_CS64_MASK);
930 env->eip &= 0xffffffff;
933 env->cr[0] = new_cr0 | CR0_ET_MASK;
935 /* update PE flag in hidden flags */
936 pe_state = (env->cr[0] & CR0_PE_MASK);
937 env->hflags = (env->hflags & ~HF_PE_MASK) | (pe_state << HF_PE_SHIFT);
938 /* ensure that ADDSEG is always set in real mode */
939 env->hflags |= ((pe_state ^ 1) << HF_ADDSEG_SHIFT);
940 /* update FPU flags */
941 env->hflags = (env->hflags & ~(HF_MP_MASK | HF_EM_MASK | HF_TS_MASK)) |
942 ((new_cr0 << (HF_MP_SHIFT - 1)) & (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK));
945 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
947 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
949 env->cr[3] = new_cr3;
950 if (env->cr[0] & CR0_PG_MASK) {
951 #if defined(DEBUG_MMU)
952 printf("CR3 update: CR3=" TARGET_FMT_lx "\n", new_cr3);
958 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
960 #if defined(DEBUG_MMU)
961 printf("CR4 update: CR4=%08x\n", (uint32_t)env->cr[4]);
963 if ((new_cr4 & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK)) !=
964 (env->cr[4] & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK))) {
968 if (!(env->cpuid_features & CPUID_SSE))
969 new_cr4 &= ~CR4_OSFXSR_MASK;
970 if (new_cr4 & CR4_OSFXSR_MASK)
971 env->hflags |= HF_OSFXSR_MASK;
973 env->hflags &= ~HF_OSFXSR_MASK;
975 env->cr[4] = new_cr4;
978 #if defined(CONFIG_USER_ONLY)
980 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
981 int is_write, int mmu_idx, int is_softmmu)
983 /* user mode only emulation */
986 env->error_code = (is_write << PG_ERROR_W_BIT);
987 env->error_code |= PG_ERROR_U_MASK;
988 env->exception_index = EXCP0E_PAGE;
992 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
999 /* XXX: This value should match the one returned by CPUID
1001 #if defined(CONFIG_KQEMU)
1002 #define PHYS_ADDR_MASK 0xfffff000LL
1004 # if defined(TARGET_X86_64)
1005 # define PHYS_ADDR_MASK 0xfffffff000LL
1007 # define PHYS_ADDR_MASK 0xffffff000LL
1012 -1 = cannot handle fault
1013 0 = nothing more to do
1014 1 = generate PF fault
1015 2 = soft MMU activation required for this block
1017 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
1018 int is_write1, int mmu_idx, int is_softmmu)
1021 target_ulong pde_addr, pte_addr;
1022 int error_code, is_dirty, prot, page_size, ret, is_write, is_user;
1023 target_phys_addr_t paddr;
1024 uint32_t page_offset;
1025 target_ulong vaddr, virt_addr;
1027 is_user = mmu_idx == MMU_USER_IDX;
1028 #if defined(DEBUG_MMU)
1029 printf("MMU fault: addr=" TARGET_FMT_lx " w=%d u=%d eip=" TARGET_FMT_lx "\n",
1030 addr, is_write1, is_user, env->eip);
1032 is_write = is_write1 & 1;
1034 if (!(env->cr[0] & CR0_PG_MASK)) {
1036 virt_addr = addr & TARGET_PAGE_MASK;
1037 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1042 if (env->cr[4] & CR4_PAE_MASK) {
1044 target_ulong pdpe_addr;
1046 #ifdef TARGET_X86_64
1047 if (env->hflags & HF_LMA_MASK) {
1048 uint64_t pml4e_addr, pml4e;
1051 /* test virtual address sign extension */
1052 sext = (int64_t)addr >> 47;
1053 if (sext != 0 && sext != -1) {
1054 env->error_code = 0;
1055 env->exception_index = EXCP0D_GPF;
1059 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
1061 pml4e = ldq_phys(pml4e_addr);
1062 if (!(pml4e & PG_PRESENT_MASK)) {
1066 if (!(env->efer & MSR_EFER_NXE) && (pml4e & PG_NX_MASK)) {
1067 error_code = PG_ERROR_RSVD_MASK;
1070 if (!(pml4e & PG_ACCESSED_MASK)) {
1071 pml4e |= PG_ACCESSED_MASK;
1072 stl_phys_notdirty(pml4e_addr, pml4e);
1074 ptep = pml4e ^ PG_NX_MASK;
1075 pdpe_addr = ((pml4e & PHYS_ADDR_MASK) + (((addr >> 30) & 0x1ff) << 3)) &
1077 pdpe = ldq_phys(pdpe_addr);
1078 if (!(pdpe & PG_PRESENT_MASK)) {
1082 if (!(env->efer & MSR_EFER_NXE) && (pdpe & PG_NX_MASK)) {
1083 error_code = PG_ERROR_RSVD_MASK;
1086 ptep &= pdpe ^ PG_NX_MASK;
1087 if (!(pdpe & PG_ACCESSED_MASK)) {
1088 pdpe |= PG_ACCESSED_MASK;
1089 stl_phys_notdirty(pdpe_addr, pdpe);
1094 /* XXX: load them when cr3 is loaded ? */
1095 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1097 pdpe = ldq_phys(pdpe_addr);
1098 if (!(pdpe & PG_PRESENT_MASK)) {
1102 ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK;
1105 pde_addr = ((pdpe & PHYS_ADDR_MASK) + (((addr >> 21) & 0x1ff) << 3)) &
1107 pde = ldq_phys(pde_addr);
1108 if (!(pde & PG_PRESENT_MASK)) {
1112 if (!(env->efer & MSR_EFER_NXE) && (pde & PG_NX_MASK)) {
1113 error_code = PG_ERROR_RSVD_MASK;
1116 ptep &= pde ^ PG_NX_MASK;
1117 if (pde & PG_PSE_MASK) {
1119 page_size = 2048 * 1024;
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 && !(pde & PG_DIRTY_MASK);
1134 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1135 pde |= PG_ACCESSED_MASK;
1137 pde |= PG_DIRTY_MASK;
1138 stl_phys_notdirty(pde_addr, pde);
1140 /* align to page_size */
1141 pte = pde & ((PHYS_ADDR_MASK & ~(page_size - 1)) | 0xfff);
1142 virt_addr = addr & ~(page_size - 1);
1145 if (!(pde & PG_ACCESSED_MASK)) {
1146 pde |= PG_ACCESSED_MASK;
1147 stl_phys_notdirty(pde_addr, pde);
1149 pte_addr = ((pde & PHYS_ADDR_MASK) + (((addr >> 12) & 0x1ff) << 3)) &
1151 pte = ldq_phys(pte_addr);
1152 if (!(pte & PG_PRESENT_MASK)) {
1156 if (!(env->efer & MSR_EFER_NXE) && (pte & PG_NX_MASK)) {
1157 error_code = PG_ERROR_RSVD_MASK;
1160 /* combine pde and pte nx, user and rw protections */
1161 ptep &= pte ^ PG_NX_MASK;
1163 if ((ptep & PG_NX_MASK) && is_write1 == 2)
1164 goto do_fault_protect;
1166 if (!(ptep & PG_USER_MASK))
1167 goto do_fault_protect;
1168 if (is_write && !(ptep & PG_RW_MASK))
1169 goto do_fault_protect;
1171 if ((env->cr[0] & CR0_WP_MASK) &&
1172 is_write && !(ptep & PG_RW_MASK))
1173 goto do_fault_protect;
1175 is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1176 if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1177 pte |= PG_ACCESSED_MASK;
1179 pte |= PG_DIRTY_MASK;
1180 stl_phys_notdirty(pte_addr, pte);
1183 virt_addr = addr & ~0xfff;
1184 pte = pte & (PHYS_ADDR_MASK | 0xfff);
1189 /* page directory entry */
1190 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) &
1192 pde = ldl_phys(pde_addr);
1193 if (!(pde & PG_PRESENT_MASK)) {
1197 /* if PSE bit is set, then we use a 4MB page */
1198 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1199 page_size = 4096 * 1024;
1201 if (!(pde & PG_USER_MASK))
1202 goto do_fault_protect;
1203 if (is_write && !(pde & PG_RW_MASK))
1204 goto do_fault_protect;
1206 if ((env->cr[0] & CR0_WP_MASK) &&
1207 is_write && !(pde & PG_RW_MASK))
1208 goto do_fault_protect;
1210 is_dirty = is_write && !(pde & PG_DIRTY_MASK);
1211 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1212 pde |= PG_ACCESSED_MASK;
1214 pde |= PG_DIRTY_MASK;
1215 stl_phys_notdirty(pde_addr, pde);
1218 pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1220 virt_addr = addr & ~(page_size - 1);
1222 if (!(pde & PG_ACCESSED_MASK)) {
1223 pde |= PG_ACCESSED_MASK;
1224 stl_phys_notdirty(pde_addr, pde);
1227 /* page directory entry */
1228 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) &
1230 pte = ldl_phys(pte_addr);
1231 if (!(pte & PG_PRESENT_MASK)) {
1235 /* combine pde and pte user and rw protections */
1238 if (!(ptep & PG_USER_MASK))
1239 goto do_fault_protect;
1240 if (is_write && !(ptep & PG_RW_MASK))
1241 goto do_fault_protect;
1243 if ((env->cr[0] & CR0_WP_MASK) &&
1244 is_write && !(ptep & PG_RW_MASK))
1245 goto do_fault_protect;
1247 is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1248 if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1249 pte |= PG_ACCESSED_MASK;
1251 pte |= PG_DIRTY_MASK;
1252 stl_phys_notdirty(pte_addr, pte);
1255 virt_addr = addr & ~0xfff;
1258 /* the page can be put in the TLB */
1260 if (!(ptep & PG_NX_MASK))
1262 if (pte & PG_DIRTY_MASK) {
1263 /* only set write access if already dirty... otherwise wait
1266 if (ptep & PG_RW_MASK)
1269 if (!(env->cr[0] & CR0_WP_MASK) ||
1270 (ptep & PG_RW_MASK))
1275 pte = pte & env->a20_mask;
1277 /* Even if 4MB pages, we map only one 4KB page in the cache to
1278 avoid filling it too fast */
1279 page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1280 paddr = (pte & TARGET_PAGE_MASK) + page_offset;
1281 vaddr = virt_addr + page_offset;
1283 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
1286 error_code = PG_ERROR_P_MASK;
1288 error_code |= (is_write << PG_ERROR_W_BIT);
1290 error_code |= PG_ERROR_U_MASK;
1291 if (is_write1 == 2 &&
1292 (env->efer & MSR_EFER_NXE) &&
1293 (env->cr[4] & CR4_PAE_MASK))
1294 error_code |= PG_ERROR_I_D_MASK;
1295 if (env->intercept_exceptions & (1 << EXCP0E_PAGE)) {
1296 /* cr2 is not modified in case of exceptions */
1297 stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2),
1302 env->error_code = error_code;
1303 env->exception_index = EXCP0E_PAGE;
1307 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
1309 target_ulong pde_addr, pte_addr;
1311 target_phys_addr_t paddr;
1312 uint32_t page_offset;
1315 if (env->cr[4] & CR4_PAE_MASK) {
1316 target_ulong pdpe_addr;
1319 #ifdef TARGET_X86_64
1320 if (env->hflags & HF_LMA_MASK) {
1321 uint64_t pml4e_addr, pml4e;
1324 /* test virtual address sign extension */
1325 sext = (int64_t)addr >> 47;
1326 if (sext != 0 && sext != -1)
1329 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
1331 pml4e = ldq_phys(pml4e_addr);
1332 if (!(pml4e & PG_PRESENT_MASK))
1335 pdpe_addr = ((pml4e & ~0xfff) + (((addr >> 30) & 0x1ff) << 3)) &
1337 pdpe = ldq_phys(pdpe_addr);
1338 if (!(pdpe & PG_PRESENT_MASK))
1343 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1345 pdpe = ldq_phys(pdpe_addr);
1346 if (!(pdpe & PG_PRESENT_MASK))
1350 pde_addr = ((pdpe & ~0xfff) + (((addr >> 21) & 0x1ff) << 3)) &
1352 pde = ldq_phys(pde_addr);
1353 if (!(pde & PG_PRESENT_MASK)) {
1356 if (pde & PG_PSE_MASK) {
1358 page_size = 2048 * 1024;
1359 pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1362 pte_addr = ((pde & ~0xfff) + (((addr >> 12) & 0x1ff) << 3)) &
1365 pte = ldq_phys(pte_addr);
1367 if (!(pte & PG_PRESENT_MASK))
1372 if (!(env->cr[0] & CR0_PG_MASK)) {
1376 /* page directory entry */
1377 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & env->a20_mask;
1378 pde = ldl_phys(pde_addr);
1379 if (!(pde & PG_PRESENT_MASK))
1381 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1382 pte = pde & ~0x003ff000; /* align to 4MB */
1383 page_size = 4096 * 1024;
1385 /* page directory entry */
1386 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & env->a20_mask;
1387 pte = ldl_phys(pte_addr);
1388 if (!(pte & PG_PRESENT_MASK))
1393 pte = pte & env->a20_mask;
1396 page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1397 paddr = (pte & TARGET_PAGE_MASK) + page_offset;
1401 void hw_breakpoint_insert(CPUState *env, int index)
1405 switch (hw_breakpoint_type(env->dr[7], index)) {
1407 if (hw_breakpoint_enabled(env->dr[7], index))
1408 err = cpu_breakpoint_insert(env, env->dr[index], BP_CPU,
1409 &env->cpu_breakpoint[index]);
1412 type = BP_CPU | BP_MEM_WRITE;
1415 /* No support for I/O watchpoints yet */
1418 type = BP_CPU | BP_MEM_ACCESS;
1420 err = cpu_watchpoint_insert(env, env->dr[index],
1421 hw_breakpoint_len(env->dr[7], index),
1422 type, &env->cpu_watchpoint[index]);
1426 env->cpu_breakpoint[index] = NULL;
1429 void hw_breakpoint_remove(CPUState *env, int index)
1431 if (!env->cpu_breakpoint[index])
1433 switch (hw_breakpoint_type(env->dr[7], index)) {
1435 if (hw_breakpoint_enabled(env->dr[7], index))
1436 cpu_breakpoint_remove_by_ref(env, env->cpu_breakpoint[index]);
1440 cpu_watchpoint_remove_by_ref(env, env->cpu_watchpoint[index]);
1443 /* No support for I/O watchpoints yet */
1448 int check_hw_breakpoints(CPUState *env, int force_dr6_update)
1452 int hit_enabled = 0;
1454 dr6 = env->dr[6] & ~0xf;
1455 for (reg = 0; reg < 4; reg++) {
1456 type = hw_breakpoint_type(env->dr[7], reg);
1457 if ((type == 0 && env->dr[reg] == env->eip) ||
1458 ((type & 1) && env->cpu_watchpoint[reg] &&
1459 (env->cpu_watchpoint[reg]->flags & BP_WATCHPOINT_HIT))) {
1461 if (hw_breakpoint_enabled(env->dr[7], reg))
1465 if (hit_enabled || force_dr6_update)
1470 static CPUDebugExcpHandler *prev_debug_excp_handler;
1472 void raise_exception(int exception_index);
1474 static void breakpoint_handler(CPUState *env)
1478 if (env->watchpoint_hit) {
1479 if (env->watchpoint_hit->flags & BP_CPU) {
1480 env->watchpoint_hit = NULL;
1481 if (check_hw_breakpoints(env, 0))
1482 raise_exception(EXCP01_DB);
1484 cpu_resume_from_signal(env, NULL);
1487 TAILQ_FOREACH(bp, &env->breakpoints, entry)
1488 if (bp->pc == env->eip) {
1489 if (bp->flags & BP_CPU) {
1490 check_hw_breakpoints(env, 1);
1491 raise_exception(EXCP01_DB);
1496 if (prev_debug_excp_handler)
1497 prev_debug_excp_handler(env);
1499 #endif /* !CONFIG_USER_ONLY */
1501 static void host_cpuid(uint32_t function, uint32_t count,
1502 uint32_t *eax, uint32_t *ebx,
1503 uint32_t *ecx, uint32_t *edx)
1505 #if defined(CONFIG_KVM)
1509 asm volatile("cpuid"
1510 : "=a"(vec[0]), "=b"(vec[1]),
1511 "=c"(vec[2]), "=d"(vec[3])
1512 : "0"(function), "c"(count) : "cc");
1514 asm volatile("pusha \n\t"
1516 "mov %%eax, 0(%2) \n\t"
1517 "mov %%ebx, 4(%2) \n\t"
1518 "mov %%ecx, 8(%2) \n\t"
1519 "mov %%edx, 12(%2) \n\t"
1521 : : "a"(function), "c"(count), "S"(vec)
1536 void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
1537 uint32_t *eax, uint32_t *ebx,
1538 uint32_t *ecx, uint32_t *edx)
1540 /* test if maximum index reached */
1541 if (index & 0x80000000) {
1542 if (index > env->cpuid_xlevel)
1543 index = env->cpuid_level;
1545 if (index > env->cpuid_level)
1546 index = env->cpuid_level;
1551 *eax = env->cpuid_level;
1552 *ebx = env->cpuid_vendor1;
1553 *edx = env->cpuid_vendor2;
1554 *ecx = env->cpuid_vendor3;
1556 /* sysenter isn't supported on compatibility mode on AMD. and syscall
1557 * isn't supported in compatibility mode on Intel. so advertise the
1558 * actuall cpu, and say goodbye to migration between different vendors
1559 * is you use compatibility mode. */
1560 if (kvm_enabled() && !env->cpuid_vendor_override)
1561 host_cpuid(0, 0, NULL, ebx, ecx, edx);
1564 *eax = env->cpuid_version;
1565 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1566 *ecx = env->cpuid_ext_features;
1567 *edx = env->cpuid_features;
1570 /* cache info: needed for Pentium Pro compatibility */
1577 /* cache info: needed for Core compatibility */
1579 case 0: /* L1 dcache info */
1585 case 1: /* L1 icache info */
1591 case 2: /* L2 cache info */
1597 default: /* end of info */
1606 /* mwait info: needed for Core compatibility */
1607 *eax = 0; /* Smallest monitor-line size in bytes */
1608 *ebx = 0; /* Largest monitor-line size in bytes */
1609 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
1613 /* Thermal and Power Leaf */
1620 /* Direct Cache Access Information Leaf */
1621 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1627 /* Architectural Performance Monitoring Leaf */
1634 *eax = env->cpuid_xlevel;
1635 *ebx = env->cpuid_vendor1;
1636 *edx = env->cpuid_vendor2;
1637 *ecx = env->cpuid_vendor3;
1640 *eax = env->cpuid_version;
1642 *ecx = env->cpuid_ext3_features;
1643 *edx = env->cpuid_ext2_features;
1645 if (kvm_enabled()) {
1646 /* Nested SVM not yet supported in KVM */
1647 *ecx &= ~CPUID_EXT3_SVM;
1649 /* AMD 3DNow! is not supported in QEMU */
1650 *edx &= ~(CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT);
1656 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
1657 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
1658 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
1659 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
1662 /* cache info (L1 cache) */
1669 /* cache info (L2 cache) */
1676 /* virtual & phys address size in low 2 bytes. */
1677 /* XXX: This value must match the one used in the MMU code. */
1678 if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
1679 /* 64 bit processor */
1680 #if defined(CONFIG_KQEMU)
1681 *eax = 0x00003020; /* 48 bits virtual, 32 bits physical */
1683 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1684 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
1687 #if defined(CONFIG_KQEMU)
1688 *eax = 0x00000020; /* 32 bits physical */
1690 if (env->cpuid_features & CPUID_PSE36)
1691 *eax = 0x00000024; /* 36 bits physical */
1693 *eax = 0x00000020; /* 32 bits physical */
1701 *eax = 0x00000001; /* SVM Revision */
1702 *ebx = 0x00000010; /* nr of ASIDs */
1704 *edx = 0; /* optional features */
1707 /* reserved values: zero */
1716 CPUX86State *cpu_x86_init(const char *cpu_model)
1721 env = qemu_mallocz(sizeof(CPUX86State));
1723 env->cpu_model_str = cpu_model;
1725 /* init various static tables */
1728 optimize_flags_init();
1729 #ifndef CONFIG_USER_ONLY
1730 prev_debug_excp_handler =
1731 cpu_set_debug_excp_handler(breakpoint_handler);
1734 if (cpu_x86_register(env, cpu_model) < 0) {
1743 qemu_init_vcpu(env);
1748 #if !defined(CONFIG_USER_ONLY)
1749 void do_cpu_init(CPUState *env)
1751 int sipi = env->interrupt_request & CPU_INTERRUPT_SIPI;
1753 env->interrupt_request = sipi;
1754 apic_init_reset(env);
1757 void do_cpu_sipi(CPUState *env)
1762 void do_cpu_init(CPUState *env)
1765 void do_cpu_sipi(CPUState *env)