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)
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))
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_SH4)
198 flags
= env
->sr
& (SR_MD
| SR_RB
);
199 cs_base
= 0; /* XXXXX */
202 #error unsupported CPU
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
);
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 */
221 /* main execution loop */
223 int cpu_exec(CPUState
*env1
)
225 int saved_T0
, saved_T1
;
230 #if defined(TARGET_I386)
255 #elif defined(TARGET_SPARC)
256 #if defined(reg_REGWPTR)
257 uint32_t *saved_regwptr
;
260 #if defined(__sparc__) && !defined(HOST_SOLARIS)
261 int saved_i7
, tmp_T0
;
263 int ret
, interrupt_request
;
264 void (*gen_func
)(void);
265 TranslationBlock
*tb
;
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
;
279 #elif defined(TARGET_PPC)
281 if (env1
->msr
[MSR_EE
] &&
282 (env1
->interrupt_request
&
283 (CPU_INTERRUPT_HARD
| CPU_INTERRUPT_TIMER
))) {
289 #elif defined(TARGET_SPARC)
291 if ((env1
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
292 (env1
->psret
!= 0)) {
298 #elif defined(TARGET_ARM)
300 /* An interrupt wakes the CPU even if the I and F CPSR bits are
302 if (env1
->interrupt_request
303 & (CPU_INTERRUPT_FIQ
| CPU_INTERRUPT_HARD
)) {
309 #elif defined(TARGET_MIPS)
311 if (env1
->interrupt_request
&
312 (CPU_INTERRUPT_HARD
| CPU_INTERRUPT_TIMER
)) {
320 cpu_single_env
= env1
;
322 /* first we save global registers */
330 #if defined(__sparc__) && !defined(HOST_SOLARIS)
331 /* we also save i7 because longjmp may not restore it */
332 asm volatile ("mov %%i7, %0" : "=r" (saved_i7
));
335 #if defined(TARGET_I386)
362 /* put eflags in CPU temporary format */
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));
365 CC_OP
= CC_OP_EFLAGS
;
366 env
->eflags
&= ~(DF_MASK
| CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
367 #elif defined(TARGET_ARM)
368 #elif defined(TARGET_SPARC)
369 #if defined(reg_REGWPTR)
370 saved_regwptr
= REGWPTR
;
372 #elif defined(TARGET_PPC)
373 #elif defined(TARGET_MIPS)
374 #elif defined(TARGET_SH4)
377 #error unsupported target CPU
379 env
->exception_index
= -1;
381 /* prepare setjmp context for exception handling */
383 if (setjmp(env
->jmp_env
) == 0) {
384 env
->current_tb
= NULL
;
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
;
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
395 #if defined(TARGET_I386)
396 do_interrupt_user(env
->exception_index
,
397 env
->exception_is_int
,
399 env
->exception_next_eip
);
401 ret
= env
->exception_index
;
404 #if defined(TARGET_I386)
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
,
411 env
->exception_next_eip
, 0);
412 #elif defined(TARGET_PPC)
414 #elif defined(TARGET_MIPS)
416 #elif defined(TARGET_SPARC)
417 do_interrupt(env
->exception_index
);
418 #elif defined(TARGET_ARM)
420 #elif defined(TARGET_SH4)
424 env
->exception_index
= -1;
427 if (kqemu_is_ok(env
) && env
->interrupt_request
== 0) {
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
);
438 longjmp(env
->jmp_env
, 1);
439 } else if (ret
== 2) {
440 /* softmmu execution needed */
442 if (env
->interrupt_request
!= 0) {
443 /* hardware interrupt will be executed just after */
445 /* otherwise, we restart */
446 longjmp(env
->jmp_env
, 1);
452 T0
= 0; /* force lookup of first TB */
454 #if defined(__sparc__) && !defined(HOST_SOLARIS)
455 /* g1 can be modified by some libc? functions */
458 interrupt_request
= env
->interrupt_request
;
459 if (__builtin_expect(interrupt_request
, 0)) {
460 #if defined(TARGET_I386)
461 /* if hardware interrupt pending, we execute it */
462 if ((interrupt_request
& CPU_INTERRUPT_HARD
) &&
463 (env
->eflags
& IF_MASK
) &&
464 !(env
->hflags
& HF_INHIBIT_IRQ_MASK
)) {
466 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
467 intno
= cpu_get_pic_interrupt(env
);
468 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
469 fprintf(logfile
, "Servicing hardware INT=0x%02x\n", intno
);
471 do_interrupt(intno
, 0, 0, 0, 1);
472 /* ensure that no TB jump will be modified as
473 the program flow was changed */
474 #if defined(__sparc__) && !defined(HOST_SOLARIS)
480 #elif defined(TARGET_PPC)
482 if ((interrupt_request
& CPU_INTERRUPT_RESET
)) {
487 if ((interrupt_request
& CPU_INTERRUPT_HARD
)) {
489 env
->exception_index
= EXCP_EXTERNAL
;
492 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
493 #if defined(__sparc__) && !defined(HOST_SOLARIS)
498 } else if ((interrupt_request
& CPU_INTERRUPT_TIMER
)) {
500 env
->exception_index
= EXCP_DECR
;
503 env
->interrupt_request
&= ~CPU_INTERRUPT_TIMER
;
504 #if defined(__sparc__) && !defined(HOST_SOLARIS)
511 #elif defined(TARGET_MIPS)
512 if ((interrupt_request
& CPU_INTERRUPT_HARD
) &&
513 (env
->CP0_Status
& (1 << CP0St_IE
)) &&
514 (env
->CP0_Status
& env
->CP0_Cause
& 0x0000FF00) &&
515 !(env
->hflags
& MIPS_HFLAG_EXL
) &&
516 !(env
->hflags
& MIPS_HFLAG_ERL
) &&
517 !(env
->hflags
& MIPS_HFLAG_DM
)) {
519 env
->exception_index
= EXCP_EXT_INTERRUPT
;
522 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
523 #if defined(__sparc__) && !defined(HOST_SOLARIS)
529 #elif defined(TARGET_SPARC)
530 if ((interrupt_request
& CPU_INTERRUPT_HARD
) &&
532 int pil
= env
->interrupt_index
& 15;
533 int type
= env
->interrupt_index
& 0xf0;
535 if (((type
== TT_EXTINT
) &&
536 (pil
== 15 || pil
> env
->psrpil
)) ||
538 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
539 do_interrupt(env
->interrupt_index
);
540 env
->interrupt_index
= 0;
541 #if defined(__sparc__) && !defined(HOST_SOLARIS)
547 } else if (interrupt_request
& CPU_INTERRUPT_TIMER
) {
548 //do_interrupt(0, 0, 0, 0, 0);
549 env
->interrupt_request
&= ~CPU_INTERRUPT_TIMER
;
550 } else if (interrupt_request
& CPU_INTERRUPT_HALT
) {
551 env
->interrupt_request
&= ~CPU_INTERRUPT_HALT
;
553 env
->exception_index
= EXCP_HLT
;
556 #elif defined(TARGET_ARM)
557 if (interrupt_request
& CPU_INTERRUPT_FIQ
558 && !(env
->uncached_cpsr
& CPSR_F
)) {
559 env
->exception_index
= EXCP_FIQ
;
562 if (interrupt_request
& CPU_INTERRUPT_HARD
563 && !(env
->uncached_cpsr
& CPSR_I
)) {
564 env
->exception_index
= EXCP_IRQ
;
567 #elif defined(TARGET_SH4)
570 /* Don't use the cached interupt_request value,
571 do_interrupt may have updated the EXITTB flag. */
572 if (env
->interrupt_request
& CPU_INTERRUPT_EXITTB
) {
573 env
->interrupt_request
&= ~CPU_INTERRUPT_EXITTB
;
574 /* ensure that no TB jump will be modified as
575 the program flow was changed */
576 #if defined(__sparc__) && !defined(HOST_SOLARIS)
582 if (interrupt_request
& CPU_INTERRUPT_EXIT
) {
583 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
584 env
->exception_index
= EXCP_INTERRUPT
;
589 if ((loglevel
& CPU_LOG_TB_CPU
)) {
590 #if defined(TARGET_I386)
591 /* restore flags in standard format */
593 env
->regs
[R_EAX
] = EAX
;
596 env
->regs
[R_EBX
] = EBX
;
599 env
->regs
[R_ECX
] = ECX
;
602 env
->regs
[R_EDX
] = EDX
;
605 env
->regs
[R_ESI
] = ESI
;
608 env
->regs
[R_EDI
] = EDI
;
611 env
->regs
[R_EBP
] = EBP
;
614 env
->regs
[R_ESP
] = ESP
;
616 env
->eflags
= env
->eflags
| cc_table
[CC_OP
].compute_all() | (DF
& DF_MASK
);
617 cpu_dump_state(env
, logfile
, fprintf
, X86_DUMP_CCOP
);
618 env
->eflags
&= ~(DF_MASK
| CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
619 #elif defined(TARGET_ARM)
620 cpu_dump_state(env
, logfile
, fprintf
, 0);
621 #elif defined(TARGET_SPARC)
622 REGWPTR
= env
->regbase
+ (env
->cwp
* 16);
623 env
->regwptr
= REGWPTR
;
624 cpu_dump_state(env
, logfile
, fprintf
, 0);
625 #elif defined(TARGET_PPC)
626 cpu_dump_state(env
, logfile
, fprintf
, 0);
627 #elif defined(TARGET_MIPS)
628 cpu_dump_state(env
, logfile
, fprintf
, 0);
629 #elif defined(TARGET_SH4)
630 cpu_dump_state(env
, logfile
, fprintf
, 0);
632 #error unsupported target CPU
638 if ((loglevel
& CPU_LOG_EXEC
)) {
639 fprintf(logfile
, "Trace 0x%08lx [" TARGET_FMT_lx
"] %s\n",
640 (long)tb
->tc_ptr
, tb
->pc
,
641 lookup_symbol(tb
->pc
));
644 #if defined(__sparc__) && !defined(HOST_SOLARIS)
647 /* see if we can patch the calling TB. When the TB
648 spans two pages, we cannot safely do a direct
653 (env
->kqemu_enabled
!= 2) &&
655 tb
->page_addr
[1] == -1
656 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
657 && (tb
->cflags
& CF_CODE_COPY
) ==
658 (((TranslationBlock
*)(T0
& ~3))->cflags
& CF_CODE_COPY
)
662 tb_add_jump((TranslationBlock
*)(long)(T0
& ~3), T0
& 3, tb
);
663 #if defined(USE_CODE_COPY)
664 /* propagates the FP use info */
665 ((TranslationBlock
*)(T0
& ~3))->cflags
|=
666 (tb
->cflags
& CF_FP_USED
);
668 spin_unlock(&tb_lock
);
672 env
->current_tb
= tb
;
673 /* execute the generated code */
674 gen_func
= (void *)tc_ptr
;
675 #if defined(__sparc__)
676 __asm__
__volatile__("call %0\n\t"
680 : "i0", "i1", "i2", "i3", "i4", "i5",
681 "l0", "l1", "l2", "l3", "l4", "l5",
683 #elif defined(__arm__)
684 asm volatile ("mov pc, %0\n\t"
685 ".global exec_loop\n\t"
689 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
690 #elif defined(TARGET_I386) && defined(USE_CODE_COPY)
692 if (!(tb
->cflags
& CF_CODE_COPY
)) {
693 if ((tb
->cflags
& CF_FP_USED
) && env
->native_fp_regs
) {
694 save_native_fp_state(env
);
698 if ((tb
->cflags
& CF_FP_USED
) && !env
->native_fp_regs
) {
699 restore_native_fp_state(env
);
701 /* we work with native eflags */
702 CC_SRC
= cc_table
[CC_OP
].compute_all();
703 CC_OP
= CC_OP_EFLAGS
;
704 asm(".globl exec_loop\n"
709 " fs movl %11, %%eax\n"
710 " andl $0x400, %%eax\n"
711 " fs orl %8, %%eax\n"
714 " fs movl %%esp, %12\n"
715 " fs movl %0, %%eax\n"
716 " fs movl %1, %%ecx\n"
717 " fs movl %2, %%edx\n"
718 " fs movl %3, %%ebx\n"
719 " fs movl %4, %%esp\n"
720 " fs movl %5, %%ebp\n"
721 " fs movl %6, %%esi\n"
722 " fs movl %7, %%edi\n"
725 " fs movl %%esp, %4\n"
726 " fs movl %12, %%esp\n"
727 " fs movl %%eax, %0\n"
728 " fs movl %%ecx, %1\n"
729 " fs movl %%edx, %2\n"
730 " fs movl %%ebx, %3\n"
731 " fs movl %%ebp, %5\n"
732 " fs movl %%esi, %6\n"
733 " fs movl %%edi, %7\n"
736 " movl %%eax, %%ecx\n"
737 " andl $0x400, %%ecx\n"
739 " andl $0x8d5, %%eax\n"
740 " fs movl %%eax, %8\n"
742 " subl %%ecx, %%eax\n"
743 " fs movl %%eax, %11\n"
744 " fs movl %9, %%ebx\n" /* get T0 value */
747 : "m" (*(uint8_t *)offsetof(CPUState
, regs
[0])),
748 "m" (*(uint8_t *)offsetof(CPUState
, regs
[1])),
749 "m" (*(uint8_t *)offsetof(CPUState
, regs
[2])),
750 "m" (*(uint8_t *)offsetof(CPUState
, regs
[3])),
751 "m" (*(uint8_t *)offsetof(CPUState
, regs
[4])),
752 "m" (*(uint8_t *)offsetof(CPUState
, regs
[5])),
753 "m" (*(uint8_t *)offsetof(CPUState
, regs
[6])),
754 "m" (*(uint8_t *)offsetof(CPUState
, regs
[7])),
755 "m" (*(uint8_t *)offsetof(CPUState
, cc_src
)),
756 "m" (*(uint8_t *)offsetof(CPUState
, tmp0
)),
758 "m" (*(uint8_t *)offsetof(CPUState
, df
)),
759 "m" (*(uint8_t *)offsetof(CPUState
, saved_esp
))
764 #elif defined(__ia64)
771 fp
.gp
= code_gen_buffer
+ 2 * (1 << 20);
772 (*(void (*)(void)) &fp
)();
776 env
->current_tb
= NULL
;
777 /* reset soft MMU for next block (it can currently
778 only be set by a memory fault) */
779 #if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
780 if (env
->hflags
& HF_SOFTMMU_MASK
) {
781 env
->hflags
&= ~HF_SOFTMMU_MASK
;
782 /* do not allow linking to another block */
786 #if defined(USE_KQEMU)
787 #define MIN_CYCLE_BEFORE_SWITCH (100 * 1000)
788 if (kqemu_is_ok(env
) &&
789 (cpu_get_time_fast() - env
->last_io_time
) >= MIN_CYCLE_BEFORE_SWITCH
) {
800 #if defined(TARGET_I386)
801 #if defined(USE_CODE_COPY)
802 if (env
->native_fp_regs
) {
803 save_native_fp_state(env
);
806 /* restore flags in standard format */
807 env
->eflags
= env
->eflags
| cc_table
[CC_OP
].compute_all() | (DF
& DF_MASK
);
809 /* restore global registers */
834 #elif defined(TARGET_ARM)
835 /* XXX: Save/restore host fpu exception state?. */
836 #elif defined(TARGET_SPARC)
837 #if defined(reg_REGWPTR)
838 REGWPTR
= saved_regwptr
;
840 #elif defined(TARGET_PPC)
841 #elif defined(TARGET_MIPS)
842 #elif defined(TARGET_SH4)
845 #error unsupported target CPU
847 #if defined(__sparc__) && !defined(HOST_SOLARIS)
848 asm volatile ("mov %0, %%i7" : : "r" (saved_i7
));
856 /* fail safe : never use cpu_single_env outside cpu_exec() */
857 cpu_single_env
= NULL
;
861 /* must only be called from the generated code as an exception can be
863 void tb_invalidate_page_range(target_ulong start
, target_ulong end
)
865 /* XXX: cannot enable it yet because it yields to MMU exception
866 where NIP != read address on PowerPC */
868 target_ulong phys_addr
;
869 phys_addr
= get_phys_addr_code(env
, start
);
870 tb_invalidate_phys_page_range(phys_addr
, phys_addr
+ end
- start
, 0);
874 #if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
876 void cpu_x86_load_seg(CPUX86State
*s
, int seg_reg
, int selector
)
878 CPUX86State
*saved_env
;
882 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
884 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
885 (selector
<< 4), 0xffff, 0);
887 load_seg(seg_reg
, selector
);
892 void cpu_x86_fsave(CPUX86State
*s
, uint8_t *ptr
, int data32
)
894 CPUX86State
*saved_env
;
899 helper_fsave((target_ulong
)ptr
, data32
);
904 void cpu_x86_frstor(CPUX86State
*s
, uint8_t *ptr
, int data32
)
906 CPUX86State
*saved_env
;
911 helper_frstor((target_ulong
)ptr
, data32
);
916 #endif /* TARGET_I386 */
918 #if !defined(CONFIG_SOFTMMU)
920 #if defined(TARGET_I386)
922 /* 'pc' is the host PC at which the exception was raised. 'address' is
923 the effective address of the memory exception. 'is_write' is 1 if a
924 write caused the exception and otherwise 0'. 'old_set' is the
925 signal set which should be restored */
926 static inline int handle_cpu_signal(unsigned long pc
, unsigned long address
,
927 int is_write
, sigset_t
*old_set
,
930 TranslationBlock
*tb
;
934 env
= cpu_single_env
; /* XXX: find a correct solution for multithread */
935 #if defined(DEBUG_SIGNAL)
936 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
937 pc
, address
, is_write
, *(unsigned long *)old_set
);
939 /* XXX: locking issue */
940 if (is_write
&& page_unprotect(h2g(address
), pc
, puc
)) {
944 /* see if it is an MMU fault */
945 ret
= cpu_x86_handle_mmu_fault(env
, address
, is_write
,
946 ((env
->hflags
& HF_CPL_MASK
) == 3), 0);
948 return 0; /* not an MMU fault */
950 return 1; /* the MMU fault was handled without causing real CPU fault */
951 /* now we have a real cpu fault */
954 /* the PC is inside the translated code. It means that we have
955 a virtual CPU fault */
956 cpu_restore_state(tb
, env
, pc
, puc
);
960 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
961 env
->eip
, env
->cr
[2], env
->error_code
);
963 /* we restore the process signal mask as the sigreturn should
964 do it (XXX: use sigsetjmp) */
965 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
966 raise_exception_err(env
->exception_index
, env
->error_code
);
968 /* activate soft MMU for this block */
969 env
->hflags
|= HF_SOFTMMU_MASK
;
970 cpu_resume_from_signal(env
, puc
);
972 /* never comes here */
976 #elif defined(TARGET_ARM)
977 static inline int handle_cpu_signal(unsigned long pc
, unsigned long address
,
978 int is_write
, sigset_t
*old_set
,
981 TranslationBlock
*tb
;
985 env
= cpu_single_env
; /* XXX: find a correct solution for multithread */
986 #if defined(DEBUG_SIGNAL)
987 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
988 pc
, address
, is_write
, *(unsigned long *)old_set
);
990 /* XXX: locking issue */
991 if (is_write
&& page_unprotect(h2g(address
), pc
, puc
)) {
994 /* see if it is an MMU fault */
995 ret
= cpu_arm_handle_mmu_fault(env
, address
, is_write
, 1, 0);
997 return 0; /* not an MMU fault */
999 return 1; /* the MMU fault was handled without causing real CPU fault */
1000 /* now we have a real cpu fault */
1001 tb
= tb_find_pc(pc
);
1003 /* the PC is inside the translated code. It means that we have
1004 a virtual CPU fault */
1005 cpu_restore_state(tb
, env
, pc
, puc
);
1007 /* we restore the process signal mask as the sigreturn should
1008 do it (XXX: use sigsetjmp) */
1009 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
1012 #elif defined(TARGET_SPARC)
1013 static inline int handle_cpu_signal(unsigned long pc
, unsigned long address
,
1014 int is_write
, sigset_t
*old_set
,
1017 TranslationBlock
*tb
;
1021 env
= cpu_single_env
; /* XXX: find a correct solution for multithread */
1022 #if defined(DEBUG_SIGNAL)
1023 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1024 pc
, address
, is_write
, *(unsigned long *)old_set
);
1026 /* XXX: locking issue */
1027 if (is_write
&& page_unprotect(h2g(address
), pc
, puc
)) {
1030 /* see if it is an MMU fault */
1031 ret
= cpu_sparc_handle_mmu_fault(env
, address
, is_write
, 1, 0);
1033 return 0; /* not an MMU fault */
1035 return 1; /* the MMU fault was handled without causing real CPU fault */
1036 /* now we have a real cpu fault */
1037 tb
= tb_find_pc(pc
);
1039 /* the PC is inside the translated code. It means that we have
1040 a virtual CPU fault */
1041 cpu_restore_state(tb
, env
, pc
, puc
);
1043 /* we restore the process signal mask as the sigreturn should
1044 do it (XXX: use sigsetjmp) */
1045 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
1048 #elif defined (TARGET_PPC)
1049 static inline int handle_cpu_signal(unsigned long pc
, unsigned long address
,
1050 int is_write
, sigset_t
*old_set
,
1053 TranslationBlock
*tb
;
1057 env
= cpu_single_env
; /* XXX: find a correct solution for multithread */
1058 #if defined(DEBUG_SIGNAL)
1059 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1060 pc
, address
, is_write
, *(unsigned long *)old_set
);
1062 /* XXX: locking issue */
1063 if (is_write
&& page_unprotect(h2g(address
), pc
, puc
)) {
1067 /* see if it is an MMU fault */
1068 ret
= cpu_ppc_handle_mmu_fault(env
, address
, is_write
, msr_pr
, 0);
1070 return 0; /* not an MMU fault */
1072 return 1; /* the MMU fault was handled without causing real CPU fault */
1074 /* now we have a real cpu fault */
1075 tb
= tb_find_pc(pc
);
1077 /* the PC is inside the translated code. It means that we have
1078 a virtual CPU fault */
1079 cpu_restore_state(tb
, env
, pc
, puc
);
1083 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1084 env
->nip
, env
->error_code
, tb
);
1086 /* we restore the process signal mask as the sigreturn should
1087 do it (XXX: use sigsetjmp) */
1088 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
1089 do_raise_exception_err(env
->exception_index
, env
->error_code
);
1091 /* activate soft MMU for this block */
1092 cpu_resume_from_signal(env
, puc
);
1094 /* never comes here */
1098 #elif defined (TARGET_MIPS)
1099 static inline int handle_cpu_signal(unsigned long pc
, unsigned long address
,
1100 int is_write
, sigset_t
*old_set
,
1103 TranslationBlock
*tb
;
1107 env
= cpu_single_env
; /* XXX: find a correct solution for multithread */
1108 #if defined(DEBUG_SIGNAL)
1109 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1110 pc
, address
, is_write
, *(unsigned long *)old_set
);
1112 /* XXX: locking issue */
1113 if (is_write
&& page_unprotect(h2g(address
), pc
, puc
)) {
1117 /* see if it is an MMU fault */
1118 ret
= cpu_mips_handle_mmu_fault(env
, address
, is_write
, 1, 0);
1120 return 0; /* not an MMU fault */
1122 return 1; /* the MMU fault was handled without causing real CPU fault */
1124 /* now we have a real cpu fault */
1125 tb
= tb_find_pc(pc
);
1127 /* the PC is inside the translated code. It means that we have
1128 a virtual CPU fault */
1129 cpu_restore_state(tb
, env
, pc
, puc
);
1133 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1134 env
->nip
, env
->error_code
, tb
);
1136 /* we restore the process signal mask as the sigreturn should
1137 do it (XXX: use sigsetjmp) */
1138 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
1139 do_raise_exception_err(env
->exception_index
, env
->error_code
);
1141 /* activate soft MMU for this block */
1142 cpu_resume_from_signal(env
, puc
);
1144 /* never comes here */
1148 #elif defined (TARGET_SH4)
1149 static inline int handle_cpu_signal(unsigned long pc
, unsigned long address
,
1150 int is_write
, sigset_t
*old_set
,
1153 TranslationBlock
*tb
;
1157 env
= cpu_single_env
; /* XXX: find a correct solution for multithread */
1158 #if defined(DEBUG_SIGNAL)
1159 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1160 pc
, address
, is_write
, *(unsigned long *)old_set
);
1162 /* XXX: locking issue */
1163 if (is_write
&& page_unprotect(h2g(address
), pc
, puc
)) {
1167 /* see if it is an MMU fault */
1168 ret
= cpu_sh4_handle_mmu_fault(env
, address
, is_write
, 1, 0);
1170 return 0; /* not an MMU fault */
1172 return 1; /* the MMU fault was handled without causing real CPU fault */
1174 /* now we have a real cpu fault */
1175 tb
= tb_find_pc(pc
);
1177 /* the PC is inside the translated code. It means that we have
1178 a virtual CPU fault */
1179 cpu_restore_state(tb
, env
, pc
, puc
);
1182 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1183 env
->nip
, env
->error_code
, tb
);
1185 /* we restore the process signal mask as the sigreturn should
1186 do it (XXX: use sigsetjmp) */
1187 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
1189 /* never comes here */
1193 #error unsupported target CPU
1196 #if defined(__i386__)
1198 #if defined(USE_CODE_COPY)
1199 static void cpu_send_trap(unsigned long pc
, int trap
,
1200 struct ucontext
*uc
)
1202 TranslationBlock
*tb
;
1205 env
= cpu_single_env
; /* XXX: find a correct solution for multithread */
1206 /* now we have a real cpu fault */
1207 tb
= tb_find_pc(pc
);
1209 /* the PC is inside the translated code. It means that we have
1210 a virtual CPU fault */
1211 cpu_restore_state(tb
, env
, pc
, uc
);
1213 sigprocmask(SIG_SETMASK
, &uc
->uc_sigmask
, NULL
);
1214 raise_exception_err(trap
, env
->error_code
);
1218 int cpu_signal_handler(int host_signum
, struct siginfo
*info
,
1221 struct ucontext
*uc
= puc
;
1229 #define REG_TRAPNO TRAPNO
1231 pc
= uc
->uc_mcontext
.gregs
[REG_EIP
];
1232 trapno
= uc
->uc_mcontext
.gregs
[REG_TRAPNO
];
1233 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1234 if (trapno
== 0x00 || trapno
== 0x05) {
1235 /* send division by zero or bound exception */
1236 cpu_send_trap(pc
, trapno
, uc
);
1240 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
1242 (uc
->uc_mcontext
.gregs
[REG_ERR
] >> 1) & 1 : 0,
1243 &uc
->uc_sigmask
, puc
);
1246 #elif defined(__x86_64__)
1248 int cpu_signal_handler(int host_signum
, struct siginfo
*info
,
1251 struct ucontext
*uc
= puc
;
1254 pc
= uc
->uc_mcontext
.gregs
[REG_RIP
];
1255 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
1256 uc
->uc_mcontext
.gregs
[REG_TRAPNO
] == 0xe ?
1257 (uc
->uc_mcontext
.gregs
[REG_ERR
] >> 1) & 1 : 0,
1258 &uc
->uc_sigmask
, puc
);
1261 #elif defined(__powerpc__)
1263 /***********************************************************************
1264 * signal context platform-specific definitions
1268 /* All Registers access - only for local access */
1269 # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
1270 /* Gpr Registers access */
1271 # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
1272 # define IAR_sig(context) REG_sig(nip, context) /* Program counter */
1273 # define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
1274 # define CTR_sig(context) REG_sig(ctr, context) /* Count register */
1275 # define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
1276 # define LR_sig(context) REG_sig(link, context) /* Link register */
1277 # define CR_sig(context) REG_sig(ccr, context) /* Condition register */
1278 /* Float Registers access */
1279 # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1280 # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1281 /* Exception Registers access */
1282 # define DAR_sig(context) REG_sig(dar, context)
1283 # define DSISR_sig(context) REG_sig(dsisr, context)
1284 # define TRAP_sig(context) REG_sig(trap, context)
1288 # include <sys/ucontext.h>
1289 typedef struct ucontext SIGCONTEXT
;
1290 /* All Registers access - only for local access */
1291 # define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
1292 # define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
1293 # define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
1294 # define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
1295 /* Gpr Registers access */
1296 # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
1297 # define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
1298 # define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
1299 # define CTR_sig(context) REG_sig(ctr, context)
1300 # define XER_sig(context) REG_sig(xer, context) /* Link register */
1301 # define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
1302 # define CR_sig(context) REG_sig(cr, context) /* Condition register */
1303 /* Float Registers access */
1304 # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
1305 # define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
1306 /* Exception Registers access */
1307 # define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
1308 # define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
1309 # define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1310 #endif /* __APPLE__ */
1312 int cpu_signal_handler(int host_signum
, struct siginfo
*info
,
1315 struct ucontext
*uc
= puc
;
1323 if (DSISR_sig(uc
) & 0x00800000)
1326 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000))
1329 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
1330 is_write
, &uc
->uc_sigmask
, puc
);
1333 #elif defined(__alpha__)
1335 int cpu_signal_handler(int host_signum
, struct siginfo
*info
,
1338 struct ucontext
*uc
= puc
;
1339 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
1340 uint32_t insn
= *pc
;
1343 /* XXX: need kernel patch to get write flag faster */
1344 switch (insn
>> 26) {
1359 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
1360 is_write
, &uc
->uc_sigmask
, puc
);
1362 #elif defined(__sparc__)
1364 int cpu_signal_handler(int host_signum
, struct siginfo
*info
,
1367 uint32_t *regs
= (uint32_t *)(info
+ 1);
1368 void *sigmask
= (regs
+ 20);
1373 /* XXX: is there a standard glibc define ? */
1375 /* XXX: need kernel patch to get write flag faster */
1377 insn
= *(uint32_t *)pc
;
1378 if ((insn
>> 30) == 3) {
1379 switch((insn
>> 19) & 0x3f) {
1391 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
1392 is_write
, sigmask
, NULL
);
1395 #elif defined(__arm__)
1397 int cpu_signal_handler(int host_signum
, struct siginfo
*info
,
1400 struct ucontext
*uc
= puc
;
1404 pc
= uc
->uc_mcontext
.gregs
[R15
];
1405 /* XXX: compute is_write */
1407 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
1409 &uc
->uc_sigmask
, puc
);
1412 #elif defined(__mc68000)
1414 int cpu_signal_handler(int host_signum
, struct siginfo
*info
,
1417 struct ucontext
*uc
= puc
;
1421 pc
= uc
->uc_mcontext
.gregs
[16];
1422 /* XXX: compute is_write */
1424 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
1426 &uc
->uc_sigmask
, puc
);
1429 #elif defined(__ia64)
1432 /* This ought to be in <bits/siginfo.h>... */
1433 # define __ISR_VALID 1
1436 int cpu_signal_handler(int host_signum
, struct siginfo
*info
, void *puc
)
1438 struct ucontext
*uc
= puc
;
1442 ip
= uc
->uc_mcontext
.sc_ip
;
1443 switch (host_signum
) {
1449 if (info
->si_code
&& (info
->si_segvflags
& __ISR_VALID
))
1450 /* ISR.W (write-access) is bit 33: */
1451 is_write
= (info
->si_isr
>> 33) & 1;
1457 return handle_cpu_signal(ip
, (unsigned long)info
->si_addr
,
1459 &uc
->uc_sigmask
, puc
);
1462 #elif defined(__s390__)
1464 int cpu_signal_handler(int host_signum
, struct siginfo
*info
,
1467 struct ucontext
*uc
= puc
;
1471 pc
= uc
->uc_mcontext
.psw
.addr
;
1472 /* XXX: compute is_write */
1474 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
1476 &uc
->uc_sigmask
, puc
);
1481 #error host CPU specific signal handler needed
1485 #endif /* !defined(CONFIG_SOFTMMU) */