ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
Revision: 1.26
Committed: 2003-09-29T08:04:22Z (21 years, 2 months ago) by gbeauche
Branch: MAIN
Changes since 1.25: +1 -1 lines
Log Message:
really fix typo!

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