blob: d414e3343b60d62bf5ce9f27bfab818c7ba6dfd4 [file] [log] [blame]
bellard7d132992003-03-06 23:23:54 +00001/*
2 * i386 emulator main execution loop
3 *
bellard66321a12005-04-06 20:47:48 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
bellard7d132992003-03-06 23:23:54 +00005 *
bellard3ef693a2003-03-23 20:17:16 +00006 * 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.
bellard7d132992003-03-06 23:23:54 +000010 *
bellard3ef693a2003-03-23 20:17:16 +000011 * 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.
bellard7d132992003-03-06 23:23:54 +000015 *
bellard3ef693a2003-03-23 20:17:16 +000016 * 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
bellard7d132992003-03-06 23:23:54 +000019 */
bellarde4533c72003-06-15 19:51:39 +000020#include "config.h"
bellard93ac68b2003-09-30 20:57:29 +000021#include "exec.h"
bellard956034d2003-04-29 20:40:53 +000022#include "disas.h"
bellard7d132992003-03-06 23:23:54 +000023
bellardfbf9eeb2004-04-25 21:21:33 +000024#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
bellard36bdbe52003-11-19 22:12:02 +000038int tb_invalidated_flag;
39
bellarddc990652003-03-19 00:00:28 +000040//#define DEBUG_EXEC
bellard9de5e442003-03-23 16:49:39 +000041//#define DEBUG_SIGNAL
bellard7d132992003-03-06 23:23:54 +000042
bellard93ac68b2003-09-30 20:57:29 +000043#if defined(TARGET_ARM) || defined(TARGET_SPARC)
bellarde4533c72003-06-15 19:51:39 +000044/* XXX: unify with i386 target */
45void cpu_loop_exit(void)
46{
47 longjmp(env->jmp_env, 1);
48}
49#endif
50
bellardfbf9eeb2004-04-25 21:21:33 +000051/* exit the current TB from a signal handler. The host registers are
52 restored in a state compatible with the CPU emulator
53 */
54void 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
bellard7d132992003-03-06 23:23:54 +000073/* main execution loop */
74
bellarde4533c72003-06-15 19:51:39 +000075int cpu_exec(CPUState *env1)
bellard7d132992003-03-06 23:23:54 +000076{
bellarde4533c72003-06-15 19:51:39 +000077 int saved_T0, saved_T1, saved_T2;
78 CPUState *saved_env;
bellard04369ff2003-03-20 22:33:23 +000079#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
bellard8c6939c2003-06-09 15:28:00 +0000103#ifdef __sparc__
104 int saved_i7, tmp_T0;
105#endif
bellard68a79312003-06-30 13:12:32 +0000106 int code_gen_size, ret, interrupt_request;
bellard7d132992003-03-06 23:23:54 +0000107 void (*gen_func)(void);
bellard9de5e442003-03-23 16:49:39 +0000108 TranslationBlock *tb, **ptb;
bellardc27004e2005-01-03 23:35:10 +0000109 target_ulong cs_base, pc;
110 uint8_t *tc_ptr;
bellard6dbad632003-03-16 18:05:05 +0000111 unsigned int flags;
bellard8c6939c2003-06-09 15:28:00 +0000112
bellard7d132992003-03-06 23:23:54 +0000113 /* first we save global registers */
bellardc27004e2005-01-03 23:35:10 +0000114 saved_env = env;
115 env = env1;
bellard7d132992003-03-06 23:23:54 +0000116 saved_T0 = T0;
117 saved_T1 = T1;
bellarde4533c72003-06-15 19:51:39 +0000118 saved_T2 = T2;
bellarde4533c72003-06-15 19:51:39 +0000119#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)
bellard04369ff2003-03-20 22:33:23 +0000125#ifdef reg_EAX
126 saved_EAX = EAX;
bellard04369ff2003-03-20 22:33:23 +0000127#endif
128#ifdef reg_ECX
129 saved_ECX = ECX;
bellard04369ff2003-03-20 22:33:23 +0000130#endif
131#ifdef reg_EDX
132 saved_EDX = EDX;
bellard04369ff2003-03-20 22:33:23 +0000133#endif
134#ifdef reg_EBX
135 saved_EBX = EBX;
bellard04369ff2003-03-20 22:33:23 +0000136#endif
137#ifdef reg_ESP
138 saved_ESP = ESP;
bellard04369ff2003-03-20 22:33:23 +0000139#endif
140#ifdef reg_EBP
141 saved_EBP = EBP;
bellard04369ff2003-03-20 22:33:23 +0000142#endif
143#ifdef reg_ESI
144 saved_ESI = ESI;
bellard04369ff2003-03-20 22:33:23 +0000145#endif
146#ifdef reg_EDI
147 saved_EDI = EDI;
bellard04369ff2003-03-20 22:33:23 +0000148#endif
bellard0d1a29f2004-10-12 22:01:28 +0000149
150 env_to_regs();
bellard9de5e442003-03-23 16:49:39 +0000151 /* put eflags in CPU temporary format */
bellardfc2b4c42003-03-29 16:52:44 +0000152 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
153 DF = 1 - (2 * ((env->eflags >> 10) & 1));
bellard9de5e442003-03-23 16:49:39 +0000154 CC_OP = CC_OP_EFLAGS;
bellardfc2b4c42003-03-29 16:52:44 +0000155 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000156#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;
bellard99c475a2005-01-31 20:45:13 +0000163 env->QF = (psr >> 27) & 1;
164 env->cpsr = psr & ~CACHED_CPSR_BITS;
bellarde4533c72003-06-15 19:51:39 +0000165 }
bellard93ac68b2003-09-30 20:57:29 +0000166#elif defined(TARGET_SPARC)
bellard67867302003-11-23 17:05:30 +0000167#elif defined(TARGET_PPC)
bellarde4533c72003-06-15 19:51:39 +0000168#else
169#error unsupported target CPU
170#endif
bellard3fb2ded2003-06-24 13:22:59 +0000171 env->exception_index = -1;
bellard9d27abd2003-05-10 13:13:54 +0000172
bellard7d132992003-03-06 23:23:54 +0000173 /* prepare setjmp context for exception handling */
bellard3fb2ded2003-06-24 13:22:59 +0000174 for(;;) {
175 if (setjmp(env->jmp_env) == 0) {
bellardee8b7022004-02-03 23:35:10 +0000176 env->current_tb = NULL;
bellard3fb2ded2003-06-24 13:22:59 +0000177 /* 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 */
bellard83479e72003-06-25 16:12:37 +0000187#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000188 do_interrupt_user(env->exception_index,
189 env->exception_is_int,
190 env->error_code,
191 env->exception_next_eip);
bellard83479e72003-06-25 16:12:37 +0000192#endif
bellard3fb2ded2003-06-24 13:22:59 +0000193 ret = env->exception_index;
194 break;
195 } else {
bellard83479e72003-06-25 16:12:37 +0000196#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000197 /* 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,
bellardd05e66d2003-08-20 21:34:35 +0000203 env->exception_next_eip, 0);
bellardce097762004-01-04 23:53:18 +0000204#elif defined(TARGET_PPC)
205 do_interrupt(env);
bellarde95c8d52004-09-30 22:22:08 +0000206#elif defined(TARGET_SPARC)
bellard1a0c3292005-02-13 19:02:07 +0000207 do_interrupt(env->exception_index);
bellard83479e72003-06-25 16:12:37 +0000208#endif
bellard3fb2ded2003-06-24 13:22:59 +0000209 }
210 env->exception_index = -1;
bellard9df217a2005-02-10 22:05:51 +0000211 }
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 }
bellard9de5e442003-03-23 16:49:39 +0000235 }
bellard9df217a2005-02-10 22:05:51 +0000236#endif
237
bellard3fb2ded2003-06-24 13:22:59 +0000238 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
bellard68a79312003-06-30 13:12:32 +0000244 interrupt_request = env->interrupt_request;
bellard2e255c62003-08-21 23:25:21 +0000245 if (__builtin_expect(interrupt_request, 0)) {
bellard68a79312003-06-30 13:12:32 +0000246#if defined(TARGET_I386)
247 /* if hardware interrupt pending, we execute it */
248 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
bellard3f337312003-08-20 23:02:09 +0000249 (env->eflags & IF_MASK) &&
250 !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
bellard68a79312003-06-30 13:12:32 +0000251 int intno;
bellardfbf9eeb2004-04-25 21:21:33 +0000252 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellarda541f292004-04-12 20:39:29 +0000253 intno = cpu_get_pic_interrupt(env);
bellardf193c792004-03-21 17:06:25 +0000254 if (loglevel & CPU_LOG_TB_IN_ASM) {
bellard68a79312003-06-30 13:12:32 +0000255 fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
256 }
bellardd05e66d2003-08-20 21:34:35 +0000257 do_interrupt(intno, 0, 0, 0, 1);
bellard907a5b22003-06-30 23:18:22 +0000258 /* 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
bellard68a79312003-06-30 13:12:32 +0000265 }
bellardce097762004-01-04 23:53:18 +0000266#elif defined(TARGET_PPC)
bellard9fddaa02004-05-21 12:59:32 +0000267#if 0
268 if ((interrupt_request & CPU_INTERRUPT_RESET)) {
269 cpu_ppc_reset(env);
270 }
271#endif
272 if (msr_ee != 0) {
bellardce097762004-01-04 23:53:18 +0000273 if ((interrupt_request & CPU_INTERRUPT_HARD)) {
bellard9fddaa02004-05-21 12:59:32 +0000274 /* Raise it */
275 env->exception_index = EXCP_EXTERNAL;
276 env->error_code = 0;
bellardce097762004-01-04 23:53:18 +0000277 do_interrupt(env);
278 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellard9fddaa02004-05-21 12:59:32 +0000279 } 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 }
bellardce097762004-01-04 23:53:18 +0000286 }
bellarde95c8d52004-09-30 22:22:08 +0000287#elif defined(TARGET_SPARC)
bellard66321a12005-04-06 20:47:48 +0000288 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
289 (env->psret != 0)) {
290 int pil = env->interrupt_index & 15;
291 int type = env->interrupt_index & 0xf0;
292
293 if (((type == TT_EXTINT) &&
294 (pil == 15 || pil > env->psrpil)) ||
295 type != TT_EXTINT) {
296 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
297 do_interrupt(env->interrupt_index);
298 env->interrupt_index = 0;
299 }
bellarde95c8d52004-09-30 22:22:08 +0000300 } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
301 //do_interrupt(0, 0, 0, 0, 0);
302 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
303 }
bellard68a79312003-06-30 13:12:32 +0000304#endif
bellardbf3e8bf2004-02-16 21:58:54 +0000305 if (interrupt_request & CPU_INTERRUPT_EXITTB) {
306 env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
307 /* ensure that no TB jump will be modified as
308 the program flow was changed */
309#ifdef __sparc__
310 tmp_T0 = 0;
311#else
312 T0 = 0;
313#endif
314 }
bellard68a79312003-06-30 13:12:32 +0000315 if (interrupt_request & CPU_INTERRUPT_EXIT) {
316 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
317 env->exception_index = EXCP_INTERRUPT;
318 cpu_loop_exit();
319 }
bellard3fb2ded2003-06-24 13:22:59 +0000320 }
321#ifdef DEBUG_EXEC
bellardc27004e2005-01-03 23:35:10 +0000322 if ((loglevel & CPU_LOG_EXEC)) {
bellard3fb2ded2003-06-24 13:22:59 +0000323#if defined(TARGET_I386)
324 /* restore flags in standard format */
bellardfc9f7152005-04-26 19:33:35 +0000325#ifdef reg_EAX
bellard3fb2ded2003-06-24 13:22:59 +0000326 env->regs[R_EAX] = EAX;
bellardfc9f7152005-04-26 19:33:35 +0000327#endif
328#ifdef reg_EBX
bellard3fb2ded2003-06-24 13:22:59 +0000329 env->regs[R_EBX] = EBX;
bellardfc9f7152005-04-26 19:33:35 +0000330#endif
331#ifdef reg_ECX
bellard3fb2ded2003-06-24 13:22:59 +0000332 env->regs[R_ECX] = ECX;
bellardfc9f7152005-04-26 19:33:35 +0000333#endif
334#ifdef reg_EDX
bellard3fb2ded2003-06-24 13:22:59 +0000335 env->regs[R_EDX] = EDX;
bellardfc9f7152005-04-26 19:33:35 +0000336#endif
337#ifdef reg_ESI
bellard3fb2ded2003-06-24 13:22:59 +0000338 env->regs[R_ESI] = ESI;
bellardfc9f7152005-04-26 19:33:35 +0000339#endif
340#ifdef reg_EDI
bellard3fb2ded2003-06-24 13:22:59 +0000341 env->regs[R_EDI] = EDI;
bellardfc9f7152005-04-26 19:33:35 +0000342#endif
343#ifdef reg_EBP
bellard3fb2ded2003-06-24 13:22:59 +0000344 env->regs[R_EBP] = EBP;
bellardfc9f7152005-04-26 19:33:35 +0000345#endif
346#ifdef reg_ESP
bellard3fb2ded2003-06-24 13:22:59 +0000347 env->regs[R_ESP] = ESP;
bellardfc9f7152005-04-26 19:33:35 +0000348#endif
bellard3fb2ded2003-06-24 13:22:59 +0000349 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard7fe48482004-10-09 18:08:01 +0000350 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
bellard3fb2ded2003-06-24 13:22:59 +0000351 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000352#elif defined(TARGET_ARM)
bellard1b21b622003-07-09 17:16:27 +0000353 env->cpsr = compute_cpsr();
bellard7fe48482004-10-09 18:08:01 +0000354 cpu_dump_state(env, logfile, fprintf, 0);
bellard99c475a2005-01-31 20:45:13 +0000355 env->cpsr &= ~CACHED_CPSR_BITS;
bellard93ac68b2003-09-30 20:57:29 +0000356#elif defined(TARGET_SPARC)
bellard7fe48482004-10-09 18:08:01 +0000357 cpu_dump_state (env, logfile, fprintf, 0);
bellard67867302003-11-23 17:05:30 +0000358#elif defined(TARGET_PPC)
bellard7fe48482004-10-09 18:08:01 +0000359 cpu_dump_state(env, logfile, fprintf, 0);
bellarde4533c72003-06-15 19:51:39 +0000360#else
361#error unsupported target CPU
362#endif
bellard3fb2ded2003-06-24 13:22:59 +0000363 }
bellard7d132992003-03-06 23:23:54 +0000364#endif
bellard3f337312003-08-20 23:02:09 +0000365 /* we record a subset of the CPU state. It will
366 always be the same before a given translated block
367 is executed. */
bellarde4533c72003-06-15 19:51:39 +0000368#if defined(TARGET_I386)
bellard2e255c62003-08-21 23:25:21 +0000369 flags = env->hflags;
bellard3f337312003-08-20 23:02:09 +0000370 flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
bellard3fb2ded2003-06-24 13:22:59 +0000371 cs_base = env->segs[R_CS].base;
372 pc = cs_base + env->eip;
bellarde4533c72003-06-15 19:51:39 +0000373#elif defined(TARGET_ARM)
bellardb7bcbe92005-02-22 19:27:29 +0000374 flags = env->thumb | (env->vfp.vec_len << 1)
375 | (env->vfp.vec_stride << 4);
bellard3fb2ded2003-06-24 13:22:59 +0000376 cs_base = 0;
bellardc27004e2005-01-03 23:35:10 +0000377 pc = env->regs[15];
bellard93ac68b2003-09-30 20:57:29 +0000378#elif defined(TARGET_SPARC)
bellard67867302003-11-23 17:05:30 +0000379 flags = 0;
bellardc27004e2005-01-03 23:35:10 +0000380 cs_base = env->npc;
381 pc = env->pc;
bellard67867302003-11-23 17:05:30 +0000382#elif defined(TARGET_PPC)
bellard111bfab2005-04-23 18:16:07 +0000383 flags = (msr_pr << MSR_PR) | (msr_fp << MSR_FP) |
384 (msr_se << MSR_SE) | (msr_le << MSR_LE);
bellard67867302003-11-23 17:05:30 +0000385 cs_base = 0;
bellardc27004e2005-01-03 23:35:10 +0000386 pc = env->nip;
bellarde4533c72003-06-15 19:51:39 +0000387#else
388#error unsupported CPU
389#endif
bellardc27004e2005-01-03 23:35:10 +0000390 tb = tb_find(&ptb, pc, cs_base,
bellard3fb2ded2003-06-24 13:22:59 +0000391 flags);
bellardd4e81642003-05-25 16:46:15 +0000392 if (!tb) {
bellard13768472004-01-04 17:43:01 +0000393 TranslationBlock **ptb1;
394 unsigned int h;
395 target_ulong phys_pc, phys_page1, phys_page2, virt_page2;
396
397
bellard3fb2ded2003-06-24 13:22:59 +0000398 spin_lock(&tb_lock);
bellard13768472004-01-04 17:43:01 +0000399
400 tb_invalidated_flag = 0;
bellard0d1a29f2004-10-12 22:01:28 +0000401
402 regs_to_env(); /* XXX: do it just before cpu_gen_code() */
bellard13768472004-01-04 17:43:01 +0000403
404 /* find translated block using physical mappings */
bellardc27004e2005-01-03 23:35:10 +0000405 phys_pc = get_phys_addr_code(env, pc);
bellard13768472004-01-04 17:43:01 +0000406 phys_page1 = phys_pc & TARGET_PAGE_MASK;
407 phys_page2 = -1;
408 h = tb_phys_hash_func(phys_pc);
409 ptb1 = &tb_phys_hash[h];
410 for(;;) {
411 tb = *ptb1;
412 if (!tb)
413 goto not_found;
bellardc27004e2005-01-03 23:35:10 +0000414 if (tb->pc == pc &&
bellard13768472004-01-04 17:43:01 +0000415 tb->page_addr[0] == phys_page1 &&
bellardc27004e2005-01-03 23:35:10 +0000416 tb->cs_base == cs_base &&
bellard13768472004-01-04 17:43:01 +0000417 tb->flags == flags) {
418 /* check next page if needed */
bellardb516f852004-01-18 21:50:04 +0000419 if (tb->page_addr[1] != -1) {
bellardc27004e2005-01-03 23:35:10 +0000420 virt_page2 = (pc & TARGET_PAGE_MASK) +
bellardb516f852004-01-18 21:50:04 +0000421 TARGET_PAGE_SIZE;
bellard13768472004-01-04 17:43:01 +0000422 phys_page2 = get_phys_addr_code(env, virt_page2);
423 if (tb->page_addr[1] == phys_page2)
424 goto found;
425 } else {
426 goto found;
427 }
428 }
429 ptb1 = &tb->phys_hash_next;
430 }
431 not_found:
bellard3fb2ded2003-06-24 13:22:59 +0000432 /* if no translated code available, then translate it now */
bellardc27004e2005-01-03 23:35:10 +0000433 tb = tb_alloc(pc);
bellard3fb2ded2003-06-24 13:22:59 +0000434 if (!tb) {
435 /* flush must be done */
bellardb453b702004-01-04 15:45:21 +0000436 tb_flush(env);
bellard3fb2ded2003-06-24 13:22:59 +0000437 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000438 tb = tb_alloc(pc);
bellard3fb2ded2003-06-24 13:22:59 +0000439 /* don't forget to invalidate previous TB info */
bellardc27004e2005-01-03 23:35:10 +0000440 ptb = &tb_hash[tb_hash_func(pc)];
bellard3fb2ded2003-06-24 13:22:59 +0000441 T0 = 0;
442 }
443 tc_ptr = code_gen_ptr;
444 tb->tc_ptr = tc_ptr;
bellardc27004e2005-01-03 23:35:10 +0000445 tb->cs_base = cs_base;
bellard3fb2ded2003-06-24 13:22:59 +0000446 tb->flags = flags;
bellardfacc68b2003-09-17 22:51:18 +0000447 cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
bellard13768472004-01-04 17:43:01 +0000448 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
449
450 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000451 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellard13768472004-01-04 17:43:01 +0000452 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000453 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellard13768472004-01-04 17:43:01 +0000454 phys_page2 = get_phys_addr_code(env, virt_page2);
455 }
456 tb_link_phys(tb, phys_pc, phys_page2);
457
458 found:
bellard36bdbe52003-11-19 22:12:02 +0000459 if (tb_invalidated_flag) {
460 /* as some TB could have been invalidated because
461 of memory exceptions while generating the code, we
462 must recompute the hash index here */
bellardc27004e2005-01-03 23:35:10 +0000463 ptb = &tb_hash[tb_hash_func(pc)];
bellard36bdbe52003-11-19 22:12:02 +0000464 while (*ptb != NULL)
465 ptb = &(*ptb)->hash_next;
466 T0 = 0;
467 }
bellard13768472004-01-04 17:43:01 +0000468 /* we add the TB in the virtual pc hash table */
bellard3fb2ded2003-06-24 13:22:59 +0000469 *ptb = tb;
470 tb->hash_next = NULL;
471 tb_link(tb);
bellard3fb2ded2003-06-24 13:22:59 +0000472 spin_unlock(&tb_lock);
473 }
bellard9d27abd2003-05-10 13:13:54 +0000474#ifdef DEBUG_EXEC
bellardc1135f62005-01-30 22:41:54 +0000475 if ((loglevel & CPU_LOG_EXEC)) {
bellardc27004e2005-01-03 23:35:10 +0000476 fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
477 (long)tb->tc_ptr, tb->pc,
478 lookup_symbol(tb->pc));
bellard3fb2ded2003-06-24 13:22:59 +0000479 }
bellard9d27abd2003-05-10 13:13:54 +0000480#endif
bellard8c6939c2003-06-09 15:28:00 +0000481#ifdef __sparc__
bellard3fb2ded2003-06-24 13:22:59 +0000482 T0 = tmp_T0;
bellard8c6939c2003-06-09 15:28:00 +0000483#endif
bellardfacc68b2003-09-17 22:51:18 +0000484 /* see if we can patch the calling TB. */
bellardc27004e2005-01-03 23:35:10 +0000485 {
486 if (T0 != 0
bellardbf3e8bf2004-02-16 21:58:54 +0000487#if defined(TARGET_I386) && defined(USE_CODE_COPY)
488 && (tb->cflags & CF_CODE_COPY) ==
489 (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
490#endif
491 ) {
bellard3fb2ded2003-06-24 13:22:59 +0000492 spin_lock(&tb_lock);
bellardc27004e2005-01-03 23:35:10 +0000493 tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
bellard97eb5b12004-02-25 23:19:55 +0000494#if defined(USE_CODE_COPY)
495 /* propagates the FP use info */
496 ((TranslationBlock *)(T0 & ~3))->cflags |=
497 (tb->cflags & CF_FP_USED);
498#endif
bellard3fb2ded2003-06-24 13:22:59 +0000499 spin_unlock(&tb_lock);
500 }
bellardc27004e2005-01-03 23:35:10 +0000501 }
bellard3fb2ded2003-06-24 13:22:59 +0000502 tc_ptr = tb->tc_ptr;
bellard83479e72003-06-25 16:12:37 +0000503 env->current_tb = tb;
bellard3fb2ded2003-06-24 13:22:59 +0000504 /* execute the generated code */
505 gen_func = (void *)tc_ptr;
506#if defined(__sparc__)
507 __asm__ __volatile__("call %0\n\t"
508 "mov %%o7,%%i0"
509 : /* no outputs */
510 : "r" (gen_func)
511 : "i0", "i1", "i2", "i3", "i4", "i5");
512#elif defined(__arm__)
513 asm volatile ("mov pc, %0\n\t"
514 ".global exec_loop\n\t"
515 "exec_loop:\n\t"
516 : /* no outputs */
517 : "r" (gen_func)
518 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
bellardbf3e8bf2004-02-16 21:58:54 +0000519#elif defined(TARGET_I386) && defined(USE_CODE_COPY)
520{
521 if (!(tb->cflags & CF_CODE_COPY)) {
bellard97eb5b12004-02-25 23:19:55 +0000522 if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
523 save_native_fp_state(env);
524 }
bellardbf3e8bf2004-02-16 21:58:54 +0000525 gen_func();
526 } else {
bellard97eb5b12004-02-25 23:19:55 +0000527 if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
528 restore_native_fp_state(env);
529 }
bellardbf3e8bf2004-02-16 21:58:54 +0000530 /* we work with native eflags */
531 CC_SRC = cc_table[CC_OP].compute_all();
532 CC_OP = CC_OP_EFLAGS;
533 asm(".globl exec_loop\n"
534 "\n"
535 "debug1:\n"
536 " pushl %%ebp\n"
537 " fs movl %10, %9\n"
538 " fs movl %11, %%eax\n"
539 " andl $0x400, %%eax\n"
540 " fs orl %8, %%eax\n"
541 " pushl %%eax\n"
542 " popf\n"
543 " fs movl %%esp, %12\n"
544 " fs movl %0, %%eax\n"
545 " fs movl %1, %%ecx\n"
546 " fs movl %2, %%edx\n"
547 " fs movl %3, %%ebx\n"
548 " fs movl %4, %%esp\n"
549 " fs movl %5, %%ebp\n"
550 " fs movl %6, %%esi\n"
551 " fs movl %7, %%edi\n"
552 " fs jmp *%9\n"
553 "exec_loop:\n"
554 " fs movl %%esp, %4\n"
555 " fs movl %12, %%esp\n"
556 " fs movl %%eax, %0\n"
557 " fs movl %%ecx, %1\n"
558 " fs movl %%edx, %2\n"
559 " fs movl %%ebx, %3\n"
560 " fs movl %%ebp, %5\n"
561 " fs movl %%esi, %6\n"
562 " fs movl %%edi, %7\n"
563 " pushf\n"
564 " popl %%eax\n"
565 " movl %%eax, %%ecx\n"
566 " andl $0x400, %%ecx\n"
567 " shrl $9, %%ecx\n"
568 " andl $0x8d5, %%eax\n"
569 " fs movl %%eax, %8\n"
570 " movl $1, %%eax\n"
571 " subl %%ecx, %%eax\n"
572 " fs movl %%eax, %11\n"
573 " fs movl %9, %%ebx\n" /* get T0 value */
574 " popl %%ebp\n"
575 :
576 : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
577 "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
578 "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
579 "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
580 "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
581 "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
582 "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
583 "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
584 "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
585 "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
586 "a" (gen_func),
587 "m" (*(uint8_t *)offsetof(CPUState, df)),
588 "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
589 : "%ecx", "%edx"
590 );
591 }
592}
bellardb8076a72005-04-07 22:20:31 +0000593#elif defined(__ia64)
594 struct fptr {
595 void *ip;
596 void *gp;
597 } fp;
598
599 fp.ip = tc_ptr;
600 fp.gp = code_gen_buffer + 2 * (1 << 20);
601 (*(void (*)(void)) &fp)();
bellard3fb2ded2003-06-24 13:22:59 +0000602#else
603 gen_func();
604#endif
bellard83479e72003-06-25 16:12:37 +0000605 env->current_tb = NULL;
bellard4cbf74b2003-08-10 21:48:43 +0000606 /* reset soft MMU for next block (it can currently
607 only be set by a memory fault) */
608#if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
bellard3f337312003-08-20 23:02:09 +0000609 if (env->hflags & HF_SOFTMMU_MASK) {
610 env->hflags &= ~HF_SOFTMMU_MASK;
bellard4cbf74b2003-08-10 21:48:43 +0000611 /* do not allow linking to another block */
612 T0 = 0;
613 }
614#endif
bellard3fb2ded2003-06-24 13:22:59 +0000615 }
616 } else {
bellard0d1a29f2004-10-12 22:01:28 +0000617 env_to_regs();
bellard7d132992003-03-06 23:23:54 +0000618 }
bellard3fb2ded2003-06-24 13:22:59 +0000619 } /* for(;;) */
620
bellard7d132992003-03-06 23:23:54 +0000621
bellarde4533c72003-06-15 19:51:39 +0000622#if defined(TARGET_I386)
bellard97eb5b12004-02-25 23:19:55 +0000623#if defined(USE_CODE_COPY)
624 if (env->native_fp_regs) {
625 save_native_fp_state(env);
626 }
627#endif
bellard9de5e442003-03-23 16:49:39 +0000628 /* restore flags in standard format */
bellardfc2b4c42003-03-29 16:52:44 +0000629 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard9de5e442003-03-23 16:49:39 +0000630
bellard7d132992003-03-06 23:23:54 +0000631 /* restore global registers */
bellard04369ff2003-03-20 22:33:23 +0000632#ifdef reg_EAX
633 EAX = saved_EAX;
634#endif
635#ifdef reg_ECX
636 ECX = saved_ECX;
637#endif
638#ifdef reg_EDX
639 EDX = saved_EDX;
640#endif
641#ifdef reg_EBX
642 EBX = saved_EBX;
643#endif
644#ifdef reg_ESP
645 ESP = saved_ESP;
646#endif
647#ifdef reg_EBP
648 EBP = saved_EBP;
649#endif
650#ifdef reg_ESI
651 ESI = saved_ESI;
652#endif
653#ifdef reg_EDI
654 EDI = saved_EDI;
655#endif
bellarde4533c72003-06-15 19:51:39 +0000656#elif defined(TARGET_ARM)
bellard1b21b622003-07-09 17:16:27 +0000657 env->cpsr = compute_cpsr();
bellardb7bcbe92005-02-22 19:27:29 +0000658 /* XXX: Save/restore host fpu exception state?. */
bellard93ac68b2003-09-30 20:57:29 +0000659#elif defined(TARGET_SPARC)
bellard67867302003-11-23 17:05:30 +0000660#elif defined(TARGET_PPC)
bellarde4533c72003-06-15 19:51:39 +0000661#else
662#error unsupported target CPU
663#endif
bellard8c6939c2003-06-09 15:28:00 +0000664#ifdef __sparc__
665 asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
666#endif
bellard7d132992003-03-06 23:23:54 +0000667 T0 = saved_T0;
668 T1 = saved_T1;
bellarde4533c72003-06-15 19:51:39 +0000669 T2 = saved_T2;
bellard7d132992003-03-06 23:23:54 +0000670 env = saved_env;
671 return ret;
672}
bellard6dbad632003-03-16 18:05:05 +0000673
bellardfbf9eeb2004-04-25 21:21:33 +0000674/* must only be called from the generated code as an exception can be
675 generated */
676void tb_invalidate_page_range(target_ulong start, target_ulong end)
677{
bellarddc5d0b32004-06-22 18:43:30 +0000678 /* XXX: cannot enable it yet because it yields to MMU exception
679 where NIP != read address on PowerPC */
680#if 0
bellardfbf9eeb2004-04-25 21:21:33 +0000681 target_ulong phys_addr;
682 phys_addr = get_phys_addr_code(env, start);
683 tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
bellarddc5d0b32004-06-22 18:43:30 +0000684#endif
bellardfbf9eeb2004-04-25 21:21:33 +0000685}
686
bellard1a18c712003-10-30 01:07:51 +0000687#if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
bellarde4533c72003-06-15 19:51:39 +0000688
bellard6dbad632003-03-16 18:05:05 +0000689void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
690{
691 CPUX86State *saved_env;
692
693 saved_env = env;
694 env = s;
bellarda412ac52003-07-26 18:01:40 +0000695 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
bellarda513fe12003-05-27 23:29:48 +0000696 selector &= 0xffff;
bellard2e255c62003-08-21 23:25:21 +0000697 cpu_x86_load_seg_cache(env, seg_reg, selector,
bellardc27004e2005-01-03 23:35:10 +0000698 (selector << 4), 0xffff, 0);
bellarda513fe12003-05-27 23:29:48 +0000699 } else {
bellardb453b702004-01-04 15:45:21 +0000700 load_seg(seg_reg, selector);
bellarda513fe12003-05-27 23:29:48 +0000701 }
bellard6dbad632003-03-16 18:05:05 +0000702 env = saved_env;
703}
bellard9de5e442003-03-23 16:49:39 +0000704
bellardd0a1ffc2003-05-29 20:04:28 +0000705void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
706{
707 CPUX86State *saved_env;
708
709 saved_env = env;
710 env = s;
711
bellardc27004e2005-01-03 23:35:10 +0000712 helper_fsave((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000713
714 env = saved_env;
715}
716
717void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
718{
719 CPUX86State *saved_env;
720
721 saved_env = env;
722 env = s;
723
bellardc27004e2005-01-03 23:35:10 +0000724 helper_frstor((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000725
726 env = saved_env;
727}
728
bellarde4533c72003-06-15 19:51:39 +0000729#endif /* TARGET_I386 */
730
bellard67b915a2004-03-31 23:37:16 +0000731#if !defined(CONFIG_SOFTMMU)
732
bellard3fb2ded2003-06-24 13:22:59 +0000733#if defined(TARGET_I386)
734
bellardb56dad12003-05-08 15:38:04 +0000735/* 'pc' is the host PC at which the exception was raised. 'address' is
bellardfd6ce8f2003-05-14 19:00:11 +0000736 the effective address of the memory exception. 'is_write' is 1 if a
737 write caused the exception and otherwise 0'. 'old_set' is the
738 signal set which should be restored */
bellard2b413142003-05-14 23:01:10 +0000739static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000740 int is_write, sigset_t *old_set,
741 void *puc)
bellard9de5e442003-03-23 16:49:39 +0000742{
bellarda513fe12003-05-27 23:29:48 +0000743 TranslationBlock *tb;
744 int ret;
bellard68a79312003-06-30 13:12:32 +0000745
bellard83479e72003-06-25 16:12:37 +0000746 if (cpu_single_env)
747 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellardfd6ce8f2003-05-14 19:00:11 +0000748#if defined(DEBUG_SIGNAL)
bellardbf3e8bf2004-02-16 21:58:54 +0000749 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
750 pc, address, is_write, *(unsigned long *)old_set);
bellard9de5e442003-03-23 16:49:39 +0000751#endif
bellard25eb4482003-05-14 21:50:54 +0000752 /* XXX: locking issue */
bellardfbf9eeb2004-04-25 21:21:33 +0000753 if (is_write && page_unprotect(address, pc, puc)) {
bellardfd6ce8f2003-05-14 19:00:11 +0000754 return 1;
755 }
bellardfbf9eeb2004-04-25 21:21:33 +0000756
bellard3fb2ded2003-06-24 13:22:59 +0000757 /* see if it is an MMU fault */
bellard93a40ea2003-10-27 21:13:06 +0000758 ret = cpu_x86_handle_mmu_fault(env, address, is_write,
759 ((env->hflags & HF_CPL_MASK) == 3), 0);
bellard3fb2ded2003-06-24 13:22:59 +0000760 if (ret < 0)
761 return 0; /* not an MMU fault */
762 if (ret == 0)
763 return 1; /* the MMU fault was handled without causing real CPU fault */
764 /* now we have a real cpu fault */
bellarda513fe12003-05-27 23:29:48 +0000765 tb = tb_find_pc(pc);
766 if (tb) {
bellard9de5e442003-03-23 16:49:39 +0000767 /* the PC is inside the translated code. It means that we have
768 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +0000769 cpu_restore_state(tb, env, pc, puc);
bellard3fb2ded2003-06-24 13:22:59 +0000770 }
bellard4cbf74b2003-08-10 21:48:43 +0000771 if (ret == 1) {
bellard3fb2ded2003-06-24 13:22:59 +0000772#if 0
bellard4cbf74b2003-08-10 21:48:43 +0000773 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
774 env->eip, env->cr[2], env->error_code);
bellard3fb2ded2003-06-24 13:22:59 +0000775#endif
bellard4cbf74b2003-08-10 21:48:43 +0000776 /* we restore the process signal mask as the sigreturn should
777 do it (XXX: use sigsetjmp) */
778 sigprocmask(SIG_SETMASK, old_set, NULL);
779 raise_exception_err(EXCP0E_PAGE, env->error_code);
780 } else {
781 /* activate soft MMU for this block */
bellard3f337312003-08-20 23:02:09 +0000782 env->hflags |= HF_SOFTMMU_MASK;
bellardfbf9eeb2004-04-25 21:21:33 +0000783 cpu_resume_from_signal(env, puc);
bellard4cbf74b2003-08-10 21:48:43 +0000784 }
bellard3fb2ded2003-06-24 13:22:59 +0000785 /* never comes here */
786 return 1;
787}
788
bellarde4533c72003-06-15 19:51:39 +0000789#elif defined(TARGET_ARM)
bellard3fb2ded2003-06-24 13:22:59 +0000790static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000791 int is_write, sigset_t *old_set,
792 void *puc)
bellard3fb2ded2003-06-24 13:22:59 +0000793{
bellard68016c62005-02-07 23:12:27 +0000794 TranslationBlock *tb;
795 int ret;
796
797 if (cpu_single_env)
798 env = cpu_single_env; /* XXX: find a correct solution for multithread */
799#if defined(DEBUG_SIGNAL)
800 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
801 pc, address, is_write, *(unsigned long *)old_set);
802#endif
bellard9f0777e2005-02-02 20:42:01 +0000803 /* XXX: locking issue */
804 if (is_write && page_unprotect(address, pc, puc)) {
805 return 1;
806 }
bellard68016c62005-02-07 23:12:27 +0000807 /* see if it is an MMU fault */
808 ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
809 if (ret < 0)
810 return 0; /* not an MMU fault */
811 if (ret == 0)
812 return 1; /* the MMU fault was handled without causing real CPU fault */
813 /* now we have a real cpu fault */
814 tb = tb_find_pc(pc);
815 if (tb) {
816 /* the PC is inside the translated code. It means that we have
817 a virtual CPU fault */
818 cpu_restore_state(tb, env, pc, puc);
819 }
820 /* we restore the process signal mask as the sigreturn should
821 do it (XXX: use sigsetjmp) */
822 sigprocmask(SIG_SETMASK, old_set, NULL);
823 cpu_loop_exit();
bellard3fb2ded2003-06-24 13:22:59 +0000824}
bellard93ac68b2003-09-30 20:57:29 +0000825#elif defined(TARGET_SPARC)
826static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000827 int is_write, sigset_t *old_set,
828 void *puc)
bellard93ac68b2003-09-30 20:57:29 +0000829{
bellard68016c62005-02-07 23:12:27 +0000830 TranslationBlock *tb;
831 int ret;
832
833 if (cpu_single_env)
834 env = cpu_single_env; /* XXX: find a correct solution for multithread */
835#if defined(DEBUG_SIGNAL)
836 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
837 pc, address, is_write, *(unsigned long *)old_set);
838#endif
bellardb453b702004-01-04 15:45:21 +0000839 /* XXX: locking issue */
bellardfbf9eeb2004-04-25 21:21:33 +0000840 if (is_write && page_unprotect(address, pc, puc)) {
bellardb453b702004-01-04 15:45:21 +0000841 return 1;
842 }
bellard68016c62005-02-07 23:12:27 +0000843 /* see if it is an MMU fault */
844 ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
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 /* now we have a real cpu fault */
850 tb = tb_find_pc(pc);
851 if (tb) {
852 /* the PC is inside the translated code. It means that we have
853 a virtual CPU fault */
854 cpu_restore_state(tb, env, pc, puc);
855 }
856 /* we restore the process signal mask as the sigreturn should
857 do it (XXX: use sigsetjmp) */
858 sigprocmask(SIG_SETMASK, old_set, NULL);
859 cpu_loop_exit();
bellard93ac68b2003-09-30 20:57:29 +0000860}
bellard67867302003-11-23 17:05:30 +0000861#elif defined (TARGET_PPC)
862static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000863 int is_write, sigset_t *old_set,
864 void *puc)
bellard67867302003-11-23 17:05:30 +0000865{
866 TranslationBlock *tb;
bellardce097762004-01-04 23:53:18 +0000867 int ret;
bellard67867302003-11-23 17:05:30 +0000868
bellard67867302003-11-23 17:05:30 +0000869 if (cpu_single_env)
870 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellard67867302003-11-23 17:05:30 +0000871#if defined(DEBUG_SIGNAL)
872 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
873 pc, address, is_write, *(unsigned long *)old_set);
874#endif
875 /* XXX: locking issue */
bellardfbf9eeb2004-04-25 21:21:33 +0000876 if (is_write && page_unprotect(address, pc, puc)) {
bellard67867302003-11-23 17:05:30 +0000877 return 1;
878 }
879
bellardce097762004-01-04 23:53:18 +0000880 /* see if it is an MMU fault */
bellard7f957d22004-01-18 23:19:48 +0000881 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
bellardce097762004-01-04 23:53:18 +0000882 if (ret < 0)
883 return 0; /* not an MMU fault */
884 if (ret == 0)
885 return 1; /* the MMU fault was handled without causing real CPU fault */
886
bellard67867302003-11-23 17:05:30 +0000887 /* 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 */
bellardbf3e8bf2004-02-16 21:58:54 +0000892 cpu_restore_state(tb, env, pc, puc);
bellard67867302003-11-23 17:05:30 +0000893 }
bellardce097762004-01-04 23:53:18 +0000894 if (ret == 1) {
bellard67867302003-11-23 17:05:30 +0000895#if 0
bellardce097762004-01-04 23:53:18 +0000896 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
897 env->nip, env->error_code, tb);
bellard67867302003-11-23 17:05:30 +0000898#endif
899 /* we restore the process signal mask as the sigreturn should
900 do it (XXX: use sigsetjmp) */
bellardbf3e8bf2004-02-16 21:58:54 +0000901 sigprocmask(SIG_SETMASK, old_set, NULL);
bellard9fddaa02004-05-21 12:59:32 +0000902 do_raise_exception_err(env->exception_index, env->error_code);
bellardce097762004-01-04 23:53:18 +0000903 } else {
904 /* activate soft MMU for this block */
bellardfbf9eeb2004-04-25 21:21:33 +0000905 cpu_resume_from_signal(env, puc);
bellardce097762004-01-04 23:53:18 +0000906 }
bellard67867302003-11-23 17:05:30 +0000907 /* never comes here */
908 return 1;
909}
bellarde4533c72003-06-15 19:51:39 +0000910#else
911#error unsupported target CPU
912#endif
bellard9de5e442003-03-23 16:49:39 +0000913
bellard2b413142003-05-14 23:01:10 +0000914#if defined(__i386__)
915
bellardbf3e8bf2004-02-16 21:58:54 +0000916#if defined(USE_CODE_COPY)
917static void cpu_send_trap(unsigned long pc, int trap,
918 struct ucontext *uc)
919{
920 TranslationBlock *tb;
921
922 if (cpu_single_env)
923 env = cpu_single_env; /* XXX: find a correct solution for multithread */
924 /* now we have a real cpu fault */
925 tb = tb_find_pc(pc);
926 if (tb) {
927 /* the PC is inside the translated code. It means that we have
928 a virtual CPU fault */
929 cpu_restore_state(tb, env, pc, uc);
930 }
931 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
932 raise_exception_err(trap, env->error_code);
933}
934#endif
935
bellarde4533c72003-06-15 19:51:39 +0000936int cpu_signal_handler(int host_signum, struct siginfo *info,
937 void *puc)
bellard9de5e442003-03-23 16:49:39 +0000938{
bellard9de5e442003-03-23 16:49:39 +0000939 struct ucontext *uc = puc;
940 unsigned long pc;
bellardbf3e8bf2004-02-16 21:58:54 +0000941 int trapno;
bellard97eb5b12004-02-25 23:19:55 +0000942
bellardd691f662003-03-24 21:58:34 +0000943#ifndef REG_EIP
944/* for glibc 2.1 */
bellardfd6ce8f2003-05-14 19:00:11 +0000945#define REG_EIP EIP
946#define REG_ERR ERR
947#define REG_TRAPNO TRAPNO
bellardd691f662003-03-24 21:58:34 +0000948#endif
bellardfc2b4c42003-03-29 16:52:44 +0000949 pc = uc->uc_mcontext.gregs[REG_EIP];
bellardbf3e8bf2004-02-16 21:58:54 +0000950 trapno = uc->uc_mcontext.gregs[REG_TRAPNO];
951#if defined(TARGET_I386) && defined(USE_CODE_COPY)
952 if (trapno == 0x00 || trapno == 0x05) {
953 /* send division by zero or bound exception */
954 cpu_send_trap(pc, trapno, uc);
955 return 1;
956 } else
957#endif
958 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
959 trapno == 0xe ?
960 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
961 &uc->uc_sigmask, puc);
bellard2b413142003-05-14 23:01:10 +0000962}
963
bellardbc51c5c2004-03-17 23:46:04 +0000964#elif defined(__x86_64__)
965
966int cpu_signal_handler(int host_signum, struct siginfo *info,
967 void *puc)
968{
969 struct ucontext *uc = puc;
970 unsigned long pc;
971
972 pc = uc->uc_mcontext.gregs[REG_RIP];
973 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
974 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
975 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
976 &uc->uc_sigmask, puc);
977}
978
bellard83fb7ad2004-07-05 21:25:26 +0000979#elif defined(__powerpc__)
bellard2b413142003-05-14 23:01:10 +0000980
bellard83fb7ad2004-07-05 21:25:26 +0000981/***********************************************************************
982 * signal context platform-specific definitions
983 * From Wine
984 */
985#ifdef linux
986/* All Registers access - only for local access */
987# define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
988/* Gpr Registers access */
989# define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
990# define IAR_sig(context) REG_sig(nip, context) /* Program counter */
991# define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
992# define CTR_sig(context) REG_sig(ctr, context) /* Count register */
993# define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
994# define LR_sig(context) REG_sig(link, context) /* Link register */
995# define CR_sig(context) REG_sig(ccr, context) /* Condition register */
996/* Float Registers access */
997# define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
998# define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
999/* Exception Registers access */
1000# define DAR_sig(context) REG_sig(dar, context)
1001# define DSISR_sig(context) REG_sig(dsisr, context)
1002# define TRAP_sig(context) REG_sig(trap, context)
1003#endif /* linux */
1004
1005#ifdef __APPLE__
1006# include <sys/ucontext.h>
1007typedef struct ucontext SIGCONTEXT;
1008/* All Registers access - only for local access */
1009# define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
1010# define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
1011# define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
1012# define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
1013/* Gpr Registers access */
1014# define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
1015# define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
1016# define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
1017# define CTR_sig(context) REG_sig(ctr, context)
1018# define XER_sig(context) REG_sig(xer, context) /* Link register */
1019# define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
1020# define CR_sig(context) REG_sig(cr, context) /* Condition register */
1021/* Float Registers access */
1022# define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
1023# define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
1024/* Exception Registers access */
1025# define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
1026# define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
1027# define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1028#endif /* __APPLE__ */
1029
bellardd1d9f422004-07-14 17:20:55 +00001030int cpu_signal_handler(int host_signum, struct siginfo *info,
bellarde4533c72003-06-15 19:51:39 +00001031 void *puc)
bellard2b413142003-05-14 23:01:10 +00001032{
bellard25eb4482003-05-14 21:50:54 +00001033 struct ucontext *uc = puc;
bellard25eb4482003-05-14 21:50:54 +00001034 unsigned long pc;
bellard25eb4482003-05-14 21:50:54 +00001035 int is_write;
1036
bellard83fb7ad2004-07-05 21:25:26 +00001037 pc = IAR_sig(uc);
bellard25eb4482003-05-14 21:50:54 +00001038 is_write = 0;
1039#if 0
1040 /* ppc 4xx case */
bellard83fb7ad2004-07-05 21:25:26 +00001041 if (DSISR_sig(uc) & 0x00800000)
bellard25eb4482003-05-14 21:50:54 +00001042 is_write = 1;
bellard9de5e442003-03-23 16:49:39 +00001043#else
bellard83fb7ad2004-07-05 21:25:26 +00001044 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
bellard25eb4482003-05-14 21:50:54 +00001045 is_write = 1;
1046#endif
1047 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001048 is_write, &uc->uc_sigmask, puc);
bellard9de5e442003-03-23 16:49:39 +00001049}
bellard2b413142003-05-14 23:01:10 +00001050
bellard2f87c602003-06-02 20:38:09 +00001051#elif defined(__alpha__)
1052
bellarde4533c72003-06-15 19:51:39 +00001053int cpu_signal_handler(int host_signum, struct siginfo *info,
bellard2f87c602003-06-02 20:38:09 +00001054 void *puc)
1055{
1056 struct ucontext *uc = puc;
1057 uint32_t *pc = uc->uc_mcontext.sc_pc;
1058 uint32_t insn = *pc;
1059 int is_write = 0;
1060
bellard8c6939c2003-06-09 15:28:00 +00001061 /* XXX: need kernel patch to get write flag faster */
bellard2f87c602003-06-02 20:38:09 +00001062 switch (insn >> 26) {
1063 case 0x0d: // stw
1064 case 0x0e: // stb
1065 case 0x0f: // stq_u
1066 case 0x24: // stf
1067 case 0x25: // stg
1068 case 0x26: // sts
1069 case 0x27: // stt
1070 case 0x2c: // stl
1071 case 0x2d: // stq
1072 case 0x2e: // stl_c
1073 case 0x2f: // stq_c
1074 is_write = 1;
1075 }
1076
1077 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001078 is_write, &uc->uc_sigmask, puc);
bellard2f87c602003-06-02 20:38:09 +00001079}
bellard8c6939c2003-06-09 15:28:00 +00001080#elif defined(__sparc__)
1081
bellarde4533c72003-06-15 19:51:39 +00001082int cpu_signal_handler(int host_signum, struct siginfo *info,
1083 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001084{
1085 uint32_t *regs = (uint32_t *)(info + 1);
1086 void *sigmask = (regs + 20);
1087 unsigned long pc;
1088 int is_write;
1089 uint32_t insn;
1090
1091 /* XXX: is there a standard glibc define ? */
1092 pc = regs[1];
1093 /* XXX: need kernel patch to get write flag faster */
1094 is_write = 0;
1095 insn = *(uint32_t *)pc;
1096 if ((insn >> 30) == 3) {
1097 switch((insn >> 19) & 0x3f) {
1098 case 0x05: // stb
1099 case 0x06: // sth
1100 case 0x04: // st
1101 case 0x07: // std
1102 case 0x24: // stf
1103 case 0x27: // stdf
1104 case 0x25: // stfsr
1105 is_write = 1;
1106 break;
1107 }
1108 }
1109 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001110 is_write, sigmask, NULL);
bellard8c6939c2003-06-09 15:28:00 +00001111}
1112
1113#elif defined(__arm__)
1114
bellarde4533c72003-06-15 19:51:39 +00001115int cpu_signal_handler(int host_signum, struct siginfo *info,
1116 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001117{
1118 struct ucontext *uc = puc;
1119 unsigned long pc;
1120 int is_write;
1121
1122 pc = uc->uc_mcontext.gregs[R15];
1123 /* XXX: compute is_write */
1124 is_write = 0;
1125 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1126 is_write,
1127 &uc->uc_sigmask);
1128}
1129
bellard38e584a2003-08-10 22:14:22 +00001130#elif defined(__mc68000)
1131
1132int cpu_signal_handler(int host_signum, struct siginfo *info,
1133 void *puc)
1134{
1135 struct ucontext *uc = puc;
1136 unsigned long pc;
1137 int is_write;
1138
1139 pc = uc->uc_mcontext.gregs[16];
1140 /* XXX: compute is_write */
1141 is_write = 0;
1142 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1143 is_write,
bellardbf3e8bf2004-02-16 21:58:54 +00001144 &uc->uc_sigmask, puc);
bellard38e584a2003-08-10 22:14:22 +00001145}
1146
bellardb8076a72005-04-07 22:20:31 +00001147#elif defined(__ia64)
1148
1149#ifndef __ISR_VALID
1150 /* This ought to be in <bits/siginfo.h>... */
1151# define __ISR_VALID 1
1152# define si_flags _sifields._sigfault._si_pad0
1153#endif
1154
1155int cpu_signal_handler(int host_signum, struct siginfo *info, void *puc)
1156{
1157 struct ucontext *uc = puc;
1158 unsigned long ip;
1159 int is_write = 0;
1160
1161 ip = uc->uc_mcontext.sc_ip;
1162 switch (host_signum) {
1163 case SIGILL:
1164 case SIGFPE:
1165 case SIGSEGV:
1166 case SIGBUS:
1167 case SIGTRAP:
1168 if (info->si_code && (info->si_flags & __ISR_VALID))
1169 /* ISR.W (write-access) is bit 33: */
1170 is_write = (info->si_isr >> 33) & 1;
1171 break;
1172
1173 default:
1174 break;
1175 }
1176 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1177 is_write,
1178 &uc->uc_sigmask, puc);
1179}
1180
bellard2b413142003-05-14 23:01:10 +00001181#else
1182
bellard3fb2ded2003-06-24 13:22:59 +00001183#error host CPU specific signal handler needed
bellard2b413142003-05-14 23:01:10 +00001184
1185#endif
bellard67b915a2004-03-31 23:37:16 +00001186
1187#endif /* !defined(CONFIG_SOFTMMU) */