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 "memory/metaspace.hpp" 28 #include "oops/compressedKlass.inline.hpp" 29 #include "runtime/globals.hpp" 30 #include "runtime/java.hpp" 31 #include "runtime/os.hpp" 32 #include "utilities/debug.hpp" 33 #include "utilities/globalDefinitions.hpp" 34 #include "utilities/ostream.hpp" 35 36 int CompressedKlassPointers::_narrow_klass_pointer_bits = -1; 37 int CompressedKlassPointers::_max_shift = -1; 38 39 address CompressedKlassPointers::_base = (address)-1; 40 int CompressedKlassPointers::_shift = -1; 41 address CompressedKlassPointers::_klass_range_start = nullptr; 42 address CompressedKlassPointers::_klass_range_end = nullptr; 43 narrowKlass CompressedKlassPointers::_lowest_valid_narrow_klass_id = (narrowKlass)-1; 44 narrowKlass CompressedKlassPointers::_highest_valid_narrow_klass_id = (narrowKlass)-1; 45 46 #ifdef _LP64 47 48 size_t CompressedKlassPointers::max_klass_range_size() { 49 // We disallow klass range sizes larger than 4GB even if the encoding 50 // range would allow for a larger Klass range (e.g. Base=zero, shift=3 -> 32GB). 51 // That is because many CPU-specific compiler decodings do not want the 52 // shifted narrow Klass to spill over into the third quadrant of the 64-bit target 53 // address, e.g. to use a 16-bit move for a simplified base addition. 54 return MIN2(4 * G, max_encoding_range_size()); 55 } 56 57 void CompressedKlassPointers::pre_initialize() { 58 if (UseCompactObjectHeaders) { 59 _narrow_klass_pointer_bits = narrow_klass_pointer_bits_coh; 60 _max_shift = max_shift_coh; 61 } else { 62 _narrow_klass_pointer_bits = narrow_klass_pointer_bits_noncoh; 63 _max_shift = max_shift_noncoh; 64 } 65 } 66 67 #ifdef ASSERT 68 void CompressedKlassPointers::sanity_check_after_initialization() { 69 // In expectation of an assert, prepare condensed info to be printed with the assert. 70 char tmp[256]; 71 os::snprintf(tmp, sizeof(tmp), "klass range: " RANGE2FMT "," 72 " base " PTR_FORMAT ", shift %d, lowest/highest valid narrowKlass %u/%u", 73 RANGE2FMTARGS(_klass_range_start, _klass_range_end), 74 p2i(_base), _shift, _lowest_valid_narrow_klass_id, _highest_valid_narrow_klass_id); 75 #define ASSERT_HERE(cond) assert(cond, " (%s)", tmp); 76 #define ASSERT_HERE_2(cond, msg) assert(cond, msg " (%s)", tmp); 77 78 // There is no technical reason preventing us from using other klass pointer bit lengths, 79 // but it should be a deliberate choice 80 ASSERT_HERE(_narrow_klass_pointer_bits == 32 || _narrow_klass_pointer_bits == 19); 81 82 // All values must be inited 83 ASSERT_HERE(_max_shift != -1); 84 ASSERT_HERE(_klass_range_start != (address)-1); 85 ASSERT_HERE(_klass_range_end != (address)-1); 86 ASSERT_HERE(_lowest_valid_narrow_klass_id != (narrowKlass)-1); 87 ASSERT_HERE(_base != (address)-1); 88 ASSERT_HERE(_shift != -1); 89 90 const size_t klass_align = klass_alignment_in_bytes(); 91 92 // must be aligned enough hold 64-bit data 93 ASSERT_HERE(is_aligned(klass_align, sizeof(uint64_t))); 94 95 // should be smaller than the minimum metaspace chunk size (soft requirement) 96 ASSERT_HERE(klass_align <= K); 97 98 ASSERT_HERE(_klass_range_end > _klass_range_start); 99 100 // Check that Klass range is fully engulfed in the encoding range 101 const address encoding_start = _base; 102 const address encoding_end = _base + nth_bit(narrow_klass_pointer_bits() + _shift); 103 ASSERT_HERE_2(_klass_range_start >= _base && _klass_range_end <= encoding_end, 104 "Resulting encoding range does not fully cover the class range"); 105 106 // Check that Klass range is aligned to Klass alignment. Note that this should never be 107 // an issue since the Klass range is handed in by either CDS- or Metaspace-initialization, and 108 // it should be the result of an mmap operation that operates on page sizes. So as long as 109 // the Klass alignment is <= page size, we are fine. 110 ASSERT_HERE_2(is_aligned(_klass_range_start, klass_align) && 111 is_aligned(_klass_range_end, klass_align), 112 "Klass range must start and end at a properly aligned address"); 113 114 // Check _lowest_valid_narrow_klass_id and _highest_valid_narrow_klass_id 115 ASSERT_HERE_2(_lowest_valid_narrow_klass_id > 0, "Null is not a valid narrowKlass"); 116 ASSERT_HERE(_highest_valid_narrow_klass_id > _lowest_valid_narrow_klass_id); 117 118 Klass* const k1 = decode_not_null_without_asserts(_lowest_valid_narrow_klass_id, _base, _shift); 119 if (encoding_start == _klass_range_start) { 120 ASSERT_HERE_2((address)k1 == _klass_range_start + klass_align, "Not lowest"); 121 } else { 122 ASSERT_HERE_2((address)k1 == _klass_range_start, "Not lowest"); 123 } 124 narrowKlass nk1 = encode_not_null_without_asserts(k1, _base, _shift); 125 ASSERT_HERE_2(nk1 == _lowest_valid_narrow_klass_id, "not reversible"); 126 127 Klass* const k2 = decode_not_null_without_asserts(_highest_valid_narrow_klass_id, _base, _shift); 128 ASSERT_HERE((address)k2 == _klass_range_end - klass_align); 129 narrowKlass nk2 = encode_not_null_without_asserts(k2, _base, _shift); 130 ASSERT_HERE_2(nk2 == _highest_valid_narrow_klass_id, "not reversible"); 131 132 #ifdef AARCH64 133 // On aarch64, we never expect a shift value > 0 in standard (non-coh) mode 134 ASSERT_HERE_2(UseCompactObjectHeaders || _shift == 0, "Shift > 0 in non-coh mode?"); 135 #endif 136 #undef ASSERT_HERE 137 #undef ASSERT_HERE_2 138 } 139 #endif // ASSERT 140 141 // Helper function: given current Klass Range, Base and Shift, calculate the lowest and highest values 142 // of narrowKlass we can expect. 143 void CompressedKlassPointers::calc_lowest_highest_narrow_klass_id() { 144 address lowest_possible_klass_location = _klass_range_start; 145 146 // A Klass will never be placed at the Encoding range start, since that would translate to a narrowKlass=0, which 147 // is disallowed. Note that both Metaspace and CDS prvent allocation at the first address for this reason. 148 if (lowest_possible_klass_location == _base) { 149 lowest_possible_klass_location += klass_alignment_in_bytes(); 150 } 151 _lowest_valid_narrow_klass_id = (narrowKlass) ((uintptr_t)(lowest_possible_klass_location - _base) >> _shift); 152 153 address highest_possible_klass_location = _klass_range_end - klass_alignment_in_bytes(); 154 _highest_valid_narrow_klass_id = (narrowKlass) ((uintptr_t)(highest_possible_klass_location - _base) >> _shift); 155 } 156 157 // Given a klass range [addr, addr+len) and a given encoding scheme, assert that this scheme covers the range, then 158 // set this encoding scheme. Used by CDS at runtime to re-instate the scheme used to pre-compute klass ids for 159 // archived heap objects. 160 void CompressedKlassPointers::initialize_for_given_encoding(address addr, size_t len, address requested_base, int requested_shift) { 161 if (len > max_klass_range_size()) { 162 stringStream ss; 163 ss.print("Class space size and CDS archive size combined (%zu) " 164 "exceed the maximum possible size (%zu)", 165 len, max_klass_range_size()); 166 vm_exit_during_initialization(ss.base()); 167 } 168 169 // Note: While it would be technically valid for the encoding base to precede the start of the Klass range, 170 // we never do this here. This is used at CDS runtime to re-instate the scheme used to precompute the 171 // narrow Klass IDs in the archive, and the requested base should point to the start of the Klass range. 172 assert(requested_base == addr, "Invalid requested base"); 173 174 // Remember Klass range: 175 _klass_range_start = addr; 176 _klass_range_end = addr + len; 177 178 _base = requested_base; 179 _shift = requested_shift; 180 181 calc_lowest_highest_narrow_klass_id(); 182 183 DEBUG_ONLY(sanity_check_after_initialization();) 184 } 185 186 char* CompressedKlassPointers::reserve_address_space_X(uintptr_t from, uintptr_t to, size_t size, size_t alignment, bool aslr) { 187 alignment = MAX2(Metaspace::reserve_alignment(), alignment); 188 return os::attempt_reserve_memory_between((char*)from, (char*)to, size, alignment, aslr); 189 } 190 191 char* CompressedKlassPointers::reserve_address_space_for_unscaled_encoding(size_t size, bool aslr) { 192 const size_t unscaled_max = nth_bit(narrow_klass_pointer_bits()); 193 return reserve_address_space_X(0, unscaled_max, size, Metaspace::reserve_alignment(), aslr); 194 } 195 196 char* CompressedKlassPointers::reserve_address_space_for_zerobased_encoding(size_t size, bool aslr) { 197 const size_t unscaled_max = nth_bit(narrow_klass_pointer_bits()); 198 const size_t zerobased_max = nth_bit(narrow_klass_pointer_bits() + max_shift()); 199 return reserve_address_space_X(unscaled_max, zerobased_max, size, Metaspace::reserve_alignment(), aslr); 200 } 201 202 char* CompressedKlassPointers::reserve_address_space_for_16bit_move(size_t size, bool aslr) { 203 return reserve_address_space_X(nth_bit(32), nth_bit(48), size, nth_bit(32), aslr); 204 } 205 206 void CompressedKlassPointers::initialize(address addr, size_t len) { 207 208 if (len > max_klass_range_size()) { 209 stringStream ss; 210 ss.print("Class space size (%zu) exceeds the maximum possible size (%zu)", 211 len, max_klass_range_size()); 212 vm_exit_during_initialization(ss.base()); 213 } 214 215 // Remember the Klass range: 216 _klass_range_start = addr; 217 _klass_range_end = addr + len; 218 219 // Calculate Base and Shift: 220 221 if (UseCompactObjectHeaders) { 222 223 // This handles the case that we - experimentally - reduce the number of 224 // class pointer bits further, such that (shift + num bits) < 32. 225 assert(len <= (size_t)nth_bit(narrow_klass_pointer_bits() + max_shift()), 226 "klass range size exceeds encoding, len: " SIZE_FORMAT ", narrow_klass_pointer_bits: %d, max_shift: %d", len, narrow_klass_pointer_bits(), max_shift()); 227 228 // In compact object header mode, with 19-bit narrowKlass, we don't attempt for 229 // zero-based mode. Instead, we set the base to the start of the klass range and 230 // then try for the smallest shift possible that still covers the whole range. 231 // The reason is that we want to avoid, if possible, shifts larger than 232 // a cacheline size. 233 _base = addr; 234 235 const int log_cacheline = exact_log2(DEFAULT_CACHE_LINE_SIZE); 236 int s = max_shift(); 237 while (s > log_cacheline && ((size_t)nth_bit(narrow_klass_pointer_bits() + s - 1) > len)) { 238 s--; 239 } 240 _shift = s; 241 242 } else { 243 244 // Traditional (non-compact) header mode 245 const uintptr_t unscaled_max = nth_bit(narrow_klass_pointer_bits()); 246 const uintptr_t zerobased_max = nth_bit(narrow_klass_pointer_bits() + max_shift()); 247 248 #ifdef AARCH64 249 // Aarch64 avoids zero-base shifted mode (_base=0 _shift>0), instead prefers 250 // non-zero-based mode with a zero shift. 251 _shift = 0; 252 address const end = addr + len; 253 _base = (end <= (address)unscaled_max) ? nullptr : addr; 254 #else 255 // We try, in order of preference: 256 // -unscaled (base=0 shift=0) 257 // -zero-based (base=0 shift>0) 258 // -nonzero-base (base>0 shift=0) 259 // Note that base>0 shift>0 should never be needed, since the klass range will 260 // never exceed 4GB. 261 address const end = addr + len; 262 if (end <= (address)unscaled_max) { 263 _base = nullptr; 264 _shift = 0; 265 } else { 266 if (end <= (address)zerobased_max) { 267 _base = nullptr; 268 _shift = max_shift(); 269 } else { 270 _base = addr; 271 _shift = 0; 272 } 273 } 274 #endif // AARCH64 275 } 276 277 calc_lowest_highest_narrow_klass_id(); 278 279 #ifdef ASSERT 280 sanity_check_after_initialization(); 281 #endif 282 } 283 284 void CompressedKlassPointers::print_mode(outputStream* st) { 285 st->print_cr("UseCompressedClassPointers %d, UseCompactObjectHeaders %d", 286 UseCompressedClassPointers, UseCompactObjectHeaders); 287 if (UseCompressedClassPointers) { 288 st->print_cr("Narrow klass pointer bits %d, Max shift %d", 289 _narrow_klass_pointer_bits, _max_shift); 290 st->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: %d", 291 p2i(base()), shift()); 292 st->print_cr("Encoding Range: " RANGE2FMT, RANGE2FMTARGS(_base, encoding_range_end())); 293 st->print_cr("Klass Range: " RANGE2FMT, RANGE2FMTARGS(_klass_range_start, _klass_range_end)); 294 st->print_cr("Klass ID Range: [%u - %u) (%u)", _lowest_valid_narrow_klass_id, _highest_valid_narrow_klass_id + 1, 295 _highest_valid_narrow_klass_id + 1 - _lowest_valid_narrow_klass_id); 296 } else { 297 st->print_cr("UseCompressedClassPointers off"); 298 } 299 } 300 301 #endif // _LP64