1 /*
2  * Copyright (C) 2011 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_X86_64_CONTEXT_X86_64_H_
18 #define ART_RUNTIME_ARCH_X86_64_CONTEXT_X86_64_H_
19 
20 #include <android-base/logging.h>
21 
22 #include "arch/context.h"
23 #include "base/macros.h"
24 #include "registers_x86_64.h"
25 
26 namespace art {
27 namespace x86_64 {
28 
29 class X86_64Context final : public Context {
30  public:
X86_64Context()31   X86_64Context() {
32     Reset();
33   }
~X86_64Context()34   virtual ~X86_64Context() {}
35 
36   void Reset() override;
37 
38   void FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& fr) override;
39 
SetSP(uintptr_t new_sp)40   void SetSP(uintptr_t new_sp) override {
41     SetGPR(RSP, new_sp);
42   }
43 
SetPC(uintptr_t new_pc)44   void SetPC(uintptr_t new_pc) override {
45     rip_ = new_pc;
46   }
47 
SetNterpDexPC(uintptr_t dex_pc_ptr)48   void SetNterpDexPC(uintptr_t dex_pc_ptr) override {
49     SetGPR(R12, dex_pc_ptr);
50   }
51 
SetArg0(uintptr_t new_arg0_value)52   void SetArg0(uintptr_t new_arg0_value) override {
53     SetGPR(RDI, new_arg0_value);
54   }
55 
IsAccessibleGPR(uint32_t reg)56   bool IsAccessibleGPR(uint32_t reg) override {
57     DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
58     return gprs_[reg] != nullptr;
59   }
60 
GetGPRAddress(uint32_t reg)61   uintptr_t* GetGPRAddress(uint32_t reg) override {
62     DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
63     return gprs_[reg];
64   }
65 
GetGPR(uint32_t reg)66   uintptr_t GetGPR(uint32_t reg) override {
67     DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
68     DCHECK(IsAccessibleGPR(reg));
69     return *gprs_[reg];
70   }
71 
72   void SetGPR(uint32_t reg, uintptr_t value) override;
73 
IsAccessibleFPR(uint32_t reg)74   bool IsAccessibleFPR(uint32_t reg) override {
75     DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfFloatRegisters));
76     return fprs_[reg] != nullptr;
77   }
78 
GetFPR(uint32_t reg)79   uintptr_t GetFPR(uint32_t reg) override {
80     DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfFloatRegisters));
81     DCHECK(IsAccessibleFPR(reg));
82     return *fprs_[reg];
83   }
84 
85   void SetFPR(uint32_t reg, uintptr_t value) override;
86 
87   void SmashCallerSaves() override;
88   NO_RETURN void DoLongJump() override;
89 
90  private:
91   // Pointers to register locations. Values are initialized to null or the special registers below.
92   uintptr_t* gprs_[kNumberOfCpuRegisters];
93   uint64_t* fprs_[kNumberOfFloatRegisters];
94   // Hold values for rsp, rip and arg0 if they are not located within a stack frame. RIP is somewhat
95   // special in that it cannot be encoded normally as a register operand to an instruction (except
96   // in 64bit addressing modes).
97   uintptr_t rsp_, rip_, arg0_;
98 };
99 }  // namespace x86_64
100 }  // namespace art
101 
102 #endif  // ART_RUNTIME_ARCH_X86_64_CONTEXT_X86_64_H_
103