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 | |
| 31 | // OatQuickMethodHeader precedes the raw code chunk generated by the compiler. |
| 32 | class PACKED(4) OatQuickMethodHeader { |
| 33 | public: |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 34 | OatQuickMethodHeader() = default; |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 35 | OatQuickMethodHeader(uint32_t vmap_table_offset, |
| 36 | uint32_t code_size) |
| 37 | : vmap_table_offset_(vmap_table_offset), |
| 38 | code_size_(code_size) { |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 39 | } |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 40 | |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 41 | static OatQuickMethodHeader* NterpMethodHeader; |
| 42 | |
| 43 | bool IsNterpMethodHeader() const; |
| 44 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 45 | static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) { |
| 46 | uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr); |
| 47 | uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_); |
Vladimir Marko | 562ff44 | 2015-10-27 18:51:20 +0000 | [diff] [blame] | 48 | DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) || |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 49 | IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA))) |
| 50 | << std::hex << code << " " << std::hex << header; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 51 | return reinterpret_cast<OatQuickMethodHeader*>(header); |
| 52 | } |
| 53 | |
| 54 | static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) { |
| 55 | return FromCodePointer(EntryPointToCodePointer(entry_point)); |
| 56 | } |
| 57 | |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 58 | static size_t InstructionAlignedSize() { |
| 59 | return RoundUp(sizeof(OatQuickMethodHeader), GetInstructionSetAlignment(kRuntimeISA)); |
| 60 | } |
| 61 | |
Yi Kong | 2665bc8 | 2017-05-09 15:55:57 -0700 | [diff] [blame] | 62 | OatQuickMethodHeader(const OatQuickMethodHeader&) = default; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 63 | OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default; |
| 64 | |
| 65 | uintptr_t NativeQuickPcOffset(const uintptr_t pc) const { |
| 66 | return pc - reinterpret_cast<uintptr_t>(GetEntryPoint()); |
| 67 | } |
| 68 | |
| 69 | bool IsOptimized() const { |
Nicolas Geoffray | bf5f0f3 | 2019-03-05 15:41:50 +0000 | [diff] [blame] | 70 | return GetCodeSize() != 0 && vmap_table_offset_ != 0; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 71 | } |
| 72 | |
David Srbecky | 79aa624 | 2018-07-12 13:28:42 +0000 | [diff] [blame] | 73 | const uint8_t* GetOptimizedCodeInfoPtr() const { |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 74 | DCHECK(IsOptimized()); |
| 75 | return code_ - vmap_table_offset_; |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 78 | uint8_t* GetOptimizedCodeInfoPtr() { |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 79 | DCHECK(IsOptimized()); |
| 80 | return code_ - vmap_table_offset_; |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 83 | const uint8_t* GetCode() const { |
| 84 | return code_; |
| 85 | } |
| 86 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 87 | uint32_t GetCodeSize() const { |
David Srbecky | 1eb5d87 | 2019-04-03 13:56:22 +0100 | [diff] [blame] | 88 | // ART compiled method are prefixed with header, but we can also easily |
| 89 | // accidentally use a function pointer to one of the stubs/trampolines. |
| 90 | // We prefix those with 0xFF in the aseembly so that we can do DCHECKs. |
Nicolas Geoffray | a00b54b | 2019-12-03 14:36:42 +0000 | [diff] [blame] | 91 | CHECK_NE(code_size_, 0xFFFFFFFF) << code_size_; |
Nicolas Geoffray | bf5f0f3 | 2019-03-05 15:41:50 +0000 | [diff] [blame] | 92 | return code_size_ & kCodeSizeMask; |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | const uint32_t* GetCodeSizeAddr() const { |
| 96 | return &code_size_; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | uint32_t GetVmapTableOffset() const { |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 100 | return vmap_table_offset_; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void SetVmapTableOffset(uint32_t offset) { |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 104 | vmap_table_offset_ = offset; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | const uint32_t* GetVmapTableOffsetAddr() const { |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 108 | return &vmap_table_offset_; |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 111 | const uint8_t* GetVmapTable() const { |
| 112 | CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler"; |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 113 | return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool Contains(uintptr_t pc) const { |
| 117 | uintptr_t code_start = reinterpret_cast<uintptr_t>(code_); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 118 | static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 119 | if (kRuntimeISA == InstructionSet::kArm) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 120 | // On Thumb-2, the pc is offset by one. |
| 121 | code_start++; |
| 122 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 123 | return code_start <= pc && pc <= (code_start + GetCodeSize()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | const uint8_t* GetEntryPoint() const { |
| 127 | // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm` |
| 128 | // (not `kThumb2`), *but* we always generate code for the Thumb-2 |
| 129 | // instruction set anyway. Thumb-2 requires the entrypoint to be of |
| 130 | // offset 1. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 131 | static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 132 | return (kRuntimeISA == InstructionSet::kArm) |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 133 | ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1) |
| 134 | : code_; |
| 135 | } |
| 136 | |
| 137 | template <bool kCheckFrameSize = true> |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 138 | uint32_t GetFrameSizeInBytes() const { |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 139 | uint32_t result = GetFrameInfo().FrameSizeInBytes(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 140 | if (kCheckFrameSize) { |
David Srbecky | 6832fbe | 2016-03-11 18:48:55 +0000 | [diff] [blame] | 141 | DCHECK_ALIGNED(result, kStackAlignment); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 142 | } |
| 143 | return result; |
| 144 | } |
| 145 | |
| 146 | QuickMethodFrameInfo GetFrameInfo() const { |
David Srbecky | 79aa624 | 2018-07-12 13:28:42 +0000 | [diff] [blame] | 147 | DCHECK(IsOptimized()); |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 148 | return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | uintptr_t ToNativeQuickPc(ArtMethod* method, |
| 152 | const uint32_t dex_pc, |
| 153 | bool is_for_catch_handler, |
| 154 | bool abort_on_failure = true) const; |
| 155 | |
Nicolas Geoffray | a00b54b | 2019-12-03 14:36:42 +0000 | [diff] [blame] | 156 | uint32_t ToDexPc(ArtMethod** frame, |
| 157 | const uintptr_t pc, |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 158 | bool abort_on_failure = true) const |
| 159 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 160 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 161 | void SetHasShouldDeoptimizeFlag() { |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 162 | DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u); |
| 163 | code_size_ |= kShouldDeoptimizeMask; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | bool HasShouldDeoptimizeFlag() const { |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 167 | return (code_size_ & kShouldDeoptimizeMask) != 0; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private: |
| 171 | static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000; |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 172 | static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 173 | |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 174 | // The offset in bytes from the start of the vmap table to the end of the header. |
| 175 | uint32_t vmap_table_offset_ = 0u; |
| 176 | // The code size in bytes. The highest bit is used to signify if the compiled |
| 177 | // code with the method header has should_deoptimize flag. |
| 178 | uint32_t code_size_ = 0u; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 179 | // The actual code. |
| 180 | uint8_t code_[0]; |
| 181 | }; |
| 182 | |
| 183 | } // namespace art |
| 184 | |
| 185 | #endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |