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/oopsHierarchy.hpp"
32 #include "runtime/globals.hpp"
33 #include "utilities/powerOfTwo.hpp"
34
35 // The markWord describes the header of an object.
36 //
37 // Bit-format of an object header (most significant first, big endian layout below):
38 //
39 // 32 bits:
40 // --------
41 // hash:25 age:4 self-fwd:1 lock:2
42 //
43 // 64 bits (without compact headers):
44 // ----------------------------------
45 // unused:22 hash:31 valhalla:4 age:4 self-fwd:1 lock:2
46 //
47 // 64 bits (with compact headers):
48 // -------------------------------
49 // klass:22 hash:31 valhalla:4 age:4 self-fwd:1 lock:2
50 //
51 // - lock bits are used to describe lock states: locked/unlocked/monitor-locked
52 // and to indicate that an object has been GC marked / forwarded.
53 //
54 // [header | 00] locked locked regular object header (fast-locking in use)
55 // [header | 01] unlocked regular object header
56 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true)
57 // [ptr | 10] monitor inflated lock (UseObjectMonitorTable == false, header is swapped out)
58 // [ptr | 11] marked used to mark an object (header is swapped out)
59 //
60 // - self-fwd - used by some GCs to indicate in-place forwarding.
61 //
62 // Note the position of 'self-fwd' is not by accident. When forwarding an
63 // object to a new heap position, HeapWord alignment guarantees the lower
64 // bits, including 'self-fwd' are 0. "is_self_forwarded()" will be correctly
65 // set to false. Otherwise encode_pointer_as_mark() may have 'self-fwd' set.
66 //
67 // - age - used by some GCs to track the age of objects.
68 //
69 // - valhalla - reserved for valhalla
70 //
71 // - hash - contains the identity hash value: largest value is 31 bits, see
72 // os::random(). Also, 64-bit VMs require a hash value no bigger than 32
73 // bits because they will not properly generate a mask larger than that:
74 // see library_call.cpp
75 //
76 // - klass - klass identifier used when UseCompactObjectHeaders == true
77
78 class ObjectMonitor;
79 class outputStream;
80
81 class markWord {
82 private:
83 uintptr_t _value;
84
85 public:
86 explicit markWord(uintptr_t value) : _value(value) {}
87
88 markWord() = default; // Doesn't initialize _value.
89
90 // It is critical for performance that this class be trivially
91 // destructable, copyable, and assignable.
92 ~markWord() = default;
93 markWord(const markWord&) = default;
94 markWord& operator=(const markWord&) = default;
95
96 static markWord from_pointer(void* ptr) {
97 return markWord((uintptr_t)ptr);
98 }
99 void* to_pointer() const {
100 return (void*)_value;
101 }
102
103 bool operator==(const markWord& other) const {
104 return _value == other._value;
105 }
106 bool operator!=(const markWord& other) const {
107 return !operator==(other);
108 }
109
110 // Conversion
111 uintptr_t value() const { return _value; }
112
113 // Constants, in least significant bit order
114
115 // Number of bits
116 static const int lock_bits = 2;
117 static const int self_fwd_bits = 1;
118 static const int age_bits = 4;
119 static const int valhalla_reserved_bits = LP64_ONLY(4) NOT_LP64(0);
120 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - self_fwd_bits - valhalla_reserved_bits;
121 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
122
123 // Shifts
124 static const int lock_shift = 0;
125 static const int self_fwd_shift = lock_shift + lock_bits;
126 static const int age_shift = self_fwd_shift + self_fwd_bits;
127 static const int valhalla_reserved_shift = age_shift + age_bits;
128 static const int hash_shift = valhalla_reserved_shift + valhalla_reserved_bits;
129
130 // Masks (in-place)
131 static const uintptr_t lock_mask_in_place = right_n_bits(lock_bits) << lock_shift;
132 static const uintptr_t self_fwd_bit_in_place = right_n_bits(self_fwd_bits) << self_fwd_shift;
133 static const uintptr_t age_mask_in_place = right_n_bits(age_bits) << age_shift;
134 static const uintptr_t hash_mask_in_place = right_n_bits(hash_bits) << hash_shift;
135
136 // Verify that _bit_in_place refers to constants with only one bit.
137 static_assert(is_power_of_2(self_fwd_bit_in_place));
138
139 // Masks (unshifted)
140 static const uintptr_t lock_mask = lock_mask_in_place >> lock_shift;
141 static const uintptr_t age_mask = age_mask_in_place >> age_shift;
142 static const uintptr_t hash_mask = hash_mask_in_place >> hash_shift;
143
144 #ifdef _LP64
145 // Used only with compact headers:
146 // We store the (narrow) Klass* in the bits 43 to 64.
147
148 // These are for bit-precise extraction of the narrow Klass* from the 64-bit markWord
149 static constexpr int klass_offset_in_bytes = 4;
150 static constexpr int klass_shift = hash_shift + hash_bits;
151 static constexpr int klass_shift_at_offset = klass_shift - klass_offset_in_bytes * BitsPerByte;
152 static constexpr int klass_bits = 22;
153 static constexpr uintptr_t klass_mask = right_n_bits(klass_bits);
154 static constexpr uintptr_t klass_mask_in_place = klass_mask << klass_shift;
155 #endif
156
157 static const uintptr_t locked_value = 0;
158 static const uintptr_t unlocked_value = 1;
159 static const uintptr_t monitor_value = 2;
160 static const uintptr_t marked_value = 3;
161
162 static const uintptr_t no_hash = 0 ; // no hash value assigned
163 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
164 static const uintptr_t no_lock_in_place = unlocked_value;
165
166 static const uint max_age = age_mask;
167
168 // Creates a markWord with all bits set to zero.
169 static markWord zero() { return markWord(uintptr_t(0)); }
170
171 // lock accessors (note that these assume lock_shift == 0)
172 bool is_locked() const {
173 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
174 }
175 bool is_unlocked() const {
176 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
177 }
178 bool is_marked() const {
179 return (mask_bits(value(), lock_mask_in_place) == marked_value);
180 }
181
182 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state
183 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
184 }
185
186 bool is_forwarded() const {
187 // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
188 return mask_bits(value(), lock_mask_in_place | self_fwd_bit_in_place) >= static_cast<intptr_t>(marked_value);
189 }
190
191 // Should this header be preserved during GC?
192 bool must_be_preserved() const {
193 return !is_unlocked() || !has_no_hash();
194 }
195
196 // WARNING: The following routines are used EXCLUSIVELY by
197 // synchronization functions. They are not really gc safe.
198 // They must get updated if markWord layout get changed.
199 markWord set_unlocked() const {
200 return markWord(value() | unlocked_value);
201 }
202
203 bool is_fast_locked() const {
204 return (value() & lock_mask_in_place) == locked_value;
205 }
206 markWord set_fast_locked() const {
207 // Clear the lock_mask_in_place bits to set locked_value:
208 return markWord(value() & ~lock_mask_in_place);
209 }
210
211 bool has_monitor() const {
212 return ((value() & lock_mask_in_place) == monitor_value);
213 }
214 markWord set_has_monitor() const {
215 return markWord((value() & ~lock_mask_in_place) | monitor_value);
216 }
217 ObjectMonitor* monitor() const {
218 assert(has_monitor(), "check");
219 assert(!UseObjectMonitorTable, "Locking with OM table does not use markWord for monitors");
220 // Use xor instead of &~ to provide one extra tag-bit check.
221 return (ObjectMonitor*) (value() ^ monitor_value);
222 }
223
224 static markWord encode(ObjectMonitor* monitor) {
225 assert(!UseObjectMonitorTable, "Locking with OM table does not use markWord for monitors");
226 uintptr_t tmp = (uintptr_t) monitor;
227 return markWord(tmp | monitor_value);
228 }
229
230 bool has_monitor_pointer() const {
231 intptr_t lockbits = value() & lock_mask_in_place;
232 return !UseObjectMonitorTable && lockbits == monitor_value;
233 }
234
235 bool has_displaced_mark_helper() const {
236 return has_monitor_pointer();
237 }
238 markWord displaced_mark_helper() const;
239 void set_displaced_mark_helper(markWord m) const;
240
241 // used to encode pointers during GC
242 markWord clear_lock_bits() const { return markWord(value() & ~lock_mask_in_place); }
243
244 // age operations
245 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
246 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
247
248 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
249 markWord set_age(uint v) const {
250 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
251 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
252 }
253 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
254
255 // hash operations
256 intptr_t hash() const {
257 return mask_bits(value() >> hash_shift, hash_mask);
258 }
259
260 bool has_no_hash() const {
261 return hash() == no_hash;
262 }
263
264 markWord copy_set_hash(intptr_t hash) const {
265 uintptr_t tmp = value() & (~hash_mask_in_place);
266 tmp |= ((hash & hash_mask) << hash_shift);
267 return markWord(tmp);
268 }
269
270 inline Klass* klass() const;
271 inline Klass* klass_or_null() const;
272 inline Klass* klass_without_asserts() const;
273 inline narrowKlass narrow_klass() const;
274 inline markWord set_narrow_klass(narrowKlass narrow_klass) const;
275
276 // Prototype mark for initialization
277 static markWord prototype() {
278 return markWord(unlocked_value);
279 }
280
281 // Debugging
282 void print_on(outputStream* st, bool print_monitor_info = true) const;
283
284 // Prepare address of oop for placement into mark
285 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
286
287 // Recover address of oop from encoded form used in mark
288 inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); }
289
290 inline bool is_self_forwarded() const {
291 return mask_bits(value(), self_fwd_bit_in_place) != 0;
292 }
293
294 inline markWord set_self_forwarded() const {
295 return markWord(value() | self_fwd_bit_in_place);
296 }
297
298 inline markWord unset_self_forwarded() const {
299 return markWord(value() & ~self_fwd_bit_in_place);
300 }
301
302 inline oop forwardee() const {
303 return cast_to_oop(decode_pointer());
304 }
305 };
306
307 // Support atomic operations.
308 template<>
309 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
310 typedef markWord Value;
311 typedef uintptr_t Decayed;
312
313 static Decayed decay(const Value& x) { return x.value(); }
314 static Value recover(Decayed x) { return Value(x); }
315 };
316
317 #endif // SHARE_OOPS_MARKWORD_HPP