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