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 "layoutKind.hpp"
 29 #include "metaprogramming/primitiveConversions.hpp"
 30 #include "oops/compressedKlass.hpp"
 31 #include "oops/oopsHierarchy.hpp"
 32 #include "runtime/globals.hpp"
 33 #include "utilities/vmEnums.hpp"
 34 
 35 #include <type_traits>
 36 
 37 // The markWord describes the header of an object.
 38 //
 39 // Bit-format of an object header (most significant first, big endian layout below):
 40 //
 41 //  32 bits:
 42 //  --------
 43 //             hash:25 ------------>| age:4  self-fwd:1  lock:2 (normal object)
 44 //
 45 //  64 bits:
 46 //  --------
 47 //  unused:22 hash:31 -->| unused_gap:4  age:4  self-fwd:1  lock:2 (normal object)
 48 //
 49 //  64 bits (with compact headers):
 50 //  -------------------------------
 51 //  klass:22  hash:31 -->| unused_gap:4  age:4  self-fwd:1  lock:2 (normal object)
 52 //
 53 //  - hash contains the identity hash value: largest value is
 54 //    31 bits, see os::random().  Also, 64-bit vm's require
 55 //    a hash value no bigger than 32 bits because they will not
 56 //    properly generate a mask larger than that: see library_call.cpp
 57 //
 58 //  - the two lock bits are used to describe three states: locked/unlocked and monitor.
 59 //
 60 //    [ptr             | 00]  locked             ptr points to real header on stack (stack-locking in use)
 61 //    [header          | 00]  locked             locked regular object header (fast-locking in use)
 62 //    [header          | 01]  unlocked           regular object header
 63 //    [ptr             | 10]  monitor            inflated lock (header is swapped out, UseObjectMonitorTable == false)
 64 //    [header          | 10]  monitor            inflated lock (UseObjectMonitorTable == true)
 65 //    [ptr             | 11]  marked             used to mark an object
 66 //    [0 ............ 0| 00]  inflating          inflation in progress (stack-locking in use)
 67 //
 68 //    We assume that stack/thread pointers have the lowest two bits cleared.
 69 //
 70 //
 71 //  - INFLATING() is a distinguished markword value of all zeros that is
 72 //    used when inflating an existing stack-lock into an ObjectMonitor.
 73 //    See below for is_being_inflated() and INFLATING().
 74 //
 75 //
 76 //
 77 //  Valhalla
 78 //
 79 //  <CMH: merge this doc into the text above>
 80 //
 81 //  Project Valhalla has mark word encoding requirements for the following oops:
 82 //
 83 //  * inline types: have alternative bytecode behavior, e.g. can not be locked
 84 //    - "larval state": mutable state, but only during object init, observable
 85 //      by only by a single thread (generally do not mutate markWord)
 86 //
 87 //  * flat arrays: load/decode of klass layout helper is expensive for aaload
 88 //
 89 //  * "null free" arrays: load/decode of klass layout helper again for aaload
 90 //
 91 //  EnableValhalla
 92 //
 93 //  Formerly known as "biased lock bit", "unused_gap" is free to use: using this
 94 //  bit to indicate inline type, combined with "unlocked" lock bits, means we
 95 //  will not interfere with lock encodings (displaced, inflating, and monitor),
 96 //  since inline types can't be locked.
 97 //
 98 //  Further state encoding
 99 //
100 //  32 bit plaforms currently have no further room for encoding. No room for
101 //  "denormalized layout helper bits", these fast mark word tests can only be made on
102 //  64 bit platforms. 32-bit platforms need to load the klass->_layout_helper. This
103 //  said, the larval state bit is still required for operation, stealing from the hash
104 //  code is simplest mechanism.
105 //
106 //  Valhalla specific encodings
107 //
108 //  Revised Bit-format of an object header (most significant first, big endian layout below):
109 //
110 //  32 bits:
111 //  --------
112 //  hash:24 ------------>| larval:1 age:4 inline_type:1 lock:2
113 //
114 //  64 bits:
115 //  --------
116 //  unused:1 | <-- hash:31 -->| unused:22 larval:1 age:4 flat_array:1 null_free_array:1 inline_type:1 lock:2
117 //  klass:22  hash:31 -->| larval:1 age:4 flat_array:1 null_free_array:1 inline_type:1 self-fwd:1 lock:2 (normal object)
118 //
119 //  The "fast" static type bits (flat_array, null_free_array, and inline_type)
120 //  are placed lowest next to lock bits to more easily decode forwarding pointers.
121 //  G1 for example, implicitly clears age bits ("G1FullGCCompactionPoint::forward()")
122 //  using "oopDesc->forwardee()", so it necessary for "markWord::decode_pointer()"
123 //  to return a non-nullptr for this case, but not confuse the static type bits for
124 //  a pointer.
125 //
126 //  Note the position of 'self-fwd' is not by accident. When forwarding an
127 //  object to a new heap position, HeapWord alignment guarantees the lower
128 //  bits, including 'self-fwd' are 0. "is_self_forwarded()" will be correctly
129 //  set to false. Otherwise encode_pointer_as_mark() may have 'self-fwd' set.
130 //
131 //
132 //  Static types bits are recorded in the "klass->prototype_header()", displaced
133 //  mark should simply use the prototype header as "slow path", rather chasing
134 //  monitor or stack lock races.
135 //
136 //  Lock patterns (note inline types can't be locked/monitor/inflating)...
137 //
138 //  [ptr            | 000]  locked             ptr points to real header on stack
139 //  [header         | ?01]  unlocked           regular object header
140 //  [ptr            | 010]  monitor            inflated lock (header is wapped out)
141 //  [ptr            | ?11]  marked             used to mark an object
142 //  [0 ............ | 000]  inflating          inflation in progress
143 //
144 //
145 
146 class BasicLock;
147 class ObjectMonitor;
148 class JavaThread;
149 class outputStream;
150 
151 class markWord {
152  private:
153   uintptr_t _value;
154 
155  public:
156   explicit markWord(uintptr_t value) : _value(value) {}
157 
158   markWord() = default;         // Doesn't initialize _value.
159 
160   // It is critical for performance that this class be trivially
161   // destructable, copyable, and assignable.
162   ~markWord() = default;
163   markWord(const markWord&) = default;
164   markWord& operator=(const markWord&) = default;
165 
166   static markWord from_pointer(void* ptr) {
167     return markWord((uintptr_t)ptr);
168   }
169   void* to_pointer() const {
170     return (void*)_value;
171   }
172 
173   bool operator==(const markWord& other) const {
174     return _value == other._value;
175   }
176   bool operator!=(const markWord& other) const {
177     return !operator==(other);
178   }
179 
180   // Conversion
181   uintptr_t value() const { return _value; }
182 
183   // Constants, in least significant bit order
184   static const int lock_bits                      = 2;
185   static const int self_fwd_bits                  = 1;
186   // EnableValhalla: static prototype header bits (fast path instead of klass layout_helper)
187   static const int inline_type_bits               = 1;
188   static const int null_free_array_bits           = LP64_ONLY(1) NOT_LP64(0);
189   static const int flat_array_bits                = LP64_ONLY(1) NOT_LP64(0);
190   // instance state
191   static const int age_bits                       = 4;
192   static const int larval_bits                    = 1;
193   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;
194   static const int hash_bits                      = max_hash_bits > 31 ? 31 : max_hash_bits;
195 
196   static const int lock_shift                     = 0;
197   static const int self_fwd_shift                 = lock_bits ;
198   static const int inline_type_shift              = self_fwd_shift + self_fwd_bits;
199   static const int null_free_array_shift          = inline_type_shift + inline_type_bits;
200   static const int flat_array_shift               = null_free_array_shift + null_free_array_bits;
201   static const int age_shift                      = flat_array_shift + flat_array_bits;
202   static const int larval_shift                   = age_shift + age_bits;
203   static const int hash_shift                     = larval_shift + larval_bits;
204 
205   static const uintptr_t lock_mask                = right_n_bits(lock_bits);
206   static const uintptr_t lock_mask_in_place       = lock_mask << lock_shift;
207   static const uintptr_t self_fwd_mask            = right_n_bits(self_fwd_bits);
208   static const uintptr_t self_fwd_mask_in_place   = self_fwd_mask << self_fwd_shift;
209   static const uintptr_t inline_type_bit_in_place = 1 << inline_type_shift;
210   static const uintptr_t inline_type_mask         = inline_type_bit_in_place + lock_mask;
211   static const uintptr_t inline_type_mask_in_place = inline_type_mask << lock_shift;
212   static const uintptr_t null_free_array_mask     = right_n_bits(null_free_array_bits);
213   static const uintptr_t null_free_array_mask_in_place = (null_free_array_mask << null_free_array_shift) | lock_mask_in_place;
214   static const uintptr_t null_free_array_bit_in_place  = (1 << null_free_array_shift);
215   static const uintptr_t flat_array_mask          = right_n_bits(flat_array_bits);
216   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;
217   static const uintptr_t flat_array_bit_in_place  = (1 << flat_array_shift);
218   static const uintptr_t age_mask                 = right_n_bits(age_bits);
219   static const uintptr_t age_mask_in_place        = age_mask << age_shift;
220 
221   static const uintptr_t larval_mask              = right_n_bits(larval_bits);
222   static const uintptr_t larval_mask_in_place     = (larval_mask << larval_shift) | inline_type_mask_in_place;
223   static const uintptr_t larval_bit_in_place      = (1 << larval_shift);
224 
225   static const uintptr_t hash_mask                = right_n_bits(hash_bits);
226   static const uintptr_t hash_mask_in_place       = hash_mask << hash_shift;
227 
228 #ifdef _LP64
229   // Used only with compact headers:
230   // We store the (narrow) Klass* in the bits 43 to 64.
231 
232   // These are for bit-precise extraction of the narrow Klass* from the 64-bit Markword
233   static constexpr int klass_offset_in_bytes      = 4;
234   static constexpr int klass_shift                = hash_shift + hash_bits;
235   static constexpr int klass_shift_at_offset      = klass_shift - klass_offset_in_bytes * BitsPerByte;
236   static constexpr int klass_bits                 = 22;
237   static constexpr uintptr_t klass_mask           = right_n_bits(klass_bits);
238   static constexpr uintptr_t klass_mask_in_place  = klass_mask << klass_shift;
239 #endif
240 
241 
242   static const uintptr_t locked_value             = 0;
243   static const uintptr_t unlocked_value           = 1;
244   static const uintptr_t monitor_value            = 2;
245   static const uintptr_t marked_value             = 3;
246 
247   static const uintptr_t inline_type_pattern      = inline_type_bit_in_place | unlocked_value;
248   static const uintptr_t null_free_array_pattern  = null_free_array_bit_in_place | unlocked_value;
249   static const uintptr_t null_free_flat_array_pattern = flat_array_bit_in_place | null_free_array_pattern;
250   static const uintptr_t nullable_flat_array_pattern = flat_array_bit_in_place | unlocked_value;
251 
252   // Has static klass prototype, used for decode/encode pointer
253   static const uintptr_t static_prototype_mask    = LP64_ONLY(right_n_bits(inline_type_bits + flat_array_bits + null_free_array_bits)) NOT_LP64(right_n_bits(inline_type_bits));
254   static const uintptr_t static_prototype_mask_in_place = static_prototype_mask << lock_bits;
255   static const uintptr_t static_prototype_value_max = (1 << age_shift) - 1;
256 
257   static const uintptr_t larval_pattern           = larval_bit_in_place | inline_type_pattern;
258 
259   static const uintptr_t no_hash                  = 0 ;  // no hash value assigned
260   static const uintptr_t no_hash_in_place         = (uintptr_t)no_hash << hash_shift;
261   static const uintptr_t no_lock_in_place         = unlocked_value;
262 
263   static const uint max_age                       = age_mask;
264 
265   // Creates a markWord with all bits set to zero.
266   static markWord zero() { return markWord(uintptr_t(0)); }
267 
268   bool is_inline_type() const {
269     return (mask_bits(value(), inline_type_mask_in_place) == inline_type_pattern);
270   }
271 
272   // lock accessors (note that these assume lock_shift == 0)
273   bool is_locked()   const {
274     return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
275   }
276   bool is_unlocked() const {
277     return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
278   }
279   bool is_marked()   const {
280     return (mask_bits(value(), lock_mask_in_place) == marked_value);
281   }
282 
283   // is unlocked and not an inline type (which cannot be involved in locking, displacement or inflation)
284   // i.e. test both lock bits and the inline type bit together
285   bool is_neutral()  const {  // Not locked, or marked - a "clean" neutral state
286     return (mask_bits(value(), inline_type_mask_in_place) == unlocked_value);
287   }
288 
289   bool is_forwarded() const {
290     // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
291     return mask_bits(value(), lock_mask_in_place | self_fwd_mask_in_place) >= static_cast<intptr_t>(marked_value);
292   }
293 
294   // Special temporary state of the markWord while being inflated.
295   // Code that looks at mark outside a lock need to take this into account.
296   bool is_being_inflated() const { return (value() == 0); }
297 
298   // Distinguished markword value - used when inflating over
299   // an existing stack-lock.  0 indicates the markword is "BUSY".
300   // Lockword mutators that use a LD...CAS idiom should always
301   // check for and avoid overwriting a 0 value installed by some
302   // other thread.  (They should spin or block instead.  The 0 value
303   // is transient and *should* be short-lived).
304   // Fast-locking does not use INFLATING.
305   static markWord INFLATING() { return zero(); }    // inflate-in-progress
306 
307   // Should this header be preserved during GC?
308   bool must_be_preserved() const {
309     return (!is_unlocked() || !has_no_hash() ||
310       (EnableValhalla && (is_larval_state() || is_inline_type() || is_flat_array() || is_null_free_array())));
311   }
312 
313   // WARNING: The following routines are used EXCLUSIVELY by
314   // synchronization functions. They are not really gc safe.
315   // They must get updated if markWord layout get changed.
316   markWord set_unlocked() const {
317     return markWord(value() | unlocked_value);
318   }
319   bool has_locker() const {
320     assert(LockingMode == LM_LEGACY, "should only be called with legacy stack locking");
321     return (value() & lock_mask_in_place) == locked_value;
322   }
323   BasicLock* locker() const {
324     assert(has_locker(), "check");
325     return (BasicLock*) value();
326   }
327 
328   bool is_fast_locked() const {
329     assert(LockingMode == LM_LIGHTWEIGHT, "should only be called with new lightweight locking");
330     return (value() & lock_mask_in_place) == locked_value;
331   }
332   markWord set_fast_locked() const {
333     // Clear the lock_mask_in_place bits to set locked_value:
334     return markWord(value() & ~lock_mask_in_place);
335   }
336 
337   bool has_monitor() const {
338     return ((value() & lock_mask_in_place) == monitor_value);
339   }
340   ObjectMonitor* monitor() const {
341     assert(has_monitor(), "check");
342     assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors");
343     // Use xor instead of &~ to provide one extra tag-bit check.
344     return (ObjectMonitor*) (value() ^ monitor_value);
345   }
346   bool has_displaced_mark_helper() const {
347     intptr_t lockbits = value() & lock_mask_in_place;
348     if (LockingMode == LM_LIGHTWEIGHT) {
349       return !UseObjectMonitorTable && lockbits == monitor_value;
350     }
351     // monitor (0b10) | stack-locked (0b00)?
352     return (lockbits & unlocked_value) == 0;
353   }
354   markWord displaced_mark_helper() const;
355   void set_displaced_mark_helper(markWord m) const;
356   markWord copy_set_hash(intptr_t hash) const {
357     uintptr_t tmp = value() & (~hash_mask_in_place);
358     tmp |= ((hash & hash_mask) << hash_shift);
359     return markWord(tmp);
360   }
361   // it is only used to be stored into BasicLock as the
362   // indicator that the lock is using heavyweight monitor
363   static markWord unused_mark() {
364     return markWord(marked_value);
365   }
366   // the following two functions create the markWord to be
367   // stored into object header, it encodes monitor info
368   static markWord encode(BasicLock* lock) {
369     return from_pointer(lock);
370   }
371   static markWord encode(ObjectMonitor* monitor) {
372     assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors");
373     uintptr_t tmp = (uintptr_t) monitor;
374     return markWord(tmp | monitor_value);
375   }
376 
377   markWord set_has_monitor() const {
378     return markWord((value() & ~lock_mask_in_place) | monitor_value);
379   }
380 
381   // used to encode pointers during GC
382   markWord clear_lock_bits() const { return markWord(value() & ~lock_mask_in_place); }
383 
384   // age operations
385   markWord set_marked()   { return markWord((value() & ~lock_mask_in_place) | marked_value); }
386   markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
387 
388   uint     age()           const { return (uint) mask_bits(value() >> age_shift, age_mask); }
389   markWord set_age(uint v) const {
390     assert((v & ~age_mask) == 0, "shouldn't overflow age field");
391     return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
392   }
393   markWord incr_age()      const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
394 
395   // hash operations
396   intptr_t hash() const {
397     return mask_bits(value() >> hash_shift, hash_mask);
398   }
399 
400   bool has_no_hash() const {
401     return hash() == no_hash;
402   }
403 
404   // private buffered value operations
405   markWord enter_larval_state() const {
406     return markWord(value() | larval_bit_in_place);
407   }
408   markWord exit_larval_state() const {
409     return markWord(value() & ~larval_bit_in_place);
410   }
411   bool is_larval_state() const {
412     return (mask_bits(value(), larval_mask_in_place) == larval_pattern);
413   }
414 
415   bool is_flat_array() const {
416 #ifdef _LP64 // 64 bit encodings only
417     return (mask_bits(value(), flat_array_mask_in_place) == null_free_flat_array_pattern)
418            || (mask_bits(value(), flat_array_mask_in_place) == nullable_flat_array_pattern);
419 #else
420     return false;
421 #endif
422   }
423 
424   bool is_null_free_array() const {
425 #ifdef _LP64 // 64 bit encodings only
426     return (mask_bits(value(), null_free_array_mask_in_place) == null_free_array_pattern);
427 #else
428     return false;
429 #endif
430   }
431 
432   inline Klass* klass() const;
433   inline Klass* klass_or_null() const;
434   inline Klass* klass_without_asserts() const;
435   inline narrowKlass narrow_klass() const;
436   inline markWord set_narrow_klass(narrowKlass narrow_klass) const;
437 
438   // Prototype mark for initialization
439   static markWord prototype() {
440     return markWord( no_hash_in_place | no_lock_in_place );
441   }
442 
443   static markWord inline_type_prototype() {
444     return markWord(inline_type_pattern);
445   }
446 
447 #ifdef _LP64 // 64 bit encodings only
448   static markWord flat_array_prototype(LayoutKind lk);
449 
450   static markWord null_free_array_prototype() {
451     return markWord(null_free_array_pattern);
452   }
453 #endif
454 
455   // Debugging
456   void print_on(outputStream* st, bool print_monitor_info = true) const;
457 
458   // Prepare address of oop for placement into mark
459   inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
460 
461   // Recover address of oop from encoded form used in mark
462   inline void* decode_pointer() const {
463     return (EnableValhalla && _value < static_prototype_value_max) ? nullptr :
464       (void*) (clear_lock_bits().value());
465   }
466 
467   inline bool is_self_forwarded() const {
468     NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");)
469     return mask_bits(value(), self_fwd_mask_in_place) != 0;
470   }
471 
472   inline markWord set_self_forwarded() const {
473     NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");)
474     return markWord(value() | self_fwd_mask_in_place);
475   }
476 
477   inline markWord unset_self_forwarded() const {
478     NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");)
479     return markWord(value() & ~self_fwd_mask_in_place);
480   }
481 
482   inline oop forwardee() const {
483     return cast_to_oop(decode_pointer());
484   }
485 };
486 
487 // Support atomic operations.
488 template<>
489 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
490   typedef markWord Value;
491   typedef uintptr_t Decayed;
492 
493   static Decayed decay(const Value& x) { return x.value(); }
494   static Value recover(Decayed x) { return Value(x); }
495 };
496 
497 #endif // SHARE_OOPS_MARKWORD_HPP