blob: 0354f0c7a6605b71e7c5ff946dbd4b155a5489ab [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
Dave Allisonb373e092014-02-20 16:06:36 -080017#include <sys/ucontext.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070018#include "fault_handler.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080019
Andreas Gampe639b2b12019-01-08 10:32:50 -080020#include "arch/instruction_set.h"
Vladimir Markod3083dd2018-05-17 08:43:47 +010021#include "arch/mips/callee_save_frame_mips.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080022#include "art_method.h"
Andreas Gampef44b3a62017-06-08 19:47:07 -070023#include "base/callee_save_type.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080024#include "base/hex_dump.h"
Andreas Gampe57943812017-12-06 21:39:13 -080025#include "base/logging.h" // For VLOG.
Dave Allisonb373e092014-02-20 16:06:36 -080026#include "base/macros.h"
Douglas Leung22bb5a22015-07-02 16:42:08 -070027#include "registers_mips.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080028#include "runtime_globals.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070029#include "thread-current-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080030
Douglas Leung22bb5a22015-07-02 16:42:08 -070031extern "C" void art_quick_throw_stack_overflow();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010032extern "C" void art_quick_throw_null_pointer_exception_from_signal();
Dave Allisonb373e092014-02-20 16:06:36 -080033
34//
35// Mips specific fault handler functions.
36//
37
38namespace art {
39
Douglas Leung22bb5a22015-07-02 16:42:08 -070040void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
41 ArtMethod** out_method,
42 uintptr_t* out_return_pc, uintptr_t* out_sp) {
43 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
44 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +020045 *out_sp = static_cast<uintptr_t>(sc->sc_regs[mips::SP]);
Douglas Leung22bb5a22015-07-02 16:42:08 -070046 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 r0.
53 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr); // BVA addr
54 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
Vladimir Marko33bff252017-11-01 14:35:42 +000055 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(InstructionSet::kMips));
Douglas Leung22bb5a22015-07-02 16:42:08 -070056 if (overflow_addr == fault_addr) {
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +020057 *out_method = reinterpret_cast<ArtMethod*>(sc->sc_regs[mips::A0]);
Douglas Leung22bb5a22015-07-02 16:42:08 -070058 } else {
59 // The method is at the top of the stack.
60 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
61 }
62
63 // Work out the return PC. This will be the address of the instruction
64 // following the faulting ldr/str instruction.
65
66 VLOG(signals) << "pc: " << std::hex
67 << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->sc_pc));
68
69 *out_return_pc = sc->sc_pc + 4;
Dave Allisonb373e092014-02-20 16:06:36 -080070}
71
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010072bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
73 if (!IsValidImplicitCheck(info)) {
74 return false;
75 }
Douglas Leung22bb5a22015-07-02 16:42:08 -070076 // The code that looks for the catch location needs to know the value of the
77 // PC at the point of call. For Null checks we insert a GC map that is immediately after
78 // the load/store instruction that might cause the fault.
79
80 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
81 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
82
Vladimir Marko3b7537b2016-09-13 11:56:01 +000083 // Decrement $sp by the frame size of the kSaveEverything method and store
84 // the fault address in the padding right after the ArtMethod*.
Andreas Gampef44b3a62017-06-08 19:47:07 -070085 sc->sc_regs[mips::SP] -= mips::MipsCalleeSaveFrameSize(CalleeSaveType::kSaveEverything);
Vladimir Marko3b7537b2016-09-13 11:56:01 +000086 uintptr_t* padding = reinterpret_cast<uintptr_t*>(sc->sc_regs[mips::SP]) + /* ArtMethod* */ 1;
87 *padding = reinterpret_cast<uintptr_t>(info->si_addr);
88
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +020089 sc->sc_regs[mips::RA] = sc->sc_pc + 4; // RA needs to point to gc map location
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010090 sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
Vladimir Marko804b03f2016-09-14 16:26:36 +010091 // Note: This entrypoint does not rely on T9 pointing to it, so we may as well preserve T9.
Douglas Leung22bb5a22015-07-02 16:42:08 -070092 VLOG(signals) << "Generating null pointer exception";
93 return true;
Dave Allisonb373e092014-02-20 16:06:36 -080094}
95
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -080096bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
97 void* context ATTRIBUTE_UNUSED) {
Dave Allisonb373e092014-02-20 16:06:36 -080098 return false;
99}
100
Douglas Leung22bb5a22015-07-02 16:42:08 -0700101// Stack overflow fault handler.
102//
103// This checks that the fault address is equal to the current stack pointer
104// minus the overflow region size (16K typically). The instruction that
105// generates this signal is:
106//
107// lw zero, -16384(sp)
108//
109// It will fault if sp is inside the protected region on the stack.
110//
111// If we determine this is a stack overflow we need to move the stack pointer
112// to the overflow region below the protected region.
113
114bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
115 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
116 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
117 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
118 VLOG(signals) << "sigcontext: " << std::hex << sc;
119
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +0200120 uintptr_t sp = sc->sc_regs[mips::SP];
Douglas Leung22bb5a22015-07-02 16:42:08 -0700121 VLOG(signals) << "sp: " << std::hex << sp;
122
123 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr); // BVA addr
124 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
125 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
126 ", fault_addr: " << fault_addr;
127
Vladimir Marko33bff252017-11-01 14:35:42 +0000128 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(InstructionSet::kMips);
Douglas Leung22bb5a22015-07-02 16:42:08 -0700129
130 // Check that the fault address is the value expected for a stack overflow.
131 if (fault_addr != overflow_addr) {
132 VLOG(signals) << "Not a stack overflow";
133 return false;
134 }
135
136 VLOG(signals) << "Stack overflow found";
137
138 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from.
139 // The value of RA must be the same as it was when we entered the code that
140 // caused this fault. This will be inserted into a callee save frame by
141 // the function to which this handler returns (art_quick_throw_stack_overflow).
142 sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
Goran Jakovljevic5d45e8f2016-06-30 16:40:20 +0200143 sc->sc_regs[mips::T9] = sc->sc_pc; // make sure T9 points to the function
Douglas Leung22bb5a22015-07-02 16:42:08 -0700144
145 // The kernel will now return to the address in sc->arm_pc.
146 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800147}
148} // namespace art