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/oopsHierarchy.hpp"
 30 #include "runtime/globals.hpp"
 31 
 32 #include <type_traits>
 33 
 34 // The markWord describes the header of an object.
 35 //
 36 // Bit-format of an object header (most significant first, big endian layout below):
 37 //
 38 //  32 bits:
 39 //  --------
 40 //             hash:25 ------------>| age:4  unused_gap:1  lock:2 (normal object)
 41 //
 42 //  64 bits:
 43 //  --------
 44 //  unused:25 hash:31 -->| unused_gap:1  age:4  unused_gap:1  lock:2 (normal object)
 45 //
 46 //  - hash contains the identity hash value: largest value is
 47 //    31 bits, see os::random().  Also, 64-bit vm's require
 48 //    a hash value no bigger than 32 bits because they will not
 49 //    properly generate a mask larger than that: see library_call.cpp
 50 //
 51 //  - the two lock bits are used to describe three states: locked/unlocked and monitor.
 52 //
 53 //    [ptr             | 00]  locked             ptr points to real header on stack (stack-locking in use)
 54 //    [header          | 00]  locked             locked regular object header (fast-locking in use)
 55 //    [header          | 01]  unlocked           regular object header
 56 //    [ptr             | 10]  monitor            inflated lock (header is swapped out, UseObjectMonitorTable == false)
 57 //    [header          | 10]  monitor            inflated lock (UseObjectMonitorTable == true)
 58 //    [ptr             | 11]  marked             used to mark an object
 59 //    [0 ............ 0| 00]  inflating          inflation in progress (stack-locking in use)
 60 //
 61 //    We assume that stack/thread pointers have the lowest two bits cleared.
 62 //
 63 //  - INFLATING() is a distinguished markword value of all zeros that is
 64 //    used when inflating an existing stack-lock into an ObjectMonitor.
 65 //    See below for is_being_inflated() and INFLATING().
 66 
 67 class BasicLock;
 68 class ObjectMonitor;
 69 class JavaThread;
 70 class outputStream;
 71 
 72 class markWord {
 73  private:
 74   uintptr_t _value;
 75 
 76  public:
 77   explicit markWord(uintptr_t value) : _value(value) {}
 78 
 79   markWord() = default;         // Doesn't initialize _value.
 80 
 81   // It is critical for performance that this class be trivially
 82   // destructable, copyable, and assignable.
 83   ~markWord() = default;
 84   markWord(const markWord&) = default;
 85   markWord& operator=(const markWord&) = default;
 86 
 87   static markWord from_pointer(void* ptr) {
 88     return markWord((uintptr_t)ptr);
 89   }
 90   void* to_pointer() const {
 91     return (void*)_value;
 92   }
 93 
 94   bool operator==(const markWord& other) const {
 95     return _value == other._value;
 96   }
 97   bool operator!=(const markWord& other) const {
 98     return !operator==(other);
 99   }
100 
101   // Conversion
102   uintptr_t value() const { return _value; }
103 
104   // Constants
105   static const int age_bits                       = 4;
106   static const int lock_bits                      = 2;
107   static const int first_unused_gap_bits          = 1;
108   static const int max_hash_bits                  = BitsPerWord - age_bits - lock_bits - first_unused_gap_bits;
109   static const int hash_bits                      = max_hash_bits > 31 ? 31 : max_hash_bits;
110   static const int second_unused_gap_bits         = LP64_ONLY(1) NOT_LP64(0);
111 
112   static const int lock_shift                     = 0;
113   static const int age_shift                      = lock_bits + first_unused_gap_bits;
114   static const int hash_shift                     = age_shift + age_bits + second_unused_gap_bits;
115 
116   static const uintptr_t lock_mask                = right_n_bits(lock_bits);
117   static const uintptr_t lock_mask_in_place       = lock_mask << lock_shift;
118   static const uintptr_t age_mask                 = right_n_bits(age_bits);
119   static const uintptr_t age_mask_in_place        = age_mask << age_shift;
120   static const uintptr_t hash_mask                = right_n_bits(hash_bits);
121   static const uintptr_t hash_mask_in_place       = hash_mask << hash_shift;
122 
123   static const uintptr_t locked_value             = 0;
124   static const uintptr_t unlocked_value           = 1;
125   static const uintptr_t monitor_value            = 2;
126   static const uintptr_t marked_value             = 3;
127 
128   static const uintptr_t no_hash                  = 0 ;  // no hash value assigned
129   static const uintptr_t no_hash_in_place         = (uintptr_t)no_hash << hash_shift;
130   static const uintptr_t no_lock_in_place         = unlocked_value;
131 
132   static const uint max_age                       = age_mask;
133 
134   // Creates a markWord with all bits set to zero.
135   static markWord zero() { return markWord(uintptr_t(0)); }
136 
137   // lock accessors (note that these assume lock_shift == 0)
138   bool is_locked()   const {
139     return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
140   }
141   bool is_unlocked() const {
142     return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
143   }
144   bool is_marked()   const {
145     return (mask_bits(value(), lock_mask_in_place) == marked_value);
146   }
147   bool is_forwarded()   const {
148     return (mask_bits(value(), lock_mask_in_place) == marked_value);
149   }
150   bool is_neutral()  const {  // Not locked, or marked - a "clean" neutral state
151     return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
152   }
153 
154   // Special temporary state of the markWord while being inflated.
155   // Code that looks at mark outside a lock need to take this into account.
156   bool is_being_inflated() const { return (value() == 0); }
157 
158   // Distinguished markword value - used when inflating over
159   // an existing stack-lock.  0 indicates the markword is "BUSY".
160   // Lockword mutators that use a LD...CAS idiom should always
161   // check for and avoid overwriting a 0 value installed by some
162   // other thread.  (They should spin or block instead.  The 0 value
163   // is transient and *should* be short-lived).
164   // Fast-locking does not use INFLATING.
165   static markWord INFLATING() { return zero(); }    // inflate-in-progress
166 
167   // Should this header be preserved during GC?
168   bool must_be_preserved(const oopDesc* obj) const {
169     return (!is_unlocked() || !has_no_hash());
170   }
171 
172   // WARNING: The following routines are used EXCLUSIVELY by
173   // synchronization functions. They are not really gc safe.
174   // They must get updated if markWord layout get changed.
175   markWord set_unlocked() const {
176     return markWord(value() | unlocked_value);
177   }
178   bool has_locker() const {
179     assert(LockingMode == LM_LEGACY, "should only be called with legacy stack locking");
180     return (value() & lock_mask_in_place) == locked_value;
181   }
182   BasicLock* locker() const {
183     assert(has_locker(), "check");
184     return (BasicLock*) value();
185   }
186 
187   bool is_fast_locked() const {
188     assert(LockingMode == LM_LIGHTWEIGHT, "should only be called with new lightweight locking");
189     return (value() & lock_mask_in_place) == locked_value;
190   }
191   markWord set_fast_locked() const {
192     // Clear the lock_mask_in_place bits to set locked_value:
193     return markWord(value() & ~lock_mask_in_place);
194   }
195 
196   bool has_monitor() const {
197     return ((value() & lock_mask_in_place) == monitor_value);
198   }
199   ObjectMonitor* monitor() const {
200     assert(has_monitor(), "check");
201     assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors");
202     // Use xor instead of &~ to provide one extra tag-bit check.
203     return (ObjectMonitor*) (value() ^ monitor_value);
204   }
205   bool has_displaced_mark_helper() const {
206     intptr_t lockbits = value() & lock_mask_in_place;
207     if (LockingMode == LM_LIGHTWEIGHT) {
208       return !UseObjectMonitorTable && lockbits == monitor_value;
209     }
210     // monitor (0b10) | stack-locked (0b00)?
211     return (lockbits & unlocked_value) == 0;
212   }
213   markWord displaced_mark_helper() const;
214   void set_displaced_mark_helper(markWord m) const;
215   markWord copy_set_hash(intptr_t hash) const {
216     uintptr_t tmp = value() & (~hash_mask_in_place);
217     tmp |= ((hash & hash_mask) << hash_shift);
218     return markWord(tmp);
219   }
220   // it is only used to be stored into BasicLock as the
221   // indicator that the lock is using heavyweight monitor
222   static markWord unused_mark() {
223     return markWord(marked_value);
224   }
225   // the following two functions create the markWord to be
226   // stored into object header, it encodes monitor info
227   static markWord encode(BasicLock* lock) {
228     return from_pointer(lock);
229   }
230   static markWord encode(ObjectMonitor* monitor) {
231     assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors");
232     uintptr_t tmp = (uintptr_t) monitor;
233     return markWord(tmp | monitor_value);
234   }
235 
236   markWord set_has_monitor() const {
237     return markWord((value() & ~lock_mask_in_place) | monitor_value);
238   }
239 
240   // used to encode pointers during GC
241   markWord clear_lock_bits() const { return markWord(value() & ~lock_mask_in_place); }
242 
243   // age operations
244   markWord set_marked()   { return markWord((value() & ~lock_mask_in_place) | marked_value); }
245   markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
246 
247   uint     age()           const { return (uint) mask_bits(value() >> age_shift, age_mask); }
248   markWord set_age(uint v) const {
249     assert((v & ~age_mask) == 0, "shouldn't overflow age field");
250     return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
251   }
252   markWord incr_age()      const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
253 
254   // hash operations
255   intptr_t hash() const {
256     return mask_bits(value() >> hash_shift, hash_mask);
257   }
258 
259   bool has_no_hash() const {
260     return hash() == no_hash;
261   }
262 
263   // Prototype mark for initialization
264   static markWord prototype() {
265     return markWord( no_hash_in_place | no_lock_in_place );
266   }
267 
268   // Debugging
269   void print_on(outputStream* st, bool print_monitor_info = true) const;
270 
271   // Prepare address of oop for placement into mark
272   inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
273 
274   // Recover address of oop from encoded form used in mark
275   inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); }
276 
277   inline oop forwardee() const {
278     return cast_to_oop(decode_pointer());
279   }
280 };
281 
282 // Support atomic operations.
283 template<>
284 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
285   typedef markWord Value;
286   typedef uintptr_t Decayed;
287 
288   static Decayed decay(const Value& x) { return x.value(); }
289   static Value recover(Decayed x) { return Value(x); }
290 };
291 
292 #endif // SHARE_OOPS_MARKWORD_HPP