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 |
* Basilisk II (C) 1997-2002 Christian Bauer |
8 |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
10 |
* it under the terms of the GNU General Public License as published by |
11 |
* the Free Software Foundation; either version 2 of the License, or |
12 |
* (at your option) any later version. |
13 |
* |
14 |
* This program is distributed in the hope that it will be useful, |
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
* GNU General Public License for more details. |
18 |
* |
19 |
* You should have received a copy of the GNU General Public License |
20 |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
*/ |
23 |
|
24 |
#ifdef HAVE_UNISTD_H |
25 |
#include <unistd.h> |
26 |
#endif |
27 |
|
28 |
#ifdef HAVE_CONFIG_H |
29 |
#include "config.h" |
30 |
#endif |
31 |
|
32 |
#include <list> |
33 |
#include <signal.h> |
34 |
#include "sigsegv.h" |
35 |
|
36 |
#ifndef NO_STD_NAMESPACE |
37 |
using std::list; |
38 |
#endif |
39 |
|
40 |
// Return value type of a signal handler (standard type if not defined) |
41 |
#ifndef RETSIGTYPE |
42 |
#define RETSIGTYPE void |
43 |
#endif |
44 |
|
45 |
// Type of the system signal handler |
46 |
typedef RETSIGTYPE (*signal_handler)(int); |
47 |
|
48 |
// Ignore range chain |
49 |
struct ignore_range_t { |
50 |
sigsegv_address_t start; |
51 |
unsigned long length; |
52 |
int transfer_type; |
53 |
}; |
54 |
|
55 |
typedef list<ignore_range_t> ignore_range_list_t; |
56 |
ignore_range_list_t sigsegv_ignore_ranges; |
57 |
|
58 |
// User's SIGSEGV handler |
59 |
static sigsegv_fault_handler_t sigsegv_fault_handler = 0; |
60 |
|
61 |
// Function called to dump state if we can't handle the fault |
62 |
static sigsegv_state_dumper_t sigsegv_state_dumper = 0; |
63 |
|
64 |
// Actual SIGSEGV handler installer |
65 |
static bool sigsegv_do_install_handler(int sig); |
66 |
|
67 |
// Find ignore range matching address |
68 |
static inline ignore_range_list_t::iterator sigsegv_find_ignore_range(sigsegv_address_t address) |
69 |
{ |
70 |
ignore_range_list_t::iterator it; |
71 |
for (it = sigsegv_ignore_ranges.begin(); it != sigsegv_ignore_ranges.end(); it++) |
72 |
if (address >= it->start && address < it->start + it->length) |
73 |
break; |
74 |
return it; |
75 |
} |
76 |
|
77 |
|
78 |
/* |
79 |
* Instruction decoding aids |
80 |
*/ |
81 |
|
82 |
// Transfer size |
83 |
enum transfer_size_t { |
84 |
SIZE_UNKNOWN, |
85 |
SIZE_BYTE, |
86 |
SIZE_WORD, |
87 |
SIZE_LONG |
88 |
}; |
89 |
|
90 |
// Transfer type |
91 |
typedef sigsegv_transfer_type_t transfer_type_t; |
92 |
|
93 |
#if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__)) |
94 |
// Addressing mode |
95 |
enum addressing_mode_t { |
96 |
MODE_UNKNOWN, |
97 |
MODE_NORM, |
98 |
MODE_U, |
99 |
MODE_X, |
100 |
MODE_UX |
101 |
}; |
102 |
|
103 |
// Decoded instruction |
104 |
struct instruction_t { |
105 |
transfer_type_t transfer_type; |
106 |
transfer_size_t transfer_size; |
107 |
addressing_mode_t addr_mode; |
108 |
unsigned int addr; |
109 |
char ra, rd; |
110 |
}; |
111 |
|
112 |
static void powerpc_decode_instruction(instruction_t *instruction, unsigned int nip, unsigned int * gpr) |
113 |
{ |
114 |
// Get opcode and divide into fields |
115 |
unsigned int opcode = *((unsigned int *)nip); |
116 |
unsigned int primop = opcode >> 26; |
117 |
unsigned int exop = (opcode >> 1) & 0x3ff; |
118 |
unsigned int ra = (opcode >> 16) & 0x1f; |
119 |
unsigned int rb = (opcode >> 11) & 0x1f; |
120 |
unsigned int rd = (opcode >> 21) & 0x1f; |
121 |
signed int imm = (signed short)(opcode & 0xffff); |
122 |
|
123 |
// Analyze opcode |
124 |
transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN; |
125 |
transfer_size_t transfer_size = SIZE_UNKNOWN; |
126 |
addressing_mode_t addr_mode = MODE_UNKNOWN; |
127 |
switch (primop) { |
128 |
case 31: |
129 |
switch (exop) { |
130 |
case 23: // lwzx |
131 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_X; break; |
132 |
case 55: // lwzux |
133 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break; |
134 |
case 87: // lbzx |
135 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break; |
136 |
case 119: // lbzux |
137 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break; |
138 |
case 151: // stwx |
139 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_X; break; |
140 |
case 183: // stwux |
141 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break; |
142 |
case 215: // stbx |
143 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break; |
144 |
case 247: // stbux |
145 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break; |
146 |
case 279: // lhzx |
147 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break; |
148 |
case 311: // lhzux |
149 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break; |
150 |
case 343: // lhax |
151 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break; |
152 |
case 375: // lhaux |
153 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break; |
154 |
case 407: // sthx |
155 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break; |
156 |
case 439: // sthux |
157 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break; |
158 |
} |
159 |
break; |
160 |
|
161 |
case 32: // lwz |
162 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break; |
163 |
case 33: // lwzu |
164 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_U; break; |
165 |
case 34: // lbz |
166 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break; |
167 |
case 35: // lbzu |
168 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break; |
169 |
case 36: // stw |
170 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break; |
171 |
case 37: // stwu |
172 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_U; break; |
173 |
case 38: // stb |
174 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break; |
175 |
case 39: // stbu |
176 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break; |
177 |
case 40: // lhz |
178 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break; |
179 |
case 41: // lhzu |
180 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break; |
181 |
case 42: // lha |
182 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break; |
183 |
case 43: // lhau |
184 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break; |
185 |
case 44: // sth |
186 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break; |
187 |
case 45: // sthu |
188 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break; |
189 |
} |
190 |
|
191 |
// Calculate effective address |
192 |
unsigned int addr = 0; |
193 |
switch (addr_mode) { |
194 |
case MODE_X: |
195 |
case MODE_UX: |
196 |
if (ra == 0) |
197 |
addr = gpr[rb]; |
198 |
else |
199 |
addr = gpr[ra] + gpr[rb]; |
200 |
break; |
201 |
case MODE_NORM: |
202 |
case MODE_U: |
203 |
if (ra == 0) |
204 |
addr = (signed int)(signed short)imm; |
205 |
else |
206 |
addr = gpr[ra] + (signed int)(signed short)imm; |
207 |
break; |
208 |
default: |
209 |
break; |
210 |
} |
211 |
|
212 |
// Commit decoded instruction |
213 |
instruction->addr = addr; |
214 |
instruction->addr_mode = addr_mode; |
215 |
instruction->transfer_type = transfer_type; |
216 |
instruction->transfer_size = transfer_size; |
217 |
instruction->ra = ra; |
218 |
instruction->rd = rd; |
219 |
} |
220 |
#endif |
221 |
|
222 |
|
223 |
/* |
224 |
* OS-dependant SIGSEGV signals support section |
225 |
*/ |
226 |
|
227 |
#if HAVE_SIGINFO_T |
228 |
// Generic extended signal handler |
229 |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
230 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
231 |
#else |
232 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
233 |
#endif |
234 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp |
235 |
#define SIGSEGV_FAULT_ADDRESS sip->si_addr |
236 |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
237 |
#if (defined(i386) || defined(__i386__)) |
238 |
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_eip) |
239 |
#define SIGSEGV_REGISTER_FILE ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */ |
240 |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
241 |
#endif |
242 |
#endif |
243 |
#if defined(__linux__) |
244 |
#if (defined(i386) || defined(__i386__)) |
245 |
#include <sys/ucontext.h> |
246 |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
247 |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[14] /* should use REG_EIP instead */ |
248 |
#define SIGSEGV_REGISTER_FILE (unsigned int *)SIGSEGV_CONTEXT_REGS |
249 |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
250 |
#endif |
251 |
#if (defined(x86_64) || defined(__x86_64__)) |
252 |
#include <sys/ucontext.h> |
253 |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
254 |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[16] /* should use REG_RIP instead */ |
255 |
#define SIGSEGV_REGISTER_FILE (unsigned long *)SIGSEGV_CONTEXT_REGS |
256 |
#endif |
257 |
#if (defined(ia64) || defined(__ia64__)) |
258 |
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */ |
259 |
#endif |
260 |
#if (defined(powerpc) || defined(__powerpc__)) |
261 |
#include <sys/ucontext.h> |
262 |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.regs) |
263 |
#define SIGSEGV_FAULT_INSTRUCTION (SIGSEGV_CONTEXT_REGS->nip) |
264 |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr) |
265 |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
266 |
#endif |
267 |
#endif |
268 |
#endif |
269 |
|
270 |
#if HAVE_SIGCONTEXT_SUBTERFUGE |
271 |
// Linux kernels prior to 2.4 ? |
272 |
#if defined(__linux__) |
273 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
274 |
#if (defined(i386) || defined(__i386__)) |
275 |
#include <asm/sigcontext.h> |
276 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs |
277 |
#define SIGSEGV_FAULT_ADDRESS scs.cr2 |
278 |
#define SIGSEGV_FAULT_INSTRUCTION scs.eip |
279 |
#define SIGSEGV_REGISTER_FILE (unsigned int *)(&scs) |
280 |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
281 |
#endif |
282 |
#if (defined(sparc) || defined(__sparc__)) |
283 |
#include <asm/sigcontext.h> |
284 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr |
285 |
#define SIGSEGV_FAULT_ADDRESS addr |
286 |
#endif |
287 |
#if (defined(powerpc) || defined(__powerpc__)) |
288 |
#include <asm/sigcontext.h> |
289 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp |
290 |
#define SIGSEGV_FAULT_ADDRESS scp->regs->dar |
291 |
#define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip |
292 |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr) |
293 |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
294 |
#endif |
295 |
#if (defined(alpha) || defined(__alpha__)) |
296 |
#include <asm/sigcontext.h> |
297 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
298 |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
299 |
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_pc |
300 |
|
301 |
// From Boehm's GC 6.0alpha8 |
302 |
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
303 |
{ |
304 |
unsigned int instruction = *((unsigned int *)(scp->sc_pc)); |
305 |
unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f]; |
306 |
fault_address += (signed long)(signed short)(instruction & 0xffff); |
307 |
return (sigsegv_address_t)fault_address; |
308 |
} |
309 |
#endif |
310 |
#endif |
311 |
|
312 |
// Irix 5 or 6 on MIPS |
313 |
#if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4)) |
314 |
#include <ucontext.h> |
315 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
316 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr |
317 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
318 |
#endif |
319 |
|
320 |
// HP-UX |
321 |
#if (defined(hpux) || defined(__hpux__)) |
322 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
323 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_sl.sl_ss.ss_narrow.ss_cr21 |
324 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS) |
325 |
#endif |
326 |
|
327 |
// OSF/1 on Alpha |
328 |
#if defined(__osf__) |
329 |
#include <ucontext.h> |
330 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
331 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0 |
332 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
333 |
#endif |
334 |
|
335 |
// AIX |
336 |
#if defined(_AIX) |
337 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
338 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr |
339 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
340 |
#endif |
341 |
|
342 |
// NetBSD or FreeBSD |
343 |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
344 |
#if (defined(m68k) || defined(__m68k__)) |
345 |
#include <m68k/frame.h> |
346 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
347 |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
348 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
349 |
|
350 |
// Use decoding scheme from BasiliskII/m68k native |
351 |
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
352 |
{ |
353 |
struct sigstate { |
354 |
int ss_flags; |
355 |
struct frame ss_frame; |
356 |
}; |
357 |
struct sigstate *state = (struct sigstate *)scp->sc_ap; |
358 |
char *fault_addr; |
359 |
switch (state->ss_frame.f_format) { |
360 |
case 7: /* 68040 access error */ |
361 |
/* "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown */ |
362 |
fault_addr = state->ss_frame.f_fmt7.f_fa; |
363 |
break; |
364 |
default: |
365 |
fault_addr = (char *)code; |
366 |
break; |
367 |
} |
368 |
return (sigsegv_address_t)fault_addr; |
369 |
} |
370 |
#else |
371 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, void *scp, char *addr |
372 |
#define SIGSEGV_FAULT_ADDRESS addr |
373 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
374 |
#endif |
375 |
#endif |
376 |
|
377 |
// MacOS X |
378 |
#if defined(__APPLE__) && defined(__MACH__) |
379 |
#if (defined(ppc) || defined(__ppc__)) |
380 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
381 |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
382 |
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_ir |
383 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
384 |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->sc_ir, &((unsigned int *) scp->sc_regs)[2] |
385 |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
386 |
|
387 |
// Use decoding scheme from SheepShaver |
388 |
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
389 |
{ |
390 |
unsigned int nip = (unsigned int) scp->sc_ir; |
391 |
unsigned int * gpr = &((unsigned int *) scp->sc_regs)[2]; |
392 |
instruction_t instr; |
393 |
|
394 |
powerpc_decode_instruction(&instr, nip, gpr); |
395 |
return (sigsegv_address_t)instr.addr; |
396 |
} |
397 |
#endif |
398 |
#endif |
399 |
#endif |
400 |
|
401 |
|
402 |
/* |
403 |
* Instruction skipping |
404 |
*/ |
405 |
|
406 |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
407 |
// Decode and skip X86 instruction |
408 |
#if (defined(i386) || defined(__i386__)) |
409 |
#if defined(__linux__) |
410 |
enum { |
411 |
X86_REG_EIP = 14, |
412 |
X86_REG_EAX = 11, |
413 |
X86_REG_ECX = 10, |
414 |
X86_REG_EDX = 9, |
415 |
X86_REG_EBX = 8, |
416 |
X86_REG_ESP = 7, |
417 |
X86_REG_EBP = 6, |
418 |
X86_REG_ESI = 5, |
419 |
X86_REG_EDI = 4 |
420 |
}; |
421 |
#endif |
422 |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
423 |
enum { |
424 |
X86_REG_EIP = 10, |
425 |
X86_REG_EAX = 7, |
426 |
X86_REG_ECX = 6, |
427 |
X86_REG_EDX = 5, |
428 |
X86_REG_EBX = 4, |
429 |
X86_REG_ESP = 13, |
430 |
X86_REG_EBP = 2, |
431 |
X86_REG_ESI = 1, |
432 |
X86_REG_EDI = 0 |
433 |
}; |
434 |
#endif |
435 |
// FIXME: this is partly redundant with the instruction decoding phase |
436 |
// to discover transfer type and register number |
437 |
static inline int ix86_step_over_modrm(unsigned char * p) |
438 |
{ |
439 |
int mod = (p[0] >> 6) & 3; |
440 |
int rm = p[0] & 7; |
441 |
int offset = 0; |
442 |
|
443 |
// ModR/M Byte |
444 |
switch (mod) { |
445 |
case 0: // [reg] |
446 |
if (rm == 5) return 4; // disp32 |
447 |
break; |
448 |
case 1: // disp8[reg] |
449 |
offset = 1; |
450 |
break; |
451 |
case 2: // disp32[reg] |
452 |
offset = 4; |
453 |
break; |
454 |
case 3: // register |
455 |
return 0; |
456 |
} |
457 |
|
458 |
// SIB Byte |
459 |
if (rm == 4) { |
460 |
if (mod == 0 && (p[1] & 7) == 5) |
461 |
offset = 5; // disp32[index] |
462 |
else |
463 |
offset++; |
464 |
} |
465 |
|
466 |
return offset; |
467 |
} |
468 |
|
469 |
static bool ix86_skip_instruction(unsigned int * regs) |
470 |
{ |
471 |
unsigned char * eip = (unsigned char *)regs[X86_REG_EIP]; |
472 |
|
473 |
if (eip == 0) |
474 |
return false; |
475 |
|
476 |
transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN; |
477 |
transfer_size_t transfer_size = SIZE_LONG; |
478 |
|
479 |
int reg = -1; |
480 |
int len = 0; |
481 |
|
482 |
// Operand size prefix |
483 |
if (*eip == 0x66) { |
484 |
eip++; |
485 |
len++; |
486 |
transfer_size = SIZE_WORD; |
487 |
} |
488 |
|
489 |
// Decode instruction |
490 |
switch (eip[0]) { |
491 |
case 0x0f: |
492 |
switch (eip[1]) { |
493 |
case 0xb6: // MOVZX r32, r/m8 |
494 |
case 0xb7: // MOVZX r32, r/m16 |
495 |
switch (eip[2] & 0xc0) { |
496 |
case 0x80: |
497 |
reg = (eip[2] >> 3) & 7; |
498 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
499 |
break; |
500 |
case 0x40: |
501 |
reg = (eip[2] >> 3) & 7; |
502 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
503 |
break; |
504 |
case 0x00: |
505 |
reg = (eip[2] >> 3) & 7; |
506 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
507 |
break; |
508 |
} |
509 |
len += 3 + ix86_step_over_modrm(eip + 2); |
510 |
break; |
511 |
} |
512 |
break; |
513 |
case 0x8a: // MOV r8, r/m8 |
514 |
transfer_size = SIZE_BYTE; |
515 |
case 0x8b: // MOV r32, r/m32 (or 16-bit operation) |
516 |
switch (eip[1] & 0xc0) { |
517 |
case 0x80: |
518 |
reg = (eip[1] >> 3) & 7; |
519 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
520 |
break; |
521 |
case 0x40: |
522 |
reg = (eip[1] >> 3) & 7; |
523 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
524 |
break; |
525 |
case 0x00: |
526 |
reg = (eip[1] >> 3) & 7; |
527 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
528 |
break; |
529 |
} |
530 |
len += 2 + ix86_step_over_modrm(eip + 1); |
531 |
break; |
532 |
case 0x88: // MOV r/m8, r8 |
533 |
transfer_size = SIZE_BYTE; |
534 |
case 0x89: // MOV r/m32, r32 (or 16-bit operation) |
535 |
switch (eip[1] & 0xc0) { |
536 |
case 0x80: |
537 |
reg = (eip[1] >> 3) & 7; |
538 |
transfer_type = SIGSEGV_TRANSFER_STORE; |
539 |
break; |
540 |
case 0x40: |
541 |
reg = (eip[1] >> 3) & 7; |
542 |
transfer_type = SIGSEGV_TRANSFER_STORE; |
543 |
break; |
544 |
case 0x00: |
545 |
reg = (eip[1] >> 3) & 7; |
546 |
transfer_type = SIGSEGV_TRANSFER_STORE; |
547 |
break; |
548 |
} |
549 |
len += 2 + ix86_step_over_modrm(eip + 1); |
550 |
break; |
551 |
} |
552 |
|
553 |
if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) { |
554 |
// Unknown machine code, let it crash. Then patch the decoder |
555 |
return false; |
556 |
} |
557 |
|
558 |
if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != -1) { |
559 |
static const int x86_reg_map[8] = { |
560 |
X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX, |
561 |
X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI |
562 |
}; |
563 |
|
564 |
if (reg < 0 || reg >= 8) |
565 |
return false; |
566 |
|
567 |
int rloc = x86_reg_map[reg]; |
568 |
switch (transfer_size) { |
569 |
case SIZE_BYTE: |
570 |
regs[rloc] = (regs[rloc] & ~0xff); |
571 |
break; |
572 |
case SIZE_WORD: |
573 |
regs[rloc] = (regs[rloc] & ~0xffff); |
574 |
break; |
575 |
case SIZE_LONG: |
576 |
regs[rloc] = 0; |
577 |
break; |
578 |
} |
579 |
} |
580 |
|
581 |
#if DEBUG |
582 |
printf("%08x: %s %s access", regs[X86_REG_EIP], |
583 |
transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long", |
584 |
transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write"); |
585 |
|
586 |
if (reg != -1) { |
587 |
static const char * x86_reg_str_map[8] = { |
588 |
"eax", "ecx", "edx", "ebx", |
589 |
"esp", "ebp", "esi", "edi" |
590 |
}; |
591 |
printf(" %s register %%%s", transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from", x86_reg_str_map[reg]); |
592 |
} |
593 |
printf(", %d bytes instruction\n", len); |
594 |
#endif |
595 |
|
596 |
regs[X86_REG_EIP] += len; |
597 |
return true; |
598 |
} |
599 |
#endif |
600 |
|
601 |
// Decode and skip PPC instruction |
602 |
#if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__)) |
603 |
static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs) |
604 |
{ |
605 |
instruction_t instr; |
606 |
powerpc_decode_instruction(&instr, *nip_p, regs); |
607 |
|
608 |
if (instr.transfer_type == SIGSEGV_TRANSFER_UNKNOWN) { |
609 |
// Unknown machine code, let it crash. Then patch the decoder |
610 |
return false; |
611 |
} |
612 |
|
613 |
ignore_range_list_t::iterator it = sigsegv_find_ignore_range((sigsegv_address_t)instr.addr); |
614 |
if (it == sigsegv_ignore_ranges.end() || ((it->transfer_type & instr.transfer_type) != instr.transfer_type)) { |
615 |
// Address doesn't fall into ignore ranges list, let it crash. |
616 |
return false; |
617 |
} |
618 |
|
619 |
#if DEBUG |
620 |
printf("%08x: %s %s access", *nip_p, |
621 |
instr.transfer_size == SIZE_BYTE ? "byte" : instr.transfer_size == SIZE_WORD ? "word" : "long", |
622 |
instr.transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write"); |
623 |
|
624 |
if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX) |
625 |
printf(" r%d (ra = %08x)\n", instr.ra, instr.addr); |
626 |
if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD) |
627 |
printf(" r%d (rd = 0)\n", instr.rd); |
628 |
#endif |
629 |
|
630 |
if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX) |
631 |
regs[instr.ra] = instr.addr; |
632 |
if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD) |
633 |
regs[instr.rd] = 0; |
634 |
|
635 |
*nip_p += 4; |
636 |
return true; |
637 |
} |
638 |
#endif |
639 |
#endif |
640 |
|
641 |
// Fallbacks |
642 |
#ifndef SIGSEGV_FAULT_INSTRUCTION |
643 |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC |
644 |
#endif |
645 |
|
646 |
// SIGSEGV recovery supported ? |
647 |
#if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS) |
648 |
#define HAVE_SIGSEGV_RECOVERY |
649 |
#endif |
650 |
|
651 |
|
652 |
/* |
653 |
* SIGSEGV global handler |
654 |
*/ |
655 |
|
656 |
#ifdef HAVE_SIGSEGV_RECOVERY |
657 |
static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST) |
658 |
{ |
659 |
sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS; |
660 |
sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION; |
661 |
bool fault_recovered = false; |
662 |
|
663 |
// Call user's handler and reinstall the global handler, if required |
664 |
if (sigsegv_fault_handler(fault_address, fault_instruction)) { |
665 |
#if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL)) |
666 |
sigsegv_do_install_handler(sig); |
667 |
#endif |
668 |
fault_recovered = true; |
669 |
} |
670 |
#if HAVE_SIGSEGV_SKIP_INSTRUCTION |
671 |
else if (sigsegv_ignore_ranges.size() > 0) { |
672 |
// Call the instruction skipper with the register file available |
673 |
if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE)) |
674 |
fault_recovered = true; |
675 |
} |
676 |
#endif |
677 |
|
678 |
if (!fault_recovered) { |
679 |
// FAIL: reinstall default handler for "safe" crash |
680 |
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
681 |
SIGSEGV_ALL_SIGNALS |
682 |
#undef FAULT_HANDLER |
683 |
|
684 |
// We can't do anything with the fault_address, dump state? |
685 |
if (sigsegv_state_dumper != 0) |
686 |
sigsegv_state_dumper(fault_address, fault_instruction); |
687 |
} |
688 |
} |
689 |
#endif |
690 |
|
691 |
|
692 |
/* |
693 |
* SIGSEGV handler initialization |
694 |
*/ |
695 |
|
696 |
#if defined(HAVE_SIGINFO_T) |
697 |
static bool sigsegv_do_install_handler(int sig) |
698 |
{ |
699 |
// Setup SIGSEGV handler to process writes to frame buffer |
700 |
#ifdef HAVE_SIGACTION |
701 |
struct sigaction sigsegv_sa; |
702 |
sigemptyset(&sigsegv_sa.sa_mask); |
703 |
sigsegv_sa.sa_sigaction = sigsegv_handler; |
704 |
sigsegv_sa.sa_flags = SA_SIGINFO; |
705 |
return (sigaction(sig, &sigsegv_sa, 0) == 0); |
706 |
#else |
707 |
return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR); |
708 |
#endif |
709 |
} |
710 |
#endif |
711 |
|
712 |
#if defined(HAVE_SIGCONTEXT_SUBTERFUGE) |
713 |
static bool sigsegv_do_install_handler(int sig) |
714 |
{ |
715 |
// Setup SIGSEGV handler to process writes to frame buffer |
716 |
#ifdef HAVE_SIGACTION |
717 |
struct sigaction sigsegv_sa; |
718 |
sigemptyset(&sigsegv_sa.sa_mask); |
719 |
sigsegv_sa.sa_handler = (signal_handler)sigsegv_handler; |
720 |
sigsegv_sa.sa_flags = 0; |
721 |
#if !EMULATED_68K && defined(__NetBSD__) |
722 |
sigaddset(&sigsegv_sa.sa_mask, SIGALRM); |
723 |
sigsegv_sa.sa_flags |= SA_ONSTACK; |
724 |
#endif |
725 |
return (sigaction(sig, &sigsegv_sa, 0) == 0); |
726 |
#else |
727 |
return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR); |
728 |
#endif |
729 |
} |
730 |
#endif |
731 |
|
732 |
bool sigsegv_install_handler(sigsegv_fault_handler_t handler) |
733 |
{ |
734 |
#ifdef HAVE_SIGSEGV_RECOVERY |
735 |
sigsegv_fault_handler = handler; |
736 |
bool success = true; |
737 |
#define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig); |
738 |
SIGSEGV_ALL_SIGNALS |
739 |
#undef FAULT_HANDLER |
740 |
return success; |
741 |
#else |
742 |
// FAIL: no siginfo_t nor sigcontext subterfuge is available |
743 |
return false; |
744 |
#endif |
745 |
} |
746 |
|
747 |
|
748 |
/* |
749 |
* SIGSEGV handler deinitialization |
750 |
*/ |
751 |
|
752 |
void sigsegv_deinstall_handler(void) |
753 |
{ |
754 |
#ifdef HAVE_SIGSEGV_RECOVERY |
755 |
sigsegv_fault_handler = 0; |
756 |
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
757 |
SIGSEGV_ALL_SIGNALS |
758 |
#undef FAULT_HANDLER |
759 |
#endif |
760 |
} |
761 |
|
762 |
|
763 |
/* |
764 |
* Add SIGSEGV ignore range |
765 |
*/ |
766 |
|
767 |
void sigsegv_add_ignore_range(sigsegv_address_t address, unsigned long length, int transfer_type) |
768 |
{ |
769 |
ignore_range_t ignore_range; |
770 |
ignore_range.start = address; |
771 |
ignore_range.length = length; |
772 |
ignore_range.transfer_type = transfer_type; |
773 |
sigsegv_ignore_ranges.push_front(ignore_range); |
774 |
} |
775 |
|
776 |
|
777 |
/* |
778 |
* Remove SIGSEGV ignore range. Range must match installed one, otherwise FALSE is returned. |
779 |
*/ |
780 |
|
781 |
bool sigsegv_remove_ignore_range(sigsegv_address_t address, unsigned long length, int transfer_type) |
782 |
{ |
783 |
ignore_range_list_t::iterator it; |
784 |
for (it = sigsegv_ignore_ranges.begin(); it != sigsegv_ignore_ranges.end(); it++) |
785 |
if (it->start == address && it->length == length && ((it->transfer_type & transfer_type) == transfer_type)) |
786 |
break; |
787 |
|
788 |
if (it != sigsegv_ignore_ranges.end()) { |
789 |
if (it->transfer_type != transfer_type) |
790 |
it->transfer_type &= ~transfer_type; |
791 |
else |
792 |
sigsegv_ignore_ranges.erase(it); |
793 |
return true; |
794 |
} |
795 |
|
796 |
return false; |
797 |
} |
798 |
|
799 |
|
800 |
/* |
801 |
* Set callback function when we cannot handle the fault |
802 |
*/ |
803 |
|
804 |
void sigsegv_set_dump_state(sigsegv_state_dumper_t handler) |
805 |
{ |
806 |
sigsegv_state_dumper = handler; |
807 |
} |
808 |
|
809 |
|
810 |
/* |
811 |
* Test program used for configure/test |
812 |
*/ |
813 |
|
814 |
#ifdef CONFIGURE_TEST_SIGSEGV_RECOVERY |
815 |
#include <stdio.h> |
816 |
#include <stdlib.h> |
817 |
#include <fcntl.h> |
818 |
#include <sys/mman.h> |
819 |
#include "vm_alloc.h" |
820 |
|
821 |
static int page_size; |
822 |
static volatile char * page = 0; |
823 |
static volatile int handler_called = 0; |
824 |
|
825 |
static bool sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
826 |
{ |
827 |
handler_called++; |
828 |
if ((fault_address - 123) != page) |
829 |
exit(1); |
830 |
if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0) |
831 |
exit(1); |
832 |
return true; |
833 |
} |
834 |
|
835 |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
836 |
static bool sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
837 |
{ |
838 |
return false; |
839 |
} |
840 |
#endif |
841 |
|
842 |
int main(void) |
843 |
{ |
844 |
if (vm_init() < 0) |
845 |
return 1; |
846 |
|
847 |
page_size = getpagesize(); |
848 |
if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED) |
849 |
return 1; |
850 |
|
851 |
if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0) |
852 |
return 1; |
853 |
|
854 |
if (!sigsegv_install_handler(sigsegv_test_handler)) |
855 |
return 1; |
856 |
|
857 |
page[123] = 45; |
858 |
page[123] = 45; |
859 |
|
860 |
if (handler_called != 1) |
861 |
return 1; |
862 |
|
863 |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
864 |
if (!sigsegv_install_handler(sigsegv_insn_handler)) |
865 |
return 1; |
866 |
|
867 |
if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0) |
868 |
return 1; |
869 |
|
870 |
for (int i = 0; i < page_size; i++) |
871 |
page[i] = (i + 1) % page_size; |
872 |
|
873 |
if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0) |
874 |
return 1; |
875 |
|
876 |
sigsegv_add_ignore_range((char *)page, page_size, SIGSEGV_TRANSFER_LOAD | SIGSEGV_TRANSFER_STORE); |
877 |
|
878 |
#define TEST_SKIP_INSTRUCTION(TYPE) do { \ |
879 |
const unsigned int TAG = 0x12345678; \ |
880 |
TYPE data = *((TYPE *)(page + sizeof(TYPE))); \ |
881 |
volatile unsigned int effect = data + TAG; \ |
882 |
if (effect != TAG) \ |
883 |
return 1; \ |
884 |
} while (0) |
885 |
|
886 |
TEST_SKIP_INSTRUCTION(unsigned char); |
887 |
TEST_SKIP_INSTRUCTION(unsigned short); |
888 |
TEST_SKIP_INSTRUCTION(unsigned int); |
889 |
#endif |
890 |
|
891 |
vm_exit(); |
892 |
return 0; |
893 |
} |
894 |
#endif |