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