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