1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #ifndef RUNTIME_VM_SIGNAL_HANDLER_H_ |
6 | #define RUNTIME_VM_SIGNAL_HANDLER_H_ |
7 | |
8 | #include "vm/allocation.h" |
9 | #include "vm/globals.h" |
10 | |
11 | #if defined(HOST_OS_LINUX) |
12 | #include <signal.h> // NOLINT |
13 | #include <ucontext.h> // NOLINT |
14 | #elif defined(HOST_OS_ANDROID) |
15 | #include <signal.h> // NOLINT |
16 | #if !defined(__BIONIC_HAVE_UCONTEXT_T) |
17 | #include <asm/sigcontext.h> // NOLINT |
18 | // If ucontext_t is not defined on Android, define it here. |
19 | typedef struct sigcontext mcontext_t; |
20 | typedef struct ucontext { |
21 | uint32_t uc_flags; |
22 | struct ucontext* uc_link; |
23 | stack_t uc_stack; |
24 | struct sigcontext uc_mcontext; |
25 | uint32_t uc_sigmask; |
26 | } ucontext_t; |
27 | #endif // !defined(__BIONIC_HAVE_UCONTEXT_T) |
28 | #elif defined(HOST_OS_MACOS) |
29 | #include <signal.h> // NOLINT |
30 | #include <sys/ucontext.h> // NOLINT |
31 | #elif defined(HOST_OS_WINDOWS) |
32 | // Stub out for windows. |
33 | struct siginfo_t; |
34 | struct mcontext_t; |
35 | struct sigset_t {}; |
36 | #elif defined(HOST_OS_FUCHSIA) |
37 | #include <signal.h> // NOLINT |
38 | #include <ucontext.h> // NOLINT |
39 | #endif |
40 | |
41 | // Old linux kernels on ARM might require a trampoline to |
42 | // work around incorrect Thumb -> ARM transitions. See SignalHandlerTrampoline |
43 | // below for more details. |
44 | #if defined(HOST_ARCH_ARM) && \ |
45 | (defined(HOST_OS_LINUX) || defined(HOST_OS_ANDROID)) && \ |
46 | !defined(__thumb__) |
47 | #define USE_SIGNAL_HANDLER_TRAMPOLINE |
48 | #endif |
49 | |
50 | namespace dart { |
51 | |
52 | typedef void (*SignalAction)(int signal, siginfo_t* info, void* context); |
53 | |
54 | class SignalHandler : public AllStatic { |
55 | public: |
56 | template <SignalAction action> |
57 | static void Install() { |
58 | #if defined(USE_SIGNAL_HANDLER_TRAMPOLINE) |
59 | InstallImpl(SignalHandlerTrampoline<action>); |
60 | #else |
61 | InstallImpl(action); |
62 | #endif // defined(USE_SIGNAL_HANDLER_TRAMPOLINE) |
63 | } |
64 | static void Remove(); |
65 | static uintptr_t GetProgramCounter(const mcontext_t& mcontext); |
66 | static uintptr_t GetFramePointer(const mcontext_t& mcontext); |
67 | static uintptr_t GetCStackPointer(const mcontext_t& mcontext); |
68 | static uintptr_t GetDartStackPointer(const mcontext_t& mcontext); |
69 | static uintptr_t GetLinkRegister(const mcontext_t& mcontext); |
70 | |
71 | private: |
72 | static void InstallImpl(SignalAction action); |
73 | |
74 | #if defined(USE_SIGNAL_HANDLER_TRAMPOLINE) |
75 | // Work around for a bug in old kernels (only fixed in 3.18 Android kernel): |
76 | // |
77 | // Kernel does not clear If-Then execution state bits when entering ARM signal |
78 | // handler which violates requirements imposed by ARM architecture reference. |
79 | // Some CPUs look at these bits even while in ARM mode which causes them |
80 | // to skip some instructions in the prologue of the signal handler. |
81 | // |
82 | // To work around the issue we insert enough NOPs in the prologue to ensure |
83 | // that no actual instructions are skipped and then branch to the actual |
84 | // signal handler. |
85 | // |
86 | // For the kernel patch that fixes the issue see: |
87 | // http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=6ecf830e5029598732e04067e325d946097519cb |
88 | // |
89 | // Note: this function is marked "naked" because we must guarantee that |
90 | // our NOPs occur before any compiler generated prologue. |
91 | template <SignalAction action> |
92 | static __attribute__((naked)) void SignalHandlerTrampoline(int signal, |
93 | siginfo_t* info, |
94 | void* context_) { |
95 | // IT (If-Then) instruction makes up to four instructions that follow it |
96 | // conditional. |
97 | // Note: clobber all register so that compiler does not attempt to hoist |
98 | // anything from the next assembly block past this one. |
99 | asm volatile("nop; nop; nop; nop;" |
100 | : |
101 | : |
102 | : "r0" , "r1" , "r2" , "r3" , "r4" , "r5" , "r6" , "r7" , "r8" , "r9" , |
103 | "r10" , "r11" , "r12" , "r13" , "r14" , "memory" ); |
104 | |
105 | // Tail-call into the actual signal handler. |
106 | // |
107 | // Note: this code is split into a separate inline assembly block because |
108 | // any code that compiler generates to satisfy register constraints must |
109 | // be generated after four NOPs. |
110 | // |
111 | // Note: there is no portable way to specify that we want to have |
112 | // signal, info and context_ in r0 - r2 respectively. So we just mark them |
113 | // as clobbered and hope that compiler does not emit any code that uses |
114 | // these registers to satisfy action constraint (we tested on clang and |
115 | // the generated code looks like one would expect). |
116 | asm volatile("bx %0;" : : "r" (action) : "r0" , "r1" , "r2" , "memory" ); |
117 | } |
118 | #endif // defined(USE_SIGNAL_HANDLER_TRAMPOLINE) |
119 | }; |
120 | |
121 | #undef USE_SIGNAL_HANDLER_TRAMPOLINE |
122 | |
123 | } // namespace dart |
124 | |
125 | #endif // RUNTIME_VM_SIGNAL_HANDLER_H_ |
126 | |