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 #include "context_x86_64.h"
18 
19 #include "base/bit_utils.h"
20 #include "quick/quick_method_frame_info.h"
21 
22 namespace art {
23 namespace x86_64 {
24 
25 static constexpr uintptr_t gZero = 0;
26 
Reset()27 void X86_64Context::Reset() {
28   std::fill_n(gprs_, arraysize(gprs_), nullptr);
29   std::fill_n(fprs_, arraysize(fprs_), nullptr);
30   gprs_[RSP] = &rsp_;
31   gprs_[RDI] = &arg0_;
32   // Initialize registers with easy to spot debug values.
33   rsp_ = X86_64Context::kBadGprBase + RSP;
34   rip_ = X86_64Context::kBadGprBase + kNumberOfCpuRegisters;
35   arg0_ = 0;
36 }
37 
FillCalleeSaves(uint8_t * frame,const QuickMethodFrameInfo & frame_info)38 void X86_64Context::FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& frame_info) {
39   int spill_pos = 0;
40 
41   // Core registers come first, from the highest down to the lowest.
42   uint32_t core_regs =
43       frame_info.CoreSpillMask() & ~(static_cast<uint32_t>(-1) << kNumberOfCpuRegisters);
44   DCHECK_EQ(1, POPCOUNT(frame_info.CoreSpillMask() & ~core_regs));  // Return address spill.
45   for (uint32_t core_reg : HighToLowBits(core_regs)) {
46     gprs_[core_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
47     ++spill_pos;
48   }
49   DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) - 1);
50 
51   // FP registers come second, from the highest down to the lowest.
52   uint32_t fp_regs = frame_info.FpSpillMask();
53   DCHECK_EQ(0u, fp_regs & (static_cast<uint32_t>(-1) << kNumberOfFloatRegisters));
54   for (uint32_t fp_reg : HighToLowBits(fp_regs)) {
55     fprs_[fp_reg] = reinterpret_cast<uint64_t*>(
56         CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes()));
57     ++spill_pos;
58   }
59   DCHECK_EQ(spill_pos,
60             POPCOUNT(frame_info.CoreSpillMask()) - 1 + POPCOUNT(frame_info.FpSpillMask()));
61 }
62 
SmashCallerSaves()63 void X86_64Context::SmashCallerSaves() {
64   // This needs to be 0 because we want a null/zero return value.
65   gprs_[RAX] = const_cast<uintptr_t*>(&gZero);
66   gprs_[RDX] = const_cast<uintptr_t*>(&gZero);
67   gprs_[RCX] = nullptr;
68   gprs_[RSI] = nullptr;
69   gprs_[RDI] = nullptr;
70   gprs_[R8] = nullptr;
71   gprs_[R9] = nullptr;
72   gprs_[R10] = nullptr;
73   gprs_[R11] = nullptr;
74   fprs_[XMM0] = nullptr;
75   fprs_[XMM1] = nullptr;
76   fprs_[XMM2] = nullptr;
77   fprs_[XMM3] = nullptr;
78   fprs_[XMM4] = nullptr;
79   fprs_[XMM5] = nullptr;
80   fprs_[XMM6] = nullptr;
81   fprs_[XMM7] = nullptr;
82   fprs_[XMM8] = nullptr;
83   fprs_[XMM9] = nullptr;
84   fprs_[XMM10] = nullptr;
85   fprs_[XMM11] = nullptr;
86 }
87 
SetGPR(uint32_t reg,uintptr_t value)88 void X86_64Context::SetGPR(uint32_t reg, uintptr_t value) {
89   CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
90   DCHECK(IsAccessibleGPR(reg));
91   CHECK_NE(gprs_[reg], &gZero);
92   *gprs_[reg] = value;
93 }
94 
SetFPR(uint32_t reg,uintptr_t value)95 void X86_64Context::SetFPR(uint32_t reg, uintptr_t value) {
96   CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFloatRegisters));
97   DCHECK(IsAccessibleFPR(reg));
98   CHECK_NE(fprs_[reg], reinterpret_cast<const uint64_t*>(&gZero));
99   *fprs_[reg] = value;
100 }
101 
102 extern "C" NO_RETURN void art_quick_do_long_jump(uintptr_t*, uintptr_t*);
103 
DoLongJump()104 void X86_64Context::DoLongJump() {
105 #if defined(__x86_64__)
106   uintptr_t gprs[kNumberOfCpuRegisters + 1];
107   uintptr_t fprs[kNumberOfFloatRegisters];
108 
109   for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
110     gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != nullptr ? *gprs_[i] : X86_64Context::kBadGprBase + i;
111   }
112   for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) {
113     fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : X86_64Context::kBadFprBase + i;
114   }
115 
116   // We want to load the stack pointer one slot below so that the ret will pop eip.
117   uintptr_t rsp = gprs[kNumberOfCpuRegisters - RSP - 1] - sizeof(intptr_t);
118   gprs[kNumberOfCpuRegisters] = rsp;
119   *(reinterpret_cast<uintptr_t*>(rsp)) = rip_;
120 
121   art_quick_do_long_jump(gprs, fprs);
122 #else
123   UNIMPLEMENTED(FATAL);
124   UNREACHABLE();
125 #endif
126 }
127 
128 }  // namespace x86_64
129 }  // namespace art
130