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 #ifndef SHARE_OOPS_COMPRESSEDKLASS_HPP 26 #define SHARE_OOPS_COMPRESSEDKLASS_HPP 27 28 #include "memory/allStatic.hpp" 29 #include "utilities/globalDefinitions.hpp" 30 31 class outputStream; 32 class Klass; 33 34 // If compressed klass pointers then use narrowKlass. 35 typedef juint narrowKlass; 36 37 // For UseCompressedClassPointers. 38 class CompressedKlassPointers : public AllStatic { 39 friend class VMStructs; 40 friend class ArchiveBuilder; 41 42 // Tiny-class-pointer mode 43 static int _tiny_cp; // -1, 0=true, 1=false 44 45 // We use a different narrow Klass pointer geometry depending on 46 // whether we run in standard mode or in compact-object-header-mode (Lilliput): 47 // In Lilliput, we use smaller-than-32-bit class pointers ("tiny classpointer mode") 48 49 // Narrow klass pointer bits for an unshifted narrow Klass pointer. 50 static constexpr int narrow_klass_pointer_bits_legacy = 32; 51 static constexpr int narrow_klass_pointer_bits_tinycp = 22; 52 53 static int _narrow_klass_pointer_bits; 54 55 // The maximum shift we can use for standard mode and for TinyCP mode 56 static constexpr int max_shift_legacy = 3; 57 static constexpr int max_shift_tinycp = 10; 58 59 static int _max_shift; 60 61 static address _base; 62 static int _shift; 63 64 // Together with base, this defines the address range within which Klass 65 // structures will be located: [base, base+range). While the maximal 66 // possible encoding range is 4|32G for shift 0|3, if we know beforehand 67 // the expected range of Klass* pointers will be smaller, a platform 68 // could use this info to optimize encoding. 69 static size_t _range; 70 71 // Helper function for common cases. 72 static char* reserve_address_space_X(uintptr_t from, uintptr_t to, size_t size, size_t alignment, bool aslr); 73 static char* reserve_address_space_for_unscaled_encoding(size_t size, bool aslr); 74 static char* reserve_address_space_for_zerobased_encoding(size_t size, bool aslr); 75 static char* reserve_address_space_for_16bit_move(size_t size, bool aslr); 76 77 // Returns the highest address expressable with an unshifted narrow Klass pointer 78 inline static uintptr_t highest_unscaled_address(); 79 80 static bool pd_initialize(address addr, size_t len); 81 82 #ifdef ASSERT 83 // For sanity checks: Klass range 84 static address _klass_range_start; 85 static address _klass_range_end; 86 // For sanity checks: lowest, highest valid narrow klass ids != null 87 static narrowKlass _lowest_valid_narrow_klass_id; 88 static narrowKlass _highest_valid_narrow_klass_id; 89 static void calc_lowest_highest_narrow_klass_id(); 90 static void sanity_check_after_initialization(); 91 #endif // ASSERT 92 93 template <typename T> 94 static inline void check_init(T var) { 95 assert(var != (T)-1, "Not yet initialized"); 96 } 97 98 static inline Klass* decode_not_null_without_asserts(narrowKlass v, address base, int shift); 99 static inline Klass* decode_not_null(narrowKlass v, address base, int shift); 100 101 static inline narrowKlass encode_not_null(Klass* v, address base, int shift); 102 103 public: 104 105 // Initialization sequence: 106 // 1) Parse arguments. The following arguments take a role: 107 // - UseCompressedClassPointers 108 // - UseCompactObjectHeaders 109 // - Xshare on off dump 110 // - CompressedClassSpaceSize 111 // 2) call pre_initialize(): depending on UseCompactObjectHeaders, defines the limits of narrow Klass pointer 112 // geometry (how many bits, the max. possible shift) 113 // 3) .. from here on, narrow_klass_pointer_bits() and max_shift() can be used 114 // 4) call reserve_address_space_for_compressed_classes() either from CDS initialization or, if CDS is off, 115 // from metaspace initialization. Reserves space for class space + CDS, attempts to reserve such that 116 // we later can use a "good" encoding scheme. Reservation is highly CPU-specific. 117 // 5) Initialize the narrow Klass encoding scheme by determining encoding base and shift: 118 // 5a) if CDS=on: Calls initialize_for_given_encoding() with the reservation base from step (4) and the 119 // CDS-intrinsic setting for shift; here, we don't have any freedom to deviate from the base. 120 // 5b) if CDS=off: Calls initialize() - here, we have more freedom and, if we want, can choose an encoding 121 // base that differs from the reservation base from step (4). That allows us, e.g., to later use 122 // zero-based encoding. 123 // 6) ... from now on, we can use base() and shift(). 124 125 // Called right after argument parsing; defines narrow klass pointer geometry limits 126 static void pre_initialize(); 127 128 static bool tiny_classpointer_mode() { check_init(_tiny_cp); return (_tiny_cp == 1); } 129 130 // The number of bits a narrow Klass pointer has; 131 static int narrow_klass_pointer_bits() { check_init(_narrow_klass_pointer_bits); return _narrow_klass_pointer_bits; } 132 133 // The maximum possible shift; the actual shift employed later can be smaller (see initialize()) 134 static int max_shift() { check_init(_max_shift); return _max_shift; } 135 136 // Returns the maximum encoding range that can be covered with the currently 137 // choosen nKlassID geometry (nKlass bit size, max shift) 138 static size_t max_encoding_range_size(); 139 140 // Reserve a range of memory that is to contain Klass strucutures which are referenced by narrow Klass IDs. 141 // If optimize_for_zero_base is true, the implementation will attempt to reserve optimized for zero-based encoding. 142 static char* reserve_address_space_for_compressed_classes(size_t size, bool aslr, bool optimize_for_zero_base); 143 144 // Given a klass range [addr, addr+len) and a given encoding scheme, assert that this scheme covers the range, then 145 // set this encoding scheme. Used by CDS at runtime to re-instate the scheme used to pre-compute klass ids for 146 // archived heap objects. In this case, we don't have the freedom to choose base and shift; they are handed to 147 // us from CDS. 148 static void initialize_for_given_encoding(address addr, size_t len, address requested_base, int requested_shift); 149 150 // Given an address range [addr, addr+len) which the encoding is supposed to 151 // cover, choose base, shift and range. 152 // The address range is the expected range of uncompressed Klass pointers we 153 // will encounter (and the implicit promise that there will be no Klass 154 // structures outside this range). 155 static void initialize(address addr, size_t len); 156 157 static void print_mode(outputStream* st); 158 159 // Can only be used after initialization 160 static address base() { check_init(_base); return _base; } 161 static size_t range() { check_init(_range); return _range; } 162 static int shift() { check_init(_shift); return _shift; } 163 164 // Returns the alignment a Klass* is guaranteed to have. 165 // Note: *Not* the same as 1 << shift ! Klass are always guaranteed to be at least 64-bit aligned, 166 // so this will return 8 even if shift is 0. 167 static int klass_alignment_in_bytes() { return nth_bit(MAX2(3, _shift)); } 168 static int klass_alignment_in_words() { return klass_alignment_in_bytes() / BytesPerWord; } 169 170 static bool is_null(Klass* v) { return v == nullptr; } 171 static bool is_null(narrowKlass v) { return v == 0; } 172 173 // Versions without asserts 174 static inline Klass* decode_not_null_without_asserts(narrowKlass v); 175 static inline Klass* decode_without_asserts(narrowKlass v); 176 177 static inline Klass* decode_not_null(narrowKlass v); 178 static inline Klass* decode(narrowKlass v); 179 180 static inline narrowKlass encode_not_null_without_asserts(Klass* k, address narrow_base, int shift); 181 static inline narrowKlass encode_not_null(Klass* v); 182 static inline narrowKlass encode(Klass* v); 183 184 #ifdef ASSERT 185 // Given a Klass* k and an encoding (base, shift), check that k can be encoded 186 inline static void check_valid_klass(const Klass* k, address base, int shift); 187 // Given a Klass* k, check that k can be encoded with the current encoding 188 inline static void check_valid_klass(const Klass* k); 189 // Given a narrow Klass ID, check that it is valid according to current encoding 190 inline static void check_valid_narrow_klass_id(narrowKlass nk); 191 #endif 192 193 }; 194 195 #endif // SHARE_OOPS_COMPRESSEDKLASS_HPP