Statistics
| Branch: | Revision:

root / cpu-exec.c @ 3475187d

History | View | Annotate | Download (38.7 kB)

1
/*
2
 *  i386 emulator main execution loop
3
 * 
4
 *  Copyright (c) 2003-2005 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 "config.h"
21
#include "exec.h"
22
#include "disas.h"
23

    
24
#if !defined(CONFIG_SOFTMMU)
25
#undef EAX
26
#undef ECX
27
#undef EDX
28
#undef EBX
29
#undef ESP
30
#undef EBP
31
#undef ESI
32
#undef EDI
33
#undef EIP
34
#include <signal.h>
35
#include <sys/ucontext.h>
36
#endif
37

    
38
int tb_invalidated_flag;
39

    
40
//#define DEBUG_EXEC
41
//#define DEBUG_SIGNAL
42

    
43
#if defined(TARGET_ARM) || defined(TARGET_SPARC)
44
/* XXX: unify with i386 target */
45
void cpu_loop_exit(void)
46
{
47
    longjmp(env->jmp_env, 1);
48
}
49
#endif
50
#ifndef TARGET_SPARC
51
#define reg_T2
52
#endif
53

    
54
/* exit the current TB from a signal handler. The host registers are
55
   restored in a state compatible with the CPU emulator
56
 */
57
void cpu_resume_from_signal(CPUState *env1, void *puc) 
58
{
59
#if !defined(CONFIG_SOFTMMU)
60
    struct ucontext *uc = puc;
61
#endif
62

    
63
    env = env1;
64

    
65
    /* XXX: restore cpu registers saved in host registers */
66

    
67
#if !defined(CONFIG_SOFTMMU)
68
    if (puc) {
69
        /* XXX: use siglongjmp ? */
70
        sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
71
    }
72
#endif
73
    longjmp(env->jmp_env, 1);
74
}
75

    
76
/* main execution loop */
77

    
78
int cpu_exec(CPUState *env1)
79
{
80
    int saved_T0, saved_T1;
81
#if defined(reg_T2)
82
    int saved_T2;
83
#endif
84
    CPUState *saved_env;
85
#if defined(TARGET_I386)
86
#ifdef reg_EAX
87
    int saved_EAX;
88
#endif
89
#ifdef reg_ECX
90
    int saved_ECX;
91
#endif
92
#ifdef reg_EDX
93
    int saved_EDX;
94
#endif
95
#ifdef reg_EBX
96
    int saved_EBX;
97
#endif
98
#ifdef reg_ESP
99
    int saved_ESP;
100
#endif
101
#ifdef reg_EBP
102
    int saved_EBP;
103
#endif
104
#ifdef reg_ESI
105
    int saved_ESI;
106
#endif
107
#ifdef reg_EDI
108
    int saved_EDI;
109
#endif
110
#elif defined(TARGET_SPARC)
111
#if defined(reg_REGWPTR)
112
    uint32_t *saved_regwptr;
113
#endif
114
#endif
115
#ifdef __sparc__
116
    int saved_i7, tmp_T0;
117
#endif
118
    int code_gen_size, ret, interrupt_request;
119
    void (*gen_func)(void);
120
    TranslationBlock *tb, **ptb;
121
    target_ulong cs_base, pc;
122
    uint8_t *tc_ptr;
123
    unsigned int flags;
124

    
125
    /* first we save global registers */
126
    saved_env = env;
127
    env = env1;
128
    saved_T0 = T0;
129
    saved_T1 = T1;
130
#if defined(reg_T2)
131
    saved_T2 = T2;
132
#endif
133
#ifdef __sparc__
134
    /* we also save i7 because longjmp may not restore it */
135
    asm volatile ("mov %%i7, %0" : "=r" (saved_i7));
136
#endif
137

    
138
#if defined(TARGET_I386)
139
#ifdef reg_EAX
140
    saved_EAX = EAX;
141
#endif
142
#ifdef reg_ECX
143
    saved_ECX = ECX;
144
#endif
145
#ifdef reg_EDX
146
    saved_EDX = EDX;
147
#endif
148
#ifdef reg_EBX
149
    saved_EBX = EBX;
150
#endif
151
#ifdef reg_ESP
152
    saved_ESP = ESP;
153
#endif
154
#ifdef reg_EBP
155
    saved_EBP = EBP;
156
#endif
157
#ifdef reg_ESI
158
    saved_ESI = ESI;
159
#endif
160
#ifdef reg_EDI
161
    saved_EDI = EDI;
162
#endif
163

    
164
    env_to_regs();
165
    /* put eflags in CPU temporary format */
166
    CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
167
    DF = 1 - (2 * ((env->eflags >> 10) & 1));
168
    CC_OP = CC_OP_EFLAGS;
169
    env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
170
#elif defined(TARGET_ARM)
171
    {
172
        unsigned int psr;
173
        psr = env->cpsr;
174
        env->CF = (psr >> 29) & 1;
175
        env->NZF = (psr & 0xc0000000) ^ 0x40000000;
176
        env->VF = (psr << 3) & 0x80000000;
177
        env->QF = (psr >> 27) & 1;
178
        env->cpsr = psr & ~CACHED_CPSR_BITS;
179
    }
180
#elif defined(TARGET_SPARC)
181
#if defined(reg_REGWPTR)
182
    saved_regwptr = REGWPTR;
183
#endif
184
#elif defined(TARGET_PPC)
185
#else
186
#error unsupported target CPU
187
#endif
188
    env->exception_index = -1;
189

    
190
    /* prepare setjmp context for exception handling */
191
    for(;;) {
192
        if (setjmp(env->jmp_env) == 0) {
193
            env->current_tb = NULL;
194
            /* if an exception is pending, we execute it here */
195
            if (env->exception_index >= 0) {
196
                if (env->exception_index >= EXCP_INTERRUPT) {
197
                    /* exit request from the cpu execution loop */
198
                    ret = env->exception_index;
199
                    break;
200
                } else if (env->user_mode_only) {
201
                    /* if user mode only, we simulate a fake exception
202
                       which will be hanlded outside the cpu execution
203
                       loop */
204
#if defined(TARGET_I386)
205
                    do_interrupt_user(env->exception_index, 
206
                                      env->exception_is_int, 
207
                                      env->error_code, 
208
                                      env->exception_next_eip);
209
#endif
210
                    ret = env->exception_index;
211
                    break;
212
                } else {
213
#if defined(TARGET_I386)
214
                    /* simulate a real cpu exception. On i386, it can
215
                       trigger new exceptions, but we do not handle
216
                       double or triple faults yet. */
217
                    do_interrupt(env->exception_index, 
218
                                 env->exception_is_int, 
219
                                 env->error_code, 
220
                                 env->exception_next_eip, 0);
221
#elif defined(TARGET_PPC)
222
                    do_interrupt(env);
223
#elif defined(TARGET_SPARC)
224
                    do_interrupt(env->exception_index);
225
#endif
226
                }
227
                env->exception_index = -1;
228
            } 
229
#ifdef USE_KQEMU
230
            if (kqemu_is_ok(env) && env->interrupt_request == 0) {
231
                int ret;
232
                env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
233
                ret = kqemu_cpu_exec(env);
234
                /* put eflags in CPU temporary format */
235
                CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
236
                DF = 1 - (2 * ((env->eflags >> 10) & 1));
237
                CC_OP = CC_OP_EFLAGS;
238
                env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
239
                if (ret == 1) {
240
                    /* exception */
241
                    longjmp(env->jmp_env, 1);
242
                } else if (ret == 2) {
243
                    /* softmmu execution needed */
244
                } else {
245
                    if (env->interrupt_request != 0) {
246
                        /* hardware interrupt will be executed just after */
247
                    } else {
248
                        /* otherwise, we restart */
249
                        longjmp(env->jmp_env, 1);
250
                    }
251
                }
252
            }
253
#endif
254

    
255
            T0 = 0; /* force lookup of first TB */
256
            for(;;) {
257
#ifdef __sparc__
258
                /* g1 can be modified by some libc? functions */ 
259
                tmp_T0 = T0;
260
#endif            
261
                interrupt_request = env->interrupt_request;
262
                if (__builtin_expect(interrupt_request, 0)) {
263
#if defined(TARGET_I386)
264
                    /* if hardware interrupt pending, we execute it */
265
                    if ((interrupt_request & CPU_INTERRUPT_HARD) &&
266
                        (env->eflags & IF_MASK) && 
267
                        !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
268
                        int intno;
269
                        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
270
                        intno = cpu_get_pic_interrupt(env);
271
                        if (loglevel & CPU_LOG_TB_IN_ASM) {
272
                            fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
273
                        }
274
                        do_interrupt(intno, 0, 0, 0, 1);
275
                        /* ensure that no TB jump will be modified as
276
                           the program flow was changed */
277
#ifdef __sparc__
278
                        tmp_T0 = 0;
279
#else
280
                        T0 = 0;
281
#endif
282
                    }
283
#elif defined(TARGET_PPC)
284
#if 0
285
                    if ((interrupt_request & CPU_INTERRUPT_RESET)) {
286
                        cpu_ppc_reset(env);
287
                    }
288
#endif
289
                    if (msr_ee != 0) {
290
                    if ((interrupt_request & CPU_INTERRUPT_HARD)) {
291
                            /* Raise it */
292
                            env->exception_index = EXCP_EXTERNAL;
293
                            env->error_code = 0;
294
                            do_interrupt(env);
295
                        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
296
                        } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) {
297
                            /* Raise it */
298
                            env->exception_index = EXCP_DECR;
299
                            env->error_code = 0;
300
                            do_interrupt(env);
301
                            env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
302
                        }
303
                    }
304
#elif defined(TARGET_SPARC)
305
                    if ((interrupt_request & CPU_INTERRUPT_HARD) &&
306
                        (env->psret != 0)) {
307
                        int pil = env->interrupt_index & 15;
308
                        int type = env->interrupt_index & 0xf0;
309

    
310
                        if (((type == TT_EXTINT) &&
311
                             (pil == 15 || pil > env->psrpil)) ||
312
                            type != TT_EXTINT) {
313
                            env->interrupt_request &= ~CPU_INTERRUPT_HARD;
314
                            do_interrupt(env->interrupt_index);
315
                            env->interrupt_index = 0;
316
                        }
317
                    } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
318
                        //do_interrupt(0, 0, 0, 0, 0);
319
                        env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
320
                    }
321
#endif
322
                    if (interrupt_request & CPU_INTERRUPT_EXITTB) {
323
                        env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
324
                        /* ensure that no TB jump will be modified as
325
                           the program flow was changed */
326
#ifdef __sparc__
327
                        tmp_T0 = 0;
328
#else
329
                        T0 = 0;
330
#endif
331
                    }
332
                    if (interrupt_request & CPU_INTERRUPT_EXIT) {
333
                        env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
334
                        env->exception_index = EXCP_INTERRUPT;
335
                        cpu_loop_exit();
336
                    }
337
                }
338
#ifdef DEBUG_EXEC
339
                if ((loglevel & CPU_LOG_EXEC)) {
340
#if defined(TARGET_I386)
341
                    /* restore flags in standard format */
342
#ifdef reg_EAX
343
                    env->regs[R_EAX] = EAX;
344
#endif
345
#ifdef reg_EBX
346
                    env->regs[R_EBX] = EBX;
347
#endif
348
#ifdef reg_ECX
349
                    env->regs[R_ECX] = ECX;
350
#endif
351
#ifdef reg_EDX
352
                    env->regs[R_EDX] = EDX;
353
#endif
354
#ifdef reg_ESI
355
                    env->regs[R_ESI] = ESI;
356
#endif
357
#ifdef reg_EDI
358
                    env->regs[R_EDI] = EDI;
359
#endif
360
#ifdef reg_EBP
361
                    env->regs[R_EBP] = EBP;
362
#endif
363
#ifdef reg_ESP
364
                    env->regs[R_ESP] = ESP;
365
#endif
366
                    env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
367
                    cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
368
                    env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
369
#elif defined(TARGET_ARM)
370
                    env->cpsr = compute_cpsr();
371
                    cpu_dump_state(env, logfile, fprintf, 0);
372
                    env->cpsr &= ~CACHED_CPSR_BITS;
373
#elif defined(TARGET_SPARC)
374
                    REGWPTR = env->regbase + (env->cwp * 16);
375
                    env->regwptr = REGWPTR;
376
                    cpu_dump_state(env, logfile, fprintf, 0);
377
#elif defined(TARGET_PPC)
378
                    cpu_dump_state(env, logfile, fprintf, 0);
379
#else
380
#error unsupported target CPU 
381
#endif
382
                }
383
#endif
384
                /* we record a subset of the CPU state. It will
385
                   always be the same before a given translated block
386
                   is executed. */
387
#if defined(TARGET_I386)
388
                flags = env->hflags;
389
                flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
390
                cs_base = env->segs[R_CS].base;
391
                pc = cs_base + env->eip;
392
#elif defined(TARGET_ARM)
393
                flags = env->thumb | (env->vfp.vec_len << 1)
394
                        | (env->vfp.vec_stride << 4);
395
                cs_base = 0;
396
                pc = env->regs[15];
397
#elif defined(TARGET_SPARC)
398
#ifdef TARGET_SPARC64
399
                flags = (env->pstate << 2) | ((env->lsu & (DMMU_E | IMMU_E)) >> 2);
400
#else
401
                flags = env->psrs | ((env->mmuregs[0] & (MMU_E | MMU_NF)) << 1);
402
#endif
403
                cs_base = env->npc;
404
                pc = env->pc;
405
#elif defined(TARGET_PPC)
406
                flags = (msr_pr << MSR_PR) | (msr_fp << MSR_FP) |
407
                    (msr_se << MSR_SE) | (msr_le << MSR_LE);
408
                cs_base = 0;
409
                pc = env->nip;
410
#else
411
#error unsupported CPU
412
#endif
413
                tb = tb_find(&ptb, pc, cs_base, 
414
                             flags);
415
                if (!tb) {
416
                    TranslationBlock **ptb1;
417
                    unsigned int h;
418
                    target_ulong phys_pc, phys_page1, phys_page2, virt_page2;
419
                    
420
                    
421
                    spin_lock(&tb_lock);
422

    
423
                    tb_invalidated_flag = 0;
424
                    
425
                    regs_to_env(); /* XXX: do it just before cpu_gen_code() */
426

    
427
                    /* find translated block using physical mappings */
428
                    phys_pc = get_phys_addr_code(env, pc);
429
                    phys_page1 = phys_pc & TARGET_PAGE_MASK;
430
                    phys_page2 = -1;
431
                    h = tb_phys_hash_func(phys_pc);
432
                    ptb1 = &tb_phys_hash[h];
433
                    for(;;) {
434
                        tb = *ptb1;
435
                        if (!tb)
436
                            goto not_found;
437
                        if (tb->pc == pc && 
438
                            tb->page_addr[0] == phys_page1 &&
439
                            tb->cs_base == cs_base && 
440
                            tb->flags == flags) {
441
                            /* check next page if needed */
442
                            if (tb->page_addr[1] != -1) {
443
                                virt_page2 = (pc & TARGET_PAGE_MASK) + 
444
                                    TARGET_PAGE_SIZE;
445
                                phys_page2 = get_phys_addr_code(env, virt_page2);
446
                                if (tb->page_addr[1] == phys_page2)
447
                                    goto found;
448
                            } else {
449
                                goto found;
450
                            }
451
                        }
452
                        ptb1 = &tb->phys_hash_next;
453
                    }
454
                not_found:
455
                    /* if no translated code available, then translate it now */
456
                    tb = tb_alloc(pc);
457
                    if (!tb) {
458
                        /* flush must be done */
459
                        tb_flush(env);
460
                        /* cannot fail at this point */
461
                        tb = tb_alloc(pc);
462
                        /* don't forget to invalidate previous TB info */
463
                        ptb = &tb_hash[tb_hash_func(pc)];
464
                        T0 = 0;
465
                    }
466
                    tc_ptr = code_gen_ptr;
467
                    tb->tc_ptr = tc_ptr;
468
                    tb->cs_base = cs_base;
469
                    tb->flags = flags;
470
                    cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
471
                    code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
472
                    
473
                    /* check next page if needed */
474
                    virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
475
                    phys_page2 = -1;
476
                    if ((pc & TARGET_PAGE_MASK) != virt_page2) {
477
                        phys_page2 = get_phys_addr_code(env, virt_page2);
478
                    }
479
                    tb_link_phys(tb, phys_pc, phys_page2);
480

    
481
                found:
482
                    if (tb_invalidated_flag) {
483
                        /* as some TB could have been invalidated because
484
                           of memory exceptions while generating the code, we
485
                           must recompute the hash index here */
486
                        ptb = &tb_hash[tb_hash_func(pc)];
487
                        while (*ptb != NULL)
488
                            ptb = &(*ptb)->hash_next;
489
                        T0 = 0;
490
                    }
491
                    /* we add the TB in the virtual pc hash table */
492
                    *ptb = tb;
493
                    tb->hash_next = NULL;
494
                    tb_link(tb);
495
                    spin_unlock(&tb_lock);
496
                }
497
#ifdef DEBUG_EXEC
498
                if ((loglevel & CPU_LOG_EXEC)) {
499
                    fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
500
                            (long)tb->tc_ptr, tb->pc,
501
                            lookup_symbol(tb->pc));
502
                }
503
#endif
504
#ifdef __sparc__
505
                T0 = tmp_T0;
506
#endif            
507
                /* see if we can patch the calling TB. */
508
                {
509
                    if (T0 != 0
510
#if defined(TARGET_I386) && defined(USE_CODE_COPY)
511
                    && (tb->cflags & CF_CODE_COPY) == 
512
                    (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
513
#endif
514
                    ) {
515
                    spin_lock(&tb_lock);
516
                    tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
517
#if defined(USE_CODE_COPY)
518
                    /* propagates the FP use info */
519
                    ((TranslationBlock *)(T0 & ~3))->cflags |= 
520
                        (tb->cflags & CF_FP_USED);
521
#endif
522
                    spin_unlock(&tb_lock);
523
                }
524
                }
525
                tc_ptr = tb->tc_ptr;
526
                env->current_tb = tb;
527
                /* execute the generated code */
528
                gen_func = (void *)tc_ptr;
529
#if defined(__sparc__)
530
                __asm__ __volatile__("call        %0\n\t"
531
                                     "mov        %%o7,%%i0"
532
                                     : /* no outputs */
533
                                     : "r" (gen_func) 
534
                                     : "i0", "i1", "i2", "i3", "i4", "i5");
535
#elif defined(__arm__)
536
                asm volatile ("mov pc, %0\n\t"
537
                              ".global exec_loop\n\t"
538
                              "exec_loop:\n\t"
539
                              : /* no outputs */
540
                              : "r" (gen_func)
541
                              : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
542
#elif defined(TARGET_I386) && defined(USE_CODE_COPY)
543
{
544
    if (!(tb->cflags & CF_CODE_COPY)) {
545
        if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
546
            save_native_fp_state(env);
547
        }
548
        gen_func();
549
    } else {
550
        if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
551
            restore_native_fp_state(env);
552
        }
553
        /* we work with native eflags */
554
        CC_SRC = cc_table[CC_OP].compute_all();
555
        CC_OP = CC_OP_EFLAGS;
556
        asm(".globl exec_loop\n"
557
            "\n"
558
            "debug1:\n"
559
            "    pushl %%ebp\n"
560
            "    fs movl %10, %9\n"
561
            "    fs movl %11, %%eax\n"
562
            "    andl $0x400, %%eax\n"
563
            "    fs orl %8, %%eax\n"
564
            "    pushl %%eax\n"
565
            "    popf\n"
566
            "    fs movl %%esp, %12\n"
567
            "    fs movl %0, %%eax\n"
568
            "    fs movl %1, %%ecx\n"
569
            "    fs movl %2, %%edx\n"
570
            "    fs movl %3, %%ebx\n"
571
            "    fs movl %4, %%esp\n"
572
            "    fs movl %5, %%ebp\n"
573
            "    fs movl %6, %%esi\n"
574
            "    fs movl %7, %%edi\n"
575
            "    fs jmp *%9\n"
576
            "exec_loop:\n"
577
            "    fs movl %%esp, %4\n"
578
            "    fs movl %12, %%esp\n"
579
            "    fs movl %%eax, %0\n"
580
            "    fs movl %%ecx, %1\n"
581
            "    fs movl %%edx, %2\n"
582
            "    fs movl %%ebx, %3\n"
583
            "    fs movl %%ebp, %5\n"
584
            "    fs movl %%esi, %6\n"
585
            "    fs movl %%edi, %7\n"
586
            "    pushf\n"
587
            "    popl %%eax\n"
588
            "    movl %%eax, %%ecx\n"
589
            "    andl $0x400, %%ecx\n"
590
            "    shrl $9, %%ecx\n"
591
            "    andl $0x8d5, %%eax\n"
592
            "    fs movl %%eax, %8\n"
593
            "    movl $1, %%eax\n"
594
            "    subl %%ecx, %%eax\n"
595
            "    fs movl %%eax, %11\n"
596
            "    fs movl %9, %%ebx\n" /* get T0 value */
597
            "    popl %%ebp\n"
598
            :
599
            : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
600
            "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
601
            "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
602
            "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
603
            "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
604
            "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
605
            "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
606
            "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
607
            "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
608
            "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
609
            "a" (gen_func),
610
            "m" (*(uint8_t *)offsetof(CPUState, df)),
611
            "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
612
            : "%ecx", "%edx"
613
            );
614
    }
615
}
616
#elif defined(__ia64)
617
                struct fptr {
618
                        void *ip;
619
                        void *gp;
620
                } fp;
621

    
622
                fp.ip = tc_ptr;
623
                fp.gp = code_gen_buffer + 2 * (1 << 20);
624
                (*(void (*)(void)) &fp)();
625
#else
626
                gen_func();
627
#endif
628
                env->current_tb = NULL;
629
                /* reset soft MMU for next block (it can currently
630
                   only be set by a memory fault) */
631
#if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
632
                if (env->hflags & HF_SOFTMMU_MASK) {
633
                    env->hflags &= ~HF_SOFTMMU_MASK;
634
                    /* do not allow linking to another block */
635
                    T0 = 0;
636
                }
637
#endif
638
            }
639
        } else {
640
            env_to_regs();
641
        }
642
    } /* for(;;) */
643

    
644

    
645
#if defined(TARGET_I386)
646
#if defined(USE_CODE_COPY)
647
    if (env->native_fp_regs) {
648
        save_native_fp_state(env);
649
    }
650
#endif
651
    /* restore flags in standard format */
652
    env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
653

    
654
    /* restore global registers */
655
#ifdef reg_EAX
656
    EAX = saved_EAX;
657
#endif
658
#ifdef reg_ECX
659
    ECX = saved_ECX;
660
#endif
661
#ifdef reg_EDX
662
    EDX = saved_EDX;
663
#endif
664
#ifdef reg_EBX
665
    EBX = saved_EBX;
666
#endif
667
#ifdef reg_ESP
668
    ESP = saved_ESP;
669
#endif
670
#ifdef reg_EBP
671
    EBP = saved_EBP;
672
#endif
673
#ifdef reg_ESI
674
    ESI = saved_ESI;
675
#endif
676
#ifdef reg_EDI
677
    EDI = saved_EDI;
678
#endif
679
#elif defined(TARGET_ARM)
680
    env->cpsr = compute_cpsr();
681
    /* XXX: Save/restore host fpu exception state?.  */
682
#elif defined(TARGET_SPARC)
683
#if defined(reg_REGWPTR)
684
    REGWPTR = saved_regwptr;
685
#endif
686
#elif defined(TARGET_PPC)
687
#else
688
#error unsupported target CPU
689
#endif
690
#ifdef __sparc__
691
    asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
692
#endif
693
    T0 = saved_T0;
694
    T1 = saved_T1;
695
#if defined(reg_T2)
696
    T2 = saved_T2;
697
#endif
698
    env = saved_env;
699
    return ret;
700
}
701

    
702
/* must only be called from the generated code as an exception can be
703
   generated */
704
void tb_invalidate_page_range(target_ulong start, target_ulong end)
705
{
706
    /* XXX: cannot enable it yet because it yields to MMU exception
707
       where NIP != read address on PowerPC */
708
#if 0
709
    target_ulong phys_addr;
710
    phys_addr = get_phys_addr_code(env, start);
711
    tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
712
#endif
713
}
714

    
715
#if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
716

    
717
void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
718
{
719
    CPUX86State *saved_env;
720

    
721
    saved_env = env;
722
    env = s;
723
    if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
724
        selector &= 0xffff;
725
        cpu_x86_load_seg_cache(env, seg_reg, selector, 
726
                               (selector << 4), 0xffff, 0);
727
    } else {
728
        load_seg(seg_reg, selector);
729
    }
730
    env = saved_env;
731
}
732

    
733
void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
734
{
735
    CPUX86State *saved_env;
736

    
737
    saved_env = env;
738
    env = s;
739
    
740
    helper_fsave((target_ulong)ptr, data32);
741

    
742
    env = saved_env;
743
}
744

    
745
void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
746
{
747
    CPUX86State *saved_env;
748

    
749
    saved_env = env;
750
    env = s;
751
    
752
    helper_frstor((target_ulong)ptr, data32);
753

    
754
    env = saved_env;
755
}
756

    
757
#endif /* TARGET_I386 */
758

    
759
#if !defined(CONFIG_SOFTMMU)
760

    
761
#if defined(TARGET_I386)
762

    
763
/* 'pc' is the host PC at which the exception was raised. 'address' is
764
   the effective address of the memory exception. 'is_write' is 1 if a
765
   write caused the exception and otherwise 0'. 'old_set' is the
766
   signal set which should be restored */
767
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
768
                                    int is_write, sigset_t *old_set, 
769
                                    void *puc)
770
{
771
    TranslationBlock *tb;
772
    int ret;
773

    
774
    if (cpu_single_env)
775
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
776
#if defined(DEBUG_SIGNAL)
777
    qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
778
                pc, address, is_write, *(unsigned long *)old_set);
779
#endif
780
    /* XXX: locking issue */
781
    if (is_write && page_unprotect(address, pc, puc)) {
782
        return 1;
783
    }
784

    
785
    /* see if it is an MMU fault */
786
    ret = cpu_x86_handle_mmu_fault(env, address, is_write, 
787
                                   ((env->hflags & HF_CPL_MASK) == 3), 0);
788
    if (ret < 0)
789
        return 0; /* not an MMU fault */
790
    if (ret == 0)
791
        return 1; /* the MMU fault was handled without causing real CPU fault */
792
    /* now we have a real cpu fault */
793
    tb = tb_find_pc(pc);
794
    if (tb) {
795
        /* the PC is inside the translated code. It means that we have
796
           a virtual CPU fault */
797
        cpu_restore_state(tb, env, pc, puc);
798
    }
799
    if (ret == 1) {
800
#if 0
801
        printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n", 
802
               env->eip, env->cr[2], env->error_code);
803
#endif
804
        /* we restore the process signal mask as the sigreturn should
805
           do it (XXX: use sigsetjmp) */
806
        sigprocmask(SIG_SETMASK, old_set, NULL);
807
        raise_exception_err(EXCP0E_PAGE, env->error_code);
808
    } else {
809
        /* activate soft MMU for this block */
810
        env->hflags |= HF_SOFTMMU_MASK;
811
        cpu_resume_from_signal(env, puc);
812
    }
813
    /* never comes here */
814
    return 1;
815
}
816

    
817
#elif defined(TARGET_ARM)
818
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
819
                                    int is_write, sigset_t *old_set,
820
                                    void *puc)
821
{
822
    TranslationBlock *tb;
823
    int ret;
824

    
825
    if (cpu_single_env)
826
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
827
#if defined(DEBUG_SIGNAL)
828
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
829
           pc, address, is_write, *(unsigned long *)old_set);
830
#endif
831
    /* XXX: locking issue */
832
    if (is_write && page_unprotect(address, pc, puc)) {
833
        return 1;
834
    }
835
    /* see if it is an MMU fault */
836
    ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
837
    if (ret < 0)
838
        return 0; /* not an MMU fault */
839
    if (ret == 0)
840
        return 1; /* the MMU fault was handled without causing real CPU fault */
841
    /* now we have a real cpu fault */
842
    tb = tb_find_pc(pc);
843
    if (tb) {
844
        /* the PC is inside the translated code. It means that we have
845
           a virtual CPU fault */
846
        cpu_restore_state(tb, env, pc, puc);
847
    }
848
    /* we restore the process signal mask as the sigreturn should
849
       do it (XXX: use sigsetjmp) */
850
    sigprocmask(SIG_SETMASK, old_set, NULL);
851
    cpu_loop_exit();
852
}
853
#elif defined(TARGET_SPARC)
854
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
855
                                    int is_write, sigset_t *old_set,
856
                                    void *puc)
857
{
858
    TranslationBlock *tb;
859
    int ret;
860

    
861
    if (cpu_single_env)
862
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
863
#if defined(DEBUG_SIGNAL)
864
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
865
           pc, address, is_write, *(unsigned long *)old_set);
866
#endif
867
    /* XXX: locking issue */
868
    if (is_write && page_unprotect(address, pc, puc)) {
869
        return 1;
870
    }
871
    /* see if it is an MMU fault */
872
    ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
873
    if (ret < 0)
874
        return 0; /* not an MMU fault */
875
    if (ret == 0)
876
        return 1; /* the MMU fault was handled without causing real CPU fault */
877
    /* now we have a real cpu fault */
878
    tb = tb_find_pc(pc);
879
    if (tb) {
880
        /* the PC is inside the translated code. It means that we have
881
           a virtual CPU fault */
882
        cpu_restore_state(tb, env, pc, puc);
883
    }
884
    /* we restore the process signal mask as the sigreturn should
885
       do it (XXX: use sigsetjmp) */
886
    sigprocmask(SIG_SETMASK, old_set, NULL);
887
    cpu_loop_exit();
888
}
889
#elif defined (TARGET_PPC)
890
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
891
                                    int is_write, sigset_t *old_set,
892
                                    void *puc)
893
{
894
    TranslationBlock *tb;
895
    int ret;
896
    
897
    if (cpu_single_env)
898
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
899
#if defined(DEBUG_SIGNAL)
900
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
901
           pc, address, is_write, *(unsigned long *)old_set);
902
#endif
903
    /* XXX: locking issue */
904
    if (is_write && page_unprotect(address, pc, puc)) {
905
        return 1;
906
    }
907

    
908
    /* see if it is an MMU fault */
909
    ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
910
    if (ret < 0)
911
        return 0; /* not an MMU fault */
912
    if (ret == 0)
913
        return 1; /* the MMU fault was handled without causing real CPU fault */
914

    
915
    /* now we have a real cpu fault */
916
    tb = tb_find_pc(pc);
917
    if (tb) {
918
        /* the PC is inside the translated code. It means that we have
919
           a virtual CPU fault */
920
        cpu_restore_state(tb, env, pc, puc);
921
    }
922
    if (ret == 1) {
923
#if 0
924
        printf("PF exception: NIP=0x%08x error=0x%x %p\n", 
925
               env->nip, env->error_code, tb);
926
#endif
927
    /* we restore the process signal mask as the sigreturn should
928
       do it (XXX: use sigsetjmp) */
929
        sigprocmask(SIG_SETMASK, old_set, NULL);
930
        do_raise_exception_err(env->exception_index, env->error_code);
931
    } else {
932
        /* activate soft MMU for this block */
933
        cpu_resume_from_signal(env, puc);
934
    }
935
    /* never comes here */
936
    return 1;
937
}
938
#else
939
#error unsupported target CPU
940
#endif
941

    
942
#if defined(__i386__)
943

    
944
#if defined(USE_CODE_COPY)
945
static void cpu_send_trap(unsigned long pc, int trap, 
946
                          struct ucontext *uc)
947
{
948
    TranslationBlock *tb;
949

    
950
    if (cpu_single_env)
951
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
952
    /* now we have a real cpu fault */
953
    tb = tb_find_pc(pc);
954
    if (tb) {
955
        /* the PC is inside the translated code. It means that we have
956
           a virtual CPU fault */
957
        cpu_restore_state(tb, env, pc, uc);
958
    }
959
    sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
960
    raise_exception_err(trap, env->error_code);
961
}
962
#endif
963

    
964
int cpu_signal_handler(int host_signum, struct siginfo *info, 
965
                       void *puc)
966
{
967
    struct ucontext *uc = puc;
968
    unsigned long pc;
969
    int trapno;
970

    
971
#ifndef REG_EIP
972
/* for glibc 2.1 */
973
#define REG_EIP    EIP
974
#define REG_ERR    ERR
975
#define REG_TRAPNO TRAPNO
976
#endif
977
    pc = uc->uc_mcontext.gregs[REG_EIP];
978
    trapno = uc->uc_mcontext.gregs[REG_TRAPNO];
979
#if defined(TARGET_I386) && defined(USE_CODE_COPY)
980
    if (trapno == 0x00 || trapno == 0x05) {
981
        /* send division by zero or bound exception */
982
        cpu_send_trap(pc, trapno, uc);
983
        return 1;
984
    } else
985
#endif
986
        return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
987
                                 trapno == 0xe ? 
988
                                 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
989
                                 &uc->uc_sigmask, puc);
990
}
991

    
992
#elif defined(__x86_64__)
993

    
994
int cpu_signal_handler(int host_signum, struct siginfo *info,
995
                       void *puc)
996
{
997
    struct ucontext *uc = puc;
998
    unsigned long pc;
999

    
1000
    pc = uc->uc_mcontext.gregs[REG_RIP];
1001
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1002
                             uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ? 
1003
                             (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1004
                             &uc->uc_sigmask, puc);
1005
}
1006

    
1007
#elif defined(__powerpc__)
1008

    
1009
/***********************************************************************
1010
 * signal context platform-specific definitions
1011
 * From Wine
1012
 */
1013
#ifdef linux
1014
/* All Registers access - only for local access */
1015
# define REG_sig(reg_name, context)                ((context)->uc_mcontext.regs->reg_name)
1016
/* Gpr Registers access  */
1017
# define GPR_sig(reg_num, context)                REG_sig(gpr[reg_num], context)
1018
# define IAR_sig(context)                        REG_sig(nip, context)        /* Program counter */
1019
# define MSR_sig(context)                        REG_sig(msr, context)   /* Machine State Register (Supervisor) */
1020
# define CTR_sig(context)                        REG_sig(ctr, context)   /* Count register */
1021
# define XER_sig(context)                        REG_sig(xer, context) /* User's integer exception register */
1022
# define LR_sig(context)                        REG_sig(link, context) /* Link register */
1023
# define CR_sig(context)                        REG_sig(ccr, context) /* Condition register */
1024
/* Float Registers access  */
1025
# define FLOAT_sig(reg_num, context)                (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1026
# define FPSCR_sig(context)                        (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1027
/* Exception Registers access */
1028
# define DAR_sig(context)                        REG_sig(dar, context)
1029
# define DSISR_sig(context)                        REG_sig(dsisr, context)
1030
# define TRAP_sig(context)                        REG_sig(trap, context)
1031
#endif /* linux */
1032

    
1033
#ifdef __APPLE__
1034
# include <sys/ucontext.h>
1035
typedef struct ucontext SIGCONTEXT;
1036
/* All Registers access - only for local access */
1037
# define REG_sig(reg_name, context)                ((context)->uc_mcontext->ss.reg_name)
1038
# define FLOATREG_sig(reg_name, context)        ((context)->uc_mcontext->fs.reg_name)
1039
# define EXCEPREG_sig(reg_name, context)        ((context)->uc_mcontext->es.reg_name)
1040
# define VECREG_sig(reg_name, context)                ((context)->uc_mcontext->vs.reg_name)
1041
/* Gpr Registers access */
1042
# define GPR_sig(reg_num, context)                REG_sig(r##reg_num, context)
1043
# define IAR_sig(context)                        REG_sig(srr0, context)        /* Program counter */
1044
# define MSR_sig(context)                        REG_sig(srr1, context)  /* Machine State Register (Supervisor) */
1045
# define CTR_sig(context)                        REG_sig(ctr, context)
1046
# define XER_sig(context)                        REG_sig(xer, context) /* Link register */
1047
# define LR_sig(context)                        REG_sig(lr, context)  /* User's integer exception register */
1048
# define CR_sig(context)                        REG_sig(cr, context)  /* Condition register */
1049
/* Float Registers access */
1050
# define FLOAT_sig(reg_num, context)                FLOATREG_sig(fpregs[reg_num], context)
1051
# define FPSCR_sig(context)                        ((double)FLOATREG_sig(fpscr, context))
1052
/* Exception Registers access */
1053
# define DAR_sig(context)                        EXCEPREG_sig(dar, context)     /* Fault registers for coredump */
1054
# define DSISR_sig(context)                        EXCEPREG_sig(dsisr, context)
1055
# define TRAP_sig(context)                        EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1056
#endif /* __APPLE__ */
1057

    
1058
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1059
                       void *puc)
1060
{
1061
    struct ucontext *uc = puc;
1062
    unsigned long pc;
1063
    int is_write;
1064

    
1065
    pc = IAR_sig(uc);
1066
    is_write = 0;
1067
#if 0
1068
    /* ppc 4xx case */
1069
    if (DSISR_sig(uc) & 0x00800000)
1070
        is_write = 1;
1071
#else
1072
    if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
1073
        is_write = 1;
1074
#endif
1075
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1076
                             is_write, &uc->uc_sigmask, puc);
1077
}
1078

    
1079
#elif defined(__alpha__)
1080

    
1081
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1082
                           void *puc)
1083
{
1084
    struct ucontext *uc = puc;
1085
    uint32_t *pc = uc->uc_mcontext.sc_pc;
1086
    uint32_t insn = *pc;
1087
    int is_write = 0;
1088

    
1089
    /* XXX: need kernel patch to get write flag faster */
1090
    switch (insn >> 26) {
1091
    case 0x0d: // stw
1092
    case 0x0e: // stb
1093
    case 0x0f: // stq_u
1094
    case 0x24: // stf
1095
    case 0x25: // stg
1096
    case 0x26: // sts
1097
    case 0x27: // stt
1098
    case 0x2c: // stl
1099
    case 0x2d: // stq
1100
    case 0x2e: // stl_c
1101
    case 0x2f: // stq_c
1102
        is_write = 1;
1103
    }
1104

    
1105
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1106
                             is_write, &uc->uc_sigmask, puc);
1107
}
1108
#elif defined(__sparc__)
1109

    
1110
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1111
                       void *puc)
1112
{
1113
    uint32_t *regs = (uint32_t *)(info + 1);
1114
    void *sigmask = (regs + 20);
1115
    unsigned long pc;
1116
    int is_write;
1117
    uint32_t insn;
1118
    
1119
    /* XXX: is there a standard glibc define ? */
1120
    pc = regs[1];
1121
    /* XXX: need kernel patch to get write flag faster */
1122
    is_write = 0;
1123
    insn = *(uint32_t *)pc;
1124
    if ((insn >> 30) == 3) {
1125
      switch((insn >> 19) & 0x3f) {
1126
      case 0x05: // stb
1127
      case 0x06: // sth
1128
      case 0x04: // st
1129
      case 0x07: // std
1130
      case 0x24: // stf
1131
      case 0x27: // stdf
1132
      case 0x25: // stfsr
1133
        is_write = 1;
1134
        break;
1135
      }
1136
    }
1137
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1138
                             is_write, sigmask, NULL);
1139
}
1140

    
1141
#elif defined(__arm__)
1142

    
1143
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1144
                       void *puc)
1145
{
1146
    struct ucontext *uc = puc;
1147
    unsigned long pc;
1148
    int is_write;
1149
    
1150
    pc = uc->uc_mcontext.gregs[R15];
1151
    /* XXX: compute is_write */
1152
    is_write = 0;
1153
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1154
                             is_write,
1155
                             &uc->uc_sigmask);
1156
}
1157

    
1158
#elif defined(__mc68000)
1159

    
1160
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1161
                       void *puc)
1162
{
1163
    struct ucontext *uc = puc;
1164
    unsigned long pc;
1165
    int is_write;
1166
    
1167
    pc = uc->uc_mcontext.gregs[16];
1168
    /* XXX: compute is_write */
1169
    is_write = 0;
1170
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1171
                             is_write,
1172
                             &uc->uc_sigmask, puc);
1173
}
1174

    
1175
#elif defined(__ia64)
1176

    
1177
#ifndef __ISR_VALID
1178
  /* This ought to be in <bits/siginfo.h>... */
1179
# define __ISR_VALID        1
1180
# define si_flags        _sifields._sigfault._si_pad0
1181
#endif
1182

    
1183
int cpu_signal_handler(int host_signum, struct siginfo *info, void *puc)
1184
{
1185
    struct ucontext *uc = puc;
1186
    unsigned long ip;
1187
    int is_write = 0;
1188

    
1189
    ip = uc->uc_mcontext.sc_ip;
1190
    switch (host_signum) {
1191
      case SIGILL:
1192
      case SIGFPE:
1193
      case SIGSEGV:
1194
      case SIGBUS:
1195
      case SIGTRAP:
1196
          if (info->si_code && (info->si_flags & __ISR_VALID))
1197
              /* ISR.W (write-access) is bit 33:  */
1198
              is_write = (info->si_isr >> 33) & 1;
1199
          break;
1200

    
1201
      default:
1202
          break;
1203
    }
1204
    return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1205
                             is_write,
1206
                             &uc->uc_sigmask, puc);
1207
}
1208

    
1209
#else
1210

    
1211
#error host CPU specific signal handler needed
1212

    
1213
#endif
1214

    
1215
#endif /* !defined(CONFIG_SOFTMMU) */