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/metadataHandleBlock.hpp"

  33 
  34 OopStorage* JVMCI::_object_handles = NULL;
  35 MetadataHandleBlock* JVMCI::_metadata_handles = NULL;
  36 JVMCIRuntime* JVMCI::_compiler_runtime = NULL;
  37 JVMCIRuntime* JVMCI::_java_runtime = NULL;




  38 
  39 bool JVMCI::can_initialize_JVMCI() {
  40   // Initializing JVMCI requires the module system to be initialized past phase 3.
  41   // The JVMCI API itself isn't available until phase 2 and ServiceLoader (which
  42   // JVMCI initialization requires) isn't usable until after phase 3. Testing
  43   // whether the system loader is initialized satisfies all these invariants.
  44   if (SystemDictionary::java_system_loader() == NULL) {
  45     return false;
  46   }
  47   assert(Universe::is_module_initialized(), "must be");
  48   return true;
  49 }
  50 


































  51 void JVMCI::initialize_compiler(TRAPS) {
  52   if (JVMCILibDumpJNIConfig) {
  53     JNIJVMCI::initialize_ids(NULL);
  54     ShouldNotReachHere();
  55   }
  56 
  57   JVMCI::compiler_runtime()->call_getCompiler(CHECK);
  58 }
  59 
  60 void JVMCI::initialize_globals() {
  61   _object_handles = new OopStorage("JVMCI Global Oop Handles",
  62                                    JVMCIGlobalAlloc_lock,
  63                                    JVMCIGlobalActive_lock);
  64   _metadata_handles = MetadataHandleBlock::allocate_block();
  65   if (UseJVMCINativeLibrary) {
  66     // There are two runtimes.
  67     _compiler_runtime = new JVMCIRuntime();
  68     _java_runtime = new JVMCIRuntime();
  69   } else {
  70     // There is only a single runtime
  71     _java_runtime = _compiler_runtime = new JVMCIRuntime();
  72   }
  73 }
  74 
  75 OopStorage* JVMCI::object_handles() {
  76   assert(_object_handles != NULL, "Uninitialized");
  77   return _object_handles;
  78 }
  79 
  80 jobject JVMCI::make_global(const Handle& obj) {
  81   assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
  82   assert(oopDesc::is_oop(obj()), "not an oop");
  83   oop* ptr = object_handles()->allocate();
  84   jobject res = NULL;
  85   if (ptr != NULL) {
  86     assert(*ptr == NULL, "invariant");
  87     NativeAccess<>::oop_store(ptr, obj());
  88     res = reinterpret_cast<jobject>(ptr);
  89   } else {
  90     vm_exit_out_of_memory(sizeof(oop), OOM_MALLOC_ERROR,
  91                           "Cannot create JVMCI oop handle");
  92   }
  93   return res;
  94 }
  95 
  96 void JVMCI::destroy_global(jobject handle) {
  97   // Assert before nulling out, for better debugging.
  98   assert(is_global_handle(handle), "precondition");
  99   oop* oop_ptr = reinterpret_cast<oop*>(handle);
 100   NativeAccess<>::oop_store(oop_ptr, (oop)NULL);
 101   object_handles()->release(oop_ptr);
 102 }
 103 
 104 bool JVMCI::is_global_handle(jobject handle) {
 105   const oop* ptr = reinterpret_cast<oop*>(handle);
 106   return object_handles()->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY;
 107 }
 108 
 109 jmetadata JVMCI::allocate_handle(const methodHandle& handle) {
 110   assert(_metadata_handles != NULL, "uninitialized");
 111   MutexLocker ml(JVMCI_lock);
 112   return _metadata_handles->allocate_handle(handle);
 113 }
 114 
 115 jmetadata JVMCI::allocate_handle(const constantPoolHandle& handle) {
 116   assert(_metadata_handles != NULL, "uninitialized");
 117   MutexLocker ml(JVMCI_lock);
 118   return _metadata_handles->allocate_handle(handle);
 119 }
 120 
 121 void JVMCI::release_handle(jmetadata handle) {
 122   MutexLocker ml(JVMCI_lock);
 123   _metadata_handles->chain_free_list(handle);
 124 }
 125 
 126 void JVMCI::oops_do(OopClosure* f) {
 127   if (_object_handles != NULL) {
 128     _object_handles->oops_do(f);



 129   }
 130 }
 131 
 132 void JVMCI::metadata_do(void f(Metadata*)) {
 133   if (_metadata_handles != NULL) {
 134     _metadata_handles->metadata_do(f);



 135   }
 136 }
 137 
 138 void JVMCI::do_unloading(bool unloading_occurred) {
 139   if (_metadata_handles != NULL && unloading_occurred) {
 140     _metadata_handles->do_unloading();





 141   }
 142 }
 143 
 144 bool JVMCI::is_compiler_initialized() {
 145   return compiler_runtime()->is_HotSpotJVMCIRuntime_initialized();
 146 }
 147 
 148 void JVMCI::shutdown() {










 149   if (compiler_runtime() != NULL) {
 150     compiler_runtime()->shutdown();
 151   }
 152 }
 153 
 154 bool JVMCI::shutdown_called() {
 155   if (compiler_runtime() != NULL) {
 156     return compiler_runtime()->shutdown_called();
 157   }
 158   return false;
 159 }
--- EOF ---