blob: 6385639f46f20dcb8b380d8644364c03df1ad5ed [file] [log] [blame]
bellard7d132992003-03-06 23:23:54 +00001/*
2 * i386 emulator main execution loop
3 *
bellard66321a12005-04-06 20:47:48 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
bellard7d132992003-03-06 23:23:54 +00005 *
bellard3ef693a2003-03-23 20:17:16 +00006 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
bellard7d132992003-03-06 23:23:54 +000010 *
bellard3ef693a2003-03-23 20:17:16 +000011 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
bellard7d132992003-03-06 23:23:54 +000015 *
bellard3ef693a2003-03-23 20:17:16 +000016 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
bellard7d132992003-03-06 23:23:54 +000019 */
bellarde4533c72003-06-15 19:51:39 +000020#include "config.h"
bellard93ac68b2003-09-30 20:57:29 +000021#include "exec.h"
bellard956034d2003-04-29 20:40:53 +000022#include "disas.h"
bellard7d132992003-03-06 23:23:54 +000023
bellardfbf9eeb2004-04-25 21:21:33 +000024#if !defined(CONFIG_SOFTMMU)
25#undef EAX
26#undef ECX
27#undef EDX
28#undef EBX
29#undef ESP
30#undef EBP
31#undef ESI
32#undef EDI
33#undef EIP
34#include <signal.h>
35#include <sys/ucontext.h>
36#endif
37
bellard36bdbe52003-11-19 22:12:02 +000038int tb_invalidated_flag;
39
bellarddc990652003-03-19 00:00:28 +000040//#define DEBUG_EXEC
bellard9de5e442003-03-23 16:49:39 +000041//#define DEBUG_SIGNAL
bellard7d132992003-03-06 23:23:54 +000042
bellard93ac68b2003-09-30 20:57:29 +000043#if defined(TARGET_ARM) || defined(TARGET_SPARC)
bellarde4533c72003-06-15 19:51:39 +000044/* XXX: unify with i386 target */
45void cpu_loop_exit(void)
46{
47 longjmp(env->jmp_env, 1);
48}
49#endif
pbrook9c2a9ea2006-06-18 19:12:54 +000050#if !(defined(TARGET_SPARC) || defined(TARGET_SH4))
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;
bellardfdf9b3e2006-04-27 21:07:38 +0000197#elif defined(TARGET_SH4)
198 flags = env->sr & (SR_MD | SR_RB);
199 cs_base = 0; /* XXXXX */
200 pc = env->pc;
bellard8a40a182005-11-20 10:35:40 +0000201#else
202#error unsupported CPU
203#endif
204 tb = env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)];
205 if (__builtin_expect(!tb || tb->pc != pc || tb->cs_base != cs_base ||
206 tb->flags != flags, 0)) {
207 tb = tb_find_slow(pc, cs_base, flags);
bellard15388002005-12-19 01:42:32 +0000208 /* Note: we do it here to avoid a gcc bug on Mac OS X when
209 doing it in tb_find_slow */
210 if (tb_invalidated_flag) {
211 /* as some TB could have been invalidated because
212 of memory exceptions while generating the code, we
213 must recompute the hash index here */
214 T0 = 0;
215 }
bellard8a40a182005-11-20 10:35:40 +0000216 }
217 return tb;
218}
219
220
bellard7d132992003-03-06 23:23:54 +0000221/* main execution loop */
222
bellarde4533c72003-06-15 19:51:39 +0000223int cpu_exec(CPUState *env1)
bellard7d132992003-03-06 23:23:54 +0000224{
bellard34751872005-07-02 14:31:34 +0000225 int saved_T0, saved_T1;
226#if defined(reg_T2)
227 int saved_T2;
228#endif
bellarde4533c72003-06-15 19:51:39 +0000229 CPUState *saved_env;
bellard34751872005-07-02 14:31:34 +0000230#if defined(TARGET_I386)
bellard04369ff2003-03-20 22:33:23 +0000231#ifdef reg_EAX
232 int saved_EAX;
233#endif
234#ifdef reg_ECX
235 int saved_ECX;
236#endif
237#ifdef reg_EDX
238 int saved_EDX;
239#endif
240#ifdef reg_EBX
241 int saved_EBX;
242#endif
243#ifdef reg_ESP
244 int saved_ESP;
245#endif
246#ifdef reg_EBP
247 int saved_EBP;
248#endif
249#ifdef reg_ESI
250 int saved_ESI;
251#endif
252#ifdef reg_EDI
253 int saved_EDI;
254#endif
bellard34751872005-07-02 14:31:34 +0000255#elif defined(TARGET_SPARC)
256#if defined(reg_REGWPTR)
257 uint32_t *saved_regwptr;
258#endif
259#endif
bellardfdbb4692006-06-14 17:32:25 +0000260#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8c6939c2003-06-09 15:28:00 +0000261 int saved_i7, tmp_T0;
262#endif
bellard8a40a182005-11-20 10:35:40 +0000263 int ret, interrupt_request;
bellard7d132992003-03-06 23:23:54 +0000264 void (*gen_func)(void);
bellard8a40a182005-11-20 10:35:40 +0000265 TranslationBlock *tb;
bellardc27004e2005-01-03 23:35:10 +0000266 uint8_t *tc_ptr;
bellard8c6939c2003-06-09 15:28:00 +0000267
bellard5a1e3cf2005-11-23 21:02:53 +0000268#if defined(TARGET_I386)
269 /* handle exit of HALTED state */
270 if (env1->hflags & HF_HALTED_MASK) {
271 /* disable halt condition */
272 if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
273 (env1->eflags & IF_MASK)) {
274 env1->hflags &= ~HF_HALTED_MASK;
275 } else {
276 return EXCP_HALTED;
277 }
278 }
bellarde80e1cc2005-11-23 22:05:28 +0000279#elif defined(TARGET_PPC)
bellard50443c92005-11-26 20:15:14 +0000280 if (env1->halted) {
bellarde80e1cc2005-11-23 22:05:28 +0000281 if (env1->msr[MSR_EE] &&
282 (env1->interrupt_request &
283 (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER))) {
bellard50443c92005-11-26 20:15:14 +0000284 env1->halted = 0;
bellarde80e1cc2005-11-23 22:05:28 +0000285 } else {
286 return EXCP_HALTED;
287 }
288 }
bellardba3c64f2005-12-05 20:31:52 +0000289#elif defined(TARGET_SPARC)
290 if (env1->halted) {
291 if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&
292 (env1->psret != 0)) {
293 env1->halted = 0;
294 } else {
295 return EXCP_HALTED;
296 }
297 }
bellard9332f9d2005-11-26 10:46:39 +0000298#elif defined(TARGET_ARM)
299 if (env1->halted) {
300 /* An interrupt wakes the CPU even if the I and F CPSR bits are
301 set. */
302 if (env1->interrupt_request
303 & (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD)) {
304 env1->halted = 0;
305 } else {
306 return EXCP_HALTED;
307 }
308 }
bellard6810e152005-12-05 19:59:05 +0000309#elif defined(TARGET_MIPS)
310 if (env1->halted) {
311 if (env1->interrupt_request &
312 (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER)) {
313 env1->halted = 0;
314 } else {
315 return EXCP_HALTED;
316 }
317 }
bellard5a1e3cf2005-11-23 21:02:53 +0000318#endif
319
bellard6a00d602005-11-21 23:25:50 +0000320 cpu_single_env = env1;
321
bellard7d132992003-03-06 23:23:54 +0000322 /* first we save global registers */
bellardc27004e2005-01-03 23:35:10 +0000323 saved_env = env;
324 env = env1;
bellard7d132992003-03-06 23:23:54 +0000325 saved_T0 = T0;
326 saved_T1 = T1;
bellard34751872005-07-02 14:31:34 +0000327#if defined(reg_T2)
bellarde4533c72003-06-15 19:51:39 +0000328 saved_T2 = T2;
bellard34751872005-07-02 14:31:34 +0000329#endif
bellardfdbb4692006-06-14 17:32:25 +0000330#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellarde4533c72003-06-15 19:51:39 +0000331 /* we also save i7 because longjmp may not restore it */
332 asm volatile ("mov %%i7, %0" : "=r" (saved_i7));
333#endif
334
335#if defined(TARGET_I386)
bellard04369ff2003-03-20 22:33:23 +0000336#ifdef reg_EAX
337 saved_EAX = EAX;
bellard04369ff2003-03-20 22:33:23 +0000338#endif
339#ifdef reg_ECX
340 saved_ECX = ECX;
bellard04369ff2003-03-20 22:33:23 +0000341#endif
342#ifdef reg_EDX
343 saved_EDX = EDX;
bellard04369ff2003-03-20 22:33:23 +0000344#endif
345#ifdef reg_EBX
346 saved_EBX = EBX;
bellard04369ff2003-03-20 22:33:23 +0000347#endif
348#ifdef reg_ESP
349 saved_ESP = ESP;
bellard04369ff2003-03-20 22:33:23 +0000350#endif
351#ifdef reg_EBP
352 saved_EBP = EBP;
bellard04369ff2003-03-20 22:33:23 +0000353#endif
354#ifdef reg_ESI
355 saved_ESI = ESI;
bellard04369ff2003-03-20 22:33:23 +0000356#endif
357#ifdef reg_EDI
358 saved_EDI = EDI;
bellard04369ff2003-03-20 22:33:23 +0000359#endif
bellard0d1a29f2004-10-12 22:01:28 +0000360
361 env_to_regs();
bellard9de5e442003-03-23 16:49:39 +0000362 /* put eflags in CPU temporary format */
bellardfc2b4c42003-03-29 16:52:44 +0000363 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
364 DF = 1 - (2 * ((env->eflags >> 10) & 1));
bellard9de5e442003-03-23 16:49:39 +0000365 CC_OP = CC_OP_EFLAGS;
bellardfc2b4c42003-03-29 16:52:44 +0000366 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000367#elif defined(TARGET_ARM)
bellard93ac68b2003-09-30 20:57:29 +0000368#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000369#if defined(reg_REGWPTR)
370 saved_regwptr = REGWPTR;
371#endif
bellard67867302003-11-23 17:05:30 +0000372#elif defined(TARGET_PPC)
bellard6af0bf92005-07-02 14:58:51 +0000373#elif defined(TARGET_MIPS)
bellardfdf9b3e2006-04-27 21:07:38 +0000374#elif defined(TARGET_SH4)
375 /* XXXXX */
bellarde4533c72003-06-15 19:51:39 +0000376#else
377#error unsupported target CPU
378#endif
bellard3fb2ded2003-06-24 13:22:59 +0000379 env->exception_index = -1;
bellard9d27abd2003-05-10 13:13:54 +0000380
bellard7d132992003-03-06 23:23:54 +0000381 /* prepare setjmp context for exception handling */
bellard3fb2ded2003-06-24 13:22:59 +0000382 for(;;) {
383 if (setjmp(env->jmp_env) == 0) {
bellardee8b7022004-02-03 23:35:10 +0000384 env->current_tb = NULL;
bellard3fb2ded2003-06-24 13:22:59 +0000385 /* if an exception is pending, we execute it here */
386 if (env->exception_index >= 0) {
387 if (env->exception_index >= EXCP_INTERRUPT) {
388 /* exit request from the cpu execution loop */
389 ret = env->exception_index;
390 break;
391 } else if (env->user_mode_only) {
392 /* if user mode only, we simulate a fake exception
393 which will be hanlded outside the cpu execution
394 loop */
bellard83479e72003-06-25 16:12:37 +0000395#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000396 do_interrupt_user(env->exception_index,
397 env->exception_is_int,
398 env->error_code,
399 env->exception_next_eip);
bellard83479e72003-06-25 16:12:37 +0000400#endif
bellard3fb2ded2003-06-24 13:22:59 +0000401 ret = env->exception_index;
402 break;
403 } else {
bellard83479e72003-06-25 16:12:37 +0000404#if defined(TARGET_I386)
bellard3fb2ded2003-06-24 13:22:59 +0000405 /* simulate a real cpu exception. On i386, it can
406 trigger new exceptions, but we do not handle
407 double or triple faults yet. */
408 do_interrupt(env->exception_index,
409 env->exception_is_int,
410 env->error_code,
bellardd05e66d2003-08-20 21:34:35 +0000411 env->exception_next_eip, 0);
bellardce097762004-01-04 23:53:18 +0000412#elif defined(TARGET_PPC)
413 do_interrupt(env);
bellard6af0bf92005-07-02 14:58:51 +0000414#elif defined(TARGET_MIPS)
415 do_interrupt(env);
bellarde95c8d52004-09-30 22:22:08 +0000416#elif defined(TARGET_SPARC)
bellard1a0c3292005-02-13 19:02:07 +0000417 do_interrupt(env->exception_index);
bellardb5ff1b32005-11-26 10:38:39 +0000418#elif defined(TARGET_ARM)
419 do_interrupt(env);
bellardfdf9b3e2006-04-27 21:07:38 +0000420#elif defined(TARGET_SH4)
421 do_interrupt(env);
bellard83479e72003-06-25 16:12:37 +0000422#endif
bellard3fb2ded2003-06-24 13:22:59 +0000423 }
424 env->exception_index = -1;
bellard9df217a2005-02-10 22:05:51 +0000425 }
426#ifdef USE_KQEMU
427 if (kqemu_is_ok(env) && env->interrupt_request == 0) {
428 int ret;
429 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
430 ret = kqemu_cpu_exec(env);
431 /* put eflags in CPU temporary format */
432 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
433 DF = 1 - (2 * ((env->eflags >> 10) & 1));
434 CC_OP = CC_OP_EFLAGS;
435 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
436 if (ret == 1) {
437 /* exception */
438 longjmp(env->jmp_env, 1);
439 } else if (ret == 2) {
440 /* softmmu execution needed */
441 } else {
442 if (env->interrupt_request != 0) {
443 /* hardware interrupt will be executed just after */
444 } else {
445 /* otherwise, we restart */
446 longjmp(env->jmp_env, 1);
447 }
448 }
bellard9de5e442003-03-23 16:49:39 +0000449 }
bellard9df217a2005-02-10 22:05:51 +0000450#endif
451
bellard3fb2ded2003-06-24 13:22:59 +0000452 T0 = 0; /* force lookup of first TB */
453 for(;;) {
bellardfdbb4692006-06-14 17:32:25 +0000454#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard3fb2ded2003-06-24 13:22:59 +0000455 /* g1 can be modified by some libc? functions */
456 tmp_T0 = T0;
457#endif
bellard68a79312003-06-30 13:12:32 +0000458 interrupt_request = env->interrupt_request;
bellard2e255c62003-08-21 23:25:21 +0000459 if (__builtin_expect(interrupt_request, 0)) {
bellard68a79312003-06-30 13:12:32 +0000460#if defined(TARGET_I386)
461 /* if hardware interrupt pending, we execute it */
462 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
bellard3f337312003-08-20 23:02:09 +0000463 (env->eflags & IF_MASK) &&
464 !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
bellard68a79312003-06-30 13:12:32 +0000465 int intno;
bellardfbf9eeb2004-04-25 21:21:33 +0000466 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellarda541f292004-04-12 20:39:29 +0000467 intno = cpu_get_pic_interrupt(env);
bellardf193c792004-03-21 17:06:25 +0000468 if (loglevel & CPU_LOG_TB_IN_ASM) {
bellard68a79312003-06-30 13:12:32 +0000469 fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
470 }
bellardd05e66d2003-08-20 21:34:35 +0000471 do_interrupt(intno, 0, 0, 0, 1);
bellard907a5b22003-06-30 23:18:22 +0000472 /* ensure that no TB jump will be modified as
473 the program flow was changed */
bellardfdbb4692006-06-14 17:32:25 +0000474#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard907a5b22003-06-30 23:18:22 +0000475 tmp_T0 = 0;
476#else
477 T0 = 0;
478#endif
bellard68a79312003-06-30 13:12:32 +0000479 }
bellardce097762004-01-04 23:53:18 +0000480#elif defined(TARGET_PPC)
bellard9fddaa02004-05-21 12:59:32 +0000481#if 0
482 if ((interrupt_request & CPU_INTERRUPT_RESET)) {
483 cpu_ppc_reset(env);
484 }
485#endif
486 if (msr_ee != 0) {
bellard8a40a182005-11-20 10:35:40 +0000487 if ((interrupt_request & CPU_INTERRUPT_HARD)) {
bellard9fddaa02004-05-21 12:59:32 +0000488 /* Raise it */
489 env->exception_index = EXCP_EXTERNAL;
490 env->error_code = 0;
bellardce097762004-01-04 23:53:18 +0000491 do_interrupt(env);
bellard8a40a182005-11-20 10:35:40 +0000492 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
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
498 } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) {
499 /* Raise it */
500 env->exception_index = EXCP_DECR;
501 env->error_code = 0;
502 do_interrupt(env);
bellard9fddaa02004-05-21 12:59:32 +0000503 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
bellardfdbb4692006-06-14 17:32:25 +0000504#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8a40a182005-11-20 10:35:40 +0000505 tmp_T0 = 0;
506#else
507 T0 = 0;
508#endif
509 }
bellardce097762004-01-04 23:53:18 +0000510 }
bellard6af0bf92005-07-02 14:58:51 +0000511#elif defined(TARGET_MIPS)
512 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
513 (env->CP0_Status & (1 << CP0St_IE)) &&
bellard7ebab692005-08-21 09:43:38 +0000514 (env->CP0_Status & env->CP0_Cause & 0x0000FF00) &&
bellard6af0bf92005-07-02 14:58:51 +0000515 !(env->hflags & MIPS_HFLAG_EXL) &&
516 !(env->hflags & MIPS_HFLAG_ERL) &&
517 !(env->hflags & MIPS_HFLAG_DM)) {
518 /* Raise it */
519 env->exception_index = EXCP_EXT_INTERRUPT;
520 env->error_code = 0;
521 do_interrupt(env);
522 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
bellardfdbb4692006-06-14 17:32:25 +0000523#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8a40a182005-11-20 10:35:40 +0000524 tmp_T0 = 0;
525#else
526 T0 = 0;
527#endif
bellard6af0bf92005-07-02 14:58:51 +0000528 }
bellarde95c8d52004-09-30 22:22:08 +0000529#elif defined(TARGET_SPARC)
bellard66321a12005-04-06 20:47:48 +0000530 if ((interrupt_request & CPU_INTERRUPT_HARD) &&
531 (env->psret != 0)) {
532 int pil = env->interrupt_index & 15;
533 int type = env->interrupt_index & 0xf0;
534
535 if (((type == TT_EXTINT) &&
536 (pil == 15 || pil > env->psrpil)) ||
537 type != TT_EXTINT) {
538 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
539 do_interrupt(env->interrupt_index);
540 env->interrupt_index = 0;
bellardfdbb4692006-06-14 17:32:25 +0000541#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8a40a182005-11-20 10:35:40 +0000542 tmp_T0 = 0;
543#else
544 T0 = 0;
545#endif
bellard66321a12005-04-06 20:47:48 +0000546 }
bellarde95c8d52004-09-30 22:22:08 +0000547 } else if (interrupt_request & CPU_INTERRUPT_TIMER) {
548 //do_interrupt(0, 0, 0, 0, 0);
549 env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
bellardba3c64f2005-12-05 20:31:52 +0000550 } else if (interrupt_request & CPU_INTERRUPT_HALT) {
bellarddf52b002006-09-20 20:30:57 +0000551 env->interrupt_request &= ~CPU_INTERRUPT_HALT;
552 env->halted = 1;
553 env->exception_index = EXCP_HLT;
554 cpu_loop_exit();
bellardba3c64f2005-12-05 20:31:52 +0000555 }
bellardb5ff1b32005-11-26 10:38:39 +0000556#elif defined(TARGET_ARM)
557 if (interrupt_request & CPU_INTERRUPT_FIQ
558 && !(env->uncached_cpsr & CPSR_F)) {
559 env->exception_index = EXCP_FIQ;
560 do_interrupt(env);
561 }
562 if (interrupt_request & CPU_INTERRUPT_HARD
563 && !(env->uncached_cpsr & CPSR_I)) {
564 env->exception_index = EXCP_IRQ;
565 do_interrupt(env);
566 }
bellardfdf9b3e2006-04-27 21:07:38 +0000567#elif defined(TARGET_SH4)
568 /* XXXXX */
bellard68a79312003-06-30 13:12:32 +0000569#endif
bellard9d050952006-05-22 22:03:52 +0000570 /* Don't use the cached interupt_request value,
571 do_interrupt may have updated the EXITTB flag. */
bellardb5ff1b32005-11-26 10:38:39 +0000572 if (env->interrupt_request & CPU_INTERRUPT_EXITTB) {
bellardbf3e8bf2004-02-16 21:58:54 +0000573 env->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
574 /* ensure that no TB jump will be modified as
575 the program flow was changed */
bellardfdbb4692006-06-14 17:32:25 +0000576#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellardbf3e8bf2004-02-16 21:58:54 +0000577 tmp_T0 = 0;
578#else
579 T0 = 0;
580#endif
581 }
bellard68a79312003-06-30 13:12:32 +0000582 if (interrupt_request & CPU_INTERRUPT_EXIT) {
583 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
584 env->exception_index = EXCP_INTERRUPT;
585 cpu_loop_exit();
586 }
bellard3fb2ded2003-06-24 13:22:59 +0000587 }
588#ifdef DEBUG_EXEC
bellardb5ff1b32005-11-26 10:38:39 +0000589 if ((loglevel & CPU_LOG_TB_CPU)) {
bellard3fb2ded2003-06-24 13:22:59 +0000590#if defined(TARGET_I386)
591 /* restore flags in standard format */
bellardfc9f7152005-04-26 19:33:35 +0000592#ifdef reg_EAX
bellard3fb2ded2003-06-24 13:22:59 +0000593 env->regs[R_EAX] = EAX;
bellardfc9f7152005-04-26 19:33:35 +0000594#endif
595#ifdef reg_EBX
bellard3fb2ded2003-06-24 13:22:59 +0000596 env->regs[R_EBX] = EBX;
bellardfc9f7152005-04-26 19:33:35 +0000597#endif
598#ifdef reg_ECX
bellard3fb2ded2003-06-24 13:22:59 +0000599 env->regs[R_ECX] = ECX;
bellardfc9f7152005-04-26 19:33:35 +0000600#endif
601#ifdef reg_EDX
bellard3fb2ded2003-06-24 13:22:59 +0000602 env->regs[R_EDX] = EDX;
bellardfc9f7152005-04-26 19:33:35 +0000603#endif
604#ifdef reg_ESI
bellard3fb2ded2003-06-24 13:22:59 +0000605 env->regs[R_ESI] = ESI;
bellardfc9f7152005-04-26 19:33:35 +0000606#endif
607#ifdef reg_EDI
bellard3fb2ded2003-06-24 13:22:59 +0000608 env->regs[R_EDI] = EDI;
bellardfc9f7152005-04-26 19:33:35 +0000609#endif
610#ifdef reg_EBP
bellard3fb2ded2003-06-24 13:22:59 +0000611 env->regs[R_EBP] = EBP;
bellardfc9f7152005-04-26 19:33:35 +0000612#endif
613#ifdef reg_ESP
bellard3fb2ded2003-06-24 13:22:59 +0000614 env->regs[R_ESP] = ESP;
bellardfc9f7152005-04-26 19:33:35 +0000615#endif
bellard3fb2ded2003-06-24 13:22:59 +0000616 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard7fe48482004-10-09 18:08:01 +0000617 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
bellard3fb2ded2003-06-24 13:22:59 +0000618 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
bellarde4533c72003-06-15 19:51:39 +0000619#elif defined(TARGET_ARM)
bellard7fe48482004-10-09 18:08:01 +0000620 cpu_dump_state(env, logfile, fprintf, 0);
bellard93ac68b2003-09-30 20:57:29 +0000621#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000622 REGWPTR = env->regbase + (env->cwp * 16);
623 env->regwptr = REGWPTR;
624 cpu_dump_state(env, logfile, fprintf, 0);
bellard67867302003-11-23 17:05:30 +0000625#elif defined(TARGET_PPC)
bellard7fe48482004-10-09 18:08:01 +0000626 cpu_dump_state(env, logfile, fprintf, 0);
bellard6af0bf92005-07-02 14:58:51 +0000627#elif defined(TARGET_MIPS)
628 cpu_dump_state(env, logfile, fprintf, 0);
bellardfdf9b3e2006-04-27 21:07:38 +0000629#elif defined(TARGET_SH4)
630 cpu_dump_state(env, logfile, fprintf, 0);
bellarde4533c72003-06-15 19:51:39 +0000631#else
632#error unsupported target CPU
633#endif
bellard3fb2ded2003-06-24 13:22:59 +0000634 }
bellard7d132992003-03-06 23:23:54 +0000635#endif
bellard8a40a182005-11-20 10:35:40 +0000636 tb = tb_find_fast();
bellard9d27abd2003-05-10 13:13:54 +0000637#ifdef DEBUG_EXEC
bellardc1135f62005-01-30 22:41:54 +0000638 if ((loglevel & CPU_LOG_EXEC)) {
bellardc27004e2005-01-03 23:35:10 +0000639 fprintf(logfile, "Trace 0x%08lx [" TARGET_FMT_lx "] %s\n",
640 (long)tb->tc_ptr, tb->pc,
641 lookup_symbol(tb->pc));
bellard3fb2ded2003-06-24 13:22:59 +0000642 }
bellard9d27abd2003-05-10 13:13:54 +0000643#endif
bellardfdbb4692006-06-14 17:32:25 +0000644#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard3fb2ded2003-06-24 13:22:59 +0000645 T0 = tmp_T0;
bellard8c6939c2003-06-09 15:28:00 +0000646#endif
bellard8a40a182005-11-20 10:35:40 +0000647 /* see if we can patch the calling TB. When the TB
648 spans two pages, we cannot safely do a direct
649 jump. */
bellardc27004e2005-01-03 23:35:10 +0000650 {
bellard8a40a182005-11-20 10:35:40 +0000651 if (T0 != 0 &&
bellardf32fc642006-02-08 22:43:39 +0000652#if USE_KQEMU
653 (env->kqemu_enabled != 2) &&
654#endif
bellard8a40a182005-11-20 10:35:40 +0000655 tb->page_addr[1] == -1
bellardbf3e8bf2004-02-16 21:58:54 +0000656#if defined(TARGET_I386) && defined(USE_CODE_COPY)
657 && (tb->cflags & CF_CODE_COPY) ==
658 (((TranslationBlock *)(T0 & ~3))->cflags & CF_CODE_COPY)
659#endif
660 ) {
bellard3fb2ded2003-06-24 13:22:59 +0000661 spin_lock(&tb_lock);
bellardc27004e2005-01-03 23:35:10 +0000662 tb_add_jump((TranslationBlock *)(long)(T0 & ~3), T0 & 3, tb);
bellard97eb5b12004-02-25 23:19:55 +0000663#if defined(USE_CODE_COPY)
664 /* propagates the FP use info */
665 ((TranslationBlock *)(T0 & ~3))->cflags |=
666 (tb->cflags & CF_FP_USED);
667#endif
bellard3fb2ded2003-06-24 13:22:59 +0000668 spin_unlock(&tb_lock);
669 }
bellardc27004e2005-01-03 23:35:10 +0000670 }
bellard3fb2ded2003-06-24 13:22:59 +0000671 tc_ptr = tb->tc_ptr;
bellard83479e72003-06-25 16:12:37 +0000672 env->current_tb = tb;
bellard3fb2ded2003-06-24 13:22:59 +0000673 /* execute the generated code */
674 gen_func = (void *)tc_ptr;
675#if defined(__sparc__)
676 __asm__ __volatile__("call %0\n\t"
677 "mov %%o7,%%i0"
678 : /* no outputs */
679 : "r" (gen_func)
bellardfdbb4692006-06-14 17:32:25 +0000680 : "i0", "i1", "i2", "i3", "i4", "i5",
681 "l0", "l1", "l2", "l3", "l4", "l5",
682 "l6", "l7");
bellard3fb2ded2003-06-24 13:22:59 +0000683#elif defined(__arm__)
684 asm volatile ("mov pc, %0\n\t"
685 ".global exec_loop\n\t"
686 "exec_loop:\n\t"
687 : /* no outputs */
688 : "r" (gen_func)
689 : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
bellardbf3e8bf2004-02-16 21:58:54 +0000690#elif defined(TARGET_I386) && defined(USE_CODE_COPY)
691{
692 if (!(tb->cflags & CF_CODE_COPY)) {
bellard97eb5b12004-02-25 23:19:55 +0000693 if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
694 save_native_fp_state(env);
695 }
bellardbf3e8bf2004-02-16 21:58:54 +0000696 gen_func();
697 } else {
bellard97eb5b12004-02-25 23:19:55 +0000698 if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
699 restore_native_fp_state(env);
700 }
bellardbf3e8bf2004-02-16 21:58:54 +0000701 /* we work with native eflags */
702 CC_SRC = cc_table[CC_OP].compute_all();
703 CC_OP = CC_OP_EFLAGS;
704 asm(".globl exec_loop\n"
705 "\n"
706 "debug1:\n"
707 " pushl %%ebp\n"
708 " fs movl %10, %9\n"
709 " fs movl %11, %%eax\n"
710 " andl $0x400, %%eax\n"
711 " fs orl %8, %%eax\n"
712 " pushl %%eax\n"
713 " popf\n"
714 " fs movl %%esp, %12\n"
715 " fs movl %0, %%eax\n"
716 " fs movl %1, %%ecx\n"
717 " fs movl %2, %%edx\n"
718 " fs movl %3, %%ebx\n"
719 " fs movl %4, %%esp\n"
720 " fs movl %5, %%ebp\n"
721 " fs movl %6, %%esi\n"
722 " fs movl %7, %%edi\n"
723 " fs jmp *%9\n"
724 "exec_loop:\n"
725 " fs movl %%esp, %4\n"
726 " fs movl %12, %%esp\n"
727 " fs movl %%eax, %0\n"
728 " fs movl %%ecx, %1\n"
729 " fs movl %%edx, %2\n"
730 " fs movl %%ebx, %3\n"
731 " fs movl %%ebp, %5\n"
732 " fs movl %%esi, %6\n"
733 " fs movl %%edi, %7\n"
734 " pushf\n"
735 " popl %%eax\n"
736 " movl %%eax, %%ecx\n"
737 " andl $0x400, %%ecx\n"
738 " shrl $9, %%ecx\n"
739 " andl $0x8d5, %%eax\n"
740 " fs movl %%eax, %8\n"
741 " movl $1, %%eax\n"
742 " subl %%ecx, %%eax\n"
743 " fs movl %%eax, %11\n"
744 " fs movl %9, %%ebx\n" /* get T0 value */
745 " popl %%ebp\n"
746 :
747 : "m" (*(uint8_t *)offsetof(CPUState, regs[0])),
748 "m" (*(uint8_t *)offsetof(CPUState, regs[1])),
749 "m" (*(uint8_t *)offsetof(CPUState, regs[2])),
750 "m" (*(uint8_t *)offsetof(CPUState, regs[3])),
751 "m" (*(uint8_t *)offsetof(CPUState, regs[4])),
752 "m" (*(uint8_t *)offsetof(CPUState, regs[5])),
753 "m" (*(uint8_t *)offsetof(CPUState, regs[6])),
754 "m" (*(uint8_t *)offsetof(CPUState, regs[7])),
755 "m" (*(uint8_t *)offsetof(CPUState, cc_src)),
756 "m" (*(uint8_t *)offsetof(CPUState, tmp0)),
757 "a" (gen_func),
758 "m" (*(uint8_t *)offsetof(CPUState, df)),
759 "m" (*(uint8_t *)offsetof(CPUState, saved_esp))
760 : "%ecx", "%edx"
761 );
762 }
763}
bellardb8076a72005-04-07 22:20:31 +0000764#elif defined(__ia64)
765 struct fptr {
766 void *ip;
767 void *gp;
768 } fp;
769
770 fp.ip = tc_ptr;
771 fp.gp = code_gen_buffer + 2 * (1 << 20);
772 (*(void (*)(void)) &fp)();
bellard3fb2ded2003-06-24 13:22:59 +0000773#else
774 gen_func();
775#endif
bellard83479e72003-06-25 16:12:37 +0000776 env->current_tb = NULL;
bellard4cbf74b2003-08-10 21:48:43 +0000777 /* reset soft MMU for next block (it can currently
778 only be set by a memory fault) */
779#if defined(TARGET_I386) && !defined(CONFIG_SOFTMMU)
bellard3f337312003-08-20 23:02:09 +0000780 if (env->hflags & HF_SOFTMMU_MASK) {
781 env->hflags &= ~HF_SOFTMMU_MASK;
bellard4cbf74b2003-08-10 21:48:43 +0000782 /* do not allow linking to another block */
783 T0 = 0;
784 }
785#endif
bellardf32fc642006-02-08 22:43:39 +0000786#if defined(USE_KQEMU)
787#define MIN_CYCLE_BEFORE_SWITCH (100 * 1000)
788 if (kqemu_is_ok(env) &&
789 (cpu_get_time_fast() - env->last_io_time) >= MIN_CYCLE_BEFORE_SWITCH) {
790 cpu_loop_exit();
791 }
792#endif
bellard3fb2ded2003-06-24 13:22:59 +0000793 }
794 } else {
bellard0d1a29f2004-10-12 22:01:28 +0000795 env_to_regs();
bellard7d132992003-03-06 23:23:54 +0000796 }
bellard3fb2ded2003-06-24 13:22:59 +0000797 } /* for(;;) */
798
bellard7d132992003-03-06 23:23:54 +0000799
bellarde4533c72003-06-15 19:51:39 +0000800#if defined(TARGET_I386)
bellard97eb5b12004-02-25 23:19:55 +0000801#if defined(USE_CODE_COPY)
802 if (env->native_fp_regs) {
803 save_native_fp_state(env);
804 }
805#endif
bellard9de5e442003-03-23 16:49:39 +0000806 /* restore flags in standard format */
bellardfc2b4c42003-03-29 16:52:44 +0000807 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
bellard9de5e442003-03-23 16:49:39 +0000808
bellard7d132992003-03-06 23:23:54 +0000809 /* restore global registers */
bellard04369ff2003-03-20 22:33:23 +0000810#ifdef reg_EAX
811 EAX = saved_EAX;
812#endif
813#ifdef reg_ECX
814 ECX = saved_ECX;
815#endif
816#ifdef reg_EDX
817 EDX = saved_EDX;
818#endif
819#ifdef reg_EBX
820 EBX = saved_EBX;
821#endif
822#ifdef reg_ESP
823 ESP = saved_ESP;
824#endif
825#ifdef reg_EBP
826 EBP = saved_EBP;
827#endif
828#ifdef reg_ESI
829 ESI = saved_ESI;
830#endif
831#ifdef reg_EDI
832 EDI = saved_EDI;
833#endif
bellarde4533c72003-06-15 19:51:39 +0000834#elif defined(TARGET_ARM)
bellardb7bcbe92005-02-22 19:27:29 +0000835 /* XXX: Save/restore host fpu exception state?. */
bellard93ac68b2003-09-30 20:57:29 +0000836#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +0000837#if defined(reg_REGWPTR)
838 REGWPTR = saved_regwptr;
839#endif
bellard67867302003-11-23 17:05:30 +0000840#elif defined(TARGET_PPC)
bellard6af0bf92005-07-02 14:58:51 +0000841#elif defined(TARGET_MIPS)
bellardfdf9b3e2006-04-27 21:07:38 +0000842#elif defined(TARGET_SH4)
843 /* XXXXX */
bellarde4533c72003-06-15 19:51:39 +0000844#else
845#error unsupported target CPU
846#endif
bellardfdbb4692006-06-14 17:32:25 +0000847#if defined(__sparc__) && !defined(HOST_SOLARIS)
bellard8c6939c2003-06-09 15:28:00 +0000848 asm volatile ("mov %0, %%i7" : : "r" (saved_i7));
849#endif
bellard7d132992003-03-06 23:23:54 +0000850 T0 = saved_T0;
851 T1 = saved_T1;
bellard34751872005-07-02 14:31:34 +0000852#if defined(reg_T2)
bellarde4533c72003-06-15 19:51:39 +0000853 T2 = saved_T2;
bellard34751872005-07-02 14:31:34 +0000854#endif
bellard7d132992003-03-06 23:23:54 +0000855 env = saved_env;
bellard6a00d602005-11-21 23:25:50 +0000856 /* fail safe : never use cpu_single_env outside cpu_exec() */
857 cpu_single_env = NULL;
bellard7d132992003-03-06 23:23:54 +0000858 return ret;
859}
bellard6dbad632003-03-16 18:05:05 +0000860
bellardfbf9eeb2004-04-25 21:21:33 +0000861/* must only be called from the generated code as an exception can be
862 generated */
863void tb_invalidate_page_range(target_ulong start, target_ulong end)
864{
bellarddc5d0b32004-06-22 18:43:30 +0000865 /* XXX: cannot enable it yet because it yields to MMU exception
866 where NIP != read address on PowerPC */
867#if 0
bellardfbf9eeb2004-04-25 21:21:33 +0000868 target_ulong phys_addr;
869 phys_addr = get_phys_addr_code(env, start);
870 tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
bellarddc5d0b32004-06-22 18:43:30 +0000871#endif
bellardfbf9eeb2004-04-25 21:21:33 +0000872}
873
bellard1a18c712003-10-30 01:07:51 +0000874#if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
bellarde4533c72003-06-15 19:51:39 +0000875
bellard6dbad632003-03-16 18:05:05 +0000876void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
877{
878 CPUX86State *saved_env;
879
880 saved_env = env;
881 env = s;
bellarda412ac52003-07-26 18:01:40 +0000882 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
bellarda513fe12003-05-27 23:29:48 +0000883 selector &= 0xffff;
bellard2e255c62003-08-21 23:25:21 +0000884 cpu_x86_load_seg_cache(env, seg_reg, selector,
bellardc27004e2005-01-03 23:35:10 +0000885 (selector << 4), 0xffff, 0);
bellarda513fe12003-05-27 23:29:48 +0000886 } else {
bellardb453b702004-01-04 15:45:21 +0000887 load_seg(seg_reg, selector);
bellarda513fe12003-05-27 23:29:48 +0000888 }
bellard6dbad632003-03-16 18:05:05 +0000889 env = saved_env;
890}
bellard9de5e442003-03-23 16:49:39 +0000891
bellardd0a1ffc2003-05-29 20:04:28 +0000892void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
893{
894 CPUX86State *saved_env;
895
896 saved_env = env;
897 env = s;
898
bellardc27004e2005-01-03 23:35:10 +0000899 helper_fsave((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000900
901 env = saved_env;
902}
903
904void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
905{
906 CPUX86State *saved_env;
907
908 saved_env = env;
909 env = s;
910
bellardc27004e2005-01-03 23:35:10 +0000911 helper_frstor((target_ulong)ptr, data32);
bellardd0a1ffc2003-05-29 20:04:28 +0000912
913 env = saved_env;
914}
915
bellarde4533c72003-06-15 19:51:39 +0000916#endif /* TARGET_I386 */
917
bellard67b915a2004-03-31 23:37:16 +0000918#if !defined(CONFIG_SOFTMMU)
919
bellard3fb2ded2003-06-24 13:22:59 +0000920#if defined(TARGET_I386)
921
bellardb56dad12003-05-08 15:38:04 +0000922/* 'pc' is the host PC at which the exception was raised. 'address' is
bellardfd6ce8f2003-05-14 19:00:11 +0000923 the effective address of the memory exception. 'is_write' is 1 if a
924 write caused the exception and otherwise 0'. 'old_set' is the
925 signal set which should be restored */
bellard2b413142003-05-14 23:01:10 +0000926static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000927 int is_write, sigset_t *old_set,
928 void *puc)
bellard9de5e442003-03-23 16:49:39 +0000929{
bellarda513fe12003-05-27 23:29:48 +0000930 TranslationBlock *tb;
931 int ret;
bellard68a79312003-06-30 13:12:32 +0000932
bellard83479e72003-06-25 16:12:37 +0000933 if (cpu_single_env)
934 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellardfd6ce8f2003-05-14 19:00:11 +0000935#if defined(DEBUG_SIGNAL)
bellardbf3e8bf2004-02-16 21:58:54 +0000936 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
937 pc, address, is_write, *(unsigned long *)old_set);
bellard9de5e442003-03-23 16:49:39 +0000938#endif
bellard25eb4482003-05-14 21:50:54 +0000939 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000940 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellardfd6ce8f2003-05-14 19:00:11 +0000941 return 1;
942 }
bellardfbf9eeb2004-04-25 21:21:33 +0000943
bellard3fb2ded2003-06-24 13:22:59 +0000944 /* see if it is an MMU fault */
bellard93a40ea2003-10-27 21:13:06 +0000945 ret = cpu_x86_handle_mmu_fault(env, address, is_write,
946 ((env->hflags & HF_CPL_MASK) == 3), 0);
bellard3fb2ded2003-06-24 13:22:59 +0000947 if (ret < 0)
948 return 0; /* not an MMU fault */
949 if (ret == 0)
950 return 1; /* the MMU fault was handled without causing real CPU fault */
951 /* now we have a real cpu fault */
bellarda513fe12003-05-27 23:29:48 +0000952 tb = tb_find_pc(pc);
953 if (tb) {
bellard9de5e442003-03-23 16:49:39 +0000954 /* the PC is inside the translated code. It means that we have
955 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +0000956 cpu_restore_state(tb, env, pc, puc);
bellard3fb2ded2003-06-24 13:22:59 +0000957 }
bellard4cbf74b2003-08-10 21:48:43 +0000958 if (ret == 1) {
bellard3fb2ded2003-06-24 13:22:59 +0000959#if 0
bellard4cbf74b2003-08-10 21:48:43 +0000960 printf("PF exception: EIP=0x%08x CR2=0x%08x error=0x%x\n",
961 env->eip, env->cr[2], env->error_code);
bellard3fb2ded2003-06-24 13:22:59 +0000962#endif
bellard4cbf74b2003-08-10 21:48:43 +0000963 /* we restore the process signal mask as the sigreturn should
964 do it (XXX: use sigsetjmp) */
965 sigprocmask(SIG_SETMASK, old_set, NULL);
bellard54ca9092005-12-04 18:46:06 +0000966 raise_exception_err(env->exception_index, env->error_code);
bellard4cbf74b2003-08-10 21:48:43 +0000967 } else {
968 /* activate soft MMU for this block */
bellard3f337312003-08-20 23:02:09 +0000969 env->hflags |= HF_SOFTMMU_MASK;
bellardfbf9eeb2004-04-25 21:21:33 +0000970 cpu_resume_from_signal(env, puc);
bellard4cbf74b2003-08-10 21:48:43 +0000971 }
bellard3fb2ded2003-06-24 13:22:59 +0000972 /* never comes here */
973 return 1;
974}
975
bellarde4533c72003-06-15 19:51:39 +0000976#elif defined(TARGET_ARM)
bellard3fb2ded2003-06-24 13:22:59 +0000977static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +0000978 int is_write, sigset_t *old_set,
979 void *puc)
bellard3fb2ded2003-06-24 13:22:59 +0000980{
bellard68016c62005-02-07 23:12:27 +0000981 TranslationBlock *tb;
982 int ret;
983
984 if (cpu_single_env)
985 env = cpu_single_env; /* XXX: find a correct solution for multithread */
986#if defined(DEBUG_SIGNAL)
987 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
988 pc, address, is_write, *(unsigned long *)old_set);
989#endif
bellard9f0777e2005-02-02 20:42:01 +0000990 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +0000991 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellard9f0777e2005-02-02 20:42:01 +0000992 return 1;
993 }
bellard68016c62005-02-07 23:12:27 +0000994 /* see if it is an MMU fault */
995 ret = cpu_arm_handle_mmu_fault(env, address, is_write, 1, 0);
996 if (ret < 0)
997 return 0; /* not an MMU fault */
998 if (ret == 0)
999 return 1; /* the MMU fault was handled without causing real CPU fault */
1000 /* now we have a real cpu fault */
1001 tb = tb_find_pc(pc);
1002 if (tb) {
1003 /* the PC is inside the translated code. It means that we have
1004 a virtual CPU fault */
1005 cpu_restore_state(tb, env, pc, puc);
1006 }
1007 /* we restore the process signal mask as the sigreturn should
1008 do it (XXX: use sigsetjmp) */
1009 sigprocmask(SIG_SETMASK, old_set, NULL);
1010 cpu_loop_exit();
bellard3fb2ded2003-06-24 13:22:59 +00001011}
bellard93ac68b2003-09-30 20:57:29 +00001012#elif defined(TARGET_SPARC)
1013static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +00001014 int is_write, sigset_t *old_set,
1015 void *puc)
bellard93ac68b2003-09-30 20:57:29 +00001016{
bellard68016c62005-02-07 23:12:27 +00001017 TranslationBlock *tb;
1018 int ret;
1019
1020 if (cpu_single_env)
1021 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1022#if defined(DEBUG_SIGNAL)
1023 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1024 pc, address, is_write, *(unsigned long *)old_set);
1025#endif
bellardb453b702004-01-04 15:45:21 +00001026 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +00001027 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellardb453b702004-01-04 15:45:21 +00001028 return 1;
1029 }
bellard68016c62005-02-07 23:12:27 +00001030 /* see if it is an MMU fault */
1031 ret = cpu_sparc_handle_mmu_fault(env, address, is_write, 1, 0);
1032 if (ret < 0)
1033 return 0; /* not an MMU fault */
1034 if (ret == 0)
1035 return 1; /* the MMU fault was handled without causing real CPU fault */
1036 /* now we have a real cpu fault */
1037 tb = tb_find_pc(pc);
1038 if (tb) {
1039 /* the PC is inside the translated code. It means that we have
1040 a virtual CPU fault */
1041 cpu_restore_state(tb, env, pc, puc);
1042 }
1043 /* we restore the process signal mask as the sigreturn should
1044 do it (XXX: use sigsetjmp) */
1045 sigprocmask(SIG_SETMASK, old_set, NULL);
1046 cpu_loop_exit();
bellard93ac68b2003-09-30 20:57:29 +00001047}
bellard67867302003-11-23 17:05:30 +00001048#elif defined (TARGET_PPC)
1049static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
bellardbf3e8bf2004-02-16 21:58:54 +00001050 int is_write, sigset_t *old_set,
1051 void *puc)
bellard67867302003-11-23 17:05:30 +00001052{
1053 TranslationBlock *tb;
bellardce097762004-01-04 23:53:18 +00001054 int ret;
bellard67867302003-11-23 17:05:30 +00001055
bellard67867302003-11-23 17:05:30 +00001056 if (cpu_single_env)
1057 env = cpu_single_env; /* XXX: find a correct solution for multithread */
bellard67867302003-11-23 17:05:30 +00001058#if defined(DEBUG_SIGNAL)
1059 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1060 pc, address, is_write, *(unsigned long *)old_set);
1061#endif
1062 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +00001063 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellard67867302003-11-23 17:05:30 +00001064 return 1;
1065 }
1066
bellardce097762004-01-04 23:53:18 +00001067 /* see if it is an MMU fault */
bellard7f957d22004-01-18 23:19:48 +00001068 ret = cpu_ppc_handle_mmu_fault(env, address, is_write, msr_pr, 0);
bellardce097762004-01-04 23:53:18 +00001069 if (ret < 0)
1070 return 0; /* not an MMU fault */
1071 if (ret == 0)
1072 return 1; /* the MMU fault was handled without causing real CPU fault */
1073
bellard67867302003-11-23 17:05:30 +00001074 /* now we have a real cpu fault */
1075 tb = tb_find_pc(pc);
1076 if (tb) {
1077 /* the PC is inside the translated code. It means that we have
1078 a virtual CPU fault */
bellardbf3e8bf2004-02-16 21:58:54 +00001079 cpu_restore_state(tb, env, pc, puc);
bellard67867302003-11-23 17:05:30 +00001080 }
bellardce097762004-01-04 23:53:18 +00001081 if (ret == 1) {
bellard67867302003-11-23 17:05:30 +00001082#if 0
bellardce097762004-01-04 23:53:18 +00001083 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1084 env->nip, env->error_code, tb);
bellard67867302003-11-23 17:05:30 +00001085#endif
1086 /* we restore the process signal mask as the sigreturn should
1087 do it (XXX: use sigsetjmp) */
bellardbf3e8bf2004-02-16 21:58:54 +00001088 sigprocmask(SIG_SETMASK, old_set, NULL);
bellard9fddaa02004-05-21 12:59:32 +00001089 do_raise_exception_err(env->exception_index, env->error_code);
bellardce097762004-01-04 23:53:18 +00001090 } else {
1091 /* activate soft MMU for this block */
bellardfbf9eeb2004-04-25 21:21:33 +00001092 cpu_resume_from_signal(env, puc);
bellardce097762004-01-04 23:53:18 +00001093 }
bellard67867302003-11-23 17:05:30 +00001094 /* never comes here */
1095 return 1;
1096}
bellard6af0bf92005-07-02 14:58:51 +00001097
1098#elif defined (TARGET_MIPS)
1099static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1100 int is_write, sigset_t *old_set,
1101 void *puc)
1102{
1103 TranslationBlock *tb;
1104 int ret;
1105
1106 if (cpu_single_env)
1107 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1108#if defined(DEBUG_SIGNAL)
1109 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1110 pc, address, is_write, *(unsigned long *)old_set);
1111#endif
1112 /* XXX: locking issue */
pbrook53a59602006-03-25 19:31:22 +00001113 if (is_write && page_unprotect(h2g(address), pc, puc)) {
bellard6af0bf92005-07-02 14:58:51 +00001114 return 1;
1115 }
1116
1117 /* see if it is an MMU fault */
bellardcc9442b2005-11-26 18:43:28 +00001118 ret = cpu_mips_handle_mmu_fault(env, address, is_write, 1, 0);
bellard6af0bf92005-07-02 14:58:51 +00001119 if (ret < 0)
1120 return 0; /* not an MMU fault */
1121 if (ret == 0)
1122 return 1; /* the MMU fault was handled without causing real CPU fault */
1123
1124 /* now we have a real cpu fault */
1125 tb = tb_find_pc(pc);
1126 if (tb) {
1127 /* the PC is inside the translated code. It means that we have
1128 a virtual CPU fault */
1129 cpu_restore_state(tb, env, pc, puc);
1130 }
1131 if (ret == 1) {
1132#if 0
1133 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1134 env->nip, env->error_code, tb);
1135#endif
1136 /* we restore the process signal mask as the sigreturn should
1137 do it (XXX: use sigsetjmp) */
1138 sigprocmask(SIG_SETMASK, old_set, NULL);
1139 do_raise_exception_err(env->exception_index, env->error_code);
1140 } else {
1141 /* activate soft MMU for this block */
1142 cpu_resume_from_signal(env, puc);
1143 }
1144 /* never comes here */
1145 return 1;
1146}
1147
bellardfdf9b3e2006-04-27 21:07:38 +00001148#elif defined (TARGET_SH4)
1149static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
1150 int is_write, sigset_t *old_set,
1151 void *puc)
1152{
1153 TranslationBlock *tb;
1154 int ret;
1155
1156 if (cpu_single_env)
1157 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1158#if defined(DEBUG_SIGNAL)
1159 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
1160 pc, address, is_write, *(unsigned long *)old_set);
1161#endif
1162 /* XXX: locking issue */
1163 if (is_write && page_unprotect(h2g(address), pc, puc)) {
1164 return 1;
1165 }
1166
1167 /* see if it is an MMU fault */
1168 ret = cpu_sh4_handle_mmu_fault(env, address, is_write, 1, 0);
1169 if (ret < 0)
1170 return 0; /* not an MMU fault */
1171 if (ret == 0)
1172 return 1; /* the MMU fault was handled without causing real CPU fault */
1173
1174 /* now we have a real cpu fault */
1175 tb = tb_find_pc(pc);
1176 if (tb) {
1177 /* the PC is inside the translated code. It means that we have
1178 a virtual CPU fault */
1179 cpu_restore_state(tb, env, pc, puc);
1180 }
bellardfdf9b3e2006-04-27 21:07:38 +00001181#if 0
1182 printf("PF exception: NIP=0x%08x error=0x%x %p\n",
1183 env->nip, env->error_code, tb);
1184#endif
1185 /* we restore the process signal mask as the sigreturn should
1186 do it (XXX: use sigsetjmp) */
pbrook355fb232006-06-17 19:58:25 +00001187 sigprocmask(SIG_SETMASK, old_set, NULL);
1188 cpu_loop_exit();
bellardfdf9b3e2006-04-27 21:07:38 +00001189 /* never comes here */
1190 return 1;
1191}
bellarde4533c72003-06-15 19:51:39 +00001192#else
1193#error unsupported target CPU
1194#endif
bellard9de5e442003-03-23 16:49:39 +00001195
bellard2b413142003-05-14 23:01:10 +00001196#if defined(__i386__)
1197
bellardbf3e8bf2004-02-16 21:58:54 +00001198#if defined(USE_CODE_COPY)
1199static void cpu_send_trap(unsigned long pc, int trap,
1200 struct ucontext *uc)
1201{
1202 TranslationBlock *tb;
1203
1204 if (cpu_single_env)
1205 env = cpu_single_env; /* XXX: find a correct solution for multithread */
1206 /* now we have a real cpu fault */
1207 tb = tb_find_pc(pc);
1208 if (tb) {
1209 /* the PC is inside the translated code. It means that we have
1210 a virtual CPU fault */
1211 cpu_restore_state(tb, env, pc, uc);
1212 }
1213 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
1214 raise_exception_err(trap, env->error_code);
1215}
1216#endif
1217
bellarde4533c72003-06-15 19:51:39 +00001218int cpu_signal_handler(int host_signum, struct siginfo *info,
1219 void *puc)
bellard9de5e442003-03-23 16:49:39 +00001220{
bellard9de5e442003-03-23 16:49:39 +00001221 struct ucontext *uc = puc;
1222 unsigned long pc;
bellardbf3e8bf2004-02-16 21:58:54 +00001223 int trapno;
bellard97eb5b12004-02-25 23:19:55 +00001224
bellardd691f662003-03-24 21:58:34 +00001225#ifndef REG_EIP
1226/* for glibc 2.1 */
bellardfd6ce8f2003-05-14 19:00:11 +00001227#define REG_EIP EIP
1228#define REG_ERR ERR
1229#define REG_TRAPNO TRAPNO
bellardd691f662003-03-24 21:58:34 +00001230#endif
bellardfc2b4c42003-03-29 16:52:44 +00001231 pc = uc->uc_mcontext.gregs[REG_EIP];
bellardbf3e8bf2004-02-16 21:58:54 +00001232 trapno = uc->uc_mcontext.gregs[REG_TRAPNO];
1233#if defined(TARGET_I386) && defined(USE_CODE_COPY)
1234 if (trapno == 0x00 || trapno == 0x05) {
1235 /* send division by zero or bound exception */
1236 cpu_send_trap(pc, trapno, uc);
1237 return 1;
1238 } else
1239#endif
1240 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1241 trapno == 0xe ?
1242 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1243 &uc->uc_sigmask, puc);
bellard2b413142003-05-14 23:01:10 +00001244}
1245
bellardbc51c5c2004-03-17 23:46:04 +00001246#elif defined(__x86_64__)
1247
1248int cpu_signal_handler(int host_signum, struct siginfo *info,
1249 void *puc)
1250{
1251 struct ucontext *uc = puc;
1252 unsigned long pc;
1253
1254 pc = uc->uc_mcontext.gregs[REG_RIP];
1255 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1256 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
1257 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
1258 &uc->uc_sigmask, puc);
1259}
1260
bellard83fb7ad2004-07-05 21:25:26 +00001261#elif defined(__powerpc__)
bellard2b413142003-05-14 23:01:10 +00001262
bellard83fb7ad2004-07-05 21:25:26 +00001263/***********************************************************************
1264 * signal context platform-specific definitions
1265 * From Wine
1266 */
1267#ifdef linux
1268/* All Registers access - only for local access */
1269# define REG_sig(reg_name, context) ((context)->uc_mcontext.regs->reg_name)
1270/* Gpr Registers access */
1271# define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
1272# define IAR_sig(context) REG_sig(nip, context) /* Program counter */
1273# define MSR_sig(context) REG_sig(msr, context) /* Machine State Register (Supervisor) */
1274# define CTR_sig(context) REG_sig(ctr, context) /* Count register */
1275# define XER_sig(context) REG_sig(xer, context) /* User's integer exception register */
1276# define LR_sig(context) REG_sig(link, context) /* Link register */
1277# define CR_sig(context) REG_sig(ccr, context) /* Condition register */
1278/* Float Registers access */
1279# define FLOAT_sig(reg_num, context) (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
1280# define FPSCR_sig(context) (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
1281/* Exception Registers access */
1282# define DAR_sig(context) REG_sig(dar, context)
1283# define DSISR_sig(context) REG_sig(dsisr, context)
1284# define TRAP_sig(context) REG_sig(trap, context)
1285#endif /* linux */
1286
1287#ifdef __APPLE__
1288# include <sys/ucontext.h>
1289typedef struct ucontext SIGCONTEXT;
1290/* All Registers access - only for local access */
1291# define REG_sig(reg_name, context) ((context)->uc_mcontext->ss.reg_name)
1292# define FLOATREG_sig(reg_name, context) ((context)->uc_mcontext->fs.reg_name)
1293# define EXCEPREG_sig(reg_name, context) ((context)->uc_mcontext->es.reg_name)
1294# define VECREG_sig(reg_name, context) ((context)->uc_mcontext->vs.reg_name)
1295/* Gpr Registers access */
1296# define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
1297# define IAR_sig(context) REG_sig(srr0, context) /* Program counter */
1298# define MSR_sig(context) REG_sig(srr1, context) /* Machine State Register (Supervisor) */
1299# define CTR_sig(context) REG_sig(ctr, context)
1300# define XER_sig(context) REG_sig(xer, context) /* Link register */
1301# define LR_sig(context) REG_sig(lr, context) /* User's integer exception register */
1302# define CR_sig(context) REG_sig(cr, context) /* Condition register */
1303/* Float Registers access */
1304# define FLOAT_sig(reg_num, context) FLOATREG_sig(fpregs[reg_num], context)
1305# define FPSCR_sig(context) ((double)FLOATREG_sig(fpscr, context))
1306/* Exception Registers access */
1307# define DAR_sig(context) EXCEPREG_sig(dar, context) /* Fault registers for coredump */
1308# define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
1309# define TRAP_sig(context) EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
1310#endif /* __APPLE__ */
1311
bellardd1d9f422004-07-14 17:20:55 +00001312int cpu_signal_handler(int host_signum, struct siginfo *info,
bellarde4533c72003-06-15 19:51:39 +00001313 void *puc)
bellard2b413142003-05-14 23:01:10 +00001314{
bellard25eb4482003-05-14 21:50:54 +00001315 struct ucontext *uc = puc;
bellard25eb4482003-05-14 21:50:54 +00001316 unsigned long pc;
bellard25eb4482003-05-14 21:50:54 +00001317 int is_write;
1318
bellard83fb7ad2004-07-05 21:25:26 +00001319 pc = IAR_sig(uc);
bellard25eb4482003-05-14 21:50:54 +00001320 is_write = 0;
1321#if 0
1322 /* ppc 4xx case */
bellard83fb7ad2004-07-05 21:25:26 +00001323 if (DSISR_sig(uc) & 0x00800000)
bellard25eb4482003-05-14 21:50:54 +00001324 is_write = 1;
bellard9de5e442003-03-23 16:49:39 +00001325#else
bellard83fb7ad2004-07-05 21:25:26 +00001326 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
bellard25eb4482003-05-14 21:50:54 +00001327 is_write = 1;
1328#endif
1329 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001330 is_write, &uc->uc_sigmask, puc);
bellard9de5e442003-03-23 16:49:39 +00001331}
bellard2b413142003-05-14 23:01:10 +00001332
bellard2f87c602003-06-02 20:38:09 +00001333#elif defined(__alpha__)
1334
bellarde4533c72003-06-15 19:51:39 +00001335int cpu_signal_handler(int host_signum, struct siginfo *info,
bellard2f87c602003-06-02 20:38:09 +00001336 void *puc)
1337{
1338 struct ucontext *uc = puc;
1339 uint32_t *pc = uc->uc_mcontext.sc_pc;
1340 uint32_t insn = *pc;
1341 int is_write = 0;
1342
bellard8c6939c2003-06-09 15:28:00 +00001343 /* XXX: need kernel patch to get write flag faster */
bellard2f87c602003-06-02 20:38:09 +00001344 switch (insn >> 26) {
1345 case 0x0d: // stw
1346 case 0x0e: // stb
1347 case 0x0f: // stq_u
1348 case 0x24: // stf
1349 case 0x25: // stg
1350 case 0x26: // sts
1351 case 0x27: // stt
1352 case 0x2c: // stl
1353 case 0x2d: // stq
1354 case 0x2e: // stl_c
1355 case 0x2f: // stq_c
1356 is_write = 1;
1357 }
1358
1359 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001360 is_write, &uc->uc_sigmask, puc);
bellard2f87c602003-06-02 20:38:09 +00001361}
bellard8c6939c2003-06-09 15:28:00 +00001362#elif defined(__sparc__)
1363
bellarde4533c72003-06-15 19:51:39 +00001364int cpu_signal_handler(int host_signum, struct siginfo *info,
1365 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001366{
1367 uint32_t *regs = (uint32_t *)(info + 1);
1368 void *sigmask = (regs + 20);
1369 unsigned long pc;
1370 int is_write;
1371 uint32_t insn;
1372
1373 /* XXX: is there a standard glibc define ? */
1374 pc = regs[1];
1375 /* XXX: need kernel patch to get write flag faster */
1376 is_write = 0;
1377 insn = *(uint32_t *)pc;
1378 if ((insn >> 30) == 3) {
1379 switch((insn >> 19) & 0x3f) {
1380 case 0x05: // stb
1381 case 0x06: // sth
1382 case 0x04: // st
1383 case 0x07: // std
1384 case 0x24: // stf
1385 case 0x27: // stdf
1386 case 0x25: // stfsr
1387 is_write = 1;
1388 break;
1389 }
1390 }
1391 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
bellardbf3e8bf2004-02-16 21:58:54 +00001392 is_write, sigmask, NULL);
bellard8c6939c2003-06-09 15:28:00 +00001393}
1394
1395#elif defined(__arm__)
1396
bellarde4533c72003-06-15 19:51:39 +00001397int cpu_signal_handler(int host_signum, struct siginfo *info,
1398 void *puc)
bellard8c6939c2003-06-09 15:28:00 +00001399{
1400 struct ucontext *uc = puc;
1401 unsigned long pc;
1402 int is_write;
1403
1404 pc = uc->uc_mcontext.gregs[R15];
1405 /* XXX: compute is_write */
1406 is_write = 0;
1407 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1408 is_write,
pbrookf3a96762006-07-29 19:09:31 +00001409 &uc->uc_sigmask, puc);
bellard8c6939c2003-06-09 15:28:00 +00001410}
1411
bellard38e584a2003-08-10 22:14:22 +00001412#elif defined(__mc68000)
1413
1414int cpu_signal_handler(int host_signum, struct siginfo *info,
1415 void *puc)
1416{
1417 struct ucontext *uc = puc;
1418 unsigned long pc;
1419 int is_write;
1420
1421 pc = uc->uc_mcontext.gregs[16];
1422 /* XXX: compute is_write */
1423 is_write = 0;
1424 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1425 is_write,
bellardbf3e8bf2004-02-16 21:58:54 +00001426 &uc->uc_sigmask, puc);
bellard38e584a2003-08-10 22:14:22 +00001427}
1428
bellardb8076a72005-04-07 22:20:31 +00001429#elif defined(__ia64)
1430
1431#ifndef __ISR_VALID
1432 /* This ought to be in <bits/siginfo.h>... */
1433# define __ISR_VALID 1
bellardb8076a72005-04-07 22:20:31 +00001434#endif
1435
1436int cpu_signal_handler(int host_signum, struct siginfo *info, void *puc)
1437{
1438 struct ucontext *uc = puc;
1439 unsigned long ip;
1440 int is_write = 0;
1441
1442 ip = uc->uc_mcontext.sc_ip;
1443 switch (host_signum) {
1444 case SIGILL:
1445 case SIGFPE:
1446 case SIGSEGV:
1447 case SIGBUS:
1448 case SIGTRAP:
bellardfd4a43e2006-04-24 20:32:17 +00001449 if (info->si_code && (info->si_segvflags & __ISR_VALID))
bellardb8076a72005-04-07 22:20:31 +00001450 /* ISR.W (write-access) is bit 33: */
1451 is_write = (info->si_isr >> 33) & 1;
1452 break;
1453
1454 default:
1455 break;
1456 }
1457 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
1458 is_write,
1459 &uc->uc_sigmask, puc);
1460}
1461
bellard90cb9492005-07-24 15:11:38 +00001462#elif defined(__s390__)
1463
1464int cpu_signal_handler(int host_signum, struct siginfo *info,
1465 void *puc)
1466{
1467 struct ucontext *uc = puc;
1468 unsigned long pc;
1469 int is_write;
1470
1471 pc = uc->uc_mcontext.psw.addr;
1472 /* XXX: compute is_write */
1473 is_write = 0;
1474 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
1475 is_write,
1476 &uc->uc_sigmask, puc);
1477}
1478
bellard2b413142003-05-14 23:01:10 +00001479#else
1480
bellard3fb2ded2003-06-24 13:22:59 +00001481#error host CPU specific signal handler needed
bellard2b413142003-05-14 23:01:10 +00001482
1483#endif
bellard67b915a2004-03-31 23:37:16 +00001484
1485#endif /* !defined(CONFIG_SOFTMMU) */