bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * i386 emulator main execution loop |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 10 | * |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 15 | * |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 19 | */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 20 | #include "config.h" |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 21 | #include "exec.h" |
bellard | 956034d | 2003-04-29 20:40:53 +0000 | [diff] [blame] | 22 | #include "disas.h" |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 23 | |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 24 | #if !defined(CONFIG_SOFTMMU) |
| 25 | #undef EAX |
| 26 | #undef ECX |
| 27 | #undef EDX |
| 28 | #undef EBX |
| 29 | #undef ESP |
| 30 | #undef EBP |
| 31 | #undef ESI |
| 32 | #undef EDI |
| 33 | #undef EIP |
| 34 | #include <signal.h> |
| 35 | #include <sys/ucontext.h> |
| 36 | #endif |
| 37 | |
bellard | 36bdbe5 | 2003-11-19 22:12:02 +0000 | [diff] [blame] | 38 | int tb_invalidated_flag; |
| 39 | |
bellard | dc99065 | 2003-03-19 00:00:28 +0000 | [diff] [blame] | 40 | //#define DEBUG_EXEC |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 41 | //#define DEBUG_SIGNAL |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 42 | |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 43 | #if defined(TARGET_ARM) || defined(TARGET_SPARC) |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 44 | /* XXX: unify with i386 target */ |
| 45 | void cpu_loop_exit(void) |
| 46 | { |
| 47 | longjmp(env->jmp_env, 1); |
| 48 | } |
| 49 | #endif |
| 50 | |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 51 | /* exit the current TB from a signal handler. The host registers are |
| 52 | restored in a state compatible with the CPU emulator |
| 53 | */ |
| 54 | void cpu_resume_from_signal(CPUState *env1, void *puc) |
| 55 | { |
| 56 | #if !defined(CONFIG_SOFTMMU) |
| 57 | struct ucontext *uc = puc; |
| 58 | #endif |
| 59 | |
| 60 | env = env1; |
| 61 | |
| 62 | /* XXX: restore cpu registers saved in host registers */ |
| 63 | |
| 64 | #if !defined(CONFIG_SOFTMMU) |
| 65 | if (puc) { |
| 66 | /* XXX: use siglongjmp ? */ |
| 67 | sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); |
| 68 | } |
| 69 | #endif |
| 70 | longjmp(env->jmp_env, 1); |
| 71 | } |
| 72 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 73 | /* main execution loop */ |
| 74 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 75 | int cpu_exec(CPUState *env1) |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 76 | { |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 77 | int saved_T0, saved_T1, saved_T2; |
| 78 | CPUState *saved_env; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 79 | #ifdef reg_EAX |
| 80 | int saved_EAX; |
| 81 | #endif |
| 82 | #ifdef reg_ECX |
| 83 | int saved_ECX; |
| 84 | #endif |
| 85 | #ifdef reg_EDX |
| 86 | int saved_EDX; |
| 87 | #endif |
| 88 | #ifdef reg_EBX |
| 89 | int saved_EBX; |
| 90 | #endif |
| 91 | #ifdef reg_ESP |
| 92 | int saved_ESP; |
| 93 | #endif |
| 94 | #ifdef reg_EBP |
| 95 | int saved_EBP; |
| 96 | #endif |
| 97 | #ifdef reg_ESI |
| 98 | int saved_ESI; |
| 99 | #endif |
| 100 | #ifdef reg_EDI |
| 101 | int saved_EDI; |
| 102 | #endif |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 103 | #ifdef __sparc__ |
| 104 | int saved_i7, tmp_T0; |
| 105 | #endif |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 106 | int code_gen_size, ret, interrupt_request; |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 107 | void (*gen_func)(void); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 108 | TranslationBlock *tb, **ptb; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 109 | target_ulong cs_base, pc; |
| 110 | uint8_t *tc_ptr; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 111 | unsigned int flags; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 112 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 113 | /* first we save global registers */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 114 | saved_env = env; |
| 115 | env = env1; |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 116 | saved_T0 = T0; |
| 117 | saved_T1 = T1; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 118 | saved_T2 = T2; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 119 | #ifdef __sparc__ |
| 120 | /* we also save i7 because longjmp may not restore it */ |
| 121 | asm volatile ("mov %%i7, %0" : "=r" (saved_i7)); |
| 122 | #endif |
| 123 | |
| 124 | #if defined(TARGET_I386) |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 125 | #ifdef reg_EAX |
| 126 | saved_EAX = EAX; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 127 | #endif |
| 128 | #ifdef reg_ECX |
| 129 | saved_ECX = ECX; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 130 | #endif |
| 131 | #ifdef reg_EDX |
| 132 | saved_EDX = EDX; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 133 | #endif |
| 134 | #ifdef reg_EBX |
| 135 | saved_EBX = EBX; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 136 | #endif |
| 137 | #ifdef reg_ESP |
| 138 | saved_ESP = ESP; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 139 | #endif |
| 140 | #ifdef reg_EBP |
| 141 | saved_EBP = EBP; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 142 | #endif |
| 143 | #ifdef reg_ESI |
| 144 | saved_ESI = ESI; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 145 | #endif |
| 146 | #ifdef reg_EDI |
| 147 | saved_EDI = EDI; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 148 | #endif |
bellard | 0d1a29f | 2004-10-12 22:01:28 +0000 | [diff] [blame] | 149 | |
| 150 | env_to_regs(); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 151 | /* put eflags in CPU temporary format */ |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 152 | CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
| 153 | DF = 1 - (2 * ((env->eflags >> 10) & 1)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 154 | CC_OP = CC_OP_EFLAGS; |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 155 | env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 156 | #elif defined(TARGET_ARM) |
| 157 | { |
| 158 | unsigned int psr; |
| 159 | psr = env->cpsr; |
| 160 | env->CF = (psr >> 29) & 1; |
| 161 | env->NZF = (psr & 0xc0000000) ^ 0x40000000; |
| 162 | env->VF = (psr << 3) & 0x80000000; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 163 | env->QF = (psr >> 27) & 1; |
| 164 | env->cpsr = psr & ~CACHED_CPSR_BITS; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 165 | } |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 166 | #elif defined(TARGET_SPARC) |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 167 | #elif defined(TARGET_PPC) |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 168 | #else |
| 169 | #error unsupported target CPU |
| 170 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 171 | env->exception_index = -1; |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 172 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 173 | /* prepare setjmp context for exception handling */ |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 174 | for(;;) { |
| 175 | if (setjmp(env->jmp_env) == 0) { |
bellard | ee8b702 | 2004-02-03 23:35:10 +0000 | [diff] [blame] | 176 | env->current_tb = NULL; |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 177 | /* if an exception is pending, we execute it here */ |
| 178 | if (env->exception_index >= 0) { |
| 179 | if (env->exception_index >= EXCP_INTERRUPT) { |
| 180 | /* exit request from the cpu execution loop */ |
| 181 | ret = env->exception_index; |
| 182 | break; |
| 183 | } else if (env->user_mode_only) { |
| 184 | /* if user mode only, we simulate a fake exception |
| 185 | which will be hanlded outside the cpu execution |
| 186 | loop */ |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 187 | #if defined(TARGET_I386) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 188 | do_interrupt_user(env->exception_index, |
| 189 | env->exception_is_int, |
| 190 | env->error_code, |
| 191 | env->exception_next_eip); |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 192 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 193 | ret = env->exception_index; |
| 194 | break; |
| 195 | } else { |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 196 | #if defined(TARGET_I386) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 197 | /* simulate a real cpu exception. On i386, it can |
| 198 | trigger new exceptions, but we do not handle |
| 199 | double or triple faults yet. */ |
| 200 | do_interrupt(env->exception_index, |
| 201 | env->exception_is_int, |
| 202 | env->error_code, |
bellard | d05e66d | 2003-08-20 21:34:35 +0000 | [diff] [blame] | 203 | env->exception_next_eip, 0); |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 204 | #elif defined(TARGET_PPC) |
| 205 | do_interrupt(env); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 206 | #elif defined(TARGET_SPARC) |
bellard | 1a0c329 | 2005-02-13 19:02:07 +0000 | [diff] [blame] | 207 | do_interrupt(env->exception_index); |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 208 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 209 | } |
| 210 | env->exception_index = -1; |
bellard | 9df217a | 2005-02-10 22:05:51 +0000 | [diff] [blame] | 211 | } |
| 212 | #ifdef USE_KQEMU |
| 213 | if (kqemu_is_ok(env) && env->interrupt_request == 0) { |
| 214 | int ret; |
| 215 | env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK); |
| 216 | ret = kqemu_cpu_exec(env); |
| 217 | /* put eflags in CPU temporary format */ |
| 218 | CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
| 219 | DF = 1 - (2 * ((env->eflags >> 10) & 1)); |
| 220 | CC_OP = CC_OP_EFLAGS; |
| 221 | env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
| 222 | if (ret == 1) { |
| 223 | /* exception */ |
| 224 | longjmp(env->jmp_env, 1); |
| 225 | } else if (ret == 2) { |
| 226 | /* softmmu execution needed */ |
| 227 | } else { |
| 228 | if (env->interrupt_request != 0) { |
| 229 | /* hardware interrupt will be executed just after */ |
| 230 | } else { |
| 231 | /* otherwise, we restart */ |
| 232 | longjmp(env->jmp_env, 1); |
| 233 | } |
| 234 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 235 | } |
bellard | 9df217a | 2005-02-10 22:05:51 +0000 | [diff] [blame] | 236 | #endif |
| 237 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 238 | T0 = 0; /* force lookup of first TB */ |
| 239 | for(;;) { |
| 240 | #ifdef __sparc__ |
| 241 | /* g1 can be modified by some libc? functions */ |
| 242 | tmp_T0 = T0; |
| 243 | #endif |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 244 | interrupt_request = env->interrupt_request; |
bellard | 2e255c6 | 2003-08-21 23:25:21 +0000 | [diff] [blame] | 245 | if (__builtin_expect(interrupt_request, 0)) { |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 246 | #if defined(TARGET_I386) |
| 247 | /* if hardware interrupt pending, we execute it */ |
| 248 | if ((interrupt_request & CPU_INTERRUPT_HARD) && |
bellard | 3f33731 | 2003-08-20 23:02:09 +0000 | [diff] [blame] | 249 | (env->eflags & IF_MASK) && |
| 250 | !(env->hflags & HF_INHIBIT_IRQ_MASK)) { |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 251 | int intno; |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 252 | env->interrupt_request &= ~CPU_INTERRUPT_HARD; |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 253 | intno = cpu_get_pic_interrupt(env); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 254 | if (loglevel & CPU_LOG_TB_IN_ASM) { |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 255 | fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno); |
| 256 | } |
bellard | d05e66d | 2003-08-20 21:34:35 +0000 | [diff] [blame] | 257 | do_interrupt(intno, 0, 0, 0, 1); |
bellard | 907a5b2 | 2003-06-30 23:18:22 +0000 | [diff] [blame] | 258 | /* ensure that no TB jump will be modified as |
| 259 | the program flow was changed */ |
| 260 | #ifdef __sparc__ |
| 261 | tmp_T0 = 0; |
| 262 | #else |
| 263 | T0 = 0; |
| 264 | #endif |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 265 | } |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 266 | #elif defined(TARGET_PPC) |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 267 | #if 0 |
| 268 | if ((interrupt_request & CPU_INTERRUPT_RESET)) { |
| 269 | cpu_ppc_reset(env); |
| 270 | } |
| 271 | #endif |
| 272 | if (msr_ee != 0) { |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 273 | if ((interrupt_request & CPU_INTERRUPT_HARD)) { |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 274 | /* Raise it */ |
| 275 | env->exception_index = EXCP_EXTERNAL; |
| 276 | env->error_code = 0; |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 277 | do_interrupt(env); |
| 278 | env->interrupt_request &= ~CPU_INTERRUPT_HARD; |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 279 | } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) { |
| 280 | /* Raise it */ |
| 281 | env->exception_index = EXCP_DECR; |
| 282 | env->error_code = 0; |
| 283 | do_interrupt(env); |
| 284 | env->interrupt_request &= ~CPU_INTERRUPT_TIMER; |
| 285 | } |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 286 | } |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 287 | #elif defined(TARGET_SPARC) |
| 288 | if (interrupt_request & CPU_INTERRUPT_HARD) { |
bellard | 1a0c329 | 2005-02-13 19:02:07 +0000 | [diff] [blame] | 289 | do_interrupt(env->interrupt_index); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 290 | env->interrupt_request &= ~CPU_INTERRUPT_HARD; |
| 291 | } else if (interrupt_request & CPU_INTERRUPT_TIMER) { |
| 292 | //do_interrupt(0, 0, 0, 0, 0); |
| 293 | env->interrupt_request &= ~CPU_INTERRUPT_TIMER; |
| 294 | } |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 295 | #endif |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 296 | if (interrupt_request & CPU_INTERRUPT_EXITTB) { |
| 297 | env->interrupt_request &= ~CPU_INTERRUPT_EXITTB; |
| 298 | /* ensure that no TB jump will be modified as |
| 299 | the program flow was changed */ |
| 300 | #ifdef __sparc__ |
| 301 | tmp_T0 = 0; |
| 302 | #else |
| 303 | T0 = 0; |
| 304 | #endif |
| 305 | } |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 306 | if (interrupt_request & CPU_INTERRUPT_EXIT) { |
| 307 | env->interrupt_request &= ~CPU_INTERRUPT_EXIT; |
| 308 | env->exception_index = EXCP_INTERRUPT; |
| 309 | cpu_loop_exit(); |
| 310 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 311 | } |
| 312 | #ifdef DEBUG_EXEC |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 313 | if ((loglevel & CPU_LOG_EXEC)) { |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 314 | #if defined(TARGET_I386) |
| 315 | /* restore flags in standard format */ |
| 316 | env->regs[R_EAX] = EAX; |
| 317 | env->regs[R_EBX] = EBX; |
| 318 | env->regs[R_ECX] = ECX; |
| 319 | env->regs[R_EDX] = EDX; |
| 320 | env->regs[R_ESI] = ESI; |
| 321 | env->regs[R_EDI] = EDI; |
| 322 | env->regs[R_EBP] = EBP; |
| 323 | env->regs[R_ESP] = ESP; |
| 324 | env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK); |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 325 | cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 326 | env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 327 | #elif defined(TARGET_ARM) |
bellard | 1b21b62 | 2003-07-09 17:16:27 +0000 | [diff] [blame] | 328 | env->cpsr = compute_cpsr(); |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 329 | cpu_dump_state(env, logfile, fprintf, 0); |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 330 | env->cpsr &= ~CACHED_CPSR_BITS; |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 331 | #elif defined(TARGET_SPARC) |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 332 | cpu_dump_state (env, logfile, fprintf, 0); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 333 | #elif defined(TARGET_PPC) |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 334 | cpu_dump_state(env, logfile, fprintf, 0); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 335 | #else |
| 336 | #error unsupported target CPU |
| 337 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 338 | } |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 339 | #endif |
bellard | 3f33731 | 2003-08-20 23:02:09 +0000 | [diff] [blame] | 340 | /* we record a subset of the CPU state. It will |
| 341 | always be the same before a given translated block |
| 342 | is executed. */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 343 | #if defined(TARGET_I386) |
bellard | 2e255c6 | 2003-08-21 23:25:21 +0000 | [diff] [blame] | 344 | flags = env->hflags; |
bellard | 3f33731 | 2003-08-20 23:02:09 +0000 | [diff] [blame] | 345 | flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK)); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 346 | cs_base = env->segs[R_CS].base; |
| 347 | pc = cs_base + env->eip; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 348 | #elif defined(TARGET_ARM) |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 349 | flags = env->thumb; |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 350 | cs_base = 0; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 351 | pc = env->regs[15]; |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 352 | #elif defined(TARGET_SPARC) |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 353 | flags = 0; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 354 | cs_base = env->npc; |
| 355 | pc = env->pc; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 356 | #elif defined(TARGET_PPC) |
bellard | c4decf3 | 2005-02-15 22:59:52 +0000 | [diff] [blame] | 357 | flags = (msr_pr << MSR_PR) | (msr_fp << MSR_FP) | (msr_se << MSR_SE); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 358 | cs_base = 0; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 359 | pc = env->nip; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 360 | #else |
| 361 | #error unsupported CPU |
| 362 | #endif |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 363 | tb = tb_find(&ptb, pc, cs_base, |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 364 | flags); |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 365 | if (!tb) { |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 366 | TranslationBlock **ptb1; |
| 367 | unsigned int h; |
| 368 | target_ulong phys_pc, phys_page1, phys_page2, virt_page2; |
| 369 | |
| 370 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 371 | spin_lock(&tb_lock); |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 372 | |
| 373 | tb_invalidated_flag = 0; |
bellard | 0d1a29f | 2004-10-12 22:01:28 +0000 | [diff] [blame] | 374 | |
| 375 | regs_to_env(); /* XXX: do it just before cpu_gen_code() */ |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 376 | |
| 377 | /* find translated block using physical mappings */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 378 | phys_pc = get_phys_addr_code(env, pc); |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 379 | phys_page1 = phys_pc & TARGET_PAGE_MASK; |
| 380 | phys_page2 = -1; |
| 381 | h = tb_phys_hash_func(phys_pc); |
| 382 | ptb1 = &tb_phys_hash[h]; |
| 383 | for(;;) { |
| 384 | tb = *ptb1; |
| 385 | if (!tb) |
| 386 | goto not_found; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 387 | if (tb->pc == pc && |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 388 | tb->page_addr[0] == phys_page1 && |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 389 | tb->cs_base == cs_base && |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 390 | tb->flags == flags) { |
| 391 | /* check next page if needed */ |
bellard | b516f85 | 2004-01-18 21:50:04 +0000 | [diff] [blame] | 392 | if (tb->page_addr[1] != -1) { |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 393 | virt_page2 = (pc & TARGET_PAGE_MASK) + |
bellard | b516f85 | 2004-01-18 21:50:04 +0000 | [diff] [blame] | 394 | TARGET_PAGE_SIZE; |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 395 | phys_page2 = get_phys_addr_code(env, virt_page2); |
| 396 | if (tb->page_addr[1] == phys_page2) |
| 397 | goto found; |
| 398 | } else { |
| 399 | goto found; |
| 400 | } |
| 401 | } |
| 402 | ptb1 = &tb->phys_hash_next; |
| 403 | } |
| 404 | not_found: |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 405 | /* if no translated code available, then translate it now */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 406 | tb = tb_alloc(pc); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 407 | if (!tb) { |
| 408 | /* flush must be done */ |
bellard | b453b70 | 2004-01-04 15:45:21 +0000 | [diff] [blame] | 409 | tb_flush(env); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 410 | /* cannot fail at this point */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 411 | tb = tb_alloc(pc); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 412 | /* don't forget to invalidate previous TB info */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 413 | ptb = &tb_hash[tb_hash_func(pc)]; |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 414 | T0 = 0; |
| 415 | } |
| 416 | tc_ptr = code_gen_ptr; |
| 417 | tb->tc_ptr = tc_ptr; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 418 | tb->cs_base = cs_base; |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 419 | tb->flags = flags; |
bellard | facc68b | 2003-09-17 22:51:18 +0000 | [diff] [blame] | 420 | cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size); |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 421 | code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1)); |
| 422 | |
| 423 | /* check next page if needed */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 424 | virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK; |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 425 | phys_page2 = -1; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 426 | if ((pc & TARGET_PAGE_MASK) != virt_page2) { |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 427 | phys_page2 = get_phys_addr_code(env, virt_page2); |
| 428 | } |
| 429 | tb_link_phys(tb, phys_pc, phys_page2); |
| 430 | |
| 431 | found: |
bellard | 36bdbe5 | 2003-11-19 22:12:02 +0000 | [diff] [blame] | 432 | if (tb_invalidated_flag) { |
| 433 | /* as some TB could have been invalidated because |
| 434 | of memory exceptions while generating the code, we |
| 435 | must recompute the hash index here */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 436 | ptb = &tb_hash[tb_hash_func(pc)]; |
bellard | 36bdbe5 | 2003-11-19 22:12:02 +0000 | [diff] [blame] | 437 | while (*ptb != NULL) |
| 438 | ptb = &(*ptb)->hash_next; |
| 439 | T0 = 0; |
| 440 | } |
bellard | 1376847 | 2004-01-04 17:43:01 +0000 | [diff] [blame] | 441 | /* we add the TB in the virtual pc hash table */ |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 442 | *ptb = tb; |
| 443 | tb->hash_next = NULL; |
| 444 | tb_link(tb); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 445 | spin_unlock(&tb_lock); |
| 446 | } |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 447 | #ifdef DEBUG_EXEC |
bellard | c1135f6 | 2005-01-30 22:41:54 +0000 | [diff] [blame] | 448 | if ((loglevel & CPU_LOG_EXEC)) { |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 449 | fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n", |
| 450 | (long)tb->tc_ptr, tb->pc, |
| 451 | lookup_symbol(tb->pc)); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 452 | } |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 453 | #endif |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 454 | #ifdef __sparc__ |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 455 | T0 = tmp_T0; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 456 | #endif |
bellard | facc68b | 2003-09-17 22:51:18 +0000 | [diff] [blame] | 457 | /* see if we can patch the calling TB. */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 458 | { |
| 459 | if (T0 != 0 |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 460 | #if defined(TARGET_I386) && defined(USE_CODE_COPY) |
| 461 | && (tb->cflags & CF_CODE_COPY) == |
| 462 | (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY) |
| 463 | #endif |
| 464 | ) { |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 465 | spin_lock(&tb_lock); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 466 | tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb); |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 467 | #if defined(USE_CODE_COPY) |
| 468 | /* propagates the FP use info */ |
| 469 | ((TranslationBlock *)(T0 & ~3))->cflags |= |
| 470 | (tb->cflags & CF_FP_USED); |
| 471 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 472 | spin_unlock(&tb_lock); |
| 473 | } |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 474 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 475 | tc_ptr = tb->tc_ptr; |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 476 | env->current_tb = tb; |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 477 | /* execute the generated code */ |
| 478 | gen_func = (void *)tc_ptr; |
| 479 | #if defined(__sparc__) |
| 480 | __asm__ __volatile__("call %0\n\t" |
| 481 | "mov %%o7,%%i0" |
| 482 | : /* no outputs */ |
| 483 | : "r" (gen_func) |
| 484 | : "i0", "i1", "i2", "i3", "i4", "i5"); |
| 485 | #elif defined(__arm__) |
| 486 | asm volatile ("mov pc, %0\n\t" |
| 487 | ".global exec_loop\n\t" |
| 488 | "exec_loop:\n\t" |
| 489 | : /* no outputs */ |
| 490 | : "r" (gen_func) |
| 491 | : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14"); |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 492 | #elif defined(TARGET_I386) && defined(USE_CODE_COPY) |
| 493 | { |
| 494 | if (!(tb->cflags & CF_CODE_COPY)) { |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 495 | if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) { |
| 496 | save_native_fp_state(env); |
| 497 | } |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 498 | gen_func(); |
| 499 | } else { |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 500 | if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) { |
| 501 | restore_native_fp_state(env); |
| 502 | } |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 503 | /* we work with native eflags */ |
| 504 | CC_SRC = cc_table[CC_OP].compute_all(); |
| 505 | CC_OP = CC_OP_EFLAGS; |
| 506 | asm(".globl exec_loop\n" |
| 507 | "\n" |
| 508 | "debug1:\n" |
| 509 | " pushl %%ebp\n" |
| 510 | " fs movl %10, %9\n" |
| 511 | " fs movl %11, %%eax\n" |
| 512 | " andl $0x400, %%eax\n" |
| 513 | " fs orl %8, %%eax\n" |
| 514 | " pushl %%eax\n" |
| 515 | " popf\n" |
| 516 | " fs movl %%esp, %12\n" |
| 517 | " fs movl %0, %%eax\n" |
| 518 | " fs movl %1, %%ecx\n" |
| 519 | " fs movl %2, %%edx\n" |
| 520 | " fs movl %3, %%ebx\n" |
| 521 | " fs movl %4, %%esp\n" |
| 522 | " fs movl %5, %%ebp\n" |
| 523 | " fs movl %6, %%esi\n" |
| 524 | " fs movl %7, %%edi\n" |
| 525 | " fs jmp *%9\n" |
| 526 | "exec_loop:\n" |
| 527 | " fs movl %%esp, %4\n" |
| 528 | " fs movl %12, %%esp\n" |
| 529 | " fs movl %%eax, %0\n" |
| 530 | " fs movl %%ecx, %1\n" |
| 531 | " fs movl %%edx, %2\n" |
| 532 | " fs movl %%ebx, %3\n" |
| 533 | " fs movl %%ebp, %5\n" |
| 534 | " fs movl %%esi, %6\n" |
| 535 | " fs movl %%edi, %7\n" |
| 536 | " pushf\n" |
| 537 | " popl %%eax\n" |
| 538 | " movl %%eax, %%ecx\n" |
| 539 | " andl $0x400, %%ecx\n" |
| 540 | " shrl $9, %%ecx\n" |
| 541 | " andl $0x8d5, %%eax\n" |
| 542 | " fs movl %%eax, %8\n" |
| 543 | " movl $1, %%eax\n" |
| 544 | " subl %%ecx, %%eax\n" |
| 545 | " fs movl %%eax, %11\n" |
| 546 | " fs movl %9, %%ebx\n" /* get T0 value */ |
| 547 | " popl %%ebp\n" |
| 548 | : |
| 549 | : "m" (*(uint8_t *)offsetof(CPUState, regs[0])), |
| 550 | "m" (*(uint8_t *)offsetof(CPUState, regs[1])), |
| 551 | "m" (*(uint8_t *)offsetof(CPUState, regs[2])), |
| 552 | "m" (*(uint8_t *)offsetof(CPUState, regs[3])), |
| 553 | "m" (*(uint8_t *)offsetof(CPUState, regs[4])), |
| 554 | "m" (*(uint8_t *)offsetof(CPUState, regs[5])), |
| 555 | "m" (*(uint8_t *)offsetof(CPUState, regs[6])), |
| 556 | "m" (*(uint8_t *)offsetof(CPUState, regs[7])), |
| 557 | "m" (*(uint8_t *)offsetof(CPUState, cc_src)), |
| 558 | "m" (*(uint8_t *)offsetof(CPUState, tmp0)), |
| 559 | "a" (gen_func), |
| 560 | "m" (*(uint8_t *)offsetof(CPUState, df)), |
| 561 | "m" (*(uint8_t *)offsetof(CPUState, saved_esp)) |
| 562 | : "%ecx", "%edx" |
| 563 | ); |
| 564 | } |
| 565 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 566 | #else |
| 567 | gen_func(); |
| 568 | #endif |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 569 | env->current_tb = NULL; |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 570 | /* reset soft MMU for next block (it can currently |
| 571 | only be set by a memory fault) */ |
| 572 | #if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU) |
bellard | 3f33731 | 2003-08-20 23:02:09 +0000 | [diff] [blame] | 573 | if (env->hflags & HF_SOFTMMU_MASK) { |
| 574 | env->hflags &= ~HF_SOFTMMU_MASK; |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 575 | /* do not allow linking to another block */ |
| 576 | T0 = 0; |
| 577 | } |
| 578 | #endif |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 579 | } |
| 580 | } else { |
bellard | 0d1a29f | 2004-10-12 22:01:28 +0000 | [diff] [blame] | 581 | env_to_regs(); |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 582 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 583 | } /* for(;;) */ |
| 584 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 585 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 586 | #if defined(TARGET_I386) |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 587 | #if defined(USE_CODE_COPY) |
| 588 | if (env->native_fp_regs) { |
| 589 | save_native_fp_state(env); |
| 590 | } |
| 591 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 592 | /* restore flags in standard format */ |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 593 | env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 594 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 595 | /* restore global registers */ |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 596 | #ifdef reg_EAX |
| 597 | EAX = saved_EAX; |
| 598 | #endif |
| 599 | #ifdef reg_ECX |
| 600 | ECX = saved_ECX; |
| 601 | #endif |
| 602 | #ifdef reg_EDX |
| 603 | EDX = saved_EDX; |
| 604 | #endif |
| 605 | #ifdef reg_EBX |
| 606 | EBX = saved_EBX; |
| 607 | #endif |
| 608 | #ifdef reg_ESP |
| 609 | ESP = saved_ESP; |
| 610 | #endif |
| 611 | #ifdef reg_EBP |
| 612 | EBP = saved_EBP; |
| 613 | #endif |
| 614 | #ifdef reg_ESI |
| 615 | ESI = saved_ESI; |
| 616 | #endif |
| 617 | #ifdef reg_EDI |
| 618 | EDI = saved_EDI; |
| 619 | #endif |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 620 | #elif defined(TARGET_ARM) |
bellard | 1b21b62 | 2003-07-09 17:16:27 +0000 | [diff] [blame] | 621 | env->cpsr = compute_cpsr(); |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 622 | #elif defined(TARGET_SPARC) |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 623 | #elif defined(TARGET_PPC) |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 624 | #else |
| 625 | #error unsupported target CPU |
| 626 | #endif |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 627 | #ifdef __sparc__ |
| 628 | asm volatile ("mov %0, %%i7" : : "r" (saved_i7)); |
| 629 | #endif |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 630 | T0 = saved_T0; |
| 631 | T1 = saved_T1; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 632 | T2 = saved_T2; |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 633 | env = saved_env; |
| 634 | return ret; |
| 635 | } |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 636 | |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 637 | /* must only be called from the generated code as an exception can be |
| 638 | generated */ |
| 639 | void tb_invalidate_page_range(target_ulong start, target_ulong end) |
| 640 | { |
bellard | dc5d0b3 | 2004-06-22 18:43:30 +0000 | [diff] [blame] | 641 | /* XXX: cannot enable it yet because it yields to MMU exception |
| 642 | where NIP != read address on PowerPC */ |
| 643 | #if 0 |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 644 | target_ulong phys_addr; |
| 645 | phys_addr = get_phys_addr_code(env, start); |
| 646 | tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0); |
bellard | dc5d0b3 | 2004-06-22 18:43:30 +0000 | [diff] [blame] | 647 | #endif |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 648 | } |
| 649 | |
bellard | 1a18c71 | 2003-10-30 01:07:51 +0000 | [diff] [blame] | 650 | #if defined(TARGET_I386) && defined(CONFIG_USER_ONLY) |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 651 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 652 | void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector) |
| 653 | { |
| 654 | CPUX86State *saved_env; |
| 655 | |
| 656 | saved_env = env; |
| 657 | env = s; |
bellard | a412ac5 | 2003-07-26 18:01:40 +0000 | [diff] [blame] | 658 | if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) { |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 659 | selector &= 0xffff; |
bellard | 2e255c6 | 2003-08-21 23:25:21 +0000 | [diff] [blame] | 660 | cpu_x86_load_seg_cache(env, seg_reg, selector, |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 661 | (selector << 4), 0xffff, 0); |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 662 | } else { |
bellard | b453b70 | 2004-01-04 15:45:21 +0000 | [diff] [blame] | 663 | load_seg(seg_reg, selector); |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 664 | } |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 665 | env = saved_env; |
| 666 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 667 | |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 668 | void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32) |
| 669 | { |
| 670 | CPUX86State *saved_env; |
| 671 | |
| 672 | saved_env = env; |
| 673 | env = s; |
| 674 | |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 675 | helper_fsave((target_ulong)ptr, data32); |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 676 | |
| 677 | env = saved_env; |
| 678 | } |
| 679 | |
| 680 | void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32) |
| 681 | { |
| 682 | CPUX86State *saved_env; |
| 683 | |
| 684 | saved_env = env; |
| 685 | env = s; |
| 686 | |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 687 | helper_frstor((target_ulong)ptr, data32); |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 688 | |
| 689 | env = saved_env; |
| 690 | } |
| 691 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 692 | #endif /* TARGET_I386 */ |
| 693 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 694 | #if !defined(CONFIG_SOFTMMU) |
| 695 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 696 | #if defined(TARGET_I386) |
| 697 | |
bellard | b56dad1 | 2003-05-08 15:38:04 +0000 | [diff] [blame] | 698 | /* 'pc' is the host PC at which the exception was raised. 'address' is |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 699 | the effective address of the memory exception. 'is_write' is 1 if a |
| 700 | write caused the exception and otherwise 0'. 'old_set' is the |
| 701 | signal set which should be restored */ |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 702 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 703 | int is_write, sigset_t *old_set, |
| 704 | void *puc) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 705 | { |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 706 | TranslationBlock *tb; |
| 707 | int ret; |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 708 | |
bellard | 83479e7 | 2003-06-25 16:12:37 +0000 | [diff] [blame] | 709 | if (cpu_single_env) |
| 710 | env = cpu_single_env; /* XXX: find a correct solution for multithread */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 711 | #if defined(DEBUG_SIGNAL) |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 712 | qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", |
| 713 | pc, address, is_write, *(unsigned long *)old_set); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 714 | #endif |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 715 | /* XXX: locking issue */ |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 716 | if (is_write && page_unprotect(address, pc, puc)) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 717 | return 1; |
| 718 | } |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 719 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 720 | /* see if it is an MMU fault */ |
bellard | 93a40ea | 2003-10-27 21:13:06 +0000 | [diff] [blame] | 721 | ret = cpu_x86_handle_mmu_fault(env, address, is_write, |
| 722 | ((env->hflags & HF_CPL_MASK) == 3), 0); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 723 | if (ret < 0) |
| 724 | return 0; /* not an MMU fault */ |
| 725 | if (ret == 0) |
| 726 | return 1; /* the MMU fault was handled without causing real CPU fault */ |
| 727 | /* now we have a real cpu fault */ |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 728 | tb = tb_find_pc(pc); |
| 729 | if (tb) { |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 730 | /* the PC is inside the translated code. It means that we have |
| 731 | a virtual CPU fault */ |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 732 | cpu_restore_state(tb, env, pc, puc); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 733 | } |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 734 | if (ret == 1) { |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 735 | #if 0 |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 736 | printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n", |
| 737 | env->eip, env->cr[2], env->error_code); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 738 | #endif |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 739 | /* we restore the process signal mask as the sigreturn should |
| 740 | do it (XXX: use sigsetjmp) */ |
| 741 | sigprocmask(SIG_SETMASK, old_set, NULL); |
| 742 | raise_exception_err(EXCP0E_PAGE, env->error_code); |
| 743 | } else { |
| 744 | /* activate soft MMU for this block */ |
bellard | 3f33731 | 2003-08-20 23:02:09 +0000 | [diff] [blame] | 745 | env->hflags |= HF_SOFTMMU_MASK; |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 746 | cpu_resume_from_signal(env, puc); |
bellard | 4cbf74b | 2003-08-10 21:48:43 +0000 | [diff] [blame] | 747 | } |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 748 | /* never comes here */ |
| 749 | return 1; |
| 750 | } |
| 751 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 752 | #elif defined(TARGET_ARM) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 753 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 754 | int is_write, sigset_t *old_set, |
| 755 | void *puc) |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 756 | { |
bellard | 68016c6 | 2005-02-07 23:12:27 +0000 | [diff] [blame] | 757 | TranslationBlock *tb; |
| 758 | int ret; |
| 759 | |
| 760 | if (cpu_single_env) |
| 761 | env = cpu_single_env; /* XXX: find a correct solution for multithread */ |
| 762 | #if defined(DEBUG_SIGNAL) |
| 763 | printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", |
| 764 | pc, address, is_write, *(unsigned long *)old_set); |
| 765 | #endif |
bellard | 9f0777e | 2005-02-02 20:42:01 +0000 | [diff] [blame] | 766 | /* XXX: locking issue */ |
| 767 | if (is_write && page_unprotect(address, pc, puc)) { |
| 768 | return 1; |
| 769 | } |
bellard | 68016c6 | 2005-02-07 23:12:27 +0000 | [diff] [blame] | 770 | /* see if it is an MMU fault */ |
| 771 | ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0); |
| 772 | if (ret < 0) |
| 773 | return 0; /* not an MMU fault */ |
| 774 | if (ret == 0) |
| 775 | return 1; /* the MMU fault was handled without causing real CPU fault */ |
| 776 | /* now we have a real cpu fault */ |
| 777 | tb = tb_find_pc(pc); |
| 778 | if (tb) { |
| 779 | /* the PC is inside the translated code. It means that we have |
| 780 | a virtual CPU fault */ |
| 781 | cpu_restore_state(tb, env, pc, puc); |
| 782 | } |
| 783 | /* we restore the process signal mask as the sigreturn should |
| 784 | do it (XXX: use sigsetjmp) */ |
| 785 | sigprocmask(SIG_SETMASK, old_set, NULL); |
| 786 | cpu_loop_exit(); |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 787 | } |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 788 | #elif defined(TARGET_SPARC) |
| 789 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 790 | int is_write, sigset_t *old_set, |
| 791 | void *puc) |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 792 | { |
bellard | 68016c6 | 2005-02-07 23:12:27 +0000 | [diff] [blame] | 793 | TranslationBlock *tb; |
| 794 | int ret; |
| 795 | |
| 796 | if (cpu_single_env) |
| 797 | env = cpu_single_env; /* XXX: find a correct solution for multithread */ |
| 798 | #if defined(DEBUG_SIGNAL) |
| 799 | printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", |
| 800 | pc, address, is_write, *(unsigned long *)old_set); |
| 801 | #endif |
bellard | b453b70 | 2004-01-04 15:45:21 +0000 | [diff] [blame] | 802 | /* XXX: locking issue */ |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 803 | if (is_write && page_unprotect(address, pc, puc)) { |
bellard | b453b70 | 2004-01-04 15:45:21 +0000 | [diff] [blame] | 804 | return 1; |
| 805 | } |
bellard | 68016c6 | 2005-02-07 23:12:27 +0000 | [diff] [blame] | 806 | /* see if it is an MMU fault */ |
| 807 | ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0); |
| 808 | if (ret < 0) |
| 809 | return 0; /* not an MMU fault */ |
| 810 | if (ret == 0) |
| 811 | return 1; /* the MMU fault was handled without causing real CPU fault */ |
| 812 | /* now we have a real cpu fault */ |
| 813 | tb = tb_find_pc(pc); |
| 814 | if (tb) { |
| 815 | /* the PC is inside the translated code. It means that we have |
| 816 | a virtual CPU fault */ |
| 817 | cpu_restore_state(tb, env, pc, puc); |
| 818 | } |
| 819 | /* we restore the process signal mask as the sigreturn should |
| 820 | do it (XXX: use sigsetjmp) */ |
| 821 | sigprocmask(SIG_SETMASK, old_set, NULL); |
| 822 | cpu_loop_exit(); |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 823 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 824 | #elif defined (TARGET_PPC) |
| 825 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 826 | int is_write, sigset_t *old_set, |
| 827 | void *puc) |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 828 | { |
| 829 | TranslationBlock *tb; |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 830 | int ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 831 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 832 | if (cpu_single_env) |
| 833 | env = cpu_single_env; /* XXX: find a correct solution for multithread */ |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 834 | #if defined(DEBUG_SIGNAL) |
| 835 | printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", |
| 836 | pc, address, is_write, *(unsigned long *)old_set); |
| 837 | #endif |
| 838 | /* XXX: locking issue */ |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 839 | if (is_write && page_unprotect(address, pc, puc)) { |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 840 | return 1; |
| 841 | } |
| 842 | |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 843 | /* see if it is an MMU fault */ |
bellard | 7f957d2 | 2004-01-18 23:19:48 +0000 | [diff] [blame] | 844 | ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0); |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 845 | if (ret < 0) |
| 846 | return 0; /* not an MMU fault */ |
| 847 | if (ret == 0) |
| 848 | return 1; /* the MMU fault was handled without causing real CPU fault */ |
| 849 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 850 | /* now we have a real cpu fault */ |
| 851 | tb = tb_find_pc(pc); |
| 852 | if (tb) { |
| 853 | /* the PC is inside the translated code. It means that we have |
| 854 | a virtual CPU fault */ |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 855 | cpu_restore_state(tb, env, pc, puc); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 856 | } |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 857 | if (ret == 1) { |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 858 | #if 0 |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 859 | printf("PF exception: NIP=0x%08x error=0x%x %p\n", |
| 860 | env->nip, env->error_code, tb); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 861 | #endif |
| 862 | /* we restore the process signal mask as the sigreturn should |
| 863 | do it (XXX: use sigsetjmp) */ |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 864 | sigprocmask(SIG_SETMASK, old_set, NULL); |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 865 | do_raise_exception_err(env->exception_index, env->error_code); |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 866 | } else { |
| 867 | /* activate soft MMU for this block */ |
bellard | fbf9eeb | 2004-04-25 21:21:33 +0000 | [diff] [blame] | 868 | cpu_resume_from_signal(env, puc); |
bellard | ce09776 | 2004-01-04 23:53:18 +0000 | [diff] [blame] | 869 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 870 | /* never comes here */ |
| 871 | return 1; |
| 872 | } |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 873 | #else |
| 874 | #error unsupported target CPU |
| 875 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 876 | |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 877 | #if defined(__i386__) |
| 878 | |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 879 | #if defined(USE_CODE_COPY) |
| 880 | static void cpu_send_trap(unsigned long pc, int trap, |
| 881 | struct ucontext *uc) |
| 882 | { |
| 883 | TranslationBlock *tb; |
| 884 | |
| 885 | if (cpu_single_env) |
| 886 | env = cpu_single_env; /* XXX: find a correct solution for multithread */ |
| 887 | /* now we have a real cpu fault */ |
| 888 | tb = tb_find_pc(pc); |
| 889 | if (tb) { |
| 890 | /* the PC is inside the translated code. It means that we have |
| 891 | a virtual CPU fault */ |
| 892 | cpu_restore_state(tb, env, pc, uc); |
| 893 | } |
| 894 | sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); |
| 895 | raise_exception_err(trap, env->error_code); |
| 896 | } |
| 897 | #endif |
| 898 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 899 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
| 900 | void *puc) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 901 | { |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 902 | struct ucontext *uc = puc; |
| 903 | unsigned long pc; |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 904 | int trapno; |
bellard | 97eb5b1 | 2004-02-25 23:19:55 +0000 | [diff] [blame] | 905 | |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 906 | #ifndef REG_EIP |
| 907 | /* for glibc 2.1 */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 908 | #define REG_EIP EIP |
| 909 | #define REG_ERR ERR |
| 910 | #define REG_TRAPNO TRAPNO |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 911 | #endif |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 912 | pc = uc->uc_mcontext.gregs[REG_EIP]; |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 913 | trapno = uc->uc_mcontext.gregs[REG_TRAPNO]; |
| 914 | #if defined(TARGET_I386) && defined(USE_CODE_COPY) |
| 915 | if (trapno == 0x00 || trapno == 0x05) { |
| 916 | /* send division by zero or bound exception */ |
| 917 | cpu_send_trap(pc, trapno, uc); |
| 918 | return 1; |
| 919 | } else |
| 920 | #endif |
| 921 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
| 922 | trapno == 0xe ? |
| 923 | (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0, |
| 924 | &uc->uc_sigmask, puc); |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 925 | } |
| 926 | |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 927 | #elif defined(__x86_64__) |
| 928 | |
| 929 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
| 930 | void *puc) |
| 931 | { |
| 932 | struct ucontext *uc = puc; |
| 933 | unsigned long pc; |
| 934 | |
| 935 | pc = uc->uc_mcontext.gregs[REG_RIP]; |
| 936 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
| 937 | uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ? |
| 938 | (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0, |
| 939 | &uc->uc_sigmask, puc); |
| 940 | } |
| 941 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 942 | #elif defined(__powerpc__) |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 943 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 944 | /*********************************************************************** |
| 945 | * signal context platform-specific definitions |
| 946 | * From Wine |
| 947 | */ |
| 948 | #ifdef linux |
| 949 | /* All Registers access - only for local access */ |
| 950 | # define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name) |
| 951 | /* Gpr Registers access */ |
| 952 | # define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context) |
| 953 | # define IAR_sig(context) REG_sig(nip, context) /* Program counter */ |
| 954 | # define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */ |
| 955 | # define CTR_sig(context) REG_sig(ctr, context) /* Count register */ |
| 956 | # define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */ |
| 957 | # define LR_sig(context) REG_sig(link, context) /* Link register */ |
| 958 | # define CR_sig(context) REG_sig(ccr, context) /* Condition register */ |
| 959 | /* Float Registers access */ |
| 960 | # define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num]) |
| 961 | # define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4))) |
| 962 | /* Exception Registers access */ |
| 963 | # define DAR_sig(context) REG_sig(dar, context) |
| 964 | # define DSISR_sig(context) REG_sig(dsisr, context) |
| 965 | # define TRAP_sig(context) REG_sig(trap, context) |
| 966 | #endif /* linux */ |
| 967 | |
| 968 | #ifdef __APPLE__ |
| 969 | # include <sys/ucontext.h> |
| 970 | typedef struct ucontext SIGCONTEXT; |
| 971 | /* All Registers access - only for local access */ |
| 972 | # define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name) |
| 973 | # define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name) |
| 974 | # define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name) |
| 975 | # define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name) |
| 976 | /* Gpr Registers access */ |
| 977 | # define GPR_sig(reg_num, context) REG_sig(r##reg_num, context) |
| 978 | # define IAR_sig(context) REG_sig(srr0, context) /* Program counter */ |
| 979 | # define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */ |
| 980 | # define CTR_sig(context) REG_sig(ctr, context) |
| 981 | # define XER_sig(context) REG_sig(xer, context) /* Link register */ |
| 982 | # define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */ |
| 983 | # define CR_sig(context) REG_sig(cr, context) /* Condition register */ |
| 984 | /* Float Registers access */ |
| 985 | # define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context) |
| 986 | # define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context)) |
| 987 | /* Exception Registers access */ |
| 988 | # define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */ |
| 989 | # define DSISR_sig(context) EXCEPREG_sig(dsisr, context) |
| 990 | # define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */ |
| 991 | #endif /* __APPLE__ */ |
| 992 | |
bellard | d1d9f42 | 2004-07-14 17:20:55 +0000 | [diff] [blame] | 993 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 994 | void *puc) |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 995 | { |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 996 | struct ucontext *uc = puc; |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 997 | unsigned long pc; |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 998 | int is_write; |
| 999 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1000 | pc = IAR_sig(uc); |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1001 | is_write = 0; |
| 1002 | #if 0 |
| 1003 | /* ppc 4xx case */ |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1004 | if (DSISR_sig(uc) & 0x00800000) |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1005 | is_write = 1; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1006 | #else |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1007 | if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 1008 | is_write = 1; |
| 1009 | #endif |
| 1010 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1011 | is_write, &uc->uc_sigmask, puc); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1012 | } |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1013 | |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1014 | #elif defined(__alpha__) |
| 1015 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1016 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1017 | void *puc) |
| 1018 | { |
| 1019 | struct ucontext *uc = puc; |
| 1020 | uint32_t *pc = uc->uc_mcontext.sc_pc; |
| 1021 | uint32_t insn = *pc; |
| 1022 | int is_write = 0; |
| 1023 | |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1024 | /* XXX: need kernel patch to get write flag faster */ |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1025 | switch (insn >> 26) { |
| 1026 | case 0x0d: // stw |
| 1027 | case 0x0e: // stb |
| 1028 | case 0x0f: // stq_u |
| 1029 | case 0x24: // stf |
| 1030 | case 0x25: // stg |
| 1031 | case 0x26: // sts |
| 1032 | case 0x27: // stt |
| 1033 | case 0x2c: // stl |
| 1034 | case 0x2d: // stq |
| 1035 | case 0x2e: // stl_c |
| 1036 | case 0x2f: // stq_c |
| 1037 | is_write = 1; |
| 1038 | } |
| 1039 | |
| 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 | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 1042 | } |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1043 | #elif defined(__sparc__) |
| 1044 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1045 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
| 1046 | void *puc) |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1047 | { |
| 1048 | uint32_t *regs = (uint32_t *)(info + 1); |
| 1049 | void *sigmask = (regs + 20); |
| 1050 | unsigned long pc; |
| 1051 | int is_write; |
| 1052 | uint32_t insn; |
| 1053 | |
| 1054 | /* XXX: is there a standard glibc define ? */ |
| 1055 | pc = regs[1]; |
| 1056 | /* XXX: need kernel patch to get write flag faster */ |
| 1057 | is_write = 0; |
| 1058 | insn = *(uint32_t *)pc; |
| 1059 | if ((insn >> 30) == 3) { |
| 1060 | switch((insn >> 19) & 0x3f) { |
| 1061 | case 0x05: // stb |
| 1062 | case 0x06: // sth |
| 1063 | case 0x04: // st |
| 1064 | case 0x07: // std |
| 1065 | case 0x24: // stf |
| 1066 | case 0x27: // stdf |
| 1067 | case 0x25: // stfsr |
| 1068 | is_write = 1; |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1073 | is_write, sigmask, NULL); |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | #elif defined(__arm__) |
| 1077 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame] | 1078 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
| 1079 | void *puc) |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 1080 | { |
| 1081 | struct ucontext *uc = puc; |
| 1082 | unsigned long pc; |
| 1083 | int is_write; |
| 1084 | |
| 1085 | pc = uc->uc_mcontext.gregs[R15]; |
| 1086 | /* XXX: compute is_write */ |
| 1087 | is_write = 0; |
| 1088 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
| 1089 | is_write, |
| 1090 | &uc->uc_sigmask); |
| 1091 | } |
| 1092 | |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1093 | #elif defined(__mc68000) |
| 1094 | |
| 1095 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
| 1096 | void *puc) |
| 1097 | { |
| 1098 | struct ucontext *uc = puc; |
| 1099 | unsigned long pc; |
| 1100 | int is_write; |
| 1101 | |
| 1102 | pc = uc->uc_mcontext.gregs[16]; |
| 1103 | /* XXX: compute is_write */ |
| 1104 | is_write = 0; |
| 1105 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
| 1106 | is_write, |
bellard | bf3e8bf | 2004-02-16 21:58:54 +0000 | [diff] [blame] | 1107 | &uc->uc_sigmask, puc); |
bellard | 38e584a | 2003-08-10 22:14:22 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1110 | #else |
| 1111 | |
bellard | 3fb2ded | 2003-06-24 13:22:59 +0000 | [diff] [blame] | 1112 | #error host CPU specific signal handler needed |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 1113 | |
| 1114 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1115 | |
| 1116 | #endif /* !defined(CONFIG_SOFTMMU) */ |