blob: 0a3148016a0c98b74ae44ee874aebf0dc160a66c [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"
Vladimir Marko7624d252014-05-02 14:40:15 +010022#include "quick/quick_method_frame_info.h"
Andreas Gampeb6886112014-11-03 08:47:01 -080023#include "utils.h"
Stuart Monteithb95a5342014-03-12 13:32:32 +000024
25namespace art {
26namespace arm64 {
27
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020028static constexpr uint64_t gZero = 0;
Stuart Monteithb95a5342014-03-12 13:32:32 +000029
30void Arm64Context::Reset() {
Alexandre Rames37c92df2014-10-17 14:35:27 +010031 for (size_t i = 0; i < kNumberOfXRegisters; i++) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020032 gprs_[i] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +000033 }
34 for (size_t i = 0; i < kNumberOfDRegisters; i++) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020035 fprs_[i] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +000036 }
37 gprs_[SP] = &sp_;
38 gprs_[LR] = &pc_;
39 // Initialize registers with easy to spot debug values.
40 sp_ = Arm64Context::kBadGprBase + SP;
41 pc_ = Arm64Context::kBadGprBase + LR;
42}
43
44void Arm64Context::FillCalleeSaves(const StackVisitor& fr) {
45 mirror::ArtMethod* method = fr.GetMethod();
Vladimir Marko7624d252014-05-02 14:40:15 +010046 const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo();
47 size_t spill_count = POPCOUNT(frame_info.CoreSpillMask());
48 size_t fp_spill_count = POPCOUNT(frame_info.FpSpillMask());
Stuart Monteithb95a5342014-03-12 13:32:32 +000049 if (spill_count > 0) {
50 // Lowest number spill is farthest away, walk registers and fill into context.
51 int j = 1;
Alexandre Rames37c92df2014-10-17 14:35:27 +010052 for (size_t i = 0; i < kNumberOfXRegisters; i++) {
Vladimir Marko7624d252014-05-02 14:40:15 +010053 if (((frame_info.CoreSpillMask() >> i) & 1) != 0) {
54 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_info.FrameSizeInBytes());
Stuart Monteithb95a5342014-03-12 13:32:32 +000055 j++;
56 }
57 }
58 }
59
60 if (fp_spill_count > 0) {
61 // Lowest number spill is farthest away, walk registers and fill into context.
62 int j = 1;
63 for (size_t i = 0; i < kNumberOfDRegisters; i++) {
Vladimir Marko7624d252014-05-02 14:40:15 +010064 if (((frame_info.FpSpillMask() >> i) & 1) != 0) {
65 fprs_[i] = fr.CalleeSaveAddress(spill_count + fp_spill_count - j,
66 frame_info.FrameSizeInBytes());
Stuart Monteithb95a5342014-03-12 13:32:32 +000067 j++;
68 }
69 }
70 }
71}
72
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020073bool Arm64Context::SetGPR(uint32_t reg, uintptr_t value) {
Alexandre Rames37c92df2014-10-17 14:35:27 +010074 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfXRegisters));
Alexandre Ramesa304f972014-10-17 14:35:27 +010075 DCHECK_NE(reg, static_cast<uint32_t>(XZR));
Stuart Monteithb95a5342014-03-12 13:32:32 +000076 DCHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020077 if (gprs_[reg] != nullptr) {
78 *gprs_[reg] = value;
79 return true;
80 } else {
81 return false;
82 }
83}
84
85bool Arm64Context::SetFPR(uint32_t reg, uintptr_t value) {
86 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfDRegisters));
87 DCHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
88 if (fprs_[reg] != nullptr) {
89 *fprs_[reg] = value;
90 return true;
91 } else {
92 return false;
93 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000094}
95
96void Arm64Context::SmashCallerSaves() {
97 // This needs to be 0 because we want a null/zero return value.
98 gprs_[X0] = const_cast<uint64_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020099 gprs_[X1] = nullptr;
100 gprs_[X2] = nullptr;
101 gprs_[X3] = nullptr;
102 gprs_[X4] = nullptr;
103 gprs_[X5] = nullptr;
104 gprs_[X6] = nullptr;
105 gprs_[X7] = nullptr;
106 gprs_[X8] = nullptr;
107 gprs_[X9] = nullptr;
108 gprs_[X10] = nullptr;
109 gprs_[X11] = nullptr;
110 gprs_[X12] = nullptr;
111 gprs_[X13] = nullptr;
112 gprs_[X14] = nullptr;
113 gprs_[X15] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000114
Andreas Gampe6cf80102014-05-19 11:32:41 -0700115 // d0-d7, d16-d31 are caller-saved; d8-d15 are callee-saved.
116
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200117 fprs_[D0] = nullptr;
118 fprs_[D1] = nullptr;
119 fprs_[D2] = nullptr;
120 fprs_[D3] = nullptr;
121 fprs_[D4] = nullptr;
122 fprs_[D5] = nullptr;
123 fprs_[D6] = nullptr;
124 fprs_[D7] = nullptr;
Andreas Gampe6cf80102014-05-19 11:32:41 -0700125
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200126 fprs_[D16] = nullptr;
127 fprs_[D17] = nullptr;
128 fprs_[D18] = nullptr;
129 fprs_[D19] = nullptr;
130 fprs_[D20] = nullptr;
131 fprs_[D21] = nullptr;
132 fprs_[D22] = nullptr;
133 fprs_[D23] = nullptr;
134 fprs_[D24] = nullptr;
135 fprs_[D25] = nullptr;
136 fprs_[D26] = nullptr;
137 fprs_[D27] = nullptr;
138 fprs_[D28] = nullptr;
139 fprs_[D29] = nullptr;
140 fprs_[D30] = nullptr;
141 fprs_[D31] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000142}
143
144extern "C" void art_quick_do_long_jump(uint64_t*, uint64_t*);
145
146void Arm64Context::DoLongJump() {
Alexandre Rames37c92df2014-10-17 14:35:27 +0100147 uint64_t gprs[kNumberOfXRegisters];
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200148 uint64_t fprs[kNumberOfDRegisters];
Stuart Monteithb95a5342014-03-12 13:32:32 +0000149
Alexandre Ramesa304f972014-10-17 14:35:27 +0100150 // The long jump routine called below expects to find the value for SP at index 31.
151 DCHECK_EQ(SP, 31);
152
Alexandre Rames37c92df2014-10-17 14:35:27 +0100153 for (size_t i = 0; i < kNumberOfXRegisters; ++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