]>
git.proxmox.com Git - qemu.git/blob - exec-i386.c
2 * i386 emulator main execution loop
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "exec-i386.h"
25 /* main execution loop */
27 /* maximum total translate dcode allocated */
28 #define CODE_GEN_BUFFER_SIZE (2048 * 1024)
29 //#define CODE_GEN_BUFFER_SIZE (128 * 1024)
30 #define CODE_GEN_MAX_SIZE 65536
31 #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
33 /* threshold to flush the translated code buffer */
34 #define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE)
36 #define CODE_GEN_MAX_BLOCKS (CODE_GEN_BUFFER_SIZE / 64)
37 #define CODE_GEN_HASH_BITS 15
38 #define CODE_GEN_HASH_SIZE (1 << CODE_GEN_HASH_BITS)
40 typedef struct TranslationBlock
{
41 unsigned long pc
; /* simulated PC corresponding to this block */
42 unsigned int flags
; /* flags defining in which context the code was generated */
43 uint8_t *tc_ptr
; /* pointer to the translated code */
44 struct TranslationBlock
*hash_next
; /* next matching block */
47 TranslationBlock tbs
[CODE_GEN_MAX_BLOCKS
];
48 TranslationBlock
*tb_hash
[CODE_GEN_HASH_SIZE
];
51 uint8_t code_gen_buffer
[CODE_GEN_BUFFER_SIZE
];
52 uint8_t *code_gen_ptr
;
55 static const char *cc_op_str
[] = {
88 static void cpu_x86_dump_state(void)
91 eflags
= cc_table
[CC_OP
].compute_all();
92 eflags
|= (DF
& DIRECTION_FLAG
);
94 "EAX=%08x EBX=%08X ECX=%08x EDX=%08x\n"
95 "ESI=%08x EDI=%08X EBP=%08x ESP=%08x\n"
96 "CCS=%08x CCD=%08x CCO=%-8s EFL=%c%c%c%c%c%c%c\n",
97 env
->regs
[R_EAX
], env
->regs
[R_EBX
], env
->regs
[R_ECX
], env
->regs
[R_EDX
],
98 env
->regs
[R_ESI
], env
->regs
[R_EDI
], env
->regs
[R_EBP
], env
->regs
[R_ESP
],
99 env
->cc_src
, env
->cc_dst
, cc_op_str
[env
->cc_op
],
100 eflags
& DIRECTION_FLAG
? 'D' : '-',
101 eflags
& CC_O
? 'O' : '-',
102 eflags
& CC_S
? 'S' : '-',
103 eflags
& CC_Z
? 'Z' : '-',
104 eflags
& CC_A
? 'A' : '-',
105 eflags
& CC_P
? 'P' : '-',
106 eflags
& CC_C
? 'C' : '-'
109 fprintf(logfile
, "ST0=%f ST1=%f ST2=%f ST3=%f\n",
110 (double)ST0
, (double)ST1
, (double)ST(2), (double)ST(3));
116 void cpu_x86_tblocks_init(void)
119 code_gen_ptr
= code_gen_buffer
;
123 /* flush all the translation blocks */
124 static void tb_flush(void)
128 printf("gemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n",
129 code_gen_ptr
- code_gen_buffer
,
131 (code_gen_ptr
- code_gen_buffer
) / nb_tbs
);
134 for(i
= 0;i
< CODE_GEN_HASH_SIZE
; i
++)
136 code_gen_ptr
= code_gen_buffer
;
137 /* XXX: flush processor icache at this point */
140 /* find a translation block in the translation cache. If not found,
141 allocate a new one */
142 static inline TranslationBlock
*tb_find_and_alloc(unsigned long pc
,
145 TranslationBlock
**ptb
, *tb
;
148 h
= pc
& (CODE_GEN_HASH_SIZE
- 1);
154 if (tb
->pc
== pc
&& tb
->flags
== flags
)
156 ptb
= &tb
->hash_next
;
158 if (nb_tbs
>= CODE_GEN_MAX_BLOCKS
||
159 (code_gen_ptr
- code_gen_buffer
) >= CODE_GEN_BUFFER_MAX_SIZE
)
166 tb
->hash_next
= NULL
;
170 int cpu_x86_exec(CPUX86State
*env1
)
172 int saved_T0
, saved_T1
, saved_A0
;
173 CPUX86State
*saved_env
;
174 int code_gen_size
, ret
;
175 void (*gen_func
)(void);
176 TranslationBlock
*tb
;
180 /* first we save global registers */
187 /* prepare setjmp context for exception handling */
188 if (setjmp(env
->jmp_env
) == 0) {
192 cpu_x86_dump_state();
195 /* we compute the CPU state. We assume it will not
196 change during the whole generated block. */
197 flags
= env
->seg_cache
[R_CS
].seg_32bit
<< GEN_FLAG_CODE32_SHIFT
;
198 flags
|= (((unsigned long)env
->seg_cache
[R_DS
].base
|
199 (unsigned long)env
->seg_cache
[R_ES
].base
|
200 (unsigned long)env
->seg_cache
[R_SS
].base
) != 0) <<
201 GEN_FLAG_ADDSEG_SHIFT
;
202 tb
= tb_find_and_alloc((unsigned long)env
->pc
, flags
);
205 /* if no translated code available, then translate it now */
206 tc_ptr
= code_gen_ptr
;
207 cpu_x86_gen_code(code_gen_ptr
, CODE_GEN_MAX_SIZE
,
208 &code_gen_size
, (uint8_t *)env
->pc
, flags
);
210 code_gen_ptr
= (void *)(((unsigned long)code_gen_ptr
+ code_gen_size
+ CODE_GEN_ALIGN
- 1) & ~(CODE_GEN_ALIGN
- 1));
212 /* execute the generated code */
213 gen_func
= (void *)tc_ptr
;
217 ret
= env
->exception_index
;
219 /* restore global registers */
227 void cpu_x86_load_seg(CPUX86State
*s
, int seg_reg
, int selector
)
229 CPUX86State
*saved_env
;
233 load_seg(seg_reg
, selector
);