1 /*
  2  * Copyright (c) 2018, 2023, 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 #include "precompiled.hpp"
 26 #include "asm/macroAssembler.inline.hpp"
 27 #include "gc/g1/g1BarrierSet.hpp"
 28 #include "gc/g1/g1BarrierSetAssembler.hpp"
 29 #include "gc/g1/g1BarrierSetRuntime.hpp"
 30 #include "gc/g1/g1CardTable.hpp"
 31 #include "gc/g1/g1HeapRegion.hpp"
 32 #include "gc/g1/g1ThreadLocalData.hpp"
 33 #include "interpreter/interp_masm.hpp"
 34 #include "runtime/sharedRuntime.hpp"
 35 #include "utilities/debug.hpp"
 36 #include "utilities/macros.hpp"
 37 #ifdef COMPILER1
 38 #include "c1/c1_LIRAssembler.hpp"
 39 #include "c1/c1_MacroAssembler.hpp"
 40 #include "gc/g1/c1/g1BarrierSetC1.hpp"
 41 #endif // COMPILER1
 42 #ifdef COMPILER2
 43 #include "gc/g1/c2/g1BarrierSetC2.hpp"
 44 #endif // COMPILER2
 45 
 46 #define __ masm->
 47 
 48 void G1BarrierSetAssembler::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators,
 49                                                             Register addr, Register count) {
 50   bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
 51 
 52   if (!dest_uninitialized) {
 53     Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
 54 #ifndef _LP64
 55     __ push(thread);
 56     __ get_thread(thread);
 57 #endif
 58 
 59     Label filtered;
 60     Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
 61     // Is marking active?
 62     if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 63       __ cmpl(in_progress, 0);
 64     } else {
 65       assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 66       __ cmpb(in_progress, 0);
 67     }
 68 
 69     NOT_LP64(__ pop(thread);)
 70 
 71     __ jcc(Assembler::equal, filtered);
 72 
 73     __ push_call_clobbered_registers(false /* save_fpu */);
 74 #ifdef _LP64
 75     if (count == c_rarg0) {
 76       if (addr == c_rarg1) {
 77         // exactly backwards!!
 78         __ xchgptr(c_rarg1, c_rarg0);
 79       } else {
 80         __ movptr(c_rarg1, count);
 81         __ movptr(c_rarg0, addr);
 82       }
 83     } else {
 84       __ movptr(c_rarg0, addr);
 85       __ movptr(c_rarg1, count);
 86     }
 87     if (UseCompressedOops) {
 88       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_narrow_oop_entry), 2);
 89     } else {
 90       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_oop_entry), 2);
 91     }
 92 #else
 93     __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_oop_entry),
 94                     addr, count);
 95 #endif
 96     __ pop_call_clobbered_registers(false /* save_fpu */);
 97 
 98     __ bind(filtered);
 99   }
100 }
101 
102 void G1BarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
103                                                              Register addr, Register count, Register tmp) {
104   __ push_call_clobbered_registers(false /* save_fpu */);
105 #ifdef _LP64
106   if (c_rarg0 == count) { // On win64 c_rarg0 == rcx
107     assert_different_registers(c_rarg1, addr);
108     __ mov(c_rarg1, count);
109     __ mov(c_rarg0, addr);
110   } else {
111     assert_different_registers(c_rarg0, count);
112     __ mov(c_rarg0, addr);
113     __ mov(c_rarg1, count);
114   }
115   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_post_entry), 2);
116 #else
117   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_post_entry),
118                   addr, count);
119 #endif
120   __ pop_call_clobbered_registers(false /* save_fpu */);
121 }
122 
123 void G1BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
124                                     Register dst, Address src, Register tmp1, Register tmp_thread) {
125   bool on_oop = is_reference_type(type);
126   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
127   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
128   bool on_reference = on_weak || on_phantom;
129   ModRefBarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
130   if (on_oop && on_reference) {
131     Register thread = NOT_LP64(tmp_thread) LP64_ONLY(r15_thread);
132 
133 #ifndef _LP64
134     // Work around the x86_32 bug that only manifests with Loom for some reason.
135     // MacroAssembler::resolve_weak_handle calls this barrier with tmp_thread == noreg.
136     if (thread == noreg) {
137       if (dst != rcx && tmp1 != rcx) {
138         thread = rcx;
139       } else if (dst != rdx && tmp1 != rdx) {
140         thread = rdx;
141       } else if (dst != rdi && tmp1 != rdi) {
142         thread = rdi;
143       }
144     }
145     assert_different_registers(dst, tmp1, thread);
146     __ push(thread);
147     __ get_thread(thread);
148 #endif
149 
150     // Generate the G1 pre-barrier code to log the value of
151     // the referent field in an SATB buffer.
152     g1_write_barrier_pre(masm /* masm */,
153                          noreg /* obj */,
154                          dst /* pre_val */,
155                          thread /* thread */,
156                          tmp1 /* tmp */,
157                          true /* tosca_live */,
158                          true /* expand_call */);
159 
160 #ifndef _LP64
161     __ pop(thread);
162 #endif
163   }
164 }
165 
166 static void generate_queue_insertion(MacroAssembler* masm, ByteSize index_offset, ByteSize buffer_offset, Label& runtime,
167                                      const Register thread, const Register value, const Register temp) {
168   // This code assumes that buffer index is pointer sized.
169   STATIC_ASSERT(in_bytes(SATBMarkQueue::byte_width_of_index()) == sizeof(intptr_t));
170   // Can we store a value in the given thread's buffer?
171   // (The index field is typed as size_t.)
172   __ movptr(temp, Address(thread, in_bytes(index_offset)));   // temp := *(index address)
173   __ testptr(temp, temp);                                     // index == 0?
174   __ jcc(Assembler::zero, runtime);                           // jump to runtime if index == 0 (full buffer)
175   // The buffer is not full, store value into it.
176   __ subptr(temp, wordSize);                                  // temp := next index
177   __ movptr(Address(thread, in_bytes(index_offset)), temp);   // *(index address) := next index
178   __ addptr(temp, Address(thread, in_bytes(buffer_offset)));  // temp := buffer address + next index
179   __ movptr(Address(temp, 0), value);                         // *(buffer address + next index) := value
180 }
181 
182 static void generate_pre_barrier_fast_path(MacroAssembler* masm,
183                                            const Register thread) {
184   Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
185   // Is marking active?
186   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
187     __ cmpl(in_progress, 0);
188   } else {
189     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
190     __ cmpb(in_progress, 0);
191   }
192 }
193 
194 static void generate_pre_barrier_slow_path(MacroAssembler* masm,
195                                            const Register obj,
196                                            const Register pre_val,
197                                            const Register thread,
198                                            const Register tmp,
199                                            Label& done,
200                                            Label& runtime) {
201   // Do we need to load the previous value?
202   if (obj != noreg) {
203     __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
204   }
205   // Is the previous value null?
206   __ cmpptr(pre_val, NULL_WORD);
207   __ jcc(Assembler::equal, done);
208   generate_queue_insertion(masm,
209                            G1ThreadLocalData::satb_mark_queue_index_offset(),
210                            G1ThreadLocalData::satb_mark_queue_buffer_offset(),
211                            runtime,
212                            thread, pre_val, tmp);
213   __ jmp(done);
214 }
215 
216 void G1BarrierSetAssembler::g1_write_barrier_pre(MacroAssembler* masm,
217                                                  Register obj,
218                                                  Register pre_val,
219                                                  Register thread,
220                                                  Register tmp,
221                                                  bool tosca_live,
222                                                  bool expand_call) {
223   // If expand_call is true then we expand the call_VM_leaf macro
224   // directly to skip generating the check by
225   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
226 
227 #ifdef _LP64
228   assert(thread == r15_thread, "must be");
229 #endif // _LP64
230 
231   Label done;
232   Label runtime;
233 
234   assert(pre_val != noreg, "check this code");
235 
236   if (obj != noreg) {
237     assert_different_registers(obj, pre_val, tmp);
238     assert(pre_val != rax, "check this code");
239   }
240 
241   generate_pre_barrier_fast_path(masm, thread);
242   // If marking is not active (*(mark queue active address) == 0), jump to done
243   __ jcc(Assembler::equal, done);
244   generate_pre_barrier_slow_path(masm, obj, pre_val, thread, tmp, done, runtime);
245 
246   __ bind(runtime);
247 
248   // Determine and save the live input values
249   __ push_call_clobbered_registers();
250 
251   // Calling the runtime using the regular call_VM_leaf mechanism generates
252   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
253   // that checks that the *(ebp+frame::interpreter_frame_last_sp) == nullptr.
254   //
255   // If we care generating the pre-barrier without a frame (e.g. in the
256   // intrinsified Reference.get() routine) then ebp might be pointing to
257   // the caller frame and so this check will most likely fail at runtime.
258   //
259   // Expanding the call directly bypasses the generation of the check.
260   // So when we do not have have a full interpreter frame on the stack
261   // expand_call should be passed true.
262 
263   if (expand_call) {
264     LP64_ONLY( assert(pre_val != c_rarg1, "smashed arg"); )
265 #ifdef _LP64
266     if (c_rarg1 != thread) {
267       __ mov(c_rarg1, thread);
268     }
269     if (c_rarg0 != pre_val) {
270       __ mov(c_rarg0, pre_val);
271     }
272 #else
273     __ push(thread);
274     __ push(pre_val);
275 #endif
276     __ MacroAssembler::call_VM_leaf_base(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), 2);
277   } else {
278     __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
279   }
280 
281   __ pop_call_clobbered_registers();
282 
283   __ bind(done);
284 }
285 
286 static void generate_post_barrier_fast_path(MacroAssembler* masm,
287                                             const Register store_addr,
288                                             const Register new_val,
289                                             const Register tmp,
290                                             const Register tmp2,
291                                             Label& done,
292                                             bool new_val_may_be_null) {
293   CardTableBarrierSet* ct = barrier_set_cast<CardTableBarrierSet>(BarrierSet::barrier_set());
294   // Does store cross heap regions?
295   __ movptr(tmp, store_addr);                                    // tmp := store address
296   __ xorptr(tmp, new_val);                                       // tmp := store address ^ new value
297   __ shrptr(tmp, G1HeapRegion::LogOfHRGrainBytes);               // ((store address ^ new value) >> LogOfHRGrainBytes) == 0?
298   __ jcc(Assembler::equal, done);
299   // Crosses regions, storing null?
300   if (new_val_may_be_null) {
301     __ cmpptr(new_val, NULL_WORD);                               // new value == null?
302     __ jcc(Assembler::equal, done);
303   }
304   // Storing region crossing non-null, is card young?
305   __ movptr(tmp, store_addr);                                    // tmp := store address
306   __ shrptr(tmp, CardTable::card_shift());                       // tmp := card address relative to card table base
307   // Do not use ExternalAddress to load 'byte_map_base', since 'byte_map_base' is NOT
308   // a valid address and therefore is not properly handled by the relocation code.
309   __ movptr(tmp2, (intptr_t)ct->card_table()->byte_map_base());  // tmp2 := card table base address
310   __ addptr(tmp, tmp2);                                          // tmp := card address
311   __ cmpb(Address(tmp, 0), G1CardTable::g1_young_card_val());    // *(card address) == young_card_val?
312 }
313 
314 static void generate_post_barrier_slow_path(MacroAssembler* masm,
315                                             const Register thread,
316                                             const Register tmp,
317                                             const Register tmp2,
318                                             Label& done,
319                                             Label& runtime) {
320   __ membar(Assembler::Membar_mask_bits(Assembler::StoreLoad));  // StoreLoad membar
321   __ cmpb(Address(tmp, 0), G1CardTable::dirty_card_val());       // *(card address) == dirty_card_val?
322   __ jcc(Assembler::equal, done);
323   // Storing a region crossing, non-null oop, card is clean.
324   // Dirty card and log.
325   __ movb(Address(tmp, 0), G1CardTable::dirty_card_val());       // *(card address) := dirty_card_val
326   generate_queue_insertion(masm,
327                            G1ThreadLocalData::dirty_card_queue_index_offset(),
328                            G1ThreadLocalData::dirty_card_queue_buffer_offset(),
329                            runtime,
330                            thread, tmp, tmp2);
331   __ jmp(done);
332 }
333 
334 void G1BarrierSetAssembler::g1_write_barrier_post(MacroAssembler* masm,
335                                                   Register store_addr,
336                                                   Register new_val,
337                                                   Register thread,
338                                                   Register tmp,
339                                                   Register tmp2) {
340 #ifdef _LP64
341   assert(thread == r15_thread, "must be");
342 #endif // _LP64
343 
344   Label done;
345   Label runtime;
346 
347   generate_post_barrier_fast_path(masm, store_addr, new_val, tmp, tmp2, done, true /* new_val_may_be_null */);
348   // If card is young, jump to done
349   __ jcc(Assembler::equal, done);
350   generate_post_barrier_slow_path(masm, thread, tmp, tmp2, done, runtime);
351 
352   __ bind(runtime);
353   // save the live input values
354   RegSet saved = RegSet::of(store_addr NOT_LP64(COMMA thread));
355   __ push_set(saved);
356   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), tmp, thread);
357   __ pop_set(saved);
358 
359   __ bind(done);
360 }
361 
362 #if defined(COMPILER2)
363 
364 static void generate_c2_barrier_runtime_call(MacroAssembler* masm, G1BarrierStubC2* stub, const Register arg, const address runtime_path) {
365 #ifdef _LP64
366   SaveLiveRegisters save_registers(masm, stub);
367   if (c_rarg0 != arg) {
368     __ mov(c_rarg0, arg);
369   }
370   __ mov(c_rarg1, r15_thread);
371   // rax is a caller-saved, non-argument-passing register, so it does not
372   // interfere with c_rarg0 or c_rarg1. If it contained any live value before
373   // entering this stub, it is saved at this point, and restored after the
374   // call. If it did not contain any live value, it is free to be used. In
375   // either case, it is safe to use it here as a call scratch register.
376   __ call(RuntimeAddress(runtime_path), rax);
377 #else
378   Unimplemented();
379 #endif // _LP64
380 }
381 
382 void G1BarrierSetAssembler::g1_write_barrier_pre_c2(MacroAssembler* masm,
383                                                     Register obj,
384                                                     Register pre_val,
385                                                     Register thread,
386                                                     Register tmp,
387                                                     G1PreBarrierStubC2* stub) {
388 #ifdef _LP64
389   assert(thread == r15_thread, "must be");
390 #endif // _LP64
391   assert(pre_val != noreg, "check this code");
392   if (obj != noreg) {
393     assert_different_registers(obj, pre_val, tmp);
394   }
395 
396   stub->initialize_registers(obj, pre_val, thread, tmp);
397 
398   generate_pre_barrier_fast_path(masm, thread);
399   // If marking is active (*(mark queue active address) != 0), jump to stub (slow path)
400   __ jcc(Assembler::notEqual, *stub->entry());
401 
402   __ bind(*stub->continuation());
403 }
404 
405 void G1BarrierSetAssembler::generate_c2_pre_barrier_stub(MacroAssembler* masm,
406                                                          G1PreBarrierStubC2* stub) const {
407   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
408   Label runtime;
409   Register obj = stub->obj();
410   Register pre_val = stub->pre_val();
411   Register thread = stub->thread();
412   Register tmp = stub->tmp1();
413   assert(stub->tmp2() == noreg, "not needed in this platform");
414 
415   __ bind(*stub->entry());
416   generate_pre_barrier_slow_path(masm, obj, pre_val, thread, tmp, *stub->continuation(), runtime);
417 
418   __ bind(runtime);
419   generate_c2_barrier_runtime_call(masm, stub, pre_val, CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry));
420   __ jmp(*stub->continuation());
421 }
422 
423 void G1BarrierSetAssembler::g1_write_barrier_post_c2(MacroAssembler* masm,
424                                                      Register store_addr,
425                                                      Register new_val,
426                                                      Register thread,
427                                                      Register tmp,
428                                                      Register tmp2,
429                                                      G1PostBarrierStubC2* stub) {
430 #ifdef _LP64
431   assert(thread == r15_thread, "must be");
432 #endif // _LP64
433 
434   stub->initialize_registers(thread, tmp, tmp2);
435 
436   bool new_val_may_be_null = (stub->barrier_data() & G1C2BarrierPostNotNull) == 0;
437   generate_post_barrier_fast_path(masm, store_addr, new_val, tmp, tmp2, *stub->continuation(), new_val_may_be_null);
438   // If card is not young, jump to stub (slow path)
439   __ jcc(Assembler::notEqual, *stub->entry());
440 
441   __ bind(*stub->continuation());
442 }
443 
444 void G1BarrierSetAssembler::generate_c2_post_barrier_stub(MacroAssembler* masm,
445                                                           G1PostBarrierStubC2* stub) const {
446   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
447   Label runtime;
448   Register thread = stub->thread();
449   Register tmp = stub->tmp1(); // tmp holds the card address.
450   Register tmp2 = stub->tmp2();
451   assert(stub->tmp3() == noreg, "not needed in this platform");
452 
453   __ bind(*stub->entry());
454   generate_post_barrier_slow_path(masm, thread, tmp, tmp2, *stub->continuation(), runtime);
455 
456   __ bind(runtime);
457   generate_c2_barrier_runtime_call(masm, stub, tmp, CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry));
458   __ jmp(*stub->continuation());
459 }
460 
461 #endif // COMPILER2
462 
463 void G1BarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
464                                          Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
465   bool in_heap = (decorators & IN_HEAP) != 0;
466   bool as_normal = (decorators & AS_NORMAL) != 0;
467 
468   bool needs_pre_barrier = as_normal;
469   bool needs_post_barrier = val != noreg && in_heap;
470 
471   Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
472   // flatten object address if needed
473   // We do it regardless of precise because we need the registers
474   if (dst.index() == noreg && dst.disp() == 0) {
475     if (dst.base() != tmp1) {
476       __ movptr(tmp1, dst.base());
477     }
478   } else {
479     __ lea(tmp1, dst);
480   }
481 
482 #ifndef _LP64
483   InterpreterMacroAssembler *imasm = static_cast<InterpreterMacroAssembler*>(masm);
484 #endif
485 
486   NOT_LP64(__ get_thread(rcx));
487   NOT_LP64(imasm->save_bcp());
488 
489   if (needs_pre_barrier) {
490     g1_write_barrier_pre(masm /*masm*/,
491                          tmp1 /* obj */,
492                          tmp2 /* pre_val */,
493                          rthread /* thread */,
494                          tmp3  /* tmp */,
495                          val != noreg /* tosca_live */,
496                          false /* expand_call */);
497   }
498   if (val == noreg) {
499     BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
500   } else {
501     Register new_val = val;
502     if (needs_post_barrier) {
503       // G1 barrier needs uncompressed oop for region cross check.
504       if (UseCompressedOops) {
505         new_val = tmp2;
506         __ movptr(new_val, val);
507       }
508     }
509     BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
510     if (needs_post_barrier) {
511       g1_write_barrier_post(masm /*masm*/,
512                             tmp1 /* store_adr */,
513                             new_val /* new_val */,
514                             rthread /* thread */,
515                             tmp3 /* tmp */,
516                             tmp2 /* tmp2 */);
517     }
518   }
519   NOT_LP64(imasm->restore_bcp());
520 }
521 
522 #ifdef COMPILER1
523 
524 #undef __
525 #define __ ce->masm()->
526 
527 void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) {
528   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
529   // At this point we know that marking is in progress.
530   // If do_load() is true then we have to emit the
531   // load of the previous value; otherwise it has already
532   // been loaded into _pre_val.
533 
534   __ bind(*stub->entry());
535   assert(stub->pre_val()->is_register(), "Precondition.");
536 
537   Register pre_val_reg = stub->pre_val()->as_register();
538 
539   if (stub->do_load()) {
540     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
541   }
542 
543   __ cmpptr(pre_val_reg, NULL_WORD);
544   __ jcc(Assembler::equal, *stub->continuation());
545   ce->store_parameter(stub->pre_val()->as_register(), 0);
546   __ call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
547   __ jmp(*stub->continuation());
548 
549 }
550 
551 void G1BarrierSetAssembler::gen_post_barrier_stub(LIR_Assembler* ce, G1PostBarrierStub* stub) {
552   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
553   __ bind(*stub->entry());
554   assert(stub->addr()->is_register(), "Precondition.");
555   assert(stub->new_val()->is_register(), "Precondition.");
556   Register new_val_reg = stub->new_val()->as_register();
557   __ cmpptr(new_val_reg, NULL_WORD);
558   __ jcc(Assembler::equal, *stub->continuation());
559   ce->store_parameter(stub->addr()->as_pointer_register(), 0);
560   __ call(RuntimeAddress(bs->post_barrier_c1_runtime_code_blob()->code_begin()));
561   __ jmp(*stub->continuation());
562 }
563 
564 #undef __
565 
566 #define __ sasm->
567 
568 void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
569   // Generated code assumes that buffer index is pointer sized.
570   STATIC_ASSERT(in_bytes(SATBMarkQueue::byte_width_of_index()) == sizeof(intptr_t));
571 
572   __ prologue("g1_pre_barrier", false);
573   // arg0 : previous value of memory
574 
575   __ push(rax);
576   __ push(rdx);
577 
578   const Register pre_val = rax;
579   const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
580   const Register tmp = rdx;
581 
582   NOT_LP64(__ get_thread(thread);)
583 
584   Address queue_active(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
585   Address queue_index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
586   Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
587 
588   Label done;
589   Label runtime;
590 
591   // Is marking still active?
592   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
593     __ cmpl(queue_active, 0);
594   } else {
595     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
596     __ cmpb(queue_active, 0);
597   }
598   __ jcc(Assembler::equal, done);
599 
600   // Can we store original value in the thread's buffer?
601 
602   __ movptr(tmp, queue_index);
603   __ testptr(tmp, tmp);
604   __ jcc(Assembler::zero, runtime);
605   __ subptr(tmp, wordSize);
606   __ movptr(queue_index, tmp);
607   __ addptr(tmp, buffer);
608 
609   // prev_val (rax)
610   __ load_parameter(0, pre_val);
611   __ movptr(Address(tmp, 0), pre_val);
612   __ jmp(done);
613 
614   __ bind(runtime);
615 
616   __ push_call_clobbered_registers();
617 
618   // load the pre-value
619   __ load_parameter(0, rcx);
620   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), rcx, thread);
621 
622   __ pop_call_clobbered_registers();
623 
624   __ bind(done);
625 
626   __ pop(rdx);
627   __ pop(rax);
628 
629   __ epilogue();
630 }
631 
632 void G1BarrierSetAssembler::generate_c1_post_barrier_runtime_stub(StubAssembler* sasm) {
633   __ prologue("g1_post_barrier", false);
634 
635   CardTableBarrierSet* ct =
636     barrier_set_cast<CardTableBarrierSet>(BarrierSet::barrier_set());
637 
638   Label done;
639   Label enqueued;
640   Label runtime;
641 
642   // At this point we know new_value is non-null and the new_value crosses regions.
643   // Must check to see if card is already dirty
644 
645   const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
646 
647   Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));
648   Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));
649 
650   __ push(rax);
651   __ push(rcx);
652 
653   const Register cardtable = rax;
654   const Register card_addr = rcx;
655 
656   __ load_parameter(0, card_addr);
657   __ shrptr(card_addr, CardTable::card_shift());
658   // Do not use ExternalAddress to load 'byte_map_base', since 'byte_map_base' is NOT
659   // a valid address and therefore is not properly handled by the relocation code.
660   __ movptr(cardtable, (intptr_t)ct->card_table()->byte_map_base());
661   __ addptr(card_addr, cardtable);
662 
663   NOT_LP64(__ get_thread(thread);)
664 
665   __ cmpb(Address(card_addr, 0), G1CardTable::g1_young_card_val());
666   __ jcc(Assembler::equal, done);
667 
668   __ membar(Assembler::Membar_mask_bits(Assembler::StoreLoad));
669   __ cmpb(Address(card_addr, 0), CardTable::dirty_card_val());
670   __ jcc(Assembler::equal, done);
671 
672   // storing region crossing non-null, card is clean.
673   // dirty card and log.
674 
675   __ movb(Address(card_addr, 0), CardTable::dirty_card_val());
676 
677   const Register tmp = rdx;
678   __ push(rdx);
679 
680   __ movptr(tmp, queue_index);
681   __ testptr(tmp, tmp);
682   __ jcc(Assembler::zero, runtime);
683   __ subptr(tmp, wordSize);
684   __ movptr(queue_index, tmp);
685   __ addptr(tmp, buffer);
686   __ movptr(Address(tmp, 0), card_addr);
687   __ jmp(enqueued);
688 
689   __ bind(runtime);
690   __ push_call_clobbered_registers();
691 
692   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread);
693 
694   __ pop_call_clobbered_registers();
695 
696   __ bind(enqueued);
697   __ pop(rdx);
698 
699   __ bind(done);
700   __ pop(rcx);
701   __ pop(rax);
702 
703   __ epilogue();
704 }
705 
706 #undef __
707 
708 #endif // COMPILER1