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 
18 #include "fault_handler.h"
19 #include <sys/ucontext.h>
20 #include "art_method-inl.h"
21 #include "base/macros.h"
22 #include "globals.h"
23 #include "base/logging.h"
24 #include "base/hex_dump.h"
25 #include "registers_mips64.h"
26 #include "thread.h"
27 #include "thread-inl.h"
28 
29 extern "C" void art_quick_throw_stack_overflow();
30 extern "C" void art_quick_throw_null_pointer_exception();
31 
32 //
33 // Mips64 specific fault handler functions.
34 //
35 
36 namespace art {
37 
HandleNestedSignal(int sig ATTRIBUTE_UNUSED,siginfo_t * info ATTRIBUTE_UNUSED,void * context ATTRIBUTE_UNUSED)38 void FaultManager::HandleNestedSignal(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
39                                       void* context ATTRIBUTE_UNUSED) {
40 }
41 
GetMethodAndReturnPcAndSp(siginfo_t * siginfo,void * context,ArtMethod ** out_method,uintptr_t * out_return_pc,uintptr_t * out_sp)42 void 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);
47   *out_sp = static_cast<uintptr_t>(sc->sc_regs[29]);   // SP register
48   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) {
59     *out_method = reinterpret_cast<ArtMethod*>(sc->sc_regs[4]);  // A0 register
60   } 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;
72 }
73 
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info ATTRIBUTE_UNUSED,void * context)74 bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
75                                 void* context) {
76   // 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 
83   sc->sc_regs[31] = sc->sc_pc + 4;      // RA needs to point to gc map location
84   sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception);
85   sc->sc_regs[25] = sc->sc_pc;          // make sure T9 points to the function
86   VLOG(signals) << "Generating null pointer exception";
87   return true;
88 }
89 
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info ATTRIBUTE_UNUSED,void * context ATTRIBUTE_UNUSED)90 bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
91                                void* context ATTRIBUTE_UNUSED) {
92   return false;
93 }
94 
95 // Stack overflow fault handler.
96 //
97 // This checks that the fault address is equal to the current stack pointer
98 // minus the overflow region size (16K typically). The instruction that
99 // generates this signal is:
100 //
101 // lw zero, -16384(sp)
102 //
103 // It will fault if sp is inside the protected region on the stack.
104 //
105 // If we determine this is a stack overflow we need to move the stack pointer
106 // to the overflow region below the protected region.
107 
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info,void * context)108 bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
109   struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
110   struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
111   VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
112   VLOG(signals) << "sigcontext: " << std::hex << sc;
113 
114   uintptr_t sp = sc->sc_regs[29];  // SP register
115   VLOG(signals) << "sp: " << std::hex << sp;
116 
117   uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);  // BVA addr
118   VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
119   VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
120     ", fault_addr: " << fault_addr;
121 
122   uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kMips64);
123 
124   // Check that the fault address is the value expected for a stack overflow.
125   if (fault_addr != overflow_addr) {
126     VLOG(signals) << "Not a stack overflow";
127     return false;
128   }
129 
130   VLOG(signals) << "Stack overflow found";
131 
132   // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from.
133   // The value of RA must be the same as it was when we entered the code that
134   // caused this fault.  This will be inserted into a callee save frame by
135   // the function to which this handler returns (art_quick_throw_stack_overflow).
136   sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
137   sc->sc_regs[25] = sc->sc_pc;          // make sure T9 points to the function
138 
139   // The kernel will now return to the address in sc->arm_pc.
140   return true;
141 }
142 }       // namespace art
143