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