1 /*
  2  * Copyright (c) 2018, 2021, 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/g1ThreadLocalData.hpp"
 32 #include "gc/g1/heapRegion.hpp"
 33 #include "gc/shared/collectedHeap.hpp"
 34 #include "runtime/sharedRuntime.hpp"
 35 #include "runtime/thread.hpp"
 36 #include "interpreter/interp_masm.hpp"
 37 #include "runtime/sharedRuntime.hpp"
 38 #ifdef COMPILER1
 39 #include "c1/c1_LIRAssembler.hpp"
 40 #include "c1/c1_MacroAssembler.hpp"
 41 #include "gc/g1/c1/g1BarrierSetC1.hpp"
 42 #endif
 43 
 44 #define __ masm->
 45 
 46 void G1BarrierSetAssembler::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators,
 47                                                             Register addr, Register count, RegSet saved_regs) {
 48   bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
 49   if (!dest_uninitialized) {
 50     Label done;
 51     Address in_progress(rthread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
 52 
 53     // Is marking active?
 54     if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 55       __ ldrw(rscratch1, in_progress);
 56     } else {
 57       assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 58       __ ldrb(rscratch1, in_progress);
 59     }
 60     __ cbzw(rscratch1, done);
 61 
 62     __ push(saved_regs, sp);
 63     if (count == c_rarg0) {
 64       if (addr == c_rarg1) {
 65         // exactly backwards!!
 66         __ mov(rscratch1, c_rarg0);
 67         __ mov(c_rarg0, c_rarg1);
 68         __ mov(c_rarg1, rscratch1);
 69       } else {
 70         __ mov(c_rarg1, count);
 71         __ mov(c_rarg0, addr);
 72       }
 73     } else {
 74       __ mov(c_rarg0, addr);
 75       __ mov(c_rarg1, count);
 76     }
 77     if (UseCompressedOops) {
 78       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_narrow_oop_entry), 2);
 79     } else {
 80       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_oop_entry), 2);
 81     }
 82     __ pop(saved_regs, sp);
 83 
 84     __ bind(done);
 85   }
 86 }
 87 
 88 void G1BarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
 89                                                              Register start, Register count, Register scratch, RegSet saved_regs) {
 90   __ push(saved_regs, sp);
 91   assert_different_registers(start, count, scratch);
 92   assert_different_registers(c_rarg0, count);
 93   __ mov(c_rarg0, start);
 94   __ mov(c_rarg1, count);
 95   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_post_entry), 2);
 96   __ pop(saved_regs, sp);
 97 }
 98 
 99 void G1BarrierSetAssembler::g1_write_barrier_pre(MacroAssembler* masm,
100                                                  Register obj,
101                                                  Register pre_val,
102                                                  Register thread,
103                                                  Register tmp,
104                                                  bool tosca_live,
105                                                  bool expand_call) {
106   // If expand_call is true then we expand the call_VM_leaf macro
107   // directly to skip generating the check by
108   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
109 
110   assert(thread == rthread, "must be");
111 
112   Label done;
113   Label runtime;
114 
115   assert_different_registers(obj, pre_val, tmp, rscratch1);
116   assert(pre_val != noreg &&  tmp != noreg, "expecting a register");
117 
118   Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
119   Address index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
120   Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
121 
122   // Is marking active?
123   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
124     __ ldrw(tmp, in_progress);
125   } else {
126     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
127     __ ldrb(tmp, in_progress);
128   }
129   __ cbzw(tmp, done);
130 
131   // Do we need to load the previous value?
132   if (obj != noreg) {
133     __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
134   }
135 
136   // Is the previous value null?
137   __ cbz(pre_val, done);
138 
139   // Can we store original value in the thread's buffer?
140   // Is index == 0?
141   // (The index field is typed as size_t.)
142 
143   __ ldr(tmp, index);                      // tmp := *index_adr
144   __ cbz(tmp, runtime);                    // tmp == 0?
145                                         // If yes, goto runtime
146 
147   __ sub(tmp, tmp, wordSize);              // tmp := tmp - wordSize
148   __ str(tmp, index);                      // *index_adr := tmp
149   __ ldr(rscratch1, buffer);
150   __ add(tmp, tmp, rscratch1);             // tmp := tmp + *buffer_adr
151 
152   // Record the previous value
153   __ str(pre_val, Address(tmp, 0));
154   __ b(done);
155 
156   __ bind(runtime);
157 
158   // save the live input values
159   RegSet saved = RegSet::of(pre_val);
160   FloatRegSet fsaved;
161   if (tosca_live) saved += RegSet::of(r0);
162   if (obj != noreg) saved += RegSet::of(obj);
163 
164   // Barriers might be emitted when converting between (scalarized) calling
165   // conventions for inline types. Save all argument registers before calling
166   // into the runtime.
167   if (EnableValhalla && InlineTypePassFieldsAsArgs) {
168     saved += RegSet::of(j_rarg0, j_rarg1, j_rarg2, j_rarg3);
169     saved += RegSet::of(j_rarg4, j_rarg5, j_rarg6, j_rarg7);
170 
171     fsaved += FloatRegSet::of(j_farg0, j_farg1, j_farg2, j_farg3);
172     fsaved += FloatRegSet::of(j_farg4, j_farg5, j_farg6, j_farg7);
173   }
174 
175   __ push(saved, sp);
176   __ push_fp(fsaved, sp);
177 
178   // Calling the runtime using the regular call_VM_leaf mechanism generates
179   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
180   // that checks that the *(rfp+frame::interpreter_frame_last_sp) == NULL.
181   //
182   // If we care generating the pre-barrier without a frame (e.g. in the
183   // intrinsified Reference.get() routine) then ebp might be pointing to
184   // the caller frame and so this check will most likely fail at runtime.
185   //
186   // Expanding the call directly bypasses the generation of the check.
187   // So when we do not have have a full interpreter frame on the stack
188   // expand_call should be passed true.
189 
190   if (expand_call) {
191     assert(pre_val != c_rarg1, "smashed arg");
192     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
193   } else {
194     __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
195   }
196 
197   __ pop_fp(fsaved, sp);
198   __ pop(saved, sp);
199 
200   __ bind(done);
201 
202 }
203 
204 void G1BarrierSetAssembler::g1_write_barrier_post(MacroAssembler* masm,
205                                                   Register store_addr,
206                                                   Register new_val,
207                                                   Register thread,
208                                                   Register tmp,
209                                                   Register tmp2) {
210   assert(thread == rthread, "must be");
211   assert_different_registers(store_addr, new_val, thread, tmp, rscratch1);

212   assert(store_addr != noreg && new_val != noreg && tmp != noreg
213          && tmp2 != noreg, "expecting a register");
214 
215   Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));
216   Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));
217 
218   BarrierSet* bs = BarrierSet::barrier_set();
219   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
220   CardTable* ct = ctbs->card_table();
221 
222   Label done;
223   Label runtime;
224 
225   // Does store cross heap regions?
226 
227   __ eor(tmp, store_addr, new_val);
228   __ lsr(tmp, tmp, HeapRegion::LogOfHRGrainBytes);
229   __ cbz(tmp, done);
230 
231   // crosses regions, storing NULL?
232 
233   __ cbz(new_val, done);
234 
235   // storing region crossing non-NULL, is card already dirty?
236 
237   assert_different_registers(store_addr, thread, tmp, tmp2, rscratch1);
238 
239   const Register card_addr = tmp;
240 
241   __ lsr(card_addr, store_addr, CardTable::card_shift);
242 
243   // get the address of the card
244   __ load_byte_map_base(tmp2);
245   __ add(card_addr, card_addr, tmp2);
246   __ ldrb(tmp2, Address(card_addr));
247   __ cmpw(tmp2, (int)G1CardTable::g1_young_card_val());
248   __ br(Assembler::EQ, done);
249 
250   assert((int)CardTable::dirty_card_val() == 0, "must be 0");
251 
252   __ membar(Assembler::StoreLoad);
253 
254   __ ldrb(tmp2, Address(card_addr));
255   __ cbzw(tmp2, done);
256 
257   // storing a region crossing, non-NULL oop, card is clean.
258   // dirty card and log.
259 
260   __ strb(zr, Address(card_addr));
261 
262   __ ldr(rscratch1, queue_index);
263   __ cbz(rscratch1, runtime);
264   __ sub(rscratch1, rscratch1, wordSize);
265   __ str(rscratch1, queue_index);
266 
267   __ ldr(tmp2, buffer);
268   __ str(card_addr, Address(tmp2, rscratch1));
269   __ b(done);
270 
271   __ bind(runtime);
272 
273   // save the live input values
274   RegSet saved = RegSet::of(store_addr);
275   FloatRegSet fsaved;
276 
277   // Barriers might be emitted when converting between (scalarized) calling
278   // conventions for inline types. Save all argument registers before calling
279   // into the runtime.
280   if (EnableValhalla && InlineTypePassFieldsAsArgs) {
281     saved += RegSet::of(j_rarg0, j_rarg1, j_rarg2, j_rarg3);
282     saved += RegSet::of(j_rarg4, j_rarg5, j_rarg6, j_rarg7);
283 
284     fsaved += FloatRegSet::of(j_farg0, j_farg1, j_farg2, j_farg3);
285     fsaved += FloatRegSet::of(j_farg4, j_farg5, j_farg6, j_farg7);
286   }
287 
288   __ push(saved, sp);
289   __ push_fp(fsaved, sp);
290   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread);
291   __ pop_fp(fsaved, sp);
292   __ pop(saved, sp);
293 
294   __ bind(done);
295 }
296 
297 void G1BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
298                                     Register dst, Address src, Register tmp1, Register tmp_thread) {
299   bool on_oop = is_reference_type(type);
300   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
301   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
302   bool on_reference = on_weak || on_phantom;
303   ModRefBarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
304   if (on_oop && on_reference) {
305     // LR is live.  It must be saved around calls.
306     __ enter(); // barrier may call runtime
307     // Generate the G1 pre-barrier code to log the value of
308     // the referent field in an SATB buffer.
309     g1_write_barrier_pre(masm /* masm */,
310                          noreg /* obj */,
311                          dst /* pre_val */,
312                          rthread /* thread */,
313                          tmp1 /* tmp */,
314                          true /* tosca_live */,
315                          true /* expand_call */);
316     __ leave();
317   }
318 }
319 
320 void G1BarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
321                                          Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
322 
323   bool in_heap = (decorators & IN_HEAP) != 0;
324   bool as_normal = (decorators & AS_NORMAL) != 0;
325   bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
326 
327   bool needs_pre_barrier = as_normal && !dest_uninitialized;
328   bool needs_post_barrier = (val != noreg && in_heap);
329 
330   if (tmp3 == noreg) {
331     tmp3 = rscratch2;
332   }
333   assert_different_registers(val, tmp1, tmp2, tmp3);
334 
335   // flatten object address if needed
336   if (dst.index() == noreg && dst.offset() == 0) {
337     if (dst.base() != tmp1) {
338       __ mov(tmp1, dst.base());
339     }
340   } else {
341     __ lea(tmp1, dst);
342   }
343 
344   if (needs_pre_barrier) {
345     g1_write_barrier_pre(masm,
346                          tmp1 /* obj */,
347                          tmp2 /* pre_val */,
348                          rthread /* thread */,
349                          tmp3  /* tmp */,
350                          val != noreg /* tosca_live */,
351                          false /* expand_call */);
352   }
353 
354   if (val == noreg) {
355     BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), noreg, noreg, noreg, noreg);
356   } else {
357     // G1 barrier needs uncompressed oop for region cross check.
358     Register new_val = val;
359     if (needs_post_barrier) {
360       if (UseCompressedOops) {
361         new_val = tmp3;
362         __ mov(new_val, val);
363       }
364     }
365 
366     BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
367 
368     if (needs_post_barrier) {
369       g1_write_barrier_post(masm,
370                             tmp1 /* store_adr */,
371                             new_val /* new_val */,
372                             rthread /* thread */,
373                             tmp2 /* tmp */,
374                             tmp3 /* tmp2 */);
375     }







376   }
377 
378 }
379 
380 #ifdef COMPILER1
381 
382 #undef __
383 #define __ ce->masm()->
384 
385 void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) {
386   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
387   // At this point we know that marking is in progress.
388   // If do_load() is true then we have to emit the
389   // load of the previous value; otherwise it has already
390   // been loaded into _pre_val.
391 
392   __ bind(*stub->entry());
393 
394   assert(stub->pre_val()->is_register(), "Precondition.");
395 
396   Register pre_val_reg = stub->pre_val()->as_register();
397 
398   if (stub->do_load()) {
399     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
400   }
401   __ cbz(pre_val_reg, *stub->continuation());
402   ce->store_parameter(stub->pre_val()->as_register(), 0);
403   __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
404   __ b(*stub->continuation());
405 }
406 
407 void G1BarrierSetAssembler::gen_post_barrier_stub(LIR_Assembler* ce, G1PostBarrierStub* stub) {
408   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
409   __ bind(*stub->entry());
410   assert(stub->addr()->is_register(), "Precondition.");
411   assert(stub->new_val()->is_register(), "Precondition.");
412   Register new_val_reg = stub->new_val()->as_register();
413   __ cbz(new_val_reg, *stub->continuation());
414   ce->store_parameter(stub->addr()->as_pointer_register(), 0);
415   __ far_call(RuntimeAddress(bs->post_barrier_c1_runtime_code_blob()->code_begin()));
416   __ b(*stub->continuation());
417 }
418 
419 #undef __
420 
421 #define __ sasm->
422 
423 void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
424   __ prologue("g1_pre_barrier", false);
425 
426   // arg0 : previous value of memory
427 
428   BarrierSet* bs = BarrierSet::barrier_set();
429 
430   const Register pre_val = r0;
431   const Register thread = rthread;
432   const Register tmp = rscratch1;
433 
434   Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
435   Address queue_index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
436   Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
437 
438   Label done;
439   Label runtime;
440 
441   // Is marking still active?
442   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
443     __ ldrw(tmp, in_progress);
444   } else {
445     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
446     __ ldrb(tmp, in_progress);
447   }
448   __ cbzw(tmp, done);
449 
450   // Can we store original value in the thread's buffer?
451   __ ldr(tmp, queue_index);
452   __ cbz(tmp, runtime);
453 
454   __ sub(tmp, tmp, wordSize);
455   __ str(tmp, queue_index);
456   __ ldr(rscratch2, buffer);
457   __ add(tmp, tmp, rscratch2);
458   __ load_parameter(0, rscratch2);
459   __ str(rscratch2, Address(tmp, 0));
460   __ b(done);
461 
462   __ bind(runtime);
463   __ push_call_clobbered_registers();
464   __ load_parameter(0, pre_val);
465   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
466   __ pop_call_clobbered_registers();
467   __ bind(done);
468 
469   __ epilogue();
470 }
471 
472 void G1BarrierSetAssembler::generate_c1_post_barrier_runtime_stub(StubAssembler* sasm) {
473   __ prologue("g1_post_barrier", false);
474 
475   // arg0: store_address
476   Address store_addr(rfp, 2*BytesPerWord);
477 
478   BarrierSet* bs = BarrierSet::barrier_set();
479   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
480   CardTable* ct = ctbs->card_table();
481 
482   Label done;
483   Label runtime;
484 
485   // At this point we know new_value is non-NULL and the new_value crosses regions.
486   // Must check to see if card is already dirty
487 
488   const Register thread = rthread;
489 
490   Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));
491   Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));
492 
493   const Register card_offset = rscratch2;
494   // LR is free here, so we can use it to hold the byte_map_base.
495   const Register byte_map_base = lr;
496 
497   assert_different_registers(card_offset, byte_map_base, rscratch1);
498 
499   __ load_parameter(0, card_offset);
500   __ lsr(card_offset, card_offset, CardTable::card_shift);
501   __ load_byte_map_base(byte_map_base);
502   __ ldrb(rscratch1, Address(byte_map_base, card_offset));
503   __ cmpw(rscratch1, (int)G1CardTable::g1_young_card_val());
504   __ br(Assembler::EQ, done);
505 
506   assert((int)CardTable::dirty_card_val() == 0, "must be 0");
507 
508   __ membar(Assembler::StoreLoad);
509   __ ldrb(rscratch1, Address(byte_map_base, card_offset));
510   __ cbzw(rscratch1, done);
511 
512   // storing region crossing non-NULL, card is clean.
513   // dirty card and log.
514   __ strb(zr, Address(byte_map_base, card_offset));
515 
516   // Convert card offset into an address in card_addr
517   Register card_addr = card_offset;
518   __ add(card_addr, byte_map_base, card_addr);
519 
520   __ ldr(rscratch1, queue_index);
521   __ cbz(rscratch1, runtime);
522   __ sub(rscratch1, rscratch1, wordSize);
523   __ str(rscratch1, queue_index);
524 
525   // Reuse LR to hold buffer_addr
526   const Register buffer_addr = lr;
527 
528   __ ldr(buffer_addr, buffer);
529   __ str(card_addr, Address(buffer_addr, rscratch1));
530   __ b(done);
531 
532   __ bind(runtime);
533   __ push_call_clobbered_registers();
534   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread);
535   __ pop_call_clobbered_registers();
536   __ bind(done);
537   __ epilogue();
538 }
539 
540 #undef __
541 
542 #endif // COMPILER1
--- EOF ---