blob: 9ddf200237014cb567a6daef0af983237d3092a5 [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010021#include "compiled_method.h"
22#include "driver/compiler_driver.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000023#include "linker/output_stream.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010024#include "oat.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010025#include "oat_quick_method_header.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000026#include "utils/arm64/assembler_arm64.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010027
28namespace art {
29namespace linker {
30
Vladimir Markocac5a7e2016-02-22 10:39:50 +000031namespace {
32
33inline bool IsAdrpPatch(const LinkerPatch& patch) {
Vladimir Markoea4c1262017-02-06 19:59:33 +000034 return (patch.IsPcRelative() && patch.GetType() != LinkerPatch::Type::kCallRelative) &&
Vladimir Markocac5a7e2016-02-22 10:39:50 +000035 patch.LiteralOffset() == patch.PcInsnOffset();
36}
37
38} // anonymous namespace
39
Vladimir Markob163bb72015-03-31 21:49:49 +010040Arm64RelativePatcher::Arm64RelativePatcher(RelativePatcherTargetProvider* provider,
41 const Arm64InstructionSetFeatures* features)
42 : ArmBaseRelativePatcher(provider, kArm64, CompileThunkCode(),
43 kMaxPositiveDisplacement, kMaxNegativeDisplacement),
44 fix_cortex_a53_843419_(features->NeedFixCortexA53_843419()),
45 reserved_adrp_thunks_(0u),
46 processed_adrp_thunks_(0u) {
47 if (fix_cortex_a53_843419_) {
48 adrp_thunk_locations_.reserve(16u);
49 current_method_thunks_.reserve(16u * kAdrpThunkSize);
50 }
51}
52
53uint32_t Arm64RelativePatcher::ReserveSpace(uint32_t offset,
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010054 const CompiledMethod* compiled_method,
55 MethodReference method_ref) {
Vladimir Markob163bb72015-03-31 21:49:49 +010056 if (!fix_cortex_a53_843419_) {
57 DCHECK(adrp_thunk_locations_.empty());
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010058 return ReserveSpaceInternal(offset, compiled_method, method_ref, 0u);
Vladimir Markob163bb72015-03-31 21:49:49 +010059 }
60
61 // Add thunks for previous method if any.
62 if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
63 size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
64 offset = CompiledMethod::AlignCode(offset, kArm64) + kAdrpThunkSize * num_adrp_thunks;
65 reserved_adrp_thunks_ = adrp_thunk_locations_.size();
66 }
67
68 // Count the number of ADRP insns as the upper bound on the number of thunks needed
69 // and use it to reserve space for other linker patches.
70 size_t num_adrp = 0u;
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010071 DCHECK(compiled_method != nullptr);
72 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +000073 if (IsAdrpPatch(patch)) {
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010074 ++num_adrp;
Vladimir Markob163bb72015-03-31 21:49:49 +010075 }
76 }
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010077 offset = ReserveSpaceInternal(offset, compiled_method, method_ref, kAdrpThunkSize * num_adrp);
Vladimir Markob163bb72015-03-31 21:49:49 +010078 if (num_adrp == 0u) {
79 return offset;
80 }
81
82 // Now that we have the actual offset where the code will be placed, locate the ADRP insns
83 // that actually require the thunk.
Vladimir Marko0c737df2016-08-01 16:33:16 +010084 uint32_t quick_code_offset = compiled_method->AlignCode(offset + sizeof(OatQuickMethodHeader));
Vladimir Marko35831e82015-09-11 11:59:18 +010085 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Vladimir Markob163bb72015-03-31 21:49:49 +010086 uint32_t thunk_offset = compiled_method->AlignCode(quick_code_offset + code.size());
87 DCHECK(compiled_method != nullptr);
88 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +000089 if (IsAdrpPatch(patch)) {
Vladimir Markob163bb72015-03-31 21:49:49 +010090 uint32_t patch_offset = quick_code_offset + patch.LiteralOffset();
91 if (NeedsErratum843419Thunk(code, patch.LiteralOffset(), patch_offset)) {
92 adrp_thunk_locations_.emplace_back(patch_offset, thunk_offset);
93 thunk_offset += kAdrpThunkSize;
94 }
95 }
96 }
97 return offset;
98}
99
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100100uint32_t Arm64RelativePatcher::ReserveSpaceEnd(uint32_t offset) {
101 if (!fix_cortex_a53_843419_) {
102 DCHECK(adrp_thunk_locations_.empty());
103 } else {
104 // Add thunks for the last method if any.
105 if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
106 size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
107 offset = CompiledMethod::AlignCode(offset, kArm64) + kAdrpThunkSize * num_adrp_thunks;
108 reserved_adrp_thunks_ = adrp_thunk_locations_.size();
109 }
110 }
111 return ArmBaseRelativePatcher::ReserveSpaceEnd(offset);
112}
113
Vladimir Markob163bb72015-03-31 21:49:49 +0100114uint32_t Arm64RelativePatcher::WriteThunks(OutputStream* out, uint32_t offset) {
115 if (fix_cortex_a53_843419_) {
116 if (!current_method_thunks_.empty()) {
117 uint32_t aligned_offset = CompiledMethod::AlignCode(offset, kArm64);
118 if (kIsDebugBuild) {
Roland Levillain14d90572015-07-16 10:52:26 +0100119 CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize);
Vladimir Markob163bb72015-03-31 21:49:49 +0100120 size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
121 CHECK_LE(num_thunks, processed_adrp_thunks_);
122 for (size_t i = 0u; i != num_thunks; ++i) {
123 const auto& entry = adrp_thunk_locations_[processed_adrp_thunks_ - num_thunks + i];
124 CHECK_EQ(entry.second, aligned_offset + i * kAdrpThunkSize);
125 }
126 }
127 uint32_t aligned_code_delta = aligned_offset - offset;
128 if (aligned_code_delta != 0u && !WriteCodeAlignment(out, aligned_code_delta)) {
129 return 0u;
130 }
131 if (!WriteMiscThunk(out, ArrayRef<const uint8_t>(current_method_thunks_))) {
132 return 0u;
133 }
134 offset = aligned_offset + current_method_thunks_.size();
135 current_method_thunks_.clear();
136 }
137 }
138 return ArmBaseRelativePatcher::WriteThunks(out, offset);
139}
140
Vladimir Marko944da602016-02-19 12:27:55 +0000141void Arm64RelativePatcher::PatchCall(std::vector<uint8_t>* code,
142 uint32_t literal_offset,
143 uint32_t patch_offset, uint32_t
144 target_offset) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100145 DCHECK_LE(literal_offset + 4u, code->size());
146 DCHECK_EQ(literal_offset & 3u, 0u);
147 DCHECK_EQ(patch_offset & 3u, 0u);
148 DCHECK_EQ(target_offset & 3u, 0u);
149 uint32_t displacement = CalculateDisplacement(patch_offset, target_offset & ~1u);
150 DCHECK_EQ(displacement & 3u, 0u);
151 DCHECK((displacement >> 27) == 0u || (displacement >> 27) == 31u); // 28-bit signed.
152 uint32_t insn = (displacement & 0x0fffffffu) >> 2;
153 insn |= 0x94000000; // BL
154
155 // Check that we're just overwriting an existing BL.
156 DCHECK_EQ(GetInsn(code, literal_offset) & 0xfc000000u, 0x94000000u);
157 // Write the new BL.
158 SetInsn(code, literal_offset, insn);
159}
160
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000161void Arm64RelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code,
162 const LinkerPatch& patch,
163 uint32_t patch_offset,
164 uint32_t target_offset) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100165 DCHECK_EQ(patch_offset & 3u, 0u);
166 DCHECK_EQ(target_offset & 3u, 0u);
167 uint32_t literal_offset = patch.LiteralOffset();
168 uint32_t insn = GetInsn(code, literal_offset);
169 uint32_t pc_insn_offset = patch.PcInsnOffset();
170 uint32_t disp = target_offset - ((patch_offset - literal_offset + pc_insn_offset) & ~0xfffu);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171 bool wide = (insn & 0x40000000) != 0;
172 uint32_t shift = wide ? 3u : 2u;
Vladimir Markob163bb72015-03-31 21:49:49 +0100173 if (literal_offset == pc_insn_offset) {
174 // Check it's an ADRP with imm == 0 (unset).
175 DCHECK_EQ((insn & 0xffffffe0u), 0x90000000u)
176 << literal_offset << ", " << pc_insn_offset << ", 0x" << std::hex << insn;
177 if (fix_cortex_a53_843419_ && processed_adrp_thunks_ != adrp_thunk_locations_.size() &&
178 adrp_thunk_locations_[processed_adrp_thunks_].first == patch_offset) {
179 DCHECK(NeedsErratum843419Thunk(ArrayRef<const uint8_t>(*code),
180 literal_offset, patch_offset));
181 uint32_t thunk_offset = adrp_thunk_locations_[processed_adrp_thunks_].second;
182 uint32_t adrp_disp = target_offset - (thunk_offset & ~0xfffu);
183 uint32_t adrp = PatchAdrp(insn, adrp_disp);
184
185 uint32_t out_disp = thunk_offset - patch_offset;
186 DCHECK_EQ(out_disp & 3u, 0u);
187 DCHECK((out_disp >> 27) == 0u || (out_disp >> 27) == 31u); // 28-bit signed.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188 insn = (out_disp & 0x0fffffffu) >> shift;
Vladimir Markob163bb72015-03-31 21:49:49 +0100189 insn |= 0x14000000; // B <thunk>
190
191 uint32_t back_disp = -out_disp;
192 DCHECK_EQ(back_disp & 3u, 0u);
193 DCHECK((back_disp >> 27) == 0u || (back_disp >> 27) == 31u); // 28-bit signed.
194 uint32_t b_back = (back_disp & 0x0fffffffu) >> 2;
195 b_back |= 0x14000000; // B <back>
196 size_t thunks_code_offset = current_method_thunks_.size();
197 current_method_thunks_.resize(thunks_code_offset + kAdrpThunkSize);
198 SetInsn(&current_method_thunks_, thunks_code_offset, adrp);
199 SetInsn(&current_method_thunks_, thunks_code_offset + 4u, b_back);
200 static_assert(kAdrpThunkSize == 2 * 4u, "thunk has 2 instructions");
201
202 processed_adrp_thunks_ += 1u;
203 } else {
204 insn = PatchAdrp(insn, disp);
205 }
206 // Write the new ADRP (or B to the erratum 843419 thunk).
207 SetInsn(code, literal_offset, insn);
208 } else {
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000209 if ((insn & 0xfffffc00) == 0x91000000) {
210 // ADD immediate, 64-bit with imm12 == 0 (unset).
Hiroshi Yamauchia5b75572016-04-18 16:05:21 -0700211 if (!kEmitCompilerReadBarrier) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100212 DCHECK(patch.GetType() == LinkerPatch::Type::kStringRelative ||
213 patch.GetType() == LinkerPatch::Type::kTypeRelative) << patch.GetType();
Hiroshi Yamauchia5b75572016-04-18 16:05:21 -0700214 } else {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000215 // With the read barrier (non-Baker) enabled, it could be kStringBssEntry or kTypeBssEntry.
Hiroshi Yamauchia5b75572016-04-18 16:05:21 -0700216 DCHECK(patch.GetType() == LinkerPatch::Type::kStringRelative ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100217 patch.GetType() == LinkerPatch::Type::kTypeRelative ||
Vladimir Markoea4c1262017-02-06 19:59:33 +0000218 patch.GetType() == LinkerPatch::Type::kStringBssEntry ||
219 patch.GetType() == LinkerPatch::Type::kTypeBssEntry) << patch.GetType();
Hiroshi Yamauchia5b75572016-04-18 16:05:21 -0700220 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000221 shift = 0u; // No shift for ADD.
222 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000223 // LDR/STR 32-bit or 64-bit with imm12 == 0 (unset).
224 DCHECK(patch.GetType() == LinkerPatch::Type::kDexCacheArray ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000225 patch.GetType() == LinkerPatch::Type::kTypeBssEntry ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000226 patch.GetType() == LinkerPatch::Type::kStringBssEntry) << patch.GetType();
227 DCHECK_EQ(insn & 0xbfbffc00, 0xb9000000) << std::hex << insn;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000228 }
Vladimir Markob163bb72015-03-31 21:49:49 +0100229 if (kIsDebugBuild) {
230 uint32_t adrp = GetInsn(code, pc_insn_offset);
231 if ((adrp & 0x9f000000u) != 0x90000000u) {
232 CHECK(fix_cortex_a53_843419_);
233 CHECK_EQ(adrp & 0xfc000000u, 0x14000000u); // B <thunk>
Roland Levillain14d90572015-07-16 10:52:26 +0100234 CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize);
Vladimir Markob163bb72015-03-31 21:49:49 +0100235 size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
236 CHECK_LE(num_thunks, processed_adrp_thunks_);
237 uint32_t b_offset = patch_offset - literal_offset + pc_insn_offset;
238 for (size_t i = processed_adrp_thunks_ - num_thunks; ; ++i) {
239 CHECK_NE(i, processed_adrp_thunks_);
240 if (adrp_thunk_locations_[i].first == b_offset) {
241 size_t idx = num_thunks - (processed_adrp_thunks_ - i);
242 adrp = GetInsn(&current_method_thunks_, idx * kAdrpThunkSize);
243 break;
244 }
245 }
246 }
247 CHECK_EQ(adrp & 0x9f00001fu, // Check that pc_insn_offset points
248 0x90000000 | ((insn >> 5) & 0x1fu)); // to ADRP with matching register.
249 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 uint32_t imm12 = (disp & 0xfffu) >> shift;
Vladimir Markob163bb72015-03-31 21:49:49 +0100251 insn = (insn & ~(0xfffu << 10)) | (imm12 << 10);
252 SetInsn(code, literal_offset, insn);
253 }
254}
255
256std::vector<uint8_t> Arm64RelativePatcher::CompileThunkCode() {
257 // The thunk just uses the entry point in the ArtMethod. This works even for calls
258 // to the generic JNI and interpreter trampolines.
Vladimir Marko93205e32016-04-13 11:59:46 +0100259 ArenaPool pool;
260 ArenaAllocator arena(&pool);
261 arm64::Arm64Assembler assembler(&arena);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Vladimir Markob163bb72015-03-31 21:49:49 +0100263 kArm64PointerSize).Int32Value());
264 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
265 // Ensure we emit the literal pool.
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000266 assembler.FinalizeCode();
Vladimir Markob163bb72015-03-31 21:49:49 +0100267 std::vector<uint8_t> thunk_code(assembler.CodeSize());
268 MemoryRegion code(thunk_code.data(), thunk_code.size());
269 assembler.FinalizeInstructions(code);
270 return thunk_code;
271}
272
273uint32_t Arm64RelativePatcher::PatchAdrp(uint32_t adrp, uint32_t disp) {
274 return (adrp & 0x9f00001fu) | // Clear offset bits, keep ADRP with destination reg.
275 // Bottom 12 bits are ignored, the next 2 lowest bits are encoded in bits 29-30.
276 ((disp & 0x00003000u) << (29 - 12)) |
277 // The next 16 bits are encoded in bits 5-22.
278 ((disp & 0xffffc000u) >> (12 + 2 - 5)) |
279 // Since the target_offset is based on the beginning of the oat file and the
280 // image space precedes the oat file, the target_offset into image space will
281 // be negative yet passed as uint32_t. Therefore we limit the displacement
282 // to +-2GiB (rather than the maximim +-4GiB) and determine the sign bit from
283 // the highest bit of the displacement. This is encoded in bit 23.
284 ((disp & 0x80000000u) >> (31 - 23));
285}
286
287bool Arm64RelativePatcher::NeedsErratum843419Thunk(ArrayRef<const uint8_t> code,
288 uint32_t literal_offset,
289 uint32_t patch_offset) {
290 DCHECK_EQ(patch_offset & 0x3u, 0u);
291 if ((patch_offset & 0xff8) == 0xff8) { // ...ff8 or ...ffc
292 uint32_t adrp = GetInsn(code, literal_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000293 DCHECK_EQ(adrp & 0x9f000000, 0x90000000);
Matteo Franchin97e2f262015-04-02 15:49:06 +0100294 uint32_t next_offset = patch_offset + 4u;
Vladimir Markob163bb72015-03-31 21:49:49 +0100295 uint32_t next_insn = GetInsn(code, literal_offset + 4u);
Matteo Franchin97e2f262015-04-02 15:49:06 +0100296
297 // Below we avoid patching sequences where the adrp is followed by a load which can easily
298 // be proved to be aligned.
299
300 // First check if the next insn is the LDR using the result of the ADRP.
301 // LDR <Wt>, [<Xn>, #pimm], where <Xn> == ADRP destination reg.
302 if ((next_insn & 0xffc00000) == 0xb9400000 &&
303 (((next_insn >> 5) ^ adrp) & 0x1f) == 0) {
304 return false;
305 }
306
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100307 // And since LinkerPatch::Type::kStringRelative is using the result of the ADRP
308 // for an ADD immediate, check for that as well. We generalize a bit to include
309 // ADD/ADDS/SUB/SUBS immediate that either uses the ADRP destination or stores
310 // the result to a different register.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000311 if ((next_insn & 0x1f000000) == 0x11000000 &&
312 ((((next_insn >> 5) ^ adrp) & 0x1f) == 0 || ((next_insn ^ adrp) & 0x1f) != 0)) {
313 return false;
314 }
315
Matteo Franchin97e2f262015-04-02 15:49:06 +0100316 // LDR <Wt>, <label> is always aligned and thus it doesn't cause boundary crossing.
317 if ((next_insn & 0xff000000) == 0x18000000) {
318 return false;
319 }
320
321 // LDR <Xt>, <label> is aligned iff the pc + displacement is a multiple of 8.
322 if ((next_insn & 0xff000000) == 0x58000000) {
323 bool is_aligned_load = (((next_offset >> 2) ^ (next_insn >> 5)) & 1) == 0;
324 return !is_aligned_load;
325 }
326
327 // LDR <Wt>, [SP, #<pimm>] and LDR <Xt>, [SP, #<pimm>] are always aligned loads, as SP is
328 // guaranteed to be 128-bits aligned and <pimm> is multiple of the load size.
329 if ((next_insn & 0xbfc003e0) == 0xb94003e0) {
330 return false;
331 }
332 return true;
Vladimir Markob163bb72015-03-31 21:49:49 +0100333 }
334 return false;
335}
336
337void Arm64RelativePatcher::SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) {
338 DCHECK_LE(offset + 4u, code->size());
339 DCHECK_EQ(offset & 3u, 0u);
340 uint8_t* addr = &(*code)[offset];
341 addr[0] = (value >> 0) & 0xff;
342 addr[1] = (value >> 8) & 0xff;
343 addr[2] = (value >> 16) & 0xff;
344 addr[3] = (value >> 24) & 0xff;
345}
346
347uint32_t Arm64RelativePatcher::GetInsn(ArrayRef<const uint8_t> code, uint32_t offset) {
348 DCHECK_LE(offset + 4u, code.size());
349 DCHECK_EQ(offset & 3u, 0u);
350 const uint8_t* addr = &code[offset];
351 return
352 (static_cast<uint32_t>(addr[0]) << 0) +
353 (static_cast<uint32_t>(addr[1]) << 8) +
354 (static_cast<uint32_t>(addr[2]) << 16)+
355 (static_cast<uint32_t>(addr[3]) << 24);
356}
357
358template <typename Alloc>
359uint32_t Arm64RelativePatcher::GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset) {
360 return GetInsn(ArrayRef<const uint8_t>(*code), offset);
361}
362
363} // namespace linker
364} // namespace art