blob: 3e9ad0da62e28465320d57eba793f3b4215aa8d4 [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070019
Stuart Monteithb95a5342014-03-12 13:32:32 +000020#include <sys/ucontext.h>
Mathieu Chartiere401d142015-04-22 13:56:20 -070021
22#include "art_method-inl.h"
Stuart Monteithb95a5342014-03-12 13:32:32 +000023#include "base/macros.h"
24#include "globals.h"
25#include "base/logging.h"
26#include "base/hex_dump.h"
Stuart Monteithd5c78f42014-06-11 16:44:46 +010027#include "registers_arm64.h"
Stuart Monteithd5c78f42014-06-11 16:44:46 +010028#include "thread.h"
29#include "thread-inl.h"
Stuart Monteithb95a5342014-03-12 13:32:32 +000030
Dave Allison648d7112014-07-25 16:15:27 -070031extern "C" void art_quick_throw_stack_overflow();
Stuart Monteithd5c78f42014-06-11 16:44:46 +010032extern "C" void art_quick_throw_null_pointer_exception();
33extern "C" void art_quick_implicit_suspend();
Stuart Monteithb95a5342014-03-12 13:32:32 +000034
35//
36// ARM64 specific fault handler functions.
37//
38
39namespace art {
40
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -080041void FaultManager::HandleNestedSignal(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
42 void* context) {
Dave Allison8ce6b902014-08-26 11:07:58 -070043 // To match the case used in ARM we return directly to the longjmp function
44 // rather than through a trivial assembly language stub.
45
46 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
47 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
48 Thread* self = Thread::Current();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070049 CHECK(self != nullptr); // This will cause a SIGABRT if self is null.
Dave Allison8ce6b902014-08-26 11:07:58 -070050
51 sc->regs[0] = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState());
52 sc->regs[1] = 1;
53 sc->pc = reinterpret_cast<uintptr_t>(longjmp);
54}
55
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -080056void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo ATTRIBUTE_UNUSED, void* context,
Mathieu Chartiere401d142015-04-22 13:56:20 -070057 ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070058 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +010059 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
60 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
61 *out_sp = static_cast<uintptr_t>(sc->sp);
62 VLOG(signals) << "sp: " << *out_sp;
63 if (*out_sp == 0) {
64 return;
65 }
66
67 // In the case of a stack overflow, the stack is not valid and we can't
68 // get the method from the top of the stack. However it's in x0.
69 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address);
70 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
71 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kArm64));
72 if (overflow_addr == fault_addr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 *out_method = reinterpret_cast<ArtMethod*>(sc->regs[0]);
Stuart Monteithd5c78f42014-06-11 16:44:46 +010074 } else {
75 // The method is at the top of the stack.
Mathieu Chartiere401d142015-04-22 13:56:20 -070076 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
Stuart Monteithd5c78f42014-06-11 16:44:46 +010077 }
78
79 // Work out the return PC. This will be the address of the instruction
80 // following the faulting ldr/str instruction.
81 VLOG(signals) << "pc: " << std::hex
82 << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->pc));
83
84 *out_return_pc = sc->pc + 4;
Stuart Monteithb95a5342014-03-12 13:32:32 +000085}
86
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -080087bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
88 void* context) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +010089 // The code that looks for the catch location needs to know the value of the
90 // PC at the point of call. For Null checks we insert a GC map that is immediately after
91 // the load/store instruction that might cause the fault.
92
93 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
94 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
95
96 sc->regs[30] = sc->pc + 4; // LR needs to point to gc map location
97
98 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception);
99 VLOG(signals) << "Generating null pointer exception";
100 return true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000101}
102
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100103// A suspend check is done using the following instruction sequence:
104// 0xf7223228: f9405640 ldr x0, [x18, #168]
105// .. some intervening instructions
106// 0xf7223230: f9400000 ldr x0, [x0]
107
108// The offset from r18 is Thread::ThreadSuspendTriggerOffset().
109// To check for a suspend check, we examine the instructions that caused
110// the fault (at PC-4 and PC).
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -0800111bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
112 void* context) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100113 // These are the instructions to check for. The first one is the ldr x0,[r18,#xxx]
114 // where xxx is the offset of the suspend trigger.
115 uint32_t checkinst1 = 0xf9400240 | (Thread::ThreadSuspendTriggerOffset<8>().Int32Value() << 7);
116 uint32_t checkinst2 = 0xf9400000;
117
118 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
119 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
120 uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->pc);
121 uint8_t* ptr1 = ptr2 - 4;
122 VLOG(signals) << "checking suspend";
123
124 uint32_t inst2 = *reinterpret_cast<uint32_t*>(ptr2);
125 VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2;
126 if (inst2 != checkinst2) {
127 // Second instruction is not good, not ours.
128 return false;
129 }
130
131 // The first instruction can a little bit up the stream due to load hoisting
132 // in the compiler.
133 uint8_t* limit = ptr1 - 80; // Compiler will hoist to a max of 20 instructions.
134 bool found = false;
135 while (ptr1 > limit) {
136 uint32_t inst1 = *reinterpret_cast<uint32_t*>(ptr1);
137 VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1;
138 if (inst1 == checkinst1) {
139 found = true;
140 break;
141 }
142 ptr1 -= 4;
143 }
144 if (found) {
145 VLOG(signals) << "suspend check match";
146 // This is a suspend check. Arrange for the signal handler to return to
147 // art_quick_implicit_suspend. Also set LR so that after the suspend check it
148 // will resume the instruction (current PC + 4). PC points to the
149 // ldr x0,[x0,#0] instruction (r0 will be 0, set by the trigger).
150
151 sc->regs[30] = sc->pc + 4;
152 sc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend);
153
154 // Now remove the suspend trigger that caused this fault.
155 Thread::Current()->RemoveSuspendTrigger();
156 VLOG(signals) << "removed suspend trigger invoking test suspend";
157 return true;
158 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000159 return false;
160}
161
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -0800162bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
163 void* context) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100164 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
165 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
166 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
167 VLOG(signals) << "sigcontext: " << std::hex << sc;
168
169 uintptr_t sp = sc->sp;
170 VLOG(signals) << "sp: " << std::hex << sp;
171
172 uintptr_t fault_addr = sc->fault_address;
173 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
174 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
175 ", fault_addr: " << fault_addr;
176
177 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kArm64);
178
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100179 // Check that the fault address is the value expected for a stack overflow.
180 if (fault_addr != overflow_addr) {
181 VLOG(signals) << "Not a stack overflow";
182 return false;
183 }
184
Dave Allison648d7112014-07-25 16:15:27 -0700185 VLOG(signals) << "Stack overflow found";
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100186
Dave Allison648d7112014-07-25 16:15:27 -0700187 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100188 // The value of LR must be the same as it was when we entered the code that
189 // caused this fault. This will be inserted into a callee save frame by
Dave Allison648d7112014-07-25 16:15:27 -0700190 // the function to which this handler returns (art_quick_throw_stack_overflow).
191 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100192
193 // The kernel will now return to the address in sc->pc.
194 return true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000195}
196} // namespace art
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100197