root / target-i386 / helper2.c @ 3e98dc8e
History | View | Annotate | Download (38 kB)
1 | 2c0262af | bellard | /*
|
---|---|---|---|
2 | 2c0262af | bellard | * i386 helpers (without register variable usage)
|
3 | 5fafdf24 | ths | *
|
4 | 2c0262af | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 2c0262af | bellard | *
|
6 | 2c0262af | bellard | * This library is free software; you can redistribute it and/or
|
7 | 2c0262af | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | 2c0262af | bellard | * License as published by the Free Software Foundation; either
|
9 | 2c0262af | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | 2c0262af | bellard | *
|
11 | 2c0262af | bellard | * This library is distributed in the hope that it will be useful,
|
12 | 2c0262af | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 2c0262af | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 2c0262af | bellard | * Lesser General Public License for more details.
|
15 | 2c0262af | bellard | *
|
16 | 2c0262af | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | 2c0262af | bellard | * License along with this library; if not, write to the Free Software
|
18 | 2c0262af | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | 2c0262af | bellard | */
|
20 | 2c0262af | bellard | #include <stdarg.h> |
21 | 2c0262af | bellard | #include <stdlib.h> |
22 | 2c0262af | bellard | #include <stdio.h> |
23 | 2c0262af | bellard | #include <string.h> |
24 | 2c0262af | bellard | #include <inttypes.h> |
25 | 2c0262af | bellard | #include <signal.h> |
26 | 2c0262af | bellard | #include <assert.h> |
27 | 2c0262af | bellard | |
28 | 2c0262af | bellard | #include "cpu.h" |
29 | 2c0262af | bellard | #include "exec-all.h" |
30 | 0573fbfc | ths | #include "svm.h" |
31 | 2c0262af | bellard | |
32 | 2c0262af | bellard | //#define DEBUG_MMU
|
33 | 2c0262af | bellard | |
34 | aaed909a | bellard | static int cpu_x86_register (CPUX86State *env, const char *cpu_model); |
35 | a049de61 | bellard | |
36 | a049de61 | bellard | static void add_flagname_to_bitmaps(char *flagname, uint32_t *features, |
37 | a049de61 | bellard | uint32_t *ext_features, |
38 | a049de61 | bellard | uint32_t *ext2_features, |
39 | a049de61 | bellard | uint32_t *ext3_features) |
40 | a049de61 | bellard | { |
41 | a049de61 | bellard | int i;
|
42 | a049de61 | bellard | /* feature flags taken from "Intel Processor Identification and the CPUID
|
43 | a049de61 | bellard | * Instruction" and AMD's "CPUID Specification". In cases of disagreement
|
44 | a049de61 | bellard | * about feature names, the Linux name is used. */
|
45 | a049de61 | bellard | const char *feature_name[] = { |
46 | a049de61 | bellard | "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce", |
47 | a049de61 | bellard | "cx8", "apic", NULL, "sep", "mtrr", "pge", "mca", "cmov", |
48 | a049de61 | bellard | "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */, NULL, "ds" /* Intel dts */, "acpi", "mmx", |
49 | a049de61 | bellard | "fxsr", "sse", "sse2", "ss", "ht" /* Intel htt */, "tm", "ia64", "pbe", |
50 | a049de61 | bellard | }; |
51 | a049de61 | bellard | const char *ext_feature_name[] = { |
52 | a049de61 | bellard | "pni" /* Intel,AMD sse3 */, NULL, NULL, "monitor", "ds_cpl", "vmx", NULL /* Linux smx */, "est", |
53 | a049de61 | bellard | "tm2", "ssse3", "cid", NULL, NULL, "cx16", "xtpr", NULL, |
54 | a049de61 | bellard | NULL, NULL, "dca", NULL, NULL, NULL, NULL, "popcnt", |
55 | a049de61 | bellard | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
56 | a049de61 | bellard | }; |
57 | a049de61 | bellard | const char *ext2_feature_name[] = { |
58 | a049de61 | bellard | "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce", |
59 | a049de61 | bellard | "cx8" /* AMD CMPXCHG8B */, "apic", NULL, "syscall", "mttr", "pge", "mca", "cmov", |
60 | a049de61 | bellard | "pat", "pse36", NULL, NULL /* Linux mp */, "nx" /* Intel xd */, NULL, "mmxext", "mmx", |
61 | a049de61 | bellard | "fxsr", "fxsr_opt" /* AMD ffxsr */, "pdpe1gb" /* AMD Page1GB */, "rdtscp", NULL, "lm" /* Intel 64 */, "3dnowext", "3dnow", |
62 | a049de61 | bellard | }; |
63 | a049de61 | bellard | const char *ext3_feature_name[] = { |
64 | a049de61 | bellard | "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */, "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse", |
65 | a049de61 | bellard | "3dnowprefetch", "osvw", NULL /* Linux ibs */, NULL, "skinit", "wdt", NULL, NULL, |
66 | a049de61 | bellard | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
67 | a049de61 | bellard | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
68 | a049de61 | bellard | }; |
69 | a049de61 | bellard | |
70 | a049de61 | bellard | for ( i = 0 ; i < 32 ; i++ ) |
71 | a049de61 | bellard | if (feature_name[i] && !strcmp (flagname, feature_name[i])) {
|
72 | a049de61 | bellard | *features |= 1 << i;
|
73 | a049de61 | bellard | return;
|
74 | a049de61 | bellard | } |
75 | a049de61 | bellard | for ( i = 0 ; i < 32 ; i++ ) |
76 | a049de61 | bellard | if (ext_feature_name[i] && !strcmp (flagname, ext_feature_name[i])) {
|
77 | a049de61 | bellard | *ext_features |= 1 << i;
|
78 | a049de61 | bellard | return;
|
79 | a049de61 | bellard | } |
80 | a049de61 | bellard | for ( i = 0 ; i < 32 ; i++ ) |
81 | a049de61 | bellard | if (ext2_feature_name[i] && !strcmp (flagname, ext2_feature_name[i])) {
|
82 | a049de61 | bellard | *ext2_features |= 1 << i;
|
83 | a049de61 | bellard | return;
|
84 | a049de61 | bellard | } |
85 | a049de61 | bellard | for ( i = 0 ; i < 32 ; i++ ) |
86 | 0b1b91c7 | bellard | if (ext3_feature_name[i] && !strcmp (flagname, ext3_feature_name[i])) {
|
87 | a049de61 | bellard | *ext3_features |= 1 << i;
|
88 | a049de61 | bellard | return;
|
89 | a049de61 | bellard | } |
90 | a049de61 | bellard | fprintf(stderr, "CPU feature %s not found\n", flagname);
|
91 | a049de61 | bellard | } |
92 | a049de61 | bellard | |
93 | aaed909a | bellard | CPUX86State *cpu_x86_init(const char *cpu_model) |
94 | 2c0262af | bellard | { |
95 | 2c0262af | bellard | CPUX86State *env; |
96 | 2c0262af | bellard | static int inited; |
97 | 2c0262af | bellard | |
98 | 173d6cfe | bellard | env = qemu_mallocz(sizeof(CPUX86State));
|
99 | 2c0262af | bellard | if (!env)
|
100 | 2c0262af | bellard | return NULL; |
101 | 173d6cfe | bellard | cpu_exec_init(env); |
102 | 01ba9816 | ths | env->cpu_model_str = cpu_model; |
103 | 173d6cfe | bellard | |
104 | ffddfee3 | bellard | /* init various static tables */
|
105 | ffddfee3 | bellard | if (!inited) {
|
106 | ffddfee3 | bellard | inited = 1;
|
107 | ffddfee3 | bellard | optimize_flags_init(); |
108 | ffddfee3 | bellard | } |
109 | aaed909a | bellard | if (cpu_x86_register(env, cpu_model) < 0) { |
110 | aaed909a | bellard | cpu_x86_close(env); |
111 | aaed909a | bellard | return NULL; |
112 | aaed909a | bellard | } |
113 | a049de61 | bellard | cpu_reset(env); |
114 | a049de61 | bellard | #ifdef USE_KQEMU
|
115 | a049de61 | bellard | kqemu_init(env); |
116 | 14ce26e7 | bellard | #endif
|
117 | a049de61 | bellard | return env;
|
118 | a049de61 | bellard | } |
119 | 8f091a59 | bellard | |
120 | aaed909a | bellard | typedef struct x86_def_t { |
121 | a049de61 | bellard | const char *name; |
122 | 3e98dc8e | balrog | uint32_t level; |
123 | a049de61 | bellard | uint32_t vendor1, vendor2, vendor3; |
124 | a049de61 | bellard | int family;
|
125 | a049de61 | bellard | int model;
|
126 | a049de61 | bellard | int stepping;
|
127 | a049de61 | bellard | uint32_t features, ext_features, ext2_features, ext3_features; |
128 | a049de61 | bellard | uint32_t xlevel; |
129 | aaed909a | bellard | } x86_def_t; |
130 | a049de61 | bellard | |
131 | a049de61 | bellard | #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
|
132 | a049de61 | bellard | CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \ |
133 | a049de61 | bellard | CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \ |
134 | a049de61 | bellard | CPUID_PAE | CPUID_SEP | CPUID_APIC) |
135 | a049de61 | bellard | static x86_def_t x86_defs[] = {
|
136 | a049de61 | bellard | #ifdef TARGET_X86_64
|
137 | a049de61 | bellard | { |
138 | a049de61 | bellard | .name = "qemu64",
|
139 | 3e98dc8e | balrog | .level = 2,
|
140 | a049de61 | bellard | .vendor1 = 0x68747541, /* "Auth" */ |
141 | a049de61 | bellard | .vendor2 = 0x69746e65, /* "enti" */ |
142 | a049de61 | bellard | .vendor3 = 0x444d4163, /* "cAMD" */ |
143 | a049de61 | bellard | .family = 6,
|
144 | a049de61 | bellard | .model = 2,
|
145 | a049de61 | bellard | .stepping = 3,
|
146 | a049de61 | bellard | .features = PPRO_FEATURES | |
147 | 8f091a59 | bellard | /* these features are needed for Win64 and aren't fully implemented */
|
148 | a049de61 | bellard | CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | |
149 | 69c3bcb4 | bellard | /* this feature is needed for Solaris and isn't fully implemented */
|
150 | a049de61 | bellard | CPUID_PSE36, |
151 | a049de61 | bellard | .ext_features = CPUID_EXT_SSE3, |
152 | a049de61 | bellard | .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
|
153 | a049de61 | bellard | CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX, |
154 | a049de61 | bellard | .ext3_features = CPUID_EXT3_SVM, |
155 | 45d242b6 | balrog | .xlevel = 0x8000000A,
|
156 | a049de61 | bellard | }, |
157 | 14ce26e7 | bellard | #endif
|
158 | a049de61 | bellard | { |
159 | a049de61 | bellard | .name = "qemu32",
|
160 | 3e98dc8e | balrog | .level = 2,
|
161 | a049de61 | bellard | .family = 6,
|
162 | a049de61 | bellard | .model = 3,
|
163 | a049de61 | bellard | .stepping = 3,
|
164 | a049de61 | bellard | .features = PPRO_FEATURES, |
165 | a049de61 | bellard | .ext_features = CPUID_EXT_SSE3, |
166 | a049de61 | bellard | .xlevel = 0,
|
167 | a049de61 | bellard | }, |
168 | a049de61 | bellard | { |
169 | a049de61 | bellard | .name = "486",
|
170 | 3e98dc8e | balrog | .level = 0,
|
171 | a049de61 | bellard | .family = 4,
|
172 | a049de61 | bellard | .model = 0,
|
173 | a049de61 | bellard | .stepping = 0,
|
174 | a049de61 | bellard | .features = 0x0000000B,
|
175 | a049de61 | bellard | .xlevel = 0,
|
176 | a049de61 | bellard | }, |
177 | a049de61 | bellard | { |
178 | a049de61 | bellard | .name = "pentium",
|
179 | 3e98dc8e | balrog | .level = 1,
|
180 | a049de61 | bellard | .family = 5,
|
181 | a049de61 | bellard | .model = 4,
|
182 | a049de61 | bellard | .stepping = 3,
|
183 | a049de61 | bellard | .features = 0x008001BF,
|
184 | a049de61 | bellard | .xlevel = 0,
|
185 | a049de61 | bellard | }, |
186 | a049de61 | bellard | { |
187 | a049de61 | bellard | .name = "pentium2",
|
188 | 3e98dc8e | balrog | .level = 2,
|
189 | a049de61 | bellard | .family = 6,
|
190 | a049de61 | bellard | .model = 5,
|
191 | a049de61 | bellard | .stepping = 2,
|
192 | a049de61 | bellard | .features = 0x0183F9FF,
|
193 | a049de61 | bellard | .xlevel = 0,
|
194 | a049de61 | bellard | }, |
195 | a049de61 | bellard | { |
196 | a049de61 | bellard | .name = "pentium3",
|
197 | 3e98dc8e | balrog | .level = 2,
|
198 | a049de61 | bellard | .family = 6,
|
199 | a049de61 | bellard | .model = 7,
|
200 | a049de61 | bellard | .stepping = 3,
|
201 | a049de61 | bellard | .features = 0x0383F9FF,
|
202 | a049de61 | bellard | .xlevel = 0,
|
203 | a049de61 | bellard | }, |
204 | a049de61 | bellard | }; |
205 | a049de61 | bellard | |
206 | aaed909a | bellard | static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model) |
207 | a049de61 | bellard | { |
208 | a049de61 | bellard | unsigned int i; |
209 | aaed909a | bellard | x86_def_t *def; |
210 | a049de61 | bellard | |
211 | a049de61 | bellard | char *s = strdup(cpu_model);
|
212 | a049de61 | bellard | char *featurestr, *name = strtok(s, ","); |
213 | a049de61 | bellard | uint32_t plus_features = 0, plus_ext_features = 0, plus_ext2_features = 0, plus_ext3_features = 0; |
214 | a049de61 | bellard | uint32_t minus_features = 0, minus_ext_features = 0, minus_ext2_features = 0, minus_ext3_features = 0; |
215 | a049de61 | bellard | int family = -1, model = -1, stepping = -1; |
216 | a049de61 | bellard | |
217 | aaed909a | bellard | def = NULL;
|
218 | a049de61 | bellard | for (i = 0; i < sizeof(x86_defs) / sizeof(x86_def_t); i++) { |
219 | a049de61 | bellard | if (strcmp(name, x86_defs[i].name) == 0) { |
220 | aaed909a | bellard | def = &x86_defs[i]; |
221 | a049de61 | bellard | break;
|
222 | a049de61 | bellard | } |
223 | 14ce26e7 | bellard | } |
224 | aaed909a | bellard | if (!def)
|
225 | a049de61 | bellard | goto error;
|
226 | aaed909a | bellard | memcpy(x86_cpu_def, def, sizeof(*def));
|
227 | a049de61 | bellard | |
228 | a049de61 | bellard | featurestr = strtok(NULL, ","); |
229 | a049de61 | bellard | |
230 | a049de61 | bellard | while (featurestr) {
|
231 | a049de61 | bellard | char *val;
|
232 | a049de61 | bellard | if (featurestr[0] == '+') { |
233 | a049de61 | bellard | add_flagname_to_bitmaps(featurestr + 1, &plus_features, &plus_ext_features, &plus_ext2_features, &plus_ext3_features);
|
234 | a049de61 | bellard | } else if (featurestr[0] == '-') { |
235 | a049de61 | bellard | add_flagname_to_bitmaps(featurestr + 1, &minus_features, &minus_ext_features, &minus_ext2_features, &minus_ext3_features);
|
236 | a049de61 | bellard | } else if ((val = strchr(featurestr, '='))) { |
237 | a049de61 | bellard | *val = 0; val++;
|
238 | a049de61 | bellard | if (!strcmp(featurestr, "family")) { |
239 | a049de61 | bellard | char *err;
|
240 | a049de61 | bellard | family = strtol(val, &err, 10);
|
241 | a049de61 | bellard | if (!*val || *err || family < 0) { |
242 | a049de61 | bellard | fprintf(stderr, "bad numerical value %s\n", val);
|
243 | a049de61 | bellard | x86_cpu_def = 0;
|
244 | a049de61 | bellard | goto error;
|
245 | a049de61 | bellard | } |
246 | a049de61 | bellard | x86_cpu_def->family = family; |
247 | a049de61 | bellard | } else if (!strcmp(featurestr, "model")) { |
248 | a049de61 | bellard | char *err;
|
249 | a049de61 | bellard | model = strtol(val, &err, 10);
|
250 | a049de61 | bellard | if (!*val || *err || model < 0 || model > 0xf) { |
251 | a049de61 | bellard | fprintf(stderr, "bad numerical value %s\n", val);
|
252 | a049de61 | bellard | x86_cpu_def = 0;
|
253 | a049de61 | bellard | goto error;
|
254 | a049de61 | bellard | } |
255 | a049de61 | bellard | x86_cpu_def->model = model; |
256 | a049de61 | bellard | } else if (!strcmp(featurestr, "stepping")) { |
257 | a049de61 | bellard | char *err;
|
258 | a049de61 | bellard | stepping = strtol(val, &err, 10);
|
259 | a049de61 | bellard | if (!*val || *err || stepping < 0 || stepping > 0xf) { |
260 | a049de61 | bellard | fprintf(stderr, "bad numerical value %s\n", val);
|
261 | a049de61 | bellard | x86_cpu_def = 0;
|
262 | a049de61 | bellard | goto error;
|
263 | a049de61 | bellard | } |
264 | a049de61 | bellard | x86_cpu_def->stepping = stepping; |
265 | a049de61 | bellard | } else {
|
266 | 867e2400 | ths | fprintf(stderr, "unrecognized feature %s\n", featurestr);
|
267 | a049de61 | bellard | x86_cpu_def = 0;
|
268 | a049de61 | bellard | goto error;
|
269 | a049de61 | bellard | } |
270 | a049de61 | bellard | } else {
|
271 | a049de61 | bellard | fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
|
272 | a049de61 | bellard | x86_cpu_def = 0;
|
273 | a049de61 | bellard | goto error;
|
274 | a049de61 | bellard | } |
275 | a049de61 | bellard | featurestr = strtok(NULL, ","); |
276 | a049de61 | bellard | } |
277 | a049de61 | bellard | x86_cpu_def->features |= plus_features; |
278 | a049de61 | bellard | x86_cpu_def->ext_features |= plus_ext_features; |
279 | a049de61 | bellard | x86_cpu_def->ext2_features |= plus_ext2_features; |
280 | a049de61 | bellard | x86_cpu_def->ext3_features |= plus_ext3_features; |
281 | a049de61 | bellard | x86_cpu_def->features &= ~minus_features; |
282 | a049de61 | bellard | x86_cpu_def->ext_features &= ~minus_ext_features; |
283 | a049de61 | bellard | x86_cpu_def->ext2_features &= ~minus_ext2_features; |
284 | a049de61 | bellard | x86_cpu_def->ext3_features &= ~minus_ext3_features; |
285 | aaed909a | bellard | free(s); |
286 | aaed909a | bellard | return 0; |
287 | a049de61 | bellard | |
288 | a049de61 | bellard | error:
|
289 | a049de61 | bellard | free(s); |
290 | aaed909a | bellard | return -1; |
291 | a049de61 | bellard | } |
292 | a049de61 | bellard | |
293 | a049de61 | bellard | void x86_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...)) |
294 | a049de61 | bellard | { |
295 | a049de61 | bellard | unsigned int i; |
296 | a049de61 | bellard | |
297 | a049de61 | bellard | for (i = 0; i < sizeof(x86_defs) / sizeof(x86_def_t); i++) |
298 | a049de61 | bellard | (*cpu_fprintf)(f, "x86 %16s\n", x86_defs[i].name);
|
299 | a049de61 | bellard | } |
300 | a049de61 | bellard | |
301 | aaed909a | bellard | static int cpu_x86_register (CPUX86State *env, const char *cpu_model) |
302 | a049de61 | bellard | { |
303 | aaed909a | bellard | x86_def_t def1, *def = &def1; |
304 | aaed909a | bellard | |
305 | aaed909a | bellard | if (cpu_x86_find_by_name(def, cpu_model) < 0) |
306 | aaed909a | bellard | return -1; |
307 | a049de61 | bellard | if (def->vendor1) {
|
308 | a049de61 | bellard | env->cpuid_vendor1 = def->vendor1; |
309 | a049de61 | bellard | env->cpuid_vendor2 = def->vendor2; |
310 | a049de61 | bellard | env->cpuid_vendor3 = def->vendor3; |
311 | a049de61 | bellard | } else {
|
312 | a049de61 | bellard | env->cpuid_vendor1 = 0x756e6547; /* "Genu" */ |
313 | a049de61 | bellard | env->cpuid_vendor2 = 0x49656e69; /* "ineI" */ |
314 | a049de61 | bellard | env->cpuid_vendor3 = 0x6c65746e; /* "ntel" */ |
315 | a049de61 | bellard | } |
316 | 3e98dc8e | balrog | env->cpuid_level = def->level; |
317 | a049de61 | bellard | env->cpuid_version = (def->family << 8) | (def->model << 4) | def->stepping; |
318 | a049de61 | bellard | env->cpuid_features = def->features; |
319 | a049de61 | bellard | env->pat = 0x0007040600070406ULL;
|
320 | a049de61 | bellard | env->cpuid_ext_features = def->ext_features; |
321 | a049de61 | bellard | env->cpuid_ext2_features = def->ext2_features; |
322 | a049de61 | bellard | env->cpuid_xlevel = def->xlevel; |
323 | a049de61 | bellard | env->cpuid_ext3_features = def->ext3_features; |
324 | a049de61 | bellard | { |
325 | a049de61 | bellard | const char *model_id = "QEMU Virtual CPU version " QEMU_VERSION; |
326 | a049de61 | bellard | int c, len, i;
|
327 | a049de61 | bellard | len = strlen(model_id); |
328 | a049de61 | bellard | for(i = 0; i < 48; i++) { |
329 | a049de61 | bellard | if (i >= len)
|
330 | a049de61 | bellard | c = '\0';
|
331 | a049de61 | bellard | else
|
332 | a049de61 | bellard | c = model_id[i]; |
333 | a049de61 | bellard | env->cpuid_model[i >> 2] |= c << (8 * (i & 3)); |
334 | a049de61 | bellard | } |
335 | a049de61 | bellard | } |
336 | a049de61 | bellard | return 0; |
337 | ffddfee3 | bellard | } |
338 | ffddfee3 | bellard | |
339 | ffddfee3 | bellard | /* NOTE: must be called outside the CPU execute loop */
|
340 | ffddfee3 | bellard | void cpu_reset(CPUX86State *env)
|
341 | ffddfee3 | bellard | { |
342 | ffddfee3 | bellard | int i;
|
343 | ffddfee3 | bellard | |
344 | ffddfee3 | bellard | memset(env, 0, offsetof(CPUX86State, breakpoints));
|
345 | 1ac157da | bellard | |
346 | 1ac157da | bellard | tlb_flush(env, 1);
|
347 | ffddfee3 | bellard | |
348 | a2cce02c | ths | env->old_exception = -1;
|
349 | a2cce02c | ths | |
350 | ffddfee3 | bellard | /* init to reset state */
|
351 | ffddfee3 | bellard | |
352 | 2c0262af | bellard | #ifdef CONFIG_SOFTMMU
|
353 | 2c0262af | bellard | env->hflags |= HF_SOFTMMU_MASK; |
354 | 2c0262af | bellard | #endif
|
355 | 0573fbfc | ths | env->hflags |= HF_GIF_MASK; |
356 | 1ac157da | bellard | |
357 | 1ac157da | bellard | cpu_x86_update_cr0(env, 0x60000010);
|
358 | 1ac157da | bellard | env->a20_mask = 0xffffffff;
|
359 | 3b21e03e | bellard | env->smbase = 0x30000;
|
360 | 3b21e03e | bellard | |
361 | 1ac157da | bellard | env->idt.limit = 0xffff;
|
362 | 1ac157da | bellard | env->gdt.limit = 0xffff;
|
363 | 1ac157da | bellard | env->ldt.limit = 0xffff;
|
364 | 1ac157da | bellard | env->ldt.flags = DESC_P_MASK; |
365 | 1ac157da | bellard | env->tr.limit = 0xffff;
|
366 | 1ac157da | bellard | env->tr.flags = DESC_P_MASK; |
367 | 3b46e624 | ths | |
368 | 5fafdf24 | ths | cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff, 0); |
369 | 14ce26e7 | bellard | cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, 0); |
370 | 14ce26e7 | bellard | cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, 0); |
371 | 14ce26e7 | bellard | cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, 0); |
372 | 14ce26e7 | bellard | cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff, 0); |
373 | 14ce26e7 | bellard | cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff, 0); |
374 | 3b46e624 | ths | |
375 | 1ac157da | bellard | env->eip = 0xfff0;
|
376 | a049de61 | bellard | env->regs[R_EDX] = env->cpuid_version; |
377 | 3b46e624 | ths | |
378 | 1ac157da | bellard | env->eflags = 0x2;
|
379 | 3b46e624 | ths | |
380 | 1ac157da | bellard | /* FPU init */
|
381 | 1ac157da | bellard | for(i = 0;i < 8; i++) |
382 | 1ac157da | bellard | env->fptags[i] = 1;
|
383 | 1ac157da | bellard | env->fpuc = 0x37f;
|
384 | 664e0f19 | bellard | |
385 | 664e0f19 | bellard | env->mxcsr = 0x1f80;
|
386 | 2c0262af | bellard | } |
387 | 2c0262af | bellard | |
388 | 2c0262af | bellard | void cpu_x86_close(CPUX86State *env)
|
389 | 2c0262af | bellard | { |
390 | 2c0262af | bellard | free(env); |
391 | 2c0262af | bellard | } |
392 | 2c0262af | bellard | |
393 | 2c0262af | bellard | /***********************************************************/
|
394 | 2c0262af | bellard | /* x86 debug */
|
395 | 2c0262af | bellard | |
396 | 2c0262af | bellard | static const char *cc_op_str[] = { |
397 | 2c0262af | bellard | "DYNAMIC",
|
398 | 2c0262af | bellard | "EFLAGS",
|
399 | 14ce26e7 | bellard | |
400 | b7f0f463 | bellard | "MULB",
|
401 | b7f0f463 | bellard | "MULW",
|
402 | b7f0f463 | bellard | "MULL",
|
403 | 14ce26e7 | bellard | "MULQ",
|
404 | 14ce26e7 | bellard | |
405 | 2c0262af | bellard | "ADDB",
|
406 | 2c0262af | bellard | "ADDW",
|
407 | 2c0262af | bellard | "ADDL",
|
408 | 14ce26e7 | bellard | "ADDQ",
|
409 | 14ce26e7 | bellard | |
410 | 2c0262af | bellard | "ADCB",
|
411 | 2c0262af | bellard | "ADCW",
|
412 | 2c0262af | bellard | "ADCL",
|
413 | 14ce26e7 | bellard | "ADCQ",
|
414 | 14ce26e7 | bellard | |
415 | 2c0262af | bellard | "SUBB",
|
416 | 2c0262af | bellard | "SUBW",
|
417 | 2c0262af | bellard | "SUBL",
|
418 | 14ce26e7 | bellard | "SUBQ",
|
419 | 14ce26e7 | bellard | |
420 | 2c0262af | bellard | "SBBB",
|
421 | 2c0262af | bellard | "SBBW",
|
422 | 2c0262af | bellard | "SBBL",
|
423 | 14ce26e7 | bellard | "SBBQ",
|
424 | 14ce26e7 | bellard | |
425 | 2c0262af | bellard | "LOGICB",
|
426 | 2c0262af | bellard | "LOGICW",
|
427 | 2c0262af | bellard | "LOGICL",
|
428 | 14ce26e7 | bellard | "LOGICQ",
|
429 | 14ce26e7 | bellard | |
430 | 2c0262af | bellard | "INCB",
|
431 | 2c0262af | bellard | "INCW",
|
432 | 2c0262af | bellard | "INCL",
|
433 | 14ce26e7 | bellard | "INCQ",
|
434 | 14ce26e7 | bellard | |
435 | 2c0262af | bellard | "DECB",
|
436 | 2c0262af | bellard | "DECW",
|
437 | 2c0262af | bellard | "DECL",
|
438 | 14ce26e7 | bellard | "DECQ",
|
439 | 14ce26e7 | bellard | |
440 | 2c0262af | bellard | "SHLB",
|
441 | 2c0262af | bellard | "SHLW",
|
442 | 2c0262af | bellard | "SHLL",
|
443 | 14ce26e7 | bellard | "SHLQ",
|
444 | 14ce26e7 | bellard | |
445 | 2c0262af | bellard | "SARB",
|
446 | 2c0262af | bellard | "SARW",
|
447 | 2c0262af | bellard | "SARL",
|
448 | 14ce26e7 | bellard | "SARQ",
|
449 | 2c0262af | bellard | }; |
450 | 2c0262af | bellard | |
451 | 5fafdf24 | ths | void cpu_dump_state(CPUState *env, FILE *f,
|
452 | 7fe48483 | bellard | int (*cpu_fprintf)(FILE *f, const char *fmt, ...), |
453 | 7fe48483 | bellard | int flags)
|
454 | 2c0262af | bellard | { |
455 | 2157fa06 | bellard | int eflags, i, nb;
|
456 | 2c0262af | bellard | char cc_op_name[32]; |
457 | 246d897f | bellard | static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" }; |
458 | 2c0262af | bellard | |
459 | 2c0262af | bellard | eflags = env->eflags; |
460 | 14ce26e7 | bellard | #ifdef TARGET_X86_64
|
461 | 14ce26e7 | bellard | if (env->hflags & HF_CS64_MASK) {
|
462 | 5fafdf24 | ths | cpu_fprintf(f, |
463 | 26a76461 | bellard | "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n" |
464 | 26a76461 | bellard | "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n" |
465 | 26a76461 | bellard | "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n" |
466 | 26a76461 | bellard | "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n" |
467 | 3b21e03e | bellard | "RIP=%016" PRIx64 " RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n", |
468 | 5fafdf24 | ths | env->regs[R_EAX], |
469 | 5fafdf24 | ths | env->regs[R_EBX], |
470 | 5fafdf24 | ths | env->regs[R_ECX], |
471 | 5fafdf24 | ths | env->regs[R_EDX], |
472 | 5fafdf24 | ths | env->regs[R_ESI], |
473 | 5fafdf24 | ths | env->regs[R_EDI], |
474 | 5fafdf24 | ths | env->regs[R_EBP], |
475 | 5fafdf24 | ths | env->regs[R_ESP], |
476 | 5fafdf24 | ths | env->regs[8],
|
477 | 5fafdf24 | ths | env->regs[9],
|
478 | 5fafdf24 | ths | env->regs[10],
|
479 | 5fafdf24 | ths | env->regs[11],
|
480 | 5fafdf24 | ths | env->regs[12],
|
481 | 5fafdf24 | ths | env->regs[13],
|
482 | 5fafdf24 | ths | env->regs[14],
|
483 | 5fafdf24 | ths | env->regs[15],
|
484 | 14ce26e7 | bellard | env->eip, eflags, |
485 | 14ce26e7 | bellard | eflags & DF_MASK ? 'D' : '-', |
486 | 14ce26e7 | bellard | eflags & CC_O ? 'O' : '-', |
487 | 14ce26e7 | bellard | eflags & CC_S ? 'S' : '-', |
488 | 14ce26e7 | bellard | eflags & CC_Z ? 'Z' : '-', |
489 | 14ce26e7 | bellard | eflags & CC_A ? 'A' : '-', |
490 | 14ce26e7 | bellard | eflags & CC_P ? 'P' : '-', |
491 | 14ce26e7 | bellard | eflags & CC_C ? 'C' : '-', |
492 | 5fafdf24 | ths | env->hflags & HF_CPL_MASK, |
493 | 14ce26e7 | bellard | (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
|
494 | d2ac63e0 | bellard | (env->a20_mask >> 20) & 1, |
495 | 3b21e03e | bellard | (env->hflags >> HF_SMM_SHIFT) & 1,
|
496 | d2ac63e0 | bellard | (env->hflags >> HF_HALTED_SHIFT) & 1);
|
497 | 5fafdf24 | ths | } else
|
498 | 14ce26e7 | bellard | #endif
|
499 | 14ce26e7 | bellard | { |
500 | 14ce26e7 | bellard | cpu_fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
|
501 | 14ce26e7 | bellard | "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
|
502 | 3b21e03e | bellard | "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
|
503 | 5fafdf24 | ths | (uint32_t)env->regs[R_EAX], |
504 | 5fafdf24 | ths | (uint32_t)env->regs[R_EBX], |
505 | 5fafdf24 | ths | (uint32_t)env->regs[R_ECX], |
506 | 5fafdf24 | ths | (uint32_t)env->regs[R_EDX], |
507 | 5fafdf24 | ths | (uint32_t)env->regs[R_ESI], |
508 | 5fafdf24 | ths | (uint32_t)env->regs[R_EDI], |
509 | 5fafdf24 | ths | (uint32_t)env->regs[R_EBP], |
510 | 5fafdf24 | ths | (uint32_t)env->regs[R_ESP], |
511 | 14ce26e7 | bellard | (uint32_t)env->eip, eflags, |
512 | 14ce26e7 | bellard | eflags & DF_MASK ? 'D' : '-', |
513 | 14ce26e7 | bellard | eflags & CC_O ? 'O' : '-', |
514 | 14ce26e7 | bellard | eflags & CC_S ? 'S' : '-', |
515 | 14ce26e7 | bellard | eflags & CC_Z ? 'Z' : '-', |
516 | 14ce26e7 | bellard | eflags & CC_A ? 'A' : '-', |
517 | 14ce26e7 | bellard | eflags & CC_P ? 'P' : '-', |
518 | 14ce26e7 | bellard | eflags & CC_C ? 'C' : '-', |
519 | 5fafdf24 | ths | env->hflags & HF_CPL_MASK, |
520 | 14ce26e7 | bellard | (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
|
521 | d2ac63e0 | bellard | (env->a20_mask >> 20) & 1, |
522 | 3b21e03e | bellard | (env->hflags >> HF_SMM_SHIFT) & 1,
|
523 | d2ac63e0 | bellard | (env->hflags >> HF_HALTED_SHIFT) & 1);
|
524 | 14ce26e7 | bellard | } |
525 | 14ce26e7 | bellard | |
526 | 14ce26e7 | bellard | #ifdef TARGET_X86_64
|
527 | 14ce26e7 | bellard | if (env->hflags & HF_LMA_MASK) {
|
528 | 14ce26e7 | bellard | for(i = 0; i < 6; i++) { |
529 | 14ce26e7 | bellard | SegmentCache *sc = &env->segs[i]; |
530 | 26a76461 | bellard | cpu_fprintf(f, "%s =%04x %016" PRIx64 " %08x %08x\n", |
531 | 14ce26e7 | bellard | seg_name[i], |
532 | 14ce26e7 | bellard | sc->selector, |
533 | 14ce26e7 | bellard | sc->base, |
534 | 14ce26e7 | bellard | sc->limit, |
535 | 14ce26e7 | bellard | sc->flags); |
536 | 14ce26e7 | bellard | } |
537 | 26a76461 | bellard | cpu_fprintf(f, "LDT=%04x %016" PRIx64 " %08x %08x\n", |
538 | 14ce26e7 | bellard | env->ldt.selector, |
539 | 14ce26e7 | bellard | env->ldt.base, |
540 | 14ce26e7 | bellard | env->ldt.limit, |
541 | 14ce26e7 | bellard | env->ldt.flags); |
542 | 26a76461 | bellard | cpu_fprintf(f, "TR =%04x %016" PRIx64 " %08x %08x\n", |
543 | 14ce26e7 | bellard | env->tr.selector, |
544 | 14ce26e7 | bellard | env->tr.base, |
545 | 14ce26e7 | bellard | env->tr.limit, |
546 | 14ce26e7 | bellard | env->tr.flags); |
547 | 26a76461 | bellard | cpu_fprintf(f, "GDT= %016" PRIx64 " %08x\n", |
548 | 14ce26e7 | bellard | env->gdt.base, env->gdt.limit); |
549 | 26a76461 | bellard | cpu_fprintf(f, "IDT= %016" PRIx64 " %08x\n", |
550 | 14ce26e7 | bellard | env->idt.base, env->idt.limit); |
551 | 26a76461 | bellard | cpu_fprintf(f, "CR0=%08x CR2=%016" PRIx64 " CR3=%016" PRIx64 " CR4=%08x\n", |
552 | 5fafdf24 | ths | (uint32_t)env->cr[0],
|
553 | 5fafdf24 | ths | env->cr[2],
|
554 | 5fafdf24 | ths | env->cr[3],
|
555 | 14ce26e7 | bellard | (uint32_t)env->cr[4]);
|
556 | 14ce26e7 | bellard | } else
|
557 | 14ce26e7 | bellard | #endif
|
558 | 14ce26e7 | bellard | { |
559 | 14ce26e7 | bellard | for(i = 0; i < 6; i++) { |
560 | 14ce26e7 | bellard | SegmentCache *sc = &env->segs[i]; |
561 | 14ce26e7 | bellard | cpu_fprintf(f, "%s =%04x %08x %08x %08x\n",
|
562 | 14ce26e7 | bellard | seg_name[i], |
563 | 14ce26e7 | bellard | sc->selector, |
564 | 14ce26e7 | bellard | (uint32_t)sc->base, |
565 | 14ce26e7 | bellard | sc->limit, |
566 | 14ce26e7 | bellard | sc->flags); |
567 | 14ce26e7 | bellard | } |
568 | 14ce26e7 | bellard | cpu_fprintf(f, "LDT=%04x %08x %08x %08x\n",
|
569 | 14ce26e7 | bellard | env->ldt.selector, |
570 | 14ce26e7 | bellard | (uint32_t)env->ldt.base, |
571 | 14ce26e7 | bellard | env->ldt.limit, |
572 | 14ce26e7 | bellard | env->ldt.flags); |
573 | 14ce26e7 | bellard | cpu_fprintf(f, "TR =%04x %08x %08x %08x\n",
|
574 | 14ce26e7 | bellard | env->tr.selector, |
575 | 14ce26e7 | bellard | (uint32_t)env->tr.base, |
576 | 14ce26e7 | bellard | env->tr.limit, |
577 | 14ce26e7 | bellard | env->tr.flags); |
578 | 14ce26e7 | bellard | cpu_fprintf(f, "GDT= %08x %08x\n",
|
579 | 14ce26e7 | bellard | (uint32_t)env->gdt.base, env->gdt.limit); |
580 | 14ce26e7 | bellard | cpu_fprintf(f, "IDT= %08x %08x\n",
|
581 | 14ce26e7 | bellard | (uint32_t)env->idt.base, env->idt.limit); |
582 | 14ce26e7 | bellard | cpu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
|
583 | 5fafdf24 | ths | (uint32_t)env->cr[0],
|
584 | 5fafdf24 | ths | (uint32_t)env->cr[2],
|
585 | 5fafdf24 | ths | (uint32_t)env->cr[3],
|
586 | 14ce26e7 | bellard | (uint32_t)env->cr[4]);
|
587 | 246d897f | bellard | } |
588 | 2c0262af | bellard | if (flags & X86_DUMP_CCOP) {
|
589 | 2c0262af | bellard | if ((unsigned)env->cc_op < CC_OP_NB) |
590 | eba2af63 | bellard | snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]); |
591 | 2c0262af | bellard | else
|
592 | 2c0262af | bellard | snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op); |
593 | 14ce26e7 | bellard | #ifdef TARGET_X86_64
|
594 | 14ce26e7 | bellard | if (env->hflags & HF_CS64_MASK) {
|
595 | 26a76461 | bellard | cpu_fprintf(f, "CCS=%016" PRIx64 " CCD=%016" PRIx64 " CCO=%-8s\n", |
596 | 5fafdf24 | ths | env->cc_src, env->cc_dst, |
597 | 14ce26e7 | bellard | cc_op_name); |
598 | 5fafdf24 | ths | } else
|
599 | 14ce26e7 | bellard | #endif
|
600 | 14ce26e7 | bellard | { |
601 | 14ce26e7 | bellard | cpu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
|
602 | 5fafdf24 | ths | (uint32_t)env->cc_src, (uint32_t)env->cc_dst, |
603 | 14ce26e7 | bellard | cc_op_name); |
604 | 14ce26e7 | bellard | } |
605 | 2c0262af | bellard | } |
606 | 2c0262af | bellard | if (flags & X86_DUMP_FPU) {
|
607 | 2157fa06 | bellard | int fptag;
|
608 | 2157fa06 | bellard | fptag = 0;
|
609 | 2157fa06 | bellard | for(i = 0; i < 8; i++) { |
610 | 2157fa06 | bellard | fptag |= ((!env->fptags[i]) << i); |
611 | 2157fa06 | bellard | } |
612 | 2157fa06 | bellard | cpu_fprintf(f, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
|
613 | 2157fa06 | bellard | env->fpuc, |
614 | 2157fa06 | bellard | (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11, |
615 | 2157fa06 | bellard | env->fpstt, |
616 | 2157fa06 | bellard | fptag, |
617 | 2157fa06 | bellard | env->mxcsr); |
618 | 2157fa06 | bellard | for(i=0;i<8;i++) { |
619 | 2157fa06 | bellard | #if defined(USE_X86LDOUBLE)
|
620 | 2157fa06 | bellard | union {
|
621 | 2157fa06 | bellard | long double d; |
622 | 2157fa06 | bellard | struct {
|
623 | 2157fa06 | bellard | uint64_t lower; |
624 | 2157fa06 | bellard | uint16_t upper; |
625 | 2157fa06 | bellard | } l; |
626 | 2157fa06 | bellard | } tmp; |
627 | 2157fa06 | bellard | tmp.d = env->fpregs[i].d; |
628 | 26a76461 | bellard | cpu_fprintf(f, "FPR%d=%016" PRIx64 " %04x", |
629 | 2157fa06 | bellard | i, tmp.l.lower, tmp.l.upper); |
630 | 2157fa06 | bellard | #else
|
631 | 26a76461 | bellard | cpu_fprintf(f, "FPR%d=%016" PRIx64,
|
632 | 2157fa06 | bellard | i, env->fpregs[i].mmx.q); |
633 | 2157fa06 | bellard | #endif
|
634 | 2157fa06 | bellard | if ((i & 1) == 1) |
635 | 2157fa06 | bellard | cpu_fprintf(f, "\n");
|
636 | 2157fa06 | bellard | else
|
637 | 2157fa06 | bellard | cpu_fprintf(f, " ");
|
638 | 2157fa06 | bellard | } |
639 | 5fafdf24 | ths | if (env->hflags & HF_CS64_MASK)
|
640 | 2157fa06 | bellard | nb = 16;
|
641 | 2157fa06 | bellard | else
|
642 | 2157fa06 | bellard | nb = 8;
|
643 | 2157fa06 | bellard | for(i=0;i<nb;i++) { |
644 | 2157fa06 | bellard | cpu_fprintf(f, "XMM%02d=%08x%08x%08x%08x",
|
645 | 5fafdf24 | ths | i, |
646 | 2157fa06 | bellard | env->xmm_regs[i].XMM_L(3),
|
647 | 2157fa06 | bellard | env->xmm_regs[i].XMM_L(2),
|
648 | 2157fa06 | bellard | env->xmm_regs[i].XMM_L(1),
|
649 | 2157fa06 | bellard | env->xmm_regs[i].XMM_L(0));
|
650 | 2157fa06 | bellard | if ((i & 1) == 1) |
651 | 2157fa06 | bellard | cpu_fprintf(f, "\n");
|
652 | 2157fa06 | bellard | else
|
653 | 2157fa06 | bellard | cpu_fprintf(f, " ");
|
654 | 2157fa06 | bellard | } |
655 | 2c0262af | bellard | } |
656 | 2c0262af | bellard | } |
657 | 2c0262af | bellard | |
658 | 2c0262af | bellard | /***********************************************************/
|
659 | 2c0262af | bellard | /* x86 mmu */
|
660 | 2c0262af | bellard | /* XXX: add PGE support */
|
661 | 2c0262af | bellard | |
662 | 461c0471 | bellard | void cpu_x86_set_a20(CPUX86State *env, int a20_state) |
663 | 461c0471 | bellard | { |
664 | 461c0471 | bellard | a20_state = (a20_state != 0);
|
665 | 1ac157da | bellard | if (a20_state != ((env->a20_mask >> 20) & 1)) { |
666 | b7f0f463 | bellard | #if defined(DEBUG_MMU)
|
667 | b7f0f463 | bellard | printf("A20 update: a20=%d\n", a20_state);
|
668 | b7f0f463 | bellard | #endif
|
669 | 6bb70571 | bellard | /* if the cpu is currently executing code, we must unlink it and
|
670 | 6bb70571 | bellard | all the potentially executing TB */
|
671 | 0e4b179d | bellard | cpu_interrupt(env, CPU_INTERRUPT_EXITTB); |
672 | 6bb70571 | bellard | |
673 | 461c0471 | bellard | /* when a20 is changed, all the MMU mappings are invalid, so
|
674 | 461c0471 | bellard | we must flush everything */
|
675 | 1ac157da | bellard | tlb_flush(env, 1);
|
676 | 1ac157da | bellard | env->a20_mask = 0xffefffff | (a20_state << 20); |
677 | 461c0471 | bellard | } |
678 | 461c0471 | bellard | } |
679 | 461c0471 | bellard | |
680 | 1ac157da | bellard | void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
|
681 | 2c0262af | bellard | { |
682 | 1ac157da | bellard | int pe_state;
|
683 | 2c0262af | bellard | |
684 | b7f0f463 | bellard | #if defined(DEBUG_MMU)
|
685 | 1ac157da | bellard | printf("CR0 update: CR0=0x%08x\n", new_cr0);
|
686 | 2c0262af | bellard | #endif
|
687 | 1ac157da | bellard | if ((new_cr0 & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK)) !=
|
688 | 1ac157da | bellard | (env->cr[0] & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK))) {
|
689 | 1ac157da | bellard | tlb_flush(env, 1);
|
690 | 2c0262af | bellard | } |
691 | 14ce26e7 | bellard | |
692 | 14ce26e7 | bellard | #ifdef TARGET_X86_64
|
693 | 14ce26e7 | bellard | if (!(env->cr[0] & CR0_PG_MASK) && (new_cr0 & CR0_PG_MASK) && |
694 | 14ce26e7 | bellard | (env->efer & MSR_EFER_LME)) { |
695 | 14ce26e7 | bellard | /* enter in long mode */
|
696 | 14ce26e7 | bellard | /* XXX: generate an exception */
|
697 | 14ce26e7 | bellard | if (!(env->cr[4] & CR4_PAE_MASK)) |
698 | 14ce26e7 | bellard | return;
|
699 | 14ce26e7 | bellard | env->efer |= MSR_EFER_LMA; |
700 | 14ce26e7 | bellard | env->hflags |= HF_LMA_MASK; |
701 | 14ce26e7 | bellard | } else if ((env->cr[0] & CR0_PG_MASK) && !(new_cr0 & CR0_PG_MASK) && |
702 | 14ce26e7 | bellard | (env->efer & MSR_EFER_LMA)) { |
703 | 14ce26e7 | bellard | /* exit long mode */
|
704 | 14ce26e7 | bellard | env->efer &= ~MSR_EFER_LMA; |
705 | 14ce26e7 | bellard | env->hflags &= ~(HF_LMA_MASK | HF_CS64_MASK); |
706 | 14ce26e7 | bellard | env->eip &= 0xffffffff;
|
707 | 14ce26e7 | bellard | } |
708 | 14ce26e7 | bellard | #endif
|
709 | 28c3ee3f | bellard | env->cr[0] = new_cr0 | CR0_ET_MASK;
|
710 | 3b46e624 | ths | |
711 | 436d8b89 | bellard | /* update PE flag in hidden flags */
|
712 | 436d8b89 | bellard | pe_state = (env->cr[0] & CR0_PE_MASK);
|
713 | 436d8b89 | bellard | env->hflags = (env->hflags & ~HF_PE_MASK) | (pe_state << HF_PE_SHIFT); |
714 | 436d8b89 | bellard | /* ensure that ADDSEG is always set in real mode */
|
715 | 436d8b89 | bellard | env->hflags |= ((pe_state ^ 1) << HF_ADDSEG_SHIFT);
|
716 | 9588b95a | bellard | /* update FPU flags */
|
717 | 9588b95a | bellard | env->hflags = (env->hflags & ~(HF_MP_MASK | HF_EM_MASK | HF_TS_MASK)) | |
718 | 9588b95a | bellard | ((new_cr0 << (HF_MP_SHIFT - 1)) & (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK));
|
719 | 2c0262af | bellard | } |
720 | 2c0262af | bellard | |
721 | bf079a1e | bellard | /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
|
722 | bf079a1e | bellard | the PDPT */
|
723 | 14ce26e7 | bellard | void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
|
724 | 2c0262af | bellard | { |
725 | 1ac157da | bellard | env->cr[3] = new_cr3;
|
726 | 2c0262af | bellard | if (env->cr[0] & CR0_PG_MASK) { |
727 | 2c0262af | bellard | #if defined(DEBUG_MMU)
|
728 | 14ce26e7 | bellard | printf("CR3 update: CR3=" TARGET_FMT_lx "\n", new_cr3); |
729 | 2c0262af | bellard | #endif
|
730 | 1ac157da | bellard | tlb_flush(env, 0);
|
731 | 2c0262af | bellard | } |
732 | 2c0262af | bellard | } |
733 | 2c0262af | bellard | |
734 | 1ac157da | bellard | void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
|
735 | 2c0262af | bellard | { |
736 | 1ac157da | bellard | #if defined(DEBUG_MMU)
|
737 | 14ce26e7 | bellard | printf("CR4 update: CR4=%08x\n", (uint32_t)env->cr[4]); |
738 | 1ac157da | bellard | #endif
|
739 | 1ac157da | bellard | if ((new_cr4 & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK)) !=
|
740 | 1ac157da | bellard | (env->cr[4] & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK))) {
|
741 | 1ac157da | bellard | tlb_flush(env, 1);
|
742 | 1ac157da | bellard | } |
743 | 664e0f19 | bellard | /* SSE handling */
|
744 | 664e0f19 | bellard | if (!(env->cpuid_features & CPUID_SSE))
|
745 | 664e0f19 | bellard | new_cr4 &= ~CR4_OSFXSR_MASK; |
746 | 664e0f19 | bellard | if (new_cr4 & CR4_OSFXSR_MASK)
|
747 | 664e0f19 | bellard | env->hflags |= HF_OSFXSR_MASK; |
748 | 664e0f19 | bellard | else
|
749 | 664e0f19 | bellard | env->hflags &= ~HF_OSFXSR_MASK; |
750 | 664e0f19 | bellard | |
751 | 1ac157da | bellard | env->cr[4] = new_cr4;
|
752 | 2c0262af | bellard | } |
753 | 2c0262af | bellard | |
754 | 2c0262af | bellard | /* XXX: also flush 4MB pages */
|
755 | 8f091a59 | bellard | void cpu_x86_flush_tlb(CPUX86State *env, target_ulong addr)
|
756 | 2c0262af | bellard | { |
757 | 2c0262af | bellard | tlb_flush_page(env, addr); |
758 | 2c0262af | bellard | } |
759 | 2c0262af | bellard | |
760 | 5fafdf24 | ths | #if defined(CONFIG_USER_ONLY)
|
761 | 14ce26e7 | bellard | |
762 | 5fafdf24 | ths | int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
|
763 | 6ebbf390 | j_mayer | int is_write, int mmu_idx, int is_softmmu) |
764 | 14ce26e7 | bellard | { |
765 | 8df1cd07 | bellard | /* user mode only emulation */
|
766 | 8df1cd07 | bellard | is_write &= 1;
|
767 | 8df1cd07 | bellard | env->cr[2] = addr;
|
768 | 8df1cd07 | bellard | env->error_code = (is_write << PG_ERROR_W_BIT); |
769 | 8df1cd07 | bellard | env->error_code |= PG_ERROR_U_MASK; |
770 | 54ca9095 | bellard | env->exception_index = EXCP0E_PAGE; |
771 | 8df1cd07 | bellard | return 1; |
772 | 14ce26e7 | bellard | } |
773 | 14ce26e7 | bellard | |
774 | 9b3c35e0 | j_mayer | target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr) |
775 | 14ce26e7 | bellard | { |
776 | 8df1cd07 | bellard | return addr;
|
777 | 14ce26e7 | bellard | } |
778 | 14ce26e7 | bellard | |
779 | 8df1cd07 | bellard | #else
|
780 | 8df1cd07 | bellard | |
781 | 4b4f782c | bellard | #define PHYS_ADDR_MASK 0xfffff000 |
782 | 4b4f782c | bellard | |
783 | 2c0262af | bellard | /* return value:
|
784 | 5fafdf24 | ths | -1 = cannot handle fault
|
785 | 5fafdf24 | ths | 0 = nothing more to do
|
786 | 2c0262af | bellard | 1 = generate PF fault
|
787 | 2c0262af | bellard | 2 = soft MMU activation required for this block
|
788 | 2c0262af | bellard | */
|
789 | 5fafdf24 | ths | int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
|
790 | 6ebbf390 | j_mayer | int is_write1, int mmu_idx, int is_softmmu) |
791 | 2c0262af | bellard | { |
792 | 4b4f782c | bellard | uint64_t ptep, pte; |
793 | 14ce26e7 | bellard | uint32_t pdpe_addr, pde_addr, pte_addr; |
794 | 6ebbf390 | j_mayer | int error_code, is_dirty, prot, page_size, ret, is_write, is_user;
|
795 | 14ce26e7 | bellard | unsigned long paddr, page_offset; |
796 | 14ce26e7 | bellard | target_ulong vaddr, virt_addr; |
797 | 3b46e624 | ths | |
798 | 6ebbf390 | j_mayer | is_user = mmu_idx == MMU_USER_IDX; |
799 | 436d8b89 | bellard | #if defined(DEBUG_MMU)
|
800 | 5fafdf24 | ths | printf("MMU fault: addr=" TARGET_FMT_lx " w=%d u=%d eip=" TARGET_FMT_lx "\n", |
801 | 4b4f782c | bellard | addr, is_write1, is_user, env->eip); |
802 | 2c0262af | bellard | #endif
|
803 | 4b4f782c | bellard | is_write = is_write1 & 1;
|
804 | 3b46e624 | ths | |
805 | 2c0262af | bellard | if (!(env->cr[0] & CR0_PG_MASK)) { |
806 | 2c0262af | bellard | pte = addr; |
807 | 461c0471 | bellard | virt_addr = addr & TARGET_PAGE_MASK; |
808 | 4b4f782c | bellard | prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
809 | 2c0262af | bellard | page_size = 4096;
|
810 | 2c0262af | bellard | goto do_mapping;
|
811 | 2c0262af | bellard | } |
812 | 2c0262af | bellard | |
813 | 14ce26e7 | bellard | if (env->cr[4] & CR4_PAE_MASK) { |
814 | 4b4f782c | bellard | uint64_t pde, pdpe; |
815 | 4b4f782c | bellard | |
816 | 14ce26e7 | bellard | /* XXX: we only use 32 bit physical addresses */
|
817 | 14ce26e7 | bellard | #ifdef TARGET_X86_64
|
818 | 14ce26e7 | bellard | if (env->hflags & HF_LMA_MASK) {
|
819 | 4b4f782c | bellard | uint32_t pml4e_addr; |
820 | 4b4f782c | bellard | uint64_t pml4e; |
821 | 14ce26e7 | bellard | int32_t sext; |
822 | 14ce26e7 | bellard | |
823 | 14ce26e7 | bellard | /* test virtual address sign extension */
|
824 | 14ce26e7 | bellard | sext = (int64_t)addr >> 47;
|
825 | 14ce26e7 | bellard | if (sext != 0 && sext != -1) { |
826 | 54ca9095 | bellard | env->error_code = 0;
|
827 | 54ca9095 | bellard | env->exception_index = EXCP0D_GPF; |
828 | 54ca9095 | bellard | return 1; |
829 | 14ce26e7 | bellard | } |
830 | 3b46e624 | ths | |
831 | 5fafdf24 | ths | pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) & |
832 | 14ce26e7 | bellard | env->a20_mask; |
833 | 4b4f782c | bellard | pml4e = ldq_phys(pml4e_addr); |
834 | 14ce26e7 | bellard | if (!(pml4e & PG_PRESENT_MASK)) {
|
835 | 14ce26e7 | bellard | error_code = 0;
|
836 | 14ce26e7 | bellard | goto do_fault;
|
837 | 14ce26e7 | bellard | } |
838 | 4b4f782c | bellard | if (!(env->efer & MSR_EFER_NXE) && (pml4e & PG_NX_MASK)) {
|
839 | 4b4f782c | bellard | error_code = PG_ERROR_RSVD_MASK; |
840 | 4b4f782c | bellard | goto do_fault;
|
841 | 4b4f782c | bellard | } |
842 | 14ce26e7 | bellard | if (!(pml4e & PG_ACCESSED_MASK)) {
|
843 | 14ce26e7 | bellard | pml4e |= PG_ACCESSED_MASK; |
844 | 8df1cd07 | bellard | stl_phys_notdirty(pml4e_addr, pml4e); |
845 | 14ce26e7 | bellard | } |
846 | 4b4f782c | bellard | ptep = pml4e ^ PG_NX_MASK; |
847 | 5fafdf24 | ths | pdpe_addr = ((pml4e & PHYS_ADDR_MASK) + (((addr >> 30) & 0x1ff) << 3)) & |
848 | 14ce26e7 | bellard | env->a20_mask; |
849 | 4b4f782c | bellard | pdpe = ldq_phys(pdpe_addr); |
850 | 14ce26e7 | bellard | if (!(pdpe & PG_PRESENT_MASK)) {
|
851 | 14ce26e7 | bellard | error_code = 0;
|
852 | 14ce26e7 | bellard | goto do_fault;
|
853 | 14ce26e7 | bellard | } |
854 | 4b4f782c | bellard | if (!(env->efer & MSR_EFER_NXE) && (pdpe & PG_NX_MASK)) {
|
855 | 4b4f782c | bellard | error_code = PG_ERROR_RSVD_MASK; |
856 | 4b4f782c | bellard | goto do_fault;
|
857 | 4b4f782c | bellard | } |
858 | 4b4f782c | bellard | ptep &= pdpe ^ PG_NX_MASK; |
859 | 14ce26e7 | bellard | if (!(pdpe & PG_ACCESSED_MASK)) {
|
860 | 14ce26e7 | bellard | pdpe |= PG_ACCESSED_MASK; |
861 | 8df1cd07 | bellard | stl_phys_notdirty(pdpe_addr, pdpe); |
862 | 14ce26e7 | bellard | } |
863 | 4b4f782c | bellard | } else
|
864 | 14ce26e7 | bellard | #endif
|
865 | 14ce26e7 | bellard | { |
866 | 4b4f782c | bellard | /* XXX: load them when cr3 is loaded ? */
|
867 | 5fafdf24 | ths | pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) & |
868 | 14ce26e7 | bellard | env->a20_mask; |
869 | 4b4f782c | bellard | pdpe = ldq_phys(pdpe_addr); |
870 | 14ce26e7 | bellard | if (!(pdpe & PG_PRESENT_MASK)) {
|
871 | 14ce26e7 | bellard | error_code = 0;
|
872 | 14ce26e7 | bellard | goto do_fault;
|
873 | 14ce26e7 | bellard | } |
874 | 4b4f782c | bellard | ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK; |
875 | 10f0e412 | bellard | } |
876 | 14ce26e7 | bellard | |
877 | 4b4f782c | bellard | pde_addr = ((pdpe & PHYS_ADDR_MASK) + (((addr >> 21) & 0x1ff) << 3)) & |
878 | 14ce26e7 | bellard | env->a20_mask; |
879 | 4b4f782c | bellard | pde = ldq_phys(pde_addr); |
880 | 14ce26e7 | bellard | if (!(pde & PG_PRESENT_MASK)) {
|
881 | 14ce26e7 | bellard | error_code = 0;
|
882 | 14ce26e7 | bellard | goto do_fault;
|
883 | 2c0262af | bellard | } |
884 | 4b4f782c | bellard | if (!(env->efer & MSR_EFER_NXE) && (pde & PG_NX_MASK)) {
|
885 | 4b4f782c | bellard | error_code = PG_ERROR_RSVD_MASK; |
886 | 4b4f782c | bellard | goto do_fault;
|
887 | 4b4f782c | bellard | } |
888 | 4b4f782c | bellard | ptep &= pde ^ PG_NX_MASK; |
889 | 14ce26e7 | bellard | if (pde & PG_PSE_MASK) {
|
890 | 14ce26e7 | bellard | /* 2 MB page */
|
891 | 14ce26e7 | bellard | page_size = 2048 * 1024; |
892 | 4b4f782c | bellard | ptep ^= PG_NX_MASK; |
893 | 4b4f782c | bellard | if ((ptep & PG_NX_MASK) && is_write1 == 2) |
894 | 4b4f782c | bellard | goto do_fault_protect;
|
895 | 4b4f782c | bellard | if (is_user) {
|
896 | 4b4f782c | bellard | if (!(ptep & PG_USER_MASK))
|
897 | 4b4f782c | bellard | goto do_fault_protect;
|
898 | 4b4f782c | bellard | if (is_write && !(ptep & PG_RW_MASK))
|
899 | 4b4f782c | bellard | goto do_fault_protect;
|
900 | 4b4f782c | bellard | } else {
|
901 | 5fafdf24 | ths | if ((env->cr[0] & CR0_WP_MASK) && |
902 | 5fafdf24 | ths | is_write && !(ptep & PG_RW_MASK)) |
903 | 4b4f782c | bellard | goto do_fault_protect;
|
904 | 4b4f782c | bellard | } |
905 | 4b4f782c | bellard | is_dirty = is_write && !(pde & PG_DIRTY_MASK); |
906 | 4b4f782c | bellard | if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
|
907 | 4b4f782c | bellard | pde |= PG_ACCESSED_MASK; |
908 | 4b4f782c | bellard | if (is_dirty)
|
909 | 4b4f782c | bellard | pde |= PG_DIRTY_MASK; |
910 | 4b4f782c | bellard | stl_phys_notdirty(pde_addr, pde); |
911 | 4b4f782c | bellard | } |
912 | 4b4f782c | bellard | /* align to page_size */
|
913 | 5fafdf24 | ths | pte = pde & ((PHYS_ADDR_MASK & ~(page_size - 1)) | 0xfff); |
914 | 4b4f782c | bellard | virt_addr = addr & ~(page_size - 1);
|
915 | 14ce26e7 | bellard | } else {
|
916 | 14ce26e7 | bellard | /* 4 KB page */
|
917 | 14ce26e7 | bellard | if (!(pde & PG_ACCESSED_MASK)) {
|
918 | 14ce26e7 | bellard | pde |= PG_ACCESSED_MASK; |
919 | 8df1cd07 | bellard | stl_phys_notdirty(pde_addr, pde); |
920 | 14ce26e7 | bellard | } |
921 | 4b4f782c | bellard | pte_addr = ((pde & PHYS_ADDR_MASK) + (((addr >> 12) & 0x1ff) << 3)) & |
922 | 14ce26e7 | bellard | env->a20_mask; |
923 | 4b4f782c | bellard | pte = ldq_phys(pte_addr); |
924 | 4b4f782c | bellard | if (!(pte & PG_PRESENT_MASK)) {
|
925 | 4b4f782c | bellard | error_code = 0;
|
926 | 4b4f782c | bellard | goto do_fault;
|
927 | 4b4f782c | bellard | } |
928 | 4b4f782c | bellard | if (!(env->efer & MSR_EFER_NXE) && (pte & PG_NX_MASK)) {
|
929 | 4b4f782c | bellard | error_code = PG_ERROR_RSVD_MASK; |
930 | 4b4f782c | bellard | goto do_fault;
|
931 | 4b4f782c | bellard | } |
932 | 4b4f782c | bellard | /* combine pde and pte nx, user and rw protections */
|
933 | 4b4f782c | bellard | ptep &= pte ^ PG_NX_MASK; |
934 | 4b4f782c | bellard | ptep ^= PG_NX_MASK; |
935 | 4b4f782c | bellard | if ((ptep & PG_NX_MASK) && is_write1 == 2) |
936 | 5fafdf24 | ths | goto do_fault_protect;
|
937 | 4b4f782c | bellard | if (is_user) {
|
938 | 4b4f782c | bellard | if (!(ptep & PG_USER_MASK))
|
939 | 4b4f782c | bellard | goto do_fault_protect;
|
940 | 4b4f782c | bellard | if (is_write && !(ptep & PG_RW_MASK))
|
941 | 4b4f782c | bellard | goto do_fault_protect;
|
942 | 4b4f782c | bellard | } else {
|
943 | 4b4f782c | bellard | if ((env->cr[0] & CR0_WP_MASK) && |
944 | 5fafdf24 | ths | is_write && !(ptep & PG_RW_MASK)) |
945 | 4b4f782c | bellard | goto do_fault_protect;
|
946 | 4b4f782c | bellard | } |
947 | 4b4f782c | bellard | is_dirty = is_write && !(pte & PG_DIRTY_MASK); |
948 | 4b4f782c | bellard | if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
|
949 | 4b4f782c | bellard | pte |= PG_ACCESSED_MASK; |
950 | 4b4f782c | bellard | if (is_dirty)
|
951 | 4b4f782c | bellard | pte |= PG_DIRTY_MASK; |
952 | 4b4f782c | bellard | stl_phys_notdirty(pte_addr, pte); |
953 | 4b4f782c | bellard | } |
954 | 4b4f782c | bellard | page_size = 4096;
|
955 | 4b4f782c | bellard | virt_addr = addr & ~0xfff;
|
956 | 4b4f782c | bellard | pte = pte & (PHYS_ADDR_MASK | 0xfff);
|
957 | 2c0262af | bellard | } |
958 | 14ce26e7 | bellard | } else {
|
959 | 4b4f782c | bellard | uint32_t pde; |
960 | 4b4f782c | bellard | |
961 | 2c0262af | bellard | /* page directory entry */
|
962 | 5fafdf24 | ths | pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & |
963 | 14ce26e7 | bellard | env->a20_mask; |
964 | 8df1cd07 | bellard | pde = ldl_phys(pde_addr); |
965 | 14ce26e7 | bellard | if (!(pde & PG_PRESENT_MASK)) {
|
966 | 2c0262af | bellard | error_code = 0;
|
967 | 2c0262af | bellard | goto do_fault;
|
968 | 2c0262af | bellard | } |
969 | 14ce26e7 | bellard | /* if PSE bit is set, then we use a 4MB page */
|
970 | 14ce26e7 | bellard | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { |
971 | 14ce26e7 | bellard | page_size = 4096 * 1024; |
972 | 14ce26e7 | bellard | if (is_user) {
|
973 | 14ce26e7 | bellard | if (!(pde & PG_USER_MASK))
|
974 | 14ce26e7 | bellard | goto do_fault_protect;
|
975 | 14ce26e7 | bellard | if (is_write && !(pde & PG_RW_MASK))
|
976 | 14ce26e7 | bellard | goto do_fault_protect;
|
977 | 14ce26e7 | bellard | } else {
|
978 | 5fafdf24 | ths | if ((env->cr[0] & CR0_WP_MASK) && |
979 | 5fafdf24 | ths | is_write && !(pde & PG_RW_MASK)) |
980 | 14ce26e7 | bellard | goto do_fault_protect;
|
981 | 14ce26e7 | bellard | } |
982 | 14ce26e7 | bellard | is_dirty = is_write && !(pde & PG_DIRTY_MASK); |
983 | 14ce26e7 | bellard | if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
|
984 | 14ce26e7 | bellard | pde |= PG_ACCESSED_MASK; |
985 | 14ce26e7 | bellard | if (is_dirty)
|
986 | 14ce26e7 | bellard | pde |= PG_DIRTY_MASK; |
987 | 8df1cd07 | bellard | stl_phys_notdirty(pde_addr, pde); |
988 | 14ce26e7 | bellard | } |
989 | 3b46e624 | ths | |
990 | 14ce26e7 | bellard | pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */ |
991 | 14ce26e7 | bellard | ptep = pte; |
992 | 14ce26e7 | bellard | virt_addr = addr & ~(page_size - 1);
|
993 | 2c0262af | bellard | } else {
|
994 | 14ce26e7 | bellard | if (!(pde & PG_ACCESSED_MASK)) {
|
995 | 14ce26e7 | bellard | pde |= PG_ACCESSED_MASK; |
996 | 8df1cd07 | bellard | stl_phys_notdirty(pde_addr, pde); |
997 | 14ce26e7 | bellard | } |
998 | 14ce26e7 | bellard | |
999 | 14ce26e7 | bellard | /* page directory entry */
|
1000 | 5fafdf24 | ths | pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & |
1001 | 14ce26e7 | bellard | env->a20_mask; |
1002 | 8df1cd07 | bellard | pte = ldl_phys(pte_addr); |
1003 | 14ce26e7 | bellard | if (!(pte & PG_PRESENT_MASK)) {
|
1004 | 14ce26e7 | bellard | error_code = 0;
|
1005 | 14ce26e7 | bellard | goto do_fault;
|
1006 | 14ce26e7 | bellard | } |
1007 | 14ce26e7 | bellard | /* combine pde and pte user and rw protections */
|
1008 | 14ce26e7 | bellard | ptep = pte & pde; |
1009 | 14ce26e7 | bellard | if (is_user) {
|
1010 | 14ce26e7 | bellard | if (!(ptep & PG_USER_MASK))
|
1011 | 14ce26e7 | bellard | goto do_fault_protect;
|
1012 | 14ce26e7 | bellard | if (is_write && !(ptep & PG_RW_MASK))
|
1013 | 14ce26e7 | bellard | goto do_fault_protect;
|
1014 | 14ce26e7 | bellard | } else {
|
1015 | 14ce26e7 | bellard | if ((env->cr[0] & CR0_WP_MASK) && |
1016 | 5fafdf24 | ths | is_write && !(ptep & PG_RW_MASK)) |
1017 | 14ce26e7 | bellard | goto do_fault_protect;
|
1018 | 14ce26e7 | bellard | } |
1019 | 14ce26e7 | bellard | is_dirty = is_write && !(pte & PG_DIRTY_MASK); |
1020 | 14ce26e7 | bellard | if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
|
1021 | 14ce26e7 | bellard | pte |= PG_ACCESSED_MASK; |
1022 | 14ce26e7 | bellard | if (is_dirty)
|
1023 | 14ce26e7 | bellard | pte |= PG_DIRTY_MASK; |
1024 | 8df1cd07 | bellard | stl_phys_notdirty(pte_addr, pte); |
1025 | 14ce26e7 | bellard | } |
1026 | 14ce26e7 | bellard | page_size = 4096;
|
1027 | 14ce26e7 | bellard | virt_addr = addr & ~0xfff;
|
1028 | 2c0262af | bellard | } |
1029 | 4b4f782c | bellard | } |
1030 | 4b4f782c | bellard | /* the page can be put in the TLB */
|
1031 | 4b4f782c | bellard | prot = PAGE_READ; |
1032 | 4b4f782c | bellard | if (!(ptep & PG_NX_MASK))
|
1033 | 4b4f782c | bellard | prot |= PAGE_EXEC; |
1034 | 4b4f782c | bellard | if (pte & PG_DIRTY_MASK) {
|
1035 | 4b4f782c | bellard | /* only set write access if already dirty... otherwise wait
|
1036 | 4b4f782c | bellard | for dirty access */
|
1037 | 4b4f782c | bellard | if (is_user) {
|
1038 | 4b4f782c | bellard | if (ptep & PG_RW_MASK)
|
1039 | 4b4f782c | bellard | prot |= PAGE_WRITE; |
1040 | 4b4f782c | bellard | } else {
|
1041 | 4b4f782c | bellard | if (!(env->cr[0] & CR0_WP_MASK) || |
1042 | 4b4f782c | bellard | (ptep & PG_RW_MASK)) |
1043 | 4b4f782c | bellard | prot |= PAGE_WRITE; |
1044 | c8135d9a | bellard | } |
1045 | 2c0262af | bellard | } |
1046 | 2c0262af | bellard | do_mapping:
|
1047 | 1ac157da | bellard | pte = pte & env->a20_mask; |
1048 | 2c0262af | bellard | |
1049 | 436d8b89 | bellard | /* Even if 4MB pages, we map only one 4KB page in the cache to
|
1050 | 436d8b89 | bellard | avoid filling it too fast */
|
1051 | 436d8b89 | bellard | page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
|
1052 | 436d8b89 | bellard | paddr = (pte & TARGET_PAGE_MASK) + page_offset; |
1053 | 436d8b89 | bellard | vaddr = virt_addr + page_offset; |
1054 | 3b46e624 | ths | |
1055 | 6ebbf390 | j_mayer | ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu); |
1056 | 2c0262af | bellard | return ret;
|
1057 | 2c0262af | bellard | do_fault_protect:
|
1058 | 2c0262af | bellard | error_code = PG_ERROR_P_MASK; |
1059 | 2c0262af | bellard | do_fault:
|
1060 | 4b4f782c | bellard | error_code |= (is_write << PG_ERROR_W_BIT); |
1061 | 2c0262af | bellard | if (is_user)
|
1062 | 4b4f782c | bellard | error_code |= PG_ERROR_U_MASK; |
1063 | 5fafdf24 | ths | if (is_write1 == 2 && |
1064 | 5fafdf24 | ths | (env->efer & MSR_EFER_NXE) && |
1065 | 4b4f782c | bellard | (env->cr[4] & CR4_PAE_MASK))
|
1066 | 4b4f782c | bellard | error_code |= PG_ERROR_I_D_MASK; |
1067 | 0573fbfc | ths | if (INTERCEPTEDl(_exceptions, 1 << EXCP0E_PAGE)) { |
1068 | 0573fbfc | ths | stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2), addr);
|
1069 | 0573fbfc | ths | } else {
|
1070 | 0573fbfc | ths | env->cr[2] = addr;
|
1071 | 0573fbfc | ths | } |
1072 | 4b4f782c | bellard | env->error_code = error_code; |
1073 | 54ca9095 | bellard | env->exception_index = EXCP0E_PAGE; |
1074 | 0573fbfc | ths | /* the VMM will handle this */
|
1075 | 0573fbfc | ths | if (INTERCEPTEDl(_exceptions, 1 << EXCP0E_PAGE)) |
1076 | 0573fbfc | ths | return 2; |
1077 | 2c0262af | bellard | return 1; |
1078 | 2c0262af | bellard | } |
1079 | 10f0e412 | bellard | |
1080 | 9b3c35e0 | j_mayer | target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr) |
1081 | 10f0e412 | bellard | { |
1082 | f51589da | bellard | uint32_t pde_addr, pte_addr; |
1083 | 10f0e412 | bellard | uint32_t pde, pte, paddr, page_offset, page_size; |
1084 | 10f0e412 | bellard | |
1085 | f51589da | bellard | if (env->cr[4] & CR4_PAE_MASK) { |
1086 | f51589da | bellard | uint32_t pdpe_addr, pde_addr, pte_addr; |
1087 | f51589da | bellard | uint32_t pdpe; |
1088 | f51589da | bellard | |
1089 | f51589da | bellard | /* XXX: we only use 32 bit physical addresses */
|
1090 | f51589da | bellard | #ifdef TARGET_X86_64
|
1091 | f51589da | bellard | if (env->hflags & HF_LMA_MASK) {
|
1092 | f51589da | bellard | uint32_t pml4e_addr, pml4e; |
1093 | f51589da | bellard | int32_t sext; |
1094 | f51589da | bellard | |
1095 | f51589da | bellard | /* test virtual address sign extension */
|
1096 | f51589da | bellard | sext = (int64_t)addr >> 47;
|
1097 | f51589da | bellard | if (sext != 0 && sext != -1) |
1098 | f51589da | bellard | return -1; |
1099 | 3b46e624 | ths | |
1100 | 5fafdf24 | ths | pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) & |
1101 | f51589da | bellard | env->a20_mask; |
1102 | 8df1cd07 | bellard | pml4e = ldl_phys(pml4e_addr); |
1103 | f51589da | bellard | if (!(pml4e & PG_PRESENT_MASK))
|
1104 | f51589da | bellard | return -1; |
1105 | 3b46e624 | ths | |
1106 | 5fafdf24 | ths | pdpe_addr = ((pml4e & ~0xfff) + (((addr >> 30) & 0x1ff) << 3)) & |
1107 | f51589da | bellard | env->a20_mask; |
1108 | 8df1cd07 | bellard | pdpe = ldl_phys(pdpe_addr); |
1109 | f51589da | bellard | if (!(pdpe & PG_PRESENT_MASK))
|
1110 | f51589da | bellard | return -1; |
1111 | 5fafdf24 | ths | } else
|
1112 | f51589da | bellard | #endif
|
1113 | f51589da | bellard | { |
1114 | 5fafdf24 | ths | pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) & |
1115 | f51589da | bellard | env->a20_mask; |
1116 | 8df1cd07 | bellard | pdpe = ldl_phys(pdpe_addr); |
1117 | f51589da | bellard | if (!(pdpe & PG_PRESENT_MASK))
|
1118 | f51589da | bellard | return -1; |
1119 | f51589da | bellard | } |
1120 | f51589da | bellard | |
1121 | f51589da | bellard | pde_addr = ((pdpe & ~0xfff) + (((addr >> 21) & 0x1ff) << 3)) & |
1122 | f51589da | bellard | env->a20_mask; |
1123 | 8df1cd07 | bellard | pde = ldl_phys(pde_addr); |
1124 | f51589da | bellard | if (!(pde & PG_PRESENT_MASK)) {
|
1125 | 10f0e412 | bellard | return -1; |
1126 | f51589da | bellard | } |
1127 | f51589da | bellard | if (pde & PG_PSE_MASK) {
|
1128 | f51589da | bellard | /* 2 MB page */
|
1129 | f51589da | bellard | page_size = 2048 * 1024; |
1130 | f51589da | bellard | pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */ |
1131 | f51589da | bellard | } else {
|
1132 | f51589da | bellard | /* 4 KB page */
|
1133 | f51589da | bellard | pte_addr = ((pde & ~0xfff) + (((addr >> 12) & 0x1ff) << 3)) & |
1134 | f51589da | bellard | env->a20_mask; |
1135 | f51589da | bellard | page_size = 4096;
|
1136 | 8df1cd07 | bellard | pte = ldl_phys(pte_addr); |
1137 | f51589da | bellard | } |
1138 | f51589da | bellard | } else {
|
1139 | f51589da | bellard | if (!(env->cr[0] & CR0_PG_MASK)) { |
1140 | f51589da | bellard | pte = addr; |
1141 | f51589da | bellard | page_size = 4096;
|
1142 | 10f0e412 | bellard | } else {
|
1143 | 10f0e412 | bellard | /* page directory entry */
|
1144 | af661ad1 | ths | pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & env->a20_mask; |
1145 | 8df1cd07 | bellard | pde = ldl_phys(pde_addr); |
1146 | 5fafdf24 | ths | if (!(pde & PG_PRESENT_MASK))
|
1147 | 10f0e412 | bellard | return -1; |
1148 | f51589da | bellard | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { |
1149 | f51589da | bellard | pte = pde & ~0x003ff000; /* align to 4MB */ |
1150 | f51589da | bellard | page_size = 4096 * 1024; |
1151 | f51589da | bellard | } else {
|
1152 | f51589da | bellard | /* page directory entry */
|
1153 | f51589da | bellard | pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & env->a20_mask; |
1154 | 8df1cd07 | bellard | pte = ldl_phys(pte_addr); |
1155 | f51589da | bellard | if (!(pte & PG_PRESENT_MASK))
|
1156 | f51589da | bellard | return -1; |
1157 | f51589da | bellard | page_size = 4096;
|
1158 | f51589da | bellard | } |
1159 | 10f0e412 | bellard | } |
1160 | f51589da | bellard | pte = pte & env->a20_mask; |
1161 | 10f0e412 | bellard | } |
1162 | f51589da | bellard | |
1163 | 10f0e412 | bellard | page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
|
1164 | 10f0e412 | bellard | paddr = (pte & TARGET_PAGE_MASK) + page_offset; |
1165 | 10f0e412 | bellard | return paddr;
|
1166 | 10f0e412 | bellard | } |
1167 | 8df1cd07 | bellard | #endif /* !CONFIG_USER_ONLY */ |