ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
Revision: 1.19
Committed: 2002-06-27T14:28:59Z (22 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.18: +2 -0 lines
Log Message:
Correctly guard FreeBSD bits

File Contents

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