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 #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 
138   // Helper function for common cases.
139   static char* reserve_address_space_X(uintptr_t from, uintptr_t to, size_t size, size_t alignment, bool aslr);
140   static char* reserve_address_space_for_unscaled_encoding(size_t size, bool aslr);
141   static char* reserve_address_space_for_zerobased_encoding(size_t size, bool aslr);
142   static char* reserve_address_space_for_16bit_move(size_t size, bool aslr);
143   static void calc_lowest_highest_narrow_klass_id();
144 
145 #ifdef ASSERT
146   static void sanity_check_after_initialization();
147 #endif // ASSERT
148 
149   template <typename T>
150   static inline void check_init(T var) {
151     assert(var != (T)-1, "Not yet initialized");
152   }
153 
154   static inline Klass* decode_not_null_without_asserts(narrowKlass v, address base, int shift);
155 
156 public:
157 
158   // Initialization sequence:
159   // 1) Parse arguments. The following arguments take a role:
160   //      - UseCompressedClassPointers
161   //      - UseCompactObjectHeaders
162   //      - Xshare on off dump
163   //      - CompressedClassSpaceSize
164   // 2) call pre_initialize(): depending on UseCompactObjectHeaders, defines the limits of narrow Klass pointer
165   //    geometry (how many bits, the max. possible shift)
166   // 3) .. from here on, narrow_klass_pointer_bits() and max_shift() can be used
167   // 4) call reserve_address_space_for_compressed_classes() either from CDS initialization or, if CDS is off,
168   //    from metaspace initialization. Reserves space for class space + CDS, attempts to reserve such that
169   //    we later can use a "good" encoding scheme. Reservation is highly CPU-specific.
170   // 5) Initialize the narrow Klass encoding scheme by determining encoding base and shift:
171   //   5a) if CDS=on: Calls initialize_for_given_encoding() with the reservation base from step (4) and the
172   //       CDS-intrinsic setting for shift; here, we don't have any freedom to deviate from the base.
173   //   5b) if CDS=off: Calls initialize() - here, we have more freedom and, if we want, can choose an encoding
174   //       base that differs from the reservation base from step (4). That allows us, e.g., to later use
175   //       zero-based encoding.
176   // 6) ... from now on, we can use base() and shift().
177 
178   // Called right after argument parsing; defines narrow klass pointer geometry limits
179   static void pre_initialize();
180 
181   // The number of bits a narrow Klass pointer has;
182   static int narrow_klass_pointer_bits() { check_init(_narrow_klass_pointer_bits); return _narrow_klass_pointer_bits; }
183 
184   // The maximum possible shift; the actual shift employed later can be smaller (see initialize())
185   static int max_shift()                 { check_init(_max_shift); return _max_shift; }
186 
187   // Returns the maximum encoding range, given the current geometry (narrow klass bit size and shift)
188   static size_t max_encoding_range_size() { return nth_bit(narrow_klass_pointer_bits() + max_shift()); }
189 
190   // Returns the maximum allowed klass range size.
191   static size_t max_klass_range_size();
192 
193   // Reserve a range of memory that is to contain Klass strucutures which are referenced by narrow Klass IDs.
194   // If optimize_for_zero_base is true, the implementation will attempt to reserve optimized for zero-based encoding.
195   static char* reserve_address_space_for_compressed_classes(size_t size, bool aslr, bool optimize_for_zero_base);
196 
197   // Given a klass range [addr, addr+len) and a given encoding scheme, assert that this scheme covers the range, then
198   // set this encoding scheme. Used by CDS at runtime to re-instate the scheme used to pre-compute klass ids for
199   // archived heap objects. In this case, we don't have the freedom to choose base and shift; they are handed to
200   // us from CDS.
201   static void initialize_for_given_encoding(address addr, size_t len, address requested_base, int requested_shift);
202 
203   // Given an address range [addr, addr+len) which the encoding is supposed to
204   //  cover, choose base, shift and range.
205   //  The address range is the expected range of uncompressed Klass pointers we
206   //  will encounter (and the implicit promise that there will be no Klass
207   //  structures outside this range).
208   static void initialize(address addr, size_t len);
209 
210   static void     print_mode(outputStream* st);
211 
212   // Can only be used after initialization
213   static address  base()             { check_init(_base); return  _base; }
214   static int      shift()            { check_init(_shift); return  _shift; }
215 
216   static address  klass_range_start()  { return  _klass_range_start; }
217   static address  klass_range_end()    { return  _klass_range_end; }
218 
219   static inline address encoding_range_end();
220 
221   // Returns the alignment a Klass* is guaranteed to have.
222   // Note: *Not* the same as 1 << shift ! Klass are always guaranteed to be at least 64-bit aligned,
223   // so this will return 8 even if shift is 0.
224   static int klass_alignment_in_bytes() { return nth_bit(MAX2(3, _shift)); }
225   static int klass_alignment_in_words() { return klass_alignment_in_bytes() / BytesPerWord; }
226 
227   // Returns the highest possible narrowKlass value given the current Klass range
228   static narrowKlass highest_valid_narrow_klass_id() { return _highest_valid_narrow_klass_id; }
229 
230   static bool is_null(Klass* v)      { return v == nullptr; }
231   static bool is_null(narrowKlass v) { return v == 0; }
232 
233   // Versions without asserts
234   static inline Klass* decode_without_asserts(narrowKlass v);
235   static inline Klass* decode_not_null(narrowKlass v);
236   static inline Klass* decode(narrowKlass v);
237 
238   static inline narrowKlass encode_not_null_without_asserts(Klass* k, address narrow_base, int shift);
239   static inline narrowKlass encode_not_null(Klass* v);
240   static inline narrowKlass encode(Klass* v);
241 
242 #ifdef ASSERT
243   // Given an address, check that it can be encoded with the current encoding
244   inline static void check_encodable(const void* addr);
245   // Given a narrow Klass ID, check that it is valid according to current encoding
246   inline static void check_valid_narrow_klass_id(narrowKlass nk);
247 #endif
248 
249   // Returns whether the pointer is in the memory region used for encoding compressed
250   // class pointers.  This includes CDS.
251   static inline bool is_encodable(const void* addr) {
252     // An address can only be encoded if:
253     //
254     // 1) the address lies within the klass range.
255     // 2) It is suitably aligned to 2^encoding_shift. This only really matters for
256     //    +UseCompactObjectHeaders, since the encoding shift can be large (max 10 bits -> 1KB).
257     return (address)addr >= _klass_range_start && (address)addr < _klass_range_end &&
258         is_aligned(addr, klass_alignment_in_bytes());
259   }
260 
261 };
262 
263 #endif // SHARE_OOPS_COMPRESSEDKLASS_HPP