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" |
| 21 | #ifdef TARGET_I386 |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 22 | #include "exec-i386.h" |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 23 | #endif |
| 24 | #ifdef TARGET_ARM |
| 25 | #include "exec-arm.h" |
| 26 | #endif |
| 27 | |
bellard | 956034d | 2003-04-29 20:40:53 +0000 | [diff] [blame] | 28 | #include "disas.h" |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 29 | |
bellard | dc99065 | 2003-03-19 00:00:28 +0000 | [diff] [blame] | 30 | //#define DEBUG_EXEC |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 31 | //#define DEBUG_SIGNAL |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 32 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 33 | #if defined(TARGET_ARM) |
| 34 | /* XXX: unify with i386 target */ |
| 35 | void cpu_loop_exit(void) |
| 36 | { |
| 37 | longjmp(env->jmp_env, 1); |
| 38 | } |
| 39 | #endif |
| 40 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 41 | /* main execution loop */ |
| 42 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 43 | int cpu_exec(CPUState *env1) |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 44 | { |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 45 | int saved_T0, saved_T1, saved_T2; |
| 46 | CPUState *saved_env; |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 47 | #ifdef reg_EAX |
| 48 | int saved_EAX; |
| 49 | #endif |
| 50 | #ifdef reg_ECX |
| 51 | int saved_ECX; |
| 52 | #endif |
| 53 | #ifdef reg_EDX |
| 54 | int saved_EDX; |
| 55 | #endif |
| 56 | #ifdef reg_EBX |
| 57 | int saved_EBX; |
| 58 | #endif |
| 59 | #ifdef reg_ESP |
| 60 | int saved_ESP; |
| 61 | #endif |
| 62 | #ifdef reg_EBP |
| 63 | int saved_EBP; |
| 64 | #endif |
| 65 | #ifdef reg_ESI |
| 66 | int saved_ESI; |
| 67 | #endif |
| 68 | #ifdef reg_EDI |
| 69 | int saved_EDI; |
| 70 | #endif |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 71 | #ifdef __sparc__ |
| 72 | int saved_i7, tmp_T0; |
| 73 | #endif |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 74 | int code_gen_size, ret; |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 75 | void (*gen_func)(void); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 76 | TranslationBlock *tb, **ptb; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 77 | uint8_t *tc_ptr, *cs_base, *pc; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 78 | unsigned int flags; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 79 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 80 | /* first we save global registers */ |
| 81 | saved_T0 = T0; |
| 82 | saved_T1 = T1; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 83 | saved_T2 = T2; |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 84 | saved_env = env; |
| 85 | env = env1; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 86 | #ifdef __sparc__ |
| 87 | /* we also save i7 because longjmp may not restore it */ |
| 88 | asm volatile ("mov %%i7, %0" : "=r" (saved_i7)); |
| 89 | #endif |
| 90 | |
| 91 | #if defined(TARGET_I386) |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 92 | #ifdef reg_EAX |
| 93 | saved_EAX = EAX; |
| 94 | EAX = env->regs[R_EAX]; |
| 95 | #endif |
| 96 | #ifdef reg_ECX |
| 97 | saved_ECX = ECX; |
| 98 | ECX = env->regs[R_ECX]; |
| 99 | #endif |
| 100 | #ifdef reg_EDX |
| 101 | saved_EDX = EDX; |
| 102 | EDX = env->regs[R_EDX]; |
| 103 | #endif |
| 104 | #ifdef reg_EBX |
| 105 | saved_EBX = EBX; |
| 106 | EBX = env->regs[R_EBX]; |
| 107 | #endif |
| 108 | #ifdef reg_ESP |
| 109 | saved_ESP = ESP; |
| 110 | ESP = env->regs[R_ESP]; |
| 111 | #endif |
| 112 | #ifdef reg_EBP |
| 113 | saved_EBP = EBP; |
| 114 | EBP = env->regs[R_EBP]; |
| 115 | #endif |
| 116 | #ifdef reg_ESI |
| 117 | saved_ESI = ESI; |
| 118 | ESI = env->regs[R_ESI]; |
| 119 | #endif |
| 120 | #ifdef reg_EDI |
| 121 | saved_EDI = EDI; |
| 122 | EDI = env->regs[R_EDI]; |
| 123 | #endif |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 124 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 125 | /* put eflags in CPU temporary format */ |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 126 | CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
| 127 | DF = 1 - (2 * ((env->eflags >> 10) & 1)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 128 | CC_OP = CC_OP_EFLAGS; |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 129 | 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^] | 130 | #elif defined(TARGET_ARM) |
| 131 | { |
| 132 | unsigned int psr; |
| 133 | psr = env->cpsr; |
| 134 | env->CF = (psr >> 29) & 1; |
| 135 | env->NZF = (psr & 0xc0000000) ^ 0x40000000; |
| 136 | env->VF = (psr << 3) & 0x80000000; |
| 137 | env->cpsr = psr & ~0xf0000000; |
| 138 | } |
| 139 | #else |
| 140 | #error unsupported target CPU |
| 141 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 142 | env->interrupt_request = 0; |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 143 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 144 | /* prepare setjmp context for exception handling */ |
| 145 | if (setjmp(env->jmp_env) == 0) { |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 146 | T0 = 0; /* force lookup of first TB */ |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 147 | for(;;) { |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 148 | #ifdef __sparc__ |
| 149 | /* g1 can be modified by some libc? functions */ |
| 150 | tmp_T0 = T0; |
| 151 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 152 | if (env->interrupt_request) { |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 153 | env->exception_index = EXCP_INTERRUPT; |
| 154 | cpu_loop_exit(); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 155 | } |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 156 | #ifdef DEBUG_EXEC |
| 157 | if (loglevel) { |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 158 | #if defined(TARGET_I386) |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 159 | /* restore flags in standard format */ |
| 160 | env->regs[R_EAX] = EAX; |
| 161 | env->regs[R_EBX] = EBX; |
| 162 | env->regs[R_ECX] = ECX; |
| 163 | env->regs[R_EDX] = EDX; |
| 164 | env->regs[R_ESI] = ESI; |
| 165 | env->regs[R_EDI] = EDI; |
| 166 | env->regs[R_EBP] = EBP; |
| 167 | env->regs[R_ESP] = ESP; |
| 168 | env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK); |
| 169 | cpu_x86_dump_state(env, logfile, 0); |
| 170 | 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^] | 171 | #elif defined(TARGET_ARM) |
| 172 | cpu_arm_dump_state(env, logfile, 0); |
| 173 | #else |
| 174 | #error unsupported target CPU |
| 175 | #endif |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 176 | } |
| 177 | #endif |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 178 | /* we compute the CPU state. We assume it will not |
| 179 | change during the whole generated block. */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 180 | #if defined(TARGET_I386) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 181 | flags = env->seg_cache[R_CS].seg_32bit << GEN_FLAG_CODE32_SHIFT; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 182 | flags |= env->seg_cache[R_SS].seg_32bit << GEN_FLAG_SS32_SHIFT; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 183 | flags |= (((unsigned long)env->seg_cache[R_DS].base | |
| 184 | (unsigned long)env->seg_cache[R_ES].base | |
| 185 | (unsigned long)env->seg_cache[R_SS].base) != 0) << |
| 186 | GEN_FLAG_ADDSEG_SHIFT; |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 187 | if (!(env->eflags & VM_MASK)) { |
| 188 | flags |= (env->segs[R_CS] & 3) << GEN_FLAG_CPL_SHIFT; |
| 189 | } else { |
| 190 | /* NOTE: a dummy CPL is kept */ |
| 191 | flags |= (1 << GEN_FLAG_VM_SHIFT); |
| 192 | flags |= (3 << GEN_FLAG_CPL_SHIFT); |
| 193 | } |
bellard | cf25629 | 2003-05-25 19:20:31 +0000 | [diff] [blame] | 194 | flags |= (env->eflags & (IOPL_MASK | TF_MASK)); |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 195 | cs_base = env->seg_cache[R_CS].base; |
| 196 | pc = cs_base + env->eip; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 197 | #elif defined(TARGET_ARM) |
| 198 | flags = 0; |
| 199 | cs_base = 0; |
| 200 | pc = (uint8_t *)env->regs[15]; |
| 201 | #else |
| 202 | #error unsupported CPU |
| 203 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 204 | tb = tb_find(&ptb, (unsigned long)pc, (unsigned long)cs_base, |
| 205 | flags); |
| 206 | if (!tb) { |
bellard | cf25629 | 2003-05-25 19:20:31 +0000 | [diff] [blame] | 207 | spin_lock(&tb_lock); |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 208 | /* if no translated code available, then translate it now */ |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 209 | tb = tb_alloc((unsigned long)pc); |
| 210 | if (!tb) { |
| 211 | /* flush must be done */ |
| 212 | tb_flush(); |
| 213 | /* cannot fail at this point */ |
| 214 | tb = tb_alloc((unsigned long)pc); |
| 215 | /* don't forget to invalidate previous TB info */ |
| 216 | ptb = &tb_hash[tb_hash_func((unsigned long)pc)]; |
| 217 | T0 = 0; |
| 218 | } |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 219 | tc_ptr = code_gen_ptr; |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 220 | tb->tc_ptr = tc_ptr; |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 221 | tb->cs_base = (unsigned long)cs_base; |
| 222 | tb->flags = flags; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 223 | ret = cpu_gen_code(tb, CODE_GEN_MAX_SIZE, &code_gen_size); |
| 224 | #if defined(TARGET_I386) |
| 225 | /* XXX: suppress that, this is incorrect */ |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 226 | /* if invalid instruction, signal it */ |
| 227 | if (ret != 0) { |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 228 | /* NOTE: the tb is allocated but not linked, so we |
| 229 | can leave it */ |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 230 | spin_unlock(&tb_lock); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 231 | raise_exception(EXCP06_ILLOP); |
| 232 | } |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 233 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 234 | *ptb = tb; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 235 | tb->hash_next = NULL; |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 236 | tb_link(tb); |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 237 | code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1)); |
bellard | cf25629 | 2003-05-25 19:20:31 +0000 | [diff] [blame] | 238 | spin_unlock(&tb_lock); |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 239 | } |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 240 | #ifdef DEBUG_EXEC |
bellard | 956034d | 2003-04-29 20:40:53 +0000 | [diff] [blame] | 241 | if (loglevel) { |
| 242 | fprintf(logfile, "Trace 0x%08lx [0x%08lx] %s\n", |
| 243 | (long)tb->tc_ptr, (long)tb->pc, |
| 244 | lookup_symbol((void *)tb->pc)); |
bellard | 956034d | 2003-04-29 20:40:53 +0000 | [diff] [blame] | 245 | } |
bellard | 9d27abd | 2003-05-10 13:13:54 +0000 | [diff] [blame] | 246 | #endif |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 247 | #ifdef __sparc__ |
| 248 | T0 = tmp_T0; |
| 249 | #endif |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 250 | /* see if we can patch the calling TB. XXX: remove TF test */ |
| 251 | if (T0 != 0 |
| 252 | #if defined(TARGET_I386) |
| 253 | && !(env->eflags & TF_MASK) |
| 254 | #endif |
| 255 | ) { |
bellard | cf25629 | 2003-05-25 19:20:31 +0000 | [diff] [blame] | 256 | spin_lock(&tb_lock); |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 257 | tb_add_jump((TranslationBlock *)(T0 & ~3), T0 & 3, tb); |
bellard | cf25629 | 2003-05-25 19:20:31 +0000 | [diff] [blame] | 258 | spin_unlock(&tb_lock); |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 259 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 260 | tc_ptr = tb->tc_ptr; |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 261 | |
| 262 | /* execute the generated code */ |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 263 | gen_func = (void *)tc_ptr; |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 264 | #if defined(__sparc__) |
bellard | ae22853 | 2003-05-13 18:59:59 +0000 | [diff] [blame] | 265 | __asm__ __volatile__("call %0\n\t" |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 266 | "mov %%o7,%%i0" |
bellard | ae22853 | 2003-05-13 18:59:59 +0000 | [diff] [blame] | 267 | : /* no outputs */ |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 268 | : "r" (gen_func) |
bellard | ae22853 | 2003-05-13 18:59:59 +0000 | [diff] [blame] | 269 | : "i0", "i1", "i2", "i3", "i4", "i5"); |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 270 | #elif defined(__arm__) |
| 271 | asm volatile ("mov pc, %0\n\t" |
| 272 | ".global exec_loop\n\t" |
| 273 | "exec_loop:\n\t" |
| 274 | : /* no outputs */ |
| 275 | : "r" (gen_func) |
| 276 | : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14"); |
bellard | ae22853 | 2003-05-13 18:59:59 +0000 | [diff] [blame] | 277 | #else |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 278 | gen_func(); |
bellard | ae22853 | 2003-05-13 18:59:59 +0000 | [diff] [blame] | 279 | #endif |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | ret = env->exception_index; |
| 283 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 284 | #if defined(TARGET_I386) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 285 | /* restore flags in standard format */ |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 286 | env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 287 | |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 288 | /* restore global registers */ |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 289 | #ifdef reg_EAX |
| 290 | EAX = saved_EAX; |
| 291 | #endif |
| 292 | #ifdef reg_ECX |
| 293 | ECX = saved_ECX; |
| 294 | #endif |
| 295 | #ifdef reg_EDX |
| 296 | EDX = saved_EDX; |
| 297 | #endif |
| 298 | #ifdef reg_EBX |
| 299 | EBX = saved_EBX; |
| 300 | #endif |
| 301 | #ifdef reg_ESP |
| 302 | ESP = saved_ESP; |
| 303 | #endif |
| 304 | #ifdef reg_EBP |
| 305 | EBP = saved_EBP; |
| 306 | #endif |
| 307 | #ifdef reg_ESI |
| 308 | ESI = saved_ESI; |
| 309 | #endif |
| 310 | #ifdef reg_EDI |
| 311 | EDI = saved_EDI; |
| 312 | #endif |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 313 | #elif defined(TARGET_ARM) |
| 314 | { |
| 315 | int ZF; |
| 316 | ZF = (env->NZF == 0); |
| 317 | env->cpsr = env->cpsr | (env->NZF & 0x80000000) | (ZF << 30) | |
| 318 | (env->CF << 29) | ((env->VF & 0x80000000) >> 3); |
| 319 | } |
| 320 | #else |
| 321 | #error unsupported target CPU |
| 322 | #endif |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 323 | #ifdef __sparc__ |
| 324 | asm volatile ("mov %0, %%i7" : : "r" (saved_i7)); |
| 325 | #endif |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 326 | T0 = saved_T0; |
| 327 | T1 = saved_T1; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 328 | T2 = saved_T2; |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 329 | env = saved_env; |
| 330 | return ret; |
| 331 | } |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 332 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 333 | void cpu_interrupt(CPUState *s) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 334 | { |
| 335 | s->interrupt_request = 1; |
| 336 | } |
| 337 | |
| 338 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 339 | #if defined(TARGET_I386) |
| 340 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 341 | void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector) |
| 342 | { |
| 343 | CPUX86State *saved_env; |
| 344 | |
| 345 | saved_env = env; |
| 346 | env = s; |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 347 | if (env->eflags & VM_MASK) { |
| 348 | SegmentCache *sc; |
| 349 | selector &= 0xffff; |
| 350 | sc = &env->seg_cache[seg_reg]; |
| 351 | /* NOTE: in VM86 mode, limit and seg_32bit are never reloaded, |
| 352 | so we must load them here */ |
| 353 | sc->base = (void *)(selector << 4); |
| 354 | sc->limit = 0xffff; |
| 355 | sc->seg_32bit = 0; |
| 356 | env->segs[seg_reg] = selector; |
| 357 | } else { |
| 358 | load_seg(seg_reg, selector, 0); |
| 359 | } |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 360 | env = saved_env; |
| 361 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 362 | |
bellard | d0a1ffc | 2003-05-29 20:04:28 +0000 | [diff] [blame] | 363 | void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32) |
| 364 | { |
| 365 | CPUX86State *saved_env; |
| 366 | |
| 367 | saved_env = env; |
| 368 | env = s; |
| 369 | |
| 370 | helper_fsave(ptr, data32); |
| 371 | |
| 372 | env = saved_env; |
| 373 | } |
| 374 | |
| 375 | void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32) |
| 376 | { |
| 377 | CPUX86State *saved_env; |
| 378 | |
| 379 | saved_env = env; |
| 380 | env = s; |
| 381 | |
| 382 | helper_frstor(ptr, data32); |
| 383 | |
| 384 | env = saved_env; |
| 385 | } |
| 386 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 387 | #endif /* TARGET_I386 */ |
| 388 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 389 | #undef EAX |
| 390 | #undef ECX |
| 391 | #undef EDX |
| 392 | #undef EBX |
| 393 | #undef ESP |
| 394 | #undef EBP |
| 395 | #undef ESI |
| 396 | #undef EDI |
| 397 | #undef EIP |
| 398 | #include <signal.h> |
| 399 | #include <sys/ucontext.h> |
| 400 | |
bellard | b56dad1 | 2003-05-08 15:38:04 +0000 | [diff] [blame] | 401 | /* 'pc' is the host PC at which the exception was raised. 'address' is |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 402 | the effective address of the memory exception. 'is_write' is 1 if a |
| 403 | write caused the exception and otherwise 0'. 'old_set' is the |
| 404 | signal set which should be restored */ |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 405 | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
| 406 | int is_write, sigset_t *old_set) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 407 | { |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 408 | TranslationBlock *tb; |
| 409 | int ret; |
| 410 | uint32_t found_pc; |
| 411 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 412 | #if defined(DEBUG_SIGNAL) |
| 413 | printf("qemu: SIGSEGV pc=0x%08lx address=%08lx wr=%d oldset=0x%08lx\n", |
| 414 | pc, address, is_write, *(unsigned long *)old_set); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 415 | #endif |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 416 | /* XXX: locking issue */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 417 | if (is_write && page_unprotect(address)) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 418 | return 1; |
| 419 | } |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 420 | tb = tb_find_pc(pc); |
| 421 | if (tb) { |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 422 | /* the PC is inside the translated code. It means that we have |
| 423 | a virtual CPU fault */ |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 424 | ret = cpu_search_pc(tb, &found_pc, pc); |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 425 | if (ret < 0) |
| 426 | return 0; |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 427 | #if defined(TARGET_I386) |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 428 | env->eip = found_pc - tb->cs_base; |
bellard | b56dad1 | 2003-05-08 15:38:04 +0000 | [diff] [blame] | 429 | env->cr2 = address; |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 430 | /* we restore the process signal mask as the sigreturn should |
| 431 | do it (XXX: use sigsetjmp) */ |
| 432 | sigprocmask(SIG_SETMASK, old_set, NULL); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 433 | raise_exception_err(EXCP0E_PAGE, 4 | (is_write << 1)); |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 434 | #elif defined(TARGET_ARM) |
| 435 | env->regs[15] = found_pc; |
| 436 | /* XXX: do more */ |
| 437 | #else |
| 438 | #error unsupported target CPU |
| 439 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 440 | /* never comes here */ |
| 441 | return 1; |
| 442 | } else { |
| 443 | return 0; |
| 444 | } |
| 445 | } |
| 446 | |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 447 | #if defined(__i386__) |
| 448 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 449 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
| 450 | void *puc) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 451 | { |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 452 | struct ucontext *uc = puc; |
| 453 | unsigned long pc; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 454 | |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 455 | #ifndef REG_EIP |
| 456 | /* for glibc 2.1 */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 457 | #define REG_EIP EIP |
| 458 | #define REG_ERR ERR |
| 459 | #define REG_TRAPNO TRAPNO |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 460 | #endif |
bellard | fc2b4c4 | 2003-03-29 16:52:44 +0000 | [diff] [blame] | 461 | pc = uc->uc_mcontext.gregs[REG_EIP]; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 462 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
| 463 | uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ? |
| 464 | (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0, |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 465 | &uc->uc_sigmask); |
| 466 | } |
| 467 | |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 468 | #elif defined(__powerpc) |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 469 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 470 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
| 471 | void *puc) |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 472 | { |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 473 | struct ucontext *uc = puc; |
| 474 | struct pt_regs *regs = uc->uc_mcontext.regs; |
| 475 | unsigned long pc; |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 476 | int is_write; |
| 477 | |
| 478 | pc = regs->nip; |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 479 | is_write = 0; |
| 480 | #if 0 |
| 481 | /* ppc 4xx case */ |
| 482 | if (regs->dsisr & 0x00800000) |
| 483 | is_write = 1; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 484 | #else |
bellard | 25eb448 | 2003-05-14 21:50:54 +0000 | [diff] [blame] | 485 | if (regs->trap != 0x400 && (regs->dsisr & 0x02000000)) |
| 486 | is_write = 1; |
| 487 | #endif |
| 488 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 489 | is_write, &uc->uc_sigmask); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 490 | } |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 491 | |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 492 | #elif defined(__alpha__) |
| 493 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 494 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 495 | void *puc) |
| 496 | { |
| 497 | struct ucontext *uc = puc; |
| 498 | uint32_t *pc = uc->uc_mcontext.sc_pc; |
| 499 | uint32_t insn = *pc; |
| 500 | int is_write = 0; |
| 501 | |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 502 | /* XXX: need kernel patch to get write flag faster */ |
bellard | 2f87c60 | 2003-06-02 20:38:09 +0000 | [diff] [blame] | 503 | switch (insn >> 26) { |
| 504 | case 0x0d: // stw |
| 505 | case 0x0e: // stb |
| 506 | case 0x0f: // stq_u |
| 507 | case 0x24: // stf |
| 508 | case 0x25: // stg |
| 509 | case 0x26: // sts |
| 510 | case 0x27: // stt |
| 511 | case 0x2c: // stl |
| 512 | case 0x2d: // stq |
| 513 | case 0x2e: // stl_c |
| 514 | case 0x2f: // stq_c |
| 515 | is_write = 1; |
| 516 | } |
| 517 | |
| 518 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
| 519 | is_write, &uc->uc_sigmask); |
| 520 | } |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 521 | #elif defined(__sparc__) |
| 522 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 523 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
| 524 | void *puc) |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 525 | { |
| 526 | uint32_t *regs = (uint32_t *)(info + 1); |
| 527 | void *sigmask = (regs + 20); |
| 528 | unsigned long pc; |
| 529 | int is_write; |
| 530 | uint32_t insn; |
| 531 | |
| 532 | /* XXX: is there a standard glibc define ? */ |
| 533 | pc = regs[1]; |
| 534 | /* XXX: need kernel patch to get write flag faster */ |
| 535 | is_write = 0; |
| 536 | insn = *(uint32_t *)pc; |
| 537 | if ((insn >> 30) == 3) { |
| 538 | switch((insn >> 19) & 0x3f) { |
| 539 | case 0x05: // stb |
| 540 | case 0x06: // sth |
| 541 | case 0x04: // st |
| 542 | case 0x07: // std |
| 543 | case 0x24: // stf |
| 544 | case 0x27: // stdf |
| 545 | case 0x25: // stfsr |
| 546 | is_write = 1; |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
| 551 | is_write, sigmask); |
| 552 | } |
| 553 | |
| 554 | #elif defined(__arm__) |
| 555 | |
bellard | e4533c7 | 2003-06-15 19:51:39 +0000 | [diff] [blame^] | 556 | int cpu_signal_handler(int host_signum, struct siginfo *info, |
| 557 | void *puc) |
bellard | 8c6939c | 2003-06-09 15:28:00 +0000 | [diff] [blame] | 558 | { |
| 559 | struct ucontext *uc = puc; |
| 560 | unsigned long pc; |
| 561 | int is_write; |
| 562 | |
| 563 | pc = uc->uc_mcontext.gregs[R15]; |
| 564 | /* XXX: compute is_write */ |
| 565 | is_write = 0; |
| 566 | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
| 567 | is_write, |
| 568 | &uc->uc_sigmask); |
| 569 | } |
| 570 | |
bellard | 2b41314 | 2003-05-14 23:01:10 +0000 | [diff] [blame] | 571 | #else |
| 572 | |
| 573 | #error CPU specific signal handler needed |
| 574 | |
| 575 | #endif |