blob: 4916b1af6d43f92d5da09e05b0497da7af95334e [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
pbrooke6e59062006-10-22 00:18:54 +000043#if defined(TARGET_ARM) || defined(TARGET_SPARC) || defined(TARGET_M68K)
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
pbrooke6e59062006-10-22 00:18:54 +000050#if !(defined(TARGET_SPARC) || defined(TARGET_SH4) || defined(TARGET_M68K))
bellard34751872005-07-02 14:31:34 +000051#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
bellard8a40a182005-11-20 10:35:40 +000076
77static TranslationBlock *tb_find_slow(target_ulong pc,
78 target_ulong cs_base,
79 unsigned int flags)
80{
81 TranslationBlock *tb, **ptb1;
82 int code_gen_size;
83 unsigned int h;
84 target_ulong phys_pc, phys_page1, phys_page2, virt_page2;
85 uint8_t *tc_ptr;
86
87 spin_lock(&tb_lock);
88
89 tb_invalidated_flag = 0;
90
91 regs_to_env(); /* XXX: do it just before cpu_gen_code() */
92
93 /* find translated block using physical mappings */
94 phys_pc = get_phys_addr_code(env, pc);
95 phys_page1 = phys_pc & TARGET_PAGE_MASK;
96 phys_page2 = -1;
97 h = tb_phys_hash_func(phys_pc);
98 ptb1 = &tb_phys_hash[h];
99 for(;;) {
100 tb = *ptb1;
101 if (!tb)
102 goto not_found;
103 if (tb->pc == pc &&
104 tb->page_addr[0] == phys_page1 &&
105 tb->cs_base == cs_base &&
106 tb->flags == flags) {
107 /* check next page if needed */
108 if (tb->page_addr[1] != -1) {
109 virt_page2 = (pc & TARGET_PAGE_MASK) +
110 TARGET_PAGE_SIZE;
111 phys_page2 = get_phys_addr_code(env, virt_page2);
112 if (tb->page_addr[1] == phys_page2)
113 goto found;
114 } else {
115 goto found;
116 }
117 }
118 ptb1 = &tb->phys_hash_next;
119 }
120 not_found:
121 /* if no translated code available, then translate it now */
122 tb = tb_alloc(pc);
123 if (!tb) {
124 /* flush must be done */
125 tb_flush(env);
126 /* cannot fail at this point */
127 tb = tb_alloc(pc);
128 /* don't forget to invalidate previous TB info */
bellard15388002005-12-19 01:42:32 +0000129 tb_invalidated_flag = 1;
bellard8a40a182005-11-20 10:35:40 +0000130 }
131 tc_ptr = code_gen_ptr;
132 tb->tc_ptr = tc_ptr;
133 tb->cs_base = cs_base;
134 tb->flags = flags;
135 cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
136 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
137
138 /* check next page if needed */
139 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
140 phys_page2 = -1;
141 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
142 phys_page2 = get_phys_addr_code(env, virt_page2);
143 }
144 tb_link_phys(tb, phys_pc, phys_page2);
145
146 found:
bellard8a40a182005-11-20 10:35:40 +0000147 /* we add the TB in the virtual pc hash table */
148 env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)] = tb;
149 spin_unlock(&tb_lock);
150 return tb;
151}
152
153static inline TranslationBlock *tb_find_fast(void)
154{
155 TranslationBlock *tb;
156 target_ulong cs_base, pc;
157 unsigned int flags;
158
159 /* we record a subset of the CPU state. It will
160 always be the same before a given translated block
161 is executed. */
162#if defined(TARGET_I386)
163 flags = env->hflags;
164 flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
165 cs_base = env->segs[R_CS].base;
166 pc = cs_base + env->eip;
167#elif defined(TARGET_ARM)
168 flags = env->thumb | (env->vfp.vec_len << 1)
bellardb5ff1b32005-11-26 10:38:39 +0000169 | (env->vfp.vec_stride << 4);
170 if ((env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR)
171 flags |= (1 << 6);
pbrook40f137e2006-02-20 00:33:36 +0000172 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30))
173 flags |= (1 << 7);
bellard8a40a182005-11-20 10:35:40 +0000174 cs_base = 0;
175 pc = env->regs[15];
176#elif defined(TARGET_SPARC)
177#ifdef TARGET_SPARC64
bellarda80dde02006-06-26 19:53:29 +0000178 // Combined FPU enable bits . PRIV . DMMU enabled . IMMU enabled
179 flags = (((env->pstate & PS_PEF) >> 1) | ((env->fprs & FPRS_FEF) << 2))
180 | (env->pstate & PS_PRIV) | ((env->lsu & (DMMU_E | IMMU_E)) >> 2);
bellard8a40a182005-11-20 10:35:40 +0000181#else
bellarda80dde02006-06-26 19:53:29 +0000182 // FPU enable . MMU enabled . MMU no-fault . Supervisor
183 flags = (env->psref << 3) | ((env->mmuregs[0] & (MMU_E | MMU_NF)) << 1)
184 | env->psrs;
bellard8a40a182005-11-20 10:35:40 +0000185#endif
186 cs_base = env->npc;
187 pc = env->pc;
188#elif defined(TARGET_PPC)
189 flags = (msr_pr << MSR_PR) | (msr_fp << MSR_FP) |
190 (msr_se << MSR_SE) | (msr_le << MSR_LE);
191 cs_base = 0;
192 pc = env->nip;
193#elif defined(TARGET_MIPS)
pbrook56b19402006-03-11 16:23:39 +0000194 flags = env->hflags & (MIPS_HFLAG_TMASK | MIPS_HFLAG_BMASK);
bellardcc9442b2005-11-26 18:43:28 +0000195 cs_base = 0;
bellard8a40a182005-11-20 10:35:40 +0000196 pc = env->PC;
pbrooke6e59062006-10-22 00:18:54 +0000197#elif defined(TARGET_M68K)
198 flags = env->fpcr & M68K_FPCR_PREC;
199 cs_base = 0;
200 pc = env->pc;
bellardfdf9b3e2006-04-27 21:07:38 +0000201#elif defined(TARGET_SH4)
202 flags = env->sr & (SR_MD | SR_RB);
203 cs_base = 0; /* XXXXX */
204 pc = env->pc;
bellard8a40a182005-11-20 10:35:40 +0000205#else
206#error unsupported CPU
207#endif
208 tb = env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)];
209 if (__builtin_expect(!tb || tb->pc != pc || tb->cs_base != cs_base ||
210 tb->flags != flags, 0)) {
211 tb = tb_find_slow(pc, cs_base, flags);
bellard15388002005-12-19 01:42:32 +0000212 /* Note: we do it here to avoid a gcc bug on Mac OS X when
213 doing it in tb_find_slow */
214 if (tb_invalidated_flag) {
215 /* as some TB could have been invalidated because
216 of memory exceptions while generating the code, we
217 must recompute the hash index here */
218 T0 = 0;
219 }
bellard8a40a182005-11-20 10:35:40 +0000220 }
221 return tb;
222}
223
224
bellard7d132992003-03-06 23:23:54 +0000225/* main execution loop */
226
bellarde4533c72003-06-15 19:51:39 +0000227int cpu_exec(CPUState *env1)
bellard7d132992003-03-06 23:23:54 +0000228{
pbrook1057eaa2007-02-04 13:37:44 +0000229#define DECLARE_HOST_REGS 1
230#include "hostregs_helper.h"
231#if defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000232#if defined(reg_REGWPTR)
233 uint32_t *saved_regwptr;
234#endif
235#endif
bellardfdbb4692006-06-14 17:32:25 +0000236#if defined(__sparc__) && !defined(HOST_SOLARIS)
thsb49d07b2007-02-02 03:57:09 +0000237 int saved_i7;
238 target_ulong tmp_T0;
bellard8c6939c2003-06-09 15:28:00 +0000239#endif
bellard8a40a182005-11-20 10:35:40 +0000240 int ret, interrupt_request;
bellard7d132992003-03-06 23:23:54 +0000241 void (*gen_func)(void);
bellard8a40a182005-11-20 10:35:40 +0000242 TranslationBlock *tb;
bellardc27004e2005-01-03 23:35:10 +0000243 uint8_t *tc_ptr;
bellard8c6939c2003-06-09 15:28:00 +0000244
bellard5a1e3cf2005-11-23 21:02:53 +0000245#if defined(TARGET_I386)
246 /* handle exit of HALTED state */
247 if (env1->hflags & HF_HALTED_MASK) {
248 /* disable halt condition */
249 if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
250 (env1->eflags & IF_MASK)) {
251 env1->hflags &= ~HF_HALTED_MASK;
252 } else {
253 return EXCP_HALTED;
254 }
255 }
bellarde80e1cc2005-11-23 22:05:28 +0000256#elif defined(TARGET_PPC)
bellard50443c92005-11-26 20:15:14 +0000257 if (env1->halted) {
bellarde80e1cc2005-11-23 22:05:28 +0000258 if (env1->msr[MSR_EE] &&
j_mayer47103572007-03-30 09:38:04 +0000259 (env1->interrupt_request & CPU_INTERRUPT_HARD)) {
bellard50443c92005-11-26 20:15:14 +0000260 env1->halted = 0;
bellarde80e1cc2005-11-23 22:05:28 +0000261 } else {
262 return EXCP_HALTED;
263 }
264 }
bellardba3c64f2005-12-05 20:31:52 +0000265#elif defined(TARGET_SPARC)
266 if (env1->halted) {
267 if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
268 (env1->psret != 0)) {
269 env1->halted = 0;
270 } else {
271 return EXCP_HALTED;
272 }
273 }
bellard9332f9d2005-11-26 10:46:39 +0000274#elif defined(TARGET_ARM)
275 if (env1->halted) {
276 /* An interrupt wakes the CPU even if the I and F CPSR bits are
277 set. */
278 if (env1->interrupt_request
279 & (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD)) {
280 env1->halted = 0;
281 } else {
282 return EXCP_HALTED;
283 }
284 }
bellard6810e152005-12-05 19:59:05 +0000285#elif defined(TARGET_MIPS)
286 if (env1->halted) {
287 if (env1->interrupt_request &
288 (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER)) {
289 env1->halted = 0;
290 } else {
291 return EXCP_HALTED;
292 }
293 }
bellard5a1e3cf2005-11-23 21:02:53 +0000294#endif
295
bellard6a00d602005-11-21 23:25:50 +0000296 cpu_single_env = env1;
297
bellard7d132992003-03-06 23:23:54 +0000298 /* first we save global registers */
pbrook1057eaa2007-02-04 13:37:44 +0000299#define SAVE_HOST_REGS 1
300#include "hostregs_helper.h"
bellardc27004e2005-01-03 23:35:10 +0000301 env = env1;
bellardfdbb4692006-06-14 17:32:25 +0000302#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellarde4533c72003-06-15 19:51:39 +0000303 /* we also save i7 because longjmp may not restore it */
304 asm volatile ("mov %%i7, %0" : "=r" (saved_i7));
305#endif
306
307#if defined(TARGET_I386)
bellard0d1a29f2004-10-12 22:01:28 +0000308 env_to_regs();
bellard9de5e442003-03-23 16:49:39 +0000309 /* put eflags in CPU temporary format */
bellardfc2b4c42003-03-29 16:52:44 +0000310 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
311 DF = 1 - (2 * ((env->eflags >> 10) & 1));
bellard9de5e442003-03-23 16:49:39 +0000312 CC_OP = CC_OP_EFLAGS;
bellardfc2b4c42003-03-29 16:52:44 +0000313 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000314#elif defined(TARGET_ARM)
bellard93ac68b2003-09-30 20:57:29 +0000315#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000316#if defined(reg_REGWPTR)
317 saved_regwptr = REGWPTR;
318#endif
bellard67867302003-11-23 17:05:30 +0000319#elif defined(TARGET_PPC)
pbrooke6e59062006-10-22 00:18:54 +0000320#elif defined(TARGET_M68K)
321 env->cc_op = CC_OP_FLAGS;
322 env->cc_dest = env->sr & 0xf;
323 env->cc_x = (env->sr >> 4) & 1;
bellard6af0bf92005-07-02 14:58:51 +0000324#elif defined(TARGET_MIPS)
bellardfdf9b3e2006-04-27 21:07:38 +0000325#elif defined(TARGET_SH4)
326 /* XXXXX */
bellarde4533c72003-06-15 19:51:39 +0000327#else
328#error unsupported target CPU
329#endif
bellard3fb2ded2003-06-24 13:22:59 +0000330 env->exception_index = -1;
bellard9d27abd2003-05-10 13:13:54 +0000331
bellard7d132992003-03-06 23:23:54 +0000332 /* prepare setjmp context for exception handling */
bellard3fb2ded2003-06-24 13:22:59 +0000333 for(;;) {
334 if (setjmp(env->jmp_env) == 0) {
bellardee8b7022004-02-03 23:35:10 +0000335 env->current_tb = NULL;
bellard3fb2ded2003-06-24 13:22:59 +0000336 /* if an exception is pending, we execute it here */
337 if (env->exception_index >= 0) {
338 if (env->exception_index >= EXCP_INTERRUPT) {
339 /* exit request from the cpu execution loop */
340 ret = env->exception_index;
341 break;
342 } else if (env->user_mode_only) {
343 /* if user mode only, we simulate a fake exception
ths9f083492006-12-07 18:28:42 +0000344 which will be handled outside the cpu execution
bellard3fb2ded2003-06-24 13:22:59 +0000345 loop */
bellard83479e72003-06-25 16:12:37 +0000346#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000347 do_interrupt_user(env->exception_index,
348 env->exception_is_int,
349 env->error_code,
350 env->exception_next_eip);
bellard83479e72003-06-25 16:12:37 +0000351#endif
bellard3fb2ded2003-06-24 13:22:59 +0000352 ret = env->exception_index;
353 break;
354 } else {
bellard83479e72003-06-25 16:12:37 +0000355#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000356 /* simulate a real cpu exception. On i386, it can
357 trigger new exceptions, but we do not handle
358 double or triple faults yet. */
359 do_interrupt(env->exception_index,
360 env->exception_is_int,
361 env->error_code,
bellardd05e66d2003-08-20 21:34:35 +0000362 env->exception_next_eip, 0);
ths678dde12007-03-31 20:28:52 +0000363 /* successfully delivered */
364 env->old_exception = -1;
bellardce097762004-01-04 23:53:18 +0000365#elif defined(TARGET_PPC)
366 do_interrupt(env);
bellard6af0bf92005-07-02 14:58:51 +0000367#elif defined(TARGET_MIPS)
368 do_interrupt(env);
bellarde95c8d52004-09-30 22:22:08 +0000369#elif defined(TARGET_SPARC)
bellard1a0c3292005-02-13 19:02:07 +0000370 do_interrupt(env->exception_index);
bellardb5ff1b32005-11-26 10:38:39 +0000371#elif defined(TARGET_ARM)
372 do_interrupt(env);
bellardfdf9b3e2006-04-27 21:07:38 +0000373#elif defined(TARGET_SH4)
374 do_interrupt(env);
bellard83479e72003-06-25 16:12:37 +0000375#endif
bellard3fb2ded2003-06-24 13:22:59 +0000376 }
377 env->exception_index = -1;
bellard9df217a2005-02-10 22:05:51 +0000378 }
379#ifdef USE_KQEMU
380 if (kqemu_is_ok(env) && env->interrupt_request == 0) {
381 int ret;
382 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
383 ret = kqemu_cpu_exec(env);
384 /* put eflags in CPU temporary format */
385 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
386 DF = 1 - (2 * ((env->eflags >> 10) & 1));
387 CC_OP = CC_OP_EFLAGS;
388 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
389 if (ret == 1) {
390 /* exception */
391 longjmp(env->jmp_env, 1);
392 } else if (ret == 2) {
393 /* softmmu execution needed */
394 } else {
395 if (env->interrupt_request != 0) {
396 /* hardware interrupt will be executed just after */
397 } else {
398 /* otherwise, we restart */
399 longjmp(env->jmp_env, 1);
400 }
401 }
bellard9de5e442003-03-23 16:49:39 +0000402 }
bellard9df217a2005-02-10 22:05:51 +0000403#endif
404
bellard3fb2ded2003-06-24 13:22:59 +0000405 T0 = 0; /* force lookup of first TB */
406 for(;;) {
bellardfdbb4692006-06-14 17:32:25 +0000407#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard3fb2ded2003-06-24 13:22:59 +0000408 /* g1 can be modified by some libc? functions */
409 tmp_T0 = T0;
410#endif
bellard68a79312003-06-30 13:12:32 +0000411 interrupt_request = env->interrupt_request;
bellard2e255c62003-08-21 23:25:21 +0000412 if (__builtin_expect(interrupt_request, 0)) {
pbrook6658ffb2007-03-16 23:58:11 +0000413 if (interrupt_request & CPU_INTERRUPT_DEBUG) {
414 env->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
415 env->exception_index = EXCP_DEBUG;
416 cpu_loop_exit();
417 }
bellard68a79312003-06-30 13:12:32 +0000418#if defined(TARGET_I386)
bellard3b21e032006-09-24 18:41:56 +0000419 if ((interrupt_request & CPU_INTERRUPT_SMI) &&
420 !(env->hflags & HF_SMM_MASK)) {
421 env->interrupt_request &= ~CPU_INTERRUPT_SMI;
422 do_smm_enter();
423#if defined(__sparc__) && !defined(HOST_SOLARIS)
424 tmp_T0 = 0;
425#else
426 T0 = 0;
427#endif
428 } else if ((interrupt_request & CPU_INTERRUPT_HARD) &&
bellard3f337312003-08-20 23:02:09 +0000429 (env->eflags & IF_MASK) &&
430 !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
bellard68a79312003-06-30 13:12:32 +0000431 int intno;
bellardfbf9eeb2004-04-25 21:21:33 +0000432 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellarda541f292004-04-12 20:39:29 +0000433 intno = cpu_get_pic_interrupt(env);
bellardf193c792004-03-21 17:06:25 +0000434 if (loglevel & CPU_LOG_TB_IN_ASM) {
bellard68a79312003-06-30 13:12:32 +0000435 fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
436 }
bellardd05e66d2003-08-20 21:34:35 +0000437 do_interrupt(intno, 0, 0, 0, 1);
bellard907a5b22003-06-30 23:18:22 +0000438 /* ensure that no TB jump will be modified as
439 the program flow was changed */
bellardfdbb4692006-06-14 17:32:25 +0000440#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard907a5b22003-06-30 23:18:22 +0000441 tmp_T0 = 0;
442#else
443 T0 = 0;
444#endif
bellard68a79312003-06-30 13:12:32 +0000445 }
bellardce097762004-01-04 23:53:18 +0000446#elif defined(TARGET_PPC)
bellard9fddaa02004-05-21 12:59:32 +0000447#if 0
448 if ((interrupt_request & CPU_INTERRUPT_RESET)) {
449 cpu_ppc_reset(env);
450 }
451#endif
j_mayer47103572007-03-30 09:38:04 +0000452 if (interrupt_request & CPU_INTERRUPT_HARD) {
453 if (ppc_hw_interrupt(env) == 1) {
454 /* Some exception was raised */
455 if (env->pending_interrupts == 0)
456 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellardfdbb4692006-06-14 17:32:25 +0000457#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8a40a182005-11-20 10:35:40 +0000458 tmp_T0 = 0;
459#else
460 T0 = 0;
461#endif
462 }
bellardce097762004-01-04 23:53:18 +0000463 }
bellard6af0bf92005-07-02 14:58:51 +0000464#elif defined(TARGET_MIPS)
465 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
ths24c7b0e2007-03-30 16:44:54 +0000466 (env->CP0_Status & env->CP0_Cause & CP0Ca_IP_mask) &&
bellard6af0bf92005-07-02 14:58:51 +0000467 (env->CP0_Status & (1 << CP0St_IE)) &&
ths24c7b0e2007-03-30 16:44:54 +0000468 !(env->CP0_Status & (1 << CP0St_EXL)) &&
469 !(env->CP0_Status & (1 << CP0St_ERL)) &&
bellard6af0bf92005-07-02 14:58:51 +0000470 !(env->hflags & MIPS_HFLAG_DM)) {
471 /* Raise it */
472 env->exception_index = EXCP_EXT_INTERRUPT;
473 env->error_code = 0;
474 do_interrupt(env);
bellardfdbb4692006-06-14 17:32:25 +0000475#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8a40a182005-11-20 10:35:40 +0000476 tmp_T0 = 0;
477#else
478 T0 = 0;
479#endif
bellard6af0bf92005-07-02 14:58:51 +0000480 }
bellarde95c8d52004-09-30 22:22:08 +0000481#elif defined(TARGET_SPARC)
bellard66321a12005-04-06 20:47:48 +0000482 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
483 (env->psret != 0)) {
484 int pil = env->interrupt_index & 15;
485 int type = env->interrupt_index & 0xf0;
486
487 if (((type == TT_EXTINT) &&
488 (pil == 15 || pil > env->psrpil)) ||
489 type != TT_EXTINT) {
490 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
491 do_interrupt(env->interrupt_index);
492 env->interrupt_index = 0;
bellardfdbb4692006-06-14 17:32:25 +0000493#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8a40a182005-11-20 10:35:40 +0000494 tmp_T0 = 0;
495#else
496 T0 = 0;
497#endif
bellard66321a12005-04-06 20:47:48 +0000498 }
bellarde95c8d52004-09-30 22:22:08 +0000499 } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
500 //do_interrupt(0, 0, 0, 0, 0);
501 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
bellardba3c64f2005-12-05 20:31:52 +0000502 } else if (interrupt_request & CPU_INTERRUPT_HALT) {
bellarddf52b002006-09-20 20:30:57 +0000503 env->interrupt_request &= ~CPU_INTERRUPT_HALT;
504 env->halted = 1;
505 env->exception_index = EXCP_HLT;
506 cpu_loop_exit();
bellardba3c64f2005-12-05 20:31:52 +0000507 }
bellardb5ff1b32005-11-26 10:38:39 +0000508#elif defined(TARGET_ARM)
509 if (interrupt_request & CPU_INTERRUPT_FIQ
510 && !(env->uncached_cpsr & CPSR_F)) {
511 env->exception_index = EXCP_FIQ;
512 do_interrupt(env);
513 }
514 if (interrupt_request & CPU_INTERRUPT_HARD
515 && !(env->uncached_cpsr & CPSR_I)) {
516 env->exception_index = EXCP_IRQ;
517 do_interrupt(env);
518 }
bellardfdf9b3e2006-04-27 21:07:38 +0000519#elif defined(TARGET_SH4)
520 /* XXXXX */
bellard68a79312003-06-30 13:12:32 +0000521#endif
bellard9d050952006-05-22 22:03:52 +0000522 /* Don't use the cached interupt_request value,
523 do_interrupt may have updated the EXITTB flag. */
bellardb5ff1b32005-11-26 10:38:39 +0000524 if (env->interrupt_request & CPU_INTERRUPT_EXITTB) {
bellardbf3e8bf2004-02-16 21:58:54 +0000525 env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
526 /* ensure that no TB jump will be modified as
527 the program flow was changed */
bellardfdbb4692006-06-14 17:32:25 +0000528#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellardbf3e8bf2004-02-16 21:58:54 +0000529 tmp_T0 = 0;
530#else
531 T0 = 0;
532#endif
533 }
bellard68a79312003-06-30 13:12:32 +0000534 if (interrupt_request & CPU_INTERRUPT_EXIT) {
535 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
536 env->exception_index = EXCP_INTERRUPT;
537 cpu_loop_exit();
538 }
bellard3fb2ded2003-06-24 13:22:59 +0000539 }
540#ifdef DEBUG_EXEC
bellardb5ff1b32005-11-26 10:38:39 +0000541 if ((loglevel & CPU_LOG_TB_CPU)) {
bellard3fb2ded2003-06-24 13:22:59 +0000542#if defined(TARGET_I386)
543 /* restore flags in standard format */
bellardfc9f7152005-04-26 19:33:35 +0000544#ifdef reg_EAX
bellard3fb2ded2003-06-24 13:22:59 +0000545 env->regs[R_EAX] = EAX;
bellardfc9f7152005-04-26 19:33:35 +0000546#endif
547#ifdef reg_EBX
bellard3fb2ded2003-06-24 13:22:59 +0000548 env->regs[R_EBX] = EBX;
bellardfc9f7152005-04-26 19:33:35 +0000549#endif
550#ifdef reg_ECX
bellard3fb2ded2003-06-24 13:22:59 +0000551 env->regs[R_ECX] = ECX;
bellardfc9f7152005-04-26 19:33:35 +0000552#endif
553#ifdef reg_EDX
bellard3fb2ded2003-06-24 13:22:59 +0000554 env->regs[R_EDX] = EDX;
bellardfc9f7152005-04-26 19:33:35 +0000555#endif
556#ifdef reg_ESI
bellard3fb2ded2003-06-24 13:22:59 +0000557 env->regs[R_ESI] = ESI;
bellardfc9f7152005-04-26 19:33:35 +0000558#endif
559#ifdef reg_EDI
bellard3fb2ded2003-06-24 13:22:59 +0000560 env->regs[R_EDI] = EDI;
bellardfc9f7152005-04-26 19:33:35 +0000561#endif
562#ifdef reg_EBP
bellard3fb2ded2003-06-24 13:22:59 +0000563 env->regs[R_EBP] = EBP;
bellardfc9f7152005-04-26 19:33:35 +0000564#endif
565#ifdef reg_ESP
bellard3fb2ded2003-06-24 13:22:59 +0000566 env->regs[R_ESP] = ESP;
bellardfc9f7152005-04-26 19:33:35 +0000567#endif
bellard3fb2ded2003-06-24 13:22:59 +0000568 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard7fe48482004-10-09 18:08:01 +0000569 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
bellard3fb2ded2003-06-24 13:22:59 +0000570 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000571#elif defined(TARGET_ARM)
bellard7fe48482004-10-09 18:08:01 +0000572 cpu_dump_state(env, logfile, fprintf, 0);
bellard93ac68b2003-09-30 20:57:29 +0000573#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000574 REGWPTR = env->regbase + (env->cwp * 16);
575 env->regwptr = REGWPTR;
576 cpu_dump_state(env, logfile, fprintf, 0);
bellard67867302003-11-23 17:05:30 +0000577#elif defined(TARGET_PPC)
bellard7fe48482004-10-09 18:08:01 +0000578 cpu_dump_state(env, logfile, fprintf, 0);
pbrooke6e59062006-10-22 00:18:54 +0000579#elif defined(TARGET_M68K)
580 cpu_m68k_flush_flags(env, env->cc_op);
581 env->cc_op = CC_OP_FLAGS;
582 env->sr = (env->sr & 0xffe0)
583 | env->cc_dest | (env->cc_x << 4);
584 cpu_dump_state(env, logfile, fprintf, 0);
bellard6af0bf92005-07-02 14:58:51 +0000585#elif defined(TARGET_MIPS)
586 cpu_dump_state(env, logfile, fprintf, 0);
bellardfdf9b3e2006-04-27 21:07:38 +0000587#elif defined(TARGET_SH4)
588 cpu_dump_state(env, logfile, fprintf, 0);
bellarde4533c72003-06-15 19:51:39 +0000589#else
590#error unsupported target CPU
591#endif
bellard3fb2ded2003-06-24 13:22:59 +0000592 }
bellard7d132992003-03-06 23:23:54 +0000593#endif
bellard8a40a182005-11-20 10:35:40 +0000594 tb = tb_find_fast();
bellard9d27abd2003-05-10 13:13:54 +0000595#ifdef DEBUG_EXEC
bellardc1135f62005-01-30 22:41:54 +0000596 if ((loglevel & CPU_LOG_EXEC)) {
bellardc27004e2005-01-03 23:35:10 +0000597 fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
598 (long)tb->tc_ptr, tb->pc,
599 lookup_symbol(tb->pc));
bellard3fb2ded2003-06-24 13:22:59 +0000600 }
bellard9d27abd2003-05-10 13:13:54 +0000601#endif
bellardfdbb4692006-06-14 17:32:25 +0000602#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard3fb2ded2003-06-24 13:22:59 +0000603 T0 = tmp_T0;
bellard8c6939c2003-06-09 15:28:00 +0000604#endif
bellard8a40a182005-11-20 10:35:40 +0000605 /* see if we can patch the calling TB. When the TB
606 spans two pages, we cannot safely do a direct
607 jump. */
bellardc27004e2005-01-03 23:35:10 +0000608 {
bellard8a40a182005-11-20 10:35:40 +0000609 if (T0 != 0 &&
bellardf32fc642006-02-08 22:43:39 +0000610#if USE_KQEMU
611 (env->kqemu_enabled != 2) &&
612#endif
bellard8a40a182005-11-20 10:35:40 +0000613 tb->page_addr[1] == -1
bellardbf3e8bf2004-02-16 21:58:54 +0000614#if defined(TARGET_I386) && defined(USE_CODE_COPY)
615 && (tb->cflags & CF_CODE_COPY) ==
616 (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
617#endif
618 ) {
bellard3fb2ded2003-06-24 13:22:59 +0000619 spin_lock(&tb_lock);
bellardc27004e2005-01-03 23:35:10 +0000620 tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
bellard97eb5b12004-02-25 23:19:55 +0000621#if defined(USE_CODE_COPY)
622 /* propagates the FP use info */
623 ((TranslationBlock *)(T0 & ~3))->cflags |=
624 (tb->cflags & CF_FP_USED);
625#endif
bellard3fb2ded2003-06-24 13:22:59 +0000626 spin_unlock(&tb_lock);
627 }
bellardc27004e2005-01-03 23:35:10 +0000628 }
bellard3fb2ded2003-06-24 13:22:59 +0000629 tc_ptr = tb->tc_ptr;
bellard83479e72003-06-25 16:12:37 +0000630 env->current_tb = tb;
bellard3fb2ded2003-06-24 13:22:59 +0000631 /* execute the generated code */
632 gen_func = (void *)tc_ptr;
633#if defined(__sparc__)
634 __asm__ __volatile__("call %0\n\t"
635 "mov %%o7,%%i0"
636 : /* no outputs */
637 : "r" (gen_func)
bellardfdbb4692006-06-14 17:32:25 +0000638 : "i0", "i1", "i2", "i3", "i4", "i5",
thsfaab7592007-03-19 20:39:49 +0000639 "o0", "o1", "o2", "o3", "o4", "o5",
bellardfdbb4692006-06-14 17:32:25 +0000640 "l0", "l1", "l2", "l3", "l4", "l5",
641 "l6", "l7");
bellard3fb2ded2003-06-24 13:22:59 +0000642#elif defined(__arm__)
643 asm volatile ("mov pc, %0\n\t"
644 ".global exec_loop\n\t"
645 "exec_loop:\n\t"
646 : /* no outputs */
647 : "r" (gen_func)
648 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
bellardbf3e8bf2004-02-16 21:58:54 +0000649#elif defined(TARGET_I386) && defined(USE_CODE_COPY)
650{
651 if (!(tb->cflags & CF_CODE_COPY)) {
bellard97eb5b12004-02-25 23:19:55 +0000652 if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
653 save_native_fp_state(env);
654 }
bellardbf3e8bf2004-02-16 21:58:54 +0000655 gen_func();
656 } else {
bellard97eb5b12004-02-25 23:19:55 +0000657 if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
658 restore_native_fp_state(env);
659 }
bellardbf3e8bf2004-02-16 21:58:54 +0000660 /* we work with native eflags */
661 CC_SRC = cc_table[CC_OP].compute_all();
662 CC_OP = CC_OP_EFLAGS;
663 asm(".globl exec_loop\n"
664 "\n"
665 "debug1:\n"
666 " pushl %%ebp\n"
667 " fs movl %10, %9\n"
668 " fs movl %11, %%eax\n"
669 " andl $0x400, %%eax\n"
670 " fs orl %8, %%eax\n"
671 " pushl %%eax\n"
672 " popf\n"
673 " fs movl %%esp, %12\n"
674 " fs movl %0, %%eax\n"
675 " fs movl %1, %%ecx\n"
676 " fs movl %2, %%edx\n"
677 " fs movl %3, %%ebx\n"
678 " fs movl %4, %%esp\n"
679 " fs movl %5, %%ebp\n"
680 " fs movl %6, %%esi\n"
681 " fs movl %7, %%edi\n"
682 " fs jmp *%9\n"
683 "exec_loop:\n"
684 " fs movl %%esp, %4\n"
685 " fs movl %12, %%esp\n"
686 " fs movl %%eax, %0\n"
687 " fs movl %%ecx, %1\n"
688 " fs movl %%edx, %2\n"
689 " fs movl %%ebx, %3\n"
690 " fs movl %%ebp, %5\n"
691 " fs movl %%esi, %6\n"
692 " fs movl %%edi, %7\n"
693 " pushf\n"
694 " popl %%eax\n"
695 " movl %%eax, %%ecx\n"
696 " andl $0x400, %%ecx\n"
697 " shrl $9, %%ecx\n"
698 " andl $0x8d5, %%eax\n"
699 " fs movl %%eax, %8\n"
700 " movl $1, %%eax\n"
701 " subl %%ecx, %%eax\n"
702 " fs movl %%eax, %11\n"
703 " fs movl %9, %%ebx\n" /* get T0 value */
704 " popl %%ebp\n"
705 :
706 : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
707 "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
708 "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
709 "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
710 "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
711 "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
712 "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
713 "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
714 "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
715 "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
716 "a" (gen_func),
717 "m" (*(uint8_t *)offsetof(CPUState, df)),
718 "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
719 : "%ecx", "%edx"
720 );
721 }
722}
bellardb8076a72005-04-07 22:20:31 +0000723#elif defined(__ia64)
724 struct fptr {
725 void *ip;
726 void *gp;
727 } fp;
728
729 fp.ip = tc_ptr;
730 fp.gp = code_gen_buffer + 2 * (1 << 20);
731 (*(void (*)(void)) &fp)();
bellard3fb2ded2003-06-24 13:22:59 +0000732#else
733 gen_func();
734#endif
bellard83479e72003-06-25 16:12:37 +0000735 env->current_tb = NULL;
bellard4cbf74b2003-08-10 21:48:43 +0000736 /* reset soft MMU for next block (it can currently
737 only be set by a memory fault) */
738#if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
bellard3f337312003-08-20 23:02:09 +0000739 if (env->hflags & HF_SOFTMMU_MASK) {
740 env->hflags &= ~HF_SOFTMMU_MASK;
bellard4cbf74b2003-08-10 21:48:43 +0000741 /* do not allow linking to another block */
742 T0 = 0;
743 }
744#endif
bellardf32fc642006-02-08 22:43:39 +0000745#if defined(USE_KQEMU)
746#define MIN_CYCLE_BEFORE_SWITCH (100 * 1000)
747 if (kqemu_is_ok(env) &&
748 (cpu_get_time_fast() - env->last_io_time) >= MIN_CYCLE_BEFORE_SWITCH) {
749 cpu_loop_exit();
750 }
751#endif
bellard3fb2ded2003-06-24 13:22:59 +0000752 }
753 } else {
bellard0d1a29f2004-10-12 22:01:28 +0000754 env_to_regs();
bellard7d132992003-03-06 23:23:54 +0000755 }
bellard3fb2ded2003-06-24 13:22:59 +0000756 } /* for(;;) */
757
bellard7d132992003-03-06 23:23:54 +0000758
bellarde4533c72003-06-15 19:51:39 +0000759#if defined(TARGET_I386)
bellard97eb5b12004-02-25 23:19:55 +0000760#if defined(USE_CODE_COPY)
761 if (env->native_fp_regs) {
762 save_native_fp_state(env);
763 }
764#endif
bellard9de5e442003-03-23 16:49:39 +0000765 /* restore flags in standard format */
bellardfc2b4c42003-03-29 16:52:44 +0000766 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellarde4533c72003-06-15 19:51:39 +0000767#elif defined(TARGET_ARM)
bellardb7bcbe92005-02-22 19:27:29 +0000768 /* XXX: Save/restore host fpu exception state?. */
bellard93ac68b2003-09-30 20:57:29 +0000769#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000770#if defined(reg_REGWPTR)
771 REGWPTR = saved_regwptr;
772#endif
bellard67867302003-11-23 17:05:30 +0000773#elif defined(TARGET_PPC)
pbrooke6e59062006-10-22 00:18:54 +0000774#elif defined(TARGET_M68K)
775 cpu_m68k_flush_flags(env, env->cc_op);
776 env->cc_op = CC_OP_FLAGS;
777 env->sr = (env->sr & 0xffe0)
778 | env->cc_dest | (env->cc_x << 4);
bellard6af0bf92005-07-02 14:58:51 +0000779#elif defined(TARGET_MIPS)
bellardfdf9b3e2006-04-27 21:07:38 +0000780#elif defined(TARGET_SH4)
781 /* XXXXX */
bellarde4533c72003-06-15 19:51:39 +0000782#else
783#error unsupported target CPU
784#endif
pbrook1057eaa2007-02-04 13:37:44 +0000785
786 /* restore global registers */
bellardfdbb4692006-06-14 17:32:25 +0000787#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8c6939c2003-06-09 15:28:00 +0000788 asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
789#endif
pbrook1057eaa2007-02-04 13:37:44 +0000790#include "hostregs_helper.h"
791
bellard6a00d602005-11-21 23:25:50 +0000792 /* fail safe : never use cpu_single_env outside cpu_exec() */
793 cpu_single_env = NULL;
bellard7d132992003-03-06 23:23:54 +0000794 return ret;
795}
bellard6dbad632003-03-16 18:05:05 +0000796
bellardfbf9eeb2004-04-25 21:21:33 +0000797/* must only be called from the generated code as an exception can be
798 generated */
799void tb_invalidate_page_range(target_ulong start, target_ulong end)
800{
bellarddc5d0b32004-06-22 18:43:30 +0000801 /* XXX: cannot enable it yet because it yields to MMU exception
802 where NIP != read address on PowerPC */
803#if 0
bellardfbf9eeb2004-04-25 21:21:33 +0000804 target_ulong phys_addr;
805 phys_addr = get_phys_addr_code(env, start);
806 tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
bellarddc5d0b32004-06-22 18:43:30 +0000807#endif
bellardfbf9eeb2004-04-25 21:21:33 +0000808}
809
bellard1a18c712003-10-30 01:07:51 +0000810#if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
bellarde4533c72003-06-15 19:51:39 +0000811
bellard6dbad632003-03-16 18:05:05 +0000812void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
813{
814 CPUX86State *saved_env;
815
816 saved_env = env;
817 env = s;
bellarda412ac52003-07-26 18:01:40 +0000818 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
bellarda513fe12003-05-27 23:29:48 +0000819 selector &= 0xffff;
bellard2e255c62003-08-21 23:25:21 +0000820 cpu_x86_load_seg_cache(env, seg_reg, selector,
bellardc27004e2005-01-03 23:35:10 +0000821 (selector << 4), 0xffff, 0);
bellarda513fe12003-05-27 23:29:48 +0000822 } else {
bellardb453b702004-01-04 15:45:21 +0000823 load_seg(seg_reg, selector);
bellarda513fe12003-05-27 23:29:48 +0000824 }
bellard6dbad632003-03-16 18:05:05 +0000825 env = saved_env;
826}
bellard9de5e442003-03-23 16:49:39 +0000827
bellardd0a1ffc2003-05-29 20:04:28 +0000828void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
829{
830 CPUX86State *saved_env;
831
832 saved_env = env;
833 env = s;
834
bellardc27004e2005-01-03 23:35:10 +0000835 helper_fsave((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000836
837 env = saved_env;
838}
839
840void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
841{
842 CPUX86State *saved_env;
843
844 saved_env = env;
845 env = s;
846
bellardc27004e2005-01-03 23:35:10 +0000847 helper_frstor((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000848
849 env = saved_env;
850}
851
bellarde4533c72003-06-15 19:51:39 +0000852#endif /* TARGET_I386 */
853
bellard67b915a2004-03-31 23:37:16 +0000854#if !defined(CONFIG_SOFTMMU)
855
bellard3fb2ded2003-06-24 13:22:59 +0000856#if defined(TARGET_I386)
857
bellardb56dad12003-05-08 15:38:04 +0000858/* 'pc' is the host PC at which the exception was raised. 'address' is
bellardfd6ce8f2003-05-14 19:00:11 +0000859 the effective address of the memory exception. 'is_write' is 1 if a
860 write caused the exception and otherwise 0'. 'old_set' is the
861 signal set which should be restored */
bellard2b413142003-05-14 23:01:10 +0000862static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000863 int is_write, sigset_t *old_set,
864 void *puc)
bellard9de5e442003-03-23 16:49:39 +0000865{
bellarda513fe12003-05-27 23:29:48 +0000866 TranslationBlock *tb;
867 int ret;
bellard68a79312003-06-30 13:12:32 +0000868
bellard83479e72003-06-25 16:12:37 +0000869 if (cpu_single_env)
870 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellardfd6ce8f2003-05-14 19:00:11 +0000871#if defined(DEBUG_SIGNAL)
bellardbf3e8bf2004-02-16 21:58:54 +0000872 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
873 pc, address, is_write, *(unsigned long *)old_set);
bellard9de5e442003-03-23 16:49:39 +0000874#endif
bellard25eb4482003-05-14 21:50:54 +0000875 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000876 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellardfd6ce8f2003-05-14 19:00:11 +0000877 return 1;
878 }
bellardfbf9eeb2004-04-25 21:21:33 +0000879
bellard3fb2ded2003-06-24 13:22:59 +0000880 /* see if it is an MMU fault */
bellard93a40ea2003-10-27 21:13:06 +0000881 ret = cpu_x86_handle_mmu_fault(env, address, is_write,
882 ((env->hflags & HF_CPL_MASK) == 3), 0);
bellard3fb2ded2003-06-24 13:22:59 +0000883 if (ret < 0)
884 return 0; /* not an MMU fault */
885 if (ret == 0)
886 return 1; /* the MMU fault was handled without causing real CPU fault */
887 /* now we have a real cpu fault */
bellarda513fe12003-05-27 23:29:48 +0000888 tb = tb_find_pc(pc);
889 if (tb) {
bellard9de5e442003-03-23 16:49:39 +0000890 /* the PC is inside the translated code. It means that we have
891 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +0000892 cpu_restore_state(tb, env, pc, puc);
bellard3fb2ded2003-06-24 13:22:59 +0000893 }
bellard4cbf74b2003-08-10 21:48:43 +0000894 if (ret == 1) {
bellard3fb2ded2003-06-24 13:22:59 +0000895#if 0
bellard4cbf74b2003-08-10 21:48:43 +0000896 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
897 env->eip, env->cr[2], env->error_code);
bellard3fb2ded2003-06-24 13:22:59 +0000898#endif
bellard4cbf74b2003-08-10 21:48:43 +0000899 /* we restore the process signal mask as the sigreturn should
900 do it (XXX: use sigsetjmp) */
901 sigprocmask(SIG_SETMASK, old_set, NULL);
bellard54ca9092005-12-04 18:46:06 +0000902 raise_exception_err(env->exception_index, env->error_code);
bellard4cbf74b2003-08-10 21:48:43 +0000903 } else {
904 /* activate soft MMU for this block */
bellard3f337312003-08-20 23:02:09 +0000905 env->hflags |= HF_SOFTMMU_MASK;
bellardfbf9eeb2004-04-25 21:21:33 +0000906 cpu_resume_from_signal(env, puc);
bellard4cbf74b2003-08-10 21:48:43 +0000907 }
bellard3fb2ded2003-06-24 13:22:59 +0000908 /* never comes here */
909 return 1;
910}
911
bellarde4533c72003-06-15 19:51:39 +0000912#elif defined(TARGET_ARM)
bellard3fb2ded2003-06-24 13:22:59 +0000913static 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)
bellard3fb2ded2003-06-24 13:22:59 +0000916{
bellard68016c62005-02-07 23:12:27 +0000917 TranslationBlock *tb;
918 int ret;
919
920 if (cpu_single_env)
921 env = cpu_single_env; /* XXX: find a correct solution for multithread */
922#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
bellard9f0777e2005-02-02 20:42:01 +0000926 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000927 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellard9f0777e2005-02-02 20:42:01 +0000928 return 1;
929 }
bellard68016c62005-02-07 23:12:27 +0000930 /* see if it is an MMU fault */
931 ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
932 if (ret < 0)
933 return 0; /* not an MMU fault */
934 if (ret == 0)
935 return 1; /* the MMU fault was handled without causing real CPU fault */
936 /* now we have a real cpu fault */
937 tb = tb_find_pc(pc);
938 if (tb) {
939 /* the PC is inside the translated code. It means that we have
940 a virtual CPU fault */
941 cpu_restore_state(tb, env, pc, puc);
942 }
943 /* we restore the process signal mask as the sigreturn should
944 do it (XXX: use sigsetjmp) */
945 sigprocmask(SIG_SETMASK, old_set, NULL);
946 cpu_loop_exit();
bellard3fb2ded2003-06-24 13:22:59 +0000947}
bellard93ac68b2003-09-30 20:57:29 +0000948#elif defined(TARGET_SPARC)
949static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000950 int is_write, sigset_t *old_set,
951 void *puc)
bellard93ac68b2003-09-30 20:57:29 +0000952{
bellard68016c62005-02-07 23:12:27 +0000953 TranslationBlock *tb;
954 int ret;
955
956 if (cpu_single_env)
957 env = cpu_single_env; /* XXX: find a correct solution for multithread */
958#if defined(DEBUG_SIGNAL)
959 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
960 pc, address, is_write, *(unsigned long *)old_set);
961#endif
bellardb453b702004-01-04 15:45:21 +0000962 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000963 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellardb453b702004-01-04 15:45:21 +0000964 return 1;
965 }
bellard68016c62005-02-07 23:12:27 +0000966 /* see if it is an MMU fault */
967 ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
968 if (ret < 0)
969 return 0; /* not an MMU fault */
970 if (ret == 0)
971 return 1; /* the MMU fault was handled without causing real CPU fault */
972 /* now we have a real cpu fault */
973 tb = tb_find_pc(pc);
974 if (tb) {
975 /* the PC is inside the translated code. It means that we have
976 a virtual CPU fault */
977 cpu_restore_state(tb, env, pc, puc);
978 }
979 /* we restore the process signal mask as the sigreturn should
980 do it (XXX: use sigsetjmp) */
981 sigprocmask(SIG_SETMASK, old_set, NULL);
982 cpu_loop_exit();
bellard93ac68b2003-09-30 20:57:29 +0000983}
bellard67867302003-11-23 17:05:30 +0000984#elif defined (TARGET_PPC)
985static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000986 int is_write, sigset_t *old_set,
987 void *puc)
bellard67867302003-11-23 17:05:30 +0000988{
989 TranslationBlock *tb;
bellardce097762004-01-04 23:53:18 +0000990 int ret;
bellard67867302003-11-23 17:05:30 +0000991
bellard67867302003-11-23 17:05:30 +0000992 if (cpu_single_env)
993 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellard67867302003-11-23 17:05:30 +0000994#if defined(DEBUG_SIGNAL)
995 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
996 pc, address, is_write, *(unsigned long *)old_set);
997#endif
998 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000999 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellard67867302003-11-23 17:05:30 +00001000 return 1;
1001 }
1002
bellardce097762004-01-04 23:53:18 +00001003 /* see if it is an MMU fault */
bellard7f957d22004-01-18 23:19:48 +00001004 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
bellardce097762004-01-04 23:53:18 +00001005 if (ret < 0)
1006 return 0; /* not an MMU fault */
1007 if (ret == 0)
1008 return 1; /* the MMU fault was handled without causing real CPU fault */
1009
bellard67867302003-11-23 17:05:30 +00001010 /* now we have a real cpu fault */
1011 tb = tb_find_pc(pc);
1012 if (tb) {
1013 /* the PC is inside the translated code. It means that we have
1014 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +00001015 cpu_restore_state(tb, env, pc, puc);
bellard67867302003-11-23 17:05:30 +00001016 }
bellardce097762004-01-04 23:53:18 +00001017 if (ret == 1) {
bellard67867302003-11-23 17:05:30 +00001018#if 0
bellardce097762004-01-04 23:53:18 +00001019 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1020 env->nip, env->error_code, tb);
bellard67867302003-11-23 17:05:30 +00001021#endif
1022 /* we restore the process signal mask as the sigreturn should
1023 do it (XXX: use sigsetjmp) */
bellardbf3e8bf2004-02-16 21:58:54 +00001024 sigprocmask(SIG_SETMASK, old_set, NULL);
bellard9fddaa02004-05-21 12:59:32 +00001025 do_raise_exception_err(env->exception_index, env->error_code);
bellardce097762004-01-04 23:53:18 +00001026 } else {
1027 /* activate soft MMU for this block */
bellardfbf9eeb2004-04-25 21:21:33 +00001028 cpu_resume_from_signal(env, puc);
bellardce097762004-01-04 23:53:18 +00001029 }
bellard67867302003-11-23 17:05:30 +00001030 /* never comes here */
1031 return 1;
1032}
bellard6af0bf92005-07-02 14:58:51 +00001033
pbrooke6e59062006-10-22 00:18:54 +00001034#elif defined(TARGET_M68K)
1035static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1036 int is_write, sigset_t *old_set,
1037 void *puc)
1038{
1039 TranslationBlock *tb;
1040 int ret;
1041
1042 if (cpu_single_env)
1043 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1044#if defined(DEBUG_SIGNAL)
1045 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1046 pc, address, is_write, *(unsigned long *)old_set);
1047#endif
1048 /* XXX: locking issue */
1049 if (is_write && page_unprotect(address, pc, puc)) {
1050 return 1;
1051 }
1052 /* see if it is an MMU fault */
1053 ret = cpu_m68k_handle_mmu_fault(env, address, is_write, 1, 0);
1054 if (ret < 0)
1055 return 0; /* not an MMU fault */
1056 if (ret == 0)
1057 return 1; /* the MMU fault was handled without causing real CPU fault */
1058 /* now we have a real cpu fault */
1059 tb = tb_find_pc(pc);
1060 if (tb) {
1061 /* the PC is inside the translated code. It means that we have
1062 a virtual CPU fault */
1063 cpu_restore_state(tb, env, pc, puc);
1064 }
1065 /* we restore the process signal mask as the sigreturn should
1066 do it (XXX: use sigsetjmp) */
1067 sigprocmask(SIG_SETMASK, old_set, NULL);
1068 cpu_loop_exit();
1069 /* never comes here */
1070 return 1;
1071}
1072
bellard6af0bf92005-07-02 14:58:51 +00001073#elif defined (TARGET_MIPS)
1074static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1075 int is_write, sigset_t *old_set,
1076 void *puc)
1077{
1078 TranslationBlock *tb;
1079 int ret;
1080
1081 if (cpu_single_env)
1082 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1083#if defined(DEBUG_SIGNAL)
1084 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1085 pc, address, is_write, *(unsigned long *)old_set);
1086#endif
1087 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +00001088 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellard6af0bf92005-07-02 14:58:51 +00001089 return 1;
1090 }
1091
1092 /* see if it is an MMU fault */
bellardcc9442b2005-11-26 18:43:28 +00001093 ret = cpu_mips_handle_mmu_fault(env, address, is_write, 1, 0);
bellard6af0bf92005-07-02 14:58:51 +00001094 if (ret < 0)
1095 return 0; /* not an MMU fault */
1096 if (ret == 0)
1097 return 1; /* the MMU fault was handled without causing real CPU fault */
1098
1099 /* now we have a real cpu fault */
1100 tb = tb_find_pc(pc);
1101 if (tb) {
1102 /* the PC is inside the translated code. It means that we have
1103 a virtual CPU fault */
1104 cpu_restore_state(tb, env, pc, puc);
1105 }
1106 if (ret == 1) {
1107#if 0
1108 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1109 env->nip, env->error_code, tb);
1110#endif
1111 /* we restore the process signal mask as the sigreturn should
1112 do it (XXX: use sigsetjmp) */
1113 sigprocmask(SIG_SETMASK, old_set, NULL);
1114 do_raise_exception_err(env->exception_index, env->error_code);
1115 } else {
1116 /* activate soft MMU for this block */
1117 cpu_resume_from_signal(env, puc);
1118 }
1119 /* never comes here */
1120 return 1;
1121}
1122
bellardfdf9b3e2006-04-27 21:07:38 +00001123#elif defined (TARGET_SH4)
1124static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1125 int is_write, sigset_t *old_set,
1126 void *puc)
1127{
1128 TranslationBlock *tb;
1129 int ret;
1130
1131 if (cpu_single_env)
1132 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1133#if defined(DEBUG_SIGNAL)
1134 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1135 pc, address, is_write, *(unsigned long *)old_set);
1136#endif
1137 /* XXX: locking issue */
1138 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1139 return 1;
1140 }
1141
1142 /* see if it is an MMU fault */
1143 ret = cpu_sh4_handle_mmu_fault(env, address, is_write, 1, 0);
1144 if (ret < 0)
1145 return 0; /* not an MMU fault */
1146 if (ret == 0)
1147 return 1; /* the MMU fault was handled without causing real CPU fault */
1148
1149 /* now we have a real cpu fault */
1150 tb = tb_find_pc(pc);
1151 if (tb) {
1152 /* the PC is inside the translated code. It means that we have
1153 a virtual CPU fault */
1154 cpu_restore_state(tb, env, pc, puc);
1155 }
bellardfdf9b3e2006-04-27 21:07:38 +00001156#if 0
1157 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1158 env->nip, env->error_code, tb);
1159#endif
1160 /* we restore the process signal mask as the sigreturn should
1161 do it (XXX: use sigsetjmp) */
pbrook355fb232006-06-17 19:58:25 +00001162 sigprocmask(SIG_SETMASK, old_set, NULL);
1163 cpu_loop_exit();
bellardfdf9b3e2006-04-27 21:07:38 +00001164 /* never comes here */
1165 return 1;
1166}
bellarde4533c72003-06-15 19:51:39 +00001167#else
1168#error unsupported target CPU
1169#endif
bellard9de5e442003-03-23 16:49:39 +00001170
bellard2b413142003-05-14 23:01:10 +00001171#if defined(__i386__)
1172
bellardd8ecc0b2007-02-05 21:41:46 +00001173#if defined(__APPLE__)
1174# include <sys/ucontext.h>
1175
1176# define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
1177# define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
1178# define ERROR_sig(context) ((context)->uc_mcontext->es.err)
1179#else
1180# define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
1181# define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
1182# define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
1183#endif
1184
bellardbf3e8bf2004-02-16 21:58:54 +00001185#if defined(USE_CODE_COPY)
1186static void cpu_send_trap(unsigned long pc, int trap,
1187 struct ucontext *uc)
1188{
1189 TranslationBlock *tb;
1190
1191 if (cpu_single_env)
1192 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1193 /* now we have a real cpu fault */
1194 tb = tb_find_pc(pc);
1195 if (tb) {
1196 /* the PC is inside the translated code. It means that we have
1197 a virtual CPU fault */
1198 cpu_restore_state(tb, env, pc, uc);
1199 }
1200 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
1201 raise_exception_err(trap, env->error_code);
1202}
1203#endif
1204
ths5a7b5422007-01-31 12:16:51 +00001205int cpu_signal_handler(int host_signum, void *pinfo,
bellarde4533c72003-06-15 19:51:39 +00001206 void *puc)
bellard9de5e442003-03-23 16:49:39 +00001207{
ths5a7b5422007-01-31 12:16:51 +00001208 siginfo_t *info = pinfo;
bellard9de5e442003-03-23 16:49:39 +00001209 struct ucontext *uc = puc;
1210 unsigned long pc;
bellardbf3e8bf2004-02-16 21:58:54 +00001211 int trapno;
bellard97eb5b12004-02-25 23:19:55 +00001212
bellardd691f662003-03-24 21:58:34 +00001213#ifndef REG_EIP
1214/* for glibc 2.1 */
bellardfd6ce8f2003-05-14 19:00:11 +00001215#define REG_EIP EIP
1216#define REG_ERR ERR
1217#define REG_TRAPNO TRAPNO
bellardd691f662003-03-24 21:58:34 +00001218#endif
bellardd8ecc0b2007-02-05 21:41:46 +00001219 pc = EIP_sig(uc);
1220 trapno = TRAP_sig(uc);
bellardbf3e8bf2004-02-16 21:58:54 +00001221#if defined(TARGET_I386) && defined(USE_CODE_COPY)
1222 if (trapno == 0x00 || trapno == 0x05) {
1223 /* send division by zero or bound exception */
1224 cpu_send_trap(pc, trapno, uc);
1225 return 1;
1226 } else
1227#endif
1228 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1229 trapno == 0xe ?
bellardd8ecc0b2007-02-05 21:41:46 +00001230 (ERROR_sig(uc) >> 1) & 1 : 0,
bellardbf3e8bf2004-02-16 21:58:54 +00001231 &uc->uc_sigmask, puc);
bellard2b413142003-05-14 23:01:10 +00001232}
1233
bellardbc51c5c2004-03-17 23:46:04 +00001234#elif defined(__x86_64__)
1235
ths5a7b5422007-01-31 12:16:51 +00001236int cpu_signal_handler(int host_signum, void *pinfo,
bellardbc51c5c2004-03-17 23:46:04 +00001237 void *puc)
1238{
ths5a7b5422007-01-31 12:16:51 +00001239 siginfo_t *info = pinfo;
bellardbc51c5c2004-03-17 23:46:04 +00001240 struct ucontext *uc = puc;
1241 unsigned long pc;
1242
1243 pc = uc->uc_mcontext.gregs[REG_RIP];
1244 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1245 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
1246 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1247 &uc->uc_sigmask, puc);
1248}
1249
bellard83fb7ad2004-07-05 21:25:26 +00001250#elif defined(__powerpc__)
bellard2b413142003-05-14 23:01:10 +00001251
bellard83fb7ad2004-07-05 21:25:26 +00001252/***********************************************************************
1253 * signal context platform-specific definitions
1254 * From Wine
1255 */
1256#ifdef linux
1257/* All Registers access - only for local access */
1258# define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
1259/* Gpr Registers access */
1260# define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
1261# define IAR_sig(context) REG_sig(nip, context) /* Program counter */
1262# define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
1263# define CTR_sig(context) REG_sig(ctr, context) /* Count register */
1264# define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
1265# define LR_sig(context) REG_sig(link, context) /* Link register */
1266# define CR_sig(context) REG_sig(ccr, context) /* Condition register */
1267/* Float Registers access */
1268# define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1269# define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1270/* Exception Registers access */
1271# define DAR_sig(context) REG_sig(dar, context)
1272# define DSISR_sig(context) REG_sig(dsisr, context)
1273# define TRAP_sig(context) REG_sig(trap, context)
1274#endif /* linux */
1275
1276#ifdef __APPLE__
1277# include <sys/ucontext.h>
1278typedef struct ucontext SIGCONTEXT;
1279/* All Registers access - only for local access */
1280# define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
1281# define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
1282# define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
1283# define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
1284/* Gpr Registers access */
1285# define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
1286# define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
1287# define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
1288# define CTR_sig(context) REG_sig(ctr, context)
1289# define XER_sig(context) REG_sig(xer, context) /* Link register */
1290# define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
1291# define CR_sig(context) REG_sig(cr, context) /* Condition register */
1292/* Float Registers access */
1293# define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
1294# define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
1295/* Exception Registers access */
1296# define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
1297# define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
1298# define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1299#endif /* __APPLE__ */
1300
ths5a7b5422007-01-31 12:16:51 +00001301int cpu_signal_handler(int host_signum, void *pinfo,
bellarde4533c72003-06-15 19:51:39 +00001302 void *puc)
bellard2b413142003-05-14 23:01:10 +00001303{
ths5a7b5422007-01-31 12:16:51 +00001304 siginfo_t *info = pinfo;
bellard25eb4482003-05-14 21:50:54 +00001305 struct ucontext *uc = puc;
bellard25eb4482003-05-14 21:50:54 +00001306 unsigned long pc;
bellard25eb4482003-05-14 21:50:54 +00001307 int is_write;
1308
bellard83fb7ad2004-07-05 21:25:26 +00001309 pc = IAR_sig(uc);
bellard25eb4482003-05-14 21:50:54 +00001310 is_write = 0;
1311#if 0
1312 /* ppc 4xx case */
bellard83fb7ad2004-07-05 21:25:26 +00001313 if (DSISR_sig(uc) & 0x00800000)
bellard25eb4482003-05-14 21:50:54 +00001314 is_write = 1;
bellard9de5e442003-03-23 16:49:39 +00001315#else
bellard83fb7ad2004-07-05 21:25:26 +00001316 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
bellard25eb4482003-05-14 21:50:54 +00001317 is_write = 1;
1318#endif
1319 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001320 is_write, &uc->uc_sigmask, puc);
bellard9de5e442003-03-23 16:49:39 +00001321}
bellard2b413142003-05-14 23:01:10 +00001322
bellard2f87c602003-06-02 20:38:09 +00001323#elif defined(__alpha__)
1324
ths5a7b5422007-01-31 12:16:51 +00001325int cpu_signal_handler(int host_signum, void *pinfo,
bellard2f87c602003-06-02 20:38:09 +00001326 void *puc)
1327{
ths5a7b5422007-01-31 12:16:51 +00001328 siginfo_t *info = pinfo;
bellard2f87c602003-06-02 20:38:09 +00001329 struct ucontext *uc = puc;
1330 uint32_t *pc = uc->uc_mcontext.sc_pc;
1331 uint32_t insn = *pc;
1332 int is_write = 0;
1333
bellard8c6939c2003-06-09 15:28:00 +00001334 /* XXX: need kernel patch to get write flag faster */
bellard2f87c602003-06-02 20:38:09 +00001335 switch (insn >> 26) {
1336 case 0x0d: // stw
1337 case 0x0e: // stb
1338 case 0x0f: // stq_u
1339 case 0x24: // stf
1340 case 0x25: // stg
1341 case 0x26: // sts
1342 case 0x27: // stt
1343 case 0x2c: // stl
1344 case 0x2d: // stq
1345 case 0x2e: // stl_c
1346 case 0x2f: // stq_c
1347 is_write = 1;
1348 }
1349
1350 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001351 is_write, &uc->uc_sigmask, puc);
bellard2f87c602003-06-02 20:38:09 +00001352}
bellard8c6939c2003-06-09 15:28:00 +00001353#elif defined(__sparc__)
1354
ths5a7b5422007-01-31 12:16:51 +00001355int cpu_signal_handler(int host_signum, void *pinfo,
bellarde4533c72003-06-15 19:51:39 +00001356 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001357{
ths5a7b5422007-01-31 12:16:51 +00001358 siginfo_t *info = pinfo;
bellard8c6939c2003-06-09 15:28:00 +00001359 uint32_t *regs = (uint32_t *)(info + 1);
1360 void *sigmask = (regs + 20);
1361 unsigned long pc;
1362 int is_write;
1363 uint32_t insn;
1364
1365 /* XXX: is there a standard glibc define ? */
1366 pc = regs[1];
1367 /* XXX: need kernel patch to get write flag faster */
1368 is_write = 0;
1369 insn = *(uint32_t *)pc;
1370 if ((insn >> 30) == 3) {
1371 switch((insn >> 19) & 0x3f) {
1372 case 0x05: // stb
1373 case 0x06: // sth
1374 case 0x04: // st
1375 case 0x07: // std
1376 case 0x24: // stf
1377 case 0x27: // stdf
1378 case 0x25: // stfsr
1379 is_write = 1;
1380 break;
1381 }
1382 }
1383 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001384 is_write, sigmask, NULL);
bellard8c6939c2003-06-09 15:28:00 +00001385}
1386
1387#elif defined(__arm__)
1388
ths5a7b5422007-01-31 12:16:51 +00001389int cpu_signal_handler(int host_signum, void *pinfo,
bellarde4533c72003-06-15 19:51:39 +00001390 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001391{
ths5a7b5422007-01-31 12:16:51 +00001392 siginfo_t *info = pinfo;
bellard8c6939c2003-06-09 15:28:00 +00001393 struct ucontext *uc = puc;
1394 unsigned long pc;
1395 int is_write;
1396
1397 pc = uc->uc_mcontext.gregs[R15];
1398 /* XXX: compute is_write */
1399 is_write = 0;
1400 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1401 is_write,
pbrookf3a96762006-07-29 19:09:31 +00001402 &uc->uc_sigmask, puc);
bellard8c6939c2003-06-09 15:28:00 +00001403}
1404
bellard38e584a2003-08-10 22:14:22 +00001405#elif defined(__mc68000)
1406
ths5a7b5422007-01-31 12:16:51 +00001407int cpu_signal_handler(int host_signum, void *pinfo,
bellard38e584a2003-08-10 22:14:22 +00001408 void *puc)
1409{
ths5a7b5422007-01-31 12:16:51 +00001410 siginfo_t *info = pinfo;
bellard38e584a2003-08-10 22:14:22 +00001411 struct ucontext *uc = puc;
1412 unsigned long pc;
1413 int is_write;
1414
1415 pc = uc->uc_mcontext.gregs[16];
1416 /* XXX: compute is_write */
1417 is_write = 0;
1418 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1419 is_write,
bellardbf3e8bf2004-02-16 21:58:54 +00001420 &uc->uc_sigmask, puc);
bellard38e584a2003-08-10 22:14:22 +00001421}
1422
bellardb8076a72005-04-07 22:20:31 +00001423#elif defined(__ia64)
1424
1425#ifndef __ISR_VALID
1426 /* This ought to be in <bits/siginfo.h>... */
1427# define __ISR_VALID 1
bellardb8076a72005-04-07 22:20:31 +00001428#endif
1429
ths5a7b5422007-01-31 12:16:51 +00001430int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
bellardb8076a72005-04-07 22:20:31 +00001431{
ths5a7b5422007-01-31 12:16:51 +00001432 siginfo_t *info = pinfo;
bellardb8076a72005-04-07 22:20:31 +00001433 struct ucontext *uc = puc;
1434 unsigned long ip;
1435 int is_write = 0;
1436
1437 ip = uc->uc_mcontext.sc_ip;
1438 switch (host_signum) {
1439 case SIGILL:
1440 case SIGFPE:
1441 case SIGSEGV:
1442 case SIGBUS:
1443 case SIGTRAP:
bellardfd4a43e2006-04-24 20:32:17 +00001444 if (info->si_code && (info->si_segvflags & __ISR_VALID))
bellardb8076a72005-04-07 22:20:31 +00001445 /* ISR.W (write-access) is bit 33: */
1446 is_write = (info->si_isr >> 33) & 1;
1447 break;
1448
1449 default:
1450 break;
1451 }
1452 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1453 is_write,
1454 &uc->uc_sigmask, puc);
1455}
1456
bellard90cb9492005-07-24 15:11:38 +00001457#elif defined(__s390__)
1458
ths5a7b5422007-01-31 12:16:51 +00001459int cpu_signal_handler(int host_signum, void *pinfo,
bellard90cb9492005-07-24 15:11:38 +00001460 void *puc)
1461{
ths5a7b5422007-01-31 12:16:51 +00001462 siginfo_t *info = pinfo;
bellard90cb9492005-07-24 15:11:38 +00001463 struct ucontext *uc = puc;
1464 unsigned long pc;
1465 int is_write;
1466
1467 pc = uc->uc_mcontext.psw.addr;
1468 /* XXX: compute is_write */
1469 is_write = 0;
1470 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1471 is_write,
1472 &uc->uc_sigmask, puc);
1473}
1474
bellard2b413142003-05-14 23:01:10 +00001475#else
1476
bellard3fb2ded2003-06-24 13:22:59 +00001477#error host CPU specific signal handler needed
bellard2b413142003-05-14 23:01:10 +00001478
1479#endif
bellard67b915a2004-03-31 23:37:16 +00001480
1481#endif /* !defined(CONFIG_SOFTMMU) */