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 #include <stdint.h> 18 19 #include "context_arm64.h" 20 21 #include "mirror/art_method-inl.h" 22 #include "mirror/object-inl.h" 23 #include "quick/quick_method_frame_info.h" 24 #include "stack.h" 25 #include "thread.h" 26 27 28 namespace art { 29 namespace arm64 { 30 31 static constexpr uint64_t gZero = 0; 32 33 void Arm64Context::Reset() { 34 for (size_t i = 0; i < kNumberOfCoreRegisters; i++) { 35 gprs_[i] = nullptr; 36 } 37 for (size_t i = 0; i < kNumberOfDRegisters; i++) { 38 fprs_[i] = nullptr; 39 } 40 gprs_[SP] = &sp_; 41 gprs_[LR] = &pc_; 42 // Initialize registers with easy to spot debug values. 43 sp_ = Arm64Context::kBadGprBase + SP; 44 pc_ = Arm64Context::kBadGprBase + LR; 45 } 46 47 void Arm64Context::FillCalleeSaves(const StackVisitor& fr) { 48 mirror::ArtMethod* method = fr.GetMethod(); 49 const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo(); 50 size_t spill_count = POPCOUNT(frame_info.CoreSpillMask()); 51 size_t fp_spill_count = POPCOUNT(frame_info.FpSpillMask()); 52 if (spill_count > 0) { 53 // Lowest number spill is farthest away, walk registers and fill into context. 54 int j = 1; 55 for (size_t i = 0; i < kNumberOfCoreRegisters; i++) { 56 if (((frame_info.CoreSpillMask() >> i) & 1) != 0) { 57 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_info.FrameSizeInBytes()); 58 j++; 59 } 60 } 61 } 62 63 if (fp_spill_count > 0) { 64 // Lowest number spill is farthest away, walk registers and fill into context. 65 int j = 1; 66 for (size_t i = 0; i < kNumberOfDRegisters; i++) { 67 if (((frame_info.FpSpillMask() >> i) & 1) != 0) { 68 fprs_[i] = fr.CalleeSaveAddress(spill_count + fp_spill_count - j, 69 frame_info.FrameSizeInBytes()); 70 j++; 71 } 72 } 73 } 74 } 75 76 bool Arm64Context::SetGPR(uint32_t reg, uintptr_t value) { 77 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters)); 78 DCHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset. 79 if (gprs_[reg] != nullptr) { 80 *gprs_[reg] = value; 81 return true; 82 } else { 83 return false; 84 } 85 } 86 87 bool Arm64Context::SetFPR(uint32_t reg, uintptr_t value) { 88 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfDRegisters)); 89 DCHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset. 90 if (fprs_[reg] != nullptr) { 91 *fprs_[reg] = value; 92 return true; 93 } else { 94 return false; 95 } 96 } 97 98 void Arm64Context::SmashCallerSaves() { 99 // This needs to be 0 because we want a null/zero return value. 100 gprs_[X0] = const_cast<uint64_t*>(&gZero); 101 gprs_[X1] = nullptr; 102 gprs_[X2] = nullptr; 103 gprs_[X3] = nullptr; 104 gprs_[X4] = nullptr; 105 gprs_[X5] = nullptr; 106 gprs_[X6] = nullptr; 107 gprs_[X7] = nullptr; 108 gprs_[X8] = nullptr; 109 gprs_[X9] = nullptr; 110 gprs_[X10] = nullptr; 111 gprs_[X11] = nullptr; 112 gprs_[X12] = nullptr; 113 gprs_[X13] = nullptr; 114 gprs_[X14] = nullptr; 115 gprs_[X15] = nullptr; 116 117 // d0-d7, d16-d31 are caller-saved; d8-d15 are callee-saved. 118 119 fprs_[D0] = nullptr; 120 fprs_[D1] = nullptr; 121 fprs_[D2] = nullptr; 122 fprs_[D3] = nullptr; 123 fprs_[D4] = nullptr; 124 fprs_[D5] = nullptr; 125 fprs_[D6] = nullptr; 126 fprs_[D7] = nullptr; 127 128 fprs_[D16] = nullptr; 129 fprs_[D17] = nullptr; 130 fprs_[D18] = nullptr; 131 fprs_[D19] = nullptr; 132 fprs_[D20] = nullptr; 133 fprs_[D21] = nullptr; 134 fprs_[D22] = nullptr; 135 fprs_[D23] = nullptr; 136 fprs_[D24] = nullptr; 137 fprs_[D25] = nullptr; 138 fprs_[D26] = nullptr; 139 fprs_[D27] = nullptr; 140 fprs_[D28] = nullptr; 141 fprs_[D29] = nullptr; 142 fprs_[D30] = nullptr; 143 fprs_[D31] = nullptr; 144 } 145 146 extern "C" void art_quick_do_long_jump(uint64_t*, uint64_t*); 147 148 void Arm64Context::DoLongJump() { 149 uint64_t gprs[32]; 150 uint64_t fprs[kNumberOfDRegisters]; 151 152 // Do not use kNumberOfCoreRegisters, as this is with the distinction of SP and XZR 153 for (size_t i = 0; i < 32; ++i) { 154 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : Arm64Context::kBadGprBase + i; 155 } 156 for (size_t i = 0; i < kNumberOfDRegisters; ++i) { 157 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : Arm64Context::kBadGprBase + i; 158 } 159 DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]); 160 art_quick_do_long_jump(gprs, fprs); 161 } 162 163 } // namespace arm64 164 } // namespace art 165