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 unused_gap:29 hash:2 -->| 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 // - With +UseCompactObjectHeaders: 57 // hashctrl bits indicate if object has been hashed: 58 // 00 - never hashed 59 // 01 - hashed, but not expanded by GC: will recompute hash 60 // 10 - not hashed, but expanded; special state used only by CDS to deal with scratch classes 61 // 11 - hashed and expanded by GC, and hashcode has been installed in hidden field 62 // 63 // When identityHashCode() is called, the transitions work as follows: 64 // 00 - set the hashctrl bits to 01, and compute the identity hash 65 // 01 - recompute idendity hash. When GC encounters 01 when moving an object, it will allocate an extra word, if 66 // necessary, for the object copy, and install 11. 67 // 11 - read hashcode from field 68 // 69 // - the two lock bits are used to describe three states: locked/unlocked and monitor. 70 // 71 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use) 72 // [header | 00] locked locked regular object header (fast-locking in use) 73 // [header | 01] unlocked regular object header 74 // [ptr | 10] monitor inflated lock (header is swapped out, UseObjectMonitorTable == false) 75 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true) 76 // [ptr | 11] marked used to mark an object 77 // [0 ............ 0| 00] inflating inflation in progress (stack-locking in use) 78 // 79 // We assume that stack/thread pointers have the lowest two bits cleared. 80 // 81 // - INFLATING() is a distinguished markword value of all zeros that is 82 // used when inflating an existing stack-lock into an ObjectMonitor. 83 // See below for is_being_inflated() and INFLATING(). 84 85 class BasicLock; 86 class ObjectMonitor; 87 class JavaThread; 88 class outputStream; 89 90 class markWord { 91 private: 92 uintptr_t _value; 93 94 public: 95 explicit markWord(uintptr_t value) : _value(value) {} 96 97 markWord() = default; // Doesn't initialize _value. 98 99 // It is critical for performance that this class be trivially 100 // destructable, copyable, and assignable. 101 ~markWord() = default; 102 markWord(const markWord&) = default; 103 markWord& operator=(const markWord&) = default; 104 105 static markWord from_pointer(void* ptr) { 106 return markWord((uintptr_t)ptr); 107 } 108 void* to_pointer() const { 109 return (void*)_value; 110 } 111 112 bool operator==(const markWord& other) const { 113 return _value == other._value; 114 } 115 bool operator!=(const markWord& other) const { 116 return !operator==(other); 117 } 118 119 // Conversion 120 uintptr_t value() const { return _value; } 121 uint32_t value32() const { return (uint32_t)_value; } 122 123 // Constants 124 static const int age_bits = 4; 125 static const int lock_bits = 2; 126 static const int self_fwd_bits = 1; 127 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - self_fwd_bits; 128 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits; 129 static const int unused_gap_bits = LP64_ONLY(4) NOT_LP64(0); // Reserved for Valhalla. 130 static const int hashctrl_bits = 2; 131 132 static const int lock_shift = 0; 133 static const int self_fwd_shift = lock_shift + lock_bits; 134 static const int age_shift = self_fwd_shift + self_fwd_bits; 135 static const int hash_shift = age_shift + age_bits + unused_gap_bits; 136 static const int hashctrl_shift = age_shift + age_bits + unused_gap_bits; 137 138 static const uintptr_t lock_mask = right_n_bits(lock_bits); 139 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift; 140 static const uintptr_t self_fwd_mask = right_n_bits(self_fwd_bits); 141 static const uintptr_t self_fwd_mask_in_place = self_fwd_mask << self_fwd_shift; 142 static const uintptr_t age_mask = right_n_bits(age_bits); 143 static const uintptr_t age_mask_in_place = age_mask << age_shift; 144 static const uintptr_t hash_mask = right_n_bits(hash_bits); 145 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift; 146 static const uintptr_t hashctrl_mask = right_n_bits(hashctrl_bits); 147 static const uintptr_t hashctrl_mask_in_place = hashctrl_mask << hashctrl_shift; 148 static const uintptr_t hashctrl_hashed_mask_in_place = ((uintptr_t)1) << hashctrl_shift; 149 static const uintptr_t hashctrl_expanded_mask_in_place = ((uintptr_t)2) << hashctrl_shift; 150 151 #ifdef _LP64 152 // Used only with compact headers: 153 // We store the (narrow) Klass* in the bits 43 to 64. 154 155 // These are for bit-precise extraction of the narrow Klass* from the 64-bit Markword 156 static constexpr int klass_shift = hashctrl_shift + hashctrl_bits; 157 static constexpr int klass_bits = 19; 158 static constexpr uintptr_t klass_mask = right_n_bits(klass_bits); 159 static constexpr uintptr_t klass_mask_in_place = klass_mask << klass_shift; 160 #endif 161 162 163 static const uintptr_t locked_value = 0; 164 static const uintptr_t unlocked_value = 1; 165 static const uintptr_t monitor_value = 2; 166 static const uintptr_t marked_value = 3; 167 static const uintptr_t forward_expanded_value = 0b111; 168 169 static const uintptr_t no_hash = 0 ; // no hash value assigned 170 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift; 171 static const uintptr_t no_lock_in_place = unlocked_value; 172 173 static const uint max_age = age_mask; 174 175 // Creates a markWord with all bits set to zero. 176 static markWord zero() { return markWord(uintptr_t(0)); } 177 178 // lock accessors (note that these assume lock_shift == 0) 179 bool is_locked() const { 180 return (mask_bits(value(), lock_mask_in_place) != unlocked_value); 181 } 182 bool is_unlocked() const { 183 return (mask_bits(value(), lock_mask_in_place) == unlocked_value); 184 } 185 bool is_marked() const { 186 return (value() & (self_fwd_mask_in_place | lock_mask_in_place)) > monitor_value; 187 } 188 bool is_forwarded() const { 189 // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx). 190 return mask_bits(value(), lock_mask_in_place | self_fwd_mask_in_place) >= static_cast<intptr_t>(marked_value); 191 } 192 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state 193 return (mask_bits(value(), lock_mask_in_place) == unlocked_value); 194 } 195 196 markWord set_forward_expanded() { 197 assert((value() & (lock_mask_in_place | self_fwd_mask_in_place)) == marked_value, "must be normal-forwarded here"); 198 return markWord(value() | forward_expanded_value); 199 } 200 bool is_forward_expanded() { 201 return (value() & (lock_mask_in_place | self_fwd_mask_in_place)) == forward_expanded_value; 202 } 203 204 // Special temporary state of the markWord while being inflated. 205 // Code that looks at mark outside a lock need to take this into account. 206 bool is_being_inflated() const { return (value() == 0); } 207 208 // Distinguished markword value - used when inflating over 209 // an existing stack-lock. 0 indicates the markword is "BUSY". 210 // Lockword mutators that use a LD...CAS idiom should always 211 // check for and avoid overwriting a 0 value installed by some 212 // other thread. (They should spin or block instead. The 0 value 213 // is transient and *should* be short-lived). 214 // Fast-locking does not use INFLATING. 215 static markWord INFLATING() { return zero(); } // inflate-in-progress 216 217 // Should this header be preserved during GC? 218 bool must_be_preserved() const { 219 return UseCompactObjectHeaders ? !is_unlocked() : (!is_unlocked() || !has_no_hash()); 220 } 221 222 // WARNING: The following routines are used EXCLUSIVELY by 223 // synchronization functions. They are not really gc safe. 224 // They must get updated if markWord layout get changed. 225 markWord set_unlocked() const { 226 return markWord(value() | unlocked_value); 227 } 228 bool has_locker() const { 229 assert(LockingMode == LM_LEGACY, "should only be called with legacy stack locking"); 230 return (value() & lock_mask_in_place) == locked_value; 231 } 232 BasicLock* locker() const { 233 assert(has_locker(), "check"); 234 return (BasicLock*) value(); 235 } 236 237 bool is_fast_locked() const { 238 assert(LockingMode == LM_LIGHTWEIGHT, "should only be called with new lightweight locking"); 239 return (value() & lock_mask_in_place) == locked_value; 240 } 241 markWord set_fast_locked() const { 242 // Clear the lock_mask_in_place bits to set locked_value: 243 return markWord(value() & ~lock_mask_in_place); 244 } 245 246 bool has_monitor() const { 247 return ((value() & lock_mask_in_place) == monitor_value); 248 } 249 ObjectMonitor* monitor() const { 250 assert(has_monitor(), "check"); 251 assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors"); 252 // Use xor instead of &~ to provide one extra tag-bit check. 253 return (ObjectMonitor*) (value() ^ monitor_value); 254 } 255 bool has_displaced_mark_helper() const { 256 intptr_t lockbits = value() & lock_mask_in_place; 257 if (LockingMode == LM_LIGHTWEIGHT) { 258 return !UseObjectMonitorTable && lockbits == monitor_value; 259 } 260 // monitor (0b10) | stack-locked (0b00)? 261 return (lockbits & unlocked_value) == 0; 262 } 263 markWord displaced_mark_helper() const; 264 void set_displaced_mark_helper(markWord m) const; 265 markWord copy_set_hash(intptr_t hash) const { 266 assert(!UseCompactObjectHeaders, "Do not use with compact i-hash"); 267 uintptr_t tmp = value() & (~hash_mask_in_place); 268 tmp |= ((hash & hash_mask) << hash_shift); 269 return markWord(tmp); 270 } 271 // it is only used to be stored into BasicLock as the 272 // indicator that the lock is using heavyweight monitor 273 static markWord unused_mark() { 274 return markWord(marked_value); 275 } 276 // the following two functions create the markWord to be 277 // stored into object header, it encodes monitor info 278 static markWord encode(BasicLock* lock) { 279 return from_pointer(lock); 280 } 281 static markWord encode(ObjectMonitor* monitor) { 282 assert(!UseObjectMonitorTable, "Lightweight locking with OM table does not use markWord for monitors"); 283 uintptr_t tmp = (uintptr_t) monitor; 284 return markWord(tmp | monitor_value); 285 } 286 287 markWord set_has_monitor() const { 288 return markWord((value() & ~lock_mask_in_place) | monitor_value); 289 } 290 291 // used to encode pointers during GC 292 markWord clear_lock_bits() const { return markWord(value() & ~(lock_mask_in_place | self_fwd_mask_in_place)); } 293 294 // age operations 295 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); } 296 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); } 297 298 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); } 299 markWord set_age(uint v) const { 300 assert((v & ~age_mask) == 0, "shouldn't overflow age field"); 301 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift)); 302 } 303 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); } 304 305 // hash operations 306 intptr_t hash() const { 307 assert(!UseCompactObjectHeaders, "only without compact i-hash"); 308 return mask_bits(value() >> hash_shift, hash_mask); 309 } 310 311 bool has_no_hash() const { 312 if (UseCompactObjectHeaders) { 313 return !is_hashed(); 314 } else { 315 return hash() == no_hash; 316 } 317 } 318 319 inline bool is_hashed_not_expanded() const { 320 assert(UseCompactObjectHeaders, "only with compact i-hash"); 321 return (value() & hashctrl_mask_in_place) == hashctrl_hashed_mask_in_place; 322 } 323 inline markWord set_hashed_not_expanded() const { 324 assert(UseCompactObjectHeaders, "only with compact i-hash"); 325 return markWord((value() & ~hashctrl_mask_in_place) | hashctrl_hashed_mask_in_place); 326 } 327 328 inline bool is_hashed_expanded() const { 329 assert(UseCompactObjectHeaders, "only with compact i-hash"); 330 return (value() & hashctrl_mask_in_place) == (hashctrl_hashed_mask_in_place | hashctrl_expanded_mask_in_place); 331 } 332 inline markWord set_hashed_expanded() const { 333 assert(UseCompactObjectHeaders, "only with compact i-hash"); 334 return markWord((value() & ~hashctrl_mask_in_place) | (hashctrl_hashed_mask_in_place | hashctrl_expanded_mask_in_place)); 335 } 336 337 // This is a special hashctrl state (11) that is only used 338 // during CDS archive dumping. There we allocate 'scratch mirrors' for 339 // each real mirror klass. We allocate those scratch mirrors 340 // in a pre-extended form, but without being hashed. When the 341 // real mirror gets hashed, then we turn the scratch mirror into 342 // hashed_moved state, otherwise we leave it in that special state 343 // which indicates that the archived copy will be allocated in the 344 // unhashed form. 345 inline bool is_not_hashed_expanded() const { 346 assert(UseCompactObjectHeaders, "only with compact i-hash"); 347 return (value() & hashctrl_mask_in_place) == hashctrl_expanded_mask_in_place; 348 } 349 inline markWord set_not_hashed_expanded() const { 350 assert(UseCompactObjectHeaders, "only with compact i-hash"); 351 return markWord((value() & ~hashctrl_mask_in_place) | hashctrl_expanded_mask_in_place); 352 } 353 // Return true when object is either hashed_moved or not_hashed_moved. 354 inline bool is_expanded() const { 355 assert(UseCompactObjectHeaders, "only with compact i-hash"); 356 return (value() & hashctrl_expanded_mask_in_place) != 0; 357 } 358 inline bool is_hashed() const { 359 assert(UseCompactObjectHeaders, "only with compact i-hash"); 360 return (value() & hashctrl_hashed_mask_in_place) != 0; 361 } 362 363 inline markWord copy_hashctrl_from(markWord m) const { 364 if (UseCompactObjectHeaders) { 365 return markWord((value() & ~hashctrl_mask_in_place) | (m.value() & hashctrl_mask_in_place)); 366 } else { 367 return markWord(value()); 368 } 369 } 370 371 inline Klass* klass() const; 372 inline Klass* klass_or_null() const; 373 inline Klass* klass_without_asserts() const; 374 inline narrowKlass narrow_klass() const; 375 inline markWord set_narrow_klass(narrowKlass narrow_klass) const; 376 377 #ifdef _LP64 378 inline int array_length() { return checked_cast<int>(value() >> 32); } 379 #endif 380 381 // Prototype mark for initialization 382 static markWord prototype() { 383 if (UseCompactObjectHeaders) { 384 return markWord(no_lock_in_place); 385 } else { 386 return markWord(no_hash_in_place | no_lock_in_place); 387 } 388 } 389 390 // Debugging 391 void print_on(outputStream* st, bool print_monitor_info = true) const; 392 393 // Prepare address of oop for placement into mark 394 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); } 395 396 // Recover address of oop from encoded form used in mark 397 inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); } 398 399 inline bool is_self_forwarded() const { 400 NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");) 401 // Match 100, 101, 110 but not 111. 402 return mask_bits(value() + 1, (lock_mask_in_place | self_fwd_mask_in_place)) > 4; 403 } 404 405 inline markWord set_self_forwarded() const { 406 NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");) 407 return markWord(value() | self_fwd_mask_in_place); 408 } 409 410 inline markWord unset_self_forwarded() const { 411 NOT_LP64(assert(LockingMode != LM_LEGACY, "incorrect with LM_LEGACY on 32 bit");) 412 return markWord(value() & ~self_fwd_mask_in_place); 413 } 414 415 inline oop forwardee() const { 416 return cast_to_oop(decode_pointer()); 417 } 418 }; 419 420 // Support atomic operations. 421 template<> 422 struct PrimitiveConversions::Translate<markWord> : public std::true_type { 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