Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 21 | #include "art_method-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 22 | #include "base/bit_utils.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 23 | #include "quick/quick_method_frame_info.h" |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | namespace arm64 { |
| 27 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 28 | static constexpr uint64_t gZero = 0; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 29 | |
| 30 | void Arm64Context::Reset() { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 31 | std::fill_n(gprs_, arraysize(gprs_), nullptr); |
| 32 | std::fill_n(fprs_, arraysize(fprs_), nullptr); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 33 | gprs_[SP] = &sp_; |
| 34 | gprs_[LR] = &pc_; |
| 35 | // Initialize registers with easy to spot debug values. |
| 36 | sp_ = Arm64Context::kBadGprBase + SP; |
| 37 | pc_ = Arm64Context::kBadGprBase + LR; |
| 38 | } |
| 39 | |
| 40 | void Arm64Context::FillCalleeSaves(const StackVisitor& fr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 41 | ArtMethod* method = fr.GetMethod(); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 42 | const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo(); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 43 | int spill_pos = 0; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 44 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 45 | // Core registers come first, from the highest down to the lowest. |
| 46 | for (uint32_t core_reg : HighToLowBits(frame_info.CoreSpillMask())) { |
| 47 | gprs_[core_reg] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes()); |
| 48 | ++spill_pos; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 49 | } |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 50 | DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask())); |
| 51 | |
| 52 | // FP registers come second, from the highest down to the lowest. |
| 53 | for (uint32_t fp_reg : HighToLowBits(frame_info.FpSpillMask())) { |
| 54 | fprs_[fp_reg] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes()); |
| 55 | ++spill_pos; |
| 56 | } |
| 57 | DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) + POPCOUNT(frame_info.FpSpillMask())); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 60 | void Arm64Context::SetGPR(uint32_t reg, uintptr_t value) { |
Alexandre Rames | 37c92df | 2014-10-17 14:35:27 +0100 | [diff] [blame] | 61 | DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfXRegisters)); |
Alexandre Rames | a304f97 | 2014-10-17 14:35:27 +0100 | [diff] [blame] | 62 | DCHECK_NE(reg, static_cast<uint32_t>(XZR)); |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 63 | DCHECK(IsAccessibleGPR(reg)); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 64 | DCHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset. |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 65 | *gprs_[reg] = value; |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 68 | void Arm64Context::SetFPR(uint32_t reg, uintptr_t value) { |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 69 | DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfDRegisters)); |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 70 | DCHECK(IsAccessibleFPR(reg)); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 71 | DCHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset. |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 72 | *fprs_[reg] = value; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void Arm64Context::SmashCallerSaves() { |
| 76 | // This needs to be 0 because we want a null/zero return value. |
| 77 | gprs_[X0] = const_cast<uint64_t*>(&gZero); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 78 | gprs_[X1] = nullptr; |
| 79 | gprs_[X2] = nullptr; |
| 80 | gprs_[X3] = nullptr; |
| 81 | gprs_[X4] = nullptr; |
| 82 | gprs_[X5] = nullptr; |
| 83 | gprs_[X6] = nullptr; |
| 84 | gprs_[X7] = nullptr; |
| 85 | gprs_[X8] = nullptr; |
| 86 | gprs_[X9] = nullptr; |
| 87 | gprs_[X10] = nullptr; |
| 88 | gprs_[X11] = nullptr; |
| 89 | gprs_[X12] = nullptr; |
| 90 | gprs_[X13] = nullptr; |
| 91 | gprs_[X14] = nullptr; |
| 92 | gprs_[X15] = nullptr; |
Serban Constantinescu | 9bd88b0 | 2015-04-22 16:24:46 +0100 | [diff] [blame] | 93 | gprs_[X18] = nullptr; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 94 | |
Andreas Gampe | 6cf8010 | 2014-05-19 11:32:41 -0700 | [diff] [blame] | 95 | // d0-d7, d16-d31 are caller-saved; d8-d15 are callee-saved. |
| 96 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 97 | fprs_[D0] = nullptr; |
| 98 | fprs_[D1] = nullptr; |
| 99 | fprs_[D2] = nullptr; |
| 100 | fprs_[D3] = nullptr; |
| 101 | fprs_[D4] = nullptr; |
| 102 | fprs_[D5] = nullptr; |
| 103 | fprs_[D6] = nullptr; |
| 104 | fprs_[D7] = nullptr; |
Andreas Gampe | 6cf8010 | 2014-05-19 11:32:41 -0700 | [diff] [blame] | 105 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 106 | fprs_[D16] = nullptr; |
| 107 | fprs_[D17] = nullptr; |
| 108 | fprs_[D18] = nullptr; |
| 109 | fprs_[D19] = nullptr; |
| 110 | fprs_[D20] = nullptr; |
| 111 | fprs_[D21] = nullptr; |
| 112 | fprs_[D22] = nullptr; |
| 113 | fprs_[D23] = nullptr; |
| 114 | fprs_[D24] = nullptr; |
| 115 | fprs_[D25] = nullptr; |
| 116 | fprs_[D26] = nullptr; |
| 117 | fprs_[D27] = nullptr; |
| 118 | fprs_[D28] = nullptr; |
| 119 | fprs_[D29] = nullptr; |
| 120 | fprs_[D30] = nullptr; |
| 121 | fprs_[D31] = nullptr; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 124 | extern "C" NO_RETURN void art_quick_do_long_jump(uint64_t*, uint64_t*); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 125 | |
| 126 | void Arm64Context::DoLongJump() { |
Alexandre Rames | 37c92df | 2014-10-17 14:35:27 +0100 | [diff] [blame] | 127 | uint64_t gprs[kNumberOfXRegisters]; |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 128 | uint64_t fprs[kNumberOfDRegisters]; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 129 | |
Alexandre Rames | a304f97 | 2014-10-17 14:35:27 +0100 | [diff] [blame] | 130 | // The long jump routine called below expects to find the value for SP at index 31. |
| 131 | DCHECK_EQ(SP, 31); |
| 132 | |
Alexandre Rames | 37c92df | 2014-10-17 14:35:27 +0100 | [diff] [blame] | 133 | for (size_t i = 0; i < kNumberOfXRegisters; ++i) { |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 134 | gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : Arm64Context::kBadGprBase + i; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 135 | } |
| 136 | for (size_t i = 0; i < kNumberOfDRegisters; ++i) { |
Vladimir Marko | 5b09ea0 | 2015-05-27 14:07:08 +0100 | [diff] [blame] | 137 | fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : Arm64Context::kBadFprBase + i; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 138 | } |
| 139 | DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]); |
| 140 | art_quick_do_long_jump(gprs, fprs); |
| 141 | } |
| 142 | |
| 143 | } // namespace arm64 |
| 144 | } // namespace art |