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 |
+ |
#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)) /* EDI is the first GPR (even below EIP) in sigcontext */ |
221 |
+ |
/* (gb) Disable because this would hang configure script for some reason |
222 |
+ |
* though standalone testing gets it right. Any idea why? |
223 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
224 |
+ |
*/ |
225 |
+ |
#endif |
226 |
|
#if defined(__linux__) |
227 |
|
#if (defined(i386) || defined(__i386__)) |
228 |
|
#include <sys/ucontext.h> |
229 |
< |
#define SIGSEGV_FAULT_INSTRUCTION (((ucontext_t *)scp)->uc_mcontext.gregs[14]) /* should use REG_EIP instead */ |
229 |
> |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
230 |
> |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[14] /* should use REG_EIP instead */ |
231 |
> |
#define SIGSEGV_REGISTER_FILE (unsigned int *)SIGSEGV_CONTEXT_REGS |
232 |
> |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
233 |
|
#endif |
234 |
|
#if (defined(ia64) || defined(__ia64__)) |
235 |
|
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */ |
236 |
|
#endif |
237 |
+ |
#if (defined(powerpc) || defined(__powerpc__)) |
238 |
+ |
#include <sys/ucontext.h> |
239 |
+ |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.regs) |
240 |
+ |
#define SIGSEGV_FAULT_INSTRUCTION (SIGSEGV_CONTEXT_REGS->nip) |
241 |
+ |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr) |
242 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
243 |
+ |
#endif |
244 |
|
#endif |
245 |
|
#endif |
246 |
|
|
253 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs |
254 |
|
#define SIGSEGV_FAULT_ADDRESS scs.cr2 |
255 |
|
#define SIGSEGV_FAULT_INSTRUCTION scs.eip |
256 |
+ |
#define SIGSEGV_REGISTER_FILE (unsigned int *)(&scs) |
257 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
258 |
|
#endif |
259 |
|
#if (defined(sparc) || defined(__sparc__)) |
260 |
|
#include <asm/sigcontext.h> |
266 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp |
267 |
|
#define SIGSEGV_FAULT_ADDRESS scp->regs->dar |
268 |
|
#define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip |
269 |
+ |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr) |
270 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
271 |
|
#endif |
272 |
|
#if (defined(alpha) || defined(__alpha__)) |
273 |
|
#include <asm/sigcontext.h> |
288 |
|
|
289 |
|
// Irix 5 or 6 on MIPS |
290 |
|
#if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4)) |
291 |
+ |
#include <ucontext.h> |
292 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
293 |
|
#define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr |
294 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
295 |
|
#endif |
296 |
|
|
297 |
+ |
// HP-UX |
298 |
+ |
#if (defined(hpux) || defined(__hpux__)) |
299 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
300 |
+ |
#define SIGSEGV_FAULT_ADDRESS scp->sc_sl.sl_ss.ss_narrow.ss_cr21 |
301 |
+ |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS) |
302 |
+ |
#endif |
303 |
+ |
|
304 |
|
// OSF/1 on Alpha |
305 |
|
#if defined(__osf__) |
306 |
+ |
#include <ucontext.h> |
307 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
308 |
|
#define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0 |
309 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
321 |
|
#if (defined(m68k) || defined(__m68k__)) |
322 |
|
#include <m68k/frame.h> |
323 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
324 |
< |
#define SIGSEGV_FAULT_ADDRESS ({ \ |
135 |
< |
struct sigstate { \ |
136 |
< |
int ss_flags; \ |
137 |
< |
struct frame ss_frame; \ |
138 |
< |
}; \ |
139 |
< |
struct sigstate *state = (struct sigstate *)scp->sc_ap; \ |
140 |
< |
char *fault_addr; \ |
141 |
< |
switch (state->ss_frame.f_format) { \ |
142 |
< |
case 7: /* 68040 access error */ \ |
143 |
< |
/* "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown */ \ |
144 |
< |
fault_addr = state->ss_frame.f_fmt7.f_fa; \ |
145 |
< |
break; \ |
146 |
< |
default: \ |
147 |
< |
fault_addr = (char *)code; \ |
148 |
< |
break; \ |
149 |
< |
} \ |
150 |
< |
fault_addr; \ |
151 |
< |
}) |
324 |
> |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
325 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
326 |
+ |
|
327 |
+ |
// Use decoding scheme from BasiliskII/m68k native |
328 |
+ |
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
329 |
+ |
{ |
330 |
+ |
struct sigstate { |
331 |
+ |
int ss_flags; |
332 |
+ |
struct frame ss_frame; |
333 |
+ |
}; |
334 |
+ |
struct sigstate *state = (struct sigstate *)scp->sc_ap; |
335 |
+ |
char *fault_addr; |
336 |
+ |
switch (state->ss_frame.f_format) { |
337 |
+ |
case 7: /* 68040 access error */ |
338 |
+ |
/* "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown */ |
339 |
+ |
fault_addr = state->ss_frame.f_fmt7.f_fa; |
340 |
+ |
break; |
341 |
+ |
default: |
342 |
+ |
fault_addr = (char *)code; |
343 |
+ |
break; |
344 |
+ |
} |
345 |
+ |
return (sigsegv_address_t)fault_addr; |
346 |
+ |
} |
347 |
|
#else |
348 |
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, void *scp, char *addr |
349 |
|
#define SIGSEGV_FAULT_ADDRESS addr |
358 |
|
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
359 |
|
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_ir |
360 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
361 |
+ |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->sc_ir, &((unsigned int *) scp->sc_regs)[2] |
362 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
363 |
|
|
364 |
< |
// From Boehm's GC 6.0alpha8 |
169 |
< |
#define EXTRACT_OP1(iw) (((iw) & 0xFC000000) >> 26) |
170 |
< |
#define EXTRACT_OP2(iw) (((iw) & 0x000007FE) >> 1) |
171 |
< |
#define EXTRACT_REGA(iw) (((iw) & 0x001F0000) >> 16) |
172 |
< |
#define EXTRACT_REGB(iw) (((iw) & 0x03E00000) >> 21) |
173 |
< |
#define EXTRACT_REGC(iw) (((iw) & 0x0000F800) >> 11) |
174 |
< |
#define EXTRACT_DISP(iw) ((short *) &(iw))[1] |
175 |
< |
|
364 |
> |
// Use decoding scheme from SheepShaver |
365 |
|
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
366 |
|
{ |
367 |
< |
unsigned int instr = *((unsigned int *) scp->sc_ir); |
368 |
< |
unsigned int * regs = &((unsigned int *) scp->sc_regs)[2]; |
369 |
< |
int disp = 0, tmp; |
370 |
< |
unsigned int baseA = 0, baseB = 0; |
371 |
< |
unsigned int addr, alignmask = 0xFFFFFFFF; |
372 |
< |
|
373 |
< |
switch(EXTRACT_OP1(instr)) { |
374 |
< |
case 38: /* stb */ |
375 |
< |
case 39: /* stbu */ |
376 |
< |
case 54: /* stfd */ |
377 |
< |
case 55: /* stfdu */ |
378 |
< |
case 52: /* stfs */ |
379 |
< |
case 53: /* stfsu */ |
380 |
< |
case 44: /* sth */ |
381 |
< |
case 45: /* sthu */ |
382 |
< |
case 47: /* stmw */ |
383 |
< |
case 36: /* stw */ |
384 |
< |
case 37: /* stwu */ |
385 |
< |
tmp = EXTRACT_REGA(instr); |
386 |
< |
if(tmp > 0) |
387 |
< |
baseA = regs[tmp]; |
388 |
< |
disp = EXTRACT_DISP(instr); |
367 |
> |
unsigned int nip = (unsigned int) scp->sc_ir; |
368 |
> |
unsigned int * gpr = &((unsigned int *) scp->sc_regs)[2]; |
369 |
> |
instruction_t instr; |
370 |
> |
|
371 |
> |
powerpc_decode_instruction(&instr, nip, gpr); |
372 |
> |
return (sigsegv_address_t)instr.addr; |
373 |
> |
} |
374 |
> |
#endif |
375 |
> |
#endif |
376 |
> |
#endif |
377 |
> |
|
378 |
> |
|
379 |
> |
/* |
380 |
> |
* Instruction skipping |
381 |
> |
*/ |
382 |
> |
|
383 |
> |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
384 |
> |
// Decode and skip X86 instruction |
385 |
> |
#if (defined(i386) || defined(__i386__)) |
386 |
> |
#if defined(__linux__) |
387 |
> |
enum { |
388 |
> |
X86_REG_EIP = 14, |
389 |
> |
X86_REG_EAX = 11, |
390 |
> |
X86_REG_ECX = 10, |
391 |
> |
X86_REG_EDX = 9, |
392 |
> |
X86_REG_EBX = 8, |
393 |
> |
X86_REG_ESP = 7, |
394 |
> |
X86_REG_EBP = 6, |
395 |
> |
X86_REG_ESI = 5, |
396 |
> |
X86_REG_EDI = 4 |
397 |
> |
}; |
398 |
> |
#endif |
399 |
> |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
400 |
> |
enum { |
401 |
> |
X86_REG_EIP = 10, |
402 |
> |
X86_REG_EAX = 7, |
403 |
> |
X86_REG_ECX = 6, |
404 |
> |
X86_REG_EDX = 5, |
405 |
> |
X86_REG_EBX = 4, |
406 |
> |
X86_REG_ESP = 13, |
407 |
> |
X86_REG_EBP = 2, |
408 |
> |
X86_REG_ESI = 1, |
409 |
> |
X86_REG_EDI = 0 |
410 |
> |
}; |
411 |
> |
#endif |
412 |
> |
// FIXME: this is partly redundant with the instruction decoding phase |
413 |
> |
// to discover transfer type and register number |
414 |
> |
static inline int ix86_step_over_modrm(unsigned char * p) |
415 |
> |
{ |
416 |
> |
int mod = (p[0] >> 6) & 3; |
417 |
> |
int rm = p[0] & 7; |
418 |
> |
int offset = 0; |
419 |
> |
|
420 |
> |
// ModR/M Byte |
421 |
> |
switch (mod) { |
422 |
> |
case 0: // [reg] |
423 |
> |
if (rm == 5) return 4; // disp32 |
424 |
|
break; |
425 |
< |
case 31: |
426 |
< |
switch(EXTRACT_OP2(instr)) { |
427 |
< |
case 86: /* dcbf */ |
428 |
< |
case 54: /* dcbst */ |
429 |
< |
case 1014: /* dcbz */ |
430 |
< |
case 247: /* stbux */ |
431 |
< |
case 215: /* stbx */ |
432 |
< |
case 759: /* stfdux */ |
433 |
< |
case 727: /* stfdx */ |
434 |
< |
case 983: /* stfiwx */ |
435 |
< |
case 695: /* stfsux */ |
436 |
< |
case 663: /* stfsx */ |
437 |
< |
case 918: /* sthbrx */ |
438 |
< |
case 439: /* sthux */ |
439 |
< |
case 407: /* sthx */ |
440 |
< |
case 661: /* stswx */ |
441 |
< |
case 662: /* stwbrx */ |
442 |
< |
case 150: /* stwcx. */ |
443 |
< |
case 183: /* stwux */ |
444 |
< |
case 151: /* stwx */ |
445 |
< |
case 135: /* stvebx */ |
446 |
< |
case 167: /* stvehx */ |
447 |
< |
case 199: /* stvewx */ |
448 |
< |
case 231: /* stvx */ |
449 |
< |
case 487: /* stvxl */ |
450 |
< |
tmp = EXTRACT_REGA(instr); |
451 |
< |
if(tmp > 0) |
452 |
< |
baseA = regs[tmp]; |
453 |
< |
baseB = regs[EXTRACT_REGC(instr)]; |
454 |
< |
/* determine Altivec alignment mask */ |
455 |
< |
switch(EXTRACT_OP2(instr)) { |
456 |
< |
case 167: /* stvehx */ |
457 |
< |
alignmask = 0xFFFFFFFE; |
458 |
< |
break; |
459 |
< |
case 199: /* stvewx */ |
460 |
< |
alignmask = 0xFFFFFFFC; |
461 |
< |
break; |
462 |
< |
case 231: /* stvx */ |
463 |
< |
alignmask = 0xFFFFFFF0; |
464 |
< |
break; |
465 |
< |
case 487: /* stvxl */ |
466 |
< |
alignmask = 0xFFFFFFF0; |
467 |
< |
break; |
468 |
< |
} |
425 |
> |
case 1: // disp8[reg] |
426 |
> |
offset = 1; |
427 |
> |
break; |
428 |
> |
case 2: // disp32[reg] |
429 |
> |
offset = 4; |
430 |
> |
break; |
431 |
> |
case 3: // register |
432 |
> |
return 0; |
433 |
> |
} |
434 |
> |
|
435 |
> |
// SIB Byte |
436 |
> |
if (rm == 4) { |
437 |
> |
if (mod == 0 && (p[1] & 7) == 5) |
438 |
> |
offset = 5; // disp32[index] |
439 |
> |
else |
440 |
> |
offset++; |
441 |
> |
} |
442 |
> |
|
443 |
> |
return offset; |
444 |
> |
} |
445 |
> |
|
446 |
> |
static bool ix86_skip_instruction(unsigned int * regs) |
447 |
> |
{ |
448 |
> |
unsigned char * eip = (unsigned char *)regs[X86_REG_EIP]; |
449 |
> |
|
450 |
> |
if (eip == 0) |
451 |
> |
return false; |
452 |
> |
|
453 |
> |
transfer_type_t transfer_type = TYPE_UNKNOWN; |
454 |
> |
transfer_size_t transfer_size = SIZE_LONG; |
455 |
> |
|
456 |
> |
int reg = -1; |
457 |
> |
int len = 0; |
458 |
> |
|
459 |
> |
// Operand size prefix |
460 |
> |
if (*eip == 0x66) { |
461 |
> |
eip++; |
462 |
> |
len++; |
463 |
> |
transfer_size = SIZE_WORD; |
464 |
> |
} |
465 |
> |
|
466 |
> |
// Decode instruction |
467 |
> |
switch (eip[0]) { |
468 |
> |
case 0x0f: |
469 |
> |
switch (eip[1]) { |
470 |
> |
case 0xb6: // MOVZX r32, r/m8 |
471 |
> |
case 0xb7: // MOVZX r32, r/m16 |
472 |
> |
switch (eip[2] & 0xc0) { |
473 |
> |
case 0x80: |
474 |
> |
reg = (eip[2] >> 3) & 7; |
475 |
> |
transfer_type = TYPE_LOAD; |
476 |
> |
break; |
477 |
> |
case 0x40: |
478 |
> |
reg = (eip[2] >> 3) & 7; |
479 |
> |
transfer_type = TYPE_LOAD; |
480 |
> |
break; |
481 |
> |
case 0x00: |
482 |
> |
reg = (eip[2] >> 3) & 7; |
483 |
> |
transfer_type = TYPE_LOAD; |
484 |
> |
break; |
485 |
> |
} |
486 |
> |
len += 3 + ix86_step_over_modrm(eip + 2); |
487 |
> |
break; |
488 |
> |
} |
489 |
> |
break; |
490 |
> |
case 0x8a: // MOV r8, r/m8 |
491 |
> |
transfer_size = SIZE_BYTE; |
492 |
> |
case 0x8b: // MOV r32, r/m32 (or 16-bit operation) |
493 |
> |
switch (eip[1] & 0xc0) { |
494 |
> |
case 0x80: |
495 |
> |
reg = (eip[1] >> 3) & 7; |
496 |
> |
transfer_type = TYPE_LOAD; |
497 |
|
break; |
498 |
< |
case 725: /* stswi */ |
499 |
< |
tmp = EXTRACT_REGA(instr); |
500 |
< |
if(tmp > 0) |
249 |
< |
baseA = regs[tmp]; |
498 |
> |
case 0x40: |
499 |
> |
reg = (eip[1] >> 3) & 7; |
500 |
> |
transfer_type = TYPE_LOAD; |
501 |
|
break; |
502 |
< |
default: /* ignore instruction */ |
503 |
< |
return 0; |
502 |
> |
case 0x00: |
503 |
> |
reg = (eip[1] >> 3) & 7; |
504 |
> |
transfer_type = TYPE_LOAD; |
505 |
|
break; |
506 |
|
} |
507 |
+ |
len += 2 + ix86_step_over_modrm(eip + 1); |
508 |
|
break; |
509 |
< |
default: /* ignore instruction */ |
510 |
< |
return 0; |
509 |
> |
case 0x88: // MOV r/m8, r8 |
510 |
> |
transfer_size = SIZE_BYTE; |
511 |
> |
case 0x89: // MOV r/m32, r32 (or 16-bit operation) |
512 |
> |
switch (eip[1] & 0xc0) { |
513 |
> |
case 0x80: |
514 |
> |
reg = (eip[1] >> 3) & 7; |
515 |
> |
transfer_type = TYPE_STORE; |
516 |
> |
break; |
517 |
> |
case 0x40: |
518 |
> |
reg = (eip[1] >> 3) & 7; |
519 |
> |
transfer_type = TYPE_STORE; |
520 |
> |
break; |
521 |
> |
case 0x00: |
522 |
> |
reg = (eip[1] >> 3) & 7; |
523 |
> |
transfer_type = TYPE_STORE; |
524 |
> |
break; |
525 |
> |
} |
526 |
> |
len += 2 + ix86_step_over_modrm(eip + 1); |
527 |
|
break; |
528 |
|
} |
529 |
+ |
|
530 |
+ |
if (transfer_type == TYPE_UNKNOWN) { |
531 |
+ |
// Unknown machine code, let it crash. Then patch the decoder |
532 |
+ |
return false; |
533 |
+ |
} |
534 |
+ |
|
535 |
+ |
if (transfer_type == TYPE_LOAD && reg != -1) { |
536 |
+ |
static const int x86_reg_map[8] = { |
537 |
+ |
X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX, |
538 |
+ |
X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI |
539 |
+ |
}; |
540 |
+ |
|
541 |
+ |
if (reg < 0 || reg >= 8) |
542 |
+ |
return false; |
543 |
+ |
|
544 |
+ |
int rloc = x86_reg_map[reg]; |
545 |
+ |
switch (transfer_size) { |
546 |
+ |
case SIZE_BYTE: |
547 |
+ |
regs[rloc] = (regs[rloc] & ~0xff); |
548 |
+ |
break; |
549 |
+ |
case SIZE_WORD: |
550 |
+ |
regs[rloc] = (regs[rloc] & ~0xffff); |
551 |
+ |
break; |
552 |
+ |
case SIZE_LONG: |
553 |
+ |
regs[rloc] = 0; |
554 |
+ |
break; |
555 |
+ |
} |
556 |
+ |
} |
557 |
+ |
|
558 |
+ |
#if DEBUG |
559 |
+ |
printf("%08x: %s %s access", regs[X86_REG_EIP], |
560 |
+ |
transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long", |
561 |
+ |
transfer_type == TYPE_LOAD ? "read" : "write"); |
562 |
|
|
563 |
< |
addr = (baseA + baseB) + disp; |
564 |
< |
addr &= alignmask; |
565 |
< |
return (sigsegv_address_t)addr; |
563 |
> |
if (reg != -1) { |
564 |
> |
static const char * x86_reg_str_map[8] = { |
565 |
> |
"eax", "ecx", "edx", "ebx", |
566 |
> |
"esp", "ebp", "esi", "edi" |
567 |
> |
}; |
568 |
> |
printf(" %s register %%%s", transfer_type == TYPE_LOAD ? "to" : "from", x86_reg_str_map[reg]); |
569 |
> |
} |
570 |
> |
printf(", %d bytes instruction\n", len); |
571 |
> |
#endif |
572 |
> |
|
573 |
> |
regs[X86_REG_EIP] += len; |
574 |
> |
return true; |
575 |
|
} |
576 |
|
#endif |
577 |
+ |
|
578 |
+ |
// Decode and skip PPC instruction |
579 |
+ |
#if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__)) |
580 |
+ |
static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs) |
581 |
+ |
{ |
582 |
+ |
instruction_t instr; |
583 |
+ |
powerpc_decode_instruction(&instr, *nip_p, regs); |
584 |
+ |
|
585 |
+ |
if (instr.transfer_type == TYPE_UNKNOWN) { |
586 |
+ |
// Unknown machine code, let it crash. Then patch the decoder |
587 |
+ |
return false; |
588 |
+ |
} |
589 |
+ |
|
590 |
+ |
#if DEBUG |
591 |
+ |
printf("%08x: %s %s access", *nip_p, |
592 |
+ |
instr.transfer_size == SIZE_BYTE ? "byte" : instr.transfer_size == SIZE_WORD ? "word" : "long", |
593 |
+ |
instr.transfer_type == TYPE_LOAD ? "read" : "write"); |
594 |
+ |
|
595 |
+ |
if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX) |
596 |
+ |
printf(" r%d (ra = %08x)\n", instr.ra, instr.addr); |
597 |
+ |
if (instr.transfer_type == TYPE_LOAD) |
598 |
+ |
printf(" r%d (rd = 0)\n", instr.rd); |
599 |
+ |
#endif |
600 |
+ |
|
601 |
+ |
if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX) |
602 |
+ |
regs[instr.ra] = instr.addr; |
603 |
+ |
if (instr.transfer_type == TYPE_LOAD) |
604 |
+ |
regs[instr.rd] = 0; |
605 |
+ |
|
606 |
+ |
*nip_p += 4; |
607 |
+ |
return true; |
608 |
+ |
} |
609 |
|
#endif |
610 |
|
#endif |
611 |
|
|
627 |
|
#ifdef HAVE_SIGSEGV_RECOVERY |
628 |
|
static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST) |
629 |
|
{ |
630 |
+ |
sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS; |
631 |
+ |
sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION; |
632 |
+ |
bool fault_recovered = false; |
633 |
+ |
|
634 |
|
// Call user's handler and reinstall the global handler, if required |
635 |
< |
if (sigsegv_user_handler((sigsegv_address_t)SIGSEGV_FAULT_ADDRESS, (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION)) { |
635 |
> |
if (sigsegv_fault_handler(fault_address, fault_instruction)) { |
636 |
|
#if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL)) |
637 |
|
sigsegv_do_install_handler(sig); |
638 |
|
#endif |
639 |
+ |
fault_recovered = true; |
640 |
+ |
} |
641 |
+ |
#if HAVE_SIGSEGV_SKIP_INSTRUCTION |
642 |
+ |
else if (sigsegv_ignore_fault) { |
643 |
+ |
// Call the instruction skipper with the register file available |
644 |
+ |
if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE)) |
645 |
+ |
fault_recovered = true; |
646 |
|
} |
647 |
< |
else { |
647 |
> |
#endif |
648 |
> |
|
649 |
> |
if (!fault_recovered) { |
650 |
|
// FAIL: reinstall default handler for "safe" crash |
651 |
|
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
652 |
|
SIGSEGV_ALL_SIGNALS |
653 |
|
#undef FAULT_HANDLER |
654 |
+ |
|
655 |
+ |
// We can't do anything with the fault_address, dump state? |
656 |
+ |
if (sigsegv_state_dumper != 0) |
657 |
+ |
sigsegv_state_dumper(fault_address, fault_instruction); |
658 |
|
} |
659 |
|
} |
660 |
|
#endif |
701 |
|
} |
702 |
|
#endif |
703 |
|
|
704 |
< |
bool sigsegv_install_handler(sigsegv_handler_t handler) |
704 |
> |
bool sigsegv_install_handler(sigsegv_fault_handler_t handler) |
705 |
|
{ |
706 |
|
#ifdef HAVE_SIGSEGV_RECOVERY |
707 |
< |
sigsegv_user_handler = handler; |
707 |
> |
sigsegv_fault_handler = handler; |
708 |
|
bool success = true; |
709 |
|
#define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig); |
710 |
|
SIGSEGV_ALL_SIGNALS |
724 |
|
void sigsegv_deinstall_handler(void) |
725 |
|
{ |
726 |
|
#ifdef HAVE_SIGSEGV_RECOVERY |
727 |
< |
sigsegv_user_handler = 0; |
727 |
> |
sigsegv_fault_handler = 0; |
728 |
|
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
729 |
|
SIGSEGV_ALL_SIGNALS |
730 |
|
#undef FAULT_HANDLER |
731 |
|
#endif |
732 |
|
} |
733 |
|
|
734 |
+ |
|
735 |
+ |
/* |
736 |
+ |
* SIGSEGV ignore state modifier |
737 |
+ |
*/ |
738 |
+ |
|
739 |
+ |
void sigsegv_set_ignore_state(bool ignore_fault) |
740 |
+ |
{ |
741 |
+ |
sigsegv_ignore_fault = ignore_fault; |
742 |
+ |
} |
743 |
+ |
|
744 |
+ |
|
745 |
+ |
/* |
746 |
+ |
* Set callback function when we cannot handle the fault |
747 |
+ |
*/ |
748 |
+ |
|
749 |
+ |
void sigsegv_set_dump_state(sigsegv_state_dumper_t handler) |
750 |
+ |
{ |
751 |
+ |
sigsegv_state_dumper = handler; |
752 |
+ |
} |
753 |
+ |
|
754 |
+ |
|
755 |
|
/* |
756 |
|
* Test program used for configure/test |
757 |
|
*/ |
777 |
|
return true; |
778 |
|
} |
779 |
|
|
780 |
+ |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
781 |
+ |
static bool sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
782 |
+ |
{ |
783 |
+ |
return false; |
784 |
+ |
} |
785 |
+ |
#endif |
786 |
+ |
|
787 |
|
int main(void) |
788 |
|
{ |
789 |
|
if (vm_init() < 0) |
805 |
|
if (handler_called != 1) |
806 |
|
return 1; |
807 |
|
|
808 |
+ |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
809 |
+ |
if (!sigsegv_install_handler(sigsegv_insn_handler)) |
810 |
+ |
return 1; |
811 |
+ |
|
812 |
+ |
if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0) |
813 |
+ |
return 1; |
814 |
+ |
|
815 |
+ |
for (int i = 0; i < page_size; i++) |
816 |
+ |
page[i] = (i + 1) % page_size; |
817 |
+ |
|
818 |
+ |
if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0) |
819 |
+ |
return 1; |
820 |
+ |
|
821 |
+ |
sigsegv_set_ignore_state(true); |
822 |
+ |
|
823 |
+ |
#define TEST_SKIP_INSTRUCTION(TYPE) do { \ |
824 |
+ |
const unsigned int TAG = 0x12345678; \ |
825 |
+ |
TYPE data = *((TYPE *)(page + sizeof(TYPE))); \ |
826 |
+ |
volatile unsigned int effect = data + TAG; \ |
827 |
+ |
if (effect != TAG) \ |
828 |
+ |
return 1; \ |
829 |
+ |
} while (0) |
830 |
+ |
|
831 |
+ |
TEST_SKIP_INSTRUCTION(unsigned char); |
832 |
+ |
TEST_SKIP_INSTRUCTION(unsigned short); |
833 |
+ |
TEST_SKIP_INSTRUCTION(unsigned int); |
834 |
+ |
#endif |
835 |
+ |
|
836 |
|
vm_exit(); |
837 |
|
return 0; |
838 |
|
} |