blob: ec9c122fc340b1d669edb677cb84a544aef48889 [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 Hertz96ba8dc2015-01-22 18:57:14 +010073void 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));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010076 DCHECK(IsAccessibleGPR(reg));
Stuart Monteithb95a5342014-03-12 13:32:32 +000077 DCHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010078 *gprs_[reg] = value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020079}
80
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010081void Arm64Context::SetFPR(uint32_t reg, uintptr_t value) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020082 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfDRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010083 DCHECK(IsAccessibleFPR(reg));
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020084 DCHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010085 *fprs_[reg] = value;
Stuart Monteithb95a5342014-03-12 13:32:32 +000086}
87
88void Arm64Context::SmashCallerSaves() {
89 // This needs to be 0 because we want a null/zero return value.
90 gprs_[X0] = const_cast<uint64_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020091 gprs_[X1] = nullptr;
92 gprs_[X2] = nullptr;
93 gprs_[X3] = nullptr;
94 gprs_[X4] = nullptr;
95 gprs_[X5] = nullptr;
96 gprs_[X6] = nullptr;
97 gprs_[X7] = nullptr;
98 gprs_[X8] = nullptr;
99 gprs_[X9] = nullptr;
100 gprs_[X10] = nullptr;
101 gprs_[X11] = nullptr;
102 gprs_[X12] = nullptr;
103 gprs_[X13] = nullptr;
104 gprs_[X14] = nullptr;
105 gprs_[X15] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000106
Andreas Gampe6cf80102014-05-19 11:32:41 -0700107 // d0-d7, d16-d31 are caller-saved; d8-d15 are callee-saved.
108
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200109 fprs_[D0] = nullptr;
110 fprs_[D1] = nullptr;
111 fprs_[D2] = nullptr;
112 fprs_[D3] = nullptr;
113 fprs_[D4] = nullptr;
114 fprs_[D5] = nullptr;
115 fprs_[D6] = nullptr;
116 fprs_[D7] = nullptr;
Andreas Gampe6cf80102014-05-19 11:32:41 -0700117
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200118 fprs_[D16] = nullptr;
119 fprs_[D17] = nullptr;
120 fprs_[D18] = nullptr;
121 fprs_[D19] = nullptr;
122 fprs_[D20] = nullptr;
123 fprs_[D21] = nullptr;
124 fprs_[D22] = nullptr;
125 fprs_[D23] = nullptr;
126 fprs_[D24] = nullptr;
127 fprs_[D25] = nullptr;
128 fprs_[D26] = nullptr;
129 fprs_[D27] = nullptr;
130 fprs_[D28] = nullptr;
131 fprs_[D29] = nullptr;
132 fprs_[D30] = nullptr;
133 fprs_[D31] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000134}
135
Andreas Gampe794ad762015-02-23 08:12:24 -0800136extern "C" NO_RETURN void art_quick_do_long_jump(uint64_t*, uint64_t*);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000137
138void Arm64Context::DoLongJump() {
Alexandre Rames37c92df2014-10-17 14:35:27 +0100139 uint64_t gprs[kNumberOfXRegisters];
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200140 uint64_t fprs[kNumberOfDRegisters];
Stuart Monteithb95a5342014-03-12 13:32:32 +0000141
Alexandre Ramesa304f972014-10-17 14:35:27 +0100142 // The long jump routine called below expects to find the value for SP at index 31.
143 DCHECK_EQ(SP, 31);
144
Alexandre Rames37c92df2014-10-17 14:35:27 +0100145 for (size_t i = 0; i < kNumberOfXRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200146 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : Arm64Context::kBadGprBase + i;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000147 }
148 for (size_t i = 0; i < kNumberOfDRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200149 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : Arm64Context::kBadGprBase + i;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000150 }
151 DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]);
152 art_quick_do_long_jump(gprs, fprs);
153}
154
155} // namespace arm64
156} // namespace art