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