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 {
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_ = ArmContext::kBadGprBase + SP;
37   pc_ = ArmContext::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   int spill_pos = 0;
43 
44   // Core registers come first, from the highest down to the lowest.
45   uint32_t core_regs = frame_info.CoreSpillMask();
46   DCHECK_EQ(0u, core_regs & (static_cast<uint32_t>(-1) << kNumberOfCoreRegisters));
47   for (uint32_t core_reg : HighToLowBits(core_regs)) {
48     gprs_[core_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
49     ++spill_pos;
50   }
51   DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()));
52 
53   // FP registers come second, from the highest down to the lowest.
54   for (uint32_t fp_reg : HighToLowBits(frame_info.FpSpillMask())) {
55     fprs_[fp_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
56     ++spill_pos;
57   }
58   DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) + POPCOUNT(frame_info.FpSpillMask()));
59 }
60 
SetGPR(uint32_t reg,uintptr_t value)61 void ArmContext::SetGPR(uint32_t reg, uintptr_t value) {
62   DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
63   DCHECK(IsAccessibleGPR(reg));
64   DCHECK_NE(gprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
65   *gprs_[reg] = value;
66 }
67 
SetFPR(uint32_t reg,uintptr_t value)68 void ArmContext::SetFPR(uint32_t reg, uintptr_t value) {
69   DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfSRegisters));
70   DCHECK(IsAccessibleFPR(reg));
71   DCHECK_NE(fprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
72   *fprs_[reg] = value;
73 }
74 
SmashCallerSaves()75 void ArmContext::SmashCallerSaves() {
76   // This needs to be 0 because we want a null/zero return value.
77   gprs_[R0] = const_cast<uint32_t*>(&gZero);
78   gprs_[R1] = const_cast<uint32_t*>(&gZero);
79   gprs_[R2] = nullptr;
80   gprs_[R3] = nullptr;
81 
82   fprs_[S0] = nullptr;
83   fprs_[S1] = nullptr;
84   fprs_[S2] = nullptr;
85   fprs_[S3] = nullptr;
86   fprs_[S4] = nullptr;
87   fprs_[S5] = nullptr;
88   fprs_[S6] = nullptr;
89   fprs_[S7] = nullptr;
90   fprs_[S8] = nullptr;
91   fprs_[S9] = nullptr;
92   fprs_[S10] = nullptr;
93   fprs_[S11] = nullptr;
94   fprs_[S12] = nullptr;
95   fprs_[S13] = nullptr;
96   fprs_[S14] = nullptr;
97   fprs_[S15] = nullptr;
98 }
99 
100 extern "C" NO_RETURN void art_quick_do_long_jump(uint32_t*, uint32_t*);
101 
DoLongJump()102 void ArmContext::DoLongJump() {
103   uintptr_t gprs[kNumberOfCoreRegisters];
104   uint32_t fprs[kNumberOfSRegisters];
105   for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
106     gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : ArmContext::kBadGprBase + i;
107   }
108   for (size_t i = 0; i < kNumberOfSRegisters; ++i) {
109     fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : ArmContext::kBadFprBase + i;
110   }
111   // Ensure the Thread Register contains the address of the current thread.
112   DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]);
113   // The Marking Register will be updated by art_quick_do_long_jump.
114   art_quick_do_long_jump(gprs, fprs);
115 }
116 
117 }  // namespace arm
118 }  // namespace art
119