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