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