1 /**************************************************************************/
2 /* */
3 /* OCaml */
4 /* */
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
6 /* */
7 /* Copyright 2004 Institut National de Recherche en Informatique et */
8 /* en Automatique. */
9 /* */
10 /* All rights reserved. This file is distributed under the terms of */
11 /* the GNU Lesser General Public License version 2.1, with the */
12 /* special exception on linking described in the file LICENSE. */
13 /* */
14 /**************************************************************************/
15
16 /* Processor- and OS-dependent signal interface */
17
18 /****************** AMD64, Linux */
19
20 #if defined(TARGET_amd64) && defined (SYS_linux)
21
22 #define DECLARE_SIGNAL_HANDLER(name) \
23 static void name(int sig, siginfo_t * info, ucontext_t * context)
24
25 #define SET_SIGACT(sigact,name) \
26 sigact.sa_sigaction = (void (*)(int,siginfo_t *,void *)) (name); \
27 sigact.sa_flags = SA_SIGINFO
28
29 typedef greg_t context_reg;
30 #define CONTEXT_C_ARG_1 (context->uc_mcontext.gregs[REG_RDI])
31 #define CONTEXT_PC (context->uc_mcontext.gregs[REG_RIP])
32 #define CONTEXT_SP (context->uc_mcontext.gregs[REG_RSP])
33 #define CONTEXT_YOUNG_PTR (context->uc_mcontext.gregs[REG_R15])
34 #define CONTEXT_FAULTING_ADDRESS ((char *)context->uc_mcontext.gregs[REG_CR2])
35
36 /****************** AMD64, MacOSX */
37
38 #elif defined(TARGET_amd64) && defined (SYS_macosx)
39
40 #define DECLARE_SIGNAL_HANDLER(name) \
41 static void name(int sig, siginfo_t * info, void * context)
42
43 #define SET_SIGACT(sigact,name) \
44 sigact.sa_sigaction = (name); \
45 sigact.sa_flags = SA_SIGINFO | SA_64REGSET
46
47 #include <sys/ucontext.h>
48 #include <AvailabilityMacros.h>
49
50 #if !defined(MAC_OS_X_VERSION_10_5) \
51 || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
52 #define CONTEXT_REG(r) r
53 #else
54 #define CONTEXT_REG(r) __##r
55 #endif
56
57 typedef unsigned long long context_reg;
58 #define CONTEXT_STATE (((ucontext_t *)context)->uc_mcontext->CONTEXT_REG(ss))
59 #define CONTEXT_C_ARG_1 (CONTEXT_STATE.CONTEXT_REG(rdi))
60 #define CONTEXT_PC (CONTEXT_STATE.CONTEXT_REG(rip))
61 #define CONTEXT_YOUNG_PTR (CONTEXT_STATE.CONTEXT_REG(r15))
62 #define CONTEXT_SP (CONTEXT_STATE.CONTEXT_REG(rsp))
63 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
64
65 #define RETURN_AFTER_STACK_OVERFLOW
66
67 /****************** ARM, Linux */
68
69 #elif defined(TARGET_arm) && (defined(SYS_linux_eabi) \
70 || defined(SYS_linux_eabihf))
71
72 #include <sys/ucontext.h>
73
74 #define DECLARE_SIGNAL_HANDLER(name) \
75 static void name(int sig, siginfo_t * info, ucontext_t * context)
76
77 #define SET_SIGACT(sigact,name) \
78 sigact.sa_sigaction = (void (*)(int,siginfo_t *,void *)) (name); \
79 sigact.sa_flags = SA_SIGINFO
80
81 typedef unsigned long context_reg;
82 #define CONTEXT_PC (context->uc_mcontext.arm_pc)
83 #define CONTEXT_SP (context->uc_mcontext.arm_sp)
84 #define CONTEXT_EXCEPTION_POINTER (context->uc_mcontext.arm_fp)
85 #define CONTEXT_YOUNG_PTR (context->uc_mcontext.arm_r8)
86 #define CONTEXT_FAULTING_ADDRESS ((char *) context->uc_mcontext.fault_address)
87
88 /****************** ARM64, Linux */
89
90 #elif defined(TARGET_arm64) && defined(SYS_linux)
91
92 #include <sys/ucontext.h>
93
94 #define DECLARE_SIGNAL_HANDLER(name) \
95 static void name(int sig, siginfo_t * info, ucontext_t * context)
96
97 #define SET_SIGACT(sigact,name) \
98 sigact.sa_sigaction = (void (*)(int,siginfo_t *,void *)) (name); \
99 sigact.sa_flags = SA_SIGINFO
100
101 typedef unsigned long context_reg;
102 #define CONTEXT_PC (context->uc_mcontext.pc)
103 #define CONTEXT_SP (context->uc_mcontext.sp)
104 #define CONTEXT_EXCEPTION_POINTER (context->uc_mcontext.regs[26])
105 #define CONTEXT_YOUNG_PTR (context->uc_mcontext.regs[27])
106 #define CONTEXT_FAULTING_ADDRESS ((char *) context->uc_mcontext.fault_address)
107
108 /****************** ARM64, FreeBSD */
109
110 #elif defined(TARGET_arm64) && defined(SYS_freebsd)
111
112 #include <sys/ucontext.h>
113
114 #define DECLARE_SIGNAL_HANDLER(name) \
115 static void name(int sig, siginfo_t * info, ucontext_t * context)
116
117 #define SET_SIGACT(sigact,name) \
118 sigact.sa_sigaction = (void (*)(int,siginfo_t *,void *)) (name); \
119 sigact.sa_flags = SA_SIGINFO
120
121 typedef unsigned long context_reg;
122 #define CONTEXT_PC (context->uc_mcontext.mc_gpregs.gp_elr)
123 #define CONTEXT_SP (context->uc_mcontext.mc_gpregs.gp_sp)
124 #define CONTEXT_EXCEPTION_POINTER (context->uc_mcontext.mc_gpregs.gp_x[26])
125 #define CONTEXT_YOUNG_PTR (context->uc_mcontext.mc_gpregs.gp_x[27])
126 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
127
128
129 /****************** AMD64, Solaris x86 */
130
131 #elif defined(TARGET_amd64) && defined (SYS_solaris)
132
133 #include <ucontext.h>
134
135 #define DECLARE_SIGNAL_HANDLER(name) \
136 static void name(int sig, siginfo_t * info, ucontext_t * context)
137
138 #define SET_SIGACT(sigact,name) \
139 sigact.sa_sigaction = (void (*)(int,siginfo_t *,void *)) (name); \
140 sigact.sa_flags = SA_SIGINFO
141
142 typedef greg_t context_reg;
143 #define CONTEXT_PC (context->uc_mcontext.gregs[REG_RIP])
144 #define CONTEXT_C_ARG_1 (context->uc_mcontext.gregs[REG_RDI])
145 #define CONTEXT_SP (context->uc_mcontext.gregs[REG_RSP])
146 #define CONTEXT_YOUNG_PTR (context->uc_mcontext.gregs[REG_R15])
147 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
148
149 /****************** AMD64, OpenBSD */
150
151 #elif defined(TARGET_amd64) && defined (SYS_openbsd)
152
153 #define DECLARE_SIGNAL_HANDLER(name) \
154 static void name(int sig, siginfo_t * info, struct sigcontext * context)
155
156 #define SET_SIGACT(sigact,name) \
157 sigact.sa_sigaction = (void (*)(int,siginfo_t *,void *)) (name); \
158 sigact.sa_flags = SA_SIGINFO
159
160 #define CONTEXT_PC (context->sc_rip)
161 #define CONTEXT_C_ARG_1 (context->sc_rdi)
162 #define CONTEXT_SP (context->sc_rsp)
163 #define CONTEXT_YOUNG_PTR (context->sc_r15)
164 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
165
166 /****************** AMD64, NetBSD */
167
168 #elif defined(TARGET_amd64) && defined (SYS_netbsd)
169
170 #include <ucontext.h>
171 #define DECLARE_SIGNAL_HANDLER(name) \
172 static void name(int sig, siginfo_t * info, ucontext_t * context)
173
174 #define SET_SIGACT(sigact,name) \
175 sigact.sa_sigaction = (void (*)(int,siginfo_t *,void *)) (name); \
176 sigact.sa_flags = SA_SIGINFO
177
178 #define CONTEXT_PC (_UC_MACHINE_PC(context))
179 #define CONTEXT_C_ARG_1 (context->uc_mcontext.gregs[REG_RDI])
180 #define CONTEXT_SP (_UC_MACHINE_SP(context))
181 #define CONTEXT_YOUNG_PTR (context->uc_mcontext.gregs[REG_R15])
182 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
183
184 /****************** I386, Linux */
185
186 #elif defined(TARGET_i386) && defined(SYS_linux_elf)
187
188 #define DECLARE_SIGNAL_HANDLER(name) \
189 static void name(int sig, struct sigcontext context)
190
191 #define SET_SIGACT(sigact,name) \
192 sigact.sa_handler = (void (*)(int)) (name); \
193 sigact.sa_flags = 0
194
195 #define CONTEXT_FAULTING_ADDRESS ((char *) context.cr2)
196 #define CONTEXT_PC (context.eip)
197 #define CONTEXT_SP (context.esp)
198
199 /****************** I386, BSD_ELF */
200
201 #elif defined(TARGET_i386) && defined(SYS_bsd_elf)
202
203 #if defined (__NetBSD__)
204 #include <ucontext.h>
205 #define DECLARE_SIGNAL_HANDLER(name) \
206 static void name(int sig, siginfo_t * info, ucontext_t * context)
207 #else
208 #define DECLARE_SIGNAL_HANDLER(name) \
209 static void name(int sig, siginfo_t * info, struct sigcontext * context)
210 #endif
211
212 #define SET_SIGACT(sigact,name) \
213 sigact.sa_sigaction = (void (*)(int,siginfo_t *,void *)) (name); \
214 sigact.sa_flags = SA_SIGINFO
215
216 #if defined (__NetBSD__)
217 #define CONTEXT_PC (_UC_MACHINE_PC(context))
218 #define CONTEXT_SP (_UC_MACHINE_SP(context))
219 #else
220 #define CONTEXT_PC (context->sc_eip)
221 #define CONTEXT_SP (context->sc_esp)
222 #endif
223 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
224
225 /****************** I386, BSD */
226
227 #elif defined(TARGET_i386) && defined(SYS_bsd)
228
229 #define DECLARE_SIGNAL_HANDLER(name) \
230 static void name(int sig, siginfo_t * info, void * context)
231
232 #define SET_SIGACT(sigact,name) \
233 sigact.sa_sigaction = (name); \
234 sigact.sa_flags = SA_SIGINFO
235
236 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
237
238 /****************** I386, MacOS X */
239
240 #elif defined(TARGET_i386) && defined(SYS_macosx)
241
242 #define DECLARE_SIGNAL_HANDLER(name) \
243 static void name(int sig, siginfo_t * info, void * context)
244
245 #define SET_SIGACT(sigact,name) \
246 sigact.sa_sigaction = (name); \
247 sigact.sa_flags = SA_SIGINFO
248
249 #include <sys/ucontext.h>
250 #include <AvailabilityMacros.h>
251
252 #if !defined(MAC_OS_X_VERSION_10_5) \
253 || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
254 #define CONTEXT_REG(r) r
255 #else
256 #define CONTEXT_REG(r) __##r
257 #endif
258
259 #define CONTEXT_STATE (((ucontext_t *)context)->uc_mcontext->CONTEXT_REG(ss))
260 #define CONTEXT_PC (CONTEXT_STATE.CONTEXT_REG(eip))
261 #define CONTEXT_SP (CONTEXT_STATE.CONTEXT_REG(esp))
262 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
263
264 /****************** I386, Solaris x86 */
265
266 #elif defined(TARGET_i386) && defined(SYS_solaris)
267
268 #define DECLARE_SIGNAL_HANDLER(name) \
269 static void name(int sig, siginfo_t * info, void * context)
270
271 #define SET_SIGACT(sigact,name) \
272 sigact.sa_sigaction = (name); \
273 sigact.sa_flags = SA_SIGINFO
274
275 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
276
277 /****************** PowerPC, MacOS X */
278
279 #elif defined(TARGET_power) && defined(SYS_rhapsody)
280
281 #define DECLARE_SIGNAL_HANDLER(name) \
282 static void name(int sig, siginfo_t * info, void * context)
283
284 #include <sys/ucontext.h>
285 #include <AvailabilityMacros.h>
286
287 #ifdef __LP64__
288 #define SET_SIGACT(sigact,name) \
289 sigact.sa_sigaction = (name); \
290 sigact.sa_flags = SA_SIGINFO | SA_64REGSET
291
292 typedef unsigned long long context_reg;
293
294 #define CONTEXT_MCONTEXT (((ucontext64_t *)context)->uc_mcontext64)
295 #else
296 #define SET_SIGACT(sigact,name) \
297 sigact.sa_sigaction = (name); \
298 sigact.sa_flags = SA_SIGINFO
299
300 typedef unsigned long context_reg;
301
302 #define CONTEXT_MCONTEXT (((ucontext_t *)context)->uc_mcontext)
303 #endif
304
305 #if !defined(MAC_OS_X_VERSION_10_5) \
306 || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
307 #define CONTEXT_REG(r) r
308 #else
309 #define CONTEXT_REG(r) __##r
310 #endif
311
312 #define CONTEXT_STATE (CONTEXT_MCONTEXT->CONTEXT_REG(ss))
313 #define CONTEXT_PC (CONTEXT_STATE.CONTEXT_REG(srr0))
314 #define CONTEXT_EXCEPTION_POINTER (CONTEXT_STATE.CONTEXT_REG(r29))
315 #define CONTEXT_YOUNG_LIMIT (CONTEXT_STATE.CONTEXT_REG(r30))
316 #define CONTEXT_YOUNG_PTR (CONTEXT_STATE.CONTEXT_REG(r31))
317 #define CONTEXT_SP (CONTEXT_STATE.CONTEXT_REG(r1))
318 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
319
320 /****************** PowerPC, ELF (Linux) */
321
322 #elif defined(TARGET_power) && defined(SYS_elf)
323
324 #define DECLARE_SIGNAL_HANDLER(name) \
325 static void name(int sig, struct sigcontext * context)
326
327 #define SET_SIGACT(sigact,name) \
328 sigact.sa_handler = (void (*)(int)) (name); \
329 sigact.sa_flags = 0
330
331 typedef unsigned long context_reg;
332 #define CONTEXT_PC (context->regs->nip)
333 #define CONTEXT_EXCEPTION_POINTER (context->regs->gpr[29])
334 #define CONTEXT_YOUNG_LIMIT (context->regs->gpr[30])
335 #define CONTEXT_YOUNG_PTR (context->regs->gpr[31])
336 #define CONTEXT_SP (context->regs->gpr[1])
337
338 /****************** PowerPC, NetBSD */
339
340 #elif defined(TARGET_power) && defined (SYS_netbsd)
341
342 #include <ucontext.h>
343 #define DECLARE_SIGNAL_HANDLER(name) \
344 static void name(int sig, siginfo_t * info, ucontext_t * context)
345
346 #define SET_SIGACT(sigact,name) \
347 sigact.sa_sigaction = (void (*)(int,siginfo_t *,void *)) (name); \
348 sigact.sa_flags = SA_SIGINFO
349
350 typedef long context_reg;
351 #define CONTEXT_PC (_UC_MACHINE_PC(context))
352 #define CONTEXT_EXCEPTION_POINTER (context->uc_mcontext.__gregs[_REG_R29])
353 #define CONTEXT_YOUNG_LIMIT (context->uc_mcontext.__gregs[_REG_R30])
354 #define CONTEXT_YOUNG_PTR (context->uc_mcontext.__gregs[_REG_R31])
355 #define CONTEXT_SP (_UC_MACHINE_SP(context))
356 #define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)
357
358
359 /****************** PowerPC, other BSDs */
360
361 #elif defined(TARGET_power) && \
362 (defined(SYS_bsd) || defined(SYS_bsd_elf))
363
364 #define DECLARE_SIGNAL_HANDLER(name) \
365 static void name(int sig, int code, struct sigcontext * context)
366
367 #define SET_SIGACT(sigact,name) \
368 sigact.sa_handler = (void (*)(int)) (name); \
369 sigact.sa_flags = 0
370
371 typedef unsigned long context_reg;
372 #define CONTEXT_PC (context->sc_frame.srr0)
373 #define CONTEXT_EXCEPTION_POINTER (context->sc_frame.fixreg[29])
374 #define CONTEXT_YOUNG_LIMIT (context->sc_frame.fixreg[30])
375 #define CONTEXT_YOUNG_PTR (context->sc_frame.fixreg[31])
376 #define CONTEXT_SP (context->sc_frame.fixreg[1])
377
378 /****************** s390x, ELF (Linux) */
379 #elif defined(TARGET_s390x) && defined(SYS_elf)
380
381 #define DECLARE_SIGNAL_HANDLER(name) \
382 static void name(int sig, struct sigcontext * context)
383
384 #define SET_SIGACT(sigact,name) \
385 sigact.sa_handler = (void (*)(int)) (name); \
386 sigact.sa_flags = 0
387
388 typedef unsigned long context_reg;
389 #define CONTEXT_PC (context->sregs->regs.psw.addr)
390 #define CONTEXT_EXCEPTION_POINTER (context->sregs->regs.gprs[13])
391 #define CONTEXT_YOUNG_LIMIT (context->sregs->regs.gprs[10])
392 #define CONTEXT_YOUNG_PTR (context->sregs->regs.gprs[11])
393 #define CONTEXT_SP (context->sregs->regs.gprs[15])
394
395 /******************** Default */
396
397 #else
398
399 #define DECLARE_SIGNAL_HANDLER(name) \
400 static void name(int sig)
401
402 #define SET_SIGACT(sigact,name) \
403 sigact.sa_handler = (name); \
404 sigact.sa_flags = 0
405
406 #endif
407