blob: e52dc73070550873ec5100a2b3837a998a02e86f [file] [log] [blame]
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001/*
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
Vladimir Marko7aa75602016-09-07 15:09:21 +010017#include "arch/mips64/quick_method_frame_info_mips64.h"
Andreas Gampe1a5c4062015-01-15 12:10:47 -080018#include "fault_handler.h"
19#include <sys/ucontext.h>
Douglas Leung22bb5a22015-07-02 16:42:08 -070020#include "art_method-inl.h"
Andreas Gampe1a5c4062015-01-15 12:10:47 -080021#include "base/macros.h"
22#include "globals.h"
23#include "base/logging.h"
24#include "base/hex_dump.h"
Douglas Leung22bb5a22015-07-02 16:42:08 -070025#include "registers_mips64.h"
26#include "thread.h"
27#include "thread-inl.h"
Andreas Gampe1a5c4062015-01-15 12:10:47 -080028
Douglas Leung22bb5a22015-07-02 16:42:08 -070029extern "C" void art_quick_throw_stack_overflow();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010030extern "C" void art_quick_throw_null_pointer_exception_from_signal();
Andreas Gampe1a5c4062015-01-15 12:10:47 -080031
32//
33// Mips64 specific fault handler functions.
34//
35
36namespace art {
37
38void FaultManager::HandleNestedSignal(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
39 void* context ATTRIBUTE_UNUSED) {
40}
41
Douglas Leung22bb5a22015-07-02 16:42:08 -070042void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
43 ArtMethod** out_method,
44 uintptr_t* out_return_pc, uintptr_t* out_sp) {
45 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
46 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +020047 *out_sp = static_cast<uintptr_t>(sc->sc_regs[mips64::SP]);
Douglas Leung22bb5a22015-07-02 16:42:08 -070048 VLOG(signals) << "sp: " << *out_sp;
49 if (*out_sp == 0) {
50 return;
51 }
52
53 // In the case of a stack overflow, the stack is not valid and we can't
54 // get the method from the top of the stack. However it's in r0.
55 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr); // BVA addr
56 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
57 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kMips64));
58 if (overflow_addr == fault_addr) {
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +020059 *out_method = reinterpret_cast<ArtMethod*>(sc->sc_regs[mips64::A0]);
Douglas Leung22bb5a22015-07-02 16:42:08 -070060 } else {
61 // The method is at the top of the stack.
62 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
63 }
64
65 // Work out the return PC. This will be the address of the instruction
66 // following the faulting ldr/str instruction.
67
68 VLOG(signals) << "pc: " << std::hex
69 << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->sc_pc));
70
71 *out_return_pc = sc->sc_pc + 4;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080072}
73
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010074bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
75 if (!IsValidImplicitCheck(info)) {
76 return false;
77 }
78
Douglas Leung22bb5a22015-07-02 16:42:08 -070079 // The code that looks for the catch location needs to know the value of the
80 // PC at the point of call. For Null checks we insert a GC map that is immediately after
81 // the load/store instruction that might cause the fault.
82
83 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
84 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
85
Vladimir Marko7aa75602016-09-07 15:09:21 +010086 // Decrement $sp by the frame size of the kSaveEverything method and store
87 // the fault address in the padding right after the ArtMethod*.
88 sc->sc_regs[mips64::SP] -= mips64::Mips64CalleeSaveFrameSize(Runtime::kSaveEverything);
89 uintptr_t* padding = reinterpret_cast<uintptr_t*>(sc->sc_regs[mips64::SP]) + /* ArtMethod* */ 1;
90 *padding = reinterpret_cast<uintptr_t>(info->si_addr);
91
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +020092 sc->sc_regs[mips64::RA] = sc->sc_pc + 4; // RA needs to point to gc map location
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010093 sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +020094 sc->sc_regs[mips64::T9] = sc->sc_pc; // make sure T9 points to the function
Douglas Leung22bb5a22015-07-02 16:42:08 -070095 VLOG(signals) << "Generating null pointer exception";
96 return true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080097}
98
99bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
100 void* context ATTRIBUTE_UNUSED) {
101 return false;
102}
103
Douglas Leung22bb5a22015-07-02 16:42:08 -0700104// Stack overflow fault handler.
105//
106// This checks that the fault address is equal to the current stack pointer
107// minus the overflow region size (16K typically). The instruction that
108// generates this signal is:
109//
110// lw zero, -16384(sp)
111//
112// It will fault if sp is inside the protected region on the stack.
113//
114// If we determine this is a stack overflow we need to move the stack pointer
115// to the overflow region below the protected region.
116
117bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
118 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
119 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
120 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
121 VLOG(signals) << "sigcontext: " << std::hex << sc;
122
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +0200123 uintptr_t sp = sc->sc_regs[mips64::SP];
Douglas Leung22bb5a22015-07-02 16:42:08 -0700124 VLOG(signals) << "sp: " << std::hex << sp;
125
126 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr); // BVA addr
127 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
128 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
129 ", fault_addr: " << fault_addr;
130
131 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kMips64);
132
133 // Check that the fault address is the value expected for a stack overflow.
134 if (fault_addr != overflow_addr) {
135 VLOG(signals) << "Not a stack overflow";
136 return false;
137 }
138
139 VLOG(signals) << "Stack overflow found";
140
141 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from.
142 // The value of RA must be the same as it was when we entered the code that
143 // caused this fault. This will be inserted into a callee save frame by
144 // the function to which this handler returns (art_quick_throw_stack_overflow).
145 sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +0200146 sc->sc_regs[mips64::T9] = sc->sc_pc; // make sure T9 points to the function
Douglas Leung22bb5a22015-07-02 16:42:08 -0700147
148 // The kernel will now return to the address in sc->arm_pc.
149 return true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800150}
151} // namespace art