blob: e32fc052f5042b22142025164d9f7341c9733068 [file] [log] [blame]
Nicolas Geoffray2a905b22019-06-06 09:04:07 +01001/*
2 * Copyright 2019 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_JIT_JIT_MEMORY_REGION_H_
18#define ART_RUNTIME_JIT_JIT_MEMORY_REGION_H_
19
20#include <string>
21
Nicolas Geoffray349845a2019-06-19 13:13:10 +010022#include "arch/instruction_set.h"
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010023#include "base/globals.h"
24#include "base/locks.h"
25#include "base/mem_map.h"
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +010026#include "gc_root-inl.h"
27#include "handle.h"
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010028
29namespace art {
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +010030
31namespace mirror {
32class Object;
33}
34
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010035namespace jit {
36
Nicolas Geoffray2411f492019-06-14 08:54:46 +010037class TestZygoteMemory;
38
Orion Hodson521ff982019-06-18 13:56:28 +010039// Number of bytes represented by a bit in the CodeCacheBitmap. Value is reasonable for all
40// architectures.
41static constexpr int kJitCodeAccountingBytes = 16;
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010042
Nicolas Geoffray349845a2019-06-19 13:13:10 +010043size_t inline GetJitCodeAlignment() {
44 if (kRuntimeISA == InstructionSet::kArm || kRuntimeISA == InstructionSet::kThumb2) {
45 // Some devices with 32-bit ARM kernels need additional JIT code alignment when using dual
46 // view JIT (b/132205399). The alignment returned here coincides with the typical ARM d-cache
47 // line (though the value should be probed ideally). Both the method header and code in the
48 // cache are aligned to this size.
49 return 64;
50 }
51 return GetInstructionSetAlignment(kRuntimeISA);
52}
53
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +010054// Helper to get the size required for emitting `number_of_roots` in the
55// data portion of a JIT memory region.
56uint32_t inline ComputeRootTableSize(uint32_t number_of_roots) {
57 return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>);
58}
59
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010060// Represents a memory region for the JIT, where code and data are stored. This class
61// provides allocation and deallocation primitives.
62class JitMemoryRegion {
63 public:
64 JitMemoryRegion()
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010065 : initial_capacity_(0),
66 max_capacity_(0),
67 current_capacity_(0),
68 data_end_(0),
69 exec_end_(0),
70 used_memory_for_code_(0),
71 used_memory_for_data_(0),
72 exec_pages_(),
73 non_exec_pages_(),
74 data_mspace_(nullptr),
75 exec_mspace_(nullptr) {}
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010076
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010077 bool Initialize(size_t initial_capacity,
78 size_t max_capacity,
79 bool rwx_memory_allowed,
80 bool is_zygote,
81 std::string* error_msg)
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010082 REQUIRES(Locks::jit_lock_);
83
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010084 // Try to increase the current capacity of the code cache. Return whether we
85 // succeeded at doing so.
86 bool IncreaseCodeCacheCapacity() REQUIRES(Locks::jit_lock_);
87
88 // Set the footprint limit of the code cache.
89 void SetFootprintLimit(size_t new_footprint) REQUIRES(Locks::jit_lock_);
Nicolas Geoffray349845a2019-06-19 13:13:10 +010090
91 // Copy the code into the region, and allocate an OatQuickMethodHeader.
92 // Callers should not write into the returned memory, as it may be read-only.
93 const uint8_t* AllocateCode(const uint8_t* code,
94 size_t code_size,
95 const uint8_t* stack_map,
96 bool has_should_deoptimize_flag)
97 REQUIRES(Locks::jit_lock_);
98 void FreeCode(const uint8_t* code) REQUIRES(Locks::jit_lock_);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010099 uint8_t* AllocateData(size_t data_size) REQUIRES(Locks::jit_lock_);
100 void FreeData(uint8_t* data) REQUIRES(Locks::jit_lock_);
101
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +0100102 // Emit roots and stack map into the memory pointed by `roots_data`.
103 void CommitData(uint8_t* roots_data,
104 const std::vector<Handle<mirror::Object>>& roots,
105 const uint8_t* stack_map,
106 size_t stack_map_size)
107 REQUIRES(Locks::jit_lock_)
108 REQUIRES_SHARED(Locks::mutator_lock_);
109
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100110 bool IsValid() const NO_THREAD_SAFETY_ANALYSIS {
111 return exec_mspace_ != nullptr || data_mspace_ != nullptr;
112 }
113
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100114 bool HasDualCodeMapping() const {
115 return non_exec_pages_.IsValid();
116 }
117
118 bool HasCodeMapping() const {
119 return exec_pages_.IsValid();
120 }
121
122 bool IsInDataSpace(const void* ptr) const {
123 return data_pages_.HasAddress(ptr);
124 }
125
126 bool IsInExecSpace(const void* ptr) const {
127 return exec_pages_.HasAddress(ptr);
128 }
129
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100130 const MemMap* GetExecPages() const {
131 return &exec_pages_;
132 }
133
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100134 void* MoreCore(const void* mspace, intptr_t increment);
135
136 bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS {
137 return mspace == data_mspace_ || mspace == exec_mspace_;
138 }
139
140 size_t GetCurrentCapacity() const REQUIRES(Locks::jit_lock_) {
141 return current_capacity_;
142 }
143
144 size_t GetMaxCapacity() const REQUIRES(Locks::jit_lock_) {
145 return max_capacity_;
146 }
147
148 size_t GetUsedMemoryForCode() const REQUIRES(Locks::jit_lock_) {
149 return used_memory_for_code_;
150 }
151
152 size_t GetUsedMemoryForData() const REQUIRES(Locks::jit_lock_) {
153 return used_memory_for_data_;
154 }
155
156 private:
157 template <typename T>
158 T* TranslateAddress(T* src_ptr, const MemMap& src, const MemMap& dst) {
159 if (!HasDualCodeMapping()) {
160 return src_ptr;
161 }
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100162 CHECK(src.HasAddress(src_ptr)) << reinterpret_cast<const void*>(src_ptr);
163 const uint8_t* const raw_src_ptr = reinterpret_cast<const uint8_t*>(src_ptr);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100164 return reinterpret_cast<T*>(raw_src_ptr - src.Begin() + dst.Begin());
165 }
166
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100167 const MemMap* GetUpdatableCodeMapping() const {
168 if (HasDualCodeMapping()) {
169 return &non_exec_pages_;
170 } else if (HasCodeMapping()) {
171 return &exec_pages_;
172 } else {
173 return nullptr;
174 }
175 }
176
177 template <typename T> T* GetExecutableAddress(T* src_ptr) {
178 return TranslateAddress(src_ptr, non_exec_pages_, exec_pages_);
179 }
180
181 template <typename T> T* GetNonExecutableAddress(T* src_ptr) {
182 return TranslateAddress(src_ptr, exec_pages_, non_exec_pages_);
183 }
184
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100185 static int CreateZygoteMemory(size_t capacity, std::string* error_msg);
186 static bool ProtectZygoteMemory(int fd, std::string* error_msg);
187
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100188 // The initial capacity in bytes this code region starts with.
189 size_t initial_capacity_ GUARDED_BY(Locks::jit_lock_);
190
191 // The maximum capacity in bytes this region can go to.
192 size_t max_capacity_ GUARDED_BY(Locks::jit_lock_);
193
194 // The current capacity in bytes of the region.
195 size_t current_capacity_ GUARDED_BY(Locks::jit_lock_);
196
197 // The current footprint in bytes of the data portion of the region.
198 size_t data_end_ GUARDED_BY(Locks::jit_lock_);
199
200 // The current footprint in bytes of the code portion of the region.
201 size_t exec_end_ GUARDED_BY(Locks::jit_lock_);
202
203 // The size in bytes of used memory for the code portion of the region.
204 size_t used_memory_for_code_ GUARDED_BY(Locks::jit_lock_);
205
206 // The size in bytes of used memory for the data portion of the region.
207 size_t used_memory_for_data_ GUARDED_BY(Locks::jit_lock_);
208
209 // Mem map which holds data (stack maps and profiling info).
210 MemMap data_pages_;
211
212 // Mem map which holds code and has executable permission.
213 MemMap exec_pages_;
214
215 // Mem map which holds code with non executable permission. Only valid for dual view JIT when
216 // this is the non-executable view of code used to write updates.
217 MemMap non_exec_pages_;
218
219 // The opaque mspace for allocating data.
220 void* data_mspace_ GUARDED_BY(Locks::jit_lock_);
221
222 // The opaque mspace for allocating code.
223 void* exec_mspace_ GUARDED_BY(Locks::jit_lock_);
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100224
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100225 friend class ScopedCodeCacheWrite; // For GetUpdatableCodeMapping
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100226 friend class TestZygoteMemory;
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100227};
228
229} // namespace jit
230} // namespace art
231
232#endif // ART_RUNTIME_JIT_JIT_MEMORY_REGION_H_