1 /*
2 * Copyright (C) 2011 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 "context_arm.h"
18
19 #include "base/bit_utils.h"
20 #include "base/bit_utils_iterator.h"
21 #include "quick/quick_method_frame_info.h"
22 #include "thread-current-inl.h"
23
24 namespace art HIDDEN {
25 namespace arm {
26
27 static constexpr uint32_t gZero = 0;
28
Reset()29 void ArmContext::Reset() {
30 std::fill_n(gprs_, arraysize(gprs_), nullptr);
31 std::fill_n(fprs_, arraysize(fprs_), nullptr);
32 gprs_[SP] = &sp_;
33 gprs_[PC] = &pc_;
34 gprs_[R0] = &arg0_;
35 // Initialize registers with easy to spot debug values.
36 sp_ = kBadGprBase + SP;
37 pc_ = kBadGprBase + PC;
38 arg0_ = 0;
39 }
40
FillCalleeSaves(uint8_t * frame,const QuickMethodFrameInfo & frame_info)41 void ArmContext::FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& frame_info) {
42 const size_t frame_size = frame_info.FrameSizeInBytes();
43 int spill_pos = 0;
44
45 // Core registers come first, from the highest down to the lowest.
46 uint32_t core_regs = frame_info.CoreSpillMask();
47 DCHECK_EQ(0u, core_regs & (static_cast<uint32_t>(-1) << kNumberOfCoreRegisters));
48 for (uint32_t core_reg : HighToLowBits(core_regs)) {
49 gprs_[core_reg] = CalleeSaveAddress<InstructionSet::kArm>(frame, spill_pos, frame_size);
50 ++spill_pos;
51 }
52 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] = CalleeSaveAddress<InstructionSet::kArm>(frame, spill_pos, frame_size);
57 ++spill_pos;
58 }
59 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) + POPCOUNT(frame_info.FpSpillMask()));
60 }
61
SetGPR(uint32_t reg,uintptr_t value)62 void ArmContext::SetGPR(uint32_t reg, uintptr_t value) {
63 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
64 DCHECK(IsAccessibleGPR(reg));
65 DCHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
66 *gprs_[reg] = value;
67 }
68
SetFPR(uint32_t reg,uintptr_t value)69 void ArmContext::SetFPR(uint32_t reg, uintptr_t value) {
70 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfSRegisters));
71 DCHECK(IsAccessibleFPR(reg));
72 DCHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
73 *fprs_[reg] = value;
74 }
75
SmashCallerSaves()76 void ArmContext::SmashCallerSaves() {
77 // This needs to be 0 because we want a null/zero return value.
78 gprs_[R0] = const_cast<uint32_t*>(&gZero);
79 gprs_[R1] = const_cast<uint32_t*>(&gZero);
80 gprs_[R2] = nullptr;
81 gprs_[R3] = nullptr;
82
83 fprs_[S0] = nullptr;
84 fprs_[S1] = nullptr;
85 fprs_[S2] = nullptr;
86 fprs_[S3] = nullptr;
87 fprs_[S4] = nullptr;
88 fprs_[S5] = nullptr;
89 fprs_[S6] = nullptr;
90 fprs_[S7] = nullptr;
91 fprs_[S8] = nullptr;
92 fprs_[S9] = nullptr;
93 fprs_[S10] = nullptr;
94 fprs_[S11] = nullptr;
95 fprs_[S12] = nullptr;
96 fprs_[S13] = nullptr;
97 fprs_[S14] = nullptr;
98 fprs_[S15] = nullptr;
99 }
100
CopyContextTo(uintptr_t * gprs,uintptr_t * fprs)101 void ArmContext::CopyContextTo(uintptr_t* gprs, uintptr_t* fprs) {
102 for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
103 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : kBadGprBase + i;
104 }
105 for (size_t i = 0; i < kNumberOfSRegisters; ++i) {
106 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : kBadFprBase + i;
107 }
108 // Ensure the Thread Register contains the address of the current thread.
109 DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]);
110 // The Marking Register will be updated after return by art_quick_do_long_jump.
111 }
112
113 } // namespace arm
114 } // namespace art
115