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