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