exec: Change cpu_watchpoint_{insert,remove{,_by_ref,_all}} argument
[qemu/kevin.git] / user-exec.c
blob75c6d5486bb44113ae936ac442bd9a05fe435b91
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 #include "disas/disas.h"
22 #include "tcg.h"
23 #include "qemu/bitops.h"
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 #ifdef __linux__
36 #include <sys/ucontext.h>
37 #endif
39 //#define DEBUG_SIGNAL
41 static void exception_action(CPUArchState *env1)
43 CPUState *cpu = ENV_GET_CPU(env1);
45 #if defined(TARGET_I386)
46 raise_exception_err(env1, cpu->exception_index, env1->error_code);
47 #else
48 cpu_loop_exit(cpu);
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 CPUState *cpu = ENV_GET_CPU(env1);
58 #ifdef __linux__
59 struct ucontext *uc = puc;
60 #elif defined(__OpenBSD__)
61 struct sigcontext *uc = puc;
62 #endif
64 if (puc) {
65 /* XXX: use siglongjmp ? */
66 #ifdef __linux__
67 #ifdef __ia64
68 sigprocmask(SIG_SETMASK, (sigset_t *)&uc->uc_sigmask, NULL);
69 #else
70 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
71 #endif
72 #elif defined(__OpenBSD__)
73 sigprocmask(SIG_SETMASK, &uc->sc_mask, NULL);
74 #endif
76 cpu->exception_index = -1;
77 siglongjmp(cpu->jmp_env, 1);
80 /* 'pc' is the host PC at which the exception was raised. 'address' is
81 the effective address of the memory exception. 'is_write' is 1 if a
82 write caused the exception and otherwise 0'. 'old_set' is the
83 signal set which should be restored */
84 static inline int handle_cpu_signal(uintptr_t pc, unsigned long address,
85 int is_write, sigset_t *old_set,
86 void *puc)
88 CPUState *cpu;
89 CPUClass *cc;
90 CPUArchState *env;
91 int ret;
93 #if defined(DEBUG_SIGNAL)
94 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
95 pc, address, is_write, *(unsigned long *)old_set);
96 #endif
97 /* XXX: locking issue */
98 if (is_write && h2g_valid(address)
99 && page_unprotect(h2g(address), pc, puc)) {
100 return 1;
103 /* Convert forcefully to guest address space, invalid addresses
104 are still valid segv ones */
105 address = h2g_nocheck(address);
107 cpu = current_cpu;
108 cc = CPU_GET_CLASS(cpu);
109 env = cpu->env_ptr;
110 /* see if it is an MMU fault */
111 g_assert(cc->handle_mmu_fault);
112 ret = cc->handle_mmu_fault(cpu, address, is_write, MMU_USER_IDX);
113 if (ret < 0) {
114 return 0; /* not an MMU fault */
116 if (ret == 0) {
117 return 1; /* the MMU fault was handled without causing real CPU fault */
119 /* now we have a real cpu fault */
120 cpu_restore_state(cpu, pc);
122 /* we restore the process signal mask as the sigreturn should
123 do it (XXX: use sigsetjmp) */
124 sigprocmask(SIG_SETMASK, old_set, NULL);
125 exception_action(env);
127 /* never comes here */
128 return 1;
131 #if defined(__i386__)
133 #if defined(__APPLE__)
134 #include <sys/ucontext.h>
136 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext->ss.eip))
137 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
138 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
139 #define MASK_sig(context) ((context)->uc_sigmask)
140 #elif defined(__NetBSD__)
141 #include <ucontext.h>
143 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
144 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
145 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
146 #define MASK_sig(context) ((context)->uc_sigmask)
147 #elif defined(__FreeBSD__) || defined(__DragonFly__)
148 #include <ucontext.h>
150 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
151 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
152 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
153 #define MASK_sig(context) ((context)->uc_sigmask)
154 #elif defined(__OpenBSD__)
155 #define EIP_sig(context) ((context)->sc_eip)
156 #define TRAP_sig(context) ((context)->sc_trapno)
157 #define ERROR_sig(context) ((context)->sc_err)
158 #define MASK_sig(context) ((context)->sc_mask)
159 #else
160 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
161 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
162 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
163 #define MASK_sig(context) ((context)->uc_sigmask)
164 #endif
166 int cpu_signal_handler(int host_signum, void *pinfo,
167 void *puc)
169 siginfo_t *info = pinfo;
170 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
171 ucontext_t *uc = puc;
172 #elif defined(__OpenBSD__)
173 struct sigcontext *uc = puc;
174 #else
175 struct ucontext *uc = puc;
176 #endif
177 unsigned long pc;
178 int trapno;
180 #ifndef REG_EIP
181 /* for glibc 2.1 */
182 #define REG_EIP EIP
183 #define REG_ERR ERR
184 #define REG_TRAPNO TRAPNO
185 #endif
186 pc = EIP_sig(uc);
187 trapno = TRAP_sig(uc);
188 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
189 trapno == 0xe ?
190 (ERROR_sig(uc) >> 1) & 1 : 0,
191 &MASK_sig(uc), puc);
194 #elif defined(__x86_64__)
196 #ifdef __NetBSD__
197 #define PC_sig(context) _UC_MACHINE_PC(context)
198 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
199 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
200 #define MASK_sig(context) ((context)->uc_sigmask)
201 #elif defined(__OpenBSD__)
202 #define PC_sig(context) ((context)->sc_rip)
203 #define TRAP_sig(context) ((context)->sc_trapno)
204 #define ERROR_sig(context) ((context)->sc_err)
205 #define MASK_sig(context) ((context)->sc_mask)
206 #elif defined(__FreeBSD__) || defined(__DragonFly__)
207 #include <ucontext.h>
209 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
210 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
211 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
212 #define MASK_sig(context) ((context)->uc_sigmask)
213 #else
214 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
215 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
216 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
217 #define MASK_sig(context) ((context)->uc_sigmask)
218 #endif
220 int cpu_signal_handler(int host_signum, void *pinfo,
221 void *puc)
223 siginfo_t *info = pinfo;
224 unsigned long pc;
225 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
226 ucontext_t *uc = puc;
227 #elif defined(__OpenBSD__)
228 struct sigcontext *uc = puc;
229 #else
230 struct ucontext *uc = puc;
231 #endif
233 pc = PC_sig(uc);
234 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
235 TRAP_sig(uc) == 0xe ?
236 (ERROR_sig(uc) >> 1) & 1 : 0,
237 &MASK_sig(uc), puc);
240 #elif defined(_ARCH_PPC)
242 /***********************************************************************
243 * signal context platform-specific definitions
244 * From Wine
246 #ifdef linux
247 /* All Registers access - only for local access */
248 #define REG_sig(reg_name, context) \
249 ((context)->uc_mcontext.regs->reg_name)
250 /* Gpr Registers access */
251 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
252 /* Program counter */
253 #define IAR_sig(context) REG_sig(nip, context)
254 /* Machine State Register (Supervisor) */
255 #define MSR_sig(context) REG_sig(msr, context)
256 /* Count register */
257 #define CTR_sig(context) REG_sig(ctr, context)
258 /* User's integer exception register */
259 #define XER_sig(context) REG_sig(xer, context)
260 /* Link register */
261 #define LR_sig(context) REG_sig(link, context)
262 /* Condition register */
263 #define CR_sig(context) REG_sig(ccr, context)
265 /* Float Registers access */
266 #define FLOAT_sig(reg_num, context) \
267 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
268 #define FPSCR_sig(context) \
269 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
270 /* Exception Registers access */
271 #define DAR_sig(context) REG_sig(dar, context)
272 #define DSISR_sig(context) REG_sig(dsisr, context)
273 #define TRAP_sig(context) REG_sig(trap, context)
274 #endif /* linux */
276 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
277 #include <ucontext.h>
278 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
279 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
280 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
281 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
282 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
283 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
284 /* Exception Registers access */
285 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
286 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
287 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
288 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
290 #ifdef __APPLE__
291 #include <sys/ucontext.h>
292 typedef struct ucontext SIGCONTEXT;
293 /* All Registers access - only for local access */
294 #define REG_sig(reg_name, context) \
295 ((context)->uc_mcontext->ss.reg_name)
296 #define FLOATREG_sig(reg_name, context) \
297 ((context)->uc_mcontext->fs.reg_name)
298 #define EXCEPREG_sig(reg_name, context) \
299 ((context)->uc_mcontext->es.reg_name)
300 #define VECREG_sig(reg_name, context) \
301 ((context)->uc_mcontext->vs.reg_name)
302 /* Gpr Registers access */
303 #define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
304 /* Program counter */
305 #define IAR_sig(context) REG_sig(srr0, context)
306 /* Machine State Register (Supervisor) */
307 #define MSR_sig(context) REG_sig(srr1, context)
308 #define CTR_sig(context) REG_sig(ctr, context)
309 /* Link register */
310 #define XER_sig(context) REG_sig(xer, context)
311 /* User's integer exception register */
312 #define LR_sig(context) REG_sig(lr, context)
313 /* Condition register */
314 #define CR_sig(context) REG_sig(cr, context)
315 /* Float Registers access */
316 #define FLOAT_sig(reg_num, context) \
317 FLOATREG_sig(fpregs[reg_num], context)
318 #define FPSCR_sig(context) \
319 ((double)FLOATREG_sig(fpscr, context))
320 /* Exception Registers access */
321 /* Fault registers for coredump */
322 #define DAR_sig(context) EXCEPREG_sig(dar, context)
323 #define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
324 /* number of powerpc exception taken */
325 #define TRAP_sig(context) EXCEPREG_sig(exception, context)
326 #endif /* __APPLE__ */
328 int cpu_signal_handler(int host_signum, void *pinfo,
329 void *puc)
331 siginfo_t *info = pinfo;
332 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
333 ucontext_t *uc = puc;
334 #else
335 struct ucontext *uc = puc;
336 #endif
337 unsigned long pc;
338 int is_write;
340 pc = IAR_sig(uc);
341 is_write = 0;
342 #if 0
343 /* ppc 4xx case */
344 if (DSISR_sig(uc) & 0x00800000) {
345 is_write = 1;
347 #else
348 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) {
349 is_write = 1;
351 #endif
352 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
353 is_write, &uc->uc_sigmask, puc);
356 #elif defined(__alpha__)
358 int cpu_signal_handler(int host_signum, void *pinfo,
359 void *puc)
361 siginfo_t *info = pinfo;
362 struct ucontext *uc = puc;
363 uint32_t *pc = uc->uc_mcontext.sc_pc;
364 uint32_t insn = *pc;
365 int is_write = 0;
367 /* XXX: need kernel patch to get write flag faster */
368 switch (insn >> 26) {
369 case 0x0d: /* stw */
370 case 0x0e: /* stb */
371 case 0x0f: /* stq_u */
372 case 0x24: /* stf */
373 case 0x25: /* stg */
374 case 0x26: /* sts */
375 case 0x27: /* stt */
376 case 0x2c: /* stl */
377 case 0x2d: /* stq */
378 case 0x2e: /* stl_c */
379 case 0x2f: /* stq_c */
380 is_write = 1;
383 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
384 is_write, &uc->uc_sigmask, puc);
386 #elif defined(__sparc__)
388 int cpu_signal_handler(int host_signum, void *pinfo,
389 void *puc)
391 siginfo_t *info = pinfo;
392 int is_write;
393 uint32_t insn;
394 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
395 uint32_t *regs = (uint32_t *)(info + 1);
396 void *sigmask = (regs + 20);
397 /* XXX: is there a standard glibc define ? */
398 unsigned long pc = regs[1];
399 #else
400 #ifdef __linux__
401 struct sigcontext *sc = puc;
402 unsigned long pc = sc->sigc_regs.tpc;
403 void *sigmask = (void *)sc->sigc_mask;
404 #elif defined(__OpenBSD__)
405 struct sigcontext *uc = puc;
406 unsigned long pc = uc->sc_pc;
407 void *sigmask = (void *)(long)uc->sc_mask;
408 #endif
409 #endif
411 /* XXX: need kernel patch to get write flag faster */
412 is_write = 0;
413 insn = *(uint32_t *)pc;
414 if ((insn >> 30) == 3) {
415 switch ((insn >> 19) & 0x3f) {
416 case 0x05: /* stb */
417 case 0x15: /* stba */
418 case 0x06: /* sth */
419 case 0x16: /* stha */
420 case 0x04: /* st */
421 case 0x14: /* sta */
422 case 0x07: /* std */
423 case 0x17: /* stda */
424 case 0x0e: /* stx */
425 case 0x1e: /* stxa */
426 case 0x24: /* stf */
427 case 0x34: /* stfa */
428 case 0x27: /* stdf */
429 case 0x37: /* stdfa */
430 case 0x26: /* stqf */
431 case 0x36: /* stqfa */
432 case 0x25: /* stfsr */
433 case 0x3c: /* casa */
434 case 0x3e: /* casxa */
435 is_write = 1;
436 break;
439 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
440 is_write, sigmask, NULL);
443 #elif defined(__arm__)
445 int cpu_signal_handler(int host_signum, void *pinfo,
446 void *puc)
448 siginfo_t *info = pinfo;
449 struct ucontext *uc = puc;
450 unsigned long pc;
451 int is_write;
453 #if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
454 pc = uc->uc_mcontext.gregs[R15];
455 #else
456 pc = uc->uc_mcontext.arm_pc;
457 #endif
459 /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
460 * later processor; on v5 we will always report this as a read).
462 is_write = extract32(uc->uc_mcontext.error_code, 11, 1);
463 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
464 is_write,
465 &uc->uc_sigmask, puc);
468 #elif defined(__aarch64__)
470 int cpu_signal_handler(int host_signum, void *pinfo,
471 void *puc)
473 siginfo_t *info = pinfo;
474 struct ucontext *uc = puc;
475 uint64_t pc;
476 int is_write = 0; /* XXX how to determine? */
478 pc = uc->uc_mcontext.pc;
479 return handle_cpu_signal(pc, (uint64_t)info->si_addr,
480 is_write, &uc->uc_sigmask, puc);
483 #elif defined(__mc68000)
485 int cpu_signal_handler(int host_signum, void *pinfo,
486 void *puc)
488 siginfo_t *info = pinfo;
489 struct ucontext *uc = puc;
490 unsigned long pc;
491 int is_write;
493 pc = uc->uc_mcontext.gregs[16];
494 /* XXX: compute is_write */
495 is_write = 0;
496 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
497 is_write,
498 &uc->uc_sigmask, puc);
501 #elif defined(__ia64)
503 #ifndef __ISR_VALID
504 /* This ought to be in <bits/siginfo.h>... */
505 # define __ISR_VALID 1
506 #endif
508 int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
510 siginfo_t *info = pinfo;
511 struct ucontext *uc = puc;
512 unsigned long ip;
513 int is_write = 0;
515 ip = uc->uc_mcontext.sc_ip;
516 switch (host_signum) {
517 case SIGILL:
518 case SIGFPE:
519 case SIGSEGV:
520 case SIGBUS:
521 case SIGTRAP:
522 if (info->si_code && (info->si_segvflags & __ISR_VALID)) {
523 /* ISR.W (write-access) is bit 33: */
524 is_write = (info->si_isr >> 33) & 1;
526 break;
528 default:
529 break;
531 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
532 is_write,
533 (sigset_t *)&uc->uc_sigmask, puc);
536 #elif defined(__s390__)
538 int cpu_signal_handler(int host_signum, void *pinfo,
539 void *puc)
541 siginfo_t *info = pinfo;
542 struct ucontext *uc = puc;
543 unsigned long pc;
544 uint16_t *pinsn;
545 int is_write = 0;
547 pc = uc->uc_mcontext.psw.addr;
549 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
550 of the normal 2 arguments. The 3rd argument contains the "int_code"
551 from the hardware which does in fact contain the is_write value.
552 The rt signal handler, as far as I can tell, does not give this value
553 at all. Not that we could get to it from here even if it were. */
554 /* ??? This is not even close to complete, since it ignores all
555 of the read-modify-write instructions. */
556 pinsn = (uint16_t *)pc;
557 switch (pinsn[0] >> 8) {
558 case 0x50: /* ST */
559 case 0x42: /* STC */
560 case 0x40: /* STH */
561 is_write = 1;
562 break;
563 case 0xc4: /* RIL format insns */
564 switch (pinsn[0] & 0xf) {
565 case 0xf: /* STRL */
566 case 0xb: /* STGRL */
567 case 0x7: /* STHRL */
568 is_write = 1;
570 break;
571 case 0xe3: /* RXY format insns */
572 switch (pinsn[2] & 0xff) {
573 case 0x50: /* STY */
574 case 0x24: /* STG */
575 case 0x72: /* STCY */
576 case 0x70: /* STHY */
577 case 0x8e: /* STPQ */
578 case 0x3f: /* STRVH */
579 case 0x3e: /* STRV */
580 case 0x2f: /* STRVG */
581 is_write = 1;
583 break;
585 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
586 is_write, &uc->uc_sigmask, puc);
589 #elif defined(__mips__)
591 int cpu_signal_handler(int host_signum, void *pinfo,
592 void *puc)
594 siginfo_t *info = pinfo;
595 struct ucontext *uc = puc;
596 greg_t pc = uc->uc_mcontext.pc;
597 int is_write;
599 /* XXX: compute is_write */
600 is_write = 0;
601 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
602 is_write, &uc->uc_sigmask, puc);
605 #elif defined(__hppa__)
607 int cpu_signal_handler(int host_signum, void *pinfo,
608 void *puc)
610 siginfo_t *info = pinfo;
611 struct ucontext *uc = puc;
612 unsigned long pc = uc->uc_mcontext.sc_iaoq[0];
613 uint32_t insn = *(uint32_t *)pc;
614 int is_write = 0;
616 /* XXX: need kernel patch to get write flag faster. */
617 switch (insn >> 26) {
618 case 0x1a: /* STW */
619 case 0x19: /* STH */
620 case 0x18: /* STB */
621 case 0x1b: /* STWM */
622 is_write = 1;
623 break;
625 case 0x09: /* CSTWX, FSTWX, FSTWS */
626 case 0x0b: /* CSTDX, FSTDX, FSTDS */
627 /* Distinguish from coprocessor load ... */
628 is_write = (insn >> 9) & 1;
629 break;
631 case 0x03:
632 switch ((insn >> 6) & 15) {
633 case 0xa: /* STWS */
634 case 0x9: /* STHS */
635 case 0x8: /* STBS */
636 case 0xe: /* STWAS */
637 case 0xc: /* STBYS */
638 is_write = 1;
640 break;
643 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
644 is_write, &uc->uc_sigmask, puc);
647 #else
649 #error host CPU specific signal handler needed
651 #endif