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