[runtime] Cycle the finalizer thread if there are too many outstanding hazard pointer...
[mono.git] / mono / utils / mono-signal-handler.h
1 /*
2  * mono-signal-handler.h: Handle signal handler differences across platforms
3  *
4  * Copyright (C) 2013 Xamarin Inc
5  */
6
7 #ifndef __MONO_SIGNAL_HANDLER_H__
8 #define __MONO_SIGNAL_HANDLER_H__
9
10 #include "config.h"
11
12 /*
13  * When a signal is delivered to a thread on a Krait Android device
14  * that's in the middle of skipping over an "IT" block, such as this
15  * one:
16  *
17  * 0x40184ef0 <dlfree+1308>:    ldr     r1, [r3, #0]
18  * 0x40184ef2 <dlfree+1310>:    add.w   r5, r12, r2, lsl #3
19  * 0x40184ef6 <dlfree+1314>:    lsls.w  r2, r0, r2
20  * 0x40184efa <dlfree+1318>:    tst     r2, r1
21  * ### this is the IT instruction
22  * 0x40184efc <dlfree+1320>:    itt     eq
23  * 0x40184efe <dlfree+1322>:    orreq   r2, r1
24  * ### signal arrives here
25  * 0x40184f00 <dlfree+1324>:    streq   r2, [r3, #0]
26  * 0x40184f02 <dlfree+1326>:    beq.n   0x40184f1a <dlfree+1350>
27  * 0x40184f04 <dlfree+1328>:    ldr     r2, [r5, #8]
28  * 0x40184f06 <dlfree+1330>:    ldr     r3, [r3, #16]
29  *
30  * then the first few (at most four, one would assume) instructions of
31  * the signal handler (!) might be skipped.  They happen to be the
32  * push of the frame pointer and return address, so once the signal
33  * handler has done its work, it returns into a SIGSEGV.
34  */
35
36 #if defined (TARGET_ARM) && defined (HAVE_ARMV7) && defined (TARGET_ANDROID)
37 #define KRAIT_IT_BUG_WORKAROUND 1
38 #endif
39
40 #ifdef KRAIT_IT_BUG_WORKAROUND
41 #define MONO_SIGNAL_HANDLER_FUNC(access, name, arglist)         \
42         static void __krait_ ## name arglist;   \
43         __attribute__ ((naked)) access void                             \
44         name arglist                                                    \
45         {                                                               \
46                 asm volatile (                                          \
47                               "mov r0, r0\n\t"                          \
48                               "mov r0, r0\n\t"                          \
49                               "mov r0, r0\n\t"                          \
50                               "mov r0, r0\n\t");                        \
51                 asm volatile (                                          \
52                               "bx %0"                                   \
53                               : : "r" (__krait_ ## name));              \
54         }       \
55         static void __krait_ ## name arglist
56 #endif
57
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