15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "ci/ciReplay.hpp" 28 #include "classfile/altHashing.hpp" 29 #include "classfile/classLoader.hpp" 30 #include "classfile/javaClasses.hpp" 31 #include "classfile/symbolTable.hpp" 32 #include "classfile/systemDictionary.hpp" 33 #include "classfile/vmSymbols.hpp" 34 #include "interpreter/linkResolver.hpp" 35 #include "jfr/jfrEvents.hpp" 36 #include "jfr/support/jfrThreadId.hpp" 37 #include "utilities/macros.hpp" 38 #include "utilities/ostream.hpp" 39 #if INCLUDE_ALL_GCS 40 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp" 41 #endif // INCLUDE_ALL_GCS 42 #include "memory/allocation.hpp" 43 #include "memory/allocation.inline.hpp" 44 #include "memory/gcLocker.inline.hpp" 45 #include "memory/oopFactory.hpp" 46 #include "memory/universe.inline.hpp" 47 #include "oops/instanceKlass.hpp" 48 #include "oops/instanceOop.hpp" 49 #include "oops/markOop.hpp" 50 #include "oops/method.hpp" 51 #include "oops/objArrayKlass.hpp" 52 #include "oops/objArrayOop.hpp" 53 #include "oops/oop.inline.hpp" 54 #include "oops/symbol.hpp" 55 #include "oops/typeArrayKlass.hpp" 56 #include "oops/typeArrayOop.hpp" 5003 func = JNI_FastGetField::generate_fast_get_double_field(); 5004 if (func != (address)-1) { 5005 jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func; 5006 } 5007 } 5008 } 5009 5010 // Returns the function structure 5011 struct JNINativeInterface_* jni_functions() { 5012 #if INCLUDE_JNI_CHECK 5013 if (CheckJNICalls) return jni_functions_check(); 5014 #endif // INCLUDE_JNI_CHECK 5015 return &jni_NativeInterface; 5016 } 5017 5018 // Returns the function structure 5019 struct JNINativeInterface_* jni_functions_nocheck() { 5020 return &jni_NativeInterface; 5021 } 5022 5023 static void post_thread_start_event(const JavaThread* jt) { 5024 assert(jt != NULL, "invariant"); 5025 EventThreadStart event; 5026 if (event.should_commit()) { 5027 event.set_thread(JFR_THREAD_ID(jt)); 5028 event.commit(); 5029 } 5030 } 5031 5032 // Invocation API 5033 5034 5035 // Forward declaration 5036 extern const struct JNIInvokeInterface_ jni_InvokeInterface; 5037 5038 // Global invocation API vars 5039 volatile jint vm_created = 0; 5040 // Indicate whether it is safe to recreate VM 5041 volatile jint safe_to_recreate_vm = 1; 5042 struct JavaVM_ main_vm = {&jni_InvokeInterface}; 5043 5044 5045 #define JAVASTACKSIZE (400 * 1024) /* Default size of a thread java stack */ 5046 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL }; 5047 5048 #ifndef USDT2 5049 HS_DTRACE_PROBE_DECL1(hotspot_jni, GetDefaultJavaVMInitArgs__entry, void*); 5050 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint); 5233 * sets safe_to_recreate_vm to 1, such that any new call to 5234 * JNI_CreateJavaVM will immediately fail using the above logic. 5235 */ 5236 bool can_try_again = true; 5237 5238 result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again); 5239 if (result == JNI_OK) { 5240 JavaThread *thread = JavaThread::current(); 5241 /* thread is thread_in_vm here */ 5242 *vm = (JavaVM *)(&main_vm); 5243 *(JNIEnv**)penv = thread->jni_environment(); 5244 5245 // Tracks the time application was running before GC 5246 RuntimeService::record_application_start(); 5247 5248 // Notify JVMTI 5249 if (JvmtiExport::should_post_thread_life()) { 5250 JvmtiExport::post_thread_start(thread); 5251 } 5252 5253 post_thread_start_event(thread); 5254 5255 #ifndef PRODUCT 5256 #ifndef CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED 5257 #define CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(f) f() 5258 #endif 5259 5260 // Check if we should compile all classes on bootclasspath 5261 if (CompileTheWorld) ClassLoader::compile_the_world(); 5262 if (ReplayCompiles) ciReplay::replay(thread); 5263 5264 // Some platforms (like Win*) need a wrapper around these test 5265 // functions in order to properly handle error conditions. 5266 CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(test_error_handler); 5267 CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(execute_internal_vm_tests); 5268 #endif 5269 5270 // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving. 5271 ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native); 5272 } else { 5273 if (can_try_again) { 5444 if (attach_failed) { 5445 // Added missing cleanup 5446 thread->cleanup_failed_attach_current_thread(); 5447 return JNI_ERR; 5448 } 5449 5450 // mark the thread as no longer attaching 5451 // this uses a fence to push the change through so we don't have 5452 // to regrab the threads_lock 5453 thread->set_done_attaching_via_jni(); 5454 5455 // Set java thread status. 5456 java_lang_Thread::set_thread_status(thread->threadObj(), 5457 java_lang_Thread::RUNNABLE); 5458 5459 // Notify the debugger 5460 if (JvmtiExport::should_post_thread_life()) { 5461 JvmtiExport::post_thread_start(thread); 5462 } 5463 5464 post_thread_start_event(thread); 5465 5466 *(JNIEnv**)penv = thread->jni_environment(); 5467 5468 // Now leaving the VM, so change thread_state. This is normally automatically taken care 5469 // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by 5470 // using ThreadStateTransition::transition, we do a callback to the safepoint code if 5471 // needed. 5472 5473 ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native); 5474 5475 // Perform any platform dependent FPU setup 5476 os::setup_fpu(); 5477 5478 return JNI_OK; 5479 } 5480 5481 5482 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) { 5483 #ifndef USDT2 5484 DTRACE_PROBE3(hotspot_jni, AttachCurrentThread__entry, vm, penv, _args); | 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "ci/ciReplay.hpp" 28 #include "classfile/altHashing.hpp" 29 #include "classfile/classLoader.hpp" 30 #include "classfile/javaClasses.hpp" 31 #include "classfile/symbolTable.hpp" 32 #include "classfile/systemDictionary.hpp" 33 #include "classfile/vmSymbols.hpp" 34 #include "interpreter/linkResolver.hpp" 35 #if INCLUDE_JFR 36 #include "jfr/jfrEvents.hpp" 37 #include "jfr/support/jfrThreadId.hpp" 38 #endif 39 #include "utilities/macros.hpp" 40 #include "utilities/ostream.hpp" 41 #if INCLUDE_ALL_GCS 42 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp" 43 #endif // INCLUDE_ALL_GCS 44 #include "memory/allocation.hpp" 45 #include "memory/allocation.inline.hpp" 46 #include "memory/gcLocker.inline.hpp" 47 #include "memory/oopFactory.hpp" 48 #include "memory/universe.inline.hpp" 49 #include "oops/instanceKlass.hpp" 50 #include "oops/instanceOop.hpp" 51 #include "oops/markOop.hpp" 52 #include "oops/method.hpp" 53 #include "oops/objArrayKlass.hpp" 54 #include "oops/objArrayOop.hpp" 55 #include "oops/oop.inline.hpp" 56 #include "oops/symbol.hpp" 57 #include "oops/typeArrayKlass.hpp" 58 #include "oops/typeArrayOop.hpp" 5005 func = JNI_FastGetField::generate_fast_get_double_field(); 5006 if (func != (address)-1) { 5007 jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func; 5008 } 5009 } 5010 } 5011 5012 // Returns the function structure 5013 struct JNINativeInterface_* jni_functions() { 5014 #if INCLUDE_JNI_CHECK 5015 if (CheckJNICalls) return jni_functions_check(); 5016 #endif // INCLUDE_JNI_CHECK 5017 return &jni_NativeInterface; 5018 } 5019 5020 // Returns the function structure 5021 struct JNINativeInterface_* jni_functions_nocheck() { 5022 return &jni_NativeInterface; 5023 } 5024 5025 #if INCLUDE_JFR 5026 static void post_thread_start_event(const JavaThread* jt) { 5027 assert(jt != NULL, "invariant"); 5028 EventThreadStart event; 5029 if (event.should_commit()) { 5030 event.set_thread(JFR_THREAD_ID(jt)); 5031 event.commit(); 5032 } 5033 } 5034 #endif 5035 5036 // Invocation API 5037 5038 5039 // Forward declaration 5040 extern const struct JNIInvokeInterface_ jni_InvokeInterface; 5041 5042 // Global invocation API vars 5043 volatile jint vm_created = 0; 5044 // Indicate whether it is safe to recreate VM 5045 volatile jint safe_to_recreate_vm = 1; 5046 struct JavaVM_ main_vm = {&jni_InvokeInterface}; 5047 5048 5049 #define JAVASTACKSIZE (400 * 1024) /* Default size of a thread java stack */ 5050 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL }; 5051 5052 #ifndef USDT2 5053 HS_DTRACE_PROBE_DECL1(hotspot_jni, GetDefaultJavaVMInitArgs__entry, void*); 5054 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint); 5237 * sets safe_to_recreate_vm to 1, such that any new call to 5238 * JNI_CreateJavaVM will immediately fail using the above logic. 5239 */ 5240 bool can_try_again = true; 5241 5242 result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again); 5243 if (result == JNI_OK) { 5244 JavaThread *thread = JavaThread::current(); 5245 /* thread is thread_in_vm here */ 5246 *vm = (JavaVM *)(&main_vm); 5247 *(JNIEnv**)penv = thread->jni_environment(); 5248 5249 // Tracks the time application was running before GC 5250 RuntimeService::record_application_start(); 5251 5252 // Notify JVMTI 5253 if (JvmtiExport::should_post_thread_life()) { 5254 JvmtiExport::post_thread_start(thread); 5255 } 5256 5257 #if INCLUDE_JFR 5258 post_thread_start_event(thread); 5259 #endif 5260 5261 #ifndef PRODUCT 5262 #ifndef CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED 5263 #define CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(f) f() 5264 #endif 5265 5266 // Check if we should compile all classes on bootclasspath 5267 if (CompileTheWorld) ClassLoader::compile_the_world(); 5268 if (ReplayCompiles) ciReplay::replay(thread); 5269 5270 // Some platforms (like Win*) need a wrapper around these test 5271 // functions in order to properly handle error conditions. 5272 CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(test_error_handler); 5273 CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(execute_internal_vm_tests); 5274 #endif 5275 5276 // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving. 5277 ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native); 5278 } else { 5279 if (can_try_again) { 5450 if (attach_failed) { 5451 // Added missing cleanup 5452 thread->cleanup_failed_attach_current_thread(); 5453 return JNI_ERR; 5454 } 5455 5456 // mark the thread as no longer attaching 5457 // this uses a fence to push the change through so we don't have 5458 // to regrab the threads_lock 5459 thread->set_done_attaching_via_jni(); 5460 5461 // Set java thread status. 5462 java_lang_Thread::set_thread_status(thread->threadObj(), 5463 java_lang_Thread::RUNNABLE); 5464 5465 // Notify the debugger 5466 if (JvmtiExport::should_post_thread_life()) { 5467 JvmtiExport::post_thread_start(thread); 5468 } 5469 5470 #if INCLUDE_JFR 5471 post_thread_start_event(thread); 5472 #endif 5473 5474 *(JNIEnv**)penv = thread->jni_environment(); 5475 5476 // Now leaving the VM, so change thread_state. This is normally automatically taken care 5477 // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by 5478 // using ThreadStateTransition::transition, we do a callback to the safepoint code if 5479 // needed. 5480 5481 ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native); 5482 5483 // Perform any platform dependent FPU setup 5484 os::setup_fpu(); 5485 5486 return JNI_OK; 5487 } 5488 5489 5490 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) { 5491 #ifndef USDT2 5492 DTRACE_PROBE3(hotspot_jni, AttachCurrentThread__entry, vm, penv, _args); |