1 /* 2 * Copyright (c) 1997, 2024, 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/primitiveConversions.hpp" 29 #include "oops/compressedKlass.hpp" 30 #include "oops/oopsHierarchy.hpp" 31 #include "runtime/globals.hpp" 32 33 #include <type_traits> 34 35 // The markWord describes the header of an object. 36 // 37 // Bit-format of an object header (most significant first, big endian layout below): 38 // 39 // 32 bits: 40 // -------- 41 // hash:25 ------------>| age:4 self-fwd:1 lock:2 (normal object) 42 // 43 // 64 bits: 44 // -------- 45 // unused:22 hash:31 -->| unused_gap:4 age:4 self-fwd:1 lock:2 (normal object) 46 // 47 // 64 bits (with compact headers): 48 // ------------------------------- 49 // klass:22 hash:31 -->| unused_gap:4 age:4 self-fwd:1 lock:2 (normal object) 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 // 56 // - the two lock bits are used to describe three states: locked/unlocked and monitor. 57 // 58 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use) 59 // [header | 00] locked locked regular object header (fast-locking in use) 60 // [header | 01] unlocked regular object header 61 // [ptr | 10] monitor inflated lock (header is swapped out, UseObjectMonitorTable == false) 62 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true) 63 // [ptr | 11] marked used to mark an object 64 // [0 ............ 0| 00] inflating inflation in progress (stack-locking in use) 65 // 66 // We assume that stack/thread pointers have the lowest two bits cleared. 67 // 68 // - INFLATING() is a distinguished markword value of all zeros that is 69 // used when inflating an existing stack-lock into an ObjectMonitor. 70 // See below for is_being_inflated() and INFLATING(). 71 72 class BasicLock; 73 class ObjectMonitor; 74 class JavaThread; 75 class outputStream; 76 77 class markWord { 78 private: 79 uintptr_t _value; 80 81 public: 82 explicit markWord(uintptr_t value) : _value(value) {} 83 84 markWord() = default; // Doesn't initialize _value. 85 86 // It is critical for performance that this class be trivially 87 // destructable, copyable, and assignable. 88 ~markWord() = default; 89 markWord(const markWord&) = default; 90 markWord& operator=(const markWord&) = default; 91 92 static markWord from_pointer(void* ptr) { 93 return markWord((uintptr_t)ptr); 94 } 95 void* to_pointer() const { 96 return (void*)_value; 97 } 98 99 bool operator==(const markWord& other) const { 100 return _value == other._value; 101 } 102 bool operator!=(const markWord& other) const { 103 return !operator==(other); 104 } 105 106 // Conversion 107 uintptr_t value() const { return _value; } 108 109 // Constants 110 static const int age_bits = 4; 111 static const int lock_bits = 2; 112 static const int self_fwd_bits = 1; 113 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - self_fwd_bits; 114 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits; 115 static const int unused_gap_bits = LP64_ONLY(4) NOT_LP64(0); // Reserved for Valhalla. 116 117 static const int lock_shift = 0; 118 static const int self_fwd_shift = lock_shift + lock_bits; 119 static const int age_shift = self_fwd_shift + self_fwd_bits; 120 static const int hash_shift = age_shift + age_bits + unused_gap_bits; 121 122 static const uintptr_t lock_mask = right_n_bits(lock_bits); 123 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift; 124 static const uintptr_t self_fwd_mask = right_n_bits(self_fwd_bits); 125 static const uintptr_t self_fwd_mask_in_place = self_fwd_mask << self_fwd_shift; 126 static const uintptr_t age_mask = right_n_bits(age_bits); 127 static const uintptr_t age_mask_in_place = age_mask << age_shift; 128 static const uintptr_t hash_mask = right_n_bits(hash_bits); 129 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift; 130 131 #ifdef _LP64 132 // Used only with compact headers: 133 // We store the (narrow) Klass* in the bits 43 to 64. 134 135 // These are for bit-precise extraction of the narrow Klass* from the 64-bit Markword 136 static constexpr int klass_offset_in_bytes = 4; 137 static constexpr int klass_shift = hash_shift + hash_bits; 138 static constexpr int klass_shift_at_offset = klass_shift - klass_offset_in_bytes * BitsPerByte; 139 static constexpr int klass_bits = 22; 140 static constexpr uintptr_t klass_mask = right_n_bits(klass_bits); 141 static constexpr uintptr_t klass_mask_in_place = klass_mask << klass_shift; 142 #endif 143 144 145 static const uintptr_t locked_value = 0; 146 static const uintptr_t unlocked_value = 1; 147 static const uintptr_t monitor_value = 2; 148 static const uintptr_t marked_value = 3; 149 150 static const uintptr_t no_hash = 0 ; // no hash value assigned 151 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift; 152 static const uintptr_t no_lock_in_place = unlocked_value; 153 154 static const uint max_age = age_mask; 155 156 // Creates a markWord with all bits set to zero. 157 static markWord zero() { return markWord(uintptr_t(0)); } 158 159 // lock accessors (note that these assume lock_shift == 0) 160 bool is_locked() const { 161 return (mask_bits(value(), lock_mask_in_place) != unlocked_value); 162 } 163 bool is_unlocked() const { 164 return (mask_bits(value(), lock_mask_in_place) == unlocked_value); 165 } 166 bool is_marked() const { 167 return (mask_bits(value(), lock_mask_in_place) == marked_value); 168 } 169 bool is_forwarded() const { 170 // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx). 171 return mask_bits(value(), lock_mask_in_place | self_fwd_mask_in_place) >= static_cast<intptr_t>(marked_value); 172 } 173 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state 174 return (mask_bits(value(), lock_mask_in_place) == unlocked_value); 175 } 176 177 // Special temporary state of the markWord while being inflated. 178 // Code that looks at mark outside a lock need to take this into account. 179 bool is_being_inflated() const { return (value() == 0); } 180 181 // Distinguished markword value - used when inflating over 182 // an existing stack-lock. 0 indicates the markword is "BUSY". 183 // Lockword mutators that use a LD...CAS idiom should always 184 // check for and avoid overwriting a 0 value installed by some 185 // other thread. (They should spin or block instead. The 0 value 186 // is transient and *should* be short-lived). 187 // Fast-locking does not use INFLATING. 188 static markWord INFLATING() { return zero(); } // inflate-in-progress 189 190 // Should this header be preserved during GC? 191 bool must_be_preserved(const oopDesc* obj) const { 192 return (!is_unlocked() || !has_no_hash()); 193 } 194 195 // WARNING: The following routines are used EXCLUSIVELY by 196 // synchronization functions. They are not really gc safe. 197 // They must get updated if markWord layout get changed. 198 markWord set_unlocked() const { 199 return markWord(value() | unlocked_value); 200 } 201 bool has_locker() const { 202 assert(LockingMode == LM_LEGACY, "should only be called with legacy stack locking"); 203 return (value() & lock_mask_in_place) == locked_value; 204 } 205 BasicLock* locker() const { 206 assert(has_locker(), "check"); 207 return (BasicLock*) value(); 208 } 209 210 bool is_fast_locked() const { 211 assert(LockingMode == LM_LIGHTWEIGHT, "should only be called with new lightweight locking"); 212 return (value() & lock_mask_in_place) == locked_value; 213 } 214 markWord set_fast_locked() const { 215 // Clear the lock_mask_in_place bits to set locked_value: 216 return markWord(value() & ~lock_mask_in_place); 217 } 218 219 bool has_monitor() const { 220 return ((value() & lock_mask_in_place) == monitor_value); 221 } 222 ObjectMonitor* monitor() const { 223 assert(has_monitor(), "check"); 224 assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors"); 225 // Use xor instead of &~ to provide one extra tag-bit check. 226 return (ObjectMonitor*) (value() ^ monitor_value); 227 } 228 bool has_displaced_mark_helper() const { 229 intptr_t lockbits = value() & lock_mask_in_place; 230 if (LockingMode == LM_LIGHTWEIGHT) { 231 return !UseObjectMonitorTable && lockbits == monitor_value; 232 } 233 // monitor (0b10) | stack-locked (0b00)? 234 return (lockbits & unlocked_value) == 0; 235 } 236 markWord displaced_mark_helper() const; 237 void set_displaced_mark_helper(markWord m) const; 238 markWord copy_set_hash(intptr_t hash) const { 239 uintptr_t tmp = value() & (~hash_mask_in_place); 240 tmp |= ((hash & hash_mask) << hash_shift); 241 return markWord(tmp); 242 } 243 // it is only used to be stored into BasicLock as the 244 // indicator that the lock is using heavyweight monitor 245 static markWord unused_mark() { 246 return markWord(marked_value); 247 } 248 // the following two functions create the markWord to be 249 // stored into object header, it encodes monitor info 250 static markWord encode(BasicLock* lock) { 251 return from_pointer(lock); 252 } 253 static markWord encode(ObjectMonitor* monitor) { 254 assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors"); 255 uintptr_t tmp = (uintptr_t) monitor; 256 return markWord(tmp | monitor_value); 257 } 258 259 markWord set_has_monitor() const { 260 return markWord((value() & ~lock_mask_in_place) | monitor_value); 261 } 262 263 // used to encode pointers during GC 264 markWord clear_lock_bits() const { return markWord(value() & ~lock_mask_in_place); } 265 266 // age operations 267 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); } 268 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); } 269 270 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); } 271 markWord set_age(uint v) const { 272 assert((v & ~age_mask) == 0, "shouldn't overflow age field"); 273 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift)); 274 } 275 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); } 276 277 // hash operations 278 intptr_t hash() const { 279 return mask_bits(value() >> hash_shift, hash_mask); 280 } 281 282 bool has_no_hash() const { 283 return hash() == no_hash; 284 } 285 286 inline Klass* klass() const; 287 inline Klass* klass_or_null() const; 288 inline Klass* klass_without_asserts() const; 289 inline narrowKlass narrow_klass() const; 290 inline markWord set_narrow_klass(narrowKlass narrow_klass) const; 291 292 // Prototype mark for initialization 293 static markWord prototype() { 294 return markWord( no_hash_in_place | no_lock_in_place ); 295 } 296 297 // Debugging 298 void print_on(outputStream* st, bool print_monitor_info = true) const; 299 300 // Prepare address of oop for placement into mark 301 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); } 302 303 // Recover address of oop from encoded form used in mark 304 inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); } 305 306 inline bool is_self_forwarded() const { 307 NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");) 308 return mask_bits(value(), self_fwd_mask_in_place) != 0; 309 } 310 311 inline markWord set_self_forwarded() const { 312 NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");) 313 return markWord(value() | self_fwd_mask_in_place); 314 } 315 316 inline markWord unset_self_forwarded() const { 317 NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");) 318 return markWord(value() & ~self_fwd_mask_in_place); 319 } 320 321 inline oop forwardee() const { 322 return cast_to_oop(decode_pointer()); 323 } 324 }; 325 326 // Support atomic operations. 327 template<> 328 struct PrimitiveConversions::Translate<markWord> : public std::true_type { 329 typedef markWord Value; 330 typedef uintptr_t Decayed; 331 332 static Decayed decay(const Value& x) { return x.value(); } 333 static Value recover(Decayed x) { return Value(x); } 334 }; 335 336 #endif // SHARE_OOPS_MARKWORD_HPP