1 /* 2 * Copyright (C) 2023 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 #ifndef ART_RUNTIME_ARCH_RISCV64_CONTEXT_RISCV64_H_ 18 #define ART_RUNTIME_ARCH_RISCV64_CONTEXT_RISCV64_H_ 19 20 #include <android-base/logging.h> 21 22 #include <cstdlib> 23 24 #include "arch/context.h" 25 #include "base/macros.h" 26 #include "registers_riscv64.h" 27 28 namespace art HIDDEN { 29 namespace riscv64 { 30 31 class Riscv64Context final : public Context { 32 public: Riscv64Context()33 Riscv64Context() { Reset(); } 34 ~Riscv64Context()35 ~Riscv64Context() {} 36 37 void Reset() override; 38 39 void FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& fr) override; 40 SetSP(uintptr_t new_sp)41 void SetSP(uintptr_t new_sp) override { SetGPR(SP, new_sp); } 42 SetPC(uintptr_t new_pc)43 void SetPC(uintptr_t new_pc) override { SetGPR(kPC, new_pc); } 44 SetNterpDexPC(uintptr_t dex_pc_ptr)45 void SetNterpDexPC(uintptr_t dex_pc_ptr) override { SetGPR(S3, dex_pc_ptr); } 46 SetArg0(uintptr_t new_arg0_value)47 void SetArg0(uintptr_t new_arg0_value) override { SetGPR(A0, new_arg0_value); } 48 IsAccessibleGPR(uint32_t reg)49 bool IsAccessibleGPR(uint32_t reg) override { 50 DCHECK_LT(reg, arraysize(gprs_)); 51 return gprs_[reg] != nullptr; 52 } 53 GetGPRAddress(uint32_t reg)54 uintptr_t* GetGPRAddress(uint32_t reg) override { 55 DCHECK_LT(reg, arraysize(gprs_)); 56 return gprs_[reg]; 57 } 58 GetGPR(uint32_t reg)59 uintptr_t GetGPR(uint32_t reg) override { 60 // Note: PC isn't an available GPR (outside of internals), so don't allow retrieving the value. 61 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfXRegisters)); 62 DCHECK(IsAccessibleGPR(reg)); 63 return *gprs_[reg]; 64 } 65 66 void SetGPR(uint32_t reg, uintptr_t value) override; 67 IsAccessibleFPR(uint32_t reg)68 bool IsAccessibleFPR(uint32_t reg) override { 69 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfFRegisters)); 70 return fprs_[reg] != nullptr; 71 } 72 GetFPR(uint32_t reg)73 uintptr_t GetFPR(uint32_t reg) override { 74 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfFRegisters)); 75 DCHECK(IsAccessibleFPR(reg)); 76 return *fprs_[reg]; 77 } 78 79 void SetFPR(uint32_t reg, uintptr_t value) override; 80 81 void SmashCallerSaves() override; 82 NO_RETURN void DoLongJump() override; 83 84 static constexpr size_t kPC = kNumberOfXRegisters; 85 86 private: 87 // Pointers to register locations, initialized to null or the specific registers below. We need 88 // an additional one for the PC. 89 uintptr_t* gprs_[kNumberOfXRegisters + 1]; 90 uint64_t* fprs_[kNumberOfFRegisters]; 91 // Hold values for sp, pc and arg0 if they are not located within a stack frame. 92 uintptr_t sp_, pc_, arg0_; 93 }; 94 95 } // namespace riscv64 96 } // namespace art 97 98 #endif // ART_RUNTIME_ARCH_RISCV64_CONTEXT_RISCV64_H_ 99