1 /* 2 * Copyright (c) 1999, 2019, 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 "asm/codeBuffer.hpp" 27 #include "c1/c1_CodeStubs.hpp" 28 #include "c1/c1_Defs.hpp" 29 #include "c1/c1_FrameMap.hpp" 30 #include "c1/c1_LIRAssembler.hpp" 31 #include "c1/c1_MacroAssembler.hpp" 32 #include "c1/c1_Runtime1.hpp" 33 #include "classfile/systemDictionary.hpp" 34 #include "classfile/vmSymbols.hpp" 35 #include "code/codeBlob.hpp" 36 #include "code/compiledIC.hpp" 37 #include "code/pcDesc.hpp" 38 #include "code/scopeDesc.hpp" 39 #include "code/vtableStubs.hpp" 40 #include "compiler/disassembler.hpp" 41 #include "gc/shared/barrierSet.hpp" 42 #include "gc/shared/c1/barrierSetC1.hpp" 43 #include "gc/shared/collectedHeap.hpp" 44 #include "interpreter/bytecode.hpp" 45 #include "interpreter/interpreter.hpp" 46 #include "jfr/support/jfrIntrinsics.hpp" 47 #include "logging/log.hpp" 48 #include "memory/allocation.inline.hpp" 49 #include "memory/oopFactory.hpp" 50 #include "memory/resourceArea.hpp" 51 #include "memory/universe.hpp" 52 #include "oops/access.inline.hpp" 53 #include "oops/objArrayOop.inline.hpp" 54 #include "oops/objArrayKlass.hpp" 55 #include "oops/oop.inline.hpp" 56 #include "runtime/atomic.hpp" 57 #include "runtime/biasedLocking.hpp" 58 #include "runtime/compilationPolicy.hpp" 59 #include "runtime/fieldDescriptor.inline.hpp" 60 #include "runtime/frame.inline.hpp" 61 #include "runtime/handles.inline.hpp" 62 #include "runtime/interfaceSupport.inline.hpp" 63 #include "runtime/javaCalls.hpp" 64 #include "runtime/sharedRuntime.hpp" 65 #include "runtime/threadCritical.hpp" 66 #include "runtime/vframe.inline.hpp" 67 #include "runtime/vframeArray.hpp" 68 #include "runtime/vm_version.hpp" 69 #include "utilities/copy.hpp" 70 #include "utilities/events.hpp" 71 72 73 // Implementation of StubAssembler 74 75 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) { 76 _name = name; 77 _must_gc_arguments = false; 78 _frame_size = no_frame_size; 79 _num_rt_args = 0; 80 _stub_id = stub_id; 81 } 82 83 84 void StubAssembler::set_info(const char* name, bool must_gc_arguments) { 85 _name = name; 86 _must_gc_arguments = must_gc_arguments; 87 } 88 89 90 void StubAssembler::set_frame_size(int size) { 91 if (_frame_size == no_frame_size) { 92 _frame_size = size; 93 } 94 assert(_frame_size == size, "can't change the frame size"); 95 } 96 97 98 void StubAssembler::set_num_rt_args(int args) { 99 if (_num_rt_args == 0) { 100 _num_rt_args = args; 101 } 102 assert(_num_rt_args == args, "can't change the number of args"); 103 } 104 105 // Implementation of Runtime1 106 107 CodeBlob* Runtime1::_blobs[Runtime1::number_of_ids]; 108 const char *Runtime1::_blob_names[] = { 109 RUNTIME1_STUBS(STUB_NAME, LAST_STUB_NAME) 110 }; 111 112 #ifndef PRODUCT 113 // statistics 114 int Runtime1::_generic_arraycopy_cnt = 0; 115 int Runtime1::_generic_arraycopystub_cnt = 0; 116 int Runtime1::_arraycopy_slowcase_cnt = 0; 117 int Runtime1::_arraycopy_checkcast_cnt = 0; 118 int Runtime1::_arraycopy_checkcast_attempt_cnt = 0; 119 int Runtime1::_new_type_array_slowcase_cnt = 0; 120 int Runtime1::_new_object_array_slowcase_cnt = 0; 121 int Runtime1::_new_instance_slowcase_cnt = 0; 122 int Runtime1::_new_multi_array_slowcase_cnt = 0; 123 int Runtime1::_monitorenter_slowcase_cnt = 0; 124 int Runtime1::_monitorexit_slowcase_cnt = 0; 125 int Runtime1::_patch_code_slowcase_cnt = 0; 126 int Runtime1::_throw_range_check_exception_count = 0; 127 int Runtime1::_throw_index_exception_count = 0; 128 int Runtime1::_throw_div0_exception_count = 0; 129 int Runtime1::_throw_null_pointer_exception_count = 0; 130 int Runtime1::_throw_class_cast_exception_count = 0; 131 int Runtime1::_throw_incompatible_class_change_error_count = 0; 132 int Runtime1::_throw_array_store_exception_count = 0; 133 int Runtime1::_throw_count = 0; 134 135 static int _byte_arraycopy_stub_cnt = 0; 136 static int _short_arraycopy_stub_cnt = 0; 137 static int _int_arraycopy_stub_cnt = 0; 138 static int _long_arraycopy_stub_cnt = 0; 139 static int _oop_arraycopy_stub_cnt = 0; 140 141 address Runtime1::arraycopy_count_address(BasicType type) { 142 switch (type) { 143 case T_BOOLEAN: 144 case T_BYTE: return (address)&_byte_arraycopy_stub_cnt; 145 case T_CHAR: 146 case T_SHORT: return (address)&_short_arraycopy_stub_cnt; 147 case T_FLOAT: 148 case T_INT: return (address)&_int_arraycopy_stub_cnt; 149 case T_DOUBLE: 150 case T_LONG: return (address)&_long_arraycopy_stub_cnt; 151 case T_ARRAY: 152 case T_OBJECT: return (address)&_oop_arraycopy_stub_cnt; 153 default: 154 ShouldNotReachHere(); 155 return NULL; 156 } 157 } 158 159 160 #endif 161 162 // Simple helper to see if the caller of a runtime stub which 163 // entered the VM has been deoptimized 164 165 static bool caller_is_deopted() { 166 JavaThread* thread = JavaThread::current(); 167 RegisterMap reg_map(thread, false); 168 frame runtime_frame = thread->last_frame(); 169 frame caller_frame = runtime_frame.sender(®_map); 170 assert(caller_frame.is_compiled_frame(), "must be compiled"); 171 return caller_frame.is_deoptimized_frame(); 172 } 173 174 // Stress deoptimization 175 static void deopt_caller() { 176 if ( !caller_is_deopted()) { 177 JavaThread* thread = JavaThread::current(); 178 RegisterMap reg_map(thread, false); 179 frame runtime_frame = thread->last_frame(); 180 frame caller_frame = runtime_frame.sender(®_map); 181 Deoptimization::deoptimize_frame(thread, caller_frame.id()); 182 assert(caller_is_deopted(), "Must be deoptimized"); 183 } 184 } 185 186 class StubIDStubAssemblerCodeGenClosure: public StubAssemblerCodeGenClosure { 187 private: 188 Runtime1::StubID _id; 189 public: 190 StubIDStubAssemblerCodeGenClosure(Runtime1::StubID id) : _id(id) {} 191 virtual OopMapSet* generate_code(StubAssembler* sasm) { 192 return Runtime1::generate_code_for(_id, sasm); 193 } 194 }; 195 196 CodeBlob* Runtime1::generate_blob(BufferBlob* buffer_blob, int stub_id, const char* name, bool expect_oop_map, StubAssemblerCodeGenClosure* cl) { 197 ResourceMark rm; 198 // create code buffer for code storage 199 CodeBuffer code(buffer_blob); 200 201 OopMapSet* oop_maps; 202 int frame_size; 203 bool must_gc_arguments; 204 205 Compilation::setup_code_buffer(&code, 0); 206 207 // create assembler for code generation 208 StubAssembler* sasm = new StubAssembler(&code, name, stub_id); 209 // generate code for runtime stub 210 oop_maps = cl->generate_code(sasm); 211 assert(oop_maps == NULL || sasm->frame_size() != no_frame_size, 212 "if stub has an oop map it must have a valid frame size"); 213 assert(!expect_oop_map || oop_maps != NULL, "must have an oopmap"); 214 215 // align so printing shows nop's instead of random code at the end (SimpleStubs are aligned) 216 sasm->align(BytesPerWord); 217 // make sure all code is in code buffer 218 sasm->flush(); 219 220 frame_size = sasm->frame_size(); 221 must_gc_arguments = sasm->must_gc_arguments(); 222 // create blob - distinguish a few special cases 223 CodeBlob* blob = RuntimeStub::new_runtime_stub(name, 224 &code, 225 CodeOffsets::frame_never_safe, 226 frame_size, 227 oop_maps, 228 must_gc_arguments); 229 assert(blob != NULL, "blob must exist"); 230 return blob; 231 } 232 233 void Runtime1::generate_blob_for(BufferBlob* buffer_blob, StubID id) { 234 assert(0 <= id && id < number_of_ids, "illegal stub id"); 235 bool expect_oop_map = true; 236 #ifdef ASSERT 237 // Make sure that stubs that need oopmaps have them 238 switch (id) { 239 // These stubs don't need to have an oopmap 240 case dtrace_object_alloc_id: 241 case slow_subtype_check_id: 242 case fpu2long_stub_id: 243 case unwind_exception_id: 244 case counter_overflow_id: 245 #if defined(SPARC) || defined(PPC32) 246 case handle_exception_nofpu_id: // Unused on sparc 247 #endif 248 expect_oop_map = false; 249 break; 250 default: 251 break; 252 } 253 #endif 254 StubIDStubAssemblerCodeGenClosure cl(id); 255 CodeBlob* blob = generate_blob(buffer_blob, id, name_for(id), expect_oop_map, &cl); 256 // install blob 257 _blobs[id] = blob; 258 } 259 260 void Runtime1::initialize(BufferBlob* blob) { 261 // platform-dependent initialization 262 initialize_pd(); 263 // generate stubs 264 for (int id = 0; id < number_of_ids; id++) generate_blob_for(blob, (StubID)id); 265 // printing 266 #ifndef PRODUCT 267 if (PrintSimpleStubs) { 268 ResourceMark rm; 269 for (int id = 0; id < number_of_ids; id++) { 270 _blobs[id]->print(); 271 if (_blobs[id]->oop_maps() != NULL) { 272 _blobs[id]->oop_maps()->print(); 273 } 274 } 275 } 276 #endif 277 BarrierSetC1* bs = BarrierSet::barrier_set()->barrier_set_c1(); 278 bs->generate_c1_runtime_stubs(blob); 279 } 280 281 CodeBlob* Runtime1::blob_for(StubID id) { 282 assert(0 <= id && id < number_of_ids, "illegal stub id"); 283 return _blobs[id]; 284 } 285 286 287 const char* Runtime1::name_for(StubID id) { 288 assert(0 <= id && id < number_of_ids, "illegal stub id"); 289 return _blob_names[id]; 290 } 291 292 const char* Runtime1::name_for_address(address entry) { 293 for (int id = 0; id < number_of_ids; id++) { 294 if (entry == entry_for((StubID)id)) return name_for((StubID)id); 295 } 296 297 BarrierSetC1* bsc1 = BarrierSet::barrier_set()->barrier_set_c1(); 298 const char* name = bsc1->rtcall_name_for_address(entry); 299 if (name != NULL) { 300 return name; 301 } 302 303 #define FUNCTION_CASE(a, f) \ 304 if ((intptr_t)a == CAST_FROM_FN_PTR(intptr_t, f)) return #f 305 306 FUNCTION_CASE(entry, os::javaTimeMillis); 307 FUNCTION_CASE(entry, os::javaTimeNanos); 308 FUNCTION_CASE(entry, SharedRuntime::OSR_migration_end); 309 FUNCTION_CASE(entry, SharedRuntime::d2f); 310 FUNCTION_CASE(entry, SharedRuntime::d2i); 311 FUNCTION_CASE(entry, SharedRuntime::d2l); 312 FUNCTION_CASE(entry, SharedRuntime::dcos); 313 FUNCTION_CASE(entry, SharedRuntime::dexp); 314 FUNCTION_CASE(entry, SharedRuntime::dlog); 315 FUNCTION_CASE(entry, SharedRuntime::dlog10); 316 FUNCTION_CASE(entry, SharedRuntime::dpow); 317 FUNCTION_CASE(entry, SharedRuntime::drem); 318 FUNCTION_CASE(entry, SharedRuntime::dsin); 319 FUNCTION_CASE(entry, SharedRuntime::dtan); 320 FUNCTION_CASE(entry, SharedRuntime::f2i); 321 FUNCTION_CASE(entry, SharedRuntime::f2l); 322 FUNCTION_CASE(entry, SharedRuntime::frem); 323 FUNCTION_CASE(entry, SharedRuntime::l2d); 324 FUNCTION_CASE(entry, SharedRuntime::l2f); 325 FUNCTION_CASE(entry, SharedRuntime::ldiv); 326 FUNCTION_CASE(entry, SharedRuntime::lmul); 327 FUNCTION_CASE(entry, SharedRuntime::lrem); 328 FUNCTION_CASE(entry, SharedRuntime::lrem); 329 FUNCTION_CASE(entry, SharedRuntime::dtrace_method_entry); 330 FUNCTION_CASE(entry, SharedRuntime::dtrace_method_exit); 331 FUNCTION_CASE(entry, is_instance_of); 332 FUNCTION_CASE(entry, trace_block_entry); 333 #ifdef JFR_HAVE_INTRINSICS 334 FUNCTION_CASE(entry, JFR_TIME_FUNCTION); 335 #endif 336 FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32()); 337 FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32C()); 338 FUNCTION_CASE(entry, StubRoutines::vectorizedMismatch()); 339 FUNCTION_CASE(entry, StubRoutines::dexp()); 340 FUNCTION_CASE(entry, StubRoutines::dlog()); 341 FUNCTION_CASE(entry, StubRoutines::dlog10()); 342 FUNCTION_CASE(entry, StubRoutines::dpow()); 343 FUNCTION_CASE(entry, StubRoutines::dsin()); 344 FUNCTION_CASE(entry, StubRoutines::dcos()); 345 FUNCTION_CASE(entry, StubRoutines::dtan()); 346 347 #undef FUNCTION_CASE 348 349 // Soft float adds more runtime names. 350 return pd_name_for_address(entry); 351 } 352 353 354 JRT_ENTRY(void, Runtime1::new_instance(JavaThread* thread, Klass* klass)) 355 NOT_PRODUCT(_new_instance_slowcase_cnt++;) 356 357 assert(klass->is_klass(), "not a class"); 358 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 359 InstanceKlass* h = InstanceKlass::cast(klass); 360 h->check_valid_for_instantiation(true, CHECK); 361 // make sure klass is initialized 362 h->initialize(CHECK); 363 // allocate instance and return via TLS 364 oop obj = h->allocate_instance(CHECK); 365 thread->set_vm_result(obj); 366 JRT_END 367 368 369 JRT_ENTRY(void, Runtime1::new_type_array(JavaThread* thread, Klass* klass, jint length)) 370 NOT_PRODUCT(_new_type_array_slowcase_cnt++;) 371 // Note: no handle for klass needed since they are not used 372 // anymore after new_typeArray() and no GC can happen before. 373 // (This may have to change if this code changes!) 374 assert(klass->is_klass(), "not a class"); 375 BasicType elt_type = TypeArrayKlass::cast(klass)->element_type(); 376 oop obj = oopFactory::new_typeArray(elt_type, length, CHECK); 377 thread->set_vm_result(obj); 378 // This is pretty rare but this runtime patch is stressful to deoptimization 379 // if we deoptimize here so force a deopt to stress the path. 380 if (DeoptimizeALot) { 381 deopt_caller(); 382 } 383 384 JRT_END 385 386 387 JRT_ENTRY(void, Runtime1::new_object_array(JavaThread* thread, Klass* array_klass, jint length)) 388 NOT_PRODUCT(_new_object_array_slowcase_cnt++;) 389 390 // Note: no handle for klass needed since they are not used 391 // anymore after new_objArray() and no GC can happen before. 392 // (This may have to change if this code changes!) 393 assert(array_klass->is_klass(), "not a class"); 394 Handle holder(THREAD, array_klass->klass_holder()); // keep the klass alive 395 Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass(); 396 objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK); 397 thread->set_vm_result(obj); 398 // This is pretty rare but this runtime patch is stressful to deoptimization 399 // if we deoptimize here so force a deopt to stress the path. 400 if (DeoptimizeALot) { 401 deopt_caller(); 402 } 403 JRT_END 404 405 406 JRT_ENTRY(void, Runtime1::new_multi_array(JavaThread* thread, Klass* klass, int rank, jint* dims)) 407 NOT_PRODUCT(_new_multi_array_slowcase_cnt++;) 408 409 assert(klass->is_klass(), "not a class"); 410 assert(rank >= 1, "rank must be nonzero"); 411 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 412 oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK); 413 thread->set_vm_result(obj); 414 JRT_END 415 416 417 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* thread, StubID id)) 418 tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", id); 419 JRT_END 420 421 422 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* thread, oopDesc* obj)) 423 ResourceMark rm(thread); 424 const char* klass_name = obj->klass()->external_name(); 425 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayStoreException(), klass_name); 426 JRT_END 427 428 429 // counter_overflow() is called from within C1-compiled methods. The enclosing method is the method 430 // associated with the top activation record. The inlinee (that is possibly included in the enclosing 431 // method) method oop is passed as an argument. In order to do that it is embedded in the code as 432 // a constant. 433 static nmethod* counter_overflow_helper(JavaThread* THREAD, int branch_bci, Method* m) { 434 nmethod* osr_nm = NULL; 435 methodHandle method(THREAD, m); 436 437 RegisterMap map(THREAD, false); 438 frame fr = THREAD->last_frame().sender(&map); 439 nmethod* nm = (nmethod*) fr.cb(); 440 assert(nm!= NULL && nm->is_nmethod(), "Sanity check"); 441 methodHandle enclosing_method(THREAD, nm->method()); 442 443 CompLevel level = (CompLevel)nm->comp_level(); 444 int bci = InvocationEntryBci; 445 if (branch_bci != InvocationEntryBci) { 446 // Compute destination bci 447 address pc = method()->code_base() + branch_bci; 448 Bytecodes::Code branch = Bytecodes::code_at(method(), pc); 449 int offset = 0; 450 switch (branch) { 451 case Bytecodes::_if_icmplt: case Bytecodes::_iflt: 452 case Bytecodes::_if_icmpgt: case Bytecodes::_ifgt: 453 case Bytecodes::_if_icmple: case Bytecodes::_ifle: 454 case Bytecodes::_if_icmpge: case Bytecodes::_ifge: 455 case Bytecodes::_if_icmpeq: case Bytecodes::_if_acmpeq: case Bytecodes::_ifeq: 456 case Bytecodes::_if_icmpne: case Bytecodes::_if_acmpne: case Bytecodes::_ifne: 457 case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: case Bytecodes::_goto: 458 offset = (int16_t)Bytes::get_Java_u2(pc + 1); 459 break; 460 case Bytecodes::_goto_w: 461 offset = Bytes::get_Java_u4(pc + 1); 462 break; 463 default: ; 464 } 465 bci = branch_bci + offset; 466 } 467 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending"); 468 osr_nm = CompilationPolicy::policy()->event(enclosing_method, method, branch_bci, bci, level, nm, THREAD); 469 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions"); 470 return osr_nm; 471 } 472 473 JRT_BLOCK_ENTRY(address, Runtime1::counter_overflow(JavaThread* thread, int bci, Method* method)) 474 nmethod* osr_nm; 475 JRT_BLOCK 476 osr_nm = counter_overflow_helper(thread, bci, method); 477 if (osr_nm != NULL) { 478 RegisterMap map(thread, false); 479 frame fr = thread->last_frame().sender(&map); 480 Deoptimization::deoptimize_frame(thread, fr.id()); 481 } 482 JRT_BLOCK_END 483 return NULL; 484 JRT_END 485 486 extern void vm_exit(int code); 487 488 // Enter this method from compiled code handler below. This is where we transition 489 // to VM mode. This is done as a helper routine so that the method called directly 490 // from compiled code does not have to transition to VM. This allows the entry 491 // method to see if the nmethod that we have just looked up a handler for has 492 // been deoptimized while we were in the vm. This simplifies the assembly code 493 // cpu directories. 494 // 495 // We are entering here from exception stub (via the entry method below) 496 // If there is a compiled exception handler in this method, we will continue there; 497 // otherwise we will unwind the stack and continue at the caller of top frame method 498 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to 499 // control the area where we can allow a safepoint. After we exit the safepoint area we can 500 // check to see if the handler we are going to return is now in a nmethod that has 501 // been deoptimized. If that is the case we return the deopt blob 502 // unpack_with_exception entry instead. This makes life for the exception blob easier 503 // because making that same check and diverting is painful from assembly language. 504 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, nmethod*& nm)) 505 // Reset method handle flag. 506 thread->set_is_method_handle_return(false); 507 508 Handle exception(thread, ex); 509 nm = CodeCache::find_nmethod(pc); 510 assert(nm != NULL, "this is not an nmethod"); 511 // Adjust the pc as needed/ 512 if (nm->is_deopt_pc(pc)) { 513 RegisterMap map(thread, false); 514 frame exception_frame = thread->last_frame().sender(&map); 515 // if the frame isn't deopted then pc must not correspond to the caller of last_frame 516 assert(exception_frame.is_deoptimized_frame(), "must be deopted"); 517 pc = exception_frame.pc(); 518 } 519 #ifdef ASSERT 520 assert(exception.not_null(), "NULL exceptions should be handled by throw_exception"); 521 // Check that exception is a subclass of Throwable, otherwise we have a VerifyError 522 if (!(exception->is_a(SystemDictionary::Throwable_klass()))) { 523 if (ExitVMOnVerifyError) vm_exit(-1); 524 ShouldNotReachHere(); 525 } 526 #endif 527 528 // Check the stack guard pages and reenable them if necessary and there is 529 // enough space on the stack to do so. Use fast exceptions only if the guard 530 // pages are enabled. 531 bool guard_pages_enabled = thread->stack_guards_enabled(); 532 if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack(); 533 534 if (JvmtiExport::can_post_on_exceptions()) { 535 // To ensure correct notification of exception catches and throws 536 // we have to deoptimize here. If we attempted to notify the 537 // catches and throws during this exception lookup it's possible 538 // we could deoptimize on the way out of the VM and end back in 539 // the interpreter at the throw site. This would result in double 540 // notifications since the interpreter would also notify about 541 // these same catches and throws as it unwound the frame. 542 543 RegisterMap reg_map(thread); 544 frame stub_frame = thread->last_frame(); 545 frame caller_frame = stub_frame.sender(®_map); 546 547 // We don't really want to deoptimize the nmethod itself since we 548 // can actually continue in the exception handler ourselves but I 549 // don't see an easy way to have the desired effect. 550 Deoptimization::deoptimize_frame(thread, caller_frame.id()); 551 assert(caller_is_deopted(), "Must be deoptimized"); 552 553 return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls(); 554 } 555 556 // ExceptionCache is used only for exceptions at call sites and not for implicit exceptions 557 if (guard_pages_enabled) { 558 address fast_continuation = nm->handler_for_exception_and_pc(exception, pc); 559 if (fast_continuation != NULL) { 560 // Set flag if return address is a method handle call site. 561 thread->set_is_method_handle_return(nm->is_method_handle_return(pc)); 562 return fast_continuation; 563 } 564 } 565 566 // If the stack guard pages are enabled, check whether there is a handler in 567 // the current method. Otherwise (guard pages disabled), force an unwind and 568 // skip the exception cache update (i.e., just leave continuation==NULL). 569 address continuation = NULL; 570 if (guard_pages_enabled) { 571 572 // New exception handling mechanism can support inlined methods 573 // with exception handlers since the mappings are from PC to PC 574 575 // debugging support 576 // tracing 577 if (log_is_enabled(Info, exceptions)) { 578 ResourceMark rm; 579 stringStream tempst; 580 assert(nm->method() != NULL, "Unexpected NULL method()"); 581 tempst.print("compiled method <%s>\n" 582 " at PC" INTPTR_FORMAT " for thread " INTPTR_FORMAT, 583 nm->method()->print_value_string(), p2i(pc), p2i(thread)); 584 Exceptions::log_exception(exception, tempst.as_string()); 585 } 586 // for AbortVMOnException flag 587 Exceptions::debug_check_abort(exception); 588 589 // Clear out the exception oop and pc since looking up an 590 // exception handler can cause class loading, which might throw an 591 // exception and those fields are expected to be clear during 592 // normal bytecode execution. 593 thread->clear_exception_oop_and_pc(); 594 595 bool recursive_exception = false; 596 continuation = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, false, false, recursive_exception); 597 // If an exception was thrown during exception dispatch, the exception oop may have changed 598 thread->set_exception_oop(exception()); 599 thread->set_exception_pc(pc); 600 601 // the exception cache is used only by non-implicit exceptions 602 // Update the exception cache only when there didn't happen 603 // another exception during the computation of the compiled 604 // exception handler. Checking for exception oop equality is not 605 // sufficient because some exceptions are pre-allocated and reused. 606 if (continuation != NULL && !recursive_exception) { 607 nm->add_handler_for_exception_and_pc(exception, pc, continuation); 608 } 609 } 610 611 thread->set_vm_result(exception()); 612 // Set flag if return address is a method handle call site. 613 thread->set_is_method_handle_return(nm->is_method_handle_return(pc)); 614 615 if (log_is_enabled(Info, exceptions)) { 616 ResourceMark rm; 617 log_info(exceptions)("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT 618 " for exception thrown at PC " PTR_FORMAT, 619 p2i(thread), p2i(continuation), p2i(pc)); 620 } 621 622 return continuation; 623 JRT_END 624 625 // Enter this method from compiled code only if there is a Java exception handler 626 // in the method handling the exception. 627 // We are entering here from exception stub. We don't do a normal VM transition here. 628 // We do it in a helper. This is so we can check to see if the nmethod we have just 629 // searched for an exception handler has been deoptimized in the meantime. 630 address Runtime1::exception_handler_for_pc(JavaThread* thread) { 631 oop exception = thread->exception_oop(); 632 address pc = thread->exception_pc(); 633 // Still in Java mode 634 DEBUG_ONLY(ResetNoHandleMark rnhm); 635 nmethod* nm = NULL; 636 address continuation = NULL; 637 { 638 // Enter VM mode by calling the helper 639 ResetNoHandleMark rnhm; 640 continuation = exception_handler_for_pc_helper(thread, exception, pc, nm); 641 } 642 // Back in JAVA, use no oops DON'T safepoint 643 644 // Now check to see if the nmethod we were called from is now deoptimized. 645 // If so we must return to the deopt blob and deoptimize the nmethod 646 if (nm != NULL && caller_is_deopted()) { 647 continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls(); 648 } 649 650 assert(continuation != NULL, "no handler found"); 651 return continuation; 652 } 653 654 655 JRT_ENTRY(void, Runtime1::throw_range_check_exception(JavaThread* thread, int index, arrayOopDesc* a)) 656 NOT_PRODUCT(_throw_range_check_exception_count++;) 657 const int len = 35; 658 assert(len < strlen("Index %d out of bounds for length %d"), "Must allocate more space for message."); 659 char message[2 * jintAsStringSize + len]; 660 sprintf(message, "Index %d out of bounds for length %d", index, a->length()); 661 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), message); 662 JRT_END 663 664 665 JRT_ENTRY(void, Runtime1::throw_index_exception(JavaThread* thread, int index)) 666 NOT_PRODUCT(_throw_index_exception_count++;) 667 char message[16]; 668 sprintf(message, "%d", index); 669 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IndexOutOfBoundsException(), message); 670 JRT_END 671 672 673 JRT_ENTRY(void, Runtime1::throw_div0_exception(JavaThread* thread)) 674 NOT_PRODUCT(_throw_div0_exception_count++;) 675 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArithmeticException(), "/ by zero"); 676 JRT_END 677 678 679 JRT_ENTRY(void, Runtime1::throw_null_pointer_exception(JavaThread* thread)) 680 NOT_PRODUCT(_throw_null_pointer_exception_count++;) 681 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_NullPointerException()); 682 JRT_END 683 684 685 JRT_ENTRY(void, Runtime1::throw_class_cast_exception(JavaThread* thread, oopDesc* object)) 686 NOT_PRODUCT(_throw_class_cast_exception_count++;) 687 ResourceMark rm(thread); 688 char* message = SharedRuntime::generate_class_cast_message( 689 thread, object->klass()); 690 SharedRuntime::throw_and_post_jvmti_exception( 691 thread, vmSymbols::java_lang_ClassCastException(), message); 692 JRT_END 693 694 695 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* thread)) 696 NOT_PRODUCT(_throw_incompatible_class_change_error_count++;) 697 ResourceMark rm(thread); 698 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IncompatibleClassChangeError()); 699 JRT_END 700 701 702 JRT_ENTRY_NO_ASYNC(void, Runtime1::monitorenter(JavaThread* thread, oopDesc* obj, BasicObjectLock* lock)) 703 NOT_PRODUCT(_monitorenter_slowcase_cnt++;) 704 if (PrintBiasedLockingStatistics) { 705 Atomic::inc(BiasedLocking::slow_path_entry_count_addr()); 706 } 707 Handle h_obj(thread, obj); 708 assert(obj == lock->obj(), "must match"); 709 ObjectSynchronizer::enter(h_obj, lock->lock(), THREAD); 710 JRT_END 711 712 713 JRT_LEAF(void, Runtime1::monitorexit(JavaThread* thread, BasicObjectLock* lock)) 714 NOT_PRODUCT(_monitorexit_slowcase_cnt++;) 715 assert(thread == JavaThread::current(), "threads must correspond"); 716 assert(thread->last_Java_sp(), "last_Java_sp must be set"); 717 // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown 718 EXCEPTION_MARK; 719 720 oop obj = lock->obj(); 721 assert(oopDesc::is_oop(obj), "must be NULL or an object"); 722 ObjectSynchronizer::exit(obj, lock->lock(), THREAD); 723 JRT_END 724 725 // Cf. OptoRuntime::deoptimize_caller_frame 726 JRT_ENTRY(void, Runtime1::deoptimize(JavaThread* thread, jint trap_request)) 727 // Called from within the owner thread, so no need for safepoint 728 RegisterMap reg_map(thread, false); 729 frame stub_frame = thread->last_frame(); 730 assert(stub_frame.is_runtime_frame(), "Sanity check"); 731 frame caller_frame = stub_frame.sender(®_map); 732 nmethod* nm = caller_frame.cb()->as_nmethod_or_null(); 733 assert(nm != NULL, "Sanity check"); 734 methodHandle method(thread, nm->method()); 735 assert(nm == CodeCache::find_nmethod(caller_frame.pc()), "Should be the same"); 736 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request); 737 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request); 738 739 if (action == Deoptimization::Action_make_not_entrant) { 740 if (nm->make_not_entrant()) { 741 if (reason == Deoptimization::Reason_tenured) { 742 MethodData* trap_mdo = Deoptimization::get_method_data(thread, method, true /*create_if_missing*/); 743 if (trap_mdo != NULL) { 744 trap_mdo->inc_tenure_traps(); 745 } 746 } 747 } 748 } 749 750 // Deoptimize the caller frame. 751 Deoptimization::deoptimize_frame(thread, caller_frame.id()); 752 // Return to the now deoptimized frame. 753 JRT_END 754 755 756 #ifndef DEOPTIMIZE_WHEN_PATCHING 757 758 static Klass* resolve_field_return_klass(const methodHandle& caller, int bci, TRAPS) { 759 Bytecode_field field_access(caller, bci); 760 // This can be static or non-static field access 761 Bytecodes::Code code = field_access.code(); 762 763 // We must load class, initialize class and resolve the field 764 fieldDescriptor result; // initialize class if needed 765 constantPoolHandle constants(THREAD, caller->constants()); 766 LinkResolver::resolve_field_access(result, constants, field_access.index(), caller, Bytecodes::java_code(code), CHECK_NULL); 767 return result.field_holder(); 768 } 769 770 771 // 772 // This routine patches sites where a class wasn't loaded or 773 // initialized at the time the code was generated. It handles 774 // references to classes, fields and forcing of initialization. Most 775 // of the cases are straightforward and involving simply forcing 776 // resolution of a class, rewriting the instruction stream with the 777 // needed constant and replacing the call in this function with the 778 // patched code. The case for static field is more complicated since 779 // the thread which is in the process of initializing a class can 780 // access it's static fields but other threads can't so the code 781 // either has to deoptimize when this case is detected or execute a 782 // check that the current thread is the initializing thread. The 783 // current 784 // 785 // Patches basically look like this: 786 // 787 // 788 // patch_site: jmp patch stub ;; will be patched 789 // continue: ... 790 // ... 791 // ... 792 // ... 793 // 794 // They have a stub which looks like this: 795 // 796 // ;; patch body 797 // movl <const>, reg (for class constants) 798 // <or> movl [reg1 + <const>], reg (for field offsets) 799 // <or> movl reg, [reg1 + <const>] (for field offsets) 800 // <being_init offset> <bytes to copy> <bytes to skip> 801 // patch_stub: call Runtime1::patch_code (through a runtime stub) 802 // jmp patch_site 803 // 804 // 805 // A normal patch is done by rewriting the patch body, usually a move, 806 // and then copying it into place over top of the jmp instruction 807 // being careful to flush caches and doing it in an MP-safe way. The 808 // constants following the patch body are used to find various pieces 809 // of the patch relative to the call site for Runtime1::patch_code. 810 // The case for getstatic and putstatic is more complicated because 811 // getstatic and putstatic have special semantics when executing while 812 // the class is being initialized. getstatic/putstatic on a class 813 // which is being_initialized may be executed by the initializing 814 // thread but other threads have to block when they execute it. This 815 // is accomplished in compiled code by executing a test of the current 816 // thread against the initializing thread of the class. It's emitted 817 // as boilerplate in their stub which allows the patched code to be 818 // executed before it's copied back into the main body of the nmethod. 819 // 820 // being_init: get_thread(<tmp reg> 821 // cmpl [reg1 + <init_thread_offset>], <tmp reg> 822 // jne patch_stub 823 // movl [reg1 + <const>], reg (for field offsets) <or> 824 // movl reg, [reg1 + <const>] (for field offsets) 825 // jmp continue 826 // <being_init offset> <bytes to copy> <bytes to skip> 827 // patch_stub: jmp Runtim1::patch_code (through a runtime stub) 828 // jmp patch_site 829 // 830 // If the class is being initialized the patch body is rewritten and 831 // the patch site is rewritten to jump to being_init, instead of 832 // patch_stub. Whenever this code is executed it checks the current 833 // thread against the intializing thread so other threads will enter 834 // the runtime and end up blocked waiting the class to finish 835 // initializing inside the calls to resolve_field below. The 836 // initializing class will continue on it's way. Once the class is 837 // fully_initialized, the intializing_thread of the class becomes 838 // NULL, so the next thread to execute this code will fail the test, 839 // call into patch_code and complete the patching process by copying 840 // the patch body back into the main part of the nmethod and resume 841 // executing. 842 843 // NB: 844 // 845 // Patchable instruction sequences inherently exhibit race conditions, 846 // where thread A is patching an instruction at the same time thread B 847 // is executing it. The algorithms we use ensure that any observation 848 // that B can make on any intermediate states during A's patching will 849 // always end up with a correct outcome. This is easiest if there are 850 // few or no intermediate states. (Some inline caches have two 851 // related instructions that must be patched in tandem. For those, 852 // intermediate states seem to be unavoidable, but we will get the 853 // right answer from all possible observation orders.) 854 // 855 // When patching the entry instruction at the head of a method, or a 856 // linkable call instruction inside of a method, we try very hard to 857 // use a patch sequence which executes as a single memory transaction. 858 // This means, in practice, that when thread A patches an instruction, 859 // it should patch a 32-bit or 64-bit word that somehow overlaps the 860 // instruction or is contained in it. We believe that memory hardware 861 // will never break up such a word write, if it is naturally aligned 862 // for the word being written. We also know that some CPUs work very 863 // hard to create atomic updates even of naturally unaligned words, 864 // but we don't want to bet the farm on this always working. 865 // 866 // Therefore, if there is any chance of a race condition, we try to 867 // patch only naturally aligned words, as single, full-word writes. 868 869 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, Runtime1::StubID stub_id )) 870 NOT_PRODUCT(_patch_code_slowcase_cnt++;) 871 872 ResourceMark rm(thread); 873 RegisterMap reg_map(thread, false); 874 frame runtime_frame = thread->last_frame(); 875 frame caller_frame = runtime_frame.sender(®_map); 876 877 // last java frame on stack 878 vframeStream vfst(thread, true); 879 assert(!vfst.at_end(), "Java frame must exist"); 880 881 methodHandle caller_method(THREAD, vfst.method()); 882 // Note that caller_method->code() may not be same as caller_code because of OSR's 883 // Note also that in the presence of inlining it is not guaranteed 884 // that caller_method() == caller_code->method() 885 886 int bci = vfst.bci(); 887 Bytecodes::Code code = caller_method()->java_code_at(bci); 888 889 // this is used by assertions in the access_field_patching_id 890 BasicType patch_field_type = T_ILLEGAL; 891 bool deoptimize_for_volatile = false; 892 bool deoptimize_for_atomic = false; 893 int patch_field_offset = -1; 894 Klass* init_klass = NULL; // klass needed by load_klass_patching code 895 Klass* load_klass = NULL; // klass needed by load_klass_patching code 896 Handle mirror(THREAD, NULL); // oop needed by load_mirror_patching code 897 Handle appendix(THREAD, NULL); // oop needed by appendix_patching code 898 bool load_klass_or_mirror_patch_id = 899 (stub_id == Runtime1::load_klass_patching_id || stub_id == Runtime1::load_mirror_patching_id); 900 901 if (stub_id == Runtime1::access_field_patching_id) { 902 903 Bytecode_field field_access(caller_method, bci); 904 fieldDescriptor result; // initialize class if needed 905 Bytecodes::Code code = field_access.code(); 906 constantPoolHandle constants(THREAD, caller_method->constants()); 907 LinkResolver::resolve_field_access(result, constants, field_access.index(), caller_method, Bytecodes::java_code(code), CHECK); 908 patch_field_offset = result.offset(); 909 910 // If we're patching a field which is volatile then at compile it 911 // must not have been know to be volatile, so the generated code 912 // isn't correct for a volatile reference. The nmethod has to be 913 // deoptimized so that the code can be regenerated correctly. 914 // This check is only needed for access_field_patching since this 915 // is the path for patching field offsets. load_klass is only 916 // used for patching references to oops which don't need special 917 // handling in the volatile case. 918 919 deoptimize_for_volatile = result.access_flags().is_volatile(); 920 921 // If we are patching a field which should be atomic, then 922 // the generated code is not correct either, force deoptimizing. 923 // We need to only cover T_LONG and T_DOUBLE fields, as we can 924 // break access atomicity only for them. 925 926 // Strictly speaking, the deoptimization on 64-bit platforms 927 // is unnecessary, and T_LONG stores on 32-bit platforms need 928 // to be handled by special patching code when AlwaysAtomicAccesses 929 // becomes product feature. At this point, we are still going 930 // for the deoptimization for consistency against volatile 931 // accesses. 932 933 patch_field_type = result.field_type(); 934 deoptimize_for_atomic = (AlwaysAtomicAccesses && (patch_field_type == T_DOUBLE || patch_field_type == T_LONG)); 935 936 } else if (load_klass_or_mirror_patch_id) { 937 Klass* k = NULL; 938 switch (code) { 939 case Bytecodes::_putstatic: 940 case Bytecodes::_getstatic: 941 { Klass* klass = resolve_field_return_klass(caller_method, bci, CHECK); 942 init_klass = klass; 943 mirror = Handle(THREAD, klass->java_mirror()); 944 } 945 break; 946 case Bytecodes::_new: 947 { Bytecode_new bnew(caller_method(), caller_method->bcp_from(bci)); 948 k = caller_method->constants()->klass_at(bnew.index(), CHECK); 949 } 950 break; 951 case Bytecodes::_multianewarray: 952 { Bytecode_multianewarray mna(caller_method(), caller_method->bcp_from(bci)); 953 k = caller_method->constants()->klass_at(mna.index(), CHECK); 954 } 955 break; 956 case Bytecodes::_instanceof: 957 { Bytecode_instanceof io(caller_method(), caller_method->bcp_from(bci)); 958 k = caller_method->constants()->klass_at(io.index(), CHECK); 959 } 960 break; 961 case Bytecodes::_checkcast: 962 { Bytecode_checkcast cc(caller_method(), caller_method->bcp_from(bci)); 963 k = caller_method->constants()->klass_at(cc.index(), CHECK); 964 } 965 break; 966 case Bytecodes::_anewarray: 967 { Bytecode_anewarray anew(caller_method(), caller_method->bcp_from(bci)); 968 Klass* ek = caller_method->constants()->klass_at(anew.index(), CHECK); 969 k = ek->array_klass(CHECK); 970 } 971 break; 972 case Bytecodes::_ldc: 973 case Bytecodes::_ldc_w: 974 { 975 Bytecode_loadconstant cc(caller_method, bci); 976 oop m = cc.resolve_constant(CHECK); 977 mirror = Handle(THREAD, m); 978 } 979 break; 980 default: fatal("unexpected bytecode for load_klass_or_mirror_patch_id"); 981 } 982 load_klass = k; 983 } else if (stub_id == load_appendix_patching_id) { 984 Bytecode_invoke bytecode(caller_method, bci); 985 Bytecodes::Code bc = bytecode.invoke_code(); 986 987 CallInfo info; 988 constantPoolHandle pool(thread, caller_method->constants()); 989 int index = bytecode.index(); 990 LinkResolver::resolve_invoke(info, Handle(), pool, index, bc, CHECK); 991 switch (bc) { 992 case Bytecodes::_invokehandle: { 993 int cache_index = ConstantPool::decode_cpcache_index(index, true); 994 assert(cache_index >= 0 && cache_index < pool->cache()->length(), "unexpected cache index"); 995 ConstantPoolCacheEntry* cpce = pool->cache()->entry_at(cache_index); 996 cpce->set_method_handle(pool, info); 997 appendix = Handle(THREAD, cpce->appendix_if_resolved(pool)); // just in case somebody already resolved the entry 998 break; 999 } 1000 case Bytecodes::_invokedynamic: { 1001 ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index); 1002 cpce->set_dynamic_call(pool, info); 1003 appendix = Handle(THREAD, cpce->appendix_if_resolved(pool)); // just in case somebody already resolved the entry 1004 break; 1005 } 1006 default: fatal("unexpected bytecode for load_appendix_patching_id"); 1007 } 1008 } else { 1009 ShouldNotReachHere(); 1010 } 1011 1012 if (deoptimize_for_volatile || deoptimize_for_atomic) { 1013 // At compile time we assumed the field wasn't volatile/atomic but after 1014 // loading it turns out it was volatile/atomic so we have to throw the 1015 // compiled code out and let it be regenerated. 1016 if (TracePatching) { 1017 if (deoptimize_for_volatile) { 1018 tty->print_cr("Deoptimizing for patching volatile field reference"); 1019 } 1020 if (deoptimize_for_atomic) { 1021 tty->print_cr("Deoptimizing for patching atomic field reference"); 1022 } 1023 } 1024 1025 // It's possible the nmethod was invalidated in the last 1026 // safepoint, but if it's still alive then make it not_entrant. 1027 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc()); 1028 if (nm != NULL) { 1029 nm->make_not_entrant(); 1030 } 1031 1032 Deoptimization::deoptimize_frame(thread, caller_frame.id()); 1033 1034 // Return to the now deoptimized frame. 1035 } 1036 1037 // Now copy code back 1038 1039 { 1040 MutexLocker ml_patch (Patching_lock, Mutex::_no_safepoint_check_flag); 1041 // 1042 // Deoptimization may have happened while we waited for the lock. 1043 // In that case we don't bother to do any patching we just return 1044 // and let the deopt happen 1045 if (!caller_is_deopted()) { 1046 NativeGeneralJump* jump = nativeGeneralJump_at(caller_frame.pc()); 1047 address instr_pc = jump->jump_destination(); 1048 NativeInstruction* ni = nativeInstruction_at(instr_pc); 1049 if (ni->is_jump() ) { 1050 // the jump has not been patched yet 1051 // The jump destination is slow case and therefore not part of the stubs 1052 // (stubs are only for StaticCalls) 1053 1054 // format of buffer 1055 // .... 1056 // instr byte 0 <-- copy_buff 1057 // instr byte 1 1058 // .. 1059 // instr byte n-1 1060 // n 1061 // .... <-- call destination 1062 1063 address stub_location = caller_frame.pc() + PatchingStub::patch_info_offset(); 1064 unsigned char* byte_count = (unsigned char*) (stub_location - 1); 1065 unsigned char* byte_skip = (unsigned char*) (stub_location - 2); 1066 unsigned char* being_initialized_entry_offset = (unsigned char*) (stub_location - 3); 1067 address copy_buff = stub_location - *byte_skip - *byte_count; 1068 address being_initialized_entry = stub_location - *being_initialized_entry_offset; 1069 if (TracePatching) { 1070 ttyLocker ttyl; 1071 tty->print_cr(" Patching %s at bci %d at address " INTPTR_FORMAT " (%s)", Bytecodes::name(code), bci, 1072 p2i(instr_pc), (stub_id == Runtime1::access_field_patching_id) ? "field" : "klass"); 1073 nmethod* caller_code = CodeCache::find_nmethod(caller_frame.pc()); 1074 assert(caller_code != NULL, "nmethod not found"); 1075 1076 // NOTE we use pc() not original_pc() because we already know they are 1077 // identical otherwise we'd have never entered this block of code 1078 1079 const ImmutableOopMap* map = caller_code->oop_map_for_return_address(caller_frame.pc()); 1080 assert(map != NULL, "null check"); 1081 map->print(); 1082 tty->cr(); 1083 1084 Disassembler::decode(copy_buff, copy_buff + *byte_count, tty); 1085 } 1086 // depending on the code below, do_patch says whether to copy the patch body back into the nmethod 1087 bool do_patch = true; 1088 if (stub_id == Runtime1::access_field_patching_id) { 1089 // The offset may not be correct if the class was not loaded at code generation time. 1090 // Set it now. 1091 NativeMovRegMem* n_move = nativeMovRegMem_at(copy_buff); 1092 assert(n_move->offset() == 0 || (n_move->offset() == 4 && (patch_field_type == T_DOUBLE || patch_field_type == T_LONG)), "illegal offset for type"); 1093 assert(patch_field_offset >= 0, "illegal offset"); 1094 n_move->add_offset_in_bytes(patch_field_offset); 1095 } else if (load_klass_or_mirror_patch_id) { 1096 // If a getstatic or putstatic is referencing a klass which 1097 // isn't fully initialized, the patch body isn't copied into 1098 // place until initialization is complete. In this case the 1099 // patch site is setup so that any threads besides the 1100 // initializing thread are forced to come into the VM and 1101 // block. 1102 do_patch = (code != Bytecodes::_getstatic && code != Bytecodes::_putstatic) || 1103 InstanceKlass::cast(init_klass)->is_initialized(); 1104 NativeGeneralJump* jump = nativeGeneralJump_at(instr_pc); 1105 if (jump->jump_destination() == being_initialized_entry) { 1106 assert(do_patch == true, "initialization must be complete at this point"); 1107 } else { 1108 // patch the instruction <move reg, klass> 1109 NativeMovConstReg* n_copy = nativeMovConstReg_at(copy_buff); 1110 1111 assert(n_copy->data() == 0 || 1112 n_copy->data() == (intptr_t)Universe::non_oop_word(), 1113 "illegal init value"); 1114 if (stub_id == Runtime1::load_klass_patching_id) { 1115 assert(load_klass != NULL, "klass not set"); 1116 n_copy->set_data((intx) (load_klass)); 1117 } else { 1118 assert(mirror() != NULL, "klass not set"); 1119 // Don't need a G1 pre-barrier here since we assert above that data isn't an oop. 1120 n_copy->set_data(cast_from_oop<intx>(mirror())); 1121 } 1122 1123 if (TracePatching) { 1124 Disassembler::decode(copy_buff, copy_buff + *byte_count, tty); 1125 } 1126 } 1127 } else if (stub_id == Runtime1::load_appendix_patching_id) { 1128 NativeMovConstReg* n_copy = nativeMovConstReg_at(copy_buff); 1129 assert(n_copy->data() == 0 || 1130 n_copy->data() == (intptr_t)Universe::non_oop_word(), 1131 "illegal init value"); 1132 n_copy->set_data(cast_from_oop<intx>(appendix())); 1133 1134 if (TracePatching) { 1135 Disassembler::decode(copy_buff, copy_buff + *byte_count, tty); 1136 } 1137 } else { 1138 ShouldNotReachHere(); 1139 } 1140 1141 #if defined(SPARC) || defined(PPC32) 1142 if (load_klass_or_mirror_patch_id || 1143 stub_id == Runtime1::load_appendix_patching_id) { 1144 // Update the location in the nmethod with the proper 1145 // metadata. When the code was generated, a NULL was stuffed 1146 // in the metadata table and that table needs to be update to 1147 // have the right value. On intel the value is kept 1148 // directly in the instruction instead of in the metadata 1149 // table, so set_data above effectively updated the value. 1150 nmethod* nm = CodeCache::find_nmethod(instr_pc); 1151 assert(nm != NULL, "invalid nmethod_pc"); 1152 RelocIterator mds(nm, copy_buff, copy_buff + 1); 1153 bool found = false; 1154 while (mds.next() && !found) { 1155 if (mds.type() == relocInfo::oop_type) { 1156 assert(stub_id == Runtime1::load_mirror_patching_id || 1157 stub_id == Runtime1::load_appendix_patching_id, "wrong stub id"); 1158 oop_Relocation* r = mds.oop_reloc(); 1159 oop* oop_adr = r->oop_addr(); 1160 *oop_adr = stub_id == Runtime1::load_mirror_patching_id ? mirror() : appendix(); 1161 r->fix_oop_relocation(); 1162 found = true; 1163 } else if (mds.type() == relocInfo::metadata_type) { 1164 assert(stub_id == Runtime1::load_klass_patching_id, "wrong stub id"); 1165 metadata_Relocation* r = mds.metadata_reloc(); 1166 Metadata** metadata_adr = r->metadata_addr(); 1167 *metadata_adr = load_klass; 1168 r->fix_metadata_relocation(); 1169 found = true; 1170 } 1171 } 1172 assert(found, "the metadata must exist!"); 1173 } 1174 #endif 1175 if (do_patch) { 1176 // replace instructions 1177 // first replace the tail, then the call 1178 #ifdef ARM 1179 if((load_klass_or_mirror_patch_id || 1180 stub_id == Runtime1::load_appendix_patching_id) && 1181 nativeMovConstReg_at(copy_buff)->is_pc_relative()) { 1182 nmethod* nm = CodeCache::find_nmethod(instr_pc); 1183 address addr = NULL; 1184 assert(nm != NULL, "invalid nmethod_pc"); 1185 RelocIterator mds(nm, copy_buff, copy_buff + 1); 1186 while (mds.next()) { 1187 if (mds.type() == relocInfo::oop_type) { 1188 assert(stub_id == Runtime1::load_mirror_patching_id || 1189 stub_id == Runtime1::load_appendix_patching_id, "wrong stub id"); 1190 oop_Relocation* r = mds.oop_reloc(); 1191 addr = (address)r->oop_addr(); 1192 break; 1193 } else if (mds.type() == relocInfo::metadata_type) { 1194 assert(stub_id == Runtime1::load_klass_patching_id, "wrong stub id"); 1195 metadata_Relocation* r = mds.metadata_reloc(); 1196 addr = (address)r->metadata_addr(); 1197 break; 1198 } 1199 } 1200 assert(addr != NULL, "metadata relocation must exist"); 1201 copy_buff -= *byte_count; 1202 NativeMovConstReg* n_copy2 = nativeMovConstReg_at(copy_buff); 1203 n_copy2->set_pc_relative_offset(addr, instr_pc); 1204 } 1205 #endif 1206 1207 for (int i = NativeGeneralJump::instruction_size; i < *byte_count; i++) { 1208 address ptr = copy_buff + i; 1209 int a_byte = (*ptr) & 0xFF; 1210 address dst = instr_pc + i; 1211 *(unsigned char*)dst = (unsigned char) a_byte; 1212 } 1213 ICache::invalidate_range(instr_pc, *byte_count); 1214 NativeGeneralJump::replace_mt_safe(instr_pc, copy_buff); 1215 1216 if (load_klass_or_mirror_patch_id || 1217 stub_id == Runtime1::load_appendix_patching_id) { 1218 relocInfo::relocType rtype = 1219 (stub_id == Runtime1::load_klass_patching_id) ? 1220 relocInfo::metadata_type : 1221 relocInfo::oop_type; 1222 // update relocInfo to metadata 1223 nmethod* nm = CodeCache::find_nmethod(instr_pc); 1224 assert(nm != NULL, "invalid nmethod_pc"); 1225 1226 // The old patch site is now a move instruction so update 1227 // the reloc info so that it will get updated during 1228 // future GCs. 1229 RelocIterator iter(nm, (address)instr_pc, (address)(instr_pc + 1)); 1230 relocInfo::change_reloc_info_for_address(&iter, (address) instr_pc, 1231 relocInfo::none, rtype); 1232 #ifdef SPARC 1233 // Sparc takes two relocations for an metadata so update the second one. 1234 address instr_pc2 = instr_pc + NativeMovConstReg::add_offset; 1235 RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1); 1236 relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2, 1237 relocInfo::none, rtype); 1238 #endif 1239 #ifdef PPC32 1240 { address instr_pc2 = instr_pc + NativeMovConstReg::lo_offset; 1241 RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1); 1242 relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2, 1243 relocInfo::none, rtype); 1244 } 1245 #endif 1246 } 1247 1248 } else { 1249 ICache::invalidate_range(copy_buff, *byte_count); 1250 NativeGeneralJump::insert_unconditional(instr_pc, being_initialized_entry); 1251 } 1252 } 1253 } 1254 } 1255 1256 // If we are patching in a non-perm oop, make sure the nmethod 1257 // is on the right list. 1258 { 1259 MutexLocker ml_code (CodeCache_lock, Mutex::_no_safepoint_check_flag); 1260 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc()); 1261 guarantee(nm != NULL, "only nmethods can contain non-perm oops"); 1262 1263 // Since we've patched some oops in the nmethod, 1264 // (re)register it with the heap. 1265 Universe::heap()->register_nmethod(nm); 1266 } 1267 JRT_END 1268 1269 #else // DEOPTIMIZE_WHEN_PATCHING 1270 1271 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, Runtime1::StubID stub_id )) 1272 RegisterMap reg_map(thread, false); 1273 1274 NOT_PRODUCT(_patch_code_slowcase_cnt++;) 1275 if (TracePatching) { 1276 tty->print_cr("Deoptimizing because patch is needed"); 1277 } 1278 1279 frame runtime_frame = thread->last_frame(); 1280 frame caller_frame = runtime_frame.sender(®_map); 1281 1282 // It's possible the nmethod was invalidated in the last 1283 // safepoint, but if it's still alive then make it not_entrant. 1284 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc()); 1285 if (nm != NULL) { 1286 nm->make_not_entrant(); 1287 } 1288 1289 Deoptimization::deoptimize_frame(thread, caller_frame.id()); 1290 1291 // Return to the now deoptimized frame. 1292 JRT_END 1293 1294 #endif // DEOPTIMIZE_WHEN_PATCHING 1295 1296 // 1297 // Entry point for compiled code. We want to patch a nmethod. 1298 // We don't do a normal VM transition here because we want to 1299 // know after the patching is complete and any safepoint(s) are taken 1300 // if the calling nmethod was deoptimized. We do this by calling a 1301 // helper method which does the normal VM transition and when it 1302 // completes we can check for deoptimization. This simplifies the 1303 // assembly code in the cpu directories. 1304 // 1305 int Runtime1::move_klass_patching(JavaThread* thread) { 1306 // 1307 // NOTE: we are still in Java 1308 // 1309 Thread* THREAD = thread; 1310 debug_only(NoHandleMark nhm;) 1311 { 1312 // Enter VM mode 1313 1314 ResetNoHandleMark rnhm; 1315 patch_code(thread, load_klass_patching_id); 1316 } 1317 // Back in JAVA, use no oops DON'T safepoint 1318 1319 // Return true if calling code is deoptimized 1320 1321 return caller_is_deopted(); 1322 } 1323 1324 int Runtime1::move_mirror_patching(JavaThread* thread) { 1325 // 1326 // NOTE: we are still in Java 1327 // 1328 Thread* THREAD = thread; 1329 debug_only(NoHandleMark nhm;) 1330 { 1331 // Enter VM mode 1332 1333 ResetNoHandleMark rnhm; 1334 patch_code(thread, load_mirror_patching_id); 1335 } 1336 // Back in JAVA, use no oops DON'T safepoint 1337 1338 // Return true if calling code is deoptimized 1339 1340 return caller_is_deopted(); 1341 } 1342 1343 int Runtime1::move_appendix_patching(JavaThread* thread) { 1344 // 1345 // NOTE: we are still in Java 1346 // 1347 Thread* THREAD = thread; 1348 debug_only(NoHandleMark nhm;) 1349 { 1350 // Enter VM mode 1351 1352 ResetNoHandleMark rnhm; 1353 patch_code(thread, load_appendix_patching_id); 1354 } 1355 // Back in JAVA, use no oops DON'T safepoint 1356 1357 // Return true if calling code is deoptimized 1358 1359 return caller_is_deopted(); 1360 } 1361 // 1362 // Entry point for compiled code. We want to patch a nmethod. 1363 // We don't do a normal VM transition here because we want to 1364 // know after the patching is complete and any safepoint(s) are taken 1365 // if the calling nmethod was deoptimized. We do this by calling a 1366 // helper method which does the normal VM transition and when it 1367 // completes we can check for deoptimization. This simplifies the 1368 // assembly code in the cpu directories. 1369 // 1370 1371 int Runtime1::access_field_patching(JavaThread* thread) { 1372 // 1373 // NOTE: we are still in Java 1374 // 1375 Thread* THREAD = thread; 1376 debug_only(NoHandleMark nhm;) 1377 { 1378 // Enter VM mode 1379 1380 ResetNoHandleMark rnhm; 1381 patch_code(thread, access_field_patching_id); 1382 } 1383 // Back in JAVA, use no oops DON'T safepoint 1384 1385 // Return true if calling code is deoptimized 1386 1387 return caller_is_deopted(); 1388 JRT_END 1389 1390 1391 JRT_LEAF(void, Runtime1::trace_block_entry(jint block_id)) 1392 // for now we just print out the block id 1393 tty->print("%d ", block_id); 1394 JRT_END 1395 1396 1397 JRT_LEAF(int, Runtime1::is_instance_of(oopDesc* mirror, oopDesc* obj)) 1398 // had to return int instead of bool, otherwise there may be a mismatch 1399 // between the C calling convention and the Java one. 1400 // e.g., on x86, GCC may clear only %al when returning a bool false, but 1401 // JVM takes the whole %eax as the return value, which may misinterpret 1402 // the return value as a boolean true. 1403 1404 assert(mirror != NULL, "should null-check on mirror before calling"); 1405 Klass* k = java_lang_Class::as_Klass(mirror); 1406 return (k != NULL && obj != NULL && obj->is_a(k)) ? 1 : 0; 1407 JRT_END 1408 1409 JRT_ENTRY(void, Runtime1::predicate_failed_trap(JavaThread* thread)) 1410 ResourceMark rm; 1411 1412 assert(!TieredCompilation, "incompatible with tiered compilation"); 1413 1414 RegisterMap reg_map(thread, false); 1415 frame runtime_frame = thread->last_frame(); 1416 frame caller_frame = runtime_frame.sender(®_map); 1417 1418 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc()); 1419 assert (nm != NULL, "no more nmethod?"); 1420 nm->make_not_entrant(); 1421 1422 methodHandle m(nm->method()); 1423 MethodData* mdo = m->method_data(); 1424 1425 if (mdo == NULL && !HAS_PENDING_EXCEPTION) { 1426 // Build an MDO. Ignore errors like OutOfMemory; 1427 // that simply means we won't have an MDO to update. 1428 Method::build_interpreter_method_data(m, THREAD); 1429 if (HAS_PENDING_EXCEPTION) { 1430 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here"); 1431 CLEAR_PENDING_EXCEPTION; 1432 } 1433 mdo = m->method_data(); 1434 } 1435 1436 if (mdo != NULL) { 1437 mdo->inc_trap_count(Deoptimization::Reason_none); 1438 } 1439 1440 if (TracePredicateFailedTraps) { 1441 stringStream ss1, ss2; 1442 vframeStream vfst(thread); 1443 methodHandle inlinee = methodHandle(vfst.method()); 1444 inlinee->print_short_name(&ss1); 1445 m->print_short_name(&ss2); 1446 tty->print_cr("Predicate failed trap in method %s at bci %d inlined in %s at pc " INTPTR_FORMAT, ss1.as_string(), vfst.bci(), ss2.as_string(), p2i(caller_frame.pc())); 1447 } 1448 1449 1450 Deoptimization::deoptimize_frame(thread, caller_frame.id()); 1451 1452 JRT_END 1453 1454 #ifndef PRODUCT 1455 void Runtime1::print_statistics() { 1456 tty->print_cr("C1 Runtime statistics:"); 1457 tty->print_cr(" _resolve_invoke_virtual_cnt: %d", SharedRuntime::_resolve_virtual_ctr); 1458 tty->print_cr(" _resolve_invoke_opt_virtual_cnt: %d", SharedRuntime::_resolve_opt_virtual_ctr); 1459 tty->print_cr(" _resolve_invoke_static_cnt: %d", SharedRuntime::_resolve_static_ctr); 1460 tty->print_cr(" _handle_wrong_method_cnt: %d", SharedRuntime::_wrong_method_ctr); 1461 tty->print_cr(" _ic_miss_cnt: %d", SharedRuntime::_ic_miss_ctr); 1462 tty->print_cr(" _generic_arraycopy_cnt: %d", _generic_arraycopy_cnt); 1463 tty->print_cr(" _generic_arraycopystub_cnt: %d", _generic_arraycopystub_cnt); 1464 tty->print_cr(" _byte_arraycopy_cnt: %d", _byte_arraycopy_stub_cnt); 1465 tty->print_cr(" _short_arraycopy_cnt: %d", _short_arraycopy_stub_cnt); 1466 tty->print_cr(" _int_arraycopy_cnt: %d", _int_arraycopy_stub_cnt); 1467 tty->print_cr(" _long_arraycopy_cnt: %d", _long_arraycopy_stub_cnt); 1468 tty->print_cr(" _oop_arraycopy_cnt: %d", _oop_arraycopy_stub_cnt); 1469 tty->print_cr(" _arraycopy_slowcase_cnt: %d", _arraycopy_slowcase_cnt); 1470 tty->print_cr(" _arraycopy_checkcast_cnt: %d", _arraycopy_checkcast_cnt); 1471 tty->print_cr(" _arraycopy_checkcast_attempt_cnt:%d", _arraycopy_checkcast_attempt_cnt); 1472 1473 tty->print_cr(" _new_type_array_slowcase_cnt: %d", _new_type_array_slowcase_cnt); 1474 tty->print_cr(" _new_object_array_slowcase_cnt: %d", _new_object_array_slowcase_cnt); 1475 tty->print_cr(" _new_instance_slowcase_cnt: %d", _new_instance_slowcase_cnt); 1476 tty->print_cr(" _new_multi_array_slowcase_cnt: %d", _new_multi_array_slowcase_cnt); 1477 tty->print_cr(" _monitorenter_slowcase_cnt: %d", _monitorenter_slowcase_cnt); 1478 tty->print_cr(" _monitorexit_slowcase_cnt: %d", _monitorexit_slowcase_cnt); 1479 tty->print_cr(" _patch_code_slowcase_cnt: %d", _patch_code_slowcase_cnt); 1480 1481 tty->print_cr(" _throw_range_check_exception_count: %d:", _throw_range_check_exception_count); 1482 tty->print_cr(" _throw_index_exception_count: %d:", _throw_index_exception_count); 1483 tty->print_cr(" _throw_div0_exception_count: %d:", _throw_div0_exception_count); 1484 tty->print_cr(" _throw_null_pointer_exception_count: %d:", _throw_null_pointer_exception_count); 1485 tty->print_cr(" _throw_class_cast_exception_count: %d:", _throw_class_cast_exception_count); 1486 tty->print_cr(" _throw_incompatible_class_change_error_count: %d:", _throw_incompatible_class_change_error_count); 1487 tty->print_cr(" _throw_array_store_exception_count: %d:", _throw_array_store_exception_count); 1488 tty->print_cr(" _throw_count: %d:", _throw_count); 1489 1490 SharedRuntime::print_ic_miss_histogram(); 1491 tty->cr(); 1492 } 1493 #endif // PRODUCT