1 /* 2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2016 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 "c1/c1_CodeStubs.hpp" 28 #include "c1/c1_FrameMap.hpp" 29 #include "c1/c1_LIRAssembler.hpp" 30 #include "c1/c1_MacroAssembler.hpp" 31 #include "c1/c1_Runtime1.hpp" 32 #include "nativeInst_s390.hpp" 33 #include "runtime/sharedRuntime.hpp" 34 #include "utilities/align.hpp" 35 #include "utilities/macros.hpp" 36 #include "vmreg_s390.inline.hpp" 37 38 #define __ ce->masm()-> 39 #undef CHECK_BAILOUT 40 #define CHECK_BAILOUT() { if (ce->compilation()->bailed_out()) return; } 41 42 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, 43 bool throw_index_out_of_bounds_exception) : 44 _throw_index_out_of_bounds_exception(throw_index_out_of_bounds_exception), 45 _index(index) { 46 assert(info != NULL, "must have info"); 47 _info = new CodeEmitInfo(info); 48 } 49 50 void RangeCheckStub::emit_code(LIR_Assembler* ce) { 51 __ bind(_entry); 52 if (_info->deoptimize_on_exception()) { 53 address a = Runtime1::entry_for (Runtime1::predicate_failed_trap_id); 54 ce->emit_call_c(a); 55 CHECK_BAILOUT(); 56 ce->add_call_info_here(_info); 57 ce->verify_oop_map(_info); 58 debug_only(__ should_not_reach_here()); 59 return; 60 } 61 62 // Pass the array index in Z_R1_scratch which is not managed by linear scan. 63 if (_index->is_cpu_register()) { 64 __ lgr_if_needed(Z_R1_scratch, _index->as_register()); 65 } else { 66 __ load_const_optimized(Z_R1_scratch, _index->as_jint()); 67 } 68 69 Runtime1::StubID stub_id; 70 if (_throw_index_out_of_bounds_exception) { 71 stub_id = Runtime1::throw_index_exception_id; 72 } else { 73 stub_id = Runtime1::throw_range_check_failed_id; 74 } 75 ce->emit_call_c(Runtime1::entry_for (stub_id)); 76 CHECK_BAILOUT(); 77 ce->add_call_info_here(_info); 78 ce->verify_oop_map(_info); 79 debug_only(__ should_not_reach_here()); 80 } 81 82 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) { 83 _info = new CodeEmitInfo(info); 84 } 85 86 void PredicateFailedStub::emit_code(LIR_Assembler* ce) { 87 __ bind(_entry); 88 address a = Runtime1::entry_for (Runtime1::predicate_failed_trap_id); 89 ce->emit_call_c(a); 90 CHECK_BAILOUT(); 91 ce->add_call_info_here(_info); 92 ce->verify_oop_map(_info); 93 debug_only(__ should_not_reach_here()); 94 } 95 96 void CounterOverflowStub::emit_code(LIR_Assembler* ce) { 97 __ bind(_entry); 98 Metadata *m = _method->as_constant_ptr()->as_metadata(); 99 bool success = __ set_metadata_constant(m, Z_R1_scratch); 100 if (!success) { 101 ce->compilation()->bailout("const section overflow"); 102 return; 103 } 104 ce->store_parameter(/*_method->as_register()*/ Z_R1_scratch, 1); 105 ce->store_parameter(_bci, 0); 106 ce->emit_call_c(Runtime1::entry_for (Runtime1::counter_overflow_id)); 107 CHECK_BAILOUT(); 108 ce->add_call_info_here(_info); 109 ce->verify_oop_map(_info); 110 __ branch_optimized(Assembler::bcondAlways, _continuation); 111 } 112 113 void DivByZeroStub::emit_code(LIR_Assembler* ce) { 114 if (_offset != -1) { 115 ce->compilation()->implicit_exception_table()->append(_offset, __ offset()); 116 } 117 __ bind(_entry); 118 ce->emit_call_c(Runtime1::entry_for (Runtime1::throw_div0_exception_id)); 119 CHECK_BAILOUT(); 120 ce->add_call_info_here(_info); 121 debug_only(__ should_not_reach_here()); 122 } 123 124 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) { 125 address a; 126 if (_info->deoptimize_on_exception()) { 127 // Deoptimize, do not throw the exception, because it is probably wrong to do it here. 128 a = Runtime1::entry_for (Runtime1::predicate_failed_trap_id); 129 } else { 130 a = Runtime1::entry_for (Runtime1::throw_null_pointer_exception_id); 131 } 132 133 ce->compilation()->implicit_exception_table()->append(_offset, __ offset()); 134 __ bind(_entry); 135 ce->emit_call_c(a); 136 CHECK_BAILOUT(); 137 ce->add_call_info_here(_info); 138 ce->verify_oop_map(_info); 139 debug_only(__ should_not_reach_here()); 140 } 141 142 // Note: pass object in Z_R1_scratch 143 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) { 144 __ bind(_entry); 145 if (_obj->is_valid()) { 146 __ z_lgr(Z_R1_scratch, _obj->as_register()); // _obj contains the optional argument to the stub 147 } 148 address a = Runtime1::entry_for (_stub); 149 ce->emit_call_c(a); 150 CHECK_BAILOUT(); 151 ce->add_call_info_here(_info); 152 debug_only(__ should_not_reach_here()); 153 } 154 155 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) { 156 _result = result; 157 _klass = klass; 158 _klass_reg = klass_reg; 159 _info = new CodeEmitInfo(info); 160 assert(stub_id == Runtime1::new_instance_id || 161 stub_id == Runtime1::fast_new_instance_id || 162 stub_id == Runtime1::fast_new_instance_init_check_id, 163 "need new_instance id"); 164 _stub_id = stub_id; 165 } 166 167 void NewInstanceStub::emit_code(LIR_Assembler* ce) { 168 __ bind(_entry); 169 assert(_klass_reg->as_register() == Z_R11, "call target expects klass in Z_R11"); 170 address a = Runtime1::entry_for (_stub_id); 171 ce->emit_call_c(a); 172 CHECK_BAILOUT(); 173 ce->add_call_info_here(_info); 174 ce->verify_oop_map(_info); 175 assert(_result->as_register() == Z_R2, "callee returns result in Z_R2,"); 176 __ z_brul(_continuation); 177 } 178 179 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) { 180 _klass_reg = klass_reg; 181 _length = length; 182 _result = result; 183 _info = new CodeEmitInfo(info); 184 } 185 186 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) { 187 __ bind(_entry); 188 assert(_klass_reg->as_register() == Z_R11, "call target expects klass in Z_R11"); 189 __ lgr_if_needed(Z_R13, _length->as_register()); 190 address a = Runtime1::entry_for (Runtime1::new_type_array_id); 191 ce->emit_call_c(a); 192 CHECK_BAILOUT(); 193 ce->add_call_info_here(_info); 194 ce->verify_oop_map(_info); 195 assert(_result->as_register() == Z_R2, "callee returns result in Z_R2,"); 196 __ z_brul(_continuation); 197 } 198 199 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) { 200 _klass_reg = klass_reg; 201 _length = length; 202 _result = result; 203 _info = new CodeEmitInfo(info); 204 } 205 206 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) { 207 __ bind(_entry); 208 assert(_klass_reg->as_register() == Z_R11, "call target expects klass in Z_R11"); 209 __ lgr_if_needed(Z_R13, _length->as_register()); 210 address a = Runtime1::entry_for (Runtime1::new_object_array_id); 211 ce->emit_call_c(a); 212 CHECK_BAILOUT(); 213 ce->add_call_info_here(_info); 214 ce->verify_oop_map(_info); 215 assert(_result->as_register() == Z_R2, "callee returns result in Z_R2,"); 216 __ z_brul(_continuation); 217 } 218 219 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info) 220 : MonitorAccessStub(obj_reg, lock_reg) { 221 _info = new CodeEmitInfo(info); 222 } 223 224 void MonitorEnterStub::emit_code(LIR_Assembler* ce) { 225 __ bind(_entry); 226 Runtime1::StubID enter_id; 227 if (ce->compilation()->has_fpu_code()) { 228 enter_id = Runtime1::monitorenter_id; 229 } else { 230 enter_id = Runtime1::monitorenter_nofpu_id; 231 } 232 __ lgr_if_needed(Z_R1_scratch, _obj_reg->as_register()); 233 __ lgr_if_needed(Z_R13, _lock_reg->as_register()); // See LIRGenerator::syncTempOpr(). 234 ce->emit_call_c(Runtime1::entry_for (enter_id)); 235 CHECK_BAILOUT(); 236 ce->add_call_info_here(_info); 237 ce->verify_oop_map(_info); 238 __ branch_optimized(Assembler::bcondAlways, _continuation); 239 } 240 241 void MonitorExitStub::emit_code(LIR_Assembler* ce) { 242 __ bind(_entry); 243 // Move address of the BasicObjectLock into Z_R1_scratch. 244 if (_compute_lock) { 245 // Lock_reg was destroyed by fast unlocking attempt => recompute it. 246 ce->monitor_address(_monitor_ix, FrameMap::as_opr(Z_R1_scratch)); 247 } else { 248 __ lgr_if_needed(Z_R1_scratch, _lock_reg->as_register()); 249 } 250 // Note: non-blocking leaf routine => no call info needed. 251 Runtime1::StubID exit_id; 252 if (ce->compilation()->has_fpu_code()) { 253 exit_id = Runtime1::monitorexit_id; 254 } else { 255 exit_id = Runtime1::monitorexit_nofpu_id; 256 } 257 ce->emit_call_c(Runtime1::entry_for (exit_id)); 258 CHECK_BAILOUT(); 259 __ branch_optimized(Assembler::bcondAlways, _continuation); 260 } 261 262 // Implementation of patching: 263 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes). 264 // - Replace original code with a call to the stub. 265 // At Runtime: 266 // - call to stub, jump to runtime. 267 // - in runtime: Preserve all registers (especially objects, i.e., source and destination object). 268 // - in runtime: After initializing class, restore original code, reexecute instruction. 269 270 int PatchingStub::_patch_info_offset = - (12 /* load const */ + 2 /*BASR*/); 271 272 void PatchingStub::align_patch_site(MacroAssembler* masm) { 273 #ifndef PRODUCT 274 const char* bc; 275 switch (_id) { 276 case access_field_id: bc = "patch site (access_field)"; break; 277 case load_klass_id: bc = "patch site (load_klass)"; break; 278 case load_mirror_id: bc = "patch site (load_mirror)"; break; 279 case load_appendix_id: bc = "patch site (load_appendix)"; break; 280 default: bc = "patch site (unknown patch id)"; break; 281 } 282 masm->block_comment(bc); 283 #endif 284 285 masm->align(align_up((int)NativeGeneralJump::instruction_size, wordSize)); 286 } 287 288 void PatchingStub::emit_code(LIR_Assembler* ce) { 289 // Copy original code here. 290 assert(NativeGeneralJump::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF, 291 "not enough room for call"); 292 293 NearLabel call_patch; 294 295 int being_initialized_entry = __ offset(); 296 297 if (_id == load_klass_id) { 298 // Produce a copy of the load klass instruction for use by the case being initialized. 299 #ifdef ASSERT 300 address start = __ pc(); 301 #endif 302 AddressLiteral addrlit((intptr_t)0, metadata_Relocation::spec(_index)); 303 __ load_const(_obj, addrlit); 304 305 #ifdef ASSERT 306 for (int i = 0; i < _bytes_to_copy; i++) { 307 address ptr = (address)(_pc_start + i); 308 int a_byte = (*ptr) & 0xFF; 309 assert(a_byte == *start++, "should be the same code"); 310 } 311 #endif 312 } else if (_id == load_mirror_id || _id == load_appendix_id) { 313 // Produce a copy of the load mirror instruction for use by the case being initialized. 314 #ifdef ASSERT 315 address start = __ pc(); 316 #endif 317 AddressLiteral addrlit((intptr_t)0, oop_Relocation::spec(_index)); 318 __ load_const(_obj, addrlit); 319 320 #ifdef ASSERT 321 for (int i = 0; i < _bytes_to_copy; i++) { 322 address ptr = (address)(_pc_start + i); 323 int a_byte = (*ptr) & 0xFF; 324 assert(a_byte == *start++, "should be the same code"); 325 } 326 #endif 327 } else { 328 // Make a copy the code which is going to be patched. 329 for (int i = 0; i < _bytes_to_copy; i++) { 330 address ptr = (address)(_pc_start + i); 331 int a_byte = (*ptr) & 0xFF; 332 __ emit_int8 (a_byte); 333 } 334 } 335 336 address end_of_patch = __ pc(); 337 int bytes_to_skip = 0; 338 if (_id == load_mirror_id) { 339 int offset = __ offset(); 340 if (CommentedAssembly) { 341 __ block_comment(" being_initialized check"); 342 } 343 344 // Static field accesses have special semantics while the class 345 // initializer is being run, so we emit a test which can be used to 346 // check that this code is being executed by the initializing 347 // thread. 348 assert(_obj != noreg, "must be a valid register"); 349 assert(_index >= 0, "must have oop index"); 350 __ z_lg(Z_R1_scratch, java_lang_Class::klass_offset_in_bytes(), _obj); 351 __ z_cg(Z_thread, Address(Z_R1_scratch, InstanceKlass::init_thread_offset())); 352 __ branch_optimized(Assembler::bcondNotEqual, call_patch); 353 354 // Load_klass patches may execute the patched code before it's 355 // copied back into place so we need to jump back into the main 356 // code of the nmethod to continue execution. 357 __ branch_optimized(Assembler::bcondAlways, _patch_site_continuation); 358 359 // Make sure this extra code gets skipped. 360 bytes_to_skip += __ offset() - offset; 361 } 362 363 // Now emit the patch record telling the runtime how to find the 364 // pieces of the patch. We only need 3 bytes but to help the disassembler 365 // we make the data look like a the following add instruction: 366 // A R1, D2(X2, B2) 367 // which requires 4 bytes. 368 int sizeof_patch_record = 4; 369 bytes_to_skip += sizeof_patch_record; 370 371 // Emit the offsets needed to find the code to patch. 372 int being_initialized_entry_offset = __ offset() - being_initialized_entry + sizeof_patch_record; 373 374 // Emit the patch record: opcode of the add followed by 3 bytes patch record data. 375 __ emit_int8((int8_t)(A_ZOPC>>24)); 376 __ emit_int8(being_initialized_entry_offset); 377 __ emit_int8(bytes_to_skip); 378 __ emit_int8(_bytes_to_copy); 379 address patch_info_pc = __ pc(); 380 assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info"); 381 382 address entry = __ pc(); 383 NativeGeneralJump::insert_unconditional((address)_pc_start, entry); 384 address target = NULL; 385 relocInfo::relocType reloc_type = relocInfo::none; 386 switch (_id) { 387 case access_field_id: target = Runtime1::entry_for (Runtime1::access_field_patching_id); break; 388 case load_klass_id: target = Runtime1::entry_for (Runtime1::load_klass_patching_id); reloc_type = relocInfo::metadata_type; break; 389 case load_mirror_id: target = Runtime1::entry_for (Runtime1::load_mirror_patching_id); reloc_type = relocInfo::oop_type; break; 390 case load_appendix_id: target = Runtime1::entry_for (Runtime1::load_appendix_patching_id); reloc_type = relocInfo::oop_type; break; 391 default: ShouldNotReachHere(); 392 } 393 __ bind(call_patch); 394 395 if (CommentedAssembly) { 396 __ block_comment("patch entry point"); 397 } 398 // Cannot use call_c_opt() because its size is not constant. 399 __ load_const(Z_R1_scratch, target); // Must not optimize in order to keep constant _patch_info_offset constant. 400 __ z_basr(Z_R14, Z_R1_scratch); 401 assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change"); 402 ce->add_call_info_here(_info); 403 __ z_brcl(Assembler::bcondAlways, _patch_site_entry); 404 if (_id == load_klass_id || _id == load_mirror_id || _id == load_appendix_id) { 405 CodeSection* cs = __ code_section(); 406 address pc = (address)_pc_start; 407 RelocIterator iter(cs, pc, pc + 1); 408 relocInfo::change_reloc_info_for_address(&iter, (address) pc, reloc_type, relocInfo::none); 409 } 410 } 411 412 void DeoptimizeStub::emit_code(LIR_Assembler* ce) { 413 __ bind(_entry); 414 __ load_const_optimized(Z_R1_scratch, _trap_request); // Pass trap request in Z_R1_scratch. 415 ce->emit_call_c(Runtime1::entry_for (Runtime1::deoptimize_id)); 416 CHECK_BAILOUT(); 417 ce->add_call_info_here(_info); 418 DEBUG_ONLY(__ should_not_reach_here()); 419 } 420 421 void ArrayCopyStub::emit_code(LIR_Assembler* ce) { 422 // Slow case: call to native. 423 __ bind(_entry); 424 __ lgr_if_needed(Z_ARG1, src()->as_register()); 425 __ lgr_if_needed(Z_ARG2, src_pos()->as_register()); 426 __ lgr_if_needed(Z_ARG3, dst()->as_register()); 427 __ lgr_if_needed(Z_ARG4, dst_pos()->as_register()); 428 __ lgr_if_needed(Z_ARG5, length()->as_register()); 429 430 // Must align calls sites, otherwise they can't be updated atomically on MP hardware. 431 ce->align_call(lir_static_call); 432 433 assert((__ offset() + NativeCall::call_far_pcrelative_displacement_offset) % NativeCall::call_far_pcrelative_displacement_alignment == 0, 434 "must be aligned"); 435 436 ce->emit_static_call_stub(); 437 438 // Prepend each BRASL with a nop. 439 __ relocate(relocInfo::static_call_type); 440 __ z_nop(); 441 __ z_brasl(Z_R14, SharedRuntime::get_resolve_static_call_stub()); 442 ce->add_call_info_here(info()); 443 ce->verify_oop_map(info()); 444 445 #ifndef PRODUCT 446 __ load_const_optimized(Z_R1_scratch, (address)&Runtime1::_arraycopy_slowcase_cnt); 447 __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch); 448 #endif 449 450 __ branch_optimized(Assembler::bcondAlways, _continuation); 451 } 452 453 #undef __