ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
Revision: 1.33
Committed: 2003-10-21T23:10:19Z (21 years ago) by gbeauche
Branch: MAIN
Changes since 1.32: +37 -15 lines
Log Message:
- FreeBSD/i386 sigcontext subterfuge support for instruction skipper
- NetBSD/alpha support from Boehm GC
- NetBSD/i386 won't work in sigcontext subterfuge mode unless instruction
  is decoded more accurately to compute the effective address. Also note
  that NetBSD 1.6 does not support siginfo_t yet.

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * sigsegv.cpp - SIGSEGV signals support
3     *
4     * Derived from Bruno Haible's work on his SIGSEGV library for clisp
5     * <http://clisp.sourceforge.net/>
6     *
7 gbeauche 1.27 * 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 cebix 1.7 * Basilisk II (C) 1997-2002 Christian Bauer
14 gbeauche 1.1 *
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
17     * the Free Software Foundation; either version 2 of the License, or
18     * (at your option) any later version.
19     *
20     * This program is distributed in the hope that it will be useful,
21     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23     * GNU General Public License for more details.
24     *
25     * You should have received a copy of the GNU General Public License
26     * along with this program; if not, write to the Free Software
27     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28     */
29    
30     #ifdef HAVE_UNISTD_H
31     #include <unistd.h>
32     #endif
33    
34     #ifdef HAVE_CONFIG_H
35     #include "config.h"
36     #endif
37    
38 gbeauche 1.22 #include <list>
39 gbeauche 1.1 #include <signal.h>
40     #include "sigsegv.h"
41    
42 gbeauche 1.22 #ifndef NO_STD_NAMESPACE
43     using std::list;
44     #endif
45    
46 gbeauche 1.1 // Return value type of a signal handler (standard type if not defined)
47     #ifndef RETSIGTYPE
48     #define RETSIGTYPE void
49     #endif
50    
51     // Type of the system signal handler
52     typedef RETSIGTYPE (*signal_handler)(int);
53    
54     // User's SIGSEGV handler
55 gbeauche 1.12 static sigsegv_fault_handler_t sigsegv_fault_handler = 0;
56 gbeauche 1.1
57 gbeauche 1.10 // Function called to dump state if we can't handle the fault
58 gbeauche 1.12 static sigsegv_state_dumper_t sigsegv_state_dumper = 0;
59 gbeauche 1.10
60 gbeauche 1.1 // Actual SIGSEGV handler installer
61     static bool sigsegv_do_install_handler(int sig);
62    
63    
64     /*
65 gbeauche 1.14 * 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 gbeauche 1.23 // Transfer type
77     typedef sigsegv_transfer_type_t transfer_type_t;
78    
79 gbeauche 1.14 #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 gbeauche 1.22 transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
111 gbeauche 1.14 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 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_X; break;
118 gbeauche 1.14 case 55: // lwzux
119 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break;
120 gbeauche 1.14 case 87: // lbzx
121 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
122 gbeauche 1.14 case 119: // lbzux
123 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
124 gbeauche 1.14 case 151: // stwx
125 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_X; break;
126 gbeauche 1.14 case 183: // stwux
127 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break;
128 gbeauche 1.14 case 215: // stbx
129 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
130 gbeauche 1.14 case 247: // stbux
131 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
132 gbeauche 1.14 case 279: // lhzx
133 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
134 gbeauche 1.14 case 311: // lhzux
135 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
136 gbeauche 1.14 case 343: // lhax
137 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
138 gbeauche 1.14 case 375: // lhaux
139 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
140 gbeauche 1.14 case 407: // sthx
141 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
142 gbeauche 1.14 case 439: // sthux
143 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
144 gbeauche 1.14 }
145     break;
146    
147     case 32: // lwz
148 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break;
149 gbeauche 1.14 case 33: // lwzu
150 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_U; break;
151 gbeauche 1.14 case 34: // lbz
152 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
153 gbeauche 1.14 case 35: // lbzu
154 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
155 gbeauche 1.14 case 36: // stw
156 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break;
157 gbeauche 1.14 case 37: // stwu
158 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_U; break;
159 gbeauche 1.14 case 38: // stb
160 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
161 gbeauche 1.14 case 39: // stbu
162 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
163 gbeauche 1.14 case 40: // lhz
164 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
165 gbeauche 1.14 case 41: // lhzu
166 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
167 gbeauche 1.14 case 42: // lha
168 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
169 gbeauche 1.14 case 43: // lhau
170 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
171 gbeauche 1.14 case 44: // sth
172 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
173 gbeauche 1.14 case 45: // sthu
174 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
175 gbeauche 1.14 }
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 gbeauche 1.1 * OS-dependant SIGSEGV signals support section
211     */
212    
213     #if HAVE_SIGINFO_T
214     // Generic extended signal handler
215 cebix 1.8 #if defined(__NetBSD__) || defined(__FreeBSD__)
216     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
217     #else
218 gbeauche 1.1 #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
219 cebix 1.8 #endif
220 gbeauche 1.5 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp
221 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 siginfo_t *sip, void *scp
222     #define SIGSEGV_FAULT_HANDLER_ARGS sip, scp
223 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS sip->si_addr
224 gbeauche 1.32 #if defined(__sun__)
225     #if (defined(sparc) || defined(__sparc__))
226     #include <sys/ucontext.h>
227     #define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs)
228     #define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[REG_PC]
229     #endif
230     #endif
231 gbeauche 1.33 #if defined(__FreeBSD__)
232 gbeauche 1.17 #if (defined(i386) || defined(__i386__))
233     #define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_eip)
234 gbeauche 1.18 #define SIGSEGV_REGISTER_FILE ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */
235 gbeauche 1.17 #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
236 gbeauche 1.19 #endif
237 gbeauche 1.17 #endif
238 gbeauche 1.5 #if defined(__linux__)
239 gbeauche 1.6 #if (defined(i386) || defined(__i386__))
240     #include <sys/ucontext.h>
241 gbeauche 1.14 #define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs)
242     #define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[14] /* should use REG_EIP instead */
243     #define SIGSEGV_REGISTER_FILE (unsigned int *)SIGSEGV_CONTEXT_REGS
244 gbeauche 1.10 #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
245 gbeauche 1.6 #endif
246 gbeauche 1.20 #if (defined(x86_64) || defined(__x86_64__))
247     #include <sys/ucontext.h>
248     #define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs)
249     #define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[16] /* should use REG_RIP instead */
250     #define SIGSEGV_REGISTER_FILE (unsigned long *)SIGSEGV_CONTEXT_REGS
251     #endif
252 gbeauche 1.5 #if (defined(ia64) || defined(__ia64__))
253     #define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */
254     #endif
255 gbeauche 1.9 #if (defined(powerpc) || defined(__powerpc__))
256     #include <sys/ucontext.h>
257 gbeauche 1.14 #define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.regs)
258     #define SIGSEGV_FAULT_INSTRUCTION (SIGSEGV_CONTEXT_REGS->nip)
259     #define SIGSEGV_REGISTER_FILE (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr)
260 gbeauche 1.13 #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
261 gbeauche 1.9 #endif
262 gbeauche 1.5 #endif
263 gbeauche 1.1 #endif
264    
265     #if HAVE_SIGCONTEXT_SUBTERFUGE
266     // Linux kernels prior to 2.4 ?
267     #if defined(__linux__)
268     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
269     #if (defined(i386) || defined(__i386__))
270     #include <asm/sigcontext.h>
271     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs
272 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 struct sigcontext *scp
273     #define SIGSEGV_FAULT_HANDLER_ARGS &scs
274     #define SIGSEGV_FAULT_ADDRESS scp->cr2
275     #define SIGSEGV_FAULT_INSTRUCTION scp->eip
276     #define SIGSEGV_REGISTER_FILE (unsigned int *)scp
277 gbeauche 1.10 #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
278 gbeauche 1.1 #endif
279     #if (defined(sparc) || defined(__sparc__))
280     #include <asm/sigcontext.h>
281 gbeauche 1.5 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr
282 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr
283 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS addr
284     #endif
285     #if (defined(powerpc) || defined(__powerpc__))
286     #include <asm/sigcontext.h>
287 gbeauche 1.4 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp
288 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGS sig, scp
289 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS scp->regs->dar
290     #define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip
291 gbeauche 1.14 #define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr)
292 gbeauche 1.13 #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
293 gbeauche 1.1 #endif
294 gbeauche 1.4 #if (defined(alpha) || defined(__alpha__))
295     #include <asm/sigcontext.h>
296     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
297 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
298 gbeauche 1.4 #define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
299     #define SIGSEGV_FAULT_INSTRUCTION scp->sc_pc
300     #endif
301 gbeauche 1.1 #endif
302    
303     // Irix 5 or 6 on MIPS
304     #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4))
305 gbeauche 1.11 #include <ucontext.h>
306 gbeauche 1.1 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
307 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
308 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr
309     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
310     #endif
311    
312 gbeauche 1.11 // HP-UX
313     #if (defined(hpux) || defined(__hpux__))
314     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
315 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
316 gbeauche 1.11 #define SIGSEGV_FAULT_ADDRESS scp->sc_sl.sl_ss.ss_narrow.ss_cr21
317     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS)
318     #endif
319    
320 gbeauche 1.1 // OSF/1 on Alpha
321     #if defined(__osf__)
322 gbeauche 1.11 #include <ucontext.h>
323 gbeauche 1.1 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
324 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
325 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0
326     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
327     #endif
328    
329     // AIX
330     #if defined(_AIX)
331     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
332 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
333 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr
334     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
335     #endif
336    
337 gbeauche 1.33 // NetBSD
338     #if defined(__NetBSD__)
339 gbeauche 1.1 #if (defined(m68k) || defined(__m68k__))
340     #include <m68k/frame.h>
341     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
342 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
343 gbeauche 1.14 #define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
344 gbeauche 1.1 #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
345 gbeauche 1.14
346     // Use decoding scheme from BasiliskII/m68k native
347     static sigsegv_address_t get_fault_address(struct sigcontext *scp)
348     {
349     struct sigstate {
350     int ss_flags;
351     struct frame ss_frame;
352     };
353     struct sigstate *state = (struct sigstate *)scp->sc_ap;
354     char *fault_addr;
355     switch (state->ss_frame.f_format) {
356     case 7: /* 68040 access error */
357     /* "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown */
358     fault_addr = state->ss_frame.f_fmt7.f_fa;
359     break;
360     default:
361     fault_addr = (char *)code;
362     break;
363     }
364     return (sigsegv_address_t)fault_addr;
365     }
366 gbeauche 1.33 #endif
367     #if (defined(alpha) || defined(__alpha__))
368     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
369     #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
370     #define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
371     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
372     #endif
373     #if (defined(i386) || defined(__i386__))
374     #error "FIXME: need to decode instruction and compute EA"
375     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
376     #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
377     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
378     #endif
379     #endif
380     #if defined(__FreeBSD__)
381     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
382     #if (defined(i386) || defined(__i386__))
383     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr
384 gbeauche 1.30 #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr
385 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS addr
386 gbeauche 1.33 #define SIGSEGV_FAULT_INSTRUCTION scp->sc_eip
387     #define SIGSEGV_REGISTER_FILE ((unsigned int *)&scp->sc_edi)
388     #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
389 gbeauche 1.1 #endif
390     #endif
391 gbeauche 1.33
392     // Extract fault address out of a sigcontext
393     #if (defined(alpha) || defined(__alpha__))
394     // From Boehm's GC 6.0alpha8
395     static sigsegv_address_t get_fault_address(struct sigcontext *scp)
396     {
397     unsigned int instruction = *((unsigned int *)(scp->sc_pc));
398     unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f];
399     fault_address += (signed long)(signed short)(instruction & 0xffff);
400     return (sigsegv_address_t)fault_address;
401     }
402     #endif
403    
404 gbeauche 1.4
405 gbeauche 1.27 // MacOS X, not sure which version this works in. Under 10.1
406     // vm_protect does not appear to work from a signal handler. Under
407     // 10.2 signal handlers get siginfo type arguments but the si_addr
408     // field is the address of the faulting instruction and not the
409     // address that caused the SIGBUS. Maybe this works in 10.0? In any
410     // case with Mach exception handlers there is a way to do what this
411     // was meant to do.
412     #ifndef HAVE_MACH_EXCEPTIONS
413 gbeauche 1.4 #if defined(__APPLE__) && defined(__MACH__)
414     #if (defined(ppc) || defined(__ppc__))
415     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
416 gbeauche 1.27 #define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
417 gbeauche 1.4 #define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
418     #define SIGSEGV_FAULT_INSTRUCTION scp->sc_ir
419     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
420 gbeauche 1.14 #define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->sc_ir, &((unsigned int *) scp->sc_regs)[2]
421     #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
422 gbeauche 1.4
423 gbeauche 1.14 // Use decoding scheme from SheepShaver
424 gbeauche 1.4 static sigsegv_address_t get_fault_address(struct sigcontext *scp)
425     {
426 gbeauche 1.14 unsigned int nip = (unsigned int) scp->sc_ir;
427     unsigned int * gpr = &((unsigned int *) scp->sc_regs)[2];
428     instruction_t instr;
429    
430     powerpc_decode_instruction(&instr, nip, gpr);
431     return (sigsegv_address_t)instr.addr;
432 gbeauche 1.4 }
433     #endif
434     #endif
435 gbeauche 1.1 #endif
436 gbeauche 1.27 #endif
437    
438     #if HAVE_MACH_EXCEPTIONS
439    
440     // This can easily be extended to other Mach systems, but really who
441     // uses HURD (oops GNU/HURD), Darwin/x86, NextStep, Rhapsody, or CMU
442     // Mach 2.5/3.0?
443     #if defined(__APPLE__) && defined(__MACH__)
444    
445     #include <sys/types.h>
446     #include <stdlib.h>
447     #include <stdio.h>
448     #include <pthread.h>
449    
450     /*
451     * If you are familiar with MIG then you will understand the frustration
452     * that was necessary to get these embedded into C++ code by hand.
453     */
454     extern "C" {
455     #include <mach/mach.h>
456     #include <mach/mach_error.h>
457    
458     extern boolean_t exc_server(mach_msg_header_t *, mach_msg_header_t *);
459     extern kern_return_t catch_exception_raise(mach_port_t, mach_port_t,
460     mach_port_t, exception_type_t, exception_data_t, mach_msg_type_number_t);
461     extern kern_return_t exception_raise(mach_port_t, mach_port_t, mach_port_t,
462     exception_type_t, exception_data_t, mach_msg_type_number_t);
463     extern kern_return_t exception_raise_state(mach_port_t, exception_type_t,
464     exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
465     thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
466     extern kern_return_t exception_raise_state_identity(mach_port_t, mach_port_t, mach_port_t,
467     exception_type_t, exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
468     thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
469     }
470    
471     // Could make this dynamic by looking for a result of MIG_ARRAY_TOO_LARGE
472     #define HANDLER_COUNT 64
473    
474     // structure to tuck away existing exception handlers
475     typedef struct _ExceptionPorts {
476     mach_msg_type_number_t maskCount;
477     exception_mask_t masks[HANDLER_COUNT];
478     exception_handler_t handlers[HANDLER_COUNT];
479     exception_behavior_t behaviors[HANDLER_COUNT];
480     thread_state_flavor_t flavors[HANDLER_COUNT];
481     } ExceptionPorts;
482    
483     // exception handler thread
484     static pthread_t exc_thread;
485    
486     // place where old exception handler info is stored
487     static ExceptionPorts ports;
488    
489     // our exception port
490     static mach_port_t _exceptionPort = MACH_PORT_NULL;
491    
492     #define MACH_CHECK_ERROR(name,ret) \
493     if (ret != KERN_SUCCESS) { \
494     mach_error(#name, ret); \
495     exit (1); \
496     }
497    
498     #define SIGSEGV_FAULT_ADDRESS code[1]
499     #define SIGSEGV_FAULT_INSTRUCTION get_fault_instruction(thread, state)
500 gbeauche 1.31 #define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP) ((code[0] == KERN_PROTECTION_FAILURE) ? sigsegv_fault_handler(ADDR, IP) : SIGSEGV_RETURN_FAILURE)
501 gbeauche 1.27 #define SIGSEGV_FAULT_HANDLER_ARGLIST mach_port_t thread, exception_data_t code, ppc_thread_state_t *state
502     #define SIGSEGV_FAULT_HANDLER_ARGS thread, code, &state
503     #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
504     #define SIGSEGV_REGISTER_FILE &state->srr0, &state->r0
505    
506     // Given a suspended thread, stuff the current instruction and
507     // registers into state.
508     //
509     // It would have been nice to have this be ppc/x86 independant which
510     // could have been done easily with a thread_state_t instead of
511     // ppc_thread_state_t, but because of the way this is called it is
512     // easier to do it this way.
513     #if (defined(ppc) || defined(__ppc__))
514     static inline sigsegv_address_t get_fault_instruction(mach_port_t thread, ppc_thread_state_t *state)
515     {
516     kern_return_t krc;
517     mach_msg_type_number_t count;
518    
519     count = MACHINE_THREAD_STATE_COUNT;
520     krc = thread_get_state(thread, MACHINE_THREAD_STATE, (thread_state_t)state, &count);
521     MACH_CHECK_ERROR (thread_get_state, krc);
522    
523     return (sigsegv_address_t)state->srr0;
524     }
525     #endif
526    
527     // Since there can only be one exception thread running at any time
528     // this is not a problem.
529     #define MSG_SIZE 512
530     static char msgbuf[MSG_SIZE];
531     static char replybuf[MSG_SIZE];
532    
533     /*
534     * This is the entry point for the exception handler thread. The job
535     * of this thread is to wait for exception messages on the exception
536     * port that was setup beforehand and to pass them on to exc_server.
537     * exc_server is a MIG generated function that is a part of Mach.
538     * Its job is to decide what to do with the exception message. In our
539     * case exc_server calls catch_exception_raise on our behalf. After
540     * exc_server returns, it is our responsibility to send the reply.
541     */
542     static void *
543     handleExceptions(void *priv)
544     {
545     mach_msg_header_t *msg, *reply;
546     kern_return_t krc;
547    
548     msg = (mach_msg_header_t *)msgbuf;
549     reply = (mach_msg_header_t *)replybuf;
550    
551     for (;;) {
552     krc = mach_msg(msg, MACH_RCV_MSG, MSG_SIZE, MSG_SIZE,
553     _exceptionPort, 0, MACH_PORT_NULL);
554     MACH_CHECK_ERROR(mach_msg, krc);
555    
556     if (!exc_server(msg, reply)) {
557     fprintf(stderr, "exc_server hated the message\n");
558     exit(1);
559     }
560    
561     krc = mach_msg(reply, MACH_SEND_MSG, reply->msgh_size, 0,
562     msg->msgh_local_port, 0, MACH_PORT_NULL);
563     if (krc != KERN_SUCCESS) {
564     fprintf(stderr, "Error sending message to original reply port, krc = %d, %s",
565     krc, mach_error_string(krc));
566     exit(1);
567     }
568     }
569     }
570     #endif
571     #endif
572 gbeauche 1.1
573 gbeauche 1.14
574     /*
575     * Instruction skipping
576     */
577    
578 gbeauche 1.10 #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
579     // Decode and skip X86 instruction
580     #if (defined(i386) || defined(__i386__))
581     #if defined(__linux__)
582     enum {
583     X86_REG_EIP = 14,
584     X86_REG_EAX = 11,
585     X86_REG_ECX = 10,
586     X86_REG_EDX = 9,
587     X86_REG_EBX = 8,
588     X86_REG_ESP = 7,
589     X86_REG_EBP = 6,
590     X86_REG_ESI = 5,
591     X86_REG_EDI = 4
592     };
593     #endif
594 gbeauche 1.17 #if defined(__NetBSD__) || defined(__FreeBSD__)
595     enum {
596     X86_REG_EIP = 10,
597     X86_REG_EAX = 7,
598     X86_REG_ECX = 6,
599     X86_REG_EDX = 5,
600     X86_REG_EBX = 4,
601     X86_REG_ESP = 13,
602     X86_REG_EBP = 2,
603     X86_REG_ESI = 1,
604     X86_REG_EDI = 0
605     };
606     #endif
607 gbeauche 1.10 // FIXME: this is partly redundant with the instruction decoding phase
608     // to discover transfer type and register number
609     static inline int ix86_step_over_modrm(unsigned char * p)
610     {
611     int mod = (p[0] >> 6) & 3;
612     int rm = p[0] & 7;
613     int offset = 0;
614    
615     // ModR/M Byte
616     switch (mod) {
617     case 0: // [reg]
618     if (rm == 5) return 4; // disp32
619     break;
620     case 1: // disp8[reg]
621     offset = 1;
622     break;
623     case 2: // disp32[reg]
624     offset = 4;
625     break;
626     case 3: // register
627     return 0;
628     }
629    
630     // SIB Byte
631     if (rm == 4) {
632     if (mod == 0 && (p[1] & 7) == 5)
633     offset = 5; // disp32[index]
634     else
635     offset++;
636     }
637    
638     return offset;
639     }
640    
641 gbeauche 1.14 static bool ix86_skip_instruction(unsigned int * regs)
642 gbeauche 1.10 {
643 gbeauche 1.14 unsigned char * eip = (unsigned char *)regs[X86_REG_EIP];
644 gbeauche 1.10
645     if (eip == 0)
646     return false;
647    
648 gbeauche 1.22 transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
649 gbeauche 1.14 transfer_size_t transfer_size = SIZE_LONG;
650 gbeauche 1.10
651     int reg = -1;
652     int len = 0;
653    
654     // Operand size prefix
655     if (*eip == 0x66) {
656     eip++;
657     len++;
658     transfer_size = SIZE_WORD;
659     }
660    
661     // Decode instruction
662     switch (eip[0]) {
663 gbeauche 1.17 case 0x0f:
664 gbeauche 1.18 switch (eip[1]) {
665     case 0xb6: // MOVZX r32, r/m8
666     case 0xb7: // MOVZX r32, r/m16
667 gbeauche 1.17 switch (eip[2] & 0xc0) {
668     case 0x80:
669     reg = (eip[2] >> 3) & 7;
670 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD;
671 gbeauche 1.17 break;
672     case 0x40:
673     reg = (eip[2] >> 3) & 7;
674 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD;
675 gbeauche 1.17 break;
676     case 0x00:
677     reg = (eip[2] >> 3) & 7;
678 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD;
679 gbeauche 1.17 break;
680     }
681     len += 3 + ix86_step_over_modrm(eip + 2);
682 gbeauche 1.18 break;
683 gbeauche 1.17 }
684     break;
685 gbeauche 1.10 case 0x8a: // MOV r8, r/m8
686     transfer_size = SIZE_BYTE;
687     case 0x8b: // MOV r32, r/m32 (or 16-bit operation)
688     switch (eip[1] & 0xc0) {
689     case 0x80:
690     reg = (eip[1] >> 3) & 7;
691 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD;
692 gbeauche 1.10 break;
693     case 0x40:
694     reg = (eip[1] >> 3) & 7;
695 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD;
696 gbeauche 1.10 break;
697     case 0x00:
698     reg = (eip[1] >> 3) & 7;
699 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_LOAD;
700 gbeauche 1.10 break;
701     }
702     len += 2 + ix86_step_over_modrm(eip + 1);
703     break;
704     case 0x88: // MOV r/m8, r8
705     transfer_size = SIZE_BYTE;
706     case 0x89: // MOV r/m32, r32 (or 16-bit operation)
707     switch (eip[1] & 0xc0) {
708     case 0x80:
709     reg = (eip[1] >> 3) & 7;
710 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE;
711 gbeauche 1.10 break;
712     case 0x40:
713     reg = (eip[1] >> 3) & 7;
714 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE;
715 gbeauche 1.10 break;
716     case 0x00:
717     reg = (eip[1] >> 3) & 7;
718 gbeauche 1.22 transfer_type = SIGSEGV_TRANSFER_STORE;
719 gbeauche 1.10 break;
720     }
721     len += 2 + ix86_step_over_modrm(eip + 1);
722     break;
723     }
724    
725 gbeauche 1.22 if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
726 gbeauche 1.10 // Unknown machine code, let it crash. Then patch the decoder
727     return false;
728     }
729    
730 gbeauche 1.22 if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != -1) {
731 gbeauche 1.10 static const int x86_reg_map[8] = {
732     X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX,
733     X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI
734     };
735    
736     if (reg < 0 || reg >= 8)
737     return false;
738    
739     int rloc = x86_reg_map[reg];
740     switch (transfer_size) {
741     case SIZE_BYTE:
742     regs[rloc] = (regs[rloc] & ~0xff);
743     break;
744     case SIZE_WORD:
745     regs[rloc] = (regs[rloc] & ~0xffff);
746     break;
747     case SIZE_LONG:
748     regs[rloc] = 0;
749     break;
750     }
751     }
752    
753     #if DEBUG
754 gbeauche 1.15 printf("%08x: %s %s access", regs[X86_REG_EIP],
755 gbeauche 1.10 transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long",
756 gbeauche 1.22 transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write");
757 gbeauche 1.10
758     if (reg != -1) {
759     static const char * x86_reg_str_map[8] = {
760     "eax", "ecx", "edx", "ebx",
761     "esp", "ebp", "esi", "edi"
762     };
763 gbeauche 1.22 printf(" %s register %%%s", transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from", x86_reg_str_map[reg]);
764 gbeauche 1.10 }
765     printf(", %d bytes instruction\n", len);
766     #endif
767    
768     regs[X86_REG_EIP] += len;
769 gbeauche 1.13 return true;
770     }
771     #endif
772 gbeauche 1.14
773 gbeauche 1.13 // Decode and skip PPC instruction
774 gbeauche 1.14 #if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__))
775     static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs)
776 gbeauche 1.13 {
777 gbeauche 1.14 instruction_t instr;
778     powerpc_decode_instruction(&instr, *nip_p, regs);
779 gbeauche 1.13
780 gbeauche 1.22 if (instr.transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
781 gbeauche 1.13 // Unknown machine code, let it crash. Then patch the decoder
782     return false;
783     }
784    
785     #if DEBUG
786 gbeauche 1.14 printf("%08x: %s %s access", *nip_p,
787     instr.transfer_size == SIZE_BYTE ? "byte" : instr.transfer_size == SIZE_WORD ? "word" : "long",
788 gbeauche 1.22 instr.transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write");
789 gbeauche 1.14
790     if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX)
791     printf(" r%d (ra = %08x)\n", instr.ra, instr.addr);
792 gbeauche 1.22 if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD)
793 gbeauche 1.14 printf(" r%d (rd = 0)\n", instr.rd);
794     #endif
795    
796     if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX)
797     regs[instr.ra] = instr.addr;
798 gbeauche 1.22 if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD)
799 gbeauche 1.14 regs[instr.rd] = 0;
800 gbeauche 1.13
801 gbeauche 1.14 *nip_p += 4;
802 gbeauche 1.10 return true;
803     }
804     #endif
805     #endif
806    
807 gbeauche 1.1 // Fallbacks
808     #ifndef SIGSEGV_FAULT_INSTRUCTION
809     #define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC
810     #endif
811 gbeauche 1.30 #ifndef SIGSEGV_FAULT_HANDLER_ARGLIST_1
812     #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 SIGSEGV_FAULT_HANDLER_ARGLIST
813     #endif
814 gbeauche 1.31 #ifndef SIGSEGV_FAULT_HANDLER_INVOKE
815     #define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP) sigsegv_fault_handler(ADDR, IP)
816     #endif
817 gbeauche 1.1
818 gbeauche 1.2 // SIGSEGV recovery supported ?
819     #if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS)
820     #define HAVE_SIGSEGV_RECOVERY
821     #endif
822    
823 gbeauche 1.1
824     /*
825     * SIGSEGV global handler
826     */
827    
828 gbeauche 1.27 #if defined(HAVE_SIGSEGV_RECOVERY) || defined(HAVE_MACH_EXCEPTIONS)
829     // This function handles the badaccess to memory.
830     // It is called from the signal handler or the exception handler.
831 gbeauche 1.30 static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST_1)
832 gbeauche 1.1 {
833 gbeauche 1.10 sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS;
834     sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION;
835    
836 gbeauche 1.1 // Call user's handler and reinstall the global handler, if required
837 gbeauche 1.31 switch (SIGSEGV_FAULT_HANDLER_INVOKE(fault_address, fault_instruction)) {
838 gbeauche 1.24 case SIGSEGV_RETURN_SUCCESS:
839 gbeauche 1.27 return true;
840    
841 gbeauche 1.10 #if HAVE_SIGSEGV_SKIP_INSTRUCTION
842 gbeauche 1.24 case SIGSEGV_RETURN_SKIP_INSTRUCTION:
843 gbeauche 1.27 // Call the instruction skipper with the register file
844     // available
845     if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE)) {
846     #ifdef HAVE_MACH_EXCEPTIONS
847     // Unlike UNIX signals where the thread state
848     // is modified off of the stack, in Mach we
849     // need to actually call thread_set_state to
850     // have the register values updated.
851     kern_return_t krc;
852    
853     krc = thread_set_state(thread,
854     MACHINE_THREAD_STATE, (thread_state_t)state,
855     MACHINE_THREAD_STATE_COUNT);
856     MACH_CHECK_ERROR (thread_get_state, krc);
857     #endif
858     return true;
859     }
860 gbeauche 1.24 break;
861     #endif
862 gbeauche 1.10 }
863 gbeauche 1.27
864     // We can't do anything with the fault_address, dump state?
865     if (sigsegv_state_dumper != 0)
866     sigsegv_state_dumper(fault_address, fault_instruction);
867    
868     return false;
869     }
870     #endif
871    
872    
873     /*
874     * There are two mechanisms for handling a bad memory access,
875     * Mach exceptions and UNIX signals. The implementation specific
876     * code appears below. Its reponsibility is to call handle_badaccess
877     * which is the routine that handles the fault in an implementation
878     * agnostic manner. The implementation specific code below is then
879     * reponsible for checking whether handle_badaccess was able
880     * to handle the memory access error and perform any implementation
881     * specific tasks necessary afterwards.
882     */
883    
884     #ifdef HAVE_MACH_EXCEPTIONS
885     /*
886     * We need to forward all exceptions that we do not handle.
887     * This is important, there are many exceptions that may be
888     * handled by other exception handlers. For example debuggers
889     * use exceptions and the exception hander is in another
890     * process in such a case. (Timothy J. Wood states in his
891     * message to the list that he based this code on that from
892     * gdb for Darwin.)
893     */
894     static inline kern_return_t
895     forward_exception(mach_port_t thread_port,
896     mach_port_t task_port,
897     exception_type_t exception_type,
898     exception_data_t exception_data,
899     mach_msg_type_number_t data_count,
900     ExceptionPorts *oldExceptionPorts)
901     {
902     kern_return_t kret;
903     unsigned int portIndex;
904     mach_port_t port;
905     exception_behavior_t behavior;
906     thread_state_flavor_t flavor;
907     thread_state_t thread_state;
908     mach_msg_type_number_t thread_state_count;
909    
910     for (portIndex = 0; portIndex < oldExceptionPorts->maskCount; portIndex++) {
911     if (oldExceptionPorts->masks[portIndex] & (1 << exception_type)) {
912     // This handler wants the exception
913     break;
914     }
915     }
916    
917     if (portIndex >= oldExceptionPorts->maskCount) {
918     fprintf(stderr, "No handler for exception_type = %d. Not fowarding\n", exception_type);
919     return KERN_FAILURE;
920     }
921    
922     port = oldExceptionPorts->handlers[portIndex];
923     behavior = oldExceptionPorts->behaviors[portIndex];
924     flavor = oldExceptionPorts->flavors[portIndex];
925    
926     /*
927     fprintf(stderr, "forwarding exception, port = 0x%x, behaviour = %d, flavor = %d\n", port, behavior, flavor);
928     */
929    
930     if (behavior != EXCEPTION_DEFAULT) {
931     thread_state_count = THREAD_STATE_MAX;
932     kret = thread_get_state (thread_port, flavor, thread_state,
933     &thread_state_count);
934     MACH_CHECK_ERROR (thread_get_state, kret);
935     }
936    
937     switch (behavior) {
938     case EXCEPTION_DEFAULT:
939     // fprintf(stderr, "forwarding to exception_raise\n");
940     kret = exception_raise(port, thread_port, task_port, exception_type,
941     exception_data, data_count);
942     MACH_CHECK_ERROR (exception_raise, kret);
943     break;
944     case EXCEPTION_STATE:
945     // fprintf(stderr, "forwarding to exception_raise_state\n");
946     kret = exception_raise_state(port, exception_type, exception_data,
947     data_count, &flavor,
948     thread_state, thread_state_count,
949     thread_state, &thread_state_count);
950     MACH_CHECK_ERROR (exception_raise_state, kret);
951     break;
952     case EXCEPTION_STATE_IDENTITY:
953     // fprintf(stderr, "forwarding to exception_raise_state_identity\n");
954     kret = exception_raise_state_identity(port, thread_port, task_port,
955     exception_type, exception_data,
956     data_count, &flavor,
957     thread_state, thread_state_count,
958     thread_state, &thread_state_count);
959     MACH_CHECK_ERROR (exception_raise_state_identity, kret);
960     break;
961     default:
962     fprintf(stderr, "forward_exception got unknown behavior\n");
963     break;
964     }
965    
966     if (behavior != EXCEPTION_DEFAULT) {
967     kret = thread_set_state (thread_port, flavor, thread_state,
968     thread_state_count);
969     MACH_CHECK_ERROR (thread_set_state, kret);
970     }
971    
972     return KERN_SUCCESS;
973     }
974    
975     /*
976     * This is the code that actually handles the exception.
977     * It is called by exc_server. For Darwin 5 Apple changed
978     * this a bit from how this family of functions worked in
979     * Mach. If you are familiar with that it is a little
980     * different. The main variation that concerns us here is
981     * that code is an array of exception specific codes and
982     * codeCount is a count of the number of codes in the code
983     * array. In typical Mach all exceptions have a code
984     * and sub-code. It happens to be the case that for a
985     * EXC_BAD_ACCESS exception the first entry is the type of
986     * bad access that occurred and the second entry is the
987     * faulting address so these entries correspond exactly to
988     * how the code and sub-code are used on Mach.
989     *
990     * This is a MIG interface. No code in Basilisk II should
991     * call this directley. This has to have external C
992     * linkage because that is what exc_server expects.
993     */
994     kern_return_t
995     catch_exception_raise(mach_port_t exception_port,
996     mach_port_t thread,
997     mach_port_t task,
998     exception_type_t exception,
999     exception_data_t code,
1000     mach_msg_type_number_t codeCount)
1001     {
1002     ppc_thread_state_t state;
1003     kern_return_t krc;
1004    
1005     if ((exception == EXC_BAD_ACCESS) && (codeCount >= 2)) {
1006     if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS))
1007     return KERN_SUCCESS;
1008     }
1009    
1010     // In Mach we do not need to remove the exception handler.
1011     // If we forward the exception, eventually some exception handler
1012     // will take care of this exception.
1013     krc = forward_exception(thread, task, exception, code, codeCount, &ports);
1014    
1015     return krc;
1016     }
1017     #endif
1018    
1019     #ifdef HAVE_SIGSEGV_RECOVERY
1020     // Handle bad memory accesses with signal handler
1021     static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
1022     {
1023     // Call handler and reinstall the global handler, if required
1024     if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS)) {
1025     #if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
1026     sigsegv_do_install_handler(sig);
1027     #endif
1028     return;
1029     }
1030 gbeauche 1.10
1031 gbeauche 1.27 // Failure: reinstall default handler for "safe" crash
1032 gbeauche 1.1 #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
1033 gbeauche 1.27 SIGSEGV_ALL_SIGNALS
1034 gbeauche 1.1 #undef FAULT_HANDLER
1035     }
1036 gbeauche 1.2 #endif
1037 gbeauche 1.1
1038    
1039     /*
1040     * SIGSEGV handler initialization
1041     */
1042    
1043     #if defined(HAVE_SIGINFO_T)
1044     static bool sigsegv_do_install_handler(int sig)
1045     {
1046     // Setup SIGSEGV handler to process writes to frame buffer
1047     #ifdef HAVE_SIGACTION
1048 gbeauche 1.22 struct sigaction sigsegv_sa;
1049     sigemptyset(&sigsegv_sa.sa_mask);
1050     sigsegv_sa.sa_sigaction = sigsegv_handler;
1051     sigsegv_sa.sa_flags = SA_SIGINFO;
1052     return (sigaction(sig, &sigsegv_sa, 0) == 0);
1053 gbeauche 1.1 #else
1054     return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
1055     #endif
1056     }
1057 gbeauche 1.2 #endif
1058    
1059     #if defined(HAVE_SIGCONTEXT_SUBTERFUGE)
1060 gbeauche 1.1 static bool sigsegv_do_install_handler(int sig)
1061     {
1062     // Setup SIGSEGV handler to process writes to frame buffer
1063     #ifdef HAVE_SIGACTION
1064 gbeauche 1.22 struct sigaction sigsegv_sa;
1065     sigemptyset(&sigsegv_sa.sa_mask);
1066     sigsegv_sa.sa_handler = (signal_handler)sigsegv_handler;
1067     sigsegv_sa.sa_flags = 0;
1068 gbeauche 1.1 #if !EMULATED_68K && defined(__NetBSD__)
1069 gbeauche 1.22 sigaddset(&sigsegv_sa.sa_mask, SIGALRM);
1070     sigsegv_sa.sa_flags |= SA_ONSTACK;
1071 gbeauche 1.1 #endif
1072 gbeauche 1.22 return (sigaction(sig, &sigsegv_sa, 0) == 0);
1073 gbeauche 1.1 #else
1074     return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
1075     #endif
1076     }
1077     #endif
1078    
1079 gbeauche 1.27 #if defined(HAVE_MACH_EXCEPTIONS)
1080     static bool sigsegv_do_install_handler(sigsegv_fault_handler_t handler)
1081     {
1082     /*
1083     * Except for the exception port functions, this should be
1084     * pretty much stock Mach. If later you choose to support
1085     * other Mach's besides Darwin, just check for __MACH__
1086     * here and __APPLE__ where the actual differences are.
1087     */
1088     #if defined(__APPLE__) && defined(__MACH__)
1089     if (sigsegv_fault_handler != NULL) {
1090     sigsegv_fault_handler = handler;
1091     return true;
1092     }
1093    
1094     kern_return_t krc;
1095    
1096     // create the the exception port
1097     krc = mach_port_allocate(mach_task_self(),
1098     MACH_PORT_RIGHT_RECEIVE, &_exceptionPort);
1099     if (krc != KERN_SUCCESS) {
1100     mach_error("mach_port_allocate", krc);
1101     return false;
1102     }
1103    
1104     // add a port send right
1105     krc = mach_port_insert_right(mach_task_self(),
1106     _exceptionPort, _exceptionPort,
1107     MACH_MSG_TYPE_MAKE_SEND);
1108     if (krc != KERN_SUCCESS) {
1109     mach_error("mach_port_insert_right", krc);
1110     return false;
1111     }
1112    
1113     // get the old exception ports
1114     ports.maskCount = sizeof (ports.masks) / sizeof (ports.masks[0]);
1115     krc = thread_get_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, ports.masks,
1116     &ports.maskCount, ports.handlers, ports.behaviors, ports.flavors);
1117     if (krc != KERN_SUCCESS) {
1118     mach_error("thread_get_exception_ports", krc);
1119     return false;
1120     }
1121    
1122     // set the new exception port
1123     //
1124     // We could have used EXCEPTION_STATE_IDENTITY instead of
1125     // EXCEPTION_DEFAULT to get the thread state in the initial
1126     // message, but it turns out that in the common case this is not
1127     // neccessary. If we need it we can later ask for it from the
1128     // suspended thread.
1129     //
1130     // Even with THREAD_STATE_NONE, Darwin provides the program
1131     // counter in the thread state. The comments in the header file
1132     // seem to imply that you can count on the GPR's on an exception
1133     // as well but just to be safe I use MACHINE_THREAD_STATE because
1134     // you have to ask for all of the GPR's anyway just to get the
1135     // program counter. In any case because of update effective
1136     // address from immediate and update address from effective
1137     // addresses of ra and rb modes (as good an name as any for these
1138     // addressing modes) used in PPC instructions, you will need the
1139     // GPR state anyway.
1140     krc = thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, _exceptionPort,
1141     EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
1142     if (krc != KERN_SUCCESS) {
1143     mach_error("thread_set_exception_ports", krc);
1144     return false;
1145     }
1146    
1147     // create the exception handler thread
1148     if (pthread_create(&exc_thread, NULL, &handleExceptions, NULL) != 0) {
1149     (void)fprintf(stderr, "creation of exception thread failed\n");
1150     return false;
1151     }
1152    
1153     // do not care about the exception thread any longer, let is run standalone
1154     (void)pthread_detach(exc_thread);
1155    
1156     sigsegv_fault_handler = handler;
1157     return true;
1158     #else
1159     return false;
1160     #endif
1161     }
1162     #endif
1163    
1164 gbeauche 1.12 bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
1165 gbeauche 1.1 {
1166 gbeauche 1.27 #if defined(HAVE_SIGSEGV_RECOVERY)
1167 gbeauche 1.1 bool success = true;
1168     #define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig);
1169     SIGSEGV_ALL_SIGNALS
1170     #undef FAULT_HANDLER
1171 gbeauche 1.27 if (success)
1172     sigsegv_fault_handler = handler;
1173 gbeauche 1.1 return success;
1174 gbeauche 1.27 #elif defined(HAVE_MACH_EXCEPTIONS)
1175     return sigsegv_do_install_handler(handler);
1176 gbeauche 1.1 #else
1177     // FAIL: no siginfo_t nor sigcontext subterfuge is available
1178     return false;
1179     #endif
1180     }
1181    
1182    
1183     /*
1184     * SIGSEGV handler deinitialization
1185     */
1186    
1187     void sigsegv_deinstall_handler(void)
1188     {
1189 gbeauche 1.27 // We do nothing for Mach exceptions, the thread would need to be
1190     // suspended if not already so, and we might mess with other
1191     // exception handlers that came after we registered ours. There is
1192     // no need to remove the exception handler, in fact this function is
1193     // not called anywhere in Basilisk II.
1194 gbeauche 1.2 #ifdef HAVE_SIGSEGV_RECOVERY
1195 gbeauche 1.12 sigsegv_fault_handler = 0;
1196 gbeauche 1.1 #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
1197     SIGSEGV_ALL_SIGNALS
1198     #undef FAULT_HANDLER
1199 gbeauche 1.2 #endif
1200 gbeauche 1.1 }
1201    
1202 gbeauche 1.10
1203     /*
1204     * Set callback function when we cannot handle the fault
1205     */
1206    
1207 gbeauche 1.12 void sigsegv_set_dump_state(sigsegv_state_dumper_t handler)
1208 gbeauche 1.10 {
1209 gbeauche 1.12 sigsegv_state_dumper = handler;
1210 gbeauche 1.10 }
1211    
1212    
1213 gbeauche 1.1 /*
1214     * Test program used for configure/test
1215     */
1216    
1217 gbeauche 1.4 #ifdef CONFIGURE_TEST_SIGSEGV_RECOVERY
1218 gbeauche 1.1 #include <stdio.h>
1219     #include <stdlib.h>
1220     #include <fcntl.h>
1221     #include <sys/mman.h>
1222 gbeauche 1.4 #include "vm_alloc.h"
1223 gbeauche 1.1
1224 gbeauche 1.32 const int REF_INDEX = 123;
1225     const int REF_VALUE = 45;
1226    
1227 gbeauche 1.1 static int page_size;
1228 gbeauche 1.3 static volatile char * page = 0;
1229     static volatile int handler_called = 0;
1230 gbeauche 1.1
1231 gbeauche 1.32 #ifdef __GNUC__
1232     // Code range where we expect the fault to come from
1233     static void *b_region, *e_region;
1234     #endif
1235    
1236 gbeauche 1.24 static sigsegv_return_t sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
1237 gbeauche 1.1 {
1238     handler_called++;
1239 gbeauche 1.32 if ((fault_address - REF_INDEX) != page)
1240 gbeauche 1.29 exit(10);
1241 gbeauche 1.32 #ifdef __GNUC__
1242     // Make sure reported fault instruction address falls into
1243     // expected code range
1244     if (instruction_address != SIGSEGV_INVALID_PC
1245     && ((instruction_address < (sigsegv_address_t)b_region) ||
1246     (instruction_address >= (sigsegv_address_t)e_region)))
1247     exit(11);
1248     #endif
1249 gbeauche 1.4 if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0)
1250 gbeauche 1.32 exit(12);
1251 gbeauche 1.24 return SIGSEGV_RETURN_SUCCESS;
1252 gbeauche 1.1 }
1253    
1254 gbeauche 1.10 #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
1255 gbeauche 1.24 static sigsegv_return_t sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
1256 gbeauche 1.10 {
1257 gbeauche 1.28 if (((unsigned long)fault_address - (unsigned long)page) < page_size) {
1258     #ifdef __GNUC__
1259     // Make sure reported fault instruction address falls into
1260     // expected code range
1261     if (instruction_address != SIGSEGV_INVALID_PC
1262     && ((instruction_address < (sigsegv_address_t)b_region) ||
1263     (instruction_address >= (sigsegv_address_t)e_region)))
1264     return SIGSEGV_RETURN_FAILURE;
1265     #endif
1266 gbeauche 1.26 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
1267 gbeauche 1.28 }
1268    
1269 gbeauche 1.24 return SIGSEGV_RETURN_FAILURE;
1270 gbeauche 1.10 }
1271     #endif
1272    
1273 gbeauche 1.1 int main(void)
1274     {
1275 gbeauche 1.4 if (vm_init() < 0)
1276 gbeauche 1.1 return 1;
1277    
1278     page_size = getpagesize();
1279 gbeauche 1.4 if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED)
1280 gbeauche 1.29 return 2;
1281 gbeauche 1.4
1282 gbeauche 1.32 memset((void *)page, 0, page_size);
1283 gbeauche 1.4 if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0)
1284 gbeauche 1.29 return 3;
1285 gbeauche 1.1
1286     if (!sigsegv_install_handler(sigsegv_test_handler))
1287 gbeauche 1.29 return 4;
1288 gbeauche 1.1
1289 gbeauche 1.32 #ifdef __GNUC__
1290     b_region = &&L_b_region1;
1291     e_region = &&L_e_region1;
1292     #endif
1293     L_b_region1:
1294     page[REF_INDEX] = REF_VALUE;
1295     if (page[REF_INDEX] != REF_VALUE)
1296     exit(20);
1297     page[REF_INDEX] = REF_VALUE;
1298     L_e_region1:
1299    
1300 gbeauche 1.1 if (handler_called != 1)
1301 gbeauche 1.29 return 5;
1302 gbeauche 1.10
1303     #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
1304     if (!sigsegv_install_handler(sigsegv_insn_handler))
1305 gbeauche 1.29 return 6;
1306 gbeauche 1.10
1307 gbeauche 1.17 if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0)
1308 gbeauche 1.29 return 7;
1309 gbeauche 1.10
1310     for (int i = 0; i < page_size; i++)
1311     page[i] = (i + 1) % page_size;
1312    
1313     if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0)
1314 gbeauche 1.29 return 8;
1315 gbeauche 1.10
1316     #define TEST_SKIP_INSTRUCTION(TYPE) do { \
1317     const unsigned int TAG = 0x12345678; \
1318     TYPE data = *((TYPE *)(page + sizeof(TYPE))); \
1319     volatile unsigned int effect = data + TAG; \
1320     if (effect != TAG) \
1321 gbeauche 1.29 return 9; \
1322 gbeauche 1.10 } while (0)
1323    
1324 gbeauche 1.28 #ifdef __GNUC__
1325 gbeauche 1.32 b_region = &&L_b_region2;
1326     e_region = &&L_e_region2;
1327 gbeauche 1.28 #endif
1328 gbeauche 1.32 L_b_region2:
1329 gbeauche 1.10 TEST_SKIP_INSTRUCTION(unsigned char);
1330     TEST_SKIP_INSTRUCTION(unsigned short);
1331     TEST_SKIP_INSTRUCTION(unsigned int);
1332 gbeauche 1.32 L_e_region2:
1333 gbeauche 1.10 #endif
1334 gbeauche 1.1
1335 gbeauche 1.4 vm_exit();
1336 gbeauche 1.1 return 0;
1337     }
1338     #endif
1339 gbeauche 1.32
1340    
1341    
1342    
1343    
1344    
1345    
1346    
1347    
1348    
1349    
1350    
1351    
1352    
1353