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
30 #include "qemu-common.h"
35 /* feature flags taken from "Intel Processor Identification and the CPUID
36 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
37 * about feature names, the Linux name is used. */
38 static const char *feature_name[] = {
39 "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
40 "cx8", "apic", NULL, "sep", "mtrr", "pge", "mca", "cmov",
41 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */, NULL, "ds" /* Intel dts */, "acpi", "mmx",
42 "fxsr", "sse", "sse2", "ss", "ht" /* Intel htt */, "tm", "ia64", "pbe",
44 static const char *ext_feature_name[] = {
45 "pni" /* Intel,AMD sse3 */, NULL, NULL, "monitor", "ds_cpl", "vmx", NULL /* Linux smx */, "est",
46 "tm2", "ssse3", "cid", NULL, NULL, "cx16", "xtpr", NULL,
47 NULL, NULL, "dca", NULL, NULL, NULL, NULL, "popcnt",
48 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
50 static const char *ext2_feature_name[] = {
51 "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
52 "cx8" /* AMD CMPXCHG8B */, "apic", NULL, "syscall", "mtrr", "pge", "mca", "cmov",
53 "pat", "pse36", NULL, NULL /* Linux mp */, "nx" /* Intel xd */, NULL, "mmxext", "mmx",
54 "fxsr", "fxsr_opt" /* AMD ffxsr */, "pdpe1gb" /* AMD Page1GB */, "rdtscp", NULL, "lm" /* Intel 64 */, "3dnowext", "3dnow",
56 static const char *ext3_feature_name[] = {
57 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */, "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
58 "3dnowprefetch", "osvw", NULL /* Linux ibs */, NULL, "skinit", "wdt", NULL, NULL,
59 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
60 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
63 static void add_flagname_to_bitmaps(char *flagname, uint32_t *features,
64 uint32_t *ext_features,
65 uint32_t *ext2_features,
66 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;
90 fprintf(stderr, "CPU feature %s not found\n", flagname);
93 typedef struct x86_def_t {
96 uint32_t vendor1, vendor2, vendor3;
100 uint32_t features, ext_features, ext2_features, ext3_features;
105 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
106 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
107 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX)
108 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
109 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
110 CPUID_PSE36 | CPUID_FXSR)
111 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
112 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
113 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
114 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
115 CPUID_PAE | CPUID_SEP | CPUID_APIC)
116 static x86_def_t x86_defs[] = {
121 .vendor1 = CPUID_VENDOR_AMD_1,
122 .vendor2 = CPUID_VENDOR_AMD_2,
123 .vendor3 = CPUID_VENDOR_AMD_3,
127 .features = PPRO_FEATURES |
128 /* these features are needed for Win64 and aren't fully implemented */
129 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
130 /* this feature is needed for Solaris and isn't fully implemented */
132 .ext_features = CPUID_EXT_SSE3,
133 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
134 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
135 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
136 .ext3_features = CPUID_EXT3_SVM,
137 .xlevel = 0x8000000A,
138 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
143 .vendor1 = CPUID_VENDOR_AMD_1,
144 .vendor2 = CPUID_VENDOR_AMD_2,
145 .vendor3 = CPUID_VENDOR_AMD_3,
149 /* Missing: CPUID_VME, CPUID_HT */
150 .features = PPRO_FEATURES |
151 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
153 /* Missing: CPUID_EXT_CX16, CPUID_EXT_POPCNT */
154 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR,
155 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
156 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
157 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
158 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
160 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
161 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
162 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
163 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
164 .ext3_features = CPUID_EXT3_SVM,
165 .xlevel = 0x8000001A,
166 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
174 /* The original CPU also implements these features:
175 CPUID_VME, CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
176 CPUID_TM, CPUID_PBE */
177 .features = PPRO_FEATURES |
178 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
180 /* The original CPU also implements these ext features:
181 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
182 CPUID_EXT_TM2, CPUID_EXT_CX16, CPUID_EXT_XTPR, CPUID_EXT_PDCM */
183 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3,
184 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
185 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
186 .xlevel = 0x80000008,
187 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
196 .features = PPRO_FEATURES,
197 .ext_features = CPUID_EXT_SSE3,
199 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
207 /* The original CPU also implements these features:
208 CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
209 CPUID_TM, CPUID_PBE */
210 .features = PPRO_FEATURES | CPUID_VME |
211 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA,
212 /* The original CPU also implements these ext features:
213 CPUID_EXT_VMX, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_XTPR,
215 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR,
216 .ext2_features = CPUID_EXT2_NX,
217 .xlevel = 0x80000008,
218 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
226 .features = I486_FEATURES,
235 .features = PENTIUM_FEATURES,
244 .features = PENTIUM2_FEATURES,
253 .features = PENTIUM3_FEATURES,
259 .vendor1 = 0x68747541, /* "Auth" */
260 .vendor2 = 0x69746e65, /* "enti" */
261 .vendor3 = 0x444d4163, /* "cAMD" */
265 .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR | CPUID_MCA,
266 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) | CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
267 .xlevel = 0x80000008,
268 /* XXX: put another string ? */
269 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
273 /* original is on level 10 */
278 .features = PPRO_FEATURES |
279 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME,
280 /* Missing: CPUID_DTS | CPUID_ACPI | CPUID_SS |
281 * CPUID_HT | CPUID_TM | CPUID_PBE */
282 /* Some CPUs got no CPUID_SEP */
283 .ext_features = CPUID_EXT_MONITOR |
284 CPUID_EXT_SSE3 /* PNI */ | CPUID_EXT_SSSE3,
285 /* Missing: CPUID_EXT_DSCPL | CPUID_EXT_EST |
286 * CPUID_EXT_TM2 | CPUID_EXT_XTPR */
287 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) | CPUID_EXT2_NX,
288 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
289 .xlevel = 0x8000000A,
290 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
294 static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
299 char *s = strdup(cpu_model);
300 char *featurestr, *name = strtok(s, ",");
301 uint32_t plus_features = 0, plus_ext_features = 0, plus_ext2_features = 0, plus_ext3_features = 0;
302 uint32_t minus_features = 0, minus_ext_features = 0, minus_ext2_features = 0, minus_ext3_features = 0;
303 int family = -1, model = -1, stepping = -1;
306 for (i = 0; i < ARRAY_SIZE(x86_defs); i++) {
307 if (strcmp(name, x86_defs[i].name) == 0) {
314 memcpy(x86_cpu_def, def, sizeof(*def));
316 featurestr = strtok(NULL, ",");
320 if (featurestr[0] == '+') {
321 add_flagname_to_bitmaps(featurestr + 1, &plus_features, &plus_ext_features, &plus_ext2_features, &plus_ext3_features);
322 } else if (featurestr[0] == '-') {
323 add_flagname_to_bitmaps(featurestr + 1, &minus_features, &minus_ext_features, &minus_ext2_features, &minus_ext3_features);
324 } else if ((val = strchr(featurestr, '='))) {
326 if (!strcmp(featurestr, "family")) {
328 family = strtol(val, &err, 10);
329 if (!*val || *err || family < 0) {
330 fprintf(stderr, "bad numerical value %s\n", val);
333 x86_cpu_def->family = family;
334 } else if (!strcmp(featurestr, "model")) {
336 model = strtol(val, &err, 10);
337 if (!*val || *err || model < 0 || model > 0xff) {
338 fprintf(stderr, "bad numerical value %s\n", val);
341 x86_cpu_def->model = model;
342 } else if (!strcmp(featurestr, "stepping")) {
344 stepping = strtol(val, &err, 10);
345 if (!*val || *err || stepping < 0 || stepping > 0xf) {
346 fprintf(stderr, "bad numerical value %s\n", val);
349 x86_cpu_def->stepping = stepping;
350 } else if (!strcmp(featurestr, "vendor")) {
351 if (strlen(val) != 12) {
352 fprintf(stderr, "vendor string must be 12 chars long\n");
355 x86_cpu_def->vendor1 = 0;
356 x86_cpu_def->vendor2 = 0;
357 x86_cpu_def->vendor3 = 0;
358 for(i = 0; i < 4; i++) {
359 x86_cpu_def->vendor1 |= ((uint8_t)val[i ]) << (8 * i);
360 x86_cpu_def->vendor2 |= ((uint8_t)val[i + 4]) << (8 * i);
361 x86_cpu_def->vendor3 |= ((uint8_t)val[i + 8]) << (8 * i);
363 } else if (!strcmp(featurestr, "model_id")) {
364 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
367 fprintf(stderr, "unrecognized feature %s\n", featurestr);
371 fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
374 featurestr = strtok(NULL, ",");
376 x86_cpu_def->features |= plus_features;
377 x86_cpu_def->ext_features |= plus_ext_features;
378 x86_cpu_def->ext2_features |= plus_ext2_features;
379 x86_cpu_def->ext3_features |= plus_ext3_features;
380 x86_cpu_def->features &= ~minus_features;
381 x86_cpu_def->ext_features &= ~minus_ext_features;
382 x86_cpu_def->ext2_features &= ~minus_ext2_features;
383 x86_cpu_def->ext3_features &= ~minus_ext3_features;
392 void x86_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
396 for (i = 0; i < ARRAY_SIZE(x86_defs); i++)
397 (*cpu_fprintf)(f, "x86 %16s\n", x86_defs[i].name);
400 static int cpu_x86_register (CPUX86State *env, const char *cpu_model)
402 x86_def_t def1, *def = &def1;
404 if (cpu_x86_find_by_name(def, cpu_model) < 0)
407 env->cpuid_vendor1 = def->vendor1;
408 env->cpuid_vendor2 = def->vendor2;
409 env->cpuid_vendor3 = def->vendor3;
411 env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1;
412 env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2;
413 env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3;
415 env->cpuid_level = def->level;
416 if (def->family > 0x0f)
417 env->cpuid_version = 0xf00 | ((def->family - 0x0f) << 20);
419 env->cpuid_version = def->family << 8;
420 env->cpuid_version |= ((def->model & 0xf) << 4) | ((def->model >> 4) << 16);
421 env->cpuid_version |= def->stepping;
422 env->cpuid_features = def->features;
423 env->pat = 0x0007040600070406ULL;
424 env->cpuid_ext_features = def->ext_features;
425 env->cpuid_ext2_features = def->ext2_features;
426 env->cpuid_xlevel = def->xlevel;
427 env->cpuid_ext3_features = def->ext3_features;
429 const char *model_id = def->model_id;
433 len = strlen(model_id);
434 for(i = 0; i < 48; i++) {
438 c = (uint8_t)model_id[i];
439 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
445 /* NOTE: must be called outside the CPU execute loop */
446 void cpu_reset(CPUX86State *env)
450 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
451 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
452 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
455 memset(env, 0, offsetof(CPUX86State, breakpoints));
459 env->old_exception = -1;
461 /* init to reset state */
463 #ifdef CONFIG_SOFTMMU
464 env->hflags |= HF_SOFTMMU_MASK;
466 env->hflags2 |= HF2_GIF_MASK;
468 cpu_x86_update_cr0(env, 0x60000010);
469 env->a20_mask = ~0x0;
470 env->smbase = 0x30000;
472 env->idt.limit = 0xffff;
473 env->gdt.limit = 0xffff;
474 env->ldt.limit = 0xffff;
475 env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
476 env->tr.limit = 0xffff;
477 env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
479 cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
480 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK | DESC_R_MASK);
481 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
482 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
483 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
484 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
485 cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
486 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
487 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
488 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
489 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
490 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
493 env->regs[R_EDX] = env->cpuid_version;
498 for(i = 0;i < 8; i++)
504 memset(env->dr, 0, sizeof(env->dr));
505 env->dr[6] = DR6_FIXED_1;
506 env->dr[7] = DR7_FIXED_1;
507 cpu_breakpoint_remove_all(env, BP_CPU);
508 cpu_watchpoint_remove_all(env, BP_CPU);
511 void cpu_x86_close(CPUX86State *env)
516 /***********************************************************/
519 static const char *cc_op_str[] = {
575 cpu_x86_dump_seg_cache(CPUState *env, FILE *f,
576 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
577 const char *name, struct SegmentCache *sc)
580 if (env->hflags & HF_CS64_MASK) {
581 cpu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name,
582 sc->selector, sc->base, sc->limit, sc->flags);
586 cpu_fprintf(f, "%-3s=%04x %08x %08x %08x", name, sc->selector,
587 (uint32_t)sc->base, sc->limit, sc->flags);
590 if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK))
593 cpu_fprintf(f, " DPL=%d ", (sc->flags & DESC_DPL_MASK) >> DESC_DPL_SHIFT);
594 if (sc->flags & DESC_S_MASK) {
595 if (sc->flags & DESC_CS_MASK) {
596 cpu_fprintf(f, (sc->flags & DESC_L_MASK) ? "CS64" :
597 ((sc->flags & DESC_B_MASK) ? "CS32" : "CS16"));
598 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_C_MASK) ? 'C' : '-',
599 (sc->flags & DESC_R_MASK) ? 'R' : '-');
601 cpu_fprintf(f, (sc->flags & DESC_B_MASK) ? "DS " : "DS16");
602 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_E_MASK) ? 'E' : '-',
603 (sc->flags & DESC_W_MASK) ? 'W' : '-');
605 cpu_fprintf(f, "%c]", (sc->flags & DESC_A_MASK) ? 'A' : '-');
607 static const char *sys_type_name[2][16] = {
609 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
610 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
611 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
612 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
615 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
616 "Reserved", "Reserved", "Reserved", "Reserved",
617 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
618 "Reserved", "IntGate64", "TrapGate64"
621 cpu_fprintf(f, sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0]
622 [(sc->flags & DESC_TYPE_MASK)
623 >> DESC_TYPE_SHIFT]);
626 cpu_fprintf(f, "\n");
629 void cpu_dump_state(CPUState *env, FILE *f,
630 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
635 static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
638 kvm_arch_get_registers(env);
640 eflags = env->eflags;
642 if (env->hflags & HF_CS64_MASK) {
644 "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n"
645 "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n"
646 "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n"
647 "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n"
648 "RIP=%016" PRIx64 " RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
666 eflags & DF_MASK ? 'D' : '-',
667 eflags & CC_O ? 'O' : '-',
668 eflags & CC_S ? 'S' : '-',
669 eflags & CC_Z ? 'Z' : '-',
670 eflags & CC_A ? 'A' : '-',
671 eflags & CC_P ? 'P' : '-',
672 eflags & CC_C ? 'C' : '-',
673 env->hflags & HF_CPL_MASK,
674 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
675 (int)(env->a20_mask >> 20) & 1,
676 (env->hflags >> HF_SMM_SHIFT) & 1,
681 cpu_fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
682 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
683 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
684 (uint32_t)env->regs[R_EAX],
685 (uint32_t)env->regs[R_EBX],
686 (uint32_t)env->regs[R_ECX],
687 (uint32_t)env->regs[R_EDX],
688 (uint32_t)env->regs[R_ESI],
689 (uint32_t)env->regs[R_EDI],
690 (uint32_t)env->regs[R_EBP],
691 (uint32_t)env->regs[R_ESP],
692 (uint32_t)env->eip, eflags,
693 eflags & DF_MASK ? 'D' : '-',
694 eflags & CC_O ? 'O' : '-',
695 eflags & CC_S ? 'S' : '-',
696 eflags & CC_Z ? 'Z' : '-',
697 eflags & CC_A ? 'A' : '-',
698 eflags & CC_P ? 'P' : '-',
699 eflags & CC_C ? 'C' : '-',
700 env->hflags & HF_CPL_MASK,
701 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
702 (int)(env->a20_mask >> 20) & 1,
703 (env->hflags >> HF_SMM_SHIFT) & 1,
707 for(i = 0; i < 6; i++) {
708 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, seg_name[i],
711 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "LDT", &env->ldt);
712 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "TR", &env->tr);
715 if (env->hflags & HF_LMA_MASK) {
716 cpu_fprintf(f, "GDT= %016" PRIx64 " %08x\n",
717 env->gdt.base, env->gdt.limit);
718 cpu_fprintf(f, "IDT= %016" PRIx64 " %08x\n",
719 env->idt.base, env->idt.limit);
720 cpu_fprintf(f, "CR0=%08x CR2=%016" PRIx64 " CR3=%016" PRIx64 " CR4=%08x\n",
721 (uint32_t)env->cr[0],
724 (uint32_t)env->cr[4]);
725 for(i = 0; i < 4; i++)
726 cpu_fprintf(f, "DR%d=%016" PRIx64 " ", i, env->dr[i]);
727 cpu_fprintf(f, "\nDR6=%016" PRIx64 " DR7=%016" PRIx64 "\n",
728 env->dr[6], env->dr[7]);
732 cpu_fprintf(f, "GDT= %08x %08x\n",
733 (uint32_t)env->gdt.base, env->gdt.limit);
734 cpu_fprintf(f, "IDT= %08x %08x\n",
735 (uint32_t)env->idt.base, env->idt.limit);
736 cpu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
737 (uint32_t)env->cr[0],
738 (uint32_t)env->cr[2],
739 (uint32_t)env->cr[3],
740 (uint32_t)env->cr[4]);
741 for(i = 0; i < 4; i++)
742 cpu_fprintf(f, "DR%d=%08x ", i, env->dr[i]);
743 cpu_fprintf(f, "\nDR6=%08x DR7=%08x\n", env->dr[6], env->dr[7]);
745 if (flags & X86_DUMP_CCOP) {
746 if ((unsigned)env->cc_op < CC_OP_NB)
747 snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]);
749 snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
751 if (env->hflags & HF_CS64_MASK) {
752 cpu_fprintf(f, "CCS=%016" PRIx64 " CCD=%016" PRIx64 " CCO=%-8s\n",
753 env->cc_src, env->cc_dst,
758 cpu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
759 (uint32_t)env->cc_src, (uint32_t)env->cc_dst,
763 if (flags & X86_DUMP_FPU) {
766 for(i = 0; i < 8; i++) {
767 fptag |= ((!env->fptags[i]) << i);
769 cpu_fprintf(f, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
771 (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11,
776 #if defined(USE_X86LDOUBLE)
784 tmp.d = env->fpregs[i].d;
785 cpu_fprintf(f, "FPR%d=%016" PRIx64 " %04x",
786 i, tmp.l.lower, tmp.l.upper);
788 cpu_fprintf(f, "FPR%d=%016" PRIx64,
789 i, env->fpregs[i].mmx.q);
792 cpu_fprintf(f, "\n");
796 if (env->hflags & HF_CS64_MASK)
801 cpu_fprintf(f, "XMM%02d=%08x%08x%08x%08x",
803 env->xmm_regs[i].XMM_L(3),
804 env->xmm_regs[i].XMM_L(2),
805 env->xmm_regs[i].XMM_L(1),
806 env->xmm_regs[i].XMM_L(0));
808 cpu_fprintf(f, "\n");
815 /***********************************************************/
817 /* XXX: add PGE support */
819 void cpu_x86_set_a20(CPUX86State *env, int a20_state)
821 a20_state = (a20_state != 0);
822 if (a20_state != ((env->a20_mask >> 20) & 1)) {
823 #if defined(DEBUG_MMU)
824 printf("A20 update: a20=%d\n", a20_state);
826 /* if the cpu is currently executing code, we must unlink it and
827 all the potentially executing TB */
828 cpu_interrupt(env, CPU_INTERRUPT_EXITTB);
830 /* when a20 is changed, all the MMU mappings are invalid, so
831 we must flush everything */
833 env->a20_mask = (~0x100000) | (a20_state << 20);
837 void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
841 #if defined(DEBUG_MMU)
842 printf("CR0 update: CR0=0x%08x\n", new_cr0);
844 if ((new_cr0 & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK)) !=
845 (env->cr[0] & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK))) {
850 if (!(env->cr[0] & CR0_PG_MASK) && (new_cr0 & CR0_PG_MASK) &&
851 (env->efer & MSR_EFER_LME)) {
852 /* enter in long mode */
853 /* XXX: generate an exception */
854 if (!(env->cr[4] & CR4_PAE_MASK))
856 env->efer |= MSR_EFER_LMA;
857 env->hflags |= HF_LMA_MASK;
858 } else if ((env->cr[0] & CR0_PG_MASK) && !(new_cr0 & CR0_PG_MASK) &&
859 (env->efer & MSR_EFER_LMA)) {
861 env->efer &= ~MSR_EFER_LMA;
862 env->hflags &= ~(HF_LMA_MASK | HF_CS64_MASK);
863 env->eip &= 0xffffffff;
866 env->cr[0] = new_cr0 | CR0_ET_MASK;
868 /* update PE flag in hidden flags */
869 pe_state = (env->cr[0] & CR0_PE_MASK);
870 env->hflags = (env->hflags & ~HF_PE_MASK) | (pe_state << HF_PE_SHIFT);
871 /* ensure that ADDSEG is always set in real mode */
872 env->hflags |= ((pe_state ^ 1) << HF_ADDSEG_SHIFT);
873 /* update FPU flags */
874 env->hflags = (env->hflags & ~(HF_MP_MASK | HF_EM_MASK | HF_TS_MASK)) |
875 ((new_cr0 << (HF_MP_SHIFT - 1)) & (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK));
878 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
880 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
882 env->cr[3] = new_cr3;
883 if (env->cr[0] & CR0_PG_MASK) {
884 #if defined(DEBUG_MMU)
885 printf("CR3 update: CR3=" TARGET_FMT_lx "\n", new_cr3);
891 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
893 #if defined(DEBUG_MMU)
894 printf("CR4 update: CR4=%08x\n", (uint32_t)env->cr[4]);
896 if ((new_cr4 & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK)) !=
897 (env->cr[4] & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK))) {
901 if (!(env->cpuid_features & CPUID_SSE))
902 new_cr4 &= ~CR4_OSFXSR_MASK;
903 if (new_cr4 & CR4_OSFXSR_MASK)
904 env->hflags |= HF_OSFXSR_MASK;
906 env->hflags &= ~HF_OSFXSR_MASK;
908 env->cr[4] = new_cr4;
911 #if defined(CONFIG_USER_ONLY)
913 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
914 int is_write, int mmu_idx, int is_softmmu)
916 /* user mode only emulation */
919 env->error_code = (is_write << PG_ERROR_W_BIT);
920 env->error_code |= PG_ERROR_U_MASK;
921 env->exception_index = EXCP0E_PAGE;
925 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
932 /* XXX: This value should match the one returned by CPUID
934 #if defined(CONFIG_KQEMU)
935 #define PHYS_ADDR_MASK 0xfffff000LL
937 # if defined(TARGET_X86_64)
938 # define PHYS_ADDR_MASK 0xfffffff000LL
940 # define PHYS_ADDR_MASK 0xffffff000LL
945 -1 = cannot handle fault
946 0 = nothing more to do
947 1 = generate PF fault
948 2 = soft MMU activation required for this block
950 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
951 int is_write1, int mmu_idx, int is_softmmu)
954 target_ulong pde_addr, pte_addr;
955 int error_code, is_dirty, prot, page_size, ret, is_write, is_user;
956 target_phys_addr_t paddr;
957 uint32_t page_offset;
958 target_ulong vaddr, virt_addr;
960 is_user = mmu_idx == MMU_USER_IDX;
961 #if defined(DEBUG_MMU)
962 printf("MMU fault: addr=" TARGET_FMT_lx " w=%d u=%d eip=" TARGET_FMT_lx "\n",
963 addr, is_write1, is_user, env->eip);
965 is_write = is_write1 & 1;
967 if (!(env->cr[0] & CR0_PG_MASK)) {
969 virt_addr = addr & TARGET_PAGE_MASK;
970 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
975 if (env->cr[4] & CR4_PAE_MASK) {
977 target_ulong pdpe_addr;
980 if (env->hflags & HF_LMA_MASK) {
981 uint64_t pml4e_addr, pml4e;
984 /* test virtual address sign extension */
985 sext = (int64_t)addr >> 47;
986 if (sext != 0 && sext != -1) {
988 env->exception_index = EXCP0D_GPF;
992 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
994 pml4e = ldq_phys(pml4e_addr);
995 if (!(pml4e & PG_PRESENT_MASK)) {
999 if (!(env->efer & MSR_EFER_NXE) && (pml4e & PG_NX_MASK)) {
1000 error_code = PG_ERROR_RSVD_MASK;
1003 if (!(pml4e & PG_ACCESSED_MASK)) {
1004 pml4e |= PG_ACCESSED_MASK;
1005 stl_phys_notdirty(pml4e_addr, pml4e);
1007 ptep = pml4e ^ PG_NX_MASK;
1008 pdpe_addr = ((pml4e & PHYS_ADDR_MASK) + (((addr >> 30) & 0x1ff) << 3)) &
1010 pdpe = ldq_phys(pdpe_addr);
1011 if (!(pdpe & PG_PRESENT_MASK)) {
1015 if (!(env->efer & MSR_EFER_NXE) && (pdpe & PG_NX_MASK)) {
1016 error_code = PG_ERROR_RSVD_MASK;
1019 ptep &= pdpe ^ PG_NX_MASK;
1020 if (!(pdpe & PG_ACCESSED_MASK)) {
1021 pdpe |= PG_ACCESSED_MASK;
1022 stl_phys_notdirty(pdpe_addr, pdpe);
1027 /* XXX: load them when cr3 is loaded ? */
1028 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1030 pdpe = ldq_phys(pdpe_addr);
1031 if (!(pdpe & PG_PRESENT_MASK)) {
1035 ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK;
1038 pde_addr = ((pdpe & PHYS_ADDR_MASK) + (((addr >> 21) & 0x1ff) << 3)) &
1040 pde = ldq_phys(pde_addr);
1041 if (!(pde & PG_PRESENT_MASK)) {
1045 if (!(env->efer & MSR_EFER_NXE) && (pde & PG_NX_MASK)) {
1046 error_code = PG_ERROR_RSVD_MASK;
1049 ptep &= pde ^ PG_NX_MASK;
1050 if (pde & PG_PSE_MASK) {
1052 page_size = 2048 * 1024;
1054 if ((ptep & PG_NX_MASK) && is_write1 == 2)
1055 goto do_fault_protect;
1057 if (!(ptep & PG_USER_MASK))
1058 goto do_fault_protect;
1059 if (is_write && !(ptep & PG_RW_MASK))
1060 goto do_fault_protect;
1062 if ((env->cr[0] & CR0_WP_MASK) &&
1063 is_write && !(ptep & PG_RW_MASK))
1064 goto do_fault_protect;
1066 is_dirty = is_write && !(pde & PG_DIRTY_MASK);
1067 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1068 pde |= PG_ACCESSED_MASK;
1070 pde |= PG_DIRTY_MASK;
1071 stl_phys_notdirty(pde_addr, pde);
1073 /* align to page_size */
1074 pte = pde & ((PHYS_ADDR_MASK & ~(page_size - 1)) | 0xfff);
1075 virt_addr = addr & ~(page_size - 1);
1078 if (!(pde & PG_ACCESSED_MASK)) {
1079 pde |= PG_ACCESSED_MASK;
1080 stl_phys_notdirty(pde_addr, pde);
1082 pte_addr = ((pde & PHYS_ADDR_MASK) + (((addr >> 12) & 0x1ff) << 3)) &
1084 pte = ldq_phys(pte_addr);
1085 if (!(pte & PG_PRESENT_MASK)) {
1089 if (!(env->efer & MSR_EFER_NXE) && (pte & PG_NX_MASK)) {
1090 error_code = PG_ERROR_RSVD_MASK;
1093 /* combine pde and pte nx, user and rw protections */
1094 ptep &= pte ^ PG_NX_MASK;
1096 if ((ptep & PG_NX_MASK) && is_write1 == 2)
1097 goto do_fault_protect;
1099 if (!(ptep & PG_USER_MASK))
1100 goto do_fault_protect;
1101 if (is_write && !(ptep & PG_RW_MASK))
1102 goto do_fault_protect;
1104 if ((env->cr[0] & CR0_WP_MASK) &&
1105 is_write && !(ptep & PG_RW_MASK))
1106 goto do_fault_protect;
1108 is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1109 if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1110 pte |= PG_ACCESSED_MASK;
1112 pte |= PG_DIRTY_MASK;
1113 stl_phys_notdirty(pte_addr, pte);
1116 virt_addr = addr & ~0xfff;
1117 pte = pte & (PHYS_ADDR_MASK | 0xfff);
1122 /* page directory entry */
1123 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) &
1125 pde = ldl_phys(pde_addr);
1126 if (!(pde & PG_PRESENT_MASK)) {
1130 /* if PSE bit is set, then we use a 4MB page */
1131 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1132 page_size = 4096 * 1024;
1134 if (!(pde & PG_USER_MASK))
1135 goto do_fault_protect;
1136 if (is_write && !(pde & PG_RW_MASK))
1137 goto do_fault_protect;
1139 if ((env->cr[0] & CR0_WP_MASK) &&
1140 is_write && !(pde & PG_RW_MASK))
1141 goto do_fault_protect;
1143 is_dirty = is_write && !(pde & PG_DIRTY_MASK);
1144 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1145 pde |= PG_ACCESSED_MASK;
1147 pde |= PG_DIRTY_MASK;
1148 stl_phys_notdirty(pde_addr, pde);
1151 pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1153 virt_addr = addr & ~(page_size - 1);
1155 if (!(pde & PG_ACCESSED_MASK)) {
1156 pde |= PG_ACCESSED_MASK;
1157 stl_phys_notdirty(pde_addr, pde);
1160 /* page directory entry */
1161 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) &
1163 pte = ldl_phys(pte_addr);
1164 if (!(pte & PG_PRESENT_MASK)) {
1168 /* combine pde and pte user and rw protections */
1171 if (!(ptep & PG_USER_MASK))
1172 goto do_fault_protect;
1173 if (is_write && !(ptep & PG_RW_MASK))
1174 goto do_fault_protect;
1176 if ((env->cr[0] & CR0_WP_MASK) &&
1177 is_write && !(ptep & PG_RW_MASK))
1178 goto do_fault_protect;
1180 is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1181 if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1182 pte |= PG_ACCESSED_MASK;
1184 pte |= PG_DIRTY_MASK;
1185 stl_phys_notdirty(pte_addr, pte);
1188 virt_addr = addr & ~0xfff;
1191 /* the page can be put in the TLB */
1193 if (!(ptep & PG_NX_MASK))
1195 if (pte & PG_DIRTY_MASK) {
1196 /* only set write access if already dirty... otherwise wait
1199 if (ptep & PG_RW_MASK)
1202 if (!(env->cr[0] & CR0_WP_MASK) ||
1203 (ptep & PG_RW_MASK))
1208 pte = pte & env->a20_mask;
1210 /* Even if 4MB pages, we map only one 4KB page in the cache to
1211 avoid filling it too fast */
1212 page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1213 paddr = (pte & TARGET_PAGE_MASK) + page_offset;
1214 vaddr = virt_addr + page_offset;
1216 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
1219 error_code = PG_ERROR_P_MASK;
1221 error_code |= (is_write << PG_ERROR_W_BIT);
1223 error_code |= PG_ERROR_U_MASK;
1224 if (is_write1 == 2 &&
1225 (env->efer & MSR_EFER_NXE) &&
1226 (env->cr[4] & CR4_PAE_MASK))
1227 error_code |= PG_ERROR_I_D_MASK;
1228 if (env->intercept_exceptions & (1 << EXCP0E_PAGE)) {
1229 /* cr2 is not modified in case of exceptions */
1230 stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2),
1235 env->error_code = error_code;
1236 env->exception_index = EXCP0E_PAGE;
1240 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
1242 target_ulong pde_addr, pte_addr;
1244 target_phys_addr_t paddr;
1245 uint32_t page_offset;
1248 if (env->cr[4] & CR4_PAE_MASK) {
1249 target_ulong pdpe_addr;
1252 #ifdef TARGET_X86_64
1253 if (env->hflags & HF_LMA_MASK) {
1254 uint64_t pml4e_addr, pml4e;
1257 /* test virtual address sign extension */
1258 sext = (int64_t)addr >> 47;
1259 if (sext != 0 && sext != -1)
1262 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
1264 pml4e = ldq_phys(pml4e_addr);
1265 if (!(pml4e & PG_PRESENT_MASK))
1268 pdpe_addr = ((pml4e & ~0xfff) + (((addr >> 30) & 0x1ff) << 3)) &
1270 pdpe = ldq_phys(pdpe_addr);
1271 if (!(pdpe & PG_PRESENT_MASK))
1276 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1278 pdpe = ldq_phys(pdpe_addr);
1279 if (!(pdpe & PG_PRESENT_MASK))
1283 pde_addr = ((pdpe & ~0xfff) + (((addr >> 21) & 0x1ff) << 3)) &
1285 pde = ldq_phys(pde_addr);
1286 if (!(pde & PG_PRESENT_MASK)) {
1289 if (pde & PG_PSE_MASK) {
1291 page_size = 2048 * 1024;
1292 pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1295 pte_addr = ((pde & ~0xfff) + (((addr >> 12) & 0x1ff) << 3)) &
1298 pte = ldq_phys(pte_addr);
1300 if (!(pte & PG_PRESENT_MASK))
1305 if (!(env->cr[0] & CR0_PG_MASK)) {
1309 /* page directory entry */
1310 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & env->a20_mask;
1311 pde = ldl_phys(pde_addr);
1312 if (!(pde & PG_PRESENT_MASK))
1314 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1315 pte = pde & ~0x003ff000; /* align to 4MB */
1316 page_size = 4096 * 1024;
1318 /* page directory entry */
1319 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & env->a20_mask;
1320 pte = ldl_phys(pte_addr);
1321 if (!(pte & PG_PRESENT_MASK))
1326 pte = pte & env->a20_mask;
1329 page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1330 paddr = (pte & TARGET_PAGE_MASK) + page_offset;
1334 void hw_breakpoint_insert(CPUState *env, int index)
1338 switch (hw_breakpoint_type(env->dr[7], index)) {
1340 if (hw_breakpoint_enabled(env->dr[7], index))
1341 err = cpu_breakpoint_insert(env, env->dr[index], BP_CPU,
1342 &env->cpu_breakpoint[index]);
1345 type = BP_CPU | BP_MEM_WRITE;
1348 /* No support for I/O watchpoints yet */
1351 type = BP_CPU | BP_MEM_ACCESS;
1353 err = cpu_watchpoint_insert(env, env->dr[index],
1354 hw_breakpoint_len(env->dr[7], index),
1355 type, &env->cpu_watchpoint[index]);
1359 env->cpu_breakpoint[index] = NULL;
1362 void hw_breakpoint_remove(CPUState *env, int index)
1364 if (!env->cpu_breakpoint[index])
1366 switch (hw_breakpoint_type(env->dr[7], index)) {
1368 if (hw_breakpoint_enabled(env->dr[7], index))
1369 cpu_breakpoint_remove_by_ref(env, env->cpu_breakpoint[index]);
1373 cpu_watchpoint_remove_by_ref(env, env->cpu_watchpoint[index]);
1376 /* No support for I/O watchpoints yet */
1381 int check_hw_breakpoints(CPUState *env, int force_dr6_update)
1385 int hit_enabled = 0;
1387 dr6 = env->dr[6] & ~0xf;
1388 for (reg = 0; reg < 4; reg++) {
1389 type = hw_breakpoint_type(env->dr[7], reg);
1390 if ((type == 0 && env->dr[reg] == env->eip) ||
1391 ((type & 1) && env->cpu_watchpoint[reg] &&
1392 (env->cpu_watchpoint[reg]->flags & BP_WATCHPOINT_HIT))) {
1394 if (hw_breakpoint_enabled(env->dr[7], reg))
1398 if (hit_enabled || force_dr6_update)
1403 static CPUDebugExcpHandler *prev_debug_excp_handler;
1405 void raise_exception(int exception_index);
1407 static void breakpoint_handler(CPUState *env)
1411 if (env->watchpoint_hit) {
1412 if (env->watchpoint_hit->flags & BP_CPU) {
1413 env->watchpoint_hit = NULL;
1414 if (check_hw_breakpoints(env, 0))
1415 raise_exception(EXCP01_DB);
1417 cpu_resume_from_signal(env, NULL);
1420 TAILQ_FOREACH(bp, &env->breakpoints, entry)
1421 if (bp->pc == env->eip) {
1422 if (bp->flags & BP_CPU) {
1423 check_hw_breakpoints(env, 1);
1424 raise_exception(EXCP01_DB);
1429 if (prev_debug_excp_handler)
1430 prev_debug_excp_handler(env);
1432 #endif /* !CONFIG_USER_ONLY */
1434 static void host_cpuid(uint32_t function, uint32_t count,
1435 uint32_t *eax, uint32_t *ebx,
1436 uint32_t *ecx, uint32_t *edx)
1438 #if defined(CONFIG_KVM)
1442 asm volatile("cpuid"
1443 : "=a"(vec[0]), "=b"(vec[1]),
1444 "=c"(vec[2]), "=d"(vec[3])
1445 : "0"(function), "c"(count) : "cc");
1447 asm volatile("pusha \n\t"
1449 "mov %%eax, 0(%2) \n\t"
1450 "mov %%ebx, 4(%2) \n\t"
1451 "mov %%ecx, 8(%2) \n\t"
1452 "mov %%edx, 12(%2) \n\t"
1454 : : "a"(function), "c"(count), "S"(vec)
1469 void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
1470 uint32_t *eax, uint32_t *ebx,
1471 uint32_t *ecx, uint32_t *edx)
1473 /* test if maximum index reached */
1474 if (index & 0x80000000) {
1475 if (index > env->cpuid_xlevel)
1476 index = env->cpuid_level;
1478 if (index > env->cpuid_level)
1479 index = env->cpuid_level;
1484 *eax = env->cpuid_level;
1485 *ebx = env->cpuid_vendor1;
1486 *edx = env->cpuid_vendor2;
1487 *ecx = env->cpuid_vendor3;
1489 /* sysenter isn't supported on compatibility mode on AMD. and syscall
1490 * isn't supported in compatibility mode on Intel. so advertise the
1491 * actuall cpu, and say goodbye to migration between different vendors
1492 * is you use compatibility mode. */
1494 host_cpuid(0, 0, NULL, ebx, ecx, edx);
1497 *eax = env->cpuid_version;
1498 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1499 *ecx = env->cpuid_ext_features;
1500 *edx = env->cpuid_features;
1502 /* "Hypervisor present" bit required for Microsoft SVVP */
1507 /* cache info: needed for Pentium Pro compatibility */
1514 /* cache info: needed for Core compatibility */
1516 case 0: /* L1 dcache info */
1522 case 1: /* L1 icache info */
1528 case 2: /* L2 cache info */
1534 default: /* end of info */
1543 /* mwait info: needed for Core compatibility */
1544 *eax = 0; /* Smallest monitor-line size in bytes */
1545 *ebx = 0; /* Largest monitor-line size in bytes */
1546 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
1550 /* Thermal and Power Leaf */
1557 /* Direct Cache Access Information Leaf */
1558 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1564 /* Architectural Performance Monitoring Leaf */
1571 *eax = env->cpuid_xlevel;
1572 *ebx = env->cpuid_vendor1;
1573 *edx = env->cpuid_vendor2;
1574 *ecx = env->cpuid_vendor3;
1577 *eax = env->cpuid_features;
1579 *ecx = env->cpuid_ext3_features;
1580 *edx = env->cpuid_ext2_features;
1582 if (kvm_enabled()) {
1583 uint32_t h_eax, h_edx;
1585 host_cpuid(index, 0, &h_eax, NULL, NULL, &h_edx);
1587 /* disable CPU features that the host does not support */
1590 if ((h_edx & 0x20000000) == 0 /* || !lm_capable_kernel */)
1591 *edx &= ~0x20000000;
1593 if ((h_edx & 0x00000800) == 0)
1594 *edx &= ~0x00000800;
1596 if ((h_edx & 0x00100000) == 0)
1597 *edx &= ~0x00100000;
1599 /* disable CPU features that KVM cannot support */
1604 *edx &= ~0xc0000000;
1610 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
1611 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
1612 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
1613 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
1616 /* cache info (L1 cache) */
1623 /* cache info (L2 cache) */
1630 /* virtual & phys address size in low 2 bytes. */
1631 /* XXX: This value must match the one used in the MMU code. */
1632 if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
1633 /* 64 bit processor */
1634 #if defined(CONFIG_KQEMU)
1635 *eax = 0x00003020; /* 48 bits virtual, 32 bits physical */
1637 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1638 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
1641 #if defined(CONFIG_KQEMU)
1642 *eax = 0x00000020; /* 32 bits physical */
1644 if (env->cpuid_features & CPUID_PSE36)
1645 *eax = 0x00000024; /* 36 bits physical */
1647 *eax = 0x00000020; /* 32 bits physical */
1655 *eax = 0x00000001; /* SVM Revision */
1656 *ebx = 0x00000010; /* nr of ASIDs */
1658 *edx = 0; /* optional features */
1661 /* reserved values: zero */
1670 CPUX86State *cpu_x86_init(const char *cpu_model)
1675 env = qemu_mallocz(sizeof(CPUX86State));
1677 env->cpu_model_str = cpu_model;
1679 /* init various static tables */
1682 optimize_flags_init();
1683 #ifndef CONFIG_USER_ONLY
1684 prev_debug_excp_handler =
1685 cpu_set_debug_excp_handler(breakpoint_handler);
1688 if (cpu_x86_register(env, cpu_model) < 0) {
1697 qemu_init_vcpu(env);