blob: 8c5557739dd7795ecf180066fd39b16f0f04b1e4 [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
bellard34751872005-07-02 14:31:34 +000050#ifndef TARGET_SPARC
51#define reg_T2
52#endif
bellarde4533c72003-06-15 19:51:39 +000053
bellardfbf9eeb2004-04-25 21:21:33 +000054/* exit the current TB from a signal handler. The host registers are
55 restored in a state compatible with the CPU emulator
56 */
57void cpu_resume_from_signal(CPUState *env1, void *puc)
58{
59#if !defined(CONFIG_SOFTMMU)
60 struct ucontext *uc = puc;
61#endif
62
63 env = env1;
64
65 /* XXX: restore cpu registers saved in host registers */
66
67#if !defined(CONFIG_SOFTMMU)
68 if (puc) {
69 /* XXX: use siglongjmp ? */
70 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
71 }
72#endif
73 longjmp(env->jmp_env, 1);
74}
75
bellard7d132992003-03-06 23:23:54 +000076/* main execution loop */
77
bellarde4533c72003-06-15 19:51:39 +000078int cpu_exec(CPUState *env1)
bellard7d132992003-03-06 23:23:54 +000079{
bellard34751872005-07-02 14:31:34 +000080 int saved_T0, saved_T1;
81#if defined(reg_T2)
82 int saved_T2;
83#endif
bellarde4533c72003-06-15 19:51:39 +000084 CPUState *saved_env;
bellard34751872005-07-02 14:31:34 +000085#if defined(TARGET_I386)
bellard04369ff2003-03-20 22:33:23 +000086#ifdef reg_EAX
87 int saved_EAX;
88#endif
89#ifdef reg_ECX
90 int saved_ECX;
91#endif
92#ifdef reg_EDX
93 int saved_EDX;
94#endif
95#ifdef reg_EBX
96 int saved_EBX;
97#endif
98#ifdef reg_ESP
99 int saved_ESP;
100#endif
101#ifdef reg_EBP
102 int saved_EBP;
103#endif
104#ifdef reg_ESI
105 int saved_ESI;
106#endif
107#ifdef reg_EDI
108 int saved_EDI;
109#endif
bellard34751872005-07-02 14:31:34 +0000110#elif defined(TARGET_SPARC)
111#if defined(reg_REGWPTR)
112 uint32_t *saved_regwptr;
113#endif
114#endif
bellard8c6939c2003-06-09 15:28:00 +0000115#ifdef __sparc__
116 int saved_i7, tmp_T0;
117#endif
bellard68a79312003-06-30 13:12:32 +0000118 int code_gen_size, ret, interrupt_request;
bellard7d132992003-03-06 23:23:54 +0000119 void (*gen_func)(void);
bellard9de5e442003-03-23 16:49:39 +0000120 TranslationBlock *tb, **ptb;
bellardc27004e2005-01-03 23:35:10 +0000121 target_ulong cs_base, pc;
122 uint8_t *tc_ptr;
bellard6dbad632003-03-16 18:05:05 +0000123 unsigned int flags;
bellard8c6939c2003-06-09 15:28:00 +0000124
bellard7d132992003-03-06 23:23:54 +0000125 /* first we save global registers */
bellardc27004e2005-01-03 23:35:10 +0000126 saved_env = env;
127 env = env1;
bellard7d132992003-03-06 23:23:54 +0000128 saved_T0 = T0;
129 saved_T1 = T1;
bellard34751872005-07-02 14:31:34 +0000130#if defined(reg_T2)
bellarde4533c72003-06-15 19:51:39 +0000131 saved_T2 = T2;
bellard34751872005-07-02 14:31:34 +0000132#endif
bellarde4533c72003-06-15 19:51:39 +0000133#ifdef __sparc__
134 /* we also save i7 because longjmp may not restore it */
135 asm volatile ("mov %%i7, %0" : "=r" (saved_i7));
136#endif
137
138#if defined(TARGET_I386)
bellard04369ff2003-03-20 22:33:23 +0000139#ifdef reg_EAX
140 saved_EAX = EAX;
bellard04369ff2003-03-20 22:33:23 +0000141#endif
142#ifdef reg_ECX
143 saved_ECX = ECX;
bellard04369ff2003-03-20 22:33:23 +0000144#endif
145#ifdef reg_EDX
146 saved_EDX = EDX;
bellard04369ff2003-03-20 22:33:23 +0000147#endif
148#ifdef reg_EBX
149 saved_EBX = EBX;
bellard04369ff2003-03-20 22:33:23 +0000150#endif
151#ifdef reg_ESP
152 saved_ESP = ESP;
bellard04369ff2003-03-20 22:33:23 +0000153#endif
154#ifdef reg_EBP
155 saved_EBP = EBP;
bellard04369ff2003-03-20 22:33:23 +0000156#endif
157#ifdef reg_ESI
158 saved_ESI = ESI;
bellard04369ff2003-03-20 22:33:23 +0000159#endif
160#ifdef reg_EDI
161 saved_EDI = EDI;
bellard04369ff2003-03-20 22:33:23 +0000162#endif
bellard0d1a29f2004-10-12 22:01:28 +0000163
164 env_to_regs();
bellard9de5e442003-03-23 16:49:39 +0000165 /* put eflags in CPU temporary format */
bellardfc2b4c42003-03-29 16:52:44 +0000166 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
167 DF = 1 - (2 * ((env->eflags >> 10) & 1));
bellard9de5e442003-03-23 16:49:39 +0000168 CC_OP = CC_OP_EFLAGS;
bellardfc2b4c42003-03-29 16:52:44 +0000169 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000170#elif defined(TARGET_ARM)
171 {
172 unsigned int psr;
173 psr = env->cpsr;
174 env->CF = (psr >> 29) & 1;
175 env->NZF = (psr & 0xc0000000) ^ 0x40000000;
176 env->VF = (psr << 3) & 0x80000000;
bellard99c475a2005-01-31 20:45:13 +0000177 env->QF = (psr >> 27) & 1;
178 env->cpsr = psr & ~CACHED_CPSR_BITS;
bellarde4533c72003-06-15 19:51:39 +0000179 }
bellard93ac68b2003-09-30 20:57:29 +0000180#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000181#if defined(reg_REGWPTR)
182 saved_regwptr = REGWPTR;
183#endif
bellard67867302003-11-23 17:05:30 +0000184#elif defined(TARGET_PPC)
bellarde4533c72003-06-15 19:51:39 +0000185#else
186#error unsupported target CPU
187#endif
bellard3fb2ded2003-06-24 13:22:59 +0000188 env->exception_index = -1;
bellard9d27abd2003-05-10 13:13:54 +0000189
bellard7d132992003-03-06 23:23:54 +0000190 /* prepare setjmp context for exception handling */
bellard3fb2ded2003-06-24 13:22:59 +0000191 for(;;) {
192 if (setjmp(env->jmp_env) == 0) {
bellardee8b7022004-02-03 23:35:10 +0000193 env->current_tb = NULL;
bellard3fb2ded2003-06-24 13:22:59 +0000194 /* if an exception is pending, we execute it here */
195 if (env->exception_index >= 0) {
196 if (env->exception_index >= EXCP_INTERRUPT) {
197 /* exit request from the cpu execution loop */
198 ret = env->exception_index;
199 break;
200 } else if (env->user_mode_only) {
201 /* if user mode only, we simulate a fake exception
202 which will be hanlded outside the cpu execution
203 loop */
bellard83479e72003-06-25 16:12:37 +0000204#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000205 do_interrupt_user(env->exception_index,
206 env->exception_is_int,
207 env->error_code,
208 env->exception_next_eip);
bellard83479e72003-06-25 16:12:37 +0000209#endif
bellard3fb2ded2003-06-24 13:22:59 +0000210 ret = env->exception_index;
211 break;
212 } else {
bellard83479e72003-06-25 16:12:37 +0000213#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000214 /* simulate a real cpu exception. On i386, it can
215 trigger new exceptions, but we do not handle
216 double or triple faults yet. */
217 do_interrupt(env->exception_index,
218 env->exception_is_int,
219 env->error_code,
bellardd05e66d2003-08-20 21:34:35 +0000220 env->exception_next_eip, 0);
bellardce097762004-01-04 23:53:18 +0000221#elif defined(TARGET_PPC)
222 do_interrupt(env);
bellarde95c8d52004-09-30 22:22:08 +0000223#elif defined(TARGET_SPARC)
bellard1a0c3292005-02-13 19:02:07 +0000224 do_interrupt(env->exception_index);
bellard83479e72003-06-25 16:12:37 +0000225#endif
bellard3fb2ded2003-06-24 13:22:59 +0000226 }
227 env->exception_index = -1;
bellard9df217a2005-02-10 22:05:51 +0000228 }
229#ifdef USE_KQEMU
230 if (kqemu_is_ok(env) && env->interrupt_request == 0) {
231 int ret;
232 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
233 ret = kqemu_cpu_exec(env);
234 /* put eflags in CPU temporary format */
235 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
236 DF = 1 - (2 * ((env->eflags >> 10) & 1));
237 CC_OP = CC_OP_EFLAGS;
238 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
239 if (ret == 1) {
240 /* exception */
241 longjmp(env->jmp_env, 1);
242 } else if (ret == 2) {
243 /* softmmu execution needed */
244 } else {
245 if (env->interrupt_request != 0) {
246 /* hardware interrupt will be executed just after */
247 } else {
248 /* otherwise, we restart */
249 longjmp(env->jmp_env, 1);
250 }
251 }
bellard9de5e442003-03-23 16:49:39 +0000252 }
bellard9df217a2005-02-10 22:05:51 +0000253#endif
254
bellard3fb2ded2003-06-24 13:22:59 +0000255 T0 = 0; /* force lookup of first TB */
256 for(;;) {
257#ifdef __sparc__
258 /* g1 can be modified by some libc? functions */
259 tmp_T0 = T0;
260#endif
bellard68a79312003-06-30 13:12:32 +0000261 interrupt_request = env->interrupt_request;
bellard2e255c62003-08-21 23:25:21 +0000262 if (__builtin_expect(interrupt_request, 0)) {
bellard68a79312003-06-30 13:12:32 +0000263#if defined(TARGET_I386)
264 /* if hardware interrupt pending, we execute it */
265 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
bellard3f337312003-08-20 23:02:09 +0000266 (env->eflags & IF_MASK) &&
267 !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
bellard68a79312003-06-30 13:12:32 +0000268 int intno;
bellardfbf9eeb2004-04-25 21:21:33 +0000269 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellarda541f292004-04-12 20:39:29 +0000270 intno = cpu_get_pic_interrupt(env);
bellardf193c792004-03-21 17:06:25 +0000271 if (loglevel & CPU_LOG_TB_IN_ASM) {
bellard68a79312003-06-30 13:12:32 +0000272 fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
273 }
bellardd05e66d2003-08-20 21:34:35 +0000274 do_interrupt(intno, 0, 0, 0, 1);
bellard907a5b22003-06-30 23:18:22 +0000275 /* ensure that no TB jump will be modified as
276 the program flow was changed */
277#ifdef __sparc__
278 tmp_T0 = 0;
279#else
280 T0 = 0;
281#endif
bellard68a79312003-06-30 13:12:32 +0000282 }
bellardce097762004-01-04 23:53:18 +0000283#elif defined(TARGET_PPC)
bellard9fddaa02004-05-21 12:59:32 +0000284#if 0
285 if ((interrupt_request & CPU_INTERRUPT_RESET)) {
286 cpu_ppc_reset(env);
287 }
288#endif
289 if (msr_ee != 0) {
bellardce097762004-01-04 23:53:18 +0000290 if ((interrupt_request & CPU_INTERRUPT_HARD)) {
bellard9fddaa02004-05-21 12:59:32 +0000291 /* Raise it */
292 env->exception_index = EXCP_EXTERNAL;
293 env->error_code = 0;
bellardce097762004-01-04 23:53:18 +0000294 do_interrupt(env);
295 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellard9fddaa02004-05-21 12:59:32 +0000296 } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) {
297 /* Raise it */
298 env->exception_index = EXCP_DECR;
299 env->error_code = 0;
300 do_interrupt(env);
301 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
302 }
bellardce097762004-01-04 23:53:18 +0000303 }
bellarde95c8d52004-09-30 22:22:08 +0000304#elif defined(TARGET_SPARC)
bellard66321a12005-04-06 20:47:48 +0000305 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
306 (env->psret != 0)) {
307 int pil = env->interrupt_index & 15;
308 int type = env->interrupt_index & 0xf0;
309
310 if (((type == TT_EXTINT) &&
311 (pil == 15 || pil > env->psrpil)) ||
312 type != TT_EXTINT) {
313 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
314 do_interrupt(env->interrupt_index);
315 env->interrupt_index = 0;
316 }
bellarde95c8d52004-09-30 22:22:08 +0000317 } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
318 //do_interrupt(0, 0, 0, 0, 0);
319 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
320 }
bellard68a79312003-06-30 13:12:32 +0000321#endif
bellardbf3e8bf2004-02-16 21:58:54 +0000322 if (interrupt_request & CPU_INTERRUPT_EXITTB) {
323 env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
324 /* ensure that no TB jump will be modified as
325 the program flow was changed */
326#ifdef __sparc__
327 tmp_T0 = 0;
328#else
329 T0 = 0;
330#endif
331 }
bellard68a79312003-06-30 13:12:32 +0000332 if (interrupt_request & CPU_INTERRUPT_EXIT) {
333 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
334 env->exception_index = EXCP_INTERRUPT;
335 cpu_loop_exit();
336 }
bellard3fb2ded2003-06-24 13:22:59 +0000337 }
338#ifdef DEBUG_EXEC
bellardc27004e2005-01-03 23:35:10 +0000339 if ((loglevel & CPU_LOG_EXEC)) {
bellard3fb2ded2003-06-24 13:22:59 +0000340#if defined(TARGET_I386)
341 /* restore flags in standard format */
bellardfc9f7152005-04-26 19:33:35 +0000342#ifdef reg_EAX
bellard3fb2ded2003-06-24 13:22:59 +0000343 env->regs[R_EAX] = EAX;
bellardfc9f7152005-04-26 19:33:35 +0000344#endif
345#ifdef reg_EBX
bellard3fb2ded2003-06-24 13:22:59 +0000346 env->regs[R_EBX] = EBX;
bellardfc9f7152005-04-26 19:33:35 +0000347#endif
348#ifdef reg_ECX
bellard3fb2ded2003-06-24 13:22:59 +0000349 env->regs[R_ECX] = ECX;
bellardfc9f7152005-04-26 19:33:35 +0000350#endif
351#ifdef reg_EDX
bellard3fb2ded2003-06-24 13:22:59 +0000352 env->regs[R_EDX] = EDX;
bellardfc9f7152005-04-26 19:33:35 +0000353#endif
354#ifdef reg_ESI
bellard3fb2ded2003-06-24 13:22:59 +0000355 env->regs[R_ESI] = ESI;
bellardfc9f7152005-04-26 19:33:35 +0000356#endif
357#ifdef reg_EDI
bellard3fb2ded2003-06-24 13:22:59 +0000358 env->regs[R_EDI] = EDI;
bellardfc9f7152005-04-26 19:33:35 +0000359#endif
360#ifdef reg_EBP
bellard3fb2ded2003-06-24 13:22:59 +0000361 env->regs[R_EBP] = EBP;
bellardfc9f7152005-04-26 19:33:35 +0000362#endif
363#ifdef reg_ESP
bellard3fb2ded2003-06-24 13:22:59 +0000364 env->regs[R_ESP] = ESP;
bellardfc9f7152005-04-26 19:33:35 +0000365#endif
bellard3fb2ded2003-06-24 13:22:59 +0000366 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard7fe48482004-10-09 18:08:01 +0000367 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
bellard3fb2ded2003-06-24 13:22:59 +0000368 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000369#elif defined(TARGET_ARM)
bellard1b21b622003-07-09 17:16:27 +0000370 env->cpsr = compute_cpsr();
bellard7fe48482004-10-09 18:08:01 +0000371 cpu_dump_state(env, logfile, fprintf, 0);
bellard99c475a2005-01-31 20:45:13 +0000372 env->cpsr &= ~CACHED_CPSR_BITS;
bellard93ac68b2003-09-30 20:57:29 +0000373#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000374 REGWPTR = env->regbase + (env->cwp * 16);
375 env->regwptr = REGWPTR;
376 cpu_dump_state(env, logfile, fprintf, 0);
bellard67867302003-11-23 17:05:30 +0000377#elif defined(TARGET_PPC)
bellard7fe48482004-10-09 18:08:01 +0000378 cpu_dump_state(env, logfile, fprintf, 0);
bellarde4533c72003-06-15 19:51:39 +0000379#else
380#error unsupported target CPU
381#endif
bellard3fb2ded2003-06-24 13:22:59 +0000382 }
bellard7d132992003-03-06 23:23:54 +0000383#endif
bellard3f337312003-08-20 23:02:09 +0000384 /* we record a subset of the CPU state. It will
385 always be the same before a given translated block
386 is executed. */
bellarde4533c72003-06-15 19:51:39 +0000387#if defined(TARGET_I386)
bellard2e255c62003-08-21 23:25:21 +0000388 flags = env->hflags;
bellard3f337312003-08-20 23:02:09 +0000389 flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
bellard3fb2ded2003-06-24 13:22:59 +0000390 cs_base = env->segs[R_CS].base;
391 pc = cs_base + env->eip;
bellarde4533c72003-06-15 19:51:39 +0000392#elif defined(TARGET_ARM)
bellardb7bcbe92005-02-22 19:27:29 +0000393 flags = env->thumb | (env->vfp.vec_len << 1)
394 | (env->vfp.vec_stride << 4);
bellard3fb2ded2003-06-24 13:22:59 +0000395 cs_base = 0;
bellardc27004e2005-01-03 23:35:10 +0000396 pc = env->regs[15];
bellard93ac68b2003-09-30 20:57:29 +0000397#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000398#ifdef TARGET_SPARC64
399 flags = (env->pstate << 2) | ((env->lsu & (DMMU_E | IMMU_E)) >> 2);
400#else
401 flags = env->psrs | ((env->mmuregs[0] & (MMU_E | MMU_NF)) << 1);
402#endif
bellardc27004e2005-01-03 23:35:10 +0000403 cs_base = env->npc;
404 pc = env->pc;
bellard67867302003-11-23 17:05:30 +0000405#elif defined(TARGET_PPC)
bellard111bfab2005-04-23 18:16:07 +0000406 flags = (msr_pr << MSR_PR) | (msr_fp << MSR_FP) |
407 (msr_se << MSR_SE) | (msr_le << MSR_LE);
bellard67867302003-11-23 17:05:30 +0000408 cs_base = 0;
bellardc27004e2005-01-03 23:35:10 +0000409 pc = env->nip;
bellarde4533c72003-06-15 19:51:39 +0000410#else
411#error unsupported CPU
412#endif
bellardc27004e2005-01-03 23:35:10 +0000413 tb = tb_find(&ptb, pc, cs_base,
bellard3fb2ded2003-06-24 13:22:59 +0000414 flags);
bellardd4e81642003-05-25 16:46:15 +0000415 if (!tb) {
bellard13768472004-01-04 17:43:01 +0000416 TranslationBlock **ptb1;
417 unsigned int h;
418 target_ulong phys_pc, phys_page1, phys_page2, virt_page2;
419
420
bellard3fb2ded2003-06-24 13:22:59 +0000421 spin_lock(&tb_lock);
bellard13768472004-01-04 17:43:01 +0000422
423 tb_invalidated_flag = 0;
bellard0d1a29f2004-10-12 22:01:28 +0000424
425 regs_to_env(); /* XXX: do it just before cpu_gen_code() */
bellard13768472004-01-04 17:43:01 +0000426
427 /* find translated block using physical mappings */
bellardc27004e2005-01-03 23:35:10 +0000428 phys_pc = get_phys_addr_code(env, pc);
bellard13768472004-01-04 17:43:01 +0000429 phys_page1 = phys_pc & TARGET_PAGE_MASK;
430 phys_page2 = -1;
431 h = tb_phys_hash_func(phys_pc);
432 ptb1 = &tb_phys_hash[h];
433 for(;;) {
434 tb = *ptb1;
435 if (!tb)
436 goto not_found;
bellardc27004e2005-01-03 23:35:10 +0000437 if (tb->pc == pc &&
bellard13768472004-01-04 17:43:01 +0000438 tb->page_addr[0] == phys_page1 &&
bellardc27004e2005-01-03 23:35:10 +0000439 tb->cs_base == cs_base &&
bellard13768472004-01-04 17:43:01 +0000440 tb->flags == flags) {
441 /* check next page if needed */
bellardb516f852004-01-18 21:50:04 +0000442 if (tb->page_addr[1] != -1) {
bellardc27004e2005-01-03 23:35:10 +0000443 virt_page2 = (pc & TARGET_PAGE_MASK) +
bellardb516f852004-01-18 21:50:04 +0000444 TARGET_PAGE_SIZE;
bellard13768472004-01-04 17:43:01 +0000445 phys_page2 = get_phys_addr_code(env, virt_page2);
446 if (tb->page_addr[1] == phys_page2)
447 goto found;
448 } else {
449 goto found;
450 }
451 }
452 ptb1 = &tb->phys_hash_next;
453 }
454 not_found:
bellard3fb2ded2003-06-24 13:22:59 +0000455 /* if no translated code available, then translate it now */
bellardc27004e2005-01-03 23:35:10 +0000456 tb = tb_alloc(pc);
bellard3fb2ded2003-06-24 13:22:59 +0000457 if (!tb) {
458 /* flush must be done */
bellardb453b702004-01-04 15:45:21 +0000459 tb_flush(env);
bellard3fb2ded2003-06-24 13:22:59 +0000460 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000461 tb = tb_alloc(pc);
bellard3fb2ded2003-06-24 13:22:59 +0000462 /* don't forget to invalidate previous TB info */
bellardc27004e2005-01-03 23:35:10 +0000463 ptb = &tb_hash[tb_hash_func(pc)];
bellard3fb2ded2003-06-24 13:22:59 +0000464 T0 = 0;
465 }
466 tc_ptr = code_gen_ptr;
467 tb->tc_ptr = tc_ptr;
bellardc27004e2005-01-03 23:35:10 +0000468 tb->cs_base = cs_base;
bellard3fb2ded2003-06-24 13:22:59 +0000469 tb->flags = flags;
bellardfacc68b2003-09-17 22:51:18 +0000470 cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
bellard13768472004-01-04 17:43:01 +0000471 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
472
473 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000474 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellard13768472004-01-04 17:43:01 +0000475 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000476 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellard13768472004-01-04 17:43:01 +0000477 phys_page2 = get_phys_addr_code(env, virt_page2);
478 }
479 tb_link_phys(tb, phys_pc, phys_page2);
480
481 found:
bellard36bdbe52003-11-19 22:12:02 +0000482 if (tb_invalidated_flag) {
483 /* as some TB could have been invalidated because
484 of memory exceptions while generating the code, we
485 must recompute the hash index here */
bellardc27004e2005-01-03 23:35:10 +0000486 ptb = &tb_hash[tb_hash_func(pc)];
bellard36bdbe52003-11-19 22:12:02 +0000487 while (*ptb != NULL)
488 ptb = &(*ptb)->hash_next;
489 T0 = 0;
490 }
bellard13768472004-01-04 17:43:01 +0000491 /* we add the TB in the virtual pc hash table */
bellard3fb2ded2003-06-24 13:22:59 +0000492 *ptb = tb;
493 tb->hash_next = NULL;
494 tb_link(tb);
bellard3fb2ded2003-06-24 13:22:59 +0000495 spin_unlock(&tb_lock);
496 }
bellard9d27abd2003-05-10 13:13:54 +0000497#ifdef DEBUG_EXEC
bellardc1135f62005-01-30 22:41:54 +0000498 if ((loglevel & CPU_LOG_EXEC)) {
bellardc27004e2005-01-03 23:35:10 +0000499 fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
500 (long)tb->tc_ptr, tb->pc,
501 lookup_symbol(tb->pc));
bellard3fb2ded2003-06-24 13:22:59 +0000502 }
bellard9d27abd2003-05-10 13:13:54 +0000503#endif
bellard8c6939c2003-06-09 15:28:00 +0000504#ifdef __sparc__
bellard3fb2ded2003-06-24 13:22:59 +0000505 T0 = tmp_T0;
bellard8c6939c2003-06-09 15:28:00 +0000506#endif
bellardfacc68b2003-09-17 22:51:18 +0000507 /* see if we can patch the calling TB. */
bellardc27004e2005-01-03 23:35:10 +0000508 {
509 if (T0 != 0
bellardbf3e8bf2004-02-16 21:58:54 +0000510#if defined(TARGET_I386) && defined(USE_CODE_COPY)
511 && (tb->cflags & CF_CODE_COPY) ==
512 (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
513#endif
514 ) {
bellard3fb2ded2003-06-24 13:22:59 +0000515 spin_lock(&tb_lock);
bellardc27004e2005-01-03 23:35:10 +0000516 tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
bellard97eb5b12004-02-25 23:19:55 +0000517#if defined(USE_CODE_COPY)
518 /* propagates the FP use info */
519 ((TranslationBlock *)(T0 & ~3))->cflags |=
520 (tb->cflags & CF_FP_USED);
521#endif
bellard3fb2ded2003-06-24 13:22:59 +0000522 spin_unlock(&tb_lock);
523 }
bellardc27004e2005-01-03 23:35:10 +0000524 }
bellard3fb2ded2003-06-24 13:22:59 +0000525 tc_ptr = tb->tc_ptr;
bellard83479e72003-06-25 16:12:37 +0000526 env->current_tb = tb;
bellard3fb2ded2003-06-24 13:22:59 +0000527 /* execute the generated code */
528 gen_func = (void *)tc_ptr;
529#if defined(__sparc__)
530 __asm__ __volatile__("call %0\n\t"
531 "mov %%o7,%%i0"
532 : /* no outputs */
533 : "r" (gen_func)
534 : "i0", "i1", "i2", "i3", "i4", "i5");
535#elif defined(__arm__)
536 asm volatile ("mov pc, %0\n\t"
537 ".global exec_loop\n\t"
538 "exec_loop:\n\t"
539 : /* no outputs */
540 : "r" (gen_func)
541 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
bellardbf3e8bf2004-02-16 21:58:54 +0000542#elif defined(TARGET_I386) && defined(USE_CODE_COPY)
543{
544 if (!(tb->cflags & CF_CODE_COPY)) {
bellard97eb5b12004-02-25 23:19:55 +0000545 if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
546 save_native_fp_state(env);
547 }
bellardbf3e8bf2004-02-16 21:58:54 +0000548 gen_func();
549 } else {
bellard97eb5b12004-02-25 23:19:55 +0000550 if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
551 restore_native_fp_state(env);
552 }
bellardbf3e8bf2004-02-16 21:58:54 +0000553 /* we work with native eflags */
554 CC_SRC = cc_table[CC_OP].compute_all();
555 CC_OP = CC_OP_EFLAGS;
556 asm(".globl exec_loop\n"
557 "\n"
558 "debug1:\n"
559 " pushl %%ebp\n"
560 " fs movl %10, %9\n"
561 " fs movl %11, %%eax\n"
562 " andl $0x400, %%eax\n"
563 " fs orl %8, %%eax\n"
564 " pushl %%eax\n"
565 " popf\n"
566 " fs movl %%esp, %12\n"
567 " fs movl %0, %%eax\n"
568 " fs movl %1, %%ecx\n"
569 " fs movl %2, %%edx\n"
570 " fs movl %3, %%ebx\n"
571 " fs movl %4, %%esp\n"
572 " fs movl %5, %%ebp\n"
573 " fs movl %6, %%esi\n"
574 " fs movl %7, %%edi\n"
575 " fs jmp *%9\n"
576 "exec_loop:\n"
577 " fs movl %%esp, %4\n"
578 " fs movl %12, %%esp\n"
579 " fs movl %%eax, %0\n"
580 " fs movl %%ecx, %1\n"
581 " fs movl %%edx, %2\n"
582 " fs movl %%ebx, %3\n"
583 " fs movl %%ebp, %5\n"
584 " fs movl %%esi, %6\n"
585 " fs movl %%edi, %7\n"
586 " pushf\n"
587 " popl %%eax\n"
588 " movl %%eax, %%ecx\n"
589 " andl $0x400, %%ecx\n"
590 " shrl $9, %%ecx\n"
591 " andl $0x8d5, %%eax\n"
592 " fs movl %%eax, %8\n"
593 " movl $1, %%eax\n"
594 " subl %%ecx, %%eax\n"
595 " fs movl %%eax, %11\n"
596 " fs movl %9, %%ebx\n" /* get T0 value */
597 " popl %%ebp\n"
598 :
599 : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
600 "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
601 "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
602 "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
603 "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
604 "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
605 "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
606 "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
607 "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
608 "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
609 "a" (gen_func),
610 "m" (*(uint8_t *)offsetof(CPUState, df)),
611 "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
612 : "%ecx", "%edx"
613 );
614 }
615}
bellardb8076a72005-04-07 22:20:31 +0000616#elif defined(__ia64)
617 struct fptr {
618 void *ip;
619 void *gp;
620 } fp;
621
622 fp.ip = tc_ptr;
623 fp.gp = code_gen_buffer + 2 * (1 << 20);
624 (*(void (*)(void)) &fp)();
bellard3fb2ded2003-06-24 13:22:59 +0000625#else
626 gen_func();
627#endif
bellard83479e72003-06-25 16:12:37 +0000628 env->current_tb = NULL;
bellard4cbf74b2003-08-10 21:48:43 +0000629 /* reset soft MMU for next block (it can currently
630 only be set by a memory fault) */
631#if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
bellard3f337312003-08-20 23:02:09 +0000632 if (env->hflags & HF_SOFTMMU_MASK) {
633 env->hflags &= ~HF_SOFTMMU_MASK;
bellard4cbf74b2003-08-10 21:48:43 +0000634 /* do not allow linking to another block */
635 T0 = 0;
636 }
637#endif
bellard3fb2ded2003-06-24 13:22:59 +0000638 }
639 } else {
bellard0d1a29f2004-10-12 22:01:28 +0000640 env_to_regs();
bellard7d132992003-03-06 23:23:54 +0000641 }
bellard3fb2ded2003-06-24 13:22:59 +0000642 } /* for(;;) */
643
bellard7d132992003-03-06 23:23:54 +0000644
bellarde4533c72003-06-15 19:51:39 +0000645#if defined(TARGET_I386)
bellard97eb5b12004-02-25 23:19:55 +0000646#if defined(USE_CODE_COPY)
647 if (env->native_fp_regs) {
648 save_native_fp_state(env);
649 }
650#endif
bellard9de5e442003-03-23 16:49:39 +0000651 /* restore flags in standard format */
bellardfc2b4c42003-03-29 16:52:44 +0000652 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard9de5e442003-03-23 16:49:39 +0000653
bellard7d132992003-03-06 23:23:54 +0000654 /* restore global registers */
bellard04369ff2003-03-20 22:33:23 +0000655#ifdef reg_EAX
656 EAX = saved_EAX;
657#endif
658#ifdef reg_ECX
659 ECX = saved_ECX;
660#endif
661#ifdef reg_EDX
662 EDX = saved_EDX;
663#endif
664#ifdef reg_EBX
665 EBX = saved_EBX;
666#endif
667#ifdef reg_ESP
668 ESP = saved_ESP;
669#endif
670#ifdef reg_EBP
671 EBP = saved_EBP;
672#endif
673#ifdef reg_ESI
674 ESI = saved_ESI;
675#endif
676#ifdef reg_EDI
677 EDI = saved_EDI;
678#endif
bellarde4533c72003-06-15 19:51:39 +0000679#elif defined(TARGET_ARM)
bellard1b21b622003-07-09 17:16:27 +0000680 env->cpsr = compute_cpsr();
bellardb7bcbe92005-02-22 19:27:29 +0000681 /* XXX: Save/restore host fpu exception state?. */
bellard93ac68b2003-09-30 20:57:29 +0000682#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000683#if defined(reg_REGWPTR)
684 REGWPTR = saved_regwptr;
685#endif
bellard67867302003-11-23 17:05:30 +0000686#elif defined(TARGET_PPC)
bellarde4533c72003-06-15 19:51:39 +0000687#else
688#error unsupported target CPU
689#endif
bellard8c6939c2003-06-09 15:28:00 +0000690#ifdef __sparc__
691 asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
692#endif
bellard7d132992003-03-06 23:23:54 +0000693 T0 = saved_T0;
694 T1 = saved_T1;
bellard34751872005-07-02 14:31:34 +0000695#if defined(reg_T2)
bellarde4533c72003-06-15 19:51:39 +0000696 T2 = saved_T2;
bellard34751872005-07-02 14:31:34 +0000697#endif
bellard7d132992003-03-06 23:23:54 +0000698 env = saved_env;
699 return ret;
700}
bellard6dbad632003-03-16 18:05:05 +0000701
bellardfbf9eeb2004-04-25 21:21:33 +0000702/* must only be called from the generated code as an exception can be
703 generated */
704void tb_invalidate_page_range(target_ulong start, target_ulong end)
705{
bellarddc5d0b32004-06-22 18:43:30 +0000706 /* XXX: cannot enable it yet because it yields to MMU exception
707 where NIP != read address on PowerPC */
708#if 0
bellardfbf9eeb2004-04-25 21:21:33 +0000709 target_ulong phys_addr;
710 phys_addr = get_phys_addr_code(env, start);
711 tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
bellarddc5d0b32004-06-22 18:43:30 +0000712#endif
bellardfbf9eeb2004-04-25 21:21:33 +0000713}
714
bellard1a18c712003-10-30 01:07:51 +0000715#if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
bellarde4533c72003-06-15 19:51:39 +0000716
bellard6dbad632003-03-16 18:05:05 +0000717void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
718{
719 CPUX86State *saved_env;
720
721 saved_env = env;
722 env = s;
bellarda412ac52003-07-26 18:01:40 +0000723 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
bellarda513fe12003-05-27 23:29:48 +0000724 selector &= 0xffff;
bellard2e255c62003-08-21 23:25:21 +0000725 cpu_x86_load_seg_cache(env, seg_reg, selector,
bellardc27004e2005-01-03 23:35:10 +0000726 (selector << 4), 0xffff, 0);
bellarda513fe12003-05-27 23:29:48 +0000727 } else {
bellardb453b702004-01-04 15:45:21 +0000728 load_seg(seg_reg, selector);
bellarda513fe12003-05-27 23:29:48 +0000729 }
bellard6dbad632003-03-16 18:05:05 +0000730 env = saved_env;
731}
bellard9de5e442003-03-23 16:49:39 +0000732
bellardd0a1ffc2003-05-29 20:04:28 +0000733void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
734{
735 CPUX86State *saved_env;
736
737 saved_env = env;
738 env = s;
739
bellardc27004e2005-01-03 23:35:10 +0000740 helper_fsave((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000741
742 env = saved_env;
743}
744
745void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
746{
747 CPUX86State *saved_env;
748
749 saved_env = env;
750 env = s;
751
bellardc27004e2005-01-03 23:35:10 +0000752 helper_frstor((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000753
754 env = saved_env;
755}
756
bellarde4533c72003-06-15 19:51:39 +0000757#endif /* TARGET_I386 */
758
bellard67b915a2004-03-31 23:37:16 +0000759#if !defined(CONFIG_SOFTMMU)
760
bellard3fb2ded2003-06-24 13:22:59 +0000761#if defined(TARGET_I386)
762
bellardb56dad12003-05-08 15:38:04 +0000763/* 'pc' is the host PC at which the exception was raised. 'address' is
bellardfd6ce8f2003-05-14 19:00:11 +0000764 the effective address of the memory exception. 'is_write' is 1 if a
765 write caused the exception and otherwise 0'. 'old_set' is the
766 signal set which should be restored */
bellard2b413142003-05-14 23:01:10 +0000767static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000768 int is_write, sigset_t *old_set,
769 void *puc)
bellard9de5e442003-03-23 16:49:39 +0000770{
bellarda513fe12003-05-27 23:29:48 +0000771 TranslationBlock *tb;
772 int ret;
bellard68a79312003-06-30 13:12:32 +0000773
bellard83479e72003-06-25 16:12:37 +0000774 if (cpu_single_env)
775 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellardfd6ce8f2003-05-14 19:00:11 +0000776#if defined(DEBUG_SIGNAL)
bellardbf3e8bf2004-02-16 21:58:54 +0000777 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
778 pc, address, is_write, *(unsigned long *)old_set);
bellard9de5e442003-03-23 16:49:39 +0000779#endif
bellard25eb4482003-05-14 21:50:54 +0000780 /* XXX: locking issue */
bellardfbf9eeb2004-04-25 21:21:33 +0000781 if (is_write && page_unprotect(address, pc, puc)) {
bellardfd6ce8f2003-05-14 19:00:11 +0000782 return 1;
783 }
bellardfbf9eeb2004-04-25 21:21:33 +0000784
bellard3fb2ded2003-06-24 13:22:59 +0000785 /* see if it is an MMU fault */
bellard93a40ea2003-10-27 21:13:06 +0000786 ret = cpu_x86_handle_mmu_fault(env, address, is_write,
787 ((env->hflags & HF_CPL_MASK) == 3), 0);
bellard3fb2ded2003-06-24 13:22:59 +0000788 if (ret < 0)
789 return 0; /* not an MMU fault */
790 if (ret == 0)
791 return 1; /* the MMU fault was handled without causing real CPU fault */
792 /* now we have a real cpu fault */
bellarda513fe12003-05-27 23:29:48 +0000793 tb = tb_find_pc(pc);
794 if (tb) {
bellard9de5e442003-03-23 16:49:39 +0000795 /* the PC is inside the translated code. It means that we have
796 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +0000797 cpu_restore_state(tb, env, pc, puc);
bellard3fb2ded2003-06-24 13:22:59 +0000798 }
bellard4cbf74b2003-08-10 21:48:43 +0000799 if (ret == 1) {
bellard3fb2ded2003-06-24 13:22:59 +0000800#if 0
bellard4cbf74b2003-08-10 21:48:43 +0000801 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
802 env->eip, env->cr[2], env->error_code);
bellard3fb2ded2003-06-24 13:22:59 +0000803#endif
bellard4cbf74b2003-08-10 21:48:43 +0000804 /* we restore the process signal mask as the sigreturn should
805 do it (XXX: use sigsetjmp) */
806 sigprocmask(SIG_SETMASK, old_set, NULL);
807 raise_exception_err(EXCP0E_PAGE, env->error_code);
808 } else {
809 /* activate soft MMU for this block */
bellard3f337312003-08-20 23:02:09 +0000810 env->hflags |= HF_SOFTMMU_MASK;
bellardfbf9eeb2004-04-25 21:21:33 +0000811 cpu_resume_from_signal(env, puc);
bellard4cbf74b2003-08-10 21:48:43 +0000812 }
bellard3fb2ded2003-06-24 13:22:59 +0000813 /* never comes here */
814 return 1;
815}
816
bellarde4533c72003-06-15 19:51:39 +0000817#elif defined(TARGET_ARM)
bellard3fb2ded2003-06-24 13:22:59 +0000818static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000819 int is_write, sigset_t *old_set,
820 void *puc)
bellard3fb2ded2003-06-24 13:22:59 +0000821{
bellard68016c62005-02-07 23:12:27 +0000822 TranslationBlock *tb;
823 int ret;
824
825 if (cpu_single_env)
826 env = cpu_single_env; /* XXX: find a correct solution for multithread */
827#if defined(DEBUG_SIGNAL)
828 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
829 pc, address, is_write, *(unsigned long *)old_set);
830#endif
bellard9f0777e2005-02-02 20:42:01 +0000831 /* XXX: locking issue */
832 if (is_write && page_unprotect(address, pc, puc)) {
833 return 1;
834 }
bellard68016c62005-02-07 23:12:27 +0000835 /* see if it is an MMU fault */
836 ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
837 if (ret < 0)
838 return 0; /* not an MMU fault */
839 if (ret == 0)
840 return 1; /* the MMU fault was handled without causing real CPU fault */
841 /* now we have a real cpu fault */
842 tb = tb_find_pc(pc);
843 if (tb) {
844 /* the PC is inside the translated code. It means that we have
845 a virtual CPU fault */
846 cpu_restore_state(tb, env, pc, puc);
847 }
848 /* we restore the process signal mask as the sigreturn should
849 do it (XXX: use sigsetjmp) */
850 sigprocmask(SIG_SETMASK, old_set, NULL);
851 cpu_loop_exit();
bellard3fb2ded2003-06-24 13:22:59 +0000852}
bellard93ac68b2003-09-30 20:57:29 +0000853#elif defined(TARGET_SPARC)
854static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000855 int is_write, sigset_t *old_set,
856 void *puc)
bellard93ac68b2003-09-30 20:57:29 +0000857{
bellard68016c62005-02-07 23:12:27 +0000858 TranslationBlock *tb;
859 int ret;
860
861 if (cpu_single_env)
862 env = cpu_single_env; /* XXX: find a correct solution for multithread */
863#if defined(DEBUG_SIGNAL)
864 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
865 pc, address, is_write, *(unsigned long *)old_set);
866#endif
bellardb453b702004-01-04 15:45:21 +0000867 /* XXX: locking issue */
bellardfbf9eeb2004-04-25 21:21:33 +0000868 if (is_write && page_unprotect(address, pc, puc)) {
bellardb453b702004-01-04 15:45:21 +0000869 return 1;
870 }
bellard68016c62005-02-07 23:12:27 +0000871 /* see if it is an MMU fault */
872 ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
873 if (ret < 0)
874 return 0; /* not an MMU fault */
875 if (ret == 0)
876 return 1; /* the MMU fault was handled without causing real CPU fault */
877 /* now we have a real cpu fault */
878 tb = tb_find_pc(pc);
879 if (tb) {
880 /* the PC is inside the translated code. It means that we have
881 a virtual CPU fault */
882 cpu_restore_state(tb, env, pc, puc);
883 }
884 /* we restore the process signal mask as the sigreturn should
885 do it (XXX: use sigsetjmp) */
886 sigprocmask(SIG_SETMASK, old_set, NULL);
887 cpu_loop_exit();
bellard93ac68b2003-09-30 20:57:29 +0000888}
bellard67867302003-11-23 17:05:30 +0000889#elif defined (TARGET_PPC)
890static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000891 int is_write, sigset_t *old_set,
892 void *puc)
bellard67867302003-11-23 17:05:30 +0000893{
894 TranslationBlock *tb;
bellardce097762004-01-04 23:53:18 +0000895 int ret;
bellard67867302003-11-23 17:05:30 +0000896
bellard67867302003-11-23 17:05:30 +0000897 if (cpu_single_env)
898 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellard67867302003-11-23 17:05:30 +0000899#if defined(DEBUG_SIGNAL)
900 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
901 pc, address, is_write, *(unsigned long *)old_set);
902#endif
903 /* XXX: locking issue */
bellardfbf9eeb2004-04-25 21:21:33 +0000904 if (is_write && page_unprotect(address, pc, puc)) {
bellard67867302003-11-23 17:05:30 +0000905 return 1;
906 }
907
bellardce097762004-01-04 23:53:18 +0000908 /* see if it is an MMU fault */
bellard7f957d22004-01-18 23:19:48 +0000909 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
bellardce097762004-01-04 23:53:18 +0000910 if (ret < 0)
911 return 0; /* not an MMU fault */
912 if (ret == 0)
913 return 1; /* the MMU fault was handled without causing real CPU fault */
914
bellard67867302003-11-23 17:05:30 +0000915 /* now we have a real cpu fault */
916 tb = tb_find_pc(pc);
917 if (tb) {
918 /* the PC is inside the translated code. It means that we have
919 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +0000920 cpu_restore_state(tb, env, pc, puc);
bellard67867302003-11-23 17:05:30 +0000921 }
bellardce097762004-01-04 23:53:18 +0000922 if (ret == 1) {
bellard67867302003-11-23 17:05:30 +0000923#if 0
bellardce097762004-01-04 23:53:18 +0000924 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
925 env->nip, env->error_code, tb);
bellard67867302003-11-23 17:05:30 +0000926#endif
927 /* we restore the process signal mask as the sigreturn should
928 do it (XXX: use sigsetjmp) */
bellardbf3e8bf2004-02-16 21:58:54 +0000929 sigprocmask(SIG_SETMASK, old_set, NULL);
bellard9fddaa02004-05-21 12:59:32 +0000930 do_raise_exception_err(env->exception_index, env->error_code);
bellardce097762004-01-04 23:53:18 +0000931 } else {
932 /* activate soft MMU for this block */
bellardfbf9eeb2004-04-25 21:21:33 +0000933 cpu_resume_from_signal(env, puc);
bellardce097762004-01-04 23:53:18 +0000934 }
bellard67867302003-11-23 17:05:30 +0000935 /* never comes here */
936 return 1;
937}
bellarde4533c72003-06-15 19:51:39 +0000938#else
939#error unsupported target CPU
940#endif
bellard9de5e442003-03-23 16:49:39 +0000941
bellard2b413142003-05-14 23:01:10 +0000942#if defined(__i386__)
943
bellardbf3e8bf2004-02-16 21:58:54 +0000944#if defined(USE_CODE_COPY)
945static void cpu_send_trap(unsigned long pc, int trap,
946 struct ucontext *uc)
947{
948 TranslationBlock *tb;
949
950 if (cpu_single_env)
951 env = cpu_single_env; /* XXX: find a correct solution for multithread */
952 /* now we have a real cpu fault */
953 tb = tb_find_pc(pc);
954 if (tb) {
955 /* the PC is inside the translated code. It means that we have
956 a virtual CPU fault */
957 cpu_restore_state(tb, env, pc, uc);
958 }
959 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
960 raise_exception_err(trap, env->error_code);
961}
962#endif
963
bellarde4533c72003-06-15 19:51:39 +0000964int cpu_signal_handler(int host_signum, struct siginfo *info,
965 void *puc)
bellard9de5e442003-03-23 16:49:39 +0000966{
bellard9de5e442003-03-23 16:49:39 +0000967 struct ucontext *uc = puc;
968 unsigned long pc;
bellardbf3e8bf2004-02-16 21:58:54 +0000969 int trapno;
bellard97eb5b12004-02-25 23:19:55 +0000970
bellardd691f662003-03-24 21:58:34 +0000971#ifndef REG_EIP
972/* for glibc 2.1 */
bellardfd6ce8f2003-05-14 19:00:11 +0000973#define REG_EIP EIP
974#define REG_ERR ERR
975#define REG_TRAPNO TRAPNO
bellardd691f662003-03-24 21:58:34 +0000976#endif
bellardfc2b4c42003-03-29 16:52:44 +0000977 pc = uc->uc_mcontext.gregs[REG_EIP];
bellardbf3e8bf2004-02-16 21:58:54 +0000978 trapno = uc->uc_mcontext.gregs[REG_TRAPNO];
979#if defined(TARGET_I386) && defined(USE_CODE_COPY)
980 if (trapno == 0x00 || trapno == 0x05) {
981 /* send division by zero or bound exception */
982 cpu_send_trap(pc, trapno, uc);
983 return 1;
984 } else
985#endif
986 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
987 trapno == 0xe ?
988 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
989 &uc->uc_sigmask, puc);
bellard2b413142003-05-14 23:01:10 +0000990}
991
bellardbc51c5c2004-03-17 23:46:04 +0000992#elif defined(__x86_64__)
993
994int cpu_signal_handler(int host_signum, struct siginfo *info,
995 void *puc)
996{
997 struct ucontext *uc = puc;
998 unsigned long pc;
999
1000 pc = uc->uc_mcontext.gregs[REG_RIP];
1001 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1002 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
1003 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1004 &uc->uc_sigmask, puc);
1005}
1006
bellard83fb7ad2004-07-05 21:25:26 +00001007#elif defined(__powerpc__)
bellard2b413142003-05-14 23:01:10 +00001008
bellard83fb7ad2004-07-05 21:25:26 +00001009/***********************************************************************
1010 * signal context platform-specific definitions
1011 * From Wine
1012 */
1013#ifdef linux
1014/* All Registers access - only for local access */
1015# define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
1016/* Gpr Registers access */
1017# define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
1018# define IAR_sig(context) REG_sig(nip, context) /* Program counter */
1019# define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
1020# define CTR_sig(context) REG_sig(ctr, context) /* Count register */
1021# define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
1022# define LR_sig(context) REG_sig(link, context) /* Link register */
1023# define CR_sig(context) REG_sig(ccr, context) /* Condition register */
1024/* Float Registers access */
1025# define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1026# define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1027/* Exception Registers access */
1028# define DAR_sig(context) REG_sig(dar, context)
1029# define DSISR_sig(context) REG_sig(dsisr, context)
1030# define TRAP_sig(context) REG_sig(trap, context)
1031#endif /* linux */
1032
1033#ifdef __APPLE__
1034# include <sys/ucontext.h>
1035typedef struct ucontext SIGCONTEXT;
1036/* All Registers access - only for local access */
1037# define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
1038# define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
1039# define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
1040# define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
1041/* Gpr Registers access */
1042# define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
1043# define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
1044# define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
1045# define CTR_sig(context) REG_sig(ctr, context)
1046# define XER_sig(context) REG_sig(xer, context) /* Link register */
1047# define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
1048# define CR_sig(context) REG_sig(cr, context) /* Condition register */
1049/* Float Registers access */
1050# define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
1051# define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
1052/* Exception Registers access */
1053# define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
1054# define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
1055# define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1056#endif /* __APPLE__ */
1057
bellardd1d9f422004-07-14 17:20:55 +00001058int cpu_signal_handler(int host_signum, struct siginfo *info,
bellarde4533c72003-06-15 19:51:39 +00001059 void *puc)
bellard2b413142003-05-14 23:01:10 +00001060{
bellard25eb4482003-05-14 21:50:54 +00001061 struct ucontext *uc = puc;
bellard25eb4482003-05-14 21:50:54 +00001062 unsigned long pc;
bellard25eb4482003-05-14 21:50:54 +00001063 int is_write;
1064
bellard83fb7ad2004-07-05 21:25:26 +00001065 pc = IAR_sig(uc);
bellard25eb4482003-05-14 21:50:54 +00001066 is_write = 0;
1067#if 0
1068 /* ppc 4xx case */
bellard83fb7ad2004-07-05 21:25:26 +00001069 if (DSISR_sig(uc) & 0x00800000)
bellard25eb4482003-05-14 21:50:54 +00001070 is_write = 1;
bellard9de5e442003-03-23 16:49:39 +00001071#else
bellard83fb7ad2004-07-05 21:25:26 +00001072 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
bellard25eb4482003-05-14 21:50:54 +00001073 is_write = 1;
1074#endif
1075 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001076 is_write, &uc->uc_sigmask, puc);
bellard9de5e442003-03-23 16:49:39 +00001077}
bellard2b413142003-05-14 23:01:10 +00001078
bellard2f87c602003-06-02 20:38:09 +00001079#elif defined(__alpha__)
1080
bellarde4533c72003-06-15 19:51:39 +00001081int cpu_signal_handler(int host_signum, struct siginfo *info,
bellard2f87c602003-06-02 20:38:09 +00001082 void *puc)
1083{
1084 struct ucontext *uc = puc;
1085 uint32_t *pc = uc->uc_mcontext.sc_pc;
1086 uint32_t insn = *pc;
1087 int is_write = 0;
1088
bellard8c6939c2003-06-09 15:28:00 +00001089 /* XXX: need kernel patch to get write flag faster */
bellard2f87c602003-06-02 20:38:09 +00001090 switch (insn >> 26) {
1091 case 0x0d: // stw
1092 case 0x0e: // stb
1093 case 0x0f: // stq_u
1094 case 0x24: // stf
1095 case 0x25: // stg
1096 case 0x26: // sts
1097 case 0x27: // stt
1098 case 0x2c: // stl
1099 case 0x2d: // stq
1100 case 0x2e: // stl_c
1101 case 0x2f: // stq_c
1102 is_write = 1;
1103 }
1104
1105 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001106 is_write, &uc->uc_sigmask, puc);
bellard2f87c602003-06-02 20:38:09 +00001107}
bellard8c6939c2003-06-09 15:28:00 +00001108#elif defined(__sparc__)
1109
bellarde4533c72003-06-15 19:51:39 +00001110int cpu_signal_handler(int host_signum, struct siginfo *info,
1111 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001112{
1113 uint32_t *regs = (uint32_t *)(info + 1);
1114 void *sigmask = (regs + 20);
1115 unsigned long pc;
1116 int is_write;
1117 uint32_t insn;
1118
1119 /* XXX: is there a standard glibc define ? */
1120 pc = regs[1];
1121 /* XXX: need kernel patch to get write flag faster */
1122 is_write = 0;
1123 insn = *(uint32_t *)pc;
1124 if ((insn >> 30) == 3) {
1125 switch((insn >> 19) & 0x3f) {
1126 case 0x05: // stb
1127 case 0x06: // sth
1128 case 0x04: // st
1129 case 0x07: // std
1130 case 0x24: // stf
1131 case 0x27: // stdf
1132 case 0x25: // stfsr
1133 is_write = 1;
1134 break;
1135 }
1136 }
1137 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001138 is_write, sigmask, NULL);
bellard8c6939c2003-06-09 15:28:00 +00001139}
1140
1141#elif defined(__arm__)
1142
bellarde4533c72003-06-15 19:51:39 +00001143int cpu_signal_handler(int host_signum, struct siginfo *info,
1144 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001145{
1146 struct ucontext *uc = puc;
1147 unsigned long pc;
1148 int is_write;
1149
1150 pc = uc->uc_mcontext.gregs[R15];
1151 /* XXX: compute is_write */
1152 is_write = 0;
1153 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1154 is_write,
1155 &uc->uc_sigmask);
1156}
1157
bellard38e584a2003-08-10 22:14:22 +00001158#elif defined(__mc68000)
1159
1160int cpu_signal_handler(int host_signum, struct siginfo *info,
1161 void *puc)
1162{
1163 struct ucontext *uc = puc;
1164 unsigned long pc;
1165 int is_write;
1166
1167 pc = uc->uc_mcontext.gregs[16];
1168 /* XXX: compute is_write */
1169 is_write = 0;
1170 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1171 is_write,
bellardbf3e8bf2004-02-16 21:58:54 +00001172 &uc->uc_sigmask, puc);
bellard38e584a2003-08-10 22:14:22 +00001173}
1174
bellardb8076a72005-04-07 22:20:31 +00001175#elif defined(__ia64)
1176
1177#ifndef __ISR_VALID
1178 /* This ought to be in <bits/siginfo.h>... */
1179# define __ISR_VALID 1
1180# define si_flags _sifields._sigfault._si_pad0
1181#endif
1182
1183int cpu_signal_handler(int host_signum, struct siginfo *info, void *puc)
1184{
1185 struct ucontext *uc = puc;
1186 unsigned long ip;
1187 int is_write = 0;
1188
1189 ip = uc->uc_mcontext.sc_ip;
1190 switch (host_signum) {
1191 case SIGILL:
1192 case SIGFPE:
1193 case SIGSEGV:
1194 case SIGBUS:
1195 case SIGTRAP:
1196 if (info->si_code && (info->si_flags & __ISR_VALID))
1197 /* ISR.W (write-access) is bit 33: */
1198 is_write = (info->si_isr >> 33) & 1;
1199 break;
1200
1201 default:
1202 break;
1203 }
1204 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1205 is_write,
1206 &uc->uc_sigmask, puc);
1207}
1208
bellard2b413142003-05-14 23:01:10 +00001209#else
1210
bellard3fb2ded2003-06-24 13:22:59 +00001211#error host CPU specific signal handler needed
bellard2b413142003-05-14 23:01:10 +00001212
1213#endif
bellard67b915a2004-03-31 23:37:16 +00001214
1215#endif /* !defined(CONFIG_SOFTMMU) */