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