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 |
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 |
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 |
|
|
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 |
|
|
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(__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 |
+ |
#if defined(__FreeBSD__) |
232 |
+ |
#if (defined(i386) || defined(__i386__)) |
233 |
+ |
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_eip) |
234 |
+ |
#define SIGSEGV_REGISTER_FILE ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */ |
235 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
236 |
+ |
#endif |
237 |
+ |
#endif |
238 |
|
#if defined(__linux__) |
239 |
|
#if (defined(i386) || defined(__i386__)) |
240 |
|
#include <sys/ucontext.h> |
241 |
< |
#define SIGSEGV_FAULT_INSTRUCTION (((ucontext_t *)scp)->uc_mcontext.gregs[14]) /* should use REG_EIP instead */ |
242 |
< |
#define SIGSEGV_REGISTER_FILE (unsigned long *)(((ucontext_t *)scp)->uc_mcontext.gregs) |
241 |
> |
#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 |
|
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
245 |
|
#endif |
246 |
+ |
#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 |
|
#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 |
|
#if (defined(powerpc) || defined(__powerpc__)) |
256 |
|
#include <sys/ucontext.h> |
257 |
< |
#define SIGSEGV_FAULT_INSTRUCTION (((ucontext_t *)scp)->uc_mcontext.regs->nip) |
257 |
> |
#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 |
> |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
261 |
|
#endif |
262 |
|
#endif |
263 |
|
#endif |
269 |
|
#if (defined(i386) || defined(__i386__)) |
270 |
|
#include <asm/sigcontext.h> |
271 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs |
272 |
< |
#define SIGSEGV_FAULT_ADDRESS scs.cr2 |
273 |
< |
#define SIGSEGV_FAULT_INSTRUCTION scs.eip |
274 |
< |
#define SIGSEGV_REGISTER_FILE (unsigned long *)(&scs) |
272 |
> |
#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 |
|
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
278 |
|
#endif |
279 |
|
#if (defined(sparc) || defined(__sparc__)) |
280 |
|
#include <asm/sigcontext.h> |
281 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr |
282 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr |
283 |
|
#define SIGSEGV_FAULT_ADDRESS addr |
284 |
|
#endif |
285 |
|
#if (defined(powerpc) || defined(__powerpc__)) |
286 |
|
#include <asm/sigcontext.h> |
287 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp |
288 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, scp |
289 |
|
#define SIGSEGV_FAULT_ADDRESS scp->regs->dar |
290 |
|
#define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip |
291 |
+ |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr) |
292 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
293 |
|
#endif |
294 |
|
#if (defined(alpha) || defined(__alpha__)) |
295 |
|
#include <asm/sigcontext.h> |
296 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
297 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
298 |
|
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
299 |
|
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_pc |
114 |
– |
|
115 |
– |
// From Boehm's GC 6.0alpha8 |
116 |
– |
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
117 |
– |
{ |
118 |
– |
unsigned int instruction = *((unsigned int *)(scp->sc_pc)); |
119 |
– |
unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f]; |
120 |
– |
fault_address += (signed long)(signed short)(instruction & 0xffff); |
121 |
– |
return (sigsegv_address_t)fault_address; |
122 |
– |
} |
300 |
|
#endif |
301 |
|
#endif |
302 |
|
|
304 |
|
#if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4)) |
305 |
|
#include <ucontext.h> |
306 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
307 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
308 |
|
#define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr |
309 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
310 |
|
#endif |
312 |
|
// HP-UX |
313 |
|
#if (defined(hpux) || defined(__hpux__)) |
314 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
315 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
316 |
|
#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 |
321 |
|
#if defined(__osf__) |
322 |
|
#include <ucontext.h> |
323 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
324 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
325 |
|
#define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0 |
326 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
327 |
|
#endif |
329 |
|
// AIX |
330 |
|
#if defined(_AIX) |
331 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
332 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
333 |
|
#define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr |
334 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
335 |
|
#endif |
336 |
|
|
337 |
< |
// NetBSD or FreeBSD |
338 |
< |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
337 |
> |
// NetBSD |
338 |
> |
#if defined(__NetBSD__) |
339 |
|
#if (defined(m68k) || defined(__m68k__)) |
340 |
|
#include <m68k/frame.h> |
341 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
342 |
< |
#define SIGSEGV_FAULT_ADDRESS ({ \ |
343 |
< |
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 |
< |
}) |
342 |
> |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
343 |
> |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
344 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
345 |
< |
#else |
346 |
< |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, void *scp, char *addr |
347 |
< |
#define SIGSEGV_FAULT_ADDRESS addr |
345 |
> |
|
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 |
> |
#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 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr |
385 |
+ |
#define SIGSEGV_FAULT_ADDRESS addr |
386 |
+ |
#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 |
|
#endif |
390 |
|
#endif |
391 |
|
|
392 |
< |
// MacOS X |
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 |
> |
|
405 |
> |
// 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 |
|
#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 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
417 |
|
#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 |
+ |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->sc_ir, &((unsigned int *) scp->sc_regs)[2] |
421 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
422 |
|
|
423 |
< |
// 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 |
< |
|
423 |
> |
// Use decoding scheme from SheepShaver |
424 |
|
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
425 |
|
{ |
426 |
< |
unsigned int instr = *((unsigned int *) scp->sc_ir); |
427 |
< |
unsigned int * regs = &((unsigned int *) scp->sc_regs)[2]; |
428 |
< |
int disp = 0, tmp; |
429 |
< |
unsigned int baseA = 0, baseB = 0; |
430 |
< |
unsigned int addr, alignmask = 0xFFFFFFFF; |
431 |
< |
|
432 |
< |
switch(EXTRACT_OP1(instr)) { |
433 |
< |
case 38: /* stb */ |
434 |
< |
case 39: /* stbu */ |
435 |
< |
case 54: /* stfd */ |
436 |
< |
case 55: /* stfdu */ |
437 |
< |
case 52: /* stfs */ |
438 |
< |
case 53: /* stfsu */ |
439 |
< |
case 44: /* sth */ |
440 |
< |
case 45: /* sthu */ |
441 |
< |
case 47: /* stmw */ |
442 |
< |
case 36: /* stw */ |
443 |
< |
case 37: /* stwu */ |
444 |
< |
tmp = EXTRACT_REGA(instr); |
445 |
< |
if(tmp > 0) |
446 |
< |
baseA = regs[tmp]; |
447 |
< |
disp = EXTRACT_DISP(instr); |
448 |
< |
break; |
449 |
< |
case 31: |
450 |
< |
switch(EXTRACT_OP2(instr)) { |
451 |
< |
case 86: /* dcbf */ |
452 |
< |
case 54: /* dcbst */ |
453 |
< |
case 1014: /* dcbz */ |
454 |
< |
case 247: /* stbux */ |
455 |
< |
case 215: /* stbx */ |
456 |
< |
case 759: /* stfdux */ |
457 |
< |
case 727: /* stfdx */ |
458 |
< |
case 983: /* stfiwx */ |
459 |
< |
case 695: /* stfsux */ |
460 |
< |
case 663: /* stfsx */ |
461 |
< |
case 918: /* sthbrx */ |
462 |
< |
case 439: /* sthux */ |
463 |
< |
case 407: /* sthx */ |
464 |
< |
case 661: /* stswx */ |
465 |
< |
case 662: /* stwbrx */ |
466 |
< |
case 150: /* stwcx. */ |
467 |
< |
case 183: /* stwux */ |
468 |
< |
case 151: /* stwx */ |
469 |
< |
case 135: /* stvebx */ |
470 |
< |
case 167: /* stvehx */ |
471 |
< |
case 199: /* stvewx */ |
472 |
< |
case 231: /* stvx */ |
473 |
< |
case 487: /* stvxl */ |
474 |
< |
tmp = EXTRACT_REGA(instr); |
475 |
< |
if(tmp > 0) |
476 |
< |
baseA = regs[tmp]; |
477 |
< |
baseB = regs[EXTRACT_REGC(instr)]; |
478 |
< |
/* determine Altivec alignment mask */ |
479 |
< |
switch(EXTRACT_OP2(instr)) { |
480 |
< |
case 167: /* stvehx */ |
481 |
< |
alignmask = 0xFFFFFFFE; |
482 |
< |
break; |
483 |
< |
case 199: /* stvewx */ |
484 |
< |
alignmask = 0xFFFFFFFC; |
485 |
< |
break; |
486 |
< |
case 231: /* stvx */ |
487 |
< |
alignmask = 0xFFFFFFF0; |
488 |
< |
break; |
489 |
< |
case 487: /* stvxl */ |
490 |
< |
alignmask = 0xFFFFFFF0; |
491 |
< |
break; |
492 |
< |
} |
493 |
< |
break; |
494 |
< |
case 725: /* stswi */ |
495 |
< |
tmp = EXTRACT_REGA(instr); |
496 |
< |
if(tmp > 0) |
497 |
< |
baseA = regs[tmp]; |
498 |
< |
break; |
499 |
< |
default: /* ignore instruction */ |
500 |
< |
return 0; |
501 |
< |
break; |
426 |
> |
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 |
> |
} |
433 |
> |
#endif |
434 |
> |
#endif |
435 |
> |
#endif |
436 |
> |
#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 |
> |
#define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP) ((code[0] == KERN_PROTECTION_FAILURE) ? sigsegv_fault_handler(ADDR, IP) : SIGSEGV_RETURN_FAILURE) |
501 |
> |
#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 |
|
} |
282 |
– |
break; |
283 |
– |
default: /* ignore instruction */ |
284 |
– |
return 0; |
285 |
– |
break; |
568 |
|
} |
287 |
– |
|
288 |
– |
addr = (baseA + baseB) + disp; |
289 |
– |
addr &= alignmask; |
290 |
– |
return (sigsegv_address_t)addr; |
569 |
|
} |
570 |
|
#endif |
571 |
|
#endif |
572 |
< |
#endif |
572 |
> |
|
573 |
> |
|
574 |
> |
/* |
575 |
> |
* Instruction skipping |
576 |
> |
*/ |
577 |
|
|
578 |
|
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
579 |
|
// Decode and skip X86 instruction |
591 |
|
X86_REG_EDI = 4 |
592 |
|
}; |
593 |
|
#endif |
594 |
+ |
#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 |
|
// 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) |
638 |
|
return offset; |
639 |
|
} |
640 |
|
|
641 |
< |
static bool ix86_skip_instruction(sigsegv_address_t fault_instruction, unsigned long * regs) |
641 |
> |
static bool ix86_skip_instruction(unsigned int * regs) |
642 |
|
{ |
643 |
< |
unsigned char * eip = (unsigned char *)fault_instruction; |
643 |
> |
unsigned char * eip = (unsigned char *)regs[X86_REG_EIP]; |
644 |
|
|
645 |
|
if (eip == 0) |
646 |
|
return false; |
647 |
|
|
648 |
< |
// Transfer type |
649 |
< |
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; |
648 |
> |
transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN; |
649 |
> |
transfer_size_t transfer_size = SIZE_LONG; |
650 |
|
|
651 |
|
int reg = -1; |
652 |
|
int len = 0; |
660 |
|
|
661 |
|
// Decode instruction |
662 |
|
switch (eip[0]) { |
663 |
+ |
case 0x0f: |
664 |
+ |
switch (eip[1]) { |
665 |
+ |
case 0xb6: // MOVZX r32, r/m8 |
666 |
+ |
case 0xb7: // MOVZX r32, r/m16 |
667 |
+ |
switch (eip[2] & 0xc0) { |
668 |
+ |
case 0x80: |
669 |
+ |
reg = (eip[2] >> 3) & 7; |
670 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
671 |
+ |
break; |
672 |
+ |
case 0x40: |
673 |
+ |
reg = (eip[2] >> 3) & 7; |
674 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
675 |
+ |
break; |
676 |
+ |
case 0x00: |
677 |
+ |
reg = (eip[2] >> 3) & 7; |
678 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
679 |
+ |
break; |
680 |
+ |
} |
681 |
+ |
len += 3 + ix86_step_over_modrm(eip + 2); |
682 |
+ |
break; |
683 |
+ |
} |
684 |
+ |
break; |
685 |
|
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 |
< |
transfer_type = TYPE_LOAD; |
691 |
> |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
692 |
|
break; |
693 |
|
case 0x40: |
694 |
|
reg = (eip[1] >> 3) & 7; |
695 |
< |
transfer_type = TYPE_LOAD; |
695 |
> |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
696 |
|
break; |
697 |
|
case 0x00: |
698 |
|
reg = (eip[1] >> 3) & 7; |
699 |
< |
transfer_type = TYPE_LOAD; |
699 |
> |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
700 |
|
break; |
701 |
|
} |
702 |
|
len += 2 + ix86_step_over_modrm(eip + 1); |
707 |
|
switch (eip[1] & 0xc0) { |
708 |
|
case 0x80: |
709 |
|
reg = (eip[1] >> 3) & 7; |
710 |
< |
transfer_type = TYPE_STORE; |
710 |
> |
transfer_type = SIGSEGV_TRANSFER_STORE; |
711 |
|
break; |
712 |
|
case 0x40: |
713 |
|
reg = (eip[1] >> 3) & 7; |
714 |
< |
transfer_type = TYPE_STORE; |
714 |
> |
transfer_type = SIGSEGV_TRANSFER_STORE; |
715 |
|
break; |
716 |
|
case 0x00: |
717 |
|
reg = (eip[1] >> 3) & 7; |
718 |
< |
transfer_type = TYPE_STORE; |
718 |
> |
transfer_type = SIGSEGV_TRANSFER_STORE; |
719 |
|
break; |
720 |
|
} |
721 |
|
len += 2 + ix86_step_over_modrm(eip + 1); |
722 |
|
break; |
723 |
|
} |
724 |
|
|
725 |
< |
if (transfer_type == TYPE_UNKNOWN) { |
725 |
> |
if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) { |
726 |
|
// Unknown machine code, let it crash. Then patch the decoder |
727 |
|
return false; |
728 |
|
} |
729 |
|
|
730 |
< |
if (transfer_type == TYPE_LOAD && reg != -1) { |
730 |
> |
if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != -1) { |
731 |
|
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 |
753 |
|
#if DEBUG |
754 |
|
printf("%08x: %s %s access", regs[X86_REG_EIP], |
755 |
|
transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long", |
756 |
< |
transfer_type == TYPE_LOAD ? "read" : "write"); |
756 |
> |
transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write"); |
757 |
|
|
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 |
< |
printf(" %s register %%%s", transfer_type == TYPE_LOAD ? "to" : "from", x86_reg_str_map[reg]); |
763 |
> |
printf(" %s register %%%s", transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from", x86_reg_str_map[reg]); |
764 |
|
} |
765 |
|
printf(", %d bytes instruction\n", len); |
766 |
|
#endif |
769 |
|
return true; |
770 |
|
} |
771 |
|
#endif |
772 |
+ |
|
773 |
+ |
// Decode and skip PPC instruction |
774 |
+ |
#if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__)) |
775 |
+ |
static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs) |
776 |
+ |
{ |
777 |
+ |
instruction_t instr; |
778 |
+ |
powerpc_decode_instruction(&instr, *nip_p, regs); |
779 |
+ |
|
780 |
+ |
if (instr.transfer_type == SIGSEGV_TRANSFER_UNKNOWN) { |
781 |
+ |
// Unknown machine code, let it crash. Then patch the decoder |
782 |
+ |
return false; |
783 |
+ |
} |
784 |
+ |
|
785 |
+ |
#if DEBUG |
786 |
+ |
printf("%08x: %s %s access", *nip_p, |
787 |
+ |
instr.transfer_size == SIZE_BYTE ? "byte" : instr.transfer_size == SIZE_WORD ? "word" : "long", |
788 |
+ |
instr.transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write"); |
789 |
+ |
|
790 |
+ |
if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX) |
791 |
+ |
printf(" r%d (ra = %08x)\n", instr.ra, instr.addr); |
792 |
+ |
if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD) |
793 |
+ |
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 |
+ |
if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD) |
799 |
+ |
regs[instr.rd] = 0; |
800 |
+ |
|
801 |
+ |
*nip_p += 4; |
802 |
+ |
return true; |
803 |
+ |
} |
804 |
+ |
#endif |
805 |
|
#endif |
806 |
|
|
807 |
|
// Fallbacks |
808 |
|
#ifndef SIGSEGV_FAULT_INSTRUCTION |
809 |
|
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC |
810 |
|
#endif |
811 |
+ |
#ifndef SIGSEGV_FAULT_HANDLER_ARGLIST_1 |
812 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGLIST_1 SIGSEGV_FAULT_HANDLER_ARGLIST |
813 |
+ |
#endif |
814 |
+ |
#ifndef SIGSEGV_FAULT_HANDLER_INVOKE |
815 |
+ |
#define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP) sigsegv_fault_handler(ADDR, IP) |
816 |
+ |
#endif |
817 |
|
|
818 |
|
// SIGSEGV recovery supported ? |
819 |
|
#if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS) |
825 |
|
* SIGSEGV global handler |
826 |
|
*/ |
827 |
|
|
828 |
< |
#ifdef HAVE_SIGSEGV_RECOVERY |
829 |
< |
static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST) |
828 |
> |
#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 |
> |
static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST_1) |
832 |
|
{ |
833 |
|
sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS; |
834 |
|
sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION; |
488 |
– |
bool fault_recovered = false; |
835 |
|
|
836 |
|
// Call user's handler and reinstall the global handler, if required |
837 |
< |
if (sigsegv_fault_handler(fault_address, fault_instruction)) { |
838 |
< |
#if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL)) |
839 |
< |
sigsegv_do_install_handler(sig); |
837 |
> |
switch (SIGSEGV_FAULT_HANDLER_INVOKE(fault_address, fault_instruction)) { |
838 |
> |
case SIGSEGV_RETURN_SUCCESS: |
839 |
> |
return true; |
840 |
> |
|
841 |
> |
#if HAVE_SIGSEGV_SKIP_INSTRUCTION |
842 |
> |
case SIGSEGV_RETURN_SKIP_INSTRUCTION: |
843 |
> |
// 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 |
> |
break; |
861 |
|
#endif |
495 |
– |
fault_recovered = true; |
862 |
|
} |
863 |
< |
#if HAVE_SIGSEGV_SKIP_INSTRUCTION |
864 |
< |
else if (sigsegv_ignore_fault) { |
865 |
< |
// Call the instruction skipper with the register file available |
866 |
< |
if (SIGSEGV_SKIP_INSTRUCTION(fault_instruction, SIGSEGV_REGISTER_FILE)) |
867 |
< |
fault_recovered = true; |
863 |
> |
|
864 |
> |
// We can't do anything with the fault_address, dump state? |
865 |
> |
if (sigsegv_state_dumper != 0) |
866 |
> |
sigsegv_state_dumper(fault_address, fault_instruction); |
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 |
< |
if (!fault_recovered) { |
1020 |
< |
// FAIL: reinstall default handler for "safe" crash |
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 |
> |
|
1031 |
> |
// Failure: reinstall default handler for "safe" crash |
1032 |
|
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
1033 |
< |
SIGSEGV_ALL_SIGNALS |
1033 |
> |
SIGSEGV_ALL_SIGNALS |
1034 |
|
#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 |
– |
} |
1035 |
|
} |
1036 |
|
#endif |
1037 |
|
|
1045 |
|
{ |
1046 |
|
// Setup SIGSEGV handler to process writes to frame buffer |
1047 |
|
#ifdef HAVE_SIGACTION |
1048 |
< |
struct sigaction vosf_sa; |
1049 |
< |
sigemptyset(&vosf_sa.sa_mask); |
1050 |
< |
vosf_sa.sa_sigaction = sigsegv_handler; |
1051 |
< |
vosf_sa.sa_flags = SA_SIGINFO; |
1052 |
< |
return (sigaction(sig, &vosf_sa, 0) == 0); |
1048 |
> |
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 |
|
#else |
1054 |
|
return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR); |
1055 |
|
#endif |
1061 |
|
{ |
1062 |
|
// Setup SIGSEGV handler to process writes to frame buffer |
1063 |
|
#ifdef HAVE_SIGACTION |
1064 |
< |
struct sigaction vosf_sa; |
1065 |
< |
sigemptyset(&vosf_sa.sa_mask); |
1066 |
< |
vosf_sa.sa_handler = (signal_handler)sigsegv_handler; |
1064 |
> |
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 |
|
#if !EMULATED_68K && defined(__NetBSD__) |
1069 |
< |
sigaddset(&vosf_sa.sa_mask, SIGALRM); |
1070 |
< |
vosf_sa.sa_flags = SA_ONSTACK; |
550 |
< |
#else |
551 |
< |
vosf_sa.sa_flags = 0; |
1069 |
> |
sigaddset(&sigsegv_sa.sa_mask, SIGALRM); |
1070 |
> |
sigsegv_sa.sa_flags |= SA_ONSTACK; |
1071 |
|
#endif |
1072 |
< |
return (sigaction(sig, &vosf_sa, 0) == 0); |
1072 |
> |
return (sigaction(sig, &sigsegv_sa, 0) == 0); |
1073 |
|
#else |
1074 |
|
return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR); |
1075 |
|
#endif |
1076 |
|
} |
1077 |
|
#endif |
1078 |
|
|
1079 |
< |
bool sigsegv_install_handler(sigsegv_fault_handler_t handler) |
1079 |
> |
#if defined(HAVE_MACH_EXCEPTIONS) |
1080 |
> |
static bool sigsegv_do_install_handler(sigsegv_fault_handler_t handler) |
1081 |
|
{ |
1082 |
< |
#ifdef HAVE_SIGSEGV_RECOVERY |
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 |
+ |
bool sigsegv_install_handler(sigsegv_fault_handler_t handler) |
1165 |
+ |
{ |
1166 |
+ |
#if defined(HAVE_SIGSEGV_RECOVERY) |
1167 |
|
bool success = true; |
1168 |
|
#define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig); |
1169 |
|
SIGSEGV_ALL_SIGNALS |
1170 |
|
#undef FAULT_HANDLER |
1171 |
+ |
if (success) |
1172 |
+ |
sigsegv_fault_handler = handler; |
1173 |
|
return success; |
1174 |
+ |
#elif defined(HAVE_MACH_EXCEPTIONS) |
1175 |
+ |
return sigsegv_do_install_handler(handler); |
1176 |
|
#else |
1177 |
|
// FAIL: no siginfo_t nor sigcontext subterfuge is available |
1178 |
|
return false; |
1186 |
|
|
1187 |
|
void sigsegv_deinstall_handler(void) |
1188 |
|
{ |
1189 |
+ |
// 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 |
|
#ifdef HAVE_SIGSEGV_RECOVERY |
1195 |
|
sigsegv_fault_handler = 0; |
1196 |
|
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
1201 |
|
|
1202 |
|
|
1203 |
|
/* |
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 |
– |
/* |
1204 |
|
* Set callback function when we cannot handle the fault |
1205 |
|
*/ |
1206 |
|
|
1221 |
|
#include <sys/mman.h> |
1222 |
|
#include "vm_alloc.h" |
1223 |
|
|
1224 |
+ |
const int REF_INDEX = 123; |
1225 |
+ |
const int REF_VALUE = 45; |
1226 |
+ |
|
1227 |
|
static int page_size; |
1228 |
|
static volatile char * page = 0; |
1229 |
|
static volatile int handler_called = 0; |
1230 |
|
|
1231 |
< |
static bool sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
1231 |
> |
#ifdef __GNUC__ |
1232 |
> |
// Code range where we expect the fault to come from |
1233 |
> |
static void *b_region, *e_region; |
1234 |
> |
#endif |
1235 |
> |
|
1236 |
> |
static sigsegv_return_t sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
1237 |
|
{ |
1238 |
|
handler_called++; |
1239 |
< |
if ((fault_address - 123) != page) |
1240 |
< |
exit(1); |
1239 |
> |
if ((fault_address - REF_INDEX) != page) |
1240 |
> |
exit(10); |
1241 |
> |
#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 |
|
if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0) |
1250 |
< |
exit(1); |
1251 |
< |
return true; |
1250 |
> |
exit(12); |
1251 |
> |
return SIGSEGV_RETURN_SUCCESS; |
1252 |
|
} |
1253 |
|
|
1254 |
|
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
1255 |
< |
static bool sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
1255 |
> |
static sigsegv_return_t sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
1256 |
|
{ |
1257 |
< |
return false; |
1257 |
> |
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 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
1267 |
> |
} |
1268 |
> |
|
1269 |
> |
return SIGSEGV_RETURN_FAILURE; |
1270 |
|
} |
1271 |
|
#endif |
1272 |
|
|
1277 |
|
|
1278 |
|
page_size = getpagesize(); |
1279 |
|
if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED) |
1280 |
< |
return 1; |
1280 |
> |
return 2; |
1281 |
|
|
1282 |
+ |
memset((void *)page, 0, page_size); |
1283 |
|
if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0) |
1284 |
< |
return 1; |
1284 |
> |
return 3; |
1285 |
|
|
1286 |
|
if (!sigsegv_install_handler(sigsegv_test_handler)) |
1287 |
< |
return 1; |
657 |
< |
|
658 |
< |
page[123] = 45; |
659 |
< |
page[123] = 45; |
1287 |
> |
return 4; |
1288 |
|
|
1289 |
+ |
#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 |
|
if (handler_called != 1) |
1301 |
< |
return 1; |
1301 |
> |
return 5; |
1302 |
|
|
1303 |
|
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
1304 |
|
if (!sigsegv_install_handler(sigsegv_insn_handler)) |
1305 |
< |
return 1; |
1305 |
> |
return 6; |
1306 |
|
|
1307 |
< |
if (vm_protect((char *)page, page_size, VM_PAGE_WRITE) < 0) |
1308 |
< |
return 1; |
1307 |
> |
if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0) |
1308 |
> |
return 7; |
1309 |
|
|
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 |
< |
return 1; |
1314 |
> |
return 8; |
1315 |
|
|
677 |
– |
sigsegv_set_ignore_state(true); |
678 |
– |
|
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 |
< |
return 1; \ |
1321 |
> |
return 9; \ |
1322 |
|
} while (0) |
1323 |
|
|
1324 |
+ |
#ifdef __GNUC__ |
1325 |
+ |
b_region = &&L_b_region2; |
1326 |
+ |
e_region = &&L_e_region2; |
1327 |
+ |
#endif |
1328 |
+ |
L_b_region2: |
1329 |
|
TEST_SKIP_INSTRUCTION(unsigned char); |
1330 |
|
TEST_SKIP_INSTRUCTION(unsigned short); |
1331 |
|
TEST_SKIP_INSTRUCTION(unsigned int); |
1332 |
+ |
L_e_region2: |
1333 |
|
#endif |
1334 |
|
|
1335 |
|
vm_exit(); |
1336 |
|
return 0; |
1337 |
|
} |
1338 |
|
#endif |
1339 |
+ |
|
1340 |
+ |
|
1341 |
+ |
|
1342 |
+ |
|
1343 |
+ |
|
1344 |
+ |
|
1345 |
+ |
|
1346 |
+ |
|
1347 |
+ |
|
1348 |
+ |
|
1349 |
+ |
|
1350 |
+ |
|
1351 |
+ |
|
1352 |
+ |
|
1353 |
+ |
|