1 /* 2 * Copyright (c) 1997, 2025, 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 "layoutKind.hpp" 30 #include "oops/compressedKlass.hpp" 31 #include "oops/oopsHierarchy.hpp" 32 #include "runtime/globals.hpp" 33 #include "utilities/vmEnums.hpp" 34 35 #include <type_traits> 36 37 // The markWord describes the header of an object. 38 // 39 // Bit-format of an object header (most significant first, big endian layout below): 40 // 41 // 32 bits: 42 // -------- 43 // hash:25 ------------>| age:4 self-fwd:1 lock:2 (normal object) 44 // 45 // 64 bits: 46 // -------- 47 // unused:22 hash:31 -->| unused_gap:4 age:4 self-fwd:1 lock:2 (normal object) 48 // 49 // 64 bits (with compact headers): 50 // ------------------------------- 51 // klass:22 hash:31 -->| unused_gap:4 age:4 self-fwd:1 lock:2 (normal object) 52 // 53 // - hash contains the identity hash value: largest value is 54 // 31 bits, see os::random(). Also, 64-bit vm's require 55 // a hash value no bigger than 32 bits because they will not 56 // properly generate a mask larger than that: see library_call.cpp 57 // 58 // - the two lock bits are used to describe three states: locked/unlocked and monitor. 59 // 60 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use) 61 // [header | 00] locked locked regular object header (fast-locking in use) 62 // [header | 01] unlocked regular object header 63 // [ptr | 10] monitor inflated lock (header is swapped out, UseObjectMonitorTable == false) 64 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true) 65 // [ptr | 11] marked used to mark an object 66 // [0 ............ 0| 00] inflating inflation in progress (stack-locking in use) 67 // 68 // We assume that stack/thread pointers have the lowest two bits cleared. 69 // 70 // 71 // - INFLATING() is a distinguished markword value of all zeros that is 72 // used when inflating an existing stack-lock into an ObjectMonitor. 73 // See below for is_being_inflated() and INFLATING(). 74 // 75 // 76 // 77 // Valhalla 78 // 79 // <CMH: merge this doc into the text above> 80 // 81 // Project Valhalla has mark word encoding requirements for the following oops: 82 // 83 // * inline types: have alternative bytecode behavior, e.g. can not be locked 84 // - "larval state": mutable state, but only during object init, observable 85 // by only by a single thread (generally do not mutate markWord) 86 // 87 // * flat arrays: load/decode of klass layout helper is expensive for aaload 88 // 89 // * "null free" arrays: load/decode of klass layout helper again for aaload 90 // 91 // EnableValhalla 92 // 93 // Formerly known as "biased lock bit", "unused_gap" is free to use: using this 94 // bit to indicate inline type, combined with "unlocked" lock bits, means we 95 // will not interfere with lock encodings (displaced, inflating, and monitor), 96 // since inline types can't be locked. 97 // 98 // Further state encoding 99 // 100 // 32 bit plaforms currently have no further room for encoding. No room for 101 // "denormalized layout helper bits", these fast mark word tests can only be made on 102 // 64 bit platforms. 32-bit platforms need to load the klass->_layout_helper. This 103 // said, the larval state bit is still required for operation, stealing from the hash 104 // code is simplest mechanism. 105 // 106 // Valhalla specific encodings 107 // 108 // Revised Bit-format of an object header (most significant first, big endian layout below): 109 // 110 // 32 bits: 111 // -------- 112 // hash:24 ------------>| larval:1 age:4 inline_type:1 lock:2 113 // 114 // 64 bits: 115 // -------- 116 // unused:1 | <-- hash:31 -->| unused:22 larval:1 age:4 flat_array:1 null_free_array:1 inline_type:1 lock:2 117 // klass:22 hash:31 -->| larval:1 age:4 flat_array:1 null_free_array:1 inline_type:1 self-fwd:1 lock:2 (normal object) 118 // 119 // The "fast" static type bits (flat_array, null_free_array, and inline_type) 120 // are placed lowest next to lock bits to more easily decode forwarding pointers. 121 // G1 for example, implicitly clears age bits ("G1FullGCCompactionPoint::forward()") 122 // using "oopDesc->forwardee()", so it necessary for "markWord::decode_pointer()" 123 // to return a non-nullptr for this case, but not confuse the static type bits for 124 // a pointer. 125 // 126 // Note the position of 'self-fwd' is not by accident. When forwarding an 127 // object to a new heap position, HeapWord alignment guarantees the lower 128 // bits, including 'self-fwd' are 0. "is_self_forwarded()" will be correctly 129 // set to false. Otherwise encode_pointer_as_mark() may have 'self-fwd' set. 130 // 131 // 132 // Static types bits are recorded in the "klass->prototype_header()", displaced 133 // mark should simply use the prototype header as "slow path", rather chasing 134 // monitor or stack lock races. 135 // 136 // Lock patterns (note inline types can't be locked/monitor/inflating)... 137 // 138 // [ptr | 000] locked ptr points to real header on stack 139 // [header | ?01] unlocked regular object header 140 // [ptr | 010] monitor inflated lock (header is wapped out) 141 // [ptr | ?11] marked used to mark an object 142 // [0 ............ | 000] inflating inflation in progress 143 // 144 // 145 146 class BasicLock; 147 class ObjectMonitor; 148 class JavaThread; 149 class outputStream; 150 151 class markWord { 152 private: 153 uintptr_t _value; 154 155 public: 156 explicit markWord(uintptr_t value) : _value(value) {} 157 158 markWord() = default; // Doesn't initialize _value. 159 160 // It is critical for performance that this class be trivially 161 // destructable, copyable, and assignable. 162 ~markWord() = default; 163 markWord(const markWord&) = default; 164 markWord& operator=(const markWord&) = default; 165 166 static markWord from_pointer(void* ptr) { 167 return markWord((uintptr_t)ptr); 168 } 169 void* to_pointer() const { 170 return (void*)_value; 171 } 172 173 bool operator==(const markWord& other) const { 174 return _value == other._value; 175 } 176 bool operator!=(const markWord& other) const { 177 return !operator==(other); 178 } 179 180 // Conversion 181 uintptr_t value() const { return _value; } 182 183 // Constants, in least significant bit order 184 static const int lock_bits = 2; 185 static const int self_fwd_bits = 1; 186 // EnableValhalla: static prototype header bits (fast path instead of klass layout_helper) 187 static const int inline_type_bits = 1; 188 static const int null_free_array_bits = LP64_ONLY(1) NOT_LP64(0); 189 static const int flat_array_bits = LP64_ONLY(1) NOT_LP64(0); 190 // instance state 191 static const int age_bits = 4; 192 static const int larval_bits = 1; 193 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - inline_type_bits - larval_bits - flat_array_bits - null_free_array_bits - self_fwd_bits; 194 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits; 195 196 static const int lock_shift = 0; 197 static const int self_fwd_shift = lock_bits ; 198 static const int inline_type_shift = self_fwd_shift + self_fwd_bits; 199 static const int null_free_array_shift = inline_type_shift + inline_type_bits; 200 static const int flat_array_shift = null_free_array_shift + null_free_array_bits; 201 static const int age_shift = flat_array_shift + flat_array_bits; 202 static const int larval_shift = age_shift + age_bits; 203 static const int hash_shift = larval_shift + larval_bits; 204 205 static const uintptr_t lock_mask = right_n_bits(lock_bits); 206 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift; 207 static const uintptr_t self_fwd_mask = right_n_bits(self_fwd_bits); 208 static const uintptr_t self_fwd_mask_in_place = self_fwd_mask << self_fwd_shift; 209 static const uintptr_t inline_type_bit_in_place = 1 << inline_type_shift; 210 static const uintptr_t inline_type_mask = inline_type_bit_in_place + lock_mask; 211 static const uintptr_t inline_type_mask_in_place = inline_type_mask << lock_shift; 212 static const uintptr_t null_free_array_mask = right_n_bits(null_free_array_bits); 213 static const uintptr_t null_free_array_mask_in_place = (null_free_array_mask << null_free_array_shift) | lock_mask_in_place; 214 static const uintptr_t null_free_array_bit_in_place = (1 << null_free_array_shift); 215 static const uintptr_t flat_array_mask = right_n_bits(flat_array_bits); 216 static const uintptr_t flat_array_mask_in_place = (flat_array_mask << flat_array_shift) | null_free_array_mask_in_place | lock_mask_in_place; 217 static const uintptr_t flat_array_bit_in_place = (1 << flat_array_shift); 218 static const uintptr_t age_mask = right_n_bits(age_bits); 219 static const uintptr_t age_mask_in_place = age_mask << age_shift; 220 221 static const uintptr_t larval_mask = right_n_bits(larval_bits); 222 static const uintptr_t larval_mask_in_place = (larval_mask << larval_shift) | inline_type_mask_in_place; 223 static const uintptr_t larval_bit_in_place = (1 << larval_shift); 224 225 static const uintptr_t hash_mask = right_n_bits(hash_bits); 226 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift; 227 228 #ifdef _LP64 229 // Used only with compact headers: 230 // We store the (narrow) Klass* in the bits 43 to 64. 231 232 // These are for bit-precise extraction of the narrow Klass* from the 64-bit Markword 233 static constexpr int klass_offset_in_bytes = 4; 234 static constexpr int klass_shift = hash_shift + hash_bits; 235 static constexpr int klass_shift_at_offset = klass_shift - klass_offset_in_bytes * BitsPerByte; 236 static constexpr int klass_bits = 22; 237 static constexpr uintptr_t klass_mask = right_n_bits(klass_bits); 238 static constexpr uintptr_t klass_mask_in_place = klass_mask << klass_shift; 239 #endif 240 241 242 static const uintptr_t locked_value = 0; 243 static const uintptr_t unlocked_value = 1; 244 static const uintptr_t monitor_value = 2; 245 static const uintptr_t marked_value = 3; 246 247 static const uintptr_t inline_type_pattern = inline_type_bit_in_place | unlocked_value; 248 static const uintptr_t null_free_array_pattern = null_free_array_bit_in_place | unlocked_value; 249 static const uintptr_t null_free_flat_array_pattern = flat_array_bit_in_place | null_free_array_pattern; 250 static const uintptr_t nullable_flat_array_pattern = flat_array_bit_in_place | unlocked_value; 251 252 // Has static klass prototype, used for decode/encode pointer 253 static const uintptr_t static_prototype_mask = LP64_ONLY(right_n_bits(inline_type_bits + flat_array_bits + null_free_array_bits)) NOT_LP64(right_n_bits(inline_type_bits)); 254 static const uintptr_t static_prototype_mask_in_place = static_prototype_mask << lock_bits; 255 static const uintptr_t static_prototype_value_max = (1 << age_shift) - 1; 256 257 static const uintptr_t larval_pattern = larval_bit_in_place | inline_type_pattern; 258 259 static const uintptr_t no_hash = 0 ; // no hash value assigned 260 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift; 261 static const uintptr_t no_lock_in_place = unlocked_value; 262 263 static const uint max_age = age_mask; 264 265 // Creates a markWord with all bits set to zero. 266 static markWord zero() { return markWord(uintptr_t(0)); } 267 268 bool is_inline_type() const { 269 return (mask_bits(value(), inline_type_mask_in_place) == inline_type_pattern); 270 } 271 272 // lock accessors (note that these assume lock_shift == 0) 273 bool is_locked() const { 274 return (mask_bits(value(), lock_mask_in_place) != unlocked_value); 275 } 276 bool is_unlocked() const { 277 return (mask_bits(value(), lock_mask_in_place) == unlocked_value); 278 } 279 bool is_marked() const { 280 return (mask_bits(value(), lock_mask_in_place) == marked_value); 281 } 282 283 // is unlocked and not an inline type (which cannot be involved in locking, displacement or inflation) 284 // i.e. test both lock bits and the inline type bit together 285 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state 286 return (mask_bits(value(), inline_type_mask_in_place) == unlocked_value); 287 } 288 289 bool is_forwarded() const { 290 // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx). 291 return mask_bits(value(), lock_mask_in_place | self_fwd_mask_in_place) >= static_cast<intptr_t>(marked_value); 292 } 293 294 // Special temporary state of the markWord while being inflated. 295 // Code that looks at mark outside a lock need to take this into account. 296 bool is_being_inflated() const { return (value() == 0); } 297 298 // Distinguished markword value - used when inflating over 299 // an existing stack-lock. 0 indicates the markword is "BUSY". 300 // Lockword mutators that use a LD...CAS idiom should always 301 // check for and avoid overwriting a 0 value installed by some 302 // other thread. (They should spin or block instead. The 0 value 303 // is transient and *should* be short-lived). 304 // Fast-locking does not use INFLATING. 305 static markWord INFLATING() { return zero(); } // inflate-in-progress 306 307 // Should this header be preserved during GC? 308 bool must_be_preserved() const { 309 return (!is_unlocked() || !has_no_hash() || (EnableValhalla && is_larval_state())); 310 } 311 312 // WARNING: The following routines are used EXCLUSIVELY by 313 // synchronization functions. They are not really gc safe. 314 // They must get updated if markWord layout get changed. 315 markWord set_unlocked() const { 316 return markWord(value() | unlocked_value); 317 } 318 bool has_locker() const { 319 assert(LockingMode == LM_LEGACY, "should only be called with legacy stack locking"); 320 return (value() & lock_mask_in_place) == locked_value; 321 } 322 BasicLock* locker() const { 323 assert(has_locker(), "check"); 324 return (BasicLock*) value(); 325 } 326 327 bool is_fast_locked() const { 328 assert(LockingMode == LM_LIGHTWEIGHT, "should only be called with new lightweight locking"); 329 return (value() & lock_mask_in_place) == locked_value; 330 } 331 markWord set_fast_locked() const { 332 // Clear the lock_mask_in_place bits to set locked_value: 333 return markWord(value() & ~lock_mask_in_place); 334 } 335 336 bool has_monitor() const { 337 return ((value() & lock_mask_in_place) == monitor_value); 338 } 339 ObjectMonitor* monitor() const { 340 assert(has_monitor(), "check"); 341 assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors"); 342 // Use xor instead of &~ to provide one extra tag-bit check. 343 return (ObjectMonitor*) (value() ^ monitor_value); 344 } 345 bool has_displaced_mark_helper() const { 346 intptr_t lockbits = value() & lock_mask_in_place; 347 if (LockingMode == LM_LIGHTWEIGHT) { 348 return !UseObjectMonitorTable && lockbits == monitor_value; 349 } 350 // monitor (0b10) | stack-locked (0b00)? 351 return (lockbits & unlocked_value) == 0; 352 } 353 markWord displaced_mark_helper() const; 354 void set_displaced_mark_helper(markWord m) const; 355 markWord copy_set_hash(intptr_t hash) const { 356 uintptr_t tmp = value() & (~hash_mask_in_place); 357 tmp |= ((hash & hash_mask) << hash_shift); 358 return markWord(tmp); 359 } 360 // it is only used to be stored into BasicLock as the 361 // indicator that the lock is using heavyweight monitor 362 static markWord unused_mark() { 363 return markWord(marked_value); 364 } 365 // the following two functions create the markWord to be 366 // stored into object header, it encodes monitor info 367 static markWord encode(BasicLock* lock) { 368 return from_pointer(lock); 369 } 370 static markWord encode(ObjectMonitor* monitor) { 371 assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors"); 372 uintptr_t tmp = (uintptr_t) monitor; 373 return markWord(tmp | monitor_value); 374 } 375 376 markWord set_has_monitor() const { 377 return markWord((value() & ~lock_mask_in_place) | monitor_value); 378 } 379 380 // used to encode pointers during GC 381 markWord clear_lock_bits() const { return markWord(value() & ~lock_mask_in_place); } 382 383 // age operations 384 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); } 385 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); } 386 387 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); } 388 markWord set_age(uint v) const { 389 assert((v & ~age_mask) == 0, "shouldn't overflow age field"); 390 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift)); 391 } 392 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); } 393 394 // hash operations 395 intptr_t hash() const { 396 return mask_bits(value() >> hash_shift, hash_mask); 397 } 398 399 bool has_no_hash() const { 400 return hash() == no_hash; 401 } 402 403 // private buffered value operations 404 markWord enter_larval_state() const { 405 return markWord(value() | larval_bit_in_place); 406 } 407 markWord exit_larval_state() const { 408 return markWord(value() & ~larval_bit_in_place); 409 } 410 bool is_larval_state() const { 411 return (mask_bits(value(), larval_mask_in_place) == larval_pattern); 412 } 413 414 #ifdef _LP64 // 64 bit encodings only 415 bool is_flat_array() const { 416 return (mask_bits(value(), flat_array_mask_in_place) == null_free_flat_array_pattern) 417 || (mask_bits(value(), flat_array_mask_in_place) == nullable_flat_array_pattern); 418 } 419 420 bool is_null_free_array() const { 421 return (mask_bits(value(), null_free_array_mask_in_place) == null_free_array_pattern); 422 } 423 #else 424 bool is_flat_array() const { 425 fatal("Should not ask this for mark word, ask oopDesc"); 426 return false; 427 } 428 429 bool is_null_free_array() const { 430 fatal("Should not ask this for mark word, ask oopDesc"); 431 return false; 432 } 433 #endif 434 inline Klass* klass() const; 435 inline Klass* klass_or_null() const; 436 inline Klass* klass_without_asserts() const; 437 inline narrowKlass narrow_klass() const; 438 inline markWord set_narrow_klass(narrowKlass narrow_klass) const; 439 440 // Prototype mark for initialization 441 static markWord prototype() { 442 return markWord( no_hash_in_place | no_lock_in_place ); 443 } 444 445 static markWord inline_type_prototype() { 446 return markWord(inline_type_pattern); 447 } 448 449 #ifdef _LP64 // 64 bit encodings only 450 static markWord flat_array_prototype(LayoutKind lk); 451 452 static markWord null_free_array_prototype() { 453 return markWord(null_free_array_pattern); 454 } 455 #endif 456 457 // Debugging 458 void print_on(outputStream* st, bool print_monitor_info = true) const; 459 460 // Prepare address of oop for placement into mark 461 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); } 462 463 // Recover address of oop from encoded form used in mark 464 inline void* decode_pointer() const { 465 return (EnableValhalla && _value < static_prototype_value_max) ? nullptr : 466 (void*) (clear_lock_bits().value()); 467 } 468 469 inline bool is_self_forwarded() const { 470 NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");) 471 return mask_bits(value(), self_fwd_mask_in_place) != 0; 472 } 473 474 inline markWord set_self_forwarded() const { 475 NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");) 476 return markWord(value() | self_fwd_mask_in_place); 477 } 478 479 inline markWord unset_self_forwarded() const { 480 NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");) 481 return markWord(value() & ~self_fwd_mask_in_place); 482 } 483 484 inline oop forwardee() const { 485 return cast_to_oop(decode_pointer()); 486 } 487 }; 488 489 // Support atomic operations. 490 template<> 491 struct PrimitiveConversions::Translate<markWord> : public std::true_type { 492 typedef markWord Value; 493 typedef uintptr_t Decayed; 494 495 static Decayed decay(const Value& x) { return x.value(); } 496 static Value recover(Decayed x) { return Value(x); } 497 }; 498 499 #endif // SHARE_OOPS_MARKWORD_HPP