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 <signal.h> |
33 |
#include "sigsegv.h" |
34 |
|
35 |
// Return value type of a signal handler (standard type if not defined) |
36 |
#ifndef RETSIGTYPE |
37 |
#define RETSIGTYPE void |
38 |
#endif |
39 |
|
40 |
// Type of the system signal handler |
41 |
typedef RETSIGTYPE (*signal_handler)(int); |
42 |
|
43 |
// Is the fault to be ignored? |
44 |
static bool sigsegv_ignore_fault = false; |
45 |
|
46 |
// User's SIGSEGV handler |
47 |
static sigsegv_handler_t sigsegv_user_handler = 0; |
48 |
|
49 |
// Function called to dump state if we can't handle the fault |
50 |
static sigsegv_handler_t sigsegv_dump_state = 0; |
51 |
|
52 |
// Actual SIGSEGV handler installer |
53 |
static bool sigsegv_do_install_handler(int sig); |
54 |
|
55 |
|
56 |
/* |
57 |
* OS-dependant SIGSEGV signals support section |
58 |
*/ |
59 |
|
60 |
#if HAVE_SIGINFO_T |
61 |
// Generic extended signal handler |
62 |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
63 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
64 |
#else |
65 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
66 |
#endif |
67 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp |
68 |
#define SIGSEGV_FAULT_ADDRESS sip->si_addr |
69 |
#if defined(__linux__) |
70 |
#if (defined(i386) || defined(__i386__)) |
71 |
#include <sys/ucontext.h> |
72 |
#define SIGSEGV_FAULT_INSTRUCTION (((ucontext_t *)scp)->uc_mcontext.gregs[14]) /* should use REG_EIP instead */ |
73 |
#define SIGSEGV_REGISTER_FILE (unsigned long *)(((ucontext_t *)scp)->uc_mcontext.gregs) |
74 |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
75 |
#endif |
76 |
#if (defined(ia64) || defined(__ia64__)) |
77 |
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */ |
78 |
#endif |
79 |
#if (defined(powerpc) || defined(__powerpc__)) |
80 |
#include <sys/ucontext.h> |
81 |
#define SIGSEGV_FAULT_INSTRUCTION (((ucontext_t *)scp)->uc_mcontext.regs->nip) |
82 |
#endif |
83 |
#endif |
84 |
#endif |
85 |
|
86 |
#if HAVE_SIGCONTEXT_SUBTERFUGE |
87 |
// Linux kernels prior to 2.4 ? |
88 |
#if defined(__linux__) |
89 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
90 |
#if (defined(i386) || defined(__i386__)) |
91 |
#include <asm/sigcontext.h> |
92 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs |
93 |
#define SIGSEGV_FAULT_ADDRESS scs.cr2 |
94 |
#define SIGSEGV_FAULT_INSTRUCTION scs.eip |
95 |
#define SIGSEGV_REGISTER_FILE (unsigned long *)(&scs) |
96 |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
97 |
#endif |
98 |
#if (defined(sparc) || defined(__sparc__)) |
99 |
#include <asm/sigcontext.h> |
100 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr |
101 |
#define SIGSEGV_FAULT_ADDRESS addr |
102 |
#endif |
103 |
#if (defined(powerpc) || defined(__powerpc__)) |
104 |
#include <asm/sigcontext.h> |
105 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp |
106 |
#define SIGSEGV_FAULT_ADDRESS scp->regs->dar |
107 |
#define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip |
108 |
#endif |
109 |
#if (defined(alpha) || defined(__alpha__)) |
110 |
#include <asm/sigcontext.h> |
111 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
112 |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
113 |
#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 |
} |
123 |
#endif |
124 |
#endif |
125 |
|
126 |
// Irix 5 or 6 on MIPS |
127 |
#if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4)) |
128 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
129 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr |
130 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
131 |
#endif |
132 |
|
133 |
// OSF/1 on Alpha |
134 |
#if defined(__osf__) |
135 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
136 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0 |
137 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
138 |
#endif |
139 |
|
140 |
// AIX |
141 |
#if defined(_AIX) |
142 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
143 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr |
144 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
145 |
#endif |
146 |
|
147 |
// NetBSD or FreeBSD |
148 |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
149 |
#if (defined(m68k) || defined(__m68k__)) |
150 |
#include <m68k/frame.h> |
151 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
152 |
#define SIGSEGV_FAULT_ADDRESS ({ \ |
153 |
struct sigstate { \ |
154 |
int ss_flags; \ |
155 |
struct frame ss_frame; \ |
156 |
}; \ |
157 |
struct sigstate *state = (struct sigstate *)scp->sc_ap; \ |
158 |
char *fault_addr; \ |
159 |
switch (state->ss_frame.f_format) { \ |
160 |
case 7: /* 68040 access error */ \ |
161 |
/* "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown */ \ |
162 |
fault_addr = state->ss_frame.f_fmt7.f_fa; \ |
163 |
break; \ |
164 |
default: \ |
165 |
fault_addr = (char *)code; \ |
166 |
break; \ |
167 |
} \ |
168 |
fault_addr; \ |
169 |
}) |
170 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
171 |
#else |
172 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, void *scp, char *addr |
173 |
#define SIGSEGV_FAULT_ADDRESS addr |
174 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
175 |
#endif |
176 |
#endif |
177 |
|
178 |
// MacOS X |
179 |
#if defined(__APPLE__) && defined(__MACH__) |
180 |
#if (defined(ppc) || defined(__ppc__)) |
181 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
182 |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
183 |
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_ir |
184 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
185 |
|
186 |
// From Boehm's GC 6.0alpha8 |
187 |
#define EXTRACT_OP1(iw) (((iw) & 0xFC000000) >> 26) |
188 |
#define EXTRACT_OP2(iw) (((iw) & 0x000007FE) >> 1) |
189 |
#define EXTRACT_REGA(iw) (((iw) & 0x001F0000) >> 16) |
190 |
#define EXTRACT_REGB(iw) (((iw) & 0x03E00000) >> 21) |
191 |
#define EXTRACT_REGC(iw) (((iw) & 0x0000F800) >> 11) |
192 |
#define EXTRACT_DISP(iw) ((short *) &(iw))[1] |
193 |
|
194 |
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
195 |
{ |
196 |
unsigned int instr = *((unsigned int *) scp->sc_ir); |
197 |
unsigned int * regs = &((unsigned int *) scp->sc_regs)[2]; |
198 |
int disp = 0, tmp; |
199 |
unsigned int baseA = 0, baseB = 0; |
200 |
unsigned int addr, alignmask = 0xFFFFFFFF; |
201 |
|
202 |
switch(EXTRACT_OP1(instr)) { |
203 |
case 38: /* stb */ |
204 |
case 39: /* stbu */ |
205 |
case 54: /* stfd */ |
206 |
case 55: /* stfdu */ |
207 |
case 52: /* stfs */ |
208 |
case 53: /* stfsu */ |
209 |
case 44: /* sth */ |
210 |
case 45: /* sthu */ |
211 |
case 47: /* stmw */ |
212 |
case 36: /* stw */ |
213 |
case 37: /* stwu */ |
214 |
tmp = EXTRACT_REGA(instr); |
215 |
if(tmp > 0) |
216 |
baseA = regs[tmp]; |
217 |
disp = EXTRACT_DISP(instr); |
218 |
break; |
219 |
case 31: |
220 |
switch(EXTRACT_OP2(instr)) { |
221 |
case 86: /* dcbf */ |
222 |
case 54: /* dcbst */ |
223 |
case 1014: /* dcbz */ |
224 |
case 247: /* stbux */ |
225 |
case 215: /* stbx */ |
226 |
case 759: /* stfdux */ |
227 |
case 727: /* stfdx */ |
228 |
case 983: /* stfiwx */ |
229 |
case 695: /* stfsux */ |
230 |
case 663: /* stfsx */ |
231 |
case 918: /* sthbrx */ |
232 |
case 439: /* sthux */ |
233 |
case 407: /* sthx */ |
234 |
case 661: /* stswx */ |
235 |
case 662: /* stwbrx */ |
236 |
case 150: /* stwcx. */ |
237 |
case 183: /* stwux */ |
238 |
case 151: /* stwx */ |
239 |
case 135: /* stvebx */ |
240 |
case 167: /* stvehx */ |
241 |
case 199: /* stvewx */ |
242 |
case 231: /* stvx */ |
243 |
case 487: /* stvxl */ |
244 |
tmp = EXTRACT_REGA(instr); |
245 |
if(tmp > 0) |
246 |
baseA = regs[tmp]; |
247 |
baseB = regs[EXTRACT_REGC(instr)]; |
248 |
/* determine Altivec alignment mask */ |
249 |
switch(EXTRACT_OP2(instr)) { |
250 |
case 167: /* stvehx */ |
251 |
alignmask = 0xFFFFFFFE; |
252 |
break; |
253 |
case 199: /* stvewx */ |
254 |
alignmask = 0xFFFFFFFC; |
255 |
break; |
256 |
case 231: /* stvx */ |
257 |
alignmask = 0xFFFFFFF0; |
258 |
break; |
259 |
case 487: /* stvxl */ |
260 |
alignmask = 0xFFFFFFF0; |
261 |
break; |
262 |
} |
263 |
break; |
264 |
case 725: /* stswi */ |
265 |
tmp = EXTRACT_REGA(instr); |
266 |
if(tmp > 0) |
267 |
baseA = regs[tmp]; |
268 |
break; |
269 |
default: /* ignore instruction */ |
270 |
return 0; |
271 |
break; |
272 |
} |
273 |
break; |
274 |
default: /* ignore instruction */ |
275 |
return 0; |
276 |
break; |
277 |
} |
278 |
|
279 |
addr = (baseA + baseB) + disp; |
280 |
addr &= alignmask; |
281 |
return (sigsegv_address_t)addr; |
282 |
} |
283 |
#endif |
284 |
#endif |
285 |
#endif |
286 |
|
287 |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
288 |
// Decode and skip X86 instruction |
289 |
#if (defined(i386) || defined(__i386__)) |
290 |
#if defined(__linux__) |
291 |
enum { |
292 |
X86_REG_EIP = 14, |
293 |
X86_REG_EAX = 11, |
294 |
X86_REG_ECX = 10, |
295 |
X86_REG_EDX = 9, |
296 |
X86_REG_EBX = 8, |
297 |
X86_REG_ESP = 7, |
298 |
X86_REG_EBP = 6, |
299 |
X86_REG_ESI = 5, |
300 |
X86_REG_EDI = 4 |
301 |
}; |
302 |
#endif |
303 |
// FIXME: this is partly redundant with the instruction decoding phase |
304 |
// to discover transfer type and register number |
305 |
static inline int ix86_step_over_modrm(unsigned char * p) |
306 |
{ |
307 |
int mod = (p[0] >> 6) & 3; |
308 |
int rm = p[0] & 7; |
309 |
int offset = 0; |
310 |
|
311 |
// ModR/M Byte |
312 |
switch (mod) { |
313 |
case 0: // [reg] |
314 |
if (rm == 5) return 4; // disp32 |
315 |
break; |
316 |
case 1: // disp8[reg] |
317 |
offset = 1; |
318 |
break; |
319 |
case 2: // disp32[reg] |
320 |
offset = 4; |
321 |
break; |
322 |
case 3: // register |
323 |
return 0; |
324 |
} |
325 |
|
326 |
// SIB Byte |
327 |
if (rm == 4) { |
328 |
if (mod == 0 && (p[1] & 7) == 5) |
329 |
offset = 5; // disp32[index] |
330 |
else |
331 |
offset++; |
332 |
} |
333 |
|
334 |
return offset; |
335 |
} |
336 |
|
337 |
static bool ix86_skip_instruction(sigsegv_address_t fault_instruction, unsigned long * regs) |
338 |
{ |
339 |
unsigned char * eip = (unsigned char *)fault_instruction; |
340 |
|
341 |
if (eip == 0) |
342 |
return false; |
343 |
|
344 |
// Transfer type |
345 |
enum { |
346 |
TYPE_UNKNOWN, |
347 |
TYPE_LOAD, |
348 |
TYPE_STORE |
349 |
} transfer_type = TYPE_UNKNOWN; |
350 |
|
351 |
// Transfer size |
352 |
enum { |
353 |
SIZE_BYTE, |
354 |
SIZE_WORD, |
355 |
SIZE_LONG |
356 |
} transfer_size = SIZE_LONG; |
357 |
|
358 |
int reg = -1; |
359 |
int len = 0; |
360 |
|
361 |
// Operand size prefix |
362 |
if (*eip == 0x66) { |
363 |
eip++; |
364 |
len++; |
365 |
transfer_size = SIZE_WORD; |
366 |
} |
367 |
|
368 |
// Decode instruction |
369 |
switch (eip[0]) { |
370 |
case 0x8a: // MOV r8, r/m8 |
371 |
transfer_size = SIZE_BYTE; |
372 |
case 0x8b: // MOV r32, r/m32 (or 16-bit operation) |
373 |
switch (eip[1] & 0xc0) { |
374 |
case 0x80: |
375 |
reg = (eip[1] >> 3) & 7; |
376 |
transfer_type = TYPE_LOAD; |
377 |
break; |
378 |
case 0x40: |
379 |
reg = (eip[1] >> 3) & 7; |
380 |
transfer_type = TYPE_LOAD; |
381 |
break; |
382 |
case 0x00: |
383 |
reg = (eip[1] >> 3) & 7; |
384 |
transfer_type = TYPE_LOAD; |
385 |
break; |
386 |
} |
387 |
len += 2 + ix86_step_over_modrm(eip + 1); |
388 |
break; |
389 |
case 0x88: // MOV r/m8, r8 |
390 |
transfer_size = SIZE_BYTE; |
391 |
case 0x89: // MOV r/m32, r32 (or 16-bit operation) |
392 |
switch (eip[1] & 0xc0) { |
393 |
case 0x80: |
394 |
reg = (eip[1] >> 3) & 7; |
395 |
transfer_type = TYPE_STORE; |
396 |
break; |
397 |
case 0x40: |
398 |
reg = (eip[1] >> 3) & 7; |
399 |
transfer_type = TYPE_STORE; |
400 |
break; |
401 |
case 0x00: |
402 |
reg = (eip[1] >> 3) & 7; |
403 |
transfer_type = TYPE_STORE; |
404 |
break; |
405 |
} |
406 |
len += 2 + ix86_step_over_modrm(eip + 1); |
407 |
break; |
408 |
} |
409 |
|
410 |
if (transfer_type == TYPE_UNKNOWN) { |
411 |
// Unknown machine code, let it crash. Then patch the decoder |
412 |
return false; |
413 |
} |
414 |
|
415 |
if (transfer_type == TYPE_LOAD && reg != -1) { |
416 |
static const int x86_reg_map[8] = { |
417 |
X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX, |
418 |
X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI |
419 |
}; |
420 |
|
421 |
if (reg < 0 || reg >= 8) |
422 |
return false; |
423 |
|
424 |
int rloc = x86_reg_map[reg]; |
425 |
switch (transfer_size) { |
426 |
case SIZE_BYTE: |
427 |
regs[rloc] = (regs[rloc] & ~0xff); |
428 |
break; |
429 |
case SIZE_WORD: |
430 |
regs[rloc] = (regs[rloc] & ~0xffff); |
431 |
break; |
432 |
case SIZE_LONG: |
433 |
regs[rloc] = 0; |
434 |
break; |
435 |
} |
436 |
} |
437 |
|
438 |
#if DEBUG |
439 |
printf("%08x: %s %s access", regs[X86_REG_EIP], |
440 |
transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long", |
441 |
transfer_type == TYPE_LOAD ? "read" : "write"); |
442 |
|
443 |
if (reg != -1) { |
444 |
static const char * x86_reg_str_map[8] = { |
445 |
"eax", "ecx", "edx", "ebx", |
446 |
"esp", "ebp", "esi", "edi" |
447 |
}; |
448 |
printf(" %s register %%%s", transfer_type == TYPE_LOAD ? "to" : "from", x86_reg_str_map[reg]); |
449 |
} |
450 |
printf(", %d bytes instruction\n", len); |
451 |
#endif |
452 |
|
453 |
regs[X86_REG_EIP] += len; |
454 |
return true; |
455 |
} |
456 |
#endif |
457 |
#endif |
458 |
|
459 |
// Fallbacks |
460 |
#ifndef SIGSEGV_FAULT_INSTRUCTION |
461 |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC |
462 |
#endif |
463 |
|
464 |
// SIGSEGV recovery supported ? |
465 |
#if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS) |
466 |
#define HAVE_SIGSEGV_RECOVERY |
467 |
#endif |
468 |
|
469 |
|
470 |
/* |
471 |
* SIGSEGV global handler |
472 |
*/ |
473 |
|
474 |
#ifdef HAVE_SIGSEGV_RECOVERY |
475 |
static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST) |
476 |
{ |
477 |
sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS; |
478 |
sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION; |
479 |
bool fault_recovered = false; |
480 |
|
481 |
// Call user's handler and reinstall the global handler, if required |
482 |
if (sigsegv_user_handler(fault_address, fault_instruction)) { |
483 |
#if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL)) |
484 |
sigsegv_do_install_handler(sig); |
485 |
#endif |
486 |
fault_recovered = true; |
487 |
} |
488 |
#if HAVE_SIGSEGV_SKIP_INSTRUCTION |
489 |
else if (sigsegv_ignore_fault) { |
490 |
// Call the instruction skipper with the register file available |
491 |
if (SIGSEGV_SKIP_INSTRUCTION(fault_instruction, SIGSEGV_REGISTER_FILE)) |
492 |
fault_recovered = true; |
493 |
} |
494 |
#endif |
495 |
|
496 |
if (!fault_recovered) { |
497 |
// FAIL: reinstall default handler for "safe" crash |
498 |
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
499 |
SIGSEGV_ALL_SIGNALS |
500 |
#undef FAULT_HANDLER |
501 |
|
502 |
// We can't do anything with the fault_address, dump state? |
503 |
if (sigsegv_dump_state != 0) |
504 |
sigsegv_dump_state(fault_address, fault_instruction); |
505 |
} |
506 |
} |
507 |
#endif |
508 |
|
509 |
|
510 |
/* |
511 |
* SIGSEGV handler initialization |
512 |
*/ |
513 |
|
514 |
#if defined(HAVE_SIGINFO_T) |
515 |
static bool sigsegv_do_install_handler(int sig) |
516 |
{ |
517 |
// Setup SIGSEGV handler to process writes to frame buffer |
518 |
#ifdef HAVE_SIGACTION |
519 |
struct sigaction vosf_sa; |
520 |
sigemptyset(&vosf_sa.sa_mask); |
521 |
vosf_sa.sa_sigaction = sigsegv_handler; |
522 |
vosf_sa.sa_flags = SA_SIGINFO; |
523 |
return (sigaction(sig, &vosf_sa, 0) == 0); |
524 |
#else |
525 |
return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR); |
526 |
#endif |
527 |
} |
528 |
#endif |
529 |
|
530 |
#if defined(HAVE_SIGCONTEXT_SUBTERFUGE) |
531 |
static bool sigsegv_do_install_handler(int sig) |
532 |
{ |
533 |
// Setup SIGSEGV handler to process writes to frame buffer |
534 |
#ifdef HAVE_SIGACTION |
535 |
struct sigaction vosf_sa; |
536 |
sigemptyset(&vosf_sa.sa_mask); |
537 |
vosf_sa.sa_handler = (signal_handler)sigsegv_handler; |
538 |
#if !EMULATED_68K && defined(__NetBSD__) |
539 |
sigaddset(&vosf_sa.sa_mask, SIGALRM); |
540 |
vosf_sa.sa_flags = SA_ONSTACK; |
541 |
#else |
542 |
vosf_sa.sa_flags = 0; |
543 |
#endif |
544 |
return (sigaction(sig, &vosf_sa, 0) == 0); |
545 |
#else |
546 |
return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR); |
547 |
#endif |
548 |
} |
549 |
#endif |
550 |
|
551 |
bool sigsegv_install_handler(sigsegv_handler_t handler) |
552 |
{ |
553 |
#ifdef HAVE_SIGSEGV_RECOVERY |
554 |
sigsegv_user_handler = handler; |
555 |
bool success = true; |
556 |
#define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig); |
557 |
SIGSEGV_ALL_SIGNALS |
558 |
#undef FAULT_HANDLER |
559 |
return success; |
560 |
#else |
561 |
// FAIL: no siginfo_t nor sigcontext subterfuge is available |
562 |
return false; |
563 |
#endif |
564 |
} |
565 |
|
566 |
|
567 |
/* |
568 |
* SIGSEGV handler deinitialization |
569 |
*/ |
570 |
|
571 |
void sigsegv_deinstall_handler(void) |
572 |
{ |
573 |
#ifdef HAVE_SIGSEGV_RECOVERY |
574 |
sigsegv_user_handler = 0; |
575 |
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
576 |
SIGSEGV_ALL_SIGNALS |
577 |
#undef FAULT_HANDLER |
578 |
#endif |
579 |
} |
580 |
|
581 |
|
582 |
/* |
583 |
* SIGSEGV ignore state modifier |
584 |
*/ |
585 |
|
586 |
void sigsegv_set_ignore_state(bool ignore_fault) |
587 |
{ |
588 |
sigsegv_ignore_fault = ignore_fault; |
589 |
} |
590 |
|
591 |
|
592 |
/* |
593 |
* Set callback function when we cannot handle the fault |
594 |
*/ |
595 |
|
596 |
void sigsegv_set_dump_state(sigsegv_handler_t handler) |
597 |
{ |
598 |
sigsegv_dump_state = handler; |
599 |
} |
600 |
|
601 |
|
602 |
/* |
603 |
* Test program used for configure/test |
604 |
*/ |
605 |
|
606 |
#ifdef CONFIGURE_TEST_SIGSEGV_RECOVERY |
607 |
#include <stdio.h> |
608 |
#include <stdlib.h> |
609 |
#include <fcntl.h> |
610 |
#include <sys/mman.h> |
611 |
#include "vm_alloc.h" |
612 |
|
613 |
static int page_size; |
614 |
static volatile char * page = 0; |
615 |
static volatile int handler_called = 0; |
616 |
|
617 |
static bool sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
618 |
{ |
619 |
handler_called++; |
620 |
if ((fault_address - 123) != page) |
621 |
exit(1); |
622 |
if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0) |
623 |
exit(1); |
624 |
return true; |
625 |
} |
626 |
|
627 |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
628 |
static bool sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
629 |
{ |
630 |
return false; |
631 |
} |
632 |
#endif |
633 |
|
634 |
int main(void) |
635 |
{ |
636 |
if (vm_init() < 0) |
637 |
return 1; |
638 |
|
639 |
page_size = getpagesize(); |
640 |
if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED) |
641 |
return 1; |
642 |
|
643 |
if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0) |
644 |
return 1; |
645 |
|
646 |
if (!sigsegv_install_handler(sigsegv_test_handler)) |
647 |
return 1; |
648 |
|
649 |
page[123] = 45; |
650 |
page[123] = 45; |
651 |
|
652 |
if (handler_called != 1) |
653 |
return 1; |
654 |
|
655 |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
656 |
if (!sigsegv_install_handler(sigsegv_insn_handler)) |
657 |
return 1; |
658 |
|
659 |
if (vm_protect((char *)page, page_size, VM_PAGE_WRITE) < 0) |
660 |
return 1; |
661 |
|
662 |
for (int i = 0; i < page_size; i++) |
663 |
page[i] = (i + 1) % page_size; |
664 |
|
665 |
if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0) |
666 |
return 1; |
667 |
|
668 |
sigsegv_set_ignore_state(true); |
669 |
|
670 |
#define TEST_SKIP_INSTRUCTION(TYPE) do { \ |
671 |
const unsigned int TAG = 0x12345678; \ |
672 |
TYPE data = *((TYPE *)(page + sizeof(TYPE))); \ |
673 |
volatile unsigned int effect = data + TAG; \ |
674 |
if (effect != TAG) \ |
675 |
return 1; \ |
676 |
} while (0) |
677 |
|
678 |
TEST_SKIP_INSTRUCTION(unsigned char); |
679 |
TEST_SKIP_INSTRUCTION(unsigned short); |
680 |
TEST_SKIP_INSTRUCTION(unsigned int); |
681 |
#endif |
682 |
|
683 |
vm_exit(); |
684 |
return 0; |
685 |
} |
686 |
#endif |