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