blob: b8b7827cb47d847c56f5b1d9143d9f4c3b586f47 [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#include "jit_memory_region.h"
18
Nicolas Geoffray2411f492019-06-14 08:54:46 +010019#include <fcntl.h>
20#include <unistd.h>
21
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010022#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 Geoffray349845a2019-06-19 13:13:10 +010026#include "base/membarrier.h"
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010027#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 Geoffray2411f492019-06-14 08:54:46 +010032#include "palette/palette.h"
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010033
34using android::base::unique_fd;
35
36namespace art {
37namespace 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?
42static constexpr size_t kCodeAndDataCapacityDivider = 2;
43
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010044bool 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 Geoffray2a905b22019-06-06 09:04:07 +010049 ScopedTrace trace(__PRETTY_FUNCTION__);
50
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010051 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 Geoffray2a905b22019-06-06 09:04:07 +010060 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
67 // Zygote shouldn't create a shared mapping for JIT, so we cannot use dual view
68 // for it.
69 if (!is_zygote) {
70 // Bionic supports memfd_create, but the call may fail on older kernels.
71 mem_fd = unique_fd(art::memfd_create("/jit-cache", /* flags= */ 0));
72 if (mem_fd.get() < 0) {
73 std::ostringstream oss;
74 oss << "Failed to initialize dual view JIT. memfd_create() error: " << strerror(errno);
75 if (!rwx_memory_allowed) {
76 // Without using RWX page permissions, the JIT can not fallback to single mapping as it
77 // requires tranitioning the code pages to RWX for updates.
78 *error_msg = oss.str();
79 return false;
80 }
81 VLOG(jit) << oss.str();
82 }
83 }
84
85 if (mem_fd.get() >= 0 && ftruncate(mem_fd, capacity) != 0) {
86 std::ostringstream oss;
87 oss << "Failed to initialize memory file: " << strerror(errno);
88 *error_msg = oss.str();
89 return false;
90 }
91
92 std::string data_cache_name = is_zygote ? "zygote-data-code-cache" : "data-code-cache";
93 std::string exec_cache_name = is_zygote ? "zygote-jit-code-cache" : "jit-code-cache";
94
95 std::string error_str;
96 // Map name specific for android_os_Debug.cpp accounting.
97 // Map in low 4gb to simplify accessing root tables for x86_64.
98 // We could do PC-relative addressing to avoid this problem, but that
99 // would require reserving code and data area before submitting, which
100 // means more windows for the code memory to be RWX.
101 int base_flags;
102 MemMap data_pages;
103 if (mem_fd.get() >= 0) {
104 // Dual view of JIT code cache case. Create an initial mapping of data pages large enough
105 // for data and non-writable view of JIT code pages. We use the memory file descriptor to
106 // enable dual mapping - we'll create a second mapping using the descriptor below. The
107 // mappings will look like:
108 //
109 // VA PA
110 //
111 // +---------------+
112 // | non exec code |\
113 // +---------------+ \
114 // : :\ \
115 // +---------------+.\.+---------------+
116 // | exec code | \| code |
117 // +---------------+...+---------------+
118 // | data | | data |
119 // +---------------+...+---------------+
120 //
121 // In this configuration code updates are written to the non-executable view of the code
122 // cache, and the executable view of the code cache has fixed RX memory protections.
123 //
124 // This memory needs to be mapped shared as the code portions will have two mappings.
125 base_flags = MAP_SHARED;
126 data_pages = MemMap::MapFile(
127 data_capacity + exec_capacity,
128 kProtRW,
129 base_flags,
130 mem_fd,
131 /* start= */ 0,
132 /* low_4gb= */ true,
133 data_cache_name.c_str(),
134 &error_str);
135 } else {
136 // Single view of JIT code cache case. Create an initial mapping of data pages large enough
137 // for data and JIT code pages. The mappings will look like:
138 //
139 // VA PA
140 //
141 // +---------------+...+---------------+
142 // | exec code | | code |
143 // +---------------+...+---------------+
144 // | data | | data |
145 // +---------------+...+---------------+
146 //
147 // In this configuration code updates are written to the executable view of the code cache,
148 // and the executable view of the code cache transitions RX to RWX for the update and then
149 // back to RX after the update.
150 base_flags = MAP_PRIVATE | MAP_ANON;
151 data_pages = MemMap::MapAnonymous(
152 data_cache_name.c_str(),
153 data_capacity + exec_capacity,
154 kProtRW,
155 /* low_4gb= */ true,
156 &error_str);
157 }
158
159 if (!data_pages.IsValid()) {
160 std::ostringstream oss;
161 oss << "Failed to create read write cache: " << error_str << " size=" << capacity;
162 *error_msg = oss.str();
163 return false;
164 }
165
166 MemMap exec_pages;
167 MemMap non_exec_pages;
168 if (exec_capacity > 0) {
169 uint8_t* const divider = data_pages.Begin() + data_capacity;
170 // Set initial permission for executable view to catch any SELinux permission problems early
171 // (for processes that cannot map WX pages). Otherwise, this region does not need to be
172 // executable as there is no code in the cache yet.
173 exec_pages = data_pages.RemapAtEnd(divider,
174 exec_cache_name.c_str(),
175 kProtRX,
176 base_flags | MAP_FIXED,
177 mem_fd.get(),
178 (mem_fd.get() >= 0) ? data_capacity : 0,
179 &error_str);
180 if (!exec_pages.IsValid()) {
181 std::ostringstream oss;
182 oss << "Failed to create read execute code cache: " << error_str << " size=" << capacity;
183 *error_msg = oss.str();
184 return false;
185 }
186
187 if (mem_fd.get() >= 0) {
188 // For dual view, create the secondary view of code memory used for updating code. This view
189 // is never executable.
190 std::string name = exec_cache_name + "-rw";
191 non_exec_pages = MemMap::MapFile(exec_capacity,
192 kProtR,
193 base_flags,
194 mem_fd,
195 /* start= */ data_capacity,
196 /* low_4GB= */ false,
197 name.c_str(),
198 &error_str);
199 if (!non_exec_pages.IsValid()) {
200 static const char* kFailedNxView = "Failed to map non-executable view of JIT code cache";
201 if (rwx_memory_allowed) {
202 // Log and continue as single view JIT (requires RWX memory).
203 VLOG(jit) << kFailedNxView;
204 } else {
205 *error_msg = kFailedNxView;
206 return false;
207 }
208 }
209 }
210 } else {
211 // Profiling only. No memory for code required.
212 }
213
214 data_pages_ = std::move(data_pages);
215 exec_pages_ = std::move(exec_pages);
216 non_exec_pages_ = std::move(non_exec_pages);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100217
Nicolas Geoffray9c54e182019-06-18 10:42:52 +0100218 // Now that the pages are initialized, initialize the spaces.
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100219
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100220 // Initialize the data heap
221 data_mspace_ = create_mspace_with_base(data_pages_.Begin(), data_end_, false /*locked*/);
222 CHECK(data_mspace_ != nullptr) << "create_mspace_with_base (data) failed";
223
224 // Initialize the code heap
225 MemMap* code_heap = nullptr;
226 if (non_exec_pages_.IsValid()) {
227 code_heap = &non_exec_pages_;
228 } else if (exec_pages_.IsValid()) {
229 code_heap = &exec_pages_;
230 }
231 if (code_heap != nullptr) {
232 // Make all pages reserved for the code heap writable. The mspace allocator, that manages the
233 // heap, will take and initialize pages in create_mspace_with_base().
234 CheckedCall(mprotect, "create code heap", code_heap->Begin(), code_heap->Size(), kProtRW);
235 exec_mspace_ = create_mspace_with_base(code_heap->Begin(), exec_end_, false /*locked*/);
236 CHECK(exec_mspace_ != nullptr) << "create_mspace_with_base (exec) failed";
237 SetFootprintLimit(initial_capacity_);
238 // Protect pages containing heap metadata. Updates to the code heap toggle write permission to
239 // perform the update and there are no other times write access is required.
240 CheckedCall(mprotect, "protect code heap", code_heap->Begin(), code_heap->Size(), kProtR);
241 } else {
242 exec_mspace_ = nullptr;
243 SetFootprintLimit(initial_capacity_);
244 }
Nicolas Geoffray9c54e182019-06-18 10:42:52 +0100245
246 return true;
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100247}
248
249void JitMemoryRegion::SetFootprintLimit(size_t new_footprint) {
250 size_t data_space_footprint = new_footprint / kCodeAndDataCapacityDivider;
251 DCHECK(IsAlignedParam(data_space_footprint, kPageSize));
252 DCHECK_EQ(data_space_footprint * kCodeAndDataCapacityDivider, new_footprint);
253 mspace_set_footprint_limit(data_mspace_, data_space_footprint);
254 if (HasCodeMapping()) {
255 ScopedCodeCacheWrite scc(*this);
256 mspace_set_footprint_limit(exec_mspace_, new_footprint - data_space_footprint);
257 }
258}
259
260bool JitMemoryRegion::IncreaseCodeCacheCapacity() {
261 if (current_capacity_ == max_capacity_) {
262 return false;
263 }
264
265 // Double the capacity if we're below 1MB, or increase it by 1MB if
266 // we're above.
267 if (current_capacity_ < 1 * MB) {
268 current_capacity_ *= 2;
269 } else {
270 current_capacity_ += 1 * MB;
271 }
272 if (current_capacity_ > max_capacity_) {
273 current_capacity_ = max_capacity_;
274 }
275
276 VLOG(jit) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
277
278 SetFootprintLimit(current_capacity_);
279
280 return true;
281}
282
283// NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
284// is already held.
285void* JitMemoryRegion::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
286 if (mspace == exec_mspace_) {
287 DCHECK(exec_mspace_ != nullptr);
288 const MemMap* const code_pages = GetUpdatableCodeMapping();
289 void* result = code_pages->Begin() + exec_end_;
290 exec_end_ += increment;
291 return result;
292 } else {
293 DCHECK_EQ(data_mspace_, mspace);
294 void* result = data_pages_.Begin() + data_end_;
295 data_end_ += increment;
296 return result;
297 }
298}
299
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100300const uint8_t* JitMemoryRegion::AllocateCode(const uint8_t* code,
301 size_t code_size,
302 const uint8_t* stack_map,
303 bool has_should_deoptimize_flag) {
304 ScopedCodeCacheWrite scc(*this);
305
306 size_t alignment = GetJitCodeAlignment();
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100307 // Ensure the header ends up at expected instruction alignment.
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100308 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
309 size_t total_size = header_size + code_size;
310
311 // Each allocation should be on its own set of cache lines.
312 // `total_size` covers the OatQuickMethodHeader, the JIT generated machine code,
313 // and any alignment padding.
314 DCHECK_GT(total_size, header_size);
315 uint8_t* w_memory = reinterpret_cast<uint8_t*>(
316 mspace_memalign(exec_mspace_, alignment, total_size));
317 if (w_memory == nullptr) {
318 return nullptr;
319 }
320 uint8_t* x_memory = GetExecutableAddress(w_memory);
321 // Ensure the header ends up at expected instruction alignment.
322 DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(w_memory + header_size), alignment);
323 used_memory_for_code_ += mspace_usable_size(w_memory);
324 const uint8_t* result = x_memory + header_size;
325
326 // Write the code.
327 std::copy(code, code + code_size, w_memory + header_size);
328
329 // Write the header.
330 OatQuickMethodHeader* method_header =
331 OatQuickMethodHeader::FromCodePointer(w_memory + header_size);
332 new (method_header) OatQuickMethodHeader(
333 (stack_map != nullptr) ? result - stack_map : 0u,
334 code_size);
335 if (has_should_deoptimize_flag) {
336 method_header->SetHasShouldDeoptimizeFlag();
337 }
338
339 // Both instruction and data caches need flushing to the point of unification where both share
340 // a common view of memory. Flushing the data cache ensures the dirty cachelines from the
341 // newly added code are written out to the point of unification. Flushing the instruction
342 // cache ensures the newly written code will be fetched from the point of unification before
343 // use. Memory in the code cache is re-cycled as code is added and removed. The flushes
344 // prevent stale code from residing in the instruction cache.
345 //
346 // Caches are flushed before write permission is removed because some ARMv8 Qualcomm kernels
347 // may trigger a segfault if a page fault occurs when requesting a cache maintenance
348 // operation. This is a kernel bug that we need to work around until affected devices
349 // (e.g. Nexus 5X and 6P) stop being supported or their kernels are fixed.
350 //
351 // For reference, this behavior is caused by this commit:
352 // https://android.googlesource.com/kernel/msm/+/3fbe6bc28a6b9939d0650f2f17eb5216c719950c
353 //
354 if (HasDualCodeMapping()) {
355 // Flush the data cache lines associated with the non-executable copy of the code just added.
356 FlushDataCache(w_memory, w_memory + total_size);
357 }
358
359 // FlushInstructionCache() flushes both data and instruction caches lines. The cacheline range
360 // flushed is for the executable mapping of the code just added.
361 FlushInstructionCache(x_memory, x_memory + total_size);
362
363 // Ensure CPU instruction pipelines are flushed for all cores. This is necessary for
364 // correctness as code may still be in instruction pipelines despite the i-cache flush. It is
365 // not safe to assume that changing permissions with mprotect (RX->RWX->RX) will cause a TLB
366 // shootdown (incidentally invalidating the CPU pipelines by sending an IPI to all cores to
367 // notify them of the TLB invalidation). Some architectures, notably ARM and ARM64, have
368 // hardware support that broadcasts TLB invalidations and so their kernels have no software
369 // based TLB shootdown. The sync-core flavor of membarrier was introduced in Linux 4.16 to
370 // address this (see mbarrier(2)). The membarrier here will fail on prior kernels and on
371 // platforms lacking the appropriate support.
372 art::membarrier(art::MembarrierCommand::kPrivateExpeditedSyncCore);
373
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100374 return result;
375}
376
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +0100377static void FillRootTable(uint8_t* roots_data, const std::vector<Handle<mirror::Object>>& roots)
378 REQUIRES(Locks::jit_lock_)
379 REQUIRES_SHARED(Locks::mutator_lock_) {
380 GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
381 const uint32_t length = roots.size();
382 // Put all roots in `roots_data`.
383 for (uint32_t i = 0; i < length; ++i) {
384 ObjPtr<mirror::Object> object = roots[i].Get();
385 gc_roots[i] = GcRoot<mirror::Object>(object);
386 }
387 // Store the length of the table at the end. This will allow fetching it from a stack_map
388 // pointer.
389 reinterpret_cast<uint32_t*>(roots_data)[length] = length;
390}
391
392void JitMemoryRegion::CommitData(uint8_t* roots_data,
393 const std::vector<Handle<mirror::Object>>& roots,
394 const uint8_t* stack_map,
395 size_t stack_map_size) {
396 size_t root_table_size = ComputeRootTableSize(roots.size());
397 uint8_t* stack_map_data = roots_data + root_table_size;
398 FillRootTable(roots_data, roots);
399 memcpy(stack_map_data, stack_map, stack_map_size);
400 // Flush data cache, as compiled code references literals in it.
401 FlushDataCache(roots_data, roots_data + root_table_size + stack_map_size);
402}
403
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100404void JitMemoryRegion::FreeCode(const uint8_t* code) {
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100405 code = GetNonExecutableAddress(code);
406 used_memory_for_code_ -= mspace_usable_size(code);
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100407 mspace_free(exec_mspace_, const_cast<uint8_t*>(code));
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100408}
409
410uint8_t* JitMemoryRegion::AllocateData(size_t data_size) {
411 void* result = mspace_malloc(data_mspace_, data_size);
412 used_memory_for_data_ += mspace_usable_size(result);
413 return reinterpret_cast<uint8_t*>(result);
414}
415
416void JitMemoryRegion::FreeData(uint8_t* data) {
417 used_memory_for_data_ -= mspace_usable_size(data);
418 mspace_free(data_mspace_, data);
419}
420
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100421#if defined(__BIONIC__)
422
423static bool IsSealFutureWriteSupportedInternal() {
424 unique_fd fd(art::memfd_create("test_android_memfd", MFD_ALLOW_SEALING));
425 if (fd == -1) {
426 LOG(INFO) << "memfd_create failed: " << strerror(errno) << ", no memfd support.";
427 return false;
428 }
429
430 if (fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) == -1) {
431 LOG(INFO) << "fcntl(F_ADD_SEALS) failed: " << strerror(errno) << ", no memfd support.";
432 return false;
433 }
434
435 LOG(INFO) << "Using memfd for future sealing";
436 return true;
437}
438
439static bool IsSealFutureWriteSupported() {
440 static bool is_seal_future_write_supported = IsSealFutureWriteSupportedInternal();
441 return is_seal_future_write_supported;
442}
443
444int JitMemoryRegion::CreateZygoteMemory(size_t capacity, std::string* error_msg) {
445 /* Check if kernel support exists, otherwise fall back to ashmem */
446 static const char* kRegionName = "/jit-zygote-cache";
447 if (IsSealFutureWriteSupported()) {
448 int fd = art::memfd_create(kRegionName, MFD_ALLOW_SEALING);
449 if (fd == -1) {
450 std::ostringstream oss;
451 oss << "Failed to create zygote mapping: " << strerror(errno);
452 *error_msg = oss.str();
453 return -1;
454 }
455
456 if (ftruncate(fd, capacity) != 0) {
457 std::ostringstream oss;
458 oss << "Failed to create zygote mapping: " << strerror(errno);
459 *error_msg = oss.str();
460 return -1;
461 }
462
463 return fd;
464 }
465
466 LOG(INFO) << "Falling back to ashmem implementation for JIT zygote mapping";
467
468 int fd;
469 PaletteStatus status = PaletteAshmemCreateRegion(kRegionName, capacity, &fd);
470 if (status != PaletteStatus::kOkay) {
471 CHECK_EQ(status, PaletteStatus::kCheckErrno);
472 std::ostringstream oss;
473 oss << "Failed to create zygote mapping: " << strerror(errno);
474 *error_msg = oss.str();
475 return -1;
476 }
477 return fd;
478}
479
480bool JitMemoryRegion::ProtectZygoteMemory(int fd, std::string* error_msg) {
481 if (IsSealFutureWriteSupported()) {
482 if (fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL | F_SEAL_FUTURE_WRITE)
483 == -1) {
484 std::ostringstream oss;
485 oss << "Failed to protect zygote mapping: " << strerror(errno);
486 *error_msg = oss.str();
487 return false;
488 }
489 } else {
490 PaletteStatus status = PaletteAshmemSetProtRegion(fd, PROT_READ);
491 if (status != PaletteStatus::kOkay) {
492 CHECK_EQ(status, PaletteStatus::kCheckErrno);
493 std::ostringstream oss;
494 oss << "Failed to protect zygote mapping: " << strerror(errno);
495 *error_msg = oss.str();
496 return false;
497 }
498 }
499 return true;
500}
501
502#else
503
504// When running on non-bionic configuration, this is not supported.
505int JitMemoryRegion::CreateZygoteMemory(size_t capacity ATTRIBUTE_UNUSED,
506 std::string* error_msg ATTRIBUTE_UNUSED) {
507 return -1;
508}
509
510bool JitMemoryRegion::ProtectZygoteMemory(int fd ATTRIBUTE_UNUSED,
511 std::string* error_msg ATTRIBUTE_UNUSED) {
512 return true;
513}
514
515#endif
516
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100517} // namespace jit
518} // namespace art