1 /* 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 #ifndef ART_COMPILER_LINKER_ARM64_RELATIVE_PATCHER_ARM64_H_ 18 #define ART_COMPILER_LINKER_ARM64_RELATIVE_PATCHER_ARM64_H_ 19 20 #include "base/array_ref.h" 21 #include "base/bit_field.h" 22 #include "base/bit_utils.h" 23 #include "linker/arm/relative_patcher_arm_base.h" 24 25 namespace art { 26 27 namespace arm64 { 28 class Arm64Assembler; 29 } // namespace arm64 30 31 namespace linker { 32 33 class Arm64RelativePatcher FINAL : public ArmBaseRelativePatcher { 34 public: 35 static uint32_t EncodeBakerReadBarrierFieldData(uint32_t base_reg, uint32_t holder_reg) { 36 CheckValidReg(base_reg); 37 CheckValidReg(holder_reg); 38 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kField) | 39 BakerReadBarrierFirstRegField::Encode(base_reg) | 40 BakerReadBarrierSecondRegField::Encode(holder_reg); 41 } 42 43 static uint32_t EncodeBakerReadBarrierArrayData(uint32_t base_reg) { 44 CheckValidReg(base_reg); 45 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kArray) | 46 BakerReadBarrierFirstRegField::Encode(base_reg) | 47 BakerReadBarrierSecondRegField::Encode(kInvalidEncodedReg); 48 } 49 50 static uint32_t EncodeBakerReadBarrierGcRootData(uint32_t root_reg) { 51 CheckValidReg(root_reg); 52 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kGcRoot) | 53 BakerReadBarrierFirstRegField::Encode(root_reg) | 54 BakerReadBarrierSecondRegField::Encode(kInvalidEncodedReg); 55 } 56 57 Arm64RelativePatcher(RelativePatcherTargetProvider* provider, 58 const Arm64InstructionSetFeatures* features); 59 60 uint32_t ReserveSpace(uint32_t offset, 61 const CompiledMethod* compiled_method, 62 MethodReference method_ref) OVERRIDE; 63 uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE; 64 uint32_t WriteThunks(OutputStream* out, uint32_t offset) OVERRIDE; 65 void PatchCall(std::vector<uint8_t>* code, 66 uint32_t literal_offset, 67 uint32_t patch_offset, 68 uint32_t target_offset) OVERRIDE; 69 void PatchPcRelativeReference(std::vector<uint8_t>* code, 70 const LinkerPatch& patch, 71 uint32_t patch_offset, 72 uint32_t target_offset) OVERRIDE; 73 void PatchBakerReadBarrierBranch(std::vector<uint8_t>* code, 74 const LinkerPatch& patch, 75 uint32_t patch_offset) OVERRIDE; 76 77 protected: 78 std::vector<uint8_t> CompileThunk(const ThunkKey& key) OVERRIDE; 79 uint32_t MaxPositiveDisplacement(const ThunkKey& key) OVERRIDE; 80 uint32_t MaxNegativeDisplacement(const ThunkKey& key) OVERRIDE; 81 82 private: 83 static constexpr uint32_t kInvalidEncodedReg = /* sp/zr is invalid */ 31u; 84 85 enum class BakerReadBarrierKind : uint8_t { 86 kField, // Field get or array get with constant offset (i.e. constant index). 87 kArray, // Array get with index in register. 88 kGcRoot, // GC root load. 89 kLast = kGcRoot 90 }; 91 92 static constexpr size_t kBitsForBakerReadBarrierKind = 93 MinimumBitsToStore(static_cast<size_t>(BakerReadBarrierKind::kLast)); 94 static constexpr size_t kBitsForRegister = 5u; 95 using BakerReadBarrierKindField = 96 BitField<BakerReadBarrierKind, 0, kBitsForBakerReadBarrierKind>; 97 using BakerReadBarrierFirstRegField = 98 BitField<uint32_t, kBitsForBakerReadBarrierKind, kBitsForRegister>; 99 using BakerReadBarrierSecondRegField = 100 BitField<uint32_t, kBitsForBakerReadBarrierKind + kBitsForRegister, kBitsForRegister>; 101 102 static void CheckValidReg(uint32_t reg) { 103 DCHECK(reg < 30u && reg != 16u && reg != 17u) << reg; 104 } 105 106 void CompileBakerReadBarrierThunk(arm64::Arm64Assembler& assembler, uint32_t encoded_data); 107 108 static uint32_t PatchAdrp(uint32_t adrp, uint32_t disp); 109 110 static bool NeedsErratum843419Thunk(ArrayRef<const uint8_t> code, uint32_t literal_offset, 111 uint32_t patch_offset); 112 void SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value); 113 static uint32_t GetInsn(ArrayRef<const uint8_t> code, uint32_t offset); 114 115 template <typename Alloc> 116 static uint32_t GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset); 117 118 const bool fix_cortex_a53_843419_; 119 // Map original patch_offset to thunk offset. 120 std::vector<std::pair<uint32_t, uint32_t>> adrp_thunk_locations_; 121 size_t reserved_adrp_thunks_; 122 size_t processed_adrp_thunks_; 123 std::vector<uint8_t> current_method_thunks_; 124 125 friend class Arm64RelativePatcherTest; 126 127 DISALLOW_COPY_AND_ASSIGN(Arm64RelativePatcher); 128 }; 129 130 } // namespace linker 131 } // namespace art 132 133 #endif // ART_COMPILER_LINKER_ARM64_RELATIVE_PATCHER_ARM64_H_ 134