Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |
| 18 | #define ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |
| 19 | |
| 20 | #include "arch/instruction_set.h" |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 21 | #include "base/locks.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 22 | #include "base/macros.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 23 | #include "base/utils.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include "quick/quick_method_frame_info.h" |
David Srbecky | 79aa624 | 2018-07-12 13:28:42 +0000 | [diff] [blame] | 25 | #include "stack_map.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | class ArtMethod; |
| 30 | |
Mythri Alle | 307fd6d | 2022-04-25 10:50:30 +0000 | [diff] [blame] | 31 | // Size in bytes of the should_deoptimize flag on stack. |
| 32 | // We just need 4 bytes for our purpose regardless of the architecture. Frame size |
| 33 | // calculation will automatically do alignment for the final frame size. |
| 34 | static constexpr size_t kShouldDeoptimizeFlagSize = 4; |
| 35 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 36 | // OatQuickMethodHeader precedes the raw code chunk generated by the compiler. |
| 37 | class PACKED(4) OatQuickMethodHeader { |
| 38 | public: |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 39 | OatQuickMethodHeader(uint32_t code_info_offset = 0) { |
| 40 | SetCodeInfoOffset(code_info_offset); |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 41 | } |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 42 | |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 43 | static OatQuickMethodHeader* NterpMethodHeader; |
| 44 | |
| 45 | bool IsNterpMethodHeader() const; |
| 46 | |
Nicolas Geoffray | 409d1db | 2021-11-12 16:27:10 +0000 | [diff] [blame] | 47 | static bool IsNterpPc(uintptr_t pc) { |
| 48 | return OatQuickMethodHeader::NterpMethodHeader != nullptr && |
| 49 | OatQuickMethodHeader::NterpMethodHeader->Contains(pc); |
| 50 | } |
| 51 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 52 | static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) { |
| 53 | uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr); |
| 54 | uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_); |
Vladimir Marko | 562ff44 | 2015-10-27 18:51:20 +0000 | [diff] [blame] | 55 | DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) || |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 56 | IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA))) |
| 57 | << std::hex << code << " " << std::hex << header; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 58 | return reinterpret_cast<OatQuickMethodHeader*>(header); |
| 59 | } |
| 60 | |
| 61 | static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) { |
| 62 | return FromCodePointer(EntryPointToCodePointer(entry_point)); |
| 63 | } |
| 64 | |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 65 | static size_t InstructionAlignedSize() { |
| 66 | return RoundUp(sizeof(OatQuickMethodHeader), GetInstructionSetAlignment(kRuntimeISA)); |
| 67 | } |
| 68 | |
Yi Kong | 2665bc8 | 2017-05-09 15:55:57 -0700 | [diff] [blame] | 69 | OatQuickMethodHeader(const OatQuickMethodHeader&) = default; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 70 | OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default; |
| 71 | |
| 72 | uintptr_t NativeQuickPcOffset(const uintptr_t pc) const { |
| 73 | return pc - reinterpret_cast<uintptr_t>(GetEntryPoint()); |
| 74 | } |
| 75 | |
David Srbecky | 0983f59 | 2021-04-08 16:36:19 +0100 | [diff] [blame] | 76 | ALWAYS_INLINE bool IsOptimized() const { |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 77 | uintptr_t code = reinterpret_cast<uintptr_t>(code_); |
| 78 | DCHECK_NE(data_, 0u) << std::hex << code; // Probably a padding of native code. |
Nicolas Geoffray | 248d5c4 | 2021-09-13 09:53:10 +0100 | [diff] [blame] | 79 | DCHECK_NE(data_, kInvalidData) << std::hex << code; // Probably a stub or trampoline. |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 80 | return (data_ & kIsCodeInfoMask) != 0; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 81 | } |
| 82 | |
David Srbecky | 0983f59 | 2021-04-08 16:36:19 +0100 | [diff] [blame] | 83 | ALWAYS_INLINE const uint8_t* GetOptimizedCodeInfoPtr() const { |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 84 | uint32_t offset = GetCodeInfoOffset(); |
| 85 | DCHECK_NE(offset, 0u); |
| 86 | return code_ - offset; |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 87 | } |
| 88 | |
David Srbecky | 0983f59 | 2021-04-08 16:36:19 +0100 | [diff] [blame] | 89 | ALWAYS_INLINE uint8_t* GetOptimizedCodeInfoPtr() { |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 90 | uint32_t offset = GetCodeInfoOffset(); |
| 91 | DCHECK_NE(offset, 0u); |
| 92 | return code_ - offset; |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 93 | } |
| 94 | |
David Srbecky | 0983f59 | 2021-04-08 16:36:19 +0100 | [diff] [blame] | 95 | ALWAYS_INLINE const uint8_t* GetCode() const { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 96 | return code_; |
| 97 | } |
| 98 | |
David Srbecky | 0983f59 | 2021-04-08 16:36:19 +0100 | [diff] [blame] | 99 | ALWAYS_INLINE uint32_t GetCodeSize() const { |
| 100 | return LIKELY(IsOptimized()) |
| 101 | ? CodeInfo::DecodeCodeSize(GetOptimizedCodeInfoPtr()) |
| 102 | : (data_ & kCodeSizeMask); |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 103 | } |
| 104 | |
David Srbecky | 0983f59 | 2021-04-08 16:36:19 +0100 | [diff] [blame] | 105 | ALWAYS_INLINE uint32_t GetCodeInfoOffset() const { |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 106 | DCHECK(IsOptimized()); |
| 107 | return data_ & kCodeInfoMask; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 108 | } |
| 109 | |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 110 | void SetCodeInfoOffset(uint32_t offset) { |
| 111 | data_ = kIsCodeInfoMask | offset; |
| 112 | DCHECK_EQ(GetCodeInfoOffset(), offset); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | bool Contains(uintptr_t pc) const { |
Nicolas Geoffray | 248d5c4 | 2021-09-13 09:53:10 +0100 | [diff] [blame] | 116 | // We should not call `Contains` on a stub or trampoline. |
| 117 | DCHECK_NE(data_, kInvalidData) << std::hex << reinterpret_cast<uintptr_t>(code_); |
David Srbecky | 2acd1ec | 2020-05-16 01:38:49 +0100 | [diff] [blame] | 118 | // Remove hwasan tag to make comparison below valid. The PC from the stack does not have it. |
| 119 | uintptr_t code_start = reinterpret_cast<uintptr_t>(HWASanUntag(code_)); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 120 | static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 121 | if (kRuntimeISA == InstructionSet::kArm) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 122 | // On Thumb-2, the pc is offset by one. |
| 123 | code_start++; |
| 124 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 125 | return code_start <= pc && pc <= (code_start + GetCodeSize()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | const uint8_t* GetEntryPoint() const { |
| 129 | // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm` |
| 130 | // (not `kThumb2`), *but* we always generate code for the Thumb-2 |
| 131 | // instruction set anyway. Thumb-2 requires the entrypoint to be of |
| 132 | // offset 1. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 133 | static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 134 | return (kRuntimeISA == InstructionSet::kArm) |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 135 | ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1) |
| 136 | : code_; |
| 137 | } |
| 138 | |
| 139 | template <bool kCheckFrameSize = true> |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 140 | uint32_t GetFrameSizeInBytes() const { |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 141 | uint32_t result = GetFrameInfo().FrameSizeInBytes(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 142 | if (kCheckFrameSize) { |
David Srbecky | 6832fbe | 2016-03-11 18:48:55 +0000 | [diff] [blame] | 143 | DCHECK_ALIGNED(result, kStackAlignment); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 144 | } |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | QuickMethodFrameInfo GetFrameInfo() const { |
David Srbecky | 79aa624 | 2018-07-12 13:28:42 +0000 | [diff] [blame] | 149 | DCHECK(IsOptimized()); |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 150 | return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Mythri Alle | 307fd6d | 2022-04-25 10:50:30 +0000 | [diff] [blame] | 153 | size_t GetShouldDeoptimizeFlagOffset() const { |
| 154 | DCHECK(IsOptimized()); |
| 155 | QuickMethodFrameInfo frame_info = GetFrameInfo(); |
| 156 | size_t frame_size = frame_info.FrameSizeInBytes(); |
| 157 | size_t core_spill_size = |
| 158 | POPCOUNT(frame_info.CoreSpillMask()) * GetBytesPerGprSpillLocation(kRuntimeISA); |
| 159 | size_t fpu_spill_size = |
| 160 | POPCOUNT(frame_info.FpSpillMask()) * GetBytesPerFprSpillLocation(kRuntimeISA); |
| 161 | return frame_size - core_spill_size - fpu_spill_size - kShouldDeoptimizeFlagSize; |
| 162 | } |
| 163 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 164 | uintptr_t ToNativeQuickPc(ArtMethod* method, |
| 165 | const uint32_t dex_pc, |
| 166 | bool is_for_catch_handler, |
| 167 | bool abort_on_failure = true) const; |
| 168 | |
Nicolas Geoffray | a00b54b | 2019-12-03 14:36:42 +0000 | [diff] [blame] | 169 | uint32_t ToDexPc(ArtMethod** frame, |
| 170 | const uintptr_t pc, |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 171 | bool abort_on_failure = true) const |
| 172 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 173 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 174 | void SetHasShouldDeoptimizeFlag() { |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 175 | DCHECK(!HasShouldDeoptimizeFlag()); |
| 176 | data_ |= kShouldDeoptimizeMask; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | bool HasShouldDeoptimizeFlag() const { |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 180 | return (data_ & kShouldDeoptimizeMask) != 0; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | private: |
| 184 | static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000; |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 185 | static constexpr uint32_t kIsCodeInfoMask = 0x40000000; |
| 186 | static constexpr uint32_t kCodeInfoMask = 0x3FFFFFFF; // If kIsCodeInfoMask is set. |
| 187 | static constexpr uint32_t kCodeSizeMask = 0x3FFFFFFF; // If kIsCodeInfoMask is clear. |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 188 | |
Nicolas Geoffray | 248d5c4 | 2021-09-13 09:53:10 +0100 | [diff] [blame] | 189 | // In order to not confuse a stub with Java-generated code, we prefix each |
| 190 | // stub with a 0xFFFFFFFF marker. |
| 191 | static constexpr uint32_t kInvalidData = 0xFFFFFFFF; |
| 192 | |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 193 | uint32_t data_ = 0u; // Combination of fields using the above masks. |
| 194 | uint8_t code_[0]; // The actual method code. |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | } // namespace art |
| 198 | |
| 199 | #endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |