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 #ifndef SHARE_OOPS_COMPRESSEDKLASS_HPP
 26 #define SHARE_OOPS_COMPRESSEDKLASS_HPP
 27 
 28 #include "memory/allStatic.hpp"
 29 #include "utilities/align.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 
 32 class outputStream;
 33 class Klass;
 34 
 35 // Narrow Klass Encoding
 36 //
 37 // Klass Range:
 38 //  a contiguous memory range into which we place Klass that should be encodable. Not every Klass
 39 //  needs to be encodable. There is only one such memory range.
 40 //  If CDS is disabled, this Klass Range is the same as the metaspace class space. If CDS is enabled, the
 41 //  Klass Range contains both CDS and class space adjacent to each other (with a potential small
 42 //  unused alignment gap between them).
 43 //
 44 // Encoding Range:
 45 //  This is the range covered by the current encoding scheme. The encoding scheme is defined by
 46 //  the encoding base, encoding shift and (implicitly) the bit size of the narrowKlass. The
 47 //  Encoding Range is:
 48 //   [ <encoding base> ... <encoding base> + (1 << (<narrowKlass-bitsize> + <shift>) )
 49 //
 50 // Note that while the Klass Range must be contained within the Encoding Range, the Encoding Range
 51 // is typically a lot larger than the Klass Range:
 52 //  - the encoding base can start before the Klass Range start (specifically, it can start at 0 for
 53 //    zero-based encoding)
 54 //  - the end of the Encoding Range usually extends far beyond the end of the Klass Range.
 55 //
 56 //
 57 // Examples:
 58 //
 59 // "unscaled" (zero-based zero-shift) encoding, CDS off, class space of 1G starts at 0x4B00_0000:
 60 // - Encoding Range: [0             .. 0x1_0000_0000 ) (4 GB)
 61 // - Klass Range:    [0x4B00_0000   .. 0x  8B00_0000 ) (1 GB)
 62 //
 63 //
 64 // _base        _klass_range_start              _klass_range_end             encoding end
 65 //   |                |//////////////////////////////|                             |
 66 //   |   ...          |///////1gb class space////////|               ...           |
 67 //   |                |//////////////////////////////|                             |
 68 //  0x0         0x4B00_0000                   0x8B00_0000                    0x1_0000_0000
 69 //
 70 //
 71 //
 72 // "zero-based" (but scaled) encoding, shift=3, CDS off, 1G Class space at 0x7_C000_0000 (31GB):
 73 // - Encoding Range: [0             .. 0x8_0000_0000 ) (32 GB)
 74 // - Klass Range:    [0x7_C000_0000 .. 0x8_0000_0000 ) (1 GB)
 75 //
 76 //                                                                  encoding end
 77 // _base                            _klass_range_start              _klass_range_end
 78 //   |                                   |//////////////////////////////|
 79 //   |   ...                             |///////1gb class space////////|
 80 //   |                                   |//////////////////////////////|
 81 //  0x0                            0x7_C000_0000                  0x8_0000_0000
 82 //
 83 //
 84 // CDS enabled, 128MB CDS region starts 0x8_0000_0000, followed by a 1GB class space. Encoding
 85 // base will point to CDS region start, shift=0:
 86 // - Encoding Range: [0x8_0000_0000 .. 0x9_0000_0000 ) (4 GB)
 87 // - Klass Range:    [0x8_0000_0000 .. 0x8_4800_0000 ) (128 MB + 1 GB)
 88 //
 89 //  _base
 90 // _klass_range_start                   _klass_range_end                        encoding end
 91 //   |//////////|///////////////////////////|                                         |
 92 //   |///CDS////|////1gb class space////////|            ...    ...                   |
 93 //   |//////////|///////////////////////////|                                         |
 94 //   |                                      |                                         |
 95 // 0x8_0000_0000                      0x8_4800_0000                            0x9_0000_0000
 96 //
 97 
 98 // If compressed klass pointers then use narrowKlass.
 99 typedef juint  narrowKlass;
100 
101 // For UseCompressedClassPointers.
102 class CompressedKlassPointers : public AllStatic {
103   friend class VMStructs;
104   friend class ArchiveBuilder;
105 
106   // We use a different narrow Klass pointer geometry depending on
107   // whether we run in standard mode or in compact-object-header-mode.
108 
109   // Narrow klass pointer bits for an unshifted narrow Klass pointer.
110   static constexpr int narrow_klass_pointer_bits_noncoh = 32;
111   static constexpr int narrow_klass_pointer_bits_coh = 19;
112 
113   // Bit size of a narrowKlass
114   static int _narrow_klass_pointer_bits;
115 
116   // The maximum shift values we can use depending on UseCompactObjectHeaders
117   static constexpr int max_shift_noncoh = 3;
118   static constexpr int max_shift_coh = 10;
119 
120   // Maximum shift usable
121   static int _max_shift;
122 
123   // Encoding Base, Encoding Shift
124   static address _base;
125   static int _shift;
126 
127   // Start and end of the Klass Range.
128   // Note: guaranteed to be aligned to 1<<shift (klass_alignment_in_bytes)
129   static address _klass_range_start;
130   static address _klass_range_end;
131 
132   // Values for the lowest (inclusive) and highest (inclusive) narrow Klass ID, given the
133   // current Klass Range and encoding settings.
134   static narrowKlass _lowest_valid_narrow_klass_id;
135   static narrowKlass _highest_valid_narrow_klass_id;
136 
137   // Protection zone size (0 if not set up)
138   static size_t _protection_zone_size;
139 
140   // Helper function for common cases.
141   static char* reserve_address_space_X(uintptr_t from, uintptr_t to, size_t size, size_t alignment, bool aslr);
142   static char* reserve_address_space_for_unscaled_encoding(size_t size, bool aslr);
143   static char* reserve_address_space_for_zerobased_encoding(size_t size, bool aslr);
144   static char* reserve_address_space_for_16bit_move(size_t size, bool aslr);
145   static void calc_lowest_highest_narrow_klass_id();
146 
147 #ifdef ASSERT
148   static void sanity_check_after_initialization();
149 #endif // ASSERT
150 
151   template <typename T>
152   static inline void check_init(T var) {
153     assert(var != (T)-1, "Not yet initialized");
154   }
155 
156   static inline Klass* decode_not_null_without_asserts(narrowKlass v, address base, int shift);
157 
158 public:
159 
160   // Initialization sequence:
161   // 1) Parse arguments. The following arguments take a role:
162   //      - UseCompressedClassPointers
163   //      - UseCompactObjectHeaders
164   //      - Xshare on off dump
165   //      - CompressedClassSpaceSize
166   // 2) call pre_initialize(): depending on UseCompactObjectHeaders, defines the limits of narrow Klass pointer
167   //    geometry (how many bits, the max. possible shift)
168   // 3) .. from here on, narrow_klass_pointer_bits() and max_shift() can be used
169   // 4) call reserve_address_space_for_compressed_classes() either from CDS initialization or, if CDS is off,
170   //    from metaspace initialization. Reserves space for class space + CDS, attempts to reserve such that
171   //    we later can use a "good" encoding scheme. Reservation is highly CPU-specific.
172   // 5) Initialize the narrow Klass encoding scheme by determining encoding base and shift:
173   //   5a) if CDS=on: Calls initialize_for_given_encoding() with the reservation base from step (4) and the
174   //       CDS-intrinsic setting for shift; here, we don't have any freedom to deviate from the base.
175   //   5b) if CDS=off: Calls initialize() - here, we have more freedom and, if we want, can choose an encoding
176   //       base that differs from the reservation base from step (4). That allows us, e.g., to later use
177   //       zero-based encoding.
178   // 6) ... from now on, we can use base() and shift().
179 
180   // Called right after argument parsing; defines narrow klass pointer geometry limits
181   static void pre_initialize();
182 
183   // The number of bits a narrow Klass pointer has;
184   static int narrow_klass_pointer_bits() { check_init(_narrow_klass_pointer_bits); return _narrow_klass_pointer_bits; }
185 
186   // The maximum possible shift; the actual shift employed later can be smaller (see initialize())
187   static int max_shift()                 { check_init(_max_shift); return _max_shift; }
188 
189   // Returns the maximum encoding range, given the current geometry (narrow klass bit size and shift)
190   static size_t max_encoding_range_size() { return nth_bit(narrow_klass_pointer_bits() + max_shift()); }
191 
192   // Returns the maximum allowed klass range size.
193   static size_t max_klass_range_size();
194 
195   // Reserve a range of memory that is to contain Klass strucutures which are referenced by narrow Klass IDs.
196   // If optimize_for_zero_base is true, the implementation will attempt to reserve optimized for zero-based encoding.
197   static char* reserve_address_space_for_compressed_classes(size_t size, bool aslr, bool optimize_for_zero_base);
198 
199   // Given a klass range [addr, addr+len) and a given encoding scheme, assert that this scheme covers the range, then
200   // set this encoding scheme. Used by CDS at runtime to re-instate the scheme used to pre-compute klass ids for
201   // archived heap objects. In this case, we don't have the freedom to choose base and shift; they are handed to
202   // us from CDS.
203   static void initialize_for_given_encoding(address addr, size_t len, address requested_base, int requested_shift);
204 
205   // Given an address range [addr, addr+len) which the encoding is supposed to
206   //  cover, choose base, shift and range.
207   //  The address range is the expected range of uncompressed Klass pointers we
208   //  will encounter (and the implicit promise that there will be no Klass
209   //  structures outside this range).
210   static void initialize(address addr, size_t len);
211 
212   static void     print_mode(outputStream* st);
213 
214   // Can only be used after initialization
215   static address  base()             { check_init(_base); return  _base; }
216   static int      shift()            { check_init(_shift); return  _shift; }
217 
218   static address  klass_range_start()  { return  _klass_range_start; }
219   static address  klass_range_end()    { return  _klass_range_end; }
220 
221   static inline address encoding_range_end();
222 
223   // Returns the alignment a Klass* is guaranteed to have.
224   // Note: *Not* the same as 1 << shift ! Klass are always guaranteed to be at least 64-bit aligned,
225   // so this will return 8 even if shift is 0.
226   static int klass_alignment_in_bytes() { return nth_bit(MAX2(3, _shift)); }
227   static int klass_alignment_in_words() { return klass_alignment_in_bytes() / BytesPerWord; }
228 
229   // Returns the highest possible narrowKlass value given the current Klass range
230   static narrowKlass highest_valid_narrow_klass_id() { return _highest_valid_narrow_klass_id; }
231 
232   static bool is_null(Klass* v)      { return v == nullptr; }
233   static bool is_null(narrowKlass v) { return v == 0; }
234 
235   // Versions without asserts
236   static inline Klass* decode_not_null_without_asserts(narrowKlass v);
237   static inline Klass* decode_without_asserts(narrowKlass v);
238   static inline Klass* decode_not_null(narrowKlass v);
239   static inline Klass* decode(narrowKlass v);
240 
241   static inline narrowKlass encode_not_null_without_asserts(Klass* k, address narrow_base, int shift);
242   static inline narrowKlass encode_not_null(Klass* v);
243   static inline narrowKlass encode(Klass* v);
244 
245 #ifdef ASSERT
246   // Given an address, check that it can be encoded with the current encoding
247   inline static void check_encodable(const void* addr);
248   // Given a narrow Klass ID, check that it is valid according to current encoding
249   inline static void check_valid_narrow_klass_id(narrowKlass nk);
250 #endif
251 
252   // Returns whether the pointer is in the memory region used for encoding compressed
253   // class pointers.  This includes CDS.
254   static inline bool is_encodable(const void* addr) {
255     // An address can only be encoded if:
256     //
257     // 1) the address lies within the klass range.
258     // 2) It is suitably aligned to 2^encoding_shift. This only really matters for
259     //    +UseCompactObjectHeaders, since the encoding shift can be large (max 10 bits -> 1KB).
260     return (address)addr >= _klass_range_start && (address)addr < _klass_range_end &&
261         is_aligned(addr, klass_alignment_in_bytes());
262   }
263 
264   // Protect a zone a the start of the encoding range
265   static void establish_protection_zone(address addr, size_t size);
266 
267   // Returns true if address points into protection zone (for error reporting)
268   static bool is_in_protection_zone(address addr);
269 
270 #if defined(AARCH64) && !defined(ZERO)
271   // Check that with the given base, shift and range, aarch64 code can encode and decode the klass pointer.
272   static bool check_klass_decode_mode(address base, int shift, const size_t range);
273   // Called after initialization.
274   static bool set_klass_decode_mode();
275 #else
276   static bool check_klass_decode_mode(address base, int shift, const size_t range) { return true; }
277   static bool set_klass_decode_mode() { return true; }
278 #endif
279 };
280 
281 #endif // SHARE_OOPS_COMPRESSEDKLASS_HPP