1 /* 2 * Copyright (c) 1999, 2025, 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 "c1/c1_MacroAssembler.hpp" 27 #include "c1/c1_Runtime1.hpp" 28 #include "gc/shared/barrierSetAssembler.hpp" 29 #include "gc/shared/collectedHeap.hpp" 30 #include "gc/shared/tlab_globals.hpp" 31 #include "interpreter/interpreter.hpp" 32 #include "oops/arrayOop.hpp" 33 #include "oops/markWord.hpp" 34 #include "runtime/basicLock.hpp" 35 #include "runtime/os.hpp" 36 #include "runtime/sharedRuntime.hpp" 37 #include "runtime/stubRoutines.hpp" 38 39 void C1_MacroAssembler::float_cmp(bool is_float, int unordered_result, 40 FloatRegister f0, FloatRegister f1, 41 Register result) 42 { 43 Label done; 44 if (is_float) { 45 fcmps(f0, f1); 46 } else { 47 fcmpd(f0, f1); 48 } 49 if (unordered_result < 0) { 50 // we want -1 for unordered or less than, 0 for equal and 1 for 51 // greater than. 52 cset(result, NE); // Not equal or unordered 53 cneg(result, result, LT); // Less than or unordered 54 } else { 55 // we want -1 for less than, 0 for equal and 1 for unordered or 56 // greater than. 57 cset(result, NE); // Not equal or unordered 58 cneg(result, result, LO); // Less than 59 } 60 } 61 62 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register temp, Label& slow_case) { 63 const int aligned_mask = BytesPerWord -1; 64 const int hdr_offset = oopDesc::mark_offset_in_bytes(); 65 assert_different_registers(hdr, obj, disp_hdr, temp, rscratch2); 66 int null_check_offset = -1; 67 68 verify_oop(obj); 69 70 // save object being locked into the BasicObjectLock 71 str(obj, Address(disp_hdr, BasicObjectLock::obj_offset())); 72 73 null_check_offset = offset(); 74 75 if (DiagnoseSyncOnValueBasedClasses != 0) { 76 load_klass(hdr, obj); 77 ldrb(hdr, Address(hdr, Klass::misc_flags_offset())); 78 tst(hdr, KlassFlags::_misc_is_value_based_class); 79 br(Assembler::NE, slow_case); 80 } 81 82 if (LockingMode == LM_LIGHTWEIGHT) { 83 lightweight_lock(disp_hdr, obj, hdr, temp, rscratch2, slow_case); 84 } else if (LockingMode == LM_LEGACY) { 85 Label done; 86 // Load object header 87 ldr(hdr, Address(obj, hdr_offset)); 88 // and mark it as unlocked 89 orr(hdr, hdr, markWord::unlocked_value); 90 // save unlocked object header into the displaced header location on the stack 91 str(hdr, Address(disp_hdr, 0)); 92 // test if object header is still the same (i.e. unlocked), and if so, store the 93 // displaced header address in the object header - if it is not the same, get the 94 // object header instead 95 lea(rscratch2, Address(obj, hdr_offset)); 96 cmpxchgptr(hdr, disp_hdr, rscratch2, rscratch1, done, /*fallthough*/nullptr); 97 // if the object header was the same, we're done 98 // if the object header was not the same, it is now in the hdr register 99 // => test if it is a stack pointer into the same stack (recursive locking), i.e.: 100 // 101 // 1) (hdr & aligned_mask) == 0 102 // 2) sp <= hdr 103 // 3) hdr <= sp + page_size 104 // 105 // these 3 tests can be done by evaluating the following expression: 106 // 107 // (hdr - sp) & (aligned_mask - page_size) 108 // 109 // assuming both the stack pointer and page_size have their least 110 // significant 2 bits cleared and page_size is a power of 2 111 mov(rscratch1, sp); 112 sub(hdr, hdr, rscratch1); 113 ands(hdr, hdr, aligned_mask - (int)os::vm_page_size()); 114 // for recursive locking, the result is zero => save it in the displaced header 115 // location (null in the displaced hdr location indicates recursive locking) 116 str(hdr, Address(disp_hdr, 0)); 117 // otherwise we don't care about the result and handle locking via runtime call 118 cbnz(hdr, slow_case); 119 // done 120 bind(done); 121 inc_held_monitor_count(rscratch1); 122 } 123 return null_check_offset; 124 } 125 126 127 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Register temp, Label& slow_case) { 128 const int aligned_mask = BytesPerWord -1; 129 const int hdr_offset = oopDesc::mark_offset_in_bytes(); 130 assert_different_registers(hdr, obj, disp_hdr, temp, rscratch2); 131 Label done; 132 133 if (LockingMode != LM_LIGHTWEIGHT) { 134 // load displaced header 135 ldr(hdr, Address(disp_hdr, 0)); 136 // if the loaded hdr is null we had recursive locking 137 // if we had recursive locking, we are done 138 cbz(hdr, done); 139 } 140 141 // load object 142 ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset())); 143 verify_oop(obj); 144 145 if (LockingMode == LM_LIGHTWEIGHT) { 146 lightweight_unlock(obj, hdr, temp, rscratch2, slow_case); 147 } else if (LockingMode == LM_LEGACY) { 148 // test if object header is pointing to the displaced header, and if so, restore 149 // the displaced header in the object - if the object header is not pointing to 150 // the displaced header, get the object header instead 151 // if the object header was not pointing to the displaced header, 152 // we do unlocking via runtime call 153 if (hdr_offset) { 154 lea(rscratch1, Address(obj, hdr_offset)); 155 cmpxchgptr(disp_hdr, hdr, rscratch1, rscratch2, done, &slow_case); 156 } else { 157 cmpxchgptr(disp_hdr, hdr, obj, rscratch2, done, &slow_case); 158 } 159 // done 160 bind(done); 161 dec_held_monitor_count(rscratch1); 162 } 163 } 164 165 166 // Defines obj, preserves var_size_in_bytes 167 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) { 168 if (UseTLAB) { 169 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case); 170 } else { 171 b(slow_case); 172 } 173 } 174 175 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) { 176 assert_different_registers(obj, klass, len); 177 178 if (UseCompactObjectHeaders) { 179 ldr(t1, Address(klass, Klass::prototype_header_offset())); 180 str(t1, Address(obj, oopDesc::mark_offset_in_bytes())); 181 } else { 182 mov(t1, checked_cast<int32_t>(markWord::prototype().value())); 183 str(t1, Address(obj, oopDesc::mark_offset_in_bytes())); 184 if (UseCompressedClassPointers) { // Take care not to kill klass 185 encode_klass_not_null(t1, klass); 186 strw(t1, Address(obj, oopDesc::klass_offset_in_bytes())); 187 } else { 188 str(klass, Address(obj, oopDesc::klass_offset_in_bytes())); 189 } 190 } 191 192 if (len->is_valid()) { 193 strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes())); 194 int base_offset = arrayOopDesc::length_offset_in_bytes() + BytesPerInt; 195 if (!is_aligned(base_offset, BytesPerWord)) { 196 assert(is_aligned(base_offset, BytesPerInt), "must be 4-byte aligned"); 197 // Clear gap/first 4 bytes following the length field. 198 strw(zr, Address(obj, base_offset)); 199 } 200 } else if (UseCompressedClassPointers && !UseCompactObjectHeaders) { 201 store_klass_gap(obj, zr); 202 } 203 } 204 205 // preserves obj, destroys len_in_bytes 206 // 207 // Scratch registers: t1 = r10, t2 = r11 208 // 209 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1, Register t2) { 210 assert(hdr_size_in_bytes >= 0, "header size must be positive or 0"); 211 assert(t1 == r10 && t2 == r11, "must be"); 212 213 Label done; 214 215 // len_in_bytes is positive and ptr sized 216 subs(len_in_bytes, len_in_bytes, hdr_size_in_bytes); 217 br(Assembler::EQ, done); 218 219 // zero_words() takes ptr in r10 and count in words in r11 220 mov(rscratch1, len_in_bytes); 221 lea(t1, Address(obj, hdr_size_in_bytes)); 222 lsr(t2, rscratch1, LogBytesPerWord); 223 address tpc = zero_words(t1, t2); 224 225 bind(done); 226 if (tpc == nullptr) { 227 Compilation::current()->bailout("no space for trampoline stub"); 228 } 229 } 230 231 232 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) { 233 assert_different_registers(obj, t1, t2); // XXX really? 234 assert(header_size >= 0 && object_size >= header_size, "illegal sizes"); 235 236 try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case); 237 238 initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2, UseTLAB); 239 } 240 241 // Scratch registers: t1 = r10, t2 = r11 242 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) { 243 assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0, 244 "con_size_in_bytes is not multiple of alignment"); 245 const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize; 246 247 initialize_header(obj, klass, noreg, t1, t2); 248 249 if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) { 250 // clear rest of allocated space 251 const Register index = t2; 252 if (var_size_in_bytes != noreg) { 253 mov(index, var_size_in_bytes); 254 initialize_body(obj, index, hdr_size_in_bytes, t1, t2); 255 if (Compilation::current()->bailed_out()) { 256 return; 257 } 258 } else if (con_size_in_bytes > hdr_size_in_bytes) { 259 con_size_in_bytes -= hdr_size_in_bytes; 260 lea(t1, Address(obj, hdr_size_in_bytes)); 261 address tpc = zero_words(t1, con_size_in_bytes / BytesPerWord); 262 if (tpc == nullptr) { 263 Compilation::current()->bailout("no space for trampoline stub"); 264 return; 265 } 266 } 267 } 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(C1StubId::dtrace_object_alloc_id))); 274 } 275 276 verify_oop(obj); 277 } 278 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int base_offset_in_bytes, int f, Register klass, Label& slow_case, bool zero_array) { 279 assert_different_registers(obj, len, t1, t2, klass); 280 281 // determine alignment mask 282 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work"); 283 284 // check for negative or excessive length 285 mov(rscratch1, (int32_t)max_array_allocation_length); 286 cmp(len, rscratch1); 287 br(Assembler::HS, slow_case); 288 289 const Register arr_size = t2; // okay to be the same 290 // align object end 291 mov(arr_size, (int32_t)base_offset_in_bytes + MinObjAlignmentInBytesMask); 292 add(arr_size, arr_size, len, ext::uxtw, f); 293 andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask); 294 295 try_allocate(obj, arr_size, 0, t1, t2, slow_case); 296 297 initialize_header(obj, klass, len, t1, t2); 298 299 // Align-up to word boundary, because we clear the 4 bytes potentially 300 // following the length field in initialize_header(). 301 int base_offset = align_up(base_offset_in_bytes, BytesPerWord); 302 // clear rest of allocated space 303 if (zero_array) { 304 initialize_body(obj, arr_size, base_offset, t1, t2); 305 } 306 if (Compilation::current()->bailed_out()) { 307 return; 308 } 309 310 membar(StoreStore); 311 312 if (CURRENT_ENV->dtrace_alloc_probes()) { 313 assert(obj == r0, "must be"); 314 far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::dtrace_object_alloc_id))); 315 } 316 317 verify_oop(obj); 318 } 319 320 void C1_MacroAssembler::build_frame(int framesize, int bang_size_in_bytes) { 321 assert(bang_size_in_bytes >= framesize, "stack bang size incorrect"); 322 // Make sure there is enough stack space for this method's activation. 323 // Note that we do this before creating a frame. 324 generate_stack_overflow_check(bang_size_in_bytes); 325 MacroAssembler::build_frame(framesize); 326 327 // Insert nmethod entry barrier into frame. 328 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler(); 329 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */, nullptr /* guard */); 330 } 331 332 void C1_MacroAssembler::remove_frame(int framesize) { 333 MacroAssembler::remove_frame(framesize); 334 } 335 336 337 void C1_MacroAssembler::verified_entry(bool breakAtEntry) { 338 // If we have to make this method not-entrant we'll overwrite its 339 // first instruction with a jump. For this action to be legal we 340 // must ensure that this first instruction is a B, BL, NOP, BKPT, 341 // SVC, HVC, or SMC. Make it a NOP. 342 nop(); 343 } 344 345 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) { 346 // rfp, + 0: link 347 // + 1: return address 348 // + 2: argument with offset 0 349 // + 3: argument with offset 1 350 // + 4: ... 351 352 ldr(reg, Address(rfp, (offset_in_words + 2) * BytesPerWord)); 353 } 354 355 #ifndef PRODUCT 356 357 void C1_MacroAssembler::verify_stack_oop(int stack_offset) { 358 if (!VerifyOops) return; 359 verify_oop_addr(Address(sp, stack_offset)); 360 } 361 362 void C1_MacroAssembler::verify_not_null_oop(Register r) { 363 if (!VerifyOops) return; 364 Label not_null; 365 cbnz(r, not_null); 366 stop("non-null oop required"); 367 bind(not_null); 368 verify_oop(r); 369 } 370 371 void C1_MacroAssembler::invalidate_registers(bool inv_r0, bool inv_r19, bool inv_r2, bool inv_r3, bool inv_r4, bool inv_r5) { 372 #ifdef ASSERT 373 static int nn; 374 if (inv_r0) mov(r0, 0xDEAD); 375 if (inv_r19) mov(r19, 0xDEAD); 376 if (inv_r2) mov(r2, nn++); 377 if (inv_r3) mov(r3, 0xDEAD); 378 if (inv_r4) mov(r4, 0xDEAD); 379 if (inv_r5) mov(r5, 0xDEAD); 380 #endif 381 } 382 #endif // ifndef PRODUCT