Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
| 7 | 7 |
| 8 #include <sys/syscall.h> // NOLINT | 8 #include <sys/syscall.h> // NOLINT |
| 9 #include <errno.h> // NOLINT | 9 #include <errno.h> // NOLINT |
| 10 | 10 |
| 11 #include "vm/flags.h" | 11 #include "vm/flags.h" |
| 12 #include "vm/os.h" | 12 #include "vm/os.h" |
| 13 #include "vm/profiler.h" | 13 #include "vm/profiler.h" |
| 14 #include "vm/signal_handler.h" | 14 #include "vm/signal_handler.h" |
| 15 #include "vm/thread_interrupter.h" | 15 #include "vm/thread_interrupter.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 | 18 |
| 19 #ifndef PRODUCT | 19 #ifndef PRODUCT |
| 20 | 20 |
| 21 DECLARE_FLAG(bool, thread_interrupter); | 21 DECLARE_FLAG(bool, thread_interrupter); |
| 22 DECLARE_FLAG(bool, trace_thread_interrupter); | 22 DECLARE_FLAG(bool, trace_thread_interrupter); |
| 23 | 23 |
| 24 class ThreadInterrupterAndroid : public AllStatic { | 24 static void ThreadInterruptSignalHandler(int signal, siginfo_t* info, |
| 25 public: | 25 void* context_) { |
| 26 static void ThreadInterruptSignalHandler(int signal, siginfo_t* info, | 26 if (signal != SIGPROF) { |
| 27 void* context_) { | 27 return; |
| 28 if (signal != SIGPROF) { | |
| 29 return; | |
| 30 } | |
| 31 Thread* thread = Thread::Current(); | |
| 32 if (thread == NULL) { | |
| 33 return; | |
| 34 } | |
| 35 // Extract thread state. | |
| 36 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); | |
| 37 mcontext_t mcontext = context->uc_mcontext; | |
| 38 InterruptedThreadState its; | |
| 39 its.pc = SignalHandler::GetProgramCounter(mcontext); | |
| 40 its.fp = SignalHandler::GetFramePointer(mcontext); | |
| 41 its.csp = SignalHandler::GetCStackPointer(mcontext); | |
| 42 its.dsp = SignalHandler::GetDartStackPointer(mcontext); | |
| 43 its.lr = SignalHandler::GetLinkRegister(mcontext); | |
| 44 Profiler::SampleThread(thread, its); | |
| 45 } | 28 } |
| 46 }; | 29 Thread* thread = Thread::Current(); |
| 30 if (thread == NULL) { | |
| 31 return; | |
| 32 } | |
| 33 // Extract thread state. | |
| 34 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); | |
| 35 mcontext_t mcontext = context->uc_mcontext; | |
| 36 InterruptedThreadState its; | |
| 37 its.pc = SignalHandler::GetProgramCounter(mcontext); | |
| 38 its.fp = SignalHandler::GetFramePointer(mcontext); | |
| 39 its.csp = SignalHandler::GetCStackPointer(mcontext); | |
| 40 its.dsp = SignalHandler::GetDartStackPointer(mcontext); | |
| 41 its.lr = SignalHandler::GetLinkRegister(mcontext); | |
| 42 Profiler::SampleThread(thread, its); | |
| 43 } | |
| 44 | |
| 45 | |
| 46 // Work around for a bug in old kernels (only fixed in 3.18 Android kernel): | |
| 47 // kernel does not clear If-Then execution state bits when entering ARM signal | |
| 48 // handler which violates requirements imposed by ARM architecture reference. | |
| 49 // Some CPUs look at these bits even while in ARM mode which causes them | |
| 50 // to skip some instructions in the prologue of the signal handler. | |
| 51 // To work around the issue we insert enough NOPs in the prologue to ensure that | |
| 52 // no actual instructions are skipped and then branch to the actual signal | |
| 53 // handler. | |
| 54 // For the kernel patch that fixes the issue see: http://git.kernel.org/cgit/lin ux/kernel/git/torvalds/linux.git/commit/?id=6ecf830e5029598732e04067e325d9460975 19cb | |
| 55 // Note: this function is marked "naked" because we must guarantee that | |
| 56 // our NOPs occur before any compiler generated prologue. | |
| 57 static __attribute__((naked)) void SignalHandlerTrampoline(int signal, | |
| 58 siginfo_t* info, | |
| 59 void* context_) { | |
| 60 // IT (If-Then) instruction makes up to four instructions that follow it | |
| 61 // conditional. | |
| 62 asm volatile("nop; nop; nop; nop" : : : "memory"); | |
| 63 | |
| 64 // Tail-call into the actual signal handler. | |
| 65 // Note: this code is split into a separate inline assembly block because | |
| 66 // any code that compiler generates to satisfy register constraints must | |
| 67 // be generated after four NOPs. | |
| 68 register int arg0 asm("r0") = signal; | |
| 69 register siginfo_t* arg1 asm("r1") = info; | |
| 70 register void* arg2 asm("r2") = context_; | |
| 71 asm volatile("bx %3" | |
|
Florian Schneider
2016/05/02 13:44:45
Need this only on ARM, not on Android ia32 for exa
| |
| 72 : | |
| 73 : "r"(arg0), "r"(arg1), "r"(arg2), | |
| 74 "r"(&ThreadInterruptSignalHandler) | |
| 75 : "memory"); | |
| 76 } | |
| 47 | 77 |
| 48 | 78 |
| 49 void ThreadInterrupter::InterruptThread(OSThread* thread) { | 79 void ThreadInterrupter::InterruptThread(OSThread* thread) { |
| 50 if (FLAG_trace_thread_interrupter) { | 80 if (FLAG_trace_thread_interrupter) { |
| 51 OS::Print("ThreadInterrupter interrupting %p\n", | 81 OS::Print("ThreadInterrupter interrupting %p\n", |
| 52 reinterpret_cast<void*>(thread->id())); | 82 reinterpret_cast<void*>(thread->id())); |
| 53 } | 83 } |
| 54 int result = syscall(__NR_tgkill, getpid(), thread->id(), SIGPROF); | 84 int result = syscall(__NR_tgkill, getpid(), thread->id(), SIGPROF); |
| 55 ASSERT((result == 0) || (result == ESRCH)); | 85 ASSERT((result == 0) || (result == ESRCH)); |
| 56 } | 86 } |
| 57 | 87 |
| 58 | 88 |
| 59 void ThreadInterrupter::InstallSignalHandler() { | 89 void ThreadInterrupter::InstallSignalHandler() { |
| 60 SignalHandler::Install( | 90 SignalHandler::Install(&SignalHandlerTrampoline); |
| 61 ThreadInterrupterAndroid::ThreadInterruptSignalHandler); | |
| 62 } | 91 } |
| 63 | 92 |
| 64 | 93 |
| 65 void ThreadInterrupter::RemoveSignalHandler() { | 94 void ThreadInterrupter::RemoveSignalHandler() { |
| 66 SignalHandler::Remove(); | 95 SignalHandler::Remove(); |
| 67 } | 96 } |
| 68 | 97 |
| 69 #endif // !PRODUCT | 98 #endif // !PRODUCT |
| 70 | 99 |
| 71 } // namespace dart | 100 } // namespace dart |
| 72 | 101 |
| 73 #endif // defined(TARGET_OS_ANDROID) | 102 #endif // defined(TARGET_OS_ANDROID) |
| OLD | NEW |