bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * i386 emulator main execution loop |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 18 | */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 19 | #include "config.h" |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 20 | #include "exec.h" |
bellard | 956034d | 2003-04-29 20:40:53 +0000 | [diff] [blame] | 21 | #include "disas.h" |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 22 | #include "tcg.h" |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 23 | #include "kvm.h" |
Jan Kiszka | 1d93f0f | 2010-06-25 16:56:49 +0200 | [diff] [blame] | 24 | #include "qemu-barrier.h" |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 25 | |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 26 | #if !defined(CONFIG_SOFTMMU) |
| 27 | #undef EAX |
| 28 | #undef ECX |
| 29 | #undef EDX |
| 30 | #undef EBX |
| 31 | #undef ESP |
| 32 | #undef EBP |
| 33 | #undef ESI |
| 34 | #undef EDI |
| 35 | #undef EIP |
| 36 | #include <signal.h> |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 37 | #ifdef __linux__ |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 38 | #include <sys/ucontext.h> |
| 39 | #endif |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 40 | #endif |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 41 | |
Juan Quintela | dfe5fff | 2009-07-27 16:12:40 +0200 | [diff] [blame] | 42 | #if defined(__sparc__) && !defined(CONFIG_SOLARIS) |
blueswir1 | 572a9d4 | 2008-05-17 07:38:10 +0000 | [diff] [blame] | 43 | // Work around ugly bugs in glibc that mangle global register contents |
| 44 | #undef env |
| 45 | #define env cpu_single_env |
| 46 | #endif |
| 47 | |
bellard | 36bdbe5 | 2003-11-19 22:12:02 +0000 | [diff] [blame] | 48 | int tb_invalidated_flag; |
| 49 | |
Juan Quintela | f0667e6 | 2009-07-27 16:13:05 +0200 | [diff] [blame] | 50 | //#define CONFIG_DEBUG_EXEC |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 51 | //#define DEBUG_SIGNAL |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 52 | |
aliguori | 6a4955a | 2009-04-24 18:03:20 +0000 | [diff] [blame] | 53 | int qemu_cpu_has_work(CPUState *env) |
| 54 | { |
| 55 | return cpu_has_work(env); |
| 56 | } |
| 57 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 58 | void cpu_loop_exit(void) |
| 59 | { |
Paolo Bonzini | 1c3569f | 2010-01-15 09:42:07 +0100 | [diff] [blame] | 60 | env->current_tb = NULL; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 61 | longjmp(env->jmp_env, 1); |
| 62 | } |
ths | bfed01f | 2007-06-03 17:44:37 +0000 | [diff] [blame] | 63 | |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 64 | /* exit the current TB from a signal handler. The host registers are |
| 65 | restored in a state compatible with the CPU emulator |
| 66 | */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 67 | #if defined(CONFIG_SOFTMMU) |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 68 | void cpu_resume_from_signal(CPUState *env1, void *puc) |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 69 | { |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 70 | env = env1; |
| 71 | |
| 72 | /* XXX: restore cpu registers saved in host registers */ |
| 73 | |
| 74 | env->exception_index = -1; |
| 75 | longjmp(env->jmp_env, 1); |
| 76 | } |
| 77 | |
| 78 | #else |
| 79 | |
| 80 | void cpu_resume_from_signal(CPUState *env1, void *puc) |
| 81 | { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 82 | #ifdef __linux__ |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 83 | struct ucontext *uc = puc; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 84 | #elif defined(__OpenBSD__) |
| 85 | struct sigcontext *uc = puc; |
| 86 | #endif |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 87 | |
| 88 | env = env1; |
| 89 | |
| 90 | /* XXX: restore cpu registers saved in host registers */ |
| 91 | |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 92 | if (puc) { |
| 93 | /* XXX: use siglongjmp ? */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 94 | #ifdef __linux__ |
Aurelien Jarno | 60e9924 | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 95 | #ifdef __ia64 |
| 96 | sigprocmask(SIG_SETMASK, (sigset_t *)&uc->uc_sigmask, NULL); |
| 97 | #else |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 98 | sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); |
Aurelien Jarno | 60e9924 | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 99 | #endif |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 100 | #elif defined(__OpenBSD__) |
| 101 | sigprocmask(SIG_SETMASK, &uc->sc_mask, NULL); |
| 102 | #endif |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 103 | } |
pbrook | 9a3ea65 | 2008-12-19 12:49:13 +0000 | [diff] [blame] | 104 | env->exception_index = -1; |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 105 | longjmp(env->jmp_env, 1); |
| 106 | } |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 107 | #endif |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 108 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 109 | /* Execute the code without caching the generated code. An interpreter |
| 110 | could be used if available. */ |
| 111 | static void cpu_exec_nocache(int max_cycles, TranslationBlock *orig_tb) |
| 112 | { |
| 113 | unsigned long next_tb; |
| 114 | TranslationBlock *tb; |
| 115 | |
| 116 | /* Should never happen. |
| 117 | We only end up here when an existing TB is too long. */ |
| 118 | if (max_cycles > CF_COUNT_MASK) |
| 119 | max_cycles = CF_COUNT_MASK; |
| 120 | |
| 121 | tb = tb_gen_code(env, orig_tb->pc, orig_tb->cs_base, orig_tb->flags, |
| 122 | max_cycles); |
| 123 | env->current_tb = tb; |
| 124 | /* execute the generated code */ |
| 125 | next_tb = tcg_qemu_tb_exec(tb->tc_ptr); |
Paolo Bonzini | 1c3569f | 2010-01-15 09:42:07 +0100 | [diff] [blame] | 126 | env->current_tb = NULL; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 127 | |
| 128 | if ((next_tb & 3) == 2) { |
| 129 | /* Restore PC. This may happen if async event occurs before |
| 130 | the TB starts executing. */ |
aliguori | 622ed36 | 2008-11-18 19:36:03 +0000 | [diff] [blame] | 131 | cpu_pc_from_tb(env, tb); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 132 | } |
| 133 | tb_phys_invalidate(tb, -1); |
| 134 | tb_free(tb); |
| 135 | } |
| 136 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 137 | static TranslationBlock *tb_find_slow(target_ulong pc, |
| 138 | target_ulong cs_base, |
j_mayer | c068688 | 2007-09-20 22:47:42 +0000 | [diff] [blame] | 139 | uint64_t flags) |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 140 | { |
| 141 | TranslationBlock *tb, **ptb1; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 142 | unsigned int h; |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 143 | tb_page_addr_t phys_pc, phys_page1, phys_page2; |
| 144 | target_ulong virt_page2; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 145 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 146 | tb_invalidated_flag = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 147 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 148 | /* find translated block using physical mappings */ |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 149 | phys_pc = get_page_addr_code(env, pc); |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 150 | phys_page1 = phys_pc & TARGET_PAGE_MASK; |
| 151 | phys_page2 = -1; |
| 152 | h = tb_phys_hash_func(phys_pc); |
| 153 | ptb1 = &tb_phys_hash[h]; |
| 154 | for(;;) { |
| 155 | tb = *ptb1; |
| 156 | if (!tb) |
| 157 | goto not_found; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 158 | if (tb->pc == pc && |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 159 | tb->page_addr[0] == phys_page1 && |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 160 | tb->cs_base == cs_base && |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 161 | tb->flags == flags) { |
| 162 | /* check next page if needed */ |
| 163 | if (tb->page_addr[1] != -1) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 164 | virt_page2 = (pc & TARGET_PAGE_MASK) + |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 165 | TARGET_PAGE_SIZE; |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 166 | phys_page2 = get_page_addr_code(env, virt_page2); |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 167 | if (tb->page_addr[1] == phys_page2) |
| 168 | goto found; |
| 169 | } else { |
| 170 | goto found; |
| 171 | } |
| 172 | } |
| 173 | ptb1 = &tb->phys_hash_next; |
| 174 | } |
| 175 | not_found: |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 176 | /* if no translated code available, then translate it now */ |
| 177 | tb = tb_gen_code(env, pc, cs_base, flags, 0); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 178 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 179 | found: |
Kirill Batuzov | 2c90fe2 | 2010-12-02 16:12:46 +0300 | [diff] [blame] | 180 | /* Move the last found TB to the head of the list */ |
| 181 | if (likely(*ptb1)) { |
| 182 | *ptb1 = tb->phys_hash_next; |
| 183 | tb->phys_hash_next = tb_phys_hash[h]; |
| 184 | tb_phys_hash[h] = tb; |
| 185 | } |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 186 | /* we add the TB in the virtual pc hash table */ |
| 187 | env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)] = tb; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 188 | return tb; |
| 189 | } |
| 190 | |
| 191 | static inline TranslationBlock *tb_find_fast(void) |
| 192 | { |
| 193 | TranslationBlock *tb; |
| 194 | target_ulong cs_base, pc; |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 195 | int flags; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 196 | |
| 197 | /* we record a subset of the CPU state. It will |
| 198 | always be the same before a given translated block |
| 199 | is executed. */ |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 200 | cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags); |
bellard | bce6184 | 2008-02-01 22:18:51 +0000 | [diff] [blame] | 201 | tb = env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)]; |
ths | 551bd27 | 2008-07-03 17:57:36 +0000 | [diff] [blame] | 202 | if (unlikely(!tb || tb->pc != pc || tb->cs_base != cs_base || |
| 203 | tb->flags != flags)) { |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 204 | tb = tb_find_slow(pc, cs_base, flags); |
| 205 | } |
| 206 | return tb; |
| 207 | } |
| 208 | |
Jan Kiszka | 1009d2e | 2011-03-15 12:26:13 +0100 | [diff] [blame] | 209 | static CPUDebugExcpHandler *debug_excp_handler; |
| 210 | |
| 211 | CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler) |
| 212 | { |
| 213 | CPUDebugExcpHandler *old_handler = debug_excp_handler; |
| 214 | |
| 215 | debug_excp_handler = handler; |
| 216 | return old_handler; |
| 217 | } |
| 218 | |
| 219 | static void cpu_handle_debug_exception(CPUState *env) |
| 220 | { |
| 221 | CPUWatchpoint *wp; |
| 222 | |
| 223 | if (!env->watchpoint_hit) { |
| 224 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
| 225 | wp->flags &= ~BP_WATCHPOINT_HIT; |
| 226 | } |
| 227 | } |
| 228 | if (debug_excp_handler) { |
| 229 | debug_excp_handler(env); |
| 230 | } |
| 231 | } |
| 232 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 233 | /* main execution loop */ |
| 234 | |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 235 | volatile sig_atomic_t exit_request; |
| 236 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 237 | int cpu_exec(CPUState *env1) |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 238 | { |
Paolo Bonzini | 1d9000e | 2010-02-23 19:21:00 +0100 | [diff] [blame] | 239 | volatile host_reg_t saved_env_reg; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 240 | int ret, interrupt_request; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 241 | TranslationBlock *tb; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 242 | uint8_t *tc_ptr; |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 243 | unsigned long next_tb; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 244 | |
Paolo Bonzini | eda48c3 | 2011-03-12 17:43:56 +0100 | [diff] [blame] | 245 | if (env1->halted) { |
| 246 | if (!cpu_has_work(env1)) { |
| 247 | return EXCP_HALTED; |
| 248 | } |
| 249 | |
| 250 | env1->halted = 0; |
| 251 | } |
bellard | 5a1e3cf | 2005-11-23 21:02:53 +0000 | [diff] [blame] | 252 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 253 | cpu_single_env = env1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 254 | |
Paolo Bonzini | 24ebf5f | 2010-02-18 21:25:23 +0100 | [diff] [blame] | 255 | /* the access to env below is actually saving the global register's |
| 256 | value, so that files not including target-xyz/exec.h are free to |
| 257 | use it. */ |
| 258 | QEMU_BUILD_BUG_ON (sizeof (saved_env_reg) != sizeof (env)); |
| 259 | saved_env_reg = (host_reg_t) env; |
Jan Kiszka | 1d93f0f | 2010-06-25 16:56:49 +0200 | [diff] [blame] | 260 | barrier(); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 261 | env = env1; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 262 | |
Jan Kiszka | c629a4b | 2010-06-25 16:56:52 +0200 | [diff] [blame] | 263 | if (unlikely(exit_request)) { |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 264 | env->exit_request = 1; |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 265 | } |
| 266 | |
ths | ecb644f | 2007-06-03 18:45:53 +0000 | [diff] [blame] | 267 | #if defined(TARGET_I386) |
Jan Kiszka | 6792a57 | 2011-02-07 12:19:18 +0100 | [diff] [blame] | 268 | /* put eflags in CPU temporary format */ |
| 269 | CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
| 270 | DF = 1 - (2 * ((env->eflags >> 10) & 1)); |
| 271 | CC_OP = CC_OP_EFLAGS; |
| 272 | env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 273 | #elif defined(TARGET_SPARC) |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 274 | #elif defined(TARGET_M68K) |
| 275 | env->cc_op = CC_OP_FLAGS; |
| 276 | env->cc_dest = env->sr & 0xf; |
| 277 | env->cc_x = (env->sr >> 4) & 1; |
ths | ecb644f | 2007-06-03 18:45:53 +0000 | [diff] [blame] | 278 | #elif defined(TARGET_ALPHA) |
| 279 | #elif defined(TARGET_ARM) |
Guan Xuetao | d2fbca9 | 2011-04-12 16:27:03 +0800 | [diff] [blame] | 280 | #elif defined(TARGET_UNICORE32) |
ths | ecb644f | 2007-06-03 18:45:53 +0000 | [diff] [blame] | 281 | #elif defined(TARGET_PPC) |
Michael Walle | 81ea0e1 | 2011-02-17 23:45:02 +0100 | [diff] [blame] | 282 | #elif defined(TARGET_LM32) |
Edgar E. Iglesias | b779e29 | 2009-05-20 21:31:33 +0200 | [diff] [blame] | 283 | #elif defined(TARGET_MICROBLAZE) |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 284 | #elif defined(TARGET_MIPS) |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 285 | #elif defined(TARGET_SH4) |
ths | f1ccf90 | 2007-10-08 13:16:14 +0000 | [diff] [blame] | 286 | #elif defined(TARGET_CRIS) |
Alexander Graf | 10ec511 | 2009-12-05 12:44:21 +0100 | [diff] [blame] | 287 | #elif defined(TARGET_S390X) |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 288 | /* XXXXX */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 289 | #else |
| 290 | #error unsupported target CPU |
| 291 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 292 | env->exception_index = -1; |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 293 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 294 | /* prepare setjmp context for exception handling */ |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 295 | for(;;) { |
| 296 | if (setjmp(env->jmp_env) == 0) { |
Juan Quintela | dfe5fff | 2009-07-27 16:12:40 +0200 | [diff] [blame] | 297 | #if defined(__sparc__) && !defined(CONFIG_SOLARIS) |
blueswir1 | 9ddff3d | 2009-04-04 07:41:20 +0000 | [diff] [blame] | 298 | #undef env |
Jan Kiszka | 6792a57 | 2011-02-07 12:19:18 +0100 | [diff] [blame] | 299 | env = cpu_single_env; |
blueswir1 | 9ddff3d | 2009-04-04 07:41:20 +0000 | [diff] [blame] | 300 | #define env cpu_single_env |
| 301 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 302 | /* if an exception is pending, we execute it here */ |
| 303 | if (env->exception_index >= 0) { |
| 304 | if (env->exception_index >= EXCP_INTERRUPT) { |
| 305 | /* exit request from the cpu execution loop */ |
| 306 | ret = env->exception_index; |
Jan Kiszka | 1009d2e | 2011-03-15 12:26:13 +0100 | [diff] [blame] | 307 | if (ret == EXCP_DEBUG) { |
| 308 | cpu_handle_debug_exception(env); |
| 309 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 310 | break; |
aurel32 | 72d239e | 2009-01-14 19:40:27 +0000 | [diff] [blame] | 311 | } else { |
| 312 | #if defined(CONFIG_USER_ONLY) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 313 | /* if user mode only, we simulate a fake exception |
ths | 9f08349 | 2006-12-07 18:28:42 +0000 | [diff] [blame] | 314 | which will be handled outside the cpu execution |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 315 | loop */ |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 316 | #if defined(TARGET_I386) |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 317 | do_interrupt_user(env->exception_index, |
| 318 | env->exception_is_int, |
| 319 | env->error_code, |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 320 | env->exception_next_eip); |
bellard | eba0162 | 2008-05-12 12:04:40 +0000 | [diff] [blame] | 321 | /* successfully delivered */ |
| 322 | env->old_exception = -1; |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 323 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 324 | ret = env->exception_index; |
| 325 | break; |
aurel32 | 72d239e | 2009-01-14 19:40:27 +0000 | [diff] [blame] | 326 | #else |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 327 | #if defined(TARGET_I386) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 328 | /* simulate a real cpu exception. On i386, it can |
| 329 | trigger new exceptions, but we do not handle |
| 330 | double or triple faults yet. */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 331 | do_interrupt(env->exception_index, |
| 332 | env->exception_is_int, |
| 333 | env->error_code, |
bellard | d05e66d | 2003-08-20 21:34:35 +0000 | [diff] [blame] | 334 | env->exception_next_eip, 0); |
ths | 678dde1 | 2007-03-31 20:28:52 +0000 | [diff] [blame] | 335 | /* successfully delivered */ |
| 336 | env->old_exception = -1; |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 337 | #elif defined(TARGET_PPC) |
| 338 | do_interrupt(env); |
Michael Walle | 81ea0e1 | 2011-02-17 23:45:02 +0100 | [diff] [blame] | 339 | #elif defined(TARGET_LM32) |
| 340 | do_interrupt(env); |
Edgar E. Iglesias | b779e29 | 2009-05-20 21:31:33 +0200 | [diff] [blame] | 341 | #elif defined(TARGET_MICROBLAZE) |
| 342 | do_interrupt(env); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 343 | #elif defined(TARGET_MIPS) |
| 344 | do_interrupt(env); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 345 | #elif defined(TARGET_SPARC) |
blueswir1 | f2bc7e7 | 2008-05-27 17:35:30 +0000 | [diff] [blame] | 346 | do_interrupt(env); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 347 | #elif defined(TARGET_ARM) |
| 348 | do_interrupt(env); |
Guan Xuetao | d2fbca9 | 2011-04-12 16:27:03 +0800 | [diff] [blame] | 349 | #elif defined(TARGET_UNICORE32) |
| 350 | do_interrupt(env); |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 351 | #elif defined(TARGET_SH4) |
| 352 | do_interrupt(env); |
j_mayer | eddf68a | 2007-04-05 07:22:49 +0000 | [diff] [blame] | 353 | #elif defined(TARGET_ALPHA) |
| 354 | do_interrupt(env); |
ths | f1ccf90 | 2007-10-08 13:16:14 +0000 | [diff] [blame] | 355 | #elif defined(TARGET_CRIS) |
| 356 | do_interrupt(env); |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 357 | #elif defined(TARGET_M68K) |
| 358 | do_interrupt(0); |
Alexander Graf | 3110e29 | 2011-04-15 17:32:48 +0200 | [diff] [blame] | 359 | #elif defined(TARGET_S390X) |
| 360 | do_interrupt(env); |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 361 | #endif |
Paolo Bonzini | 301d290 | 2010-01-15 09:41:01 +0100 | [diff] [blame] | 362 | env->exception_index = -1; |
aurel32 | 72d239e | 2009-01-14 19:40:27 +0000 | [diff] [blame] | 363 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 364 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 365 | } |
bellard | 9df217a | 2005-02-10 22:05:51 +0000 | [diff] [blame] | 366 | |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 367 | next_tb = 0; /* force lookup of first TB */ |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 368 | for(;;) { |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 369 | interrupt_request = env->interrupt_request; |
malc | e1638bd | 2008-11-06 18:54:46 +0000 | [diff] [blame] | 370 | if (unlikely(interrupt_request)) { |
| 371 | if (unlikely(env->singlestep_enabled & SSTEP_NOIRQ)) { |
| 372 | /* Mask out external interrupts for this step. */ |
Richard Henderson | 3125f76 | 2011-05-04 13:34:25 -0700 | [diff] [blame] | 373 | interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK; |
malc | e1638bd | 2008-11-06 18:54:46 +0000 | [diff] [blame] | 374 | } |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 375 | if (interrupt_request & CPU_INTERRUPT_DEBUG) { |
| 376 | env->interrupt_request &= ~CPU_INTERRUPT_DEBUG; |
| 377 | env->exception_index = EXCP_DEBUG; |
| 378 | cpu_loop_exit(); |
| 379 | } |
balrog | a90b731 | 2007-05-01 01:28:01 +0000 | [diff] [blame] | 380 | #if defined(TARGET_ARM) || defined(TARGET_SPARC) || defined(TARGET_MIPS) || \ |
Edgar E. Iglesias | b779e29 | 2009-05-20 21:31:33 +0200 | [diff] [blame] | 381 | defined(TARGET_PPC) || defined(TARGET_ALPHA) || defined(TARGET_CRIS) || \ |
Guan Xuetao | d2fbca9 | 2011-04-12 16:27:03 +0800 | [diff] [blame] | 382 | defined(TARGET_MICROBLAZE) || defined(TARGET_LM32) || defined(TARGET_UNICORE32) |
balrog | a90b731 | 2007-05-01 01:28:01 +0000 | [diff] [blame] | 383 | if (interrupt_request & CPU_INTERRUPT_HALT) { |
| 384 | env->interrupt_request &= ~CPU_INTERRUPT_HALT; |
| 385 | env->halted = 1; |
| 386 | env->exception_index = EXCP_HLT; |
| 387 | cpu_loop_exit(); |
| 388 | } |
| 389 | #endif |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 390 | #if defined(TARGET_I386) |
Gleb Natapov | b09ea7d | 2009-06-17 23:26:59 +0300 | [diff] [blame] | 391 | if (interrupt_request & CPU_INTERRUPT_INIT) { |
| 392 | svm_check_intercept(SVM_EXIT_INIT); |
| 393 | do_cpu_init(env); |
| 394 | env->exception_index = EXCP_HALTED; |
| 395 | cpu_loop_exit(); |
| 396 | } else if (interrupt_request & CPU_INTERRUPT_SIPI) { |
| 397 | do_cpu_sipi(env); |
| 398 | } else if (env->hflags2 & HF2_GIF_MASK) { |
bellard | db620f4 | 2008-06-04 17:02:19 +0000 | [diff] [blame] | 399 | if ((interrupt_request & CPU_INTERRUPT_SMI) && |
| 400 | !(env->hflags & HF_SMM_MASK)) { |
| 401 | svm_check_intercept(SVM_EXIT_SMI); |
| 402 | env->interrupt_request &= ~CPU_INTERRUPT_SMI; |
| 403 | do_smm_enter(); |
| 404 | next_tb = 0; |
| 405 | } else if ((interrupt_request & CPU_INTERRUPT_NMI) && |
| 406 | !(env->hflags2 & HF2_NMI_MASK)) { |
| 407 | env->interrupt_request &= ~CPU_INTERRUPT_NMI; |
| 408 | env->hflags2 |= HF2_NMI_MASK; |
| 409 | do_interrupt(EXCP02_NMI, 0, 0, 0, 1); |
| 410 | next_tb = 0; |
Huang Ying | 79c4f6b | 2009-06-23 10:05:14 +0800 | [diff] [blame] | 411 | } else if (interrupt_request & CPU_INTERRUPT_MCE) { |
| 412 | env->interrupt_request &= ~CPU_INTERRUPT_MCE; |
| 413 | do_interrupt(EXCP12_MCHK, 0, 0, 0, 0); |
| 414 | next_tb = 0; |
bellard | db620f4 | 2008-06-04 17:02:19 +0000 | [diff] [blame] | 415 | } else if ((interrupt_request & CPU_INTERRUPT_HARD) && |
| 416 | (((env->hflags2 & HF2_VINTR_MASK) && |
| 417 | (env->hflags2 & HF2_HIF_MASK)) || |
| 418 | (!(env->hflags2 & HF2_VINTR_MASK) && |
| 419 | (env->eflags & IF_MASK && |
| 420 | !(env->hflags & HF_INHIBIT_IRQ_MASK))))) { |
| 421 | int intno; |
| 422 | svm_check_intercept(SVM_EXIT_INTR); |
| 423 | env->interrupt_request &= ~(CPU_INTERRUPT_HARD | CPU_INTERRUPT_VIRQ); |
| 424 | intno = cpu_get_pic_interrupt(env); |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 425 | qemu_log_mask(CPU_LOG_TB_IN_ASM, "Servicing hardware INT=0x%02x\n", intno); |
Juan Quintela | dfe5fff | 2009-07-27 16:12:40 +0200 | [diff] [blame] | 426 | #if defined(__sparc__) && !defined(CONFIG_SOLARIS) |
blueswir1 | 9ddff3d | 2009-04-04 07:41:20 +0000 | [diff] [blame] | 427 | #undef env |
| 428 | env = cpu_single_env; |
| 429 | #define env cpu_single_env |
| 430 | #endif |
bellard | db620f4 | 2008-06-04 17:02:19 +0000 | [diff] [blame] | 431 | do_interrupt(intno, 0, 0, 0, 1); |
| 432 | /* ensure that no TB jump will be modified as |
| 433 | the program flow was changed */ |
| 434 | next_tb = 0; |
ths | 0573fbf | 2007-09-23 15:28:04 +0000 | [diff] [blame] | 435 | #if !defined(CONFIG_USER_ONLY) |
bellard | db620f4 | 2008-06-04 17:02:19 +0000 | [diff] [blame] | 436 | } else if ((interrupt_request & CPU_INTERRUPT_VIRQ) && |
| 437 | (env->eflags & IF_MASK) && |
| 438 | !(env->hflags & HF_INHIBIT_IRQ_MASK)) { |
| 439 | int intno; |
| 440 | /* FIXME: this should respect TPR */ |
| 441 | svm_check_intercept(SVM_EXIT_VINTR); |
bellard | db620f4 | 2008-06-04 17:02:19 +0000 | [diff] [blame] | 442 | intno = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_vector)); |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 443 | qemu_log_mask(CPU_LOG_TB_IN_ASM, "Servicing virtual hardware INT=0x%02x\n", intno); |
bellard | db620f4 | 2008-06-04 17:02:19 +0000 | [diff] [blame] | 444 | do_interrupt(intno, 0, 0, 0, 1); |
aurel32 | d40c54d | 2008-12-13 12:33:02 +0000 | [diff] [blame] | 445 | env->interrupt_request &= ~CPU_INTERRUPT_VIRQ; |
bellard | db620f4 | 2008-06-04 17:02:19 +0000 | [diff] [blame] | 446 | next_tb = 0; |
ths | 0573fbf | 2007-09-23 15:28:04 +0000 | [diff] [blame] | 447 | #endif |
bellard | db620f4 | 2008-06-04 17:02:19 +0000 | [diff] [blame] | 448 | } |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 449 | } |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 450 | #elif defined(TARGET_PPC) |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 451 | #if 0 |
| 452 | if ((interrupt_request & CPU_INTERRUPT_RESET)) { |
Blue Swirl | d84bda4 | 2009-11-07 10:36:04 +0000 | [diff] [blame] | 453 | cpu_reset(env); |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 454 | } |
| 455 | #endif |
j_mayer | 4710357 | 2007-03-30 09:38:04 +0000 | [diff] [blame] | 456 | if (interrupt_request & CPU_INTERRUPT_HARD) { |
j_mayer | e9df014 | 2007-04-09 22:45:36 +0000 | [diff] [blame] | 457 | ppc_hw_interrupt(env); |
| 458 | if (env->pending_interrupts == 0) |
| 459 | env->interrupt_request &= ~CPU_INTERRUPT_HARD; |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 460 | next_tb = 0; |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 461 | } |
Michael Walle | 81ea0e1 | 2011-02-17 23:45:02 +0100 | [diff] [blame] | 462 | #elif defined(TARGET_LM32) |
| 463 | if ((interrupt_request & CPU_INTERRUPT_HARD) |
| 464 | && (env->ie & IE_IE)) { |
| 465 | env->exception_index = EXCP_IRQ; |
| 466 | do_interrupt(env); |
| 467 | next_tb = 0; |
| 468 | } |
Edgar E. Iglesias | b779e29 | 2009-05-20 21:31:33 +0200 | [diff] [blame] | 469 | #elif defined(TARGET_MICROBLAZE) |
| 470 | if ((interrupt_request & CPU_INTERRUPT_HARD) |
| 471 | && (env->sregs[SR_MSR] & MSR_IE) |
| 472 | && !(env->sregs[SR_MSR] & (MSR_EIP | MSR_BIP)) |
| 473 | && !(env->iflags & (D_FLAG | IMM_FLAG))) { |
| 474 | env->exception_index = EXCP_IRQ; |
| 475 | do_interrupt(env); |
| 476 | next_tb = 0; |
| 477 | } |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 478 | #elif defined(TARGET_MIPS) |
| 479 | if ((interrupt_request & CPU_INTERRUPT_HARD) && |
Aurelien Jarno | 4cdc1cd | 2010-12-25 22:56:32 +0100 | [diff] [blame] | 480 | cpu_mips_hw_interrupts_pending(env)) { |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 481 | /* Raise it */ |
| 482 | env->exception_index = EXCP_EXT_INTERRUPT; |
| 483 | env->error_code = 0; |
| 484 | do_interrupt(env); |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 485 | next_tb = 0; |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 486 | } |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 487 | #elif defined(TARGET_SPARC) |
Igor V. Kovalenko | d532b26 | 2010-01-07 23:28:31 +0300 | [diff] [blame] | 488 | if (interrupt_request & CPU_INTERRUPT_HARD) { |
| 489 | if (cpu_interrupts_enabled(env) && |
| 490 | env->interrupt_index > 0) { |
| 491 | int pil = env->interrupt_index & 0xf; |
| 492 | int type = env->interrupt_index & 0xf0; |
bellard | 66321a1 | 2005-04-06 20:47:48 +0000 | [diff] [blame] | 493 | |
Igor V. Kovalenko | d532b26 | 2010-01-07 23:28:31 +0300 | [diff] [blame] | 494 | if (((type == TT_EXTINT) && |
| 495 | cpu_pil_allowed(env, pil)) || |
| 496 | type != TT_EXTINT) { |
| 497 | env->exception_index = env->interrupt_index; |
| 498 | do_interrupt(env); |
| 499 | next_tb = 0; |
| 500 | } |
| 501 | } |
balrog | a90b731 | 2007-05-01 01:28:01 +0000 | [diff] [blame] | 502 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 503 | #elif defined(TARGET_ARM) |
| 504 | if (interrupt_request & CPU_INTERRUPT_FIQ |
| 505 | && !(env->uncached_cpsr & CPSR_F)) { |
| 506 | env->exception_index = EXCP_FIQ; |
| 507 | do_interrupt(env); |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 508 | next_tb = 0; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 509 | } |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 510 | /* ARMv7-M interrupt return works by loading a magic value |
| 511 | into the PC. On real hardware the load causes the |
| 512 | return to occur. The qemu implementation performs the |
| 513 | jump normally, then does the exception return when the |
| 514 | CPU tries to execute code at the magic address. |
| 515 | This will cause the magic PC value to be pushed to |
Stefan Weil | a1c7273 | 2011-04-28 17:20:38 +0200 | [diff] [blame] | 516 | the stack if an interrupt occurred at the wrong time. |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 517 | We avoid this by disabling interrupts when |
| 518 | pc contains a magic address. */ |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 519 | if (interrupt_request & CPU_INTERRUPT_HARD |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 520 | && ((IS_M(env) && env->regs[15] < 0xfffffff0) |
| 521 | || !(env->uncached_cpsr & CPSR_I))) { |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 522 | env->exception_index = EXCP_IRQ; |
| 523 | do_interrupt(env); |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 524 | next_tb = 0; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 525 | } |
Guan Xuetao | d2fbca9 | 2011-04-12 16:27:03 +0800 | [diff] [blame] | 526 | #elif defined(TARGET_UNICORE32) |
| 527 | if (interrupt_request & CPU_INTERRUPT_HARD |
| 528 | && !(env->uncached_asr & ASR_I)) { |
| 529 | do_interrupt(env); |
| 530 | next_tb = 0; |
| 531 | } |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 532 | #elif defined(TARGET_SH4) |
ths | e96e204 | 2007-12-02 06:18:24 +0000 | [diff] [blame] | 533 | if (interrupt_request & CPU_INTERRUPT_HARD) { |
| 534 | do_interrupt(env); |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 535 | next_tb = 0; |
ths | e96e204 | 2007-12-02 06:18:24 +0000 | [diff] [blame] | 536 | } |
j_mayer | eddf68a | 2007-04-05 07:22:49 +0000 | [diff] [blame] | 537 | #elif defined(TARGET_ALPHA) |
| 538 | if (interrupt_request & CPU_INTERRUPT_HARD) { |
| 539 | do_interrupt(env); |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 540 | next_tb = 0; |
j_mayer | eddf68a | 2007-04-05 07:22:49 +0000 | [diff] [blame] | 541 | } |
ths | f1ccf90 | 2007-10-08 13:16:14 +0000 | [diff] [blame] | 542 | #elif defined(TARGET_CRIS) |
edgar_igl | 1b1a38b | 2008-06-09 23:18:06 +0000 | [diff] [blame] | 543 | if (interrupt_request & CPU_INTERRUPT_HARD |
Edgar E. Iglesias | fb9fb69 | 2010-02-15 11:17:33 +0100 | [diff] [blame] | 544 | && (env->pregs[PR_CCS] & I_FLAG) |
| 545 | && !env->locked_irq) { |
edgar_igl | 1b1a38b | 2008-06-09 23:18:06 +0000 | [diff] [blame] | 546 | env->exception_index = EXCP_IRQ; |
| 547 | do_interrupt(env); |
| 548 | next_tb = 0; |
| 549 | } |
| 550 | if (interrupt_request & CPU_INTERRUPT_NMI |
| 551 | && (env->pregs[PR_CCS] & M_FLAG)) { |
| 552 | env->exception_index = EXCP_NMI; |
ths | f1ccf90 | 2007-10-08 13:16:14 +0000 | [diff] [blame] | 553 | do_interrupt(env); |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 554 | next_tb = 0; |
ths | f1ccf90 | 2007-10-08 13:16:14 +0000 | [diff] [blame] | 555 | } |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 556 | #elif defined(TARGET_M68K) |
| 557 | if (interrupt_request & CPU_INTERRUPT_HARD |
| 558 | && ((env->sr & SR_I) >> SR_I_SHIFT) |
| 559 | < env->pending_level) { |
| 560 | /* Real hardware gets the interrupt vector via an |
| 561 | IACK cycle at this point. Current emulated |
| 562 | hardware doesn't rely on this, so we |
| 563 | provide/save the vector when the interrupt is |
| 564 | first signalled. */ |
| 565 | env->exception_index = env->pending_vector; |
| 566 | do_interrupt(1); |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 567 | next_tb = 0; |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 568 | } |
Alexander Graf | 3110e29 | 2011-04-15 17:32:48 +0200 | [diff] [blame] | 569 | #elif defined(TARGET_S390X) && !defined(CONFIG_USER_ONLY) |
| 570 | if ((interrupt_request & CPU_INTERRUPT_HARD) && |
| 571 | (env->psw.mask & PSW_MASK_EXT)) { |
| 572 | do_interrupt(env); |
| 573 | next_tb = 0; |
| 574 | } |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 575 | #endif |
Stefan Weil | ff2712b | 2011-04-28 17:20:35 +0200 | [diff] [blame] | 576 | /* Don't use the cached interrupt_request value, |
bellard | 9d05095 | 2006-05-22 22:03:52 +0000 | [diff] [blame] | 577 | do_interrupt may have updated the EXITTB flag. */ |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 578 | if (env->interrupt_request & CPU_INTERRUPT_EXITTB) { |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 579 | env->interrupt_request &= ~CPU_INTERRUPT_EXITTB; |
| 580 | /* ensure that no TB jump will be modified as |
| 581 | the program flow was changed */ |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 582 | next_tb = 0; |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 583 | } |
aurel32 | be214e6 | 2009-03-06 21:48:00 +0000 | [diff] [blame] | 584 | } |
| 585 | if (unlikely(env->exit_request)) { |
| 586 | env->exit_request = 0; |
| 587 | env->exception_index = EXCP_INTERRUPT; |
| 588 | cpu_loop_exit(); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 589 | } |
Richard Henderson | a73b1fd | 2010-04-28 16:07:57 -0700 | [diff] [blame] | 590 | #if defined(DEBUG_DISAS) || defined(CONFIG_DEBUG_EXEC) |
aliguori | 8fec2b8 | 2009-01-15 22:36:53 +0000 | [diff] [blame] | 591 | if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) { |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 592 | /* restore flags in standard format */ |
ths | ecb644f | 2007-06-03 18:45:53 +0000 | [diff] [blame] | 593 | #if defined(TARGET_I386) |
pbrook | a7812ae | 2008-11-17 14:43:54 +0000 | [diff] [blame] | 594 | env->eflags = env->eflags | helper_cc_compute_all(CC_OP) | (DF & DF_MASK); |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 595 | log_cpu_state(env, X86_DUMP_CCOP); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 596 | env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 597 | #elif defined(TARGET_M68K) |
| 598 | cpu_m68k_flush_flags(env, env->cc_op); |
| 599 | env->cc_op = CC_OP_FLAGS; |
| 600 | env->sr = (env->sr & 0xffe0) |
| 601 | | env->cc_dest | (env->cc_x << 4); |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 602 | log_cpu_state(env, 0); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 603 | #else |
Richard Henderson | a73b1fd | 2010-04-28 16:07:57 -0700 | [diff] [blame] | 604 | log_cpu_state(env, 0); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 605 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 606 | } |
Richard Henderson | a73b1fd | 2010-04-28 16:07:57 -0700 | [diff] [blame] | 607 | #endif /* DEBUG_DISAS || CONFIG_DEBUG_EXEC */ |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 608 | spin_lock(&tb_lock); |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 609 | tb = tb_find_fast(); |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 610 | /* Note: we do it here to avoid a gcc bug on Mac OS X when |
| 611 | doing it in tb_find_slow */ |
| 612 | if (tb_invalidated_flag) { |
| 613 | /* as some TB could have been invalidated because |
| 614 | of memory exceptions while generating the code, we |
| 615 | must recompute the hash index here */ |
| 616 | next_tb = 0; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 617 | tb_invalidated_flag = 0; |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 618 | } |
Juan Quintela | f0667e6 | 2009-07-27 16:13:05 +0200 | [diff] [blame] | 619 | #ifdef CONFIG_DEBUG_EXEC |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 620 | qemu_log_mask(CPU_LOG_EXEC, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n", |
| 621 | (long)tb->tc_ptr, tb->pc, |
| 622 | lookup_symbol(tb->pc)); |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 623 | #endif |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 624 | /* see if we can patch the calling TB. When the TB |
| 625 | spans two pages, we cannot safely do a direct |
| 626 | jump. */ |
Paolo Bonzini | 040f2fb | 2010-01-15 08:56:36 +0100 | [diff] [blame] | 627 | if (next_tb != 0 && tb->page_addr[1] == -1) { |
blueswir1 | b5fc09a | 2008-05-04 06:38:18 +0000 | [diff] [blame] | 628 | tb_add_jump((TranslationBlock *)(next_tb & ~3), next_tb & 3, tb); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 629 | } |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 630 | spin_unlock(&tb_lock); |
malc | 55e8b85 | 2008-11-04 14:18:13 +0000 | [diff] [blame] | 631 | |
| 632 | /* cpu_interrupt might be called while translating the |
| 633 | TB, but before it is linked into a potentially |
| 634 | infinite loop and becomes env->current_tb. Avoid |
| 635 | starting execution if there is a pending interrupt. */ |
Jan Kiszka | b0052d1 | 2010-06-25 16:56:50 +0200 | [diff] [blame] | 636 | env->current_tb = tb; |
| 637 | barrier(); |
| 638 | if (likely(!env->exit_request)) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 639 | tc_ptr = tb->tc_ptr; |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 640 | /* execute the generated code */ |
Juan Quintela | dfe5fff | 2009-07-27 16:12:40 +0200 | [diff] [blame] | 641 | #if defined(__sparc__) && !defined(CONFIG_SOLARIS) |
blueswir1 | 572a9d4 | 2008-05-17 07:38:10 +0000 | [diff] [blame] | 642 | #undef env |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 643 | env = cpu_single_env; |
blueswir1 | 572a9d4 | 2008-05-17 07:38:10 +0000 | [diff] [blame] | 644 | #define env cpu_single_env |
| 645 | #endif |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 646 | next_tb = tcg_qemu_tb_exec(tc_ptr); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 647 | if ((next_tb & 3) == 2) { |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 648 | /* Instruction counter expired. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 649 | int insns_left; |
| 650 | tb = (TranslationBlock *)(long)(next_tb & ~3); |
| 651 | /* Restore PC. */ |
aliguori | 622ed36 | 2008-11-18 19:36:03 +0000 | [diff] [blame] | 652 | cpu_pc_from_tb(env, tb); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 653 | insns_left = env->icount_decr.u32; |
| 654 | if (env->icount_extra && insns_left >= 0) { |
| 655 | /* Refill decrementer and continue execution. */ |
| 656 | env->icount_extra += insns_left; |
| 657 | if (env->icount_extra > 0xffff) { |
| 658 | insns_left = 0xffff; |
| 659 | } else { |
| 660 | insns_left = env->icount_extra; |
| 661 | } |
| 662 | env->icount_extra -= insns_left; |
| 663 | env->icount_decr.u16.low = insns_left; |
| 664 | } else { |
| 665 | if (insns_left > 0) { |
| 666 | /* Execute remaining instructions. */ |
| 667 | cpu_exec_nocache(insns_left, tb); |
| 668 | } |
| 669 | env->exception_index = EXCP_INTERRUPT; |
| 670 | next_tb = 0; |
| 671 | cpu_loop_exit(); |
| 672 | } |
| 673 | } |
| 674 | } |
Jan Kiszka | b0052d1 | 2010-06-25 16:56:50 +0200 | [diff] [blame] | 675 | env->current_tb = NULL; |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 676 | /* reset soft MMU for next block (it can currently |
| 677 | only be set by a memory fault) */ |
ths | 50a518e | 2007-06-03 18:52:15 +0000 | [diff] [blame] | 678 | } /* for(;;) */ |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 679 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 680 | } /* for(;;) */ |
| 681 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 682 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 683 | #if defined(TARGET_I386) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 684 | /* restore flags in standard format */ |
pbrook | a7812ae | 2008-11-17 14:43:54 +0000 | [diff] [blame] | 685 | env->eflags = env->eflags | helper_cc_compute_all(CC_OP) | (DF & DF_MASK); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 686 | #elif defined(TARGET_ARM) |
bellard | b7bcbe9 | 2005-02-22 19:27:29 +0000 | [diff] [blame] | 687 | /* XXX: Save/restore host fpu exception state?. */ |
Guan Xuetao | d2fbca9 | 2011-04-12 16:27:03 +0800 | [diff] [blame] | 688 | #elif defined(TARGET_UNICORE32) |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 689 | #elif defined(TARGET_SPARC) |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 690 | #elif defined(TARGET_PPC) |
Michael Walle | 81ea0e1 | 2011-02-17 23:45:02 +0100 | [diff] [blame] | 691 | #elif defined(TARGET_LM32) |
pbrook | e6e5906 | 2006-10-22 00:18:54 +0000 | [diff] [blame] | 692 | #elif defined(TARGET_M68K) |
| 693 | cpu_m68k_flush_flags(env, env->cc_op); |
| 694 | env->cc_op = CC_OP_FLAGS; |
| 695 | env->sr = (env->sr & 0xffe0) |
| 696 | | env->cc_dest | (env->cc_x << 4); |
Edgar E. Iglesias | b779e29 | 2009-05-20 21:31:33 +0200 | [diff] [blame] | 697 | #elif defined(TARGET_MICROBLAZE) |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 698 | #elif defined(TARGET_MIPS) |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 699 | #elif defined(TARGET_SH4) |
j_mayer | eddf68a | 2007-04-05 07:22:49 +0000 | [diff] [blame] | 700 | #elif defined(TARGET_ALPHA) |
ths | f1ccf90 | 2007-10-08 13:16:14 +0000 | [diff] [blame] | 701 | #elif defined(TARGET_CRIS) |
Alexander Graf | 10ec511 | 2009-12-05 12:44:21 +0100 | [diff] [blame] | 702 | #elif defined(TARGET_S390X) |
bellard | fdf9b3e | 2006-04-27 21:07:38 +0000 | [diff] [blame] | 703 | /* XXXXX */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 704 | #else |
| 705 | #error unsupported target CPU |
| 706 | #endif |
pbrook | 1057eaa | 2007-02-04 13:37:44 +0000 | [diff] [blame] | 707 | |
| 708 | /* restore global registers */ |
Jan Kiszka | 1d93f0f | 2010-06-25 16:56:49 +0200 | [diff] [blame] | 709 | barrier(); |
Paolo Bonzini | 24ebf5f | 2010-02-18 21:25:23 +0100 | [diff] [blame] | 710 | env = (void *) saved_env_reg; |
pbrook | 1057eaa | 2007-02-04 13:37:44 +0000 | [diff] [blame] | 711 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 712 | /* fail safe : never use cpu_single_env outside cpu_exec() */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 713 | cpu_single_env = NULL; |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 714 | return ret; |
| 715 | } |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 716 | |
bellard | 1a18c71 | 2003-10-30 01:07:51 +0000 | [diff] [blame] | 717 | #if defined(TARGET_I386) && defined(CONFIG_USER_ONLY) |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 718 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 719 | void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector) |
| 720 | { |
| 721 | CPUX86State *saved_env; |
| 722 | |
| 723 | saved_env = env; |
| 724 | env = s; |
bellard | a412ac5 | 2003-07-26 18:01:40 +0000 | [diff] [blame] | 725 | if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) { |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 726 | selector &= 0xffff; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 727 | cpu_x86_load_seg_cache(env, seg_reg, selector, |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 728 | (selector << 4), 0xffff, 0); |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 729 | } else { |
bellard | 5d97559 | 2008-05-12 22:05:33 +0000 | [diff] [blame] | 730 | helper_load_seg(seg_reg, selector); |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 731 | } |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 732 | env = saved_env; |
| 733 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 734 | |
bellard | 6f12a2a | 2007-11-11 22:16:56 +0000 | [diff] [blame] | 735 | void cpu_x86_fsave(CPUX86State *s, target_ulong ptr, int data32) |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 736 | { |
| 737 | CPUX86State *saved_env; |
| 738 | |
| 739 | saved_env = env; |
| 740 | env = s; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 741 | |
bellard | 6f12a2a | 2007-11-11 22:16:56 +0000 | [diff] [blame] | 742 | helper_fsave(ptr, data32); |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 743 | |
| 744 | env = saved_env; |
| 745 | } |
| 746 | |
bellard | 6f12a2a | 2007-11-11 22:16:56 +0000 | [diff] [blame] | 747 | void cpu_x86_frstor(CPUX86State *s, target_ulong ptr, int data32) |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 748 | { |
| 749 | CPUX86State *saved_env; |
| 750 | |
| 751 | saved_env = env; |
| 752 | env = s; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 753 | |
bellard | 6f12a2a | 2007-11-11 22:16:56 +0000 | [diff] [blame] | 754 | helper_frstor(ptr, data32); |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 755 | |
| 756 | env = saved_env; |
| 757 | } |
| 758 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 759 | #endif /* TARGET_I386 */ |
| 760 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 761 | #if !defined(CONFIG_SOFTMMU) |
| 762 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 763 | #if defined(TARGET_I386) |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 764 | #define EXCEPTION_ACTION \ |
| 765 | raise_exception_err(env->exception_index, env->error_code) |
Nathan Froyd | 0b5c1ce | 2009-08-10 13:37:36 -0700 | [diff] [blame] | 766 | #else |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 767 | #define EXCEPTION_ACTION \ |
| 768 | cpu_loop_exit() |
Nathan Froyd | 0b5c1ce | 2009-08-10 13:37:36 -0700 | [diff] [blame] | 769 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 770 | |
bellard | b56dad1 | 2003-05-08 15:38:04 +0000 | [diff] [blame] | 771 | /* 'pc' is the host PC at which the exception was raised. 'address' is |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 772 | the effective address of the memory exception. 'is_write' is 1 if a |
| 773 | write caused the exception and otherwise 0'. 'old_set' is the |
| 774 | signal set which should be restored */ |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 775 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 776 | int is_write, sigset_t *old_set, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 777 | void *puc) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 778 | { |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 779 | TranslationBlock *tb; |
| 780 | int ret; |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 781 | |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 782 | if (cpu_single_env) { |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 783 | env = cpu_single_env; /* XXX: find a correct solution for multithread */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 784 | } |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 785 | #if defined(DEBUG_SIGNAL) |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 786 | qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 787 | pc, address, is_write, *(unsigned long *)old_set); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 788 | #endif |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 789 | /* XXX: locking issue */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 790 | if (is_write && page_unprotect(h2g(address), pc, puc)) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 791 | return 1; |
| 792 | } |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 793 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 794 | /* see if it is an MMU fault */ |
Nathan Froyd | 0b5c1ce | 2009-08-10 13:37:36 -0700 | [diff] [blame] | 795 | ret = cpu_handle_mmu_fault(env, address, is_write, MMU_USER_IDX, 0); |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 796 | if (ret < 0) { |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 797 | return 0; /* not an MMU fault */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 798 | } |
| 799 | if (ret == 0) { |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 800 | return 1; /* the MMU fault was handled without causing real CPU fault */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 801 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 802 | /* now we have a real cpu fault */ |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 803 | tb = tb_find_pc(pc); |
| 804 | if (tb) { |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 805 | /* the PC is inside the translated code. It means that we have |
| 806 | a virtual CPU fault */ |
Stefan Weil | 618ba8e | 2011-04-18 06:39:53 +0000 | [diff] [blame] | 807 | cpu_restore_state(tb, env, pc); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 808 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 809 | |
bellard | 68016c6 | 2005-02-07 23:12:27 +0000 | [diff] [blame] | 810 | /* we restore the process signal mask as the sigreturn should |
| 811 | do it (XXX: use sigsetjmp) */ |
| 812 | sigprocmask(SIG_SETMASK, old_set, NULL); |
Nathan Froyd | 0b5c1ce | 2009-08-10 13:37:36 -0700 | [diff] [blame] | 813 | EXCEPTION_ACTION; |
| 814 | |
aurel32 | 968c74d | 2008-04-11 04:55:17 +0000 | [diff] [blame] | 815 | /* never comes here */ |
| 816 | return 1; |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 817 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 818 | |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 819 | #if defined(__i386__) |
| 820 | |
bellard | d8ecc0b | 2007-02-05 21:41:46 +0000 | [diff] [blame] | 821 | #if defined(__APPLE__) |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 822 | #include <sys/ucontext.h> |
bellard | d8ecc0b | 2007-02-05 21:41:46 +0000 | [diff] [blame] | 823 | |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 824 | #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext->ss.eip)) |
| 825 | #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno) |
| 826 | #define ERROR_sig(context) ((context)->uc_mcontext->es.err) |
| 827 | #define MASK_sig(context) ((context)->uc_sigmask) |
| 828 | #elif defined(__NetBSD__) |
| 829 | #include <ucontext.h> |
Juergen Lock | 78cfb07 | 2009-10-17 00:34:26 +0200 | [diff] [blame] | 830 | |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 831 | #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP]) |
| 832 | #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO]) |
| 833 | #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR]) |
| 834 | #define MASK_sig(context) ((context)->uc_sigmask) |
| 835 | #elif defined(__FreeBSD__) || defined(__DragonFly__) |
| 836 | #include <ucontext.h> |
Juergen Lock | 78cfb07 | 2009-10-17 00:34:26 +0200 | [diff] [blame] | 837 | |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 838 | #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip)) |
| 839 | #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno) |
| 840 | #define ERROR_sig(context) ((context)->uc_mcontext.mc_err) |
| 841 | #define MASK_sig(context) ((context)->uc_sigmask) |
blueswir1 | d39bb24 | 2009-04-10 07:29:34 +0000 | [diff] [blame] | 842 | #elif defined(__OpenBSD__) |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 843 | #define EIP_sig(context) ((context)->sc_eip) |
| 844 | #define TRAP_sig(context) ((context)->sc_trapno) |
| 845 | #define ERROR_sig(context) ((context)->sc_err) |
| 846 | #define MASK_sig(context) ((context)->sc_mask) |
bellard | d8ecc0b | 2007-02-05 21:41:46 +0000 | [diff] [blame] | 847 | #else |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 848 | #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP]) |
| 849 | #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO]) |
| 850 | #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR]) |
| 851 | #define MASK_sig(context) ((context)->uc_sigmask) |
bellard | d8ecc0b | 2007-02-05 21:41:46 +0000 | [diff] [blame] | 852 | #endif |
| 853 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 854 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 855 | void *puc) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 856 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 857 | siginfo_t *info = pinfo; |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 858 | #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) |
Juergen Lock | 78cfb07 | 2009-10-17 00:34:26 +0200 | [diff] [blame] | 859 | ucontext_t *uc = puc; |
| 860 | #elif defined(__OpenBSD__) |
blueswir1 | d39bb24 | 2009-04-10 07:29:34 +0000 | [diff] [blame] | 861 | struct sigcontext *uc = puc; |
| 862 | #else |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 863 | struct ucontext *uc = puc; |
blueswir1 | d39bb24 | 2009-04-10 07:29:34 +0000 | [diff] [blame] | 864 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 865 | unsigned long pc; |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 866 | int trapno; |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 867 | |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 868 | #ifndef REG_EIP |
| 869 | /* for glibc 2.1 */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 870 | #define REG_EIP EIP |
| 871 | #define REG_ERR ERR |
| 872 | #define REG_TRAPNO TRAPNO |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 873 | #endif |
bellard | d8ecc0b | 2007-02-05 21:41:46 +0000 | [diff] [blame] | 874 | pc = EIP_sig(uc); |
| 875 | trapno = TRAP_sig(uc); |
bellard | ec6338b | 2007-11-08 14:25:03 +0000 | [diff] [blame] | 876 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
| 877 | trapno == 0xe ? |
| 878 | (ERROR_sig(uc) >> 1) & 1 : 0, |
blueswir1 | d39bb24 | 2009-04-10 07:29:34 +0000 | [diff] [blame] | 879 | &MASK_sig(uc), puc); |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 880 | } |
| 881 | |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 882 | #elif defined(__x86_64__) |
| 883 | |
blueswir1 | b3efe5c | 2008-12-05 17:55:45 +0000 | [diff] [blame] | 884 | #ifdef __NetBSD__ |
blueswir1 | d397abb | 2009-04-10 13:00:29 +0000 | [diff] [blame] | 885 | #define PC_sig(context) _UC_MACHINE_PC(context) |
| 886 | #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO]) |
| 887 | #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR]) |
| 888 | #define MASK_sig(context) ((context)->uc_sigmask) |
| 889 | #elif defined(__OpenBSD__) |
| 890 | #define PC_sig(context) ((context)->sc_rip) |
| 891 | #define TRAP_sig(context) ((context)->sc_trapno) |
| 892 | #define ERROR_sig(context) ((context)->sc_err) |
| 893 | #define MASK_sig(context) ((context)->sc_mask) |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 894 | #elif defined(__FreeBSD__) || defined(__DragonFly__) |
Juergen Lock | 78cfb07 | 2009-10-17 00:34:26 +0200 | [diff] [blame] | 895 | #include <ucontext.h> |
| 896 | |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 897 | #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip)) |
Juergen Lock | 78cfb07 | 2009-10-17 00:34:26 +0200 | [diff] [blame] | 898 | #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno) |
| 899 | #define ERROR_sig(context) ((context)->uc_mcontext.mc_err) |
| 900 | #define MASK_sig(context) ((context)->uc_sigmask) |
blueswir1 | b3efe5c | 2008-12-05 17:55:45 +0000 | [diff] [blame] | 901 | #else |
blueswir1 | d397abb | 2009-04-10 13:00:29 +0000 | [diff] [blame] | 902 | #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP]) |
| 903 | #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO]) |
| 904 | #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR]) |
| 905 | #define MASK_sig(context) ((context)->uc_sigmask) |
blueswir1 | b3efe5c | 2008-12-05 17:55:45 +0000 | [diff] [blame] | 906 | #endif |
| 907 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 908 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 909 | void *puc) |
| 910 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 911 | siginfo_t *info = pinfo; |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 912 | unsigned long pc; |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 913 | #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) |
blueswir1 | b3efe5c | 2008-12-05 17:55:45 +0000 | [diff] [blame] | 914 | ucontext_t *uc = puc; |
blueswir1 | d397abb | 2009-04-10 13:00:29 +0000 | [diff] [blame] | 915 | #elif defined(__OpenBSD__) |
| 916 | struct sigcontext *uc = puc; |
blueswir1 | b3efe5c | 2008-12-05 17:55:45 +0000 | [diff] [blame] | 917 | #else |
| 918 | struct ucontext *uc = puc; |
| 919 | #endif |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 920 | |
blueswir1 | d397abb | 2009-04-10 13:00:29 +0000 | [diff] [blame] | 921 | pc = PC_sig(uc); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 922 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
blueswir1 | d397abb | 2009-04-10 13:00:29 +0000 | [diff] [blame] | 923 | TRAP_sig(uc) == 0xe ? |
| 924 | (ERROR_sig(uc) >> 1) & 1 : 0, |
| 925 | &MASK_sig(uc), puc); |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 926 | } |
| 927 | |
malc | e58ffeb | 2009-01-14 18:39:49 +0000 | [diff] [blame] | 928 | #elif defined(_ARCH_PPC) |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 929 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 930 | /*********************************************************************** |
| 931 | * signal context platform-specific definitions |
| 932 | * From Wine |
| 933 | */ |
| 934 | #ifdef linux |
| 935 | /* All Registers access - only for local access */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 936 | #define REG_sig(reg_name, context) \ |
| 937 | ((context)->uc_mcontext.regs->reg_name) |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 938 | /* Gpr Registers access */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 939 | #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context) |
| 940 | /* Program counter */ |
| 941 | #define IAR_sig(context) REG_sig(nip, context) |
| 942 | /* Machine State Register (Supervisor) */ |
| 943 | #define MSR_sig(context) REG_sig(msr, context) |
| 944 | /* Count register */ |
| 945 | #define CTR_sig(context) REG_sig(ctr, context) |
| 946 | /* User's integer exception register */ |
| 947 | #define XER_sig(context) REG_sig(xer, context) |
| 948 | /* Link register */ |
| 949 | #define LR_sig(context) REG_sig(link, context) |
| 950 | /* Condition register */ |
| 951 | #define CR_sig(context) REG_sig(ccr, context) |
| 952 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 953 | /* Float Registers access */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 954 | #define FLOAT_sig(reg_num, context) \ |
| 955 | (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num]) |
| 956 | #define FPSCR_sig(context) \ |
| 957 | (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4))) |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 958 | /* Exception Registers access */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 959 | #define DAR_sig(context) REG_sig(dar, context) |
| 960 | #define DSISR_sig(context) REG_sig(dsisr, context) |
| 961 | #define TRAP_sig(context) REG_sig(trap, context) |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 962 | #endif /* linux */ |
| 963 | |
Juergen Lock | 58d9b1e | 2010-02-19 19:29:25 +0100 | [diff] [blame] | 964 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 965 | #include <ucontext.h> |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 966 | #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0) |
| 967 | #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1) |
| 968 | #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr) |
| 969 | #define XER_sig(context) ((context)->uc_mcontext.mc_xer) |
| 970 | #define LR_sig(context) ((context)->uc_mcontext.mc_lr) |
| 971 | #define CR_sig(context) ((context)->uc_mcontext.mc_cr) |
Juergen Lock | 58d9b1e | 2010-02-19 19:29:25 +0100 | [diff] [blame] | 972 | /* Exception Registers access */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 973 | #define DAR_sig(context) ((context)->uc_mcontext.mc_dar) |
| 974 | #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr) |
| 975 | #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc) |
Juergen Lock | 58d9b1e | 2010-02-19 19:29:25 +0100 | [diff] [blame] | 976 | #endif /* __FreeBSD__|| __FreeBSD_kernel__ */ |
| 977 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 978 | #ifdef __APPLE__ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 979 | #include <sys/ucontext.h> |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 980 | typedef struct ucontext SIGCONTEXT; |
| 981 | /* All Registers access - only for local access */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 982 | #define REG_sig(reg_name, context) \ |
| 983 | ((context)->uc_mcontext->ss.reg_name) |
| 984 | #define FLOATREG_sig(reg_name, context) \ |
| 985 | ((context)->uc_mcontext->fs.reg_name) |
| 986 | #define EXCEPREG_sig(reg_name, context) \ |
| 987 | ((context)->uc_mcontext->es.reg_name) |
| 988 | #define VECREG_sig(reg_name, context) \ |
| 989 | ((context)->uc_mcontext->vs.reg_name) |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 990 | /* Gpr Registers access */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 991 | #define GPR_sig(reg_num, context) REG_sig(r##reg_num, context) |
| 992 | /* Program counter */ |
| 993 | #define IAR_sig(context) REG_sig(srr0, context) |
| 994 | /* Machine State Register (Supervisor) */ |
| 995 | #define MSR_sig(context) REG_sig(srr1, context) |
| 996 | #define CTR_sig(context) REG_sig(ctr, context) |
| 997 | /* Link register */ |
| 998 | #define XER_sig(context) REG_sig(xer, context) |
| 999 | /* User's integer exception register */ |
| 1000 | #define LR_sig(context) REG_sig(lr, context) |
| 1001 | /* Condition register */ |
| 1002 | #define CR_sig(context) REG_sig(cr, context) |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1003 | /* Float Registers access */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1004 | #define FLOAT_sig(reg_num, context) \ |
| 1005 | FLOATREG_sig(fpregs[reg_num], context) |
| 1006 | #define FPSCR_sig(context) \ |
| 1007 | ((double)FLOATREG_sig(fpscr, context)) |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1008 | /* Exception Registers access */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1009 | /* Fault registers for coredump */ |
| 1010 | #define DAR_sig(context) EXCEPREG_sig(dar, context) |
| 1011 | #define DSISR_sig(context) EXCEPREG_sig(dsisr, context) |
| 1012 | /* number of powerpc exception taken */ |
| 1013 | #define TRAP_sig(context) EXCEPREG_sig(exception, context) |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1014 | #endif /* __APPLE__ */ |
| 1015 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1016 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1017 | void *puc) |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1018 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1019 | siginfo_t *info = pinfo; |
Juergen Lock | 58d9b1e | 2010-02-19 19:29:25 +0100 | [diff] [blame] | 1020 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 1021 | ucontext_t *uc = puc; |
| 1022 | #else |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1023 | struct ucontext *uc = puc; |
Juergen Lock | 58d9b1e | 2010-02-19 19:29:25 +0100 | [diff] [blame] | 1024 | #endif |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1025 | unsigned long pc; |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1026 | int is_write; |
| 1027 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1028 | pc = IAR_sig(uc); |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1029 | is_write = 0; |
| 1030 | #if 0 |
| 1031 | /* ppc 4xx case */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1032 | if (DSISR_sig(uc) & 0x00800000) { |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1033 | is_write = 1; |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1034 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1035 | #else |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1036 | if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) { |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1037 | is_write = 1; |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1038 | } |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1039 | #endif |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1040 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1041 | is_write, &uc->uc_sigmask, puc); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1042 | } |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1043 | |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1044 | #elif defined(__alpha__) |
| 1045 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1046 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1047 | void *puc) |
| 1048 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1049 | siginfo_t *info = pinfo; |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1050 | struct ucontext *uc = puc; |
| 1051 | uint32_t *pc = uc->uc_mcontext.sc_pc; |
| 1052 | uint32_t insn = *pc; |
| 1053 | int is_write = 0; |
| 1054 | |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1055 | /* XXX: need kernel patch to get write flag faster */ |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1056 | switch (insn >> 26) { |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1057 | case 0x0d: /* stw */ |
| 1058 | case 0x0e: /* stb */ |
| 1059 | case 0x0f: /* stq_u */ |
| 1060 | case 0x24: /* stf */ |
| 1061 | case 0x25: /* stg */ |
| 1062 | case 0x26: /* sts */ |
| 1063 | case 0x27: /* stt */ |
| 1064 | case 0x2c: /* stl */ |
| 1065 | case 0x2d: /* stq */ |
| 1066 | case 0x2e: /* stl_c */ |
| 1067 | case 0x2f: /* stq_c */ |
| 1068 | is_write = 1; |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1071 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1072 | is_write, &uc->uc_sigmask, puc); |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1073 | } |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1074 | #elif defined(__sparc__) |
| 1075 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1076 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1077 | void *puc) |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1078 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1079 | siginfo_t *info = pinfo; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1080 | int is_write; |
| 1081 | uint32_t insn; |
Juan Quintela | dfe5fff | 2009-07-27 16:12:40 +0200 | [diff] [blame] | 1082 | #if !defined(__arch64__) || defined(CONFIG_SOLARIS) |
blueswir1 | c9e1e2b | 2008-05-18 06:40:16 +0000 | [diff] [blame] | 1083 | uint32_t *regs = (uint32_t *)(info + 1); |
| 1084 | void *sigmask = (regs + 20); |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1085 | /* XXX: is there a standard glibc define ? */ |
blueswir1 | c9e1e2b | 2008-05-18 06:40:16 +0000 | [diff] [blame] | 1086 | unsigned long pc = regs[1]; |
| 1087 | #else |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1088 | #ifdef __linux__ |
blueswir1 | c9e1e2b | 2008-05-18 06:40:16 +0000 | [diff] [blame] | 1089 | struct sigcontext *sc = puc; |
| 1090 | unsigned long pc = sc->sigc_regs.tpc; |
| 1091 | void *sigmask = (void *)sc->sigc_mask; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1092 | #elif defined(__OpenBSD__) |
| 1093 | struct sigcontext *uc = puc; |
| 1094 | unsigned long pc = uc->sc_pc; |
| 1095 | void *sigmask = (void *)(long)uc->sc_mask; |
| 1096 | #endif |
blueswir1 | c9e1e2b | 2008-05-18 06:40:16 +0000 | [diff] [blame] | 1097 | #endif |
| 1098 | |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1099 | /* XXX: need kernel patch to get write flag faster */ |
| 1100 | is_write = 0; |
| 1101 | insn = *(uint32_t *)pc; |
| 1102 | if ((insn >> 30) == 3) { |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1103 | switch ((insn >> 19) & 0x3f) { |
| 1104 | case 0x05: /* stb */ |
| 1105 | case 0x15: /* stba */ |
| 1106 | case 0x06: /* sth */ |
| 1107 | case 0x16: /* stha */ |
| 1108 | case 0x04: /* st */ |
| 1109 | case 0x14: /* sta */ |
| 1110 | case 0x07: /* std */ |
| 1111 | case 0x17: /* stda */ |
| 1112 | case 0x0e: /* stx */ |
| 1113 | case 0x1e: /* stxa */ |
| 1114 | case 0x24: /* stf */ |
| 1115 | case 0x34: /* stfa */ |
| 1116 | case 0x27: /* stdf */ |
| 1117 | case 0x37: /* stdfa */ |
| 1118 | case 0x26: /* stqf */ |
| 1119 | case 0x36: /* stqfa */ |
| 1120 | case 0x25: /* stfsr */ |
| 1121 | case 0x3c: /* casa */ |
| 1122 | case 0x3e: /* casxa */ |
| 1123 | is_write = 1; |
| 1124 | break; |
| 1125 | } |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1126 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1127 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1128 | is_write, sigmask, NULL); |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | #elif defined(__arm__) |
| 1132 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1133 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1134 | void *puc) |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1135 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1136 | siginfo_t *info = pinfo; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1137 | struct ucontext *uc = puc; |
| 1138 | unsigned long pc; |
| 1139 | int is_write; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1140 | |
blueswir1 | 48bbf11 | 2008-07-08 18:35:02 +0000 | [diff] [blame] | 1141 | #if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) |
balrog | 5c49b36 | 2008-06-02 01:01:18 +0000 | [diff] [blame] | 1142 | pc = uc->uc_mcontext.gregs[R15]; |
| 1143 | #else |
balrog | 4eee57f | 2008-05-06 14:47:19 +0000 | [diff] [blame] | 1144 | pc = uc->uc_mcontext.arm_pc; |
balrog | 5c49b36 | 2008-06-02 01:01:18 +0000 | [diff] [blame] | 1145 | #endif |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1146 | /* XXX: compute is_write */ |
| 1147 | is_write = 0; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1148 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1149 | is_write, |
pbrook | f3a9676 | 2006-07-29 19:09:31 +0000 | [diff] [blame] | 1150 | &uc->uc_sigmask, puc); |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1153 | #elif defined(__mc68000) |
| 1154 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1155 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1156 | void *puc) |
| 1157 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1158 | siginfo_t *info = pinfo; |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1159 | struct ucontext *uc = puc; |
| 1160 | unsigned long pc; |
| 1161 | int is_write; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1162 | |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1163 | pc = uc->uc_mcontext.gregs[16]; |
| 1164 | /* XXX: compute is_write */ |
| 1165 | is_write = 0; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1166 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1167 | is_write, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1168 | &uc->uc_sigmask, puc); |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1171 | #elif defined(__ia64) |
| 1172 | |
| 1173 | #ifndef __ISR_VALID |
| 1174 | /* This ought to be in <bits/siginfo.h>... */ |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1175 | # define __ISR_VALID 1 |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1176 | #endif |
| 1177 | |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1178 | int cpu_signal_handler(int host_signum, void *pinfo, void *puc) |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1179 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1180 | siginfo_t *info = pinfo; |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1181 | struct ucontext *uc = puc; |
| 1182 | unsigned long ip; |
| 1183 | int is_write = 0; |
| 1184 | |
| 1185 | ip = uc->uc_mcontext.sc_ip; |
| 1186 | switch (host_signum) { |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1187 | case SIGILL: |
| 1188 | case SIGFPE: |
| 1189 | case SIGSEGV: |
| 1190 | case SIGBUS: |
| 1191 | case SIGTRAP: |
| 1192 | if (info->si_code && (info->si_segvflags & __ISR_VALID)) { |
| 1193 | /* ISR.W (write-access) is bit 33: */ |
| 1194 | is_write = (info->si_isr >> 33) & 1; |
| 1195 | } |
| 1196 | break; |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1197 | |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1198 | default: |
| 1199 | break; |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1200 | } |
| 1201 | return handle_cpu_signal(ip, (unsigned long)info->si_addr, |
| 1202 | is_write, |
Aurelien Jarno | 60e9924 | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 1203 | (sigset_t *)&uc->uc_sigmask, puc); |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
bellard | 90cb949 | 2005-07-24 15:11:38 +0000 | [diff] [blame] | 1206 | #elif defined(__s390__) |
| 1207 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1208 | int cpu_signal_handler(int host_signum, void *pinfo, |
bellard | 90cb949 | 2005-07-24 15:11:38 +0000 | [diff] [blame] | 1209 | void *puc) |
| 1210 | { |
ths | 5a7b542 | 2007-01-31 12:16:51 +0000 | [diff] [blame] | 1211 | siginfo_t *info = pinfo; |
bellard | 90cb949 | 2005-07-24 15:11:38 +0000 | [diff] [blame] | 1212 | struct ucontext *uc = puc; |
| 1213 | unsigned long pc; |
Richard Henderson | 6a1621b | 2010-06-04 12:14:12 -0700 | [diff] [blame] | 1214 | uint16_t *pinsn; |
| 1215 | int is_write = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1216 | |
bellard | 90cb949 | 2005-07-24 15:11:38 +0000 | [diff] [blame] | 1217 | pc = uc->uc_mcontext.psw.addr; |
Richard Henderson | 6a1621b | 2010-06-04 12:14:12 -0700 | [diff] [blame] | 1218 | |
| 1219 | /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead |
| 1220 | of the normal 2 arguments. The 3rd argument contains the "int_code" |
| 1221 | from the hardware which does in fact contain the is_write value. |
| 1222 | The rt signal handler, as far as I can tell, does not give this value |
| 1223 | at all. Not that we could get to it from here even if it were. */ |
| 1224 | /* ??? This is not even close to complete, since it ignores all |
| 1225 | of the read-modify-write instructions. */ |
| 1226 | pinsn = (uint16_t *)pc; |
| 1227 | switch (pinsn[0] >> 8) { |
| 1228 | case 0x50: /* ST */ |
| 1229 | case 0x42: /* STC */ |
| 1230 | case 0x40: /* STH */ |
| 1231 | is_write = 1; |
| 1232 | break; |
| 1233 | case 0xc4: /* RIL format insns */ |
| 1234 | switch (pinsn[0] & 0xf) { |
| 1235 | case 0xf: /* STRL */ |
| 1236 | case 0xb: /* STGRL */ |
| 1237 | case 0x7: /* STHRL */ |
| 1238 | is_write = 1; |
| 1239 | } |
| 1240 | break; |
| 1241 | case 0xe3: /* RXY format insns */ |
| 1242 | switch (pinsn[2] & 0xff) { |
| 1243 | case 0x50: /* STY */ |
| 1244 | case 0x24: /* STG */ |
| 1245 | case 0x72: /* STCY */ |
| 1246 | case 0x70: /* STHY */ |
| 1247 | case 0x8e: /* STPQ */ |
| 1248 | case 0x3f: /* STRVH */ |
| 1249 | case 0x3e: /* STRV */ |
| 1250 | case 0x2f: /* STRVG */ |
| 1251 | is_write = 1; |
| 1252 | } |
| 1253 | break; |
| 1254 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1255 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
ths | c4b89d1 | 2007-05-05 19:23:11 +0000 | [diff] [blame] | 1256 | is_write, &uc->uc_sigmask, puc); |
| 1257 | } |
| 1258 | |
| 1259 | #elif defined(__mips__) |
| 1260 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1261 | int cpu_signal_handler(int host_signum, void *pinfo, |
ths | c4b89d1 | 2007-05-05 19:23:11 +0000 | [diff] [blame] | 1262 | void *puc) |
| 1263 | { |
ths | 9617efe | 2007-05-08 21:05:55 +0000 | [diff] [blame] | 1264 | siginfo_t *info = pinfo; |
ths | c4b89d1 | 2007-05-05 19:23:11 +0000 | [diff] [blame] | 1265 | struct ucontext *uc = puc; |
| 1266 | greg_t pc = uc->uc_mcontext.pc; |
| 1267 | int is_write; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1268 | |
ths | c4b89d1 | 2007-05-05 19:23:11 +0000 | [diff] [blame] | 1269 | /* XXX: compute is_write */ |
| 1270 | is_write = 0; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1271 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
ths | c4b89d1 | 2007-05-05 19:23:11 +0000 | [diff] [blame] | 1272 | is_write, &uc->uc_sigmask, puc); |
bellard | 90cb949 | 2005-07-24 15:11:38 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
aurel32 | f54b3f9 | 2008-04-12 20:14:54 +0000 | [diff] [blame] | 1275 | #elif defined(__hppa__) |
| 1276 | |
| 1277 | int cpu_signal_handler(int host_signum, void *pinfo, |
| 1278 | void *puc) |
| 1279 | { |
| 1280 | struct siginfo *info = pinfo; |
| 1281 | struct ucontext *uc = puc; |
Richard Henderson | f57040b | 2010-03-12 15:58:08 +0100 | [diff] [blame] | 1282 | unsigned long pc = uc->uc_mcontext.sc_iaoq[0]; |
| 1283 | uint32_t insn = *(uint32_t *)pc; |
| 1284 | int is_write = 0; |
aurel32 | f54b3f9 | 2008-04-12 20:14:54 +0000 | [diff] [blame] | 1285 | |
Richard Henderson | f57040b | 2010-03-12 15:58:08 +0100 | [diff] [blame] | 1286 | /* XXX: need kernel patch to get write flag faster. */ |
| 1287 | switch (insn >> 26) { |
| 1288 | case 0x1a: /* STW */ |
| 1289 | case 0x19: /* STH */ |
| 1290 | case 0x18: /* STB */ |
| 1291 | case 0x1b: /* STWM */ |
| 1292 | is_write = 1; |
| 1293 | break; |
| 1294 | |
| 1295 | case 0x09: /* CSTWX, FSTWX, FSTWS */ |
| 1296 | case 0x0b: /* CSTDX, FSTDX, FSTDS */ |
| 1297 | /* Distinguish from coprocessor load ... */ |
| 1298 | is_write = (insn >> 9) & 1; |
| 1299 | break; |
| 1300 | |
| 1301 | case 0x03: |
| 1302 | switch ((insn >> 6) & 15) { |
| 1303 | case 0xa: /* STWS */ |
| 1304 | case 0x9: /* STHS */ |
| 1305 | case 0x8: /* STBS */ |
| 1306 | case 0xe: /* STWAS */ |
| 1307 | case 0xc: /* STBYS */ |
| 1308 | is_write = 1; |
| 1309 | } |
| 1310 | break; |
| 1311 | } |
| 1312 | |
Blue Swirl | 9eff14f | 2011-05-21 08:42:35 +0000 | [diff] [blame^] | 1313 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
Richard Henderson | f57040b | 2010-03-12 15:58:08 +0100 | [diff] [blame] | 1314 | is_write, &uc->uc_sigmask, puc); |
aurel32 | f54b3f9 | 2008-04-12 20:14:54 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1317 | #else |
| 1318 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 1319 | #error host CPU specific signal handler needed |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1320 | |
| 1321 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1322 | |
| 1323 | #endif /* !defined(CONFIG_SOFTMMU) */ |