1 /* 2 * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2012, 2024 SAP SE. 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 "asm/macroAssembler.inline.hpp" 28 #include "c1/c1_MacroAssembler.hpp" 29 #include "c1/c1_Runtime1.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 #include "utilities/align.hpp" 40 #include "utilities/macros.hpp" 41 #include "utilities/powerOfTwo.hpp" 42 43 44 void C1_MacroAssembler::explicit_null_check(Register base) { 45 Unimplemented(); 46 } 47 48 49 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) { 50 // Avoid stack bang as first instruction. It may get overwritten by patch_verified_entry. 51 const Register return_pc = R20; 52 mflr(return_pc); 53 54 // Make sure there is enough stack space for this method's activation. 55 assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect"); 56 generate_stack_overflow_check(bang_size_in_bytes); 57 58 std(return_pc, _abi0(lr), R1_SP); // SP->lr = return_pc 59 push_frame(frame_size_in_bytes, R0); // SP -= frame_size_in_bytes 60 61 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler(); 62 bs->nmethod_entry_barrier(this, R20); 63 } 64 65 66 void C1_MacroAssembler::verified_entry(bool breakAtEntry) { 67 if (breakAtEntry) illtrap(); 68 // build frame 69 } 70 71 72 void C1_MacroAssembler::lock_object(Register Rmark, Register Roop, Register Rbox, Register Rscratch, Label& slow_case) { 73 assert_different_registers(Rmark, Roop, Rbox, Rscratch); 74 75 Label done, cas_failed, slow_int; 76 77 // The following move must be the first instruction of emitted since debug 78 // information may be generated for it. 79 // Load object header. 80 ld(Rmark, oopDesc::mark_offset_in_bytes(), Roop); 81 82 verify_oop(Roop, FILE_AND_LINE); 83 84 // Save object being locked into the BasicObjectLock... 85 std(Roop, in_bytes(BasicObjectLock::obj_offset()), Rbox); 86 87 if (DiagnoseSyncOnValueBasedClasses != 0) { 88 load_klass(Rscratch, Roop); 89 lbz(Rscratch, in_bytes(Klass::misc_flags_offset()), Rscratch); 90 testbitdi(CCR0, R0, Rscratch, exact_log2(KlassFlags::_misc_is_value_based_class)); 91 bne(CCR0, slow_int); 92 } 93 94 if (LockingMode == LM_LIGHTWEIGHT) { 95 lightweight_lock(Rbox, Roop, Rmark, Rscratch, slow_int); 96 } else if (LockingMode == LM_LEGACY) { 97 // ... and mark it unlocked. 98 ori(Rmark, Rmark, markWord::unlocked_value); 99 100 // Save unlocked object header into the displaced header location on the stack. 101 std(Rmark, BasicLock::displaced_header_offset_in_bytes(), Rbox); 102 103 // Compare object markWord with Rmark and if equal exchange Rscratch with object markWord. 104 assert(oopDesc::mark_offset_in_bytes() == 0, "cas must take a zero displacement"); 105 cmpxchgd(/*flag=*/CCR0, 106 /*current_value=*/Rscratch, 107 /*compare_value=*/Rmark, 108 /*exchange_value=*/Rbox, 109 /*where=*/Roop/*+0==mark_offset_in_bytes*/, 110 MacroAssembler::MemBarRel | MacroAssembler::MemBarAcq, 111 MacroAssembler::cmpxchgx_hint_acquire_lock(), 112 noreg, 113 &cas_failed, 114 /*check without membar and ldarx first*/true); 115 // If compare/exchange succeeded we found an unlocked object and we now have locked it 116 // hence we are done. 117 } else { 118 assert(false, "Unhandled LockingMode:%d", LockingMode); 119 } 120 b(done); 121 122 bind(slow_int); 123 b(slow_case); // far 124 125 if (LockingMode == LM_LEGACY) { 126 bind(cas_failed); 127 // We did not find an unlocked object so see if this is a recursive case. 128 sub(Rscratch, Rscratch, R1_SP); 129 load_const_optimized(R0, (~(os::vm_page_size()-1) | markWord::lock_mask_in_place)); 130 and_(R0/*==0?*/, Rscratch, R0); 131 std(R0/*==0, perhaps*/, BasicLock::displaced_header_offset_in_bytes(), Rbox); 132 bne(CCR0, slow_int); 133 } 134 135 bind(done); 136 inc_held_monitor_count(Rmark /*tmp*/); 137 } 138 139 140 void C1_MacroAssembler::unlock_object(Register Rmark, Register Roop, Register Rbox, Label& slow_case) { 141 assert_different_registers(Rmark, Roop, Rbox); 142 143 Label slow_int, done; 144 145 Address mark_addr(Roop, oopDesc::mark_offset_in_bytes()); 146 assert(mark_addr.disp() == 0, "cas must take a zero displacement"); 147 148 if (LockingMode != LM_LIGHTWEIGHT) { 149 // Test first if it is a fast recursive unlock. 150 ld(Rmark, BasicLock::displaced_header_offset_in_bytes(), Rbox); 151 cmpdi(CCR0, Rmark, 0); 152 beq(CCR0, done); 153 } 154 155 // Load object. 156 ld(Roop, in_bytes(BasicObjectLock::obj_offset()), Rbox); 157 verify_oop(Roop, FILE_AND_LINE); 158 159 if (LockingMode == LM_LIGHTWEIGHT) { 160 lightweight_unlock(Roop, Rmark, slow_int); 161 } else if (LockingMode == LM_LEGACY) { 162 // Check if it is still a light weight lock, this is is true if we see 163 // the stack address of the basicLock in the markWord of the object. 164 cmpxchgd(/*flag=*/CCR0, 165 /*current_value=*/R0, 166 /*compare_value=*/Rbox, 167 /*exchange_value=*/Rmark, 168 /*where=*/Roop, 169 MacroAssembler::MemBarRel, 170 MacroAssembler::cmpxchgx_hint_release_lock(), 171 noreg, 172 &slow_int); 173 } else { 174 assert(false, "Unhandled LockingMode:%d", LockingMode); 175 } 176 b(done); 177 bind(slow_int); 178 b(slow_case); // far 179 180 // Done 181 bind(done); 182 dec_held_monitor_count(Rmark /*tmp*/); 183 } 184 185 186 void C1_MacroAssembler::try_allocate( 187 Register obj, // result: pointer to object after successful allocation 188 Register var_size_in_bytes, // object size in bytes if unknown at compile time; invalid otherwise 189 int con_size_in_bytes, // object size in bytes if known at compile time 190 Register t1, // temp register 191 Register t2, // temp register 192 Label& slow_case // continuation point if fast allocation fails 193 ) { 194 if (UseTLAB) { 195 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case); 196 } else { 197 b(slow_case); 198 } 199 } 200 201 202 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) { 203 assert_different_registers(obj, klass, len, t1, t2); 204 load_const_optimized(t1, (intx)markWord::prototype().value()); 205 std(t1, oopDesc::mark_offset_in_bytes(), obj); 206 store_klass(obj, klass); 207 if (len->is_valid()) { 208 stw(len, arrayOopDesc::length_offset_in_bytes(), obj); 209 } else if (UseCompressedClassPointers) { 210 // Otherwise length is in the class gap. 211 store_klass_gap(obj); 212 } 213 } 214 215 216 void C1_MacroAssembler::initialize_body(Register base, Register index) { 217 assert_different_registers(base, index); 218 srdi(index, index, LogBytesPerWord); 219 clear_memory_doubleword(base, index); 220 } 221 222 void C1_MacroAssembler::initialize_body(Register obj, Register tmp1, Register tmp2, 223 int obj_size_in_bytes, int hdr_size_in_bytes) { 224 const int index = (obj_size_in_bytes - hdr_size_in_bytes) / HeapWordSize; 225 226 // 2x unrolled loop is shorter with more than 9 HeapWords. 227 if (index <= 9) { 228 clear_memory_unrolled(obj, index, R0, hdr_size_in_bytes); 229 } else { 230 const Register base_ptr = tmp1, 231 cnt_dwords = tmp2; 232 233 addi(base_ptr, obj, hdr_size_in_bytes); // Compute address of first element. 234 clear_memory_doubleword(base_ptr, cnt_dwords, R0, index); 235 } 236 } 237 238 void C1_MacroAssembler::allocate_object( 239 Register obj, // result: pointer to object after successful allocation 240 Register t1, // temp register 241 Register t2, // temp register 242 Register t3, // temp register 243 int hdr_size, // object header size in words 244 int obj_size, // object size in words 245 Register klass, // object klass 246 Label& slow_case // continuation point if fast allocation fails 247 ) { 248 assert_different_registers(obj, t1, t2, t3, klass); 249 250 // allocate space & initialize header 251 if (!is_simm16(obj_size * wordSize)) { 252 // Would need to use extra register to load 253 // object size => go the slow case for now. 254 b(slow_case); 255 return; 256 } 257 try_allocate(obj, noreg, obj_size * wordSize, t2, t3, slow_case); 258 259 initialize_object(obj, klass, noreg, obj_size * HeapWordSize, t1, t2); 260 } 261 262 void C1_MacroAssembler::initialize_object( 263 Register obj, // result: pointer to object after successful allocation 264 Register klass, // object klass 265 Register var_size_in_bytes, // object size in bytes if unknown at compile time; invalid otherwise 266 int con_size_in_bytes, // object size in bytes if known at compile time 267 Register t1, // temp register 268 Register t2 // temp register 269 ) { 270 const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize; 271 272 initialize_header(obj, klass, noreg, t1, t2); 273 274 #ifdef ASSERT 275 { 276 lwz(t1, in_bytes(Klass::layout_helper_offset()), klass); 277 if (var_size_in_bytes != noreg) { 278 cmpw(CCR0, t1, var_size_in_bytes); 279 } else { 280 cmpwi(CCR0, t1, con_size_in_bytes); 281 } 282 asm_assert_eq("bad size in initialize_object"); 283 } 284 #endif 285 286 // Initialize body. 287 if (var_size_in_bytes != noreg) { 288 // Use a loop. 289 addi(t1, obj, hdr_size_in_bytes); // Compute address of first element. 290 addi(t2, var_size_in_bytes, -hdr_size_in_bytes); // Compute size of body. 291 initialize_body(t1, t2); 292 } else if (con_size_in_bytes > hdr_size_in_bytes) { 293 // Use a loop. 294 initialize_body(obj, t1, t2, con_size_in_bytes, hdr_size_in_bytes); 295 } 296 297 if (CURRENT_ENV->dtrace_alloc_probes()) { 298 Unimplemented(); 299 // assert(obj == O0, "must be"); 300 // call(CAST_FROM_FN_PTR(address, Runtime1::entry_for(C1StubId::dtrace_object_alloc_id)), 301 // relocInfo::runtime_call_type); 302 } 303 304 verify_oop(obj, FILE_AND_LINE); 305 } 306 307 308 void C1_MacroAssembler::allocate_array( 309 Register obj, // result: pointer to array after successful allocation 310 Register len, // array length 311 Register t1, // temp register 312 Register t2, // temp register 313 Register t3, // temp register 314 int base_offset_in_bytes, // elements offset in bytes 315 int elt_size, // element size in bytes 316 Register klass, // object klass 317 Label& slow_case, // continuation point if fast allocation fails 318 bool zero_array // zero the allocated array or not 319 ) { 320 assert_different_registers(obj, len, t1, t2, t3, klass); 321 322 // Determine alignment mask. 323 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work"); 324 int log2_elt_size = exact_log2(elt_size); 325 326 // Check for negative or excessive length. 327 size_t max_length = max_array_allocation_length >> log2_elt_size; 328 if (UseTLAB) { 329 size_t max_tlab = align_up(ThreadLocalAllocBuffer::max_size() >> log2_elt_size, 64*K); 330 if (max_tlab < max_length) { max_length = max_tlab; } 331 } 332 load_const_optimized(t1, max_length); 333 cmpld(CCR0, len, t1); 334 bc_far_optimized(Assembler::bcondCRbiIs1, bi0(CCR0, Assembler::greater), slow_case); 335 336 // compute array size 337 // note: If 0 <= len <= max_length, len*elt_size + header + alignment is 338 // smaller or equal to the largest integer; also, since top is always 339 // aligned, we can do the alignment here instead of at the end address 340 // computation. 341 const Register arr_size = t1; 342 Register arr_len_in_bytes = len; 343 if (elt_size != 1) { 344 sldi(t1, len, log2_elt_size); 345 arr_len_in_bytes = t1; 346 } 347 addi(arr_size, arr_len_in_bytes, base_offset_in_bytes + MinObjAlignmentInBytesMask); // Add space for header & alignment. 348 clrrdi(arr_size, arr_size, LogMinObjAlignmentInBytes); // Align array size. 349 350 // Allocate space & initialize header. 351 try_allocate(obj, arr_size, 0, t2, t3, slow_case); 352 initialize_header(obj, klass, len, t2, t3); 353 354 if (zero_array) { 355 // Initialize body. 356 const Register base = t2; 357 const Register index = t3; 358 addi(base, obj, base_offset_in_bytes); // compute address of first element 359 addi(index, arr_size, -(base_offset_in_bytes)); // compute index = number of bytes to clear 360 361 // Zero first 4 bytes, if start offset is not word aligned. 362 if (!is_aligned(base_offset_in_bytes, BytesPerWord)) { 363 assert(is_aligned(base_offset_in_bytes, BytesPerInt), "must be 4-byte aligned"); 364 li(t1, 0); 365 stw(t1, 0, base); 366 addi(base, base, BytesPerInt); 367 // Note: initialize_body will align index down, no need to correct it here. 368 } 369 370 initialize_body(base, index); 371 } 372 373 if (CURRENT_ENV->dtrace_alloc_probes()) { 374 Unimplemented(); 375 //assert(obj == O0, "must be"); 376 //call(CAST_FROM_FN_PTR(address, Runtime1::entry_for(C1StubId::dtrace_object_alloc_id)), 377 // relocInfo::runtime_call_type); 378 } 379 380 verify_oop(obj, FILE_AND_LINE); 381 } 382 383 384 #ifndef PRODUCT 385 386 void C1_MacroAssembler::verify_stack_oop(int stack_offset) { 387 verify_oop_addr((RegisterOrConstant)stack_offset, R1_SP, "broken oop in stack slot"); 388 } 389 390 void C1_MacroAssembler::verify_not_null_oop(Register r) { 391 Label not_null; 392 cmpdi(CCR0, r, 0); 393 bne(CCR0, not_null); 394 stop("non-null oop required"); 395 bind(not_null); 396 verify_oop(r, FILE_AND_LINE); 397 } 398 399 #endif // PRODUCT 400 401 void C1_MacroAssembler::null_check(Register r, Label* Lnull) { 402 if (TrapBasedNullChecks) { // SIGTRAP based 403 trap_null_check(r); 404 } else { // explicit 405 //const address exception_entry = Runtime1::entry_for(C1StubId::throw_null_pointer_exception_id); 406 assert(Lnull != nullptr, "must have Label for explicit check"); 407 cmpdi(CCR0, r, 0); 408 bc_far_optimized(Assembler::bcondCRbiIs1, bi0(CCR0, Assembler::equal), *Lnull); 409 } 410 }