1 /*
   2  * Copyright (c) 2019, 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 #include "precompiled.hpp"
  25 #include "classfile/systemDictionary.hpp"
  26 #include "gc/shared/collectedHeap.hpp"
  27 #include "gc/shared/oopStorage.inline.hpp"
  28 #include "jvmci/jvmci.hpp"
  29 #include "jvmci/jvmci_globals.hpp"
  30 #include "jvmci/jvmciJavaClasses.hpp"
  31 #include "jvmci/jvmciRuntime.hpp"
  32 #include "jvmci/metadataHandles.hpp"
  33 #include "memory/resourceArea.hpp"
  34 


  35 JVMCIRuntime* JVMCI::_compiler_runtime = NULL;
  36 JVMCIRuntime* JVMCI::_java_runtime = NULL;
  37 volatile bool JVMCI::_is_initialized = false;
  38 void* JVMCI::_shared_library_handle = NULL;
  39 char* JVMCI::_shared_library_path = NULL;
  40 volatile bool JVMCI::_in_shutdown = false;
  41 
  42 bool JVMCI::can_initialize_JVMCI() {
  43   // Initializing JVMCI requires the module system to be initialized past phase 3.
  44   // The JVMCI API itself isn't available until phase 2 and ServiceLoader (which
  45   // JVMCI initialization requires) isn't usable until after phase 3. Testing
  46   // whether the system loader is initialized satisfies all these invariants.
  47   if (SystemDictionary::java_system_loader() == NULL) {
  48     return false;
  49   }
  50   assert(Universe::is_module_initialized(), "must be");
  51   return true;
  52 }
  53 
  54 void* JVMCI::get_shared_library(char*& path, bool load) {
  55   void* sl_handle = _shared_library_handle;
  56   if (sl_handle != NULL || !load) {
  57     path = _shared_library_path;
  58     return sl_handle;
  59   }
  60   assert(JVMCI_lock->owner() == Thread::current(), "must be");
  61   path = NULL;
  62   if (_shared_library_handle == NULL) {
  63     char path[JVM_MAXPATHLEN];
  64     char ebuf[1024];
  65     if (JVMCILibPath != NULL) {
  66       if (!os::dll_locate_lib(path, sizeof(path), JVMCILibPath, JVMCI_SHARED_LIBRARY_NAME)) {
  67         vm_exit_during_initialization("Unable to locate JVMCI shared library in path specified by -XX:JVMCILibPath value", JVMCILibPath);
  68       }
  69     } else {
  70       if (!os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), JVMCI_SHARED_LIBRARY_NAME)) {
  71         vm_exit_during_initialization("Unable to create path to JVMCI shared library");
  72       }
  73     }
  74 
  75     void* handle = os::dll_load(path, ebuf, sizeof ebuf);
  76     if (handle == NULL) {
  77       vm_exit_during_initialization("Unable to load JVMCI shared library", ebuf);
  78     }
  79     _shared_library_handle = handle;
  80     _shared_library_path = strdup(path);
  81 
  82     TRACE_jvmci_1("loaded JVMCI shared library from %s", path);
  83   }
  84   path = _shared_library_path;
  85   return _shared_library_handle;
  86 }
  87 
  88 void JVMCI::initialize_compiler(TRAPS) {
  89   if (JVMCILibDumpJNIConfig) {
  90     JNIJVMCI::initialize_ids(NULL);
  91     ShouldNotReachHere();
  92   }
  93 
  94   JVMCI::compiler_runtime()->call_getCompiler(CHECK);
  95 }
  96 
  97 void JVMCI::initialize_globals() {




  98   if (UseJVMCINativeLibrary) {
  99     // There are two runtimes.
 100     _compiler_runtime = new JVMCIRuntime(0);
 101     _java_runtime = new JVMCIRuntime(-1);
 102   } else {
 103     // There is only a single runtime
 104     _java_runtime = _compiler_runtime = new JVMCIRuntime(0);




















 105   }































 106 }
 107 
 108 void JVMCI::oops_do(OopClosure* f) {
 109   if (_java_runtime != NULL) {
 110     _java_runtime->_object_handles->oops_do(f);
 111   }
 112   if (_compiler_runtime != NULL && _compiler_runtime != _java_runtime) {
 113     _compiler_runtime->_object_handles->oops_do(f);
 114   }
 115 }
 116 
 117 void JVMCI::metadata_do(void f(Metadata*)) {
 118   if (_java_runtime != NULL) {
 119     _java_runtime->_metadata_handles->metadata_do(f);
 120   }
 121   if (_compiler_runtime != NULL && _compiler_runtime != _java_runtime) {
 122     _compiler_runtime->_metadata_handles->metadata_do(f);
 123   }
 124 }
 125 
 126 void JVMCI::do_unloading(bool unloading_occurred) {
 127   if (unloading_occurred) {
 128     if (_java_runtime != NULL) {
 129       _java_runtime->_metadata_handles->do_unloading();
 130     }
 131     if (_compiler_runtime != NULL && _compiler_runtime != _java_runtime) {
 132       _compiler_runtime->_metadata_handles->do_unloading();
 133     }
 134   }
 135 }
 136 
 137 bool JVMCI::is_compiler_initialized() {
 138   return _is_initialized;
 139 }
 140 
 141 void JVMCI::shutdown() {
 142   ResourceMark rm;
 143   {
 144     MutexLocker locker(JVMCI_lock);
 145     _in_shutdown = true;
 146     TRACE_jvmci_1("shutting down JVMCI");
 147   }
 148   JVMCIRuntime* java_runtime = _java_runtime;
 149   if (java_runtime != compiler_runtime()) {
 150     java_runtime->shutdown();
 151   }
 152   if (compiler_runtime() != NULL) {
 153     compiler_runtime()->shutdown();
 154   }
 155 }
 156 
 157 bool JVMCI::in_shutdown() {
 158   return _in_shutdown;



 159 }
--- EOF ---