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 | |
| 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" |
| 24 | #include "thread.h" |
| 25 | #include "thread-inl.h" |
| 26 | |
| 27 | // |
| 28 | // ARM specific fault handler functions. |
| 29 | // |
| 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | extern "C" void art_quick_throw_null_pointer_exception(); |
| 34 | extern "C" void art_quick_test_suspend(); |
| 35 | |
| 36 | void FaultManager::GetMethodAndReturnPC(void* context, uintptr_t& method, uintptr_t& return_pc) { |
| 37 | struct ucontext *uc = (struct ucontext *)context; |
| 38 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 39 | uintptr_t* sp = reinterpret_cast<uint32_t*>(sc->arm_sp); |
| 40 | if (sp == nullptr) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // Work out the return PC. This will be the address of the instruction |
| 45 | // following the faulting ldr/str instruction. This is in thumb mode so |
| 46 | // the instruction might be a 16 or 32 bit one. Also, the GC map always |
| 47 | // has the bottom bit of the PC set so we also need to set that. |
| 48 | |
| 49 | // Need to work out the size of the instruction that caused the exception. |
| 50 | uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc); |
| 51 | |
| 52 | uint16_t instr = ptr[0] | ptr[1] << 8; |
| 53 | bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800); |
| 54 | uint32_t instr_size = is_32bit ? 4 : 2; |
| 55 | |
| 56 | // The method is at the top of the stack. |
| 57 | method = sp[0]; |
| 58 | |
| 59 | return_pc = (sc->arm_pc + instr_size) | 1; |
| 60 | } |
| 61 | |
| 62 | bool NullPointerHandler::Action(int sig, siginfo_t* info, void* context) { |
| 63 | // The code that looks for the catch location needs to know the value of the |
| 64 | // ARM PC at the point of call. For Null checks we insert a GC map that is immediately after |
| 65 | // the load/store instruction that might cause the fault. However the mapping table has |
| 66 | // the low bits set for thumb mode so we need to set the bottom bit for the LR |
| 67 | // register in order to find the mapping. |
| 68 | |
| 69 | // Need to work out the size of the instruction that caused the exception. |
| 70 | struct ucontext *uc = (struct ucontext *)context; |
| 71 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 72 | uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc); |
| 73 | |
| 74 | uint16_t instr = ptr[0] | ptr[1] << 8; |
| 75 | bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800); |
| 76 | uint32_t instr_size = is_32bit ? 4 : 2; |
| 77 | sc->arm_lr = (sc->arm_pc + instr_size) | 1; // LR needs to point to gc map location |
| 78 | sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception); |
| 79 | LOG(DEBUG) << "Generating null pointer exception"; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | // A suspend check is done using the following instruction sequence: |
| 84 | // 0xf723c0b2: f8d902c0 ldr.w r0, [r9, #704] ; suspend_trigger_ |
| 85 | // .. some intervening instruction |
| 86 | // 0xf723c0b6: 6800 ldr r0, [r0, #0] |
| 87 | |
| 88 | // The offset from r9 is Thread::ThreadSuspendTriggerOffset(). |
| 89 | // To check for a suspend check, we examine the instructions that caused |
| 90 | // the fault (at PC-4 and PC). |
| 91 | bool SuspensionHandler::Action(int sig, siginfo_t* info, void* context) { |
| 92 | // These are the instructions to check for. The first one is the ldr r0,[r9,#xxx] |
| 93 | // where xxx is the offset of the suspend trigger. |
| 94 | uint32_t checkinst1 = 0xf8d90000 + Thread::ThreadSuspendTriggerOffset().Int32Value(); |
| 95 | uint16_t checkinst2 = 0x6800; |
| 96 | |
| 97 | struct ucontext *uc = (struct ucontext *)context; |
| 98 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 99 | uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->arm_pc); |
| 100 | uint8_t* ptr1 = ptr2 - 4; |
| 101 | LOG(DEBUG) << "checking suspend"; |
| 102 | |
| 103 | uint16_t inst2 = ptr2[0] | ptr2[1] << 8; |
| 104 | LOG(DEBUG) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2; |
| 105 | if (inst2 != checkinst2) { |
| 106 | // Second instruction is not good, not ours. |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | // The first instruction can a little bit up the stream due to load hoisting |
| 111 | // in the compiler. |
| 112 | uint8_t* limit = ptr1 - 40; // Compiler will hoist to a max of 20 instructions. |
| 113 | bool found = false; |
| 114 | while (ptr1 > limit) { |
| 115 | uint32_t inst1 = ((ptr1[0] | ptr1[1] << 8) << 16) | (ptr1[2] | ptr1[3] << 8); |
| 116 | LOG(DEBUG) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1; |
| 117 | if (inst1 == checkinst1) { |
| 118 | found = true; |
| 119 | break; |
| 120 | } |
| 121 | ptr1 -= 2; // Min instruction size is 2 bytes. |
| 122 | } |
| 123 | if (found) { |
| 124 | LOG(DEBUG) << "suspend check match"; |
| 125 | // This is a suspend check. Arrange for the signal handler to return to |
| 126 | // art_quick_test_suspend. Also set LR so that after the suspend check it |
| 127 | // will resume the instruction (current PC + 2). PC points to the |
| 128 | // ldr r0,[r0,#0] instruction (r0 will be 0, set by the trigger). |
| 129 | |
| 130 | // NB: remember that we need to set the bottom bit of the LR register |
| 131 | // to switch to thumb mode. |
| 132 | LOG(DEBUG) << "arm lr: " << std::hex << sc->arm_lr; |
| 133 | LOG(DEBUG) << "arm pc: " << std::hex << sc->arm_pc; |
| 134 | sc->arm_lr = sc->arm_pc + 3; // +2 + 1 (for thumb) |
| 135 | sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_test_suspend); |
| 136 | |
| 137 | // Now remove the suspend trigger that caused this fault. |
| 138 | Thread::Current()->RemoveSuspendTrigger(); |
| 139 | LOG(DEBUG) << "removed suspend trigger invoking test suspend"; |
| 140 | return true; |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | bool StackOverflowHandler::Action(int sig, siginfo_t* info, void* context) { |
| 146 | return false; |
| 147 | } |
| 148 | } // namespace art |