blob: e22c56ec693aff298d194de5ccd11181f36d0fd8 [file] [log] [blame]
Dave Allisonb373e092014-02-20 16:06:36 -08001/*
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 Allisonf9439142014-03-27 15:10:22 -070021#include "base/hex_dump.h"
Dave Allisonb373e092014-02-20 16:06:36 -080022#include "globals.h"
23#include "base/logging.h"
24#include "base/hex_dump.h"
Andreas Gampe7cd26f32014-06-18 17:01:15 -070025#include "instruction_set.h"
Dave Allisonf9439142014-03-27 15:10:22 -070026#include "mirror/art_method.h"
27#include "mirror/art_method-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080028#include "thread.h"
29#include "thread-inl.h"
30
31//
32// ARM specific fault handler functions.
33//
34
35namespace art {
36
37extern "C" void art_quick_throw_null_pointer_exception();
Dave Allison5cd33752014-04-15 15:57:58 -070038extern "C" void art_quick_throw_stack_overflow_from_signal();
Dave Allison83252962014-04-03 16:33:48 -070039extern "C" void art_quick_implicit_suspend();
Dave Allisonb373e092014-02-20 16:06:36 -080040
Dave Allisonf9439142014-03-27 15:10:22 -070041// Get the size of a thumb2 instruction in bytes.
42static 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
Dave Allison69dfe512014-07-11 17:11:58 +000049void FaultManager::GetMethodAndReturnPCAndSP(siginfo_t* siginfo, void* context,
50 mirror::ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070051 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Dave Allison69dfe512014-07-11 17:11:58 +000052 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
Dave Allisonb373e092014-02-20 16:06:36 -080053 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070054 *out_sp = static_cast<uintptr_t>(sc->arm_sp);
Dave Allison5cd33752014-04-15 15:57:58 -070055 VLOG(signals) << "sp: " << *out_sp;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070056 if (*out_sp == 0) {
Dave Allisonb373e092014-02-20 16:06:36 -080057 return;
58 }
59
Dave Allisonf9439142014-03-27 15:10:22 -070060 // In the case of a stack overflow, the stack is not valid and we can't
61 // get the method from the top of the stack. However it's in r0.
62 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address);
63 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
Andreas Gampe7cd26f32014-06-18 17:01:15 -070064 reinterpret_cast<uint8_t*>(*out_sp) - kArmStackOverflowReservedBytes);
Dave Allisonf9439142014-03-27 15:10:22 -070065 if (overflow_addr == fault_addr) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070066 *out_method = reinterpret_cast<mirror::ArtMethod*>(sc->arm_r0);
Dave Allisonf9439142014-03-27 15:10:22 -070067 } else {
68 // The method is at the top of the stack.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070069 *out_method = reinterpret_cast<mirror::ArtMethod*>(reinterpret_cast<uintptr_t*>(*out_sp)[0]);
Dave Allisonf9439142014-03-27 15:10:22 -070070 }
71
Dave Allisonb373e092014-02-20 16:06:36 -080072 // Work out the return PC. This will be the address of the instruction
73 // following the faulting ldr/str instruction. This is in thumb mode so
74 // the instruction might be a 16 or 32 bit one. Also, the GC map always
75 // has the bottom bit of the PC set so we also need to set that.
76
77 // Need to work out the size of the instruction that caused the exception.
78 uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc);
Dave Allison5cd33752014-04-15 15:57:58 -070079 VLOG(signals) << "pc: " << std::hex << static_cast<void*>(ptr);
Dave Allisonf9439142014-03-27 15:10:22 -070080 uint32_t instr_size = GetInstructionSize(ptr);
Dave Allisonb373e092014-02-20 16:06:36 -080081
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070082 *out_return_pc = (sc->arm_pc + instr_size) | 1;
Dave Allisonb373e092014-02-20 16:06:36 -080083}
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 // ARM 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. However the mapping table has
89 // the low bits set for thumb mode so we need to set the bottom bit for the LR
90 // register in order to find the mapping.
91
92 // Need to work out the size of the instruction that caused the exception.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070093 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
Dave Allisonb373e092014-02-20 16:06:36 -080094 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
95 uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc);
96
Dave Allisonf9439142014-03-27 15:10:22 -070097 uint32_t instr_size = GetInstructionSize(ptr);
Dave Allisonb373e092014-02-20 16:06:36 -080098 sc->arm_lr = (sc->arm_pc + instr_size) | 1; // LR needs to point to gc map location
99 sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception);
Dave Allison5cd33752014-04-15 15:57:58 -0700100 VLOG(signals) << "Generating null pointer exception";
Dave Allisonb373e092014-02-20 16:06:36 -0800101 return true;
102}
103
104// A suspend check is done using the following instruction sequence:
105// 0xf723c0b2: f8d902c0 ldr.w r0, [r9, #704] ; suspend_trigger_
106// .. some intervening instruction
107// 0xf723c0b6: 6800 ldr r0, [r0, #0]
108
109// The offset from r9 is Thread::ThreadSuspendTriggerOffset().
110// To check for a suspend check, we examine the instructions that caused
111// the fault (at PC-4 and PC).
112bool SuspensionHandler::Action(int sig, siginfo_t* info, void* context) {
113 // These are the instructions to check for. The first one is the ldr r0,[r9,#xxx]
114 // where xxx is the offset of the suspend trigger.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700115 uint32_t checkinst1 = 0xf8d90000 + Thread::ThreadSuspendTriggerOffset<4>().Int32Value();
Dave Allisonb373e092014-02-20 16:06:36 -0800116 uint16_t checkinst2 = 0x6800;
117
Dave Allison69dfe512014-07-11 17:11:58 +0000118 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
Dave Allisonb373e092014-02-20 16:06:36 -0800119 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
120 uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->arm_pc);
121 uint8_t* ptr1 = ptr2 - 4;
Dave Allison5cd33752014-04-15 15:57:58 -0700122 VLOG(signals) << "checking suspend";
Dave Allisonb373e092014-02-20 16:06:36 -0800123
124 uint16_t inst2 = ptr2[0] | ptr2[1] << 8;
Dave Allison5cd33752014-04-15 15:57:58 -0700125 VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2;
Dave Allisonb373e092014-02-20 16:06:36 -0800126 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 - 40; // Compiler will hoist to a max of 20 instructions.
134 bool found = false;
135 while (ptr1 > limit) {
136 uint32_t inst1 = ((ptr1[0] | ptr1[1] << 8) << 16) | (ptr1[2] | ptr1[3] << 8);
Dave Allison5cd33752014-04-15 15:57:58 -0700137 VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1;
Dave Allisonb373e092014-02-20 16:06:36 -0800138 if (inst1 == checkinst1) {
139 found = true;
140 break;
141 }
142 ptr1 -= 2; // Min instruction size is 2 bytes.
143 }
144 if (found) {
Dave Allison5cd33752014-04-15 15:57:58 -0700145 VLOG(signals) << "suspend check match";
Dave Allisonb373e092014-02-20 16:06:36 -0800146 // This is a suspend check. Arrange for the signal handler to return to
Dave Allison83252962014-04-03 16:33:48 -0700147 // art_quick_implicit_suspend. Also set LR so that after the suspend check it
Dave Allisonb373e092014-02-20 16:06:36 -0800148 // will resume the instruction (current PC + 2). PC points to the
149 // ldr r0,[r0,#0] instruction (r0 will be 0, set by the trigger).
150
151 // NB: remember that we need to set the bottom bit of the LR register
152 // to switch to thumb mode.
Dave Allison5cd33752014-04-15 15:57:58 -0700153 VLOG(signals) << "arm lr: " << std::hex << sc->arm_lr;
154 VLOG(signals) << "arm pc: " << std::hex << sc->arm_pc;
Dave Allisonb373e092014-02-20 16:06:36 -0800155 sc->arm_lr = sc->arm_pc + 3; // +2 + 1 (for thumb)
Dave Allison83252962014-04-03 16:33:48 -0700156 sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend);
Dave Allisonb373e092014-02-20 16:06:36 -0800157
158 // Now remove the suspend trigger that caused this fault.
Dave Allisond6ed6422014-04-09 23:36:15 +0000159 Thread::Current()->RemoveSuspendTrigger();
Dave Allison5cd33752014-04-15 15:57:58 -0700160 VLOG(signals) << "removed suspend trigger invoking test suspend";
Dave Allisonb373e092014-02-20 16:06:36 -0800161 return true;
162 }
163 return false;
164}
165
Dave Allisonf9439142014-03-27 15:10:22 -0700166// Stack overflow fault handler.
167//
168// This checks that the fault address is equal to the current stack pointer
169// minus the overflow region size (16K typically). The instruction sequence
170// that generates this signal is:
171//
172// sub r12,sp,#16384
173// ldr.w r12,[r12,#0]
174//
175// The second instruction will fault if r12 is inside the protected region
176// on the stack.
177//
178// If we determine this is a stack overflow we need to move the stack pointer
Dave Allison5cd33752014-04-15 15:57:58 -0700179// to the overflow region below the protected region.
Dave Allisonf9439142014-03-27 15:10:22 -0700180
Dave Allisonb373e092014-02-20 16:06:36 -0800181bool StackOverflowHandler::Action(int sig, siginfo_t* info, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000182 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
Dave Allisonf9439142014-03-27 15:10:22 -0700183 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
Dave Allison5cd33752014-04-15 15:57:58 -0700184 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
185 VLOG(signals) << "sigcontext: " << std::hex << sc;
Dave Allisonf9439142014-03-27 15:10:22 -0700186
Dave Allison5cd33752014-04-15 15:57:58 -0700187 uintptr_t sp = sc->arm_sp;
188 VLOG(signals) << "sp: " << std::hex << sp;
Dave Allisonf9439142014-03-27 15:10:22 -0700189
Dave Allison5cd33752014-04-15 15:57:58 -0700190 uintptr_t fault_addr = sc->fault_address;
191 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
192 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
Dave Allisonf9439142014-03-27 15:10:22 -0700193 ", fault_addr: " << fault_addr;
Dave Allison5cd33752014-04-15 15:57:58 -0700194
Andreas Gampe7cd26f32014-06-18 17:01:15 -0700195 uintptr_t overflow_addr = sp - kArmStackOverflowReservedBytes;
Dave Allison5cd33752014-04-15 15:57:58 -0700196
197 Thread* self = reinterpret_cast<Thread*>(sc->arm_r9);
198 CHECK_EQ(self, Thread::Current());
199 uintptr_t pregion = reinterpret_cast<uintptr_t>(self->GetStackEnd()) -
200 Thread::kStackOverflowProtectedSize;
Dave Allisonf9439142014-03-27 15:10:22 -0700201
202 // Check that the fault address is the value expected for a stack overflow.
203 if (fault_addr != overflow_addr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700204 VLOG(signals) << "Not a stack overflow";
Dave Allisonf9439142014-03-27 15:10:22 -0700205 return false;
206 }
207
208 // We know this is a stack overflow. We need to move the sp to the overflow region
Dave Allison69dfe512014-07-11 17:11:58 +0000209 // that exists below the protected region. Determine the address of the next
Dave Allison5cd33752014-04-15 15:57:58 -0700210 // available valid address below the protected region.
211 uintptr_t prevsp = sp;
212 sp = pregion;
213 VLOG(signals) << "setting sp to overflow region at " << std::hex << sp;
Dave Allisonf9439142014-03-27 15:10:22 -0700214
Dave Allison5cd33752014-04-15 15:57:58 -0700215 // Since the compiler puts the implicit overflow
216 // check before the callee save instructions, the SP is already pointing to
217 // the previous frame.
218 VLOG(signals) << "previous frame: " << std::hex << prevsp;
Dave Allisonf9439142014-03-27 15:10:22 -0700219
220 // Now establish the stack pointer for the signal return.
Dave Allison5cd33752014-04-15 15:57:58 -0700221 sc->arm_sp = prevsp;
Dave Allisonf9439142014-03-27 15:10:22 -0700222
Dave Allison5cd33752014-04-15 15:57:58 -0700223 // Tell the stack overflow code where the new stack pointer should be.
224 sc->arm_ip = sp; // aka r12
Dave Allisonf9439142014-03-27 15:10:22 -0700225
Dave Allison5cd33752014-04-15 15:57:58 -0700226 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from_signal.
227 // The value of LR must be the same as it was when we entered the code that
228 // caused this fault. This will be inserted into a callee save frame by
229 // the function to which this handler returns (art_quick_throw_stack_overflow_from_signal).
230 sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow_from_signal);
231
232 // The kernel will now return to the address in sc->arm_pc.
Dave Allisonf9439142014-03-27 15:10:22 -0700233 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800234}
235} // namespace art