2 * i386 emulator main execution loop
4 * Copyright (c) 2003 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);
51 /* exit the current TB from a signal handler. The host registers are
52 restored in a state compatible with the CPU emulator
54 void cpu_resume_from_signal(CPUState *env1, void *puc)
56 #if !defined(CONFIG_SOFTMMU)
57 struct ucontext *uc = puc;
62 /* XXX: restore cpu registers saved in host registers */
64 #if !defined(CONFIG_SOFTMMU)
66 /* XXX: use siglongjmp ? */
67 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
70 longjmp(env->jmp_env, 1);
73 /* main execution loop */
75 int cpu_exec(CPUState *env1)
77 int saved_T0, saved_T1, saved_T2;
104 int saved_i7, tmp_T0;
106 int code_gen_size, ret, interrupt_request;
107 void (*gen_func)(void);
108 TranslationBlock *tb, **ptb;
109 target_ulong cs_base, pc;
113 /* first we save global registers */
120 /* we also save i7 because longjmp may not restore it */
121 asm volatile ("mov %%i7, %0" : "=r" (saved_i7));
124 #if defined(TARGET_I386)
151 /* put eflags in CPU temporary format */
152 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
153 DF = 1 - (2 * ((env->eflags >> 10) & 1));
154 CC_OP = CC_OP_EFLAGS;
155 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
156 #elif defined(TARGET_ARM)
160 env->CF = (psr >> 29) & 1;
161 env->NZF = (psr & 0xc0000000) ^ 0x40000000;
162 env->VF = (psr << 3) & 0x80000000;
163 env->QF = (psr >> 27) & 1;
164 env->cpsr = psr & ~CACHED_CPSR_BITS;
166 #elif defined(TARGET_SPARC)
167 #elif defined(TARGET_PPC)
169 #error unsupported target CPU
171 env->exception_index = -1;
173 /* prepare setjmp context for exception handling */
175 if (setjmp(env->jmp_env) == 0) {
176 env->current_tb = NULL;
177 /* if an exception is pending, we execute it here */
178 if (env->exception_index >= 0) {
179 if (env->exception_index >= EXCP_INTERRUPT) {
180 /* exit request from the cpu execution loop */
181 ret = env->exception_index;
183 } else if (env->user_mode_only) {
184 /* if user mode only, we simulate a fake exception
185 which will be hanlded outside the cpu execution
187 #if defined(TARGET_I386)
188 do_interrupt_user(env->exception_index,
189 env->exception_is_int,
191 env->exception_next_eip);
193 ret = env->exception_index;
196 #if defined(TARGET_I386)
197 /* simulate a real cpu exception. On i386, it can
198 trigger new exceptions, but we do not handle
199 double or triple faults yet. */
200 do_interrupt(env->exception_index,
201 env->exception_is_int,
203 env->exception_next_eip, 0);
204 #elif defined(TARGET_PPC)
206 #elif defined(TARGET_SPARC)
207 do_interrupt(env->exception_index,
211 env->exception_index = -1;
214 if (kqemu_is_ok(env) && env->interrupt_request == 0) {
216 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
217 ret = kqemu_cpu_exec(env);
218 /* put eflags in CPU temporary format */
219 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
220 DF = 1 - (2 * ((env->eflags >> 10) & 1));
221 CC_OP = CC_OP_EFLAGS;
222 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
225 longjmp(env->jmp_env, 1);
226 } else if (ret == 2) {
227 /* softmmu execution needed */
229 if (env->interrupt_request != 0) {
230 /* hardware interrupt will be executed just after */
232 /* otherwise, we restart */
233 longjmp(env->jmp_env, 1);
239 T0 = 0; /* force lookup of first TB */
242 /* g1 can be modified by some libc? functions */
245 interrupt_request = env->interrupt_request;
246 if (__builtin_expect(interrupt_request, 0)) {
247 #if defined(TARGET_I386)
248 /* if hardware interrupt pending, we execute it */
249 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
250 (env->eflags & IF_MASK) &&
251 !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
253 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
254 intno = cpu_get_pic_interrupt(env);
255 if (loglevel & CPU_LOG_TB_IN_ASM) {
256 fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
258 do_interrupt(intno, 0, 0, 0, 1);
259 /* ensure that no TB jump will be modified as
260 the program flow was changed */
267 #elif defined(TARGET_PPC)
269 if ((interrupt_request & CPU_INTERRUPT_RESET)) {
274 if ((interrupt_request & CPU_INTERRUPT_HARD)) {
276 env->exception_index = EXCP_EXTERNAL;
279 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
280 } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) {
282 env->exception_index = EXCP_DECR;
285 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
288 #elif defined(TARGET_SPARC)
289 if (interrupt_request & CPU_INTERRUPT_HARD) {
290 do_interrupt(env->interrupt_index, 0);
291 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
292 } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
293 //do_interrupt(0, 0, 0, 0, 0);
294 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
297 if (interrupt_request & CPU_INTERRUPT_EXITTB) {
298 env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
299 /* ensure that no TB jump will be modified as
300 the program flow was changed */
307 if (interrupt_request & CPU_INTERRUPT_EXIT) {
308 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
309 env->exception_index = EXCP_INTERRUPT;
314 if ((loglevel & CPU_LOG_EXEC)) {
315 #if defined(TARGET_I386)
316 /* restore flags in standard format */
317 env->regs[R_EAX] = EAX;
318 env->regs[R_EBX] = EBX;
319 env->regs[R_ECX] = ECX;
320 env->regs[R_EDX] = EDX;
321 env->regs[R_ESI] = ESI;
322 env->regs[R_EDI] = EDI;
323 env->regs[R_EBP] = EBP;
324 env->regs[R_ESP] = ESP;
325 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
326 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
327 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
328 #elif defined(TARGET_ARM)
329 env->cpsr = compute_cpsr();
330 cpu_dump_state(env, logfile, fprintf, 0);
331 env->cpsr &= ~CACHED_CPSR_BITS;
332 #elif defined(TARGET_SPARC)
333 cpu_dump_state (env, logfile, fprintf, 0);
334 #elif defined(TARGET_PPC)
335 cpu_dump_state(env, logfile, fprintf, 0);
337 #error unsupported target CPU
341 /* we record a subset of the CPU state. It will
342 always be the same before a given translated block
344 #if defined(TARGET_I386)
346 flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
347 cs_base = env->segs[R_CS].base;
348 pc = cs_base + env->eip;
349 #elif defined(TARGET_ARM)
353 #elif defined(TARGET_SPARC)
357 #elif defined(TARGET_PPC)
362 #error unsupported CPU
364 tb = tb_find(&ptb, pc, cs_base,
367 TranslationBlock **ptb1;
369 target_ulong phys_pc, phys_page1, phys_page2, virt_page2;
374 tb_invalidated_flag = 0;
376 regs_to_env(); /* XXX: do it just before cpu_gen_code() */
378 /* find translated block using physical mappings */
379 phys_pc = get_phys_addr_code(env, pc);
380 phys_page1 = phys_pc & TARGET_PAGE_MASK;
382 h = tb_phys_hash_func(phys_pc);
383 ptb1 = &tb_phys_hash[h];
389 tb->page_addr[0] == phys_page1 &&
390 tb->cs_base == cs_base &&
391 tb->flags == flags) {
392 /* check next page if needed */
393 if (tb->page_addr[1] != -1) {
394 virt_page2 = (pc & TARGET_PAGE_MASK) +
396 phys_page2 = get_phys_addr_code(env, virt_page2);
397 if (tb->page_addr[1] == phys_page2)
403 ptb1 = &tb->phys_hash_next;
406 /* if no translated code available, then translate it now */
409 /* flush must be done */
411 /* cannot fail at this point */
413 /* don't forget to invalidate previous TB info */
414 ptb = &tb_hash[tb_hash_func(pc)];
417 tc_ptr = code_gen_ptr;
419 tb->cs_base = cs_base;
421 cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
422 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
424 /* check next page if needed */
425 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
427 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
428 phys_page2 = get_phys_addr_code(env, virt_page2);
430 tb_link_phys(tb, phys_pc, phys_page2);
433 if (tb_invalidated_flag) {
434 /* as some TB could have been invalidated because
435 of memory exceptions while generating the code, we
436 must recompute the hash index here */
437 ptb = &tb_hash[tb_hash_func(pc)];
439 ptb = &(*ptb)->hash_next;
442 /* we add the TB in the virtual pc hash table */
444 tb->hash_next = NULL;
446 spin_unlock(&tb_lock);
449 if ((loglevel & CPU_LOG_EXEC)) {
450 fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
451 (long)tb->tc_ptr, tb->pc,
452 lookup_symbol(tb->pc));
458 /* see if we can patch the calling TB. */
461 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
462 && (tb->cflags & CF_CODE_COPY) ==
463 (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
467 tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
468 #if defined(USE_CODE_COPY)
469 /* propagates the FP use info */
470 ((TranslationBlock *)(T0 & ~3))->cflags |=
471 (tb->cflags & CF_FP_USED);
473 spin_unlock(&tb_lock);
477 env->current_tb = tb;
478 /* execute the generated code */
479 gen_func = (void *)tc_ptr;
480 #if defined(__sparc__)
481 __asm__ __volatile__("call %0\n\t"
485 : "i0", "i1", "i2", "i3", "i4", "i5");
486 #elif defined(__arm__)
487 asm volatile ("mov pc, %0\n\t"
488 ".global exec_loop\n\t"
492 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
493 #elif defined(TARGET_I386) && defined(USE_CODE_COPY)
495 if (!(tb->cflags & CF_CODE_COPY)) {
496 if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
497 save_native_fp_state(env);
501 if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
502 restore_native_fp_state(env);
504 /* we work with native eflags */
505 CC_SRC = cc_table[CC_OP].compute_all();
506 CC_OP = CC_OP_EFLAGS;
507 asm(".globl exec_loop\n"
512 " fs movl %11, %%eax\n"
513 " andl $0x400, %%eax\n"
514 " fs orl %8, %%eax\n"
517 " fs movl %%esp, %12\n"
518 " fs movl %0, %%eax\n"
519 " fs movl %1, %%ecx\n"
520 " fs movl %2, %%edx\n"
521 " fs movl %3, %%ebx\n"
522 " fs movl %4, %%esp\n"
523 " fs movl %5, %%ebp\n"
524 " fs movl %6, %%esi\n"
525 " fs movl %7, %%edi\n"
528 " fs movl %%esp, %4\n"
529 " fs movl %12, %%esp\n"
530 " fs movl %%eax, %0\n"
531 " fs movl %%ecx, %1\n"
532 " fs movl %%edx, %2\n"
533 " fs movl %%ebx, %3\n"
534 " fs movl %%ebp, %5\n"
535 " fs movl %%esi, %6\n"
536 " fs movl %%edi, %7\n"
539 " movl %%eax, %%ecx\n"
540 " andl $0x400, %%ecx\n"
542 " andl $0x8d5, %%eax\n"
543 " fs movl %%eax, %8\n"
545 " subl %%ecx, %%eax\n"
546 " fs movl %%eax, %11\n"
547 " fs movl %9, %%ebx\n" /* get T0 value */
550 : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
551 "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
552 "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
553 "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
554 "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
555 "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
556 "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
557 "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
558 "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
559 "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
561 "m" (*(uint8_t *)offsetof(CPUState, df)),
562 "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
570 env->current_tb = NULL;
571 /* reset soft MMU for next block (it can currently
572 only be set by a memory fault) */
573 #if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
574 if (env->hflags & HF_SOFTMMU_MASK) {
575 env->hflags &= ~HF_SOFTMMU_MASK;
576 /* do not allow linking to another block */
587 #if defined(TARGET_I386)
588 #if defined(USE_CODE_COPY)
589 if (env->native_fp_regs) {
590 save_native_fp_state(env);
593 /* restore flags in standard format */
594 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
596 /* restore global registers */
621 #elif defined(TARGET_ARM)
622 env->cpsr = compute_cpsr();
623 #elif defined(TARGET_SPARC)
624 #elif defined(TARGET_PPC)
626 #error unsupported target CPU
629 asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
638 /* must only be called from the generated code as an exception can be
640 void tb_invalidate_page_range(target_ulong start, target_ulong end)
642 /* XXX: cannot enable it yet because it yields to MMU exception
643 where NIP != read address on PowerPC */
645 target_ulong phys_addr;
646 phys_addr = get_phys_addr_code(env, start);
647 tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
651 #if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
653 void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
655 CPUX86State *saved_env;
659 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
661 cpu_x86_load_seg_cache(env, seg_reg, selector,
662 (selector << 4), 0xffff, 0);
664 load_seg(seg_reg, selector);
669 void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
671 CPUX86State *saved_env;
676 helper_fsave((target_ulong)ptr, data32);
681 void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
683 CPUX86State *saved_env;
688 helper_frstor((target_ulong)ptr, data32);
693 #endif /* TARGET_I386 */
695 #if !defined(CONFIG_SOFTMMU)
697 #if defined(TARGET_I386)
699 /* 'pc' is the host PC at which the exception was raised. 'address' is
700 the effective address of the memory exception. 'is_write' is 1 if a
701 write caused the exception and otherwise 0'. 'old_set' is the
702 signal set which should be restored */
703 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
704 int is_write, sigset_t *old_set,
707 TranslationBlock *tb;
711 env = cpu_single_env; /* XXX: find a correct solution for multithread */
712 #if defined(DEBUG_SIGNAL)
713 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
714 pc, address, is_write, *(unsigned long *)old_set);
716 /* XXX: locking issue */
717 if (is_write && page_unprotect(address, pc, puc)) {
721 /* see if it is an MMU fault */
722 ret = cpu_x86_handle_mmu_fault(env, address, is_write,
723 ((env->hflags & HF_CPL_MASK) == 3), 0);
725 return 0; /* not an MMU fault */
727 return 1; /* the MMU fault was handled without causing real CPU fault */
728 /* now we have a real cpu fault */
731 /* the PC is inside the translated code. It means that we have
732 a virtual CPU fault */
733 cpu_restore_state(tb, env, pc, puc);
737 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
738 env->eip, env->cr[2], env->error_code);
740 /* we restore the process signal mask as the sigreturn should
741 do it (XXX: use sigsetjmp) */
742 sigprocmask(SIG_SETMASK, old_set, NULL);
743 raise_exception_err(EXCP0E_PAGE, env->error_code);
745 /* activate soft MMU for this block */
746 env->hflags |= HF_SOFTMMU_MASK;
747 cpu_resume_from_signal(env, puc);
749 /* never comes here */
753 #elif defined(TARGET_ARM)
754 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
755 int is_write, sigset_t *old_set,
758 TranslationBlock *tb;
762 env = cpu_single_env; /* XXX: find a correct solution for multithread */
763 #if defined(DEBUG_SIGNAL)
764 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
765 pc, address, is_write, *(unsigned long *)old_set);
767 /* XXX: locking issue */
768 if (is_write && page_unprotect(address, pc, puc)) {
771 /* see if it is an MMU fault */
772 ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
774 return 0; /* not an MMU fault */
776 return 1; /* the MMU fault was handled without causing real CPU fault */
777 /* now we have a real cpu fault */
780 /* the PC is inside the translated code. It means that we have
781 a virtual CPU fault */
782 cpu_restore_state(tb, env, pc, puc);
784 /* we restore the process signal mask as the sigreturn should
785 do it (XXX: use sigsetjmp) */
786 sigprocmask(SIG_SETMASK, old_set, NULL);
789 #elif defined(TARGET_SPARC)
790 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
791 int is_write, sigset_t *old_set,
794 TranslationBlock *tb;
798 env = cpu_single_env; /* XXX: find a correct solution for multithread */
799 #if defined(DEBUG_SIGNAL)
800 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
801 pc, address, is_write, *(unsigned long *)old_set);
803 /* XXX: locking issue */
804 if (is_write && page_unprotect(address, pc, puc)) {
807 /* see if it is an MMU fault */
808 ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
810 return 0; /* not an MMU fault */
812 return 1; /* the MMU fault was handled without causing real CPU fault */
813 /* now we have a real cpu fault */
816 /* the PC is inside the translated code. It means that we have
817 a virtual CPU fault */
818 cpu_restore_state(tb, env, pc, puc);
820 /* we restore the process signal mask as the sigreturn should
821 do it (XXX: use sigsetjmp) */
822 sigprocmask(SIG_SETMASK, old_set, NULL);
825 #elif defined (TARGET_PPC)
826 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
827 int is_write, sigset_t *old_set,
830 TranslationBlock *tb;
834 env = cpu_single_env; /* XXX: find a correct solution for multithread */
835 #if defined(DEBUG_SIGNAL)
836 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
837 pc, address, is_write, *(unsigned long *)old_set);
839 /* XXX: locking issue */
840 if (is_write && page_unprotect(address, pc, puc)) {
844 /* see if it is an MMU fault */
845 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
847 return 0; /* not an MMU fault */
849 return 1; /* the MMU fault was handled without causing real CPU fault */
851 /* now we have a real cpu fault */
854 /* the PC is inside the translated code. It means that we have
855 a virtual CPU fault */
856 cpu_restore_state(tb, env, pc, puc);
860 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
861 env->nip, env->error_code, tb);
863 /* we restore the process signal mask as the sigreturn should
864 do it (XXX: use sigsetjmp) */
865 sigprocmask(SIG_SETMASK, old_set, NULL);
866 do_raise_exception_err(env->exception_index, env->error_code);
868 /* activate soft MMU for this block */
869 cpu_resume_from_signal(env, puc);
871 /* never comes here */
875 #error unsupported target CPU
878 #if defined(__i386__)
880 #if defined(USE_CODE_COPY)
881 static void cpu_send_trap(unsigned long pc, int trap,
884 TranslationBlock *tb;
887 env = cpu_single_env; /* XXX: find a correct solution for multithread */
888 /* now we have a real cpu fault */
891 /* the PC is inside the translated code. It means that we have
892 a virtual CPU fault */
893 cpu_restore_state(tb, env, pc, uc);
895 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
896 raise_exception_err(trap, env->error_code);
900 int cpu_signal_handler(int host_signum, struct siginfo *info,
903 struct ucontext *uc = puc;
911 #define REG_TRAPNO TRAPNO
913 pc = uc->uc_mcontext.gregs[REG_EIP];
914 trapno = uc->uc_mcontext.gregs[REG_TRAPNO];
915 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
916 if (trapno == 0x00 || trapno == 0x05) {
917 /* send division by zero or bound exception */
918 cpu_send_trap(pc, trapno, uc);
922 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
924 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
925 &uc->uc_sigmask, puc);
928 #elif defined(__x86_64__)
930 int cpu_signal_handler(int host_signum, struct siginfo *info,
933 struct ucontext *uc = puc;
936 pc = uc->uc_mcontext.gregs[REG_RIP];
937 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
938 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
939 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
940 &uc->uc_sigmask, puc);
943 #elif defined(__powerpc__)
945 /***********************************************************************
946 * signal context platform-specific definitions
950 /* All Registers access - only for local access */
951 # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
952 /* Gpr Registers access */
953 # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
954 # define IAR_sig(context) REG_sig(nip, context) /* Program counter */
955 # define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
956 # define CTR_sig(context) REG_sig(ctr, context) /* Count register */
957 # define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
958 # define LR_sig(context) REG_sig(link, context) /* Link register */
959 # define CR_sig(context) REG_sig(ccr, context) /* Condition register */
960 /* Float Registers access */
961 # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
962 # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
963 /* Exception Registers access */
964 # define DAR_sig(context) REG_sig(dar, context)
965 # define DSISR_sig(context) REG_sig(dsisr, context)
966 # define TRAP_sig(context) REG_sig(trap, context)
970 # include <sys/ucontext.h>
971 typedef struct ucontext SIGCONTEXT;
972 /* All Registers access - only for local access */
973 # define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
974 # define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
975 # define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
976 # define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
977 /* Gpr Registers access */
978 # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
979 # define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
980 # define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
981 # define CTR_sig(context) REG_sig(ctr, context)
982 # define XER_sig(context) REG_sig(xer, context) /* Link register */
983 # define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
984 # define CR_sig(context) REG_sig(cr, context) /* Condition register */
985 /* Float Registers access */
986 # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
987 # define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
988 /* Exception Registers access */
989 # define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
990 # define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
991 # define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
992 #endif /* __APPLE__ */
994 int cpu_signal_handler(int host_signum, struct siginfo *info,
997 struct ucontext *uc = puc;
1005 if (DSISR_sig(uc) & 0x00800000)
1008 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
1011 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1012 is_write, &uc->uc_sigmask, puc);
1015 #elif defined(__alpha__)
1017 int cpu_signal_handler(int host_signum, struct siginfo *info,
1020 struct ucontext *uc = puc;
1021 uint32_t *pc = uc->uc_mcontext.sc_pc;
1022 uint32_t insn = *pc;
1025 /* XXX: need kernel patch to get write flag faster */
1026 switch (insn >> 26) {
1041 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1042 is_write, &uc->uc_sigmask, puc);
1044 #elif defined(__sparc__)
1046 int cpu_signal_handler(int host_signum, struct siginfo *info,
1049 uint32_t *regs = (uint32_t *)(info + 1);
1050 void *sigmask = (regs + 20);
1055 /* XXX: is there a standard glibc define ? */
1057 /* XXX: need kernel patch to get write flag faster */
1059 insn = *(uint32_t *)pc;
1060 if ((insn >> 30) == 3) {
1061 switch((insn >> 19) & 0x3f) {
1073 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1074 is_write, sigmask, NULL);
1077 #elif defined(__arm__)
1079 int cpu_signal_handler(int host_signum, struct siginfo *info,
1082 struct ucontext *uc = puc;
1086 pc = uc->uc_mcontext.gregs[R15];
1087 /* XXX: compute is_write */
1089 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1094 #elif defined(__mc68000)
1096 int cpu_signal_handler(int host_signum, struct siginfo *info,
1099 struct ucontext *uc = puc;
1103 pc = uc->uc_mcontext.gregs[16];
1104 /* XXX: compute is_write */
1106 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1108 &uc->uc_sigmask, puc);
1113 #error host CPU specific signal handler needed
1117 #endif /* !defined(CONFIG_SOFTMMU) */