1 /*
  2  * Copyright (c) 1997, 2024, 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 "metaprogramming/primitiveConversions.hpp"
 29 #include "oops/compressedKlass.hpp"
 30 #include "oops/oopsHierarchy.hpp"
 31 #include "runtime/globals.hpp"
 32 
 33 #include <type_traits>
 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 (normal object)
 42 //
 43 //  64 bits:
 44 //  --------
 45 //  unused:22 hash:31 -->| unused_gap:4  age:4  self-fwd:1  lock:2 (normal object)
 46 //
 47 //  64 bits (with compact headers):
 48 //  -------------------------------
 49 //  unused:32 klass:19 hashctrl:2 -->| unused_gap:4  age:4  self-fwd:1  lock:2 (normal object)
 50 //
 51 //  Note: klass occupies bits 13-31 (19 bits), hashctrl occupies bits 11-12 (2 bits)
 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 //  - With +UseCompactObjectHeaders:
 59 //    hashctrl bits indicate if object has been hashed:
 60 //    00 - never hashed
 61 //    01 - hashed, but not expanded by GC: will recompute hash
 62 //    10 - not hashed, but expanded; special state used only by CDS to deal with scratch classes
 63 //    11 - hashed and expanded by GC, and hashcode has been installed in hidden field
 64 //
 65 //    When identityHashCode() is called, the transitions work as follows:
 66 //    00 - set the hashctrl bits to 01, and compute the identity hash
 67 //    01 - recompute idendity hash. When GC encounters 01 when moving an object, it will allocate an extra word, if
 68 //         necessary, for the object copy, and install 11.
 69 //    11 - read hashcode from field
 70 //
 71 //  - the two lock bits are used to describe three states: locked/unlocked and monitor.
 72 //
 73 //    [ptr             | 00]  locked             ptr points to real header on stack (stack-locking in use)
 74 //    [header          | 00]  locked             locked regular object header (fast-locking in use)
 75 //    [header          | 01]  unlocked           regular object header
 76 //    [ptr             | 10]  monitor            inflated lock (header is swapped out, UseObjectMonitorTable == false)
 77 //    [header          | 10]  monitor            inflated lock (UseObjectMonitorTable == true)
 78 //    [ptr             | 11]  marked             used to mark an object
 79 //    [0 ............ 0| 00]  inflating          inflation in progress (stack-locking in use)
 80 //
 81 //    We assume that stack/thread pointers have the lowest two bits cleared.
 82 //
 83 //  - INFLATING() is a distinguished markword value of all zeros that is
 84 //    used when inflating an existing stack-lock into an ObjectMonitor.
 85 //    See below for is_being_inflated() and INFLATING().
 86 
 87 class BasicLock;
 88 class ObjectMonitor;
 89 class JavaThread;
 90 class outputStream;
 91 
 92 class markWord {
 93  private:
 94   uintptr_t _value;
 95 
 96  public:
 97   explicit markWord(uintptr_t value) : _value(value) {}
 98 
 99   markWord() = default;         // Doesn't initialize _value.
100 
101   // It is critical for performance that this class be trivially
102   // destructable, copyable, and assignable.
103   ~markWord() = default;
104   markWord(const markWord&) = default;
105   markWord& operator=(const markWord&) = default;
106 
107   static markWord from_pointer(void* ptr) {
108     return markWord((uintptr_t)ptr);
109   }
110   void* to_pointer() const {
111     return (void*)_value;
112   }
113 
114   bool operator==(const markWord& other) const {
115     return _value == other._value;
116   }
117   bool operator!=(const markWord& other) const {
118     return !operator==(other);
119   }
120 
121   // Conversion
122   uintptr_t value() const { return _value; }
123   uint32_t value32() const { return (uint32_t)_value; }
124 
125   // Constants
126   static const int age_bits                       = 4;
127   static const int lock_bits                      = 2;
128   static const int self_fwd_bits                  = 1;
129   static const int max_hash_bits                  = BitsPerWord - age_bits - lock_bits - self_fwd_bits;
130   static const int hash_bits                      = max_hash_bits > 31 ? 31 : max_hash_bits;
131   static const int unused_gap_bits                = LP64_ONLY(4) NOT_LP64(0); // Reserved for Valhalla.
132   static const int hashctrl_bits                  = 2;
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 hash_shift                     = age_shift + age_bits + unused_gap_bits;
138   static const int hashctrl_shift                 = age_shift + age_bits + unused_gap_bits;
139 
140   static const uintptr_t lock_mask                = right_n_bits(lock_bits);
141   static const uintptr_t lock_mask_in_place       = lock_mask << lock_shift;
142   static const uintptr_t self_fwd_mask            = right_n_bits(self_fwd_bits);
143   static const uintptr_t self_fwd_mask_in_place   = self_fwd_mask << self_fwd_shift;
144   static const uintptr_t age_mask                 = right_n_bits(age_bits);
145   static const uintptr_t age_mask_in_place        = age_mask << age_shift;
146   static const uintptr_t hash_mask                = right_n_bits(hash_bits);
147   static const uintptr_t hash_mask_in_place       = hash_mask << hash_shift;
148   static const uintptr_t hashctrl_mask            = right_n_bits(hashctrl_bits);
149   static const uintptr_t hashctrl_mask_in_place   = hashctrl_mask << hashctrl_shift;
150   static const uintptr_t hashctrl_hashed_mask_in_place    = ((uintptr_t)1) << hashctrl_shift;
151   static const uintptr_t hashctrl_expanded_mask_in_place  = ((uintptr_t)2) << hashctrl_shift;
152 
153 #ifdef _LP64
154   // Used only with compact headers:
155   // With UseCompactObjectHeaders: We store the (narrow) Klass* in bits 13-31 (19 bits total).
156   // Without UseCompactObjectHeaders: Klass* is stored separately in object header, not in markword.
157 
158   // These are for bit-precise extraction of the narrow Klass* from the markword (UseCompactObjectHeaders only)
159   //
160   // Bit position summary for UseCompactObjectHeaders:
161   // Bits  0- 1: lock (2 bits)
162   // Bit   2   : self-fwd (1 bit)
163   // Bits  3- 6: age (4 bits)
164   // Bits  7-10: unused_gap (4 bits)
165   // Bits 11-12: hashctrl (2 bits) - hash control state
166   // Bits 13-31: klass (19 bits) - narrow klass pointer
167   // Bits 32-63: unused (32 bits)
168   //
169   // Without UseCompactObjectHeaders, klass is stored separately in object header
170   static constexpr int klass_shift                = hashctrl_shift + hashctrl_bits;
171   static constexpr int klass_bits                 = 19;
172   static constexpr uintptr_t klass_mask           = right_n_bits(klass_bits);
173   static constexpr uintptr_t klass_mask_in_place  = klass_mask << klass_shift;
174 #endif
175 
176 
177   static const uintptr_t locked_value             = 0;
178   static const uintptr_t unlocked_value           = 1;
179   static const uintptr_t monitor_value            = 2;
180   static const uintptr_t marked_value             = 3;
181   static const uintptr_t forward_expanded_value   = 0b111;
182 
183   static const uintptr_t no_hash                  = 0 ;  // no hash value assigned
184   static const uintptr_t no_hash_in_place         = (uintptr_t)no_hash << hash_shift;
185   static const uintptr_t no_lock_in_place         = unlocked_value;
186 
187   static const uint max_age                       = age_mask;
188 
189   // Creates a markWord with all bits set to zero.
190   static markWord zero() { return markWord(uintptr_t(0)); }
191 
192   // lock accessors (note that these assume lock_shift == 0)
193   bool is_locked()   const {
194     return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
195   }
196   bool is_unlocked() const {
197     return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
198   }
199   bool is_marked()   const {
200     return (value() & (self_fwd_mask_in_place | lock_mask_in_place)) > monitor_value;
201   }
202   bool is_forwarded() const {
203     // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
204     return mask_bits(value(), lock_mask_in_place | self_fwd_mask_in_place) >= static_cast<intptr_t>(marked_value);
205   }
206   bool is_neutral()  const {  // Not locked, or marked - a "clean" neutral state
207     return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
208   }
209 
210   markWord set_forward_expanded() {
211     assert((value() & (lock_mask_in_place | self_fwd_mask_in_place)) == marked_value, "must be normal-forwarded here");
212     return markWord(value() | forward_expanded_value);
213   }
214   bool is_forward_expanded() {
215     return (value() & (lock_mask_in_place | self_fwd_mask_in_place)) == forward_expanded_value;
216   }
217 
218   // Special temporary state of the markWord while being inflated.
219   // Code that looks at mark outside a lock need to take this into account.
220   bool is_being_inflated() const { return (value() == 0); }
221 
222   // Distinguished markword value - used when inflating over
223   // an existing stack-lock.  0 indicates the markword is "BUSY".
224   // Lockword mutators that use a LD...CAS idiom should always
225   // check for and avoid overwriting a 0 value installed by some
226   // other thread.  (They should spin or block instead.  The 0 value
227   // is transient and *should* be short-lived).
228   // Fast-locking does not use INFLATING.
229   static markWord INFLATING() { return zero(); }    // inflate-in-progress
230 
231   // Should this header be preserved during GC?
232   bool must_be_preserved() const {
233     return UseCompactObjectHeaders ? !is_unlocked() : (!is_unlocked() || !has_no_hash());
234   }
235 
236   // WARNING: The following routines are used EXCLUSIVELY by
237   // synchronization functions. They are not really gc safe.
238   // They must get updated if markWord layout get changed.
239   markWord set_unlocked() const {
240     return markWord(value() | unlocked_value);
241   }
242   bool has_locker() const {
243     assert(LockingMode == LM_LEGACY, "should only be called with legacy stack locking");
244     return (value() & lock_mask_in_place) == locked_value;
245   }
246   BasicLock* locker() const {
247     assert(has_locker(), "check");
248     return (BasicLock*) value();
249   }
250 
251   bool is_fast_locked() const {
252     assert(LockingMode == LM_LIGHTWEIGHT, "should only be called with new lightweight locking");
253     return (value() & lock_mask_in_place) == locked_value;
254   }
255   markWord set_fast_locked() const {
256     // Clear the lock_mask_in_place bits to set locked_value:
257     return markWord(value() & ~lock_mask_in_place);
258   }
259 
260   bool has_monitor() const {
261     return ((value() & lock_mask_in_place) == monitor_value);
262   }
263   ObjectMonitor* monitor() const {
264     assert(has_monitor(), "check");
265     assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors");
266     // Use xor instead of &~ to provide one extra tag-bit check.
267     return (ObjectMonitor*) (value() ^ monitor_value);
268   }
269   bool has_displaced_mark_helper() const {
270     intptr_t lockbits = value() & lock_mask_in_place;
271     if (LockingMode == LM_LIGHTWEIGHT) {
272       return !UseObjectMonitorTable && lockbits == monitor_value;
273     }
274     // monitor (0b10) | stack-locked (0b00)?
275     return (lockbits & unlocked_value) == 0;
276   }
277   markWord displaced_mark_helper() const;
278   void set_displaced_mark_helper(markWord m) const;
279   markWord copy_set_hash(intptr_t hash) const {
280     assert(!UseCompactObjectHeaders, "Do not use with compact i-hash");
281     uintptr_t tmp = value() & (~hash_mask_in_place);
282     tmp |= ((hash & hash_mask) << hash_shift);
283     return markWord(tmp);
284   }
285   // it is only used to be stored into BasicLock as the
286   // indicator that the lock is using heavyweight monitor
287   static markWord unused_mark() {
288     return markWord(marked_value);
289   }
290   // the following two functions create the markWord to be
291   // stored into object header, it encodes monitor info
292   static markWord encode(BasicLock* lock) {
293     return from_pointer(lock);
294   }
295   static markWord encode(ObjectMonitor* monitor) {
296     assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors");
297     uintptr_t tmp = (uintptr_t) monitor;
298     return markWord(tmp | monitor_value);
299   }
300 
301   markWord set_has_monitor() const {
302     return markWord((value() & ~lock_mask_in_place) | monitor_value);
303   }
304 
305   // used to encode pointers during GC
306   markWord clear_lock_bits() const { return markWord(value() & ~(lock_mask_in_place | self_fwd_mask_in_place)); }
307 
308   // age operations
309   markWord set_marked()   { return markWord((value() & ~lock_mask_in_place) | marked_value); }
310   markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
311 
312   uint     age()           const { return (uint) mask_bits(value() >> age_shift, age_mask); }
313   markWord set_age(uint v) const {
314     assert((v & ~age_mask) == 0, "shouldn't overflow age field");
315     return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
316   }
317   markWord incr_age()      const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
318 
319   // hash operations
320   intptr_t hash() const {
321     assert(!UseCompactObjectHeaders, "only without compact i-hash");
322     return mask_bits(value() >> hash_shift, hash_mask);
323   }
324 
325   bool has_no_hash() const {
326     if (UseCompactObjectHeaders) {
327       return !is_hashed();
328     } else {
329       return hash() == no_hash;
330     }
331   }
332 
333   inline bool is_hashed_not_expanded() const {
334     assert(UseCompactObjectHeaders, "only with compact i-hash");
335     return (value() & hashctrl_mask_in_place) == hashctrl_hashed_mask_in_place;
336   }
337   inline markWord set_hashed_not_expanded() const {
338     assert(UseCompactObjectHeaders, "only with compact i-hash");
339     return markWord((value() & ~hashctrl_mask_in_place) | hashctrl_hashed_mask_in_place);
340   }
341 
342   inline bool is_hashed_expanded() const {
343     assert(UseCompactObjectHeaders, "only with compact i-hash");
344     return (value() & hashctrl_mask_in_place) == (hashctrl_hashed_mask_in_place | hashctrl_expanded_mask_in_place);
345   }
346   inline markWord set_hashed_expanded() const {
347     assert(UseCompactObjectHeaders, "only with compact i-hash");
348     return markWord((value() & ~hashctrl_mask_in_place) | (hashctrl_hashed_mask_in_place | hashctrl_expanded_mask_in_place));
349   }
350 
351   // This is a special hashctrl state (11) that is only used
352   // during CDS archive dumping. There we allocate 'scratch mirrors' for
353   // each real mirror klass. We allocate those scratch mirrors
354   // in a pre-extended form, but without being hashed. When the
355   // real mirror gets hashed, then we turn the scratch mirror into
356   // hashed_moved state, otherwise we leave it in that special state
357   // which indicates that the archived copy will be allocated in the
358   // unhashed form.
359   inline bool is_not_hashed_expanded() const {
360     assert(UseCompactObjectHeaders, "only with compact i-hash");
361     return (value() & hashctrl_mask_in_place) == hashctrl_expanded_mask_in_place;
362   }
363   inline markWord set_not_hashed_expanded() const {
364     assert(UseCompactObjectHeaders, "only with compact i-hash");
365     return markWord((value() & ~hashctrl_mask_in_place) | hashctrl_expanded_mask_in_place);
366   }
367   // Return true when object is either hashed_moved or not_hashed_moved.
368   inline bool is_expanded() const {
369     assert(UseCompactObjectHeaders, "only with compact i-hash");
370     return (value() & hashctrl_expanded_mask_in_place) != 0;
371   }
372   inline bool is_hashed() const {
373     assert(UseCompactObjectHeaders, "only with compact i-hash");
374     return (value() & hashctrl_hashed_mask_in_place) != 0;
375   }
376 
377   inline markWord copy_hashctrl_from(markWord m) const {
378     if (UseCompactObjectHeaders) {
379       return markWord((value() & ~hashctrl_mask_in_place) | (m.value() & hashctrl_mask_in_place));
380     } else {
381       return markWord(value());
382     }
383   }
384 
385   inline Klass* klass() const;
386   inline Klass* klass_or_null() const;
387   inline Klass* klass_without_asserts() const;
388   inline narrowKlass narrow_klass() const;
389   inline markWord set_narrow_klass(narrowKlass narrow_klass) const;
390 
391 #ifdef _LP64
392   inline int array_length() { return checked_cast<int>(value() >> 32); }
393 #endif
394 
395   // Prototype mark for initialization
396   static markWord prototype() {
397     if (UseCompactObjectHeaders) {
398       return markWord(no_lock_in_place);
399     } else {
400       return markWord(no_hash_in_place | no_lock_in_place);
401     }
402   }
403 
404   // Debugging
405   void print_on(outputStream* st, bool print_monitor_info = true) const;
406 
407   // Prepare address of oop for placement into mark
408   inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
409 
410   // Recover address of oop from encoded form used in mark
411   inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); }
412 
413   inline bool is_self_forwarded() const {
414     NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");)
415     // Match 100, 101, 110 but not 111.
416     return mask_bits(value() + 1, (lock_mask_in_place | self_fwd_mask_in_place)) > 4;
417   }
418 
419   inline markWord set_self_forwarded() const {
420     NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");)
421     return markWord(value() | self_fwd_mask_in_place);
422   }
423 
424   inline markWord unset_self_forwarded() const {
425     NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");)
426     return markWord(value() & ~self_fwd_mask_in_place);
427   }
428 
429   inline oop forwardee() const {
430     return cast_to_oop(decode_pointer());
431   }
432 };
433 
434 // Support atomic operations.
435 template<>
436 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
437   typedef markWord Value;
438   typedef uintptr_t Decayed;
439 
440   static Decayed decay(const Value& x) { return x.value(); }
441   static Value recover(Decayed x) { return Value(x); }
442 };
443 
444 #endif // SHARE_OOPS_MARKWORD_HPP