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