blob: 0549327e7a39afa74cef4d6c233471f907beb565 [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) {
34 LinkerPatchType type = patch.Type();
35 return (type == kLinkerPatchStringRelative || type == kLinkerPatchDexCacheArray) &&
36 patch.LiteralOffset() == patch.PcInsnOffset();
37}
38
39} // anonymous namespace
40
Vladimir Markob163bb72015-03-31 21:49:49 +010041Arm64RelativePatcher::Arm64RelativePatcher(RelativePatcherTargetProvider* provider,
42 const Arm64InstructionSetFeatures* features)
43 : ArmBaseRelativePatcher(provider, kArm64, CompileThunkCode(),
44 kMaxPositiveDisplacement, kMaxNegativeDisplacement),
45 fix_cortex_a53_843419_(features->NeedFixCortexA53_843419()),
46 reserved_adrp_thunks_(0u),
47 processed_adrp_thunks_(0u) {
48 if (fix_cortex_a53_843419_) {
49 adrp_thunk_locations_.reserve(16u);
50 current_method_thunks_.reserve(16u * kAdrpThunkSize);
51 }
52}
53
54uint32_t Arm64RelativePatcher::ReserveSpace(uint32_t offset,
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010055 const CompiledMethod* compiled_method,
56 MethodReference method_ref) {
Vladimir Markob163bb72015-03-31 21:49:49 +010057 if (!fix_cortex_a53_843419_) {
58 DCHECK(adrp_thunk_locations_.empty());
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010059 return ReserveSpaceInternal(offset, compiled_method, method_ref, 0u);
Vladimir Markob163bb72015-03-31 21:49:49 +010060 }
61
62 // Add thunks for previous method if any.
63 if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
64 size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
65 offset = CompiledMethod::AlignCode(offset, kArm64) + kAdrpThunkSize * num_adrp_thunks;
66 reserved_adrp_thunks_ = adrp_thunk_locations_.size();
67 }
68
69 // Count the number of ADRP insns as the upper bound on the number of thunks needed
70 // and use it to reserve space for other linker patches.
71 size_t num_adrp = 0u;
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010072 DCHECK(compiled_method != nullptr);
73 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +000074 if (IsAdrpPatch(patch)) {
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010075 ++num_adrp;
Vladimir Markob163bb72015-03-31 21:49:49 +010076 }
77 }
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010078 offset = ReserveSpaceInternal(offset, compiled_method, method_ref, kAdrpThunkSize * num_adrp);
Vladimir Markob163bb72015-03-31 21:49:49 +010079 if (num_adrp == 0u) {
80 return offset;
81 }
82
83 // Now that we have the actual offset where the code will be placed, locate the ADRP insns
84 // that actually require the thunk.
85 uint32_t quick_code_offset = compiled_method->AlignCode(offset) + sizeof(OatQuickMethodHeader);
Vladimir Marko35831e82015-09-11 11:59:18 +010086 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Vladimir Markob163bb72015-03-31 21:49:49 +010087 uint32_t thunk_offset = compiled_method->AlignCode(quick_code_offset + code.size());
88 DCHECK(compiled_method != nullptr);
89 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +000090 if (IsAdrpPatch(patch)) {
Vladimir Markob163bb72015-03-31 21:49:49 +010091 uint32_t patch_offset = quick_code_offset + patch.LiteralOffset();
92 if (NeedsErratum843419Thunk(code, patch.LiteralOffset(), patch_offset)) {
93 adrp_thunk_locations_.emplace_back(patch_offset, thunk_offset);
94 thunk_offset += kAdrpThunkSize;
95 }
96 }
97 }
98 return offset;
99}
100
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100101uint32_t Arm64RelativePatcher::ReserveSpaceEnd(uint32_t offset) {
102 if (!fix_cortex_a53_843419_) {
103 DCHECK(adrp_thunk_locations_.empty());
104 } else {
105 // Add thunks for the last method if any.
106 if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
107 size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
108 offset = CompiledMethod::AlignCode(offset, kArm64) + kAdrpThunkSize * num_adrp_thunks;
109 reserved_adrp_thunks_ = adrp_thunk_locations_.size();
110 }
111 }
112 return ArmBaseRelativePatcher::ReserveSpaceEnd(offset);
113}
114
Vladimir Markob163bb72015-03-31 21:49:49 +0100115uint32_t Arm64RelativePatcher::WriteThunks(OutputStream* out, uint32_t offset) {
116 if (fix_cortex_a53_843419_) {
117 if (!current_method_thunks_.empty()) {
118 uint32_t aligned_offset = CompiledMethod::AlignCode(offset, kArm64);
119 if (kIsDebugBuild) {
Roland Levillain14d90572015-07-16 10:52:26 +0100120 CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize);
Vladimir Markob163bb72015-03-31 21:49:49 +0100121 size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
122 CHECK_LE(num_thunks, processed_adrp_thunks_);
123 for (size_t i = 0u; i != num_thunks; ++i) {
124 const auto& entry = adrp_thunk_locations_[processed_adrp_thunks_ - num_thunks + i];
125 CHECK_EQ(entry.second, aligned_offset + i * kAdrpThunkSize);
126 }
127 }
128 uint32_t aligned_code_delta = aligned_offset - offset;
129 if (aligned_code_delta != 0u && !WriteCodeAlignment(out, aligned_code_delta)) {
130 return 0u;
131 }
132 if (!WriteMiscThunk(out, ArrayRef<const uint8_t>(current_method_thunks_))) {
133 return 0u;
134 }
135 offset = aligned_offset + current_method_thunks_.size();
136 current_method_thunks_.clear();
137 }
138 }
139 return ArmBaseRelativePatcher::WriteThunks(out, offset);
140}
141
Vladimir Marko944da602016-02-19 12:27:55 +0000142void Arm64RelativePatcher::PatchCall(std::vector<uint8_t>* code,
143 uint32_t literal_offset,
144 uint32_t patch_offset, uint32_t
145 target_offset) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100146 DCHECK_LE(literal_offset + 4u, code->size());
147 DCHECK_EQ(literal_offset & 3u, 0u);
148 DCHECK_EQ(patch_offset & 3u, 0u);
149 DCHECK_EQ(target_offset & 3u, 0u);
150 uint32_t displacement = CalculateDisplacement(patch_offset, target_offset & ~1u);
151 DCHECK_EQ(displacement & 3u, 0u);
152 DCHECK((displacement >> 27) == 0u || (displacement >> 27) == 31u); // 28-bit signed.
153 uint32_t insn = (displacement & 0x0fffffffu) >> 2;
154 insn |= 0x94000000; // BL
155
156 // Check that we're just overwriting an existing BL.
157 DCHECK_EQ(GetInsn(code, literal_offset) & 0xfc000000u, 0x94000000u);
158 // Write the new BL.
159 SetInsn(code, literal_offset, insn);
160}
161
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000162void Arm64RelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code,
163 const LinkerPatch& patch,
164 uint32_t patch_offset,
165 uint32_t target_offset) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100166 DCHECK_EQ(patch_offset & 3u, 0u);
167 DCHECK_EQ(target_offset & 3u, 0u);
168 uint32_t literal_offset = patch.LiteralOffset();
169 uint32_t insn = GetInsn(code, literal_offset);
170 uint32_t pc_insn_offset = patch.PcInsnOffset();
171 uint32_t disp = target_offset - ((patch_offset - literal_offset + pc_insn_offset) & ~0xfffu);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700172 bool wide = (insn & 0x40000000) != 0;
173 uint32_t shift = wide ? 3u : 2u;
Vladimir Markob163bb72015-03-31 21:49:49 +0100174 if (literal_offset == pc_insn_offset) {
175 // Check it's an ADRP with imm == 0 (unset).
176 DCHECK_EQ((insn & 0xffffffe0u), 0x90000000u)
177 << literal_offset << ", " << pc_insn_offset << ", 0x" << std::hex << insn;
178 if (fix_cortex_a53_843419_ && processed_adrp_thunks_ != adrp_thunk_locations_.size() &&
179 adrp_thunk_locations_[processed_adrp_thunks_].first == patch_offset) {
180 DCHECK(NeedsErratum843419Thunk(ArrayRef<const uint8_t>(*code),
181 literal_offset, patch_offset));
182 uint32_t thunk_offset = adrp_thunk_locations_[processed_adrp_thunks_].second;
183 uint32_t adrp_disp = target_offset - (thunk_offset & ~0xfffu);
184 uint32_t adrp = PatchAdrp(insn, adrp_disp);
185
186 uint32_t out_disp = thunk_offset - patch_offset;
187 DCHECK_EQ(out_disp & 3u, 0u);
188 DCHECK((out_disp >> 27) == 0u || (out_disp >> 27) == 31u); // 28-bit signed.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189 insn = (out_disp & 0x0fffffffu) >> shift;
Vladimir Markob163bb72015-03-31 21:49:49 +0100190 insn |= 0x14000000; // B <thunk>
191
192 uint32_t back_disp = -out_disp;
193 DCHECK_EQ(back_disp & 3u, 0u);
194 DCHECK((back_disp >> 27) == 0u || (back_disp >> 27) == 31u); // 28-bit signed.
195 uint32_t b_back = (back_disp & 0x0fffffffu) >> 2;
196 b_back |= 0x14000000; // B <back>
197 size_t thunks_code_offset = current_method_thunks_.size();
198 current_method_thunks_.resize(thunks_code_offset + kAdrpThunkSize);
199 SetInsn(&current_method_thunks_, thunks_code_offset, adrp);
200 SetInsn(&current_method_thunks_, thunks_code_offset + 4u, b_back);
201 static_assert(kAdrpThunkSize == 2 * 4u, "thunk has 2 instructions");
202
203 processed_adrp_thunks_ += 1u;
204 } else {
205 insn = PatchAdrp(insn, disp);
206 }
207 // Write the new ADRP (or B to the erratum 843419 thunk).
208 SetInsn(code, literal_offset, insn);
209 } else {
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000210 if ((insn & 0xfffffc00) == 0x91000000) {
211 // ADD immediate, 64-bit with imm12 == 0 (unset).
212 DCHECK(patch.Type() == kLinkerPatchStringRelative) << patch.Type();
213 shift = 0u; // No shift for ADD.
214 } else {
215 // LDR 32-bit or 64-bit with imm12 == 0 (unset).
216 DCHECK(patch.Type() == kLinkerPatchDexCacheArray) << patch.Type();
217 DCHECK_EQ(insn & 0xbffffc00, 0xb9400000) << std::hex << insn;
218 }
Vladimir Markob163bb72015-03-31 21:49:49 +0100219 if (kIsDebugBuild) {
220 uint32_t adrp = GetInsn(code, pc_insn_offset);
221 if ((adrp & 0x9f000000u) != 0x90000000u) {
222 CHECK(fix_cortex_a53_843419_);
223 CHECK_EQ(adrp & 0xfc000000u, 0x14000000u); // B <thunk>
Roland Levillain14d90572015-07-16 10:52:26 +0100224 CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize);
Vladimir Markob163bb72015-03-31 21:49:49 +0100225 size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
226 CHECK_LE(num_thunks, processed_adrp_thunks_);
227 uint32_t b_offset = patch_offset - literal_offset + pc_insn_offset;
228 for (size_t i = processed_adrp_thunks_ - num_thunks; ; ++i) {
229 CHECK_NE(i, processed_adrp_thunks_);
230 if (adrp_thunk_locations_[i].first == b_offset) {
231 size_t idx = num_thunks - (processed_adrp_thunks_ - i);
232 adrp = GetInsn(&current_method_thunks_, idx * kAdrpThunkSize);
233 break;
234 }
235 }
236 }
237 CHECK_EQ(adrp & 0x9f00001fu, // Check that pc_insn_offset points
238 0x90000000 | ((insn >> 5) & 0x1fu)); // to ADRP with matching register.
239 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700240 uint32_t imm12 = (disp & 0xfffu) >> shift;
Vladimir Markob163bb72015-03-31 21:49:49 +0100241 insn = (insn & ~(0xfffu << 10)) | (imm12 << 10);
242 SetInsn(code, literal_offset, insn);
243 }
244}
245
246std::vector<uint8_t> Arm64RelativePatcher::CompileThunkCode() {
247 // The thunk just uses the entry point in the ArtMethod. This works even for calls
248 // to the generic JNI and interpreter trampolines.
249 arm64::Arm64Assembler assembler;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Vladimir Markob163bb72015-03-31 21:49:49 +0100251 kArm64PointerSize).Int32Value());
252 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
253 // Ensure we emit the literal pool.
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000254 assembler.FinalizeCode();
Vladimir Markob163bb72015-03-31 21:49:49 +0100255 std::vector<uint8_t> thunk_code(assembler.CodeSize());
256 MemoryRegion code(thunk_code.data(), thunk_code.size());
257 assembler.FinalizeInstructions(code);
258 return thunk_code;
259}
260
261uint32_t Arm64RelativePatcher::PatchAdrp(uint32_t adrp, uint32_t disp) {
262 return (adrp & 0x9f00001fu) | // Clear offset bits, keep ADRP with destination reg.
263 // Bottom 12 bits are ignored, the next 2 lowest bits are encoded in bits 29-30.
264 ((disp & 0x00003000u) << (29 - 12)) |
265 // The next 16 bits are encoded in bits 5-22.
266 ((disp & 0xffffc000u) >> (12 + 2 - 5)) |
267 // Since the target_offset is based on the beginning of the oat file and the
268 // image space precedes the oat file, the target_offset into image space will
269 // be negative yet passed as uint32_t. Therefore we limit the displacement
270 // to +-2GiB (rather than the maximim +-4GiB) and determine the sign bit from
271 // the highest bit of the displacement. This is encoded in bit 23.
272 ((disp & 0x80000000u) >> (31 - 23));
273}
274
275bool Arm64RelativePatcher::NeedsErratum843419Thunk(ArrayRef<const uint8_t> code,
276 uint32_t literal_offset,
277 uint32_t patch_offset) {
278 DCHECK_EQ(patch_offset & 0x3u, 0u);
279 if ((patch_offset & 0xff8) == 0xff8) { // ...ff8 or ...ffc
280 uint32_t adrp = GetInsn(code, literal_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000281 DCHECK_EQ(adrp & 0x9f000000, 0x90000000);
Matteo Franchin97e2f262015-04-02 15:49:06 +0100282 uint32_t next_offset = patch_offset + 4u;
Vladimir Markob163bb72015-03-31 21:49:49 +0100283 uint32_t next_insn = GetInsn(code, literal_offset + 4u);
Matteo Franchin97e2f262015-04-02 15:49:06 +0100284
285 // Below we avoid patching sequences where the adrp is followed by a load which can easily
286 // be proved to be aligned.
287
288 // First check if the next insn is the LDR using the result of the ADRP.
289 // LDR <Wt>, [<Xn>, #pimm], where <Xn> == ADRP destination reg.
290 if ((next_insn & 0xffc00000) == 0xb9400000 &&
291 (((next_insn >> 5) ^ adrp) & 0x1f) == 0) {
292 return false;
293 }
294
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000295 // And since kLinkerPatchStringRelative is using the result of the ADRP for an ADD immediate,
296 // check for that as well. We generalize a bit to include ADD/ADDS/SUB/SUBS immediate that
297 // either uses the ADRP destination or stores the result to a different register.
298 if ((next_insn & 0x1f000000) == 0x11000000 &&
299 ((((next_insn >> 5) ^ adrp) & 0x1f) == 0 || ((next_insn ^ adrp) & 0x1f) != 0)) {
300 return false;
301 }
302
Matteo Franchin97e2f262015-04-02 15:49:06 +0100303 // LDR <Wt>, <label> is always aligned and thus it doesn't cause boundary crossing.
304 if ((next_insn & 0xff000000) == 0x18000000) {
305 return false;
306 }
307
308 // LDR <Xt>, <label> is aligned iff the pc + displacement is a multiple of 8.
309 if ((next_insn & 0xff000000) == 0x58000000) {
310 bool is_aligned_load = (((next_offset >> 2) ^ (next_insn >> 5)) & 1) == 0;
311 return !is_aligned_load;
312 }
313
314 // LDR <Wt>, [SP, #<pimm>] and LDR <Xt>, [SP, #<pimm>] are always aligned loads, as SP is
315 // guaranteed to be 128-bits aligned and <pimm> is multiple of the load size.
316 if ((next_insn & 0xbfc003e0) == 0xb94003e0) {
317 return false;
318 }
319 return true;
Vladimir Markob163bb72015-03-31 21:49:49 +0100320 }
321 return false;
322}
323
324void Arm64RelativePatcher::SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) {
325 DCHECK_LE(offset + 4u, code->size());
326 DCHECK_EQ(offset & 3u, 0u);
327 uint8_t* addr = &(*code)[offset];
328 addr[0] = (value >> 0) & 0xff;
329 addr[1] = (value >> 8) & 0xff;
330 addr[2] = (value >> 16) & 0xff;
331 addr[3] = (value >> 24) & 0xff;
332}
333
334uint32_t Arm64RelativePatcher::GetInsn(ArrayRef<const uint8_t> code, uint32_t offset) {
335 DCHECK_LE(offset + 4u, code.size());
336 DCHECK_EQ(offset & 3u, 0u);
337 const uint8_t* addr = &code[offset];
338 return
339 (static_cast<uint32_t>(addr[0]) << 0) +
340 (static_cast<uint32_t>(addr[1]) << 8) +
341 (static_cast<uint32_t>(addr[2]) << 16)+
342 (static_cast<uint32_t>(addr[3]) << 24);
343}
344
345template <typename Alloc>
346uint32_t Arm64RelativePatcher::GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset) {
347 return GetInsn(ArrayRef<const uint8_t>(*code), offset);
348}
349
350} // namespace linker
351} // namespace art