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.17 by gbeauche, 2002-05-20T17:49:04Z vs.
Revision 1.68 by gbeauche, 2007-12-30T12:11:17Z

# Line 4 | Line 4
4   *  Derived from Bruno Haible's work on his SIGSEGV library for clisp
5   *  <http://clisp.sourceforge.net/>
6   *
7 < *  Basilisk II (C) 1997-2002 Christian Bauer
7 > *  MacOS X support derived from the post by Timothy J. Wood to the
8 > *  omnigroup macosx-dev list:
9 > *    Mach Exception Handlers 101 (Was Re: ptrace, gdb)
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-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 29 | Line 35
35   #include "config.h"
36   #endif
37  
38 + #include <list>
39 + #include <stdio.h>
40   #include <signal.h>
41   #include "sigsegv.h"
42  
43 + #ifndef NO_STD_NAMESPACE
44 + using std::list;
45 + #endif
46 +
47   // Return value type of a signal handler (standard type if not defined)
48   #ifndef RETSIGTYPE
49   #define RETSIGTYPE void
# Line 40 | Line 52
52   // Type of the system signal handler
53   typedef RETSIGTYPE (*signal_handler)(int);
54  
43 // Is the fault to be ignored?
44 static bool sigsegv_ignore_fault = false;
45
55   // User's SIGSEGV handler
56   static sigsegv_fault_handler_t sigsegv_fault_handler = 0;
57  
# Line 59 | Line 68 | static bool sigsegv_do_install_handler(i
68  
69   // Transfer type
70   enum transfer_type_t {
71 <        TYPE_UNKNOWN,
72 <        TYPE_LOAD,
73 <        TYPE_STORE
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  
85   #if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__))
# Line 91 | 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 103 | Line 113 | static void powerpc_decode_instruction(i
113          signed int imm = (signed short)(opcode & 0xffff);
114          
115          // Analyze opcode
116 <        transfer_type_t transfer_type = TYPE_UNKNOWN;
116 >        transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
117          transfer_size_t transfer_size = SIZE_UNKNOWN;
118          addressing_mode_t addr_mode = MODE_UNKNOWN;
119          switch (primop) {
120          case 31:
121                  switch (exop) {
122                  case 23:        // lwzx
123 <                        transfer_type = TYPE_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_X; break;
123 >                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_X; break;
124                  case 55:        // lwzux
125 <                        transfer_type = TYPE_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break;
125 >                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break;
126                  case 87:        // lbzx
127 <                        transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
127 >                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
128                  case 119:       // lbzux
129 <                        transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
129 >                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
130                  case 151:       // stwx
131 <                        transfer_type = TYPE_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_X; break;
131 >                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_X; break;
132                  case 183:       // stwux
133 <                        transfer_type = TYPE_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break;
133 >                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break;
134                  case 215:       // stbx
135 <                        transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
135 >                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
136                  case 247:       // stbux
137 <                        transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
137 >                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
138                  case 279:       // lhzx
139 <                        transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
139 >                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
140                  case 311:       // lhzux
141 <                        transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
141 >                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
142                  case 343:       // lhax
143 <                        transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
143 >                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
144                  case 375:       // lhaux
145 <                        transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
145 >                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
146                  case 407:       // sthx
147 <                        transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
147 >                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
148                  case 439:       // sthux
149 <                        transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
149 >                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
150                  }
151                  break;
152          
153          case 32:        // lwz
154 <                transfer_type = TYPE_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break;
154 >                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break;
155          case 33:        // lwzu
156 <                transfer_type = TYPE_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_U; break;
156 >                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_U; break;
157          case 34:        // lbz
158 <                transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
158 >                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
159          case 35:        // lbzu
160 <                transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
160 >                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
161          case 36:        // stw
162 <                transfer_type = TYPE_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break;
162 >                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break;
163          case 37:        // stwu
164 <                transfer_type = TYPE_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_U; break;
164 >                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_U; break;
165          case 38:        // stb
166 <                transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
166 >                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
167          case 39:        // stbu
168 <                transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
168 >                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
169          case 40:        // lhz
170 <                transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
170 >                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
171          case 41:        // lhzu
172 <                transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
172 >                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
173          case 42:        // lha
174 <                transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
174 >                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
175          case 43:        // lhau
176 <                transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
176 >                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
177          case 44:        // sth
178 <                transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
178 >                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
179          case 45:        // sthu
180 <                transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
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 208 | 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)
237   #endif
238   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, siginfo_t *sip, void *scp
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__) || 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))
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__))
303 > #include <sys/ucontext.h>
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__))
# Line 235 | 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 248 | Line 344 | static void powerpc_decode_instruction(i
344   #if (defined(i386) || defined(__i386__))
345   #include <asm/sigcontext.h>
346   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, struct sigcontext scs
347 < #define SIGSEGV_FAULT_ADDRESS                   scs.cr2
348 < #define SIGSEGV_FAULT_INSTRUCTION               scs.eip
349 < #define SIGSEGV_REGISTER_FILE                   (unsigned int *)(&scs)
347 > #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 struct sigcontext *scp
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 long *)scp
352   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
353   #endif
354   #if (defined(sparc) || defined(__sparc__))
355   #include <asm/sigcontext.h>
356   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp, char *addr
357 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp, addr
358   #define SIGSEGV_FAULT_ADDRESS                   addr
359   #endif
360   #if (defined(powerpc) || defined(__powerpc__))
361   #include <asm/sigcontext.h>
362   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, struct sigcontext *scp
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__))
370   #include <asm/sigcontext.h>
371   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
372 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
373   #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
374   #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_pc
375 <
376 < // From Boehm's GC 6.0alpha8
377 < static sigsegv_address_t get_fault_address(struct sigcontext *scp)
378 < {
379 <        unsigned int instruction = *((unsigned int *)(scp->sc_pc));
380 <        unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f];
381 <        fault_address += (signed long)(signed short)(instruction & 0xffff);
382 <        return (sigsegv_address_t)fault_address;
383 < }
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_ADDRESS                   scp->sc_badvaddr
391 > #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
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  
397   // HP-UX
398   #if (defined(hpux) || defined(__hpux__))
399   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
400 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
401   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_sl.sl_ss.ss_narrow.ss_cr21
402   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS)
403   #endif
# Line 302 | Line 406 | static sigsegv_address_t get_fault_addre
406   #if defined(__osf__)
407   #include <ucontext.h>
408   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
409 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
410   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_traparg_a0
411   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
412   #endif
# Line 309 | Line 414 | static sigsegv_address_t get_fault_addre
414   // AIX
415   #if defined(_AIX)
416   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
417 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
418   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_jmpbuf.jmp_context.o_vaddr
419   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
420   #endif
421  
422 < // NetBSD or FreeBSD
423 < #if defined(__NetBSD__) || defined(__FreeBSD__)
422 > // NetBSD
423 > #if defined(__NetBSD__)
424   #if (defined(m68k) || defined(__m68k__))
425   #include <m68k/frame.h>
426   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
427 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
428   #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
429   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
430  
# Line 341 | Line 448 | static sigsegv_address_t get_fault_addre
448          }
449          return (sigsegv_address_t)fault_addr;
450   }
451 < #else
452 < #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, void *scp, char *addr
453 < #define SIGSEGV_FAULT_ADDRESS                   addr
451 > #endif
452 > #if (defined(alpha) || defined(__alpha__))
453 > #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
454 > #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
455 > #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
456 > #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
457 > #endif
458 > #if (defined(i386) || defined(__i386__))
459 > #error "FIXME: need to decode instruction and compute EA"
460 > #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
461 > #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
462 > #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
463 > #endif
464 > #endif
465 > #if defined(__FreeBSD__)
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 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 < // MacOS X
484 > // Extract fault address out of a sigcontext
485 > #if (defined(alpha) || defined(__alpha__))
486 > // From Boehm's GC 6.0alpha8
487 > static sigsegv_address_t get_fault_address(struct sigcontext *scp)
488 > {
489 >        unsigned int instruction = *((unsigned int *)(scp->sc_pc));
490 >        unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f];
491 >        fault_address += (signed long)(signed short)(instruction & 0xffff);
492 >        return (sigsegv_address_t)fault_address;
493 > }
494 > #endif
495 >
496 >
497 > // MacOS X, not sure which version this works in. Under 10.1
498 > // vm_protect does not appear to work from a signal handler. Under
499 > // 10.2 signal handlers get siginfo type arguments but the si_addr
500 > // field is the address of the faulting instruction and not the
501 > // address that caused the SIGBUS. Maybe this works in 10.0? In any
502 > // case with Mach exception handlers there is a way to do what this
503 > // was meant to do.
504 > #ifndef HAVE_MACH_EXCEPTIONS
505   #if defined(__APPLE__) && defined(__MACH__)
506   #if (defined(ppc) || defined(__ppc__))
507   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
508 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
509   #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
510   #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_ir
511   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
# Line 371 | Line 525 | static sigsegv_address_t get_fault_addre
525   #endif
526   #endif
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
547 + // uses HURD (oops GNU/HURD), Darwin/x86, NextStep, Rhapsody, or CMU
548 + // Mach 2.5/3.0?
549 + #if defined(__APPLE__) && defined(__MACH__)
550 +
551 + #include <sys/types.h>
552 + #include <stdlib.h>
553 + #include <stdio.h>
554 + #include <pthread.h>
555 +
556 + /*
557 + * If you are familiar with MIG then you will understand the frustration
558 + * that was necessary to get these embedded into C++ code by hand.
559 + */
560 + extern "C" {
561 + #include <mach/mach.h>
562 + #include <mach/mach_error.h>
563 +
564 + extern boolean_t exc_server(mach_msg_header_t *, mach_msg_header_t *);
565 + extern kern_return_t catch_exception_raise(mach_port_t, mach_port_t,
566 +        mach_port_t, exception_type_t, exception_data_t, mach_msg_type_number_t);
567 + extern kern_return_t exception_raise(mach_port_t, mach_port_t, mach_port_t,
568 +        exception_type_t, exception_data_t, mach_msg_type_number_t);
569 + extern kern_return_t exception_raise_state(mach_port_t, exception_type_t,
570 +        exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
571 +        thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
572 + extern kern_return_t exception_raise_state_identity(mach_port_t, mach_port_t, mach_port_t,
573 +        exception_type_t, exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
574 +        thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
575 + }
576 +
577 + // Could make this dynamic by looking for a result of MIG_ARRAY_TOO_LARGE
578 + #define HANDLER_COUNT 64
579 +
580 + // structure to tuck away existing exception handlers
581 + typedef struct _ExceptionPorts {
582 +        mach_msg_type_number_t maskCount;
583 +        exception_mask_t masks[HANDLER_COUNT];
584 +        exception_handler_t handlers[HANDLER_COUNT];
585 +        exception_behavior_t behaviors[HANDLER_COUNT];
586 +        thread_state_flavor_t flavors[HANDLER_COUNT];
587 + } ExceptionPorts;
588 +
589 + // exception handler thread
590 + static pthread_t exc_thread;
591 +
592 + // place where old exception handler info is stored
593 + static ExceptionPorts ports;
594 +
595 + // our exception port
596 + static mach_port_t _exceptionPort = MACH_PORT_NULL;
597 +
598 + #define MACH_CHECK_ERROR(name,ret) \
599 + if (ret != KERN_SUCCESS) { \
600 +        mach_error(#name, ret); \
601 +        exit (1); \
602 + }
603 +
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                   (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.
647 + #define MSG_SIZE 512
648 + static char msgbuf[MSG_SIZE];
649 + static char replybuf[MSG_SIZE];
650 +
651 + /*
652 + * This is the entry point for the exception handler thread. The job
653 + * of this thread is to wait for exception messages on the exception
654 + * port that was setup beforehand and to pass them on to exc_server.
655 + * exc_server is a MIG generated function that is a part of Mach.
656 + * Its job is to decide what to do with the exception message. In our
657 + * case exc_server calls catch_exception_raise on our behalf. After
658 + * exc_server returns, it is our responsibility to send the reply.
659 + */
660 + static void *
661 + handleExceptions(void *priv)
662 + {
663 +        mach_msg_header_t *msg, *reply;
664 +        kern_return_t krc;
665 +
666 +        msg = (mach_msg_header_t *)msgbuf;
667 +        reply = (mach_msg_header_t *)replybuf;
668 +
669 +        for (;;) {
670 +                krc = mach_msg(msg, MACH_RCV_MSG, MSG_SIZE, MSG_SIZE,
671 +                                _exceptionPort, 0, MACH_PORT_NULL);
672 +                MACH_CHECK_ERROR(mach_msg, krc);
673 +
674 +                if (!exc_server(msg, reply)) {
675 +                        fprintf(stderr, "exc_server hated the message\n");
676 +                        exit(1);
677 +                }
678 +
679 +                krc = mach_msg(reply, MACH_SEND_MSG, reply->msgh_size, 0,
680 +                                 msg->msgh_local_port, 0, MACH_PORT_NULL);
681 +                if (krc != KERN_SUCCESS) {
682 +                        fprintf(stderr, "Error sending message to original reply port, krc = %d, %s",
683 +                                krc, mach_error_string(krc));
684 +                        exit(1);
685 +                }
686 +        }
687 + }
688 + #endif
689 + #endif
690  
691  
692   /*
# Line 379 | Line 695 | static sigsegv_address_t get_fault_addre
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 391 | 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(__NetBSD__) || defined(__FreeBSD__)
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(__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 404 | 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 440 | 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 <        transfer_type_t transfer_type = TYPE_UNKNOWN;
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 460 | 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 <            if (eip[1] == 0xb7) { // MOVZX r32, r/m16
970 <                switch (eip[2] & 0xc0) {
971 <                case 0x80:
972 <                    reg = (eip[2] >> 3) & 7;
973 <                    transfer_type = TYPE_LOAD;
974 <                    break;
975 <                case 0x40:
976 <                    reg = (eip[2] >> 3) & 7;
977 <                    transfer_type = TYPE_LOAD;
978 <                    break;
979 <                case 0x00:
980 <                    reg = (eip[2] >> 3) & 7;
981 <                    transfer_type = TYPE_LOAD;
479 <                    break;
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 >                        transfer_size = SIZE_WORD;
978 >                        goto do_mov_extend;
979 >                  do_mov_extend:
980 >                        op_len = 2;
981 >                        goto do_transfer_load;
982                  }
983 <                len += 3 + ix86_step_over_modrm(eip + 2);
984 <            }
985 <          break;
983 >                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;
1008 <                        transfer_type = TYPE_LOAD;
1007 >                        reg = (eip[op_len] >> 3) & 7;
1008 >                        transfer_type = SIGSEGV_TRANSFER_LOAD;
1009                          break;
1010                  case 0x40:
1011 <                        reg = (eip[1] >> 3) & 7;
1012 <                        transfer_type = TYPE_LOAD;
1011 >                        reg = (eip[op_len] >> 3) & 7;
1012 >                        transfer_type = SIGSEGV_TRANSFER_LOAD;
1013                          break;
1014                  case 0x00:
1015 <                        reg = (eip[1] >> 3) & 7;
1016 <                        transfer_type = TYPE_LOAD;
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;
1033 <                        transfer_type = TYPE_STORE;
1032 >                        reg = (eip[op_len] >> 3) & 7;
1033 >                        transfer_type = SIGSEGV_TRANSFER_STORE;
1034                          break;
1035                  case 0x40:
1036 <                        reg = (eip[1] >> 3) & 7;
1037 <                        transfer_type = TYPE_STORE;
1036 >                        reg = (eip[op_len] >> 3) & 7;
1037 >                        transfer_type = SIGSEGV_TRANSFER_STORE;
1038                          break;
1039                  case 0x00:
1040 <                        reg = (eip[1] >> 3) & 7;
1041 <                        transfer_type = TYPE_STORE;
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 == TYPE_UNKNOWN) {
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 == TYPE_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",
1098 <                   transfer_type == TYPE_LOAD ? "read" : "write");
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 == TYPE_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 571 | 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);
1157          
1158 <        if (instr.transfer_type == TYPE_UNKNOWN) {
1158 >        if (instr.transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
1159                  // Unknown machine code, let it crash. Then patch the decoder
1160                  return false;
1161          }
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",
1166 <                   instr.transfer_type == TYPE_LOAD ? "read" : "write");
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)
1171                  printf(" r%d (ra = %08x)\n", instr.ra, instr.addr);
1172 <        if (instr.transfer_type == TYPE_LOAD)
1172 >        if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD)
1173                  printf(" r%d (rd = 0)\n", instr.rd);
1174   #endif
1175          
1176          if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX)
1177                  regs[instr.ra] = instr.addr;
1178 <        if (instr.transfer_type == TYPE_LOAD)
1178 >        if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD)
1179                  regs[instr.rd] = 0;
1180          
1181          *nip_p += 4;
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(P) sigsegv_fault_handler(P)
1641   #endif
1642  
1643   // SIGSEGV recovery supported ?
# Line 618 | Line 1650 | static bool powerpc_skip_instruction(uns
1650   *  SIGSEGV global handler
1651   */
1652  
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_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(sip)) {
1726 +        case SIGSEGV_RETURN_SUCCESS:
1727 +                return true;
1728 +
1729 + #if HAVE_SIGSEGV_SKIP_INSTRUCTION
1730 +        case SIGSEGV_RETURN_SKIP_INSTRUCTION:
1731 +                // Call the instruction skipper with the register file
1732 +                // available
1733 +                if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE)) {
1734 + #ifdef HAVE_MACH_EXCEPTIONS
1735 +                        // Unlike UNIX signals where the thread state
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 = 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 +        }
1755 +
1756 +        return false;
1757 + }
1758 +
1759 +
1760 + /*
1761 + * There are two mechanisms for handling a bad memory access,
1762 + * Mach exceptions and UNIX signals. The implementation specific
1763 + * code appears below. Its reponsibility is to call handle_badaccess
1764 + * which is the routine that handles the fault in an implementation
1765 + * agnostic manner. The implementation specific code below is then
1766 + * reponsible for checking whether handle_badaccess was able
1767 + * to handle the memory access error and perform any implementation
1768 + * specific tasks necessary afterwards.
1769 + */
1770 +
1771 + #ifdef HAVE_MACH_EXCEPTIONS
1772 + /*
1773 + * We need to forward all exceptions that we do not handle.
1774 + * This is important, there are many exceptions that may be
1775 + * handled by other exception handlers. For example debuggers
1776 + * use exceptions and the exception hander is in another
1777 + * process in such a case. (Timothy J. Wood states in his
1778 + * message to the list that he based this code on that from
1779 + * gdb for Darwin.)
1780 + */
1781 + static inline kern_return_t
1782 + forward_exception(mach_port_t thread_port,
1783 +                                  mach_port_t task_port,
1784 +                                  exception_type_t exception_type,
1785 +                                  exception_data_t exception_data,
1786 +                                  mach_msg_type_number_t data_count,
1787 +                                  ExceptionPorts *oldExceptionPorts)
1788 + {
1789 +        kern_return_t kret;
1790 +        unsigned int portIndex;
1791 +        mach_port_t port;
1792 +        exception_behavior_t behavior;
1793 +        thread_state_flavor_t flavor;
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++) {
1798 +                if (oldExceptionPorts->masks[portIndex] & (1 << exception_type)) {
1799 +                        // This handler wants the exception
1800 +                        break;
1801 +                }
1802 +        }
1803 +
1804 +        if (portIndex >= oldExceptionPorts->maskCount) {
1805 +                fprintf(stderr, "No handler for exception_type = %d. Not fowarding\n", exception_type);
1806 +                return KERN_FAILURE;
1807 +        }
1808 +
1809 +        port = oldExceptionPorts->handlers[portIndex];
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, (natural_t *)&thread_state,
1825 +                                                                 &thread_state_count);
1826 +                MACH_CHECK_ERROR (thread_get_state, kret);
1827 +        }
1828 +
1829 +        switch (behavior) {
1830 +        case EXCEPTION_DEFAULT:
1831 +          // fprintf(stderr, "forwarding to exception_raise\n");
1832 +          kret = exception_raise(port, thread_port, task_port, exception_type,
1833 +                                                         exception_data, data_count);
1834 +          MACH_CHECK_ERROR (exception_raise, kret);
1835 +          break;
1836 +        case EXCEPTION_STATE:
1837 +          // fprintf(stderr, "forwarding to exception_raise_state\n");
1838 +          kret = exception_raise_state(port, exception_type, exception_data,
1839 +                                                                   data_count, &flavor,
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:
1845 +          // fprintf(stderr, "forwarding to exception_raise_state_identity\n");
1846 +          kret = exception_raise_state_identity(port, thread_port, task_port,
1847 +                                                                                        exception_type, exception_data,
1848 +                                                                                        data_count, &flavor,
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, (natural_t *)&thread_state,
1861 +                                                                 thread_state_count);
1862 +                MACH_CHECK_ERROR (thread_set_state, kret);
1863 +        }
1864 +
1865 +        return kret;
1866 + }
1867 +
1868 + /*
1869 + * This is the code that actually handles the exception.
1870 + * It is called by exc_server. For Darwin 5 Apple changed
1871 + * this a bit from how this family of functions worked in
1872 + * Mach. If you are familiar with that it is a little
1873 + * different. The main variation that concerns us here is
1874 + * that code is an array of exception specific codes and
1875 + * codeCount is a count of the number of codes in the code
1876 + * array. In typical Mach all exceptions have a code
1877 + * and sub-code. It happens to be the case that for a
1878 + * EXC_BAD_ACCESS exception the first entry is the type of
1879 + * bad access that occurred and the second entry is the
1880 + * faulting address so these entries correspond exactly to
1881 + * how the code and sub-code are used on Mach.
1882 + *
1883 + * This is a MIG interface. No code in Basilisk II should
1884 + * call this directley. This has to have external C
1885 + * linkage because that is what exc_server expects.
1886 + */
1887 + kern_return_t
1888 + catch_exception_raise(mach_port_t exception_port,
1889 +                                          mach_port_t thread,
1890 +                                          mach_port_t task,
1891 +                                          exception_type_t exception,
1892 +                                          exception_data_t code,
1893 +                                          mach_msg_type_number_t code_count)
1894 + {
1895 +        kern_return_t krc;
1896 +
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, code_count, &ports);
1911 +
1912 +        return krc;
1913 + }
1914 + #endif
1915 +
1916   #ifdef HAVE_SIGSEGV_RECOVERY
1917 + // Handle bad memory accesses with signal handler
1918   static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
1919   {
1920 <        sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS;
1921 <        sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION;
626 <        bool fault_recovered = false;
627 <        
628 <        // Call user's handler and reinstall the global handler, if required
629 <        if (sigsegv_fault_handler(fault_address, fault_instruction)) {
1920 >        // Call handler and reinstall the global handler, if required
1921 >        if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS)) {
1922   #if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
1923                  sigsegv_do_install_handler(sig);
1924   #endif
1925 <                fault_recovered = true;
634 <        }
635 < #if HAVE_SIGSEGV_SKIP_INSTRUCTION
636 <        else if (sigsegv_ignore_fault) {
637 <                // Call the instruction skipper with the register file available
638 <                if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE))
639 <                        fault_recovered = true;
1925 >                return;
1926          }
641 #endif
1927  
1928 <        if (!fault_recovered) {
644 <                // FAIL: reinstall default handler for "safe" crash
1928 >        // Failure: reinstall default handler for "safe" crash
1929   #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
1930 <                SIGSEGV_ALL_SIGNALS
1930 >        SIGSEGV_ALL_SIGNALS
1931   #undef FAULT_HANDLER
648                
649                // We can't do anything with the fault_address, dump state?
650                if (sigsegv_state_dumper != 0)
651                        sigsegv_state_dumper(fault_address, fault_instruction);
652        }
1932   }
1933   #endif
1934  
# Line 663 | Line 1942 | static bool sigsegv_do_install_handler(i
1942   {
1943          // Setup SIGSEGV handler to process writes to frame buffer
1944   #ifdef HAVE_SIGACTION
1945 <        struct sigaction vosf_sa;
1946 <        sigemptyset(&vosf_sa.sa_mask);
1947 <        vosf_sa.sa_sigaction = sigsegv_handler;
1948 <        vosf_sa.sa_flags = SA_SIGINFO;
1949 <        return (sigaction(sig, &vosf_sa, 0) == 0);
1945 >        struct sigaction sigsegv_sa;
1946 >        sigemptyset(&sigsegv_sa.sa_mask);
1947 >        sigsegv_sa.sa_sigaction = sigsegv_handler;
1948 >        sigsegv_sa.sa_flags = SA_SIGINFO;
1949 >        return (sigaction(sig, &sigsegv_sa, 0) == 0);
1950   #else
1951          return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
1952   #endif
# Line 679 | Line 1958 | static bool sigsegv_do_install_handler(i
1958   {
1959          // Setup SIGSEGV handler to process writes to frame buffer
1960   #ifdef HAVE_SIGACTION
1961 <        struct sigaction vosf_sa;
1962 <        sigemptyset(&vosf_sa.sa_mask);
1963 <        vosf_sa.sa_handler = (signal_handler)sigsegv_handler;
1961 >        struct sigaction sigsegv_sa;
1962 >        sigemptyset(&sigsegv_sa.sa_mask);
1963 >        sigsegv_sa.sa_handler = (signal_handler)sigsegv_handler;
1964 >        sigsegv_sa.sa_flags = 0;
1965   #if !EMULATED_68K && defined(__NetBSD__)
1966 <        sigaddset(&vosf_sa.sa_mask, SIGALRM);
1967 <        vosf_sa.sa_flags = SA_ONSTACK;
688 < #else
689 <        vosf_sa.sa_flags = 0;
1966 >        sigaddset(&sigsegv_sa.sa_mask, SIGALRM);
1967 >        sigsegv_sa.sa_flags |= SA_ONSTACK;
1968   #endif
1969 <        return (sigaction(sig, &vosf_sa, 0) == 0);
1969 >        return (sigaction(sig, &sigsegv_sa, 0) == 0);
1970   #else
1971          return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
1972   #endif
1973   }
1974   #endif
1975  
1976 < bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
1976 > #if defined(HAVE_MACH_EXCEPTIONS)
1977 > static bool sigsegv_do_install_handler(sigsegv_fault_handler_t handler)
1978   {
1979 < #ifdef HAVE_SIGSEGV_RECOVERY
1979 >        /*
1980 >         * Except for the exception port functions, this should be
1981 >         * pretty much stock Mach. If later you choose to support
1982 >         * other Mach's besides Darwin, just check for __MACH__
1983 >         * here and __APPLE__ where the actual differences are.
1984 >         */
1985 > #if defined(__APPLE__) && defined(__MACH__)
1986 >        if (sigsegv_fault_handler != NULL) {
1987 >                sigsegv_fault_handler = handler;
1988 >                return true;
1989 >        }
1990 >
1991 >        kern_return_t krc;
1992 >
1993 >        // create the the exception port
1994 >        krc = mach_port_allocate(mach_task_self(),
1995 >                          MACH_PORT_RIGHT_RECEIVE, &_exceptionPort);
1996 >        if (krc != KERN_SUCCESS) {
1997 >                mach_error("mach_port_allocate", krc);
1998 >                return false;
1999 >        }
2000 >
2001 >        // add a port send right
2002 >        krc = mach_port_insert_right(mach_task_self(),
2003 >                              _exceptionPort, _exceptionPort,
2004 >                              MACH_MSG_TYPE_MAKE_SEND);
2005 >        if (krc != KERN_SUCCESS) {
2006 >                mach_error("mach_port_insert_right", krc);
2007 >                return false;
2008 >        }
2009 >
2010 >        // get the old exception ports
2011 >        ports.maskCount = sizeof (ports.masks) / sizeof (ports.masks[0]);
2012 >        krc = thread_get_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, ports.masks,
2013 >                                &ports.maskCount, ports.handlers, ports.behaviors, ports.flavors);
2014 >        if (krc != KERN_SUCCESS) {
2015 >                mach_error("thread_get_exception_ports", krc);
2016 >                return false;
2017 >        }
2018 >
2019 >        // set the new exception port
2020 >        //
2021 >        // We could have used EXCEPTION_STATE_IDENTITY instead of
2022 >        // EXCEPTION_DEFAULT to get the thread state in the initial
2023 >        // message, but it turns out that in the common case this is not
2024 >        // neccessary. If we need it we can later ask for it from the
2025 >        // suspended thread.
2026 >        //
2027 >        // Even with THREAD_STATE_NONE, Darwin provides the program
2028 >        // counter in the thread state.  The comments in the header file
2029 >        // seem to imply that you can count on the GPR's on an exception
2030 >        // as well but just to be safe I use MACHINE_THREAD_STATE because
2031 >        // you have to ask for all of the GPR's anyway just to get the
2032 >        // program counter. In any case because of update effective
2033 >        // address from immediate and update address from effective
2034 >        // addresses of ra and rb modes (as good an name as any for these
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, SIGSEGV_THREAD_STATE_FLAVOR);
2039 >        if (krc != KERN_SUCCESS) {
2040 >                mach_error("thread_set_exception_ports", krc);
2041 >                return false;
2042 >        }
2043 >
2044 >        // create the exception handler thread
2045 >        if (pthread_create(&exc_thread, NULL, &handleExceptions, NULL) != 0) {
2046 >                (void)fprintf(stderr, "creation of exception thread failed\n");
2047 >                return false;
2048 >        }
2049 >
2050 >        // do not care about the exception thread any longer, let is run standalone
2051 >        (void)pthread_detach(exc_thread);
2052 >
2053          sigsegv_fault_handler = handler;
2054 +        return true;
2055 + #else
2056 +        return false;
2057 + #endif
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)
2153          bool success = true;
2154   #define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig);
2155          SIGSEGV_ALL_SIGNALS
2156   #undef FAULT_HANDLER
2157 +        if (success)
2158 +            sigsegv_fault_handler = handler;
2159          return success;
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
2164          return false;
# Line 717 | Line 2172 | bool sigsegv_install_handler(sigsegv_fau
2172  
2173   void sigsegv_deinstall_handler(void)
2174   {
2175 +  // We do nothing for Mach exceptions, the thread would need to be
2176 +  // suspended if not already so, and we might mess with other
2177 +  // exception handlers that came after we registered ours. There is
2178 +  // no need to remove the exception handler, in fact this function is
2179 +  // not called anywhere in Basilisk II.
2180   #ifdef HAVE_SIGSEGV_RECOVERY
2181          sigsegv_fault_handler = 0;
2182   #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
2183          SIGSEGV_ALL_SIGNALS
2184   #undef FAULT_HANDLER
2185   #endif
2186 < }
2187 <
2188 <
729 < /*
730 < *  SIGSEGV ignore state modifier
731 < */
732 <
733 < void sigsegv_set_ignore_state(bool ignore_fault)
734 < {
735 <        sigsegv_ignore_fault = ignore_fault;
2186 > #ifdef HAVE_WIN32_EXCEPTIONS
2187 >        sigsegv_fault_handler = NULL;
2188 > #endif
2189   }
2190  
2191  
# Line 754 | 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;
2216 + const int REF_VALUE = 45;
2217 +
2218   static int page_size;
2219   static volatile char * page = 0;
2220   static volatile int handler_called = 0;
2221  
2222 < static bool sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
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_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 - 123) != page)
2247 <                exit(1);
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_ADDRESS
2252 >                && ((instruction_address <  (sigsegv_address_t)b_region) ||
2253 >                        (instruction_address >= (sigsegv_address_t)e_region)))
2254 >                exit(11);
2255 > #endif
2256          if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0)
2257 <                exit(1);
2258 <        return true;
2257 >                exit(12);
2258 >        return SIGSEGV_RETURN_SUCCESS;
2259   }
2260  
2261   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
2262 < static bool 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 <        return false;
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_ADDRESS
2274 >                        && ((instruction_address <  (sigsegv_address_t)b_region) ||
2275 >                                (instruction_address >= (sigsegv_address_t)e_region)))
2276 >                        return SIGSEGV_RETURN_FAILURE;
2277 > #endif
2278 >                return SIGSEGV_RETURN_SKIP_INSTRUCTION;
2279 >        }
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  
# Line 783 | 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 1;
2351 >                return 2;
2352          
2353 +        memset((void *)page, 0, page_size);
2354          if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0)
2355 <                return 1;
2355 >                return 3;
2356          
2357          if (!sigsegv_install_handler(sigsegv_test_handler))
2358 <                return 1;
795 <        
796 <        page[123] = 45;
797 <        page[123] = 45;
2358 >                return 4;
2359          
2360 + #ifdef __GNUC__
2361 +        b_region = &&L_b_region1;
2362 +        e_region = &&L_e_region1;
2363 + #endif
2364 + L_b_region1:
2365 +        page[REF_INDEX] = REF_VALUE;
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)
2373 <                return 1;
2373 >                return 5;
2374  
2375   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
2376          if (!sigsegv_install_handler(sigsegv_insn_handler))
2377 <                return 1;
2377 >                return 6;
2378          
2379          if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0)
2380 <                return 1;
2380 >                return 7;
2381          
2382          for (int i = 0; i < page_size; i++)
2383                  page[i] = (i + 1) % page_size;
2384          
2385          if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0)
2386 <                return 1;
2386 >                return 8;
2387          
815        sigsegv_set_ignore_state(true);
816
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 1;                                                                       \
2394 >                        return 9;                                                                       \
2395          } while (0)
2396          
2397 + #ifdef __GNUC__
2398 +        b_region = &&L_b_region2;
2399 +        e_region = &&L_e_region2;
2400 + #endif
2401 + L_b_region2:
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();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines