blob: abce838e4c05908bc05d1c57d6a82545c0796c91 [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"
Dave Allisonf9439142014-03-27 15:10:22 -070025#include "mirror/art_method.h"
26#include "mirror/art_method-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080027#include "thread.h"
28#include "thread-inl.h"
29
30//
31// ARM specific fault handler functions.
32//
33
34namespace art {
35
36extern "C" void art_quick_throw_null_pointer_exception();
Dave Allisonf9439142014-03-27 15:10:22 -070037extern "C" void art_quick_throw_stack_overflow(void*);
Dave Allisonb373e092014-02-20 16:06:36 -080038extern "C" void art_quick_test_suspend();
39
Dave Allisonf9439142014-03-27 15:10:22 -070040// Get the size of a thumb2 instruction in bytes.
41static uint32_t GetInstructionSize(uint8_t* pc) {
42 uint16_t instr = pc[0] | pc[1] << 8;
43 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
44 uint32_t instr_size = is_32bit ? 4 : 2;
45 return instr_size;
46}
47
Dave Allisonb373e092014-02-20 16:06:36 -080048void FaultManager::GetMethodAndReturnPC(void* context, uintptr_t& method, uintptr_t& return_pc) {
49 struct ucontext *uc = (struct ucontext *)context;
50 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
51 uintptr_t* sp = reinterpret_cast<uint32_t*>(sc->arm_sp);
Dave Allisonf9439142014-03-27 15:10:22 -070052 LOG(DEBUG) << "sp: " << sp;
Dave Allisonb373e092014-02-20 16:06:36 -080053 if (sp == nullptr) {
54 return;
55 }
56
Dave Allisonf9439142014-03-27 15:10:22 -070057 // In the case of a stack overflow, the stack is not valid and we can't
58 // get the method from the top of the stack. However it's in r0.
59 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address);
60 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
61 reinterpret_cast<uint8_t*>(sp) - Thread::kStackOverflowReservedBytes);
62 if (overflow_addr == fault_addr) {
63 method = sc->arm_r0;
64 } else {
65 // The method is at the top of the stack.
66 method = sp[0];
67 }
68
Dave Allisonb373e092014-02-20 16:06:36 -080069 // Work out the return PC. This will be the address of the instruction
70 // following the faulting ldr/str instruction. This is in thumb mode so
71 // the instruction might be a 16 or 32 bit one. Also, the GC map always
72 // has the bottom bit of the PC set so we also need to set that.
73
74 // Need to work out the size of the instruction that caused the exception.
75 uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc);
Dave Allisonf9439142014-03-27 15:10:22 -070076 LOG(DEBUG) << "pc: " << std::hex << static_cast<void*>(ptr);
77 uint32_t instr_size = GetInstructionSize(ptr);
Dave Allisonb373e092014-02-20 16:06:36 -080078
79 return_pc = (sc->arm_pc + instr_size) | 1;
80}
81
82bool NullPointerHandler::Action(int sig, siginfo_t* info, void* context) {
83 // The code that looks for the catch location needs to know the value of the
84 // ARM PC at the point of call. For Null checks we insert a GC map that is immediately after
85 // the load/store instruction that might cause the fault. However the mapping table has
86 // the low bits set for thumb mode so we need to set the bottom bit for the LR
87 // register in order to find the mapping.
88
89 // Need to work out the size of the instruction that caused the exception.
90 struct ucontext *uc = (struct ucontext *)context;
91 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
92 uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc);
93
Dave Allisonf9439142014-03-27 15:10:22 -070094 uint32_t instr_size = GetInstructionSize(ptr);
Dave Allisonb373e092014-02-20 16:06:36 -080095 sc->arm_lr = (sc->arm_pc + instr_size) | 1; // LR needs to point to gc map location
96 sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception);
97 LOG(DEBUG) << "Generating null pointer exception";
98 return true;
99}
100
101// A suspend check is done using the following instruction sequence:
102// 0xf723c0b2: f8d902c0 ldr.w r0, [r9, #704] ; suspend_trigger_
103// .. some intervening instruction
104// 0xf723c0b6: 6800 ldr r0, [r0, #0]
105
106// The offset from r9 is Thread::ThreadSuspendTriggerOffset().
107// To check for a suspend check, we examine the instructions that caused
108// the fault (at PC-4 and PC).
109bool SuspensionHandler::Action(int sig, siginfo_t* info, void* context) {
110 // These are the instructions to check for. The first one is the ldr r0,[r9,#xxx]
111 // where xxx is the offset of the suspend trigger.
112 uint32_t checkinst1 = 0xf8d90000 + Thread::ThreadSuspendTriggerOffset().Int32Value();
113 uint16_t checkinst2 = 0x6800;
114
115 struct ucontext *uc = (struct ucontext *)context;
116 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
117 uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->arm_pc);
118 uint8_t* ptr1 = ptr2 - 4;
119 LOG(DEBUG) << "checking suspend";
120
121 uint16_t inst2 = ptr2[0] | ptr2[1] << 8;
122 LOG(DEBUG) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2;
123 if (inst2 != checkinst2) {
124 // Second instruction is not good, not ours.
125 return false;
126 }
127
128 // The first instruction can a little bit up the stream due to load hoisting
129 // in the compiler.
130 uint8_t* limit = ptr1 - 40; // Compiler will hoist to a max of 20 instructions.
131 bool found = false;
132 while (ptr1 > limit) {
133 uint32_t inst1 = ((ptr1[0] | ptr1[1] << 8) << 16) | (ptr1[2] | ptr1[3] << 8);
134 LOG(DEBUG) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1;
135 if (inst1 == checkinst1) {
136 found = true;
137 break;
138 }
139 ptr1 -= 2; // Min instruction size is 2 bytes.
140 }
141 if (found) {
142 LOG(DEBUG) << "suspend check match";
143 // This is a suspend check. Arrange for the signal handler to return to
144 // art_quick_test_suspend. Also set LR so that after the suspend check it
145 // will resume the instruction (current PC + 2). PC points to the
146 // ldr r0,[r0,#0] instruction (r0 will be 0, set by the trigger).
147
148 // NB: remember that we need to set the bottom bit of the LR register
149 // to switch to thumb mode.
150 LOG(DEBUG) << "arm lr: " << std::hex << sc->arm_lr;
151 LOG(DEBUG) << "arm pc: " << std::hex << sc->arm_pc;
152 sc->arm_lr = sc->arm_pc + 3; // +2 + 1 (for thumb)
153 sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_test_suspend);
154
155 // Now remove the suspend trigger that caused this fault.
156 Thread::Current()->RemoveSuspendTrigger();
157 LOG(DEBUG) << "removed suspend trigger invoking test suspend";
158 return true;
159 }
160 return false;
161}
162
Dave Allisonf9439142014-03-27 15:10:22 -0700163// Stack overflow fault handler.
164//
165// This checks that the fault address is equal to the current stack pointer
166// minus the overflow region size (16K typically). The instruction sequence
167// that generates this signal is:
168//
169// sub r12,sp,#16384
170// ldr.w r12,[r12,#0]
171//
172// The second instruction will fault if r12 is inside the protected region
173// on the stack.
174//
175// If we determine this is a stack overflow we need to move the stack pointer
176// to the overflow region below the protected region. Because we now have
177// a gap in the stack (skips over protected region), we need to arrange
178// for the rest of the system to be unaware of the new stack arrangement
179// and behave as if there is a fully valid stack. We do this by placing
180// a unique address onto the stack followed by
181// the size of the gap. The stack walker will detect this and skip over the
182// gap.
183
184// NB. We also need to be careful of stack alignment as the ARM EABI specifies that
185// stack must be 8 byte aligned when making any calls.
186
187// NB. The size of the gap is the difference between the previous frame's SP and
188// the SP at which the size word is pushed.
189
Dave Allisonb373e092014-02-20 16:06:36 -0800190bool StackOverflowHandler::Action(int sig, siginfo_t* info, void* context) {
Dave Allisonf9439142014-03-27 15:10:22 -0700191 struct ucontext *uc = (struct ucontext *)context;
192 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
193 LOG(DEBUG) << "stack overflow handler with sp at " << std::hex << &uc;
194 LOG(DEBUG) << "sigcontext: " << std::hex << sc;
195
196 uint8_t* sp = reinterpret_cast<uint8_t*>(sc->arm_sp);
197 LOG(DEBUG) << "sp: " << static_cast<void*>(sp);
198
199 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address);
200 LOG(DEBUG) << "fault_addr: " << std::hex << fault_addr;
201 LOG(DEBUG) << "checking for stack overflow, sp: " << std::hex << static_cast<void*>(sp) <<
202 ", fault_addr: " << fault_addr;
203 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(sp - Thread::kStackOverflowReservedBytes);
204
205 // Check that the fault address is the value expected for a stack overflow.
206 if (fault_addr != overflow_addr) {
207 LOG(DEBUG) << "Not a stack overflow";
208 return false;
209 }
210
211 // We know this is a stack overflow. We need to move the sp to the overflow region
212 // the exists below the protected region. R9 contains the current Thread* so
213 // we can read the stack_end from that and subtract the size of the
214 // protected region. This creates a gap in the stack that needs to be marked.
215 Thread* self = reinterpret_cast<Thread*>(sc->arm_r9);
216
217 uint8_t* prevsp = sp;
218 sp = self->GetStackEnd() - Thread::kStackOverflowProtectedSize;
219 LOG(DEBUG) << "setting sp to overflow region at " << std::hex << static_cast<void*>(sp);
220
221 // We need to find the previous frame. Remember that
222 // this has not yet been fully constructed because the SP has not been
223 // decremented. So we need to work out the size of the spill portion of the
224 // frame. This consists of something like:
225 //
226 // 0xb6a1d49c: e92d40e0 push {r5, r6, r7, lr}
227 // 0xb6a1d4a0: ed2d8a06 vpush.f32 {s16-s21}
228 //
229 // The first is encoded in the ArtMethod as the spill_mask, the second as the
230 // fp_spill_mask. A population count on each will give the number of registers
231 // in each mask. Each register is 4 bytes on ARM32.
232
233 mirror::ArtMethod* method = reinterpret_cast<mirror::ArtMethod*>(sc->arm_r0);
234 uint32_t spill_mask = method->GetCoreSpillMask();
235 uint32_t numcores = __builtin_popcount(spill_mask);
236 uint32_t fp_spill_mask = method->GetFpSpillMask();
237 uint32_t numfps = __builtin_popcount(fp_spill_mask);
238 uint32_t spill_size = (numcores + numfps) * 4;
239 LOG(DEBUG) << "spill size: " << spill_size;
240 uint8_t* prevframe = prevsp + spill_size;
241 LOG(DEBUG) << "previous frame: " << static_cast<void*>(prevframe);
242
243 // NOTE: the ARM EABI needs an 8 byte alignment. In the case of ARM32 a pointer
244 // is 4 bytes so that, together with the offset to the previous frame is 8
245 // bytes. On other architectures we will need to align the stack.
246
247 // Push a marker onto the stack to tell the stack walker that there is a stack
248 // overflow and the stack is not contiguous.
249
250 // First the offset from SP to the previous frame.
251 sp -= sizeof(uint32_t);
252 LOG(DEBUG) << "push gap of " << static_cast<uint32_t>(prevframe - sp);
253 *reinterpret_cast<uint32_t*>(sp) = static_cast<uint32_t>(prevframe - sp);
254
255 // Now the gap marker (pointer sized).
256 sp -= sizeof(mirror::ArtMethod*);
257 *reinterpret_cast<void**>(sp) = stack_overflow_gap_marker;
258
259 // Now establish the stack pointer for the signal return.
260 sc->arm_sp = reinterpret_cast<uintptr_t>(sp);
261
262 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
263 // We need the LR to point to the GC map just after the fault instruction.
264 uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc);
265 uint32_t instr_size = GetInstructionSize(ptr);
266 sc->arm_lr = (sc->arm_pc + instr_size) | 1; // LR needs to point to gc map location
267 sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
268
269 // The kernel will now return to the address in sc->arm_pc. We have arranged the
270 // stack pointer to be in the overflow region. Throwing the exception will perform
271 // a longjmp which will restore the stack pointer to the correct location for the
272 // exception catch.
273 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800274}
275} // namespace art