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