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 | #include "jit_memory_region.h" |
| 18 | |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 22 | #include <android-base/unique_fd.h> |
| 23 | #include "base/bit_utils.h" // For RoundDown, RoundUp |
| 24 | #include "base/globals.h" |
| 25 | #include "base/logging.h" // For VLOG. |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 26 | #include "base/membarrier.h" |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 27 | #include "base/memfd.h" |
| 28 | #include "base/systrace.h" |
| 29 | #include "gc/allocator/dlmalloc.h" |
| 30 | #include "jit/jit_scoped_code_cache_write.h" |
| 31 | #include "oat_quick_method_header.h" |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 32 | #include "palette/palette.h" |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 33 | |
| 34 | using android::base::unique_fd; |
| 35 | |
| 36 | namespace art { |
| 37 | namespace jit { |
| 38 | |
| 39 | // Data cache will be half of the capacity |
| 40 | // Code cache will be the other half of the capacity. |
| 41 | // TODO: Make this variable? |
| 42 | static constexpr size_t kCodeAndDataCapacityDivider = 2; |
| 43 | |
Nicolas Geoffray | 9c54e18 | 2019-06-18 10:42:52 +0100 | [diff] [blame] | 44 | bool JitMemoryRegion::Initialize(size_t initial_capacity, |
| 45 | size_t max_capacity, |
| 46 | bool rwx_memory_allowed, |
| 47 | bool is_zygote, |
| 48 | std::string* error_msg) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 49 | ScopedTrace trace(__PRETTY_FUNCTION__); |
| 50 | |
Nicolas Geoffray | 9c54e18 | 2019-06-18 10:42:52 +0100 | [diff] [blame] | 51 | CHECK_GE(max_capacity, initial_capacity); |
| 52 | CHECK(max_capacity <= 1 * GB) << "The max supported size for JIT code cache is 1GB"; |
| 53 | // Align both capacities to page size, as that's the unit mspaces use. |
| 54 | initial_capacity_ = RoundDown(initial_capacity, 2 * kPageSize); |
| 55 | max_capacity_ = RoundDown(max_capacity, 2 * kPageSize); |
| 56 | current_capacity_ = initial_capacity, |
| 57 | data_end_ = initial_capacity / kCodeAndDataCapacityDivider; |
| 58 | exec_end_ = initial_capacity - data_end_; |
| 59 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 60 | const size_t capacity = max_capacity_; |
| 61 | const size_t data_capacity = capacity / kCodeAndDataCapacityDivider; |
| 62 | const size_t exec_capacity = capacity - data_capacity; |
| 63 | |
| 64 | // File descriptor enabling dual-view mapping of code section. |
| 65 | unique_fd mem_fd; |
| 66 | |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 67 | if (is_zygote) { |
| 68 | // Because we are not going to GC code generated by the zygote, just use all available. |
| 69 | current_capacity_ = max_capacity; |
| 70 | mem_fd = unique_fd(CreateZygoteMemory(capacity, error_msg)); |
| 71 | if (mem_fd.get() < 0) { |
| 72 | return false; |
| 73 | } |
| 74 | } else { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 75 | // Bionic supports memfd_create, but the call may fail on older kernels. |
Nicolas Geoffray | 00b8d45 | 2019-10-02 15:34:45 +0100 | [diff] [blame] | 76 | mem_fd = unique_fd(art::memfd_create("jit-cache", /* flags= */ 0)); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 77 | if (mem_fd.get() < 0) { |
| 78 | std::ostringstream oss; |
| 79 | oss << "Failed to initialize dual view JIT. memfd_create() error: " << strerror(errno); |
| 80 | if (!rwx_memory_allowed) { |
| 81 | // Without using RWX page permissions, the JIT can not fallback to single mapping as it |
| 82 | // requires tranitioning the code pages to RWX for updates. |
| 83 | *error_msg = oss.str(); |
| 84 | return false; |
| 85 | } |
| 86 | VLOG(jit) << oss.str(); |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 87 | } else if (ftruncate(mem_fd, capacity) != 0) { |
| 88 | std::ostringstream oss; |
| 89 | oss << "Failed to initialize memory file: " << strerror(errno); |
| 90 | *error_msg = oss.str(); |
| 91 | return false; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 95 | std::string data_cache_name = is_zygote ? "zygote-data-code-cache" : "data-code-cache"; |
| 96 | std::string exec_cache_name = is_zygote ? "zygote-jit-code-cache" : "jit-code-cache"; |
| 97 | |
| 98 | std::string error_str; |
Nicolas Geoffray | 55dace0 | 2021-10-01 08:27:53 +0000 | [diff] [blame] | 99 | // Map name specific for android_os_Debug.cpp accounting. |
| 100 | // Map in low 4gb to simplify accessing root tables for x86_64. |
| 101 | // We could do PC-relative addressing to avoid this problem, but that |
| 102 | // would require reserving code and data area before submitting, which |
| 103 | // means more windows for the code memory to be RWX. |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 104 | int base_flags; |
Nicolas Geoffray | 55dace0 | 2021-10-01 08:27:53 +0000 | [diff] [blame] | 105 | MemMap data_pages; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 106 | if (mem_fd.get() >= 0) { |
| 107 | // Dual view of JIT code cache case. Create an initial mapping of data pages large enough |
| 108 | // for data and non-writable view of JIT code pages. We use the memory file descriptor to |
| 109 | // enable dual mapping - we'll create a second mapping using the descriptor below. The |
| 110 | // mappings will look like: |
| 111 | // |
| 112 | // VA PA |
| 113 | // |
| 114 | // +---------------+ |
| 115 | // | non exec code |\ |
| 116 | // +---------------+ \ |
David Srbecky | f0e90ba | 2020-01-17 15:38:24 +0000 | [diff] [blame] | 117 | // | writable data |\ \ |
| 118 | // +---------------+ \ \ |
| 119 | // : :\ \ \ |
| 120 | // +---------------+.\.\.+---------------+ |
| 121 | // | exec code | \ \| code | |
| 122 | // +---------------+...\.+---------------+ |
| 123 | // | readonly data | \| data | |
| 124 | // +---------------+.....+---------------+ |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 125 | // |
| 126 | // In this configuration code updates are written to the non-executable view of the code |
| 127 | // cache, and the executable view of the code cache has fixed RX memory protections. |
| 128 | // |
| 129 | // This memory needs to be mapped shared as the code portions will have two mappings. |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame] | 130 | // |
| 131 | // Additionally, the zyzote will create a dual view of the data portion of |
| 132 | // the cache. This mapping will be read-only, whereas the second mapping |
| 133 | // will be writable. |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 134 | base_flags = MAP_SHARED; |
| 135 | data_pages = MemMap::MapFile( |
| 136 | data_capacity + exec_capacity, |
David Srbecky | f0e90ba | 2020-01-17 15:38:24 +0000 | [diff] [blame] | 137 | kProtR, |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 138 | base_flags, |
| 139 | mem_fd, |
| 140 | /* start= */ 0, |
| 141 | /* low_4gb= */ true, |
| 142 | data_cache_name.c_str(), |
| 143 | &error_str); |
| 144 | } else { |
| 145 | // Single view of JIT code cache case. Create an initial mapping of data pages large enough |
| 146 | // for data and JIT code pages. The mappings will look like: |
| 147 | // |
| 148 | // VA PA |
| 149 | // |
| 150 | // +---------------+...+---------------+ |
| 151 | // | exec code | | code | |
| 152 | // +---------------+...+---------------+ |
| 153 | // | data | | data | |
| 154 | // +---------------+...+---------------+ |
| 155 | // |
| 156 | // In this configuration code updates are written to the executable view of the code cache, |
| 157 | // and the executable view of the code cache transitions RX to RWX for the update and then |
| 158 | // back to RX after the update. |
| 159 | base_flags = MAP_PRIVATE | MAP_ANON; |
| 160 | data_pages = MemMap::MapAnonymous( |
| 161 | data_cache_name.c_str(), |
| 162 | data_capacity + exec_capacity, |
| 163 | kProtRW, |
| 164 | /* low_4gb= */ true, |
| 165 | &error_str); |
| 166 | } |
| 167 | |
| 168 | if (!data_pages.IsValid()) { |
| 169 | std::ostringstream oss; |
| 170 | oss << "Failed to create read write cache: " << error_str << " size=" << capacity; |
| 171 | *error_msg = oss.str(); |
| 172 | return false; |
| 173 | } |
| 174 | |
Nicolas Geoffray | 55dace0 | 2021-10-01 08:27:53 +0000 | [diff] [blame] | 175 | MemMap exec_pages; |
| 176 | MemMap non_exec_pages; |
| 177 | MemMap writable_data_pages; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 178 | if (exec_capacity > 0) { |
| 179 | uint8_t* const divider = data_pages.Begin() + data_capacity; |
| 180 | // Set initial permission for executable view to catch any SELinux permission problems early |
| 181 | // (for processes that cannot map WX pages). Otherwise, this region does not need to be |
| 182 | // executable as there is no code in the cache yet. |
| 183 | exec_pages = data_pages.RemapAtEnd(divider, |
| 184 | exec_cache_name.c_str(), |
| 185 | kProtRX, |
| 186 | base_flags | MAP_FIXED, |
| 187 | mem_fd.get(), |
| 188 | (mem_fd.get() >= 0) ? data_capacity : 0, |
| 189 | &error_str); |
| 190 | if (!exec_pages.IsValid()) { |
| 191 | std::ostringstream oss; |
| 192 | oss << "Failed to create read execute code cache: " << error_str << " size=" << capacity; |
| 193 | *error_msg = oss.str(); |
| 194 | return false; |
| 195 | } |
Nicolas Geoffray | 55dace0 | 2021-10-01 08:27:53 +0000 | [diff] [blame] | 196 | |
| 197 | if (mem_fd.get() >= 0) { |
| 198 | // For dual view, create the secondary view of code memory used for updating code. This view |
| 199 | // is never executable. |
| 200 | std::string name = exec_cache_name + "-rw"; |
| 201 | non_exec_pages = MemMap::MapFile(exec_capacity, |
| 202 | kIsDebugBuild ? kProtR : kProtRW, |
| 203 | base_flags, |
| 204 | mem_fd, |
| 205 | /* start= */ data_capacity, |
| 206 | /* low_4GB= */ false, |
| 207 | name.c_str(), |
| 208 | &error_str); |
| 209 | if (!non_exec_pages.IsValid()) { |
| 210 | static const char* kFailedNxView = "Failed to map non-executable view of JIT code cache"; |
| 211 | if (rwx_memory_allowed) { |
| 212 | // Log and continue as single view JIT (requires RWX memory). |
| 213 | VLOG(jit) << kFailedNxView; |
| 214 | } else { |
| 215 | *error_msg = kFailedNxView; |
| 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | // Create a dual view of the data cache. |
| 220 | name = data_cache_name + "-rw"; |
| 221 | writable_data_pages = MemMap::MapFile(data_capacity, |
| 222 | kProtRW, |
| 223 | base_flags, |
| 224 | mem_fd, |
| 225 | /* start= */ 0, |
| 226 | /* low_4GB= */ false, |
| 227 | name.c_str(), |
| 228 | &error_str); |
| 229 | if (!writable_data_pages.IsValid()) { |
| 230 | std::ostringstream oss; |
| 231 | oss << "Failed to create dual data view: " << error_str; |
| 232 | *error_msg = oss.str(); |
| 233 | return false; |
| 234 | } |
| 235 | if (writable_data_pages.MadviseDontFork() != 0) { |
| 236 | *error_msg = "Failed to madvise dont fork the writable data view"; |
| 237 | return false; |
| 238 | } |
| 239 | if (non_exec_pages.MadviseDontFork() != 0) { |
| 240 | *error_msg = "Failed to madvise dont fork the writable code view"; |
| 241 | return false; |
| 242 | } |
| 243 | // Now that we have created the writable and executable mappings, prevent creating any new |
| 244 | // ones. |
| 245 | if (is_zygote && !ProtectZygoteMemory(mem_fd.get(), error_msg)) { |
| 246 | return false; |
| 247 | } |
| 248 | } |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 249 | } else { |
| 250 | // Profiling only. No memory for code required. |
| 251 | } |
| 252 | |
| 253 | data_pages_ = std::move(data_pages); |
| 254 | exec_pages_ = std::move(exec_pages); |
| 255 | non_exec_pages_ = std::move(non_exec_pages); |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame] | 256 | writable_data_pages_ = std::move(writable_data_pages); |
| 257 | |
| 258 | VLOG(jit) << "Created JitMemoryRegion" |
| 259 | << ": data_pages=" << reinterpret_cast<void*>(data_pages_.Begin()) |
| 260 | << ", exec_pages=" << reinterpret_cast<void*>(exec_pages_.Begin()) |
| 261 | << ", non_exec_pages=" << reinterpret_cast<void*>(non_exec_pages_.Begin()) |
| 262 | << ", writable_data_pages=" << reinterpret_cast<void*>(writable_data_pages_.Begin()); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 263 | |
Nicolas Geoffray | 9c54e18 | 2019-06-18 10:42:52 +0100 | [diff] [blame] | 264 | // Now that the pages are initialized, initialize the spaces. |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 265 | |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame] | 266 | // Initialize the data heap. |
| 267 | data_mspace_ = create_mspace_with_base( |
| 268 | HasDualDataMapping() ? writable_data_pages_.Begin() : data_pages_.Begin(), |
| 269 | data_end_, |
| 270 | /* locked= */ false); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 271 | CHECK(data_mspace_ != nullptr) << "create_mspace_with_base (data) failed"; |
| 272 | |
David Srbecky | ac8f936 | 2019-08-13 16:59:25 +0100 | [diff] [blame] | 273 | // Allow mspace to use the full data capacity. |
| 274 | // It will still only use as litle memory as possible and ask for MoreCore as needed. |
| 275 | CHECK(IsAlignedParam(data_capacity, kPageSize)); |
| 276 | mspace_set_footprint_limit(data_mspace_, data_capacity); |
| 277 | |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame] | 278 | // Initialize the code heap. |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 279 | MemMap* code_heap = nullptr; |
| 280 | if (non_exec_pages_.IsValid()) { |
| 281 | code_heap = &non_exec_pages_; |
| 282 | } else if (exec_pages_.IsValid()) { |
| 283 | code_heap = &exec_pages_; |
| 284 | } |
| 285 | if (code_heap != nullptr) { |
| 286 | // Make all pages reserved for the code heap writable. The mspace allocator, that manages the |
| 287 | // heap, will take and initialize pages in create_mspace_with_base(). |
Nicolas Geoffray | 670ff88 | 2020-11-06 18:00:39 +0000 | [diff] [blame] | 288 | { |
| 289 | ScopedCodeCacheWrite scc(*this); |
| 290 | exec_mspace_ = create_mspace_with_base(code_heap->Begin(), exec_end_, false /*locked*/); |
| 291 | } |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 292 | CHECK(exec_mspace_ != nullptr) << "create_mspace_with_base (exec) failed"; |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 293 | SetFootprintLimit(current_capacity_); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 294 | } else { |
| 295 | exec_mspace_ = nullptr; |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 296 | SetFootprintLimit(current_capacity_); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 297 | } |
Nicolas Geoffray | 9c54e18 | 2019-06-18 10:42:52 +0100 | [diff] [blame] | 298 | return true; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void JitMemoryRegion::SetFootprintLimit(size_t new_footprint) { |
| 302 | size_t data_space_footprint = new_footprint / kCodeAndDataCapacityDivider; |
| 303 | DCHECK(IsAlignedParam(data_space_footprint, kPageSize)); |
| 304 | DCHECK_EQ(data_space_footprint * kCodeAndDataCapacityDivider, new_footprint); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 305 | if (HasCodeMapping()) { |
| 306 | ScopedCodeCacheWrite scc(*this); |
| 307 | mspace_set_footprint_limit(exec_mspace_, new_footprint - data_space_footprint); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | bool JitMemoryRegion::IncreaseCodeCacheCapacity() { |
| 312 | if (current_capacity_ == max_capacity_) { |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | // Double the capacity if we're below 1MB, or increase it by 1MB if |
| 317 | // we're above. |
| 318 | if (current_capacity_ < 1 * MB) { |
| 319 | current_capacity_ *= 2; |
| 320 | } else { |
| 321 | current_capacity_ += 1 * MB; |
| 322 | } |
| 323 | if (current_capacity_ > max_capacity_) { |
| 324 | current_capacity_ = max_capacity_; |
| 325 | } |
| 326 | |
| 327 | VLOG(jit) << "Increasing code cache capacity to " << PrettySize(current_capacity_); |
| 328 | |
| 329 | SetFootprintLimit(current_capacity_); |
| 330 | |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | // NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock |
| 335 | // is already held. |
| 336 | void* JitMemoryRegion::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS { |
| 337 | if (mspace == exec_mspace_) { |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame] | 338 | CHECK(exec_mspace_ != nullptr); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 339 | const MemMap* const code_pages = GetUpdatableCodeMapping(); |
| 340 | void* result = code_pages->Begin() + exec_end_; |
| 341 | exec_end_ += increment; |
| 342 | return result; |
| 343 | } else { |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame] | 344 | CHECK_EQ(data_mspace_, mspace); |
| 345 | const MemMap* const writable_data_pages = GetWritableDataMapping(); |
| 346 | void* result = writable_data_pages->Begin() + data_end_; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 347 | data_end_ += increment; |
| 348 | return result; |
| 349 | } |
| 350 | } |
| 351 | |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 352 | const uint8_t* JitMemoryRegion::CommitCode(ArrayRef<const uint8_t> reserved_code, |
| 353 | ArrayRef<const uint8_t> code, |
| 354 | const uint8_t* stack_map, |
| 355 | bool has_should_deoptimize_flag) { |
| 356 | DCHECK(IsInExecSpace(reserved_code.data())); |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 357 | ScopedCodeCacheWrite scc(*this); |
| 358 | |
Orion Hodson | e764f38 | 2019-06-27 12:56:48 +0100 | [diff] [blame] | 359 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 360 | size_t header_size = OatQuickMethodHeader::InstructionAlignedSize(); |
| 361 | size_t total_size = header_size + code.size(); |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 362 | |
| 363 | // Each allocation should be on its own set of cache lines. |
| 364 | // `total_size` covers the OatQuickMethodHeader, the JIT generated machine code, |
| 365 | // and any alignment padding. |
| 366 | DCHECK_GT(total_size, header_size); |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 367 | DCHECK_LE(total_size, reserved_code.size()); |
| 368 | uint8_t* x_memory = const_cast<uint8_t*>(reserved_code.data()); |
| 369 | uint8_t* w_memory = const_cast<uint8_t*>(GetNonExecutableAddress(x_memory)); |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 370 | // Ensure the header ends up at expected instruction alignment. |
| 371 | DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(w_memory + header_size), alignment); |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 372 | const uint8_t* result = x_memory + header_size; |
| 373 | |
| 374 | // Write the code. |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 375 | std::copy(code.begin(), code.end(), w_memory + header_size); |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 376 | |
| 377 | // Write the header. |
| 378 | OatQuickMethodHeader* method_header = |
| 379 | OatQuickMethodHeader::FromCodePointer(w_memory + header_size); |
David Srbecky | 113d6ea | 2021-03-02 22:49:46 +0000 | [diff] [blame] | 380 | new (method_header) OatQuickMethodHeader((stack_map != nullptr) ? result - stack_map : 0u); |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 381 | if (has_should_deoptimize_flag) { |
| 382 | method_header->SetHasShouldDeoptimizeFlag(); |
| 383 | } |
| 384 | |
| 385 | // Both instruction and data caches need flushing to the point of unification where both share |
| 386 | // a common view of memory. Flushing the data cache ensures the dirty cachelines from the |
| 387 | // newly added code are written out to the point of unification. Flushing the instruction |
| 388 | // cache ensures the newly written code will be fetched from the point of unification before |
| 389 | // use. Memory in the code cache is re-cycled as code is added and removed. The flushes |
| 390 | // prevent stale code from residing in the instruction cache. |
| 391 | // |
| 392 | // Caches are flushed before write permission is removed because some ARMv8 Qualcomm kernels |
| 393 | // may trigger a segfault if a page fault occurs when requesting a cache maintenance |
| 394 | // operation. This is a kernel bug that we need to work around until affected devices |
| 395 | // (e.g. Nexus 5X and 6P) stop being supported or their kernels are fixed. |
| 396 | // |
| 397 | // For reference, this behavior is caused by this commit: |
| 398 | // https://android.googlesource.com/kernel/msm/+/3fbe6bc28a6b9939d0650f2f17eb5216c719950c |
| 399 | // |
Orion Hodson | aeb0223 | 2019-06-25 14:18:18 +0100 | [diff] [blame] | 400 | bool cache_flush_success = true; |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 401 | if (HasDualCodeMapping()) { |
Orion Hodson | aeb0223 | 2019-06-25 14:18:18 +0100 | [diff] [blame] | 402 | // Flush d-cache for the non-executable mapping. |
| 403 | cache_flush_success = FlushCpuCaches(w_memory, w_memory + total_size); |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Orion Hodson | aeb0223 | 2019-06-25 14:18:18 +0100 | [diff] [blame] | 406 | // Invalidate i-cache for the executable mapping. |
| 407 | if (cache_flush_success) { |
| 408 | cache_flush_success = FlushCpuCaches(x_memory, x_memory + total_size); |
| 409 | } |
| 410 | |
| 411 | // If flushing the cache has failed, reject the allocation because we can't guarantee |
| 412 | // correctness of the instructions present in the processor caches. |
| 413 | if (!cache_flush_success) { |
| 414 | PLOG(ERROR) << "Cache flush failed triggering code allocation failure"; |
Orion Hodson | aeb0223 | 2019-06-25 14:18:18 +0100 | [diff] [blame] | 415 | return nullptr; |
| 416 | } |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 417 | |
| 418 | // Ensure CPU instruction pipelines are flushed for all cores. This is necessary for |
| 419 | // correctness as code may still be in instruction pipelines despite the i-cache flush. It is |
| 420 | // not safe to assume that changing permissions with mprotect (RX->RWX->RX) will cause a TLB |
| 421 | // shootdown (incidentally invalidating the CPU pipelines by sending an IPI to all cores to |
| 422 | // notify them of the TLB invalidation). Some architectures, notably ARM and ARM64, have |
| 423 | // hardware support that broadcasts TLB invalidations and so their kernels have no software |
| 424 | // based TLB shootdown. The sync-core flavor of membarrier was introduced in Linux 4.16 to |
| 425 | // address this (see mbarrier(2)). The membarrier here will fail on prior kernels and on |
| 426 | // platforms lacking the appropriate support. |
| 427 | art::membarrier(art::MembarrierCommand::kPrivateExpeditedSyncCore); |
| 428 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 429 | return result; |
| 430 | } |
| 431 | |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 432 | static void FillRootTable(uint8_t* roots_data, const std::vector<Handle<mirror::Object>>& roots) |
| 433 | REQUIRES(Locks::jit_lock_) |
| 434 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 435 | GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data); |
| 436 | const uint32_t length = roots.size(); |
| 437 | // Put all roots in `roots_data`. |
| 438 | for (uint32_t i = 0; i < length; ++i) { |
| 439 | ObjPtr<mirror::Object> object = roots[i].Get(); |
| 440 | gc_roots[i] = GcRoot<mirror::Object>(object); |
| 441 | } |
| 442 | // Store the length of the table at the end. This will allow fetching it from a stack_map |
| 443 | // pointer. |
| 444 | reinterpret_cast<uint32_t*>(roots_data)[length] = length; |
| 445 | } |
| 446 | |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 447 | bool JitMemoryRegion::CommitData(ArrayRef<const uint8_t> reserved_data, |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 448 | const std::vector<Handle<mirror::Object>>& roots, |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 449 | ArrayRef<const uint8_t> stack_map) { |
| 450 | DCHECK(IsInDataSpace(reserved_data.data())); |
| 451 | uint8_t* roots_data = GetWritableDataAddress(reserved_data.data()); |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 452 | size_t root_table_size = ComputeRootTableSize(roots.size()); |
| 453 | uint8_t* stack_map_data = roots_data + root_table_size; |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 454 | DCHECK_LE(root_table_size + stack_map.size(), reserved_data.size()); |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 455 | FillRootTable(roots_data, roots); |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 456 | memcpy(stack_map_data, stack_map.data(), stack_map.size()); |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 457 | // Flush data cache, as compiled code references literals in it. |
Orion Hodson | aeb0223 | 2019-06-25 14:18:18 +0100 | [diff] [blame] | 458 | // TODO(oth): establish whether this is necessary. |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 459 | if (UNLIKELY(!FlushCpuCaches(roots_data, roots_data + root_table_size + stack_map.size()))) { |
Orion Hodson | aeb0223 | 2019-06-25 14:18:18 +0100 | [diff] [blame] | 460 | VLOG(jit) << "Failed to flush data in CommitData"; |
| 461 | return false; |
| 462 | } |
| 463 | return true; |
Nicolas Geoffray | 00a37ff | 2019-06-20 14:27:22 +0100 | [diff] [blame] | 464 | } |
| 465 | |
David Srbecky | adb66f9 | 2019-10-10 12:59:43 +0000 | [diff] [blame] | 466 | const uint8_t* JitMemoryRegion::AllocateCode(size_t size) { |
| 467 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 468 | void* result = mspace_memalign(exec_mspace_, alignment, size); |
| 469 | if (UNLIKELY(result == nullptr)) { |
| 470 | return nullptr; |
| 471 | } |
| 472 | used_memory_for_code_ += mspace_usable_size(result); |
| 473 | return reinterpret_cast<uint8_t*>(GetExecutableAddress(result)); |
| 474 | } |
| 475 | |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 476 | void JitMemoryRegion::FreeCode(const uint8_t* code) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 477 | code = GetNonExecutableAddress(code); |
| 478 | used_memory_for_code_ -= mspace_usable_size(code); |
Nicolas Geoffray | 349845a | 2019-06-19 13:13:10 +0100 | [diff] [blame] | 479 | mspace_free(exec_mspace_, const_cast<uint8_t*>(code)); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 480 | } |
| 481 | |
David Srbecky | 87fb032 | 2019-08-20 10:34:02 +0100 | [diff] [blame] | 482 | const uint8_t* JitMemoryRegion::AllocateData(size_t data_size) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 483 | void* result = mspace_malloc(data_mspace_, data_size); |
Orion Hodson | acb1237 | 2019-09-06 10:04:01 +0100 | [diff] [blame] | 484 | if (UNLIKELY(result == nullptr)) { |
| 485 | return nullptr; |
| 486 | } |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 487 | used_memory_for_data_ += mspace_usable_size(result); |
Nicolas Geoffray | ac933ed | 2019-06-26 13:36:37 +0100 | [diff] [blame] | 488 | return reinterpret_cast<uint8_t*>(GetNonWritableDataAddress(result)); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 489 | } |
| 490 | |
David Srbecky | 87fb032 | 2019-08-20 10:34:02 +0100 | [diff] [blame] | 491 | void JitMemoryRegion::FreeData(const uint8_t* data) { |
| 492 | FreeWritableData(GetWritableDataAddress(data)); |
| 493 | } |
| 494 | |
| 495 | void JitMemoryRegion::FreeWritableData(uint8_t* writable_data) REQUIRES(Locks::jit_lock_) { |
| 496 | used_memory_for_data_ -= mspace_usable_size(writable_data); |
| 497 | mspace_free(data_mspace_, writable_data); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Nicolas Geoffray | ab682a7 | 2019-07-04 10:11:55 +0100 | [diff] [blame] | 500 | #if defined(__BIONIC__) && defined(ART_TARGET) |
| 501 | // The code below only works on bionic on target. |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 502 | |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 503 | int JitMemoryRegion::CreateZygoteMemory(size_t capacity, std::string* error_msg) { |
Orion Hodson | d17eac6 | 2019-07-03 17:39:12 +0100 | [diff] [blame] | 504 | if (CacheOperationsMaySegFault()) { |
| 505 | // Zygote JIT requires dual code mappings by design. We can only do this if the cache flush |
| 506 | // and invalidate instructions work without raising faults. |
| 507 | *error_msg = "Zygote memory only works with dual mappings"; |
| 508 | return -1; |
| 509 | } |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 510 | /* Check if kernel support exists, otherwise fall back to ashmem */ |
Nicolas Geoffray | 00b8d45 | 2019-10-02 15:34:45 +0100 | [diff] [blame] | 511 | static const char* kRegionName = "jit-zygote-cache"; |
Nicolas Geoffray | 3a614ea | 2019-06-27 15:47:09 +0100 | [diff] [blame] | 512 | if (art::IsSealFutureWriteSupported()) { |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 513 | int fd = art::memfd_create(kRegionName, MFD_ALLOW_SEALING); |
| 514 | if (fd == -1) { |
| 515 | std::ostringstream oss; |
| 516 | oss << "Failed to create zygote mapping: " << strerror(errno); |
| 517 | *error_msg = oss.str(); |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | if (ftruncate(fd, capacity) != 0) { |
| 522 | std::ostringstream oss; |
| 523 | oss << "Failed to create zygote mapping: " << strerror(errno); |
| 524 | *error_msg = oss.str(); |
| 525 | return -1; |
| 526 | } |
| 527 | |
| 528 | return fd; |
| 529 | } |
| 530 | |
| 531 | LOG(INFO) << "Falling back to ashmem implementation for JIT zygote mapping"; |
| 532 | |
| 533 | int fd; |
Orion Hodson | c5323fe | 2021-02-04 21:20:30 +0000 | [diff] [blame] | 534 | palette_status_t status = PaletteAshmemCreateRegion(kRegionName, capacity, &fd); |
| 535 | if (status != PALETTE_STATUS_OK) { |
| 536 | CHECK_EQ(status, PALETTE_STATUS_CHECK_ERRNO); |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 537 | std::ostringstream oss; |
| 538 | oss << "Failed to create zygote mapping: " << strerror(errno); |
| 539 | *error_msg = oss.str(); |
| 540 | return -1; |
| 541 | } |
| 542 | return fd; |
| 543 | } |
| 544 | |
| 545 | bool JitMemoryRegion::ProtectZygoteMemory(int fd, std::string* error_msg) { |
Nicolas Geoffray | 3a614ea | 2019-06-27 15:47:09 +0100 | [diff] [blame] | 546 | if (art::IsSealFutureWriteSupported()) { |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 547 | if (fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL | F_SEAL_FUTURE_WRITE) |
| 548 | == -1) { |
| 549 | std::ostringstream oss; |
| 550 | oss << "Failed to protect zygote mapping: " << strerror(errno); |
| 551 | *error_msg = oss.str(); |
| 552 | return false; |
| 553 | } |
| 554 | } else { |
Nicolas Geoffray | 8c63ce2 | 2021-10-19 17:14:04 +0100 | [diff] [blame] | 555 | palette_status_t status = PaletteAshmemSetProtRegion(fd, PROT_READ | PROT_EXEC); |
Orion Hodson | c5323fe | 2021-02-04 21:20:30 +0000 | [diff] [blame] | 556 | if (status != PALETTE_STATUS_OK) { |
| 557 | CHECK_EQ(status, PALETTE_STATUS_CHECK_ERRNO); |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 558 | std::ostringstream oss; |
| 559 | oss << "Failed to protect zygote mapping: " << strerror(errno); |
| 560 | *error_msg = oss.str(); |
| 561 | return false; |
| 562 | } |
| 563 | } |
| 564 | return true; |
| 565 | } |
| 566 | |
| 567 | #else |
| 568 | |
Nicolas Geoffray | af213cc | 2019-07-01 10:50:55 +0100 | [diff] [blame] | 569 | int JitMemoryRegion::CreateZygoteMemory(size_t capacity, std::string* error_msg) { |
| 570 | // To simplify host building, we don't rely on the latest memfd features. |
| 571 | LOG(WARNING) << "Returning un-sealable region on non-bionic"; |
| 572 | static const char* kRegionName = "/jit-zygote-cache"; |
| 573 | int fd = art::memfd_create(kRegionName, 0); |
| 574 | if (fd == -1) { |
| 575 | std::ostringstream oss; |
| 576 | oss << "Failed to create zygote mapping: " << strerror(errno); |
| 577 | *error_msg = oss.str(); |
| 578 | return -1; |
| 579 | } |
| 580 | if (ftruncate(fd, capacity) != 0) { |
| 581 | std::ostringstream oss; |
| 582 | oss << "Failed to create zygote mapping: " << strerror(errno); |
| 583 | *error_msg = oss.str(); |
| 584 | return -1; |
| 585 | } |
| 586 | return fd; |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | bool JitMemoryRegion::ProtectZygoteMemory(int fd ATTRIBUTE_UNUSED, |
| 590 | std::string* error_msg ATTRIBUTE_UNUSED) { |
| 591 | return true; |
| 592 | } |
| 593 | |
| 594 | #endif |
| 595 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 596 | } // namespace jit |
| 597 | } // namespace art |