blob: b5948cbc9eb7269d0b172e2d0f191c1e527003ff [file] [log] [blame]
Stuart Monteithb95a5342014-03-12 13:32:32 +00001/*
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 Monteithd5c78f42014-06-11 16:44:46 +010024#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 Monteithb95a5342014-03-12 13:32:32 +000029
Dave Allison648d7112014-07-25 16:15:27 -070030extern "C" void art_quick_throw_stack_overflow();
Stuart Monteithd5c78f42014-06-11 16:44:46 +010031extern "C" void art_quick_throw_null_pointer_exception();
32extern "C" void art_quick_implicit_suspend();
Stuart Monteithb95a5342014-03-12 13:32:32 +000033
34//
35// ARM64 specific fault handler functions.
36//
37
38namespace art {
39
Dave Allisondfd3b472014-07-16 16:04:32 -070040void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
Dave Allison69dfe512014-07-11 17:11:58 +000041 mirror::ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070042 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +010043 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
44 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
45 *out_sp = static_cast<uintptr_t>(sc->sp);
46 VLOG(signals) << "sp: " << *out_sp;
47 if (*out_sp == 0) {
48 return;
49 }
50
51 // In the case of a stack overflow, the stack is not valid and we can't
52 // get the method from the top of the stack. However it's in x0.
53 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address);
54 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
55 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kArm64));
56 if (overflow_addr == fault_addr) {
57 *out_method = reinterpret_cast<mirror::ArtMethod*>(sc->regs[0]);
58 } else {
59 // The method is at the top of the stack.
60 *out_method = (reinterpret_cast<StackReference<mirror::ArtMethod>* >(*out_sp)[0]).AsMirrorPtr();
61 }
62
63 // Work out the return PC. This will be the address of the instruction
64 // following the faulting ldr/str instruction.
65 VLOG(signals) << "pc: " << std::hex
66 << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->pc));
67
68 *out_return_pc = sc->pc + 4;
Stuart Monteithb95a5342014-03-12 13:32:32 +000069}
70
71bool NullPointerHandler::Action(int sig, siginfo_t* info, void* context) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +010072 // The code that looks for the catch location needs to know the value of the
73 // PC at the point of call. For Null checks we insert a GC map that is immediately after
74 // the load/store instruction that might cause the fault.
75
76 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
77 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
78
79 sc->regs[30] = sc->pc + 4; // LR needs to point to gc map location
80
81 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception);
82 VLOG(signals) << "Generating null pointer exception";
83 return true;
Stuart Monteithb95a5342014-03-12 13:32:32 +000084}
85
Stuart Monteithd5c78f42014-06-11 16:44:46 +010086// A suspend check is done using the following instruction sequence:
87// 0xf7223228: f9405640 ldr x0, [x18, #168]
88// .. some intervening instructions
89// 0xf7223230: f9400000 ldr x0, [x0]
90
91// The offset from r18 is Thread::ThreadSuspendTriggerOffset().
92// To check for a suspend check, we examine the instructions that caused
93// the fault (at PC-4 and PC).
Stuart Monteithb95a5342014-03-12 13:32:32 +000094bool SuspensionHandler::Action(int sig, siginfo_t* info, void* context) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +010095 // These are the instructions to check for. The first one is the ldr x0,[r18,#xxx]
96 // where xxx is the offset of the suspend trigger.
97 uint32_t checkinst1 = 0xf9400240 | (Thread::ThreadSuspendTriggerOffset<8>().Int32Value() << 7);
98 uint32_t checkinst2 = 0xf9400000;
99
100 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
101 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
102 uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->pc);
103 uint8_t* ptr1 = ptr2 - 4;
104 VLOG(signals) << "checking suspend";
105
106 uint32_t inst2 = *reinterpret_cast<uint32_t*>(ptr2);
107 VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2;
108 if (inst2 != checkinst2) {
109 // Second instruction is not good, not ours.
110 return false;
111 }
112
113 // The first instruction can a little bit up the stream due to load hoisting
114 // in the compiler.
115 uint8_t* limit = ptr1 - 80; // Compiler will hoist to a max of 20 instructions.
116 bool found = false;
117 while (ptr1 > limit) {
118 uint32_t inst1 = *reinterpret_cast<uint32_t*>(ptr1);
119 VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1;
120 if (inst1 == checkinst1) {
121 found = true;
122 break;
123 }
124 ptr1 -= 4;
125 }
126 if (found) {
127 VLOG(signals) << "suspend check match";
128 // This is a suspend check. Arrange for the signal handler to return to
129 // art_quick_implicit_suspend. Also set LR so that after the suspend check it
130 // will resume the instruction (current PC + 4). PC points to the
131 // ldr x0,[x0,#0] instruction (r0 will be 0, set by the trigger).
132
133 sc->regs[30] = sc->pc + 4;
134 sc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend);
135
136 // Now remove the suspend trigger that caused this fault.
137 Thread::Current()->RemoveSuspendTrigger();
138 VLOG(signals) << "removed suspend trigger invoking test suspend";
139 return true;
140 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000141 return false;
142}
143
144bool StackOverflowHandler::Action(int sig, siginfo_t* info, void* context) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100145 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
146 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
147 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
148 VLOG(signals) << "sigcontext: " << std::hex << sc;
149
150 uintptr_t sp = sc->sp;
151 VLOG(signals) << "sp: " << std::hex << sp;
152
153 uintptr_t fault_addr = sc->fault_address;
154 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
155 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
156 ", fault_addr: " << fault_addr;
157
158 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kArm64);
159
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100160 // Check that the fault address is the value expected for a stack overflow.
161 if (fault_addr != overflow_addr) {
162 VLOG(signals) << "Not a stack overflow";
163 return false;
164 }
165
Dave Allison648d7112014-07-25 16:15:27 -0700166 VLOG(signals) << "Stack overflow found";
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100167
Dave Allison648d7112014-07-25 16:15:27 -0700168 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100169 // The value of LR must be the same as it was when we entered the code that
170 // caused this fault. This will be inserted into a callee save frame by
Dave Allison648d7112014-07-25 16:15:27 -0700171 // the function to which this handler returns (art_quick_throw_stack_overflow).
172 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100173
174 // The kernel will now return to the address in sc->pc.
175 return true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000176}
177} // namespace art
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100178