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