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