Make x86 cpuid feature names available in file scope
[qemu] / target-i386 / helper.c
1 /*
2  *  i386 helpers (without register variable usage)
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
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.
10  *
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.
15  *
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
19  */
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <signal.h>
26 #include <assert.h>
27
28 #include "cpu.h"
29 #include "exec-all.h"
30 #include "qemu-common.h"
31 #include "kvm.h"
32
33 //#define DEBUG_MMU
34
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",
43 };
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,
49 };
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",
55 };
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,
61 };
62
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)
67 {
68     int i;
69
70     for ( i = 0 ; i < 32 ; i++ ) 
71         if (feature_name[i] && !strcmp (flagname, feature_name[i])) {
72             *features |= 1 << i;
73             return;
74         }
75     for ( i = 0 ; i < 32 ; i++ ) 
76         if (ext_feature_name[i] && !strcmp (flagname, ext_feature_name[i])) {
77             *ext_features |= 1 << i;
78             return;
79         }
80     for ( i = 0 ; i < 32 ; i++ ) 
81         if (ext2_feature_name[i] && !strcmp (flagname, ext2_feature_name[i])) {
82             *ext2_features |= 1 << i;
83             return;
84         }
85     for ( i = 0 ; i < 32 ; i++ ) 
86         if (ext3_feature_name[i] && !strcmp (flagname, ext3_feature_name[i])) {
87             *ext3_features |= 1 << i;
88             return;
89         }
90     fprintf(stderr, "CPU feature %s not found\n", flagname);
91 }
92
93 typedef struct x86_def_t {
94     const char *name;
95     uint32_t level;
96     uint32_t vendor1, vendor2, vendor3;
97     int family;
98     int model;
99     int stepping;
100     uint32_t features, ext_features, ext2_features, ext3_features;
101     uint32_t xlevel;
102     char model_id[48];
103 } x86_def_t;
104
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[] = {
117 #ifdef TARGET_X86_64
118     {
119         .name = "qemu64",
120         .level = 2,
121         .vendor1 = CPUID_VENDOR_AMD_1,
122         .vendor2 = CPUID_VENDOR_AMD_2,
123         .vendor3 = CPUID_VENDOR_AMD_3,
124         .family = 6,
125         .model = 2,
126         .stepping = 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 */
131             CPUID_PSE36,
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,
139     },
140     {
141         .name = "phenom",
142         .level = 5,
143         .vendor1 = CPUID_VENDOR_AMD_1,
144         .vendor2 = CPUID_VENDOR_AMD_2,
145         .vendor3 = CPUID_VENDOR_AMD_3,
146         .family = 16,
147         .model = 2,
148         .stepping = 3,
149         /* Missing: CPUID_VME, CPUID_HT */
150         .features = PPRO_FEATURES | 
151             CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
152             CPUID_PSE36,
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 |
159             CPUID_EXT2_FFXSR,
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"
167     },
168     {
169         .name = "core2duo",
170         .level = 10,
171         .family = 6,
172         .model = 15,
173         .stepping = 11,
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 |
179             CPUID_PSE36,
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",
188     },
189 #endif
190     {
191         .name = "qemu32",
192         .level = 2,
193         .family = 6,
194         .model = 3,
195         .stepping = 3,
196         .features = PPRO_FEATURES,
197         .ext_features = CPUID_EXT_SSE3,
198         .xlevel = 0,
199         .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
200     },
201     {
202         .name = "coreduo",
203         .level = 10,
204         .family = 6,
205         .model = 14,
206         .stepping = 8,
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,
214                CPUID_EXT_PDCM */
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",
219     },
220     {
221         .name = "486",
222         .level = 0,
223         .family = 4,
224         .model = 0,
225         .stepping = 0,
226         .features = I486_FEATURES,
227         .xlevel = 0,
228     },
229     {
230         .name = "pentium",
231         .level = 1,
232         .family = 5,
233         .model = 4,
234         .stepping = 3,
235         .features = PENTIUM_FEATURES,
236         .xlevel = 0,
237     },
238     {
239         .name = "pentium2",
240         .level = 2,
241         .family = 6,
242         .model = 5,
243         .stepping = 2,
244         .features = PENTIUM2_FEATURES,
245         .xlevel = 0,
246     },
247     {
248         .name = "pentium3",
249         .level = 2,
250         .family = 6,
251         .model = 7,
252         .stepping = 3,
253         .features = PENTIUM3_FEATURES,
254         .xlevel = 0,
255     },
256     {
257         .name = "athlon",
258         .level = 2,
259         .vendor1 = 0x68747541, /* "Auth" */
260         .vendor2 = 0x69746e65, /* "enti" */
261         .vendor3 = 0x444d4163, /* "cAMD" */
262         .family = 6,
263         .model = 2,
264         .stepping = 3,
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,
270     },
271     {
272         .name = "n270",
273         /* original is on level 10 */
274         .level = 5,
275         .family = 6,
276         .model = 28,
277         .stepping = 2,
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",
291     },
292 };
293
294 static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
295 {
296     unsigned int i;
297     x86_def_t *def;
298
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;
304
305     def = NULL;
306     for (i = 0; i < ARRAY_SIZE(x86_defs); i++) {
307         if (strcmp(name, x86_defs[i].name) == 0) {
308             def = &x86_defs[i];
309             break;
310         }
311     }
312     if (!def)
313         goto error;
314     memcpy(x86_cpu_def, def, sizeof(*def));
315
316     featurestr = strtok(NULL, ",");
317
318     while (featurestr) {
319         char *val;
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, '='))) {
325             *val = 0; val++;
326             if (!strcmp(featurestr, "family")) {
327                 char *err;
328                 family = strtol(val, &err, 10);
329                 if (!*val || *err || family < 0) {
330                     fprintf(stderr, "bad numerical value %s\n", val);
331                     goto error;
332                 }
333                 x86_cpu_def->family = family;
334             } else if (!strcmp(featurestr, "model")) {
335                 char *err;
336                 model = strtol(val, &err, 10);
337                 if (!*val || *err || model < 0 || model > 0xff) {
338                     fprintf(stderr, "bad numerical value %s\n", val);
339                     goto error;
340                 }
341                 x86_cpu_def->model = model;
342             } else if (!strcmp(featurestr, "stepping")) {
343                 char *err;
344                 stepping = strtol(val, &err, 10);
345                 if (!*val || *err || stepping < 0 || stepping > 0xf) {
346                     fprintf(stderr, "bad numerical value %s\n", val);
347                     goto error;
348                 }
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");
353                     goto error;
354                 }
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);
362                 }
363             } else if (!strcmp(featurestr, "model_id")) {
364                 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
365                         val);
366             } else {
367                 fprintf(stderr, "unrecognized feature %s\n", featurestr);
368                 goto error;
369             }
370         } else {
371             fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
372             goto error;
373         }
374         featurestr = strtok(NULL, ",");
375     }
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;
384     free(s);
385     return 0;
386
387 error:
388     free(s);
389     return -1;
390 }
391
392 void x86_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
393 {
394     unsigned int i;
395
396     for (i = 0; i < ARRAY_SIZE(x86_defs); i++)
397         (*cpu_fprintf)(f, "x86 %16s\n", x86_defs[i].name);
398 }
399
400 static int cpu_x86_register (CPUX86State *env, const char *cpu_model)
401 {
402     x86_def_t def1, *def = &def1;
403
404     if (cpu_x86_find_by_name(def, cpu_model) < 0)
405         return -1;
406     if (def->vendor1) {
407         env->cpuid_vendor1 = def->vendor1;
408         env->cpuid_vendor2 = def->vendor2;
409         env->cpuid_vendor3 = def->vendor3;
410     } else {
411         env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1;
412         env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2;
413         env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3;
414     }
415     env->cpuid_level = def->level;
416     if (def->family > 0x0f)
417         env->cpuid_version = 0xf00 | ((def->family - 0x0f) << 20);
418     else
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;
428     {
429         const char *model_id = def->model_id;
430         int c, len, i;
431         if (!model_id)
432             model_id = "";
433         len = strlen(model_id);
434         for(i = 0; i < 48; i++) {
435             if (i >= len)
436                 c = '\0';
437             else
438                 c = (uint8_t)model_id[i];
439             env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
440         }
441     }
442     return 0;
443 }
444
445 /* NOTE: must be called outside the CPU execute loop */
446 void cpu_reset(CPUX86State *env)
447 {
448     int i;
449
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);
453     }
454
455     memset(env, 0, offsetof(CPUX86State, breakpoints));
456
457     tlb_flush(env, 1);
458
459     env->old_exception = -1;
460
461     /* init to reset state */
462
463 #ifdef CONFIG_SOFTMMU
464     env->hflags |= HF_SOFTMMU_MASK;
465 #endif
466     env->hflags2 |= HF2_GIF_MASK;
467
468     cpu_x86_update_cr0(env, 0x60000010);
469     env->a20_mask = ~0x0;
470     env->smbase = 0x30000;
471
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);
478
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);
491
492     env->eip = 0xfff0;
493     env->regs[R_EDX] = env->cpuid_version;
494
495     env->eflags = 0x2;
496
497     /* FPU init */
498     for(i = 0;i < 8; i++)
499         env->fptags[i] = 1;
500     env->fpuc = 0x37f;
501
502     env->mxcsr = 0x1f80;
503
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);
509 }
510
511 void cpu_x86_close(CPUX86State *env)
512 {
513     qemu_free(env);
514 }
515
516 /***********************************************************/
517 /* x86 debug */
518
519 static const char *cc_op_str[] = {
520     "DYNAMIC",
521     "EFLAGS",
522
523     "MULB",
524     "MULW",
525     "MULL",
526     "MULQ",
527
528     "ADDB",
529     "ADDW",
530     "ADDL",
531     "ADDQ",
532
533     "ADCB",
534     "ADCW",
535     "ADCL",
536     "ADCQ",
537
538     "SUBB",
539     "SUBW",
540     "SUBL",
541     "SUBQ",
542
543     "SBBB",
544     "SBBW",
545     "SBBL",
546     "SBBQ",
547
548     "LOGICB",
549     "LOGICW",
550     "LOGICL",
551     "LOGICQ",
552
553     "INCB",
554     "INCW",
555     "INCL",
556     "INCQ",
557
558     "DECB",
559     "DECW",
560     "DECL",
561     "DECQ",
562
563     "SHLB",
564     "SHLW",
565     "SHLL",
566     "SHLQ",
567
568     "SARB",
569     "SARW",
570     "SARL",
571     "SARQ",
572 };
573
574 static void
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)
578 {
579 #ifdef TARGET_X86_64
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);
583     } else
584 #endif
585     {
586         cpu_fprintf(f, "%-3s=%04x %08x %08x %08x", name, sc->selector,
587                     (uint32_t)sc->base, sc->limit, sc->flags);
588     }
589
590     if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK))
591         goto done;
592
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' : '-');
600         } else {
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' : '-');
604         }
605         cpu_fprintf(f, "%c]", (sc->flags & DESC_A_MASK) ? 'A' : '-');
606     } else {
607         static const char *sys_type_name[2][16] = {
608             { /* 32 bit mode */
609                 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
610                 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
611                 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
612                 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
613             },
614             { /* 64 bit mode */
615                 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
616                 "Reserved", "Reserved", "Reserved", "Reserved",
617                 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
618                 "Reserved", "IntGate64", "TrapGate64"
619             }
620         };
621         cpu_fprintf(f, sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0]
622                                     [(sc->flags & DESC_TYPE_MASK)
623                                      >> DESC_TYPE_SHIFT]);
624     }
625 done:
626     cpu_fprintf(f, "\n");
627 }
628
629 void cpu_dump_state(CPUState *env, FILE *f,
630                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
631                     int flags)
632 {
633     int eflags, i, nb;
634     char cc_op_name[32];
635     static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
636
637     if (kvm_enabled())
638         kvm_arch_get_registers(env);
639
640     eflags = env->eflags;
641 #ifdef TARGET_X86_64
642     if (env->hflags & HF_CS64_MASK) {
643         cpu_fprintf(f,
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",
649                     env->regs[R_EAX],
650                     env->regs[R_EBX],
651                     env->regs[R_ECX],
652                     env->regs[R_EDX],
653                     env->regs[R_ESI],
654                     env->regs[R_EDI],
655                     env->regs[R_EBP],
656                     env->regs[R_ESP],
657                     env->regs[8],
658                     env->regs[9],
659                     env->regs[10],
660                     env->regs[11],
661                     env->regs[12],
662                     env->regs[13],
663                     env->regs[14],
664                     env->regs[15],
665                     env->eip, eflags,
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,
677                     env->halted);
678     } else
679 #endif
680     {
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,
704                     env->halted);
705     }
706
707     for(i = 0; i < 6; i++) {
708         cpu_x86_dump_seg_cache(env, f, cpu_fprintf, seg_name[i],
709                                &env->segs[i]);
710     }
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);
713
714 #ifdef TARGET_X86_64
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],
722                     env->cr[2],
723                     env->cr[3],
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]);
729     } else
730 #endif
731     {
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]);
744     }
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]);
748         else
749             snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
750 #ifdef TARGET_X86_64
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,
754                         cc_op_name);
755         } else
756 #endif
757         {
758             cpu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
759                         (uint32_t)env->cc_src, (uint32_t)env->cc_dst,
760                         cc_op_name);
761         }
762     }
763     if (flags & X86_DUMP_FPU) {
764         int fptag;
765         fptag = 0;
766         for(i = 0; i < 8; i++) {
767             fptag |= ((!env->fptags[i]) << i);
768         }
769         cpu_fprintf(f, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
770                     env->fpuc,
771                     (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11,
772                     env->fpstt,
773                     fptag,
774                     env->mxcsr);
775         for(i=0;i<8;i++) {
776 #if defined(USE_X86LDOUBLE)
777             union {
778                 long double d;
779                 struct {
780                     uint64_t lower;
781                     uint16_t upper;
782                 } l;
783             } tmp;
784             tmp.d = env->fpregs[i].d;
785             cpu_fprintf(f, "FPR%d=%016" PRIx64 " %04x",
786                         i, tmp.l.lower, tmp.l.upper);
787 #else
788             cpu_fprintf(f, "FPR%d=%016" PRIx64,
789                         i, env->fpregs[i].mmx.q);
790 #endif
791             if ((i & 1) == 1)
792                 cpu_fprintf(f, "\n");
793             else
794                 cpu_fprintf(f, " ");
795         }
796         if (env->hflags & HF_CS64_MASK)
797             nb = 16;
798         else
799             nb = 8;
800         for(i=0;i<nb;i++) {
801             cpu_fprintf(f, "XMM%02d=%08x%08x%08x%08x",
802                         i,
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));
807             if ((i & 1) == 1)
808                 cpu_fprintf(f, "\n");
809             else
810                 cpu_fprintf(f, " ");
811         }
812     }
813 }
814
815 /***********************************************************/
816 /* x86 mmu */
817 /* XXX: add PGE support */
818
819 void cpu_x86_set_a20(CPUX86State *env, int a20_state)
820 {
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);
825 #endif
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);
829
830         /* when a20 is changed, all the MMU mappings are invalid, so
831            we must flush everything */
832         tlb_flush(env, 1);
833         env->a20_mask = (~0x100000) | (a20_state << 20);
834     }
835 }
836
837 void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
838 {
839     int pe_state;
840
841 #if defined(DEBUG_MMU)
842     printf("CR0 update: CR0=0x%08x\n", new_cr0);
843 #endif
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))) {
846         tlb_flush(env, 1);
847     }
848
849 #ifdef TARGET_X86_64
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))
855             return;
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)) {
860         /* exit long mode */
861         env->efer &= ~MSR_EFER_LMA;
862         env->hflags &= ~(HF_LMA_MASK | HF_CS64_MASK);
863         env->eip &= 0xffffffff;
864     }
865 #endif
866     env->cr[0] = new_cr0 | CR0_ET_MASK;
867
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));
876 }
877
878 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
879    the PDPT */
880 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
881 {
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);
886 #endif
887         tlb_flush(env, 0);
888     }
889 }
890
891 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
892 {
893 #if defined(DEBUG_MMU)
894     printf("CR4 update: CR4=%08x\n", (uint32_t)env->cr[4]);
895 #endif
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))) {
898         tlb_flush(env, 1);
899     }
900     /* SSE handling */
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;
905     else
906         env->hflags &= ~HF_OSFXSR_MASK;
907
908     env->cr[4] = new_cr4;
909 }
910
911 #if defined(CONFIG_USER_ONLY)
912
913 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
914                              int is_write, int mmu_idx, int is_softmmu)
915 {
916     /* user mode only emulation */
917     is_write &= 1;
918     env->cr[2] = addr;
919     env->error_code = (is_write << PG_ERROR_W_BIT);
920     env->error_code |= PG_ERROR_U_MASK;
921     env->exception_index = EXCP0E_PAGE;
922     return 1;
923 }
924
925 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
926 {
927     return addr;
928 }
929
930 #else
931
932 /* XXX: This value should match the one returned by CPUID
933  * and in exec.c */
934 #if defined(CONFIG_KQEMU)
935 #define PHYS_ADDR_MASK 0xfffff000LL
936 #else
937 # if defined(TARGET_X86_64)
938 # define PHYS_ADDR_MASK 0xfffffff000LL
939 # else
940 # define PHYS_ADDR_MASK 0xffffff000LL
941 # endif
942 #endif
943
944 /* return value:
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
949 */
950 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
951                              int is_write1, int mmu_idx, int is_softmmu)
952 {
953     uint64_t ptep, pte;
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;
959
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);
964 #endif
965     is_write = is_write1 & 1;
966
967     if (!(env->cr[0] & CR0_PG_MASK)) {
968         pte = addr;
969         virt_addr = addr & TARGET_PAGE_MASK;
970         prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
971         page_size = 4096;
972         goto do_mapping;
973     }
974
975     if (env->cr[4] & CR4_PAE_MASK) {
976         uint64_t pde, pdpe;
977         target_ulong pdpe_addr;
978
979 #ifdef TARGET_X86_64
980         if (env->hflags & HF_LMA_MASK) {
981             uint64_t pml4e_addr, pml4e;
982             int32_t sext;
983
984             /* test virtual address sign extension */
985             sext = (int64_t)addr >> 47;
986             if (sext != 0 && sext != -1) {
987                 env->error_code = 0;
988                 env->exception_index = EXCP0D_GPF;
989                 return 1;
990             }
991
992             pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
993                 env->a20_mask;
994             pml4e = ldq_phys(pml4e_addr);
995             if (!(pml4e & PG_PRESENT_MASK)) {
996                 error_code = 0;
997                 goto do_fault;
998             }
999             if (!(env->efer & MSR_EFER_NXE) && (pml4e & PG_NX_MASK)) {
1000                 error_code = PG_ERROR_RSVD_MASK;
1001                 goto do_fault;
1002             }
1003             if (!(pml4e & PG_ACCESSED_MASK)) {
1004                 pml4e |= PG_ACCESSED_MASK;
1005                 stl_phys_notdirty(pml4e_addr, pml4e);
1006             }
1007             ptep = pml4e ^ PG_NX_MASK;
1008             pdpe_addr = ((pml4e & PHYS_ADDR_MASK) + (((addr >> 30) & 0x1ff) << 3)) &
1009                 env->a20_mask;
1010             pdpe = ldq_phys(pdpe_addr);
1011             if (!(pdpe & PG_PRESENT_MASK)) {
1012                 error_code = 0;
1013                 goto do_fault;
1014             }
1015             if (!(env->efer & MSR_EFER_NXE) && (pdpe & PG_NX_MASK)) {
1016                 error_code = PG_ERROR_RSVD_MASK;
1017                 goto do_fault;
1018             }
1019             ptep &= pdpe ^ PG_NX_MASK;
1020             if (!(pdpe & PG_ACCESSED_MASK)) {
1021                 pdpe |= PG_ACCESSED_MASK;
1022                 stl_phys_notdirty(pdpe_addr, pdpe);
1023             }
1024         } else
1025 #endif
1026         {
1027             /* XXX: load them when cr3 is loaded ? */
1028             pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1029                 env->a20_mask;
1030             pdpe = ldq_phys(pdpe_addr);
1031             if (!(pdpe & PG_PRESENT_MASK)) {
1032                 error_code = 0;
1033                 goto do_fault;
1034             }
1035             ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK;
1036         }
1037
1038         pde_addr = ((pdpe & PHYS_ADDR_MASK) + (((addr >> 21) & 0x1ff) << 3)) &
1039             env->a20_mask;
1040         pde = ldq_phys(pde_addr);
1041         if (!(pde & PG_PRESENT_MASK)) {
1042             error_code = 0;
1043             goto do_fault;
1044         }
1045         if (!(env->efer & MSR_EFER_NXE) && (pde & PG_NX_MASK)) {
1046             error_code = PG_ERROR_RSVD_MASK;
1047             goto do_fault;
1048         }
1049         ptep &= pde ^ PG_NX_MASK;
1050         if (pde & PG_PSE_MASK) {
1051             /* 2 MB page */
1052             page_size = 2048 * 1024;
1053             ptep ^= PG_NX_MASK;
1054             if ((ptep & PG_NX_MASK) && is_write1 == 2)
1055                 goto do_fault_protect;
1056             if (is_user) {
1057                 if (!(ptep & PG_USER_MASK))
1058                     goto do_fault_protect;
1059                 if (is_write && !(ptep & PG_RW_MASK))
1060                     goto do_fault_protect;
1061             } else {
1062                 if ((env->cr[0] & CR0_WP_MASK) &&
1063                     is_write && !(ptep & PG_RW_MASK))
1064                     goto do_fault_protect;
1065             }
1066             is_dirty = is_write && !(pde & PG_DIRTY_MASK);
1067             if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1068                 pde |= PG_ACCESSED_MASK;
1069                 if (is_dirty)
1070                     pde |= PG_DIRTY_MASK;
1071                 stl_phys_notdirty(pde_addr, pde);
1072             }
1073             /* align to page_size */
1074             pte = pde & ((PHYS_ADDR_MASK & ~(page_size - 1)) | 0xfff);
1075             virt_addr = addr & ~(page_size - 1);
1076         } else {
1077             /* 4 KB page */
1078             if (!(pde & PG_ACCESSED_MASK)) {
1079                 pde |= PG_ACCESSED_MASK;
1080                 stl_phys_notdirty(pde_addr, pde);
1081             }
1082             pte_addr = ((pde & PHYS_ADDR_MASK) + (((addr >> 12) & 0x1ff) << 3)) &
1083                 env->a20_mask;
1084             pte = ldq_phys(pte_addr);
1085             if (!(pte & PG_PRESENT_MASK)) {
1086                 error_code = 0;
1087                 goto do_fault;
1088             }
1089             if (!(env->efer & MSR_EFER_NXE) && (pte & PG_NX_MASK)) {
1090                 error_code = PG_ERROR_RSVD_MASK;
1091                 goto do_fault;
1092             }
1093             /* combine pde and pte nx, user and rw protections */
1094             ptep &= pte ^ PG_NX_MASK;
1095             ptep ^= PG_NX_MASK;
1096             if ((ptep & PG_NX_MASK) && is_write1 == 2)
1097                 goto do_fault_protect;
1098             if (is_user) {
1099                 if (!(ptep & PG_USER_MASK))
1100                     goto do_fault_protect;
1101                 if (is_write && !(ptep & PG_RW_MASK))
1102                     goto do_fault_protect;
1103             } else {
1104                 if ((env->cr[0] & CR0_WP_MASK) &&
1105                     is_write && !(ptep & PG_RW_MASK))
1106                     goto do_fault_protect;
1107             }
1108             is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1109             if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1110                 pte |= PG_ACCESSED_MASK;
1111                 if (is_dirty)
1112                     pte |= PG_DIRTY_MASK;
1113                 stl_phys_notdirty(pte_addr, pte);
1114             }
1115             page_size = 4096;
1116             virt_addr = addr & ~0xfff;
1117             pte = pte & (PHYS_ADDR_MASK | 0xfff);
1118         }
1119     } else {
1120         uint32_t pde;
1121
1122         /* page directory entry */
1123         pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) &
1124             env->a20_mask;
1125         pde = ldl_phys(pde_addr);
1126         if (!(pde & PG_PRESENT_MASK)) {
1127             error_code = 0;
1128             goto do_fault;
1129         }
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;
1133             if (is_user) {
1134                 if (!(pde & PG_USER_MASK))
1135                     goto do_fault_protect;
1136                 if (is_write && !(pde & PG_RW_MASK))
1137                     goto do_fault_protect;
1138             } else {
1139                 if ((env->cr[0] & CR0_WP_MASK) &&
1140                     is_write && !(pde & PG_RW_MASK))
1141                     goto do_fault_protect;
1142             }
1143             is_dirty = is_write && !(pde & PG_DIRTY_MASK);
1144             if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1145                 pde |= PG_ACCESSED_MASK;
1146                 if (is_dirty)
1147                     pde |= PG_DIRTY_MASK;
1148                 stl_phys_notdirty(pde_addr, pde);
1149             }
1150
1151             pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1152             ptep = pte;
1153             virt_addr = addr & ~(page_size - 1);
1154         } else {
1155             if (!(pde & PG_ACCESSED_MASK)) {
1156                 pde |= PG_ACCESSED_MASK;
1157                 stl_phys_notdirty(pde_addr, pde);
1158             }
1159
1160             /* page directory entry */
1161             pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) &
1162                 env->a20_mask;
1163             pte = ldl_phys(pte_addr);
1164             if (!(pte & PG_PRESENT_MASK)) {
1165                 error_code = 0;
1166                 goto do_fault;
1167             }
1168             /* combine pde and pte user and rw protections */
1169             ptep = pte & pde;
1170             if (is_user) {
1171                 if (!(ptep & PG_USER_MASK))
1172                     goto do_fault_protect;
1173                 if (is_write && !(ptep & PG_RW_MASK))
1174                     goto do_fault_protect;
1175             } else {
1176                 if ((env->cr[0] & CR0_WP_MASK) &&
1177                     is_write && !(ptep & PG_RW_MASK))
1178                     goto do_fault_protect;
1179             }
1180             is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1181             if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1182                 pte |= PG_ACCESSED_MASK;
1183                 if (is_dirty)
1184                     pte |= PG_DIRTY_MASK;
1185                 stl_phys_notdirty(pte_addr, pte);
1186             }
1187             page_size = 4096;
1188             virt_addr = addr & ~0xfff;
1189         }
1190     }
1191     /* the page can be put in the TLB */
1192     prot = PAGE_READ;
1193     if (!(ptep & PG_NX_MASK))
1194         prot |= PAGE_EXEC;
1195     if (pte & PG_DIRTY_MASK) {
1196         /* only set write access if already dirty... otherwise wait
1197            for dirty access */
1198         if (is_user) {
1199             if (ptep & PG_RW_MASK)
1200                 prot |= PAGE_WRITE;
1201         } else {
1202             if (!(env->cr[0] & CR0_WP_MASK) ||
1203                 (ptep & PG_RW_MASK))
1204                 prot |= PAGE_WRITE;
1205         }
1206     }
1207  do_mapping:
1208     pte = pte & env->a20_mask;
1209
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;
1215
1216     ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
1217     return ret;
1218  do_fault_protect:
1219     error_code = PG_ERROR_P_MASK;
1220  do_fault:
1221     error_code |= (is_write << PG_ERROR_W_BIT);
1222     if (is_user)
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), 
1231                  addr);
1232     } else {
1233         env->cr[2] = addr;
1234     }
1235     env->error_code = error_code;
1236     env->exception_index = EXCP0E_PAGE;
1237     return 1;
1238 }
1239
1240 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
1241 {
1242     target_ulong pde_addr, pte_addr;
1243     uint64_t pte;
1244     target_phys_addr_t paddr;
1245     uint32_t page_offset;
1246     int page_size;
1247
1248     if (env->cr[4] & CR4_PAE_MASK) {
1249         target_ulong pdpe_addr;
1250         uint64_t pde, pdpe;
1251
1252 #ifdef TARGET_X86_64
1253         if (env->hflags & HF_LMA_MASK) {
1254             uint64_t pml4e_addr, pml4e;
1255             int32_t sext;
1256
1257             /* test virtual address sign extension */
1258             sext = (int64_t)addr >> 47;
1259             if (sext != 0 && sext != -1)
1260                 return -1;
1261
1262             pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
1263                 env->a20_mask;
1264             pml4e = ldq_phys(pml4e_addr);
1265             if (!(pml4e & PG_PRESENT_MASK))
1266                 return -1;
1267
1268             pdpe_addr = ((pml4e & ~0xfff) + (((addr >> 30) & 0x1ff) << 3)) &
1269                 env->a20_mask;
1270             pdpe = ldq_phys(pdpe_addr);
1271             if (!(pdpe & PG_PRESENT_MASK))
1272                 return -1;
1273         } else
1274 #endif
1275         {
1276             pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1277                 env->a20_mask;
1278             pdpe = ldq_phys(pdpe_addr);
1279             if (!(pdpe & PG_PRESENT_MASK))
1280                 return -1;
1281         }
1282
1283         pde_addr = ((pdpe & ~0xfff) + (((addr >> 21) & 0x1ff) << 3)) &
1284             env->a20_mask;
1285         pde = ldq_phys(pde_addr);
1286         if (!(pde & PG_PRESENT_MASK)) {
1287             return -1;
1288         }
1289         if (pde & PG_PSE_MASK) {
1290             /* 2 MB page */
1291             page_size = 2048 * 1024;
1292             pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1293         } else {
1294             /* 4 KB page */
1295             pte_addr = ((pde & ~0xfff) + (((addr >> 12) & 0x1ff) << 3)) &
1296                 env->a20_mask;
1297             page_size = 4096;
1298             pte = ldq_phys(pte_addr);
1299         }
1300         if (!(pte & PG_PRESENT_MASK))
1301             return -1;
1302     } else {
1303         uint32_t pde;
1304
1305         if (!(env->cr[0] & CR0_PG_MASK)) {
1306             pte = addr;
1307             page_size = 4096;
1308         } else {
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))
1313                 return -1;
1314             if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1315                 pte = pde & ~0x003ff000; /* align to 4MB */
1316                 page_size = 4096 * 1024;
1317             } else {
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))
1322                     return -1;
1323                 page_size = 4096;
1324             }
1325         }
1326         pte = pte & env->a20_mask;
1327     }
1328
1329     page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1330     paddr = (pte & TARGET_PAGE_MASK) + page_offset;
1331     return paddr;
1332 }
1333
1334 void hw_breakpoint_insert(CPUState *env, int index)
1335 {
1336     int type, err = 0;
1337
1338     switch (hw_breakpoint_type(env->dr[7], index)) {
1339     case 0:
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]);
1343         break;
1344     case 1:
1345         type = BP_CPU | BP_MEM_WRITE;
1346         goto insert_wp;
1347     case 2:
1348          /* No support for I/O watchpoints yet */
1349         break;
1350     case 3:
1351         type = BP_CPU | BP_MEM_ACCESS;
1352     insert_wp:
1353         err = cpu_watchpoint_insert(env, env->dr[index],
1354                                     hw_breakpoint_len(env->dr[7], index),
1355                                     type, &env->cpu_watchpoint[index]);
1356         break;
1357     }
1358     if (err)
1359         env->cpu_breakpoint[index] = NULL;
1360 }
1361
1362 void hw_breakpoint_remove(CPUState *env, int index)
1363 {
1364     if (!env->cpu_breakpoint[index])
1365         return;
1366     switch (hw_breakpoint_type(env->dr[7], index)) {
1367     case 0:
1368         if (hw_breakpoint_enabled(env->dr[7], index))
1369             cpu_breakpoint_remove_by_ref(env, env->cpu_breakpoint[index]);
1370         break;
1371     case 1:
1372     case 3:
1373         cpu_watchpoint_remove_by_ref(env, env->cpu_watchpoint[index]);
1374         break;
1375     case 2:
1376         /* No support for I/O watchpoints yet */
1377         break;
1378     }
1379 }
1380
1381 int check_hw_breakpoints(CPUState *env, int force_dr6_update)
1382 {
1383     target_ulong dr6;
1384     int reg, type;
1385     int hit_enabled = 0;
1386
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))) {
1393             dr6 |= 1 << reg;
1394             if (hw_breakpoint_enabled(env->dr[7], reg))
1395                 hit_enabled = 1;
1396         }
1397     }
1398     if (hit_enabled || force_dr6_update)
1399         env->dr[6] = dr6;
1400     return hit_enabled;
1401 }
1402
1403 static CPUDebugExcpHandler *prev_debug_excp_handler;
1404
1405 void raise_exception(int exception_index);
1406
1407 static void breakpoint_handler(CPUState *env)
1408 {
1409     CPUBreakpoint *bp;
1410
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);
1416             else
1417                 cpu_resume_from_signal(env, NULL);
1418         }
1419     } else {
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);
1425                 }
1426                 break;
1427             }
1428     }
1429     if (prev_debug_excp_handler)
1430         prev_debug_excp_handler(env);
1431 }
1432 #endif /* !CONFIG_USER_ONLY */
1433
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)
1437 {
1438 #if defined(CONFIG_KVM)
1439     uint32_t vec[4];
1440
1441 #ifdef __x86_64__
1442     asm volatile("cpuid"
1443                  : "=a"(vec[0]), "=b"(vec[1]),
1444                    "=c"(vec[2]), "=d"(vec[3])
1445                  : "0"(function), "c"(count) : "cc");
1446 #else
1447     asm volatile("pusha \n\t"
1448                  "cpuid \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"
1453                  "popa"
1454                  : : "a"(function), "c"(count), "S"(vec)
1455                  : "memory", "cc");
1456 #endif
1457
1458     if (eax)
1459         *eax = vec[0];
1460     if (ebx)
1461         *ebx = vec[1];
1462     if (ecx)
1463         *ecx = vec[2];
1464     if (edx)
1465         *edx = vec[3];
1466 #endif
1467 }
1468
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)
1472 {
1473     /* test if maximum index reached */
1474     if (index & 0x80000000) {
1475         if (index > env->cpuid_xlevel)
1476             index = env->cpuid_level;
1477     } else {
1478         if (index > env->cpuid_level)
1479             index = env->cpuid_level;
1480     }
1481
1482     switch(index) {
1483     case 0:
1484         *eax = env->cpuid_level;
1485         *ebx = env->cpuid_vendor1;
1486         *edx = env->cpuid_vendor2;
1487         *ecx = env->cpuid_vendor3;
1488
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. */
1493         if (kvm_enabled())
1494             host_cpuid(0, 0, NULL, ebx, ecx, edx);
1495         break;
1496     case 1:
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;
1501
1502         /* "Hypervisor present" bit required for Microsoft SVVP */
1503         if (kvm_enabled())
1504             *ecx |= (1 << 31);
1505         break;
1506     case 2:
1507         /* cache info: needed for Pentium Pro compatibility */
1508         *eax = 1;
1509         *ebx = 0;
1510         *ecx = 0;
1511         *edx = 0x2c307d;
1512         break;
1513     case 4:
1514         /* cache info: needed for Core compatibility */
1515         switch (count) {
1516             case 0: /* L1 dcache info */
1517                 *eax = 0x0000121;
1518                 *ebx = 0x1c0003f;
1519                 *ecx = 0x000003f;
1520                 *edx = 0x0000001;
1521                 break;
1522             case 1: /* L1 icache info */
1523                 *eax = 0x0000122;
1524                 *ebx = 0x1c0003f;
1525                 *ecx = 0x000003f;
1526                 *edx = 0x0000001;
1527                 break;
1528             case 2: /* L2 cache info */
1529                 *eax = 0x0000143;
1530                 *ebx = 0x3c0003f;
1531                 *ecx = 0x0000fff;
1532                 *edx = 0x0000001;
1533                 break;
1534             default: /* end of info */
1535                 *eax = 0;
1536                 *ebx = 0;
1537                 *ecx = 0;
1538                 *edx = 0;
1539                 break;
1540         }
1541         break;
1542     case 5:
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;
1547         *edx = 0;
1548         break;
1549     case 6:
1550         /* Thermal and Power Leaf */
1551         *eax = 0;
1552         *ebx = 0;
1553         *ecx = 0;
1554         *edx = 0;
1555         break;
1556     case 9:
1557         /* Direct Cache Access Information Leaf */
1558         *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1559         *ebx = 0;
1560         *ecx = 0;
1561         *edx = 0;
1562         break;
1563     case 0xA:
1564         /* Architectural Performance Monitoring Leaf */
1565         *eax = 0;
1566         *ebx = 0;
1567         *ecx = 0;
1568         *edx = 0;
1569         break;
1570     case 0x80000000:
1571         *eax = env->cpuid_xlevel;
1572         *ebx = env->cpuid_vendor1;
1573         *edx = env->cpuid_vendor2;
1574         *ecx = env->cpuid_vendor3;
1575         break;
1576     case 0x80000001:
1577         *eax = env->cpuid_features;
1578         *ebx = 0;
1579         *ecx = env->cpuid_ext3_features;
1580         *edx = env->cpuid_ext2_features;
1581
1582         if (kvm_enabled()) {
1583             uint32_t h_eax, h_edx;
1584
1585             host_cpuid(index, 0, &h_eax, NULL, NULL, &h_edx);
1586
1587             /* disable CPU features that the host does not support */
1588
1589             /* long mode */
1590             if ((h_edx & 0x20000000) == 0 /* || !lm_capable_kernel */)
1591                 *edx &= ~0x20000000;
1592             /* syscall */
1593             if ((h_edx & 0x00000800) == 0)
1594                 *edx &= ~0x00000800;
1595             /* nx */
1596             if ((h_edx & 0x00100000) == 0)
1597                 *edx &= ~0x00100000;
1598
1599             /* disable CPU features that KVM cannot support */
1600
1601             /* svm */
1602             *ecx &= ~4UL;
1603             /* 3dnow */
1604             *edx &= ~0xc0000000;
1605         }
1606         break;
1607     case 0x80000002:
1608     case 0x80000003:
1609     case 0x80000004:
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];
1614         break;
1615     case 0x80000005:
1616         /* cache info (L1 cache) */
1617         *eax = 0x01ff01ff;
1618         *ebx = 0x01ff01ff;
1619         *ecx = 0x40020140;
1620         *edx = 0x40020140;
1621         break;
1622     case 0x80000006:
1623         /* cache info (L2 cache) */
1624         *eax = 0;
1625         *ebx = 0x42004200;
1626         *ecx = 0x02008140;
1627         *edx = 0;
1628         break;
1629     case 0x80000008:
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 */
1636 #else
1637 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1638             *eax = 0x00003028;  /* 48 bits virtual, 40 bits physical */
1639 #endif
1640         } else {
1641 #if defined(CONFIG_KQEMU)
1642             *eax = 0x00000020;  /* 32 bits physical */
1643 #else
1644             if (env->cpuid_features & CPUID_PSE36)
1645                 *eax = 0x00000024; /* 36 bits physical */
1646             else
1647                 *eax = 0x00000020; /* 32 bits physical */
1648 #endif
1649         }
1650         *ebx = 0;
1651         *ecx = 0;
1652         *edx = 0;
1653         break;
1654     case 0x8000000A:
1655         *eax = 0x00000001; /* SVM Revision */
1656         *ebx = 0x00000010; /* nr of ASIDs */
1657         *ecx = 0;
1658         *edx = 0; /* optional features */
1659         break;
1660     default:
1661         /* reserved values: zero */
1662         *eax = 0;
1663         *ebx = 0;
1664         *ecx = 0;
1665         *edx = 0;
1666         break;
1667     }
1668 }
1669
1670 CPUX86State *cpu_x86_init(const char *cpu_model)
1671 {
1672     CPUX86State *env;
1673     static int inited;
1674
1675     env = qemu_mallocz(sizeof(CPUX86State));
1676     cpu_exec_init(env);
1677     env->cpu_model_str = cpu_model;
1678
1679     /* init various static tables */
1680     if (!inited) {
1681         inited = 1;
1682         optimize_flags_init();
1683 #ifndef CONFIG_USER_ONLY
1684         prev_debug_excp_handler =
1685             cpu_set_debug_excp_handler(breakpoint_handler);
1686 #endif
1687     }
1688     if (cpu_x86_register(env, cpu_model) < 0) {
1689         cpu_x86_close(env);
1690         return NULL;
1691     }
1692     cpu_reset(env);
1693 #ifdef CONFIG_KQEMU
1694     kqemu_init(env);
1695 #endif
1696
1697     qemu_init_vcpu(env);
1698
1699     return env;
1700 }