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 // All values must be inited 79 ASSERT_HERE(_max_shift != -1); 80 ASSERT_HERE(_klass_range_start != (address)-1); 81 ASSERT_HERE(_klass_range_end != (address)-1); 82 ASSERT_HERE(_lowest_valid_narrow_klass_id != (narrowKlass)-1); 83 ASSERT_HERE(_base != (address)-1); 84 ASSERT_HERE(_shift != -1); 85 86 const size_t klass_align = klass_alignment_in_bytes(); 87 88 // must be aligned enough hold 64-bit data 89 ASSERT_HERE(is_aligned(klass_align, sizeof(uint64_t))); 90 91 // should be smaller than the minimum metaspace chunk size (soft requirement) 92 ASSERT_HERE(klass_align <= K); 93 94 ASSERT_HERE(_klass_range_end > _klass_range_start); 95 96 // Check that Klass range is fully engulfed in the encoding range 97 const address encoding_start = _base; 98 const address encoding_end = _base + nth_bit(narrow_klass_pointer_bits() + _shift); 99 ASSERT_HERE_2(_klass_range_start >= _base && _klass_range_end <= encoding_end, 100 "Resulting encoding range does not fully cover the class range"); 101 102 // Check that Klass range is aligned to Klass alignment. Note that this should never be 103 // an issue since the Klass range is handed in by either CDS- or Metaspace-initialization, and 104 // it should be the result of an mmap operation that operates on page sizes. So as long as 105 // the Klass alignment is <= page size, we are fine. 106 ASSERT_HERE_2(is_aligned(_klass_range_start, klass_align) && 107 is_aligned(_klass_range_end, klass_align), 108 "Klass range must start and end at a properly aligned address"); 109 110 // Check _lowest_valid_narrow_klass_id and _highest_valid_narrow_klass_id 111 ASSERT_HERE_2(_lowest_valid_narrow_klass_id > 0, "Null is not a valid narrowKlass"); 112 ASSERT_HERE(_highest_valid_narrow_klass_id > _lowest_valid_narrow_klass_id); 113 114 Klass* const k1 = decode_not_null_without_asserts(_lowest_valid_narrow_klass_id, _base, _shift); 115 if (encoding_start == _klass_range_start) { 116 ASSERT_HERE_2((address)k1 == _klass_range_start + klass_align, "Not lowest"); 117 } else { 118 ASSERT_HERE_2((address)k1 == _klass_range_start, "Not lowest"); 119 } 120 narrowKlass nk1 = encode_not_null_without_asserts(k1, _base, _shift); 121 ASSERT_HERE_2(nk1 == _lowest_valid_narrow_klass_id, "not reversible"); 122 123 Klass* const k2 = decode_not_null_without_asserts(_highest_valid_narrow_klass_id, _base, _shift); 124 ASSERT_HERE((address)k2 == _klass_range_end - klass_align); 125 narrowKlass nk2 = encode_not_null_without_asserts(k2, _base, _shift); 126 ASSERT_HERE_2(nk2 == _highest_valid_narrow_klass_id, "not reversible"); 127 128 #ifdef AARCH64 129 // On aarch64, we never expect a shift value > 0 in standard (non-coh) mode 130 ASSERT_HERE_2(UseCompactObjectHeaders || _shift == 0, "Shift > 0 in non-coh mode?"); 131 #endif 132 #undef ASSERT_HERE 133 #undef ASSERT_HERE_2 134 } 135 #endif // ASSERT 136 137 // Helper function: given current Klass Range, Base and Shift, calculate the lowest and highest values 138 // of narrowKlass we can expect. 139 void CompressedKlassPointers::calc_lowest_highest_narrow_klass_id() { 140 address lowest_possible_klass_location = _klass_range_start; 141 142 // A Klass will never be placed at the Encoding range start, since that would translate to a narrowKlass=0, which 143 // is disallowed. Note that both Metaspace and CDS prvent allocation at the first address for this reason. 144 if (lowest_possible_klass_location == _base) { 145 lowest_possible_klass_location += klass_alignment_in_bytes(); 146 } 147 _lowest_valid_narrow_klass_id = (narrowKlass) ((uintptr_t)(lowest_possible_klass_location - _base) >> _shift); 148 149 address highest_possible_klass_location = _klass_range_end - klass_alignment_in_bytes(); 150 _highest_valid_narrow_klass_id = (narrowKlass) ((uintptr_t)(highest_possible_klass_location - _base) >> _shift); 151 } 152 153 // Given a klass range [addr, addr+len) and a given encoding scheme, assert that this scheme covers the range, then 154 // set this encoding scheme. Used by CDS at runtime to re-instate the scheme used to pre-compute klass ids for 155 // archived heap objects. 156 void CompressedKlassPointers::initialize_for_given_encoding(address addr, size_t len, address requested_base, int requested_shift) { 157 if (len > max_klass_range_size()) { 158 stringStream ss; 159 ss.print("Class space size and CDS archive size combined (%zu) " 160 "exceed the maximum possible size (%zu)", 161 len, max_klass_range_size()); 162 vm_exit_during_initialization(ss.base()); 163 } 164 165 // Note: While it would be technically valid for the encoding base to precede the start of the Klass range, 166 // we never do this here. This is used at CDS runtime to re-instate the scheme used to precompute the 167 // narrow Klass IDs in the archive, and the requested base should point to the start of the Klass range. 168 assert(requested_base == addr, "Invalid requested base"); 169 170 // Remember Klass range: 171 _klass_range_start = addr; 172 _klass_range_end = addr + len; 173 174 _base = requested_base; 175 _shift = requested_shift; 176 177 calc_lowest_highest_narrow_klass_id(); 178 179 DEBUG_ONLY(sanity_check_after_initialization();) 180 } 181 182 char* CompressedKlassPointers::reserve_address_space_X(uintptr_t from, uintptr_t to, size_t size, size_t alignment, bool aslr) { 183 alignment = MAX2(Metaspace::reserve_alignment(), alignment); 184 return os::attempt_reserve_memory_between((char*)from, (char*)to, size, alignment, aslr); 185 } 186 187 char* CompressedKlassPointers::reserve_address_space_for_unscaled_encoding(size_t size, bool aslr) { 188 const size_t unscaled_max = nth_bit(narrow_klass_pointer_bits()); 189 return reserve_address_space_X(0, unscaled_max, size, Metaspace::reserve_alignment(), aslr); 190 } 191 192 char* CompressedKlassPointers::reserve_address_space_for_zerobased_encoding(size_t size, bool aslr) { 193 const size_t unscaled_max = nth_bit(narrow_klass_pointer_bits()); 194 const size_t zerobased_max = nth_bit(narrow_klass_pointer_bits() + max_shift()); 195 return reserve_address_space_X(unscaled_max, zerobased_max, size, Metaspace::reserve_alignment(), aslr); 196 } 197 198 char* CompressedKlassPointers::reserve_address_space_for_16bit_move(size_t size, bool aslr) { 199 return reserve_address_space_X(nth_bit(32), nth_bit(48), size, nth_bit(32), aslr); 200 } 201 202 void CompressedKlassPointers::initialize(address addr, size_t len) { 203 204 if (len > max_klass_range_size()) { 205 stringStream ss; 206 ss.print("Class space size (%zu) exceeds the maximum possible size (%zu)", 207 len, max_klass_range_size()); 208 vm_exit_during_initialization(ss.base()); 209 } 210 211 // Remember the Klass range: 212 _klass_range_start = addr; 213 _klass_range_end = addr + len; 214 215 // Calculate Base and Shift: 216 217 if (UseCompactObjectHeaders) { 218 219 // In compact object header mode, with 22-bit narrowKlass, we don't attempt for 220 // zero-based mode. Instead, we set the base to the start of the klass range and 221 // then try for the smallest shift possible that still covers the whole range. 222 // The reason is that we want to avoid, if possible, shifts larger than 223 // a cacheline size. 224 _base = addr; 225 226 const int log_cacheline = exact_log2(DEFAULT_CACHE_LINE_SIZE); 227 int s = max_shift(); 228 while (s > log_cacheline && ((size_t)nth_bit(narrow_klass_pointer_bits() + s - 1) > len)) { 229 s--; 230 } 231 _shift = s; 232 233 } else { 234 235 // Traditional (non-compact) header mode 236 const uintptr_t unscaled_max = nth_bit(narrow_klass_pointer_bits()); 237 const uintptr_t zerobased_max = nth_bit(narrow_klass_pointer_bits() + max_shift()); 238 239 #ifdef AARCH64 240 // Aarch64 avoids zero-base shifted mode (_base=0 _shift>0), instead prefers 241 // non-zero-based mode with a zero shift. 242 _shift = 0; 243 address const end = addr + len; 244 _base = (end <= (address)unscaled_max) ? nullptr : addr; 245 #else 246 // We try, in order of preference: 247 // -unscaled (base=0 shift=0) 248 // -zero-based (base=0 shift>0) 249 // -nonzero-base (base>0 shift=0) 250 // Note that base>0 shift>0 should never be needed, since the klass range will 251 // never exceed 4GB. 252 address const end = addr + len; 253 if (end <= (address)unscaled_max) { 254 _base = nullptr; 255 _shift = 0; 256 } else { 257 if (end <= (address)zerobased_max) { 258 _base = nullptr; 259 _shift = max_shift(); 260 } else { 261 _base = addr; 262 _shift = 0; 263 } 264 } 265 #endif // AARCH64 266 } 267 268 calc_lowest_highest_narrow_klass_id(); 269 270 #ifdef ASSERT 271 sanity_check_after_initialization(); 272 #endif 273 } 274 275 void CompressedKlassPointers::print_mode(outputStream* st) { 276 st->print_cr("UseCompressedClassPointers %d, UseCompactObjectHeaders %d", 277 UseCompressedClassPointers, UseCompactObjectHeaders); 278 if (UseCompressedClassPointers) { 279 st->print_cr("Narrow klass pointer bits %d, Max shift %d", 280 _narrow_klass_pointer_bits, _max_shift); 281 st->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: %d", 282 p2i(base()), shift()); 283 st->print_cr("Encoding Range: " RANGE2FMT, RANGE2FMTARGS(_base, encoding_range_end())); 284 st->print_cr("Klass Range: " RANGE2FMT, RANGE2FMTARGS(_klass_range_start, _klass_range_end)); 285 st->print_cr("Klass ID Range: [%u - %u) (%u)", _lowest_valid_narrow_klass_id, _highest_valid_narrow_klass_id + 1, 286 _highest_valid_narrow_klass_id + 1 - _lowest_valid_narrow_klass_id); 287 } else { 288 st->print_cr("UseCompressedClassPointers off"); 289 } 290 } 291 292 #endif // _LP64