1 /*
  2  * Copyright (c) 2018, 2020, 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   // save the live input values
158   RegSet saved = RegSet::of(pre_val);

159   if (tosca_live) saved += RegSet::of(r0);
160   if (obj != noreg) saved += RegSet::of(obj);
161 











162   __ push(saved, sp);

163 
164   // Calling the runtime using the regular call_VM_leaf mechanism generates
165   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
166   // that checks that the *(rfp+frame::interpreter_frame_last_sp) == NULL.
167   //
168   // If we care generating the pre-barrier without a frame (e.g. in the
169   // intrinsified Reference.get() routine) then ebp might be pointing to
170   // the caller frame and so this check will most likely fail at runtime.
171   //
172   // Expanding the call directly bypasses the generation of the check.
173   // So when we do not have have a full interpreter frame on the stack
174   // expand_call should be passed true.
175 
176   if (expand_call) {
177     assert(pre_val != c_rarg1, "smashed arg");
178     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
179   } else {
180     __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
181   }
182 

183   __ pop(saved, sp);
184 
185   __ bind(done);
186 
187 }
188 
189 void G1BarrierSetAssembler::g1_write_barrier_post(MacroAssembler* masm,
190                                                   Register store_addr,
191                                                   Register new_val,
192                                                   Register thread,
193                                                   Register tmp,
194                                                   Register tmp2) {
195   assert(thread == rthread, "must be");
196   assert_different_registers(store_addr, new_val, thread, tmp, tmp2,
197                              rscratch1);
198   assert(store_addr != noreg && new_val != noreg && tmp != noreg
199          && tmp2 != noreg, "expecting a register");
200 
201   Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));
202   Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));
203 
204   BarrierSet* bs = BarrierSet::barrier_set();
205   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
206   CardTable* ct = ctbs->card_table();
207 
208   Label done;
209   Label runtime;
210 
211   // Does store cross heap regions?
212 
213   __ eor(tmp, store_addr, new_val);
214   __ lsr(tmp, tmp, HeapRegion::LogOfHRGrainBytes);
215   __ cbz(tmp, done);
216 
217   // crosses regions, storing NULL?
218 
219   __ cbz(new_val, done);
220 
221   // storing region crossing non-NULL, is card already dirty?
222 


223   const Register card_addr = tmp;
224 
225   __ lsr(card_addr, store_addr, CardTable::card_shift);
226 
227   // get the address of the card
228   __ load_byte_map_base(tmp2);
229   __ add(card_addr, card_addr, tmp2);
230   __ ldrb(tmp2, Address(card_addr));
231   __ cmpw(tmp2, (int)G1CardTable::g1_young_card_val());
232   __ br(Assembler::EQ, done);
233 
234   assert((int)CardTable::dirty_card_val() == 0, "must be 0");
235 
236   __ membar(Assembler::StoreLoad);
237 
238   __ ldrb(tmp2, Address(card_addr));
239   __ cbzw(tmp2, done);
240 
241   // storing a region crossing, non-NULL oop, card is clean.
242   // dirty card and log.
243 
244   __ strb(zr, Address(card_addr));
245 
246   __ ldr(rscratch1, queue_index);
247   __ cbz(rscratch1, runtime);
248   __ sub(rscratch1, rscratch1, wordSize);
249   __ str(rscratch1, queue_index);
250 
251   __ ldr(tmp2, buffer);
252   __ str(card_addr, Address(tmp2, rscratch1));
253   __ b(done);
254 
255   __ bind(runtime);

256   // save the live input values
257   RegSet saved = RegSet::of(store_addr);













258   __ push(saved, sp);

259   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread);

260   __ pop(saved, sp);
261 
262   __ bind(done);
263 }
264 
265 void G1BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
266                                     Register dst, Address src, Register tmp1, Register tmp_thread) {
267   bool on_oop = is_reference_type(type);
268   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
269   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
270   bool on_reference = on_weak || on_phantom;
271   ModRefBarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
272   if (on_oop && on_reference) {
273     // LR is live.  It must be saved around calls.
274     __ enter(); // barrier may call runtime
275     // Generate the G1 pre-barrier code to log the value of
276     // the referent field in an SATB buffer.
277     g1_write_barrier_pre(masm /* masm */,
278                          noreg /* obj */,
279                          dst /* pre_val */,
280                          rthread /* thread */,
281                          tmp1 /* tmp */,
282                          true /* tosca_live */,
283                          true /* expand_call */);
284     __ leave();
285   }
286 }
287 
288 void G1BarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
289                                          Address dst, Register val, Register tmp1, Register tmp2) {













290   // flatten object address if needed
291   if (dst.index() == noreg && dst.offset() == 0) {
292     if (dst.base() != r3) {
293       __ mov(r3, dst.base());
294     }
295   } else {
296     __ lea(r3, dst);
297   }
298 
299   g1_write_barrier_pre(masm,
300                        r3 /* obj */,
301                        tmp2 /* pre_val */,
302                        rthread /* thread */,
303                        tmp1  /* tmp */,
304                        val != noreg /* tosca_live */,
305                        false /* expand_call */);


306 
307   if (val == noreg) {
308     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), noreg, noreg, noreg);
309   } else {
310     // G1 barrier needs uncompressed oop for region cross check.
311     Register new_val = val;
312     if (UseCompressedOops) {
313       new_val = rscratch2;
314       __ mov(new_val, val);













315     }
316     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), val, noreg, noreg);
317     g1_write_barrier_post(masm,
318                           r3 /* store_adr */,
319                           new_val /* new_val */,
320                           rthread /* thread */,
321                           tmp1 /* tmp */,
322                           tmp2 /* tmp2 */);
323   }
324 
325 }
326 
327 #ifdef COMPILER1
328 
329 #undef __
330 #define __ ce->masm()->
331 
332 void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) {
333   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
334   // At this point we know that marking is in progress.
335   // If do_load() is true then we have to emit the
336   // load of the previous value; otherwise it has already
337   // been loaded into _pre_val.
338 
339   __ bind(*stub->entry());
340 
341   assert(stub->pre_val()->is_register(), "Precondition.");
342 
343   Register pre_val_reg = stub->pre_val()->as_register();
344 
345   if (stub->do_load()) {
346     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
347   }
348   __ cbz(pre_val_reg, *stub->continuation());
349   ce->store_parameter(stub->pre_val()->as_register(), 0);
350   __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
351   __ b(*stub->continuation());
352 }
353 
354 void G1BarrierSetAssembler::gen_post_barrier_stub(LIR_Assembler* ce, G1PostBarrierStub* stub) {
355   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
356   __ bind(*stub->entry());
357   assert(stub->addr()->is_register(), "Precondition.");
358   assert(stub->new_val()->is_register(), "Precondition.");
359   Register new_val_reg = stub->new_val()->as_register();
360   __ cbz(new_val_reg, *stub->continuation());
361   ce->store_parameter(stub->addr()->as_pointer_register(), 0);
362   __ far_call(RuntimeAddress(bs->post_barrier_c1_runtime_code_blob()->code_begin()));
363   __ b(*stub->continuation());
364 }
365 
366 #undef __
367 
368 #define __ sasm->
369 
370 void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
371   __ prologue("g1_pre_barrier", false);
372 
373   // arg0 : previous value of memory
374 
375   BarrierSet* bs = BarrierSet::barrier_set();
376 
377   const Register pre_val = r0;
378   const Register thread = rthread;
379   const Register tmp = rscratch1;
380 
381   Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
382   Address queue_index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
383   Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
384 
385   Label done;
386   Label runtime;
387 
388   // Is marking still active?
389   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
390     __ ldrw(tmp, in_progress);
391   } else {
392     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
393     __ ldrb(tmp, in_progress);
394   }
395   __ cbzw(tmp, done);
396 
397   // Can we store original value in the thread's buffer?
398   __ ldr(tmp, queue_index);
399   __ cbz(tmp, runtime);
400 
401   __ sub(tmp, tmp, wordSize);
402   __ str(tmp, queue_index);
403   __ ldr(rscratch2, buffer);
404   __ add(tmp, tmp, rscratch2);
405   __ load_parameter(0, rscratch2);
406   __ str(rscratch2, Address(tmp, 0));
407   __ b(done);
408 
409   __ bind(runtime);
410   __ push_call_clobbered_registers();
411   __ load_parameter(0, pre_val);
412   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
413   __ pop_call_clobbered_registers();
414   __ bind(done);
415 
416   __ epilogue();
417 }
418 
419 void G1BarrierSetAssembler::generate_c1_post_barrier_runtime_stub(StubAssembler* sasm) {
420   __ prologue("g1_post_barrier", false);
421 
422   // arg0: store_address
423   Address store_addr(rfp, 2*BytesPerWord);
424 
425   BarrierSet* bs = BarrierSet::barrier_set();
426   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
427   CardTable* ct = ctbs->card_table();
428 
429   Label done;
430   Label runtime;
431 
432   // At this point we know new_value is non-NULL and the new_value crosses regions.
433   // Must check to see if card is already dirty
434 
435   const Register thread = rthread;
436 
437   Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));
438   Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));
439 
440   const Register card_offset = rscratch2;
441   // LR is free here, so we can use it to hold the byte_map_base.
442   const Register byte_map_base = lr;
443 
444   assert_different_registers(card_offset, byte_map_base, rscratch1);
445 
446   __ load_parameter(0, card_offset);
447   __ lsr(card_offset, card_offset, CardTable::card_shift);
448   __ load_byte_map_base(byte_map_base);
449   __ ldrb(rscratch1, Address(byte_map_base, card_offset));
450   __ cmpw(rscratch1, (int)G1CardTable::g1_young_card_val());
451   __ br(Assembler::EQ, done);
452 
453   assert((int)CardTable::dirty_card_val() == 0, "must be 0");
454 
455   __ membar(Assembler::StoreLoad);
456   __ ldrb(rscratch1, Address(byte_map_base, card_offset));
457   __ cbzw(rscratch1, done);
458 
459   // storing region crossing non-NULL, card is clean.
460   // dirty card and log.
461   __ strb(zr, Address(byte_map_base, card_offset));
462 
463   // Convert card offset into an address in card_addr
464   Register card_addr = card_offset;
465   __ add(card_addr, byte_map_base, card_addr);
466 
467   __ ldr(rscratch1, queue_index);
468   __ cbz(rscratch1, runtime);
469   __ sub(rscratch1, rscratch1, wordSize);
470   __ str(rscratch1, queue_index);
471 
472   // Reuse LR to hold buffer_addr
473   const Register buffer_addr = lr;
474 
475   __ ldr(buffer_addr, buffer);
476   __ str(card_addr, Address(buffer_addr, rscratch1));
477   __ b(done);
478 
479   __ bind(runtime);
480   __ push_call_clobbered_registers();
481   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread);
482   __ pop_call_clobbered_registers();
483   __ bind(done);
484   __ epilogue();
485 }
486 
487 #undef __
488 
489 #endif // COMPILER1
--- EOF ---