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