root / exec.h @ 4021dab0
History | View | Annotate | Download (9.9 kB)
1 | d4e8164f | bellard | /*
|
---|---|---|---|
2 | d4e8164f | bellard | * internal execution defines for qemu
|
3 | d4e8164f | bellard | *
|
4 | d4e8164f | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | d4e8164f | bellard | *
|
6 | d4e8164f | bellard | * This library is free software; you can redistribute it and/or
|
7 | d4e8164f | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | d4e8164f | bellard | * License as published by the Free Software Foundation; either
|
9 | d4e8164f | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | d4e8164f | bellard | *
|
11 | d4e8164f | bellard | * This library is distributed in the hope that it will be useful,
|
12 | d4e8164f | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | d4e8164f | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | d4e8164f | bellard | * Lesser General Public License for more details.
|
15 | d4e8164f | bellard | *
|
16 | d4e8164f | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | d4e8164f | bellard | * License along with this library; if not, write to the Free Software
|
18 | d4e8164f | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | d4e8164f | bellard | */
|
20 | d4e8164f | bellard | |
21 | b346ff46 | bellard | /* allow to see translation results - the slowdown should be negligible, so we leave it */
|
22 | b346ff46 | bellard | #define DEBUG_DISAS
|
23 | b346ff46 | bellard | |
24 | b346ff46 | bellard | /* is_jmp field values */
|
25 | b346ff46 | bellard | #define DISAS_NEXT 0 /* next instruction can be analyzed */ |
26 | b346ff46 | bellard | #define DISAS_JUMP 1 /* only pc was modified dynamically */ |
27 | b346ff46 | bellard | #define DISAS_UPDATE 2 /* cpu state was modified dynamically */ |
28 | b346ff46 | bellard | #define DISAS_TB_JUMP 3 /* only pc was modified statically */ |
29 | b346ff46 | bellard | |
30 | b346ff46 | bellard | struct TranslationBlock;
|
31 | b346ff46 | bellard | |
32 | b346ff46 | bellard | /* XXX: make safe guess about sizes */
|
33 | b346ff46 | bellard | #define MAX_OP_PER_INSTR 32 |
34 | b346ff46 | bellard | #define OPC_BUF_SIZE 512 |
35 | b346ff46 | bellard | #define OPC_MAX_SIZE (OPC_BUF_SIZE - MAX_OP_PER_INSTR)
|
36 | b346ff46 | bellard | |
37 | b346ff46 | bellard | #define OPPARAM_BUF_SIZE (OPC_BUF_SIZE * 3) |
38 | b346ff46 | bellard | |
39 | b346ff46 | bellard | extern uint16_t gen_opc_buf[OPC_BUF_SIZE];
|
40 | b346ff46 | bellard | extern uint32_t gen_opparam_buf[OPPARAM_BUF_SIZE];
|
41 | b346ff46 | bellard | extern uint32_t gen_opc_pc[OPC_BUF_SIZE];
|
42 | 66e85a21 | bellard | extern uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
|
43 | b346ff46 | bellard | extern uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
|
44 | b346ff46 | bellard | |
45 | b346ff46 | bellard | #if defined(TARGET_I386)
|
46 | b346ff46 | bellard | |
47 | d4e8164f | bellard | #define GEN_FLAG_CODE32_SHIFT 0 |
48 | d4e8164f | bellard | #define GEN_FLAG_ADDSEG_SHIFT 1 |
49 | d4e8164f | bellard | #define GEN_FLAG_SS32_SHIFT 2 |
50 | d4e8164f | bellard | #define GEN_FLAG_VM_SHIFT 3 |
51 | d4e8164f | bellard | #define GEN_FLAG_ST_SHIFT 4 |
52 | cf25629d | bellard | #define GEN_FLAG_TF_SHIFT 8 /* same position as eflags */ |
53 | cf25629d | bellard | #define GEN_FLAG_CPL_SHIFT 9 |
54 | cf25629d | bellard | #define GEN_FLAG_IOPL_SHIFT 12 /* same position as eflags */ |
55 | d4e8164f | bellard | |
56 | b346ff46 | bellard | #endif
|
57 | b346ff46 | bellard | |
58 | b346ff46 | bellard | extern FILE *logfile;
|
59 | b346ff46 | bellard | extern int loglevel; |
60 | b346ff46 | bellard | |
61 | 4c3a88a2 | bellard | int gen_intermediate_code(CPUState *env, struct TranslationBlock *tb); |
62 | 4c3a88a2 | bellard | int gen_intermediate_code_pc(CPUState *env, struct TranslationBlock *tb); |
63 | b346ff46 | bellard | void dump_ops(const uint16_t *opc_buf, const uint32_t *opparam_buf); |
64 | 4c3a88a2 | bellard | int cpu_gen_code(CPUState *env, struct TranslationBlock *tb, |
65 | b346ff46 | bellard | int max_code_size, int *gen_code_size_ptr); |
66 | 66e85a21 | bellard | int cpu_restore_state(struct TranslationBlock *tb, |
67 | 66e85a21 | bellard | CPUState *env, unsigned long searched_pc); |
68 | b346ff46 | bellard | void cpu_exec_init(void); |
69 | d4e8164f | bellard | int page_unprotect(unsigned long address); |
70 | 66e85a21 | bellard | void page_unmap(void); |
71 | d4e8164f | bellard | |
72 | d4e8164f | bellard | #define CODE_GEN_MAX_SIZE 65536 |
73 | d4e8164f | bellard | #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */ |
74 | d4e8164f | bellard | |
75 | d4e8164f | bellard | #define CODE_GEN_HASH_BITS 15 |
76 | d4e8164f | bellard | #define CODE_GEN_HASH_SIZE (1 << CODE_GEN_HASH_BITS) |
77 | d4e8164f | bellard | |
78 | d4e8164f | bellard | /* maximum total translate dcode allocated */
|
79 | d4e8164f | bellard | #define CODE_GEN_BUFFER_SIZE (2048 * 1024) |
80 | d4e8164f | bellard | //#define CODE_GEN_BUFFER_SIZE (128 * 1024)
|
81 | d4e8164f | bellard | |
82 | d4e8164f | bellard | #if defined(__powerpc__)
|
83 | d4e8164f | bellard | #define USE_DIRECT_JUMP
|
84 | d4e8164f | bellard | #endif
|
85 | d4e8164f | bellard | |
86 | d4e8164f | bellard | typedef struct TranslationBlock { |
87 | d4e8164f | bellard | unsigned long pc; /* simulated PC corresponding to this block (EIP + CS base) */ |
88 | d4e8164f | bellard | unsigned long cs_base; /* CS base for this block */ |
89 | d4e8164f | bellard | unsigned int flags; /* flags defining in which context the code was generated */ |
90 | d4e8164f | bellard | uint16_t size; /* size of target code for this block (1 <=
|
91 | d4e8164f | bellard | size <= TARGET_PAGE_SIZE) */
|
92 | d4e8164f | bellard | uint8_t *tc_ptr; /* pointer to the translated code */
|
93 | d4e8164f | bellard | struct TranslationBlock *hash_next; /* next matching block */ |
94 | d4e8164f | bellard | struct TranslationBlock *page_next[2]; /* next blocks in even/odd page */ |
95 | d4e8164f | bellard | /* the following data are used to directly call another TB from
|
96 | d4e8164f | bellard | the code of this one. */
|
97 | d4e8164f | bellard | uint16_t tb_next_offset[2]; /* offset of original jump target */ |
98 | d4e8164f | bellard | #ifdef USE_DIRECT_JUMP
|
99 | d4e8164f | bellard | uint16_t tb_jmp_offset[2]; /* offset of jump instruction */ |
100 | d4e8164f | bellard | #else
|
101 | 95f7652d | bellard | uint32_t tb_next[2]; /* address of jump generated code */ |
102 | d4e8164f | bellard | #endif
|
103 | d4e8164f | bellard | /* list of TBs jumping to this one. This is a circular list using
|
104 | d4e8164f | bellard | the two least significant bits of the pointers to tell what is
|
105 | d4e8164f | bellard | the next pointer: 0 = jmp_next[0], 1 = jmp_next[1], 2 =
|
106 | d4e8164f | bellard | jmp_first */
|
107 | d4e8164f | bellard | struct TranslationBlock *jmp_next[2]; |
108 | d4e8164f | bellard | struct TranslationBlock *jmp_first;
|
109 | d4e8164f | bellard | } TranslationBlock; |
110 | d4e8164f | bellard | |
111 | d4e8164f | bellard | static inline unsigned int tb_hash_func(unsigned long pc) |
112 | d4e8164f | bellard | { |
113 | d4e8164f | bellard | return pc & (CODE_GEN_HASH_SIZE - 1); |
114 | d4e8164f | bellard | } |
115 | d4e8164f | bellard | |
116 | d4e8164f | bellard | TranslationBlock *tb_alloc(unsigned long pc); |
117 | d4e8164f | bellard | void tb_flush(void); |
118 | d4e8164f | bellard | void tb_link(TranslationBlock *tb);
|
119 | d4e8164f | bellard | |
120 | d4e8164f | bellard | extern TranslationBlock *tb_hash[CODE_GEN_HASH_SIZE];
|
121 | d4e8164f | bellard | |
122 | d4e8164f | bellard | extern uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE];
|
123 | d4e8164f | bellard | extern uint8_t *code_gen_ptr;
|
124 | d4e8164f | bellard | |
125 | d4e8164f | bellard | /* find a translation block in the translation cache. If not found,
|
126 | d4e8164f | bellard | return NULL and the pointer to the last element of the list in pptb */
|
127 | d4e8164f | bellard | static inline TranslationBlock *tb_find(TranslationBlock ***pptb, |
128 | d4e8164f | bellard | unsigned long pc, |
129 | d4e8164f | bellard | unsigned long cs_base, |
130 | d4e8164f | bellard | unsigned int flags) |
131 | d4e8164f | bellard | { |
132 | d4e8164f | bellard | TranslationBlock **ptb, *tb; |
133 | d4e8164f | bellard | unsigned int h; |
134 | d4e8164f | bellard | |
135 | d4e8164f | bellard | h = tb_hash_func(pc); |
136 | d4e8164f | bellard | ptb = &tb_hash[h]; |
137 | d4e8164f | bellard | for(;;) {
|
138 | d4e8164f | bellard | tb = *ptb; |
139 | d4e8164f | bellard | if (!tb)
|
140 | d4e8164f | bellard | break;
|
141 | d4e8164f | bellard | if (tb->pc == pc && tb->cs_base == cs_base && tb->flags == flags)
|
142 | d4e8164f | bellard | return tb;
|
143 | d4e8164f | bellard | ptb = &tb->hash_next; |
144 | d4e8164f | bellard | } |
145 | d4e8164f | bellard | *pptb = ptb; |
146 | d4e8164f | bellard | return NULL; |
147 | d4e8164f | bellard | } |
148 | d4e8164f | bellard | |
149 | d4e8164f | bellard | #if defined(__powerpc__)
|
150 | d4e8164f | bellard | |
151 | d4e8164f | bellard | static inline void tb_set_jmp_target(TranslationBlock *tb, |
152 | d4e8164f | bellard | int n, unsigned long addr) |
153 | d4e8164f | bellard | { |
154 | d4e8164f | bellard | uint32_t val, *ptr; |
155 | d4e8164f | bellard | unsigned long offset; |
156 | d4e8164f | bellard | |
157 | d4e8164f | bellard | offset = (unsigned long)(tb->tc_ptr + tb->tb_jmp_offset[n]); |
158 | d4e8164f | bellard | |
159 | d4e8164f | bellard | /* patch the branch destination */
|
160 | d4e8164f | bellard | ptr = (uint32_t *)offset; |
161 | d4e8164f | bellard | val = *ptr; |
162 | d4e8164f | bellard | val = (val & ~0x03fffffc) | ((addr - offset) & 0x03fffffc); |
163 | d4e8164f | bellard | *ptr = val; |
164 | d4e8164f | bellard | /* flush icache */
|
165 | d4e8164f | bellard | asm volatile ("dcbst 0,%0" : : "r"(ptr) : "memory"); |
166 | d4e8164f | bellard | asm volatile ("sync" : : : "memory"); |
167 | d4e8164f | bellard | asm volatile ("icbi 0,%0" : : "r"(ptr) : "memory"); |
168 | d4e8164f | bellard | asm volatile ("sync" : : : "memory"); |
169 | d4e8164f | bellard | asm volatile ("isync" : : : "memory"); |
170 | d4e8164f | bellard | } |
171 | d4e8164f | bellard | |
172 | d4e8164f | bellard | #else
|
173 | d4e8164f | bellard | |
174 | d4e8164f | bellard | /* set the jump target */
|
175 | d4e8164f | bellard | static inline void tb_set_jmp_target(TranslationBlock *tb, |
176 | d4e8164f | bellard | int n, unsigned long addr) |
177 | d4e8164f | bellard | { |
178 | 95f7652d | bellard | tb->tb_next[n] = addr; |
179 | d4e8164f | bellard | } |
180 | d4e8164f | bellard | |
181 | d4e8164f | bellard | #endif
|
182 | d4e8164f | bellard | |
183 | d4e8164f | bellard | static inline void tb_add_jump(TranslationBlock *tb, int n, |
184 | d4e8164f | bellard | TranslationBlock *tb_next) |
185 | d4e8164f | bellard | { |
186 | cf25629d | bellard | /* NOTE: this test is only needed for thread safety */
|
187 | cf25629d | bellard | if (!tb->jmp_next[n]) {
|
188 | cf25629d | bellard | /* patch the native jump address */
|
189 | cf25629d | bellard | tb_set_jmp_target(tb, n, (unsigned long)tb_next->tc_ptr); |
190 | cf25629d | bellard | |
191 | cf25629d | bellard | /* add in TB jmp circular list */
|
192 | cf25629d | bellard | tb->jmp_next[n] = tb_next->jmp_first; |
193 | cf25629d | bellard | tb_next->jmp_first = (TranslationBlock *)((long)(tb) | (n));
|
194 | cf25629d | bellard | } |
195 | d4e8164f | bellard | } |
196 | d4e8164f | bellard | |
197 | a513fe19 | bellard | TranslationBlock *tb_find_pc(unsigned long pc_ptr); |
198 | a513fe19 | bellard | |
199 | d4e8164f | bellard | #ifndef offsetof
|
200 | d4e8164f | bellard | #define offsetof(type, field) ((size_t) &((type *)0)->field) |
201 | d4e8164f | bellard | #endif
|
202 | d4e8164f | bellard | |
203 | b346ff46 | bellard | #if defined(__powerpc__)
|
204 | b346ff46 | bellard | |
205 | b346ff46 | bellard | /* on PowerPC we patch the jump instruction directly */
|
206 | b346ff46 | bellard | #define JUMP_TB(tbparam, n, eip)\
|
207 | b346ff46 | bellard | do {\
|
208 | b346ff46 | bellard | static void __attribute__((unused)) *__op_label ## n = &&label ## n;\ |
209 | b346ff46 | bellard | asm volatile ("b %0" : : "i" (&__op_jmp ## n));\ |
210 | b346ff46 | bellard | label ## n:\ |
211 | b346ff46 | bellard | T0 = (long)(tbparam) + (n);\
|
212 | b346ff46 | bellard | EIP = eip;\ |
213 | b346ff46 | bellard | } while (0) |
214 | b346ff46 | bellard | |
215 | b346ff46 | bellard | #else
|
216 | b346ff46 | bellard | |
217 | b346ff46 | bellard | /* jump to next block operations (more portable code, does not need
|
218 | b346ff46 | bellard | cache flushing, but slower because of indirect jump) */
|
219 | b346ff46 | bellard | #define JUMP_TB(tbparam, n, eip)\
|
220 | b346ff46 | bellard | do {\
|
221 | b346ff46 | bellard | static void __attribute__((unused)) *__op_label ## n = &&label ## n;\ |
222 | 2f62b397 | bellard | static void __attribute__((unused)) *dummy ## n = &&dummy_label ## n;\ |
223 | b346ff46 | bellard | goto *(void *)(((TranslationBlock *)tbparam)->tb_next[n]);\ |
224 | b346ff46 | bellard | label ## n:\ |
225 | b346ff46 | bellard | T0 = (long)(tbparam) + (n);\
|
226 | b346ff46 | bellard | EIP = eip;\ |
227 | 2f62b397 | bellard | dummy_label ## n:\ |
228 | 9621339d | bellard | EXIT_TB();\ |
229 | b346ff46 | bellard | } while (0) |
230 | b346ff46 | bellard | |
231 | b346ff46 | bellard | #endif
|
232 | b346ff46 | bellard | |
233 | d4e8164f | bellard | #ifdef __powerpc__
|
234 | d4e8164f | bellard | static inline int testandset (int *p) |
235 | d4e8164f | bellard | { |
236 | d4e8164f | bellard | int ret;
|
237 | d4e8164f | bellard | __asm__ __volatile__ ( |
238 | d4e8164f | bellard | "0: lwarx %0,0,%1 ;"
|
239 | d4e8164f | bellard | " xor. %0,%3,%0;"
|
240 | d4e8164f | bellard | " bne 1f;"
|
241 | d4e8164f | bellard | " stwcx. %2,0,%1;"
|
242 | d4e8164f | bellard | " bne- 0b;"
|
243 | d4e8164f | bellard | "1: "
|
244 | d4e8164f | bellard | : "=&r" (ret)
|
245 | d4e8164f | bellard | : "r" (p), "r" (1), "r" (0) |
246 | d4e8164f | bellard | : "cr0", "memory"); |
247 | d4e8164f | bellard | return ret;
|
248 | d4e8164f | bellard | } |
249 | d4e8164f | bellard | #endif
|
250 | d4e8164f | bellard | |
251 | d4e8164f | bellard | #ifdef __i386__
|
252 | d4e8164f | bellard | static inline int testandset (int *p) |
253 | d4e8164f | bellard | { |
254 | d4e8164f | bellard | char ret;
|
255 | d4e8164f | bellard | long int readval; |
256 | d4e8164f | bellard | |
257 | d4e8164f | bellard | __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
|
258 | d4e8164f | bellard | : "=q" (ret), "=m" (*p), "=a" (readval) |
259 | d4e8164f | bellard | : "r" (1), "m" (*p), "a" (0) |
260 | d4e8164f | bellard | : "memory");
|
261 | d4e8164f | bellard | return ret;
|
262 | d4e8164f | bellard | } |
263 | d4e8164f | bellard | #endif
|
264 | d4e8164f | bellard | |
265 | d4e8164f | bellard | #ifdef __s390__
|
266 | d4e8164f | bellard | static inline int testandset (int *p) |
267 | d4e8164f | bellard | { |
268 | d4e8164f | bellard | int ret;
|
269 | d4e8164f | bellard | |
270 | d4e8164f | bellard | __asm__ __volatile__ ("0: cs %0,%1,0(%2)\n"
|
271 | d4e8164f | bellard | " jl 0b"
|
272 | d4e8164f | bellard | : "=&d" (ret)
|
273 | d4e8164f | bellard | : "r" (1), "a" (p), "0" (*p) |
274 | d4e8164f | bellard | : "cc", "memory" ); |
275 | d4e8164f | bellard | return ret;
|
276 | d4e8164f | bellard | } |
277 | d4e8164f | bellard | #endif
|
278 | d4e8164f | bellard | |
279 | d4e8164f | bellard | #ifdef __alpha__
|
280 | 2f87c607 | bellard | static inline int testandset (int *p) |
281 | d4e8164f | bellard | { |
282 | d4e8164f | bellard | int ret;
|
283 | d4e8164f | bellard | unsigned long one; |
284 | d4e8164f | bellard | |
285 | d4e8164f | bellard | __asm__ __volatile__ ("0: mov 1,%2\n"
|
286 | d4e8164f | bellard | " ldl_l %0,%1\n"
|
287 | d4e8164f | bellard | " stl_c %2,%1\n"
|
288 | d4e8164f | bellard | " beq %2,1f\n"
|
289 | d4e8164f | bellard | ".subsection 2\n"
|
290 | d4e8164f | bellard | "1: br 0b\n"
|
291 | d4e8164f | bellard | ".previous"
|
292 | d4e8164f | bellard | : "=r" (ret), "=m" (*p), "=r" (one) |
293 | d4e8164f | bellard | : "m" (*p));
|
294 | d4e8164f | bellard | return ret;
|
295 | d4e8164f | bellard | } |
296 | d4e8164f | bellard | #endif
|
297 | d4e8164f | bellard | |
298 | d4e8164f | bellard | #ifdef __sparc__
|
299 | d4e8164f | bellard | static inline int testandset (int *p) |
300 | d4e8164f | bellard | { |
301 | d4e8164f | bellard | int ret;
|
302 | d4e8164f | bellard | |
303 | d4e8164f | bellard | __asm__ __volatile__("ldstub [%1], %0"
|
304 | d4e8164f | bellard | : "=r" (ret)
|
305 | d4e8164f | bellard | : "r" (p)
|
306 | d4e8164f | bellard | : "memory");
|
307 | d4e8164f | bellard | |
308 | d4e8164f | bellard | return (ret ? 1 : 0); |
309 | d4e8164f | bellard | } |
310 | d4e8164f | bellard | #endif
|
311 | d4e8164f | bellard | |
312 | a95c6790 | bellard | #ifdef __arm__
|
313 | a95c6790 | bellard | static inline int testandset (int *spinlock) |
314 | a95c6790 | bellard | { |
315 | a95c6790 | bellard | register unsigned int ret; |
316 | a95c6790 | bellard | __asm__ __volatile__("swp %0, %1, [%2]"
|
317 | a95c6790 | bellard | : "=r"(ret)
|
318 | a95c6790 | bellard | : "0"(1), "r"(spinlock)); |
319 | a95c6790 | bellard | |
320 | a95c6790 | bellard | return ret;
|
321 | a95c6790 | bellard | } |
322 | a95c6790 | bellard | #endif
|
323 | a95c6790 | bellard | |
324 | d4e8164f | bellard | typedef int spinlock_t; |
325 | d4e8164f | bellard | |
326 | d4e8164f | bellard | #define SPIN_LOCK_UNLOCKED 0 |
327 | d4e8164f | bellard | |
328 | 3c1cf9fa | bellard | #if 1 |
329 | d4e8164f | bellard | static inline void spin_lock(spinlock_t *lock) |
330 | d4e8164f | bellard | { |
331 | d4e8164f | bellard | while (testandset(lock));
|
332 | d4e8164f | bellard | } |
333 | d4e8164f | bellard | |
334 | d4e8164f | bellard | static inline void spin_unlock(spinlock_t *lock) |
335 | d4e8164f | bellard | { |
336 | d4e8164f | bellard | *lock = 0;
|
337 | d4e8164f | bellard | } |
338 | d4e8164f | bellard | |
339 | d4e8164f | bellard | static inline int spin_trylock(spinlock_t *lock) |
340 | d4e8164f | bellard | { |
341 | d4e8164f | bellard | return !testandset(lock);
|
342 | d4e8164f | bellard | } |
343 | 3c1cf9fa | bellard | #else
|
344 | 3c1cf9fa | bellard | static inline void spin_lock(spinlock_t *lock) |
345 | 3c1cf9fa | bellard | { |
346 | 3c1cf9fa | bellard | } |
347 | 3c1cf9fa | bellard | |
348 | 3c1cf9fa | bellard | static inline void spin_unlock(spinlock_t *lock) |
349 | 3c1cf9fa | bellard | { |
350 | 3c1cf9fa | bellard | } |
351 | 3c1cf9fa | bellard | |
352 | 3c1cf9fa | bellard | static inline int spin_trylock(spinlock_t *lock) |
353 | 3c1cf9fa | bellard | { |
354 | 3c1cf9fa | bellard | return 1; |
355 | 3c1cf9fa | bellard | } |
356 | 3c1cf9fa | bellard | #endif
|
357 | d4e8164f | bellard | |
358 | d4e8164f | bellard | extern spinlock_t tb_lock;
|