blob: f325480f6274fa0af5226eb957c524d554a06a0a [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),
61 exec_pages_(),
62 non_exec_pages_(),
63 data_mspace_(nullptr),
64 exec_mspace_(nullptr) {}
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010065
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010066 bool Initialize(size_t initial_capacity,
67 size_t max_capacity,
68 bool rwx_memory_allowed,
69 bool is_zygote,
70 std::string* error_msg)
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010071 REQUIRES(Locks::jit_lock_);
72
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010073 // Try to increase the current capacity of the code cache. Return whether we
74 // succeeded at doing so.
75 bool IncreaseCodeCacheCapacity() REQUIRES(Locks::jit_lock_);
76
77 // Set the footprint limit of the code cache.
78 void SetFootprintLimit(size_t new_footprint) REQUIRES(Locks::jit_lock_);
Nicolas Geoffray349845a2019-06-19 13:13:10 +010079
80 // Copy the code into the region, and allocate an OatQuickMethodHeader.
81 // Callers should not write into the returned memory, as it may be read-only.
82 const uint8_t* AllocateCode(const uint8_t* code,
83 size_t code_size,
84 const uint8_t* stack_map,
85 bool has_should_deoptimize_flag)
86 REQUIRES(Locks::jit_lock_);
87 void FreeCode(const uint8_t* code) REQUIRES(Locks::jit_lock_);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010088 uint8_t* AllocateData(size_t data_size) REQUIRES(Locks::jit_lock_);
89 void FreeData(uint8_t* data) REQUIRES(Locks::jit_lock_);
90
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +010091 // Emit roots and stack map into the memory pointed by `roots_data`.
Orion Hodsonaeb02232019-06-25 14:18:18 +010092 bool CommitData(uint8_t* roots_data,
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +010093 const std::vector<Handle<mirror::Object>>& roots,
94 const uint8_t* stack_map,
95 size_t stack_map_size)
96 REQUIRES(Locks::jit_lock_)
97 REQUIRES_SHARED(Locks::mutator_lock_);
98
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010099 bool HasDualCodeMapping() const {
100 return non_exec_pages_.IsValid();
101 }
102
103 bool HasCodeMapping() const {
104 return exec_pages_.IsValid();
105 }
106
107 bool IsInDataSpace(const void* ptr) const {
108 return data_pages_.HasAddress(ptr);
109 }
110
111 bool IsInExecSpace(const void* ptr) const {
112 return exec_pages_.HasAddress(ptr);
113 }
114
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100115 const MemMap* GetExecPages() const {
116 return &exec_pages_;
117 }
118
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100119 void* MoreCore(const void* mspace, intptr_t increment);
120
121 bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS {
122 return mspace == data_mspace_ || mspace == exec_mspace_;
123 }
124
125 size_t GetCurrentCapacity() const REQUIRES(Locks::jit_lock_) {
126 return current_capacity_;
127 }
128
129 size_t GetMaxCapacity() const REQUIRES(Locks::jit_lock_) {
130 return max_capacity_;
131 }
132
133 size_t GetUsedMemoryForCode() const REQUIRES(Locks::jit_lock_) {
134 return used_memory_for_code_;
135 }
136
137 size_t GetUsedMemoryForData() const REQUIRES(Locks::jit_lock_) {
138 return used_memory_for_data_;
139 }
140
141 private:
142 template <typename T>
143 T* TranslateAddress(T* src_ptr, const MemMap& src, const MemMap& dst) {
144 if (!HasDualCodeMapping()) {
145 return src_ptr;
146 }
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100147 CHECK(src.HasAddress(src_ptr)) << reinterpret_cast<const void*>(src_ptr);
148 const uint8_t* const raw_src_ptr = reinterpret_cast<const uint8_t*>(src_ptr);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100149 return reinterpret_cast<T*>(raw_src_ptr - src.Begin() + dst.Begin());
150 }
151
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100152 const MemMap* GetUpdatableCodeMapping() const {
153 if (HasDualCodeMapping()) {
154 return &non_exec_pages_;
155 } else if (HasCodeMapping()) {
156 return &exec_pages_;
157 } else {
158 return nullptr;
159 }
160 }
161
162 template <typename T> T* GetExecutableAddress(T* src_ptr) {
163 return TranslateAddress(src_ptr, non_exec_pages_, exec_pages_);
164 }
165
166 template <typename T> T* GetNonExecutableAddress(T* src_ptr) {
167 return TranslateAddress(src_ptr, exec_pages_, non_exec_pages_);
168 }
169
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100170 static int CreateZygoteMemory(size_t capacity, std::string* error_msg);
171 static bool ProtectZygoteMemory(int fd, std::string* error_msg);
172
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100173 // The initial capacity in bytes this code region starts with.
174 size_t initial_capacity_ GUARDED_BY(Locks::jit_lock_);
175
176 // The maximum capacity in bytes this region can go to.
177 size_t max_capacity_ GUARDED_BY(Locks::jit_lock_);
178
179 // The current capacity in bytes of the region.
180 size_t current_capacity_ GUARDED_BY(Locks::jit_lock_);
181
182 // The current footprint in bytes of the data portion of the region.
183 size_t data_end_ GUARDED_BY(Locks::jit_lock_);
184
185 // The current footprint in bytes of the code portion of the region.
186 size_t exec_end_ GUARDED_BY(Locks::jit_lock_);
187
188 // The size in bytes of used memory for the code portion of the region.
189 size_t used_memory_for_code_ GUARDED_BY(Locks::jit_lock_);
190
191 // The size in bytes of used memory for the data portion of the region.
192 size_t used_memory_for_data_ GUARDED_BY(Locks::jit_lock_);
193
194 // Mem map which holds data (stack maps and profiling info).
195 MemMap data_pages_;
196
197 // Mem map which holds code and has executable permission.
198 MemMap exec_pages_;
199
200 // Mem map which holds code with non executable permission. Only valid for dual view JIT when
201 // this is the non-executable view of code used to write updates.
202 MemMap non_exec_pages_;
203
204 // The opaque mspace for allocating data.
205 void* data_mspace_ GUARDED_BY(Locks::jit_lock_);
206
207 // The opaque mspace for allocating code.
208 void* exec_mspace_ GUARDED_BY(Locks::jit_lock_);
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100209
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100210 friend class ScopedCodeCacheWrite; // For GetUpdatableCodeMapping
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100211 friend class TestZygoteMemory;
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100212};
213
214} // namespace jit
215} // namespace art
216
217#endif // ART_RUNTIME_JIT_JIT_MEMORY_REGION_H_