Merge remote-tracking branch 'kwolf/for-anthony' into staging
[qemu-kvm.git] / user-exec.c
blobb9ea9dd32f010613ce5349a26a752801fd5e5a1a
1 /*
2 * User emulator execution
4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * 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.
11 * 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
20 #include "cpu.h"
21 #ifndef CONFIG_TCG_PASS_AREG0
22 #include "dyngen-exec.h"
23 #endif
24 #include "disas.h"
25 #include "tcg.h"
27 #undef EAX
28 #undef ECX
29 #undef EDX
30 #undef EBX
31 #undef ESP
32 #undef EBP
33 #undef ESI
34 #undef EDI
35 #undef EIP
36 #include <signal.h>
37 #ifdef __linux__
38 #include <sys/ucontext.h>
39 #endif
41 //#define DEBUG_SIGNAL
43 static void exception_action(CPUArchState *env1)
45 #if defined(TARGET_I386)
46 raise_exception_err(env1, env1->exception_index, env1->error_code);
47 #else
48 cpu_loop_exit(env1);
49 #endif
52 /* exit the current TB from a signal handler. The host registers are
53 restored in a state compatible with the CPU emulator
55 void cpu_resume_from_signal(CPUArchState *env1, void *puc)
57 #ifdef __linux__
58 struct ucontext *uc = puc;
59 #elif defined(__OpenBSD__)
60 struct sigcontext *uc = puc;
61 #endif
63 #ifndef CONFIG_TCG_PASS_AREG0
64 env = env1;
66 /* XXX: restore cpu registers saved in host registers */
67 #endif
69 if (puc) {
70 /* XXX: use siglongjmp ? */
71 #ifdef __linux__
72 #ifdef __ia64
73 sigprocmask(SIG_SETMASK, (sigset_t *)&uc->uc_sigmask, NULL);
74 #else
75 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
76 #endif
77 #elif defined(__OpenBSD__)
78 sigprocmask(SIG_SETMASK, &uc->sc_mask, NULL);
79 #endif
81 env1->exception_index = -1;
82 longjmp(env1->jmp_env, 1);
85 /* 'pc' is the host PC at which the exception was raised. 'address' is
86 the effective address of the memory exception. 'is_write' is 1 if a
87 write caused the exception and otherwise 0'. 'old_set' is the
88 signal set which should be restored */
89 static inline int handle_cpu_signal(uintptr_t pc, unsigned long address,
90 int is_write, sigset_t *old_set,
91 void *puc)
93 TranslationBlock *tb;
94 int ret;
96 #ifndef CONFIG_TCG_PASS_AREG0
97 if (cpu_single_env) {
98 env = cpu_single_env; /* XXX: find a correct solution for multithread */
100 #endif
101 #if defined(DEBUG_SIGNAL)
102 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
103 pc, address, is_write, *(unsigned long *)old_set);
104 #endif
105 /* XXX: locking issue */
106 if (is_write && h2g_valid(address)
107 && page_unprotect(h2g(address), pc, puc)) {
108 return 1;
111 /* see if it is an MMU fault */
112 ret = cpu_handle_mmu_fault(cpu_single_env, address, is_write,
113 MMU_USER_IDX);
114 if (ret < 0) {
115 return 0; /* not an MMU fault */
117 if (ret == 0) {
118 return 1; /* the MMU fault was handled without causing real CPU fault */
120 /* now we have a real cpu fault */
121 tb = tb_find_pc(pc);
122 if (tb) {
123 /* the PC is inside the translated code. It means that we have
124 a virtual CPU fault */
125 cpu_restore_state(tb, cpu_single_env, pc);
128 /* we restore the process signal mask as the sigreturn should
129 do it (XXX: use sigsetjmp) */
130 sigprocmask(SIG_SETMASK, old_set, NULL);
131 exception_action(cpu_single_env);
133 /* never comes here */
134 return 1;
137 #if defined(__i386__)
139 #if defined(__APPLE__)
140 #include <sys/ucontext.h>
142 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext->ss.eip))
143 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
144 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
145 #define MASK_sig(context) ((context)->uc_sigmask)
146 #elif defined(__NetBSD__)
147 #include <ucontext.h>
149 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
150 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
151 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
152 #define MASK_sig(context) ((context)->uc_sigmask)
153 #elif defined(__FreeBSD__) || defined(__DragonFly__)
154 #include <ucontext.h>
156 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
157 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
158 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
159 #define MASK_sig(context) ((context)->uc_sigmask)
160 #elif defined(__OpenBSD__)
161 #define EIP_sig(context) ((context)->sc_eip)
162 #define TRAP_sig(context) ((context)->sc_trapno)
163 #define ERROR_sig(context) ((context)->sc_err)
164 #define MASK_sig(context) ((context)->sc_mask)
165 #else
166 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
167 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
168 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
169 #define MASK_sig(context) ((context)->uc_sigmask)
170 #endif
172 int cpu_signal_handler(int host_signum, void *pinfo,
173 void *puc)
175 siginfo_t *info = pinfo;
176 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
177 ucontext_t *uc = puc;
178 #elif defined(__OpenBSD__)
179 struct sigcontext *uc = puc;
180 #else
181 struct ucontext *uc = puc;
182 #endif
183 unsigned long pc;
184 int trapno;
186 #ifndef REG_EIP
187 /* for glibc 2.1 */
188 #define REG_EIP EIP
189 #define REG_ERR ERR
190 #define REG_TRAPNO TRAPNO
191 #endif
192 pc = EIP_sig(uc);
193 trapno = TRAP_sig(uc);
194 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
195 trapno == 0xe ?
196 (ERROR_sig(uc) >> 1) & 1 : 0,
197 &MASK_sig(uc), puc);
200 #elif defined(__x86_64__)
202 #ifdef __NetBSD__
203 #define PC_sig(context) _UC_MACHINE_PC(context)
204 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
205 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
206 #define MASK_sig(context) ((context)->uc_sigmask)
207 #elif defined(__OpenBSD__)
208 #define PC_sig(context) ((context)->sc_rip)
209 #define TRAP_sig(context) ((context)->sc_trapno)
210 #define ERROR_sig(context) ((context)->sc_err)
211 #define MASK_sig(context) ((context)->sc_mask)
212 #elif defined(__FreeBSD__) || defined(__DragonFly__)
213 #include <ucontext.h>
215 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
216 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
217 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
218 #define MASK_sig(context) ((context)->uc_sigmask)
219 #else
220 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
221 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
222 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
223 #define MASK_sig(context) ((context)->uc_sigmask)
224 #endif
226 int cpu_signal_handler(int host_signum, void *pinfo,
227 void *puc)
229 siginfo_t *info = pinfo;
230 unsigned long pc;
231 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
232 ucontext_t *uc = puc;
233 #elif defined(__OpenBSD__)
234 struct sigcontext *uc = puc;
235 #else
236 struct ucontext *uc = puc;
237 #endif
239 pc = PC_sig(uc);
240 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
241 TRAP_sig(uc) == 0xe ?
242 (ERROR_sig(uc) >> 1) & 1 : 0,
243 &MASK_sig(uc), puc);
246 #elif defined(_ARCH_PPC)
248 /***********************************************************************
249 * signal context platform-specific definitions
250 * From Wine
252 #ifdef linux
253 /* All Registers access - only for local access */
254 #define REG_sig(reg_name, context) \
255 ((context)->uc_mcontext.regs->reg_name)
256 /* Gpr Registers access */
257 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
258 /* Program counter */
259 #define IAR_sig(context) REG_sig(nip, context)
260 /* Machine State Register (Supervisor) */
261 #define MSR_sig(context) REG_sig(msr, context)
262 /* Count register */
263 #define CTR_sig(context) REG_sig(ctr, context)
264 /* User's integer exception register */
265 #define XER_sig(context) REG_sig(xer, context)
266 /* Link register */
267 #define LR_sig(context) REG_sig(link, context)
268 /* Condition register */
269 #define CR_sig(context) REG_sig(ccr, context)
271 /* Float Registers access */
272 #define FLOAT_sig(reg_num, context) \
273 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
274 #define FPSCR_sig(context) \
275 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
276 /* Exception Registers access */
277 #define DAR_sig(context) REG_sig(dar, context)
278 #define DSISR_sig(context) REG_sig(dsisr, context)
279 #define TRAP_sig(context) REG_sig(trap, context)
280 #endif /* linux */
282 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
283 #include <ucontext.h>
284 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
285 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
286 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
287 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
288 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
289 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
290 /* Exception Registers access */
291 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
292 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
293 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
294 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
296 #ifdef __APPLE__
297 #include <sys/ucontext.h>
298 typedef struct ucontext SIGCONTEXT;
299 /* All Registers access - only for local access */
300 #define REG_sig(reg_name, context) \
301 ((context)->uc_mcontext->ss.reg_name)
302 #define FLOATREG_sig(reg_name, context) \
303 ((context)->uc_mcontext->fs.reg_name)
304 #define EXCEPREG_sig(reg_name, context) \
305 ((context)->uc_mcontext->es.reg_name)
306 #define VECREG_sig(reg_name, context) \
307 ((context)->uc_mcontext->vs.reg_name)
308 /* Gpr Registers access */
309 #define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
310 /* Program counter */
311 #define IAR_sig(context) REG_sig(srr0, context)
312 /* Machine State Register (Supervisor) */
313 #define MSR_sig(context) REG_sig(srr1, context)
314 #define CTR_sig(context) REG_sig(ctr, context)
315 /* Link register */
316 #define XER_sig(context) REG_sig(xer, context)
317 /* User's integer exception register */
318 #define LR_sig(context) REG_sig(lr, context)
319 /* Condition register */
320 #define CR_sig(context) REG_sig(cr, context)
321 /* Float Registers access */
322 #define FLOAT_sig(reg_num, context) \
323 FLOATREG_sig(fpregs[reg_num], context)
324 #define FPSCR_sig(context) \
325 ((double)FLOATREG_sig(fpscr, context))
326 /* Exception Registers access */
327 /* Fault registers for coredump */
328 #define DAR_sig(context) EXCEPREG_sig(dar, context)
329 #define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
330 /* number of powerpc exception taken */
331 #define TRAP_sig(context) EXCEPREG_sig(exception, context)
332 #endif /* __APPLE__ */
334 int cpu_signal_handler(int host_signum, void *pinfo,
335 void *puc)
337 siginfo_t *info = pinfo;
338 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
339 ucontext_t *uc = puc;
340 #else
341 struct ucontext *uc = puc;
342 #endif
343 unsigned long pc;
344 int is_write;
346 pc = IAR_sig(uc);
347 is_write = 0;
348 #if 0
349 /* ppc 4xx case */
350 if (DSISR_sig(uc) & 0x00800000) {
351 is_write = 1;
353 #else
354 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) {
355 is_write = 1;
357 #endif
358 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
359 is_write, &uc->uc_sigmask, puc);
362 #elif defined(__alpha__)
364 int cpu_signal_handler(int host_signum, void *pinfo,
365 void *puc)
367 siginfo_t *info = pinfo;
368 struct ucontext *uc = puc;
369 uint32_t *pc = uc->uc_mcontext.sc_pc;
370 uint32_t insn = *pc;
371 int is_write = 0;
373 /* XXX: need kernel patch to get write flag faster */
374 switch (insn >> 26) {
375 case 0x0d: /* stw */
376 case 0x0e: /* stb */
377 case 0x0f: /* stq_u */
378 case 0x24: /* stf */
379 case 0x25: /* stg */
380 case 0x26: /* sts */
381 case 0x27: /* stt */
382 case 0x2c: /* stl */
383 case 0x2d: /* stq */
384 case 0x2e: /* stl_c */
385 case 0x2f: /* stq_c */
386 is_write = 1;
389 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
390 is_write, &uc->uc_sigmask, puc);
392 #elif defined(__sparc__)
394 int cpu_signal_handler(int host_signum, void *pinfo,
395 void *puc)
397 siginfo_t *info = pinfo;
398 int is_write;
399 uint32_t insn;
400 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
401 uint32_t *regs = (uint32_t *)(info + 1);
402 void *sigmask = (regs + 20);
403 /* XXX: is there a standard glibc define ? */
404 unsigned long pc = regs[1];
405 #else
406 #ifdef __linux__
407 struct sigcontext *sc = puc;
408 unsigned long pc = sc->sigc_regs.tpc;
409 void *sigmask = (void *)sc->sigc_mask;
410 #elif defined(__OpenBSD__)
411 struct sigcontext *uc = puc;
412 unsigned long pc = uc->sc_pc;
413 void *sigmask = (void *)(long)uc->sc_mask;
414 #endif
415 #endif
417 /* XXX: need kernel patch to get write flag faster */
418 is_write = 0;
419 insn = *(uint32_t *)pc;
420 if ((insn >> 30) == 3) {
421 switch ((insn >> 19) & 0x3f) {
422 case 0x05: /* stb */
423 case 0x15: /* stba */
424 case 0x06: /* sth */
425 case 0x16: /* stha */
426 case 0x04: /* st */
427 case 0x14: /* sta */
428 case 0x07: /* std */
429 case 0x17: /* stda */
430 case 0x0e: /* stx */
431 case 0x1e: /* stxa */
432 case 0x24: /* stf */
433 case 0x34: /* stfa */
434 case 0x27: /* stdf */
435 case 0x37: /* stdfa */
436 case 0x26: /* stqf */
437 case 0x36: /* stqfa */
438 case 0x25: /* stfsr */
439 case 0x3c: /* casa */
440 case 0x3e: /* casxa */
441 is_write = 1;
442 break;
445 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
446 is_write, sigmask, NULL);
449 #elif defined(__arm__)
451 int cpu_signal_handler(int host_signum, void *pinfo,
452 void *puc)
454 siginfo_t *info = pinfo;
455 struct ucontext *uc = puc;
456 unsigned long pc;
457 int is_write;
459 #if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
460 pc = uc->uc_mcontext.gregs[R15];
461 #else
462 pc = uc->uc_mcontext.arm_pc;
463 #endif
464 /* XXX: compute is_write */
465 is_write = 0;
466 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
467 is_write,
468 &uc->uc_sigmask, puc);
471 #elif defined(__mc68000)
473 int cpu_signal_handler(int host_signum, void *pinfo,
474 void *puc)
476 siginfo_t *info = pinfo;
477 struct ucontext *uc = puc;
478 unsigned long pc;
479 int is_write;
481 pc = uc->uc_mcontext.gregs[16];
482 /* XXX: compute is_write */
483 is_write = 0;
484 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
485 is_write,
486 &uc->uc_sigmask, puc);
489 #elif defined(__ia64)
491 #ifndef __ISR_VALID
492 /* This ought to be in <bits/siginfo.h>... */
493 # define __ISR_VALID 1
494 #endif
496 int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
498 siginfo_t *info = pinfo;
499 struct ucontext *uc = puc;
500 unsigned long ip;
501 int is_write = 0;
503 ip = uc->uc_mcontext.sc_ip;
504 switch (host_signum) {
505 case SIGILL:
506 case SIGFPE:
507 case SIGSEGV:
508 case SIGBUS:
509 case SIGTRAP:
510 if (info->si_code && (info->si_segvflags & __ISR_VALID)) {
511 /* ISR.W (write-access) is bit 33: */
512 is_write = (info->si_isr >> 33) & 1;
514 break;
516 default:
517 break;
519 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
520 is_write,
521 (sigset_t *)&uc->uc_sigmask, puc);
524 #elif defined(__s390__)
526 int cpu_signal_handler(int host_signum, void *pinfo,
527 void *puc)
529 siginfo_t *info = pinfo;
530 struct ucontext *uc = puc;
531 unsigned long pc;
532 uint16_t *pinsn;
533 int is_write = 0;
535 pc = uc->uc_mcontext.psw.addr;
537 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
538 of the normal 2 arguments. The 3rd argument contains the "int_code"
539 from the hardware which does in fact contain the is_write value.
540 The rt signal handler, as far as I can tell, does not give this value
541 at all. Not that we could get to it from here even if it were. */
542 /* ??? This is not even close to complete, since it ignores all
543 of the read-modify-write instructions. */
544 pinsn = (uint16_t *)pc;
545 switch (pinsn[0] >> 8) {
546 case 0x50: /* ST */
547 case 0x42: /* STC */
548 case 0x40: /* STH */
549 is_write = 1;
550 break;
551 case 0xc4: /* RIL format insns */
552 switch (pinsn[0] & 0xf) {
553 case 0xf: /* STRL */
554 case 0xb: /* STGRL */
555 case 0x7: /* STHRL */
556 is_write = 1;
558 break;
559 case 0xe3: /* RXY format insns */
560 switch (pinsn[2] & 0xff) {
561 case 0x50: /* STY */
562 case 0x24: /* STG */
563 case 0x72: /* STCY */
564 case 0x70: /* STHY */
565 case 0x8e: /* STPQ */
566 case 0x3f: /* STRVH */
567 case 0x3e: /* STRV */
568 case 0x2f: /* STRVG */
569 is_write = 1;
571 break;
573 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
574 is_write, &uc->uc_sigmask, puc);
577 #elif defined(__mips__)
579 int cpu_signal_handler(int host_signum, void *pinfo,
580 void *puc)
582 siginfo_t *info = pinfo;
583 struct ucontext *uc = puc;
584 greg_t pc = uc->uc_mcontext.pc;
585 int is_write;
587 /* XXX: compute is_write */
588 is_write = 0;
589 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
590 is_write, &uc->uc_sigmask, puc);
593 #elif defined(__hppa__)
595 int cpu_signal_handler(int host_signum, void *pinfo,
596 void *puc)
598 siginfo_t *info = pinfo;
599 struct ucontext *uc = puc;
600 unsigned long pc = uc->uc_mcontext.sc_iaoq[0];
601 uint32_t insn = *(uint32_t *)pc;
602 int is_write = 0;
604 /* XXX: need kernel patch to get write flag faster. */
605 switch (insn >> 26) {
606 case 0x1a: /* STW */
607 case 0x19: /* STH */
608 case 0x18: /* STB */
609 case 0x1b: /* STWM */
610 is_write = 1;
611 break;
613 case 0x09: /* CSTWX, FSTWX, FSTWS */
614 case 0x0b: /* CSTDX, FSTDX, FSTDS */
615 /* Distinguish from coprocessor load ... */
616 is_write = (insn >> 9) & 1;
617 break;
619 case 0x03:
620 switch ((insn >> 6) & 15) {
621 case 0xa: /* STWS */
622 case 0x9: /* STHS */
623 case 0x8: /* STBS */
624 case 0xe: /* STWAS */
625 case 0xc: /* STBYS */
626 is_write = 1;
628 break;
631 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
632 is_write, &uc->uc_sigmask, puc);
635 #else
637 #error host CPU specific signal handler needed
639 #endif