blob: 0d08149693f17b266c84b7ee6de9a51a085530e5 [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"
21#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080022#include "base/utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "quick/quick_method_frame_info.h"
David Srbecky79aa6242018-07-12 13:28:42 +000024#include "stack_map.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010025
26namespace art {
27
28class ArtMethod;
29
30// OatQuickMethodHeader precedes the raw code chunk generated by the compiler.
31class PACKED(4) OatQuickMethodHeader {
32 public:
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070033 OatQuickMethodHeader() = default;
Nicolas Geoffray57083762019-03-05 09:24:45 +000034 OatQuickMethodHeader(uint32_t vmap_table_offset,
35 uint32_t code_size)
36 : vmap_table_offset_(vmap_table_offset),
37 code_size_(code_size) {
David Srbecky88087562018-06-23 22:05:56 +010038 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010040 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 Marko562ff442015-10-27 18:51:20 +000043 DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
Jeff Haodcdc85b2015-12-04 14:06:18 -080044 IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
45 << std::hex << code << " " << std::hex << header;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010046 return reinterpret_cast<OatQuickMethodHeader*>(header);
47 }
48
49 static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
50 return FromCodePointer(EntryPointToCodePointer(entry_point));
51 }
52
David Srbeckyadb66f92019-10-10 12:59:43 +000053 static size_t InstructionAlignedSize() {
54 return RoundUp(sizeof(OatQuickMethodHeader), GetInstructionSetAlignment(kRuntimeISA));
55 }
56
Yi Kong2665bc82017-05-09 15:55:57 -070057 OatQuickMethodHeader(const OatQuickMethodHeader&) = default;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010058 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 Geoffraybf5f0f32019-03-05 15:41:50 +000065 return GetCodeSize() != 0 && vmap_table_offset_ != 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010066 }
67
David Srbecky79aa6242018-07-12 13:28:42 +000068 const uint8_t* GetOptimizedCodeInfoPtr() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +000069 DCHECK(IsOptimized());
70 return code_ - vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +000071 }
72
Nicolas Geoffray132d8362016-11-16 09:19:42 +000073 uint8_t* GetOptimizedCodeInfoPtr() {
Nicolas Geoffray57083762019-03-05 09:24:45 +000074 DCHECK(IsOptimized());
75 return code_ - vmap_table_offset_;
Nicolas Geoffray132d8362016-11-16 09:19:42 +000076 }
77
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010078 const uint8_t* GetCode() const {
79 return code_;
80 }
81
David Srbecky5d950762016-03-07 20:47:29 +000082 uint32_t GetCodeSize() const {
David Srbecky1eb5d872019-04-03 13:56:22 +010083 // 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 Geoffraya00b54b2019-12-03 14:36:42 +000086 CHECK_NE(code_size_, 0xFFFFFFFF) << code_size_;
Nicolas Geoffraybf5f0f32019-03-05 15:41:50 +000087 return code_size_ & kCodeSizeMask;
Nicolas Geoffray57083762019-03-05 09:24:45 +000088 }
89
90 const uint32_t* GetCodeSizeAddr() const {
91 return &code_size_;
Mingyao Yang063fc772016-08-02 11:02:54 -070092 }
93
94 uint32_t GetVmapTableOffset() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +000095 return vmap_table_offset_;
Mingyao Yang063fc772016-08-02 11:02:54 -070096 }
97
98 void SetVmapTableOffset(uint32_t offset) {
Nicolas Geoffray57083762019-03-05 09:24:45 +000099 vmap_table_offset_ = offset;
Mingyao Yang063fc772016-08-02 11:02:54 -0700100 }
101
102 const uint32_t* GetVmapTableOffsetAddr() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000103 return &vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +0000104 }
105
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100106 const uint8_t* GetVmapTable() const {
107 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
Nicolas Geoffray57083762019-03-05 09:24:45 +0000108 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100109 }
110
111 bool Contains(uintptr_t pc) const {
112 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Vladimir Marko33bff252017-11-01 14:35:42 +0000113 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
114 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100115 // On Thumb-2, the pc is offset by one.
116 code_start++;
117 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700118 return code_start <= pc && pc <= (code_start + GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100119 }
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 Marko33bff252017-11-01 14:35:42 +0000126 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
127 return (kRuntimeISA == InstructionSet::kArm)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100128 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
129 : code_;
130 }
131
132 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000133 uint32_t GetFrameSizeInBytes() const {
David Srbecky88087562018-06-23 22:05:56 +0100134 uint32_t result = GetFrameInfo().FrameSizeInBytes();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100135 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000136 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100137 }
138 return result;
139 }
140
141 QuickMethodFrameInfo GetFrameInfo() const {
David Srbecky79aa6242018-07-12 13:28:42 +0000142 DCHECK(IsOptimized());
David Srbecky88087562018-06-23 22:05:56 +0100143 return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100144 }
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 Geoffraya00b54b2019-12-03 14:36:42 +0000151 uint32_t ToDexPc(ArtMethod** frame,
152 const uintptr_t pc,
153 bool abort_on_failure = true) const;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100154
Mingyao Yang063fc772016-08-02 11:02:54 -0700155 void SetHasShouldDeoptimizeFlag() {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000156 DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u);
157 code_size_ |= kShouldDeoptimizeMask;
Mingyao Yang063fc772016-08-02 11:02:54 -0700158 }
159
160 bool HasShouldDeoptimizeFlag() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000161 return (code_size_ & kShouldDeoptimizeMask) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700162 }
163
164 private:
165 static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
Nicolas Geoffray57083762019-03-05 09:24:45 +0000166 static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask;
Mingyao Yang063fc772016-08-02 11:02:54 -0700167
Nicolas Geoffray57083762019-03-05 09:24:45 +0000168 // 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 Geoffray524e7ea2015-10-16 17:13:34 +0100173 // The actual code.
174 uint8_t code_[0];
175};
176
177} // namespace art
178
179#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_