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