Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "fault_handler.h" |
| 19 | #include <sys/ucontext.h> |
| 20 | #include "base/macros.h" |
| 21 | #include "globals.h" |
| 22 | #include "base/logging.h" |
| 23 | #include "base/hex_dump.h" |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 24 | #include "registers_arm64.h" |
| 25 | #include "mirror/art_method.h" |
| 26 | #include "mirror/art_method-inl.h" |
| 27 | #include "thread.h" |
| 28 | #include "thread-inl.h" |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 29 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 30 | extern "C" void art_quick_throw_stack_overflow(); |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 31 | extern "C" void art_quick_throw_null_pointer_exception(); |
| 32 | extern "C" void art_quick_implicit_suspend(); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 33 | |
| 34 | // |
| 35 | // ARM64 specific fault handler functions. |
| 36 | // |
| 37 | |
| 38 | namespace art { |
| 39 | |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 40 | void FaultManager::HandleNestedSignal(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, |
| 41 | void* context) { |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 42 | // To match the case used in ARM we return directly to the longjmp function |
| 43 | // rather than through a trivial assembly language stub. |
| 44 | |
| 45 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 46 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 47 | Thread* self = Thread::Current(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 48 | CHECK(self != nullptr); // This will cause a SIGABRT if self is null. |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 49 | |
| 50 | sc->regs[0] = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState()); |
| 51 | sc->regs[1] = 1; |
| 52 | sc->pc = reinterpret_cast<uintptr_t>(longjmp); |
| 53 | } |
| 54 | |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 55 | void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo ATTRIBUTE_UNUSED, void* context, |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 56 | mirror::ArtMethod** out_method, |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 57 | uintptr_t* out_return_pc, uintptr_t* out_sp) { |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 58 | struct ucontext *uc = reinterpret_cast<struct ucontext *>(context); |
| 59 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 60 | *out_sp = static_cast<uintptr_t>(sc->sp); |
| 61 | VLOG(signals) << "sp: " << *out_sp; |
| 62 | if (*out_sp == 0) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // In the case of a stack overflow, the stack is not valid and we can't |
| 67 | // get the method from the top of the stack. However it's in x0. |
| 68 | uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address); |
| 69 | uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>( |
| 70 | reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kArm64)); |
| 71 | if (overflow_addr == fault_addr) { |
| 72 | *out_method = reinterpret_cast<mirror::ArtMethod*>(sc->regs[0]); |
| 73 | } else { |
| 74 | // The method is at the top of the stack. |
| 75 | *out_method = (reinterpret_cast<StackReference<mirror::ArtMethod>* >(*out_sp)[0]).AsMirrorPtr(); |
| 76 | } |
| 77 | |
| 78 | // Work out the return PC. This will be the address of the instruction |
| 79 | // following the faulting ldr/str instruction. |
| 80 | VLOG(signals) << "pc: " << std::hex |
| 81 | << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->pc)); |
| 82 | |
| 83 | *out_return_pc = sc->pc + 4; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 86 | bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, |
| 87 | void* context) { |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 88 | // The code that looks for the catch location needs to know the value of the |
| 89 | // PC at the point of call. For Null checks we insert a GC map that is immediately after |
| 90 | // the load/store instruction that might cause the fault. |
| 91 | |
| 92 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 93 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 94 | |
| 95 | sc->regs[30] = sc->pc + 4; // LR needs to point to gc map location |
| 96 | |
| 97 | sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception); |
| 98 | VLOG(signals) << "Generating null pointer exception"; |
| 99 | return true; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 102 | // A suspend check is done using the following instruction sequence: |
| 103 | // 0xf7223228: f9405640 ldr x0, [x18, #168] |
| 104 | // .. some intervening instructions |
| 105 | // 0xf7223230: f9400000 ldr x0, [x0] |
| 106 | |
| 107 | // The offset from r18 is Thread::ThreadSuspendTriggerOffset(). |
| 108 | // To check for a suspend check, we examine the instructions that caused |
| 109 | // the fault (at PC-4 and PC). |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 110 | bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, |
| 111 | void* context) { |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 112 | // These are the instructions to check for. The first one is the ldr x0,[r18,#xxx] |
| 113 | // where xxx is the offset of the suspend trigger. |
| 114 | uint32_t checkinst1 = 0xf9400240 | (Thread::ThreadSuspendTriggerOffset<8>().Int32Value() << 7); |
| 115 | uint32_t checkinst2 = 0xf9400000; |
| 116 | |
| 117 | struct ucontext *uc = reinterpret_cast<struct ucontext *>(context); |
| 118 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 119 | uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->pc); |
| 120 | uint8_t* ptr1 = ptr2 - 4; |
| 121 | VLOG(signals) << "checking suspend"; |
| 122 | |
| 123 | uint32_t inst2 = *reinterpret_cast<uint32_t*>(ptr2); |
| 124 | VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2; |
| 125 | if (inst2 != checkinst2) { |
| 126 | // Second instruction is not good, not ours. |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | // The first instruction can a little bit up the stream due to load hoisting |
| 131 | // in the compiler. |
| 132 | uint8_t* limit = ptr1 - 80; // Compiler will hoist to a max of 20 instructions. |
| 133 | bool found = false; |
| 134 | while (ptr1 > limit) { |
| 135 | uint32_t inst1 = *reinterpret_cast<uint32_t*>(ptr1); |
| 136 | VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1; |
| 137 | if (inst1 == checkinst1) { |
| 138 | found = true; |
| 139 | break; |
| 140 | } |
| 141 | ptr1 -= 4; |
| 142 | } |
| 143 | if (found) { |
| 144 | VLOG(signals) << "suspend check match"; |
| 145 | // This is a suspend check. Arrange for the signal handler to return to |
| 146 | // art_quick_implicit_suspend. Also set LR so that after the suspend check it |
| 147 | // will resume the instruction (current PC + 4). PC points to the |
| 148 | // ldr x0,[x0,#0] instruction (r0 will be 0, set by the trigger). |
| 149 | |
| 150 | sc->regs[30] = sc->pc + 4; |
| 151 | sc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend); |
| 152 | |
| 153 | // Now remove the suspend trigger that caused this fault. |
| 154 | Thread::Current()->RemoveSuspendTrigger(); |
| 155 | VLOG(signals) << "removed suspend trigger invoking test suspend"; |
| 156 | return true; |
| 157 | } |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 161 | bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, |
| 162 | void* context) { |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 163 | struct ucontext *uc = reinterpret_cast<struct ucontext *>(context); |
| 164 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 165 | VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc; |
| 166 | VLOG(signals) << "sigcontext: " << std::hex << sc; |
| 167 | |
| 168 | uintptr_t sp = sc->sp; |
| 169 | VLOG(signals) << "sp: " << std::hex << sp; |
| 170 | |
| 171 | uintptr_t fault_addr = sc->fault_address; |
| 172 | VLOG(signals) << "fault_addr: " << std::hex << fault_addr; |
| 173 | VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp << |
| 174 | ", fault_addr: " << fault_addr; |
| 175 | |
| 176 | uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kArm64); |
| 177 | |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 178 | // Check that the fault address is the value expected for a stack overflow. |
| 179 | if (fault_addr != overflow_addr) { |
| 180 | VLOG(signals) << "Not a stack overflow"; |
| 181 | return false; |
| 182 | } |
| 183 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 184 | VLOG(signals) << "Stack overflow found"; |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 185 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 186 | // Now arrange for the signal handler to return to art_quick_throw_stack_overflow. |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 187 | // The value of LR must be the same as it was when we entered the code that |
| 188 | // caused this fault. This will be inserted into a callee save frame by |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 189 | // the function to which this handler returns (art_quick_throw_stack_overflow). |
| 190 | sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow); |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 191 | |
| 192 | // The kernel will now return to the address in sc->pc. |
| 193 | return true; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 194 | } |
| 195 | } // namespace art |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 196 | |