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 "classfile/classLoaderData.hpp"
 27 #include "gc/shared/barrierSet.hpp"
 28 #include "gc/shared/barrierSetAssembler.hpp"
 29 #include "gc/shared/barrierSetNMethod.hpp"
 30 #include "gc/shared/barrierSetRuntime.hpp"
 31 #include "gc/shared/collectedHeap.hpp"
 32 #include "interpreter/interp_masm.hpp"
 33 #include "memory/universe.hpp"
 34 #include "runtime/jniHandles.hpp"
 35 #include "runtime/sharedRuntime.hpp"
 36 #include "runtime/stubRoutines.hpp"
 37 #include "runtime/thread.hpp"
 38 
 39 
 40 #define __ masm->
 41 
 42 void BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 43                                   Register dst, Address src, Register tmp1, Register tmp_thread) {
 44 
 45   // LR is live.  It must be saved around calls.
 46 
 47   bool in_heap = (decorators & IN_HEAP) != 0;
 48   bool in_native = (decorators & IN_NATIVE) != 0;
 49   bool is_not_null = (decorators & IS_NOT_NULL) != 0;
 50 
 51   assert(type != T_INLINE_TYPE, "Not supported yet");
 52   switch (type) {
 53   case T_OBJECT:
 54   case T_ARRAY: {
 55     if (in_heap) {
 56       if (UseCompressedOops) {
 57         __ ldrw(dst, src);
 58         if (is_not_null) {
 59           __ decode_heap_oop_not_null(dst);
 60         } else {
 61           __ decode_heap_oop(dst);
 62         }
 63       } else {
 64         __ ldr(dst, src);
 65       }
 66     } else {
 67       assert(in_native, "why else?");
 68       __ ldr(dst, src);
 69     }
 70     break;
 71   }
 72   case T_BOOLEAN: __ load_unsigned_byte (dst, src); break;
 73   case T_BYTE:    __ load_signed_byte   (dst, src); break;
 74   case T_CHAR:    __ load_unsigned_short(dst, src); break;
 75   case T_SHORT:   __ load_signed_short  (dst, src); break;
 76   case T_INT:     __ ldrw               (dst, src); break;
 77   case T_LONG:    __ ldr                (dst, src); break;
 78   case T_ADDRESS: __ ldr                (dst, src); break;
 79   case T_FLOAT:   __ ldrs               (v0, src);  break;
 80   case T_DOUBLE:  __ ldrd               (v0, src);  break;
 81   default: Unimplemented();
 82   }
 83 }
 84 
 85 void BarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 86                                    Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
 87   bool in_heap = (decorators & IN_HEAP) != 0;
 88   bool in_native = (decorators & IN_NATIVE) != 0;
 89   bool is_not_null = (decorators & IS_NOT_NULL) != 0;
 90 
 91   assert(type != T_INLINE_TYPE, "Not supported yet");
 92   switch (type) {
 93   case T_OBJECT:
 94   case T_ARRAY: {
 95     if (in_heap) {
 96       if (val == noreg) {
 97         assert(!is_not_null, "inconsistent access");
 98         if (UseCompressedOops) {
 99           __ strw(zr, dst);
100         } else {
101           __ str(zr, dst);
102         }
103       } else {
104         if (UseCompressedOops) {
105           assert(!dst.uses(val), "not enough registers");
106           if (is_not_null) {
107             __ encode_heap_oop_not_null(val);
108           } else {
109             __ encode_heap_oop(val);
110           }
111           __ strw(val, dst);
112         } else {
113           __ str(val, dst);
114         }
115       }
116     } else {
117       assert(in_native, "why else?");
118       assert(val != noreg, "not supported");
119       __ str(val, dst);
120     }
121     break;
122   }
123   case T_BOOLEAN:
124     __ andw(val, val, 0x1);  // boolean is true if LSB is 1
125     __ strb(val, dst);
126     break;
127   case T_BYTE:    __ strb(val, dst); break;
128   case T_CHAR:    __ strh(val, dst); break;
129   case T_SHORT:   __ strh(val, dst); break;
130   case T_INT:     __ strw(val, dst); break;
131   case T_LONG:    __ str (val, dst); break;
132   case T_ADDRESS: __ str (val, dst); break;
133   case T_FLOAT:   __ strs(v0,  dst); break;
134   case T_DOUBLE:  __ strd(v0,  dst); break;
135   default: Unimplemented();
136   }
137 }
138 
139 void BarrierSetAssembler::value_copy(MacroAssembler* masm, DecoratorSet decorators,
140                                      Register src, Register dst, Register value_klass) {
141   // value_copy implementation is fairly complex, and there are not any
142   // "short-cuts" to be made from asm. What there is, appears to have the same
143   // cost in C++, so just "call_VM_leaf" for now rather than maintain hundreds
144   // of hand-rolled instructions...
145   if (decorators & IS_DEST_UNINITIALIZED) {
146     __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSetRuntime::value_copy_is_dest_uninitialized), src, dst, value_klass);
147   } else {
148     __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSetRuntime::value_copy), src, dst, value_klass);
149   }
150 }
151 
152 void BarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
153                                                         Register obj, Register tmp, Label& slowpath) {
154   // If mask changes we need to ensure that the inverse is still encodable as an immediate
155   STATIC_ASSERT(JNIHandles::weak_tag_mask == 1);
156   __ andr(obj, obj, ~JNIHandles::weak_tag_mask);
157   __ ldr(obj, Address(obj, 0));             // *obj
158 }
159 
160 // Defines obj, preserves var_size_in_bytes, okay for t2 == var_size_in_bytes.
161 void BarrierSetAssembler::tlab_allocate(MacroAssembler* masm, Register obj,
162                                         Register var_size_in_bytes,
163                                         int con_size_in_bytes,
164                                         Register t1,
165                                         Register t2,
166                                         Label& slow_case) {
167   assert_different_registers(obj, t2);
168   assert_different_registers(obj, var_size_in_bytes);
169   Register end = t2;
170 
171   // verify_tlab();
172 
173   __ ldr(obj, Address(rthread, JavaThread::tlab_top_offset()));
174   if (var_size_in_bytes == noreg) {
175     __ lea(end, Address(obj, con_size_in_bytes));
176   } else {
177     __ lea(end, Address(obj, var_size_in_bytes));
178   }
179   __ ldr(rscratch1, Address(rthread, JavaThread::tlab_end_offset()));
180   __ cmp(end, rscratch1);
181   __ br(Assembler::HI, slow_case);
182 
183   // update the tlab top pointer
184   __ str(end, Address(rthread, JavaThread::tlab_top_offset()));
185 
186   // recover var_size_in_bytes if necessary
187   if (var_size_in_bytes == end) {
188     __ sub(var_size_in_bytes, var_size_in_bytes, obj);
189   }
190   // verify_tlab();
191 }
192 
193 // Defines obj, preserves var_size_in_bytes
194 void BarrierSetAssembler::eden_allocate(MacroAssembler* masm, Register obj,
195                                         Register var_size_in_bytes,
196                                         int con_size_in_bytes,
197                                         Register t1,
198                                         Label& slow_case) {
199   assert_different_registers(obj, var_size_in_bytes, t1);
200   if (!Universe::heap()->supports_inline_contig_alloc()) {
201     __ b(slow_case);
202   } else {
203     Register end = t1;
204     Register heap_end = rscratch2;
205     Label retry;
206     __ bind(retry);
207     {
208       uint64_t offset;
209       __ adrp(rscratch1, ExternalAddress((address) Universe::heap()->end_addr()), offset);
210       __ ldr(heap_end, Address(rscratch1, offset));
211     }
212 
213     ExternalAddress heap_top((address) Universe::heap()->top_addr());
214 
215     // Get the current top of the heap
216     {
217       uint64_t offset;
218       __ adrp(rscratch1, heap_top, offset);
219       // Use add() here after ARDP, rather than lea().
220       // lea() does not generate anything if its offset is zero.
221       // However, relocs expect to find either an ADD or a load/store
222       // insn after an ADRP.  add() always generates an ADD insn, even
223       // for add(Rn, Rn, 0).
224       __ add(rscratch1, rscratch1, offset);
225       __ ldaxr(obj, rscratch1);
226     }
227 
228     // Adjust it my the size of our new object
229     if (var_size_in_bytes == noreg) {
230       __ lea(end, Address(obj, con_size_in_bytes));
231     } else {
232       __ lea(end, Address(obj, var_size_in_bytes));
233     }
234 
235     // if end < obj then we wrapped around high memory
236     __ cmp(end, obj);
237     __ br(Assembler::LO, slow_case);
238 
239     __ cmp(end, heap_end);
240     __ br(Assembler::HI, slow_case);
241 
242     // If heap_top hasn't been changed by some other thread, update it.
243     __ stlxr(rscratch2, end, rscratch1);
244     __ cbnzw(rscratch2, retry);
245 
246     incr_allocated_bytes(masm, var_size_in_bytes, con_size_in_bytes, t1);
247   }
248 }
249 
250 void BarrierSetAssembler::incr_allocated_bytes(MacroAssembler* masm,
251                                                Register var_size_in_bytes,
252                                                int con_size_in_bytes,
253                                                Register t1) {
254   assert(t1->is_valid(), "need temp reg");
255 
256   __ ldr(t1, Address(rthread, in_bytes(JavaThread::allocated_bytes_offset())));
257   if (var_size_in_bytes->is_valid()) {
258     __ add(t1, t1, var_size_in_bytes);
259   } else {
260     __ add(t1, t1, con_size_in_bytes);
261   }
262   __ str(t1, Address(rthread, in_bytes(JavaThread::allocated_bytes_offset())));
263 }
264 
265 void BarrierSetAssembler::nmethod_entry_barrier(MacroAssembler* masm) {
266   BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
267 
268   if (bs_nm == NULL) {
269     return;
270   }
271 
272   Label skip, guard;
273   Address thread_disarmed_addr(rthread, in_bytes(bs_nm->thread_disarmed_offset()));
274 
275   __ ldrw(rscratch1, guard);
276 
277   // Subsequent loads of oops must occur after load of guard value.
278   // BarrierSetNMethod::disarm sets guard with release semantics.
279   __ membar(__ LoadLoad);
280   __ ldrw(rscratch2, thread_disarmed_addr);
281   __ cmpw(rscratch1, rscratch2);
282   __ br(Assembler::EQ, skip);
283 
284   __ movptr(rscratch1, (uintptr_t) StubRoutines::aarch64::method_entry_barrier());
285   __ blr(rscratch1);
286   __ b(skip);
287 
288   __ bind(guard);
289 
290   __ emit_int32(0);   // nmethod guard value. Skipped over in common case.
291 
292   __ bind(skip);
293 }
294 
295 void BarrierSetAssembler::c2i_entry_barrier(MacroAssembler* masm) {
296   BarrierSetNMethod* bs = BarrierSet::barrier_set()->barrier_set_nmethod();
297   if (bs == NULL) {
298     return;
299   }
300 
301   Label bad_call;
302   __ cbz(rmethod, bad_call);
303 
304   // Pointer chase to the method holder to find out if the method is concurrently unloading.
305   Label method_live;
306   __ load_method_holder_cld(rscratch1, rmethod);
307 
308   // Is it a strong CLD?
309   __ ldrw(rscratch2, Address(rscratch1, ClassLoaderData::keep_alive_offset()));
310   __ cbnz(rscratch2, method_live);
311 
312   // Is it a weak but alive CLD?
313   __ stp(r10, r11, Address(__ pre(sp, -2 * wordSize)));
314   __ ldr(r10, Address(rscratch1, ClassLoaderData::holder_offset()));
315 
316   // Uses rscratch1 & rscratch2, so we must pass new temporaries.
317   __ resolve_weak_handle(r10, r11);
318   __ mov(rscratch1, r10);
319   __ ldp(r10, r11, Address(__ post(sp, 2 * wordSize)));
320   __ cbnz(rscratch1, method_live);
321 
322   __ bind(bad_call);
323 
324   __ far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
325   __ bind(method_live);
326 }