1 /* 2 * Copyright (c) 1997, 2014, 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 "code/vtableStubs.hpp" 27 #include "compiler/compileBroker.hpp" 28 #include "compiler/disassembler.hpp" 29 #include "memory/allocation.inline.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "oops/instanceKlass.hpp" 32 #include "oops/klassVtable.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "prims/forte.hpp" 35 #include "prims/jvmtiExport.hpp" 36 #include "runtime/handles.inline.hpp" 37 #include "runtime/mutexLocker.hpp" 38 #include "runtime/sharedRuntime.hpp" 39 #ifdef COMPILER2 40 #include "opto/matcher.hpp" 41 #endif 42 43 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 44 45 // ----------------------------------------------------------------------------------------- 46 // Implementation of VtableStub 47 48 address VtableStub::_chunk = NULL; 49 address VtableStub::_chunk_end = NULL; 50 VMReg VtableStub::_receiver_location = VMRegImpl::Bad(); 51 52 53 void* VtableStub::operator new(size_t size, int code_size) throw() { 54 assert(size == sizeof(VtableStub), "mismatched size"); 55 // compute real VtableStub size (rounded to nearest word) 56 const int real_size = round_to(code_size + sizeof(VtableStub), wordSize); 57 // malloc them in chunks to minimize header overhead 58 const int chunk_factor = 32; 59 if (_chunk == NULL || _chunk + real_size > _chunk_end) { 60 const int bytes = chunk_factor * real_size + pd_code_alignment(); 61 62 // There is a dependency on the name of the blob in src/share/vm/prims/jvmtiCodeBlobEvents.cpp 63 // If changing the name, update the other file accordingly. 64 BufferBlob* blob = BufferBlob::create("vtable chunks", bytes); 65 if (blob == NULL) { 66 CompileBroker::handle_full_code_cache(CodeBlobType::NonMethod); 67 return NULL; 68 } 69 _chunk = blob->content_begin(); 70 _chunk_end = _chunk + bytes; 71 Forte::register_stub("vtable stub", _chunk, _chunk_end); 72 align_chunk(); 73 } 74 assert(_chunk + real_size <= _chunk_end, "bad allocation"); 75 void* res = _chunk; 76 _chunk += real_size; 77 align_chunk(); 78 return res; 79 } 80 81 82 void VtableStub::print_on(outputStream* st) const { 83 st->print("vtable stub (index = %d, receiver_location = %d, code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)", 84 index(), receiver_location(), code_begin(), code_end()); 85 } 86 87 88 // ----------------------------------------------------------------------------------------- 89 // Implementation of VtableStubs 90 // 91 // For each hash value there's a linked list of vtable stubs (with that 92 // hash value). Each list is anchored in a little hash _table, indexed 93 // by that hash value. 94 95 VtableStub* VtableStubs::_table[VtableStubs::N]; 96 int VtableStubs::_number_of_vtable_stubs = 0; 97 98 99 void VtableStubs::initialize() { 100 VtableStub::_receiver_location = SharedRuntime::name_for_receiver(); 101 { 102 MutexLocker ml(VtableStubs_lock); 103 assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once"); 104 assert(is_power_of_2(N), "N must be a power of 2"); 105 for (int i = 0; i < N; i++) { 106 _table[i] = NULL; 107 } 108 } 109 } 110 111 112 address VtableStubs::find_stub(bool is_vtable_stub, int vtable_index) { 113 assert(vtable_index >= 0, "must be positive"); 114 115 VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL; 116 if (s == NULL) { 117 if (is_vtable_stub) { 118 s = create_vtable_stub(vtable_index); 119 } else { 120 s = create_itable_stub(vtable_index); 121 } 122 123 // Creation of vtable or itable can fail if there is not enough free space in the code cache. 124 if (s == NULL) { 125 return NULL; 126 } 127 128 enter(is_vtable_stub, vtable_index, s); 129 if (PrintAdapterHandlers) { 130 tty->print_cr("Decoding VtableStub %s[%d]@%d", 131 is_vtable_stub? "vtbl": "itbl", vtable_index, VtableStub::receiver_location()); 132 Disassembler::decode(s->code_begin(), s->code_end()); 133 } 134 // Notify JVMTI about this stub. The event will be recorded by the enclosing 135 // JvmtiDynamicCodeEventCollector and posted when this thread has released 136 // all locks. 137 if (JvmtiExport::should_post_dynamic_code_generated()) { 138 JvmtiExport::post_dynamic_code_generated_while_holding_locks(is_vtable_stub? "vtable stub": "itable stub", 139 s->code_begin(), s->code_end()); 140 } 141 } 142 return s->entry_point(); 143 } 144 145 146 inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){ 147 // Assumption: receiver_location < 4 in most cases. 148 int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index; 149 return (is_vtable_stub ? ~hash : hash) & mask; 150 } 151 152 153 VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) { 154 MutexLocker ml(VtableStubs_lock); 155 unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index); 156 VtableStub* s = _table[hash]; 157 while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next(); 158 return s; 159 } 160 161 162 void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) { 163 MutexLocker ml(VtableStubs_lock); 164 assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub"); 165 unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index); 166 // enter s at the beginning of the corresponding list 167 s->set_next(_table[h]); 168 _table[h] = s; 169 _number_of_vtable_stubs++; 170 } 171 172 173 bool VtableStubs::is_entry_point(address pc) { 174 MutexLocker ml(VtableStubs_lock); 175 VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset()); 176 uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index()); 177 VtableStub* s; 178 for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {} 179 return s == stub; 180 } 181 182 183 bool VtableStubs::contains(address pc) { 184 // simple solution for now - we may want to use 185 // a faster way if this function is called often 186 return stub_containing(pc) != NULL; 187 } 188 189 190 VtableStub* VtableStubs::stub_containing(address pc) { 191 // Note: No locking needed since any change to the data structure 192 // happens with an atomic store into it (we don't care about 193 // consistency with the _number_of_vtable_stubs counter). 194 for (int i = 0; i < N; i++) { 195 for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { 196 if (s->contains(pc)) return s; 197 } 198 } 199 return NULL; 200 } 201 202 void vtableStubs_init() { 203 VtableStubs::initialize(); 204 } 205 206 void VtableStubs::vtable_stub_do(void f(VtableStub*)) { 207 for (int i = 0; i < N; i++) { 208 for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { 209 f(s); 210 } 211 } 212 } 213 214 215 //----------------------------------------------------------------------------------------------------- 216 // Non-product code 217 #ifndef PRODUCT 218 219 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) { 220 ResourceMark rm; 221 HandleMark hm; 222 Klass* klass = receiver->klass(); 223 InstanceKlass* ik = InstanceKlass::cast(klass); 224 klassVtable* vt = ik->vtable(); 225 ik->print(); 226 fatal(err_msg("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", " 227 "index %d (vtable length %d)", 228 (address)receiver, index, vt->length())); 229 } 230 231 #endif // Product