Statistics
| Branch: | Revision:

root / exec.h @ b67d5959

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