xref: /aosp_15_r20/art/runtime/arch/riscv64/fault_handler_riscv64.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "fault_handler.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <sys/ucontext.h>
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"  // For VLOG.
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker extern "C" void art_quick_throw_stack_overflow();
25*795d594fSAndroid Build Coastguard Worker extern "C" void art_quick_throw_null_pointer_exception_from_signal();
26*795d594fSAndroid Build Coastguard Worker extern "C" void art_quick_implicit_suspend();
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker // RISCV64 specific fault handler functions (or stubs if unimplemented yet).
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
31*795d594fSAndroid Build Coastguard Worker 
GetFaultPc(siginfo_t *,void * context)32*795d594fSAndroid Build Coastguard Worker uintptr_t FaultManager::GetFaultPc(siginfo_t*, void* context) {
33*795d594fSAndroid Build Coastguard Worker   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
34*795d594fSAndroid Build Coastguard Worker   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
35*795d594fSAndroid Build Coastguard Worker   if (mc->__gregs[REG_SP] == 0) {
36*795d594fSAndroid Build Coastguard Worker     VLOG(signals) << "Missing SP";
37*795d594fSAndroid Build Coastguard Worker     return 0u;
38*795d594fSAndroid Build Coastguard Worker   }
39*795d594fSAndroid Build Coastguard Worker   return mc->__gregs[REG_PC];
40*795d594fSAndroid Build Coastguard Worker }
41*795d594fSAndroid Build Coastguard Worker 
GetFaultSp(void * context)42*795d594fSAndroid Build Coastguard Worker uintptr_t FaultManager::GetFaultSp(void* context) {
43*795d594fSAndroid Build Coastguard Worker   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
44*795d594fSAndroid Build Coastguard Worker   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
45*795d594fSAndroid Build Coastguard Worker   return mc->__gregs[REG_SP];
46*795d594fSAndroid Build Coastguard Worker }
47*795d594fSAndroid Build Coastguard Worker 
Action(int sig,siginfo_t * info,void * context)48*795d594fSAndroid Build Coastguard Worker bool NullPointerHandler::Action([[maybe_unused]] int sig, siginfo_t* info, void* context) {
49*795d594fSAndroid Build Coastguard Worker   uintptr_t fault_address = reinterpret_cast<uintptr_t>(info->si_addr);
50*795d594fSAndroid Build Coastguard Worker   if (!IsValidFaultAddress(fault_address)) {
51*795d594fSAndroid Build Coastguard Worker     return false;
52*795d594fSAndroid Build Coastguard Worker   }
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
55*795d594fSAndroid Build Coastguard Worker   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
56*795d594fSAndroid Build Coastguard Worker   ArtMethod** sp = reinterpret_cast<ArtMethod**>(mc->__gregs[REG_SP]);
57*795d594fSAndroid Build Coastguard Worker   if (!IsValidMethod(*sp)) {
58*795d594fSAndroid Build Coastguard Worker     return false;
59*795d594fSAndroid Build Coastguard Worker   }
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   // For null checks in compiled code we insert a stack map that is immediately
62*795d594fSAndroid Build Coastguard Worker   // after the load/store instruction that might cause the fault and we need to
63*795d594fSAndroid Build Coastguard Worker   // pass the return PC to the handler. For null checks in Nterp, we similarly
64*795d594fSAndroid Build Coastguard Worker   // need the return PC to recognize that this was a null check in Nterp, so
65*795d594fSAndroid Build Coastguard Worker   // that the handler can get the needed data from the Nterp frame.
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker   // Need to work out the size of the instruction that caused the exception.
68*795d594fSAndroid Build Coastguard Worker   uintptr_t old_pc = mc->__gregs[REG_PC];
69*795d594fSAndroid Build Coastguard Worker   uintptr_t instr_size = (reinterpret_cast<uint16_t*>(old_pc)[0] & 3u) == 3u ? 4u : 2u;
70*795d594fSAndroid Build Coastguard Worker   uintptr_t return_pc = old_pc + instr_size;
71*795d594fSAndroid Build Coastguard Worker   if (!IsValidReturnPc(sp, return_pc)) {
72*795d594fSAndroid Build Coastguard Worker     return false;
73*795d594fSAndroid Build Coastguard Worker   }
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker   // Push the return PC to the stack and pass the fault address in RA.
76*795d594fSAndroid Build Coastguard Worker   mc->__gregs[REG_SP] -= sizeof(uintptr_t);
77*795d594fSAndroid Build Coastguard Worker   *reinterpret_cast<uintptr_t*>(mc->__gregs[REG_SP]) = return_pc;
78*795d594fSAndroid Build Coastguard Worker   mc->__gregs[REG_RA] = fault_address;
79*795d594fSAndroid Build Coastguard Worker 
80*795d594fSAndroid Build Coastguard Worker   // Arrange for the signal handler to return to the NPE entrypoint.
81*795d594fSAndroid Build Coastguard Worker   mc->__gregs[REG_PC] =
82*795d594fSAndroid Build Coastguard Worker       reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
83*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "Generating null pointer exception";
84*795d594fSAndroid Build Coastguard Worker   return true;
85*795d594fSAndroid Build Coastguard Worker }
86*795d594fSAndroid Build Coastguard Worker 
Action(int,siginfo_t *,void *)87*795d594fSAndroid Build Coastguard Worker bool SuspensionHandler::Action(int, siginfo_t*, void*) {
88*795d594fSAndroid Build Coastguard Worker   LOG(FATAL) << "SuspensionHandler::Action is not implemented for RISC-V";
89*795d594fSAndroid Build Coastguard Worker   return false;
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker 
Action(int sig,siginfo_t * info,void * context)92*795d594fSAndroid Build Coastguard Worker bool StackOverflowHandler::Action([[maybe_unused]] int sig,
93*795d594fSAndroid Build Coastguard Worker                                   siginfo_t* info,
94*795d594fSAndroid Build Coastguard Worker                                   void* context) {
95*795d594fSAndroid Build Coastguard Worker   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
96*795d594fSAndroid Build Coastguard Worker   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
97*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
98*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "sigcontext: " << std::hex << mc;
99*795d594fSAndroid Build Coastguard Worker 
100*795d594fSAndroid Build Coastguard Worker   uintptr_t sp = mc->__gregs[REG_SP];
101*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "sp: " << std::hex << sp;
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker   uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
104*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
105*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
106*795d594fSAndroid Build Coastguard Worker       ", fault_addr: " << fault_addr;
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker   uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(InstructionSet::kRiscv64);
109*795d594fSAndroid Build Coastguard Worker 
110*795d594fSAndroid Build Coastguard Worker   // Check that the fault address is the value expected for a stack overflow.
111*795d594fSAndroid Build Coastguard Worker   if (fault_addr != overflow_addr) {
112*795d594fSAndroid Build Coastguard Worker     VLOG(signals) << "Not a stack overflow";
113*795d594fSAndroid Build Coastguard Worker     return false;
114*795d594fSAndroid Build Coastguard Worker   }
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "Stack overflow found";
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker   // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
119*795d594fSAndroid Build Coastguard Worker   // The value of RA must be the same as it was when we entered the code that
120*795d594fSAndroid Build Coastguard Worker   // caused this fault.  This will be inserted into a callee save frame by
121*795d594fSAndroid Build Coastguard Worker   // the function to which this handler returns (art_quick_throw_stack_overflow).
122*795d594fSAndroid Build Coastguard Worker   mc->__gregs[REG_PC] = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker   // The kernel will now return to the address in `mc->__gregs[REG_PC]`.
125*795d594fSAndroid Build Coastguard Worker   return true;
126*795d594fSAndroid Build Coastguard Worker }
127*795d594fSAndroid Build Coastguard Worker 
128*795d594fSAndroid Build Coastguard Worker }  // namespace art
129