ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
Revision: 1.21
Committed: 2002-10-03T15:49:14Z (21 years, 11 months ago) by gbeauche
Branch: MAIN
CVS Tags: nigel-build-12, nigel-build-13
Changes since 1.20: +0 -3 lines
Log Message:
configure script is reportedly no longer crashing on FreeBSD when
SIGSEGV_SKIP_INSTRUCTION is set.

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.19 #if defined(__NetBSD__) || defined(__FreeBSD__)
219 gbeauche 1.17 #if (defined(i386) || defined(__i386__))
220     #define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_eip)
221 gbeauche 1.18 #define SIGSEGV_REGISTER_FILE ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */
222 gbeauche 1.17 #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
223 gbeauche 1.19 #endif
224 gbeauche 1.17 #endif
225 gbeauche 1.5 #if defined(__linux__)
226 gbeauche 1.6 #if (defined(i386) || defined(__i386__))
227     #include <sys/ucontext.h>
228 gbeauche 1.14 #define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs)
229     #define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[14] /* should use REG_EIP instead */
230     #define SIGSEGV_REGISTER_FILE (unsigned int *)SIGSEGV_CONTEXT_REGS
231 gbeauche 1.10 #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
232 gbeauche 1.6 #endif
233 gbeauche 1.20 #if (defined(x86_64) || defined(__x86_64__))
234     #include <sys/ucontext.h>
235     #define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs)
236     #define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[16] /* should use REG_RIP instead */
237     #define SIGSEGV_REGISTER_FILE (unsigned long *)SIGSEGV_CONTEXT_REGS
238     #endif
239 gbeauche 1.5 #if (defined(ia64) || defined(__ia64__))
240     #define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */
241     #endif
242 gbeauche 1.9 #if (defined(powerpc) || defined(__powerpc__))
243     #include <sys/ucontext.h>
244 gbeauche 1.14 #define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.regs)
245     #define SIGSEGV_FAULT_INSTRUCTION (SIGSEGV_CONTEXT_REGS->nip)
246     #define SIGSEGV_REGISTER_FILE (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr)
247 gbeauche 1.13 #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
248 gbeauche 1.9 #endif
249 gbeauche 1.5 #endif
250 gbeauche 1.1 #endif
251    
252     #if HAVE_SIGCONTEXT_SUBTERFUGE
253     // Linux kernels prior to 2.4 ?
254     #if defined(__linux__)
255     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
256     #if (defined(i386) || defined(__i386__))
257     #include <asm/sigcontext.h>
258     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs
259     #define SIGSEGV_FAULT_ADDRESS scs.cr2
260     #define SIGSEGV_FAULT_INSTRUCTION scs.eip
261 gbeauche 1.16 #define SIGSEGV_REGISTER_FILE (unsigned int *)(&scs)
262 gbeauche 1.10 #define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction
263 gbeauche 1.1 #endif
264     #if (defined(sparc) || defined(__sparc__))
265     #include <asm/sigcontext.h>
266 gbeauche 1.5 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr
267 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS addr
268     #endif
269     #if (defined(powerpc) || defined(__powerpc__))
270     #include <asm/sigcontext.h>
271 gbeauche 1.4 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp
272 gbeauche 1.1 #define SIGSEGV_FAULT_ADDRESS scp->regs->dar
273     #define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip
274 gbeauche 1.14 #define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr)
275 gbeauche 1.13 #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
276 gbeauche 1.1 #endif
277 gbeauche 1.4 #if (defined(alpha) || defined(__alpha__))
278     #include <asm/sigcontext.h>
279     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
280     #define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
281     #define SIGSEGV_FAULT_INSTRUCTION scp->sc_pc
282    
283     // From Boehm's GC 6.0alpha8
284     static sigsegv_address_t get_fault_address(struct sigcontext *scp)
285     {
286     unsigned int instruction = *((unsigned int *)(scp->sc_pc));
287     unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f];
288     fault_address += (signed long)(signed short)(instruction & 0xffff);
289     return (sigsegv_address_t)fault_address;
290     }
291     #endif
292 gbeauche 1.1 #endif
293    
294     // Irix 5 or 6 on MIPS
295     #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4))
296 gbeauche 1.11 #include <ucontext.h>
297 gbeauche 1.1 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
298     #define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr
299     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
300     #endif
301    
302 gbeauche 1.11 // HP-UX
303     #if (defined(hpux) || defined(__hpux__))
304     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
305     #define SIGSEGV_FAULT_ADDRESS scp->sc_sl.sl_ss.ss_narrow.ss_cr21
306     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS)
307     #endif
308    
309 gbeauche 1.1 // OSF/1 on Alpha
310     #if defined(__osf__)
311 gbeauche 1.11 #include <ucontext.h>
312 gbeauche 1.1 #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
313     #define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0
314     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
315     #endif
316    
317     // AIX
318     #if defined(_AIX)
319     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
320     #define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr
321     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
322     #endif
323    
324     // NetBSD or FreeBSD
325     #if defined(__NetBSD__) || defined(__FreeBSD__)
326     #if (defined(m68k) || defined(__m68k__))
327     #include <m68k/frame.h>
328     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
329 gbeauche 1.14 #define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
330 gbeauche 1.1 #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
331 gbeauche 1.14
332     // Use decoding scheme from BasiliskII/m68k native
333     static sigsegv_address_t get_fault_address(struct sigcontext *scp)
334     {
335     struct sigstate {
336     int ss_flags;
337     struct frame ss_frame;
338     };
339     struct sigstate *state = (struct sigstate *)scp->sc_ap;
340     char *fault_addr;
341     switch (state->ss_frame.f_format) {
342     case 7: /* 68040 access error */
343     /* "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown */
344     fault_addr = state->ss_frame.f_fmt7.f_fa;
345     break;
346     default:
347     fault_addr = (char *)code;
348     break;
349     }
350     return (sigsegv_address_t)fault_addr;
351     }
352 gbeauche 1.1 #else
353     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, void *scp, char *addr
354     #define SIGSEGV_FAULT_ADDRESS addr
355     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
356     #endif
357     #endif
358 gbeauche 1.4
359     // MacOS X
360     #if defined(__APPLE__) && defined(__MACH__)
361     #if (defined(ppc) || defined(__ppc__))
362     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
363     #define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
364     #define SIGSEGV_FAULT_INSTRUCTION scp->sc_ir
365     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
366 gbeauche 1.14 #define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->sc_ir, &((unsigned int *) scp->sc_regs)[2]
367     #define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
368 gbeauche 1.4
369 gbeauche 1.14 // Use decoding scheme from SheepShaver
370 gbeauche 1.4 static sigsegv_address_t get_fault_address(struct sigcontext *scp)
371     {
372 gbeauche 1.14 unsigned int nip = (unsigned int) scp->sc_ir;
373     unsigned int * gpr = &((unsigned int *) scp->sc_regs)[2];
374     instruction_t instr;
375    
376     powerpc_decode_instruction(&instr, nip, gpr);
377     return (sigsegv_address_t)instr.addr;
378 gbeauche 1.4 }
379     #endif
380     #endif
381 gbeauche 1.1 #endif
382    
383 gbeauche 1.14
384     /*
385     * Instruction skipping
386     */
387    
388 gbeauche 1.10 #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
389     // Decode and skip X86 instruction
390     #if (defined(i386) || defined(__i386__))
391     #if defined(__linux__)
392     enum {
393     X86_REG_EIP = 14,
394     X86_REG_EAX = 11,
395     X86_REG_ECX = 10,
396     X86_REG_EDX = 9,
397     X86_REG_EBX = 8,
398     X86_REG_ESP = 7,
399     X86_REG_EBP = 6,
400     X86_REG_ESI = 5,
401     X86_REG_EDI = 4
402     };
403     #endif
404 gbeauche 1.17 #if defined(__NetBSD__) || defined(__FreeBSD__)
405     enum {
406     X86_REG_EIP = 10,
407     X86_REG_EAX = 7,
408     X86_REG_ECX = 6,
409     X86_REG_EDX = 5,
410     X86_REG_EBX = 4,
411     X86_REG_ESP = 13,
412     X86_REG_EBP = 2,
413     X86_REG_ESI = 1,
414     X86_REG_EDI = 0
415     };
416     #endif
417 gbeauche 1.10 // FIXME: this is partly redundant with the instruction decoding phase
418     // to discover transfer type and register number
419     static inline int ix86_step_over_modrm(unsigned char * p)
420     {
421     int mod = (p[0] >> 6) & 3;
422     int rm = p[0] & 7;
423     int offset = 0;
424    
425     // ModR/M Byte
426     switch (mod) {
427     case 0: // [reg]
428     if (rm == 5) return 4; // disp32
429     break;
430     case 1: // disp8[reg]
431     offset = 1;
432     break;
433     case 2: // disp32[reg]
434     offset = 4;
435     break;
436     case 3: // register
437     return 0;
438     }
439    
440     // SIB Byte
441     if (rm == 4) {
442     if (mod == 0 && (p[1] & 7) == 5)
443     offset = 5; // disp32[index]
444     else
445     offset++;
446     }
447    
448     return offset;
449     }
450    
451 gbeauche 1.14 static bool ix86_skip_instruction(unsigned int * regs)
452 gbeauche 1.10 {
453 gbeauche 1.14 unsigned char * eip = (unsigned char *)regs[X86_REG_EIP];
454 gbeauche 1.10
455     if (eip == 0)
456     return false;
457    
458 gbeauche 1.14 transfer_type_t transfer_type = TYPE_UNKNOWN;
459     transfer_size_t transfer_size = SIZE_LONG;
460 gbeauche 1.10
461     int reg = -1;
462     int len = 0;
463    
464     // Operand size prefix
465     if (*eip == 0x66) {
466     eip++;
467     len++;
468     transfer_size = SIZE_WORD;
469     }
470    
471     // Decode instruction
472     switch (eip[0]) {
473 gbeauche 1.17 case 0x0f:
474 gbeauche 1.18 switch (eip[1]) {
475     case 0xb6: // MOVZX r32, r/m8
476     case 0xb7: // MOVZX r32, r/m16
477 gbeauche 1.17 switch (eip[2] & 0xc0) {
478     case 0x80:
479     reg = (eip[2] >> 3) & 7;
480     transfer_type = TYPE_LOAD;
481     break;
482     case 0x40:
483     reg = (eip[2] >> 3) & 7;
484     transfer_type = TYPE_LOAD;
485     break;
486     case 0x00:
487     reg = (eip[2] >> 3) & 7;
488     transfer_type = TYPE_LOAD;
489     break;
490     }
491     len += 3 + ix86_step_over_modrm(eip + 2);
492 gbeauche 1.18 break;
493 gbeauche 1.17 }
494     break;
495 gbeauche 1.10 case 0x8a: // MOV r8, r/m8
496     transfer_size = SIZE_BYTE;
497     case 0x8b: // MOV r32, r/m32 (or 16-bit operation)
498     switch (eip[1] & 0xc0) {
499     case 0x80:
500     reg = (eip[1] >> 3) & 7;
501     transfer_type = TYPE_LOAD;
502     break;
503     case 0x40:
504     reg = (eip[1] >> 3) & 7;
505     transfer_type = TYPE_LOAD;
506     break;
507     case 0x00:
508     reg = (eip[1] >> 3) & 7;
509     transfer_type = TYPE_LOAD;
510     break;
511     }
512     len += 2 + ix86_step_over_modrm(eip + 1);
513     break;
514     case 0x88: // MOV r/m8, r8
515     transfer_size = SIZE_BYTE;
516     case 0x89: // MOV r/m32, r32 (or 16-bit operation)
517     switch (eip[1] & 0xc0) {
518     case 0x80:
519     reg = (eip[1] >> 3) & 7;
520     transfer_type = TYPE_STORE;
521     break;
522     case 0x40:
523     reg = (eip[1] >> 3) & 7;
524     transfer_type = TYPE_STORE;
525     break;
526     case 0x00:
527     reg = (eip[1] >> 3) & 7;
528     transfer_type = TYPE_STORE;
529     break;
530     }
531     len += 2 + ix86_step_over_modrm(eip + 1);
532     break;
533     }
534    
535     if (transfer_type == TYPE_UNKNOWN) {
536     // Unknown machine code, let it crash. Then patch the decoder
537     return false;
538     }
539    
540     if (transfer_type == TYPE_LOAD && reg != -1) {
541     static const int x86_reg_map[8] = {
542     X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX,
543     X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI
544     };
545    
546     if (reg < 0 || reg >= 8)
547     return false;
548    
549     int rloc = x86_reg_map[reg];
550     switch (transfer_size) {
551     case SIZE_BYTE:
552     regs[rloc] = (regs[rloc] & ~0xff);
553     break;
554     case SIZE_WORD:
555     regs[rloc] = (regs[rloc] & ~0xffff);
556     break;
557     case SIZE_LONG:
558     regs[rloc] = 0;
559     break;
560     }
561     }
562    
563     #if DEBUG
564 gbeauche 1.15 printf("%08x: %s %s access", regs[X86_REG_EIP],
565 gbeauche 1.10 transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long",
566     transfer_type == TYPE_LOAD ? "read" : "write");
567    
568     if (reg != -1) {
569     static const char * x86_reg_str_map[8] = {
570     "eax", "ecx", "edx", "ebx",
571     "esp", "ebp", "esi", "edi"
572     };
573     printf(" %s register %%%s", transfer_type == TYPE_LOAD ? "to" : "from", x86_reg_str_map[reg]);
574     }
575     printf(", %d bytes instruction\n", len);
576     #endif
577    
578     regs[X86_REG_EIP] += len;
579 gbeauche 1.13 return true;
580     }
581     #endif
582 gbeauche 1.14
583 gbeauche 1.13 // Decode and skip PPC instruction
584 gbeauche 1.14 #if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__))
585     static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs)
586 gbeauche 1.13 {
587 gbeauche 1.14 instruction_t instr;
588     powerpc_decode_instruction(&instr, *nip_p, regs);
589 gbeauche 1.13
590 gbeauche 1.14 if (instr.transfer_type == TYPE_UNKNOWN) {
591 gbeauche 1.13 // Unknown machine code, let it crash. Then patch the decoder
592     return false;
593     }
594    
595     #if DEBUG
596 gbeauche 1.14 printf("%08x: %s %s access", *nip_p,
597     instr.transfer_size == SIZE_BYTE ? "byte" : instr.transfer_size == SIZE_WORD ? "word" : "long",
598     instr.transfer_type == TYPE_LOAD ? "read" : "write");
599    
600     if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX)
601     printf(" r%d (ra = %08x)\n", instr.ra, instr.addr);
602     if (instr.transfer_type == TYPE_LOAD)
603     printf(" r%d (rd = 0)\n", instr.rd);
604     #endif
605    
606     if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX)
607     regs[instr.ra] = instr.addr;
608     if (instr.transfer_type == TYPE_LOAD)
609     regs[instr.rd] = 0;
610 gbeauche 1.13
611 gbeauche 1.14 *nip_p += 4;
612 gbeauche 1.10 return true;
613     }
614     #endif
615     #endif
616    
617 gbeauche 1.1 // Fallbacks
618     #ifndef SIGSEGV_FAULT_INSTRUCTION
619     #define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC
620     #endif
621    
622 gbeauche 1.2 // SIGSEGV recovery supported ?
623     #if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS)
624     #define HAVE_SIGSEGV_RECOVERY
625     #endif
626    
627 gbeauche 1.1
628     /*
629     * SIGSEGV global handler
630     */
631    
632 gbeauche 1.2 #ifdef HAVE_SIGSEGV_RECOVERY
633 gbeauche 1.1 static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
634     {
635 gbeauche 1.10 sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS;
636     sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION;
637     bool fault_recovered = false;
638    
639 gbeauche 1.1 // Call user's handler and reinstall the global handler, if required
640 gbeauche 1.12 if (sigsegv_fault_handler(fault_address, fault_instruction)) {
641 gbeauche 1.1 #if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
642     sigsegv_do_install_handler(sig);
643     #endif
644 gbeauche 1.10 fault_recovered = true;
645 gbeauche 1.1 }
646 gbeauche 1.10 #if HAVE_SIGSEGV_SKIP_INSTRUCTION
647     else if (sigsegv_ignore_fault) {
648     // Call the instruction skipper with the register file available
649 gbeauche 1.14 if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE))
650 gbeauche 1.10 fault_recovered = true;
651     }
652     #endif
653    
654     if (!fault_recovered) {
655 gbeauche 1.1 // FAIL: reinstall default handler for "safe" crash
656     #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
657     SIGSEGV_ALL_SIGNALS
658     #undef FAULT_HANDLER
659 gbeauche 1.10
660     // We can't do anything with the fault_address, dump state?
661 gbeauche 1.12 if (sigsegv_state_dumper != 0)
662     sigsegv_state_dumper(fault_address, fault_instruction);
663 gbeauche 1.1 }
664     }
665 gbeauche 1.2 #endif
666 gbeauche 1.1
667    
668     /*
669     * SIGSEGV handler initialization
670     */
671    
672     #if defined(HAVE_SIGINFO_T)
673     static bool sigsegv_do_install_handler(int sig)
674     {
675     // Setup SIGSEGV handler to process writes to frame buffer
676     #ifdef HAVE_SIGACTION
677     struct sigaction vosf_sa;
678     sigemptyset(&vosf_sa.sa_mask);
679     vosf_sa.sa_sigaction = sigsegv_handler;
680     vosf_sa.sa_flags = SA_SIGINFO;
681     return (sigaction(sig, &vosf_sa, 0) == 0);
682     #else
683     return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
684     #endif
685     }
686 gbeauche 1.2 #endif
687    
688     #if defined(HAVE_SIGCONTEXT_SUBTERFUGE)
689 gbeauche 1.1 static bool sigsegv_do_install_handler(int sig)
690     {
691     // Setup SIGSEGV handler to process writes to frame buffer
692     #ifdef HAVE_SIGACTION
693     struct sigaction vosf_sa;
694     sigemptyset(&vosf_sa.sa_mask);
695     vosf_sa.sa_handler = (signal_handler)sigsegv_handler;
696     #if !EMULATED_68K && defined(__NetBSD__)
697     sigaddset(&vosf_sa.sa_mask, SIGALRM);
698     vosf_sa.sa_flags = SA_ONSTACK;
699     #else
700     vosf_sa.sa_flags = 0;
701     #endif
702     return (sigaction(sig, &vosf_sa, 0) == 0);
703     #else
704     return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
705     #endif
706     }
707     #endif
708    
709 gbeauche 1.12 bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
710 gbeauche 1.1 {
711 gbeauche 1.2 #ifdef HAVE_SIGSEGV_RECOVERY
712 gbeauche 1.12 sigsegv_fault_handler = handler;
713 gbeauche 1.1 bool success = true;
714     #define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig);
715     SIGSEGV_ALL_SIGNALS
716     #undef FAULT_HANDLER
717     return success;
718     #else
719     // FAIL: no siginfo_t nor sigcontext subterfuge is available
720     return false;
721     #endif
722     }
723    
724    
725     /*
726     * SIGSEGV handler deinitialization
727     */
728    
729     void sigsegv_deinstall_handler(void)
730     {
731 gbeauche 1.2 #ifdef HAVE_SIGSEGV_RECOVERY
732 gbeauche 1.12 sigsegv_fault_handler = 0;
733 gbeauche 1.1 #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
734     SIGSEGV_ALL_SIGNALS
735     #undef FAULT_HANDLER
736 gbeauche 1.2 #endif
737 gbeauche 1.1 }
738    
739 gbeauche 1.10
740     /*
741     * SIGSEGV ignore state modifier
742     */
743    
744     void sigsegv_set_ignore_state(bool ignore_fault)
745     {
746     sigsegv_ignore_fault = ignore_fault;
747     }
748    
749    
750     /*
751     * Set callback function when we cannot handle the fault
752     */
753    
754 gbeauche 1.12 void sigsegv_set_dump_state(sigsegv_state_dumper_t handler)
755 gbeauche 1.10 {
756 gbeauche 1.12 sigsegv_state_dumper = handler;
757 gbeauche 1.10 }
758    
759    
760 gbeauche 1.1 /*
761     * Test program used for configure/test
762     */
763    
764 gbeauche 1.4 #ifdef CONFIGURE_TEST_SIGSEGV_RECOVERY
765 gbeauche 1.1 #include <stdio.h>
766     #include <stdlib.h>
767     #include <fcntl.h>
768     #include <sys/mman.h>
769 gbeauche 1.4 #include "vm_alloc.h"
770 gbeauche 1.1
771     static int page_size;
772 gbeauche 1.3 static volatile char * page = 0;
773     static volatile int handler_called = 0;
774 gbeauche 1.1
775     static bool sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
776     {
777     handler_called++;
778     if ((fault_address - 123) != page)
779     exit(1);
780 gbeauche 1.4 if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0)
781 gbeauche 1.1 exit(1);
782     return true;
783     }
784    
785 gbeauche 1.10 #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
786     static bool sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
787     {
788     return false;
789     }
790     #endif
791    
792 gbeauche 1.1 int main(void)
793     {
794 gbeauche 1.4 if (vm_init() < 0)
795 gbeauche 1.1 return 1;
796    
797     page_size = getpagesize();
798 gbeauche 1.4 if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED)
799     return 1;
800    
801     if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0)
802 gbeauche 1.1 return 1;
803    
804     if (!sigsegv_install_handler(sigsegv_test_handler))
805     return 1;
806    
807     page[123] = 45;
808     page[123] = 45;
809    
810     if (handler_called != 1)
811     return 1;
812 gbeauche 1.10
813     #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
814     if (!sigsegv_install_handler(sigsegv_insn_handler))
815     return 1;
816    
817 gbeauche 1.17 if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0)
818 gbeauche 1.10 return 1;
819    
820     for (int i = 0; i < page_size; i++)
821     page[i] = (i + 1) % page_size;
822    
823     if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0)
824     return 1;
825    
826     sigsegv_set_ignore_state(true);
827    
828     #define TEST_SKIP_INSTRUCTION(TYPE) do { \
829     const unsigned int TAG = 0x12345678; \
830     TYPE data = *((TYPE *)(page + sizeof(TYPE))); \
831     volatile unsigned int effect = data + TAG; \
832     if (effect != TAG) \
833     return 1; \
834     } while (0)
835    
836     TEST_SKIP_INSTRUCTION(unsigned char);
837     TEST_SKIP_INSTRUCTION(unsigned short);
838     TEST_SKIP_INSTRUCTION(unsigned int);
839     #endif
840 gbeauche 1.1
841 gbeauche 1.4 vm_exit();
842 gbeauche 1.1 return 0;
843     }
844     #endif