1 /*
2 * Copyright (c) 1997, 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_MARKWORD_HPP
26 #define SHARE_OOPS_MARKWORD_HPP
27
28 #include "cppstdlib/type_traits.hpp"
29 #include "metaprogramming/primitiveConversions.hpp"
30 #include "oops/compressedKlass.hpp"
31 #include "oops/layoutKind.hpp"
32 #include "oops/oopsHierarchy.hpp"
33 #include "runtime/globals.hpp"
34 #include "utilities/vmEnums.hpp"
35
36 // The markWord describes the header of an object.
37 //
38 // Bit-format of an object header (most significant first, big endian layout below):
39 //
40 // 32 bits:
41 // --------
42 // hash:25 ------------>| age:4 self-fwd:1 lock:2 (normal object)
43 //
44 // 64 bits:
45 // --------
46 // unused:22 hash:31 -->| valhalla:4 age:4 self-fwd:1 lock:2 (normal object)
47 //
48 // 64 bits (with compact headers):
49 // -------------------------------
50 // klass:22 hash:31 -->| valhalla:4 age:4 self-fwd:1 lock:2 (normal object)
51 //
52 // - hash contains the identity hash value: largest value is
53 // 31 bits, see os::random(). Also, 64-bit vm's require
54 // a hash value no bigger than 32 bits because they will not
55 // properly generate a mask larger than that: see library_call.cpp
56 //
57 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
58 //
59 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use)
60 // [header | 00] locked locked regular object header (fast-locking in use)
61 // [header | 01] unlocked regular object header
62 // [ptr | 10] monitor inflated lock (header is swapped out, UseObjectMonitorTable == false)
63 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true)
64 // [ptr | 11] marked used to mark an object
65 //
66 // VALHALLA EXTENSIONS:
67 //
68 // N.B.: 32 bit mode is not supported, this section assumes 64 bit systems.
69 //
70 // Project Valhalla uses markWord bits to denote the following oops (listed least to most significant):
71 // * inline types: have alternative bytecode behavior, e.g. can not be locked
72 // * flat arrays: load/decode of klass layout helper is expensive for aaload
73 // * "null free" arrays: load/decode of klass layout helper again for aaload
74 // * inline type: "larval state": mutable state, but only during object init, observable
75 // by only by a single thread (generally do not mutate markWord)
76 //
77 // Inline types cannot be locked, monitored or inflating.
78 //
79 // Note the position of 'self-fwd' is not by accident. When forwarding an
80 // object to a new heap position, HeapWord alignment guarantees the lower
81 // bits, including 'self-fwd' are 0. "is_self_forwarded()" will be correctly
82 // set to false. Otherwise encode_pointer_as_mark() may have 'self-fwd' set.
83
84 class BasicLock;
85 class ObjectMonitor;
86 class JavaThread;
87 class outputStream;
88
89 class markWord {
90 private:
91 uintptr_t _value;
92
93 public:
94 explicit markWord(uintptr_t value) : _value(value) {}
95
96 markWord() = default; // Doesn't initialize _value.
97
98 // It is critical for performance that this class be trivially
99 // destructable, copyable, and assignable.
100 ~markWord() = default;
101 markWord(const markWord&) = default;
102 markWord& operator=(const markWord&) = default;
103
104 static markWord from_pointer(void* ptr) {
105 return markWord((uintptr_t)ptr);
106 }
107 void* to_pointer() const {
108 return (void*)_value;
109 }
110
111 bool operator==(const markWord& other) const {
112 return _value == other._value;
113 }
114 bool operator!=(const markWord& other) const {
115 return !operator==(other);
116 }
117
118 // Conversion
119 uintptr_t value() const { return _value; }
120
121 // Constants, in least significant bit order
122 static const int lock_bits = 2;
123 static const int self_fwd_bits = 1;
124 // instance state
125 static const int age_bits = 4;
126 // prototype header bits (fast path instead of klass layout_helper)
127 static const int inline_type_bits = 1;
128 static const int null_free_array_bits = LP64_ONLY(1) NOT_LP64(0);
129 static const int flat_array_bits = LP64_ONLY(1) NOT_LP64(0);
130 static const int larval_bits = 1;
131 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - inline_type_bits - larval_bits - flat_array_bits - null_free_array_bits - self_fwd_bits;
132 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
133
134 static const int lock_shift = 0;
135 static const int self_fwd_shift = lock_shift + lock_bits;
136 static const int age_shift = self_fwd_shift + self_fwd_bits;
137 static const int inline_type_shift = age_shift + age_bits;
138 static const int null_free_array_shift = inline_type_shift + inline_type_bits;
139 static const int flat_array_shift = null_free_array_shift + null_free_array_bits;
140 static const int larval_shift = flat_array_shift + flat_array_bits;
141 static const int hash_shift = larval_shift + larval_bits;
142
143 static const uintptr_t lock_mask = right_n_bits(lock_bits);
144 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift;
145 static const uintptr_t self_fwd_mask = right_n_bits(self_fwd_bits);
146 static const uintptr_t self_fwd_mask_in_place = self_fwd_mask << self_fwd_shift;
147 static const uintptr_t inline_type_bit_in_place = right_n_bits(inline_type_bits) << inline_type_shift;
148 static const uintptr_t inline_type_mask_in_place = inline_type_bit_in_place + lock_mask;
149 static const uintptr_t null_free_array_mask = right_n_bits(null_free_array_bits);
150 static const uintptr_t null_free_array_mask_in_place = (null_free_array_mask << null_free_array_shift) | lock_mask_in_place;
151 static const uintptr_t null_free_array_bit_in_place = (right_n_bits(null_free_array_bits) << null_free_array_shift);
152 static const uintptr_t flat_array_mask = right_n_bits(flat_array_bits);
153 static const uintptr_t flat_array_mask_in_place = (flat_array_mask << flat_array_shift) | null_free_array_mask_in_place | lock_mask_in_place;
154 static const uintptr_t flat_array_bit_in_place = right_n_bits(flat_array_bits) << flat_array_shift;
155 static const uintptr_t age_mask = right_n_bits(age_bits);
156 static const uintptr_t age_mask_in_place = age_mask << age_shift;
157
158 static const uintptr_t larval_mask = right_n_bits(larval_bits);
159 static const uintptr_t larval_mask_in_place = (larval_mask << larval_shift) | inline_type_mask_in_place;
160 static const uintptr_t larval_bit_in_place = right_n_bits(larval_bits) << larval_shift;
161
162 static const uintptr_t hash_mask = right_n_bits(hash_bits);
163 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift;
164
165 #ifdef _LP64
166 // Used only with compact headers:
167 // We store the (narrow) Klass* in the bits 43 to 64.
168
169 // These are for bit-precise extraction of the narrow Klass* from the 64-bit Markword
170 static constexpr int klass_offset_in_bytes = 4;
171 static constexpr int klass_shift = hash_shift + hash_bits;
172 static constexpr int klass_shift_at_offset = klass_shift - klass_offset_in_bytes * BitsPerByte;
173 static constexpr int klass_bits = 22;
174 static constexpr uintptr_t klass_mask = right_n_bits(klass_bits);
175 static constexpr uintptr_t klass_mask_in_place = klass_mask << klass_shift;
176 #endif
177
178
179 static const uintptr_t locked_value = 0;
180 static const uintptr_t unlocked_value = 1;
181 static const uintptr_t monitor_value = 2;
182 static const uintptr_t marked_value = 3;
183
184 static const uintptr_t inline_type_pattern = inline_type_bit_in_place | unlocked_value;
185 static const uintptr_t null_free_array_pattern = null_free_array_bit_in_place | unlocked_value;
186 static const uintptr_t null_free_flat_array_pattern = flat_array_bit_in_place | null_free_array_pattern;
187 static const uintptr_t nullable_flat_array_pattern = flat_array_bit_in_place | unlocked_value;
188
189 static const uintptr_t larval_pattern = larval_bit_in_place | inline_type_pattern;
190
191 static const uintptr_t no_hash = 0 ; // no hash value assigned
192 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
193 static const uintptr_t no_lock_in_place = unlocked_value;
194
195 static const uint max_age = age_mask;
196
197 // Creates a markWord with all bits set to zero.
198 static markWord zero() { return markWord(uintptr_t(0)); }
199
200 bool is_inline_type() const {
201 return (mask_bits(value(), inline_type_mask_in_place) == inline_type_pattern);
202 }
203
204 // lock accessors (note that these assume lock_shift == 0)
205 bool is_locked() const {
206 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
207 }
208 bool is_unlocked() const {
209 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
210 }
211 bool is_marked() const {
212 return (mask_bits(value(), lock_mask_in_place) == marked_value);
213 }
214
215 // is unlocked and not an inline type (which cannot be involved in locking, displacement or inflation)
216 // i.e. test both lock bits and the inline type bit together
217 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state
218 return (mask_bits(value(), inline_type_mask_in_place) == unlocked_value);
219 }
220
221 bool is_forwarded() const {
222 // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
223 return mask_bits(value(), lock_mask_in_place | self_fwd_mask_in_place) >= static_cast<intptr_t>(marked_value);
224 }
225
226 // Should this header be preserved during GC?
227 bool must_be_preserved() const {
228 return (!is_unlocked() || !has_no_hash() || is_larval_state());
229 }
230
231 // WARNING: The following routines are used EXCLUSIVELY by
232 // synchronization functions. They are not really gc safe.
233 // They must get updated if markWord layout get changed.
234 markWord set_unlocked() const {
235 return markWord(value() | unlocked_value);
236 }
237
238 bool is_fast_locked() const {
239 return (value() & lock_mask_in_place) == locked_value;
240 }
241 markWord set_fast_locked() const {
242 // Clear the lock_mask_in_place bits to set locked_value:
243 return markWord(value() & ~lock_mask_in_place);
244 }
245
246 bool has_monitor() const {
247 return ((value() & lock_mask_in_place) == monitor_value);
248 }
249 markWord set_has_monitor() const {
250 return markWord((value() & ~lock_mask_in_place) | monitor_value);
251 }
252 ObjectMonitor* monitor() const {
253 assert(has_monitor(), "check");
254 assert(!UseObjectMonitorTable, "Locking with OM table does not use markWord for monitors");
255 // Use xor instead of &~ to provide one extra tag-bit check.
256 return (ObjectMonitor*) (value() ^ monitor_value);
257 }
258
259 static markWord encode(ObjectMonitor* monitor) {
260 assert(!UseObjectMonitorTable, "Locking with OM table does not use markWord for monitors");
261 uintptr_t tmp = (uintptr_t) monitor;
262 return markWord(tmp | monitor_value);
263 }
264
265 bool has_displaced_mark_helper() const {
266 intptr_t lockbits = value() & lock_mask_in_place;
267 return !UseObjectMonitorTable && lockbits == monitor_value;
268 }
269 markWord displaced_mark_helper() const;
270 void set_displaced_mark_helper(markWord m) const;
271
272 // used to encode pointers during GC
273 markWord clear_lock_bits() const { return markWord(value() & ~lock_mask_in_place); }
274
275 // age operations
276 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
277 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
278
279 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
280 markWord set_age(uint v) const {
281 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
282 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
283 }
284 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
285
286 // hash operations
287 intptr_t hash() const {
288 return mask_bits(value() >> hash_shift, hash_mask);
289 }
290
291 bool has_no_hash() const {
292 return hash() == no_hash;
293 }
294
295 // private buffered value operations
296 markWord enter_larval_state() const {
297 return markWord(value() | larval_bit_in_place);
298 }
299 markWord exit_larval_state() const {
300 return markWord(value() & ~larval_bit_in_place);
301 }
302 bool is_larval_state() const {
303 return (mask_bits(value(), larval_mask_in_place) == larval_pattern);
304 }
305
306 bool is_flat_array() const {
307 #ifdef _LP64 // 64 bit encodings only
308 return (mask_bits(value(), flat_array_mask_in_place) == null_free_flat_array_pattern)
309 || (mask_bits(value(), flat_array_mask_in_place) == nullable_flat_array_pattern);
310 #else
311 return false;
312 #endif
313 }
314
315 bool is_null_free_array() const {
316 #ifdef _LP64 // 64 bit encodings only
317 return (mask_bits(value(), null_free_array_mask_in_place) == null_free_array_pattern);
318 #else
319 return false;
320 #endif
321 }
322
323 markWord copy_set_hash(intptr_t hash) const {
324 uintptr_t tmp = value() & (~hash_mask_in_place);
325 tmp |= ((hash & hash_mask) << hash_shift);
326 return markWord(tmp);
327 }
328
329 inline Klass* klass() const;
330 inline Klass* klass_or_null() const;
331 inline Klass* klass_without_asserts() const;
332 inline narrowKlass narrow_klass() const;
333 inline markWord set_narrow_klass(narrowKlass narrow_klass) const;
334
335 // Prototype mark for initialization
336 static markWord prototype() {
337 return markWord( no_hash_in_place | no_lock_in_place );
338 }
339
340 static markWord inline_type_prototype() {
341 return markWord(inline_type_pattern);
342 }
343
344 #ifdef _LP64 // 64 bit encodings only
345 static markWord flat_array_prototype(LayoutKind lk);
346
347 static markWord null_free_array_prototype() {
348 return markWord(null_free_array_pattern);
349 }
350 #endif
351
352 // Debugging
353 void print_on(outputStream* st, bool print_monitor_info = true) const;
354
355 // Prepare address of oop for placement into mark
356 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
357
358 inline void* decode_pointer() const {
359 return (void*) (clear_lock_bits().value());
360 }
361
362 inline bool is_self_forwarded() const {
363 return mask_bits(value(), self_fwd_mask_in_place) != 0;
364 }
365
366 inline markWord set_self_forwarded() const {
367 return markWord(value() | self_fwd_mask_in_place);
368 }
369
370 inline markWord unset_self_forwarded() const {
371 return markWord(value() & ~self_fwd_mask_in_place);
372 }
373
374 inline oop forwardee() const {
375 return cast_to_oop(decode_pointer());
376 }
377 };
378
379 // Support atomic operations.
380 template<>
381 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
382 typedef markWord Value;
383 typedef uintptr_t Decayed;
384
385 static Decayed decay(const Value& x) { return x.value(); }
386 static Value recover(Decayed x) { return Value(x); }
387 };
388
389 #endif // SHARE_OOPS_MARKWORD_HPP