bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * i386 emulator main execution loop |
| 3 | * |
bellard | 66321a1 | 2005-04-06 20:47:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2005 Fabrice Bellard |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 5 | * |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 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. |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 10 | * |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 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. |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 15 | * |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 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 |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 19 | */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 20 | #include "config.h" |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 21 | #include "exec.h" |
bellard | 956034d | 2003-04-29 20:40:53 +0000 | [diff] [blame] | 22 | #include "disas.h" |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 23 | |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 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 | |
bellard | 36bdbe5 | 2003-11-19 22:12:02 +0000 | [diff] [blame] | 38 | int tb_invalidated_flag; |
| 39 | |
bellard | dc99065 | 2003-03-19 00:00:28 +0000 | [diff] [blame] | 40 | //#define DEBUG_EXEC |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 41 | //#define DEBUG_SIGNAL |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 42 | |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 43 | #if defined(TARGET_ARM) || defined(TARGET_SPARC) || defined(TARGET_M68K) |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 44 | /* XXX: unify with i386 target */ |
| 45 | void cpu_loop_exit(void) |
| 46 | { |
| 47 | longjmp(env->jmp_env, 1); |
| 48 | } |
| 49 | #endif |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 50 | #if !(defined(TARGET_SPARC) || defined(TARGET_SH4) || defined(TARGET_M68K)) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 51 | #define reg_T2 |
| 52 | #endif |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 53 | |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 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 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 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 */ |
bellard | 1538800 | 2005-12-19 01:42:32 +0000 | [diff] [blame] | 129 | tb_invalidated_flag = 1; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 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: |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 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) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 169 | | (env->vfp.vec_stride << 4); |
| 170 | if ((env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) |
| 171 | flags |= (1 << 6); |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 172 | if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) |
| 173 | flags |= (1 << 7); |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 174 | cs_base = 0; |
| 175 | pc = env->regs[15]; |
| 176 | #elif defined(TARGET_SPARC) |
| 177 | #ifdef TARGET_SPARC64 |
bellard | a80dde0 | 2006-06-26 19:53:29 +0000 | [diff] [blame] | 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); |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 181 | #else |
bellard | a80dde0 | 2006-06-26 19:53:29 +0000 | [diff] [blame] | 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; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 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) |
pbrook | 56b1940 | 2006-03-11 16:23:39 +0000 | [diff] [blame] | 194 | flags = env->hflags & (MIPS_HFLAG_TMASK | MIPS_HFLAG_BMASK); |
bellard | cc9442b | 2005-11-26 18:43:28 +0000 | [diff] [blame] | 195 | cs_base = 0; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 196 | pc = env->PC; |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 197 | #elif defined(TARGET_M68K) |
| 198 | flags = env->fpcr & M68K_FPCR_PREC; |
| 199 | cs_base = 0; |
| 200 | pc = env->pc; |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 201 | #elif defined(TARGET_SH4) |
| 202 | flags = env->sr & (SR_MD | SR_RB); |
| 203 | cs_base = 0; /* XXXXX */ |
| 204 | pc = env->pc; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 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); |
bellard | 1538800 | 2005-12-19 01:42:32 +0000 | [diff] [blame] | 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 | } |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 220 | } |
| 221 | return tb; |
| 222 | } |
| 223 | |
| 224 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 225 | /* main execution loop */ |
| 226 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 227 | int cpu_exec(CPUState *env1) |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 228 | { |
pbrook | 1057eaa | 2007-02-04 13:37:44 +0000 | [diff] [blame] | 229 | #define DECLARE_HOST_REGS 1 |
| 230 | #include "hostregs_helper.h" |
| 231 | #if defined(TARGET_SPARC) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 232 | #if defined(reg_REGWPTR) |
| 233 | uint32_t *saved_regwptr; |
| 234 | #endif |
| 235 | #endif |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 236 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
ths | b49d07b | 2007-02-02 03:57:09 +0000 | [diff] [blame] | 237 | int saved_i7; |
| 238 | target_ulong tmp_T0; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 239 | #endif |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 240 | int ret, interrupt_request; |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 241 | void (*gen_func)(void); |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 242 | TranslationBlock *tb; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 243 | uint8_t *tc_ptr; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 244 | |
bellard | 5a1e3cf | 2005-11-23 21:02:53 +0000 | [diff] [blame] | 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 | } |
bellard | e80e1cc | 2005-11-23 22:05:28 +0000 | [diff] [blame] | 256 | #elif defined(TARGET_PPC) |
bellard | 50443c9 | 2005-11-26 20:15:14 +0000 | [diff] [blame] | 257 | if (env1->halted) { |
bellard | e80e1cc | 2005-11-23 22:05:28 +0000 | [diff] [blame] | 258 | if (env1->msr[MSR_EE] && |
j_mayer | 4710357 | 2007-03-30 09:38:04 +0000 | [diff] [blame] | 259 | (env1->interrupt_request & CPU_INTERRUPT_HARD)) { |
bellard | 50443c9 | 2005-11-26 20:15:14 +0000 | [diff] [blame] | 260 | env1->halted = 0; |
bellard | e80e1cc | 2005-11-23 22:05:28 +0000 | [diff] [blame] | 261 | } else { |
| 262 | return EXCP_HALTED; |
| 263 | } |
| 264 | } |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 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 | } |
bellard | 9332f9d | 2005-11-26 10:46:39 +0000 | [diff] [blame] | 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 | } |
bellard | 6810e15 | 2005-12-05 19:59:05 +0000 | [diff] [blame] | 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 | } |
bellard | 5a1e3cf | 2005-11-23 21:02:53 +0000 | [diff] [blame] | 294 | #endif |
| 295 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 296 | cpu_single_env = env1; |
| 297 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 298 | /* first we save global registers */ |
pbrook | 1057eaa | 2007-02-04 13:37:44 +0000 | [diff] [blame] | 299 | #define SAVE_HOST_REGS 1 |
| 300 | #include "hostregs_helper.h" |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 301 | env = env1; |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 302 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 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) |
bellard | 0d1a29f | 2004-10-12 22:01:28 +0000 | [diff] [blame] | 308 | env_to_regs(); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 309 | /* put eflags in CPU temporary format */ |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 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)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 312 | CC_OP = CC_OP_EFLAGS; |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 313 | env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 314 | #elif defined(TARGET_ARM) |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 315 | #elif defined(TARGET_SPARC) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 316 | #if defined(reg_REGWPTR) |
| 317 | saved_regwptr = REGWPTR; |
| 318 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 319 | #elif defined(TARGET_PPC) |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 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; |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 324 | #elif defined(TARGET_MIPS) |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 325 | #elif defined(TARGET_SH4) |
| 326 | /* XXXXX */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 327 | #else |
| 328 | #error unsupported target CPU |
| 329 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 330 | env->exception_index = -1; |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 331 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 332 | /* prepare setjmp context for exception handling */ |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 333 | for(;;) { |
| 334 | if (setjmp(env->jmp_env) == 0) { |
bellard | ee8b702 | 2004-02-03 23:35:10 +0000 | [diff] [blame] | 335 | env->current_tb = NULL; |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 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 |
ths | 9f08349 | 2006-12-07 18:28:42 +0000 | [diff] [blame] | 344 | which will be handled outside the cpu execution |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 345 | loop */ |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 346 | #if defined(TARGET_I386) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 347 | do_interrupt_user(env->exception_index, |
| 348 | env->exception_is_int, |
| 349 | env->error_code, |
| 350 | env->exception_next_eip); |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 351 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 352 | ret = env->exception_index; |
| 353 | break; |
| 354 | } else { |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 355 | #if defined(TARGET_I386) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 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, |
bellard | d05e66d | 2003-08-20 21:34:35 +0000 | [diff] [blame] | 362 | env->exception_next_eip, 0); |
ths | 678dde1 | 2007-03-31 20:28:52 +0000 | [diff] [blame] | 363 | /* successfully delivered */ |
| 364 | env->old_exception = -1; |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 365 | #elif defined(TARGET_PPC) |
| 366 | do_interrupt(env); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 367 | #elif defined(TARGET_MIPS) |
| 368 | do_interrupt(env); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 369 | #elif defined(TARGET_SPARC) |
bellard | 1a0c329 | 2005-02-13 19:02:07 +0000 | [diff] [blame] | 370 | do_interrupt(env->exception_index); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 371 | #elif defined(TARGET_ARM) |
| 372 | do_interrupt(env); |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 373 | #elif defined(TARGET_SH4) |
| 374 | do_interrupt(env); |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 375 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 376 | } |
| 377 | env->exception_index = -1; |
bellard | 9df217a | 2005-02-10 22:05:51 +0000 | [diff] [blame] | 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 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 402 | } |
bellard | 9df217a | 2005-02-10 22:05:51 +0000 | [diff] [blame] | 403 | #endif |
| 404 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 405 | T0 = 0; /* force lookup of first TB */ |
| 406 | for(;;) { |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 407 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 408 | /* g1 can be modified by some libc? functions */ |
| 409 | tmp_T0 = T0; |
| 410 | #endif |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 411 | interrupt_request = env->interrupt_request; |
bellard | 2e255c6 | 2003-08-21 23:25:21 +0000 | [diff] [blame] | 412 | if (__builtin_expect(interrupt_request, 0)) { |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 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 | } |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 418 | #if defined(TARGET_I386) |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 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) && |
bellard | 3f33731 | 2003-08-20 23:02:09 +0000 | [diff] [blame] | 429 | (env->eflags & IF_MASK) && |
| 430 | !(env->hflags & HF_INHIBIT_IRQ_MASK)) { |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 431 | int intno; |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 432 | env->interrupt_request &= ~CPU_INTERRUPT_HARD; |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 433 | intno = cpu_get_pic_interrupt(env); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 434 | if (loglevel & CPU_LOG_TB_IN_ASM) { |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 435 | fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno); |
| 436 | } |
bellard | d05e66d | 2003-08-20 21:34:35 +0000 | [diff] [blame] | 437 | do_interrupt(intno, 0, 0, 0, 1); |
bellard | 907a5b2 | 2003-06-30 23:18:22 +0000 | [diff] [blame] | 438 | /* ensure that no TB jump will be modified as |
| 439 | the program flow was changed */ |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 440 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
bellard | 907a5b2 | 2003-06-30 23:18:22 +0000 | [diff] [blame] | 441 | tmp_T0 = 0; |
| 442 | #else |
| 443 | T0 = 0; |
| 444 | #endif |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 445 | } |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 446 | #elif defined(TARGET_PPC) |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 447 | #if 0 |
| 448 | if ((interrupt_request & CPU_INTERRUPT_RESET)) { |
| 449 | cpu_ppc_reset(env); |
| 450 | } |
| 451 | #endif |
j_mayer | 4710357 | 2007-03-30 09:38:04 +0000 | [diff] [blame] | 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; |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 457 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 458 | tmp_T0 = 0; |
| 459 | #else |
| 460 | T0 = 0; |
| 461 | #endif |
| 462 | } |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 463 | } |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 464 | #elif defined(TARGET_MIPS) |
| 465 | if ((interrupt_request & CPU_INTERRUPT_HARD) && |
ths | 24c7b0e | 2007-03-30 16:44:54 +0000 | [diff] [blame] | 466 | (env->CP0_Status & env->CP0_Cause & CP0Ca_IP_mask) && |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 467 | (env->CP0_Status & (1 << CP0St_IE)) && |
ths | 24c7b0e | 2007-03-30 16:44:54 +0000 | [diff] [blame] | 468 | !(env->CP0_Status & (1 << CP0St_EXL)) && |
| 469 | !(env->CP0_Status & (1 << CP0St_ERL)) && |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 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); |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 475 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 476 | tmp_T0 = 0; |
| 477 | #else |
| 478 | T0 = 0; |
| 479 | #endif |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 480 | } |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 481 | #elif defined(TARGET_SPARC) |
bellard | 66321a1 | 2005-04-06 20:47:48 +0000 | [diff] [blame] | 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; |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 493 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 494 | tmp_T0 = 0; |
| 495 | #else |
| 496 | T0 = 0; |
| 497 | #endif |
bellard | 66321a1 | 2005-04-06 20:47:48 +0000 | [diff] [blame] | 498 | } |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 499 | } else if (interrupt_request & CPU_INTERRUPT_TIMER) { |
| 500 | //do_interrupt(0, 0, 0, 0, 0); |
| 501 | env->interrupt_request &= ~CPU_INTERRUPT_TIMER; |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 502 | } else if (interrupt_request & CPU_INTERRUPT_HALT) { |
bellard | df52b00 | 2006-09-20 20:30:57 +0000 | [diff] [blame] | 503 | env->interrupt_request &= ~CPU_INTERRUPT_HALT; |
| 504 | env->halted = 1; |
| 505 | env->exception_index = EXCP_HLT; |
| 506 | cpu_loop_exit(); |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 507 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 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 | } |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 519 | #elif defined(TARGET_SH4) |
| 520 | /* XXXXX */ |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 521 | #endif |
bellard | 9d05095 | 2006-05-22 22:03:52 +0000 | [diff] [blame] | 522 | /* Don't use the cached interupt_request value, |
| 523 | do_interrupt may have updated the EXITTB flag. */ |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 524 | if (env->interrupt_request & CPU_INTERRUPT_EXITTB) { |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 525 | env->interrupt_request &= ~CPU_INTERRUPT_EXITTB; |
| 526 | /* ensure that no TB jump will be modified as |
| 527 | the program flow was changed */ |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 528 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 529 | tmp_T0 = 0; |
| 530 | #else |
| 531 | T0 = 0; |
| 532 | #endif |
| 533 | } |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 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 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 539 | } |
| 540 | #ifdef DEBUG_EXEC |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 541 | if ((loglevel & CPU_LOG_TB_CPU)) { |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 542 | #if defined(TARGET_I386) |
| 543 | /* restore flags in standard format */ |
bellard | fc9f715 | 2005-04-26 19:33:35 +0000 | [diff] [blame] | 544 | #ifdef reg_EAX |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 545 | env->regs[R_EAX] = EAX; |
bellard | fc9f715 | 2005-04-26 19:33:35 +0000 | [diff] [blame] | 546 | #endif |
| 547 | #ifdef reg_EBX |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 548 | env->regs[R_EBX] = EBX; |
bellard | fc9f715 | 2005-04-26 19:33:35 +0000 | [diff] [blame] | 549 | #endif |
| 550 | #ifdef reg_ECX |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 551 | env->regs[R_ECX] = ECX; |
bellard | fc9f715 | 2005-04-26 19:33:35 +0000 | [diff] [blame] | 552 | #endif |
| 553 | #ifdef reg_EDX |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 554 | env->regs[R_EDX] = EDX; |
bellard | fc9f715 | 2005-04-26 19:33:35 +0000 | [diff] [blame] | 555 | #endif |
| 556 | #ifdef reg_ESI |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 557 | env->regs[R_ESI] = ESI; |
bellard | fc9f715 | 2005-04-26 19:33:35 +0000 | [diff] [blame] | 558 | #endif |
| 559 | #ifdef reg_EDI |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 560 | env->regs[R_EDI] = EDI; |
bellard | fc9f715 | 2005-04-26 19:33:35 +0000 | [diff] [blame] | 561 | #endif |
| 562 | #ifdef reg_EBP |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 563 | env->regs[R_EBP] = EBP; |
bellard | fc9f715 | 2005-04-26 19:33:35 +0000 | [diff] [blame] | 564 | #endif |
| 565 | #ifdef reg_ESP |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 566 | env->regs[R_ESP] = ESP; |
bellard | fc9f715 | 2005-04-26 19:33:35 +0000 | [diff] [blame] | 567 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 568 | env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK); |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 569 | cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 570 | env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 571 | #elif defined(TARGET_ARM) |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 572 | cpu_dump_state(env, logfile, fprintf, 0); |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 573 | #elif defined(TARGET_SPARC) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 574 | REGWPTR = env->regbase + (env->cwp * 16); |
| 575 | env->regwptr = REGWPTR; |
| 576 | cpu_dump_state(env, logfile, fprintf, 0); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 577 | #elif defined(TARGET_PPC) |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 578 | cpu_dump_state(env, logfile, fprintf, 0); |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 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); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 585 | #elif defined(TARGET_MIPS) |
| 586 | cpu_dump_state(env, logfile, fprintf, 0); |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 587 | #elif defined(TARGET_SH4) |
| 588 | cpu_dump_state(env, logfile, fprintf, 0); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 589 | #else |
| 590 | #error unsupported target CPU |
| 591 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 592 | } |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 593 | #endif |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 594 | tb = tb_find_fast(); |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 595 | #ifdef DEBUG_EXEC |
bellard | c1135f6 | 2005-01-30 22:41:54 +0000 | [diff] [blame] | 596 | if ((loglevel & CPU_LOG_EXEC)) { |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 597 | fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n", |
| 598 | (long)tb->tc_ptr, tb->pc, |
| 599 | lookup_symbol(tb->pc)); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 600 | } |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 601 | #endif |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 602 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 603 | T0 = tmp_T0; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 604 | #endif |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 605 | /* see if we can patch the calling TB. When the TB |
| 606 | spans two pages, we cannot safely do a direct |
| 607 | jump. */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 608 | { |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 609 | if (T0 != 0 && |
bellard | f32fc64 | 2006-02-08 22:43:39 +0000 | [diff] [blame] | 610 | #if USE_KQEMU |
| 611 | (env->kqemu_enabled != 2) && |
| 612 | #endif |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 613 | tb->page_addr[1] == -1 |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 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 | ) { |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 619 | spin_lock(&tb_lock); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 620 | tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb); |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 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 |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 626 | spin_unlock(&tb_lock); |
| 627 | } |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 628 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 629 | tc_ptr = tb->tc_ptr; |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 630 | env->current_tb = tb; |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 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) |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 638 | : "i0", "i1", "i2", "i3", "i4", "i5", |
ths | faab759 | 2007-03-19 20:39:49 +0000 | [diff] [blame] | 639 | "o0", "o1", "o2", "o3", "o4", "o5", |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 640 | "l0", "l1", "l2", "l3", "l4", "l5", |
| 641 | "l6", "l7"); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 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"); |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 649 | #elif defined(TARGET_I386) && defined(USE_CODE_COPY) |
| 650 | { |
| 651 | if (!(tb->cflags & CF_CODE_COPY)) { |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 652 | if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) { |
| 653 | save_native_fp_state(env); |
| 654 | } |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 655 | gen_func(); |
| 656 | } else { |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 657 | if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) { |
| 658 | restore_native_fp_state(env); |
| 659 | } |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 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 | } |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 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)(); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 732 | #else |
| 733 | gen_func(); |
| 734 | #endif |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 735 | env->current_tb = NULL; |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 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) |
bellard | 3f33731 | 2003-08-20 23:02:09 +0000 | [diff] [blame] | 739 | if (env->hflags & HF_SOFTMMU_MASK) { |
| 740 | env->hflags &= ~HF_SOFTMMU_MASK; |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 741 | /* do not allow linking to another block */ |
| 742 | T0 = 0; |
| 743 | } |
| 744 | #endif |
bellard | f32fc64 | 2006-02-08 22:43:39 +0000 | [diff] [blame] | 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 |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 752 | } |
| 753 | } else { |
bellard | 0d1a29f | 2004-10-12 22:01:28 +0000 | [diff] [blame] | 754 | env_to_regs(); |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 755 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 756 | } /* for(;;) */ |
| 757 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 758 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 759 | #if defined(TARGET_I386) |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 760 | #if defined(USE_CODE_COPY) |
| 761 | if (env->native_fp_regs) { |
| 762 | save_native_fp_state(env); |
| 763 | } |
| 764 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 765 | /* restore flags in standard format */ |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 766 | env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 767 | #elif defined(TARGET_ARM) |
bellard | b7bcbe9 | 2005-02-22 19:27:29 +0000 | [diff] [blame] | 768 | /* XXX: Save/restore host fpu exception state?. */ |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 769 | #elif defined(TARGET_SPARC) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 770 | #if defined(reg_REGWPTR) |
| 771 | REGWPTR = saved_regwptr; |
| 772 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 773 | #elif defined(TARGET_PPC) |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 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); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 779 | #elif defined(TARGET_MIPS) |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 780 | #elif defined(TARGET_SH4) |
| 781 | /* XXXXX */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 782 | #else |
| 783 | #error unsupported target CPU |
| 784 | #endif |
pbrook | 1057eaa | 2007-02-04 13:37:44 +0000 | [diff] [blame] | 785 | |
| 786 | /* restore global registers */ |
bellard | fdbb469 | 2006-06-14 17:32:25 +0000 | [diff] [blame] | 787 | #if defined(__sparc__) && !defined(HOST_SOLARIS) |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 788 | asm volatile ("mov %0, %%i7" : : "r" (saved_i7)); |
| 789 | #endif |
pbrook | 1057eaa | 2007-02-04 13:37:44 +0000 | [diff] [blame] | 790 | #include "hostregs_helper.h" |
| 791 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 792 | /* fail safe : never use cpu_single_env outside cpu_exec() */ |
| 793 | cpu_single_env = NULL; |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 794 | return ret; |
| 795 | } |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 796 | |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 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 | { |
bellard | dc5d0b3 | 2004-06-22 18:43:30 +0000 | [diff] [blame] | 801 | /* XXX: cannot enable it yet because it yields to MMU exception |
| 802 | where NIP != read address on PowerPC */ |
| 803 | #if 0 |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 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); |
bellard | dc5d0b3 | 2004-06-22 18:43:30 +0000 | [diff] [blame] | 807 | #endif |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 808 | } |
| 809 | |
bellard | 1a18c71 | 2003-10-30 01:07:51 +0000 | [diff] [blame] | 810 | #if defined(TARGET_I386) && defined(CONFIG_USER_ONLY) |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 811 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 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; |
bellard | a412ac5 | 2003-07-26 18:01:40 +0000 | [diff] [blame] | 818 | if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) { |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 819 | selector &= 0xffff; |
bellard | 2e255c6 | 2003-08-21 23:25:21 +0000 | [diff] [blame] | 820 | cpu_x86_load_seg_cache(env, seg_reg, selector, |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 821 | (selector << 4), 0xffff, 0); |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 822 | } else { |
bellard | b453b70 | 2004-01-04 15:45:21 +0000 | [diff] [blame] | 823 | load_seg(seg_reg, selector); |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 824 | } |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 825 | env = saved_env; |
| 826 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 827 | |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 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 | |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 835 | helper_fsave((target_ulong)ptr, data32); |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 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 | |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 847 | helper_frstor((target_ulong)ptr, data32); |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 848 | |
| 849 | env = saved_env; |
| 850 | } |
| 851 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 852 | #endif /* TARGET_I386 */ |
| 853 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 854 | #if !defined(CONFIG_SOFTMMU) |
| 855 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 856 | #if defined(TARGET_I386) |
| 857 | |
bellard | b56dad1 | 2003-05-08 15:38:04 +0000 | [diff] [blame] | 858 | /* 'pc' is the host PC at which the exception was raised. 'address' is |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 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 */ |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 862 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 863 | int is_write, sigset_t *old_set, |
| 864 | void *puc) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 865 | { |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 866 | TranslationBlock *tb; |
| 867 | int ret; |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 868 | |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 869 | if (cpu_single_env) |
| 870 | env = cpu_single_env; /* XXX: find a correct solution for multithread */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 871 | #if defined(DEBUG_SIGNAL) |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 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); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 874 | #endif |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 875 | /* XXX: locking issue */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 876 | if (is_write && page_unprotect(h2g(address), pc, puc)) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 877 | return 1; |
| 878 | } |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 879 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 880 | /* see if it is an MMU fault */ |
bellard | 93a40ea | 2003-10-27 21:13:06 +0000 | [diff] [blame] | 881 | ret = cpu_x86_handle_mmu_fault(env, address, is_write, |
| 882 | ((env->hflags & HF_CPL_MASK) == 3), 0); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 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 */ |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 888 | tb = tb_find_pc(pc); |
| 889 | if (tb) { |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 890 | /* the PC is inside the translated code. It means that we have |
| 891 | a virtual CPU fault */ |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 892 | cpu_restore_state(tb, env, pc, puc); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 893 | } |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 894 | if (ret == 1) { |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 895 | #if 0 |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 896 | printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n", |
| 897 | env->eip, env->cr[2], env->error_code); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 898 | #endif |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 899 | /* we restore the process signal mask as the sigreturn should |
| 900 | do it (XXX: use sigsetjmp) */ |
| 901 | sigprocmask(SIG_SETMASK, old_set, NULL); |
bellard | 54ca909 | 2005-12-04 18:46:06 +0000 | [diff] [blame] | 902 | raise_exception_err(env->exception_index, env->error_code); |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 903 | } else { |
| 904 | /* activate soft MMU for this block */ |
bellard | 3f33731 | 2003-08-20 23:02:09 +0000 | [diff] [blame] | 905 | env->hflags |= HF_SOFTMMU_MASK; |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 906 | cpu_resume_from_signal(env, puc); |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 907 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 908 | /* never comes here */ |
| 909 | return 1; |
| 910 | } |
| 911 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 912 | #elif defined(TARGET_ARM) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 913 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 914 | int is_write, sigset_t *old_set, |
| 915 | void *puc) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 916 | { |
bellard | 68016c6 | 2005-02-07 23:12:27 +0000 | [diff] [blame] | 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 |
bellard | 9f0777e | 2005-02-02 20:42:01 +0000 | [diff] [blame] | 926 | /* XXX: locking issue */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 927 | if (is_write && page_unprotect(h2g(address), pc, puc)) { |
bellard | 9f0777e | 2005-02-02 20:42:01 +0000 | [diff] [blame] | 928 | return 1; |
| 929 | } |
bellard | 68016c6 | 2005-02-07 23:12:27 +0000 | [diff] [blame] | 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(); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 947 | } |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 948 | #elif defined(TARGET_SPARC) |
| 949 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 950 | int is_write, sigset_t *old_set, |
| 951 | void *puc) |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 952 | { |
bellard | 68016c6 | 2005-02-07 23:12:27 +0000 | [diff] [blame] | 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 |
bellard | b453b70 | 2004-01-04 15:45:21 +0000 | [diff] [blame] | 962 | /* XXX: locking issue */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 963 | if (is_write && page_unprotect(h2g(address), pc, puc)) { |
bellard | b453b70 | 2004-01-04 15:45:21 +0000 | [diff] [blame] | 964 | return 1; |
| 965 | } |
bellard | 68016c6 | 2005-02-07 23:12:27 +0000 | [diff] [blame] | 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(); |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 983 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 984 | #elif defined (TARGET_PPC) |
| 985 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 986 | int is_write, sigset_t *old_set, |
| 987 | void *puc) |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 988 | { |
| 989 | TranslationBlock *tb; |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 990 | int ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 991 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 992 | if (cpu_single_env) |
| 993 | env = cpu_single_env; /* XXX: find a correct solution for multithread */ |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 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 */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 999 | if (is_write && page_unprotect(h2g(address), pc, puc)) { |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 1000 | return 1; |
| 1001 | } |
| 1002 | |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 1003 | /* see if it is an MMU fault */ |
bellard | 7f957d2 | 2004-01-18 23:19:48 +0000 | [diff] [blame] | 1004 | ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0); |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 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 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 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 */ |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1015 | cpu_restore_state(tb, env, pc, puc); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 1016 | } |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 1017 | if (ret == 1) { |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 1018 | #if 0 |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 1019 | printf("PF exception: NIP=0x%08x error=0x%x %p\n", |
| 1020 | env->nip, env->error_code, tb); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 1021 | #endif |
| 1022 | /* we restore the process signal mask as the sigreturn should |
| 1023 | do it (XXX: use sigsetjmp) */ |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1024 | sigprocmask(SIG_SETMASK, old_set, NULL); |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 1025 | do_raise_exception_err(env->exception_index, env->error_code); |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 1026 | } else { |
| 1027 | /* activate soft MMU for this block */ |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 1028 | cpu_resume_from_signal(env, puc); |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 1029 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 1030 | /* never comes here */ |
| 1031 | return 1; |
| 1032 | } |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 1033 | |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 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 | |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 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 */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1088 | if (is_write && page_unprotect(h2g(address), pc, puc)) { |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 1089 | return 1; |
| 1090 | } |
| 1091 | |
| 1092 | /* see if it is an MMU fault */ |
bellard | cc9442b | 2005-11-26 18:43:28 +0000 | [diff] [blame] | 1093 | ret = cpu_mips_handle_mmu_fault(env, address, is_write, 1, 0); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 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 | |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 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 | } |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 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) */ |
pbrook | 355fb23 | 2006-06-17 19:58:25 +0000 | [diff] [blame] | 1162 | sigprocmask(SIG_SETMASK, old_set, NULL); |
| 1163 | cpu_loop_exit(); |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 1164 | /* never comes here */ |
| 1165 | return 1; |
| 1166 | } |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1167 | #else |
| 1168 | #error unsupported target CPU |
| 1169 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1170 | |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1171 | #if defined(__i386__) |
| 1172 | |
bellard | d8ecc0b | 2007-02-05 21:41:46 +0000 | [diff] [blame] | 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 | |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 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 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1205 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1206 | void *puc) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1207 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1208 | siginfo_t *info = pinfo; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1209 | struct ucontext *uc = puc; |
| 1210 | unsigned long pc; |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1211 | int trapno; |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 1212 | |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 1213 | #ifndef REG_EIP |
| 1214 | /* for glibc 2.1 */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1215 | #define REG_EIP EIP |
| 1216 | #define REG_ERR ERR |
| 1217 | #define REG_TRAPNO TRAPNO |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 1218 | #endif |
bellard | d8ecc0b | 2007-02-05 21:41:46 +0000 | [diff] [blame] | 1219 | pc = EIP_sig(uc); |
| 1220 | trapno = TRAP_sig(uc); |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 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 ? |
bellard | d8ecc0b | 2007-02-05 21:41:46 +0000 | [diff] [blame] | 1230 | (ERROR_sig(uc) >> 1) & 1 : 0, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1231 | &uc->uc_sigmask, puc); |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 1234 | #elif defined(__x86_64__) |
| 1235 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1236 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 1237 | void *puc) |
| 1238 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1239 | siginfo_t *info = pinfo; |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 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 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1250 | #elif defined(__powerpc__) |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1251 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 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 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1301 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1302 | void *puc) |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1303 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1304 | siginfo_t *info = pinfo; |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1305 | struct ucontext *uc = puc; |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1306 | unsigned long pc; |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1307 | int is_write; |
| 1308 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1309 | pc = IAR_sig(uc); |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1310 | is_write = 0; |
| 1311 | #if 0 |
| 1312 | /* ppc 4xx case */ |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1313 | if (DSISR_sig(uc) & 0x00800000) |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1314 | is_write = 1; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1315 | #else |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1316 | if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1317 | is_write = 1; |
| 1318 | #endif |
| 1319 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1320 | is_write, &uc->uc_sigmask, puc); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1321 | } |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1322 | |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1323 | #elif defined(__alpha__) |
| 1324 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1325 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1326 | void *puc) |
| 1327 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1328 | siginfo_t *info = pinfo; |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 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 | |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1334 | /* XXX: need kernel patch to get write flag faster */ |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 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, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1351 | is_write, &uc->uc_sigmask, puc); |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1352 | } |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1353 | #elif defined(__sparc__) |
| 1354 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1355 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1356 | void *puc) |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1357 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1358 | siginfo_t *info = pinfo; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 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, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1384 | is_write, sigmask, NULL); |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | #elif defined(__arm__) |
| 1388 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1389 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1390 | void *puc) |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1391 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1392 | siginfo_t *info = pinfo; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 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, |
pbrook | f3a9676 | 2006-07-29 19:09:31 +0000 | [diff] [blame] | 1402 | &uc->uc_sigmask, puc); |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1405 | #elif defined(__mc68000) |
| 1406 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1407 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1408 | void *puc) |
| 1409 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1410 | siginfo_t *info = pinfo; |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 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, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1420 | &uc->uc_sigmask, puc); |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1423 | #elif defined(__ia64) |
| 1424 | |
| 1425 | #ifndef __ISR_VALID |
| 1426 | /* This ought to be in <bits/siginfo.h>... */ |
| 1427 | # define __ISR_VALID 1 |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1428 | #endif |
| 1429 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1430 | int cpu_signal_handler(int host_signum, void *pinfo, void *puc) |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1431 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1432 | siginfo_t *info = pinfo; |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 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: |
bellard | fd4a43e | 2006-04-24 20:32:17 +0000 | [diff] [blame] | 1444 | if (info->si_code && (info->si_segvflags & __ISR_VALID)) |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 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 | |
bellard | 90cb949 | 2005-07-24 15:11:38 +0000 | [diff] [blame] | 1457 | #elif defined(__s390__) |
| 1458 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1459 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | 90cb949 | 2005-07-24 15:11:38 +0000 | [diff] [blame] | 1460 | void *puc) |
| 1461 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1462 | siginfo_t *info = pinfo; |
bellard | 90cb949 | 2005-07-24 15:11:38 +0000 | [diff] [blame] | 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 | |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1475 | #else |
| 1476 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 1477 | #error host CPU specific signal handler needed |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1478 | |
| 1479 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1480 | |
| 1481 | #endif /* !defined(CONFIG_SOFTMMU) */ |