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