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 "gc/g1/g1BarrierSet.hpp"
 27 #include "gc/g1/g1BarrierSetAssembler.hpp"
 28 #include "gc/g1/g1BarrierSetRuntime.hpp"
 29 #include "gc/g1/g1CardTable.hpp"
 30 #include "gc/g1/g1HeapRegion.hpp"
 31 #include "gc/g1/g1ThreadLocalData.hpp"
 32 #include "interpreter/interp_masm.hpp"
 33 #include "runtime/sharedRuntime.hpp"
 34 #include "utilities/debug.hpp"
 35 #include "utilities/macros.hpp"
 36 #ifdef COMPILER1
 37 #include "c1/c1_LIRAssembler.hpp"
 38 #include "c1/c1_MacroAssembler.hpp"
 39 #include "gc/g1/c1/g1BarrierSetC1.hpp"
 40 #endif // COMPILER1
 41 #ifdef COMPILER2
 42 #include "gc/g1/c2/g1BarrierSetC2.hpp"
 43 #endif // COMPILER2
 44 
 45 #define __ masm->
 46 
 47 void G1BarrierSetAssembler::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators,
 48                                                             Register addr, Register count) {
 49   bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
 50 
 51   if (!dest_uninitialized) {
 52     Register thread = r15_thread;
 53 
 54     Label filtered;
 55     Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
 56     // Is marking active?
 57     if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 58       __ cmpl(in_progress, 0);
 59     } else {
 60       assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 61       __ cmpb(in_progress, 0);
 62     }
 63 
 64     __ jcc(Assembler::equal, filtered);
 65 
 66     __ push_call_clobbered_registers(false /* save_fpu */);
 67     if (count == c_rarg0) {
 68       if (addr == c_rarg1) {
 69         // exactly backwards!!
 70         __ xchgptr(c_rarg1, c_rarg0);
 71       } else {
 72         __ movptr(c_rarg1, count);
 73         __ movptr(c_rarg0, addr);
 74       }
 75     } else {
 76       __ movptr(c_rarg0, addr);
 77       __ movptr(c_rarg1, count);
 78     }
 79     if (UseCompressedOops) {
 80       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_narrow_oop_entry), 2);
 81     } else {
 82       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_oop_entry), 2);
 83     }
 84     __ pop_call_clobbered_registers(false /* save_fpu */);
 85 
 86     __ bind(filtered);
 87   }
 88 }
 89 
 90 void G1BarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
 91                                                              Register addr, Register count, Register tmp) {
 92   Label L_done;
 93 
 94   __ testptr(count, count);
 95   __ jccb(Assembler::zero, L_done);
 96 
 97   // Calculate end address in "count".
 98   Address::ScaleFactor scale = UseCompressedOops ? Address::times_4 : Address::times_8;
 99   __ leaq(count, Address(addr, count, scale));
100 
101   // Calculate start card address in "addr".
102   __ shrptr(addr, CardTable::card_shift());
103 
104   Register thread = r15_thread;
105 
106   __ movptr(tmp, Address(thread, in_bytes(G1ThreadLocalData::card_table_base_offset())));
107   __ addptr(addr, tmp);
108 
109   // Calculate address of card of last word in the array.
110   __ subptr(count, 1);
111   __ shrptr(count, CardTable::card_shift());
112   __ addptr(count, tmp);
113 
114   Label L_loop;
115   // Iterate from start card to end card (inclusive).
116   __ bind(L_loop);
117 
118   Label L_is_clean_card;
119   if (UseCondCardMark) {
120     __ cmpb(Address(addr, 0), G1CardTable::clean_card_val());
121     __ jccb(Assembler::equal, L_is_clean_card);
122   } else {
123    __ movb(Address(addr, 0), G1CardTable::dirty_card_val());
124   }
125 
126   Label L_next_card;
127   __ bind(L_next_card);
128   __ addptr(addr, sizeof(CardTable::CardValue));
129   __ cmpptr(addr, count);
130   __ jccb(Assembler::belowEqual, L_loop);
131   __ jmpb(L_done);
132 
133   __ bind(L_is_clean_card);
134   // Card was clean. Dirty card and go to next.
135   __ movb(Address(addr, 0), G1CardTable::dirty_card_val());
136   __ jmpb(L_next_card);
137 
138   __ bind(L_done);
139 }
140 
141 void G1BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
142                                     Register dst, Address src, Register tmp1) {
143   bool on_oop = is_reference_type(type);
144   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
145   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
146   bool on_reference = on_weak || on_phantom;
147   CardTableBarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
148   if (on_oop && on_reference) {
149     // Generate the G1 pre-barrier code to log the value of
150     // the referent field in an SATB buffer.
151     g1_write_barrier_pre(masm /* masm */,
152                          noreg /* obj */,
153                          dst /* pre_val */,
154                          tmp1 /* tmp */,
155                          true /* tosca_live */,
156                          true /* expand_call */);
157   }
158 }
159 
160 static void generate_pre_barrier_fast_path(MacroAssembler* masm,
161                                            const Register thread) {
162   Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
163   // Is marking active?
164   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
165     __ cmpl(in_progress, 0);
166   } else {
167     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
168     __ cmpb(in_progress, 0);
169   }
170 }
171 
172 static void generate_pre_barrier_slow_path(MacroAssembler* masm,
173                                            const Register obj,
174                                            const Register pre_val,
175                                            const Register thread,
176                                            const Register tmp,
177                                            Label& L_done) {
178   Address index_addr(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
179   Address buffer_addr(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
180 
181   // This code assumes that buffer index is pointer sized.
182   STATIC_ASSERT(in_bytes(SATBMarkQueue::byte_width_of_index()) == sizeof(intptr_t));
183 
184   Label L_runtime;
185 
186   // Do we need to load the previous value?
187   if (obj != noreg) {
188     __ load_heap_oop(pre_val, Address(obj, 0), noreg, AS_RAW);
189   }
190 
191   // Is the previous value null?
192   __ testptr(pre_val, pre_val);
193   __ jcc(Assembler::equal, L_done);
194 
195   // Can we store a value in the given thread's buffer?
196   // (The index field is typed as size_t.)
197   __ movptr(tmp, index_addr);             // temp := *(index address)
198   __ testptr(tmp, tmp);                   // index == 0?
199   __ jccb(Assembler::zero, L_runtime);    // jump to runtime if index == 0 (full buffer)
200 
201   // The buffer is not full, store value into it.
202   __ subptr(tmp, wordSize);               // temp := next index
203   __ movptr(index_addr, tmp);             // *(index address) := next index
204   __ addptr(tmp, buffer_addr);            // temp := buffer address + next index
205   __ movptr(Address(tmp, 0), pre_val);    // *(buffer address + next index) := value
206 
207   // Jump out if done, or fall-through to runtime.
208   // "L_done" is far away, so jump cannot be short.
209   __ jmp(L_done);
210   __ bind(L_runtime);
211 }
212 
213 void G1BarrierSetAssembler::g1_write_barrier_pre(MacroAssembler* masm,
214                                                  Register obj,
215                                                  Register pre_val,
216                                                  Register tmp,
217                                                  bool tosca_live,
218                                                  bool expand_call) {
219   // If expand_call is true then we expand the call_VM_leaf macro
220   // directly to skip generating the check by
221   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
222   const Register thread = r15_thread;
223 
224   Label done;
225 
226   assert(pre_val != noreg, "check this code");
227 
228   if (obj != noreg) {
229     assert_different_registers(obj, pre_val, tmp);
230     assert(pre_val != rax, "check this code");
231   }
232 
233   generate_pre_barrier_fast_path(masm, thread);
234   // If marking is not active (*(mark queue active address) == 0), jump to done
235   __ jcc(Assembler::equal, done);
236   generate_pre_barrier_slow_path(masm, obj, pre_val, thread, tmp, done);
237 
238   // Determine and save the live input values
239   __ push_call_clobbered_registers();
240 
241   // Calling the runtime using the regular call_VM_leaf mechanism generates
242   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
243   // that checks that the *(ebp+frame::interpreter_frame_last_sp) == nullptr.
244   //
245   // If we care generating the pre-barrier without a frame (e.g. in the
246   // intrinsified Reference.get() routine) then ebp might be pointing to
247   // the caller frame and so this check will most likely fail at runtime.
248   //
249   // Expanding the call directly bypasses the generation of the check.
250   // So when we do not have have a full interpreter frame on the stack
251   // expand_call should be passed true.
252 
253   if (expand_call) {
254     assert(pre_val != c_rarg1, "smashed arg");
255     if (c_rarg1 != thread) {
256       __ mov(c_rarg1, thread);
257     }
258     if (c_rarg0 != pre_val) {
259       __ mov(c_rarg0, pre_val);
260     }
261     __ MacroAssembler::call_VM_leaf_base(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), 2);
262   } else {
263     __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
264   }
265 
266   __ pop_call_clobbered_registers();
267 
268   __ bind(done);
269 }
270 
271 static void generate_post_barrier(MacroAssembler* masm,
272                                   const Register store_addr,
273                                   const Register new_val,
274                                   const Register tmp1,
275                                   bool new_val_may_be_null) {
276 
277   assert_different_registers(store_addr, new_val, tmp1, noreg);
278 
279   Register thread = r15_thread;
280 
281   Label L_done;
282   // Does store cross heap regions?
283   __ movptr(tmp1, store_addr);                                    // tmp1 := store address
284   __ xorptr(tmp1, new_val);                                       // tmp1 := store address ^ new value
285   __ shrptr(tmp1, G1HeapRegion::LogOfHRGrainBytes);               // ((store address ^ new value) >> LogOfHRGrainBytes) == 0?
286   __ jccb(Assembler::equal, L_done);
287 
288   // Crosses regions, storing null?
289   if (new_val_may_be_null) {
290     __ testptr(new_val, new_val);                                 // new value == null?
291     __ jccb(Assembler::equal, L_done);
292   }
293 
294   __ movptr(tmp1, store_addr);                                    // tmp1 := store address
295   __ shrptr(tmp1, CardTable::card_shift());                       // tmp1 := card address relative to card table base
296 
297   Address card_table_addr(thread, in_bytes(G1ThreadLocalData::card_table_base_offset()));
298   __ addptr(tmp1, card_table_addr);                               // tmp1 := card address
299   if (UseCondCardMark) {
300     __ cmpb(Address(tmp1, 0), G1CardTable::clean_card_val());     // *(card address) == clean_card_val?
301     __ jccb(Assembler::notEqual, L_done);
302   }
303   // Storing a region crossing, non-null oop, card is clean.
304   // Dirty card.
305   __ movb(Address(tmp1, 0), G1CardTable::dirty_card_val());       // *(card address) := dirty_card_val
306   __ bind(L_done);
307 }
308 
309 void G1BarrierSetAssembler::g1_write_barrier_post(MacroAssembler* masm,
310                                                   Register store_addr,
311                                                   Register new_val,
312                                                   Register tmp) {
313   generate_post_barrier(masm, store_addr, new_val, tmp, true /* new_val_may_be_null */);
314 }
315 
316 #if defined(COMPILER2)
317 
318 static void generate_c2_barrier_runtime_call(MacroAssembler* masm, G1BarrierStubC2* stub, const Register arg, const address runtime_path) {
319   SaveLiveRegisters save_registers(masm, stub);
320   if (c_rarg0 != arg) {
321     __ mov(c_rarg0, arg);
322   }
323   __ mov(c_rarg1, r15_thread);
324   // rax is a caller-saved, non-argument-passing register, so it does not
325   // interfere with c_rarg0 or c_rarg1. If it contained any live value before
326   // entering this stub, it is saved at this point, and restored after the
327   // call. If it did not contain any live value, it is free to be used. In
328   // either case, it is safe to use it here as a call scratch register.
329   __ call(RuntimeAddress(runtime_path), rax);
330 }
331 
332 void G1BarrierSetAssembler::g1_write_barrier_pre_c2(MacroAssembler* masm,
333                                                     Register obj,
334                                                     Register pre_val,
335                                                     Register tmp,
336                                                     G1PreBarrierStubC2* stub) {
337   const Register thread = r15_thread;
338 
339   assert(pre_val != noreg, "check this code");
340   if (obj != noreg) {
341     assert_different_registers(obj, pre_val, tmp);
342   }
343 
344   stub->initialize_registers(obj, pre_val, thread, tmp);
345 
346   generate_pre_barrier_fast_path(masm, thread);
347   // If marking is active (*(mark queue active address) != 0), jump to stub (slow path)
348   __ jcc(Assembler::notEqual, *stub->entry());
349 
350   __ bind(*stub->continuation());
351 }
352 
353 void G1BarrierSetAssembler::generate_c2_pre_barrier_stub(MacroAssembler* masm,
354                                                          G1PreBarrierStubC2* stub) const {
355   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
356   Register obj = stub->obj();
357   Register pre_val = stub->pre_val();
358   Register thread = stub->thread();
359   Register tmp = stub->tmp1();
360   assert(stub->tmp2() == noreg, "not needed in this platform");
361 
362   __ bind(*stub->entry());
363   generate_pre_barrier_slow_path(masm, obj, pre_val, thread, tmp, *stub->continuation());
364 
365   generate_c2_barrier_runtime_call(masm, stub, pre_val, CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry));
366   __ jmp(*stub->continuation());
367 }
368 
369 void G1BarrierSetAssembler::g1_write_barrier_post_c2(MacroAssembler* masm,
370                                                      Register store_addr,
371                                                      Register new_val,
372                                                      Register tmp,
373                                                      bool new_val_may_be_null) {
374   generate_post_barrier(masm, store_addr, new_val, tmp, new_val_may_be_null);
375 }
376 
377 #endif // COMPILER2
378 
379 void G1BarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
380                                          Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
381   bool in_heap = (decorators & IN_HEAP) != 0;
382   bool as_normal = (decorators & AS_NORMAL) != 0;
383 
384   bool needs_pre_barrier = as_normal;
385   bool needs_post_barrier = val != noreg && in_heap;
386 
387   // flatten object address if needed
388   // We do it regardless of precise because we need the registers
389   if (dst.index() == noreg && dst.disp() == 0) {
390     if (dst.base() != tmp1) {
391       __ movptr(tmp1, dst.base());
392     }
393   } else {
394     __ lea(tmp1, dst);
395   }
396 
397   if (needs_pre_barrier) {
398     g1_write_barrier_pre(masm /*masm*/,
399                          tmp1 /* obj */,
400                          tmp2 /* pre_val */,
401                          tmp3  /* tmp */,
402                          val != noreg /* tosca_live */,
403                          false /* expand_call */);
404   }
405   if (val == noreg) {
406     BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
407   } else {
408     Register new_val = val;
409     if (needs_post_barrier) {
410       // G1 barrier needs uncompressed oop for region cross check.
411       if (UseCompressedOops) {
412         new_val = tmp2;
413         __ movptr(new_val, val);
414       }
415     }
416     BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
417     if (needs_post_barrier) {
418       g1_write_barrier_post(masm /*masm*/,
419                             tmp1 /* store_adr */,
420                             new_val /* new_val */,
421                             tmp3 /* tmp */);
422     }
423   }
424 }
425 
426 #ifdef COMPILER1
427 
428 #undef __
429 #define __ ce->masm()->
430 
431 void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) {
432   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
433   // At this point we know that marking is in progress.
434   // If do_load() is true then we have to emit the
435   // load of the previous value; otherwise it has already
436   // been loaded into _pre_val.
437 
438   __ bind(*stub->entry());
439   assert(stub->pre_val()->is_register(), "Precondition.");
440 
441   Register pre_val_reg = stub->pre_val()->as_register();
442 
443   if (stub->do_load()) {
444     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
445   }
446 
447   __ testptr(pre_val_reg, pre_val_reg);
448   __ jcc(Assembler::equal, *stub->continuation());
449   ce->store_parameter(stub->pre_val()->as_register(), 0);
450   __ call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
451   __ jmp(*stub->continuation());
452 
453 }
454 
455 #undef __
456 
457 void G1BarrierSetAssembler::g1_write_barrier_post_c1(MacroAssembler* masm,
458                                                      Register store_addr,
459                                                      Register new_val,
460                                                      Register thread,
461                                                      Register tmp1,
462                                                      Register tmp2 /* unused on x86 */) {
463   generate_post_barrier(masm, store_addr, new_val, tmp1, true /* new_val_may_be_null */);
464 }
465 
466 #define __ sasm->
467 
468 void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
469   // Generated code assumes that buffer index is pointer sized.
470   STATIC_ASSERT(in_bytes(SATBMarkQueue::byte_width_of_index()) == sizeof(intptr_t));
471 
472   __ prologue("g1_pre_barrier", false);
473   // arg0 : previous value of memory
474 
475   __ push_ppx(rax);
476   __ push_ppx(rdx);
477 
478   const Register pre_val = rax;
479   const Register thread = r15_thread;
480   const Register tmp = rdx;
481 
482   Address queue_active(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
483   Address queue_index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
484   Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
485 
486   Label L_done, L_runtime;
487 
488   // Is marking still active?
489   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
490     __ cmpl(queue_active, 0);
491   } else {
492     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
493     __ cmpb(queue_active, 0);
494   }
495   __ jcc(Assembler::equal, L_done);
496 
497   // Can we store original value in the thread's buffer?
498 
499   __ movptr(tmp, queue_index);
500   __ testptr(tmp, tmp);
501   __ jccb(Assembler::zero, L_runtime);
502   __ subptr(tmp, wordSize);
503   __ movptr(queue_index, tmp);
504   __ addptr(tmp, buffer);
505 
506   // prev_val (rax)
507   __ load_parameter(0, pre_val);
508   __ movptr(Address(tmp, 0), pre_val);
509   __ jmp(L_done);
510 
511   __ bind(L_runtime);
512 
513   __ push_call_clobbered_registers();
514 
515   // load the pre-value
516   __ load_parameter(0, rcx);
517   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), rcx, thread);
518 
519   __ pop_call_clobbered_registers();
520 
521   __ bind(L_done);
522 
523   __ pop_ppx(rdx);
524   __ pop_ppx(rax);
525 
526   __ epilogue();
527 }
528 
529 #undef __
530 
531 #endif // COMPILER1