1 /*
   2  * Copyright (c) 1997, 2020, 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 "gc/shared/collectedHeap.hpp"
  30 #if INCLUDE_JVMCI
  31 #include "jvmci/jvmci.hpp"
  32 #endif
  33 #include "interpreter/bytecodes.hpp"
  34 #include "logging/log.hpp"
  35 #include "logging/logTag.hpp"
  36 #include "memory/universe.hpp"
  37 #include "prims/methodHandles.hpp"
  38 #include "runtime/atomic.hpp"
  39 #include "runtime/flags/jvmFlag.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 #include "runtime/icache.hpp"
  42 #include "runtime/init.hpp"
  43 #include "runtime/safepoint.hpp"
  44 #include "runtime/sharedRuntime.hpp"
  45 #include "services/memTracker.hpp"
  46 #include "utilities/macros.hpp"
  47 
  48 
  49 // Initialization done by VM thread in vm_init_globals()
  50 void check_ThreadShadow();
  51 void eventlog_init();
  52 void mutex_init();
  53 void oopstorage_init();
  54 void chunkpool_init();
  55 void perfMemory_init();
  56 void SuspendibleThreadSet_init();
  57 
  58 // Initialization done by Java thread in init_globals()
  59 void management_init();
  60 void bytecodes_init();
  61 void classLoader_init1();
  62 void compilationPolicy_init();
  63 void codeCache_init();
  64 void VM_Version_init();
  65 void stubRoutines_init1();
  66 jint universe_init();          // depends on codeCache_init and stubRoutines_init
  67 // depends on universe_init, must be before interpreter_init (currently only on SPARC)
  68 void gc_barrier_stubs_init();
  69 void interpreter_init_stub();  // before any methods loaded
  70 void interpreter_init_code();  // after methods loaded, but before they are linked
  71 void invocationCounter_init(); // after methods loaded, but before they are linked
  72 void accessFlags_init();
  73 void InterfaceSupport_init();
  74 void universe2_init();  // dependent on codeCache_init and stubRoutines_init, loads primordial classes
  75 void referenceProcessor_init();
  76 void jni_handles_init();
  77 void vmStructs_init() NOT_DEBUG_RETURN;
  78 
  79 void vtableStubs_init();
  80 void InlineCacheBuffer_init();
  81 void compilerOracle_init();
  82 bool compileBroker_init();
  83 void dependencyContext_init();
  84 
  85 // Initialization after compiler initialization
  86 bool universe_post_init();  // must happen after compiler_init
  87 void javaClasses_init();  // must happen after vtable initialization
  88 void stubRoutines_init2(); // note: StubRoutines need 2-phase init
  89 
  90 // Do not disable thread-local-storage, as it is important for some
  91 // JNI/JVM/JVMTI functions and signal handlers to work properly
  92 // during VM shutdown
  93 void perfMemory_exit();
  94 void ostream_exit();
  95 
  96 void vm_init_globals() {
  97   check_ThreadShadow();
  98   basic_types_init();
  99   eventlog_init();
 100   mutex_init();
 101   oopstorage_init();
 102   chunkpool_init();
 103   perfMemory_init();
 104   SuspendibleThreadSet_init();
 105 }
 106 
 107 
 108 jint init_globals() {
 109   HandleMark hm;
 110   management_init();
 111   bytecodes_init();
 112   classLoader_init1();
 113   compilationPolicy_init();
 114   codeCache_init();
 115   VM_Version_init();
 116   stubRoutines_init1();
 117   jint status = universe_init();  // dependent on codeCache_init and
 118                                   // stubRoutines_init1 and metaspace_init.
 119   if (status != JNI_OK)
 120     return status;
 121 
 122   gc_barrier_stubs_init();  // depends on universe_init, must be before interpreter_init
 123   interpreter_init_stub();  // before methods get loaded
 124   accessFlags_init();
 125   InterfaceSupport_init();
 126   VMRegImpl::set_regName(); // need this before generate_stubs (for printing oop maps).
 127   SharedRuntime::generate_stubs();
 128   universe2_init();  // dependent on codeCache_init and stubRoutines_init1
 129   javaClasses_init();// must happen after vtable initialization, before referenceProcessor_init
 130   interpreter_init_code();  // after javaClasses_init and before any method gets linked
 131   invocationCounter_init(); // after javaClasses_init and before any method gets linked
 132   referenceProcessor_init();
 133   jni_handles_init();
 134 #if INCLUDE_VM_STRUCTS
 135   vmStructs_init();
 136 #endif // INCLUDE_VM_STRUCTS
 137 
 138   vtableStubs_init();
 139   InlineCacheBuffer_init();
 140   compilerOracle_init();
 141   dependencyContext_init();
 142 
 143   if (!compileBroker_init()) {
 144     return JNI_EINVAL;
 145   }
 146 #if INCLUDE_JVMCI
 147   if (EnableJVMCI) {
 148     JVMCI::initialize_globals();
 149   }
 150 #endif
 151 
 152   if (!universe_post_init()) {
 153     return JNI_ERR;
 154   }
 155   stubRoutines_init2(); // note: StubRoutines need 2-phase init
 156   MethodHandles::generate_adapters();
 157 
 158   // All the flags that get adjusted by VM_Version_init and os::init_2
 159   // have been set so dump the flags now.
 160   if (PrintFlagsFinal || PrintFlagsRanges) {
 161     JVMFlag::printFlags(tty, false, PrintFlagsRanges);
 162   }
 163 
 164   return JNI_OK;
 165 }
 166 
 167 
 168 void exit_globals() {
 169   static bool destructorsCalled = false;
 170   if (!destructorsCalled) {
 171     destructorsCalled = true;
 172     if (log_is_enabled(Info, monitorinflation)) {
 173       // The ObjectMonitor subsystem uses perf counters so
 174       // do this before perfMemory_exit().
 175       // ObjectSynchronizer::finish_deflate_idle_monitors()'s call
 176       // to audit_and_print_stats() is done at the Debug level.
 177       ObjectSynchronizer::audit_and_print_stats(true /* on_exit */);
 178     }
 179     perfMemory_exit();
 180     SafepointTracing::statistics_exit_log();
 181     if (PrintStringTableStatistics) {
 182       SymbolTable::dump(tty);
 183       StringTable::dump(tty);
 184     }
 185     ostream_exit();
 186   }
 187 }
 188 
 189 static volatile bool _init_completed = false;
 190 
 191 bool is_init_completed() {
 192   return Atomic::load_acquire(&_init_completed);
 193 }
 194 
 195 void wait_init_completed() {
 196   MonitorLocker ml(InitCompleted_lock, Monitor::_no_safepoint_check_flag);
 197   while (!_init_completed) {
 198     ml.wait();
 199   }
 200 }
 201 
 202 void set_init_completed() {
 203   assert(Universe::is_fully_initialized(), "Should have completed initialization");
 204   MonitorLocker ml(InitCompleted_lock, Monitor::_no_safepoint_check_flag);
 205   Atomic::release_store(&_init_completed, true);
 206   ml.notify_all();
 207 }