]> git.proxmox.com Git - qemu.git/blame - target-i386/helper.c
Remove cpu_get_phys_page_debug from userspace emulation
[qemu.git] / target-i386 / helper.c
CommitLineData
2c0262af 1/*
eaa728ee 2 * i386 helpers (without register variable usage)
5fafdf24 3 *
2c0262af
FB
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
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
2c0262af 18 */
eaa728ee
FB
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24#include <signal.h>
2c0262af 25
eaa728ee
FB
26#include "cpu.h"
27#include "exec-all.h"
eaa728ee 28#include "qemu-common.h"
7ba1e619 29#include "kvm.h"
f3f2d9be 30
eaa728ee 31//#define DEBUG_MMU
b5ec5ce0 32#include "qemu-option.h"
33#include "qemu-config.h"
2c0262af 34
c6fa82c4 35/* feature flags taken from "Intel Processor Identification and the CPUID
b5ec5ce0 36 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
37 * between feature naming conventions, aliases may be added.
38 */
c6fa82c4 39static const char *feature_name[] = {
b5ec5ce0 40 "fpu", "vme", "de", "pse",
41 "tsc", "msr", "pae", "mce",
42 "cx8", "apic", NULL, "sep",
43 "mtrr", "pge", "mca", "cmov",
44 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
45 NULL, "ds" /* Intel dts */, "acpi", "mmx",
46 "fxsr", "sse", "sse2", "ss",
47 "ht" /* Intel htt */, "tm", "ia64", "pbe",
c6fa82c4
AK
48};
49static const char *ext_feature_name[] = {
b5ec5ce0 50 "pni|sse3" /* Intel,AMD sse3 */, NULL, NULL, "monitor",
51 "ds_cpl", "vmx", NULL /* Linux smx */, "est",
52 "tm2", "ssse3", "cid", NULL,
53 NULL, "cx16", "xtpr", NULL,
54 NULL, NULL, "dca", "sse4.1|sse4_1",
55 "sse4.2|sse4_2", "x2apic", NULL, "popcnt",
56 NULL, NULL, NULL, NULL,
57 NULL, NULL, NULL, "hypervisor",
c6fa82c4
AK
58};
59static const char *ext2_feature_name[] = {
b5ec5ce0 60 "fpu", "vme", "de", "pse",
61 "tsc", "msr", "pae", "mce",
62 "cx8" /* AMD CMPXCHG8B */, "apic", NULL, "syscall",
63 "mtrr", "pge", "mca", "cmov",
64 "pat", "pse36", NULL, NULL /* Linux mp */,
65 "nx" /* Intel xd */, NULL, "mmxext", "mmx",
66 "fxsr", "fxsr_opt" /* AMD ffxsr */, "pdpe1gb" /* AMD Page1GB */, "rdtscp",
67 NULL, "lm" /* Intel 64 */, "3dnowext", "3dnow",
c6fa82c4
AK
68};
69static const char *ext3_feature_name[] = {
b5ec5ce0 70 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
71 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
72 "3dnowprefetch", "osvw", NULL /* Linux ibs */, NULL,
73 "skinit", "wdt", NULL, NULL,
74 NULL, NULL, NULL, NULL,
75 NULL, NULL, NULL, NULL,
76 NULL, NULL, NULL, NULL,
77 NULL, NULL, NULL, NULL,
c6fa82c4
AK
78};
79
bb0300dc
GN
80static const char *kvm_feature_name[] = {
81 "kvmclock", "kvm_nopiodelay", "kvm_mmu", NULL, NULL, NULL, NULL, NULL,
82 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
83 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
84 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
85};
86
b5ec5ce0 87/* collects per-function cpuid data
88 */
89typedef struct model_features_t {
90 uint32_t *guest_feat;
91 uint32_t *host_feat;
92 uint32_t check_feat;
93 const char **flag_names;
94 uint32_t cpuid;
95 } model_features_t;
96
97int check_cpuid = 0;
98int enforce_cpuid = 0;
99
100static void host_cpuid(uint32_t function, uint32_t count, uint32_t *eax,
101 uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
102
103#define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
104
105/* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
106 * a substring. ex if !NULL points to the first char after a substring,
107 * otherwise the string is assumed to sized by a terminating nul.
108 * Return lexical ordering of *s1:*s2.
109 */
110static int sstrcmp(const char *s1, const char *e1, const char *s2,
111 const char *e2)
112{
113 for (;;) {
114 if (!*s1 || !*s2 || *s1 != *s2)
115 return (*s1 - *s2);
116 ++s1, ++s2;
117 if (s1 == e1 && s2 == e2)
118 return (0);
119 else if (s1 == e1)
120 return (*s2);
121 else if (s2 == e2)
122 return (*s1);
123 }
124}
125
126/* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
127 * '|' delimited (possibly empty) strings in which case search for a match
128 * within the alternatives proceeds left to right. Return 0 for success,
129 * non-zero otherwise.
130 */
131static int altcmp(const char *s, const char *e, const char *altstr)
132{
133 const char *p, *q;
134
135 for (q = p = altstr; ; ) {
136 while (*p && *p != '|')
137 ++p;
138 if ((q == p && !*s) || (q != p && !sstrcmp(s, e, q, p)))
139 return (0);
140 if (!*p)
141 return (1);
142 else
143 q = ++p;
144 }
145}
146
147/* search featureset for flag *[s..e), if found set corresponding bit in
148 * *pval and return success, otherwise return zero
149 */
150static int lookup_feature(uint32_t *pval, const char *s, const char *e,
151 const char **featureset)
152{
153 uint32_t mask;
154 const char **ppc;
155
156 for (mask = 1, ppc = featureset; mask; mask <<= 1, ++ppc)
157 if (*ppc && !altcmp(s, e, *ppc)) {
158 *pval |= mask;
159 break;
160 }
161 return (mask ? 1 : 0);
162}
163
6d2edc43
AP
164static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features,
165 uint32_t *ext_features,
166 uint32_t *ext2_features,
bb0300dc
GN
167 uint32_t *ext3_features,
168 uint32_t *kvm_features)
eaa728ee 169{
b5ec5ce0 170 if (!lookup_feature(features, flagname, NULL, feature_name) &&
171 !lookup_feature(ext_features, flagname, NULL, ext_feature_name) &&
172 !lookup_feature(ext2_features, flagname, NULL, ext2_feature_name) &&
173 !lookup_feature(ext3_features, flagname, NULL, ext3_feature_name) &&
174 !lookup_feature(kvm_features, flagname, NULL, kvm_feature_name))
175 fprintf(stderr, "CPU feature %s not found\n", flagname);
eaa728ee 176}
2c0262af 177
c227f099 178typedef struct x86_def_t {
b5ec5ce0 179 struct x86_def_t *next;
eaa728ee
FB
180 const char *name;
181 uint32_t level;
182 uint32_t vendor1, vendor2, vendor3;
183 int family;
184 int model;
185 int stepping;
bb0300dc 186 uint32_t features, ext_features, ext2_features, ext3_features, kvm_features;
eaa728ee 187 uint32_t xlevel;
40f8e2fa 188 char model_id[48];
ef768138 189 int vendor_override;
b5ec5ce0 190 uint32_t flags;
c227f099 191} x86_def_t;
eaa728ee
FB
192
193#define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
194#define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
c84bd4f1 195 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
eaa728ee
FB
196#define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
197 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
198 CPUID_PSE36 | CPUID_FXSR)
199#define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
200#define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
201 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
202 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
203 CPUID_PAE | CPUID_SEP | CPUID_APIC)
b5ec5ce0 204
205/* maintains list of cpu model definitions
206 */
207static x86_def_t *x86_defs = {NULL};
208
209/* built-in cpu model definitions (deprecated)
210 */
211static x86_def_t builtin_x86_defs[] = {
eaa728ee
FB
212#ifdef TARGET_X86_64
213 {
214 .name = "qemu64",
ac72472b 215 .level = 4,
c5096daf
AZ
216 .vendor1 = CPUID_VENDOR_AMD_1,
217 .vendor2 = CPUID_VENDOR_AMD_2,
218 .vendor3 = CPUID_VENDOR_AMD_3,
eaa728ee
FB
219 .family = 6,
220 .model = 2,
221 .stepping = 3,
222 .features = PPRO_FEATURES |
223 /* these features are needed for Win64 and aren't fully implemented */
224 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
225 /* this feature is needed for Solaris and isn't fully implemented */
226 CPUID_PSE36,
f1e00a9c 227 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT,
eaa728ee 228 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
09ac35ac 229 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
f1e00a9c
AP
230 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
231 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
eaa728ee 232 .xlevel = 0x8000000A,
40f8e2fa 233 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
eaa728ee 234 },
9bdba1b6
AL
235 {
236 .name = "phenom",
237 .level = 5,
238 .vendor1 = CPUID_VENDOR_AMD_1,
239 .vendor2 = CPUID_VENDOR_AMD_2,
240 .vendor3 = CPUID_VENDOR_AMD_3,
241 .family = 16,
242 .model = 2,
243 .stepping = 3,
244 /* Missing: CPUID_VME, CPUID_HT */
245 .features = PPRO_FEATURES |
246 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
247 CPUID_PSE36,
f1e00a9c
AP
248 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_CX16 |
249 CPUID_EXT_POPCNT,
9bdba1b6
AL
250 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
251 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
252 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
253 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
254 CPUID_EXT2_FFXSR,
f1e00a9c
AP
255 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
256 CPUID_EXT3_CR8LEG,
9bdba1b6
AL
257 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
258 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
f1e00a9c
AP
259 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
260 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
9bdba1b6
AL
261 .xlevel = 0x8000001A,
262 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
263 },
e737b32a
AZ
264 {
265 .name = "core2duo",
558fa836 266 .level = 10,
e737b32a
AZ
267 .family = 6,
268 .model = 15,
269 .stepping = 11,
558fa836
PB
270 /* The original CPU also implements these features:
271 CPUID_VME, CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
272 CPUID_TM, CPUID_PBE */
0086de1c 273 .features = PPRO_FEATURES |
e737b32a
AZ
274 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
275 CPUID_PSE36,
558fa836
PB
276 /* The original CPU also implements these ext features:
277 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
278 CPUID_EXT_TM2, CPUID_EXT_CX16, CPUID_EXT_XTPR, CPUID_EXT_PDCM */
0086de1c 279 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3,
558fa836 280 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
f1e00a9c 281 .ext3_features = CPUID_EXT3_LAHF_LM,
45fd08ef 282 .xlevel = 0x80000008,
e737b32a
AZ
283 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
284 },
d1cd4bf4
AP
285 {
286 .name = "kvm64",
287 .level = 5,
288 .vendor1 = CPUID_VENDOR_INTEL_1,
289 .vendor2 = CPUID_VENDOR_INTEL_2,
290 .vendor3 = CPUID_VENDOR_INTEL_3,
291 .family = 15,
292 .model = 6,
293 .stepping = 1,
294 /* Missing: CPUID_VME, CPUID_HT */
295 .features = PPRO_FEATURES |
296 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
297 CPUID_PSE36,
298 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
299 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16,
300 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
301 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) |
302 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
303 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
304 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
305 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
306 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
307 .ext3_features = 0,
308 .xlevel = 0x80000008,
309 .model_id = "Common KVM processor"
310 },
eaa728ee
FB
311#endif
312 {
313 .name = "qemu32",
ac72472b 314 .level = 4,
eaa728ee
FB
315 .family = 6,
316 .model = 3,
317 .stepping = 3,
318 .features = PPRO_FEATURES,
f1e00a9c 319 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_POPCNT,
eaa728ee 320 .xlevel = 0,
40f8e2fa 321 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
eaa728ee 322 },
45fd08ef
AJ
323 {
324 .name = "coreduo",
325 .level = 10,
326 .family = 6,
327 .model = 14,
328 .stepping = 8,
329 /* The original CPU also implements these features:
330 CPUID_DTS, CPUID_ACPI, CPUID_SS, CPUID_HT,
331 CPUID_TM, CPUID_PBE */
332 .features = PPRO_FEATURES | CPUID_VME |
333 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA,
334 /* The original CPU also implements these ext features:
335 CPUID_EXT_VMX, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_XTPR,
336 CPUID_EXT_PDCM */
337 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR,
338 .ext2_features = CPUID_EXT2_NX,
339 .xlevel = 0x80000008,
340 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
341 },
eaa728ee
FB
342 {
343 .name = "486",
344 .level = 0,
345 .family = 4,
346 .model = 0,
347 .stepping = 0,
348 .features = I486_FEATURES,
349 .xlevel = 0,
350 },
351 {
352 .name = "pentium",
353 .level = 1,
354 .family = 5,
355 .model = 4,
356 .stepping = 3,
357 .features = PENTIUM_FEATURES,
358 .xlevel = 0,
359 },
360 {
361 .name = "pentium2",
362 .level = 2,
363 .family = 6,
364 .model = 5,
365 .stepping = 2,
366 .features = PENTIUM2_FEATURES,
367 .xlevel = 0,
368 },
369 {
370 .name = "pentium3",
371 .level = 2,
372 .family = 6,
373 .model = 7,
374 .stepping = 3,
375 .features = PENTIUM3_FEATURES,
376 .xlevel = 0,
377 },
378 {
379 .name = "athlon",
380 .level = 2,
fe4bce09
AP
381 .vendor1 = CPUID_VENDOR_AMD_1,
382 .vendor2 = CPUID_VENDOR_AMD_2,
383 .vendor3 = CPUID_VENDOR_AMD_3,
eaa728ee
FB
384 .family = 6,
385 .model = 2,
386 .stepping = 3,
558fa836 387 .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR | CPUID_MCA,
eaa728ee
FB
388 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) | CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
389 .xlevel = 0x80000008,
40f8e2fa
FB
390 /* XXX: put another string ? */
391 .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
eaa728ee 392 },
0086de1c 393 {
c0d82995 394 .name = "n270",
0086de1c
AZ
395 /* original is on level 10 */
396 .level = 5,
397 .family = 6,
398 .model = 28,
399 .stepping = 2,
400 .features = PPRO_FEATURES |
401 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME,
402 /* Missing: CPUID_DTS | CPUID_ACPI | CPUID_SS |
403 * CPUID_HT | CPUID_TM | CPUID_PBE */
404 /* Some CPUs got no CPUID_SEP */
405 .ext_features = CPUID_EXT_MONITOR |
853f6931 406 CPUID_EXT_SSE3 /* PNI */ | CPUID_EXT_SSSE3,
0086de1c
AZ
407 /* Missing: CPUID_EXT_DSCPL | CPUID_EXT_EST |
408 * CPUID_EXT_TM2 | CPUID_EXT_XTPR */
409 .ext2_features = (PPRO_FEATURES & 0x0183F3FF) | CPUID_EXT2_NX,
410 /* Missing: .ext3_features = CPUID_EXT3_LAHF_LM */
411 .xlevel = 0x8000000A,
412 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
413 },
eaa728ee 414};
2c0262af 415
fe4bce09
AP
416static int cpu_x86_fill_model_id(char *str)
417{
e6f9e6b4 418 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
fe4bce09
AP
419 int i;
420
421 for (i = 0; i < 3; i++) {
422 host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx);
423 memcpy(str + i * 16 + 0, &eax, 4);
424 memcpy(str + i * 16 + 4, &ebx, 4);
425 memcpy(str + i * 16 + 8, &ecx, 4);
426 memcpy(str + i * 16 + 12, &edx, 4);
427 }
428 return 0;
429}
430
c227f099 431static int cpu_x86_fill_host(x86_def_t *x86_cpu_def)
fe4bce09 432{
97b35e35 433 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
fe4bce09
AP
434
435 x86_cpu_def->name = "host";
436 host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
437 x86_cpu_def->level = eax;
438 x86_cpu_def->vendor1 = ebx;
439 x86_cpu_def->vendor2 = edx;
440 x86_cpu_def->vendor3 = ecx;
441
442 host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
443 x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
444 x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
445 x86_cpu_def->stepping = eax & 0x0F;
446 x86_cpu_def->ext_features = ecx;
447 x86_cpu_def->features = edx;
448
449 host_cpuid(0x80000000, 0, &eax, &ebx, &ecx, &edx);
450 x86_cpu_def->xlevel = eax;
451
452 host_cpuid(0x80000001, 0, &eax, &ebx, &ecx, &edx);
453 x86_cpu_def->ext2_features = edx;
454 x86_cpu_def->ext3_features = ecx;
455 cpu_x86_fill_model_id(x86_cpu_def->model_id);
456 x86_cpu_def->vendor_override = 0;
457
458 return 0;
459}
460
b5ec5ce0 461static int unavailable_host_feature(struct model_features_t *f, uint32_t mask)
462{
463 int i;
464
465 for (i = 0; i < 32; ++i)
466 if (1 << i & mask) {
467 fprintf(stderr, "warning: host cpuid %04x_%04x lacks requested"
468 " flag '%s' [0x%08x]\n",
469 f->cpuid >> 16, f->cpuid & 0xffff,
470 f->flag_names[i] ? f->flag_names[i] : "[reserved]", mask);
471 break;
472 }
473 return 0;
474}
475
476/* best effort attempt to inform user requested cpu flags aren't making
477 * their way to the guest. Note: ft[].check_feat ideally should be
478 * specified via a guest_def field to suppress report of extraneous flags.
479 */
480static int check_features_against_host(x86_def_t *guest_def)
481{
482 x86_def_t host_def;
483 uint32_t mask;
484 int rv, i;
485 struct model_features_t ft[] = {
486 {&guest_def->features, &host_def.features,
487 ~0, feature_name, 0x00000000},
488 {&guest_def->ext_features, &host_def.ext_features,
489 ~CPUID_EXT_HYPERVISOR, ext_feature_name, 0x00000001},
490 {&guest_def->ext2_features, &host_def.ext2_features,
491 ~PPRO_FEATURES, ext2_feature_name, 0x80000000},
492 {&guest_def->ext3_features, &host_def.ext3_features,
493 ~CPUID_EXT3_SVM, ext3_feature_name, 0x80000001}};
494
495 cpu_x86_fill_host(&host_def);
496 for (rv = 0, i = 0; i < sizeof (ft) / sizeof (ft[0]); ++i)
497 for (mask = 1; mask; mask <<= 1)
498 if (ft[i].check_feat & mask && *ft[i].guest_feat & mask &&
499 !(*ft[i].host_feat & mask)) {
500 unavailable_host_feature(&ft[i], mask);
501 rv = 1;
502 }
503 return rv;
504}
505
c227f099 506static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
2c0262af 507{
eaa728ee 508 unsigned int i;
c227f099 509 x86_def_t *def;
2c0262af 510
eaa728ee
FB
511 char *s = strdup(cpu_model);
512 char *featurestr, *name = strtok(s, ",");
bb0300dc
GN
513 uint32_t plus_features = 0, plus_ext_features = 0, plus_ext2_features = 0, plus_ext3_features = 0, plus_kvm_features = 0;
514 uint32_t minus_features = 0, minus_ext_features = 0, minus_ext2_features = 0, minus_ext3_features = 0, minus_kvm_features = 0;
a8a358bf 515 uint32_t numvalue;
2c0262af 516
b5ec5ce0 517 for (def = x86_defs; def; def = def->next)
518 if (!strcmp(name, def->name))
eaa728ee 519 break;
5e650002 520 if (kvm_enabled() && strcmp(name, "host") == 0) {
fe4bce09 521 cpu_x86_fill_host(x86_cpu_def);
5e650002
AL
522 } else if (!def) {
523 goto error;
fe4bce09
AP
524 } else {
525 memcpy(x86_cpu_def, def, sizeof(*def));
526 }
eaa728ee 527
bb0300dc
GN
528 plus_kvm_features = ~0; /* not supported bits will be filtered out later */
529
6d2edc43 530 add_flagname_to_bitmaps("hypervisor", &plus_features,
bb0300dc
GN
531 &plus_ext_features, &plus_ext2_features, &plus_ext3_features,
532 &plus_kvm_features);
6d2edc43 533
eaa728ee
FB
534 featurestr = strtok(NULL, ",");
535
536 while (featurestr) {
537 char *val;
538 if (featurestr[0] == '+') {
bb0300dc 539 add_flagname_to_bitmaps(featurestr + 1, &plus_features, &plus_ext_features, &plus_ext2_features, &plus_ext3_features, &plus_kvm_features);
eaa728ee 540 } else if (featurestr[0] == '-') {
bb0300dc 541 add_flagname_to_bitmaps(featurestr + 1, &minus_features, &minus_ext_features, &minus_ext2_features, &minus_ext3_features, &minus_kvm_features);
eaa728ee
FB
542 } else if ((val = strchr(featurestr, '='))) {
543 *val = 0; val++;
544 if (!strcmp(featurestr, "family")) {
545 char *err;
a8a358bf
AP
546 numvalue = strtoul(val, &err, 0);
547 if (!*val || *err) {
eaa728ee 548 fprintf(stderr, "bad numerical value %s\n", val);
eaa728ee
FB
549 goto error;
550 }
a8a358bf 551 x86_cpu_def->family = numvalue;
eaa728ee
FB
552 } else if (!strcmp(featurestr, "model")) {
553 char *err;
a8a358bf
AP
554 numvalue = strtoul(val, &err, 0);
555 if (!*val || *err || numvalue > 0xff) {
eaa728ee 556 fprintf(stderr, "bad numerical value %s\n", val);
eaa728ee
FB
557 goto error;
558 }
a8a358bf 559 x86_cpu_def->model = numvalue;
eaa728ee
FB
560 } else if (!strcmp(featurestr, "stepping")) {
561 char *err;
a8a358bf
AP
562 numvalue = strtoul(val, &err, 0);
563 if (!*val || *err || numvalue > 0xf) {
eaa728ee 564 fprintf(stderr, "bad numerical value %s\n", val);
eaa728ee
FB
565 goto error;
566 }
a8a358bf
AP
567 x86_cpu_def->stepping = numvalue ;
568 } else if (!strcmp(featurestr, "level")) {
569 char *err;
570 numvalue = strtoul(val, &err, 0);
571 if (!*val || *err) {
572 fprintf(stderr, "bad numerical value %s\n", val);
573 goto error;
574 }
575 x86_cpu_def->level = numvalue;
576 } else if (!strcmp(featurestr, "xlevel")) {
577 char *err;
578 numvalue = strtoul(val, &err, 0);
579 if (!*val || *err) {
580 fprintf(stderr, "bad numerical value %s\n", val);
581 goto error;
582 }
583 if (numvalue < 0x80000000) {
584 numvalue += 0x80000000;
585 }
586 x86_cpu_def->xlevel = numvalue;
40f8e2fa
FB
587 } else if (!strcmp(featurestr, "vendor")) {
588 if (strlen(val) != 12) {
589 fprintf(stderr, "vendor string must be 12 chars long\n");
590 goto error;
591 }
592 x86_cpu_def->vendor1 = 0;
593 x86_cpu_def->vendor2 = 0;
594 x86_cpu_def->vendor3 = 0;
595 for(i = 0; i < 4; i++) {
596 x86_cpu_def->vendor1 |= ((uint8_t)val[i ]) << (8 * i);
597 x86_cpu_def->vendor2 |= ((uint8_t)val[i + 4]) << (8 * i);
598 x86_cpu_def->vendor3 |= ((uint8_t)val[i + 8]) << (8 * i);
599 }
ef768138 600 x86_cpu_def->vendor_override = 1;
40f8e2fa
FB
601 } else if (!strcmp(featurestr, "model_id")) {
602 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
603 val);
eaa728ee
FB
604 } else {
605 fprintf(stderr, "unrecognized feature %s\n", featurestr);
eaa728ee
FB
606 goto error;
607 }
b5ec5ce0 608 } else if (!strcmp(featurestr, "check")) {
609 check_cpuid = 1;
610 } else if (!strcmp(featurestr, "enforce")) {
611 check_cpuid = enforce_cpuid = 1;
eaa728ee
FB
612 } else {
613 fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
eaa728ee
FB
614 goto error;
615 }
616 featurestr = strtok(NULL, ",");
617 }
618 x86_cpu_def->features |= plus_features;
619 x86_cpu_def->ext_features |= plus_ext_features;
620 x86_cpu_def->ext2_features |= plus_ext2_features;
621 x86_cpu_def->ext3_features |= plus_ext3_features;
bb0300dc 622 x86_cpu_def->kvm_features |= plus_kvm_features;
eaa728ee
FB
623 x86_cpu_def->features &= ~minus_features;
624 x86_cpu_def->ext_features &= ~minus_ext_features;
625 x86_cpu_def->ext2_features &= ~minus_ext2_features;
626 x86_cpu_def->ext3_features &= ~minus_ext3_features;
bb0300dc 627 x86_cpu_def->kvm_features &= ~minus_kvm_features;
b5ec5ce0 628 if (check_cpuid) {
629 if (check_features_against_host(x86_cpu_def) && enforce_cpuid)
630 goto error;
631 }
eaa728ee
FB
632 free(s);
633 return 0;
634
635error:
636 free(s);
637 return -1;
bd7a7b33
FB
638}
639
b5ec5ce0 640/* generate a composite string into buf of all cpuid names in featureset
641 * selected by fbits. indicate truncation at bufsize in the event of overflow.
642 * if flags, suppress names undefined in featureset.
643 */
644static void listflags(char *buf, int bufsize, uint32_t fbits,
645 const char **featureset, uint32_t flags)
bd7a7b33 646{
b5ec5ce0 647 const char **p = &featureset[31];
648 char *q, *b, bit;
649 int nc;
650
651 b = 4 <= bufsize ? buf + (bufsize -= 3) - 1 : NULL;
652 *buf = '\0';
653 for (q = buf, bit = 31; fbits && bufsize; --p, fbits &= ~(1 << bit), --bit)
654 if (fbits & 1 << bit && (*p || !flags)) {
655 if (*p)
656 nc = snprintf(q, bufsize, "%s%s", q == buf ? "" : " ", *p);
657 else
658 nc = snprintf(q, bufsize, "%s[%d]", q == buf ? "" : " ", bit);
659 if (bufsize <= nc) {
d354899c
BS
660 if (b) {
661 memcpy(b, "...", sizeof("..."));
662 }
b5ec5ce0 663 return;
664 }
665 q += nc;
666 bufsize -= nc;
667 }
668}
eaa728ee 669
b5ec5ce0 670/* generate CPU information:
671 * -? list model names
672 * -?model list model names/IDs
673 * -?dump output all model (x86_def_t) data
674 * -?cpuid list all recognized cpuid flag names
675 */
676void x86_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
677 const char *optarg)
678{
679 unsigned char model = !strcmp("?model", optarg);
680 unsigned char dump = !strcmp("?dump", optarg);
681 unsigned char cpuid = !strcmp("?cpuid", optarg);
682 x86_def_t *def;
683 char buf[256];
684
685 if (cpuid) {
686 (*cpu_fprintf)(f, "Recognized CPUID flags:\n");
687 listflags(buf, sizeof (buf), (uint32_t)~0, feature_name, 1);
688 (*cpu_fprintf)(f, " f_edx: %s\n", buf);
689 listflags(buf, sizeof (buf), (uint32_t)~0, ext_feature_name, 1);
690 (*cpu_fprintf)(f, " f_ecx: %s\n", buf);
691 listflags(buf, sizeof (buf), (uint32_t)~0, ext2_feature_name, 1);
692 (*cpu_fprintf)(f, " extf_edx: %s\n", buf);
693 listflags(buf, sizeof (buf), (uint32_t)~0, ext3_feature_name, 1);
694 (*cpu_fprintf)(f, " extf_ecx: %s\n", buf);
695 return;
696 }
697 for (def = x86_defs; def; def = def->next) {
698 snprintf(buf, sizeof (buf), def->flags ? "[%s]": "%s", def->name);
699 if (model || dump) {
700 (*cpu_fprintf)(f, "x86 %16s %-48s\n", buf, def->model_id);
701 } else {
702 (*cpu_fprintf)(f, "x86 %16s\n", buf);
703 }
704 if (dump) {
705 memcpy(buf, &def->vendor1, sizeof (def->vendor1));
706 memcpy(buf + 4, &def->vendor2, sizeof (def->vendor2));
707 memcpy(buf + 8, &def->vendor3, sizeof (def->vendor3));
708 buf[12] = '\0';
709 (*cpu_fprintf)(f,
710 " family %d model %d stepping %d level %d xlevel 0x%x"
711 " vendor \"%s\"\n",
712 def->family, def->model, def->stepping, def->level,
713 def->xlevel, buf);
714 listflags(buf, sizeof (buf), def->features, feature_name, 0);
715 (*cpu_fprintf)(f, " feature_edx %08x (%s)\n", def->features,
716 buf);
717 listflags(buf, sizeof (buf), def->ext_features, ext_feature_name,
718 0);
719 (*cpu_fprintf)(f, " feature_ecx %08x (%s)\n", def->ext_features,
720 buf);
721 listflags(buf, sizeof (buf), def->ext2_features, ext2_feature_name,
722 0);
723 (*cpu_fprintf)(f, " extfeature_edx %08x (%s)\n",
724 def->ext2_features, buf);
725 listflags(buf, sizeof (buf), def->ext3_features, ext3_feature_name,
726 0);
727 (*cpu_fprintf)(f, " extfeature_ecx %08x (%s)\n",
728 def->ext3_features, buf);
729 (*cpu_fprintf)(f, "\n");
730 }
731 }
bd7a7b33
FB
732}
733
eaa728ee 734static int cpu_x86_register (CPUX86State *env, const char *cpu_model)
7e84c249 735{
c227f099 736 x86_def_t def1, *def = &def1;
7e84c249 737
eaa728ee 738 if (cpu_x86_find_by_name(def, cpu_model) < 0)
7e84c249 739 return -1;
eaa728ee
FB
740 if (def->vendor1) {
741 env->cpuid_vendor1 = def->vendor1;
742 env->cpuid_vendor2 = def->vendor2;
743 env->cpuid_vendor3 = def->vendor3;
744 } else {
c5096daf
AZ
745 env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1;
746 env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2;
747 env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3;
eaa728ee 748 }
ef768138 749 env->cpuid_vendor_override = def->vendor_override;
eaa728ee 750 env->cpuid_level = def->level;
59795a1f
AZ
751 if (def->family > 0x0f)
752 env->cpuid_version = 0xf00 | ((def->family - 0x0f) << 20);
753 else
754 env->cpuid_version = def->family << 8;
755 env->cpuid_version |= ((def->model & 0xf) << 4) | ((def->model >> 4) << 16);
756 env->cpuid_version |= def->stepping;
eaa728ee
FB
757 env->cpuid_features = def->features;
758 env->pat = 0x0007040600070406ULL;
759 env->cpuid_ext_features = def->ext_features;
760 env->cpuid_ext2_features = def->ext2_features;
761 env->cpuid_xlevel = def->xlevel;
bb0300dc 762 env->cpuid_kvm_features = def->kvm_features;
eaa728ee 763 {
40f8e2fa 764 const char *model_id = def->model_id;
eaa728ee 765 int c, len, i;
40f8e2fa
FB
766 if (!model_id)
767 model_id = "";
eaa728ee
FB
768 len = strlen(model_id);
769 for(i = 0; i < 48; i++) {
770 if (i >= len)
771 c = '\0';
772 else
40f8e2fa 773 c = (uint8_t)model_id[i];
eaa728ee
FB
774 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
775 }
776 }
7e84c249
FB
777 return 0;
778}
3b46e624 779
161717d2 780#if !defined(CONFIG_USER_ONLY)
b5ec5ce0 781/* copy vendor id string to 32 bit register, nul pad as needed
782 */
783static void cpyid(const char *s, uint32_t *id)
784{
785 char *d = (char *)id;
786 char i;
787
788 for (i = sizeof (*id); i--; )
789 *d++ = *s ? *s++ : '\0';
790}
791
792/* interpret radix and convert from string to arbitrary scalar,
793 * otherwise flag failure
794 */
795#define setscalar(pval, str, perr) \
796{ \
797 char *pend; \
798 unsigned long ul; \
799 \
800 ul = strtoul(str, &pend, 0); \
801 *str && !*pend ? (*pval = ul) : (*perr = 1); \
802}
803
804/* map cpuid options to feature bits, otherwise return failure
805 * (option tags in *str are delimited by whitespace)
806 */
807static void setfeatures(uint32_t *pval, const char *str,
808 const char **featureset, int *perr)
809{
810 const char *p, *q;
811
812 for (q = p = str; *p || *q; q = p) {
813 while (iswhite(*p))
814 q = ++p;
815 while (*p && !iswhite(*p))
816 ++p;
817 if (!*q && !*p)
818 return;
819 if (!lookup_feature(pval, q, p, featureset)) {
820 fprintf(stderr, "error: feature \"%.*s\" not available in set\n",
821 (int)(p - q), q);
822 *perr = 1;
823 return;
824 }
825 }
826}
827
828/* map config file options to x86_def_t form
829 */
830static int cpudef_setfield(const char *name, const char *str, void *opaque)
831{
832 x86_def_t *def = opaque;
833 int err = 0;
834
835 if (!strcmp(name, "name")) {
836 def->name = strdup(str);
837 } else if (!strcmp(name, "model_id")) {
838 strncpy(def->model_id, str, sizeof (def->model_id));
839 } else if (!strcmp(name, "level")) {
840 setscalar(&def->level, str, &err)
841 } else if (!strcmp(name, "vendor")) {
842 cpyid(&str[0], &def->vendor1);
843 cpyid(&str[4], &def->vendor2);
844 cpyid(&str[8], &def->vendor3);
845 } else if (!strcmp(name, "family")) {
846 setscalar(&def->family, str, &err)
847 } else if (!strcmp(name, "model")) {
848 setscalar(&def->model, str, &err)
849 } else if (!strcmp(name, "stepping")) {
850 setscalar(&def->stepping, str, &err)
851 } else if (!strcmp(name, "feature_edx")) {
852 setfeatures(&def->features, str, feature_name, &err);
853 } else if (!strcmp(name, "feature_ecx")) {
854 setfeatures(&def->ext_features, str, ext_feature_name, &err);
855 } else if (!strcmp(name, "extfeature_edx")) {
856 setfeatures(&def->ext2_features, str, ext2_feature_name, &err);
857 } else if (!strcmp(name, "extfeature_ecx")) {
858 setfeatures(&def->ext3_features, str, ext3_feature_name, &err);
859 } else if (!strcmp(name, "xlevel")) {
860 setscalar(&def->xlevel, str, &err)
861 } else {
862 fprintf(stderr, "error: unknown option [%s = %s]\n", name, str);
863 return (1);
864 }
865 if (err) {
866 fprintf(stderr, "error: bad option value [%s = %s]\n", name, str);
867 return (1);
868 }
869 return (0);
870}
871
872/* register config file entry as x86_def_t
873 */
874static int cpudef_register(QemuOpts *opts, void *opaque)
875{
876 x86_def_t *def = qemu_mallocz(sizeof (x86_def_t));
877
878 qemu_opt_foreach(opts, cpudef_setfield, def, 1);
879 def->next = x86_defs;
880 x86_defs = def;
881 return (0);
882}
161717d2 883#endif /* !CONFIG_USER_ONLY */
b5ec5ce0 884
885/* register "cpudef" models defined in configuration file. Here we first
886 * preload any built-in definitions
887 */
888void x86_cpudef_setup(void)
889{
890 int i;
891
892 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); ++i) {
893 builtin_x86_defs[i].next = x86_defs;
894 builtin_x86_defs[i].flags = 1;
895 x86_defs = &builtin_x86_defs[i];
896 }
161717d2 897#if !defined(CONFIG_USER_ONLY)
b5ec5ce0 898 qemu_opts_foreach(&qemu_cpudef_opts, cpudef_register, NULL, 0);
899#endif
900}
901
eaa728ee
FB
902/* NOTE: must be called outside the CPU execute loop */
903void cpu_reset(CPUX86State *env)
7e84c249 904{
eaa728ee 905 int i;
7e84c249 906
eca1bdf4
AL
907 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
908 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
909 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
910 }
911
eaa728ee 912 memset(env, 0, offsetof(CPUX86State, breakpoints));
7e84c249 913
eaa728ee 914 tlb_flush(env, 1);
7e84c249 915
eaa728ee 916 env->old_exception = -1;
7e84c249 917
eaa728ee 918 /* init to reset state */
3b46e624 919
eaa728ee
FB
920#ifdef CONFIG_SOFTMMU
921 env->hflags |= HF_SOFTMMU_MASK;
2c0262af 922#endif
db620f46 923 env->hflags2 |= HF2_GIF_MASK;
2c0262af 924
eaa728ee
FB
925 cpu_x86_update_cr0(env, 0x60000010);
926 env->a20_mask = ~0x0;
927 env->smbase = 0x30000;
7e84c249 928
eaa728ee
FB
929 env->idt.limit = 0xffff;
930 env->gdt.limit = 0xffff;
931 env->ldt.limit = 0xffff;
262ffdae 932 env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
eaa728ee 933 env->tr.limit = 0xffff;
23e6c399 934 env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
262ffdae
FB
935
936 cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
538f3686
NK
937 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK |
938 DESC_R_MASK | DESC_A_MASK);
262ffdae 939 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
538f3686
NK
940 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
941 DESC_A_MASK);
262ffdae 942 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
538f3686
NK
943 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
944 DESC_A_MASK);
262ffdae 945 cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
538f3686
NK
946 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
947 DESC_A_MASK);
262ffdae 948 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
538f3686
NK
949 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
950 DESC_A_MASK);
262ffdae 951 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
538f3686
NK
952 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
953 DESC_A_MASK);
7e84c249 954
eaa728ee
FB
955 env->eip = 0xfff0;
956 env->regs[R_EDX] = env->cpuid_version;
2c0262af 957
eaa728ee 958 env->eflags = 0x2;
7e84c249 959
eaa728ee
FB
960 /* FPU init */
961 for(i = 0;i < 8; i++)
962 env->fptags[i] = 1;
963 env->fpuc = 0x37f;
7e84c249 964
eaa728ee 965 env->mxcsr = 0x1f80;
01df040b
AL
966
967 memset(env->dr, 0, sizeof(env->dr));
968 env->dr[6] = DR6_FIXED_1;
969 env->dr[7] = DR7_FIXED_1;
970 cpu_breakpoint_remove_all(env, BP_CPU);
971 cpu_watchpoint_remove_all(env, BP_CPU);
af364b41
HY
972
973 env->mcg_status = 0;
eaa728ee 974}
7e84c249 975
eaa728ee
FB
976void cpu_x86_close(CPUX86State *env)
977{
bb332cb2 978 qemu_free(env);
eaa728ee 979}
7e84c249 980
eaa728ee
FB
981/***********************************************************/
982/* x86 debug */
3b46e624 983
eaa728ee
FB
984static const char *cc_op_str[] = {
985 "DYNAMIC",
986 "EFLAGS",
7e84c249 987
eaa728ee
FB
988 "MULB",
989 "MULW",
990 "MULL",
991 "MULQ",
3b46e624 992
eaa728ee
FB
993 "ADDB",
994 "ADDW",
995 "ADDL",
996 "ADDQ",
3b46e624 997
eaa728ee
FB
998 "ADCB",
999 "ADCW",
1000 "ADCL",
1001 "ADCQ",
3b46e624 1002
eaa728ee
FB
1003 "SUBB",
1004 "SUBW",
1005 "SUBL",
1006 "SUBQ",
7e84c249 1007
eaa728ee
FB
1008 "SBBB",
1009 "SBBW",
1010 "SBBL",
1011 "SBBQ",
7e84c249 1012
eaa728ee
FB
1013 "LOGICB",
1014 "LOGICW",
1015 "LOGICL",
1016 "LOGICQ",
7e84c249 1017
eaa728ee
FB
1018 "INCB",
1019 "INCW",
1020 "INCL",
1021 "INCQ",
3b46e624 1022
eaa728ee
FB
1023 "DECB",
1024 "DECW",
1025 "DECL",
1026 "DECQ",
3b46e624 1027
eaa728ee
FB
1028 "SHLB",
1029 "SHLW",
1030 "SHLL",
1031 "SHLQ",
3b46e624 1032
eaa728ee
FB
1033 "SARB",
1034 "SARW",
1035 "SARL",
1036 "SARQ",
1037};
7e84c249 1038
a3867ed2
AL
1039static void
1040cpu_x86_dump_seg_cache(CPUState *env, FILE *f,
1041 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1042 const char *name, struct SegmentCache *sc)
1043{
1044#ifdef TARGET_X86_64
1045 if (env->hflags & HF_CS64_MASK) {
1046 cpu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name,
1047 sc->selector, sc->base, sc->limit, sc->flags);
1048 } else
1049#endif
1050 {
1051 cpu_fprintf(f, "%-3s=%04x %08x %08x %08x", name, sc->selector,
1052 (uint32_t)sc->base, sc->limit, sc->flags);
1053 }
1054
1055 if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK))
1056 goto done;
1057
1058 cpu_fprintf(f, " DPL=%d ", (sc->flags & DESC_DPL_MASK) >> DESC_DPL_SHIFT);
1059 if (sc->flags & DESC_S_MASK) {
1060 if (sc->flags & DESC_CS_MASK) {
1061 cpu_fprintf(f, (sc->flags & DESC_L_MASK) ? "CS64" :
1062 ((sc->flags & DESC_B_MASK) ? "CS32" : "CS16"));
1063 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_C_MASK) ? 'C' : '-',
1064 (sc->flags & DESC_R_MASK) ? 'R' : '-');
1065 } else {
1066 cpu_fprintf(f, (sc->flags & DESC_B_MASK) ? "DS " : "DS16");
1067 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_E_MASK) ? 'E' : '-',
1068 (sc->flags & DESC_W_MASK) ? 'W' : '-');
1069 }
1070 cpu_fprintf(f, "%c]", (sc->flags & DESC_A_MASK) ? 'A' : '-');
1071 } else {
1072 static const char *sys_type_name[2][16] = {
1073 { /* 32 bit mode */
1074 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
1075 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
1076 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
1077 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
1078 },
1079 { /* 64 bit mode */
1080 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
1081 "Reserved", "Reserved", "Reserved", "Reserved",
1082 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
1083 "Reserved", "IntGate64", "TrapGate64"
1084 }
1085 };
1086 cpu_fprintf(f, sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0]
1087 [(sc->flags & DESC_TYPE_MASK)
1088 >> DESC_TYPE_SHIFT]);
1089 }
1090done:
1091 cpu_fprintf(f, "\n");
1092}
1093
eaa728ee
FB
1094void cpu_dump_state(CPUState *env, FILE *f,
1095 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1096 int flags)
1097{
1098 int eflags, i, nb;
1099 char cc_op_name[32];
1100 static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
7e84c249 1101
23054111 1102 cpu_synchronize_state(env);
ff3c01ca 1103
eaa728ee
FB
1104 eflags = env->eflags;
1105#ifdef TARGET_X86_64
1106 if (env->hflags & HF_CS64_MASK) {
1107 cpu_fprintf(f,
1108 "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n"
1109 "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n"
1110 "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n"
1111 "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n"
1112 "RIP=%016" PRIx64 " RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
1113 env->regs[R_EAX],
1114 env->regs[R_EBX],
1115 env->regs[R_ECX],
1116 env->regs[R_EDX],
1117 env->regs[R_ESI],
1118 env->regs[R_EDI],
1119 env->regs[R_EBP],
1120 env->regs[R_ESP],
1121 env->regs[8],
1122 env->regs[9],
1123 env->regs[10],
1124 env->regs[11],
1125 env->regs[12],
1126 env->regs[13],
1127 env->regs[14],
1128 env->regs[15],
1129 env->eip, eflags,
1130 eflags & DF_MASK ? 'D' : '-',
1131 eflags & CC_O ? 'O' : '-',
1132 eflags & CC_S ? 'S' : '-',
1133 eflags & CC_Z ? 'Z' : '-',
1134 eflags & CC_A ? 'A' : '-',
1135 eflags & CC_P ? 'P' : '-',
1136 eflags & CC_C ? 'C' : '-',
1137 env->hflags & HF_CPL_MASK,
1138 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
5ee0ffaa 1139 (env->a20_mask >> 20) & 1,
eaa728ee 1140 (env->hflags >> HF_SMM_SHIFT) & 1,
ce5232c5 1141 env->halted);
eaa728ee
FB
1142 } else
1143#endif
1144 {
1145 cpu_fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
1146 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
1147 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
1148 (uint32_t)env->regs[R_EAX],
1149 (uint32_t)env->regs[R_EBX],
1150 (uint32_t)env->regs[R_ECX],
1151 (uint32_t)env->regs[R_EDX],
1152 (uint32_t)env->regs[R_ESI],
1153 (uint32_t)env->regs[R_EDI],
1154 (uint32_t)env->regs[R_EBP],
1155 (uint32_t)env->regs[R_ESP],
1156 (uint32_t)env->eip, eflags,
1157 eflags & DF_MASK ? 'D' : '-',
1158 eflags & CC_O ? 'O' : '-',
1159 eflags & CC_S ? 'S' : '-',
1160 eflags & CC_Z ? 'Z' : '-',
1161 eflags & CC_A ? 'A' : '-',
1162 eflags & CC_P ? 'P' : '-',
1163 eflags & CC_C ? 'C' : '-',
1164 env->hflags & HF_CPL_MASK,
1165 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
5ee0ffaa 1166 (env->a20_mask >> 20) & 1,
eaa728ee 1167 (env->hflags >> HF_SMM_SHIFT) & 1,
ce5232c5 1168 env->halted);
8145122b 1169 }
3b46e624 1170
a3867ed2
AL
1171 for(i = 0; i < 6; i++) {
1172 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, seg_name[i],
1173 &env->segs[i]);
1174 }
1175 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "LDT", &env->ldt);
1176 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "TR", &env->tr);
1177
eaa728ee
FB
1178#ifdef TARGET_X86_64
1179 if (env->hflags & HF_LMA_MASK) {
eaa728ee
FB
1180 cpu_fprintf(f, "GDT= %016" PRIx64 " %08x\n",
1181 env->gdt.base, env->gdt.limit);
1182 cpu_fprintf(f, "IDT= %016" PRIx64 " %08x\n",
1183 env->idt.base, env->idt.limit);
1184 cpu_fprintf(f, "CR0=%08x CR2=%016" PRIx64 " CR3=%016" PRIx64 " CR4=%08x\n",
1185 (uint32_t)env->cr[0],
1186 env->cr[2],
1187 env->cr[3],
1188 (uint32_t)env->cr[4]);
a59cb4e0
AL
1189 for(i = 0; i < 4; i++)
1190 cpu_fprintf(f, "DR%d=%016" PRIx64 " ", i, env->dr[i]);
1191 cpu_fprintf(f, "\nDR6=%016" PRIx64 " DR7=%016" PRIx64 "\n",
d4b55be5 1192 env->dr[6], env->dr[7]);
eaa728ee
FB
1193 } else
1194#endif
1195 {
eaa728ee
FB
1196 cpu_fprintf(f, "GDT= %08x %08x\n",
1197 (uint32_t)env->gdt.base, env->gdt.limit);
1198 cpu_fprintf(f, "IDT= %08x %08x\n",
1199 (uint32_t)env->idt.base, env->idt.limit);
1200 cpu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
1201 (uint32_t)env->cr[0],
1202 (uint32_t)env->cr[2],
1203 (uint32_t)env->cr[3],
1204 (uint32_t)env->cr[4]);
a59cb4e0
AL
1205 for(i = 0; i < 4; i++)
1206 cpu_fprintf(f, "DR%d=%08x ", i, env->dr[i]);
d4b55be5 1207 cpu_fprintf(f, "\nDR6=%08x DR7=%08x\n", env->dr[6], env->dr[7]);
eaa728ee
FB
1208 }
1209 if (flags & X86_DUMP_CCOP) {
1210 if ((unsigned)env->cc_op < CC_OP_NB)
1211 snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]);
1212 else
1213 snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
1214#ifdef TARGET_X86_64
1215 if (env->hflags & HF_CS64_MASK) {
1216 cpu_fprintf(f, "CCS=%016" PRIx64 " CCD=%016" PRIx64 " CCO=%-8s\n",
1217 env->cc_src, env->cc_dst,
1218 cc_op_name);
1219 } else
1220#endif
1221 {
1222 cpu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
1223 (uint32_t)env->cc_src, (uint32_t)env->cc_dst,
1224 cc_op_name);
1225 }
7e84c249 1226 }
eaa728ee
FB
1227 if (flags & X86_DUMP_FPU) {
1228 int fptag;
1229 fptag = 0;
1230 for(i = 0; i < 8; i++) {
1231 fptag |= ((!env->fptags[i]) << i);
1232 }
1233 cpu_fprintf(f, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
1234 env->fpuc,
1235 (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11,
1236 env->fpstt,
1237 fptag,
1238 env->mxcsr);
1239 for(i=0;i<8;i++) {
1240#if defined(USE_X86LDOUBLE)
1241 union {
1242 long double d;
1243 struct {
1244 uint64_t lower;
1245 uint16_t upper;
1246 } l;
1247 } tmp;
1248 tmp.d = env->fpregs[i].d;
1249 cpu_fprintf(f, "FPR%d=%016" PRIx64 " %04x",
1250 i, tmp.l.lower, tmp.l.upper);
1251#else
1252 cpu_fprintf(f, "FPR%d=%016" PRIx64,
1253 i, env->fpregs[i].mmx.q);
1254#endif
1255 if ((i & 1) == 1)
1256 cpu_fprintf(f, "\n");
1257 else
1258 cpu_fprintf(f, " ");
1259 }
1260 if (env->hflags & HF_CS64_MASK)
1261 nb = 16;
1262 else
1263 nb = 8;
1264 for(i=0;i<nb;i++) {
1265 cpu_fprintf(f, "XMM%02d=%08x%08x%08x%08x",
1266 i,
1267 env->xmm_regs[i].XMM_L(3),
1268 env->xmm_regs[i].XMM_L(2),
1269 env->xmm_regs[i].XMM_L(1),
1270 env->xmm_regs[i].XMM_L(0));
1271 if ((i & 1) == 1)
1272 cpu_fprintf(f, "\n");
1273 else
1274 cpu_fprintf(f, " ");
1275 }
7e84c249 1276 }
2c0262af 1277}
7e84c249 1278
eaa728ee
FB
1279/***********************************************************/
1280/* x86 mmu */
1281/* XXX: add PGE support */
1282
1283void cpu_x86_set_a20(CPUX86State *env, int a20_state)
2c0262af 1284{
eaa728ee
FB
1285 a20_state = (a20_state != 0);
1286 if (a20_state != ((env->a20_mask >> 20) & 1)) {
1287#if defined(DEBUG_MMU)
1288 printf("A20 update: a20=%d\n", a20_state);
1289#endif
1290 /* if the cpu is currently executing code, we must unlink it and
1291 all the potentially executing TB */
1292 cpu_interrupt(env, CPU_INTERRUPT_EXITTB);
3b46e624 1293
eaa728ee
FB
1294 /* when a20 is changed, all the MMU mappings are invalid, so
1295 we must flush everything */
1296 tlb_flush(env, 1);
5ee0ffaa 1297 env->a20_mask = ~(1 << 20) | (a20_state << 20);
7e84c249 1298 }
2c0262af
FB
1299}
1300
eaa728ee 1301void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
2c0262af 1302{
eaa728ee 1303 int pe_state;
2c0262af 1304
eaa728ee
FB
1305#if defined(DEBUG_MMU)
1306 printf("CR0 update: CR0=0x%08x\n", new_cr0);
1307#endif
1308 if ((new_cr0 & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK)) !=
1309 (env->cr[0] & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK))) {
1310 tlb_flush(env, 1);
1311 }
2c0262af 1312
eaa728ee
FB
1313#ifdef TARGET_X86_64
1314 if (!(env->cr[0] & CR0_PG_MASK) && (new_cr0 & CR0_PG_MASK) &&
1315 (env->efer & MSR_EFER_LME)) {
1316 /* enter in long mode */
1317 /* XXX: generate an exception */
1318 if (!(env->cr[4] & CR4_PAE_MASK))
1319 return;
1320 env->efer |= MSR_EFER_LMA;
1321 env->hflags |= HF_LMA_MASK;
1322 } else if ((env->cr[0] & CR0_PG_MASK) && !(new_cr0 & CR0_PG_MASK) &&
1323 (env->efer & MSR_EFER_LMA)) {
1324 /* exit long mode */
1325 env->efer &= ~MSR_EFER_LMA;
1326 env->hflags &= ~(HF_LMA_MASK | HF_CS64_MASK);
1327 env->eip &= 0xffffffff;
1328 }
1329#endif
1330 env->cr[0] = new_cr0 | CR0_ET_MASK;
7e84c249 1331
eaa728ee
FB
1332 /* update PE flag in hidden flags */
1333 pe_state = (env->cr[0] & CR0_PE_MASK);
1334 env->hflags = (env->hflags & ~HF_PE_MASK) | (pe_state << HF_PE_SHIFT);
1335 /* ensure that ADDSEG is always set in real mode */
1336 env->hflags |= ((pe_state ^ 1) << HF_ADDSEG_SHIFT);
1337 /* update FPU flags */
1338 env->hflags = (env->hflags & ~(HF_MP_MASK | HF_EM_MASK | HF_TS_MASK)) |
1339 ((new_cr0 << (HF_MP_SHIFT - 1)) & (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK));
7e84c249
FB
1340}
1341
eaa728ee
FB
1342/* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
1343 the PDPT */
1344void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
7e84c249 1345{
eaa728ee
FB
1346 env->cr[3] = new_cr3;
1347 if (env->cr[0] & CR0_PG_MASK) {
1348#if defined(DEBUG_MMU)
1349 printf("CR3 update: CR3=" TARGET_FMT_lx "\n", new_cr3);
1350#endif
1351 tlb_flush(env, 0);
1352 }
7e84c249
FB
1353}
1354
eaa728ee 1355void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
7e84c249 1356{
eaa728ee
FB
1357#if defined(DEBUG_MMU)
1358 printf("CR4 update: CR4=%08x\n", (uint32_t)env->cr[4]);
1359#endif
1360 if ((new_cr4 & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK)) !=
1361 (env->cr[4] & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK))) {
1362 tlb_flush(env, 1);
1363 }
1364 /* SSE handling */
1365 if (!(env->cpuid_features & CPUID_SSE))
1366 new_cr4 &= ~CR4_OSFXSR_MASK;
1367 if (new_cr4 & CR4_OSFXSR_MASK)
1368 env->hflags |= HF_OSFXSR_MASK;
1369 else
1370 env->hflags &= ~HF_OSFXSR_MASK;
b8b6a50b 1371
eaa728ee 1372 env->cr[4] = new_cr4;
b8b6a50b
FB
1373}
1374
eaa728ee
FB
1375#if defined(CONFIG_USER_ONLY)
1376
1377int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
1378 int is_write, int mmu_idx, int is_softmmu)
b8b6a50b 1379{
eaa728ee
FB
1380 /* user mode only emulation */
1381 is_write &= 1;
1382 env->cr[2] = addr;
1383 env->error_code = (is_write << PG_ERROR_W_BIT);
1384 env->error_code |= PG_ERROR_U_MASK;
1385 env->exception_index = EXCP0E_PAGE;
1386 return 1;
2c0262af
FB
1387}
1388
8d7b0fbb 1389#else
891b38e4 1390
eaa728ee
FB
1391/* XXX: This value should match the one returned by CPUID
1392 * and in exec.c */
eaa728ee 1393# if defined(TARGET_X86_64)
2c90d794 1394# define PHYS_ADDR_MASK 0xfffffff000LL
eaa728ee 1395# else
2c90d794 1396# define PHYS_ADDR_MASK 0xffffff000LL
eaa728ee 1397# endif
eaa728ee
FB
1398
1399/* return value:
1400 -1 = cannot handle fault
1401 0 = nothing more to do
1402 1 = generate PF fault
1403 2 = soft MMU activation required for this block
1404*/
1405int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
1406 int is_write1, int mmu_idx, int is_softmmu)
1407{
1408 uint64_t ptep, pte;
1409 target_ulong pde_addr, pte_addr;
1410 int error_code, is_dirty, prot, page_size, ret, is_write, is_user;
c227f099 1411 target_phys_addr_t paddr;
eaa728ee
FB
1412 uint32_t page_offset;
1413 target_ulong vaddr, virt_addr;
1414
1415 is_user = mmu_idx == MMU_USER_IDX;
1416#if defined(DEBUG_MMU)
1417 printf("MMU fault: addr=" TARGET_FMT_lx " w=%d u=%d eip=" TARGET_FMT_lx "\n",
1418 addr, is_write1, is_user, env->eip);
1419#endif
1420 is_write = is_write1 & 1;
1421
1422 if (!(env->cr[0] & CR0_PG_MASK)) {
1423 pte = addr;
1424 virt_addr = addr & TARGET_PAGE_MASK;
1425 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1426 page_size = 4096;
1427 goto do_mapping;
1428 }
1429
1430 if (env->cr[4] & CR4_PAE_MASK) {
1431 uint64_t pde, pdpe;
1432 target_ulong pdpe_addr;
2c0262af 1433
eaa728ee
FB
1434#ifdef TARGET_X86_64
1435 if (env->hflags & HF_LMA_MASK) {
1436 uint64_t pml4e_addr, pml4e;
1437 int32_t sext;
1438
1439 /* test virtual address sign extension */
1440 sext = (int64_t)addr >> 47;
1441 if (sext != 0 && sext != -1) {
1442 env->error_code = 0;
1443 env->exception_index = EXCP0D_GPF;
1444 return 1;
1445 }
0573fbfc 1446
eaa728ee
FB
1447 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
1448 env->a20_mask;
1449 pml4e = ldq_phys(pml4e_addr);
1450 if (!(pml4e & PG_PRESENT_MASK)) {
1451 error_code = 0;
1452 goto do_fault;
1453 }
1454 if (!(env->efer & MSR_EFER_NXE) && (pml4e & PG_NX_MASK)) {
1455 error_code = PG_ERROR_RSVD_MASK;
1456 goto do_fault;
1457 }
1458 if (!(pml4e & PG_ACCESSED_MASK)) {
1459 pml4e |= PG_ACCESSED_MASK;
1460 stl_phys_notdirty(pml4e_addr, pml4e);
1461 }
1462 ptep = pml4e ^ PG_NX_MASK;
1463 pdpe_addr = ((pml4e & PHYS_ADDR_MASK) + (((addr >> 30) & 0x1ff) << 3)) &
1464 env->a20_mask;
1465 pdpe = ldq_phys(pdpe_addr);
1466 if (!(pdpe & PG_PRESENT_MASK)) {
1467 error_code = 0;
1468 goto do_fault;
1469 }
1470 if (!(env->efer & MSR_EFER_NXE) && (pdpe & PG_NX_MASK)) {
1471 error_code = PG_ERROR_RSVD_MASK;
1472 goto do_fault;
1473 }
1474 ptep &= pdpe ^ PG_NX_MASK;
1475 if (!(pdpe & PG_ACCESSED_MASK)) {
1476 pdpe |= PG_ACCESSED_MASK;
1477 stl_phys_notdirty(pdpe_addr, pdpe);
1478 }
1479 } else
1480#endif
1481 {
1482 /* XXX: load them when cr3 is loaded ? */
1483 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1484 env->a20_mask;
1485 pdpe = ldq_phys(pdpe_addr);
1486 if (!(pdpe & PG_PRESENT_MASK)) {
1487 error_code = 0;
1488 goto do_fault;
1489 }
1490 ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK;
7e84c249 1491 }
7e84c249 1492
eaa728ee
FB
1493 pde_addr = ((pdpe & PHYS_ADDR_MASK) + (((addr >> 21) & 0x1ff) << 3)) &
1494 env->a20_mask;
1495 pde = ldq_phys(pde_addr);
1496 if (!(pde & PG_PRESENT_MASK)) {
1497 error_code = 0;
1498 goto do_fault;
1499 }
1500 if (!(env->efer & MSR_EFER_NXE) && (pde & PG_NX_MASK)) {
1501 error_code = PG_ERROR_RSVD_MASK;
1502 goto do_fault;
1503 }
1504 ptep &= pde ^ PG_NX_MASK;
1505 if (pde & PG_PSE_MASK) {
1506 /* 2 MB page */
1507 page_size = 2048 * 1024;
1508 ptep ^= PG_NX_MASK;
1509 if ((ptep & PG_NX_MASK) && is_write1 == 2)
1510 goto do_fault_protect;
1511 if (is_user) {
1512 if (!(ptep & PG_USER_MASK))
1513 goto do_fault_protect;
1514 if (is_write && !(ptep & PG_RW_MASK))
1515 goto do_fault_protect;
1516 } else {
1517 if ((env->cr[0] & CR0_WP_MASK) &&
1518 is_write && !(ptep & PG_RW_MASK))
1519 goto do_fault_protect;
1520 }
1521 is_dirty = is_write && !(pde & PG_DIRTY_MASK);
1522 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1523 pde |= PG_ACCESSED_MASK;
1524 if (is_dirty)
1525 pde |= PG_DIRTY_MASK;
1526 stl_phys_notdirty(pde_addr, pde);
1527 }
1528 /* align to page_size */
1529 pte = pde & ((PHYS_ADDR_MASK & ~(page_size - 1)) | 0xfff);
1530 virt_addr = addr & ~(page_size - 1);
1531 } else {
1532 /* 4 KB page */
1533 if (!(pde & PG_ACCESSED_MASK)) {
1534 pde |= PG_ACCESSED_MASK;
1535 stl_phys_notdirty(pde_addr, pde);
1536 }
1537 pte_addr = ((pde & PHYS_ADDR_MASK) + (((addr >> 12) & 0x1ff) << 3)) &
1538 env->a20_mask;
1539 pte = ldq_phys(pte_addr);
1540 if (!(pte & PG_PRESENT_MASK)) {
1541 error_code = 0;
1542 goto do_fault;
1543 }
1544 if (!(env->efer & MSR_EFER_NXE) && (pte & PG_NX_MASK)) {
1545 error_code = PG_ERROR_RSVD_MASK;
1546 goto do_fault;
1547 }
1548 /* combine pde and pte nx, user and rw protections */
1549 ptep &= pte ^ PG_NX_MASK;
1550 ptep ^= PG_NX_MASK;
1551 if ((ptep & PG_NX_MASK) && is_write1 == 2)
1552 goto do_fault_protect;
1553 if (is_user) {
1554 if (!(ptep & PG_USER_MASK))
1555 goto do_fault_protect;
1556 if (is_write && !(ptep & PG_RW_MASK))
1557 goto do_fault_protect;
1558 } else {
1559 if ((env->cr[0] & CR0_WP_MASK) &&
1560 is_write && !(ptep & PG_RW_MASK))
1561 goto do_fault_protect;
1562 }
1563 is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1564 if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1565 pte |= PG_ACCESSED_MASK;
1566 if (is_dirty)
1567 pte |= PG_DIRTY_MASK;
1568 stl_phys_notdirty(pte_addr, pte);
1569 }
1570 page_size = 4096;
1571 virt_addr = addr & ~0xfff;
1572 pte = pte & (PHYS_ADDR_MASK | 0xfff);
7e84c249 1573 }
2c0262af 1574 } else {
eaa728ee
FB
1575 uint32_t pde;
1576
1577 /* page directory entry */
1578 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) &
1579 env->a20_mask;
1580 pde = ldl_phys(pde_addr);
1581 if (!(pde & PG_PRESENT_MASK)) {
1582 error_code = 0;
1583 goto do_fault;
1584 }
1585 /* if PSE bit is set, then we use a 4MB page */
1586 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1587 page_size = 4096 * 1024;
1588 if (is_user) {
1589 if (!(pde & PG_USER_MASK))
1590 goto do_fault_protect;
1591 if (is_write && !(pde & PG_RW_MASK))
1592 goto do_fault_protect;
1593 } else {
1594 if ((env->cr[0] & CR0_WP_MASK) &&
1595 is_write && !(pde & PG_RW_MASK))
1596 goto do_fault_protect;
1597 }
1598 is_dirty = is_write && !(pde & PG_DIRTY_MASK);
1599 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
1600 pde |= PG_ACCESSED_MASK;
1601 if (is_dirty)
1602 pde |= PG_DIRTY_MASK;
1603 stl_phys_notdirty(pde_addr, pde);
1604 }
2c0262af 1605
eaa728ee
FB
1606 pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1607 ptep = pte;
1608 virt_addr = addr & ~(page_size - 1);
1609 } else {
1610 if (!(pde & PG_ACCESSED_MASK)) {
1611 pde |= PG_ACCESSED_MASK;
1612 stl_phys_notdirty(pde_addr, pde);
1613 }
891b38e4 1614
eaa728ee
FB
1615 /* page directory entry */
1616 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) &
1617 env->a20_mask;
1618 pte = ldl_phys(pte_addr);
1619 if (!(pte & PG_PRESENT_MASK)) {
1620 error_code = 0;
1621 goto do_fault;
8e682019 1622 }
eaa728ee
FB
1623 /* combine pde and pte user and rw protections */
1624 ptep = pte & pde;
1625 if (is_user) {
1626 if (!(ptep & PG_USER_MASK))
1627 goto do_fault_protect;
1628 if (is_write && !(ptep & PG_RW_MASK))
1629 goto do_fault_protect;
1630 } else {
1631 if ((env->cr[0] & CR0_WP_MASK) &&
1632 is_write && !(ptep & PG_RW_MASK))
1633 goto do_fault_protect;
8e682019 1634 }
eaa728ee
FB
1635 is_dirty = is_write && !(pte & PG_DIRTY_MASK);
1636 if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
1637 pte |= PG_ACCESSED_MASK;
1638 if (is_dirty)
1639 pte |= PG_DIRTY_MASK;
1640 stl_phys_notdirty(pte_addr, pte);
1641 }
1642 page_size = 4096;
1643 virt_addr = addr & ~0xfff;
2c0262af
FB
1644 }
1645 }
eaa728ee
FB
1646 /* the page can be put in the TLB */
1647 prot = PAGE_READ;
1648 if (!(ptep & PG_NX_MASK))
1649 prot |= PAGE_EXEC;
1650 if (pte & PG_DIRTY_MASK) {
1651 /* only set write access if already dirty... otherwise wait
1652 for dirty access */
1653 if (is_user) {
1654 if (ptep & PG_RW_MASK)
1655 prot |= PAGE_WRITE;
1656 } else {
1657 if (!(env->cr[0] & CR0_WP_MASK) ||
1658 (ptep & PG_RW_MASK))
1659 prot |= PAGE_WRITE;
8e682019 1660 }
891b38e4 1661 }
eaa728ee
FB
1662 do_mapping:
1663 pte = pte & env->a20_mask;
1664
1665 /* Even if 4MB pages, we map only one 4KB page in the cache to
1666 avoid filling it too fast */
1667 page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1668 paddr = (pte & TARGET_PAGE_MASK) + page_offset;
1669 vaddr = virt_addr + page_offset;
1670
1671 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
1672 return ret;
1673 do_fault_protect:
1674 error_code = PG_ERROR_P_MASK;
1675 do_fault:
1676 error_code |= (is_write << PG_ERROR_W_BIT);
1677 if (is_user)
1678 error_code |= PG_ERROR_U_MASK;
1679 if (is_write1 == 2 &&
1680 (env->efer & MSR_EFER_NXE) &&
1681 (env->cr[4] & CR4_PAE_MASK))
1682 error_code |= PG_ERROR_I_D_MASK;
872929aa
FB
1683 if (env->intercept_exceptions & (1 << EXCP0E_PAGE)) {
1684 /* cr2 is not modified in case of exceptions */
1685 stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2),
1686 addr);
eaa728ee
FB
1687 } else {
1688 env->cr[2] = addr;
2c0262af 1689 }
eaa728ee
FB
1690 env->error_code = error_code;
1691 env->exception_index = EXCP0E_PAGE;
eaa728ee 1692 return 1;
14ce26e7
FB
1693}
1694
c227f099 1695target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
14ce26e7 1696{
eaa728ee
FB
1697 target_ulong pde_addr, pte_addr;
1698 uint64_t pte;
c227f099 1699 target_phys_addr_t paddr;
eaa728ee
FB
1700 uint32_t page_offset;
1701 int page_size;
14ce26e7 1702
eaa728ee
FB
1703 if (env->cr[4] & CR4_PAE_MASK) {
1704 target_ulong pdpe_addr;
1705 uint64_t pde, pdpe;
14ce26e7 1706
eaa728ee
FB
1707#ifdef TARGET_X86_64
1708 if (env->hflags & HF_LMA_MASK) {
1709 uint64_t pml4e_addr, pml4e;
1710 int32_t sext;
1711
1712 /* test virtual address sign extension */
1713 sext = (int64_t)addr >> 47;
1714 if (sext != 0 && sext != -1)
1715 return -1;
1716
1717 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
1718 env->a20_mask;
1719 pml4e = ldq_phys(pml4e_addr);
1720 if (!(pml4e & PG_PRESENT_MASK))
1721 return -1;
1722
1723 pdpe_addr = ((pml4e & ~0xfff) + (((addr >> 30) & 0x1ff) << 3)) &
1724 env->a20_mask;
1725 pdpe = ldq_phys(pdpe_addr);
1726 if (!(pdpe & PG_PRESENT_MASK))
1727 return -1;
1728 } else
1729#endif
1730 {
1731 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1732 env->a20_mask;
1733 pdpe = ldq_phys(pdpe_addr);
1734 if (!(pdpe & PG_PRESENT_MASK))
1735 return -1;
14ce26e7 1736 }
14ce26e7 1737
eaa728ee
FB
1738 pde_addr = ((pdpe & ~0xfff) + (((addr >> 21) & 0x1ff) << 3)) &
1739 env->a20_mask;
1740 pde = ldq_phys(pde_addr);
1741 if (!(pde & PG_PRESENT_MASK)) {
1742 return -1;
1743 }
1744 if (pde & PG_PSE_MASK) {
1745 /* 2 MB page */
1746 page_size = 2048 * 1024;
1747 pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
1748 } else {
1749 /* 4 KB page */
1750 pte_addr = ((pde & ~0xfff) + (((addr >> 12) & 0x1ff) << 3)) &
1751 env->a20_mask;
1752 page_size = 4096;
1753 pte = ldq_phys(pte_addr);
1754 }
ca1c9e15
AL
1755 if (!(pte & PG_PRESENT_MASK))
1756 return -1;
14ce26e7 1757 } else {
eaa728ee 1758 uint32_t pde;
3b46e624 1759
eaa728ee
FB
1760 if (!(env->cr[0] & CR0_PG_MASK)) {
1761 pte = addr;
1762 page_size = 4096;
1763 } else {
1764 /* page directory entry */
1765 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & env->a20_mask;
1766 pde = ldl_phys(pde_addr);
1767 if (!(pde & PG_PRESENT_MASK))
1768 return -1;
1769 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1770 pte = pde & ~0x003ff000; /* align to 4MB */
1771 page_size = 4096 * 1024;
1772 } else {
1773 /* page directory entry */
1774 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & env->a20_mask;
1775 pte = ldl_phys(pte_addr);
1776 if (!(pte & PG_PRESENT_MASK))
1777 return -1;
1778 page_size = 4096;
1779 }
1780 }
1781 pte = pte & env->a20_mask;
14ce26e7 1782 }
14ce26e7 1783
eaa728ee
FB
1784 page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1785 paddr = (pte & TARGET_PAGE_MASK) + page_offset;
1786 return paddr;
3b21e03e 1787}
01df040b
AL
1788
1789void hw_breakpoint_insert(CPUState *env, int index)
1790{
1791 int type, err = 0;
1792
1793 switch (hw_breakpoint_type(env->dr[7], index)) {
1794 case 0:
1795 if (hw_breakpoint_enabled(env->dr[7], index))
1796 err = cpu_breakpoint_insert(env, env->dr[index], BP_CPU,
1797 &env->cpu_breakpoint[index]);
1798 break;
1799 case 1:
1800 type = BP_CPU | BP_MEM_WRITE;
1801 goto insert_wp;
1802 case 2:
1803 /* No support for I/O watchpoints yet */
1804 break;
1805 case 3:
1806 type = BP_CPU | BP_MEM_ACCESS;
1807 insert_wp:
1808 err = cpu_watchpoint_insert(env, env->dr[index],
1809 hw_breakpoint_len(env->dr[7], index),
1810 type, &env->cpu_watchpoint[index]);
1811 break;
1812 }
1813 if (err)
1814 env->cpu_breakpoint[index] = NULL;
1815}
1816
1817void hw_breakpoint_remove(CPUState *env, int index)
1818{
1819 if (!env->cpu_breakpoint[index])
1820 return;
1821 switch (hw_breakpoint_type(env->dr[7], index)) {
1822 case 0:
1823 if (hw_breakpoint_enabled(env->dr[7], index))
1824 cpu_breakpoint_remove_by_ref(env, env->cpu_breakpoint[index]);
1825 break;
1826 case 1:
1827 case 3:
1828 cpu_watchpoint_remove_by_ref(env, env->cpu_watchpoint[index]);
1829 break;
1830 case 2:
1831 /* No support for I/O watchpoints yet */
1832 break;
1833 }
1834}
1835
1836int check_hw_breakpoints(CPUState *env, int force_dr6_update)
1837{
1838 target_ulong dr6;
1839 int reg, type;
1840 int hit_enabled = 0;
1841
1842 dr6 = env->dr[6] & ~0xf;
1843 for (reg = 0; reg < 4; reg++) {
1844 type = hw_breakpoint_type(env->dr[7], reg);
1845 if ((type == 0 && env->dr[reg] == env->eip) ||
1846 ((type & 1) && env->cpu_watchpoint[reg] &&
1847 (env->cpu_watchpoint[reg]->flags & BP_WATCHPOINT_HIT))) {
1848 dr6 |= 1 << reg;
1849 if (hw_breakpoint_enabled(env->dr[7], reg))
1850 hit_enabled = 1;
1851 }
1852 }
1853 if (hit_enabled || force_dr6_update)
1854 env->dr[6] = dr6;
1855 return hit_enabled;
1856}
1857
1858static CPUDebugExcpHandler *prev_debug_excp_handler;
1859
63a54736 1860void raise_exception_env(int exception_index, CPUState *env);
01df040b
AL
1861
1862static void breakpoint_handler(CPUState *env)
1863{
1864 CPUBreakpoint *bp;
1865
1866 if (env->watchpoint_hit) {
1867 if (env->watchpoint_hit->flags & BP_CPU) {
1868 env->watchpoint_hit = NULL;
1869 if (check_hw_breakpoints(env, 0))
63a54736 1870 raise_exception_env(EXCP01_DB, env);
01df040b
AL
1871 else
1872 cpu_resume_from_signal(env, NULL);
1873 }
1874 } else {
72cf2d4f 1875 QTAILQ_FOREACH(bp, &env->breakpoints, entry)
01df040b
AL
1876 if (bp->pc == env->eip) {
1877 if (bp->flags & BP_CPU) {
1878 check_hw_breakpoints(env, 1);
63a54736 1879 raise_exception_env(EXCP01_DB, env);
01df040b
AL
1880 }
1881 break;
1882 }
1883 }
1884 if (prev_debug_excp_handler)
1885 prev_debug_excp_handler(env);
1886}
79c4f6b0
HY
1887
1888/* This should come from sysemu.h - if we could include it here... */
1889void qemu_system_reset_request(void);
1890
1891void cpu_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
1892 uint64_t mcg_status, uint64_t addr, uint64_t misc)
1893{
1894 uint64_t mcg_cap = cenv->mcg_cap;
1895 unsigned bank_num = mcg_cap & 0xff;
1896 uint64_t *banks = cenv->mce_banks;
1897
1898 if (bank >= bank_num || !(status & MCI_STATUS_VAL))
1899 return;
1900
1901 /*
1902 * if MSR_MCG_CTL is not all 1s, the uncorrected error
1903 * reporting is disabled
1904 */
1905 if ((status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
1906 cenv->mcg_ctl != ~(uint64_t)0)
1907 return;
1908 banks += 4 * bank;
1909 /*
1910 * if MSR_MCi_CTL is not all 1s, the uncorrected error
1911 * reporting is disabled for the bank
1912 */
1913 if ((status & MCI_STATUS_UC) && banks[0] != ~(uint64_t)0)
1914 return;
1915 if (status & MCI_STATUS_UC) {
1916 if ((cenv->mcg_status & MCG_STATUS_MCIP) ||
1917 !(cenv->cr[4] & CR4_MCE_MASK)) {
1918 fprintf(stderr, "injects mce exception while previous "
1919 "one is in progress!\n");
1920 qemu_log_mask(CPU_LOG_RESET, "Triple fault\n");
1921 qemu_system_reset_request();
1922 return;
1923 }
1924 if (banks[1] & MCI_STATUS_VAL)
1925 status |= MCI_STATUS_OVER;
1926 banks[2] = addr;
1927 banks[3] = misc;
1928 cenv->mcg_status = mcg_status;
1929 banks[1] = status;
1930 cpu_interrupt(cenv, CPU_INTERRUPT_MCE);
1931 } else if (!(banks[1] & MCI_STATUS_VAL)
1932 || !(banks[1] & MCI_STATUS_UC)) {
1933 if (banks[1] & MCI_STATUS_VAL)
1934 status |= MCI_STATUS_OVER;
1935 banks[2] = addr;
1936 banks[3] = misc;
1937 banks[1] = status;
1938 } else
1939 banks[1] |= MCI_STATUS_OVER;
1940}
74ce674f 1941#endif /* !CONFIG_USER_ONLY */
6fd805e1 1942
79c4f6b0
HY
1943static void mce_init(CPUX86State *cenv)
1944{
1945 unsigned int bank, bank_num;
1946
1947 if (((cenv->cpuid_version >> 8)&0xf) >= 6
1948 && (cenv->cpuid_features&(CPUID_MCE|CPUID_MCA)) == (CPUID_MCE|CPUID_MCA)) {
1949 cenv->mcg_cap = MCE_CAP_DEF | MCE_BANKS_DEF;
1950 cenv->mcg_ctl = ~(uint64_t)0;
ac74d0f1 1951 bank_num = MCE_BANKS_DEF;
79c4f6b0
HY
1952 for (bank = 0; bank < bank_num; bank++)
1953 cenv->mce_banks[bank*4] = ~(uint64_t)0;
1954 }
1955}
1956
e00b6f80
AL
1957static void host_cpuid(uint32_t function, uint32_t count,
1958 uint32_t *eax, uint32_t *ebx,
7ba1e619
AL
1959 uint32_t *ecx, uint32_t *edx)
1960{
10781c09 1961#if defined(CONFIG_KVM)
7ba1e619
AL
1962 uint32_t vec[4];
1963
1964#ifdef __x86_64__
1965 asm volatile("cpuid"
e00b6f80
AL
1966 : "=a"(vec[0]), "=b"(vec[1]),
1967 "=c"(vec[2]), "=d"(vec[3])
1968 : "0"(function), "c"(count) : "cc");
7ba1e619
AL
1969#else
1970 asm volatile("pusha \n\t"
e00b6f80 1971 "cpuid \n\t"
b36d24b6
AL
1972 "mov %%eax, 0(%2) \n\t"
1973 "mov %%ebx, 4(%2) \n\t"
1974 "mov %%ecx, 8(%2) \n\t"
1975 "mov %%edx, 12(%2) \n\t"
e00b6f80
AL
1976 "popa"
1977 : : "a"(function), "c"(count), "S"(vec)
1978 : "memory", "cc");
7ba1e619
AL
1979#endif
1980
1981 if (eax)
1982 *eax = vec[0];
1983 if (ebx)
1984 *ebx = vec[1];
1985 if (ecx)
1986 *ecx = vec[2];
1987 if (edx)
1988 *edx = vec[3];
7ba1e619 1989#endif
10781c09 1990}
7ba1e619 1991
6d9fef1a
AP
1992static void get_cpuid_vendor(CPUX86State *env, uint32_t *ebx,
1993 uint32_t *ecx, uint32_t *edx)
1994{
1995 *ebx = env->cpuid_vendor1;
1996 *edx = env->cpuid_vendor2;
1997 *ecx = env->cpuid_vendor3;
1998
1999 /* sysenter isn't supported on compatibility mode on AMD, syscall
2000 * isn't supported in compatibility mode on Intel.
2001 * Normally we advertise the actual cpu vendor, but you can override
2002 * this if you want to use KVM's sysenter/syscall emulation
2003 * in compatibility mode and when doing cross vendor migration
2004 */
2005 if (kvm_enabled() && env->cpuid_vendor_override) {
2006 host_cpuid(0, 0, NULL, ebx, ecx, edx);
2007 }
2008}
2009
e00b6f80 2010void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
6fd805e1
AL
2011 uint32_t *eax, uint32_t *ebx,
2012 uint32_t *ecx, uint32_t *edx)
2013{
2014 /* test if maximum index reached */
2015 if (index & 0x80000000) {
2016 if (index > env->cpuid_xlevel)
2017 index = env->cpuid_level;
2018 } else {
2019 if (index > env->cpuid_level)
2020 index = env->cpuid_level;
2021 }
2022
2023 switch(index) {
2024 case 0:
2025 *eax = env->cpuid_level;
6d9fef1a 2026 get_cpuid_vendor(env, ebx, ecx, edx);
6fd805e1
AL
2027 break;
2028 case 1:
2029 *eax = env->cpuid_version;
2030 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
2031 *ecx = env->cpuid_ext_features;
2032 *edx = env->cpuid_features;
400281af
AP
2033 if (env->nr_cores * env->nr_threads > 1) {
2034 *ebx |= (env->nr_cores * env->nr_threads) << 16;
2035 *edx |= 1 << 28; /* HTT bit */
2036 }
6fd805e1
AL
2037 break;
2038 case 2:
2039 /* cache info: needed for Pentium Pro compatibility */
2040 *eax = 1;
2041 *ebx = 0;
2042 *ecx = 0;
2043 *edx = 0x2c307d;
2044 break;
2045 case 4:
2046 /* cache info: needed for Core compatibility */
400281af
AP
2047 if (env->nr_cores > 1) {
2048 *eax = (env->nr_cores - 1) << 26;
2049 } else {
2050 *eax = 0;
2051 }
e00b6f80 2052 switch (count) {
6fd805e1 2053 case 0: /* L1 dcache info */
400281af 2054 *eax |= 0x0000121;
6fd805e1
AL
2055 *ebx = 0x1c0003f;
2056 *ecx = 0x000003f;
2057 *edx = 0x0000001;
2058 break;
2059 case 1: /* L1 icache info */
400281af 2060 *eax |= 0x0000122;
6fd805e1
AL
2061 *ebx = 0x1c0003f;
2062 *ecx = 0x000003f;
2063 *edx = 0x0000001;
2064 break;
2065 case 2: /* L2 cache info */
400281af
AP
2066 *eax |= 0x0000143;
2067 if (env->nr_threads > 1) {
2068 *eax |= (env->nr_threads - 1) << 14;
2069 }
6fd805e1
AL
2070 *ebx = 0x3c0003f;
2071 *ecx = 0x0000fff;
2072 *edx = 0x0000001;
2073 break;
2074 default: /* end of info */
2075 *eax = 0;
2076 *ebx = 0;
2077 *ecx = 0;
2078 *edx = 0;
2079 break;
2080 }
6fd805e1
AL
2081 break;
2082 case 5:
2083 /* mwait info: needed for Core compatibility */
2084 *eax = 0; /* Smallest monitor-line size in bytes */
2085 *ebx = 0; /* Largest monitor-line size in bytes */
2086 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
2087 *edx = 0;
2088 break;
2089 case 6:
2090 /* Thermal and Power Leaf */
2091 *eax = 0;
2092 *ebx = 0;
2093 *ecx = 0;
2094 *edx = 0;
2095 break;
2096 case 9:
2097 /* Direct Cache Access Information Leaf */
2098 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
2099 *ebx = 0;
2100 *ecx = 0;
2101 *edx = 0;
2102 break;
2103 case 0xA:
2104 /* Architectural Performance Monitoring Leaf */
2105 *eax = 0;
2106 *ebx = 0;
2107 *ecx = 0;
2108 *edx = 0;
2109 break;
2110 case 0x80000000:
2111 *eax = env->cpuid_xlevel;
2112 *ebx = env->cpuid_vendor1;
2113 *edx = env->cpuid_vendor2;
2114 *ecx = env->cpuid_vendor3;
2115 break;
2116 case 0x80000001:
f441bee8 2117 *eax = env->cpuid_version;
6fd805e1
AL
2118 *ebx = 0;
2119 *ecx = env->cpuid_ext3_features;
2120 *edx = env->cpuid_ext2_features;
7ba1e619 2121
6d9fef1a
AP
2122 /* The Linux kernel checks for the CMPLegacy bit and
2123 * discards multiple thread information if it is set.
2124 * So dont set it here for Intel to make Linux guests happy.
2125 */
2126 if (env->nr_cores * env->nr_threads > 1) {
2127 uint32_t tebx, tecx, tedx;
2128 get_cpuid_vendor(env, &tebx, &tecx, &tedx);
2129 if (tebx != CPUID_VENDOR_INTEL_1 ||
2130 tedx != CPUID_VENDOR_INTEL_2 ||
2131 tecx != CPUID_VENDOR_INTEL_3) {
2132 *ecx |= 1 << 1; /* CmpLegacy bit */
2133 }
400281af
AP
2134 }
2135
7ba1e619 2136 if (kvm_enabled()) {
f1e00a9c 2137 /* Nested SVM not yet supported in upstream QEMU */
671e4676 2138 *ecx &= ~CPUID_EXT3_SVM;
7ba1e619 2139 }
6fd805e1
AL
2140 break;
2141 case 0x80000002:
2142 case 0x80000003:
2143 case 0x80000004:
2144 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
2145 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
2146 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
2147 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
2148 break;
2149 case 0x80000005:
2150 /* cache info (L1 cache) */
2151 *eax = 0x01ff01ff;
2152 *ebx = 0x01ff01ff;
2153 *ecx = 0x40020140;
2154 *edx = 0x40020140;
2155 break;
2156 case 0x80000006:
2157 /* cache info (L2 cache) */
2158 *eax = 0;
2159 *ebx = 0x42004200;
2160 *ecx = 0x02008140;
2161 *edx = 0;
2162 break;
2163 case 0x80000008:
2164 /* virtual & phys address size in low 2 bytes. */
2165/* XXX: This value must match the one used in the MMU code. */
2166 if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
2167 /* 64 bit processor */
6fd805e1
AL
2168/* XXX: The physical address space is limited to 42 bits in exec.c. */
2169 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
6fd805e1 2170 } else {
6fd805e1
AL
2171 if (env->cpuid_features & CPUID_PSE36)
2172 *eax = 0x00000024; /* 36 bits physical */
2173 else
2174 *eax = 0x00000020; /* 32 bits physical */
6fd805e1
AL
2175 }
2176 *ebx = 0;
2177 *ecx = 0;
2178 *edx = 0;
400281af
AP
2179 if (env->nr_cores * env->nr_threads > 1) {
2180 *ecx |= (env->nr_cores * env->nr_threads) - 1;
2181 }
6fd805e1
AL
2182 break;
2183 case 0x8000000A:
2184 *eax = 0x00000001; /* SVM Revision */
2185 *ebx = 0x00000010; /* nr of ASIDs */
2186 *ecx = 0;
2187 *edx = 0; /* optional features */
2188 break;
2189 default:
2190 /* reserved values: zero */
2191 *eax = 0;
2192 *ebx = 0;
2193 *ecx = 0;
2194 *edx = 0;
2195 break;
2196 }
2197}
01df040b 2198
84273177
JK
2199
2200int cpu_x86_get_descr_debug(CPUX86State *env, unsigned int selector,
2201 target_ulong *base, unsigned int *limit,
2202 unsigned int *flags)
2203{
2204 SegmentCache *dt;
2205 target_ulong ptr;
2206 uint32_t e1, e2;
2207 int index;
2208
2209 if (selector & 0x4)
2210 dt = &env->ldt;
2211 else
2212 dt = &env->gdt;
2213 index = selector & ~7;
2214 ptr = dt->base + index;
2215 if ((index + 7) > dt->limit
2216 || cpu_memory_rw_debug(env, ptr, (uint8_t *)&e1, sizeof(e1), 0) != 0
2217 || cpu_memory_rw_debug(env, ptr+4, (uint8_t *)&e2, sizeof(e2), 0) != 0)
2218 return 0;
2219
2220 *base = ((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
2221 *limit = (e1 & 0xffff) | (e2 & 0x000f0000);
2222 if (e2 & DESC_G_MASK)
2223 *limit = (*limit << 12) | 0xfff;
2224 *flags = e2;
2225
2226 return 1;
2227}
2228
01df040b
AL
2229CPUX86State *cpu_x86_init(const char *cpu_model)
2230{
2231 CPUX86State *env;
2232 static int inited;
2233
2234 env = qemu_mallocz(sizeof(CPUX86State));
01df040b
AL
2235 cpu_exec_init(env);
2236 env->cpu_model_str = cpu_model;
2237
2238 /* init various static tables */
2239 if (!inited) {
2240 inited = 1;
2241 optimize_flags_init();
2242#ifndef CONFIG_USER_ONLY
2243 prev_debug_excp_handler =
2244 cpu_set_debug_excp_handler(breakpoint_handler);
2245#endif
2246 }
2247 if (cpu_x86_register(env, cpu_model) < 0) {
2248 cpu_x86_close(env);
2249 return NULL;
2250 }
79c4f6b0 2251 mce_init(env);
0bf46a40
AL
2252
2253 qemu_init_vcpu(env);
2254
01df040b
AL
2255 return env;
2256}
b09ea7d5
GN
2257
2258#if !defined(CONFIG_USER_ONLY)
2259void do_cpu_init(CPUState *env)
2260{
2261 int sipi = env->interrupt_request & CPU_INTERRUPT_SIPI;
2262 cpu_reset(env);
2263 env->interrupt_request = sipi;
2264 apic_init_reset(env);
2265}
2266
2267void do_cpu_sipi(CPUState *env)
2268{
2269 apic_sipi(env);
2270}
2271#else
2272void do_cpu_init(CPUState *env)
2273{
2274}
2275void do_cpu_sipi(CPUState *env)
2276{
2277}
2278#endif