|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, 2025, 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 "gc/z/zAddress.inline.hpp" |
| 25 | +#include "gc/z/zGlobals.hpp" |
| 26 | +#include "gc/z/zLargePages.inline.hpp" |
| 27 | +#include "gc/z/zMapper_windows.hpp" |
| 28 | +#include "gc/z/zSyscall_windows.hpp" |
| 29 | +#include "gc/z/zValue.inline.hpp" |
| 30 | +#include "gc/z/zMemory.inline.hpp" |
| 31 | +#include "utilities/align.hpp" |
| 32 | +#include "utilities/debug.hpp" |
| 33 | + |
| 34 | +class ZVirtualMemoryManagerImpl : public CHeapObj<mtGC> { |
| 35 | +public: |
| 36 | + virtual void initialize_before_reserve() {} |
| 37 | + virtual void initialize_after_reserve(ZMemoryManager* manager) {} |
| 38 | + virtual bool reserve(zaddress_unsafe addr, size_t size) = 0; |
| 39 | + virtual void unreserve(zaddress_unsafe addr, size_t size) = 0; |
| 40 | +}; |
| 41 | + |
| 42 | +// Implements small pages (paged) support using placeholder reservation. |
| 43 | +// |
| 44 | +// When a memory area is free (kept by the virtual memory manager) a |
| 45 | +// single placeholder is covering that memory area. When memory is |
| 46 | +// allocated from the manager the placeholder is split into granule |
| 47 | +// sized placeholders to allow mapping operations on that granularity. |
| 48 | +class ZVirtualMemoryManagerSmallPages : public ZVirtualMemoryManagerImpl { |
| 49 | +private: |
| 50 | + class PlaceholderCallbacks : public AllStatic { |
| 51 | + public: |
| 52 | + static void split_placeholder(zoffset start, size_t size) { |
| 53 | + ZMapper::split_placeholder(ZOffset::address_unsafe(start), size); |
| 54 | + } |
| 55 | + |
| 56 | + static void coalesce_placeholders(zoffset start, size_t size) { |
| 57 | + ZMapper::coalesce_placeholders(ZOffset::address_unsafe(start), size); |
| 58 | + } |
| 59 | + |
| 60 | + // Turn the single placeholder covering the memory area into granule |
| 61 | + // sized placeholders. |
| 62 | + static void split_into_granule_sized_placeholders(zoffset start, size_t size) { |
| 63 | + assert(size >= ZGranuleSize, "Must be at least one granule"); |
| 64 | + assert(is_aligned(size, ZGranuleSize), "Must be granule aligned"); |
| 65 | + |
| 66 | + // Don't call split_placeholder on the last granule, since it is already |
| 67 | + // a placeholder and the system call would therefore fail. |
| 68 | + const size_t limit = size - ZGranuleSize; |
| 69 | + for (size_t offset = 0; offset < limit; offset += ZGranuleSize) { |
| 70 | + split_placeholder(start + offset, ZGranuleSize); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + static void coalesce_into_one_placeholder(zoffset start, size_t size) { |
| 75 | + assert(is_aligned(size, ZGranuleSize), "Must be granule aligned"); |
| 76 | + |
| 77 | + // Granule sized areas are already covered by a single placeholder |
| 78 | + if (size > ZGranuleSize) { |
| 79 | + coalesce_placeholders(start, size); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Called when a memory area is returned to the memory manager but can't |
| 84 | + // be merged with an already existing area. Make sure this area is covered |
| 85 | + // by a single placeholder. |
| 86 | + static void create_callback(const ZMemoryRange& range) { |
| 87 | + assert(is_aligned(range.size(), ZGranuleSize), "Must be granule aligned"); |
| 88 | + |
| 89 | + coalesce_into_one_placeholder(range.start(), range.size()); |
| 90 | + } |
| 91 | + |
| 92 | + // Called when a complete memory area in the memory manager is allocated. |
| 93 | + // Create granule sized placeholders for the entire area. |
| 94 | + static void destroy_callback(const ZMemoryRange& range) { |
| 95 | + assert(is_aligned(range.size(), ZGranuleSize), "Must be granule aligned"); |
| 96 | + |
| 97 | + split_into_granule_sized_placeholders(range.start(), range.size()); |
| 98 | + } |
| 99 | + |
| 100 | + // Called when a memory area is allocated at the front of an exising memory area. |
| 101 | + // Turn the first part of the memory area into granule sized placeholders. |
| 102 | + static void shrink_from_front_callback(const ZMemoryRange& range, size_t size) { |
| 103 | + assert(range.size() > size, "Must be larger than what we try to split out"); |
| 104 | + assert(is_aligned(size, ZGranuleSize), "Must be granule aligned"); |
| 105 | + |
| 106 | + // Split the area into two placeholders |
| 107 | + split_placeholder(range.start(), size); |
| 108 | + |
| 109 | + // Split the first part into granule sized placeholders |
| 110 | + split_into_granule_sized_placeholders(range.start(), size); |
| 111 | + } |
| 112 | + |
| 113 | + // Called when a memory area is allocated at the end of an existing memory area. |
| 114 | + // Turn the second part of the memory area into granule sized placeholders. |
| 115 | + static void shrink_from_back_callback(const ZMemoryRange& range, size_t size) { |
| 116 | + assert(range.size() > size, "Must be larger than what we try to split out"); |
| 117 | + assert(is_aligned(size, ZGranuleSize), "Must be granule aligned"); |
| 118 | + |
| 119 | + // Split the area into two placeholders |
| 120 | + const zoffset start = to_zoffset(range.end() - size); |
| 121 | + split_placeholder(start, size); |
| 122 | + |
| 123 | + // Split the second part into granule sized placeholders |
| 124 | + split_into_granule_sized_placeholders(start, size); |
| 125 | + } |
| 126 | + |
| 127 | + // Called when freeing a memory area and it can be merged at the start of an |
| 128 | + // existing area. Coalesce the underlying placeholders into one. |
| 129 | + static void grow_from_front_callback(const ZMemoryRange& range, size_t size) { |
| 130 | + assert(is_aligned(range.size(), ZGranuleSize), "Must be granule aligned"); |
| 131 | + |
| 132 | + const zoffset start = range.start() - size; |
| 133 | + coalesce_into_one_placeholder(start, range.size() + size); |
| 134 | + } |
| 135 | + |
| 136 | + // Called when freeing a memory area and it can be merged at the end of an |
| 137 | + // existing area. Coalesce the underlying placeholders into one. |
| 138 | + static void grow_from_back_callback(const ZMemoryRange& range, size_t size) { |
| 139 | + assert(is_aligned(range.size(), ZGranuleSize), "Must be granule aligned"); |
| 140 | + |
| 141 | + coalesce_into_one_placeholder(range.start(), range.size() + size); |
| 142 | + } |
| 143 | + |
| 144 | + static void register_with(ZMemoryManager* manager) { |
| 145 | + // Each reserved virtual memory address area registered in _manager is |
| 146 | + // exactly covered by a single placeholder. Callbacks are installed so |
| 147 | + // that whenever a memory area changes, the corresponding placeholder |
| 148 | + // is adjusted. |
| 149 | + // |
| 150 | + // The create and grow callbacks are called when virtual memory is |
| 151 | + // returned to the memory manager. The new memory area is then covered |
| 152 | + // by a new single placeholder. |
| 153 | + // |
| 154 | + // The destroy and shrink callbacks are called when virtual memory is |
| 155 | + // allocated from the memory manager. The memory area is then is split |
| 156 | + // into granule-sized placeholders. |
| 157 | + // |
| 158 | + // See comment in zMapper_windows.cpp explaining why placeholders are |
| 159 | + // split into ZGranuleSize sized placeholders. |
| 160 | + |
| 161 | + ZMemoryManager::Callbacks callbacks; |
| 162 | + |
| 163 | + callbacks._create = &create_callback; |
| 164 | + callbacks._destroy = &destroy_callback; |
| 165 | + callbacks._shrink_from_front = &shrink_from_front_callback; |
| 166 | + callbacks._shrink_from_back = &shrink_from_back_callback; |
| 167 | + callbacks._grow_from_front = &grow_from_front_callback; |
| 168 | + callbacks._grow_from_back = &grow_from_back_callback; |
| 169 | + |
| 170 | + manager->register_callbacks(callbacks); |
| 171 | + } |
| 172 | + }; |
| 173 | + |
| 174 | + virtual void initialize_after_reserve(ZMemoryManager* manager) { |
| 175 | + PlaceholderCallbacks::register_with(manager); |
| 176 | + } |
| 177 | + |
| 178 | + virtual bool reserve(zaddress_unsafe addr, size_t size) { |
| 179 | + const zaddress_unsafe res = ZMapper::reserve(addr, size); |
| 180 | + |
| 181 | + assert(res == addr || untype(res) == 0, "Should not reserve other memory than requested"); |
| 182 | + return res == addr; |
| 183 | + } |
| 184 | + |
| 185 | + virtual void unreserve(zaddress_unsafe addr, size_t size) { |
| 186 | + ZMapper::unreserve(addr, size); |
| 187 | + } |
| 188 | +}; |
| 189 | + |
| 190 | +// Implements Large Pages (locked) support using shared AWE physical memory. |
| 191 | + |
| 192 | +// ZPhysicalMemory layer needs access to the section |
| 193 | +HANDLE ZAWESection; |
| 194 | + |
| 195 | +class ZVirtualMemoryManagerLargePages : public ZVirtualMemoryManagerImpl { |
| 196 | +private: |
| 197 | + virtual void initialize_before_reserve() { |
| 198 | + ZAWESection = ZMapper::create_shared_awe_section(); |
| 199 | + } |
| 200 | + |
| 201 | + virtual bool reserve(zaddress_unsafe addr, size_t size) { |
| 202 | + const zaddress_unsafe res = ZMapper::reserve_for_shared_awe(ZAWESection, addr, size); |
| 203 | + |
| 204 | + assert(res == addr || untype(res) == 0, "Should not reserve other memory than requested"); |
| 205 | + return res == addr; |
| 206 | + } |
| 207 | + |
| 208 | + virtual void unreserve(zaddress_unsafe addr, size_t size) { |
| 209 | + ZMapper::unreserve_for_shared_awe(addr, size); |
| 210 | + } |
| 211 | +}; |
| 212 | + |
| 213 | +static ZVirtualMemoryManagerImpl* _impl = nullptr; |
| 214 | + |
| 215 | +void ZVirtualMemoryManager::pd_initialize_before_reserve() { |
| 216 | + if (ZLargePages::is_enabled()) { |
| 217 | + _impl = new ZVirtualMemoryManagerLargePages(); |
| 218 | + } else { |
| 219 | + _impl = new ZVirtualMemoryManagerSmallPages(); |
| 220 | + } |
| 221 | + _impl->initialize_before_reserve(); |
| 222 | +} |
| 223 | + |
| 224 | +void ZVirtualMemoryManager::pd_initialize_after_reserve() { |
| 225 | + _impl->initialize_after_reserve(_managers.addr(0)); |
| 226 | +} |
| 227 | + |
| 228 | +bool ZVirtualMemoryManager::pd_reserve(zaddress_unsafe addr, size_t size) { |
| 229 | + return _impl->reserve(addr, size); |
| 230 | +} |
| 231 | + |
| 232 | +void ZVirtualMemoryManager::pd_unreserve(zaddress_unsafe addr, size_t size) { |
| 233 | + _impl->unreserve(addr, size); |
| 234 | +} |
0 commit comments