]> git.proxmox.com Git - qemu.git/blob - target-i386/helper.c
kvm: Trim cpu features not supported by kvm
[qemu.git] / 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 int found = 0;
70
71 for ( i = 0 ; i < 32 ; i++ )
72 if (feature_name[i] && !strcmp (flagname, feature_name[i])) {
73 *features |= 1 << i;
74 found = 1;
75 }
76 for ( i = 0 ; i < 32 ; i++ )
77 if (ext_feature_name[i] && !strcmp (flagname, ext_feature_name[i])) {
78 *ext_features |= 1 << i;
79 found = 1;
80 }
81 for ( i = 0 ; i < 32 ; i++ )
82 if (ext2_feature_name[i] && !strcmp (flagname, ext2_feature_name[i])) {
83 *ext2_features |= 1 << i;
84 found = 1;
85 }
86 for ( i = 0 ; i < 32 ; i++ )
87 if (ext3_feature_name[i] && !strcmp (flagname, ext3_feature_name[i])) {
88 *ext3_features |= 1 << i;
89 found = 1;
90 }
91 if (!found) {
92 fprintf(stderr, "CPU feature %s not found\n", flagname);
93 }
94 }
95
96 static void kvm_trim_features(uint32_t *features, uint32_t supported,
97 const char *names[])
98 {
99 int i;
100 uint32_t mask;
101
102 for (i = 0; i < 32; ++i) {
103 mask = 1U << i;
104 if ((*features & mask) && !(supported & mask)) {
105 printf("Processor feature %s not supported by kvm\n", names[i]);
106 *features &= ~mask;
107 }
108 }
109 }
110
111 typedef struct x86_def_t {
112 const char *name;
113 uint32_t level;
114 uint32_t vendor1, vendor2, vendor3;
115 int family;
116 int model;
117 int stepping;
118 uint32_t features, ext_features, ext2_features, ext3_features;
119 uint32_t xlevel;
120 char model_id[48];
121 } x86_def_t;
122
123 #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
124 #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
125 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX)
126 #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
127 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
128 CPUID_PSE36 | CPUID_FXSR)
129 #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
130 #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
131 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
132 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
133 CPUID_PAE | CPUID_SEP | CPUID_APIC)
134 static x86_def_t x86_defs[] = {
135 #ifdef TARGET_X86_64
136 {
137 .name = "qemu64",
138 .level = 2,
139 .vendor1 = CPUID_VENDOR_AMD_1,
140 .vendor2 = CPUID_VENDOR_AMD_2,
141 .vendor3 = CPUID_VENDOR_AMD_3,
142 .family = 6,
143 .model = 2,
144 .stepping = 3,
145 .features = PPRO_FEATURES |
146 /* these features are needed for Win64 and aren't fully implemented */
147 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
148 /* this feature is needed for Solaris and isn't fully implemented */
149 CPUID_PSE36,
150 .ext_features = CPUID_EXT_SSE3,
151 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
152 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
153 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
154 .ext3_features = CPUID_EXT3_SVM,
155 .xlevel = 0x8000000A,
156 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
157 },
158 {
159 .name = "phenom",
160 .level = 5,
161 .vendor1 = CPUID_VENDOR_AMD_1,
162 .vendor2 = CPUID_VENDOR_AMD_2,
163 .vendor3 = CPUID_VENDOR_AMD_3,
164 .family = 16,
165 .model = 2,
166 .stepping = 3,
167 /* Missing: CPUID_VME, CPUID_HT */
168 .features = PPRO_FEATURES |
169 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
170 CPUID_PSE36,
171 /* Missing: CPUID_EXT_CX16, CPUID_EXT_POPCNT */
172 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR,
173 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
174 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
175 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
176 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
177 CPUID_EXT2_FFXSR,
178 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
179 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
180 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
181 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
182 .ext3_features = CPUID_EXT3_SVM,
183 .xlevel = 0x8000001A,
184 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
185 },
186 {
187 .name = "core2duo",
188 .level = 10,
189 .family = 6,
190 .model = 15,
191 .stepping = 11,
192 /* The original CPU also implements these features:
193 CPUID_VME, CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
194 CPUID_TM, CPUID_PBE */
195 .features = PPRO_FEATURES |
196 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
197 CPUID_PSE36,
198 /* The original CPU also implements these ext features:
199 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
200 CPUID_EXT_TM2, CPUID_EXT_CX16, CPUID_EXT_XTPR, CPUID_EXT_PDCM */
201 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3,
202 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
203 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
204 .xlevel = 0x80000008,
205 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
206 },
207 #endif
208 {
209 .name = "qemu32",
210 .level = 2,
211 .family = 6,
212 .model = 3,
213 .stepping = 3,
214 .features = PPRO_FEATURES,
215 .ext_features = CPUID_EXT_SSE3,
216 .xlevel = 0,
217 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
218 },
219 {
220 .name = "coreduo",
221 .level = 10,
222 .family = 6,
223 .model = 14,
224 .stepping = 8,
225 /* The original CPU also implements these features:
226 CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
227 CPUID_TM, CPUID_PBE */
228 .features = PPRO_FEATURES | CPUID_VME |
229 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA,
230 /* The original CPU also implements these ext features:
231 CPUID_EXT_VMX, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_XTPR,
232 CPUID_EXT_PDCM */
233 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR,
234 .ext2_features = CPUID_EXT2_NX,
235 .xlevel = 0x80000008,
236 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
237 },
238 {
239 .name = "486",
240 .level = 0,
241 .family = 4,
242 .model = 0,
243 .stepping = 0,
244 .features = I486_FEATURES,
245 .xlevel = 0,
246 },
247 {
248 .name = "pentium",
249 .level = 1,
250 .family = 5,
251 .model = 4,
252 .stepping = 3,
253 .features = PENTIUM_FEATURES,
254 .xlevel = 0,
255 },
256 {
257 .name = "pentium2",
258 .level = 2,
259 .family = 6,
260 .model = 5,
261 .stepping = 2,
262 .features = PENTIUM2_FEATURES,
263 .xlevel = 0,
264 },
265 {
266 .name = "pentium3",
267 .level = 2,
268 .family = 6,
269 .model = 7,
270 .stepping = 3,
271 .features = PENTIUM3_FEATURES,
272 .xlevel = 0,
273 },
274 {
275 .name = "athlon",
276 .level = 2,
277 .vendor1 = 0x68747541, /* "Auth" */
278 .vendor2 = 0x69746e65, /* "enti" */
279 .vendor3 = 0x444d4163, /* "cAMD" */
280 .family = 6,
281 .model = 2,
282 .stepping = 3,
283 .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR | CPUID_MCA,
284 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) | CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
285 .xlevel = 0x80000008,
286 /* XXX: put another string ? */
287 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
288 },
289 {
290 .name = "n270",
291 /* original is on level 10 */
292 .level = 5,
293 .family = 6,
294 .model = 28,
295 .stepping = 2,
296 .features = PPRO_FEATURES |
297 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME,
298 /* Missing: CPUID_DTS | CPUID_ACPI | CPUID_SS |
299 * CPUID_HT | CPUID_TM | CPUID_PBE */
300 /* Some CPUs got no CPUID_SEP */
301 .ext_features = CPUID_EXT_MONITOR |
302 CPUID_EXT_SSE3 /* PNI */ | CPUID_EXT_SSSE3,
303 /* Missing: CPUID_EXT_DSCPL | CPUID_EXT_EST |
304 * CPUID_EXT_TM2 | CPUID_EXT_XTPR */
305 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) | CPUID_EXT2_NX,
306 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
307 .xlevel = 0x8000000A,
308 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
309 },
310 };
311
312 static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
313 {
314 unsigned int i;
315 x86_def_t *def;
316
317 char *s = strdup(cpu_model);
318 char *featurestr, *name = strtok(s, ",");
319 uint32_t plus_features = 0, plus_ext_features = 0, plus_ext2_features = 0, plus_ext3_features = 0;
320 uint32_t minus_features = 0, minus_ext_features = 0, minus_ext2_features = 0, minus_ext3_features = 0;
321 int family = -1, model = -1, stepping = -1;
322
323 def = NULL;
324 for (i = 0; i < ARRAY_SIZE(x86_defs); i++) {
325 if (strcmp(name, x86_defs[i].name) == 0) {
326 def = &x86_defs[i];
327 break;
328 }
329 }
330 if (!def)
331 goto error;
332 memcpy(x86_cpu_def, def, sizeof(*def));
333
334 featurestr = strtok(NULL, ",");
335
336 while (featurestr) {
337 char *val;
338 if (featurestr[0] == '+') {
339 add_flagname_to_bitmaps(featurestr + 1, &plus_features, &plus_ext_features, &plus_ext2_features, &plus_ext3_features);
340 } else if (featurestr[0] == '-') {
341 add_flagname_to_bitmaps(featurestr + 1, &minus_features, &minus_ext_features, &minus_ext2_features, &minus_ext3_features);
342 } else if ((val = strchr(featurestr, '='))) {
343 *val = 0; val++;
344 if (!strcmp(featurestr, "family")) {
345 char *err;
346 family = strtol(val, &err, 10);
347 if (!*val || *err || family < 0) {
348 fprintf(stderr, "bad numerical value %s\n", val);
349 goto error;
350 }
351 x86_cpu_def->family = family;
352 } else if (!strcmp(featurestr, "model")) {
353 char *err;
354 model = strtol(val, &err, 10);
355 if (!*val || *err || model < 0 || model > 0xff) {
356 fprintf(stderr, "bad numerical value %s\n", val);
357 goto error;
358 }
359 x86_cpu_def->model = model;
360 } else if (!strcmp(featurestr, "stepping")) {
361 char *err;
362 stepping = strtol(val, &err, 10);
363 if (!*val || *err || stepping < 0 || stepping > 0xf) {
364 fprintf(stderr, "bad numerical value %s\n", val);
365 goto error;
366 }
367 x86_cpu_def->stepping = stepping;
368 } else if (!strcmp(featurestr, "vendor")) {
369 if (strlen(val) != 12) {
370 fprintf(stderr, "vendor string must be 12 chars long\n");
371 goto error;
372 }
373 x86_cpu_def->vendor1 = 0;
374 x86_cpu_def->vendor2 = 0;
375 x86_cpu_def->vendor3 = 0;
376 for(i = 0; i < 4; i++) {
377 x86_cpu_def->vendor1 |= ((uint8_t)val[i ]) << (8 * i);
378 x86_cpu_def->vendor2 |= ((uint8_t)val[i + 4]) << (8 * i);
379 x86_cpu_def->vendor3 |= ((uint8_t)val[i + 8]) << (8 * i);
380 }
381 } else if (!strcmp(featurestr, "model_id")) {
382 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
383 val);
384 } else {
385 fprintf(stderr, "unrecognized feature %s\n", featurestr);
386 goto error;
387 }
388 } else {
389 fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
390 goto error;
391 }
392 featurestr = strtok(NULL, ",");
393 }
394 x86_cpu_def->features |= plus_features;
395 x86_cpu_def->ext_features |= plus_ext_features;
396 x86_cpu_def->ext2_features |= plus_ext2_features;
397 x86_cpu_def->ext3_features |= plus_ext3_features;
398 x86_cpu_def->features &= ~minus_features;
399 x86_cpu_def->ext_features &= ~minus_ext_features;
400 x86_cpu_def->ext2_features &= ~minus_ext2_features;
401 x86_cpu_def->ext3_features &= ~minus_ext3_features;
402 free(s);
403 return 0;
404
405 error:
406 free(s);
407 return -1;
408 }
409
410 void x86_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
411 {
412 unsigned int i;
413
414 for (i = 0; i < ARRAY_SIZE(x86_defs); i++)
415 (*cpu_fprintf)(f, "x86 %16s\n", x86_defs[i].name);
416 }
417
418 static int cpu_x86_register (CPUX86State *env, const char *cpu_model)
419 {
420 x86_def_t def1, *def = &def1;
421
422 if (cpu_x86_find_by_name(def, cpu_model) < 0)
423 return -1;
424 if (def->vendor1) {
425 env->cpuid_vendor1 = def->vendor1;
426 env->cpuid_vendor2 = def->vendor2;
427 env->cpuid_vendor3 = def->vendor3;
428 } else {
429 env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1;
430 env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2;
431 env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3;
432 }
433 env->cpuid_level = def->level;
434 if (def->family > 0x0f)
435 env->cpuid_version = 0xf00 | ((def->family - 0x0f) << 20);
436 else
437 env->cpuid_version = def->family << 8;
438 env->cpuid_version |= ((def->model & 0xf) << 4) | ((def->model >> 4) << 16);
439 env->cpuid_version |= def->stepping;
440 env->cpuid_features = def->features;
441 env->pat = 0x0007040600070406ULL;
442 env->cpuid_ext_features = def->ext_features;
443 env->cpuid_ext2_features = def->ext2_features;
444 env->cpuid_xlevel = def->xlevel;
445 env->cpuid_ext3_features = def->ext3_features;
446 {
447 const char *model_id = def->model_id;
448 int c, len, i;
449 if (!model_id)
450 model_id = "";
451 len = strlen(model_id);
452 for(i = 0; i < 48; i++) {
453 if (i >= len)
454 c = '\0';
455 else
456 c = (uint8_t)model_id[i];
457 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
458 }
459 }
460 return 0;
461 }
462
463 /* NOTE: must be called outside the CPU execute loop */
464 void cpu_reset(CPUX86State *env)
465 {
466 int i;
467
468 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
469 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
470 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
471 }
472
473 memset(env, 0, offsetof(CPUX86State, breakpoints));
474
475 tlb_flush(env, 1);
476
477 env->old_exception = -1;
478
479 /* init to reset state */
480
481 #ifdef CONFIG_SOFTMMU
482 env->hflags |= HF_SOFTMMU_MASK;
483 #endif
484 env->hflags2 |= HF2_GIF_MASK;
485
486 cpu_x86_update_cr0(env, 0x60000010);
487 env->a20_mask = ~0x0;
488 env->smbase = 0x30000;
489
490 env->idt.limit = 0xffff;
491 env->gdt.limit = 0xffff;
492 env->ldt.limit = 0xffff;
493 env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
494 env->tr.limit = 0xffff;
495 env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
496
497 cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
498 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK | DESC_R_MASK);
499 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
500 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
501 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
502 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
503 cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
504 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
505 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
506 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
507 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
508 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
509
510 env->eip = 0xfff0;
511 env->regs[R_EDX] = env->cpuid_version;
512
513 env->eflags = 0x2;
514
515 /* FPU init */
516 for(i = 0;i < 8; i++)
517 env->fptags[i] = 1;
518 env->fpuc = 0x37f;
519
520 env->mxcsr = 0x1f80;
521
522 memset(env->dr, 0, sizeof(env->dr));
523 env->dr[6] = DR6_FIXED_1;
524 env->dr[7] = DR7_FIXED_1;
525 cpu_breakpoint_remove_all(env, BP_CPU);
526 cpu_watchpoint_remove_all(env, BP_CPU);
527 }
528
529 void cpu_x86_close(CPUX86State *env)
530 {
531 qemu_free(env);
532 }
533
534 /***********************************************************/
535 /* x86 debug */
536
537 static const char *cc_op_str[] = {
538 "DYNAMIC",
539 "EFLAGS",
540
541 "MULB",
542 "MULW",
543 "MULL",
544 "MULQ",
545
546 "ADDB",
547 "ADDW",
548 "ADDL",
549 "ADDQ",
550
551 "ADCB",
552 "ADCW",
553 "ADCL",
554 "ADCQ",
555
556 "SUBB",
557 "SUBW",
558 "SUBL",
559 "SUBQ",
560
561 "SBBB",
562 "SBBW",
563 "SBBL",
564 "SBBQ",
565
566 "LOGICB",
567 "LOGICW",
568 "LOGICL",
569 "LOGICQ",
570
571 "INCB",
572 "INCW",
573 "INCL",
574 "INCQ",
575
576 "DECB",
577 "DECW",
578 "DECL",
579 "DECQ",
580
581 "SHLB",
582 "SHLW",
583 "SHLL",
584 "SHLQ",
585
586 "SARB",
587 "SARW",
588 "SARL",
589 "SARQ",
590 };
591
592 static void
593 cpu_x86_dump_seg_cache(CPUState *env, FILE *f,
594 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
595 const char *name, struct SegmentCache *sc)
596 {
597 #ifdef TARGET_X86_64
598 if (env->hflags & HF_CS64_MASK) {
599 cpu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name,
600 sc->selector, sc->base, sc->limit, sc->flags);
601 } else
602 #endif
603 {
604 cpu_fprintf(f, "%-3s=%04x %08x %08x %08x", name, sc->selector,
605 (uint32_t)sc->base, sc->limit, sc->flags);
606 }
607
608 if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK))
609 goto done;
610
611 cpu_fprintf(f, " DPL=%d ", (sc->flags & DESC_DPL_MASK) >> DESC_DPL_SHIFT);
612 if (sc->flags & DESC_S_MASK) {
613 if (sc->flags & DESC_CS_MASK) {
614 cpu_fprintf(f, (sc->flags & DESC_L_MASK) ? "CS64" :
615 ((sc->flags & DESC_B_MASK) ? "CS32" : "CS16"));
616 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_C_MASK) ? 'C' : '-',
617 (sc->flags & DESC_R_MASK) ? 'R' : '-');
618 } else {
619 cpu_fprintf(f, (sc->flags & DESC_B_MASK) ? "DS " : "DS16");
620 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_E_MASK) ? 'E' : '-',
621 (sc->flags & DESC_W_MASK) ? 'W' : '-');
622 }
623 cpu_fprintf(f, "%c]", (sc->flags & DESC_A_MASK) ? 'A' : '-');
624 } else {
625 static const char *sys_type_name[2][16] = {
626 { /* 32 bit mode */
627 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
628 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
629 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
630 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
631 },
632 { /* 64 bit mode */
633 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
634 "Reserved", "Reserved", "Reserved", "Reserved",
635 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
636 "Reserved", "IntGate64", "TrapGate64"
637 }
638 };
639 cpu_fprintf(f, sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0]
640 [(sc->flags & DESC_TYPE_MASK)
641 >> DESC_TYPE_SHIFT]);
642 }
643 done:
644 cpu_fprintf(f, "\n");
645 }
646
647 void cpu_dump_state(CPUState *env, FILE *f,
648 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
649 int flags)
650 {
651 int eflags, i, nb;
652 char cc_op_name[32];
653 static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
654
655 if (kvm_enabled())
656 kvm_arch_get_registers(env);
657
658 eflags = env->eflags;
659 #ifdef TARGET_X86_64
660 if (env->hflags & HF_CS64_MASK) {
661 cpu_fprintf(f,
662 "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n"
663 "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n"
664 "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n"
665 "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n"
666 "RIP=%016" PRIx64 " RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
667 env->regs[R_EAX],
668 env->regs[R_EBX],
669 env->regs[R_ECX],
670 env->regs[R_EDX],
671 env->regs[R_ESI],
672 env->regs[R_EDI],
673 env->regs[R_EBP],
674 env->regs[R_ESP],
675 env->regs[8],
676 env->regs[9],
677 env->regs[10],
678 env->regs[11],
679 env->regs[12],
680 env->regs[13],
681 env->regs[14],
682 env->regs[15],
683 env->eip, eflags,
684 eflags & DF_MASK ? 'D' : '-',
685 eflags & CC_O ? 'O' : '-',
686 eflags & CC_S ? 'S' : '-',
687 eflags & CC_Z ? 'Z' : '-',
688 eflags & CC_A ? 'A' : '-',
689 eflags & CC_P ? 'P' : '-',
690 eflags & CC_C ? 'C' : '-',
691 env->hflags & HF_CPL_MASK,
692 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
693 (int)(env->a20_mask >> 20) & 1,
694 (env->hflags >> HF_SMM_SHIFT) & 1,
695 env->halted);
696 } else
697 #endif
698 {
699 cpu_fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
700 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
701 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
702 (uint32_t)env->regs[R_EAX],
703 (uint32_t)env->regs[R_EBX],
704 (uint32_t)env->regs[R_ECX],
705 (uint32_t)env->regs[R_EDX],
706 (uint32_t)env->regs[R_ESI],
707 (uint32_t)env->regs[R_EDI],
708 (uint32_t)env->regs[R_EBP],
709 (uint32_t)env->regs[R_ESP],
710 (uint32_t)env->eip, eflags,
711 eflags & DF_MASK ? 'D' : '-',
712 eflags & CC_O ? 'O' : '-',
713 eflags & CC_S ? 'S' : '-',
714 eflags & CC_Z ? 'Z' : '-',
715 eflags & CC_A ? 'A' : '-',
716 eflags & CC_P ? 'P' : '-',
717 eflags & CC_C ? 'C' : '-',
718 env->hflags & HF_CPL_MASK,
719 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
720 (int)(env->a20_mask >> 20) & 1,
721 (env->hflags >> HF_SMM_SHIFT) & 1,
722 env->halted);
723 }
724
725 for(i = 0; i < 6; i++) {
726 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, seg_name[i],
727 &env->segs[i]);
728 }
729 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "LDT", &env->ldt);
730 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "TR", &env->tr);
731
732 #ifdef TARGET_X86_64
733 if (env->hflags & HF_LMA_MASK) {
734 cpu_fprintf(f, "GDT= %016" PRIx64 " %08x\n",
735 env->gdt.base, env->gdt.limit);
736 cpu_fprintf(f, "IDT= %016" PRIx64 " %08x\n",
737 env->idt.base, env->idt.limit);
738 cpu_fprintf(f, "CR0=%08x CR2=%016" PRIx64 " CR3=%016" PRIx64 " CR4=%08x\n",
739 (uint32_t)env->cr[0],
740 env->cr[2],
741 env->cr[3],
742 (uint32_t)env->cr[4]);
743 for(i = 0; i < 4; i++)
744 cpu_fprintf(f, "DR%d=%016" PRIx64 " ", i, env->dr[i]);
745 cpu_fprintf(f, "\nDR6=%016" PRIx64 " DR7=%016" PRIx64 "\n",
746 env->dr[6], env->dr[7]);
747 } else
748 #endif
749 {
750 cpu_fprintf(f, "GDT= %08x %08x\n",
751 (uint32_t)env->gdt.base, env->gdt.limit);
752 cpu_fprintf(f, "IDT= %08x %08x\n",
753 (uint32_t)env->idt.base, env->idt.limit);
754 cpu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
755 (uint32_t)env->cr[0],
756 (uint32_t)env->cr[2],
757 (uint32_t)env->cr[3],
758 (uint32_t)env->cr[4]);
759 for(i = 0; i < 4; i++)
760 cpu_fprintf(f, "DR%d=%08x ", i, env->dr[i]);
761 cpu_fprintf(f, "\nDR6=%08x DR7=%08x\n", env->dr[6], env->dr[7]);
762 }
763 if (flags & X86_DUMP_CCOP) {
764 if ((unsigned)env->cc_op < CC_OP_NB)
765 snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]);
766 else
767 snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
768 #ifdef TARGET_X86_64
769 if (env->hflags & HF_CS64_MASK) {
770 cpu_fprintf(f, "CCS=%016" PRIx64 " CCD=%016" PRIx64 " CCO=%-8s\n",
771 env->cc_src, env->cc_dst,
772 cc_op_name);
773 } else
774 #endif
775 {
776 cpu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
777 (uint32_t)env->cc_src, (uint32_t)env->cc_dst,
778 cc_op_name);
779 }
780 }
781 if (flags & X86_DUMP_FPU) {
782 int fptag;
783 fptag = 0;
784 for(i = 0; i < 8; i++) {
785 fptag |= ((!env->fptags[i]) << i);
786 }
787 cpu_fprintf(f, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
788 env->fpuc,
789 (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11,
790 env->fpstt,
791 fptag,
792 env->mxcsr);
793 for(i=0;i<8;i++) {
794 #if defined(USE_X86LDOUBLE)
795 union {
796 long double d;
797 struct {
798 uint64_t lower;
799 uint16_t upper;
800 } l;
801 } tmp;
802 tmp.d = env->fpregs[i].d;
803 cpu_fprintf(f, "FPR%d=%016" PRIx64 " %04x",
804 i, tmp.l.lower, tmp.l.upper);
805 #else
806 cpu_fprintf(f, "FPR%d=%016" PRIx64,
807 i, env->fpregs[i].mmx.q);
808 #endif
809 if ((i & 1) == 1)
810 cpu_fprintf(f, "\n");
811 else
812 cpu_fprintf(f, " ");
813 }
814 if (env->hflags & HF_CS64_MASK)
815 nb = 16;
816 else
817 nb = 8;
818 for(i=0;i<nb;i++) {
819 cpu_fprintf(f, "XMM%02d=%08x%08x%08x%08x",
820 i,
821 env->xmm_regs[i].XMM_L(3),
822 env->xmm_regs[i].XMM_L(2),
823 env->xmm_regs[i].XMM_L(1),
824 env->xmm_regs[i].XMM_L(0));
825 if ((i & 1) == 1)
826 cpu_fprintf(f, "\n");
827 else
828 cpu_fprintf(f, " ");
829 }
830 }
831 }
832
833 /***********************************************************/
834 /* x86 mmu */
835 /* XXX: add PGE support */
836
837 void cpu_x86_set_a20(CPUX86State *env, int a20_state)
838 {
839 a20_state = (a20_state != 0);
840 if (a20_state != ((env->a20_mask >> 20) & 1)) {
841 #if defined(DEBUG_MMU)
842 printf("A20 update: a20=%d\n", a20_state);
843 #endif
844 /* if the cpu is currently executing code, we must unlink it and
845 all the potentially executing TB */
846 cpu_interrupt(env, CPU_INTERRUPT_EXITTB);
847
848 /* when a20 is changed, all the MMU mappings are invalid, so
849 we must flush everything */
850 tlb_flush(env, 1);
851 env->a20_mask = (~0x100000) | (a20_state << 20);
852 }
853 }
854
855 void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
856 {
857 int pe_state;
858
859 #if defined(DEBUG_MMU)
860 printf("CR0 update: CR0=0x%08x\n", new_cr0);
861 #endif
862 if ((new_cr0 & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK)) !=
863 (env->cr[0] & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK))) {
864 tlb_flush(env, 1);
865 }
866
867 #ifdef TARGET_X86_64
868 if (!(env->cr[0] & CR0_PG_MASK) && (new_cr0 & CR0_PG_MASK) &&
869 (env->efer & MSR_EFER_LME)) {
870 /* enter in long mode */
871 /* XXX: generate an exception */
872 if (!(env->cr[4] & CR4_PAE_MASK))
873 return;
874 env->efer |= MSR_EFER_LMA;
875 env->hflags |= HF_LMA_MASK;
876 } else if ((env->cr[0] & CR0_PG_MASK) && !(new_cr0 & CR0_PG_MASK) &&
877 (env->efer & MSR_EFER_LMA)) {
878 /* exit long mode */
879 env->efer &= ~MSR_EFER_LMA;
880 env->hflags &= ~(HF_LMA_MASK | HF_CS64_MASK);
881 env->eip &= 0xffffffff;
882 }
883 #endif
884 env->cr[0] = new_cr0 | CR0_ET_MASK;
885
886 /* update PE flag in hidden flags */
887 pe_state = (env->cr[0] & CR0_PE_MASK);
888 env->hflags = (env->hflags & ~HF_PE_MASK) | (pe_state << HF_PE_SHIFT);
889 /* ensure that ADDSEG is always set in real mode */
890 env->hflags |= ((pe_state ^ 1) << HF_ADDSEG_SHIFT);
891 /* update FPU flags */
892 env->hflags = (env->hflags & ~(HF_MP_MASK | HF_EM_MASK | HF_TS_MASK)) |
893 ((new_cr0 << (HF_MP_SHIFT - 1)) & (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK));
894 }
895
896 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
897 the PDPT */
898 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
899 {
900 env->cr[3] = new_cr3;
901 if (env->cr[0] & CR0_PG_MASK) {
902 #if defined(DEBUG_MMU)
903 printf("CR3 update: CR3=" TARGET_FMT_lx "\n", new_cr3);
904 #endif
905 tlb_flush(env, 0);
906 }
907 }
908
909 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
910 {
911 #if defined(DEBUG_MMU)
912 printf("CR4 update: CR4=%08x\n", (uint32_t)env->cr[4]);
913 #endif
914 if ((new_cr4 & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK)) !=
915 (env->cr[4] & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK))) {
916 tlb_flush(env, 1);
917 }
918 /* SSE handling */
919 if (!(env->cpuid_features & CPUID_SSE))
920 new_cr4 &= ~CR4_OSFXSR_MASK;
921 if (new_cr4 & CR4_OSFXSR_MASK)
922 env->hflags |= HF_OSFXSR_MASK;
923 else
924 env->hflags &= ~HF_OSFXSR_MASK;
925
926 env->cr[4] = new_cr4;
927 }
928
929 #if defined(CONFIG_USER_ONLY)
930
931 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
932 int is_write, int mmu_idx, int is_softmmu)
933 {
934 /* user mode only emulation */
935 is_write &= 1;
936 env->cr[2] = addr;
937 env->error_code = (is_write << PG_ERROR_W_BIT);
938 env->error_code |= PG_ERROR_U_MASK;
939 env->exception_index = EXCP0E_PAGE;
940 return 1;
941 }
942
943 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
944 {
945 return addr;
946 }
947
948 #else
949
950 /* XXX: This value should match the one returned by CPUID
951 * and in exec.c */
952 #if defined(CONFIG_KQEMU)
953 #define PHYS_ADDR_MASK 0xfffff000LL
954 #else
955 # if defined(TARGET_X86_64)
956 # define PHYS_ADDR_MASK 0xfffffff000LL
957 # else
958 # define PHYS_ADDR_MASK 0xffffff000LL
959 # endif
960 #endif
961
962 /* return value:
963 -1 = cannot handle fault
964 0 = nothing more to do
965 1 = generate PF fault
966 2 = soft MMU activation required for this block
967 */
968 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
969 int is_write1, int mmu_idx, int is_softmmu)
970 {
971 uint64_t ptep, pte;
972 target_ulong pde_addr, pte_addr;
973 int error_code, is_dirty, prot, page_size, ret, is_write, is_user;
974 target_phys_addr_t paddr;
975 uint32_t page_offset;
976 target_ulong vaddr, virt_addr;
977
978 is_user = mmu_idx == MMU_USER_IDX;
979 #if defined(DEBUG_MMU)
980 printf("MMU fault: addr=" TARGET_FMT_lx " w=%d u=%d eip=" TARGET_FMT_lx "\n",
981 addr, is_write1, is_user, env->eip);
982 #endif
983 is_write = is_write1 & 1;
984
985 if (!(env->cr[0] & CR0_PG_MASK)) {
986 pte = addr;
987 virt_addr = addr & TARGET_PAGE_MASK;
988 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
989 page_size = 4096;
990 goto do_mapping;
991 }
992
993 if (env->cr[4] & CR4_PAE_MASK) {
994 uint64_t pde, pdpe;
995 target_ulong pdpe_addr;
996
997 #ifdef TARGET_X86_64
998 if (env->hflags & HF_LMA_MASK) {
999 uint64_t pml4e_addr, pml4e;
1000 int32_t sext;
1001
1002 /* test virtual address sign extension */
1003 sext = (int64_t)addr >> 47;
1004 if (sext != 0 && sext != -1) {
1005 env->error_code = 0;
1006 env->exception_index = EXCP0D_GPF;
1007 return 1;
1008 }
1009
1010 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
1011 env->a20_mask;
1012 pml4e = ldq_phys(pml4e_addr);
1013 if (!(pml4e & PG_PRESENT_MASK)) {
1014 error_code = 0;
1015 goto do_fault;
1016 }
1017 if (!(env->efer & MSR_EFER_NXE) && (pml4e & PG_NX_MASK)) {
1018 error_code = PG_ERROR_RSVD_MASK;
1019 goto do_fault;
1020 }
1021 if (!(pml4e & PG_ACCESSED_MASK)) {
1022 pml4e |= PG_ACCESSED_MASK;
1023 stl_phys_notdirty(pml4e_addr, pml4e);
1024 }
1025 ptep = pml4e ^ PG_NX_MASK;
1026 pdpe_addr = ((pml4e & PHYS_ADDR_MASK) + (((addr >> 30) & 0x1ff) << 3)) &
1027 env->a20_mask;
1028 pdpe = ldq_phys(pdpe_addr);
1029 if (!(pdpe & PG_PRESENT_MASK)) {
1030 error_code = 0;
1031 goto do_fault;
1032 }
1033 if (!(env->efer & MSR_EFER_NXE) && (pdpe & PG_NX_MASK)) {
1034 error_code = PG_ERROR_RSVD_MASK;
1035 goto do_fault;
1036 }
1037 ptep &= pdpe ^ PG_NX_MASK;
1038 if (!(pdpe & PG_ACCESSED_MASK)) {
1039 pdpe |= PG_ACCESSED_MASK;
1040 stl_phys_notdirty(pdpe_addr, pdpe);
1041 }
1042 } else
1043 #endif
1044 {
1045 /* XXX: load them when cr3 is loaded ? */
1046 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1047 env->a20_mask;
1048 pdpe = ldq_phys(pdpe_addr);
1049 if (!(pdpe & PG_PRESENT_MASK)) {
1050 error_code = 0;
1051 goto do_fault;
1052 }
1053 ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK;
1054 }
1055
1056 pde_addr = ((pdpe & PHYS_ADDR_MASK) + (((addr >> 21) & 0x1ff) << 3)) &
1057 env->a20_mask;
1058 pde = ldq_phys(pde_addr);
1059 if (!(pde & PG_PRESENT_MASK)) {
1060 error_code = 0;
1061 goto do_fault;
1062 }
1063 if (!(env->efer & MSR_EFER_NXE) && (pde & PG_NX_MASK)) {
1064 error_code = PG_ERROR_RSVD_MASK;
1065 goto do_fault;
1066 }
1067 ptep &= pde ^ PG_NX_MASK;
1068 if (pde & PG_PSE_MASK) {
1069 /* 2 MB page */
1070 page_size = 2048 * 1024;
1071 ptep ^= PG_NX_MASK;
1072 if ((ptep & PG_NX_MASK) && is_write1 == 2)
1073 goto do_fault_protect;
1074 if (is_user) {
1075 if (!(ptep & PG_USER_MASK))
1076 goto do_fault_protect;
1077 if (is_write && !(ptep & PG_RW_MASK))
1078 goto do_fault_protect;
1079 } else {
1080 if ((env->cr[0] & CR0_WP_MASK) &&
1081 is_write && !(ptep & PG_RW_MASK))
1082 goto do_fault_protect;
1083 }
1084 is_dirty = is_write && !(pde & PG_DIRTY_MASK);
1085 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1086 pde |= PG_ACCESSED_MASK;
1087 if (is_dirty)
1088 pde |= PG_DIRTY_MASK;
1089 stl_phys_notdirty(pde_addr, pde);
1090 }
1091 /* align to page_size */
1092 pte = pde & ((PHYS_ADDR_MASK & ~(page_size - 1)) | 0xfff);
1093 virt_addr = addr & ~(page_size - 1);
1094 } else {
1095 /* 4 KB page */
1096 if (!(pde & PG_ACCESSED_MASK)) {
1097 pde |= PG_ACCESSED_MASK;
1098 stl_phys_notdirty(pde_addr, pde);
1099 }
1100 pte_addr = ((pde & PHYS_ADDR_MASK) + (((addr >> 12) & 0x1ff) << 3)) &
1101 env->a20_mask;
1102 pte = ldq_phys(pte_addr);
1103 if (!(pte & PG_PRESENT_MASK)) {
1104 error_code = 0;
1105 goto do_fault;
1106 }
1107 if (!(env->efer & MSR_EFER_NXE) && (pte & PG_NX_MASK)) {
1108 error_code = PG_ERROR_RSVD_MASK;
1109 goto do_fault;
1110 }
1111 /* combine pde and pte nx, user and rw protections */
1112 ptep &= pte ^ PG_NX_MASK;
1113 ptep ^= PG_NX_MASK;
1114 if ((ptep & PG_NX_MASK) && is_write1 == 2)
1115 goto do_fault_protect;
1116 if (is_user) {
1117 if (!(ptep & PG_USER_MASK))
1118 goto do_fault_protect;
1119 if (is_write && !(ptep & PG_RW_MASK))
1120 goto do_fault_protect;
1121 } else {
1122 if ((env->cr[0] & CR0_WP_MASK) &&
1123 is_write && !(ptep & PG_RW_MASK))
1124 goto do_fault_protect;
1125 }
1126 is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1127 if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1128 pte |= PG_ACCESSED_MASK;
1129 if (is_dirty)
1130 pte |= PG_DIRTY_MASK;
1131 stl_phys_notdirty(pte_addr, pte);
1132 }
1133 page_size = 4096;
1134 virt_addr = addr & ~0xfff;
1135 pte = pte & (PHYS_ADDR_MASK | 0xfff);
1136 }
1137 } else {
1138 uint32_t pde;
1139
1140 /* page directory entry */
1141 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) &
1142 env->a20_mask;
1143 pde = ldl_phys(pde_addr);
1144 if (!(pde & PG_PRESENT_MASK)) {
1145 error_code = 0;
1146 goto do_fault;
1147 }
1148 /* if PSE bit is set, then we use a 4MB page */
1149 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1150 page_size = 4096 * 1024;
1151 if (is_user) {
1152 if (!(pde & PG_USER_MASK))
1153 goto do_fault_protect;
1154 if (is_write && !(pde & PG_RW_MASK))
1155 goto do_fault_protect;
1156 } else {
1157 if ((env->cr[0] & CR0_WP_MASK) &&
1158 is_write && !(pde & PG_RW_MASK))
1159 goto do_fault_protect;
1160 }
1161 is_dirty = is_write && !(pde & PG_DIRTY_MASK);
1162 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1163 pde |= PG_ACCESSED_MASK;
1164 if (is_dirty)
1165 pde |= PG_DIRTY_MASK;
1166 stl_phys_notdirty(pde_addr, pde);
1167 }
1168
1169 pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1170 ptep = pte;
1171 virt_addr = addr & ~(page_size - 1);
1172 } else {
1173 if (!(pde & PG_ACCESSED_MASK)) {
1174 pde |= PG_ACCESSED_MASK;
1175 stl_phys_notdirty(pde_addr, pde);
1176 }
1177
1178 /* page directory entry */
1179 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) &
1180 env->a20_mask;
1181 pte = ldl_phys(pte_addr);
1182 if (!(pte & PG_PRESENT_MASK)) {
1183 error_code = 0;
1184 goto do_fault;
1185 }
1186 /* combine pde and pte user and rw protections */
1187 ptep = pte & pde;
1188 if (is_user) {
1189 if (!(ptep & PG_USER_MASK))
1190 goto do_fault_protect;
1191 if (is_write && !(ptep & PG_RW_MASK))
1192 goto do_fault_protect;
1193 } else {
1194 if ((env->cr[0] & CR0_WP_MASK) &&
1195 is_write && !(ptep & PG_RW_MASK))
1196 goto do_fault_protect;
1197 }
1198 is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1199 if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1200 pte |= PG_ACCESSED_MASK;
1201 if (is_dirty)
1202 pte |= PG_DIRTY_MASK;
1203 stl_phys_notdirty(pte_addr, pte);
1204 }
1205 page_size = 4096;
1206 virt_addr = addr & ~0xfff;
1207 }
1208 }
1209 /* the page can be put in the TLB */
1210 prot = PAGE_READ;
1211 if (!(ptep & PG_NX_MASK))
1212 prot |= PAGE_EXEC;
1213 if (pte & PG_DIRTY_MASK) {
1214 /* only set write access if already dirty... otherwise wait
1215 for dirty access */
1216 if (is_user) {
1217 if (ptep & PG_RW_MASK)
1218 prot |= PAGE_WRITE;
1219 } else {
1220 if (!(env->cr[0] & CR0_WP_MASK) ||
1221 (ptep & PG_RW_MASK))
1222 prot |= PAGE_WRITE;
1223 }
1224 }
1225 do_mapping:
1226 pte = pte & env->a20_mask;
1227
1228 /* Even if 4MB pages, we map only one 4KB page in the cache to
1229 avoid filling it too fast */
1230 page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1231 paddr = (pte & TARGET_PAGE_MASK) + page_offset;
1232 vaddr = virt_addr + page_offset;
1233
1234 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
1235 return ret;
1236 do_fault_protect:
1237 error_code = PG_ERROR_P_MASK;
1238 do_fault:
1239 error_code |= (is_write << PG_ERROR_W_BIT);
1240 if (is_user)
1241 error_code |= PG_ERROR_U_MASK;
1242 if (is_write1 == 2 &&
1243 (env->efer & MSR_EFER_NXE) &&
1244 (env->cr[4] & CR4_PAE_MASK))
1245 error_code |= PG_ERROR_I_D_MASK;
1246 if (env->intercept_exceptions & (1 << EXCP0E_PAGE)) {
1247 /* cr2 is not modified in case of exceptions */
1248 stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2),
1249 addr);
1250 } else {
1251 env->cr[2] = addr;
1252 }
1253 env->error_code = error_code;
1254 env->exception_index = EXCP0E_PAGE;
1255 return 1;
1256 }
1257
1258 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
1259 {
1260 target_ulong pde_addr, pte_addr;
1261 uint64_t pte;
1262 target_phys_addr_t paddr;
1263 uint32_t page_offset;
1264 int page_size;
1265
1266 if (env->cr[4] & CR4_PAE_MASK) {
1267 target_ulong pdpe_addr;
1268 uint64_t pde, pdpe;
1269
1270 #ifdef TARGET_X86_64
1271 if (env->hflags & HF_LMA_MASK) {
1272 uint64_t pml4e_addr, pml4e;
1273 int32_t sext;
1274
1275 /* test virtual address sign extension */
1276 sext = (int64_t)addr >> 47;
1277 if (sext != 0 && sext != -1)
1278 return -1;
1279
1280 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
1281 env->a20_mask;
1282 pml4e = ldq_phys(pml4e_addr);
1283 if (!(pml4e & PG_PRESENT_MASK))
1284 return -1;
1285
1286 pdpe_addr = ((pml4e & ~0xfff) + (((addr >> 30) & 0x1ff) << 3)) &
1287 env->a20_mask;
1288 pdpe = ldq_phys(pdpe_addr);
1289 if (!(pdpe & PG_PRESENT_MASK))
1290 return -1;
1291 } else
1292 #endif
1293 {
1294 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1295 env->a20_mask;
1296 pdpe = ldq_phys(pdpe_addr);
1297 if (!(pdpe & PG_PRESENT_MASK))
1298 return -1;
1299 }
1300
1301 pde_addr = ((pdpe & ~0xfff) + (((addr >> 21) & 0x1ff) << 3)) &
1302 env->a20_mask;
1303 pde = ldq_phys(pde_addr);
1304 if (!(pde & PG_PRESENT_MASK)) {
1305 return -1;
1306 }
1307 if (pde & PG_PSE_MASK) {
1308 /* 2 MB page */
1309 page_size = 2048 * 1024;
1310 pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1311 } else {
1312 /* 4 KB page */
1313 pte_addr = ((pde & ~0xfff) + (((addr >> 12) & 0x1ff) << 3)) &
1314 env->a20_mask;
1315 page_size = 4096;
1316 pte = ldq_phys(pte_addr);
1317 }
1318 if (!(pte & PG_PRESENT_MASK))
1319 return -1;
1320 } else {
1321 uint32_t pde;
1322
1323 if (!(env->cr[0] & CR0_PG_MASK)) {
1324 pte = addr;
1325 page_size = 4096;
1326 } else {
1327 /* page directory entry */
1328 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & env->a20_mask;
1329 pde = ldl_phys(pde_addr);
1330 if (!(pde & PG_PRESENT_MASK))
1331 return -1;
1332 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1333 pte = pde & ~0x003ff000; /* align to 4MB */
1334 page_size = 4096 * 1024;
1335 } else {
1336 /* page directory entry */
1337 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & env->a20_mask;
1338 pte = ldl_phys(pte_addr);
1339 if (!(pte & PG_PRESENT_MASK))
1340 return -1;
1341 page_size = 4096;
1342 }
1343 }
1344 pte = pte & env->a20_mask;
1345 }
1346
1347 page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1348 paddr = (pte & TARGET_PAGE_MASK) + page_offset;
1349 return paddr;
1350 }
1351
1352 void hw_breakpoint_insert(CPUState *env, int index)
1353 {
1354 int type, err = 0;
1355
1356 switch (hw_breakpoint_type(env->dr[7], index)) {
1357 case 0:
1358 if (hw_breakpoint_enabled(env->dr[7], index))
1359 err = cpu_breakpoint_insert(env, env->dr[index], BP_CPU,
1360 &env->cpu_breakpoint[index]);
1361 break;
1362 case 1:
1363 type = BP_CPU | BP_MEM_WRITE;
1364 goto insert_wp;
1365 case 2:
1366 /* No support for I/O watchpoints yet */
1367 break;
1368 case 3:
1369 type = BP_CPU | BP_MEM_ACCESS;
1370 insert_wp:
1371 err = cpu_watchpoint_insert(env, env->dr[index],
1372 hw_breakpoint_len(env->dr[7], index),
1373 type, &env->cpu_watchpoint[index]);
1374 break;
1375 }
1376 if (err)
1377 env->cpu_breakpoint[index] = NULL;
1378 }
1379
1380 void hw_breakpoint_remove(CPUState *env, int index)
1381 {
1382 if (!env->cpu_breakpoint[index])
1383 return;
1384 switch (hw_breakpoint_type(env->dr[7], index)) {
1385 case 0:
1386 if (hw_breakpoint_enabled(env->dr[7], index))
1387 cpu_breakpoint_remove_by_ref(env, env->cpu_breakpoint[index]);
1388 break;
1389 case 1:
1390 case 3:
1391 cpu_watchpoint_remove_by_ref(env, env->cpu_watchpoint[index]);
1392 break;
1393 case 2:
1394 /* No support for I/O watchpoints yet */
1395 break;
1396 }
1397 }
1398
1399 int check_hw_breakpoints(CPUState *env, int force_dr6_update)
1400 {
1401 target_ulong dr6;
1402 int reg, type;
1403 int hit_enabled = 0;
1404
1405 dr6 = env->dr[6] & ~0xf;
1406 for (reg = 0; reg < 4; reg++) {
1407 type = hw_breakpoint_type(env->dr[7], reg);
1408 if ((type == 0 && env->dr[reg] == env->eip) ||
1409 ((type & 1) && env->cpu_watchpoint[reg] &&
1410 (env->cpu_watchpoint[reg]->flags & BP_WATCHPOINT_HIT))) {
1411 dr6 |= 1 << reg;
1412 if (hw_breakpoint_enabled(env->dr[7], reg))
1413 hit_enabled = 1;
1414 }
1415 }
1416 if (hit_enabled || force_dr6_update)
1417 env->dr[6] = dr6;
1418 return hit_enabled;
1419 }
1420
1421 static CPUDebugExcpHandler *prev_debug_excp_handler;
1422
1423 void raise_exception(int exception_index);
1424
1425 static void breakpoint_handler(CPUState *env)
1426 {
1427 CPUBreakpoint *bp;
1428
1429 if (env->watchpoint_hit) {
1430 if (env->watchpoint_hit->flags & BP_CPU) {
1431 env->watchpoint_hit = NULL;
1432 if (check_hw_breakpoints(env, 0))
1433 raise_exception(EXCP01_DB);
1434 else
1435 cpu_resume_from_signal(env, NULL);
1436 }
1437 } else {
1438 TAILQ_FOREACH(bp, &env->breakpoints, entry)
1439 if (bp->pc == env->eip) {
1440 if (bp->flags & BP_CPU) {
1441 check_hw_breakpoints(env, 1);
1442 raise_exception(EXCP01_DB);
1443 }
1444 break;
1445 }
1446 }
1447 if (prev_debug_excp_handler)
1448 prev_debug_excp_handler(env);
1449 }
1450 #endif /* !CONFIG_USER_ONLY */
1451
1452 static void host_cpuid(uint32_t function, uint32_t count,
1453 uint32_t *eax, uint32_t *ebx,
1454 uint32_t *ecx, uint32_t *edx)
1455 {
1456 #if defined(CONFIG_KVM)
1457 uint32_t vec[4];
1458
1459 #ifdef __x86_64__
1460 asm volatile("cpuid"
1461 : "=a"(vec[0]), "=b"(vec[1]),
1462 "=c"(vec[2]), "=d"(vec[3])
1463 : "0"(function), "c"(count) : "cc");
1464 #else
1465 asm volatile("pusha \n\t"
1466 "cpuid \n\t"
1467 "mov %%eax, 0(%2) \n\t"
1468 "mov %%ebx, 4(%2) \n\t"
1469 "mov %%ecx, 8(%2) \n\t"
1470 "mov %%edx, 12(%2) \n\t"
1471 "popa"
1472 : : "a"(function), "c"(count), "S"(vec)
1473 : "memory", "cc");
1474 #endif
1475
1476 if (eax)
1477 *eax = vec[0];
1478 if (ebx)
1479 *ebx = vec[1];
1480 if (ecx)
1481 *ecx = vec[2];
1482 if (edx)
1483 *edx = vec[3];
1484 #endif
1485 }
1486
1487 void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
1488 uint32_t *eax, uint32_t *ebx,
1489 uint32_t *ecx, uint32_t *edx)
1490 {
1491 /* test if maximum index reached */
1492 if (index & 0x80000000) {
1493 if (index > env->cpuid_xlevel)
1494 index = env->cpuid_level;
1495 } else {
1496 if (index > env->cpuid_level)
1497 index = env->cpuid_level;
1498 }
1499
1500 switch(index) {
1501 case 0:
1502 *eax = env->cpuid_level;
1503 *ebx = env->cpuid_vendor1;
1504 *edx = env->cpuid_vendor2;
1505 *ecx = env->cpuid_vendor3;
1506
1507 /* sysenter isn't supported on compatibility mode on AMD. and syscall
1508 * isn't supported in compatibility mode on Intel. so advertise the
1509 * actuall cpu, and say goodbye to migration between different vendors
1510 * is you use compatibility mode. */
1511 if (kvm_enabled())
1512 host_cpuid(0, 0, NULL, ebx, ecx, edx);
1513 break;
1514 case 1:
1515 *eax = env->cpuid_version;
1516 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1517 *ecx = env->cpuid_ext_features;
1518 *edx = env->cpuid_features;
1519
1520 /* "Hypervisor present" bit required for Microsoft SVVP */
1521 if (kvm_enabled())
1522 *ecx |= (1 << 31);
1523 break;
1524 case 2:
1525 /* cache info: needed for Pentium Pro compatibility */
1526 *eax = 1;
1527 *ebx = 0;
1528 *ecx = 0;
1529 *edx = 0x2c307d;
1530 break;
1531 case 4:
1532 /* cache info: needed for Core compatibility */
1533 switch (count) {
1534 case 0: /* L1 dcache info */
1535 *eax = 0x0000121;
1536 *ebx = 0x1c0003f;
1537 *ecx = 0x000003f;
1538 *edx = 0x0000001;
1539 break;
1540 case 1: /* L1 icache info */
1541 *eax = 0x0000122;
1542 *ebx = 0x1c0003f;
1543 *ecx = 0x000003f;
1544 *edx = 0x0000001;
1545 break;
1546 case 2: /* L2 cache info */
1547 *eax = 0x0000143;
1548 *ebx = 0x3c0003f;
1549 *ecx = 0x0000fff;
1550 *edx = 0x0000001;
1551 break;
1552 default: /* end of info */
1553 *eax = 0;
1554 *ebx = 0;
1555 *ecx = 0;
1556 *edx = 0;
1557 break;
1558 }
1559 break;
1560 case 5:
1561 /* mwait info: needed for Core compatibility */
1562 *eax = 0; /* Smallest monitor-line size in bytes */
1563 *ebx = 0; /* Largest monitor-line size in bytes */
1564 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
1565 *edx = 0;
1566 break;
1567 case 6:
1568 /* Thermal and Power Leaf */
1569 *eax = 0;
1570 *ebx = 0;
1571 *ecx = 0;
1572 *edx = 0;
1573 break;
1574 case 9:
1575 /* Direct Cache Access Information Leaf */
1576 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1577 *ebx = 0;
1578 *ecx = 0;
1579 *edx = 0;
1580 break;
1581 case 0xA:
1582 /* Architectural Performance Monitoring Leaf */
1583 *eax = 0;
1584 *ebx = 0;
1585 *ecx = 0;
1586 *edx = 0;
1587 break;
1588 case 0x80000000:
1589 *eax = env->cpuid_xlevel;
1590 *ebx = env->cpuid_vendor1;
1591 *edx = env->cpuid_vendor2;
1592 *ecx = env->cpuid_vendor3;
1593 break;
1594 case 0x80000001:
1595 *eax = env->cpuid_features;
1596 *ebx = 0;
1597 *ecx = env->cpuid_ext3_features;
1598 *edx = env->cpuid_ext2_features;
1599
1600 if (kvm_enabled()) {
1601 uint32_t h_eax, h_edx;
1602
1603 host_cpuid(index, 0, &h_eax, NULL, NULL, &h_edx);
1604
1605 /* disable CPU features that the host does not support */
1606
1607 /* long mode */
1608 if ((h_edx & 0x20000000) == 0 /* || !lm_capable_kernel */)
1609 *edx &= ~0x20000000;
1610 /* syscall */
1611 if ((h_edx & 0x00000800) == 0)
1612 *edx &= ~0x00000800;
1613 /* nx */
1614 if ((h_edx & 0x00100000) == 0)
1615 *edx &= ~0x00100000;
1616
1617 /* disable CPU features that KVM cannot support */
1618
1619 /* svm */
1620 *ecx &= ~4UL;
1621 /* 3dnow */
1622 *edx &= ~0xc0000000;
1623 }
1624 break;
1625 case 0x80000002:
1626 case 0x80000003:
1627 case 0x80000004:
1628 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
1629 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
1630 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
1631 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
1632 break;
1633 case 0x80000005:
1634 /* cache info (L1 cache) */
1635 *eax = 0x01ff01ff;
1636 *ebx = 0x01ff01ff;
1637 *ecx = 0x40020140;
1638 *edx = 0x40020140;
1639 break;
1640 case 0x80000006:
1641 /* cache info (L2 cache) */
1642 *eax = 0;
1643 *ebx = 0x42004200;
1644 *ecx = 0x02008140;
1645 *edx = 0;
1646 break;
1647 case 0x80000008:
1648 /* virtual & phys address size in low 2 bytes. */
1649 /* XXX: This value must match the one used in the MMU code. */
1650 if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
1651 /* 64 bit processor */
1652 #if defined(CONFIG_KQEMU)
1653 *eax = 0x00003020; /* 48 bits virtual, 32 bits physical */
1654 #else
1655 /* XXX: The physical address space is limited to 42 bits in exec.c. */
1656 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
1657 #endif
1658 } else {
1659 #if defined(CONFIG_KQEMU)
1660 *eax = 0x00000020; /* 32 bits physical */
1661 #else
1662 if (env->cpuid_features & CPUID_PSE36)
1663 *eax = 0x00000024; /* 36 bits physical */
1664 else
1665 *eax = 0x00000020; /* 32 bits physical */
1666 #endif
1667 }
1668 *ebx = 0;
1669 *ecx = 0;
1670 *edx = 0;
1671 break;
1672 case 0x8000000A:
1673 *eax = 0x00000001; /* SVM Revision */
1674 *ebx = 0x00000010; /* nr of ASIDs */
1675 *ecx = 0;
1676 *edx = 0; /* optional features */
1677 break;
1678 default:
1679 /* reserved values: zero */
1680 *eax = 0;
1681 *ebx = 0;
1682 *ecx = 0;
1683 *edx = 0;
1684 break;
1685 }
1686 }
1687
1688 CPUX86State *cpu_x86_init(const char *cpu_model)
1689 {
1690 CPUX86State *env;
1691 static int inited;
1692
1693 env = qemu_mallocz(sizeof(CPUX86State));
1694 cpu_exec_init(env);
1695 env->cpu_model_str = cpu_model;
1696
1697 /* init various static tables */
1698 if (!inited) {
1699 inited = 1;
1700 optimize_flags_init();
1701 #ifndef CONFIG_USER_ONLY
1702 prev_debug_excp_handler =
1703 cpu_set_debug_excp_handler(breakpoint_handler);
1704 #endif
1705 }
1706 if (cpu_x86_register(env, cpu_model) < 0) {
1707 cpu_x86_close(env);
1708 return NULL;
1709 }
1710 cpu_reset(env);
1711 #ifdef CONFIG_KQEMU
1712 kqemu_init(env);
1713 #endif
1714
1715 qemu_init_vcpu(env);
1716
1717 if (kvm_enabled()) {
1718 kvm_trim_features(&env->cpuid_features,
1719 kvm_arch_get_supported_cpuid(env, 1, R_EDX),
1720 feature_name);
1721 kvm_trim_features(&env->cpuid_ext_features,
1722 kvm_arch_get_supported_cpuid(env, 1, R_ECX),
1723 ext_feature_name);
1724 kvm_trim_features(&env->cpuid_ext2_features,
1725 kvm_arch_get_supported_cpuid(env, 0x80000001, R_EDX),
1726 ext2_feature_name);
1727 kvm_trim_features(&env->cpuid_ext3_features,
1728 kvm_arch_get_supported_cpuid(env, 0x80000001, R_ECX),
1729 ext3_feature_name);
1730 }
1731
1732 return env;
1733 }