1 /*
   2  * Copyright (c) 1997, 2019, 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    biased_lock:1 lock:2 (normal object)
  40 //             JavaThread*:23 epoch:2 age:4    biased_lock:1 lock:2 (biased object)
  41 //             size:32 ------------------------------------------>| (CMS free block)
  42 //             PromotedObject*:29 ---------->| promo_bits:3 ----->| (CMS promoted object)
  43 //
  44 //  64 bits:
  45 //  --------
  46 //  unused:25 hash:31 -->| unused:1   age:4    biased_lock:1 lock:2 (normal object)
  47 //  JavaThread*:54 epoch:2 unused:1   age:4    biased_lock:1 lock:2 (biased object)
  48 //  PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object)
  49 //  size:64 ----------------------------------------------------->| (CMS free block)
  50 //
  51 //  unused:25 hash:31 -->| cms_free:1 age:4    biased_lock:1 lock:2 (COOPs && normal object)
  52 //  JavaThread*:54 epoch:2 cms_free:1 age:4    biased_lock:1 lock:2 (COOPs && biased object)
  53 //  narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 ----->| (COOPs && CMS promoted object)
  54 //  unused:21 size:35 -->| cms_free:1 unused:7 ------------------>| (COOPs && CMS free block)
  55 //
  56 //  - hash contains the identity hash value: largest value is
  57 //    31 bits, see os::random().  Also, 64-bit vm's require
  58 //    a hash value no bigger than 32 bits because they will not
  59 //    properly generate a mask larger than that: see library_call.cpp
  60 //    and c1_CodePatterns_sparc.cpp.
  61 //
  62 //  - the biased lock pattern is used to bias a lock toward a given
  63 //    thread. When this pattern is set in the low three bits, the lock
  64 //    is either biased toward a given thread or "anonymously" biased,
  65 //    indicating that it is possible for it to be biased. When the
  66 //    lock is biased toward a given thread, locking and unlocking can
  67 //    be performed by that thread without using atomic operations.
  68 //    When a lock's bias is revoked, it reverts back to the normal
  69 //    locking scheme described below.
  70 //
  71 //    Note that we are overloading the meaning of the "unlocked" state
  72 //    of the header. Because we steal a bit from the age we can
  73 //    guarantee that the bias pattern will never be seen for a truly
  74 //    unlocked object.
  75 //
  76 //    Note also that the biased state contains the age bits normally
  77 //    contained in the object header. Large increases in scavenge
  78 //    times were seen when these bits were absent and an arbitrary age
  79 //    assigned to all biased objects, because they tended to consume a
  80 //    significant fraction of the eden semispaces and were not
  81 //    promoted promptly, causing an increase in the amount of copying
  82 //    performed. The runtime system aligns all JavaThread* pointers to
  83 //    a very large value (currently 128 bytes (32bVM) or 256 bytes (64bVM))
  84 //    to make room for the age bits & the epoch bits (used in support of
  85 //    biased locking), and for the CMS "freeness" bit in the 64bVM (+COOPs).
  86 //
  87 //    [JavaThread* | epoch | age | 1 | 01]       lock is biased toward given thread
  88 //    [0           | epoch | age | 1 | 01]       lock is anonymously biased
  89 //
  90 //  - the two lock bits are used to describe three states: locked/unlocked and monitor.
  91 //
  92 //    [ptr             | 00]  locked             ptr points to real header on stack
  93 //    [header      | 0 | 01]  unlocked           regular object header
  94 //    [ptr             | 10]  monitor            inflated lock (header is wapped out)
  95 //    [ptr             | 11]  marked             used by markSweep to mark an object
  96 //                                               not valid at any other time
  97 //
  98 //    We assume that stack/thread pointers have the lowest two bits cleared.
  99 
 100 class BasicLock;
 101 class ObjectMonitor;
 102 class JavaThread;
 103 
 104 class markWord {
 105  private:
 106   uintptr_t _value;
 107 
 108  public:
 109   explicit markWord(uintptr_t value) : _value(value) {}
 110 
 111   markWord() { /* uninitialized */}
 112 
 113   // It is critical for performance that this class be trivially
 114   // destructable, copyable, and assignable.
 115 
 116   static markWord from_pointer(void* ptr) {
 117     return markWord((uintptr_t)ptr);
 118   }
 119   void* to_pointer() const {
 120     return (void*)_value;
 121   }
 122 
 123   bool operator==(const markWord& other) const {
 124     return _value == other._value;
 125   }
 126   bool operator!=(const markWord& other) const {
 127     return !operator==(other);
 128   }
 129 
 130   // Conversion
 131   uintptr_t value() const { return _value; }
 132 
 133   // Constants
 134   static const int age_bits                       = 4;
 135   static const int lock_bits                      = 2;
 136   static const int biased_lock_bits               = 1;
 137   static const int max_hash_bits                  = BitsPerWord - age_bits - lock_bits - biased_lock_bits;
 138   static const int hash_bits                      = max_hash_bits > 31 ? 31 : max_hash_bits;
 139   static const int cms_bits                       = LP64_ONLY(1) NOT_LP64(0);
 140   static const int epoch_bits                     = 2;
 141 
 142   // The biased locking code currently requires that the age bits be
 143   // contiguous to the lock bits.
 144   static const int lock_shift                     = 0;
 145   static const int biased_lock_shift              = lock_bits;
 146   static const int age_shift                      = lock_bits + biased_lock_bits;
 147   static const int cms_shift                      = age_shift + age_bits;
 148   static const int hash_shift                     = cms_shift + cms_bits;
 149   static const int epoch_shift                    = hash_shift;
 150 
 151   static const uintptr_t lock_mask                = right_n_bits(lock_bits);
 152   static const uintptr_t lock_mask_in_place       = lock_mask << lock_shift;
 153   static const uintptr_t biased_lock_mask         = right_n_bits(lock_bits + biased_lock_bits);
 154   static const uintptr_t biased_lock_mask_in_place= biased_lock_mask << lock_shift;
 155   static const uintptr_t biased_lock_bit_in_place = 1 << biased_lock_shift;
 156   static const uintptr_t age_mask                 = right_n_bits(age_bits);
 157   static const uintptr_t age_mask_in_place        = age_mask << age_shift;
 158   static const uintptr_t epoch_mask               = right_n_bits(epoch_bits);
 159   static const uintptr_t epoch_mask_in_place      = epoch_mask << epoch_shift;
 160   static const uintptr_t cms_mask                 = right_n_bits(cms_bits);
 161   static const uintptr_t cms_mask_in_place        = cms_mask << cms_shift;
 162 
 163   static const uintptr_t hash_mask                = right_n_bits(hash_bits);
 164   static const uintptr_t hash_mask_in_place       = hash_mask << hash_shift;
 165 
 166   // Alignment of JavaThread pointers encoded in object header required by biased locking
 167   static const size_t biased_lock_alignment       = 2 << (epoch_shift + epoch_bits);
 168 
 169   static const uintptr_t locked_value             = 0;
 170   static const uintptr_t unlocked_value           = 1;
 171   static const uintptr_t monitor_value            = 2;
 172   static const uintptr_t marked_value             = 3;
 173   static const uintptr_t biased_lock_pattern      = 5;
 174 
 175   static const uintptr_t no_hash                  = 0 ;  // no hash value assigned
 176   static const uintptr_t no_hash_in_place         = (address_word)no_hash << hash_shift;
 177   static const uintptr_t no_lock_in_place         = unlocked_value;
 178 
 179   static const uint max_age                       = age_mask;
 180 
 181   static const int max_bias_epoch                 = epoch_mask;
 182 
 183   // Creates a markWord with all bits set to zero.
 184   static markWord zero() { return markWord(uintptr_t(0)); }
 185 
 186   // Biased Locking accessors.
 187   // These must be checked by all code which calls into the
 188   // ObjectSynchronizer and other code. The biasing is not understood
 189   // by the lower-level CAS-based locking code, although the runtime
 190   // fixes up biased locks to be compatible with it when a bias is
 191   // revoked.
 192   bool has_bias_pattern() const {
 193     return (mask_bits(value(), biased_lock_mask_in_place) == biased_lock_pattern);
 194   }
 195   JavaThread* biased_locker() const {
 196     assert(has_bias_pattern(), "should not call this otherwise");
 197     return (JavaThread*) mask_bits(value(), ~(biased_lock_mask_in_place | age_mask_in_place | epoch_mask_in_place));
 198   }
 199   // Indicates that the mark has the bias bit set but that it has not
 200   // yet been biased toward a particular thread
 201   bool is_biased_anonymously() const {
 202     return (has_bias_pattern() && (biased_locker() == NULL));
 203   }
 204   // Indicates epoch in which this bias was acquired. If the epoch
 205   // changes due to too many bias revocations occurring, the biases
 206   // from the previous epochs are all considered invalid.
 207   int bias_epoch() const {
 208     assert(has_bias_pattern(), "should not call this otherwise");
 209     return (mask_bits(value(), epoch_mask_in_place) >> epoch_shift);
 210   }
 211   markWord set_bias_epoch(int epoch) {
 212     assert(has_bias_pattern(), "should not call this otherwise");
 213     assert((epoch & (~epoch_mask)) == 0, "epoch overflow");
 214     return markWord(mask_bits(value(), ~epoch_mask_in_place) | (epoch << epoch_shift));
 215   }
 216   markWord incr_bias_epoch() {
 217     return set_bias_epoch((1 + bias_epoch()) & epoch_mask);
 218   }
 219   // Prototype mark for initialization
 220   static markWord biased_locking_prototype() {
 221     return markWord( biased_lock_pattern );
 222   }
 223 
 224   // lock accessors (note that these assume lock_shift == 0)
 225   bool is_locked()   const {
 226     return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
 227   }
 228   bool is_unlocked() const {
 229     return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value);
 230   }
 231   // ObjectMonitor::install_displaced_markword_in_object() uses
 232   // is_marked() on ObjectMonitor::_header as part of the restoration
 233   // protocol for an object's header. In this usage, the mark bits are
 234   // only ever set (and cleared) on the ObjectMonitor::_header field.
 235   bool is_marked()   const {
 236     return (mask_bits(value(), lock_mask_in_place) == marked_value);
 237   }
 238   bool is_neutral()  const { return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value); }
 239 
 240   // Special temporary state of the markWord while being inflated.
 241   // Code that looks at mark outside a lock need to take this into account.
 242   bool is_being_inflated() const { return (value() == 0); }
 243 
 244   // Distinguished markword value - used when inflating over
 245   // an existing stacklock.  0 indicates the markword is "BUSY".
 246   // Lockword mutators that use a LD...CAS idiom should always
 247   // check for and avoid overwriting a 0 value installed by some
 248   // other thread.  (They should spin or block instead.  The 0 value
 249   // is transient and *should* be short-lived).
 250   static markWord INFLATING() { return zero(); }    // inflate-in-progress
 251 
 252   // Should this header be preserved during GC?
 253   template <typename KlassProxy>
 254   inline bool must_be_preserved(KlassProxy klass) const;
 255 
 256   // Should this header (including its age bits) be preserved in the
 257   // case of a promotion failure during scavenge?
 258   // Note that we special case this situation. We want to avoid
 259   // calling BiasedLocking::preserve_marks()/restore_marks() (which
 260   // decrease the number of mark words that need to be preserved
 261   // during GC) during each scavenge. During scavenges in which there
 262   // is no promotion failure, we actually don't need to call the above
 263   // routines at all, since we don't mutate and re-initialize the
 264   // marks of promoted objects using init_mark(). However, during
 265   // scavenges which result in promotion failure, we do re-initialize
 266   // the mark words of objects, meaning that we should have called
 267   // these mark word preservation routines. Currently there's no good
 268   // place in which to call them in any of the scavengers (although
 269   // guarded by appropriate locks we could make one), but the
 270   // observation is that promotion failures are quite rare and
 271   // reducing the number of mark words preserved during them isn't a
 272   // high priority.
 273   template <typename KlassProxy>
 274   inline bool must_be_preserved_for_promotion_failure(KlassProxy klass) const;
 275 
 276   // Should this header be preserved during a scavenge where CMS is
 277   // the old generation?
 278   // (This is basically the same body as must_be_preserved_for_promotion_failure(),
 279   // but takes the Klass* as argument instead)
 280   inline bool must_be_preserved_for_cms_scavenge(Klass* klass_of_obj_containing_mark) const;
 281 
 282   // WARNING: The following routines are used EXCLUSIVELY by
 283   // synchronization functions. They are not really gc safe.
 284   // They must get updated if markWord layout get changed.
 285   markWord set_unlocked() const {
 286     return markWord(value() | unlocked_value);
 287   }
 288   bool has_locker() const {
 289     return ((value() & lock_mask_in_place) == locked_value);
 290   }
 291   BasicLock* locker() const {
 292     assert(has_locker(), "check");
 293     return (BasicLock*) value();
 294   }
 295   bool has_monitor() const {
 296     return ((value() & monitor_value) != 0);
 297   }
 298   ObjectMonitor* monitor() const {
 299     assert(has_monitor(), "check");
 300     // Use xor instead of &~ to provide one extra tag-bit check.
 301     return (ObjectMonitor*) (value() ^ monitor_value);
 302   }
 303   bool has_displaced_mark_helper() const {
 304     return ((value() & unlocked_value) == 0);
 305   }
 306   markWord displaced_mark_helper() const {
 307     assert(has_displaced_mark_helper(), "check");
 308     uintptr_t ptr = (value() & ~monitor_value);
 309     return *(markWord*)ptr;
 310   }
 311   void set_displaced_mark_helper(markWord m) const {
 312     assert(has_displaced_mark_helper(), "check");
 313     uintptr_t ptr = (value() & ~monitor_value);
 314     ((markWord*)ptr)->_value = m._value;
 315   }
 316   markWord copy_set_hash(intptr_t hash) const {
 317     uintptr_t tmp = value() & (~hash_mask_in_place);
 318     tmp |= ((hash & hash_mask) << hash_shift);
 319     return markWord(tmp);
 320   }
 321   // it is only used to be stored into BasicLock as the
 322   // indicator that the lock is using heavyweight monitor
 323   static markWord unused_mark() {
 324     return markWord(marked_value);
 325   }
 326   // the following two functions create the markWord to be
 327   // stored into object header, it encodes monitor info
 328   static markWord encode(BasicLock* lock) {
 329     return from_pointer(lock);
 330   }
 331   static markWord encode(ObjectMonitor* monitor) {
 332     uintptr_t tmp = (uintptr_t) monitor;
 333     return markWord(tmp | monitor_value);
 334   }
 335   static markWord encode(JavaThread* thread, uint age, int bias_epoch) {
 336     uintptr_t tmp = (uintptr_t) thread;
 337     assert(UseBiasedLocking && ((tmp & (epoch_mask_in_place | age_mask_in_place | biased_lock_mask_in_place)) == 0), "misaligned JavaThread pointer");
 338     assert(age <= max_age, "age too large");
 339     assert(bias_epoch <= max_bias_epoch, "bias epoch too large");
 340     return markWord(tmp | (bias_epoch << epoch_shift) | (age << age_shift) | biased_lock_pattern);
 341   }
 342 
 343   // used to encode pointers during GC
 344   markWord clear_lock_bits() { return markWord(value() & ~lock_mask_in_place); }
 345 
 346   // age operations
 347   markWord set_marked()   { return markWord((value() & ~lock_mask_in_place) | marked_value); }
 348   markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
 349 
 350   uint     age()           const { return mask_bits(value() >> age_shift, age_mask); }
 351   markWord set_age(uint v) const {
 352     assert((v & ~age_mask) == 0, "shouldn't overflow age field");
 353     return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
 354   }
 355   markWord incr_age()      const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
 356 
 357   // hash operations
 358   intptr_t hash() const {
 359     return mask_bits(value() >> hash_shift, hash_mask);
 360   }
 361 
 362   bool has_no_hash() const {
 363     return hash() == no_hash;
 364   }
 365 
 366   // Prototype mark for initialization
 367   static markWord prototype() {
 368     return markWord( no_hash_in_place | no_lock_in_place );
 369   }
 370 
 371   // Helper function for restoration of unmarked mark oops during GC
 372   static inline markWord prototype_for_klass(const Klass* klass);
 373 
 374   // Debugging
 375   void print_on(outputStream* st) const;
 376 
 377   // Prepare address of oop for placement into mark
 378   inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
 379 
 380   // Recover address of oop from encoded form used in mark
 381   inline void* decode_pointer() { if (UseBiasedLocking && has_bias_pattern()) return NULL; return (void*)clear_lock_bits().value(); }
 382 
 383   // These markWords indicate cms free chunk blocks and not objects.
 384   // In 64 bit, the markWord is set to distinguish them from oops.
 385   // These are defined in 32 bit mode for vmStructs.
 386   const static uintptr_t cms_free_chunk_pattern  = 0x1;
 387 
 388   // Constants for the size field.
 389   enum { size_shift                = cms_shift + cms_bits,
 390          size_bits                 = 35    // need for compressed oops 32G
 391        };
 392   // These values are too big for Win64
 393   const static uintptr_t size_mask = LP64_ONLY(right_n_bits(size_bits))
 394                                      NOT_LP64(0);
 395   const static uintptr_t size_mask_in_place =
 396                                      (address_word)size_mask << size_shift;
 397 
 398 #ifdef _LP64
 399   static markWord cms_free_prototype() {
 400     return markWord((prototype().value() & ~cms_mask_in_place) |
 401                     ((cms_free_chunk_pattern & cms_mask) << cms_shift));
 402   }
 403   uintptr_t cms_encoding() const {
 404     return mask_bits(value() >> cms_shift, cms_mask);
 405   }
 406   bool is_cms_free_chunk() const {
 407     return is_neutral() &&
 408            (cms_encoding() & cms_free_chunk_pattern) == cms_free_chunk_pattern;
 409   }
 410 
 411   size_t get_size() const       { return (size_t)(value() >> size_shift); }
 412   static markWord set_size_and_free(size_t size) {
 413     assert((size & ~size_mask) == 0, "shouldn't overflow size field");
 414     return markWord((cms_free_prototype().value() & ~size_mask_in_place) |
 415                     ((size & size_mask) << size_shift));
 416   }
 417 #endif // _LP64
 418 };
 419 
 420 // Support atomic operations.
 421 template<>
 422 struct PrimitiveConversions::Translate<markWord> : public TrueType {
 423   typedef markWord Value;
 424   typedef uintptr_t Decayed;
 425 
 426   static Decayed decay(const Value& x) { return x.value(); }
 427   static Value recover(Decayed x) { return Value(x); }
 428 };
 429 
 430 #endif // SHARE_OOPS_MARKWORD_HPP