Statistics
| Branch: | Revision:

root / exec-i386.c @ 7d13299d

History | View | Annotate | Download (5.8 kB)

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
typedef struct TranslationBlock {
40
    unsigned long pc;   /* simulated PC corresponding to this block */
41
    uint8_t *tc_ptr;    /* pointer to the translated code */
42
    struct TranslationBlock *hash_next; /* next matching block */
43
} TranslationBlock;
44

    
45
TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
46
TranslationBlock *tb_hash[CODE_GEN_HASH_SIZE];
47
int nb_tbs;
48

    
49
uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE];
50
uint8_t *code_gen_ptr;
51

    
52
#ifdef DEBUG_EXEC
53
static const char *cc_op_str[] = {
54
    "DYNAMIC",
55
    "EFLAGS",
56
    "MUL",
57
    "ADDB",
58
    "ADDW",
59
    "ADDL",
60
    "ADCB",
61
    "ADCW",
62
    "ADCL",
63
    "SUBB",
64
    "SUBW",
65
    "SUBL",
66
    "SBBB",
67
    "SBBW",
68
    "SBBL",
69
    "LOGICB",
70
    "LOGICW",
71
    "LOGICL",
72
    "INCB",
73
    "INCW",
74
    "INCL",
75
    "DECB",
76
    "DECW",
77
    "DECL",
78
    "SHLB",
79
    "SHLW",
80
    "SHLL",
81
    "SARB",
82
    "SARW",
83
    "SARL",
84
};
85

    
86
static void cpu_x86_dump_state(void)
87
{
88
    int eflags;
89
    eflags = cc_table[CC_OP].compute_all();
90
    eflags |= (DF & DIRECTION_FLAG);
91
    fprintf(logfile, 
92
            "EAX=%08x EBX=%08X ECX=%08x EDX=%08x\n"
93
            "ESI=%08x EDI=%08X EBP=%08x ESP=%08x\n"
94
            "CCS=%08x CCD=%08x CCO=%-8s EFL=%c%c%c%c%c%c%c\n",
95
            env->regs[R_EAX], env->regs[R_EBX], env->regs[R_ECX], env->regs[R_EDX], 
96
            env->regs[R_ESI], env->regs[R_EDI], env->regs[R_EBP], env->regs[R_ESP], 
97
            env->cc_src, env->cc_dst, cc_op_str[env->cc_op],
98
            eflags & DIRECTION_FLAG ? 'D' : '-',
99
            eflags & CC_O ? 'O' : '-',
100
            eflags & CC_S ? 'S' : '-',
101
            eflags & CC_Z ? 'Z' : '-',
102
            eflags & CC_A ? 'A' : '-',
103
            eflags & CC_P ? 'P' : '-',
104
            eflags & CC_C ? 'C' : '-'
105
            );
106
#if 1
107
    fprintf(logfile, "ST0=%f ST1=%f ST2=%f ST3=%f\n", 
108
            (double)ST0, (double)ST1, (double)ST(2), (double)ST(3));
109
#endif
110
}
111

    
112
#endif
113

    
114
void cpu_x86_tblocks_init(void)
115
{
116
    if (!code_gen_ptr) {
117
        code_gen_ptr = code_gen_buffer;
118
    }
119
}
120

    
121
/* flush all the translation blocks */
122
static void tb_flush(void)
123
{
124
    int i;
125
#ifdef DEBUG_FLUSH
126
    printf("gemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n", 
127
           code_gen_ptr - code_gen_buffer, 
128
           nb_tbs, 
129
           (code_gen_ptr - code_gen_buffer) / nb_tbs);
130
#endif
131
    nb_tbs = 0;
132
    for(i = 0;i < CODE_GEN_HASH_SIZE; i++)
133
        tb_hash[i] = NULL;
134
    code_gen_ptr = code_gen_buffer;
135
    /* XXX: flush processor icache at this point */
136
}
137

    
138
/* find a translation block in the translation cache. If not found,
139
   allocate a new one */
140
static inline TranslationBlock *tb_find_and_alloc(unsigned long pc)
141
{
142
    TranslationBlock **ptb, *tb;
143
    unsigned int h;
144
 
145
    h = pc & (CODE_GEN_HASH_SIZE - 1);
146
    ptb = &tb_hash[h];
147
    for(;;) {
148
        tb = *ptb;
149
        if (!tb)
150
            break;
151
        if (tb->pc == pc)
152
            return tb;
153
        ptb = &tb->hash_next;
154
    }
155
    if (nb_tbs >= CODE_GEN_MAX_BLOCKS || 
156
        (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
157
        tb_flush();
158
    tb = &tbs[nb_tbs++];
159
    *ptb = tb;
160
    tb->pc = pc;
161
    tb->tc_ptr = NULL;
162
    tb->hash_next = NULL;
163
    return tb;
164
}
165

    
166
int cpu_x86_exec(CPUX86State *env1)
167
{
168
    int saved_T0, saved_T1, saved_A0;
169
    CPUX86State *saved_env;
170
    int code_gen_size, ret;
171
    void (*gen_func)(void);
172
    TranslationBlock *tb;
173
    uint8_t *tc_ptr;
174
    
175
    /* first we save global registers */
176
    saved_T0 = T0;
177
    saved_T1 = T1;
178
    saved_A0 = A0;
179
    saved_env = env;
180
    env = env1;
181
    
182
    /* prepare setjmp context for exception handling */
183
    if (setjmp(env->jmp_env) == 0) {
184
        for(;;) {
185
#ifdef DEBUG_EXEC
186
            if (loglevel) {
187
                cpu_x86_dump_state();
188
            }
189
#endif
190
            tb = tb_find_and_alloc((unsigned long)env->pc);
191
            tc_ptr = tb->tc_ptr;
192
            if (!tb->tc_ptr) {
193
                /* if no translated code available, then translate it now */
194
                tc_ptr = code_gen_ptr;
195
                cpu_x86_gen_code(code_gen_ptr, CODE_GEN_MAX_SIZE, 
196
                                 &code_gen_size, (uint8_t *)env->pc);
197
                tb->tc_ptr = tc_ptr;
198
                code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
199
            }
200
            /* execute the generated code */
201
            gen_func = (void *)tc_ptr;
202
            gen_func();
203
        }
204
    }
205
    ret = env->exception_index;
206

    
207
    /* restore global registers */
208
    T0 = saved_T0;
209
    T1 = saved_T1;
210
    A0 = saved_A0;
211
    env = saved_env;
212
    return ret;
213
}