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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include <linux/unistd.h>
36 #include <linux/version.h>
38 _syscall3(int, modify_ldt
, int, func
, void *, ptr
, unsigned long, bytecount
)
40 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
41 #define modify_ldt_ldt_s user_desc
43 #endif /* USE_CODE_COPY */
45 CPUX86State
*cpu_x86_init(void)
50 env
= qemu_mallocz(sizeof(CPUX86State
));
55 /* init various static tables */
58 optimize_flags_init();
61 /* testing code for code copy case */
63 struct modify_ldt_ldt_s ldt
;
66 ldt
.base_addr
= (unsigned long)env
;
67 ldt
.limit
= (sizeof(CPUState
) + 0xfff) >> 12;
69 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
70 ldt
.read_exec_only
= 0;
71 ldt
.limit_in_pages
= 1;
72 ldt
.seg_not_present
= 0;
74 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
76 asm volatile ("movl %0, %%fs" : : "r" ((1 << 3) | 7));
80 int family
, model
, stepping
;
82 env
->cpuid_vendor1
= 0x68747541; /* "Auth" */
83 env
->cpuid_vendor2
= 0x69746e65; /* "enti" */
84 env
->cpuid_vendor3
= 0x444d4163; /* "cAMD" */
89 env
->cpuid_vendor1
= 0x756e6547; /* "Genu" */
90 env
->cpuid_vendor2
= 0x49656e69; /* "ineI" */
91 env
->cpuid_vendor3
= 0x6c65746e; /* "ntel" */
104 env
->cpuid_level
= 2;
105 env
->cpuid_version
= (family
<< 8) | (model
<< 4) | stepping
;
106 env
->cpuid_features
= (CPUID_FP87
| CPUID_DE
| CPUID_PSE
|
107 CPUID_TSC
| CPUID_MSR
| CPUID_MCE
|
108 CPUID_CX8
| CPUID_PGE
| CPUID_CMOV
|
110 env
->pat
= 0x0007040600070406ULL
;
111 env
->cpuid_ext_features
= CPUID_EXT_SSE3
;
112 env
->cpuid_features
|= CPUID_FXSR
| CPUID_MMX
| CPUID_SSE
| CPUID_SSE2
| CPUID_PAE
| CPUID_SEP
;
113 env
->cpuid_features
|= CPUID_APIC
;
114 env
->cpuid_xlevel
= 0;
116 const char *model_id
= "QEMU Virtual CPU version " QEMU_VERSION
;
118 len
= strlen(model_id
);
119 for(i
= 0; i
< 48; i
++) {
124 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
128 /* currently not enabled for std i386 because not fully tested */
129 env
->cpuid_ext2_features
= (env
->cpuid_features
& 0x0183F3FF);
130 env
->cpuid_ext2_features
|= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
;
131 env
->cpuid_xlevel
= 0x80000008;
133 /* these features are needed for Win64 and aren't fully implemented */
134 env
->cpuid_features
|= CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
;
144 /* NOTE: must be called outside the CPU execute loop */
145 void cpu_reset(CPUX86State
*env
)
149 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
153 /* init to reset state */
155 #ifdef CONFIG_SOFTMMU
156 env
->hflags
|= HF_SOFTMMU_MASK
;
159 cpu_x86_update_cr0(env
, 0x60000010);
160 env
->a20_mask
= 0xffffffff;
162 env
->idt
.limit
= 0xffff;
163 env
->gdt
.limit
= 0xffff;
164 env
->ldt
.limit
= 0xffff;
165 env
->ldt
.flags
= DESC_P_MASK
;
166 env
->tr
.limit
= 0xffff;
167 env
->tr
.flags
= DESC_P_MASK
;
169 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff, 0);
170 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff, 0);
171 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff, 0);
172 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff, 0);
173 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff, 0);
174 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff, 0);
177 env
->regs
[R_EDX
] = 0x600; /* indicate P6 processor */
182 for(i
= 0;i
< 8; i
++)
189 void cpu_x86_close(CPUX86State
*env
)
194 /***********************************************************/
197 static const char *cc_op_str
[] = {
252 void cpu_dump_state(CPUState
*env
, FILE *f
,
253 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
258 static const char *seg_name
[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
260 eflags
= env
->eflags
;
262 if (env
->hflags
& HF_CS64_MASK
) {
264 "RAX=%016llx RBX=%016llx RCX=%016llx RDX=%016llx\n"
265 "RSI=%016llx RDI=%016llx RBP=%016llx RSP=%016llx\n"
266 "R8 =%016llx R9 =%016llx R10=%016llx R11=%016llx\n"
267 "R12=%016llx R13=%016llx R14=%016llx R15=%016llx\n"
268 "RIP=%016llx RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d HLT=%d\n",
286 eflags
& DF_MASK
? 'D' : '-',
287 eflags
& CC_O
? 'O' : '-',
288 eflags
& CC_S
? 'S' : '-',
289 eflags
& CC_Z
? 'Z' : '-',
290 eflags
& CC_A
? 'A' : '-',
291 eflags
& CC_P
? 'P' : '-',
292 eflags
& CC_C
? 'C' : '-',
293 env
->hflags
& HF_CPL_MASK
,
294 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
295 (env
->a20_mask
>> 20) & 1,
296 (env
->hflags
>> HF_HALTED_SHIFT
) & 1);
300 cpu_fprintf(f
, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
301 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
302 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d HLT=%d\n",
303 (uint32_t)env
->regs
[R_EAX
],
304 (uint32_t)env
->regs
[R_EBX
],
305 (uint32_t)env
->regs
[R_ECX
],
306 (uint32_t)env
->regs
[R_EDX
],
307 (uint32_t)env
->regs
[R_ESI
],
308 (uint32_t)env
->regs
[R_EDI
],
309 (uint32_t)env
->regs
[R_EBP
],
310 (uint32_t)env
->regs
[R_ESP
],
311 (uint32_t)env
->eip
, eflags
,
312 eflags
& DF_MASK
? 'D' : '-',
313 eflags
& CC_O
? 'O' : '-',
314 eflags
& CC_S
? 'S' : '-',
315 eflags
& CC_Z
? 'Z' : '-',
316 eflags
& CC_A
? 'A' : '-',
317 eflags
& CC_P
? 'P' : '-',
318 eflags
& CC_C
? 'C' : '-',
319 env
->hflags
& HF_CPL_MASK
,
320 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
321 (env
->a20_mask
>> 20) & 1,
322 (env
->hflags
>> HF_HALTED_SHIFT
) & 1);
326 if (env
->hflags
& HF_LMA_MASK
) {
327 for(i
= 0; i
< 6; i
++) {
328 SegmentCache
*sc
= &env
->segs
[i
];
329 cpu_fprintf(f
, "%s =%04x %016llx %08x %08x\n",
336 cpu_fprintf(f
, "LDT=%04x %016llx %08x %08x\n",
341 cpu_fprintf(f
, "TR =%04x %016llx %08x %08x\n",
346 cpu_fprintf(f
, "GDT= %016llx %08x\n",
347 env
->gdt
.base
, env
->gdt
.limit
);
348 cpu_fprintf(f
, "IDT= %016llx %08x\n",
349 env
->idt
.base
, env
->idt
.limit
);
350 cpu_fprintf(f
, "CR0=%08x CR2=%016llx CR3=%016llx CR4=%08x\n",
351 (uint32_t)env
->cr
[0],
354 (uint32_t)env
->cr
[4]);
358 for(i
= 0; i
< 6; i
++) {
359 SegmentCache
*sc
= &env
->segs
[i
];
360 cpu_fprintf(f
, "%s =%04x %08x %08x %08x\n",
367 cpu_fprintf(f
, "LDT=%04x %08x %08x %08x\n",
369 (uint32_t)env
->ldt
.base
,
372 cpu_fprintf(f
, "TR =%04x %08x %08x %08x\n",
374 (uint32_t)env
->tr
.base
,
377 cpu_fprintf(f
, "GDT= %08x %08x\n",
378 (uint32_t)env
->gdt
.base
, env
->gdt
.limit
);
379 cpu_fprintf(f
, "IDT= %08x %08x\n",
380 (uint32_t)env
->idt
.base
, env
->idt
.limit
);
381 cpu_fprintf(f
, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
382 (uint32_t)env
->cr
[0],
383 (uint32_t)env
->cr
[2],
384 (uint32_t)env
->cr
[3],
385 (uint32_t)env
->cr
[4]);
387 if (flags
& X86_DUMP_CCOP
) {
388 if ((unsigned)env
->cc_op
< CC_OP_NB
)
389 snprintf(cc_op_name
, sizeof(cc_op_name
), "%s", cc_op_str
[env
->cc_op
]);
391 snprintf(cc_op_name
, sizeof(cc_op_name
), "[%d]", env
->cc_op
);
393 if (env
->hflags
& HF_CS64_MASK
) {
394 cpu_fprintf(f
, "CCS=%016llx CCD=%016llx CCO=%-8s\n",
395 env
->cc_src
, env
->cc_dst
,
400 cpu_fprintf(f
, "CCS=%08x CCD=%08x CCO=%-8s\n",
401 (uint32_t)env
->cc_src
, (uint32_t)env
->cc_dst
,
405 if (flags
& X86_DUMP_FPU
) {
408 for(i
= 0; i
< 8; i
++) {
409 fptag
|= ((!env
->fptags
[i
]) << i
);
411 cpu_fprintf(f
, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
413 (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11,
418 #if defined(USE_X86LDOUBLE)
426 tmp
.d
= env
->fpregs
[i
].d
;
427 cpu_fprintf(f
, "FPR%d=%016llx %04x",
428 i
, tmp
.l
.lower
, tmp
.l
.upper
);
430 cpu_fprintf(f
, "FPR%d=%016llx",
431 i
, env
->fpregs
[i
].mmx
.q
);
434 cpu_fprintf(f
, "\n");
438 if (env
->hflags
& HF_CS64_MASK
)
443 cpu_fprintf(f
, "XMM%02d=%08x%08x%08x%08x",
445 env
->xmm_regs
[i
].XMM_L(3),
446 env
->xmm_regs
[i
].XMM_L(2),
447 env
->xmm_regs
[i
].XMM_L(1),
448 env
->xmm_regs
[i
].XMM_L(0));
450 cpu_fprintf(f
, "\n");
457 /***********************************************************/
459 /* XXX: add PGE support */
461 void cpu_x86_set_a20(CPUX86State
*env
, int a20_state
)
463 a20_state
= (a20_state
!= 0);
464 if (a20_state
!= ((env
->a20_mask
>> 20) & 1)) {
465 #if defined(DEBUG_MMU)
466 printf("A20 update: a20=%d\n", a20_state
);
468 /* if the cpu is currently executing code, we must unlink it and
469 all the potentially executing TB */
470 cpu_interrupt(env
, CPU_INTERRUPT_EXITTB
);
472 /* when a20 is changed, all the MMU mappings are invalid, so
473 we must flush everything */
475 env
->a20_mask
= 0xffefffff | (a20_state
<< 20);
479 void cpu_x86_update_cr0(CPUX86State
*env
, uint32_t new_cr0
)
483 #if defined(DEBUG_MMU)
484 printf("CR0 update: CR0=0x%08x\n", new_cr0
);
486 if ((new_cr0
& (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
)) !=
487 (env
->cr
[0] & (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
))) {
492 if (!(env
->cr
[0] & CR0_PG_MASK
) && (new_cr0
& CR0_PG_MASK
) &&
493 (env
->efer
& MSR_EFER_LME
)) {
494 /* enter in long mode */
495 /* XXX: generate an exception */
496 if (!(env
->cr
[4] & CR4_PAE_MASK
))
498 env
->efer
|= MSR_EFER_LMA
;
499 env
->hflags
|= HF_LMA_MASK
;
500 } else if ((env
->cr
[0] & CR0_PG_MASK
) && !(new_cr0
& CR0_PG_MASK
) &&
501 (env
->efer
& MSR_EFER_LMA
)) {
503 env
->efer
&= ~MSR_EFER_LMA
;
504 env
->hflags
&= ~(HF_LMA_MASK
| HF_CS64_MASK
);
505 env
->eip
&= 0xffffffff;
508 env
->cr
[0] = new_cr0
| CR0_ET_MASK
;
510 /* update PE flag in hidden flags */
511 pe_state
= (env
->cr
[0] & CR0_PE_MASK
);
512 env
->hflags
= (env
->hflags
& ~HF_PE_MASK
) | (pe_state
<< HF_PE_SHIFT
);
513 /* ensure that ADDSEG is always set in real mode */
514 env
->hflags
|= ((pe_state
^ 1) << HF_ADDSEG_SHIFT
);
515 /* update FPU flags */
516 env
->hflags
= (env
->hflags
& ~(HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
)) |
517 ((new_cr0
<< (HF_MP_SHIFT
- 1)) & (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
));
520 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
522 void cpu_x86_update_cr3(CPUX86State
*env
, target_ulong new_cr3
)
524 env
->cr
[3] = new_cr3
;
525 if (env
->cr
[0] & CR0_PG_MASK
) {
526 #if defined(DEBUG_MMU)
527 printf("CR3 update: CR3=" TARGET_FMT_lx
"\n", new_cr3
);
533 void cpu_x86_update_cr4(CPUX86State
*env
, uint32_t new_cr4
)
535 #if defined(DEBUG_MMU)
536 printf("CR4 update: CR4=%08x\n", (uint32_t)env
->cr
[4]);
538 if ((new_cr4
& (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
)) !=
539 (env
->cr
[4] & (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
))) {
543 if (!(env
->cpuid_features
& CPUID_SSE
))
544 new_cr4
&= ~CR4_OSFXSR_MASK
;
545 if (new_cr4
& CR4_OSFXSR_MASK
)
546 env
->hflags
|= HF_OSFXSR_MASK
;
548 env
->hflags
&= ~HF_OSFXSR_MASK
;
550 env
->cr
[4] = new_cr4
;
553 /* XXX: also flush 4MB pages */
554 void cpu_x86_flush_tlb(CPUX86State
*env
, target_ulong addr
)
556 tlb_flush_page(env
, addr
);
559 #if defined(CONFIG_USER_ONLY)
561 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
562 int is_write
, int is_user
, int is_softmmu
)
564 /* user mode only emulation */
567 env
->error_code
= (is_write
<< PG_ERROR_W_BIT
);
568 env
->error_code
|= PG_ERROR_U_MASK
;
569 env
->exception_index
= EXCP0E_PAGE
;
573 target_ulong
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
580 #define PHYS_ADDR_MASK 0xfffff000
583 -1 = cannot handle fault
584 0 = nothing more to do
585 1 = generate PF fault
586 2 = soft MMU activation required for this block
588 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
589 int is_write1
, int is_user
, int is_softmmu
)
592 uint32_t pdpe_addr
, pde_addr
, pte_addr
;
593 int error_code
, is_dirty
, prot
, page_size
, ret
, is_write
;
594 unsigned long paddr
, page_offset
;
595 target_ulong vaddr
, virt_addr
;
597 #if defined(DEBUG_MMU)
598 printf("MMU fault: addr=" TARGET_FMT_lx
" w=%d u=%d eip=" TARGET_FMT_lx
"\n",
599 addr
, is_write1
, is_user
, env
->eip
);
601 is_write
= is_write1
& 1;
603 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
605 virt_addr
= addr
& TARGET_PAGE_MASK
;
606 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
611 if (env
->cr
[4] & CR4_PAE_MASK
) {
614 /* XXX: we only use 32 bit physical addresses */
616 if (env
->hflags
& HF_LMA_MASK
) {
621 /* test virtual address sign extension */
622 sext
= (int64_t)addr
>> 47;
623 if (sext
!= 0 && sext
!= -1) {
625 env
->exception_index
= EXCP0D_GPF
;
629 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
631 pml4e
= ldq_phys(pml4e_addr
);
632 if (!(pml4e
& PG_PRESENT_MASK
)) {
636 if (!(env
->efer
& MSR_EFER_NXE
) && (pml4e
& PG_NX_MASK
)) {
637 error_code
= PG_ERROR_RSVD_MASK
;
640 if (!(pml4e
& PG_ACCESSED_MASK
)) {
641 pml4e
|= PG_ACCESSED_MASK
;
642 stl_phys_notdirty(pml4e_addr
, pml4e
);
644 ptep
= pml4e
^ PG_NX_MASK
;
645 pdpe_addr
= ((pml4e
& PHYS_ADDR_MASK
) + (((addr
>> 30) & 0x1ff) << 3)) &
647 pdpe
= ldq_phys(pdpe_addr
);
648 if (!(pdpe
& PG_PRESENT_MASK
)) {
652 if (!(env
->efer
& MSR_EFER_NXE
) && (pdpe
& PG_NX_MASK
)) {
653 error_code
= PG_ERROR_RSVD_MASK
;
656 ptep
&= pdpe
^ PG_NX_MASK
;
657 if (!(pdpe
& PG_ACCESSED_MASK
)) {
658 pdpe
|= PG_ACCESSED_MASK
;
659 stl_phys_notdirty(pdpe_addr
, pdpe
);
664 /* XXX: load them when cr3 is loaded ? */
665 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 30) << 3)) &
667 pdpe
= ldq_phys(pdpe_addr
);
668 if (!(pdpe
& PG_PRESENT_MASK
)) {
672 ptep
= PG_NX_MASK
| PG_USER_MASK
| PG_RW_MASK
;
675 pde_addr
= ((pdpe
& PHYS_ADDR_MASK
) + (((addr
>> 21) & 0x1ff) << 3)) &
677 pde
= ldq_phys(pde_addr
);
678 if (!(pde
& PG_PRESENT_MASK
)) {
682 if (!(env
->efer
& MSR_EFER_NXE
) && (pde
& PG_NX_MASK
)) {
683 error_code
= PG_ERROR_RSVD_MASK
;
686 ptep
&= pde
^ PG_NX_MASK
;
687 if (pde
& PG_PSE_MASK
) {
689 page_size
= 2048 * 1024;
691 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
692 goto do_fault_protect
;
694 if (!(ptep
& PG_USER_MASK
))
695 goto do_fault_protect
;
696 if (is_write
&& !(ptep
& PG_RW_MASK
))
697 goto do_fault_protect
;
699 if ((env
->cr
[0] & CR0_WP_MASK
) &&
700 is_write
&& !(ptep
& PG_RW_MASK
))
701 goto do_fault_protect
;
703 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
704 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
705 pde
|= PG_ACCESSED_MASK
;
707 pde
|= PG_DIRTY_MASK
;
708 stl_phys_notdirty(pde_addr
, pde
);
710 /* align to page_size */
711 pte
= pde
& ((PHYS_ADDR_MASK
& ~(page_size
- 1)) | 0xfff);
712 virt_addr
= addr
& ~(page_size
- 1);
715 if (!(pde
& PG_ACCESSED_MASK
)) {
716 pde
|= PG_ACCESSED_MASK
;
717 stl_phys_notdirty(pde_addr
, pde
);
719 pte_addr
= ((pde
& PHYS_ADDR_MASK
) + (((addr
>> 12) & 0x1ff) << 3)) &
721 pte
= ldq_phys(pte_addr
);
722 if (!(pte
& PG_PRESENT_MASK
)) {
726 if (!(env
->efer
& MSR_EFER_NXE
) && (pte
& PG_NX_MASK
)) {
727 error_code
= PG_ERROR_RSVD_MASK
;
730 /* combine pde and pte nx, user and rw protections */
731 ptep
&= pte
^ PG_NX_MASK
;
733 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
734 goto do_fault_protect
;
736 if (!(ptep
& PG_USER_MASK
))
737 goto do_fault_protect
;
738 if (is_write
&& !(ptep
& PG_RW_MASK
))
739 goto do_fault_protect
;
741 if ((env
->cr
[0] & CR0_WP_MASK
) &&
742 is_write
&& !(ptep
& PG_RW_MASK
))
743 goto do_fault_protect
;
745 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
746 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
747 pte
|= PG_ACCESSED_MASK
;
749 pte
|= PG_DIRTY_MASK
;
750 stl_phys_notdirty(pte_addr
, pte
);
753 virt_addr
= addr
& ~0xfff;
754 pte
= pte
& (PHYS_ADDR_MASK
| 0xfff);
759 /* page directory entry */
760 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & ~3)) &
762 pde
= ldl_phys(pde_addr
);
763 if (!(pde
& PG_PRESENT_MASK
)) {
767 /* if PSE bit is set, then we use a 4MB page */
768 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
769 page_size
= 4096 * 1024;
771 if (!(pde
& PG_USER_MASK
))
772 goto do_fault_protect
;
773 if (is_write
&& !(pde
& PG_RW_MASK
))
774 goto do_fault_protect
;
776 if ((env
->cr
[0] & CR0_WP_MASK
) &&
777 is_write
&& !(pde
& PG_RW_MASK
))
778 goto do_fault_protect
;
780 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
781 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
782 pde
|= PG_ACCESSED_MASK
;
784 pde
|= PG_DIRTY_MASK
;
785 stl_phys_notdirty(pde_addr
, pde
);
788 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
790 virt_addr
= addr
& ~(page_size
- 1);
792 if (!(pde
& PG_ACCESSED_MASK
)) {
793 pde
|= PG_ACCESSED_MASK
;
794 stl_phys_notdirty(pde_addr
, pde
);
797 /* page directory entry */
798 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) &
800 pte
= ldl_phys(pte_addr
);
801 if (!(pte
& PG_PRESENT_MASK
)) {
805 /* combine pde and pte user and rw protections */
808 if (!(ptep
& PG_USER_MASK
))
809 goto do_fault_protect
;
810 if (is_write
&& !(ptep
& PG_RW_MASK
))
811 goto do_fault_protect
;
813 if ((env
->cr
[0] & CR0_WP_MASK
) &&
814 is_write
&& !(ptep
& PG_RW_MASK
))
815 goto do_fault_protect
;
817 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
818 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
819 pte
|= PG_ACCESSED_MASK
;
821 pte
|= PG_DIRTY_MASK
;
822 stl_phys_notdirty(pte_addr
, pte
);
825 virt_addr
= addr
& ~0xfff;
828 /* the page can be put in the TLB */
830 if (!(ptep
& PG_NX_MASK
))
832 if (pte
& PG_DIRTY_MASK
) {
833 /* only set write access if already dirty... otherwise wait
836 if (ptep
& PG_RW_MASK
)
839 if (!(env
->cr
[0] & CR0_WP_MASK
) ||
845 pte
= pte
& env
->a20_mask
;
847 /* Even if 4MB pages, we map only one 4KB page in the cache to
848 avoid filling it too fast */
849 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
850 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
851 vaddr
= virt_addr
+ page_offset
;
853 ret
= tlb_set_page_exec(env
, vaddr
, paddr
, prot
, is_user
, is_softmmu
);
856 error_code
= PG_ERROR_P_MASK
;
859 error_code
|= (is_write
<< PG_ERROR_W_BIT
);
861 error_code
|= PG_ERROR_U_MASK
;
862 if (is_write1
== 2 &&
863 (env
->efer
& MSR_EFER_NXE
) &&
864 (env
->cr
[4] & CR4_PAE_MASK
))
865 error_code
|= PG_ERROR_I_D_MASK
;
866 env
->error_code
= error_code
;
867 env
->exception_index
= EXCP0E_PAGE
;
871 target_ulong
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
873 uint32_t pde_addr
, pte_addr
;
874 uint32_t pde
, pte
, paddr
, page_offset
, page_size
;
876 if (env
->cr
[4] & CR4_PAE_MASK
) {
877 uint32_t pdpe_addr
, pde_addr
, pte_addr
;
880 /* XXX: we only use 32 bit physical addresses */
882 if (env
->hflags
& HF_LMA_MASK
) {
883 uint32_t pml4e_addr
, pml4e
;
886 /* test virtual address sign extension */
887 sext
= (int64_t)addr
>> 47;
888 if (sext
!= 0 && sext
!= -1)
891 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
893 pml4e
= ldl_phys(pml4e_addr
);
894 if (!(pml4e
& PG_PRESENT_MASK
))
897 pdpe_addr
= ((pml4e
& ~0xfff) + (((addr
>> 30) & 0x1ff) << 3)) &
899 pdpe
= ldl_phys(pdpe_addr
);
900 if (!(pdpe
& PG_PRESENT_MASK
))
905 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 30) << 3)) &
907 pdpe
= ldl_phys(pdpe_addr
);
908 if (!(pdpe
& PG_PRESENT_MASK
))
912 pde_addr
= ((pdpe
& ~0xfff) + (((addr
>> 21) & 0x1ff) << 3)) &
914 pde
= ldl_phys(pde_addr
);
915 if (!(pde
& PG_PRESENT_MASK
)) {
918 if (pde
& PG_PSE_MASK
) {
920 page_size
= 2048 * 1024;
921 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
924 pte_addr
= ((pde
& ~0xfff) + (((addr
>> 12) & 0x1ff) << 3)) &
927 pte
= ldl_phys(pte_addr
);
930 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
934 /* page directory entry */
935 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & ~3)) & env
->a20_mask
;
936 pde
= ldl_phys(pde_addr
);
937 if (!(pde
& PG_PRESENT_MASK
))
939 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
940 pte
= pde
& ~0x003ff000; /* align to 4MB */
941 page_size
= 4096 * 1024;
943 /* page directory entry */
944 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) & env
->a20_mask
;
945 pte
= ldl_phys(pte_addr
);
946 if (!(pte
& PG_PRESENT_MASK
))
951 pte
= pte
& env
->a20_mask
;
954 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
955 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
958 #endif /* !CONFIG_USER_ONLY */
960 #if defined(USE_CODE_COPY)
973 uint8_t fpregs1
[8 * 10];
976 void restore_native_fp_state(CPUState
*env
)
979 struct fpstate fp1
, *fp
= &fp1
;
981 fp
->fpuc
= env
->fpuc
;
982 fp
->fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
984 for (i
=7; i
>=0; i
--) {
986 if (env
->fptags
[i
]) {
989 /* the FPU automatically computes it */
994 for(i
= 0;i
< 8; i
++) {
995 memcpy(&fp
->fpregs1
[i
* 10], &env
->fpregs
[j
].d
, 10);
998 asm volatile ("frstor %0" : "=m" (*fp
));
999 env
->native_fp_regs
= 1;
1002 void save_native_fp_state(CPUState
*env
)
1006 struct fpstate fp1
, *fp
= &fp1
;
1008 asm volatile ("fsave %0" : : "m" (*fp
));
1009 env
->fpuc
= fp
->fpuc
;
1010 env
->fpstt
= (fp
->fpus
>> 11) & 7;
1011 env
->fpus
= fp
->fpus
& ~0x3800;
1013 for(i
= 0;i
< 8; i
++) {
1014 env
->fptags
[i
] = ((fptag
& 3) == 3);
1018 for(i
= 0;i
< 8; i
++) {
1019 memcpy(&env
->fpregs
[j
].d
, &fp
->fpregs1
[i
* 10], 10);
1022 /* we must restore the default rounding state */
1023 /* XXX: we do not restore the exception state */
1024 fpuc
= 0x037f | (env
->fpuc
& (3 << 10));
1025 asm volatile("fldcw %0" : : "m" (fpuc
));
1026 env
->native_fp_regs
= 0;