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"
22 #include "quick/quick_method_frame_info.h"
23 #include "stack_map.h"
24 #include "utils.h"
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:
33   OatQuickMethodHeader(uint32_t vmap_table_offset = 0U,
34                        uint32_t frame_size_in_bytes = 0U,
35                        uint32_t core_spill_mask = 0U,
36                        uint32_t fp_spill_mask = 0U,
37                        uint32_t code_size = 0U);
38 
39   ~OatQuickMethodHeader();
40 
FromCodePointer(const void * code_ptr)41   static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) {
42     uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr);
43     uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_);
44     DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
45            IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
46         << std::hex << code << " " << std::hex << header;
47     return reinterpret_cast<OatQuickMethodHeader*>(header);
48   }
49 
FromEntryPoint(const void * entry_point)50   static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
51     return FromCodePointer(EntryPointToCodePointer(entry_point));
52   }
53 
54   OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default;
55 
NativeQuickPcOffset(const uintptr_t pc)56   uintptr_t NativeQuickPcOffset(const uintptr_t pc) const {
57     return pc - reinterpret_cast<uintptr_t>(GetEntryPoint());
58   }
59 
IsOptimized()60   bool IsOptimized() const {
61     return code_size_ != 0 && vmap_table_offset_ != 0;
62   }
63 
GetOptimizedCodeInfoPtr()64   const void* GetOptimizedCodeInfoPtr() const {
65     DCHECK(IsOptimized());
66     const void* data = reinterpret_cast<const void*>(code_ - vmap_table_offset_);
67     return data;
68   }
69 
GetOptimizedCodeInfo()70   CodeInfo GetOptimizedCodeInfo() const {
71     return CodeInfo(GetOptimizedCodeInfoPtr());
72   }
73 
GetCode()74   const uint8_t* GetCode() const {
75     return code_;
76   }
77 
GetCodeSize()78   uint32_t GetCodeSize() const {
79     return code_size_;
80   }
81 
GetVmapTable()82   const uint8_t* GetVmapTable() const {
83     CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
84     return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
85   }
86 
Contains(uintptr_t pc)87   bool Contains(uintptr_t pc) const {
88     uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
89     static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
90     if (kRuntimeISA == kArm) {
91       // On Thumb-2, the pc is offset by one.
92       code_start++;
93     }
94     return code_start <= pc && pc <= (code_start + code_size_);
95   }
96 
GetEntryPoint()97   const uint8_t* GetEntryPoint() const {
98     // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
99     // (not `kThumb2`), *but* we always generate code for the Thumb-2
100     // instruction set anyway. Thumb-2 requires the entrypoint to be of
101     // offset 1.
102     static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
103     return (kRuntimeISA == kArm)
104         ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
105         : code_;
106   }
107 
108   template <bool kCheckFrameSize = true>
GetFrameSizeInBytes()109   uint32_t GetFrameSizeInBytes() const {
110     uint32_t result = frame_info_.FrameSizeInBytes();
111     if (kCheckFrameSize) {
112       DCHECK_ALIGNED(result, kStackAlignment);
113     }
114     return result;
115   }
116 
GetFrameInfo()117   QuickMethodFrameInfo GetFrameInfo() const {
118     return frame_info_;
119   }
120 
121   uintptr_t ToNativeQuickPc(ArtMethod* method,
122                             const uint32_t dex_pc,
123                             bool is_for_catch_handler,
124                             bool abort_on_failure = true) const;
125 
126   uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
127 
128   // The offset in bytes from the start of the vmap table to the end of the header.
129   uint32_t vmap_table_offset_;
130   // The stack frame information.
131   QuickMethodFrameInfo frame_info_;
132   // The code size in bytes.
133   uint32_t code_size_;
134   // The actual code.
135   uint8_t code_[0];
136 };
137 
138 }  // namespace art
139 
140 #endif  // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_
141