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
36 #include <linux/unistd.h>
37 #include <linux/version.h>
39 int modify_ldt(int func
, void *ptr
, unsigned long bytecount
)
41 return syscall(__NR_modify_ldt
, func
, ptr
, bytecount
);
44 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
45 #define modify_ldt_ldt_s user_desc
47 #endif /* USE_CODE_COPY */
49 CPUX86State
*cpu_x86_init(void)
54 env
= qemu_mallocz(sizeof(CPUX86State
));
59 /* init various static tables */
62 optimize_flags_init();
65 /* testing code for code copy case */
67 struct modify_ldt_ldt_s ldt
;
70 ldt
.base_addr
= (unsigned long)env
;
71 ldt
.limit
= (sizeof(CPUState
) + 0xfff) >> 12;
73 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
74 ldt
.read_exec_only
= 0;
75 ldt
.limit_in_pages
= 1;
76 ldt
.seg_not_present
= 0;
78 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
80 asm volatile ("movl %0, %%fs" : : "r" ((1 << 3) | 7));
84 int family
, model
, stepping
;
86 env
->cpuid_vendor1
= 0x68747541; /* "Auth" */
87 env
->cpuid_vendor2
= 0x69746e65; /* "enti" */
88 env
->cpuid_vendor3
= 0x444d4163; /* "cAMD" */
93 env
->cpuid_vendor1
= 0x756e6547; /* "Genu" */
94 env
->cpuid_vendor2
= 0x49656e69; /* "ineI" */
95 env
->cpuid_vendor3
= 0x6c65746e; /* "ntel" */
108 env
->cpuid_level
= 2;
109 env
->cpuid_version
= (family
<< 8) | (model
<< 4) | stepping
;
110 env
->cpuid_features
= (CPUID_FP87
| CPUID_DE
| CPUID_PSE
|
111 CPUID_TSC
| CPUID_MSR
| CPUID_MCE
|
112 CPUID_CX8
| CPUID_PGE
| CPUID_CMOV
|
114 env
->pat
= 0x0007040600070406ULL
;
115 env
->cpuid_ext3_features
= CPUID_EXT3_SVM
;
116 env
->cpuid_ext_features
= CPUID_EXT_SSE3
;
117 env
->cpuid_features
|= CPUID_FXSR
| CPUID_MMX
| CPUID_SSE
| CPUID_SSE2
| CPUID_PAE
| CPUID_SEP
;
118 env
->cpuid_features
|= CPUID_APIC
;
119 env
->cpuid_xlevel
= 0x8000000e;
121 const char *model_id
= "QEMU Virtual CPU version " QEMU_VERSION
;
123 len
= strlen(model_id
);
124 for(i
= 0; i
< 48; i
++) {
129 env
->cpuid_model
[i
>> 2] |= c
<< (8 * (i
& 3));
133 /* currently not enabled for std i386 because not fully tested */
134 env
->cpuid_ext2_features
= (env
->cpuid_features
& 0x0183F3FF);
135 env
->cpuid_ext2_features
|= CPUID_EXT2_LM
| CPUID_EXT2_SYSCALL
| CPUID_EXT2_NX
;
137 /* these features are needed for Win64 and aren't fully implemented */
138 env
->cpuid_features
|= CPUID_MTRR
| CPUID_CLFLUSH
| CPUID_MCA
;
139 /* this feature is needed for Solaris and isn't fully implemented */
140 env
->cpuid_features
|= CPUID_PSE36
;
150 /* NOTE: must be called outside the CPU execute loop */
151 void cpu_reset(CPUX86State
*env
)
155 memset(env
, 0, offsetof(CPUX86State
, breakpoints
));
159 env
->old_exception
= -1;
161 /* init to reset state */
163 #ifdef CONFIG_SOFTMMU
164 env
->hflags
|= HF_SOFTMMU_MASK
;
166 env
->hflags
|= HF_GIF_MASK
;
168 cpu_x86_update_cr0(env
, 0x60000010);
169 env
->a20_mask
= 0xffffffff;
170 env
->smbase
= 0x30000;
172 env
->idt
.limit
= 0xffff;
173 env
->gdt
.limit
= 0xffff;
174 env
->ldt
.limit
= 0xffff;
175 env
->ldt
.flags
= DESC_P_MASK
;
176 env
->tr
.limit
= 0xffff;
177 env
->tr
.flags
= DESC_P_MASK
;
179 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, 0xffff0000, 0xffff, 0);
180 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffff, 0);
181 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffff, 0);
182 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffff, 0);
183 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffff, 0);
184 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffff, 0);
187 env
->regs
[R_EDX
] = 0x600; /* indicate P6 processor */
192 for(i
= 0;i
< 8; i
++)
199 void cpu_x86_close(CPUX86State
*env
)
204 /***********************************************************/
207 static const char *cc_op_str
[] = {
262 void cpu_dump_state(CPUState
*env
, FILE *f
,
263 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
268 static const char *seg_name
[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
270 eflags
= env
->eflags
;
272 if (env
->hflags
& HF_CS64_MASK
) {
274 "RAX=%016" PRIx64
" RBX=%016" PRIx64
" RCX=%016" PRIx64
" RDX=%016" PRIx64
"\n"
275 "RSI=%016" PRIx64
" RDI=%016" PRIx64
" RBP=%016" PRIx64
" RSP=%016" PRIx64
"\n"
276 "R8 =%016" PRIx64
" R9 =%016" PRIx64
" R10=%016" PRIx64
" R11=%016" PRIx64
"\n"
277 "R12=%016" PRIx64
" R13=%016" PRIx64
" R14=%016" PRIx64
" R15=%016" PRIx64
"\n"
278 "RIP=%016" PRIx64
" RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
296 eflags
& DF_MASK
? 'D' : '-',
297 eflags
& CC_O
? 'O' : '-',
298 eflags
& CC_S
? 'S' : '-',
299 eflags
& CC_Z
? 'Z' : '-',
300 eflags
& CC_A
? 'A' : '-',
301 eflags
& CC_P
? 'P' : '-',
302 eflags
& CC_C
? 'C' : '-',
303 env
->hflags
& HF_CPL_MASK
,
304 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
305 (env
->a20_mask
>> 20) & 1,
306 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
307 (env
->hflags
>> HF_HALTED_SHIFT
) & 1);
311 cpu_fprintf(f
, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
312 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
313 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
314 (uint32_t)env
->regs
[R_EAX
],
315 (uint32_t)env
->regs
[R_EBX
],
316 (uint32_t)env
->regs
[R_ECX
],
317 (uint32_t)env
->regs
[R_EDX
],
318 (uint32_t)env
->regs
[R_ESI
],
319 (uint32_t)env
->regs
[R_EDI
],
320 (uint32_t)env
->regs
[R_EBP
],
321 (uint32_t)env
->regs
[R_ESP
],
322 (uint32_t)env
->eip
, eflags
,
323 eflags
& DF_MASK
? 'D' : '-',
324 eflags
& CC_O
? 'O' : '-',
325 eflags
& CC_S
? 'S' : '-',
326 eflags
& CC_Z
? 'Z' : '-',
327 eflags
& CC_A
? 'A' : '-',
328 eflags
& CC_P
? 'P' : '-',
329 eflags
& CC_C
? 'C' : '-',
330 env
->hflags
& HF_CPL_MASK
,
331 (env
->hflags
>> HF_INHIBIT_IRQ_SHIFT
) & 1,
332 (env
->a20_mask
>> 20) & 1,
333 (env
->hflags
>> HF_SMM_SHIFT
) & 1,
334 (env
->hflags
>> HF_HALTED_SHIFT
) & 1);
338 if (env
->hflags
& HF_LMA_MASK
) {
339 for(i
= 0; i
< 6; i
++) {
340 SegmentCache
*sc
= &env
->segs
[i
];
341 cpu_fprintf(f
, "%s =%04x %016" PRIx64
" %08x %08x\n",
348 cpu_fprintf(f
, "LDT=%04x %016" PRIx64
" %08x %08x\n",
353 cpu_fprintf(f
, "TR =%04x %016" PRIx64
" %08x %08x\n",
358 cpu_fprintf(f
, "GDT= %016" PRIx64
" %08x\n",
359 env
->gdt
.base
, env
->gdt
.limit
);
360 cpu_fprintf(f
, "IDT= %016" PRIx64
" %08x\n",
361 env
->idt
.base
, env
->idt
.limit
);
362 cpu_fprintf(f
, "CR0=%08x CR2=%016" PRIx64
" CR3=%016" PRIx64
" CR4=%08x\n",
363 (uint32_t)env
->cr
[0],
366 (uint32_t)env
->cr
[4]);
370 for(i
= 0; i
< 6; i
++) {
371 SegmentCache
*sc
= &env
->segs
[i
];
372 cpu_fprintf(f
, "%s =%04x %08x %08x %08x\n",
379 cpu_fprintf(f
, "LDT=%04x %08x %08x %08x\n",
381 (uint32_t)env
->ldt
.base
,
384 cpu_fprintf(f
, "TR =%04x %08x %08x %08x\n",
386 (uint32_t)env
->tr
.base
,
389 cpu_fprintf(f
, "GDT= %08x %08x\n",
390 (uint32_t)env
->gdt
.base
, env
->gdt
.limit
);
391 cpu_fprintf(f
, "IDT= %08x %08x\n",
392 (uint32_t)env
->idt
.base
, env
->idt
.limit
);
393 cpu_fprintf(f
, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
394 (uint32_t)env
->cr
[0],
395 (uint32_t)env
->cr
[2],
396 (uint32_t)env
->cr
[3],
397 (uint32_t)env
->cr
[4]);
399 if (flags
& X86_DUMP_CCOP
) {
400 if ((unsigned)env
->cc_op
< CC_OP_NB
)
401 snprintf(cc_op_name
, sizeof(cc_op_name
), "%s", cc_op_str
[env
->cc_op
]);
403 snprintf(cc_op_name
, sizeof(cc_op_name
), "[%d]", env
->cc_op
);
405 if (env
->hflags
& HF_CS64_MASK
) {
406 cpu_fprintf(f
, "CCS=%016" PRIx64
" CCD=%016" PRIx64
" CCO=%-8s\n",
407 env
->cc_src
, env
->cc_dst
,
412 cpu_fprintf(f
, "CCS=%08x CCD=%08x CCO=%-8s\n",
413 (uint32_t)env
->cc_src
, (uint32_t)env
->cc_dst
,
417 if (flags
& X86_DUMP_FPU
) {
420 for(i
= 0; i
< 8; i
++) {
421 fptag
|= ((!env
->fptags
[i
]) << i
);
423 cpu_fprintf(f
, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
425 (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11,
430 #if defined(USE_X86LDOUBLE)
438 tmp
.d
= env
->fpregs
[i
].d
;
439 cpu_fprintf(f
, "FPR%d=%016" PRIx64
" %04x",
440 i
, tmp
.l
.lower
, tmp
.l
.upper
);
442 cpu_fprintf(f
, "FPR%d=%016" PRIx64
,
443 i
, env
->fpregs
[i
].mmx
.q
);
446 cpu_fprintf(f
, "\n");
450 if (env
->hflags
& HF_CS64_MASK
)
455 cpu_fprintf(f
, "XMM%02d=%08x%08x%08x%08x",
457 env
->xmm_regs
[i
].XMM_L(3),
458 env
->xmm_regs
[i
].XMM_L(2),
459 env
->xmm_regs
[i
].XMM_L(1),
460 env
->xmm_regs
[i
].XMM_L(0));
462 cpu_fprintf(f
, "\n");
469 /***********************************************************/
471 /* XXX: add PGE support */
473 void cpu_x86_set_a20(CPUX86State
*env
, int a20_state
)
475 a20_state
= (a20_state
!= 0);
476 if (a20_state
!= ((env
->a20_mask
>> 20) & 1)) {
477 #if defined(DEBUG_MMU)
478 printf("A20 update: a20=%d\n", a20_state
);
480 /* if the cpu is currently executing code, we must unlink it and
481 all the potentially executing TB */
482 cpu_interrupt(env
, CPU_INTERRUPT_EXITTB
);
484 /* when a20 is changed, all the MMU mappings are invalid, so
485 we must flush everything */
487 env
->a20_mask
= 0xffefffff | (a20_state
<< 20);
491 void cpu_x86_update_cr0(CPUX86State
*env
, uint32_t new_cr0
)
495 #if defined(DEBUG_MMU)
496 printf("CR0 update: CR0=0x%08x\n", new_cr0
);
498 if ((new_cr0
& (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
)) !=
499 (env
->cr
[0] & (CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
))) {
504 if (!(env
->cr
[0] & CR0_PG_MASK
) && (new_cr0
& CR0_PG_MASK
) &&
505 (env
->efer
& MSR_EFER_LME
)) {
506 /* enter in long mode */
507 /* XXX: generate an exception */
508 if (!(env
->cr
[4] & CR4_PAE_MASK
))
510 env
->efer
|= MSR_EFER_LMA
;
511 env
->hflags
|= HF_LMA_MASK
;
512 } else if ((env
->cr
[0] & CR0_PG_MASK
) && !(new_cr0
& CR0_PG_MASK
) &&
513 (env
->efer
& MSR_EFER_LMA
)) {
515 env
->efer
&= ~MSR_EFER_LMA
;
516 env
->hflags
&= ~(HF_LMA_MASK
| HF_CS64_MASK
);
517 env
->eip
&= 0xffffffff;
520 env
->cr
[0] = new_cr0
| CR0_ET_MASK
;
522 /* update PE flag in hidden flags */
523 pe_state
= (env
->cr
[0] & CR0_PE_MASK
);
524 env
->hflags
= (env
->hflags
& ~HF_PE_MASK
) | (pe_state
<< HF_PE_SHIFT
);
525 /* ensure that ADDSEG is always set in real mode */
526 env
->hflags
|= ((pe_state
^ 1) << HF_ADDSEG_SHIFT
);
527 /* update FPU flags */
528 env
->hflags
= (env
->hflags
& ~(HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
)) |
529 ((new_cr0
<< (HF_MP_SHIFT
- 1)) & (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
));
532 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
534 void cpu_x86_update_cr3(CPUX86State
*env
, target_ulong new_cr3
)
536 env
->cr
[3] = new_cr3
;
537 if (env
->cr
[0] & CR0_PG_MASK
) {
538 #if defined(DEBUG_MMU)
539 printf("CR3 update: CR3=" TARGET_FMT_lx
"\n", new_cr3
);
545 void cpu_x86_update_cr4(CPUX86State
*env
, uint32_t new_cr4
)
547 #if defined(DEBUG_MMU)
548 printf("CR4 update: CR4=%08x\n", (uint32_t)env
->cr
[4]);
550 if ((new_cr4
& (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
)) !=
551 (env
->cr
[4] & (CR4_PGE_MASK
| CR4_PAE_MASK
| CR4_PSE_MASK
))) {
555 if (!(env
->cpuid_features
& CPUID_SSE
))
556 new_cr4
&= ~CR4_OSFXSR_MASK
;
557 if (new_cr4
& CR4_OSFXSR_MASK
)
558 env
->hflags
|= HF_OSFXSR_MASK
;
560 env
->hflags
&= ~HF_OSFXSR_MASK
;
562 env
->cr
[4] = new_cr4
;
565 /* XXX: also flush 4MB pages */
566 void cpu_x86_flush_tlb(CPUX86State
*env
, target_ulong addr
)
568 tlb_flush_page(env
, addr
);
571 #if defined(CONFIG_USER_ONLY)
573 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
574 int is_write
, int is_user
, int is_softmmu
)
576 /* user mode only emulation */
579 env
->error_code
= (is_write
<< PG_ERROR_W_BIT
);
580 env
->error_code
|= PG_ERROR_U_MASK
;
581 env
->exception_index
= EXCP0E_PAGE
;
585 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
592 #define PHYS_ADDR_MASK 0xfffff000
595 -1 = cannot handle fault
596 0 = nothing more to do
597 1 = generate PF fault
598 2 = soft MMU activation required for this block
600 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, target_ulong addr
,
601 int is_write1
, int is_user
, int is_softmmu
)
604 uint32_t pdpe_addr
, pde_addr
, pte_addr
;
605 int error_code
, is_dirty
, prot
, page_size
, ret
, is_write
;
606 unsigned long paddr
, page_offset
;
607 target_ulong vaddr
, virt_addr
;
609 #if defined(DEBUG_MMU)
610 printf("MMU fault: addr=" TARGET_FMT_lx
" w=%d u=%d eip=" TARGET_FMT_lx
"\n",
611 addr
, is_write1
, is_user
, env
->eip
);
613 is_write
= is_write1
& 1;
615 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
617 virt_addr
= addr
& TARGET_PAGE_MASK
;
618 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
623 if (env
->cr
[4] & CR4_PAE_MASK
) {
626 /* XXX: we only use 32 bit physical addresses */
628 if (env
->hflags
& HF_LMA_MASK
) {
633 /* test virtual address sign extension */
634 sext
= (int64_t)addr
>> 47;
635 if (sext
!= 0 && sext
!= -1) {
637 env
->exception_index
= EXCP0D_GPF
;
641 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
643 pml4e
= ldq_phys(pml4e_addr
);
644 if (!(pml4e
& PG_PRESENT_MASK
)) {
648 if (!(env
->efer
& MSR_EFER_NXE
) && (pml4e
& PG_NX_MASK
)) {
649 error_code
= PG_ERROR_RSVD_MASK
;
652 if (!(pml4e
& PG_ACCESSED_MASK
)) {
653 pml4e
|= PG_ACCESSED_MASK
;
654 stl_phys_notdirty(pml4e_addr
, pml4e
);
656 ptep
= pml4e
^ PG_NX_MASK
;
657 pdpe_addr
= ((pml4e
& PHYS_ADDR_MASK
) + (((addr
>> 30) & 0x1ff) << 3)) &
659 pdpe
= ldq_phys(pdpe_addr
);
660 if (!(pdpe
& PG_PRESENT_MASK
)) {
664 if (!(env
->efer
& MSR_EFER_NXE
) && (pdpe
& PG_NX_MASK
)) {
665 error_code
= PG_ERROR_RSVD_MASK
;
668 ptep
&= pdpe
^ PG_NX_MASK
;
669 if (!(pdpe
& PG_ACCESSED_MASK
)) {
670 pdpe
|= PG_ACCESSED_MASK
;
671 stl_phys_notdirty(pdpe_addr
, pdpe
);
676 /* XXX: load them when cr3 is loaded ? */
677 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
679 pdpe
= ldq_phys(pdpe_addr
);
680 if (!(pdpe
& PG_PRESENT_MASK
)) {
684 ptep
= PG_NX_MASK
| PG_USER_MASK
| PG_RW_MASK
;
687 pde_addr
= ((pdpe
& PHYS_ADDR_MASK
) + (((addr
>> 21) & 0x1ff) << 3)) &
689 pde
= ldq_phys(pde_addr
);
690 if (!(pde
& PG_PRESENT_MASK
)) {
694 if (!(env
->efer
& MSR_EFER_NXE
) && (pde
& PG_NX_MASK
)) {
695 error_code
= PG_ERROR_RSVD_MASK
;
698 ptep
&= pde
^ PG_NX_MASK
;
699 if (pde
& PG_PSE_MASK
) {
701 page_size
= 2048 * 1024;
703 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
704 goto do_fault_protect
;
706 if (!(ptep
& PG_USER_MASK
))
707 goto do_fault_protect
;
708 if (is_write
&& !(ptep
& PG_RW_MASK
))
709 goto do_fault_protect
;
711 if ((env
->cr
[0] & CR0_WP_MASK
) &&
712 is_write
&& !(ptep
& PG_RW_MASK
))
713 goto do_fault_protect
;
715 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
716 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
717 pde
|= PG_ACCESSED_MASK
;
719 pde
|= PG_DIRTY_MASK
;
720 stl_phys_notdirty(pde_addr
, pde
);
722 /* align to page_size */
723 pte
= pde
& ((PHYS_ADDR_MASK
& ~(page_size
- 1)) | 0xfff);
724 virt_addr
= addr
& ~(page_size
- 1);
727 if (!(pde
& PG_ACCESSED_MASK
)) {
728 pde
|= PG_ACCESSED_MASK
;
729 stl_phys_notdirty(pde_addr
, pde
);
731 pte_addr
= ((pde
& PHYS_ADDR_MASK
) + (((addr
>> 12) & 0x1ff) << 3)) &
733 pte
= ldq_phys(pte_addr
);
734 if (!(pte
& PG_PRESENT_MASK
)) {
738 if (!(env
->efer
& MSR_EFER_NXE
) && (pte
& PG_NX_MASK
)) {
739 error_code
= PG_ERROR_RSVD_MASK
;
742 /* combine pde and pte nx, user and rw protections */
743 ptep
&= pte
^ PG_NX_MASK
;
745 if ((ptep
& PG_NX_MASK
) && is_write1
== 2)
746 goto do_fault_protect
;
748 if (!(ptep
& PG_USER_MASK
))
749 goto do_fault_protect
;
750 if (is_write
&& !(ptep
& PG_RW_MASK
))
751 goto do_fault_protect
;
753 if ((env
->cr
[0] & CR0_WP_MASK
) &&
754 is_write
&& !(ptep
& PG_RW_MASK
))
755 goto do_fault_protect
;
757 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
758 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
759 pte
|= PG_ACCESSED_MASK
;
761 pte
|= PG_DIRTY_MASK
;
762 stl_phys_notdirty(pte_addr
, pte
);
765 virt_addr
= addr
& ~0xfff;
766 pte
= pte
& (PHYS_ADDR_MASK
| 0xfff);
771 /* page directory entry */
772 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) &
774 pde
= ldl_phys(pde_addr
);
775 if (!(pde
& PG_PRESENT_MASK
)) {
779 /* if PSE bit is set, then we use a 4MB page */
780 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
781 page_size
= 4096 * 1024;
783 if (!(pde
& PG_USER_MASK
))
784 goto do_fault_protect
;
785 if (is_write
&& !(pde
& PG_RW_MASK
))
786 goto do_fault_protect
;
788 if ((env
->cr
[0] & CR0_WP_MASK
) &&
789 is_write
&& !(pde
& PG_RW_MASK
))
790 goto do_fault_protect
;
792 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
793 if (!(pde
& PG_ACCESSED_MASK
) || is_dirty
) {
794 pde
|= PG_ACCESSED_MASK
;
796 pde
|= PG_DIRTY_MASK
;
797 stl_phys_notdirty(pde_addr
, pde
);
800 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
802 virt_addr
= addr
& ~(page_size
- 1);
804 if (!(pde
& PG_ACCESSED_MASK
)) {
805 pde
|= PG_ACCESSED_MASK
;
806 stl_phys_notdirty(pde_addr
, pde
);
809 /* page directory entry */
810 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) &
812 pte
= ldl_phys(pte_addr
);
813 if (!(pte
& PG_PRESENT_MASK
)) {
817 /* combine pde and pte user and rw protections */
820 if (!(ptep
& PG_USER_MASK
))
821 goto do_fault_protect
;
822 if (is_write
&& !(ptep
& PG_RW_MASK
))
823 goto do_fault_protect
;
825 if ((env
->cr
[0] & CR0_WP_MASK
) &&
826 is_write
&& !(ptep
& PG_RW_MASK
))
827 goto do_fault_protect
;
829 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
830 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
831 pte
|= PG_ACCESSED_MASK
;
833 pte
|= PG_DIRTY_MASK
;
834 stl_phys_notdirty(pte_addr
, pte
);
837 virt_addr
= addr
& ~0xfff;
840 /* the page can be put in the TLB */
842 if (!(ptep
& PG_NX_MASK
))
844 if (pte
& PG_DIRTY_MASK
) {
845 /* only set write access if already dirty... otherwise wait
848 if (ptep
& PG_RW_MASK
)
851 if (!(env
->cr
[0] & CR0_WP_MASK
) ||
857 pte
= pte
& env
->a20_mask
;
859 /* Even if 4MB pages, we map only one 4KB page in the cache to
860 avoid filling it too fast */
861 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
862 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
863 vaddr
= virt_addr
+ page_offset
;
865 ret
= tlb_set_page_exec(env
, vaddr
, paddr
, prot
, is_user
, is_softmmu
);
868 error_code
= PG_ERROR_P_MASK
;
870 error_code
|= (is_write
<< PG_ERROR_W_BIT
);
872 error_code
|= PG_ERROR_U_MASK
;
873 if (is_write1
== 2 &&
874 (env
->efer
& MSR_EFER_NXE
) &&
875 (env
->cr
[4] & CR4_PAE_MASK
))
876 error_code
|= PG_ERROR_I_D_MASK
;
877 if (INTERCEPTEDl(_exceptions
, 1 << EXCP0E_PAGE
)) {
878 stq_phys(env
->vm_vmcb
+ offsetof(struct vmcb
, control
.exit_info_2
), addr
);
882 env
->error_code
= error_code
;
883 env
->exception_index
= EXCP0E_PAGE
;
884 /* the VMM will handle this */
885 if (INTERCEPTEDl(_exceptions
, 1 << EXCP0E_PAGE
))
890 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
892 uint32_t pde_addr
, pte_addr
;
893 uint32_t pde
, pte
, paddr
, page_offset
, page_size
;
895 if (env
->cr
[4] & CR4_PAE_MASK
) {
896 uint32_t pdpe_addr
, pde_addr
, pte_addr
;
899 /* XXX: we only use 32 bit physical addresses */
901 if (env
->hflags
& HF_LMA_MASK
) {
902 uint32_t pml4e_addr
, pml4e
;
905 /* test virtual address sign extension */
906 sext
= (int64_t)addr
>> 47;
907 if (sext
!= 0 && sext
!= -1)
910 pml4e_addr
= ((env
->cr
[3] & ~0xfff) + (((addr
>> 39) & 0x1ff) << 3)) &
912 pml4e
= ldl_phys(pml4e_addr
);
913 if (!(pml4e
& PG_PRESENT_MASK
))
916 pdpe_addr
= ((pml4e
& ~0xfff) + (((addr
>> 30) & 0x1ff) << 3)) &
918 pdpe
= ldl_phys(pdpe_addr
);
919 if (!(pdpe
& PG_PRESENT_MASK
))
924 pdpe_addr
= ((env
->cr
[3] & ~0x1f) + ((addr
>> 27) & 0x18)) &
926 pdpe
= ldl_phys(pdpe_addr
);
927 if (!(pdpe
& PG_PRESENT_MASK
))
931 pde_addr
= ((pdpe
& ~0xfff) + (((addr
>> 21) & 0x1ff) << 3)) &
933 pde
= ldl_phys(pde_addr
);
934 if (!(pde
& PG_PRESENT_MASK
)) {
937 if (pde
& PG_PSE_MASK
) {
939 page_size
= 2048 * 1024;
940 pte
= pde
& ~( (page_size
- 1) & ~0xfff); /* align to page_size */
943 pte_addr
= ((pde
& ~0xfff) + (((addr
>> 12) & 0x1ff) << 3)) &
946 pte
= ldl_phys(pte_addr
);
949 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
953 /* page directory entry */
954 pde_addr
= ((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & 0xffc)) & env
->a20_mask
;
955 pde
= ldl_phys(pde_addr
);
956 if (!(pde
& PG_PRESENT_MASK
))
958 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
959 pte
= pde
& ~0x003ff000; /* align to 4MB */
960 page_size
= 4096 * 1024;
962 /* page directory entry */
963 pte_addr
= ((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) & env
->a20_mask
;
964 pte
= ldl_phys(pte_addr
);
965 if (!(pte
& PG_PRESENT_MASK
))
970 pte
= pte
& env
->a20_mask
;
973 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
974 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
977 #endif /* !CONFIG_USER_ONLY */
979 #if defined(USE_CODE_COPY)
992 uint8_t fpregs1
[8 * 10];
995 void restore_native_fp_state(CPUState
*env
)
998 struct fpstate fp1
, *fp
= &fp1
;
1000 fp
->fpuc
= env
->fpuc
;
1001 fp
->fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1003 for (i
=7; i
>=0; i
--) {
1005 if (env
->fptags
[i
]) {
1008 /* the FPU automatically computes it */
1013 for(i
= 0;i
< 8; i
++) {
1014 memcpy(&fp
->fpregs1
[i
* 10], &env
->fpregs
[j
].d
, 10);
1017 asm volatile ("frstor %0" : "=m" (*fp
));
1018 env
->native_fp_regs
= 1;
1021 void save_native_fp_state(CPUState
*env
)
1025 struct fpstate fp1
, *fp
= &fp1
;
1027 asm volatile ("fsave %0" : : "m" (*fp
));
1028 env
->fpuc
= fp
->fpuc
;
1029 env
->fpstt
= (fp
->fpus
>> 11) & 7;
1030 env
->fpus
= fp
->fpus
& ~0x3800;
1032 for(i
= 0;i
< 8; i
++) {
1033 env
->fptags
[i
] = ((fptag
& 3) == 3);
1037 for(i
= 0;i
< 8; i
++) {
1038 memcpy(&env
->fpregs
[j
].d
, &fp
->fpregs1
[i
* 10], 10);
1041 /* we must restore the default rounding state */
1042 /* XXX: we do not restore the exception state */
1043 fpuc
= 0x037f | (env
->fpuc
& (3 << 10));
1044 asm volatile("fldcw %0" : : "m" (fpuc
));
1045 env
->native_fp_regs
= 0;