]> Git Repo - qemu.git/blob - target-i386/helper.c
target-i386: Add XSAVE extension
[qemu.git] / target-i386 / helper.c
1 /*
2  *  i386 helpers (without register variable usage)
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "sysemu/kvm.h"
23 #include "kvm_i386.h"
24 #ifndef CONFIG_USER_ONLY
25 #include "sysemu/sysemu.h"
26 #include "monitor/monitor.h"
27 #include "hw/i386/apic_internal.h"
28 #endif
29
30 static void cpu_x86_version(CPUX86State *env, int *family, int *model)
31 {
32     int cpuver = env->cpuid_version;
33
34     if (family == NULL || model == NULL) {
35         return;
36     }
37
38     *family = (cpuver >> 8) & 0x0f;
39     *model = ((cpuver >> 12) & 0xf0) + ((cpuver >> 4) & 0x0f);
40 }
41
42 /* Broadcast MCA signal for processor version 06H_EH and above */
43 int cpu_x86_support_mca_broadcast(CPUX86State *env)
44 {
45     int family = 0;
46     int model = 0;
47
48     cpu_x86_version(env, &family, &model);
49     if ((family == 6 && model >= 14) || family > 6) {
50         return 1;
51     }
52
53     return 0;
54 }
55
56 /***********************************************************/
57 /* x86 debug */
58
59 static const char *cc_op_str[CC_OP_NB] = {
60     "DYNAMIC",
61     "EFLAGS",
62
63     "MULB",
64     "MULW",
65     "MULL",
66     "MULQ",
67
68     "ADDB",
69     "ADDW",
70     "ADDL",
71     "ADDQ",
72
73     "ADCB",
74     "ADCW",
75     "ADCL",
76     "ADCQ",
77
78     "SUBB",
79     "SUBW",
80     "SUBL",
81     "SUBQ",
82
83     "SBBB",
84     "SBBW",
85     "SBBL",
86     "SBBQ",
87
88     "LOGICB",
89     "LOGICW",
90     "LOGICL",
91     "LOGICQ",
92
93     "INCB",
94     "INCW",
95     "INCL",
96     "INCQ",
97
98     "DECB",
99     "DECW",
100     "DECL",
101     "DECQ",
102
103     "SHLB",
104     "SHLW",
105     "SHLL",
106     "SHLQ",
107
108     "SARB",
109     "SARW",
110     "SARL",
111     "SARQ",
112
113     "BMILGB",
114     "BMILGW",
115     "BMILGL",
116     "BMILGQ",
117
118     "ADCX",
119     "ADOX",
120     "ADCOX",
121
122     "CLR",
123 };
124
125 static void
126 cpu_x86_dump_seg_cache(CPUX86State *env, FILE *f, fprintf_function cpu_fprintf,
127                        const char *name, struct SegmentCache *sc)
128 {
129 #ifdef TARGET_X86_64
130     if (env->hflags & HF_CS64_MASK) {
131         cpu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name,
132                     sc->selector, sc->base, sc->limit, sc->flags & 0x00ffff00);
133     } else
134 #endif
135     {
136         cpu_fprintf(f, "%-3s=%04x %08x %08x %08x", name, sc->selector,
137                     (uint32_t)sc->base, sc->limit, sc->flags & 0x00ffff00);
138     }
139
140     if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK))
141         goto done;
142
143     cpu_fprintf(f, " DPL=%d ", (sc->flags & DESC_DPL_MASK) >> DESC_DPL_SHIFT);
144     if (sc->flags & DESC_S_MASK) {
145         if (sc->flags & DESC_CS_MASK) {
146             cpu_fprintf(f, (sc->flags & DESC_L_MASK) ? "CS64" :
147                            ((sc->flags & DESC_B_MASK) ? "CS32" : "CS16"));
148             cpu_fprintf(f, " [%c%c", (sc->flags & DESC_C_MASK) ? 'C' : '-',
149                         (sc->flags & DESC_R_MASK) ? 'R' : '-');
150         } else {
151             cpu_fprintf(f,
152                         (sc->flags & DESC_B_MASK || env->hflags & HF_LMA_MASK)
153                         ? "DS  " : "DS16");
154             cpu_fprintf(f, " [%c%c", (sc->flags & DESC_E_MASK) ? 'E' : '-',
155                         (sc->flags & DESC_W_MASK) ? 'W' : '-');
156         }
157         cpu_fprintf(f, "%c]", (sc->flags & DESC_A_MASK) ? 'A' : '-');
158     } else {
159         static const char *sys_type_name[2][16] = {
160             { /* 32 bit mode */
161                 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
162                 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
163                 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
164                 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
165             },
166             { /* 64 bit mode */
167                 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
168                 "Reserved", "Reserved", "Reserved", "Reserved",
169                 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
170                 "Reserved", "IntGate64", "TrapGate64"
171             }
172         };
173         cpu_fprintf(f, "%s",
174                     sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0]
175                                  [(sc->flags & DESC_TYPE_MASK)
176                                   >> DESC_TYPE_SHIFT]);
177     }
178 done:
179     cpu_fprintf(f, "\n");
180 }
181
182 #ifndef CONFIG_USER_ONLY
183
184 /* ARRAY_SIZE check is not required because
185  * DeliveryMode(dm) has a size of 3 bit.
186  */
187 static inline const char *dm2str(uint32_t dm)
188 {
189     static const char *str[] = {
190         "Fixed",
191         "...",
192         "SMI",
193         "...",
194         "NMI",
195         "INIT",
196         "...",
197         "ExtINT"
198     };
199     return str[dm];
200 }
201
202 static void dump_apic_lvt(FILE *f, fprintf_function cpu_fprintf,
203                           const char *name, uint32_t lvt, bool is_timer)
204 {
205     uint32_t dm = (lvt & APIC_LVT_DELIV_MOD) >> APIC_LVT_DELIV_MOD_SHIFT;
206     cpu_fprintf(f,
207                 "%s\t 0x%08x %s %-5s %-6s %-7s %-12s %-6s",
208                 name, lvt,
209                 lvt & APIC_LVT_INT_POLARITY ? "active-lo" : "active-hi",
210                 lvt & APIC_LVT_LEVEL_TRIGGER ? "level" : "edge",
211                 lvt & APIC_LVT_MASKED ? "masked" : "",
212                 lvt & APIC_LVT_DELIV_STS ? "pending" : "",
213                 !is_timer ?
214                     "" : lvt & APIC_LVT_TIMER_PERIODIC ?
215                             "periodic" : lvt & APIC_LVT_TIMER_TSCDEADLINE ?
216                                             "tsc-deadline" : "one-shot",
217                 dm2str(dm));
218     if (dm != APIC_DM_NMI) {
219         cpu_fprintf(f, " (vec %u)\n", lvt & APIC_VECTOR_MASK);
220     } else {
221         cpu_fprintf(f, "\n");
222     }
223 }
224
225 /* ARRAY_SIZE check is not required because
226  * destination shorthand has a size of 2 bit.
227  */
228 static inline const char *shorthand2str(uint32_t shorthand)
229 {
230     const char *str[] = {
231         "no-shorthand", "self", "all-self", "all"
232     };
233     return str[shorthand];
234 }
235
236 static inline uint8_t divider_conf(uint32_t divide_conf)
237 {
238     uint8_t divide_val = ((divide_conf & 0x8) >> 1) | (divide_conf & 0x3);
239
240     return divide_val == 7 ? 1 : 2 << divide_val;
241 }
242
243 static inline void mask2str(char *str, uint32_t val, uint8_t size)
244 {
245     while (size--) {
246         *str++ = (val >> size) & 1 ? '1' : '0';
247     }
248     *str = 0;
249 }
250
251 #define MAX_LOGICAL_APIC_ID_MASK_SIZE 16
252
253 static void dump_apic_icr(FILE *f, fprintf_function cpu_fprintf,
254                           APICCommonState *s, CPUX86State *env)
255 {
256     uint32_t icr = s->icr[0], icr2 = s->icr[1];
257     uint8_t dest_shorthand = \
258         (icr & APIC_ICR_DEST_SHORT) >> APIC_ICR_DEST_SHORT_SHIFT;
259     bool logical_mod = icr & APIC_ICR_DEST_MOD;
260     char apic_id_str[MAX_LOGICAL_APIC_ID_MASK_SIZE + 1];
261     uint32_t dest_field;
262     bool x2apic;
263
264     cpu_fprintf(f, "ICR\t 0x%08x %s %s %s %s\n",
265                 icr,
266                 logical_mod ? "logical" : "physical",
267                 icr & APIC_ICR_TRIGGER_MOD ? "level" : "edge",
268                 icr & APIC_ICR_LEVEL ? "assert" : "de-assert",
269                 shorthand2str(dest_shorthand));
270
271     cpu_fprintf(f, "ICR2\t 0x%08x", icr2);
272     if (dest_shorthand != 0) {
273         cpu_fprintf(f, "\n");
274         return;
275     }
276     x2apic = env->features[FEAT_1_ECX] & CPUID_EXT_X2APIC;
277     dest_field = x2apic ? icr2 : icr2 >> APIC_ICR_DEST_SHIFT;
278
279     if (!logical_mod) {
280         if (x2apic) {
281             cpu_fprintf(f, " cpu %u (X2APIC ID)\n", dest_field);
282         } else {
283             cpu_fprintf(f, " cpu %u (APIC ID)\n",
284                         dest_field & APIC_LOGDEST_XAPIC_ID);
285         }
286         return;
287     }
288
289     if (s->dest_mode == 0xf) { /* flat mode */
290         mask2str(apic_id_str, icr2 >> APIC_ICR_DEST_SHIFT, 8);
291         cpu_fprintf(f, " mask %s (APIC ID)\n", apic_id_str);
292     } else if (s->dest_mode == 0) { /* cluster mode */
293         if (x2apic) {
294             mask2str(apic_id_str, dest_field & APIC_LOGDEST_X2APIC_ID, 16);
295             cpu_fprintf(f, " cluster %u mask %s (X2APIC ID)\n",
296                         dest_field >> APIC_LOGDEST_X2APIC_SHIFT, apic_id_str);
297         } else {
298             mask2str(apic_id_str, dest_field & APIC_LOGDEST_XAPIC_ID, 4);
299             cpu_fprintf(f, " cluster %u mask %s (APIC ID)\n",
300                         dest_field >> APIC_LOGDEST_XAPIC_SHIFT, apic_id_str);
301         }
302     }
303 }
304
305 static void dump_apic_interrupt(FILE *f, fprintf_function cpu_fprintf,
306                                 const char *name, uint32_t *ireg_tab,
307                                 uint32_t *tmr_tab)
308 {
309     int i, empty = true;
310
311     cpu_fprintf(f, "%s\t ", name);
312     for (i = 0; i < 256; i++) {
313         if (apic_get_bit(ireg_tab, i)) {
314             cpu_fprintf(f, "%u%s ", i,
315                         apic_get_bit(tmr_tab, i) ? "(level)" : "");
316             empty = false;
317         }
318     }
319     cpu_fprintf(f, "%s\n", empty ? "(none)" : "");
320 }
321
322 void x86_cpu_dump_local_apic_state(CPUState *cs, FILE *f,
323                                    fprintf_function cpu_fprintf, int flags)
324 {
325     X86CPU *cpu = X86_CPU(cs);
326     APICCommonState *s = APIC_COMMON(cpu->apic_state);
327     uint32_t *lvt = s->lvt;
328
329     cpu_fprintf(f, "dumping local APIC state for CPU %-2u\n\n",
330                 CPU(cpu)->cpu_index);
331     dump_apic_lvt(f, cpu_fprintf, "LVT0", lvt[APIC_LVT_LINT0], false);
332     dump_apic_lvt(f, cpu_fprintf, "LVT1", lvt[APIC_LVT_LINT1], false);
333     dump_apic_lvt(f, cpu_fprintf, "LVTPC", lvt[APIC_LVT_PERFORM], false);
334     dump_apic_lvt(f, cpu_fprintf, "LVTERR", lvt[APIC_LVT_ERROR], false);
335     dump_apic_lvt(f, cpu_fprintf, "LVTTHMR", lvt[APIC_LVT_THERMAL], false);
336     dump_apic_lvt(f, cpu_fprintf, "LVTT", lvt[APIC_LVT_TIMER], true);
337
338     cpu_fprintf(f, "Timer\t DCR=0x%x (divide by %u) initial_count = %u\n",
339                 s->divide_conf & APIC_DCR_MASK,
340                 divider_conf(s->divide_conf),
341                 s->initial_count);
342
343     cpu_fprintf(f, "SPIV\t 0x%08x APIC %s, focus=%s, spurious vec %u\n",
344                 s->spurious_vec,
345                 s->spurious_vec & APIC_SPURIO_ENABLED ? "enabled" : "disabled",
346                 s->spurious_vec & APIC_SPURIO_FOCUS ? "on" : "off",
347                 s->spurious_vec & APIC_VECTOR_MASK);
348
349     dump_apic_icr(f, cpu_fprintf, s, &cpu->env);
350
351     cpu_fprintf(f, "ESR\t 0x%08x\n", s->esr);
352
353     dump_apic_interrupt(f, cpu_fprintf, "ISR", s->isr, s->tmr);
354     dump_apic_interrupt(f, cpu_fprintf, "IRR", s->irr, s->tmr);
355
356     cpu_fprintf(f, "\nAPR 0x%02x TPR 0x%02x DFR 0x%02x LDR 0x%02x",
357                 s->arb_id, s->tpr, s->dest_mode, s->log_dest);
358     if (s->dest_mode == 0) {
359         cpu_fprintf(f, "(cluster %u: id %u)",
360                     s->log_dest >> APIC_LOGDEST_XAPIC_SHIFT,
361                     s->log_dest & APIC_LOGDEST_XAPIC_ID);
362     }
363     cpu_fprintf(f, " PPR 0x%02x\n", apic_get_ppr(s));
364 }
365 #else
366 void x86_cpu_dump_local_apic_state(CPUState *cs, FILE *f,
367                                    fprintf_function cpu_fprintf, int flags)
368 {
369 }
370 #endif /* !CONFIG_USER_ONLY */
371
372 #define DUMP_CODE_BYTES_TOTAL    50
373 #define DUMP_CODE_BYTES_BACKWARD 20
374
375 void x86_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
376                         int flags)
377 {
378     X86CPU *cpu = X86_CPU(cs);
379     CPUX86State *env = &cpu->env;
380     int eflags, i, nb;
381     char cc_op_name[32];
382     static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
383
384     eflags = cpu_compute_eflags(env);
385 #ifdef TARGET_X86_64
386     if (env->hflags & HF_CS64_MASK) {
387         cpu_fprintf(f,
388                     "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n"
389                     "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n"
390                     "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n"
391                     "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n"
392                     "RIP=%016" PRIx64 " RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
393                     env->regs[R_EAX],
394                     env->regs[R_EBX],
395                     env->regs[R_ECX],
396                     env->regs[R_EDX],
397                     env->regs[R_ESI],
398                     env->regs[R_EDI],
399                     env->regs[R_EBP],
400                     env->regs[R_ESP],
401                     env->regs[8],
402                     env->regs[9],
403                     env->regs[10],
404                     env->regs[11],
405                     env->regs[12],
406                     env->regs[13],
407                     env->regs[14],
408                     env->regs[15],
409                     env->eip, eflags,
410                     eflags & DF_MASK ? 'D' : '-',
411                     eflags & CC_O ? 'O' : '-',
412                     eflags & CC_S ? 'S' : '-',
413                     eflags & CC_Z ? 'Z' : '-',
414                     eflags & CC_A ? 'A' : '-',
415                     eflags & CC_P ? 'P' : '-',
416                     eflags & CC_C ? 'C' : '-',
417                     env->hflags & HF_CPL_MASK,
418                     (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
419                     (env->a20_mask >> 20) & 1,
420                     (env->hflags >> HF_SMM_SHIFT) & 1,
421                     cs->halted);
422     } else
423 #endif
424     {
425         cpu_fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
426                     "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
427                     "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
428                     (uint32_t)env->regs[R_EAX],
429                     (uint32_t)env->regs[R_EBX],
430                     (uint32_t)env->regs[R_ECX],
431                     (uint32_t)env->regs[R_EDX],
432                     (uint32_t)env->regs[R_ESI],
433                     (uint32_t)env->regs[R_EDI],
434                     (uint32_t)env->regs[R_EBP],
435                     (uint32_t)env->regs[R_ESP],
436                     (uint32_t)env->eip, eflags,
437                     eflags & DF_MASK ? 'D' : '-',
438                     eflags & CC_O ? 'O' : '-',
439                     eflags & CC_S ? 'S' : '-',
440                     eflags & CC_Z ? 'Z' : '-',
441                     eflags & CC_A ? 'A' : '-',
442                     eflags & CC_P ? 'P' : '-',
443                     eflags & CC_C ? 'C' : '-',
444                     env->hflags & HF_CPL_MASK,
445                     (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
446                     (env->a20_mask >> 20) & 1,
447                     (env->hflags >> HF_SMM_SHIFT) & 1,
448                     cs->halted);
449     }
450
451     for(i = 0; i < 6; i++) {
452         cpu_x86_dump_seg_cache(env, f, cpu_fprintf, seg_name[i],
453                                &env->segs[i]);
454     }
455     cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "LDT", &env->ldt);
456     cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "TR", &env->tr);
457
458 #ifdef TARGET_X86_64
459     if (env->hflags & HF_LMA_MASK) {
460         cpu_fprintf(f, "GDT=     %016" PRIx64 " %08x\n",
461                     env->gdt.base, env->gdt.limit);
462         cpu_fprintf(f, "IDT=     %016" PRIx64 " %08x\n",
463                     env->idt.base, env->idt.limit);
464         cpu_fprintf(f, "CR0=%08x CR2=%016" PRIx64 " CR3=%016" PRIx64 " CR4=%08x\n",
465                     (uint32_t)env->cr[0],
466                     env->cr[2],
467                     env->cr[3],
468                     (uint32_t)env->cr[4]);
469         for(i = 0; i < 4; i++)
470             cpu_fprintf(f, "DR%d=%016" PRIx64 " ", i, env->dr[i]);
471         cpu_fprintf(f, "\nDR6=%016" PRIx64 " DR7=%016" PRIx64 "\n",
472                     env->dr[6], env->dr[7]);
473     } else
474 #endif
475     {
476         cpu_fprintf(f, "GDT=     %08x %08x\n",
477                     (uint32_t)env->gdt.base, env->gdt.limit);
478         cpu_fprintf(f, "IDT=     %08x %08x\n",
479                     (uint32_t)env->idt.base, env->idt.limit);
480         cpu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
481                     (uint32_t)env->cr[0],
482                     (uint32_t)env->cr[2],
483                     (uint32_t)env->cr[3],
484                     (uint32_t)env->cr[4]);
485         for(i = 0; i < 4; i++) {
486             cpu_fprintf(f, "DR%d=" TARGET_FMT_lx " ", i, env->dr[i]);
487         }
488         cpu_fprintf(f, "\nDR6=" TARGET_FMT_lx " DR7=" TARGET_FMT_lx "\n",
489                     env->dr[6], env->dr[7]);
490     }
491     if (flags & CPU_DUMP_CCOP) {
492         if ((unsigned)env->cc_op < CC_OP_NB)
493             snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]);
494         else
495             snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
496 #ifdef TARGET_X86_64
497         if (env->hflags & HF_CS64_MASK) {
498             cpu_fprintf(f, "CCS=%016" PRIx64 " CCD=%016" PRIx64 " CCO=%-8s\n",
499                         env->cc_src, env->cc_dst,
500                         cc_op_name);
501         } else
502 #endif
503         {
504             cpu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
505                         (uint32_t)env->cc_src, (uint32_t)env->cc_dst,
506                         cc_op_name);
507         }
508     }
509     cpu_fprintf(f, "EFER=%016" PRIx64 "\n", env->efer);
510     if (flags & CPU_DUMP_FPU) {
511         int fptag;
512         fptag = 0;
513         for(i = 0; i < 8; i++) {
514             fptag |= ((!env->fptags[i]) << i);
515         }
516         cpu_fprintf(f, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
517                     env->fpuc,
518                     (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11,
519                     env->fpstt,
520                     fptag,
521                     env->mxcsr);
522         for(i=0;i<8;i++) {
523             CPU_LDoubleU u;
524             u.d = env->fpregs[i].d;
525             cpu_fprintf(f, "FPR%d=%016" PRIx64 " %04x",
526                         i, u.l.lower, u.l.upper);
527             if ((i & 1) == 1)
528                 cpu_fprintf(f, "\n");
529             else
530                 cpu_fprintf(f, " ");
531         }
532         if (env->hflags & HF_CS64_MASK)
533             nb = 16;
534         else
535             nb = 8;
536         for(i=0;i<nb;i++) {
537             cpu_fprintf(f, "XMM%02d=%08x%08x%08x%08x",
538                         i,
539                         env->xmm_regs[i].ZMM_L(3),
540                         env->xmm_regs[i].ZMM_L(2),
541                         env->xmm_regs[i].ZMM_L(1),
542                         env->xmm_regs[i].ZMM_L(0));
543             if ((i & 1) == 1)
544                 cpu_fprintf(f, "\n");
545             else
546                 cpu_fprintf(f, " ");
547         }
548     }
549     if (flags & CPU_DUMP_CODE) {
550         target_ulong base = env->segs[R_CS].base + env->eip;
551         target_ulong offs = MIN(env->eip, DUMP_CODE_BYTES_BACKWARD);
552         uint8_t code;
553         char codestr[3];
554
555         cpu_fprintf(f, "Code=");
556         for (i = 0; i < DUMP_CODE_BYTES_TOTAL; i++) {
557             if (cpu_memory_rw_debug(cs, base - offs + i, &code, 1, 0) == 0) {
558                 snprintf(codestr, sizeof(codestr), "%02x", code);
559             } else {
560                 snprintf(codestr, sizeof(codestr), "??");
561             }
562             cpu_fprintf(f, "%s%s%s%s", i > 0 ? " " : "",
563                         i == offs ? "<" : "", codestr, i == offs ? ">" : "");
564         }
565         cpu_fprintf(f, "\n");
566     }
567 }
568
569 /***********************************************************/
570 /* x86 mmu */
571 /* XXX: add PGE support */
572
573 void x86_cpu_set_a20(X86CPU *cpu, int a20_state)
574 {
575     CPUX86State *env = &cpu->env;
576
577     a20_state = (a20_state != 0);
578     if (a20_state != ((env->a20_mask >> 20) & 1)) {
579         CPUState *cs = CPU(cpu);
580
581         qemu_log_mask(CPU_LOG_MMU, "A20 update: a20=%d\n", a20_state);
582         /* if the cpu is currently executing code, we must unlink it and
583            all the potentially executing TB */
584         cpu_interrupt(cs, CPU_INTERRUPT_EXITTB);
585
586         /* when a20 is changed, all the MMU mappings are invalid, so
587            we must flush everything */
588         tlb_flush(cs, 1);
589         env->a20_mask = ~(1 << 20) | (a20_state << 20);
590     }
591 }
592
593 void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
594 {
595     X86CPU *cpu = x86_env_get_cpu(env);
596     int pe_state;
597
598     qemu_log_mask(CPU_LOG_MMU, "CR0 update: CR0=0x%08x\n", new_cr0);
599     if ((new_cr0 & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK)) !=
600         (env->cr[0] & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK))) {
601         tlb_flush(CPU(cpu), 1);
602     }
603
604 #ifdef TARGET_X86_64
605     if (!(env->cr[0] & CR0_PG_MASK) && (new_cr0 & CR0_PG_MASK) &&
606         (env->efer & MSR_EFER_LME)) {
607         /* enter in long mode */
608         /* XXX: generate an exception */
609         if (!(env->cr[4] & CR4_PAE_MASK))
610             return;
611         env->efer |= MSR_EFER_LMA;
612         env->hflags |= HF_LMA_MASK;
613     } else if ((env->cr[0] & CR0_PG_MASK) && !(new_cr0 & CR0_PG_MASK) &&
614                (env->efer & MSR_EFER_LMA)) {
615         /* exit long mode */
616         env->efer &= ~MSR_EFER_LMA;
617         env->hflags &= ~(HF_LMA_MASK | HF_CS64_MASK);
618         env->eip &= 0xffffffff;
619     }
620 #endif
621     env->cr[0] = new_cr0 | CR0_ET_MASK;
622
623     /* update PE flag in hidden flags */
624     pe_state = (env->cr[0] & CR0_PE_MASK);
625     env->hflags = (env->hflags & ~HF_PE_MASK) | (pe_state << HF_PE_SHIFT);
626     /* ensure that ADDSEG is always set in real mode */
627     env->hflags |= ((pe_state ^ 1) << HF_ADDSEG_SHIFT);
628     /* update FPU flags */
629     env->hflags = (env->hflags & ~(HF_MP_MASK | HF_EM_MASK | HF_TS_MASK)) |
630         ((new_cr0 << (HF_MP_SHIFT - 1)) & (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK));
631 }
632
633 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
634    the PDPT */
635 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
636 {
637     X86CPU *cpu = x86_env_get_cpu(env);
638
639     env->cr[3] = new_cr3;
640     if (env->cr[0] & CR0_PG_MASK) {
641         qemu_log_mask(CPU_LOG_MMU,
642                         "CR3 update: CR3=" TARGET_FMT_lx "\n", new_cr3);
643         tlb_flush(CPU(cpu), 0);
644     }
645 }
646
647 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
648 {
649     X86CPU *cpu = x86_env_get_cpu(env);
650     uint32_t hflags;
651
652 #if defined(DEBUG_MMU)
653     printf("CR4 update: CR4=%08x\n", (uint32_t)env->cr[4]);
654 #endif
655     if ((new_cr4 ^ env->cr[4]) &
656         (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK |
657          CR4_SMEP_MASK | CR4_SMAP_MASK)) {
658         tlb_flush(CPU(cpu), 1);
659     }
660
661     /* Clear bits we're going to recompute.  */
662     hflags = env->hflags & ~(HF_OSFXSR_MASK | HF_SMAP_MASK);
663
664     /* SSE handling */
665     if (!(env->features[FEAT_1_EDX] & CPUID_SSE)) {
666         new_cr4 &= ~CR4_OSFXSR_MASK;
667     }
668     if (new_cr4 & CR4_OSFXSR_MASK) {
669         hflags |= HF_OSFXSR_MASK;
670     }
671
672     if (!(env->features[FEAT_7_0_EBX] & CPUID_7_0_EBX_SMAP)) {
673         new_cr4 &= ~CR4_SMAP_MASK;
674     }
675     if (new_cr4 & CR4_SMAP_MASK) {
676         hflags |= HF_SMAP_MASK;
677     }
678
679     env->cr[4] = new_cr4;
680     env->hflags = hflags;
681 }
682
683 #if defined(CONFIG_USER_ONLY)
684
685 int x86_cpu_handle_mmu_fault(CPUState *cs, vaddr addr,
686                              int is_write, int mmu_idx)
687 {
688     X86CPU *cpu = X86_CPU(cs);
689     CPUX86State *env = &cpu->env;
690
691     /* user mode only emulation */
692     is_write &= 1;
693     env->cr[2] = addr;
694     env->error_code = (is_write << PG_ERROR_W_BIT);
695     env->error_code |= PG_ERROR_U_MASK;
696     cs->exception_index = EXCP0E_PAGE;
697     return 1;
698 }
699
700 #else
701
702 /* return value:
703  * -1 = cannot handle fault
704  * 0  = nothing more to do
705  * 1  = generate PF fault
706  */
707 int x86_cpu_handle_mmu_fault(CPUState *cs, vaddr addr,
708                              int is_write1, int mmu_idx)
709 {
710     X86CPU *cpu = X86_CPU(cs);
711     CPUX86State *env = &cpu->env;
712     uint64_t ptep, pte;
713     target_ulong pde_addr, pte_addr;
714     int error_code = 0;
715     int is_dirty, prot, page_size, is_write, is_user;
716     hwaddr paddr;
717     uint64_t rsvd_mask = PG_HI_RSVD_MASK;
718     uint32_t page_offset;
719     target_ulong vaddr;
720
721     is_user = mmu_idx == MMU_USER_IDX;
722 #if defined(DEBUG_MMU)
723     printf("MMU fault: addr=%" VADDR_PRIx " w=%d u=%d eip=" TARGET_FMT_lx "\n",
724            addr, is_write1, is_user, env->eip);
725 #endif
726     is_write = is_write1 & 1;
727
728     if (!(env->cr[0] & CR0_PG_MASK)) {
729         pte = addr;
730 #ifdef TARGET_X86_64
731         if (!(env->hflags & HF_LMA_MASK)) {
732             /* Without long mode we can only address 32bits in real mode */
733             pte = (uint32_t)pte;
734         }
735 #endif
736         prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
737         page_size = 4096;
738         goto do_mapping;
739     }
740
741     if (!(env->efer & MSR_EFER_NXE)) {
742         rsvd_mask |= PG_NX_MASK;
743     }
744
745     if (env->cr[4] & CR4_PAE_MASK) {
746         uint64_t pde, pdpe;
747         target_ulong pdpe_addr;
748
749 #ifdef TARGET_X86_64
750         if (env->hflags & HF_LMA_MASK) {
751             uint64_t pml4e_addr, pml4e;
752             int32_t sext;
753
754             /* test virtual address sign extension */
755             sext = (int64_t)addr >> 47;
756             if (sext != 0 && sext != -1) {
757                 env->error_code = 0;
758                 cs->exception_index = EXCP0D_GPF;
759                 return 1;
760             }
761
762             pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
763                 env->a20_mask;
764             pml4e = x86_ldq_phys(cs, pml4e_addr);
765             if (!(pml4e & PG_PRESENT_MASK)) {
766                 goto do_fault;
767             }
768             if (pml4e & (rsvd_mask | PG_PSE_MASK)) {
769                 goto do_fault_rsvd;
770             }
771             if (!(pml4e & PG_ACCESSED_MASK)) {
772                 pml4e |= PG_ACCESSED_MASK;
773                 x86_stl_phys_notdirty(cs, pml4e_addr, pml4e);
774             }
775             ptep = pml4e ^ PG_NX_MASK;
776             pdpe_addr = ((pml4e & PG_ADDRESS_MASK) + (((addr >> 30) & 0x1ff) << 3)) &
777                 env->a20_mask;
778             pdpe = x86_ldq_phys(cs, pdpe_addr);
779             if (!(pdpe & PG_PRESENT_MASK)) {
780                 goto do_fault;
781             }
782             if (pdpe & rsvd_mask) {
783                 goto do_fault_rsvd;
784             }
785             ptep &= pdpe ^ PG_NX_MASK;
786             if (!(pdpe & PG_ACCESSED_MASK)) {
787                 pdpe |= PG_ACCESSED_MASK;
788                 x86_stl_phys_notdirty(cs, pdpe_addr, pdpe);
789             }
790             if (pdpe & PG_PSE_MASK) {
791                 /* 1 GB page */
792                 page_size = 1024 * 1024 * 1024;
793                 pte_addr = pdpe_addr;
794                 pte = pdpe;
795                 goto do_check_protect;
796             }
797         } else
798 #endif
799         {
800             /* XXX: load them when cr3 is loaded ? */
801             pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
802                 env->a20_mask;
803             pdpe = x86_ldq_phys(cs, pdpe_addr);
804             if (!(pdpe & PG_PRESENT_MASK)) {
805                 goto do_fault;
806             }
807             rsvd_mask |= PG_HI_USER_MASK;
808             if (pdpe & (rsvd_mask | PG_NX_MASK)) {
809                 goto do_fault_rsvd;
810             }
811             ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK;
812         }
813
814         pde_addr = ((pdpe & PG_ADDRESS_MASK) + (((addr >> 21) & 0x1ff) << 3)) &
815             env->a20_mask;
816         pde = x86_ldq_phys(cs, pde_addr);
817         if (!(pde & PG_PRESENT_MASK)) {
818             goto do_fault;
819         }
820         if (pde & rsvd_mask) {
821             goto do_fault_rsvd;
822         }
823         ptep &= pde ^ PG_NX_MASK;
824         if (pde & PG_PSE_MASK) {
825             /* 2 MB page */
826             page_size = 2048 * 1024;
827             pte_addr = pde_addr;
828             pte = pde;
829             goto do_check_protect;
830         }
831         /* 4 KB page */
832         if (!(pde & PG_ACCESSED_MASK)) {
833             pde |= PG_ACCESSED_MASK;
834             x86_stl_phys_notdirty(cs, pde_addr, pde);
835         }
836         pte_addr = ((pde & PG_ADDRESS_MASK) + (((addr >> 12) & 0x1ff) << 3)) &
837             env->a20_mask;
838         pte = x86_ldq_phys(cs, pte_addr);
839         if (!(pte & PG_PRESENT_MASK)) {
840             goto do_fault;
841         }
842         if (pte & rsvd_mask) {
843             goto do_fault_rsvd;
844         }
845         /* combine pde and pte nx, user and rw protections */
846         ptep &= pte ^ PG_NX_MASK;
847         page_size = 4096;
848     } else {
849         uint32_t pde;
850
851         /* page directory entry */
852         pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) &
853             env->a20_mask;
854         pde = x86_ldl_phys(cs, pde_addr);
855         if (!(pde & PG_PRESENT_MASK)) {
856             goto do_fault;
857         }
858         ptep = pde | PG_NX_MASK;
859
860         /* if PSE bit is set, then we use a 4MB page */
861         if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
862             page_size = 4096 * 1024;
863             pte_addr = pde_addr;
864
865             /* Bits 20-13 provide bits 39-32 of the address, bit 21 is reserved.
866              * Leave bits 20-13 in place for setting accessed/dirty bits below.
867              */
868             pte = pde | ((pde & 0x1fe000LL) << (32 - 13));
869             rsvd_mask = 0x200000;
870             goto do_check_protect_pse36;
871         }
872
873         if (!(pde & PG_ACCESSED_MASK)) {
874             pde |= PG_ACCESSED_MASK;
875             x86_stl_phys_notdirty(cs, pde_addr, pde);
876         }
877
878         /* page directory entry */
879         pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) &
880             env->a20_mask;
881         pte = x86_ldl_phys(cs, pte_addr);
882         if (!(pte & PG_PRESENT_MASK)) {
883             goto do_fault;
884         }
885         /* combine pde and pte user and rw protections */
886         ptep &= pte | PG_NX_MASK;
887         page_size = 4096;
888         rsvd_mask = 0;
889     }
890
891 do_check_protect:
892     rsvd_mask |= (page_size - 1) & PG_ADDRESS_MASK & ~PG_PSE_PAT_MASK;
893 do_check_protect_pse36:
894     if (pte & rsvd_mask) {
895         goto do_fault_rsvd;
896     }
897     ptep ^= PG_NX_MASK;
898
899     /* can the page can be put in the TLB?  prot will tell us */
900     if (is_user && !(ptep & PG_USER_MASK)) {
901         goto do_fault_protect;
902     }
903
904     prot = 0;
905     if (mmu_idx != MMU_KSMAP_IDX || !(ptep & PG_USER_MASK)) {
906         prot |= PAGE_READ;
907         if ((ptep & PG_RW_MASK) || (!is_user && !(env->cr[0] & CR0_WP_MASK))) {
908             prot |= PAGE_WRITE;
909         }
910     }
911     if (!(ptep & PG_NX_MASK) &&
912         (mmu_idx == MMU_USER_IDX ||
913          !((env->cr[4] & CR4_SMEP_MASK) && (ptep & PG_USER_MASK)))) {
914         prot |= PAGE_EXEC;
915     }
916
917     if ((prot & (1 << is_write1)) == 0) {
918         goto do_fault_protect;
919     }
920
921     /* yes, it can! */
922     is_dirty = is_write && !(pte & PG_DIRTY_MASK);
923     if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
924         pte |= PG_ACCESSED_MASK;
925         if (is_dirty) {
926             pte |= PG_DIRTY_MASK;
927         }
928         x86_stl_phys_notdirty(cs, pte_addr, pte);
929     }
930
931     if (!(pte & PG_DIRTY_MASK)) {
932         /* only set write access if already dirty... otherwise wait
933            for dirty access */
934         assert(!is_write);
935         prot &= ~PAGE_WRITE;
936     }
937
938  do_mapping:
939     pte = pte & env->a20_mask;
940
941     /* align to page_size */
942     pte &= PG_ADDRESS_MASK & ~(page_size - 1);
943
944     /* Even if 4MB pages, we map only one 4KB page in the cache to
945        avoid filling it too fast */
946     vaddr = addr & TARGET_PAGE_MASK;
947     page_offset = vaddr & (page_size - 1);
948     paddr = pte + page_offset;
949
950     assert(prot & (1 << is_write1));
951     tlb_set_page_with_attrs(cs, vaddr, paddr, cpu_get_mem_attrs(env),
952                             prot, mmu_idx, page_size);
953     return 0;
954  do_fault_rsvd:
955     error_code |= PG_ERROR_RSVD_MASK;
956  do_fault_protect:
957     error_code |= PG_ERROR_P_MASK;
958  do_fault:
959     error_code |= (is_write << PG_ERROR_W_BIT);
960     if (is_user)
961         error_code |= PG_ERROR_U_MASK;
962     if (is_write1 == 2 &&
963         (((env->efer & MSR_EFER_NXE) &&
964           (env->cr[4] & CR4_PAE_MASK)) ||
965          (env->cr[4] & CR4_SMEP_MASK)))
966         error_code |= PG_ERROR_I_D_MASK;
967     if (env->intercept_exceptions & (1 << EXCP0E_PAGE)) {
968         /* cr2 is not modified in case of exceptions */
969         x86_stq_phys(cs,
970                  env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2),
971                  addr);
972     } else {
973         env->cr[2] = addr;
974     }
975     env->error_code = error_code;
976     cs->exception_index = EXCP0E_PAGE;
977     return 1;
978 }
979
980 hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
981 {
982     X86CPU *cpu = X86_CPU(cs);
983     CPUX86State *env = &cpu->env;
984     target_ulong pde_addr, pte_addr;
985     uint64_t pte;
986     uint32_t page_offset;
987     int page_size;
988
989     if (!(env->cr[0] & CR0_PG_MASK)) {
990         pte = addr & env->a20_mask;
991         page_size = 4096;
992     } else if (env->cr[4] & CR4_PAE_MASK) {
993         target_ulong pdpe_addr;
994         uint64_t pde, pdpe;
995
996 #ifdef TARGET_X86_64
997         if (env->hflags & HF_LMA_MASK) {
998             uint64_t pml4e_addr, pml4e;
999             int32_t sext;
1000
1001             /* test virtual address sign extension */
1002             sext = (int64_t)addr >> 47;
1003             if (sext != 0 && sext != -1) {
1004                 return -1;
1005             }
1006             pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
1007                 env->a20_mask;
1008             pml4e = x86_ldq_phys(cs, pml4e_addr);
1009             if (!(pml4e & PG_PRESENT_MASK)) {
1010                 return -1;
1011             }
1012             pdpe_addr = ((pml4e & PG_ADDRESS_MASK) +
1013                          (((addr >> 30) & 0x1ff) << 3)) & env->a20_mask;
1014             pdpe = x86_ldq_phys(cs, pdpe_addr);
1015             if (!(pdpe & PG_PRESENT_MASK)) {
1016                 return -1;
1017             }
1018             if (pdpe & PG_PSE_MASK) {
1019                 page_size = 1024 * 1024 * 1024;
1020                 pte = pdpe;
1021                 goto out;
1022             }
1023
1024         } else
1025 #endif
1026         {
1027             pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
1028                 env->a20_mask;
1029             pdpe = x86_ldq_phys(cs, pdpe_addr);
1030             if (!(pdpe & PG_PRESENT_MASK))
1031                 return -1;
1032         }
1033
1034         pde_addr = ((pdpe & PG_ADDRESS_MASK) +
1035                     (((addr >> 21) & 0x1ff) << 3)) & env->a20_mask;
1036         pde = x86_ldq_phys(cs, pde_addr);
1037         if (!(pde & PG_PRESENT_MASK)) {
1038             return -1;
1039         }
1040         if (pde & PG_PSE_MASK) {
1041             /* 2 MB page */
1042             page_size = 2048 * 1024;
1043             pte = pde;
1044         } else {
1045             /* 4 KB page */
1046             pte_addr = ((pde & PG_ADDRESS_MASK) +
1047                         (((addr >> 12) & 0x1ff) << 3)) & env->a20_mask;
1048             page_size = 4096;
1049             pte = x86_ldq_phys(cs, pte_addr);
1050         }
1051         if (!(pte & PG_PRESENT_MASK)) {
1052             return -1;
1053         }
1054     } else {
1055         uint32_t pde;
1056
1057         /* page directory entry */
1058         pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & env->a20_mask;
1059         pde = x86_ldl_phys(cs, pde_addr);
1060         if (!(pde & PG_PRESENT_MASK))
1061             return -1;
1062         if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1063             pte = pde | ((pde & 0x1fe000LL) << (32 - 13));
1064             page_size = 4096 * 1024;
1065         } else {
1066             /* page directory entry */
1067             pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & env->a20_mask;
1068             pte = x86_ldl_phys(cs, pte_addr);
1069             if (!(pte & PG_PRESENT_MASK)) {
1070                 return -1;
1071             }
1072             page_size = 4096;
1073         }
1074         pte = pte & env->a20_mask;
1075     }
1076
1077 #ifdef TARGET_X86_64
1078 out:
1079 #endif
1080     pte &= PG_ADDRESS_MASK & ~(page_size - 1);
1081     page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
1082     return pte | page_offset;
1083 }
1084
1085 typedef struct MCEInjectionParams {
1086     Monitor *mon;
1087     X86CPU *cpu;
1088     int bank;
1089     uint64_t status;
1090     uint64_t mcg_status;
1091     uint64_t addr;
1092     uint64_t misc;
1093     int flags;
1094 } MCEInjectionParams;
1095
1096 static void do_inject_x86_mce(void *data)
1097 {
1098     MCEInjectionParams *params = data;
1099     CPUX86State *cenv = &params->cpu->env;
1100     CPUState *cpu = CPU(params->cpu);
1101     uint64_t *banks = cenv->mce_banks + 4 * params->bank;
1102
1103     cpu_synchronize_state(cpu);
1104
1105     /*
1106      * If there is an MCE exception being processed, ignore this SRAO MCE
1107      * unless unconditional injection was requested.
1108      */
1109     if (!(params->flags & MCE_INJECT_UNCOND_AO)
1110         && !(params->status & MCI_STATUS_AR)
1111         && (cenv->mcg_status & MCG_STATUS_MCIP)) {
1112         return;
1113     }
1114
1115     if (params->status & MCI_STATUS_UC) {
1116         /*
1117          * if MSR_MCG_CTL is not all 1s, the uncorrected error
1118          * reporting is disabled
1119          */
1120         if ((cenv->mcg_cap & MCG_CTL_P) && cenv->mcg_ctl != ~(uint64_t)0) {
1121             monitor_printf(params->mon,
1122                            "CPU %d: Uncorrected error reporting disabled\n",
1123                            cpu->cpu_index);
1124             return;
1125         }
1126
1127         /*
1128          * if MSR_MCi_CTL is not all 1s, the uncorrected error
1129          * reporting is disabled for the bank
1130          */
1131         if (banks[0] != ~(uint64_t)0) {
1132             monitor_printf(params->mon,
1133                            "CPU %d: Uncorrected error reporting disabled for"
1134                            " bank %d\n",
1135                            cpu->cpu_index, params->bank);
1136             return;
1137         }
1138
1139         if ((cenv->mcg_status & MCG_STATUS_MCIP) ||
1140             !(cenv->cr[4] & CR4_MCE_MASK)) {
1141             monitor_printf(params->mon,
1142                            "CPU %d: Previous MCE still in progress, raising"
1143                            " triple fault\n",
1144                            cpu->cpu_index);
1145             qemu_log_mask(CPU_LOG_RESET, "Triple fault\n");
1146             qemu_system_reset_request();
1147             return;
1148         }
1149         if (banks[1] & MCI_STATUS_VAL) {
1150             params->status |= MCI_STATUS_OVER;
1151         }
1152         banks[2] = params->addr;
1153         banks[3] = params->misc;
1154         cenv->mcg_status = params->mcg_status;
1155         banks[1] = params->status;
1156         cpu_interrupt(cpu, CPU_INTERRUPT_MCE);
1157     } else if (!(banks[1] & MCI_STATUS_VAL)
1158                || !(banks[1] & MCI_STATUS_UC)) {
1159         if (banks[1] & MCI_STATUS_VAL) {
1160             params->status |= MCI_STATUS_OVER;
1161         }
1162         banks[2] = params->addr;
1163         banks[3] = params->misc;
1164         banks[1] = params->status;
1165     } else {
1166         banks[1] |= MCI_STATUS_OVER;
1167     }
1168 }
1169
1170 void cpu_x86_inject_mce(Monitor *mon, X86CPU *cpu, int bank,
1171                         uint64_t status, uint64_t mcg_status, uint64_t addr,
1172                         uint64_t misc, int flags)
1173 {
1174     CPUState *cs = CPU(cpu);
1175     CPUX86State *cenv = &cpu->env;
1176     MCEInjectionParams params = {
1177         .mon = mon,
1178         .cpu = cpu,
1179         .bank = bank,
1180         .status = status,
1181         .mcg_status = mcg_status,
1182         .addr = addr,
1183         .misc = misc,
1184         .flags = flags,
1185     };
1186     unsigned bank_num = cenv->mcg_cap & 0xff;
1187
1188     if (!cenv->mcg_cap) {
1189         monitor_printf(mon, "MCE injection not supported\n");
1190         return;
1191     }
1192     if (bank >= bank_num) {
1193         monitor_printf(mon, "Invalid MCE bank number\n");
1194         return;
1195     }
1196     if (!(status & MCI_STATUS_VAL)) {
1197         monitor_printf(mon, "Invalid MCE status code\n");
1198         return;
1199     }
1200     if ((flags & MCE_INJECT_BROADCAST)
1201         && !cpu_x86_support_mca_broadcast(cenv)) {
1202         monitor_printf(mon, "Guest CPU does not support MCA broadcast\n");
1203         return;
1204     }
1205
1206     run_on_cpu(cs, do_inject_x86_mce, &params);
1207     if (flags & MCE_INJECT_BROADCAST) {
1208         CPUState *other_cs;
1209
1210         params.bank = 1;
1211         params.status = MCI_STATUS_VAL | MCI_STATUS_UC;
1212         params.mcg_status = MCG_STATUS_MCIP | MCG_STATUS_RIPV;
1213         params.addr = 0;
1214         params.misc = 0;
1215         CPU_FOREACH(other_cs) {
1216             if (other_cs == cs) {
1217                 continue;
1218             }
1219             params.cpu = X86_CPU(other_cs);
1220             run_on_cpu(other_cs, do_inject_x86_mce, &params);
1221         }
1222     }
1223 }
1224
1225 void cpu_report_tpr_access(CPUX86State *env, TPRAccess access)
1226 {
1227     X86CPU *cpu = x86_env_get_cpu(env);
1228     CPUState *cs = CPU(cpu);
1229
1230     if (kvm_enabled()) {
1231         env->tpr_access_type = access;
1232
1233         cpu_interrupt(cs, CPU_INTERRUPT_TPR);
1234     } else {
1235         cpu_restore_state(cs, cs->mem_io_pc);
1236
1237         apic_handle_tpr_access_report(cpu->apic_state, env->eip, access);
1238     }
1239 }
1240 #endif /* !CONFIG_USER_ONLY */
1241
1242 int cpu_x86_get_descr_debug(CPUX86State *env, unsigned int selector,
1243                             target_ulong *base, unsigned int *limit,
1244                             unsigned int *flags)
1245 {
1246     X86CPU *cpu = x86_env_get_cpu(env);
1247     CPUState *cs = CPU(cpu);
1248     SegmentCache *dt;
1249     target_ulong ptr;
1250     uint32_t e1, e2;
1251     int index;
1252
1253     if (selector & 0x4)
1254         dt = &env->ldt;
1255     else
1256         dt = &env->gdt;
1257     index = selector & ~7;
1258     ptr = dt->base + index;
1259     if ((index + 7) > dt->limit
1260         || cpu_memory_rw_debug(cs, ptr, (uint8_t *)&e1, sizeof(e1), 0) != 0
1261         || cpu_memory_rw_debug(cs, ptr+4, (uint8_t *)&e2, sizeof(e2), 0) != 0)
1262         return 0;
1263
1264     *base = ((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
1265     *limit = (e1 & 0xffff) | (e2 & 0x000f0000);
1266     if (e2 & DESC_G_MASK)
1267         *limit = (*limit << 12) | 0xfff;
1268     *flags = e2;
1269
1270     return 1;
1271 }
1272
1273 #if !defined(CONFIG_USER_ONLY)
1274 void do_cpu_init(X86CPU *cpu)
1275 {
1276     CPUState *cs = CPU(cpu);
1277     CPUX86State *env = &cpu->env;
1278     CPUX86State *save = g_new(CPUX86State, 1);
1279     int sipi = cs->interrupt_request & CPU_INTERRUPT_SIPI;
1280
1281     *save = *env;
1282
1283     cpu_reset(cs);
1284     cs->interrupt_request = sipi;
1285     memcpy(&env->start_init_save, &save->start_init_save,
1286            offsetof(CPUX86State, end_init_save) -
1287            offsetof(CPUX86State, start_init_save));
1288     g_free(save);
1289
1290     if (kvm_enabled()) {
1291         kvm_arch_do_init_vcpu(cpu);
1292     }
1293     apic_init_reset(cpu->apic_state);
1294 }
1295
1296 void do_cpu_sipi(X86CPU *cpu)
1297 {
1298     apic_sipi(cpu->apic_state);
1299 }
1300 #else
1301 void do_cpu_init(X86CPU *cpu)
1302 {
1303 }
1304 void do_cpu_sipi(X86CPU *cpu)
1305 {
1306 }
1307 #endif
1308
1309 /* Frob eflags into and out of the CPU temporary format.  */
1310
1311 void x86_cpu_exec_enter(CPUState *cs)
1312 {
1313     X86CPU *cpu = X86_CPU(cs);
1314     CPUX86State *env = &cpu->env;
1315
1316     CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1317     env->df = 1 - (2 * ((env->eflags >> 10) & 1));
1318     CC_OP = CC_OP_EFLAGS;
1319     env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1320 }
1321
1322 void x86_cpu_exec_exit(CPUState *cs)
1323 {
1324     X86CPU *cpu = X86_CPU(cs);
1325     CPUX86State *env = &cpu->env;
1326
1327     env->eflags = cpu_compute_eflags(env);
1328 }
1329
1330 #ifndef CONFIG_USER_ONLY
1331 uint8_t x86_ldub_phys(CPUState *cs, hwaddr addr)
1332 {
1333     X86CPU *cpu = X86_CPU(cs);
1334     CPUX86State *env = &cpu->env;
1335
1336     return address_space_ldub(cs->as, addr,
1337                               cpu_get_mem_attrs(env),
1338                               NULL);
1339 }
1340
1341 uint32_t x86_lduw_phys(CPUState *cs, hwaddr addr)
1342 {
1343     X86CPU *cpu = X86_CPU(cs);
1344     CPUX86State *env = &cpu->env;
1345
1346     return address_space_lduw(cs->as, addr,
1347                               cpu_get_mem_attrs(env),
1348                               NULL);
1349 }
1350
1351 uint32_t x86_ldl_phys(CPUState *cs, hwaddr addr)
1352 {
1353     X86CPU *cpu = X86_CPU(cs);
1354     CPUX86State *env = &cpu->env;
1355
1356     return address_space_ldl(cs->as, addr,
1357                              cpu_get_mem_attrs(env),
1358                              NULL);
1359 }
1360
1361 uint64_t x86_ldq_phys(CPUState *cs, hwaddr addr)
1362 {
1363     X86CPU *cpu = X86_CPU(cs);
1364     CPUX86State *env = &cpu->env;
1365
1366     return address_space_ldq(cs->as, addr,
1367                              cpu_get_mem_attrs(env),
1368                              NULL);
1369 }
1370
1371 void x86_stb_phys(CPUState *cs, hwaddr addr, uint8_t val)
1372 {
1373     X86CPU *cpu = X86_CPU(cs);
1374     CPUX86State *env = &cpu->env;
1375
1376     address_space_stb(cs->as, addr, val,
1377                       cpu_get_mem_attrs(env),
1378                       NULL);
1379 }
1380
1381 void x86_stl_phys_notdirty(CPUState *cs, hwaddr addr, uint32_t val)
1382 {
1383     X86CPU *cpu = X86_CPU(cs);
1384     CPUX86State *env = &cpu->env;
1385
1386     address_space_stl_notdirty(cs->as, addr, val,
1387                                cpu_get_mem_attrs(env),
1388                                NULL);
1389 }
1390
1391 void x86_stw_phys(CPUState *cs, hwaddr addr, uint32_t val)
1392 {
1393     X86CPU *cpu = X86_CPU(cs);
1394     CPUX86State *env = &cpu->env;
1395
1396     address_space_stw(cs->as, addr, val,
1397                       cpu_get_mem_attrs(env),
1398                       NULL);
1399 }
1400
1401 void x86_stl_phys(CPUState *cs, hwaddr addr, uint32_t val)
1402 {
1403     X86CPU *cpu = X86_CPU(cs);
1404     CPUX86State *env = &cpu->env;
1405
1406     address_space_stl(cs->as, addr, val,
1407                       cpu_get_mem_attrs(env),
1408                       NULL);
1409 }
1410
1411 void x86_stq_phys(CPUState *cs, hwaddr addr, uint64_t val)
1412 {
1413     X86CPU *cpu = X86_CPU(cs);
1414     CPUX86State *env = &cpu->env;
1415
1416     address_space_stq(cs->as, addr, val,
1417                       cpu_get_mem_attrs(env),
1418                       NULL);
1419 }
1420 #endif
This page took 0.10716 seconds and 4 git commands to generate.