root / cpu-exec.c @ b6d78bfa
History | View | Annotate | Download (18.4 kB)
1 | 7d13299d | bellard | /*
|
---|---|---|---|
2 | 7d13299d | bellard | * i386 emulator main execution loop
|
3 | 7d13299d | bellard | *
|
4 | 7d13299d | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 7d13299d | bellard | *
|
6 | 3ef693a0 | bellard | * This library is free software; you can redistribute it and/or
|
7 | 3ef693a0 | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | 3ef693a0 | bellard | * License as published by the Free Software Foundation; either
|
9 | 3ef693a0 | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | 7d13299d | bellard | *
|
11 | 3ef693a0 | bellard | * This library is distributed in the hope that it will be useful,
|
12 | 3ef693a0 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 3ef693a0 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 3ef693a0 | bellard | * Lesser General Public License for more details.
|
15 | 7d13299d | bellard | *
|
16 | 3ef693a0 | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | 3ef693a0 | bellard | * License along with this library; if not, write to the Free Software
|
18 | 3ef693a0 | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | 7d13299d | bellard | */
|
20 | e4533c7a | bellard | #include "config.h" |
21 | e4533c7a | bellard | #ifdef TARGET_I386
|
22 | 7d13299d | bellard | #include "exec-i386.h" |
23 | e4533c7a | bellard | #endif
|
24 | e4533c7a | bellard | #ifdef TARGET_ARM
|
25 | e4533c7a | bellard | #include "exec-arm.h" |
26 | e4533c7a | bellard | #endif
|
27 | e4533c7a | bellard | |
28 | 956034d7 | bellard | #include "disas.h" |
29 | 7d13299d | bellard | |
30 | dc99065b | bellard | //#define DEBUG_EXEC
|
31 | 9de5e440 | bellard | //#define DEBUG_SIGNAL
|
32 | 7d13299d | bellard | |
33 | e4533c7a | bellard | #if defined(TARGET_ARM)
|
34 | e4533c7a | bellard | /* XXX: unify with i386 target */
|
35 | e4533c7a | bellard | void cpu_loop_exit(void) |
36 | e4533c7a | bellard | { |
37 | e4533c7a | bellard | longjmp(env->jmp_env, 1);
|
38 | e4533c7a | bellard | } |
39 | e4533c7a | bellard | #endif
|
40 | e4533c7a | bellard | |
41 | 7d13299d | bellard | /* main execution loop */
|
42 | 7d13299d | bellard | |
43 | e4533c7a | bellard | int cpu_exec(CPUState *env1)
|
44 | 7d13299d | bellard | { |
45 | e4533c7a | bellard | int saved_T0, saved_T1, saved_T2;
|
46 | e4533c7a | bellard | CPUState *saved_env; |
47 | 04369ff2 | bellard | #ifdef reg_EAX
|
48 | 04369ff2 | bellard | int saved_EAX;
|
49 | 04369ff2 | bellard | #endif
|
50 | 04369ff2 | bellard | #ifdef reg_ECX
|
51 | 04369ff2 | bellard | int saved_ECX;
|
52 | 04369ff2 | bellard | #endif
|
53 | 04369ff2 | bellard | #ifdef reg_EDX
|
54 | 04369ff2 | bellard | int saved_EDX;
|
55 | 04369ff2 | bellard | #endif
|
56 | 04369ff2 | bellard | #ifdef reg_EBX
|
57 | 04369ff2 | bellard | int saved_EBX;
|
58 | 04369ff2 | bellard | #endif
|
59 | 04369ff2 | bellard | #ifdef reg_ESP
|
60 | 04369ff2 | bellard | int saved_ESP;
|
61 | 04369ff2 | bellard | #endif
|
62 | 04369ff2 | bellard | #ifdef reg_EBP
|
63 | 04369ff2 | bellard | int saved_EBP;
|
64 | 04369ff2 | bellard | #endif
|
65 | 04369ff2 | bellard | #ifdef reg_ESI
|
66 | 04369ff2 | bellard | int saved_ESI;
|
67 | 04369ff2 | bellard | #endif
|
68 | 04369ff2 | bellard | #ifdef reg_EDI
|
69 | 04369ff2 | bellard | int saved_EDI;
|
70 | 04369ff2 | bellard | #endif
|
71 | 8c6939c0 | bellard | #ifdef __sparc__
|
72 | 8c6939c0 | bellard | int saved_i7, tmp_T0;
|
73 | 8c6939c0 | bellard | #endif
|
74 | 68a79315 | bellard | int code_gen_size, ret, interrupt_request;
|
75 | 7d13299d | bellard | void (*gen_func)(void); |
76 | 9de5e440 | bellard | TranslationBlock *tb, **ptb; |
77 | dab2ed99 | bellard | uint8_t *tc_ptr, *cs_base, *pc; |
78 | 6dbad63e | bellard | unsigned int flags; |
79 | 8c6939c0 | bellard | |
80 | 7d13299d | bellard | /* first we save global registers */
|
81 | 7d13299d | bellard | saved_T0 = T0; |
82 | 7d13299d | bellard | saved_T1 = T1; |
83 | e4533c7a | bellard | saved_T2 = T2; |
84 | 7d13299d | bellard | saved_env = env; |
85 | 7d13299d | bellard | env = env1; |
86 | e4533c7a | bellard | #ifdef __sparc__
|
87 | e4533c7a | bellard | /* we also save i7 because longjmp may not restore it */
|
88 | e4533c7a | bellard | asm volatile ("mov %%i7, %0" : "=r" (saved_i7)); |
89 | e4533c7a | bellard | #endif
|
90 | e4533c7a | bellard | |
91 | e4533c7a | bellard | #if defined(TARGET_I386)
|
92 | 04369ff2 | bellard | #ifdef reg_EAX
|
93 | 04369ff2 | bellard | saved_EAX = EAX; |
94 | 04369ff2 | bellard | EAX = env->regs[R_EAX]; |
95 | 04369ff2 | bellard | #endif
|
96 | 04369ff2 | bellard | #ifdef reg_ECX
|
97 | 04369ff2 | bellard | saved_ECX = ECX; |
98 | 04369ff2 | bellard | ECX = env->regs[R_ECX]; |
99 | 04369ff2 | bellard | #endif
|
100 | 04369ff2 | bellard | #ifdef reg_EDX
|
101 | 04369ff2 | bellard | saved_EDX = EDX; |
102 | 04369ff2 | bellard | EDX = env->regs[R_EDX]; |
103 | 04369ff2 | bellard | #endif
|
104 | 04369ff2 | bellard | #ifdef reg_EBX
|
105 | 04369ff2 | bellard | saved_EBX = EBX; |
106 | 04369ff2 | bellard | EBX = env->regs[R_EBX]; |
107 | 04369ff2 | bellard | #endif
|
108 | 04369ff2 | bellard | #ifdef reg_ESP
|
109 | 04369ff2 | bellard | saved_ESP = ESP; |
110 | 04369ff2 | bellard | ESP = env->regs[R_ESP]; |
111 | 04369ff2 | bellard | #endif
|
112 | 04369ff2 | bellard | #ifdef reg_EBP
|
113 | 04369ff2 | bellard | saved_EBP = EBP; |
114 | 04369ff2 | bellard | EBP = env->regs[R_EBP]; |
115 | 04369ff2 | bellard | #endif
|
116 | 04369ff2 | bellard | #ifdef reg_ESI
|
117 | 04369ff2 | bellard | saved_ESI = ESI; |
118 | 04369ff2 | bellard | ESI = env->regs[R_ESI]; |
119 | 04369ff2 | bellard | #endif
|
120 | 04369ff2 | bellard | #ifdef reg_EDI
|
121 | 04369ff2 | bellard | saved_EDI = EDI; |
122 | 04369ff2 | bellard | EDI = env->regs[R_EDI]; |
123 | 04369ff2 | bellard | #endif
|
124 | 7d13299d | bellard | |
125 | 9de5e440 | bellard | /* put eflags in CPU temporary format */
|
126 | fc2b4c48 | bellard | CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
127 | fc2b4c48 | bellard | DF = 1 - (2 * ((env->eflags >> 10) & 1)); |
128 | 9de5e440 | bellard | CC_OP = CC_OP_EFLAGS; |
129 | fc2b4c48 | bellard | env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
130 | e4533c7a | bellard | #elif defined(TARGET_ARM)
|
131 | e4533c7a | bellard | { |
132 | e4533c7a | bellard | unsigned int psr; |
133 | e4533c7a | bellard | psr = env->cpsr; |
134 | e4533c7a | bellard | env->CF = (psr >> 29) & 1; |
135 | e4533c7a | bellard | env->NZF = (psr & 0xc0000000) ^ 0x40000000; |
136 | e4533c7a | bellard | env->VF = (psr << 3) & 0x80000000; |
137 | e4533c7a | bellard | env->cpsr = psr & ~0xf0000000;
|
138 | e4533c7a | bellard | } |
139 | e4533c7a | bellard | #else
|
140 | e4533c7a | bellard | #error unsupported target CPU
|
141 | e4533c7a | bellard | #endif
|
142 | 3fb2ded1 | bellard | env->exception_index = -1;
|
143 | 9d27abd9 | bellard | |
144 | 7d13299d | bellard | /* prepare setjmp context for exception handling */
|
145 | 3fb2ded1 | bellard | for(;;) {
|
146 | 3fb2ded1 | bellard | if (setjmp(env->jmp_env) == 0) { |
147 | 3fb2ded1 | bellard | /* if an exception is pending, we execute it here */
|
148 | 3fb2ded1 | bellard | if (env->exception_index >= 0) { |
149 | 3fb2ded1 | bellard | if (env->exception_index >= EXCP_INTERRUPT) {
|
150 | 3fb2ded1 | bellard | /* exit request from the cpu execution loop */
|
151 | 3fb2ded1 | bellard | ret = env->exception_index; |
152 | 3fb2ded1 | bellard | break;
|
153 | 3fb2ded1 | bellard | } else if (env->user_mode_only) { |
154 | 3fb2ded1 | bellard | /* if user mode only, we simulate a fake exception
|
155 | 3fb2ded1 | bellard | which will be hanlded outside the cpu execution
|
156 | 3fb2ded1 | bellard | loop */
|
157 | 83479e77 | bellard | #if defined(TARGET_I386)
|
158 | 3fb2ded1 | bellard | do_interrupt_user(env->exception_index, |
159 | 3fb2ded1 | bellard | env->exception_is_int, |
160 | 3fb2ded1 | bellard | env->error_code, |
161 | 3fb2ded1 | bellard | env->exception_next_eip); |
162 | 83479e77 | bellard | #endif
|
163 | 3fb2ded1 | bellard | ret = env->exception_index; |
164 | 3fb2ded1 | bellard | break;
|
165 | 3fb2ded1 | bellard | } else {
|
166 | 83479e77 | bellard | #if defined(TARGET_I386)
|
167 | 3fb2ded1 | bellard | /* simulate a real cpu exception. On i386, it can
|
168 | 3fb2ded1 | bellard | trigger new exceptions, but we do not handle
|
169 | 3fb2ded1 | bellard | double or triple faults yet. */
|
170 | 3fb2ded1 | bellard | do_interrupt(env->exception_index, |
171 | 3fb2ded1 | bellard | env->exception_is_int, |
172 | 3fb2ded1 | bellard | env->error_code, |
173 | 3fb2ded1 | bellard | env->exception_next_eip); |
174 | 83479e77 | bellard | #endif
|
175 | 3fb2ded1 | bellard | } |
176 | 3fb2ded1 | bellard | env->exception_index = -1;
|
177 | 3fb2ded1 | bellard | } |
178 | 3fb2ded1 | bellard | T0 = 0; /* force lookup of first TB */ |
179 | 3fb2ded1 | bellard | for(;;) {
|
180 | 8c6939c0 | bellard | #ifdef __sparc__
|
181 | 3fb2ded1 | bellard | /* g1 can be modified by some libc? functions */
|
182 | 3fb2ded1 | bellard | tmp_T0 = T0; |
183 | 8c6939c0 | bellard | #endif
|
184 | 68a79315 | bellard | interrupt_request = env->interrupt_request; |
185 | 68a79315 | bellard | if (interrupt_request) {
|
186 | 68a79315 | bellard | #if defined(TARGET_I386)
|
187 | 68a79315 | bellard | /* if hardware interrupt pending, we execute it */
|
188 | 68a79315 | bellard | if ((interrupt_request & CPU_INTERRUPT_HARD) &&
|
189 | 68a79315 | bellard | (env->eflags & IF_MASK)) { |
190 | 68a79315 | bellard | int intno;
|
191 | 68a79315 | bellard | intno = cpu_x86_get_pic_interrupt(env); |
192 | 68a79315 | bellard | if (loglevel) {
|
193 | 68a79315 | bellard | fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
|
194 | 68a79315 | bellard | } |
195 | 68a79315 | bellard | do_interrupt(intno, 0, 0, 0); |
196 | 68a79315 | bellard | env->interrupt_request &= ~CPU_INTERRUPT_HARD; |
197 | 907a5b26 | bellard | /* ensure that no TB jump will be modified as
|
198 | 907a5b26 | bellard | the program flow was changed */
|
199 | 907a5b26 | bellard | #ifdef __sparc__
|
200 | 907a5b26 | bellard | tmp_T0 = 0;
|
201 | 907a5b26 | bellard | #else
|
202 | 907a5b26 | bellard | T0 = 0;
|
203 | 907a5b26 | bellard | #endif
|
204 | 68a79315 | bellard | } |
205 | 68a79315 | bellard | #endif
|
206 | 68a79315 | bellard | if (interrupt_request & CPU_INTERRUPT_EXIT) {
|
207 | 68a79315 | bellard | env->interrupt_request &= ~CPU_INTERRUPT_EXIT; |
208 | 68a79315 | bellard | env->exception_index = EXCP_INTERRUPT; |
209 | 68a79315 | bellard | cpu_loop_exit(); |
210 | 68a79315 | bellard | } |
211 | 3fb2ded1 | bellard | } |
212 | 7d13299d | bellard | #ifdef DEBUG_EXEC
|
213 | 3fb2ded1 | bellard | if (loglevel) {
|
214 | e4533c7a | bellard | #if defined(TARGET_I386)
|
215 | 3fb2ded1 | bellard | /* restore flags in standard format */
|
216 | 3fb2ded1 | bellard | env->regs[R_EAX] = EAX; |
217 | 3fb2ded1 | bellard | env->regs[R_EBX] = EBX; |
218 | 3fb2ded1 | bellard | env->regs[R_ECX] = ECX; |
219 | 3fb2ded1 | bellard | env->regs[R_EDX] = EDX; |
220 | 3fb2ded1 | bellard | env->regs[R_ESI] = ESI; |
221 | 3fb2ded1 | bellard | env->regs[R_EDI] = EDI; |
222 | 3fb2ded1 | bellard | env->regs[R_EBP] = EBP; |
223 | 3fb2ded1 | bellard | env->regs[R_ESP] = ESP; |
224 | 3fb2ded1 | bellard | env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK); |
225 | 68a79315 | bellard | cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP); |
226 | 3fb2ded1 | bellard | env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
227 | e4533c7a | bellard | #elif defined(TARGET_ARM)
|
228 | 1b21b62a | bellard | env->cpsr = compute_cpsr(); |
229 | 3fb2ded1 | bellard | cpu_arm_dump_state(env, logfile, 0);
|
230 | 1b21b62a | bellard | env->cpsr &= ~0xf0000000;
|
231 | e4533c7a | bellard | #else
|
232 | e4533c7a | bellard | #error unsupported target CPU
|
233 | e4533c7a | bellard | #endif
|
234 | 3fb2ded1 | bellard | } |
235 | 7d13299d | bellard | #endif
|
236 | 3fb2ded1 | bellard | /* we compute the CPU state. We assume it will not
|
237 | 3fb2ded1 | bellard | change during the whole generated block. */
|
238 | e4533c7a | bellard | #if defined(TARGET_I386)
|
239 | 3fb2ded1 | bellard | flags = (env->segs[R_CS].flags & DESC_B_MASK) |
240 | 3fb2ded1 | bellard | >> (DESC_B_SHIFT - GEN_FLAG_CODE32_SHIFT); |
241 | 3fb2ded1 | bellard | flags |= (env->segs[R_SS].flags & DESC_B_MASK) |
242 | 3fb2ded1 | bellard | >> (DESC_B_SHIFT - GEN_FLAG_SS32_SHIFT); |
243 | 3fb2ded1 | bellard | flags |= (((unsigned long)env->segs[R_DS].base | |
244 | 3fb2ded1 | bellard | (unsigned long)env->segs[R_ES].base | |
245 | 3fb2ded1 | bellard | (unsigned long)env->segs[R_SS].base) != 0) << |
246 | 3fb2ded1 | bellard | GEN_FLAG_ADDSEG_SHIFT; |
247 | b6d78bfa | bellard | flags |= env->cpl << GEN_FLAG_CPL_SHIFT; |
248 | b6d78bfa | bellard | flags |= (env->eflags & VM_MASK) >> (17 - GEN_FLAG_VM_SHIFT);
|
249 | 3fb2ded1 | bellard | flags |= (env->eflags & (IOPL_MASK | TF_MASK)); |
250 | 3fb2ded1 | bellard | cs_base = env->segs[R_CS].base; |
251 | 3fb2ded1 | bellard | pc = cs_base + env->eip; |
252 | e4533c7a | bellard | #elif defined(TARGET_ARM)
|
253 | 3fb2ded1 | bellard | flags = 0;
|
254 | 3fb2ded1 | bellard | cs_base = 0;
|
255 | 3fb2ded1 | bellard | pc = (uint8_t *)env->regs[15];
|
256 | e4533c7a | bellard | #else
|
257 | e4533c7a | bellard | #error unsupported CPU
|
258 | e4533c7a | bellard | #endif
|
259 | 3fb2ded1 | bellard | tb = tb_find(&ptb, (unsigned long)pc, (unsigned long)cs_base, |
260 | 3fb2ded1 | bellard | flags); |
261 | d4e8164f | bellard | if (!tb) {
|
262 | 3fb2ded1 | bellard | spin_lock(&tb_lock); |
263 | 3fb2ded1 | bellard | /* if no translated code available, then translate it now */
|
264 | d4e8164f | bellard | tb = tb_alloc((unsigned long)pc); |
265 | 3fb2ded1 | bellard | if (!tb) {
|
266 | 3fb2ded1 | bellard | /* flush must be done */
|
267 | 3fb2ded1 | bellard | tb_flush(); |
268 | 3fb2ded1 | bellard | /* cannot fail at this point */
|
269 | 3fb2ded1 | bellard | tb = tb_alloc((unsigned long)pc); |
270 | 3fb2ded1 | bellard | /* don't forget to invalidate previous TB info */
|
271 | 3fb2ded1 | bellard | ptb = &tb_hash[tb_hash_func((unsigned long)pc)]; |
272 | 3fb2ded1 | bellard | T0 = 0;
|
273 | 3fb2ded1 | bellard | } |
274 | 3fb2ded1 | bellard | tc_ptr = code_gen_ptr; |
275 | 3fb2ded1 | bellard | tb->tc_ptr = tc_ptr; |
276 | 3fb2ded1 | bellard | tb->cs_base = (unsigned long)cs_base; |
277 | 3fb2ded1 | bellard | tb->flags = flags; |
278 | 4c3a88a2 | bellard | ret = cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size); |
279 | e4533c7a | bellard | #if defined(TARGET_I386)
|
280 | 3fb2ded1 | bellard | /* XXX: suppress that, this is incorrect */
|
281 | 3fb2ded1 | bellard | /* if invalid instruction, signal it */
|
282 | 3fb2ded1 | bellard | if (ret != 0) { |
283 | 3fb2ded1 | bellard | /* NOTE: the tb is allocated but not linked, so we
|
284 | 3fb2ded1 | bellard | can leave it */
|
285 | 3fb2ded1 | bellard | spin_unlock(&tb_lock); |
286 | 3fb2ded1 | bellard | raise_exception(EXCP06_ILLOP); |
287 | 3fb2ded1 | bellard | } |
288 | 3fb2ded1 | bellard | #endif
|
289 | 3fb2ded1 | bellard | *ptb = tb; |
290 | 3fb2ded1 | bellard | tb->hash_next = NULL;
|
291 | 3fb2ded1 | bellard | tb_link(tb); |
292 | 3fb2ded1 | bellard | code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1)); |
293 | 25eb4484 | bellard | spin_unlock(&tb_lock); |
294 | 9de5e440 | bellard | } |
295 | 9d27abd9 | bellard | #ifdef DEBUG_EXEC
|
296 | 3fb2ded1 | bellard | if (loglevel) {
|
297 | 3fb2ded1 | bellard | fprintf(logfile, "Trace 0x%08lx [0x%08lx] %s\n",
|
298 | 3fb2ded1 | bellard | (long)tb->tc_ptr, (long)tb->pc, |
299 | 3fb2ded1 | bellard | lookup_symbol((void *)tb->pc));
|
300 | 3fb2ded1 | bellard | } |
301 | 9d27abd9 | bellard | #endif
|
302 | 8c6939c0 | bellard | #ifdef __sparc__
|
303 | 3fb2ded1 | bellard | T0 = tmp_T0; |
304 | 8c6939c0 | bellard | #endif
|
305 | 3fb2ded1 | bellard | /* see if we can patch the calling TB. XXX: remove TF test */
|
306 | 1b21b62a | bellard | if (T0 != 0 |
307 | e4533c7a | bellard | #if defined(TARGET_I386)
|
308 | 3fb2ded1 | bellard | && !(env->eflags & TF_MASK) |
309 | e4533c7a | bellard | #endif
|
310 | 3fb2ded1 | bellard | ) { |
311 | 3fb2ded1 | bellard | spin_lock(&tb_lock); |
312 | 3fb2ded1 | bellard | tb_add_jump((TranslationBlock *)(T0 & ~3), T0 & 3, tb); |
313 | 3fb2ded1 | bellard | spin_unlock(&tb_lock); |
314 | 3fb2ded1 | bellard | } |
315 | 3fb2ded1 | bellard | tc_ptr = tb->tc_ptr; |
316 | 83479e77 | bellard | env->current_tb = tb; |
317 | 3fb2ded1 | bellard | /* execute the generated code */
|
318 | 3fb2ded1 | bellard | gen_func = (void *)tc_ptr;
|
319 | 8c6939c0 | bellard | #if defined(__sparc__)
|
320 | 3fb2ded1 | bellard | __asm__ __volatile__("call %0\n\t"
|
321 | 3fb2ded1 | bellard | "mov %%o7,%%i0"
|
322 | 3fb2ded1 | bellard | : /* no outputs */
|
323 | 3fb2ded1 | bellard | : "r" (gen_func)
|
324 | 3fb2ded1 | bellard | : "i0", "i1", "i2", "i3", "i4", "i5"); |
325 | 8c6939c0 | bellard | #elif defined(__arm__)
|
326 | 3fb2ded1 | bellard | asm volatile ("mov pc, %0\n\t" |
327 | 3fb2ded1 | bellard | ".global exec_loop\n\t"
|
328 | 3fb2ded1 | bellard | "exec_loop:\n\t"
|
329 | 3fb2ded1 | bellard | : /* no outputs */
|
330 | 3fb2ded1 | bellard | : "r" (gen_func)
|
331 | 3fb2ded1 | bellard | : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14"); |
332 | ae228531 | bellard | #else
|
333 | 3fb2ded1 | bellard | gen_func(); |
334 | ae228531 | bellard | #endif
|
335 | 83479e77 | bellard | env->current_tb = NULL;
|
336 | 3fb2ded1 | bellard | } |
337 | 3fb2ded1 | bellard | } else {
|
338 | 7d13299d | bellard | } |
339 | 3fb2ded1 | bellard | } /* for(;;) */
|
340 | 3fb2ded1 | bellard | |
341 | 7d13299d | bellard | |
342 | e4533c7a | bellard | #if defined(TARGET_I386)
|
343 | 9de5e440 | bellard | /* restore flags in standard format */
|
344 | fc2b4c48 | bellard | env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK); |
345 | 9de5e440 | bellard | |
346 | 7d13299d | bellard | /* restore global registers */
|
347 | 04369ff2 | bellard | #ifdef reg_EAX
|
348 | 04369ff2 | bellard | EAX = saved_EAX; |
349 | 04369ff2 | bellard | #endif
|
350 | 04369ff2 | bellard | #ifdef reg_ECX
|
351 | 04369ff2 | bellard | ECX = saved_ECX; |
352 | 04369ff2 | bellard | #endif
|
353 | 04369ff2 | bellard | #ifdef reg_EDX
|
354 | 04369ff2 | bellard | EDX = saved_EDX; |
355 | 04369ff2 | bellard | #endif
|
356 | 04369ff2 | bellard | #ifdef reg_EBX
|
357 | 04369ff2 | bellard | EBX = saved_EBX; |
358 | 04369ff2 | bellard | #endif
|
359 | 04369ff2 | bellard | #ifdef reg_ESP
|
360 | 04369ff2 | bellard | ESP = saved_ESP; |
361 | 04369ff2 | bellard | #endif
|
362 | 04369ff2 | bellard | #ifdef reg_EBP
|
363 | 04369ff2 | bellard | EBP = saved_EBP; |
364 | 04369ff2 | bellard | #endif
|
365 | 04369ff2 | bellard | #ifdef reg_ESI
|
366 | 04369ff2 | bellard | ESI = saved_ESI; |
367 | 04369ff2 | bellard | #endif
|
368 | 04369ff2 | bellard | #ifdef reg_EDI
|
369 | 04369ff2 | bellard | EDI = saved_EDI; |
370 | 04369ff2 | bellard | #endif
|
371 | e4533c7a | bellard | #elif defined(TARGET_ARM)
|
372 | 1b21b62a | bellard | env->cpsr = compute_cpsr(); |
373 | e4533c7a | bellard | #else
|
374 | e4533c7a | bellard | #error unsupported target CPU
|
375 | e4533c7a | bellard | #endif
|
376 | 8c6939c0 | bellard | #ifdef __sparc__
|
377 | 8c6939c0 | bellard | asm volatile ("mov %0, %%i7" : : "r" (saved_i7)); |
378 | 8c6939c0 | bellard | #endif
|
379 | 7d13299d | bellard | T0 = saved_T0; |
380 | 7d13299d | bellard | T1 = saved_T1; |
381 | e4533c7a | bellard | T2 = saved_T2; |
382 | 7d13299d | bellard | env = saved_env; |
383 | 7d13299d | bellard | return ret;
|
384 | 7d13299d | bellard | } |
385 | 6dbad63e | bellard | |
386 | e4533c7a | bellard | #if defined(TARGET_I386)
|
387 | e4533c7a | bellard | |
388 | 6dbad63e | bellard | void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector) |
389 | 6dbad63e | bellard | { |
390 | 6dbad63e | bellard | CPUX86State *saved_env; |
391 | 6dbad63e | bellard | |
392 | 6dbad63e | bellard | saved_env = env; |
393 | 6dbad63e | bellard | env = s; |
394 | a412ac57 | bellard | if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) { |
395 | a513fe19 | bellard | SegmentCache *sc; |
396 | a513fe19 | bellard | selector &= 0xffff;
|
397 | 970a87a6 | bellard | sc = &env->segs[seg_reg]; |
398 | a513fe19 | bellard | sc->base = (void *)(selector << 4); |
399 | a513fe19 | bellard | sc->limit = 0xffff;
|
400 | 3fb2ded1 | bellard | sc->flags = 0;
|
401 | 970a87a6 | bellard | sc->selector = selector; |
402 | a513fe19 | bellard | } else {
|
403 | a513fe19 | bellard | load_seg(seg_reg, selector, 0);
|
404 | a513fe19 | bellard | } |
405 | 6dbad63e | bellard | env = saved_env; |
406 | 6dbad63e | bellard | } |
407 | 9de5e440 | bellard | |
408 | d0a1ffc9 | bellard | void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32) |
409 | d0a1ffc9 | bellard | { |
410 | d0a1ffc9 | bellard | CPUX86State *saved_env; |
411 | d0a1ffc9 | bellard | |
412 | d0a1ffc9 | bellard | saved_env = env; |
413 | d0a1ffc9 | bellard | env = s; |
414 | d0a1ffc9 | bellard | |
415 | d0a1ffc9 | bellard | helper_fsave(ptr, data32); |
416 | d0a1ffc9 | bellard | |
417 | d0a1ffc9 | bellard | env = saved_env; |
418 | d0a1ffc9 | bellard | } |
419 | d0a1ffc9 | bellard | |
420 | d0a1ffc9 | bellard | void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32) |
421 | d0a1ffc9 | bellard | { |
422 | d0a1ffc9 | bellard | CPUX86State *saved_env; |
423 | d0a1ffc9 | bellard | |
424 | d0a1ffc9 | bellard | saved_env = env; |
425 | d0a1ffc9 | bellard | env = s; |
426 | d0a1ffc9 | bellard | |
427 | d0a1ffc9 | bellard | helper_frstor(ptr, data32); |
428 | d0a1ffc9 | bellard | |
429 | d0a1ffc9 | bellard | env = saved_env; |
430 | d0a1ffc9 | bellard | } |
431 | d0a1ffc9 | bellard | |
432 | e4533c7a | bellard | #endif /* TARGET_I386 */ |
433 | e4533c7a | bellard | |
434 | 9de5e440 | bellard | #undef EAX
|
435 | 9de5e440 | bellard | #undef ECX
|
436 | 9de5e440 | bellard | #undef EDX
|
437 | 9de5e440 | bellard | #undef EBX
|
438 | 9de5e440 | bellard | #undef ESP
|
439 | 9de5e440 | bellard | #undef EBP
|
440 | 9de5e440 | bellard | #undef ESI
|
441 | 9de5e440 | bellard | #undef EDI
|
442 | 9de5e440 | bellard | #undef EIP
|
443 | 9de5e440 | bellard | #include <signal.h> |
444 | 9de5e440 | bellard | #include <sys/ucontext.h> |
445 | 9de5e440 | bellard | |
446 | 3fb2ded1 | bellard | #if defined(TARGET_I386)
|
447 | 3fb2ded1 | bellard | |
448 | b56dad1c | bellard | /* 'pc' is the host PC at which the exception was raised. 'address' is
|
449 | fd6ce8f6 | bellard | the effective address of the memory exception. 'is_write' is 1 if a
|
450 | fd6ce8f6 | bellard | write caused the exception and otherwise 0'. 'old_set' is the
|
451 | fd6ce8f6 | bellard | signal set which should be restored */
|
452 | 2b413144 | bellard | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
453 | 2b413144 | bellard | int is_write, sigset_t *old_set)
|
454 | 9de5e440 | bellard | { |
455 | a513fe19 | bellard | TranslationBlock *tb; |
456 | a513fe19 | bellard | int ret;
|
457 | 68a79315 | bellard | |
458 | 83479e77 | bellard | if (cpu_single_env)
|
459 | 83479e77 | bellard | env = cpu_single_env; /* XXX: find a correct solution for multithread */
|
460 | fd6ce8f6 | bellard | #if defined(DEBUG_SIGNAL)
|
461 | 3fb2ded1 | bellard | printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
|
462 | fd6ce8f6 | bellard | pc, address, is_write, *(unsigned long *)old_set); |
463 | 9de5e440 | bellard | #endif
|
464 | 25eb4484 | bellard | /* XXX: locking issue */
|
465 | fd6ce8f6 | bellard | if (is_write && page_unprotect(address)) {
|
466 | fd6ce8f6 | bellard | return 1; |
467 | fd6ce8f6 | bellard | } |
468 | 3fb2ded1 | bellard | /* see if it is an MMU fault */
|
469 | 3fb2ded1 | bellard | ret = cpu_x86_handle_mmu_fault(env, address, is_write); |
470 | 3fb2ded1 | bellard | if (ret < 0) |
471 | 3fb2ded1 | bellard | return 0; /* not an MMU fault */ |
472 | 3fb2ded1 | bellard | if (ret == 0) |
473 | 3fb2ded1 | bellard | return 1; /* the MMU fault was handled without causing real CPU fault */ |
474 | 3fb2ded1 | bellard | /* now we have a real cpu fault */
|
475 | a513fe19 | bellard | tb = tb_find_pc(pc); |
476 | a513fe19 | bellard | if (tb) {
|
477 | 9de5e440 | bellard | /* the PC is inside the translated code. It means that we have
|
478 | 9de5e440 | bellard | a virtual CPU fault */
|
479 | 3fb2ded1 | bellard | cpu_restore_state(tb, env, pc); |
480 | 3fb2ded1 | bellard | } |
481 | 3fb2ded1 | bellard | #if 0
|
482 | 3fb2ded1 | bellard | printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
|
483 | 3fb2ded1 | bellard | env->eip, env->cr[2], env->error_code);
|
484 | 3fb2ded1 | bellard | #endif
|
485 | 3fb2ded1 | bellard | /* we restore the process signal mask as the sigreturn should
|
486 | 3fb2ded1 | bellard | do it (XXX: use sigsetjmp) */
|
487 | 3fb2ded1 | bellard | sigprocmask(SIG_SETMASK, old_set, NULL);
|
488 | 3fb2ded1 | bellard | raise_exception_err(EXCP0E_PAGE, env->error_code); |
489 | 3fb2ded1 | bellard | /* never comes here */
|
490 | 3fb2ded1 | bellard | return 1; |
491 | 3fb2ded1 | bellard | } |
492 | 3fb2ded1 | bellard | |
493 | e4533c7a | bellard | #elif defined(TARGET_ARM)
|
494 | 3fb2ded1 | bellard | static inline int handle_cpu_signal(unsigned long pc, unsigned long address, |
495 | 3fb2ded1 | bellard | int is_write, sigset_t *old_set)
|
496 | 3fb2ded1 | bellard | { |
497 | 3fb2ded1 | bellard | /* XXX: do more */
|
498 | 3fb2ded1 | bellard | return 0; |
499 | 3fb2ded1 | bellard | } |
500 | e4533c7a | bellard | #else
|
501 | e4533c7a | bellard | #error unsupported target CPU
|
502 | e4533c7a | bellard | #endif
|
503 | 9de5e440 | bellard | |
504 | 2b413144 | bellard | #if defined(__i386__)
|
505 | 2b413144 | bellard | |
506 | e4533c7a | bellard | int cpu_signal_handler(int host_signum, struct siginfo *info, |
507 | e4533c7a | bellard | void *puc)
|
508 | 9de5e440 | bellard | { |
509 | 9de5e440 | bellard | struct ucontext *uc = puc;
|
510 | 9de5e440 | bellard | unsigned long pc; |
511 | 9de5e440 | bellard | |
512 | d691f669 | bellard | #ifndef REG_EIP
|
513 | d691f669 | bellard | /* for glibc 2.1 */
|
514 | fd6ce8f6 | bellard | #define REG_EIP EIP
|
515 | fd6ce8f6 | bellard | #define REG_ERR ERR
|
516 | fd6ce8f6 | bellard | #define REG_TRAPNO TRAPNO
|
517 | d691f669 | bellard | #endif
|
518 | fc2b4c48 | bellard | pc = uc->uc_mcontext.gregs[REG_EIP]; |
519 | fd6ce8f6 | bellard | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
520 | fd6ce8f6 | bellard | uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
|
521 | fd6ce8f6 | bellard | (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0, |
522 | 2b413144 | bellard | &uc->uc_sigmask); |
523 | 2b413144 | bellard | } |
524 | 2b413144 | bellard | |
525 | 25eb4484 | bellard | #elif defined(__powerpc)
|
526 | 2b413144 | bellard | |
527 | e4533c7a | bellard | int cpu_signal_handler(int host_signum, struct siginfo *info, |
528 | e4533c7a | bellard | void *puc)
|
529 | 2b413144 | bellard | { |
530 | 25eb4484 | bellard | struct ucontext *uc = puc;
|
531 | 25eb4484 | bellard | struct pt_regs *regs = uc->uc_mcontext.regs;
|
532 | 25eb4484 | bellard | unsigned long pc; |
533 | 25eb4484 | bellard | int is_write;
|
534 | 25eb4484 | bellard | |
535 | 25eb4484 | bellard | pc = regs->nip; |
536 | 25eb4484 | bellard | is_write = 0;
|
537 | 25eb4484 | bellard | #if 0
|
538 | 25eb4484 | bellard | /* ppc 4xx case */
|
539 | 25eb4484 | bellard | if (regs->dsisr & 0x00800000)
|
540 | 25eb4484 | bellard | is_write = 1;
|
541 | 25eb4484 | bellard | #else
|
542 | 25eb4484 | bellard | if (regs->trap != 0x400 && (regs->dsisr & 0x02000000)) |
543 | 25eb4484 | bellard | is_write = 1;
|
544 | 25eb4484 | bellard | #endif
|
545 | 25eb4484 | bellard | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
546 | 2b413144 | bellard | is_write, &uc->uc_sigmask); |
547 | 2b413144 | bellard | } |
548 | 2b413144 | bellard | |
549 | 2f87c607 | bellard | #elif defined(__alpha__)
|
550 | 2f87c607 | bellard | |
551 | e4533c7a | bellard | int cpu_signal_handler(int host_signum, struct siginfo *info, |
552 | 2f87c607 | bellard | void *puc)
|
553 | 2f87c607 | bellard | { |
554 | 2f87c607 | bellard | struct ucontext *uc = puc;
|
555 | 2f87c607 | bellard | uint32_t *pc = uc->uc_mcontext.sc_pc; |
556 | 2f87c607 | bellard | uint32_t insn = *pc; |
557 | 2f87c607 | bellard | int is_write = 0; |
558 | 2f87c607 | bellard | |
559 | 8c6939c0 | bellard | /* XXX: need kernel patch to get write flag faster */
|
560 | 2f87c607 | bellard | switch (insn >> 26) { |
561 | 2f87c607 | bellard | case 0x0d: // stw |
562 | 2f87c607 | bellard | case 0x0e: // stb |
563 | 2f87c607 | bellard | case 0x0f: // stq_u |
564 | 2f87c607 | bellard | case 0x24: // stf |
565 | 2f87c607 | bellard | case 0x25: // stg |
566 | 2f87c607 | bellard | case 0x26: // sts |
567 | 2f87c607 | bellard | case 0x27: // stt |
568 | 2f87c607 | bellard | case 0x2c: // stl |
569 | 2f87c607 | bellard | case 0x2d: // stq |
570 | 2f87c607 | bellard | case 0x2e: // stl_c |
571 | 2f87c607 | bellard | case 0x2f: // stq_c |
572 | 2f87c607 | bellard | is_write = 1;
|
573 | 2f87c607 | bellard | } |
574 | 2f87c607 | bellard | |
575 | 2f87c607 | bellard | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
576 | 2f87c607 | bellard | is_write, &uc->uc_sigmask); |
577 | 2f87c607 | bellard | } |
578 | 8c6939c0 | bellard | #elif defined(__sparc__)
|
579 | 8c6939c0 | bellard | |
580 | e4533c7a | bellard | int cpu_signal_handler(int host_signum, struct siginfo *info, |
581 | e4533c7a | bellard | void *puc)
|
582 | 8c6939c0 | bellard | { |
583 | 8c6939c0 | bellard | uint32_t *regs = (uint32_t *)(info + 1);
|
584 | 8c6939c0 | bellard | void *sigmask = (regs + 20); |
585 | 8c6939c0 | bellard | unsigned long pc; |
586 | 8c6939c0 | bellard | int is_write;
|
587 | 8c6939c0 | bellard | uint32_t insn; |
588 | 8c6939c0 | bellard | |
589 | 8c6939c0 | bellard | /* XXX: is there a standard glibc define ? */
|
590 | 8c6939c0 | bellard | pc = regs[1];
|
591 | 8c6939c0 | bellard | /* XXX: need kernel patch to get write flag faster */
|
592 | 8c6939c0 | bellard | is_write = 0;
|
593 | 8c6939c0 | bellard | insn = *(uint32_t *)pc; |
594 | 8c6939c0 | bellard | if ((insn >> 30) == 3) { |
595 | 8c6939c0 | bellard | switch((insn >> 19) & 0x3f) { |
596 | 8c6939c0 | bellard | case 0x05: // stb |
597 | 8c6939c0 | bellard | case 0x06: // sth |
598 | 8c6939c0 | bellard | case 0x04: // st |
599 | 8c6939c0 | bellard | case 0x07: // std |
600 | 8c6939c0 | bellard | case 0x24: // stf |
601 | 8c6939c0 | bellard | case 0x27: // stdf |
602 | 8c6939c0 | bellard | case 0x25: // stfsr |
603 | 8c6939c0 | bellard | is_write = 1;
|
604 | 8c6939c0 | bellard | break;
|
605 | 8c6939c0 | bellard | } |
606 | 8c6939c0 | bellard | } |
607 | 8c6939c0 | bellard | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
608 | 8c6939c0 | bellard | is_write, sigmask); |
609 | 8c6939c0 | bellard | } |
610 | 8c6939c0 | bellard | |
611 | 8c6939c0 | bellard | #elif defined(__arm__)
|
612 | 8c6939c0 | bellard | |
613 | e4533c7a | bellard | int cpu_signal_handler(int host_signum, struct siginfo *info, |
614 | e4533c7a | bellard | void *puc)
|
615 | 8c6939c0 | bellard | { |
616 | 8c6939c0 | bellard | struct ucontext *uc = puc;
|
617 | 8c6939c0 | bellard | unsigned long pc; |
618 | 8c6939c0 | bellard | int is_write;
|
619 | 8c6939c0 | bellard | |
620 | 8c6939c0 | bellard | pc = uc->uc_mcontext.gregs[R15]; |
621 | 8c6939c0 | bellard | /* XXX: compute is_write */
|
622 | 8c6939c0 | bellard | is_write = 0;
|
623 | 8c6939c0 | bellard | return handle_cpu_signal(pc, (unsigned long)info->si_addr, |
624 | 8c6939c0 | bellard | is_write, |
625 | 8c6939c0 | bellard | &uc->uc_sigmask); |
626 | 8c6939c0 | bellard | } |
627 | 8c6939c0 | bellard | |
628 | 9de5e440 | bellard | #else
|
629 | 2b413144 | bellard | |
630 | 3fb2ded1 | bellard | #error host CPU specific signal handler needed
|
631 | 2b413144 | bellard | |
632 | 9de5e440 | bellard | #endif |