ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
Revision: 1.22
Committed: 2003-05-14T06:50:05Z (21 years, 6 months ago) by gbeauche
Branch: MAIN
Changes since 1.21: +122 -74 lines
Log Message:
New API to ignore a SIGSEGV fault. This should help on SheepShaver/x86 for now
since I still don't know why MacOS would like to write to ROM on a particular
test.

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