1 /*
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 #include "fault_handler.h"
18
19 #include <sys/ucontext.h>
20
21 #include "arch/instruction_set.h"
22 #include "arch/mips64/callee_save_frame_mips64.h"
23 #include "art_method.h"
24 #include "base/callee_save_type.h"
25 #include "base/hex_dump.h"
26 #include "base/logging.h" // For VLOG.
27 #include "base/macros.h"
28 #include "registers_mips64.h"
29 #include "runtime_globals.h"
30 #include "thread-current-inl.h"
31
32 extern "C" void art_quick_throw_stack_overflow();
33 extern "C" void art_quick_throw_null_pointer_exception_from_signal();
34
35 //
36 // Mips64 specific fault handler functions.
37 //
38
39 namespace art {
40
GetMethodAndReturnPcAndSp(siginfo_t * siginfo,void * context,ArtMethod ** out_method,uintptr_t * out_return_pc,uintptr_t * out_sp)41 void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
42 ArtMethod** out_method,
43 uintptr_t* out_return_pc, uintptr_t* out_sp) {
44 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
45 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
46 *out_sp = static_cast<uintptr_t>(sc->sc_regs[mips64::SP]);
47 VLOG(signals) << "sp: " << *out_sp;
48 if (*out_sp == 0) {
49 return;
50 }
51
52 // In the case of a stack overflow, the stack is not valid and we can't
53 // get the method from the top of the stack. However it's in r0.
54 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr); // BVA addr
55 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
56 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(InstructionSet::kMips64));
57 if (overflow_addr == fault_addr) {
58 *out_method = reinterpret_cast<ArtMethod*>(sc->sc_regs[mips64::A0]);
59 } else {
60 // The method is at the top of the stack.
61 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
62 }
63
64 // Work out the return PC. This will be the address of the instruction
65 // following the faulting ldr/str instruction.
66
67 VLOG(signals) << "pc: " << std::hex
68 << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->sc_pc));
69
70 *out_return_pc = sc->sc_pc + 4;
71 }
72
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info,void * context)73 bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
74 if (!IsValidImplicitCheck(info)) {
75 return false;
76 }
77
78 // The code that looks for the catch location needs to know the value of the
79 // PC at the point of call. For Null checks we insert a GC map that is immediately after
80 // the load/store instruction that might cause the fault.
81
82 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
83 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
84
85 // Decrement $sp by the frame size of the kSaveEverything method and store
86 // the fault address in the padding right after the ArtMethod*.
87 sc->sc_regs[mips64::SP] -= mips64::Mips64CalleeSaveFrameSize(CalleeSaveType::kSaveEverything);
88 uintptr_t* padding = reinterpret_cast<uintptr_t*>(sc->sc_regs[mips64::SP]) + /* ArtMethod* */ 1;
89 *padding = reinterpret_cast<uintptr_t>(info->si_addr);
90
91 sc->sc_regs[mips64::RA] = sc->sc_pc + 4; // RA needs to point to gc map location
92 sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
93 // Note: This entrypoint does not rely on T9 pointing to it, so we may as well preserve T9.
94 VLOG(signals) << "Generating null pointer exception";
95 return true;
96 }
97
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info ATTRIBUTE_UNUSED,void * context ATTRIBUTE_UNUSED)98 bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
99 void* context ATTRIBUTE_UNUSED) {
100 return false;
101 }
102
103 // Stack overflow fault handler.
104 //
105 // This checks that the fault address is equal to the current stack pointer
106 // minus the overflow region size (16K typically). The instruction that
107 // generates this signal is:
108 //
109 // lw zero, -16384(sp)
110 //
111 // It will fault if sp is inside the protected region on the stack.
112 //
113 // If we determine this is a stack overflow we need to move the stack pointer
114 // to the overflow region below the protected region.
115
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info,void * context)116 bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
117 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
118 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
119 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
120 VLOG(signals) << "sigcontext: " << std::hex << sc;
121
122 uintptr_t sp = sc->sc_regs[mips64::SP];
123 VLOG(signals) << "sp: " << std::hex << sp;
124
125 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr); // BVA addr
126 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
127 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
128 ", fault_addr: " << fault_addr;
129
130 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(InstructionSet::kMips64);
131
132 // Check that the fault address is the value expected for a stack overflow.
133 if (fault_addr != overflow_addr) {
134 VLOG(signals) << "Not a stack overflow";
135 return false;
136 }
137
138 VLOG(signals) << "Stack overflow found";
139
140 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from.
141 // The value of RA must be the same as it was when we entered the code that
142 // caused this fault. This will be inserted into a callee save frame by
143 // the function to which this handler returns (art_quick_throw_stack_overflow).
144 sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
145 sc->sc_regs[mips64::T9] = sc->sc_pc; // make sure T9 points to the function
146
147 // The kernel will now return to the address in sc->arm_pc.
148 return true;
149 }
150 } // namespace art
151