1 /* 2 * Copyright (c) 2019, 2024, 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 25 #include "precompiled.hpp" 26 #include "logging/log.hpp" 27 #include "logging/logStream.hpp" 28 #include "memory/memRegion.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "memory/universe.hpp" 31 #include "memory/virtualspace.hpp" 32 #include "oops/compressedOops.hpp" 33 #include "gc/shared/collectedHeap.hpp" 34 #include "runtime/arguments.hpp" 35 #include "runtime/globals.hpp" 36 37 // For UseCompressedOops. 38 address CompressedOops::_base = nullptr; 39 int CompressedOops::_shift = 0; 40 bool CompressedOops::_use_implicit_null_checks = true; 41 MemRegion CompressedOops::_heap_address_range; 42 43 // Choose the heap base address and oop encoding mode 44 // when compressed oops are used: 45 // Unscaled - Use 32-bits oops without encoding when 46 // NarrowOopHeapBaseMin + heap_size < 4Gb 47 // ZeroBased - Use zero based compressed oops with encoding when 48 // NarrowOopHeapBaseMin + heap_size < 32Gb 49 // HeapBased - Use compressed oops with heap base + encoding. 50 void CompressedOops::initialize(const ReservedHeapSpace& heap_space) { 51 #ifdef _LP64 52 // Subtract a page because something can get allocated at heap base. 53 // This also makes implicit null checking work, because the 54 // memory+1 page below heap_base needs to cause a signal. 55 // See needs_explicit_null_check. 56 // Only set the heap base for compressed oops because it indicates 57 // compressed oops for pstack code. 58 if ((uint64_t)heap_space.end() > UnscaledOopHeapMax) { 59 // Didn't reserve heap below 4Gb. Must shift. 60 set_shift(LogMinObjAlignmentInBytes); 61 } 62 if ((uint64_t)heap_space.end() <= OopEncodingHeapMax) { 63 // Did reserve heap below 32Gb. Can use base == 0; 64 set_base(nullptr); 65 } else { 66 set_base((address)heap_space.compressed_oop_base()); 67 } 68 69 _heap_address_range = heap_space.region(); 70 71 LogTarget(Debug, gc, heap, coops) lt; 72 if (lt.is_enabled()) { 73 ResourceMark rm; 74 LogStream ls(lt); 75 print_mode(&ls); 76 } 77 78 // Tell tests in which mode we run. 79 Arguments::PropertyList_add(new SystemProperty("java.vm.compressedOopsMode", 80 mode_to_string(mode()), 81 false)); 82 83 // base() is one page below the heap. 84 assert((intptr_t)base() <= ((intptr_t)_heap_address_range.start() - (intptr_t)os::vm_page_size()) || 85 base() == nullptr, "invalid value"); 86 assert(shift() == LogMinObjAlignmentInBytes || 87 shift() == 0, "invalid value"); 88 #endif 89 } 90 91 void CompressedOops::set_base(address base) { 92 assert(UseCompressedOops, "no compressed oops?"); 93 _base = base; 94 } 95 96 void CompressedOops::set_shift(int shift) { 97 _shift = shift; 98 } 99 100 void CompressedOops::set_use_implicit_null_checks(bool use) { 101 assert(UseCompressedOops, "no compressed ptrs?"); 102 _use_implicit_null_checks = use; 103 } 104 105 bool CompressedOops::is_in(void* addr) { 106 return _heap_address_range.contains(addr); 107 } 108 109 bool CompressedOops::is_in(MemRegion mr) { 110 return _heap_address_range.contains(mr); 111 } 112 113 CompressedOops::Mode CompressedOops::mode() { 114 if (base_disjoint()) { 115 return DisjointBaseNarrowOop; 116 } 117 118 if (base() != nullptr) { 119 return HeapBasedNarrowOop; 120 } 121 122 if (shift() != 0) { 123 return ZeroBasedNarrowOop; 124 } 125 126 return UnscaledNarrowOop; 127 } 128 129 const char* CompressedOops::mode_to_string(Mode mode) { 130 switch (mode) { 131 case UnscaledNarrowOop: 132 return "32-bit"; 133 case ZeroBasedNarrowOop: 134 return "Zero based"; 135 case DisjointBaseNarrowOop: 136 return "Non-zero disjoint base"; 137 case HeapBasedNarrowOop: 138 return "Non-zero based"; 139 default: 140 ShouldNotReachHere(); 141 return ""; 142 } 143 } 144 145 // Test whether bits of addr and possible offsets into the heap overlap. 146 bool CompressedOops::is_disjoint_heap_base_address(address addr) { 147 return (((uint64_t)(intptr_t)addr) & 148 (((uint64_t)UCONST64(0xFFFFffffFFFFffff)) >> (32-LogMinObjAlignmentInBytes))) == 0; 149 } 150 151 // Check for disjoint base compressed oops. 152 bool CompressedOops::base_disjoint() { 153 return _base != nullptr && is_disjoint_heap_base_address(_base); 154 } 155 156 // Check for real heapbased compressed oops. 157 // We must subtract the base as the bits overlap. 158 // If we negate above function, we also get unscaled and zerobased. 159 bool CompressedOops::base_overlaps() { 160 return _base != nullptr && !is_disjoint_heap_base_address(_base); 161 } 162 163 void CompressedOops::print_mode(outputStream* st) { 164 st->print("Heap address: " PTR_FORMAT ", size: " SIZE_FORMAT " MB", 165 p2i(_heap_address_range.start()), _heap_address_range.byte_size()/M); 166 167 st->print(", Compressed Oops mode: %s", mode_to_string(mode())); 168 169 if (base() != nullptr) { 170 st->print(": " PTR_FORMAT, p2i(base())); 171 } 172 173 if (shift() != 0) { 174 st->print(", Oop shift amount: %d", shift()); 175 } 176 177 if (!use_implicit_null_checks()) { 178 st->print(", no protected page in front of the heap"); 179 } 180 st->cr(); 181 }