1 /*
  2  * Copyright (c) 2018, 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 "c1/c1_LIRAssembler.hpp"
 28 #include "c1/c1_MacroAssembler.hpp"
 29 #include "gc/g1/c1/g1BarrierSetC1.hpp"
 30 #include "gc/g1/g1BarrierSet.hpp"
 31 #include "gc/g1/g1BarrierSetAssembler.hpp"
 32 #include "gc/g1/g1CardTable.hpp"
 33 #include "gc/g1/g1ThreadLocalData.hpp"
 34 #include "gc/g1/heapRegion.hpp"
 35 #include "gc/shared/collectedHeap.hpp"
 36 #include "runtime/sharedRuntime.hpp"
 37 #include "runtime/thread.hpp"
 38 #include "interpreter/interp_masm.hpp"
 39 #include "runtime/sharedRuntime.hpp"
 40 
 41 #define __ masm->
 42 
 43 void G1BarrierSetAssembler::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators,
 44                                                             Register addr, Register count, RegSet saved_regs) {
 45   bool dest_uninitialized = (decorators & AS_DEST_NOT_INITIALIZED) != 0;
 46   if (!dest_uninitialized) {
 47     __ push(saved_regs, sp);
 48     if (count == c_rarg0) {
 49       if (addr == c_rarg1) {
 50         // exactly backwards!!
 51         __ mov(rscratch1, c_rarg0);
 52         __ mov(c_rarg0, c_rarg1);
 53         __ mov(c_rarg1, rscratch1);
 54       } else {
 55         __ mov(c_rarg1, count);
 56         __ mov(c_rarg0, addr);
 57       }
 58     } else {
 59       __ mov(c_rarg0, addr);
 60       __ mov(c_rarg1, count);
 61     }
 62     if (UseCompressedOops) {
 63       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_pre_narrow_oop_entry), 2);
 64     } else {
 65       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_pre_oop_entry), 2);
 66     }
 67     __ pop(saved_regs, sp);
 68   }
 69 }
 70 
 71 void G1BarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
 72                                                              Register start, Register end, Register scratch, RegSet saved_regs) {
 73   __ push(saved_regs, sp);
 74   // must compute element count unless barrier set interface is changed (other platforms supply count)
 75   assert_different_registers(start, end, scratch);
 76   __ lea(scratch, Address(end, BytesPerHeapOop));
 77   __ sub(scratch, scratch, start);               // subtract start to get #bytes
 78   __ lsr(scratch, scratch, LogBytesPerHeapOop);  // convert to element count
 79   __ mov(c_rarg0, start);
 80   __ mov(c_rarg1, scratch);
 81   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_post_entry), 2);
 82   __ pop(saved_regs, sp);
 83 }
 84 
 85 void G1BarrierSetAssembler::g1_write_barrier_pre(MacroAssembler* masm,
 86                                                  Register obj,
 87                                                  Register pre_val,
 88                                                  Register thread,
 89                                                  Register tmp,
 90                                                  bool tosca_live,
 91                                                  bool expand_call) {
 92   // If expand_call is true then we expand the call_VM_leaf macro
 93   // directly to skip generating the check by
 94   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
 95 
 96   assert(thread == rthread, "must be");
 97 
 98   Label done;
 99   Label runtime;
100 
101   assert_different_registers(obj, pre_val, tmp, rscratch1);
102   assert(pre_val != noreg &&  tmp != noreg, "expecting a register");
103 
104   Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
105   Address index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
106   Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
107 
108   // Is marking active?
109   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
110     __ ldrw(tmp, in_progress);
111   } else {
112     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
113     __ ldrb(tmp, in_progress);
114   }
115   __ cbzw(tmp, done);
116 
117   // Do we need to load the previous value?
118   if (obj != noreg) {
119     __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
120   }
121 
122   // Is the previous value null?
123   __ cbz(pre_val, done);
124 
125   // Can we store original value in the thread's buffer?
126   // Is index == 0?
127   // (The index field is typed as size_t.)
128 
129   __ ldr(tmp, index);                      // tmp := *index_adr
130   __ cbz(tmp, runtime);                    // tmp == 0?
131                                         // If yes, goto runtime
132 
133   __ sub(tmp, tmp, wordSize);              // tmp := tmp - wordSize
134   __ str(tmp, index);                      // *index_adr := tmp
135   __ ldr(rscratch1, buffer);
136   __ add(tmp, tmp, rscratch1);             // tmp := tmp + *buffer_adr
137 
138   // Record the previous value
139   __ str(pre_val, Address(tmp, 0));
140   __ b(done);
141 
142   __ bind(runtime);
143   // save the live input values
144   RegSet saved = RegSet::of(pre_val);
145   if (tosca_live) saved += RegSet::of(r0);
146   if (obj != noreg) saved += RegSet::of(obj);
147 
148   __ push(saved, sp);
149 
150   // Calling the runtime using the regular call_VM_leaf mechanism generates
151   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
152   // that checks that the *(rfp+frame::interpreter_frame_last_sp) == NULL.
153   //
154   // If we care generating the pre-barrier without a frame (e.g. in the
155   // intrinsified Reference.get() routine) then ebp might be pointing to
156   // the caller frame and so this check will most likely fail at runtime.
157   //
158   // Expanding the call directly bypasses the generation of the check.
159   // So when we do not have have a full interpreter frame on the stack
160   // expand_call should be passed true.
161 
162   if (expand_call) {
163     assert(pre_val != c_rarg1, "smashed arg");
164     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_pre), pre_val, thread);
165   } else {
166     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_pre), pre_val, thread);
167   }
168 
169   __ pop(saved, sp);
170 
171   __ bind(done);
172 
173 }
174 
175 void G1BarrierSetAssembler::g1_write_barrier_post(MacroAssembler* masm,
176                                                   Register store_addr,
177                                                   Register new_val,
178                                                   Register thread,
179                                                   Register tmp,
180                                                   Register tmp2) {
181   assert(thread == rthread, "must be");
182   assert_different_registers(store_addr, new_val, thread, tmp, tmp2,
183                              rscratch1);
184   assert(store_addr != noreg && new_val != noreg && tmp != noreg
185          && tmp2 != noreg, "expecting a register");
186 
187   Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));
188   Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));
189 
190   BarrierSet* bs = BarrierSet::barrier_set();
191   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
192   CardTable* ct = ctbs->card_table();
193   assert(sizeof(*ct->byte_map_base()) == sizeof(jbyte), "adjust this code");
194 
195   Label done;
196   Label runtime;
197 
198   // Does store cross heap regions?
199 
200   __ eor(tmp, store_addr, new_val);
201   __ lsr(tmp, tmp, HeapRegion::LogOfHRGrainBytes);
202   __ cbz(tmp, done);
203 
204   // crosses regions, storing NULL?
205 
206   __ cbz(new_val, done);
207 
208   // storing region crossing non-NULL, is card already dirty?
209 
210   ExternalAddress cardtable((address) ct->byte_map_base());
211   assert(sizeof(*ct->byte_map_base()) == sizeof(jbyte), "adjust this code");
212   const Register card_addr = tmp;
213 
214   __ lsr(card_addr, store_addr, CardTable::card_shift);
215 
216   // get the address of the card
217   __ load_byte_map_base(tmp2);
218   __ add(card_addr, card_addr, tmp2);
219   __ ldrb(tmp2, Address(card_addr));
220   __ cmpw(tmp2, (int)G1CardTable::g1_young_card_val());
221   __ br(Assembler::EQ, done);
222 
223   assert((int)CardTable::dirty_card_val() == 0, "must be 0");
224 
225   __ membar(Assembler::StoreLoad);
226 
227   __ ldrb(tmp2, Address(card_addr));
228   __ cbzw(tmp2, done);
229 
230   // storing a region crossing, non-NULL oop, card is clean.
231   // dirty card and log.
232 
233   __ strb(zr, Address(card_addr));
234 
235   __ ldr(rscratch1, queue_index);
236   __ cbz(rscratch1, runtime);
237   __ sub(rscratch1, rscratch1, wordSize);
238   __ str(rscratch1, queue_index);
239 
240   __ ldr(tmp2, buffer);
241   __ str(card_addr, Address(tmp2, rscratch1));
242   __ b(done);
243 
244   __ bind(runtime);
245   // save the live input values
246   RegSet saved = RegSet::of(store_addr, new_val);
247   __ push(saved, sp);
248   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_post), card_addr, thread);
249   __ pop(saved, sp);
250 
251   __ bind(done);
252 }
253 
254 void G1BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
255                                     Register dst, Address src, Register tmp1, Register tmp_thread) {
256   bool on_oop = type == T_OBJECT || type == T_ARRAY;
257   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
258   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
259   bool on_reference = on_weak || on_phantom;
260   ModRefBarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
261   if (on_oop && on_reference) {
262     // LR is live.  It must be saved around calls.
263     __ enter(); // barrier may call runtime
264     // Generate the G1 pre-barrier code to log the value of
265     // the referent field in an SATB buffer.
266     g1_write_barrier_pre(masm /* masm */,
267                          noreg /* obj */,
268                          dst /* pre_val */,
269                          rthread /* thread */,
270                          tmp1 /* tmp */,
271                          true /* tosca_live */,
272                          true /* expand_call */);
273     __ leave();
274   }
275 }
276 
277 void G1BarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
278                                          Address dst, Register val, Register tmp1, Register tmp2) {
279   // flatten object address if needed
280   if (dst.index() == noreg && dst.offset() == 0) {
281     if (dst.base() != r3) {
282       __ mov(r3, dst.base());
283     }
284   } else {
285     __ lea(r3, dst);
286   }
287 
288   g1_write_barrier_pre(masm,
289                        r3 /* obj */,
290                        tmp2 /* pre_val */,
291                        rthread /* thread */,
292                        tmp1  /* tmp */,
293                        val != noreg /* tosca_live */,
294                        false /* expand_call */);
295 
296   if (val == noreg) {
297     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), noreg, noreg, noreg);
298   } else {
299     // G1 barrier needs uncompressed oop for region cross check.
300     Register new_val = val;
301     if (UseCompressedOops) {
302       new_val = rscratch2;
303       __ mov(new_val, val);
304     }
305     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), val, noreg, noreg);
306     g1_write_barrier_post(masm,
307                           r3 /* store_adr */,
308                           new_val /* new_val */,
309                           rthread /* thread */,
310                           tmp1 /* tmp */,
311                           tmp2 /* tmp2 */);
312   }
313 
314 }
315 
316 #ifdef COMPILER1
317 
318 #undef __
319 #define __ ce->masm()->
320 
321 void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) {
322   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
323   // At this point we know that marking is in progress.
324   // If do_load() is true then we have to emit the
325   // load of the previous value; otherwise it has already
326   // been loaded into _pre_val.
327 
328   __ bind(*stub->entry());
329 
330   assert(stub->pre_val()->is_register(), "Precondition.");
331 
332   Register pre_val_reg = stub->pre_val()->as_register();
333 
334   if (stub->do_load()) {
335     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/);
336   }
337   __ cbz(pre_val_reg, *stub->continuation());
338   ce->store_parameter(stub->pre_val()->as_register(), 0);
339   __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
340   __ b(*stub->continuation());
341 }
342 
343 void G1BarrierSetAssembler::gen_post_barrier_stub(LIR_Assembler* ce, G1PostBarrierStub* stub) {
344   G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
345   __ bind(*stub->entry());
346   assert(stub->addr()->is_register(), "Precondition.");
347   assert(stub->new_val()->is_register(), "Precondition.");
348   Register new_val_reg = stub->new_val()->as_register();
349   __ cbz(new_val_reg, *stub->continuation());
350   ce->store_parameter(stub->addr()->as_pointer_register(), 0);
351   __ far_call(RuntimeAddress(bs->post_barrier_c1_runtime_code_blob()->code_begin()));
352   __ b(*stub->continuation());
353 }
354 
355 #undef __
356 
357 #define __ sasm->
358 
359 void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
360   __ prologue("g1_pre_barrier", false);
361 
362   // arg0 : previous value of memory
363 
364   BarrierSet* bs = BarrierSet::barrier_set();
365 
366   const Register pre_val = r0;
367   const Register thread = rthread;
368   const Register tmp = rscratch1;
369 
370   Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
371   Address queue_index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
372   Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
373 
374   Label done;
375   Label runtime;
376 
377   // Is marking still active?
378   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
379     __ ldrw(tmp, in_progress);
380   } else {
381     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
382     __ ldrb(tmp, in_progress);
383   }
384   __ cbzw(tmp, done);
385 
386   // Can we store original value in the thread's buffer?
387   __ ldr(tmp, queue_index);
388   __ cbz(tmp, runtime);
389 
390   __ sub(tmp, tmp, wordSize);
391   __ str(tmp, queue_index);
392   __ ldr(rscratch2, buffer);
393   __ add(tmp, tmp, rscratch2);
394   __ load_parameter(0, rscratch2);
395   __ str(rscratch2, Address(tmp, 0));
396   __ b(done);
397 
398   __ bind(runtime);
399   __ push_call_clobbered_registers();
400   __ load_parameter(0, pre_val);
401   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_pre), pre_val, thread);
402   __ pop_call_clobbered_registers();
403   __ bind(done);
404 
405   __ epilogue();
406 }
407 
408 void G1BarrierSetAssembler::generate_c1_post_barrier_runtime_stub(StubAssembler* sasm) {
409   __ prologue("g1_post_barrier", false);
410 
411   // arg0: store_address
412   Address store_addr(rfp, 2*BytesPerWord);
413 
414   BarrierSet* bs = BarrierSet::barrier_set();
415   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
416   CardTable* ct = ctbs->card_table();
417   assert(sizeof(*ct->byte_map_base()) == sizeof(jbyte), "adjust this code");
418 
419   Label done;
420   Label runtime;
421 
422   // At this point we know new_value is non-NULL and the new_value crosses regions.
423   // Must check to see if card is already dirty
424 
425   const Register thread = rthread;
426 
427   Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));
428   Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));
429 
430   const Register card_offset = rscratch2;
431   // LR is free here, so we can use it to hold the byte_map_base.
432   const Register byte_map_base = lr;
433 
434   assert_different_registers(card_offset, byte_map_base, rscratch1);
435 
436   __ load_parameter(0, card_offset);
437   __ lsr(card_offset, card_offset, CardTable::card_shift);
438   __ load_byte_map_base(byte_map_base);
439   __ ldrb(rscratch1, Address(byte_map_base, card_offset));
440   __ cmpw(rscratch1, (int)G1CardTable::g1_young_card_val());
441   __ br(Assembler::EQ, done);
442 
443   assert((int)CardTable::dirty_card_val() == 0, "must be 0");
444 
445   __ membar(Assembler::StoreLoad);
446   __ ldrb(rscratch1, Address(byte_map_base, card_offset));
447   __ cbzw(rscratch1, done);
448 
449   // storing region crossing non-NULL, card is clean.
450   // dirty card and log.
451   __ strb(zr, Address(byte_map_base, card_offset));
452 
453   // Convert card offset into an address in card_addr
454   Register card_addr = card_offset;
455   __ add(card_addr, byte_map_base, card_addr);
456 
457   __ ldr(rscratch1, queue_index);
458   __ cbz(rscratch1, runtime);
459   __ sub(rscratch1, rscratch1, wordSize);
460   __ str(rscratch1, queue_index);
461 
462   // Reuse LR to hold buffer_addr
463   const Register buffer_addr = lr;
464 
465   __ ldr(buffer_addr, buffer);
466   __ str(card_addr, Address(buffer_addr, rscratch1));
467   __ b(done);
468 
469   __ bind(runtime);
470   __ push_call_clobbered_registers();
471   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_post), card_addr, thread);
472   __ pop_call_clobbered_registers();
473   __ bind(done);
474   __ epilogue();
475 }
476 
477 #undef __
478 
479 #endif // COMPILER1