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 //  nklass:32 hash:25 -->| 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 self_forwarded_bits            = 1;
105   static const int max_hash_bits                  = BitsPerWord - age_bits - lock_bits - self_forwarded_bits;
106   static const int hash_bits                      = max_hash_bits > 25 ? 25 : max_hash_bits;
107 #ifdef _LP64
108   static const int klass_bits                     = 32;
109 #endif
110 
111   static const int lock_shift                     = 0;
112   static const int self_forwarded_shift           = lock_shift + lock_bits;
113   static const int age_shift                      = self_forwarded_shift + self_forwarded_bits;
114   static const int hash_shift                     = age_shift + age_bits;
115 #ifdef _LP64
116   static const int klass_shift                    = hash_shift + hash_bits;
117 #endif
118 
119   static const uintptr_t lock_mask                = right_n_bits(lock_bits);
120   static const uintptr_t lock_mask_in_place       = lock_mask << lock_shift;
121   static const uintptr_t self_forwarded_mask      = right_n_bits(self_forwarded_bits);
122   static const uintptr_t self_forwarded_mask_in_place = self_forwarded_mask << self_forwarded_shift;
123   static const uintptr_t age_mask                 = right_n_bits(age_bits);
124   static const uintptr_t age_mask_in_place        = age_mask << age_shift;
125   static const uintptr_t hash_mask                = right_n_bits(hash_bits);
126   static const uintptr_t hash_mask_in_place       = hash_mask << hash_shift;
127 
128 #ifdef _LP64
129   static const uintptr_t klass_mask               = right_n_bits(klass_bits);
130   static const uintptr_t klass_mask_in_place      = klass_mask << klass_shift;
131 #endif
132 
133   static const uintptr_t locked_value             = 0;
134   static const uintptr_t unlocked_value           = 1;
135   static const uintptr_t monitor_value            = 2;
136   static const uintptr_t marked_value             = 3;
137 
138   static const uintptr_t no_hash                  = 0 ;  // no hash value assigned
139   static const uintptr_t no_hash_in_place         = (address_word)no_hash << hash_shift;
140   static const uintptr_t no_lock_in_place         = unlocked_value;
141 
142   static const uint max_age                       = age_mask;
143 
144   // Creates a markWord with all bits set to zero.
145   static markWord zero() { return markWord(uintptr_t(0)); }
146 
147   // lock accessors (note that these assume lock_shift == 0)
148   bool is_locked()   const {
149     return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
150   }
151   bool is_unlocked() const {
152     return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
153   }
154   bool is_marked()   const {
155     return (mask_bits(value(), lock_mask_in_place) == marked_value);
156   }
157   bool is_neutral()  const {
158     return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
159   }
160 
161   // Special temporary state of the markWord while being inflated.
162   // Code that looks at mark outside a lock need to take this into account.
163   bool is_being_inflated() const { return (value() == 0); }
164 
165   // Distinguished markword value - used when inflating over
166   // an existing stack-lock.  0 indicates the markword is "BUSY".
167   // Lockword mutators that use a LD...CAS idiom should always
168   // check for and avoid overwriting a 0 value installed by some
169   // other thread.  (They should spin or block instead.  The 0 value
170   // is transient and *should* be short-lived).
171   static markWord INFLATING() { return zero(); }    // inflate-in-progress
172 
173   // Should this header be preserved during GC?
174   bool must_be_preserved(const oopDesc* obj) const {
175     return (!is_unlocked() || !has_no_hash());
176   }
177 
178   // WARNING: The following routines are used EXCLUSIVELY by
179   // synchronization functions. They are not really gc safe.
180   // They must get updated if markWord layout get changed.
181   markWord set_unlocked() const {
182     return markWord(value() | unlocked_value);
183   }
184   bool has_locker() const {
185     return ((value() & lock_mask_in_place) == locked_value);
186   }
187   BasicLock* locker() const {
188     assert(has_locker(), "check");
189     return (BasicLock*) value();
190   }
191   bool has_monitor() const {
192     return ((value() & monitor_value) != 0);
193   }
194   ObjectMonitor* monitor() const {
195     assert(has_monitor(), "check");
196     // Use xor instead of &~ to provide one extra tag-bit check.
197     return (ObjectMonitor*) (value() ^ monitor_value);
198   }
199   bool has_displaced_mark_helper() const {
200     return ((value() & unlocked_value) == 0);
201   }
202   markWord displaced_mark_helper() const;
203   void set_displaced_mark_helper(markWord m) const;
204   markWord copy_set_hash(intptr_t hash) const {
205     uintptr_t tmp = value() & (~hash_mask_in_place);
206     tmp |= ((hash & hash_mask) << hash_shift);
207     return markWord(tmp);
208   }
209   // it is only used to be stored into BasicLock as the
210   // indicator that the lock is using heavyweight monitor
211   static markWord unused_mark() {
212     return markWord(marked_value);
213   }
214   // the following two functions create the markWord to be
215   // stored into object header, it encodes monitor info
216   static markWord encode(BasicLock* lock) {
217     return from_pointer(lock);
218   }
219   static markWord encode(ObjectMonitor* monitor) {
220     uintptr_t tmp = (uintptr_t) monitor;
221     return markWord(tmp | monitor_value);
222   }
223 
224   // used to encode pointers during GC
225   markWord clear_lock_bits() { return markWord(value() & ~lock_mask_in_place); }
226 
227   // age operations
228   markWord set_marked()   { return markWord((value() & ~lock_mask_in_place) | marked_value); }
229   markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
230 
231   uint     age()           const { return mask_bits(value() >> age_shift, age_mask); }
232   markWord set_age(uint v) const {
233     assert((v & ~age_mask) == 0, "shouldn't overflow age field");
234     return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
235   }
236   markWord incr_age()      const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
237 
238   // hash operations
239   intptr_t hash() const {
240     return mask_bits(value() >> hash_shift, hash_mask);
241   }
242 
243   bool has_no_hash() const {
244     return hash() == no_hash;
245   }
246 
247 #ifdef _LP64
248   inline Klass* klass() const;
249   inline Klass* klass_or_null() const;
250   inline Klass* safe_klass() const;
251   inline markWord set_klass(const Klass* klass) const;
252   inline narrowKlass narrow_klass() const;
253   inline markWord set_narrow_klass(const narrowKlass klass) const;
254 #endif
255 
256   // Prototype mark for initialization
257   static markWord prototype() {
258     return markWord( no_hash_in_place | no_lock_in_place );
259   }
260 
261   // Debugging
262   void print_on(outputStream* st, bool print_monitor_info = true) const;
263 
264   // Prepare address of oop for placement into mark
265   inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
266 
267   // Recover address of oop from encoded form used in mark
268   inline void* decode_pointer() { return (void*)clear_lock_bits().value(); }
269 
270   inline bool self_forwarded() const {
271     return mask_bits(value(), self_forwarded_mask_in_place) != 0;
272   }
273 
274   inline markWord set_self_forwarded() const {
275     return markWord(value() | self_forwarded_mask_in_place | marked_value);
276   }
277 };
278 
279 // Support atomic operations.
280 template<>
281 struct PrimitiveConversions::Translate<markWord> : public TrueType {
282   typedef markWord Value;
283   typedef uintptr_t Decayed;
284 
285   static Decayed decay(const Value& x) { return x.value(); }
286   static Value recover(Decayed x) { return Value(x); }
287 };
288 
289 #endif // SHARE_OOPS_MARKWORD_HPP