Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1 | /* |
| 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 Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 22 | #include "arch/instruction_set.h" |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 23 | #include "base/globals.h" |
| 24 | #include "base/locks.h" |
| 25 | #include "base/mem_map.h" |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 26 | #include "gc_root-inl.h" |
| 27 | #include "handle.h" |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 28 | |
| 29 | namespace art { |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 30 | |
| 31 | namespace mirror { |
| 32 | class Object; |
| 33 | } |
| 34 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 35 | namespace jit { |
| 36 | |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 37 | class TestZygoteMemory; |
| 38 | |
Orion Hodson | 521ff98 | 2019-06-18 13:56:28 +0100 | [diff] [blame] | 39 | // Number of bytes represented by a bit in the CodeCacheBitmap. Value is reasonable for all |
| 40 | // architectures. |
| 41 | static constexpr int kJitCodeAccountingBytes = 16; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 42 | |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 43 | // Helper to get the size required for emitting `number_of_roots` in the |
| 44 | // data portion of a JIT memory region. |
| 45 | uint32_t inline ComputeRootTableSize(uint32_t number_of_roots) { |
| 46 | return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>); |
| 47 | } |
| 48 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 49 | // Represents a memory region for the JIT, where code and data are stored. This class |
| 50 | // provides allocation and deallocation primitives. |
| 51 | class JitMemoryRegion { |
| 52 | public: |
| 53 | JitMemoryRegion() |
Nicolas Geoffray | 9c54e18 | 2019-06-18 10:42:52 +0100 | [diff] [blame] | 54 | : 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 Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame^] | 61 | data_pages_(), |
| 62 | writable_data_pages_(), |
Nicolas Geoffray | 9c54e18 | 2019-06-18 10:42:52 +0100 | [diff] [blame] | 63 | exec_pages_(), |
| 64 | non_exec_pages_(), |
| 65 | data_mspace_(nullptr), |
| 66 | exec_mspace_(nullptr) {} |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 67 | |
Nicolas Geoffray | 9c54e18 | 2019-06-18 10:42:52 +0100 | [diff] [blame] | 68 | 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 Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 73 | REQUIRES(Locks::jit_lock_); |
| 74 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 75 | // 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 Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 81 | |
| 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 Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 90 | uint8_t* AllocateData(size_t data_size) REQUIRES(Locks::jit_lock_); |
| 91 | void FreeData(uint8_t* data) REQUIRES(Locks::jit_lock_); |
| 92 | |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 93 | // Emit roots and stack map into the memory pointed by `roots_data`. |
Orion Hodson | aeb0223 | 2019-06-25 14:18:18 +0100 | [diff] [blame] | 94 | bool CommitData(uint8_t* roots_data, |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 95 | 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 Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 101 | bool IsValid() const NO_THREAD_SAFETY_ANALYSIS { |
| 102 | return exec_mspace_ != nullptr || data_mspace_ != nullptr; |
| 103 | } |
| 104 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 105 | bool HasDualCodeMapping() const { |
| 106 | return non_exec_pages_.IsValid(); |
| 107 | } |
| 108 | |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame^] | 109 | bool HasDualDataMapping() const { |
| 110 | return writable_data_pages_.IsValid(); |
| 111 | } |
| 112 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 113 | bool HasCodeMapping() const { |
| 114 | return exec_pages_.IsValid(); |
| 115 | } |
| 116 | |
| 117 | bool IsInDataSpace(const void* ptr) const { |
| 118 | return data_pages_.HasAddress(ptr); |
| 119 | } |
| 120 | |
| 121 | bool IsInExecSpace(const void* ptr) const { |
| 122 | return exec_pages_.HasAddress(ptr); |
| 123 | } |
| 124 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 125 | const MemMap* GetExecPages() const { |
| 126 | return &exec_pages_; |
| 127 | } |
| 128 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 129 | void* MoreCore(const void* mspace, intptr_t increment); |
| 130 | |
| 131 | bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS { |
| 132 | return mspace == data_mspace_ || mspace == exec_mspace_; |
| 133 | } |
| 134 | |
| 135 | size_t GetCurrentCapacity() const REQUIRES(Locks::jit_lock_) { |
| 136 | return current_capacity_; |
| 137 | } |
| 138 | |
| 139 | size_t GetMaxCapacity() const REQUIRES(Locks::jit_lock_) { |
| 140 | return max_capacity_; |
| 141 | } |
| 142 | |
| 143 | size_t GetUsedMemoryForCode() const REQUIRES(Locks::jit_lock_) { |
| 144 | return used_memory_for_code_; |
| 145 | } |
| 146 | |
| 147 | size_t GetUsedMemoryForData() const REQUIRES(Locks::jit_lock_) { |
| 148 | return used_memory_for_data_; |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | template <typename T> |
| 153 | T* TranslateAddress(T* src_ptr, const MemMap& src, const MemMap& dst) { |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 154 | CHECK(src.HasAddress(src_ptr)) << reinterpret_cast<const void*>(src_ptr); |
| 155 | const uint8_t* const raw_src_ptr = reinterpret_cast<const uint8_t*>(src_ptr); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 156 | return reinterpret_cast<T*>(raw_src_ptr - src.Begin() + dst.Begin()); |
| 157 | } |
| 158 | |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 159 | const MemMap* GetUpdatableCodeMapping() const { |
| 160 | if (HasDualCodeMapping()) { |
| 161 | return &non_exec_pages_; |
| 162 | } else if (HasCodeMapping()) { |
| 163 | return &exec_pages_; |
| 164 | } else { |
| 165 | return nullptr; |
| 166 | } |
| 167 | } |
| 168 | |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame^] | 169 | const MemMap* GetWritableDataMapping() const { |
| 170 | if (HasDualDataMapping()) { |
| 171 | return &writable_data_pages_; |
| 172 | } else { |
| 173 | return &data_pages_; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | template <typename T> T* GetNonWritableDataAddress(T* src_ptr) { |
| 178 | if (!HasDualDataMapping()) { |
| 179 | return src_ptr; |
| 180 | } |
| 181 | return TranslateAddress(src_ptr, writable_data_pages_, data_pages_); |
| 182 | } |
| 183 | |
| 184 | template <typename T> T* GetWritableDataAddress(T* src_ptr) { |
| 185 | if (!HasDualDataMapping()) { |
| 186 | return src_ptr; |
| 187 | } |
| 188 | return TranslateAddress(src_ptr, data_pages_, writable_data_pages_); |
| 189 | } |
| 190 | |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 191 | template <typename T> T* GetExecutableAddress(T* src_ptr) { |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame^] | 192 | if (!HasDualCodeMapping()) { |
| 193 | return src_ptr; |
| 194 | } |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 195 | return TranslateAddress(src_ptr, non_exec_pages_, exec_pages_); |
| 196 | } |
| 197 | |
| 198 | template <typename T> T* GetNonExecutableAddress(T* src_ptr) { |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame^] | 199 | if (!HasDualCodeMapping()) { |
| 200 | return src_ptr; |
| 201 | } |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 202 | return TranslateAddress(src_ptr, exec_pages_, non_exec_pages_); |
| 203 | } |
| 204 | |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 205 | static int CreateZygoteMemory(size_t capacity, std::string* error_msg); |
| 206 | static bool ProtectZygoteMemory(int fd, std::string* error_msg); |
| 207 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 208 | // The initial capacity in bytes this code region starts with. |
| 209 | size_t initial_capacity_ GUARDED_BY(Locks::jit_lock_); |
| 210 | |
| 211 | // The maximum capacity in bytes this region can go to. |
| 212 | size_t max_capacity_ GUARDED_BY(Locks::jit_lock_); |
| 213 | |
| 214 | // The current capacity in bytes of the region. |
| 215 | size_t current_capacity_ GUARDED_BY(Locks::jit_lock_); |
| 216 | |
| 217 | // The current footprint in bytes of the data portion of the region. |
| 218 | size_t data_end_ GUARDED_BY(Locks::jit_lock_); |
| 219 | |
| 220 | // The current footprint in bytes of the code portion of the region. |
| 221 | size_t exec_end_ GUARDED_BY(Locks::jit_lock_); |
| 222 | |
| 223 | // The size in bytes of used memory for the code portion of the region. |
| 224 | size_t used_memory_for_code_ GUARDED_BY(Locks::jit_lock_); |
| 225 | |
| 226 | // The size in bytes of used memory for the data portion of the region. |
| 227 | size_t used_memory_for_data_ GUARDED_BY(Locks::jit_lock_); |
| 228 | |
| 229 | // Mem map which holds data (stack maps and profiling info). |
| 230 | MemMap data_pages_; |
| 231 | |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame^] | 232 | // Mem map which holds data with writable permission. Only valid for dual view |
| 233 | // JIT when this is the writable view and data_pages_ is the readable view. |
| 234 | MemMap writable_data_pages_; |
| 235 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 236 | // Mem map which holds code and has executable permission. |
| 237 | MemMap exec_pages_; |
| 238 | |
| 239 | // Mem map which holds code with non executable permission. Only valid for dual view JIT when |
| 240 | // this is the non-executable view of code used to write updates. |
| 241 | MemMap non_exec_pages_; |
| 242 | |
| 243 | // The opaque mspace for allocating data. |
| 244 | void* data_mspace_ GUARDED_BY(Locks::jit_lock_); |
| 245 | |
| 246 | // The opaque mspace for allocating code. |
| 247 | void* exec_mspace_ GUARDED_BY(Locks::jit_lock_); |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 248 | |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 249 | friend class ScopedCodeCacheWrite; // For GetUpdatableCodeMapping |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 250 | friend class TestZygoteMemory; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | } // namespace jit |
| 254 | } // namespace art |
| 255 | |
| 256 | #endif // ART_RUNTIME_JIT_JIT_MEMORY_REGION_H_ |