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