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