blob: c8ee9b4e9136ef1736316fdee68b2a67285b7ef4 [file] [log] [blame]
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001/*
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 Geoffray013d1ee2019-12-04 16:18:15 +000021#include "base/locks.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010022#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080023#include "base/utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "quick/quick_method_frame_info.h"
David Srbecky79aa6242018-07-12 13:28:42 +000025#include "stack_map.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010026
27namespace art {
28
29class ArtMethod;
30
31// OatQuickMethodHeader precedes the raw code chunk generated by the compiler.
32class PACKED(4) OatQuickMethodHeader {
33 public:
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070034 OatQuickMethodHeader() = default;
Nicolas Geoffray57083762019-03-05 09:24:45 +000035 OatQuickMethodHeader(uint32_t vmap_table_offset,
36 uint32_t code_size)
37 : vmap_table_offset_(vmap_table_offset),
38 code_size_(code_size) {
David Srbecky88087562018-06-23 22:05:56 +010039 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010040
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000041 static OatQuickMethodHeader* NterpMethodHeader;
42
43 bool IsNterpMethodHeader() const;
44
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010045 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 Marko562ff442015-10-27 18:51:20 +000048 DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
Jeff Haodcdc85b2015-12-04 14:06:18 -080049 IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
50 << std::hex << code << " " << std::hex << header;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010051 return reinterpret_cast<OatQuickMethodHeader*>(header);
52 }
53
54 static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
55 return FromCodePointer(EntryPointToCodePointer(entry_point));
56 }
57
David Srbeckyadb66f92019-10-10 12:59:43 +000058 static size_t InstructionAlignedSize() {
59 return RoundUp(sizeof(OatQuickMethodHeader), GetInstructionSetAlignment(kRuntimeISA));
60 }
61
Yi Kong2665bc82017-05-09 15:55:57 -070062 OatQuickMethodHeader(const OatQuickMethodHeader&) = default;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010063 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 Geoffraybf5f0f32019-03-05 15:41:50 +000070 return GetCodeSize() != 0 && vmap_table_offset_ != 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010071 }
72
David Srbecky79aa6242018-07-12 13:28:42 +000073 const uint8_t* GetOptimizedCodeInfoPtr() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +000074 DCHECK(IsOptimized());
75 return code_ - vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +000076 }
77
Nicolas Geoffray132d8362016-11-16 09:19:42 +000078 uint8_t* GetOptimizedCodeInfoPtr() {
Nicolas Geoffray57083762019-03-05 09:24:45 +000079 DCHECK(IsOptimized());
80 return code_ - vmap_table_offset_;
Nicolas Geoffray132d8362016-11-16 09:19:42 +000081 }
82
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010083 const uint8_t* GetCode() const {
84 return code_;
85 }
86
David Srbecky5d950762016-03-07 20:47:29 +000087 uint32_t GetCodeSize() const {
David Srbecky1eb5d872019-04-03 13:56:22 +010088 // 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 Geoffraya00b54b2019-12-03 14:36:42 +000091 CHECK_NE(code_size_, 0xFFFFFFFF) << code_size_;
Nicolas Geoffraybf5f0f32019-03-05 15:41:50 +000092 return code_size_ & kCodeSizeMask;
Nicolas Geoffray57083762019-03-05 09:24:45 +000093 }
94
95 const uint32_t* GetCodeSizeAddr() const {
96 return &code_size_;
Mingyao Yang063fc772016-08-02 11:02:54 -070097 }
98
99 uint32_t GetVmapTableOffset() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000100 return vmap_table_offset_;
Mingyao Yang063fc772016-08-02 11:02:54 -0700101 }
102
103 void SetVmapTableOffset(uint32_t offset) {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000104 vmap_table_offset_ = offset;
Mingyao Yang063fc772016-08-02 11:02:54 -0700105 }
106
107 const uint32_t* GetVmapTableOffsetAddr() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000108 return &vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +0000109 }
110
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100111 const uint8_t* GetVmapTable() const {
112 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
Nicolas Geoffray57083762019-03-05 09:24:45 +0000113 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100114 }
115
116 bool Contains(uintptr_t pc) const {
117 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Vladimir Marko33bff252017-11-01 14:35:42 +0000118 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
119 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100120 // On Thumb-2, the pc is offset by one.
121 code_start++;
122 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700123 return code_start <= pc && pc <= (code_start + GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100124 }
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 Marko33bff252017-11-01 14:35:42 +0000131 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
132 return (kRuntimeISA == InstructionSet::kArm)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100133 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
134 : code_;
135 }
136
137 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000138 uint32_t GetFrameSizeInBytes() const {
David Srbecky88087562018-06-23 22:05:56 +0100139 uint32_t result = GetFrameInfo().FrameSizeInBytes();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100140 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000141 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100142 }
143 return result;
144 }
145
146 QuickMethodFrameInfo GetFrameInfo() const {
David Srbecky79aa6242018-07-12 13:28:42 +0000147 DCHECK(IsOptimized());
David Srbecky88087562018-06-23 22:05:56 +0100148 return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100149 }
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 Geoffraya00b54b2019-12-03 14:36:42 +0000156 uint32_t ToDexPc(ArtMethod** frame,
157 const uintptr_t pc,
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000158 bool abort_on_failure = true) const
159 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100160
Mingyao Yang063fc772016-08-02 11:02:54 -0700161 void SetHasShouldDeoptimizeFlag() {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000162 DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u);
163 code_size_ |= kShouldDeoptimizeMask;
Mingyao Yang063fc772016-08-02 11:02:54 -0700164 }
165
166 bool HasShouldDeoptimizeFlag() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000167 return (code_size_ & kShouldDeoptimizeMask) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700168 }
169
170 private:
171 static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
Nicolas Geoffray57083762019-03-05 09:24:45 +0000172 static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask;
Mingyao Yang063fc772016-08-02 11:02:54 -0700173
Nicolas Geoffray57083762019-03-05 09:24:45 +0000174 // 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 Geoffray524e7ea2015-10-16 17:13:34 +0100179 // The actual code.
180 uint8_t code_[0];
181};
182
183} // namespace art
184
185#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_