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