ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
Revision: 1.18
Committed: 2002-05-20T18:12:01Z (22 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.17: +8 -2 lines
Log Message:
Backout "ignoresegv" support on FreeBSD/x86 for now. Unfortunately, the
configure script would hang whereas standalone testing will pass all tests.
Any idea why??
- Unix/sigsegv.cpp (ix86_skip_instruction): Add decoder for mozbl instruction.
  (SIGSEGV_REGISTER_FILE [FreeBSD/x86]): Note why we start at EDI offset
  rather than plain sigcontext pointer. i.e. I don't know sigset_t size
  beforehand and don't intend to.

File Contents

# User Rev Content
1 gbeauche 1.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 cebix 1.7 * Basilisk II (C) 1997-2002 Christian Bauer
8 gbeauche 1.1 *
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 gbeauche 1.10 // Is the fault to be ignored?
44     static bool sigsegv_ignore_fault = false;
45    
46 gbeauche 1.1 // User's SIGSEGV handler
47 gbeauche 1.12 static sigsegv_fault_handler_t sigsegv_fault_handler = 0;
48 gbeauche 1.1
49 gbeauche 1.10 // Function called to dump state if we can't handle the fault
50 gbeauche 1.12 static sigsegv_state_dumper_t sigsegv_state_dumper = 0;
51 gbeauche 1.10
52 gbeauche 1.1 // Actual SIGSEGV handler installer
53     static bool sigsegv_do_install_handler(int sig);
54    
55    
56     /*
57 gbeauche 1.14 * 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 gbeauche 1.1 * OS-dependant SIGSEGV signals support section
207     */
208    
209     #if HAVE_SIGINFO_T
210     // Generic extended signal handler
211 cebix 1.8 #if defined(__NetBSD__) || defined(__FreeBSD__)
212     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
213     #else
214 gbeauche 1.1 #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
215 cebix 1.8 #endif
216 gbeauche 1.5 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp
217 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS sip->si_addr
218 gbeauche 1.17 #if (defined(i386) || defined(__i386__))
219     #define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_eip)
220 gbeauche 1.18 #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 gbeauche 1.17 #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
224 gbeauche 1.18 */
225 gbeauche 1.17 #endif
226 gbeauche 1.5 #if defined(__linux__)
227 gbeauche 1.6 #if (defined(i386) || defined(__i386__))
228     #include <sys/ucontext.h>
229 gbeauche 1.14 #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 gbeauche 1.10 #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
233 gbeauche 1.6 #endif
234 gbeauche 1.5 #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 gbeauche 1.9 #if (defined(powerpc) || defined(__powerpc__))
238     #include <sys/ucontext.h>
239 gbeauche 1.14 #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 gbeauche 1.13 #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
243 gbeauche 1.9 #endif
244 gbeauche 1.5 #endif
245 gbeauche 1.1 #endif
246    
247     #if HAVE_SIGCONTEXT_SUBTERFUGE
248     // Linux kernels prior to 2.4 ?
249     #if defined(__linux__)
250     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
251     #if (defined(i386) || defined(__i386__))
252     #include <asm/sigcontext.h>
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 gbeauche 1.16 #define SIGSEGV_REGISTER_FILE (unsigned int *)(&scs)
257 gbeauche 1.10 #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
258 gbeauche 1.1 #endif
259     #if (defined(sparc) || defined(__sparc__))
260     #include <asm/sigcontext.h>
261 gbeauche 1.5 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr
262 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS addr
263     #endif
264     #if (defined(powerpc) || defined(__powerpc__))
265     #include <asm/sigcontext.h>
266 gbeauche 1.4 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp
267 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS scp->regs->dar
268     #define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip
269 gbeauche 1.14 #define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr)
270 gbeauche 1.13 #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
271 gbeauche 1.1 #endif
272 gbeauche 1.4 #if (defined(alpha) || defined(__alpha__))
273     #include <asm/sigcontext.h>
274     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
275     #define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
276     #define SIGSEGV_FAULT_INSTRUCTION scp->sc_pc
277    
278     // From Boehm's GC 6.0alpha8
279     static sigsegv_address_t get_fault_address(struct sigcontext *scp)
280     {
281     unsigned int instruction = *((unsigned int *)(scp->sc_pc));
282     unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f];
283     fault_address += (signed long)(signed short)(instruction & 0xffff);
284     return (sigsegv_address_t)fault_address;
285     }
286     #endif
287 gbeauche 1.1 #endif
288    
289     // Irix 5 or 6 on MIPS
290     #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4))
291 gbeauche 1.11 #include <ucontext.h>
292 gbeauche 1.1 #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 gbeauche 1.11 // 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 gbeauche 1.1 // OSF/1 on Alpha
305     #if defined(__osf__)
306 gbeauche 1.11 #include <ucontext.h>
307 gbeauche 1.1 #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)
310     #endif
311    
312     // AIX
313     #if defined(_AIX)
314     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
315     #define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr
316     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
317     #endif
318    
319     // NetBSD or FreeBSD
320     #if defined(__NetBSD__) || defined(__FreeBSD__)
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 gbeauche 1.14 #define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
325 gbeauche 1.1 #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
326 gbeauche 1.14
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 gbeauche 1.1 #else
348     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, void *scp, char *addr
349     #define SIGSEGV_FAULT_ADDRESS addr
350     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
351     #endif
352     #endif
353 gbeauche 1.4
354     // MacOS X
355     #if defined(__APPLE__) && defined(__MACH__)
356     #if (defined(ppc) || defined(__ppc__))
357     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
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 gbeauche 1.14 #define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->sc_ir, &((unsigned int *) scp->sc_regs)[2]
362     #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
363 gbeauche 1.4
364 gbeauche 1.14 // Use decoding scheme from SheepShaver
365 gbeauche 1.4 static sigsegv_address_t get_fault_address(struct sigcontext *scp)
366     {
367 gbeauche 1.14 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 gbeauche 1.4 }
374     #endif
375     #endif
376 gbeauche 1.1 #endif
377    
378 gbeauche 1.14
379     /*
380     * Instruction skipping
381     */
382    
383 gbeauche 1.10 #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 gbeauche 1.17 #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 gbeauche 1.10 // 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 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 gbeauche 1.14 static bool ix86_skip_instruction(unsigned int * regs)
447 gbeauche 1.10 {
448 gbeauche 1.14 unsigned char * eip = (unsigned char *)regs[X86_REG_EIP];
449 gbeauche 1.10
450     if (eip == 0)
451     return false;
452    
453 gbeauche 1.14 transfer_type_t transfer_type = TYPE_UNKNOWN;
454     transfer_size_t transfer_size = SIZE_LONG;
455 gbeauche 1.10
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 gbeauche 1.17 case 0x0f:
469 gbeauche 1.18 switch (eip[1]) {
470     case 0xb6: // MOVZX r32, r/m8
471     case 0xb7: // MOVZX r32, r/m16
472 gbeauche 1.17 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 gbeauche 1.18 break;
488 gbeauche 1.17 }
489     break;
490 gbeauche 1.10 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 0x40:
499     reg = (eip[1] >> 3) & 7;
500     transfer_type = TYPE_LOAD;
501     break;
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     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 gbeauche 1.15 printf("%08x: %s %s access", regs[X86_REG_EIP],
560 gbeauche 1.10 transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long",
561     transfer_type == TYPE_LOAD ? "read" : "write");
562    
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 gbeauche 1.13 return true;
575     }
576     #endif
577 gbeauche 1.14
578 gbeauche 1.13 // Decode and skip PPC instruction
579 gbeauche 1.14 #if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__))
580     static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs)
581 gbeauche 1.13 {
582 gbeauche 1.14 instruction_t instr;
583     powerpc_decode_instruction(&instr, *nip_p, regs);
584 gbeauche 1.13
585 gbeauche 1.14 if (instr.transfer_type == TYPE_UNKNOWN) {
586 gbeauche 1.13 // Unknown machine code, let it crash. Then patch the decoder
587     return false;
588     }
589    
590     #if DEBUG
591 gbeauche 1.14 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 gbeauche 1.13
606 gbeauche 1.14 *nip_p += 4;
607 gbeauche 1.10 return true;
608     }
609     #endif
610     #endif
611    
612 gbeauche 1.1 // Fallbacks
613     #ifndef SIGSEGV_FAULT_INSTRUCTION
614     #define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC
615     #endif
616    
617 gbeauche 1.2 // SIGSEGV recovery supported ?
618     #if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS)
619     #define HAVE_SIGSEGV_RECOVERY
620     #endif
621    
622 gbeauche 1.1
623     /*
624     * SIGSEGV global handler
625     */
626    
627 gbeauche 1.2 #ifdef HAVE_SIGSEGV_RECOVERY
628 gbeauche 1.1 static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
629     {
630 gbeauche 1.10 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 gbeauche 1.1 // Call user's handler and reinstall the global handler, if required
635 gbeauche 1.12 if (sigsegv_fault_handler(fault_address, fault_instruction)) {
636 gbeauche 1.1 #if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
637     sigsegv_do_install_handler(sig);
638     #endif
639 gbeauche 1.10 fault_recovered = true;
640 gbeauche 1.1 }
641 gbeauche 1.10 #if HAVE_SIGSEGV_SKIP_INSTRUCTION
642     else if (sigsegv_ignore_fault) {
643     // Call the instruction skipper with the register file available
644 gbeauche 1.14 if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE))
645 gbeauche 1.10 fault_recovered = true;
646     }
647     #endif
648    
649     if (!fault_recovered) {
650 gbeauche 1.1 // 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 gbeauche 1.10
655     // We can't do anything with the fault_address, dump state?
656 gbeauche 1.12 if (sigsegv_state_dumper != 0)
657     sigsegv_state_dumper(fault_address, fault_instruction);
658 gbeauche 1.1 }
659     }
660 gbeauche 1.2 #endif
661 gbeauche 1.1
662    
663     /*
664     * SIGSEGV handler initialization
665     */
666    
667     #if defined(HAVE_SIGINFO_T)
668     static bool sigsegv_do_install_handler(int sig)
669     {
670     // Setup SIGSEGV handler to process writes to frame buffer
671     #ifdef HAVE_SIGACTION
672     struct sigaction vosf_sa;
673     sigemptyset(&vosf_sa.sa_mask);
674     vosf_sa.sa_sigaction = sigsegv_handler;
675     vosf_sa.sa_flags = SA_SIGINFO;
676     return (sigaction(sig, &vosf_sa, 0) == 0);
677     #else
678     return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
679     #endif
680     }
681 gbeauche 1.2 #endif
682    
683     #if defined(HAVE_SIGCONTEXT_SUBTERFUGE)
684 gbeauche 1.1 static bool sigsegv_do_install_handler(int sig)
685     {
686     // Setup SIGSEGV handler to process writes to frame buffer
687     #ifdef HAVE_SIGACTION
688     struct sigaction vosf_sa;
689     sigemptyset(&vosf_sa.sa_mask);
690     vosf_sa.sa_handler = (signal_handler)sigsegv_handler;
691     #if !EMULATED_68K && defined(__NetBSD__)
692     sigaddset(&vosf_sa.sa_mask, SIGALRM);
693     vosf_sa.sa_flags = SA_ONSTACK;
694     #else
695     vosf_sa.sa_flags = 0;
696     #endif
697     return (sigaction(sig, &vosf_sa, 0) == 0);
698     #else
699     return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
700     #endif
701     }
702     #endif
703    
704 gbeauche 1.12 bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
705 gbeauche 1.1 {
706 gbeauche 1.2 #ifdef HAVE_SIGSEGV_RECOVERY
707 gbeauche 1.12 sigsegv_fault_handler = handler;
708 gbeauche 1.1 bool success = true;
709     #define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig);
710     SIGSEGV_ALL_SIGNALS
711     #undef FAULT_HANDLER
712     return success;
713     #else
714     // FAIL: no siginfo_t nor sigcontext subterfuge is available
715     return false;
716     #endif
717     }
718    
719    
720     /*
721     * SIGSEGV handler deinitialization
722     */
723    
724     void sigsegv_deinstall_handler(void)
725     {
726 gbeauche 1.2 #ifdef HAVE_SIGSEGV_RECOVERY
727 gbeauche 1.12 sigsegv_fault_handler = 0;
728 gbeauche 1.1 #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
729     SIGSEGV_ALL_SIGNALS
730     #undef FAULT_HANDLER
731 gbeauche 1.2 #endif
732 gbeauche 1.1 }
733    
734 gbeauche 1.10
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 gbeauche 1.12 void sigsegv_set_dump_state(sigsegv_state_dumper_t handler)
750 gbeauche 1.10 {
751 gbeauche 1.12 sigsegv_state_dumper = handler;
752 gbeauche 1.10 }
753    
754    
755 gbeauche 1.1 /*
756     * Test program used for configure/test
757     */
758    
759 gbeauche 1.4 #ifdef CONFIGURE_TEST_SIGSEGV_RECOVERY
760 gbeauche 1.1 #include <stdio.h>
761     #include <stdlib.h>
762     #include <fcntl.h>
763     #include <sys/mman.h>
764 gbeauche 1.4 #include "vm_alloc.h"
765 gbeauche 1.1
766     static int page_size;
767 gbeauche 1.3 static volatile char * page = 0;
768     static volatile int handler_called = 0;
769 gbeauche 1.1
770     static bool sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
771     {
772     handler_called++;
773     if ((fault_address - 123) != page)
774     exit(1);
775 gbeauche 1.4 if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0)
776 gbeauche 1.1 exit(1);
777     return true;
778     }
779    
780 gbeauche 1.10 #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 gbeauche 1.1 int main(void)
788     {
789 gbeauche 1.4 if (vm_init() < 0)
790 gbeauche 1.1 return 1;
791    
792     page_size = getpagesize();
793 gbeauche 1.4 if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED)
794     return 1;
795    
796     if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0)
797 gbeauche 1.1 return 1;
798    
799     if (!sigsegv_install_handler(sigsegv_test_handler))
800     return 1;
801    
802     page[123] = 45;
803     page[123] = 45;
804    
805     if (handler_called != 1)
806     return 1;
807 gbeauche 1.10
808     #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
809     if (!sigsegv_install_handler(sigsegv_insn_handler))
810     return 1;
811    
812 gbeauche 1.17 if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0)
813 gbeauche 1.10 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 gbeauche 1.1
836 gbeauche 1.4 vm_exit();
837 gbeauche 1.1 return 0;
838     }
839     #endif