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