]> git.proxmox.com Git - mirror_qemu.git/blob - exec-i386.c
added minimal segment support
[mirror_qemu.git] / exec-i386.c
1 /*
2 * i386 emulator main execution loop
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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.
19 */
20 #include "exec-i386.h"
21
22 #define DEBUG_EXEC
23 #define DEBUG_FLUSH
24
25 /* main execution loop */
26
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 */
32
33 /* threshold to flush the translated code buffer */
34 #define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE)
35
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)
39
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 */
45 } TranslationBlock;
46
47 TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
48 TranslationBlock *tb_hash[CODE_GEN_HASH_SIZE];
49 int nb_tbs;
50
51 uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE];
52 uint8_t *code_gen_ptr;
53
54 #ifdef DEBUG_EXEC
55 static const char *cc_op_str[] = {
56 "DYNAMIC",
57 "EFLAGS",
58 "MUL",
59 "ADDB",
60 "ADDW",
61 "ADDL",
62 "ADCB",
63 "ADCW",
64 "ADCL",
65 "SUBB",
66 "SUBW",
67 "SUBL",
68 "SBBB",
69 "SBBW",
70 "SBBL",
71 "LOGICB",
72 "LOGICW",
73 "LOGICL",
74 "INCB",
75 "INCW",
76 "INCL",
77 "DECB",
78 "DECW",
79 "DECL",
80 "SHLB",
81 "SHLW",
82 "SHLL",
83 "SARB",
84 "SARW",
85 "SARL",
86 };
87
88 static void cpu_x86_dump_state(void)
89 {
90 int eflags;
91 eflags = cc_table[CC_OP].compute_all();
92 eflags |= (DF & DIRECTION_FLAG);
93 fprintf(logfile,
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' : '-'
107 );
108 #if 1
109 fprintf(logfile, "ST0=%f ST1=%f ST2=%f ST3=%f\n",
110 (double)ST0, (double)ST1, (double)ST(2), (double)ST(3));
111 #endif
112 }
113
114 #endif
115
116 void cpu_x86_tblocks_init(void)
117 {
118 if (!code_gen_ptr) {
119 code_gen_ptr = code_gen_buffer;
120 }
121 }
122
123 /* flush all the translation blocks */
124 static void tb_flush(void)
125 {
126 int i;
127 #ifdef DEBUG_FLUSH
128 printf("gemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n",
129 code_gen_ptr - code_gen_buffer,
130 nb_tbs,
131 (code_gen_ptr - code_gen_buffer) / nb_tbs);
132 #endif
133 nb_tbs = 0;
134 for(i = 0;i < CODE_GEN_HASH_SIZE; i++)
135 tb_hash[i] = NULL;
136 code_gen_ptr = code_gen_buffer;
137 /* XXX: flush processor icache at this point */
138 }
139
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,
143 unsigned int flags)
144 {
145 TranslationBlock **ptb, *tb;
146 unsigned int h;
147
148 h = pc & (CODE_GEN_HASH_SIZE - 1);
149 ptb = &tb_hash[h];
150 for(;;) {
151 tb = *ptb;
152 if (!tb)
153 break;
154 if (tb->pc == pc && tb->flags == flags)
155 return tb;
156 ptb = &tb->hash_next;
157 }
158 if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
159 (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
160 tb_flush();
161 tb = &tbs[nb_tbs++];
162 *ptb = tb;
163 tb->pc = pc;
164 tb->flags = flags;
165 tb->tc_ptr = NULL;
166 tb->hash_next = NULL;
167 return tb;
168 }
169
170 int cpu_x86_exec(CPUX86State *env1)
171 {
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;
177 uint8_t *tc_ptr;
178 unsigned int flags;
179
180 /* first we save global registers */
181 saved_T0 = T0;
182 saved_T1 = T1;
183 saved_A0 = A0;
184 saved_env = env;
185 env = env1;
186
187 /* prepare setjmp context for exception handling */
188 if (setjmp(env->jmp_env) == 0) {
189 for(;;) {
190 #ifdef DEBUG_EXEC
191 if (loglevel) {
192 cpu_x86_dump_state();
193 }
194 #endif
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);
203 tc_ptr = tb->tc_ptr;
204 if (!tb->tc_ptr) {
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);
209 tb->tc_ptr = tc_ptr;
210 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
211 }
212 /* execute the generated code */
213 gen_func = (void *)tc_ptr;
214 gen_func();
215 }
216 }
217 ret = env->exception_index;
218
219 /* restore global registers */
220 T0 = saved_T0;
221 T1 = saved_T1;
222 A0 = saved_A0;
223 env = saved_env;
224 return ret;
225 }
226
227 void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
228 {
229 CPUX86State *saved_env;
230
231 saved_env = env;
232 env = s;
233 load_seg(seg_reg, selector);
234 env = saved_env;
235 }