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