Statistics
| Branch: | Revision:

root / cpu-exec.c @ c5d6edc3

History | View | Annotate | Download (46 kB)

1 7d13299d bellard
/*
2 7d13299d bellard
 *  i386 emulator main execution loop
3 7d13299d bellard
 * 
4 66321a11 bellard
 *  Copyright (c) 2003-2005 Fabrice Bellard
5 7d13299d bellard
 *
6 3ef693a0 bellard
 * This library is free software; you can redistribute it and/or
7 3ef693a0 bellard
 * modify it under the terms of the GNU Lesser General Public
8 3ef693a0 bellard
 * License as published by the Free Software Foundation; either
9 3ef693a0 bellard
 * version 2 of the License, or (at your option) any later version.
10 7d13299d bellard
 *
11 3ef693a0 bellard
 * This library is distributed in the hope that it will be useful,
12 3ef693a0 bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 3ef693a0 bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 3ef693a0 bellard
 * Lesser General Public License for more details.
15 7d13299d bellard
 *
16 3ef693a0 bellard
 * You should have received a copy of the GNU Lesser General Public
17 3ef693a0 bellard
 * License along with this library; if not, write to the Free Software
18 3ef693a0 bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 7d13299d bellard
 */
20 e4533c7a bellard
#include "config.h"
21 93ac68bc bellard
#include "exec.h"
22 956034d7 bellard
#include "disas.h"
23 7d13299d bellard
24 fbf9eeb3 bellard
#if !defined(CONFIG_SOFTMMU)
25 fbf9eeb3 bellard
#undef EAX
26 fbf9eeb3 bellard
#undef ECX
27 fbf9eeb3 bellard
#undef EDX
28 fbf9eeb3 bellard
#undef EBX
29 fbf9eeb3 bellard
#undef ESP
30 fbf9eeb3 bellard
#undef EBP
31 fbf9eeb3 bellard
#undef ESI
32 fbf9eeb3 bellard
#undef EDI
33 fbf9eeb3 bellard
#undef EIP
34 fbf9eeb3 bellard
#include <signal.h>
35 fbf9eeb3 bellard
#include <sys/ucontext.h>
36 fbf9eeb3 bellard
#endif
37 fbf9eeb3 bellard
38 36bdbe54 bellard
int tb_invalidated_flag;
39 36bdbe54 bellard
40 dc99065b bellard
//#define DEBUG_EXEC
41 9de5e440 bellard
//#define DEBUG_SIGNAL
42 7d13299d bellard
43 93ac68bc bellard
#if defined(TARGET_ARM) || defined(TARGET_SPARC)
44 e4533c7a bellard
/* XXX: unify with i386 target */
45 e4533c7a bellard
void cpu_loop_exit(void)
46 e4533c7a bellard
{
47 e4533c7a bellard
    longjmp(env->jmp_env, 1);
48 e4533c7a bellard
}
49 e4533c7a bellard
#endif
50 3475187d bellard
#ifndef TARGET_SPARC
51 3475187d bellard
#define reg_T2
52 3475187d bellard
#endif
53 e4533c7a bellard
54 fbf9eeb3 bellard
/* exit the current TB from a signal handler. The host registers are
55 fbf9eeb3 bellard
   restored in a state compatible with the CPU emulator
56 fbf9eeb3 bellard
 */
57 fbf9eeb3 bellard
void cpu_resume_from_signal(CPUState *env1, void *puc) 
58 fbf9eeb3 bellard
{
59 fbf9eeb3 bellard
#if !defined(CONFIG_SOFTMMU)
60 fbf9eeb3 bellard
    struct ucontext *uc = puc;
61 fbf9eeb3 bellard
#endif
62 fbf9eeb3 bellard
63 fbf9eeb3 bellard
    env = env1;
64 fbf9eeb3 bellard
65 fbf9eeb3 bellard
    /* XXX: restore cpu registers saved in host registers */
66 fbf9eeb3 bellard
67 fbf9eeb3 bellard
#if !defined(CONFIG_SOFTMMU)
68 fbf9eeb3 bellard
    if (puc) {
69 fbf9eeb3 bellard
        /* XXX: use siglongjmp ? */
70 fbf9eeb3 bellard
        sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
71 fbf9eeb3 bellard
    }
72 fbf9eeb3 bellard
#endif
73 fbf9eeb3 bellard
    longjmp(env->jmp_env, 1);
74 fbf9eeb3 bellard
}
75 fbf9eeb3 bellard
76 8a40a180 bellard
77 8a40a180 bellard
static TranslationBlock *tb_find_slow(target_ulong pc,
78 8a40a180 bellard
                                      target_ulong cs_base,
79 8a40a180 bellard
                                      unsigned int flags)
80 8a40a180 bellard
{
81 8a40a180 bellard
    TranslationBlock *tb, **ptb1;
82 8a40a180 bellard
    int code_gen_size;
83 8a40a180 bellard
    unsigned int h;
84 8a40a180 bellard
    target_ulong phys_pc, phys_page1, phys_page2, virt_page2;
85 8a40a180 bellard
    uint8_t *tc_ptr;
86 8a40a180 bellard
    
87 8a40a180 bellard
    spin_lock(&tb_lock);
88 8a40a180 bellard
89 8a40a180 bellard
    tb_invalidated_flag = 0;
90 8a40a180 bellard
    
91 8a40a180 bellard
    regs_to_env(); /* XXX: do it just before cpu_gen_code() */
92 8a40a180 bellard
    
93 8a40a180 bellard
    /* find translated block using physical mappings */
94 8a40a180 bellard
    phys_pc = get_phys_addr_code(env, pc);
95 8a40a180 bellard
    phys_page1 = phys_pc & TARGET_PAGE_MASK;
96 8a40a180 bellard
    phys_page2 = -1;
97 8a40a180 bellard
    h = tb_phys_hash_func(phys_pc);
98 8a40a180 bellard
    ptb1 = &tb_phys_hash[h];
99 8a40a180 bellard
    for(;;) {
100 8a40a180 bellard
        tb = *ptb1;
101 8a40a180 bellard
        if (!tb)
102 8a40a180 bellard
            goto not_found;
103 8a40a180 bellard
        if (tb->pc == pc && 
104 8a40a180 bellard
            tb->page_addr[0] == phys_page1 &&
105 8a40a180 bellard
            tb->cs_base == cs_base && 
106 8a40a180 bellard
            tb->flags == flags) {
107 8a40a180 bellard
            /* check next page if needed */
108 8a40a180 bellard
            if (tb->page_addr[1] != -1) {
109 8a40a180 bellard
                virt_page2 = (pc & TARGET_PAGE_MASK) + 
110 8a40a180 bellard
                    TARGET_PAGE_SIZE;
111 8a40a180 bellard
                phys_page2 = get_phys_addr_code(env, virt_page2);
112 8a40a180 bellard
                if (tb->page_addr[1] == phys_page2)
113 8a40a180 bellard
                    goto found;
114 8a40a180 bellard
            } else {
115 8a40a180 bellard
                goto found;
116 8a40a180 bellard
            }
117 8a40a180 bellard
        }
118 8a40a180 bellard
        ptb1 = &tb->phys_hash_next;
119 8a40a180 bellard
    }
120 8a40a180 bellard
 not_found:
121 8a40a180 bellard
    /* if no translated code available, then translate it now */
122 8a40a180 bellard
    tb = tb_alloc(pc);
123 8a40a180 bellard
    if (!tb) {
124 8a40a180 bellard
        /* flush must be done */
125 8a40a180 bellard
        tb_flush(env);
126 8a40a180 bellard
        /* cannot fail at this point */
127 8a40a180 bellard
        tb = tb_alloc(pc);
128 8a40a180 bellard
        /* don't forget to invalidate previous TB info */
129 15388002 bellard
        tb_invalidated_flag = 1;
130 8a40a180 bellard
    }
131 8a40a180 bellard
    tc_ptr = code_gen_ptr;
132 8a40a180 bellard
    tb->tc_ptr = tc_ptr;
133 8a40a180 bellard
    tb->cs_base = cs_base;
134 8a40a180 bellard
    tb->flags = flags;
135 8a40a180 bellard
    cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
136 8a40a180 bellard
    code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
137 8a40a180 bellard
    
138 8a40a180 bellard
    /* check next page if needed */
139 8a40a180 bellard
    virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
140 8a40a180 bellard
    phys_page2 = -1;
141 8a40a180 bellard
    if ((pc & TARGET_PAGE_MASK) != virt_page2) {
142 8a40a180 bellard
        phys_page2 = get_phys_addr_code(env, virt_page2);
143 8a40a180 bellard
    }
144 8a40a180 bellard
    tb_link_phys(tb, phys_pc, phys_page2);
145 8a40a180 bellard
    
146 8a40a180 bellard
 found:
147 8a40a180 bellard
    /* we add the TB in the virtual pc hash table */
148 8a40a180 bellard
    env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)] = tb;
149 8a40a180 bellard
    spin_unlock(&tb_lock);
150 8a40a180 bellard
    return tb;
151 8a40a180 bellard
}
152 8a40a180 bellard
153 8a40a180 bellard
static inline TranslationBlock *tb_find_fast(void)
154 8a40a180 bellard
{
155 8a40a180 bellard
    TranslationBlock *tb;
156 8a40a180 bellard
    target_ulong cs_base, pc;
157 8a40a180 bellard
    unsigned int flags;
158 8a40a180 bellard
159 8a40a180 bellard
    /* we record a subset of the CPU state. It will
160 8a40a180 bellard
       always be the same before a given translated block
161 8a40a180 bellard
       is executed. */
162 8a40a180 bellard
#if defined(TARGET_I386)
163 8a40a180 bellard
    flags = env->hflags;
164 8a40a180 bellard
    flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
165 8a40a180 bellard
    cs_base = env->segs[R_CS].base;
166 8a40a180 bellard
    pc = cs_base + env->eip;
167 8a40a180 bellard
#elif defined(TARGET_ARM)
168 8a40a180 bellard
    flags = env->thumb | (env->vfp.vec_len << 1)
169 b5ff1b31 bellard
            | (env->vfp.vec_stride << 4);
170 b5ff1b31 bellard
    if ((env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR)
171 b5ff1b31 bellard
        flags |= (1 << 6);
172 40f137e1 pbrook
    if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30))
173 40f137e1 pbrook
        flags |= (1 << 7);
174 8a40a180 bellard
    cs_base = 0;
175 8a40a180 bellard
    pc = env->regs[15];
176 8a40a180 bellard
#elif defined(TARGET_SPARC)
177 8a40a180 bellard
#ifdef TARGET_SPARC64
178 8a40a180 bellard
    flags = (env->pstate << 2) | ((env->lsu & (DMMU_E | IMMU_E)) >> 2);
179 8a40a180 bellard
#else
180 8a40a180 bellard
    flags = env->psrs | ((env->mmuregs[0] & (MMU_E | MMU_NF)) << 1);
181 8a40a180 bellard
#endif
182 8a40a180 bellard
    cs_base = env->npc;
183 8a40a180 bellard
    pc = env->pc;
184 8a40a180 bellard
#elif defined(TARGET_PPC)
185 8a40a180 bellard
    flags = (msr_pr << MSR_PR) | (msr_fp << MSR_FP) |
186 8a40a180 bellard
        (msr_se << MSR_SE) | (msr_le << MSR_LE);
187 8a40a180 bellard
    cs_base = 0;
188 8a40a180 bellard
    pc = env->nip;
189 8a40a180 bellard
#elif defined(TARGET_MIPS)
190 56b19403 pbrook
    flags = env->hflags & (MIPS_HFLAG_TMASK | MIPS_HFLAG_BMASK);
191 cc9442b9 bellard
    cs_base = 0;
192 8a40a180 bellard
    pc = env->PC;
193 fdf9b3e8 bellard
#elif defined(TARGET_SH4)
194 fdf9b3e8 bellard
    flags = env->sr & (SR_MD | SR_RB);
195 fdf9b3e8 bellard
    cs_base = 0;         /* XXXXX */
196 fdf9b3e8 bellard
    pc = env->pc;
197 8a40a180 bellard
#else
198 8a40a180 bellard
#error unsupported CPU
199 8a40a180 bellard
#endif
200 8a40a180 bellard
    tb = env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)];
201 8a40a180 bellard
    if (__builtin_expect(!tb || tb->pc != pc || tb->cs_base != cs_base ||
202 8a40a180 bellard
                         tb->flags != flags, 0)) {
203 8a40a180 bellard
        tb = tb_find_slow(pc, cs_base, flags);
204 15388002 bellard
        /* Note: we do it here to avoid a gcc bug on Mac OS X when
205 15388002 bellard
           doing it in tb_find_slow */
206 15388002 bellard
        if (tb_invalidated_flag) {
207 15388002 bellard
            /* as some TB could have been invalidated because
208 15388002 bellard
               of memory exceptions while generating the code, we
209 15388002 bellard
               must recompute the hash index here */
210 15388002 bellard
            T0 = 0;
211 15388002 bellard
        }
212 8a40a180 bellard
    }
213 8a40a180 bellard
    return tb;
214 8a40a180 bellard
}
215 8a40a180 bellard
216 8a40a180 bellard
217 7d13299d bellard
/* main execution loop */
218 7d13299d bellard
219 e4533c7a bellard
int cpu_exec(CPUState *env1)
220 7d13299d bellard
{
221 3475187d bellard
    int saved_T0, saved_T1;
222 3475187d bellard
#if defined(reg_T2)
223 3475187d bellard
    int saved_T2;
224 3475187d bellard
#endif
225 e4533c7a bellard
    CPUState *saved_env;
226 3475187d bellard
#if defined(TARGET_I386)
227 04369ff2 bellard
#ifdef reg_EAX
228 04369ff2 bellard
    int saved_EAX;
229 04369ff2 bellard
#endif
230 04369ff2 bellard
#ifdef reg_ECX
231 04369ff2 bellard
    int saved_ECX;
232 04369ff2 bellard
#endif
233 04369ff2 bellard
#ifdef reg_EDX
234 04369ff2 bellard
    int saved_EDX;
235 04369ff2 bellard
#endif
236 04369ff2 bellard
#ifdef reg_EBX
237 04369ff2 bellard
    int saved_EBX;
238 04369ff2 bellard
#endif
239 04369ff2 bellard
#ifdef reg_ESP
240 04369ff2 bellard
    int saved_ESP;
241 04369ff2 bellard
#endif
242 04369ff2 bellard
#ifdef reg_EBP
243 04369ff2 bellard
    int saved_EBP;
244 04369ff2 bellard
#endif
245 04369ff2 bellard
#ifdef reg_ESI
246 04369ff2 bellard
    int saved_ESI;
247 04369ff2 bellard
#endif
248 04369ff2 bellard
#ifdef reg_EDI
249 04369ff2 bellard
    int saved_EDI;
250 04369ff2 bellard
#endif
251 3475187d bellard
#elif defined(TARGET_SPARC)
252 3475187d bellard
#if defined(reg_REGWPTR)
253 3475187d bellard
    uint32_t *saved_regwptr;
254 3475187d bellard
#endif
255 3475187d bellard
#endif
256 8c6939c0 bellard
#ifdef __sparc__
257 8c6939c0 bellard
    int saved_i7, tmp_T0;
258 8c6939c0 bellard
#endif
259 8a40a180 bellard
    int ret, interrupt_request;
260 7d13299d bellard
    void (*gen_func)(void);
261 8a40a180 bellard
    TranslationBlock *tb;
262 c27004ec bellard
    uint8_t *tc_ptr;
263 8c6939c0 bellard
264 5a1e3cfc bellard
#if defined(TARGET_I386)
265 5a1e3cfc bellard
    /* handle exit of HALTED state */
266 5a1e3cfc bellard
    if (env1->hflags & HF_HALTED_MASK) {
267 5a1e3cfc bellard
        /* disable halt condition */
268 5a1e3cfc bellard
        if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
269 5a1e3cfc bellard
            (env1->eflags & IF_MASK)) {
270 5a1e3cfc bellard
            env1->hflags &= ~HF_HALTED_MASK;
271 5a1e3cfc bellard
        } else {
272 5a1e3cfc bellard
            return EXCP_HALTED;
273 e80e1cc4 bellard
        }
274 e80e1cc4 bellard
    }
275 e80e1cc4 bellard
#elif defined(TARGET_PPC)
276 50443c98 bellard
    if (env1->halted) {
277 e80e1cc4 bellard
        if (env1->msr[MSR_EE] && 
278 e80e1cc4 bellard
            (env1->interrupt_request & 
279 e80e1cc4 bellard
             (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER))) {
280 50443c98 bellard
            env1->halted = 0;
281 e80e1cc4 bellard
        } else {
282 e80e1cc4 bellard
            return EXCP_HALTED;
283 5a1e3cfc bellard
        }
284 5a1e3cfc bellard
    }
285 ba3c64fb bellard
#elif defined(TARGET_SPARC)
286 ba3c64fb bellard
    if (env1->halted) {
287 ba3c64fb bellard
        if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
288 ba3c64fb bellard
            (env1->psret != 0)) {
289 ba3c64fb bellard
            env1->halted = 0;
290 ba3c64fb bellard
        } else {
291 ba3c64fb bellard
            return EXCP_HALTED;
292 ba3c64fb bellard
        }
293 ba3c64fb bellard
    }
294 9332f9da bellard
#elif defined(TARGET_ARM)
295 9332f9da bellard
    if (env1->halted) {
296 9332f9da bellard
        /* An interrupt wakes the CPU even if the I and F CPSR bits are
297 9332f9da bellard
           set.  */
298 9332f9da bellard
        if (env1->interrupt_request
299 9332f9da bellard
            & (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD)) {
300 9332f9da bellard
            env1->halted = 0;
301 9332f9da bellard
        } else {
302 9332f9da bellard
            return EXCP_HALTED;
303 9332f9da bellard
        }
304 9332f9da bellard
    }
305 6810e154 bellard
#elif defined(TARGET_MIPS)
306 6810e154 bellard
    if (env1->halted) {
307 6810e154 bellard
        if (env1->interrupt_request &
308 6810e154 bellard
            (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER)) {
309 6810e154 bellard
            env1->halted = 0;
310 6810e154 bellard
        } else {
311 6810e154 bellard
            return EXCP_HALTED;
312 6810e154 bellard
        }
313 6810e154 bellard
    }
314 5a1e3cfc bellard
#endif
315 5a1e3cfc bellard
316 6a00d601 bellard
    cpu_single_env = env1; 
317 6a00d601 bellard
318 7d13299d bellard
    /* first we save global registers */
319 c27004ec bellard
    saved_env = env;
320 c27004ec bellard
    env = env1;
321 7d13299d bellard
    saved_T0 = T0;
322 7d13299d bellard
    saved_T1 = T1;
323 3475187d bellard
#if defined(reg_T2)
324 e4533c7a bellard
    saved_T2 = T2;
325 3475187d bellard
#endif
326 e4533c7a bellard
#ifdef __sparc__
327 e4533c7a bellard
    /* we also save i7 because longjmp may not restore it */
328 e4533c7a bellard
    asm volatile ("mov %%i7, %0" : "=r" (saved_i7));
329 e4533c7a bellard
#endif
330 e4533c7a bellard
331 e4533c7a bellard
#if defined(TARGET_I386)
332 04369ff2 bellard
#ifdef reg_EAX
333 04369ff2 bellard
    saved_EAX = EAX;
334 04369ff2 bellard
#endif
335 04369ff2 bellard
#ifdef reg_ECX
336 04369ff2 bellard
    saved_ECX = ECX;
337 04369ff2 bellard
#endif
338 04369ff2 bellard
#ifdef reg_EDX
339 04369ff2 bellard
    saved_EDX = EDX;
340 04369ff2 bellard
#endif
341 04369ff2 bellard
#ifdef reg_EBX
342 04369ff2 bellard
    saved_EBX = EBX;
343 04369ff2 bellard
#endif
344 04369ff2 bellard
#ifdef reg_ESP
345 04369ff2 bellard
    saved_ESP = ESP;
346 04369ff2 bellard
#endif
347 04369ff2 bellard
#ifdef reg_EBP
348 04369ff2 bellard
    saved_EBP = EBP;
349 04369ff2 bellard
#endif
350 04369ff2 bellard
#ifdef reg_ESI
351 04369ff2 bellard
    saved_ESI = ESI;
352 04369ff2 bellard
#endif
353 04369ff2 bellard
#ifdef reg_EDI
354 04369ff2 bellard
    saved_EDI = EDI;
355 04369ff2 bellard
#endif
356 0d1a29f9 bellard
357 0d1a29f9 bellard
    env_to_regs();
358 9de5e440 bellard
    /* put eflags in CPU temporary format */
359 fc2b4c48 bellard
    CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
360 fc2b4c48 bellard
    DF = 1 - (2 * ((env->eflags >> 10) & 1));
361 9de5e440 bellard
    CC_OP = CC_OP_EFLAGS;
362 fc2b4c48 bellard
    env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
363 e4533c7a bellard
#elif defined(TARGET_ARM)
364 93ac68bc bellard
#elif defined(TARGET_SPARC)
365 3475187d bellard
#if defined(reg_REGWPTR)
366 3475187d bellard
    saved_regwptr = REGWPTR;
367 3475187d bellard
#endif
368 67867308 bellard
#elif defined(TARGET_PPC)
369 6af0bf9c bellard
#elif defined(TARGET_MIPS)
370 fdf9b3e8 bellard
#elif defined(TARGET_SH4)
371 fdf9b3e8 bellard
    /* XXXXX */
372 e4533c7a bellard
#else
373 e4533c7a bellard
#error unsupported target CPU
374 e4533c7a bellard
#endif
375 3fb2ded1 bellard
    env->exception_index = -1;
376 9d27abd9 bellard
377 7d13299d bellard
    /* prepare setjmp context for exception handling */
378 3fb2ded1 bellard
    for(;;) {
379 3fb2ded1 bellard
        if (setjmp(env->jmp_env) == 0) {
380 ee8b7021 bellard
            env->current_tb = NULL;
381 3fb2ded1 bellard
            /* if an exception is pending, we execute it here */
382 3fb2ded1 bellard
            if (env->exception_index >= 0) {
383 3fb2ded1 bellard
                if (env->exception_index >= EXCP_INTERRUPT) {
384 3fb2ded1 bellard
                    /* exit request from the cpu execution loop */
385 3fb2ded1 bellard
                    ret = env->exception_index;
386 3fb2ded1 bellard
                    break;
387 3fb2ded1 bellard
                } else if (env->user_mode_only) {
388 3fb2ded1 bellard
                    /* if user mode only, we simulate a fake exception
389 3fb2ded1 bellard
                       which will be hanlded outside the cpu execution
390 3fb2ded1 bellard
                       loop */
391 83479e77 bellard
#if defined(TARGET_I386)
392 3fb2ded1 bellard
                    do_interrupt_user(env->exception_index, 
393 3fb2ded1 bellard
                                      env->exception_is_int, 
394 3fb2ded1 bellard
                                      env->error_code, 
395 3fb2ded1 bellard
                                      env->exception_next_eip);
396 83479e77 bellard
#endif
397 3fb2ded1 bellard
                    ret = env->exception_index;
398 3fb2ded1 bellard
                    break;
399 3fb2ded1 bellard
                } else {
400 83479e77 bellard
#if defined(TARGET_I386)
401 3fb2ded1 bellard
                    /* simulate a real cpu exception. On i386, it can
402 3fb2ded1 bellard
                       trigger new exceptions, but we do not handle
403 3fb2ded1 bellard
                       double or triple faults yet. */
404 3fb2ded1 bellard
                    do_interrupt(env->exception_index, 
405 3fb2ded1 bellard
                                 env->exception_is_int, 
406 3fb2ded1 bellard
                                 env->error_code, 
407 d05e66d2 bellard
                                 env->exception_next_eip, 0);
408 ce09776b bellard
#elif defined(TARGET_PPC)
409 ce09776b bellard
                    do_interrupt(env);
410 6af0bf9c bellard
#elif defined(TARGET_MIPS)
411 6af0bf9c bellard
                    do_interrupt(env);
412 e95c8d51 bellard
#elif defined(TARGET_SPARC)
413 1a0c3292 bellard
                    do_interrupt(env->exception_index);
414 b5ff1b31 bellard
#elif defined(TARGET_ARM)
415 b5ff1b31 bellard
                    do_interrupt(env);
416 fdf9b3e8 bellard
#elif defined(TARGET_SH4)
417 fdf9b3e8 bellard
                    do_interrupt(env);
418 83479e77 bellard
#endif
419 3fb2ded1 bellard
                }
420 3fb2ded1 bellard
                env->exception_index = -1;
421 9df217a3 bellard
            } 
422 9df217a3 bellard
#ifdef USE_KQEMU
423 9df217a3 bellard
            if (kqemu_is_ok(env) && env->interrupt_request == 0) {
424 9df217a3 bellard
                int ret;
425 9df217a3 bellard
                env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
426 9df217a3 bellard
                ret = kqemu_cpu_exec(env);
427 9df217a3 bellard
                /* put eflags in CPU temporary format */
428 9df217a3 bellard
                CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
429 9df217a3 bellard
                DF = 1 - (2 * ((env->eflags >> 10) & 1));
430 9df217a3 bellard
                CC_OP = CC_OP_EFLAGS;
431 9df217a3 bellard
                env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
432 9df217a3 bellard
                if (ret == 1) {
433 9df217a3 bellard
                    /* exception */
434 9df217a3 bellard
                    longjmp(env->jmp_env, 1);
435 9df217a3 bellard
                } else if (ret == 2) {
436 9df217a3 bellard
                    /* softmmu execution needed */
437 9df217a3 bellard
                } else {
438 9df217a3 bellard
                    if (env->interrupt_request != 0) {
439 9df217a3 bellard
                        /* hardware interrupt will be executed just after */
440 9df217a3 bellard
                    } else {
441 9df217a3 bellard
                        /* otherwise, we restart */
442 9df217a3 bellard
                        longjmp(env->jmp_env, 1);
443 9df217a3 bellard
                    }
444 9df217a3 bellard
                }
445 3fb2ded1 bellard
            }
446 9df217a3 bellard
#endif
447 9df217a3 bellard
448 3fb2ded1 bellard
            T0 = 0; /* force lookup of first TB */
449 3fb2ded1 bellard
            for(;;) {
450 8c6939c0 bellard
#ifdef __sparc__
451 3fb2ded1 bellard
                /* g1 can be modified by some libc? functions */ 
452 3fb2ded1 bellard
                tmp_T0 = T0;
453 8c6939c0 bellard
#endif            
454 68a79315 bellard
                interrupt_request = env->interrupt_request;
455 2e255c6b bellard
                if (__builtin_expect(interrupt_request, 0)) {
456 68a79315 bellard
#if defined(TARGET_I386)
457 68a79315 bellard
                    /* if hardware interrupt pending, we execute it */
458 68a79315 bellard
                    if ((interrupt_request & CPU_INTERRUPT_HARD) &&
459 3f337316 bellard
                        (env->eflags & IF_MASK) && 
460 3f337316 bellard
                        !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
461 68a79315 bellard
                        int intno;
462 fbf9eeb3 bellard
                        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
463 a541f297 bellard
                        intno = cpu_get_pic_interrupt(env);
464 f193c797 bellard
                        if (loglevel & CPU_LOG_TB_IN_ASM) {
465 68a79315 bellard
                            fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
466 68a79315 bellard
                        }
467 d05e66d2 bellard
                        do_interrupt(intno, 0, 0, 0, 1);
468 907a5b26 bellard
                        /* ensure that no TB jump will be modified as
469 907a5b26 bellard
                           the program flow was changed */
470 907a5b26 bellard
#ifdef __sparc__
471 907a5b26 bellard
                        tmp_T0 = 0;
472 907a5b26 bellard
#else
473 907a5b26 bellard
                        T0 = 0;
474 907a5b26 bellard
#endif
475 68a79315 bellard
                    }
476 ce09776b bellard
#elif defined(TARGET_PPC)
477 9fddaa0c bellard
#if 0
478 9fddaa0c bellard
                    if ((interrupt_request & CPU_INTERRUPT_RESET)) {
479 9fddaa0c bellard
                        cpu_ppc_reset(env);
480 9fddaa0c bellard
                    }
481 9fddaa0c bellard
#endif
482 9fddaa0c bellard
                    if (msr_ee != 0) {
483 8a40a180 bellard
                        if ((interrupt_request & CPU_INTERRUPT_HARD)) {
484 9fddaa0c bellard
                            /* Raise it */
485 9fddaa0c bellard
                            env->exception_index = EXCP_EXTERNAL;
486 9fddaa0c bellard
                            env->error_code = 0;
487 ce09776b bellard
                            do_interrupt(env);
488 8a40a180 bellard
                            env->interrupt_request &= ~CPU_INTERRUPT_HARD;
489 8a40a180 bellard
#ifdef __sparc__
490 8a40a180 bellard
                            tmp_T0 = 0;
491 8a40a180 bellard
#else
492 8a40a180 bellard
                            T0 = 0;
493 8a40a180 bellard
#endif
494 8a40a180 bellard
                        } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) {
495 8a40a180 bellard
                            /* Raise it */
496 8a40a180 bellard
                            env->exception_index = EXCP_DECR;
497 8a40a180 bellard
                            env->error_code = 0;
498 8a40a180 bellard
                            do_interrupt(env);
499 9fddaa0c bellard
                            env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
500 8a40a180 bellard
#ifdef __sparc__
501 8a40a180 bellard
                            tmp_T0 = 0;
502 8a40a180 bellard
#else
503 8a40a180 bellard
                            T0 = 0;
504 8a40a180 bellard
#endif
505 8a40a180 bellard
                        }
506 ce09776b bellard
                    }
507 6af0bf9c bellard
#elif defined(TARGET_MIPS)
508 6af0bf9c bellard
                    if ((interrupt_request & CPU_INTERRUPT_HARD) &&
509 6af0bf9c bellard
                        (env->CP0_Status & (1 << CP0St_IE)) &&
510 7ebab699 bellard
                        (env->CP0_Status & env->CP0_Cause & 0x0000FF00) &&
511 6af0bf9c bellard
                        !(env->hflags & MIPS_HFLAG_EXL) &&
512 6af0bf9c bellard
                        !(env->hflags & MIPS_HFLAG_ERL) &&
513 6af0bf9c bellard
                        !(env->hflags & MIPS_HFLAG_DM)) {
514 6af0bf9c bellard
                        /* Raise it */
515 6af0bf9c bellard
                        env->exception_index = EXCP_EXT_INTERRUPT;
516 6af0bf9c bellard
                        env->error_code = 0;
517 6af0bf9c bellard
                        do_interrupt(env);
518 6af0bf9c bellard
                        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
519 8a40a180 bellard
#ifdef __sparc__
520 8a40a180 bellard
                        tmp_T0 = 0;
521 8a40a180 bellard
#else
522 8a40a180 bellard
                        T0 = 0;
523 8a40a180 bellard
#endif
524 6af0bf9c bellard
                    }
525 e95c8d51 bellard
#elif defined(TARGET_SPARC)
526 66321a11 bellard
                    if ((interrupt_request & CPU_INTERRUPT_HARD) &&
527 66321a11 bellard
                        (env->psret != 0)) {
528 66321a11 bellard
                        int pil = env->interrupt_index & 15;
529 66321a11 bellard
                        int type = env->interrupt_index & 0xf0;
530 66321a11 bellard
531 66321a11 bellard
                        if (((type == TT_EXTINT) &&
532 66321a11 bellard
                             (pil == 15 || pil > env->psrpil)) ||
533 66321a11 bellard
                            type != TT_EXTINT) {
534 66321a11 bellard
                            env->interrupt_request &= ~CPU_INTERRUPT_HARD;
535 66321a11 bellard
                            do_interrupt(env->interrupt_index);
536 66321a11 bellard
                            env->interrupt_index = 0;
537 8a40a180 bellard
#ifdef __sparc__
538 8a40a180 bellard
                            tmp_T0 = 0;
539 8a40a180 bellard
#else
540 8a40a180 bellard
                            T0 = 0;
541 8a40a180 bellard
#endif
542 66321a11 bellard
                        }
543 e95c8d51 bellard
                    } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
544 e95c8d51 bellard
                        //do_interrupt(0, 0, 0, 0, 0);
545 e95c8d51 bellard
                        env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
546 ba3c64fb bellard
                    } else if (interrupt_request & CPU_INTERRUPT_HALT) {
547 ba3c64fb bellard
                        env1->halted = 1;
548 ba3c64fb bellard
                        return EXCP_HALTED;
549 ba3c64fb bellard
                    }
550 b5ff1b31 bellard
#elif defined(TARGET_ARM)
551 b5ff1b31 bellard
                    if (interrupt_request & CPU_INTERRUPT_FIQ
552 b5ff1b31 bellard
                        && !(env->uncached_cpsr & CPSR_F)) {
553 b5ff1b31 bellard
                        env->exception_index = EXCP_FIQ;
554 b5ff1b31 bellard
                        do_interrupt(env);
555 b5ff1b31 bellard
                    }
556 b5ff1b31 bellard
                    if (interrupt_request & CPU_INTERRUPT_HARD
557 b5ff1b31 bellard
                        && !(env->uncached_cpsr & CPSR_I)) {
558 b5ff1b31 bellard
                        env->exception_index = EXCP_IRQ;
559 b5ff1b31 bellard
                        do_interrupt(env);
560 b5ff1b31 bellard
                    }
561 fdf9b3e8 bellard
#elif defined(TARGET_SH4)
562 fdf9b3e8 bellard
                    /* XXXXX */
563 68a79315 bellard
#endif
564 9d05095e bellard
                   /* Don't use the cached interupt_request value,
565 9d05095e bellard
                      do_interrupt may have updated the EXITTB flag. */
566 b5ff1b31 bellard
                    if (env->interrupt_request & CPU_INTERRUPT_EXITTB) {
567 bf3e8bf1 bellard
                        env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
568 bf3e8bf1 bellard
                        /* ensure that no TB jump will be modified as
569 bf3e8bf1 bellard
                           the program flow was changed */
570 bf3e8bf1 bellard
#ifdef __sparc__
571 bf3e8bf1 bellard
                        tmp_T0 = 0;
572 bf3e8bf1 bellard
#else
573 bf3e8bf1 bellard
                        T0 = 0;
574 bf3e8bf1 bellard
#endif
575 bf3e8bf1 bellard
                    }
576 68a79315 bellard
                    if (interrupt_request & CPU_INTERRUPT_EXIT) {
577 68a79315 bellard
                        env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
578 68a79315 bellard
                        env->exception_index = EXCP_INTERRUPT;
579 68a79315 bellard
                        cpu_loop_exit();
580 68a79315 bellard
                    }
581 3fb2ded1 bellard
                }
582 7d13299d bellard
#ifdef DEBUG_EXEC
583 b5ff1b31 bellard
                if ((loglevel & CPU_LOG_TB_CPU)) {
584 e4533c7a bellard
#if defined(TARGET_I386)
585 3fb2ded1 bellard
                    /* restore flags in standard format */
586 fc9f715d bellard
#ifdef reg_EAX
587 3fb2ded1 bellard
                    env->regs[R_EAX] = EAX;
588 fc9f715d bellard
#endif
589 fc9f715d bellard
#ifdef reg_EBX
590 3fb2ded1 bellard
                    env->regs[R_EBX] = EBX;
591 fc9f715d bellard
#endif
592 fc9f715d bellard
#ifdef reg_ECX
593 3fb2ded1 bellard
                    env->regs[R_ECX] = ECX;
594 fc9f715d bellard
#endif
595 fc9f715d bellard
#ifdef reg_EDX
596 3fb2ded1 bellard
                    env->regs[R_EDX] = EDX;
597 fc9f715d bellard
#endif
598 fc9f715d bellard
#ifdef reg_ESI
599 3fb2ded1 bellard
                    env->regs[R_ESI] = ESI;
600 fc9f715d bellard
#endif
601 fc9f715d bellard
#ifdef reg_EDI
602 3fb2ded1 bellard
                    env->regs[R_EDI] = EDI;
603 fc9f715d bellard
#endif
604 fc9f715d bellard
#ifdef reg_EBP
605 3fb2ded1 bellard
                    env->regs[R_EBP] = EBP;
606 fc9f715d bellard
#endif
607 fc9f715d bellard
#ifdef reg_ESP
608 3fb2ded1 bellard
                    env->regs[R_ESP] = ESP;
609 fc9f715d bellard
#endif
610 3fb2ded1 bellard
                    env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
611 7fe48483 bellard
                    cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
612 3fb2ded1 bellard
                    env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
613 e4533c7a bellard
#elif defined(TARGET_ARM)
614 7fe48483 bellard
                    cpu_dump_state(env, logfile, fprintf, 0);
615 93ac68bc bellard
#elif defined(TARGET_SPARC)
616 3475187d bellard
                    REGWPTR = env->regbase + (env->cwp * 16);
617 3475187d bellard
                    env->regwptr = REGWPTR;
618 3475187d bellard
                    cpu_dump_state(env, logfile, fprintf, 0);
619 67867308 bellard
#elif defined(TARGET_PPC)
620 7fe48483 bellard
                    cpu_dump_state(env, logfile, fprintf, 0);
621 6af0bf9c bellard
#elif defined(TARGET_MIPS)
622 6af0bf9c bellard
                    cpu_dump_state(env, logfile, fprintf, 0);
623 fdf9b3e8 bellard
#elif defined(TARGET_SH4)
624 fdf9b3e8 bellard
                    cpu_dump_state(env, logfile, fprintf, 0);
625 e4533c7a bellard
#else
626 e4533c7a bellard
#error unsupported target CPU 
627 e4533c7a bellard
#endif
628 3fb2ded1 bellard
                }
629 7d13299d bellard
#endif
630 8a40a180 bellard
                tb = tb_find_fast();
631 9d27abd9 bellard
#ifdef DEBUG_EXEC
632 c1135f61 bellard
                if ((loglevel & CPU_LOG_EXEC)) {
633 c27004ec bellard
                    fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
634 c27004ec bellard
                            (long)tb->tc_ptr, tb->pc,
635 c27004ec bellard
                            lookup_symbol(tb->pc));
636 3fb2ded1 bellard
                }
637 9d27abd9 bellard
#endif
638 8c6939c0 bellard
#ifdef __sparc__
639 3fb2ded1 bellard
                T0 = tmp_T0;
640 8c6939c0 bellard
#endif            
641 8a40a180 bellard
                /* see if we can patch the calling TB. When the TB
642 8a40a180 bellard
                   spans two pages, we cannot safely do a direct
643 8a40a180 bellard
                   jump. */
644 c27004ec bellard
                {
645 8a40a180 bellard
                    if (T0 != 0 &&
646 f32fc648 bellard
#if USE_KQEMU
647 f32fc648 bellard
                        (env->kqemu_enabled != 2) &&
648 f32fc648 bellard
#endif
649 8a40a180 bellard
                        tb->page_addr[1] == -1
650 bf3e8bf1 bellard
#if defined(TARGET_I386) && defined(USE_CODE_COPY)
651 bf3e8bf1 bellard
                    && (tb->cflags & CF_CODE_COPY) == 
652 bf3e8bf1 bellard
                    (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
653 bf3e8bf1 bellard
#endif
654 bf3e8bf1 bellard
                    ) {
655 3fb2ded1 bellard
                    spin_lock(&tb_lock);
656 c27004ec bellard
                    tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
657 97eb5b14 bellard
#if defined(USE_CODE_COPY)
658 97eb5b14 bellard
                    /* propagates the FP use info */
659 97eb5b14 bellard
                    ((TranslationBlock *)(T0 & ~3))->cflags |= 
660 97eb5b14 bellard
                        (tb->cflags & CF_FP_USED);
661 97eb5b14 bellard
#endif
662 3fb2ded1 bellard
                    spin_unlock(&tb_lock);
663 3fb2ded1 bellard
                }
664 c27004ec bellard
                }
665 3fb2ded1 bellard
                tc_ptr = tb->tc_ptr;
666 83479e77 bellard
                env->current_tb = tb;
667 3fb2ded1 bellard
                /* execute the generated code */
668 3fb2ded1 bellard
                gen_func = (void *)tc_ptr;
669 8c6939c0 bellard
#if defined(__sparc__)
670 3fb2ded1 bellard
                __asm__ __volatile__("call        %0\n\t"
671 3fb2ded1 bellard
                                     "mov        %%o7,%%i0"
672 3fb2ded1 bellard
                                     : /* no outputs */
673 3fb2ded1 bellard
                                     : "r" (gen_func) 
674 3fb2ded1 bellard
                                     : "i0", "i1", "i2", "i3", "i4", "i5");
675 8c6939c0 bellard
#elif defined(__arm__)
676 3fb2ded1 bellard
                asm volatile ("mov pc, %0\n\t"
677 3fb2ded1 bellard
                              ".global exec_loop\n\t"
678 3fb2ded1 bellard
                              "exec_loop:\n\t"
679 3fb2ded1 bellard
                              : /* no outputs */
680 3fb2ded1 bellard
                              : "r" (gen_func)
681 3fb2ded1 bellard
                              : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
682 bf3e8bf1 bellard
#elif defined(TARGET_I386) && defined(USE_CODE_COPY)
683 bf3e8bf1 bellard
{
684 bf3e8bf1 bellard
    if (!(tb->cflags & CF_CODE_COPY)) {
685 97eb5b14 bellard
        if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
686 97eb5b14 bellard
            save_native_fp_state(env);
687 97eb5b14 bellard
        }
688 bf3e8bf1 bellard
        gen_func();
689 bf3e8bf1 bellard
    } else {
690 97eb5b14 bellard
        if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
691 97eb5b14 bellard
            restore_native_fp_state(env);
692 97eb5b14 bellard
        }
693 bf3e8bf1 bellard
        /* we work with native eflags */
694 bf3e8bf1 bellard
        CC_SRC = cc_table[CC_OP].compute_all();
695 bf3e8bf1 bellard
        CC_OP = CC_OP_EFLAGS;
696 bf3e8bf1 bellard
        asm(".globl exec_loop\n"
697 bf3e8bf1 bellard
            "\n"
698 bf3e8bf1 bellard
            "debug1:\n"
699 bf3e8bf1 bellard
            "    pushl %%ebp\n"
700 bf3e8bf1 bellard
            "    fs movl %10, %9\n"
701 bf3e8bf1 bellard
            "    fs movl %11, %%eax\n"
702 bf3e8bf1 bellard
            "    andl $0x400, %%eax\n"
703 bf3e8bf1 bellard
            "    fs orl %8, %%eax\n"
704 bf3e8bf1 bellard
            "    pushl %%eax\n"
705 bf3e8bf1 bellard
            "    popf\n"
706 bf3e8bf1 bellard
            "    fs movl %%esp, %12\n"
707 bf3e8bf1 bellard
            "    fs movl %0, %%eax\n"
708 bf3e8bf1 bellard
            "    fs movl %1, %%ecx\n"
709 bf3e8bf1 bellard
            "    fs movl %2, %%edx\n"
710 bf3e8bf1 bellard
            "    fs movl %3, %%ebx\n"
711 bf3e8bf1 bellard
            "    fs movl %4, %%esp\n"
712 bf3e8bf1 bellard
            "    fs movl %5, %%ebp\n"
713 bf3e8bf1 bellard
            "    fs movl %6, %%esi\n"
714 bf3e8bf1 bellard
            "    fs movl %7, %%edi\n"
715 bf3e8bf1 bellard
            "    fs jmp *%9\n"
716 bf3e8bf1 bellard
            "exec_loop:\n"
717 bf3e8bf1 bellard
            "    fs movl %%esp, %4\n"
718 bf3e8bf1 bellard
            "    fs movl %12, %%esp\n"
719 bf3e8bf1 bellard
            "    fs movl %%eax, %0\n"
720 bf3e8bf1 bellard
            "    fs movl %%ecx, %1\n"
721 bf3e8bf1 bellard
            "    fs movl %%edx, %2\n"
722 bf3e8bf1 bellard
            "    fs movl %%ebx, %3\n"
723 bf3e8bf1 bellard
            "    fs movl %%ebp, %5\n"
724 bf3e8bf1 bellard
            "    fs movl %%esi, %6\n"
725 bf3e8bf1 bellard
            "    fs movl %%edi, %7\n"
726 bf3e8bf1 bellard
            "    pushf\n"
727 bf3e8bf1 bellard
            "    popl %%eax\n"
728 bf3e8bf1 bellard
            "    movl %%eax, %%ecx\n"
729 bf3e8bf1 bellard
            "    andl $0x400, %%ecx\n"
730 bf3e8bf1 bellard
            "    shrl $9, %%ecx\n"
731 bf3e8bf1 bellard
            "    andl $0x8d5, %%eax\n"
732 bf3e8bf1 bellard
            "    fs movl %%eax, %8\n"
733 bf3e8bf1 bellard
            "    movl $1, %%eax\n"
734 bf3e8bf1 bellard
            "    subl %%ecx, %%eax\n"
735 bf3e8bf1 bellard
            "    fs movl %%eax, %11\n"
736 bf3e8bf1 bellard
            "    fs movl %9, %%ebx\n" /* get T0 value */
737 bf3e8bf1 bellard
            "    popl %%ebp\n"
738 bf3e8bf1 bellard
            :
739 bf3e8bf1 bellard
            : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
740 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
741 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
742 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
743 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
744 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
745 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
746 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
747 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
748 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
749 bf3e8bf1 bellard
            "a" (gen_func),
750 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, df)),
751 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
752 bf3e8bf1 bellard
            : "%ecx", "%edx"
753 bf3e8bf1 bellard
            );
754 bf3e8bf1 bellard
    }
755 bf3e8bf1 bellard
}
756 b8076a74 bellard
#elif defined(__ia64)
757 b8076a74 bellard
                struct fptr {
758 b8076a74 bellard
                        void *ip;
759 b8076a74 bellard
                        void *gp;
760 b8076a74 bellard
                } fp;
761 b8076a74 bellard
762 b8076a74 bellard
                fp.ip = tc_ptr;
763 b8076a74 bellard
                fp.gp = code_gen_buffer + 2 * (1 << 20);
764 b8076a74 bellard
                (*(void (*)(void)) &fp)();
765 ae228531 bellard
#else
766 3fb2ded1 bellard
                gen_func();
767 ae228531 bellard
#endif
768 83479e77 bellard
                env->current_tb = NULL;
769 4cbf74b6 bellard
                /* reset soft MMU for next block (it can currently
770 4cbf74b6 bellard
                   only be set by a memory fault) */
771 4cbf74b6 bellard
#if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
772 3f337316 bellard
                if (env->hflags & HF_SOFTMMU_MASK) {
773 3f337316 bellard
                    env->hflags &= ~HF_SOFTMMU_MASK;
774 4cbf74b6 bellard
                    /* do not allow linking to another block */
775 4cbf74b6 bellard
                    T0 = 0;
776 4cbf74b6 bellard
                }
777 4cbf74b6 bellard
#endif
778 f32fc648 bellard
#if defined(USE_KQEMU)
779 f32fc648 bellard
#define MIN_CYCLE_BEFORE_SWITCH (100 * 1000)
780 f32fc648 bellard
                if (kqemu_is_ok(env) &&
781 f32fc648 bellard
                    (cpu_get_time_fast() - env->last_io_time) >= MIN_CYCLE_BEFORE_SWITCH) {
782 f32fc648 bellard
                    cpu_loop_exit();
783 f32fc648 bellard
                }
784 f32fc648 bellard
#endif
785 3fb2ded1 bellard
            }
786 3fb2ded1 bellard
        } else {
787 0d1a29f9 bellard
            env_to_regs();
788 7d13299d bellard
        }
789 3fb2ded1 bellard
    } /* for(;;) */
790 3fb2ded1 bellard
791 7d13299d bellard
792 e4533c7a bellard
#if defined(TARGET_I386)
793 97eb5b14 bellard
#if defined(USE_CODE_COPY)
794 97eb5b14 bellard
    if (env->native_fp_regs) {
795 97eb5b14 bellard
        save_native_fp_state(env);
796 97eb5b14 bellard
    }
797 97eb5b14 bellard
#endif
798 9de5e440 bellard
    /* restore flags in standard format */
799 fc2b4c48 bellard
    env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
800 9de5e440 bellard
801 7d13299d bellard
    /* restore global registers */
802 04369ff2 bellard
#ifdef reg_EAX
803 04369ff2 bellard
    EAX = saved_EAX;
804 04369ff2 bellard
#endif
805 04369ff2 bellard
#ifdef reg_ECX
806 04369ff2 bellard
    ECX = saved_ECX;
807 04369ff2 bellard
#endif
808 04369ff2 bellard
#ifdef reg_EDX
809 04369ff2 bellard
    EDX = saved_EDX;
810 04369ff2 bellard
#endif
811 04369ff2 bellard
#ifdef reg_EBX
812 04369ff2 bellard
    EBX = saved_EBX;
813 04369ff2 bellard
#endif
814 04369ff2 bellard
#ifdef reg_ESP
815 04369ff2 bellard
    ESP = saved_ESP;
816 04369ff2 bellard
#endif
817 04369ff2 bellard
#ifdef reg_EBP
818 04369ff2 bellard
    EBP = saved_EBP;
819 04369ff2 bellard
#endif
820 04369ff2 bellard
#ifdef reg_ESI
821 04369ff2 bellard
    ESI = saved_ESI;
822 04369ff2 bellard
#endif
823 04369ff2 bellard
#ifdef reg_EDI
824 04369ff2 bellard
    EDI = saved_EDI;
825 04369ff2 bellard
#endif
826 e4533c7a bellard
#elif defined(TARGET_ARM)
827 b7bcbe95 bellard
    /* XXX: Save/restore host fpu exception state?.  */
828 93ac68bc bellard
#elif defined(TARGET_SPARC)
829 3475187d bellard
#if defined(reg_REGWPTR)
830 3475187d bellard
    REGWPTR = saved_regwptr;
831 3475187d bellard
#endif
832 67867308 bellard
#elif defined(TARGET_PPC)
833 6af0bf9c bellard
#elif defined(TARGET_MIPS)
834 fdf9b3e8 bellard
#elif defined(TARGET_SH4)
835 fdf9b3e8 bellard
    /* XXXXX */
836 e4533c7a bellard
#else
837 e4533c7a bellard
#error unsupported target CPU
838 e4533c7a bellard
#endif
839 8c6939c0 bellard
#ifdef __sparc__
840 8c6939c0 bellard
    asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
841 8c6939c0 bellard
#endif
842 7d13299d bellard
    T0 = saved_T0;
843 7d13299d bellard
    T1 = saved_T1;
844 3475187d bellard
#if defined(reg_T2)
845 e4533c7a bellard
    T2 = saved_T2;
846 3475187d bellard
#endif
847 7d13299d bellard
    env = saved_env;
848 6a00d601 bellard
    /* fail safe : never use cpu_single_env outside cpu_exec() */
849 6a00d601 bellard
    cpu_single_env = NULL; 
850 7d13299d bellard
    return ret;
851 7d13299d bellard
}
852 6dbad63e bellard
853 fbf9eeb3 bellard
/* must only be called from the generated code as an exception can be
854 fbf9eeb3 bellard
   generated */
855 fbf9eeb3 bellard
void tb_invalidate_page_range(target_ulong start, target_ulong end)
856 fbf9eeb3 bellard
{
857 dc5d0b3d bellard
    /* XXX: cannot enable it yet because it yields to MMU exception
858 dc5d0b3d bellard
       where NIP != read address on PowerPC */
859 dc5d0b3d bellard
#if 0
860 fbf9eeb3 bellard
    target_ulong phys_addr;
861 fbf9eeb3 bellard
    phys_addr = get_phys_addr_code(env, start);
862 fbf9eeb3 bellard
    tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
863 dc5d0b3d bellard
#endif
864 fbf9eeb3 bellard
}
865 fbf9eeb3 bellard
866 1a18c71b bellard
#if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
867 e4533c7a bellard
868 6dbad63e bellard
void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
869 6dbad63e bellard
{
870 6dbad63e bellard
    CPUX86State *saved_env;
871 6dbad63e bellard
872 6dbad63e bellard
    saved_env = env;
873 6dbad63e bellard
    env = s;
874 a412ac57 bellard
    if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
875 a513fe19 bellard
        selector &= 0xffff;
876 2e255c6b bellard
        cpu_x86_load_seg_cache(env, seg_reg, selector, 
877 c27004ec bellard
                               (selector << 4), 0xffff, 0);
878 a513fe19 bellard
    } else {
879 b453b70b bellard
        load_seg(seg_reg, selector);
880 a513fe19 bellard
    }
881 6dbad63e bellard
    env = saved_env;
882 6dbad63e bellard
}
883 9de5e440 bellard
884 d0a1ffc9 bellard
void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
885 d0a1ffc9 bellard
{
886 d0a1ffc9 bellard
    CPUX86State *saved_env;
887 d0a1ffc9 bellard
888 d0a1ffc9 bellard
    saved_env = env;
889 d0a1ffc9 bellard
    env = s;
890 d0a1ffc9 bellard
    
891 c27004ec bellard
    helper_fsave((target_ulong)ptr, data32);
892 d0a1ffc9 bellard
893 d0a1ffc9 bellard
    env = saved_env;
894 d0a1ffc9 bellard
}
895 d0a1ffc9 bellard
896 d0a1ffc9 bellard
void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
897 d0a1ffc9 bellard
{
898 d0a1ffc9 bellard
    CPUX86State *saved_env;
899 d0a1ffc9 bellard
900 d0a1ffc9 bellard
    saved_env = env;
901 d0a1ffc9 bellard
    env = s;
902 d0a1ffc9 bellard
    
903 c27004ec bellard
    helper_frstor((target_ulong)ptr, data32);
904 d0a1ffc9 bellard
905 d0a1ffc9 bellard
    env = saved_env;
906 d0a1ffc9 bellard
}
907 d0a1ffc9 bellard
908 e4533c7a bellard
#endif /* TARGET_I386 */
909 e4533c7a bellard
910 67b915a5 bellard
#if !defined(CONFIG_SOFTMMU)
911 67b915a5 bellard
912 3fb2ded1 bellard
#if defined(TARGET_I386)
913 3fb2ded1 bellard
914 b56dad1c bellard
/* 'pc' is the host PC at which the exception was raised. 'address' is
915 fd6ce8f6 bellard
   the effective address of the memory exception. 'is_write' is 1 if a
916 fd6ce8f6 bellard
   write caused the exception and otherwise 0'. 'old_set' is the
917 fd6ce8f6 bellard
   signal set which should be restored */
918 2b413144 bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
919 bf3e8bf1 bellard
                                    int is_write, sigset_t *old_set, 
920 bf3e8bf1 bellard
                                    void *puc)
921 9de5e440 bellard
{
922 a513fe19 bellard
    TranslationBlock *tb;
923 a513fe19 bellard
    int ret;
924 68a79315 bellard
925 83479e77 bellard
    if (cpu_single_env)
926 83479e77 bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
927 fd6ce8f6 bellard
#if defined(DEBUG_SIGNAL)
928 bf3e8bf1 bellard
    qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
929 bf3e8bf1 bellard
                pc, address, is_write, *(unsigned long *)old_set);
930 9de5e440 bellard
#endif
931 25eb4484 bellard
    /* XXX: locking issue */
932 53a5960a pbrook
    if (is_write && page_unprotect(h2g(address), pc, puc)) {
933 fd6ce8f6 bellard
        return 1;
934 fd6ce8f6 bellard
    }
935 fbf9eeb3 bellard
936 3fb2ded1 bellard
    /* see if it is an MMU fault */
937 93a40ea9 bellard
    ret = cpu_x86_handle_mmu_fault(env, address, is_write, 
938 93a40ea9 bellard
                                   ((env->hflags & HF_CPL_MASK) == 3), 0);
939 3fb2ded1 bellard
    if (ret < 0)
940 3fb2ded1 bellard
        return 0; /* not an MMU fault */
941 3fb2ded1 bellard
    if (ret == 0)
942 3fb2ded1 bellard
        return 1; /* the MMU fault was handled without causing real CPU fault */
943 3fb2ded1 bellard
    /* now we have a real cpu fault */
944 a513fe19 bellard
    tb = tb_find_pc(pc);
945 a513fe19 bellard
    if (tb) {
946 9de5e440 bellard
        /* the PC is inside the translated code. It means that we have
947 9de5e440 bellard
           a virtual CPU fault */
948 bf3e8bf1 bellard
        cpu_restore_state(tb, env, pc, puc);
949 3fb2ded1 bellard
    }
950 4cbf74b6 bellard
    if (ret == 1) {
951 3fb2ded1 bellard
#if 0
952 4cbf74b6 bellard
        printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n", 
953 4cbf74b6 bellard
               env->eip, env->cr[2], env->error_code);
954 3fb2ded1 bellard
#endif
955 4cbf74b6 bellard
        /* we restore the process signal mask as the sigreturn should
956 4cbf74b6 bellard
           do it (XXX: use sigsetjmp) */
957 4cbf74b6 bellard
        sigprocmask(SIG_SETMASK, old_set, NULL);
958 54ca9095 bellard
        raise_exception_err(env->exception_index, env->error_code);
959 4cbf74b6 bellard
    } else {
960 4cbf74b6 bellard
        /* activate soft MMU for this block */
961 3f337316 bellard
        env->hflags |= HF_SOFTMMU_MASK;
962 fbf9eeb3 bellard
        cpu_resume_from_signal(env, puc);
963 4cbf74b6 bellard
    }
964 3fb2ded1 bellard
    /* never comes here */
965 3fb2ded1 bellard
    return 1;
966 3fb2ded1 bellard
}
967 3fb2ded1 bellard
968 e4533c7a bellard
#elif defined(TARGET_ARM)
969 3fb2ded1 bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
970 bf3e8bf1 bellard
                                    int is_write, sigset_t *old_set,
971 bf3e8bf1 bellard
                                    void *puc)
972 3fb2ded1 bellard
{
973 68016c62 bellard
    TranslationBlock *tb;
974 68016c62 bellard
    int ret;
975 68016c62 bellard
976 68016c62 bellard
    if (cpu_single_env)
977 68016c62 bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
978 68016c62 bellard
#if defined(DEBUG_SIGNAL)
979 68016c62 bellard
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
980 68016c62 bellard
           pc, address, is_write, *(unsigned long *)old_set);
981 68016c62 bellard
#endif
982 9f0777ed bellard
    /* XXX: locking issue */
983 53a5960a pbrook
    if (is_write && page_unprotect(h2g(address), pc, puc)) {
984 9f0777ed bellard
        return 1;
985 9f0777ed bellard
    }
986 68016c62 bellard
    /* see if it is an MMU fault */
987 68016c62 bellard
    ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
988 68016c62 bellard
    if (ret < 0)
989 68016c62 bellard
        return 0; /* not an MMU fault */
990 68016c62 bellard
    if (ret == 0)
991 68016c62 bellard
        return 1; /* the MMU fault was handled without causing real CPU fault */
992 68016c62 bellard
    /* now we have a real cpu fault */
993 68016c62 bellard
    tb = tb_find_pc(pc);
994 68016c62 bellard
    if (tb) {
995 68016c62 bellard
        /* the PC is inside the translated code. It means that we have
996 68016c62 bellard
           a virtual CPU fault */
997 68016c62 bellard
        cpu_restore_state(tb, env, pc, puc);
998 68016c62 bellard
    }
999 68016c62 bellard
    /* we restore the process signal mask as the sigreturn should
1000 68016c62 bellard
       do it (XXX: use sigsetjmp) */
1001 68016c62 bellard
    sigprocmask(SIG_SETMASK, old_set, NULL);
1002 68016c62 bellard
    cpu_loop_exit();
1003 3fb2ded1 bellard
}
1004 93ac68bc bellard
#elif defined(TARGET_SPARC)
1005 93ac68bc bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1006 bf3e8bf1 bellard
                                    int is_write, sigset_t *old_set,
1007 bf3e8bf1 bellard
                                    void *puc)
1008 93ac68bc bellard
{
1009 68016c62 bellard
    TranslationBlock *tb;
1010 68016c62 bellard
    int ret;
1011 68016c62 bellard
1012 68016c62 bellard
    if (cpu_single_env)
1013 68016c62 bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
1014 68016c62 bellard
#if defined(DEBUG_SIGNAL)
1015 68016c62 bellard
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
1016 68016c62 bellard
           pc, address, is_write, *(unsigned long *)old_set);
1017 68016c62 bellard
#endif
1018 b453b70b bellard
    /* XXX: locking issue */
1019 53a5960a pbrook
    if (is_write && page_unprotect(h2g(address), pc, puc)) {
1020 b453b70b bellard
        return 1;
1021 b453b70b bellard
    }
1022 68016c62 bellard
    /* see if it is an MMU fault */
1023 68016c62 bellard
    ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
1024 68016c62 bellard
    if (ret < 0)
1025 68016c62 bellard
        return 0; /* not an MMU fault */
1026 68016c62 bellard
    if (ret == 0)
1027 68016c62 bellard
        return 1; /* the MMU fault was handled without causing real CPU fault */
1028 68016c62 bellard
    /* now we have a real cpu fault */
1029 68016c62 bellard
    tb = tb_find_pc(pc);
1030 68016c62 bellard
    if (tb) {
1031 68016c62 bellard
        /* the PC is inside the translated code. It means that we have
1032 68016c62 bellard
           a virtual CPU fault */
1033 68016c62 bellard
        cpu_restore_state(tb, env, pc, puc);
1034 68016c62 bellard
    }
1035 68016c62 bellard
    /* we restore the process signal mask as the sigreturn should
1036 68016c62 bellard
       do it (XXX: use sigsetjmp) */
1037 68016c62 bellard
    sigprocmask(SIG_SETMASK, old_set, NULL);
1038 68016c62 bellard
    cpu_loop_exit();
1039 93ac68bc bellard
}
1040 67867308 bellard
#elif defined (TARGET_PPC)
1041 67867308 bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1042 bf3e8bf1 bellard
                                    int is_write, sigset_t *old_set,
1043 bf3e8bf1 bellard
                                    void *puc)
1044 67867308 bellard
{
1045 67867308 bellard
    TranslationBlock *tb;
1046 ce09776b bellard
    int ret;
1047 67867308 bellard
    
1048 67867308 bellard
    if (cpu_single_env)
1049 67867308 bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
1050 67867308 bellard
#if defined(DEBUG_SIGNAL)
1051 67867308 bellard
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
1052 67867308 bellard
           pc, address, is_write, *(unsigned long *)old_set);
1053 67867308 bellard
#endif
1054 67867308 bellard
    /* XXX: locking issue */
1055 53a5960a pbrook
    if (is_write && page_unprotect(h2g(address), pc, puc)) {
1056 67867308 bellard
        return 1;
1057 67867308 bellard
    }
1058 67867308 bellard
1059 ce09776b bellard
    /* see if it is an MMU fault */
1060 7f957d28 bellard
    ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
1061 ce09776b bellard
    if (ret < 0)
1062 ce09776b bellard
        return 0; /* not an MMU fault */
1063 ce09776b bellard
    if (ret == 0)
1064 ce09776b bellard
        return 1; /* the MMU fault was handled without causing real CPU fault */
1065 ce09776b bellard
1066 67867308 bellard
    /* now we have a real cpu fault */
1067 67867308 bellard
    tb = tb_find_pc(pc);
1068 67867308 bellard
    if (tb) {
1069 67867308 bellard
        /* the PC is inside the translated code. It means that we have
1070 67867308 bellard
           a virtual CPU fault */
1071 bf3e8bf1 bellard
        cpu_restore_state(tb, env, pc, puc);
1072 67867308 bellard
    }
1073 ce09776b bellard
    if (ret == 1) {
1074 67867308 bellard
#if 0
1075 ce09776b bellard
        printf("PF exception: NIP=0x%08x error=0x%x %p\n", 
1076 ce09776b bellard
               env->nip, env->error_code, tb);
1077 67867308 bellard
#endif
1078 67867308 bellard
    /* we restore the process signal mask as the sigreturn should
1079 67867308 bellard
       do it (XXX: use sigsetjmp) */
1080 bf3e8bf1 bellard
        sigprocmask(SIG_SETMASK, old_set, NULL);
1081 9fddaa0c bellard
        do_raise_exception_err(env->exception_index, env->error_code);
1082 ce09776b bellard
    } else {
1083 ce09776b bellard
        /* activate soft MMU for this block */
1084 fbf9eeb3 bellard
        cpu_resume_from_signal(env, puc);
1085 ce09776b bellard
    }
1086 67867308 bellard
    /* never comes here */
1087 67867308 bellard
    return 1;
1088 67867308 bellard
}
1089 6af0bf9c bellard
1090 6af0bf9c bellard
#elif defined (TARGET_MIPS)
1091 6af0bf9c bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1092 6af0bf9c bellard
                                    int is_write, sigset_t *old_set,
1093 6af0bf9c bellard
                                    void *puc)
1094 6af0bf9c bellard
{
1095 6af0bf9c bellard
    TranslationBlock *tb;
1096 6af0bf9c bellard
    int ret;
1097 6af0bf9c bellard
    
1098 6af0bf9c bellard
    if (cpu_single_env)
1099 6af0bf9c bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
1100 6af0bf9c bellard
#if defined(DEBUG_SIGNAL)
1101 6af0bf9c bellard
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
1102 6af0bf9c bellard
           pc, address, is_write, *(unsigned long *)old_set);
1103 6af0bf9c bellard
#endif
1104 6af0bf9c bellard
    /* XXX: locking issue */
1105 53a5960a pbrook
    if (is_write && page_unprotect(h2g(address), pc, puc)) {
1106 6af0bf9c bellard
        return 1;
1107 6af0bf9c bellard
    }
1108 6af0bf9c bellard
1109 6af0bf9c bellard
    /* see if it is an MMU fault */
1110 cc9442b9 bellard
    ret = cpu_mips_handle_mmu_fault(env, address, is_write, 1, 0);
1111 6af0bf9c bellard
    if (ret < 0)
1112 6af0bf9c bellard
        return 0; /* not an MMU fault */
1113 6af0bf9c bellard
    if (ret == 0)
1114 6af0bf9c bellard
        return 1; /* the MMU fault was handled without causing real CPU fault */
1115 6af0bf9c bellard
1116 6af0bf9c bellard
    /* now we have a real cpu fault */
1117 6af0bf9c bellard
    tb = tb_find_pc(pc);
1118 6af0bf9c bellard
    if (tb) {
1119 6af0bf9c bellard
        /* the PC is inside the translated code. It means that we have
1120 6af0bf9c bellard
           a virtual CPU fault */
1121 6af0bf9c bellard
        cpu_restore_state(tb, env, pc, puc);
1122 6af0bf9c bellard
    }
1123 6af0bf9c bellard
    if (ret == 1) {
1124 6af0bf9c bellard
#if 0
1125 6af0bf9c bellard
        printf("PF exception: NIP=0x%08x error=0x%x %p\n", 
1126 6af0bf9c bellard
               env->nip, env->error_code, tb);
1127 6af0bf9c bellard
#endif
1128 6af0bf9c bellard
    /* we restore the process signal mask as the sigreturn should
1129 6af0bf9c bellard
       do it (XXX: use sigsetjmp) */
1130 6af0bf9c bellard
        sigprocmask(SIG_SETMASK, old_set, NULL);
1131 6af0bf9c bellard
        do_raise_exception_err(env->exception_index, env->error_code);
1132 6af0bf9c bellard
    } else {
1133 6af0bf9c bellard
        /* activate soft MMU for this block */
1134 6af0bf9c bellard
        cpu_resume_from_signal(env, puc);
1135 6af0bf9c bellard
    }
1136 6af0bf9c bellard
    /* never comes here */
1137 6af0bf9c bellard
    return 1;
1138 6af0bf9c bellard
}
1139 6af0bf9c bellard
1140 fdf9b3e8 bellard
#elif defined (TARGET_SH4)
1141 fdf9b3e8 bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1142 fdf9b3e8 bellard
                                    int is_write, sigset_t *old_set,
1143 fdf9b3e8 bellard
                                    void *puc)
1144 fdf9b3e8 bellard
{
1145 fdf9b3e8 bellard
    TranslationBlock *tb;
1146 fdf9b3e8 bellard
    int ret;
1147 fdf9b3e8 bellard
    
1148 fdf9b3e8 bellard
    if (cpu_single_env)
1149 fdf9b3e8 bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
1150 fdf9b3e8 bellard
#if defined(DEBUG_SIGNAL)
1151 fdf9b3e8 bellard
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
1152 fdf9b3e8 bellard
           pc, address, is_write, *(unsigned long *)old_set);
1153 fdf9b3e8 bellard
#endif
1154 fdf9b3e8 bellard
    /* XXX: locking issue */
1155 fdf9b3e8 bellard
    if (is_write && page_unprotect(h2g(address), pc, puc)) {
1156 fdf9b3e8 bellard
        return 1;
1157 fdf9b3e8 bellard
    }
1158 fdf9b3e8 bellard
1159 fdf9b3e8 bellard
    /* see if it is an MMU fault */
1160 fdf9b3e8 bellard
    ret = cpu_sh4_handle_mmu_fault(env, address, is_write, 1, 0);
1161 fdf9b3e8 bellard
    if (ret < 0)
1162 fdf9b3e8 bellard
        return 0; /* not an MMU fault */
1163 fdf9b3e8 bellard
    if (ret == 0)
1164 fdf9b3e8 bellard
        return 1; /* the MMU fault was handled without causing real CPU fault */
1165 fdf9b3e8 bellard
1166 fdf9b3e8 bellard
    /* now we have a real cpu fault */
1167 fdf9b3e8 bellard
    tb = tb_find_pc(pc);
1168 fdf9b3e8 bellard
    if (tb) {
1169 fdf9b3e8 bellard
        /* the PC is inside the translated code. It means that we have
1170 fdf9b3e8 bellard
           a virtual CPU fault */
1171 fdf9b3e8 bellard
        cpu_restore_state(tb, env, pc, puc);
1172 fdf9b3e8 bellard
    }
1173 fdf9b3e8 bellard
    if (ret == 1) {
1174 fdf9b3e8 bellard
#if 0
1175 fdf9b3e8 bellard
        printf("PF exception: NIP=0x%08x error=0x%x %p\n", 
1176 fdf9b3e8 bellard
               env->nip, env->error_code, tb);
1177 fdf9b3e8 bellard
#endif
1178 fdf9b3e8 bellard
    /* we restore the process signal mask as the sigreturn should
1179 fdf9b3e8 bellard
       do it (XXX: use sigsetjmp) */
1180 fdf9b3e8 bellard
        sigprocmask(SIG_SETMASK, old_set, NULL);
1181 fdf9b3e8 bellard
        //        do_raise_exception_err(env->exception_index, env->error_code);
1182 fdf9b3e8 bellard
    } else {
1183 fdf9b3e8 bellard
        /* activate soft MMU for this block */
1184 fdf9b3e8 bellard
        cpu_resume_from_signal(env, puc);
1185 fdf9b3e8 bellard
    }
1186 fdf9b3e8 bellard
    /* never comes here */
1187 fdf9b3e8 bellard
    return 1;
1188 fdf9b3e8 bellard
}
1189 e4533c7a bellard
#else
1190 e4533c7a bellard
#error unsupported target CPU
1191 e4533c7a bellard
#endif
1192 9de5e440 bellard
1193 2b413144 bellard
#if defined(__i386__)
1194 2b413144 bellard
1195 bf3e8bf1 bellard
#if defined(USE_CODE_COPY)
1196 bf3e8bf1 bellard
static void cpu_send_trap(unsigned long pc, int trap, 
1197 bf3e8bf1 bellard
                          struct ucontext *uc)
1198 bf3e8bf1 bellard
{
1199 bf3e8bf1 bellard
    TranslationBlock *tb;
1200 bf3e8bf1 bellard
1201 bf3e8bf1 bellard
    if (cpu_single_env)
1202 bf3e8bf1 bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
1203 bf3e8bf1 bellard
    /* now we have a real cpu fault */
1204 bf3e8bf1 bellard
    tb = tb_find_pc(pc);
1205 bf3e8bf1 bellard
    if (tb) {
1206 bf3e8bf1 bellard
        /* the PC is inside the translated code. It means that we have
1207 bf3e8bf1 bellard
           a virtual CPU fault */
1208 bf3e8bf1 bellard
        cpu_restore_state(tb, env, pc, uc);
1209 bf3e8bf1 bellard
    }
1210 bf3e8bf1 bellard
    sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
1211 bf3e8bf1 bellard
    raise_exception_err(trap, env->error_code);
1212 bf3e8bf1 bellard
}
1213 bf3e8bf1 bellard
#endif
1214 bf3e8bf1 bellard
1215 e4533c7a bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1216 e4533c7a bellard
                       void *puc)
1217 9de5e440 bellard
{
1218 9de5e440 bellard
    struct ucontext *uc = puc;
1219 9de5e440 bellard
    unsigned long pc;
1220 bf3e8bf1 bellard
    int trapno;
1221 97eb5b14 bellard
1222 d691f669 bellard
#ifndef REG_EIP
1223 d691f669 bellard
/* for glibc 2.1 */
1224 fd6ce8f6 bellard
#define REG_EIP    EIP
1225 fd6ce8f6 bellard
#define REG_ERR    ERR
1226 fd6ce8f6 bellard
#define REG_TRAPNO TRAPNO
1227 d691f669 bellard
#endif
1228 fc2b4c48 bellard
    pc = uc->uc_mcontext.gregs[REG_EIP];
1229 bf3e8bf1 bellard
    trapno = uc->uc_mcontext.gregs[REG_TRAPNO];
1230 bf3e8bf1 bellard
#if defined(TARGET_I386) && defined(USE_CODE_COPY)
1231 bf3e8bf1 bellard
    if (trapno == 0x00 || trapno == 0x05) {
1232 bf3e8bf1 bellard
        /* send division by zero or bound exception */
1233 bf3e8bf1 bellard
        cpu_send_trap(pc, trapno, uc);
1234 bf3e8bf1 bellard
        return 1;
1235 bf3e8bf1 bellard
    } else
1236 bf3e8bf1 bellard
#endif
1237 bf3e8bf1 bellard
        return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1238 bf3e8bf1 bellard
                                 trapno == 0xe ? 
1239 bf3e8bf1 bellard
                                 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1240 bf3e8bf1 bellard
                                 &uc->uc_sigmask, puc);
1241 2b413144 bellard
}
1242 2b413144 bellard
1243 bc51c5c9 bellard
#elif defined(__x86_64__)
1244 bc51c5c9 bellard
1245 bc51c5c9 bellard
int cpu_signal_handler(int host_signum, struct siginfo *info,
1246 bc51c5c9 bellard
                       void *puc)
1247 bc51c5c9 bellard
{
1248 bc51c5c9 bellard
    struct ucontext *uc = puc;
1249 bc51c5c9 bellard
    unsigned long pc;
1250 bc51c5c9 bellard
1251 bc51c5c9 bellard
    pc = uc->uc_mcontext.gregs[REG_RIP];
1252 bc51c5c9 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1253 bc51c5c9 bellard
                             uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ? 
1254 bc51c5c9 bellard
                             (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1255 bc51c5c9 bellard
                             &uc->uc_sigmask, puc);
1256 bc51c5c9 bellard
}
1257 bc51c5c9 bellard
1258 83fb7adf bellard
#elif defined(__powerpc__)
1259 2b413144 bellard
1260 83fb7adf bellard
/***********************************************************************
1261 83fb7adf bellard
 * signal context platform-specific definitions
1262 83fb7adf bellard
 * From Wine
1263 83fb7adf bellard
 */
1264 83fb7adf bellard
#ifdef linux
1265 83fb7adf bellard
/* All Registers access - only for local access */
1266 83fb7adf bellard
# define REG_sig(reg_name, context)                ((context)->uc_mcontext.regs->reg_name)
1267 83fb7adf bellard
/* Gpr Registers access  */
1268 83fb7adf bellard
# define GPR_sig(reg_num, context)                REG_sig(gpr[reg_num], context)
1269 83fb7adf bellard
# define IAR_sig(context)                        REG_sig(nip, context)        /* Program counter */
1270 83fb7adf bellard
# define MSR_sig(context)                        REG_sig(msr, context)   /* Machine State Register (Supervisor) */
1271 83fb7adf bellard
# define CTR_sig(context)                        REG_sig(ctr, context)   /* Count register */
1272 83fb7adf bellard
# define XER_sig(context)                        REG_sig(xer, context) /* User's integer exception register */
1273 83fb7adf bellard
# define LR_sig(context)                        REG_sig(link, context) /* Link register */
1274 83fb7adf bellard
# define CR_sig(context)                        REG_sig(ccr, context) /* Condition register */
1275 83fb7adf bellard
/* Float Registers access  */
1276 83fb7adf bellard
# define FLOAT_sig(reg_num, context)                (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1277 83fb7adf bellard
# define FPSCR_sig(context)                        (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1278 83fb7adf bellard
/* Exception Registers access */
1279 83fb7adf bellard
# define DAR_sig(context)                        REG_sig(dar, context)
1280 83fb7adf bellard
# define DSISR_sig(context)                        REG_sig(dsisr, context)
1281 83fb7adf bellard
# define TRAP_sig(context)                        REG_sig(trap, context)
1282 83fb7adf bellard
#endif /* linux */
1283 83fb7adf bellard
1284 83fb7adf bellard
#ifdef __APPLE__
1285 83fb7adf bellard
# include <sys/ucontext.h>
1286 83fb7adf bellard
typedef struct ucontext SIGCONTEXT;
1287 83fb7adf bellard
/* All Registers access - only for local access */
1288 83fb7adf bellard
# define REG_sig(reg_name, context)                ((context)->uc_mcontext->ss.reg_name)
1289 83fb7adf bellard
# define FLOATREG_sig(reg_name, context)        ((context)->uc_mcontext->fs.reg_name)
1290 83fb7adf bellard
# define EXCEPREG_sig(reg_name, context)        ((context)->uc_mcontext->es.reg_name)
1291 83fb7adf bellard
# define VECREG_sig(reg_name, context)                ((context)->uc_mcontext->vs.reg_name)
1292 83fb7adf bellard
/* Gpr Registers access */
1293 83fb7adf bellard
# define GPR_sig(reg_num, context)                REG_sig(r##reg_num, context)
1294 83fb7adf bellard
# define IAR_sig(context)                        REG_sig(srr0, context)        /* Program counter */
1295 83fb7adf bellard
# define MSR_sig(context)                        REG_sig(srr1, context)  /* Machine State Register (Supervisor) */
1296 83fb7adf bellard
# define CTR_sig(context)                        REG_sig(ctr, context)
1297 83fb7adf bellard
# define XER_sig(context)                        REG_sig(xer, context) /* Link register */
1298 83fb7adf bellard
# define LR_sig(context)                        REG_sig(lr, context)  /* User's integer exception register */
1299 83fb7adf bellard
# define CR_sig(context)                        REG_sig(cr, context)  /* Condition register */
1300 83fb7adf bellard
/* Float Registers access */
1301 83fb7adf bellard
# define FLOAT_sig(reg_num, context)                FLOATREG_sig(fpregs[reg_num], context)
1302 83fb7adf bellard
# define FPSCR_sig(context)                        ((double)FLOATREG_sig(fpscr, context))
1303 83fb7adf bellard
/* Exception Registers access */
1304 83fb7adf bellard
# define DAR_sig(context)                        EXCEPREG_sig(dar, context)     /* Fault registers for coredump */
1305 83fb7adf bellard
# define DSISR_sig(context)                        EXCEPREG_sig(dsisr, context)
1306 83fb7adf bellard
# define TRAP_sig(context)                        EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1307 83fb7adf bellard
#endif /* __APPLE__ */
1308 83fb7adf bellard
1309 d1d9f421 bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1310 e4533c7a bellard
                       void *puc)
1311 2b413144 bellard
{
1312 25eb4484 bellard
    struct ucontext *uc = puc;
1313 25eb4484 bellard
    unsigned long pc;
1314 25eb4484 bellard
    int is_write;
1315 25eb4484 bellard
1316 83fb7adf bellard
    pc = IAR_sig(uc);
1317 25eb4484 bellard
    is_write = 0;
1318 25eb4484 bellard
#if 0
1319 25eb4484 bellard
    /* ppc 4xx case */
1320 83fb7adf bellard
    if (DSISR_sig(uc) & 0x00800000)
1321 25eb4484 bellard
        is_write = 1;
1322 25eb4484 bellard
#else
1323 83fb7adf bellard
    if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
1324 25eb4484 bellard
        is_write = 1;
1325 25eb4484 bellard
#endif
1326 25eb4484 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1327 bf3e8bf1 bellard
                             is_write, &uc->uc_sigmask, puc);
1328 2b413144 bellard
}
1329 2b413144 bellard
1330 2f87c607 bellard
#elif defined(__alpha__)
1331 2f87c607 bellard
1332 e4533c7a bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1333 2f87c607 bellard
                           void *puc)
1334 2f87c607 bellard
{
1335 2f87c607 bellard
    struct ucontext *uc = puc;
1336 2f87c607 bellard
    uint32_t *pc = uc->uc_mcontext.sc_pc;
1337 2f87c607 bellard
    uint32_t insn = *pc;
1338 2f87c607 bellard
    int is_write = 0;
1339 2f87c607 bellard
1340 8c6939c0 bellard
    /* XXX: need kernel patch to get write flag faster */
1341 2f87c607 bellard
    switch (insn >> 26) {
1342 2f87c607 bellard
    case 0x0d: // stw
1343 2f87c607 bellard
    case 0x0e: // stb
1344 2f87c607 bellard
    case 0x0f: // stq_u
1345 2f87c607 bellard
    case 0x24: // stf
1346 2f87c607 bellard
    case 0x25: // stg
1347 2f87c607 bellard
    case 0x26: // sts
1348 2f87c607 bellard
    case 0x27: // stt
1349 2f87c607 bellard
    case 0x2c: // stl
1350 2f87c607 bellard
    case 0x2d: // stq
1351 2f87c607 bellard
    case 0x2e: // stl_c
1352 2f87c607 bellard
    case 0x2f: // stq_c
1353 2f87c607 bellard
        is_write = 1;
1354 2f87c607 bellard
    }
1355 2f87c607 bellard
1356 2f87c607 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1357 bf3e8bf1 bellard
                             is_write, &uc->uc_sigmask, puc);
1358 2f87c607 bellard
}
1359 8c6939c0 bellard
#elif defined(__sparc__)
1360 8c6939c0 bellard
1361 e4533c7a bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1362 e4533c7a bellard
                       void *puc)
1363 8c6939c0 bellard
{
1364 8c6939c0 bellard
    uint32_t *regs = (uint32_t *)(info + 1);
1365 8c6939c0 bellard
    void *sigmask = (regs + 20);
1366 8c6939c0 bellard
    unsigned long pc;
1367 8c6939c0 bellard
    int is_write;
1368 8c6939c0 bellard
    uint32_t insn;
1369 8c6939c0 bellard
    
1370 8c6939c0 bellard
    /* XXX: is there a standard glibc define ? */
1371 8c6939c0 bellard
    pc = regs[1];
1372 8c6939c0 bellard
    /* XXX: need kernel patch to get write flag faster */
1373 8c6939c0 bellard
    is_write = 0;
1374 8c6939c0 bellard
    insn = *(uint32_t *)pc;
1375 8c6939c0 bellard
    if ((insn >> 30) == 3) {
1376 8c6939c0 bellard
      switch((insn >> 19) & 0x3f) {
1377 8c6939c0 bellard
      case 0x05: // stb
1378 8c6939c0 bellard
      case 0x06: // sth
1379 8c6939c0 bellard
      case 0x04: // st
1380 8c6939c0 bellard
      case 0x07: // std
1381 8c6939c0 bellard
      case 0x24: // stf
1382 8c6939c0 bellard
      case 0x27: // stdf
1383 8c6939c0 bellard
      case 0x25: // stfsr
1384 8c6939c0 bellard
        is_write = 1;
1385 8c6939c0 bellard
        break;
1386 8c6939c0 bellard
      }
1387 8c6939c0 bellard
    }
1388 8c6939c0 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1389 bf3e8bf1 bellard
                             is_write, sigmask, NULL);
1390 8c6939c0 bellard
}
1391 8c6939c0 bellard
1392 8c6939c0 bellard
#elif defined(__arm__)
1393 8c6939c0 bellard
1394 e4533c7a bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1395 e4533c7a bellard
                       void *puc)
1396 8c6939c0 bellard
{
1397 8c6939c0 bellard
    struct ucontext *uc = puc;
1398 8c6939c0 bellard
    unsigned long pc;
1399 8c6939c0 bellard
    int is_write;
1400 8c6939c0 bellard
    
1401 8c6939c0 bellard
    pc = uc->uc_mcontext.gregs[R15];
1402 8c6939c0 bellard
    /* XXX: compute is_write */
1403 8c6939c0 bellard
    is_write = 0;
1404 8c6939c0 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1405 8c6939c0 bellard
                             is_write,
1406 8c6939c0 bellard
                             &uc->uc_sigmask);
1407 8c6939c0 bellard
}
1408 8c6939c0 bellard
1409 38e584a0 bellard
#elif defined(__mc68000)
1410 38e584a0 bellard
1411 38e584a0 bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1412 38e584a0 bellard
                       void *puc)
1413 38e584a0 bellard
{
1414 38e584a0 bellard
    struct ucontext *uc = puc;
1415 38e584a0 bellard
    unsigned long pc;
1416 38e584a0 bellard
    int is_write;
1417 38e584a0 bellard
    
1418 38e584a0 bellard
    pc = uc->uc_mcontext.gregs[16];
1419 38e584a0 bellard
    /* XXX: compute is_write */
1420 38e584a0 bellard
    is_write = 0;
1421 38e584a0 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1422 38e584a0 bellard
                             is_write,
1423 bf3e8bf1 bellard
                             &uc->uc_sigmask, puc);
1424 38e584a0 bellard
}
1425 38e584a0 bellard
1426 b8076a74 bellard
#elif defined(__ia64)
1427 b8076a74 bellard
1428 b8076a74 bellard
#ifndef __ISR_VALID
1429 b8076a74 bellard
  /* This ought to be in <bits/siginfo.h>... */
1430 b8076a74 bellard
# define __ISR_VALID        1
1431 b8076a74 bellard
#endif
1432 b8076a74 bellard
1433 b8076a74 bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, void *puc)
1434 b8076a74 bellard
{
1435 b8076a74 bellard
    struct ucontext *uc = puc;
1436 b8076a74 bellard
    unsigned long ip;
1437 b8076a74 bellard
    int is_write = 0;
1438 b8076a74 bellard
1439 b8076a74 bellard
    ip = uc->uc_mcontext.sc_ip;
1440 b8076a74 bellard
    switch (host_signum) {
1441 b8076a74 bellard
      case SIGILL:
1442 b8076a74 bellard
      case SIGFPE:
1443 b8076a74 bellard
      case SIGSEGV:
1444 b8076a74 bellard
      case SIGBUS:
1445 b8076a74 bellard
      case SIGTRAP:
1446 fd4a43e4 bellard
          if (info->si_code && (info->si_segvflags & __ISR_VALID))
1447 b8076a74 bellard
              /* ISR.W (write-access) is bit 33:  */
1448 b8076a74 bellard
              is_write = (info->si_isr >> 33) & 1;
1449 b8076a74 bellard
          break;
1450 b8076a74 bellard
1451 b8076a74 bellard
      default:
1452 b8076a74 bellard
          break;
1453 b8076a74 bellard
    }
1454 b8076a74 bellard
    return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1455 b8076a74 bellard
                             is_write,
1456 b8076a74 bellard
                             &uc->uc_sigmask, puc);
1457 b8076a74 bellard
}
1458 b8076a74 bellard
1459 90cb9493 bellard
#elif defined(__s390__)
1460 90cb9493 bellard
1461 90cb9493 bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
1462 90cb9493 bellard
                       void *puc)
1463 90cb9493 bellard
{
1464 90cb9493 bellard
    struct ucontext *uc = puc;
1465 90cb9493 bellard
    unsigned long pc;
1466 90cb9493 bellard
    int is_write;
1467 90cb9493 bellard
    
1468 90cb9493 bellard
    pc = uc->uc_mcontext.psw.addr;
1469 90cb9493 bellard
    /* XXX: compute is_write */
1470 90cb9493 bellard
    is_write = 0;
1471 90cb9493 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
1472 90cb9493 bellard
                             is_write,
1473 90cb9493 bellard
                             &uc->uc_sigmask, puc);
1474 90cb9493 bellard
}
1475 90cb9493 bellard
1476 9de5e440 bellard
#else
1477 2b413144 bellard
1478 3fb2ded1 bellard
#error host CPU specific signal handler needed
1479 2b413144 bellard
1480 9de5e440 bellard
#endif
1481 67b915a5 bellard
1482 67b915a5 bellard
#endif /* !defined(CONFIG_SOFTMMU) */