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