ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
Revision: 1.15
Committed: 2002-05-20T16:00:07Z (22 years, 5 months ago) by gbeauche
Branch: MAIN
Changes since 1.14: +1 -1 lines
Log Message:
- sigsegv.cpp (ix86_skip_instruction): Fix DEBUG compilation.

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