1 /*
2 * Copyright (c) 2018, 2021, Red Hat, Inc. 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 #include "precompiled.hpp"
26 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
27 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
28 #include "gc/shenandoah/shenandoahForwarding.hpp"
29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
30 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
31 #include "gc/shenandoah/shenandoahRuntime.hpp"
32 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
33 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
34 #include "interpreter/interpreter.hpp"
35 #include "interpreter/interp_masm.hpp"
36 #include "runtime/sharedRuntime.hpp"
37 #include "runtime/thread.hpp"
38 #ifdef COMPILER1
39 #include "c1/c1_LIRAssembler.hpp"
40 #include "c1/c1_MacroAssembler.hpp"
41 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
42 #endif
43
44 #define __ masm->
45
arraycopy_prologue(MacroAssembler * masm,DecoratorSet decorators,bool is_oop,Register src,Register dst,Register count,RegSet saved_regs)46 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
47 Register src, Register dst, Register count, RegSet saved_regs) {
48 if (is_oop) {
49 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
50 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahIUBarrier || ShenandoahLoadRefBarrier) {
51
52 Label done;
53
54 // Avoid calling runtime if count == 0
55 __ cbz(count, done);
56
57 // Is GC active?
58 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
59 __ ldrb(rscratch1, gc_state);
60 if (ShenandoahSATBBarrier && dest_uninitialized) {
61 __ tbz(rscratch1, ShenandoahHeap::HAS_FORWARDED_BITPOS, done);
62 } else {
63 __ mov(rscratch2, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING);
64 __ tst(rscratch1, rscratch2);
65 __ br(Assembler::EQ, done);
66 }
67
68 __ push(saved_regs, sp);
69 if (UseCompressedOops) {
70 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop_entry), src, dst, count);
71 } else {
72 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop_entry), src, dst, count);
73 }
74 __ pop(saved_regs, sp);
75 __ bind(done);
76 }
77 }
78 }
79
shenandoah_write_barrier_pre(MacroAssembler * masm,Register obj,Register pre_val,Register thread,Register tmp,bool tosca_live,bool expand_call)80 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm,
81 Register obj,
82 Register pre_val,
83 Register thread,
84 Register tmp,
85 bool tosca_live,
86 bool expand_call) {
87 if (ShenandoahSATBBarrier) {
88 satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, tosca_live, expand_call);
89 }
90 }
91
satb_write_barrier_pre(MacroAssembler * masm,Register obj,Register pre_val,Register thread,Register tmp,bool tosca_live,bool expand_call)92 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm,
93 Register obj,
94 Register pre_val,
95 Register thread,
96 Register tmp,
97 bool tosca_live,
98 bool expand_call) {
99 // If expand_call is true then we expand the call_VM_leaf macro
100 // directly to skip generating the check by
101 // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
102
103 assert(thread == rthread, "must be");
104
105 Label done;
106 Label runtime;
107
108 assert_different_registers(obj, pre_val, tmp, rscratch1);
109 assert(pre_val != noreg && tmp != noreg, "expecting a register");
110
111 Address in_progress(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()));
112 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
113 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
114
115 // Is marking active?
116 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
117 __ ldrw(tmp, in_progress);
118 } else {
119 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
120 __ ldrb(tmp, in_progress);
121 }
122 __ cbzw(tmp, done);
123
124 // Do we need to load the previous value?
125 if (obj != noreg) {
126 __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
127 }
128
129 // Is the previous value null?
130 __ cbz(pre_val, done);
131
132 // Can we store original value in the thread's buffer?
133 // Is index == 0?
134 // (The index field is typed as size_t.)
135
136 __ ldr(tmp, index); // tmp := *index_adr
137 __ cbz(tmp, runtime); // tmp == 0?
138 // If yes, goto runtime
139
140 __ sub(tmp, tmp, wordSize); // tmp := tmp - wordSize
141 __ str(tmp, index); // *index_adr := tmp
142 __ ldr(rscratch1, buffer);
143 __ add(tmp, tmp, rscratch1); // tmp := tmp + *buffer_adr
144
145 // Record the previous value
146 __ str(pre_val, Address(tmp, 0));
147 __ b(done);
148
149 __ bind(runtime);
150 // save the live input values
151 RegSet saved = RegSet::of(pre_val);
152 if (tosca_live) saved += RegSet::of(r0);
153 if (obj != noreg) saved += RegSet::of(obj);
154
155 __ push(saved, sp);
156
157 // Calling the runtime using the regular call_VM_leaf mechanism generates
158 // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
159 // that checks that the *(rfp+frame::interpreter_frame_last_sp) == NULL.
160 //
161 // If we care generating the pre-barrier without a frame (e.g. in the
162 // intrinsified Reference.get() routine) then ebp might be pointing to
163 // the caller frame and so this check will most likely fail at runtime.
164 //
165 // Expanding the call directly bypasses the generation of the check.
166 // So when we do not have have a full interpreter frame on the stack
167 // expand_call should be passed true.
168
169 if (expand_call) {
170 assert(pre_val != c_rarg1, "smashed arg");
171 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
172 } else {
173 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
174 }
175
176 __ pop(saved, sp);
177
178 __ bind(done);
179 }
180
resolve_forward_pointer(MacroAssembler * masm,Register dst,Register tmp)181 void ShenandoahBarrierSetAssembler::resolve_forward_pointer(MacroAssembler* masm, Register dst, Register tmp) {
182 assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled");
183 Label is_null;
184 __ cbz(dst, is_null);
185 resolve_forward_pointer_not_null(masm, dst, tmp);
186 __ bind(is_null);
187 }
188
189 // IMPORTANT: This must preserve all registers, even rscratch1 and rscratch2, except those explicitely
190 // passed in.
resolve_forward_pointer_not_null(MacroAssembler * masm,Register dst,Register tmp)191 void ShenandoahBarrierSetAssembler::resolve_forward_pointer_not_null(MacroAssembler* masm, Register dst, Register tmp) {
192 assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled");
193 // The below loads the mark word, checks if the lowest two bits are
194 // set, and if so, clear the lowest two bits and copy the result
195 // to dst. Otherwise it leaves dst alone.
196 // Implementing this is surprisingly awkward. I do it here by:
197 // - Inverting the mark word
198 // - Test lowest two bits == 0
199 // - If so, set the lowest two bits
200 // - Invert the result back, and copy to dst
201
202 bool borrow_reg = (tmp == noreg);
203 if (borrow_reg) {
204 // No free registers available. Make one useful.
205 tmp = rscratch1;
206 if (tmp == dst) {
207 tmp = rscratch2;
208 }
209 __ push(RegSet::of(tmp), sp);
210 }
211
212 assert_different_registers(tmp, dst);
213
214 Label done;
215 __ ldr(tmp, Address(dst, oopDesc::mark_offset_in_bytes()));
216 __ eon(tmp, tmp, zr);
217 __ ands(zr, tmp, markWord::lock_mask_in_place);
218 __ br(Assembler::NE, done);
219 __ orr(tmp, tmp, markWord::marked_value);
220 __ eon(dst, tmp, zr);
221 __ bind(done);
222
223 if (borrow_reg) {
224 __ pop(RegSet::of(tmp), sp);
225 }
226 }
227
load_reference_barrier(MacroAssembler * masm,Register dst,Address load_addr,DecoratorSet decorators)228 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Address load_addr, DecoratorSet decorators) {
229 assert(ShenandoahLoadRefBarrier, "Should be enabled");
230 assert(dst != rscratch2, "need rscratch2");
231 assert_different_registers(load_addr.base(), load_addr.index(), rscratch1, rscratch2);
232
233 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
234 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
235 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
236 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
237 bool is_narrow = UseCompressedOops && !is_native;
238
239 Label heap_stable, not_cset;
240 __ enter();
241 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
242 __ ldrb(rscratch2, gc_state);
243
244 // Check for heap stability
245 __ tbz(rscratch2, ShenandoahHeap::HAS_FORWARDED_BITPOS, heap_stable);
246
247 // use r1 for load address
248 Register result_dst = dst;
249 if (dst == r1) {
250 __ mov(rscratch1, dst);
251 dst = rscratch1;
252 }
253
254 // Save r0 and r1, unless it is an output register
255 RegSet to_save = RegSet::of(r0, r1) - result_dst;
256 __ push(to_save, sp);
257 __ lea(r1, load_addr);
258 __ mov(r0, dst);
259
260 // Test for in-cset
261 if (is_strong) {
262 __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr());
263 __ lsr(rscratch1, r0, ShenandoahHeapRegion::region_size_bytes_shift_jint());
264 __ ldrb(rscratch2, Address(rscratch2, rscratch1));
265 __ tbz(rscratch2, 0, not_cset);
266 }
267
268 __ push_call_clobbered_registers();
269 if (is_strong) {
270 if (is_narrow) {
271 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow));
272 } else {
273 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
274 }
275 } else if (is_weak) {
276 if (is_narrow) {
277 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow));
278 } else {
279 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak));
280 }
281 } else {
282 assert(is_phantom, "only remaining strength");
283 assert(!is_narrow, "phantom access cannot be narrow");
284 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak));
285 }
286 __ blr(lr);
287 __ mov(rscratch1, r0);
288 __ pop_call_clobbered_registers();
289 __ mov(r0, rscratch1);
290
291 __ bind(not_cset);
292
293 __ mov(result_dst, r0);
294 __ pop(to_save, sp);
295
296 __ bind(heap_stable);
297 __ leave();
298 }
299
iu_barrier(MacroAssembler * masm,Register dst,Register tmp)300 void ShenandoahBarrierSetAssembler::iu_barrier(MacroAssembler* masm, Register dst, Register tmp) {
301 if (ShenandoahIUBarrier) {
302 __ push_call_clobbered_registers();
303 satb_write_barrier_pre(masm, noreg, dst, rthread, tmp, true, false);
304 __ pop_call_clobbered_registers();
305 }
306 }
307
308 //
309 // Arguments:
310 //
311 // Inputs:
312 // src: oop location to load from, might be clobbered
313 //
314 // Output:
315 // dst: oop loaded from src location
316 //
317 // Kill:
318 // rscratch1 (scratch reg)
319 //
320 // Alias:
321 // dst: rscratch1 (might use rscratch1 as temporary output register to avoid clobbering src)
322 //
load_at(MacroAssembler * masm,DecoratorSet decorators,BasicType type,Register dst,Address src,Register tmp1,Register tmp_thread)323 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
324 Register dst, Address src, Register tmp1, Register tmp_thread) {
325 // 1: non-reference load, no additional barrier is needed
326 if (!is_reference_type(type)) {
327 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
328 return;
329 }
330
331 // 2: load a reference from src location and apply LRB if needed
332 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
333 Register result_dst = dst;
334
335 // Preserve src location for LRB
336 if (dst == src.base() || dst == src.index()) {
337 dst = rscratch1;
338 }
339 assert_different_registers(dst, src.base(), src.index());
340
341 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
342
343 load_reference_barrier(masm, dst, src, decorators);
344
345 if (dst != result_dst) {
346 __ mov(result_dst, dst);
347 dst = result_dst;
348 }
349 } else {
350 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
351 }
352
353 // 3: apply keep-alive barrier if needed
354 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
355 __ enter();
356 __ push_call_clobbered_registers();
357 satb_write_barrier_pre(masm /* masm */,
358 noreg /* obj */,
359 dst /* pre_val */,
360 rthread /* thread */,
361 tmp1 /* tmp */,
362 true /* tosca_live */,
363 true /* expand_call */);
364 __ pop_call_clobbered_registers();
365 __ leave();
366 }
367 }
368
store_at(MacroAssembler * masm,DecoratorSet decorators,BasicType type,Address dst,Register val,Register tmp1,Register tmp2)369 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
370 Address dst, Register val, Register tmp1, Register tmp2) {
371 bool on_oop = is_reference_type(type);
372 if (!on_oop) {
373 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2);
374 return;
375 }
376
377 // flatten object address if needed
378 if (dst.index() == noreg && dst.offset() == 0) {
379 if (dst.base() != r3) {
380 __ mov(r3, dst.base());
381 }
382 } else {
383 __ lea(r3, dst);
384 }
385
386 shenandoah_write_barrier_pre(masm,
387 r3 /* obj */,
388 tmp2 /* pre_val */,
389 rthread /* thread */,
390 tmp1 /* tmp */,
391 val != noreg /* tosca_live */,
392 false /* expand_call */);
393
394 if (val == noreg) {
395 BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), noreg, noreg, noreg);
396 } else {
397 iu_barrier(masm, val, tmp1);
398 // G1 barrier needs uncompressed oop for region cross check.
399 Register new_val = val;
400 if (UseCompressedOops) {
401 new_val = rscratch2;
402 __ mov(new_val, val);
403 }
404 BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), val, noreg, noreg);
405 }
406
407 }
408
try_resolve_jobject_in_native(MacroAssembler * masm,Register jni_env,Register obj,Register tmp,Label & slowpath)409 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
410 Register obj, Register tmp, Label& slowpath) {
411 Label done;
412 // Resolve jobject
413 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
414
415 // Check for null.
416 __ cbz(obj, done);
417
418 assert(obj != rscratch2, "need rscratch2");
419 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
420 __ lea(rscratch2, gc_state);
421 __ ldrb(rscratch2, Address(rscratch2));
422
423 // Check for heap in evacuation phase
424 __ tbnz(rscratch2, ShenandoahHeap::EVACUATION_BITPOS, slowpath);
425
426 __ bind(done);
427 }
428
429 // Special Shenandoah CAS implementation that handles false negatives due
430 // to concurrent evacuation. The service is more complex than a
431 // traditional CAS operation because the CAS operation is intended to
432 // succeed if the reference at addr exactly matches expected or if the
433 // reference at addr holds a pointer to a from-space object that has
434 // been relocated to the location named by expected. There are two
435 // races that must be addressed:
436 // a) A parallel thread may mutate the contents of addr so that it points
437 // to a different object. In this case, the CAS operation should fail.
438 // b) A parallel thread may heal the contents of addr, replacing a
439 // from-space pointer held in addr with the to-space pointer
440 // representing the new location of the object.
441 // Upon entry to cmpxchg_oop, it is assured that new_val equals NULL
442 // or it refers to an object that is not being evacuated out of
443 // from-space, or it refers to the to-space version of an object that
444 // is being evacuated out of from-space.
445 //
446 // By default the value held in the result register following execution
447 // of the generated code sequence is 0 to indicate failure of CAS,
448 // non-zero to indicate success. If is_cae, the result is the value most
449 // recently fetched from addr rather than a boolean success indicator.
450 //
451 // Clobbers rscratch1, rscratch2
cmpxchg_oop(MacroAssembler * masm,Register addr,Register expected,Register new_val,bool acquire,bool release,bool is_cae,Register result)452 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm,
453 Register addr,
454 Register expected,
455 Register new_val,
456 bool acquire, bool release,
457 bool is_cae,
458 Register result) {
459 Register tmp1 = rscratch1;
460 Register tmp2 = rscratch2;
461 bool is_narrow = UseCompressedOops;
462 Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword;
463
464 assert_different_registers(addr, expected, tmp1, tmp2);
465 assert_different_registers(addr, new_val, tmp1, tmp2);
466
467 Label step4, done;
468
469 // There are two ways to reach this label. Initial entry into the
470 // cmpxchg_oop code expansion starts at step1 (which is equivalent
471 // to label step4). Additionally, in the rare case that four steps
472 // are required to perform the requested operation, the fourth step
473 // is the same as the first. On a second pass through step 1,
474 // control may flow through step 2 on its way to failure. It will
475 // not flow from step 2 to step 3 since we are assured that the
476 // memory at addr no longer holds a from-space pointer.
477 //
478 // The comments that immediately follow the step4 label apply only
479 // to the case in which control reaches this label by branch from
480 // step 3.
481
482 __ bind (step4);
483
484 // Step 4. CAS has failed because the value most recently fetched
485 // from addr is no longer the from-space pointer held in tmp2. If a
486 // different thread replaced the in-memory value with its equivalent
487 // to-space pointer, then CAS may still be able to succeed. The
488 // value held in the expected register has not changed.
489 //
490 // It is extremely rare we reach this point. For this reason, the
491 // implementation opts for smaller rather than potentially faster
492 // code. Ultimately, smaller code for this rare case most likely
493 // delivers higher overall throughput by enabling improved icache
494 // performance.
495
496 // Step 1. Fast-path.
497 //
498 // Try to CAS with given arguments. If successful, then we are done.
499 //
500 // No label required for step 1.
501
502 __ cmpxchg(addr, expected, new_val, size, acquire, release, false, tmp2);
503 // EQ flag set iff success. tmp2 holds value fetched.
504
505 // If expected equals null but tmp2 does not equal null, the
506 // following branches to done to report failure of CAS. If both
507 // expected and tmp2 equal null, the following branches to done to
508 // report success of CAS. There's no need for a special test of
509 // expected equal to null.
510
511 __ br(Assembler::EQ, done);
512 // if CAS failed, fall through to step 2
513
514 // Step 2. CAS has failed because the value held at addr does not
515 // match expected. This may be a false negative because the value fetched
516 // from addr (now held in tmp2) may be a from-space pointer to the
517 // original copy of same object referenced by to-space pointer expected.
518 //
519 // To resolve this, it suffices to find the forward pointer associated
520 // with fetched value. If this matches expected, retry CAS with new
521 // parameters. If this mismatches, then we have a legitimate
522 // failure, and we're done.
523 //
524 // No need for step2 label.
525
526 // overwrite tmp1 with from-space pointer fetched from memory
527 __ mov(tmp1, tmp2);
528
529 if (is_narrow) {
530 // Decode tmp1 in order to resolve its forward pointer
531 __ decode_heap_oop(tmp1, tmp1);
532 }
533 resolve_forward_pointer(masm, tmp1);
534 // Encode tmp1 to compare against expected.
535 __ encode_heap_oop(tmp1, tmp1);
536
537 // Does forwarded value of fetched from-space pointer match original
538 // value of expected? If tmp1 holds null, this comparison will fail
539 // because we know from step1 that expected is not null. There is
540 // no need for a separate test for tmp1 (the value originally held
541 // in memory) equal to null.
542 __ cmp(tmp1, expected);
543
544 // If not, then the failure was legitimate and we're done.
545 // Branching to done with NE condition denotes failure.
546 __ br(Assembler::NE, done);
547
548 // Fall through to step 3. No need for step3 label.
549
550 // Step 3. We've confirmed that the value originally held in memory
551 // (now held in tmp2) pointed to from-space version of original
552 // expected value. Try the CAS again with the from-space expected
553 // value. If it now succeeds, we're good.
554 //
555 // Note: tmp2 holds encoded from-space pointer that matches to-space
556 // object residing at expected. tmp2 is the new "expected".
557
558 // Note that macro implementation of __cmpxchg cannot use same register
559 // tmp2 for result and expected since it overwrites result before it
560 // compares result with expected.
561 __ cmpxchg(addr, tmp2, new_val, size, acquire, release, false, noreg);
562 // EQ flag set iff success. tmp2 holds value fetched, tmp1 (rscratch1) clobbered.
563
564 // If fetched value did not equal the new expected, this could
565 // still be a false negative because some other thread may have
566 // newly overwritten the memory value with its to-space equivalent.
567 __ br(Assembler::NE, step4);
568
569 if (is_cae) {
570 // We're falling through to done to indicate success. Success
571 // with is_cae is denoted by returning the value of expected as
572 // result.
573 __ mov(tmp2, expected);
574 }
575
576 __ bind(done);
577 // At entry to done, the Z (EQ) flag is on iff if the CAS
578 // operation was successful. Additionally, if is_cae, tmp2 holds
579 // the value most recently fetched from addr. In this case, success
580 // is denoted by tmp2 matching expected.
581
582 if (is_cae) {
583 __ mov(result, tmp2);
584 } else {
585 __ cset(result, Assembler::EQ);
586 }
587 }
588
589 #undef __
590
591 #ifdef COMPILER1
592
593 #define __ ce->masm()->
594
gen_pre_barrier_stub(LIR_Assembler * ce,ShenandoahPreBarrierStub * stub)595 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
596 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
597 // At this point we know that marking is in progress.
598 // If do_load() is true then we have to emit the
599 // load of the previous value; otherwise it has already
600 // been loaded into _pre_val.
601
602 __ bind(*stub->entry());
603
604 assert(stub->pre_val()->is_register(), "Precondition.");
605
606 Register pre_val_reg = stub->pre_val()->as_register();
607
608 if (stub->do_load()) {
609 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/);
610 }
611 __ cbz(pre_val_reg, *stub->continuation());
612 ce->store_parameter(stub->pre_val()->as_register(), 0);
613 __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
614 __ b(*stub->continuation());
615 }
616
gen_load_reference_barrier_stub(LIR_Assembler * ce,ShenandoahLoadReferenceBarrierStub * stub)617 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
618 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
619 __ bind(*stub->entry());
620
621 DecoratorSet decorators = stub->decorators();
622 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
623 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
624 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
625 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
626
627 Register obj = stub->obj()->as_register();
628 Register res = stub->result()->as_register();
629 Register addr = stub->addr()->as_pointer_register();
630 Register tmp1 = stub->tmp1()->as_register();
631 Register tmp2 = stub->tmp2()->as_register();
632
633 assert(res == r0, "result must arrive in r0");
634
635 if (res != obj) {
636 __ mov(res, obj);
637 }
638
639 if (is_strong) {
640 // Check for object in cset.
641 __ mov(tmp2, ShenandoahHeap::in_cset_fast_test_addr());
642 __ lsr(tmp1, res, ShenandoahHeapRegion::region_size_bytes_shift_jint());
643 __ ldrb(tmp2, Address(tmp2, tmp1));
644 __ cbz(tmp2, *stub->continuation());
645 }
646
647 ce->store_parameter(res, 0);
648 ce->store_parameter(addr, 1);
649 if (is_strong) {
650 if (is_native) {
651 __ far_call(RuntimeAddress(bs->load_reference_barrier_strong_native_rt_code_blob()->code_begin()));
652 } else {
653 __ far_call(RuntimeAddress(bs->load_reference_barrier_strong_rt_code_blob()->code_begin()));
654 }
655 } else if (is_weak) {
656 __ far_call(RuntimeAddress(bs->load_reference_barrier_weak_rt_code_blob()->code_begin()));
657 } else {
658 assert(is_phantom, "only remaining strength");
659 __ far_call(RuntimeAddress(bs->load_reference_barrier_phantom_rt_code_blob()->code_begin()));
660 }
661
662 __ b(*stub->continuation());
663 }
664
665 #undef __
666
667 #define __ sasm->
668
generate_c1_pre_barrier_runtime_stub(StubAssembler * sasm)669 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
670 __ prologue("shenandoah_pre_barrier", false);
671
672 // arg0 : previous value of memory
673
674 BarrierSet* bs = BarrierSet::barrier_set();
675
676 const Register pre_val = r0;
677 const Register thread = rthread;
678 const Register tmp = rscratch1;
679
680 Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
681 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
682
683 Label done;
684 Label runtime;
685
686 // Is marking still active?
687 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
688 __ ldrb(tmp, gc_state);
689 __ mov(rscratch2, ShenandoahHeap::MARKING);
690 __ tst(tmp, rscratch2);
691 __ br(Assembler::EQ, done);
692
693 // Can we store original value in the thread's buffer?
694 __ ldr(tmp, queue_index);
695 __ cbz(tmp, runtime);
696
697 __ sub(tmp, tmp, wordSize);
698 __ str(tmp, queue_index);
699 __ ldr(rscratch2, buffer);
700 __ add(tmp, tmp, rscratch2);
701 __ load_parameter(0, rscratch2);
702 __ str(rscratch2, Address(tmp, 0));
703 __ b(done);
704
705 __ bind(runtime);
706 __ push_call_clobbered_registers();
707 __ load_parameter(0, pre_val);
708 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
709 __ pop_call_clobbered_registers();
710 __ bind(done);
711
712 __ epilogue();
713 }
714
generate_c1_load_reference_barrier_runtime_stub(StubAssembler * sasm,DecoratorSet decorators)715 void ShenandoahBarrierSetAssembler::generate_c1_load_reference_barrier_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
716 __ prologue("shenandoah_load_reference_barrier", false);
717 // arg0 : object to be resolved
718
719 __ push_call_clobbered_registers();
720 __ load_parameter(0, r0);
721 __ load_parameter(1, r1);
722
723 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
724 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
725 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
726 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
727 if (is_strong) {
728 if (is_native) {
729 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
730 } else {
731 if (UseCompressedOops) {
732 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow));
733 } else {
734 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
735 }
736 }
737 } else if (is_weak) {
738 assert(!is_native, "weak must not be called off-heap");
739 if (UseCompressedOops) {
740 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow));
741 } else {
742 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak));
743 }
744 } else {
745 assert(is_phantom, "only remaining strength");
746 assert(is_native, "phantom must only be called off-heap");
747 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom));
748 }
749 __ blr(lr);
750 __ mov(rscratch1, r0);
751 __ pop_call_clobbered_registers();
752 __ mov(r0, rscratch1);
753
754 __ epilogue();
755 }
756
757 #undef __
758
759 #endif // COMPILER1
760