fault_handler_arm64.cc revision fabe91e0d558936ac26b98d2b4ee1af08f58831d
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" 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" 29 30extern "C" void art_quick_throw_stack_overflow(); 31extern "C" void art_quick_throw_null_pointer_exception(); 32extern "C" void art_quick_implicit_suspend(); 33 34// 35// ARM64 specific fault handler functions. 36// 37 38namespace art { 39 40void FaultManager::HandleNestedSignal(int sig, siginfo_t* info, void* context) { 41 // To match the case used in ARM we return directly to the longjmp function 42 // rather than through a trivial assembly language stub. 43 44 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); 45 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); 46 Thread* self = Thread::Current(); 47 CHECK(self != nullptr); // This will cause a SIGABRT if self is nullptr. 48 49 sc->regs[0] = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState()); 50 sc->regs[1] = 1; 51 sc->pc = reinterpret_cast<uintptr_t>(longjmp); 52} 53 54void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, 55 mirror::ArtMethod** out_method, 56 uintptr_t* out_return_pc, uintptr_t* out_sp) { 57 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context); 58 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); 59 *out_sp = static_cast<uintptr_t>(sc->sp); 60 VLOG(signals) << "sp: " << *out_sp; 61 if (*out_sp == 0) { 62 return; 63 } 64 65 // In the case of a stack overflow, the stack is not valid and we can't 66 // get the method from the top of the stack. However it's in x0. 67 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address); 68 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>( 69 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kArm64)); 70 if (overflow_addr == fault_addr) { 71 *out_method = reinterpret_cast<mirror::ArtMethod*>(sc->regs[0]); 72 } else { 73 // The method is at the top of the stack. 74 *out_method = (reinterpret_cast<StackReference<mirror::ArtMethod>* >(*out_sp)[0]).AsMirrorPtr(); 75 } 76 77 // Work out the return PC. This will be the address of the instruction 78 // following the faulting ldr/str instruction. 79 VLOG(signals) << "pc: " << std::hex 80 << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->pc)); 81 82 *out_return_pc = sc->pc + 4; 83} 84 85bool NullPointerHandler::Action(int sig, siginfo_t* info, void* context) { 86 // The code that looks for the catch location needs to know the value of the 87 // PC at the point of call. For Null checks we insert a GC map that is immediately after 88 // the load/store instruction that might cause the fault. 89 90 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); 91 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); 92 93 sc->regs[30] = sc->pc + 4; // LR needs to point to gc map location 94 95 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception); 96 VLOG(signals) << "Generating null pointer exception"; 97 return true; 98} 99 100// A suspend check is done using the following instruction sequence: 101// 0xf7223228: f9405640 ldr x0, [x18, #168] 102// .. some intervening instructions 103// 0xf7223230: f9400000 ldr x0, [x0] 104 105// The offset from r18 is Thread::ThreadSuspendTriggerOffset(). 106// To check for a suspend check, we examine the instructions that caused 107// the fault (at PC-4 and PC). 108bool SuspensionHandler::Action(int sig, siginfo_t* info, void* context) { 109 // These are the instructions to check for. The first one is the ldr x0,[r18,#xxx] 110 // where xxx is the offset of the suspend trigger. 111 uint32_t checkinst1 = 0xf9400240 | (Thread::ThreadSuspendTriggerOffset<8>().Int32Value() << 7); 112 uint32_t checkinst2 = 0xf9400000; 113 114 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context); 115 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); 116 uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->pc); 117 uint8_t* ptr1 = ptr2 - 4; 118 VLOG(signals) << "checking suspend"; 119 120 uint32_t inst2 = *reinterpret_cast<uint32_t*>(ptr2); 121 VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2; 122 if (inst2 != checkinst2) { 123 // Second instruction is not good, not ours. 124 return false; 125 } 126 127 // The first instruction can a little bit up the stream due to load hoisting 128 // in the compiler. 129 uint8_t* limit = ptr1 - 80; // Compiler will hoist to a max of 20 instructions. 130 bool found = false; 131 while (ptr1 > limit) { 132 uint32_t inst1 = *reinterpret_cast<uint32_t*>(ptr1); 133 VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1; 134 if (inst1 == checkinst1) { 135 found = true; 136 break; 137 } 138 ptr1 -= 4; 139 } 140 if (found) { 141 VLOG(signals) << "suspend check match"; 142 // This is a suspend check. Arrange for the signal handler to return to 143 // art_quick_implicit_suspend. Also set LR so that after the suspend check it 144 // will resume the instruction (current PC + 4). PC points to the 145 // ldr x0,[x0,#0] instruction (r0 will be 0, set by the trigger). 146 147 sc->regs[30] = sc->pc + 4; 148 sc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend); 149 150 // Now remove the suspend trigger that caused this fault. 151 Thread::Current()->RemoveSuspendTrigger(); 152 VLOG(signals) << "removed suspend trigger invoking test suspend"; 153 return true; 154 } 155 return false; 156} 157 158bool StackOverflowHandler::Action(int sig, siginfo_t* info, void* context) { 159 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context); 160 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); 161 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc; 162 VLOG(signals) << "sigcontext: " << std::hex << sc; 163 164 uintptr_t sp = sc->sp; 165 VLOG(signals) << "sp: " << std::hex << sp; 166 167 uintptr_t fault_addr = sc->fault_address; 168 VLOG(signals) << "fault_addr: " << std::hex << fault_addr; 169 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp << 170 ", fault_addr: " << fault_addr; 171 172 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kArm64); 173 174 // Check that the fault address is the value expected for a stack overflow. 175 if (fault_addr != overflow_addr) { 176 VLOG(signals) << "Not a stack overflow"; 177 return false; 178 } 179 180 VLOG(signals) << "Stack overflow found"; 181 182 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow. 183 // The value of LR must be the same as it was when we entered the code that 184 // caused this fault. This will be inserted into a callee save frame by 185 // the function to which this handler returns (art_quick_throw_stack_overflow). 186 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow); 187 188 // The kernel will now return to the address in sc->pc. 189 return true; 190} 191} // namespace art 192 193