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 int saved_T0, saved_T1;
234 #if defined(TARGET_I386)
259 #elif defined(TARGET_SPARC)
260 #if defined(reg_REGWPTR)
261 uint32_t *saved_regwptr;
264 #if defined(__sparc__) && !defined(HOST_SOLARIS)
265 int saved_i7, tmp_T0;
267 int ret, interrupt_request;
268 void (*gen_func)(void);
269 TranslationBlock *tb;
272 #if defined(TARGET_I386)
273 /* handle exit of HALTED state */
274 if (env1->hflags & HF_HALTED_MASK) {
275 /* disable halt condition */
276 if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
277 (env1->eflags & IF_MASK)) {
278 env1->hflags &= ~HF_HALTED_MASK;
283 #elif defined(TARGET_PPC)
285 if (env1->msr[MSR_EE] &&
286 (env1->interrupt_request &
287 (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER))) {
293 #elif defined(TARGET_SPARC)
295 if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
296 (env1->psret != 0)) {
302 #elif defined(TARGET_ARM)
304 /* An interrupt wakes the CPU even if the I and F CPSR bits are
306 if (env1->interrupt_request
307 & (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD)) {
313 #elif defined(TARGET_MIPS)
315 if (env1->interrupt_request &
316 (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER)) {
324 cpu_single_env = env1;
326 /* first we save global registers */
334 #if defined(__sparc__) && !defined(HOST_SOLARIS)
335 /* we also save i7 because longjmp may not restore it */
336 asm volatile ("mov %%i7, %0" : "=r" (saved_i7));
339 #if defined(TARGET_I386)
366 /* put eflags in CPU temporary format */
367 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
368 DF = 1 - (2 * ((env->eflags >> 10) & 1));
369 CC_OP = CC_OP_EFLAGS;
370 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
371 #elif defined(TARGET_ARM)
372 #elif defined(TARGET_SPARC)
373 #if defined(reg_REGWPTR)
374 saved_regwptr = REGWPTR;
376 #elif defined(TARGET_PPC)
377 #elif defined(TARGET_M68K)
378 env->cc_op = CC_OP_FLAGS;
379 env->cc_dest = env->sr & 0xf;
380 env->cc_x = (env->sr >> 4) & 1;
381 #elif defined(TARGET_MIPS)
382 #elif defined(TARGET_SH4)
385 #error unsupported target CPU
387 env->exception_index = -1;
389 /* prepare setjmp context for exception handling */
391 if (setjmp(env->jmp_env) == 0) {
392 env->current_tb = NULL;
393 /* if an exception is pending, we execute it here */
394 if (env->exception_index >= 0) {
395 if (env->exception_index >= EXCP_INTERRUPT) {
396 /* exit request from the cpu execution loop */
397 ret = env->exception_index;
399 } else if (env->user_mode_only) {
400 /* if user mode only, we simulate a fake exception
401 which will be handled outside the cpu execution
403 #if defined(TARGET_I386)
404 do_interrupt_user(env->exception_index,
405 env->exception_is_int,
407 env->exception_next_eip);
409 ret = env->exception_index;
412 #if defined(TARGET_I386)
413 /* simulate a real cpu exception. On i386, it can
414 trigger new exceptions, but we do not handle
415 double or triple faults yet. */
416 do_interrupt(env->exception_index,
417 env->exception_is_int,
419 env->exception_next_eip, 0);
420 #elif defined(TARGET_PPC)
422 #elif defined(TARGET_MIPS)
424 #elif defined(TARGET_SPARC)
425 do_interrupt(env->exception_index);
426 #elif defined(TARGET_ARM)
428 #elif defined(TARGET_SH4)
432 env->exception_index = -1;
435 if (kqemu_is_ok(env) && env->interrupt_request == 0) {
437 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
438 ret = kqemu_cpu_exec(env);
439 /* put eflags in CPU temporary format */
440 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
441 DF = 1 - (2 * ((env->eflags >> 10) & 1));
442 CC_OP = CC_OP_EFLAGS;
443 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
446 longjmp(env->jmp_env, 1);
447 } else if (ret == 2) {
448 /* softmmu execution needed */
450 if (env->interrupt_request != 0) {
451 /* hardware interrupt will be executed just after */
453 /* otherwise, we restart */
454 longjmp(env->jmp_env, 1);
460 T0 = 0; /* force lookup of first TB */
462 #if defined(__sparc__) && !defined(HOST_SOLARIS)
463 /* g1 can be modified by some libc? functions */
466 interrupt_request = env->interrupt_request;
467 if (__builtin_expect(interrupt_request, 0)) {
468 #if defined(TARGET_I386)
469 if ((interrupt_request & CPU_INTERRUPT_SMI) &&
470 !(env->hflags & HF_SMM_MASK)) {
471 env->interrupt_request &= ~CPU_INTERRUPT_SMI;
473 #if defined(__sparc__) && !defined(HOST_SOLARIS)
478 } else if ((interrupt_request & CPU_INTERRUPT_HARD) &&
479 (env->eflags & IF_MASK) &&
480 !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
482 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
483 intno = cpu_get_pic_interrupt(env);
484 if (loglevel & CPU_LOG_TB_IN_ASM) {
485 fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
487 do_interrupt(intno, 0, 0, 0, 1);
488 /* ensure that no TB jump will be modified as
489 the program flow was changed */
490 #if defined(__sparc__) && !defined(HOST_SOLARIS)
496 #elif defined(TARGET_PPC)
498 if ((interrupt_request & CPU_INTERRUPT_RESET)) {
503 if ((interrupt_request & CPU_INTERRUPT_HARD)) {
505 env->exception_index = EXCP_EXTERNAL;
508 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
509 #if defined(__sparc__) && !defined(HOST_SOLARIS)
514 } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) {
516 env->exception_index = EXCP_DECR;
519 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
520 #if defined(__sparc__) && !defined(HOST_SOLARIS)
527 #elif defined(TARGET_MIPS)
528 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
529 (env->CP0_Status & (1 << CP0St_IE)) &&
530 (env->CP0_Status & env->CP0_Cause & 0x0000FF00) &&
531 !(env->hflags & MIPS_HFLAG_EXL) &&
532 !(env->hflags & MIPS_HFLAG_ERL) &&
533 !(env->hflags & MIPS_HFLAG_DM)) {
535 env->exception_index = EXCP_EXT_INTERRUPT;
538 #if defined(__sparc__) && !defined(HOST_SOLARIS)
544 #elif defined(TARGET_SPARC)
545 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
547 int pil = env->interrupt_index & 15;
548 int type = env->interrupt_index & 0xf0;
550 if (((type == TT_EXTINT) &&
551 (pil == 15 || pil > env->psrpil)) ||
553 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
554 do_interrupt(env->interrupt_index);
555 env->interrupt_index = 0;
556 #if defined(__sparc__) && !defined(HOST_SOLARIS)
562 } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
563 //do_interrupt(0, 0, 0, 0, 0);
564 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
565 } else if (interrupt_request & CPU_INTERRUPT_HALT) {
566 env->interrupt_request &= ~CPU_INTERRUPT_HALT;
568 env->exception_index = EXCP_HLT;
571 #elif defined(TARGET_ARM)
572 if (interrupt_request & CPU_INTERRUPT_FIQ
573 && !(env->uncached_cpsr & CPSR_F)) {
574 env->exception_index = EXCP_FIQ;
577 if (interrupt_request & CPU_INTERRUPT_HARD
578 && !(env->uncached_cpsr & CPSR_I)) {
579 env->exception_index = EXCP_IRQ;
582 #elif defined(TARGET_SH4)
585 /* Don't use the cached interupt_request value,
586 do_interrupt may have updated the EXITTB flag. */
587 if (env->interrupt_request & CPU_INTERRUPT_EXITTB) {
588 env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
589 /* ensure that no TB jump will be modified as
590 the program flow was changed */
591 #if defined(__sparc__) && !defined(HOST_SOLARIS)
597 if (interrupt_request & CPU_INTERRUPT_EXIT) {
598 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
599 env->exception_index = EXCP_INTERRUPT;
604 if ((loglevel & CPU_LOG_TB_CPU)) {
605 #if defined(TARGET_I386)
606 /* restore flags in standard format */
608 env->regs[R_EAX] = EAX;
611 env->regs[R_EBX] = EBX;
614 env->regs[R_ECX] = ECX;
617 env->regs[R_EDX] = EDX;
620 env->regs[R_ESI] = ESI;
623 env->regs[R_EDI] = EDI;
626 env->regs[R_EBP] = EBP;
629 env->regs[R_ESP] = ESP;
631 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
632 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
633 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
634 #elif defined(TARGET_ARM)
635 cpu_dump_state(env, logfile, fprintf, 0);
636 #elif defined(TARGET_SPARC)
637 REGWPTR = env->regbase + (env->cwp * 16);
638 env->regwptr = REGWPTR;
639 cpu_dump_state(env, logfile, fprintf, 0);
640 #elif defined(TARGET_PPC)
641 cpu_dump_state(env, logfile, fprintf, 0);
642 #elif defined(TARGET_M68K)
643 cpu_m68k_flush_flags(env, env->cc_op);
644 env->cc_op = CC_OP_FLAGS;
645 env->sr = (env->sr & 0xffe0)
646 | env->cc_dest | (env->cc_x << 4);
647 cpu_dump_state(env, logfile, fprintf, 0);
648 #elif defined(TARGET_MIPS)
649 cpu_dump_state(env, logfile, fprintf, 0);
650 #elif defined(TARGET_SH4)
651 cpu_dump_state(env, logfile, fprintf, 0);
653 #error unsupported target CPU
659 if ((loglevel & CPU_LOG_EXEC)) {
660 fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
661 (long)tb->tc_ptr, tb->pc,
662 lookup_symbol(tb->pc));
665 #if defined(__sparc__) && !defined(HOST_SOLARIS)
668 /* see if we can patch the calling TB. When the TB
669 spans two pages, we cannot safely do a direct
674 (env->kqemu_enabled != 2) &&
676 tb->page_addr[1] == -1
677 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
678 && (tb->cflags & CF_CODE_COPY) ==
679 (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
683 tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
684 #if defined(USE_CODE_COPY)
685 /* propagates the FP use info */
686 ((TranslationBlock *)(T0 & ~3))->cflags |=
687 (tb->cflags & CF_FP_USED);
689 spin_unlock(&tb_lock);
693 env->current_tb = tb;
694 /* execute the generated code */
695 gen_func = (void *)tc_ptr;
696 #if defined(__sparc__)
697 __asm__ __volatile__("call %0\n\t"
701 : "i0", "i1", "i2", "i3", "i4", "i5",
702 "l0", "l1", "l2", "l3", "l4", "l5",
704 #elif defined(__arm__)
705 asm volatile ("mov pc, %0\n\t"
706 ".global exec_loop\n\t"
710 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
711 #elif defined(TARGET_I386) && defined(USE_CODE_COPY)
713 if (!(tb->cflags & CF_CODE_COPY)) {
714 if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
715 save_native_fp_state(env);
719 if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
720 restore_native_fp_state(env);
722 /* we work with native eflags */
723 CC_SRC = cc_table[CC_OP].compute_all();
724 CC_OP = CC_OP_EFLAGS;
725 asm(".globl exec_loop\n"
730 " fs movl %11, %%eax\n"
731 " andl $0x400, %%eax\n"
732 " fs orl %8, %%eax\n"
735 " fs movl %%esp, %12\n"
736 " fs movl %0, %%eax\n"
737 " fs movl %1, %%ecx\n"
738 " fs movl %2, %%edx\n"
739 " fs movl %3, %%ebx\n"
740 " fs movl %4, %%esp\n"
741 " fs movl %5, %%ebp\n"
742 " fs movl %6, %%esi\n"
743 " fs movl %7, %%edi\n"
746 " fs movl %%esp, %4\n"
747 " fs movl %12, %%esp\n"
748 " fs movl %%eax, %0\n"
749 " fs movl %%ecx, %1\n"
750 " fs movl %%edx, %2\n"
751 " fs movl %%ebx, %3\n"
752 " fs movl %%ebp, %5\n"
753 " fs movl %%esi, %6\n"
754 " fs movl %%edi, %7\n"
757 " movl %%eax, %%ecx\n"
758 " andl $0x400, %%ecx\n"
760 " andl $0x8d5, %%eax\n"
761 " fs movl %%eax, %8\n"
763 " subl %%ecx, %%eax\n"
764 " fs movl %%eax, %11\n"
765 " fs movl %9, %%ebx\n" /* get T0 value */
768 : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
769 "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
770 "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
771 "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
772 "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
773 "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
774 "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
775 "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
776 "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
777 "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
779 "m" (*(uint8_t *)offsetof(CPUState, df)),
780 "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
785 #elif defined(__ia64)
792 fp.gp = code_gen_buffer + 2 * (1 << 20);
793 (*(void (*)(void)) &fp)();
797 env->current_tb = NULL;
798 /* reset soft MMU for next block (it can currently
799 only be set by a memory fault) */
800 #if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
801 if (env->hflags & HF_SOFTMMU_MASK) {
802 env->hflags &= ~HF_SOFTMMU_MASK;
803 /* do not allow linking to another block */
807 #if defined(USE_KQEMU)
808 #define MIN_CYCLE_BEFORE_SWITCH (100 * 1000)
809 if (kqemu_is_ok(env) &&
810 (cpu_get_time_fast() - env->last_io_time) >= MIN_CYCLE_BEFORE_SWITCH) {
821 #if defined(TARGET_I386)
822 #if defined(USE_CODE_COPY)
823 if (env->native_fp_regs) {
824 save_native_fp_state(env);
827 /* restore flags in standard format */
828 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
830 /* restore global registers */
855 #elif defined(TARGET_ARM)
856 /* XXX: Save/restore host fpu exception state?. */
857 #elif defined(TARGET_SPARC)
858 #if defined(reg_REGWPTR)
859 REGWPTR = saved_regwptr;
861 #elif defined(TARGET_PPC)
862 #elif defined(TARGET_M68K)
863 cpu_m68k_flush_flags(env, env->cc_op);
864 env->cc_op = CC_OP_FLAGS;
865 env->sr = (env->sr & 0xffe0)
866 | env->cc_dest | (env->cc_x << 4);
867 #elif defined(TARGET_MIPS)
868 #elif defined(TARGET_SH4)
871 #error unsupported target CPU
873 #if defined(__sparc__) && !defined(HOST_SOLARIS)
874 asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
882 /* fail safe : never use cpu_single_env outside cpu_exec() */
883 cpu_single_env = NULL;
887 /* must only be called from the generated code as an exception can be
889 void tb_invalidate_page_range(target_ulong start, target_ulong end)
891 /* XXX: cannot enable it yet because it yields to MMU exception
892 where NIP != read address on PowerPC */
894 target_ulong phys_addr;
895 phys_addr = get_phys_addr_code(env, start);
896 tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
900 #if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
902 void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
904 CPUX86State *saved_env;
908 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
910 cpu_x86_load_seg_cache(env, seg_reg, selector,
911 (selector << 4), 0xffff, 0);
913 load_seg(seg_reg, selector);
918 void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
920 CPUX86State *saved_env;
925 helper_fsave((target_ulong)ptr, data32);
930 void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
932 CPUX86State *saved_env;
937 helper_frstor((target_ulong)ptr, data32);
942 #endif /* TARGET_I386 */
944 #if !defined(CONFIG_SOFTMMU)
946 #if defined(TARGET_I386)
948 /* 'pc' is the host PC at which the exception was raised. 'address' is
949 the effective address of the memory exception. 'is_write' is 1 if a
950 write caused the exception and otherwise 0'. 'old_set' is the
951 signal set which should be restored */
952 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
953 int is_write, sigset_t *old_set,
956 TranslationBlock *tb;
960 env = cpu_single_env; /* XXX: find a correct solution for multithread */
961 #if defined(DEBUG_SIGNAL)
962 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
963 pc, address, is_write, *(unsigned long *)old_set);
965 /* XXX: locking issue */
966 if (is_write && page_unprotect(h2g(address), pc, puc)) {
970 /* see if it is an MMU fault */
971 ret = cpu_x86_handle_mmu_fault(env, address, is_write,
972 ((env->hflags & HF_CPL_MASK) == 3), 0);
974 return 0; /* not an MMU fault */
976 return 1; /* the MMU fault was handled without causing real CPU fault */
977 /* now we have a real cpu fault */
980 /* the PC is inside the translated code. It means that we have
981 a virtual CPU fault */
982 cpu_restore_state(tb, env, pc, puc);
986 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
987 env->eip, env->cr[2], env->error_code);
989 /* we restore the process signal mask as the sigreturn should
990 do it (XXX: use sigsetjmp) */
991 sigprocmask(SIG_SETMASK, old_set, NULL);
992 raise_exception_err(env->exception_index, env->error_code);
994 /* activate soft MMU for this block */
995 env->hflags |= HF_SOFTMMU_MASK;
996 cpu_resume_from_signal(env, puc);
998 /* never comes here */
1002 #elif defined(TARGET_ARM)
1003 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1004 int is_write, sigset_t *old_set,
1007 TranslationBlock *tb;
1011 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1012 #if defined(DEBUG_SIGNAL)
1013 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1014 pc, address, is_write, *(unsigned long *)old_set);
1016 /* XXX: locking issue */
1017 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1020 /* see if it is an MMU fault */
1021 ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
1023 return 0; /* not an MMU fault */
1025 return 1; /* the MMU fault was handled without causing real CPU fault */
1026 /* now we have a real cpu fault */
1027 tb = tb_find_pc(pc);
1029 /* the PC is inside the translated code. It means that we have
1030 a virtual CPU fault */
1031 cpu_restore_state(tb, env, pc, puc);
1033 /* we restore the process signal mask as the sigreturn should
1034 do it (XXX: use sigsetjmp) */
1035 sigprocmask(SIG_SETMASK, old_set, NULL);
1038 #elif defined(TARGET_SPARC)
1039 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1040 int is_write, sigset_t *old_set,
1043 TranslationBlock *tb;
1047 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1048 #if defined(DEBUG_SIGNAL)
1049 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1050 pc, address, is_write, *(unsigned long *)old_set);
1052 /* XXX: locking issue */
1053 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1056 /* see if it is an MMU fault */
1057 ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
1059 return 0; /* not an MMU fault */
1061 return 1; /* the MMU fault was handled without causing real CPU fault */
1062 /* now we have a real cpu fault */
1063 tb = tb_find_pc(pc);
1065 /* the PC is inside the translated code. It means that we have
1066 a virtual CPU fault */
1067 cpu_restore_state(tb, env, pc, puc);
1069 /* we restore the process signal mask as the sigreturn should
1070 do it (XXX: use sigsetjmp) */
1071 sigprocmask(SIG_SETMASK, old_set, NULL);
1074 #elif defined (TARGET_PPC)
1075 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1076 int is_write, sigset_t *old_set,
1079 TranslationBlock *tb;
1083 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1084 #if defined(DEBUG_SIGNAL)
1085 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1086 pc, address, is_write, *(unsigned long *)old_set);
1088 /* XXX: locking issue */
1089 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1093 /* see if it is an MMU fault */
1094 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
1096 return 0; /* not an MMU fault */
1098 return 1; /* the MMU fault was handled without causing real CPU fault */
1100 /* now we have a real cpu fault */
1101 tb = tb_find_pc(pc);
1103 /* the PC is inside the translated code. It means that we have
1104 a virtual CPU fault */
1105 cpu_restore_state(tb, env, pc, puc);
1109 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1110 env->nip, env->error_code, tb);
1112 /* we restore the process signal mask as the sigreturn should
1113 do it (XXX: use sigsetjmp) */
1114 sigprocmask(SIG_SETMASK, old_set, NULL);
1115 do_raise_exception_err(env->exception_index, env->error_code);
1117 /* activate soft MMU for this block */
1118 cpu_resume_from_signal(env, puc);
1120 /* never comes here */
1124 #elif defined(TARGET_M68K)
1125 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1126 int is_write, sigset_t *old_set,
1129 TranslationBlock *tb;
1133 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1134 #if defined(DEBUG_SIGNAL)
1135 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1136 pc, address, is_write, *(unsigned long *)old_set);
1138 /* XXX: locking issue */
1139 if (is_write && page_unprotect(address, pc, puc)) {
1142 /* see if it is an MMU fault */
1143 ret = cpu_m68k_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 */
1148 /* now we have a real cpu fault */
1149 tb = tb_find_pc(pc);
1151 /* the PC is inside the translated code. It means that we have
1152 a virtual CPU fault */
1153 cpu_restore_state(tb, env, pc, puc);
1155 /* we restore the process signal mask as the sigreturn should
1156 do it (XXX: use sigsetjmp) */
1157 sigprocmask(SIG_SETMASK, old_set, NULL);
1159 /* never comes here */
1163 #elif defined (TARGET_MIPS)
1164 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1165 int is_write, sigset_t *old_set,
1168 TranslationBlock *tb;
1172 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1173 #if defined(DEBUG_SIGNAL)
1174 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1175 pc, address, is_write, *(unsigned long *)old_set);
1177 /* XXX: locking issue */
1178 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1182 /* see if it is an MMU fault */
1183 ret = cpu_mips_handle_mmu_fault(env, address, is_write, 1, 0);
1185 return 0; /* not an MMU fault */
1187 return 1; /* the MMU fault was handled without causing real CPU fault */
1189 /* now we have a real cpu fault */
1190 tb = tb_find_pc(pc);
1192 /* the PC is inside the translated code. It means that we have
1193 a virtual CPU fault */
1194 cpu_restore_state(tb, env, pc, puc);
1198 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1199 env->nip, env->error_code, tb);
1201 /* we restore the process signal mask as the sigreturn should
1202 do it (XXX: use sigsetjmp) */
1203 sigprocmask(SIG_SETMASK, old_set, NULL);
1204 do_raise_exception_err(env->exception_index, env->error_code);
1206 /* activate soft MMU for this block */
1207 cpu_resume_from_signal(env, puc);
1209 /* never comes here */
1213 #elif defined (TARGET_SH4)
1214 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1215 int is_write, sigset_t *old_set,
1218 TranslationBlock *tb;
1222 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1223 #if defined(DEBUG_SIGNAL)
1224 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1225 pc, address, is_write, *(unsigned long *)old_set);
1227 /* XXX: locking issue */
1228 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1232 /* see if it is an MMU fault */
1233 ret = cpu_sh4_handle_mmu_fault(env, address, is_write, 1, 0);
1235 return 0; /* not an MMU fault */
1237 return 1; /* the MMU fault was handled without causing real CPU fault */
1239 /* now we have a real cpu fault */
1240 tb = tb_find_pc(pc);
1242 /* the PC is inside the translated code. It means that we have
1243 a virtual CPU fault */
1244 cpu_restore_state(tb, env, pc, puc);
1247 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1248 env->nip, env->error_code, tb);
1250 /* we restore the process signal mask as the sigreturn should
1251 do it (XXX: use sigsetjmp) */
1252 sigprocmask(SIG_SETMASK, old_set, NULL);
1254 /* never comes here */
1258 #error unsupported target CPU
1261 #if defined(__i386__)
1263 #if defined(USE_CODE_COPY)
1264 static void cpu_send_trap(unsigned long pc, int trap,
1265 struct ucontext *uc)
1267 TranslationBlock *tb;
1270 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1271 /* now we have a real cpu fault */
1272 tb = tb_find_pc(pc);
1274 /* the PC is inside the translated code. It means that we have
1275 a virtual CPU fault */
1276 cpu_restore_state(tb, env, pc, uc);
1278 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
1279 raise_exception_err(trap, env->error_code);
1283 int cpu_signal_handler(int host_signum, void *pinfo,
1286 siginfo_t *info = pinfo;
1287 struct ucontext *uc = puc;
1295 #define REG_TRAPNO TRAPNO
1297 pc = uc->uc_mcontext.gregs[REG_EIP];
1298 trapno = uc->uc_mcontext.gregs[REG_TRAPNO];
1299 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1300 if (trapno == 0x00 || trapno == 0x05) {
1301 /* send division by zero or bound exception */
1302 cpu_send_trap(pc, trapno, uc);
1306 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1308 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1309 &uc->uc_sigmask, puc);
1312 #elif defined(__x86_64__)
1314 int cpu_signal_handler(int host_signum, void *pinfo,
1317 siginfo_t *info = pinfo;
1318 struct ucontext *uc = puc;
1321 pc = uc->uc_mcontext.gregs[REG_RIP];
1322 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1323 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
1324 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1325 &uc->uc_sigmask, puc);
1328 #elif defined(__powerpc__)
1330 /***********************************************************************
1331 * signal context platform-specific definitions
1335 /* All Registers access - only for local access */
1336 # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
1337 /* Gpr Registers access */
1338 # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
1339 # define IAR_sig(context) REG_sig(nip, context) /* Program counter */
1340 # define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
1341 # define CTR_sig(context) REG_sig(ctr, context) /* Count register */
1342 # define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
1343 # define LR_sig(context) REG_sig(link, context) /* Link register */
1344 # define CR_sig(context) REG_sig(ccr, context) /* Condition register */
1345 /* Float Registers access */
1346 # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1347 # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1348 /* Exception Registers access */
1349 # define DAR_sig(context) REG_sig(dar, context)
1350 # define DSISR_sig(context) REG_sig(dsisr, context)
1351 # define TRAP_sig(context) REG_sig(trap, context)
1355 # include <sys/ucontext.h>
1356 typedef struct ucontext SIGCONTEXT;
1357 /* All Registers access - only for local access */
1358 # define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
1359 # define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
1360 # define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
1361 # define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
1362 /* Gpr Registers access */
1363 # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
1364 # define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
1365 # define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
1366 # define CTR_sig(context) REG_sig(ctr, context)
1367 # define XER_sig(context) REG_sig(xer, context) /* Link register */
1368 # define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
1369 # define CR_sig(context) REG_sig(cr, context) /* Condition register */
1370 /* Float Registers access */
1371 # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
1372 # define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
1373 /* Exception Registers access */
1374 # define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
1375 # define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
1376 # define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1377 #endif /* __APPLE__ */
1379 int cpu_signal_handler(int host_signum, void *pinfo,
1382 siginfo_t *info = pinfo;
1383 struct ucontext *uc = puc;
1391 if (DSISR_sig(uc) & 0x00800000)
1394 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
1397 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1398 is_write, &uc->uc_sigmask, puc);
1401 #elif defined(__alpha__)
1403 int cpu_signal_handler(int host_signum, void *pinfo,
1406 siginfo_t *info = pinfo;
1407 struct ucontext *uc = puc;
1408 uint32_t *pc = uc->uc_mcontext.sc_pc;
1409 uint32_t insn = *pc;
1412 /* XXX: need kernel patch to get write flag faster */
1413 switch (insn >> 26) {
1428 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1429 is_write, &uc->uc_sigmask, puc);
1431 #elif defined(__sparc__)
1433 int cpu_signal_handler(int host_signum, void *pinfo,
1436 siginfo_t *info = pinfo;
1437 uint32_t *regs = (uint32_t *)(info + 1);
1438 void *sigmask = (regs + 20);
1443 /* XXX: is there a standard glibc define ? */
1445 /* XXX: need kernel patch to get write flag faster */
1447 insn = *(uint32_t *)pc;
1448 if ((insn >> 30) == 3) {
1449 switch((insn >> 19) & 0x3f) {
1461 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1462 is_write, sigmask, NULL);
1465 #elif defined(__arm__)
1467 int cpu_signal_handler(int host_signum, void *pinfo,
1470 siginfo_t *info = pinfo;
1471 struct ucontext *uc = puc;
1475 pc = uc->uc_mcontext.gregs[R15];
1476 /* XXX: compute is_write */
1478 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1480 &uc->uc_sigmask, puc);
1483 #elif defined(__mc68000)
1485 int cpu_signal_handler(int host_signum, void *pinfo,
1488 siginfo_t *info = pinfo;
1489 struct ucontext *uc = puc;
1493 pc = uc->uc_mcontext.gregs[16];
1494 /* XXX: compute is_write */
1496 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1498 &uc->uc_sigmask, puc);
1501 #elif defined(__ia64)
1504 /* This ought to be in <bits/siginfo.h>... */
1505 # define __ISR_VALID 1
1508 int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
1510 siginfo_t *info = pinfo;
1511 struct ucontext *uc = puc;
1515 ip = uc->uc_mcontext.sc_ip;
1516 switch (host_signum) {
1522 if (info->si_code && (info->si_segvflags & __ISR_VALID))
1523 /* ISR.W (write-access) is bit 33: */
1524 is_write = (info->si_isr >> 33) & 1;
1530 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1532 &uc->uc_sigmask, puc);
1535 #elif defined(__s390__)
1537 int cpu_signal_handler(int host_signum, void *pinfo,
1540 siginfo_t *info = pinfo;
1541 struct ucontext *uc = puc;
1545 pc = uc->uc_mcontext.psw.addr;
1546 /* XXX: compute is_write */
1548 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1550 &uc->uc_sigmask, puc);
1555 #error host CPU specific signal handler needed
1559 #endif /* !defined(CONFIG_SOFTMMU) */