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.12 by gbeauche, 2002-05-16T15:48:06Z vs.
Revision 1.31 by gbeauche, 2003-10-13T20:15:41Z

# Line 4 | Line 4
4   *  Derived from Bruno Haible's work on his SIGSEGV library for clisp
5   *  <http://clisp.sourceforge.net/>
6   *
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-2002 Christian Bauer
14   *
15   *  This program is free software; you can redistribute it and/or modify
# Line 29 | Line 35
35   #include "config.h"
36   #endif
37  
38 + #include <list>
39   #include <signal.h>
40   #include "sigsegv.h"
41  
42 + #ifndef NO_STD_NAMESPACE
43 + using std::list;
44 + #endif
45 +
46   // Return value type of a signal handler (standard type if not defined)
47   #ifndef RETSIGTYPE
48   #define RETSIGTYPE void
# Line 40 | Line 51
51   // Type of the system signal handler
52   typedef RETSIGTYPE (*signal_handler)(int);
53  
43 // Is the fault to be ignored?
44 static bool sigsegv_ignore_fault = false;
45
54   // User's SIGSEGV handler
55   static sigsegv_fault_handler_t sigsegv_fault_handler = 0;
56  
# Line 54 | Line 62 | static bool sigsegv_do_install_handler(i
62  
63  
64   /*
65 + *  Instruction decoding aids
66 + */
67 +
68 + // Transfer size
69 + enum transfer_size_t {
70 +        SIZE_UNKNOWN,
71 +        SIZE_BYTE,
72 +        SIZE_WORD,
73 +        SIZE_LONG
74 + };
75 +
76 + // Transfer type
77 + typedef sigsegv_transfer_type_t transfer_type_t;
78 +
79 + #if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__))
80 + // Addressing mode
81 + enum addressing_mode_t {
82 +        MODE_UNKNOWN,
83 +        MODE_NORM,
84 +        MODE_U,
85 +        MODE_X,
86 +        MODE_UX
87 + };
88 +
89 + // Decoded instruction
90 + struct instruction_t {
91 +        transfer_type_t         transfer_type;
92 +        transfer_size_t         transfer_size;
93 +        addressing_mode_t       addr_mode;
94 +        unsigned int            addr;
95 +        char                            ra, rd;
96 + };
97 +
98 + static void powerpc_decode_instruction(instruction_t *instruction, unsigned int nip, unsigned int * gpr)
99 + {
100 +        // Get opcode and divide into fields
101 +        unsigned int opcode = *((unsigned int *)nip);
102 +        unsigned int primop = opcode >> 26;
103 +        unsigned int exop = (opcode >> 1) & 0x3ff;
104 +        unsigned int ra = (opcode >> 16) & 0x1f;
105 +        unsigned int rb = (opcode >> 11) & 0x1f;
106 +        unsigned int rd = (opcode >> 21) & 0x1f;
107 +        signed int imm = (signed short)(opcode & 0xffff);
108 +        
109 +        // Analyze opcode
110 +        transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
111 +        transfer_size_t transfer_size = SIZE_UNKNOWN;
112 +        addressing_mode_t addr_mode = MODE_UNKNOWN;
113 +        switch (primop) {
114 +        case 31:
115 +                switch (exop) {
116 +                case 23:        // lwzx
117 +                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_X; break;
118 +                case 55:        // lwzux
119 +                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break;
120 +                case 87:        // lbzx
121 +                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
122 +                case 119:       // lbzux
123 +                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
124 +                case 151:       // stwx
125 +                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_X; break;
126 +                case 183:       // stwux
127 +                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break;
128 +                case 215:       // stbx
129 +                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
130 +                case 247:       // stbux
131 +                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
132 +                case 279:       // lhzx
133 +                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
134 +                case 311:       // lhzux
135 +                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
136 +                case 343:       // lhax
137 +                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
138 +                case 375:       // lhaux
139 +                        transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
140 +                case 407:       // sthx
141 +                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
142 +                case 439:       // sthux
143 +                        transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
144 +                }
145 +                break;
146 +        
147 +        case 32:        // lwz
148 +                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break;
149 +        case 33:        // lwzu
150 +                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_U; break;
151 +        case 34:        // lbz
152 +                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
153 +        case 35:        // lbzu
154 +                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
155 +        case 36:        // stw
156 +                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break;
157 +        case 37:        // stwu
158 +                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_U; break;
159 +        case 38:        // stb
160 +                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
161 +        case 39:        // stbu
162 +                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
163 +        case 40:        // lhz
164 +                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
165 +        case 41:        // lhzu
166 +                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
167 +        case 42:        // lha
168 +                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
169 +        case 43:        // lhau
170 +                transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
171 +        case 44:        // sth
172 +                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
173 +        case 45:        // sthu
174 +                transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
175 +        }
176 +        
177 +        // Calculate effective address
178 +        unsigned int addr = 0;
179 +        switch (addr_mode) {
180 +        case MODE_X:
181 +        case MODE_UX:
182 +                if (ra == 0)
183 +                        addr = gpr[rb];
184 +                else
185 +                        addr = gpr[ra] + gpr[rb];
186 +                break;
187 +        case MODE_NORM:
188 +        case MODE_U:
189 +                if (ra == 0)
190 +                        addr = (signed int)(signed short)imm;
191 +                else
192 +                        addr = gpr[ra] + (signed int)(signed short)imm;
193 +                break;
194 +        default:
195 +                break;
196 +        }
197 +        
198 +        // Commit decoded instruction
199 +        instruction->addr = addr;
200 +        instruction->addr_mode = addr_mode;
201 +        instruction->transfer_type = transfer_type;
202 +        instruction->transfer_size = transfer_size;
203 +        instruction->ra = ra;
204 +        instruction->rd = rd;
205 + }
206 + #endif
207 +
208 +
209 + /*
210   *  OS-dependant SIGSEGV signals support section
211   */
212  
# Line 65 | Line 218 | static bool sigsegv_do_install_handler(i
218   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
219   #endif
220   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, siginfo_t *sip, void *scp
221 + #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 siginfo_t *sip, void *scp
222 + #define SIGSEGV_FAULT_HANDLER_ARGS              sip, scp
223   #define SIGSEGV_FAULT_ADDRESS                   sip->si_addr
224 + #if defined(__NetBSD__) || defined(__FreeBSD__)
225 + #if (defined(i386) || defined(__i386__))
226 + #define SIGSEGV_FAULT_INSTRUCTION               (((struct sigcontext *)scp)->sc_eip)
227 + #define SIGSEGV_REGISTER_FILE                   ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */
228 + #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
229 + #endif
230 + #endif
231   #if defined(__linux__)
232   #if (defined(i386) || defined(__i386__))
233   #include <sys/ucontext.h>
234 < #define SIGSEGV_FAULT_INSTRUCTION               (((ucontext_t *)scp)->uc_mcontext.gregs[14]) /* should use REG_EIP instead */
235 < #define SIGSEGV_REGISTER_FILE                   (unsigned long *)(((ucontext_t *)scp)->uc_mcontext.gregs)
234 > #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
235 > #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[14] /* should use REG_EIP instead */
236 > #define SIGSEGV_REGISTER_FILE                   (unsigned int *)SIGSEGV_CONTEXT_REGS
237   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
238   #endif
239 + #if (defined(x86_64) || defined(__x86_64__))
240 + #include <sys/ucontext.h>
241 + #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
242 + #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[16] /* should use REG_RIP instead */
243 + #define SIGSEGV_REGISTER_FILE                   (unsigned long *)SIGSEGV_CONTEXT_REGS
244 + #endif
245   #if (defined(ia64) || defined(__ia64__))
246   #define SIGSEGV_FAULT_INSTRUCTION               (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */
247   #endif
248   #if (defined(powerpc) || defined(__powerpc__))
249   #include <sys/ucontext.h>
250 < #define SIGSEGV_FAULT_INSTRUCTION               (((ucontext_t *)scp)->uc_mcontext.regs->nip)
250 > #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.regs)
251 > #define SIGSEGV_FAULT_INSTRUCTION               (SIGSEGV_CONTEXT_REGS->nip)
252 > #define SIGSEGV_REGISTER_FILE                   (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr)
253 > #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
254   #endif
255   #endif
256   #endif
# Line 90 | Line 262 | static bool sigsegv_do_install_handler(i
262   #if (defined(i386) || defined(__i386__))
263   #include <asm/sigcontext.h>
264   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, struct sigcontext scs
265 < #define SIGSEGV_FAULT_ADDRESS                   scs.cr2
266 < #define SIGSEGV_FAULT_INSTRUCTION               scs.eip
267 < #define SIGSEGV_REGISTER_FILE                   (unsigned long *)(&scs)
265 > #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 struct sigcontext *scp
266 > #define SIGSEGV_FAULT_HANDLER_ARGS              &scs
267 > #define SIGSEGV_FAULT_ADDRESS                   scp->cr2
268 > #define SIGSEGV_FAULT_INSTRUCTION               scp->eip
269 > #define SIGSEGV_REGISTER_FILE                   (unsigned int *)scp
270   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
271   #endif
272   #if (defined(sparc) || defined(__sparc__))
273   #include <asm/sigcontext.h>
274   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp, char *addr
275 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp, addr
276   #define SIGSEGV_FAULT_ADDRESS                   addr
277   #endif
278   #if (defined(powerpc) || defined(__powerpc__))
279   #include <asm/sigcontext.h>
280   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, struct sigcontext *scp
281 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, scp
282   #define SIGSEGV_FAULT_ADDRESS                   scp->regs->dar
283   #define SIGSEGV_FAULT_INSTRUCTION               scp->regs->nip
284 + #define SIGSEGV_REGISTER_FILE                   (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr)
285 + #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
286   #endif
287   #if (defined(alpha) || defined(__alpha__))
288   #include <asm/sigcontext.h>
289   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
290 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
291   #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
292   #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_pc
293  
# Line 127 | Line 306 | static sigsegv_address_t get_fault_addre
306   #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4))
307   #include <ucontext.h>
308   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
309 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
310   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_badvaddr
311   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
312   #endif
# Line 134 | Line 314 | static sigsegv_address_t get_fault_addre
314   // HP-UX
315   #if (defined(hpux) || defined(__hpux__))
316   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
317 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
318   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_sl.sl_ss.ss_narrow.ss_cr21
319   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS)
320   #endif
# Line 142 | Line 323 | static sigsegv_address_t get_fault_addre
323   #if defined(__osf__)
324   #include <ucontext.h>
325   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
326 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
327   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_traparg_a0
328   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
329   #endif
# Line 149 | Line 331 | static sigsegv_address_t get_fault_addre
331   // AIX
332   #if defined(_AIX)
333   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
334 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
335   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_jmpbuf.jmp_context.o_vaddr
336   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
337   #endif
# Line 158 | Line 341 | static sigsegv_address_t get_fault_addre
341   #if (defined(m68k) || defined(__m68k__))
342   #include <m68k/frame.h>
343   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
344 < #define SIGSEGV_FAULT_ADDRESS                   ({                                                                                                                              \
345 <        struct sigstate {                                                                                                                                                                       \
163 <                int ss_flags;                                                                                                                                                                   \
164 <                struct frame ss_frame;                                                                                                                                                  \
165 <        };                                                                                                                                                                                                      \
166 <        struct sigstate *state = (struct sigstate *)scp->sc_ap;                                                                                         \
167 <        char *fault_addr;                                                                                                                                                                       \
168 <        switch (state->ss_frame.f_format) {                                                                                                                                     \
169 <        case 7:         /* 68040 access error */                                                                                                                                \
170 <                /* "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown */    \
171 <                fault_addr = state->ss_frame.f_fmt7.f_fa;                                                                                                               \
172 <                break;                                                                                                                                                                                  \
173 <        default:                                                                                                                                                                                        \
174 <                fault_addr = (char *)code;                                                                                                                                              \
175 <                break;                                                                                                                                                                                  \
176 <        }                                                                                                                                                                                                       \
177 <        fault_addr;                                                                                                                                                                                     \
178 < })
344 > #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
345 > #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
346   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
347 +
348 + // Use decoding scheme from BasiliskII/m68k native
349 + static sigsegv_address_t get_fault_address(struct sigcontext *scp)
350 + {
351 +        struct sigstate {
352 +                int ss_flags;
353 +                struct frame ss_frame;
354 +        };
355 +        struct sigstate *state = (struct sigstate *)scp->sc_ap;
356 +        char *fault_addr;
357 +        switch (state->ss_frame.f_format) {
358 +        case 7:         /* 68040 access error */
359 +                /* "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown */
360 +                fault_addr = state->ss_frame.f_fmt7.f_fa;
361 +                break;
362 +        default:
363 +                fault_addr = (char *)code;
364 +                break;
365 +        }
366 +        return (sigsegv_address_t)fault_addr;
367 + }
368   #else
369   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, void *scp, char *addr
370 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp, addr
371   #define SIGSEGV_FAULT_ADDRESS                   addr
372   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
373   #endif
374   #endif
375  
376 < // MacOS X
376 > // MacOS X, not sure which version this works in. Under 10.1
377 > // vm_protect does not appear to work from a signal handler. Under
378 > // 10.2 signal handlers get siginfo type arguments but the si_addr
379 > // field is the address of the faulting instruction and not the
380 > // address that caused the SIGBUS. Maybe this works in 10.0? In any
381 > // case with Mach exception handlers there is a way to do what this
382 > // was meant to do.
383 > #ifndef HAVE_MACH_EXCEPTIONS
384   #if defined(__APPLE__) && defined(__MACH__)
385   #if (defined(ppc) || defined(__ppc__))
386   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
387 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
388   #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
389   #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_ir
390   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
391 + #define SIGSEGV_REGISTER_FILE                   (unsigned int *)&scp->sc_ir, &((unsigned int *) scp->sc_regs)[2]
392 + #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
393  
394 < // From Boehm's GC 6.0alpha8
196 < #define EXTRACT_OP1(iw)     (((iw) & 0xFC000000) >> 26)
197 < #define EXTRACT_OP2(iw)     (((iw) & 0x000007FE) >> 1)
198 < #define EXTRACT_REGA(iw)    (((iw) & 0x001F0000) >> 16)
199 < #define EXTRACT_REGB(iw)    (((iw) & 0x03E00000) >> 21)
200 < #define EXTRACT_REGC(iw)    (((iw) & 0x0000F800) >> 11)
201 < #define EXTRACT_DISP(iw)    ((short *) &(iw))[1]
202 <
394 > // Use decoding scheme from SheepShaver
395   static sigsegv_address_t get_fault_address(struct sigcontext *scp)
396   {
397 <        unsigned int   instr = *((unsigned int *) scp->sc_ir);
398 <        unsigned int * regs = &((unsigned int *) scp->sc_regs)[2];
399 <        int            disp = 0, tmp;
400 <        unsigned int   baseA = 0, baseB = 0;
401 <        unsigned int   addr, alignmask = 0xFFFFFFFF;
402 <
403 <        switch(EXTRACT_OP1(instr)) {
404 <        case 38:   /* stb */
405 <        case 39:   /* stbu */
406 <        case 54:   /* stfd */
407 <        case 55:   /* stfdu */
408 <        case 52:   /* stfs */
409 <        case 53:   /* stfsu */
410 <        case 44:   /* sth */
411 <        case 45:   /* sthu */
412 <        case 47:   /* stmw */
413 <        case 36:   /* stw */
414 <        case 37:   /* stwu */
415 <                tmp = EXTRACT_REGA(instr);
416 <                if(tmp > 0)
417 <                        baseA = regs[tmp];
418 <                disp = EXTRACT_DISP(instr);
419 <                break;
420 <        case 31:
421 <                switch(EXTRACT_OP2(instr)) {
422 <                case 86:    /* dcbf */
423 <                case 54:    /* dcbst */
424 <                case 1014:  /* dcbz */
425 <                case 247:   /* stbux */
426 <                case 215:   /* stbx */
427 <                case 759:   /* stfdux */
428 <                case 727:   /* stfdx */
429 <                case 983:   /* stfiwx */
430 <                case 695:   /* stfsux */
431 <                case 663:   /* stfsx */
432 <                case 918:   /* sthbrx */
433 <                case 439:   /* sthux */
434 <                case 407:   /* sthx */
435 <                case 661:   /* stswx */
436 <                case 662:   /* stwbrx */
437 <                case 150:   /* stwcx. */
438 <                case 183:   /* stwux */
439 <                case 151:   /* stwx */
440 <                case 135:   /* stvebx */
441 <                case 167:   /* stvehx */
442 <                case 199:   /* stvewx */
443 <                case 231:   /* stvx */
444 <                case 487:   /* stvxl */
445 <                        tmp = EXTRACT_REGA(instr);
446 <                        if(tmp > 0)
447 <                                baseA = regs[tmp];
448 <                        baseB = regs[EXTRACT_REGC(instr)];
449 <                        /* determine Altivec alignment mask */
450 <                        switch(EXTRACT_OP2(instr)) {
451 <                        case 167:   /* stvehx */
452 <                                alignmask = 0xFFFFFFFE;
453 <                                break;
454 <                        case 199:   /* stvewx */
455 <                                alignmask = 0xFFFFFFFC;
456 <                                break;
457 <                        case 231:   /* stvx */
458 <                                alignmask = 0xFFFFFFF0;
459 <                                break;
460 <                        case 487:  /* stvxl */
461 <                                alignmask = 0xFFFFFFF0;
462 <                                break;
463 <                        }
464 <                        break;
465 <                case 725:   /* stswi */
466 <                        tmp = EXTRACT_REGA(instr);
467 <                        if(tmp > 0)
468 <                                baseA = regs[tmp];
469 <                        break;
470 <                default:   /* ignore instruction */
471 <                        return 0;
472 <                        break;
397 >        unsigned int   nip = (unsigned int) scp->sc_ir;
398 >        unsigned int * gpr = &((unsigned int *) scp->sc_regs)[2];
399 >        instruction_t  instr;
400 >
401 >        powerpc_decode_instruction(&instr, nip, gpr);
402 >        return (sigsegv_address_t)instr.addr;
403 > }
404 > #endif
405 > #endif
406 > #endif
407 > #endif
408 >
409 > #if HAVE_MACH_EXCEPTIONS
410 >
411 > // This can easily be extended to other Mach systems, but really who
412 > // uses HURD (oops GNU/HURD), Darwin/x86, NextStep, Rhapsody, or CMU
413 > // Mach 2.5/3.0?
414 > #if defined(__APPLE__) && defined(__MACH__)
415 >
416 > #include <sys/types.h>
417 > #include <stdlib.h>
418 > #include <stdio.h>
419 > #include <pthread.h>
420 >
421 > /*
422 > * If you are familiar with MIG then you will understand the frustration
423 > * that was necessary to get these embedded into C++ code by hand.
424 > */
425 > extern "C" {
426 > #include <mach/mach.h>
427 > #include <mach/mach_error.h>
428 >
429 > extern boolean_t exc_server(mach_msg_header_t *, mach_msg_header_t *);
430 > extern kern_return_t catch_exception_raise(mach_port_t, mach_port_t,
431 >        mach_port_t, exception_type_t, exception_data_t, mach_msg_type_number_t);
432 > extern kern_return_t exception_raise(mach_port_t, mach_port_t, mach_port_t,
433 >        exception_type_t, exception_data_t, mach_msg_type_number_t);
434 > extern kern_return_t exception_raise_state(mach_port_t, exception_type_t,
435 >        exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
436 >        thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
437 > extern kern_return_t exception_raise_state_identity(mach_port_t, mach_port_t, mach_port_t,
438 >        exception_type_t, exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
439 >        thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
440 > }
441 >
442 > // Could make this dynamic by looking for a result of MIG_ARRAY_TOO_LARGE
443 > #define HANDLER_COUNT 64
444 >
445 > // structure to tuck away existing exception handlers
446 > typedef struct _ExceptionPorts {
447 >        mach_msg_type_number_t maskCount;
448 >        exception_mask_t masks[HANDLER_COUNT];
449 >        exception_handler_t handlers[HANDLER_COUNT];
450 >        exception_behavior_t behaviors[HANDLER_COUNT];
451 >        thread_state_flavor_t flavors[HANDLER_COUNT];
452 > } ExceptionPorts;
453 >
454 > // exception handler thread
455 > static pthread_t exc_thread;
456 >
457 > // place where old exception handler info is stored
458 > static ExceptionPorts ports;
459 >
460 > // our exception port
461 > static mach_port_t _exceptionPort = MACH_PORT_NULL;
462 >
463 > #define MACH_CHECK_ERROR(name,ret) \
464 > if (ret != KERN_SUCCESS) { \
465 >        mach_error(#name, ret); \
466 >        exit (1); \
467 > }
468 >
469 > #define SIGSEGV_FAULT_ADDRESS                   code[1]
470 > #define SIGSEGV_FAULT_INSTRUCTION               get_fault_instruction(thread, state)
471 > #define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP)  ((code[0] == KERN_PROTECTION_FAILURE) ? sigsegv_fault_handler(ADDR, IP) : SIGSEGV_RETURN_FAILURE)
472 > #define SIGSEGV_FAULT_HANDLER_ARGLIST   mach_port_t thread, exception_data_t code, ppc_thread_state_t *state
473 > #define SIGSEGV_FAULT_HANDLER_ARGS              thread, code, &state
474 > #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
475 > #define SIGSEGV_REGISTER_FILE                   &state->srr0, &state->r0
476 >
477 > // Given a suspended thread, stuff the current instruction and
478 > // registers into state.
479 > //
480 > // It would have been nice to have this be ppc/x86 independant which
481 > // could have been done easily with a thread_state_t instead of
482 > // ppc_thread_state_t, but because of the way this is called it is
483 > // easier to do it this way.
484 > #if (defined(ppc) || defined(__ppc__))
485 > static inline sigsegv_address_t get_fault_instruction(mach_port_t thread, ppc_thread_state_t *state)
486 > {
487 >        kern_return_t krc;
488 >        mach_msg_type_number_t count;
489 >
490 >        count = MACHINE_THREAD_STATE_COUNT;
491 >        krc = thread_get_state(thread, MACHINE_THREAD_STATE, (thread_state_t)state, &count);
492 >        MACH_CHECK_ERROR (thread_get_state, krc);
493 >
494 >        return (sigsegv_address_t)state->srr0;
495 > }
496 > #endif
497 >
498 > // Since there can only be one exception thread running at any time
499 > // this is not a problem.
500 > #define MSG_SIZE 512
501 > static char msgbuf[MSG_SIZE];
502 > static char replybuf[MSG_SIZE];
503 >
504 > /*
505 > * This is the entry point for the exception handler thread. The job
506 > * of this thread is to wait for exception messages on the exception
507 > * port that was setup beforehand and to pass them on to exc_server.
508 > * exc_server is a MIG generated function that is a part of Mach.
509 > * Its job is to decide what to do with the exception message. In our
510 > * case exc_server calls catch_exception_raise on our behalf. After
511 > * exc_server returns, it is our responsibility to send the reply.
512 > */
513 > static void *
514 > handleExceptions(void *priv)
515 > {
516 >        mach_msg_header_t *msg, *reply;
517 >        kern_return_t krc;
518 >
519 >        msg = (mach_msg_header_t *)msgbuf;
520 >        reply = (mach_msg_header_t *)replybuf;
521 >
522 >        for (;;) {
523 >                krc = mach_msg(msg, MACH_RCV_MSG, MSG_SIZE, MSG_SIZE,
524 >                                _exceptionPort, 0, MACH_PORT_NULL);
525 >                MACH_CHECK_ERROR(mach_msg, krc);
526 >
527 >                if (!exc_server(msg, reply)) {
528 >                        fprintf(stderr, "exc_server hated the message\n");
529 >                        exit(1);
530 >                }
531 >
532 >                krc = mach_msg(reply, MACH_SEND_MSG, reply->msgh_size, 0,
533 >                                 msg->msgh_local_port, 0, MACH_PORT_NULL);
534 >                if (krc != KERN_SUCCESS) {
535 >                        fprintf(stderr, "Error sending message to original reply port, krc = %d, %s",
536 >                                krc, mach_error_string(krc));
537 >                        exit(1);
538                  }
282                break;
283        default:   /* ignore instruction */
284                return 0;
285                break;
539          }
287        
288        addr = (baseA + baseB) + disp;
289        addr &= alignmask;
290        return (sigsegv_address_t)addr;
540   }
541   #endif
542   #endif
543 < #endif
543 >
544 >
545 > /*
546 > *  Instruction skipping
547 > */
548  
549   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
550   // Decode and skip X86 instruction
# Line 309 | Line 562 | enum {
562          X86_REG_EDI = 4
563   };
564   #endif
565 + #if defined(__NetBSD__) || defined(__FreeBSD__)
566 + enum {
567 +        X86_REG_EIP = 10,
568 +        X86_REG_EAX = 7,
569 +        X86_REG_ECX = 6,
570 +        X86_REG_EDX = 5,
571 +        X86_REG_EBX = 4,
572 +        X86_REG_ESP = 13,
573 +        X86_REG_EBP = 2,
574 +        X86_REG_ESI = 1,
575 +        X86_REG_EDI = 0
576 + };
577 + #endif
578   // FIXME: this is partly redundant with the instruction decoding phase
579   // to discover transfer type and register number
580   static inline int ix86_step_over_modrm(unsigned char * p)
# Line 343 | Line 609 | static inline int ix86_step_over_modrm(u
609          return offset;
610   }
611  
612 < static bool ix86_skip_instruction(sigsegv_address_t fault_instruction, unsigned long * regs)
612 > static bool ix86_skip_instruction(unsigned int * regs)
613   {
614 <        unsigned char * eip = (unsigned char *)fault_instruction;
614 >        unsigned char * eip = (unsigned char *)regs[X86_REG_EIP];
615  
616          if (eip == 0)
617                  return false;
618          
619 <        // Transfer type
620 <        enum {
355 <                TYPE_UNKNOWN,
356 <                TYPE_LOAD,
357 <                TYPE_STORE
358 <        } transfer_type = TYPE_UNKNOWN;
359 <        
360 <        // Transfer size
361 <        enum {
362 <                SIZE_BYTE,
363 <                SIZE_WORD,
364 <                SIZE_LONG
365 <        } transfer_size = SIZE_LONG;
619 >        transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
620 >        transfer_size_t transfer_size = SIZE_LONG;
621          
622          int reg = -1;
623          int len = 0;
# Line 376 | Line 631 | static bool ix86_skip_instruction(sigseg
631  
632          // Decode instruction
633          switch (eip[0]) {
634 +        case 0x0f:
635 +            switch (eip[1]) {
636 +            case 0xb6: // MOVZX r32, r/m8
637 +            case 0xb7: // MOVZX r32, r/m16
638 +                switch (eip[2] & 0xc0) {
639 +                case 0x80:
640 +                    reg = (eip[2] >> 3) & 7;
641 +                    transfer_type = SIGSEGV_TRANSFER_LOAD;
642 +                    break;
643 +                case 0x40:
644 +                    reg = (eip[2] >> 3) & 7;
645 +                    transfer_type = SIGSEGV_TRANSFER_LOAD;
646 +                    break;
647 +                case 0x00:
648 +                    reg = (eip[2] >> 3) & 7;
649 +                    transfer_type = SIGSEGV_TRANSFER_LOAD;
650 +                    break;
651 +                }
652 +                len += 3 + ix86_step_over_modrm(eip + 2);
653 +                break;
654 +            }
655 +          break;
656          case 0x8a: // MOV r8, r/m8
657                  transfer_size = SIZE_BYTE;
658          case 0x8b: // MOV r32, r/m32 (or 16-bit operation)
659                  switch (eip[1] & 0xc0) {
660                  case 0x80:
661                          reg = (eip[1] >> 3) & 7;
662 <                        transfer_type = TYPE_LOAD;
662 >                        transfer_type = SIGSEGV_TRANSFER_LOAD;
663                          break;
664                  case 0x40:
665                          reg = (eip[1] >> 3) & 7;
666 <                        transfer_type = TYPE_LOAD;
666 >                        transfer_type = SIGSEGV_TRANSFER_LOAD;
667                          break;
668                  case 0x00:
669                          reg = (eip[1] >> 3) & 7;
670 <                        transfer_type = TYPE_LOAD;
670 >                        transfer_type = SIGSEGV_TRANSFER_LOAD;
671                          break;
672                  }
673                  len += 2 + ix86_step_over_modrm(eip + 1);
# Line 401 | Line 678 | static bool ix86_skip_instruction(sigseg
678                  switch (eip[1] & 0xc0) {
679                  case 0x80:
680                          reg = (eip[1] >> 3) & 7;
681 <                        transfer_type = TYPE_STORE;
681 >                        transfer_type = SIGSEGV_TRANSFER_STORE;
682                          break;
683                  case 0x40:
684                          reg = (eip[1] >> 3) & 7;
685 <                        transfer_type = TYPE_STORE;
685 >                        transfer_type = SIGSEGV_TRANSFER_STORE;
686                          break;
687                  case 0x00:
688                          reg = (eip[1] >> 3) & 7;
689 <                        transfer_type = TYPE_STORE;
689 >                        transfer_type = SIGSEGV_TRANSFER_STORE;
690                          break;
691                  }
692                  len += 2 + ix86_step_over_modrm(eip + 1);
693                  break;
694          }
695  
696 <        if (transfer_type == TYPE_UNKNOWN) {
696 >        if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
697                  // Unknown machine code, let it crash. Then patch the decoder
698                  return false;
699          }
700  
701 <        if (transfer_type == TYPE_LOAD && reg != -1) {
701 >        if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != -1) {
702                  static const int x86_reg_map[8] = {
703                          X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX,
704                          X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI
# Line 447 | Line 724 | static bool ix86_skip_instruction(sigseg
724   #if DEBUG
725          printf("%08x: %s %s access", regs[X86_REG_EIP],
726                     transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long",
727 <                   transfer_type == TYPE_LOAD ? "read" : "write");
727 >                   transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write");
728          
729          if (reg != -1) {
730                  static const char * x86_reg_str_map[8] = {
731                          "eax", "ecx", "edx", "ebx",
732                          "esp", "ebp", "esi", "edi"
733                  };
734 <                printf(" %s register %%%s", transfer_type == TYPE_LOAD ? "to" : "from", x86_reg_str_map[reg]);
734 >                printf(" %s register %%%s", transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from", x86_reg_str_map[reg]);
735          }
736          printf(", %d bytes instruction\n", len);
737   #endif
# Line 463 | Line 740 | static bool ix86_skip_instruction(sigseg
740          return true;
741   }
742   #endif
743 +
744 + // Decode and skip PPC instruction
745 + #if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__))
746 + static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs)
747 + {
748 +        instruction_t instr;
749 +        powerpc_decode_instruction(&instr, *nip_p, regs);
750 +        
751 +        if (instr.transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
752 +                // Unknown machine code, let it crash. Then patch the decoder
753 +                return false;
754 +        }
755 +
756 + #if DEBUG
757 +        printf("%08x: %s %s access", *nip_p,
758 +                   instr.transfer_size == SIZE_BYTE ? "byte" : instr.transfer_size == SIZE_WORD ? "word" : "long",
759 +                   instr.transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write");
760 +        
761 +        if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX)
762 +                printf(" r%d (ra = %08x)\n", instr.ra, instr.addr);
763 +        if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD)
764 +                printf(" r%d (rd = 0)\n", instr.rd);
765 + #endif
766 +        
767 +        if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX)
768 +                regs[instr.ra] = instr.addr;
769 +        if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD)
770 +                regs[instr.rd] = 0;
771 +        
772 +        *nip_p += 4;
773 +        return true;
774 + }
775 + #endif
776   #endif
777  
778   // Fallbacks
779   #ifndef SIGSEGV_FAULT_INSTRUCTION
780   #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_INVALID_PC
781   #endif
782 + #ifndef SIGSEGV_FAULT_HANDLER_ARGLIST_1
783 + #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 SIGSEGV_FAULT_HANDLER_ARGLIST
784 + #endif
785 + #ifndef SIGSEGV_FAULT_HANDLER_INVOKE
786 + #define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP)  sigsegv_fault_handler(ADDR, IP)
787 + #endif
788  
789   // SIGSEGV recovery supported ?
790   #if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS)
# Line 480 | Line 796 | static bool ix86_skip_instruction(sigseg
796   *  SIGSEGV global handler
797   */
798  
799 < #ifdef HAVE_SIGSEGV_RECOVERY
800 < static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
799 > #if defined(HAVE_SIGSEGV_RECOVERY) || defined(HAVE_MACH_EXCEPTIONS)
800 > // This function handles the badaccess to memory.
801 > // It is called from the signal handler or the exception handler.
802 > static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST_1)
803   {
804          sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS;
805          sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION;
488        bool fault_recovered = false;
806          
807          // Call user's handler and reinstall the global handler, if required
808 <        if (sigsegv_fault_handler(fault_address, fault_instruction)) {
809 < #if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
810 <                sigsegv_do_install_handler(sig);
808 >        switch (SIGSEGV_FAULT_HANDLER_INVOKE(fault_address, fault_instruction)) {
809 >        case SIGSEGV_RETURN_SUCCESS:
810 >                return true;
811 >
812 > #if HAVE_SIGSEGV_SKIP_INSTRUCTION
813 >        case SIGSEGV_RETURN_SKIP_INSTRUCTION:
814 >                // Call the instruction skipper with the register file
815 >                // available
816 >                if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE)) {
817 > #ifdef HAVE_MACH_EXCEPTIONS
818 >                        // Unlike UNIX signals where the thread state
819 >                        // is modified off of the stack, in Mach we
820 >                        // need to actually call thread_set_state to
821 >                        // have the register values updated.
822 >                        kern_return_t krc;
823 >
824 >                        krc = thread_set_state(thread,
825 >                                                                   MACHINE_THREAD_STATE, (thread_state_t)state,
826 >                                                                   MACHINE_THREAD_STATE_COUNT);
827 >                        MACH_CHECK_ERROR (thread_get_state, krc);
828 > #endif
829 >                        return true;
830 >                }
831 >                break;
832   #endif
495                fault_recovered = true;
833          }
834 < #if HAVE_SIGSEGV_SKIP_INSTRUCTION
835 <        else if (sigsegv_ignore_fault) {
836 <                // Call the instruction skipper with the register file available
837 <                if (SIGSEGV_SKIP_INSTRUCTION(fault_instruction, SIGSEGV_REGISTER_FILE))
838 <                        fault_recovered = true;
834 >        
835 >        // We can't do anything with the fault_address, dump state?
836 >        if (sigsegv_state_dumper != 0)
837 >                sigsegv_state_dumper(fault_address, fault_instruction);
838 >
839 >        return false;
840 > }
841 > #endif
842 >
843 >
844 > /*
845 > * There are two mechanisms for handling a bad memory access,
846 > * Mach exceptions and UNIX signals. The implementation specific
847 > * code appears below. Its reponsibility is to call handle_badaccess
848 > * which is the routine that handles the fault in an implementation
849 > * agnostic manner. The implementation specific code below is then
850 > * reponsible for checking whether handle_badaccess was able
851 > * to handle the memory access error and perform any implementation
852 > * specific tasks necessary afterwards.
853 > */
854 >
855 > #ifdef HAVE_MACH_EXCEPTIONS
856 > /*
857 > * We need to forward all exceptions that we do not handle.
858 > * This is important, there are many exceptions that may be
859 > * handled by other exception handlers. For example debuggers
860 > * use exceptions and the exception hander is in another
861 > * process in such a case. (Timothy J. Wood states in his
862 > * message to the list that he based this code on that from
863 > * gdb for Darwin.)
864 > */
865 > static inline kern_return_t
866 > forward_exception(mach_port_t thread_port,
867 >                                  mach_port_t task_port,
868 >                                  exception_type_t exception_type,
869 >                                  exception_data_t exception_data,
870 >                                  mach_msg_type_number_t data_count,
871 >                                  ExceptionPorts *oldExceptionPorts)
872 > {
873 >        kern_return_t kret;
874 >        unsigned int portIndex;
875 >        mach_port_t port;
876 >        exception_behavior_t behavior;
877 >        thread_state_flavor_t flavor;
878 >        thread_state_t thread_state;
879 >        mach_msg_type_number_t thread_state_count;
880 >
881 >        for (portIndex = 0; portIndex < oldExceptionPorts->maskCount; portIndex++) {
882 >                if (oldExceptionPorts->masks[portIndex] & (1 << exception_type)) {
883 >                        // This handler wants the exception
884 >                        break;
885 >                }
886 >        }
887 >
888 >        if (portIndex >= oldExceptionPorts->maskCount) {
889 >                fprintf(stderr, "No handler for exception_type = %d. Not fowarding\n", exception_type);
890 >                return KERN_FAILURE;
891 >        }
892 >
893 >        port = oldExceptionPorts->handlers[portIndex];
894 >        behavior = oldExceptionPorts->behaviors[portIndex];
895 >        flavor = oldExceptionPorts->flavors[portIndex];
896 >
897 >        /*
898 >         fprintf(stderr, "forwarding exception, port = 0x%x, behaviour = %d, flavor = %d\n", port, behavior, flavor);
899 >         */
900 >
901 >        if (behavior != EXCEPTION_DEFAULT) {
902 >                thread_state_count = THREAD_STATE_MAX;
903 >                kret = thread_get_state (thread_port, flavor, thread_state,
904 >                                                                 &thread_state_count);
905 >                MACH_CHECK_ERROR (thread_get_state, kret);
906          }
907 +
908 +        switch (behavior) {
909 +        case EXCEPTION_DEFAULT:
910 +          // fprintf(stderr, "forwarding to exception_raise\n");
911 +          kret = exception_raise(port, thread_port, task_port, exception_type,
912 +                                                         exception_data, data_count);
913 +          MACH_CHECK_ERROR (exception_raise, kret);
914 +          break;
915 +        case EXCEPTION_STATE:
916 +          // fprintf(stderr, "forwarding to exception_raise_state\n");
917 +          kret = exception_raise_state(port, exception_type, exception_data,
918 +                                                                   data_count, &flavor,
919 +                                                                   thread_state, thread_state_count,
920 +                                                                   thread_state, &thread_state_count);
921 +          MACH_CHECK_ERROR (exception_raise_state, kret);
922 +          break;
923 +        case EXCEPTION_STATE_IDENTITY:
924 +          // fprintf(stderr, "forwarding to exception_raise_state_identity\n");
925 +          kret = exception_raise_state_identity(port, thread_port, task_port,
926 +                                                                                        exception_type, exception_data,
927 +                                                                                        data_count, &flavor,
928 +                                                                                        thread_state, thread_state_count,
929 +                                                                                        thread_state, &thread_state_count);
930 +          MACH_CHECK_ERROR (exception_raise_state_identity, kret);
931 +          break;
932 +        default:
933 +          fprintf(stderr, "forward_exception got unknown behavior\n");
934 +          break;
935 +        }
936 +
937 +        if (behavior != EXCEPTION_DEFAULT) {
938 +                kret = thread_set_state (thread_port, flavor, thread_state,
939 +                                                                 thread_state_count);
940 +                MACH_CHECK_ERROR (thread_set_state, kret);
941 +        }
942 +
943 +        return KERN_SUCCESS;
944 + }
945 +
946 + /*
947 + * This is the code that actually handles the exception.
948 + * It is called by exc_server. For Darwin 5 Apple changed
949 + * this a bit from how this family of functions worked in
950 + * Mach. If you are familiar with that it is a little
951 + * different. The main variation that concerns us here is
952 + * that code is an array of exception specific codes and
953 + * codeCount is a count of the number of codes in the code
954 + * array. In typical Mach all exceptions have a code
955 + * and sub-code. It happens to be the case that for a
956 + * EXC_BAD_ACCESS exception the first entry is the type of
957 + * bad access that occurred and the second entry is the
958 + * faulting address so these entries correspond exactly to
959 + * how the code and sub-code are used on Mach.
960 + *
961 + * This is a MIG interface. No code in Basilisk II should
962 + * call this directley. This has to have external C
963 + * linkage because that is what exc_server expects.
964 + */
965 + kern_return_t
966 + catch_exception_raise(mach_port_t exception_port,
967 +                                          mach_port_t thread,
968 +                                          mach_port_t task,
969 +                                          exception_type_t exception,
970 +                                          exception_data_t code,
971 +                                          mach_msg_type_number_t codeCount)
972 + {
973 +        ppc_thread_state_t state;
974 +        kern_return_t krc;
975 +
976 +        if ((exception == EXC_BAD_ACCESS)  && (codeCount >= 2)) {
977 +                if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS))
978 +                        return KERN_SUCCESS;
979 +        }
980 +
981 +        // In Mach we do not need to remove the exception handler.
982 +        // If we forward the exception, eventually some exception handler
983 +        // will take care of this exception.
984 +        krc = forward_exception(thread, task, exception, code, codeCount, &ports);
985 +
986 +        return krc;
987 + }
988   #endif
989  
990 <        if (!fault_recovered) {
991 <                // FAIL: reinstall default handler for "safe" crash
990 > #ifdef HAVE_SIGSEGV_RECOVERY
991 > // Handle bad memory accesses with signal handler
992 > static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
993 > {
994 >        // Call handler and reinstall the global handler, if required
995 >        if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS)) {
996 > #if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
997 >                sigsegv_do_install_handler(sig);
998 > #endif
999 >                return;
1000 >        }
1001 >
1002 >        // Failure: reinstall default handler for "safe" crash
1003   #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
1004 <                SIGSEGV_ALL_SIGNALS
1004 >        SIGSEGV_ALL_SIGNALS
1005   #undef FAULT_HANDLER
510                
511                // We can't do anything with the fault_address, dump state?
512                if (sigsegv_state_dumper != 0)
513                        sigsegv_state_dumper(fault_address, fault_instruction);
514        }
1006   }
1007   #endif
1008  
# Line 525 | Line 1016 | static bool sigsegv_do_install_handler(i
1016   {
1017          // Setup SIGSEGV handler to process writes to frame buffer
1018   #ifdef HAVE_SIGACTION
1019 <        struct sigaction vosf_sa;
1020 <        sigemptyset(&vosf_sa.sa_mask);
1021 <        vosf_sa.sa_sigaction = sigsegv_handler;
1022 <        vosf_sa.sa_flags = SA_SIGINFO;
1023 <        return (sigaction(sig, &vosf_sa, 0) == 0);
1019 >        struct sigaction sigsegv_sa;
1020 >        sigemptyset(&sigsegv_sa.sa_mask);
1021 >        sigsegv_sa.sa_sigaction = sigsegv_handler;
1022 >        sigsegv_sa.sa_flags = SA_SIGINFO;
1023 >        return (sigaction(sig, &sigsegv_sa, 0) == 0);
1024   #else
1025          return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
1026   #endif
# Line 541 | Line 1032 | static bool sigsegv_do_install_handler(i
1032   {
1033          // Setup SIGSEGV handler to process writes to frame buffer
1034   #ifdef HAVE_SIGACTION
1035 <        struct sigaction vosf_sa;
1036 <        sigemptyset(&vosf_sa.sa_mask);
1037 <        vosf_sa.sa_handler = (signal_handler)sigsegv_handler;
1035 >        struct sigaction sigsegv_sa;
1036 >        sigemptyset(&sigsegv_sa.sa_mask);
1037 >        sigsegv_sa.sa_handler = (signal_handler)sigsegv_handler;
1038 >        sigsegv_sa.sa_flags = 0;
1039   #if !EMULATED_68K && defined(__NetBSD__)
1040 <        sigaddset(&vosf_sa.sa_mask, SIGALRM);
1041 <        vosf_sa.sa_flags = SA_ONSTACK;
550 < #else
551 <        vosf_sa.sa_flags = 0;
1040 >        sigaddset(&sigsegv_sa.sa_mask, SIGALRM);
1041 >        sigsegv_sa.sa_flags |= SA_ONSTACK;
1042   #endif
1043 <        return (sigaction(sig, &vosf_sa, 0) == 0);
1043 >        return (sigaction(sig, &sigsegv_sa, 0) == 0);
1044   #else
1045          return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
1046   #endif
1047   }
1048   #endif
1049  
1050 < bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
1050 > #if defined(HAVE_MACH_EXCEPTIONS)
1051 > static bool sigsegv_do_install_handler(sigsegv_fault_handler_t handler)
1052   {
1053 < #ifdef HAVE_SIGSEGV_RECOVERY
1053 >        /*
1054 >         * Except for the exception port functions, this should be
1055 >         * pretty much stock Mach. If later you choose to support
1056 >         * other Mach's besides Darwin, just check for __MACH__
1057 >         * here and __APPLE__ where the actual differences are.
1058 >         */
1059 > #if defined(__APPLE__) && defined(__MACH__)
1060 >        if (sigsegv_fault_handler != NULL) {
1061 >                sigsegv_fault_handler = handler;
1062 >                return true;
1063 >        }
1064 >
1065 >        kern_return_t krc;
1066 >
1067 >        // create the the exception port
1068 >        krc = mach_port_allocate(mach_task_self(),
1069 >                          MACH_PORT_RIGHT_RECEIVE, &_exceptionPort);
1070 >        if (krc != KERN_SUCCESS) {
1071 >                mach_error("mach_port_allocate", krc);
1072 >                return false;
1073 >        }
1074 >
1075 >        // add a port send right
1076 >        krc = mach_port_insert_right(mach_task_self(),
1077 >                              _exceptionPort, _exceptionPort,
1078 >                              MACH_MSG_TYPE_MAKE_SEND);
1079 >        if (krc != KERN_SUCCESS) {
1080 >                mach_error("mach_port_insert_right", krc);
1081 >                return false;
1082 >        }
1083 >
1084 >        // get the old exception ports
1085 >        ports.maskCount = sizeof (ports.masks) / sizeof (ports.masks[0]);
1086 >        krc = thread_get_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, ports.masks,
1087 >                                &ports.maskCount, ports.handlers, ports.behaviors, ports.flavors);
1088 >        if (krc != KERN_SUCCESS) {
1089 >                mach_error("thread_get_exception_ports", krc);
1090 >                return false;
1091 >        }
1092 >
1093 >        // set the new exception port
1094 >        //
1095 >        // We could have used EXCEPTION_STATE_IDENTITY instead of
1096 >        // EXCEPTION_DEFAULT to get the thread state in the initial
1097 >        // message, but it turns out that in the common case this is not
1098 >        // neccessary. If we need it we can later ask for it from the
1099 >        // suspended thread.
1100 >        //
1101 >        // Even with THREAD_STATE_NONE, Darwin provides the program
1102 >        // counter in the thread state.  The comments in the header file
1103 >        // seem to imply that you can count on the GPR's on an exception
1104 >        // as well but just to be safe I use MACHINE_THREAD_STATE because
1105 >        // you have to ask for all of the GPR's anyway just to get the
1106 >        // program counter. In any case because of update effective
1107 >        // address from immediate and update address from effective
1108 >        // addresses of ra and rb modes (as good an name as any for these
1109 >        // addressing modes) used in PPC instructions, you will need the
1110 >        // GPR state anyway.
1111 >        krc = thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, _exceptionPort,
1112 >                                EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
1113 >        if (krc != KERN_SUCCESS) {
1114 >                mach_error("thread_set_exception_ports", krc);
1115 >                return false;
1116 >        }
1117 >
1118 >        // create the exception handler thread
1119 >        if (pthread_create(&exc_thread, NULL, &handleExceptions, NULL) != 0) {
1120 >                (void)fprintf(stderr, "creation of exception thread failed\n");
1121 >                return false;
1122 >        }
1123 >
1124 >        // do not care about the exception thread any longer, let is run standalone
1125 >        (void)pthread_detach(exc_thread);
1126 >
1127          sigsegv_fault_handler = handler;
1128 +        return true;
1129 + #else
1130 +        return false;
1131 + #endif
1132 + }
1133 + #endif
1134 +
1135 + bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
1136 + {
1137 + #if defined(HAVE_SIGSEGV_RECOVERY)
1138          bool success = true;
1139   #define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig);
1140          SIGSEGV_ALL_SIGNALS
1141   #undef FAULT_HANDLER
1142 +        if (success)
1143 +            sigsegv_fault_handler = handler;
1144          return success;
1145 + #elif defined(HAVE_MACH_EXCEPTIONS)
1146 +        return sigsegv_do_install_handler(handler);
1147   #else
1148          // FAIL: no siginfo_t nor sigcontext subterfuge is available
1149          return false;
# Line 579 | Line 1157 | bool sigsegv_install_handler(sigsegv_fau
1157  
1158   void sigsegv_deinstall_handler(void)
1159   {
1160 +  // We do nothing for Mach exceptions, the thread would need to be
1161 +  // suspended if not already so, and we might mess with other
1162 +  // exception handlers that came after we registered ours. There is
1163 +  // no need to remove the exception handler, in fact this function is
1164 +  // not called anywhere in Basilisk II.
1165   #ifdef HAVE_SIGSEGV_RECOVERY
1166          sigsegv_fault_handler = 0;
1167   #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
# Line 589 | Line 1172 | void sigsegv_deinstall_handler(void)
1172  
1173  
1174   /*
592 *  SIGSEGV ignore state modifier
593 */
594
595 void sigsegv_set_ignore_state(bool ignore_fault)
596 {
597        sigsegv_ignore_fault = ignore_fault;
598 }
599
600
601 /*
1175   *  Set callback function when we cannot handle the fault
1176   */
1177  
# Line 623 | Line 1196 | static int page_size;
1196   static volatile char * page = 0;
1197   static volatile int handler_called = 0;
1198  
1199 < static bool sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
1199 > static sigsegv_return_t sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
1200   {
1201          handler_called++;
1202          if ((fault_address - 123) != page)
1203 <                exit(1);
1203 >                exit(10);
1204          if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0)
1205 <                exit(1);
1206 <        return true;
1205 >                exit(11);
1206 >        return SIGSEGV_RETURN_SUCCESS;
1207   }
1208  
1209   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
1210 < static bool sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
1210 > #ifdef __GNUC__
1211 > // Code range where we expect the fault to come from
1212 > static void *b_region, *e_region;
1213 > #endif
1214 >
1215 > static sigsegv_return_t sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
1216   {
1217 <        return false;
1217 >        if (((unsigned long)fault_address - (unsigned long)page) < page_size) {
1218 > #ifdef __GNUC__
1219 >                // Make sure reported fault instruction address falls into
1220 >                // expected code range
1221 >                if (instruction_address != SIGSEGV_INVALID_PC
1222 >                        && ((instruction_address <  (sigsegv_address_t)b_region) ||
1223 >                                (instruction_address >= (sigsegv_address_t)e_region)))
1224 >                        return SIGSEGV_RETURN_FAILURE;
1225 > #endif
1226 >                return SIGSEGV_RETURN_SKIP_INSTRUCTION;
1227 >        }
1228 >
1229 >        return SIGSEGV_RETURN_FAILURE;
1230   }
1231   #endif
1232  
# Line 647 | Line 1237 | int main(void)
1237  
1238          page_size = getpagesize();
1239          if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED)
1240 <                return 1;
1240 >                return 2;
1241          
1242          if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0)
1243 <                return 1;
1243 >                return 3;
1244          
1245          if (!sigsegv_install_handler(sigsegv_test_handler))
1246 <                return 1;
1246 >                return 4;
1247          
1248          page[123] = 45;
1249          page[123] = 45;
1250          
1251          if (handler_called != 1)
1252 <                return 1;
1252 >                return 5;
1253  
1254   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
1255          if (!sigsegv_install_handler(sigsegv_insn_handler))
1256 <                return 1;
1256 >                return 6;
1257          
1258 <        if (vm_protect((char *)page, page_size, VM_PAGE_WRITE) < 0)
1259 <                return 1;
1258 >        if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0)
1259 >                return 7;
1260          
1261          for (int i = 0; i < page_size; i++)
1262                  page[i] = (i + 1) % page_size;
1263          
1264          if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0)
1265 <                return 1;
1265 >                return 8;
1266          
677        sigsegv_set_ignore_state(true);
678
1267   #define TEST_SKIP_INSTRUCTION(TYPE) do {                                \
1268                  const unsigned int TAG = 0x12345678;                    \
1269                  TYPE data = *((TYPE *)(page + sizeof(TYPE)));   \
1270                  volatile unsigned int effect = data + TAG;              \
1271                  if (effect != TAG)                                                              \
1272 <                        return 1;                                                                       \
1272 >                        return 9;                                                                       \
1273          } while (0)
1274          
1275 + #ifdef __GNUC__
1276 +        b_region = &&L_b_region;
1277 +        e_region = &&L_e_region;
1278 + #endif
1279 + L_b_region:
1280          TEST_SKIP_INSTRUCTION(unsigned char);
1281          TEST_SKIP_INSTRUCTION(unsigned short);
1282          TEST_SKIP_INSTRUCTION(unsigned int);
1283 + L_e_region:
1284   #endif
1285  
1286          vm_exit();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines