Statistics
| Branch: | Revision:

root / exec-i386.c @ 25eb4484

History | View | Annotate | Download (11.1 kB)

1
/*
2
 *  i386 emulator main execution loop
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
#include "exec-i386.h"
21
#include "disas.h"
22

    
23
//#define DEBUG_EXEC
24
//#define DEBUG_SIGNAL
25

    
26
/* main execution loop */
27

    
28
/* thread support */
29

    
30
spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
31

    
32
void cpu_lock(void)
33
{
34
    spin_lock(&global_cpu_lock);
35
}
36

    
37
void cpu_unlock(void)
38
{
39
    spin_unlock(&global_cpu_lock);
40
}
41

    
42
/* exception support */
43
/* NOTE: not static to force relocation generation by GCC */
44
void raise_exception_err(int exception_index, int error_code)
45
{
46
    /* NOTE: the register at this point must be saved by hand because
47
       longjmp restore them */
48
#ifdef __sparc__
49
        /* We have to stay in the same register window as our caller,
50
         * thus this trick.
51
         */
52
        __asm__ __volatile__("restore\n\t"
53
                             "mov\t%o0, %i0");
54
#endif
55
#ifdef reg_EAX
56
    env->regs[R_EAX] = EAX;
57
#endif
58
#ifdef reg_ECX
59
    env->regs[R_ECX] = ECX;
60
#endif
61
#ifdef reg_EDX
62
    env->regs[R_EDX] = EDX;
63
#endif
64
#ifdef reg_EBX
65
    env->regs[R_EBX] = EBX;
66
#endif
67
#ifdef reg_ESP
68
    env->regs[R_ESP] = ESP;
69
#endif
70
#ifdef reg_EBP
71
    env->regs[R_EBP] = EBP;
72
#endif
73
#ifdef reg_ESI
74
    env->regs[R_ESI] = ESI;
75
#endif
76
#ifdef reg_EDI
77
    env->regs[R_EDI] = EDI;
78
#endif
79
    env->exception_index = exception_index;
80
    env->error_code = error_code;
81
    longjmp(env->jmp_env, 1);
82
}
83

    
84
/* short cut if error_code is 0 or not present */
85
void raise_exception(int exception_index)
86
{
87
    raise_exception_err(exception_index, 0);
88
}
89

    
90
int cpu_x86_exec(CPUX86State *env1)
91
{
92
    int saved_T0, saved_T1, saved_A0;
93
    CPUX86State *saved_env;
94
#ifdef reg_EAX
95
    int saved_EAX;
96
#endif
97
#ifdef reg_ECX
98
    int saved_ECX;
99
#endif
100
#ifdef reg_EDX
101
    int saved_EDX;
102
#endif
103
#ifdef reg_EBX
104
    int saved_EBX;
105
#endif
106
#ifdef reg_ESP
107
    int saved_ESP;
108
#endif
109
#ifdef reg_EBP
110
    int saved_EBP;
111
#endif
112
#ifdef reg_ESI
113
    int saved_ESI;
114
#endif
115
#ifdef reg_EDI
116
    int saved_EDI;
117
#endif
118
    int code_gen_size, ret, code_size;
119
    void (*gen_func)(void);
120
    TranslationBlock *tb, **ptb;
121
    uint8_t *tc_ptr, *cs_base, *pc;
122
    unsigned int flags;
123

    
124
    /* first we save global registers */
125
    saved_T0 = T0;
126
    saved_T1 = T1;
127
    saved_A0 = A0;
128
    saved_env = env;
129
    env = env1;
130
#ifdef reg_EAX
131
    saved_EAX = EAX;
132
    EAX = env->regs[R_EAX];
133
#endif
134
#ifdef reg_ECX
135
    saved_ECX = ECX;
136
    ECX = env->regs[R_ECX];
137
#endif
138
#ifdef reg_EDX
139
    saved_EDX = EDX;
140
    EDX = env->regs[R_EDX];
141
#endif
142
#ifdef reg_EBX
143
    saved_EBX = EBX;
144
    EBX = env->regs[R_EBX];
145
#endif
146
#ifdef reg_ESP
147
    saved_ESP = ESP;
148
    ESP = env->regs[R_ESP];
149
#endif
150
#ifdef reg_EBP
151
    saved_EBP = EBP;
152
    EBP = env->regs[R_EBP];
153
#endif
154
#ifdef reg_ESI
155
    saved_ESI = ESI;
156
    ESI = env->regs[R_ESI];
157
#endif
158
#ifdef reg_EDI
159
    saved_EDI = EDI;
160
    EDI = env->regs[R_EDI];
161
#endif
162
    
163
    /* put eflags in CPU temporary format */
164
    CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
165
    DF = 1 - (2 * ((env->eflags >> 10) & 1));
166
    CC_OP = CC_OP_EFLAGS;
167
    env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
168
    env->interrupt_request = 0;
169

    
170
    /* prepare setjmp context for exception handling */
171
    if (setjmp(env->jmp_env) == 0) {
172
        for(;;) {
173
            if (env->interrupt_request) {
174
                raise_exception(EXCP_INTERRUPT);
175
            }
176
#ifdef DEBUG_EXEC
177
            if (loglevel) {
178
                /* XXX: save all volatile state in cpu state */
179
                /* restore flags in standard format */
180
                env->regs[R_EAX] = EAX;
181
                env->regs[R_EBX] = EBX;
182
                env->regs[R_ECX] = ECX;
183
                env->regs[R_EDX] = EDX;
184
                env->regs[R_ESI] = ESI;
185
                env->regs[R_EDI] = EDI;
186
                env->regs[R_EBP] = EBP;
187
                env->regs[R_ESP] = ESP;
188
                env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
189
                cpu_x86_dump_state(env, logfile, 0);
190
                env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
191
            }
192
#endif
193
            /* we compute the CPU state. We assume it will not
194
               change during the whole generated block. */
195
            flags = env->seg_cache[R_CS].seg_32bit << GEN_FLAG_CODE32_SHIFT;
196
            flags |= env->seg_cache[R_SS].seg_32bit << GEN_FLAG_SS32_SHIFT;
197
            flags |= (((unsigned long)env->seg_cache[R_DS].base | 
198
                       (unsigned long)env->seg_cache[R_ES].base |
199
                       (unsigned long)env->seg_cache[R_SS].base) != 0) << 
200
                GEN_FLAG_ADDSEG_SHIFT;
201
            if (!(env->eflags & VM_MASK)) {
202
                flags |= (env->segs[R_CS] & 3) << GEN_FLAG_CPL_SHIFT;
203
            } else {
204
                /* NOTE: a dummy CPL is kept */
205
                flags |= (1 << GEN_FLAG_VM_SHIFT);
206
                flags |= (3 << GEN_FLAG_CPL_SHIFT);
207
            }
208
            flags |= (env->eflags & IOPL_MASK) >> (12 - GEN_FLAG_IOPL_SHIFT);
209
            flags |= (env->eflags & TF_MASK) << (GEN_FLAG_TF_SHIFT - 8);
210
            cs_base = env->seg_cache[R_CS].base;
211
            pc = cs_base + env->eip;
212
            tb = tb_find(&ptb, (unsigned long)pc, (unsigned long)cs_base, 
213
                         flags);
214
            if (!tb) {
215
                /* if no translated code available, then translate it now */
216
                /* very inefficient but safe: we lock all the cpus
217
                   when generating code */
218
                spin_lock(&tb_lock);
219
                tc_ptr = code_gen_ptr;
220
                ret = cpu_x86_gen_code(code_gen_ptr, CODE_GEN_MAX_SIZE, 
221
                                       &code_gen_size, pc, cs_base, flags,
222
                                       &code_size);
223
                /* if invalid instruction, signal it */
224
                if (ret != 0) {
225
                    spin_unlock(&tb_lock);
226
                    raise_exception(EXCP06_ILLOP);
227
                }
228
                tb = tb_alloc((unsigned long)pc, code_size);
229
                *ptb = tb;
230
                tb->cs_base = (unsigned long)cs_base;
231
                tb->flags = flags;
232
                tb->tc_ptr = tc_ptr;
233
                tb->hash_next = NULL;
234
                code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
235
                spin_unlock(&tb_lock);
236
            }
237
#ifdef DEBUG_EXEC
238
            if (loglevel) {
239
                fprintf(logfile, "Trace 0x%08lx [0x%08lx] %s\n",
240
                        (long)tb->tc_ptr, (long)tb->pc,
241
                        lookup_symbol((void *)tb->pc));
242
            }
243
#endif
244
            /* execute the generated code */
245
            tc_ptr = tb->tc_ptr;
246
            gen_func = (void *)tc_ptr;
247
#ifdef __sparc__
248
            __asm__ __volatile__("call        %0\n\t"
249
                                 " mov        %%o7,%%i0"
250
                                 : /* no outputs */
251
                                 : "r" (gen_func)
252
                                 : "i0", "i1", "i2", "i3", "i4", "i5");
253
#else
254
            gen_func();
255
#endif
256
        }
257
    }
258
    ret = env->exception_index;
259

    
260
    /* restore flags in standard format */
261
    env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
262

    
263
    /* restore global registers */
264
#ifdef reg_EAX
265
    EAX = saved_EAX;
266
#endif
267
#ifdef reg_ECX
268
    ECX = saved_ECX;
269
#endif
270
#ifdef reg_EDX
271
    EDX = saved_EDX;
272
#endif
273
#ifdef reg_EBX
274
    EBX = saved_EBX;
275
#endif
276
#ifdef reg_ESP
277
    ESP = saved_ESP;
278
#endif
279
#ifdef reg_EBP
280
    EBP = saved_EBP;
281
#endif
282
#ifdef reg_ESI
283
    ESI = saved_ESI;
284
#endif
285
#ifdef reg_EDI
286
    EDI = saved_EDI;
287
#endif
288
    T0 = saved_T0;
289
    T1 = saved_T1;
290
    A0 = saved_A0;
291
    env = saved_env;
292
    return ret;
293
}
294

    
295
void cpu_x86_interrupt(CPUX86State *s)
296
{
297
    s->interrupt_request = 1;
298
}
299

    
300

    
301
void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
302
{
303
    CPUX86State *saved_env;
304

    
305
    saved_env = env;
306
    env = s;
307
    load_seg(seg_reg, selector);
308
    env = saved_env;
309
}
310

    
311
#undef EAX
312
#undef ECX
313
#undef EDX
314
#undef EBX
315
#undef ESP
316
#undef EBP
317
#undef ESI
318
#undef EDI
319
#undef EIP
320
#include <signal.h>
321
#include <sys/ucontext.h>
322

    
323
/* 'pc' is the host PC at which the exception was raised. 'address' is
324
   the effective address of the memory exception. 'is_write' is 1 if a
325
   write caused the exception and otherwise 0'. 'old_set' is the
326
   signal set which should be restored */
327
static inline int handle_cpu_signal(unsigned long pc,
328
                                    unsigned long address,
329
                                    int is_write,
330
                                    sigset_t *old_set)
331
{
332
#if defined(DEBUG_SIGNAL)
333
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx wr=%d oldset=0x%08lx\n", 
334
           pc, address, is_write, *(unsigned long *)old_set);
335
#endif
336
    /* XXX: locking issue */
337
    if (is_write && page_unprotect(address)) {
338
        sigprocmask(SIG_SETMASK, old_set, NULL);
339
        return 1;
340
    }
341
    if (pc >= (unsigned long)code_gen_buffer &&
342
        pc < (unsigned long)code_gen_buffer + CODE_GEN_BUFFER_SIZE) {
343
        /* the PC is inside the translated code. It means that we have
344
           a virtual CPU fault */
345
        /* we restore the process signal mask as the sigreturn should
346
           do it */
347
        sigprocmask(SIG_SETMASK, old_set, NULL);
348
        /* XXX: need to compute virtual pc position by retranslating
349
           code. The rest of the CPU state should be correct. */
350
        env->cr2 = address;
351
        raise_exception_err(EXCP0E_PAGE, 4 | (is_write << 1));
352
        /* never comes here */
353
        return 1;
354
    } else {
355
        return 0;
356
    }
357
}
358

    
359
int cpu_x86_signal_handler(int host_signum, struct siginfo *info, 
360
                           void *puc)
361
{
362
#if defined(__i386__)
363
    struct ucontext *uc = puc;
364
    unsigned long pc;
365
    sigset_t *pold_set;
366
    
367
#ifndef REG_EIP
368
/* for glibc 2.1 */
369
#define REG_EIP    EIP
370
#define REG_ERR    ERR
371
#define REG_TRAPNO TRAPNO
372
#endif
373
    pc = uc->uc_mcontext.gregs[REG_EIP];
374
    pold_set = &uc->uc_sigmask;
375
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
376
                             uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ? 
377
                             (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
378
                             pold_set);
379
#elif defined(__powerpc)
380
    struct ucontext *uc = puc;
381
    struct pt_regs *regs = uc->uc_mcontext.regs;
382
    unsigned long pc;
383
    sigset_t *pold_set;
384
    int is_write;
385

    
386
    pc = regs->nip;
387
    pold_set = &uc->uc_sigmask;
388
    is_write = 0;
389
#if 0
390
    /* ppc 4xx case */
391
    if (regs->dsisr & 0x00800000)
392
        is_write = 1;
393
#else
394
    if (regs->trap != 0x400 && (regs->dsisr & 0x02000000))
395
        is_write = 1;
396
#endif
397
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
398
                             is_write, pold_set);
399
#else
400
#error CPU specific signal handler needed
401
    return 0;
402
#endif
403
}