1 /* 2 * Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2014, 2021, Red Hat Inc. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "c1/c1_MacroAssembler.hpp" 28 #include "c1/c1_Runtime1.hpp" 29 #include "gc/shared/barrierSetAssembler.hpp" 30 #include "gc/shared/collectedHeap.hpp" 31 #include "gc/shared/tlab_globals.hpp" 32 #include "interpreter/interpreter.hpp" 33 #include "oops/arrayOop.hpp" 34 #include "oops/markWord.hpp" 35 #include "runtime/basicLock.hpp" 36 #include "runtime/os.hpp" 37 #include "runtime/sharedRuntime.hpp" 38 #include "runtime/stubRoutines.hpp" 39 40 void C1_MacroAssembler::float_cmp(bool is_float, int unordered_result, 41 FloatRegister f0, FloatRegister f1, 42 Register result) 43 { 44 Label done; 45 if (is_float) { 46 fcmps(f0, f1); 47 } else { 48 fcmpd(f0, f1); 49 } 50 if (unordered_result < 0) { 51 // we want -1 for unordered or less than, 0 for equal and 1 for 52 // greater than. 53 cset(result, NE); // Not equal or unordered 54 cneg(result, result, LT); // Less than or unordered 55 } else { 56 // we want -1 for less than, 0 for equal and 1 for unordered or 57 // greater than. 58 cset(result, NE); // Not equal or unordered 59 cneg(result, result, LO); // Less than 60 } 61 } 62 63 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) { 64 const int aligned_mask = BytesPerWord -1; 65 const int hdr_offset = oopDesc::mark_offset_in_bytes(); 66 assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different"); 67 Label done; 68 int null_check_offset = -1; 69 70 verify_oop(obj); 71 72 // save object being locked into the BasicObjectLock 73 str(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes())); 74 75 null_check_offset = offset(); 76 77 if (DiagnoseSyncOnValueBasedClasses != 0) { 78 load_klass(hdr, obj); 79 ldrw(hdr, Address(hdr, Klass::access_flags_offset())); 80 tstw(hdr, JVM_ACC_IS_VALUE_BASED_CLASS); 81 br(Assembler::NE, slow_case); 82 } 83 84 // Load object header 85 ldr(hdr, Address(obj, hdr_offset)); 86 // and mark it as unlocked 87 orr(hdr, hdr, markWord::unlocked_value); 88 // save unlocked object header into the displaced header location on the stack 89 str(hdr, Address(disp_hdr, 0)); 90 // test if object header is still the same (i.e. unlocked), and if so, store the 91 // displaced header address in the object header - if it is not the same, get the 92 // object header instead 93 lea(rscratch2, Address(obj, hdr_offset)); 94 cmpxchgptr(hdr, disp_hdr, rscratch2, rscratch1, done, /*fallthough*/NULL); 95 // if the object header was the same, we're done 96 // if the object header was not the same, it is now in the hdr register 97 // => test if it is a stack pointer into the same stack (recursive locking), i.e.: 98 // 99 // 1) (hdr & aligned_mask) == 0 100 // 2) sp <= hdr 101 // 3) hdr <= sp + page_size 102 // 103 // these 3 tests can be done by evaluating the following expression: 104 // 105 // (hdr - sp) & (aligned_mask - page_size) 106 // 107 // assuming both the stack pointer and page_size have their least 108 // significant 2 bits cleared and page_size is a power of 2 109 mov(rscratch1, sp); 110 sub(hdr, hdr, rscratch1); 111 ands(hdr, hdr, aligned_mask - os::vm_page_size()); 112 // for recursive locking, the result is zero => save it in the displaced header 113 // location (NULL in the displaced hdr location indicates recursive locking) 114 str(hdr, Address(disp_hdr, 0)); 115 // otherwise we don't care about the result and handle locking via runtime call 116 cbnz(hdr, slow_case); 117 // done 118 bind(done); 119 return null_check_offset; 120 } 121 122 123 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) { 124 const int aligned_mask = BytesPerWord -1; 125 const int hdr_offset = oopDesc::mark_offset_in_bytes(); 126 assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different"); 127 Label done; 128 129 // load displaced header 130 ldr(hdr, Address(disp_hdr, 0)); 131 // if the loaded hdr is NULL we had recursive locking 132 // if we had recursive locking, we are done 133 cbz(hdr, done); 134 // load object 135 ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes())); 136 verify_oop(obj); 137 // test if object header is pointing to the displaced header, and if so, restore 138 // the displaced header in the object - if the object header is not pointing to 139 // the displaced header, get the object header instead 140 // if the object header was not pointing to the displaced header, 141 // we do unlocking via runtime call 142 if (hdr_offset) { 143 lea(rscratch1, Address(obj, hdr_offset)); 144 cmpxchgptr(disp_hdr, hdr, rscratch1, rscratch2, done, &slow_case); 145 } else { 146 cmpxchgptr(disp_hdr, hdr, obj, rscratch2, done, &slow_case); 147 } 148 // done 149 bind(done); 150 } 151 152 153 // Defines obj, preserves var_size_in_bytes 154 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) { 155 if (UseTLAB) { 156 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case); 157 } else { 158 eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case); 159 } 160 } 161 162 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) { 163 assert_different_registers(obj, klass, len); 164 // This assumes that all prototype bits fit in an int32_t 165 mov(t1, (int32_t)(intptr_t)markWord::prototype().value()); 166 str(t1, Address(obj, oopDesc::mark_offset_in_bytes())); 167 168 if (UseCompressedClassPointers) { // Take care not to kill klass 169 encode_klass_not_null(t1, klass); 170 strw(t1, Address(obj, oopDesc::klass_offset_in_bytes())); 171 } else { 172 str(klass, Address(obj, oopDesc::klass_offset_in_bytes())); 173 } 174 175 if (len->is_valid()) { 176 strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes())); 177 } else if (UseCompressedClassPointers) { 178 store_klass_gap(obj, zr); 179 } 180 } 181 182 // preserves obj, destroys len_in_bytes 183 // 184 // Scratch registers: t1 = r10, t2 = r11 185 // 186 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1, Register t2) { 187 assert(hdr_size_in_bytes >= 0, "header size must be positive or 0"); 188 assert(t1 == r10 && t2 == r11, "must be"); 189 190 Label done; 191 192 // len_in_bytes is positive and ptr sized 193 subs(len_in_bytes, len_in_bytes, hdr_size_in_bytes); 194 br(Assembler::EQ, done); 195 196 // zero_words() takes ptr in r10 and count in words in r11 197 mov(rscratch1, len_in_bytes); 198 lea(t1, Address(obj, hdr_size_in_bytes)); 199 lsr(t2, rscratch1, LogBytesPerWord); 200 zero_words(t1, t2); 201 202 bind(done); 203 } 204 205 206 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) { 207 assert_different_registers(obj, t1, t2); // XXX really? 208 assert(header_size >= 0 && object_size >= header_size, "illegal sizes"); 209 210 try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case); 211 212 initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2, UseTLAB); 213 } 214 215 // Scratch registers: t1 = r10, t2 = r11 216 void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, bool is_tlab_allocated) { 217 assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0, 218 "con_size_in_bytes is not multiple of alignment"); 219 const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize; 220 221 initialize_header(obj, klass, noreg, t1, t2); 222 223 if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) { 224 // clear rest of allocated space 225 const Register index = t2; 226 if (var_size_in_bytes != noreg) { 227 mov(index, var_size_in_bytes); 228 initialize_body(obj, index, hdr_size_in_bytes, t1, t2); 229 } else if (con_size_in_bytes > hdr_size_in_bytes) { 230 con_size_in_bytes -= hdr_size_in_bytes; 231 lea(t1, Address(obj, hdr_size_in_bytes)); 232 zero_words(t1, con_size_in_bytes / BytesPerWord); 233 } 234 } 235 236 membar(StoreStore); 237 238 if (CURRENT_ENV->dtrace_alloc_probes()) { 239 assert(obj == r0, "must be"); 240 far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id))); 241 } 242 243 verify_oop(obj); 244 } 245 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, int f, Register klass, Label& slow_case) { 246 assert_different_registers(obj, len, t1, t2, klass); 247 248 // determine alignment mask 249 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work"); 250 251 // check for negative or excessive length 252 mov(rscratch1, (int32_t)max_array_allocation_length); 253 cmp(len, rscratch1); 254 br(Assembler::HS, slow_case); 255 256 const Register arr_size = t2; // okay to be the same 257 // align object end 258 mov(arr_size, (int32_t)header_size * BytesPerWord + MinObjAlignmentInBytesMask); 259 add(arr_size, arr_size, len, ext::uxtw, f); 260 andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask); 261 262 try_allocate(obj, arr_size, 0, t1, t2, slow_case); 263 264 initialize_header(obj, klass, len, t1, t2); 265 266 // clear rest of allocated space 267 initialize_body(obj, arr_size, header_size * BytesPerWord, t1, t2); 268 269 membar(StoreStore); 270 271 if (CURRENT_ENV->dtrace_alloc_probes()) { 272 assert(obj == r0, "must be"); 273 far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id))); 274 } 275 276 verify_oop(obj); 277 } 278 279 280 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) { 281 verify_oop(receiver); 282 // explicit NULL check not needed since load from [klass_offset] causes a trap 283 // check against inline cache 284 assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check"); 285 286 cmp_klass(receiver, iCache, rscratch1); 287 } 288 289 290 void C1_MacroAssembler::build_frame(int framesize, int bang_size_in_bytes) { 291 assert(bang_size_in_bytes >= framesize, "stack bang size incorrect"); 292 // Make sure there is enough stack space for this method's activation. 293 // Note that we do this before creating a frame. 294 generate_stack_overflow_check(bang_size_in_bytes); 295 MacroAssembler::build_frame(framesize); 296 297 // Insert nmethod entry barrier into frame. 298 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler(); 299 bs->nmethod_entry_barrier(this); 300 } 301 302 void C1_MacroAssembler::remove_frame(int framesize) { 303 MacroAssembler::remove_frame(framesize); 304 } 305 306 307 void C1_MacroAssembler::verified_entry(bool breakAtEntry) { 308 // If we have to make this method not-entrant we'll overwrite its 309 // first instruction with a jump. For this action to be legal we 310 // must ensure that this first instruction is a B, BL, NOP, BKPT, 311 // SVC, HVC, or SMC. Make it a NOP. 312 nop(); 313 } 314 315 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) { 316 // rbp, + 0: link 317 // + 1: return address 318 // + 2: argument with offset 0 319 // + 3: argument with offset 1 320 // + 4: ... 321 322 ldr(reg, Address(rfp, (offset_in_words + 2) * BytesPerWord)); 323 } 324 325 #ifndef PRODUCT 326 327 void C1_MacroAssembler::verify_stack_oop(int stack_offset) { 328 if (!VerifyOops) return; 329 verify_oop_addr(Address(sp, stack_offset)); 330 } 331 332 void C1_MacroAssembler::verify_not_null_oop(Register r) { 333 if (!VerifyOops) return; 334 Label not_null; 335 cbnz(r, not_null); 336 stop("non-null oop required"); 337 bind(not_null); 338 verify_oop(r); 339 } 340 341 void C1_MacroAssembler::invalidate_registers(bool inv_r0, bool inv_r19, bool inv_r2, bool inv_r3, bool inv_r4, bool inv_r5) { 342 #ifdef ASSERT 343 static int nn; 344 if (inv_r0) mov(r0, 0xDEAD); 345 if (inv_r19) mov(r19, 0xDEAD); 346 if (inv_r2) mov(r2, nn++); 347 if (inv_r3) mov(r3, 0xDEAD); 348 if (inv_r4) mov(r4, 0xDEAD); 349 if (inv_r5) mov(r5, 0xDEAD); 350 #endif 351 } 352 #endif // ifndef PRODUCT