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