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 "mirror/art_method-inl.h"
20 #include "mirror/object-inl.h"
21 #include "quick/quick_method_frame_info.h"
22 #include "stack.h"
23 #include "thread.h"
24 
25 namespace art {
26 namespace arm {
27 
28 static constexpr uint32_t gZero = 0;
29 
Reset()30 void ArmContext::Reset() {
31   for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
32     gprs_[i] = nullptr;
33   }
34   for (size_t i = 0; i < kNumberOfSRegisters; i++) {
35     fprs_[i] = nullptr;
36   }
37   gprs_[SP] = &sp_;
38   gprs_[PC] = &pc_;
39   // Initialize registers with easy to spot debug values.
40   sp_ = ArmContext::kBadGprBase + SP;
41   pc_ = ArmContext::kBadGprBase + PC;
42 }
43 
FillCalleeSaves(const StackVisitor & fr)44 void ArmContext::FillCalleeSaves(const StackVisitor& fr) {
45   mirror::ArtMethod* method = fr.GetMethod();
46   const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo();
47   size_t spill_count = POPCOUNT(frame_info.CoreSpillMask());
48   size_t fp_spill_count = POPCOUNT(frame_info.FpSpillMask());
49   if (spill_count > 0) {
50     // Lowest number spill is farthest away, walk registers and fill into context
51     int j = 1;
52     for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
53       if (((frame_info.CoreSpillMask() >> i) & 1) != 0) {
54         gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_info.FrameSizeInBytes());
55         j++;
56       }
57     }
58   }
59   if (fp_spill_count > 0) {
60     // Lowest number spill is farthest away, walk registers and fill into context
61     int j = 1;
62     for (size_t i = 0; i < kNumberOfSRegisters; i++) {
63       if (((frame_info.FpSpillMask() >> i) & 1) != 0) {
64         fprs_[i] = fr.CalleeSaveAddress(spill_count + fp_spill_count - j,
65                                         frame_info.FrameSizeInBytes());
66         j++;
67       }
68     }
69   }
70 }
71 
SetGPR(uint32_t reg,uintptr_t value)72 bool ArmContext::SetGPR(uint32_t reg, uintptr_t value) {
73   DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
74   DCHECK_NE(gprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
75   if (gprs_[reg] != nullptr) {
76     *gprs_[reg] = value;
77     return true;
78   } else {
79     return false;
80   }
81 }
82 
SetFPR(uint32_t reg,uintptr_t value)83 bool ArmContext::SetFPR(uint32_t reg, uintptr_t value) {
84   DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfSRegisters));
85   DCHECK_NE(fprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
86   if (fprs_[reg] != nullptr) {
87     *fprs_[reg] = value;
88     return true;
89   } else {
90     return false;
91   }
92 }
93 
SmashCallerSaves()94 void ArmContext::SmashCallerSaves() {
95   // This needs to be 0 because we want a null/zero return value.
96   gprs_[R0] = const_cast<uint32_t*>(&gZero);
97   gprs_[R1] = const_cast<uint32_t*>(&gZero);
98   gprs_[R2] = nullptr;
99   gprs_[R3] = nullptr;
100 }
101 
102 extern "C" void art_quick_do_long_jump(uint32_t*, uint32_t*);
103 
DoLongJump()104 void ArmContext::DoLongJump() {
105   uintptr_t gprs[kNumberOfCoreRegisters];
106   uint32_t fprs[kNumberOfSRegisters];
107   for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
108     gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : ArmContext::kBadGprBase + i;
109   }
110   for (size_t i = 0; i < kNumberOfSRegisters; ++i) {
111     fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : ArmContext::kBadFprBase + i;
112   }
113   DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]);
114   art_quick_do_long_jump(gprs, fprs);
115 }
116 
117 }  // namespace arm
118 }  // namespace art
119