blob: a81c85c707325fb97c614a849c3b9b8836345df8 [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
31Arm64RelativePatcher::Arm64RelativePatcher(RelativePatcherTargetProvider* provider,
32 const Arm64InstructionSetFeatures* features)
33 : ArmBaseRelativePatcher(provider, kArm64, CompileThunkCode(),
34 kMaxPositiveDisplacement, kMaxNegativeDisplacement),
35 fix_cortex_a53_843419_(features->NeedFixCortexA53_843419()),
36 reserved_adrp_thunks_(0u),
37 processed_adrp_thunks_(0u) {
38 if (fix_cortex_a53_843419_) {
39 adrp_thunk_locations_.reserve(16u);
40 current_method_thunks_.reserve(16u * kAdrpThunkSize);
41 }
42}
43
44uint32_t Arm64RelativePatcher::ReserveSpace(uint32_t offset,
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010045 const CompiledMethod* compiled_method,
46 MethodReference method_ref) {
Vladimir Markob163bb72015-03-31 21:49:49 +010047 if (!fix_cortex_a53_843419_) {
48 DCHECK(adrp_thunk_locations_.empty());
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010049 return ReserveSpaceInternal(offset, compiled_method, method_ref, 0u);
Vladimir Markob163bb72015-03-31 21:49:49 +010050 }
51
52 // Add thunks for previous method if any.
53 if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
54 size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
55 offset = CompiledMethod::AlignCode(offset, kArm64) + kAdrpThunkSize * num_adrp_thunks;
56 reserved_adrp_thunks_ = adrp_thunk_locations_.size();
57 }
58
59 // Count the number of ADRP insns as the upper bound on the number of thunks needed
60 // and use it to reserve space for other linker patches.
61 size_t num_adrp = 0u;
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010062 DCHECK(compiled_method != nullptr);
63 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
64 if (patch.Type() == kLinkerPatchDexCacheArray &&
65 patch.LiteralOffset() == patch.PcInsnOffset()) { // ADRP patch
66 ++num_adrp;
Vladimir Markob163bb72015-03-31 21:49:49 +010067 }
68 }
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010069 offset = ReserveSpaceInternal(offset, compiled_method, method_ref, kAdrpThunkSize * num_adrp);
Vladimir Markob163bb72015-03-31 21:49:49 +010070 if (num_adrp == 0u) {
71 return offset;
72 }
73
74 // Now that we have the actual offset where the code will be placed, locate the ADRP insns
75 // that actually require the thunk.
76 uint32_t quick_code_offset = compiled_method->AlignCode(offset) + sizeof(OatQuickMethodHeader);
Vladimir Marko35831e82015-09-11 11:59:18 +010077 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Vladimir Markob163bb72015-03-31 21:49:49 +010078 uint32_t thunk_offset = compiled_method->AlignCode(quick_code_offset + code.size());
79 DCHECK(compiled_method != nullptr);
80 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
81 if (patch.Type() == kLinkerPatchDexCacheArray &&
82 patch.LiteralOffset() == patch.PcInsnOffset()) { // ADRP patch
83 uint32_t patch_offset = quick_code_offset + patch.LiteralOffset();
84 if (NeedsErratum843419Thunk(code, patch.LiteralOffset(), patch_offset)) {
85 adrp_thunk_locations_.emplace_back(patch_offset, thunk_offset);
86 thunk_offset += kAdrpThunkSize;
87 }
88 }
89 }
90 return offset;
91}
92
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010093uint32_t Arm64RelativePatcher::ReserveSpaceEnd(uint32_t offset) {
94 if (!fix_cortex_a53_843419_) {
95 DCHECK(adrp_thunk_locations_.empty());
96 } else {
97 // Add thunks for the last method if any.
98 if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
99 size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
100 offset = CompiledMethod::AlignCode(offset, kArm64) + kAdrpThunkSize * num_adrp_thunks;
101 reserved_adrp_thunks_ = adrp_thunk_locations_.size();
102 }
103 }
104 return ArmBaseRelativePatcher::ReserveSpaceEnd(offset);
105}
106
Vladimir Markob163bb72015-03-31 21:49:49 +0100107uint32_t Arm64RelativePatcher::WriteThunks(OutputStream* out, uint32_t offset) {
108 if (fix_cortex_a53_843419_) {
109 if (!current_method_thunks_.empty()) {
110 uint32_t aligned_offset = CompiledMethod::AlignCode(offset, kArm64);
111 if (kIsDebugBuild) {
Roland Levillain14d90572015-07-16 10:52:26 +0100112 CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize);
Vladimir Markob163bb72015-03-31 21:49:49 +0100113 size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
114 CHECK_LE(num_thunks, processed_adrp_thunks_);
115 for (size_t i = 0u; i != num_thunks; ++i) {
116 const auto& entry = adrp_thunk_locations_[processed_adrp_thunks_ - num_thunks + i];
117 CHECK_EQ(entry.second, aligned_offset + i * kAdrpThunkSize);
118 }
119 }
120 uint32_t aligned_code_delta = aligned_offset - offset;
121 if (aligned_code_delta != 0u && !WriteCodeAlignment(out, aligned_code_delta)) {
122 return 0u;
123 }
124 if (!WriteMiscThunk(out, ArrayRef<const uint8_t>(current_method_thunks_))) {
125 return 0u;
126 }
127 offset = aligned_offset + current_method_thunks_.size();
128 current_method_thunks_.clear();
129 }
130 }
131 return ArmBaseRelativePatcher::WriteThunks(out, offset);
132}
133
Vladimir Marko944da602016-02-19 12:27:55 +0000134void Arm64RelativePatcher::PatchCall(std::vector<uint8_t>* code,
135 uint32_t literal_offset,
136 uint32_t patch_offset, uint32_t
137 target_offset) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100138 DCHECK_LE(literal_offset + 4u, code->size());
139 DCHECK_EQ(literal_offset & 3u, 0u);
140 DCHECK_EQ(patch_offset & 3u, 0u);
141 DCHECK_EQ(target_offset & 3u, 0u);
142 uint32_t displacement = CalculateDisplacement(patch_offset, target_offset & ~1u);
143 DCHECK_EQ(displacement & 3u, 0u);
144 DCHECK((displacement >> 27) == 0u || (displacement >> 27) == 31u); // 28-bit signed.
145 uint32_t insn = (displacement & 0x0fffffffu) >> 2;
146 insn |= 0x94000000; // BL
147
148 // Check that we're just overwriting an existing BL.
149 DCHECK_EQ(GetInsn(code, literal_offset) & 0xfc000000u, 0x94000000u);
150 // Write the new BL.
151 SetInsn(code, literal_offset, insn);
152}
153
154void Arm64RelativePatcher::PatchDexCacheReference(std::vector<uint8_t>* code,
155 const LinkerPatch& patch,
156 uint32_t patch_offset,
157 uint32_t target_offset) {
158 DCHECK_EQ(patch_offset & 3u, 0u);
159 DCHECK_EQ(target_offset & 3u, 0u);
160 uint32_t literal_offset = patch.LiteralOffset();
161 uint32_t insn = GetInsn(code, literal_offset);
162 uint32_t pc_insn_offset = patch.PcInsnOffset();
163 uint32_t disp = target_offset - ((patch_offset - literal_offset + pc_insn_offset) & ~0xfffu);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700164 bool wide = (insn & 0x40000000) != 0;
165 uint32_t shift = wide ? 3u : 2u;
Vladimir Markob163bb72015-03-31 21:49:49 +0100166 if (literal_offset == pc_insn_offset) {
167 // Check it's an ADRP with imm == 0 (unset).
168 DCHECK_EQ((insn & 0xffffffe0u), 0x90000000u)
169 << literal_offset << ", " << pc_insn_offset << ", 0x" << std::hex << insn;
170 if (fix_cortex_a53_843419_ && processed_adrp_thunks_ != adrp_thunk_locations_.size() &&
171 adrp_thunk_locations_[processed_adrp_thunks_].first == patch_offset) {
172 DCHECK(NeedsErratum843419Thunk(ArrayRef<const uint8_t>(*code),
173 literal_offset, patch_offset));
174 uint32_t thunk_offset = adrp_thunk_locations_[processed_adrp_thunks_].second;
175 uint32_t adrp_disp = target_offset - (thunk_offset & ~0xfffu);
176 uint32_t adrp = PatchAdrp(insn, adrp_disp);
177
178 uint32_t out_disp = thunk_offset - patch_offset;
179 DCHECK_EQ(out_disp & 3u, 0u);
180 DCHECK((out_disp >> 27) == 0u || (out_disp >> 27) == 31u); // 28-bit signed.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700181 insn = (out_disp & 0x0fffffffu) >> shift;
Vladimir Markob163bb72015-03-31 21:49:49 +0100182 insn |= 0x14000000; // B <thunk>
183
184 uint32_t back_disp = -out_disp;
185 DCHECK_EQ(back_disp & 3u, 0u);
186 DCHECK((back_disp >> 27) == 0u || (back_disp >> 27) == 31u); // 28-bit signed.
187 uint32_t b_back = (back_disp & 0x0fffffffu) >> 2;
188 b_back |= 0x14000000; // B <back>
189 size_t thunks_code_offset = current_method_thunks_.size();
190 current_method_thunks_.resize(thunks_code_offset + kAdrpThunkSize);
191 SetInsn(&current_method_thunks_, thunks_code_offset, adrp);
192 SetInsn(&current_method_thunks_, thunks_code_offset + 4u, b_back);
193 static_assert(kAdrpThunkSize == 2 * 4u, "thunk has 2 instructions");
194
195 processed_adrp_thunks_ += 1u;
196 } else {
197 insn = PatchAdrp(insn, disp);
198 }
199 // Write the new ADRP (or B to the erratum 843419 thunk).
200 SetInsn(code, literal_offset, insn);
201 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202 // LDR 32-bit or 64-bit with imm12 == 0 (unset).
203 DCHECK_EQ(insn & 0xbffffc00, 0xb9400000) << insn;
Vladimir Markob163bb72015-03-31 21:49:49 +0100204 if (kIsDebugBuild) {
205 uint32_t adrp = GetInsn(code, pc_insn_offset);
206 if ((adrp & 0x9f000000u) != 0x90000000u) {
207 CHECK(fix_cortex_a53_843419_);
208 CHECK_EQ(adrp & 0xfc000000u, 0x14000000u); // B <thunk>
Roland Levillain14d90572015-07-16 10:52:26 +0100209 CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize);
Vladimir Markob163bb72015-03-31 21:49:49 +0100210 size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
211 CHECK_LE(num_thunks, processed_adrp_thunks_);
212 uint32_t b_offset = patch_offset - literal_offset + pc_insn_offset;
213 for (size_t i = processed_adrp_thunks_ - num_thunks; ; ++i) {
214 CHECK_NE(i, processed_adrp_thunks_);
215 if (adrp_thunk_locations_[i].first == b_offset) {
216 size_t idx = num_thunks - (processed_adrp_thunks_ - i);
217 adrp = GetInsn(&current_method_thunks_, idx * kAdrpThunkSize);
218 break;
219 }
220 }
221 }
222 CHECK_EQ(adrp & 0x9f00001fu, // Check that pc_insn_offset points
223 0x90000000 | ((insn >> 5) & 0x1fu)); // to ADRP with matching register.
224 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 uint32_t imm12 = (disp & 0xfffu) >> shift;
Vladimir Markob163bb72015-03-31 21:49:49 +0100226 insn = (insn & ~(0xfffu << 10)) | (imm12 << 10);
227 SetInsn(code, literal_offset, insn);
228 }
229}
230
231std::vector<uint8_t> Arm64RelativePatcher::CompileThunkCode() {
232 // The thunk just uses the entry point in the ArtMethod. This works even for calls
233 // to the generic JNI and interpreter trampolines.
234 arm64::Arm64Assembler assembler;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700235 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Vladimir Markob163bb72015-03-31 21:49:49 +0100236 kArm64PointerSize).Int32Value());
237 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
238 // Ensure we emit the literal pool.
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000239 assembler.FinalizeCode();
Vladimir Markob163bb72015-03-31 21:49:49 +0100240 std::vector<uint8_t> thunk_code(assembler.CodeSize());
241 MemoryRegion code(thunk_code.data(), thunk_code.size());
242 assembler.FinalizeInstructions(code);
243 return thunk_code;
244}
245
246uint32_t Arm64RelativePatcher::PatchAdrp(uint32_t adrp, uint32_t disp) {
247 return (adrp & 0x9f00001fu) | // Clear offset bits, keep ADRP with destination reg.
248 // Bottom 12 bits are ignored, the next 2 lowest bits are encoded in bits 29-30.
249 ((disp & 0x00003000u) << (29 - 12)) |
250 // The next 16 bits are encoded in bits 5-22.
251 ((disp & 0xffffc000u) >> (12 + 2 - 5)) |
252 // Since the target_offset is based on the beginning of the oat file and the
253 // image space precedes the oat file, the target_offset into image space will
254 // be negative yet passed as uint32_t. Therefore we limit the displacement
255 // to +-2GiB (rather than the maximim +-4GiB) and determine the sign bit from
256 // the highest bit of the displacement. This is encoded in bit 23.
257 ((disp & 0x80000000u) >> (31 - 23));
258}
259
260bool Arm64RelativePatcher::NeedsErratum843419Thunk(ArrayRef<const uint8_t> code,
261 uint32_t literal_offset,
262 uint32_t patch_offset) {
263 DCHECK_EQ(patch_offset & 0x3u, 0u);
264 if ((patch_offset & 0xff8) == 0xff8) { // ...ff8 or ...ffc
265 uint32_t adrp = GetInsn(code, literal_offset);
266 DCHECK_EQ(adrp & 0xff000000, 0x90000000);
Matteo Franchin97e2f262015-04-02 15:49:06 +0100267 uint32_t next_offset = patch_offset + 4u;
Vladimir Markob163bb72015-03-31 21:49:49 +0100268 uint32_t next_insn = GetInsn(code, literal_offset + 4u);
Matteo Franchin97e2f262015-04-02 15:49:06 +0100269
270 // Below we avoid patching sequences where the adrp is followed by a load which can easily
271 // be proved to be aligned.
272
273 // First check if the next insn is the LDR using the result of the ADRP.
274 // LDR <Wt>, [<Xn>, #pimm], where <Xn> == ADRP destination reg.
275 if ((next_insn & 0xffc00000) == 0xb9400000 &&
276 (((next_insn >> 5) ^ adrp) & 0x1f) == 0) {
277 return false;
278 }
279
280 // LDR <Wt>, <label> is always aligned and thus it doesn't cause boundary crossing.
281 if ((next_insn & 0xff000000) == 0x18000000) {
282 return false;
283 }
284
285 // LDR <Xt>, <label> is aligned iff the pc + displacement is a multiple of 8.
286 if ((next_insn & 0xff000000) == 0x58000000) {
287 bool is_aligned_load = (((next_offset >> 2) ^ (next_insn >> 5)) & 1) == 0;
288 return !is_aligned_load;
289 }
290
291 // LDR <Wt>, [SP, #<pimm>] and LDR <Xt>, [SP, #<pimm>] are always aligned loads, as SP is
292 // guaranteed to be 128-bits aligned and <pimm> is multiple of the load size.
293 if ((next_insn & 0xbfc003e0) == 0xb94003e0) {
294 return false;
295 }
296 return true;
Vladimir Markob163bb72015-03-31 21:49:49 +0100297 }
298 return false;
299}
300
301void Arm64RelativePatcher::SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) {
302 DCHECK_LE(offset + 4u, code->size());
303 DCHECK_EQ(offset & 3u, 0u);
304 uint8_t* addr = &(*code)[offset];
305 addr[0] = (value >> 0) & 0xff;
306 addr[1] = (value >> 8) & 0xff;
307 addr[2] = (value >> 16) & 0xff;
308 addr[3] = (value >> 24) & 0xff;
309}
310
311uint32_t Arm64RelativePatcher::GetInsn(ArrayRef<const uint8_t> code, uint32_t offset) {
312 DCHECK_LE(offset + 4u, code.size());
313 DCHECK_EQ(offset & 3u, 0u);
314 const uint8_t* addr = &code[offset];
315 return
316 (static_cast<uint32_t>(addr[0]) << 0) +
317 (static_cast<uint32_t>(addr[1]) << 8) +
318 (static_cast<uint32_t>(addr[2]) << 16)+
319 (static_cast<uint32_t>(addr[3]) << 24);
320}
321
322template <typename Alloc>
323uint32_t Arm64RelativePatcher::GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset) {
324 return GetInsn(ArrayRef<const uint8_t>(*code), offset);
325}
326
327} // namespace linker
328} // namespace art