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