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 { 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)45 void SetNterpDexPC(uintptr_t /*dex_pc_ptr*/) override { 46 UNREACHABLE(); // Nterp is not supported on RISC-V yet 47 } 48 SetArg0(uintptr_t new_arg0_value)49 void SetArg0(uintptr_t new_arg0_value) override { SetGPR(A0, new_arg0_value); } 50 IsAccessibleGPR(uint32_t reg)51 bool IsAccessibleGPR(uint32_t reg) override { 52 DCHECK_LT(reg, arraysize(gprs_)); 53 return gprs_[reg] != nullptr; 54 } 55 GetGPRAddress(uint32_t reg)56 uintptr_t* GetGPRAddress(uint32_t reg) override { 57 DCHECK_LT(reg, arraysize(gprs_)); 58 return gprs_[reg]; 59 } 60 GetGPR(uint32_t reg)61 uintptr_t GetGPR(uint32_t reg) override { 62 // Note: PC isn't an available GPR (outside of internals), so don't allow retrieving the value. 63 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfXRegisters)); 64 DCHECK(IsAccessibleGPR(reg)); 65 return *gprs_[reg]; 66 } 67 68 void SetGPR(uint32_t reg, uintptr_t value) override; 69 IsAccessibleFPR(uint32_t reg)70 bool IsAccessibleFPR(uint32_t reg) override { 71 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfFRegisters)); 72 return fprs_[reg] != nullptr; 73 } 74 GetFPR(uint32_t reg)75 uintptr_t GetFPR(uint32_t reg) override { 76 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfFRegisters)); 77 DCHECK(IsAccessibleFPR(reg)); 78 return *fprs_[reg]; 79 } 80 81 void SetFPR(uint32_t reg, uintptr_t value) override; 82 83 void SmashCallerSaves() override; 84 NO_RETURN void DoLongJump() override; 85 86 static constexpr size_t kPC = kNumberOfXRegisters; 87 88 private: 89 // Pointers to register locations, initialized to null or the specific registers below. We need 90 // an additional one for the PC. 91 uintptr_t* gprs_[kNumberOfXRegisters + 1]; 92 uint64_t* fprs_[kNumberOfFRegisters]; 93 // Hold values for sp, pc and arg0 if they are not located within a stack frame. 94 uintptr_t sp_, pc_, arg0_; 95 }; 96 97 } // namespace riscv64 98 } // namespace art 99 100 #endif // ART_RUNTIME_ARCH_RISCV64_CONTEXT_RISCV64_H_ 101