blob: d09c46a55a6afdc3c0809c7ec5bb882e17070a4b [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 Geoffray00a37ff2019-06-20 14:27:22 +010043// Helper to get the size required for emitting `number_of_roots` in the
44// data portion of a JIT memory region.
45uint32_t inline ComputeRootTableSize(uint32_t number_of_roots) {
46 return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>);
47}
48
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010049// Represents a memory region for the JIT, where code and data are stored. This class
50// provides allocation and deallocation primitives.
51class JitMemoryRegion {
52 public:
53 JitMemoryRegion()
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010054 : initial_capacity_(0),
55 max_capacity_(0),
56 current_capacity_(0),
57 data_end_(0),
58 exec_end_(0),
59 used_memory_for_code_(0),
60 used_memory_for_data_(0),
Nicolas Geoffrayac933ed2019-06-26 13:36:37 +010061 data_pages_(),
62 writable_data_pages_(),
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010063 exec_pages_(),
64 non_exec_pages_(),
65 data_mspace_(nullptr),
66 exec_mspace_(nullptr) {}
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010067
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010068 bool Initialize(size_t initial_capacity,
69 size_t max_capacity,
70 bool rwx_memory_allowed,
71 bool is_zygote,
72 std::string* error_msg)
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010073 REQUIRES(Locks::jit_lock_);
74
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010075 // Try to increase the current capacity of the code cache. Return whether we
76 // succeeded at doing so.
77 bool IncreaseCodeCacheCapacity() REQUIRES(Locks::jit_lock_);
78
79 // Set the footprint limit of the code cache.
80 void SetFootprintLimit(size_t new_footprint) REQUIRES(Locks::jit_lock_);
Nicolas Geoffray349845a2019-06-19 13:13:10 +010081
82 // Copy the code into the region, and allocate an OatQuickMethodHeader.
83 // Callers should not write into the returned memory, as it may be read-only.
84 const uint8_t* AllocateCode(const uint8_t* code,
85 size_t code_size,
86 const uint8_t* stack_map,
87 bool has_should_deoptimize_flag)
88 REQUIRES(Locks::jit_lock_);
89 void FreeCode(const uint8_t* code) REQUIRES(Locks::jit_lock_);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010090 uint8_t* AllocateData(size_t data_size) REQUIRES(Locks::jit_lock_);
91 void FreeData(uint8_t* data) REQUIRES(Locks::jit_lock_);
92
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +010093 // Emit roots and stack map into the memory pointed by `roots_data`.
Orion Hodsonaeb02232019-06-25 14:18:18 +010094 bool CommitData(uint8_t* roots_data,
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +010095 const std::vector<Handle<mirror::Object>>& roots,
96 const uint8_t* stack_map,
97 size_t stack_map_size)
98 REQUIRES(Locks::jit_lock_)
99 REQUIRES_SHARED(Locks::mutator_lock_);
100
Nicolas Geoffray88f3fd92019-06-27 16:32:13 +0100101 void ResetWritableMappings() REQUIRES(Locks::jit_lock_) {
102 non_exec_pages_.ResetInForkedProcess();
103 writable_data_pages_.ResetInForkedProcess();
104 // Also clear the mspaces, which, in their implementation,
105 // point to the discarded mappings.
106 exec_mspace_ = nullptr;
107 data_mspace_ = nullptr;
108 }
109
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000110 bool IsValid() const NO_THREAD_SAFETY_ANALYSIS {
111 return exec_mspace_ != nullptr || data_mspace_ != nullptr;
112 }
113
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100114 template <typename T>
115 void FillData(T* address, size_t n, const T& t) REQUIRES(Locks::jit_lock_) {
116 std::fill_n(GetWritableDataAddress(address), n, t);
117 }
118
119 // Generic helper for writing abritrary data in the data portion of the
120 // region.
121 template <typename T>
122 void WriteData(T* address, const T& value) {
123 *GetWritableDataAddress(address) = value;
124 }
125
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100126 bool HasDualCodeMapping() const {
127 return non_exec_pages_.IsValid();
128 }
129
Nicolas Geoffrayac933ed2019-06-26 13:36:37 +0100130 bool HasDualDataMapping() const {
131 return writable_data_pages_.IsValid();
132 }
133
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100134 bool HasCodeMapping() const {
135 return exec_pages_.IsValid();
136 }
137
138 bool IsInDataSpace(const void* ptr) const {
139 return data_pages_.HasAddress(ptr);
140 }
141
142 bool IsInExecSpace(const void* ptr) const {
143 return exec_pages_.HasAddress(ptr);
144 }
145
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100146 const MemMap* GetExecPages() const {
147 return &exec_pages_;
148 }
149
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100150 void* MoreCore(const void* mspace, intptr_t increment);
151
152 bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS {
153 return mspace == data_mspace_ || mspace == exec_mspace_;
154 }
155
156 size_t GetCurrentCapacity() const REQUIRES(Locks::jit_lock_) {
157 return current_capacity_;
158 }
159
160 size_t GetMaxCapacity() const REQUIRES(Locks::jit_lock_) {
161 return max_capacity_;
162 }
163
164 size_t GetUsedMemoryForCode() const REQUIRES(Locks::jit_lock_) {
165 return used_memory_for_code_;
166 }
167
168 size_t GetUsedMemoryForData() const REQUIRES(Locks::jit_lock_) {
169 return used_memory_for_data_;
170 }
171
172 private:
173 template <typename T>
174 T* TranslateAddress(T* src_ptr, const MemMap& src, const MemMap& dst) {
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100175 CHECK(src.HasAddress(src_ptr)) << reinterpret_cast<const void*>(src_ptr);
176 const uint8_t* const raw_src_ptr = reinterpret_cast<const uint8_t*>(src_ptr);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100177 return reinterpret_cast<T*>(raw_src_ptr - src.Begin() + dst.Begin());
178 }
179
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100180 const MemMap* GetUpdatableCodeMapping() const {
181 if (HasDualCodeMapping()) {
182 return &non_exec_pages_;
183 } else if (HasCodeMapping()) {
184 return &exec_pages_;
185 } else {
186 return nullptr;
187 }
188 }
189
Nicolas Geoffrayac933ed2019-06-26 13:36:37 +0100190 const MemMap* GetWritableDataMapping() const {
191 if (HasDualDataMapping()) {
192 return &writable_data_pages_;
193 } else {
194 return &data_pages_;
195 }
196 }
197
198 template <typename T> T* GetNonWritableDataAddress(T* src_ptr) {
199 if (!HasDualDataMapping()) {
200 return src_ptr;
201 }
202 return TranslateAddress(src_ptr, writable_data_pages_, data_pages_);
203 }
204
205 template <typename T> T* GetWritableDataAddress(T* src_ptr) {
206 if (!HasDualDataMapping()) {
207 return src_ptr;
208 }
209 return TranslateAddress(src_ptr, data_pages_, writable_data_pages_);
210 }
211
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100212 template <typename T> T* GetExecutableAddress(T* src_ptr) {
Nicolas Geoffrayac933ed2019-06-26 13:36:37 +0100213 if (!HasDualCodeMapping()) {
214 return src_ptr;
215 }
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100216 return TranslateAddress(src_ptr, non_exec_pages_, exec_pages_);
217 }
218
219 template <typename T> T* GetNonExecutableAddress(T* src_ptr) {
Nicolas Geoffrayac933ed2019-06-26 13:36:37 +0100220 if (!HasDualCodeMapping()) {
221 return src_ptr;
222 }
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100223 return TranslateAddress(src_ptr, exec_pages_, non_exec_pages_);
224 }
225
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100226 static int CreateZygoteMemory(size_t capacity, std::string* error_msg);
227 static bool ProtectZygoteMemory(int fd, std::string* error_msg);
228
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100229 // The initial capacity in bytes this code region starts with.
230 size_t initial_capacity_ GUARDED_BY(Locks::jit_lock_);
231
232 // The maximum capacity in bytes this region can go to.
233 size_t max_capacity_ GUARDED_BY(Locks::jit_lock_);
234
235 // The current capacity in bytes of the region.
236 size_t current_capacity_ GUARDED_BY(Locks::jit_lock_);
237
238 // The current footprint in bytes of the data portion of the region.
239 size_t data_end_ GUARDED_BY(Locks::jit_lock_);
240
241 // The current footprint in bytes of the code portion of the region.
242 size_t exec_end_ GUARDED_BY(Locks::jit_lock_);
243
244 // The size in bytes of used memory for the code portion of the region.
245 size_t used_memory_for_code_ GUARDED_BY(Locks::jit_lock_);
246
247 // The size in bytes of used memory for the data portion of the region.
248 size_t used_memory_for_data_ GUARDED_BY(Locks::jit_lock_);
249
250 // Mem map which holds data (stack maps and profiling info).
251 MemMap data_pages_;
252
Nicolas Geoffrayac933ed2019-06-26 13:36:37 +0100253 // Mem map which holds data with writable permission. Only valid for dual view
254 // JIT when this is the writable view and data_pages_ is the readable view.
255 MemMap writable_data_pages_;
256
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100257 // Mem map which holds code and has executable permission.
258 MemMap exec_pages_;
259
260 // Mem map which holds code with non executable permission. Only valid for dual view JIT when
261 // this is the non-executable view of code used to write updates.
262 MemMap non_exec_pages_;
263
264 // The opaque mspace for allocating data.
265 void* data_mspace_ GUARDED_BY(Locks::jit_lock_);
266
267 // The opaque mspace for allocating code.
268 void* exec_mspace_ GUARDED_BY(Locks::jit_lock_);
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100269
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100270 friend class ScopedCodeCacheWrite; // For GetUpdatableCodeMapping
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100271 friend class TestZygoteMemory;
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100272};
273
274} // namespace jit
275} // namespace art
276
277#endif // ART_RUNTIME_JIT_JIT_MEMORY_REGION_H_