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