1 /* 2 * Copyright (c) 1997, 2017, 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 #ifndef SHARE_VM_RUNTIME_INTERFACESUPPORT_HPP 26 #define SHARE_VM_RUNTIME_INTERFACESUPPORT_HPP 27 28 #include "gc/shared/gcLocker.hpp" 29 #include "runtime/handles.inline.hpp" 30 #include "runtime/mutexLocker.hpp" 31 #include "runtime/orderAccess.hpp" 32 #include "runtime/os.hpp" 33 #include "runtime/safepoint.hpp" 34 #include "runtime/thread.inline.hpp" 35 #include "runtime/vmThread.hpp" 36 #include "utilities/globalDefinitions.hpp" 37 #include "utilities/macros.hpp" 38 #include "utilities/preserveException.hpp" 39 40 // Wrapper for all entry points to the virtual machine. 41 // The HandleMarkCleaner is a faster version of HandleMark. 42 // It relies on the fact that there is a HandleMark further 43 // down the stack (in JavaCalls::call_helper), and just resets 44 // to the saved values in that HandleMark. 45 46 class HandleMarkCleaner: public StackObj { 47 private: 48 Thread* _thread; 49 public: 50 HandleMarkCleaner(Thread* thread) { 51 _thread = thread; 52 _thread->last_handle_mark()->push(); 53 } 54 ~HandleMarkCleaner() { 55 _thread->last_handle_mark()->pop_and_restore(); 56 } 57 58 private: 59 inline void* operator new(size_t size, void* ptr) throw() { 60 return ptr; 61 } 62 }; 63 64 // InterfaceSupport provides functionality used by the VM_LEAF_BASE and 65 // VM_ENTRY_BASE macros. These macros are used to guard entry points into 66 // the VM and perform checks upon leave of the VM. 67 68 69 class InterfaceSupport: AllStatic { 70 # ifdef ASSERT 71 public: 72 static long _scavenge_alot_counter; 73 static long _fullgc_alot_counter; 74 static long _number_of_calls; 75 static long _fullgc_alot_invocation; 76 77 // Helper methods used to implement +ScavengeALot and +FullGCALot 78 static void check_gc_alot() { if (ScavengeALot || FullGCALot) gc_alot(); } 79 static void gc_alot(); 80 81 static void walk_stack_from(vframe* start_vf); 82 static void walk_stack(); 83 84 static void zombieAll(); 85 static void unlinkSymbols(); 86 static void deoptimizeAll(); 87 static void stress_derived_pointers(); 88 static void verify_stack(); 89 static void verify_last_frame(); 90 # endif 91 92 public: 93 // OS dependent stuff 94 95 #include OS_HEADER(interfaceSupport) 96 97 }; 98 99 100 // Basic class for all thread transition classes. 101 102 class ThreadStateTransition : public StackObj { 103 protected: 104 JavaThread* _thread; 105 public: 106 ThreadStateTransition(JavaThread *thread) { 107 _thread = thread; 108 assert(thread != NULL && thread->is_Java_thread(), "must be Java thread"); 109 } 110 111 // Change threadstate in a manner, so safepoint can detect changes. 112 // Time-critical: called on exit from every runtime routine 113 static inline void transition(JavaThread *thread, JavaThreadState from, JavaThreadState to) { 114 assert(from != _thread_in_Java, "use transition_from_java"); 115 assert(from != _thread_in_native, "use transition_from_native"); 116 assert((from & 1) == 0 && (to & 1) == 0, "odd numbers are transitions states"); 117 assert(thread->thread_state() == from, "coming from wrong thread state"); 118 // Change to transition state 119 thread->set_thread_state((JavaThreadState)(from + 1)); 120 121 // Make sure new state is seen by VM thread 122 if (os::is_MP()) { 123 if (UseMembar) { 124 // Force a fence between the write above and read below 125 OrderAccess::fence(); 126 } else { 127 // store to serialize page so VM thread can do pseudo remote membar 128 os::write_memory_serialize_page(thread); 129 } 130 } 131 132 if (SafepointSynchronize::do_call_back()) { 133 SafepointSynchronize::block(thread); 134 } 135 thread->set_thread_state(to); 136 137 CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();) 138 } 139 140 // transition_and_fence must be used on any thread state transition 141 // where there might not be a Java call stub on the stack, in 142 // particular on Windows where the Structured Exception Handler is 143 // set up in the call stub. os::write_memory_serialize_page() can 144 // fault and we can't recover from it on Windows without a SEH in 145 // place. 146 static inline void transition_and_fence(JavaThread *thread, JavaThreadState from, JavaThreadState to) { 147 assert(thread->thread_state() == from, "coming from wrong thread state"); 148 assert((from & 1) == 0 && (to & 1) == 0, "odd numbers are transitions states"); 149 // Change to transition state 150 thread->set_thread_state((JavaThreadState)(from + 1)); 151 152 // Make sure new state is seen by VM thread 153 if (os::is_MP()) { 154 if (UseMembar) { 155 // Force a fence between the write above and read below 156 OrderAccess::fence(); 157 } else { 158 // Must use this rather than serialization page in particular on Windows 159 InterfaceSupport::serialize_memory(thread); 160 } 161 } 162 163 if (SafepointSynchronize::do_call_back()) { 164 SafepointSynchronize::block(thread); 165 } 166 thread->set_thread_state(to); 167 168 CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();) 169 } 170 171 // Same as above, but assumes from = _thread_in_Java. This is simpler, since we 172 // never block on entry to the VM. This will break the code, since e.g. preserve arguments 173 // have not been setup. 174 static inline void transition_from_java(JavaThread *thread, JavaThreadState to) { 175 assert(thread->thread_state() == _thread_in_Java, "coming from wrong thread state"); 176 thread->set_thread_state(to); 177 } 178 179 static inline void transition_from_native(JavaThread *thread, JavaThreadState to) { 180 assert((to & 1) == 0, "odd numbers are transitions states"); 181 assert(thread->thread_state() == _thread_in_native, "coming from wrong thread state"); 182 // Change to transition state 183 thread->set_thread_state(_thread_in_native_trans); 184 185 // Make sure new state is seen by GC thread 186 if (os::is_MP()) { 187 if (UseMembar) { 188 // Force a fence between the write above and read below 189 OrderAccess::fence(); 190 } else { 191 // Must use this rather than serialization page in particular on Windows 192 InterfaceSupport::serialize_memory(thread); 193 } 194 } 195 196 // We never install asynchronous exceptions when coming (back) in 197 // to the runtime from native code because the runtime is not set 198 // up to handle exceptions floating around at arbitrary points. 199 if (SafepointSynchronize::do_call_back() || thread->is_suspend_after_native()) { 200 JavaThread::check_safepoint_and_suspend_for_native_trans(thread); 201 202 // Clear unhandled oops anywhere where we could block, even if we don't. 203 CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();) 204 } 205 206 thread->set_thread_state(to); 207 } 208 protected: 209 void trans(JavaThreadState from, JavaThreadState to) { transition(_thread, from, to); } 210 void trans_from_java(JavaThreadState to) { transition_from_java(_thread, to); } 211 void trans_from_native(JavaThreadState to) { transition_from_native(_thread, to); } 212 void trans_and_fence(JavaThreadState from, JavaThreadState to) { transition_and_fence(_thread, from, to); } 213 }; 214 215 216 class ThreadInVMfromJava : public ThreadStateTransition { 217 public: 218 ThreadInVMfromJava(JavaThread* thread) : ThreadStateTransition(thread) { 219 trans_from_java(_thread_in_vm); 220 } 221 ~ThreadInVMfromJava() { 222 if (_thread->stack_yellow_reserved_zone_disabled()) { 223 _thread->enable_stack_yellow_reserved_zone(); 224 } 225 trans(_thread_in_vm, _thread_in_Java); 226 // Check for pending. async. exceptions or suspends. 227 if (_thread->has_special_runtime_exit_condition()) _thread->handle_special_runtime_exit_condition(); 228 } 229 }; 230 231 232 class ThreadInVMfromUnknown { 233 private: 234 JavaThread* _thread; 235 public: 236 ThreadInVMfromUnknown() : _thread(NULL) { 237 Thread* t = Thread::current(); 238 if (t->is_Java_thread()) { 239 JavaThread* t2 = (JavaThread*) t; 240 if (t2->thread_state() == _thread_in_native) { 241 _thread = t2; 242 ThreadStateTransition::transition_from_native(t2, _thread_in_vm); 243 // Used to have a HandleMarkCleaner but that is dangerous as 244 // it could free a handle in our (indirect, nested) caller. 245 // We expect any handles will be short lived and figure we 246 // don't need an actual HandleMark. 247 } 248 } 249 } 250 ~ThreadInVMfromUnknown() { 251 if (_thread) { 252 ThreadStateTransition::transition_and_fence(_thread, _thread_in_vm, _thread_in_native); 253 } 254 } 255 }; 256 257 258 class ThreadInVMfromNative : public ThreadStateTransition { 259 public: 260 ThreadInVMfromNative(JavaThread* thread) : ThreadStateTransition(thread) { 261 trans_from_native(_thread_in_vm); 262 } 263 ~ThreadInVMfromNative() { 264 trans_and_fence(_thread_in_vm, _thread_in_native); 265 } 266 }; 267 268 269 class ThreadToNativeFromVM : public ThreadStateTransition { 270 public: 271 ThreadToNativeFromVM(JavaThread *thread) : ThreadStateTransition(thread) { 272 // We are leaving the VM at this point and going directly to native code. 273 // Block, if we are in the middle of a safepoint synchronization. 274 assert(!thread->owns_locks(), "must release all locks when leaving VM"); 275 thread->frame_anchor()->make_walkable(thread); 276 trans_and_fence(_thread_in_vm, _thread_in_native); 277 // Check for pending. async. exceptions or suspends. 278 if (_thread->has_special_runtime_exit_condition()) _thread->handle_special_runtime_exit_condition(false); 279 } 280 281 ~ThreadToNativeFromVM() { 282 trans_from_native(_thread_in_vm); 283 assert(!_thread->is_pending_jni_exception_check(), "Pending JNI Exception Check"); 284 // We don't need to clear_walkable because it will happen automagically when we return to java 285 } 286 }; 287 288 289 class ThreadBlockInVM : public ThreadStateTransition { 290 public: 291 ThreadBlockInVM(JavaThread *thread) 292 : ThreadStateTransition(thread) { 293 // Once we are blocked vm expects stack to be walkable 294 thread->frame_anchor()->make_walkable(thread); 295 trans_and_fence(_thread_in_vm, _thread_blocked); 296 } 297 ~ThreadBlockInVM() { 298 trans_and_fence(_thread_blocked, _thread_in_vm); 299 // We don't need to clear_walkable because it will happen automagically when we return to java 300 } 301 }; 302 303 304 // This special transition class is only used to prevent asynchronous exceptions 305 // from being installed on vm exit in situations where we can't tolerate them. 306 // See bugs: 4324348, 4854693, 4998314, 5040492, 5050705. 307 class ThreadInVMfromJavaNoAsyncException : public ThreadStateTransition { 308 public: 309 ThreadInVMfromJavaNoAsyncException(JavaThread* thread) : ThreadStateTransition(thread) { 310 trans_from_java(_thread_in_vm); 311 } 312 ~ThreadInVMfromJavaNoAsyncException() { 313 if (_thread->stack_yellow_reserved_zone_disabled()) { 314 _thread->enable_stack_yellow_reserved_zone(); 315 } 316 trans(_thread_in_vm, _thread_in_Java); 317 // NOTE: We do not check for pending. async. exceptions. 318 // If we did and moved the pending async exception over into the 319 // pending exception field, we would need to deopt (currently C2 320 // only). However, to do so would require that we transition back 321 // to the _thread_in_vm state. Instead we postpone the handling of 322 // the async exception. 323 324 325 // Check for pending. suspends only. 326 if (_thread->has_special_runtime_exit_condition()) 327 _thread->handle_special_runtime_exit_condition(false); 328 } 329 }; 330 331 // Debug class instantiated in JRT_ENTRY and ITR_ENTRY macro. 332 // Can be used to verify properties on enter/exit of the VM. 333 334 #ifdef ASSERT 335 class VMEntryWrapper { 336 public: 337 VMEntryWrapper() { 338 if (VerifyLastFrame) { 339 InterfaceSupport::verify_last_frame(); 340 } 341 } 342 343 ~VMEntryWrapper() { 344 InterfaceSupport::check_gc_alot(); 345 if (WalkStackALot) { 346 InterfaceSupport::walk_stack(); 347 } 348 #ifdef COMPILER2 349 // This option is not used by Compiler 1 350 if (StressDerivedPointers) { 351 InterfaceSupport::stress_derived_pointers(); 352 } 353 #endif 354 if (DeoptimizeALot || DeoptimizeRandom) { 355 InterfaceSupport::deoptimizeAll(); 356 } 357 if (ZombieALot) { 358 InterfaceSupport::zombieAll(); 359 } 360 if (UnlinkSymbolsALot) { 361 InterfaceSupport::unlinkSymbols(); 362 } 363 // do verification AFTER potential deoptimization 364 if (VerifyStack) { 365 InterfaceSupport::verify_stack(); 366 } 367 368 } 369 }; 370 371 372 class VMNativeEntryWrapper { 373 public: 374 VMNativeEntryWrapper() { 375 if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot(); 376 } 377 378 ~VMNativeEntryWrapper() { 379 if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot(); 380 } 381 }; 382 383 #endif 384 385 386 // VM-internal runtime interface support 387 388 #ifdef ASSERT 389 390 class RuntimeHistogramElement : public HistogramElement { 391 public: 392 RuntimeHistogramElement(const char* name); 393 }; 394 395 #define TRACE_CALL(result_type, header) \ 396 InterfaceSupport::_number_of_calls++; \ 397 if (CountRuntimeCalls) { \ 398 static RuntimeHistogramElement* e = new RuntimeHistogramElement(#header); \ 399 if (e != NULL) e->increment_count(); \ 400 } 401 #else 402 #define TRACE_CALL(result_type, header) \ 403 /* do nothing */ 404 #endif 405 406 407 // LEAF routines do not lock, GC or throw exceptions 408 409 #define VM_LEAF_BASE(result_type, header) \ 410 TRACE_CALL(result_type, header) \ 411 debug_only(NoHandleMark __hm;) \ 412 os::verify_stack_alignment(); \ 413 /* begin of body */ 414 415 #define VM_ENTRY_BASE_FROM_LEAF(result_type, header, thread) \ 416 TRACE_CALL(result_type, header) \ 417 debug_only(ResetNoHandleMark __rnhm;) \ 418 HandleMarkCleaner __hm(thread); \ 419 Thread* THREAD = thread; \ 420 os::verify_stack_alignment(); \ 421 /* begin of body */ 422 423 424 // ENTRY routines may lock, GC and throw exceptions 425 426 #define VM_ENTRY_BASE(result_type, header, thread) \ 427 TRACE_CALL(result_type, header) \ 428 HandleMarkCleaner __hm(thread); \ 429 Thread* THREAD = thread; \ 430 os::verify_stack_alignment(); \ 431 /* begin of body */ 432 433 434 // QUICK_ENTRY routines behave like ENTRY but without a handle mark 435 436 #define VM_QUICK_ENTRY_BASE(result_type, header, thread) \ 437 TRACE_CALL(result_type, header) \ 438 debug_only(NoHandleMark __hm;) \ 439 Thread* THREAD = thread; \ 440 os::verify_stack_alignment(); \ 441 /* begin of body */ 442 443 444 // Definitions for IRT (Interpreter Runtime) 445 // (thread is an argument passed in to all these routines) 446 447 #define IRT_ENTRY(result_type, header) \ 448 result_type header { \ 449 ThreadInVMfromJava __tiv(thread); \ 450 VM_ENTRY_BASE(result_type, header, thread) \ 451 debug_only(VMEntryWrapper __vew;) 452 453 454 #define IRT_LEAF(result_type, header) \ 455 result_type header { \ 456 VM_LEAF_BASE(result_type, header) \ 457 debug_only(NoSafepointVerifier __nspv(true);) 458 459 460 #define IRT_ENTRY_NO_ASYNC(result_type, header) \ 461 result_type header { \ 462 ThreadInVMfromJavaNoAsyncException __tiv(thread); \ 463 VM_ENTRY_BASE(result_type, header, thread) \ 464 debug_only(VMEntryWrapper __vew;) 465 466 #define IRT_END } 467 468 469 // Definitions for JRT (Java (Compiler/Shared) Runtime) 470 471 #define JRT_ENTRY(result_type, header) \ 472 result_type header { \ 473 ThreadInVMfromJava __tiv(thread); \ 474 VM_ENTRY_BASE(result_type, header, thread) \ 475 debug_only(VMEntryWrapper __vew;) 476 477 478 #define JRT_LEAF(result_type, header) \ 479 result_type header { \ 480 VM_LEAF_BASE(result_type, header) \ 481 debug_only(JRTLeafVerifier __jlv;) 482 483 484 #define JRT_ENTRY_NO_ASYNC(result_type, header) \ 485 result_type header { \ 486 ThreadInVMfromJavaNoAsyncException __tiv(thread); \ 487 VM_ENTRY_BASE(result_type, header, thread) \ 488 debug_only(VMEntryWrapper __vew;) 489 490 // Same as JRT Entry but allows for return value after the safepoint 491 // to get back into Java from the VM 492 #define JRT_BLOCK_ENTRY(result_type, header) \ 493 result_type header { \ 494 TRACE_CALL(result_type, header) \ 495 HandleMarkCleaner __hm(thread); 496 497 #define JRT_BLOCK \ 498 { \ 499 ThreadInVMfromJava __tiv(thread); \ 500 Thread* THREAD = thread; \ 501 debug_only(VMEntryWrapper __vew;) 502 503 #define JRT_BLOCK_NO_ASYNC \ 504 { \ 505 ThreadInVMfromJavaNoAsyncException __tiv(thread); \ 506 Thread* THREAD = thread; \ 507 debug_only(VMEntryWrapper __vew;) 508 509 #define JRT_BLOCK_END } 510 511 #define JRT_END } 512 513 // Definitions for JNI 514 515 #define JNI_ENTRY(result_type, header) \ 516 JNI_ENTRY_NO_PRESERVE(result_type, header) \ 517 WeakPreserveExceptionMark __wem(thread); 518 519 #define JNI_ENTRY_NO_PRESERVE(result_type, header) \ 520 extern "C" { \ 521 result_type JNICALL header { \ 522 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 523 assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \ 524 ThreadInVMfromNative __tiv(thread); \ 525 debug_only(VMNativeEntryWrapper __vew;) \ 526 VM_ENTRY_BASE(result_type, header, thread) 527 528 529 // Ensure that the VMNativeEntryWrapper constructor, which can cause 530 // a GC, is called outside the NoHandleMark (set via VM_QUICK_ENTRY_BASE). 531 #define JNI_QUICK_ENTRY(result_type, header) \ 532 extern "C" { \ 533 result_type JNICALL header { \ 534 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 535 assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \ 536 ThreadInVMfromNative __tiv(thread); \ 537 debug_only(VMNativeEntryWrapper __vew;) \ 538 VM_QUICK_ENTRY_BASE(result_type, header, thread) 539 540 541 #define JNI_LEAF(result_type, header) \ 542 extern "C" { \ 543 result_type JNICALL header { \ 544 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 545 assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \ 546 VM_LEAF_BASE(result_type, header) 547 548 549 // Close the routine and the extern "C" 550 #define JNI_END } } 551 552 553 554 // Definitions for JVM 555 556 #define JVM_ENTRY(result_type, header) \ 557 extern "C" { \ 558 result_type JNICALL header { \ 559 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 560 ThreadInVMfromNative __tiv(thread); \ 561 debug_only(VMNativeEntryWrapper __vew;) \ 562 VM_ENTRY_BASE(result_type, header, thread) 563 564 565 #define JVM_ENTRY_NO_ENV(result_type, header) \ 566 extern "C" { \ 567 result_type JNICALL header { \ 568 JavaThread* thread = JavaThread::current(); \ 569 ThreadInVMfromNative __tiv(thread); \ 570 debug_only(VMNativeEntryWrapper __vew;) \ 571 VM_ENTRY_BASE(result_type, header, thread) 572 573 574 #define JVM_QUICK_ENTRY(result_type, header) \ 575 extern "C" { \ 576 result_type JNICALL header { \ 577 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 578 ThreadInVMfromNative __tiv(thread); \ 579 debug_only(VMNativeEntryWrapper __vew;) \ 580 VM_QUICK_ENTRY_BASE(result_type, header, thread) 581 582 583 #define JVM_LEAF(result_type, header) \ 584 extern "C" { \ 585 result_type JNICALL header { \ 586 VM_Exit::block_if_vm_exited(); \ 587 VM_LEAF_BASE(result_type, header) 588 589 590 #define JVM_ENTRY_FROM_LEAF(env, result_type, header) \ 591 { { \ 592 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 593 ThreadInVMfromNative __tiv(thread); \ 594 debug_only(VMNativeEntryWrapper __vew;) \ 595 VM_ENTRY_BASE_FROM_LEAF(result_type, header, thread) 596 597 598 #define JVM_END } } 599 600 #endif // SHARE_VM_RUNTIME_INTERFACESUPPORT_HPP