2 * i386 helpers (without register variable usage)
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
28 #include "qemu-common.h"
35 /* NOTE: must be called outside the CPU execute loop */
36 void cpu_reset(CPUX86State
*env
)
40 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
41 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
42 log_cpu_state(env
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
45 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
49 env
->old_exception
= -1;
51 /* init to reset state */
54 env
->hflags
|= HF_SOFTMMU_MASK
;
56 env
->hflags2
|= HF2_GIF_MASK
;
58 cpu_x86_update_cr0(env
, 0x60000010);
60 env
->smbase
= 0x30000;
62 env
->idt
.limit
= 0xffff;
63 env
->gdt
.limit
= 0xffff;
64 env
->ldt
.limit
= 0xffff;
65 env
->ldt
.flags
= DESC_P_MASK
| (2 << DESC_TYPE_SHIFT
);
66 env
->tr
.limit
= 0xffff;
67 env
->tr
.flags
= DESC_P_MASK
| (11 << DESC_TYPE_SHIFT
);
69 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff,
70 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
|
71 DESC_R_MASK
| DESC_A_MASK
);
72 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff,
73 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
75 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff,
76 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
78 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff,
79 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
81 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff,
82 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
84 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff,
85 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
89 env
->regs
[R_EDX
] = env
->cpuid_version
;
100 memset(env
->dr
, 0, sizeof(env
->dr
));
101 env
->dr
[6] = DR6_FIXED_1
;
102 env
->dr
[7] = DR7_FIXED_1
;
103 cpu_breakpoint_remove_all(env
, BP_CPU
);
104 cpu_watchpoint_remove_all(env
, BP_CPU
);
109 void cpu_x86_close(CPUX86State
*env
)
114 /***********************************************************/
117 static const char *cc_op_str
[] = {
173 cpu_x86_dump_seg_cache(CPUState
*env
, FILE *f
,
174 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
175 const char *name
, struct SegmentCache
*sc
)
178 if (env
->hflags
& HF_CS64_MASK
) {
179 cpu_fprintf(f
, "%-3s=%04x %016" PRIx64
" %08x %08x", name
,
180 sc
->selector
, sc
->base
, sc
->limit
, sc
->flags
);
184 cpu_fprintf(f
, "%-3s=%04x %08x %08x %08x", name
, sc
->selector
,
185 (uint32_t)sc
->base
, sc
->limit
, sc
->flags
);
188 if (!(env
->hflags
& HF_PE_MASK
) || !(sc
->flags
& DESC_P_MASK
))
191 cpu_fprintf(f
, " DPL=%d ", (sc
->flags
& DESC_DPL_MASK
) >> DESC_DPL_SHIFT
);
192 if (sc
->flags
& DESC_S_MASK
) {
193 if (sc
->flags
& DESC_CS_MASK
) {
194 cpu_fprintf(f
, (sc
->flags
& DESC_L_MASK
) ? "CS64" :
195 ((sc
->flags
& DESC_B_MASK
) ? "CS32" : "CS16"));
196 cpu_fprintf(f
, " [%c%c", (sc
->flags
& DESC_C_MASK
) ? 'C' : '-',
197 (sc
->flags
& DESC_R_MASK
) ? 'R' : '-');
199 cpu_fprintf(f
, (sc
->flags
& DESC_B_MASK
) ? "DS " : "DS16");
200 cpu_fprintf(f
, " [%c%c", (sc
->flags
& DESC_E_MASK
) ? 'E' : '-',
201 (sc
->flags
& DESC_W_MASK
) ? 'W' : '-');
203 cpu_fprintf(f
, "%c]", (sc
->flags
& DESC_A_MASK
) ? 'A' : '-');
205 static const char *sys_type_name
[2][16] = {
207 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
208 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
209 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
210 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
213 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
214 "Reserved", "Reserved", "Reserved", "Reserved",
215 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
216 "Reserved", "IntGate64", "TrapGate64"
220 sys_type_name
[(env
->hflags
& HF_LMA_MASK
) ? 1 : 0]
221 [(sc
->flags
& DESC_TYPE_MASK
)
222 >> DESC_TYPE_SHIFT
]);
225 cpu_fprintf(f
, "\n");
228 void cpu_dump_state(CPUState
*env
, FILE *f
,
229 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
234 static const char *seg_name
[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
236 cpu_synchronize_state(env
);
238 eflags
= env
->eflags
;
240 if (env
->hflags
& HF_CS64_MASK
) {
242 "RAX=%016" PRIx64
" RBX=%016" PRIx64
" RCX=%016" PRIx64
" RDX=%016" PRIx64
"\n"
243 "RSI=%016" PRIx64
" RDI=%016" PRIx64
" RBP=%016" PRIx64
" RSP=%016" PRIx64
"\n"
244 "R8 =%016" PRIx64
" R9 =%016" PRIx64
" R10=%016" PRIx64
" R11=%016" PRIx64
"\n"
245 "R12=%016" PRIx64
" R13=%016" PRIx64
" R14=%016" PRIx64
" R15=%016" PRIx64
"\n"
246 "RIP=%016" PRIx64
" RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
264 eflags
& DF_MASK
? 'D' : '-',
265 eflags
& CC_O
? 'O' : '-',
266 eflags
& CC_S
? 'S' : '-',
267 eflags
& CC_Z
? 'Z' : '-',
268 eflags
& CC_A
? 'A' : '-',
269 eflags
& CC_P
? 'P' : '-',
270 eflags
& CC_C
? 'C' : '-',
271 env
->hflags
& HF_CPL_MASK
,
272 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
273 (env
->a20_mask
>> 20) & 1,
274 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
279 cpu_fprintf(f
, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
280 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
281 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
282 (uint32_t)env
->regs
[R_EAX
],
283 (uint32_t)env
->regs
[R_EBX
],
284 (uint32_t)env
->regs
[R_ECX
],
285 (uint32_t)env
->regs
[R_EDX
],
286 (uint32_t)env
->regs
[R_ESI
],
287 (uint32_t)env
->regs
[R_EDI
],
288 (uint32_t)env
->regs
[R_EBP
],
289 (uint32_t)env
->regs
[R_ESP
],
290 (uint32_t)env
->eip
, eflags
,
291 eflags
& DF_MASK
? 'D' : '-',
292 eflags
& CC_O
? 'O' : '-',
293 eflags
& CC_S
? 'S' : '-',
294 eflags
& CC_Z
? 'Z' : '-',
295 eflags
& CC_A
? 'A' : '-',
296 eflags
& CC_P
? 'P' : '-',
297 eflags
& CC_C
? 'C' : '-',
298 env
->hflags
& HF_CPL_MASK
,
299 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
300 (env
->a20_mask
>> 20) & 1,
301 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
305 for(i
= 0; i
< 6; i
++) {
306 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, seg_name
[i
],
309 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, "LDT", &env
->ldt
);
310 cpu_x86_dump_seg_cache(env
, f
, cpu_fprintf
, "TR", &env
->tr
);
313 if (env
->hflags
& HF_LMA_MASK
) {
314 cpu_fprintf(f
, "GDT= %016" PRIx64
" %08x\n",
315 env
->gdt
.base
, env
->gdt
.limit
);
316 cpu_fprintf(f
, "IDT= %016" PRIx64
" %08x\n",
317 env
->idt
.base
, env
->idt
.limit
);
318 cpu_fprintf(f
, "CR0=%08x CR2=%016" PRIx64
" CR3=%016" PRIx64
" CR4=%08x\n",
319 (uint32_t)env
->cr
[0],
322 (uint32_t)env
->cr
[4]);
323 for(i
= 0; i
< 4; i
++)
324 cpu_fprintf(f
, "DR%d=%016" PRIx64
" ", i
, env
->dr
[i
]);
325 cpu_fprintf(f
, "\nDR6=%016" PRIx64
" DR7=%016" PRIx64
"\n",
326 env
->dr
[6], env
->dr
[7]);
330 cpu_fprintf(f
, "GDT= %08x %08x\n",
331 (uint32_t)env
->gdt
.base
, env
->gdt
.limit
);
332 cpu_fprintf(f
, "IDT= %08x %08x\n",
333 (uint32_t)env
->idt
.base
, env
->idt
.limit
);
334 cpu_fprintf(f
, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
335 (uint32_t)env
->cr
[0],
336 (uint32_t)env
->cr
[2],
337 (uint32_t)env
->cr
[3],
338 (uint32_t)env
->cr
[4]);
339 for(i
= 0; i
< 4; i
++)
340 cpu_fprintf(f
, "DR%d=%08x ", i
, env
->dr
[i
]);
341 cpu_fprintf(f
, "\nDR6=%08x DR7=%08x\n", env
->dr
[6], env
->dr
[7]);
343 if (flags
& X86_DUMP_CCOP
) {
344 if ((unsigned)env
->cc_op
< CC_OP_NB
)
345 snprintf(cc_op_name
, sizeof(cc_op_name
), "%s", cc_op_str
[env
->cc_op
]);
347 snprintf(cc_op_name
, sizeof(cc_op_name
), "[%d]", env
->cc_op
);
349 if (env
->hflags
& HF_CS64_MASK
) {
350 cpu_fprintf(f
, "CCS=%016" PRIx64
" CCD=%016" PRIx64
" CCO=%-8s\n",
351 env
->cc_src
, env
->cc_dst
,
356 cpu_fprintf(f
, "CCS=%08x CCD=%08x CCO=%-8s\n",
357 (uint32_t)env
->cc_src
, (uint32_t)env
->cc_dst
,
361 cpu_fprintf(f
, "EFER=%016" PRIx64
"\n", env
->efer
);
362 if (flags
& X86_DUMP_FPU
) {
365 for(i
= 0; i
< 8; i
++) {
366 fptag
|= ((!env
->fptags
[i
]) << i
);
368 cpu_fprintf(f
, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
370 (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11,
375 #if defined(USE_X86LDOUBLE)
383 tmp
.d
= env
->fpregs
[i
].d
;
384 cpu_fprintf(f
, "FPR%d=%016" PRIx64
" %04x",
385 i
, tmp
.l
.lower
, tmp
.l
.upper
);
387 cpu_fprintf(f
, "FPR%d=%016" PRIx64
,
388 i
, env
->fpregs
[i
].mmx
.q
);
391 cpu_fprintf(f
, "\n");
395 if (env
->hflags
& HF_CS64_MASK
)
400 cpu_fprintf(f
, "XMM%02d=%08x%08x%08x%08x",
402 env
->xmm_regs
[i
].XMM_L(3),
403 env
->xmm_regs
[i
].XMM_L(2),
404 env
->xmm_regs
[i
].XMM_L(1),
405 env
->xmm_regs
[i
].XMM_L(0));
407 cpu_fprintf(f
, "\n");
414 /***********************************************************/
416 /* XXX: add PGE support */
418 void cpu_x86_set_a20(CPUX86State
*env
, int a20_state
)
420 a20_state
= (a20_state
!= 0);
421 if (a20_state
!= ((env
->a20_mask
>> 20) & 1)) {
422 #if defined(DEBUG_MMU)
423 printf("A20 update: a20=%d\n", a20_state
);
425 /* if the cpu is currently executing code, we must unlink it and
426 all the potentially executing TB */
427 cpu_interrupt(env
, CPU_INTERRUPT_EXITTB
);
429 /* when a20 is changed, all the MMU mappings are invalid, so
430 we must flush everything */
432 env
->a20_mask
= ~(1 << 20) | (a20_state
<< 20);
436 void cpu_x86_update_cr0(CPUX86State
*env
, uint32_t new_cr0
)
440 #if defined(DEBUG_MMU)
441 printf("CR0 update: CR0=0x%08x\n", new_cr0
);
443 if ((new_cr0
& (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
)) !=
444 (env
->cr
[0] & (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
))) {
449 if (!(env
->cr
[0] & CR0_PG_MASK
) && (new_cr0
& CR0_PG_MASK
) &&
450 (env
->efer
& MSR_EFER_LME
)) {
451 /* enter in long mode */
452 /* XXX: generate an exception */
453 if (!(env
->cr
[4] & CR4_PAE_MASK
))
455 env
->efer
|= MSR_EFER_LMA
;
456 env
->hflags
|= HF_LMA_MASK
;
457 } else if ((env
->cr
[0] & CR0_PG_MASK
) && !(new_cr0
& CR0_PG_MASK
) &&
458 (env
->efer
& MSR_EFER_LMA
)) {
460 env
->efer
&= ~MSR_EFER_LMA
;
461 env
->hflags
&= ~(HF_LMA_MASK
| HF_CS64_MASK
);
462 env
->eip
&= 0xffffffff;
465 env
->cr
[0] = new_cr0
| CR0_ET_MASK
;
467 /* update PE flag in hidden flags */
468 pe_state
= (env
->cr
[0] & CR0_PE_MASK
);
469 env
->hflags
= (env
->hflags
& ~HF_PE_MASK
) | (pe_state
<< HF_PE_SHIFT
);
470 /* ensure that ADDSEG is always set in real mode */
471 env
->hflags
|= ((pe_state
^ 1) << HF_ADDSEG_SHIFT
);
472 /* update FPU flags */
473 env
->hflags
= (env
->hflags
& ~(HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
)) |
474 ((new_cr0
<< (HF_MP_SHIFT
- 1)) & (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
));
477 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
479 void cpu_x86_update_cr3(CPUX86State
*env
, target_ulong new_cr3
)
481 env
->cr
[3] = new_cr3
;
482 if (env
->cr
[0] & CR0_PG_MASK
) {
483 #if defined(DEBUG_MMU)
484 printf("CR3 update: CR3=" TARGET_FMT_lx
"\n", new_cr3
);
490 void cpu_x86_update_cr4(CPUX86State
*env
, uint32_t new_cr4
)
492 #if defined(DEBUG_MMU)
493 printf("CR4 update: CR4=%08x\n", (uint32_t)env
->cr
[4]);
495 if ((new_cr4
& (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
)) !=
496 (env
->cr
[4] & (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
))) {
500 if (!(env
->cpuid_features
& CPUID_SSE
))
501 new_cr4
&= ~CR4_OSFXSR_MASK
;
502 if (new_cr4
& CR4_OSFXSR_MASK
)
503 env
->hflags
|= HF_OSFXSR_MASK
;
505 env
->hflags
&= ~HF_OSFXSR_MASK
;
507 env
->cr
[4] = new_cr4
;
510 #if defined(CONFIG_USER_ONLY)
512 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
513 int is_write
, int mmu_idx
, int is_softmmu
)
515 /* user mode only emulation */
518 env
->error_code
= (is_write
<< PG_ERROR_W_BIT
);
519 env
->error_code
|= PG_ERROR_U_MASK
;
520 env
->exception_index
= EXCP0E_PAGE
;
526 /* XXX: This value should match the one returned by CPUID
528 # if defined(TARGET_X86_64)
529 # define PHYS_ADDR_MASK 0xfffffff000LL
531 # define PHYS_ADDR_MASK 0xffffff000LL
535 -1 = cannot handle fault
536 0 = nothing more to do
537 1 = generate PF fault
539 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
540 int is_write1
, int mmu_idx
, int is_softmmu
)
543 target_ulong pde_addr
, pte_addr
;
544 int error_code
, is_dirty
, prot
, page_size
, is_write
, is_user
;
545 target_phys_addr_t paddr
;
546 uint32_t page_offset
;
547 target_ulong vaddr
, virt_addr
;
549 is_user
= mmu_idx
== MMU_USER_IDX
;
550 #if defined(DEBUG_MMU)
551 printf("MMU fault: addr=" TARGET_FMT_lx
" w=%d u=%d eip=" TARGET_FMT_lx
"\n",
552 addr
, is_write1
, is_user
, env
->eip
);
554 is_write
= is_write1
& 1;
556 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
558 virt_addr
= addr
& TARGET_PAGE_MASK
;
559 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
564 if (env
->cr
[4] & CR4_PAE_MASK
) {
566 target_ulong pdpe_addr
;
569 if (env
->hflags
& HF_LMA_MASK
) {
570 uint64_t pml4e_addr
, pml4e
;
573 /* test virtual address sign extension */
574 sext
= (int64_t)addr
>> 47;
575 if (sext
!= 0 && sext
!= -1) {
577 env
->exception_index
= EXCP0D_GPF
;
581 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
583 pml4e
= ldq_phys(pml4e_addr
);
584 if (!(pml4e
& PG_PRESENT_MASK
)) {
588 if (!(env
->efer
& MSR_EFER_NXE
) && (pml4e
& PG_NX_MASK
)) {
589 error_code
= PG_ERROR_RSVD_MASK
;
592 if (!(pml4e
& PG_ACCESSED_MASK
)) {
593 pml4e
|= PG_ACCESSED_MASK
;
594 stl_phys_notdirty(pml4e_addr
, pml4e
);
596 ptep
= pml4e
^ PG_NX_MASK
;
597 pdpe_addr
= ((pml4e
& PHYS_ADDR_MASK
) + (((addr
>> 30) & 0x1ff) << 3)) &
599 pdpe
= ldq_phys(pdpe_addr
);
600 if (!(pdpe
& PG_PRESENT_MASK
)) {
604 if (!(env
->efer
& MSR_EFER_NXE
) && (pdpe
& PG_NX_MASK
)) {
605 error_code
= PG_ERROR_RSVD_MASK
;
608 ptep
&= pdpe
^ PG_NX_MASK
;
609 if (!(pdpe
& PG_ACCESSED_MASK
)) {
610 pdpe
|= PG_ACCESSED_MASK
;
611 stl_phys_notdirty(pdpe_addr
, pdpe
);
616 /* XXX: load them when cr3 is loaded ? */
617 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
619 pdpe
= ldq_phys(pdpe_addr
);
620 if (!(pdpe
& PG_PRESENT_MASK
)) {
624 ptep
= PG_NX_MASK
| PG_USER_MASK
| PG_RW_MASK
;
627 pde_addr
= ((pdpe
& PHYS_ADDR_MASK
) + (((addr
>> 21) & 0x1ff) << 3)) &
629 pde
= ldq_phys(pde_addr
);
630 if (!(pde
& PG_PRESENT_MASK
)) {
634 if (!(env
->efer
& MSR_EFER_NXE
) && (pde
& PG_NX_MASK
)) {
635 error_code
= PG_ERROR_RSVD_MASK
;
638 ptep
&= pde
^ PG_NX_MASK
;
639 if (pde
& PG_PSE_MASK
) {
641 page_size
= 2048 * 1024;
643 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
644 goto do_fault_protect
;
646 if (!(ptep
& PG_USER_MASK
))
647 goto do_fault_protect
;
648 if (is_write
&& !(ptep
& PG_RW_MASK
))
649 goto do_fault_protect
;
651 if ((env
->cr
[0] & CR0_WP_MASK
) &&
652 is_write
&& !(ptep
& PG_RW_MASK
))
653 goto do_fault_protect
;
655 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
656 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
657 pde
|= PG_ACCESSED_MASK
;
659 pde
|= PG_DIRTY_MASK
;
660 stl_phys_notdirty(pde_addr
, pde
);
662 /* align to page_size */
663 pte
= pde
& ((PHYS_ADDR_MASK
& ~(page_size
- 1)) | 0xfff);
664 virt_addr
= addr
& ~(page_size
- 1);
667 if (!(pde
& PG_ACCESSED_MASK
)) {
668 pde
|= PG_ACCESSED_MASK
;
669 stl_phys_notdirty(pde_addr
, pde
);
671 pte_addr
= ((pde
& PHYS_ADDR_MASK
) + (((addr
>> 12) & 0x1ff) << 3)) &
673 pte
= ldq_phys(pte_addr
);
674 if (!(pte
& PG_PRESENT_MASK
)) {
678 if (!(env
->efer
& MSR_EFER_NXE
) && (pte
& PG_NX_MASK
)) {
679 error_code
= PG_ERROR_RSVD_MASK
;
682 /* combine pde and pte nx, user and rw protections */
683 ptep
&= pte
^ PG_NX_MASK
;
685 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
686 goto do_fault_protect
;
688 if (!(ptep
& PG_USER_MASK
))
689 goto do_fault_protect
;
690 if (is_write
&& !(ptep
& PG_RW_MASK
))
691 goto do_fault_protect
;
693 if ((env
->cr
[0] & CR0_WP_MASK
) &&
694 is_write
&& !(ptep
& PG_RW_MASK
))
695 goto do_fault_protect
;
697 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
698 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
699 pte
|= PG_ACCESSED_MASK
;
701 pte
|= PG_DIRTY_MASK
;
702 stl_phys_notdirty(pte_addr
, pte
);
705 virt_addr
= addr
& ~0xfff;
706 pte
= pte
& (PHYS_ADDR_MASK
| 0xfff);
711 /* page directory entry */
712 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) &
714 pde
= ldl_phys(pde_addr
);
715 if (!(pde
& PG_PRESENT_MASK
)) {
719 /* if PSE bit is set, then we use a 4MB page */
720 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
721 page_size
= 4096 * 1024;
723 if (!(pde
& PG_USER_MASK
))
724 goto do_fault_protect
;
725 if (is_write
&& !(pde
& PG_RW_MASK
))
726 goto do_fault_protect
;
728 if ((env
->cr
[0] & CR0_WP_MASK
) &&
729 is_write
&& !(pde
& PG_RW_MASK
))
730 goto do_fault_protect
;
732 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
733 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
734 pde
|= PG_ACCESSED_MASK
;
736 pde
|= PG_DIRTY_MASK
;
737 stl_phys_notdirty(pde_addr
, pde
);
740 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
742 virt_addr
= addr
& ~(page_size
- 1);
744 if (!(pde
& PG_ACCESSED_MASK
)) {
745 pde
|= PG_ACCESSED_MASK
;
746 stl_phys_notdirty(pde_addr
, pde
);
749 /* page directory entry */
750 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) &
752 pte
= ldl_phys(pte_addr
);
753 if (!(pte
& PG_PRESENT_MASK
)) {
757 /* combine pde and pte user and rw protections */
760 if (!(ptep
& PG_USER_MASK
))
761 goto do_fault_protect
;
762 if (is_write
&& !(ptep
& PG_RW_MASK
))
763 goto do_fault_protect
;
765 if ((env
->cr
[0] & CR0_WP_MASK
) &&
766 is_write
&& !(ptep
& PG_RW_MASK
))
767 goto do_fault_protect
;
769 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
770 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
771 pte
|= PG_ACCESSED_MASK
;
773 pte
|= PG_DIRTY_MASK
;
774 stl_phys_notdirty(pte_addr
, pte
);
777 virt_addr
= addr
& ~0xfff;
780 /* the page can be put in the TLB */
782 if (!(ptep
& PG_NX_MASK
))
784 if (pte
& PG_DIRTY_MASK
) {
785 /* only set write access if already dirty... otherwise wait
788 if (ptep
& PG_RW_MASK
)
791 if (!(env
->cr
[0] & CR0_WP_MASK
) ||
797 pte
= pte
& env
->a20_mask
;
799 /* Even if 4MB pages, we map only one 4KB page in the cache to
800 avoid filling it too fast */
801 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
802 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
803 vaddr
= virt_addr
+ page_offset
;
805 tlb_set_page(env
, vaddr
, paddr
, prot
, mmu_idx
, page_size
);
808 error_code
= PG_ERROR_P_MASK
;
810 error_code
|= (is_write
<< PG_ERROR_W_BIT
);
812 error_code
|= PG_ERROR_U_MASK
;
813 if (is_write1
== 2 &&
814 (env
->efer
& MSR_EFER_NXE
) &&
815 (env
->cr
[4] & CR4_PAE_MASK
))
816 error_code
|= PG_ERROR_I_D_MASK
;
817 if (env
->intercept_exceptions
& (1 << EXCP0E_PAGE
)) {
818 /* cr2 is not modified in case of exceptions */
819 stq_phys(env
->vm_vmcb
+ offsetof(struct vmcb
, control
.exit_info_2
),
824 env
->error_code
= error_code
;
825 env
->exception_index
= EXCP0E_PAGE
;
829 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
831 target_ulong pde_addr
, pte_addr
;
833 target_phys_addr_t paddr
;
834 uint32_t page_offset
;
837 if (env
->cr
[4] & CR4_PAE_MASK
) {
838 target_ulong pdpe_addr
;
842 if (env
->hflags
& HF_LMA_MASK
) {
843 uint64_t pml4e_addr
, pml4e
;
846 /* test virtual address sign extension */
847 sext
= (int64_t)addr
>> 47;
848 if (sext
!= 0 && sext
!= -1)
851 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
853 pml4e
= ldq_phys(pml4e_addr
);
854 if (!(pml4e
& PG_PRESENT_MASK
))
857 pdpe_addr
= ((pml4e
& ~0xfff) + (((addr
>> 30) & 0x1ff) << 3)) &
859 pdpe
= ldq_phys(pdpe_addr
);
860 if (!(pdpe
& PG_PRESENT_MASK
))
865 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
867 pdpe
= ldq_phys(pdpe_addr
);
868 if (!(pdpe
& PG_PRESENT_MASK
))
872 pde_addr
= ((pdpe
& ~0xfff) + (((addr
>> 21) & 0x1ff) << 3)) &
874 pde
= ldq_phys(pde_addr
);
875 if (!(pde
& PG_PRESENT_MASK
)) {
878 if (pde
& PG_PSE_MASK
) {
880 page_size
= 2048 * 1024;
881 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
884 pte_addr
= ((pde
& ~0xfff) + (((addr
>> 12) & 0x1ff) << 3)) &
887 pte
= ldq_phys(pte_addr
);
889 if (!(pte
& PG_PRESENT_MASK
))
894 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
898 /* page directory entry */
899 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) & env
->a20_mask
;
900 pde
= ldl_phys(pde_addr
);
901 if (!(pde
& PG_PRESENT_MASK
))
903 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
904 pte
= pde
& ~0x003ff000; /* align to 4MB */
905 page_size
= 4096 * 1024;
907 /* page directory entry */
908 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) & env
->a20_mask
;
909 pte
= ldl_phys(pte_addr
);
910 if (!(pte
& PG_PRESENT_MASK
))
915 pte
= pte
& env
->a20_mask
;
918 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
919 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
923 void hw_breakpoint_insert(CPUState
*env
, int index
)
927 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
929 if (hw_breakpoint_enabled(env
->dr
[7], index
))
930 err
= cpu_breakpoint_insert(env
, env
->dr
[index
], BP_CPU
,
931 &env
->cpu_breakpoint
[index
]);
934 type
= BP_CPU
| BP_MEM_WRITE
;
937 /* No support for I/O watchpoints yet */
940 type
= BP_CPU
| BP_MEM_ACCESS
;
942 err
= cpu_watchpoint_insert(env
, env
->dr
[index
],
943 hw_breakpoint_len(env
->dr
[7], index
),
944 type
, &env
->cpu_watchpoint
[index
]);
948 env
->cpu_breakpoint
[index
] = NULL
;
951 void hw_breakpoint_remove(CPUState
*env
, int index
)
953 if (!env
->cpu_breakpoint
[index
])
955 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
957 if (hw_breakpoint_enabled(env
->dr
[7], index
))
958 cpu_breakpoint_remove_by_ref(env
, env
->cpu_breakpoint
[index
]);
962 cpu_watchpoint_remove_by_ref(env
, env
->cpu_watchpoint
[index
]);
965 /* No support for I/O watchpoints yet */
970 int check_hw_breakpoints(CPUState
*env
, int force_dr6_update
)
976 dr6
= env
->dr
[6] & ~0xf;
977 for (reg
= 0; reg
< 4; reg
++) {
978 type
= hw_breakpoint_type(env
->dr
[7], reg
);
979 if ((type
== 0 && env
->dr
[reg
] == env
->eip
) ||
980 ((type
& 1) && env
->cpu_watchpoint
[reg
] &&
981 (env
->cpu_watchpoint
[reg
]->flags
& BP_WATCHPOINT_HIT
))) {
983 if (hw_breakpoint_enabled(env
->dr
[7], reg
))
987 if (hit_enabled
|| force_dr6_update
)
992 static CPUDebugExcpHandler
*prev_debug_excp_handler
;
994 void raise_exception_env(int exception_index
, CPUState
*env
);
996 static void breakpoint_handler(CPUState
*env
)
1000 if (env
->watchpoint_hit
) {
1001 if (env
->watchpoint_hit
->flags
& BP_CPU
) {
1002 env
->watchpoint_hit
= NULL
;
1003 if (check_hw_breakpoints(env
, 0))
1004 raise_exception_env(EXCP01_DB
, env
);
1006 cpu_resume_from_signal(env
, NULL
);
1009 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
)
1010 if (bp
->pc
== env
->eip
) {
1011 if (bp
->flags
& BP_CPU
) {
1012 check_hw_breakpoints(env
, 1);
1013 raise_exception_env(EXCP01_DB
, env
);
1018 if (prev_debug_excp_handler
)
1019 prev_debug_excp_handler(env
);
1022 /* This should come from sysemu.h - if we could include it here... */
1023 void qemu_system_reset_request(void);
1025 void cpu_inject_x86_mce(CPUState
*cenv
, int bank
, uint64_t status
,
1026 uint64_t mcg_status
, uint64_t addr
, uint64_t misc
)
1028 uint64_t mcg_cap
= cenv
->mcg_cap
;
1029 unsigned bank_num
= mcg_cap
& 0xff;
1030 uint64_t *banks
= cenv
->mce_banks
;
1032 if (kvm_enabled()) {
1033 kvm_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
, 0);
1037 if (bank
>= bank_num
|| !(status
& MCI_STATUS_VAL
))
1041 * if MSR_MCG_CTL is not all 1s, the uncorrected error
1042 * reporting is disabled
1044 if ((status
& MCI_STATUS_UC
) && (mcg_cap
& MCG_CTL_P
) &&
1045 cenv
->mcg_ctl
!= ~(uint64_t)0)
1049 * if MSR_MCi_CTL is not all 1s, the uncorrected error
1050 * reporting is disabled for the bank
1052 if ((status
& MCI_STATUS_UC
) && banks
[0] != ~(uint64_t)0)
1054 if (status
& MCI_STATUS_UC
) {
1055 if ((cenv
->mcg_status
& MCG_STATUS_MCIP
) ||
1056 !(cenv
->cr
[4] & CR4_MCE_MASK
)) {
1057 fprintf(stderr
, "injects mce exception while previous "
1058 "one is in progress!\n");
1059 qemu_log_mask(CPU_LOG_RESET
, "Triple fault\n");
1060 qemu_system_reset_request();
1063 if (banks
[1] & MCI_STATUS_VAL
)
1064 status
|= MCI_STATUS_OVER
;
1067 cenv
->mcg_status
= mcg_status
;
1069 cpu_interrupt(cenv
, CPU_INTERRUPT_MCE
);
1070 } else if (!(banks
[1] & MCI_STATUS_VAL
)
1071 || !(banks
[1] & MCI_STATUS_UC
)) {
1072 if (banks
[1] & MCI_STATUS_VAL
)
1073 status
|= MCI_STATUS_OVER
;
1078 banks
[1] |= MCI_STATUS_OVER
;
1080 #endif /* !CONFIG_USER_ONLY */
1082 static void mce_init(CPUX86State
*cenv
)
1084 unsigned int bank
, bank_num
;
1086 if (((cenv
->cpuid_version
>> 8)&0xf) >= 6
1087 && (cenv
->cpuid_features
&(CPUID_MCE
|CPUID_MCA
)) == (CPUID_MCE
|CPUID_MCA
)) {
1088 cenv
->mcg_cap
= MCE_CAP_DEF
| MCE_BANKS_DEF
;
1089 cenv
->mcg_ctl
= ~(uint64_t)0;
1090 bank_num
= MCE_BANKS_DEF
;
1091 for (bank
= 0; bank
< bank_num
; bank
++)
1092 cenv
->mce_banks
[bank
*4] = ~(uint64_t)0;
1096 int cpu_x86_get_descr_debug(CPUX86State
*env
, unsigned int selector
,
1097 target_ulong
*base
, unsigned int *limit
,
1098 unsigned int *flags
)
1109 index
= selector
& ~7;
1110 ptr
= dt
->base
+ index
;
1111 if ((index
+ 7) > dt
->limit
1112 || cpu_memory_rw_debug(env
, ptr
, (uint8_t *)&e1
, sizeof(e1
), 0) != 0
1113 || cpu_memory_rw_debug(env
, ptr
+4, (uint8_t *)&e2
, sizeof(e2
), 0) != 0)
1116 *base
= ((e1
>> 16) | ((e2
& 0xff) << 16) | (e2
& 0xff000000));
1117 *limit
= (e1
& 0xffff) | (e2
& 0x000f0000);
1118 if (e2
& DESC_G_MASK
)
1119 *limit
= (*limit
<< 12) | 0xfff;
1125 CPUX86State
*cpu_x86_init(const char *cpu_model
)
1130 env
= qemu_mallocz(sizeof(CPUX86State
));
1132 env
->cpu_model_str
= cpu_model
;
1134 /* init various static tables */
1137 optimize_flags_init();
1138 #ifndef CONFIG_USER_ONLY
1139 prev_debug_excp_handler
=
1140 cpu_set_debug_excp_handler(breakpoint_handler
);
1143 if (cpu_x86_register(env
, cpu_model
) < 0) {
1149 qemu_init_vcpu(env
);
1154 #if !defined(CONFIG_USER_ONLY)
1155 void do_cpu_init(CPUState
*env
)
1157 int sipi
= env
->interrupt_request
& CPU_INTERRUPT_SIPI
;
1159 env
->interrupt_request
= sipi
;
1160 apic_init_reset(env
);
1163 void do_cpu_sipi(CPUState
*env
)
1168 void do_cpu_init(CPUState
*env
)
1171 void do_cpu_sipi(CPUState
*env
)