1 /* 2 * Copyright (c) 1997, 2025, 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 #include "classfile/stringTable.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "compiler/compiler_globals.hpp" 28 #include "gc/shared/collectedHeap.hpp" 29 #include "gc/shared/gcHeapSummary.hpp" 30 #include "interpreter/bytecodes.hpp" 31 #include "logging/logAsyncWriter.hpp" 32 #include "memory/universe.hpp" 33 #include "nmt/memTracker.hpp" 34 #include "prims/downcallLinker.hpp" 35 #include "prims/jvmtiExport.hpp" 36 #include "prims/methodHandles.hpp" 37 #include "runtime/atomic.hpp" 38 #include "runtime/continuation.hpp" 39 #include "runtime/flags/jvmFlag.hpp" 40 #include "runtime/globals.hpp" 41 #include "runtime/handles.inline.hpp" 42 #include "runtime/icache.hpp" 43 #include "runtime/init.hpp" 44 #include "runtime/safepoint.hpp" 45 #include "runtime/sharedRuntime.hpp" 46 #include "sanitizers/leak.hpp" 47 #include "utilities/macros.hpp" 48 #if INCLUDE_JVMCI 49 #include "jvmci/jvmci.hpp" 50 #endif 51 52 // Initialization done by VM thread in vm_init_globals() 53 void check_ThreadShadow(); 54 void eventlog_init(); 55 void mutex_init(); 56 void universe_oopstorage_init(); 57 void perfMemory_init(); 58 void SuspendibleThreadSet_init(); 59 void ExternalsRecorder_init(); // After mutex_init() and before CodeCache_init 60 61 // Initialization done by Java thread in init_globals() 62 void management_init(); 63 void bytecodes_init(); 64 void classLoader_init1(); 65 void compilationPolicy_init(); 66 void codeCache_init(); 67 void VM_Version_init(); 68 void icache_init2(); 69 void initial_stubs_init(); 70 71 jint universe_init(); // depends on codeCache_init and initial_stubs_init 72 // depends on universe_init, must be before interpreter_init (currently only on SPARC) 73 void gc_barrier_stubs_init(); 74 void continuations_init(); // depends on flags (UseCompressedOops) and barrier sets 75 void continuation_stubs_init(); // depend on continuations_init 76 void interpreter_init_stub(); // before any methods loaded 77 void interpreter_init_code(); // after methods loaded, but before they are linked 78 void accessFlags_init(); 79 void InterfaceSupport_init(); 80 void universe2_init(); // dependent on codeCache_init and initial_stubs_init, loads primordial classes 81 void referenceProcessor_init(); 82 void jni_handles_init(); 83 void vmStructs_init() NOT_DEBUG_RETURN; 84 85 void vtableStubs_init(); 86 bool compilerOracle_init(); 87 bool compileBroker_init(); 88 void dependencyContext_init(); 89 void dependencies_init(); 90 91 // Initialization after compiler initialization 92 bool universe_post_init(); // must happen after compiler_init 93 void javaClasses_init(); // must happen after vtable initialization 94 void compiler_stubs_init(bool in_compiler_thread); // compiler's StubRoutines stubs 95 void final_stubs_init(); // final StubRoutines stubs 96 97 // Do not disable thread-local-storage, as it is important for some 98 // JNI/JVM/JVMTI functions and signal handlers to work properly 99 // during VM shutdown 100 void perfMemory_exit(); 101 void ostream_exit(); 102 103 void vm_init_globals() { 104 check_ThreadShadow(); 105 basic_types_init(); 106 eventlog_init(); 107 mutex_init(); 108 universe_oopstorage_init(); 109 perfMemory_init(); 110 SuspendibleThreadSet_init(); 111 ExternalsRecorder_init(); // After mutex_init() and before CodeCache_init 112 } 113 114 115 jint init_globals() { 116 management_init(); 117 JvmtiExport::initialize_oop_storage(); 118 #if INCLUDE_JVMTI 119 if (AlwaysRecordEvolDependencies) { 120 JvmtiExport::set_can_hotswap_or_post_breakpoint(true); 121 JvmtiExport::set_all_dependencies_are_recorded(true); 122 } 123 #endif 124 bytecodes_init(); 125 classLoader_init1(); 126 compilationPolicy_init(); 127 codeCache_init(); 128 VM_Version_init(); // depends on codeCache_init for emitting code 129 icache_init2(); // depends on VM_Version for choosing the mechanism 130 // stub routines in initial blob are referenced by later generated code 131 initial_stubs_init(); 132 // stack overflow exception blob is referenced by the interpreter 133 SharedRuntime::generate_initial_stubs(); 134 jint status = universe_init(); // dependent on codeCache_init and 135 // initial_stubs_init and metaspace_init. 136 if (status != JNI_OK) 137 return status; 138 139 #ifdef LEAK_SANITIZER 140 { 141 // Register the Java heap with LSan. 142 VirtualSpaceSummary summary = Universe::heap()->create_heap_space_summary(); 143 LSAN_REGISTER_ROOT_REGION(summary.start(), summary.reserved_size()); 144 } 145 #endif // LEAK_SANITIZER 146 147 AsyncLogWriter::initialize(); 148 gc_barrier_stubs_init(); // depends on universe_init, must be before interpreter_init 149 continuations_init(); // must precede continuation stub generation 150 continuation_stubs_init(); // depends on continuations_init 151 #if INCLUDE_JFR 152 SharedRuntime::generate_jfr_stubs(); 153 #endif 154 interpreter_init_stub(); // before methods get loaded 155 accessFlags_init(); 156 InterfaceSupport_init(); 157 VMRegImpl::set_regName(); // need this before generate_stubs (for printing oop maps). 158 SharedRuntime::generate_stubs(); 159 return JNI_OK; 160 } 161 162 jint init_globals2() { 163 universe2_init(); // dependent on codeCache_init and initial_stubs_init 164 javaClasses_init(); // must happen after vtable initialization, before referenceProcessor_init 165 interpreter_init_code(); // after javaClasses_init and before any method gets linked 166 referenceProcessor_init(); 167 jni_handles_init(); 168 #if INCLUDE_VM_STRUCTS 169 vmStructs_init(); 170 #endif // INCLUDE_VM_STRUCTS 171 172 vtableStubs_init(); 173 if (!compilerOracle_init()) { 174 return JNI_EINVAL; 175 } 176 dependencyContext_init(); 177 dependencies_init(); 178 179 if (!compileBroker_init()) { 180 return JNI_EINVAL; 181 } 182 #if INCLUDE_JVMCI 183 if (EnableJVMCI) { 184 JVMCI::initialize_globals(); 185 } 186 #endif 187 188 if (!universe_post_init()) { 189 return JNI_ERR; 190 } 191 compiler_stubs_init(false /* in_compiler_thread */); // compiler's intrinsics stubs 192 final_stubs_init(); // final StubRoutines stubs 193 MethodHandles::generate_adapters(); 194 195 // All the flags that get adjusted by VM_Version_init and os::init_2 196 // have been set so dump the flags now. 197 if (PrintFlagsFinal || PrintFlagsRanges) { 198 JVMFlag::printFlags(tty, false, PrintFlagsRanges); 199 } 200 201 return JNI_OK; 202 } 203 204 205 void exit_globals() { 206 static bool destructorsCalled = false; 207 if (!destructorsCalled) { 208 destructorsCalled = true; 209 perfMemory_exit(); 210 SafepointTracing::statistics_exit_log(); 211 if (PrintStringTableStatistics) { 212 SymbolTable::dump(tty); 213 StringTable::dump(tty); 214 } 215 ostream_exit(); 216 #ifdef LEAK_SANITIZER 217 { 218 // Unregister the Java heap with LSan. 219 VirtualSpaceSummary summary = Universe::heap()->create_heap_space_summary(); 220 LSAN_UNREGISTER_ROOT_REGION(summary.start(), summary.reserved_size()); 221 } 222 #endif // LEAK_SANITIZER 223 } 224 } 225 226 static volatile bool _init_completed = false; 227 228 bool is_init_completed() { 229 return Atomic::load_acquire(&_init_completed); 230 } 231 232 void wait_init_completed() { 233 MonitorLocker ml(InitCompleted_lock, Monitor::_no_safepoint_check_flag); 234 while (!_init_completed) { 235 ml.wait(); 236 } 237 } 238 239 void set_init_completed() { 240 assert(Universe::is_fully_initialized(), "Should have completed initialization"); 241 MonitorLocker ml(InitCompleted_lock, Monitor::_no_safepoint_check_flag); 242 Atomic::release_store(&_init_completed, true); 243 ml.notify_all(); 244 }