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