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 - only supported on 64-bit VMs
 70 //
 71 //    * inline types:      A value class instance
 72 //    * flat arrays:       An array with flattened value class elements
 73 //    * null-free arrays:  An array instance without null elements
 74 //    * valhalla reserved: Reserved for future use
 75 //
 76 //    Inline types cannot be locked and do not have an identity hash.
 77 //
 78 //  - hash - contains the identity hash value: largest value is 31 bits, see
 79 //    os::random().  Also, 64-bit VMs require a hash value no bigger than 32
 80 //    bits because they will not properly generate a mask larger than that:
 81 //    see library_call.cpp
 82 //
 83 //  - klass - klass identifier used when UseCompactObjectHeaders == true
 84 
 85 class ObjectMonitor;
 86 class outputStream;
 87 
 88 class markWord {
 89  private:
 90   uintptr_t _value;
 91 
 92  public:
 93   explicit markWord(uintptr_t value) : _value(value) {}
 94 
 95   markWord() = default;         // Doesn't initialize _value.
 96 
 97   // It is critical for performance that this class be trivially
 98   // destructable, copyable, and assignable.
 99   ~markWord() = default;
100   markWord(const markWord&) = default;
101   markWord& operator=(const markWord&) = default;
102 
103   static markWord from_pointer(void* ptr) {
104     return markWord((uintptr_t)ptr);
105   }
106   void* to_pointer() const {
107     return (void*)_value;
108   }
109 
110   bool operator==(const markWord& other) const {
111     return _value == other._value;
112   }
113   bool operator!=(const markWord& other) const {
114     return !operator==(other);
115   }
116 
117   // Conversion
118   uintptr_t value() const { return _value; }
119 
120   // Constants, in least significant bit order
121 
122   // Number of bits
123   static const int lock_bits                      = 2;
124   static const int self_fwd_bits                  = 1;
125   static const int age_bits                       = 4;
126   static const int inline_type_bits               = LP64_ONLY(1) NOT_LP64(0);
127   static const int null_free_array_bits           = LP64_ONLY(1) NOT_LP64(0);
128   static const int flat_array_bits                = LP64_ONLY(1) NOT_LP64(0);
129   static const int valhalla_reserved_bits         = LP64_ONLY(1) NOT_LP64(0);
130   static const int max_hash_bits                  = BitsPerWord - age_bits - lock_bits - inline_type_bits - valhalla_reserved_bits - flat_array_bits - null_free_array_bits - self_fwd_bits;
131   static const int hash_bits                      = max_hash_bits > 31 ? 31 : max_hash_bits;
132 
133   // Shifts
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 valhalla_reserved_shift        = flat_array_shift + flat_array_bits;
141   static const int hash_shift                     = valhalla_reserved_shift + valhalla_reserved_bits;
142 
143   // Masks (in-place)
144   static const uintptr_t lock_mask_in_place       = right_n_bits(lock_bits) << lock_shift;
145   static const uintptr_t self_fwd_bit_in_place    = right_n_bits(self_fwd_bits) << self_fwd_shift;
146   static const uintptr_t age_mask_in_place        = right_n_bits(age_bits) << age_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 null_free_array_bit_in_place = right_n_bits(null_free_array_bits) << null_free_array_shift;
149   static const uintptr_t flat_array_bit_in_place  = right_n_bits(flat_array_bits) << flat_array_shift;
150   static const uintptr_t valhalla_reserved_bit_in_place = right_n_bits(valhalla_reserved_bits) << valhalla_reserved_shift;
151   static const uintptr_t hash_mask_in_place       = right_n_bits(hash_bits) << hash_shift;
152 
153   // Verify that _bit_in_place refers to constants with only one bit.
154   static_assert(is_power_of_2(self_fwd_bit_in_place));
155 #ifdef _LP64
156   static_assert(is_power_of_2(inline_type_bit_in_place));
157   static_assert(is_power_of_2(null_free_array_bit_in_place));
158   static_assert(is_power_of_2(flat_array_bit_in_place));
159   static_assert(is_power_of_2(valhalla_reserved_bit_in_place));
160 #endif
161 
162   // Masks (unshifted)
163   static const uintptr_t lock_mask                = lock_mask_in_place >> lock_shift;
164   static const uintptr_t age_mask                 = age_mask_in_place >> age_shift;
165   static const uintptr_t hash_mask                = hash_mask_in_place >> hash_shift;
166 
167 #ifdef _LP64
168   // Used only with compact headers:
169   // We store the (narrow) Klass* in the bits 43 to 64.
170 
171   // These are for bit-precise extraction of the narrow Klass* from the 64-bit markWord
172   static constexpr int klass_offset_in_bytes      = 4;
173   static constexpr int klass_shift                = hash_shift + hash_bits;
174   static constexpr int klass_shift_at_offset      = klass_shift - klass_offset_in_bytes * BitsPerByte;
175   static constexpr int klass_bits                 = 22;
176   static constexpr uintptr_t klass_mask           = right_n_bits(klass_bits);
177   static constexpr uintptr_t klass_mask_in_place  = klass_mask << klass_shift;
178 #endif
179 
180   static const uintptr_t locked_value             = 0;
181   static const uintptr_t unlocked_value           = 1;
182   static const uintptr_t monitor_value            = 2;
183   static const uintptr_t marked_value             = 3;
184 
185   static const uintptr_t inline_type_pattern      = inline_type_bit_in_place | unlocked_value;
186   static const uintptr_t inline_type_pattern_mask = inline_type_bit_in_place | lock_mask_in_place;
187 
188   static const uintptr_t no_hash                  = 0 ;  // no hash value assigned
189   static const uintptr_t no_hash_in_place         = (uintptr_t)no_hash << hash_shift;
190   static const uintptr_t no_lock_in_place         = unlocked_value;
191 
192   static const uint max_age                       = age_mask;
193 
194   // Creates a markWord with all bits set to zero.
195   static markWord zero() { return markWord(uintptr_t(0)); }
196 
197   bool is_inline_type() const {
198 #ifdef _LP64 // 64 bit encodings only
199     return (mask_bits(value(), inline_type_pattern_mask) == inline_type_pattern);
200 #else
201     return false;
202 #endif
203   }
204 
205   // lock accessors (note that these assume lock_shift == 0)
206   bool is_locked()   const {
207     return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
208   }
209   bool is_unlocked() const {
210     return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
211   }
212   bool is_marked()   const {
213     return (mask_bits(value(), lock_mask_in_place) == marked_value);
214   }
215 
216   bool is_neutral()  const {  // Not locked, or marked - a "clean" neutral state
217     LP64_ONLY(assert(!is_unlocked() || mask_bits(value(), inline_type_bit_in_place) == 0,
218                      "Inline types should not be used for locking. _value: " PTR_FORMAT, _value));
219     return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
220   }
221 
222   bool is_forwarded() const {
223     // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
224     return mask_bits(value(), lock_mask_in_place | self_fwd_bit_in_place) >= static_cast<intptr_t>(marked_value);
225   }
226 
227   // Should this header be preserved during GC?
228   bool must_be_preserved() const {
229     // The reserved bits are only guaranteed to be unset if the mark word is "unlocked"
230     LP64_ONLY(assert(!is_unlocked() || mask_bits(value(),  valhalla_reserved_bit_in_place) == 0,
231                      "Reserved bits should not be used. _value: " PTR_FORMAT, _value));
232     return !is_unlocked() || !has_no_hash();
233   }
234 
235   // WARNING: The following routines are used EXCLUSIVELY by
236   // synchronization functions. They are not really gc safe.
237   // They must get updated if markWord layout get changed.
238   markWord set_unlocked() const {
239     return markWord(value() | unlocked_value);
240   }
241 
242   bool is_fast_locked() const {
243     return (value() & lock_mask_in_place) == locked_value;
244   }
245   markWord set_fast_locked() const {
246     // Clear the lock_mask_in_place bits to set locked_value:
247     return markWord(value() & ~lock_mask_in_place);
248   }
249 
250   bool has_monitor() const {
251     return ((value() & lock_mask_in_place) == monitor_value);
252   }
253   markWord set_has_monitor() const {
254     return markWord((value() & ~lock_mask_in_place) | monitor_value);
255   }
256   ObjectMonitor* monitor() const {
257     assert(has_monitor(), "check");
258     assert(!UseObjectMonitorTable, "Locking with OM table does not use markWord for monitors");
259     // Use xor instead of &~ to provide one extra tag-bit check.
260     return (ObjectMonitor*) (value() ^ monitor_value);
261   }
262 
263   static markWord encode(ObjectMonitor* monitor) {
264     assert(!UseObjectMonitorTable, "Locking with OM table does not use markWord for monitors");
265     uintptr_t tmp = (uintptr_t) monitor;
266     return markWord(tmp | monitor_value);
267   }
268 
269   bool has_monitor_pointer() const {
270     intptr_t lockbits = value() & lock_mask_in_place;
271     return !UseObjectMonitorTable && lockbits == monitor_value;
272   }
273 
274   bool has_displaced_mark_helper() const {
275     return has_monitor_pointer();
276   }
277   markWord displaced_mark_helper() const;
278   void set_displaced_mark_helper(markWord m) const;
279 
280   // used to encode pointers during GC
281   markWord clear_lock_bits() const { return markWord(value() & ~lock_mask_in_place); }
282 
283   // age operations
284   markWord set_marked()   { return markWord((value() & ~lock_mask_in_place) | marked_value); }
285   markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
286 
287   uint     age()           const { return (uint) mask_bits(value() >> age_shift, age_mask); }
288   markWord set_age(uint v) const {
289     assert((v & ~age_mask) == 0, "shouldn't overflow age field");
290     return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
291   }
292   markWord incr_age()      const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
293 
294   // hash operations
295   intptr_t hash() const {
296     return mask_bits(value() >> hash_shift, hash_mask);
297   }
298 
299   bool has_no_hash() const {
300     return hash() == no_hash;
301   }
302 
303   bool is_flat_array() const {
304     assert(!has_monitor_pointer(), "Bits are not valid if replaced by a monitor pointer: " PTR_FORMAT, value());
305     assert(!is_marked(), "Bits might not be valid if marked by the GC: " PTR_FORMAT, value());
306 #ifdef _LP64 // 64 bit encodings only
307     return (mask_bits(value(), flat_array_bit_in_place) != 0);
308 #else
309     return false;
310 #endif
311   }
312 
313   bool is_null_free_array() const {
314     assert(!has_monitor_pointer(), "Bits are not valid if replaced by a monitor pointer: " PTR_FORMAT, value());
315     assert(!is_marked(), "Bits might not be valid if marked by the GC: " PTR_FORMAT, value());
316 #ifdef _LP64 // 64 bit encodings only
317     return (mask_bits(value(), null_free_array_bit_in_place) != 0);
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 marks for initialization
336 
337   static markWord prototype() {
338     return markWord(unlocked_value);
339   }
340 
341   static markWord inline_type_prototype() {
342     NOT_LP64(assert(false, "Should not be called in 32 bit mode"));
343     return markWord(unlocked_value | inline_type_bit_in_place);
344   }
345 
346   static markWord flat_array_prototype(bool null_free) {
347     NOT_LP64(assert(false, "Should not be called in 32 bit mode"));
348     if (null_free) {
349       return markWord(unlocked_value | flat_array_bit_in_place | null_free_array_bit_in_place);
350     } else {
351       return markWord(unlocked_value | flat_array_bit_in_place);
352     }
353   }
354 
355   static markWord null_free_array_prototype() {
356     NOT_LP64(assert(false, "Should not be called in 32 bit mode"));
357     return markWord(unlocked_value | null_free_array_bit_in_place);
358   }
359 
360   // Debugging
361   void print_on(outputStream* st, bool print_monitor_info = true) const;
362 
363   // Prepare address of oop for placement into mark
364   inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
365 
366   // Recover address of oop from encoded form used in mark
367   inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); }
368 
369   inline bool is_self_forwarded() const {
370     return mask_bits(value(), self_fwd_bit_in_place) != 0;
371   }
372 
373   inline markWord set_self_forwarded() const {
374     return markWord(value() | self_fwd_bit_in_place);
375   }
376 
377   inline markWord unset_self_forwarded() const {
378     return markWord(value() & ~self_fwd_bit_in_place);
379   }
380 
381   inline oop forwardee() const {
382     return cast_to_oop(decode_pointer());
383   }
384 };
385 
386 // Support atomic operations.
387 template<>
388 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
389   typedef markWord Value;
390   typedef uintptr_t Decayed;
391 
392   static Decayed decay(const Value& x) { return x.value(); }
393   static Value recover(Decayed x) { return Value(x); }
394 };
395 
396 #endif // SHARE_OOPS_MARKWORD_HPP