blob: 3eb92c85565688a25808c50ecfb853c703df6271 [file] [log] [blame]
Stuart Monteithb95a5342014-03-12 13:32:32 +00001/*
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
Vladimir Marko7624d252014-05-02 14:40:15 +010021#include "mirror/art_method-inl.h"
Stuart Monteithb95a5342014-03-12 13:32:32 +000022#include "mirror/object-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010023#include "quick/quick_method_frame_info.h"
Stuart Monteithb95a5342014-03-12 13:32:32 +000024#include "stack.h"
25#include "thread.h"
26
27
28namespace art {
29namespace arm64 {
30
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020031static constexpr uint64_t gZero = 0;
Stuart Monteithb95a5342014-03-12 13:32:32 +000032
33void Arm64Context::Reset() {
34 for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020035 gprs_[i] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +000036 }
37 for (size_t i = 0; i < kNumberOfDRegisters; i++) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020038 fprs_[i] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +000039 }
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
47void Arm64Context::FillCalleeSaves(const StackVisitor& fr) {
48 mirror::ArtMethod* method = fr.GetMethod();
Vladimir Marko7624d252014-05-02 14:40:15 +010049 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());
Stuart Monteithb95a5342014-03-12 13:32:32 +000052 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++) {
Vladimir Marko7624d252014-05-02 14:40:15 +010056 if (((frame_info.CoreSpillMask() >> i) & 1) != 0) {
57 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_info.FrameSizeInBytes());
Stuart Monteithb95a5342014-03-12 13:32:32 +000058 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++) {
Vladimir Marko7624d252014-05-02 14:40:15 +010067 if (((frame_info.FpSpillMask() >> i) & 1) != 0) {
68 fprs_[i] = fr.CalleeSaveAddress(spill_count + fp_spill_count - j,
69 frame_info.FrameSizeInBytes());
Stuart Monteithb95a5342014-03-12 13:32:32 +000070 j++;
71 }
72 }
73 }
74}
75
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020076bool Arm64Context::SetGPR(uint32_t reg, uintptr_t value) {
Stuart Monteithb95a5342014-03-12 13:32:32 +000077 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.
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020079 if (gprs_[reg] != nullptr) {
80 *gprs_[reg] = value;
81 return true;
82 } else {
83 return false;
84 }
85}
86
87bool 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 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000096}
97
98void Arm64Context::SmashCallerSaves() {
99 // This needs to be 0 because we want a null/zero return value.
100 gprs_[X0] = const_cast<uint64_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200101 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;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000116
Andreas Gampe6cf80102014-05-19 11:32:41 -0700117 // d0-d7, d16-d31 are caller-saved; d8-d15 are callee-saved.
118
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200119 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;
Andreas Gampe6cf80102014-05-19 11:32:41 -0700127
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200128 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;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000144}
145
146extern "C" void art_quick_do_long_jump(uint64_t*, uint64_t*);
147
148void Arm64Context::DoLongJump() {
149 uint64_t gprs[32];
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200150 uint64_t fprs[kNumberOfDRegisters];
Stuart Monteithb95a5342014-03-12 13:32:32 +0000151
Andreas Gampea3763282014-04-09 10:25:11 -0700152 // Do not use kNumberOfCoreRegisters, as this is with the distinction of SP and XZR
153 for (size_t i = 0; i < 32; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200154 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : Arm64Context::kBadGprBase + i;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000155 }
156 for (size_t i = 0; i < kNumberOfDRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200157 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : Arm64Context::kBadGprBase + i;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000158 }
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