blob: d168886620f9224ba2ae72ac65a8f9fd593dee49 [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);
bellardce097762004-01-04 23:53:18 +0000363#elif defined(TARGET_PPC)
364 do_interrupt(env);
bellard6af0bf92005-07-02 14:58:51 +0000365#elif defined(TARGET_MIPS)
366 do_interrupt(env);
bellarde95c8d52004-09-30 22:22:08 +0000367#elif defined(TARGET_SPARC)
bellard1a0c3292005-02-13 19:02:07 +0000368 do_interrupt(env->exception_index);
bellardb5ff1b32005-11-26 10:38:39 +0000369#elif defined(TARGET_ARM)
370 do_interrupt(env);
bellardfdf9b3e2006-04-27 21:07:38 +0000371#elif defined(TARGET_SH4)
372 do_interrupt(env);
bellard83479e72003-06-25 16:12:37 +0000373#endif
bellard3fb2ded2003-06-24 13:22:59 +0000374 }
375 env->exception_index = -1;
bellard9df217a2005-02-10 22:05:51 +0000376 }
377#ifdef USE_KQEMU
378 if (kqemu_is_ok(env) && env->interrupt_request == 0) {
379 int ret;
380 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
381 ret = kqemu_cpu_exec(env);
382 /* put eflags in CPU temporary format */
383 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
384 DF = 1 - (2 * ((env->eflags >> 10) & 1));
385 CC_OP = CC_OP_EFLAGS;
386 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
387 if (ret == 1) {
388 /* exception */
389 longjmp(env->jmp_env, 1);
390 } else if (ret == 2) {
391 /* softmmu execution needed */
392 } else {
393 if (env->interrupt_request != 0) {
394 /* hardware interrupt will be executed just after */
395 } else {
396 /* otherwise, we restart */
397 longjmp(env->jmp_env, 1);
398 }
399 }
bellard9de5e442003-03-23 16:49:39 +0000400 }
bellard9df217a2005-02-10 22:05:51 +0000401#endif
402
bellard3fb2ded2003-06-24 13:22:59 +0000403 T0 = 0; /* force lookup of first TB */
404 for(;;) {
bellardfdbb4692006-06-14 17:32:25 +0000405#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard3fb2ded2003-06-24 13:22:59 +0000406 /* g1 can be modified by some libc? functions */
407 tmp_T0 = T0;
408#endif
bellard68a79312003-06-30 13:12:32 +0000409 interrupt_request = env->interrupt_request;
bellard2e255c62003-08-21 23:25:21 +0000410 if (__builtin_expect(interrupt_request, 0)) {
pbrook6658ffb2007-03-16 23:58:11 +0000411 if (interrupt_request & CPU_INTERRUPT_DEBUG) {
412 env->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
413 env->exception_index = EXCP_DEBUG;
414 cpu_loop_exit();
415 }
bellard68a79312003-06-30 13:12:32 +0000416#if defined(TARGET_I386)
bellard3b21e032006-09-24 18:41:56 +0000417 if ((interrupt_request & CPU_INTERRUPT_SMI) &&
418 !(env->hflags & HF_SMM_MASK)) {
419 env->interrupt_request &= ~CPU_INTERRUPT_SMI;
420 do_smm_enter();
421#if defined(__sparc__) && !defined(HOST_SOLARIS)
422 tmp_T0 = 0;
423#else
424 T0 = 0;
425#endif
426 } else if ((interrupt_request & CPU_INTERRUPT_HARD) &&
bellard3f337312003-08-20 23:02:09 +0000427 (env->eflags & IF_MASK) &&
428 !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
bellard68a79312003-06-30 13:12:32 +0000429 int intno;
bellardfbf9eeb2004-04-25 21:21:33 +0000430 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellarda541f292004-04-12 20:39:29 +0000431 intno = cpu_get_pic_interrupt(env);
bellardf193c792004-03-21 17:06:25 +0000432 if (loglevel & CPU_LOG_TB_IN_ASM) {
bellard68a79312003-06-30 13:12:32 +0000433 fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
434 }
bellardd05e66d2003-08-20 21:34:35 +0000435 do_interrupt(intno, 0, 0, 0, 1);
bellard907a5b22003-06-30 23:18:22 +0000436 /* ensure that no TB jump will be modified as
437 the program flow was changed */
bellardfdbb4692006-06-14 17:32:25 +0000438#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard907a5b22003-06-30 23:18:22 +0000439 tmp_T0 = 0;
440#else
441 T0 = 0;
442#endif
bellard68a79312003-06-30 13:12:32 +0000443 }
bellardce097762004-01-04 23:53:18 +0000444#elif defined(TARGET_PPC)
bellard9fddaa02004-05-21 12:59:32 +0000445#if 0
446 if ((interrupt_request & CPU_INTERRUPT_RESET)) {
447 cpu_ppc_reset(env);
448 }
449#endif
j_mayer47103572007-03-30 09:38:04 +0000450 if (interrupt_request & CPU_INTERRUPT_HARD) {
451 if (ppc_hw_interrupt(env) == 1) {
452 /* Some exception was raised */
453 if (env->pending_interrupts == 0)
454 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellardfdbb4692006-06-14 17:32:25 +0000455#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8a40a182005-11-20 10:35:40 +0000456 tmp_T0 = 0;
457#else
458 T0 = 0;
459#endif
460 }
bellardce097762004-01-04 23:53:18 +0000461 }
bellard6af0bf92005-07-02 14:58:51 +0000462#elif defined(TARGET_MIPS)
463 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
ths24c7b0e2007-03-30 16:44:54 +0000464 (env->CP0_Status & env->CP0_Cause & CP0Ca_IP_mask) &&
bellard6af0bf92005-07-02 14:58:51 +0000465 (env->CP0_Status & (1 << CP0St_IE)) &&
ths24c7b0e2007-03-30 16:44:54 +0000466 !(env->CP0_Status & (1 << CP0St_EXL)) &&
467 !(env->CP0_Status & (1 << CP0St_ERL)) &&
bellard6af0bf92005-07-02 14:58:51 +0000468 !(env->hflags & MIPS_HFLAG_DM)) {
469 /* Raise it */
470 env->exception_index = EXCP_EXT_INTERRUPT;
471 env->error_code = 0;
472 do_interrupt(env);
bellardfdbb4692006-06-14 17:32:25 +0000473#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8a40a182005-11-20 10:35:40 +0000474 tmp_T0 = 0;
475#else
476 T0 = 0;
477#endif
bellard6af0bf92005-07-02 14:58:51 +0000478 }
bellarde95c8d52004-09-30 22:22:08 +0000479#elif defined(TARGET_SPARC)
bellard66321a12005-04-06 20:47:48 +0000480 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
481 (env->psret != 0)) {
482 int pil = env->interrupt_index & 15;
483 int type = env->interrupt_index & 0xf0;
484
485 if (((type == TT_EXTINT) &&
486 (pil == 15 || pil > env->psrpil)) ||
487 type != TT_EXTINT) {
488 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
489 do_interrupt(env->interrupt_index);
490 env->interrupt_index = 0;
bellardfdbb4692006-06-14 17:32:25 +0000491#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8a40a182005-11-20 10:35:40 +0000492 tmp_T0 = 0;
493#else
494 T0 = 0;
495#endif
bellard66321a12005-04-06 20:47:48 +0000496 }
bellarde95c8d52004-09-30 22:22:08 +0000497 } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
498 //do_interrupt(0, 0, 0, 0, 0);
499 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
bellardba3c64f2005-12-05 20:31:52 +0000500 } else if (interrupt_request & CPU_INTERRUPT_HALT) {
bellarddf52b002006-09-20 20:30:57 +0000501 env->interrupt_request &= ~CPU_INTERRUPT_HALT;
502 env->halted = 1;
503 env->exception_index = EXCP_HLT;
504 cpu_loop_exit();
bellardba3c64f2005-12-05 20:31:52 +0000505 }
bellardb5ff1b32005-11-26 10:38:39 +0000506#elif defined(TARGET_ARM)
507 if (interrupt_request & CPU_INTERRUPT_FIQ
508 && !(env->uncached_cpsr & CPSR_F)) {
509 env->exception_index = EXCP_FIQ;
510 do_interrupt(env);
511 }
512 if (interrupt_request & CPU_INTERRUPT_HARD
513 && !(env->uncached_cpsr & CPSR_I)) {
514 env->exception_index = EXCP_IRQ;
515 do_interrupt(env);
516 }
bellardfdf9b3e2006-04-27 21:07:38 +0000517#elif defined(TARGET_SH4)
518 /* XXXXX */
bellard68a79312003-06-30 13:12:32 +0000519#endif
bellard9d050952006-05-22 22:03:52 +0000520 /* Don't use the cached interupt_request value,
521 do_interrupt may have updated the EXITTB flag. */
bellardb5ff1b32005-11-26 10:38:39 +0000522 if (env->interrupt_request & CPU_INTERRUPT_EXITTB) {
bellardbf3e8bf2004-02-16 21:58:54 +0000523 env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
524 /* ensure that no TB jump will be modified as
525 the program flow was changed */
bellardfdbb4692006-06-14 17:32:25 +0000526#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellardbf3e8bf2004-02-16 21:58:54 +0000527 tmp_T0 = 0;
528#else
529 T0 = 0;
530#endif
531 }
bellard68a79312003-06-30 13:12:32 +0000532 if (interrupt_request & CPU_INTERRUPT_EXIT) {
533 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
534 env->exception_index = EXCP_INTERRUPT;
535 cpu_loop_exit();
536 }
bellard3fb2ded2003-06-24 13:22:59 +0000537 }
538#ifdef DEBUG_EXEC
bellardb5ff1b32005-11-26 10:38:39 +0000539 if ((loglevel & CPU_LOG_TB_CPU)) {
bellard3fb2ded2003-06-24 13:22:59 +0000540#if defined(TARGET_I386)
541 /* restore flags in standard format */
bellardfc9f7152005-04-26 19:33:35 +0000542#ifdef reg_EAX
bellard3fb2ded2003-06-24 13:22:59 +0000543 env->regs[R_EAX] = EAX;
bellardfc9f7152005-04-26 19:33:35 +0000544#endif
545#ifdef reg_EBX
bellard3fb2ded2003-06-24 13:22:59 +0000546 env->regs[R_EBX] = EBX;
bellardfc9f7152005-04-26 19:33:35 +0000547#endif
548#ifdef reg_ECX
bellard3fb2ded2003-06-24 13:22:59 +0000549 env->regs[R_ECX] = ECX;
bellardfc9f7152005-04-26 19:33:35 +0000550#endif
551#ifdef reg_EDX
bellard3fb2ded2003-06-24 13:22:59 +0000552 env->regs[R_EDX] = EDX;
bellardfc9f7152005-04-26 19:33:35 +0000553#endif
554#ifdef reg_ESI
bellard3fb2ded2003-06-24 13:22:59 +0000555 env->regs[R_ESI] = ESI;
bellardfc9f7152005-04-26 19:33:35 +0000556#endif
557#ifdef reg_EDI
bellard3fb2ded2003-06-24 13:22:59 +0000558 env->regs[R_EDI] = EDI;
bellardfc9f7152005-04-26 19:33:35 +0000559#endif
560#ifdef reg_EBP
bellard3fb2ded2003-06-24 13:22:59 +0000561 env->regs[R_EBP] = EBP;
bellardfc9f7152005-04-26 19:33:35 +0000562#endif
563#ifdef reg_ESP
bellard3fb2ded2003-06-24 13:22:59 +0000564 env->regs[R_ESP] = ESP;
bellardfc9f7152005-04-26 19:33:35 +0000565#endif
bellard3fb2ded2003-06-24 13:22:59 +0000566 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard7fe48482004-10-09 18:08:01 +0000567 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
bellard3fb2ded2003-06-24 13:22:59 +0000568 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000569#elif defined(TARGET_ARM)
bellard7fe48482004-10-09 18:08:01 +0000570 cpu_dump_state(env, logfile, fprintf, 0);
bellard93ac68b2003-09-30 20:57:29 +0000571#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000572 REGWPTR = env->regbase + (env->cwp * 16);
573 env->regwptr = REGWPTR;
574 cpu_dump_state(env, logfile, fprintf, 0);
bellard67867302003-11-23 17:05:30 +0000575#elif defined(TARGET_PPC)
bellard7fe48482004-10-09 18:08:01 +0000576 cpu_dump_state(env, logfile, fprintf, 0);
pbrooke6e59062006-10-22 00:18:54 +0000577#elif defined(TARGET_M68K)
578 cpu_m68k_flush_flags(env, env->cc_op);
579 env->cc_op = CC_OP_FLAGS;
580 env->sr = (env->sr & 0xffe0)
581 | env->cc_dest | (env->cc_x << 4);
582 cpu_dump_state(env, logfile, fprintf, 0);
bellard6af0bf92005-07-02 14:58:51 +0000583#elif defined(TARGET_MIPS)
584 cpu_dump_state(env, logfile, fprintf, 0);
bellardfdf9b3e2006-04-27 21:07:38 +0000585#elif defined(TARGET_SH4)
586 cpu_dump_state(env, logfile, fprintf, 0);
bellarde4533c72003-06-15 19:51:39 +0000587#else
588#error unsupported target CPU
589#endif
bellard3fb2ded2003-06-24 13:22:59 +0000590 }
bellard7d132992003-03-06 23:23:54 +0000591#endif
bellard8a40a182005-11-20 10:35:40 +0000592 tb = tb_find_fast();
bellard9d27abd2003-05-10 13:13:54 +0000593#ifdef DEBUG_EXEC
bellardc1135f62005-01-30 22:41:54 +0000594 if ((loglevel & CPU_LOG_EXEC)) {
bellardc27004e2005-01-03 23:35:10 +0000595 fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
596 (long)tb->tc_ptr, tb->pc,
597 lookup_symbol(tb->pc));
bellard3fb2ded2003-06-24 13:22:59 +0000598 }
bellard9d27abd2003-05-10 13:13:54 +0000599#endif
bellardfdbb4692006-06-14 17:32:25 +0000600#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard3fb2ded2003-06-24 13:22:59 +0000601 T0 = tmp_T0;
bellard8c6939c2003-06-09 15:28:00 +0000602#endif
bellard8a40a182005-11-20 10:35:40 +0000603 /* see if we can patch the calling TB. When the TB
604 spans two pages, we cannot safely do a direct
605 jump. */
bellardc27004e2005-01-03 23:35:10 +0000606 {
bellard8a40a182005-11-20 10:35:40 +0000607 if (T0 != 0 &&
bellardf32fc642006-02-08 22:43:39 +0000608#if USE_KQEMU
609 (env->kqemu_enabled != 2) &&
610#endif
bellard8a40a182005-11-20 10:35:40 +0000611 tb->page_addr[1] == -1
bellardbf3e8bf2004-02-16 21:58:54 +0000612#if defined(TARGET_I386) && defined(USE_CODE_COPY)
613 && (tb->cflags & CF_CODE_COPY) ==
614 (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
615#endif
616 ) {
bellard3fb2ded2003-06-24 13:22:59 +0000617 spin_lock(&tb_lock);
bellardc27004e2005-01-03 23:35:10 +0000618 tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
bellard97eb5b12004-02-25 23:19:55 +0000619#if defined(USE_CODE_COPY)
620 /* propagates the FP use info */
621 ((TranslationBlock *)(T0 & ~3))->cflags |=
622 (tb->cflags & CF_FP_USED);
623#endif
bellard3fb2ded2003-06-24 13:22:59 +0000624 spin_unlock(&tb_lock);
625 }
bellardc27004e2005-01-03 23:35:10 +0000626 }
bellard3fb2ded2003-06-24 13:22:59 +0000627 tc_ptr = tb->tc_ptr;
bellard83479e72003-06-25 16:12:37 +0000628 env->current_tb = tb;
bellard3fb2ded2003-06-24 13:22:59 +0000629 /* execute the generated code */
630 gen_func = (void *)tc_ptr;
631#if defined(__sparc__)
632 __asm__ __volatile__("call %0\n\t"
633 "mov %%o7,%%i0"
634 : /* no outputs */
635 : "r" (gen_func)
bellardfdbb4692006-06-14 17:32:25 +0000636 : "i0", "i1", "i2", "i3", "i4", "i5",
thsfaab7592007-03-19 20:39:49 +0000637 "o0", "o1", "o2", "o3", "o4", "o5",
bellardfdbb4692006-06-14 17:32:25 +0000638 "l0", "l1", "l2", "l3", "l4", "l5",
639 "l6", "l7");
bellard3fb2ded2003-06-24 13:22:59 +0000640#elif defined(__arm__)
641 asm volatile ("mov pc, %0\n\t"
642 ".global exec_loop\n\t"
643 "exec_loop:\n\t"
644 : /* no outputs */
645 : "r" (gen_func)
646 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
bellardbf3e8bf2004-02-16 21:58:54 +0000647#elif defined(TARGET_I386) && defined(USE_CODE_COPY)
648{
649 if (!(tb->cflags & CF_CODE_COPY)) {
bellard97eb5b12004-02-25 23:19:55 +0000650 if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
651 save_native_fp_state(env);
652 }
bellardbf3e8bf2004-02-16 21:58:54 +0000653 gen_func();
654 } else {
bellard97eb5b12004-02-25 23:19:55 +0000655 if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
656 restore_native_fp_state(env);
657 }
bellardbf3e8bf2004-02-16 21:58:54 +0000658 /* we work with native eflags */
659 CC_SRC = cc_table[CC_OP].compute_all();
660 CC_OP = CC_OP_EFLAGS;
661 asm(".globl exec_loop\n"
662 "\n"
663 "debug1:\n"
664 " pushl %%ebp\n"
665 " fs movl %10, %9\n"
666 " fs movl %11, %%eax\n"
667 " andl $0x400, %%eax\n"
668 " fs orl %8, %%eax\n"
669 " pushl %%eax\n"
670 " popf\n"
671 " fs movl %%esp, %12\n"
672 " fs movl %0, %%eax\n"
673 " fs movl %1, %%ecx\n"
674 " fs movl %2, %%edx\n"
675 " fs movl %3, %%ebx\n"
676 " fs movl %4, %%esp\n"
677 " fs movl %5, %%ebp\n"
678 " fs movl %6, %%esi\n"
679 " fs movl %7, %%edi\n"
680 " fs jmp *%9\n"
681 "exec_loop:\n"
682 " fs movl %%esp, %4\n"
683 " fs movl %12, %%esp\n"
684 " fs movl %%eax, %0\n"
685 " fs movl %%ecx, %1\n"
686 " fs movl %%edx, %2\n"
687 " fs movl %%ebx, %3\n"
688 " fs movl %%ebp, %5\n"
689 " fs movl %%esi, %6\n"
690 " fs movl %%edi, %7\n"
691 " pushf\n"
692 " popl %%eax\n"
693 " movl %%eax, %%ecx\n"
694 " andl $0x400, %%ecx\n"
695 " shrl $9, %%ecx\n"
696 " andl $0x8d5, %%eax\n"
697 " fs movl %%eax, %8\n"
698 " movl $1, %%eax\n"
699 " subl %%ecx, %%eax\n"
700 " fs movl %%eax, %11\n"
701 " fs movl %9, %%ebx\n" /* get T0 value */
702 " popl %%ebp\n"
703 :
704 : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
705 "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
706 "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
707 "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
708 "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
709 "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
710 "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
711 "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
712 "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
713 "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
714 "a" (gen_func),
715 "m" (*(uint8_t *)offsetof(CPUState, df)),
716 "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
717 : "%ecx", "%edx"
718 );
719 }
720}
bellardb8076a72005-04-07 22:20:31 +0000721#elif defined(__ia64)
722 struct fptr {
723 void *ip;
724 void *gp;
725 } fp;
726
727 fp.ip = tc_ptr;
728 fp.gp = code_gen_buffer + 2 * (1 << 20);
729 (*(void (*)(void)) &fp)();
bellard3fb2ded2003-06-24 13:22:59 +0000730#else
731 gen_func();
732#endif
bellard83479e72003-06-25 16:12:37 +0000733 env->current_tb = NULL;
bellard4cbf74b2003-08-10 21:48:43 +0000734 /* reset soft MMU for next block (it can currently
735 only be set by a memory fault) */
736#if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
bellard3f337312003-08-20 23:02:09 +0000737 if (env->hflags & HF_SOFTMMU_MASK) {
738 env->hflags &= ~HF_SOFTMMU_MASK;
bellard4cbf74b2003-08-10 21:48:43 +0000739 /* do not allow linking to another block */
740 T0 = 0;
741 }
742#endif
bellardf32fc642006-02-08 22:43:39 +0000743#if defined(USE_KQEMU)
744#define MIN_CYCLE_BEFORE_SWITCH (100 * 1000)
745 if (kqemu_is_ok(env) &&
746 (cpu_get_time_fast() - env->last_io_time) >= MIN_CYCLE_BEFORE_SWITCH) {
747 cpu_loop_exit();
748 }
749#endif
bellard3fb2ded2003-06-24 13:22:59 +0000750 }
751 } else {
bellard0d1a29f2004-10-12 22:01:28 +0000752 env_to_regs();
bellard7d132992003-03-06 23:23:54 +0000753 }
bellard3fb2ded2003-06-24 13:22:59 +0000754 } /* for(;;) */
755
bellard7d132992003-03-06 23:23:54 +0000756
bellarde4533c72003-06-15 19:51:39 +0000757#if defined(TARGET_I386)
bellard97eb5b12004-02-25 23:19:55 +0000758#if defined(USE_CODE_COPY)
759 if (env->native_fp_regs) {
760 save_native_fp_state(env);
761 }
762#endif
bellard9de5e442003-03-23 16:49:39 +0000763 /* restore flags in standard format */
bellardfc2b4c42003-03-29 16:52:44 +0000764 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellarde4533c72003-06-15 19:51:39 +0000765#elif defined(TARGET_ARM)
bellardb7bcbe92005-02-22 19:27:29 +0000766 /* XXX: Save/restore host fpu exception state?. */
bellard93ac68b2003-09-30 20:57:29 +0000767#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000768#if defined(reg_REGWPTR)
769 REGWPTR = saved_regwptr;
770#endif
bellard67867302003-11-23 17:05:30 +0000771#elif defined(TARGET_PPC)
pbrooke6e59062006-10-22 00:18:54 +0000772#elif defined(TARGET_M68K)
773 cpu_m68k_flush_flags(env, env->cc_op);
774 env->cc_op = CC_OP_FLAGS;
775 env->sr = (env->sr & 0xffe0)
776 | env->cc_dest | (env->cc_x << 4);
bellard6af0bf92005-07-02 14:58:51 +0000777#elif defined(TARGET_MIPS)
bellardfdf9b3e2006-04-27 21:07:38 +0000778#elif defined(TARGET_SH4)
779 /* XXXXX */
bellarde4533c72003-06-15 19:51:39 +0000780#else
781#error unsupported target CPU
782#endif
pbrook1057eaa2007-02-04 13:37:44 +0000783
784 /* restore global registers */
bellardfdbb4692006-06-14 17:32:25 +0000785#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8c6939c2003-06-09 15:28:00 +0000786 asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
787#endif
pbrook1057eaa2007-02-04 13:37:44 +0000788#include "hostregs_helper.h"
789
bellard6a00d602005-11-21 23:25:50 +0000790 /* fail safe : never use cpu_single_env outside cpu_exec() */
791 cpu_single_env = NULL;
bellard7d132992003-03-06 23:23:54 +0000792 return ret;
793}
bellard6dbad632003-03-16 18:05:05 +0000794
bellardfbf9eeb2004-04-25 21:21:33 +0000795/* must only be called from the generated code as an exception can be
796 generated */
797void tb_invalidate_page_range(target_ulong start, target_ulong end)
798{
bellarddc5d0b32004-06-22 18:43:30 +0000799 /* XXX: cannot enable it yet because it yields to MMU exception
800 where NIP != read address on PowerPC */
801#if 0
bellardfbf9eeb2004-04-25 21:21:33 +0000802 target_ulong phys_addr;
803 phys_addr = get_phys_addr_code(env, start);
804 tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
bellarddc5d0b32004-06-22 18:43:30 +0000805#endif
bellardfbf9eeb2004-04-25 21:21:33 +0000806}
807
bellard1a18c712003-10-30 01:07:51 +0000808#if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
bellarde4533c72003-06-15 19:51:39 +0000809
bellard6dbad632003-03-16 18:05:05 +0000810void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
811{
812 CPUX86State *saved_env;
813
814 saved_env = env;
815 env = s;
bellarda412ac52003-07-26 18:01:40 +0000816 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
bellarda513fe12003-05-27 23:29:48 +0000817 selector &= 0xffff;
bellard2e255c62003-08-21 23:25:21 +0000818 cpu_x86_load_seg_cache(env, seg_reg, selector,
bellardc27004e2005-01-03 23:35:10 +0000819 (selector << 4), 0xffff, 0);
bellarda513fe12003-05-27 23:29:48 +0000820 } else {
bellardb453b702004-01-04 15:45:21 +0000821 load_seg(seg_reg, selector);
bellarda513fe12003-05-27 23:29:48 +0000822 }
bellard6dbad632003-03-16 18:05:05 +0000823 env = saved_env;
824}
bellard9de5e442003-03-23 16:49:39 +0000825
bellardd0a1ffc2003-05-29 20:04:28 +0000826void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
827{
828 CPUX86State *saved_env;
829
830 saved_env = env;
831 env = s;
832
bellardc27004e2005-01-03 23:35:10 +0000833 helper_fsave((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000834
835 env = saved_env;
836}
837
838void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
839{
840 CPUX86State *saved_env;
841
842 saved_env = env;
843 env = s;
844
bellardc27004e2005-01-03 23:35:10 +0000845 helper_frstor((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000846
847 env = saved_env;
848}
849
bellarde4533c72003-06-15 19:51:39 +0000850#endif /* TARGET_I386 */
851
bellard67b915a2004-03-31 23:37:16 +0000852#if !defined(CONFIG_SOFTMMU)
853
bellard3fb2ded2003-06-24 13:22:59 +0000854#if defined(TARGET_I386)
855
bellardb56dad12003-05-08 15:38:04 +0000856/* 'pc' is the host PC at which the exception was raised. 'address' is
bellardfd6ce8f2003-05-14 19:00:11 +0000857 the effective address of the memory exception. 'is_write' is 1 if a
858 write caused the exception and otherwise 0'. 'old_set' is the
859 signal set which should be restored */
bellard2b413142003-05-14 23:01:10 +0000860static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000861 int is_write, sigset_t *old_set,
862 void *puc)
bellard9de5e442003-03-23 16:49:39 +0000863{
bellarda513fe12003-05-27 23:29:48 +0000864 TranslationBlock *tb;
865 int ret;
bellard68a79312003-06-30 13:12:32 +0000866
bellard83479e72003-06-25 16:12:37 +0000867 if (cpu_single_env)
868 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellardfd6ce8f2003-05-14 19:00:11 +0000869#if defined(DEBUG_SIGNAL)
bellardbf3e8bf2004-02-16 21:58:54 +0000870 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
871 pc, address, is_write, *(unsigned long *)old_set);
bellard9de5e442003-03-23 16:49:39 +0000872#endif
bellard25eb4482003-05-14 21:50:54 +0000873 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000874 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellardfd6ce8f2003-05-14 19:00:11 +0000875 return 1;
876 }
bellardfbf9eeb2004-04-25 21:21:33 +0000877
bellard3fb2ded2003-06-24 13:22:59 +0000878 /* see if it is an MMU fault */
bellard93a40ea2003-10-27 21:13:06 +0000879 ret = cpu_x86_handle_mmu_fault(env, address, is_write,
880 ((env->hflags & HF_CPL_MASK) == 3), 0);
bellard3fb2ded2003-06-24 13:22:59 +0000881 if (ret < 0)
882 return 0; /* not an MMU fault */
883 if (ret == 0)
884 return 1; /* the MMU fault was handled without causing real CPU fault */
885 /* now we have a real cpu fault */
bellarda513fe12003-05-27 23:29:48 +0000886 tb = tb_find_pc(pc);
887 if (tb) {
bellard9de5e442003-03-23 16:49:39 +0000888 /* the PC is inside the translated code. It means that we have
889 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +0000890 cpu_restore_state(tb, env, pc, puc);
bellard3fb2ded2003-06-24 13:22:59 +0000891 }
bellard4cbf74b2003-08-10 21:48:43 +0000892 if (ret == 1) {
bellard3fb2ded2003-06-24 13:22:59 +0000893#if 0
bellard4cbf74b2003-08-10 21:48:43 +0000894 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
895 env->eip, env->cr[2], env->error_code);
bellard3fb2ded2003-06-24 13:22:59 +0000896#endif
bellard4cbf74b2003-08-10 21:48:43 +0000897 /* we restore the process signal mask as the sigreturn should
898 do it (XXX: use sigsetjmp) */
899 sigprocmask(SIG_SETMASK, old_set, NULL);
bellard54ca9092005-12-04 18:46:06 +0000900 raise_exception_err(env->exception_index, env->error_code);
bellard4cbf74b2003-08-10 21:48:43 +0000901 } else {
902 /* activate soft MMU for this block */
bellard3f337312003-08-20 23:02:09 +0000903 env->hflags |= HF_SOFTMMU_MASK;
bellardfbf9eeb2004-04-25 21:21:33 +0000904 cpu_resume_from_signal(env, puc);
bellard4cbf74b2003-08-10 21:48:43 +0000905 }
bellard3fb2ded2003-06-24 13:22:59 +0000906 /* never comes here */
907 return 1;
908}
909
bellarde4533c72003-06-15 19:51:39 +0000910#elif defined(TARGET_ARM)
bellard3fb2ded2003-06-24 13:22:59 +0000911static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000912 int is_write, sigset_t *old_set,
913 void *puc)
bellard3fb2ded2003-06-24 13:22:59 +0000914{
bellard68016c62005-02-07 23:12:27 +0000915 TranslationBlock *tb;
916 int ret;
917
918 if (cpu_single_env)
919 env = cpu_single_env; /* XXX: find a correct solution for multithread */
920#if defined(DEBUG_SIGNAL)
921 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
922 pc, address, is_write, *(unsigned long *)old_set);
923#endif
bellard9f0777e2005-02-02 20:42:01 +0000924 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000925 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellard9f0777e2005-02-02 20:42:01 +0000926 return 1;
927 }
bellard68016c62005-02-07 23:12:27 +0000928 /* see if it is an MMU fault */
929 ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
930 if (ret < 0)
931 return 0; /* not an MMU fault */
932 if (ret == 0)
933 return 1; /* the MMU fault was handled without causing real CPU fault */
934 /* now we have a real cpu fault */
935 tb = tb_find_pc(pc);
936 if (tb) {
937 /* the PC is inside the translated code. It means that we have
938 a virtual CPU fault */
939 cpu_restore_state(tb, env, pc, puc);
940 }
941 /* we restore the process signal mask as the sigreturn should
942 do it (XXX: use sigsetjmp) */
943 sigprocmask(SIG_SETMASK, old_set, NULL);
944 cpu_loop_exit();
bellard3fb2ded2003-06-24 13:22:59 +0000945}
bellard93ac68b2003-09-30 20:57:29 +0000946#elif defined(TARGET_SPARC)
947static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000948 int is_write, sigset_t *old_set,
949 void *puc)
bellard93ac68b2003-09-30 20:57:29 +0000950{
bellard68016c62005-02-07 23:12:27 +0000951 TranslationBlock *tb;
952 int ret;
953
954 if (cpu_single_env)
955 env = cpu_single_env; /* XXX: find a correct solution for multithread */
956#if defined(DEBUG_SIGNAL)
957 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
958 pc, address, is_write, *(unsigned long *)old_set);
959#endif
bellardb453b702004-01-04 15:45:21 +0000960 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000961 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellardb453b702004-01-04 15:45:21 +0000962 return 1;
963 }
bellard68016c62005-02-07 23:12:27 +0000964 /* see if it is an MMU fault */
965 ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
966 if (ret < 0)
967 return 0; /* not an MMU fault */
968 if (ret == 0)
969 return 1; /* the MMU fault was handled without causing real CPU fault */
970 /* now we have a real cpu fault */
971 tb = tb_find_pc(pc);
972 if (tb) {
973 /* the PC is inside the translated code. It means that we have
974 a virtual CPU fault */
975 cpu_restore_state(tb, env, pc, puc);
976 }
977 /* we restore the process signal mask as the sigreturn should
978 do it (XXX: use sigsetjmp) */
979 sigprocmask(SIG_SETMASK, old_set, NULL);
980 cpu_loop_exit();
bellard93ac68b2003-09-30 20:57:29 +0000981}
bellard67867302003-11-23 17:05:30 +0000982#elif defined (TARGET_PPC)
983static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000984 int is_write, sigset_t *old_set,
985 void *puc)
bellard67867302003-11-23 17:05:30 +0000986{
987 TranslationBlock *tb;
bellardce097762004-01-04 23:53:18 +0000988 int ret;
bellard67867302003-11-23 17:05:30 +0000989
bellard67867302003-11-23 17:05:30 +0000990 if (cpu_single_env)
991 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellard67867302003-11-23 17:05:30 +0000992#if defined(DEBUG_SIGNAL)
993 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
994 pc, address, is_write, *(unsigned long *)old_set);
995#endif
996 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000997 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellard67867302003-11-23 17:05:30 +0000998 return 1;
999 }
1000
bellardce097762004-01-04 23:53:18 +00001001 /* see if it is an MMU fault */
bellard7f957d22004-01-18 23:19:48 +00001002 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
bellardce097762004-01-04 23:53:18 +00001003 if (ret < 0)
1004 return 0; /* not an MMU fault */
1005 if (ret == 0)
1006 return 1; /* the MMU fault was handled without causing real CPU fault */
1007
bellard67867302003-11-23 17:05:30 +00001008 /* now we have a real cpu fault */
1009 tb = tb_find_pc(pc);
1010 if (tb) {
1011 /* the PC is inside the translated code. It means that we have
1012 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +00001013 cpu_restore_state(tb, env, pc, puc);
bellard67867302003-11-23 17:05:30 +00001014 }
bellardce097762004-01-04 23:53:18 +00001015 if (ret == 1) {
bellard67867302003-11-23 17:05:30 +00001016#if 0
bellardce097762004-01-04 23:53:18 +00001017 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1018 env->nip, env->error_code, tb);
bellard67867302003-11-23 17:05:30 +00001019#endif
1020 /* we restore the process signal mask as the sigreturn should
1021 do it (XXX: use sigsetjmp) */
bellardbf3e8bf2004-02-16 21:58:54 +00001022 sigprocmask(SIG_SETMASK, old_set, NULL);
bellard9fddaa02004-05-21 12:59:32 +00001023 do_raise_exception_err(env->exception_index, env->error_code);
bellardce097762004-01-04 23:53:18 +00001024 } else {
1025 /* activate soft MMU for this block */
bellardfbf9eeb2004-04-25 21:21:33 +00001026 cpu_resume_from_signal(env, puc);
bellardce097762004-01-04 23:53:18 +00001027 }
bellard67867302003-11-23 17:05:30 +00001028 /* never comes here */
1029 return 1;
1030}
bellard6af0bf92005-07-02 14:58:51 +00001031
pbrooke6e59062006-10-22 00:18:54 +00001032#elif defined(TARGET_M68K)
1033static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1034 int is_write, sigset_t *old_set,
1035 void *puc)
1036{
1037 TranslationBlock *tb;
1038 int ret;
1039
1040 if (cpu_single_env)
1041 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1042#if defined(DEBUG_SIGNAL)
1043 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1044 pc, address, is_write, *(unsigned long *)old_set);
1045#endif
1046 /* XXX: locking issue */
1047 if (is_write && page_unprotect(address, pc, puc)) {
1048 return 1;
1049 }
1050 /* see if it is an MMU fault */
1051 ret = cpu_m68k_handle_mmu_fault(env, address, is_write, 1, 0);
1052 if (ret < 0)
1053 return 0; /* not an MMU fault */
1054 if (ret == 0)
1055 return 1; /* the MMU fault was handled without causing real CPU fault */
1056 /* now we have a real cpu fault */
1057 tb = tb_find_pc(pc);
1058 if (tb) {
1059 /* the PC is inside the translated code. It means that we have
1060 a virtual CPU fault */
1061 cpu_restore_state(tb, env, pc, puc);
1062 }
1063 /* we restore the process signal mask as the sigreturn should
1064 do it (XXX: use sigsetjmp) */
1065 sigprocmask(SIG_SETMASK, old_set, NULL);
1066 cpu_loop_exit();
1067 /* never comes here */
1068 return 1;
1069}
1070
bellard6af0bf92005-07-02 14:58:51 +00001071#elif defined (TARGET_MIPS)
1072static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1073 int is_write, sigset_t *old_set,
1074 void *puc)
1075{
1076 TranslationBlock *tb;
1077 int ret;
1078
1079 if (cpu_single_env)
1080 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1081#if defined(DEBUG_SIGNAL)
1082 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1083 pc, address, is_write, *(unsigned long *)old_set);
1084#endif
1085 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +00001086 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellard6af0bf92005-07-02 14:58:51 +00001087 return 1;
1088 }
1089
1090 /* see if it is an MMU fault */
bellardcc9442b2005-11-26 18:43:28 +00001091 ret = cpu_mips_handle_mmu_fault(env, address, is_write, 1, 0);
bellard6af0bf92005-07-02 14:58:51 +00001092 if (ret < 0)
1093 return 0; /* not an MMU fault */
1094 if (ret == 0)
1095 return 1; /* the MMU fault was handled without causing real CPU fault */
1096
1097 /* now we have a real cpu fault */
1098 tb = tb_find_pc(pc);
1099 if (tb) {
1100 /* the PC is inside the translated code. It means that we have
1101 a virtual CPU fault */
1102 cpu_restore_state(tb, env, pc, puc);
1103 }
1104 if (ret == 1) {
1105#if 0
1106 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1107 env->nip, env->error_code, tb);
1108#endif
1109 /* we restore the process signal mask as the sigreturn should
1110 do it (XXX: use sigsetjmp) */
1111 sigprocmask(SIG_SETMASK, old_set, NULL);
1112 do_raise_exception_err(env->exception_index, env->error_code);
1113 } else {
1114 /* activate soft MMU for this block */
1115 cpu_resume_from_signal(env, puc);
1116 }
1117 /* never comes here */
1118 return 1;
1119}
1120
bellardfdf9b3e2006-04-27 21:07:38 +00001121#elif defined (TARGET_SH4)
1122static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1123 int is_write, sigset_t *old_set,
1124 void *puc)
1125{
1126 TranslationBlock *tb;
1127 int ret;
1128
1129 if (cpu_single_env)
1130 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1131#if defined(DEBUG_SIGNAL)
1132 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1133 pc, address, is_write, *(unsigned long *)old_set);
1134#endif
1135 /* XXX: locking issue */
1136 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1137 return 1;
1138 }
1139
1140 /* see if it is an MMU fault */
1141 ret = cpu_sh4_handle_mmu_fault(env, address, is_write, 1, 0);
1142 if (ret < 0)
1143 return 0; /* not an MMU fault */
1144 if (ret == 0)
1145 return 1; /* the MMU fault was handled without causing real CPU fault */
1146
1147 /* now we have a real cpu fault */
1148 tb = tb_find_pc(pc);
1149 if (tb) {
1150 /* the PC is inside the translated code. It means that we have
1151 a virtual CPU fault */
1152 cpu_restore_state(tb, env, pc, puc);
1153 }
bellardfdf9b3e2006-04-27 21:07:38 +00001154#if 0
1155 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1156 env->nip, env->error_code, tb);
1157#endif
1158 /* we restore the process signal mask as the sigreturn should
1159 do it (XXX: use sigsetjmp) */
pbrook355fb232006-06-17 19:58:25 +00001160 sigprocmask(SIG_SETMASK, old_set, NULL);
1161 cpu_loop_exit();
bellardfdf9b3e2006-04-27 21:07:38 +00001162 /* never comes here */
1163 return 1;
1164}
bellarde4533c72003-06-15 19:51:39 +00001165#else
1166#error unsupported target CPU
1167#endif
bellard9de5e442003-03-23 16:49:39 +00001168
bellard2b413142003-05-14 23:01:10 +00001169#if defined(__i386__)
1170
bellardd8ecc0b2007-02-05 21:41:46 +00001171#if defined(__APPLE__)
1172# include <sys/ucontext.h>
1173
1174# define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
1175# define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
1176# define ERROR_sig(context) ((context)->uc_mcontext->es.err)
1177#else
1178# define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
1179# define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
1180# define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
1181#endif
1182
bellardbf3e8bf2004-02-16 21:58:54 +00001183#if defined(USE_CODE_COPY)
1184static void cpu_send_trap(unsigned long pc, int trap,
1185 struct ucontext *uc)
1186{
1187 TranslationBlock *tb;
1188
1189 if (cpu_single_env)
1190 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1191 /* now we have a real cpu fault */
1192 tb = tb_find_pc(pc);
1193 if (tb) {
1194 /* the PC is inside the translated code. It means that we have
1195 a virtual CPU fault */
1196 cpu_restore_state(tb, env, pc, uc);
1197 }
1198 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
1199 raise_exception_err(trap, env->error_code);
1200}
1201#endif
1202
ths5a7b5422007-01-31 12:16:51 +00001203int cpu_signal_handler(int host_signum, void *pinfo,
bellarde4533c72003-06-15 19:51:39 +00001204 void *puc)
bellard9de5e442003-03-23 16:49:39 +00001205{
ths5a7b5422007-01-31 12:16:51 +00001206 siginfo_t *info = pinfo;
bellard9de5e442003-03-23 16:49:39 +00001207 struct ucontext *uc = puc;
1208 unsigned long pc;
bellardbf3e8bf2004-02-16 21:58:54 +00001209 int trapno;
bellard97eb5b12004-02-25 23:19:55 +00001210
bellardd691f662003-03-24 21:58:34 +00001211#ifndef REG_EIP
1212/* for glibc 2.1 */
bellardfd6ce8f2003-05-14 19:00:11 +00001213#define REG_EIP EIP
1214#define REG_ERR ERR
1215#define REG_TRAPNO TRAPNO
bellardd691f662003-03-24 21:58:34 +00001216#endif
bellardd8ecc0b2007-02-05 21:41:46 +00001217 pc = EIP_sig(uc);
1218 trapno = TRAP_sig(uc);
bellardbf3e8bf2004-02-16 21:58:54 +00001219#if defined(TARGET_I386) && defined(USE_CODE_COPY)
1220 if (trapno == 0x00 || trapno == 0x05) {
1221 /* send division by zero or bound exception */
1222 cpu_send_trap(pc, trapno, uc);
1223 return 1;
1224 } else
1225#endif
1226 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1227 trapno == 0xe ?
bellardd8ecc0b2007-02-05 21:41:46 +00001228 (ERROR_sig(uc) >> 1) & 1 : 0,
bellardbf3e8bf2004-02-16 21:58:54 +00001229 &uc->uc_sigmask, puc);
bellard2b413142003-05-14 23:01:10 +00001230}
1231
bellardbc51c5c2004-03-17 23:46:04 +00001232#elif defined(__x86_64__)
1233
ths5a7b5422007-01-31 12:16:51 +00001234int cpu_signal_handler(int host_signum, void *pinfo,
bellardbc51c5c2004-03-17 23:46:04 +00001235 void *puc)
1236{
ths5a7b5422007-01-31 12:16:51 +00001237 siginfo_t *info = pinfo;
bellardbc51c5c2004-03-17 23:46:04 +00001238 struct ucontext *uc = puc;
1239 unsigned long pc;
1240
1241 pc = uc->uc_mcontext.gregs[REG_RIP];
1242 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1243 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
1244 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1245 &uc->uc_sigmask, puc);
1246}
1247
bellard83fb7ad2004-07-05 21:25:26 +00001248#elif defined(__powerpc__)
bellard2b413142003-05-14 23:01:10 +00001249
bellard83fb7ad2004-07-05 21:25:26 +00001250/***********************************************************************
1251 * signal context platform-specific definitions
1252 * From Wine
1253 */
1254#ifdef linux
1255/* All Registers access - only for local access */
1256# define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
1257/* Gpr Registers access */
1258# define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
1259# define IAR_sig(context) REG_sig(nip, context) /* Program counter */
1260# define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
1261# define CTR_sig(context) REG_sig(ctr, context) /* Count register */
1262# define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
1263# define LR_sig(context) REG_sig(link, context) /* Link register */
1264# define CR_sig(context) REG_sig(ccr, context) /* Condition register */
1265/* Float Registers access */
1266# define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1267# define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1268/* Exception Registers access */
1269# define DAR_sig(context) REG_sig(dar, context)
1270# define DSISR_sig(context) REG_sig(dsisr, context)
1271# define TRAP_sig(context) REG_sig(trap, context)
1272#endif /* linux */
1273
1274#ifdef __APPLE__
1275# include <sys/ucontext.h>
1276typedef struct ucontext SIGCONTEXT;
1277/* All Registers access - only for local access */
1278# define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
1279# define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
1280# define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
1281# define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
1282/* Gpr Registers access */
1283# define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
1284# define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
1285# define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
1286# define CTR_sig(context) REG_sig(ctr, context)
1287# define XER_sig(context) REG_sig(xer, context) /* Link register */
1288# define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
1289# define CR_sig(context) REG_sig(cr, context) /* Condition register */
1290/* Float Registers access */
1291# define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
1292# define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
1293/* Exception Registers access */
1294# define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
1295# define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
1296# define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1297#endif /* __APPLE__ */
1298
ths5a7b5422007-01-31 12:16:51 +00001299int cpu_signal_handler(int host_signum, void *pinfo,
bellarde4533c72003-06-15 19:51:39 +00001300 void *puc)
bellard2b413142003-05-14 23:01:10 +00001301{
ths5a7b5422007-01-31 12:16:51 +00001302 siginfo_t *info = pinfo;
bellard25eb4482003-05-14 21:50:54 +00001303 struct ucontext *uc = puc;
bellard25eb4482003-05-14 21:50:54 +00001304 unsigned long pc;
bellard25eb4482003-05-14 21:50:54 +00001305 int is_write;
1306
bellard83fb7ad2004-07-05 21:25:26 +00001307 pc = IAR_sig(uc);
bellard25eb4482003-05-14 21:50:54 +00001308 is_write = 0;
1309#if 0
1310 /* ppc 4xx case */
bellard83fb7ad2004-07-05 21:25:26 +00001311 if (DSISR_sig(uc) & 0x00800000)
bellard25eb4482003-05-14 21:50:54 +00001312 is_write = 1;
bellard9de5e442003-03-23 16:49:39 +00001313#else
bellard83fb7ad2004-07-05 21:25:26 +00001314 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
bellard25eb4482003-05-14 21:50:54 +00001315 is_write = 1;
1316#endif
1317 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001318 is_write, &uc->uc_sigmask, puc);
bellard9de5e442003-03-23 16:49:39 +00001319}
bellard2b413142003-05-14 23:01:10 +00001320
bellard2f87c602003-06-02 20:38:09 +00001321#elif defined(__alpha__)
1322
ths5a7b5422007-01-31 12:16:51 +00001323int cpu_signal_handler(int host_signum, void *pinfo,
bellard2f87c602003-06-02 20:38:09 +00001324 void *puc)
1325{
ths5a7b5422007-01-31 12:16:51 +00001326 siginfo_t *info = pinfo;
bellard2f87c602003-06-02 20:38:09 +00001327 struct ucontext *uc = puc;
1328 uint32_t *pc = uc->uc_mcontext.sc_pc;
1329 uint32_t insn = *pc;
1330 int is_write = 0;
1331
bellard8c6939c2003-06-09 15:28:00 +00001332 /* XXX: need kernel patch to get write flag faster */
bellard2f87c602003-06-02 20:38:09 +00001333 switch (insn >> 26) {
1334 case 0x0d: // stw
1335 case 0x0e: // stb
1336 case 0x0f: // stq_u
1337 case 0x24: // stf
1338 case 0x25: // stg
1339 case 0x26: // sts
1340 case 0x27: // stt
1341 case 0x2c: // stl
1342 case 0x2d: // stq
1343 case 0x2e: // stl_c
1344 case 0x2f: // stq_c
1345 is_write = 1;
1346 }
1347
1348 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001349 is_write, &uc->uc_sigmask, puc);
bellard2f87c602003-06-02 20:38:09 +00001350}
bellard8c6939c2003-06-09 15:28:00 +00001351#elif defined(__sparc__)
1352
ths5a7b5422007-01-31 12:16:51 +00001353int cpu_signal_handler(int host_signum, void *pinfo,
bellarde4533c72003-06-15 19:51:39 +00001354 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001355{
ths5a7b5422007-01-31 12:16:51 +00001356 siginfo_t *info = pinfo;
bellard8c6939c2003-06-09 15:28:00 +00001357 uint32_t *regs = (uint32_t *)(info + 1);
1358 void *sigmask = (regs + 20);
1359 unsigned long pc;
1360 int is_write;
1361 uint32_t insn;
1362
1363 /* XXX: is there a standard glibc define ? */
1364 pc = regs[1];
1365 /* XXX: need kernel patch to get write flag faster */
1366 is_write = 0;
1367 insn = *(uint32_t *)pc;
1368 if ((insn >> 30) == 3) {
1369 switch((insn >> 19) & 0x3f) {
1370 case 0x05: // stb
1371 case 0x06: // sth
1372 case 0x04: // st
1373 case 0x07: // std
1374 case 0x24: // stf
1375 case 0x27: // stdf
1376 case 0x25: // stfsr
1377 is_write = 1;
1378 break;
1379 }
1380 }
1381 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001382 is_write, sigmask, NULL);
bellard8c6939c2003-06-09 15:28:00 +00001383}
1384
1385#elif defined(__arm__)
1386
ths5a7b5422007-01-31 12:16:51 +00001387int cpu_signal_handler(int host_signum, void *pinfo,
bellarde4533c72003-06-15 19:51:39 +00001388 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001389{
ths5a7b5422007-01-31 12:16:51 +00001390 siginfo_t *info = pinfo;
bellard8c6939c2003-06-09 15:28:00 +00001391 struct ucontext *uc = puc;
1392 unsigned long pc;
1393 int is_write;
1394
1395 pc = uc->uc_mcontext.gregs[R15];
1396 /* XXX: compute is_write */
1397 is_write = 0;
1398 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1399 is_write,
pbrookf3a96762006-07-29 19:09:31 +00001400 &uc->uc_sigmask, puc);
bellard8c6939c2003-06-09 15:28:00 +00001401}
1402
bellard38e584a2003-08-10 22:14:22 +00001403#elif defined(__mc68000)
1404
ths5a7b5422007-01-31 12:16:51 +00001405int cpu_signal_handler(int host_signum, void *pinfo,
bellard38e584a2003-08-10 22:14:22 +00001406 void *puc)
1407{
ths5a7b5422007-01-31 12:16:51 +00001408 siginfo_t *info = pinfo;
bellard38e584a2003-08-10 22:14:22 +00001409 struct ucontext *uc = puc;
1410 unsigned long pc;
1411 int is_write;
1412
1413 pc = uc->uc_mcontext.gregs[16];
1414 /* XXX: compute is_write */
1415 is_write = 0;
1416 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1417 is_write,
bellardbf3e8bf2004-02-16 21:58:54 +00001418 &uc->uc_sigmask, puc);
bellard38e584a2003-08-10 22:14:22 +00001419}
1420
bellardb8076a72005-04-07 22:20:31 +00001421#elif defined(__ia64)
1422
1423#ifndef __ISR_VALID
1424 /* This ought to be in <bits/siginfo.h>... */
1425# define __ISR_VALID 1
bellardb8076a72005-04-07 22:20:31 +00001426#endif
1427
ths5a7b5422007-01-31 12:16:51 +00001428int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
bellardb8076a72005-04-07 22:20:31 +00001429{
ths5a7b5422007-01-31 12:16:51 +00001430 siginfo_t *info = pinfo;
bellardb8076a72005-04-07 22:20:31 +00001431 struct ucontext *uc = puc;
1432 unsigned long ip;
1433 int is_write = 0;
1434
1435 ip = uc->uc_mcontext.sc_ip;
1436 switch (host_signum) {
1437 case SIGILL:
1438 case SIGFPE:
1439 case SIGSEGV:
1440 case SIGBUS:
1441 case SIGTRAP:
bellardfd4a43e2006-04-24 20:32:17 +00001442 if (info->si_code && (info->si_segvflags & __ISR_VALID))
bellardb8076a72005-04-07 22:20:31 +00001443 /* ISR.W (write-access) is bit 33: */
1444 is_write = (info->si_isr >> 33) & 1;
1445 break;
1446
1447 default:
1448 break;
1449 }
1450 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1451 is_write,
1452 &uc->uc_sigmask, puc);
1453}
1454
bellard90cb9492005-07-24 15:11:38 +00001455#elif defined(__s390__)
1456
ths5a7b5422007-01-31 12:16:51 +00001457int cpu_signal_handler(int host_signum, void *pinfo,
bellard90cb9492005-07-24 15:11:38 +00001458 void *puc)
1459{
ths5a7b5422007-01-31 12:16:51 +00001460 siginfo_t *info = pinfo;
bellard90cb9492005-07-24 15:11:38 +00001461 struct ucontext *uc = puc;
1462 unsigned long pc;
1463 int is_write;
1464
1465 pc = uc->uc_mcontext.psw.addr;
1466 /* XXX: compute is_write */
1467 is_write = 0;
1468 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1469 is_write,
1470 &uc->uc_sigmask, puc);
1471}
1472
bellard2b413142003-05-14 23:01:10 +00001473#else
1474
bellard3fb2ded2003-06-24 13:22:59 +00001475#error host CPU specific signal handler needed
bellard2b413142003-05-14 23:01:10 +00001476
1477#endif
bellard67b915a2004-03-31 23:37:16 +00001478
1479#endif /* !defined(CONFIG_SOFTMMU) */