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_ARM_RELATIVE_PATCHER_THUMB2_H_ 18 #define ART_COMPILER_LINKER_ARM_RELATIVE_PATCHER_THUMB2_H_ 19 20 #include "arch/arm/registers_arm.h" 21 #include "base/array_ref.h" 22 #include "base/bit_field.h" 23 #include "base/bit_utils.h" 24 #include "linker/arm/relative_patcher_arm_base.h" 25 26 namespace art { 27 28 namespace arm { 29 class ArmVIXLAssembler; 30 } // namespace arm 31 32 namespace linker { 33 34 class Thumb2RelativePatcher FINAL : public ArmBaseRelativePatcher { 35 public: 36 static constexpr uint32_t kBakerCcEntrypointRegister = 4u; 37 38 static uint32_t EncodeBakerReadBarrierFieldData(uint32_t base_reg, 39 uint32_t holder_reg, 40 bool narrow) { 41 CheckValidReg(base_reg); 42 CheckValidReg(holder_reg); 43 DCHECK(!narrow || base_reg < 8u) << base_reg; 44 BakerReadBarrierWidth width = 45 narrow ? BakerReadBarrierWidth::kNarrow : BakerReadBarrierWidth::kWide; 46 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kField) | 47 BakerReadBarrierFirstRegField::Encode(base_reg) | 48 BakerReadBarrierSecondRegField::Encode(holder_reg) | 49 BakerReadBarrierWidthField::Encode(width); 50 } 51 52 static uint32_t EncodeBakerReadBarrierArrayData(uint32_t base_reg) { 53 CheckValidReg(base_reg); 54 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kArray) | 55 BakerReadBarrierFirstRegField::Encode(base_reg) | 56 BakerReadBarrierSecondRegField::Encode(kInvalidEncodedReg) | 57 BakerReadBarrierWidthField::Encode(BakerReadBarrierWidth::kWide); 58 } 59 60 static uint32_t EncodeBakerReadBarrierGcRootData(uint32_t root_reg, bool narrow) { 61 CheckValidReg(root_reg); 62 DCHECK(!narrow || root_reg < 8u) << root_reg; 63 BakerReadBarrierWidth width = 64 narrow ? BakerReadBarrierWidth::kNarrow : BakerReadBarrierWidth::kWide; 65 return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kGcRoot) | 66 BakerReadBarrierFirstRegField::Encode(root_reg) | 67 BakerReadBarrierSecondRegField::Encode(kInvalidEncodedReg) | 68 BakerReadBarrierWidthField::Encode(width); 69 } 70 71 explicit Thumb2RelativePatcher(RelativePatcherTargetProvider* provider); 72 73 void PatchCall(std::vector<uint8_t>* code, 74 uint32_t literal_offset, 75 uint32_t patch_offset, 76 uint32_t target_offset) OVERRIDE; 77 void PatchPcRelativeReference(std::vector<uint8_t>* code, 78 const LinkerPatch& patch, 79 uint32_t patch_offset, 80 uint32_t target_offset) OVERRIDE; 81 void PatchBakerReadBarrierBranch(std::vector<uint8_t>* code, 82 const LinkerPatch& patch, 83 uint32_t patch_offset) OVERRIDE; 84 85 protected: 86 std::vector<uint8_t> CompileThunk(const ThunkKey& key) OVERRIDE; 87 uint32_t MaxPositiveDisplacement(const ThunkKey& key) OVERRIDE; 88 uint32_t MaxNegativeDisplacement(const ThunkKey& key) OVERRIDE; 89 90 private: 91 static constexpr uint32_t kInvalidEncodedReg = /* pc is invalid */ 15u; 92 93 enum class BakerReadBarrierKind : uint8_t { 94 kField, // Field get or array get with constant offset (i.e. constant index). 95 kArray, // Array get with index in register. 96 kGcRoot, // GC root load. 97 kLast = kGcRoot 98 }; 99 100 enum class BakerReadBarrierWidth : uint8_t { 101 kWide, // 32-bit LDR (and 32-bit NEG if heap poisoning is enabled). 102 kNarrow, // 16-bit LDR (and 16-bit NEG if heap poisoning is enabled). 103 kLast = kNarrow 104 }; 105 106 static constexpr size_t kBitsForBakerReadBarrierKind = 107 MinimumBitsToStore(static_cast<size_t>(BakerReadBarrierKind::kLast)); 108 static constexpr size_t kBitsForRegister = 4u; 109 using BakerReadBarrierKindField = 110 BitField<BakerReadBarrierKind, 0, kBitsForBakerReadBarrierKind>; 111 using BakerReadBarrierFirstRegField = 112 BitField<uint32_t, kBitsForBakerReadBarrierKind, kBitsForRegister>; 113 using BakerReadBarrierSecondRegField = 114 BitField<uint32_t, kBitsForBakerReadBarrierKind + kBitsForRegister, kBitsForRegister>; 115 static constexpr size_t kBitsForBakerReadBarrierWidth = 116 MinimumBitsToStore(static_cast<size_t>(BakerReadBarrierWidth::kLast)); 117 using BakerReadBarrierWidthField = BitField<BakerReadBarrierWidth, 118 kBitsForBakerReadBarrierKind + 2 * kBitsForRegister, 119 kBitsForBakerReadBarrierWidth>; 120 121 static void CheckValidReg(uint32_t reg) { 122 DCHECK(reg < 12u && reg != kBakerCcEntrypointRegister) << reg; 123 } 124 125 void CompileBakerReadBarrierThunk(arm::ArmVIXLAssembler& assembler, uint32_t encoded_data); 126 127 void SetInsn32(std::vector<uint8_t>* code, uint32_t offset, uint32_t value); 128 static uint32_t GetInsn32(ArrayRef<const uint8_t> code, uint32_t offset); 129 130 template <typename Vector> 131 static uint32_t GetInsn32(Vector* code, uint32_t offset); 132 133 static uint32_t GetInsn16(ArrayRef<const uint8_t> code, uint32_t offset); 134 135 template <typename Vector> 136 static uint32_t GetInsn16(Vector* code, uint32_t offset); 137 138 friend class Thumb2RelativePatcherTest; 139 140 DISALLOW_COPY_AND_ASSIGN(Thumb2RelativePatcher); 141 }; 142 143 } // namespace linker 144 } // namespace art 145 146 #endif // ART_COMPILER_LINKER_ARM_RELATIVE_PATCHER_THUMB2_H_ 147