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