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_windows.hpp"
  26 #include "gc/z/zGlobals.hpp"
  27 #include "gc/z/zGranuleMap.inline.hpp"
  28 #include "gc/z/zMapper_windows.hpp"
  29 #include "logging/log.hpp"
  30 #include "runtime/globals.hpp"
  31 #include "utilities/debug.hpp"
  32 
  33 // The backing file 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 chunked is mapped to
  36 // a separate paging file mapping.
  37 
  38 ZBackingFile::ZBackingFile() :
  39     _handles(MaxHeapSize),
  40     _size(0) {}
  41 
  42 bool ZBackingFile::is_initialized() const {
  43   return true;
  44 }
  45 
  46 void ZBackingFile::warn_commit_limits(size_t max) const {
  47   // Does nothing
  48 }
  49 
  50 size_t ZBackingFile::size() const {
  51   return _size;
  52 }
  53 
  54 HANDLE ZBackingFile::get_handle(uintptr_t offset) const {
  55   HANDLE const handle = _handles.get(offset);
  56   assert(handle != 0, "Should be set");
  57   return handle;
  58 }
  59 
  60 void ZBackingFile::put_handle(uintptr_t offset, HANDLE handle) {
  61   assert(handle != INVALID_HANDLE_VALUE, "Invalid handle");
  62   assert(_handles.get(offset) == 0, "Should be cleared");
  63   _handles.put(offset, handle);
  64 }
  65 
  66 void ZBackingFile::clear_handle(uintptr_t offset) {
  67   assert(_handles.get(offset) != 0, "Should be set");
  68   _handles.put(offset, 0);
  69 }
  70 
  71 size_t ZBackingFile::commit_from_paging_file(size_t offset, size_t size) {
  72   for (size_t i = 0; i < size; i += ZGranuleSize) {
  73     HANDLE const handle = ZMapper::create_and_commit_paging_file_mapping(ZGranuleSize);
  74     if (handle == 0) {
  75       return i;
  76     }
  77 
  78     put_handle(offset + i, handle);
  79   }
  80 
  81   return size;
  82 }
  83 
  84 size_t ZBackingFile::uncommit_from_paging_file(size_t offset, size_t size) {
  85   for (size_t i = 0; i < size; i += ZGranuleSize) {
  86     HANDLE const handle = get_handle(offset + i);
  87     clear_handle(offset + i);
  88     ZMapper::close_paging_file_mapping(handle);
  89   }
  90 
  91   return size;
  92 }
  93 
  94 size_t ZBackingFile::commit(size_t offset, size_t length) {
  95   log_trace(gc, heap)("Committing memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
  96                       offset / M, (offset + length) / M, length / M);
  97 
  98   const size_t committed = commit_from_paging_file(offset, length);
  99 
 100   const size_t end = offset + committed;
 101   if (end > _size) {
 102     // Update size
 103     _size = end;
 104   }
 105 
 106   return committed;
 107 }
 108 
 109 size_t ZBackingFile::uncommit(size_t offset, size_t length) {
 110   log_trace(gc, heap)("Uncommitting memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
 111                       offset / M, (offset + length) / M, length / M);
 112 
 113   return uncommit_from_paging_file(offset, length);
 114 }
 115 
 116 void ZBackingFile::map(uintptr_t addr, size_t size, size_t offset) const {
 117   assert(is_aligned(offset, ZGranuleSize), "Misaligned");
 118   assert(is_aligned(addr, ZGranuleSize), "Misaligned");
 119   assert(is_aligned(size, ZGranuleSize), "Misaligned");
 120 
 121   for (size_t i = 0; i < size; i += ZGranuleSize) {
 122     HANDLE const handle = get_handle(offset + i);
 123     ZMapper::map_view_replace_placeholder(handle, 0 /* offset */, addr + i, ZGranuleSize);
 124   }
 125 }
 126 
 127 void ZBackingFile::unmap(uintptr_t addr, size_t size) const {
 128   assert(is_aligned(addr, ZGranuleSize), "Misaligned");
 129   assert(is_aligned(size, ZGranuleSize), "Misaligned");
 130 
 131   for (size_t i = 0; i < size; i += ZGranuleSize) {
 132     ZMapper::unmap_view_preserve_placeholder(addr + i, ZGranuleSize);
 133   }
 134 }