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