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