1 /*
  2  * Copyright (c) 1997, 2023, 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 "precompiled.hpp"
 26 #include "classfile/stringTable.hpp"
 27 #include "classfile/symbolTable.hpp"
 28 #include "code/icBuffer.hpp"
 29 #include "compiler/compiler_globals.hpp"
 30 #include "gc/shared/collectedHeap.hpp"
 31 #include "gc/shared/gcHeapSummary.hpp"
 32 #include "interpreter/bytecodes.hpp"
 33 #include "logging/logAsyncWriter.hpp"
 34 #include "memory/universe.hpp"
 35 #include "nmt/memTracker.hpp"
 36 #include "prims/downcallLinker.hpp"
 37 #include "prims/jvmtiExport.hpp"
 38 #include "prims/methodHandles.hpp"
 39 #include "runtime/atomic.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 
 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 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 void InlineCacheBuffer_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 }
113 
114 
115 jint init_globals() {
116   management_init();
117   JvmtiExport::initialize_oop_storage();
118   bytecodes_init();
119   classLoader_init1();
120   compilationPolicy_init();
121   codeCache_init();
122   VM_Version_init();              // depends on codeCache_init for emitting code
123   VMRegImpl::set_regName();       // need this before generate_stubs (for printing oop maps).
124   initial_stubs_init();
125   jint status = universe_init();  // dependent on codeCache_init and
126                                   // initial_stubs_init and metaspace_init.
127   if (status != JNI_OK)
128     return status;
129 
130 #ifdef LEAK_SANITIZER
131   {
132     // Register the Java heap with LSan.
133     VirtualSpaceSummary summary = Universe::heap()->create_heap_space_summary();
134     LSAN_REGISTER_ROOT_REGION(summary.start(), summary.reserved_size());
135   }
136 #endif // LEAK_SANITIZER
137 
138   AsyncLogWriter::initialize();
139   gc_barrier_stubs_init();   // depends on universe_init, must be before interpreter_init
140   continuations_init();      // must precede continuation stub generation
141   continuation_stubs_init(); // depends on continuations_init
142   interpreter_init_stub();   // before methods get loaded
143   accessFlags_init();
144   InterfaceSupport_init();
145   SharedRuntime::generate_stubs();
146   return JNI_OK;
147 }
148 
149 jint init_globals2() {
150   universe2_init();          // dependent on codeCache_init and initial_stubs_init
151   javaClasses_init();        // must happen after vtable initialization, before referenceProcessor_init
152   interpreter_init_code();   // after javaClasses_init and before any method gets linked
153   referenceProcessor_init();
154   jni_handles_init();
155 #if INCLUDE_VM_STRUCTS
156   vmStructs_init();
157 #endif // INCLUDE_VM_STRUCTS
158 
159   vtableStubs_init();
160   InlineCacheBuffer_init();
161   if (!compilerOracle_init()) {
162     return JNI_EINVAL;
163   }
164   dependencyContext_init();
165   dependencies_init();
166 
167   if (!compileBroker_init()) {
168     return JNI_EINVAL;
169   }
170 #if INCLUDE_JVMCI
171   if (EnableJVMCI) {
172     JVMCI::initialize_globals();
173   }
174 #endif
175 
176   if (!universe_post_init()) {
177     return JNI_ERR;
178   }
179   compiler_stubs_init(false /* in_compiler_thread */); // compiler's intrinsics stubs
180   final_stubs_init();    // final StubRoutines stubs
181   MethodHandles::generate_adapters();
182 
183   // All the flags that get adjusted by VM_Version_init and os::init_2
184   // have been set so dump the flags now.
185   if (PrintFlagsFinal || PrintFlagsRanges) {
186     JVMFlag::printFlags(tty, false, PrintFlagsRanges);
187   }
188 
189   return JNI_OK;
190 }
191 
192 
193 void exit_globals() {
194   static bool destructorsCalled = false;
195   if (!destructorsCalled) {
196     destructorsCalled = true;
197     perfMemory_exit();
198     SafepointTracing::statistics_exit_log();
199     if (PrintStringTableStatistics) {
200       SymbolTable::dump(tty);
201       StringTable::dump(tty);
202     }
203     ostream_exit();
204 #ifdef LEAK_SANITIZER
205     {
206       // Unregister the Java heap with LSan.
207       VirtualSpaceSummary summary = Universe::heap()->create_heap_space_summary();
208       LSAN_UNREGISTER_ROOT_REGION(summary.start(), summary.reserved_size());
209     }
210 #endif // LEAK_SANITIZER
211   }
212 }
213 
214 static volatile bool _init_completed = false;
215 
216 bool is_init_completed() {
217   return Atomic::load_acquire(&_init_completed);
218 }
219 
220 void wait_init_completed() {
221   MonitorLocker ml(InitCompleted_lock, Monitor::_no_safepoint_check_flag);
222   while (!_init_completed) {
223     ml.wait();
224   }
225 }
226 
227 void set_init_completed() {
228   assert(Universe::is_fully_initialized(), "Should have completed initialization");
229   MonitorLocker ml(InitCompleted_lock, Monitor::_no_safepoint_check_flag);
230   Atomic::release_store(&_init_completed, true);
231   ml.notify_all();
232 }