blob: bdfc8655064009277c15a296c7ce0d210812131e [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)
bellard6af0bf92005-07-02 14:58:51 +0000185#elif defined(TARGET_MIPS)
bellarde4533c72003-06-15 19:51:39 +0000186#else
187#error unsupported target CPU
188#endif
bellard3fb2ded2003-06-24 13:22:59 +0000189 env->exception_index = -1;
bellard9d27abd2003-05-10 13:13:54 +0000190
bellard7d132992003-03-06 23:23:54 +0000191 /* prepare setjmp context for exception handling */
bellard3fb2ded2003-06-24 13:22:59 +0000192 for(;;) {
193 if (setjmp(env->jmp_env) == 0) {
bellardee8b7022004-02-03 23:35:10 +0000194 env->current_tb = NULL;
bellard3fb2ded2003-06-24 13:22:59 +0000195 /* if an exception is pending, we execute it here */
196 if (env->exception_index >= 0) {
197 if (env->exception_index >= EXCP_INTERRUPT) {
198 /* exit request from the cpu execution loop */
199 ret = env->exception_index;
200 break;
201 } else if (env->user_mode_only) {
202 /* if user mode only, we simulate a fake exception
203 which will be hanlded outside the cpu execution
204 loop */
bellard83479e72003-06-25 16:12:37 +0000205#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000206 do_interrupt_user(env->exception_index,
207 env->exception_is_int,
208 env->error_code,
209 env->exception_next_eip);
bellard83479e72003-06-25 16:12:37 +0000210#endif
bellard3fb2ded2003-06-24 13:22:59 +0000211 ret = env->exception_index;
212 break;
213 } else {
bellard83479e72003-06-25 16:12:37 +0000214#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000215 /* simulate a real cpu exception. On i386, it can
216 trigger new exceptions, but we do not handle
217 double or triple faults yet. */
218 do_interrupt(env->exception_index,
219 env->exception_is_int,
220 env->error_code,
bellardd05e66d2003-08-20 21:34:35 +0000221 env->exception_next_eip, 0);
bellardce097762004-01-04 23:53:18 +0000222#elif defined(TARGET_PPC)
223 do_interrupt(env);
bellard6af0bf92005-07-02 14:58:51 +0000224#elif defined(TARGET_MIPS)
225 do_interrupt(env);
bellarde95c8d52004-09-30 22:22:08 +0000226#elif defined(TARGET_SPARC)
bellard1a0c3292005-02-13 19:02:07 +0000227 do_interrupt(env->exception_index);
bellard83479e72003-06-25 16:12:37 +0000228#endif
bellard3fb2ded2003-06-24 13:22:59 +0000229 }
230 env->exception_index = -1;
bellard9df217a2005-02-10 22:05:51 +0000231 }
232#ifdef USE_KQEMU
233 if (kqemu_is_ok(env) && env->interrupt_request == 0) {
234 int ret;
235 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
236 ret = kqemu_cpu_exec(env);
237 /* put eflags in CPU temporary format */
238 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
239 DF = 1 - (2 * ((env->eflags >> 10) & 1));
240 CC_OP = CC_OP_EFLAGS;
241 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
242 if (ret == 1) {
243 /* exception */
244 longjmp(env->jmp_env, 1);
245 } else if (ret == 2) {
246 /* softmmu execution needed */
247 } else {
248 if (env->interrupt_request != 0) {
249 /* hardware interrupt will be executed just after */
250 } else {
251 /* otherwise, we restart */
252 longjmp(env->jmp_env, 1);
253 }
254 }
bellard9de5e442003-03-23 16:49:39 +0000255 }
bellard9df217a2005-02-10 22:05:51 +0000256#endif
257
bellard3fb2ded2003-06-24 13:22:59 +0000258 T0 = 0; /* force lookup of first TB */
259 for(;;) {
260#ifdef __sparc__
261 /* g1 can be modified by some libc? functions */
262 tmp_T0 = T0;
263#endif
bellard68a79312003-06-30 13:12:32 +0000264 interrupt_request = env->interrupt_request;
bellard2e255c62003-08-21 23:25:21 +0000265 if (__builtin_expect(interrupt_request, 0)) {
bellard68a79312003-06-30 13:12:32 +0000266#if defined(TARGET_I386)
267 /* if hardware interrupt pending, we execute it */
268 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
bellard3f337312003-08-20 23:02:09 +0000269 (env->eflags & IF_MASK) &&
270 !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
bellard68a79312003-06-30 13:12:32 +0000271 int intno;
bellardfbf9eeb2004-04-25 21:21:33 +0000272 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellarda541f292004-04-12 20:39:29 +0000273 intno = cpu_get_pic_interrupt(env);
bellardf193c792004-03-21 17:06:25 +0000274 if (loglevel & CPU_LOG_TB_IN_ASM) {
bellard68a79312003-06-30 13:12:32 +0000275 fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
276 }
bellardd05e66d2003-08-20 21:34:35 +0000277 do_interrupt(intno, 0, 0, 0, 1);
bellard907a5b22003-06-30 23:18:22 +0000278 /* ensure that no TB jump will be modified as
279 the program flow was changed */
280#ifdef __sparc__
281 tmp_T0 = 0;
282#else
283 T0 = 0;
284#endif
bellard68a79312003-06-30 13:12:32 +0000285 }
bellardce097762004-01-04 23:53:18 +0000286#elif defined(TARGET_PPC)
bellard9fddaa02004-05-21 12:59:32 +0000287#if 0
288 if ((interrupt_request & CPU_INTERRUPT_RESET)) {
289 cpu_ppc_reset(env);
290 }
291#endif
292 if (msr_ee != 0) {
bellardce097762004-01-04 23:53:18 +0000293 if ((interrupt_request & CPU_INTERRUPT_HARD)) {
bellard9fddaa02004-05-21 12:59:32 +0000294 /* Raise it */
295 env->exception_index = EXCP_EXTERNAL;
296 env->error_code = 0;
bellardce097762004-01-04 23:53:18 +0000297 do_interrupt(env);
298 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellard9fddaa02004-05-21 12:59:32 +0000299 } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) {
300 /* Raise it */
301 env->exception_index = EXCP_DECR;
302 env->error_code = 0;
303 do_interrupt(env);
304 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
305 }
bellardce097762004-01-04 23:53:18 +0000306 }
bellard6af0bf92005-07-02 14:58:51 +0000307#elif defined(TARGET_MIPS)
308 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
309 (env->CP0_Status & (1 << CP0St_IE)) &&
bellard7ebab692005-08-21 09:43:38 +0000310 (env->CP0_Status & env->CP0_Cause & 0x0000FF00) &&
bellard6af0bf92005-07-02 14:58:51 +0000311 !(env->hflags & MIPS_HFLAG_EXL) &&
312 !(env->hflags & MIPS_HFLAG_ERL) &&
313 !(env->hflags & MIPS_HFLAG_DM)) {
314 /* Raise it */
315 env->exception_index = EXCP_EXT_INTERRUPT;
316 env->error_code = 0;
317 do_interrupt(env);
318 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
319 }
bellarde95c8d52004-09-30 22:22:08 +0000320#elif defined(TARGET_SPARC)
bellard66321a12005-04-06 20:47:48 +0000321 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
322 (env->psret != 0)) {
323 int pil = env->interrupt_index & 15;
324 int type = env->interrupt_index & 0xf0;
325
326 if (((type == TT_EXTINT) &&
327 (pil == 15 || pil > env->psrpil)) ||
328 type != TT_EXTINT) {
329 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
330 do_interrupt(env->interrupt_index);
331 env->interrupt_index = 0;
332 }
bellarde95c8d52004-09-30 22:22:08 +0000333 } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
334 //do_interrupt(0, 0, 0, 0, 0);
335 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
336 }
bellard68a79312003-06-30 13:12:32 +0000337#endif
bellardbf3e8bf2004-02-16 21:58:54 +0000338 if (interrupt_request & CPU_INTERRUPT_EXITTB) {
339 env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
340 /* ensure that no TB jump will be modified as
341 the program flow was changed */
342#ifdef __sparc__
343 tmp_T0 = 0;
344#else
345 T0 = 0;
346#endif
347 }
bellard68a79312003-06-30 13:12:32 +0000348 if (interrupt_request & CPU_INTERRUPT_EXIT) {
349 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
350 env->exception_index = EXCP_INTERRUPT;
351 cpu_loop_exit();
352 }
bellard3fb2ded2003-06-24 13:22:59 +0000353 }
354#ifdef DEBUG_EXEC
bellardc27004e2005-01-03 23:35:10 +0000355 if ((loglevel & CPU_LOG_EXEC)) {
bellard3fb2ded2003-06-24 13:22:59 +0000356#if defined(TARGET_I386)
357 /* restore flags in standard format */
bellardfc9f7152005-04-26 19:33:35 +0000358#ifdef reg_EAX
bellard3fb2ded2003-06-24 13:22:59 +0000359 env->regs[R_EAX] = EAX;
bellardfc9f7152005-04-26 19:33:35 +0000360#endif
361#ifdef reg_EBX
bellard3fb2ded2003-06-24 13:22:59 +0000362 env->regs[R_EBX] = EBX;
bellardfc9f7152005-04-26 19:33:35 +0000363#endif
364#ifdef reg_ECX
bellard3fb2ded2003-06-24 13:22:59 +0000365 env->regs[R_ECX] = ECX;
bellardfc9f7152005-04-26 19:33:35 +0000366#endif
367#ifdef reg_EDX
bellard3fb2ded2003-06-24 13:22:59 +0000368 env->regs[R_EDX] = EDX;
bellardfc9f7152005-04-26 19:33:35 +0000369#endif
370#ifdef reg_ESI
bellard3fb2ded2003-06-24 13:22:59 +0000371 env->regs[R_ESI] = ESI;
bellardfc9f7152005-04-26 19:33:35 +0000372#endif
373#ifdef reg_EDI
bellard3fb2ded2003-06-24 13:22:59 +0000374 env->regs[R_EDI] = EDI;
bellardfc9f7152005-04-26 19:33:35 +0000375#endif
376#ifdef reg_EBP
bellard3fb2ded2003-06-24 13:22:59 +0000377 env->regs[R_EBP] = EBP;
bellardfc9f7152005-04-26 19:33:35 +0000378#endif
379#ifdef reg_ESP
bellard3fb2ded2003-06-24 13:22:59 +0000380 env->regs[R_ESP] = ESP;
bellardfc9f7152005-04-26 19:33:35 +0000381#endif
bellard3fb2ded2003-06-24 13:22:59 +0000382 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard7fe48482004-10-09 18:08:01 +0000383 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
bellard3fb2ded2003-06-24 13:22:59 +0000384 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000385#elif defined(TARGET_ARM)
bellard1b21b622003-07-09 17:16:27 +0000386 env->cpsr = compute_cpsr();
bellard7fe48482004-10-09 18:08:01 +0000387 cpu_dump_state(env, logfile, fprintf, 0);
bellard99c475a2005-01-31 20:45:13 +0000388 env->cpsr &= ~CACHED_CPSR_BITS;
bellard93ac68b2003-09-30 20:57:29 +0000389#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000390 REGWPTR = env->regbase + (env->cwp * 16);
391 env->regwptr = REGWPTR;
392 cpu_dump_state(env, logfile, fprintf, 0);
bellard67867302003-11-23 17:05:30 +0000393#elif defined(TARGET_PPC)
bellard7fe48482004-10-09 18:08:01 +0000394 cpu_dump_state(env, logfile, fprintf, 0);
bellard6af0bf92005-07-02 14:58:51 +0000395#elif defined(TARGET_MIPS)
396 cpu_dump_state(env, logfile, fprintf, 0);
bellarde4533c72003-06-15 19:51:39 +0000397#else
398#error unsupported target CPU
399#endif
bellard3fb2ded2003-06-24 13:22:59 +0000400 }
bellard7d132992003-03-06 23:23:54 +0000401#endif
bellard3f337312003-08-20 23:02:09 +0000402 /* we record a subset of the CPU state. It will
403 always be the same before a given translated block
404 is executed. */
bellarde4533c72003-06-15 19:51:39 +0000405#if defined(TARGET_I386)
bellard2e255c62003-08-21 23:25:21 +0000406 flags = env->hflags;
bellard3f337312003-08-20 23:02:09 +0000407 flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
bellard3fb2ded2003-06-24 13:22:59 +0000408 cs_base = env->segs[R_CS].base;
409 pc = cs_base + env->eip;
bellarde4533c72003-06-15 19:51:39 +0000410#elif defined(TARGET_ARM)
bellardb7bcbe92005-02-22 19:27:29 +0000411 flags = env->thumb | (env->vfp.vec_len << 1)
412 | (env->vfp.vec_stride << 4);
bellard3fb2ded2003-06-24 13:22:59 +0000413 cs_base = 0;
bellardc27004e2005-01-03 23:35:10 +0000414 pc = env->regs[15];
bellard93ac68b2003-09-30 20:57:29 +0000415#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000416#ifdef TARGET_SPARC64
417 flags = (env->pstate << 2) | ((env->lsu & (DMMU_E | IMMU_E)) >> 2);
418#else
419 flags = env->psrs | ((env->mmuregs[0] & (MMU_E | MMU_NF)) << 1);
420#endif
bellardc27004e2005-01-03 23:35:10 +0000421 cs_base = env->npc;
422 pc = env->pc;
bellard67867302003-11-23 17:05:30 +0000423#elif defined(TARGET_PPC)
bellard111bfab2005-04-23 18:16:07 +0000424 flags = (msr_pr << MSR_PR) | (msr_fp << MSR_FP) |
425 (msr_se << MSR_SE) | (msr_le << MSR_LE);
bellard67867302003-11-23 17:05:30 +0000426 cs_base = 0;
bellardc27004e2005-01-03 23:35:10 +0000427 pc = env->nip;
bellard6af0bf92005-07-02 14:58:51 +0000428#elif defined(TARGET_MIPS)
429 flags = env->hflags & MIPS_HFLAGS_TMASK;
430 cs_base = NULL;
431 pc = env->PC;
bellarde4533c72003-06-15 19:51:39 +0000432#else
433#error unsupported CPU
434#endif
bellardc27004e2005-01-03 23:35:10 +0000435 tb = tb_find(&ptb, pc, cs_base,
bellard3fb2ded2003-06-24 13:22:59 +0000436 flags);
bellardd4e81642003-05-25 16:46:15 +0000437 if (!tb) {
bellard13768472004-01-04 17:43:01 +0000438 TranslationBlock **ptb1;
439 unsigned int h;
440 target_ulong phys_pc, phys_page1, phys_page2, virt_page2;
441
442
bellard3fb2ded2003-06-24 13:22:59 +0000443 spin_lock(&tb_lock);
bellard13768472004-01-04 17:43:01 +0000444
445 tb_invalidated_flag = 0;
bellard0d1a29f2004-10-12 22:01:28 +0000446
447 regs_to_env(); /* XXX: do it just before cpu_gen_code() */
bellard13768472004-01-04 17:43:01 +0000448
449 /* find translated block using physical mappings */
bellardc27004e2005-01-03 23:35:10 +0000450 phys_pc = get_phys_addr_code(env, pc);
bellard13768472004-01-04 17:43:01 +0000451 phys_page1 = phys_pc & TARGET_PAGE_MASK;
452 phys_page2 = -1;
453 h = tb_phys_hash_func(phys_pc);
454 ptb1 = &tb_phys_hash[h];
455 for(;;) {
456 tb = *ptb1;
457 if (!tb)
458 goto not_found;
bellardc27004e2005-01-03 23:35:10 +0000459 if (tb->pc == pc &&
bellard13768472004-01-04 17:43:01 +0000460 tb->page_addr[0] == phys_page1 &&
bellardc27004e2005-01-03 23:35:10 +0000461 tb->cs_base == cs_base &&
bellard13768472004-01-04 17:43:01 +0000462 tb->flags == flags) {
463 /* check next page if needed */
bellardb516f852004-01-18 21:50:04 +0000464 if (tb->page_addr[1] != -1) {
bellardc27004e2005-01-03 23:35:10 +0000465 virt_page2 = (pc & TARGET_PAGE_MASK) +
bellardb516f852004-01-18 21:50:04 +0000466 TARGET_PAGE_SIZE;
bellard13768472004-01-04 17:43:01 +0000467 phys_page2 = get_phys_addr_code(env, virt_page2);
468 if (tb->page_addr[1] == phys_page2)
469 goto found;
470 } else {
471 goto found;
472 }
473 }
474 ptb1 = &tb->phys_hash_next;
475 }
476 not_found:
bellard3fb2ded2003-06-24 13:22:59 +0000477 /* if no translated code available, then translate it now */
bellardc27004e2005-01-03 23:35:10 +0000478 tb = tb_alloc(pc);
bellard3fb2ded2003-06-24 13:22:59 +0000479 if (!tb) {
480 /* flush must be done */
bellardb453b702004-01-04 15:45:21 +0000481 tb_flush(env);
bellard3fb2ded2003-06-24 13:22:59 +0000482 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000483 tb = tb_alloc(pc);
bellard3fb2ded2003-06-24 13:22:59 +0000484 /* don't forget to invalidate previous TB info */
bellardc27004e2005-01-03 23:35:10 +0000485 ptb = &tb_hash[tb_hash_func(pc)];
bellard3fb2ded2003-06-24 13:22:59 +0000486 T0 = 0;
487 }
488 tc_ptr = code_gen_ptr;
489 tb->tc_ptr = tc_ptr;
bellardc27004e2005-01-03 23:35:10 +0000490 tb->cs_base = cs_base;
bellard3fb2ded2003-06-24 13:22:59 +0000491 tb->flags = flags;
bellardfacc68b2003-09-17 22:51:18 +0000492 cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
bellard13768472004-01-04 17:43:01 +0000493 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
494
495 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000496 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellard13768472004-01-04 17:43:01 +0000497 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000498 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellard13768472004-01-04 17:43:01 +0000499 phys_page2 = get_phys_addr_code(env, virt_page2);
500 }
501 tb_link_phys(tb, phys_pc, phys_page2);
502
503 found:
bellard36bdbe52003-11-19 22:12:02 +0000504 if (tb_invalidated_flag) {
505 /* as some TB could have been invalidated because
506 of memory exceptions while generating the code, we
507 must recompute the hash index here */
bellardc27004e2005-01-03 23:35:10 +0000508 ptb = &tb_hash[tb_hash_func(pc)];
bellard36bdbe52003-11-19 22:12:02 +0000509 while (*ptb != NULL)
510 ptb = &(*ptb)->hash_next;
511 T0 = 0;
512 }
bellard13768472004-01-04 17:43:01 +0000513 /* we add the TB in the virtual pc hash table */
bellard3fb2ded2003-06-24 13:22:59 +0000514 *ptb = tb;
515 tb->hash_next = NULL;
516 tb_link(tb);
bellard3fb2ded2003-06-24 13:22:59 +0000517 spin_unlock(&tb_lock);
518 }
bellard9d27abd2003-05-10 13:13:54 +0000519#ifdef DEBUG_EXEC
bellardc1135f62005-01-30 22:41:54 +0000520 if ((loglevel & CPU_LOG_EXEC)) {
bellardc27004e2005-01-03 23:35:10 +0000521 fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
522 (long)tb->tc_ptr, tb->pc,
523 lookup_symbol(tb->pc));
bellard3fb2ded2003-06-24 13:22:59 +0000524 }
bellard9d27abd2003-05-10 13:13:54 +0000525#endif
bellard8c6939c2003-06-09 15:28:00 +0000526#ifdef __sparc__
bellard3fb2ded2003-06-24 13:22:59 +0000527 T0 = tmp_T0;
bellard8c6939c2003-06-09 15:28:00 +0000528#endif
bellardfacc68b2003-09-17 22:51:18 +0000529 /* see if we can patch the calling TB. */
bellardc27004e2005-01-03 23:35:10 +0000530 {
531 if (T0 != 0
bellardbf3e8bf2004-02-16 21:58:54 +0000532#if defined(TARGET_I386) && defined(USE_CODE_COPY)
533 && (tb->cflags & CF_CODE_COPY) ==
534 (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
535#endif
536 ) {
bellard3fb2ded2003-06-24 13:22:59 +0000537 spin_lock(&tb_lock);
bellardc27004e2005-01-03 23:35:10 +0000538 tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
bellard97eb5b12004-02-25 23:19:55 +0000539#if defined(USE_CODE_COPY)
540 /* propagates the FP use info */
541 ((TranslationBlock *)(T0 & ~3))->cflags |=
542 (tb->cflags & CF_FP_USED);
543#endif
bellard3fb2ded2003-06-24 13:22:59 +0000544 spin_unlock(&tb_lock);
545 }
bellardc27004e2005-01-03 23:35:10 +0000546 }
bellard3fb2ded2003-06-24 13:22:59 +0000547 tc_ptr = tb->tc_ptr;
bellard83479e72003-06-25 16:12:37 +0000548 env->current_tb = tb;
bellard3fb2ded2003-06-24 13:22:59 +0000549 /* execute the generated code */
550 gen_func = (void *)tc_ptr;
551#if defined(__sparc__)
552 __asm__ __volatile__("call %0\n\t"
553 "mov %%o7,%%i0"
554 : /* no outputs */
555 : "r" (gen_func)
556 : "i0", "i1", "i2", "i3", "i4", "i5");
557#elif defined(__arm__)
558 asm volatile ("mov pc, %0\n\t"
559 ".global exec_loop\n\t"
560 "exec_loop:\n\t"
561 : /* no outputs */
562 : "r" (gen_func)
563 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
bellardbf3e8bf2004-02-16 21:58:54 +0000564#elif defined(TARGET_I386) && defined(USE_CODE_COPY)
565{
566 if (!(tb->cflags & CF_CODE_COPY)) {
bellard97eb5b12004-02-25 23:19:55 +0000567 if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
568 save_native_fp_state(env);
569 }
bellardbf3e8bf2004-02-16 21:58:54 +0000570 gen_func();
571 } else {
bellard97eb5b12004-02-25 23:19:55 +0000572 if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
573 restore_native_fp_state(env);
574 }
bellardbf3e8bf2004-02-16 21:58:54 +0000575 /* we work with native eflags */
576 CC_SRC = cc_table[CC_OP].compute_all();
577 CC_OP = CC_OP_EFLAGS;
578 asm(".globl exec_loop\n"
579 "\n"
580 "debug1:\n"
581 " pushl %%ebp\n"
582 " fs movl %10, %9\n"
583 " fs movl %11, %%eax\n"
584 " andl $0x400, %%eax\n"
585 " fs orl %8, %%eax\n"
586 " pushl %%eax\n"
587 " popf\n"
588 " fs movl %%esp, %12\n"
589 " fs movl %0, %%eax\n"
590 " fs movl %1, %%ecx\n"
591 " fs movl %2, %%edx\n"
592 " fs movl %3, %%ebx\n"
593 " fs movl %4, %%esp\n"
594 " fs movl %5, %%ebp\n"
595 " fs movl %6, %%esi\n"
596 " fs movl %7, %%edi\n"
597 " fs jmp *%9\n"
598 "exec_loop:\n"
599 " fs movl %%esp, %4\n"
600 " fs movl %12, %%esp\n"
601 " fs movl %%eax, %0\n"
602 " fs movl %%ecx, %1\n"
603 " fs movl %%edx, %2\n"
604 " fs movl %%ebx, %3\n"
605 " fs movl %%ebp, %5\n"
606 " fs movl %%esi, %6\n"
607 " fs movl %%edi, %7\n"
608 " pushf\n"
609 " popl %%eax\n"
610 " movl %%eax, %%ecx\n"
611 " andl $0x400, %%ecx\n"
612 " shrl $9, %%ecx\n"
613 " andl $0x8d5, %%eax\n"
614 " fs movl %%eax, %8\n"
615 " movl $1, %%eax\n"
616 " subl %%ecx, %%eax\n"
617 " fs movl %%eax, %11\n"
618 " fs movl %9, %%ebx\n" /* get T0 value */
619 " popl %%ebp\n"
620 :
621 : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
622 "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
623 "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
624 "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
625 "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
626 "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
627 "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
628 "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
629 "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
630 "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
631 "a" (gen_func),
632 "m" (*(uint8_t *)offsetof(CPUState, df)),
633 "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
634 : "%ecx", "%edx"
635 );
636 }
637}
bellardb8076a72005-04-07 22:20:31 +0000638#elif defined(__ia64)
639 struct fptr {
640 void *ip;
641 void *gp;
642 } fp;
643
644 fp.ip = tc_ptr;
645 fp.gp = code_gen_buffer + 2 * (1 << 20);
646 (*(void (*)(void)) &fp)();
bellard3fb2ded2003-06-24 13:22:59 +0000647#else
648 gen_func();
649#endif
bellard83479e72003-06-25 16:12:37 +0000650 env->current_tb = NULL;
bellard4cbf74b2003-08-10 21:48:43 +0000651 /* reset soft MMU for next block (it can currently
652 only be set by a memory fault) */
653#if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
bellard3f337312003-08-20 23:02:09 +0000654 if (env->hflags & HF_SOFTMMU_MASK) {
655 env->hflags &= ~HF_SOFTMMU_MASK;
bellard4cbf74b2003-08-10 21:48:43 +0000656 /* do not allow linking to another block */
657 T0 = 0;
658 }
659#endif
bellard3fb2ded2003-06-24 13:22:59 +0000660 }
661 } else {
bellard0d1a29f2004-10-12 22:01:28 +0000662 env_to_regs();
bellard7d132992003-03-06 23:23:54 +0000663 }
bellard3fb2ded2003-06-24 13:22:59 +0000664 } /* for(;;) */
665
bellard7d132992003-03-06 23:23:54 +0000666
bellarde4533c72003-06-15 19:51:39 +0000667#if defined(TARGET_I386)
bellard97eb5b12004-02-25 23:19:55 +0000668#if defined(USE_CODE_COPY)
669 if (env->native_fp_regs) {
670 save_native_fp_state(env);
671 }
672#endif
bellard9de5e442003-03-23 16:49:39 +0000673 /* restore flags in standard format */
bellardfc2b4c42003-03-29 16:52:44 +0000674 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard9de5e442003-03-23 16:49:39 +0000675
bellard7d132992003-03-06 23:23:54 +0000676 /* restore global registers */
bellard04369ff2003-03-20 22:33:23 +0000677#ifdef reg_EAX
678 EAX = saved_EAX;
679#endif
680#ifdef reg_ECX
681 ECX = saved_ECX;
682#endif
683#ifdef reg_EDX
684 EDX = saved_EDX;
685#endif
686#ifdef reg_EBX
687 EBX = saved_EBX;
688#endif
689#ifdef reg_ESP
690 ESP = saved_ESP;
691#endif
692#ifdef reg_EBP
693 EBP = saved_EBP;
694#endif
695#ifdef reg_ESI
696 ESI = saved_ESI;
697#endif
698#ifdef reg_EDI
699 EDI = saved_EDI;
700#endif
bellarde4533c72003-06-15 19:51:39 +0000701#elif defined(TARGET_ARM)
bellard1b21b622003-07-09 17:16:27 +0000702 env->cpsr = compute_cpsr();
bellardb7bcbe92005-02-22 19:27:29 +0000703 /* XXX: Save/restore host fpu exception state?. */
bellard93ac68b2003-09-30 20:57:29 +0000704#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000705#if defined(reg_REGWPTR)
706 REGWPTR = saved_regwptr;
707#endif
bellard67867302003-11-23 17:05:30 +0000708#elif defined(TARGET_PPC)
bellard6af0bf92005-07-02 14:58:51 +0000709#elif defined(TARGET_MIPS)
bellarde4533c72003-06-15 19:51:39 +0000710#else
711#error unsupported target CPU
712#endif
bellard8c6939c2003-06-09 15:28:00 +0000713#ifdef __sparc__
714 asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
715#endif
bellard7d132992003-03-06 23:23:54 +0000716 T0 = saved_T0;
717 T1 = saved_T1;
bellard34751872005-07-02 14:31:34 +0000718#if defined(reg_T2)
bellarde4533c72003-06-15 19:51:39 +0000719 T2 = saved_T2;
bellard34751872005-07-02 14:31:34 +0000720#endif
bellard7d132992003-03-06 23:23:54 +0000721 env = saved_env;
722 return ret;
723}
bellard6dbad632003-03-16 18:05:05 +0000724
bellardfbf9eeb2004-04-25 21:21:33 +0000725/* must only be called from the generated code as an exception can be
726 generated */
727void tb_invalidate_page_range(target_ulong start, target_ulong end)
728{
bellarddc5d0b32004-06-22 18:43:30 +0000729 /* XXX: cannot enable it yet because it yields to MMU exception
730 where NIP != read address on PowerPC */
731#if 0
bellardfbf9eeb2004-04-25 21:21:33 +0000732 target_ulong phys_addr;
733 phys_addr = get_phys_addr_code(env, start);
734 tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
bellarddc5d0b32004-06-22 18:43:30 +0000735#endif
bellardfbf9eeb2004-04-25 21:21:33 +0000736}
737
bellard1a18c712003-10-30 01:07:51 +0000738#if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
bellarde4533c72003-06-15 19:51:39 +0000739
bellard6dbad632003-03-16 18:05:05 +0000740void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
741{
742 CPUX86State *saved_env;
743
744 saved_env = env;
745 env = s;
bellarda412ac52003-07-26 18:01:40 +0000746 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
bellarda513fe12003-05-27 23:29:48 +0000747 selector &= 0xffff;
bellard2e255c62003-08-21 23:25:21 +0000748 cpu_x86_load_seg_cache(env, seg_reg, selector,
bellardc27004e2005-01-03 23:35:10 +0000749 (selector << 4), 0xffff, 0);
bellarda513fe12003-05-27 23:29:48 +0000750 } else {
bellardb453b702004-01-04 15:45:21 +0000751 load_seg(seg_reg, selector);
bellarda513fe12003-05-27 23:29:48 +0000752 }
bellard6dbad632003-03-16 18:05:05 +0000753 env = saved_env;
754}
bellard9de5e442003-03-23 16:49:39 +0000755
bellardd0a1ffc2003-05-29 20:04:28 +0000756void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
757{
758 CPUX86State *saved_env;
759
760 saved_env = env;
761 env = s;
762
bellardc27004e2005-01-03 23:35:10 +0000763 helper_fsave((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000764
765 env = saved_env;
766}
767
768void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
769{
770 CPUX86State *saved_env;
771
772 saved_env = env;
773 env = s;
774
bellardc27004e2005-01-03 23:35:10 +0000775 helper_frstor((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000776
777 env = saved_env;
778}
779
bellarde4533c72003-06-15 19:51:39 +0000780#endif /* TARGET_I386 */
781
bellard67b915a2004-03-31 23:37:16 +0000782#if !defined(CONFIG_SOFTMMU)
783
bellard3fb2ded2003-06-24 13:22:59 +0000784#if defined(TARGET_I386)
785
bellardb56dad12003-05-08 15:38:04 +0000786/* 'pc' is the host PC at which the exception was raised. 'address' is
bellardfd6ce8f2003-05-14 19:00:11 +0000787 the effective address of the memory exception. 'is_write' is 1 if a
788 write caused the exception and otherwise 0'. 'old_set' is the
789 signal set which should be restored */
bellard2b413142003-05-14 23:01:10 +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)
bellard9de5e442003-03-23 16:49:39 +0000793{
bellarda513fe12003-05-27 23:29:48 +0000794 TranslationBlock *tb;
795 int ret;
bellard68a79312003-06-30 13:12:32 +0000796
bellard83479e72003-06-25 16:12:37 +0000797 if (cpu_single_env)
798 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellardfd6ce8f2003-05-14 19:00:11 +0000799#if defined(DEBUG_SIGNAL)
bellardbf3e8bf2004-02-16 21:58:54 +0000800 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
801 pc, address, is_write, *(unsigned long *)old_set);
bellard9de5e442003-03-23 16:49:39 +0000802#endif
bellard25eb4482003-05-14 21:50:54 +0000803 /* XXX: locking issue */
bellardfbf9eeb2004-04-25 21:21:33 +0000804 if (is_write && page_unprotect(address, pc, puc)) {
bellardfd6ce8f2003-05-14 19:00:11 +0000805 return 1;
806 }
bellardfbf9eeb2004-04-25 21:21:33 +0000807
bellard3fb2ded2003-06-24 13:22:59 +0000808 /* see if it is an MMU fault */
bellard93a40ea2003-10-27 21:13:06 +0000809 ret = cpu_x86_handle_mmu_fault(env, address, is_write,
810 ((env->hflags & HF_CPL_MASK) == 3), 0);
bellard3fb2ded2003-06-24 13:22:59 +0000811 if (ret < 0)
812 return 0; /* not an MMU fault */
813 if (ret == 0)
814 return 1; /* the MMU fault was handled without causing real CPU fault */
815 /* now we have a real cpu fault */
bellarda513fe12003-05-27 23:29:48 +0000816 tb = tb_find_pc(pc);
817 if (tb) {
bellard9de5e442003-03-23 16:49:39 +0000818 /* the PC is inside the translated code. It means that we have
819 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +0000820 cpu_restore_state(tb, env, pc, puc);
bellard3fb2ded2003-06-24 13:22:59 +0000821 }
bellard4cbf74b2003-08-10 21:48:43 +0000822 if (ret == 1) {
bellard3fb2ded2003-06-24 13:22:59 +0000823#if 0
bellard4cbf74b2003-08-10 21:48:43 +0000824 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
825 env->eip, env->cr[2], env->error_code);
bellard3fb2ded2003-06-24 13:22:59 +0000826#endif
bellard4cbf74b2003-08-10 21:48:43 +0000827 /* we restore the process signal mask as the sigreturn should
828 do it (XXX: use sigsetjmp) */
829 sigprocmask(SIG_SETMASK, old_set, NULL);
830 raise_exception_err(EXCP0E_PAGE, env->error_code);
831 } else {
832 /* activate soft MMU for this block */
bellard3f337312003-08-20 23:02:09 +0000833 env->hflags |= HF_SOFTMMU_MASK;
bellardfbf9eeb2004-04-25 21:21:33 +0000834 cpu_resume_from_signal(env, puc);
bellard4cbf74b2003-08-10 21:48:43 +0000835 }
bellard3fb2ded2003-06-24 13:22:59 +0000836 /* never comes here */
837 return 1;
838}
839
bellarde4533c72003-06-15 19:51:39 +0000840#elif defined(TARGET_ARM)
bellard3fb2ded2003-06-24 13:22:59 +0000841static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000842 int is_write, sigset_t *old_set,
843 void *puc)
bellard3fb2ded2003-06-24 13:22:59 +0000844{
bellard68016c62005-02-07 23:12:27 +0000845 TranslationBlock *tb;
846 int ret;
847
848 if (cpu_single_env)
849 env = cpu_single_env; /* XXX: find a correct solution for multithread */
850#if defined(DEBUG_SIGNAL)
851 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
852 pc, address, is_write, *(unsigned long *)old_set);
853#endif
bellard9f0777e2005-02-02 20:42:01 +0000854 /* XXX: locking issue */
855 if (is_write && page_unprotect(address, pc, puc)) {
856 return 1;
857 }
bellard68016c62005-02-07 23:12:27 +0000858 /* see if it is an MMU fault */
859 ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
860 if (ret < 0)
861 return 0; /* not an MMU fault */
862 if (ret == 0)
863 return 1; /* the MMU fault was handled without causing real CPU fault */
864 /* now we have a real cpu fault */
865 tb = tb_find_pc(pc);
866 if (tb) {
867 /* the PC is inside the translated code. It means that we have
868 a virtual CPU fault */
869 cpu_restore_state(tb, env, pc, puc);
870 }
871 /* we restore the process signal mask as the sigreturn should
872 do it (XXX: use sigsetjmp) */
873 sigprocmask(SIG_SETMASK, old_set, NULL);
874 cpu_loop_exit();
bellard3fb2ded2003-06-24 13:22:59 +0000875}
bellard93ac68b2003-09-30 20:57:29 +0000876#elif defined(TARGET_SPARC)
877static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000878 int is_write, sigset_t *old_set,
879 void *puc)
bellard93ac68b2003-09-30 20:57:29 +0000880{
bellard68016c62005-02-07 23:12:27 +0000881 TranslationBlock *tb;
882 int ret;
883
884 if (cpu_single_env)
885 env = cpu_single_env; /* XXX: find a correct solution for multithread */
886#if defined(DEBUG_SIGNAL)
887 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
888 pc, address, is_write, *(unsigned long *)old_set);
889#endif
bellardb453b702004-01-04 15:45:21 +0000890 /* XXX: locking issue */
bellardfbf9eeb2004-04-25 21:21:33 +0000891 if (is_write && page_unprotect(address, pc, puc)) {
bellardb453b702004-01-04 15:45:21 +0000892 return 1;
893 }
bellard68016c62005-02-07 23:12:27 +0000894 /* see if it is an MMU fault */
895 ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
896 if (ret < 0)
897 return 0; /* not an MMU fault */
898 if (ret == 0)
899 return 1; /* the MMU fault was handled without causing real CPU fault */
900 /* now we have a real cpu fault */
901 tb = tb_find_pc(pc);
902 if (tb) {
903 /* the PC is inside the translated code. It means that we have
904 a virtual CPU fault */
905 cpu_restore_state(tb, env, pc, puc);
906 }
907 /* we restore the process signal mask as the sigreturn should
908 do it (XXX: use sigsetjmp) */
909 sigprocmask(SIG_SETMASK, old_set, NULL);
910 cpu_loop_exit();
bellard93ac68b2003-09-30 20:57:29 +0000911}
bellard67867302003-11-23 17:05:30 +0000912#elif defined (TARGET_PPC)
913static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000914 int is_write, sigset_t *old_set,
915 void *puc)
bellard67867302003-11-23 17:05:30 +0000916{
917 TranslationBlock *tb;
bellardce097762004-01-04 23:53:18 +0000918 int ret;
bellard67867302003-11-23 17:05:30 +0000919
bellard67867302003-11-23 17:05:30 +0000920 if (cpu_single_env)
921 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellard67867302003-11-23 17:05:30 +0000922#if defined(DEBUG_SIGNAL)
923 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
924 pc, address, is_write, *(unsigned long *)old_set);
925#endif
926 /* XXX: locking issue */
bellardfbf9eeb2004-04-25 21:21:33 +0000927 if (is_write && page_unprotect(address, pc, puc)) {
bellard67867302003-11-23 17:05:30 +0000928 return 1;
929 }
930
bellardce097762004-01-04 23:53:18 +0000931 /* see if it is an MMU fault */
bellard7f957d22004-01-18 23:19:48 +0000932 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
bellardce097762004-01-04 23:53:18 +0000933 if (ret < 0)
934 return 0; /* not an MMU fault */
935 if (ret == 0)
936 return 1; /* the MMU fault was handled without causing real CPU fault */
937
bellard67867302003-11-23 17:05:30 +0000938 /* now we have a real cpu fault */
939 tb = tb_find_pc(pc);
940 if (tb) {
941 /* the PC is inside the translated code. It means that we have
942 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +0000943 cpu_restore_state(tb, env, pc, puc);
bellard67867302003-11-23 17:05:30 +0000944 }
bellardce097762004-01-04 23:53:18 +0000945 if (ret == 1) {
bellard67867302003-11-23 17:05:30 +0000946#if 0
bellardce097762004-01-04 23:53:18 +0000947 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
948 env->nip, env->error_code, tb);
bellard67867302003-11-23 17:05:30 +0000949#endif
950 /* we restore the process signal mask as the sigreturn should
951 do it (XXX: use sigsetjmp) */
bellardbf3e8bf2004-02-16 21:58:54 +0000952 sigprocmask(SIG_SETMASK, old_set, NULL);
bellard9fddaa02004-05-21 12:59:32 +0000953 do_raise_exception_err(env->exception_index, env->error_code);
bellardce097762004-01-04 23:53:18 +0000954 } else {
955 /* activate soft MMU for this block */
bellardfbf9eeb2004-04-25 21:21:33 +0000956 cpu_resume_from_signal(env, puc);
bellardce097762004-01-04 23:53:18 +0000957 }
bellard67867302003-11-23 17:05:30 +0000958 /* never comes here */
959 return 1;
960}
bellard6af0bf92005-07-02 14:58:51 +0000961
962#elif defined (TARGET_MIPS)
963static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
964 int is_write, sigset_t *old_set,
965 void *puc)
966{
967 TranslationBlock *tb;
968 int ret;
969
970 if (cpu_single_env)
971 env = cpu_single_env; /* XXX: find a correct solution for multithread */
972#if defined(DEBUG_SIGNAL)
973 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
974 pc, address, is_write, *(unsigned long *)old_set);
975#endif
976 /* XXX: locking issue */
977 if (is_write && page_unprotect(address, pc, puc)) {
978 return 1;
979 }
980
981 /* see if it is an MMU fault */
982 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
983 if (ret < 0)
984 return 0; /* not an MMU fault */
985 if (ret == 0)
986 return 1; /* the MMU fault was handled without causing real CPU fault */
987
988 /* now we have a real cpu fault */
989 tb = tb_find_pc(pc);
990 if (tb) {
991 /* the PC is inside the translated code. It means that we have
992 a virtual CPU fault */
993 cpu_restore_state(tb, env, pc, puc);
994 }
995 if (ret == 1) {
996#if 0
997 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
998 env->nip, env->error_code, tb);
999#endif
1000 /* we restore the process signal mask as the sigreturn should
1001 do it (XXX: use sigsetjmp) */
1002 sigprocmask(SIG_SETMASK, old_set, NULL);
1003 do_raise_exception_err(env->exception_index, env->error_code);
1004 } else {
1005 /* activate soft MMU for this block */
1006 cpu_resume_from_signal(env, puc);
1007 }
1008 /* never comes here */
1009 return 1;
1010}
1011
bellarde4533c72003-06-15 19:51:39 +00001012#else
1013#error unsupported target CPU
1014#endif
bellard9de5e442003-03-23 16:49:39 +00001015
bellard2b413142003-05-14 23:01:10 +00001016#if defined(__i386__)
1017
bellardbf3e8bf2004-02-16 21:58:54 +00001018#if defined(USE_CODE_COPY)
1019static void cpu_send_trap(unsigned long pc, int trap,
1020 struct ucontext *uc)
1021{
1022 TranslationBlock *tb;
1023
1024 if (cpu_single_env)
1025 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1026 /* now we have a real cpu fault */
1027 tb = tb_find_pc(pc);
1028 if (tb) {
1029 /* the PC is inside the translated code. It means that we have
1030 a virtual CPU fault */
1031 cpu_restore_state(tb, env, pc, uc);
1032 }
1033 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
1034 raise_exception_err(trap, env->error_code);
1035}
1036#endif
1037
bellarde4533c72003-06-15 19:51:39 +00001038int cpu_signal_handler(int host_signum, struct siginfo *info,
1039 void *puc)
bellard9de5e442003-03-23 16:49:39 +00001040{
bellard9de5e442003-03-23 16:49:39 +00001041 struct ucontext *uc = puc;
1042 unsigned long pc;
bellardbf3e8bf2004-02-16 21:58:54 +00001043 int trapno;
bellard97eb5b12004-02-25 23:19:55 +00001044
bellardd691f662003-03-24 21:58:34 +00001045#ifndef REG_EIP
1046/* for glibc 2.1 */
bellardfd6ce8f2003-05-14 19:00:11 +00001047#define REG_EIP EIP
1048#define REG_ERR ERR
1049#define REG_TRAPNO TRAPNO
bellardd691f662003-03-24 21:58:34 +00001050#endif
bellardfc2b4c42003-03-29 16:52:44 +00001051 pc = uc->uc_mcontext.gregs[REG_EIP];
bellardbf3e8bf2004-02-16 21:58:54 +00001052 trapno = uc->uc_mcontext.gregs[REG_TRAPNO];
1053#if defined(TARGET_I386) && defined(USE_CODE_COPY)
1054 if (trapno == 0x00 || trapno == 0x05) {
1055 /* send division by zero or bound exception */
1056 cpu_send_trap(pc, trapno, uc);
1057 return 1;
1058 } else
1059#endif
1060 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1061 trapno == 0xe ?
1062 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1063 &uc->uc_sigmask, puc);
bellard2b413142003-05-14 23:01:10 +00001064}
1065
bellardbc51c5c2004-03-17 23:46:04 +00001066#elif defined(__x86_64__)
1067
1068int cpu_signal_handler(int host_signum, struct siginfo *info,
1069 void *puc)
1070{
1071 struct ucontext *uc = puc;
1072 unsigned long pc;
1073
1074 pc = uc->uc_mcontext.gregs[REG_RIP];
1075 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1076 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
1077 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1078 &uc->uc_sigmask, puc);
1079}
1080
bellard83fb7ad2004-07-05 21:25:26 +00001081#elif defined(__powerpc__)
bellard2b413142003-05-14 23:01:10 +00001082
bellard83fb7ad2004-07-05 21:25:26 +00001083/***********************************************************************
1084 * signal context platform-specific definitions
1085 * From Wine
1086 */
1087#ifdef linux
1088/* All Registers access - only for local access */
1089# define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
1090/* Gpr Registers access */
1091# define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
1092# define IAR_sig(context) REG_sig(nip, context) /* Program counter */
1093# define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
1094# define CTR_sig(context) REG_sig(ctr, context) /* Count register */
1095# define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
1096# define LR_sig(context) REG_sig(link, context) /* Link register */
1097# define CR_sig(context) REG_sig(ccr, context) /* Condition register */
1098/* Float Registers access */
1099# define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1100# define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1101/* Exception Registers access */
1102# define DAR_sig(context) REG_sig(dar, context)
1103# define DSISR_sig(context) REG_sig(dsisr, context)
1104# define TRAP_sig(context) REG_sig(trap, context)
1105#endif /* linux */
1106
1107#ifdef __APPLE__
1108# include <sys/ucontext.h>
1109typedef struct ucontext SIGCONTEXT;
1110/* All Registers access - only for local access */
1111# define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
1112# define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
1113# define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
1114# define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
1115/* Gpr Registers access */
1116# define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
1117# define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
1118# define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
1119# define CTR_sig(context) REG_sig(ctr, context)
1120# define XER_sig(context) REG_sig(xer, context) /* Link register */
1121# define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
1122# define CR_sig(context) REG_sig(cr, context) /* Condition register */
1123/* Float Registers access */
1124# define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
1125# define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
1126/* Exception Registers access */
1127# define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
1128# define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
1129# define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1130#endif /* __APPLE__ */
1131
bellardd1d9f422004-07-14 17:20:55 +00001132int cpu_signal_handler(int host_signum, struct siginfo *info,
bellarde4533c72003-06-15 19:51:39 +00001133 void *puc)
bellard2b413142003-05-14 23:01:10 +00001134{
bellard25eb4482003-05-14 21:50:54 +00001135 struct ucontext *uc = puc;
bellard25eb4482003-05-14 21:50:54 +00001136 unsigned long pc;
bellard25eb4482003-05-14 21:50:54 +00001137 int is_write;
1138
bellard83fb7ad2004-07-05 21:25:26 +00001139 pc = IAR_sig(uc);
bellard25eb4482003-05-14 21:50:54 +00001140 is_write = 0;
1141#if 0
1142 /* ppc 4xx case */
bellard83fb7ad2004-07-05 21:25:26 +00001143 if (DSISR_sig(uc) & 0x00800000)
bellard25eb4482003-05-14 21:50:54 +00001144 is_write = 1;
bellard9de5e442003-03-23 16:49:39 +00001145#else
bellard83fb7ad2004-07-05 21:25:26 +00001146 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
bellard25eb4482003-05-14 21:50:54 +00001147 is_write = 1;
1148#endif
1149 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001150 is_write, &uc->uc_sigmask, puc);
bellard9de5e442003-03-23 16:49:39 +00001151}
bellard2b413142003-05-14 23:01:10 +00001152
bellard2f87c602003-06-02 20:38:09 +00001153#elif defined(__alpha__)
1154
bellarde4533c72003-06-15 19:51:39 +00001155int cpu_signal_handler(int host_signum, struct siginfo *info,
bellard2f87c602003-06-02 20:38:09 +00001156 void *puc)
1157{
1158 struct ucontext *uc = puc;
1159 uint32_t *pc = uc->uc_mcontext.sc_pc;
1160 uint32_t insn = *pc;
1161 int is_write = 0;
1162
bellard8c6939c2003-06-09 15:28:00 +00001163 /* XXX: need kernel patch to get write flag faster */
bellard2f87c602003-06-02 20:38:09 +00001164 switch (insn >> 26) {
1165 case 0x0d: // stw
1166 case 0x0e: // stb
1167 case 0x0f: // stq_u
1168 case 0x24: // stf
1169 case 0x25: // stg
1170 case 0x26: // sts
1171 case 0x27: // stt
1172 case 0x2c: // stl
1173 case 0x2d: // stq
1174 case 0x2e: // stl_c
1175 case 0x2f: // stq_c
1176 is_write = 1;
1177 }
1178
1179 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001180 is_write, &uc->uc_sigmask, puc);
bellard2f87c602003-06-02 20:38:09 +00001181}
bellard8c6939c2003-06-09 15:28:00 +00001182#elif defined(__sparc__)
1183
bellarde4533c72003-06-15 19:51:39 +00001184int cpu_signal_handler(int host_signum, struct siginfo *info,
1185 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001186{
1187 uint32_t *regs = (uint32_t *)(info + 1);
1188 void *sigmask = (regs + 20);
1189 unsigned long pc;
1190 int is_write;
1191 uint32_t insn;
1192
1193 /* XXX: is there a standard glibc define ? */
1194 pc = regs[1];
1195 /* XXX: need kernel patch to get write flag faster */
1196 is_write = 0;
1197 insn = *(uint32_t *)pc;
1198 if ((insn >> 30) == 3) {
1199 switch((insn >> 19) & 0x3f) {
1200 case 0x05: // stb
1201 case 0x06: // sth
1202 case 0x04: // st
1203 case 0x07: // std
1204 case 0x24: // stf
1205 case 0x27: // stdf
1206 case 0x25: // stfsr
1207 is_write = 1;
1208 break;
1209 }
1210 }
1211 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001212 is_write, sigmask, NULL);
bellard8c6939c2003-06-09 15:28:00 +00001213}
1214
1215#elif defined(__arm__)
1216
bellarde4533c72003-06-15 19:51:39 +00001217int cpu_signal_handler(int host_signum, struct siginfo *info,
1218 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001219{
1220 struct ucontext *uc = puc;
1221 unsigned long pc;
1222 int is_write;
1223
1224 pc = uc->uc_mcontext.gregs[R15];
1225 /* XXX: compute is_write */
1226 is_write = 0;
1227 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1228 is_write,
1229 &uc->uc_sigmask);
1230}
1231
bellard38e584a2003-08-10 22:14:22 +00001232#elif defined(__mc68000)
1233
1234int cpu_signal_handler(int host_signum, struct siginfo *info,
1235 void *puc)
1236{
1237 struct ucontext *uc = puc;
1238 unsigned long pc;
1239 int is_write;
1240
1241 pc = uc->uc_mcontext.gregs[16];
1242 /* XXX: compute is_write */
1243 is_write = 0;
1244 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1245 is_write,
bellardbf3e8bf2004-02-16 21:58:54 +00001246 &uc->uc_sigmask, puc);
bellard38e584a2003-08-10 22:14:22 +00001247}
1248
bellardb8076a72005-04-07 22:20:31 +00001249#elif defined(__ia64)
1250
1251#ifndef __ISR_VALID
1252 /* This ought to be in <bits/siginfo.h>... */
1253# define __ISR_VALID 1
1254# define si_flags _sifields._sigfault._si_pad0
1255#endif
1256
1257int cpu_signal_handler(int host_signum, struct siginfo *info, void *puc)
1258{
1259 struct ucontext *uc = puc;
1260 unsigned long ip;
1261 int is_write = 0;
1262
1263 ip = uc->uc_mcontext.sc_ip;
1264 switch (host_signum) {
1265 case SIGILL:
1266 case SIGFPE:
1267 case SIGSEGV:
1268 case SIGBUS:
1269 case SIGTRAP:
1270 if (info->si_code && (info->si_flags & __ISR_VALID))
1271 /* ISR.W (write-access) is bit 33: */
1272 is_write = (info->si_isr >> 33) & 1;
1273 break;
1274
1275 default:
1276 break;
1277 }
1278 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1279 is_write,
1280 &uc->uc_sigmask, puc);
1281}
1282
bellard90cb9492005-07-24 15:11:38 +00001283#elif defined(__s390__)
1284
1285int cpu_signal_handler(int host_signum, struct siginfo *info,
1286 void *puc)
1287{
1288 struct ucontext *uc = puc;
1289 unsigned long pc;
1290 int is_write;
1291
1292 pc = uc->uc_mcontext.psw.addr;
1293 /* XXX: compute is_write */
1294 is_write = 0;
1295 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1296 is_write,
1297 &uc->uc_sigmask, puc);
1298}
1299
bellard2b413142003-05-14 23:01:10 +00001300#else
1301
bellard3fb2ded2003-06-24 13:22:59 +00001302#error host CPU specific signal handler needed
bellard2b413142003-05-14 23:01:10 +00001303
1304#endif
bellard67b915a2004-03-31 23:37:16 +00001305
1306#endif /* !defined(CONFIG_SOFTMMU) */