• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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_riscv64.h"
18 
19 #include <stdint.h>
20 
21 #include "base/bit_utils.h"
22 #include "base/bit_utils_iterator.h"
23 #include "quick/quick_method_frame_info.h"
24 #include "thread-current-inl.h"
25 
26 #if __has_feature(hwaddress_sanitizer)
27 #include <sanitizer/hwasan_interface.h>
28 #else
29 #define __hwasan_handle_longjmp(sp)
30 #endif
31 
32 namespace art {
33 namespace riscv64 {
34 
35 static constexpr uint64_t gZero = 0;
36 
Reset()37 void Riscv64Context::Reset() {
38   std::fill_n(gprs_, arraysize(gprs_), nullptr);
39   std::fill_n(fprs_, arraysize(fprs_), nullptr);
40   gprs_[SP] = &sp_;
41   gprs_[kPC] = &pc_;
42   gprs_[A0] = &arg0_;
43   // Initialize registers with easy to spot debug values.
44   sp_ = kBadGprBase + SP;
45   pc_ = kBadGprBase + kPC;
46   arg0_ = 0;
47 }
48 
FillCalleeSaves(uint8_t * frame,const QuickMethodFrameInfo & frame_info)49 void Riscv64Context::FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& frame_info) {
50   // RA is at top of the frame
51   DCHECK_NE(frame_info.CoreSpillMask() & ~(1u << RA), 0u);
52   gprs_[RA] = CalleeSaveAddress(frame, 0, frame_info.FrameSizeInBytes());
53 
54   // Core registers come first, from the highest down to the lowest, with the exception of RA/X1.
55   int spill_pos = 1;
56   for (uint32_t core_reg : HighToLowBits(frame_info.CoreSpillMask() & ~(1u << RA))) {
57     gprs_[core_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
58     ++spill_pos;
59   }
60   DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()));
61 
62   // FP registers come second, from the highest down to the lowest.
63   for (uint32_t fp_reg : HighToLowBits(frame_info.FpSpillMask())) {
64     fprs_[fp_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
65     ++spill_pos;
66   }
67   DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) + POPCOUNT(frame_info.FpSpillMask()));
68 }
69 
SetGPR(uint32_t reg,uintptr_t value)70 void Riscv64Context::SetGPR(uint32_t reg, uintptr_t value) {
71   DCHECK_LT(reg, arraysize(gprs_));
72   DCHECK_NE(reg, static_cast<uint32_t>(Zero));  // Zero/X0 is immutable (hard-wired zero)
73   DCHECK(IsAccessibleGPR(reg));
74   DCHECK_NE(gprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
75   *gprs_[reg] = value;
76 }
77 
SetFPR(uint32_t reg,uintptr_t value)78 void Riscv64Context::SetFPR(uint32_t reg, uintptr_t value) {
79   DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfFRegisters));
80   DCHECK(IsAccessibleFPR(reg));
81   DCHECK_NE(fprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
82   *fprs_[reg] = value;
83 }
84 
SmashCallerSaves()85 void Riscv64Context::SmashCallerSaves() {
86   // Temporary registers T0 - T6 and argument registers A0 - A7 are caller-saved.
87   gprs_[Zero] = const_cast<uint64_t*>(&gZero);  // hard-wired zero
88   gprs_[T0] = nullptr;
89   gprs_[T1] = nullptr;
90   gprs_[T2] = nullptr;
91   gprs_[T3] = nullptr;
92   gprs_[T4] = nullptr;
93   gprs_[T5] = nullptr;
94   gprs_[T6] = nullptr;
95   gprs_[A0] = const_cast<uint64_t*>(&gZero);  // must be 0 because we want a null/zero return value
96   gprs_[A1] = nullptr;
97   gprs_[A2] = nullptr;
98   gprs_[A3] = nullptr;
99   gprs_[A4] = nullptr;
100   gprs_[A5] = nullptr;
101   gprs_[A6] = nullptr;
102   gprs_[A7] = nullptr;
103 
104   // Temporary registers FT0 - FT11 and argument registers FA0 - FA7 are caller-saved.
105   fprs_[FT0] = nullptr;
106   fprs_[FT1] = nullptr;
107   fprs_[FT2] = nullptr;
108   fprs_[FT3] = nullptr;
109   fprs_[FT4] = nullptr;
110   fprs_[FT5] = nullptr;
111   fprs_[FT6] = nullptr;
112   fprs_[FT7] = nullptr;
113   fprs_[FT8] = nullptr;
114   fprs_[FT9] = nullptr;
115   fprs_[FT10] = nullptr;
116   fprs_[FT11] = nullptr;
117   fprs_[FA0] = nullptr;
118   fprs_[FA1] = nullptr;
119   fprs_[FA2] = nullptr;
120   fprs_[FA3] = nullptr;
121   fprs_[FA4] = nullptr;
122   fprs_[FA5] = nullptr;
123   fprs_[FA6] = nullptr;
124   fprs_[FA7] = nullptr;
125 }
126 
127 extern "C" NO_RETURN void art_quick_do_long_jump(uint64_t*, uint64_t*);
128 
DoLongJump()129 void Riscv64Context::DoLongJump() {
130   uint64_t gprs[arraysize(gprs_)];
131   uint64_t fprs[kNumberOfFRegisters];
132 
133   // The long jump routine called below expects to find the value for SP at index 2.
134   DCHECK_EQ(SP, 2);
135 
136   for (size_t i = 0; i < arraysize(gprs_); ++i) {
137     gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : kBadGprBase + i;
138   }
139   for (size_t i = 0; i < kNumberOfFRegisters; ++i) {
140     fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : kBadFprBase + i;
141   }
142 
143   // Fill in TR (the ART Thread Register) with the address of the current thread.
144   gprs[TR] = reinterpret_cast<uintptr_t>(Thread::Current());
145 
146   // Tell HWASan about the new stack top.
147   __hwasan_handle_longjmp(reinterpret_cast<void*>(gprs[SP]));
148   art_quick_do_long_jump(gprs, fprs);
149 }
150 
151 }  // namespace riscv64
152 }  // namespace art
153