xref: /aosp_15_r20/art/runtime/arch/arm/context_arm.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "context_arm.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
20*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils_iterator.h"
21*795d594fSAndroid Build Coastguard Worker #include "quick/quick_method_frame_info.h"
22*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
25*795d594fSAndroid Build Coastguard Worker namespace arm {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t gZero = 0;
28*795d594fSAndroid Build Coastguard Worker 
Reset()29*795d594fSAndroid Build Coastguard Worker void ArmContext::Reset() {
30*795d594fSAndroid Build Coastguard Worker   std::fill_n(gprs_, arraysize(gprs_), nullptr);
31*795d594fSAndroid Build Coastguard Worker   std::fill_n(fprs_, arraysize(fprs_), nullptr);
32*795d594fSAndroid Build Coastguard Worker   gprs_[SP] = &sp_;
33*795d594fSAndroid Build Coastguard Worker   gprs_[PC] = &pc_;
34*795d594fSAndroid Build Coastguard Worker   gprs_[R0] = &arg0_;
35*795d594fSAndroid Build Coastguard Worker   // Initialize registers with easy to spot debug values.
36*795d594fSAndroid Build Coastguard Worker   sp_ = kBadGprBase + SP;
37*795d594fSAndroid Build Coastguard Worker   pc_ = kBadGprBase + PC;
38*795d594fSAndroid Build Coastguard Worker   arg0_ = 0;
39*795d594fSAndroid Build Coastguard Worker }
40*795d594fSAndroid Build Coastguard Worker 
FillCalleeSaves(uint8_t * frame,const QuickMethodFrameInfo & frame_info)41*795d594fSAndroid Build Coastguard Worker void ArmContext::FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& frame_info) {
42*795d594fSAndroid Build Coastguard Worker   const size_t frame_size = frame_info.FrameSizeInBytes();
43*795d594fSAndroid Build Coastguard Worker   int spill_pos = 0;
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker   // Core registers come first, from the highest down to the lowest.
46*795d594fSAndroid Build Coastguard Worker   uint32_t core_regs = frame_info.CoreSpillMask();
47*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(0u, core_regs & (static_cast<uint32_t>(-1) << kNumberOfCoreRegisters));
48*795d594fSAndroid Build Coastguard Worker   for (uint32_t core_reg : HighToLowBits(core_regs)) {
49*795d594fSAndroid Build Coastguard Worker     gprs_[core_reg] = CalleeSaveAddress<InstructionSet::kArm>(frame, spill_pos, frame_size);
50*795d594fSAndroid Build Coastguard Worker     ++spill_pos;
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()));
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   // FP registers come second, from the highest down to the lowest.
55*795d594fSAndroid Build Coastguard Worker   for (uint32_t fp_reg : HighToLowBits(frame_info.FpSpillMask())) {
56*795d594fSAndroid Build Coastguard Worker     fprs_[fp_reg] = CalleeSaveAddress<InstructionSet::kArm>(frame, spill_pos, frame_size);
57*795d594fSAndroid Build Coastguard Worker     ++spill_pos;
58*795d594fSAndroid Build Coastguard Worker   }
59*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) + POPCOUNT(frame_info.FpSpillMask()));
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker 
SetGPR(uint32_t reg,uintptr_t value)62*795d594fSAndroid Build Coastguard Worker void ArmContext::SetGPR(uint32_t reg, uintptr_t value) {
63*795d594fSAndroid Build Coastguard Worker   DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
64*795d594fSAndroid Build Coastguard Worker   DCHECK(IsAccessibleGPR(reg));
65*795d594fSAndroid Build Coastguard Worker   DCHECK_NE(gprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
66*795d594fSAndroid Build Coastguard Worker   *gprs_[reg] = value;
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker 
SetFPR(uint32_t reg,uintptr_t value)69*795d594fSAndroid Build Coastguard Worker void ArmContext::SetFPR(uint32_t reg, uintptr_t value) {
70*795d594fSAndroid Build Coastguard Worker   DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfSRegisters));
71*795d594fSAndroid Build Coastguard Worker   DCHECK(IsAccessibleFPR(reg));
72*795d594fSAndroid Build Coastguard Worker   DCHECK_NE(fprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
73*795d594fSAndroid Build Coastguard Worker   *fprs_[reg] = value;
74*795d594fSAndroid Build Coastguard Worker }
75*795d594fSAndroid Build Coastguard Worker 
SmashCallerSaves()76*795d594fSAndroid Build Coastguard Worker void ArmContext::SmashCallerSaves() {
77*795d594fSAndroid Build Coastguard Worker   // This needs to be 0 because we want a null/zero return value.
78*795d594fSAndroid Build Coastguard Worker   gprs_[R0] = const_cast<uint32_t*>(&gZero);
79*795d594fSAndroid Build Coastguard Worker   gprs_[R1] = const_cast<uint32_t*>(&gZero);
80*795d594fSAndroid Build Coastguard Worker   gprs_[R2] = nullptr;
81*795d594fSAndroid Build Coastguard Worker   gprs_[R3] = nullptr;
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker   fprs_[S0] = nullptr;
84*795d594fSAndroid Build Coastguard Worker   fprs_[S1] = nullptr;
85*795d594fSAndroid Build Coastguard Worker   fprs_[S2] = nullptr;
86*795d594fSAndroid Build Coastguard Worker   fprs_[S3] = nullptr;
87*795d594fSAndroid Build Coastguard Worker   fprs_[S4] = nullptr;
88*795d594fSAndroid Build Coastguard Worker   fprs_[S5] = nullptr;
89*795d594fSAndroid Build Coastguard Worker   fprs_[S6] = nullptr;
90*795d594fSAndroid Build Coastguard Worker   fprs_[S7] = nullptr;
91*795d594fSAndroid Build Coastguard Worker   fprs_[S8] = nullptr;
92*795d594fSAndroid Build Coastguard Worker   fprs_[S9] = nullptr;
93*795d594fSAndroid Build Coastguard Worker   fprs_[S10] = nullptr;
94*795d594fSAndroid Build Coastguard Worker   fprs_[S11] = nullptr;
95*795d594fSAndroid Build Coastguard Worker   fprs_[S12] = nullptr;
96*795d594fSAndroid Build Coastguard Worker   fprs_[S13] = nullptr;
97*795d594fSAndroid Build Coastguard Worker   fprs_[S14] = nullptr;
98*795d594fSAndroid Build Coastguard Worker   fprs_[S15] = nullptr;
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker 
CopyContextTo(uintptr_t * gprs,uintptr_t * fprs)101*795d594fSAndroid Build Coastguard Worker void ArmContext::CopyContextTo(uintptr_t* gprs, uintptr_t* fprs) {
102*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
103*795d594fSAndroid Build Coastguard Worker     gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : kBadGprBase + i;
104*795d594fSAndroid Build Coastguard Worker   }
105*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < kNumberOfSRegisters; ++i) {
106*795d594fSAndroid Build Coastguard Worker     fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : kBadFprBase + i;
107*795d594fSAndroid Build Coastguard Worker   }
108*795d594fSAndroid Build Coastguard Worker   // Ensure the Thread Register contains the address of the current thread.
109*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]);
110*795d594fSAndroid Build Coastguard Worker   // The Marking Register will be updated after return by art_quick_do_long_jump.
111*795d594fSAndroid Build Coastguard Worker }
112*795d594fSAndroid Build Coastguard Worker 
113*795d594fSAndroid Build Coastguard Worker }  // namespace arm
114*795d594fSAndroid Build Coastguard Worker }  // namespace art
115