10 |
|
* tjw@omnigroup.com Sun, 4 Jun 2000 |
11 |
|
* www.omnigroup.com/mailman/archive/macosx-dev/2000-June/002030.html |
12 |
|
* |
13 |
< |
* Basilisk II (C) 1997-2002 Christian Bauer |
13 |
> |
* Basilisk II (C) 1997-2004 Christian Bauer |
14 |
|
* |
15 |
|
* This program is free software; you can redistribute it and/or modify |
16 |
|
* it under the terms of the GNU General Public License as published by |
36 |
|
#endif |
37 |
|
|
38 |
|
#include <list> |
39 |
+ |
#include <stdio.h> |
40 |
|
#include <signal.h> |
41 |
|
#include "sigsegv.h" |
42 |
|
|
70 |
|
enum transfer_size_t { |
71 |
|
SIZE_UNKNOWN, |
72 |
|
SIZE_BYTE, |
73 |
< |
SIZE_WORD, |
74 |
< |
SIZE_LONG |
73 |
> |
SIZE_WORD, // 2 bytes |
74 |
> |
SIZE_LONG, // 4 bytes |
75 |
> |
SIZE_QUAD, // 8 bytes |
76 |
|
}; |
77 |
|
|
78 |
|
// Transfer type |
214 |
|
|
215 |
|
#if HAVE_SIGINFO_T |
216 |
|
// Generic extended signal handler |
215 |
– |
#define SIGSEGV_FAULT_HANDLER sigsegv_fault_handler |
217 |
|
#if defined(__NetBSD__) || defined(__FreeBSD__) |
218 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
219 |
|
#else |
220 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
221 |
|
#endif |
222 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp |
223 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, sip, scp |
223 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGLIST_1 siginfo_t *sip, void *scp |
224 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sip, scp |
225 |
|
#define SIGSEGV_FAULT_ADDRESS sip->si_addr |
226 |
< |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
226 |
> |
#if (defined(sgi) || defined(__sgi)) |
227 |
> |
#include <ucontext.h> |
228 |
> |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
229 |
> |
#define SIGSEGV_FAULT_INSTRUCTION (unsigned long)SIGSEGV_CONTEXT_REGS[CTX_EPC] |
230 |
> |
#if (defined(mips) || defined(__mips)) |
231 |
> |
#define SIGSEGV_REGISTER_FILE SIGSEGV_CONTEXT_REGS |
232 |
> |
#define SIGSEGV_SKIP_INSTRUCTION mips_skip_instruction |
233 |
> |
#endif |
234 |
> |
#endif |
235 |
> |
#if defined(__sun__) |
236 |
> |
#if (defined(sparc) || defined(__sparc__)) |
237 |
> |
#include <sys/stack.h> |
238 |
> |
#include <sys/regset.h> |
239 |
> |
#include <sys/ucontext.h> |
240 |
> |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
241 |
> |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[REG_PC] |
242 |
> |
#define SIGSEGV_SPARC_GWINDOWS (((ucontext_t *)scp)->uc_mcontext.gwins) |
243 |
> |
#define SIGSEGV_SPARC_RWINDOW (struct rwindow *)((char *)SIGSEGV_CONTEXT_REGS[REG_SP] + STACK_BIAS) |
244 |
> |
#define SIGSEGV_REGISTER_FILE ((unsigned long *)SIGSEGV_CONTEXT_REGS), SIGSEGV_SPARC_GWINDOWS, SIGSEGV_SPARC_RWINDOW |
245 |
> |
#define SIGSEGV_SKIP_INSTRUCTION sparc_skip_instruction |
246 |
> |
#endif |
247 |
> |
#endif |
248 |
> |
#if defined(__FreeBSD__) |
249 |
|
#if (defined(i386) || defined(__i386__)) |
250 |
|
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_eip) |
251 |
< |
#define SIGSEGV_REGISTER_FILE ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */ |
251 |
> |
#define SIGSEGV_REGISTER_FILE ((unsigned long *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */ |
252 |
|
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
253 |
|
#endif |
254 |
|
#endif |
257 |
|
#include <sys/ucontext.h> |
258 |
|
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
259 |
|
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[14] /* should use REG_EIP instead */ |
260 |
< |
#define SIGSEGV_REGISTER_FILE (unsigned int *)SIGSEGV_CONTEXT_REGS |
260 |
> |
#define SIGSEGV_REGISTER_FILE (unsigned long *)SIGSEGV_CONTEXT_REGS |
261 |
|
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
262 |
|
#endif |
263 |
|
#if (defined(x86_64) || defined(__x86_64__)) |
265 |
|
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
266 |
|
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[16] /* should use REG_RIP instead */ |
267 |
|
#define SIGSEGV_REGISTER_FILE (unsigned long *)SIGSEGV_CONTEXT_REGS |
268 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
269 |
|
#endif |
270 |
|
#if (defined(ia64) || defined(__ia64__)) |
271 |
|
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */ |
277 |
|
#define SIGSEGV_REGISTER_FILE (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr) |
278 |
|
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
279 |
|
#endif |
280 |
+ |
#if (defined(hppa) || defined(__hppa__)) |
281 |
+ |
#undef SIGSEGV_FAULT_ADDRESS |
282 |
+ |
#define SIGSEGV_FAULT_ADDRESS sip->si_ptr |
283 |
+ |
#endif |
284 |
|
#endif |
285 |
|
#endif |
286 |
|
|
287 |
|
#if HAVE_SIGCONTEXT_SUBTERFUGE |
259 |
– |
#define SIGSEGV_FAULT_HANDLER sigsegv_fault_handler |
288 |
|
// Linux kernels prior to 2.4 ? |
289 |
|
#if defined(__linux__) |
290 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
291 |
|
#if (defined(i386) || defined(__i386__)) |
292 |
|
#include <asm/sigcontext.h> |
293 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs |
294 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, scs |
295 |
< |
#define SIGSEGV_FAULT_ADDRESS scs.cr2 |
296 |
< |
#define SIGSEGV_FAULT_INSTRUCTION scs.eip |
297 |
< |
#define SIGSEGV_REGISTER_FILE (unsigned int *)(&scs) |
294 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGLIST_1 struct sigcontext *scp |
295 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS &scs |
296 |
> |
#define SIGSEGV_FAULT_ADDRESS scp->cr2 |
297 |
> |
#define SIGSEGV_FAULT_INSTRUCTION scp->eip |
298 |
> |
#define SIGSEGV_REGISTER_FILE (unsigned long *)scp |
299 |
|
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
300 |
|
#endif |
301 |
|
#if (defined(sparc) || defined(__sparc__)) |
302 |
|
#include <asm/sigcontext.h> |
303 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr |
304 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr |
304 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr |
305 |
|
#define SIGSEGV_FAULT_ADDRESS addr |
306 |
|
#endif |
307 |
|
#if (defined(powerpc) || defined(__powerpc__)) |
308 |
|
#include <asm/sigcontext.h> |
309 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp |
310 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, scp |
310 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, scp |
311 |
|
#define SIGSEGV_FAULT_ADDRESS scp->regs->dar |
312 |
|
#define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip |
313 |
|
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr) |
316 |
|
#if (defined(alpha) || defined(__alpha__)) |
317 |
|
#include <asm/sigcontext.h> |
318 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
319 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
319 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
320 |
|
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
321 |
|
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_pc |
293 |
– |
|
294 |
– |
// From Boehm's GC 6.0alpha8 |
295 |
– |
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
296 |
– |
{ |
297 |
– |
unsigned int instruction = *((unsigned int *)(scp->sc_pc)); |
298 |
– |
unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f]; |
299 |
– |
fault_address += (signed long)(signed short)(instruction & 0xffff); |
300 |
– |
return (sigsegv_address_t)fault_address; |
301 |
– |
} |
322 |
|
#endif |
323 |
|
#endif |
324 |
|
|
325 |
|
// Irix 5 or 6 on MIPS |
326 |
< |
#if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4)) |
326 |
> |
#if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(_SYSTYPE_SVR4)) |
327 |
|
#include <ucontext.h> |
328 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
329 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
330 |
< |
#define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr |
329 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
330 |
> |
#define SIGSEGV_FAULT_ADDRESS (unsigned long)scp->sc_badvaddr |
331 |
> |
#define SIGSEGV_FAULT_INSTRUCTION (unsigned long)scp->sc_pc |
332 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
333 |
|
#endif |
334 |
|
|
335 |
|
// HP-UX |
336 |
|
#if (defined(hpux) || defined(__hpux__)) |
337 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
338 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
338 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
339 |
|
#define SIGSEGV_FAULT_ADDRESS scp->sc_sl.sl_ss.ss_narrow.ss_cr21 |
340 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS) |
341 |
|
#endif |
344 |
|
#if defined(__osf__) |
345 |
|
#include <ucontext.h> |
346 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
347 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
347 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
348 |
|
#define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0 |
349 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
350 |
|
#endif |
352 |
|
// AIX |
353 |
|
#if defined(_AIX) |
354 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
355 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
355 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
356 |
|
#define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr |
357 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
358 |
|
#endif |
359 |
|
|
360 |
< |
// NetBSD or FreeBSD |
361 |
< |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
360 |
> |
// NetBSD |
361 |
> |
#if defined(__NetBSD__) |
362 |
|
#if (defined(m68k) || defined(__m68k__)) |
363 |
|
#include <m68k/frame.h> |
364 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
365 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
365 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
366 |
|
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
367 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
368 |
|
|
386 |
|
} |
387 |
|
return (sigsegv_address_t)fault_addr; |
388 |
|
} |
389 |
< |
#else |
390 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, void *scp, char *addr |
391 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr |
392 |
< |
#define SIGSEGV_FAULT_ADDRESS addr |
389 |
> |
#endif |
390 |
> |
#if (defined(alpha) || defined(__alpha__)) |
391 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
392 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
393 |
> |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
394 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
395 |
|
#endif |
396 |
+ |
#if (defined(i386) || defined(__i386__)) |
397 |
+ |
#error "FIXME: need to decode instruction and compute EA" |
398 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
399 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
400 |
+ |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
401 |
+ |
#endif |
402 |
+ |
#endif |
403 |
+ |
#if defined(__FreeBSD__) |
404 |
+ |
#if (defined(i386) || defined(__i386__)) |
405 |
+ |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
406 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr |
407 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr |
408 |
+ |
#define SIGSEGV_FAULT_ADDRESS addr |
409 |
+ |
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_eip |
410 |
+ |
#define SIGSEGV_REGISTER_FILE ((unsigned long *)&scp->sc_edi) |
411 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
412 |
+ |
#endif |
413 |
+ |
#if (defined(alpha) || defined(__alpha__)) |
414 |
+ |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
415 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, char *addr, struct sigcontext *scp |
416 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, addr, scp |
417 |
+ |
#define SIGSEGV_FAULT_ADDRESS addr |
418 |
+ |
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_pc |
419 |
+ |
#endif |
420 |
|
#endif |
421 |
|
|
422 |
+ |
// Extract fault address out of a sigcontext |
423 |
+ |
#if (defined(alpha) || defined(__alpha__)) |
424 |
+ |
// From Boehm's GC 6.0alpha8 |
425 |
+ |
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
426 |
+ |
{ |
427 |
+ |
unsigned int instruction = *((unsigned int *)(scp->sc_pc)); |
428 |
+ |
unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f]; |
429 |
+ |
fault_address += (signed long)(signed short)(instruction & 0xffff); |
430 |
+ |
return (sigsegv_address_t)fault_address; |
431 |
+ |
} |
432 |
+ |
#endif |
433 |
+ |
|
434 |
+ |
|
435 |
|
// MacOS X, not sure which version this works in. Under 10.1 |
436 |
|
// vm_protect does not appear to work from a signal handler. Under |
437 |
|
// 10.2 signal handlers get siginfo type arguments but the si_addr |
527 |
|
|
528 |
|
#define SIGSEGV_FAULT_ADDRESS code[1] |
529 |
|
#define SIGSEGV_FAULT_INSTRUCTION get_fault_instruction(thread, state) |
530 |
< |
#define SIGSEGV_FAULT_HANDLER (code[0] == KERN_PROTECTION_FAILURE) && sigsegv_fault_handler |
530 |
> |
#define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP) ((code[0] == KERN_PROTECTION_FAILURE) ? sigsegv_fault_handler(ADDR, IP) : SIGSEGV_RETURN_FAILURE) |
531 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST mach_port_t thread, exception_data_t code, ppc_thread_state_t *state |
532 |
|
#define SIGSEGV_FAULT_HANDLER_ARGS thread, code, &state |
533 |
|
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
607 |
|
|
608 |
|
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
609 |
|
// Decode and skip X86 instruction |
610 |
< |
#if (defined(i386) || defined(__i386__)) |
610 |
> |
#if (defined(i386) || defined(__i386__)) || defined(__x86_64__) |
611 |
|
#if defined(__linux__) |
612 |
|
enum { |
613 |
+ |
#if (defined(i386) || defined(__i386__)) |
614 |
|
X86_REG_EIP = 14, |
615 |
|
X86_REG_EAX = 11, |
616 |
|
X86_REG_ECX = 10, |
620 |
|
X86_REG_EBP = 6, |
621 |
|
X86_REG_ESI = 5, |
622 |
|
X86_REG_EDI = 4 |
623 |
+ |
#endif |
624 |
+ |
#if defined(__x86_64__) |
625 |
+ |
X86_REG_R8 = 0, |
626 |
+ |
X86_REG_R9 = 1, |
627 |
+ |
X86_REG_R10 = 2, |
628 |
+ |
X86_REG_R11 = 3, |
629 |
+ |
X86_REG_R12 = 4, |
630 |
+ |
X86_REG_R13 = 5, |
631 |
+ |
X86_REG_R14 = 6, |
632 |
+ |
X86_REG_R15 = 7, |
633 |
+ |
X86_REG_EDI = 8, |
634 |
+ |
X86_REG_ESI = 9, |
635 |
+ |
X86_REG_EBP = 10, |
636 |
+ |
X86_REG_EBX = 11, |
637 |
+ |
X86_REG_EDX = 12, |
638 |
+ |
X86_REG_EAX = 13, |
639 |
+ |
X86_REG_ECX = 14, |
640 |
+ |
X86_REG_ESP = 15, |
641 |
+ |
X86_REG_EIP = 16 |
642 |
+ |
#endif |
643 |
|
}; |
644 |
|
#endif |
645 |
|
#if defined(__NetBSD__) || defined(__FreeBSD__) |
646 |
|
enum { |
647 |
+ |
#if (defined(i386) || defined(__i386__)) |
648 |
|
X86_REG_EIP = 10, |
649 |
|
X86_REG_EAX = 7, |
650 |
|
X86_REG_ECX = 6, |
654 |
|
X86_REG_EBP = 2, |
655 |
|
X86_REG_ESI = 1, |
656 |
|
X86_REG_EDI = 0 |
657 |
+ |
#endif |
658 |
|
}; |
659 |
|
#endif |
660 |
|
// FIXME: this is partly redundant with the instruction decoding phase |
691 |
|
return offset; |
692 |
|
} |
693 |
|
|
694 |
< |
static bool ix86_skip_instruction(unsigned int * regs) |
694 |
> |
static bool ix86_skip_instruction(unsigned long * regs) |
695 |
|
{ |
696 |
|
unsigned char * eip = (unsigned char *)regs[X86_REG_EIP]; |
697 |
|
|
703 |
|
|
704 |
|
int reg = -1; |
705 |
|
int len = 0; |
706 |
< |
|
706 |
> |
|
707 |
> |
#if DEBUG |
708 |
> |
printf("IP: %p [%02x %02x %02x %02x...]\n", |
709 |
> |
eip, eip[0], eip[1], eip[2], eip[3]); |
710 |
> |
#endif |
711 |
> |
|
712 |
|
// Operand size prefix |
713 |
|
if (*eip == 0x66) { |
714 |
|
eip++; |
716 |
|
transfer_size = SIZE_WORD; |
717 |
|
} |
718 |
|
|
719 |
+ |
// REX prefix |
720 |
+ |
#if defined(__x86_64__) |
721 |
+ |
struct rex_t { |
722 |
+ |
unsigned char W; |
723 |
+ |
unsigned char R; |
724 |
+ |
unsigned char X; |
725 |
+ |
unsigned char B; |
726 |
+ |
}; |
727 |
+ |
rex_t rex = { 0, 0, 0, 0 }; |
728 |
+ |
bool has_rex = false; |
729 |
+ |
if ((*eip & 0xf0) == 0x40) { |
730 |
+ |
has_rex = true; |
731 |
+ |
const unsigned char b = *eip; |
732 |
+ |
rex.W = b & (1 << 3); |
733 |
+ |
rex.R = b & (1 << 2); |
734 |
+ |
rex.X = b & (1 << 1); |
735 |
+ |
rex.B = b & (1 << 0); |
736 |
+ |
#if DEBUG |
737 |
+ |
printf("REX: %c,%c,%c,%c\n", |
738 |
+ |
rex.W ? 'W' : '_', |
739 |
+ |
rex.R ? 'R' : '_', |
740 |
+ |
rex.X ? 'X' : '_', |
741 |
+ |
rex.B ? 'B' : '_'); |
742 |
+ |
#endif |
743 |
+ |
eip++; |
744 |
+ |
len++; |
745 |
+ |
if (rex.W) |
746 |
+ |
transfer_size = SIZE_QUAD; |
747 |
+ |
} |
748 |
+ |
#else |
749 |
+ |
const bool has_rex = false; |
750 |
+ |
#endif |
751 |
+ |
|
752 |
|
// Decode instruction |
753 |
|
switch (eip[0]) { |
754 |
|
case 0x0f: |
818 |
|
return false; |
819 |
|
} |
820 |
|
|
821 |
+ |
#if defined(__x86_64__) |
822 |
+ |
if (rex.R) |
823 |
+ |
reg += 8; |
824 |
+ |
#endif |
825 |
+ |
|
826 |
|
if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != -1) { |
827 |
< |
static const int x86_reg_map[8] = { |
827 |
> |
static const int x86_reg_map[] = { |
828 |
|
X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX, |
829 |
< |
X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI |
829 |
> |
X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI, |
830 |
> |
#if defined(__x86_64__) |
831 |
> |
X86_REG_R8, X86_REG_R9, X86_REG_R10, X86_REG_R11, |
832 |
> |
X86_REG_R12, X86_REG_R13, X86_REG_R14, X86_REG_R15, |
833 |
> |
#endif |
834 |
|
}; |
835 |
|
|
836 |
< |
if (reg < 0 || reg >= 8) |
836 |
> |
if (reg < 0 || reg >= (sizeof(x86_reg_map)/sizeof(x86_reg_map[0]) - 1)) |
837 |
|
return false; |
838 |
|
|
839 |
+ |
// Set 0 to the relevant register part |
840 |
+ |
// NOTE: this is only valid for MOV alike instructions |
841 |
|
int rloc = x86_reg_map[reg]; |
842 |
|
switch (transfer_size) { |
843 |
|
case SIZE_BYTE: |
844 |
< |
regs[rloc] = (regs[rloc] & ~0xff); |
844 |
> |
if (has_rex || reg < 4) |
845 |
> |
regs[rloc] = (regs[rloc] & ~0x00ffL); |
846 |
> |
else { |
847 |
> |
rloc = x86_reg_map[reg - 4]; |
848 |
> |
regs[rloc] = (regs[rloc] & ~0xff00L); |
849 |
> |
} |
850 |
|
break; |
851 |
|
case SIZE_WORD: |
852 |
< |
regs[rloc] = (regs[rloc] & ~0xffff); |
852 |
> |
regs[rloc] = (regs[rloc] & ~0xffffL); |
853 |
|
break; |
854 |
|
case SIZE_LONG: |
855 |
+ |
case SIZE_QUAD: // zero-extension |
856 |
|
regs[rloc] = 0; |
857 |
|
break; |
858 |
|
} |
860 |
|
|
861 |
|
#if DEBUG |
862 |
|
printf("%08x: %s %s access", regs[X86_REG_EIP], |
863 |
< |
transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long", |
863 |
> |
transfer_size == SIZE_BYTE ? "byte" : |
864 |
> |
transfer_size == SIZE_WORD ? "word" : |
865 |
> |
transfer_size == SIZE_LONG ? "long" : |
866 |
> |
transfer_size == SIZE_QUAD ? "quad" : "unknown", |
867 |
|
transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write"); |
868 |
|
|
869 |
|
if (reg != -1) { |
870 |
< |
static const char * x86_reg_str_map[8] = { |
871 |
< |
"eax", "ecx", "edx", "ebx", |
872 |
< |
"esp", "ebp", "esi", "edi" |
870 |
> |
static const char * x86_byte_reg_str_map[] = { |
871 |
> |
"al", "cl", "dl", "bl", |
872 |
> |
"spl", "bpl", "sil", "dil", |
873 |
> |
"r8b", "r9b", "r10b", "r11b", |
874 |
> |
"r12b", "r13b", "r14b", "r15b", |
875 |
> |
"ah", "ch", "dh", "bh", |
876 |
> |
}; |
877 |
> |
static const char * x86_word_reg_str_map[] = { |
878 |
> |
"ax", "cx", "dx", "bx", |
879 |
> |
"sp", "bp", "si", "di", |
880 |
> |
"r8w", "r9w", "r10w", "r11w", |
881 |
> |
"r12w", "r13w", "r14w", "r15w", |
882 |
> |
}; |
883 |
> |
static const char *x86_long_reg_str_map[] = { |
884 |
> |
"eax", "ecx", "edx", "ebx", |
885 |
> |
"esp", "ebp", "esi", "edi", |
886 |
> |
"r8d", "r9d", "r10d", "r11d", |
887 |
> |
"r12d", "r13d", "r14d", "r15d", |
888 |
|
}; |
889 |
< |
printf(" %s register %%%s", transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from", x86_reg_str_map[reg]); |
889 |
> |
static const char *x86_quad_reg_str_map[] = { |
890 |
> |
"rax", "rcx", "rdx", "rbx", |
891 |
> |
"rsp", "rbp", "rsi", "rdi", |
892 |
> |
"r8", "r9", "r10", "r11", |
893 |
> |
"r12", "r13", "r14", "r15", |
894 |
> |
}; |
895 |
> |
const char * reg_str = NULL; |
896 |
> |
switch (transfer_size) { |
897 |
> |
case SIZE_BYTE: |
898 |
> |
reg_str = x86_byte_reg_str_map[(!has_rex && reg >= 4 ? 12 : 0) + reg]; |
899 |
> |
break; |
900 |
> |
case SIZE_WORD: reg_str = x86_word_reg_str_map[reg]; break; |
901 |
> |
case SIZE_LONG: reg_str = x86_long_reg_str_map[reg]; break; |
902 |
> |
case SIZE_QUAD: reg_str = x86_quad_reg_str_map[reg]; break; |
903 |
> |
} |
904 |
> |
if (reg_str) |
905 |
> |
printf(" %s register %%%s", |
906 |
> |
transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from", |
907 |
> |
reg_str); |
908 |
|
} |
909 |
|
printf(", %d bytes instruction\n", len); |
910 |
|
#endif |
946 |
|
return true; |
947 |
|
} |
948 |
|
#endif |
949 |
+ |
|
950 |
+ |
// Decode and skip MIPS instruction |
951 |
+ |
#if (defined(mips) || defined(__mips)) |
952 |
+ |
enum { |
953 |
+ |
#if (defined(sgi) || defined(__sgi)) |
954 |
+ |
MIPS_REG_EPC = 35, |
955 |
+ |
#endif |
956 |
+ |
}; |
957 |
+ |
static bool mips_skip_instruction(greg_t * regs) |
958 |
+ |
{ |
959 |
+ |
unsigned int * epc = (unsigned int *)(unsigned long)regs[MIPS_REG_EPC]; |
960 |
+ |
|
961 |
+ |
if (epc == 0) |
962 |
+ |
return false; |
963 |
+ |
|
964 |
+ |
#if DEBUG |
965 |
+ |
printf("IP: %p [%08x]\n", epc, epc[0]); |
966 |
+ |
#endif |
967 |
+ |
|
968 |
+ |
transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN; |
969 |
+ |
transfer_size_t transfer_size = SIZE_LONG; |
970 |
+ |
int direction = 0; |
971 |
+ |
|
972 |
+ |
const unsigned int opcode = epc[0]; |
973 |
+ |
switch (opcode >> 26) { |
974 |
+ |
case 32: // Load Byte |
975 |
+ |
case 36: // Load Byte Unsigned |
976 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
977 |
+ |
transfer_size = SIZE_BYTE; |
978 |
+ |
break; |
979 |
+ |
case 33: // Load Halfword |
980 |
+ |
case 37: // Load Halfword Unsigned |
981 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
982 |
+ |
transfer_size = SIZE_WORD; |
983 |
+ |
break; |
984 |
+ |
case 35: // Load Word |
985 |
+ |
case 39: // Load Word Unsigned |
986 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
987 |
+ |
transfer_size = SIZE_LONG; |
988 |
+ |
break; |
989 |
+ |
case 34: // Load Word Left |
990 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
991 |
+ |
transfer_size = SIZE_LONG; |
992 |
+ |
direction = -1; |
993 |
+ |
break; |
994 |
+ |
case 38: // Load Word Right |
995 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
996 |
+ |
transfer_size = SIZE_LONG; |
997 |
+ |
direction = 1; |
998 |
+ |
break; |
999 |
+ |
case 55: // Load Doubleword |
1000 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1001 |
+ |
transfer_size = SIZE_QUAD; |
1002 |
+ |
break; |
1003 |
+ |
case 26: // Load Doubleword Left |
1004 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1005 |
+ |
transfer_size = SIZE_QUAD; |
1006 |
+ |
direction = -1; |
1007 |
+ |
break; |
1008 |
+ |
case 27: // Load Doubleword Right |
1009 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1010 |
+ |
transfer_size = SIZE_QUAD; |
1011 |
+ |
direction = 1; |
1012 |
+ |
break; |
1013 |
+ |
case 40: // Store Byte |
1014 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1015 |
+ |
transfer_size = SIZE_BYTE; |
1016 |
+ |
break; |
1017 |
+ |
case 41: // Store Halfword |
1018 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1019 |
+ |
transfer_size = SIZE_WORD; |
1020 |
+ |
break; |
1021 |
+ |
case 43: // Store Word |
1022 |
+ |
case 42: // Store Word Left |
1023 |
+ |
case 46: // Store Word Right |
1024 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1025 |
+ |
transfer_size = SIZE_LONG; |
1026 |
+ |
break; |
1027 |
+ |
case 63: // Store Doubleword |
1028 |
+ |
case 44: // Store Doubleword Left |
1029 |
+ |
case 45: // Store Doubleword Right |
1030 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1031 |
+ |
transfer_size = SIZE_QUAD; |
1032 |
+ |
break; |
1033 |
+ |
/* Misc instructions unlikely to be used within CPU emulators */ |
1034 |
+ |
case 48: // Load Linked Word |
1035 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1036 |
+ |
transfer_size = SIZE_LONG; |
1037 |
+ |
break; |
1038 |
+ |
case 52: // Load Linked Doubleword |
1039 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1040 |
+ |
transfer_size = SIZE_QUAD; |
1041 |
+ |
break; |
1042 |
+ |
case 56: // Store Conditional Word |
1043 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1044 |
+ |
transfer_size = SIZE_LONG; |
1045 |
+ |
break; |
1046 |
+ |
case 60: // Store Conditional Doubleword |
1047 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1048 |
+ |
transfer_size = SIZE_QUAD; |
1049 |
+ |
break; |
1050 |
+ |
} |
1051 |
+ |
|
1052 |
+ |
if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) { |
1053 |
+ |
// Unknown machine code, let it crash. Then patch the decoder |
1054 |
+ |
return false; |
1055 |
+ |
} |
1056 |
+ |
|
1057 |
+ |
// Zero target register in case of a load operation |
1058 |
+ |
const int reg = (opcode >> 16) & 0x1f; |
1059 |
+ |
if (transfer_type == SIGSEGV_TRANSFER_LOAD) { |
1060 |
+ |
if (direction == 0) |
1061 |
+ |
regs[reg] = 0; |
1062 |
+ |
else { |
1063 |
+ |
// FIXME: untested code |
1064 |
+ |
unsigned long ea = regs[(opcode >> 21) & 0x1f]; |
1065 |
+ |
ea += (signed long)(signed int)(signed short)(opcode & 0xffff); |
1066 |
+ |
const int offset = ea & (transfer_size == SIZE_LONG ? 3 : 7); |
1067 |
+ |
unsigned long value; |
1068 |
+ |
if (direction > 0) { |
1069 |
+ |
const unsigned long rmask = ~((1L << ((offset + 1) * 8)) - 1); |
1070 |
+ |
value = regs[reg] & rmask; |
1071 |
+ |
} |
1072 |
+ |
else { |
1073 |
+ |
const unsigned long lmask = (1L << (offset * 8)) - 1; |
1074 |
+ |
value = regs[reg] & lmask; |
1075 |
+ |
} |
1076 |
+ |
// restore most significant bits |
1077 |
+ |
if (transfer_size == SIZE_LONG) |
1078 |
+ |
value = (signed long)(signed int)value; |
1079 |
+ |
regs[reg] = value; |
1080 |
+ |
} |
1081 |
+ |
} |
1082 |
+ |
|
1083 |
+ |
#if DEBUG |
1084 |
+ |
#if (defined(_ABIN32) || defined(_ABI64)) |
1085 |
+ |
static const char * mips_gpr_names[32] = { |
1086 |
+ |
"zero", "at", "v0", "v1", "a0", "a1", "a2", "a3", |
1087 |
+ |
"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", |
1088 |
+ |
"s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", |
1089 |
+ |
"t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra" |
1090 |
+ |
}; |
1091 |
+ |
#else |
1092 |
+ |
static const char * mips_gpr_names[32] = { |
1093 |
+ |
"zero", "at", "v0", "v1", "a0", "a1", "a2", "a3", |
1094 |
+ |
"a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3", |
1095 |
+ |
"s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", |
1096 |
+ |
"t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra" |
1097 |
+ |
}; |
1098 |
+ |
#endif |
1099 |
+ |
printf("%s %s register %s\n", |
1100 |
+ |
transfer_size == SIZE_BYTE ? "byte" : |
1101 |
+ |
transfer_size == SIZE_WORD ? "word" : |
1102 |
+ |
transfer_size == SIZE_LONG ? "long" : |
1103 |
+ |
transfer_size == SIZE_QUAD ? "quad" : "unknown", |
1104 |
+ |
transfer_type == SIGSEGV_TRANSFER_LOAD ? "load to" : "store from", |
1105 |
+ |
mips_gpr_names[reg]); |
1106 |
+ |
#endif |
1107 |
+ |
|
1108 |
+ |
regs[MIPS_REG_EPC] += 4; |
1109 |
+ |
return true; |
1110 |
+ |
} |
1111 |
+ |
#endif |
1112 |
+ |
|
1113 |
+ |
// Decode and skip SPARC instruction |
1114 |
+ |
#if (defined(sparc) || defined(__sparc__)) |
1115 |
+ |
enum { |
1116 |
+ |
#if (defined(__sun__)) |
1117 |
+ |
SPARC_REG_G1 = REG_G1, |
1118 |
+ |
SPARC_REG_O0 = REG_O0, |
1119 |
+ |
SPARC_REG_PC = REG_PC, |
1120 |
+ |
#endif |
1121 |
+ |
}; |
1122 |
+ |
static bool sparc_skip_instruction(unsigned long * regs, gwindows_t * gwins, struct rwindow * rwin) |
1123 |
+ |
{ |
1124 |
+ |
unsigned int * pc = (unsigned int *)regs[SPARC_REG_PC]; |
1125 |
+ |
|
1126 |
+ |
if (pc == 0) |
1127 |
+ |
return false; |
1128 |
+ |
|
1129 |
+ |
#if DEBUG |
1130 |
+ |
printf("IP: %p [%08x]\n", pc, pc[0]); |
1131 |
+ |
#endif |
1132 |
+ |
|
1133 |
+ |
transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN; |
1134 |
+ |
transfer_size_t transfer_size = SIZE_LONG; |
1135 |
+ |
bool register_pair = false; |
1136 |
+ |
|
1137 |
+ |
const unsigned int opcode = pc[0]; |
1138 |
+ |
if ((opcode >> 30) != 3) |
1139 |
+ |
return false; |
1140 |
+ |
switch ((opcode >> 19) & 0x3f) { |
1141 |
+ |
case 9: // Load Signed Byte |
1142 |
+ |
case 1: // Load Unsigned Byte |
1143 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1144 |
+ |
transfer_size = SIZE_BYTE; |
1145 |
+ |
break; |
1146 |
+ |
case 10:// Load Signed Halfword |
1147 |
+ |
case 2: // Load Unsigned Word |
1148 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1149 |
+ |
transfer_size = SIZE_WORD; |
1150 |
+ |
break; |
1151 |
+ |
case 8: // Load Word |
1152 |
+ |
case 0: // Load Unsigned Word |
1153 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1154 |
+ |
transfer_size = SIZE_LONG; |
1155 |
+ |
break; |
1156 |
+ |
case 11:// Load Extended Word |
1157 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1158 |
+ |
transfer_size = SIZE_QUAD; |
1159 |
+ |
break; |
1160 |
+ |
case 3: // Load Doubleword |
1161 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1162 |
+ |
transfer_size = SIZE_LONG; |
1163 |
+ |
register_pair = true; |
1164 |
+ |
break; |
1165 |
+ |
case 5: // Store Byte |
1166 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1167 |
+ |
transfer_size = SIZE_BYTE; |
1168 |
+ |
break; |
1169 |
+ |
case 6: // Store Halfword |
1170 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1171 |
+ |
transfer_size = SIZE_WORD; |
1172 |
+ |
break; |
1173 |
+ |
case 4: // Store Word |
1174 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1175 |
+ |
transfer_size = SIZE_LONG; |
1176 |
+ |
break; |
1177 |
+ |
case 14:// Store Extended Word |
1178 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1179 |
+ |
transfer_size = SIZE_QUAD; |
1180 |
+ |
break; |
1181 |
+ |
case 7: // Store Doubleword |
1182 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1183 |
+ |
transfer_size = SIZE_WORD; |
1184 |
+ |
register_pair = true; |
1185 |
+ |
break; |
1186 |
+ |
} |
1187 |
+ |
|
1188 |
+ |
if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) { |
1189 |
+ |
// Unknown machine code, let it crash. Then patch the decoder |
1190 |
+ |
return false; |
1191 |
+ |
} |
1192 |
+ |
|
1193 |
+ |
// Zero target register in case of a load operation |
1194 |
+ |
const int reg = (opcode >> 25) & 0x1f; |
1195 |
+ |
if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != 0) { |
1196 |
+ |
// FIXME: code to handle local & input registers is not tested |
1197 |
+ |
if (reg >= 1 && reg <= 7) { |
1198 |
+ |
// global registers |
1199 |
+ |
regs[reg - 1 + SPARC_REG_G1] = 0; |
1200 |
+ |
} |
1201 |
+ |
else if (reg >= 8 && reg <= 15) { |
1202 |
+ |
// output registers |
1203 |
+ |
regs[reg - 8 + SPARC_REG_O0] = 0; |
1204 |
+ |
} |
1205 |
+ |
else if (reg >= 16 && reg <= 23) { |
1206 |
+ |
// local registers (in register windows) |
1207 |
+ |
if (gwins) |
1208 |
+ |
gwins->wbuf->rw_local[reg - 16] = 0; |
1209 |
+ |
else |
1210 |
+ |
rwin->rw_local[reg - 16] = 0; |
1211 |
+ |
} |
1212 |
+ |
else { |
1213 |
+ |
// input registers (in register windows) |
1214 |
+ |
if (gwins) |
1215 |
+ |
gwins->wbuf->rw_in[reg - 24] = 0; |
1216 |
+ |
else |
1217 |
+ |
rwin->rw_in[reg - 24] = 0; |
1218 |
+ |
} |
1219 |
+ |
} |
1220 |
+ |
|
1221 |
+ |
#if DEBUG |
1222 |
+ |
static const char * reg_names[] = { |
1223 |
+ |
"g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", |
1224 |
+ |
"o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7", |
1225 |
+ |
"l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", |
1226 |
+ |
"i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7" |
1227 |
+ |
}; |
1228 |
+ |
printf("%s %s register %s\n", |
1229 |
+ |
transfer_size == SIZE_BYTE ? "byte" : |
1230 |
+ |
transfer_size == SIZE_WORD ? "word" : |
1231 |
+ |
transfer_size == SIZE_LONG ? "long" : |
1232 |
+ |
transfer_size == SIZE_QUAD ? "quad" : "unknown", |
1233 |
+ |
transfer_type == SIGSEGV_TRANSFER_LOAD ? "load to" : "store from", |
1234 |
+ |
reg_names[reg]); |
1235 |
+ |
#endif |
1236 |
+ |
|
1237 |
+ |
regs[SPARC_REG_PC] += 4; |
1238 |
+ |
return true; |
1239 |
+ |
} |
1240 |
+ |
#endif |
1241 |
|
#endif |
1242 |
|
|
1243 |
|
// Fallbacks |
1244 |
|
#ifndef SIGSEGV_FAULT_INSTRUCTION |
1245 |
|
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC |
1246 |
|
#endif |
1247 |
+ |
#ifndef SIGSEGV_FAULT_HANDLER_ARGLIST_1 |
1248 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGLIST_1 SIGSEGV_FAULT_HANDLER_ARGLIST |
1249 |
+ |
#endif |
1250 |
+ |
#ifndef SIGSEGV_FAULT_HANDLER_INVOKE |
1251 |
+ |
#define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP) sigsegv_fault_handler(ADDR, IP) |
1252 |
+ |
#endif |
1253 |
|
|
1254 |
|
// SIGSEGV recovery supported ? |
1255 |
|
#if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS) |
1264 |
|
#if defined(HAVE_SIGSEGV_RECOVERY) || defined(HAVE_MACH_EXCEPTIONS) |
1265 |
|
// This function handles the badaccess to memory. |
1266 |
|
// It is called from the signal handler or the exception handler. |
1267 |
< |
static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST) |
1267 |
> |
static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST_1) |
1268 |
|
{ |
1269 |
|
sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS; |
1270 |
|
sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION; |
1271 |
|
|
1272 |
|
// Call user's handler and reinstall the global handler, if required |
1273 |
< |
switch (sigsegv_fault_handler(fault_address, fault_instruction)) { |
1273 |
> |
switch (SIGSEGV_FAULT_HANDLER_INVOKE(fault_address, fault_instruction)) { |
1274 |
|
case SIGSEGV_RETURN_SUCCESS: |
1275 |
|
return true; |
1276 |
|
|
1657 |
|
#include <sys/mman.h> |
1658 |
|
#include "vm_alloc.h" |
1659 |
|
|
1660 |
+ |
const int REF_INDEX = 123; |
1661 |
+ |
const int REF_VALUE = 45; |
1662 |
+ |
|
1663 |
|
static int page_size; |
1664 |
|
static volatile char * page = 0; |
1665 |
|
static volatile int handler_called = 0; |
1666 |
|
|
1667 |
+ |
#ifdef __GNUC__ |
1668 |
+ |
// Code range where we expect the fault to come from |
1669 |
+ |
static void *b_region, *e_region; |
1670 |
+ |
#endif |
1671 |
+ |
|
1672 |
|
static sigsegv_return_t sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
1673 |
|
{ |
1674 |
+ |
#if DEBUG |
1675 |
+ |
printf("sigsegv_test_handler(%p, %p)\n", fault_address, instruction_address); |
1676 |
+ |
printf("expected fault at %p\n", page + REF_INDEX); |
1677 |
+ |
#ifdef __GNUC__ |
1678 |
+ |
printf("expected instruction address range: %p-%p\n", b_region, e_region); |
1679 |
+ |
#endif |
1680 |
+ |
#endif |
1681 |
|
handler_called++; |
1682 |
< |
if ((fault_address - 123) != page) |
1683 |
< |
exit(1); |
1682 |
> |
if ((fault_address - REF_INDEX) != page) |
1683 |
> |
exit(10); |
1684 |
> |
#ifdef __GNUC__ |
1685 |
> |
// Make sure reported fault instruction address falls into |
1686 |
> |
// expected code range |
1687 |
> |
if (instruction_address != SIGSEGV_INVALID_PC |
1688 |
> |
&& ((instruction_address < (sigsegv_address_t)b_region) || |
1689 |
> |
(instruction_address >= (sigsegv_address_t)e_region))) |
1690 |
> |
exit(11); |
1691 |
> |
#endif |
1692 |
|
if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0) |
1693 |
< |
exit(1); |
1693 |
> |
exit(12); |
1694 |
|
return SIGSEGV_RETURN_SUCCESS; |
1695 |
|
} |
1696 |
|
|
1697 |
|
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
1204 |
– |
#ifdef __GNUC__ |
1205 |
– |
// Code range where we expect the fault to come from |
1206 |
– |
static void *b_region, *e_region; |
1207 |
– |
#endif |
1208 |
– |
|
1698 |
|
static sigsegv_return_t sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
1699 |
|
{ |
1700 |
|
if (((unsigned long)fault_address - (unsigned long)page) < page_size) { |
1711 |
|
|
1712 |
|
return SIGSEGV_RETURN_FAILURE; |
1713 |
|
} |
1714 |
+ |
|
1715 |
+ |
// More sophisticated tests for instruction skipper |
1716 |
+ |
static bool arch_insn_skipper_tests() |
1717 |
+ |
{ |
1718 |
+ |
#if (defined(i386) || defined(__i386__)) || defined(__x86_64__) |
1719 |
+ |
static const unsigned char code[] = { |
1720 |
+ |
0x8a, 0x00, // mov (%eax),%al |
1721 |
+ |
0x8a, 0x2c, 0x18, // mov (%eax,%ebx,1),%ch |
1722 |
+ |
0x88, 0x20, // mov %ah,(%eax) |
1723 |
+ |
0x88, 0x08, // mov %cl,(%eax) |
1724 |
+ |
0x66, 0x8b, 0x00, // mov (%eax),%ax |
1725 |
+ |
0x66, 0x8b, 0x0c, 0x18, // mov (%eax,%ebx,1),%cx |
1726 |
+ |
0x66, 0x89, 0x00, // mov %ax,(%eax) |
1727 |
+ |
0x66, 0x89, 0x0c, 0x18, // mov %cx,(%eax,%ebx,1) |
1728 |
+ |
0x8b, 0x00, // mov (%eax),%eax |
1729 |
+ |
0x8b, 0x0c, 0x18, // mov (%eax,%ebx,1),%ecx |
1730 |
+ |
0x89, 0x00, // mov %eax,(%eax) |
1731 |
+ |
0x89, 0x0c, 0x18, // mov %ecx,(%eax,%ebx,1) |
1732 |
+ |
#if defined(__x86_64__) |
1733 |
+ |
0x44, 0x8a, 0x00, // mov (%rax),%r8b |
1734 |
+ |
0x44, 0x8a, 0x20, // mov (%rax),%r12b |
1735 |
+ |
0x42, 0x8a, 0x3c, 0x10, // mov (%rax,%r10,1),%dil |
1736 |
+ |
0x44, 0x88, 0x00, // mov %r8b,(%rax) |
1737 |
+ |
0x44, 0x88, 0x20, // mov %r12b,(%rax) |
1738 |
+ |
0x42, 0x88, 0x3c, 0x10, // mov %dil,(%rax,%r10,1) |
1739 |
+ |
0x66, 0x44, 0x8b, 0x00, // mov (%rax),%r8w |
1740 |
+ |
0x66, 0x42, 0x8b, 0x0c, 0x10, // mov (%rax,%r10,1),%cx |
1741 |
+ |
0x66, 0x44, 0x89, 0x00, // mov %r8w,(%rax) |
1742 |
+ |
0x66, 0x42, 0x89, 0x0c, 0x10, // mov %cx,(%rax,%r10,1) |
1743 |
+ |
0x44, 0x8b, 0x00, // mov (%rax),%r8d |
1744 |
+ |
0x42, 0x8b, 0x0c, 0x10, // mov (%rax,%r10,1),%ecx |
1745 |
+ |
0x44, 0x89, 0x00, // mov %r8d,(%rax) |
1746 |
+ |
0x42, 0x89, 0x0c, 0x10, // mov %ecx,(%rax,%r10,1) |
1747 |
+ |
0x48, 0x8b, 0x08, // mov (%rax),%rcx |
1748 |
+ |
0x4c, 0x8b, 0x18, // mov (%rax),%r11 |
1749 |
+ |
0x4a, 0x8b, 0x0c, 0x10, // mov (%rax,%r10,1),%rcx |
1750 |
+ |
0x4e, 0x8b, 0x1c, 0x10, // mov (%rax,%r10,1),%r11 |
1751 |
+ |
0x48, 0x89, 0x08, // mov %rcx,(%rax) |
1752 |
+ |
0x4c, 0x89, 0x18, // mov %r11,(%rax) |
1753 |
+ |
0x4a, 0x89, 0x0c, 0x10, // mov %rcx,(%rax,%r10,1) |
1754 |
+ |
0x4e, 0x89, 0x1c, 0x10, // mov %r11,(%rax,%r10,1) |
1755 |
+ |
#endif |
1756 |
+ |
0 // end |
1757 |
+ |
}; |
1758 |
+ |
const int N_REGS = 20; |
1759 |
+ |
unsigned long regs[N_REGS]; |
1760 |
+ |
for (int i = 0; i < N_REGS; i++) |
1761 |
+ |
regs[i] = i; |
1762 |
+ |
const unsigned long start_code = (unsigned long)&code; |
1763 |
+ |
regs[X86_REG_EIP] = start_code; |
1764 |
+ |
while ((regs[X86_REG_EIP] - start_code) < (sizeof(code) - 1) |
1765 |
+ |
&& ix86_skip_instruction(regs)) |
1766 |
+ |
; /* simply iterate */ |
1767 |
+ |
return (regs[X86_REG_EIP] - start_code) == (sizeof(code) - 1); |
1768 |
+ |
#endif |
1769 |
+ |
return true; |
1770 |
+ |
} |
1771 |
|
#endif |
1772 |
|
|
1773 |
|
int main(void) |
1777 |
|
|
1778 |
|
page_size = getpagesize(); |
1779 |
|
if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED) |
1780 |
< |
return 1; |
1780 |
> |
return 2; |
1781 |
|
|
1782 |
+ |
memset((void *)page, 0, page_size); |
1783 |
|
if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0) |
1784 |
< |
return 1; |
1784 |
> |
return 3; |
1785 |
|
|
1786 |
|
if (!sigsegv_install_handler(sigsegv_test_handler)) |
1787 |
< |
return 1; |
1241 |
< |
|
1242 |
< |
page[123] = 45; |
1243 |
< |
page[123] = 45; |
1787 |
> |
return 4; |
1788 |
|
|
1789 |
+ |
#ifdef __GNUC__ |
1790 |
+ |
b_region = &&L_b_region1; |
1791 |
+ |
e_region = &&L_e_region1; |
1792 |
+ |
#endif |
1793 |
+ |
L_b_region1: |
1794 |
+ |
page[REF_INDEX] = REF_VALUE; |
1795 |
+ |
if (page[REF_INDEX] != REF_VALUE) |
1796 |
+ |
exit(20); |
1797 |
+ |
page[REF_INDEX] = REF_VALUE; |
1798 |
+ |
L_e_region1: |
1799 |
+ |
|
1800 |
|
if (handler_called != 1) |
1801 |
< |
return 1; |
1801 |
> |
return 5; |
1802 |
|
|
1803 |
|
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
1804 |
|
if (!sigsegv_install_handler(sigsegv_insn_handler)) |
1805 |
< |
return 1; |
1805 |
> |
return 6; |
1806 |
|
|
1807 |
|
if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0) |
1808 |
< |
return 1; |
1808 |
> |
return 7; |
1809 |
|
|
1810 |
|
for (int i = 0; i < page_size; i++) |
1811 |
|
page[i] = (i + 1) % page_size; |
1812 |
|
|
1813 |
|
if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0) |
1814 |
< |
return 1; |
1814 |
> |
return 8; |
1815 |
|
|
1816 |
|
#define TEST_SKIP_INSTRUCTION(TYPE) do { \ |
1817 |
< |
const unsigned int TAG = 0x12345678; \ |
1817 |
> |
const unsigned long TAG = 0x12345678 | \ |
1818 |
> |
(sizeof(long) == 8 ? 0x9abcdef0UL << 31 : 0); \ |
1819 |
|
TYPE data = *((TYPE *)(page + sizeof(TYPE))); \ |
1820 |
< |
volatile unsigned int effect = data + TAG; \ |
1820 |
> |
volatile unsigned long effect = data + TAG; \ |
1821 |
|
if (effect != TAG) \ |
1822 |
< |
return 1; \ |
1822 |
> |
return 9; \ |
1823 |
|
} while (0) |
1824 |
|
|
1825 |
|
#ifdef __GNUC__ |
1826 |
< |
b_region = &&L_b_region; |
1827 |
< |
e_region = &&L_e_region; |
1826 |
> |
b_region = &&L_b_region2; |
1827 |
> |
e_region = &&L_e_region2; |
1828 |
|
#endif |
1829 |
< |
L_b_region: |
1829 |
> |
L_b_region2: |
1830 |
|
TEST_SKIP_INSTRUCTION(unsigned char); |
1831 |
|
TEST_SKIP_INSTRUCTION(unsigned short); |
1832 |
|
TEST_SKIP_INSTRUCTION(unsigned int); |
1833 |
< |
L_e_region: |
1833 |
> |
TEST_SKIP_INSTRUCTION(unsigned long); |
1834 |
> |
L_e_region2: |
1835 |
> |
|
1836 |
> |
if (!arch_insn_skipper_tests()) |
1837 |
> |
return 20; |
1838 |
|
#endif |
1839 |
|
|
1840 |
|
vm_exit(); |