duke@435: /* duke@435: * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_vtableStubs.cpp.incl" duke@435: duke@435: // ----------------------------------------------------------------------------------------- duke@435: // Implementation of VtableStub duke@435: duke@435: address VtableStub::_chunk = NULL; duke@435: address VtableStub::_chunk_end = NULL; duke@435: VMReg VtableStub::_receiver_location = VMRegImpl::Bad(); duke@435: duke@435: static int num_vtable_chunks = 0; duke@435: duke@435: duke@435: void* VtableStub::operator new(size_t size, int code_size) { duke@435: assert(size == sizeof(VtableStub), "mismatched size"); duke@435: num_vtable_chunks++; duke@435: // compute real VtableStub size (rounded to nearest word) duke@435: const int real_size = round_to(code_size + sizeof(VtableStub), wordSize); duke@435: // malloc them in chunks to minimize header overhead duke@435: const int chunk_factor = 32; duke@435: if (_chunk == NULL || _chunk + real_size > _chunk_end) { duke@435: const int bytes = chunk_factor * real_size + pd_code_alignment(); duke@435: BufferBlob* blob = BufferBlob::create("vtable chunks", bytes); duke@435: if( blob == NULL ) vm_exit_out_of_memory1(bytes, "CodeCache: no room for %s", "vtable chunks"); duke@435: _chunk = blob->instructions_begin(); duke@435: _chunk_end = _chunk + bytes; duke@435: VTune::register_stub("vtable stub", _chunk, _chunk_end); duke@435: Forte::register_stub("vtable stub", _chunk, _chunk_end); duke@435: // Notify JVMTI about this stub. The event will be recorded by the enclosing duke@435: // JvmtiDynamicCodeEventCollector and posted when this thread has released duke@435: // all locks. duke@435: if (JvmtiExport::should_post_dynamic_code_generated()) { duke@435: JvmtiExport::post_dynamic_code_generated_while_holding_locks("vtable stub", _chunk, _chunk_end); duke@435: } duke@435: align_chunk(); duke@435: } duke@435: assert(_chunk + real_size <= _chunk_end, "bad allocation"); duke@435: void* res = _chunk; duke@435: _chunk += real_size; duke@435: align_chunk(); duke@435: return res; duke@435: } duke@435: duke@435: duke@435: void VtableStub::print() { duke@435: tty->print("vtable stub (index = %d, receiver_location = %d, code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)", duke@435: index(), receiver_location(), code_begin(), code_end()); duke@435: } duke@435: duke@435: duke@435: // ----------------------------------------------------------------------------------------- duke@435: // Implementation of VtableStubs duke@435: // duke@435: // For each hash value there's a linked list of vtable stubs (with that duke@435: // hash value). Each list is anchored in a little hash _table, indexed duke@435: // by that hash value. duke@435: duke@435: VtableStub* VtableStubs::_table[VtableStubs::N]; duke@435: int VtableStubs::_number_of_vtable_stubs = 0; duke@435: duke@435: duke@435: void VtableStubs::initialize() { duke@435: VtableStub::_receiver_location = SharedRuntime::name_for_receiver(); duke@435: { duke@435: MutexLocker ml(VtableStubs_lock); duke@435: assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once"); duke@435: assert(is_power_of_2(N), "N must be a power of 2"); duke@435: for (int i = 0; i < N; i++) { duke@435: _table[i] = NULL; duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: address VtableStubs::create_stub(bool is_vtable_stub, int vtable_index, methodOop method) { duke@435: assert(vtable_index >= 0, "must be positive"); duke@435: duke@435: VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL; duke@435: if (s == NULL) { duke@435: if (is_vtable_stub) { duke@435: s = create_vtable_stub(vtable_index); duke@435: } else { duke@435: s = create_itable_stub(vtable_index); duke@435: } duke@435: enter(is_vtable_stub, vtable_index, s); duke@435: if (PrintAdapterHandlers) { duke@435: tty->print_cr("Decoding VtableStub %s[%d]@%d", duke@435: is_vtable_stub? "vtbl": "itbl", vtable_index, VtableStub::receiver_location()); duke@435: Disassembler::decode(s->code_begin(), s->code_end()); duke@435: } duke@435: } duke@435: return s->entry_point(); duke@435: } duke@435: duke@435: duke@435: inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){ duke@435: // Assumption: receiver_location < 4 in most cases. duke@435: int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index; duke@435: return (is_vtable_stub ? ~hash : hash) & mask; duke@435: } duke@435: duke@435: duke@435: VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) { duke@435: MutexLocker ml(VtableStubs_lock); duke@435: unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index); duke@435: VtableStub* s = _table[hash]; duke@435: while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next(); duke@435: return s; duke@435: } duke@435: duke@435: duke@435: void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) { duke@435: MutexLocker ml(VtableStubs_lock); duke@435: assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub"); duke@435: unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index); duke@435: // enter s at the beginning of the corresponding list duke@435: s->set_next(_table[h]); duke@435: _table[h] = s; duke@435: _number_of_vtable_stubs++; duke@435: } duke@435: duke@435: duke@435: bool VtableStubs::is_entry_point(address pc) { duke@435: MutexLocker ml(VtableStubs_lock); duke@435: VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset()); duke@435: uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index()); duke@435: VtableStub* s; duke@435: for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {} duke@435: return s == stub; duke@435: } duke@435: duke@435: duke@435: bool VtableStubs::contains(address pc) { duke@435: // simple solution for now - we may want to use duke@435: // a faster way if this function is called often duke@435: return stub_containing(pc) != NULL; duke@435: } duke@435: duke@435: duke@435: VtableStub* VtableStubs::stub_containing(address pc) { duke@435: // Note: No locking needed since any change to the data structure duke@435: // happens with an atomic store into it (we don't care about duke@435: // consistency with the _number_of_vtable_stubs counter). duke@435: for (int i = 0; i < N; i++) { duke@435: for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { duke@435: if (s->contains(pc)) return s; duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: void vtableStubs_init() { duke@435: VtableStubs::initialize(); duke@435: } duke@435: duke@435: duke@435: //----------------------------------------------------------------------------------------------------- duke@435: // Non-product code duke@435: #ifndef PRODUCT duke@435: duke@435: extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) { duke@435: ResourceMark rm; duke@435: HandleMark hm; duke@435: klassOop klass = receiver->klass(); duke@435: instanceKlass* ik = instanceKlass::cast(klass); duke@435: klassVtable* vt = ik->vtable(); duke@435: klass->print(); duke@435: fatal3("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", index %d (vtable length %d)", (address)receiver, index, vt->length()); duke@435: } duke@435: duke@435: #endif // Product