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 size_t ZBackingFile::size() const {
  43   return _size;
  44 }
  45 
  46 HANDLE ZBackingFile::get_handle(uintptr_t offset) const {
  47   HANDLE const handle = _handles.get(offset);
  48   assert(handle != 0, "Should be set");
  49   return handle;
  50 }
  51 
  52 void ZBackingFile::put_handle(uintptr_t offset, HANDLE handle) {
  53   assert(handle != INVALID_HANDLE_VALUE, "Invalid handle");
  54   assert(_handles.get(offset) == 0, "Should be cleared");
  55   _handles.put(offset, handle);
  56 }
  57 
  58 void ZBackingFile::clear_handle(uintptr_t offset) {
  59   assert(_handles.get(offset) != 0, "Should be set");
  60   _handles.put(offset, 0);
  61 }
  62 
  63 size_t ZBackingFile::commit_from_paging_file(size_t offset, size_t size) {
  64   for (size_t i = 0; i < size; i += ZGranuleSize) {
  65     HANDLE const handle = ZMapper::create_and_commit_paging_file_mapping(ZGranuleSize);
  66     if (handle == 0) {
  67       return i;
  68     }
  69 
  70     put_handle(offset + i, handle);
  71   }
  72 
  73   return size;
  74 }
  75 
  76 size_t ZBackingFile::uncommit_from_paging_file(size_t offset, size_t size) {
  77   for (size_t i = 0; i < size; i += ZGranuleSize) {
  78     HANDLE const handle = get_handle(offset + i);
  79     clear_handle(offset + i);
  80     ZMapper::close_paging_file_mapping(handle);
  81   }
  82 
  83   return size;
  84 }
  85 
  86 size_t ZBackingFile::commit(size_t offset, size_t length) {
  87   log_trace(gc, heap)("Committing memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
  88                       offset / M, (offset + length) / M, length / M);
  89 
  90   const size_t committed = commit_from_paging_file(offset, length);
  91 
  92   const size_t end = offset + committed;
  93   if (end > _size) {
  94     // Update size
  95     _size = end;
  96   }
  97 
  98   return committed;
  99 }
 100 
 101 size_t ZBackingFile::uncommit(size_t offset, size_t length) {
 102   log_trace(gc, heap)("Uncommitting memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
 103                       offset / M, (offset + length) / M, length / M);
 104 
 105   return uncommit_from_paging_file(offset, length);
 106 }
 107 
 108 void ZBackingFile::map(uintptr_t addr, size_t size, size_t offset) const {
 109   assert(is_aligned(offset, ZGranuleSize), "Misaligned");
 110   assert(is_aligned(addr, ZGranuleSize), "Misaligned");
 111   assert(is_aligned(size, ZGranuleSize), "Misaligned");
 112 
 113   for (size_t i = 0; i < size; i += ZGranuleSize) {
 114     HANDLE const handle = get_handle(offset + i);
 115     ZMapper::map_view_replace_placeholder(handle, 0 /* offset */, addr + i, ZGranuleSize);
 116   }
 117 }
 118 
 119 void ZBackingFile::unmap(uintptr_t addr, size_t size) const {
 120   assert(is_aligned(addr, ZGranuleSize), "Misaligned");
 121   assert(is_aligned(size, ZGranuleSize), "Misaligned");
 122 
 123   for (size_t i = 0; i < size; i += ZGranuleSize) {
 124     ZMapper::unmap_view_preserve_placeholder(addr + i, ZGranuleSize);
 125   }
 126 }