ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
(Generate patch)

Comparing BasiliskII/src/Unix/sigsegv.cpp (file contents):
Revision 1.33 by gbeauche, 2003-10-21T23:10:19Z vs.
Revision 1.68 by gbeauche, 2007-12-30T12:11:17Z

# Line 10 | Line 10
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-2005 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
# Line 36 | Line 36
36   #endif
37  
38   #include <list>
39 + #include <stdio.h>
40   #include <signal.h>
41   #include "sigsegv.h"
42  
# Line 65 | Line 66 | static bool sigsegv_do_install_handler(i
66   *  Instruction decoding aids
67   */
68  
69 + // Transfer type
70 + enum transfer_type_t {
71 +        SIGSEGV_TRANSFER_UNKNOWN        = 0,
72 +        SIGSEGV_TRANSFER_LOAD           = 1,
73 +        SIGSEGV_TRANSFER_STORE          = 2,
74 + };
75 +
76   // Transfer size
77   enum transfer_size_t {
78          SIZE_UNKNOWN,
79          SIZE_BYTE,
80 <        SIZE_WORD,
81 <        SIZE_LONG
80 >        SIZE_WORD, // 2 bytes
81 >        SIZE_LONG, // 4 bytes
82 >        SIZE_QUAD, // 8 bytes
83   };
84  
76 // Transfer type
77 typedef sigsegv_transfer_type_t transfer_type_t;
78
85   #if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__))
86   // Addressing mode
87   enum addressing_mode_t {
# Line 95 | Line 101 | struct instruction_t {
101          char                            ra, rd;
102   };
103  
104 < static void powerpc_decode_instruction(instruction_t *instruction, unsigned int nip, unsigned int * gpr)
104 > static void powerpc_decode_instruction(instruction_t *instruction, unsigned int nip, unsigned long * gpr)
105   {
106          // Get opcode and divide into fields
107 <        unsigned int opcode = *((unsigned int *)nip);
107 >        unsigned int opcode = *((unsigned int *)(unsigned long)nip);
108          unsigned int primop = opcode >> 26;
109          unsigned int exop = (opcode >> 1) & 0x3ff;
110          unsigned int ra = (opcode >> 16) & 0x1f;
# Line 172 | Line 178 | static void powerpc_decode_instruction(i
178                  transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
179          case 45:        // sthu
180                  transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
181 +        case 58:        // ld, ldu, lwa
182 +                transfer_type = SIGSEGV_TRANSFER_LOAD;
183 +                transfer_size = SIZE_QUAD;
184 +                addr_mode = ((opcode & 3) == 1) ? MODE_U : MODE_NORM;
185 +                imm &= ~3;
186 +                break;
187 +        case 62:        // std, stdu, stq
188 +                transfer_type = SIGSEGV_TRANSFER_STORE;
189 +                transfer_size = SIZE_QUAD;
190 +                addr_mode = ((opcode & 3) == 1) ? MODE_U : MODE_NORM;
191 +                imm &= ~3;
192 +                break;
193          }
194          
195          // Calculate effective address
# Line 212 | Line 230 | static void powerpc_decode_instruction(i
230  
231   #if HAVE_SIGINFO_T
232   // Generic extended signal handler
233 < #if defined(__NetBSD__) || defined(__FreeBSD__)
233 > #if defined(__FreeBSD__)
234   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
235   #else
236   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
# Line 221 | Line 239 | static void powerpc_decode_instruction(i
239   #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 siginfo_t *sip, void *scp
240   #define SIGSEGV_FAULT_HANDLER_ARGS              sip, scp
241   #define SIGSEGV_FAULT_ADDRESS                   sip->si_addr
242 + #if (defined(sgi) || defined(__sgi))
243 + #include <ucontext.h>
244 + #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
245 + #define SIGSEGV_FAULT_INSTRUCTION               (unsigned long)SIGSEGV_CONTEXT_REGS[CTX_EPC]
246 + #if (defined(mips) || defined(__mips))
247 + #define SIGSEGV_REGISTER_FILE                   &SIGSEGV_CONTEXT_REGS[CTX_EPC], &SIGSEGV_CONTEXT_REGS[CTX_R0]
248 + #define SIGSEGV_SKIP_INSTRUCTION                mips_skip_instruction
249 + #endif
250 + #endif
251   #if defined(__sun__)
252   #if (defined(sparc) || defined(__sparc__))
253 + #include <sys/stack.h>
254 + #include <sys/regset.h>
255   #include <sys/ucontext.h>
256   #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
257   #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[REG_PC]
258 + #define SIGSEGV_SPARC_GWINDOWS                  (((ucontext_t *)scp)->uc_mcontext.gwins)
259 + #define SIGSEGV_SPARC_RWINDOW                   (struct rwindow *)((char *)SIGSEGV_CONTEXT_REGS[REG_SP] + STACK_BIAS)
260 + #define SIGSEGV_REGISTER_FILE                   ((unsigned long *)SIGSEGV_CONTEXT_REGS), SIGSEGV_SPARC_GWINDOWS, SIGSEGV_SPARC_RWINDOW
261 + #define SIGSEGV_SKIP_INSTRUCTION                sparc_skip_instruction
262 + #endif
263 + #if defined(__i386__)
264 + #include <sys/regset.h>
265 + #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
266 + #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[EIP]
267 + #define SIGSEGV_REGISTER_FILE                   (unsigned long *)SIGSEGV_CONTEXT_REGS
268 + #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
269   #endif
270   #endif
271 < #if defined(__FreeBSD__)
271 > #if defined(__FreeBSD__) || defined(__OpenBSD__)
272   #if (defined(i386) || defined(__i386__))
273   #define SIGSEGV_FAULT_INSTRUCTION               (((struct sigcontext *)scp)->sc_eip)
274 < #define SIGSEGV_REGISTER_FILE                   ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */
274 > #define SIGSEGV_REGISTER_FILE                   ((unsigned long *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */
275 > #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
276 > #endif
277 > #endif
278 > #if defined(__NetBSD__)
279 > #if (defined(i386) || defined(__i386__))
280 > #include <sys/ucontext.h>
281 > #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.__gregs)
282 > #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[_REG_EIP]
283 > #define SIGSEGV_REGISTER_FILE                   (unsigned long *)SIGSEGV_CONTEXT_REGS
284   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
285   #endif
286 + #if (defined(powerpc) || defined(__powerpc__))
287 + #include <sys/ucontext.h>
288 + #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.__gregs)
289 + #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[_REG_PC]
290 + #define SIGSEGV_REGISTER_FILE                   (unsigned long *)&SIGSEGV_CONTEXT_REGS[_REG_PC], (unsigned long *)&SIGSEGV_CONTEXT_REGS[_REG_R0]
291 + #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
292 + #endif
293   #endif
294   #if defined(__linux__)
295   #if (defined(i386) || defined(__i386__))
296   #include <sys/ucontext.h>
297   #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
298   #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[14] /* should use REG_EIP instead */
299 < #define SIGSEGV_REGISTER_FILE                   (unsigned int *)SIGSEGV_CONTEXT_REGS
299 > #define SIGSEGV_REGISTER_FILE                   (unsigned long *)SIGSEGV_CONTEXT_REGS
300   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
301   #endif
302   #if (defined(x86_64) || defined(__x86_64__))
# Line 248 | Line 304 | static void powerpc_decode_instruction(i
304   #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
305   #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[16] /* should use REG_RIP instead */
306   #define SIGSEGV_REGISTER_FILE                   (unsigned long *)SIGSEGV_CONTEXT_REGS
307 + #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
308   #endif
309   #if (defined(ia64) || defined(__ia64__))
310   #define SIGSEGV_FAULT_INSTRUCTION               (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */
# Line 256 | Line 313 | static void powerpc_decode_instruction(i
313   #include <sys/ucontext.h>
314   #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.regs)
315   #define SIGSEGV_FAULT_INSTRUCTION               (SIGSEGV_CONTEXT_REGS->nip)
316 < #define SIGSEGV_REGISTER_FILE                   (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr)
316 > #define SIGSEGV_REGISTER_FILE                   (unsigned long *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned long *)(SIGSEGV_CONTEXT_REGS->gpr)
317   #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
318   #endif
319 + #if (defined(hppa) || defined(__hppa__))
320 + #undef  SIGSEGV_FAULT_ADDRESS
321 + #define SIGSEGV_FAULT_ADDRESS                   sip->si_ptr
322 + #endif
323 + #if (defined(arm) || defined(__arm__))
324 + #include <asm/ucontext.h> /* use kernel structure, glibc may not be in sync */
325 + #define SIGSEGV_CONTEXT_REGS                    (((struct ucontext *)scp)->uc_mcontext)
326 + #define SIGSEGV_FAULT_INSTRUCTION               (SIGSEGV_CONTEXT_REGS.arm_pc)
327 + #define SIGSEGV_REGISTER_FILE                   (&SIGSEGV_CONTEXT_REGS.arm_r0)
328 + #define SIGSEGV_SKIP_INSTRUCTION                arm_skip_instruction
329 + #endif
330 + #if (defined(mips) || defined(__mips__))
331 + #include <sys/ucontext.h>
332 + #define SIGSEGV_CONTEXT_REGS                    (((struct ucontext *)scp)->uc_mcontext)
333 + #define SIGSEGV_FAULT_INSTRUCTION               (SIGSEGV_CONTEXT_REGS.pc)
334 + #define SIGSEGV_REGISTER_FILE                   &SIGSEGV_CONTEXT_REGS.pc, &SIGSEGV_CONTEXT_REGS.gregs[0]
335 + #define SIGSEGV_SKIP_INSTRUCTION                mips_skip_instruction
336 + #endif
337   #endif
338   #endif
339  
# Line 273 | Line 348 | static void powerpc_decode_instruction(i
348   #define SIGSEGV_FAULT_HANDLER_ARGS              &scs
349   #define SIGSEGV_FAULT_ADDRESS                   scp->cr2
350   #define SIGSEGV_FAULT_INSTRUCTION               scp->eip
351 < #define SIGSEGV_REGISTER_FILE                   (unsigned int *)scp
351 > #define SIGSEGV_REGISTER_FILE                   (unsigned long *)scp
352   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
353   #endif
354   #if (defined(sparc) || defined(__sparc__))
# Line 288 | Line 363 | static void powerpc_decode_instruction(i
363   #define SIGSEGV_FAULT_HANDLER_ARGS              sig, scp
364   #define SIGSEGV_FAULT_ADDRESS                   scp->regs->dar
365   #define SIGSEGV_FAULT_INSTRUCTION               scp->regs->nip
366 < #define SIGSEGV_REGISTER_FILE                   (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr)
366 > #define SIGSEGV_REGISTER_FILE                   (unsigned long *)&scp->regs->nip, (unsigned long *)(scp->regs->gpr)
367   #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
368   #endif
369   #if (defined(alpha) || defined(__alpha__))
# Line 298 | Line 373 | static void powerpc_decode_instruction(i
373   #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
374   #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_pc
375   #endif
376 + #if (defined(arm) || defined(__arm__))
377 + #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int r1, int r2, int r3, struct sigcontext sc
378 + #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 struct sigcontext *scp
379 + #define SIGSEGV_FAULT_HANDLER_ARGS              &sc
380 + #define SIGSEGV_FAULT_ADDRESS                   scp->fault_address
381 + #define SIGSEGV_FAULT_INSTRUCTION               scp->arm_pc
382 + #define SIGSEGV_REGISTER_FILE                   &scp->arm_r0
383 + #define SIGSEGV_SKIP_INSTRUCTION                arm_skip_instruction
384 + #endif
385   #endif
386  
387   // Irix 5 or 6 on MIPS
388 < #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4))
388 > #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(_SYSTYPE_SVR4))
389   #include <ucontext.h>
390   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
391   #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
392 < #define SIGSEGV_FAULT_ADDRESS                   scp->sc_badvaddr
392 > #define SIGSEGV_FAULT_ADDRESS                   (unsigned long)scp->sc_badvaddr
393 > #define SIGSEGV_FAULT_INSTRUCTION               (unsigned long)scp->sc_pc
394   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
395   #endif
396  
# Line 378 | Line 463 | static sigsegv_address_t get_fault_addre
463   #endif
464   #endif
465   #if defined(__FreeBSD__)
381 #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
466   #if (defined(i386) || defined(__i386__))
467 + #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
468   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp, char *addr
469   #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp, addr
470   #define SIGSEGV_FAULT_ADDRESS                   addr
471   #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_eip
472 < #define SIGSEGV_REGISTER_FILE                   ((unsigned int *)&scp->sc_edi)
472 > #define SIGSEGV_REGISTER_FILE                   ((unsigned long *)&scp->sc_edi)
473   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
474   #endif
475 + #if (defined(alpha) || defined(__alpha__))
476 + #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
477 + #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, char *addr, struct sigcontext *scp
478 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, addr, scp
479 + #define SIGSEGV_FAULT_ADDRESS                   addr
480 + #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_pc
481 + #endif
482   #endif
483  
484   // Extract fault address out of a sigcontext
# Line 435 | Line 527 | static sigsegv_address_t get_fault_addre
527   #endif
528   #endif
529  
530 + #if HAVE_WIN32_EXCEPTIONS
531 + #define WIN32_LEAN_AND_MEAN /* avoid including junk */
532 + #include <windows.h>
533 + #include <winerror.h>
534 +
535 + #define SIGSEGV_FAULT_HANDLER_ARGLIST   EXCEPTION_POINTERS *ExceptionInfo
536 + #define SIGSEGV_FAULT_HANDLER_ARGS              ExceptionInfo
537 + #define SIGSEGV_FAULT_ADDRESS                   ExceptionInfo->ExceptionRecord->ExceptionInformation[1]
538 + #define SIGSEGV_CONTEXT_REGS                    ExceptionInfo->ContextRecord
539 + #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS->Eip
540 + #define SIGSEGV_REGISTER_FILE                   ((unsigned long *)&SIGSEGV_CONTEXT_REGS->Edi)
541 + #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
542 + #endif
543 +
544   #if HAVE_MACH_EXCEPTIONS
545  
546   // This can easily be extended to other Mach systems, but really who
# Line 495 | Line 601 | if (ret != KERN_SUCCESS) { \
601          exit (1); \
602   }
603  
604 < #define SIGSEGV_FAULT_ADDRESS                   code[1]
605 < #define SIGSEGV_FAULT_INSTRUCTION               get_fault_instruction(thread, state)
606 < #define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP)  ((code[0] == KERN_PROTECTION_FAILURE) ? sigsegv_fault_handler(ADDR, IP) : SIGSEGV_RETURN_FAILURE)
607 < #define SIGSEGV_FAULT_HANDLER_ARGLIST   mach_port_t thread, exception_data_t code, ppc_thread_state_t *state
608 < #define SIGSEGV_FAULT_HANDLER_ARGS              thread, code, &state
604 > #ifdef __ppc__
605 > #define SIGSEGV_EXCEPTION_STATE_TYPE    ppc_exception_state_t
606 > #define SIGSEGV_EXCEPTION_STATE_FLAVOR  PPC_EXCEPTION_STATE
607 > #define SIGSEGV_EXCEPTION_STATE_COUNT   PPC_EXCEPTION_STATE_COUNT
608 > #define SIGSEGV_FAULT_ADDRESS                   sip->exc_state.dar
609 > #define SIGSEGV_THREAD_STATE_TYPE               ppc_thread_state_t
610 > #define SIGSEGV_THREAD_STATE_FLAVOR             PPC_THREAD_STATE
611 > #define SIGSEGV_THREAD_STATE_COUNT              PPC_THREAD_STATE_COUNT
612 > #define SIGSEGV_FAULT_INSTRUCTION               sip->thr_state.srr0
613   #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
614 < #define SIGSEGV_REGISTER_FILE                   &state->srr0, &state->r0
615 <
616 < // Given a suspended thread, stuff the current instruction and
617 < // registers into state.
618 < //
619 < // It would have been nice to have this be ppc/x86 independant which
620 < // could have been done easily with a thread_state_t instead of
621 < // ppc_thread_state_t, but because of the way this is called it is
622 < // easier to do it this way.
623 < #if (defined(ppc) || defined(__ppc__))
624 < static inline sigsegv_address_t get_fault_instruction(mach_port_t thread, ppc_thread_state_t *state)
625 < {
626 <        kern_return_t krc;
517 <        mach_msg_type_number_t count;
518 <
519 <        count = MACHINE_THREAD_STATE_COUNT;
520 <        krc = thread_get_state(thread, MACHINE_THREAD_STATE, (thread_state_t)state, &count);
521 <        MACH_CHECK_ERROR (thread_get_state, krc);
522 <
523 <        return (sigsegv_address_t)state->srr0;
524 < }
614 > #define SIGSEGV_REGISTER_FILE                   (unsigned long *)&sip->thr_state.srr0, (unsigned long *)&sip->thr_state.r0
615 > #endif
616 > #ifdef __i386__
617 > #define SIGSEGV_EXCEPTION_STATE_TYPE    struct i386_exception_state
618 > #define SIGSEGV_EXCEPTION_STATE_FLAVOR  i386_EXCEPTION_STATE
619 > #define SIGSEGV_EXCEPTION_STATE_COUNT   i386_EXCEPTION_STATE_COUNT
620 > #define SIGSEGV_FAULT_ADDRESS                   sip->exc_state.faultvaddr
621 > #define SIGSEGV_THREAD_STATE_TYPE               struct i386_thread_state
622 > #define SIGSEGV_THREAD_STATE_FLAVOR             i386_THREAD_STATE
623 > #define SIGSEGV_THREAD_STATE_COUNT              i386_THREAD_STATE_COUNT
624 > #define SIGSEGV_FAULT_INSTRUCTION               sip->thr_state.eip
625 > #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
626 > #define SIGSEGV_REGISTER_FILE                   ((unsigned long *)&sip->thr_state.eax) /* EAX is the first GPR we consider */
627   #endif
628 + #ifdef __x86_64__
629 + #define SIGSEGV_EXCEPTION_STATE_TYPE    struct x86_exception_state64
630 + #define SIGSEGV_EXCEPTION_STATE_FLAVOR  x86_EXCEPTION_STATE64
631 + #define SIGSEGV_EXCEPTION_STATE_COUNT   x86_EXCEPTION_STATE64_COUNT
632 + #define SIGSEGV_FAULT_ADDRESS                   sip->exc_state.faultvaddr
633 + #define SIGSEGV_THREAD_STATE_TYPE               struct x86_thread_state64
634 + #define SIGSEGV_THREAD_STATE_FLAVOR             x86_THREAD_STATE64
635 + #define SIGSEGV_THREAD_STATE_COUNT              x86_THREAD_STATE64_COUNT
636 + #define SIGSEGV_FAULT_INSTRUCTION               sip->thr_state.rip
637 + #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
638 + #define SIGSEGV_REGISTER_FILE                   ((unsigned long *)&sip->thr_state.rax) /* RAX is the first GPR we consider */
639 + #endif
640 + #define SIGSEGV_FAULT_ADDRESS_FAST              code[1]
641 + #define SIGSEGV_FAULT_INSTRUCTION_FAST  SIGSEGV_INVALID_ADDRESS
642 + #define SIGSEGV_FAULT_HANDLER_ARGLIST   mach_port_t thread, exception_data_t code
643 + #define SIGSEGV_FAULT_HANDLER_ARGS              thread, code
644  
645   // Since there can only be one exception thread running at any time
646   // this is not a problem.
# Line 577 | Line 695 | handleExceptions(void *priv)
695  
696   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
697   // Decode and skip X86 instruction
698 < #if (defined(i386) || defined(__i386__))
698 > #if (defined(i386) || defined(__i386__)) || defined(__x86_64__)
699   #if defined(__linux__)
700   enum {
701 + #if (defined(i386) || defined(__i386__))
702          X86_REG_EIP = 14,
703          X86_REG_EAX = 11,
704          X86_REG_ECX = 10,
# Line 589 | Line 708 | enum {
708          X86_REG_EBP = 6,
709          X86_REG_ESI = 5,
710          X86_REG_EDI = 4
711 + #endif
712 + #if defined(__x86_64__)
713 +        X86_REG_R8  = 0,
714 +        X86_REG_R9  = 1,
715 +        X86_REG_R10 = 2,
716 +        X86_REG_R11 = 3,
717 +        X86_REG_R12 = 4,
718 +        X86_REG_R13 = 5,
719 +        X86_REG_R14 = 6,
720 +        X86_REG_R15 = 7,
721 +        X86_REG_EDI = 8,
722 +        X86_REG_ESI = 9,
723 +        X86_REG_EBP = 10,
724 +        X86_REG_EBX = 11,
725 +        X86_REG_EDX = 12,
726 +        X86_REG_EAX = 13,
727 +        X86_REG_ECX = 14,
728 +        X86_REG_ESP = 15,
729 +        X86_REG_EIP = 16
730 + #endif
731 + };
732 + #endif
733 + #if defined(__NetBSD__)
734 + enum {
735 + #if (defined(i386) || defined(__i386__))
736 +        X86_REG_EIP = _REG_EIP,
737 +        X86_REG_EAX = _REG_EAX,
738 +        X86_REG_ECX = _REG_ECX,
739 +        X86_REG_EDX = _REG_EDX,
740 +        X86_REG_EBX = _REG_EBX,
741 +        X86_REG_ESP = _REG_ESP,
742 +        X86_REG_EBP = _REG_EBP,
743 +        X86_REG_ESI = _REG_ESI,
744 +        X86_REG_EDI = _REG_EDI
745 + #endif
746 + };
747 + #endif
748 + #if defined(__FreeBSD__)
749 + enum {
750 + #if (defined(i386) || defined(__i386__))
751 +        X86_REG_EIP = 10,
752 +        X86_REG_EAX = 7,
753 +        X86_REG_ECX = 6,
754 +        X86_REG_EDX = 5,
755 +        X86_REG_EBX = 4,
756 +        X86_REG_ESP = 13,
757 +        X86_REG_EBP = 2,
758 +        X86_REG_ESI = 1,
759 +        X86_REG_EDI = 0
760 + #endif
761 + };
762 + #endif
763 + #if defined(__OpenBSD__)
764 + enum {
765 + #if defined(__i386__)
766 +        // EDI is the first register we consider
767 + #define OREG(REG) offsetof(struct sigcontext, sc_##REG)
768 + #define DREG(REG) ((OREG(REG) - OREG(edi)) / 4)
769 +        X86_REG_EIP = DREG(eip), // 7
770 +        X86_REG_EAX = DREG(eax), // 6
771 +        X86_REG_ECX = DREG(ecx), // 5
772 +        X86_REG_EDX = DREG(edx), // 4
773 +        X86_REG_EBX = DREG(ebx), // 3
774 +        X86_REG_ESP = DREG(esp), // 10
775 +        X86_REG_EBP = DREG(ebp), // 2
776 +        X86_REG_ESI = DREG(esi), // 1
777 +        X86_REG_EDI = DREG(edi)  // 0
778 + #undef DREG
779 + #undef OREG
780 + #endif
781 + };
782 + #endif
783 + #if defined(__sun__)
784 + // Same as for Linux, need to check for x86-64
785 + enum {
786 + #if defined(__i386__)
787 +        X86_REG_EIP = EIP,
788 +        X86_REG_EAX = EAX,
789 +        X86_REG_ECX = ECX,
790 +        X86_REG_EDX = EDX,
791 +        X86_REG_EBX = EBX,
792 +        X86_REG_ESP = ESP,
793 +        X86_REG_EBP = EBP,
794 +        X86_REG_ESI = ESI,
795 +        X86_REG_EDI = EDI
796 + #endif
797   };
798   #endif
799 < #if defined(__NetBSD__) || defined(__FreeBSD__)
799 > #if defined(__APPLE__) && defined(__MACH__)
800   enum {
801 + #if (defined(i386) || defined(__i386__))
802 + #ifdef i386_SAVED_STATE
803 +        // same as FreeBSD (in Open Darwin 8.0.1)
804          X86_REG_EIP = 10,
805          X86_REG_EAX = 7,
806          X86_REG_ECX = 6,
# Line 602 | Line 810 | enum {
810          X86_REG_EBP = 2,
811          X86_REG_ESI = 1,
812          X86_REG_EDI = 0
813 + #else
814 +        // new layout (MacOS X 10.4.4 for x86)
815 +        X86_REG_EIP = 10,
816 +        X86_REG_EAX = 0,
817 +        X86_REG_ECX = 2,
818 +        X86_REG_EDX = 3,
819 +        X86_REG_EBX = 1,
820 +        X86_REG_ESP = 7,
821 +        X86_REG_EBP = 6,
822 +        X86_REG_ESI = 5,
823 +        X86_REG_EDI = 4
824 + #endif
825 + #endif
826 + #if defined(__x86_64__)
827 +        X86_REG_R8  = 8,
828 +        X86_REG_R9  = 9,
829 +        X86_REG_R10 = 10,
830 +        X86_REG_R11 = 11,
831 +        X86_REG_R12 = 12,
832 +        X86_REG_R13 = 13,
833 +        X86_REG_R14 = 14,
834 +        X86_REG_R15 = 15,
835 +        X86_REG_EDI = 4,
836 +        X86_REG_ESI = 5,
837 +        X86_REG_EBP = 6,
838 +        X86_REG_EBX = 1,
839 +        X86_REG_EDX = 3,
840 +        X86_REG_EAX = 0,
841 +        X86_REG_ECX = 2,
842 +        X86_REG_ESP = 7,
843 +        X86_REG_EIP = 16
844 + #endif
845 + };
846 + #endif
847 + #if defined(_WIN32)
848 + enum {
849 + #if (defined(i386) || defined(__i386__))
850 +        X86_REG_EIP = 7,
851 +        X86_REG_EAX = 5,
852 +        X86_REG_ECX = 4,
853 +        X86_REG_EDX = 3,
854 +        X86_REG_EBX = 2,
855 +        X86_REG_ESP = 10,
856 +        X86_REG_EBP = 6,
857 +        X86_REG_ESI = 1,
858 +        X86_REG_EDI = 0
859 + #endif
860   };
861   #endif
862   // FIXME: this is partly redundant with the instruction decoding phase
# Line 638 | Line 893 | static inline int ix86_step_over_modrm(u
893          return offset;
894   }
895  
896 < static bool ix86_skip_instruction(unsigned int * regs)
896 > static bool ix86_skip_instruction(unsigned long * regs)
897   {
898          unsigned char * eip = (unsigned char *)regs[X86_REG_EIP];
899  
900          if (eip == 0)
901                  return false;
902 + #ifdef _WIN32
903 +        if (IsBadCodePtr((FARPROC)eip))
904 +                return false;
905 + #endif
906          
907 +        enum instruction_type_t {
908 +                i_MOV,
909 +                i_ADD
910 +        };
911 +
912          transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
913          transfer_size_t transfer_size = SIZE_LONG;
914 +        instruction_type_t instruction_type = i_MOV;
915          
916          int reg = -1;
917          int len = 0;
918 <        
918 >
919 > #if DEBUG
920 >        printf("IP: %p [%02x %02x %02x %02x...]\n",
921 >                   eip, eip[0], eip[1], eip[2], eip[3]);
922 > #endif
923 >
924          // Operand size prefix
925          if (*eip == 0x66) {
926                  eip++;
# Line 658 | Line 928 | static bool ix86_skip_instruction(unsign
928                  transfer_size = SIZE_WORD;
929          }
930  
931 +        // REX prefix
932 + #if defined(__x86_64__)
933 +        struct rex_t {
934 +                unsigned char W;
935 +                unsigned char R;
936 +                unsigned char X;
937 +                unsigned char B;
938 +        };
939 +        rex_t rex = { 0, 0, 0, 0 };
940 +        bool has_rex = false;
941 +        if ((*eip & 0xf0) == 0x40) {
942 +                has_rex = true;
943 +                const unsigned char b = *eip;
944 +                rex.W = b & (1 << 3);
945 +                rex.R = b & (1 << 2);
946 +                rex.X = b & (1 << 1);
947 +                rex.B = b & (1 << 0);
948 + #if DEBUG
949 +                printf("REX: %c,%c,%c,%c\n",
950 +                           rex.W ? 'W' : '_',
951 +                           rex.R ? 'R' : '_',
952 +                           rex.X ? 'X' : '_',
953 +                           rex.B ? 'B' : '_');
954 + #endif
955 +                eip++;
956 +                len++;
957 +                if (rex.W)
958 +                        transfer_size = SIZE_QUAD;
959 +        }
960 + #else
961 +        const bool has_rex = false;
962 + #endif
963 +
964          // Decode instruction
965 +        int op_len = 1;
966 +        int target_size = SIZE_UNKNOWN;
967          switch (eip[0]) {
968          case 0x0f:
969 +                target_size = transfer_size;
970              switch (eip[1]) {
971 +                case 0xbe: // MOVSX r32, r/m8
972              case 0xb6: // MOVZX r32, r/m8
973 +                        transfer_size = SIZE_BYTE;
974 +                        goto do_mov_extend;
975 +                case 0xbf: // MOVSX r32, r/m16
976              case 0xb7: // MOVZX r32, r/m16
977 <                switch (eip[2] & 0xc0) {
978 <                case 0x80:
979 <                    reg = (eip[2] >> 3) & 7;
980 <                    transfer_type = SIGSEGV_TRANSFER_LOAD;
981 <                    break;
672 <                case 0x40:
673 <                    reg = (eip[2] >> 3) & 7;
674 <                    transfer_type = SIGSEGV_TRANSFER_LOAD;
675 <                    break;
676 <                case 0x00:
677 <                    reg = (eip[2] >> 3) & 7;
678 <                    transfer_type = SIGSEGV_TRANSFER_LOAD;
679 <                    break;
977 >                        transfer_size = SIZE_WORD;
978 >                        goto do_mov_extend;
979 >                  do_mov_extend:
980 >                        op_len = 2;
981 >                        goto do_transfer_load;
982                  }
681                len += 3 + ix86_step_over_modrm(eip + 2);
983                  break;
984 <            }
985 <          break;
984 > #if defined(__x86_64__)
985 >        case 0x63: // MOVSXD r64, r/m32
986 >                if (has_rex && rex.W) {
987 >                        transfer_size = SIZE_LONG;
988 >                        target_size = SIZE_QUAD;
989 >                }
990 >                else if (transfer_size != SIZE_WORD) {
991 >                        transfer_size = SIZE_LONG;
992 >                        target_size = SIZE_QUAD;
993 >                }
994 >                goto do_transfer_load;
995 > #endif
996 >        case 0x02: // ADD r8, r/m8
997 >                transfer_size = SIZE_BYTE;
998 >        case 0x03: // ADD r32, r/m32
999 >                instruction_type = i_ADD;
1000 >                goto do_transfer_load;
1001          case 0x8a: // MOV r8, r/m8
1002                  transfer_size = SIZE_BYTE;
1003          case 0x8b: // MOV r32, r/m32 (or 16-bit operation)
1004 <                switch (eip[1] & 0xc0) {
1004 >          do_transfer_load:
1005 >                switch (eip[op_len] & 0xc0) {
1006                  case 0x80:
1007 <                        reg = (eip[1] >> 3) & 7;
1007 >                        reg = (eip[op_len] >> 3) & 7;
1008                          transfer_type = SIGSEGV_TRANSFER_LOAD;
1009                          break;
1010                  case 0x40:
1011 <                        reg = (eip[1] >> 3) & 7;
1011 >                        reg = (eip[op_len] >> 3) & 7;
1012                          transfer_type = SIGSEGV_TRANSFER_LOAD;
1013                          break;
1014                  case 0x00:
1015 <                        reg = (eip[1] >> 3) & 7;
1015 >                        reg = (eip[op_len] >> 3) & 7;
1016                          transfer_type = SIGSEGV_TRANSFER_LOAD;
1017                          break;
1018                  }
1019 <                len += 2 + ix86_step_over_modrm(eip + 1);
1019 >                len += 1 + op_len + ix86_step_over_modrm(eip + op_len);
1020                  break;
1021 +        case 0x00: // ADD r/m8, r8
1022 +                transfer_size = SIZE_BYTE;
1023 +        case 0x01: // ADD r/m32, r32
1024 +                instruction_type = i_ADD;
1025 +                goto do_transfer_store;
1026          case 0x88: // MOV r/m8, r8
1027                  transfer_size = SIZE_BYTE;
1028          case 0x89: // MOV r/m32, r32 (or 16-bit operation)
1029 <                switch (eip[1] & 0xc0) {
1029 >          do_transfer_store:
1030 >                switch (eip[op_len] & 0xc0) {
1031                  case 0x80:
1032 <                        reg = (eip[1] >> 3) & 7;
1032 >                        reg = (eip[op_len] >> 3) & 7;
1033                          transfer_type = SIGSEGV_TRANSFER_STORE;
1034                          break;
1035                  case 0x40:
1036 <                        reg = (eip[1] >> 3) & 7;
1036 >                        reg = (eip[op_len] >> 3) & 7;
1037                          transfer_type = SIGSEGV_TRANSFER_STORE;
1038                          break;
1039                  case 0x00:
1040 <                        reg = (eip[1] >> 3) & 7;
1040 >                        reg = (eip[op_len] >> 3) & 7;
1041                          transfer_type = SIGSEGV_TRANSFER_STORE;
1042                          break;
1043                  }
1044 <                len += 2 + ix86_step_over_modrm(eip + 1);
1044 >                len += 1 + op_len + ix86_step_over_modrm(eip + op_len);
1045                  break;
1046          }
1047 +        if (target_size == SIZE_UNKNOWN)
1048 +                target_size = transfer_size;
1049  
1050          if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
1051                  // Unknown machine code, let it crash. Then patch the decoder
1052                  return false;
1053          }
1054  
1055 <        if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != -1) {
1056 <                static const int x86_reg_map[8] = {
1055 > #if defined(__x86_64__)
1056 >        if (rex.R)
1057 >                reg += 8;
1058 > #endif
1059 >
1060 >        if (instruction_type == i_MOV && transfer_type == SIGSEGV_TRANSFER_LOAD && reg != -1) {
1061 >                static const int x86_reg_map[] = {
1062                          X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX,
1063 <                        X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI
1063 >                        X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI,
1064 > #if defined(__x86_64__)
1065 >                        X86_REG_R8,  X86_REG_R9,  X86_REG_R10, X86_REG_R11,
1066 >                        X86_REG_R12, X86_REG_R13, X86_REG_R14, X86_REG_R15,
1067 > #endif
1068                  };
1069                  
1070 <                if (reg < 0 || reg >= 8)
1070 >                if (reg < 0 || reg >= (sizeof(x86_reg_map)/sizeof(x86_reg_map[0]) - 1))
1071                          return false;
1072  
1073 +                // Set 0 to the relevant register part
1074 +                // NOTE: this is only valid for MOV alike instructions
1075                  int rloc = x86_reg_map[reg];
1076 <                switch (transfer_size) {
1076 >                switch (target_size) {
1077                  case SIZE_BYTE:
1078 <                        regs[rloc] = (regs[rloc] & ~0xff);
1078 >                        if (has_rex || reg < 4)
1079 >                                regs[rloc] = (regs[rloc] & ~0x00ffL);
1080 >                        else {
1081 >                                rloc = x86_reg_map[reg - 4];
1082 >                                regs[rloc] = (regs[rloc] & ~0xff00L);
1083 >                        }
1084                          break;
1085                  case SIZE_WORD:
1086 <                        regs[rloc] = (regs[rloc] & ~0xffff);
1086 >                        regs[rloc] = (regs[rloc] & ~0xffffL);
1087                          break;
1088                  case SIZE_LONG:
1089 +                case SIZE_QUAD: // zero-extension
1090                          regs[rloc] = 0;
1091                          break;
1092                  }
1093          }
1094  
1095   #if DEBUG
1096 <        printf("%08x: %s %s access", regs[X86_REG_EIP],
1097 <                   transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long",
1096 >        printf("%p: %s %s access", (void *)regs[X86_REG_EIP],
1097 >                   transfer_size == SIZE_BYTE ? "byte" :
1098 >                   transfer_size == SIZE_WORD ? "word" :
1099 >                   transfer_size == SIZE_LONG ? "long" :
1100 >                   transfer_size == SIZE_QUAD ? "quad" : "unknown",
1101                     transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write");
1102          
1103          if (reg != -1) {
1104 <                static const char * x86_reg_str_map[8] = {
1105 <                        "eax", "ecx", "edx", "ebx",
1106 <                        "esp", "ebp", "esi", "edi"
1104 >                static const char * x86_byte_reg_str_map[] = {
1105 >                        "al",   "cl",   "dl",   "bl",
1106 >                        "spl",  "bpl",  "sil",  "dil",
1107 >                        "r8b",  "r9b",  "r10b", "r11b",
1108 >                        "r12b", "r13b", "r14b", "r15b",
1109 >                        "ah",   "ch",   "dh",   "bh",
1110 >                };
1111 >                static const char * x86_word_reg_str_map[] = {
1112 >                        "ax",   "cx",   "dx",   "bx",
1113 >                        "sp",   "bp",   "si",   "di",
1114 >                        "r8w",  "r9w",  "r10w", "r11w",
1115 >                        "r12w", "r13w", "r14w", "r15w",
1116 >                };
1117 >                static const char *x86_long_reg_str_map[] = {
1118 >                        "eax",  "ecx",  "edx",  "ebx",
1119 >                        "esp",  "ebp",  "esi",  "edi",
1120 >                        "r8d",  "r9d",  "r10d", "r11d",
1121 >                        "r12d", "r13d", "r14d", "r15d",
1122                  };
1123 <                printf(" %s register %%%s", transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from", x86_reg_str_map[reg]);
1123 >                static const char *x86_quad_reg_str_map[] = {
1124 >                        "rax", "rcx", "rdx", "rbx",
1125 >                        "rsp", "rbp", "rsi", "rdi",
1126 >                        "r8",  "r9",  "r10", "r11",
1127 >                        "r12", "r13", "r14", "r15",
1128 >                };
1129 >                const char * reg_str = NULL;
1130 >                switch (target_size) {
1131 >                case SIZE_BYTE:
1132 >                        reg_str = x86_byte_reg_str_map[(!has_rex && reg >= 4 ? 12 : 0) + reg];
1133 >                        break;
1134 >                case SIZE_WORD: reg_str = x86_word_reg_str_map[reg]; break;
1135 >                case SIZE_LONG: reg_str = x86_long_reg_str_map[reg]; break;
1136 >                case SIZE_QUAD: reg_str = x86_quad_reg_str_map[reg]; break;
1137 >                }
1138 >                if (reg_str)
1139 >                        printf(" %s register %%%s",
1140 >                                   transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from",
1141 >                                   reg_str);
1142          }
1143          printf(", %d bytes instruction\n", len);
1144   #endif
# Line 772 | Line 1150 | static bool ix86_skip_instruction(unsign
1150  
1151   // Decode and skip PPC instruction
1152   #if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__))
1153 < static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs)
1153 > static bool powerpc_skip_instruction(unsigned long * nip_p, unsigned long * regs)
1154   {
1155          instruction_t instr;
1156          powerpc_decode_instruction(&instr, *nip_p, regs);
# Line 784 | Line 1162 | static bool powerpc_skip_instruction(uns
1162  
1163   #if DEBUG
1164          printf("%08x: %s %s access", *nip_p,
1165 <                   instr.transfer_size == SIZE_BYTE ? "byte" : instr.transfer_size == SIZE_WORD ? "word" : "long",
1165 >                   instr.transfer_size == SIZE_BYTE ? "byte" :
1166 >                   instr.transfer_size == SIZE_WORD ? "word" :
1167 >                   instr.transfer_size == SIZE_LONG ? "long" : "quad",
1168                     instr.transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write");
1169          
1170          if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX)
# Line 802 | Line 1182 | static bool powerpc_skip_instruction(uns
1182          return true;
1183   }
1184   #endif
1185 +
1186 + // Decode and skip MIPS instruction
1187 + #if (defined(mips) || defined(__mips))
1188 + static bool mips_skip_instruction(greg_t * pc_p, greg_t * regs)
1189 + {
1190 +  unsigned int * epc = (unsigned int *)(unsigned long)*pc_p;
1191 +
1192 +  if (epc == 0)
1193 +        return false;
1194 +
1195 + #if DEBUG
1196 +  printf("IP: %p [%08x]\n", epc, epc[0]);
1197   #endif
1198  
1199 +  transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
1200 +  transfer_size_t transfer_size = SIZE_LONG;
1201 +  int direction = 0;
1202 +
1203 +  const unsigned int opcode = epc[0];
1204 +  switch (opcode >> 26) {
1205 +  case 32: // Load Byte
1206 +  case 36: // Load Byte Unsigned
1207 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1208 +        transfer_size = SIZE_BYTE;
1209 +        break;
1210 +  case 33: // Load Halfword
1211 +  case 37: // Load Halfword Unsigned
1212 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1213 +        transfer_size = SIZE_WORD;
1214 +        break;
1215 +  case 35: // Load Word
1216 +  case 39: // Load Word Unsigned
1217 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1218 +        transfer_size = SIZE_LONG;
1219 +        break;
1220 +  case 34: // Load Word Left
1221 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1222 +        transfer_size = SIZE_LONG;
1223 +        direction = -1;
1224 +        break;
1225 +  case 38: // Load Word Right
1226 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1227 +        transfer_size = SIZE_LONG;
1228 +        direction = 1;
1229 +        break;
1230 +  case 55: // Load Doubleword
1231 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1232 +        transfer_size = SIZE_QUAD;
1233 +        break;
1234 +  case 26: // Load Doubleword Left
1235 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1236 +        transfer_size = SIZE_QUAD;
1237 +        direction = -1;
1238 +        break;
1239 +  case 27: // Load Doubleword Right
1240 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1241 +        transfer_size = SIZE_QUAD;
1242 +        direction = 1;
1243 +        break;
1244 +  case 40: // Store Byte
1245 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1246 +        transfer_size = SIZE_BYTE;
1247 +        break;
1248 +  case 41: // Store Halfword
1249 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1250 +        transfer_size = SIZE_WORD;
1251 +        break;
1252 +  case 43: // Store Word
1253 +  case 42: // Store Word Left
1254 +  case 46: // Store Word Right
1255 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1256 +        transfer_size = SIZE_LONG;
1257 +        break;
1258 +  case 63: // Store Doubleword
1259 +  case 44: // Store Doubleword Left
1260 +  case 45: // Store Doubleword Right
1261 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1262 +        transfer_size = SIZE_QUAD;
1263 +        break;
1264 +  /* Misc instructions unlikely to be used within CPU emulators */
1265 +  case 48: // Load Linked Word
1266 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1267 +        transfer_size = SIZE_LONG;
1268 +        break;
1269 +  case 52: // Load Linked Doubleword
1270 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1271 +        transfer_size = SIZE_QUAD;
1272 +        break;
1273 +  case 56: // Store Conditional Word
1274 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1275 +        transfer_size = SIZE_LONG;
1276 +        break;
1277 +  case 60: // Store Conditional Doubleword
1278 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1279 +        transfer_size = SIZE_QUAD;
1280 +        break;
1281 +  }
1282 +
1283 +  if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
1284 +        // Unknown machine code, let it crash. Then patch the decoder
1285 +        return false;
1286 +  }
1287 +
1288 +  // Zero target register in case of a load operation
1289 +  const int reg = (opcode >> 16) & 0x1f;
1290 +  if (transfer_type == SIGSEGV_TRANSFER_LOAD) {
1291 +        if (direction == 0)
1292 +          regs[reg] = 0;
1293 +        else {
1294 +          // FIXME: untested code
1295 +          unsigned long ea = regs[(opcode >> 21) & 0x1f];
1296 +          ea += (signed long)(signed int)(signed short)(opcode & 0xffff);
1297 +          const int offset = ea & (transfer_size == SIZE_LONG ? 3 : 7);
1298 +          unsigned long value;
1299 +          if (direction > 0) {
1300 +                const unsigned long rmask = ~((1L << ((offset + 1) * 8)) - 1);
1301 +                value = regs[reg] & rmask;
1302 +          }
1303 +          else {
1304 +                const unsigned long lmask = (1L << (offset * 8)) - 1;
1305 +                value = regs[reg] & lmask;
1306 +          }
1307 +          // restore most significant bits
1308 +          if (transfer_size == SIZE_LONG)
1309 +                value = (signed long)(signed int)value;
1310 +          regs[reg] = value;
1311 +        }
1312 +  }
1313 +
1314 + #if DEBUG
1315 + #if (defined(_ABIN32) || defined(_ABI64))
1316 +  static const char * mips_gpr_names[32] = {
1317 +        "zero", "at",   "v0",   "v1",   "a0",   "a1",   "a2",   "a3",
1318 +        "t0",   "t1",   "t2",   "t3",   "t4",   "t5",   "t6",   "t7",
1319 +        "s0",   "s1",   "s2",   "s3",   "s4",   "s5",   "s6",   "s7",
1320 +        "t8",   "t9",   "k0",   "k1",   "gp",   "sp",   "s8",   "ra"
1321 +  };
1322 + #else
1323 +  static const char * mips_gpr_names[32] = {
1324 +        "zero", "at",   "v0",   "v1",   "a0",   "a1",   "a2",   "a3",
1325 +        "a4",   "a5",   "a6",   "a7",   "t0",   "t1",   "t2",   "t3",
1326 +        "s0",   "s1",   "s2",   "s3",   "s4",   "s5",   "s6",   "s7",
1327 +        "t8",   "t9",   "k0",   "k1",   "gp",   "sp",   "s8",   "ra"
1328 +  };
1329 + #endif
1330 +  printf("%s %s register %s\n",
1331 +                 transfer_size == SIZE_BYTE ? "byte" :
1332 +                 transfer_size == SIZE_WORD ? "word" :
1333 +                 transfer_size == SIZE_LONG ? "long" :
1334 +                 transfer_size == SIZE_QUAD ? "quad" : "unknown",
1335 +                 transfer_type == SIGSEGV_TRANSFER_LOAD ? "load to" : "store from",
1336 +                 mips_gpr_names[reg]);
1337 + #endif
1338 +
1339 +  *pc_p += 4;
1340 +  return true;
1341 + }
1342 + #endif
1343 +
1344 + // Decode and skip SPARC instruction
1345 + #if (defined(sparc) || defined(__sparc__))
1346 + enum {
1347 + #if (defined(__sun__))
1348 +  SPARC_REG_G1 = REG_G1,
1349 +  SPARC_REG_O0 = REG_O0,
1350 +  SPARC_REG_PC = REG_PC,
1351 +  SPARC_REG_nPC = REG_nPC
1352 + #endif
1353 + };
1354 + static bool sparc_skip_instruction(unsigned long * regs, gwindows_t * gwins, struct rwindow * rwin)
1355 + {
1356 +  unsigned int * pc = (unsigned int *)regs[SPARC_REG_PC];
1357 +
1358 +  if (pc == 0)
1359 +        return false;
1360 +
1361 + #if DEBUG
1362 +  printf("IP: %p [%08x]\n", pc, pc[0]);
1363 + #endif
1364 +
1365 +  transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
1366 +  transfer_size_t transfer_size = SIZE_LONG;
1367 +  bool register_pair = false;
1368 +
1369 +  const unsigned int opcode = pc[0];
1370 +  if ((opcode >> 30) != 3)
1371 +        return false;
1372 +  switch ((opcode >> 19) & 0x3f) {
1373 +  case 9: // Load Signed Byte
1374 +  case 1: // Load Unsigned Byte
1375 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1376 +        transfer_size = SIZE_BYTE;
1377 +        break;
1378 +  case 10:// Load Signed Halfword
1379 +  case 2: // Load Unsigned Word
1380 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1381 +        transfer_size = SIZE_WORD;
1382 +        break;
1383 +  case 8: // Load Word
1384 +  case 0: // Load Unsigned Word
1385 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1386 +        transfer_size = SIZE_LONG;
1387 +        break;
1388 +  case 11:// Load Extended Word
1389 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1390 +        transfer_size = SIZE_QUAD;
1391 +        break;
1392 +  case 3: // Load Doubleword
1393 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1394 +        transfer_size = SIZE_LONG;
1395 +        register_pair = true;
1396 +        break;
1397 +  case 5: // Store Byte
1398 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1399 +        transfer_size = SIZE_BYTE;
1400 +        break;
1401 +  case 6: // Store Halfword
1402 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1403 +        transfer_size = SIZE_WORD;
1404 +        break;
1405 +  case 4: // Store Word
1406 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1407 +        transfer_size = SIZE_LONG;
1408 +        break;
1409 +  case 14:// Store Extended Word
1410 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1411 +        transfer_size = SIZE_QUAD;
1412 +        break;
1413 +  case 7: // Store Doubleword
1414 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1415 +        transfer_size = SIZE_LONG;
1416 +        register_pair = true;
1417 +        break;
1418 +  }
1419 +
1420 +  if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
1421 +        // Unknown machine code, let it crash. Then patch the decoder
1422 +        return false;
1423 +  }
1424 +
1425 +  const int reg = (opcode >> 25) & 0x1f;
1426 +
1427 + #if DEBUG
1428 +  static const char * reg_names[] = {
1429 +        "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
1430 +        "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
1431 +        "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
1432 +        "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7"
1433 +  };
1434 +  printf("%s %s register %s\n",
1435 +                 transfer_size == SIZE_BYTE ? "byte" :
1436 +                 transfer_size == SIZE_WORD ? "word" :
1437 +                 transfer_size == SIZE_LONG ? "long" :
1438 +                 transfer_size == SIZE_QUAD ? "quad" : "unknown",
1439 +                 transfer_type == SIGSEGV_TRANSFER_LOAD ? "load to" : "store from",
1440 +                 reg_names[reg]);
1441 + #endif
1442 +
1443 +  // Zero target register in case of a load operation
1444 +  if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != 0) {
1445 +        // FIXME: code to handle local & input registers is not tested
1446 +        if (reg >= 1 && reg < 8) {
1447 +          // global registers
1448 +          regs[reg - 1 + SPARC_REG_G1] = 0;
1449 +        }
1450 +        else if (reg >= 8 && reg < 16) {
1451 +          // output registers
1452 +          regs[reg - 8 + SPARC_REG_O0] = 0;
1453 +        }
1454 +        else if (reg >= 16 && reg < 24) {
1455 +          // local registers (in register windows)
1456 +          if (gwins)
1457 +                gwins->wbuf->rw_local[reg - 16] = 0;
1458 +          else
1459 +                rwin->rw_local[reg - 16] = 0;
1460 +        }
1461 +        else {
1462 +          // input registers (in register windows)
1463 +          if (gwins)
1464 +                gwins->wbuf->rw_in[reg - 24] = 0;
1465 +          else
1466 +                rwin->rw_in[reg - 24] = 0;
1467 +        }
1468 +  }
1469 +
1470 +  regs[SPARC_REG_PC] += 4;
1471 +  regs[SPARC_REG_nPC] += 4;
1472 +  return true;
1473 + }
1474 + #endif
1475 + #endif
1476 +
1477 + // Decode and skip ARM instruction
1478 + #if (defined(arm) || defined(__arm__))
1479 + enum {
1480 + #if (defined(__linux__))
1481 +  ARM_REG_PC = 15,
1482 +  ARM_REG_CPSR = 16
1483 + #endif
1484 + };
1485 + static bool arm_skip_instruction(unsigned long * regs)
1486 + {
1487 +  unsigned int * pc = (unsigned int *)regs[ARM_REG_PC];
1488 +
1489 +  if (pc == 0)
1490 +        return false;
1491 +
1492 + #if DEBUG
1493 +  printf("IP: %p [%08x]\n", pc, pc[0]);
1494 + #endif
1495 +
1496 +  transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
1497 +  transfer_size_t transfer_size = SIZE_UNKNOWN;
1498 +  enum { op_sdt = 1, op_sdth = 2 };
1499 +  int op = 0;
1500 +
1501 +  // Handle load/store instructions only
1502 +  const unsigned int opcode = pc[0];
1503 +  switch ((opcode >> 25) & 7) {
1504 +  case 0: // Halfword and Signed Data Transfer (LDRH, STRH, LDRSB, LDRSH)
1505 +        op = op_sdth;
1506 +        // Determine transfer size (S/H bits)
1507 +        switch ((opcode >> 5) & 3) {
1508 +        case 0: // SWP instruction
1509 +          break;
1510 +        case 1: // Unsigned halfwords
1511 +        case 3: // Signed halfwords
1512 +          transfer_size = SIZE_WORD;
1513 +          break;
1514 +        case 2: // Signed byte
1515 +          transfer_size = SIZE_BYTE;
1516 +          break;
1517 +        }
1518 +        break;
1519 +  case 2:
1520 +  case 3: // Single Data Transfer (LDR, STR)
1521 +        op = op_sdt;
1522 +        // Determine transfer size (B bit)
1523 +        if (((opcode >> 22) & 1) == 1)
1524 +          transfer_size = SIZE_BYTE;
1525 +        else
1526 +          transfer_size = SIZE_LONG;
1527 +        break;
1528 +  default:
1529 +        // FIXME: support load/store mutliple?
1530 +        return false;
1531 +  }
1532 +
1533 +  // Check for invalid transfer size (SWP instruction?)
1534 +  if (transfer_size == SIZE_UNKNOWN)
1535 +        return false;
1536 +
1537 +  // Determine transfer type (L bit)
1538 +  if (((opcode >> 20) & 1) == 1)
1539 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1540 +  else
1541 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1542 +
1543 +  // Compute offset
1544 +  int offset;
1545 +  if (((opcode >> 25) & 1) == 0) {
1546 +        if (op == op_sdt)
1547 +          offset = opcode & 0xfff;
1548 +        else if (op == op_sdth) {
1549 +          int rm = opcode & 0xf;
1550 +          if (((opcode >> 22) & 1) == 0) {
1551 +                // register offset
1552 +                offset = regs[rm];
1553 +          }
1554 +          else {
1555 +                // immediate offset
1556 +                offset = ((opcode >> 4) & 0xf0) | (opcode & 0x0f);
1557 +          }
1558 +        }
1559 +  }
1560 +  else {
1561 +        const int rm = opcode & 0xf;
1562 +        const int sh = (opcode >> 7) & 0x1f;
1563 +        if (((opcode >> 4) & 1) == 1) {
1564 +          // we expect only legal load/store instructions
1565 +          printf("FATAL: invalid shift operand\n");
1566 +          return false;
1567 +        }
1568 +        const unsigned int v = regs[rm];
1569 +        switch ((opcode >> 5) & 3) {
1570 +        case 0: // logical shift left
1571 +          offset = sh ? v << sh : v;
1572 +          break;
1573 +        case 1: // logical shift right
1574 +          offset = sh ? v >> sh : 0;
1575 +          break;
1576 +        case 2: // arithmetic shift right
1577 +          if (sh)
1578 +                offset = ((signed int)v) >> sh;
1579 +          else
1580 +                offset = (v & 0x80000000) ? 0xffffffff : 0;
1581 +          break;
1582 +        case 3: // rotate right
1583 +          if (sh)
1584 +                offset = (v >> sh) | (v << (32 - sh));
1585 +          else
1586 +                offset = (v >> 1) | ((regs[ARM_REG_CPSR] << 2) & 0x80000000);
1587 +          break;
1588 +        }
1589 +  }
1590 +  if (((opcode >> 23) & 1) == 0)
1591 +        offset = -offset;
1592 +
1593 +  int rd = (opcode >> 12) & 0xf;
1594 +  int rn = (opcode >> 16) & 0xf;
1595 + #if DEBUG
1596 +  static const char * reg_names[] = {
1597 +        "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1598 +        "r9", "r9", "sl", "fp", "ip", "sp", "lr", "pc"
1599 +  };
1600 +  printf("%s %s register %s\n",
1601 +                 transfer_size == SIZE_BYTE ? "byte" :
1602 +                 transfer_size == SIZE_WORD ? "word" :
1603 +                 transfer_size == SIZE_LONG ? "long" : "unknown",
1604 +                 transfer_type == SIGSEGV_TRANSFER_LOAD ? "load to" : "store from",
1605 +                 reg_names[rd]);
1606 + #endif
1607 +
1608 +  unsigned int base = regs[rn];
1609 +  if (((opcode >> 24) & 1) == 1)
1610 +        base += offset;
1611 +
1612 +  if (transfer_type == SIGSEGV_TRANSFER_LOAD)
1613 +        regs[rd] = 0;
1614 +
1615 +  if (((opcode >> 24) & 1) == 0)                // post-index addressing
1616 +        regs[rn] += offset;
1617 +  else if (((opcode >> 21) & 1) == 1)   // write-back address into base
1618 +        regs[rn] = base;
1619 +
1620 +  regs[ARM_REG_PC] += 4;
1621 +  return true;
1622 + }
1623 + #endif
1624 +
1625 +
1626   // Fallbacks
1627 + #ifndef SIGSEGV_FAULT_ADDRESS_FAST
1628 + #define SIGSEGV_FAULT_ADDRESS_FAST              SIGSEGV_FAULT_ADDRESS
1629 + #endif
1630 + #ifndef SIGSEGV_FAULT_INSTRUCTION_FAST
1631 + #define SIGSEGV_FAULT_INSTRUCTION_FAST  SIGSEGV_FAULT_INSTRUCTION
1632 + #endif
1633   #ifndef SIGSEGV_FAULT_INSTRUCTION
1634 < #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_INVALID_PC
1634 > #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_INVALID_ADDRESS
1635   #endif
1636   #ifndef SIGSEGV_FAULT_HANDLER_ARGLIST_1
1637   #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 SIGSEGV_FAULT_HANDLER_ARGLIST
1638   #endif
1639   #ifndef SIGSEGV_FAULT_HANDLER_INVOKE
1640 < #define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP)  sigsegv_fault_handler(ADDR, IP)
1640 > #define SIGSEGV_FAULT_HANDLER_INVOKE(P) sigsegv_fault_handler(P)
1641   #endif
1642  
1643   // SIGSEGV recovery supported ?
# Line 825 | Line 1650 | static bool powerpc_skip_instruction(uns
1650   *  SIGSEGV global handler
1651   */
1652  
1653 < #if defined(HAVE_SIGSEGV_RECOVERY) || defined(HAVE_MACH_EXCEPTIONS)
1653 > struct sigsegv_info_t {
1654 >        sigsegv_address_t addr;
1655 >        sigsegv_address_t pc;
1656 > #ifdef HAVE_MACH_EXCEPTIONS
1657 >        mach_port_t thread;
1658 >        bool has_exc_state;
1659 >        SIGSEGV_EXCEPTION_STATE_TYPE exc_state;
1660 >        mach_msg_type_number_t exc_state_count;
1661 >        bool has_thr_state;
1662 >        SIGSEGV_THREAD_STATE_TYPE thr_state;
1663 >        mach_msg_type_number_t thr_state_count;
1664 > #endif
1665 > };
1666 >
1667 > // Return the address of the invalid memory reference
1668 > sigsegv_address_t sigsegv_get_fault_address(sigsegv_info_t *sip)
1669 > {
1670 > #ifdef HAVE_MACH_EXCEPTIONS
1671 >        static int use_fast_path = -1;
1672 >        if (use_fast_path != 1 && !sip->has_exc_state) {
1673 >                sip->exc_state_count = SIGSEGV_EXCEPTION_STATE_COUNT;
1674 >                kern_return_t krc = thread_get_state(sip->thread,
1675 >                                                                                         SIGSEGV_EXCEPTION_STATE_FLAVOR,
1676 >                                                                                         (natural_t *)&sip->exc_state,
1677 >                                                                                         &sip->exc_state_count);
1678 >                MACH_CHECK_ERROR(thread_get_state, krc);
1679 >                sip->has_exc_state = true;
1680 >
1681 >                sigsegv_address_t addr = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS;
1682 >                if (use_fast_path < 0)
1683 >                        use_fast_path = addr == sip->addr;
1684 >                sip->addr = addr;
1685 >        }
1686 > #endif
1687 >        return sip->addr;
1688 > }
1689 >
1690 > // Return the address of the instruction that caused the fault, or
1691 > // SIGSEGV_INVALID_ADDRESS if we could not retrieve this information
1692 > sigsegv_address_t sigsegv_get_fault_instruction_address(sigsegv_info_t *sip)
1693 > {
1694 > #ifdef HAVE_MACH_EXCEPTIONS
1695 >        if (!sip->has_thr_state) {
1696 >                sip->thr_state_count = SIGSEGV_THREAD_STATE_COUNT;
1697 >                kern_return_t krc = thread_get_state(sip->thread,
1698 >                                                                                         SIGSEGV_THREAD_STATE_FLAVOR,
1699 >                                                                                         (natural_t *)&sip->thr_state,
1700 >                                                                                         &sip->thr_state_count);
1701 >                MACH_CHECK_ERROR(thread_get_state, krc);
1702 >                sip->has_thr_state = true;
1703 >
1704 >                sip->pc = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION;
1705 >        }
1706 > #endif
1707 >        return sip->pc;
1708 > }
1709 >
1710   // This function handles the badaccess to memory.
1711   // It is called from the signal handler or the exception handler.
1712   static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST_1)
1713   {
1714 <        sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS;
1715 <        sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION;
1716 <        
1714 >        sigsegv_info_t si;
1715 >        si.addr = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS_FAST;
1716 >        si.pc = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION_FAST;
1717 > #ifdef HAVE_MACH_EXCEPTIONS
1718 >        si.thread = thread;
1719 >        si.has_exc_state = false;
1720 >        si.has_thr_state = false;
1721 > #endif
1722 >        sigsegv_info_t * const sip = &si;
1723 >
1724          // Call user's handler and reinstall the global handler, if required
1725 <        switch (SIGSEGV_FAULT_HANDLER_INVOKE(fault_address, fault_instruction)) {
1725 >        switch (SIGSEGV_FAULT_HANDLER_INVOKE(sip)) {
1726          case SIGSEGV_RETURN_SUCCESS:
1727                  return true;
1728  
# Line 848 | Line 1736 | static bool handle_badaccess(SIGSEGV_FAU
1736                          // is modified off of the stack, in Mach we
1737                          // need to actually call thread_set_state to
1738                          // have the register values updated.
1739 <                        kern_return_t krc;
1740 <
1741 <                        krc = thread_set_state(thread,
1742 <                                                                   MACHINE_THREAD_STATE, (thread_state_t)state,
1743 <                                                                   MACHINE_THREAD_STATE_COUNT);
856 <                        MACH_CHECK_ERROR (thread_get_state, krc);
1739 >                        kern_return_t krc = thread_set_state(sip->thread,
1740 >                                                                                                 SIGSEGV_THREAD_STATE_FLAVOR,
1741 >                                                                                                 (natural_t *)&sip->thr_state,
1742 >                                                                                                 sip->thr_state_count);
1743 >                        MACH_CHECK_ERROR(thread_set_state, krc);
1744   #endif
1745                          return true;
1746                  }
1747                  break;
1748   #endif
1749 +        case SIGSEGV_RETURN_FAILURE:
1750 +                // We can't do anything with the fault_address, dump state?
1751 +                if (sigsegv_state_dumper != 0)
1752 +                        sigsegv_state_dumper(sip);
1753 +                break;
1754          }
863        
864        // We can't do anything with the fault_address, dump state?
865        if (sigsegv_state_dumper != 0)
866                sigsegv_state_dumper(fault_address, fault_instruction);
1755  
1756          return false;
1757   }
870 #endif
1758  
1759  
1760   /*
# Line 904 | Line 1791 | forward_exception(mach_port_t thread_por
1791          mach_port_t port;
1792          exception_behavior_t behavior;
1793          thread_state_flavor_t flavor;
1794 <        thread_state_t thread_state;
1794 >        thread_state_data_t thread_state;
1795          mach_msg_type_number_t thread_state_count;
1796  
1797          for (portIndex = 0; portIndex < oldExceptionPorts->maskCount; portIndex++) {
# Line 923 | Line 1810 | forward_exception(mach_port_t thread_por
1810          behavior = oldExceptionPorts->behaviors[portIndex];
1811          flavor = oldExceptionPorts->flavors[portIndex];
1812  
1813 +        if (!VALID_THREAD_STATE_FLAVOR(flavor)) {
1814 +                fprintf(stderr, "Invalid thread_state flavor = %d. Not forwarding\n", flavor);
1815 +                return KERN_FAILURE;
1816 +        }
1817 +
1818          /*
1819           fprintf(stderr, "forwarding exception, port = 0x%x, behaviour = %d, flavor = %d\n", port, behavior, flavor);
1820           */
1821  
1822          if (behavior != EXCEPTION_DEFAULT) {
1823                  thread_state_count = THREAD_STATE_MAX;
1824 <                kret = thread_get_state (thread_port, flavor, thread_state,
1824 >                kret = thread_get_state (thread_port, flavor, (natural_t *)&thread_state,
1825                                                                   &thread_state_count);
1826                  MACH_CHECK_ERROR (thread_get_state, kret);
1827          }
# Line 945 | Line 1837 | forward_exception(mach_port_t thread_por
1837            // fprintf(stderr, "forwarding to exception_raise_state\n");
1838            kret = exception_raise_state(port, exception_type, exception_data,
1839                                                                     data_count, &flavor,
1840 <                                                                   thread_state, thread_state_count,
1841 <                                                                   thread_state, &thread_state_count);
1840 >                                                                   (natural_t *)&thread_state, thread_state_count,
1841 >                                                                   (natural_t *)&thread_state, &thread_state_count);
1842            MACH_CHECK_ERROR (exception_raise_state, kret);
1843            break;
1844          case EXCEPTION_STATE_IDENTITY:
# Line 954 | Line 1846 | forward_exception(mach_port_t thread_por
1846            kret = exception_raise_state_identity(port, thread_port, task_port,
1847                                                                                          exception_type, exception_data,
1848                                                                                          data_count, &flavor,
1849 <                                                                                        thread_state, thread_state_count,
1850 <                                                                                        thread_state, &thread_state_count);
1849 >                                                                                        (natural_t *)&thread_state, thread_state_count,
1850 >                                                                                        (natural_t *)&thread_state, &thread_state_count);
1851            MACH_CHECK_ERROR (exception_raise_state_identity, kret);
1852            break;
1853          default:
1854            fprintf(stderr, "forward_exception got unknown behavior\n");
1855 +          kret = KERN_FAILURE;
1856            break;
1857          }
1858  
1859          if (behavior != EXCEPTION_DEFAULT) {
1860 <                kret = thread_set_state (thread_port, flavor, thread_state,
1860 >                kret = thread_set_state (thread_port, flavor, (natural_t *)&thread_state,
1861                                                                   thread_state_count);
1862                  MACH_CHECK_ERROR (thread_set_state, kret);
1863          }
1864  
1865 <        return KERN_SUCCESS;
1865 >        return kret;
1866   }
1867  
1868   /*
# Line 997 | Line 1890 | catch_exception_raise(mach_port_t except
1890                                            mach_port_t task,
1891                                            exception_type_t exception,
1892                                            exception_data_t code,
1893 <                                          mach_msg_type_number_t codeCount)
1893 >                                          mach_msg_type_number_t code_count)
1894   {
1002        ppc_thread_state_t state;
1895          kern_return_t krc;
1896  
1897 <        if ((exception == EXC_BAD_ACCESS)  && (codeCount >= 2)) {
1898 <                if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS))
1899 <                        return KERN_SUCCESS;
1897 >        if (exception == EXC_BAD_ACCESS) {
1898 >                switch (code[0]) {
1899 >                case KERN_PROTECTION_FAILURE:
1900 >                case KERN_INVALID_ADDRESS:
1901 >                        if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS))
1902 >                                return KERN_SUCCESS;
1903 >                        break;
1904 >                }
1905          }
1906  
1907          // In Mach we do not need to remove the exception handler.
1908          // If we forward the exception, eventually some exception handler
1909          // will take care of this exception.
1910 <        krc = forward_exception(thread, task, exception, code, codeCount, &ports);
1910 >        krc = forward_exception(thread, task, exception, code, code_count, &ports);
1911  
1912          return krc;
1913   }
# Line 1138 | Line 2035 | static bool sigsegv_do_install_handler(s
2035          // addressing modes) used in PPC instructions, you will need the
2036          // GPR state anyway.
2037          krc = thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, _exceptionPort,
2038 <                                EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
2038 >                                EXCEPTION_DEFAULT, SIGSEGV_THREAD_STATE_FLAVOR);
2039          if (krc != KERN_SUCCESS) {
2040                  mach_error("thread_set_exception_ports", krc);
2041                  return false;
# Line 1161 | Line 2058 | static bool sigsegv_do_install_handler(s
2058   }
2059   #endif
2060  
2061 + #ifdef HAVE_WIN32_EXCEPTIONS
2062 + static LONG WINAPI main_exception_filter(EXCEPTION_POINTERS *ExceptionInfo)
2063 + {
2064 +        if (sigsegv_fault_handler != NULL
2065 +                && ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION
2066 +                && ExceptionInfo->ExceptionRecord->NumberParameters == 2
2067 +                && handle_badaccess(ExceptionInfo))
2068 +                return EXCEPTION_CONTINUE_EXECUTION;
2069 +
2070 +        return EXCEPTION_CONTINUE_SEARCH;
2071 + }
2072 +
2073 + #if defined __CYGWIN__ && defined __i386__
2074 + /* In Cygwin programs, SetUnhandledExceptionFilter has no effect because Cygwin
2075 +   installs a global exception handler.  We have to dig deep in order to install
2076 +   our main_exception_filter.  */
2077 +
2078 + /* Data structures for the current thread's exception handler chain.
2079 +   On the x86 Windows uses register fs, offset 0 to point to the current
2080 +   exception handler; Cygwin mucks with it, so we must do the same... :-/ */
2081 +
2082 + /* Magic taken from winsup/cygwin/include/exceptions.h.  */
2083 +
2084 + struct exception_list {
2085 +    struct exception_list *prev;
2086 +    int (*handler) (EXCEPTION_RECORD *, void *, CONTEXT *, void *);
2087 + };
2088 + typedef struct exception_list exception_list;
2089 +
2090 + /* Magic taken from winsup/cygwin/exceptions.cc.  */
2091 +
2092 + __asm__ (".equ __except_list,0");
2093 +
2094 + extern exception_list *_except_list __asm__ ("%fs:__except_list");
2095 +
2096 + /* For debugging.  _except_list is not otherwise accessible from gdb.  */
2097 + static exception_list *
2098 + debug_get_except_list ()
2099 + {
2100 +  return _except_list;
2101 + }
2102 +
2103 + /* Cygwin's original exception handler.  */
2104 + static int (*cygwin_exception_handler) (EXCEPTION_RECORD *, void *, CONTEXT *, void *);
2105 +
2106 + /* Our exception handler.  */
2107 + static int
2108 + libsigsegv_exception_handler (EXCEPTION_RECORD *exception, void *frame, CONTEXT *context, void *dispatch)
2109 + {
2110 +  EXCEPTION_POINTERS ExceptionInfo;
2111 +  ExceptionInfo.ExceptionRecord = exception;
2112 +  ExceptionInfo.ContextRecord = context;
2113 +  if (main_exception_filter (&ExceptionInfo) == EXCEPTION_CONTINUE_SEARCH)
2114 +    return cygwin_exception_handler (exception, frame, context, dispatch);
2115 +  else
2116 +    return 0;
2117 + }
2118 +
2119 + static void
2120 + do_install_main_exception_filter ()
2121 + {
2122 +  /* We cannot insert any handler into the chain, because such handlers
2123 +     must lie on the stack (?).  Instead, we have to replace(!) Cygwin's
2124 +     global exception handler.  */
2125 +  cygwin_exception_handler = _except_list->handler;
2126 +  _except_list->handler = libsigsegv_exception_handler;
2127 + }
2128 +
2129 + #else
2130 +
2131 + static void
2132 + do_install_main_exception_filter ()
2133 + {
2134 +  SetUnhandledExceptionFilter ((LPTOP_LEVEL_EXCEPTION_FILTER) &main_exception_filter);
2135 + }
2136 + #endif
2137 +
2138 + static bool sigsegv_do_install_handler(sigsegv_fault_handler_t handler)
2139 + {
2140 +        static bool main_exception_filter_installed = false;
2141 +        if (!main_exception_filter_installed) {
2142 +                do_install_main_exception_filter();
2143 +                main_exception_filter_installed = true;
2144 +        }
2145 +        sigsegv_fault_handler = handler;
2146 +        return true;
2147 + }
2148 + #endif
2149 +
2150   bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
2151   {
2152   #if defined(HAVE_SIGSEGV_RECOVERY)
# Line 1171 | Line 2157 | bool sigsegv_install_handler(sigsegv_fau
2157          if (success)
2158              sigsegv_fault_handler = handler;
2159          return success;
2160 < #elif defined(HAVE_MACH_EXCEPTIONS)
2160 > #elif defined(HAVE_MACH_EXCEPTIONS) || defined(HAVE_WIN32_EXCEPTIONS)
2161          return sigsegv_do_install_handler(handler);
2162   #else
2163          // FAIL: no siginfo_t nor sigcontext subterfuge is available
# Line 1197 | Line 2183 | void sigsegv_deinstall_handler(void)
2183          SIGSEGV_ALL_SIGNALS
2184   #undef FAULT_HANDLER
2185   #endif
2186 + #ifdef HAVE_WIN32_EXCEPTIONS
2187 +        sigsegv_fault_handler = NULL;
2188 + #endif
2189   }
2190  
2191  
# Line 1218 | Line 2207 | void sigsegv_set_dump_state(sigsegv_stat
2207   #include <stdio.h>
2208   #include <stdlib.h>
2209   #include <fcntl.h>
2210 + #ifdef HAVE_SYS_MMAN_H
2211   #include <sys/mman.h>
2212 + #endif
2213   #include "vm_alloc.h"
2214  
2215   const int REF_INDEX = 123;
# Line 1228 | Line 2219 | static int page_size;
2219   static volatile char * page = 0;
2220   static volatile int handler_called = 0;
2221  
2222 + /* Barriers */
2223 + #ifdef __GNUC__
2224 + #define BARRIER() asm volatile ("" : : : "memory")
2225 + #else
2226 + #define BARRIER() /* nothing */
2227 + #endif
2228 +
2229   #ifdef __GNUC__
2230   // Code range where we expect the fault to come from
2231   static void *b_region, *e_region;
2232   #endif
2233  
2234 < static sigsegv_return_t sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
2234 > static sigsegv_return_t sigsegv_test_handler(sigsegv_info_t *sip)
2235   {
2236 +        const sigsegv_address_t fault_address = sigsegv_get_fault_address(sip);
2237 +        const sigsegv_address_t instruction_address = sigsegv_get_fault_instruction_address(sip);
2238 + #if DEBUG
2239 +        printf("sigsegv_test_handler(%p, %p)\n", fault_address, instruction_address);
2240 +        printf("expected fault at %p\n", page + REF_INDEX);
2241 + #ifdef __GNUC__
2242 +        printf("expected instruction address range: %p-%p\n", b_region, e_region);
2243 + #endif
2244 + #endif
2245          handler_called++;
2246          if ((fault_address - REF_INDEX) != page)
2247                  exit(10);
2248   #ifdef __GNUC__
2249          // Make sure reported fault instruction address falls into
2250          // expected code range
2251 <        if (instruction_address != SIGSEGV_INVALID_PC
2251 >        if (instruction_address != SIGSEGV_INVALID_ADDRESS
2252                  && ((instruction_address <  (sigsegv_address_t)b_region) ||
2253                          (instruction_address >= (sigsegv_address_t)e_region)))
2254                  exit(11);
# Line 1252 | Line 2259 | static sigsegv_return_t sigsegv_test_han
2259   }
2260  
2261   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
2262 < static sigsegv_return_t sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
2262 > static sigsegv_return_t sigsegv_insn_handler(sigsegv_info_t *sip)
2263   {
2264 +        const sigsegv_address_t fault_address = sigsegv_get_fault_address(sip);
2265 +        const sigsegv_address_t instruction_address = sigsegv_get_fault_instruction_address(sip);
2266 + #if DEBUG
2267 +        printf("sigsegv_insn_handler(%p, %p)\n", fault_address, instruction_address);
2268 + #endif
2269          if (((unsigned long)fault_address - (unsigned long)page) < page_size) {
2270   #ifdef __GNUC__
2271                  // Make sure reported fault instruction address falls into
2272                  // expected code range
2273 <                if (instruction_address != SIGSEGV_INVALID_PC
2273 >                if (instruction_address != SIGSEGV_INVALID_ADDRESS
2274                          && ((instruction_address <  (sigsegv_address_t)b_region) ||
2275                                  (instruction_address >= (sigsegv_address_t)e_region)))
2276                          return SIGSEGV_RETURN_FAILURE;
# Line 1268 | Line 2280 | static sigsegv_return_t sigsegv_insn_han
2280  
2281          return SIGSEGV_RETURN_FAILURE;
2282   }
2283 +
2284 + // More sophisticated tests for instruction skipper
2285 + static bool arch_insn_skipper_tests()
2286 + {
2287 + #if (defined(i386) || defined(__i386__)) || defined(__x86_64__)
2288 +        static const unsigned char code[] = {
2289 +                0x8a, 0x00,                    // mov    (%eax),%al
2290 +                0x8a, 0x2c, 0x18,              // mov    (%eax,%ebx,1),%ch
2291 +                0x88, 0x20,                    // mov    %ah,(%eax)
2292 +                0x88, 0x08,                    // mov    %cl,(%eax)
2293 +                0x66, 0x8b, 0x00,              // mov    (%eax),%ax
2294 +                0x66, 0x8b, 0x0c, 0x18,        // mov    (%eax,%ebx,1),%cx
2295 +                0x66, 0x89, 0x00,              // mov    %ax,(%eax)
2296 +                0x66, 0x89, 0x0c, 0x18,        // mov    %cx,(%eax,%ebx,1)
2297 +                0x8b, 0x00,                    // mov    (%eax),%eax
2298 +                0x8b, 0x0c, 0x18,              // mov    (%eax,%ebx,1),%ecx
2299 +                0x89, 0x00,                    // mov    %eax,(%eax)
2300 +                0x89, 0x0c, 0x18,              // mov    %ecx,(%eax,%ebx,1)
2301 + #if defined(__x86_64__)
2302 +                0x44, 0x8a, 0x00,              // mov    (%rax),%r8b
2303 +                0x44, 0x8a, 0x20,              // mov    (%rax),%r12b
2304 +                0x42, 0x8a, 0x3c, 0x10,        // mov    (%rax,%r10,1),%dil
2305 +                0x44, 0x88, 0x00,              // mov    %r8b,(%rax)
2306 +                0x44, 0x88, 0x20,              // mov    %r12b,(%rax)
2307 +                0x42, 0x88, 0x3c, 0x10,        // mov    %dil,(%rax,%r10,1)
2308 +                0x66, 0x44, 0x8b, 0x00,        // mov    (%rax),%r8w
2309 +                0x66, 0x42, 0x8b, 0x0c, 0x10,  // mov    (%rax,%r10,1),%cx
2310 +                0x66, 0x44, 0x89, 0x00,        // mov    %r8w,(%rax)
2311 +                0x66, 0x42, 0x89, 0x0c, 0x10,  // mov    %cx,(%rax,%r10,1)
2312 +                0x44, 0x8b, 0x00,              // mov    (%rax),%r8d
2313 +                0x42, 0x8b, 0x0c, 0x10,        // mov    (%rax,%r10,1),%ecx
2314 +                0x44, 0x89, 0x00,              // mov    %r8d,(%rax)
2315 +                0x42, 0x89, 0x0c, 0x10,        // mov    %ecx,(%rax,%r10,1)
2316 +                0x48, 0x8b, 0x08,              // mov    (%rax),%rcx
2317 +                0x4c, 0x8b, 0x18,              // mov    (%rax),%r11
2318 +                0x4a, 0x8b, 0x0c, 0x10,        // mov    (%rax,%r10,1),%rcx
2319 +                0x4e, 0x8b, 0x1c, 0x10,        // mov    (%rax,%r10,1),%r11
2320 +                0x48, 0x89, 0x08,              // mov    %rcx,(%rax)
2321 +                0x4c, 0x89, 0x18,              // mov    %r11,(%rax)
2322 +                0x4a, 0x89, 0x0c, 0x10,        // mov    %rcx,(%rax,%r10,1)
2323 +                0x4e, 0x89, 0x1c, 0x10,        // mov    %r11,(%rax,%r10,1)
2324 +                0x63, 0x47, 0x04,              // movslq 4(%rdi),%eax
2325 +                0x48, 0x63, 0x47, 0x04,        // movslq 4(%rdi),%rax
2326 + #endif
2327 +                0                              // end
2328 +        };
2329 +        const int N_REGS = 20;
2330 +        unsigned long regs[N_REGS];
2331 +        for (int i = 0; i < N_REGS; i++)
2332 +                regs[i] = i;
2333 +        const unsigned long start_code = (unsigned long)&code;
2334 +        regs[X86_REG_EIP] = start_code;
2335 +        while ((regs[X86_REG_EIP] - start_code) < (sizeof(code) - 1)
2336 +                   && ix86_skip_instruction(regs))
2337 +                ; /* simply iterate */
2338 +        return (regs[X86_REG_EIP] - start_code) == (sizeof(code) - 1);
2339 + #endif
2340 +        return true;
2341 + }
2342   #endif
2343  
2344   int main(void)
# Line 1275 | Line 2346 | int main(void)
2346          if (vm_init() < 0)
2347                  return 1;
2348  
2349 <        page_size = getpagesize();
2349 >        page_size = vm_get_page_size();
2350          if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED)
2351                  return 2;
2352          
# Line 1295 | Line 2366 | int main(void)
2366          if (page[REF_INDEX] != REF_VALUE)
2367            exit(20);
2368          page[REF_INDEX] = REF_VALUE;
2369 +        BARRIER();
2370   L_e_region1:
2371  
2372          if (handler_called != 1)
# Line 1314 | Line 2386 | int main(void)
2386                  return 8;
2387          
2388   #define TEST_SKIP_INSTRUCTION(TYPE) do {                                \
2389 <                const unsigned int TAG = 0x12345678;                    \
2389 >                const unsigned long TAG = 0x12345678 |                  \
2390 >                (sizeof(long) == 8 ? 0x9abcdef0UL << 31 : 0);   \
2391                  TYPE data = *((TYPE *)(page + sizeof(TYPE)));   \
2392 <                volatile unsigned int effect = data + TAG;              \
2392 >                volatile unsigned long effect = data + TAG;             \
2393                  if (effect != TAG)                                                              \
2394                          return 9;                                                                       \
2395          } while (0)
# Line 1329 | Line 2402 | int main(void)
2402          TEST_SKIP_INSTRUCTION(unsigned char);
2403          TEST_SKIP_INSTRUCTION(unsigned short);
2404          TEST_SKIP_INSTRUCTION(unsigned int);
2405 +        TEST_SKIP_INSTRUCTION(unsigned long);
2406 +        TEST_SKIP_INSTRUCTION(signed char);
2407 +        TEST_SKIP_INSTRUCTION(signed short);
2408 +        TEST_SKIP_INSTRUCTION(signed int);
2409 +        TEST_SKIP_INSTRUCTION(signed long);
2410 +        BARRIER();
2411   L_e_region2:
2412 +
2413 +        if (!arch_insn_skipper_tests())
2414 +                return 20;
2415   #endif
2416  
2417          vm_exit();
2418          return 0;
2419   }
2420   #endif
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines