1 /* 2 * Copyright (c) 2012, 2018, 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 #include "precompiled.hpp" 25 #include "jvm.h" 26 #include "asm/codeBuffer.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "code/codeCache.hpp" 29 #include "code/compiledMethod.inline.hpp" 30 #include "compiler/compileBroker.hpp" 31 #include "compiler/disassembler.hpp" 32 #include "jvmci/jvmciRuntime.hpp" 33 #include "jvmci/jvmciCompilerToVM.hpp" 34 #include "jvmci/jvmciCompiler.hpp" 35 #include "jvmci/jvmciJavaClasses.hpp" 36 #include "jvmci/jvmciEnv.hpp" 37 #include "logging/log.hpp" 38 #include "memory/allocation.inline.hpp" 39 #include "memory/oopFactory.hpp" 40 #include "memory/resourceArea.hpp" 41 #include "oops/oop.inline.hpp" 42 #include "oops/objArrayOop.inline.hpp" 43 #include "runtime/biasedLocking.hpp" 44 #include "runtime/frame.inline.hpp" 45 #include "runtime/interfaceSupport.inline.hpp" 46 #include "runtime/jniHandles.inline.hpp" 47 #include "runtime/reflection.hpp" 48 #include "runtime/sharedRuntime.hpp" 49 #include "runtime/threadSMR.hpp" 50 #include "utilities/debug.hpp" 51 #include "utilities/defaultStream.hpp" 52 #include "utilities/macros.hpp" 53 #if INCLUDE_G1GC 54 #include "gc/g1/g1ThreadLocalData.hpp" 55 #endif // INCLUDE_G1GC 56 57 #if defined(_MSC_VER) 58 #define strtoll _strtoi64 59 #endif 60 61 jobject JVMCIRuntime::_HotSpotJVMCIRuntime_instance = NULL; 62 bool JVMCIRuntime::_HotSpotJVMCIRuntime_initialized = false; 63 bool JVMCIRuntime::_well_known_classes_initialized = false; 64 JVMCIRuntime::CompLevelAdjustment JVMCIRuntime::_comp_level_adjustment = JVMCIRuntime::none; 65 bool JVMCIRuntime::_shutdown_called = false; 66 67 BasicType JVMCIRuntime::kindToBasicType(Handle kind, TRAPS) { 68 if (kind.is_null()) { 69 THROW_(vmSymbols::java_lang_NullPointerException(), T_ILLEGAL); 70 } 71 jchar ch = JavaKind::typeChar(kind); 72 switch(ch) { 73 case 'Z': return T_BOOLEAN; 74 case 'B': return T_BYTE; 75 case 'S': return T_SHORT; 76 case 'C': return T_CHAR; 77 case 'I': return T_INT; 78 case 'F': return T_FLOAT; 79 case 'J': return T_LONG; 80 case 'D': return T_DOUBLE; 81 case 'A': return T_OBJECT; 82 case '-': return T_ILLEGAL; 83 default: 84 JVMCI_ERROR_(T_ILLEGAL, "unexpected Kind: %c", ch); 85 } 86 } 87 88 // Simple helper to see if the caller of a runtime stub which 89 // entered the VM has been deoptimized 90 91 static bool caller_is_deopted() { 92 JavaThread* thread = JavaThread::current(); 93 RegisterMap reg_map(thread, false); 94 frame runtime_frame = thread->last_frame(); 95 frame caller_frame = runtime_frame.sender(®_map); 96 assert(caller_frame.is_compiled_frame(), "must be compiled"); 97 return caller_frame.is_deoptimized_frame(); 98 } 99 100 // Stress deoptimization 101 static void deopt_caller() { 102 if ( !caller_is_deopted()) { 103 JavaThread* thread = JavaThread::current(); 104 RegisterMap reg_map(thread, false); 105 frame runtime_frame = thread->last_frame(); 106 frame caller_frame = runtime_frame.sender(®_map); 107 Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint); 108 assert(caller_is_deopted(), "Must be deoptimized"); 109 } 110 } 111 112 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_instance(JavaThread* thread, Klass* klass)) 113 JRT_BLOCK; 114 assert(klass->is_klass(), "not a class"); 115 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 116 InstanceKlass* ik = InstanceKlass::cast(klass); 117 ik->check_valid_for_instantiation(true, CHECK); 118 // make sure klass is initialized 119 ik->initialize(CHECK); 120 // allocate instance and return via TLS 121 oop obj = ik->allocate_instance(CHECK); 122 thread->set_vm_result(obj); 123 JRT_BLOCK_END; 124 SharedRuntime::on_slowpath_allocation_exit(thread); 125 JRT_END 126 127 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_array(JavaThread* thread, Klass* array_klass, jint length)) 128 JRT_BLOCK; 129 // Note: no handle for klass needed since they are not used 130 // anymore after new_objArray() and no GC can happen before. 131 // (This may have to change if this code changes!) 132 assert(array_klass->is_klass(), "not a class"); 133 oop obj; 134 if (array_klass->is_typeArray_klass()) { 135 BasicType elt_type = TypeArrayKlass::cast(array_klass)->element_type(); 136 obj = oopFactory::new_typeArray(elt_type, length, CHECK); 137 } else { 138 Handle holder(THREAD, array_klass->klass_holder()); // keep the klass alive 139 Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass(); 140 obj = oopFactory::new_objArray(elem_klass, length, CHECK); 141 } 142 thread->set_vm_result(obj); 143 // This is pretty rare but this runtime patch is stressful to deoptimization 144 // if we deoptimize here so force a deopt to stress the path. 145 if (DeoptimizeALot) { 146 static int deopts = 0; 147 // Alternate between deoptimizing and raising an error (which will also cause a deopt) 148 if (deopts++ % 2 == 0) { 149 ResourceMark rm(THREAD); 150 THROW(vmSymbols::java_lang_OutOfMemoryError()); 151 } else { 152 deopt_caller(); 153 } 154 } 155 JRT_BLOCK_END; 156 SharedRuntime::on_slowpath_allocation_exit(thread); 157 JRT_END 158 159 JRT_ENTRY(void, JVMCIRuntime::new_multi_array(JavaThread* thread, Klass* klass, int rank, jint* dims)) 160 assert(klass->is_klass(), "not a class"); 161 assert(rank >= 1, "rank must be nonzero"); 162 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 163 oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK); 164 thread->set_vm_result(obj); 165 JRT_END 166 167 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_array(JavaThread* thread, oopDesc* element_mirror, jint length)) 168 oop obj = Reflection::reflect_new_array(element_mirror, length, CHECK); 169 thread->set_vm_result(obj); 170 JRT_END 171 172 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_instance(JavaThread* thread, oopDesc* type_mirror)) 173 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(type_mirror)); 174 175 if (klass == NULL) { 176 ResourceMark rm(THREAD); 177 THROW(vmSymbols::java_lang_InstantiationException()); 178 } 179 180 // Create new instance (the receiver) 181 klass->check_valid_for_instantiation(false, CHECK); 182 183 // Make sure klass gets initialized 184 klass->initialize(CHECK); 185 186 oop obj = klass->allocate_instance(CHECK); 187 thread->set_vm_result(obj); 188 JRT_END 189 190 extern void vm_exit(int code); 191 192 // Enter this method from compiled code handler below. This is where we transition 193 // to VM mode. This is done as a helper routine so that the method called directly 194 // from compiled code does not have to transition to VM. This allows the entry 195 // method to see if the nmethod that we have just looked up a handler for has 196 // been deoptimized while we were in the vm. This simplifies the assembly code 197 // cpu directories. 198 // 199 // We are entering here from exception stub (via the entry method below) 200 // If there is a compiled exception handler in this method, we will continue there; 201 // otherwise we will unwind the stack and continue at the caller of top frame method 202 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to 203 // control the area where we can allow a safepoint. After we exit the safepoint area we can 204 // check to see if the handler we are going to return is now in a nmethod that has 205 // been deoptimized. If that is the case we return the deopt blob 206 // unpack_with_exception entry instead. This makes life for the exception blob easier 207 // because making that same check and diverting is painful from assembly language. 208 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, CompiledMethod*& cm)) 209 // Reset method handle flag. 210 thread->set_is_method_handle_return(false); 211 212 Handle exception(thread, ex); 213 cm = CodeCache::find_compiled(pc); 214 assert(cm != NULL, "this is not a compiled method"); 215 // Adjust the pc as needed/ 216 if (cm->is_deopt_pc(pc)) { 217 RegisterMap map(thread, false); 218 frame exception_frame = thread->last_frame().sender(&map); 219 // if the frame isn't deopted then pc must not correspond to the caller of last_frame 220 assert(exception_frame.is_deoptimized_frame(), "must be deopted"); 221 pc = exception_frame.pc(); 222 } 223 #ifdef ASSERT 224 assert(exception.not_null(), "NULL exceptions should be handled by throw_exception"); 225 // Check that exception is a subclass of Throwable, otherwise we have a VerifyError 226 if (!(exception->is_a(SystemDictionary::Throwable_klass()))) { 227 if (ExitVMOnVerifyError) vm_exit(-1); 228 ShouldNotReachHere(); 229 } 230 #endif 231 232 // Check the stack guard pages and reenable them if necessary and there is 233 // enough space on the stack to do so. Use fast exceptions only if the guard 234 // pages are enabled. 235 bool guard_pages_enabled = thread->stack_guards_enabled(); 236 if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack(); 237 238 if (JvmtiExport::can_post_on_exceptions()) { 239 // To ensure correct notification of exception catches and throws 240 // we have to deoptimize here. If we attempted to notify the 241 // catches and throws during this exception lookup it's possible 242 // we could deoptimize on the way out of the VM and end back in 243 // the interpreter at the throw site. This would result in double 244 // notifications since the interpreter would also notify about 245 // these same catches and throws as it unwound the frame. 246 247 RegisterMap reg_map(thread); 248 frame stub_frame = thread->last_frame(); 249 frame caller_frame = stub_frame.sender(®_map); 250 251 // We don't really want to deoptimize the nmethod itself since we 252 // can actually continue in the exception handler ourselves but I 253 // don't see an easy way to have the desired effect. 254 Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint); 255 assert(caller_is_deopted(), "Must be deoptimized"); 256 257 return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls(); 258 } 259 260 // ExceptionCache is used only for exceptions at call sites and not for implicit exceptions 261 if (guard_pages_enabled) { 262 address fast_continuation = cm->handler_for_exception_and_pc(exception, pc); 263 if (fast_continuation != NULL) { 264 // Set flag if return address is a method handle call site. 265 thread->set_is_method_handle_return(cm->is_method_handle_return(pc)); 266 return fast_continuation; 267 } 268 } 269 270 // If the stack guard pages are enabled, check whether there is a handler in 271 // the current method. Otherwise (guard pages disabled), force an unwind and 272 // skip the exception cache update (i.e., just leave continuation==NULL). 273 address continuation = NULL; 274 if (guard_pages_enabled) { 275 276 // New exception handling mechanism can support inlined methods 277 // with exception handlers since the mappings are from PC to PC 278 279 // debugging support 280 // tracing 281 if (log_is_enabled(Info, exceptions)) { 282 ResourceMark rm; 283 stringStream tempst; 284 tempst.print("compiled method <%s>\n" 285 " at PC" INTPTR_FORMAT " for thread " INTPTR_FORMAT, 286 cm->method()->print_value_string(), p2i(pc), p2i(thread)); 287 Exceptions::log_exception(exception, tempst); 288 } 289 // for AbortVMOnException flag 290 NOT_PRODUCT(Exceptions::debug_check_abort(exception)); 291 292 // Clear out the exception oop and pc since looking up an 293 // exception handler can cause class loading, which might throw an 294 // exception and those fields are expected to be clear during 295 // normal bytecode execution. 296 thread->clear_exception_oop_and_pc(); 297 298 bool recursive_exception = false; 299 continuation = SharedRuntime::compute_compiled_exc_handler(cm, pc, exception, false, false, recursive_exception); 300 // If an exception was thrown during exception dispatch, the exception oop may have changed 301 thread->set_exception_oop(exception()); 302 thread->set_exception_pc(pc); 303 304 // the exception cache is used only by non-implicit exceptions 305 // Update the exception cache only when there didn't happen 306 // another exception during the computation of the compiled 307 // exception handler. Checking for exception oop equality is not 308 // sufficient because some exceptions are pre-allocated and reused. 309 if (continuation != NULL && !recursive_exception && !SharedRuntime::deopt_blob()->contains(continuation)) { 310 cm->add_handler_for_exception_and_pc(exception, pc, continuation); 311 } 312 } 313 314 // Set flag if return address is a method handle call site. 315 thread->set_is_method_handle_return(cm->is_method_handle_return(pc)); 316 317 if (log_is_enabled(Info, exceptions)) { 318 ResourceMark rm; 319 log_info(exceptions)("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT 320 " for exception thrown at PC " PTR_FORMAT, 321 p2i(thread), p2i(continuation), p2i(pc)); 322 } 323 324 return continuation; 325 JRT_END 326 327 // Enter this method from compiled code only if there is a Java exception handler 328 // in the method handling the exception. 329 // We are entering here from exception stub. We don't do a normal VM transition here. 330 // We do it in a helper. This is so we can check to see if the nmethod we have just 331 // searched for an exception handler has been deoptimized in the meantime. 332 address JVMCIRuntime::exception_handler_for_pc(JavaThread* thread) { 333 oop exception = thread->exception_oop(); 334 address pc = thread->exception_pc(); 335 // Still in Java mode 336 DEBUG_ONLY(ResetNoHandleMark rnhm); 337 CompiledMethod* cm = NULL; 338 address continuation = NULL; 339 { 340 // Enter VM mode by calling the helper 341 ResetNoHandleMark rnhm; 342 continuation = exception_handler_for_pc_helper(thread, exception, pc, cm); 343 } 344 // Back in JAVA, use no oops DON'T safepoint 345 346 // Now check to see if the compiled method we were called from is now deoptimized. 347 // If so we must return to the deopt blob and deoptimize the nmethod 348 if (cm != NULL && caller_is_deopted()) { 349 continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls(); 350 } 351 352 assert(continuation != NULL, "no handler found"); 353 return continuation; 354 } 355 356 JRT_ENTRY_NO_ASYNC(void, JVMCIRuntime::monitorenter(JavaThread* thread, oopDesc* obj, BasicLock* lock)) 357 IF_TRACE_jvmci_3 { 358 char type[O_BUFLEN]; 359 obj->klass()->name()->as_C_string(type, O_BUFLEN); 360 markOop mark = obj->mark(); 361 TRACE_jvmci_3("%s: entered locking slow case with obj=" INTPTR_FORMAT ", type=%s, mark=" INTPTR_FORMAT ", lock=" INTPTR_FORMAT, thread->name(), p2i(obj), type, p2i(mark), p2i(lock)); 362 tty->flush(); 363 } 364 #ifdef ASSERT 365 if (PrintBiasedLockingStatistics) { 366 Atomic::inc(BiasedLocking::slow_path_entry_count_addr()); 367 } 368 #endif 369 Handle h_obj(thread, obj); 370 if (UseBiasedLocking) { 371 // Retry fast entry if bias is revoked to avoid unnecessary inflation 372 ObjectSynchronizer::fast_enter(h_obj, lock, true, CHECK); 373 } else { 374 if (JVMCIUseFastLocking) { 375 // When using fast locking, the compiled code has already tried the fast case 376 ObjectSynchronizer::slow_enter(h_obj, lock, THREAD); 377 } else { 378 ObjectSynchronizer::fast_enter(h_obj, lock, false, THREAD); 379 } 380 } 381 TRACE_jvmci_3("%s: exiting locking slow with obj=" INTPTR_FORMAT, thread->name(), p2i(obj)); 382 JRT_END 383 384 JRT_LEAF(void, JVMCIRuntime::monitorexit(JavaThread* thread, oopDesc* obj, BasicLock* lock)) 385 assert(thread == JavaThread::current(), "threads must correspond"); 386 assert(thread->last_Java_sp(), "last_Java_sp must be set"); 387 // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown 388 EXCEPTION_MARK; 389 390 #ifdef DEBUG 391 if (!oopDesc::is_oop(obj)) { 392 ResetNoHandleMark rhm; 393 nmethod* method = thread->last_frame().cb()->as_nmethod_or_null(); 394 if (method != NULL) { 395 tty->print_cr("ERROR in monitorexit in method %s wrong obj " INTPTR_FORMAT, method->name(), p2i(obj)); 396 } 397 thread->print_stack_on(tty); 398 assert(false, "invalid lock object pointer dected"); 399 } 400 #endif 401 402 if (JVMCIUseFastLocking) { 403 // When using fast locking, the compiled code has already tried the fast case 404 ObjectSynchronizer::slow_exit(obj, lock, THREAD); 405 } else { 406 ObjectSynchronizer::fast_exit(obj, lock, THREAD); 407 } 408 IF_TRACE_jvmci_3 { 409 char type[O_BUFLEN]; 410 obj->klass()->name()->as_C_string(type, O_BUFLEN); 411 TRACE_jvmci_3("%s: exited locking slow case with obj=" INTPTR_FORMAT ", type=%s, mark=" INTPTR_FORMAT ", lock=" INTPTR_FORMAT, thread->name(), p2i(obj), type, p2i(obj->mark()), p2i(lock)); 412 tty->flush(); 413 } 414 JRT_END 415 416 // Object.notify() fast path, caller does slow path 417 JRT_LEAF(jboolean, JVMCIRuntime::object_notify(JavaThread *thread, oopDesc* obj)) 418 419 // Very few notify/notifyAll operations find any threads on the waitset, so 420 // the dominant fast-path is to simply return. 421 // Relatedly, it's critical that notify/notifyAll be fast in order to 422 // reduce lock hold times. 423 if (!SafepointSynchronize::is_synchronizing()) { 424 if (ObjectSynchronizer::quick_notify(obj, thread, false)) { 425 return true; 426 } 427 } 428 return false; // caller must perform slow path 429 430 JRT_END 431 432 // Object.notifyAll() fast path, caller does slow path 433 JRT_LEAF(jboolean, JVMCIRuntime::object_notifyAll(JavaThread *thread, oopDesc* obj)) 434 435 if (!SafepointSynchronize::is_synchronizing() ) { 436 if (ObjectSynchronizer::quick_notify(obj, thread, true)) { 437 return true; 438 } 439 } 440 return false; // caller must perform slow path 441 442 JRT_END 443 444 JRT_ENTRY(void, JVMCIRuntime::throw_and_post_jvmti_exception(JavaThread* thread, const char* exception, const char* message)) 445 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 446 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message); 447 JRT_END 448 449 JRT_ENTRY(void, JVMCIRuntime::throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass)) 450 ResourceMark rm(thread); 451 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 452 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, klass->external_name()); 453 JRT_END 454 455 JRT_ENTRY(void, JVMCIRuntime::throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass)) 456 ResourceMark rm(thread); 457 const char* message = SharedRuntime::generate_class_cast_message(caster_klass, target_klass); 458 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 459 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message); 460 JRT_END 461 462 JRT_LEAF(void, JVMCIRuntime::log_object(JavaThread* thread, oopDesc* obj, bool as_string, bool newline)) 463 ttyLocker ttyl; 464 465 if (obj == NULL) { 466 tty->print("NULL"); 467 } else if (oopDesc::is_oop_or_null(obj, true) && (!as_string || !java_lang_String::is_instance(obj))) { 468 if (oopDesc::is_oop_or_null(obj, true)) { 469 char buf[O_BUFLEN]; 470 tty->print("%s@" INTPTR_FORMAT, obj->klass()->name()->as_C_string(buf, O_BUFLEN), p2i(obj)); 471 } else { 472 tty->print(INTPTR_FORMAT, p2i(obj)); 473 } 474 } else { 475 ResourceMark rm; 476 assert(obj != NULL && java_lang_String::is_instance(obj), "must be"); 477 char *buf = java_lang_String::as_utf8_string(obj); 478 tty->print_raw(buf); 479 } 480 if (newline) { 481 tty->cr(); 482 } 483 JRT_END 484 485 #if INCLUDE_G1GC 486 487 JRT_LEAF(void, JVMCIRuntime::write_barrier_pre(JavaThread* thread, oopDesc* obj)) 488 G1ThreadLocalData::satb_mark_queue(thread).enqueue(obj); 489 JRT_END 490 491 JRT_LEAF(void, JVMCIRuntime::write_barrier_post(JavaThread* thread, void* card_addr)) 492 G1ThreadLocalData::dirty_card_queue(thread).enqueue(card_addr); 493 JRT_END 494 495 #endif // INCLUDE_G1GC 496 497 JRT_LEAF(jboolean, JVMCIRuntime::validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child)) 498 bool ret = true; 499 if(!Universe::heap()->is_in_closed_subset(parent)) { 500 tty->print_cr("Parent Object " INTPTR_FORMAT " not in heap", p2i(parent)); 501 parent->print(); 502 ret=false; 503 } 504 if(!Universe::heap()->is_in_closed_subset(child)) { 505 tty->print_cr("Child Object " INTPTR_FORMAT " not in heap", p2i(child)); 506 child->print(); 507 ret=false; 508 } 509 return (jint)ret; 510 JRT_END 511 512 JRT_ENTRY(void, JVMCIRuntime::vm_error(JavaThread* thread, jlong where, jlong format, jlong value)) 513 ResourceMark rm; 514 const char *error_msg = where == 0L ? "<internal JVMCI error>" : (char*) (address) where; 515 char *detail_msg = NULL; 516 if (format != 0L) { 517 const char* buf = (char*) (address) format; 518 size_t detail_msg_length = strlen(buf) * 2; 519 detail_msg = (char *) NEW_RESOURCE_ARRAY(u_char, detail_msg_length); 520 jio_snprintf(detail_msg, detail_msg_length, buf, value); 521 report_vm_error(__FILE__, __LINE__, error_msg, "%s", detail_msg); 522 } else { 523 report_vm_error(__FILE__, __LINE__, error_msg); 524 } 525 JRT_END 526 527 JRT_LEAF(oopDesc*, JVMCIRuntime::load_and_clear_exception(JavaThread* thread)) 528 oop exception = thread->exception_oop(); 529 assert(exception != NULL, "npe"); 530 thread->set_exception_oop(NULL); 531 thread->set_exception_pc(0); 532 return exception; 533 JRT_END 534 535 PRAGMA_DIAG_PUSH 536 PRAGMA_FORMAT_NONLITERAL_IGNORED 537 JRT_LEAF(void, JVMCIRuntime::log_printf(JavaThread* thread, const char* format, jlong v1, jlong v2, jlong v3)) 538 ResourceMark rm; 539 tty->print(format, v1, v2, v3); 540 JRT_END 541 PRAGMA_DIAG_POP 542 543 static void decipher(jlong v, bool ignoreZero) { 544 if (v != 0 || !ignoreZero) { 545 void* p = (void *)(address) v; 546 CodeBlob* cb = CodeCache::find_blob(p); 547 if (cb) { 548 if (cb->is_nmethod()) { 549 char buf[O_BUFLEN]; 550 tty->print("%s [" INTPTR_FORMAT "+" JLONG_FORMAT "]", cb->as_nmethod_or_null()->method()->name_and_sig_as_C_string(buf, O_BUFLEN), p2i(cb->code_begin()), (jlong)((address)v - cb->code_begin())); 551 return; 552 } 553 cb->print_value_on(tty); 554 return; 555 } 556 if (Universe::heap()->is_in(p)) { 557 oop obj = oop(p); 558 obj->print_value_on(tty); 559 return; 560 } 561 tty->print(INTPTR_FORMAT " [long: " JLONG_FORMAT ", double %lf, char %c]",p2i((void *)v), (jlong)v, (jdouble)v, (char)v); 562 } 563 } 564 565 PRAGMA_DIAG_PUSH 566 PRAGMA_FORMAT_NONLITERAL_IGNORED 567 JRT_LEAF(void, JVMCIRuntime::vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3)) 568 ResourceMark rm; 569 const char *buf = (const char*) (address) format; 570 if (vmError) { 571 if (buf != NULL) { 572 fatal(buf, v1, v2, v3); 573 } else { 574 fatal("<anonymous error>"); 575 } 576 } else if (buf != NULL) { 577 tty->print(buf, v1, v2, v3); 578 } else { 579 assert(v2 == 0, "v2 != 0"); 580 assert(v3 == 0, "v3 != 0"); 581 decipher(v1, false); 582 } 583 JRT_END 584 PRAGMA_DIAG_POP 585 586 JRT_LEAF(void, JVMCIRuntime::log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline)) 587 union { 588 jlong l; 589 jdouble d; 590 jfloat f; 591 } uu; 592 uu.l = value; 593 switch (typeChar) { 594 case 'Z': tty->print(value == 0 ? "false" : "true"); break; 595 case 'B': tty->print("%d", (jbyte) value); break; 596 case 'C': tty->print("%c", (jchar) value); break; 597 case 'S': tty->print("%d", (jshort) value); break; 598 case 'I': tty->print("%d", (jint) value); break; 599 case 'F': tty->print("%f", uu.f); break; 600 case 'J': tty->print(JLONG_FORMAT, value); break; 601 case 'D': tty->print("%lf", uu.d); break; 602 default: assert(false, "unknown typeChar"); break; 603 } 604 if (newline) { 605 tty->cr(); 606 } 607 JRT_END 608 609 JRT_ENTRY(jint, JVMCIRuntime::identity_hash_code(JavaThread* thread, oopDesc* obj)) 610 return (jint) obj->identity_hash(); 611 JRT_END 612 613 JRT_ENTRY(jboolean, JVMCIRuntime::thread_is_interrupted(JavaThread* thread, oopDesc* receiver, jboolean clear_interrupted)) 614 Handle receiverHandle(thread, receiver); 615 // A nested ThreadsListHandle may require the Threads_lock which 616 // requires thread_in_vm which is why this method cannot be JRT_LEAF. 617 ThreadsListHandle tlh; 618 619 JavaThread* receiverThread = java_lang_Thread::thread(receiverHandle()); 620 if (receiverThread == NULL || (EnableThreadSMRExtraValidityChecks && !tlh.includes(receiverThread))) { 621 // The other thread may exit during this process, which is ok so return false. 622 return JNI_FALSE; 623 } else { 624 return (jint) Thread::is_interrupted(receiverThread, clear_interrupted != 0); 625 } 626 JRT_END 627 628 JRT_ENTRY(int, JVMCIRuntime::test_deoptimize_call_int(JavaThread* thread, int value)) 629 deopt_caller(); 630 return value; 631 JRT_END 632 633 void JVMCIRuntime::force_initialization(TRAPS) { 634 JVMCIRuntime::initialize_well_known_classes(CHECK); 635 636 ResourceMark rm; 637 TempNewSymbol getCompiler = SymbolTable::new_symbol("getCompiler", CHECK); 638 TempNewSymbol sig = SymbolTable::new_symbol("()Ljdk/vm/ci/runtime/JVMCICompiler;", CHECK); 639 Handle jvmciRuntime = JVMCIRuntime::get_HotSpotJVMCIRuntime(CHECK); 640 JavaValue result(T_OBJECT); 641 JavaCalls::call_virtual(&result, jvmciRuntime, HotSpotJVMCIRuntime::klass(), getCompiler, sig, CHECK); 642 } 643 644 // private static JVMCIRuntime JVMCI.initializeRuntime() 645 JVM_ENTRY(jobject, JVM_GetJVMCIRuntime(JNIEnv *env, jclass c)) 646 if (!EnableJVMCI) { 647 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVMCI is not enabled") 648 } 649 JVMCIRuntime::initialize_HotSpotJVMCIRuntime(CHECK_NULL); 650 jobject ret = JVMCIRuntime::get_HotSpotJVMCIRuntime_jobject(CHECK_NULL); 651 return ret; 652 JVM_END 653 654 Handle JVMCIRuntime::callStatic(const char* className, const char* methodName, const char* signature, JavaCallArguments* args, TRAPS) { 655 TempNewSymbol name = SymbolTable::new_symbol(className, CHECK_(Handle())); 656 Klass* klass = SystemDictionary::resolve_or_fail(name, true, CHECK_(Handle())); 657 TempNewSymbol runtime = SymbolTable::new_symbol(methodName, CHECK_(Handle())); 658 TempNewSymbol sig = SymbolTable::new_symbol(signature, CHECK_(Handle())); 659 JavaValue result(T_OBJECT); 660 if (args == NULL) { 661 JavaCalls::call_static(&result, klass, runtime, sig, CHECK_(Handle())); 662 } else { 663 JavaCalls::call_static(&result, klass, runtime, sig, args, CHECK_(Handle())); 664 } 665 return Handle(THREAD, (oop)result.get_jobject()); 666 } 667 668 Handle JVMCIRuntime::get_HotSpotJVMCIRuntime(TRAPS) { 669 initialize_JVMCI(CHECK_(Handle())); 670 return Handle(THREAD, JNIHandles::resolve_non_null(_HotSpotJVMCIRuntime_instance)); 671 } 672 673 void JVMCIRuntime::initialize_HotSpotJVMCIRuntime(TRAPS) { 674 guarantee(!_HotSpotJVMCIRuntime_initialized, "cannot reinitialize HotSpotJVMCIRuntime"); 675 JVMCIRuntime::initialize_well_known_classes(CHECK); 676 // This should only be called in the context of the JVMCI class being initialized 677 InstanceKlass* klass = SystemDictionary::JVMCI_klass(); 678 guarantee(klass->is_being_initialized() && klass->is_reentrant_initialization(THREAD), 679 "HotSpotJVMCIRuntime initialization should only be triggered through JVMCI initialization"); 680 681 Handle result = callStatic("jdk/vm/ci/hotspot/HotSpotJVMCIRuntime", 682 "runtime", 683 "()Ljdk/vm/ci/hotspot/HotSpotJVMCIRuntime;", NULL, CHECK); 684 int adjustment = HotSpotJVMCIRuntime::compilationLevelAdjustment(result); 685 assert(adjustment >= JVMCIRuntime::none && 686 adjustment <= JVMCIRuntime::by_full_signature, 687 "compilation level adjustment out of bounds"); 688 _comp_level_adjustment = (CompLevelAdjustment) adjustment; 689 _HotSpotJVMCIRuntime_initialized = true; 690 _HotSpotJVMCIRuntime_instance = JNIHandles::make_global(result); 691 } 692 693 void JVMCIRuntime::initialize_JVMCI(TRAPS) { 694 if (JNIHandles::resolve(_HotSpotJVMCIRuntime_instance) == NULL) { 695 callStatic("jdk/vm/ci/runtime/JVMCI", 696 "getRuntime", 697 "()Ljdk/vm/ci/runtime/JVMCIRuntime;", NULL, CHECK); 698 } 699 assert(_HotSpotJVMCIRuntime_initialized == true, "what?"); 700 } 701 702 bool JVMCIRuntime::can_initialize_JVMCI() { 703 // Initializing JVMCI requires the module system to be initialized past phase 3. 704 // The JVMCI API itself isn't available until phase 2 and ServiceLoader (which 705 // JVMCI initialization requires) isn't usable until after phase 3. Testing 706 // whether the system loader is initialized satisfies all these invariants. 707 if (SystemDictionary::java_system_loader() == NULL) { 708 return false; 709 } 710 assert(Universe::is_module_initialized(), "must be"); 711 return true; 712 } 713 714 void JVMCIRuntime::initialize_well_known_classes(TRAPS) { 715 if (JVMCIRuntime::_well_known_classes_initialized == false) { 716 guarantee(can_initialize_JVMCI(), "VM is not yet sufficiently booted to initialize JVMCI"); 717 SystemDictionary::WKID scan = SystemDictionary::FIRST_JVMCI_WKID; 718 SystemDictionary::resolve_wk_klasses_through(SystemDictionary::LAST_JVMCI_WKID, scan, CHECK); 719 JVMCIJavaClasses::compute_offsets(CHECK); 720 JVMCIRuntime::_well_known_classes_initialized = true; 721 } 722 } 723 724 void JVMCIRuntime::metadata_do(void f(Metadata*)) { 725 // For simplicity, the existence of HotSpotJVMCIMetaAccessContext in 726 // the SystemDictionary well known classes should ensure the other 727 // classes have already been loaded, so make sure their order in the 728 // table enforces that. 729 assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotResolvedJavaMethodImpl) < 730 SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier"); 731 assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotConstantPool) < 732 SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier"); 733 assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotResolvedObjectTypeImpl) < 734 SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier"); 735 736 if (HotSpotJVMCIMetaAccessContext::klass() == NULL || 737 !HotSpotJVMCIMetaAccessContext::klass()->is_linked()) { 738 // Nothing could be registered yet 739 return; 740 } 741 742 // WeakReference<HotSpotJVMCIMetaAccessContext>[] 743 objArrayOop allContexts = HotSpotJVMCIMetaAccessContext::allContexts(); 744 if (allContexts == NULL) { 745 return; 746 } 747 748 // These must be loaded at this point but the linking state doesn't matter. 749 assert(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass() != NULL, "must be loaded"); 750 assert(SystemDictionary::HotSpotConstantPool_klass() != NULL, "must be loaded"); 751 assert(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass() != NULL, "must be loaded"); 752 753 for (int i = 0; i < allContexts->length(); i++) { 754 oop ref = allContexts->obj_at(i); 755 if (ref != NULL) { 756 oop referent = java_lang_ref_Reference::referent(ref); 757 if (referent != NULL) { 758 // Chunked Object[] with last element pointing to next chunk 759 objArrayOop metadataRoots = HotSpotJVMCIMetaAccessContext::metadataRoots(referent); 760 while (metadataRoots != NULL) { 761 for (int typeIndex = 0; typeIndex < metadataRoots->length() - 1; typeIndex++) { 762 oop reference = metadataRoots->obj_at(typeIndex); 763 if (reference == NULL) { 764 continue; 765 } 766 oop metadataRoot = java_lang_ref_Reference::referent(reference); 767 if (metadataRoot == NULL) { 768 continue; 769 } 770 if (metadataRoot->is_a(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass())) { 771 Method* method = CompilerToVM::asMethod(metadataRoot); 772 f(method); 773 } else if (metadataRoot->is_a(SystemDictionary::HotSpotConstantPool_klass())) { 774 ConstantPool* constantPool = CompilerToVM::asConstantPool(metadataRoot); 775 f(constantPool); 776 } else if (metadataRoot->is_a(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass())) { 777 Klass* klass = CompilerToVM::asKlass(metadataRoot); 778 f(klass); 779 } else { 780 metadataRoot->print(); 781 ShouldNotReachHere(); 782 } 783 } 784 metadataRoots = (objArrayOop)metadataRoots->obj_at(metadataRoots->length() - 1); 785 assert(metadataRoots == NULL || metadataRoots->is_objArray(), "wrong type"); 786 } 787 } 788 } 789 } 790 } 791 792 // private static void CompilerToVM.registerNatives() 793 JVM_ENTRY(void, JVM_RegisterJVMCINatives(JNIEnv *env, jclass c2vmClass)) 794 if (!EnableJVMCI) { 795 THROW_MSG(vmSymbols::java_lang_InternalError(), "JVMCI is not enabled"); 796 } 797 798 #ifdef _LP64 799 #ifndef SPARC 800 uintptr_t heap_end = (uintptr_t) Universe::heap()->reserved_region().end(); 801 uintptr_t allocation_end = heap_end + ((uintptr_t)16) * 1024 * 1024 * 1024; 802 guarantee(heap_end < allocation_end, "heap end too close to end of address space (might lead to erroneous TLAB allocations)"); 803 #endif // !SPARC 804 #else 805 fatal("check TLAB allocation code for address space conflicts"); 806 #endif // _LP64 807 808 JVMCIRuntime::initialize_well_known_classes(CHECK); 809 810 { 811 ThreadToNativeFromVM trans(thread); 812 env->RegisterNatives(c2vmClass, CompilerToVM::methods, CompilerToVM::methods_count()); 813 } 814 JVM_END 815 816 void JVMCIRuntime::shutdown(TRAPS) { 817 if (_HotSpotJVMCIRuntime_instance != NULL) { 818 _shutdown_called = true; 819 HandleMark hm(THREAD); 820 Handle receiver = get_HotSpotJVMCIRuntime(CHECK); 821 JavaValue result(T_VOID); 822 JavaCallArguments args; 823 args.push_oop(receiver); 824 JavaCalls::call_special(&result, receiver->klass(), vmSymbols::shutdown_method_name(), vmSymbols::void_method_signature(), &args, CHECK); 825 } 826 } 827 828 CompLevel JVMCIRuntime::adjust_comp_level_inner(const methodHandle& method, bool is_osr, CompLevel level, JavaThread* thread) { 829 JVMCICompiler* compiler = JVMCICompiler::instance(false, thread); 830 if (compiler != NULL && compiler->is_bootstrapping()) { 831 return level; 832 } 833 if (!is_HotSpotJVMCIRuntime_initialized() || _comp_level_adjustment == JVMCIRuntime::none) { 834 // JVMCI cannot participate in compilation scheduling until 835 // JVMCI is initialized and indicates it wants to participate. 836 return level; 837 } 838 839 #define CHECK_RETURN THREAD); \ 840 if (HAS_PENDING_EXCEPTION) { \ 841 Handle exception(THREAD, PENDING_EXCEPTION); \ 842 CLEAR_PENDING_EXCEPTION; \ 843 \ 844 if (exception->is_a(SystemDictionary::ThreadDeath_klass())) { \ 845 /* In the special case of ThreadDeath, we need to reset the */ \ 846 /* pending async exception so that it is propagated. */ \ 847 thread->set_pending_async_exception(exception()); \ 848 return level; \ 849 } \ 850 tty->print("Uncaught exception while adjusting compilation level: "); \ 851 java_lang_Throwable::print(exception(), tty); \ 852 tty->cr(); \ 853 java_lang_Throwable::print_stack_trace(exception, tty); \ 854 if (HAS_PENDING_EXCEPTION) { \ 855 CLEAR_PENDING_EXCEPTION; \ 856 } \ 857 return level; \ 858 } \ 859 (void)(0 860 861 862 Thread* THREAD = thread; 863 HandleMark hm; 864 Handle receiver = JVMCIRuntime::get_HotSpotJVMCIRuntime(CHECK_RETURN); 865 Handle name; 866 Handle sig; 867 if (_comp_level_adjustment == JVMCIRuntime::by_full_signature) { 868 name = java_lang_String::create_from_symbol(method->name(), CHECK_RETURN); 869 sig = java_lang_String::create_from_symbol(method->signature(), CHECK_RETURN); 870 } else { 871 name = Handle(); 872 sig = Handle(); 873 } 874 875 JavaValue result(T_INT); 876 JavaCallArguments args; 877 args.push_oop(receiver); 878 args.push_oop(Handle(THREAD, method->method_holder()->java_mirror())); 879 args.push_oop(name); 880 args.push_oop(sig); 881 args.push_int(is_osr); 882 args.push_int(level); 883 JavaCalls::call_special(&result, receiver->klass(), vmSymbols::adjustCompilationLevel_name(), 884 vmSymbols::adjustCompilationLevel_signature(), &args, CHECK_RETURN); 885 886 int comp_level = result.get_jint(); 887 if (comp_level < CompLevel_none || comp_level > CompLevel_full_optimization) { 888 assert(false, "compilation level out of bounds"); 889 return level; 890 } 891 return (CompLevel) comp_level; 892 #undef CHECK_RETURN 893 } 894 895 void JVMCIRuntime::bootstrap_finished(TRAPS) { 896 HandleMark hm(THREAD); 897 Handle receiver = get_HotSpotJVMCIRuntime(CHECK); 898 JavaValue result(T_VOID); 899 JavaCallArguments args; 900 args.push_oop(receiver); 901 JavaCalls::call_special(&result, receiver->klass(), vmSymbols::bootstrapFinished_method_name(), vmSymbols::void_method_signature(), &args, CHECK); 902 }