Statistics
| Branch: | Revision:

root / cpu-exec.c @ 9fddaa0c

History | View | Annotate | Download (30.4 kB)

1 7d13299d bellard
/*
2 7d13299d bellard
 *  i386 emulator main execution loop
3 7d13299d bellard
 * 
4 7d13299d bellard
 *  Copyright (c) 2003 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 e4533c7a bellard
51 fbf9eeb3 bellard
/* exit the current TB from a signal handler. The host registers are
52 fbf9eeb3 bellard
   restored in a state compatible with the CPU emulator
53 fbf9eeb3 bellard
 */
54 fbf9eeb3 bellard
void cpu_resume_from_signal(CPUState *env1, void *puc) 
55 fbf9eeb3 bellard
{
56 fbf9eeb3 bellard
#if !defined(CONFIG_SOFTMMU)
57 fbf9eeb3 bellard
    struct ucontext *uc = puc;
58 fbf9eeb3 bellard
#endif
59 fbf9eeb3 bellard
60 fbf9eeb3 bellard
    env = env1;
61 fbf9eeb3 bellard
62 fbf9eeb3 bellard
    /* XXX: restore cpu registers saved in host registers */
63 fbf9eeb3 bellard
64 fbf9eeb3 bellard
#if !defined(CONFIG_SOFTMMU)
65 fbf9eeb3 bellard
    if (puc) {
66 fbf9eeb3 bellard
        /* XXX: use siglongjmp ? */
67 fbf9eeb3 bellard
        sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
68 fbf9eeb3 bellard
    }
69 fbf9eeb3 bellard
#endif
70 fbf9eeb3 bellard
    longjmp(env->jmp_env, 1);
71 fbf9eeb3 bellard
}
72 fbf9eeb3 bellard
73 7d13299d bellard
/* main execution loop */
74 7d13299d bellard
75 e4533c7a bellard
int cpu_exec(CPUState *env1)
76 7d13299d bellard
{
77 e4533c7a bellard
    int saved_T0, saved_T1, saved_T2;
78 e4533c7a bellard
    CPUState *saved_env;
79 04369ff2 bellard
#ifdef reg_EAX
80 04369ff2 bellard
    int saved_EAX;
81 04369ff2 bellard
#endif
82 04369ff2 bellard
#ifdef reg_ECX
83 04369ff2 bellard
    int saved_ECX;
84 04369ff2 bellard
#endif
85 04369ff2 bellard
#ifdef reg_EDX
86 04369ff2 bellard
    int saved_EDX;
87 04369ff2 bellard
#endif
88 04369ff2 bellard
#ifdef reg_EBX
89 04369ff2 bellard
    int saved_EBX;
90 04369ff2 bellard
#endif
91 04369ff2 bellard
#ifdef reg_ESP
92 04369ff2 bellard
    int saved_ESP;
93 04369ff2 bellard
#endif
94 04369ff2 bellard
#ifdef reg_EBP
95 04369ff2 bellard
    int saved_EBP;
96 04369ff2 bellard
#endif
97 04369ff2 bellard
#ifdef reg_ESI
98 04369ff2 bellard
    int saved_ESI;
99 04369ff2 bellard
#endif
100 04369ff2 bellard
#ifdef reg_EDI
101 04369ff2 bellard
    int saved_EDI;
102 04369ff2 bellard
#endif
103 8c6939c0 bellard
#ifdef __sparc__
104 8c6939c0 bellard
    int saved_i7, tmp_T0;
105 8c6939c0 bellard
#endif
106 68a79315 bellard
    int code_gen_size, ret, interrupt_request;
107 7d13299d bellard
    void (*gen_func)(void);
108 9de5e440 bellard
    TranslationBlock *tb, **ptb;
109 dab2ed99 bellard
    uint8_t *tc_ptr, *cs_base, *pc;
110 6dbad63e bellard
    unsigned int flags;
111 8c6939c0 bellard
112 7d13299d bellard
    /* first we save global registers */
113 7d13299d bellard
    saved_T0 = T0;
114 7d13299d bellard
    saved_T1 = T1;
115 e4533c7a bellard
    saved_T2 = T2;
116 7d13299d bellard
    saved_env = env;
117 7d13299d bellard
    env = env1;
118 e4533c7a bellard
#ifdef __sparc__
119 e4533c7a bellard
    /* we also save i7 because longjmp may not restore it */
120 e4533c7a bellard
    asm volatile ("mov %%i7, %0" : "=r" (saved_i7));
121 e4533c7a bellard
#endif
122 e4533c7a bellard
123 e4533c7a bellard
#if defined(TARGET_I386)
124 04369ff2 bellard
#ifdef reg_EAX
125 04369ff2 bellard
    saved_EAX = EAX;
126 04369ff2 bellard
    EAX = env->regs[R_EAX];
127 04369ff2 bellard
#endif
128 04369ff2 bellard
#ifdef reg_ECX
129 04369ff2 bellard
    saved_ECX = ECX;
130 04369ff2 bellard
    ECX = env->regs[R_ECX];
131 04369ff2 bellard
#endif
132 04369ff2 bellard
#ifdef reg_EDX
133 04369ff2 bellard
    saved_EDX = EDX;
134 04369ff2 bellard
    EDX = env->regs[R_EDX];
135 04369ff2 bellard
#endif
136 04369ff2 bellard
#ifdef reg_EBX
137 04369ff2 bellard
    saved_EBX = EBX;
138 04369ff2 bellard
    EBX = env->regs[R_EBX];
139 04369ff2 bellard
#endif
140 04369ff2 bellard
#ifdef reg_ESP
141 04369ff2 bellard
    saved_ESP = ESP;
142 04369ff2 bellard
    ESP = env->regs[R_ESP];
143 04369ff2 bellard
#endif
144 04369ff2 bellard
#ifdef reg_EBP
145 04369ff2 bellard
    saved_EBP = EBP;
146 04369ff2 bellard
    EBP = env->regs[R_EBP];
147 04369ff2 bellard
#endif
148 04369ff2 bellard
#ifdef reg_ESI
149 04369ff2 bellard
    saved_ESI = ESI;
150 04369ff2 bellard
    ESI = env->regs[R_ESI];
151 04369ff2 bellard
#endif
152 04369ff2 bellard
#ifdef reg_EDI
153 04369ff2 bellard
    saved_EDI = EDI;
154 04369ff2 bellard
    EDI = env->regs[R_EDI];
155 04369ff2 bellard
#endif
156 7d13299d bellard
    
157 9de5e440 bellard
    /* put eflags in CPU temporary format */
158 fc2b4c48 bellard
    CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
159 fc2b4c48 bellard
    DF = 1 - (2 * ((env->eflags >> 10) & 1));
160 9de5e440 bellard
    CC_OP = CC_OP_EFLAGS;
161 fc2b4c48 bellard
    env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
162 e4533c7a bellard
#elif defined(TARGET_ARM)
163 e4533c7a bellard
    {
164 e4533c7a bellard
        unsigned int psr;
165 e4533c7a bellard
        psr = env->cpsr;
166 e4533c7a bellard
        env->CF = (psr >> 29) & 1;
167 e4533c7a bellard
        env->NZF = (psr & 0xc0000000) ^ 0x40000000;
168 e4533c7a bellard
        env->VF = (psr << 3) & 0x80000000;
169 e4533c7a bellard
        env->cpsr = psr & ~0xf0000000;
170 e4533c7a bellard
    }
171 93ac68bc bellard
#elif defined(TARGET_SPARC)
172 67867308 bellard
#elif defined(TARGET_PPC)
173 e4533c7a bellard
#else
174 e4533c7a bellard
#error unsupported target CPU
175 e4533c7a bellard
#endif
176 3fb2ded1 bellard
    env->exception_index = -1;
177 9d27abd9 bellard
178 7d13299d bellard
    /* prepare setjmp context for exception handling */
179 3fb2ded1 bellard
    for(;;) {
180 3fb2ded1 bellard
        if (setjmp(env->jmp_env) == 0) {
181 ee8b7021 bellard
            env->current_tb = NULL;
182 3fb2ded1 bellard
            /* if an exception is pending, we execute it here */
183 3fb2ded1 bellard
            if (env->exception_index >= 0) {
184 3fb2ded1 bellard
                if (env->exception_index >= EXCP_INTERRUPT) {
185 3fb2ded1 bellard
                    /* exit request from the cpu execution loop */
186 3fb2ded1 bellard
                    ret = env->exception_index;
187 3fb2ded1 bellard
                    break;
188 3fb2ded1 bellard
                } else if (env->user_mode_only) {
189 3fb2ded1 bellard
                    /* if user mode only, we simulate a fake exception
190 3fb2ded1 bellard
                       which will be hanlded outside the cpu execution
191 3fb2ded1 bellard
                       loop */
192 83479e77 bellard
#if defined(TARGET_I386)
193 3fb2ded1 bellard
                    do_interrupt_user(env->exception_index, 
194 3fb2ded1 bellard
                                      env->exception_is_int, 
195 3fb2ded1 bellard
                                      env->error_code, 
196 3fb2ded1 bellard
                                      env->exception_next_eip);
197 83479e77 bellard
#endif
198 3fb2ded1 bellard
                    ret = env->exception_index;
199 3fb2ded1 bellard
                    break;
200 3fb2ded1 bellard
                } else {
201 83479e77 bellard
#if defined(TARGET_I386)
202 3fb2ded1 bellard
                    /* simulate a real cpu exception. On i386, it can
203 3fb2ded1 bellard
                       trigger new exceptions, but we do not handle
204 3fb2ded1 bellard
                       double or triple faults yet. */
205 3fb2ded1 bellard
                    do_interrupt(env->exception_index, 
206 3fb2ded1 bellard
                                 env->exception_is_int, 
207 3fb2ded1 bellard
                                 env->error_code, 
208 d05e66d2 bellard
                                 env->exception_next_eip, 0);
209 ce09776b bellard
#elif defined(TARGET_PPC)
210 ce09776b bellard
                    do_interrupt(env);
211 83479e77 bellard
#endif
212 3fb2ded1 bellard
                }
213 3fb2ded1 bellard
                env->exception_index = -1;
214 3fb2ded1 bellard
            }
215 3fb2ded1 bellard
            T0 = 0; /* force lookup of first TB */
216 3fb2ded1 bellard
            for(;;) {
217 8c6939c0 bellard
#ifdef __sparc__
218 3fb2ded1 bellard
                /* g1 can be modified by some libc? functions */ 
219 3fb2ded1 bellard
                tmp_T0 = T0;
220 8c6939c0 bellard
#endif            
221 68a79315 bellard
                interrupt_request = env->interrupt_request;
222 2e255c6b bellard
                if (__builtin_expect(interrupt_request, 0)) {
223 68a79315 bellard
#if defined(TARGET_I386)
224 68a79315 bellard
                    /* if hardware interrupt pending, we execute it */
225 68a79315 bellard
                    if ((interrupt_request & CPU_INTERRUPT_HARD) &&
226 3f337316 bellard
                        (env->eflags & IF_MASK) && 
227 3f337316 bellard
                        !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
228 68a79315 bellard
                        int intno;
229 fbf9eeb3 bellard
                        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
230 a541f297 bellard
                        intno = cpu_get_pic_interrupt(env);
231 f193c797 bellard
                        if (loglevel & CPU_LOG_TB_IN_ASM) {
232 68a79315 bellard
                            fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
233 68a79315 bellard
                        }
234 d05e66d2 bellard
                        do_interrupt(intno, 0, 0, 0, 1);
235 907a5b26 bellard
                        /* ensure that no TB jump will be modified as
236 907a5b26 bellard
                           the program flow was changed */
237 907a5b26 bellard
#ifdef __sparc__
238 907a5b26 bellard
                        tmp_T0 = 0;
239 907a5b26 bellard
#else
240 907a5b26 bellard
                        T0 = 0;
241 907a5b26 bellard
#endif
242 68a79315 bellard
                    }
243 ce09776b bellard
#elif defined(TARGET_PPC)
244 9fddaa0c bellard
#if 0
245 9fddaa0c bellard
                    if ((interrupt_request & CPU_INTERRUPT_RESET)) {
246 9fddaa0c bellard
                        cpu_ppc_reset(env);
247 9fddaa0c bellard
                    }
248 9fddaa0c bellard
#endif
249 9fddaa0c bellard
                    if (msr_ee != 0) {
250 ce09776b bellard
                    if ((interrupt_request & CPU_INTERRUPT_HARD)) {
251 9fddaa0c bellard
                            /* Raise it */
252 9fddaa0c bellard
                            env->exception_index = EXCP_EXTERNAL;
253 9fddaa0c bellard
                            env->error_code = 0;
254 ce09776b bellard
                            do_interrupt(env);
255 ce09776b bellard
                        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
256 9fddaa0c bellard
                        } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) {
257 9fddaa0c bellard
                            /* Raise it */
258 9fddaa0c bellard
                            env->exception_index = EXCP_DECR;
259 9fddaa0c bellard
                            env->error_code = 0;
260 9fddaa0c bellard
                            do_interrupt(env);
261 9fddaa0c bellard
                            env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
262 9fddaa0c bellard
                        }
263 ce09776b bellard
                    }
264 68a79315 bellard
#endif
265 bf3e8bf1 bellard
                    if (interrupt_request & CPU_INTERRUPT_EXITTB) {
266 bf3e8bf1 bellard
                        env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
267 bf3e8bf1 bellard
                        /* ensure that no TB jump will be modified as
268 bf3e8bf1 bellard
                           the program flow was changed */
269 bf3e8bf1 bellard
#ifdef __sparc__
270 bf3e8bf1 bellard
                        tmp_T0 = 0;
271 bf3e8bf1 bellard
#else
272 bf3e8bf1 bellard
                        T0 = 0;
273 bf3e8bf1 bellard
#endif
274 bf3e8bf1 bellard
                    }
275 68a79315 bellard
                    if (interrupt_request & CPU_INTERRUPT_EXIT) {
276 68a79315 bellard
                        env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
277 68a79315 bellard
                        env->exception_index = EXCP_INTERRUPT;
278 68a79315 bellard
                        cpu_loop_exit();
279 68a79315 bellard
                    }
280 3fb2ded1 bellard
                }
281 7d13299d bellard
#ifdef DEBUG_EXEC
282 f193c797 bellard
                if (loglevel & CPU_LOG_EXEC) {
283 e4533c7a bellard
#if defined(TARGET_I386)
284 3fb2ded1 bellard
                    /* restore flags in standard format */
285 3fb2ded1 bellard
                    env->regs[R_EAX] = EAX;
286 3fb2ded1 bellard
                    env->regs[R_EBX] = EBX;
287 3fb2ded1 bellard
                    env->regs[R_ECX] = ECX;
288 3fb2ded1 bellard
                    env->regs[R_EDX] = EDX;
289 3fb2ded1 bellard
                    env->regs[R_ESI] = ESI;
290 3fb2ded1 bellard
                    env->regs[R_EDI] = EDI;
291 3fb2ded1 bellard
                    env->regs[R_EBP] = EBP;
292 3fb2ded1 bellard
                    env->regs[R_ESP] = ESP;
293 3fb2ded1 bellard
                    env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
294 68a79315 bellard
                    cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
295 3fb2ded1 bellard
                    env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
296 e4533c7a bellard
#elif defined(TARGET_ARM)
297 1b21b62a bellard
                    env->cpsr = compute_cpsr();
298 3fb2ded1 bellard
                    cpu_arm_dump_state(env, logfile, 0);
299 1b21b62a bellard
                    env->cpsr &= ~0xf0000000;
300 93ac68bc bellard
#elif defined(TARGET_SPARC)
301 93a40ea9 bellard
                    cpu_sparc_dump_state (env, logfile, 0);
302 67867308 bellard
#elif defined(TARGET_PPC)
303 67867308 bellard
                    cpu_ppc_dump_state(env, logfile, 0);
304 e4533c7a bellard
#else
305 e4533c7a bellard
#error unsupported target CPU 
306 e4533c7a bellard
#endif
307 3fb2ded1 bellard
                }
308 7d13299d bellard
#endif
309 3f337316 bellard
                /* we record a subset of the CPU state. It will
310 3f337316 bellard
                   always be the same before a given translated block
311 3f337316 bellard
                   is executed. */
312 e4533c7a bellard
#if defined(TARGET_I386)
313 2e255c6b bellard
                flags = env->hflags;
314 3f337316 bellard
                flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
315 3fb2ded1 bellard
                cs_base = env->segs[R_CS].base;
316 3fb2ded1 bellard
                pc = cs_base + env->eip;
317 e4533c7a bellard
#elif defined(TARGET_ARM)
318 3fb2ded1 bellard
                flags = 0;
319 3fb2ded1 bellard
                cs_base = 0;
320 3fb2ded1 bellard
                pc = (uint8_t *)env->regs[15];
321 93ac68bc bellard
#elif defined(TARGET_SPARC)
322 67867308 bellard
                flags = 0;
323 ce09776b bellard
                cs_base = (uint8_t *)env->npc;
324 67867308 bellard
                pc = (uint8_t *) env->pc;
325 67867308 bellard
#elif defined(TARGET_PPC)
326 67867308 bellard
                flags = 0;
327 67867308 bellard
                cs_base = 0;
328 67867308 bellard
                pc = (uint8_t *)env->nip;
329 e4533c7a bellard
#else
330 e4533c7a bellard
#error unsupported CPU
331 e4533c7a bellard
#endif
332 3fb2ded1 bellard
                tb = tb_find(&ptb, (unsigned long)pc, (unsigned long)cs_base, 
333 3fb2ded1 bellard
                             flags);
334 d4e8164f bellard
                if (!tb) {
335 1376847f bellard
                    TranslationBlock **ptb1;
336 1376847f bellard
                    unsigned int h;
337 1376847f bellard
                    target_ulong phys_pc, phys_page1, phys_page2, virt_page2;
338 1376847f bellard
                    
339 1376847f bellard
                    
340 3fb2ded1 bellard
                    spin_lock(&tb_lock);
341 1376847f bellard
342 1376847f bellard
                    tb_invalidated_flag = 0;
343 1376847f bellard
344 1376847f bellard
                    /* find translated block using physical mappings */
345 1376847f bellard
                    phys_pc = get_phys_addr_code(env, (unsigned long)pc);
346 1376847f bellard
                    phys_page1 = phys_pc & TARGET_PAGE_MASK;
347 1376847f bellard
                    phys_page2 = -1;
348 1376847f bellard
                    h = tb_phys_hash_func(phys_pc);
349 1376847f bellard
                    ptb1 = &tb_phys_hash[h];
350 1376847f bellard
                    for(;;) {
351 1376847f bellard
                        tb = *ptb1;
352 1376847f bellard
                        if (!tb)
353 1376847f bellard
                            goto not_found;
354 1376847f bellard
                        if (tb->pc == (unsigned long)pc && 
355 1376847f bellard
                            tb->page_addr[0] == phys_page1 &&
356 1376847f bellard
                            tb->cs_base == (unsigned long)cs_base && 
357 1376847f bellard
                            tb->flags == flags) {
358 1376847f bellard
                            /* check next page if needed */
359 b516f85c bellard
                            if (tb->page_addr[1] != -1) {
360 b516f85c bellard
                                virt_page2 = ((unsigned long)pc & TARGET_PAGE_MASK) + 
361 b516f85c bellard
                                    TARGET_PAGE_SIZE;
362 1376847f bellard
                                phys_page2 = get_phys_addr_code(env, virt_page2);
363 1376847f bellard
                                if (tb->page_addr[1] == phys_page2)
364 1376847f bellard
                                    goto found;
365 1376847f bellard
                            } else {
366 1376847f bellard
                                goto found;
367 1376847f bellard
                            }
368 1376847f bellard
                        }
369 1376847f bellard
                        ptb1 = &tb->phys_hash_next;
370 1376847f bellard
                    }
371 1376847f bellard
                not_found:
372 3fb2ded1 bellard
                    /* if no translated code available, then translate it now */
373 d4e8164f bellard
                    tb = tb_alloc((unsigned long)pc);
374 3fb2ded1 bellard
                    if (!tb) {
375 3fb2ded1 bellard
                        /* flush must be done */
376 b453b70b bellard
                        tb_flush(env);
377 3fb2ded1 bellard
                        /* cannot fail at this point */
378 3fb2ded1 bellard
                        tb = tb_alloc((unsigned long)pc);
379 3fb2ded1 bellard
                        /* don't forget to invalidate previous TB info */
380 3fb2ded1 bellard
                        ptb = &tb_hash[tb_hash_func((unsigned long)pc)];
381 3fb2ded1 bellard
                        T0 = 0;
382 3fb2ded1 bellard
                    }
383 3fb2ded1 bellard
                    tc_ptr = code_gen_ptr;
384 3fb2ded1 bellard
                    tb->tc_ptr = tc_ptr;
385 3fb2ded1 bellard
                    tb->cs_base = (unsigned long)cs_base;
386 3fb2ded1 bellard
                    tb->flags = flags;
387 facc68be bellard
                    cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
388 1376847f bellard
                    code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
389 1376847f bellard
                    
390 1376847f bellard
                    /* check next page if needed */
391 1376847f bellard
                    virt_page2 = ((unsigned long)pc + tb->size - 1) & TARGET_PAGE_MASK;
392 1376847f bellard
                    phys_page2 = -1;
393 1376847f bellard
                    if (((unsigned long)pc & TARGET_PAGE_MASK) != virt_page2) {
394 1376847f bellard
                        phys_page2 = get_phys_addr_code(env, virt_page2);
395 1376847f bellard
                    }
396 1376847f bellard
                    tb_link_phys(tb, phys_pc, phys_page2);
397 1376847f bellard
398 1376847f bellard
                found:
399 36bdbe54 bellard
                    if (tb_invalidated_flag) {
400 36bdbe54 bellard
                        /* as some TB could have been invalidated because
401 36bdbe54 bellard
                           of memory exceptions while generating the code, we
402 36bdbe54 bellard
                           must recompute the hash index here */
403 36bdbe54 bellard
                        ptb = &tb_hash[tb_hash_func((unsigned long)pc)];
404 36bdbe54 bellard
                        while (*ptb != NULL)
405 36bdbe54 bellard
                            ptb = &(*ptb)->hash_next;
406 36bdbe54 bellard
                        T0 = 0;
407 36bdbe54 bellard
                    }
408 1376847f bellard
                    /* we add the TB in the virtual pc hash table */
409 3fb2ded1 bellard
                    *ptb = tb;
410 3fb2ded1 bellard
                    tb->hash_next = NULL;
411 3fb2ded1 bellard
                    tb_link(tb);
412 25eb4484 bellard
                    spin_unlock(&tb_lock);
413 9de5e440 bellard
                }
414 9d27abd9 bellard
#ifdef DEBUG_EXEC
415 f193c797 bellard
                if (loglevel & CPU_LOG_EXEC) {
416 3fb2ded1 bellard
                    fprintf(logfile, "Trace 0x%08lx [0x%08lx] %s\n",
417 3fb2ded1 bellard
                            (long)tb->tc_ptr, (long)tb->pc,
418 3fb2ded1 bellard
                            lookup_symbol((void *)tb->pc));
419 3fb2ded1 bellard
                }
420 9d27abd9 bellard
#endif
421 8c6939c0 bellard
#ifdef __sparc__
422 3fb2ded1 bellard
                T0 = tmp_T0;
423 8c6939c0 bellard
#endif            
424 facc68be bellard
                /* see if we can patch the calling TB. */
425 bf3e8bf1 bellard
                if (T0 != 0
426 bf3e8bf1 bellard
#if defined(TARGET_I386) && defined(USE_CODE_COPY)
427 bf3e8bf1 bellard
                    && (tb->cflags & CF_CODE_COPY) == 
428 bf3e8bf1 bellard
                    (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
429 bf3e8bf1 bellard
#endif
430 bf3e8bf1 bellard
                    ) {
431 3fb2ded1 bellard
                    spin_lock(&tb_lock);
432 3fb2ded1 bellard
                    tb_add_jump((TranslationBlock *)(T0 & ~3), T0 & 3, tb);
433 97eb5b14 bellard
#if defined(USE_CODE_COPY)
434 97eb5b14 bellard
                    /* propagates the FP use info */
435 97eb5b14 bellard
                    ((TranslationBlock *)(T0 & ~3))->cflags |= 
436 97eb5b14 bellard
                        (tb->cflags & CF_FP_USED);
437 97eb5b14 bellard
#endif
438 3fb2ded1 bellard
                    spin_unlock(&tb_lock);
439 3fb2ded1 bellard
                }
440 3fb2ded1 bellard
                tc_ptr = tb->tc_ptr;
441 83479e77 bellard
                env->current_tb = tb;
442 3fb2ded1 bellard
                /* execute the generated code */
443 3fb2ded1 bellard
                gen_func = (void *)tc_ptr;
444 8c6939c0 bellard
#if defined(__sparc__)
445 3fb2ded1 bellard
                __asm__ __volatile__("call        %0\n\t"
446 3fb2ded1 bellard
                                     "mov        %%o7,%%i0"
447 3fb2ded1 bellard
                                     : /* no outputs */
448 3fb2ded1 bellard
                                     : "r" (gen_func) 
449 3fb2ded1 bellard
                                     : "i0", "i1", "i2", "i3", "i4", "i5");
450 8c6939c0 bellard
#elif defined(__arm__)
451 3fb2ded1 bellard
                asm volatile ("mov pc, %0\n\t"
452 3fb2ded1 bellard
                              ".global exec_loop\n\t"
453 3fb2ded1 bellard
                              "exec_loop:\n\t"
454 3fb2ded1 bellard
                              : /* no outputs */
455 3fb2ded1 bellard
                              : "r" (gen_func)
456 3fb2ded1 bellard
                              : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
457 bf3e8bf1 bellard
#elif defined(TARGET_I386) && defined(USE_CODE_COPY)
458 bf3e8bf1 bellard
{
459 bf3e8bf1 bellard
    if (!(tb->cflags & CF_CODE_COPY)) {
460 97eb5b14 bellard
        if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
461 97eb5b14 bellard
            save_native_fp_state(env);
462 97eb5b14 bellard
        }
463 bf3e8bf1 bellard
        gen_func();
464 bf3e8bf1 bellard
    } else {
465 97eb5b14 bellard
        if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
466 97eb5b14 bellard
            restore_native_fp_state(env);
467 97eb5b14 bellard
        }
468 bf3e8bf1 bellard
        /* we work with native eflags */
469 bf3e8bf1 bellard
        CC_SRC = cc_table[CC_OP].compute_all();
470 bf3e8bf1 bellard
        CC_OP = CC_OP_EFLAGS;
471 bf3e8bf1 bellard
        asm(".globl exec_loop\n"
472 bf3e8bf1 bellard
            "\n"
473 bf3e8bf1 bellard
            "debug1:\n"
474 bf3e8bf1 bellard
            "    pushl %%ebp\n"
475 bf3e8bf1 bellard
            "    fs movl %10, %9\n"
476 bf3e8bf1 bellard
            "    fs movl %11, %%eax\n"
477 bf3e8bf1 bellard
            "    andl $0x400, %%eax\n"
478 bf3e8bf1 bellard
            "    fs orl %8, %%eax\n"
479 bf3e8bf1 bellard
            "    pushl %%eax\n"
480 bf3e8bf1 bellard
            "    popf\n"
481 bf3e8bf1 bellard
            "    fs movl %%esp, %12\n"
482 bf3e8bf1 bellard
            "    fs movl %0, %%eax\n"
483 bf3e8bf1 bellard
            "    fs movl %1, %%ecx\n"
484 bf3e8bf1 bellard
            "    fs movl %2, %%edx\n"
485 bf3e8bf1 bellard
            "    fs movl %3, %%ebx\n"
486 bf3e8bf1 bellard
            "    fs movl %4, %%esp\n"
487 bf3e8bf1 bellard
            "    fs movl %5, %%ebp\n"
488 bf3e8bf1 bellard
            "    fs movl %6, %%esi\n"
489 bf3e8bf1 bellard
            "    fs movl %7, %%edi\n"
490 bf3e8bf1 bellard
            "    fs jmp *%9\n"
491 bf3e8bf1 bellard
            "exec_loop:\n"
492 bf3e8bf1 bellard
            "    fs movl %%esp, %4\n"
493 bf3e8bf1 bellard
            "    fs movl %12, %%esp\n"
494 bf3e8bf1 bellard
            "    fs movl %%eax, %0\n"
495 bf3e8bf1 bellard
            "    fs movl %%ecx, %1\n"
496 bf3e8bf1 bellard
            "    fs movl %%edx, %2\n"
497 bf3e8bf1 bellard
            "    fs movl %%ebx, %3\n"
498 bf3e8bf1 bellard
            "    fs movl %%ebp, %5\n"
499 bf3e8bf1 bellard
            "    fs movl %%esi, %6\n"
500 bf3e8bf1 bellard
            "    fs movl %%edi, %7\n"
501 bf3e8bf1 bellard
            "    pushf\n"
502 bf3e8bf1 bellard
            "    popl %%eax\n"
503 bf3e8bf1 bellard
            "    movl %%eax, %%ecx\n"
504 bf3e8bf1 bellard
            "    andl $0x400, %%ecx\n"
505 bf3e8bf1 bellard
            "    shrl $9, %%ecx\n"
506 bf3e8bf1 bellard
            "    andl $0x8d5, %%eax\n"
507 bf3e8bf1 bellard
            "    fs movl %%eax, %8\n"
508 bf3e8bf1 bellard
            "    movl $1, %%eax\n"
509 bf3e8bf1 bellard
            "    subl %%ecx, %%eax\n"
510 bf3e8bf1 bellard
            "    fs movl %%eax, %11\n"
511 bf3e8bf1 bellard
            "    fs movl %9, %%ebx\n" /* get T0 value */
512 bf3e8bf1 bellard
            "    popl %%ebp\n"
513 bf3e8bf1 bellard
            :
514 bf3e8bf1 bellard
            : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
515 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
516 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
517 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
518 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
519 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
520 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
521 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
522 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
523 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
524 bf3e8bf1 bellard
            "a" (gen_func),
525 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, df)),
526 bf3e8bf1 bellard
            "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
527 bf3e8bf1 bellard
            : "%ecx", "%edx"
528 bf3e8bf1 bellard
            );
529 bf3e8bf1 bellard
    }
530 bf3e8bf1 bellard
}
531 ae228531 bellard
#else
532 3fb2ded1 bellard
                gen_func();
533 ae228531 bellard
#endif
534 83479e77 bellard
                env->current_tb = NULL;
535 4cbf74b6 bellard
                /* reset soft MMU for next block (it can currently
536 4cbf74b6 bellard
                   only be set by a memory fault) */
537 4cbf74b6 bellard
#if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
538 3f337316 bellard
                if (env->hflags & HF_SOFTMMU_MASK) {
539 3f337316 bellard
                    env->hflags &= ~HF_SOFTMMU_MASK;
540 4cbf74b6 bellard
                    /* do not allow linking to another block */
541 4cbf74b6 bellard
                    T0 = 0;
542 4cbf74b6 bellard
                }
543 4cbf74b6 bellard
#endif
544 3fb2ded1 bellard
            }
545 3fb2ded1 bellard
        } else {
546 7d13299d bellard
        }
547 3fb2ded1 bellard
    } /* for(;;) */
548 3fb2ded1 bellard
549 7d13299d bellard
550 e4533c7a bellard
#if defined(TARGET_I386)
551 97eb5b14 bellard
#if defined(USE_CODE_COPY)
552 97eb5b14 bellard
    if (env->native_fp_regs) {
553 97eb5b14 bellard
        save_native_fp_state(env);
554 97eb5b14 bellard
    }
555 97eb5b14 bellard
#endif
556 9de5e440 bellard
    /* restore flags in standard format */
557 fc2b4c48 bellard
    env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
558 9de5e440 bellard
559 7d13299d bellard
    /* restore global registers */
560 04369ff2 bellard
#ifdef reg_EAX
561 04369ff2 bellard
    EAX = saved_EAX;
562 04369ff2 bellard
#endif
563 04369ff2 bellard
#ifdef reg_ECX
564 04369ff2 bellard
    ECX = saved_ECX;
565 04369ff2 bellard
#endif
566 04369ff2 bellard
#ifdef reg_EDX
567 04369ff2 bellard
    EDX = saved_EDX;
568 04369ff2 bellard
#endif
569 04369ff2 bellard
#ifdef reg_EBX
570 04369ff2 bellard
    EBX = saved_EBX;
571 04369ff2 bellard
#endif
572 04369ff2 bellard
#ifdef reg_ESP
573 04369ff2 bellard
    ESP = saved_ESP;
574 04369ff2 bellard
#endif
575 04369ff2 bellard
#ifdef reg_EBP
576 04369ff2 bellard
    EBP = saved_EBP;
577 04369ff2 bellard
#endif
578 04369ff2 bellard
#ifdef reg_ESI
579 04369ff2 bellard
    ESI = saved_ESI;
580 04369ff2 bellard
#endif
581 04369ff2 bellard
#ifdef reg_EDI
582 04369ff2 bellard
    EDI = saved_EDI;
583 04369ff2 bellard
#endif
584 e4533c7a bellard
#elif defined(TARGET_ARM)
585 1b21b62a bellard
    env->cpsr = compute_cpsr();
586 93ac68bc bellard
#elif defined(TARGET_SPARC)
587 67867308 bellard
#elif defined(TARGET_PPC)
588 e4533c7a bellard
#else
589 e4533c7a bellard
#error unsupported target CPU
590 e4533c7a bellard
#endif
591 8c6939c0 bellard
#ifdef __sparc__
592 8c6939c0 bellard
    asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
593 8c6939c0 bellard
#endif
594 7d13299d bellard
    T0 = saved_T0;
595 7d13299d bellard
    T1 = saved_T1;
596 e4533c7a bellard
    T2 = saved_T2;
597 7d13299d bellard
    env = saved_env;
598 7d13299d bellard
    return ret;
599 7d13299d bellard
}
600 6dbad63e bellard
601 fbf9eeb3 bellard
/* must only be called from the generated code as an exception can be
602 fbf9eeb3 bellard
   generated */
603 fbf9eeb3 bellard
void tb_invalidate_page_range(target_ulong start, target_ulong end)
604 fbf9eeb3 bellard
{
605 fbf9eeb3 bellard
    target_ulong phys_addr;
606 fbf9eeb3 bellard
    phys_addr = get_phys_addr_code(env, start);
607 fbf9eeb3 bellard
    tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
608 fbf9eeb3 bellard
}
609 fbf9eeb3 bellard
610 1a18c71b bellard
#if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
611 e4533c7a bellard
612 6dbad63e bellard
void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
613 6dbad63e bellard
{
614 6dbad63e bellard
    CPUX86State *saved_env;
615 6dbad63e bellard
616 6dbad63e bellard
    saved_env = env;
617 6dbad63e bellard
    env = s;
618 a412ac57 bellard
    if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
619 a513fe19 bellard
        selector &= 0xffff;
620 2e255c6b bellard
        cpu_x86_load_seg_cache(env, seg_reg, selector, 
621 2e255c6b bellard
                               (uint8_t *)(selector << 4), 0xffff, 0);
622 a513fe19 bellard
    } else {
623 b453b70b bellard
        load_seg(seg_reg, selector);
624 a513fe19 bellard
    }
625 6dbad63e bellard
    env = saved_env;
626 6dbad63e bellard
}
627 9de5e440 bellard
628 d0a1ffc9 bellard
void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
629 d0a1ffc9 bellard
{
630 d0a1ffc9 bellard
    CPUX86State *saved_env;
631 d0a1ffc9 bellard
632 d0a1ffc9 bellard
    saved_env = env;
633 d0a1ffc9 bellard
    env = s;
634 d0a1ffc9 bellard
    
635 d0a1ffc9 bellard
    helper_fsave(ptr, data32);
636 d0a1ffc9 bellard
637 d0a1ffc9 bellard
    env = saved_env;
638 d0a1ffc9 bellard
}
639 d0a1ffc9 bellard
640 d0a1ffc9 bellard
void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
641 d0a1ffc9 bellard
{
642 d0a1ffc9 bellard
    CPUX86State *saved_env;
643 d0a1ffc9 bellard
644 d0a1ffc9 bellard
    saved_env = env;
645 d0a1ffc9 bellard
    env = s;
646 d0a1ffc9 bellard
    
647 d0a1ffc9 bellard
    helper_frstor(ptr, data32);
648 d0a1ffc9 bellard
649 d0a1ffc9 bellard
    env = saved_env;
650 d0a1ffc9 bellard
}
651 d0a1ffc9 bellard
652 e4533c7a bellard
#endif /* TARGET_I386 */
653 e4533c7a bellard
654 67b915a5 bellard
#if !defined(CONFIG_SOFTMMU)
655 67b915a5 bellard
656 3fb2ded1 bellard
#if defined(TARGET_I386)
657 3fb2ded1 bellard
658 b56dad1c bellard
/* 'pc' is the host PC at which the exception was raised. 'address' is
659 fd6ce8f6 bellard
   the effective address of the memory exception. 'is_write' is 1 if a
660 fd6ce8f6 bellard
   write caused the exception and otherwise 0'. 'old_set' is the
661 fd6ce8f6 bellard
   signal set which should be restored */
662 2b413144 bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
663 bf3e8bf1 bellard
                                    int is_write, sigset_t *old_set, 
664 bf3e8bf1 bellard
                                    void *puc)
665 9de5e440 bellard
{
666 a513fe19 bellard
    TranslationBlock *tb;
667 a513fe19 bellard
    int ret;
668 68a79315 bellard
669 83479e77 bellard
    if (cpu_single_env)
670 83479e77 bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
671 fd6ce8f6 bellard
#if defined(DEBUG_SIGNAL)
672 bf3e8bf1 bellard
    qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
673 bf3e8bf1 bellard
                pc, address, is_write, *(unsigned long *)old_set);
674 9de5e440 bellard
#endif
675 25eb4484 bellard
    /* XXX: locking issue */
676 fbf9eeb3 bellard
    if (is_write && page_unprotect(address, pc, puc)) {
677 fd6ce8f6 bellard
        return 1;
678 fd6ce8f6 bellard
    }
679 fbf9eeb3 bellard
680 3fb2ded1 bellard
    /* see if it is an MMU fault */
681 93a40ea9 bellard
    ret = cpu_x86_handle_mmu_fault(env, address, is_write, 
682 93a40ea9 bellard
                                   ((env->hflags & HF_CPL_MASK) == 3), 0);
683 3fb2ded1 bellard
    if (ret < 0)
684 3fb2ded1 bellard
        return 0; /* not an MMU fault */
685 3fb2ded1 bellard
    if (ret == 0)
686 3fb2ded1 bellard
        return 1; /* the MMU fault was handled without causing real CPU fault */
687 3fb2ded1 bellard
    /* now we have a real cpu fault */
688 a513fe19 bellard
    tb = tb_find_pc(pc);
689 a513fe19 bellard
    if (tb) {
690 9de5e440 bellard
        /* the PC is inside the translated code. It means that we have
691 9de5e440 bellard
           a virtual CPU fault */
692 bf3e8bf1 bellard
        cpu_restore_state(tb, env, pc, puc);
693 3fb2ded1 bellard
    }
694 4cbf74b6 bellard
    if (ret == 1) {
695 3fb2ded1 bellard
#if 0
696 4cbf74b6 bellard
        printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n", 
697 4cbf74b6 bellard
               env->eip, env->cr[2], env->error_code);
698 3fb2ded1 bellard
#endif
699 4cbf74b6 bellard
        /* we restore the process signal mask as the sigreturn should
700 4cbf74b6 bellard
           do it (XXX: use sigsetjmp) */
701 4cbf74b6 bellard
        sigprocmask(SIG_SETMASK, old_set, NULL);
702 4cbf74b6 bellard
        raise_exception_err(EXCP0E_PAGE, env->error_code);
703 4cbf74b6 bellard
    } else {
704 4cbf74b6 bellard
        /* activate soft MMU for this block */
705 3f337316 bellard
        env->hflags |= HF_SOFTMMU_MASK;
706 fbf9eeb3 bellard
        cpu_resume_from_signal(env, puc);
707 4cbf74b6 bellard
    }
708 3fb2ded1 bellard
    /* never comes here */
709 3fb2ded1 bellard
    return 1;
710 3fb2ded1 bellard
}
711 3fb2ded1 bellard
712 e4533c7a bellard
#elif defined(TARGET_ARM)
713 3fb2ded1 bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
714 bf3e8bf1 bellard
                                    int is_write, sigset_t *old_set,
715 bf3e8bf1 bellard
                                    void *puc)
716 3fb2ded1 bellard
{
717 3fb2ded1 bellard
    /* XXX: do more */
718 3fb2ded1 bellard
    return 0;
719 3fb2ded1 bellard
}
720 93ac68bc bellard
#elif defined(TARGET_SPARC)
721 93ac68bc bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
722 bf3e8bf1 bellard
                                    int is_write, sigset_t *old_set,
723 bf3e8bf1 bellard
                                    void *puc)
724 93ac68bc bellard
{
725 b453b70b bellard
    /* XXX: locking issue */
726 fbf9eeb3 bellard
    if (is_write && page_unprotect(address, pc, puc)) {
727 b453b70b bellard
        return 1;
728 b453b70b bellard
    }
729 b453b70b bellard
    return 0;
730 93ac68bc bellard
}
731 67867308 bellard
#elif defined (TARGET_PPC)
732 67867308 bellard
static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
733 bf3e8bf1 bellard
                                    int is_write, sigset_t *old_set,
734 bf3e8bf1 bellard
                                    void *puc)
735 67867308 bellard
{
736 67867308 bellard
    TranslationBlock *tb;
737 ce09776b bellard
    int ret;
738 67867308 bellard
    
739 ce09776b bellard
#if 1
740 67867308 bellard
    if (cpu_single_env)
741 67867308 bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
742 67867308 bellard
#endif
743 67867308 bellard
#if defined(DEBUG_SIGNAL)
744 67867308 bellard
    printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 
745 67867308 bellard
           pc, address, is_write, *(unsigned long *)old_set);
746 67867308 bellard
#endif
747 67867308 bellard
    /* XXX: locking issue */
748 fbf9eeb3 bellard
    if (is_write && page_unprotect(address, pc, puc)) {
749 67867308 bellard
        return 1;
750 67867308 bellard
    }
751 67867308 bellard
752 ce09776b bellard
    /* see if it is an MMU fault */
753 7f957d28 bellard
    ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
754 ce09776b bellard
    if (ret < 0)
755 ce09776b bellard
        return 0; /* not an MMU fault */
756 ce09776b bellard
    if (ret == 0)
757 ce09776b bellard
        return 1; /* the MMU fault was handled without causing real CPU fault */
758 ce09776b bellard
759 67867308 bellard
    /* now we have a real cpu fault */
760 67867308 bellard
    tb = tb_find_pc(pc);
761 67867308 bellard
    if (tb) {
762 67867308 bellard
        /* the PC is inside the translated code. It means that we have
763 67867308 bellard
           a virtual CPU fault */
764 bf3e8bf1 bellard
        cpu_restore_state(tb, env, pc, puc);
765 67867308 bellard
    }
766 ce09776b bellard
    if (ret == 1) {
767 67867308 bellard
#if 0
768 ce09776b bellard
        printf("PF exception: NIP=0x%08x error=0x%x %p\n", 
769 ce09776b bellard
               env->nip, env->error_code, tb);
770 67867308 bellard
#endif
771 67867308 bellard
    /* we restore the process signal mask as the sigreturn should
772 67867308 bellard
       do it (XXX: use sigsetjmp) */
773 bf3e8bf1 bellard
        sigprocmask(SIG_SETMASK, old_set, NULL);
774 9fddaa0c bellard
        do_raise_exception_err(env->exception_index, env->error_code);
775 ce09776b bellard
    } else {
776 ce09776b bellard
        /* activate soft MMU for this block */
777 fbf9eeb3 bellard
        cpu_resume_from_signal(env, puc);
778 ce09776b bellard
    }
779 67867308 bellard
    /* never comes here */
780 67867308 bellard
    return 1;
781 67867308 bellard
}
782 e4533c7a bellard
#else
783 e4533c7a bellard
#error unsupported target CPU
784 e4533c7a bellard
#endif
785 9de5e440 bellard
786 2b413144 bellard
#if defined(__i386__)
787 2b413144 bellard
788 bf3e8bf1 bellard
#if defined(USE_CODE_COPY)
789 bf3e8bf1 bellard
static void cpu_send_trap(unsigned long pc, int trap, 
790 bf3e8bf1 bellard
                          struct ucontext *uc)
791 bf3e8bf1 bellard
{
792 bf3e8bf1 bellard
    TranslationBlock *tb;
793 bf3e8bf1 bellard
794 bf3e8bf1 bellard
    if (cpu_single_env)
795 bf3e8bf1 bellard
        env = cpu_single_env; /* XXX: find a correct solution for multithread */
796 bf3e8bf1 bellard
    /* now we have a real cpu fault */
797 bf3e8bf1 bellard
    tb = tb_find_pc(pc);
798 bf3e8bf1 bellard
    if (tb) {
799 bf3e8bf1 bellard
        /* the PC is inside the translated code. It means that we have
800 bf3e8bf1 bellard
           a virtual CPU fault */
801 bf3e8bf1 bellard
        cpu_restore_state(tb, env, pc, uc);
802 bf3e8bf1 bellard
    }
803 bf3e8bf1 bellard
    sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
804 bf3e8bf1 bellard
    raise_exception_err(trap, env->error_code);
805 bf3e8bf1 bellard
}
806 bf3e8bf1 bellard
#endif
807 bf3e8bf1 bellard
808 e4533c7a bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
809 e4533c7a bellard
                       void *puc)
810 9de5e440 bellard
{
811 9de5e440 bellard
    struct ucontext *uc = puc;
812 9de5e440 bellard
    unsigned long pc;
813 bf3e8bf1 bellard
    int trapno;
814 97eb5b14 bellard
815 d691f669 bellard
#ifndef REG_EIP
816 d691f669 bellard
/* for glibc 2.1 */
817 fd6ce8f6 bellard
#define REG_EIP    EIP
818 fd6ce8f6 bellard
#define REG_ERR    ERR
819 fd6ce8f6 bellard
#define REG_TRAPNO TRAPNO
820 d691f669 bellard
#endif
821 fc2b4c48 bellard
    pc = uc->uc_mcontext.gregs[REG_EIP];
822 bf3e8bf1 bellard
    trapno = uc->uc_mcontext.gregs[REG_TRAPNO];
823 bf3e8bf1 bellard
#if defined(TARGET_I386) && defined(USE_CODE_COPY)
824 bf3e8bf1 bellard
    if (trapno == 0x00 || trapno == 0x05) {
825 bf3e8bf1 bellard
        /* send division by zero or bound exception */
826 bf3e8bf1 bellard
        cpu_send_trap(pc, trapno, uc);
827 bf3e8bf1 bellard
        return 1;
828 bf3e8bf1 bellard
    } else
829 bf3e8bf1 bellard
#endif
830 bf3e8bf1 bellard
        return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
831 bf3e8bf1 bellard
                                 trapno == 0xe ? 
832 bf3e8bf1 bellard
                                 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
833 bf3e8bf1 bellard
                                 &uc->uc_sigmask, puc);
834 2b413144 bellard
}
835 2b413144 bellard
836 bc51c5c9 bellard
#elif defined(__x86_64__)
837 bc51c5c9 bellard
838 bc51c5c9 bellard
int cpu_signal_handler(int host_signum, struct siginfo *info,
839 bc51c5c9 bellard
                       void *puc)
840 bc51c5c9 bellard
{
841 bc51c5c9 bellard
    struct ucontext *uc = puc;
842 bc51c5c9 bellard
    unsigned long pc;
843 bc51c5c9 bellard
844 bc51c5c9 bellard
    pc = uc->uc_mcontext.gregs[REG_RIP];
845 bc51c5c9 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
846 bc51c5c9 bellard
                             uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ? 
847 bc51c5c9 bellard
                             (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
848 bc51c5c9 bellard
                             &uc->uc_sigmask, puc);
849 bc51c5c9 bellard
}
850 bc51c5c9 bellard
851 25eb4484 bellard
#elif defined(__powerpc)
852 2b413144 bellard
853 e4533c7a bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
854 e4533c7a bellard
                       void *puc)
855 2b413144 bellard
{
856 25eb4484 bellard
    struct ucontext *uc = puc;
857 25eb4484 bellard
    struct pt_regs *regs = uc->uc_mcontext.regs;
858 25eb4484 bellard
    unsigned long pc;
859 25eb4484 bellard
    int is_write;
860 25eb4484 bellard
861 25eb4484 bellard
    pc = regs->nip;
862 25eb4484 bellard
    is_write = 0;
863 25eb4484 bellard
#if 0
864 25eb4484 bellard
    /* ppc 4xx case */
865 25eb4484 bellard
    if (regs->dsisr & 0x00800000)
866 25eb4484 bellard
        is_write = 1;
867 25eb4484 bellard
#else
868 25eb4484 bellard
    if (regs->trap != 0x400 && (regs->dsisr & 0x02000000))
869 25eb4484 bellard
        is_write = 1;
870 25eb4484 bellard
#endif
871 25eb4484 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
872 bf3e8bf1 bellard
                             is_write, &uc->uc_sigmask, puc);
873 2b413144 bellard
}
874 2b413144 bellard
875 2f87c607 bellard
#elif defined(__alpha__)
876 2f87c607 bellard
877 e4533c7a bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
878 2f87c607 bellard
                           void *puc)
879 2f87c607 bellard
{
880 2f87c607 bellard
    struct ucontext *uc = puc;
881 2f87c607 bellard
    uint32_t *pc = uc->uc_mcontext.sc_pc;
882 2f87c607 bellard
    uint32_t insn = *pc;
883 2f87c607 bellard
    int is_write = 0;
884 2f87c607 bellard
885 8c6939c0 bellard
    /* XXX: need kernel patch to get write flag faster */
886 2f87c607 bellard
    switch (insn >> 26) {
887 2f87c607 bellard
    case 0x0d: // stw
888 2f87c607 bellard
    case 0x0e: // stb
889 2f87c607 bellard
    case 0x0f: // stq_u
890 2f87c607 bellard
    case 0x24: // stf
891 2f87c607 bellard
    case 0x25: // stg
892 2f87c607 bellard
    case 0x26: // sts
893 2f87c607 bellard
    case 0x27: // stt
894 2f87c607 bellard
    case 0x2c: // stl
895 2f87c607 bellard
    case 0x2d: // stq
896 2f87c607 bellard
    case 0x2e: // stl_c
897 2f87c607 bellard
    case 0x2f: // stq_c
898 2f87c607 bellard
        is_write = 1;
899 2f87c607 bellard
    }
900 2f87c607 bellard
901 2f87c607 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
902 bf3e8bf1 bellard
                             is_write, &uc->uc_sigmask, puc);
903 2f87c607 bellard
}
904 8c6939c0 bellard
#elif defined(__sparc__)
905 8c6939c0 bellard
906 e4533c7a bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
907 e4533c7a bellard
                       void *puc)
908 8c6939c0 bellard
{
909 8c6939c0 bellard
    uint32_t *regs = (uint32_t *)(info + 1);
910 8c6939c0 bellard
    void *sigmask = (regs + 20);
911 8c6939c0 bellard
    unsigned long pc;
912 8c6939c0 bellard
    int is_write;
913 8c6939c0 bellard
    uint32_t insn;
914 8c6939c0 bellard
    
915 8c6939c0 bellard
    /* XXX: is there a standard glibc define ? */
916 8c6939c0 bellard
    pc = regs[1];
917 8c6939c0 bellard
    /* XXX: need kernel patch to get write flag faster */
918 8c6939c0 bellard
    is_write = 0;
919 8c6939c0 bellard
    insn = *(uint32_t *)pc;
920 8c6939c0 bellard
    if ((insn >> 30) == 3) {
921 8c6939c0 bellard
      switch((insn >> 19) & 0x3f) {
922 8c6939c0 bellard
      case 0x05: // stb
923 8c6939c0 bellard
      case 0x06: // sth
924 8c6939c0 bellard
      case 0x04: // st
925 8c6939c0 bellard
      case 0x07: // std
926 8c6939c0 bellard
      case 0x24: // stf
927 8c6939c0 bellard
      case 0x27: // stdf
928 8c6939c0 bellard
      case 0x25: // stfsr
929 8c6939c0 bellard
        is_write = 1;
930 8c6939c0 bellard
        break;
931 8c6939c0 bellard
      }
932 8c6939c0 bellard
    }
933 8c6939c0 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
934 bf3e8bf1 bellard
                             is_write, sigmask, NULL);
935 8c6939c0 bellard
}
936 8c6939c0 bellard
937 8c6939c0 bellard
#elif defined(__arm__)
938 8c6939c0 bellard
939 e4533c7a bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
940 e4533c7a bellard
                       void *puc)
941 8c6939c0 bellard
{
942 8c6939c0 bellard
    struct ucontext *uc = puc;
943 8c6939c0 bellard
    unsigned long pc;
944 8c6939c0 bellard
    int is_write;
945 8c6939c0 bellard
    
946 8c6939c0 bellard
    pc = uc->uc_mcontext.gregs[R15];
947 8c6939c0 bellard
    /* XXX: compute is_write */
948 8c6939c0 bellard
    is_write = 0;
949 8c6939c0 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
950 8c6939c0 bellard
                             is_write,
951 8c6939c0 bellard
                             &uc->uc_sigmask);
952 8c6939c0 bellard
}
953 8c6939c0 bellard
954 38e584a0 bellard
#elif defined(__mc68000)
955 38e584a0 bellard
956 38e584a0 bellard
int cpu_signal_handler(int host_signum, struct siginfo *info, 
957 38e584a0 bellard
                       void *puc)
958 38e584a0 bellard
{
959 38e584a0 bellard
    struct ucontext *uc = puc;
960 38e584a0 bellard
    unsigned long pc;
961 38e584a0 bellard
    int is_write;
962 38e584a0 bellard
    
963 38e584a0 bellard
    pc = uc->uc_mcontext.gregs[16];
964 38e584a0 bellard
    /* XXX: compute is_write */
965 38e584a0 bellard
    is_write = 0;
966 38e584a0 bellard
    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
967 38e584a0 bellard
                             is_write,
968 bf3e8bf1 bellard
                             &uc->uc_sigmask, puc);
969 38e584a0 bellard
}
970 38e584a0 bellard
971 9de5e440 bellard
#else
972 2b413144 bellard
973 3fb2ded1 bellard
#error host CPU specific signal handler needed
974 2b413144 bellard
975 9de5e440 bellard
#endif
976 67b915a5 bellard
977 67b915a5 bellard
#endif /* !defined(CONFIG_SOFTMMU) */