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 |
gbeauche |
1.27 |
* MacOS X support derived from the post by Timothy J. Wood to the |
8 |
|
|
* omnigroup macosx-dev list: |
9 |
|
|
* Mach Exception Handlers 101 (Was Re: ptrace, gdb) |
10 |
|
|
* tjw@omnigroup.com Sun, 4 Jun 2000 |
11 |
|
|
* www.omnigroup.com/mailman/archive/macosx-dev/2000-June/002030.html |
12 |
|
|
* |
13 |
cebix |
1.7 |
* Basilisk II (C) 1997-2002 Christian Bauer |
14 |
gbeauche |
1.1 |
* |
15 |
|
|
* This program is free software; you can redistribute it and/or modify |
16 |
|
|
* it under the terms of the GNU General Public License as published by |
17 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
18 |
|
|
* (at your option) any later version. |
19 |
|
|
* |
20 |
|
|
* This program is distributed in the hope that it will be useful, |
21 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 |
|
|
* GNU General Public License for more details. |
24 |
|
|
* |
25 |
|
|
* You should have received a copy of the GNU General Public License |
26 |
|
|
* along with this program; if not, write to the Free Software |
27 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
|
|
*/ |
29 |
|
|
|
30 |
|
|
#ifdef HAVE_UNISTD_H |
31 |
|
|
#include <unistd.h> |
32 |
|
|
#endif |
33 |
|
|
|
34 |
|
|
#ifdef HAVE_CONFIG_H |
35 |
|
|
#include "config.h" |
36 |
|
|
#endif |
37 |
|
|
|
38 |
gbeauche |
1.22 |
#include <list> |
39 |
gbeauche |
1.1 |
#include <signal.h> |
40 |
|
|
#include "sigsegv.h" |
41 |
|
|
|
42 |
gbeauche |
1.22 |
#ifndef NO_STD_NAMESPACE |
43 |
|
|
using std::list; |
44 |
|
|
#endif |
45 |
|
|
|
46 |
gbeauche |
1.1 |
// Return value type of a signal handler (standard type if not defined) |
47 |
|
|
#ifndef RETSIGTYPE |
48 |
|
|
#define RETSIGTYPE void |
49 |
|
|
#endif |
50 |
|
|
|
51 |
|
|
// Type of the system signal handler |
52 |
|
|
typedef RETSIGTYPE (*signal_handler)(int); |
53 |
|
|
|
54 |
|
|
// User's SIGSEGV handler |
55 |
gbeauche |
1.12 |
static sigsegv_fault_handler_t sigsegv_fault_handler = 0; |
56 |
gbeauche |
1.1 |
|
57 |
gbeauche |
1.10 |
// Function called to dump state if we can't handle the fault |
58 |
gbeauche |
1.12 |
static sigsegv_state_dumper_t sigsegv_state_dumper = 0; |
59 |
gbeauche |
1.10 |
|
60 |
gbeauche |
1.1 |
// Actual SIGSEGV handler installer |
61 |
|
|
static bool sigsegv_do_install_handler(int sig); |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
/* |
65 |
gbeauche |
1.14 |
* Instruction decoding aids |
66 |
|
|
*/ |
67 |
|
|
|
68 |
|
|
// Transfer size |
69 |
|
|
enum transfer_size_t { |
70 |
|
|
SIZE_UNKNOWN, |
71 |
|
|
SIZE_BYTE, |
72 |
|
|
SIZE_WORD, |
73 |
|
|
SIZE_LONG |
74 |
|
|
}; |
75 |
|
|
|
76 |
gbeauche |
1.23 |
// Transfer type |
77 |
|
|
typedef sigsegv_transfer_type_t transfer_type_t; |
78 |
|
|
|
79 |
gbeauche |
1.14 |
#if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__)) |
80 |
|
|
// Addressing mode |
81 |
|
|
enum addressing_mode_t { |
82 |
|
|
MODE_UNKNOWN, |
83 |
|
|
MODE_NORM, |
84 |
|
|
MODE_U, |
85 |
|
|
MODE_X, |
86 |
|
|
MODE_UX |
87 |
|
|
}; |
88 |
|
|
|
89 |
|
|
// Decoded instruction |
90 |
|
|
struct instruction_t { |
91 |
|
|
transfer_type_t transfer_type; |
92 |
|
|
transfer_size_t transfer_size; |
93 |
|
|
addressing_mode_t addr_mode; |
94 |
|
|
unsigned int addr; |
95 |
|
|
char ra, rd; |
96 |
|
|
}; |
97 |
|
|
|
98 |
|
|
static void powerpc_decode_instruction(instruction_t *instruction, unsigned int nip, unsigned int * gpr) |
99 |
|
|
{ |
100 |
|
|
// Get opcode and divide into fields |
101 |
|
|
unsigned int opcode = *((unsigned int *)nip); |
102 |
|
|
unsigned int primop = opcode >> 26; |
103 |
|
|
unsigned int exop = (opcode >> 1) & 0x3ff; |
104 |
|
|
unsigned int ra = (opcode >> 16) & 0x1f; |
105 |
|
|
unsigned int rb = (opcode >> 11) & 0x1f; |
106 |
|
|
unsigned int rd = (opcode >> 21) & 0x1f; |
107 |
|
|
signed int imm = (signed short)(opcode & 0xffff); |
108 |
|
|
|
109 |
|
|
// Analyze opcode |
110 |
gbeauche |
1.22 |
transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN; |
111 |
gbeauche |
1.14 |
transfer_size_t transfer_size = SIZE_UNKNOWN; |
112 |
|
|
addressing_mode_t addr_mode = MODE_UNKNOWN; |
113 |
|
|
switch (primop) { |
114 |
|
|
case 31: |
115 |
|
|
switch (exop) { |
116 |
|
|
case 23: // lwzx |
117 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_X; break; |
118 |
gbeauche |
1.14 |
case 55: // lwzux |
119 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break; |
120 |
gbeauche |
1.14 |
case 87: // lbzx |
121 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break; |
122 |
gbeauche |
1.14 |
case 119: // lbzux |
123 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break; |
124 |
gbeauche |
1.14 |
case 151: // stwx |
125 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_X; break; |
126 |
gbeauche |
1.14 |
case 183: // stwux |
127 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_UX; break; |
128 |
gbeauche |
1.14 |
case 215: // stbx |
129 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break; |
130 |
gbeauche |
1.14 |
case 247: // stbux |
131 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break; |
132 |
gbeauche |
1.14 |
case 279: // lhzx |
133 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break; |
134 |
gbeauche |
1.14 |
case 311: // lhzux |
135 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break; |
136 |
gbeauche |
1.14 |
case 343: // lhax |
137 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break; |
138 |
gbeauche |
1.14 |
case 375: // lhaux |
139 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break; |
140 |
gbeauche |
1.14 |
case 407: // sthx |
141 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break; |
142 |
gbeauche |
1.14 |
case 439: // sthux |
143 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break; |
144 |
gbeauche |
1.14 |
} |
145 |
|
|
break; |
146 |
|
|
|
147 |
|
|
case 32: // lwz |
148 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break; |
149 |
gbeauche |
1.14 |
case 33: // lwzu |
150 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_LONG; addr_mode = MODE_U; break; |
151 |
gbeauche |
1.14 |
case 34: // lbz |
152 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break; |
153 |
gbeauche |
1.14 |
case 35: // lbzu |
154 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break; |
155 |
gbeauche |
1.14 |
case 36: // stw |
156 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_NORM; break; |
157 |
gbeauche |
1.14 |
case 37: // stwu |
158 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_LONG; addr_mode = MODE_U; break; |
159 |
gbeauche |
1.14 |
case 38: // stb |
160 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break; |
161 |
gbeauche |
1.14 |
case 39: // stbu |
162 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break; |
163 |
gbeauche |
1.14 |
case 40: // lhz |
164 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break; |
165 |
gbeauche |
1.14 |
case 41: // lhzu |
166 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break; |
167 |
gbeauche |
1.14 |
case 42: // lha |
168 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break; |
169 |
gbeauche |
1.14 |
case 43: // lhau |
170 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break; |
171 |
gbeauche |
1.14 |
case 44: // sth |
172 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break; |
173 |
gbeauche |
1.14 |
case 45: // sthu |
174 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break; |
175 |
gbeauche |
1.14 |
} |
176 |
|
|
|
177 |
|
|
// Calculate effective address |
178 |
|
|
unsigned int addr = 0; |
179 |
|
|
switch (addr_mode) { |
180 |
|
|
case MODE_X: |
181 |
|
|
case MODE_UX: |
182 |
|
|
if (ra == 0) |
183 |
|
|
addr = gpr[rb]; |
184 |
|
|
else |
185 |
|
|
addr = gpr[ra] + gpr[rb]; |
186 |
|
|
break; |
187 |
|
|
case MODE_NORM: |
188 |
|
|
case MODE_U: |
189 |
|
|
if (ra == 0) |
190 |
|
|
addr = (signed int)(signed short)imm; |
191 |
|
|
else |
192 |
|
|
addr = gpr[ra] + (signed int)(signed short)imm; |
193 |
|
|
break; |
194 |
|
|
default: |
195 |
|
|
break; |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
// Commit decoded instruction |
199 |
|
|
instruction->addr = addr; |
200 |
|
|
instruction->addr_mode = addr_mode; |
201 |
|
|
instruction->transfer_type = transfer_type; |
202 |
|
|
instruction->transfer_size = transfer_size; |
203 |
|
|
instruction->ra = ra; |
204 |
|
|
instruction->rd = rd; |
205 |
|
|
} |
206 |
|
|
#endif |
207 |
|
|
|
208 |
|
|
|
209 |
|
|
/* |
210 |
gbeauche |
1.1 |
* OS-dependant SIGSEGV signals support section |
211 |
|
|
*/ |
212 |
|
|
|
213 |
|
|
#if HAVE_SIGINFO_T |
214 |
|
|
// Generic extended signal handler |
215 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER sigsegv_fault_handler |
216 |
cebix |
1.8 |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
217 |
|
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
218 |
|
|
#else |
219 |
gbeauche |
1.1 |
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
220 |
cebix |
1.8 |
#endif |
221 |
gbeauche |
1.5 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp |
222 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, sip, scp |
223 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_ADDRESS sip->si_addr |
224 |
gbeauche |
1.19 |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
225 |
gbeauche |
1.17 |
#if (defined(i386) || defined(__i386__)) |
226 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_eip) |
227 |
gbeauche |
1.18 |
#define SIGSEGV_REGISTER_FILE ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */ |
228 |
gbeauche |
1.17 |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
229 |
gbeauche |
1.19 |
#endif |
230 |
gbeauche |
1.17 |
#endif |
231 |
gbeauche |
1.5 |
#if defined(__linux__) |
232 |
gbeauche |
1.6 |
#if (defined(i386) || defined(__i386__)) |
233 |
|
|
#include <sys/ucontext.h> |
234 |
gbeauche |
1.14 |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
235 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[14] /* should use REG_EIP instead */ |
236 |
|
|
#define SIGSEGV_REGISTER_FILE (unsigned int *)SIGSEGV_CONTEXT_REGS |
237 |
gbeauche |
1.10 |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
238 |
gbeauche |
1.6 |
#endif |
239 |
gbeauche |
1.20 |
#if (defined(x86_64) || defined(__x86_64__)) |
240 |
|
|
#include <sys/ucontext.h> |
241 |
|
|
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
242 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[16] /* should use REG_RIP instead */ |
243 |
|
|
#define SIGSEGV_REGISTER_FILE (unsigned long *)SIGSEGV_CONTEXT_REGS |
244 |
|
|
#endif |
245 |
gbeauche |
1.5 |
#if (defined(ia64) || defined(__ia64__)) |
246 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */ |
247 |
|
|
#endif |
248 |
gbeauche |
1.9 |
#if (defined(powerpc) || defined(__powerpc__)) |
249 |
|
|
#include <sys/ucontext.h> |
250 |
gbeauche |
1.14 |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.regs) |
251 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION (SIGSEGV_CONTEXT_REGS->nip) |
252 |
|
|
#define SIGSEGV_REGISTER_FILE (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr) |
253 |
gbeauche |
1.13 |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
254 |
gbeauche |
1.9 |
#endif |
255 |
gbeauche |
1.5 |
#endif |
256 |
gbeauche |
1.1 |
#endif |
257 |
|
|
|
258 |
|
|
#if HAVE_SIGCONTEXT_SUBTERFUGE |
259 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER sigsegv_fault_handler |
260 |
gbeauche |
1.1 |
// Linux kernels prior to 2.4 ? |
261 |
|
|
#if defined(__linux__) |
262 |
|
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
263 |
|
|
#if (defined(i386) || defined(__i386__)) |
264 |
|
|
#include <asm/sigcontext.h> |
265 |
|
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs |
266 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, scs |
267 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_ADDRESS scs.cr2 |
268 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION scs.eip |
269 |
gbeauche |
1.16 |
#define SIGSEGV_REGISTER_FILE (unsigned int *)(&scs) |
270 |
gbeauche |
1.10 |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
271 |
gbeauche |
1.1 |
#endif |
272 |
|
|
#if (defined(sparc) || defined(__sparc__)) |
273 |
|
|
#include <asm/sigcontext.h> |
274 |
gbeauche |
1.5 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr |
275 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr |
276 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_ADDRESS addr |
277 |
|
|
#endif |
278 |
|
|
#if (defined(powerpc) || defined(__powerpc__)) |
279 |
|
|
#include <asm/sigcontext.h> |
280 |
gbeauche |
1.4 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp |
281 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, scp |
282 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_ADDRESS scp->regs->dar |
283 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip |
284 |
gbeauche |
1.14 |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr) |
285 |
gbeauche |
1.13 |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
286 |
gbeauche |
1.1 |
#endif |
287 |
gbeauche |
1.4 |
#if (defined(alpha) || defined(__alpha__)) |
288 |
|
|
#include <asm/sigcontext.h> |
289 |
|
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
290 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
291 |
gbeauche |
1.4 |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
292 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_pc |
293 |
|
|
|
294 |
|
|
// From Boehm's GC 6.0alpha8 |
295 |
|
|
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
296 |
|
|
{ |
297 |
|
|
unsigned int instruction = *((unsigned int *)(scp->sc_pc)); |
298 |
|
|
unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f]; |
299 |
|
|
fault_address += (signed long)(signed short)(instruction & 0xffff); |
300 |
|
|
return (sigsegv_address_t)fault_address; |
301 |
|
|
} |
302 |
|
|
#endif |
303 |
gbeauche |
1.1 |
#endif |
304 |
|
|
|
305 |
|
|
// Irix 5 or 6 on MIPS |
306 |
|
|
#if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4)) |
307 |
gbeauche |
1.11 |
#include <ucontext.h> |
308 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
309 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
310 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr |
311 |
|
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
312 |
|
|
#endif |
313 |
|
|
|
314 |
gbeauche |
1.11 |
// HP-UX |
315 |
|
|
#if (defined(hpux) || defined(__hpux__)) |
316 |
|
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
317 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
318 |
gbeauche |
1.11 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_sl.sl_ss.ss_narrow.ss_cr21 |
319 |
|
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS) |
320 |
|
|
#endif |
321 |
|
|
|
322 |
gbeauche |
1.1 |
// OSF/1 on Alpha |
323 |
|
|
#if defined(__osf__) |
324 |
gbeauche |
1.11 |
#include <ucontext.h> |
325 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
326 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
327 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0 |
328 |
|
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
329 |
|
|
#endif |
330 |
|
|
|
331 |
|
|
// AIX |
332 |
|
|
#if defined(_AIX) |
333 |
|
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
334 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
335 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr |
336 |
|
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
337 |
|
|
#endif |
338 |
|
|
|
339 |
|
|
// NetBSD or FreeBSD |
340 |
|
|
#if defined(__NetBSD__) || defined(__FreeBSD__) |
341 |
|
|
#if (defined(m68k) || defined(__m68k__)) |
342 |
|
|
#include <m68k/frame.h> |
343 |
|
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
344 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, 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 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr |
371 |
gbeauche |
1.1 |
#define SIGSEGV_FAULT_ADDRESS addr |
372 |
|
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
373 |
|
|
#endif |
374 |
|
|
#endif |
375 |
gbeauche |
1.4 |
|
376 |
gbeauche |
1.27 |
// MacOS X, not sure which version this works in. Under 10.1 |
377 |
|
|
// vm_protect does not appear to work from a signal handler. Under |
378 |
|
|
// 10.2 signal handlers get siginfo type arguments but the si_addr |
379 |
|
|
// field is the address of the faulting instruction and not the |
380 |
|
|
// address that caused the SIGBUS. Maybe this works in 10.0? In any |
381 |
|
|
// case with Mach exception handlers there is a way to do what this |
382 |
|
|
// was meant to do. |
383 |
|
|
#ifndef HAVE_MACH_EXCEPTIONS |
384 |
gbeauche |
1.4 |
#if defined(__APPLE__) && defined(__MACH__) |
385 |
|
|
#if (defined(ppc) || defined(__ppc__)) |
386 |
|
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
387 |
gbeauche |
1.27 |
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp |
388 |
gbeauche |
1.4 |
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp) |
389 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_ir |
390 |
|
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
391 |
gbeauche |
1.14 |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->sc_ir, &((unsigned int *) scp->sc_regs)[2] |
392 |
|
|
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
393 |
gbeauche |
1.4 |
|
394 |
gbeauche |
1.14 |
// Use decoding scheme from SheepShaver |
395 |
gbeauche |
1.4 |
static sigsegv_address_t get_fault_address(struct sigcontext *scp) |
396 |
|
|
{ |
397 |
gbeauche |
1.14 |
unsigned int nip = (unsigned int) scp->sc_ir; |
398 |
|
|
unsigned int * gpr = &((unsigned int *) scp->sc_regs)[2]; |
399 |
|
|
instruction_t instr; |
400 |
|
|
|
401 |
|
|
powerpc_decode_instruction(&instr, nip, gpr); |
402 |
|
|
return (sigsegv_address_t)instr.addr; |
403 |
gbeauche |
1.4 |
} |
404 |
|
|
#endif |
405 |
|
|
#endif |
406 |
gbeauche |
1.1 |
#endif |
407 |
gbeauche |
1.27 |
#endif |
408 |
|
|
|
409 |
|
|
#if HAVE_MACH_EXCEPTIONS |
410 |
|
|
|
411 |
|
|
// This can easily be extended to other Mach systems, but really who |
412 |
|
|
// uses HURD (oops GNU/HURD), Darwin/x86, NextStep, Rhapsody, or CMU |
413 |
|
|
// Mach 2.5/3.0? |
414 |
|
|
#if defined(__APPLE__) && defined(__MACH__) |
415 |
|
|
|
416 |
|
|
#include <sys/types.h> |
417 |
|
|
#include <stdlib.h> |
418 |
|
|
#include <stdio.h> |
419 |
|
|
#include <pthread.h> |
420 |
|
|
|
421 |
|
|
/* |
422 |
|
|
* If you are familiar with MIG then you will understand the frustration |
423 |
|
|
* that was necessary to get these embedded into C++ code by hand. |
424 |
|
|
*/ |
425 |
|
|
extern "C" { |
426 |
|
|
#include <mach/mach.h> |
427 |
|
|
#include <mach/mach_error.h> |
428 |
|
|
|
429 |
|
|
extern boolean_t exc_server(mach_msg_header_t *, mach_msg_header_t *); |
430 |
|
|
extern kern_return_t catch_exception_raise(mach_port_t, mach_port_t, |
431 |
|
|
mach_port_t, exception_type_t, exception_data_t, mach_msg_type_number_t); |
432 |
|
|
extern kern_return_t exception_raise(mach_port_t, mach_port_t, mach_port_t, |
433 |
|
|
exception_type_t, exception_data_t, mach_msg_type_number_t); |
434 |
|
|
extern kern_return_t exception_raise_state(mach_port_t, exception_type_t, |
435 |
|
|
exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *, |
436 |
|
|
thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *); |
437 |
|
|
extern kern_return_t exception_raise_state_identity(mach_port_t, mach_port_t, mach_port_t, |
438 |
|
|
exception_type_t, exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *, |
439 |
|
|
thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *); |
440 |
|
|
} |
441 |
|
|
|
442 |
|
|
// Could make this dynamic by looking for a result of MIG_ARRAY_TOO_LARGE |
443 |
|
|
#define HANDLER_COUNT 64 |
444 |
|
|
|
445 |
|
|
// structure to tuck away existing exception handlers |
446 |
|
|
typedef struct _ExceptionPorts { |
447 |
|
|
mach_msg_type_number_t maskCount; |
448 |
|
|
exception_mask_t masks[HANDLER_COUNT]; |
449 |
|
|
exception_handler_t handlers[HANDLER_COUNT]; |
450 |
|
|
exception_behavior_t behaviors[HANDLER_COUNT]; |
451 |
|
|
thread_state_flavor_t flavors[HANDLER_COUNT]; |
452 |
|
|
} ExceptionPorts; |
453 |
|
|
|
454 |
|
|
// exception handler thread |
455 |
|
|
static pthread_t exc_thread; |
456 |
|
|
|
457 |
|
|
// place where old exception handler info is stored |
458 |
|
|
static ExceptionPorts ports; |
459 |
|
|
|
460 |
|
|
// our exception port |
461 |
|
|
static mach_port_t _exceptionPort = MACH_PORT_NULL; |
462 |
|
|
|
463 |
|
|
#define MACH_CHECK_ERROR(name,ret) \ |
464 |
|
|
if (ret != KERN_SUCCESS) { \ |
465 |
|
|
mach_error(#name, ret); \ |
466 |
|
|
exit (1); \ |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
#define SIGSEGV_FAULT_ADDRESS code[1] |
470 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION get_fault_instruction(thread, state) |
471 |
|
|
#define SIGSEGV_FAULT_HANDLER (code[0] == KERN_PROTECTION_FAILURE) && sigsegv_fault_handler |
472 |
|
|
#define SIGSEGV_FAULT_HANDLER_ARGLIST mach_port_t thread, exception_data_t code, ppc_thread_state_t *state |
473 |
|
|
#define SIGSEGV_FAULT_HANDLER_ARGS thread, code, &state |
474 |
|
|
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
475 |
|
|
#define SIGSEGV_REGISTER_FILE &state->srr0, &state->r0 |
476 |
|
|
|
477 |
|
|
// Given a suspended thread, stuff the current instruction and |
478 |
|
|
// registers into state. |
479 |
|
|
// |
480 |
|
|
// It would have been nice to have this be ppc/x86 independant which |
481 |
|
|
// could have been done easily with a thread_state_t instead of |
482 |
|
|
// ppc_thread_state_t, but because of the way this is called it is |
483 |
|
|
// easier to do it this way. |
484 |
|
|
#if (defined(ppc) || defined(__ppc__)) |
485 |
|
|
static inline sigsegv_address_t get_fault_instruction(mach_port_t thread, ppc_thread_state_t *state) |
486 |
|
|
{ |
487 |
|
|
kern_return_t krc; |
488 |
|
|
mach_msg_type_number_t count; |
489 |
|
|
|
490 |
|
|
count = MACHINE_THREAD_STATE_COUNT; |
491 |
|
|
krc = thread_get_state(thread, MACHINE_THREAD_STATE, (thread_state_t)state, &count); |
492 |
|
|
MACH_CHECK_ERROR (thread_get_state, krc); |
493 |
|
|
|
494 |
|
|
return (sigsegv_address_t)state->srr0; |
495 |
|
|
} |
496 |
|
|
#endif |
497 |
|
|
|
498 |
|
|
// Since there can only be one exception thread running at any time |
499 |
|
|
// this is not a problem. |
500 |
|
|
#define MSG_SIZE 512 |
501 |
|
|
static char msgbuf[MSG_SIZE]; |
502 |
|
|
static char replybuf[MSG_SIZE]; |
503 |
|
|
|
504 |
|
|
/* |
505 |
|
|
* This is the entry point for the exception handler thread. The job |
506 |
|
|
* of this thread is to wait for exception messages on the exception |
507 |
|
|
* port that was setup beforehand and to pass them on to exc_server. |
508 |
|
|
* exc_server is a MIG generated function that is a part of Mach. |
509 |
|
|
* Its job is to decide what to do with the exception message. In our |
510 |
|
|
* case exc_server calls catch_exception_raise on our behalf. After |
511 |
|
|
* exc_server returns, it is our responsibility to send the reply. |
512 |
|
|
*/ |
513 |
|
|
static void * |
514 |
|
|
handleExceptions(void *priv) |
515 |
|
|
{ |
516 |
|
|
mach_msg_header_t *msg, *reply; |
517 |
|
|
kern_return_t krc; |
518 |
|
|
|
519 |
|
|
msg = (mach_msg_header_t *)msgbuf; |
520 |
|
|
reply = (mach_msg_header_t *)replybuf; |
521 |
|
|
|
522 |
|
|
for (;;) { |
523 |
|
|
krc = mach_msg(msg, MACH_RCV_MSG, MSG_SIZE, MSG_SIZE, |
524 |
|
|
_exceptionPort, 0, MACH_PORT_NULL); |
525 |
|
|
MACH_CHECK_ERROR(mach_msg, krc); |
526 |
|
|
|
527 |
|
|
if (!exc_server(msg, reply)) { |
528 |
|
|
fprintf(stderr, "exc_server hated the message\n"); |
529 |
|
|
exit(1); |
530 |
|
|
} |
531 |
|
|
|
532 |
|
|
krc = mach_msg(reply, MACH_SEND_MSG, reply->msgh_size, 0, |
533 |
|
|
msg->msgh_local_port, 0, MACH_PORT_NULL); |
534 |
|
|
if (krc != KERN_SUCCESS) { |
535 |
|
|
fprintf(stderr, "Error sending message to original reply port, krc = %d, %s", |
536 |
|
|
krc, mach_error_string(krc)); |
537 |
|
|
exit(1); |
538 |
|
|
} |
539 |
|
|
} |
540 |
|
|
} |
541 |
|
|
#endif |
542 |
|
|
#endif |
543 |
gbeauche |
1.1 |
|
544 |
gbeauche |
1.14 |
|
545 |
|
|
/* |
546 |
|
|
* Instruction skipping |
547 |
|
|
*/ |
548 |
|
|
|
549 |
gbeauche |
1.10 |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
550 |
|
|
// Decode and skip X86 instruction |
551 |
|
|
#if (defined(i386) || defined(__i386__)) |
552 |
|
|
#if defined(__linux__) |
553 |
|
|
enum { |
554 |
|
|
X86_REG_EIP = 14, |
555 |
|
|
X86_REG_EAX = 11, |
556 |
|
|
X86_REG_ECX = 10, |
557 |
|
|
X86_REG_EDX = 9, |
558 |
|
|
X86_REG_EBX = 8, |
559 |
|
|
X86_REG_ESP = 7, |
560 |
|
|
X86_REG_EBP = 6, |
561 |
|
|
X86_REG_ESI = 5, |
562 |
|
|
X86_REG_EDI = 4 |
563 |
|
|
}; |
564 |
|
|
#endif |
565 |
gbeauche |
1.17 |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
566 |
|
|
enum { |
567 |
|
|
X86_REG_EIP = 10, |
568 |
|
|
X86_REG_EAX = 7, |
569 |
|
|
X86_REG_ECX = 6, |
570 |
|
|
X86_REG_EDX = 5, |
571 |
|
|
X86_REG_EBX = 4, |
572 |
|
|
X86_REG_ESP = 13, |
573 |
|
|
X86_REG_EBP = 2, |
574 |
|
|
X86_REG_ESI = 1, |
575 |
|
|
X86_REG_EDI = 0 |
576 |
|
|
}; |
577 |
|
|
#endif |
578 |
gbeauche |
1.10 |
// FIXME: this is partly redundant with the instruction decoding phase |
579 |
|
|
// to discover transfer type and register number |
580 |
|
|
static inline int ix86_step_over_modrm(unsigned char * p) |
581 |
|
|
{ |
582 |
|
|
int mod = (p[0] >> 6) & 3; |
583 |
|
|
int rm = p[0] & 7; |
584 |
|
|
int offset = 0; |
585 |
|
|
|
586 |
|
|
// ModR/M Byte |
587 |
|
|
switch (mod) { |
588 |
|
|
case 0: // [reg] |
589 |
|
|
if (rm == 5) return 4; // disp32 |
590 |
|
|
break; |
591 |
|
|
case 1: // disp8[reg] |
592 |
|
|
offset = 1; |
593 |
|
|
break; |
594 |
|
|
case 2: // disp32[reg] |
595 |
|
|
offset = 4; |
596 |
|
|
break; |
597 |
|
|
case 3: // register |
598 |
|
|
return 0; |
599 |
|
|
} |
600 |
|
|
|
601 |
|
|
// SIB Byte |
602 |
|
|
if (rm == 4) { |
603 |
|
|
if (mod == 0 && (p[1] & 7) == 5) |
604 |
|
|
offset = 5; // disp32[index] |
605 |
|
|
else |
606 |
|
|
offset++; |
607 |
|
|
} |
608 |
|
|
|
609 |
|
|
return offset; |
610 |
|
|
} |
611 |
|
|
|
612 |
gbeauche |
1.14 |
static bool ix86_skip_instruction(unsigned int * regs) |
613 |
gbeauche |
1.10 |
{ |
614 |
gbeauche |
1.14 |
unsigned char * eip = (unsigned char *)regs[X86_REG_EIP]; |
615 |
gbeauche |
1.10 |
|
616 |
|
|
if (eip == 0) |
617 |
|
|
return false; |
618 |
|
|
|
619 |
gbeauche |
1.22 |
transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN; |
620 |
gbeauche |
1.14 |
transfer_size_t transfer_size = SIZE_LONG; |
621 |
gbeauche |
1.10 |
|
622 |
|
|
int reg = -1; |
623 |
|
|
int len = 0; |
624 |
|
|
|
625 |
|
|
// Operand size prefix |
626 |
|
|
if (*eip == 0x66) { |
627 |
|
|
eip++; |
628 |
|
|
len++; |
629 |
|
|
transfer_size = SIZE_WORD; |
630 |
|
|
} |
631 |
|
|
|
632 |
|
|
// Decode instruction |
633 |
|
|
switch (eip[0]) { |
634 |
gbeauche |
1.17 |
case 0x0f: |
635 |
gbeauche |
1.18 |
switch (eip[1]) { |
636 |
|
|
case 0xb6: // MOVZX r32, r/m8 |
637 |
|
|
case 0xb7: // MOVZX r32, r/m16 |
638 |
gbeauche |
1.17 |
switch (eip[2] & 0xc0) { |
639 |
|
|
case 0x80: |
640 |
|
|
reg = (eip[2] >> 3) & 7; |
641 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
642 |
gbeauche |
1.17 |
break; |
643 |
|
|
case 0x40: |
644 |
|
|
reg = (eip[2] >> 3) & 7; |
645 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
646 |
gbeauche |
1.17 |
break; |
647 |
|
|
case 0x00: |
648 |
|
|
reg = (eip[2] >> 3) & 7; |
649 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
650 |
gbeauche |
1.17 |
break; |
651 |
|
|
} |
652 |
|
|
len += 3 + ix86_step_over_modrm(eip + 2); |
653 |
gbeauche |
1.18 |
break; |
654 |
gbeauche |
1.17 |
} |
655 |
|
|
break; |
656 |
gbeauche |
1.10 |
case 0x8a: // MOV r8, r/m8 |
657 |
|
|
transfer_size = SIZE_BYTE; |
658 |
|
|
case 0x8b: // MOV r32, r/m32 (or 16-bit operation) |
659 |
|
|
switch (eip[1] & 0xc0) { |
660 |
|
|
case 0x80: |
661 |
|
|
reg = (eip[1] >> 3) & 7; |
662 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
663 |
gbeauche |
1.10 |
break; |
664 |
|
|
case 0x40: |
665 |
|
|
reg = (eip[1] >> 3) & 7; |
666 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
667 |
gbeauche |
1.10 |
break; |
668 |
|
|
case 0x00: |
669 |
|
|
reg = (eip[1] >> 3) & 7; |
670 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
671 |
gbeauche |
1.10 |
break; |
672 |
|
|
} |
673 |
|
|
len += 2 + ix86_step_over_modrm(eip + 1); |
674 |
|
|
break; |
675 |
|
|
case 0x88: // MOV r/m8, r8 |
676 |
|
|
transfer_size = SIZE_BYTE; |
677 |
|
|
case 0x89: // MOV r/m32, r32 (or 16-bit operation) |
678 |
|
|
switch (eip[1] & 0xc0) { |
679 |
|
|
case 0x80: |
680 |
|
|
reg = (eip[1] >> 3) & 7; |
681 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; |
682 |
gbeauche |
1.10 |
break; |
683 |
|
|
case 0x40: |
684 |
|
|
reg = (eip[1] >> 3) & 7; |
685 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; |
686 |
gbeauche |
1.10 |
break; |
687 |
|
|
case 0x00: |
688 |
|
|
reg = (eip[1] >> 3) & 7; |
689 |
gbeauche |
1.22 |
transfer_type = SIGSEGV_TRANSFER_STORE; |
690 |
gbeauche |
1.10 |
break; |
691 |
|
|
} |
692 |
|
|
len += 2 + ix86_step_over_modrm(eip + 1); |
693 |
|
|
break; |
694 |
|
|
} |
695 |
|
|
|
696 |
gbeauche |
1.22 |
if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) { |
697 |
gbeauche |
1.10 |
// Unknown machine code, let it crash. Then patch the decoder |
698 |
|
|
return false; |
699 |
|
|
} |
700 |
|
|
|
701 |
gbeauche |
1.22 |
if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != -1) { |
702 |
gbeauche |
1.10 |
static const int x86_reg_map[8] = { |
703 |
|
|
X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX, |
704 |
|
|
X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI |
705 |
|
|
}; |
706 |
|
|
|
707 |
|
|
if (reg < 0 || reg >= 8) |
708 |
|
|
return false; |
709 |
|
|
|
710 |
|
|
int rloc = x86_reg_map[reg]; |
711 |
|
|
switch (transfer_size) { |
712 |
|
|
case SIZE_BYTE: |
713 |
|
|
regs[rloc] = (regs[rloc] & ~0xff); |
714 |
|
|
break; |
715 |
|
|
case SIZE_WORD: |
716 |
|
|
regs[rloc] = (regs[rloc] & ~0xffff); |
717 |
|
|
break; |
718 |
|
|
case SIZE_LONG: |
719 |
|
|
regs[rloc] = 0; |
720 |
|
|
break; |
721 |
|
|
} |
722 |
|
|
} |
723 |
|
|
|
724 |
|
|
#if DEBUG |
725 |
gbeauche |
1.15 |
printf("%08x: %s %s access", regs[X86_REG_EIP], |
726 |
gbeauche |
1.10 |
transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long", |
727 |
gbeauche |
1.22 |
transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write"); |
728 |
gbeauche |
1.10 |
|
729 |
|
|
if (reg != -1) { |
730 |
|
|
static const char * x86_reg_str_map[8] = { |
731 |
|
|
"eax", "ecx", "edx", "ebx", |
732 |
|
|
"esp", "ebp", "esi", "edi" |
733 |
|
|
}; |
734 |
gbeauche |
1.22 |
printf(" %s register %%%s", transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from", x86_reg_str_map[reg]); |
735 |
gbeauche |
1.10 |
} |
736 |
|
|
printf(", %d bytes instruction\n", len); |
737 |
|
|
#endif |
738 |
|
|
|
739 |
|
|
regs[X86_REG_EIP] += len; |
740 |
gbeauche |
1.13 |
return true; |
741 |
|
|
} |
742 |
|
|
#endif |
743 |
gbeauche |
1.14 |
|
744 |
gbeauche |
1.13 |
// Decode and skip PPC instruction |
745 |
gbeauche |
1.14 |
#if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__)) |
746 |
|
|
static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs) |
747 |
gbeauche |
1.13 |
{ |
748 |
gbeauche |
1.14 |
instruction_t instr; |
749 |
|
|
powerpc_decode_instruction(&instr, *nip_p, regs); |
750 |
gbeauche |
1.13 |
|
751 |
gbeauche |
1.22 |
if (instr.transfer_type == SIGSEGV_TRANSFER_UNKNOWN) { |
752 |
gbeauche |
1.13 |
// Unknown machine code, let it crash. Then patch the decoder |
753 |
|
|
return false; |
754 |
|
|
} |
755 |
|
|
|
756 |
|
|
#if DEBUG |
757 |
gbeauche |
1.14 |
printf("%08x: %s %s access", *nip_p, |
758 |
|
|
instr.transfer_size == SIZE_BYTE ? "byte" : instr.transfer_size == SIZE_WORD ? "word" : "long", |
759 |
gbeauche |
1.22 |
instr.transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write"); |
760 |
gbeauche |
1.14 |
|
761 |
|
|
if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX) |
762 |
|
|
printf(" r%d (ra = %08x)\n", instr.ra, instr.addr); |
763 |
gbeauche |
1.22 |
if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD) |
764 |
gbeauche |
1.14 |
printf(" r%d (rd = 0)\n", instr.rd); |
765 |
|
|
#endif |
766 |
|
|
|
767 |
|
|
if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX) |
768 |
|
|
regs[instr.ra] = instr.addr; |
769 |
gbeauche |
1.22 |
if (instr.transfer_type == SIGSEGV_TRANSFER_LOAD) |
770 |
gbeauche |
1.14 |
regs[instr.rd] = 0; |
771 |
gbeauche |
1.13 |
|
772 |
gbeauche |
1.14 |
*nip_p += 4; |
773 |
gbeauche |
1.10 |
return true; |
774 |
|
|
} |
775 |
|
|
#endif |
776 |
|
|
#endif |
777 |
|
|
|
778 |
gbeauche |
1.1 |
// Fallbacks |
779 |
|
|
#ifndef SIGSEGV_FAULT_INSTRUCTION |
780 |
|
|
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC |
781 |
|
|
#endif |
782 |
|
|
|
783 |
gbeauche |
1.2 |
// SIGSEGV recovery supported ? |
784 |
|
|
#if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS) |
785 |
|
|
#define HAVE_SIGSEGV_RECOVERY |
786 |
|
|
#endif |
787 |
|
|
|
788 |
gbeauche |
1.1 |
|
789 |
|
|
/* |
790 |
|
|
* SIGSEGV global handler |
791 |
|
|
*/ |
792 |
|
|
|
793 |
gbeauche |
1.27 |
#if defined(HAVE_SIGSEGV_RECOVERY) || defined(HAVE_MACH_EXCEPTIONS) |
794 |
|
|
// This function handles the badaccess to memory. |
795 |
|
|
// It is called from the signal handler or the exception handler. |
796 |
|
|
static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST) |
797 |
gbeauche |
1.1 |
{ |
798 |
gbeauche |
1.10 |
sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS; |
799 |
|
|
sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION; |
800 |
|
|
|
801 |
gbeauche |
1.1 |
// Call user's handler and reinstall the global handler, if required |
802 |
gbeauche |
1.24 |
switch (sigsegv_fault_handler(fault_address, fault_instruction)) { |
803 |
|
|
case SIGSEGV_RETURN_SUCCESS: |
804 |
gbeauche |
1.27 |
return true; |
805 |
|
|
|
806 |
gbeauche |
1.10 |
#if HAVE_SIGSEGV_SKIP_INSTRUCTION |
807 |
gbeauche |
1.24 |
case SIGSEGV_RETURN_SKIP_INSTRUCTION: |
808 |
gbeauche |
1.27 |
// Call the instruction skipper with the register file |
809 |
|
|
// available |
810 |
|
|
if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE)) { |
811 |
|
|
#ifdef HAVE_MACH_EXCEPTIONS |
812 |
|
|
// Unlike UNIX signals where the thread state |
813 |
|
|
// is modified off of the stack, in Mach we |
814 |
|
|
// need to actually call thread_set_state to |
815 |
|
|
// have the register values updated. |
816 |
|
|
kern_return_t krc; |
817 |
|
|
|
818 |
|
|
krc = thread_set_state(thread, |
819 |
|
|
MACHINE_THREAD_STATE, (thread_state_t)state, |
820 |
|
|
MACHINE_THREAD_STATE_COUNT); |
821 |
|
|
MACH_CHECK_ERROR (thread_get_state, krc); |
822 |
|
|
#endif |
823 |
|
|
return true; |
824 |
|
|
} |
825 |
gbeauche |
1.24 |
break; |
826 |
|
|
#endif |
827 |
gbeauche |
1.10 |
} |
828 |
gbeauche |
1.27 |
|
829 |
|
|
// We can't do anything with the fault_address, dump state? |
830 |
|
|
if (sigsegv_state_dumper != 0) |
831 |
|
|
sigsegv_state_dumper(fault_address, fault_instruction); |
832 |
|
|
|
833 |
|
|
return false; |
834 |
|
|
} |
835 |
|
|
#endif |
836 |
|
|
|
837 |
|
|
|
838 |
|
|
/* |
839 |
|
|
* There are two mechanisms for handling a bad memory access, |
840 |
|
|
* Mach exceptions and UNIX signals. The implementation specific |
841 |
|
|
* code appears below. Its reponsibility is to call handle_badaccess |
842 |
|
|
* which is the routine that handles the fault in an implementation |
843 |
|
|
* agnostic manner. The implementation specific code below is then |
844 |
|
|
* reponsible for checking whether handle_badaccess was able |
845 |
|
|
* to handle the memory access error and perform any implementation |
846 |
|
|
* specific tasks necessary afterwards. |
847 |
|
|
*/ |
848 |
|
|
|
849 |
|
|
#ifdef HAVE_MACH_EXCEPTIONS |
850 |
|
|
/* |
851 |
|
|
* We need to forward all exceptions that we do not handle. |
852 |
|
|
* This is important, there are many exceptions that may be |
853 |
|
|
* handled by other exception handlers. For example debuggers |
854 |
|
|
* use exceptions and the exception hander is in another |
855 |
|
|
* process in such a case. (Timothy J. Wood states in his |
856 |
|
|
* message to the list that he based this code on that from |
857 |
|
|
* gdb for Darwin.) |
858 |
|
|
*/ |
859 |
|
|
static inline kern_return_t |
860 |
|
|
forward_exception(mach_port_t thread_port, |
861 |
|
|
mach_port_t task_port, |
862 |
|
|
exception_type_t exception_type, |
863 |
|
|
exception_data_t exception_data, |
864 |
|
|
mach_msg_type_number_t data_count, |
865 |
|
|
ExceptionPorts *oldExceptionPorts) |
866 |
|
|
{ |
867 |
|
|
kern_return_t kret; |
868 |
|
|
unsigned int portIndex; |
869 |
|
|
mach_port_t port; |
870 |
|
|
exception_behavior_t behavior; |
871 |
|
|
thread_state_flavor_t flavor; |
872 |
|
|
thread_state_t thread_state; |
873 |
|
|
mach_msg_type_number_t thread_state_count; |
874 |
|
|
|
875 |
|
|
for (portIndex = 0; portIndex < oldExceptionPorts->maskCount; portIndex++) { |
876 |
|
|
if (oldExceptionPorts->masks[portIndex] & (1 << exception_type)) { |
877 |
|
|
// This handler wants the exception |
878 |
|
|
break; |
879 |
|
|
} |
880 |
|
|
} |
881 |
|
|
|
882 |
|
|
if (portIndex >= oldExceptionPorts->maskCount) { |
883 |
|
|
fprintf(stderr, "No handler for exception_type = %d. Not fowarding\n", exception_type); |
884 |
|
|
return KERN_FAILURE; |
885 |
|
|
} |
886 |
|
|
|
887 |
|
|
port = oldExceptionPorts->handlers[portIndex]; |
888 |
|
|
behavior = oldExceptionPorts->behaviors[portIndex]; |
889 |
|
|
flavor = oldExceptionPorts->flavors[portIndex]; |
890 |
|
|
|
891 |
|
|
/* |
892 |
|
|
fprintf(stderr, "forwarding exception, port = 0x%x, behaviour = %d, flavor = %d\n", port, behavior, flavor); |
893 |
|
|
*/ |
894 |
|
|
|
895 |
|
|
if (behavior != EXCEPTION_DEFAULT) { |
896 |
|
|
thread_state_count = THREAD_STATE_MAX; |
897 |
|
|
kret = thread_get_state (thread_port, flavor, thread_state, |
898 |
|
|
&thread_state_count); |
899 |
|
|
MACH_CHECK_ERROR (thread_get_state, kret); |
900 |
|
|
} |
901 |
|
|
|
902 |
|
|
switch (behavior) { |
903 |
|
|
case EXCEPTION_DEFAULT: |
904 |
|
|
// fprintf(stderr, "forwarding to exception_raise\n"); |
905 |
|
|
kret = exception_raise(port, thread_port, task_port, exception_type, |
906 |
|
|
exception_data, data_count); |
907 |
|
|
MACH_CHECK_ERROR (exception_raise, kret); |
908 |
|
|
break; |
909 |
|
|
case EXCEPTION_STATE: |
910 |
|
|
// fprintf(stderr, "forwarding to exception_raise_state\n"); |
911 |
|
|
kret = exception_raise_state(port, exception_type, exception_data, |
912 |
|
|
data_count, &flavor, |
913 |
|
|
thread_state, thread_state_count, |
914 |
|
|
thread_state, &thread_state_count); |
915 |
|
|
MACH_CHECK_ERROR (exception_raise_state, kret); |
916 |
|
|
break; |
917 |
|
|
case EXCEPTION_STATE_IDENTITY: |
918 |
|
|
// fprintf(stderr, "forwarding to exception_raise_state_identity\n"); |
919 |
|
|
kret = exception_raise_state_identity(port, thread_port, task_port, |
920 |
|
|
exception_type, exception_data, |
921 |
|
|
data_count, &flavor, |
922 |
|
|
thread_state, thread_state_count, |
923 |
|
|
thread_state, &thread_state_count); |
924 |
|
|
MACH_CHECK_ERROR (exception_raise_state_identity, kret); |
925 |
|
|
break; |
926 |
|
|
default: |
927 |
|
|
fprintf(stderr, "forward_exception got unknown behavior\n"); |
928 |
|
|
break; |
929 |
|
|
} |
930 |
|
|
|
931 |
|
|
if (behavior != EXCEPTION_DEFAULT) { |
932 |
|
|
kret = thread_set_state (thread_port, flavor, thread_state, |
933 |
|
|
thread_state_count); |
934 |
|
|
MACH_CHECK_ERROR (thread_set_state, kret); |
935 |
|
|
} |
936 |
|
|
|
937 |
|
|
return KERN_SUCCESS; |
938 |
|
|
} |
939 |
|
|
|
940 |
|
|
/* |
941 |
|
|
* This is the code that actually handles the exception. |
942 |
|
|
* It is called by exc_server. For Darwin 5 Apple changed |
943 |
|
|
* this a bit from how this family of functions worked in |
944 |
|
|
* Mach. If you are familiar with that it is a little |
945 |
|
|
* different. The main variation that concerns us here is |
946 |
|
|
* that code is an array of exception specific codes and |
947 |
|
|
* codeCount is a count of the number of codes in the code |
948 |
|
|
* array. In typical Mach all exceptions have a code |
949 |
|
|
* and sub-code. It happens to be the case that for a |
950 |
|
|
* EXC_BAD_ACCESS exception the first entry is the type of |
951 |
|
|
* bad access that occurred and the second entry is the |
952 |
|
|
* faulting address so these entries correspond exactly to |
953 |
|
|
* how the code and sub-code are used on Mach. |
954 |
|
|
* |
955 |
|
|
* This is a MIG interface. No code in Basilisk II should |
956 |
|
|
* call this directley. This has to have external C |
957 |
|
|
* linkage because that is what exc_server expects. |
958 |
|
|
*/ |
959 |
|
|
kern_return_t |
960 |
|
|
catch_exception_raise(mach_port_t exception_port, |
961 |
|
|
mach_port_t thread, |
962 |
|
|
mach_port_t task, |
963 |
|
|
exception_type_t exception, |
964 |
|
|
exception_data_t code, |
965 |
|
|
mach_msg_type_number_t codeCount) |
966 |
|
|
{ |
967 |
|
|
ppc_thread_state_t state; |
968 |
|
|
kern_return_t krc; |
969 |
|
|
|
970 |
|
|
if ((exception == EXC_BAD_ACCESS) && (codeCount >= 2)) { |
971 |
|
|
if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS)) |
972 |
|
|
return KERN_SUCCESS; |
973 |
|
|
} |
974 |
|
|
|
975 |
|
|
// In Mach we do not need to remove the exception handler. |
976 |
|
|
// If we forward the exception, eventually some exception handler |
977 |
|
|
// will take care of this exception. |
978 |
|
|
krc = forward_exception(thread, task, exception, code, codeCount, &ports); |
979 |
|
|
|
980 |
|
|
return krc; |
981 |
|
|
} |
982 |
|
|
#endif |
983 |
|
|
|
984 |
|
|
#ifdef HAVE_SIGSEGV_RECOVERY |
985 |
|
|
// Handle bad memory accesses with signal handler |
986 |
|
|
static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST) |
987 |
|
|
{ |
988 |
|
|
// Call handler and reinstall the global handler, if required |
989 |
|
|
if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS)) { |
990 |
|
|
#if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL)) |
991 |
|
|
sigsegv_do_install_handler(sig); |
992 |
|
|
#endif |
993 |
|
|
return; |
994 |
|
|
} |
995 |
gbeauche |
1.10 |
|
996 |
gbeauche |
1.27 |
// Failure: reinstall default handler for "safe" crash |
997 |
gbeauche |
1.1 |
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
998 |
gbeauche |
1.27 |
SIGSEGV_ALL_SIGNALS |
999 |
gbeauche |
1.1 |
#undef FAULT_HANDLER |
1000 |
|
|
} |
1001 |
gbeauche |
1.2 |
#endif |
1002 |
gbeauche |
1.1 |
|
1003 |
|
|
|
1004 |
|
|
/* |
1005 |
|
|
* SIGSEGV handler initialization |
1006 |
|
|
*/ |
1007 |
|
|
|
1008 |
|
|
#if defined(HAVE_SIGINFO_T) |
1009 |
|
|
static bool sigsegv_do_install_handler(int sig) |
1010 |
|
|
{ |
1011 |
|
|
// Setup SIGSEGV handler to process writes to frame buffer |
1012 |
|
|
#ifdef HAVE_SIGACTION |
1013 |
gbeauche |
1.22 |
struct sigaction sigsegv_sa; |
1014 |
|
|
sigemptyset(&sigsegv_sa.sa_mask); |
1015 |
|
|
sigsegv_sa.sa_sigaction = sigsegv_handler; |
1016 |
|
|
sigsegv_sa.sa_flags = SA_SIGINFO; |
1017 |
|
|
return (sigaction(sig, &sigsegv_sa, 0) == 0); |
1018 |
gbeauche |
1.1 |
#else |
1019 |
|
|
return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR); |
1020 |
|
|
#endif |
1021 |
|
|
} |
1022 |
gbeauche |
1.2 |
#endif |
1023 |
|
|
|
1024 |
|
|
#if defined(HAVE_SIGCONTEXT_SUBTERFUGE) |
1025 |
gbeauche |
1.1 |
static bool sigsegv_do_install_handler(int sig) |
1026 |
|
|
{ |
1027 |
|
|
// Setup SIGSEGV handler to process writes to frame buffer |
1028 |
|
|
#ifdef HAVE_SIGACTION |
1029 |
gbeauche |
1.22 |
struct sigaction sigsegv_sa; |
1030 |
|
|
sigemptyset(&sigsegv_sa.sa_mask); |
1031 |
|
|
sigsegv_sa.sa_handler = (signal_handler)sigsegv_handler; |
1032 |
|
|
sigsegv_sa.sa_flags = 0; |
1033 |
gbeauche |
1.1 |
#if !EMULATED_68K && defined(__NetBSD__) |
1034 |
gbeauche |
1.22 |
sigaddset(&sigsegv_sa.sa_mask, SIGALRM); |
1035 |
|
|
sigsegv_sa.sa_flags |= SA_ONSTACK; |
1036 |
gbeauche |
1.1 |
#endif |
1037 |
gbeauche |
1.22 |
return (sigaction(sig, &sigsegv_sa, 0) == 0); |
1038 |
gbeauche |
1.1 |
#else |
1039 |
|
|
return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR); |
1040 |
|
|
#endif |
1041 |
|
|
} |
1042 |
|
|
#endif |
1043 |
|
|
|
1044 |
gbeauche |
1.27 |
#if defined(HAVE_MACH_EXCEPTIONS) |
1045 |
|
|
static bool sigsegv_do_install_handler(sigsegv_fault_handler_t handler) |
1046 |
|
|
{ |
1047 |
|
|
/* |
1048 |
|
|
* Except for the exception port functions, this should be |
1049 |
|
|
* pretty much stock Mach. If later you choose to support |
1050 |
|
|
* other Mach's besides Darwin, just check for __MACH__ |
1051 |
|
|
* here and __APPLE__ where the actual differences are. |
1052 |
|
|
*/ |
1053 |
|
|
#if defined(__APPLE__) && defined(__MACH__) |
1054 |
|
|
if (sigsegv_fault_handler != NULL) { |
1055 |
|
|
sigsegv_fault_handler = handler; |
1056 |
|
|
return true; |
1057 |
|
|
} |
1058 |
|
|
|
1059 |
|
|
kern_return_t krc; |
1060 |
|
|
|
1061 |
|
|
// create the the exception port |
1062 |
|
|
krc = mach_port_allocate(mach_task_self(), |
1063 |
|
|
MACH_PORT_RIGHT_RECEIVE, &_exceptionPort); |
1064 |
|
|
if (krc != KERN_SUCCESS) { |
1065 |
|
|
mach_error("mach_port_allocate", krc); |
1066 |
|
|
return false; |
1067 |
|
|
} |
1068 |
|
|
|
1069 |
|
|
// add a port send right |
1070 |
|
|
krc = mach_port_insert_right(mach_task_self(), |
1071 |
|
|
_exceptionPort, _exceptionPort, |
1072 |
|
|
MACH_MSG_TYPE_MAKE_SEND); |
1073 |
|
|
if (krc != KERN_SUCCESS) { |
1074 |
|
|
mach_error("mach_port_insert_right", krc); |
1075 |
|
|
return false; |
1076 |
|
|
} |
1077 |
|
|
|
1078 |
|
|
// get the old exception ports |
1079 |
|
|
ports.maskCount = sizeof (ports.masks) / sizeof (ports.masks[0]); |
1080 |
|
|
krc = thread_get_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, ports.masks, |
1081 |
|
|
&ports.maskCount, ports.handlers, ports.behaviors, ports.flavors); |
1082 |
|
|
if (krc != KERN_SUCCESS) { |
1083 |
|
|
mach_error("thread_get_exception_ports", krc); |
1084 |
|
|
return false; |
1085 |
|
|
} |
1086 |
|
|
|
1087 |
|
|
// set the new exception port |
1088 |
|
|
// |
1089 |
|
|
// We could have used EXCEPTION_STATE_IDENTITY instead of |
1090 |
|
|
// EXCEPTION_DEFAULT to get the thread state in the initial |
1091 |
|
|
// message, but it turns out that in the common case this is not |
1092 |
|
|
// neccessary. If we need it we can later ask for it from the |
1093 |
|
|
// suspended thread. |
1094 |
|
|
// |
1095 |
|
|
// Even with THREAD_STATE_NONE, Darwin provides the program |
1096 |
|
|
// counter in the thread state. The comments in the header file |
1097 |
|
|
// seem to imply that you can count on the GPR's on an exception |
1098 |
|
|
// as well but just to be safe I use MACHINE_THREAD_STATE because |
1099 |
|
|
// you have to ask for all of the GPR's anyway just to get the |
1100 |
|
|
// program counter. In any case because of update effective |
1101 |
|
|
// address from immediate and update address from effective |
1102 |
|
|
// addresses of ra and rb modes (as good an name as any for these |
1103 |
|
|
// addressing modes) used in PPC instructions, you will need the |
1104 |
|
|
// GPR state anyway. |
1105 |
|
|
krc = thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, _exceptionPort, |
1106 |
|
|
EXCEPTION_DEFAULT, MACHINE_THREAD_STATE); |
1107 |
|
|
if (krc != KERN_SUCCESS) { |
1108 |
|
|
mach_error("thread_set_exception_ports", krc); |
1109 |
|
|
return false; |
1110 |
|
|
} |
1111 |
|
|
|
1112 |
|
|
// create the exception handler thread |
1113 |
|
|
if (pthread_create(&exc_thread, NULL, &handleExceptions, NULL) != 0) { |
1114 |
|
|
(void)fprintf(stderr, "creation of exception thread failed\n"); |
1115 |
|
|
return false; |
1116 |
|
|
} |
1117 |
|
|
|
1118 |
|
|
// do not care about the exception thread any longer, let is run standalone |
1119 |
|
|
(void)pthread_detach(exc_thread); |
1120 |
|
|
|
1121 |
|
|
sigsegv_fault_handler = handler; |
1122 |
|
|
return true; |
1123 |
|
|
#else |
1124 |
|
|
return false; |
1125 |
|
|
#endif |
1126 |
|
|
} |
1127 |
|
|
#endif |
1128 |
|
|
|
1129 |
gbeauche |
1.12 |
bool sigsegv_install_handler(sigsegv_fault_handler_t handler) |
1130 |
gbeauche |
1.1 |
{ |
1131 |
gbeauche |
1.27 |
#if defined(HAVE_SIGSEGV_RECOVERY) |
1132 |
gbeauche |
1.1 |
bool success = true; |
1133 |
|
|
#define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig); |
1134 |
|
|
SIGSEGV_ALL_SIGNALS |
1135 |
|
|
#undef FAULT_HANDLER |
1136 |
gbeauche |
1.27 |
if (success) |
1137 |
|
|
sigsegv_fault_handler = handler; |
1138 |
gbeauche |
1.1 |
return success; |
1139 |
gbeauche |
1.27 |
#elif defined(HAVE_MACH_EXCEPTIONS) |
1140 |
|
|
return sigsegv_do_install_handler(handler); |
1141 |
gbeauche |
1.1 |
#else |
1142 |
|
|
// FAIL: no siginfo_t nor sigcontext subterfuge is available |
1143 |
|
|
return false; |
1144 |
|
|
#endif |
1145 |
|
|
} |
1146 |
|
|
|
1147 |
|
|
|
1148 |
|
|
/* |
1149 |
|
|
* SIGSEGV handler deinitialization |
1150 |
|
|
*/ |
1151 |
|
|
|
1152 |
|
|
void sigsegv_deinstall_handler(void) |
1153 |
|
|
{ |
1154 |
gbeauche |
1.27 |
// We do nothing for Mach exceptions, the thread would need to be |
1155 |
|
|
// suspended if not already so, and we might mess with other |
1156 |
|
|
// exception handlers that came after we registered ours. There is |
1157 |
|
|
// no need to remove the exception handler, in fact this function is |
1158 |
|
|
// not called anywhere in Basilisk II. |
1159 |
gbeauche |
1.2 |
#ifdef HAVE_SIGSEGV_RECOVERY |
1160 |
gbeauche |
1.12 |
sigsegv_fault_handler = 0; |
1161 |
gbeauche |
1.1 |
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL); |
1162 |
|
|
SIGSEGV_ALL_SIGNALS |
1163 |
|
|
#undef FAULT_HANDLER |
1164 |
gbeauche |
1.2 |
#endif |
1165 |
gbeauche |
1.1 |
} |
1166 |
|
|
|
1167 |
gbeauche |
1.10 |
|
1168 |
|
|
/* |
1169 |
|
|
* Set callback function when we cannot handle the fault |
1170 |
|
|
*/ |
1171 |
|
|
|
1172 |
gbeauche |
1.12 |
void sigsegv_set_dump_state(sigsegv_state_dumper_t handler) |
1173 |
gbeauche |
1.10 |
{ |
1174 |
gbeauche |
1.12 |
sigsegv_state_dumper = handler; |
1175 |
gbeauche |
1.10 |
} |
1176 |
|
|
|
1177 |
|
|
|
1178 |
gbeauche |
1.1 |
/* |
1179 |
|
|
* Test program used for configure/test |
1180 |
|
|
*/ |
1181 |
|
|
|
1182 |
gbeauche |
1.4 |
#ifdef CONFIGURE_TEST_SIGSEGV_RECOVERY |
1183 |
gbeauche |
1.1 |
#include <stdio.h> |
1184 |
|
|
#include <stdlib.h> |
1185 |
|
|
#include <fcntl.h> |
1186 |
|
|
#include <sys/mman.h> |
1187 |
gbeauche |
1.4 |
#include "vm_alloc.h" |
1188 |
gbeauche |
1.1 |
|
1189 |
|
|
static int page_size; |
1190 |
gbeauche |
1.3 |
static volatile char * page = 0; |
1191 |
|
|
static volatile int handler_called = 0; |
1192 |
gbeauche |
1.1 |
|
1193 |
gbeauche |
1.24 |
static sigsegv_return_t sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
1194 |
gbeauche |
1.1 |
{ |
1195 |
|
|
handler_called++; |
1196 |
|
|
if ((fault_address - 123) != page) |
1197 |
gbeauche |
1.29 |
exit(10); |
1198 |
gbeauche |
1.4 |
if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0) |
1199 |
gbeauche |
1.29 |
exit(11); |
1200 |
gbeauche |
1.24 |
return SIGSEGV_RETURN_SUCCESS; |
1201 |
gbeauche |
1.1 |
} |
1202 |
|
|
|
1203 |
gbeauche |
1.10 |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
1204 |
gbeauche |
1.28 |
#ifdef __GNUC__ |
1205 |
|
|
// Code range where we expect the fault to come from |
1206 |
|
|
static void *b_region, *e_region; |
1207 |
|
|
#endif |
1208 |
|
|
|
1209 |
gbeauche |
1.24 |
static sigsegv_return_t sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
1210 |
gbeauche |
1.10 |
{ |
1211 |
gbeauche |
1.28 |
if (((unsigned long)fault_address - (unsigned long)page) < page_size) { |
1212 |
|
|
#ifdef __GNUC__ |
1213 |
|
|
// Make sure reported fault instruction address falls into |
1214 |
|
|
// expected code range |
1215 |
|
|
if (instruction_address != SIGSEGV_INVALID_PC |
1216 |
|
|
&& ((instruction_address < (sigsegv_address_t)b_region) || |
1217 |
|
|
(instruction_address >= (sigsegv_address_t)e_region))) |
1218 |
|
|
return SIGSEGV_RETURN_FAILURE; |
1219 |
|
|
#endif |
1220 |
gbeauche |
1.26 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
1221 |
gbeauche |
1.28 |
} |
1222 |
|
|
|
1223 |
gbeauche |
1.24 |
return SIGSEGV_RETURN_FAILURE; |
1224 |
gbeauche |
1.10 |
} |
1225 |
|
|
#endif |
1226 |
|
|
|
1227 |
gbeauche |
1.1 |
int main(void) |
1228 |
|
|
{ |
1229 |
gbeauche |
1.4 |
if (vm_init() < 0) |
1230 |
gbeauche |
1.1 |
return 1; |
1231 |
|
|
|
1232 |
|
|
page_size = getpagesize(); |
1233 |
gbeauche |
1.4 |
if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED) |
1234 |
gbeauche |
1.29 |
return 2; |
1235 |
gbeauche |
1.4 |
|
1236 |
|
|
if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0) |
1237 |
gbeauche |
1.29 |
return 3; |
1238 |
gbeauche |
1.1 |
|
1239 |
|
|
if (!sigsegv_install_handler(sigsegv_test_handler)) |
1240 |
gbeauche |
1.29 |
return 4; |
1241 |
gbeauche |
1.1 |
|
1242 |
|
|
page[123] = 45; |
1243 |
|
|
page[123] = 45; |
1244 |
|
|
|
1245 |
|
|
if (handler_called != 1) |
1246 |
gbeauche |
1.29 |
return 5; |
1247 |
gbeauche |
1.10 |
|
1248 |
|
|
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
1249 |
|
|
if (!sigsegv_install_handler(sigsegv_insn_handler)) |
1250 |
gbeauche |
1.29 |
return 6; |
1251 |
gbeauche |
1.10 |
|
1252 |
gbeauche |
1.17 |
if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0) |
1253 |
gbeauche |
1.29 |
return 7; |
1254 |
gbeauche |
1.10 |
|
1255 |
|
|
for (int i = 0; i < page_size; i++) |
1256 |
|
|
page[i] = (i + 1) % page_size; |
1257 |
|
|
|
1258 |
|
|
if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0) |
1259 |
gbeauche |
1.29 |
return 8; |
1260 |
gbeauche |
1.10 |
|
1261 |
|
|
#define TEST_SKIP_INSTRUCTION(TYPE) do { \ |
1262 |
|
|
const unsigned int TAG = 0x12345678; \ |
1263 |
|
|
TYPE data = *((TYPE *)(page + sizeof(TYPE))); \ |
1264 |
|
|
volatile unsigned int effect = data + TAG; \ |
1265 |
|
|
if (effect != TAG) \ |
1266 |
gbeauche |
1.29 |
return 9; \ |
1267 |
gbeauche |
1.10 |
} while (0) |
1268 |
|
|
|
1269 |
gbeauche |
1.28 |
#ifdef __GNUC__ |
1270 |
|
|
b_region = &&L_b_region; |
1271 |
|
|
e_region = &&L_e_region; |
1272 |
|
|
#endif |
1273 |
|
|
L_b_region: |
1274 |
gbeauche |
1.10 |
TEST_SKIP_INSTRUCTION(unsigned char); |
1275 |
|
|
TEST_SKIP_INSTRUCTION(unsigned short); |
1276 |
|
|
TEST_SKIP_INSTRUCTION(unsigned int); |
1277 |
gbeauche |
1.28 |
L_e_region: |
1278 |
gbeauche |
1.10 |
#endif |
1279 |
gbeauche |
1.1 |
|
1280 |
gbeauche |
1.4 |
vm_exit(); |
1281 |
gbeauche |
1.1 |
return 0; |
1282 |
|
|
} |
1283 |
|
|
#endif |