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 | |
| 22 | #include "base/globals.h" |
| 23 | #include "base/locks.h" |
| 24 | #include "base/mem_map.h" |
| 25 | |
| 26 | namespace art { |
| 27 | namespace jit { |
| 28 | |
| 29 | // Alignment in bytes that will suit all architectures for JIT code cache allocations. The |
| 30 | // allocated block is used for method header followed by generated code. Allocations should be |
| 31 | // aligned to avoid sharing cache lines between different allocations. The alignment should be |
| 32 | // determined from the hardware, but this isn't readily exposed in userland plus some hardware |
| 33 | // misreports. |
| 34 | static constexpr int kJitCodeAlignment = 64; |
| 35 | |
| 36 | // Represents a memory region for the JIT, where code and data are stored. This class |
| 37 | // provides allocation and deallocation primitives. |
| 38 | class JitMemoryRegion { |
| 39 | public: |
| 40 | JitMemoryRegion() |
| 41 | : used_memory_for_code_(0), |
| 42 | used_memory_for_data_(0) {} |
| 43 | |
| 44 | void InitializeState(size_t initial_capacity, size_t max_capacity) |
| 45 | REQUIRES(Locks::jit_lock_); |
| 46 | |
| 47 | bool InitializeMappings(bool rwx_memory_allowed, bool is_zygote, std::string* error_msg) |
| 48 | REQUIRES(Locks::jit_lock_); |
| 49 | |
| 50 | void InitializeSpaces() REQUIRES(Locks::jit_lock_); |
| 51 | |
| 52 | // Try to increase the current capacity of the code cache. Return whether we |
| 53 | // succeeded at doing so. |
| 54 | bool IncreaseCodeCacheCapacity() REQUIRES(Locks::jit_lock_); |
| 55 | |
| 56 | // Set the footprint limit of the code cache. |
| 57 | void SetFootprintLimit(size_t new_footprint) REQUIRES(Locks::jit_lock_); |
| 58 | uint8_t* AllocateCode(size_t code_size) REQUIRES(Locks::jit_lock_); |
| 59 | void FreeCode(uint8_t* code) REQUIRES(Locks::jit_lock_); |
| 60 | uint8_t* AllocateData(size_t data_size) REQUIRES(Locks::jit_lock_); |
| 61 | void FreeData(uint8_t* data) REQUIRES(Locks::jit_lock_); |
| 62 | |
| 63 | bool HasDualCodeMapping() const { |
| 64 | return non_exec_pages_.IsValid(); |
| 65 | } |
| 66 | |
| 67 | bool HasCodeMapping() const { |
| 68 | return exec_pages_.IsValid(); |
| 69 | } |
| 70 | |
| 71 | bool IsInDataSpace(const void* ptr) const { |
| 72 | return data_pages_.HasAddress(ptr); |
| 73 | } |
| 74 | |
| 75 | bool IsInExecSpace(const void* ptr) const { |
| 76 | return exec_pages_.HasAddress(ptr); |
| 77 | } |
| 78 | |
| 79 | const MemMap* GetUpdatableCodeMapping() const { |
| 80 | if (HasDualCodeMapping()) { |
| 81 | return &non_exec_pages_; |
| 82 | } else if (HasCodeMapping()) { |
| 83 | return &exec_pages_; |
| 84 | } else { |
| 85 | return nullptr; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | const MemMap* GetExecPages() const { |
| 90 | return &exec_pages_; |
| 91 | } |
| 92 | |
| 93 | template <typename T> T* GetExecutableAddress(T* src_ptr) { |
| 94 | return TranslateAddress(src_ptr, non_exec_pages_, exec_pages_); |
| 95 | } |
| 96 | |
| 97 | template <typename T> T* GetNonExecutableAddress(T* src_ptr) { |
| 98 | return TranslateAddress(src_ptr, exec_pages_, non_exec_pages_); |
| 99 | } |
| 100 | |
| 101 | void* MoreCore(const void* mspace, intptr_t increment); |
| 102 | |
| 103 | bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS { |
| 104 | return mspace == data_mspace_ || mspace == exec_mspace_; |
| 105 | } |
| 106 | |
| 107 | size_t GetCurrentCapacity() const REQUIRES(Locks::jit_lock_) { |
| 108 | return current_capacity_; |
| 109 | } |
| 110 | |
| 111 | size_t GetMaxCapacity() const REQUIRES(Locks::jit_lock_) { |
| 112 | return max_capacity_; |
| 113 | } |
| 114 | |
| 115 | size_t GetUsedMemoryForCode() const REQUIRES(Locks::jit_lock_) { |
| 116 | return used_memory_for_code_; |
| 117 | } |
| 118 | |
| 119 | size_t GetUsedMemoryForData() const REQUIRES(Locks::jit_lock_) { |
| 120 | return used_memory_for_data_; |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | template <typename T> |
| 125 | T* TranslateAddress(T* src_ptr, const MemMap& src, const MemMap& dst) { |
| 126 | if (!HasDualCodeMapping()) { |
| 127 | return src_ptr; |
| 128 | } |
| 129 | CHECK(src.HasAddress(src_ptr)); |
| 130 | uint8_t* const raw_src_ptr = reinterpret_cast<uint8_t*>(src_ptr); |
| 131 | return reinterpret_cast<T*>(raw_src_ptr - src.Begin() + dst.Begin()); |
| 132 | } |
| 133 | |
| 134 | // The initial capacity in bytes this code region starts with. |
| 135 | size_t initial_capacity_ GUARDED_BY(Locks::jit_lock_); |
| 136 | |
| 137 | // The maximum capacity in bytes this region can go to. |
| 138 | size_t max_capacity_ GUARDED_BY(Locks::jit_lock_); |
| 139 | |
| 140 | // The current capacity in bytes of the region. |
| 141 | size_t current_capacity_ GUARDED_BY(Locks::jit_lock_); |
| 142 | |
| 143 | // The current footprint in bytes of the data portion of the region. |
| 144 | size_t data_end_ GUARDED_BY(Locks::jit_lock_); |
| 145 | |
| 146 | // The current footprint in bytes of the code portion of the region. |
| 147 | size_t exec_end_ GUARDED_BY(Locks::jit_lock_); |
| 148 | |
| 149 | // The size in bytes of used memory for the code portion of the region. |
| 150 | size_t used_memory_for_code_ GUARDED_BY(Locks::jit_lock_); |
| 151 | |
| 152 | // The size in bytes of used memory for the data portion of the region. |
| 153 | size_t used_memory_for_data_ GUARDED_BY(Locks::jit_lock_); |
| 154 | |
| 155 | // Mem map which holds data (stack maps and profiling info). |
| 156 | MemMap data_pages_; |
| 157 | |
| 158 | // Mem map which holds code and has executable permission. |
| 159 | MemMap exec_pages_; |
| 160 | |
| 161 | // Mem map which holds code with non executable permission. Only valid for dual view JIT when |
| 162 | // this is the non-executable view of code used to write updates. |
| 163 | MemMap non_exec_pages_; |
| 164 | |
| 165 | // The opaque mspace for allocating data. |
| 166 | void* data_mspace_ GUARDED_BY(Locks::jit_lock_); |
| 167 | |
| 168 | // The opaque mspace for allocating code. |
| 169 | void* exec_mspace_ GUARDED_BY(Locks::jit_lock_); |
| 170 | }; |
| 171 | |
| 172 | } // namespace jit |
| 173 | } // namespace art |
| 174 | |
| 175 | #endif // ART_RUNTIME_JIT_JIT_MEMORY_REGION_H_ |