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