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