blob: bac3001181a3a8648d511efa0c668ef54ae0a26b [file] [log] [blame]
Vladimir Markob163bb72015-03-31 21:49:49 +01001/*
2 * Copyright (C) 2015 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 "linker/arm64/relative_patcher_arm64.h"
18
19#include "arch/arm64/instruction_set_features_arm64.h"
20#include "compiled_method.h"
21#include "driver/compiler_driver.h"
22#include "mirror/art_method.h"
23#include "utils/arm64/assembler_arm64.h"
24#include "oat.h"
25#include "output_stream.h"
26
27namespace art {
28namespace linker {
29
30Arm64RelativePatcher::Arm64RelativePatcher(RelativePatcherTargetProvider* provider,
31 const Arm64InstructionSetFeatures* features)
32 : ArmBaseRelativePatcher(provider, kArm64, CompileThunkCode(),
33 kMaxPositiveDisplacement, kMaxNegativeDisplacement),
34 fix_cortex_a53_843419_(features->NeedFixCortexA53_843419()),
35 reserved_adrp_thunks_(0u),
36 processed_adrp_thunks_(0u) {
37 if (fix_cortex_a53_843419_) {
38 adrp_thunk_locations_.reserve(16u);
39 current_method_thunks_.reserve(16u * kAdrpThunkSize);
40 }
41}
42
43uint32_t Arm64RelativePatcher::ReserveSpace(uint32_t offset,
44 const CompiledMethod* compiled_method) {
45 if (!fix_cortex_a53_843419_) {
46 DCHECK(adrp_thunk_locations_.empty());
47 return ReserveSpaceInternal(offset, compiled_method, 0u);
48 }
49
50 // Add thunks for previous method if any.
51 if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
52 size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
53 offset = CompiledMethod::AlignCode(offset, kArm64) + kAdrpThunkSize * num_adrp_thunks;
54 reserved_adrp_thunks_ = adrp_thunk_locations_.size();
55 }
56
57 // Count the number of ADRP insns as the upper bound on the number of thunks needed
58 // and use it to reserve space for other linker patches.
59 size_t num_adrp = 0u;
60 if (LIKELY(compiled_method != nullptr)) {
61 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
62 if (patch.Type() == kLinkerPatchDexCacheArray &&
63 patch.LiteralOffset() == patch.PcInsnOffset()) { // ADRP patch
64 ++num_adrp;
65 }
66 }
67 }
68 offset = ReserveSpaceInternal(offset, compiled_method, kAdrpThunkSize * num_adrp);
69 if (num_adrp == 0u) {
70 return offset;
71 }
72
73 // Now that we have the actual offset where the code will be placed, locate the ADRP insns
74 // that actually require the thunk.
75 uint32_t quick_code_offset = compiled_method->AlignCode(offset) + sizeof(OatQuickMethodHeader);
76 ArrayRef<const uint8_t> code(*compiled_method->GetQuickCode());
77 uint32_t thunk_offset = compiled_method->AlignCode(quick_code_offset + code.size());
78 DCHECK(compiled_method != nullptr);
79 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
80 if (patch.Type() == kLinkerPatchDexCacheArray &&
81 patch.LiteralOffset() == patch.PcInsnOffset()) { // ADRP patch
82 uint32_t patch_offset = quick_code_offset + patch.LiteralOffset();
83 if (NeedsErratum843419Thunk(code, patch.LiteralOffset(), patch_offset)) {
84 adrp_thunk_locations_.emplace_back(patch_offset, thunk_offset);
85 thunk_offset += kAdrpThunkSize;
86 }
87 }
88 }
89 return offset;
90}
91
92uint32_t Arm64RelativePatcher::WriteThunks(OutputStream* out, uint32_t offset) {
93 if (fix_cortex_a53_843419_) {
94 if (!current_method_thunks_.empty()) {
95 uint32_t aligned_offset = CompiledMethod::AlignCode(offset, kArm64);
96 if (kIsDebugBuild) {
97 CHECK(IsAligned<kAdrpThunkSize>(current_method_thunks_.size()));
98 size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
99 CHECK_LE(num_thunks, processed_adrp_thunks_);
100 for (size_t i = 0u; i != num_thunks; ++i) {
101 const auto& entry = adrp_thunk_locations_[processed_adrp_thunks_ - num_thunks + i];
102 CHECK_EQ(entry.second, aligned_offset + i * kAdrpThunkSize);
103 }
104 }
105 uint32_t aligned_code_delta = aligned_offset - offset;
106 if (aligned_code_delta != 0u && !WriteCodeAlignment(out, aligned_code_delta)) {
107 return 0u;
108 }
109 if (!WriteMiscThunk(out, ArrayRef<const uint8_t>(current_method_thunks_))) {
110 return 0u;
111 }
112 offset = aligned_offset + current_method_thunks_.size();
113 current_method_thunks_.clear();
114 }
115 }
116 return ArmBaseRelativePatcher::WriteThunks(out, offset);
117}
118
119void Arm64RelativePatcher::PatchCall(std::vector<uint8_t>* code, uint32_t literal_offset,
120 uint32_t patch_offset, uint32_t target_offset) {
121 DCHECK_LE(literal_offset + 4u, code->size());
122 DCHECK_EQ(literal_offset & 3u, 0u);
123 DCHECK_EQ(patch_offset & 3u, 0u);
124 DCHECK_EQ(target_offset & 3u, 0u);
125 uint32_t displacement = CalculateDisplacement(patch_offset, target_offset & ~1u);
126 DCHECK_EQ(displacement & 3u, 0u);
127 DCHECK((displacement >> 27) == 0u || (displacement >> 27) == 31u); // 28-bit signed.
128 uint32_t insn = (displacement & 0x0fffffffu) >> 2;
129 insn |= 0x94000000; // BL
130
131 // Check that we're just overwriting an existing BL.
132 DCHECK_EQ(GetInsn(code, literal_offset) & 0xfc000000u, 0x94000000u);
133 // Write the new BL.
134 SetInsn(code, literal_offset, insn);
135}
136
137void Arm64RelativePatcher::PatchDexCacheReference(std::vector<uint8_t>* code,
138 const LinkerPatch& patch,
139 uint32_t patch_offset,
140 uint32_t target_offset) {
141 DCHECK_EQ(patch_offset & 3u, 0u);
142 DCHECK_EQ(target_offset & 3u, 0u);
143 uint32_t literal_offset = patch.LiteralOffset();
144 uint32_t insn = GetInsn(code, literal_offset);
145 uint32_t pc_insn_offset = patch.PcInsnOffset();
146 uint32_t disp = target_offset - ((patch_offset - literal_offset + pc_insn_offset) & ~0xfffu);
147 if (literal_offset == pc_insn_offset) {
148 // Check it's an ADRP with imm == 0 (unset).
149 DCHECK_EQ((insn & 0xffffffe0u), 0x90000000u)
150 << literal_offset << ", " << pc_insn_offset << ", 0x" << std::hex << insn;
151 if (fix_cortex_a53_843419_ && processed_adrp_thunks_ != adrp_thunk_locations_.size() &&
152 adrp_thunk_locations_[processed_adrp_thunks_].first == patch_offset) {
153 DCHECK(NeedsErratum843419Thunk(ArrayRef<const uint8_t>(*code),
154 literal_offset, patch_offset));
155 uint32_t thunk_offset = adrp_thunk_locations_[processed_adrp_thunks_].second;
156 uint32_t adrp_disp = target_offset - (thunk_offset & ~0xfffu);
157 uint32_t adrp = PatchAdrp(insn, adrp_disp);
158
159 uint32_t out_disp = thunk_offset - patch_offset;
160 DCHECK_EQ(out_disp & 3u, 0u);
161 DCHECK((out_disp >> 27) == 0u || (out_disp >> 27) == 31u); // 28-bit signed.
162 insn = (out_disp & 0x0fffffffu) >> 2;
163 insn |= 0x14000000; // B <thunk>
164
165 uint32_t back_disp = -out_disp;
166 DCHECK_EQ(back_disp & 3u, 0u);
167 DCHECK((back_disp >> 27) == 0u || (back_disp >> 27) == 31u); // 28-bit signed.
168 uint32_t b_back = (back_disp & 0x0fffffffu) >> 2;
169 b_back |= 0x14000000; // B <back>
170 size_t thunks_code_offset = current_method_thunks_.size();
171 current_method_thunks_.resize(thunks_code_offset + kAdrpThunkSize);
172 SetInsn(&current_method_thunks_, thunks_code_offset, adrp);
173 SetInsn(&current_method_thunks_, thunks_code_offset + 4u, b_back);
174 static_assert(kAdrpThunkSize == 2 * 4u, "thunk has 2 instructions");
175
176 processed_adrp_thunks_ += 1u;
177 } else {
178 insn = PatchAdrp(insn, disp);
179 }
180 // Write the new ADRP (or B to the erratum 843419 thunk).
181 SetInsn(code, literal_offset, insn);
182 } else {
183 DCHECK_EQ(insn & 0xfffffc00, 0xb9400000); // LDR 32-bit with imm12 == 0 (unset).
184 if (kIsDebugBuild) {
185 uint32_t adrp = GetInsn(code, pc_insn_offset);
186 if ((adrp & 0x9f000000u) != 0x90000000u) {
187 CHECK(fix_cortex_a53_843419_);
188 CHECK_EQ(adrp & 0xfc000000u, 0x14000000u); // B <thunk>
189 CHECK(IsAligned<kAdrpThunkSize>(current_method_thunks_.size()));
190 size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
191 CHECK_LE(num_thunks, processed_adrp_thunks_);
192 uint32_t b_offset = patch_offset - literal_offset + pc_insn_offset;
193 for (size_t i = processed_adrp_thunks_ - num_thunks; ; ++i) {
194 CHECK_NE(i, processed_adrp_thunks_);
195 if (adrp_thunk_locations_[i].first == b_offset) {
196 size_t idx = num_thunks - (processed_adrp_thunks_ - i);
197 adrp = GetInsn(&current_method_thunks_, idx * kAdrpThunkSize);
198 break;
199 }
200 }
201 }
202 CHECK_EQ(adrp & 0x9f00001fu, // Check that pc_insn_offset points
203 0x90000000 | ((insn >> 5) & 0x1fu)); // to ADRP with matching register.
204 }
205 uint32_t imm12 = (disp & 0xfffu) >> 2;
206 insn = (insn & ~(0xfffu << 10)) | (imm12 << 10);
207 SetInsn(code, literal_offset, insn);
208 }
209}
210
211std::vector<uint8_t> Arm64RelativePatcher::CompileThunkCode() {
212 // The thunk just uses the entry point in the ArtMethod. This works even for calls
213 // to the generic JNI and interpreter trampolines.
214 arm64::Arm64Assembler assembler;
215 Offset offset(mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
216 kArm64PointerSize).Int32Value());
217 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
218 // Ensure we emit the literal pool.
219 assembler.EmitSlowPaths();
220 std::vector<uint8_t> thunk_code(assembler.CodeSize());
221 MemoryRegion code(thunk_code.data(), thunk_code.size());
222 assembler.FinalizeInstructions(code);
223 return thunk_code;
224}
225
226uint32_t Arm64RelativePatcher::PatchAdrp(uint32_t adrp, uint32_t disp) {
227 return (adrp & 0x9f00001fu) | // Clear offset bits, keep ADRP with destination reg.
228 // Bottom 12 bits are ignored, the next 2 lowest bits are encoded in bits 29-30.
229 ((disp & 0x00003000u) << (29 - 12)) |
230 // The next 16 bits are encoded in bits 5-22.
231 ((disp & 0xffffc000u) >> (12 + 2 - 5)) |
232 // Since the target_offset is based on the beginning of the oat file and the
233 // image space precedes the oat file, the target_offset into image space will
234 // be negative yet passed as uint32_t. Therefore we limit the displacement
235 // to +-2GiB (rather than the maximim +-4GiB) and determine the sign bit from
236 // the highest bit of the displacement. This is encoded in bit 23.
237 ((disp & 0x80000000u) >> (31 - 23));
238}
239
240bool Arm64RelativePatcher::NeedsErratum843419Thunk(ArrayRef<const uint8_t> code,
241 uint32_t literal_offset,
242 uint32_t patch_offset) {
243 DCHECK_EQ(patch_offset & 0x3u, 0u);
244 if ((patch_offset & 0xff8) == 0xff8) { // ...ff8 or ...ffc
245 uint32_t adrp = GetInsn(code, literal_offset);
246 DCHECK_EQ(adrp & 0xff000000, 0x90000000);
247 // TODO: Improve the check. For now, we're just checking if the next insn is
248 // the LDR using the result of the ADRP, otherwise we implement the workaround.
249 uint32_t next_insn = GetInsn(code, literal_offset + 4u);
250 bool ok = (next_insn & 0xffc00000) == 0xb9400000 && // LDR <Wt>, [<Xn>, #pimm]
251 (((next_insn >> 5) ^ adrp) & 0x1f) == 0; // <Xn> == ADRP destination reg
252 return !ok;
253 }
254 return false;
255}
256
257void Arm64RelativePatcher::SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) {
258 DCHECK_LE(offset + 4u, code->size());
259 DCHECK_EQ(offset & 3u, 0u);
260 uint8_t* addr = &(*code)[offset];
261 addr[0] = (value >> 0) & 0xff;
262 addr[1] = (value >> 8) & 0xff;
263 addr[2] = (value >> 16) & 0xff;
264 addr[3] = (value >> 24) & 0xff;
265}
266
267uint32_t Arm64RelativePatcher::GetInsn(ArrayRef<const uint8_t> code, uint32_t offset) {
268 DCHECK_LE(offset + 4u, code.size());
269 DCHECK_EQ(offset & 3u, 0u);
270 const uint8_t* addr = &code[offset];
271 return
272 (static_cast<uint32_t>(addr[0]) << 0) +
273 (static_cast<uint32_t>(addr[1]) << 8) +
274 (static_cast<uint32_t>(addr[2]) << 16)+
275 (static_cast<uint32_t>(addr[3]) << 24);
276}
277
278template <typename Alloc>
279uint32_t Arm64RelativePatcher::GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset) {
280 return GetInsn(ArrayRef<const uint8_t>(*code), offset);
281}
282
283} // namespace linker
284} // namespace art