Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 17 | #include "fault_handler.h" |
| 18 | #include <sys/ucontext.h> |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 19 | |
| 20 | #include "art_method.h" |
| 21 | #include "base/hex_dump.h" |
| 22 | #include "base/logging.h" |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 23 | #include "base/macros.h" |
| 24 | #include "globals.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 25 | #include "quick_method_frame_info_mips.h" |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 26 | #include "registers_mips.h" |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 27 | #include "thread-inl.h" |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 28 | |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 29 | extern "C" void art_quick_throw_stack_overflow(); |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 30 | extern "C" void art_quick_throw_null_pointer_exception_from_signal(); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 31 | |
| 32 | // |
| 33 | // Mips specific fault handler functions. |
| 34 | // |
| 35 | |
| 36 | namespace art { |
| 37 | |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 38 | void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, |
| 39 | ArtMethod** out_method, |
| 40 | uintptr_t* out_return_pc, uintptr_t* out_sp) { |
| 41 | struct ucontext* uc = reinterpret_cast<struct ucontext*>(context); |
| 42 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
Goran Jakovljevic | 5d45e8f | 2016-06-30 16:40:20 +0200 | [diff] [blame] | 43 | *out_sp = static_cast<uintptr_t>(sc->sc_regs[mips::SP]); |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 44 | VLOG(signals) << "sp: " << *out_sp; |
| 45 | if (*out_sp == 0) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // In the case of a stack overflow, the stack is not valid and we can't |
| 50 | // get the method from the top of the stack. However it's in r0. |
| 51 | uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr); // BVA addr |
| 52 | uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>( |
| 53 | reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kMips)); |
| 54 | if (overflow_addr == fault_addr) { |
Goran Jakovljevic | 5d45e8f | 2016-06-30 16:40:20 +0200 | [diff] [blame] | 55 | *out_method = reinterpret_cast<ArtMethod*>(sc->sc_regs[mips::A0]); |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 56 | } else { |
| 57 | // The method is at the top of the stack. |
| 58 | *out_method = *reinterpret_cast<ArtMethod**>(*out_sp); |
| 59 | } |
| 60 | |
| 61 | // Work out the return PC. This will be the address of the instruction |
| 62 | // following the faulting ldr/str instruction. |
| 63 | |
| 64 | VLOG(signals) << "pc: " << std::hex |
| 65 | << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->sc_pc)); |
| 66 | |
| 67 | *out_return_pc = sc->sc_pc + 4; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 70 | bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) { |
| 71 | if (!IsValidImplicitCheck(info)) { |
| 72 | return false; |
| 73 | } |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 74 | // The code that looks for the catch location needs to know the value of the |
| 75 | // PC at the point of call. For Null checks we insert a GC map that is immediately after |
| 76 | // the load/store instruction that might cause the fault. |
| 77 | |
| 78 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 79 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 80 | |
Vladimir Marko | 3b7537b | 2016-09-13 11:56:01 +0000 | [diff] [blame] | 81 | // Decrement $sp by the frame size of the kSaveEverything method and store |
| 82 | // the fault address in the padding right after the ArtMethod*. |
| 83 | sc->sc_regs[mips::SP] -= mips::MipsCalleeSaveFrameSize(Runtime::kSaveEverything); |
| 84 | uintptr_t* padding = reinterpret_cast<uintptr_t*>(sc->sc_regs[mips::SP]) + /* ArtMethod* */ 1; |
| 85 | *padding = reinterpret_cast<uintptr_t>(info->si_addr); |
| 86 | |
Goran Jakovljevic | 5d45e8f | 2016-06-30 16:40:20 +0200 | [diff] [blame] | 87 | sc->sc_regs[mips::RA] = sc->sc_pc + 4; // RA needs to point to gc map location |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 88 | sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal); |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 89 | // Note: This entrypoint does not rely on T9 pointing to it, so we may as well preserve T9. |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 90 | VLOG(signals) << "Generating null pointer exception"; |
| 91 | return true; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 94 | bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, |
| 95 | void* context ATTRIBUTE_UNUSED) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 99 | // Stack overflow fault handler. |
| 100 | // |
| 101 | // This checks that the fault address is equal to the current stack pointer |
| 102 | // minus the overflow region size (16K typically). The instruction that |
| 103 | // generates this signal is: |
| 104 | // |
| 105 | // lw zero, -16384(sp) |
| 106 | // |
| 107 | // It will fault if sp is inside the protected region on the stack. |
| 108 | // |
| 109 | // If we determine this is a stack overflow we need to move the stack pointer |
| 110 | // to the overflow region below the protected region. |
| 111 | |
| 112 | bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) { |
| 113 | struct ucontext* uc = reinterpret_cast<struct ucontext*>(context); |
| 114 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 115 | VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc; |
| 116 | VLOG(signals) << "sigcontext: " << std::hex << sc; |
| 117 | |
Goran Jakovljevic | 5d45e8f | 2016-06-30 16:40:20 +0200 | [diff] [blame] | 118 | uintptr_t sp = sc->sc_regs[mips::SP]; |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 119 | VLOG(signals) << "sp: " << std::hex << sp; |
| 120 | |
| 121 | uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr); // BVA addr |
| 122 | VLOG(signals) << "fault_addr: " << std::hex << fault_addr; |
| 123 | VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp << |
| 124 | ", fault_addr: " << fault_addr; |
| 125 | |
| 126 | uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kMips); |
| 127 | |
| 128 | // Check that the fault address is the value expected for a stack overflow. |
| 129 | if (fault_addr != overflow_addr) { |
| 130 | VLOG(signals) << "Not a stack overflow"; |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | VLOG(signals) << "Stack overflow found"; |
| 135 | |
| 136 | // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from. |
| 137 | // The value of RA must be the same as it was when we entered the code that |
| 138 | // caused this fault. This will be inserted into a callee save frame by |
| 139 | // the function to which this handler returns (art_quick_throw_stack_overflow). |
| 140 | sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow); |
Goran Jakovljevic | 5d45e8f | 2016-06-30 16:40:20 +0200 | [diff] [blame] | 141 | sc->sc_regs[mips::T9] = sc->sc_pc; // make sure T9 points to the function |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 142 | |
| 143 | // The kernel will now return to the address in sc->arm_pc. |
| 144 | return true; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 145 | } |
| 146 | } // namespace art |