Initial set of Ward sgen annotations (#5705)
[mono.git] / mono / utils / mono-signal-handler.h
1 /**
2  * \file
3  * Handle signal handler differences across platforms
4  *
5  * Copyright (C) 2013 Xamarin Inc
6  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7  */
8
9 #ifndef __MONO_SIGNAL_HANDLER_H__
10 #define __MONO_SIGNAL_HANDLER_H__
11
12 #include "config.h"
13
14 /*
15  * When a signal is delivered to a thread on a Krait Android device
16  * that's in the middle of skipping over an "IT" block, such as this
17  * one:
18  *
19  * 0x40184ef0 <dlfree+1308>:    ldr     r1, [r3, #0]
20  * 0x40184ef2 <dlfree+1310>:    add.w   r5, r12, r2, lsl #3
21  * 0x40184ef6 <dlfree+1314>:    lsls.w  r2, r0, r2
22  * 0x40184efa <dlfree+1318>:    tst     r2, r1
23  * ### this is the IT instruction
24  * 0x40184efc <dlfree+1320>:    itt     eq
25  * 0x40184efe <dlfree+1322>:    orreq   r2, r1
26  * ### signal arrives here
27  * 0x40184f00 <dlfree+1324>:    streq   r2, [r3, #0]
28  * 0x40184f02 <dlfree+1326>:    beq.n   0x40184f1a <dlfree+1350>
29  * 0x40184f04 <dlfree+1328>:    ldr     r2, [r5, #8]
30  * 0x40184f06 <dlfree+1330>:    ldr     r3, [r3, #16]
31  *
32  * then the first few (at most four, one would assume) instructions of
33  * the signal handler (!) might be skipped.  They happen to be the
34  * push of the frame pointer and return address, so once the signal
35  * handler has done its work, it returns into a SIGSEGV.
36  */
37
38 #if defined (TARGET_ARM) && defined (HAVE_ARMV7) && defined (TARGET_ANDROID)
39 #define KRAIT_IT_BUG_WORKAROUND 1
40 #endif
41
42 #ifdef KRAIT_IT_BUG_WORKAROUND
43 #define MONO_SIGNAL_HANDLER_FUNC(access, name, arglist)         \
44         static void __krait_ ## name arglist;   \
45         __attribute__ ((__naked__)) access void                         \
46         name arglist                                                    \
47         {                                                               \
48                 asm volatile (                                          \
49                               "mov r0, r0\n\t"                          \
50                               "mov r0, r0\n\t"                          \
51                               "mov r0, r0\n\t"                          \
52                               "mov r0, r0\n\t"                          \
53                                   "b __krait_" # name                   \
54                                   "\n\t");                                              \
55         }       \
56         static __attribute__ ((__used__)) void __krait_ ## name arglist
57 #endif
58
59 /* Don't use this */
60 #ifndef MONO_SIGNAL_HANDLER_FUNC
61 #define MONO_SIGNAL_HANDLER_FUNC(access, name, arglist) access void name arglist
62 #endif
63
64 /*
65  * Macros to work around signal handler differences on various platforms.
66  *
67  * To declare a signal handler function:
68  * void MONO_SIG_HANDLER_SIGNATURE (handler_func)
69  * To define a signal handler function:
70  * MONO_SIG_HANDLER_FUNC(access, name)
71  * To call another signal handler function:
72  * handler_func (MONO_SIG_HANDLER_PARAMS);
73  * To obtain the signal number:
74  * int signo = MONO_SIG_HANDLER_GET_SIGNO ();
75  * To obtain the signal context:
76  * MONO_SIG_HANDLER_GET_CONTEXT ().
77  * This will define a variable name 'ctx'.
78  */
79
80 #ifdef HOST_WIN32
81 #define MONO_SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, EXCEPTION_POINTERS *_info, void *context)
82 #define MONO_SIG_HANDLER_FUNC(access, ftn) MONO_SIGNAL_HANDLER_FUNC (access, ftn, (int _dummy, EXCEPTION_POINTERS *_info, void *context))
83 #define MONO_SIG_HANDLER_PARAMS _dummy, _info, context
84 #define MONO_SIG_HANDLER_GET_SIGNO() (_dummy)
85 #define MONO_SIG_HANDLER_GET_INFO() (_info)
86 #define MONO_SIG_HANDLER_INFO_TYPE EXCEPTION_POINTERS
87 /* seh_vectored_exception_handler () passes in a CONTEXT* */
88 #define MONO_SIG_HANDLER_GET_CONTEXT \
89     void *ctx = context;
90 #else
91 /* sigaction */
92 #define MONO_SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *_info, void *context)
93 #define MONO_SIG_HANDLER_FUNC(access, ftn) MONO_SIGNAL_HANDLER_FUNC (access, ftn, (int _dummy, siginfo_t *_info, void *context))
94 #define MONO_SIG_HANDLER_PARAMS _dummy, _info, context
95 #define MONO_SIG_HANDLER_GET_SIGNO() (_dummy)
96 #define MONO_SIG_HANDLER_GET_INFO() (_info)
97 #define MONO_SIG_HANDLER_INFO_TYPE siginfo_t
98 #define MONO_SIG_HANDLER_GET_CONTEXT \
99     void *ctx = context;
100 #endif
101
102 #endif