1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/z/zBackingFile_bsd.hpp"
  26 #include "gc/z/zErrno.hpp"
  27 #include "gc/z/zGlobals.hpp"
  28 #include "gc/z/zLargePages.inline.hpp"
  29 #include "gc/z/zPhysicalMemory.inline.hpp"
  30 #include "logging/log.hpp"
  31 #include "runtime/globals.hpp"
  32 #include "runtime/os.hpp"
  33 #include "utilities/align.hpp"
  34 #include "utilities/debug.hpp"
  35 
  36 #include <mach/mach.h>
  37 #include <mach/mach_vm.h>
  38 #include <sys/mman.h>
  39 #include <sys/types.h>
  40 
  41 static int vm_flags_superpage() {
  42   if (!ZLargePages::is_explicit()) {
  43     return 0;
  44   }
  45 
  46   const int page_size_in_megabytes = ZGranuleSize >> 20;
  47   return page_size_in_megabytes << VM_FLAGS_SUPERPAGE_SHIFT;
  48 }
  49 
  50 static ZErrno mremap(uintptr_t from_addr, uintptr_t to_addr, size_t size) {
  51   mach_vm_address_t remap_addr = to_addr;
  52   vm_prot_t remap_cur_prot;
  53   vm_prot_t remap_max_prot;
  54 
  55   // Remap memory to an additional location
  56   const kern_return_t res = mach_vm_remap(mach_task_self(),
  57                                           &remap_addr,
  58                                           size,
  59                                           0 /* mask */,
  60                                           VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE | vm_flags_superpage(),
  61                                           mach_task_self(),
  62                                           from_addr,
  63                                           FALSE /* copy */,
  64                                           &remap_cur_prot,
  65                                           &remap_max_prot,
  66                                           VM_INHERIT_COPY);
  67 
  68   return (res == KERN_SUCCESS) ? ZErrno(0) : ZErrno(EINVAL);
  69 }
  70 
  71 ZBackingFile::ZBackingFile() :
  72     _base(0),
  73     _size(0),
  74     _initialized(false) {
  75 
  76   // Reserve address space for virtual backing file
  77   _base = (uintptr_t)os::reserve_memory(MaxHeapSize);
  78   if (_base == 0) {
  79     // Failed
  80     log_error(gc)("Failed to reserve address space for virtual backing file");
  81     return;
  82   }
  83 
  84   // Successfully initialized
  85   _initialized = true;
  86 }
  87 
  88 bool ZBackingFile::is_initialized() const {
  89   return _initialized;
  90 }
  91 
  92 void ZBackingFile::warn_commit_limits(size_t max) const {
  93   // Does nothing
  94 }
  95 
  96 size_t ZBackingFile::size() const {
  97   return _size;
  98 }
  99 
 100 bool ZBackingFile::commit_inner(size_t offset, size_t length) {
 101   assert(is_aligned(offset, os::vm_page_size()), "Invalid offset");
 102   assert(is_aligned(length, os::vm_page_size()), "Invalid length");
 103 
 104   log_trace(gc, heap)("Committing memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
 105                       offset / M, (offset + length) / M, length / M);
 106 
 107   const uintptr_t addr = _base + offset;
 108   const void* const res = mmap((void*)addr, length, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
 109   if (res == MAP_FAILED) {
 110     ZErrno err;
 111     log_error(gc)("Failed to commit memory (%s)", err.to_string());
 112     return false;
 113   }
 114 
 115   const size_t end = offset + length;
 116   if (end > _size) {
 117     // Record new virtual file size
 118     _size = end;
 119   }
 120 
 121   // Success
 122   return true;
 123 }
 124 
 125 size_t ZBackingFile::commit(size_t offset, size_t length) {
 126   // Try to commit the whole region
 127   if (commit_inner(offset, length)) {
 128     // Success
 129     return length;
 130   }
 131 
 132   // Failed, try to commit as much as possible
 133   size_t start = offset;
 134   size_t end = offset + length;
 135 
 136   for (;;) {
 137     length = align_down((end - start) / 2, ZGranuleSize);
 138     if (length == 0) {
 139       // Done, don't commit more
 140       return start - offset;
 141     }
 142 
 143     if (commit_inner(start, length)) {
 144       // Success, try commit more
 145       start += length;
 146     } else {
 147       // Failed, try commit less
 148       end -= length;
 149     }
 150   }
 151 }
 152 
 153 size_t ZBackingFile::uncommit(size_t offset, size_t length) {
 154   assert(is_aligned(offset, os::vm_page_size()), "Invalid offset");
 155   assert(is_aligned(length, os::vm_page_size()), "Invalid length");
 156 
 157   log_trace(gc, heap)("Uncommitting memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
 158                       offset / M, (offset + length) / M, length / M);
 159 
 160   const uintptr_t start = _base + offset;
 161   const void* const res = mmap((void*)start, length, PROT_NONE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
 162   if (res == MAP_FAILED) {
 163     ZErrno err;
 164     log_error(gc)("Failed to uncommit memory (%s)", err.to_string());
 165     return 0;
 166   }
 167 
 168   return length;
 169 }
 170 
 171 void ZBackingFile::map(uintptr_t addr, size_t size, uintptr_t offset) const {
 172   const ZErrno err = mremap(_base + offset, addr, size);
 173   if (err) {
 174     fatal("Failed to remap memory (%s)", err.to_string());
 175   }
 176 }
 177 
 178 void ZBackingFile::unmap(uintptr_t addr, size_t size) const {
 179   // Note that we must keep the address space reservation intact and just detach
 180   // the backing memory. For this reason we map a new anonymous, non-accessible
 181   // and non-reserved page over the mapping instead of actually unmapping.
 182   const void* const res = mmap((void*)addr, size, PROT_NONE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
 183   if (res == MAP_FAILED) {
 184     ZErrno err;
 185     fatal("Failed to map memory (%s)", err.to_string());
 186   }
 187 }