// Maximum positive and negative displacement for method call measured from the patch location. // (Signed 28 bit displacement with the last two bits 0 has range [-2^27, 2^27-4] measured from // the ARM64 PC pointing to the BL.)
constexpr uint32_t kMaxMethodCallPositiveDisplacement = (1u << 27) - 4u;
constexpr uint32_t kMaxMethodCallNegativeDisplacement = (1u << 27);
// Maximum positive and negative displacement for a conditional branch measured from the patch // location. (Signed 21 bit displacement with the last two bits 0 has range [-2^20, 2^20-4] // measured from the ARM64 PC pointing to the B.cond.)
constexpr uint32_t kMaxBcondPositiveDisplacement = (1u << 20) - 4u;
constexpr uint32_t kMaxBcondNegativeDisplacement = (1u << 20);
// The ADRP thunk for erratum 843419 is 2 instructions, i.e. 8 bytes.
constexpr uint32_t kAdrpThunkSize = 8u;
inlinebool IsAdrpPatch(const LinkerPatch& patch) { switch (patch.GetType()) { case LinkerPatch::Type::kCallRelative: case LinkerPatch::Type::kCallEntrypoint: case LinkerPatch::Type::kBakerReadBarrierBranch: returnfalse; case LinkerPatch::Type::kIntrinsicReference: case LinkerPatch::Type::kBootImageRelRo: case LinkerPatch::Type::kMethodRelative: case LinkerPatch::Type::kMethodAppImageRelRo: case LinkerPatch::Type::kMethodBssEntry: case LinkerPatch::Type::kJniEntrypointRelative: case LinkerPatch::Type::kTypeRelative: case LinkerPatch::Type::kTypeAppImageRelRo: case LinkerPatch::Type::kTypeBssEntry: case LinkerPatch::Type::kPublicTypeBssEntry: case LinkerPatch::Type::kPackageTypeBssEntry: case LinkerPatch::Type::kStringRelative: case LinkerPatch::Type::kStringAppImageRelRo: case LinkerPatch::Type::kStringBssEntry: case LinkerPatch::Type::kMethodTypeBssEntry: return patch.LiteralOffset() == patch.PcInsnOffset();
}
}
// Count the number of ADRP insns as the upper bound on the number of thunks needed // and use it to reserve space for other linker patches.
size_t num_adrp = 0u;
DCHECK(compiled_method != nullptr); for (const LinkerPatch& patch : compiled_method->GetPatches()) { if (IsAdrpPatch(patch)) {
++num_adrp;
}
}
ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
uint32_t max_extra_space = MaxExtraSpace(num_adrp, code.size());
offset = ReserveSpaceInternal(offset, compiled_method, method_ref, max_extra_space); if (num_adrp == 0u) { return offset;
}
// Now that we have the actual offset where the code will be placed, locate the ADRP insns // that actually require the thunk.
uint32_t quick_code_offset = compiled_method->AlignCode(offset + sizeof(OatQuickMethodHeader));
uint32_t thunk_offset = compiled_method->AlignCode(quick_code_offset + code.size());
DCHECK(compiled_method != nullptr); for (const LinkerPatch& patch : compiled_method->GetPatches()) { if (IsAdrpPatch(patch)) {
uint32_t patch_offset = quick_code_offset + patch.LiteralOffset(); if (NeedsErratum843419Thunk(code, patch.LiteralOffset(), patch_offset)) {
adrp_thunk_locations_.emplace_back(patch_offset, thunk_offset);
thunk_offset += kAdrpThunkSize;
}
}
} return offset;
}
uint32_t Arm64RelativePatcher::ReserveSpaceEnd(uint32_t offset) { if (!fix_cortex_a53_843419_) {
DCHECK(adrp_thunk_locations_.empty());
} else { // Add thunks for the last method if any. if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
offset = CompiledMethod::AlignCode(offset, InstructionSet::kArm64) +
kAdrpThunkSize * num_adrp_thunks;
reserved_adrp_thunks_ = adrp_thunk_locations_.size();
}
} return ArmBaseRelativePatcher::ReserveSpaceEnd(offset);
}
uint32_t Arm64RelativePatcher::MaxPositiveDisplacement(const ThunkKey& key) { switch (key.GetType()) { case ThunkType::kMethodCall: case ThunkType::kEntrypointCall: return kMaxMethodCallPositiveDisplacement; case ThunkType::kBakerReadBarrier: return kMaxBcondPositiveDisplacement;
}
}
uint32_t Arm64RelativePatcher::MaxNegativeDisplacement(const ThunkKey& key) { switch (key.GetType()) { case ThunkType::kMethodCall: case ThunkType::kEntrypointCall: return kMaxMethodCallNegativeDisplacement; case ThunkType::kBakerReadBarrier: return kMaxBcondNegativeDisplacement;
}
}
uint32_t Arm64RelativePatcher::PatchAdrp(uint32_t adrp, uint32_t disp) { return (adrp & 0x9f00001fu) | // Clear offset bits, keep ADRP with destination reg. // Bottom 12 bits are ignored, the next 2 lowest bits are encoded in bits 29-30.
((disp & 0x00003000u) << (29 - 12)) | // The next 16 bits are encoded in bits 5-22.
((disp & 0xffffc000u) >> (12 + 2 - 5)) | // Since the target_offset is based on the beginning of the oat file and the // image space precedes the oat file, the target_offset into image space will // be negative yet passed as uint32_t. Therefore we limit the displacement // to +-2GiB (rather than the maximim +-4GiB) and determine the sign bit from // the highest bit of the displacement. This is encoded in bit 23.
((disp & 0x80000000u) >> (31 - 23));
}
// Check that we're just overwriting an existing BL.
DCHECK_EQ(GetInsn(code, literal_offset) & 0xfc000000u, 0x94000000u); // Write the new BL.
SetInsn(code, literal_offset, insn);
}
// Below we avoid patching sequences where the adrp is followed by a load which can easily // be proved to be aligned.
// First check if the next insn is the LDR using the result of the ADRP. // LDR <Wt>, [<Xn>, #pimm], where <Xn> == ADRP destination reg. if ((next_insn & 0xffc00000) == 0xb9400000 &&
(((next_insn >> 5) ^ adrp) & 0x1f) == 0) { returnfalse;
}
// And since LinkerPatch::Type::k{Method,Type,String}Relative is using the result // of the ADRP for an ADD immediate, check for that as well. We generalize a bit // to include ADD/ADDS/SUB/SUBS immediate that either uses the ADRP destination // or stores the result to a different register. if ((next_insn & 0x1f000000) == 0x11000000 &&
((((next_insn >> 5) ^ adrp) & 0x1f) == 0 || ((next_insn ^ adrp) & 0x1f) != 0)) { returnfalse;
}
// LDR <Wt>, <label> is always aligned and thus it doesn't cause boundary crossing. if ((next_insn & 0xff000000) == 0x18000000) { returnfalse;
}
// LDR <Xt>, <label> is aligned iff the pc + displacement is a multiple of 8. if ((next_insn & 0xff000000) == 0x58000000) { bool is_aligned_load = (((next_offset >> 2) ^ (next_insn >> 5)) & 1) == 0; return !is_aligned_load;
}
// LDR <Wt>, [SP, #<pimm>] and LDR <Xt>, [SP, #<pimm>] are always aligned loads, as SP is // guaranteed to be 128-bits aligned and <pimm> is multiple of the load size. if ((next_insn & 0xbfc003e0) == 0xb94003e0) { returnfalse;
} returntrue;
} returnfalse;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.