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