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" |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 21 | #include "base/hex_dump.h" |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 22 | #include "globals.h" |
| 23 | #include "base/logging.h" |
| 24 | #include "base/hex_dump.h" |
Andreas Gampe | 7cd26f3 | 2014-06-18 17:01:15 -0700 | [diff] [blame] | 25 | #include "instruction_set.h" |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 26 | #include "mirror/art_method.h" |
| 27 | #include "mirror/art_method-inl.h" |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 28 | #include "thread.h" |
| 29 | #include "thread-inl.h" |
| 30 | |
| 31 | // |
| 32 | // ARM specific fault handler functions. |
| 33 | // |
| 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | extern "C" void art_quick_throw_null_pointer_exception(); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 38 | extern "C" void art_quick_throw_stack_overflow_from_signal(); |
Dave Allison | 8325296 | 2014-04-03 16:33:48 -0700 | [diff] [blame] | 39 | extern "C" void art_quick_implicit_suspend(); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 40 | |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 41 | // Get the size of a thumb2 instruction in bytes. |
| 42 | static uint32_t GetInstructionSize(uint8_t* pc) { |
| 43 | uint16_t instr = pc[0] | pc[1] << 8; |
| 44 | bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800); |
| 45 | uint32_t instr_size = is_32bit ? 4 : 2; |
| 46 | return instr_size; |
| 47 | } |
| 48 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 49 | void FaultManager::GetMethodAndReturnPCAndSP(void* context, mirror::ArtMethod** out_method, |
| 50 | uintptr_t* out_return_pc, uintptr_t* out_sp) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 51 | struct ucontext *uc = (struct ucontext *)context; |
| 52 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 53 | *out_sp = static_cast<uintptr_t>(sc->arm_sp); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 54 | VLOG(signals) << "sp: " << *out_sp; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 55 | if (*out_sp == 0) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 59 | // In the case of a stack overflow, the stack is not valid and we can't |
| 60 | // get the method from the top of the stack. However it's in r0. |
| 61 | uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address); |
| 62 | uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>( |
Andreas Gampe | 7cd26f3 | 2014-06-18 17:01:15 -0700 | [diff] [blame] | 63 | reinterpret_cast<uint8_t*>(*out_sp) - kArmStackOverflowReservedBytes); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 64 | if (overflow_addr == fault_addr) { |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 65 | *out_method = reinterpret_cast<mirror::ArtMethod*>(sc->arm_r0); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 66 | } else { |
| 67 | // The method is at the top of the stack. |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 68 | *out_method = reinterpret_cast<mirror::ArtMethod*>(reinterpret_cast<uintptr_t*>(*out_sp)[0]); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 71 | // Work out the return PC. This will be the address of the instruction |
| 72 | // following the faulting ldr/str instruction. This is in thumb mode so |
| 73 | // the instruction might be a 16 or 32 bit one. Also, the GC map always |
| 74 | // has the bottom bit of the PC set so we also need to set that. |
| 75 | |
| 76 | // Need to work out the size of the instruction that caused the exception. |
| 77 | uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 78 | VLOG(signals) << "pc: " << std::hex << static_cast<void*>(ptr); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 79 | uint32_t instr_size = GetInstructionSize(ptr); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 80 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 81 | *out_return_pc = (sc->arm_pc + instr_size) | 1; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | bool NullPointerHandler::Action(int sig, siginfo_t* info, void* context) { |
| 85 | // The code that looks for the catch location needs to know the value of the |
| 86 | // ARM PC at the point of call. For Null checks we insert a GC map that is immediately after |
| 87 | // the load/store instruction that might cause the fault. However the mapping table has |
| 88 | // the low bits set for thumb mode so we need to set the bottom bit for the LR |
| 89 | // register in order to find the mapping. |
| 90 | |
| 91 | // Need to work out the size of the instruction that caused the exception. |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 92 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 93 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 94 | uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc); |
| 95 | |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 96 | uint32_t instr_size = GetInstructionSize(ptr); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 97 | sc->arm_lr = (sc->arm_pc + instr_size) | 1; // LR needs to point to gc map location |
| 98 | sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 99 | VLOG(signals) << "Generating null pointer exception"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
| 103 | // A suspend check is done using the following instruction sequence: |
| 104 | // 0xf723c0b2: f8d902c0 ldr.w r0, [r9, #704] ; suspend_trigger_ |
| 105 | // .. some intervening instruction |
| 106 | // 0xf723c0b6: 6800 ldr r0, [r0, #0] |
| 107 | |
| 108 | // The offset from r9 is Thread::ThreadSuspendTriggerOffset(). |
| 109 | // To check for a suspend check, we examine the instructions that caused |
| 110 | // the fault (at PC-4 and PC). |
| 111 | bool SuspensionHandler::Action(int sig, siginfo_t* info, void* context) { |
| 112 | // These are the instructions to check for. The first one is the ldr r0,[r9,#xxx] |
| 113 | // where xxx is the offset of the suspend trigger. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 114 | uint32_t checkinst1 = 0xf8d90000 + Thread::ThreadSuspendTriggerOffset<4>().Int32Value(); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 115 | uint16_t checkinst2 = 0x6800; |
| 116 | |
| 117 | struct ucontext *uc = (struct ucontext *)context; |
| 118 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 119 | uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->arm_pc); |
| 120 | uint8_t* ptr1 = ptr2 - 4; |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 121 | VLOG(signals) << "checking suspend"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 122 | |
| 123 | uint16_t inst2 = ptr2[0] | ptr2[1] << 8; |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 124 | VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 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 - 40; // Compiler will hoist to a max of 20 instructions. |
| 133 | bool found = false; |
| 134 | while (ptr1 > limit) { |
| 135 | uint32_t inst1 = ((ptr1[0] | ptr1[1] << 8) << 16) | (ptr1[2] | ptr1[3] << 8); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 136 | VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 137 | if (inst1 == checkinst1) { |
| 138 | found = true; |
| 139 | break; |
| 140 | } |
| 141 | ptr1 -= 2; // Min instruction size is 2 bytes. |
| 142 | } |
| 143 | if (found) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 144 | VLOG(signals) << "suspend check match"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 145 | // This is a suspend check. Arrange for the signal handler to return to |
Dave Allison | 8325296 | 2014-04-03 16:33:48 -0700 | [diff] [blame] | 146 | // art_quick_implicit_suspend. Also set LR so that after the suspend check it |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 147 | // will resume the instruction (current PC + 2). PC points to the |
| 148 | // ldr r0,[r0,#0] instruction (r0 will be 0, set by the trigger). |
| 149 | |
| 150 | // NB: remember that we need to set the bottom bit of the LR register |
| 151 | // to switch to thumb mode. |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 152 | VLOG(signals) << "arm lr: " << std::hex << sc->arm_lr; |
| 153 | VLOG(signals) << "arm pc: " << std::hex << sc->arm_pc; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 154 | sc->arm_lr = sc->arm_pc + 3; // +2 + 1 (for thumb) |
Dave Allison | 8325296 | 2014-04-03 16:33:48 -0700 | [diff] [blame] | 155 | sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 156 | |
| 157 | // Now remove the suspend trigger that caused this fault. |
Dave Allison | d6ed642 | 2014-04-09 23:36:15 +0000 | [diff] [blame] | 158 | Thread::Current()->RemoveSuspendTrigger(); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 159 | VLOG(signals) << "removed suspend trigger invoking test suspend"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 165 | // Stack overflow fault handler. |
| 166 | // |
| 167 | // This checks that the fault address is equal to the current stack pointer |
| 168 | // minus the overflow region size (16K typically). The instruction sequence |
| 169 | // that generates this signal is: |
| 170 | // |
| 171 | // sub r12,sp,#16384 |
| 172 | // ldr.w r12,[r12,#0] |
| 173 | // |
| 174 | // The second instruction will fault if r12 is inside the protected region |
| 175 | // on the stack. |
| 176 | // |
| 177 | // If we determine this is a stack overflow we need to move the stack pointer |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 178 | // to the overflow region below the protected region. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 179 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 180 | bool StackOverflowHandler::Action(int sig, siginfo_t* info, void* context) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 181 | struct ucontext *uc = (struct ucontext *)context; |
| 182 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 183 | VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc; |
| 184 | VLOG(signals) << "sigcontext: " << std::hex << sc; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 185 | |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 186 | uintptr_t sp = sc->arm_sp; |
| 187 | VLOG(signals) << "sp: " << std::hex << sp; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 188 | |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 189 | uintptr_t fault_addr = sc->fault_address; |
| 190 | VLOG(signals) << "fault_addr: " << std::hex << fault_addr; |
| 191 | VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp << |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 192 | ", fault_addr: " << fault_addr; |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 193 | |
Andreas Gampe | 7cd26f3 | 2014-06-18 17:01:15 -0700 | [diff] [blame] | 194 | uintptr_t overflow_addr = sp - kArmStackOverflowReservedBytes; |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 195 | |
| 196 | Thread* self = reinterpret_cast<Thread*>(sc->arm_r9); |
| 197 | CHECK_EQ(self, Thread::Current()); |
| 198 | uintptr_t pregion = reinterpret_cast<uintptr_t>(self->GetStackEnd()) - |
| 199 | Thread::kStackOverflowProtectedSize; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 200 | |
| 201 | // Check that the fault address is the value expected for a stack overflow. |
| 202 | if (fault_addr != overflow_addr) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 203 | VLOG(signals) << "Not a stack overflow"; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | |
| 207 | // We know this is a stack overflow. We need to move the sp to the overflow region |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 208 | // the exists below the protected region. Determine the address of the next |
| 209 | // available valid address below the protected region. |
| 210 | uintptr_t prevsp = sp; |
| 211 | sp = pregion; |
| 212 | VLOG(signals) << "setting sp to overflow region at " << std::hex << sp; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 213 | |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 214 | // Since the compiler puts the implicit overflow |
| 215 | // check before the callee save instructions, the SP is already pointing to |
| 216 | // the previous frame. |
| 217 | VLOG(signals) << "previous frame: " << std::hex << prevsp; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 218 | |
| 219 | // Now establish the stack pointer for the signal return. |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 220 | sc->arm_sp = prevsp; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 221 | |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 222 | // Tell the stack overflow code where the new stack pointer should be. |
| 223 | sc->arm_ip = sp; // aka r12 |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 224 | |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 225 | // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from_signal. |
| 226 | // The value of LR must be the same as it was when we entered the code that |
| 227 | // caused this fault. This will be inserted into a callee save frame by |
| 228 | // the function to which this handler returns (art_quick_throw_stack_overflow_from_signal). |
| 229 | sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow_from_signal); |
| 230 | |
| 231 | // The kernel will now return to the address in sc->arm_pc. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 232 | return true; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 233 | } |
| 234 | } // namespace art |