ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
Revision: 1.23
Committed: 2003-08-17T10:52:52Z (21 years, 2 months ago) by gbeauche
Branch: MAIN
Changes since 1.22: +3 -1 lines
Log Message:
Bring x86 instruction skipper back to life

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