]>
git.proxmox.com Git - qemu.git/blob - target-i386/helper2.c
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
34 CPUX86State
*cpu_x86_init(void)
42 env
= malloc(sizeof(CPUX86State
));
45 memset(env
, 0, sizeof(CPUX86State
));
50 /* flags setup : we activate the IRQs by default as in user mode */
51 env
->eflags
= 0x2 | IF_MASK
;
55 env
->hflags
|= HF_SOFTMMU_MASK
;
57 /* init various static tables */
60 optimize_flags_init();
65 void cpu_x86_close(CPUX86State
*env
)
70 /***********************************************************/
73 static const char *cc_op_str
[] = {
106 void cpu_x86_dump_state(CPUX86State
*env
, FILE *f
, int flags
)
110 static const char *seg_name
[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
112 eflags
= env
->eflags
;
113 fprintf(f
, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
114 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
115 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d\n",
116 env
->regs
[R_EAX
], env
->regs
[R_EBX
], env
->regs
[R_ECX
], env
->regs
[R_EDX
],
117 env
->regs
[R_ESI
], env
->regs
[R_EDI
], env
->regs
[R_EBP
], env
->regs
[R_ESP
],
119 eflags
& DF_MASK
? 'D' : '-',
120 eflags
& CC_O
? 'O' : '-',
121 eflags
& CC_S
? 'S' : '-',
122 eflags
& CC_Z
? 'Z' : '-',
123 eflags
& CC_A
? 'A' : '-',
124 eflags
& CC_P
? 'P' : '-',
125 eflags
& CC_C
? 'C' : '-',
126 env
->hflags
& HF_CPL_MASK
);
127 for(i
= 0; i
< 6; i
++) {
128 SegmentCache
*sc
= &env
->segs
[i
];
129 fprintf(f
, "%s =%04x %08x %08x %08x\n",
136 fprintf(f
, "LDT=%04x %08x %08x %08x\n",
141 fprintf(f
, "TR =%04x %08x %08x %08x\n",
146 fprintf(f
, "GDT= %08x %08x\n",
147 (int)env
->gdt
.base
, env
->gdt
.limit
);
148 fprintf(f
, "IDT= %08x %08x\n",
149 (int)env
->idt
.base
, env
->idt
.limit
);
150 fprintf(f
, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
151 env
->cr
[0], env
->cr
[2], env
->cr
[3], env
->cr
[4]);
153 if (flags
& X86_DUMP_CCOP
) {
154 if ((unsigned)env
->cc_op
< CC_OP_NB
)
155 strcpy(cc_op_name
, cc_op_str
[env
->cc_op
]);
157 snprintf(cc_op_name
, sizeof(cc_op_name
), "[%d]", env
->cc_op
);
158 fprintf(f
, "CCS=%08x CCD=%08x CCO=%-8s\n",
159 env
->cc_src
, env
->cc_dst
, cc_op_name
);
161 if (flags
& X86_DUMP_FPU
) {
162 fprintf(f
, "ST0=%f ST1=%f ST2=%f ST3=%f\n",
163 (double)env
->fpregs
[0],
164 (double)env
->fpregs
[1],
165 (double)env
->fpregs
[2],
166 (double)env
->fpregs
[3]);
167 fprintf(f
, "ST4=%f ST5=%f ST6=%f ST7=%f\n",
168 (double)env
->fpregs
[4],
169 (double)env
->fpregs
[5],
170 (double)env
->fpregs
[7],
171 (double)env
->fpregs
[8]);
175 /***********************************************************/
177 /* XXX: add PGE support */
179 /* called when cr3 or PG bit are modified */
180 static int last_pg_state
= -1;
181 static int last_pe_state
= 0;
182 static uint32_t a20_mask
;
187 uint8_t *phys_ram_base
;
189 void cpu_x86_set_a20(CPUX86State
*env
, int a20_state
)
191 a20_state
= (a20_state
!= 0);
192 if (a20_state
!= a20_enabled
) {
193 /* when a20 is changed, all the MMU mappings are invalid, so
194 we must flush everything */
197 a20_enabled
= a20_state
;
199 a20_mask
= 0xffffffff;
201 a20_mask
= 0xffefffff;
205 void cpu_x86_update_cr0(CPUX86State
*env
)
207 int pg_state
, pe_state
;
210 printf("CR0 update: CR0=0x%08x\n", env
->cr
[0]);
212 pg_state
= env
->cr
[0] & CR0_PG_MASK
;
213 if (pg_state
!= last_pg_state
) {
216 last_pg_state
= pg_state
;
218 pe_state
= env
->cr
[0] & CR0_PE_MASK
;
219 if (last_pe_state
!= pe_state
) {
221 last_pe_state
= pe_state
;
225 void cpu_x86_update_cr3(CPUX86State
*env
)
227 if (env
->cr
[0] & CR0_PG_MASK
) {
228 #if defined(DEBUG_MMU)
229 printf("CR3 update: CR3=%08x\n", env
->cr
[3]);
236 void cpu_x86_init_mmu(CPUX86State
*env
)
239 a20_mask
= 0xffffffff;
242 cpu_x86_update_cr0(env
);
245 /* XXX: also flush 4MB pages */
246 void cpu_x86_flush_tlb(CPUX86State
*env
, uint32_t addr
)
249 unsigned long virt_addr
;
251 tlb_flush_page(env
, addr
);
253 flags
= page_get_flags(addr
);
254 if (flags
& PAGE_VALID
) {
255 virt_addr
= addr
& ~0xfff;
256 #if !defined(CONFIG_SOFTMMU)
257 munmap((void *)virt_addr
, 4096);
259 page_set_flags(virt_addr
, virt_addr
+ 4096, 0);
264 -1 = cannot handle fault
265 0 = nothing more to do
266 1 = generate PF fault
267 2 = soft MMU activation required for this block
269 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, uint32_t addr
,
270 int is_write
, int is_user
, int is_softmmu
)
272 uint8_t *pde_ptr
, *pte_ptr
;
273 uint32_t pde
, pte
, virt_addr
;
274 int error_code
, is_dirty
, prot
, page_size
, ret
;
278 printf("MMU fault: addr=0x%08x w=%d u=%d eip=%08x\n",
279 addr
, is_write
, is_user
, env
->eip
);
282 if (env
->user_mode_only
) {
283 /* user mode only emulation */
288 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
290 virt_addr
= addr
& TARGET_PAGE_MASK
;
291 prot
= PROT_READ
| PROT_WRITE
;
296 /* page directory entry */
297 pde_ptr
= phys_ram_base
+
298 (((env
->cr
[3] & ~0xfff) + ((addr
>> 20) & ~3)) & a20_mask
);
299 pde
= ldl_raw(pde_ptr
);
300 if (!(pde
& PG_PRESENT_MASK
)) {
305 if (!(pde
& PG_USER_MASK
))
306 goto do_fault_protect
;
307 if (is_write
&& !(pde
& PG_RW_MASK
))
308 goto do_fault_protect
;
310 if ((env
->cr
[0] & CR0_WP_MASK
) && (pde
& PG_USER_MASK
) &&
311 is_write
&& !(pde
& PG_RW_MASK
))
312 goto do_fault_protect
;
314 /* if PSE bit is set, then we use a 4MB page */
315 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
316 is_dirty
= is_write
&& !(pde
& PG_DIRTY_MASK
);
317 if (!(pde
& PG_ACCESSED_MASK
)) {
318 pde
|= PG_ACCESSED_MASK
;
320 pde
|= PG_DIRTY_MASK
;
321 stl_raw(pde_ptr
, pde
);
324 pte
= pde
& ~0x003ff000; /* align to 4MB */
325 page_size
= 4096 * 1024;
326 virt_addr
= addr
& ~0x003fffff;
328 if (!(pde
& PG_ACCESSED_MASK
)) {
329 pde
|= PG_ACCESSED_MASK
;
330 stl_raw(pde_ptr
, pde
);
333 /* page directory entry */
334 pte_ptr
= phys_ram_base
+
335 (((pde
& ~0xfff) + ((addr
>> 10) & 0xffc)) & a20_mask
);
336 pte
= ldl_raw(pte_ptr
);
337 if (!(pte
& PG_PRESENT_MASK
)) {
342 if (!(pte
& PG_USER_MASK
))
343 goto do_fault_protect
;
344 if (is_write
&& !(pte
& PG_RW_MASK
))
345 goto do_fault_protect
;
347 if ((env
->cr
[0] & CR0_WP_MASK
) && (pte
& PG_USER_MASK
) &&
348 is_write
&& !(pte
& PG_RW_MASK
))
349 goto do_fault_protect
;
351 is_dirty
= is_write
&& !(pte
& PG_DIRTY_MASK
);
352 if (!(pte
& PG_ACCESSED_MASK
) || is_dirty
) {
353 pte
|= PG_ACCESSED_MASK
;
355 pte
|= PG_DIRTY_MASK
;
356 stl_raw(pte_ptr
, pte
);
359 virt_addr
= addr
& ~0xfff;
361 /* the page can be put in the TLB */
364 if (pte
& PG_RW_MASK
)
367 if (!(env
->cr
[0] & CR0_WP_MASK
) || !(pte
& PG_USER_MASK
) ||
373 pte
= pte
& a20_mask
;
374 #if !defined(CONFIG_SOFTMMU)
378 unsigned long paddr
, vaddr
, address
, addend
, page_offset
;
381 /* software MMU case. Even if 4MB pages, we map only one 4KB
382 page in the cache to avoid filling it too fast */
383 page_offset
= (addr
& TARGET_PAGE_MASK
) & (page_size
- 1);
384 paddr
= (pte
& TARGET_PAGE_MASK
) + page_offset
;
385 vaddr
= virt_addr
+ page_offset
;
386 index
= (addr
>> 12) & (CPU_TLB_SIZE
- 1);
387 pd
= physpage_find(paddr
);
390 address
= vaddr
| pd
;
393 /* standard memory */
395 addend
= (unsigned long)phys_ram_base
+ pd
;
398 env
->tlb_read
[is_user
][index
].address
= address
;
399 env
->tlb_read
[is_user
][index
].addend
= addend
;
400 if (prot
& PROT_WRITE
) {
401 env
->tlb_write
[is_user
][index
].address
= address
;
402 env
->tlb_write
[is_user
][index
].addend
= addend
;
404 env
->tlb_write
[is_user
][index
].address
= -1;
405 env
->tlb_write
[is_user
][index
].addend
= -1;
407 page_set_flags(vaddr
, vaddr
+ TARGET_PAGE_SIZE
,
408 PAGE_VALID
| PAGE_EXEC
| prot
);
411 #if !defined(CONFIG_SOFTMMU)
414 /* XXX: incorrect for 4MB pages */
415 pd
= physpage_find(pte
& ~0xfff);
416 if ((pd
& 0xfff) != 0) {
417 /* IO access: no mapping is done as it will be handled by the
419 if (!(env
->hflags
& HF_SOFTMMU_MASK
))
423 map_addr
= mmap((void *)virt_addr
, page_size
, prot
,
424 MAP_SHARED
| MAP_FIXED
, phys_ram_fd
, pd
);
425 if (map_addr
== MAP_FAILED
) {
427 "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
428 pte
& ~0xfff, virt_addr
);
432 printf("mmaping 0x%08x to virt 0x%08x pse=%d\n",
433 pte
& ~0xfff, virt_addr
, (page_size
!= 4096));
435 page_set_flags(virt_addr
, virt_addr
+ page_size
,
436 PAGE_VALID
| PAGE_EXEC
| prot
);
442 error_code
= PG_ERROR_P_MASK
;
445 env
->error_code
= (is_write
<< PG_ERROR_W_BIT
) | error_code
;
447 env
->error_code
|= PG_ERROR_U_MASK
;