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