2 * i386 emulator main execution loop
4 * Copyright (c) 2003-2005 Fabrice Bellard
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.
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.
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
24 #if !defined(CONFIG_SOFTMMU)
35 #include <sys/ucontext.h>
38 int tb_invalidated_flag;
41 //#define DEBUG_SIGNAL
43 #if defined(TARGET_ARM) || defined(TARGET_SPARC) || defined(TARGET_M68K)
44 /* XXX: unify with i386 target */
45 void cpu_loop_exit(void)
47 longjmp(env->jmp_env, 1);
50 #if !(defined(TARGET_SPARC) || defined(TARGET_SH4) || defined(TARGET_M68K))
54 /* exit the current TB from a signal handler. The host registers are
55 restored in a state compatible with the CPU emulator
57 void cpu_resume_from_signal(CPUState *env1, void *puc)
59 #if !defined(CONFIG_SOFTMMU)
60 struct ucontext *uc = puc;
65 /* XXX: restore cpu registers saved in host registers */
67 #if !defined(CONFIG_SOFTMMU)
69 /* XXX: use siglongjmp ? */
70 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
73 longjmp(env->jmp_env, 1);
77 static TranslationBlock *tb_find_slow(target_ulong pc,
81 TranslationBlock *tb, **ptb1;
84 target_ulong phys_pc, phys_page1, phys_page2, virt_page2;
89 tb_invalidated_flag = 0;
91 regs_to_env(); /* XXX: do it just before cpu_gen_code() */
93 /* find translated block using physical mappings */
94 phys_pc = get_phys_addr_code(env, pc);
95 phys_page1 = phys_pc & TARGET_PAGE_MASK;
97 h = tb_phys_hash_func(phys_pc);
98 ptb1 = &tb_phys_hash[h];
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) +
111 phys_page2 = get_phys_addr_code(env, virt_page2);
112 if (tb->page_addr[1] == phys_page2)
118 ptb1 = &tb->phys_hash_next;
121 /* if no translated code available, then translate it now */
124 /* flush must be done */
126 /* cannot fail at this point */
128 /* don't forget to invalidate previous TB info */
129 tb_invalidated_flag = 1;
131 tc_ptr = code_gen_ptr;
133 tb->cs_base = cs_base;
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));
138 /* check next page if needed */
139 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
141 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
142 phys_page2 = get_phys_addr_code(env, virt_page2);
144 tb_link_phys(tb, phys_pc, phys_page2);
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);
153 static inline TranslationBlock *tb_find_fast(void)
155 TranslationBlock *tb;
156 target_ulong cs_base, pc;
159 /* we record a subset of the CPU state. It will
160 always be the same before a given translated block
162 #if defined(TARGET_I386)
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)
169 | (env->vfp.vec_stride << 4);
170 if ((env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR)
172 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30))
176 #elif defined(TARGET_SPARC)
177 #ifdef TARGET_SPARC64
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);
182 // FPU enable . MMU enabled . MMU no-fault . Supervisor
183 flags = (env->psref << 3) | ((env->mmuregs[0] & (MMU_E | MMU_NF)) << 1)
188 #elif defined(TARGET_PPC)
189 flags = (msr_pr << MSR_PR) | (msr_fp << MSR_FP) |
190 (msr_se << MSR_SE) | (msr_le << MSR_LE);
193 #elif defined(TARGET_MIPS)
194 flags = env->hflags & (MIPS_HFLAG_TMASK | MIPS_HFLAG_BMASK);
197 #elif defined(TARGET_M68K)
198 flags = env->fpcr & M68K_FPCR_PREC;
201 #elif defined(TARGET_SH4)
202 flags = env->sr & (SR_MD | SR_RB);
203 cs_base = 0; /* XXXXX */
206 #error unsupported CPU
208 tb = env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)];
209 if (__builtin_expect(!tb || tb->pc != pc || tb->cs_base != cs_base ||
210 tb->flags != flags, 0)) {
211 tb = tb_find_slow(pc, cs_base, flags);
212 /* Note: we do it here to avoid a gcc bug on Mac OS X when
213 doing it in tb_find_slow */
214 if (tb_invalidated_flag) {
215 /* as some TB could have been invalidated because
216 of memory exceptions while generating the code, we
217 must recompute the hash index here */
225 /* main execution loop */
227 int cpu_exec(CPUState *env1)
229 #define DECLARE_HOST_REGS 1
230 #include "hostregs_helper.h"
231 #if defined(TARGET_SPARC)
232 #if defined(reg_REGWPTR)
233 uint32_t *saved_regwptr;
236 #if defined(__sparc__) && !defined(HOST_SOLARIS)
240 int ret, interrupt_request;
241 void (*gen_func)(void);
242 TranslationBlock *tb;
245 #if defined(TARGET_I386)
246 /* handle exit of HALTED state */
247 if (env1->hflags & HF_HALTED_MASK) {
248 /* disable halt condition */
249 if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
250 (env1->eflags & IF_MASK)) {
251 env1->hflags &= ~HF_HALTED_MASK;
256 #elif defined(TARGET_PPC)
258 if (env1->msr[MSR_EE] &&
259 (env1->interrupt_request & CPU_INTERRUPT_HARD)) {
265 #elif defined(TARGET_SPARC)
267 if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
268 (env1->psret != 0)) {
274 #elif defined(TARGET_ARM)
276 /* An interrupt wakes the CPU even if the I and F CPSR bits are
278 if (env1->interrupt_request
279 & (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD)) {
285 #elif defined(TARGET_MIPS)
287 if (env1->interrupt_request &
288 (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER)) {
296 cpu_single_env = env1;
298 /* first we save global registers */
299 #define SAVE_HOST_REGS 1
300 #include "hostregs_helper.h"
302 #if defined(__sparc__) && !defined(HOST_SOLARIS)
303 /* we also save i7 because longjmp may not restore it */
304 asm volatile ("mov %%i7, %0" : "=r" (saved_i7));
307 #if defined(TARGET_I386)
309 /* put eflags in CPU temporary format */
310 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
311 DF = 1 - (2 * ((env->eflags >> 10) & 1));
312 CC_OP = CC_OP_EFLAGS;
313 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
314 #elif defined(TARGET_ARM)
315 #elif defined(TARGET_SPARC)
316 #if defined(reg_REGWPTR)
317 saved_regwptr = REGWPTR;
319 #elif defined(TARGET_PPC)
320 #elif defined(TARGET_M68K)
321 env->cc_op = CC_OP_FLAGS;
322 env->cc_dest = env->sr & 0xf;
323 env->cc_x = (env->sr >> 4) & 1;
324 #elif defined(TARGET_MIPS)
325 #elif defined(TARGET_SH4)
328 #error unsupported target CPU
330 env->exception_index = -1;
332 /* prepare setjmp context for exception handling */
334 if (setjmp(env->jmp_env) == 0) {
335 env->current_tb = NULL;
336 /* if an exception is pending, we execute it here */
337 if (env->exception_index >= 0) {
338 if (env->exception_index >= EXCP_INTERRUPT) {
339 /* exit request from the cpu execution loop */
340 ret = env->exception_index;
342 } else if (env->user_mode_only) {
343 /* if user mode only, we simulate a fake exception
344 which will be handled outside the cpu execution
346 #if defined(TARGET_I386)
347 do_interrupt_user(env->exception_index,
348 env->exception_is_int,
350 env->exception_next_eip);
352 ret = env->exception_index;
355 #if defined(TARGET_I386)
356 /* simulate a real cpu exception. On i386, it can
357 trigger new exceptions, but we do not handle
358 double or triple faults yet. */
359 do_interrupt(env->exception_index,
360 env->exception_is_int,
362 env->exception_next_eip, 0);
363 /* successfully delivered */
364 env->old_exception = -1;
365 #elif defined(TARGET_PPC)
367 #elif defined(TARGET_MIPS)
369 #elif defined(TARGET_SPARC)
370 do_interrupt(env->exception_index);
371 #elif defined(TARGET_ARM)
373 #elif defined(TARGET_SH4)
377 env->exception_index = -1;
380 if (kqemu_is_ok(env) && env->interrupt_request == 0) {
382 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
383 ret = kqemu_cpu_exec(env);
384 /* put eflags in CPU temporary format */
385 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
386 DF = 1 - (2 * ((env->eflags >> 10) & 1));
387 CC_OP = CC_OP_EFLAGS;
388 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
391 longjmp(env->jmp_env, 1);
392 } else if (ret == 2) {
393 /* softmmu execution needed */
395 if (env->interrupt_request != 0) {
396 /* hardware interrupt will be executed just after */
398 /* otherwise, we restart */
399 longjmp(env->jmp_env, 1);
405 T0 = 0; /* force lookup of first TB */
407 #if defined(__sparc__) && !defined(HOST_SOLARIS)
408 /* g1 can be modified by some libc? functions */
411 interrupt_request = env->interrupt_request;
412 if (__builtin_expect(interrupt_request, 0)) {
413 if (interrupt_request & CPU_INTERRUPT_DEBUG) {
414 env->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
415 env->exception_index = EXCP_DEBUG;
418 #if defined(TARGET_I386)
419 if ((interrupt_request & CPU_INTERRUPT_SMI) &&
420 !(env->hflags & HF_SMM_MASK)) {
421 env->interrupt_request &= ~CPU_INTERRUPT_SMI;
423 #if defined(__sparc__) && !defined(HOST_SOLARIS)
428 } else if ((interrupt_request & CPU_INTERRUPT_HARD) &&
429 (env->eflags & IF_MASK) &&
430 !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
432 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
433 intno = cpu_get_pic_interrupt(env);
434 if (loglevel & CPU_LOG_TB_IN_ASM) {
435 fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
437 do_interrupt(intno, 0, 0, 0, 1);
438 /* ensure that no TB jump will be modified as
439 the program flow was changed */
440 #if defined(__sparc__) && !defined(HOST_SOLARIS)
446 #elif defined(TARGET_PPC)
448 if ((interrupt_request & CPU_INTERRUPT_RESET)) {
452 if (interrupt_request & CPU_INTERRUPT_HARD) {
453 if (ppc_hw_interrupt(env) == 1) {
454 /* Some exception was raised */
455 if (env->pending_interrupts == 0)
456 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
457 #if defined(__sparc__) && !defined(HOST_SOLARIS)
464 #elif defined(TARGET_MIPS)
465 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
466 (env->CP0_Status & env->CP0_Cause & CP0Ca_IP_mask) &&
467 (env->CP0_Status & (1 << CP0St_IE)) &&
468 !(env->CP0_Status & (1 << CP0St_EXL)) &&
469 !(env->CP0_Status & (1 << CP0St_ERL)) &&
470 !(env->hflags & MIPS_HFLAG_DM)) {
472 env->exception_index = EXCP_EXT_INTERRUPT;
475 #if defined(__sparc__) && !defined(HOST_SOLARIS)
481 #elif defined(TARGET_SPARC)
482 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
484 int pil = env->interrupt_index & 15;
485 int type = env->interrupt_index & 0xf0;
487 if (((type == TT_EXTINT) &&
488 (pil == 15 || pil > env->psrpil)) ||
490 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
491 do_interrupt(env->interrupt_index);
492 env->interrupt_index = 0;
493 #if defined(__sparc__) && !defined(HOST_SOLARIS)
499 } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
500 //do_interrupt(0, 0, 0, 0, 0);
501 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
502 } else if (interrupt_request & CPU_INTERRUPT_HALT) {
503 env->interrupt_request &= ~CPU_INTERRUPT_HALT;
505 env->exception_index = EXCP_HLT;
508 #elif defined(TARGET_ARM)
509 if (interrupt_request & CPU_INTERRUPT_FIQ
510 && !(env->uncached_cpsr & CPSR_F)) {
511 env->exception_index = EXCP_FIQ;
514 if (interrupt_request & CPU_INTERRUPT_HARD
515 && !(env->uncached_cpsr & CPSR_I)) {
516 env->exception_index = EXCP_IRQ;
519 #elif defined(TARGET_SH4)
522 /* Don't use the cached interupt_request value,
523 do_interrupt may have updated the EXITTB flag. */
524 if (env->interrupt_request & CPU_INTERRUPT_EXITTB) {
525 env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
526 /* ensure that no TB jump will be modified as
527 the program flow was changed */
528 #if defined(__sparc__) && !defined(HOST_SOLARIS)
534 if (interrupt_request & CPU_INTERRUPT_EXIT) {
535 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
536 env->exception_index = EXCP_INTERRUPT;
541 if ((loglevel & CPU_LOG_TB_CPU)) {
542 #if defined(TARGET_I386)
543 /* restore flags in standard format */
545 env->regs[R_EAX] = EAX;
548 env->regs[R_EBX] = EBX;
551 env->regs[R_ECX] = ECX;
554 env->regs[R_EDX] = EDX;
557 env->regs[R_ESI] = ESI;
560 env->regs[R_EDI] = EDI;
563 env->regs[R_EBP] = EBP;
566 env->regs[R_ESP] = ESP;
568 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
569 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
570 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
571 #elif defined(TARGET_ARM)
572 cpu_dump_state(env, logfile, fprintf, 0);
573 #elif defined(TARGET_SPARC)
574 REGWPTR = env->regbase + (env->cwp * 16);
575 env->regwptr = REGWPTR;
576 cpu_dump_state(env, logfile, fprintf, 0);
577 #elif defined(TARGET_PPC)
578 cpu_dump_state(env, logfile, fprintf, 0);
579 #elif defined(TARGET_M68K)
580 cpu_m68k_flush_flags(env, env->cc_op);
581 env->cc_op = CC_OP_FLAGS;
582 env->sr = (env->sr & 0xffe0)
583 | env->cc_dest | (env->cc_x << 4);
584 cpu_dump_state(env, logfile, fprintf, 0);
585 #elif defined(TARGET_MIPS)
586 cpu_dump_state(env, logfile, fprintf, 0);
587 #elif defined(TARGET_SH4)
588 cpu_dump_state(env, logfile, fprintf, 0);
590 #error unsupported target CPU
596 if ((loglevel & CPU_LOG_EXEC)) {
597 fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
598 (long)tb->tc_ptr, tb->pc,
599 lookup_symbol(tb->pc));
602 #if defined(__sparc__) && !defined(HOST_SOLARIS)
605 /* see if we can patch the calling TB. When the TB
606 spans two pages, we cannot safely do a direct
611 (env->kqemu_enabled != 2) &&
613 tb->page_addr[1] == -1
614 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
615 && (tb->cflags & CF_CODE_COPY) ==
616 (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
620 tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
621 #if defined(USE_CODE_COPY)
622 /* propagates the FP use info */
623 ((TranslationBlock *)(T0 & ~3))->cflags |=
624 (tb->cflags & CF_FP_USED);
626 spin_unlock(&tb_lock);
630 env->current_tb = tb;
631 /* execute the generated code */
632 gen_func = (void *)tc_ptr;
633 #if defined(__sparc__)
634 __asm__ __volatile__("call %0\n\t"
638 : "i0", "i1", "i2", "i3", "i4", "i5",
639 "o0", "o1", "o2", "o3", "o4", "o5",
640 "l0", "l1", "l2", "l3", "l4", "l5",
642 #elif defined(__arm__)
643 asm volatile ("mov pc, %0\n\t"
644 ".global exec_loop\n\t"
648 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
649 #elif defined(TARGET_I386) && defined(USE_CODE_COPY)
651 if (!(tb->cflags & CF_CODE_COPY)) {
652 if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
653 save_native_fp_state(env);
657 if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
658 restore_native_fp_state(env);
660 /* we work with native eflags */
661 CC_SRC = cc_table[CC_OP].compute_all();
662 CC_OP = CC_OP_EFLAGS;
663 asm(".globl exec_loop\n"
668 " fs movl %11, %%eax\n"
669 " andl $0x400, %%eax\n"
670 " fs orl %8, %%eax\n"
673 " fs movl %%esp, %12\n"
674 " fs movl %0, %%eax\n"
675 " fs movl %1, %%ecx\n"
676 " fs movl %2, %%edx\n"
677 " fs movl %3, %%ebx\n"
678 " fs movl %4, %%esp\n"
679 " fs movl %5, %%ebp\n"
680 " fs movl %6, %%esi\n"
681 " fs movl %7, %%edi\n"
684 " fs movl %%esp, %4\n"
685 " fs movl %12, %%esp\n"
686 " fs movl %%eax, %0\n"
687 " fs movl %%ecx, %1\n"
688 " fs movl %%edx, %2\n"
689 " fs movl %%ebx, %3\n"
690 " fs movl %%ebp, %5\n"
691 " fs movl %%esi, %6\n"
692 " fs movl %%edi, %7\n"
695 " movl %%eax, %%ecx\n"
696 " andl $0x400, %%ecx\n"
698 " andl $0x8d5, %%eax\n"
699 " fs movl %%eax, %8\n"
701 " subl %%ecx, %%eax\n"
702 " fs movl %%eax, %11\n"
703 " fs movl %9, %%ebx\n" /* get T0 value */
706 : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
707 "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
708 "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
709 "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
710 "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
711 "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
712 "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
713 "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
714 "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
715 "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
717 "m" (*(uint8_t *)offsetof(CPUState, df)),
718 "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
723 #elif defined(__ia64)
730 fp.gp = code_gen_buffer + 2 * (1 << 20);
731 (*(void (*)(void)) &fp)();
735 env->current_tb = NULL;
736 /* reset soft MMU for next block (it can currently
737 only be set by a memory fault) */
738 #if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
739 if (env->hflags & HF_SOFTMMU_MASK) {
740 env->hflags &= ~HF_SOFTMMU_MASK;
741 /* do not allow linking to another block */
745 #if defined(USE_KQEMU)
746 #define MIN_CYCLE_BEFORE_SWITCH (100 * 1000)
747 if (kqemu_is_ok(env) &&
748 (cpu_get_time_fast() - env->last_io_time) >= MIN_CYCLE_BEFORE_SWITCH) {
759 #if defined(TARGET_I386)
760 #if defined(USE_CODE_COPY)
761 if (env->native_fp_regs) {
762 save_native_fp_state(env);
765 /* restore flags in standard format */
766 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
767 #elif defined(TARGET_ARM)
768 /* XXX: Save/restore host fpu exception state?. */
769 #elif defined(TARGET_SPARC)
770 #if defined(reg_REGWPTR)
771 REGWPTR = saved_regwptr;
773 #elif defined(TARGET_PPC)
774 #elif defined(TARGET_M68K)
775 cpu_m68k_flush_flags(env, env->cc_op);
776 env->cc_op = CC_OP_FLAGS;
777 env->sr = (env->sr & 0xffe0)
778 | env->cc_dest | (env->cc_x << 4);
779 #elif defined(TARGET_MIPS)
780 #elif defined(TARGET_SH4)
783 #error unsupported target CPU
786 /* restore global registers */
787 #if defined(__sparc__) && !defined(HOST_SOLARIS)
788 asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
790 #include "hostregs_helper.h"
792 /* fail safe : never use cpu_single_env outside cpu_exec() */
793 cpu_single_env = NULL;
797 /* must only be called from the generated code as an exception can be
799 void tb_invalidate_page_range(target_ulong start, target_ulong end)
801 /* XXX: cannot enable it yet because it yields to MMU exception
802 where NIP != read address on PowerPC */
804 target_ulong phys_addr;
805 phys_addr = get_phys_addr_code(env, start);
806 tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
810 #if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
812 void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
814 CPUX86State *saved_env;
818 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
820 cpu_x86_load_seg_cache(env, seg_reg, selector,
821 (selector << 4), 0xffff, 0);
823 load_seg(seg_reg, selector);
828 void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
830 CPUX86State *saved_env;
835 helper_fsave((target_ulong)ptr, data32);
840 void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
842 CPUX86State *saved_env;
847 helper_frstor((target_ulong)ptr, data32);
852 #endif /* TARGET_I386 */
854 #if !defined(CONFIG_SOFTMMU)
856 #if defined(TARGET_I386)
858 /* 'pc' is the host PC at which the exception was raised. 'address' is
859 the effective address of the memory exception. 'is_write' is 1 if a
860 write caused the exception and otherwise 0'. 'old_set' is the
861 signal set which should be restored */
862 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
863 int is_write, sigset_t *old_set,
866 TranslationBlock *tb;
870 env = cpu_single_env; /* XXX: find a correct solution for multithread */
871 #if defined(DEBUG_SIGNAL)
872 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
873 pc, address, is_write, *(unsigned long *)old_set);
875 /* XXX: locking issue */
876 if (is_write && page_unprotect(h2g(address), pc, puc)) {
880 /* see if it is an MMU fault */
881 ret = cpu_x86_handle_mmu_fault(env, address, is_write,
882 ((env->hflags & HF_CPL_MASK) == 3), 0);
884 return 0; /* not an MMU fault */
886 return 1; /* the MMU fault was handled without causing real CPU fault */
887 /* now we have a real cpu fault */
890 /* the PC is inside the translated code. It means that we have
891 a virtual CPU fault */
892 cpu_restore_state(tb, env, pc, puc);
896 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
897 env->eip, env->cr[2], env->error_code);
899 /* we restore the process signal mask as the sigreturn should
900 do it (XXX: use sigsetjmp) */
901 sigprocmask(SIG_SETMASK, old_set, NULL);
902 raise_exception_err(env->exception_index, env->error_code);
904 /* activate soft MMU for this block */
905 env->hflags |= HF_SOFTMMU_MASK;
906 cpu_resume_from_signal(env, puc);
908 /* never comes here */
912 #elif defined(TARGET_ARM)
913 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
914 int is_write, sigset_t *old_set,
917 TranslationBlock *tb;
921 env = cpu_single_env; /* XXX: find a correct solution for multithread */
922 #if defined(DEBUG_SIGNAL)
923 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
924 pc, address, is_write, *(unsigned long *)old_set);
926 /* XXX: locking issue */
927 if (is_write && page_unprotect(h2g(address), pc, puc)) {
930 /* see if it is an MMU fault */
931 ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
933 return 0; /* not an MMU fault */
935 return 1; /* the MMU fault was handled without causing real CPU fault */
936 /* now we have a real cpu fault */
939 /* the PC is inside the translated code. It means that we have
940 a virtual CPU fault */
941 cpu_restore_state(tb, env, pc, puc);
943 /* we restore the process signal mask as the sigreturn should
944 do it (XXX: use sigsetjmp) */
945 sigprocmask(SIG_SETMASK, old_set, NULL);
948 #elif defined(TARGET_SPARC)
949 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
950 int is_write, sigset_t *old_set,
953 TranslationBlock *tb;
957 env = cpu_single_env; /* XXX: find a correct solution for multithread */
958 #if defined(DEBUG_SIGNAL)
959 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
960 pc, address, is_write, *(unsigned long *)old_set);
962 /* XXX: locking issue */
963 if (is_write && page_unprotect(h2g(address), pc, puc)) {
966 /* see if it is an MMU fault */
967 ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
969 return 0; /* not an MMU fault */
971 return 1; /* the MMU fault was handled without causing real CPU fault */
972 /* now we have a real cpu fault */
975 /* the PC is inside the translated code. It means that we have
976 a virtual CPU fault */
977 cpu_restore_state(tb, env, pc, puc);
979 /* we restore the process signal mask as the sigreturn should
980 do it (XXX: use sigsetjmp) */
981 sigprocmask(SIG_SETMASK, old_set, NULL);
984 #elif defined (TARGET_PPC)
985 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
986 int is_write, sigset_t *old_set,
989 TranslationBlock *tb;
993 env = cpu_single_env; /* XXX: find a correct solution for multithread */
994 #if defined(DEBUG_SIGNAL)
995 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
996 pc, address, is_write, *(unsigned long *)old_set);
998 /* XXX: locking issue */
999 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1003 /* see if it is an MMU fault */
1004 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
1006 return 0; /* not an MMU fault */
1008 return 1; /* the MMU fault was handled without causing real CPU fault */
1010 /* now we have a real cpu fault */
1011 tb = tb_find_pc(pc);
1013 /* the PC is inside the translated code. It means that we have
1014 a virtual CPU fault */
1015 cpu_restore_state(tb, env, pc, puc);
1019 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1020 env->nip, env->error_code, tb);
1022 /* we restore the process signal mask as the sigreturn should
1023 do it (XXX: use sigsetjmp) */
1024 sigprocmask(SIG_SETMASK, old_set, NULL);
1025 do_raise_exception_err(env->exception_index, env->error_code);
1027 /* activate soft MMU for this block */
1028 cpu_resume_from_signal(env, puc);
1030 /* never comes here */
1034 #elif defined(TARGET_M68K)
1035 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1036 int is_write, sigset_t *old_set,
1039 TranslationBlock *tb;
1043 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1044 #if defined(DEBUG_SIGNAL)
1045 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1046 pc, address, is_write, *(unsigned long *)old_set);
1048 /* XXX: locking issue */
1049 if (is_write && page_unprotect(address, pc, puc)) {
1052 /* see if it is an MMU fault */
1053 ret = cpu_m68k_handle_mmu_fault(env, address, is_write, 1, 0);
1055 return 0; /* not an MMU fault */
1057 return 1; /* the MMU fault was handled without causing real CPU fault */
1058 /* now we have a real cpu fault */
1059 tb = tb_find_pc(pc);
1061 /* the PC is inside the translated code. It means that we have
1062 a virtual CPU fault */
1063 cpu_restore_state(tb, env, pc, puc);
1065 /* we restore the process signal mask as the sigreturn should
1066 do it (XXX: use sigsetjmp) */
1067 sigprocmask(SIG_SETMASK, old_set, NULL);
1069 /* never comes here */
1073 #elif defined (TARGET_MIPS)
1074 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1075 int is_write, sigset_t *old_set,
1078 TranslationBlock *tb;
1082 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1083 #if defined(DEBUG_SIGNAL)
1084 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1085 pc, address, is_write, *(unsigned long *)old_set);
1087 /* XXX: locking issue */
1088 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1092 /* see if it is an MMU fault */
1093 ret = cpu_mips_handle_mmu_fault(env, address, is_write, 1, 0);
1095 return 0; /* not an MMU fault */
1097 return 1; /* the MMU fault was handled without causing real CPU fault */
1099 /* now we have a real cpu fault */
1100 tb = tb_find_pc(pc);
1102 /* the PC is inside the translated code. It means that we have
1103 a virtual CPU fault */
1104 cpu_restore_state(tb, env, pc, puc);
1108 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1109 env->nip, env->error_code, tb);
1111 /* we restore the process signal mask as the sigreturn should
1112 do it (XXX: use sigsetjmp) */
1113 sigprocmask(SIG_SETMASK, old_set, NULL);
1114 do_raise_exception_err(env->exception_index, env->error_code);
1116 /* activate soft MMU for this block */
1117 cpu_resume_from_signal(env, puc);
1119 /* never comes here */
1123 #elif defined (TARGET_SH4)
1124 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1125 int is_write, sigset_t *old_set,
1128 TranslationBlock *tb;
1132 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1133 #if defined(DEBUG_SIGNAL)
1134 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1135 pc, address, is_write, *(unsigned long *)old_set);
1137 /* XXX: locking issue */
1138 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1142 /* see if it is an MMU fault */
1143 ret = cpu_sh4_handle_mmu_fault(env, address, is_write, 1, 0);
1145 return 0; /* not an MMU fault */
1147 return 1; /* the MMU fault was handled without causing real CPU fault */
1149 /* now we have a real cpu fault */
1150 tb = tb_find_pc(pc);
1152 /* the PC is inside the translated code. It means that we have
1153 a virtual CPU fault */
1154 cpu_restore_state(tb, env, pc, puc);
1157 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1158 env->nip, env->error_code, tb);
1160 /* we restore the process signal mask as the sigreturn should
1161 do it (XXX: use sigsetjmp) */
1162 sigprocmask(SIG_SETMASK, old_set, NULL);
1164 /* never comes here */
1168 #error unsupported target CPU
1171 #if defined(__i386__)
1173 #if defined(__APPLE__)
1174 # include <sys/ucontext.h>
1176 # define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
1177 # define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
1178 # define ERROR_sig(context) ((context)->uc_mcontext->es.err)
1180 # define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
1181 # define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
1182 # define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
1185 #if defined(USE_CODE_COPY)
1186 static void cpu_send_trap(unsigned long pc, int trap,
1187 struct ucontext *uc)
1189 TranslationBlock *tb;
1192 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1193 /* now we have a real cpu fault */
1194 tb = tb_find_pc(pc);
1196 /* the PC is inside the translated code. It means that we have
1197 a virtual CPU fault */
1198 cpu_restore_state(tb, env, pc, uc);
1200 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
1201 raise_exception_err(trap, env->error_code);
1205 int cpu_signal_handler(int host_signum, void *pinfo,
1208 siginfo_t *info = pinfo;
1209 struct ucontext *uc = puc;
1217 #define REG_TRAPNO TRAPNO
1220 trapno = TRAP_sig(uc);
1221 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1222 if (trapno == 0x00 || trapno == 0x05) {
1223 /* send division by zero or bound exception */
1224 cpu_send_trap(pc, trapno, uc);
1228 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1230 (ERROR_sig(uc) >> 1) & 1 : 0,
1231 &uc->uc_sigmask, puc);
1234 #elif defined(__x86_64__)
1236 int cpu_signal_handler(int host_signum, void *pinfo,
1239 siginfo_t *info = pinfo;
1240 struct ucontext *uc = puc;
1243 pc = uc->uc_mcontext.gregs[REG_RIP];
1244 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1245 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
1246 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1247 &uc->uc_sigmask, puc);
1250 #elif defined(__powerpc__)
1252 /***********************************************************************
1253 * signal context platform-specific definitions
1257 /* All Registers access - only for local access */
1258 # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
1259 /* Gpr Registers access */
1260 # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
1261 # define IAR_sig(context) REG_sig(nip, context) /* Program counter */
1262 # define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
1263 # define CTR_sig(context) REG_sig(ctr, context) /* Count register */
1264 # define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
1265 # define LR_sig(context) REG_sig(link, context) /* Link register */
1266 # define CR_sig(context) REG_sig(ccr, context) /* Condition register */
1267 /* Float Registers access */
1268 # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1269 # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1270 /* Exception Registers access */
1271 # define DAR_sig(context) REG_sig(dar, context)
1272 # define DSISR_sig(context) REG_sig(dsisr, context)
1273 # define TRAP_sig(context) REG_sig(trap, context)
1277 # include <sys/ucontext.h>
1278 typedef struct ucontext SIGCONTEXT;
1279 /* All Registers access - only for local access */
1280 # define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
1281 # define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
1282 # define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
1283 # define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
1284 /* Gpr Registers access */
1285 # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
1286 # define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
1287 # define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
1288 # define CTR_sig(context) REG_sig(ctr, context)
1289 # define XER_sig(context) REG_sig(xer, context) /* Link register */
1290 # define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
1291 # define CR_sig(context) REG_sig(cr, context) /* Condition register */
1292 /* Float Registers access */
1293 # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
1294 # define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
1295 /* Exception Registers access */
1296 # define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
1297 # define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
1298 # define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1299 #endif /* __APPLE__ */
1301 int cpu_signal_handler(int host_signum, void *pinfo,
1304 siginfo_t *info = pinfo;
1305 struct ucontext *uc = puc;
1313 if (DSISR_sig(uc) & 0x00800000)
1316 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
1319 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1320 is_write, &uc->uc_sigmask, puc);
1323 #elif defined(__alpha__)
1325 int cpu_signal_handler(int host_signum, void *pinfo,
1328 siginfo_t *info = pinfo;
1329 struct ucontext *uc = puc;
1330 uint32_t *pc = uc->uc_mcontext.sc_pc;
1331 uint32_t insn = *pc;
1334 /* XXX: need kernel patch to get write flag faster */
1335 switch (insn >> 26) {
1350 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1351 is_write, &uc->uc_sigmask, puc);
1353 #elif defined(__sparc__)
1355 int cpu_signal_handler(int host_signum, void *pinfo,
1358 siginfo_t *info = pinfo;
1359 uint32_t *regs = (uint32_t *)(info + 1);
1360 void *sigmask = (regs + 20);
1365 /* XXX: is there a standard glibc define ? */
1367 /* XXX: need kernel patch to get write flag faster */
1369 insn = *(uint32_t *)pc;
1370 if ((insn >> 30) == 3) {
1371 switch((insn >> 19) & 0x3f) {
1383 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1384 is_write, sigmask, NULL);
1387 #elif defined(__arm__)
1389 int cpu_signal_handler(int host_signum, void *pinfo,
1392 siginfo_t *info = pinfo;
1393 struct ucontext *uc = puc;
1397 pc = uc->uc_mcontext.gregs[R15];
1398 /* XXX: compute is_write */
1400 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1402 &uc->uc_sigmask, puc);
1405 #elif defined(__mc68000)
1407 int cpu_signal_handler(int host_signum, void *pinfo,
1410 siginfo_t *info = pinfo;
1411 struct ucontext *uc = puc;
1415 pc = uc->uc_mcontext.gregs[16];
1416 /* XXX: compute is_write */
1418 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1420 &uc->uc_sigmask, puc);
1423 #elif defined(__ia64)
1426 /* This ought to be in <bits/siginfo.h>... */
1427 # define __ISR_VALID 1
1430 int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
1432 siginfo_t *info = pinfo;
1433 struct ucontext *uc = puc;
1437 ip = uc->uc_mcontext.sc_ip;
1438 switch (host_signum) {
1444 if (info->si_code && (info->si_segvflags & __ISR_VALID))
1445 /* ISR.W (write-access) is bit 33: */
1446 is_write = (info->si_isr >> 33) & 1;
1452 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1454 &uc->uc_sigmask, puc);
1457 #elif defined(__s390__)
1459 int cpu_signal_handler(int host_signum, void *pinfo,
1462 siginfo_t *info = pinfo;
1463 struct ucontext *uc = puc;
1467 pc = uc->uc_mcontext.psw.addr;
1468 /* XXX: compute is_write */
1470 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1472 &uc->uc_sigmask, puc);
1477 #error host CPU specific signal handler needed
1481 #endif /* !defined(CONFIG_SOFTMMU) */