| 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/jvmciJavaClasses.hpp" |
| 30 | #include "jvmci/jvmciRuntime.hpp" |
| 31 | #include "jvmci/metadataHandleBlock.hpp" |
| 32 | #include "memory/universe.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 | bool JVMCI::is_global_handle(jobject handle) { |
| 97 | const oop* ptr = reinterpret_cast<oop*>(handle); |
| 98 | return object_handles()->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY; |
| 99 | } |
| 100 | |
| 101 | jmetadata JVMCI::allocate_handle(const methodHandle& handle) { |
| 102 | assert(_metadata_handles != NULL, "uninitialized" ); |
| 103 | MutexLocker ml(JVMCI_lock); |
| 104 | return _metadata_handles->allocate_handle(handle); |
| 105 | } |
| 106 | |
| 107 | jmetadata JVMCI::allocate_handle(const constantPoolHandle& handle) { |
| 108 | assert(_metadata_handles != NULL, "uninitialized" ); |
| 109 | MutexLocker ml(JVMCI_lock); |
| 110 | return _metadata_handles->allocate_handle(handle); |
| 111 | } |
| 112 | |
| 113 | void JVMCI::release_handle(jmetadata handle) { |
| 114 | MutexLocker ml(JVMCI_lock); |
| 115 | _metadata_handles->chain_free_list(handle); |
| 116 | } |
| 117 | |
| 118 | void JVMCI::oops_do(OopClosure* f) { |
| 119 | if (_object_handles != NULL) { |
| 120 | _object_handles->oops_do(f); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void JVMCI::metadata_do(void f(Metadata*)) { |
| 125 | if (_metadata_handles != NULL) { |
| 126 | _metadata_handles->metadata_do(f); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void JVMCI::do_unloading(bool unloading_occurred) { |
| 131 | if (_metadata_handles != NULL && unloading_occurred) { |
| 132 | _metadata_handles->do_unloading(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | bool JVMCI::is_compiler_initialized() { |
| 137 | return compiler_runtime()->is_HotSpotJVMCIRuntime_initialized(); |
| 138 | } |
| 139 | |
| 140 | void JVMCI::shutdown() { |
| 141 | if (compiler_runtime() != NULL) { |
| 142 | compiler_runtime()->shutdown(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | bool JVMCI::shutdown_called() { |
| 147 | if (compiler_runtime() != NULL) { |
| 148 | return compiler_runtime()->shutdown_called(); |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |