1 /*
   2  * Copyright (c) 2019, 2020, 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/zGlobals.hpp"
  26 #include "gc/z/zGranuleMap.inline.hpp"
  27 #include "gc/z/zMapper_windows.hpp"
  28 #include "gc/z/zPhysicalMemoryBacking_windows.hpp"
  29 #include "logging/log.hpp"
  30 #include "runtime/globals.hpp"
  31 #include "utilities/debug.hpp"
  32 
  33 // The backing commits and uncommits physical memory, that can be
  34 // multi-mapped into the virtual address space. To support fine-graned
  35 // committing and uncommitting, each ZGranuleSize'd chunk is mapped to
  36 // a separate paging file mapping.
  37 
  38 ZPhysicalMemoryBacking::ZPhysicalMemoryBacking(size_t max_capacity) :
  39     _handles(max_capacity) {}
  40 
  41 bool ZPhysicalMemoryBacking::is_initialized() const {
  42   return true;
  43 }
  44 
  45 void ZPhysicalMemoryBacking::warn_commit_limits(size_t max_capacity) const {
  46   // Does nothing
  47 }
  48 
  49 HANDLE ZPhysicalMemoryBacking::get_handle(uintptr_t offset) const {
  50   HANDLE const handle = _handles.get(offset);
  51   assert(handle != 0, "Should be set");
  52   return handle;
  53 }
  54 
  55 void ZPhysicalMemoryBacking::put_handle(uintptr_t offset, HANDLE handle) {
  56   assert(handle != INVALID_HANDLE_VALUE, "Invalid handle");
  57   assert(_handles.get(offset) == 0, "Should be cleared");
  58   _handles.put(offset, handle);
  59 }
  60 
  61 void ZPhysicalMemoryBacking::clear_handle(uintptr_t offset) {
  62   assert(_handles.get(offset) != 0, "Should be set");
  63   _handles.put(offset, 0);
  64 }
  65 
  66 size_t ZPhysicalMemoryBacking::commit_from_paging_file(size_t offset, size_t size) {
  67   for (size_t i = 0; i < size; i += ZGranuleSize) {
  68     HANDLE const handle = ZMapper::create_and_commit_paging_file_mapping(ZGranuleSize);
  69     if (handle == 0) {
  70       return i;
  71     }
  72 
  73     put_handle(offset + i, handle);
  74   }
  75 
  76   return size;
  77 }
  78 
  79 size_t ZPhysicalMemoryBacking::uncommit_from_paging_file(size_t offset, size_t size) {
  80   for (size_t i = 0; i < size; i += ZGranuleSize) {
  81     HANDLE const handle = get_handle(offset + i);
  82     clear_handle(offset + i);
  83     ZMapper::close_paging_file_mapping(handle);
  84   }
  85 
  86   return size;
  87 }
  88 
  89 size_t ZPhysicalMemoryBacking::commit(size_t offset, size_t length) {
  90   log_trace(gc, heap)("Committing memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
  91                       offset / M, (offset + length) / M, length / M);
  92 
  93   return commit_from_paging_file(offset, length);
  94 }
  95 
  96 size_t ZPhysicalMemoryBacking::uncommit(size_t offset, size_t length) {
  97   log_trace(gc, heap)("Uncommitting memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
  98                       offset / M, (offset + length) / M, length / M);
  99 
 100   return uncommit_from_paging_file(offset, length);
 101 }
 102 
 103 bool ZPhysicalMemoryBacking::map(uintptr_t addr, size_t size, size_t offset) const {
 104   assert(is_aligned(offset, ZGranuleSize), "Misaligned");
 105   assert(is_aligned(addr, ZGranuleSize), "Misaligned");
 106   assert(is_aligned(size, ZGranuleSize), "Misaligned");
 107 
 108   for (size_t i = 0; i < size; i += ZGranuleSize) {
 109     HANDLE const handle = get_handle(offset + i);
 110     if (!ZMapper::map_view_replace_placeholder(handle, 0 /* offset */, addr + i, ZGranuleSize)) {
 111       // Unmap any successfully mapped granules
 112       if (i > 0) {
 113         unmap(addr, i - ZGranuleSize);
 114       }
 115       return false;
 116     }
 117   }
 118 
 119   return true;
 120 }
 121 
 122 void ZPhysicalMemoryBacking::unmap(uintptr_t addr, size_t size) const {
 123   assert(is_aligned(addr, ZGranuleSize), "Misaligned");
 124   assert(is_aligned(size, ZGranuleSize), "Misaligned");
 125 
 126   for (size_t i = 0; i < size; i += ZGranuleSize) {
 127     ZMapper::unmap_view_preserve_placeholder(addr + i, ZGranuleSize);
 128   }
 129 }