blob: 4477631c675ff4e25dedc07275f44589c42c73b3 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010023#include "quick/quick_method_frame_info.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() {
Vladimir Marko80afd022015-05-19 18:08:00 +010031 std::fill_n(gprs_, arraysize(gprs_), nullptr);
32 std::fill_n(fprs_, arraysize(fprs_), nullptr);
Stuart Monteithb95a5342014-03-12 13:32:32 +000033 gprs_[SP] = &sp_;
Andreas Gampe639bdd12015-06-03 11:22:45 -070034 gprs_[kPC] = &pc_;
35 gprs_[X0] = &arg0_;
Stuart Monteithb95a5342014-03-12 13:32:32 +000036 // Initialize registers with easy to spot debug values.
37 sp_ = Arm64Context::kBadGprBase + SP;
Andreas Gampe639bdd12015-06-03 11:22:45 -070038 pc_ = Arm64Context::kBadGprBase + kPC;
39 arg0_ = 0;
Stuart Monteithb95a5342014-03-12 13:32:32 +000040}
41
42void Arm64Context::FillCalleeSaves(const StackVisitor& fr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070043 ArtMethod* method = fr.GetMethod();
Vladimir Marko7624d252014-05-02 14:40:15 +010044 const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo();
Vladimir Marko80afd022015-05-19 18:08:00 +010045 int spill_pos = 0;
Stuart Monteithb95a5342014-03-12 13:32:32 +000046
Vladimir Marko80afd022015-05-19 18:08:00 +010047 // Core registers come first, from the highest down to the lowest.
48 for (uint32_t core_reg : HighToLowBits(frame_info.CoreSpillMask())) {
49 gprs_[core_reg] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes());
50 ++spill_pos;
Stuart Monteithb95a5342014-03-12 13:32:32 +000051 }
Vladimir Marko80afd022015-05-19 18:08:00 +010052 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()));
53
54 // FP registers come second, from the highest down to the lowest.
55 for (uint32_t fp_reg : HighToLowBits(frame_info.FpSpillMask())) {
56 fprs_[fp_reg] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes());
57 ++spill_pos;
58 }
59 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) + POPCOUNT(frame_info.FpSpillMask()));
Stuart Monteithb95a5342014-03-12 13:32:32 +000060}
61
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010062void Arm64Context::SetGPR(uint32_t reg, uintptr_t value) {
Andreas Gampe639bdd12015-06-03 11:22:45 -070063 DCHECK_LT(reg, arraysize(gprs_));
64 // Note: we use kPC == XZR, so do not ensure that reg != XZR.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010065 DCHECK(IsAccessibleGPR(reg));
Stuart Monteithb95a5342014-03-12 13:32:32 +000066 DCHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010067 *gprs_[reg] = value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020068}
69
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010070void Arm64Context::SetFPR(uint32_t reg, uintptr_t value) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020071 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfDRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010072 DCHECK(IsAccessibleFPR(reg));
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020073 DCHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010074 *fprs_[reg] = value;
Stuart Monteithb95a5342014-03-12 13:32:32 +000075}
76
77void Arm64Context::SmashCallerSaves() {
78 // This needs to be 0 because we want a null/zero return value.
79 gprs_[X0] = const_cast<uint64_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020080 gprs_[X1] = nullptr;
81 gprs_[X2] = nullptr;
82 gprs_[X3] = nullptr;
83 gprs_[X4] = nullptr;
84 gprs_[X5] = nullptr;
85 gprs_[X6] = nullptr;
86 gprs_[X7] = nullptr;
87 gprs_[X8] = nullptr;
88 gprs_[X9] = nullptr;
89 gprs_[X10] = nullptr;
90 gprs_[X11] = nullptr;
91 gprs_[X12] = nullptr;
92 gprs_[X13] = nullptr;
93 gprs_[X14] = nullptr;
94 gprs_[X15] = nullptr;
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010095 gprs_[X18] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +000096
Andreas Gampe6cf80102014-05-19 11:32:41 -070097 // d0-d7, d16-d31 are caller-saved; d8-d15 are callee-saved.
98
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020099 fprs_[D0] = nullptr;
100 fprs_[D1] = nullptr;
101 fprs_[D2] = nullptr;
102 fprs_[D3] = nullptr;
103 fprs_[D4] = nullptr;
104 fprs_[D5] = nullptr;
105 fprs_[D6] = nullptr;
106 fprs_[D7] = nullptr;
Andreas Gampe6cf80102014-05-19 11:32:41 -0700107
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200108 fprs_[D16] = nullptr;
109 fprs_[D17] = nullptr;
110 fprs_[D18] = nullptr;
111 fprs_[D19] = nullptr;
112 fprs_[D20] = nullptr;
113 fprs_[D21] = nullptr;
114 fprs_[D22] = nullptr;
115 fprs_[D23] = nullptr;
116 fprs_[D24] = nullptr;
117 fprs_[D25] = nullptr;
118 fprs_[D26] = nullptr;
119 fprs_[D27] = nullptr;
120 fprs_[D28] = nullptr;
121 fprs_[D29] = nullptr;
122 fprs_[D30] = nullptr;
123 fprs_[D31] = nullptr;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000124}
125
Andreas Gampe794ad762015-02-23 08:12:24 -0800126extern "C" NO_RETURN void art_quick_do_long_jump(uint64_t*, uint64_t*);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000127
128void Arm64Context::DoLongJump() {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700129 uint64_t gprs[arraysize(gprs_)];
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200130 uint64_t fprs[kNumberOfDRegisters];
Stuart Monteithb95a5342014-03-12 13:32:32 +0000131
Alexandre Ramesa304f972014-10-17 14:35:27 +0100132 // The long jump routine called below expects to find the value for SP at index 31.
133 DCHECK_EQ(SP, 31);
134
Andreas Gampe639bdd12015-06-03 11:22:45 -0700135 for (size_t i = 0; i < arraysize(gprs_); ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200136 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : Arm64Context::kBadGprBase + i;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000137 }
138 for (size_t i = 0; i < kNumberOfDRegisters; ++i) {
Vladimir Marko5b09ea02015-05-27 14:07:08 +0100139 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : Arm64Context::kBadFprBase + i;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000140 }
141 DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]);
142 art_quick_do_long_jump(gprs, fprs);
143}
144
145} // namespace arm64
146} // namespace art