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 
158   __ push_call_clobbered_registers();
159 
160   // Calling the runtime using the regular call_VM_leaf mechanism generates
161   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
162   // that checks that the *(rfp+frame::interpreter_frame_last_sp) == NULL.
163   //
164   // If we care generating the pre-barrier without a frame (e.g. in the
165   // intrinsified Reference.get() routine) then ebp might be pointing to
166   // the caller frame and so this check will most likely fail at runtime.
167   //
168   // Expanding the call directly bypasses the generation of the check.
169   // So when we do not have have a full interpreter frame on the stack
170   // expand_call should be passed true.
171 
172   if (expand_call) {
173     assert(pre_val != c_rarg1, "smashed arg");
174     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
175   } else {
176     __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
177   }
178 
179   __ pop_call_clobbered_registers();
180 
181   __ bind(done);
182 
183 }
184 
185 void G1BarrierSetAssembler::g1_write_barrier_post(MacroAssembler* masm,
186                                                   Register store_addr,
187                                                   Register new_val,
188                                                   Register thread,
189                                                   Register tmp,
190                                                   Register tmp2) {
191   assert(thread == rthread, "must be");
192   assert_different_registers(store_addr, new_val, thread, tmp, tmp2,
193                              rscratch1);
194   assert(store_addr != noreg && new_val != noreg && tmp != noreg
195          && tmp2 != noreg, "expecting a register");
196 
197   Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));
198   Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));
199 
200   BarrierSet* bs = BarrierSet::barrier_set();
201   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
202   CardTable* ct = ctbs->card_table();
203 
204   Label done;
205   Label runtime;
206 
207   // Does store cross heap regions?
208 
209   __ eor(tmp, store_addr, new_val);
210   __ lsr(tmp, tmp, HeapRegion::LogOfHRGrainBytes);
211   __ cbz(tmp, done);
212 
213   // crosses regions, storing NULL?
214 
215   __ cbz(new_val, done);
216 
217   // storing region crossing non-NULL, is card already dirty?
218 
219   const Register card_addr = tmp;
220 
221   __ lsr(card_addr, store_addr, CardTable::card_shift);
222 
223   // get the address of the card
224   __ load_byte_map_base(tmp2);
225   __ add(card_addr, card_addr, tmp2);
226   __ ldrb(tmp2, Address(card_addr));
227   __ cmpw(tmp2, (int)G1CardTable::g1_young_card_val());
228   __ br(Assembler::EQ, done);
229 
230   assert((int)CardTable::dirty_card_val() == 0, "must be 0");
231 
232   __ membar(Assembler::StoreLoad);
233 
234   __ ldrb(tmp2, Address(card_addr));
235   __ cbzw(tmp2, done);
236 
237   // storing a region crossing, non-NULL oop, card is clean.
238   // dirty card and log.
239 
240   __ strb(zr, Address(card_addr));
241 
242   __ ldr(rscratch1, queue_index);
243   __ cbz(rscratch1, runtime);
244   __ sub(rscratch1, rscratch1, wordSize);
245   __ str(rscratch1, queue_index);
246 
247   __ ldr(tmp2, buffer);
248   __ str(card_addr, Address(tmp2, rscratch1));
249   __ b(done);
250 
251   __ bind(runtime);
252   // save the live input values
253   RegSet saved = RegSet::of(store_addr);
254   __ push(saved, sp);
255   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread);
256   __ pop(saved, sp);
257 
258   __ bind(done);
259 }
260 
261 void G1BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
262                                     Register dst, Address src, Register tmp1, Register tmp_thread) {
263   bool on_oop = is_reference_type(type);
264   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
265   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
266   bool on_reference = on_weak || on_phantom;
267   ModRefBarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
268   if (on_oop && on_reference) {
269     // LR is live.  It must be saved around calls.
270     __ enter(); // barrier may call runtime
271     // Generate the G1 pre-barrier code to log the value of
272     // the referent field in an SATB buffer.
273     g1_write_barrier_pre(masm /* masm */,
274                          noreg /* obj */,
275                          dst /* pre_val */,
276                          rthread /* thread */,
277                          tmp1 /* tmp */,
278                          true /* tosca_live */,
279                          true /* expand_call */);
280     __ leave();
281   }
282 }
283 
284 void G1BarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
285                                          Address dst, Register val, Register tmp1, Register tmp2) {
286   // flatten object address if needed
287   if (dst.index() == noreg && dst.offset() == 0) {
288     if (dst.base() != r3) {
289       __ mov(r3, dst.base());
290     }
291   } else {
292     __ lea(r3, dst);
293   }
294 
295   g1_write_barrier_pre(masm,
296                        r3 /* obj */,
297                        tmp2 /* pre_val */,
298                        rthread /* thread */,
299                        tmp1  /* tmp */,
300                        val != noreg /* tosca_live */,
301                        false /* expand_call */);
302 
303   if (val == noreg) {
304     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), noreg, noreg, noreg);
305   } else {
306     // G1 barrier needs uncompressed oop for region cross check.
307     Register new_val = val;
308     if (UseCompressedOops) {
309       new_val = rscratch2;
310       __ mov(new_val, val);
311     }
312     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), val, noreg, noreg);
313     g1_write_barrier_post(masm,
314                           r3 /* store_adr */,
315                           new_val /* new_val */,
316                           rthread /* thread */,
317                           tmp1 /* tmp */,
318                           tmp2 /* tmp2 */);
319   }
320 
321 }
322 
323 #ifdef COMPILER1
324 
325 #undef __
326 #define __ ce->masm()->
327 
328 void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) {
329   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
330   // At this point we know that marking is in progress.
331   // If do_load() is true then we have to emit the
332   // load of the previous value; otherwise it has already
333   // been loaded into _pre_val.
334 
335   __ bind(*stub->entry());
336 
337   assert(stub->pre_val()->is_register(), "Precondition.");
338 
339   Register pre_val_reg = stub->pre_val()->as_register();
340 
341   if (stub->do_load()) {
342     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
343   }
344   __ cbz(pre_val_reg, *stub->continuation());
345   ce->store_parameter(stub->pre_val()->as_register(), 0);
346   __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
347   __ b(*stub->continuation());
348 }
349 
350 void G1BarrierSetAssembler::gen_post_barrier_stub(LIR_Assembler* ce, G1PostBarrierStub* stub) {
351   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
352   __ bind(*stub->entry());
353   assert(stub->addr()->is_register(), "Precondition.");
354   assert(stub->new_val()->is_register(), "Precondition.");
355   Register new_val_reg = stub->new_val()->as_register();
356   __ cbz(new_val_reg, *stub->continuation());
357   ce->store_parameter(stub->addr()->as_pointer_register(), 0);
358   __ far_call(RuntimeAddress(bs->post_barrier_c1_runtime_code_blob()->code_begin()));
359   __ b(*stub->continuation());
360 }
361 
362 #undef __
363 
364 #define __ sasm->
365 
366 void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
367   __ prologue("g1_pre_barrier", false);
368 
369   // arg0 : previous value of memory
370 
371   BarrierSet* bs = BarrierSet::barrier_set();
372 
373   const Register pre_val = r0;
374   const Register thread = rthread;
375   const Register tmp = rscratch1;
376 
377   Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
378   Address queue_index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
379   Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
380 
381   Label done;
382   Label runtime;
383 
384   // Is marking still active?
385   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
386     __ ldrw(tmp, in_progress);
387   } else {
388     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
389     __ ldrb(tmp, in_progress);
390   }
391   __ cbzw(tmp, done);
392 
393   // Can we store original value in the thread's buffer?
394   __ ldr(tmp, queue_index);
395   __ cbz(tmp, runtime);
396 
397   __ sub(tmp, tmp, wordSize);
398   __ str(tmp, queue_index);
399   __ ldr(rscratch2, buffer);
400   __ add(tmp, tmp, rscratch2);
401   __ load_parameter(0, rscratch2);
402   __ str(rscratch2, Address(tmp, 0));
403   __ b(done);
404 
405   __ bind(runtime);
406   __ push_call_clobbered_registers();
407   __ load_parameter(0, pre_val);
408   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
409   __ pop_call_clobbered_registers();
410   __ bind(done);
411 
412   __ epilogue();
413 }
414 
415 void G1BarrierSetAssembler::generate_c1_post_barrier_runtime_stub(StubAssembler* sasm) {
416   __ prologue("g1_post_barrier", false);
417 
418   // arg0: store_address
419   Address store_addr(rfp, 2*BytesPerWord);
420 
421   BarrierSet* bs = BarrierSet::barrier_set();
422   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
423   CardTable* ct = ctbs->card_table();
424 
425   Label done;
426   Label runtime;
427 
428   // At this point we know new_value is non-NULL and the new_value crosses regions.
429   // Must check to see if card is already dirty
430 
431   const Register thread = rthread;
432 
433   Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));
434   Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));
435 
436   const Register card_offset = rscratch2;
437   // LR is free here, so we can use it to hold the byte_map_base.
438   const Register byte_map_base = lr;
439 
440   assert_different_registers(card_offset, byte_map_base, rscratch1);
441 
442   __ load_parameter(0, card_offset);
443   __ lsr(card_offset, card_offset, CardTable::card_shift);
444   __ load_byte_map_base(byte_map_base);
445   __ ldrb(rscratch1, Address(byte_map_base, card_offset));
446   __ cmpw(rscratch1, (int)G1CardTable::g1_young_card_val());
447   __ br(Assembler::EQ, done);
448 
449   assert((int)CardTable::dirty_card_val() == 0, "must be 0");
450 
451   __ membar(Assembler::StoreLoad);
452   __ ldrb(rscratch1, Address(byte_map_base, card_offset));
453   __ cbzw(rscratch1, done);
454 
455   // storing region crossing non-NULL, card is clean.
456   // dirty card and log.
457   __ strb(zr, Address(byte_map_base, card_offset));
458 
459   // Convert card offset into an address in card_addr
460   Register card_addr = card_offset;
461   __ add(card_addr, byte_map_base, card_addr);
462 
463   __ ldr(rscratch1, queue_index);
464   __ cbz(rscratch1, runtime);
465   __ sub(rscratch1, rscratch1, wordSize);
466   __ str(rscratch1, queue_index);
467 
468   // Reuse LR to hold buffer_addr
469   const Register buffer_addr = lr;
470 
471   __ ldr(buffer_addr, buffer);
472   __ str(card_addr, Address(buffer_addr, rscratch1));
473   __ b(done);
474 
475   __ bind(runtime);
476   __ push_call_clobbered_registers();
477   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread);
478   __ pop_call_clobbered_registers();
479   __ bind(done);
480   __ epilogue();
481 }
482 
483 #undef __
484 
485 #endif // COMPILER1